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_msi(int irq
, void *dev_id
)
110 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
111 int ret
= kvm_set_irq_inatomic(assigned_dev
->kvm
,
112 assigned_dev
->irq_source_id
,
113 assigned_dev
->guest_irq
, 1);
114 return unlikely(ret
== -EWOULDBLOCK
) ? IRQ_WAKE_THREAD
: IRQ_HANDLED
;
117 static irqreturn_t
kvm_assigned_dev_thread_msi(int irq
, void *dev_id
)
119 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
121 kvm_assigned_dev_raise_guest_irq(assigned_dev
,
122 assigned_dev
->guest_irq
);
128 #ifdef __KVM_HAVE_MSIX
129 static irqreturn_t
kvm_assigned_dev_msix(int irq
, void *dev_id
)
131 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
132 int index
= find_index_from_host_irq(assigned_dev
, irq
);
137 vector
= assigned_dev
->guest_msix_entries
[index
].vector
;
138 ret
= kvm_set_irq_inatomic(assigned_dev
->kvm
,
139 assigned_dev
->irq_source_id
,
143 return unlikely(ret
== -EWOULDBLOCK
) ? IRQ_WAKE_THREAD
: IRQ_HANDLED
;
146 static irqreturn_t
kvm_assigned_dev_thread_msix(int irq
, void *dev_id
)
148 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
149 int index
= find_index_from_host_irq(assigned_dev
, irq
);
153 vector
= assigned_dev
->guest_msix_entries
[index
].vector
;
154 kvm_assigned_dev_raise_guest_irq(assigned_dev
, vector
);
161 /* Ack the irq line for an assigned device */
162 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier
*kian
)
164 struct kvm_assigned_dev_kernel
*dev
=
165 container_of(kian
, struct kvm_assigned_dev_kernel
,
168 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0);
170 spin_lock(&dev
->intx_mask_lock
);
172 if (!(dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
)) {
173 bool reassert
= false;
175 spin_lock_irq(&dev
->intx_lock
);
177 * The guest IRQ may be shared so this ack can come from an
178 * IRQ for another guest device.
180 if (dev
->host_irq_disabled
) {
181 if (!(dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
))
182 enable_irq(dev
->host_irq
);
183 else if (!pci_check_and_unmask_intx(dev
->dev
))
185 dev
->host_irq_disabled
= reassert
;
187 spin_unlock_irq(&dev
->intx_lock
);
190 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
,
194 spin_unlock(&dev
->intx_mask_lock
);
197 static void deassign_guest_irq(struct kvm
*kvm
,
198 struct kvm_assigned_dev_kernel
*assigned_dev
)
200 if (assigned_dev
->ack_notifier
.gsi
!= -1)
201 kvm_unregister_irq_ack_notifier(kvm
,
202 &assigned_dev
->ack_notifier
);
204 kvm_set_irq(assigned_dev
->kvm
, assigned_dev
->irq_source_id
,
205 assigned_dev
->guest_irq
, 0);
207 if (assigned_dev
->irq_source_id
!= -1)
208 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
209 assigned_dev
->irq_source_id
= -1;
210 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_GUEST_MASK
);
213 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
214 static void deassign_host_irq(struct kvm
*kvm
,
215 struct kvm_assigned_dev_kernel
*assigned_dev
)
218 * We disable irq here to prevent further events.
220 * Notice this maybe result in nested disable if the interrupt type is
221 * INTx, but it's OK for we are going to free it.
223 * If this function is a part of VM destroy, please ensure that till
224 * now, the kvm state is still legal for probably we also have to wait
225 * on a currently running IRQ handler.
227 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSIX
) {
229 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
230 disable_irq(assigned_dev
->host_msix_entries
[i
].vector
);
232 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
233 free_irq(assigned_dev
->host_msix_entries
[i
].vector
,
236 assigned_dev
->entries_nr
= 0;
237 kfree(assigned_dev
->host_msix_entries
);
238 kfree(assigned_dev
->guest_msix_entries
);
239 pci_disable_msix(assigned_dev
->dev
);
241 /* Deal with MSI and INTx */
242 if ((assigned_dev
->irq_requested_type
&
243 KVM_DEV_IRQ_HOST_INTX
) &&
244 (assigned_dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
)) {
245 spin_lock_irq(&assigned_dev
->intx_lock
);
246 pci_intx(assigned_dev
->dev
, false);
247 spin_unlock_irq(&assigned_dev
->intx_lock
);
248 synchronize_irq(assigned_dev
->host_irq
);
250 disable_irq(assigned_dev
->host_irq
);
252 free_irq(assigned_dev
->host_irq
, assigned_dev
);
254 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSI
)
255 pci_disable_msi(assigned_dev
->dev
);
258 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_HOST_MASK
);
261 static int kvm_deassign_irq(struct kvm
*kvm
,
262 struct kvm_assigned_dev_kernel
*assigned_dev
,
263 unsigned long irq_requested_type
)
265 unsigned long guest_irq_type
, host_irq_type
;
267 if (!irqchip_in_kernel(kvm
))
269 /* no irq assignment to deassign */
270 if (!assigned_dev
->irq_requested_type
)
273 host_irq_type
= irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
;
274 guest_irq_type
= irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
;
277 deassign_host_irq(kvm
, assigned_dev
);
279 deassign_guest_irq(kvm
, assigned_dev
);
284 static void kvm_free_assigned_irq(struct kvm
*kvm
,
285 struct kvm_assigned_dev_kernel
*assigned_dev
)
287 kvm_deassign_irq(kvm
, assigned_dev
, assigned_dev
->irq_requested_type
);
290 static void kvm_free_assigned_device(struct kvm
*kvm
,
291 struct kvm_assigned_dev_kernel
294 kvm_free_assigned_irq(kvm
, assigned_dev
);
296 pci_reset_function(assigned_dev
->dev
);
297 if (pci_load_and_free_saved_state(assigned_dev
->dev
,
298 &assigned_dev
->pci_saved_state
))
299 printk(KERN_INFO
"%s: Couldn't reload %s saved state\n",
300 __func__
, dev_name(&assigned_dev
->dev
->dev
));
302 pci_restore_state(assigned_dev
->dev
);
304 assigned_dev
->dev
->dev_flags
&= ~PCI_DEV_FLAGS_ASSIGNED
;
306 pci_release_regions(assigned_dev
->dev
);
307 pci_disable_device(assigned_dev
->dev
);
308 pci_dev_put(assigned_dev
->dev
);
310 list_del(&assigned_dev
->list
);
314 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
316 struct list_head
*ptr
, *ptr2
;
317 struct kvm_assigned_dev_kernel
*assigned_dev
;
319 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
320 assigned_dev
= list_entry(ptr
,
321 struct kvm_assigned_dev_kernel
,
324 kvm_free_assigned_device(kvm
, assigned_dev
);
328 static int assigned_device_enable_host_intx(struct kvm
*kvm
,
329 struct kvm_assigned_dev_kernel
*dev
)
331 irq_handler_t irq_handler
;
334 dev
->host_irq
= dev
->dev
->irq
;
337 * We can only share the IRQ line with other host devices if we are
338 * able to disable the IRQ source at device-level - independently of
339 * the guest driver. Otherwise host devices may suffer from unbounded
340 * IRQ latencies when the guest keeps the line asserted.
342 if (dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
) {
343 irq_handler
= kvm_assigned_dev_intx
;
347 flags
= IRQF_ONESHOT
;
349 if (request_threaded_irq(dev
->host_irq
, irq_handler
,
350 kvm_assigned_dev_thread_intx
, flags
,
354 if (dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
) {
355 spin_lock_irq(&dev
->intx_lock
);
356 pci_intx(dev
->dev
, true);
357 spin_unlock_irq(&dev
->intx_lock
);
362 #ifdef __KVM_HAVE_MSI
363 static int assigned_device_enable_host_msi(struct kvm
*kvm
,
364 struct kvm_assigned_dev_kernel
*dev
)
368 if (!dev
->dev
->msi_enabled
) {
369 r
= pci_enable_msi(dev
->dev
);
374 dev
->host_irq
= dev
->dev
->irq
;
375 if (request_threaded_irq(dev
->host_irq
, kvm_assigned_dev_msi
,
376 kvm_assigned_dev_thread_msi
, 0,
377 dev
->irq_name
, dev
)) {
378 pci_disable_msi(dev
->dev
);
386 #ifdef __KVM_HAVE_MSIX
387 static int assigned_device_enable_host_msix(struct kvm
*kvm
,
388 struct kvm_assigned_dev_kernel
*dev
)
392 /* host_msix_entries and guest_msix_entries should have been
394 if (dev
->entries_nr
== 0)
397 r
= pci_enable_msix(dev
->dev
, dev
->host_msix_entries
, dev
->entries_nr
);
401 for (i
= 0; i
< dev
->entries_nr
; i
++) {
402 r
= request_threaded_irq(dev
->host_msix_entries
[i
].vector
,
403 kvm_assigned_dev_msix
,
404 kvm_assigned_dev_thread_msix
,
405 0, dev
->irq_name
, dev
);
412 for (i
-= 1; i
>= 0; i
--)
413 free_irq(dev
->host_msix_entries
[i
].vector
, dev
);
414 pci_disable_msix(dev
->dev
);
420 static int assigned_device_enable_guest_intx(struct kvm
*kvm
,
421 struct kvm_assigned_dev_kernel
*dev
,
422 struct kvm_assigned_irq
*irq
)
424 dev
->guest_irq
= irq
->guest_irq
;
425 dev
->ack_notifier
.gsi
= irq
->guest_irq
;
429 #ifdef __KVM_HAVE_MSI
430 static int assigned_device_enable_guest_msi(struct kvm
*kvm
,
431 struct kvm_assigned_dev_kernel
*dev
,
432 struct kvm_assigned_irq
*irq
)
434 dev
->guest_irq
= irq
->guest_irq
;
435 dev
->ack_notifier
.gsi
= -1;
440 #ifdef __KVM_HAVE_MSIX
441 static int assigned_device_enable_guest_msix(struct kvm
*kvm
,
442 struct kvm_assigned_dev_kernel
*dev
,
443 struct kvm_assigned_irq
*irq
)
445 dev
->guest_irq
= irq
->guest_irq
;
446 dev
->ack_notifier
.gsi
= -1;
451 static int assign_host_irq(struct kvm
*kvm
,
452 struct kvm_assigned_dev_kernel
*dev
,
457 if (dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
)
460 snprintf(dev
->irq_name
, sizeof(dev
->irq_name
), "kvm:%s",
463 switch (host_irq_type
) {
464 case KVM_DEV_IRQ_HOST_INTX
:
465 r
= assigned_device_enable_host_intx(kvm
, dev
);
467 #ifdef __KVM_HAVE_MSI
468 case KVM_DEV_IRQ_HOST_MSI
:
469 r
= assigned_device_enable_host_msi(kvm
, dev
);
472 #ifdef __KVM_HAVE_MSIX
473 case KVM_DEV_IRQ_HOST_MSIX
:
474 r
= assigned_device_enable_host_msix(kvm
, dev
);
480 dev
->host_irq_disabled
= false;
483 dev
->irq_requested_type
|= host_irq_type
;
488 static int assign_guest_irq(struct kvm
*kvm
,
489 struct kvm_assigned_dev_kernel
*dev
,
490 struct kvm_assigned_irq
*irq
,
491 unsigned long guest_irq_type
)
496 if (dev
->irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
)
499 id
= kvm_request_irq_source_id(kvm
);
503 dev
->irq_source_id
= id
;
505 switch (guest_irq_type
) {
506 case KVM_DEV_IRQ_GUEST_INTX
:
507 r
= assigned_device_enable_guest_intx(kvm
, dev
, irq
);
509 #ifdef __KVM_HAVE_MSI
510 case KVM_DEV_IRQ_GUEST_MSI
:
511 r
= assigned_device_enable_guest_msi(kvm
, dev
, irq
);
514 #ifdef __KVM_HAVE_MSIX
515 case KVM_DEV_IRQ_GUEST_MSIX
:
516 r
= assigned_device_enable_guest_msix(kvm
, dev
, irq
);
524 dev
->irq_requested_type
|= guest_irq_type
;
525 if (dev
->ack_notifier
.gsi
!= -1)
526 kvm_register_irq_ack_notifier(kvm
, &dev
->ack_notifier
);
528 kvm_free_irq_source_id(kvm
, dev
->irq_source_id
);
533 /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
534 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
535 struct kvm_assigned_irq
*assigned_irq
)
538 struct kvm_assigned_dev_kernel
*match
;
539 unsigned long host_irq_type
, guest_irq_type
;
541 if (!irqchip_in_kernel(kvm
))
544 mutex_lock(&kvm
->lock
);
546 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
547 assigned_irq
->assigned_dev_id
);
551 host_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_HOST_MASK
);
552 guest_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_GUEST_MASK
);
555 /* can only assign one type at a time */
556 if (hweight_long(host_irq_type
) > 1)
558 if (hweight_long(guest_irq_type
) > 1)
560 if (host_irq_type
== 0 && guest_irq_type
== 0)
565 r
= assign_host_irq(kvm
, match
, host_irq_type
);
570 r
= assign_guest_irq(kvm
, match
, assigned_irq
, guest_irq_type
);
572 mutex_unlock(&kvm
->lock
);
576 static int kvm_vm_ioctl_deassign_dev_irq(struct kvm
*kvm
,
577 struct kvm_assigned_irq
581 struct kvm_assigned_dev_kernel
*match
;
582 unsigned long irq_type
;
584 mutex_lock(&kvm
->lock
);
586 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
587 assigned_irq
->assigned_dev_id
);
591 irq_type
= assigned_irq
->flags
& (KVM_DEV_IRQ_HOST_MASK
|
592 KVM_DEV_IRQ_GUEST_MASK
);
593 r
= kvm_deassign_irq(kvm
, match
, irq_type
);
595 mutex_unlock(&kvm
->lock
);
600 * We want to test whether the caller has been granted permissions to
601 * use this device. To be able to configure and control the device,
602 * the user needs access to PCI configuration space and BAR resources.
603 * These are accessed through PCI sysfs. PCI config space is often
604 * passed to the process calling this ioctl via file descriptor, so we
605 * can't rely on access to that file. We can check for permissions
606 * on each of the BAR resource files, which is a pretty clear
607 * indicator that the user has been granted access to the device.
609 static int probe_sysfs_permissions(struct pci_dev
*dev
)
613 bool bar_found
= false;
615 for (i
= PCI_STD_RESOURCES
; i
<= PCI_STD_RESOURCE_END
; i
++) {
616 char *kpath
, *syspath
;
621 if (!pci_resource_len(dev
, i
))
624 kpath
= kobject_get_path(&dev
->dev
.kobj
, GFP_KERNEL
);
628 /* Per sysfs-rules, sysfs is always at /sys */
629 syspath
= kasprintf(GFP_KERNEL
, "/sys%s/resource%d", kpath
, i
);
634 r
= kern_path(syspath
, LOOKUP_FOLLOW
, &path
);
639 inode
= path
.dentry
->d_inode
;
641 r
= inode_permission(inode
, MAY_READ
| MAY_WRITE
| MAY_ACCESS
);
649 /* If no resources, probably something special */
655 return -EINVAL
; /* No way to control the device without sysfs */
659 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
660 struct kvm_assigned_pci_dev
*assigned_dev
)
663 struct kvm_assigned_dev_kernel
*match
;
666 if (!(assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
))
669 mutex_lock(&kvm
->lock
);
670 idx
= srcu_read_lock(&kvm
->srcu
);
672 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
673 assigned_dev
->assigned_dev_id
);
675 /* device already assigned */
680 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
682 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
687 dev
= pci_get_domain_bus_and_slot(assigned_dev
->segnr
,
689 assigned_dev
->devfn
);
691 printk(KERN_INFO
"%s: host device not found\n", __func__
);
696 /* Don't allow bridges to be assigned */
697 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
) {
702 r
= probe_sysfs_permissions(dev
);
706 if (pci_enable_device(dev
)) {
707 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
711 r
= pci_request_regions(dev
, "kvm_assigned_device");
713 printk(KERN_INFO
"%s: Could not get access to device regions\n",
718 pci_reset_function(dev
);
720 match
->pci_saved_state
= pci_store_saved_state(dev
);
721 if (!match
->pci_saved_state
)
722 printk(KERN_DEBUG
"%s: Couldn't store %s saved state\n",
723 __func__
, dev_name(&dev
->dev
));
725 if (!pci_intx_mask_supported(dev
))
726 assigned_dev
->flags
&= ~KVM_DEV_ASSIGN_PCI_2_3
;
728 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
729 match
->host_segnr
= assigned_dev
->segnr
;
730 match
->host_busnr
= assigned_dev
->busnr
;
731 match
->host_devfn
= assigned_dev
->devfn
;
732 match
->flags
= assigned_dev
->flags
;
734 spin_lock_init(&match
->intx_lock
);
735 spin_lock_init(&match
->intx_mask_lock
);
736 match
->irq_source_id
= -1;
738 match
->ack_notifier
.irq_acked
= kvm_assigned_dev_ack_irq
;
740 list_add(&match
->list
, &kvm
->arch
.assigned_dev_head
);
742 if (!kvm
->arch
.iommu_domain
) {
743 r
= kvm_iommu_map_guest(kvm
);
747 r
= kvm_assign_device(kvm
, match
);
752 srcu_read_unlock(&kvm
->srcu
, idx
);
753 mutex_unlock(&kvm
->lock
);
756 if (pci_load_and_free_saved_state(dev
, &match
->pci_saved_state
))
757 printk(KERN_INFO
"%s: Couldn't reload %s saved state\n",
758 __func__
, dev_name(&dev
->dev
));
759 list_del(&match
->list
);
760 pci_release_regions(dev
);
762 pci_disable_device(dev
);
767 srcu_read_unlock(&kvm
->srcu
, idx
);
768 mutex_unlock(&kvm
->lock
);
772 static int kvm_vm_ioctl_deassign_device(struct kvm
*kvm
,
773 struct kvm_assigned_pci_dev
*assigned_dev
)
776 struct kvm_assigned_dev_kernel
*match
;
778 mutex_lock(&kvm
->lock
);
780 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
781 assigned_dev
->assigned_dev_id
);
783 printk(KERN_INFO
"%s: device hasn't been assigned before, "
784 "so cannot be deassigned\n", __func__
);
789 kvm_deassign_device(kvm
, match
);
791 kvm_free_assigned_device(kvm
, match
);
794 mutex_unlock(&kvm
->lock
);
799 #ifdef __KVM_HAVE_MSIX
800 static int kvm_vm_ioctl_set_msix_nr(struct kvm
*kvm
,
801 struct kvm_assigned_msix_nr
*entry_nr
)
804 struct kvm_assigned_dev_kernel
*adev
;
806 mutex_lock(&kvm
->lock
);
808 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
809 entry_nr
->assigned_dev_id
);
815 if (adev
->entries_nr
== 0) {
816 adev
->entries_nr
= entry_nr
->entry_nr
;
817 if (adev
->entries_nr
== 0 ||
818 adev
->entries_nr
> KVM_MAX_MSIX_PER_DEV
) {
823 adev
->host_msix_entries
= kzalloc(sizeof(struct msix_entry
) *
826 if (!adev
->host_msix_entries
) {
830 adev
->guest_msix_entries
=
831 kzalloc(sizeof(struct msix_entry
) * entry_nr
->entry_nr
,
833 if (!adev
->guest_msix_entries
) {
834 kfree(adev
->host_msix_entries
);
838 } else /* Not allowed set MSI-X number twice */
841 mutex_unlock(&kvm
->lock
);
845 static int kvm_vm_ioctl_set_msix_entry(struct kvm
*kvm
,
846 struct kvm_assigned_msix_entry
*entry
)
849 struct kvm_assigned_dev_kernel
*adev
;
851 mutex_lock(&kvm
->lock
);
853 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
854 entry
->assigned_dev_id
);
861 for (i
= 0; i
< adev
->entries_nr
; i
++)
862 if (adev
->guest_msix_entries
[i
].vector
== 0 ||
863 adev
->guest_msix_entries
[i
].entry
== entry
->entry
) {
864 adev
->guest_msix_entries
[i
].entry
= entry
->entry
;
865 adev
->guest_msix_entries
[i
].vector
= entry
->gsi
;
866 adev
->host_msix_entries
[i
].entry
= entry
->entry
;
869 if (i
== adev
->entries_nr
) {
875 mutex_unlock(&kvm
->lock
);
881 static int kvm_vm_ioctl_set_pci_irq_mask(struct kvm
*kvm
,
882 struct kvm_assigned_pci_dev
*assigned_dev
)
885 struct kvm_assigned_dev_kernel
*match
;
887 mutex_lock(&kvm
->lock
);
889 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
890 assigned_dev
->assigned_dev_id
);
896 spin_lock(&match
->intx_mask_lock
);
898 match
->flags
&= ~KVM_DEV_ASSIGN_MASK_INTX
;
899 match
->flags
|= assigned_dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
;
901 if (match
->irq_requested_type
& KVM_DEV_IRQ_GUEST_INTX
) {
902 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
) {
903 kvm_set_irq(match
->kvm
, match
->irq_source_id
,
904 match
->guest_irq
, 0);
906 * Masking at hardware-level is performed on demand,
907 * i.e. when an IRQ actually arrives at the host.
909 } else if (!(assigned_dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
)) {
911 * Unmask the IRQ line if required. Unmasking at
912 * device level will be performed by user space.
914 spin_lock_irq(&match
->intx_lock
);
915 if (match
->host_irq_disabled
) {
916 enable_irq(match
->host_irq
);
917 match
->host_irq_disabled
= false;
919 spin_unlock_irq(&match
->intx_lock
);
923 spin_unlock(&match
->intx_mask_lock
);
926 mutex_unlock(&kvm
->lock
);
930 long kvm_vm_ioctl_assigned_device(struct kvm
*kvm
, unsigned ioctl
,
933 void __user
*argp
= (void __user
*)arg
;
937 case KVM_ASSIGN_PCI_DEVICE
: {
938 struct kvm_assigned_pci_dev assigned_dev
;
941 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
943 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
948 case KVM_ASSIGN_IRQ
: {
952 case KVM_ASSIGN_DEV_IRQ
: {
953 struct kvm_assigned_irq assigned_irq
;
956 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
958 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
963 case KVM_DEASSIGN_DEV_IRQ
: {
964 struct kvm_assigned_irq assigned_irq
;
967 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
969 r
= kvm_vm_ioctl_deassign_dev_irq(kvm
, &assigned_irq
);
974 case KVM_DEASSIGN_PCI_DEVICE
: {
975 struct kvm_assigned_pci_dev assigned_dev
;
978 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
980 r
= kvm_vm_ioctl_deassign_device(kvm
, &assigned_dev
);
985 #ifdef KVM_CAP_IRQ_ROUTING
986 case KVM_SET_GSI_ROUTING
: {
987 struct kvm_irq_routing routing
;
988 struct kvm_irq_routing __user
*urouting
;
989 struct kvm_irq_routing_entry
*entries
;
992 if (copy_from_user(&routing
, argp
, sizeof(routing
)))
995 if (routing
.nr
>= KVM_MAX_IRQ_ROUTES
)
1000 entries
= vmalloc(routing
.nr
* sizeof(*entries
));
1005 if (copy_from_user(entries
, urouting
->entries
,
1006 routing
.nr
* sizeof(*entries
)))
1007 goto out_free_irq_routing
;
1008 r
= kvm_set_irq_routing(kvm
, entries
, routing
.nr
,
1010 out_free_irq_routing
:
1014 #endif /* KVM_CAP_IRQ_ROUTING */
1015 #ifdef __KVM_HAVE_MSIX
1016 case KVM_ASSIGN_SET_MSIX_NR
: {
1017 struct kvm_assigned_msix_nr entry_nr
;
1019 if (copy_from_user(&entry_nr
, argp
, sizeof entry_nr
))
1021 r
= kvm_vm_ioctl_set_msix_nr(kvm
, &entry_nr
);
1026 case KVM_ASSIGN_SET_MSIX_ENTRY
: {
1027 struct kvm_assigned_msix_entry entry
;
1029 if (copy_from_user(&entry
, argp
, sizeof entry
))
1031 r
= kvm_vm_ioctl_set_msix_entry(kvm
, &entry
);
1037 case KVM_ASSIGN_SET_INTX_MASK
: {
1038 struct kvm_assigned_pci_dev assigned_dev
;
1041 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
1043 r
= kvm_vm_ioctl_set_pci_irq_mask(kvm
, &assigned_dev
);