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 <linux/slab.h>
24 #include <trace/events/kvm.h>
26 #include <asm/msidef.h>
28 #include <asm/iosapic.h>
35 static inline int kvm_irq_line_state(unsigned long *irq_state
,
36 int irq_source_id
, int level
)
38 /* Logical OR for level trig interrupt */
40 set_bit(irq_source_id
, irq_state
);
42 clear_bit(irq_source_id
, irq_state
);
44 return !!(*irq_state
);
47 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry
*e
,
48 struct kvm
*kvm
, int irq_source_id
, int level
)
51 struct kvm_pic
*pic
= pic_irqchip(kvm
);
52 level
= kvm_irq_line_state(&pic
->irq_states
[e
->irqchip
.pin
],
53 irq_source_id
, level
);
54 return kvm_pic_set_irq(pic
, e
->irqchip
.pin
, level
);
60 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry
*e
,
61 struct kvm
*kvm
, int irq_source_id
, int level
)
63 struct kvm_ioapic
*ioapic
= kvm
->arch
.vioapic
;
64 level
= kvm_irq_line_state(&ioapic
->irq_states
[e
->irqchip
.pin
],
65 irq_source_id
, level
);
67 return kvm_ioapic_set_irq(ioapic
, e
->irqchip
.pin
, level
);
70 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq
*irq
)
73 return irq
->delivery_mode
==
74 (IOSAPIC_LOWEST_PRIORITY
<< IOSAPIC_DELIVERY_SHIFT
);
76 return irq
->delivery_mode
== APIC_DM_LOWEST
;
80 int kvm_irq_delivery_to_apic(struct kvm
*kvm
, struct kvm_lapic
*src
,
81 struct kvm_lapic_irq
*irq
)
84 struct kvm_vcpu
*vcpu
, *lowest
= NULL
;
86 if (irq
->dest_mode
== 0 && irq
->dest_id
== 0xff &&
87 kvm_is_dm_lowest_prio(irq
))
88 printk(KERN_INFO
"kvm: apic: phys broadcast and lowest prio\n");
90 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
91 if (!kvm_apic_present(vcpu
))
94 if (!kvm_apic_match_dest(vcpu
, src
, irq
->shorthand
,
95 irq
->dest_id
, irq
->dest_mode
))
98 if (!kvm_is_dm_lowest_prio(irq
)) {
101 r
+= kvm_apic_set_irq(vcpu
, irq
);
105 else if (kvm_apic_compare_prio(vcpu
, lowest
) < 0)
111 r
= kvm_apic_set_irq(lowest
, irq
);
116 static int kvm_set_msi(struct kvm_kernel_irq_routing_entry
*e
,
117 struct kvm
*kvm
, int irq_source_id
, int level
)
119 struct kvm_lapic_irq irq
;
124 trace_kvm_msi_set_irq(e
->msi
.address_lo
, e
->msi
.data
);
126 irq
.dest_id
= (e
->msi
.address_lo
&
127 MSI_ADDR_DEST_ID_MASK
) >> MSI_ADDR_DEST_ID_SHIFT
;
128 irq
.vector
= (e
->msi
.data
&
129 MSI_DATA_VECTOR_MASK
) >> MSI_DATA_VECTOR_SHIFT
;
130 irq
.dest_mode
= (1 << MSI_ADDR_DEST_MODE_SHIFT
) & e
->msi
.address_lo
;
131 irq
.trig_mode
= (1 << MSI_DATA_TRIGGER_SHIFT
) & e
->msi
.data
;
132 irq
.delivery_mode
= e
->msi
.data
& 0x700;
136 /* TODO Deal with RH bit of MSI message address */
137 return kvm_irq_delivery_to_apic(kvm
, NULL
, &irq
);
142 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
143 * = 0 Interrupt was coalesced (previous irq is still pending)
144 * > 0 Number of CPUs interrupt was delivered to
146 int kvm_set_irq(struct kvm
*kvm
, int irq_source_id
, u32 irq
, int level
)
148 struct kvm_kernel_irq_routing_entry
*e
, irq_set
[KVM_NR_IRQCHIPS
];
150 struct kvm_irq_routing_table
*irq_rt
;
151 struct hlist_node
*n
;
153 trace_kvm_set_irq(irq
, level
, irq_source_id
);
155 /* Not possible to detect if the guest uses the PIC or the
156 * IOAPIC. So set the bit in both. The guest will ignore
157 * writes to the unused one.
160 irq_rt
= rcu_dereference(kvm
->irq_routing
);
161 if (irq
< irq_rt
->nr_rt_entries
)
162 hlist_for_each_entry(e
, n
, &irq_rt
->map
[irq
], link
)
168 r
= irq_set
[i
].set(&irq_set
[i
], kvm
, irq_source_id
, level
);
172 ret
= r
+ ((ret
< 0) ? 0 : ret
);
178 void kvm_notify_acked_irq(struct kvm
*kvm
, unsigned irqchip
, unsigned pin
)
180 struct kvm_irq_ack_notifier
*kian
;
181 struct hlist_node
*n
;
184 trace_kvm_ack_irq(irqchip
, pin
);
187 gsi
= rcu_dereference(kvm
->irq_routing
)->chip
[irqchip
][pin
];
189 hlist_for_each_entry_rcu(kian
, n
, &kvm
->irq_ack_notifier_list
,
191 if (kian
->gsi
== gsi
)
192 kian
->irq_acked(kian
);
196 void kvm_register_irq_ack_notifier(struct kvm
*kvm
,
197 struct kvm_irq_ack_notifier
*kian
)
199 mutex_lock(&kvm
->irq_lock
);
200 hlist_add_head_rcu(&kian
->link
, &kvm
->irq_ack_notifier_list
);
201 mutex_unlock(&kvm
->irq_lock
);
204 void kvm_unregister_irq_ack_notifier(struct kvm
*kvm
,
205 struct kvm_irq_ack_notifier
*kian
)
207 mutex_lock(&kvm
->irq_lock
);
208 hlist_del_init_rcu(&kian
->link
);
209 mutex_unlock(&kvm
->irq_lock
);
213 int kvm_request_irq_source_id(struct kvm
*kvm
)
215 unsigned long *bitmap
= &kvm
->arch
.irq_sources_bitmap
;
218 mutex_lock(&kvm
->irq_lock
);
219 irq_source_id
= find_first_zero_bit(bitmap
, BITS_PER_LONG
);
221 if (irq_source_id
>= BITS_PER_LONG
) {
222 printk(KERN_WARNING
"kvm: exhaust allocatable IRQ sources!\n");
223 irq_source_id
= -EFAULT
;
227 ASSERT(irq_source_id
!= KVM_USERSPACE_IRQ_SOURCE_ID
);
228 set_bit(irq_source_id
, bitmap
);
230 mutex_unlock(&kvm
->irq_lock
);
232 return irq_source_id
;
235 void kvm_free_irq_source_id(struct kvm
*kvm
, int irq_source_id
)
239 ASSERT(irq_source_id
!= KVM_USERSPACE_IRQ_SOURCE_ID
);
241 mutex_lock(&kvm
->irq_lock
);
242 if (irq_source_id
< 0 ||
243 irq_source_id
>= BITS_PER_LONG
) {
244 printk(KERN_ERR
"kvm: IRQ source ID out of range!\n");
247 clear_bit(irq_source_id
, &kvm
->arch
.irq_sources_bitmap
);
248 if (!irqchip_in_kernel(kvm
))
251 for (i
= 0; i
< KVM_IOAPIC_NUM_PINS
; i
++) {
252 clear_bit(irq_source_id
, &kvm
->arch
.vioapic
->irq_states
[i
]);
256 clear_bit(irq_source_id
, &pic_irqchip(kvm
)->irq_states
[i
]);
260 mutex_unlock(&kvm
->irq_lock
);
263 void kvm_register_irq_mask_notifier(struct kvm
*kvm
, int irq
,
264 struct kvm_irq_mask_notifier
*kimn
)
266 mutex_lock(&kvm
->irq_lock
);
268 hlist_add_head_rcu(&kimn
->link
, &kvm
->mask_notifier_list
);
269 mutex_unlock(&kvm
->irq_lock
);
272 void kvm_unregister_irq_mask_notifier(struct kvm
*kvm
, int irq
,
273 struct kvm_irq_mask_notifier
*kimn
)
275 mutex_lock(&kvm
->irq_lock
);
276 hlist_del_rcu(&kimn
->link
);
277 mutex_unlock(&kvm
->irq_lock
);
281 void kvm_fire_mask_notifiers(struct kvm
*kvm
, int irq
, bool mask
)
283 struct kvm_irq_mask_notifier
*kimn
;
284 struct hlist_node
*n
;
287 hlist_for_each_entry_rcu(kimn
, n
, &kvm
->mask_notifier_list
, link
)
288 if (kimn
->irq
== irq
)
289 kimn
->func(kimn
, mask
);
293 void kvm_free_irq_routing(struct kvm
*kvm
)
295 /* Called only during vm destruction. Nobody can use the pointer
297 kfree(kvm
->irq_routing
);
300 static int setup_routing_entry(struct kvm_irq_routing_table
*rt
,
301 struct kvm_kernel_irq_routing_entry
*e
,
302 const struct kvm_irq_routing_entry
*ue
)
307 struct kvm_kernel_irq_routing_entry
*ei
;
308 struct hlist_node
*n
;
311 * Do not allow GSI to be mapped to the same irqchip more than once.
312 * Allow only one to one mapping between GSI and MSI.
314 hlist_for_each_entry(ei
, n
, &rt
->map
[ue
->gsi
], link
)
315 if (ei
->type
== KVM_IRQ_ROUTING_MSI
||
316 ue
->u
.irqchip
.irqchip
== ei
->irqchip
.irqchip
)
322 case KVM_IRQ_ROUTING_IRQCHIP
:
324 switch (ue
->u
.irqchip
.irqchip
) {
325 case KVM_IRQCHIP_PIC_MASTER
:
326 e
->set
= kvm_set_pic_irq
;
329 case KVM_IRQCHIP_PIC_SLAVE
:
330 e
->set
= kvm_set_pic_irq
;
334 case KVM_IRQCHIP_IOAPIC
:
335 max_pin
= KVM_IOAPIC_NUM_PINS
;
336 e
->set
= kvm_set_ioapic_irq
;
341 e
->irqchip
.irqchip
= ue
->u
.irqchip
.irqchip
;
342 e
->irqchip
.pin
= ue
->u
.irqchip
.pin
+ delta
;
343 if (e
->irqchip
.pin
>= max_pin
)
345 rt
->chip
[ue
->u
.irqchip
.irqchip
][e
->irqchip
.pin
] = ue
->gsi
;
347 case KVM_IRQ_ROUTING_MSI
:
348 e
->set
= kvm_set_msi
;
349 e
->msi
.address_lo
= ue
->u
.msi
.address_lo
;
350 e
->msi
.address_hi
= ue
->u
.msi
.address_hi
;
351 e
->msi
.data
= ue
->u
.msi
.data
;
357 hlist_add_head(&e
->link
, &rt
->map
[e
->gsi
]);
364 int kvm_set_irq_routing(struct kvm
*kvm
,
365 const struct kvm_irq_routing_entry
*ue
,
369 struct kvm_irq_routing_table
*new, *old
;
370 u32 i
, j
, nr_rt_entries
= 0;
373 for (i
= 0; i
< nr
; ++i
) {
374 if (ue
[i
].gsi
>= KVM_MAX_IRQ_ROUTES
)
376 nr_rt_entries
= max(nr_rt_entries
, ue
[i
].gsi
);
381 new = kzalloc(sizeof(*new) + (nr_rt_entries
* sizeof(struct hlist_head
))
382 + (nr
* sizeof(struct kvm_kernel_irq_routing_entry
)),
388 new->rt_entries
= (void *)&new->map
[nr_rt_entries
];
390 new->nr_rt_entries
= nr_rt_entries
;
391 for (i
= 0; i
< 3; i
++)
392 for (j
= 0; j
< KVM_IOAPIC_NUM_PINS
; j
++)
393 new->chip
[i
][j
] = -1;
395 for (i
= 0; i
< nr
; ++i
) {
399 r
= setup_routing_entry(new, &new->rt_entries
[i
], ue
);
405 mutex_lock(&kvm
->irq_lock
);
406 old
= kvm
->irq_routing
;
407 rcu_assign_pointer(kvm
->irq_routing
, new);
408 mutex_unlock(&kvm
->irq_lock
);
419 #define IOAPIC_ROUTING_ENTRY(irq) \
420 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
421 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
422 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
425 # define PIC_ROUTING_ENTRY(irq) \
426 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
427 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
428 # define ROUTING_ENTRY2(irq) \
429 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
431 # define ROUTING_ENTRY2(irq) \
432 IOAPIC_ROUTING_ENTRY(irq)
435 static const struct kvm_irq_routing_entry default_routing
[] = {
436 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
437 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
438 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
439 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
440 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
441 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
442 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
443 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
444 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
445 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
446 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
447 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
449 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
450 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
451 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
452 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
453 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
454 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
455 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
456 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
457 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
458 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
459 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
460 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
464 int kvm_setup_default_irq_routing(struct kvm
*kvm
)
466 return kvm_set_irq_routing(kvm
, default_routing
,
467 ARRAY_SIZE(default_routing
), 0);