4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
9 #include "config-host.h"
15 #include "qemu-common.h"
25 #define EXPECTED_KVM_API_VERSION 12
27 #if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
28 #error libkvm: userspace and kernel version mismatch
31 #define ALIGN(x, y) (((x)+(y)-1) & ~((y)-1))
33 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
34 int kvm_assign_pci_device(KVMState
*s
,
35 struct kvm_assigned_pci_dev
*assigned_dev
)
37 return kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, assigned_dev
);
40 static int kvm_old_assign_irq(KVMState
*s
,
41 struct kvm_assigned_irq
*assigned_irq
)
43 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, assigned_irq
);
46 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
48 struct kvm_assigned_pci_dev assigned_dev
;
50 assigned_dev
.assigned_dev_id
= dev_id
;
51 assigned_dev
.flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0;
52 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &assigned_dev
);
55 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
56 int kvm_assign_irq(KVMState
*s
, struct kvm_assigned_irq
*assigned_irq
)
60 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_ASSIGN_DEV_IRQ
);
62 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, assigned_irq
);
65 return kvm_old_assign_irq(s
, assigned_irq
);
68 int kvm_deassign_irq(KVMState
*s
, struct kvm_assigned_irq
*assigned_irq
)
70 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, assigned_irq
);
73 int kvm_assign_irq(KVMState
*s
, struct kvm_assigned_irq
*assigned_irq
)
75 return kvm_old_assign_irq(s
, assigned_irq
);
80 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
81 int kvm_deassign_pci_device(KVMState
*s
,
82 struct kvm_assigned_pci_dev
*assigned_dev
)
84 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, assigned_dev
);
88 int kvm_del_routing_entry(struct kvm_irq_routing_entry
*entry
)
90 #ifdef KVM_CAP_IRQ_ROUTING
91 KVMState
*s
= kvm_state
;
92 struct kvm_irq_routing_entry
*e
, *p
;
93 int i
, gsi
, found
= 0;
97 for (i
= 0; i
< s
->irq_routes
->nr
; ++i
) {
98 e
= &s
->irq_routes
->entries
[i
];
99 if (e
->type
== entry
->type
&& e
->gsi
== gsi
) {
101 case KVM_IRQ_ROUTING_IRQCHIP
:{
102 if (e
->u
.irqchip
.irqchip
==
103 entry
->u
.irqchip
.irqchip
104 && e
->u
.irqchip
.pin
== entry
->u
.irqchip
.pin
) {
105 p
= &s
->irq_routes
->entries
[--s
->irq_routes
->nr
];
111 case KVM_IRQ_ROUTING_MSI
:{
112 if (e
->u
.msi
.address_lo
==
113 entry
->u
.msi
.address_lo
114 && e
->u
.msi
.address_hi
==
115 entry
->u
.msi
.address_hi
116 && e
->u
.msi
.data
== entry
->u
.msi
.data
) {
117 p
= &s
->irq_routes
->entries
[--s
->irq_routes
->nr
];
127 /* If there are no other users of this GSI
128 * mark it available in the bitmap */
129 for (i
= 0; i
< s
->irq_routes
->nr
; i
++) {
130 e
= &s
->irq_routes
->entries
[i
];
134 if (i
== s
->irq_routes
->nr
) {
148 int kvm_update_routing_entry(struct kvm_irq_routing_entry
*entry
,
149 struct kvm_irq_routing_entry
*newentry
)
151 #ifdef KVM_CAP_IRQ_ROUTING
152 KVMState
*s
= kvm_state
;
153 struct kvm_irq_routing_entry
*e
;
156 if (entry
->gsi
!= newentry
->gsi
|| entry
->type
!= newentry
->type
) {
160 for (i
= 0; i
< s
->irq_routes
->nr
; ++i
) {
161 e
= &s
->irq_routes
->entries
[i
];
162 if (e
->type
!= entry
->type
|| e
->gsi
!= entry
->gsi
) {
166 case KVM_IRQ_ROUTING_IRQCHIP
:
167 if (e
->u
.irqchip
.irqchip
== entry
->u
.irqchip
.irqchip
&&
168 e
->u
.irqchip
.pin
== entry
->u
.irqchip
.pin
) {
169 memcpy(&e
->u
.irqchip
, &newentry
->u
.irqchip
,
170 sizeof e
->u
.irqchip
);
174 case KVM_IRQ_ROUTING_MSI
:
175 if (e
->u
.msi
.address_lo
== entry
->u
.msi
.address_lo
&&
176 e
->u
.msi
.address_hi
== entry
->u
.msi
.address_hi
&&
177 e
->u
.msi
.data
== entry
->u
.msi
.data
) {
178 memcpy(&e
->u
.msi
, &newentry
->u
.msi
, sizeof e
->u
.msi
);
192 int kvm_get_irq_route_gsi(void)
194 #ifdef KVM_CAP_IRQ_ROUTING
195 KVMState
*s
= kvm_state
;
196 int max_words
= ALIGN(s
->gsi_count
, 32) / 32;
198 uint32_t *buf
= s
->used_gsi_bitmap
;
200 /* Return the lowest unused GSI in the bitmap */
201 for (i
= 0; i
< max_words
; i
++) {
207 return bit
- 1 + i
* 32;
216 #ifdef KVM_CAP_DEVICE_MSIX
217 int kvm_assign_set_msix_nr(KVMState
*s
, struct kvm_assigned_msix_nr
*msix_nr
)
219 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, msix_nr
);
222 int kvm_assign_set_msix_entry(KVMState
*s
,
223 struct kvm_assigned_msix_entry
*entry
)
225 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, entry
);
229 #if !defined(TARGET_I386)
230 void kvm_arch_init_irq_routing(KVMState
*s
)