1 // SPDX-License-Identifier: GPL-2.0-only
3 * VFIO PCI interrupt handling
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
8 * Derived from original vfio:
9 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
10 * Author: Tom Lyon, pugs@cisco.com
13 #include <linux/device.h>
14 #include <linux/interrupt.h>
15 #include <linux/eventfd.h>
16 #include <linux/msi.h>
17 #include <linux/pci.h>
18 #include <linux/file.h>
19 #include <linux/vfio.h>
20 #include <linux/wait.h>
21 #include <linux/slab.h>
23 #include "vfio_pci_priv.h"
25 struct vfio_pci_irq_ctx {
26 struct eventfd_ctx *trigger;
27 struct virqfd *unmask;
31 struct irq_bypass_producer producer;
34 static bool irq_is(struct vfio_pci_core_device *vdev, int type)
36 return vdev->irq_type == type;
39 static bool is_intx(struct vfio_pci_core_device *vdev)
41 return vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX;
44 static bool is_irq_none(struct vfio_pci_core_device *vdev)
46 return !(vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX ||
47 vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX ||
48 vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX);
52 struct vfio_pci_irq_ctx *vfio_irq_ctx_get(struct vfio_pci_core_device *vdev,
55 return xa_load(&vdev->ctx, index);
58 static void vfio_irq_ctx_free(struct vfio_pci_core_device *vdev,
59 struct vfio_pci_irq_ctx *ctx, unsigned long index)
61 xa_erase(&vdev->ctx, index);
65 static struct vfio_pci_irq_ctx *
66 vfio_irq_ctx_alloc(struct vfio_pci_core_device *vdev, unsigned long index)
68 struct vfio_pci_irq_ctx *ctx;
71 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
75 ret = xa_insert(&vdev->ctx, index, ctx, GFP_KERNEL_ACCOUNT);
87 static void vfio_send_intx_eventfd(void *opaque, void *unused)
89 struct vfio_pci_core_device *vdev = opaque;
91 if (likely(is_intx(vdev) && !vdev->virq_disabled)) {
92 struct vfio_pci_irq_ctx *ctx;
93 struct eventfd_ctx *trigger;
95 ctx = vfio_irq_ctx_get(vdev, 0);
96 if (WARN_ON_ONCE(!ctx))
99 trigger = READ_ONCE(ctx->trigger);
101 eventfd_signal(trigger);
105 /* Returns true if the INTx vfio_pci_irq_ctx.masked value is changed. */
106 static bool __vfio_pci_intx_mask(struct vfio_pci_core_device *vdev)
108 struct pci_dev *pdev = vdev->pdev;
109 struct vfio_pci_irq_ctx *ctx;
111 bool masked_changed = false;
113 lockdep_assert_held(&vdev->igate);
115 spin_lock_irqsave(&vdev->irqlock, flags);
118 * Masking can come from interrupt, ioctl, or config space
119 * via INTx disable. The latter means this can get called
120 * even when not using intx delivery. In this case, just
121 * try to have the physical bit follow the virtual bit.
123 if (unlikely(!is_intx(vdev))) {
129 ctx = vfio_irq_ctx_get(vdev, 0);
130 if (WARN_ON_ONCE(!ctx))
135 * Can't use check_and_mask here because we always want to
136 * mask, not just when something is pending.
141 disable_irq_nosync(pdev->irq);
144 masked_changed = true;
148 spin_unlock_irqrestore(&vdev->irqlock, flags);
149 return masked_changed;
152 bool vfio_pci_intx_mask(struct vfio_pci_core_device *vdev)
156 mutex_lock(&vdev->igate);
157 mask_changed = __vfio_pci_intx_mask(vdev);
158 mutex_unlock(&vdev->igate);
164 * If this is triggered by an eventfd, we can't call eventfd_signal
165 * or else we'll deadlock on the eventfd wait queue. Return >0 when
166 * a signal is necessary, which can then be handled via a work queue
167 * or directly depending on the caller.
169 static int vfio_pci_intx_unmask_handler(void *opaque, void *unused)
171 struct vfio_pci_core_device *vdev = opaque;
172 struct pci_dev *pdev = vdev->pdev;
173 struct vfio_pci_irq_ctx *ctx;
177 spin_lock_irqsave(&vdev->irqlock, flags);
180 * Unmasking comes from ioctl or config, so again, have the
181 * physical bit follow the virtual even when not using INTx.
183 if (unlikely(!is_intx(vdev))) {
189 ctx = vfio_irq_ctx_get(vdev, 0);
190 if (WARN_ON_ONCE(!ctx))
193 if (ctx->masked && !vdev->virq_disabled) {
195 * A pending interrupt here would immediately trigger,
196 * but we can avoid that overhead by just re-sending
197 * the interrupt to the user.
200 if (!pci_check_and_unmask_intx(pdev))
203 enable_irq(pdev->irq);
205 ctx->masked = (ret > 0);
209 spin_unlock_irqrestore(&vdev->irqlock, flags);
214 static void __vfio_pci_intx_unmask(struct vfio_pci_core_device *vdev)
216 lockdep_assert_held(&vdev->igate);
218 if (vfio_pci_intx_unmask_handler(vdev, NULL) > 0)
219 vfio_send_intx_eventfd(vdev, NULL);
222 void vfio_pci_intx_unmask(struct vfio_pci_core_device *vdev)
224 mutex_lock(&vdev->igate);
225 __vfio_pci_intx_unmask(vdev);
226 mutex_unlock(&vdev->igate);
229 static irqreturn_t vfio_intx_handler(int irq, void *dev_id)
231 struct vfio_pci_core_device *vdev = dev_id;
232 struct vfio_pci_irq_ctx *ctx;
236 ctx = vfio_irq_ctx_get(vdev, 0);
237 if (WARN_ON_ONCE(!ctx))
240 spin_lock_irqsave(&vdev->irqlock, flags);
242 if (!vdev->pci_2_3) {
243 disable_irq_nosync(vdev->pdev->irq);
246 } else if (!ctx->masked && /* may be shared */
247 pci_check_and_mask_intx(vdev->pdev)) {
252 spin_unlock_irqrestore(&vdev->irqlock, flags);
254 if (ret == IRQ_HANDLED)
255 vfio_send_intx_eventfd(vdev, NULL);
260 static int vfio_intx_enable(struct vfio_pci_core_device *vdev,
261 struct eventfd_ctx *trigger)
263 struct pci_dev *pdev = vdev->pdev;
264 struct vfio_pci_irq_ctx *ctx;
265 unsigned long irqflags;
269 if (!is_irq_none(vdev))
275 name = kasprintf(GFP_KERNEL_ACCOUNT, "vfio-intx(%s)", pci_name(pdev));
279 ctx = vfio_irq_ctx_alloc(vdev, 0);
284 ctx->trigger = trigger;
287 * Fill the initial masked state based on virq_disabled. After
288 * enable, changing the DisINTx bit in vconfig directly changes INTx
289 * masking. igate prevents races during setup, once running masked
290 * is protected via irqlock.
292 * Devices supporting DisINTx also reflect the current mask state in
293 * the physical DisINTx bit, which is not affected during IRQ setup.
295 * Devices without DisINTx support require an exclusive interrupt.
296 * IRQ masking is performed at the IRQ chip. Again, igate protects
297 * against races during setup and IRQ handlers and irqfds are not
298 * yet active, therefore masked is stable and can be used to
299 * conditionally auto-enable the IRQ.
301 * irq_type must be stable while the IRQ handler is registered,
302 * therefore it must be set before request_irq().
304 ctx->masked = vdev->virq_disabled;
306 pci_intx(pdev, !ctx->masked);
307 irqflags = IRQF_SHARED;
309 irqflags = ctx->masked ? IRQF_NO_AUTOEN : 0;
312 vdev->irq_type = VFIO_PCI_INTX_IRQ_INDEX;
314 ret = request_irq(pdev->irq, vfio_intx_handler,
315 irqflags, ctx->name, vdev);
317 vdev->irq_type = VFIO_PCI_NUM_IRQS;
319 vfio_irq_ctx_free(vdev, ctx, 0);
326 static int vfio_intx_set_signal(struct vfio_pci_core_device *vdev,
327 struct eventfd_ctx *trigger)
329 struct pci_dev *pdev = vdev->pdev;
330 struct vfio_pci_irq_ctx *ctx;
331 struct eventfd_ctx *old;
333 ctx = vfio_irq_ctx_get(vdev, 0);
334 if (WARN_ON_ONCE(!ctx))
339 WRITE_ONCE(ctx->trigger, trigger);
341 /* Releasing an old ctx requires synchronizing in-flight users */
343 synchronize_irq(pdev->irq);
344 vfio_virqfd_flush_thread(&ctx->unmask);
345 eventfd_ctx_put(old);
351 static void vfio_intx_disable(struct vfio_pci_core_device *vdev)
353 struct pci_dev *pdev = vdev->pdev;
354 struct vfio_pci_irq_ctx *ctx;
356 ctx = vfio_irq_ctx_get(vdev, 0);
359 vfio_virqfd_disable(&ctx->unmask);
360 vfio_virqfd_disable(&ctx->mask);
361 free_irq(pdev->irq, vdev);
363 eventfd_ctx_put(ctx->trigger);
365 vfio_irq_ctx_free(vdev, ctx, 0);
367 vdev->irq_type = VFIO_PCI_NUM_IRQS;
373 static irqreturn_t vfio_msihandler(int irq, void *arg)
375 struct eventfd_ctx *trigger = arg;
377 eventfd_signal(trigger);
381 static int vfio_msi_enable(struct vfio_pci_core_device *vdev, int nvec, bool msix)
383 struct pci_dev *pdev = vdev->pdev;
384 unsigned int flag = msix ? PCI_IRQ_MSIX : PCI_IRQ_MSI;
388 if (!is_irq_none(vdev))
391 /* return the number of supported vectors if we can't get all: */
392 cmd = vfio_pci_memory_lock_and_enable(vdev);
393 ret = pci_alloc_irq_vectors(pdev, 1, nvec, flag);
396 pci_free_irq_vectors(pdev);
397 vfio_pci_memory_unlock_and_restore(vdev, cmd);
400 vfio_pci_memory_unlock_and_restore(vdev, cmd);
402 vdev->irq_type = msix ? VFIO_PCI_MSIX_IRQ_INDEX :
403 VFIO_PCI_MSI_IRQ_INDEX;
407 * Compute the virtual hardware field for max msi vectors -
408 * it is the log base 2 of the number of vectors.
410 vdev->msi_qmax = fls(nvec * 2 - 1) - 1;
417 * vfio_msi_alloc_irq() returns the Linux IRQ number of an MSI or MSI-X device
418 * interrupt vector. If a Linux IRQ number is not available then a new
419 * interrupt is allocated if dynamic MSI-X is supported.
421 * Where is vfio_msi_free_irq()? Allocated interrupts are maintained,
422 * essentially forming a cache that subsequent allocations can draw from.
423 * Interrupts are freed using pci_free_irq_vectors() when MSI/MSI-X is
426 static int vfio_msi_alloc_irq(struct vfio_pci_core_device *vdev,
427 unsigned int vector, bool msix)
429 struct pci_dev *pdev = vdev->pdev;
434 irq = pci_irq_vector(pdev, vector);
435 if (WARN_ON_ONCE(irq == 0))
437 if (irq > 0 || !msix || !vdev->has_dyn_msix)
440 cmd = vfio_pci_memory_lock_and_enable(vdev);
441 map = pci_msix_alloc_irq_at(pdev, vector, NULL);
442 vfio_pci_memory_unlock_and_restore(vdev, cmd);
444 return map.index < 0 ? map.index : map.virq;
447 static int vfio_msi_set_vector_signal(struct vfio_pci_core_device *vdev,
448 unsigned int vector, int fd, bool msix)
450 struct pci_dev *pdev = vdev->pdev;
451 struct vfio_pci_irq_ctx *ctx;
452 struct eventfd_ctx *trigger;
453 int irq = -EINVAL, ret;
456 ctx = vfio_irq_ctx_get(vdev, vector);
459 irq_bypass_unregister_producer(&ctx->producer);
460 irq = pci_irq_vector(pdev, vector);
461 cmd = vfio_pci_memory_lock_and_enable(vdev);
462 free_irq(irq, ctx->trigger);
463 vfio_pci_memory_unlock_and_restore(vdev, cmd);
464 /* Interrupt stays allocated, will be freed at MSI-X disable. */
466 eventfd_ctx_put(ctx->trigger);
467 vfio_irq_ctx_free(vdev, ctx, vector);
473 if (irq == -EINVAL) {
474 /* Interrupt stays allocated, will be freed at MSI-X disable. */
475 irq = vfio_msi_alloc_irq(vdev, vector, msix);
480 ctx = vfio_irq_ctx_alloc(vdev, vector);
484 ctx->name = kasprintf(GFP_KERNEL_ACCOUNT, "vfio-msi%s[%d](%s)",
485 msix ? "x" : "", vector, pci_name(pdev));
491 trigger = eventfd_ctx_fdget(fd);
492 if (IS_ERR(trigger)) {
493 ret = PTR_ERR(trigger);
498 * If the vector was previously allocated, refresh the on-device
499 * message data before enabling in case it had been cleared or
500 * corrupted (e.g. due to backdoor resets) since writing.
502 cmd = vfio_pci_memory_lock_and_enable(vdev);
506 get_cached_msi_msg(irq, &msg);
507 pci_write_msi_msg(irq, &msg);
510 ret = request_irq(irq, vfio_msihandler, 0, ctx->name, trigger);
511 vfio_pci_memory_unlock_and_restore(vdev, cmd);
513 goto out_put_eventfd_ctx;
515 ctx->producer.token = trigger;
516 ctx->producer.irq = irq;
517 ret = irq_bypass_register_producer(&ctx->producer);
520 "irq bypass producer (token %p) registration fails: %d\n",
521 ctx->producer.token, ret);
523 ctx->producer.token = NULL;
525 ctx->trigger = trigger;
530 eventfd_ctx_put(trigger);
534 vfio_irq_ctx_free(vdev, ctx, vector);
538 static int vfio_msi_set_block(struct vfio_pci_core_device *vdev, unsigned start,
539 unsigned count, int32_t *fds, bool msix)
544 for (i = 0, j = start; i < count && !ret; i++, j++) {
545 int fd = fds ? fds[i] : -1;
546 ret = vfio_msi_set_vector_signal(vdev, j, fd, msix);
550 for (i = start; i < j; i++)
551 vfio_msi_set_vector_signal(vdev, i, -1, msix);
557 static void vfio_msi_disable(struct vfio_pci_core_device *vdev, bool msix)
559 struct pci_dev *pdev = vdev->pdev;
560 struct vfio_pci_irq_ctx *ctx;
564 xa_for_each(&vdev->ctx, i, ctx) {
565 vfio_virqfd_disable(&ctx->unmask);
566 vfio_virqfd_disable(&ctx->mask);
567 vfio_msi_set_vector_signal(vdev, i, -1, msix);
570 cmd = vfio_pci_memory_lock_and_enable(vdev);
571 pci_free_irq_vectors(pdev);
572 vfio_pci_memory_unlock_and_restore(vdev, cmd);
575 * Both disable paths above use pci_intx_for_msi() to clear DisINTx
576 * via their shutdown paths. Restore for NoINTx devices.
581 vdev->irq_type = VFIO_PCI_NUM_IRQS;
587 static int vfio_pci_set_intx_unmask(struct vfio_pci_core_device *vdev,
588 unsigned index, unsigned start,
589 unsigned count, uint32_t flags, void *data)
591 if (!is_intx(vdev) || start != 0 || count != 1)
594 if (flags & VFIO_IRQ_SET_DATA_NONE) {
595 __vfio_pci_intx_unmask(vdev);
596 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
597 uint8_t unmask = *(uint8_t *)data;
599 __vfio_pci_intx_unmask(vdev);
600 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
601 struct vfio_pci_irq_ctx *ctx = vfio_irq_ctx_get(vdev, 0);
602 int32_t fd = *(int32_t *)data;
604 if (WARN_ON_ONCE(!ctx))
607 return vfio_virqfd_enable((void *) vdev,
608 vfio_pci_intx_unmask_handler,
609 vfio_send_intx_eventfd, NULL,
612 vfio_virqfd_disable(&ctx->unmask);
618 static int vfio_pci_set_intx_mask(struct vfio_pci_core_device *vdev,
619 unsigned index, unsigned start,
620 unsigned count, uint32_t flags, void *data)
622 if (!is_intx(vdev) || start != 0 || count != 1)
625 if (flags & VFIO_IRQ_SET_DATA_NONE) {
626 __vfio_pci_intx_mask(vdev);
627 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
628 uint8_t mask = *(uint8_t *)data;
630 __vfio_pci_intx_mask(vdev);
631 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
632 return -ENOTTY; /* XXX implement me */
638 static int vfio_pci_set_intx_trigger(struct vfio_pci_core_device *vdev,
639 unsigned index, unsigned start,
640 unsigned count, uint32_t flags, void *data)
642 if (is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
643 vfio_intx_disable(vdev);
647 if (!(is_intx(vdev) || is_irq_none(vdev)) || start != 0 || count != 1)
650 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
651 struct eventfd_ctx *trigger = NULL;
652 int32_t fd = *(int32_t *)data;
656 trigger = eventfd_ctx_fdget(fd);
658 return PTR_ERR(trigger);
662 ret = vfio_intx_set_signal(vdev, trigger);
664 ret = vfio_intx_enable(vdev, trigger);
667 eventfd_ctx_put(trigger);
675 if (flags & VFIO_IRQ_SET_DATA_NONE) {
676 vfio_send_intx_eventfd(vdev, NULL);
677 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
678 uint8_t trigger = *(uint8_t *)data;
680 vfio_send_intx_eventfd(vdev, NULL);
685 static int vfio_pci_set_msi_trigger(struct vfio_pci_core_device *vdev,
686 unsigned index, unsigned start,
687 unsigned count, uint32_t flags, void *data)
689 struct vfio_pci_irq_ctx *ctx;
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))
723 for (i = start; i < start + count; i++) {
724 ctx = vfio_irq_ctx_get(vdev, i);
727 if (flags & VFIO_IRQ_SET_DATA_NONE) {
728 eventfd_signal(ctx->trigger);
729 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
730 uint8_t *bools = data;
731 if (bools[i - start])
732 eventfd_signal(ctx->trigger);
738 static int vfio_pci_set_ctx_trigger_single(struct eventfd_ctx **ctx,
739 unsigned int count, uint32_t flags,
742 /* DATA_NONE/DATA_BOOL enables loopback testing */
743 if (flags & VFIO_IRQ_SET_DATA_NONE) {
746 eventfd_signal(*ctx);
748 eventfd_ctx_put(*ctx);
753 } else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
759 trigger = *(uint8_t *)data;
761 eventfd_signal(*ctx);
764 } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
770 fd = *(int32_t *)data;
773 eventfd_ctx_put(*ctx);
775 } else if (fd >= 0) {
776 struct eventfd_ctx *efdctx;
778 efdctx = eventfd_ctx_fdget(fd);
780 return PTR_ERR(efdctx);
783 eventfd_ctx_put(*ctx);
793 static int vfio_pci_set_err_trigger(struct vfio_pci_core_device *vdev,
794 unsigned index, unsigned start,
795 unsigned count, uint32_t flags, void *data)
797 if (index != VFIO_PCI_ERR_IRQ_INDEX || start != 0 || count > 1)
800 return vfio_pci_set_ctx_trigger_single(&vdev->err_trigger,
804 static int vfio_pci_set_req_trigger(struct vfio_pci_core_device *vdev,
805 unsigned index, unsigned start,
806 unsigned count, uint32_t flags, void *data)
808 if (index != VFIO_PCI_REQ_IRQ_INDEX || start != 0 || count > 1)
811 return vfio_pci_set_ctx_trigger_single(&vdev->req_trigger,
815 int vfio_pci_set_irqs_ioctl(struct vfio_pci_core_device *vdev, uint32_t flags,
816 unsigned index, unsigned start, unsigned count,
819 int (*func)(struct vfio_pci_core_device *vdev, unsigned index,
820 unsigned start, unsigned count, uint32_t flags,
824 case VFIO_PCI_INTX_IRQ_INDEX:
825 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
826 case VFIO_IRQ_SET_ACTION_MASK:
827 func = vfio_pci_set_intx_mask;
829 case VFIO_IRQ_SET_ACTION_UNMASK:
830 func = vfio_pci_set_intx_unmask;
832 case VFIO_IRQ_SET_ACTION_TRIGGER:
833 func = vfio_pci_set_intx_trigger;
837 case VFIO_PCI_MSI_IRQ_INDEX:
838 case VFIO_PCI_MSIX_IRQ_INDEX:
839 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
840 case VFIO_IRQ_SET_ACTION_MASK:
841 case VFIO_IRQ_SET_ACTION_UNMASK:
842 /* XXX Need masking support exported */
844 case VFIO_IRQ_SET_ACTION_TRIGGER:
845 func = vfio_pci_set_msi_trigger;
849 case VFIO_PCI_ERR_IRQ_INDEX:
850 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
851 case VFIO_IRQ_SET_ACTION_TRIGGER:
852 if (pci_is_pcie(vdev->pdev))
853 func = vfio_pci_set_err_trigger;
857 case VFIO_PCI_REQ_IRQ_INDEX:
858 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
859 case VFIO_IRQ_SET_ACTION_TRIGGER:
860 func = vfio_pci_set_req_trigger;
869 return func(vdev, index, start, count, flags, data);