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