vfio_pci_intrs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. vdev->irq_type = VFIO_PCI_INTX_IRQ_INDEX;
  304. return 0;
  305. }
  306. static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
  307. {
  308. struct pci_dev *pdev = vdev->pdev;
  309. unsigned long irqflags = IRQF_SHARED;
  310. struct eventfd_ctx *trigger;
  311. unsigned long flags;
  312. int ret;
  313. if (vdev->ctx[0].trigger) {
  314. free_irq(pdev->irq, vdev);
  315. kfree(vdev->ctx[0].name);
  316. eventfd_ctx_put(vdev->ctx[0].trigger);
  317. vdev->ctx[0].trigger = NULL;
  318. }
  319. if (fd < 0) /* Disable only */
  320. return 0;
  321. vdev->ctx[0].name = kasprintf(GFP_KERNEL, "vfio-intx(%s)",
  322. pci_name(pdev));
  323. if (!vdev->ctx[0].name)
  324. return -ENOMEM;
  325. trigger = eventfd_ctx_fdget(fd);
  326. if (IS_ERR(trigger)) {
  327. kfree(vdev->ctx[0].name);
  328. return PTR_ERR(trigger);
  329. }
  330. vdev->ctx[0].trigger = trigger;
  331. if (!vdev->pci_2_3)
  332. irqflags = 0;
  333. ret = request_irq(pdev->irq, vfio_intx_handler,
  334. irqflags, vdev->ctx[0].name, vdev);
  335. if (ret) {
  336. vdev->ctx[0].trigger = NULL;
  337. kfree(vdev->ctx[0].name);
  338. eventfd_ctx_put(trigger);
  339. return ret;
  340. }
  341. /*
  342. * INTx disable will stick across the new irq setup,
  343. * disable_irq won't.
  344. */
  345. spin_lock_irqsave(&vdev->irqlock, flags);
  346. if (!vdev->pci_2_3 && (vdev->ctx[0].masked || vdev->virq_disabled))
  347. disable_irq_nosync(pdev->irq);
  348. spin_unlock_irqrestore(&vdev->irqlock, flags);
  349. return 0;
  350. }
  351. static void vfio_intx_disable(struct vfio_pci_device *vdev)
  352. {
  353. vfio_intx_set_signal(vdev, -1);
  354. virqfd_disable(vdev, &vdev->ctx[0].unmask);
  355. virqfd_disable(vdev, &vdev->ctx[0].mask);
  356. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  357. vdev->num_ctx = 0;
  358. kfree(vdev->ctx);
  359. }
  360. /*
  361. * MSI/MSI-X
  362. */
  363. static irqreturn_t vfio_msihandler(int irq, void *arg)
  364. {
  365. struct eventfd_ctx *trigger = arg;
  366. eventfd_signal(trigger, 1);
  367. return IRQ_HANDLED;
  368. }
  369. static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
  370. {
  371. struct pci_dev *pdev = vdev->pdev;
  372. int ret;
  373. if (!is_irq_none(vdev))
  374. return -EINVAL;
  375. vdev->ctx = kzalloc(nvec * sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
  376. if (!vdev->ctx)
  377. return -ENOMEM;
  378. if (msix) {
  379. int i;
  380. vdev->msix = kzalloc(nvec * sizeof(struct msix_entry),
  381. GFP_KERNEL);
  382. if (!vdev->msix) {
  383. kfree(vdev->ctx);
  384. return -ENOMEM;
  385. }
  386. for (i = 0; i < nvec; i++)
  387. vdev->msix[i].entry = i;
  388. ret = pci_enable_msix(pdev, vdev->msix, nvec);
  389. if (ret) {
  390. kfree(vdev->msix);
  391. kfree(vdev->ctx);
  392. return ret;
  393. }
  394. } else {
  395. ret = pci_enable_msi_block(pdev, nvec);
  396. if (ret) {
  397. kfree(vdev->ctx);
  398. return ret;
  399. }
  400. }
  401. vdev->num_ctx = nvec;
  402. vdev->irq_type = msix ? VFIO_PCI_MSIX_IRQ_INDEX :
  403. VFIO_PCI_MSI_IRQ_INDEX;
  404. if (!msix) {
  405. /*
  406. * Compute the virtual hardware field for max msi vectors -
  407. * it is the log base 2 of the number of vectors.
  408. */
  409. vdev->msi_qmax = fls(nvec * 2 - 1) - 1;
  410. }
  411. return 0;
  412. }
  413. static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
  414. int vector, int fd, bool msix)
  415. {
  416. struct pci_dev *pdev = vdev->pdev;
  417. int irq = msix ? vdev->msix[vector].vector : pdev->irq + vector;
  418. char *name = msix ? "vfio-msix" : "vfio-msi";
  419. struct eventfd_ctx *trigger;
  420. int ret;
  421. if (vector >= vdev->num_ctx)
  422. return -EINVAL;
  423. if (vdev->ctx[vector].trigger) {
  424. free_irq(irq, vdev->ctx[vector].trigger);
  425. kfree(vdev->ctx[vector].name);
  426. eventfd_ctx_put(vdev->ctx[vector].trigger);
  427. vdev->ctx[vector].trigger = NULL;
  428. }
  429. if (fd < 0)
  430. return 0;
  431. vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "%s[%d](%s)",
  432. name, vector, pci_name(pdev));
  433. if (!vdev->ctx[vector].name)
  434. return -ENOMEM;
  435. trigger = eventfd_ctx_fdget(fd);
  436. if (IS_ERR(trigger)) {
  437. kfree(vdev->ctx[vector].name);
  438. return PTR_ERR(trigger);
  439. }
  440. ret = request_irq(irq, vfio_msihandler, 0,
  441. vdev->ctx[vector].name, trigger);
  442. if (ret) {
  443. kfree(vdev->ctx[vector].name);
  444. eventfd_ctx_put(trigger);
  445. return ret;
  446. }
  447. vdev->ctx[vector].trigger = trigger;
  448. return 0;
  449. }
  450. static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start,
  451. unsigned count, int32_t *fds, bool msix)
  452. {
  453. int i, j, ret = 0;
  454. if (start + count > vdev->num_ctx)
  455. return -EINVAL;
  456. for (i = 0, j = start; i < count && !ret; i++, j++) {
  457. int fd = fds ? fds[i] : -1;
  458. ret = vfio_msi_set_vector_signal(vdev, j, fd, msix);
  459. }
  460. if (ret) {
  461. for (--j; j >= start; j--)
  462. vfio_msi_set_vector_signal(vdev, j, -1, msix);
  463. }
  464. return ret;
  465. }
  466. static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
  467. {
  468. struct pci_dev *pdev = vdev->pdev;
  469. int i;
  470. vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
  471. for (i = 0; i < vdev->num_ctx; i++) {
  472. virqfd_disable(vdev, &vdev->ctx[i].unmask);
  473. virqfd_disable(vdev, &vdev->ctx[i].mask);
  474. }
  475. if (msix) {
  476. pci_disable_msix(vdev->pdev);
  477. kfree(vdev->msix);
  478. } else
  479. pci_disable_msi(pdev);
  480. vdev->irq_type = VFIO_PCI_NUM_IRQS;
  481. vdev->num_ctx = 0;
  482. kfree(vdev->ctx);
  483. }
  484. /*
  485. * IOCTL support
  486. */
  487. static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
  488. unsigned index, unsigned start,
  489. unsigned count, uint32_t flags, void *data)
  490. {
  491. if (!is_intx(vdev) || start != 0 || count != 1)
  492. return -EINVAL;
  493. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  494. vfio_pci_intx_unmask(vdev);
  495. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  496. uint8_t unmask = *(uint8_t *)data;
  497. if (unmask)
  498. vfio_pci_intx_unmask(vdev);
  499. } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  500. int32_t fd = *(int32_t *)data;
  501. if (fd >= 0)
  502. return virqfd_enable(vdev, vfio_pci_intx_unmask_handler,
  503. vfio_send_intx_eventfd, NULL,
  504. &vdev->ctx[0].unmask, fd);
  505. virqfd_disable(vdev, &vdev->ctx[0].unmask);
  506. }
  507. return 0;
  508. }
  509. static int vfio_pci_set_intx_mask(struct vfio_pci_device *vdev,
  510. unsigned index, unsigned start,
  511. unsigned count, uint32_t flags, void *data)
  512. {
  513. if (!is_intx(vdev) || start != 0 || count != 1)
  514. return -EINVAL;
  515. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  516. vfio_pci_intx_mask(vdev);
  517. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  518. uint8_t mask = *(uint8_t *)data;
  519. if (mask)
  520. vfio_pci_intx_mask(vdev);
  521. } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  522. return -ENOTTY; /* XXX implement me */
  523. }
  524. return 0;
  525. }
  526. static int vfio_pci_set_intx_trigger(struct vfio_pci_device *vdev,
  527. unsigned index, unsigned start,
  528. unsigned count, uint32_t flags, void *data)
  529. {
  530. if (is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
  531. vfio_intx_disable(vdev);
  532. return 0;
  533. }
  534. if (!(is_intx(vdev) || is_irq_none(vdev)) || start != 0 || count != 1)
  535. return -EINVAL;
  536. if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  537. int32_t fd = *(int32_t *)data;
  538. int ret;
  539. if (is_intx(vdev))
  540. return vfio_intx_set_signal(vdev, fd);
  541. ret = vfio_intx_enable(vdev);
  542. if (ret)
  543. return ret;
  544. ret = vfio_intx_set_signal(vdev, fd);
  545. if (ret)
  546. vfio_intx_disable(vdev);
  547. return ret;
  548. }
  549. if (!is_intx(vdev))
  550. return -EINVAL;
  551. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  552. vfio_send_intx_eventfd(vdev, NULL);
  553. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  554. uint8_t trigger = *(uint8_t *)data;
  555. if (trigger)
  556. vfio_send_intx_eventfd(vdev, NULL);
  557. }
  558. return 0;
  559. }
  560. static int vfio_pci_set_msi_trigger(struct vfio_pci_device *vdev,
  561. unsigned index, unsigned start,
  562. unsigned count, uint32_t flags, void *data)
  563. {
  564. int i;
  565. bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false;
  566. if (irq_is(vdev, index) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
  567. vfio_msi_disable(vdev, msix);
  568. return 0;
  569. }
  570. if (!(irq_is(vdev, index) || is_irq_none(vdev)))
  571. return -EINVAL;
  572. if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
  573. int32_t *fds = data;
  574. int ret;
  575. if (vdev->irq_type == index)
  576. return vfio_msi_set_block(vdev, start, count,
  577. fds, msix);
  578. ret = vfio_msi_enable(vdev, start + count, msix);
  579. if (ret)
  580. return ret;
  581. ret = vfio_msi_set_block(vdev, start, count, fds, msix);
  582. if (ret)
  583. vfio_msi_disable(vdev, msix);
  584. return ret;
  585. }
  586. if (!irq_is(vdev, index) || start + count > vdev->num_ctx)
  587. return -EINVAL;
  588. for (i = start; i < start + count; i++) {
  589. if (!vdev->ctx[i].trigger)
  590. continue;
  591. if (flags & VFIO_IRQ_SET_DATA_NONE) {
  592. eventfd_signal(vdev->ctx[i].trigger, 1);
  593. } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
  594. uint8_t *bools = data;
  595. if (bools[i - start])
  596. eventfd_signal(vdev->ctx[i].trigger, 1);
  597. }
  598. }
  599. return 0;
  600. }
  601. int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev, uint32_t flags,
  602. unsigned index, unsigned start, unsigned count,
  603. void *data)
  604. {
  605. int (*func)(struct vfio_pci_device *vdev, unsigned index,
  606. unsigned start, unsigned count, uint32_t flags,
  607. void *data) = NULL;
  608. switch (index) {
  609. case VFIO_PCI_INTX_IRQ_INDEX:
  610. switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
  611. case VFIO_IRQ_SET_ACTION_MASK:
  612. func = vfio_pci_set_intx_mask;
  613. break;
  614. case VFIO_IRQ_SET_ACTION_UNMASK:
  615. func = vfio_pci_set_intx_unmask;
  616. break;
  617. case VFIO_IRQ_SET_ACTION_TRIGGER:
  618. func = vfio_pci_set_intx_trigger;
  619. break;
  620. }
  621. break;
  622. case VFIO_PCI_MSI_IRQ_INDEX:
  623. case VFIO_PCI_MSIX_IRQ_INDEX:
  624. switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
  625. case VFIO_IRQ_SET_ACTION_MASK:
  626. case VFIO_IRQ_SET_ACTION_UNMASK:
  627. /* XXX Need masking support exported */
  628. break;
  629. case VFIO_IRQ_SET_ACTION_TRIGGER:
  630. func = vfio_pci_set_msi_trigger;
  631. break;
  632. }
  633. break;
  634. }
  635. if (!func)
  636. return -ENOTTY;
  637. return func(vdev, index, start, count, flags, data);
  638. }