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
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.
18 * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
22 #include <linux/kvm_host.h>
24 #include <asm/msidef.h>
30 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
31 struct kvm *kvm, int level)
34 return kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level);
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)
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);
66 deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
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,
74 r = kvm_apic_set_irq(vcpu, vector, trig_mode);
76 printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
79 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
80 if (!(deliver_bitmask & (1 << vcpu_id)))
82 deliver_bitmask &= ~(1 << vcpu_id);
83 vcpu = ioapic->kvm->vcpus[vcpu_id];
87 r += kvm_apic_set_irq(vcpu, vector, trig_mode);
97 /* This should be called with the kvm->lock mutex held
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;
109 if (irq < KVM_IOAPIC_NUM_PINS) {
110 irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
112 /* Logical OR for level trig interrupt */
114 set_bit(irq_source_id, irq_state);
116 clear_bit(irq_source_id, irq_state);
117 sig_level = !!(*irq_state);
118 } else /* Deal with MSI/MSI-X */
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)
127 int r = e->set(e, kvm, sig_level);
131 ret = r + ((ret < 0) ? 0 : 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;
143 list_for_each_entry(e, &kvm->irq_routing, link)
144 if (e->irqchip.irqchip == irqchip &&
145 e->irqchip.pin == pin) {
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");
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)
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");
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)
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)
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)
244 case KVM_IRQ_ROUTING_IRQCHIP:
246 switch (ue->u.irqchip.irqchip) {
247 case KVM_IRQCHIP_PIC_MASTER:
248 e->set = kvm_set_pic_irq;
250 case KVM_IRQCHIP_PIC_SLAVE:
251 e->set = kvm_set_pic_irq;
254 case KVM_IRQCHIP_IOAPIC:
255 e->set = kvm_set_ioapic_irq;
260 e->irqchip.irqchip = ue->u.irqchip.irqchip;
261 e->irqchip.pin = ue->u.irqchip.pin + delta;
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;
278 int kvm_set_irq_routing(struct kvm *kvm,
279 const struct kvm_irq_routing_entry *ue,
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;
289 for (i = 0; i < nr; ++i) {
291 if (ue->gsi >= KVM_MAX_IRQ_ROUTES)
296 e = kzalloc(sizeof(*e), GFP_KERNEL);
299 r = setup_routing_entry(e, ue);
303 list_add(&e->link, &irq_list);
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);
319 __kvm_free_irq_routing(&irq_list);
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)
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)
335 # define ROUTING_ENTRY2(irq) \
336 IOAPIC_ROUTING_ENTRY(irq)
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),
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),
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);