l2tp: Add ppp device name to L2TP ppp session data
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / virt / kvm / irq_comm.c
blob9fd5b3ebc5175b193e083e45d594dcf34c4e04ab
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>
23 #include <trace/events/kvm.h>
25 #include <asm/msidef.h>
26 #ifdef CONFIG_IA64
27 #include <asm/iosapic.h>
28 #endif
30 #include "irq.h"
32 #include "ioapic.h"
34 static inline int kvm_irq_line_state(unsigned long *irq_state,
35 int irq_source_id, int level)
37 /* Logical OR for level trig interrupt */
38 if (level)
39 set_bit(irq_source_id, irq_state);
40 else
41 clear_bit(irq_source_id, irq_state);
43 return !!(*irq_state);
46 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
47 struct kvm *kvm, int irq_source_id, int level)
49 #ifdef CONFIG_X86
50 struct kvm_pic *pic = pic_irqchip(kvm);
51 level = kvm_irq_line_state(&pic->irq_states[e->irqchip.pin],
52 irq_source_id, level);
53 return kvm_pic_set_irq(pic, e->irqchip.pin, level);
54 #else
55 return -1;
56 #endif
59 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
60 struct kvm *kvm, int irq_source_id, int level)
62 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
63 level = kvm_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
64 irq_source_id, level);
66 return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
69 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
71 #ifdef CONFIG_IA64
72 return irq->delivery_mode ==
73 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
74 #else
75 return irq->delivery_mode == APIC_DM_LOWEST;
76 #endif
79 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
80 struct kvm_lapic_irq *irq)
82 int i, r = -1;
83 struct kvm_vcpu *vcpu, *lowest = NULL;
85 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
86 kvm_is_dm_lowest_prio(irq))
87 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
89 kvm_for_each_vcpu(i, vcpu, kvm) {
90 if (!kvm_apic_present(vcpu))
91 continue;
93 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
94 irq->dest_id, irq->dest_mode))
95 continue;
97 if (!kvm_is_dm_lowest_prio(irq)) {
98 if (r < 0)
99 r = 0;
100 r += kvm_apic_set_irq(vcpu, irq);
101 } else {
102 if (!lowest)
103 lowest = vcpu;
104 else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
105 lowest = vcpu;
109 if (lowest)
110 r = kvm_apic_set_irq(lowest, irq);
112 return r;
115 static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
116 struct kvm *kvm, int irq_source_id, int level)
118 struct kvm_lapic_irq irq;
120 if (!level)
121 return -1;
123 trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data);
125 irq.dest_id = (e->msi.address_lo &
126 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
127 irq.vector = (e->msi.data &
128 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
129 irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
130 irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
131 irq.delivery_mode = e->msi.data & 0x700;
132 irq.level = 1;
133 irq.shorthand = 0;
135 /* TODO Deal with RH bit of MSI message address */
136 return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
140 * Return value:
141 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
142 * = 0 Interrupt was coalesced (previous irq is still pending)
143 * > 0 Number of CPUs interrupt was delivered to
145 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
147 struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
148 int ret = -1, i = 0;
149 struct kvm_irq_routing_table *irq_rt;
150 struct hlist_node *n;
152 trace_kvm_set_irq(irq, level, irq_source_id);
154 /* Not possible to detect if the guest uses the PIC or the
155 * IOAPIC. So set the bit in both. The guest will ignore
156 * writes to the unused one.
158 rcu_read_lock();
159 irq_rt = rcu_dereference(kvm->irq_routing);
160 if (irq < irq_rt->nr_rt_entries)
161 hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
162 irq_set[i++] = *e;
163 rcu_read_unlock();
165 while(i--) {
166 int r;
167 r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level);
168 if (r < 0)
169 continue;
171 ret = r + ((ret < 0) ? 0 : ret);
174 return ret;
177 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
179 struct kvm_irq_ack_notifier *kian;
180 struct hlist_node *n;
181 int gsi;
183 trace_kvm_ack_irq(irqchip, pin);
185 rcu_read_lock();
186 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
187 if (gsi != -1)
188 hlist_for_each_entry_rcu(kian, n, &kvm->irq_ack_notifier_list,
189 link)
190 if (kian->gsi == gsi)
191 kian->irq_acked(kian);
192 rcu_read_unlock();
195 void kvm_register_irq_ack_notifier(struct kvm *kvm,
196 struct kvm_irq_ack_notifier *kian)
198 mutex_lock(&kvm->irq_lock);
199 hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
200 mutex_unlock(&kvm->irq_lock);
203 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
204 struct kvm_irq_ack_notifier *kian)
206 mutex_lock(&kvm->irq_lock);
207 hlist_del_init_rcu(&kian->link);
208 mutex_unlock(&kvm->irq_lock);
209 synchronize_rcu();
212 int kvm_request_irq_source_id(struct kvm *kvm)
214 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
215 int irq_source_id;
217 mutex_lock(&kvm->irq_lock);
218 irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
220 if (irq_source_id >= BITS_PER_LONG) {
221 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
222 irq_source_id = -EFAULT;
223 goto unlock;
226 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
227 set_bit(irq_source_id, bitmap);
228 unlock:
229 mutex_unlock(&kvm->irq_lock);
231 return irq_source_id;
234 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
236 int i;
238 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
240 mutex_lock(&kvm->irq_lock);
241 if (irq_source_id < 0 ||
242 irq_source_id >= BITS_PER_LONG) {
243 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
244 goto unlock;
246 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
247 if (!irqchip_in_kernel(kvm))
248 goto unlock;
250 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++) {
251 clear_bit(irq_source_id, &kvm->arch.vioapic->irq_states[i]);
252 if (i >= 16)
253 continue;
254 #ifdef CONFIG_X86
255 clear_bit(irq_source_id, &pic_irqchip(kvm)->irq_states[i]);
256 #endif
258 unlock:
259 mutex_unlock(&kvm->irq_lock);
262 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
263 struct kvm_irq_mask_notifier *kimn)
265 mutex_lock(&kvm->irq_lock);
266 kimn->irq = irq;
267 hlist_add_head_rcu(&kimn->link, &kvm->mask_notifier_list);
268 mutex_unlock(&kvm->irq_lock);
271 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
272 struct kvm_irq_mask_notifier *kimn)
274 mutex_lock(&kvm->irq_lock);
275 hlist_del_rcu(&kimn->link);
276 mutex_unlock(&kvm->irq_lock);
277 synchronize_rcu();
280 void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
282 struct kvm_irq_mask_notifier *kimn;
283 struct hlist_node *n;
285 rcu_read_lock();
286 hlist_for_each_entry_rcu(kimn, n, &kvm->mask_notifier_list, link)
287 if (kimn->irq == irq)
288 kimn->func(kimn, mask);
289 rcu_read_unlock();
292 void kvm_free_irq_routing(struct kvm *kvm)
294 /* Called only during vm destruction. Nobody can use the pointer
295 at this stage */
296 kfree(kvm->irq_routing);
299 static int setup_routing_entry(struct kvm_irq_routing_table *rt,
300 struct kvm_kernel_irq_routing_entry *e,
301 const struct kvm_irq_routing_entry *ue)
303 int r = -EINVAL;
304 int delta;
305 unsigned max_pin;
306 struct kvm_kernel_irq_routing_entry *ei;
307 struct hlist_node *n;
310 * Do not allow GSI to be mapped to the same irqchip more than once.
311 * Allow only one to one mapping between GSI and MSI.
313 hlist_for_each_entry(ei, n, &rt->map[ue->gsi], link)
314 if (ei->type == KVM_IRQ_ROUTING_MSI ||
315 ue->u.irqchip.irqchip == ei->irqchip.irqchip)
316 return r;
318 e->gsi = ue->gsi;
319 e->type = ue->type;
320 switch (ue->type) {
321 case KVM_IRQ_ROUTING_IRQCHIP:
322 delta = 0;
323 switch (ue->u.irqchip.irqchip) {
324 case KVM_IRQCHIP_PIC_MASTER:
325 e->set = kvm_set_pic_irq;
326 max_pin = 16;
327 break;
328 case KVM_IRQCHIP_PIC_SLAVE:
329 e->set = kvm_set_pic_irq;
330 max_pin = 16;
331 delta = 8;
332 break;
333 case KVM_IRQCHIP_IOAPIC:
334 max_pin = KVM_IOAPIC_NUM_PINS;
335 e->set = kvm_set_ioapic_irq;
336 break;
337 default:
338 goto out;
340 e->irqchip.irqchip = ue->u.irqchip.irqchip;
341 e->irqchip.pin = ue->u.irqchip.pin + delta;
342 if (e->irqchip.pin >= max_pin)
343 goto out;
344 rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi;
345 break;
346 case KVM_IRQ_ROUTING_MSI:
347 e->set = kvm_set_msi;
348 e->msi.address_lo = ue->u.msi.address_lo;
349 e->msi.address_hi = ue->u.msi.address_hi;
350 e->msi.data = ue->u.msi.data;
351 break;
352 default:
353 goto out;
356 hlist_add_head(&e->link, &rt->map[e->gsi]);
357 r = 0;
358 out:
359 return r;
363 int kvm_set_irq_routing(struct kvm *kvm,
364 const struct kvm_irq_routing_entry *ue,
365 unsigned nr,
366 unsigned flags)
368 struct kvm_irq_routing_table *new, *old;
369 u32 i, j, nr_rt_entries = 0;
370 int r;
372 for (i = 0; i < nr; ++i) {
373 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
374 return -EINVAL;
375 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
378 nr_rt_entries += 1;
380 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
381 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
382 GFP_KERNEL);
384 if (!new)
385 return -ENOMEM;
387 new->rt_entries = (void *)&new->map[nr_rt_entries];
389 new->nr_rt_entries = nr_rt_entries;
390 for (i = 0; i < 3; i++)
391 for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++)
392 new->chip[i][j] = -1;
394 for (i = 0; i < nr; ++i) {
395 r = -EINVAL;
396 if (ue->flags)
397 goto out;
398 r = setup_routing_entry(new, &new->rt_entries[i], ue);
399 if (r)
400 goto out;
401 ++ue;
404 mutex_lock(&kvm->irq_lock);
405 old = kvm->irq_routing;
406 rcu_assign_pointer(kvm->irq_routing, new);
407 mutex_unlock(&kvm->irq_lock);
408 synchronize_rcu();
410 new = old;
411 r = 0;
413 out:
414 kfree(new);
415 return r;
418 #define IOAPIC_ROUTING_ENTRY(irq) \
419 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
420 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
421 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
423 #ifdef CONFIG_X86
424 # define PIC_ROUTING_ENTRY(irq) \
425 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
426 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
427 # define ROUTING_ENTRY2(irq) \
428 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
429 #else
430 # define ROUTING_ENTRY2(irq) \
431 IOAPIC_ROUTING_ENTRY(irq)
432 #endif
434 static const struct kvm_irq_routing_entry default_routing[] = {
435 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
436 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
437 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
438 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
439 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
440 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
441 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
442 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
443 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
444 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
445 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
446 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
447 #ifdef CONFIG_IA64
448 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
449 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
450 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
451 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
452 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
453 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
454 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
455 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
456 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
457 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
458 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
459 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
460 #endif
463 int kvm_setup_default_irq_routing(struct kvm *kvm)
465 return kvm_set_irq_routing(kvm, default_routing,
466 ARRAY_SIZE(default_routing), 0);