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_IRQ_ROUTING
34 static inline void clear_gsi(KVMState
*s
, unsigned int gsi
)
36 uint32_t *bitmap
= s
->used_gsi_bitmap
;
38 if (gsi
< s
->max_gsi
) {
39 bitmap
[gsi
/ 32] &= ~(1U << (gsi
% 32));
41 DPRINTF("Invalid GSI %u\n", gsi
);
46 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
47 int kvm_assign_pci_device(KVMState
*s
,
48 struct kvm_assigned_pci_dev
*assigned_dev
)
50 return kvm_vm_ioctl(s
, KVM_ASSIGN_PCI_DEVICE
, assigned_dev
);
53 static int kvm_old_assign_irq(KVMState
*s
,
54 struct kvm_assigned_irq
*assigned_irq
)
56 return kvm_vm_ioctl(s
, KVM_ASSIGN_IRQ
, assigned_irq
);
59 int kvm_device_intx_set_mask(KVMState
*s
, uint32_t dev_id
, bool masked
)
61 struct kvm_assigned_pci_dev assigned_dev
;
63 assigned_dev
.assigned_dev_id
= dev_id
;
64 assigned_dev
.flags
= masked
? KVM_DEV_ASSIGN_MASK_INTX
: 0;
65 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_INTX_MASK
, &assigned_dev
);
68 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
69 int kvm_assign_irq(KVMState
*s
, struct kvm_assigned_irq
*assigned_irq
)
73 ret
= kvm_ioctl(s
, KVM_CHECK_EXTENSION
, KVM_CAP_ASSIGN_DEV_IRQ
);
75 return kvm_vm_ioctl(s
, KVM_ASSIGN_DEV_IRQ
, assigned_irq
);
78 return kvm_old_assign_irq(s
, assigned_irq
);
81 int kvm_deassign_irq(KVMState
*s
, struct kvm_assigned_irq
*assigned_irq
)
83 return kvm_vm_ioctl(s
, KVM_DEASSIGN_DEV_IRQ
, assigned_irq
);
86 int kvm_assign_irq(KVMState
*s
, struct kvm_assigned_irq
*assigned_irq
)
88 return kvm_old_assign_irq(s
, assigned_irq
);
93 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
94 int kvm_deassign_pci_device(KVMState
*s
,
95 struct kvm_assigned_pci_dev
*assigned_dev
)
97 return kvm_vm_ioctl(s
, KVM_DEASSIGN_PCI_DEVICE
, assigned_dev
);
101 int kvm_del_routing_entry(struct kvm_irq_routing_entry
*entry
)
103 #ifdef KVM_CAP_IRQ_ROUTING
104 KVMState
*s
= kvm_state
;
105 struct kvm_irq_routing_entry
*e
, *p
;
106 int i
, gsi
, found
= 0;
110 for (i
= 0; i
< s
->irq_routes
->nr
; ++i
) {
111 e
= &s
->irq_routes
->entries
[i
];
112 if (e
->type
== entry
->type
&& e
->gsi
== gsi
) {
114 case KVM_IRQ_ROUTING_IRQCHIP
:{
115 if (e
->u
.irqchip
.irqchip
==
116 entry
->u
.irqchip
.irqchip
117 && e
->u
.irqchip
.pin
== entry
->u
.irqchip
.pin
) {
118 p
= &s
->irq_routes
->entries
[--s
->irq_routes
->nr
];
124 case KVM_IRQ_ROUTING_MSI
:{
125 if (e
->u
.msi
.address_lo
==
126 entry
->u
.msi
.address_lo
127 && e
->u
.msi
.address_hi
==
128 entry
->u
.msi
.address_hi
129 && e
->u
.msi
.data
== entry
->u
.msi
.data
) {
130 p
= &s
->irq_routes
->entries
[--s
->irq_routes
->nr
];
140 /* If there are no other users of this GSI
141 * mark it available in the bitmap */
142 for (i
= 0; i
< s
->irq_routes
->nr
; i
++) {
143 e
= &s
->irq_routes
->entries
[i
];
147 if (i
== s
->irq_routes
->nr
) {
161 int kvm_update_routing_entry(struct kvm_irq_routing_entry
*entry
,
162 struct kvm_irq_routing_entry
*newentry
)
164 #ifdef KVM_CAP_IRQ_ROUTING
165 KVMState
*s
= kvm_state
;
166 struct kvm_irq_routing_entry
*e
;
169 if (entry
->gsi
!= newentry
->gsi
|| entry
->type
!= newentry
->type
) {
173 for (i
= 0; i
< s
->irq_routes
->nr
; ++i
) {
174 e
= &s
->irq_routes
->entries
[i
];
175 if (e
->type
!= entry
->type
|| e
->gsi
!= entry
->gsi
) {
179 case KVM_IRQ_ROUTING_IRQCHIP
:
180 if (e
->u
.irqchip
.irqchip
== entry
->u
.irqchip
.irqchip
&&
181 e
->u
.irqchip
.pin
== entry
->u
.irqchip
.pin
) {
182 memcpy(&e
->u
.irqchip
, &newentry
->u
.irqchip
,
183 sizeof e
->u
.irqchip
);
187 case KVM_IRQ_ROUTING_MSI
:
188 if (e
->u
.msi
.address_lo
== entry
->u
.msi
.address_lo
&&
189 e
->u
.msi
.address_hi
== entry
->u
.msi
.address_hi
&&
190 e
->u
.msi
.data
== entry
->u
.msi
.data
) {
191 memcpy(&e
->u
.msi
, &newentry
->u
.msi
, sizeof e
->u
.msi
);
205 int kvm_get_irq_route_gsi(void)
207 #ifdef KVM_CAP_IRQ_ROUTING
208 KVMState
*s
= kvm_state
;
210 uint32_t *buf
= s
->used_gsi_bitmap
;
212 /* Return the lowest unused GSI in the bitmap */
213 for (i
= 0; i
< s
->max_gsi
/ 32; i
++) {
219 return bit
- 1 + i
* 32;
228 #ifdef KVM_CAP_IRQ_ROUTING
229 static void kvm_msi_routing_entry(struct kvm_irq_routing_entry
*e
,
234 e
->type
= KVM_IRQ_ROUTING_MSI
;
236 e
->u
.msi
.address_lo
= msg
->addr_lo
;
237 e
->u
.msi
.address_hi
= msg
->addr_hi
;
238 e
->u
.msi
.data
= msg
->data
;
242 int kvm_msi_message_add(KVMMsiMessage
*msg
)
244 #ifdef KVM_CAP_IRQ_ROUTING
245 struct kvm_irq_routing_entry e
;
248 ret
= kvm_get_irq_route_gsi();
254 kvm_msi_routing_entry(&e
, msg
);
255 kvm_add_routing_entry(kvm_state
, &e
);
262 int kvm_msi_message_del(KVMMsiMessage
*msg
)
264 #ifdef KVM_CAP_IRQ_ROUTING
265 struct kvm_irq_routing_entry e
;
267 kvm_msi_routing_entry(&e
, msg
);
268 return kvm_del_routing_entry(&e
);
274 int kvm_msi_message_update(KVMMsiMessage
*old
, KVMMsiMessage
*new)
276 #ifdef KVM_CAP_IRQ_ROUTING
277 struct kvm_irq_routing_entry e1
, e2
;
281 if (memcmp(old
, new, sizeof(KVMMsiMessage
)) == 0) {
285 kvm_msi_routing_entry(&e1
, old
);
286 kvm_msi_routing_entry(&e2
, new);
288 ret
= kvm_update_routing_entry(&e1
, &e2
);
300 #ifdef KVM_CAP_DEVICE_MSIX
301 int kvm_assign_set_msix_nr(KVMState
*s
, struct kvm_assigned_msix_nr
*msix_nr
)
303 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_NR
, msix_nr
);
306 int kvm_assign_set_msix_entry(KVMState
*s
,
307 struct kvm_assigned_msix_entry
*entry
)
309 return kvm_vm_ioctl(s
, KVM_ASSIGN_SET_MSIX_ENTRY
, entry
);
313 #if !defined(TARGET_I386)
314 void kvm_arch_init_irq_routing(KVMState
*s
)