vfio_pci_intrs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * VFIO PCI interrupt handling
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  5. * Author: Alex Williamson <alex.williamson@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Derived from original vfio:
  12. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  13. * Author: Tom Lyon, pugs@cisco.com
  14. */
  15. #include <linux/device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/eventfd.h>
  18. #include <linux/msi.h>
  19. #include <linux/pci.h>
  20. #include <linux/file.h>
  21. #include <linux/poll.h>
  22. #include <linux/vfio.h>
  23. #include <linux/wait.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/slab.h>
  26. #include "vfio_pci_private.h"
  27. /*
  28. * IRQfd - generic
  29. */
  30. struct virqfd {
  31. struct vfio_pci_device *vdev;
  32. struct eventfd_ctx *eventfd;
  33. int (*handler)(struct vfio_pci_device *, void *);
  34. void (*thread)(struct vfio_pci_device *, void *);
  35. void *data;
  36. struct work_struct inject;
  37. wait_queue_t wait;
  38. poll_table pt;
  39. struct work_struct shutdown;
  40. struct virqfd **pvirqfd;
  41. };
  42. static struct workqueue_struct *vfio_irqfd_cleanup_wq;
  43. int __init vfio_pci_virqfd_init(void)
  44. {
  45. vfio_irqfd_cleanup_wq =
  46. create_singlethread_workqueue("vfio-irqfd-cleanup");
  47. if (!vfio_irqfd_cleanup_wq)
  48. return -ENOMEM;
  49. return 0;
  50. }
  51. void vfio_pci_virqfd_exit(void)
  52. {
  53. destroy_workqueue(vfio_irqfd_cleanup_wq);
  54. }
  55. static void virqfd_deactivate(struct virqfd *virqfd)
  56. {
  57. queue_work(vfio_irqfd_cleanup_wq, &virqfd->shutdown);
  58. }
  59. static int virqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
  60. {
  61. struct virqfd *virqfd = container_of(wait, struct virqfd, wait);
  62. unsigned long flags = (unsigned long)key;
  63. if (flags & POLLIN) {
  64. /* An event has been signaled, call function */
  65. if ((!virqfd->handler ||
  66. virqfd->handler(virqfd->vdev, virqfd->data)) &&
  67. virqfd->thread)
  68. schedule_work(&virqfd->inject);
  69. }
  70. if (flags & POLLHUP) {
  71. unsigned long flags;
  72. spin_lock_irqsave(&virqfd->vdev->irqlock, flags);
  73. /*
  74. * The eventfd is closing, if the virqfd has not yet been
  75. * queued for release, as determined by testing whether the
  76. * vdev pointer to it is still valid, queue it now. As
  77. * with kvm irqfds, we know we won't race against the virqfd
  78. * going away because we hold wqh->lock to get here.
  79. */
  80. if (*(virqfd->pvirqfd) == virqfd) {
  81. *(virqfd->pvirqfd) = NULL;
  82. virqfd_deactivate(virqfd);
  83. }
  84. spin_unlock_irqrestore(&virqfd->vdev->irqlock, flags);
  85. }
  86. return 0;
  87. }
  88. static void virqfd_ptable_queue_proc(struct file *file,
  89. wait_queue_head_t *wqh, poll_table *pt)
  90. {
  91. struct virqfd *virqfd = container_of(pt, struct virqfd, pt);
  92. add_wait_queue(wqh, &virqfd->wait);
  93. }
  94. static void virqfd_shutdown(struct work_struct *work)
  95. {
  96. struct virqfd *virqfd = container_of(work, struct virqfd, shutdown);
  97. u64 cnt;
  98. eventfd_ctx_remove_wait_queue(virqfd->eventfd, &virqfd->wait, &cnt);
  99. flush_work(&virqfd->inject);
  100. eventfd_ctx_put(virqfd->eventfd);
  101. kfree(virqfd);
  102. }
  103. static void virqfd_inject(struct work_struct *work)
  104. {
  105. struct virqfd *virqfd = container_of(work, struct virqfd, inject);
  106. if (virqfd->thread)
  107. virqfd->thread(virqfd->vdev, virqfd->data);
  108. }
  109. static int virqfd_enable(struct vfio_pci_device *vdev,
  110. int (*handler)(struct vfio_pci_device *, void *),
  111. void (*thread)(struct vfio_pci_device *, void *),
  112. void *data, struct virqfd **pvirqfd, int fd)
  113. {
  114. struct fd irqfd;
  115. struct eventfd_ctx *ctx;
  116. struct virqfd *virqfd;
  117. int ret = 0;
  118. unsigned int events;
  119. virqfd = kzalloc(sizeof(*virqfd), GFP_KERNEL);
  120. if (!virqfd)
  121. return -ENOMEM;
  122. virqfd->pvirqfd = pvirqfd;
  123. virqfd->vdev = vdev;
  124. virqfd->handler = handler;
  125. virqfd->thread = thread;
  126. virqfd->data = data;
  127. INIT_WORK(&virqfd->shutdown, virqfd_shutdown);
  128. INIT_WORK(&virqfd->inject, virqfd_inject);
  129. irqfd = fdget(fd);
  130. if (!irqfd.file) {
  131. ret = -EBADF;
  132. goto err_fd;
  133. }
  134. ctx = eventfd_ctx_fileget(irqfd.file);
  135. if (IS_ERR(ctx)) {
  136. ret = PTR_ERR(ctx);
  137. goto err_ctx;
  138. }
  139. virqfd->eventfd = ctx;
  140. /*
  141. * virqfds can be released by closing the eventfd or directly
  142. * through ioctl. These are both done through a workqueue, so
  143. * we update the pointer to the virqfd under lock to avoid
  144. * pushing multiple jobs to release the same virqfd.
  145. */
  146. spin_lock_irq(&vdev->irqlock);
  147. if (*pvirqfd) {
  148. spin_unlock_irq(&vdev->irqlock);
  149. ret = -EBUSY;
  150. goto err_busy;
  151. }
  152. *pvirqfd = virqfd;
  153. spin_unlock_irq(&vdev->irqlock);
  154. /*
  155. * Install our own custom wake-up handling so we are notified via
  156. * a callback whenever someone signals the underlying eventfd.
  157. */
  158. init_waitqueue_func_entry(&virqfd->wait, virqfd_wakeup);
  159. init_poll_funcptr(&virqfd->pt, virqfd_ptable_queue_proc);
  160. events = irqfd.file->f_op->poll(irqfd.file, &virqfd->pt);
  161. /*
  162. * Check if there was an event already pending on the eventfd
  163. * before we registered and trigger it as if we didn't miss it.
  164. */
  165. if (events & POLLIN) {
  166. if ((!handler || handler(vdev, data)) && thread)
  167. schedule_work(&virqfd->inject);
  168. }
  169. /*
  170. * Do not drop the file until the irqfd is fully initialized,
  171. * otherwise we might race against the POLLHUP.
  172. */
  173. fdput(irqfd);
  174. return 0;
  175. err_busy:
  176. eventfd_ctx_put(ctx);
  177. err_ctx:
  178. fdput(irqfd);
  179. err_fd:
  180. kfree(virqfd);
  181. return ret;
  182. }
  183. static void virqfd_disable(struct vfio_pci_device *vdev,
  184. struct virqfd **pvirqfd)
  185. {
  186. unsigned long flags;
  187. spin_lock_irqsave(&vdev->irqlock, flags);
  188. if (*pvirqfd) {
  189. virqfd_deactivate(*pvirqfd);
  190. *pvirqfd = NULL;
  191. }
  192. spin_unlock_irqrestore(&vdev->irqlock, flags);
  193. /*
  194. * Block until we know all outstanding shutdown jobs have completed.
  195. * Even if we don't queue the job, flush the wq to be sure it's
  196. * been released.
  197. */
  198. flush_workqueue(vfio_irqfd_cleanup_wq);
  199. }
  200. /*
  201. * INTx
  202. */
  203. static void vfio_send_intx_eventfd(struct vfio_pci_device *vdev, void *unused)
  204. {
  205. if (likely(is_intx(vdev) && !vdev->virq_disabled))
  206. eventfd_signal(vdev->ctx[0].trigger, 1);
  207. }
  208. void vfio_pci_intx_mask(struct vfio_pci_device *vdev)
  209. {
  210. struct pci_dev *pdev = vdev->pdev;
  211. unsigned long flags;
  212. spin_lock_irqsave(&vdev->irqlock, flags);
  213. /*
  214. * Masking can come from interrupt, ioctl, or config space
  215. * via INTx disable. The latter means this can get called
  216. * even when not using intx delivery. In this case, just
  217. * try to have the physical bit follow the virtual bit.
  218. */
  219. if (unlikely(!is_intx(vdev))) {
  220. if (vdev->pci_2_3)
  221. pci_intx(pdev, 0);
  222. } else if (!vdev->ctx[0].masked) {
  223. /*
  224. * Can't use check_and_mask here because we always want to
  225. * mask, not just when something is pending.
  226. */
  227. if (vdev->pci_2_3)
  228. pci_intx(pdev, 0);
  229. else
  230. disable_irq_nosync(pdev->irq);
  231. vdev->ctx[0].masked = true;
  232. }
  233. spin_unlock_irqrestore(&vdev->irqlock, flags);
  234. }
  235. /*
  236. * If this is triggered by an eventfd, we can't call eventfd_signal
  237. * or else we'll deadlock on the eventfd wait queue. Return >0 when
  238. * a signal is necessary, which can then be handled via a work queue
  239. * or directly depending on the caller.
  240. */
  241. static int vfio_pci_intx_unmask_handler(struct vfio_pci_device *vdev,
  242. void *unused)
  243. {
  244. struct pci_dev *pdev = vdev->pdev;
  245. unsigned long flags;
  246. int ret = 0;
  247. spin_lock_irqsave(&vdev->irqlock, flags);
  248. /*
  249. * Unmasking comes from ioctl or config, so again, have the
  250. * physical bit follow the virtual even when not using INTx.
  251. */
  252. if (unlikely(!is_intx(vdev))) {
  253. if (vdev->pci_2_3)
  254. pci_intx(pdev, 1);
  255. } else if (vdev->ctx[0].masked && !vdev->virq_disabled) {
  256. /*
  257. * A pending interrupt here would immediately trigger,
  258. * but we can avoid that overhead by just re-sending
  259. * the interrupt to the user.
  260. */
  261. if (vdev->pci_2_3) {
  262. if (!pci_check_and_unmask_intx(pdev))
  263. ret = 1;
  264. } else
  265. enable_irq(pdev->irq);
  266. vdev->ctx[0].masked = (ret > 0);
  267. }
  268. spin_unlock_irqrestore(&vdev->irqlock, flags);
  269. return ret;
  270. }
  271. void vfio_pci_intx_unmask(struct vfio_pci_device *vdev)
  272. {
  273. if (vfio_pci_intx_unmask_handler(vdev, NULL) > 0)
  274. vfio_send_intx_eventfd(vdev, NULL);
  275. }
  276. static irqreturn_t vfio_intx_handler(int irq, void *dev_id)
  277. {
  278. struct vfio_pci_device *vdev = dev_id;
  279. unsigned long flags;
  280. int ret = IRQ_NONE;
  281. spin_lock_irqsave(&vdev->irqlock, flags);
  282. if (!vdev->pci_2_3) {
  283. disable_irq_nosync(vdev->pdev->irq);
  284. vdev->ctx[0].masked = true;
  285. ret = IRQ_HANDLED;
  286. } else if (!vdev->ctx[0].masked && /* may be shared */
  287. pci_check_and_mask_intx(vdev->pdev)) {
  288. vdev->ctx[0].masked = true;
  289. ret = IRQ_HANDLED;
  290. }
  291. spin_unlock_irqrestore(&vdev->irqlock, flags);
  292. if (ret == IRQ_HANDLED)
  293. vfio_send_intx_eventfd(vdev, NULL);
  294. return ret;
  295. }
  296. static int vfio_intx_enable(struct vfio_pci_device *vdev)
  297. {
  298. if (!is_irq_none(vdev))
  299. return -EINVAL;
  300. if (!vdev->pdev->irq)
  301. return -ENODEV;
  302. vdev->ctx = kzalloc(sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
  303. if (!vdev->ctx)
  304. return -ENOMEM;
  305. vdev->num_ctx = 1;
  306. /*
  307. * If the virtual interrupt is masked, restore it. Devices
  308. * supporting DisINTx can be masked at the hardware level
  309. * here, non-PCI-2.3 devices will have to wait until the
  310. * interrupt is enabled.
  311. */
  312. vdev->ctx[0].masked = vdev->virq_disabled;
  313. if (vdev->pci_2_3)
  314. pci_intx(vdev->pdev, !vdev->ctx[0].masked);
  315. vdev->irq_type = VFIO_PCI_INTX_IRQ_INDEX;
  316. return 0;
  317. }
  318. static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
  319. {
  320. struct pci_dev *pdev = vdev->pdev;
  321. unsigned long irqflags = IRQF_SHARED;
  322. struct eventfd_ctx *trigger;
  323. unsigned long flags;
  324. int ret;
  325. if (vdev->ctx[0].trigger) {
  326. free_irq(pdev->irq, vdev);
  327. kfree(vdev->ctx[0].name);
  328. eventfd_ctx_put(vdev->ctx[0].trigger);
  329. vdev->ctx[0].trigger = NULL;
  330. }
  331. if (fd < 0) /* Disable only */
  332. return 0;
  333. vdev->ctx[0].name = kasprintf(GFP_KERNEL, "vfio-intx(%s)",
  334. pci_name(pdev));
  335. if (!vdev->ctx[0].name)
  336. return -ENOMEM;
  337. trigger = eventfd_ctx_fdget(fd);
  338. if (IS_ERR(trigger)) {
  339. kfree(vdev->ctx[0].name);
  340. return PTR_ERR(trigger);
  341. }
  342. vdev->ctx[0].trigger = trigger;
  343. if (!vdev->pci_2_3)
  344. irqflags = 0;
  345. ret = request_irq(pdev->irq, vfio_intx_handler,
  346. irqflags, vdev->ctx[0].name, vdev);
  347. if (ret) {
  348. vdev->ctx[0].trigger = NULL;
  349. kfree(vdev->ctx[0].name);
  350. eventfd_ctx_put(trigger);
  351. return ret;
  352. }
  353. /*
  354. * INTx disable will stick across the new irq setup,
  355. * disable_irq won't.
  356. */
  357. spin_lock_irqsave(&vdev->irqlock, flags);
  358. if (!vdev->pci_2_3 && vdev->ctx[0].masked)
  359. disable_irq_nosync(pdev->irq);
  360. spin_unlock_irqrestore(&vdev->irqlock, flags);
  361. return 0;
  362. }
  363. static void vfio_intx_disable(struct vfio_pci_device *vdev)
  364. {
  365. vfio_intx_set_signal(vdev, -1);
  366. virqfd_disable(vdev, &vdev->ctx[0].unmask);
  367. virqfd_disable(vdev, &vdev->ctx[0].mask);
  368. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  369. vdev->num_ctx = 0;
  370. kfree(vdev->ctx);
  371. }
  372. /*
  373. * MSI/MSI-X
  374. */
  375. static irqreturn_t vfio_msihandler(int irq, void *arg)
  376. {
  377. struct eventfd_ctx *trigger = arg;
  378. eventfd_signal(trigger, 1);
  379. return IRQ_HANDLED;
  380. }
  381. static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
  382. {
  383. struct pci_dev *pdev = vdev->pdev;
  384. int ret;
  385. if (!is_irq_none(vdev))
  386. return -EINVAL;
  387. vdev->ctx = kzalloc(nvec * sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
  388. if (!vdev->ctx)
  389. return -ENOMEM;
  390. if (msix) {
  391. int i;
  392. vdev->msix = kzalloc(nvec * sizeof(struct msix_entry),
  393. GFP_KERNEL);
  394. if (!vdev->msix) {
  395. kfree(vdev->ctx);
  396. return -ENOMEM;
  397. }
  398. for (i = 0; i < nvec; i++)
  399. vdev->msix[i].entry = i;
  400. ret = pci_enable_msix_range(pdev, vdev->msix, 1, nvec);
  401. if (ret < nvec) {
  402. if (ret > 0)
  403. pci_disable_msix(pdev);
  404. kfree(vdev->msix);
  405. kfree(vdev->ctx);
  406. return ret;
  407. }
  408. } else {
  409. ret = pci_enable_msi_range(pdev, 1, nvec);
  410. if (ret < nvec) {
  411. if (ret > 0)
  412. pci_disable_msi(pdev);
  413. kfree(vdev->ctx);
  414. return ret;
  415. }
  416. }
  417. vdev->num_ctx = nvec;
  418. vdev->irq_type = msix ? VFIO_PCI_MSIX_IRQ_INDEX :
  419. VFIO_PCI_MSI_IRQ_INDEX;
  420. if (!msix) {
  421. /*
  422. * Compute the virtual hardware field for max msi vectors -
  423. * it is the log base 2 of the number of vectors.
  424. */
  425. vdev->msi_qmax = fls(nvec * 2 - 1) - 1;
  426. }
  427. return 0;
  428. }
  429. static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
  430. int vector, int fd, bool msix)
  431. {
  432. struct pci_dev *pdev = vdev->pdev;
  433. int irq = msix ? vdev->msix[vector].vector : pdev->irq + vector;
  434. char *name = msix ? "vfio-msix" : "vfio-msi";
  435. struct eventfd_ctx *trigger;
  436. int ret;
  437. if (vector >= vdev->num_ctx)
  438. return -EINVAL;
  439. if (vdev->ctx[vector].trigger) {
  440. free_irq(irq, vdev->ctx[vector].trigger);
  441. kfree(vdev->ctx[vector].name);
  442. eventfd_ctx_put(vdev->ctx[vector].trigger);
  443. vdev->ctx[vector].trigger = NULL;
  444. }
  445. if (fd < 0)
  446. return 0;
  447. vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "%s[%d](%s)",
  448. name, vector, pci_name(pdev));
  449. if (!vdev->ctx[vector].name)
  450. return -ENOMEM;
  451. trigger = eventfd_ctx_fdget(fd);
  452. if (IS_ERR(trigger)) {
  453. kfree(vdev->ctx[vector].name);
  454. return PTR_ERR(trigger);
  455. }
  456. /*
  457. * The MSIx vector table resides in device memory which may be cleared
  458. * via backdoor resets. We don't allow direct access to the vector
  459. * table so even if a userspace driver attempts to save/restore around
  460. * such a reset it would be unsuccessful. To avoid this, restore the
  461. * cached value of the message prior to enabling.
  462. */
  463. if (msix) {
  464. struct msi_msg msg;
  465. get_cached_msi_msg(irq, &msg);
  466. pci_write_msi_msg(irq, &msg);
  467. }
  468. ret = request_irq(irq, vfio_msihandler, 0,
  469. vdev->ctx[vector].name, trigger);
  470. if (ret) {
  471. kfree(vdev->ctx[vector].name);
  472. eventfd_ctx_put(trigger);
  473. return ret;
  474. }
  475. vdev->ctx[vector].trigger = trigger;
  476. return 0;
  477. }
  478. static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start,
  479. unsigned count, int32_t *fds, bool msix)
  480. {
  481. int i, j, ret = 0;
  482. if (start + count > vdev->num_ctx)
  483. return -EINVAL;
  484. for (i = 0, j = start; i < count && !ret; i++, j++) {
  485. int fd = fds ? fds[i] : -1;
  486. ret = vfio_msi_set_vector_signal(vdev, j, fd, msix);
  487. }
  488. if (ret) {
  489. for (--j; j >= start; j--)
  490. vfio_msi_set_vector_signal(vdev, j, -1, msix);
  491. }
  492. return ret;
  493. }
  494. static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
  495. {
  496. struct pci_dev *pdev = vdev->pdev;
  497. int i;
  498. vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
  499. for (i = 0; i < vdev->num_ctx; i++) {
  500. virqfd_disable(vdev, &vdev->ctx[i].unmask);
  501. virqfd_disable(vdev, &vdev->ctx[i].mask);
  502. }
  503. if (msix) {
  504. pci_disable_msix(vdev->pdev);
  505. kfree(vdev->msix);
  506. } else
  507. pci_disable_msi(pdev);
  508. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  509. vdev->num_ctx = 0;
  510. kfree(vdev->ctx);
  511. }
  512. /*
  513. * IOCTL support
  514. */
  515. static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
  516. unsigned index, unsigned start,
  517. unsigned count, uint32_t flags, void *data)
  518. {
  519. if (!is_intx(vdev) || start != 0 || count != 1)
  520. return -EINVAL;
  521. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  522. vfio_pci_intx_unmask(vdev);
  523. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  524. uint8_t unmask = *(uint8_t *)data;
  525. if (unmask)
  526. vfio_pci_intx_unmask(vdev);
  527. } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  528. int32_t fd = *(int32_t *)data;
  529. if (fd >= 0)
  530. return virqfd_enable(vdev, vfio_pci_intx_unmask_handler,
  531. vfio_send_intx_eventfd, NULL,
  532. &vdev->ctx[0].unmask, fd);
  533. virqfd_disable(vdev, &vdev->ctx[0].unmask);
  534. }
  535. return 0;
  536. }
  537. static int vfio_pci_set_intx_mask(struct vfio_pci_device *vdev,
  538. unsigned index, unsigned start,
  539. unsigned count, uint32_t flags, void *data)
  540. {
  541. if (!is_intx(vdev) || start != 0 || count != 1)
  542. return -EINVAL;
  543. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  544. vfio_pci_intx_mask(vdev);
  545. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  546. uint8_t mask = *(uint8_t *)data;
  547. if (mask)
  548. vfio_pci_intx_mask(vdev);
  549. } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  550. return -ENOTTY; /* XXX implement me */
  551. }
  552. return 0;
  553. }
  554. static int vfio_pci_set_intx_trigger(struct vfio_pci_device *vdev,
  555. unsigned index, unsigned start,
  556. unsigned count, uint32_t flags, void *data)
  557. {
  558. if (is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
  559. vfio_intx_disable(vdev);
  560. return 0;
  561. }
  562. if (!(is_intx(vdev) || is_irq_none(vdev)) || start != 0 || count != 1)
  563. return -EINVAL;
  564. if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  565. int32_t fd = *(int32_t *)data;
  566. int ret;
  567. if (is_intx(vdev))
  568. return vfio_intx_set_signal(vdev, fd);
  569. ret = vfio_intx_enable(vdev);
  570. if (ret)
  571. return ret;
  572. ret = vfio_intx_set_signal(vdev, fd);
  573. if (ret)
  574. vfio_intx_disable(vdev);
  575. return ret;
  576. }
  577. if (!is_intx(vdev))
  578. return -EINVAL;
  579. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  580. vfio_send_intx_eventfd(vdev, NULL);
  581. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  582. uint8_t trigger = *(uint8_t *)data;
  583. if (trigger)
  584. vfio_send_intx_eventfd(vdev, NULL);
  585. }
  586. return 0;
  587. }
  588. static int vfio_pci_set_msi_trigger(struct vfio_pci_device *vdev,
  589. unsigned index, unsigned start,
  590. unsigned count, uint32_t flags, void *data)
  591. {
  592. int i;
  593. bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false;
  594. if (irq_is(vdev, index) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
  595. vfio_msi_disable(vdev, msix);
  596. return 0;
  597. }
  598. if (!(irq_is(vdev, index) || is_irq_none(vdev)))
  599. return -EINVAL;
  600. if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  601. int32_t *fds = data;
  602. int ret;
  603. if (vdev->irq_type == index)
  604. return vfio_msi_set_block(vdev, start, count,
  605. fds, msix);
  606. ret = vfio_msi_enable(vdev, start + count, msix);
  607. if (ret)
  608. return ret;
  609. ret = vfio_msi_set_block(vdev, start, count, fds, msix);
  610. if (ret)
  611. vfio_msi_disable(vdev, msix);
  612. return ret;
  613. }
  614. if (!irq_is(vdev, index) || start + count > vdev->num_ctx)
  615. return -EINVAL;
  616. for (i = start; i < start + count; i++) {
  617. if (!vdev->ctx[i].trigger)
  618. continue;
  619. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  620. eventfd_signal(vdev->ctx[i].trigger, 1);
  621. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  622. uint8_t *bools = data;
  623. if (bools[i - start])
  624. eventfd_signal(vdev->ctx[i].trigger, 1);
  625. }
  626. }
  627. return 0;
  628. }
  629. static int vfio_pci_set_ctx_trigger_single(struct eventfd_ctx **ctx,
  630. uint32_t flags, void *data)
  631. {
  632. int32_t fd = *(int32_t *)data;
  633. if (!(flags & VFIO_IRQ_SET_DATA_TYPE_MASK))
  634. return -EINVAL;
  635. /* DATA_NONE/DATA_BOOL enables loopback testing */
  636. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  637. if (*ctx)
  638. eventfd_signal(*ctx, 1);
  639. return 0;
  640. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  641. uint8_t trigger = *(uint8_t *)data;
  642. if (trigger && *ctx)
  643. eventfd_signal(*ctx, 1);
  644. return 0;
  645. }
  646. /* Handle SET_DATA_EVENTFD */
  647. if (fd == -1) {
  648. if (*ctx)
  649. eventfd_ctx_put(*ctx);
  650. *ctx = NULL;
  651. return 0;
  652. } else if (fd >= 0) {
  653. struct eventfd_ctx *efdctx;
  654. efdctx = eventfd_ctx_fdget(fd);
  655. if (IS_ERR(efdctx))
  656. return PTR_ERR(efdctx);
  657. if (*ctx)
  658. eventfd_ctx_put(*ctx);
  659. *ctx = efdctx;
  660. return 0;
  661. } else
  662. return -EINVAL;
  663. }
  664. static int vfio_pci_set_err_trigger(struct vfio_pci_device *vdev,
  665. unsigned index, unsigned start,
  666. unsigned count, uint32_t flags, void *data)
  667. {
  668. if (index != VFIO_PCI_ERR_IRQ_INDEX)
  669. return -EINVAL;
  670. /*
  671. * We should sanitize start & count, but that wasn't caught
  672. * originally, so this IRQ index must forever ignore them :-(
  673. */
  674. return vfio_pci_set_ctx_trigger_single(&vdev->err_trigger, flags, data);
  675. }
  676. static int vfio_pci_set_req_trigger(struct vfio_pci_device *vdev,
  677. unsigned index, unsigned start,
  678. unsigned count, uint32_t flags, void *data)
  679. {
  680. if (index != VFIO_PCI_REQ_IRQ_INDEX || start != 0 || count != 1)
  681. return -EINVAL;
  682. return vfio_pci_set_ctx_trigger_single(&vdev->req_trigger, flags, data);
  683. }
  684. int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev, uint32_t flags,
  685. unsigned index, unsigned start, unsigned count,
  686. void *data)
  687. {
  688. int (*func)(struct vfio_pci_device *vdev, unsigned index,
  689. unsigned start, unsigned count, uint32_t flags,
  690. void *data) = NULL;
  691. switch (index) {
  692. case VFIO_PCI_INTX_IRQ_INDEX:
  693. switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
  694. case VFIO_IRQ_SET_ACTION_MASK:
  695. func = vfio_pci_set_intx_mask;
  696. break;
  697. case VFIO_IRQ_SET_ACTION_UNMASK:
  698. func = vfio_pci_set_intx_unmask;
  699. break;
  700. case VFIO_IRQ_SET_ACTION_TRIGGER:
  701. func = vfio_pci_set_intx_trigger;
  702. break;
  703. }
  704. break;
  705. case VFIO_PCI_MSI_IRQ_INDEX:
  706. case VFIO_PCI_MSIX_IRQ_INDEX:
  707. switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
  708. case VFIO_IRQ_SET_ACTION_MASK:
  709. case VFIO_IRQ_SET_ACTION_UNMASK:
  710. /* XXX Need masking support exported */
  711. break;
  712. case VFIO_IRQ_SET_ACTION_TRIGGER:
  713. func = vfio_pci_set_msi_trigger;
  714. break;
  715. }
  716. break;
  717. case VFIO_PCI_ERR_IRQ_INDEX:
  718. switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
  719. case VFIO_IRQ_SET_ACTION_TRIGGER:
  720. if (pci_is_pcie(vdev->pdev))
  721. func = vfio_pci_set_err_trigger;
  722. break;
  723. }
  724. case VFIO_PCI_REQ_IRQ_INDEX:
  725. switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
  726. case VFIO_IRQ_SET_ACTION_TRIGGER:
  727. func = vfio_pci_set_req_trigger;
  728. break;
  729. }
  730. }
  731. if (!func)
  732. return -ENOTTY;
  733. return func(vdev, index, start, count, flags, data);
  734. }