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