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