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