drm/nouveau/bios: check that fixed tvdac gpio data is valid before using it
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / virt / kvm / irq_comm.c
blob83402d74a767bec214d2c2467cceb3a271a6be44
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>
20 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
23 #include <linux/kvm_host.h>
24 #include <linux/slab.h>
25 #include <trace/events/kvm.h>
27 #include <asm/msidef.h>
28 #ifdef CONFIG_IA64
29 #include <asm/iosapic.h>
30 #endif
32 #include "irq.h"
34 #include "ioapic.h"
36 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
37 struct kvm *kvm, int irq_source_id, int level)
39 #ifdef CONFIG_X86
40 struct kvm_pic *pic = pic_irqchip(kvm);
41 return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level);
42 #else
43 return -1;
44 #endif
47 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
48 struct kvm *kvm, int irq_source_id, int level)
50 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
51 return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level);
54 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
56 #ifdef CONFIG_IA64
57 return irq->delivery_mode ==
58 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
59 #else
60 return irq->delivery_mode == APIC_DM_LOWEST;
61 #endif
64 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
65 struct kvm_lapic_irq *irq)
67 int i, r = -1;
68 struct kvm_vcpu *vcpu, *lowest = NULL;
70 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
71 kvm_is_dm_lowest_prio(irq))
72 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
74 kvm_for_each_vcpu(i, vcpu, kvm) {
75 if (!kvm_apic_present(vcpu))
76 continue;
78 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
79 irq->dest_id, irq->dest_mode))
80 continue;
82 if (!kvm_is_dm_lowest_prio(irq)) {
83 if (r < 0)
84 r = 0;
85 r += kvm_apic_set_irq(vcpu, irq);
86 } else if (kvm_lapic_enabled(vcpu)) {
87 if (!lowest)
88 lowest = vcpu;
89 else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
90 lowest = vcpu;
94 if (lowest)
95 r = kvm_apic_set_irq(lowest, irq);
97 return r;
100 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
101 struct kvm *kvm, int irq_source_id, int level)
103 struct kvm_lapic_irq irq;
105 if (!level)
106 return -1;
108 trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data);
110 irq.dest_id = (e->msi.address_lo &
111 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
112 irq.vector = (e->msi.data &
113 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
114 irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
115 irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
116 irq.delivery_mode = e->msi.data & 0x700;
117 irq.level = 1;
118 irq.shorthand = 0;
120 /* TODO Deal with RH bit of MSI message address */
121 return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
124 int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
126 struct kvm_kernel_irq_routing_entry route;
128 if (!irqchip_in_kernel(kvm) || msi->flags != 0)
129 return -EINVAL;
131 route.msi.address_lo = msi->address_lo;
132 route.msi.address_hi = msi->address_hi;
133 route.msi.data = msi->data;
135 return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1);
139 * Return value:
140 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
141 * = 0 Interrupt was coalesced (previous irq is still pending)
142 * > 0 Number of CPUs interrupt was delivered to
144 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
146 struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
147 int ret = -1, i = 0;
148 struct kvm_irq_routing_table *irq_rt;
149 struct hlist_node *n;
151 trace_kvm_set_irq(irq, level, irq_source_id);
153 /* Not possible to detect if the guest uses the PIC or the
154 * IOAPIC. So set the bit in both. The guest will ignore
155 * writes to the unused one.
157 rcu_read_lock();
158 irq_rt = rcu_dereference(kvm->irq_routing);
159 if (irq < irq_rt->nr_rt_entries)
160 hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
161 irq_set[i++] = *e;
162 rcu_read_unlock();
164 while(i--) {
165 int r;
166 r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level);
167 if (r < 0)
168 continue;
170 ret = r + ((ret < 0) ? 0 : ret);
173 return ret;
176 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
178 struct kvm_irq_ack_notifier *kian;
179 struct hlist_node *n;
180 int gsi;
182 trace_kvm_ack_irq(irqchip, pin);
184 rcu_read_lock();
185 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
186 if (gsi != -1)
187 hlist_for_each_entry_rcu(kian, n, &kvm->irq_ack_notifier_list,
188 link)
189 if (kian->gsi == gsi)
190 kian->irq_acked(kian);
191 rcu_read_unlock();
194 void kvm_register_irq_ack_notifier(struct kvm *kvm,
195 struct kvm_irq_ack_notifier *kian)
197 mutex_lock(&kvm->irq_lock);
198 hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
199 mutex_unlock(&kvm->irq_lock);
202 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
203 struct kvm_irq_ack_notifier *kian)
205 mutex_lock(&kvm->irq_lock);
206 hlist_del_init_rcu(&kian->link);
207 mutex_unlock(&kvm->irq_lock);
208 synchronize_rcu();
211 int kvm_request_irq_source_id(struct kvm *kvm)
213 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
214 int irq_source_id;
216 mutex_lock(&kvm->irq_lock);
217 irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
219 if (irq_source_id >= BITS_PER_LONG) {
220 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
221 irq_source_id = -EFAULT;
222 goto unlock;
225 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
226 set_bit(irq_source_id, bitmap);
227 unlock:
228 mutex_unlock(&kvm->irq_lock);
230 return irq_source_id;
233 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
235 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
237 mutex_lock(&kvm->irq_lock);
238 if (irq_source_id < 0 ||
239 irq_source_id >= BITS_PER_LONG) {
240 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
241 goto unlock;
243 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
244 if (!irqchip_in_kernel(kvm))
245 goto unlock;
247 kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
248 #ifdef CONFIG_X86
249 kvm_pic_clear_all(pic_irqchip(kvm), irq_source_id);
250 #endif
251 unlock:
252 mutex_unlock(&kvm->irq_lock);
255 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
256 struct kvm_irq_mask_notifier *kimn)
258 mutex_lock(&kvm->irq_lock);
259 kimn->irq = irq;
260 hlist_add_head_rcu(&kimn->link, &kvm->mask_notifier_list);
261 mutex_unlock(&kvm->irq_lock);
264 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
265 struct kvm_irq_mask_notifier *kimn)
267 mutex_lock(&kvm->irq_lock);
268 hlist_del_rcu(&kimn->link);
269 mutex_unlock(&kvm->irq_lock);
270 synchronize_rcu();
273 void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
274 bool mask)
276 struct kvm_irq_mask_notifier *kimn;
277 struct hlist_node *n;
278 int gsi;
280 rcu_read_lock();
281 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
282 if (gsi != -1)
283 hlist_for_each_entry_rcu(kimn, n, &kvm->mask_notifier_list, link)
284 if (kimn->irq == gsi)
285 kimn->func(kimn, mask);
286 rcu_read_unlock();
289 void kvm_free_irq_routing(struct kvm *kvm)
291 /* Called only during vm destruction. Nobody can use the pointer
292 at this stage */
293 kfree(kvm->irq_routing);
296 static int setup_routing_entry(struct kvm_irq_routing_table *rt,
297 struct kvm_kernel_irq_routing_entry *e,
298 const struct kvm_irq_routing_entry *ue)
300 int r = -EINVAL;
301 int delta;
302 unsigned max_pin;
303 struct kvm_kernel_irq_routing_entry *ei;
304 struct hlist_node *n;
307 * Do not allow GSI to be mapped to the same irqchip more than once.
308 * Allow only one to one mapping between GSI and MSI.
310 hlist_for_each_entry(ei, n, &rt->map[ue->gsi], link)
311 if (ei->type == KVM_IRQ_ROUTING_MSI ||
312 ue->type == KVM_IRQ_ROUTING_MSI ||
313 ue->u.irqchip.irqchip == ei->irqchip.irqchip)
314 return r;
316 e->gsi = ue->gsi;
317 e->type = ue->type;
318 switch (ue->type) {
319 case KVM_IRQ_ROUTING_IRQCHIP:
320 delta = 0;
321 switch (ue->u.irqchip.irqchip) {
322 case KVM_IRQCHIP_PIC_MASTER:
323 e->set = kvm_set_pic_irq;
324 max_pin = 16;
325 break;
326 case KVM_IRQCHIP_PIC_SLAVE:
327 e->set = kvm_set_pic_irq;
328 max_pin = 16;
329 delta = 8;
330 break;
331 case KVM_IRQCHIP_IOAPIC:
332 max_pin = KVM_IOAPIC_NUM_PINS;
333 e->set = kvm_set_ioapic_irq;
334 break;
335 default:
336 goto out;
338 e->irqchip.irqchip = ue->u.irqchip.irqchip;
339 e->irqchip.pin = ue->u.irqchip.pin + delta;
340 if (e->irqchip.pin >= max_pin)
341 goto out;
342 rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi;
343 break;
344 case KVM_IRQ_ROUTING_MSI:
345 e->set = kvm_set_msi;
346 e->msi.address_lo = ue->u.msi.address_lo;
347 e->msi.address_hi = ue->u.msi.address_hi;
348 e->msi.data = ue->u.msi.data;
349 break;
350 default:
351 goto out;
354 hlist_add_head(&e->link, &rt->map[e->gsi]);
355 r = 0;
356 out:
357 return r;
361 int kvm_set_irq_routing(struct kvm *kvm,
362 const struct kvm_irq_routing_entry *ue,
363 unsigned nr,
364 unsigned flags)
366 struct kvm_irq_routing_table *new, *old;
367 u32 i, j, nr_rt_entries = 0;
368 int r;
370 for (i = 0; i < nr; ++i) {
371 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
372 return -EINVAL;
373 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
376 nr_rt_entries += 1;
378 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
379 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
380 GFP_KERNEL);
382 if (!new)
383 return -ENOMEM;
385 new->rt_entries = (void *)&new->map[nr_rt_entries];
387 new->nr_rt_entries = nr_rt_entries;
388 for (i = 0; i < 3; i++)
389 for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++)
390 new->chip[i][j] = -1;
392 for (i = 0; i < nr; ++i) {
393 r = -EINVAL;
394 if (ue->flags)
395 goto out;
396 r = setup_routing_entry(new, &new->rt_entries[i], ue);
397 if (r)
398 goto out;
399 ++ue;
402 mutex_lock(&kvm->irq_lock);
403 old = kvm->irq_routing;
404 kvm_irq_routing_update(kvm, new);
405 mutex_unlock(&kvm->irq_lock);
407 synchronize_rcu();
409 new = old;
410 r = 0;
412 out:
413 kfree(new);
414 return r;
417 #define IOAPIC_ROUTING_ENTRY(irq) \
418 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
419 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
420 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
422 #ifdef CONFIG_X86
423 # define PIC_ROUTING_ENTRY(irq) \
424 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
425 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
426 # define ROUTING_ENTRY2(irq) \
427 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
428 #else
429 # define ROUTING_ENTRY2(irq) \
430 IOAPIC_ROUTING_ENTRY(irq)
431 #endif
433 static const struct kvm_irq_routing_entry default_routing[] = {
434 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
435 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
436 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
437 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
438 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
439 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
440 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
441 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
442 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
443 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
444 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
445 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
446 #ifdef CONFIG_IA64
447 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
448 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
449 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
450 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
451 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
452 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
453 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
454 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
455 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
456 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
457 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
458 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
459 #endif
462 int kvm_setup_default_irq_routing(struct kvm *kvm)
464 return kvm_set_irq_routing(kvm, default_routing,
465 ARRAY_SIZE(default_routing), 0);