aec7a0d93a3fc3ee7121a68b991da37146e5d81a
[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
24 #include <asm/msidef.h>
25
26 #include "irq.h"
27
28 #include "ioapic.h"
29
30 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
31                            struct kvm *kvm, int level)
32 {
33 #ifdef CONFIG_X86
34         return kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level);
35 #else
36         return -1;
37 #endif
38 }
39
40 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
41                               struct kvm *kvm, int level)
42 {
43         return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
44 }
45
46 void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
47                                    union kvm_ioapic_redirect_entry *entry,
48                                    unsigned long *deliver_bitmask)
49 {
50         struct kvm_vcpu *vcpu;
51
52         *deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
53                                 entry->fields.dest_id, entry->fields.dest_mode);
54         switch (entry->fields.delivery_mode) {
55         case IOAPIC_LOWEST_PRIORITY:
56                 vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm,
57                                 entry->fields.vector, *deliver_bitmask);
58                 *deliver_bitmask = 1 << vcpu->vcpu_id;
59                 break;
60         case IOAPIC_FIXED:
61         case IOAPIC_NMI:
62                 break;
63         default:
64                 if (printk_ratelimit())
65                         printk(KERN_INFO "kvm: unsupported delivery mode %d\n",
66                                 entry->fields.delivery_mode);
67                 *deliver_bitmask = 0;
68         }
69 }
70
71 static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
72                        struct kvm *kvm, int level)
73 {
74         int vcpu_id, r = -1;
75         struct kvm_vcpu *vcpu;
76         struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
77         union kvm_ioapic_redirect_entry entry;
78         unsigned long deliver_bitmask;
79
80         BUG_ON(!ioapic);
81
82         entry.bits = 0;
83         entry.fields.dest_id = (e->msi.address_lo &
84                         MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
85         entry.fields.vector = (e->msi.data &
86                         MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
87         entry.fields.dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
88                         (unsigned long *)&e->msi.address_lo);
89         entry.fields.trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
90                         (unsigned long *)&e->msi.data);
91         entry.fields.delivery_mode = test_bit(
92                         MSI_DATA_DELIVERY_MODE_SHIFT,
93                         (unsigned long *)&e->msi.data);
94
95         /* TODO Deal with RH bit of MSI message address */
96
97         kvm_get_intr_delivery_bitmask(ioapic, &entry, &deliver_bitmask);
98
99         if (!deliver_bitmask) {
100                 printk(KERN_WARNING "kvm: no destination for MSI delivery!");
101                 return -1;
102         }
103         for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
104                 if (!(deliver_bitmask & (1 << vcpu_id)))
105                         continue;
106                 deliver_bitmask &= ~(1 << vcpu_id);
107                 vcpu = ioapic->kvm->vcpus[vcpu_id];
108                 if (vcpu) {
109                         if (r < 0)
110                                 r = 0;
111                         r += kvm_apic_set_irq(vcpu, entry.fields.vector,
112                                               entry.fields.trig_mode);
113                 }
114         }
115         return r;
116 }
117
118 /* This should be called with the kvm->lock mutex held
119  * Return value:
120  *  < 0   Interrupt was ignored (masked or not delivered for other reasons)
121  *  = 0   Interrupt was coalesced (previous irq is still pending)
122  *  > 0   Number of CPUs interrupt was delivered to
123  */
124 int kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
125 {
126         struct kvm_kernel_irq_routing_entry *e;
127         unsigned long *irq_state, sig_level;
128         int ret = -1;
129
130         if (irq < KVM_IOAPIC_NUM_PINS) {
131                 irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
132
133                 /* Logical OR for level trig interrupt */
134                 if (level)
135                         set_bit(irq_source_id, irq_state);
136                 else
137                         clear_bit(irq_source_id, irq_state);
138                 sig_level = !!(*irq_state);
139         } else /* Deal with MSI/MSI-X */
140                 sig_level = 1;
141
142         /* Not possible to detect if the guest uses the PIC or the
143          * IOAPIC.  So set the bit in both. The guest will ignore
144          * writes to the unused one.
145          */
146         list_for_each_entry(e, &kvm->irq_routing, link)
147                 if (e->gsi == irq) {
148                         int r = e->set(e, kvm, sig_level);
149                         if (r < 0)
150                                 continue;
151
152                         ret = r + ((ret < 0) ? 0 : ret);
153                 }
154         return ret;
155 }
156
157 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
158 {
159         struct kvm_kernel_irq_routing_entry *e;
160         struct kvm_irq_ack_notifier *kian;
161         struct hlist_node *n;
162         unsigned gsi = pin;
163
164         list_for_each_entry(e, &kvm->irq_routing, link)
165                 if (e->irqchip.irqchip == irqchip &&
166                     e->irqchip.pin == pin) {
167                         gsi = e->gsi;
168                         break;
169                 }
170
171         hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
172                 if (kian->gsi == gsi)
173                         kian->irq_acked(kian);
174 }
175
176 void kvm_register_irq_ack_notifier(struct kvm *kvm,
177                                    struct kvm_irq_ack_notifier *kian)
178 {
179         hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
180 }
181
182 void kvm_unregister_irq_ack_notifier(struct kvm_irq_ack_notifier *kian)
183 {
184         hlist_del_init(&kian->link);
185 }
186
187 /* The caller must hold kvm->lock mutex */
188 int kvm_request_irq_source_id(struct kvm *kvm)
189 {
190         unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
191         int irq_source_id = find_first_zero_bit(bitmap,
192                                 sizeof(kvm->arch.irq_sources_bitmap));
193
194         if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
195                 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
196                 return -EFAULT;
197         }
198
199         ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
200         set_bit(irq_source_id, bitmap);
201
202         return irq_source_id;
203 }
204
205 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
206 {
207         int i;
208
209         ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
210
211         if (irq_source_id < 0 ||
212             irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
213                 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
214                 return;
215         }
216         for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
217                 clear_bit(irq_source_id, &kvm->arch.irq_states[i]);
218         clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
219 }
220
221 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
222                                     struct kvm_irq_mask_notifier *kimn)
223 {
224         kimn->irq = irq;
225         hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
226 }
227
228 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
229                                       struct kvm_irq_mask_notifier *kimn)
230 {
231         hlist_del(&kimn->link);
232 }
233
234 void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
235 {
236         struct kvm_irq_mask_notifier *kimn;
237         struct hlist_node *n;
238
239         hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
240                 if (kimn->irq == irq)
241                         kimn->func(kimn, mask);
242 }
243
244 static void __kvm_free_irq_routing(struct list_head *irq_routing)
245 {
246         struct kvm_kernel_irq_routing_entry *e, *n;
247
248         list_for_each_entry_safe(e, n, irq_routing, link)
249                 kfree(e);
250 }
251
252 void kvm_free_irq_routing(struct kvm *kvm)
253 {
254         __kvm_free_irq_routing(&kvm->irq_routing);
255 }
256
257 static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
258                                const struct kvm_irq_routing_entry *ue)
259 {
260         int r = -EINVAL;
261         int delta;
262
263         e->gsi = ue->gsi;
264         switch (ue->type) {
265         case KVM_IRQ_ROUTING_IRQCHIP:
266                 delta = 0;
267                 switch (ue->u.irqchip.irqchip) {
268                 case KVM_IRQCHIP_PIC_MASTER:
269                         e->set = kvm_set_pic_irq;
270                         break;
271                 case KVM_IRQCHIP_PIC_SLAVE:
272                         e->set = kvm_set_pic_irq;
273                         delta = 8;
274                         break;
275                 case KVM_IRQCHIP_IOAPIC:
276                                 e->set = kvm_set_ioapic_irq;
277                         break;
278                 default:
279                         goto out;
280                 }
281                 e->irqchip.irqchip = ue->u.irqchip.irqchip;
282                 e->irqchip.pin = ue->u.irqchip.pin + delta;
283                 break;
284         case KVM_IRQ_ROUTING_MSI:
285                 e->set = kvm_set_msi;
286                 e->msi.address_lo = ue->u.msi.address_lo;
287                 e->msi.address_hi = ue->u.msi.address_hi;
288                 e->msi.data = ue->u.msi.data;
289                 break;
290         default:
291                 goto out;
292         }
293         r = 0;
294 out:
295         return r;
296 }
297
298
299 int kvm_set_irq_routing(struct kvm *kvm,
300                         const struct kvm_irq_routing_entry *ue,
301                         unsigned nr,
302                         unsigned flags)
303 {
304         struct list_head irq_list = LIST_HEAD_INIT(irq_list);
305         struct list_head tmp = LIST_HEAD_INIT(tmp);
306         struct kvm_kernel_irq_routing_entry *e = NULL;
307         unsigned i;
308         int r;
309
310         for (i = 0; i < nr; ++i) {
311                 r = -EINVAL;
312                 if (ue->gsi >= KVM_MAX_IRQ_ROUTES)
313                         goto out;
314                 if (ue->flags)
315                         goto out;
316                 r = -ENOMEM;
317                 e = kzalloc(sizeof(*e), GFP_KERNEL);
318                 if (!e)
319                         goto out;
320                 r = setup_routing_entry(e, ue);
321                 if (r)
322                         goto out;
323                 ++ue;
324                 list_add(&e->link, &irq_list);
325                 e = NULL;
326         }
327
328         mutex_lock(&kvm->lock);
329         list_splice(&kvm->irq_routing, &tmp);
330         INIT_LIST_HEAD(&kvm->irq_routing);
331         list_splice(&irq_list, &kvm->irq_routing);
332         INIT_LIST_HEAD(&irq_list);
333         list_splice(&tmp, &irq_list);
334         mutex_unlock(&kvm->lock);
335
336         r = 0;
337
338 out:
339         kfree(e);
340         __kvm_free_irq_routing(&irq_list);
341         return r;
342 }
343
344 #define IOAPIC_ROUTING_ENTRY(irq) \
345         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
346           .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
347 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
348
349 #ifdef CONFIG_X86
350 #  define PIC_ROUTING_ENTRY(irq) \
351         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
352           .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
353 #  define ROUTING_ENTRY2(irq) \
354         IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
355 #else
356 #  define ROUTING_ENTRY2(irq) \
357         IOAPIC_ROUTING_ENTRY(irq)
358 #endif
359
360 static const struct kvm_irq_routing_entry default_routing[] = {
361         ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
362         ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
363         ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
364         ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
365         ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
366         ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
367         ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
368         ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
369         ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
370         ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
371         ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
372         ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
373 #ifdef CONFIG_IA64
374         ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
375         ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
376         ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
377         ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
378         ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
379         ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
380         ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
381         ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
382         ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
383         ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
384         ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
385         ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
386 #endif
387 };
388
389 int kvm_setup_default_irq_routing(struct kvm *kvm)
390 {
391         return kvm_set_irq_routing(kvm, default_routing,
392                                    ARRAY_SIZE(default_routing), 0);
393 }