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