vfio_pci_intrs.c 20 KB

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