KVM: make irq ack notifications aware of routing table
[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 #include "irq.h"
24
25 #include "ioapic.h"
26
27 static void kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
28                             struct kvm *kvm, int level)
29 {
30 #ifdef CONFIG_X86
31         kvm_pic_set_irq(pic_irqchip(kvm), e->irqchip.pin, level);
32 #endif
33 }
34
35 static void kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
36                                struct kvm *kvm, int level)
37 {
38         kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
39 }
40
41 /* This should be called with the kvm->lock mutex held */
42 void kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
43 {
44         struct kvm_kernel_irq_routing_entry *e;
45         unsigned long *irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
46
47         /* Logical OR for level trig interrupt */
48         if (level)
49                 set_bit(irq_source_id, irq_state);
50         else
51                 clear_bit(irq_source_id, irq_state);
52
53         /* Not possible to detect if the guest uses the PIC or the
54          * IOAPIC.  So set the bit in both. The guest will ignore
55          * writes to the unused one.
56          */
57         list_for_each_entry(e, &kvm->irq_routing, link)
58                 if (e->gsi == irq)
59                         e->set(e, kvm, !!(*irq_state));
60 }
61
62 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
63 {
64         struct kvm_kernel_irq_routing_entry *e;
65         struct kvm_irq_ack_notifier *kian;
66         struct hlist_node *n;
67         unsigned gsi = pin;
68
69         list_for_each_entry(e, &kvm->irq_routing, link)
70                 if (e->irqchip.irqchip == irqchip &&
71                     e->irqchip.pin == pin) {
72                         gsi = e->gsi;
73                         break;
74                 }
75
76         hlist_for_each_entry(kian, n, &kvm->arch.irq_ack_notifier_list, link)
77                 if (kian->gsi == gsi)
78                         kian->irq_acked(kian);
79 }
80
81 void kvm_register_irq_ack_notifier(struct kvm *kvm,
82                                    struct kvm_irq_ack_notifier *kian)
83 {
84         hlist_add_head(&kian->link, &kvm->arch.irq_ack_notifier_list);
85 }
86
87 void kvm_unregister_irq_ack_notifier(struct kvm_irq_ack_notifier *kian)
88 {
89         hlist_del_init(&kian->link);
90 }
91
92 /* The caller must hold kvm->lock mutex */
93 int kvm_request_irq_source_id(struct kvm *kvm)
94 {
95         unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
96         int irq_source_id = find_first_zero_bit(bitmap,
97                                 sizeof(kvm->arch.irq_sources_bitmap));
98
99         if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
100                 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
101                 return -EFAULT;
102         }
103
104         ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
105         set_bit(irq_source_id, bitmap);
106
107         return irq_source_id;
108 }
109
110 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
111 {
112         int i;
113
114         ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
115
116         if (irq_source_id < 0 ||
117             irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
118                 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
119                 return;
120         }
121         for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++)
122                 clear_bit(irq_source_id, &kvm->arch.irq_states[i]);
123         clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
124 }
125
126 void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
127                                     struct kvm_irq_mask_notifier *kimn)
128 {
129         kimn->irq = irq;
130         hlist_add_head(&kimn->link, &kvm->mask_notifier_list);
131 }
132
133 void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
134                                       struct kvm_irq_mask_notifier *kimn)
135 {
136         hlist_del(&kimn->link);
137 }
138
139 void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
140 {
141         struct kvm_irq_mask_notifier *kimn;
142         struct hlist_node *n;
143
144         hlist_for_each_entry(kimn, n, &kvm->mask_notifier_list, link)
145                 if (kimn->irq == irq)
146                         kimn->func(kimn, mask);
147 }
148
149 static void __kvm_free_irq_routing(struct list_head *irq_routing)
150 {
151         struct kvm_kernel_irq_routing_entry *e, *n;
152
153         list_for_each_entry_safe(e, n, irq_routing, link)
154                 kfree(e);
155 }
156
157 void kvm_free_irq_routing(struct kvm *kvm)
158 {
159         __kvm_free_irq_routing(&kvm->irq_routing);
160 }
161
162 int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
163                         const struct kvm_irq_routing_entry *ue)
164 {
165         int r = -EINVAL;
166         int delta;
167
168         e->gsi = ue->gsi;
169         switch (ue->type) {
170         case KVM_IRQ_ROUTING_IRQCHIP:
171                 delta = 0;
172                 switch (ue->u.irqchip.irqchip) {
173                 case KVM_IRQCHIP_PIC_MASTER:
174                         e->set = kvm_set_pic_irq;
175                         break;
176                 case KVM_IRQCHIP_PIC_SLAVE:
177                                 e->set = kvm_set_pic_irq;
178                         delta = 8;
179                         break;
180                 case KVM_IRQCHIP_IOAPIC:
181                                 e->set = kvm_set_ioapic_irq;
182                         break;
183                 default:
184                         goto out;
185                 }
186                 e->irqchip.irqchip = ue->u.irqchip.irqchip;
187                 e->irqchip.pin = ue->u.irqchip.pin + delta;
188                 break;
189         default:
190                 goto out;
191         }
192         r = 0;
193 out:
194         return r;
195 }
196
197
198 int kvm_set_irq_routing(struct kvm *kvm,
199                         const struct kvm_irq_routing_entry *ue,
200                         unsigned nr,
201                         unsigned flags)
202 {
203         struct list_head irq_list = LIST_HEAD_INIT(irq_list);
204         struct list_head tmp = LIST_HEAD_INIT(tmp);
205         struct kvm_kernel_irq_routing_entry *e = NULL;
206         unsigned i;
207         int r;
208
209         for (i = 0; i < nr; ++i) {
210                 r = -EINVAL;
211                 if (ue->gsi >= KVM_MAX_IRQ_ROUTES)
212                         goto out;
213                 if (ue->flags)
214                         goto out;
215                 r = -ENOMEM;
216                 e = kzalloc(sizeof(*e), GFP_KERNEL);
217                 if (!e)
218                         goto out;
219                 r = setup_routing_entry(e, ue);
220                 if (r)
221                         goto out;
222                 ++ue;
223                 list_add(&e->link, &irq_list);
224                 e = NULL;
225         }
226
227         mutex_lock(&kvm->lock);
228         list_splice(&kvm->irq_routing, &tmp);
229         INIT_LIST_HEAD(&kvm->irq_routing);
230         list_splice(&irq_list, &kvm->irq_routing);
231         INIT_LIST_HEAD(&irq_list);
232         list_splice(&tmp, &irq_list);
233         mutex_unlock(&kvm->lock);
234
235         r = 0;
236
237 out:
238         kfree(e);
239         __kvm_free_irq_routing(&irq_list);
240         return r;
241 }
242
243 #define IOAPIC_ROUTING_ENTRY(irq) \
244         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
245           .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
246 #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
247
248 #ifdef CONFIG_X86
249 #  define PIC_ROUTING_ENTRY(irq) \
250         { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,  \
251           .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
252 #  define ROUTING_ENTRY2(irq) \
253         IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
254 #else
255 #  define ROUTING_ENTRY2(irq) \
256         IOAPIC_ROUTING_ENTRY(irq)
257 #endif
258
259 static const struct kvm_irq_routing_entry default_routing[] = {
260         ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
261         ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
262         ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
263         ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
264         ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
265         ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
266         ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
267         ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
268         ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
269         ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
270         ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
271         ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
272 #ifdef CONFIG_IA64
273         ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
274         ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
275         ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
276         ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
277         ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
278         ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
279         ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
280         ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
281         ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
282         ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
283         ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
284         ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
285 #endif
286 };
287
288 int kvm_setup_default_irq_routing(struct kvm *kvm)
289 {
290         return kvm_set_irq_routing(kvm, default_routing,
291                                    ARRAY_SIZE(default_routing), 0);
292 }