vfio_pci_intrs.c 18 KB

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