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>
25 #include <linux/slab.h>
27 #include "vfio_pci_private.h"
33 struct vfio_pci_device
*vdev
;
34 struct eventfd_ctx
*eventfd
;
35 int (*handler
)(struct vfio_pci_device
*, void *);
36 void (*thread
)(struct vfio_pci_device
*, void *);
38 struct work_struct inject
;
41 struct work_struct shutdown
;
42 struct virqfd
**pvirqfd
;
45 static struct workqueue_struct
*vfio_irqfd_cleanup_wq
;
47 int __init
vfio_pci_virqfd_init(void)
49 vfio_irqfd_cleanup_wq
=
50 create_singlethread_workqueue("vfio-irqfd-cleanup");
51 if (!vfio_irqfd_cleanup_wq
)
57 void vfio_pci_virqfd_exit(void)
59 destroy_workqueue(vfio_irqfd_cleanup_wq
);
62 static void virqfd_deactivate(struct virqfd
*virqfd
)
64 queue_work(vfio_irqfd_cleanup_wq
, &virqfd
->shutdown
);
67 static int virqfd_wakeup(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
)
69 struct virqfd
*virqfd
= container_of(wait
, struct virqfd
, wait
);
70 unsigned long flags
= (unsigned long)key
;
73 /* An event has been signaled, call function */
74 if ((!virqfd
->handler
||
75 virqfd
->handler(virqfd
->vdev
, virqfd
->data
)) &&
77 schedule_work(&virqfd
->inject
);
80 if (flags
& POLLHUP
) {
82 spin_lock_irqsave(&virqfd
->vdev
->irqlock
, flags
);
85 * The eventfd is closing, if the virqfd has not yet been
86 * queued for release, as determined by testing whether the
87 * vdev pointer to it is still valid, queue it now. As
88 * with kvm irqfds, we know we won't race against the virqfd
89 * going away because we hold wqh->lock to get here.
91 if (*(virqfd
->pvirqfd
) == virqfd
) {
92 *(virqfd
->pvirqfd
) = NULL
;
93 virqfd_deactivate(virqfd
);
96 spin_unlock_irqrestore(&virqfd
->vdev
->irqlock
, flags
);
102 static void virqfd_ptable_queue_proc(struct file
*file
,
103 wait_queue_head_t
*wqh
, poll_table
*pt
)
105 struct virqfd
*virqfd
= container_of(pt
, struct virqfd
, pt
);
106 add_wait_queue(wqh
, &virqfd
->wait
);
109 static void virqfd_shutdown(struct work_struct
*work
)
111 struct virqfd
*virqfd
= container_of(work
, struct virqfd
, shutdown
);
114 eventfd_ctx_remove_wait_queue(virqfd
->eventfd
, &virqfd
->wait
, &cnt
);
115 flush_work(&virqfd
->inject
);
116 eventfd_ctx_put(virqfd
->eventfd
);
121 static void virqfd_inject(struct work_struct
*work
)
123 struct virqfd
*virqfd
= container_of(work
, struct virqfd
, inject
);
125 virqfd
->thread(virqfd
->vdev
, virqfd
->data
);
128 static int virqfd_enable(struct vfio_pci_device
*vdev
,
129 int (*handler
)(struct vfio_pci_device
*, void *),
130 void (*thread
)(struct vfio_pci_device
*, void *),
131 void *data
, struct virqfd
**pvirqfd
, int fd
)
134 struct eventfd_ctx
*ctx
;
135 struct virqfd
*virqfd
;
139 virqfd
= kzalloc(sizeof(*virqfd
), GFP_KERNEL
);
143 virqfd
->pvirqfd
= pvirqfd
;
145 virqfd
->handler
= handler
;
146 virqfd
->thread
= thread
;
149 INIT_WORK(&virqfd
->shutdown
, virqfd_shutdown
);
150 INIT_WORK(&virqfd
->inject
, virqfd_inject
);
158 ctx
= eventfd_ctx_fileget(irqfd
.file
);
164 virqfd
->eventfd
= ctx
;
167 * virqfds can be released by closing the eventfd or directly
168 * through ioctl. These are both done through a workqueue, so
169 * we update the pointer to the virqfd under lock to avoid
170 * pushing multiple jobs to release the same virqfd.
172 spin_lock_irq(&vdev
->irqlock
);
175 spin_unlock_irq(&vdev
->irqlock
);
181 spin_unlock_irq(&vdev
->irqlock
);
184 * Install our own custom wake-up handling so we are notified via
185 * a callback whenever someone signals the underlying eventfd.
187 init_waitqueue_func_entry(&virqfd
->wait
, virqfd_wakeup
);
188 init_poll_funcptr(&virqfd
->pt
, virqfd_ptable_queue_proc
);
190 events
= irqfd
.file
->f_op
->poll(irqfd
.file
, &virqfd
->pt
);
193 * Check if there was an event already pending on the eventfd
194 * before we registered and trigger it as if we didn't miss it.
196 if (events
& POLLIN
) {
197 if ((!handler
|| handler(vdev
, data
)) && thread
)
198 schedule_work(&virqfd
->inject
);
202 * Do not drop the file until the irqfd is fully initialized,
203 * otherwise we might race against the POLLHUP.
209 eventfd_ctx_put(ctx
);
218 static void virqfd_disable(struct vfio_pci_device
*vdev
,
219 struct virqfd
**pvirqfd
)
223 spin_lock_irqsave(&vdev
->irqlock
, flags
);
226 virqfd_deactivate(*pvirqfd
);
230 spin_unlock_irqrestore(&vdev
->irqlock
, flags
);
233 * Block until we know all outstanding shutdown jobs have completed.
234 * Even if we don't queue the job, flush the wq to be sure it's
237 flush_workqueue(vfio_irqfd_cleanup_wq
);
243 static void vfio_send_intx_eventfd(struct vfio_pci_device
*vdev
, void *unused
)
245 if (likely(is_intx(vdev
) && !vdev
->virq_disabled
))
246 eventfd_signal(vdev
->ctx
[0].trigger
, 1);
249 void vfio_pci_intx_mask(struct vfio_pci_device
*vdev
)
251 struct pci_dev
*pdev
= vdev
->pdev
;
254 spin_lock_irqsave(&vdev
->irqlock
, flags
);
257 * Masking can come from interrupt, ioctl, or config space
258 * via INTx disable. The latter means this can get called
259 * even when not using intx delivery. In this case, just
260 * try to have the physical bit follow the virtual bit.
262 if (unlikely(!is_intx(vdev
))) {
265 } else if (!vdev
->ctx
[0].masked
) {
267 * Can't use check_and_mask here because we always want to
268 * mask, not just when something is pending.
273 disable_irq_nosync(pdev
->irq
);
275 vdev
->ctx
[0].masked
= true;
278 spin_unlock_irqrestore(&vdev
->irqlock
, flags
);
282 * If this is triggered by an eventfd, we can't call eventfd_signal
283 * or else we'll deadlock on the eventfd wait queue. Return >0 when
284 * a signal is necessary, which can then be handled via a work queue
285 * or directly depending on the caller.
287 static int vfio_pci_intx_unmask_handler(struct vfio_pci_device
*vdev
,
290 struct pci_dev
*pdev
= vdev
->pdev
;
294 spin_lock_irqsave(&vdev
->irqlock
, flags
);
297 * Unmasking comes from ioctl or config, so again, have the
298 * physical bit follow the virtual even when not using INTx.
300 if (unlikely(!is_intx(vdev
))) {
303 } else if (vdev
->ctx
[0].masked
&& !vdev
->virq_disabled
) {
305 * A pending interrupt here would immediately trigger,
306 * but we can avoid that overhead by just re-sending
307 * the interrupt to the user.
310 if (!pci_check_and_unmask_intx(pdev
))
313 enable_irq(pdev
->irq
);
315 vdev
->ctx
[0].masked
= (ret
> 0);
318 spin_unlock_irqrestore(&vdev
->irqlock
, flags
);
323 void vfio_pci_intx_unmask(struct vfio_pci_device
*vdev
)
325 if (vfio_pci_intx_unmask_handler(vdev
, NULL
) > 0)
326 vfio_send_intx_eventfd(vdev
, NULL
);
329 static irqreturn_t
vfio_intx_handler(int irq
, void *dev_id
)
331 struct vfio_pci_device
*vdev
= dev_id
;
335 spin_lock_irqsave(&vdev
->irqlock
, flags
);
337 if (!vdev
->pci_2_3
) {
338 disable_irq_nosync(vdev
->pdev
->irq
);
339 vdev
->ctx
[0].masked
= true;
341 } else if (!vdev
->ctx
[0].masked
&& /* may be shared */
342 pci_check_and_mask_intx(vdev
->pdev
)) {
343 vdev
->ctx
[0].masked
= true;
347 spin_unlock_irqrestore(&vdev
->irqlock
, flags
);
349 if (ret
== IRQ_HANDLED
)
350 vfio_send_intx_eventfd(vdev
, NULL
);
355 static int vfio_intx_enable(struct vfio_pci_device
*vdev
)
357 if (!is_irq_none(vdev
))
360 if (!vdev
->pdev
->irq
)
363 vdev
->ctx
= kzalloc(sizeof(struct vfio_pci_irq_ctx
), GFP_KERNEL
);
370 * If the virtual interrupt is masked, restore it. Devices
371 * supporting DisINTx can be masked at the hardware level
372 * here, non-PCI-2.3 devices will have to wait until the
373 * interrupt is enabled.
375 vdev
->ctx
[0].masked
= vdev
->virq_disabled
;
377 pci_intx(vdev
->pdev
, !vdev
->ctx
[0].masked
);
379 vdev
->irq_type
= VFIO_PCI_INTX_IRQ_INDEX
;
384 static int vfio_intx_set_signal(struct vfio_pci_device
*vdev
, int fd
)
386 struct pci_dev
*pdev
= vdev
->pdev
;
387 unsigned long irqflags
= IRQF_SHARED
;
388 struct eventfd_ctx
*trigger
;
392 if (vdev
->ctx
[0].trigger
) {
393 free_irq(pdev
->irq
, vdev
);
394 kfree(vdev
->ctx
[0].name
);
395 eventfd_ctx_put(vdev
->ctx
[0].trigger
);
396 vdev
->ctx
[0].trigger
= NULL
;
399 if (fd
< 0) /* Disable only */
402 vdev
->ctx
[0].name
= kasprintf(GFP_KERNEL
, "vfio-intx(%s)",
404 if (!vdev
->ctx
[0].name
)
407 trigger
= eventfd_ctx_fdget(fd
);
408 if (IS_ERR(trigger
)) {
409 kfree(vdev
->ctx
[0].name
);
410 return PTR_ERR(trigger
);
413 vdev
->ctx
[0].trigger
= trigger
;
418 ret
= request_irq(pdev
->irq
, vfio_intx_handler
,
419 irqflags
, vdev
->ctx
[0].name
, vdev
);
421 vdev
->ctx
[0].trigger
= NULL
;
422 kfree(vdev
->ctx
[0].name
);
423 eventfd_ctx_put(trigger
);
428 * INTx disable will stick across the new irq setup,
431 spin_lock_irqsave(&vdev
->irqlock
, flags
);
432 if (!vdev
->pci_2_3
&& vdev
->ctx
[0].masked
)
433 disable_irq_nosync(pdev
->irq
);
434 spin_unlock_irqrestore(&vdev
->irqlock
, flags
);
439 static void vfio_intx_disable(struct vfio_pci_device
*vdev
)
441 vfio_intx_set_signal(vdev
, -1);
442 virqfd_disable(vdev
, &vdev
->ctx
[0].unmask
);
443 virqfd_disable(vdev
, &vdev
->ctx
[0].mask
);
444 vdev
->irq_type
= VFIO_PCI_NUM_IRQS
;
452 static irqreturn_t
vfio_msihandler(int irq
, void *arg
)
454 struct eventfd_ctx
*trigger
= arg
;
456 eventfd_signal(trigger
, 1);
460 static int vfio_msi_enable(struct vfio_pci_device
*vdev
, int nvec
, bool msix
)
462 struct pci_dev
*pdev
= vdev
->pdev
;
465 if (!is_irq_none(vdev
))
468 vdev
->ctx
= kzalloc(nvec
* sizeof(struct vfio_pci_irq_ctx
), GFP_KERNEL
);
475 vdev
->msix
= kzalloc(nvec
* sizeof(struct msix_entry
),
482 for (i
= 0; i
< nvec
; i
++)
483 vdev
->msix
[i
].entry
= i
;
485 ret
= pci_enable_msix_range(pdev
, vdev
->msix
, 1, nvec
);
488 pci_disable_msix(pdev
);
494 ret
= pci_enable_msi_range(pdev
, 1, nvec
);
497 pci_disable_msi(pdev
);
503 vdev
->num_ctx
= nvec
;
504 vdev
->irq_type
= msix
? VFIO_PCI_MSIX_IRQ_INDEX
:
505 VFIO_PCI_MSI_IRQ_INDEX
;
509 * Compute the virtual hardware field for max msi vectors -
510 * it is the log base 2 of the number of vectors.
512 vdev
->msi_qmax
= fls(nvec
* 2 - 1) - 1;
518 static int vfio_msi_set_vector_signal(struct vfio_pci_device
*vdev
,
519 int vector
, int fd
, bool msix
)
521 struct pci_dev
*pdev
= vdev
->pdev
;
522 int irq
= msix
? vdev
->msix
[vector
].vector
: pdev
->irq
+ vector
;
523 char *name
= msix
? "vfio-msix" : "vfio-msi";
524 struct eventfd_ctx
*trigger
;
527 if (vector
>= vdev
->num_ctx
)
530 if (vdev
->ctx
[vector
].trigger
) {
531 free_irq(irq
, vdev
->ctx
[vector
].trigger
);
532 kfree(vdev
->ctx
[vector
].name
);
533 eventfd_ctx_put(vdev
->ctx
[vector
].trigger
);
534 vdev
->ctx
[vector
].trigger
= NULL
;
540 vdev
->ctx
[vector
].name
= kasprintf(GFP_KERNEL
, "%s[%d](%s)",
541 name
, vector
, pci_name(pdev
));
542 if (!vdev
->ctx
[vector
].name
)
545 trigger
= eventfd_ctx_fdget(fd
);
546 if (IS_ERR(trigger
)) {
547 kfree(vdev
->ctx
[vector
].name
);
548 return PTR_ERR(trigger
);
551 ret
= request_irq(irq
, vfio_msihandler
, 0,
552 vdev
->ctx
[vector
].name
, trigger
);
554 kfree(vdev
->ctx
[vector
].name
);
555 eventfd_ctx_put(trigger
);
559 vdev
->ctx
[vector
].trigger
= trigger
;
564 static int vfio_msi_set_block(struct vfio_pci_device
*vdev
, unsigned start
,
565 unsigned count
, int32_t *fds
, bool msix
)
569 if (start
+ count
> vdev
->num_ctx
)
572 for (i
= 0, j
= start
; i
< count
&& !ret
; i
++, j
++) {
573 int fd
= fds
? fds
[i
] : -1;
574 ret
= vfio_msi_set_vector_signal(vdev
, j
, fd
, msix
);
578 for (--j
; j
>= start
; j
--)
579 vfio_msi_set_vector_signal(vdev
, j
, -1, msix
);
585 static void vfio_msi_disable(struct vfio_pci_device
*vdev
, bool msix
)
587 struct pci_dev
*pdev
= vdev
->pdev
;
590 vfio_msi_set_block(vdev
, 0, vdev
->num_ctx
, NULL
, msix
);
592 for (i
= 0; i
< vdev
->num_ctx
; i
++) {
593 virqfd_disable(vdev
, &vdev
->ctx
[i
].unmask
);
594 virqfd_disable(vdev
, &vdev
->ctx
[i
].mask
);
598 pci_disable_msix(vdev
->pdev
);
601 pci_disable_msi(pdev
);
603 vdev
->irq_type
= VFIO_PCI_NUM_IRQS
;
611 static int vfio_pci_set_intx_unmask(struct vfio_pci_device
*vdev
,
612 unsigned index
, unsigned start
,
613 unsigned count
, uint32_t flags
, void *data
)
615 if (!is_intx(vdev
) || start
!= 0 || count
!= 1)
618 if (flags
& VFIO_IRQ_SET_DATA_NONE
) {
619 vfio_pci_intx_unmask(vdev
);
620 } else if (flags
& VFIO_IRQ_SET_DATA_BOOL
) {
621 uint8_t unmask
= *(uint8_t *)data
;
623 vfio_pci_intx_unmask(vdev
);
624 } else if (flags
& VFIO_IRQ_SET_DATA_EVENTFD
) {
625 int32_t fd
= *(int32_t *)data
;
627 return virqfd_enable(vdev
, vfio_pci_intx_unmask_handler
,
628 vfio_send_intx_eventfd
, NULL
,
629 &vdev
->ctx
[0].unmask
, fd
);
631 virqfd_disable(vdev
, &vdev
->ctx
[0].unmask
);
637 static int vfio_pci_set_intx_mask(struct vfio_pci_device
*vdev
,
638 unsigned index
, unsigned start
,
639 unsigned count
, uint32_t flags
, void *data
)
641 if (!is_intx(vdev
) || start
!= 0 || count
!= 1)
644 if (flags
& VFIO_IRQ_SET_DATA_NONE
) {
645 vfio_pci_intx_mask(vdev
);
646 } else if (flags
& VFIO_IRQ_SET_DATA_BOOL
) {
647 uint8_t mask
= *(uint8_t *)data
;
649 vfio_pci_intx_mask(vdev
);
650 } else if (flags
& VFIO_IRQ_SET_DATA_EVENTFD
) {
651 return -ENOTTY
; /* XXX implement me */
657 static int vfio_pci_set_intx_trigger(struct vfio_pci_device
*vdev
,
658 unsigned index
, unsigned start
,
659 unsigned count
, uint32_t flags
, void *data
)
661 if (is_intx(vdev
) && !count
&& (flags
& VFIO_IRQ_SET_DATA_NONE
)) {
662 vfio_intx_disable(vdev
);
666 if (!(is_intx(vdev
) || is_irq_none(vdev
)) || start
!= 0 || count
!= 1)
669 if (flags
& VFIO_IRQ_SET_DATA_EVENTFD
) {
670 int32_t fd
= *(int32_t *)data
;
674 return vfio_intx_set_signal(vdev
, fd
);
676 ret
= vfio_intx_enable(vdev
);
680 ret
= vfio_intx_set_signal(vdev
, fd
);
682 vfio_intx_disable(vdev
);
690 if (flags
& VFIO_IRQ_SET_DATA_NONE
) {
691 vfio_send_intx_eventfd(vdev
, NULL
);
692 } else if (flags
& VFIO_IRQ_SET_DATA_BOOL
) {
693 uint8_t trigger
= *(uint8_t *)data
;
695 vfio_send_intx_eventfd(vdev
, NULL
);
700 static int vfio_pci_set_msi_trigger(struct vfio_pci_device
*vdev
,
701 unsigned index
, unsigned start
,
702 unsigned count
, uint32_t flags
, void *data
)
705 bool msix
= (index
== VFIO_PCI_MSIX_IRQ_INDEX
) ? true : false;
707 if (irq_is(vdev
, index
) && !count
&& (flags
& VFIO_IRQ_SET_DATA_NONE
)) {
708 vfio_msi_disable(vdev
, msix
);
712 if (!(irq_is(vdev
, index
) || is_irq_none(vdev
)))
715 if (flags
& VFIO_IRQ_SET_DATA_EVENTFD
) {
719 if (vdev
->irq_type
== index
)
720 return vfio_msi_set_block(vdev
, start
, count
,
723 ret
= vfio_msi_enable(vdev
, start
+ count
, msix
);
727 ret
= vfio_msi_set_block(vdev
, start
, count
, fds
, msix
);
729 vfio_msi_disable(vdev
, msix
);
734 if (!irq_is(vdev
, index
) || start
+ count
> vdev
->num_ctx
)
737 for (i
= start
; i
< start
+ count
; i
++) {
738 if (!vdev
->ctx
[i
].trigger
)
740 if (flags
& VFIO_IRQ_SET_DATA_NONE
) {
741 eventfd_signal(vdev
->ctx
[i
].trigger
, 1);
742 } else if (flags
& VFIO_IRQ_SET_DATA_BOOL
) {
743 uint8_t *bools
= data
;
744 if (bools
[i
- start
])
745 eventfd_signal(vdev
->ctx
[i
].trigger
, 1);
751 static int vfio_pci_set_err_trigger(struct vfio_pci_device
*vdev
,
752 unsigned index
, unsigned start
,
753 unsigned count
, uint32_t flags
, void *data
)
755 int32_t fd
= *(int32_t *)data
;
757 if ((index
!= VFIO_PCI_ERR_IRQ_INDEX
) ||
758 !(flags
& VFIO_IRQ_SET_DATA_TYPE_MASK
))
761 /* DATA_NONE/DATA_BOOL enables loopback testing */
762 if (flags
& VFIO_IRQ_SET_DATA_NONE
) {
763 if (vdev
->err_trigger
)
764 eventfd_signal(vdev
->err_trigger
, 1);
766 } else if (flags
& VFIO_IRQ_SET_DATA_BOOL
) {
767 uint8_t trigger
= *(uint8_t *)data
;
768 if (trigger
&& vdev
->err_trigger
)
769 eventfd_signal(vdev
->err_trigger
, 1);
773 /* Handle SET_DATA_EVENTFD */
775 if (vdev
->err_trigger
)
776 eventfd_ctx_put(vdev
->err_trigger
);
777 vdev
->err_trigger
= NULL
;
779 } else if (fd
>= 0) {
780 struct eventfd_ctx
*efdctx
;
781 efdctx
= eventfd_ctx_fdget(fd
);
783 return PTR_ERR(efdctx
);
784 if (vdev
->err_trigger
)
785 eventfd_ctx_put(vdev
->err_trigger
);
786 vdev
->err_trigger
= efdctx
;
791 int vfio_pci_set_irqs_ioctl(struct vfio_pci_device
*vdev
, uint32_t flags
,
792 unsigned index
, unsigned start
, unsigned count
,
795 int (*func
)(struct vfio_pci_device
*vdev
, unsigned index
,
796 unsigned start
, unsigned count
, uint32_t flags
,
800 case VFIO_PCI_INTX_IRQ_INDEX
:
801 switch (flags
& VFIO_IRQ_SET_ACTION_TYPE_MASK
) {
802 case VFIO_IRQ_SET_ACTION_MASK
:
803 func
= vfio_pci_set_intx_mask
;
805 case VFIO_IRQ_SET_ACTION_UNMASK
:
806 func
= vfio_pci_set_intx_unmask
;
808 case VFIO_IRQ_SET_ACTION_TRIGGER
:
809 func
= vfio_pci_set_intx_trigger
;
813 case VFIO_PCI_MSI_IRQ_INDEX
:
814 case VFIO_PCI_MSIX_IRQ_INDEX
:
815 switch (flags
& VFIO_IRQ_SET_ACTION_TYPE_MASK
) {
816 case VFIO_IRQ_SET_ACTION_MASK
:
817 case VFIO_IRQ_SET_ACTION_UNMASK
:
818 /* XXX Need masking support exported */
820 case VFIO_IRQ_SET_ACTION_TRIGGER
:
821 func
= vfio_pci_set_msi_trigger
;
825 case VFIO_PCI_ERR_IRQ_INDEX
:
826 switch (flags
& VFIO_IRQ_SET_ACTION_TYPE_MASK
) {
827 case VFIO_IRQ_SET_ACTION_TRIGGER
:
828 if (pci_is_pcie(vdev
->pdev
))
829 func
= vfio_pci_set_err_trigger
;
837 return func(vdev
, index
, start
, count
, flags
, data
);