aboutsummaryrefslogtreecommitdiffstats
path: root/virt/kvm
diff options
context:
space:
mode:
authorAvi Kivity <avi@redhat.com>2008-11-19 06:58:46 -0500
committerAvi Kivity <avi@redhat.com>2009-03-24 05:03:06 -0400
commit399ec807ddc38ecccf8c06dbde04531cbdc63e11 (patch)
tree75a4e3ee9cfffb4ecf7b4608bb592c89f2b62219 /virt/kvm
parent193554750441d91e127dd5066b8aebe0f769101c (diff)
KVM: Userspace controlled irq routing
Currently KVM has a static routing from GSI numbers to interrupts (namely, 0-15 are mapped 1:1 to both PIC and IOAPIC, and 16:23 are mapped 1:1 to the IOAPIC). This is insufficient for several reasons: - HPET requires non 1:1 mapping for the timer interrupt - MSIs need a new method to assign interrupt numbers and dispatch them - ACPI APIC mode needs to be able to reassign the PCI LINK interrupts to the ioapics This patch implements an interrupt routing table (as a linked list, but this can be easily changed) and a userspace interface to replace the table. The routing table is initialized according to the current hardwired mapping. Signed-off-by: Avi Kivity <avi@redhat.com>
Diffstat (limited to 'virt/kvm')
-rw-r--r--virt/kvm/irq_comm.c168
-rw-r--r--virt/kvm/kvm_main.c36
2 files changed, 200 insertions, 4 deletions
diff --git a/virt/kvm/irq_comm.c b/virt/kvm/irq_comm.c
index 5162a411e4d2..a797fa5e6420 100644
--- a/virt/kvm/irq_comm.c
+++ b/virt/kvm/irq_comm.c
@@ -24,9 +24,24 @@
24 24
25#include "ioapic.h" 25#include "ioapic.h"
26 26
27static 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
27/* This should be called with the kvm->lock mutex held */ 41/* This should be called with the kvm->lock mutex held */
28void kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level) 42void kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
29{ 43{
44 struct kvm_kernel_irq_routing_entry *e;
30 unsigned long *irq_state = (unsigned long *)&kvm->arch.irq_states[irq]; 45 unsigned long *irq_state = (unsigned long *)&kvm->arch.irq_states[irq];
31 46
32 /* Logical OR for level trig interrupt */ 47 /* Logical OR for level trig interrupt */
@@ -39,10 +54,9 @@ void kvm_set_irq(struct kvm *kvm, int irq_source_id, int irq, int level)
39 * IOAPIC. So set the bit in both. The guest will ignore 54 * IOAPIC. So set the bit in both. The guest will ignore
40 * writes to the unused one. 55 * writes to the unused one.
41 */ 56 */
42 kvm_ioapic_set_irq(kvm->arch.vioapic, irq, !!(*irq_state)); 57 list_for_each_entry(e, &kvm->irq_routing, link)
43#ifdef CONFIG_X86 58 if (e->gsi == irq)
44 kvm_pic_set_irq(pic_irqchip(kvm), irq, !!(*irq_state)); 59 e->set(e, kvm, !!(*irq_state));
45#endif
46} 60}
47 61
48void kvm_notify_acked_irq(struct kvm *kvm, unsigned gsi) 62void kvm_notify_acked_irq(struct kvm *kvm, unsigned gsi)
@@ -123,3 +137,149 @@ void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
123 kimn->func(kimn, mask); 137 kimn->func(kimn, mask);
124} 138}
125 139
140static 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}
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 786a3ae373b0..c65484b471c6 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -843,6 +843,7 @@ static struct kvm *kvm_create_vm(void)
843 if (IS_ERR(kvm)) 843 if (IS_ERR(kvm))
844 goto out; 844 goto out;
845#ifdef CONFIG_HAVE_KVM_IRQCHIP 845#ifdef CONFIG_HAVE_KVM_IRQCHIP
846 INIT_LIST_HEAD(&kvm->irq_routing);
846 INIT_HLIST_HEAD(&kvm->mask_notifier_list); 847 INIT_HLIST_HEAD(&kvm->mask_notifier_list);
847#endif 848#endif
848 849
@@ -926,6 +927,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
926 spin_lock(&kvm_lock); 927 spin_lock(&kvm_lock);
927 list_del(&kvm->vm_list); 928 list_del(&kvm->vm_list);
928 spin_unlock(&kvm_lock); 929 spin_unlock(&kvm_lock);
930 kvm_free_irq_routing(kvm);
929 kvm_io_bus_destroy(&kvm->pio_bus); 931 kvm_io_bus_destroy(&kvm->pio_bus);
930 kvm_io_bus_destroy(&kvm->mmio_bus); 932 kvm_io_bus_destroy(&kvm->mmio_bus);
931#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET 933#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
@@ -1946,6 +1948,36 @@ static long kvm_vm_ioctl(struct file *filp,
1946 break; 1948 break;
1947 } 1949 }
1948#endif 1950#endif
1951#ifdef KVM_CAP_IRQ_ROUTING
1952 case KVM_SET_GSI_ROUTING: {
1953 struct kvm_irq_routing routing;
1954 struct kvm_irq_routing __user *urouting;
1955 struct kvm_irq_routing_entry *entries;
1956
1957 r = -EFAULT;
1958 if (copy_from_user(&routing, argp, sizeof(routing)))
1959 goto out;
1960 r = -EINVAL;
1961 if (routing.nr >= KVM_MAX_IRQ_ROUTES)
1962 goto out;
1963 if (routing.flags)
1964 goto out;
1965 r = -ENOMEM;
1966 entries = vmalloc(routing.nr * sizeof(*entries));
1967 if (!entries)
1968 goto out;
1969 r = -EFAULT;
1970 urouting = argp;
1971 if (copy_from_user(entries, urouting->entries,
1972 routing.nr * sizeof(*entries)))
1973 goto out_free_irq_routing;
1974 r = kvm_set_irq_routing(kvm, entries, routing.nr,
1975 routing.flags);
1976 out_free_irq_routing:
1977 vfree(entries);
1978 break;
1979 }
1980#endif
1949 default: 1981 default:
1950 r = kvm_arch_vm_ioctl(filp, ioctl, arg); 1982 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
1951 } 1983 }
@@ -2012,6 +2044,10 @@ static long kvm_dev_ioctl_check_extension_generic(long arg)
2012 case KVM_CAP_USER_MEMORY: 2044 case KVM_CAP_USER_MEMORY:
2013 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: 2045 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2014 return 1; 2046 return 1;
2047#ifdef CONFIG_HAVE_KVM_IRQCHIP
2048 case KVM_CAP_IRQ_ROUTING:
2049 return 1;
2050#endif
2015 default: 2051 default:
2016 break; 2052 break;
2017 } 2053 }