Merge commit '5a4d701acde890a5ca134236424ece45545f70c7' into upstream-merge
[qemu-kvm.git] / qemu-kvm.c
blobefdeff120047898d3b7eb3ccca11314b95a1de80
1 /*
2 * qemu/kvm integration
4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
7 */
8 #include "config.h"
9 #include "config-host.h"
11 #include <assert.h>
12 #include <string.h>
13 #include "hw/hw.h"
14 #include "sysemu.h"
15 #include "qemu-common.h"
16 #include "console.h"
17 #include "block.h"
18 #include "compatfd.h"
19 #include "gdbstub.h"
20 #include "monitor.h"
21 #include "cpus.h"
23 #include "qemu-kvm.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
29 #endif
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)
58 int ret;
60 ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_ASSIGN_DEV_IRQ);
61 if (ret > 0) {
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);
72 #else
73 int kvm_assign_irq(KVMState *s, struct kvm_assigned_irq *assigned_irq)
75 return kvm_old_assign_irq(s, assigned_irq);
77 #endif
78 #endif
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);
86 #endif
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;
95 gsi = entry->gsi;
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) {
100 switch (e->type) {
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];
106 *e = *p;
107 found = 1;
109 break;
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];
118 *e = *p;
119 found = 1;
121 break;
123 default:
124 break;
126 if (found) {
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];
131 if (e->gsi == gsi)
132 break;
134 if (i == s->irq_routes->nr) {
135 clear_gsi(s, gsi);
138 return 0;
142 return -ESRCH;
143 #else
144 return -ENOSYS;
145 #endif
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;
154 int i;
156 if (entry->gsi != newentry->gsi || entry->type != newentry->type) {
157 return -EINVAL;
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) {
163 continue;
165 switch (e->type) {
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);
171 return 0;
173 break;
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);
179 return 0;
181 break;
182 default:
183 break;
186 return -ESRCH;
187 #else
188 return -ENOSYS;
189 #endif
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;
197 int i, bit;
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++) {
202 bit = ffs(~buf[i]);
203 if (!bit) {
204 continue;
207 return bit - 1 + i * 32;
210 return -ENOSPC;
211 #else
212 return -ENOSYS;
213 #endif
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);
227 #endif
229 #if !defined(TARGET_I386)
230 void kvm_arch_init_irq_routing(KVMState *s)
233 #endif