fsl_hypervisor.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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; /* return value from get_user_pages() */
  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() 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. num_pages = (param.count + lb_offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
  187. /* Allocate the buffers we need */
  188. /*
  189. * 'pages' is an array of struct page pointers that's initialized by
  190. * get_user_pages().
  191. */
  192. pages = kzalloc(num_pages * sizeof(struct page *), GFP_KERNEL);
  193. if (!pages) {
  194. pr_debug("fsl-hv: could not allocate page list\n");
  195. return -ENOMEM;
  196. }
  197. /*
  198. * sg_list is the list of fh_sg_list objects that we pass to the
  199. * hypervisor.
  200. */
  201. sg_list_unaligned = kmalloc(num_pages * sizeof(struct fh_sg_list) +
  202. sizeof(struct fh_sg_list) - 1, GFP_KERNEL);
  203. if (!sg_list_unaligned) {
  204. pr_debug("fsl-hv: could not allocate S/G list\n");
  205. ret = -ENOMEM;
  206. goto exit;
  207. }
  208. sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list));
  209. /* Get the physical addresses of the source buffer */
  210. down_read(&current->mm->mmap_sem);
  211. num_pinned = get_user_pages(current, current->mm,
  212. param.local_vaddr - lb_offset, num_pages,
  213. (param.source == -1) ? 0 : FOLL_WRITE,
  214. pages, NULL);
  215. up_read(&current->mm->mmap_sem);
  216. if (num_pinned != num_pages) {
  217. /* get_user_pages() failed */
  218. pr_debug("fsl-hv: could not lock source buffer\n");
  219. ret = (num_pinned < 0) ? num_pinned : -EFAULT;
  220. goto exit;
  221. }
  222. /*
  223. * Build the fh_sg_list[] array. The first page is special
  224. * because it's misaligned.
  225. */
  226. if (param.source == -1) {
  227. sg_list[0].source = page_to_phys(pages[0]) + lb_offset;
  228. sg_list[0].target = param.remote_paddr;
  229. } else {
  230. sg_list[0].source = param.remote_paddr;
  231. sg_list[0].target = page_to_phys(pages[0]) + lb_offset;
  232. }
  233. sg_list[0].size = min_t(uint64_t, param.count, PAGE_SIZE - lb_offset);
  234. remote_paddr = param.remote_paddr + sg_list[0].size;
  235. count = param.count - sg_list[0].size;
  236. for (i = 1; i < num_pages; i++) {
  237. if (param.source == -1) {
  238. /* local to remote */
  239. sg_list[i].source = page_to_phys(pages[i]);
  240. sg_list[i].target = remote_paddr;
  241. } else {
  242. /* remote to local */
  243. sg_list[i].source = remote_paddr;
  244. sg_list[i].target = page_to_phys(pages[i]);
  245. }
  246. sg_list[i].size = min_t(uint64_t, count, PAGE_SIZE);
  247. remote_paddr += sg_list[i].size;
  248. count -= sg_list[i].size;
  249. }
  250. param.ret = fh_partition_memcpy(param.source, param.target,
  251. virt_to_phys(sg_list), num_pages);
  252. exit:
  253. if (pages) {
  254. for (i = 0; i < num_pages; i++)
  255. if (pages[i])
  256. put_page(pages[i]);
  257. }
  258. kfree(sg_list_unaligned);
  259. kfree(pages);
  260. if (!ret)
  261. if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
  262. return -EFAULT;
  263. return ret;
  264. }
  265. /*
  266. * Ioctl interface for FSL_HV_IOCTL_DOORBELL
  267. *
  268. * Ring a doorbell
  269. */
  270. static long ioctl_doorbell(struct fsl_hv_ioctl_doorbell __user *p)
  271. {
  272. struct fsl_hv_ioctl_doorbell param;
  273. /* Get the parameters from the user. */
  274. if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_doorbell)))
  275. return -EFAULT;
  276. param.ret = ev_doorbell_send(param.doorbell);
  277. if (copy_to_user(&p->ret, &param.ret, sizeof(__u32)))
  278. return -EFAULT;
  279. return 0;
  280. }
  281. static long ioctl_dtprop(struct fsl_hv_ioctl_prop __user *p, int set)
  282. {
  283. struct fsl_hv_ioctl_prop param;
  284. char __user *upath, *upropname;
  285. void __user *upropval;
  286. char *path = NULL, *propname = NULL;
  287. void *propval = NULL;
  288. int ret = 0;
  289. /* Get the parameters from the user. */
  290. if (copy_from_user(&param, p, sizeof(struct fsl_hv_ioctl_prop)))
  291. return -EFAULT;
  292. upath = (char __user *)(uintptr_t)param.path;
  293. upropname = (char __user *)(uintptr_t)param.propname;
  294. upropval = (void __user *)(uintptr_t)param.propval;
  295. path = strndup_user(upath, FH_DTPROP_MAX_PATHLEN);
  296. if (IS_ERR(path)) {
  297. ret = PTR_ERR(path);
  298. goto out;
  299. }
  300. propname = strndup_user(upropname, FH_DTPROP_MAX_PATHLEN);
  301. if (IS_ERR(propname)) {
  302. ret = PTR_ERR(propname);
  303. goto out;
  304. }
  305. if (param.proplen > FH_DTPROP_MAX_PROPLEN) {
  306. ret = -EINVAL;
  307. goto out;
  308. }
  309. propval = kmalloc(param.proplen, GFP_KERNEL);
  310. if (!propval) {
  311. ret = -ENOMEM;
  312. goto out;
  313. }
  314. if (set) {
  315. if (copy_from_user(propval, upropval, param.proplen)) {
  316. ret = -EFAULT;
  317. goto out;
  318. }
  319. param.ret = fh_partition_set_dtprop(param.handle,
  320. virt_to_phys(path),
  321. virt_to_phys(propname),
  322. virt_to_phys(propval),
  323. param.proplen);
  324. } else {
  325. param.ret = fh_partition_get_dtprop(param.handle,
  326. virt_to_phys(path),
  327. virt_to_phys(propname),
  328. virt_to_phys(propval),
  329. &param.proplen);
  330. if (param.ret == 0) {
  331. if (copy_to_user(upropval, propval, param.proplen) ||
  332. put_user(param.proplen, &p->proplen)) {
  333. ret = -EFAULT;
  334. goto out;
  335. }
  336. }
  337. }
  338. if (put_user(param.ret, &p->ret))
  339. ret = -EFAULT;
  340. out:
  341. kfree(path);
  342. kfree(propval);
  343. kfree(propname);
  344. return ret;
  345. }
  346. /*
  347. * Ioctl main entry point
  348. */
  349. static long fsl_hv_ioctl(struct file *file, unsigned int cmd,
  350. unsigned long argaddr)
  351. {
  352. void __user *arg = (void __user *)argaddr;
  353. long ret;
  354. switch (cmd) {
  355. case FSL_HV_IOCTL_PARTITION_RESTART:
  356. ret = ioctl_restart(arg);
  357. break;
  358. case FSL_HV_IOCTL_PARTITION_GET_STATUS:
  359. ret = ioctl_status(arg);
  360. break;
  361. case FSL_HV_IOCTL_PARTITION_START:
  362. ret = ioctl_start(arg);
  363. break;
  364. case FSL_HV_IOCTL_PARTITION_STOP:
  365. ret = ioctl_stop(arg);
  366. break;
  367. case FSL_HV_IOCTL_MEMCPY:
  368. ret = ioctl_memcpy(arg);
  369. break;
  370. case FSL_HV_IOCTL_DOORBELL:
  371. ret = ioctl_doorbell(arg);
  372. break;
  373. case FSL_HV_IOCTL_GETPROP:
  374. ret = ioctl_dtprop(arg, 0);
  375. break;
  376. case FSL_HV_IOCTL_SETPROP:
  377. ret = ioctl_dtprop(arg, 1);
  378. break;
  379. default:
  380. pr_debug("fsl-hv: bad ioctl dir=%u type=%u cmd=%u size=%u\n",
  381. _IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd),
  382. _IOC_SIZE(cmd));
  383. return -ENOTTY;
  384. }
  385. return ret;
  386. }
  387. /* Linked list of processes that have us open */
  388. static struct list_head db_list;
  389. /* spinlock for db_list */
  390. static DEFINE_SPINLOCK(db_list_lock);
  391. /* The size of the doorbell event queue. This must be a power of two. */
  392. #define QSIZE 16
  393. /* Returns the next head/tail pointer, wrapping around the queue if necessary */
  394. #define nextp(x) (((x) + 1) & (QSIZE - 1))
  395. /* Per-open data structure */
  396. struct doorbell_queue {
  397. struct list_head list;
  398. spinlock_t lock;
  399. wait_queue_head_t wait;
  400. unsigned int head;
  401. unsigned int tail;
  402. uint32_t q[QSIZE];
  403. };
  404. /* Linked list of ISRs that we registered */
  405. struct list_head isr_list;
  406. /* Per-ISR data structure */
  407. struct doorbell_isr {
  408. struct list_head list;
  409. unsigned int irq;
  410. uint32_t doorbell; /* The doorbell handle */
  411. uint32_t partition; /* The partition handle, if used */
  412. };
  413. /*
  414. * Add a doorbell to all of the doorbell queues
  415. */
  416. static void fsl_hv_queue_doorbell(uint32_t doorbell)
  417. {
  418. struct doorbell_queue *dbq;
  419. unsigned long flags;
  420. /* Prevent another core from modifying db_list */
  421. spin_lock_irqsave(&db_list_lock, flags);
  422. list_for_each_entry(dbq, &db_list, list) {
  423. if (dbq->head != nextp(dbq->tail)) {
  424. dbq->q[dbq->tail] = doorbell;
  425. /*
  426. * This memory barrier eliminates the need to grab
  427. * the spinlock for dbq.
  428. */
  429. smp_wmb();
  430. dbq->tail = nextp(dbq->tail);
  431. wake_up_interruptible(&dbq->wait);
  432. }
  433. }
  434. spin_unlock_irqrestore(&db_list_lock, flags);
  435. }
  436. /*
  437. * Interrupt handler for all doorbells
  438. *
  439. * We use the same interrupt handler for all doorbells. Whenever a doorbell
  440. * is rung, and we receive an interrupt, we just put the handle for that
  441. * doorbell (passed to us as *data) into all of the queues.
  442. */
  443. static irqreturn_t fsl_hv_isr(int irq, void *data)
  444. {
  445. fsl_hv_queue_doorbell((uintptr_t) data);
  446. return IRQ_HANDLED;
  447. }
  448. /*
  449. * State change thread function
  450. *
  451. * The state change notification arrives in an interrupt, but we can't call
  452. * blocking_notifier_call_chain() in an interrupt handler. We could call
  453. * atomic_notifier_call_chain(), but that would require the clients' call-back
  454. * function to run in interrupt context. Since we don't want to impose that
  455. * restriction on the clients, we use a threaded IRQ to process the
  456. * notification in kernel context.
  457. */
  458. static irqreturn_t fsl_hv_state_change_thread(int irq, void *data)
  459. {
  460. struct doorbell_isr *dbisr = data;
  461. blocking_notifier_call_chain(&failover_subscribers, dbisr->partition,
  462. NULL);
  463. return IRQ_HANDLED;
  464. }
  465. /*
  466. * Interrupt handler for state-change doorbells
  467. */
  468. static irqreturn_t fsl_hv_state_change_isr(int irq, void *data)
  469. {
  470. unsigned int status;
  471. struct doorbell_isr *dbisr = data;
  472. int ret;
  473. /* It's still a doorbell, so add it to all the queues. */
  474. fsl_hv_queue_doorbell(dbisr->doorbell);
  475. /* Determine the new state, and if it's stopped, notify the clients. */
  476. ret = fh_partition_get_status(dbisr->partition, &status);
  477. if (!ret && (status == FH_PARTITION_STOPPED))
  478. return IRQ_WAKE_THREAD;
  479. return IRQ_HANDLED;
  480. }
  481. /*
  482. * Returns a bitmask indicating whether a read will block
  483. */
  484. static unsigned int fsl_hv_poll(struct file *filp, struct poll_table_struct *p)
  485. {
  486. struct doorbell_queue *dbq = filp->private_data;
  487. unsigned long flags;
  488. unsigned int mask;
  489. spin_lock_irqsave(&dbq->lock, flags);
  490. poll_wait(filp, &dbq->wait, p);
  491. mask = (dbq->head == dbq->tail) ? 0 : (POLLIN | POLLRDNORM);
  492. spin_unlock_irqrestore(&dbq->lock, flags);
  493. return mask;
  494. }
  495. /*
  496. * Return the handles for any incoming doorbells
  497. *
  498. * If there are doorbell handles in the queue for this open instance, then
  499. * return them to the caller as an array of 32-bit integers. Otherwise,
  500. * block until there is at least one handle to return.
  501. */
  502. static ssize_t fsl_hv_read(struct file *filp, char __user *buf, size_t len,
  503. loff_t *off)
  504. {
  505. struct doorbell_queue *dbq = filp->private_data;
  506. uint32_t __user *p = (uint32_t __user *) buf; /* for put_user() */
  507. unsigned long flags;
  508. ssize_t count = 0;
  509. /* Make sure we stop when the user buffer is full. */
  510. while (len >= sizeof(uint32_t)) {
  511. uint32_t dbell; /* Local copy of doorbell queue data */
  512. spin_lock_irqsave(&dbq->lock, flags);
  513. /*
  514. * If the queue is empty, then either we're done or we need
  515. * to block. If the application specified O_NONBLOCK, then
  516. * we return the appropriate error code.
  517. */
  518. if (dbq->head == dbq->tail) {
  519. spin_unlock_irqrestore(&dbq->lock, flags);
  520. if (count)
  521. break;
  522. if (filp->f_flags & O_NONBLOCK)
  523. return -EAGAIN;
  524. if (wait_event_interruptible(dbq->wait,
  525. dbq->head != dbq->tail))
  526. return -ERESTARTSYS;
  527. continue;
  528. }
  529. /*
  530. * Even though we have an smp_wmb() in the ISR, the core
  531. * might speculatively execute the "dbell = ..." below while
  532. * it's evaluating the if-statement above. In that case, the
  533. * value put into dbell could be stale if the core accepts the
  534. * speculation. To prevent that, we need a read memory barrier
  535. * here as well.
  536. */
  537. smp_rmb();
  538. /* Copy the data to a temporary local buffer, because
  539. * we can't call copy_to_user() from inside a spinlock
  540. */
  541. dbell = dbq->q[dbq->head];
  542. dbq->head = nextp(dbq->head);
  543. spin_unlock_irqrestore(&dbq->lock, flags);
  544. if (put_user(dbell, p))
  545. return -EFAULT;
  546. p++;
  547. count += sizeof(uint32_t);
  548. len -= sizeof(uint32_t);
  549. }
  550. return count;
  551. }
  552. /*
  553. * Open the driver and prepare for reading doorbells.
  554. *
  555. * Every time an application opens the driver, we create a doorbell queue
  556. * for that file handle. This queue is used for any incoming doorbells.
  557. */
  558. static int fsl_hv_open(struct inode *inode, struct file *filp)
  559. {
  560. struct doorbell_queue *dbq;
  561. unsigned long flags;
  562. int ret = 0;
  563. dbq = kzalloc(sizeof(struct doorbell_queue), GFP_KERNEL);
  564. if (!dbq) {
  565. pr_err("fsl-hv: out of memory\n");
  566. return -ENOMEM;
  567. }
  568. spin_lock_init(&dbq->lock);
  569. init_waitqueue_head(&dbq->wait);
  570. spin_lock_irqsave(&db_list_lock, flags);
  571. list_add(&dbq->list, &db_list);
  572. spin_unlock_irqrestore(&db_list_lock, flags);
  573. filp->private_data = dbq;
  574. return ret;
  575. }
  576. /*
  577. * Close the driver
  578. */
  579. static int fsl_hv_close(struct inode *inode, struct file *filp)
  580. {
  581. struct doorbell_queue *dbq = filp->private_data;
  582. unsigned long flags;
  583. int ret = 0;
  584. spin_lock_irqsave(&db_list_lock, flags);
  585. list_del(&dbq->list);
  586. spin_unlock_irqrestore(&db_list_lock, flags);
  587. kfree(dbq);
  588. return ret;
  589. }
  590. static const struct file_operations fsl_hv_fops = {
  591. .owner = THIS_MODULE,
  592. .open = fsl_hv_open,
  593. .release = fsl_hv_close,
  594. .poll = fsl_hv_poll,
  595. .read = fsl_hv_read,
  596. .unlocked_ioctl = fsl_hv_ioctl,
  597. .compat_ioctl = fsl_hv_ioctl,
  598. };
  599. static struct miscdevice fsl_hv_misc_dev = {
  600. MISC_DYNAMIC_MINOR,
  601. "fsl-hv",
  602. &fsl_hv_fops
  603. };
  604. static irqreturn_t fsl_hv_shutdown_isr(int irq, void *data)
  605. {
  606. orderly_poweroff(false);
  607. return IRQ_HANDLED;
  608. }
  609. /*
  610. * Returns the handle of the parent of the given node
  611. *
  612. * The handle is the value of the 'hv-handle' property
  613. */
  614. static int get_parent_handle(struct device_node *np)
  615. {
  616. struct device_node *parent;
  617. const uint32_t *prop;
  618. uint32_t handle;
  619. int len;
  620. parent = of_get_parent(np);
  621. if (!parent)
  622. /* It's not really possible for this to fail */
  623. return -ENODEV;
  624. /*
  625. * The proper name for the handle property is "hv-handle", but some
  626. * older versions of the hypervisor used "reg".
  627. */
  628. prop = of_get_property(parent, "hv-handle", &len);
  629. if (!prop)
  630. prop = of_get_property(parent, "reg", &len);
  631. if (!prop || (len != sizeof(uint32_t))) {
  632. /* This can happen only if the node is malformed */
  633. of_node_put(parent);
  634. return -ENODEV;
  635. }
  636. handle = be32_to_cpup(prop);
  637. of_node_put(parent);
  638. return handle;
  639. }
  640. /*
  641. * Register a callback for failover events
  642. *
  643. * This function is called by device drivers to register their callback
  644. * functions for fail-over events.
  645. */
  646. int fsl_hv_failover_register(struct notifier_block *nb)
  647. {
  648. return blocking_notifier_chain_register(&failover_subscribers, nb);
  649. }
  650. EXPORT_SYMBOL(fsl_hv_failover_register);
  651. /*
  652. * Unregister a callback for failover events
  653. */
  654. int fsl_hv_failover_unregister(struct notifier_block *nb)
  655. {
  656. return blocking_notifier_chain_unregister(&failover_subscribers, nb);
  657. }
  658. EXPORT_SYMBOL(fsl_hv_failover_unregister);
  659. /*
  660. * Return TRUE if we're running under FSL hypervisor
  661. *
  662. * This function checks to see if we're running under the Freescale
  663. * hypervisor, and returns zero if we're not, or non-zero if we are.
  664. *
  665. * First, it checks if MSR[GS]==1, which means we're running under some
  666. * hypervisor. Then it checks if there is a hypervisor node in the device
  667. * tree. Currently, that means there needs to be a node in the root called
  668. * "hypervisor" and which has a property named "fsl,hv-version".
  669. */
  670. static int has_fsl_hypervisor(void)
  671. {
  672. struct device_node *node;
  673. int ret;
  674. node = of_find_node_by_path("/hypervisor");
  675. if (!node)
  676. return 0;
  677. ret = of_find_property(node, "fsl,hv-version", NULL) != NULL;
  678. of_node_put(node);
  679. return ret;
  680. }
  681. /*
  682. * Freescale hypervisor management driver init
  683. *
  684. * This function is called when this module is loaded.
  685. *
  686. * Register ourselves as a miscellaneous driver. This will register the
  687. * fops structure and create the right sysfs entries for udev.
  688. */
  689. static int __init fsl_hypervisor_init(void)
  690. {
  691. struct device_node *np;
  692. struct doorbell_isr *dbisr, *n;
  693. int ret;
  694. pr_info("Freescale hypervisor management driver\n");
  695. if (!has_fsl_hypervisor()) {
  696. pr_info("fsl-hv: no hypervisor found\n");
  697. return -ENODEV;
  698. }
  699. ret = misc_register(&fsl_hv_misc_dev);
  700. if (ret) {
  701. pr_err("fsl-hv: cannot register device\n");
  702. return ret;
  703. }
  704. INIT_LIST_HEAD(&db_list);
  705. INIT_LIST_HEAD(&isr_list);
  706. for_each_compatible_node(np, NULL, "epapr,hv-receive-doorbell") {
  707. unsigned int irq;
  708. const uint32_t *handle;
  709. handle = of_get_property(np, "interrupts", NULL);
  710. irq = irq_of_parse_and_map(np, 0);
  711. if (!handle || (irq == NO_IRQ)) {
  712. pr_err("fsl-hv: no 'interrupts' property in %s node\n",
  713. np->full_name);
  714. continue;
  715. }
  716. dbisr = kzalloc(sizeof(*dbisr), GFP_KERNEL);
  717. if (!dbisr)
  718. goto out_of_memory;
  719. dbisr->irq = irq;
  720. dbisr->doorbell = be32_to_cpup(handle);
  721. if (of_device_is_compatible(np, "fsl,hv-shutdown-doorbell")) {
  722. /* The shutdown doorbell gets its own ISR */
  723. ret = request_irq(irq, fsl_hv_shutdown_isr, 0,
  724. np->name, NULL);
  725. } else if (of_device_is_compatible(np,
  726. "fsl,hv-state-change-doorbell")) {
  727. /*
  728. * The state change doorbell triggers a notification if
  729. * the state of the managed partition changes to
  730. * "stopped". We need a separate interrupt handler for
  731. * that, and we also need to know the handle of the
  732. * target partition, not just the handle of the
  733. * doorbell.
  734. */
  735. dbisr->partition = ret = get_parent_handle(np);
  736. if (ret < 0) {
  737. pr_err("fsl-hv: node %s has missing or "
  738. "malformed parent\n", np->full_name);
  739. kfree(dbisr);
  740. continue;
  741. }
  742. ret = request_threaded_irq(irq, fsl_hv_state_change_isr,
  743. fsl_hv_state_change_thread,
  744. 0, np->name, dbisr);
  745. } else
  746. ret = request_irq(irq, fsl_hv_isr, 0, np->name, dbisr);
  747. if (ret < 0) {
  748. pr_err("fsl-hv: could not request irq %u for node %s\n",
  749. irq, np->full_name);
  750. kfree(dbisr);
  751. continue;
  752. }
  753. list_add(&dbisr->list, &isr_list);
  754. pr_info("fsl-hv: registered handler for doorbell %u\n",
  755. dbisr->doorbell);
  756. }
  757. return 0;
  758. out_of_memory:
  759. list_for_each_entry_safe(dbisr, n, &isr_list, list) {
  760. free_irq(dbisr->irq, dbisr);
  761. list_del(&dbisr->list);
  762. kfree(dbisr);
  763. }
  764. misc_deregister(&fsl_hv_misc_dev);
  765. return -ENOMEM;
  766. }
  767. /*
  768. * Freescale hypervisor management driver termination
  769. *
  770. * This function is called when this driver is unloaded.
  771. */
  772. static void __exit fsl_hypervisor_exit(void)
  773. {
  774. struct doorbell_isr *dbisr, *n;
  775. list_for_each_entry_safe(dbisr, n, &isr_list, list) {
  776. free_irq(dbisr->irq, dbisr);
  777. list_del(&dbisr->list);
  778. kfree(dbisr);
  779. }
  780. misc_deregister(&fsl_hv_misc_dev);
  781. }
  782. module_init(fsl_hypervisor_init);
  783. module_exit(fsl_hypervisor_exit);
  784. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  785. MODULE_DESCRIPTION("Freescale hypervisor management driver");
  786. MODULE_LICENSE("GPL v2");