vfio_pci_intrs.c 17 KB

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