blob: a8bd466d00ccf1af71ab09e229197c2366fcdef5 [file] [log] [blame]
Xiantao Zhang3de42dc2008-10-06 13:48:45 +08001/*
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 */
21
22#include <linux/kvm_host.h>
Sheng Yang79950e12009-02-10 13:57:06 +080023
Sheng Yang79950e12009-02-10 13:57:06 +080024#include <asm/msidef.h>
Gleb Natapov58c2dde2009-03-05 16:35:04 +020025#ifdef CONFIG_IA64
26#include <asm/iosapic.h>
27#endif
Sheng Yang79950e12009-02-10 13:57:06 +080028
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080029#include "irq.h"
30
31#include "ioapic.h"
32
Gleb Natapov49256632009-02-04 17:28:14 +020033static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
34 struct kvm *kvm, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020035{
36#ifdef CONFIG_X86
Gleb Natapov49256632009-02-04 17:28:14 +020037 return kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level);
38#else
39 return -1;
Avi Kivity399ec802008-11-19 13:58:46 +020040#endif
41}
42
Gleb Natapov49256632009-02-04 17:28:14 +020043static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
44 struct kvm *kvm, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020045{
Gleb Natapov49256632009-02-04 17:28:14 +020046 return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
Avi Kivity399ec802008-11-19 13:58:46 +020047}
48
Gleb Natapov58c2dde2009-03-05 16:35:04 +020049inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
Sheng Yang116191b2009-02-11 16:03:37 +080050{
Gleb Natapov58c2dde2009-03-05 16:35:04 +020051#ifdef CONFIG_IA64
52 return irq->delivery_mode ==
53 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
54#else
55 return irq->delivery_mode == APIC_DM_LOWEST;
56#endif
57}
Sheng Yang116191b2009-02-11 16:03:37 +080058
Gleb Natapov58c2dde2009-03-05 16:35:04 +020059int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
60 struct kvm_lapic_irq *irq)
61{
62 int i, r = -1;
63 struct kvm_vcpu *vcpu, *lowest = NULL;
64
65 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
66 kvm_is_dm_lowest_prio(irq))
Gleb Natapov343f94f2009-03-05 16:34:54 +020067 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
68
Gleb Natapov343f94f2009-03-05 16:34:54 +020069 for (i = 0; i < KVM_MAX_VCPUS; i++) {
70 vcpu = kvm->vcpus[i];
Sheng Yang74a3a8f2009-03-04 13:33:02 +080071
Gleb Natapov343f94f2009-03-05 16:34:54 +020072 if (!vcpu || !kvm_apic_present(vcpu))
73 continue;
Sheng Yang74a3a8f2009-03-04 13:33:02 +080074
Gleb Natapov58c2dde2009-03-05 16:35:04 +020075 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
76 irq->dest_id, irq->dest_mode))
Gleb Natapov343f94f2009-03-05 16:34:54 +020077 continue;
78
Gleb Natapov58c2dde2009-03-05 16:35:04 +020079 if (!kvm_is_dm_lowest_prio(irq)) {
80 if (r < 0)
81 r = 0;
82 r += kvm_apic_set_irq(vcpu, irq);
Gleb Natapove1035712009-03-05 16:34:59 +020083 } else {
Gleb Natapov58c2dde2009-03-05 16:35:04 +020084 if (!lowest)
85 lowest = vcpu;
86 else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
87 lowest = vcpu;
Gleb Natapove1035712009-03-05 16:34:59 +020088 }
Gleb Natapov343f94f2009-03-05 16:34:54 +020089 }
90
Gleb Natapov58c2dde2009-03-05 16:35:04 +020091 if (lowest)
92 r = kvm_apic_set_irq(lowest, irq);
93
94 return r;
Sheng Yang116191b2009-02-11 16:03:37 +080095}
96
Gleb Natapov49256632009-02-04 17:28:14 +020097static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
98 struct kvm *kvm, int level)
Sheng Yang79950e12009-02-10 13:57:06 +080099{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200100 struct kvm_lapic_irq irq;
Sheng Yang79950e12009-02-10 13:57:06 +0800101
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200102 irq.dest_id = (e->msi.address_lo &
Sheng Yang116191b2009-02-11 16:03:37 +0800103 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200104 irq.vector = (e->msi.data &
Sheng Yang116191b2009-02-11 16:03:37 +0800105 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200106 irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
107 irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
108 irq.delivery_mode = e->msi.data & 0x700;
109 irq.level = 1;
110 irq.shorthand = 0;
Sheng Yang116191b2009-02-11 16:03:37 +0800111
112 /* TODO Deal with RH bit of MSI message address */
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200113 return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
Sheng Yang79950e12009-02-10 13:57:06 +0800114}
115
Gleb Natapov49256632009-02-04 17:28:14 +0200116/* This should be called with the kvm->lock mutex held
117 * Return value:
118 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
119 * = 0 Interrupt was coalesced (previous irq is still pending)
120 * > 0 Number of CPUs interrupt was delivered to
121 */
122int kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800123{
Avi Kivity399ec802008-11-19 13:58:46 +0200124 struct kvm_kernel_irq_routing_entry *e;
Sheng Yang79950e12009-02-10 13:57:06 +0800125 unsigned long *irq_state, sig_level;
Gleb Natapov49256632009-02-04 17:28:14 +0200126 int ret = -1;
Sheng Yang5550af42008-10-15 20:15:06 +0800127
Sheng Yang79950e12009-02-10 13:57:06 +0800128 if (irq < KVM_IOAPIC_NUM_PINS) {
129 irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
130
131 /* Logical OR for level trig interrupt */
132 if (level)
133 set_bit(irq_source_id, irq_state);
134 else
135 clear_bit(irq_source_id, irq_state);
136 sig_level = !!(*irq_state);
137 } else /* Deal with MSI/MSI-X */
138 sig_level = 1;
Sheng Yang5550af42008-10-15 20:15:06 +0800139
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800140 /* Not possible to detect if the guest uses the PIC or the
141 * IOAPIC. So set the bit in both. The guest will ignore
142 * writes to the unused one.
143 */
Avi Kivity399ec802008-11-19 13:58:46 +0200144 list_for_each_entry(e, &kvm->irq_routing, link)
Gleb Natapov49256632009-02-04 17:28:14 +0200145 if (e->gsi == irq) {
146 int r = e->set(e, kvm, sig_level);
147 if (r < 0)
148 continue;
149
150 ret = r + ((ret < 0) ? 0 : ret);
151 }
152 return ret;
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800153}
154
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200155void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800156{
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200157 struct kvm_kernel_irq_routing_entry *e;
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800158 struct kvm_irq_ack_notifier *kian;
159 struct hlist_node *n;
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200160 unsigned gsi = pin;
161
162 list_for_each_entry(e, &kvm->irq_routing, link)
163 if (e->irqchip.irqchip == irqchip &&
164 e->irqchip.pin == pin) {
165 gsi = e->gsi;
166 break;
167 }
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800168
169 hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
170 if (kian->gsi == gsi)
171 kian->irq_acked(kian);
172}
173
174void kvm_register_irq_ack_notifier(struct kvm *kvm,
175 struct kvm_irq_ack_notifier *kian)
176{
177 hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
178}
179
Sheng Yange19e30e2008-10-20 16:07:10 +0800180void kvm_unregister_irq_ack_notifier(struct kvm_irq_ack_notifier *kian)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800181{
Mark McLoughlinfdd897e2008-12-01 13:57:46 +0000182 hlist_del_init(&kian->link);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800183}
Sheng Yang5550af42008-10-15 20:15:06 +0800184
185/* The caller must hold kvm->lock mutex */
186int kvm_request_irq_source_id(struct kvm *kvm)
187{
188 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
189 int irq_source_id = find_first_zero_bit(bitmap,
190 sizeof(kvm->arch.irq_sources_bitmap));
Mark McLoughlin61552362008-12-01 13:57:48 +0000191
Sheng Yang5550af42008-10-15 20:15:06 +0800192 if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
193 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Mark McLoughlin61552362008-12-01 13:57:48 +0000194 return -EFAULT;
195 }
196
197 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
198 set_bit(irq_source_id, bitmap);
199
Sheng Yang5550af42008-10-15 20:15:06 +0800200 return irq_source_id;
201}
202
203void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
204{
205 int i;
206
Mark McLoughlin61552362008-12-01 13:57:48 +0000207 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
208
209 if (irq_source_id < 0 ||
Sheng Yang5550af42008-10-15 20:15:06 +0800210 irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
211 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
212 return;
213 }
214 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
215 clear_bit(irq_source_id, &kvm->arch.irq_states[i]);
216 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
217}
Avi Kivity75858a82009-01-04 17:10:50 +0200218
219void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
220 struct kvm_irq_mask_notifier *kimn)
221{
222 kimn->irq = irq;
223 hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
224}
225
226void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
227 struct kvm_irq_mask_notifier *kimn)
228{
229 hlist_del(&kimn->link);
230}
231
232void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
233{
234 struct kvm_irq_mask_notifier *kimn;
235 struct hlist_node *n;
236
237 hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
238 if (kimn->irq == irq)
239 kimn->func(kimn, mask);
240}
241
Avi Kivity399ec802008-11-19 13:58:46 +0200242static void __kvm_free_irq_routing(struct list_head *irq_routing)
243{
244 struct kvm_kernel_irq_routing_entry *e, *n;
245
246 list_for_each_entry_safe(e, n, irq_routing, link)
247 kfree(e);
248}
249
250void kvm_free_irq_routing(struct kvm *kvm)
251{
252 __kvm_free_irq_routing(&kvm->irq_routing);
253}
254
Hannes Edercded19f2009-02-21 02:19:13 +0100255static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
256 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200257{
258 int r = -EINVAL;
259 int delta;
260
261 e->gsi = ue->gsi;
262 switch (ue->type) {
263 case KVM_IRQ_ROUTING_IRQCHIP:
264 delta = 0;
265 switch (ue->u.irqchip.irqchip) {
266 case KVM_IRQCHIP_PIC_MASTER:
267 e->set = kvm_set_pic_irq;
268 break;
269 case KVM_IRQCHIP_PIC_SLAVE:
Gleb Natapov49256632009-02-04 17:28:14 +0200270 e->set = kvm_set_pic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200271 delta = 8;
272 break;
273 case KVM_IRQCHIP_IOAPIC:
Chris Wrightefbc1002009-05-01 14:15:43 -0700274 e->set = kvm_set_ioapic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200275 break;
276 default:
277 goto out;
278 }
279 e->irqchip.irqchip = ue->u.irqchip.irqchip;
280 e->irqchip.pin = ue->u.irqchip.pin + delta;
281 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800282 case KVM_IRQ_ROUTING_MSI:
283 e->set = kvm_set_msi;
284 e->msi.address_lo = ue->u.msi.address_lo;
285 e->msi.address_hi = ue->u.msi.address_hi;
286 e->msi.data = ue->u.msi.data;
287 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200288 default:
289 goto out;
290 }
291 r = 0;
292out:
293 return r;
294}
295
296
297int kvm_set_irq_routing(struct kvm *kvm,
298 const struct kvm_irq_routing_entry *ue,
299 unsigned nr,
300 unsigned flags)
301{
302 struct list_head irq_list = LIST_HEAD_INIT(irq_list);
303 struct list_head tmp = LIST_HEAD_INIT(tmp);
304 struct kvm_kernel_irq_routing_entry *e = NULL;
305 unsigned i;
306 int r;
307
308 for (i = 0; i < nr; ++i) {
309 r = -EINVAL;
310 if (ue->gsi >= KVM_MAX_IRQ_ROUTES)
311 goto out;
312 if (ue->flags)
313 goto out;
314 r = -ENOMEM;
315 e = kzalloc(sizeof(*e), GFP_KERNEL);
316 if (!e)
317 goto out;
318 r = setup_routing_entry(e, ue);
319 if (r)
320 goto out;
321 ++ue;
322 list_add(&e->link, &irq_list);
323 e = NULL;
324 }
325
326 mutex_lock(&kvm->lock);
327 list_splice(&kvm->irq_routing, &tmp);
328 INIT_LIST_HEAD(&kvm->irq_routing);
329 list_splice(&irq_list, &kvm->irq_routing);
330 INIT_LIST_HEAD(&irq_list);
331 list_splice(&tmp, &irq_list);
332 mutex_unlock(&kvm->lock);
333
334 r = 0;
335
336out:
337 kfree(e);
338 __kvm_free_irq_routing(&irq_list);
339 return r;
340}
341
342#define IOAPIC_ROUTING_ENTRY(irq) \
343 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
344 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
345#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
346
347#ifdef CONFIG_X86
Avi Kivity399ec802008-11-19 13:58:46 +0200348# define PIC_ROUTING_ENTRY(irq) \
349 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
350 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
351# define ROUTING_ENTRY2(irq) \
352 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
353#else
354# define ROUTING_ENTRY2(irq) \
355 IOAPIC_ROUTING_ENTRY(irq)
356#endif
357
358static const struct kvm_irq_routing_entry default_routing[] = {
359 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
360 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
361 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
362 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
363 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
364 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
365 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
366 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
367 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
368 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
369 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
370 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
371#ifdef CONFIG_IA64
372 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
373 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
374 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
375 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
376 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
377 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
378 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
379 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
380 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
381 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
382 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
383 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
384#endif
385};
386
387int kvm_setup_default_irq_routing(struct kvm *kvm)
388{
389 return kvm_set_irq_routing(kvm, default_routing,
390 ARRAY_SIZE(default_routing), 0);
391}