vfio_pci_intrs.c 19 KB

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