2 * Kernel-based Virtual Machine - device assignment support
4 * Copyright (C) 2010 Red Hat, Inc. and/or its affiliates.
6 * This work is licensed under the terms of the GNU GPL, version 2. See
7 * the COPYING file in the top-level directory.
11 #include <linux/kvm_host.h>
12 #include <linux/kvm.h>
13 #include <linux/uaccess.h>
14 #include <linux/vmalloc.h>
15 #include <linux/errno.h>
16 #include <linux/spinlock.h>
17 #include <linux/pci.h>
18 #include <linux/interrupt.h>
19 #include <linux/slab.h>
20 #include <linux/namei.h>
24 static struct kvm_assigned_dev_kernel
*kvm_find_assigned_dev(struct list_head
*head
,
27 struct list_head
*ptr
;
28 struct kvm_assigned_dev_kernel
*match
;
30 list_for_each(ptr
, head
) {
31 match
= list_entry(ptr
, struct kvm_assigned_dev_kernel
, list
);
32 if (match
->assigned_dev_id
== assigned_dev_id
)
38 static int find_index_from_host_irq(struct kvm_assigned_dev_kernel
39 *assigned_dev
, int irq
)
42 struct msix_entry
*host_msix_entries
;
44 host_msix_entries
= assigned_dev
->host_msix_entries
;
47 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
48 if (irq
== host_msix_entries
[i
].vector
) {
53 printk(KERN_WARNING
"Fail to find correlated MSI-X entry!\n");
58 static irqreturn_t
kvm_assigned_dev_intx(int irq
, void *dev_id
)
60 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
63 spin_lock(&assigned_dev
->intx_lock
);
64 if (pci_check_and_mask_intx(assigned_dev
->dev
)) {
65 assigned_dev
->host_irq_disabled
= true;
66 ret
= IRQ_WAKE_THREAD
;
69 spin_unlock(&assigned_dev
->intx_lock
);
75 kvm_assigned_dev_raise_guest_irq(struct kvm_assigned_dev_kernel
*assigned_dev
,
78 if (unlikely(assigned_dev
->irq_requested_type
&
79 KVM_DEV_IRQ_GUEST_INTX
)) {
80 spin_lock(&assigned_dev
->intx_mask_lock
);
81 if (!(assigned_dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
))
82 kvm_set_irq(assigned_dev
->kvm
,
83 assigned_dev
->irq_source_id
, vector
, 1);
84 spin_unlock(&assigned_dev
->intx_mask_lock
);
86 kvm_set_irq(assigned_dev
->kvm
, assigned_dev
->irq_source_id
,
90 static irqreturn_t
kvm_assigned_dev_thread_intx(int irq
, void *dev_id
)
92 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
94 if (!(assigned_dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
)) {
95 spin_lock_irq(&assigned_dev
->intx_lock
);
96 disable_irq_nosync(irq
);
97 assigned_dev
->host_irq_disabled
= true;
98 spin_unlock_irq(&assigned_dev
->intx_lock
);
101 kvm_assigned_dev_raise_guest_irq(assigned_dev
,
102 assigned_dev
->guest_irq
);
107 #ifdef __KVM_HAVE_MSI
108 static irqreturn_t
kvm_assigned_dev_thread_msi(int irq
, void *dev_id
)
110 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
112 kvm_assigned_dev_raise_guest_irq(assigned_dev
,
113 assigned_dev
->guest_irq
);
119 #ifdef __KVM_HAVE_MSIX
120 static irqreturn_t
kvm_assigned_dev_thread_msix(int irq
, void *dev_id
)
122 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
123 int index
= find_index_from_host_irq(assigned_dev
, irq
);
127 vector
= assigned_dev
->guest_msix_entries
[index
].vector
;
128 kvm_assigned_dev_raise_guest_irq(assigned_dev
, vector
);
135 /* Ack the irq line for an assigned device */
136 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier
*kian
)
138 struct kvm_assigned_dev_kernel
*dev
=
139 container_of(kian
, struct kvm_assigned_dev_kernel
,
142 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0);
144 spin_lock(&dev
->intx_mask_lock
);
146 if (!(dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
)) {
147 bool reassert
= false;
149 spin_lock_irq(&dev
->intx_lock
);
151 * The guest IRQ may be shared so this ack can come from an
152 * IRQ for another guest device.
154 if (dev
->host_irq_disabled
) {
155 if (!(dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
))
156 enable_irq(dev
->host_irq
);
157 else if (!pci_check_and_unmask_intx(dev
->dev
))
159 dev
->host_irq_disabled
= reassert
;
161 spin_unlock_irq(&dev
->intx_lock
);
164 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
,
168 spin_unlock(&dev
->intx_mask_lock
);
171 static void deassign_guest_irq(struct kvm
*kvm
,
172 struct kvm_assigned_dev_kernel
*assigned_dev
)
174 if (assigned_dev
->ack_notifier
.gsi
!= -1)
175 kvm_unregister_irq_ack_notifier(kvm
,
176 &assigned_dev
->ack_notifier
);
178 kvm_set_irq(assigned_dev
->kvm
, assigned_dev
->irq_source_id
,
179 assigned_dev
->guest_irq
, 0);
181 if (assigned_dev
->irq_source_id
!= -1)
182 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
183 assigned_dev
->irq_source_id
= -1;
184 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_GUEST_MASK
);
187 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
188 static void deassign_host_irq(struct kvm
*kvm
,
189 struct kvm_assigned_dev_kernel
*assigned_dev
)
192 * We disable irq here to prevent further events.
194 * Notice this maybe result in nested disable if the interrupt type is
195 * INTx, but it's OK for we are going to free it.
197 * If this function is a part of VM destroy, please ensure that till
198 * now, the kvm state is still legal for probably we also have to wait
199 * on a currently running IRQ handler.
201 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSIX
) {
203 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
204 disable_irq(assigned_dev
->host_msix_entries
[i
].vector
);
206 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
207 free_irq(assigned_dev
->host_msix_entries
[i
].vector
,
210 assigned_dev
->entries_nr
= 0;
211 kfree(assigned_dev
->host_msix_entries
);
212 kfree(assigned_dev
->guest_msix_entries
);
213 pci_disable_msix(assigned_dev
->dev
);
215 /* Deal with MSI and INTx */
216 if ((assigned_dev
->irq_requested_type
&
217 KVM_DEV_IRQ_HOST_INTX
) &&
218 (assigned_dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
)) {
219 spin_lock_irq(&assigned_dev
->intx_lock
);
220 pci_intx(assigned_dev
->dev
, false);
221 spin_unlock_irq(&assigned_dev
->intx_lock
);
222 synchronize_irq(assigned_dev
->host_irq
);
224 disable_irq(assigned_dev
->host_irq
);
226 free_irq(assigned_dev
->host_irq
, assigned_dev
);
228 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSI
)
229 pci_disable_msi(assigned_dev
->dev
);
232 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_HOST_MASK
);
235 static int kvm_deassign_irq(struct kvm
*kvm
,
236 struct kvm_assigned_dev_kernel
*assigned_dev
,
237 unsigned long irq_requested_type
)
239 unsigned long guest_irq_type
, host_irq_type
;
241 if (!irqchip_in_kernel(kvm
))
243 /* no irq assignment to deassign */
244 if (!assigned_dev
->irq_requested_type
)
247 host_irq_type
= irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
;
248 guest_irq_type
= irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
;
251 deassign_host_irq(kvm
, assigned_dev
);
253 deassign_guest_irq(kvm
, assigned_dev
);
258 static void kvm_free_assigned_irq(struct kvm
*kvm
,
259 struct kvm_assigned_dev_kernel
*assigned_dev
)
261 kvm_deassign_irq(kvm
, assigned_dev
, assigned_dev
->irq_requested_type
);
264 static void kvm_free_assigned_device(struct kvm
*kvm
,
265 struct kvm_assigned_dev_kernel
268 kvm_free_assigned_irq(kvm
, assigned_dev
);
270 pci_reset_function(assigned_dev
->dev
);
271 if (pci_load_and_free_saved_state(assigned_dev
->dev
,
272 &assigned_dev
->pci_saved_state
))
273 printk(KERN_INFO
"%s: Couldn't reload %s saved state\n",
274 __func__
, dev_name(&assigned_dev
->dev
->dev
));
276 pci_restore_state(assigned_dev
->dev
);
278 assigned_dev
->dev
->dev_flags
&= ~PCI_DEV_FLAGS_ASSIGNED
;
280 pci_release_regions(assigned_dev
->dev
);
281 pci_disable_device(assigned_dev
->dev
);
282 pci_dev_put(assigned_dev
->dev
);
284 list_del(&assigned_dev
->list
);
288 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
290 struct list_head
*ptr
, *ptr2
;
291 struct kvm_assigned_dev_kernel
*assigned_dev
;
293 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
294 assigned_dev
= list_entry(ptr
,
295 struct kvm_assigned_dev_kernel
,
298 kvm_free_assigned_device(kvm
, assigned_dev
);
302 static int assigned_device_enable_host_intx(struct kvm
*kvm
,
303 struct kvm_assigned_dev_kernel
*dev
)
305 irq_handler_t irq_handler
;
308 dev
->host_irq
= dev
->dev
->irq
;
311 * We can only share the IRQ line with other host devices if we are
312 * able to disable the IRQ source at device-level - independently of
313 * the guest driver. Otherwise host devices may suffer from unbounded
314 * IRQ latencies when the guest keeps the line asserted.
316 if (dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
) {
317 irq_handler
= kvm_assigned_dev_intx
;
321 flags
= IRQF_ONESHOT
;
323 if (request_threaded_irq(dev
->host_irq
, irq_handler
,
324 kvm_assigned_dev_thread_intx
, flags
,
328 if (dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
) {
329 spin_lock_irq(&dev
->intx_lock
);
330 pci_intx(dev
->dev
, true);
331 spin_unlock_irq(&dev
->intx_lock
);
336 #ifdef __KVM_HAVE_MSI
337 static irqreturn_t
kvm_assigned_dev_msi(int irq
, void *dev_id
)
339 return IRQ_WAKE_THREAD
;
342 static int assigned_device_enable_host_msi(struct kvm
*kvm
,
343 struct kvm_assigned_dev_kernel
*dev
)
347 if (!dev
->dev
->msi_enabled
) {
348 r
= pci_enable_msi(dev
->dev
);
353 dev
->host_irq
= dev
->dev
->irq
;
354 if (request_threaded_irq(dev
->host_irq
, kvm_assigned_dev_msi
,
355 kvm_assigned_dev_thread_msi
, 0,
356 dev
->irq_name
, dev
)) {
357 pci_disable_msi(dev
->dev
);
365 #ifdef __KVM_HAVE_MSIX
366 static irqreturn_t
kvm_assigned_dev_msix(int irq
, void *dev_id
)
368 return IRQ_WAKE_THREAD
;
371 static int assigned_device_enable_host_msix(struct kvm
*kvm
,
372 struct kvm_assigned_dev_kernel
*dev
)
376 /* host_msix_entries and guest_msix_entries should have been
378 if (dev
->entries_nr
== 0)
381 r
= pci_enable_msix(dev
->dev
, dev
->host_msix_entries
, dev
->entries_nr
);
385 for (i
= 0; i
< dev
->entries_nr
; i
++) {
386 r
= request_threaded_irq(dev
->host_msix_entries
[i
].vector
,
387 kvm_assigned_dev_msix
,
388 kvm_assigned_dev_thread_msix
,
389 0, dev
->irq_name
, dev
);
396 for (i
-= 1; i
>= 0; i
--)
397 free_irq(dev
->host_msix_entries
[i
].vector
, dev
);
398 pci_disable_msix(dev
->dev
);
404 static int assigned_device_enable_guest_intx(struct kvm
*kvm
,
405 struct kvm_assigned_dev_kernel
*dev
,
406 struct kvm_assigned_irq
*irq
)
408 dev
->guest_irq
= irq
->guest_irq
;
409 dev
->ack_notifier
.gsi
= irq
->guest_irq
;
413 #ifdef __KVM_HAVE_MSI
414 static int assigned_device_enable_guest_msi(struct kvm
*kvm
,
415 struct kvm_assigned_dev_kernel
*dev
,
416 struct kvm_assigned_irq
*irq
)
418 dev
->guest_irq
= irq
->guest_irq
;
419 dev
->ack_notifier
.gsi
= -1;
424 #ifdef __KVM_HAVE_MSIX
425 static int assigned_device_enable_guest_msix(struct kvm
*kvm
,
426 struct kvm_assigned_dev_kernel
*dev
,
427 struct kvm_assigned_irq
*irq
)
429 dev
->guest_irq
= irq
->guest_irq
;
430 dev
->ack_notifier
.gsi
= -1;
435 static int assign_host_irq(struct kvm
*kvm
,
436 struct kvm_assigned_dev_kernel
*dev
,
441 if (dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
)
444 snprintf(dev
->irq_name
, sizeof(dev
->irq_name
), "kvm:%s",
447 switch (host_irq_type
) {
448 case KVM_DEV_IRQ_HOST_INTX
:
449 r
= assigned_device_enable_host_intx(kvm
, dev
);
451 #ifdef __KVM_HAVE_MSI
452 case KVM_DEV_IRQ_HOST_MSI
:
453 r
= assigned_device_enable_host_msi(kvm
, dev
);
456 #ifdef __KVM_HAVE_MSIX
457 case KVM_DEV_IRQ_HOST_MSIX
:
458 r
= assigned_device_enable_host_msix(kvm
, dev
);
464 dev
->host_irq_disabled
= false;
467 dev
->irq_requested_type
|= host_irq_type
;
472 static int assign_guest_irq(struct kvm
*kvm
,
473 struct kvm_assigned_dev_kernel
*dev
,
474 struct kvm_assigned_irq
*irq
,
475 unsigned long guest_irq_type
)
480 if (dev
->irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
)
483 id
= kvm_request_irq_source_id(kvm
);
487 dev
->irq_source_id
= id
;
489 switch (guest_irq_type
) {
490 case KVM_DEV_IRQ_GUEST_INTX
:
491 r
= assigned_device_enable_guest_intx(kvm
, dev
, irq
);
493 #ifdef __KVM_HAVE_MSI
494 case KVM_DEV_IRQ_GUEST_MSI
:
495 r
= assigned_device_enable_guest_msi(kvm
, dev
, irq
);
498 #ifdef __KVM_HAVE_MSIX
499 case KVM_DEV_IRQ_GUEST_MSIX
:
500 r
= assigned_device_enable_guest_msix(kvm
, dev
, irq
);
508 dev
->irq_requested_type
|= guest_irq_type
;
509 if (dev
->ack_notifier
.gsi
!= -1)
510 kvm_register_irq_ack_notifier(kvm
, &dev
->ack_notifier
);
512 kvm_free_irq_source_id(kvm
, dev
->irq_source_id
);
517 /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
518 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
519 struct kvm_assigned_irq
*assigned_irq
)
522 struct kvm_assigned_dev_kernel
*match
;
523 unsigned long host_irq_type
, guest_irq_type
;
525 if (!irqchip_in_kernel(kvm
))
528 mutex_lock(&kvm
->lock
);
530 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
531 assigned_irq
->assigned_dev_id
);
535 host_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_HOST_MASK
);
536 guest_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_GUEST_MASK
);
539 /* can only assign one type at a time */
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
);
556 mutex_unlock(&kvm
->lock
);
560 static int kvm_vm_ioctl_deassign_dev_irq(struct kvm
*kvm
,
561 struct kvm_assigned_irq
565 struct kvm_assigned_dev_kernel
*match
;
566 unsigned long irq_type
;
568 mutex_lock(&kvm
->lock
);
570 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
571 assigned_irq
->assigned_dev_id
);
575 irq_type
= assigned_irq
->flags
& (KVM_DEV_IRQ_HOST_MASK
|
576 KVM_DEV_IRQ_GUEST_MASK
);
577 r
= kvm_deassign_irq(kvm
, match
, irq_type
);
579 mutex_unlock(&kvm
->lock
);
584 * We want to test whether the caller has been granted permissions to
585 * use this device. To be able to configure and control the device,
586 * the user needs access to PCI configuration space and BAR resources.
587 * These are accessed through PCI sysfs. PCI config space is often
588 * passed to the process calling this ioctl via file descriptor, so we
589 * can't rely on access to that file. We can check for permissions
590 * on each of the BAR resource files, which is a pretty clear
591 * indicator that the user has been granted access to the device.
593 static int probe_sysfs_permissions(struct pci_dev
*dev
)
597 bool bar_found
= false;
599 for (i
= PCI_STD_RESOURCES
; i
<= PCI_STD_RESOURCE_END
; i
++) {
600 char *kpath
, *syspath
;
605 if (!pci_resource_len(dev
, i
))
608 kpath
= kobject_get_path(&dev
->dev
.kobj
, GFP_KERNEL
);
612 /* Per sysfs-rules, sysfs is always at /sys */
613 syspath
= kasprintf(GFP_KERNEL
, "/sys%s/resource%d", kpath
, i
);
618 r
= kern_path(syspath
, LOOKUP_FOLLOW
, &path
);
623 inode
= path
.dentry
->d_inode
;
625 r
= inode_permission(inode
, MAY_READ
| MAY_WRITE
| MAY_ACCESS
);
633 /* If no resources, probably something special */
639 return -EINVAL
; /* No way to control the device without sysfs */
643 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
644 struct kvm_assigned_pci_dev
*assigned_dev
)
647 struct kvm_assigned_dev_kernel
*match
;
650 if (!(assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
))
653 mutex_lock(&kvm
->lock
);
654 idx
= srcu_read_lock(&kvm
->srcu
);
656 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
657 assigned_dev
->assigned_dev_id
);
659 /* device already assigned */
664 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
666 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
671 dev
= pci_get_domain_bus_and_slot(assigned_dev
->segnr
,
673 assigned_dev
->devfn
);
675 printk(KERN_INFO
"%s: host device not found\n", __func__
);
680 /* Don't allow bridges to be assigned */
681 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
) {
686 r
= probe_sysfs_permissions(dev
);
690 if (pci_enable_device(dev
)) {
691 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
695 r
= pci_request_regions(dev
, "kvm_assigned_device");
697 printk(KERN_INFO
"%s: Could not get access to device regions\n",
702 pci_reset_function(dev
);
704 match
->pci_saved_state
= pci_store_saved_state(dev
);
705 if (!match
->pci_saved_state
)
706 printk(KERN_DEBUG
"%s: Couldn't store %s saved state\n",
707 __func__
, dev_name(&dev
->dev
));
709 if (!pci_intx_mask_supported(dev
))
710 assigned_dev
->flags
&= ~KVM_DEV_ASSIGN_PCI_2_3
;
712 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
713 match
->host_segnr
= assigned_dev
->segnr
;
714 match
->host_busnr
= assigned_dev
->busnr
;
715 match
->host_devfn
= assigned_dev
->devfn
;
716 match
->flags
= assigned_dev
->flags
;
718 spin_lock_init(&match
->intx_lock
);
719 spin_lock_init(&match
->intx_mask_lock
);
720 match
->irq_source_id
= -1;
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
) {
727 r
= kvm_iommu_map_guest(kvm
);
731 r
= kvm_assign_device(kvm
, match
);
736 srcu_read_unlock(&kvm
->srcu
, idx
);
737 mutex_unlock(&kvm
->lock
);
740 if (pci_load_and_free_saved_state(dev
, &match
->pci_saved_state
))
741 printk(KERN_INFO
"%s: Couldn't reload %s saved state\n",
742 __func__
, dev_name(&dev
->dev
));
743 list_del(&match
->list
);
744 pci_release_regions(dev
);
746 pci_disable_device(dev
);
751 srcu_read_unlock(&kvm
->srcu
, idx
);
752 mutex_unlock(&kvm
->lock
);
756 static int kvm_vm_ioctl_deassign_device(struct kvm
*kvm
,
757 struct kvm_assigned_pci_dev
*assigned_dev
)
760 struct kvm_assigned_dev_kernel
*match
;
762 mutex_lock(&kvm
->lock
);
764 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
765 assigned_dev
->assigned_dev_id
);
767 printk(KERN_INFO
"%s: device hasn't been assigned before, "
768 "so cannot be deassigned\n", __func__
);
773 kvm_deassign_device(kvm
, match
);
775 kvm_free_assigned_device(kvm
, match
);
778 mutex_unlock(&kvm
->lock
);
783 #ifdef __KVM_HAVE_MSIX
784 static int kvm_vm_ioctl_set_msix_nr(struct kvm
*kvm
,
785 struct kvm_assigned_msix_nr
*entry_nr
)
788 struct kvm_assigned_dev_kernel
*adev
;
790 mutex_lock(&kvm
->lock
);
792 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
793 entry_nr
->assigned_dev_id
);
799 if (adev
->entries_nr
== 0) {
800 adev
->entries_nr
= entry_nr
->entry_nr
;
801 if (adev
->entries_nr
== 0 ||
802 adev
->entries_nr
> KVM_MAX_MSIX_PER_DEV
) {
807 adev
->host_msix_entries
= kzalloc(sizeof(struct msix_entry
) *
810 if (!adev
->host_msix_entries
) {
814 adev
->guest_msix_entries
=
815 kzalloc(sizeof(struct msix_entry
) * entry_nr
->entry_nr
,
817 if (!adev
->guest_msix_entries
) {
818 kfree(adev
->host_msix_entries
);
822 } else /* Not allowed set MSI-X number twice */
825 mutex_unlock(&kvm
->lock
);
829 static int kvm_vm_ioctl_set_msix_entry(struct kvm
*kvm
,
830 struct kvm_assigned_msix_entry
*entry
)
833 struct kvm_assigned_dev_kernel
*adev
;
835 mutex_lock(&kvm
->lock
);
837 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
838 entry
->assigned_dev_id
);
845 for (i
= 0; i
< adev
->entries_nr
; i
++)
846 if (adev
->guest_msix_entries
[i
].vector
== 0 ||
847 adev
->guest_msix_entries
[i
].entry
== entry
->entry
) {
848 adev
->guest_msix_entries
[i
].entry
= entry
->entry
;
849 adev
->guest_msix_entries
[i
].vector
= entry
->gsi
;
850 adev
->host_msix_entries
[i
].entry
= entry
->entry
;
853 if (i
== adev
->entries_nr
) {
859 mutex_unlock(&kvm
->lock
);
865 static int kvm_vm_ioctl_set_pci_irq_mask(struct kvm
*kvm
,
866 struct kvm_assigned_pci_dev
*assigned_dev
)
869 struct kvm_assigned_dev_kernel
*match
;
871 mutex_lock(&kvm
->lock
);
873 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
874 assigned_dev
->assigned_dev_id
);
880 spin_lock(&match
->intx_mask_lock
);
882 match
->flags
&= ~KVM_DEV_ASSIGN_MASK_INTX
;
883 match
->flags
|= assigned_dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
;
885 if (match
->irq_requested_type
& KVM_DEV_IRQ_GUEST_INTX
) {
886 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
) {
887 kvm_set_irq(match
->kvm
, match
->irq_source_id
,
888 match
->guest_irq
, 0);
890 * Masking at hardware-level is performed on demand,
891 * i.e. when an IRQ actually arrives at the host.
893 } else if (!(assigned_dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
)) {
895 * Unmask the IRQ line if required. Unmasking at
896 * device level will be performed by user space.
898 spin_lock_irq(&match
->intx_lock
);
899 if (match
->host_irq_disabled
) {
900 enable_irq(match
->host_irq
);
901 match
->host_irq_disabled
= false;
903 spin_unlock_irq(&match
->intx_lock
);
907 spin_unlock(&match
->intx_mask_lock
);
910 mutex_unlock(&kvm
->lock
);
914 long kvm_vm_ioctl_assigned_device(struct kvm
*kvm
, unsigned ioctl
,
917 void __user
*argp
= (void __user
*)arg
;
921 case KVM_ASSIGN_PCI_DEVICE
: {
922 struct kvm_assigned_pci_dev assigned_dev
;
925 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
927 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
932 case KVM_ASSIGN_IRQ
: {
936 case KVM_ASSIGN_DEV_IRQ
: {
937 struct kvm_assigned_irq assigned_irq
;
940 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
942 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
947 case KVM_DEASSIGN_DEV_IRQ
: {
948 struct kvm_assigned_irq assigned_irq
;
951 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
953 r
= kvm_vm_ioctl_deassign_dev_irq(kvm
, &assigned_irq
);
958 case KVM_DEASSIGN_PCI_DEVICE
: {
959 struct kvm_assigned_pci_dev assigned_dev
;
962 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
964 r
= kvm_vm_ioctl_deassign_device(kvm
, &assigned_dev
);
969 #ifdef KVM_CAP_IRQ_ROUTING
970 case KVM_SET_GSI_ROUTING
: {
971 struct kvm_irq_routing routing
;
972 struct kvm_irq_routing __user
*urouting
;
973 struct kvm_irq_routing_entry
*entries
;
976 if (copy_from_user(&routing
, argp
, sizeof(routing
)))
979 if (routing
.nr
>= KVM_MAX_IRQ_ROUTES
)
984 entries
= vmalloc(routing
.nr
* sizeof(*entries
));
989 if (copy_from_user(entries
, urouting
->entries
,
990 routing
.nr
* sizeof(*entries
)))
991 goto out_free_irq_routing
;
992 r
= kvm_set_irq_routing(kvm
, entries
, routing
.nr
,
994 out_free_irq_routing
:
998 #endif /* KVM_CAP_IRQ_ROUTING */
999 #ifdef __KVM_HAVE_MSIX
1000 case KVM_ASSIGN_SET_MSIX_NR
: {
1001 struct kvm_assigned_msix_nr entry_nr
;
1003 if (copy_from_user(&entry_nr
, argp
, sizeof entry_nr
))
1005 r
= kvm_vm_ioctl_set_msix_nr(kvm
, &entry_nr
);
1010 case KVM_ASSIGN_SET_MSIX_ENTRY
: {
1011 struct kvm_assigned_msix_entry entry
;
1013 if (copy_from_user(&entry
, argp
, sizeof entry
))
1015 r
= kvm_vm_ioctl_set_msix_entry(kvm
, &entry
);
1021 case KVM_ASSIGN_SET_INTX_MASK
: {
1022 struct kvm_assigned_pci_dev assigned_dev
;
1025 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
1027 r
= kvm_vm_ioctl_set_pci_irq_mask(kvm
, &assigned_dev
);