blob: a0e88809e45e7468d9c2439d638f2dc18502d128 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030024#include <trace/events/kvm.h>
Sheng Yang79950e12009-02-10 13:57:06 +080025
Sheng Yang79950e12009-02-10 13:57:06 +080026#include <asm/msidef.h>
Gleb Natapov58c2dde2009-03-05 16:35:04 +020027#ifdef CONFIG_IA64
28#include <asm/iosapic.h>
29#endif
Sheng Yang79950e12009-02-10 13:57:06 +080030
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080031#include "irq.h"
32
33#include "ioapic.h"
34
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030035static inline int kvm_irq_line_state(unsigned long *irq_state,
36 int irq_source_id, int level)
37{
38 /* Logical OR for level trig interrupt */
39 if (level)
40 set_bit(irq_source_id, irq_state);
41 else
42 clear_bit(irq_source_id, irq_state);
43
44 return !!(*irq_state);
45}
46
Gleb Natapov49256632009-02-04 17:28:14 +020047static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030048 struct kvm *kvm, int irq_source_id, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020049{
50#ifdef CONFIG_X86
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030051 struct kvm_pic *pic = pic_irqchip(kvm);
52 level = kvm_irq_line_state(&pic->irq_states[e->irqchip.pin],
53 irq_source_id, level);
54 return kvm_pic_set_irq(pic, e->irqchip.pin, level);
Gleb Natapov49256632009-02-04 17:28:14 +020055#else
56 return -1;
Avi Kivity399ec802008-11-19 13:58:46 +020057#endif
58}
59
Gleb Natapov49256632009-02-04 17:28:14 +020060static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030061 struct kvm *kvm, int irq_source_id, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020062{
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030063 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
64 level = kvm_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
65 irq_source_id, level);
66
67 return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
Avi Kivity399ec802008-11-19 13:58:46 +020068}
69
Gleb Natapov58c2dde2009-03-05 16:35:04 +020070inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
Sheng Yang116191b2009-02-11 16:03:37 +080071{
Gleb Natapov58c2dde2009-03-05 16:35:04 +020072#ifdef CONFIG_IA64
73 return irq->delivery_mode ==
74 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
75#else
76 return irq->delivery_mode == APIC_DM_LOWEST;
77#endif
78}
Sheng Yang116191b2009-02-11 16:03:37 +080079
Gleb Natapov58c2dde2009-03-05 16:35:04 +020080int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
81 struct kvm_lapic_irq *irq)
82{
83 int i, r = -1;
84 struct kvm_vcpu *vcpu, *lowest = NULL;
85
86 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
87 kvm_is_dm_lowest_prio(irq))
Gleb Natapov343f94f2009-03-05 16:34:54 +020088 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
89
Gleb Natapov988a2ca2009-06-09 15:56:29 +030090 kvm_for_each_vcpu(i, vcpu, kvm) {
91 if (!kvm_apic_present(vcpu))
Gleb Natapov343f94f2009-03-05 16:34:54 +020092 continue;
Sheng Yang74a3a8f2009-03-04 13:33:02 +080093
Gleb Natapov58c2dde2009-03-05 16:35:04 +020094 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
95 irq->dest_id, irq->dest_mode))
Gleb Natapov343f94f2009-03-05 16:34:54 +020096 continue;
97
Gleb Natapov58c2dde2009-03-05 16:35:04 +020098 if (!kvm_is_dm_lowest_prio(irq)) {
99 if (r < 0)
100 r = 0;
101 r += kvm_apic_set_irq(vcpu, irq);
Gleb Natapove1035712009-03-05 16:34:59 +0200102 } else {
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200103 if (!lowest)
104 lowest = vcpu;
105 else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
106 lowest = vcpu;
Gleb Natapove1035712009-03-05 16:34:59 +0200107 }
Gleb Natapov343f94f2009-03-05 16:34:54 +0200108 }
109
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200110 if (lowest)
111 r = kvm_apic_set_irq(lowest, irq);
112
113 return r;
Sheng Yang116191b2009-02-11 16:03:37 +0800114}
115
Gleb Natapov49256632009-02-04 17:28:14 +0200116static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300117 struct kvm *kvm, int irq_source_id, int level)
Sheng Yang79950e12009-02-10 13:57:06 +0800118{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200119 struct kvm_lapic_irq irq;
Sheng Yang79950e12009-02-10 13:57:06 +0800120
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300121 if (!level)
122 return -1;
123
Gleb Natapov1000ff82009-07-07 16:00:57 +0300124 trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data);
125
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200126 irq.dest_id = (e->msi.address_lo &
Sheng Yang116191b2009-02-11 16:03:37 +0800127 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200128 irq.vector = (e->msi.data &
Sheng Yang116191b2009-02-11 16:03:37 +0800129 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200130 irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
131 irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
132 irq.delivery_mode = e->msi.data & 0x700;
133 irq.level = 1;
134 irq.shorthand = 0;
Sheng Yang116191b2009-02-11 16:03:37 +0800135
136 /* TODO Deal with RH bit of MSI message address */
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200137 return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
Sheng Yang79950e12009-02-10 13:57:06 +0800138}
139
Gleb Natapov680b3642009-08-24 11:54:26 +0300140/*
Gleb Natapov49256632009-02-04 17:28:14 +0200141 * Return value:
142 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
143 * = 0 Interrupt was coalesced (previous irq is still pending)
144 * > 0 Number of CPUs interrupt was delivered to
145 */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300146int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800147{
Gleb Natapoveba02262009-08-24 11:54:25 +0300148 struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
149 int ret = -1, i = 0;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300150 struct kvm_irq_routing_table *irq_rt;
151 struct hlist_node *n;
Sheng Yang5550af42008-10-15 20:15:06 +0800152
Avi Kivityae8c1c42009-07-01 12:09:41 +0300153 trace_kvm_set_irq(irq, level, irq_source_id);
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300154
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800155 /* Not possible to detect if the guest uses the PIC or the
156 * IOAPIC. So set the bit in both. The guest will ignore
157 * writes to the unused one.
158 */
Gleb Natapove42bba92009-08-24 11:54:22 +0300159 rcu_read_lock();
160 irq_rt = rcu_dereference(kvm->irq_routing);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300161 if (irq < irq_rt->nr_rt_entries)
Gleb Natapoveba02262009-08-24 11:54:25 +0300162 hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
163 irq_set[i++] = *e;
Gleb Natapove42bba92009-08-24 11:54:22 +0300164 rcu_read_unlock();
Gleb Natapoveba02262009-08-24 11:54:25 +0300165
166 while(i--) {
167 int r;
168 r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level);
169 if (r < 0)
170 continue;
171
172 ret = r + ((ret < 0) ? 0 : ret);
173 }
174
Gleb Natapov49256632009-02-04 17:28:14 +0200175 return ret;
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800176}
177
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200178void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800179{
180 struct kvm_irq_ack_notifier *kian;
181 struct hlist_node *n;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300182 int gsi;
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200183
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300184 trace_kvm_ack_irq(irqchip, pin);
185
Gleb Natapove42bba92009-08-24 11:54:22 +0300186 rcu_read_lock();
187 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
Gleb Natapov3e71f882009-08-24 11:54:21 +0300188 if (gsi != -1)
Gleb Natapov280aa172009-08-24 11:54:24 +0300189 hlist_for_each_entry_rcu(kian, n, &kvm->irq_ack_notifier_list,
190 link)
Gleb Natapov3e71f882009-08-24 11:54:21 +0300191 if (kian->gsi == gsi)
192 kian->irq_acked(kian);
Gleb Natapov280aa172009-08-24 11:54:24 +0300193 rcu_read_unlock();
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800194}
195
196void kvm_register_irq_ack_notifier(struct kvm *kvm,
197 struct kvm_irq_ack_notifier *kian)
198{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300199 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300200 hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300201 mutex_unlock(&kvm->irq_lock);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800202}
203
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300204void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
205 struct kvm_irq_ack_notifier *kian)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800206{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300207 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300208 hlist_del_init_rcu(&kian->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300209 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300210 synchronize_rcu();
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800211}
Sheng Yang5550af42008-10-15 20:15:06 +0800212
Sheng Yang5550af42008-10-15 20:15:06 +0800213int kvm_request_irq_source_id(struct kvm *kvm)
214{
215 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300216 int irq_source_id;
217
218 mutex_lock(&kvm->irq_lock);
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300219 irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
Mark McLoughlin61552362008-12-01 13:57:48 +0000220
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300221 if (irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800222 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200223 irq_source_id = -EFAULT;
224 goto unlock;
Mark McLoughlin61552362008-12-01 13:57:48 +0000225 }
226
227 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
228 set_bit(irq_source_id, bitmap);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200229unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300230 mutex_unlock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000231
Sheng Yang5550af42008-10-15 20:15:06 +0800232 return irq_source_id;
233}
234
235void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
236{
237 int i;
238
Mark McLoughlin61552362008-12-01 13:57:48 +0000239 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
240
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300241 mutex_lock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000242 if (irq_source_id < 0 ||
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300243 irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800244 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200245 goto unlock;
Sheng Yang5550af42008-10-15 20:15:06 +0800246 }
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200247 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
248 if (!irqchip_in_kernel(kvm))
249 goto unlock;
250
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300251 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++) {
252 clear_bit(irq_source_id, &kvm->arch.vioapic->irq_states[i]);
253 if (i >= 16)
254 continue;
255#ifdef CONFIG_X86
256 clear_bit(irq_source_id, &pic_irqchip(kvm)->irq_states[i]);
257#endif
258 }
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200259unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300260 mutex_unlock(&kvm->irq_lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800261}
Avi Kivity75858a82009-01-04 17:10:50 +0200262
263void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
264 struct kvm_irq_mask_notifier *kimn)
265{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300266 mutex_lock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200267 kimn->irq = irq;
Gleb Natapov280aa172009-08-24 11:54:24 +0300268 hlist_add_head_rcu(&kimn->link, &kvm->mask_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300269 mutex_unlock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200270}
271
272void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
273 struct kvm_irq_mask_notifier *kimn)
274{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300275 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300276 hlist_del_rcu(&kimn->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300277 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300278 synchronize_rcu();
Avi Kivity75858a82009-01-04 17:10:50 +0200279}
280
281void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
282{
283 struct kvm_irq_mask_notifier *kimn;
284 struct hlist_node *n;
285
Gleb Natapov280aa172009-08-24 11:54:24 +0300286 rcu_read_lock();
287 hlist_for_each_entry_rcu(kimn, n, &kvm->mask_notifier_list, link)
Avi Kivity75858a82009-01-04 17:10:50 +0200288 if (kimn->irq == irq)
289 kimn->func(kimn, mask);
Gleb Natapov280aa172009-08-24 11:54:24 +0300290 rcu_read_unlock();
Avi Kivity75858a82009-01-04 17:10:50 +0200291}
292
Avi Kivity399ec802008-11-19 13:58:46 +0200293void kvm_free_irq_routing(struct kvm *kvm)
294{
Gleb Natapove42bba92009-08-24 11:54:22 +0300295 /* Called only during vm destruction. Nobody can use the pointer
296 at this stage */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300297 kfree(kvm->irq_routing);
Avi Kivity399ec802008-11-19 13:58:46 +0200298}
299
Gleb Natapov46e624b2009-08-24 11:54:20 +0300300static int setup_routing_entry(struct kvm_irq_routing_table *rt,
301 struct kvm_kernel_irq_routing_entry *e,
Hannes Edercded19f2009-02-21 02:19:13 +0100302 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200303{
304 int r = -EINVAL;
305 int delta;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200306 unsigned max_pin;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300307 struct kvm_kernel_irq_routing_entry *ei;
308 struct hlist_node *n;
309
310 /*
311 * Do not allow GSI to be mapped to the same irqchip more than once.
312 * Allow only one to one mapping between GSI and MSI.
313 */
314 hlist_for_each_entry(ei, n, &rt->map[ue->gsi], link)
315 if (ei->type == KVM_IRQ_ROUTING_MSI ||
316 ue->u.irqchip.irqchip == ei->irqchip.irqchip)
317 return r;
Avi Kivity399ec802008-11-19 13:58:46 +0200318
319 e->gsi = ue->gsi;
Michael S. Tsirkin5116d8f2009-07-26 17:10:01 +0300320 e->type = ue->type;
Avi Kivity399ec802008-11-19 13:58:46 +0200321 switch (ue->type) {
322 case KVM_IRQ_ROUTING_IRQCHIP:
323 delta = 0;
324 switch (ue->u.irqchip.irqchip) {
325 case KVM_IRQCHIP_PIC_MASTER:
326 e->set = kvm_set_pic_irq;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200327 max_pin = 16;
Avi Kivity399ec802008-11-19 13:58:46 +0200328 break;
329 case KVM_IRQCHIP_PIC_SLAVE:
Gleb Natapov49256632009-02-04 17:28:14 +0200330 e->set = kvm_set_pic_irq;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200331 max_pin = 16;
Avi Kivity399ec802008-11-19 13:58:46 +0200332 delta = 8;
333 break;
334 case KVM_IRQCHIP_IOAPIC:
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200335 max_pin = KVM_IOAPIC_NUM_PINS;
Chris Wrightefbc1002009-05-01 14:15:43 -0700336 e->set = kvm_set_ioapic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200337 break;
338 default:
339 goto out;
340 }
341 e->irqchip.irqchip = ue->u.irqchip.irqchip;
342 e->irqchip.pin = ue->u.irqchip.pin + delta;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200343 if (e->irqchip.pin >= max_pin)
Gleb Natapov3e71f882009-08-24 11:54:21 +0300344 goto out;
345 rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi;
Avi Kivity399ec802008-11-19 13:58:46 +0200346 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800347 case KVM_IRQ_ROUTING_MSI:
348 e->set = kvm_set_msi;
349 e->msi.address_lo = ue->u.msi.address_lo;
350 e->msi.address_hi = ue->u.msi.address_hi;
351 e->msi.data = ue->u.msi.data;
352 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200353 default:
354 goto out;
355 }
Gleb Natapov46e624b2009-08-24 11:54:20 +0300356
357 hlist_add_head(&e->link, &rt->map[e->gsi]);
Avi Kivity399ec802008-11-19 13:58:46 +0200358 r = 0;
359out:
360 return r;
361}
362
363
364int kvm_set_irq_routing(struct kvm *kvm,
365 const struct kvm_irq_routing_entry *ue,
366 unsigned nr,
367 unsigned flags)
368{
Gleb Natapov46e624b2009-08-24 11:54:20 +0300369 struct kvm_irq_routing_table *new, *old;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300370 u32 i, j, nr_rt_entries = 0;
Avi Kivity399ec802008-11-19 13:58:46 +0200371 int r;
372
373 for (i = 0; i < nr; ++i) {
Gleb Natapov46e624b2009-08-24 11:54:20 +0300374 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
375 return -EINVAL;
376 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
377 }
378
379 nr_rt_entries += 1;
380
381 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
382 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
383 GFP_KERNEL);
384
385 if (!new)
386 return -ENOMEM;
387
388 new->rt_entries = (void *)&new->map[nr_rt_entries];
389
390 new->nr_rt_entries = nr_rt_entries;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300391 for (i = 0; i < 3; i++)
392 for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++)
393 new->chip[i][j] = -1;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300394
395 for (i = 0; i < nr; ++i) {
Avi Kivity399ec802008-11-19 13:58:46 +0200396 r = -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200397 if (ue->flags)
398 goto out;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300399 r = setup_routing_entry(new, &new->rt_entries[i], ue);
Avi Kivity399ec802008-11-19 13:58:46 +0200400 if (r)
401 goto out;
402 ++ue;
Avi Kivity399ec802008-11-19 13:58:46 +0200403 }
404
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300405 mutex_lock(&kvm->irq_lock);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300406 old = kvm->irq_routing;
Gleb Natapove42bba92009-08-24 11:54:22 +0300407 rcu_assign_pointer(kvm->irq_routing, new);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300408 mutex_unlock(&kvm->irq_lock);
Gleb Natapove42bba92009-08-24 11:54:22 +0300409 synchronize_rcu();
Avi Kivity399ec802008-11-19 13:58:46 +0200410
Gleb Natapov46e624b2009-08-24 11:54:20 +0300411 new = old;
Avi Kivity399ec802008-11-19 13:58:46 +0200412 r = 0;
413
414out:
Gleb Natapov46e624b2009-08-24 11:54:20 +0300415 kfree(new);
Avi Kivity399ec802008-11-19 13:58:46 +0200416 return r;
417}
418
419#define IOAPIC_ROUTING_ENTRY(irq) \
420 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
421 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
422#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
423
424#ifdef CONFIG_X86
Avi Kivity399ec802008-11-19 13:58:46 +0200425# define PIC_ROUTING_ENTRY(irq) \
426 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
427 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
428# define ROUTING_ENTRY2(irq) \
429 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
430#else
431# define ROUTING_ENTRY2(irq) \
432 IOAPIC_ROUTING_ENTRY(irq)
433#endif
434
435static const struct kvm_irq_routing_entry default_routing[] = {
436 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
437 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
438 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
439 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
440 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
441 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
442 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
443 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
444 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
445 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
446 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
447 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
448#ifdef CONFIG_IA64
449 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
450 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
451 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
452 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
453 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
454 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
455 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
456 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
457 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
458 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
459 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
460 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
461#endif
462};
463
464int kvm_setup_default_irq_routing(struct kvm *kvm)
465{
466 return kvm_set_irq_routing(kvm, default_routing,
467 ARRAY_SIZE(default_routing), 0);
468}