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