KVM: Use macro to iterate over vcpus.
[platform/adaptation/renesas_rcar/renesas_kernel.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 #ifdef CONFIG_IA64
26 #include <asm/iosapic.h>
27 #endif
28
29 #include "irq.h"
30
31 #include "ioapic.h"
32
33 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
34                            struct kvm *kvm, int level)
35 {
36 #ifdef CONFIG_X86
37         return kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level);
38 #else
39         return -1;
40 #endif
41 }
42
43 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
44                               struct kvm *kvm, int level)
45 {
46         return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
47 }
48
49 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
50 {
51 #ifdef CONFIG_IA64
52         return irq->delivery_mode ==
53                 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
54 #else
55         return irq->delivery_mode == APIC_DM_LOWEST;
56 #endif
57 }
58
59 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
60                 struct kvm_lapic_irq *irq)
61 {
62         int i, r = -1;
63         struct kvm_vcpu *vcpu, *lowest = NULL;
64
65         WARN_ON(!mutex_is_locked(&kvm->irq_lock));
66
67         if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
68                         kvm_is_dm_lowest_prio(irq))
69                 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
70
71         kvm_for_each_vcpu(i, vcpu, kvm) {
72                 if (!kvm_apic_present(vcpu))
73                         continue;
74
75                 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
76                                         irq->dest_id, irq->dest_mode))
77                         continue;
78
79                 if (!kvm_is_dm_lowest_prio(irq)) {
80                         if (r < 0)
81                                 r = 0;
82                         r += kvm_apic_set_irq(vcpu, irq);
83                 } else {
84                         if (!lowest)
85                                 lowest = vcpu;
86                         else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
87                                 lowest = vcpu;
88                 }
89         }
90
91         if (lowest)
92                 r = kvm_apic_set_irq(lowest, irq);
93
94         return r;
95 }
96
97 static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
98                        struct kvm *kvm, int level)
99 {
100         struct kvm_lapic_irq irq;
101
102         irq.dest_id = (e->msi.address_lo &
103                         MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
104         irq.vector = (e->msi.data &
105                         MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
106         irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
107         irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
108         irq.delivery_mode = e->msi.data & 0x700;
109         irq.level = 1;
110         irq.shorthand = 0;
111
112         /* TODO Deal with RH bit of MSI message address */
113         return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
114 }
115
116 /* This should be called with the kvm->irq_lock mutex held
117  * Return value:
118  *  < 0   Interrupt was ignored (masked or not delivered for other reasons)
119  *  = 0   Interrupt was coalesced (previous irq is still pending)
120  *  > 0   Number of CPUs interrupt was delivered to
121  */
122 int kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
123 {
124         struct kvm_kernel_irq_routing_entry *e;
125         unsigned long *irq_state, sig_level;
126         int ret = -1;
127
128         WARN_ON(!mutex_is_locked(&kvm->irq_lock));
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->type == KVM_IRQ_ROUTING_IRQCHIP &&
166                     e->irqchip.irqchip == irqchip &&
167                     e->irqchip.pin == pin) {
168                         gsi = e->gsi;
169                         break;
170                 }
171
172         hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
173                 if (kian->gsi == gsi)
174                         kian->irq_acked(kian);
175 }
176
177 void kvm_register_irq_ack_notifier(struct kvm *kvm,
178                                    struct kvm_irq_ack_notifier *kian)
179 {
180         mutex_lock(&kvm->irq_lock);
181         hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
182         mutex_unlock(&kvm->irq_lock);
183 }
184
185 void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
186                                     struct kvm_irq_ack_notifier *kian)
187 {
188         mutex_lock(&kvm->irq_lock);
189         hlist_del_init(&kian->link);
190         mutex_unlock(&kvm->irq_lock);
191 }
192
193 int kvm_request_irq_source_id(struct kvm *kvm)
194 {
195         unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
196         int irq_source_id;
197
198         mutex_lock(&kvm->irq_lock);
199         irq_source_id = find_first_zero_bit(bitmap,
200                                 sizeof(kvm->arch.irq_sources_bitmap));
201
202         if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
203                 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
204                 return -EFAULT;
205         }
206
207         ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
208         set_bit(irq_source_id, bitmap);
209         mutex_unlock(&kvm->irq_lock);
210
211         return irq_source_id;
212 }
213
214 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
215 {
216         int i;
217
218         ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
219
220         mutex_lock(&kvm->irq_lock);
221         if (irq_source_id < 0 ||
222             irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
223                 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
224                 return;
225         }
226         for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
227                 clear_bit(irq_source_id, &kvm->arch.irq_states[i]);
228         clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
229         mutex_unlock(&kvm->irq_lock);
230 }
231
232 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
233                                     struct kvm_irq_mask_notifier *kimn)
234 {
235         mutex_lock(&kvm->irq_lock);
236         kimn->irq = irq;
237         hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
238         mutex_unlock(&kvm->irq_lock);
239 }
240
241 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
242                                       struct kvm_irq_mask_notifier *kimn)
243 {
244         mutex_lock(&kvm->irq_lock);
245         hlist_del(&kimn->link);
246         mutex_unlock(&kvm->irq_lock);
247 }
248
249 void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
250 {
251         struct kvm_irq_mask_notifier *kimn;
252         struct hlist_node *n;
253
254         WARN_ON(!mutex_is_locked(&kvm->irq_lock));
255
256         hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
257                 if (kimn->irq == irq)
258                         kimn->func(kimn, mask);
259 }
260
261 static void __kvm_free_irq_routing(struct list_head *irq_routing)
262 {
263         struct kvm_kernel_irq_routing_entry *e, *n;
264
265         list_for_each_entry_safe(e, n, irq_routing, link)
266                 kfree(e);
267 }
268
269 void kvm_free_irq_routing(struct kvm *kvm)
270 {
271         mutex_lock(&kvm->irq_lock);
272         __kvm_free_irq_routing(&kvm->irq_routing);
273         mutex_unlock(&kvm->irq_lock);
274 }
275
276 static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
277                                const struct kvm_irq_routing_entry *ue)
278 {
279         int r = -EINVAL;
280         int delta;
281
282         e->gsi = ue->gsi;
283         e->type = ue->type;
284         switch (ue->type) {
285         case KVM_IRQ_ROUTING_IRQCHIP:
286                 delta = 0;
287                 switch (ue->u.irqchip.irqchip) {
288                 case KVM_IRQCHIP_PIC_MASTER:
289                         e->set = kvm_set_pic_irq;
290                         break;
291                 case KVM_IRQCHIP_PIC_SLAVE:
292                         e->set = kvm_set_pic_irq;
293                         delta = 8;
294                         break;
295                 case KVM_IRQCHIP_IOAPIC:
296                         e->set = kvm_set_ioapic_irq;
297                         break;
298                 default:
299                         goto out;
300                 }
301                 e->irqchip.irqchip = ue->u.irqchip.irqchip;
302                 e->irqchip.pin = ue->u.irqchip.pin + delta;
303                 break;
304         case KVM_IRQ_ROUTING_MSI:
305                 e->set = kvm_set_msi;
306                 e->msi.address_lo = ue->u.msi.address_lo;
307                 e->msi.address_hi = ue->u.msi.address_hi;
308                 e->msi.data = ue->u.msi.data;
309                 break;
310         default:
311                 goto out;
312         }
313         r = 0;
314 out:
315         return r;
316 }
317
318
319 int kvm_set_irq_routing(struct kvm *kvm,
320                         const struct kvm_irq_routing_entry *ue,
321                         unsigned nr,
322                         unsigned flags)
323 {
324         struct list_head irq_list = LIST_HEAD_INIT(irq_list);
325         struct list_head tmp = LIST_HEAD_INIT(tmp);
326         struct kvm_kernel_irq_routing_entry *e = NULL;
327         unsigned i;
328         int r;
329
330         for (i = 0; i < nr; ++i) {
331                 r = -EINVAL;
332                 if (ue->gsi >= KVM_MAX_IRQ_ROUTES)
333                         goto out;
334                 if (ue->flags)
335                         goto out;
336                 r = -ENOMEM;
337                 e = kzalloc(sizeof(*e), GFP_KERNEL);
338                 if (!e)
339                         goto out;
340                 r = setup_routing_entry(e, ue);
341                 if (r)
342                         goto out;
343                 ++ue;
344                 list_add(&e->link, &irq_list);
345                 e = NULL;
346         }
347
348         mutex_lock(&kvm->irq_lock);
349         list_splice(&kvm->irq_routing, &tmp);
350         INIT_LIST_HEAD(&kvm->irq_routing);
351         list_splice(&irq_list, &kvm->irq_routing);
352         INIT_LIST_HEAD(&irq_list);
353         list_splice(&tmp, &irq_list);
354         mutex_unlock(&kvm->irq_lock);
355
356         r = 0;
357
358 out:
359         kfree(e);
360         __kvm_free_irq_routing(&irq_list);
361         return r;
362 }
363
364 #define IOAPIC_ROUTING_ENTRY(irq) \
365         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
366           .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
367 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
368
369 #ifdef CONFIG_X86
370 #  define PIC_ROUTING_ENTRY(irq) \
371         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
372           .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
373 #  define ROUTING_ENTRY2(irq) \
374         IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
375 #else
376 #  define ROUTING_ENTRY2(irq) \
377         IOAPIC_ROUTING_ENTRY(irq)
378 #endif
379
380 static const struct kvm_irq_routing_entry default_routing[] = {
381         ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
382         ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
383         ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
384         ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
385         ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
386         ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
387         ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
388         ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
389         ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
390         ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
391         ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
392         ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
393 #ifdef CONFIG_IA64
394         ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
395         ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
396         ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
397         ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
398         ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
399         ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
400         ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
401         ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
402         ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
403         ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
404         ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
405         ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
406 #endif
407 };
408
409 int kvm_setup_default_irq_routing(struct kvm *kvm)
410 {
411         return kvm_set_irq_routing(kvm, default_routing,
412                                    ARRAY_SIZE(default_routing), 0);
413 }