fsl_hypervisor.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. * Freescale Hypervisor Management Driver
  3. * Copyright (C) 2008-2011 Freescale Semiconductor, Inc.
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. *
  10. * The Freescale hypervisor management driver provides several services to
  11. * drivers and applications related to the Freescale hypervisor:
  12. *
  13. * 1. An ioctl interface for querying and managing partitions.
  14. *
  15. * 2. A file interface to reading incoming doorbells.
  16. *
  17. * 3. An interrupt handler for shutting down the partition upon receiving the
  18. * shutdown doorbell from a manager partition.
  19. *
  20. * 4. A kernel interface for receiving callbacks when a managed partition
  21. * shuts down.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/types.h>
  27. #include <linux/err.h>
  28. #include <linux/fs.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/mm.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/slab.h>
  33. #include <linux/poll.h>
  34. #include <linux/of.h>
  35. #include <linux/of_irq.h>
  36. #include <linux/reboot.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/notifier.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/io.h>
  41. #include <asm/fsl_hcalls.h>
  42. #include <linux/fsl_hypervisor.h>
  43. static BLOCKING_NOTIFIER_HEAD(failover_subscribers);
  44. /*
  45. * Ioctl interface for FSL_HV_IOCTL_PARTITION_RESTART
  46. *
  47. * Restart a running partition
  48. */
  49. static long ioctl_restart(struct fsl_hv_ioctl_restart __user *p)
  50. {
  51. struct fsl_hv_ioctl_restart param;
  52. /* Get the parameters from the user */
  53. if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_restart)))
  54. return -EFAULT;
  55. param.ret = fh_partition_restart(param.partition);
  56. if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
  57. return -EFAULT;
  58. return 0;
  59. }
  60. /*
  61. * Ioctl interface for FSL_HV_IOCTL_PARTITION_STATUS
  62. *
  63. * Query the status of a partition
  64. */
  65. static long ioctl_status(struct fsl_hv_ioctl_status __user *p)
  66. {
  67. struct fsl_hv_ioctl_status param;
  68. u32 status;
  69. /* Get the parameters from the user */
  70. if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_status)))
  71. return -EFAULT;
  72. param.ret = fh_partition_get_status(param.partition, &status);
  73. if (!param.ret)
  74. param.status = status;
  75. if (copy_to_user(p, &param, sizeof(struct fsl_hv_ioctl_status)))
  76. return -EFAULT;
  77. return 0;
  78. }
  79. /*
  80. * Ioctl interface for FSL_HV_IOCTL_PARTITION_START
  81. *
  82. * Start a stopped partition.
  83. */
  84. static long ioctl_start(struct fsl_hv_ioctl_start __user *p)
  85. {
  86. struct fsl_hv_ioctl_start param;
  87. /* Get the parameters from the user */
  88. if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_start)))
  89. return -EFAULT;
  90. param.ret = fh_partition_start(param.partition, param.entry_point,
  91. param.load);
  92. if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
  93. return -EFAULT;
  94. return 0;
  95. }
  96. /*
  97. * Ioctl interface for FSL_HV_IOCTL_PARTITION_STOP
  98. *
  99. * Stop a running partition
  100. */
  101. static long ioctl_stop(struct fsl_hv_ioctl_stop __user *p)
  102. {
  103. struct fsl_hv_ioctl_stop param;
  104. /* Get the parameters from the user */
  105. if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_stop)))
  106. return -EFAULT;
  107. param.ret = fh_partition_stop(param.partition);
  108. if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
  109. return -EFAULT;
  110. return 0;
  111. }
  112. /*
  113. * Ioctl interface for FSL_HV_IOCTL_MEMCPY
  114. *
  115. * The FH_MEMCPY hypercall takes an array of address/address/size structures
  116. * to represent the data being copied. As a convenience to the user, this
  117. * ioctl takes a user-create buffer and a pointer to a guest physically
  118. * contiguous buffer in the remote partition, and creates the
  119. * address/address/size array for the hypercall.
  120. */
  121. static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
  122. {
  123. struct fsl_hv_ioctl_memcpy param;
  124. struct page **pages = NULL;
  125. void *sg_list_unaligned = NULL;
  126. struct fh_sg_list *sg_list = NULL;
  127. unsigned int num_pages;
  128. unsigned long lb_offset; /* Offset within a page of the local buffer */
  129. unsigned int i;
  130. long ret = 0;
  131. int num_pinned = 0; /* return value from get_user_pages_fast() */
  132. phys_addr_t remote_paddr; /* The next address in the remote buffer */
  133. uint32_t count; /* The number of bytes left to copy */
  134. /* Get the parameters from the user */
  135. if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_memcpy)))
  136. return -EFAULT;
  137. /*
  138. * One partition must be local, the other must be remote. In other
  139. * words, if source and target are both -1, or are both not -1, then
  140. * return an error.
  141. */
  142. if ((param.source == -1) == (param.target == -1))
  143. return -EINVAL;
  144. /*
  145. * The array of pages returned by get_user_pages_fast() covers only
  146. * page-aligned memory. Since the user buffer is probably not
  147. * page-aligned, we need to handle the discrepancy.
  148. *
  149. * We calculate the offset within a page of the S/G list, and make
  150. * adjustments accordingly. This will result in a page list that looks
  151. * like this:
  152. *
  153. * ---- <-- first page starts before the buffer
  154. * | |
  155. * |////|-> ----
  156. * |////| | |
  157. * ---- | |
  158. * | |
  159. * ---- | |
  160. * |////| | |
  161. * |////| | |
  162. * |////| | |
  163. * ---- | |
  164. * | |
  165. * ---- | |
  166. * |////| | |
  167. * |////| | |
  168. * |////| | |
  169. * ---- | |
  170. * | |
  171. * ---- | |
  172. * |////| | |
  173. * |////|-> ----
  174. * | | <-- last page ends after the buffer
  175. * ----
  176. *
  177. * The distance between the start of the first page and the start of the
  178. * buffer is lb_offset. The hashed (///) areas are the parts of the
  179. * page list that contain the actual buffer.
  180. *
  181. * The advantage of this approach is that the number of pages is
  182. * equal to the number of entries in the S/G list that we give to the
  183. * hypervisor.
  184. */
  185. lb_offset = param.local_vaddr & (PAGE_SIZE - 1);
  186. if (param.count == 0 ||
  187. param.count > U64_MAX - lb_offset - PAGE_SIZE + 1)
  188. return -EINVAL;
  189. num_pages = (param.count + lb_offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
  190. /* Allocate the buffers we need */
  191. /*
  192. * 'pages' is an array of struct page pointers that's initialized by
  193. * get_user_pages_fast().
  194. */
  195. pages = kzalloc(num_pages * sizeof(struct page *), GFP_KERNEL);
  196. if (!pages) {
  197. pr_debug("fsl-hv: could not allocate page list\n");
  198. return -ENOMEM;
  199. }
  200. /*
  201. * sg_list is the list of fh_sg_list objects that we pass to the
  202. * hypervisor.
  203. */
  204. sg_list_unaligned = kmalloc(num_pages * sizeof(struct fh_sg_list) +
  205. sizeof(struct fh_sg_list) - 1, GFP_KERNEL);
  206. if (!sg_list_unaligned) {
  207. pr_debug("fsl-hv: could not allocate S/G list\n");
  208. ret = -ENOMEM;
  209. goto free_pages;
  210. }
  211. sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list));
  212. /* Get the physical addresses of the source buffer */
  213. num_pinned = get_user_pages_unlocked(param.local_vaddr - lb_offset,
  214. num_pages, pages, (param.source == -1) ? 0 : FOLL_WRITE);
  215. if (num_pinned != num_pages) {
  216. pr_debug("fsl-hv: could not lock source buffer\n");
  217. ret = (num_pinned < 0) ? num_pinned : -EFAULT;
  218. goto exit;
  219. }
  220. /*
  221. * Build the fh_sg_list[] array. The first page is special
  222. * because it's misaligned.
  223. */
  224. if (param.source == -1) {
  225. sg_list[0].source = page_to_phys(pages[0]) + lb_offset;
  226. sg_list[0].target = param.remote_paddr;
  227. } else {
  228. sg_list[0].source = param.remote_paddr;
  229. sg_list[0].target = page_to_phys(pages[0]) + lb_offset;
  230. }
  231. sg_list[0].size = min_t(uint64_t, param.count, PAGE_SIZE - lb_offset);
  232. remote_paddr = param.remote_paddr + sg_list[0].size;
  233. count = param.count - sg_list[0].size;
  234. for (i = 1; i < num_pages; i++) {
  235. if (param.source == -1) {
  236. /* local to remote */
  237. sg_list[i].source = page_to_phys(pages[i]);
  238. sg_list[i].target = remote_paddr;
  239. } else {
  240. /* remote to local */
  241. sg_list[i].source = remote_paddr;
  242. sg_list[i].target = page_to_phys(pages[i]);
  243. }
  244. sg_list[i].size = min_t(uint64_t, count, PAGE_SIZE);
  245. remote_paddr += sg_list[i].size;
  246. count -= sg_list[i].size;
  247. }
  248. param.ret = fh_partition_memcpy(param.source, param.target,
  249. virt_to_phys(sg_list), num_pages);
  250. exit:
  251. if (pages && (num_pinned > 0)) {
  252. for (i = 0; i < num_pinned; i++)
  253. put_page(pages[i]);
  254. }
  255. kfree(sg_list_unaligned);
  256. free_pages:
  257. kfree(pages);
  258. if (!ret)
  259. if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
  260. return -EFAULT;
  261. return ret;
  262. }
  263. /*
  264. * Ioctl interface for FSL_HV_IOCTL_DOORBELL
  265. *
  266. * Ring a doorbell
  267. */
  268. static long ioctl_doorbell(struct fsl_hv_ioctl_doorbell __user *p)
  269. {
  270. struct fsl_hv_ioctl_doorbell param;
  271. /* Get the parameters from the user. */
  272. if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_doorbell)))
  273. return -EFAULT;
  274. param.ret = ev_doorbell_send(param.doorbell);
  275. if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
  276. return -EFAULT;
  277. return 0;
  278. }
  279. static long ioctl_dtprop(struct fsl_hv_ioctl_prop __user *p, int set)
  280. {
  281. struct fsl_hv_ioctl_prop param;
  282. char __user *upath, *upropname;
  283. void __user *upropval;
  284. char *path, *propname;
  285. void *propval;
  286. int ret = 0;
  287. /* Get the parameters from the user. */
  288. if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_prop)))
  289. return -EFAULT;
  290. upath = (char __user *)(uintptr_t)param.path;
  291. upropname = (char __user *)(uintptr_t)param.propname;
  292. upropval = (void __user *)(uintptr_t)param.propval;
  293. path = strndup_user(upath, FH_DTPROP_MAX_PATHLEN);
  294. if (IS_ERR(path))
  295. return PTR_ERR(path);
  296. propname = strndup_user(upropname, FH_DTPROP_MAX_PATHLEN);
  297. if (IS_ERR(propname)) {
  298. ret = PTR_ERR(propname);
  299. goto err_free_path;
  300. }
  301. if (param.proplen > FH_DTPROP_MAX_PROPLEN) {
  302. ret = -EINVAL;
  303. goto err_free_propname;
  304. }
  305. propval = kmalloc(param.proplen, GFP_KERNEL);
  306. if (!propval) {
  307. ret = -ENOMEM;
  308. goto err_free_propname;
  309. }
  310. if (set) {
  311. if (copy_from_user(propval, upropval, param.proplen)) {
  312. ret = -EFAULT;
  313. goto err_free_propval;
  314. }
  315. param.ret = fh_partition_set_dtprop(param.handle,
  316. virt_to_phys(path),
  317. virt_to_phys(propname),
  318. virt_to_phys(propval),
  319. param.proplen);
  320. } else {
  321. param.ret = fh_partition_get_dtprop(param.handle,
  322. virt_to_phys(path),
  323. virt_to_phys(propname),
  324. virt_to_phys(propval),
  325. &param.proplen);
  326. if (param.ret == 0) {
  327. if (copy_to_user(upropval, propval, param.proplen) ||
  328. put_user(param.proplen, &p->proplen)) {
  329. ret = -EFAULT;
  330. goto err_free_propval;
  331. }
  332. }
  333. }
  334. if (put_user(param.ret, &p->ret))
  335. ret = -EFAULT;
  336. err_free_propval:
  337. kfree(propval);
  338. err_free_propname:
  339. kfree(propname);
  340. err_free_path:
  341. kfree(path);
  342. return ret;
  343. }
  344. /*
  345. * Ioctl main entry point
  346. */
  347. static long fsl_hv_ioctl(struct file *file, unsigned int cmd,
  348. unsigned long argaddr)
  349. {
  350. void __user *arg = (void __user *)argaddr;
  351. long ret;
  352. switch (cmd) {
  353. case FSL_HV_IOCTL_PARTITION_RESTART:
  354. ret = ioctl_restart(arg);
  355. break;
  356. case FSL_HV_IOCTL_PARTITION_GET_STATUS:
  357. ret = ioctl_status(arg);
  358. break;
  359. case FSL_HV_IOCTL_PARTITION_START:
  360. ret = ioctl_start(arg);
  361. break;
  362. case FSL_HV_IOCTL_PARTITION_STOP:
  363. ret = ioctl_stop(arg);
  364. break;
  365. case FSL_HV_IOCTL_MEMCPY:
  366. ret = ioctl_memcpy(arg);
  367. break;
  368. case FSL_HV_IOCTL_DOORBELL:
  369. ret = ioctl_doorbell(arg);
  370. break;
  371. case FSL_HV_IOCTL_GETPROP:
  372. ret = ioctl_dtprop(arg, 0);
  373. break;
  374. case FSL_HV_IOCTL_SETPROP:
  375. ret = ioctl_dtprop(arg, 1);
  376. break;
  377. default:
  378. pr_debug("fsl-hv: bad ioctl dir=%u type=%u cmd=%u size=%u\n",
  379. _IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd),
  380. _IOC_SIZE(cmd));
  381. return -ENOTTY;
  382. }
  383. return ret;
  384. }
  385. /* Linked list of processes that have us open */
  386. static struct list_head db_list;
  387. /* spinlock for db_list */
  388. static DEFINE_SPINLOCK(db_list_lock);
  389. /* The size of the doorbell event queue. This must be a power of two. */
  390. #define QSIZE 16
  391. /* Returns the next head/tail pointer, wrapping around the queue if necessary */
  392. #define nextp(x) (((x) + 1) & (QSIZE - 1))
  393. /* Per-open data structure */
  394. struct doorbell_queue {
  395. struct list_head list;
  396. spinlock_t lock;
  397. wait_queue_head_t wait;
  398. unsigned int head;
  399. unsigned int tail;
  400. uint32_t q[QSIZE];
  401. };
  402. /* Linked list of ISRs that we registered */
  403. struct list_head isr_list;
  404. /* Per-ISR data structure */
  405. struct doorbell_isr {
  406. struct list_head list;
  407. unsigned int irq;
  408. uint32_t doorbell; /* The doorbell handle */
  409. uint32_t partition; /* The partition handle, if used */
  410. };
  411. /*
  412. * Add a doorbell to all of the doorbell queues
  413. */
  414. static void fsl_hv_queue_doorbell(uint32_t doorbell)
  415. {
  416. struct doorbell_queue *dbq;
  417. unsigned long flags;
  418. /* Prevent another core from modifying db_list */
  419. spin_lock_irqsave(&db_list_lock, flags);
  420. list_for_each_entry(dbq, &db_list, list) {
  421. if (dbq->head != nextp(dbq->tail)) {
  422. dbq->q[dbq->tail] = doorbell;
  423. /*
  424. * This memory barrier eliminates the need to grab
  425. * the spinlock for dbq.
  426. */
  427. smp_wmb();
  428. dbq->tail = nextp(dbq->tail);
  429. wake_up_interruptible(&dbq->wait);
  430. }
  431. }
  432. spin_unlock_irqrestore(&db_list_lock, flags);
  433. }
  434. /*
  435. * Interrupt handler for all doorbells
  436. *
  437. * We use the same interrupt handler for all doorbells. Whenever a doorbell
  438. * is rung, and we receive an interrupt, we just put the handle for that
  439. * doorbell (passed to us as *data) into all of the queues.
  440. */
  441. static irqreturn_t fsl_hv_isr(int irq, void *data)
  442. {
  443. fsl_hv_queue_doorbell((uintptr_t) data);
  444. return IRQ_HANDLED;
  445. }
  446. /*
  447. * State change thread function
  448. *
  449. * The state change notification arrives in an interrupt, but we can't call
  450. * blocking_notifier_call_chain() in an interrupt handler. We could call
  451. * atomic_notifier_call_chain(), but that would require the clients' call-back
  452. * function to run in interrupt context. Since we don't want to impose that
  453. * restriction on the clients, we use a threaded IRQ to process the
  454. * notification in kernel context.
  455. */
  456. static irqreturn_t fsl_hv_state_change_thread(int irq, void *data)
  457. {
  458. struct doorbell_isr *dbisr = data;
  459. blocking_notifier_call_chain(&failover_subscribers, dbisr->partition,
  460. NULL);
  461. return IRQ_HANDLED;
  462. }
  463. /*
  464. * Interrupt handler for state-change doorbells
  465. */
  466. static irqreturn_t fsl_hv_state_change_isr(int irq, void *data)
  467. {
  468. unsigned int status;
  469. struct doorbell_isr *dbisr = data;
  470. int ret;
  471. /* It's still a doorbell, so add it to all the queues. */
  472. fsl_hv_queue_doorbell(dbisr->doorbell);
  473. /* Determine the new state, and if it's stopped, notify the clients. */
  474. ret = fh_partition_get_status(dbisr->partition, &status);
  475. if (!ret && (status == FH_PARTITION_STOPPED))
  476. return IRQ_WAKE_THREAD;
  477. return IRQ_HANDLED;
  478. }
  479. /*
  480. * Returns a bitmask indicating whether a read will block
  481. */
  482. static unsigned int fsl_hv_poll(struct file *filp, struct poll_table_struct *p)
  483. {
  484. struct doorbell_queue *dbq = filp->private_data;
  485. unsigned long flags;
  486. unsigned int mask;
  487. spin_lock_irqsave(&dbq->lock, flags);
  488. poll_wait(filp, &dbq->wait, p);
  489. mask = (dbq->head == dbq->tail) ? 0 : (POLLIN | POLLRDNORM);
  490. spin_unlock_irqrestore(&dbq->lock, flags);
  491. return mask;
  492. }
  493. /*
  494. * Return the handles for any incoming doorbells
  495. *
  496. * If there are doorbell handles in the queue for this open instance, then
  497. * return them to the caller as an array of 32-bit integers. Otherwise,
  498. * block until there is at least one handle to return.
  499. */
  500. static ssize_t fsl_hv_read(struct file *filp, char __user *buf, size_t len,
  501. loff_t *off)
  502. {
  503. struct doorbell_queue *dbq = filp->private_data;
  504. uint32_t __user *p = (uint32_t __user *) buf; /* for put_user() */
  505. unsigned long flags;
  506. ssize_t count = 0;
  507. /* Make sure we stop when the user buffer is full. */
  508. while (len >= sizeof(uint32_t)) {
  509. uint32_t dbell; /* Local copy of doorbell queue data */
  510. spin_lock_irqsave(&dbq->lock, flags);
  511. /*
  512. * If the queue is empty, then either we're done or we need
  513. * to block. If the application specified O_NONBLOCK, then
  514. * we return the appropriate error code.
  515. */
  516. if (dbq->head == dbq->tail) {
  517. spin_unlock_irqrestore(&dbq->lock, flags);
  518. if (count)
  519. break;
  520. if (filp->f_flags & O_NONBLOCK)
  521. return -EAGAIN;
  522. if (wait_event_interruptible(dbq->wait,
  523. dbq->head != dbq->tail))
  524. return -ERESTARTSYS;
  525. continue;
  526. }
  527. /*
  528. * Even though we have an smp_wmb() in the ISR, the core
  529. * might speculatively execute the "dbell = ..." below while
  530. * it's evaluating the if-statement above. In that case, the
  531. * value put into dbell could be stale if the core accepts the
  532. * speculation. To prevent that, we need a read memory barrier
  533. * here as well.
  534. */
  535. smp_rmb();
  536. /* Copy the data to a temporary local buffer, because
  537. * we can't call copy_to_user() from inside a spinlock
  538. */
  539. dbell = dbq->q[dbq->head];
  540. dbq->head = nextp(dbq->head);
  541. spin_unlock_irqrestore(&dbq->lock, flags);
  542. if (put_user(dbell, p))
  543. return -EFAULT;
  544. p++;
  545. count += sizeof(uint32_t);
  546. len -= sizeof(uint32_t);
  547. }
  548. return count;
  549. }
  550. /*
  551. * Open the driver and prepare for reading doorbells.
  552. *
  553. * Every time an application opens the driver, we create a doorbell queue
  554. * for that file handle. This queue is used for any incoming doorbells.
  555. */
  556. static int fsl_hv_open(struct inode *inode, struct file *filp)
  557. {
  558. struct doorbell_queue *dbq;
  559. unsigned long flags;
  560. int ret = 0;
  561. dbq = kzalloc(sizeof(struct doorbell_queue), GFP_KERNEL);
  562. if (!dbq) {
  563. pr_err("fsl-hv: out of memory\n");
  564. return -ENOMEM;
  565. }
  566. spin_lock_init(&dbq->lock);
  567. init_waitqueue_head(&dbq->wait);
  568. spin_lock_irqsave(&db_list_lock, flags);
  569. list_add(&dbq->list, &db_list);
  570. spin_unlock_irqrestore(&db_list_lock, flags);
  571. filp->private_data = dbq;
  572. return ret;
  573. }
  574. /*
  575. * Close the driver
  576. */
  577. static int fsl_hv_close(struct inode *inode, struct file *filp)
  578. {
  579. struct doorbell_queue *dbq = filp->private_data;
  580. unsigned long flags;
  581. int ret = 0;
  582. spin_lock_irqsave(&db_list_lock, flags);
  583. list_del(&dbq->list);
  584. spin_unlock_irqrestore(&db_list_lock, flags);
  585. kfree(dbq);
  586. return ret;
  587. }
  588. static const struct file_operations fsl_hv_fops = {
  589. .owner = THIS_MODULE,
  590. .open = fsl_hv_open,
  591. .release = fsl_hv_close,
  592. .poll = fsl_hv_poll,
  593. .read = fsl_hv_read,
  594. .unlocked_ioctl = fsl_hv_ioctl,
  595. .compat_ioctl = fsl_hv_ioctl,
  596. };
  597. static struct miscdevice fsl_hv_misc_dev = {
  598. MISC_DYNAMIC_MINOR,
  599. "fsl-hv",
  600. &fsl_hv_fops
  601. };
  602. static irqreturn_t fsl_hv_shutdown_isr(int irq, void *data)
  603. {
  604. orderly_poweroff(false);
  605. return IRQ_HANDLED;
  606. }
  607. /*
  608. * Returns the handle of the parent of the given node
  609. *
  610. * The handle is the value of the 'hv-handle' property
  611. */
  612. static int get_parent_handle(struct device_node *np)
  613. {
  614. struct device_node *parent;
  615. const uint32_t *prop;
  616. uint32_t handle;
  617. int len;
  618. parent = of_get_parent(np);
  619. if (!parent)
  620. /* It's not really possible for this to fail */
  621. return -ENODEV;
  622. /*
  623. * The proper name for the handle property is "hv-handle", but some
  624. * older versions of the hypervisor used "reg".
  625. */
  626. prop = of_get_property(parent, "hv-handle", &len);
  627. if (!prop)
  628. prop = of_get_property(parent, "reg", &len);
  629. if (!prop || (len != sizeof(uint32_t))) {
  630. /* This can happen only if the node is malformed */
  631. of_node_put(parent);
  632. return -ENODEV;
  633. }
  634. handle = be32_to_cpup(prop);
  635. of_node_put(parent);
  636. return handle;
  637. }
  638. /*
  639. * Register a callback for failover events
  640. *
  641. * This function is called by device drivers to register their callback
  642. * functions for fail-over events.
  643. */
  644. int fsl_hv_failover_register(struct notifier_block *nb)
  645. {
  646. return blocking_notifier_chain_register(&failover_subscribers, nb);
  647. }
  648. EXPORT_SYMBOL(fsl_hv_failover_register);
  649. /*
  650. * Unregister a callback for failover events
  651. */
  652. int fsl_hv_failover_unregister(struct notifier_block *nb)
  653. {
  654. return blocking_notifier_chain_unregister(&failover_subscribers, nb);
  655. }
  656. EXPORT_SYMBOL(fsl_hv_failover_unregister);
  657. /*
  658. * Return TRUE if we're running under FSL hypervisor
  659. *
  660. * This function checks to see if we're running under the Freescale
  661. * hypervisor, and returns zero if we're not, or non-zero if we are.
  662. *
  663. * First, it checks if MSR[GS]==1, which means we're running under some
  664. * hypervisor. Then it checks if there is a hypervisor node in the device
  665. * tree. Currently, that means there needs to be a node in the root called
  666. * "hypervisor" and which has a property named "fsl,hv-version".
  667. */
  668. static int has_fsl_hypervisor(void)
  669. {
  670. struct device_node *node;
  671. int ret;
  672. node = of_find_node_by_path("/hypervisor");
  673. if (!node)
  674. return 0;
  675. ret = of_find_property(node, "fsl,hv-version", NULL) != NULL;
  676. of_node_put(node);
  677. return ret;
  678. }
  679. /*
  680. * Freescale hypervisor management driver init
  681. *
  682. * This function is called when this module is loaded.
  683. *
  684. * Register ourselves as a miscellaneous driver. This will register the
  685. * fops structure and create the right sysfs entries for udev.
  686. */
  687. static int __init fsl_hypervisor_init(void)
  688. {
  689. struct device_node *np;
  690. struct doorbell_isr *dbisr, *n;
  691. int ret;
  692. pr_info("Freescale hypervisor management driver\n");
  693. if (!has_fsl_hypervisor()) {
  694. pr_info("fsl-hv: no hypervisor found\n");
  695. return -ENODEV;
  696. }
  697. ret = misc_register(&fsl_hv_misc_dev);
  698. if (ret) {
  699. pr_err("fsl-hv: cannot register device\n");
  700. return ret;
  701. }
  702. INIT_LIST_HEAD(&db_list);
  703. INIT_LIST_HEAD(&isr_list);
  704. for_each_compatible_node(np, NULL, "epapr,hv-receive-doorbell") {
  705. unsigned int irq;
  706. const uint32_t *handle;
  707. handle = of_get_property(np, "interrupts", NULL);
  708. irq = irq_of_parse_and_map(np, 0);
  709. if (!handle || (irq == NO_IRQ)) {
  710. pr_err("fsl-hv: no 'interrupts' property in %pOF node\n",
  711. np);
  712. continue;
  713. }
  714. dbisr = kzalloc(sizeof(*dbisr), GFP_KERNEL);
  715. if (!dbisr)
  716. goto out_of_memory;
  717. dbisr->irq = irq;
  718. dbisr->doorbell = be32_to_cpup(handle);
  719. if (of_device_is_compatible(np, "fsl,hv-shutdown-doorbell")) {
  720. /* The shutdown doorbell gets its own ISR */
  721. ret = request_irq(irq, fsl_hv_shutdown_isr, 0,
  722. np->name, NULL);
  723. } else if (of_device_is_compatible(np,
  724. "fsl,hv-state-change-doorbell")) {
  725. /*
  726. * The state change doorbell triggers a notification if
  727. * the state of the managed partition changes to
  728. * "stopped". We need a separate interrupt handler for
  729. * that, and we also need to know the handle of the
  730. * target partition, not just the handle of the
  731. * doorbell.
  732. */
  733. dbisr->partition = ret = get_parent_handle(np);
  734. if (ret < 0) {
  735. pr_err("fsl-hv: node %pOF has missing or "
  736. "malformed parent\n", np);
  737. kfree(dbisr);
  738. continue;
  739. }
  740. ret = request_threaded_irq(irq, fsl_hv_state_change_isr,
  741. fsl_hv_state_change_thread,
  742. 0, np->name, dbisr);
  743. } else
  744. ret = request_irq(irq, fsl_hv_isr, 0, np->name, dbisr);
  745. if (ret < 0) {
  746. pr_err("fsl-hv: could not request irq %u for node %pOF\n",
  747. irq, np);
  748. kfree(dbisr);
  749. continue;
  750. }
  751. list_add(&dbisr->list, &isr_list);
  752. pr_info("fsl-hv: registered handler for doorbell %u\n",
  753. dbisr->doorbell);
  754. }
  755. return 0;
  756. out_of_memory:
  757. list_for_each_entry_safe(dbisr, n, &isr_list, list) {
  758. free_irq(dbisr->irq, dbisr);
  759. list_del(&dbisr->list);
  760. kfree(dbisr);
  761. }
  762. misc_deregister(&fsl_hv_misc_dev);
  763. return -ENOMEM;
  764. }
  765. /*
  766. * Freescale hypervisor management driver termination
  767. *
  768. * This function is called when this driver is unloaded.
  769. */
  770. static void __exit fsl_hypervisor_exit(void)
  771. {
  772. struct doorbell_isr *dbisr, *n;
  773. list_for_each_entry_safe(dbisr, n, &isr_list, list) {
  774. free_irq(dbisr->irq, dbisr);
  775. list_del(&dbisr->list);
  776. kfree(dbisr);
  777. }
  778. misc_deregister(&fsl_hv_misc_dev);
  779. }
  780. module_init(fsl_hypervisor_init);
  781. module_exit(fsl_hypervisor_exit);
  782. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  783. MODULE_DESCRIPTION("Freescale hypervisor management driver");
  784. MODULE_LICENSE("GPL v2");