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>
26 #include <asm/iosapic.h>
33 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
34 struct kvm *kvm, int level)
37 return kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level);
43 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
44 struct kvm *kvm, int level)
46 return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
49 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
52 return irq->delivery_mode ==
53 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
55 return irq->delivery_mode == APIC_DM_LOWEST;
59 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
60 struct kvm_lapic_irq *irq)
63 struct kvm_vcpu *vcpu, *lowest = NULL;
65 WARN_ON(!mutex_is_locked(&kvm->irq_lock));
67 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
68 kvm_is_dm_lowest_prio(irq))
69 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
71 for (i = 0; i < KVM_MAX_VCPUS; i++) {
74 if (!vcpu || !kvm_apic_present(vcpu))
77 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
78 irq->dest_id, irq->dest_mode))
81 if (!kvm_is_dm_lowest_prio(irq)) {
84 r += kvm_apic_set_irq(vcpu, irq);
88 else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
94 r = kvm_apic_set_irq(lowest, irq);
99 static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
100 struct kvm *kvm, int level)
102 struct kvm_lapic_irq irq;
104 irq.dest_id = (e->msi.address_lo &
105 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
106 irq.vector = (e->msi.data &
107 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
108 irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
109 irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
110 irq.delivery_mode = e->msi.data & 0x700;
114 /* TODO Deal with RH bit of MSI message address */
115 return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
118 /* This should be called with the kvm->irq_lock mutex held
120 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
121 * = 0 Interrupt was coalesced (previous irq is still pending)
122 * > 0 Number of CPUs interrupt was delivered to
124 int kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
126 struct kvm_kernel_irq_routing_entry *e;
127 unsigned long *irq_state, sig_level;
130 WARN_ON(!mutex_is_locked(&kvm->irq_lock));
132 if (irq < KVM_IOAPIC_NUM_PINS) {
133 irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
135 /* Logical OR for level trig interrupt */
137 set_bit(irq_source_id, irq_state);
139 clear_bit(irq_source_id, irq_state);
140 sig_level = !!(*irq_state);
141 } else /* Deal with MSI/MSI-X */
144 /* Not possible to detect if the guest uses the PIC or the
145 * IOAPIC. So set the bit in both. The guest will ignore
146 * writes to the unused one.
148 list_for_each_entry(e, &kvm->irq_routing, link)
150 int r = e->set(e, kvm, sig_level);
154 ret = r + ((ret < 0) ? 0 : ret);
159 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
161 struct kvm_kernel_irq_routing_entry *e;
162 struct kvm_irq_ack_notifier *kian;
163 struct hlist_node *n;
166 list_for_each_entry(e, &kvm->irq_routing, link)
167 if (e->type == KVM_IRQ_ROUTING_IRQCHIP &&
168 e->irqchip.irqchip == irqchip &&
169 e->irqchip.pin == pin) {
174 hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
175 if (kian->gsi == gsi)
176 kian->irq_acked(kian);
179 void kvm_register_irq_ack_notifier(struct kvm *kvm,
180 struct kvm_irq_ack_notifier *kian)
182 mutex_lock(&kvm->irq_lock);
183 hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
184 mutex_unlock(&kvm->irq_lock);
187 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
188 struct kvm_irq_ack_notifier *kian)
190 mutex_lock(&kvm->irq_lock);
191 hlist_del_init(&kian->link);
192 mutex_unlock(&kvm->irq_lock);
195 int kvm_request_irq_source_id(struct kvm *kvm)
197 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
200 mutex_lock(&kvm->irq_lock);
201 irq_source_id = find_first_zero_bit(bitmap,
202 sizeof(kvm->arch.irq_sources_bitmap));
204 if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
205 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
209 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
210 set_bit(irq_source_id, bitmap);
211 mutex_unlock(&kvm->irq_lock);
213 return irq_source_id;
216 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
220 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
222 mutex_lock(&kvm->irq_lock);
223 if (irq_source_id < 0 ||
224 irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
225 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
228 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
229 clear_bit(irq_source_id, &kvm->arch.irq_states[i]);
230 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
231 mutex_unlock(&kvm->irq_lock);
234 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
235 struct kvm_irq_mask_notifier *kimn)
237 mutex_lock(&kvm->irq_lock);
239 hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
240 mutex_unlock(&kvm->irq_lock);
243 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
244 struct kvm_irq_mask_notifier *kimn)
246 mutex_lock(&kvm->irq_lock);
247 hlist_del(&kimn->link);
248 mutex_unlock(&kvm->irq_lock);
251 void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
253 struct kvm_irq_mask_notifier *kimn;
254 struct hlist_node *n;
256 WARN_ON(!mutex_is_locked(&kvm->irq_lock));
258 hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
259 if (kimn->irq == irq)
260 kimn->func(kimn, mask);
263 static void __kvm_free_irq_routing(struct list_head *irq_routing)
265 struct kvm_kernel_irq_routing_entry *e, *n;
267 list_for_each_entry_safe(e, n, irq_routing, link)
271 void kvm_free_irq_routing(struct kvm *kvm)
273 mutex_lock(&kvm->irq_lock);
274 __kvm_free_irq_routing(&kvm->irq_routing);
275 mutex_unlock(&kvm->irq_lock);
278 static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
279 const struct kvm_irq_routing_entry *ue)
287 case KVM_IRQ_ROUTING_IRQCHIP:
289 switch (ue->u.irqchip.irqchip) {
290 case KVM_IRQCHIP_PIC_MASTER:
291 e->set = kvm_set_pic_irq;
293 case KVM_IRQCHIP_PIC_SLAVE:
294 e->set = kvm_set_pic_irq;
297 case KVM_IRQCHIP_IOAPIC:
298 e->set = kvm_set_ioapic_irq;
303 e->irqchip.irqchip = ue->u.irqchip.irqchip;
304 e->irqchip.pin = ue->u.irqchip.pin + delta;
306 case KVM_IRQ_ROUTING_MSI:
307 e->set = kvm_set_msi;
308 e->msi.address_lo = ue->u.msi.address_lo;
309 e->msi.address_hi = ue->u.msi.address_hi;
310 e->msi.data = ue->u.msi.data;
321 int kvm_set_irq_routing(struct kvm *kvm,
322 const struct kvm_irq_routing_entry *ue,
326 struct list_head irq_list = LIST_HEAD_INIT(irq_list);
327 struct list_head tmp = LIST_HEAD_INIT(tmp);
328 struct kvm_kernel_irq_routing_entry *e = NULL;
332 for (i = 0; i < nr; ++i) {
334 if (ue->gsi >= KVM_MAX_IRQ_ROUTES)
339 e = kzalloc(sizeof(*e), GFP_KERNEL);
342 r = setup_routing_entry(e, ue);
346 list_add(&e->link, &irq_list);
350 mutex_lock(&kvm->irq_lock);
351 list_splice(&kvm->irq_routing, &tmp);
352 INIT_LIST_HEAD(&kvm->irq_routing);
353 list_splice(&irq_list, &kvm->irq_routing);
354 INIT_LIST_HEAD(&irq_list);
355 list_splice(&tmp, &irq_list);
356 mutex_unlock(&kvm->irq_lock);
362 __kvm_free_irq_routing(&irq_list);
366 #define IOAPIC_ROUTING_ENTRY(irq) \
367 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
368 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
369 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
372 # define PIC_ROUTING_ENTRY(irq) \
373 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
374 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
375 # define ROUTING_ENTRY2(irq) \
376 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
378 # define ROUTING_ENTRY2(irq) \
379 IOAPIC_ROUTING_ENTRY(irq)
382 static const struct kvm_irq_routing_entry default_routing[] = {
383 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
384 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
385 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
386 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
387 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
388 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
389 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
390 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
391 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
392 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
393 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
394 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
396 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
397 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
398 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
399 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
400 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
401 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
402 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
403 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
404 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
405 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
406 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
407 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
411 int kvm_setup_default_irq_routing(struct kvm *kvm)
413 return kvm_set_irq_routing(kvm, default_routing,
414 ARRAY_SIZE(default_routing), 0);