2 * VFIO PCI interrupt handling
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
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.
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
16 #include <linux/device.h>
17 #include <linux/interrupt.h>
18 #include <linux/eventfd.h>
19 #include <linux/pci.h>
20 #include <linux/file.h>
21 #include <linux/poll.h>
22 #include <linux/vfio.h>
23 #include <linux/wait.h>
24 #include <linux/workqueue.h>
26 #include "vfio_pci_private.h"
32 struct vfio_pci_device *vdev;
33 struct eventfd_ctx *eventfd;
34 int (*handler)(struct vfio_pci_device *, void *);
35 void (*thread)(struct vfio_pci_device *, void *);
37 struct work_struct inject;
40 struct work_struct shutdown;
41 struct virqfd **pvirqfd;
44 static struct workqueue_struct *vfio_irqfd_cleanup_wq;
46 int __init vfio_pci_virqfd_init(void)
48 vfio_irqfd_cleanup_wq =
49 create_singlethread_workqueue("vfio-irqfd-cleanup");
50 if (!vfio_irqfd_cleanup_wq)
56 void vfio_pci_virqfd_exit(void)
58 destroy_workqueue(vfio_irqfd_cleanup_wq);
61 static void virqfd_deactivate(struct virqfd *virqfd)
63 queue_work(vfio_irqfd_cleanup_wq, &virqfd->shutdown);
66 static int virqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
68 struct virqfd *virqfd = container_of(wait, struct virqfd, wait);
69 unsigned long flags = (unsigned long)key;
72 /* An event has been signaled, call function */
73 if ((!virqfd->handler ||
74 virqfd->handler(virqfd->vdev, virqfd->data)) &&
76 schedule_work(&virqfd->inject);
79 if (flags & POLLHUP) {
81 spin_lock_irqsave(&virqfd->vdev->irqlock, flags);
84 * The eventfd is closing, if the virqfd has not yet been
85 * queued for release, as determined by testing whether the
86 * vdev pointer to it is still valid, queue it now. As
87 * with kvm irqfds, we know we won't race against the virqfd
88 * going away because we hold wqh->lock to get here.
90 if (*(virqfd->pvirqfd) == virqfd) {
91 *(virqfd->pvirqfd) = NULL;
92 virqfd_deactivate(virqfd);
95 spin_unlock_irqrestore(&virqfd->vdev->irqlock, flags);
101 static void virqfd_ptable_queue_proc(struct file *file,
102 wait_queue_head_t *wqh, poll_table *pt)
104 struct virqfd *virqfd = container_of(pt, struct virqfd, pt);
105 add_wait_queue(wqh, &virqfd->wait);
108 static void virqfd_shutdown(struct work_struct *work)
110 struct virqfd *virqfd = container_of(work, struct virqfd, shutdown);
113 eventfd_ctx_remove_wait_queue(virqfd->eventfd, &virqfd->wait, &cnt);
114 flush_work(&virqfd->inject);
115 eventfd_ctx_put(virqfd->eventfd);
120 static void virqfd_inject(struct work_struct *work)
122 struct virqfd *virqfd = container_of(work, struct virqfd, inject);
124 virqfd->thread(virqfd->vdev, virqfd->data);
127 static int virqfd_enable(struct vfio_pci_device *vdev,
128 int (*handler)(struct vfio_pci_device *, void *),
129 void (*thread)(struct vfio_pci_device *, void *),
130 void *data, struct virqfd **pvirqfd, int fd)
132 struct file *file = NULL;
133 struct eventfd_ctx *ctx = NULL;
134 struct virqfd *virqfd;
138 virqfd = kzalloc(sizeof(*virqfd), GFP_KERNEL);
142 virqfd->pvirqfd = pvirqfd;
144 virqfd->handler = handler;
145 virqfd->thread = thread;
148 INIT_WORK(&virqfd->shutdown, virqfd_shutdown);
149 INIT_WORK(&virqfd->inject, virqfd_inject);
151 file = eventfd_fget(fd);
157 ctx = eventfd_ctx_fileget(file);
163 virqfd->eventfd = ctx;
166 * virqfds can be released by closing the eventfd or directly
167 * through ioctl. These are both done through a workqueue, so
168 * we update the pointer to the virqfd under lock to avoid
169 * pushing multiple jobs to release the same virqfd.
171 spin_lock_irq(&vdev->irqlock);
174 spin_unlock_irq(&vdev->irqlock);
180 spin_unlock_irq(&vdev->irqlock);
183 * Install our own custom wake-up handling so we are notified via
184 * a callback whenever someone signals the underlying eventfd.
186 init_waitqueue_func_entry(&virqfd->wait, virqfd_wakeup);
187 init_poll_funcptr(&virqfd->pt, virqfd_ptable_queue_proc);
189 events = file->f_op->poll(file, &virqfd->pt);
192 * Check if there was an event already pending on the eventfd
193 * before we registered and trigger it as if we didn't miss it.
195 if (events & POLLIN) {
196 if ((!handler || handler(vdev, data)) && thread)
197 schedule_work(&virqfd->inject);
201 * Do not drop the file until the irqfd is fully initialized,
202 * otherwise we might race against the POLLHUP.
209 if (ctx && !IS_ERR(ctx))
210 eventfd_ctx_put(ctx);
212 if (file && !IS_ERR(file))
220 static void virqfd_disable(struct vfio_pci_device *vdev,
221 struct virqfd **pvirqfd)
225 spin_lock_irqsave(&vdev->irqlock, flags);
228 virqfd_deactivate(*pvirqfd);
232 spin_unlock_irqrestore(&vdev->irqlock, flags);
235 * Block until we know all outstanding shutdown jobs have completed.
236 * Even if we don't queue the job, flush the wq to be sure it's
239 flush_workqueue(vfio_irqfd_cleanup_wq);
245 static void vfio_send_intx_eventfd(struct vfio_pci_device *vdev, void *unused)
247 if (likely(is_intx(vdev) && !vdev->virq_disabled))
248 eventfd_signal(vdev->ctx[0].trigger, 1);
251 void vfio_pci_intx_mask(struct vfio_pci_device *vdev)
253 struct pci_dev *pdev = vdev->pdev;
256 spin_lock_irqsave(&vdev->irqlock, flags);
259 * Masking can come from interrupt, ioctl, or config space
260 * via INTx disable. The latter means this can get called
261 * even when not using intx delivery. In this case, just
262 * try to have the physical bit follow the virtual bit.
264 if (unlikely(!is_intx(vdev))) {
267 } else if (!vdev->ctx[0].masked) {
269 * Can't use check_and_mask here because we always want to
270 * mask, not just when something is pending.
275 disable_irq_nosync(pdev->irq);
277 vdev->ctx[0].masked = true;
280 spin_unlock_irqrestore(&vdev->irqlock, flags);
284 * If this is triggered by an eventfd, we can't call eventfd_signal
285 * or else we'll deadlock on the eventfd wait queue. Return >0 when
286 * a signal is necessary, which can then be handled via a work queue
287 * or directly depending on the caller.
289 int vfio_pci_intx_unmask_handler(struct vfio_pci_device *vdev, void *unused)
291 struct pci_dev *pdev = vdev->pdev;
295 spin_lock_irqsave(&vdev->irqlock, flags);
298 * Unmasking comes from ioctl or config, so again, have the
299 * physical bit follow the virtual even when not using INTx.
301 if (unlikely(!is_intx(vdev))) {
304 } else if (vdev->ctx[0].masked && !vdev->virq_disabled) {
306 * A pending interrupt here would immediately trigger,
307 * but we can avoid that overhead by just re-sending
308 * the interrupt to the user.
311 if (!pci_check_and_unmask_intx(pdev))
314 enable_irq(pdev->irq);
316 vdev->ctx[0].masked = (ret > 0);
319 spin_unlock_irqrestore(&vdev->irqlock, flags);
324 void vfio_pci_intx_unmask(struct vfio_pci_device *vdev)
326 if (vfio_pci_intx_unmask_handler(vdev, NULL) > 0)
327 vfio_send_intx_eventfd(vdev, NULL);
330 static irqreturn_t vfio_intx_handler(int irq, void *dev_id)
332 struct vfio_pci_device *vdev = dev_id;
336 spin_lock_irqsave(&vdev->irqlock, flags);
338 if (!vdev->pci_2_3) {
339 disable_irq_nosync(vdev->pdev->irq);
340 vdev->ctx[0].masked = true;
342 } else if (!vdev->ctx[0].masked && /* may be shared */
343 pci_check_and_mask_intx(vdev->pdev)) {
344 vdev->ctx[0].masked = true;
348 spin_unlock_irqrestore(&vdev->irqlock, flags);
350 if (ret == IRQ_HANDLED)
351 vfio_send_intx_eventfd(vdev, NULL);
356 static int vfio_intx_enable(struct vfio_pci_device *vdev)
358 if (!is_irq_none(vdev))
361 if (!vdev->pdev->irq)
364 vdev->ctx = kzalloc(sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
369 vdev->irq_type = VFIO_PCI_INTX_IRQ_INDEX;
374 static int vfio_intx_set_signal(struct vfio_pci_device *vdev, int fd)
376 struct pci_dev *pdev = vdev->pdev;
377 unsigned long irqflags = IRQF_SHARED;
378 struct eventfd_ctx *trigger;
382 if (vdev->ctx[0].trigger) {
383 free_irq(pdev->irq, vdev);
384 kfree(vdev->ctx[0].name);
385 eventfd_ctx_put(vdev->ctx[0].trigger);
386 vdev->ctx[0].trigger = NULL;
389 if (fd < 0) /* Disable only */
392 vdev->ctx[0].name = kasprintf(GFP_KERNEL, "vfio-intx(%s)",
394 if (!vdev->ctx[0].name)
397 trigger = eventfd_ctx_fdget(fd);
398 if (IS_ERR(trigger)) {
399 kfree(vdev->ctx[0].name);
400 return PTR_ERR(trigger);
403 vdev->ctx[0].trigger = trigger;
408 ret = request_irq(pdev->irq, vfio_intx_handler,
409 irqflags, vdev->ctx[0].name, vdev);
411 vdev->ctx[0].trigger = NULL;
412 kfree(vdev->ctx[0].name);
413 eventfd_ctx_put(trigger);
418 * INTx disable will stick across the new irq setup,
421 spin_lock_irqsave(&vdev->irqlock, flags);
422 if (!vdev->pci_2_3 && (vdev->ctx[0].masked || vdev->virq_disabled))
423 disable_irq_nosync(pdev->irq);
424 spin_unlock_irqrestore(&vdev->irqlock, flags);
429 static void vfio_intx_disable(struct vfio_pci_device *vdev)
431 vfio_intx_set_signal(vdev, -1);
432 virqfd_disable(vdev, &vdev->ctx[0].unmask);
433 virqfd_disable(vdev, &vdev->ctx[0].mask);
434 vdev->irq_type = VFIO_PCI_NUM_IRQS;
442 static irqreturn_t vfio_msihandler(int irq, void *arg)
444 struct eventfd_ctx *trigger = arg;
446 eventfd_signal(trigger, 1);
450 static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
452 struct pci_dev *pdev = vdev->pdev;
455 if (!is_irq_none(vdev))
458 vdev->ctx = kzalloc(nvec * sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL);
465 vdev->msix = kzalloc(nvec * sizeof(struct msix_entry),
472 for (i = 0; i < nvec; i++)
473 vdev->msix[i].entry = i;
475 ret = pci_enable_msix(pdev, vdev->msix, nvec);
482 ret = pci_enable_msi_block(pdev, nvec);
489 vdev->num_ctx = nvec;
490 vdev->irq_type = msix ? VFIO_PCI_MSIX_IRQ_INDEX :
491 VFIO_PCI_MSI_IRQ_INDEX;
495 * Compute the virtual hardware field for max msi vectors -
496 * it is the log base 2 of the number of vectors.
498 vdev->msi_qmax = fls(nvec * 2 - 1) - 1;
504 static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
505 int vector, int fd, bool msix)
507 struct pci_dev *pdev = vdev->pdev;
508 int irq = msix ? vdev->msix[vector].vector : pdev->irq + vector;
509 char *name = msix ? "vfio-msix" : "vfio-msi";
510 struct eventfd_ctx *trigger;
513 if (vector >= vdev->num_ctx)
516 if (vdev->ctx[vector].trigger) {
517 free_irq(irq, vdev->ctx[vector].trigger);
518 kfree(vdev->ctx[vector].name);
519 eventfd_ctx_put(vdev->ctx[vector].trigger);
520 vdev->ctx[vector].trigger = NULL;
526 vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "%s[%d](%s)",
527 name, vector, pci_name(pdev));
528 if (!vdev->ctx[vector].name)
531 trigger = eventfd_ctx_fdget(fd);
532 if (IS_ERR(trigger)) {
533 kfree(vdev->ctx[vector].name);
534 return PTR_ERR(trigger);
537 ret = request_irq(irq, vfio_msihandler, 0,
538 vdev->ctx[vector].name, trigger);
540 kfree(vdev->ctx[vector].name);
541 eventfd_ctx_put(trigger);
545 vdev->ctx[vector].trigger = trigger;
550 static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start,
551 unsigned count, int32_t *fds, bool msix)
555 if (start + count > vdev->num_ctx)
558 for (i = 0, j = start; i < count && !ret; i++, j++) {
559 int fd = fds ? fds[i] : -1;
560 ret = vfio_msi_set_vector_signal(vdev, j, fd, msix);
564 for (--j; j >= start; j--)
565 vfio_msi_set_vector_signal(vdev, j, -1, msix);
571 static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix)
573 struct pci_dev *pdev = vdev->pdev;
576 vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix);
578 for (i = 0; i < vdev->num_ctx; i++) {
579 virqfd_disable(vdev, &vdev->ctx[i].unmask);
580 virqfd_disable(vdev, &vdev->ctx[i].mask);
584 pci_disable_msix(vdev->pdev);
587 pci_disable_msi(pdev);
589 vdev->irq_type = VFIO_PCI_NUM_IRQS;
597 static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev,
598 unsigned index, unsigned start,
599 unsigned count, uint32_t flags, void *data)
601 if (!is_intx(vdev) || start != 0 || count != 1)
604 if (flags & VFIO_IRQ_SET_DATA_NONE) {
605 vfio_pci_intx_unmask(vdev);
606 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
607 uint8_t unmask = *(uint8_t *)data;
609 vfio_pci_intx_unmask(vdev);
610 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
611 int32_t fd = *(int32_t *)data;
613 return virqfd_enable(vdev, vfio_pci_intx_unmask_handler,
614 vfio_send_intx_eventfd, NULL,
615 &vdev->ctx[0].unmask, fd);
617 virqfd_disable(vdev, &vdev->ctx[0].unmask);
623 static int vfio_pci_set_intx_mask(struct vfio_pci_device *vdev,
624 unsigned index, unsigned start,
625 unsigned count, uint32_t flags, void *data)
627 if (!is_intx(vdev) || start != 0 || count != 1)
630 if (flags & VFIO_IRQ_SET_DATA_NONE) {
631 vfio_pci_intx_mask(vdev);
632 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
633 uint8_t mask = *(uint8_t *)data;
635 vfio_pci_intx_mask(vdev);
636 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
637 return -ENOTTY; /* XXX implement me */
643 static int vfio_pci_set_intx_trigger(struct vfio_pci_device *vdev,
644 unsigned index, unsigned start,
645 unsigned count, uint32_t flags, void *data)
647 if (is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
648 vfio_intx_disable(vdev);
652 if (!(is_intx(vdev) || is_irq_none(vdev)) || start != 0 || count != 1)
655 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
656 int32_t fd = *(int32_t *)data;
660 return vfio_intx_set_signal(vdev, fd);
662 ret = vfio_intx_enable(vdev);
666 ret = vfio_intx_set_signal(vdev, fd);
668 vfio_intx_disable(vdev);
676 if (flags & VFIO_IRQ_SET_DATA_NONE) {
677 vfio_send_intx_eventfd(vdev, NULL);
678 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
679 uint8_t trigger = *(uint8_t *)data;
681 vfio_send_intx_eventfd(vdev, NULL);
686 static int vfio_pci_set_msi_trigger(struct vfio_pci_device *vdev,
687 unsigned index, unsigned start,
688 unsigned count, uint32_t flags, void *data)
691 bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false;
693 if (irq_is(vdev, index) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
694 vfio_msi_disable(vdev, msix);
698 if (!(irq_is(vdev, index) || is_irq_none(vdev)))
701 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
705 if (vdev->irq_type == index)
706 return vfio_msi_set_block(vdev, start, count,
709 ret = vfio_msi_enable(vdev, start + count, msix);
713 ret = vfio_msi_set_block(vdev, start, count, fds, msix);
715 vfio_msi_disable(vdev, msix);
720 if (!irq_is(vdev, index) || start + count > vdev->num_ctx)
723 for (i = start; i < start + count; i++) {
724 if (!vdev->ctx[i].trigger)
726 if (flags & VFIO_IRQ_SET_DATA_NONE) {
727 eventfd_signal(vdev->ctx[i].trigger, 1);
728 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
729 uint8_t *bools = data;
730 if (bools[i - start])
731 eventfd_signal(vdev->ctx[i].trigger, 1);
737 int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev, uint32_t flags,
738 unsigned index, unsigned start, unsigned count,
741 int (*func)(struct vfio_pci_device *vdev, unsigned index,
742 unsigned start, unsigned count, uint32_t flags,
746 case VFIO_PCI_INTX_IRQ_INDEX:
747 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
748 case VFIO_IRQ_SET_ACTION_MASK:
749 func = vfio_pci_set_intx_mask;
751 case VFIO_IRQ_SET_ACTION_UNMASK:
752 func = vfio_pci_set_intx_unmask;
754 case VFIO_IRQ_SET_ACTION_TRIGGER:
755 func = vfio_pci_set_intx_trigger;
759 case VFIO_PCI_MSI_IRQ_INDEX:
760 case VFIO_PCI_MSIX_IRQ_INDEX:
761 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
762 case VFIO_IRQ_SET_ACTION_MASK:
763 case VFIO_IRQ_SET_ACTION_UNMASK:
764 /* XXX Need masking support exported */
766 case VFIO_IRQ_SET_ACTION_TRIGGER:
767 func = vfio_pci_set_msi_trigger;
776 return func(vdev, index, start, count, flags, data);