16 #include <linux/device.h>
19 #include <linux/pci.h>
21 #include <linux/poll.h>
22 #include <linux/vfio.h>
23 #include <linux/wait.h>
48 vfio_irqfd_cleanup_wq =
50 if (!vfio_irqfd_cleanup_wq)
90 if (*(virqfd->
pvirqfd) == virqfd) {
92 virqfd_deactivate(virqfd);
95 spin_unlock_irqrestore(&virqfd->
vdev->irqlock, flags);
101 static void virqfd_ptable_queue_proc(
struct file *
file,
104 struct virqfd *virqfd =
container_of(pt,
struct virqfd, pt);
134 struct virqfd *virqfd;
138 virqfd = kzalloc(
sizeof(*virqfd),
GFP_KERNEL);
174 spin_unlock_irq(&vdev->
irqlock);
180 spin_unlock_irq(&vdev->
irqlock);
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);
195 if (events & POLLIN) {
209 if (ctx && !IS_ERR(ctx))
212 if (file && !IS_ERR(file))
221 struct virqfd **pvirqfd)
228 virqfd_deactivate(*pvirqfd);
232 spin_unlock_irqrestore(&vdev->
irqlock, flags);
267 }
else if (!vdev->
ctx[0].masked) {
277 vdev->
ctx[0].masked =
true;
280 spin_unlock_irqrestore(&vdev->
irqlock, flags);
316 vdev->
ctx[0].masked = (ret > 0);
319 spin_unlock_irqrestore(&vdev->
irqlock, flags);
327 vfio_send_intx_eventfd(vdev,
NULL);
340 vdev->
ctx[0].masked =
true;
342 }
else if (!vdev->
ctx[0].masked &&
344 vdev->
ctx[0].masked =
true;
348 spin_unlock_irqrestore(&vdev->
irqlock, flags);
351 vfio_send_intx_eventfd(vdev,
NULL);
361 if (!vdev->
pdev->irq)
393 if (vdev->
ctx[0].trigger) {
405 if (!vdev->
ctx[0].name)
409 if (IS_ERR(trigger)) {
411 return PTR_ERR(trigger);
420 irqflags, vdev->
ctx[0].name, vdev);
435 spin_unlock_irqrestore(&vdev->
irqlock, flags);
442 vfio_intx_set_signal(vdev, -1);
443 virqfd_disable(vdev, &vdev->
ctx[0].unmask);
444 virqfd_disable(vdev, &vdev->
ctx[0].mask);
461 static int vfio_msi_enable(
struct vfio_pci_device *vdev,
int nvec,
bool msix)
476 vdev->
msix = kzalloc(nvec *
sizeof(
struct msix_entry),
483 for (i = 0; i < nvec; i++)
484 vdev->
msix[i].entry = i;
509 vdev->
msi_qmax = fls(nvec * 2 - 1) - 1;
516 int vector,
int fd,
bool msix)
520 char *
name = msix ?
"vfio-msix" :
"vfio-msi";
527 if (vdev->
ctx[vector].trigger) {
538 name, vector, pci_name(pdev));
539 if (!vdev->
ctx[vector].name)
543 if (IS_ERR(trigger)) {
545 return PTR_ERR(trigger);
549 vdev->
ctx[vector].name, trigger);
566 if (start + count > vdev->
num_ctx)
569 for (i = 0, j = start; i < count && !
ret; i++, j++) {
570 int fd = fds ? fds[
i] : -1;
571 ret = vfio_msi_set_vector_signal(vdev, j, fd, msix);
575 for (--j; j >=
start; j--)
576 vfio_msi_set_vector_signal(vdev, j, -1, msix);
587 vfio_msi_set_block(vdev, 0, vdev->
num_ctx,
NULL, msix);
589 for (i = 0; i < vdev->
num_ctx; i++) {
590 virqfd_disable(vdev, &vdev->
ctx[i].unmask);
591 virqfd_disable(vdev, &vdev->
ctx[i].mask);
609 unsigned index,
unsigned start,
610 unsigned count,
uint32_t flags,
void *data)
612 if (!
is_intx(vdev) || start != 0 || count != 1)
625 vfio_send_intx_eventfd,
NULL,
626 &vdev->
ctx[0].unmask, fd);
628 virqfd_disable(vdev, &vdev->
ctx[0].unmask);
635 unsigned index,
unsigned start,
636 unsigned count,
uint32_t flags,
void *data)
638 if (!
is_intx(vdev) || start != 0 || count != 1)
641 if (flags & VFIO_IRQ_SET_DATA_NONE) {
643 }
else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
647 }
else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
655 unsigned index,
unsigned start,
656 unsigned count,
uint32_t flags,
void *data)
658 if (
is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) {
659 vfio_intx_disable(vdev);
666 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
671 return vfio_intx_set_signal(vdev, fd);
673 ret = vfio_intx_enable(vdev);
677 ret = vfio_intx_set_signal(vdev, fd);
679 vfio_intx_disable(vdev);
687 if (flags & VFIO_IRQ_SET_DATA_NONE) {
688 vfio_send_intx_eventfd(vdev,
NULL);
689 }
else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
692 vfio_send_intx_eventfd(vdev,
NULL);
698 unsigned index,
unsigned start,
699 unsigned count,
uint32_t flags,
void *data)
705 vfio_msi_disable(vdev, msix);
712 if (flags & VFIO_IRQ_SET_DATA_EVENTFD) {
717 return vfio_msi_set_block(vdev, start, count,
720 ret = vfio_msi_enable(vdev, start + count, msix);
724 ret = vfio_msi_set_block(vdev, start, count, fds, msix);
726 vfio_msi_disable(vdev, msix);
734 for (i = start; i < start +
count; i++) {
735 if (!vdev->
ctx[i].trigger)
737 if (flags & VFIO_IRQ_SET_DATA_NONE) {
739 }
else if (flags & VFIO_IRQ_SET_DATA_BOOL) {
741 if (bools[i - start])
749 unsigned index,
unsigned start,
unsigned count,
760 func = vfio_pci_set_intx_mask;
763 func = vfio_pci_set_intx_unmask;
766 func = vfio_pci_set_intx_trigger;
772 switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) {
778 func = vfio_pci_set_msi_trigger;
787 return func(vdev, index, start, count, flags, data);