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,
85 spin_unlock(&assigned_dev
->intx_mask_lock
);
87 kvm_set_irq(assigned_dev
->kvm
, assigned_dev
->irq_source_id
,
91 static irqreturn_t
kvm_assigned_dev_thread_intx(int irq
, void *dev_id
)
93 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
95 if (!(assigned_dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
)) {
96 spin_lock_irq(&assigned_dev
->intx_lock
);
97 disable_irq_nosync(irq
);
98 assigned_dev
->host_irq_disabled
= true;
99 spin_unlock_irq(&assigned_dev
->intx_lock
);
102 kvm_assigned_dev_raise_guest_irq(assigned_dev
,
103 assigned_dev
->guest_irq
);
108 #ifdef __KVM_HAVE_MSI
109 static irqreturn_t
kvm_assigned_dev_msi(int irq
, void *dev_id
)
111 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
112 int ret
= kvm_set_irq_inatomic(assigned_dev
->kvm
,
113 assigned_dev
->irq_source_id
,
114 assigned_dev
->guest_irq
, 1);
115 return unlikely(ret
== -EWOULDBLOCK
) ? IRQ_WAKE_THREAD
: IRQ_HANDLED
;
118 static irqreturn_t
kvm_assigned_dev_thread_msi(int irq
, void *dev_id
)
120 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
122 kvm_assigned_dev_raise_guest_irq(assigned_dev
,
123 assigned_dev
->guest_irq
);
129 #ifdef __KVM_HAVE_MSIX
130 static irqreturn_t
kvm_assigned_dev_msix(int irq
, void *dev_id
)
132 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
133 int index
= find_index_from_host_irq(assigned_dev
, irq
);
138 vector
= assigned_dev
->guest_msix_entries
[index
].vector
;
139 ret
= kvm_set_irq_inatomic(assigned_dev
->kvm
,
140 assigned_dev
->irq_source_id
,
144 return unlikely(ret
== -EWOULDBLOCK
) ? IRQ_WAKE_THREAD
: IRQ_HANDLED
;
147 static irqreturn_t
kvm_assigned_dev_thread_msix(int irq
, void *dev_id
)
149 struct kvm_assigned_dev_kernel
*assigned_dev
= dev_id
;
150 int index
= find_index_from_host_irq(assigned_dev
, irq
);
154 vector
= assigned_dev
->guest_msix_entries
[index
].vector
;
155 kvm_assigned_dev_raise_guest_irq(assigned_dev
, vector
);
162 /* Ack the irq line for an assigned device */
163 static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier
*kian
)
165 struct kvm_assigned_dev_kernel
*dev
=
166 container_of(kian
, struct kvm_assigned_dev_kernel
,
169 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
, dev
->guest_irq
, 0, false);
171 spin_lock(&dev
->intx_mask_lock
);
173 if (!(dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
)) {
174 bool reassert
= false;
176 spin_lock_irq(&dev
->intx_lock
);
178 * The guest IRQ may be shared so this ack can come from an
179 * IRQ for another guest device.
181 if (dev
->host_irq_disabled
) {
182 if (!(dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
))
183 enable_irq(dev
->host_irq
);
184 else if (!pci_check_and_unmask_intx(dev
->dev
))
186 dev
->host_irq_disabled
= reassert
;
188 spin_unlock_irq(&dev
->intx_lock
);
191 kvm_set_irq(dev
->kvm
, dev
->irq_source_id
,
192 dev
->guest_irq
, 1, false);
195 spin_unlock(&dev
->intx_mask_lock
);
198 static void deassign_guest_irq(struct kvm
*kvm
,
199 struct kvm_assigned_dev_kernel
*assigned_dev
)
201 if (assigned_dev
->ack_notifier
.gsi
!= -1)
202 kvm_unregister_irq_ack_notifier(kvm
,
203 &assigned_dev
->ack_notifier
);
205 kvm_set_irq(assigned_dev
->kvm
, assigned_dev
->irq_source_id
,
206 assigned_dev
->guest_irq
, 0, false);
208 if (assigned_dev
->irq_source_id
!= -1)
209 kvm_free_irq_source_id(kvm
, assigned_dev
->irq_source_id
);
210 assigned_dev
->irq_source_id
= -1;
211 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_GUEST_MASK
);
214 /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
215 static void deassign_host_irq(struct kvm
*kvm
,
216 struct kvm_assigned_dev_kernel
*assigned_dev
)
219 * We disable irq here to prevent further events.
221 * Notice this maybe result in nested disable if the interrupt type is
222 * INTx, but it's OK for we are going to free it.
224 * If this function is a part of VM destroy, please ensure that till
225 * now, the kvm state is still legal for probably we also have to wait
226 * on a currently running IRQ handler.
228 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSIX
) {
230 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
231 disable_irq(assigned_dev
->host_msix_entries
[i
].vector
);
233 for (i
= 0; i
< assigned_dev
->entries_nr
; i
++)
234 free_irq(assigned_dev
->host_msix_entries
[i
].vector
,
237 assigned_dev
->entries_nr
= 0;
238 kfree(assigned_dev
->host_msix_entries
);
239 kfree(assigned_dev
->guest_msix_entries
);
240 pci_disable_msix(assigned_dev
->dev
);
242 /* Deal with MSI and INTx */
243 if ((assigned_dev
->irq_requested_type
&
244 KVM_DEV_IRQ_HOST_INTX
) &&
245 (assigned_dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
)) {
246 spin_lock_irq(&assigned_dev
->intx_lock
);
247 pci_intx(assigned_dev
->dev
, false);
248 spin_unlock_irq(&assigned_dev
->intx_lock
);
249 synchronize_irq(assigned_dev
->host_irq
);
251 disable_irq(assigned_dev
->host_irq
);
253 free_irq(assigned_dev
->host_irq
, assigned_dev
);
255 if (assigned_dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MSI
)
256 pci_disable_msi(assigned_dev
->dev
);
259 assigned_dev
->irq_requested_type
&= ~(KVM_DEV_IRQ_HOST_MASK
);
262 static int kvm_deassign_irq(struct kvm
*kvm
,
263 struct kvm_assigned_dev_kernel
*assigned_dev
,
264 unsigned long irq_requested_type
)
266 unsigned long guest_irq_type
, host_irq_type
;
268 if (!irqchip_in_kernel(kvm
))
270 /* no irq assignment to deassign */
271 if (!assigned_dev
->irq_requested_type
)
274 host_irq_type
= irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
;
275 guest_irq_type
= irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
;
278 deassign_host_irq(kvm
, assigned_dev
);
280 deassign_guest_irq(kvm
, assigned_dev
);
285 static void kvm_free_assigned_irq(struct kvm
*kvm
,
286 struct kvm_assigned_dev_kernel
*assigned_dev
)
288 kvm_deassign_irq(kvm
, assigned_dev
, assigned_dev
->irq_requested_type
);
291 static void kvm_free_assigned_device(struct kvm
*kvm
,
292 struct kvm_assigned_dev_kernel
295 kvm_free_assigned_irq(kvm
, assigned_dev
);
297 pci_reset_function(assigned_dev
->dev
);
298 if (pci_load_and_free_saved_state(assigned_dev
->dev
,
299 &assigned_dev
->pci_saved_state
))
300 printk(KERN_INFO
"%s: Couldn't reload %s saved state\n",
301 __func__
, dev_name(&assigned_dev
->dev
->dev
));
303 pci_restore_state(assigned_dev
->dev
);
305 assigned_dev
->dev
->dev_flags
&= ~PCI_DEV_FLAGS_ASSIGNED
;
307 pci_release_regions(assigned_dev
->dev
);
308 pci_disable_device(assigned_dev
->dev
);
309 pci_dev_put(assigned_dev
->dev
);
311 list_del(&assigned_dev
->list
);
315 void kvm_free_all_assigned_devices(struct kvm
*kvm
)
317 struct list_head
*ptr
, *ptr2
;
318 struct kvm_assigned_dev_kernel
*assigned_dev
;
320 list_for_each_safe(ptr
, ptr2
, &kvm
->arch
.assigned_dev_head
) {
321 assigned_dev
= list_entry(ptr
,
322 struct kvm_assigned_dev_kernel
,
325 kvm_free_assigned_device(kvm
, assigned_dev
);
329 static int assigned_device_enable_host_intx(struct kvm
*kvm
,
330 struct kvm_assigned_dev_kernel
*dev
)
332 irq_handler_t irq_handler
;
335 dev
->host_irq
= dev
->dev
->irq
;
338 * We can only share the IRQ line with other host devices if we are
339 * able to disable the IRQ source at device-level - independently of
340 * the guest driver. Otherwise host devices may suffer from unbounded
341 * IRQ latencies when the guest keeps the line asserted.
343 if (dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
) {
344 irq_handler
= kvm_assigned_dev_intx
;
348 flags
= IRQF_ONESHOT
;
350 if (request_threaded_irq(dev
->host_irq
, irq_handler
,
351 kvm_assigned_dev_thread_intx
, flags
,
355 if (dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
) {
356 spin_lock_irq(&dev
->intx_lock
);
357 pci_intx(dev
->dev
, true);
358 spin_unlock_irq(&dev
->intx_lock
);
363 #ifdef __KVM_HAVE_MSI
364 static int assigned_device_enable_host_msi(struct kvm
*kvm
,
365 struct kvm_assigned_dev_kernel
*dev
)
369 if (!dev
->dev
->msi_enabled
) {
370 r
= pci_enable_msi(dev
->dev
);
375 dev
->host_irq
= dev
->dev
->irq
;
376 if (request_threaded_irq(dev
->host_irq
, kvm_assigned_dev_msi
,
377 kvm_assigned_dev_thread_msi
, 0,
378 dev
->irq_name
, dev
)) {
379 pci_disable_msi(dev
->dev
);
387 #ifdef __KVM_HAVE_MSIX
388 static int assigned_device_enable_host_msix(struct kvm
*kvm
,
389 struct kvm_assigned_dev_kernel
*dev
)
393 /* host_msix_entries and guest_msix_entries should have been
395 if (dev
->entries_nr
== 0)
398 r
= pci_enable_msix_exact(dev
->dev
,
399 dev
->host_msix_entries
, dev
->entries_nr
);
403 for (i
= 0; i
< dev
->entries_nr
; i
++) {
404 r
= request_threaded_irq(dev
->host_msix_entries
[i
].vector
,
405 kvm_assigned_dev_msix
,
406 kvm_assigned_dev_thread_msix
,
407 0, dev
->irq_name
, dev
);
414 for (i
-= 1; i
>= 0; i
--)
415 free_irq(dev
->host_msix_entries
[i
].vector
, dev
);
416 pci_disable_msix(dev
->dev
);
422 static int assigned_device_enable_guest_intx(struct kvm
*kvm
,
423 struct kvm_assigned_dev_kernel
*dev
,
424 struct kvm_assigned_irq
*irq
)
426 dev
->guest_irq
= irq
->guest_irq
;
427 dev
->ack_notifier
.gsi
= irq
->guest_irq
;
431 #ifdef __KVM_HAVE_MSI
432 static int assigned_device_enable_guest_msi(struct kvm
*kvm
,
433 struct kvm_assigned_dev_kernel
*dev
,
434 struct kvm_assigned_irq
*irq
)
436 dev
->guest_irq
= irq
->guest_irq
;
437 dev
->ack_notifier
.gsi
= -1;
442 #ifdef __KVM_HAVE_MSIX
443 static int assigned_device_enable_guest_msix(struct kvm
*kvm
,
444 struct kvm_assigned_dev_kernel
*dev
,
445 struct kvm_assigned_irq
*irq
)
447 dev
->guest_irq
= irq
->guest_irq
;
448 dev
->ack_notifier
.gsi
= -1;
453 static int assign_host_irq(struct kvm
*kvm
,
454 struct kvm_assigned_dev_kernel
*dev
,
459 if (dev
->irq_requested_type
& KVM_DEV_IRQ_HOST_MASK
)
462 snprintf(dev
->irq_name
, sizeof(dev
->irq_name
), "kvm:%s",
465 switch (host_irq_type
) {
466 case KVM_DEV_IRQ_HOST_INTX
:
467 r
= assigned_device_enable_host_intx(kvm
, dev
);
469 #ifdef __KVM_HAVE_MSI
470 case KVM_DEV_IRQ_HOST_MSI
:
471 r
= assigned_device_enable_host_msi(kvm
, dev
);
474 #ifdef __KVM_HAVE_MSIX
475 case KVM_DEV_IRQ_HOST_MSIX
:
476 r
= assigned_device_enable_host_msix(kvm
, dev
);
482 dev
->host_irq_disabled
= false;
485 dev
->irq_requested_type
|= host_irq_type
;
490 static int assign_guest_irq(struct kvm
*kvm
,
491 struct kvm_assigned_dev_kernel
*dev
,
492 struct kvm_assigned_irq
*irq
,
493 unsigned long guest_irq_type
)
498 if (dev
->irq_requested_type
& KVM_DEV_IRQ_GUEST_MASK
)
501 id
= kvm_request_irq_source_id(kvm
);
505 dev
->irq_source_id
= id
;
507 switch (guest_irq_type
) {
508 case KVM_DEV_IRQ_GUEST_INTX
:
509 r
= assigned_device_enable_guest_intx(kvm
, dev
, irq
);
511 #ifdef __KVM_HAVE_MSI
512 case KVM_DEV_IRQ_GUEST_MSI
:
513 r
= assigned_device_enable_guest_msi(kvm
, dev
, irq
);
516 #ifdef __KVM_HAVE_MSIX
517 case KVM_DEV_IRQ_GUEST_MSIX
:
518 r
= assigned_device_enable_guest_msix(kvm
, dev
, irq
);
526 dev
->irq_requested_type
|= guest_irq_type
;
527 if (dev
->ack_notifier
.gsi
!= -1)
528 kvm_register_irq_ack_notifier(kvm
, &dev
->ack_notifier
);
530 kvm_free_irq_source_id(kvm
, dev
->irq_source_id
);
531 dev
->irq_source_id
= -1;
537 /* TODO Deal with KVM_DEV_IRQ_ASSIGNED_MASK_MSIX */
538 static int kvm_vm_ioctl_assign_irq(struct kvm
*kvm
,
539 struct kvm_assigned_irq
*assigned_irq
)
542 struct kvm_assigned_dev_kernel
*match
;
543 unsigned long host_irq_type
, guest_irq_type
;
545 if (!irqchip_in_kernel(kvm
))
548 mutex_lock(&kvm
->lock
);
550 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
551 assigned_irq
->assigned_dev_id
);
555 host_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_HOST_MASK
);
556 guest_irq_type
= (assigned_irq
->flags
& KVM_DEV_IRQ_GUEST_MASK
);
559 /* can only assign one type at a time */
560 if (hweight_long(host_irq_type
) > 1)
562 if (hweight_long(guest_irq_type
) > 1)
564 if (host_irq_type
== 0 && guest_irq_type
== 0)
569 r
= assign_host_irq(kvm
, match
, host_irq_type
);
574 r
= assign_guest_irq(kvm
, match
, assigned_irq
, guest_irq_type
);
576 mutex_unlock(&kvm
->lock
);
580 static int kvm_vm_ioctl_deassign_dev_irq(struct kvm
*kvm
,
581 struct kvm_assigned_irq
585 struct kvm_assigned_dev_kernel
*match
;
586 unsigned long irq_type
;
588 mutex_lock(&kvm
->lock
);
590 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
591 assigned_irq
->assigned_dev_id
);
595 irq_type
= assigned_irq
->flags
& (KVM_DEV_IRQ_HOST_MASK
|
596 KVM_DEV_IRQ_GUEST_MASK
);
597 r
= kvm_deassign_irq(kvm
, match
, irq_type
);
599 mutex_unlock(&kvm
->lock
);
604 * We want to test whether the caller has been granted permissions to
605 * use this device. To be able to configure and control the device,
606 * the user needs access to PCI configuration space and BAR resources.
607 * These are accessed through PCI sysfs. PCI config space is often
608 * passed to the process calling this ioctl via file descriptor, so we
609 * can't rely on access to that file. We can check for permissions
610 * on each of the BAR resource files, which is a pretty clear
611 * indicator that the user has been granted access to the device.
613 static int probe_sysfs_permissions(struct pci_dev
*dev
)
617 bool bar_found
= false;
619 for (i
= PCI_STD_RESOURCES
; i
<= PCI_STD_RESOURCE_END
; i
++) {
620 char *kpath
, *syspath
;
625 if (!pci_resource_len(dev
, i
))
628 kpath
= kobject_get_path(&dev
->dev
.kobj
, GFP_KERNEL
);
632 /* Per sysfs-rules, sysfs is always at /sys */
633 syspath
= kasprintf(GFP_KERNEL
, "/sys%s/resource%d", kpath
, i
);
638 r
= kern_path(syspath
, LOOKUP_FOLLOW
, &path
);
643 inode
= path
.dentry
->d_inode
;
645 r
= inode_permission(inode
, MAY_READ
| MAY_WRITE
| MAY_ACCESS
);
653 /* If no resources, probably something special */
659 return -EINVAL
; /* No way to control the device without sysfs */
663 static int kvm_vm_ioctl_assign_device(struct kvm
*kvm
,
664 struct kvm_assigned_pci_dev
*assigned_dev
)
667 struct kvm_assigned_dev_kernel
*match
;
670 if (!(assigned_dev
->flags
& KVM_DEV_ASSIGN_ENABLE_IOMMU
))
673 mutex_lock(&kvm
->lock
);
674 idx
= srcu_read_lock(&kvm
->srcu
);
676 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
677 assigned_dev
->assigned_dev_id
);
679 /* device already assigned */
684 match
= kzalloc(sizeof(struct kvm_assigned_dev_kernel
), GFP_KERNEL
);
686 printk(KERN_INFO
"%s: Couldn't allocate memory\n",
691 dev
= pci_get_domain_bus_and_slot(assigned_dev
->segnr
,
693 assigned_dev
->devfn
);
695 printk(KERN_INFO
"%s: host device not found\n", __func__
);
700 /* Don't allow bridges to be assigned */
701 if (dev
->hdr_type
!= PCI_HEADER_TYPE_NORMAL
) {
706 r
= probe_sysfs_permissions(dev
);
710 if (pci_enable_device(dev
)) {
711 printk(KERN_INFO
"%s: Could not enable PCI device\n", __func__
);
715 r
= pci_request_regions(dev
, "kvm_assigned_device");
717 printk(KERN_INFO
"%s: Could not get access to device regions\n",
722 pci_reset_function(dev
);
724 match
->pci_saved_state
= pci_store_saved_state(dev
);
725 if (!match
->pci_saved_state
)
726 printk(KERN_DEBUG
"%s: Couldn't store %s saved state\n",
727 __func__
, dev_name(&dev
->dev
));
729 if (!pci_intx_mask_supported(dev
))
730 assigned_dev
->flags
&= ~KVM_DEV_ASSIGN_PCI_2_3
;
732 match
->assigned_dev_id
= assigned_dev
->assigned_dev_id
;
733 match
->host_segnr
= assigned_dev
->segnr
;
734 match
->host_busnr
= assigned_dev
->busnr
;
735 match
->host_devfn
= assigned_dev
->devfn
;
736 match
->flags
= assigned_dev
->flags
;
738 spin_lock_init(&match
->intx_lock
);
739 spin_lock_init(&match
->intx_mask_lock
);
740 match
->irq_source_id
= -1;
742 match
->ack_notifier
.irq_acked
= kvm_assigned_dev_ack_irq
;
744 list_add(&match
->list
, &kvm
->arch
.assigned_dev_head
);
746 if (!kvm
->arch
.iommu_domain
) {
747 r
= kvm_iommu_map_guest(kvm
);
751 r
= kvm_assign_device(kvm
, match
);
756 srcu_read_unlock(&kvm
->srcu
, idx
);
757 mutex_unlock(&kvm
->lock
);
760 if (pci_load_and_free_saved_state(dev
, &match
->pci_saved_state
))
761 printk(KERN_INFO
"%s: Couldn't reload %s saved state\n",
762 __func__
, dev_name(&dev
->dev
));
763 list_del(&match
->list
);
764 pci_release_regions(dev
);
766 pci_disable_device(dev
);
771 srcu_read_unlock(&kvm
->srcu
, idx
);
772 mutex_unlock(&kvm
->lock
);
776 static int kvm_vm_ioctl_deassign_device(struct kvm
*kvm
,
777 struct kvm_assigned_pci_dev
*assigned_dev
)
780 struct kvm_assigned_dev_kernel
*match
;
782 mutex_lock(&kvm
->lock
);
784 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
785 assigned_dev
->assigned_dev_id
);
787 printk(KERN_INFO
"%s: device hasn't been assigned before, "
788 "so cannot be deassigned\n", __func__
);
793 kvm_deassign_device(kvm
, match
);
795 kvm_free_assigned_device(kvm
, match
);
798 mutex_unlock(&kvm
->lock
);
803 #ifdef __KVM_HAVE_MSIX
804 static int kvm_vm_ioctl_set_msix_nr(struct kvm
*kvm
,
805 struct kvm_assigned_msix_nr
*entry_nr
)
808 struct kvm_assigned_dev_kernel
*adev
;
810 mutex_lock(&kvm
->lock
);
812 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
813 entry_nr
->assigned_dev_id
);
819 if (adev
->entries_nr
== 0) {
820 adev
->entries_nr
= entry_nr
->entry_nr
;
821 if (adev
->entries_nr
== 0 ||
822 adev
->entries_nr
> KVM_MAX_MSIX_PER_DEV
) {
827 adev
->host_msix_entries
= kzalloc(sizeof(struct msix_entry
) *
830 if (!adev
->host_msix_entries
) {
834 adev
->guest_msix_entries
=
835 kzalloc(sizeof(struct msix_entry
) * entry_nr
->entry_nr
,
837 if (!adev
->guest_msix_entries
) {
838 kfree(adev
->host_msix_entries
);
842 } else /* Not allowed set MSI-X number twice */
845 mutex_unlock(&kvm
->lock
);
849 static int kvm_vm_ioctl_set_msix_entry(struct kvm
*kvm
,
850 struct kvm_assigned_msix_entry
*entry
)
853 struct kvm_assigned_dev_kernel
*adev
;
855 mutex_lock(&kvm
->lock
);
857 adev
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
858 entry
->assigned_dev_id
);
865 for (i
= 0; i
< adev
->entries_nr
; i
++)
866 if (adev
->guest_msix_entries
[i
].vector
== 0 ||
867 adev
->guest_msix_entries
[i
].entry
== entry
->entry
) {
868 adev
->guest_msix_entries
[i
].entry
= entry
->entry
;
869 adev
->guest_msix_entries
[i
].vector
= entry
->gsi
;
870 adev
->host_msix_entries
[i
].entry
= entry
->entry
;
873 if (i
== adev
->entries_nr
) {
879 mutex_unlock(&kvm
->lock
);
885 static int kvm_vm_ioctl_set_pci_irq_mask(struct kvm
*kvm
,
886 struct kvm_assigned_pci_dev
*assigned_dev
)
889 struct kvm_assigned_dev_kernel
*match
;
891 mutex_lock(&kvm
->lock
);
893 match
= kvm_find_assigned_dev(&kvm
->arch
.assigned_dev_head
,
894 assigned_dev
->assigned_dev_id
);
900 spin_lock(&match
->intx_mask_lock
);
902 match
->flags
&= ~KVM_DEV_ASSIGN_MASK_INTX
;
903 match
->flags
|= assigned_dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
;
905 if (match
->irq_requested_type
& KVM_DEV_IRQ_GUEST_INTX
) {
906 if (assigned_dev
->flags
& KVM_DEV_ASSIGN_MASK_INTX
) {
907 kvm_set_irq(match
->kvm
, match
->irq_source_id
,
908 match
->guest_irq
, 0, false);
910 * Masking at hardware-level is performed on demand,
911 * i.e. when an IRQ actually arrives at the host.
913 } else if (!(assigned_dev
->flags
& KVM_DEV_ASSIGN_PCI_2_3
)) {
915 * Unmask the IRQ line if required. Unmasking at
916 * device level will be performed by user space.
918 spin_lock_irq(&match
->intx_lock
);
919 if (match
->host_irq_disabled
) {
920 enable_irq(match
->host_irq
);
921 match
->host_irq_disabled
= false;
923 spin_unlock_irq(&match
->intx_lock
);
927 spin_unlock(&match
->intx_mask_lock
);
930 mutex_unlock(&kvm
->lock
);
934 long kvm_vm_ioctl_assigned_device(struct kvm
*kvm
, unsigned ioctl
,
937 void __user
*argp
= (void __user
*)arg
;
941 case KVM_ASSIGN_PCI_DEVICE
: {
942 struct kvm_assigned_pci_dev assigned_dev
;
945 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
947 r
= kvm_vm_ioctl_assign_device(kvm
, &assigned_dev
);
952 case KVM_ASSIGN_IRQ
: {
956 case KVM_ASSIGN_DEV_IRQ
: {
957 struct kvm_assigned_irq assigned_irq
;
960 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
962 r
= kvm_vm_ioctl_assign_irq(kvm
, &assigned_irq
);
967 case KVM_DEASSIGN_DEV_IRQ
: {
968 struct kvm_assigned_irq assigned_irq
;
971 if (copy_from_user(&assigned_irq
, argp
, sizeof assigned_irq
))
973 r
= kvm_vm_ioctl_deassign_dev_irq(kvm
, &assigned_irq
);
978 case KVM_DEASSIGN_PCI_DEVICE
: {
979 struct kvm_assigned_pci_dev assigned_dev
;
982 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
984 r
= kvm_vm_ioctl_deassign_device(kvm
, &assigned_dev
);
989 #ifdef __KVM_HAVE_MSIX
990 case KVM_ASSIGN_SET_MSIX_NR
: {
991 struct kvm_assigned_msix_nr entry_nr
;
993 if (copy_from_user(&entry_nr
, argp
, sizeof entry_nr
))
995 r
= kvm_vm_ioctl_set_msix_nr(kvm
, &entry_nr
);
1000 case KVM_ASSIGN_SET_MSIX_ENTRY
: {
1001 struct kvm_assigned_msix_entry entry
;
1003 if (copy_from_user(&entry
, argp
, sizeof entry
))
1005 r
= kvm_vm_ioctl_set_msix_entry(kvm
, &entry
);
1011 case KVM_ASSIGN_SET_INTX_MASK
: {
1012 struct kvm_assigned_pci_dev assigned_dev
;
1015 if (copy_from_user(&assigned_dev
, argp
, sizeof assigned_dev
))
1017 r
= kvm_vm_ioctl_set_pci_irq_mask(kvm
, &assigned_dev
);