blob: 864ac5483baade3fce7085c3c51f23e493426914 [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>
Sheng Yang79950e12009-02-10 13:57:06 +080025
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080026#include "irq.h"
27
28#include "ioapic.h"
29
Gleb Natapov49256632009-02-04 17:28:14 +020030static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
31 struct kvm *kvm, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020032{
33#ifdef CONFIG_X86
Gleb Natapov49256632009-02-04 17:28:14 +020034 return kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level);
35#else
36 return -1;
Avi Kivity399ec802008-11-19 13:58:46 +020037#endif
38}
39
Gleb Natapov49256632009-02-04 17:28:14 +020040static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
41 struct kvm *kvm, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020042{
Gleb Natapov49256632009-02-04 17:28:14 +020043 return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
Avi Kivity399ec802008-11-19 13:58:46 +020044}
45
Gleb Natapov49256632009-02-04 17:28:14 +020046static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
47 struct kvm *kvm, int level)
Sheng Yang79950e12009-02-10 13:57:06 +080048{
Gleb Natapov71450f72009-02-23 12:57:11 +020049 int vcpu_id, r = -1;
Sheng Yang79950e12009-02-10 13:57:06 +080050 struct kvm_vcpu *vcpu;
51 struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
52 int dest_id = (e->msi.address_lo & MSI_ADDR_DEST_ID_MASK)
53 >> MSI_ADDR_DEST_ID_SHIFT;
54 int vector = (e->msi.data & MSI_DATA_VECTOR_MASK)
55 >> MSI_DATA_VECTOR_SHIFT;
56 int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
57 (unsigned long *)&e->msi.address_lo);
58 int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
59 (unsigned long *)&e->msi.data);
60 int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
61 (unsigned long *)&e->msi.data);
62 u32 deliver_bitmask;
63
64 BUG_ON(!ioapic);
65
66 deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
67 dest_id, dest_mode);
68 /* IOAPIC delivery mode value is the same as MSI here */
69 switch (delivery_mode) {
70 case IOAPIC_LOWEST_PRIORITY:
71 vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
72 deliver_bitmask);
73 if (vcpu != NULL)
Gleb Natapov71450f72009-02-23 12:57:11 +020074 r = kvm_apic_set_irq(vcpu, vector, trig_mode);
Sheng Yang79950e12009-02-10 13:57:06 +080075 else
76 printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
77 break;
78 case IOAPIC_FIXED:
79 for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
80 if (!(deliver_bitmask & (1 << vcpu_id)))
81 continue;
82 deliver_bitmask &= ~(1 << vcpu_id);
83 vcpu = ioapic->kvm->vcpus[vcpu_id];
Gleb Natapov71450f72009-02-23 12:57:11 +020084 if (vcpu) {
85 if (r < 0)
86 r = 0;
87 r += kvm_apic_set_irq(vcpu, vector, trig_mode);
88 }
Sheng Yang79950e12009-02-10 13:57:06 +080089 }
90 break;
91 default:
92 break;
93 }
Gleb Natapov71450f72009-02-23 12:57:11 +020094 return r;
Sheng Yang79950e12009-02-10 13:57:06 +080095}
96
Gleb Natapov49256632009-02-04 17:28:14 +020097/* This should be called with the kvm->lock mutex held
98 * Return value:
99 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
100 * = 0 Interrupt was coalesced (previous irq is still pending)
101 * > 0 Number of CPUs interrupt was delivered to
102 */
103int kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800104{
Avi Kivity399ec802008-11-19 13:58:46 +0200105 struct kvm_kernel_irq_routing_entry *e;
Sheng Yang79950e12009-02-10 13:57:06 +0800106 unsigned long *irq_state, sig_level;
Gleb Natapov49256632009-02-04 17:28:14 +0200107 int ret = -1;
Sheng Yang5550af42008-10-15 20:15:06 +0800108
Sheng Yang79950e12009-02-10 13:57:06 +0800109 if (irq < KVM_IOAPIC_NUM_PINS) {
110 irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
111
112 /* Logical OR for level trig interrupt */
113 if (level)
114 set_bit(irq_source_id, irq_state);
115 else
116 clear_bit(irq_source_id, irq_state);
117 sig_level = !!(*irq_state);
118 } else /* Deal with MSI/MSI-X */
119 sig_level = 1;
Sheng Yang5550af42008-10-15 20:15:06 +0800120
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800121 /* Not possible to detect if the guest uses the PIC or the
122 * IOAPIC. So set the bit in both. The guest will ignore
123 * writes to the unused one.
124 */
Avi Kivity399ec802008-11-19 13:58:46 +0200125 list_for_each_entry(e, &kvm->irq_routing, link)
Gleb Natapov49256632009-02-04 17:28:14 +0200126 if (e->gsi == irq) {
127 int r = e->set(e, kvm, sig_level);
128 if (r < 0)
129 continue;
130
131 ret = r + ((ret < 0) ? 0 : ret);
132 }
133 return ret;
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800134}
135
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200136void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800137{
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200138 struct kvm_kernel_irq_routing_entry *e;
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800139 struct kvm_irq_ack_notifier *kian;
140 struct hlist_node *n;
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200141 unsigned gsi = pin;
142
143 list_for_each_entry(e, &kvm->irq_routing, link)
144 if (e->irqchip.irqchip == irqchip &&
145 e->irqchip.pin == pin) {
146 gsi = e->gsi;
147 break;
148 }
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800149
150 hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
151 if (kian->gsi == gsi)
152 kian->irq_acked(kian);
153}
154
155void kvm_register_irq_ack_notifier(struct kvm *kvm,
156 struct kvm_irq_ack_notifier *kian)
157{
158 hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
159}
160
Sheng Yange19e30e2008-10-20 16:07:10 +0800161void kvm_unregister_irq_ack_notifier(struct kvm_irq_ack_notifier *kian)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800162{
Mark McLoughlinfdd897e2008-12-01 13:57:46 +0000163 hlist_del_init(&kian->link);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800164}
Sheng Yang5550af42008-10-15 20:15:06 +0800165
166/* The caller must hold kvm->lock mutex */
167int kvm_request_irq_source_id(struct kvm *kvm)
168{
169 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
170 int irq_source_id = find_first_zero_bit(bitmap,
171 sizeof(kvm->arch.irq_sources_bitmap));
Mark McLoughlin61552362008-12-01 13:57:48 +0000172
Sheng Yang5550af42008-10-15 20:15:06 +0800173 if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
174 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Mark McLoughlin61552362008-12-01 13:57:48 +0000175 return -EFAULT;
176 }
177
178 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
179 set_bit(irq_source_id, bitmap);
180
Sheng Yang5550af42008-10-15 20:15:06 +0800181 return irq_source_id;
182}
183
184void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
185{
186 int i;
187
Mark McLoughlin61552362008-12-01 13:57:48 +0000188 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
189
190 if (irq_source_id < 0 ||
Sheng Yang5550af42008-10-15 20:15:06 +0800191 irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
192 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
193 return;
194 }
195 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
196 clear_bit(irq_source_id, &kvm->arch.irq_states[i]);
197 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
198}
Avi Kivity75858a82009-01-04 17:10:50 +0200199
200void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
201 struct kvm_irq_mask_notifier *kimn)
202{
203 kimn->irq = irq;
204 hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
205}
206
207void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
208 struct kvm_irq_mask_notifier *kimn)
209{
210 hlist_del(&kimn->link);
211}
212
213void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
214{
215 struct kvm_irq_mask_notifier *kimn;
216 struct hlist_node *n;
217
218 hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
219 if (kimn->irq == irq)
220 kimn->func(kimn, mask);
221}
222
Avi Kivity399ec802008-11-19 13:58:46 +0200223static void __kvm_free_irq_routing(struct list_head *irq_routing)
224{
225 struct kvm_kernel_irq_routing_entry *e, *n;
226
227 list_for_each_entry_safe(e, n, irq_routing, link)
228 kfree(e);
229}
230
231void kvm_free_irq_routing(struct kvm *kvm)
232{
233 __kvm_free_irq_routing(&kvm->irq_routing);
234}
235
Hannes Edercded19f2009-02-21 02:19:13 +0100236static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
237 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200238{
239 int r = -EINVAL;
240 int delta;
241
242 e->gsi = ue->gsi;
243 switch (ue->type) {
244 case KVM_IRQ_ROUTING_IRQCHIP:
245 delta = 0;
246 switch (ue->u.irqchip.irqchip) {
247 case KVM_IRQCHIP_PIC_MASTER:
248 e->set = kvm_set_pic_irq;
249 break;
250 case KVM_IRQCHIP_PIC_SLAVE:
Gleb Natapov49256632009-02-04 17:28:14 +0200251 e->set = kvm_set_pic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200252 delta = 8;
253 break;
254 case KVM_IRQCHIP_IOAPIC:
255 e->set = kvm_set_ioapic_irq;
256 break;
257 default:
258 goto out;
259 }
260 e->irqchip.irqchip = ue->u.irqchip.irqchip;
261 e->irqchip.pin = ue->u.irqchip.pin + delta;
262 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800263 case KVM_IRQ_ROUTING_MSI:
264 e->set = kvm_set_msi;
265 e->msi.address_lo = ue->u.msi.address_lo;
266 e->msi.address_hi = ue->u.msi.address_hi;
267 e->msi.data = ue->u.msi.data;
268 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200269 default:
270 goto out;
271 }
272 r = 0;
273out:
274 return r;
275}
276
277
278int kvm_set_irq_routing(struct kvm *kvm,
279 const struct kvm_irq_routing_entry *ue,
280 unsigned nr,
281 unsigned flags)
282{
283 struct list_head irq_list = LIST_HEAD_INIT(irq_list);
284 struct list_head tmp = LIST_HEAD_INIT(tmp);
285 struct kvm_kernel_irq_routing_entry *e = NULL;
286 unsigned i;
287 int r;
288
289 for (i = 0; i < nr; ++i) {
290 r = -EINVAL;
291 if (ue->gsi >= KVM_MAX_IRQ_ROUTES)
292 goto out;
293 if (ue->flags)
294 goto out;
295 r = -ENOMEM;
296 e = kzalloc(sizeof(*e), GFP_KERNEL);
297 if (!e)
298 goto out;
299 r = setup_routing_entry(e, ue);
300 if (r)
301 goto out;
302 ++ue;
303 list_add(&e->link, &irq_list);
304 e = NULL;
305 }
306
307 mutex_lock(&kvm->lock);
308 list_splice(&kvm->irq_routing, &tmp);
309 INIT_LIST_HEAD(&kvm->irq_routing);
310 list_splice(&irq_list, &kvm->irq_routing);
311 INIT_LIST_HEAD(&irq_list);
312 list_splice(&tmp, &irq_list);
313 mutex_unlock(&kvm->lock);
314
315 r = 0;
316
317out:
318 kfree(e);
319 __kvm_free_irq_routing(&irq_list);
320 return r;
321}
322
323#define IOAPIC_ROUTING_ENTRY(irq) \
324 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
325 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
326#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
327
328#ifdef CONFIG_X86
Avi Kivity399ec802008-11-19 13:58:46 +0200329# define PIC_ROUTING_ENTRY(irq) \
330 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
331 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
332# define ROUTING_ENTRY2(irq) \
333 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
334#else
335# define ROUTING_ENTRY2(irq) \
336 IOAPIC_ROUTING_ENTRY(irq)
337#endif
338
339static const struct kvm_irq_routing_entry default_routing[] = {
340 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
341 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
342 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
343 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
344 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
345 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
346 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
347 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
348 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
349 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
350 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
351 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
352#ifdef CONFIG_IA64
353 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
354 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
355 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
356 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
357 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
358 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
359 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
360 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
361 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
362 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
363 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
364 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
365#endif
366};
367
368int kvm_setup_default_irq_routing(struct kvm *kvm)
369{
370 return kvm_set_irq_routing(kvm, default_routing,
371 ARRAY_SIZE(default_routing), 0);
372}