81950f6f6fd97e0ef6d803542cb2b12fec473387
[linux-2.6.git] / virt / kvm / irq_comm.c
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  */
21
22 #include <linux/kvm_host.h>
23 #include <trace/events/kvm.h>
24
25 #include <asm/msidef.h>
26 #ifdef CONFIG_IA64
27 #include <asm/iosapic.h>
28 #endif
29
30 #include "irq.h"
31
32 #include "ioapic.h"
33
34 static inline int kvm_irq_line_state(unsigned long *irq_state,
35                                      int irq_source_id, int level)
36 {
37         /* Logical OR for level trig interrupt */
38         if (level)
39                 set_bit(irq_source_id, irq_state);
40         else
41                 clear_bit(irq_source_id, irq_state);
42
43         return !!(*irq_state);
44 }
45
46 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
47                            struct kvm *kvm, int irq_source_id, int level)
48 {
49 #ifdef CONFIG_X86
50         struct kvm_pic *pic = pic_irqchip(kvm);
51         level = kvm_irq_line_state(&pic->irq_states[e->irqchip.pin],
52                                    irq_source_id, level);
53         return kvm_pic_set_irq(pic, e->irqchip.pin, level);
54 #else
55         return -1;
56 #endif
57 }
58
59 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
60                               struct kvm *kvm, int irq_source_id, int level)
61 {
62         struct kvm_ioapic *ioapic = kvm->arch.vioapic;
63         level = kvm_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
64                                    irq_source_id, level);
65
66         return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
67 }
68
69 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
70 {
71 #ifdef CONFIG_IA64
72         return irq->delivery_mode ==
73                 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
74 #else
75         return irq->delivery_mode == APIC_DM_LOWEST;
76 #endif
77 }
78
79 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
80                 struct kvm_lapic_irq *irq)
81 {
82         int i, r = -1;
83         struct kvm_vcpu *vcpu, *lowest = NULL;
84
85         WARN_ON(!mutex_is_locked(&kvm->irq_lock));
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 {
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
117 static 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 /* This should be called with the kvm->irq_lock mutex held
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  */
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;
150         int ret = -1;
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         WARN_ON(!mutex_is_locked(&kvm->irq_lock));
157
158         /* Not possible to detect if the guest uses the PIC or the
159          * IOAPIC.  So set the bit in both. The guest will ignore
160          * writes to the unused one.
161          */
162         irq_rt = kvm->irq_routing;
163         if (irq < irq_rt->nr_rt_entries)
164                 hlist_for_each_entry(e, n, &irq_rt->map[irq], link) {
165                         int r = e->set(e, kvm, irq_source_id, level);
166                         if (r < 0)
167                                 continue;
168
169                         ret = r + ((ret < 0) ? 0 : ret);
170                 }
171         return ret;
172 }
173
174 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
175 {
176         struct kvm_irq_ack_notifier *kian;
177         struct hlist_node *n;
178         unsigned gsi = pin;
179         int i;
180
181         trace_kvm_ack_irq(irqchip, pin);
182
183         for (i = 0; i < kvm->irq_routing->nr_rt_entries; i++) {
184                 struct kvm_kernel_irq_routing_entry *e;
185                 e = &kvm->irq_routing->rt_entries[i];
186                 if (e->type == KVM_IRQ_ROUTING_IRQCHIP &&
187                     e->irqchip.irqchip == irqchip &&
188                     e->irqchip.pin == pin) {
189                         gsi = e->gsi;
190                         break;
191                 }
192         }
193
194         hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
195                 if (kian->gsi == gsi)
196                         kian->irq_acked(kian);
197 }
198
199 void kvm_register_irq_ack_notifier(struct kvm *kvm,
200                                    struct kvm_irq_ack_notifier *kian)
201 {
202         mutex_lock(&kvm->irq_lock);
203         hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
204         mutex_unlock(&kvm->irq_lock);
205 }
206
207 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
208                                     struct kvm_irq_ack_notifier *kian)
209 {
210         mutex_lock(&kvm->irq_lock);
211         hlist_del_init(&kian->link);
212         mutex_unlock(&kvm->irq_lock);
213 }
214
215 int kvm_request_irq_source_id(struct kvm *kvm)
216 {
217         unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
218         int irq_source_id;
219
220         mutex_lock(&kvm->irq_lock);
221         irq_source_id = find_first_zero_bit(bitmap,
222                                 sizeof(kvm->arch.irq_sources_bitmap));
223
224         if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
225                 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
226                 return -EFAULT;
227         }
228
229         ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
230         set_bit(irq_source_id, bitmap);
231         mutex_unlock(&kvm->irq_lock);
232
233         return irq_source_id;
234 }
235
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 >= sizeof(kvm->arch.irq_sources_bitmap)) {
245                 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
246                 return;
247         }
248         for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++) {
249                 clear_bit(irq_source_id, &kvm->arch.vioapic->irq_states[i]);
250                 if (i >= 16)
251                         continue;
252 #ifdef CONFIG_X86
253                 clear_bit(irq_source_id, &pic_irqchip(kvm)->irq_states[i]);
254 #endif
255         }
256         clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
257         mutex_unlock(&kvm->irq_lock);
258 }
259
260 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
261                                     struct kvm_irq_mask_notifier *kimn)
262 {
263         mutex_lock(&kvm->irq_lock);
264         kimn->irq = irq;
265         hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
266         mutex_unlock(&kvm->irq_lock);
267 }
268
269 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
270                                       struct kvm_irq_mask_notifier *kimn)
271 {
272         mutex_lock(&kvm->irq_lock);
273         hlist_del(&kimn->link);
274         mutex_unlock(&kvm->irq_lock);
275 }
276
277 void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
278 {
279         struct kvm_irq_mask_notifier *kimn;
280         struct hlist_node *n;
281
282         WARN_ON(!mutex_is_locked(&kvm->irq_lock));
283
284         hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
285                 if (kimn->irq == irq)
286                         kimn->func(kimn, mask);
287 }
288
289 void kvm_free_irq_routing(struct kvm *kvm)
290 {
291         mutex_lock(&kvm->irq_lock);
292         kfree(kvm->irq_routing);
293         mutex_unlock(&kvm->irq_lock);
294 }
295
296 static int setup_routing_entry(struct kvm_irq_routing_table *rt,
297                                struct kvm_kernel_irq_routing_entry *e,
298                                const struct kvm_irq_routing_entry *ue)
299 {
300         int r = -EINVAL;
301         int delta;
302         struct kvm_kernel_irq_routing_entry *ei;
303         struct hlist_node *n;
304
305         /*
306          * Do not allow GSI to be mapped to the same irqchip more than once.
307          * Allow only one to one mapping between GSI and MSI.
308          */
309         hlist_for_each_entry(ei, n, &rt->map[ue->gsi], link)
310                 if (ei->type == KVM_IRQ_ROUTING_MSI ||
311                     ue->u.irqchip.irqchip == ei->irqchip.irqchip)
312                         return r;
313
314         e->gsi = ue->gsi;
315         e->type = ue->type;
316         switch (ue->type) {
317         case KVM_IRQ_ROUTING_IRQCHIP:
318                 delta = 0;
319                 switch (ue->u.irqchip.irqchip) {
320                 case KVM_IRQCHIP_PIC_MASTER:
321                         e->set = kvm_set_pic_irq;
322                         break;
323                 case KVM_IRQCHIP_PIC_SLAVE:
324                         e->set = kvm_set_pic_irq;
325                         delta = 8;
326                         break;
327                 case KVM_IRQCHIP_IOAPIC:
328                         e->set = kvm_set_ioapic_irq;
329                         break;
330                 default:
331                         goto out;
332                 }
333                 e->irqchip.irqchip = ue->u.irqchip.irqchip;
334                 e->irqchip.pin = ue->u.irqchip.pin + delta;
335                 break;
336         case KVM_IRQ_ROUTING_MSI:
337                 e->set = kvm_set_msi;
338                 e->msi.address_lo = ue->u.msi.address_lo;
339                 e->msi.address_hi = ue->u.msi.address_hi;
340                 e->msi.data = ue->u.msi.data;
341                 break;
342         default:
343                 goto out;
344         }
345
346         hlist_add_head(&e->link, &rt->map[e->gsi]);
347         r = 0;
348 out:
349         return r;
350 }
351
352
353 int kvm_set_irq_routing(struct kvm *kvm,
354                         const struct kvm_irq_routing_entry *ue,
355                         unsigned nr,
356                         unsigned flags)
357 {
358         struct kvm_irq_routing_table *new, *old;
359         u32 i, nr_rt_entries = 0;
360         int r;
361
362         for (i = 0; i < nr; ++i) {
363                 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
364                         return -EINVAL;
365                 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
366         }
367
368         nr_rt_entries += 1;
369
370         new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
371                       + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
372                       GFP_KERNEL);
373
374         if (!new)
375                 return -ENOMEM;
376
377         new->rt_entries = (void *)&new->map[nr_rt_entries];
378
379         new->nr_rt_entries = nr_rt_entries;
380
381         for (i = 0; i < nr; ++i) {
382                 r = -EINVAL;
383                 if (ue->flags)
384                         goto out;
385                 r = setup_routing_entry(new, &new->rt_entries[i], ue);
386                 if (r)
387                         goto out;
388                 ++ue;
389         }
390
391         mutex_lock(&kvm->irq_lock);
392         old = kvm->irq_routing;
393         kvm->irq_routing = new;
394         mutex_unlock(&kvm->irq_lock);
395
396         new = old;
397         r = 0;
398
399 out:
400         kfree(new);
401         return r;
402 }
403
404 #define IOAPIC_ROUTING_ENTRY(irq) \
405         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
406           .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
407 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
408
409 #ifdef CONFIG_X86
410 #  define PIC_ROUTING_ENTRY(irq) \
411         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
412           .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
413 #  define ROUTING_ENTRY2(irq) \
414         IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
415 #else
416 #  define ROUTING_ENTRY2(irq) \
417         IOAPIC_ROUTING_ENTRY(irq)
418 #endif
419
420 static const struct kvm_irq_routing_entry default_routing[] = {
421         ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
422         ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
423         ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
424         ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
425         ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
426         ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
427         ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
428         ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
429         ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
430         ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
431         ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
432         ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
433 #ifdef CONFIG_IA64
434         ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
435         ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
436         ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
437         ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
438         ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
439         ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
440         ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
441         ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
442         ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
443         ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
444         ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
445         ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
446 #endif
447 };
448
449 int kvm_setup_default_irq_routing(struct kvm *kvm)
450 {
451         return kvm_set_irq_routing(kvm, default_routing,
452                                    ARRAY_SIZE(default_routing), 0);
453 }