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