x86: 46 bit physical address support on 64 bits
[linux-2.6.git] / virt / kvm / irq_comm.c
blob864ac5483baade3fce7085c3c51f23e493426914
1 /*
2 * irq_comm.c: Common API for in kernel interrupt controller
3 * Copyright (c) 2007, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
17 * Authors:
18 * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
22 #include <linux/kvm_host.h>
24 #include <asm/msidef.h>
26 #include "irq.h"
28 #include "ioapic.h"
30 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
31 struct kvm *kvm, int level)
33 #ifdef CONFIG_X86
34 return kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level);
35 #else
36 return -1;
37 #endif
40 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
41 struct kvm *kvm, int level)
43 return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
46 static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
47 struct kvm *kvm, int level)
49 int vcpu_id, r = -1;
50 struct kvm_vcpu *vcpu;
51 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
52 int dest_id = (e->msi.address_lo & MSI_ADDR_DEST_ID_MASK)
53 >> MSI_ADDR_DEST_ID_SHIFT;
54 int vector = (e->msi.data & MSI_DATA_VECTOR_MASK)
55 >> MSI_DATA_VECTOR_SHIFT;
56 int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
57 (unsigned long *)&e->msi.address_lo);
58 int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
59 (unsigned long *)&e->msi.data);
60 int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
61 (unsigned long *)&e->msi.data);
62 u32 deliver_bitmask;
64 BUG_ON(!ioapic);
66 deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
67 dest_id, dest_mode);
68 /* IOAPIC delivery mode value is the same as MSI here */
69 switch (delivery_mode) {
70 case IOAPIC_LOWEST_PRIORITY:
71 vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
72 deliver_bitmask);
73 if (vcpu != NULL)
74 r = kvm_apic_set_irq(vcpu, vector, trig_mode);
75 else
76 printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
77 break;
78 case IOAPIC_FIXED:
79 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
80 if (!(deliver_bitmask & (1 << vcpu_id)))
81 continue;
82 deliver_bitmask &= ~(1 << vcpu_id);
83 vcpu = ioapic->kvm->vcpus[vcpu_id];
84 if (vcpu) {
85 if (r < 0)
86 r = 0;
87 r += kvm_apic_set_irq(vcpu, vector, trig_mode);
90 break;
91 default:
92 break;
94 return r;
97 /* This should be called with the kvm->lock mutex held
98 * Return value:
99 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
100 * = 0 Interrupt was coalesced (previous irq is still pending)
101 * > 0 Number of CPUs interrupt was delivered to
103 int kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
105 struct kvm_kernel_irq_routing_entry *e;
106 unsigned long *irq_state, sig_level;
107 int ret = -1;
109 if (irq < KVM_IOAPIC_NUM_PINS) {
110 irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
112 /* Logical OR for level trig interrupt */
113 if (level)
114 set_bit(irq_source_id, irq_state);
115 else
116 clear_bit(irq_source_id, irq_state);
117 sig_level = !!(*irq_state);
118 } else /* Deal with MSI/MSI-X */
119 sig_level = 1;
121 /* Not possible to detect if the guest uses the PIC or the
122 * IOAPIC. So set the bit in both. The guest will ignore
123 * writes to the unused one.
125 list_for_each_entry(e, &kvm->irq_routing, link)
126 if (e->gsi == irq) {
127 int r = e->set(e, kvm, sig_level);
128 if (r < 0)
129 continue;
131 ret = r + ((ret < 0) ? 0 : ret);
133 return ret;
136 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
138 struct kvm_kernel_irq_routing_entry *e;
139 struct kvm_irq_ack_notifier *kian;
140 struct hlist_node *n;
141 unsigned gsi = pin;
143 list_for_each_entry(e, &kvm->irq_routing, link)
144 if (e->irqchip.irqchip == irqchip &&
145 e->irqchip.pin == pin) {
146 gsi = e->gsi;
147 break;
150 hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
151 if (kian->gsi == gsi)
152 kian->irq_acked(kian);
155 void kvm_register_irq_ack_notifier(struct kvm *kvm,
156 struct kvm_irq_ack_notifier *kian)
158 hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
161 void kvm_unregister_irq_ack_notifier(struct kvm_irq_ack_notifier *kian)
163 hlist_del_init(&kian->link);
166 /* The caller must hold kvm->lock mutex */
167 int kvm_request_irq_source_id(struct kvm *kvm)
169 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
170 int irq_source_id = find_first_zero_bit(bitmap,
171 sizeof(kvm->arch.irq_sources_bitmap));
173 if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
174 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
175 return -EFAULT;
178 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
179 set_bit(irq_source_id, bitmap);
181 return irq_source_id;
184 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
186 int i;
188 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
190 if (irq_source_id < 0 ||
191 irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
192 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
193 return;
195 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
196 clear_bit(irq_source_id, &kvm->arch.irq_states[i]);
197 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
200 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
201 struct kvm_irq_mask_notifier *kimn)
203 kimn->irq = irq;
204 hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
207 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
208 struct kvm_irq_mask_notifier *kimn)
210 hlist_del(&kimn->link);
213 void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
215 struct kvm_irq_mask_notifier *kimn;
216 struct hlist_node *n;
218 hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
219 if (kimn->irq == irq)
220 kimn->func(kimn, mask);
223 static void __kvm_free_irq_routing(struct list_head *irq_routing)
225 struct kvm_kernel_irq_routing_entry *e, *n;
227 list_for_each_entry_safe(e, n, irq_routing, link)
228 kfree(e);
231 void kvm_free_irq_routing(struct kvm *kvm)
233 __kvm_free_irq_routing(&kvm->irq_routing);
236 static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
237 const struct kvm_irq_routing_entry *ue)
239 int r = -EINVAL;
240 int delta;
242 e->gsi = ue->gsi;
243 switch (ue->type) {
244 case KVM_IRQ_ROUTING_IRQCHIP:
245 delta = 0;
246 switch (ue->u.irqchip.irqchip) {
247 case KVM_IRQCHIP_PIC_MASTER:
248 e->set = kvm_set_pic_irq;
249 break;
250 case KVM_IRQCHIP_PIC_SLAVE:
251 e->set = kvm_set_pic_irq;
252 delta = 8;
253 break;
254 case KVM_IRQCHIP_IOAPIC:
255 e->set = kvm_set_ioapic_irq;
256 break;
257 default:
258 goto out;
260 e->irqchip.irqchip = ue->u.irqchip.irqchip;
261 e->irqchip.pin = ue->u.irqchip.pin + delta;
262 break;
263 case KVM_IRQ_ROUTING_MSI:
264 e->set = kvm_set_msi;
265 e->msi.address_lo = ue->u.msi.address_lo;
266 e->msi.address_hi = ue->u.msi.address_hi;
267 e->msi.data = ue->u.msi.data;
268 break;
269 default:
270 goto out;
272 r = 0;
273 out:
274 return r;
278 int kvm_set_irq_routing(struct kvm *kvm,
279 const struct kvm_irq_routing_entry *ue,
280 unsigned nr,
281 unsigned flags)
283 struct list_head irq_list = LIST_HEAD_INIT(irq_list);
284 struct list_head tmp = LIST_HEAD_INIT(tmp);
285 struct kvm_kernel_irq_routing_entry *e = NULL;
286 unsigned i;
287 int r;
289 for (i = 0; i < nr; ++i) {
290 r = -EINVAL;
291 if (ue->gsi >= KVM_MAX_IRQ_ROUTES)
292 goto out;
293 if (ue->flags)
294 goto out;
295 r = -ENOMEM;
296 e = kzalloc(sizeof(*e), GFP_KERNEL);
297 if (!e)
298 goto out;
299 r = setup_routing_entry(e, ue);
300 if (r)
301 goto out;
302 ++ue;
303 list_add(&e->link, &irq_list);
304 e = NULL;
307 mutex_lock(&kvm->lock);
308 list_splice(&kvm->irq_routing, &tmp);
309 INIT_LIST_HEAD(&kvm->irq_routing);
310 list_splice(&irq_list, &kvm->irq_routing);
311 INIT_LIST_HEAD(&irq_list);
312 list_splice(&tmp, &irq_list);
313 mutex_unlock(&kvm->lock);
315 r = 0;
317 out:
318 kfree(e);
319 __kvm_free_irq_routing(&irq_list);
320 return r;
323 #define IOAPIC_ROUTING_ENTRY(irq) \
324 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
325 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
326 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
328 #ifdef CONFIG_X86
329 # define PIC_ROUTING_ENTRY(irq) \
330 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
331 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
332 # define ROUTING_ENTRY2(irq) \
333 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
334 #else
335 # define ROUTING_ENTRY2(irq) \
336 IOAPIC_ROUTING_ENTRY(irq)
337 #endif
339 static const struct kvm_irq_routing_entry default_routing[] = {
340 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
341 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
342 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
343 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
344 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
345 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
346 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
347 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
348 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
349 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
350 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
351 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
352 #ifdef CONFIG_IA64
353 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
354 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
355 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
356 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
357 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
358 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
359 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
360 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
361 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
362 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
363 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
364 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
365 #endif
368 int kvm_setup_default_irq_routing(struct kvm *kvm)
370 return kvm_set_irq_routing(kvm, default_routing,
371 ARRAY_SIZE(default_routing), 0);