15 #include <linux/errno.h>
17 #include <linux/pci.h>
19 #include <linux/slab.h>
39 *assigned_dev,
int irq)
42 struct msix_entry *host_msix_entries;
48 if (irq == host_msix_entries[i].
vector) {
90 static irqreturn_t kvm_assigned_dev_thread_intx(
int irq,
void *dev_id)
98 spin_unlock_irq(&assigned_dev->
intx_lock);
101 kvm_assigned_dev_raise_guest_irq(assigned_dev,
107 #ifdef __KVM_HAVE_MSI
108 static irqreturn_t kvm_assigned_dev_thread_msi(
int irq,
void *dev_id)
112 kvm_assigned_dev_raise_guest_irq(assigned_dev,
119 #ifdef __KVM_HAVE_MSIX
120 static irqreturn_t kvm_assigned_dev_thread_msix(
int irq,
void *dev_id)
123 int index = find_index_from_host_irq(assigned_dev, irq);
128 kvm_assigned_dev_raise_guest_irq(assigned_dev, vector);
147 bool reassert =
false;
171 static void deassign_guest_irq(
struct kvm *
kvm,
188 static void deassign_host_irq(
struct kvm *kvm,
203 for (i = 0; i < assigned_dev->
entries_nr; i++)
206 for (i = 0; i < assigned_dev->
entries_nr; i++)
221 spin_unlock_irq(&assigned_dev->
intx_lock);
235 static int kvm_deassign_irq(
struct kvm *kvm,
239 unsigned long guest_irq_type, host_irq_type;
241 if (!irqchip_in_kernel(kvm))
251 deassign_host_irq(kvm, assigned_dev);
253 deassign_guest_irq(kvm, assigned_dev);
258 static void kvm_free_assigned_irq(
struct kvm *kvm,
264 static void kvm_free_assigned_device(
struct kvm *kvm,
268 kvm_free_assigned_irq(kvm, assigned_dev);
274 __func__, dev_name(&assigned_dev->
dev->dev));
298 kvm_free_assigned_device(kvm, assigned_dev);
302 static int assigned_device_enable_host_intx(
struct kvm *kvm,
317 irq_handler = kvm_assigned_dev_intx;
324 kvm_assigned_dev_thread_intx, flags,
336 #ifdef __KVM_HAVE_MSI
337 static irqreturn_t kvm_assigned_dev_msi(
int irq,
void *dev_id)
342 static int assigned_device_enable_host_msi(
struct kvm *kvm,
347 if (!dev->
dev->msi_enabled) {
348 r = pci_enable_msi(dev->
dev);
355 kvm_assigned_dev_thread_msi, 0,
365 #ifdef __KVM_HAVE_MSIX
366 static irqreturn_t kvm_assigned_dev_msix(
int irq,
void *dev_id)
371 static int assigned_device_enable_host_msix(
struct kvm *kvm,
387 kvm_assigned_dev_msix,
388 kvm_assigned_dev_thread_msix,
396 for (i -= 1; i >= 0; i--)
404 static int assigned_device_enable_guest_intx(
struct kvm *kvm,
413 #ifdef __KVM_HAVE_MSI
414 static int assigned_device_enable_guest_msi(
struct kvm *kvm,
424 #ifdef __KVM_HAVE_MSIX
425 static int assigned_device_enable_guest_msix(
struct kvm *kvm,
435 static int assign_host_irq(
struct kvm *kvm,
447 switch (host_irq_type) {
449 r = assigned_device_enable_host_intx(kvm, dev);
451 #ifdef __KVM_HAVE_MSI
453 r = assigned_device_enable_host_msi(kvm, dev);
456 #ifdef __KVM_HAVE_MSIX
458 r = assigned_device_enable_host_msix(kvm, dev);
472 static int assign_guest_irq(
struct kvm *kvm,
475 unsigned long guest_irq_type)
489 switch (guest_irq_type) {
491 r = assigned_device_enable_guest_intx(kvm, dev, irq);
493 #ifdef __KVM_HAVE_MSI
495 r = assigned_device_enable_guest_msi(kvm, dev, irq);
498 #ifdef __KVM_HAVE_MSIX
500 r = assigned_device_enable_guest_msix(kvm, dev, irq);
518 static int kvm_vm_ioctl_assign_irq(
struct kvm *kvm,
523 unsigned long host_irq_type, guest_irq_type;
525 if (!irqchip_in_kernel(kvm))
530 match = kvm_find_assigned_dev(&kvm->
arch.assigned_dev_head,
540 if (hweight_long(host_irq_type) > 1)
542 if (hweight_long(guest_irq_type) > 1)
544 if (host_irq_type == 0 && guest_irq_type == 0)
549 r = assign_host_irq(kvm, match, host_irq_type);
554 r = assign_guest_irq(kvm, match, assigned_irq, guest_irq_type);
560 static int kvm_vm_ioctl_deassign_dev_irq(
struct kvm *kvm,
566 unsigned long irq_type;
570 match = kvm_find_assigned_dev(&kvm->
arch.assigned_dev_head,
575 irq_type = assigned_irq->
flags & (KVM_DEV_IRQ_HOST_MASK |
577 r = kvm_deassign_irq(kvm, match, irq_type);
593 static int probe_sysfs_permissions(
struct pci_dev *dev)
597 bool bar_found =
false;
600 char *kpath, *syspath;
643 static int kvm_vm_ioctl_assign_device(
struct kvm *kvm,
654 idx = srcu_read_lock(&kvm->
srcu);
656 match = kvm_find_assigned_dev(&kvm->
arch.assigned_dev_head,
673 assigned_dev->
devfn);
686 r = probe_sysfs_permissions(dev);
707 __func__, dev_name(&dev->
dev));
722 match->
ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
724 list_add(&match->
list, &kvm->
arch.assigned_dev_head);
726 if (!kvm->
arch.iommu_domain) {
736 srcu_read_unlock(&kvm->
srcu,
idx);
742 __func__, dev_name(&dev->
dev));
751 srcu_read_unlock(&kvm->
srcu,
idx);
756 static int kvm_vm_ioctl_deassign_device(
struct kvm *kvm,
764 match = kvm_find_assigned_dev(&kvm->
arch.assigned_dev_head,
768 "so cannot be deassigned\n", __func__);
775 kvm_free_assigned_device(kvm, match);
783 #ifdef __KVM_HAVE_MSIX
784 static int kvm_vm_ioctl_set_msix_nr(
struct kvm *kvm,
792 adev = kvm_find_assigned_dev(&kvm->
arch.assigned_dev_head,
815 kzalloc(
sizeof(
struct msix_entry) * entry_nr->
entry_nr,
829 static int kvm_vm_ioctl_set_msix_entry(
struct kvm *kvm,
837 adev = kvm_find_assigned_dev(&kvm->
arch.assigned_dev_head,
865 static int kvm_vm_ioctl_set_pci_irq_mask(
struct kvm *kvm,
873 match = kvm_find_assigned_dev(&kvm->
arch.assigned_dev_head,
927 r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
942 r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
953 r = kvm_vm_ioctl_deassign_dev_irq(kvm, &assigned_irq);
964 r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
969 #ifdef KVM_CAP_IRQ_ROUTING
971 struct kvm_irq_routing routing;
972 struct kvm_irq_routing __user *urouting;
973 struct kvm_irq_routing_entry *
entries;
979 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
984 entries =
vmalloc(routing.nr *
sizeof(*entries));
990 routing.nr *
sizeof(*entries)))
991 goto out_free_irq_routing;
994 out_free_irq_routing:
999 #ifdef __KVM_HAVE_MSIX
1005 r = kvm_vm_ioctl_set_msix_nr(kvm, &entry_nr);
1015 r = kvm_vm_ioctl_set_msix_entry(kvm, &entry);
1027 r = kvm_vm_ioctl_set_pci_irq_mask(kvm, &assigned_dev);