KVM: Maintain back mapping from irqchip/pin to gsi
[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         int gsi;
179
180         trace_kvm_ack_irq(irqchip, pin);
181
182         gsi = kvm->irq_routing->chip[irqchip][pin];
183         if (gsi != -1)
184                 hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list,
185                                      link)
186                         if (kian->gsi == gsi)
187                                 kian->irq_acked(kian);
188 }
189
190 void kvm_register_irq_ack_notifier(struct kvm *kvm,
191                                    struct kvm_irq_ack_notifier *kian)
192 {
193         mutex_lock(&kvm->irq_lock);
194         hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
195         mutex_unlock(&kvm->irq_lock);
196 }
197
198 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
199                                     struct kvm_irq_ack_notifier *kian)
200 {
201         mutex_lock(&kvm->irq_lock);
202         hlist_del_init(&kian->link);
203         mutex_unlock(&kvm->irq_lock);
204 }
205
206 int kvm_request_irq_source_id(struct kvm *kvm)
207 {
208         unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
209         int irq_source_id;
210
211         mutex_lock(&kvm->irq_lock);
212         irq_source_id = find_first_zero_bit(bitmap,
213                                 sizeof(kvm->arch.irq_sources_bitmap));
214
215         if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
216                 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
217                 return -EFAULT;
218         }
219
220         ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
221         set_bit(irq_source_id, bitmap);
222         mutex_unlock(&kvm->irq_lock);
223
224         return irq_source_id;
225 }
226
227 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
228 {
229         int i;
230
231         ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
232
233         mutex_lock(&kvm->irq_lock);
234         if (irq_source_id < 0 ||
235             irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
236                 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
237                 return;
238         }
239         for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++) {
240                 clear_bit(irq_source_id, &kvm->arch.vioapic->irq_states[i]);
241                 if (i >= 16)
242                         continue;
243 #ifdef CONFIG_X86
244                 clear_bit(irq_source_id, &pic_irqchip(kvm)->irq_states[i]);
245 #endif
246         }
247         clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
248         mutex_unlock(&kvm->irq_lock);
249 }
250
251 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
252                                     struct kvm_irq_mask_notifier *kimn)
253 {
254         mutex_lock(&kvm->irq_lock);
255         kimn->irq = irq;
256         hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
257         mutex_unlock(&kvm->irq_lock);
258 }
259
260 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
261                                       struct kvm_irq_mask_notifier *kimn)
262 {
263         mutex_lock(&kvm->irq_lock);
264         hlist_del(&kimn->link);
265         mutex_unlock(&kvm->irq_lock);
266 }
267
268 void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
269 {
270         struct kvm_irq_mask_notifier *kimn;
271         struct hlist_node *n;
272
273         WARN_ON(!mutex_is_locked(&kvm->irq_lock));
274
275         hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
276                 if (kimn->irq == irq)
277                         kimn->func(kimn, mask);
278 }
279
280 void kvm_free_irq_routing(struct kvm *kvm)
281 {
282         mutex_lock(&kvm->irq_lock);
283         kfree(kvm->irq_routing);
284         mutex_unlock(&kvm->irq_lock);
285 }
286
287 static int setup_routing_entry(struct kvm_irq_routing_table *rt,
288                                struct kvm_kernel_irq_routing_entry *e,
289                                const struct kvm_irq_routing_entry *ue)
290 {
291         int r = -EINVAL;
292         int delta;
293         struct kvm_kernel_irq_routing_entry *ei;
294         struct hlist_node *n;
295
296         /*
297          * Do not allow GSI to be mapped to the same irqchip more than once.
298          * Allow only one to one mapping between GSI and MSI.
299          */
300         hlist_for_each_entry(ei, n, &rt->map[ue->gsi], link)
301                 if (ei->type == KVM_IRQ_ROUTING_MSI ||
302                     ue->u.irqchip.irqchip == ei->irqchip.irqchip)
303                         return r;
304
305         e->gsi = ue->gsi;
306         e->type = ue->type;
307         switch (ue->type) {
308         case KVM_IRQ_ROUTING_IRQCHIP:
309                 delta = 0;
310                 switch (ue->u.irqchip.irqchip) {
311                 case KVM_IRQCHIP_PIC_MASTER:
312                         e->set = kvm_set_pic_irq;
313                         break;
314                 case KVM_IRQCHIP_PIC_SLAVE:
315                         e->set = kvm_set_pic_irq;
316                         delta = 8;
317                         break;
318                 case KVM_IRQCHIP_IOAPIC:
319                         e->set = kvm_set_ioapic_irq;
320                         break;
321                 default:
322                         goto out;
323                 }
324                 e->irqchip.irqchip = ue->u.irqchip.irqchip;
325                 e->irqchip.pin = ue->u.irqchip.pin + delta;
326                 if (e->irqchip.pin >= KVM_IOAPIC_NUM_PINS)
327                         goto out;
328                 rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi;
329                 break;
330         case KVM_IRQ_ROUTING_MSI:
331                 e->set = kvm_set_msi;
332                 e->msi.address_lo = ue->u.msi.address_lo;
333                 e->msi.address_hi = ue->u.msi.address_hi;
334                 e->msi.data = ue->u.msi.data;
335                 break;
336         default:
337                 goto out;
338         }
339
340         hlist_add_head(&e->link, &rt->map[e->gsi]);
341         r = 0;
342 out:
343         return r;
344 }
345
346
347 int kvm_set_irq_routing(struct kvm *kvm,
348                         const struct kvm_irq_routing_entry *ue,
349                         unsigned nr,
350                         unsigned flags)
351 {
352         struct kvm_irq_routing_table *new, *old;
353         u32 i, j, nr_rt_entries = 0;
354         int r;
355
356         for (i = 0; i < nr; ++i) {
357                 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
358                         return -EINVAL;
359                 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
360         }
361
362         nr_rt_entries += 1;
363
364         new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
365                       + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
366                       GFP_KERNEL);
367
368         if (!new)
369                 return -ENOMEM;
370
371         new->rt_entries = (void *)&new->map[nr_rt_entries];
372
373         new->nr_rt_entries = nr_rt_entries;
374         for (i = 0; i < 3; i++)
375                 for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++)
376                         new->chip[i][j] = -1;
377
378         for (i = 0; i < nr; ++i) {
379                 r = -EINVAL;
380                 if (ue->flags)
381                         goto out;
382                 r = setup_routing_entry(new, &new->rt_entries[i], ue);
383                 if (r)
384                         goto out;
385                 ++ue;
386         }
387
388         mutex_lock(&kvm->irq_lock);
389         old = kvm->irq_routing;
390         kvm->irq_routing = new;
391         mutex_unlock(&kvm->irq_lock);
392
393         new = old;
394         r = 0;
395
396 out:
397         kfree(new);
398         return r;
399 }
400
401 #define IOAPIC_ROUTING_ENTRY(irq) \
402         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
403           .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
404 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
405
406 #ifdef CONFIG_X86
407 #  define PIC_ROUTING_ENTRY(irq) \
408         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
409           .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
410 #  define ROUTING_ENTRY2(irq) \
411         IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
412 #else
413 #  define ROUTING_ENTRY2(irq) \
414         IOAPIC_ROUTING_ENTRY(irq)
415 #endif
416
417 static const struct kvm_irq_routing_entry default_routing[] = {
418         ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
419         ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
420         ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
421         ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
422         ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
423         ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
424         ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
425         ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
426         ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
427         ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
428         ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
429         ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
430 #ifdef CONFIG_IA64
431         ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
432         ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
433         ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
434         ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
435         ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
436         ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
437         ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
438         ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
439         ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
440         ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
441         ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
442         ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
443 #endif
444 };
445
446 int kvm_setup_default_irq_routing(struct kvm *kvm)
447 {
448         return kvm_set_irq_routing(kvm, default_routing,
449                                    ARRAY_SIZE(default_routing), 0);
450 }