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>
23 #include <trace/events/kvm.h>
25 #include <asm/msidef.h>
27 #include <asm/iosapic.h>
34 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry
*e
,
35 struct kvm
*kvm
, int level
)
38 return kvm_pic_set_irq(pic_irqchip(kvm
), e
->irqchip
.pin
, level
);
44 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry
*e
,
45 struct kvm
*kvm
, int level
)
47 return kvm_ioapic_set_irq(kvm
->arch
.vioapic
, e
->irqchip
.pin
, level
);
50 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq
*irq
)
53 return irq
->delivery_mode
==
54 (IOSAPIC_LOWEST_PRIORITY
<< IOSAPIC_DELIVERY_SHIFT
);
56 return irq
->delivery_mode
== APIC_DM_LOWEST
;
60 int kvm_irq_delivery_to_apic(struct kvm
*kvm
, struct kvm_lapic
*src
,
61 struct kvm_lapic_irq
*irq
)
64 struct kvm_vcpu
*vcpu
, *lowest
= NULL
;
66 WARN_ON(!mutex_is_locked(&kvm
->irq_lock
));
68 if (irq
->dest_mode
== 0 && irq
->dest_id
== 0xff &&
69 kvm_is_dm_lowest_prio(irq
))
70 printk(KERN_INFO
"kvm: apic: phys broadcast and lowest prio\n");
72 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
73 if (!kvm_apic_present(vcpu
))
76 if (!kvm_apic_match_dest(vcpu
, src
, irq
->shorthand
,
77 irq
->dest_id
, irq
->dest_mode
))
80 if (!kvm_is_dm_lowest_prio(irq
)) {
83 r
+= kvm_apic_set_irq(vcpu
, irq
);
87 else if (kvm_apic_compare_prio(vcpu
, lowest
) < 0)
93 r
= kvm_apic_set_irq(lowest
, irq
);
98 static int kvm_set_msi(struct kvm_kernel_irq_routing_entry
*e
,
99 struct kvm
*kvm
, int level
)
101 struct kvm_lapic_irq irq
;
103 trace_kvm_msi_set_irq(e
->msi
.address_lo
, e
->msi
.data
);
105 irq
.dest_id
= (e
->msi
.address_lo
&
106 MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
107 irq
.vector
= (e
->msi
.data
&
108 MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
109 irq
.dest_mode
= (1 << MSI_ADDR_DEST_MODE_SHIFT
) & e
->msi
.address_lo
;
110 irq
.trig_mode
= (1 << MSI_DATA_TRIGGER_SHIFT
) & e
->msi
.data
;
111 irq
.delivery_mode
= e
->msi
.data
& 0x700;
115 /* TODO Deal with RH bit of MSI message address */
116 return kvm_irq_delivery_to_apic(kvm
, NULL
, &irq
);
119 /* This should be called with the kvm->irq_lock mutex held
121 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
122 * = 0 Interrupt was coalesced (previous irq is still pending)
123 * > 0 Number of CPUs interrupt was delivered to
125 int kvm_set_irq(struct kvm
*kvm
, int irq_source_id
, int irq
, int level
)
127 struct kvm_kernel_irq_routing_entry
*e
;
128 unsigned long *irq_state
, sig_level
;
131 trace_kvm_set_irq(irq
, level
, irq_source_id
);
133 WARN_ON(!mutex_is_locked(&kvm
->irq_lock
));
135 if (irq
< KVM_IOAPIC_NUM_PINS
) {
136 irq_state
= (unsigned long *)&kvm
->arch
.irq_states
[irq
];
138 /* Logical OR for level trig interrupt */
140 set_bit(irq_source_id
, irq_state
);
142 clear_bit(irq_source_id
, irq_state
);
143 sig_level
= !!(*irq_state
);
146 else /* Deal with MSI/MSI-X */
149 /* Not possible to detect if the guest uses the PIC or the
150 * IOAPIC. So set the bit in both. The guest will ignore
151 * writes to the unused one.
153 list_for_each_entry(e
, &kvm
->irq_routing
, link
)
155 int r
= e
->set(e
, kvm
, sig_level
);
159 ret
= r
+ ((ret
< 0) ? 0 : ret
);
164 void kvm_notify_acked_irq(struct kvm
*kvm
, unsigned irqchip
, unsigned pin
)
166 struct kvm_kernel_irq_routing_entry
*e
;
167 struct kvm_irq_ack_notifier
*kian
;
168 struct hlist_node
*n
;
171 trace_kvm_ack_irq(irqchip
, pin
);
173 list_for_each_entry(e
, &kvm
->irq_routing
, link
)
174 if (e
->type
== KVM_IRQ_ROUTING_IRQCHIP
&&
175 e
->irqchip
.irqchip
== irqchip
&&
176 e
->irqchip
.pin
== pin
) {
181 hlist_for_each_entry(kian
, n
, &kvm
->arch
.irq_ack_notifier_list
, link
)
182 if (kian
->gsi
== gsi
)
183 kian
->irq_acked(kian
);
186 void kvm_register_irq_ack_notifier(struct kvm
*kvm
,
187 struct kvm_irq_ack_notifier
*kian
)
189 mutex_lock(&kvm
->irq_lock
);
190 hlist_add_head(&kian
->link
, &kvm
->arch
.irq_ack_notifier_list
);
191 mutex_unlock(&kvm
->irq_lock
);
194 void kvm_unregister_irq_ack_notifier(struct kvm
*kvm
,
195 struct kvm_irq_ack_notifier
*kian
)
197 mutex_lock(&kvm
->irq_lock
);
198 hlist_del_init(&kian
->link
);
199 mutex_unlock(&kvm
->irq_lock
);
202 int kvm_request_irq_source_id(struct kvm
*kvm
)
204 unsigned long *bitmap
= &kvm
->arch
.irq_sources_bitmap
;
207 mutex_lock(&kvm
->irq_lock
);
208 irq_source_id
= find_first_zero_bit(bitmap
,
209 sizeof(kvm
->arch
.irq_sources_bitmap
));
211 if (irq_source_id
>= sizeof(kvm
->arch
.irq_sources_bitmap
)) {
212 printk(KERN_WARNING
"kvm: exhaust allocatable IRQ sources!\n");
216 ASSERT(irq_source_id
!= KVM_USERSPACE_IRQ_SOURCE_ID
);
217 set_bit(irq_source_id
, bitmap
);
218 mutex_unlock(&kvm
->irq_lock
);
220 return irq_source_id
;
223 void kvm_free_irq_source_id(struct kvm
*kvm
, int irq_source_id
)
227 ASSERT(irq_source_id
!= KVM_USERSPACE_IRQ_SOURCE_ID
);
229 mutex_lock(&kvm
->irq_lock
);
230 if (irq_source_id
< 0 ||
231 irq_source_id
>= sizeof(kvm
->arch
.irq_sources_bitmap
)) {
232 printk(KERN_ERR
"kvm: IRQ source ID out of range!\n");
235 for (i
= 0; i
< KVM_IOAPIC_NUM_PINS
; i
++)
236 clear_bit(irq_source_id
, &kvm
->arch
.irq_states
[i
]);
237 clear_bit(irq_source_id
, &kvm
->arch
.irq_sources_bitmap
);
238 mutex_unlock(&kvm
->irq_lock
);
241 void kvm_register_irq_mask_notifier(struct kvm
*kvm
, int irq
,
242 struct kvm_irq_mask_notifier
*kimn
)
244 mutex_lock(&kvm
->irq_lock
);
246 hlist_add_head(&kimn
->link
, &kvm
->mask_notifier_list
);
247 mutex_unlock(&kvm
->irq_lock
);
250 void kvm_unregister_irq_mask_notifier(struct kvm
*kvm
, int irq
,
251 struct kvm_irq_mask_notifier
*kimn
)
253 mutex_lock(&kvm
->irq_lock
);
254 hlist_del(&kimn
->link
);
255 mutex_unlock(&kvm
->irq_lock
);
258 void kvm_fire_mask_notifiers(struct kvm
*kvm
, int irq
, bool mask
)
260 struct kvm_irq_mask_notifier
*kimn
;
261 struct hlist_node
*n
;
263 WARN_ON(!mutex_is_locked(&kvm
->irq_lock
));
265 hlist_for_each_entry(kimn
, n
, &kvm
->mask_notifier_list
, link
)
266 if (kimn
->irq
== irq
)
267 kimn
->func(kimn
, mask
);
270 static void __kvm_free_irq_routing(struct list_head
*irq_routing
)
272 struct kvm_kernel_irq_routing_entry
*e
, *n
;
274 list_for_each_entry_safe(e
, n
, irq_routing
, link
)
278 void kvm_free_irq_routing(struct kvm
*kvm
)
280 mutex_lock(&kvm
->irq_lock
);
281 __kvm_free_irq_routing(&kvm
->irq_routing
);
282 mutex_unlock(&kvm
->irq_lock
);
285 static int setup_routing_entry(struct kvm_kernel_irq_routing_entry
*e
,
286 const struct kvm_irq_routing_entry
*ue
)
294 case KVM_IRQ_ROUTING_IRQCHIP
:
296 switch (ue
->u
.irqchip
.irqchip
) {
297 case KVM_IRQCHIP_PIC_MASTER
:
298 e
->set
= kvm_set_pic_irq
;
300 case KVM_IRQCHIP_PIC_SLAVE
:
301 e
->set
= kvm_set_pic_irq
;
304 case KVM_IRQCHIP_IOAPIC
:
305 e
->set
= kvm_set_ioapic_irq
;
310 e
->irqchip
.irqchip
= ue
->u
.irqchip
.irqchip
;
311 e
->irqchip
.pin
= ue
->u
.irqchip
.pin
+ delta
;
313 case KVM_IRQ_ROUTING_MSI
:
314 e
->set
= kvm_set_msi
;
315 e
->msi
.address_lo
= ue
->u
.msi
.address_lo
;
316 e
->msi
.address_hi
= ue
->u
.msi
.address_hi
;
317 e
->msi
.data
= ue
->u
.msi
.data
;
328 int kvm_set_irq_routing(struct kvm
*kvm
,
329 const struct kvm_irq_routing_entry
*ue
,
333 struct list_head irq_list
= LIST_HEAD_INIT(irq_list
);
334 struct list_head tmp
= LIST_HEAD_INIT(tmp
);
335 struct kvm_kernel_irq_routing_entry
*e
= NULL
;
339 for (i
= 0; i
< nr
; ++i
) {
341 if (ue
->gsi
>= KVM_MAX_IRQ_ROUTES
)
346 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
349 r
= setup_routing_entry(e
, ue
);
353 list_add(&e
->link
, &irq_list
);
357 mutex_lock(&kvm
->irq_lock
);
358 list_splice(&kvm
->irq_routing
, &tmp
);
359 INIT_LIST_HEAD(&kvm
->irq_routing
);
360 list_splice(&irq_list
, &kvm
->irq_routing
);
361 INIT_LIST_HEAD(&irq_list
);
362 list_splice(&tmp
, &irq_list
);
363 mutex_unlock(&kvm
->irq_lock
);
369 __kvm_free_irq_routing(&irq_list
);
373 #define IOAPIC_ROUTING_ENTRY(irq) \
374 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
375 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
376 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
379 # define PIC_ROUTING_ENTRY(irq) \
380 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
381 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
382 # define ROUTING_ENTRY2(irq) \
383 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
385 # define ROUTING_ENTRY2(irq) \
386 IOAPIC_ROUTING_ENTRY(irq)
389 static const struct kvm_irq_routing_entry default_routing
[] = {
390 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
391 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
392 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
393 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
394 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
395 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
396 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
397 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
398 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
399 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
400 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
401 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
403 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
404 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
405 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
406 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
407 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
408 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
409 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
410 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
411 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
412 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
413 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
414 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
418 int kvm_setup_default_irq_routing(struct kvm
*kvm
)
420 return kvm_set_irq_routing(kvm
, default_routing
,
421 ARRAY_SIZE(default_routing
), 0);