vfio_pci_intrs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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 file *file = NULL;
  114. struct eventfd_ctx *ctx = NULL;
  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. file = eventfd_fget(fd);
  129. if (IS_ERR(file)) {
  130. ret = PTR_ERR(file);
  131. goto fail;
  132. }
  133. ctx = eventfd_ctx_fileget(file);
  134. if (IS_ERR(ctx)) {
  135. ret = PTR_ERR(ctx);
  136. goto fail;
  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 fail;
  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 = file->f_op->poll(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. fput(file);
  173. return 0;
  174. fail:
  175. if (ctx && !IS_ERR(ctx))
  176. eventfd_ctx_put(ctx);
  177. if (file && !IS_ERR(file))
  178. fput(file);
  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. int vfio_pci_intx_unmask_handler(struct vfio_pci_device *vdev, void *unused)
  241. {
  242. struct pci_dev *pdev = vdev->pdev;
  243. unsigned long flags;
  244. int ret = 0;
  245. spin_lock_irqsave(&vdev->irqlock, flags);
  246. /*
  247. * Unmasking comes from ioctl or config, so again, have the
  248. * physical bit follow the virtual even when not using INTx.
  249. */
  250. if (unlikely(!is_intx(vdev))) {
  251. if (vdev->pci_2_3)
  252. pci_intx(pdev, 1);
  253. } else if (vdev->ctx[0].masked && !vdev->virq_disabled) {
  254. /*
  255. * A pending interrupt here would immediately trigger,
  256. * but we can avoid that overhead by just re-sending
  257. * the interrupt to the user.
  258. */
  259. if (vdev->pci_2_3) {
  260. if (!pci_check_and_unmask_intx(pdev))
  261. ret = 1;
  262. } else
  263. enable_irq(pdev->irq);
  264. vdev->ctx[0].masked = (ret > 0);
  265. }
  266. spin_unlock_irqrestore(&vdev->irqlock, flags);
  267. return ret;
  268. }
  269. void vfio_pci_intx_unmask(struct vfio_pci_device *vdev)
  270. {
  271. if (vfio_pci_intx_unmask_handler(vdev, NULL) > 0)
  272. vfio_send_intx_eventfd(vdev, NULL);
  273. }
  274. static irqreturn_t vfio_intx_handler(int irq, void *dev_id)
  275. {
  276. struct vfio_pci_device *vdev = dev_id;
  277. unsigned long flags;
  278. int ret = IRQ_NONE;
  279. spin_lock_irqsave(&vdev->irqlock, flags);
  280. if (!vdev->pci_2_3) {
  281. disable_irq_nosync(vdev->pdev->irq);
  282. vdev->ctx[0].masked = true;
  283. ret = IRQ_HANDLED;
  284. } else if (!vdev->ctx[0].masked && /* may be shared */
  285. pci_check_and_mask_intx(vdev->pdev)) {
  286. vdev->ctx[0].masked = true;
  287. ret = IRQ_HANDLED;
  288. }
  289. spin_unlock_irqrestore(&vdev->irqlock, flags);
  290. if (ret == IRQ_HANDLED)
  291. vfio_send_intx_eventfd(vdev, NULL);
  292. return ret;
  293. }
  294. static int vfio_intx_enable(struct vfio_pci_device *vdev)
  295. {
  296. if (!is_irq_none(vdev))
  297. return -EINVAL;
  298. if (!vdev->pdev->irq)
  299. return -ENODEV;
  300. vdev->ctx = kzalloc(sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
  301. if (!vdev->ctx)
  302. return -ENOMEM;
  303. vdev->num_ctx = 1;
  304. /*
  305. * If the virtual interrupt is masked, restore it. Devices
  306. * supporting DisINTx can be masked at the hardware level
  307. * here, non-PCI-2.3 devices will have to wait until the
  308. * interrupt is enabled.
  309. */
  310. vdev->ctx[0].masked = vdev->virq_disabled;
  311. if (vdev->pci_2_3)
  312. pci_intx(vdev->pdev, !vdev->ctx[0].masked);
  313. vdev->irq_type = VFIO_PCI_INTX_IRQ_INDEX;
  314. return 0;
  315. }
  316. static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
  317. {
  318. struct pci_dev *pdev = vdev->pdev;
  319. unsigned long irqflags = IRQF_SHARED;
  320. struct eventfd_ctx *trigger;
  321. unsigned long flags;
  322. int ret;
  323. if (vdev->ctx[0].trigger) {
  324. free_irq(pdev->irq, vdev);
  325. kfree(vdev->ctx[0].name);
  326. eventfd_ctx_put(vdev->ctx[0].trigger);
  327. vdev->ctx[0].trigger = NULL;
  328. }
  329. if (fd < 0) /* Disable only */
  330. return 0;
  331. vdev->ctx[0].name = kasprintf(GFP_KERNEL, "vfio-intx(%s)",
  332. pci_name(pdev));
  333. if (!vdev->ctx[0].name)
  334. return -ENOMEM;
  335. trigger = eventfd_ctx_fdget(fd);
  336. if (IS_ERR(trigger)) {
  337. kfree(vdev->ctx[0].name);
  338. return PTR_ERR(trigger);
  339. }
  340. vdev->ctx[0].trigger = trigger;
  341. if (!vdev->pci_2_3)
  342. irqflags = 0;
  343. ret = request_irq(pdev->irq, vfio_intx_handler,
  344. irqflags, vdev->ctx[0].name, vdev);
  345. if (ret) {
  346. vdev->ctx[0].trigger = NULL;
  347. kfree(vdev->ctx[0].name);
  348. eventfd_ctx_put(trigger);
  349. return ret;
  350. }
  351. /*
  352. * INTx disable will stick across the new irq setup,
  353. * disable_irq won't.
  354. */
  355. spin_lock_irqsave(&vdev->irqlock, flags);
  356. if (!vdev->pci_2_3 && vdev->ctx[0].masked)
  357. disable_irq_nosync(pdev->irq);
  358. spin_unlock_irqrestore(&vdev->irqlock, flags);
  359. return 0;
  360. }
  361. static void vfio_intx_disable(struct vfio_pci_device *vdev)
  362. {
  363. vfio_intx_set_signal(vdev, -1);
  364. virqfd_disable(vdev, &vdev->ctx[0].unmask);
  365. virqfd_disable(vdev, &vdev->ctx[0].mask);
  366. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  367. vdev->num_ctx = 0;
  368. kfree(vdev->ctx);
  369. }
  370. /*
  371. * MSI/MSI-X
  372. */
  373. static irqreturn_t vfio_msihandler(int irq, void *arg)
  374. {
  375. struct eventfd_ctx *trigger = arg;
  376. eventfd_signal(trigger, 1);
  377. return IRQ_HANDLED;
  378. }
  379. static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
  380. {
  381. struct pci_dev *pdev = vdev->pdev;
  382. int ret;
  383. if (!is_irq_none(vdev))
  384. return -EINVAL;
  385. vdev->ctx = kzalloc(nvec * sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
  386. if (!vdev->ctx)
  387. return -ENOMEM;
  388. if (msix) {
  389. int i;
  390. vdev->msix = kzalloc(nvec * sizeof(struct msix_entry),
  391. GFP_KERNEL);
  392. if (!vdev->msix) {
  393. kfree(vdev->ctx);
  394. return -ENOMEM;
  395. }
  396. for (i = 0; i < nvec; i++)
  397. vdev->msix[i].entry = i;
  398. ret = pci_enable_msix(pdev, vdev->msix, nvec);
  399. if (ret) {
  400. kfree(vdev->msix);
  401. kfree(vdev->ctx);
  402. return ret;
  403. }
  404. } else {
  405. ret = pci_enable_msi_block(pdev, nvec);
  406. if (ret) {
  407. kfree(vdev->ctx);
  408. return ret;
  409. }
  410. }
  411. vdev->num_ctx = nvec;
  412. vdev->irq_type = msix ? VFIO_PCI_MSIX_IRQ_INDEX :
  413. VFIO_PCI_MSI_IRQ_INDEX;
  414. if (!msix) {
  415. /*
  416. * Compute the virtual hardware field for max msi vectors -
  417. * it is the log base 2 of the number of vectors.
  418. */
  419. vdev->msi_qmax = fls(nvec * 2 - 1) - 1;
  420. }
  421. return 0;
  422. }
  423. static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
  424. int vector, int fd, bool msix)
  425. {
  426. struct pci_dev *pdev = vdev->pdev;
  427. int irq = msix ? vdev->msix[vector].vector : pdev->irq + vector;
  428. char *name = msix ? "vfio-msix" : "vfio-msi";
  429. struct eventfd_ctx *trigger;
  430. int ret;
  431. if (vector >= vdev->num_ctx)
  432. return -EINVAL;
  433. if (vdev->ctx[vector].trigger) {
  434. free_irq(irq, vdev->ctx[vector].trigger);
  435. kfree(vdev->ctx[vector].name);
  436. eventfd_ctx_put(vdev->ctx[vector].trigger);
  437. vdev->ctx[vector].trigger = NULL;
  438. }
  439. if (fd < 0)
  440. return 0;
  441. vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "%s[%d](%s)",
  442. name, vector, pci_name(pdev));
  443. if (!vdev->ctx[vector].name)
  444. return -ENOMEM;
  445. trigger = eventfd_ctx_fdget(fd);
  446. if (IS_ERR(trigger)) {
  447. kfree(vdev->ctx[vector].name);
  448. return PTR_ERR(trigger);
  449. }
  450. ret = request_irq(irq, vfio_msihandler, 0,
  451. vdev->ctx[vector].name, trigger);
  452. if (ret) {
  453. kfree(vdev->ctx[vector].name);
  454. eventfd_ctx_put(trigger);
  455. return ret;
  456. }
  457. vdev->ctx[vector].trigger = trigger;
  458. return 0;
  459. }
  460. static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start,
  461. unsigned count, int32_t *fds, bool msix)
  462. {
  463. int i, j, ret = 0;
  464. if (start + count > vdev->num_ctx)
  465. return -EINVAL;
  466. for (i = 0, j = start; i < count && !ret; i++, j++) {
  467. int fd = fds ? fds[i] : -1;
  468. ret = vfio_msi_set_vector_signal(vdev, j, fd, msix);
  469. }
  470. if (ret) {
  471. for (--j; j >= start; j--)
  472. vfio_msi_set_vector_signal(vdev, j, -1, msix);
  473. }
  474. return ret;
  475. }
  476. static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
  477. {
  478. struct pci_dev *pdev = vdev->pdev;
  479. int i;
  480. vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
  481. for (i = 0; i < vdev->num_ctx; i++) {
  482. virqfd_disable(vdev, &vdev->ctx[i].unmask);
  483. virqfd_disable(vdev, &vdev->ctx[i].mask);
  484. }
  485. if (msix) {
  486. pci_disable_msix(vdev->pdev);
  487. kfree(vdev->msix);
  488. } else
  489. pci_disable_msi(pdev);
  490. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  491. vdev->num_ctx = 0;
  492. kfree(vdev->ctx);
  493. }
  494. /*
  495. * IOCTL support
  496. */
  497. static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
  498. unsigned index, unsigned start,
  499. unsigned count, uint32_t flags, void *data)
  500. {
  501. if (!is_intx(vdev) || start != 0 || count != 1)
  502. return -EINVAL;
  503. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  504. vfio_pci_intx_unmask(vdev);
  505. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  506. uint8_t unmask = *(uint8_t *)data;
  507. if (unmask)
  508. vfio_pci_intx_unmask(vdev);
  509. } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  510. int32_t fd = *(int32_t *)data;
  511. if (fd >= 0)
  512. return virqfd_enable(vdev, vfio_pci_intx_unmask_handler,
  513. vfio_send_intx_eventfd, NULL,
  514. &vdev->ctx[0].unmask, fd);
  515. virqfd_disable(vdev, &vdev->ctx[0].unmask);
  516. }
  517. return 0;
  518. }
  519. static int vfio_pci_set_intx_mask(struct vfio_pci_device *vdev,
  520. unsigned index, unsigned start,
  521. unsigned count, uint32_t flags, void *data)
  522. {
  523. if (!is_intx(vdev) || start != 0 || count != 1)
  524. return -EINVAL;
  525. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  526. vfio_pci_intx_mask(vdev);
  527. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  528. uint8_t mask = *(uint8_t *)data;
  529. if (mask)
  530. vfio_pci_intx_mask(vdev);
  531. } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  532. return -ENOTTY; /* XXX implement me */
  533. }
  534. return 0;
  535. }
  536. static int vfio_pci_set_intx_trigger(struct vfio_pci_device *vdev,
  537. unsigned index, unsigned start,
  538. unsigned count, uint32_t flags, void *data)
  539. {
  540. if (is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
  541. vfio_intx_disable(vdev);
  542. return 0;
  543. }
  544. if (!(is_intx(vdev) || is_irq_none(vdev)) || start != 0 || count != 1)
  545. return -EINVAL;
  546. if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  547. int32_t fd = *(int32_t *)data;
  548. int ret;
  549. if (is_intx(vdev))
  550. return vfio_intx_set_signal(vdev, fd);
  551. ret = vfio_intx_enable(vdev);
  552. if (ret)
  553. return ret;
  554. ret = vfio_intx_set_signal(vdev, fd);
  555. if (ret)
  556. vfio_intx_disable(vdev);
  557. return ret;
  558. }
  559. if (!is_intx(vdev))
  560. return -EINVAL;
  561. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  562. vfio_send_intx_eventfd(vdev, NULL);
  563. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  564. uint8_t trigger = *(uint8_t *)data;
  565. if (trigger)
  566. vfio_send_intx_eventfd(vdev, NULL);
  567. }
  568. return 0;
  569. }
  570. static int vfio_pci_set_msi_trigger(struct vfio_pci_device *vdev,
  571. unsigned index, unsigned start,
  572. unsigned count, uint32_t flags, void *data)
  573. {
  574. int i;
  575. bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false;
  576. if (irq_is(vdev, index) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
  577. vfio_msi_disable(vdev, msix);
  578. return 0;
  579. }
  580. if (!(irq_is(vdev, index) || is_irq_none(vdev)))
  581. return -EINVAL;
  582. if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  583. int32_t *fds = data;
  584. int ret;
  585. if (vdev->irq_type == index)
  586. return vfio_msi_set_block(vdev, start, count,
  587. fds, msix);
  588. ret = vfio_msi_enable(vdev, start + count, msix);
  589. if (ret)
  590. return ret;
  591. ret = vfio_msi_set_block(vdev, start, count, fds, msix);
  592. if (ret)
  593. vfio_msi_disable(vdev, msix);
  594. return ret;
  595. }
  596. if (!irq_is(vdev, index) || start + count > vdev->num_ctx)
  597. return -EINVAL;
  598. for (i = start; i < start + count; i++) {
  599. if (!vdev->ctx[i].trigger)
  600. continue;
  601. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  602. eventfd_signal(vdev->ctx[i].trigger, 1);
  603. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  604. uint8_t *bools = data;
  605. if (bools[i - start])
  606. eventfd_signal(vdev->ctx[i].trigger, 1);
  607. }
  608. }
  609. return 0;
  610. }
  611. int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev, uint32_t flags,
  612. unsigned index, unsigned start, unsigned count,
  613. void *data)
  614. {
  615. int (*func)(struct vfio_pci_device *vdev, unsigned index,
  616. unsigned start, unsigned count, uint32_t flags,
  617. void *data) = NULL;
  618. switch (index) {
  619. case VFIO_PCI_INTX_IRQ_INDEX:
  620. switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
  621. case VFIO_IRQ_SET_ACTION_MASK:
  622. func = vfio_pci_set_intx_mask;
  623. break;
  624. case VFIO_IRQ_SET_ACTION_UNMASK:
  625. func = vfio_pci_set_intx_unmask;
  626. break;
  627. case VFIO_IRQ_SET_ACTION_TRIGGER:
  628. func = vfio_pci_set_intx_trigger;
  629. break;
  630. }
  631. break;
  632. case VFIO_PCI_MSI_IRQ_INDEX:
  633. case VFIO_PCI_MSIX_IRQ_INDEX:
  634. switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
  635. case VFIO_IRQ_SET_ACTION_MASK:
  636. case VFIO_IRQ_SET_ACTION_UNMASK:
  637. /* XXX Need masking support exported */
  638. break;
  639. case VFIO_IRQ_SET_ACTION_TRIGGER:
  640. func = vfio_pci_set_msi_trigger;
  641. break;
  642. }
  643. break;
  644. }
  645. if (!func)
  646. return -ENOTTY;
  647. return func(vdev, index, start, count, flags, data);
  648. }