blob: 00c68d29857972ec4424817a85a328178a5c85ed [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>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030023#include <trace/events/kvm.h>
Sheng Yang79950e12009-02-10 13:57:06 +080024
Sheng Yang79950e12009-02-10 13:57:06 +080025#include <asm/msidef.h>
Gleb Natapov58c2dde2009-03-05 16:35:04 +020026#ifdef CONFIG_IA64
27#include <asm/iosapic.h>
28#endif
Sheng Yang79950e12009-02-10 13:57:06 +080029
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080030#include "irq.h"
31
32#include "ioapic.h"
33
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030034static inline int kvm_irq_line_state(unsigned long *irq_state,
35 int irq_source_id, int level)
36{
37 /* Logical OR for level trig interrupt */
38 if (level)
39 set_bit(irq_source_id, irq_state);
40 else
41 clear_bit(irq_source_id, irq_state);
42
43 return !!(*irq_state);
44}
45
Gleb Natapov49256632009-02-04 17:28:14 +020046static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030047 struct kvm *kvm, int irq_source_id, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020048{
49#ifdef CONFIG_X86
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030050 struct kvm_pic *pic = pic_irqchip(kvm);
51 level = kvm_irq_line_state(&pic->irq_states[e->irqchip.pin],
52 irq_source_id, level);
53 return kvm_pic_set_irq(pic, e->irqchip.pin, level);
Gleb Natapov49256632009-02-04 17:28:14 +020054#else
55 return -1;
Avi Kivity399ec802008-11-19 13:58:46 +020056#endif
57}
58
Gleb Natapov49256632009-02-04 17:28:14 +020059static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030060 struct kvm *kvm, int irq_source_id, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020061{
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030062 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
63 level = kvm_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
64 irq_source_id, level);
65
66 return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
Avi Kivity399ec802008-11-19 13:58:46 +020067}
68
Gleb Natapov58c2dde2009-03-05 16:35:04 +020069inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
Sheng Yang116191b2009-02-11 16:03:37 +080070{
Gleb Natapov58c2dde2009-03-05 16:35:04 +020071#ifdef CONFIG_IA64
72 return irq->delivery_mode ==
73 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
74#else
75 return irq->delivery_mode == APIC_DM_LOWEST;
76#endif
77}
Sheng Yang116191b2009-02-11 16:03:37 +080078
Gleb Natapov58c2dde2009-03-05 16:35:04 +020079int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
80 struct kvm_lapic_irq *irq)
81{
82 int i, r = -1;
83 struct kvm_vcpu *vcpu, *lowest = NULL;
84
85 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
86 kvm_is_dm_lowest_prio(irq))
Gleb Natapov343f94f2009-03-05 16:34:54 +020087 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
88
Gleb Natapov988a2ca2009-06-09 15:56:29 +030089 kvm_for_each_vcpu(i, vcpu, kvm) {
90 if (!kvm_apic_present(vcpu))
Gleb Natapov343f94f2009-03-05 16:34:54 +020091 continue;
Sheng Yang74a3a8f2009-03-04 13:33:02 +080092
Gleb Natapov58c2dde2009-03-05 16:35:04 +020093 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
94 irq->dest_id, irq->dest_mode))
Gleb Natapov343f94f2009-03-05 16:34:54 +020095 continue;
96
Gleb Natapov58c2dde2009-03-05 16:35:04 +020097 if (!kvm_is_dm_lowest_prio(irq)) {
98 if (r < 0)
99 r = 0;
100 r += kvm_apic_set_irq(vcpu, irq);
Gleb Natapove1035712009-03-05 16:34:59 +0200101 } else {
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200102 if (!lowest)
103 lowest = vcpu;
104 else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
105 lowest = vcpu;
Gleb Natapove1035712009-03-05 16:34:59 +0200106 }
Gleb Natapov343f94f2009-03-05 16:34:54 +0200107 }
108
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200109 if (lowest)
110 r = kvm_apic_set_irq(lowest, irq);
111
112 return r;
Sheng Yang116191b2009-02-11 16:03:37 +0800113}
114
Gleb Natapov49256632009-02-04 17:28:14 +0200115static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300116 struct kvm *kvm, int irq_source_id, int level)
Sheng Yang79950e12009-02-10 13:57:06 +0800117{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200118 struct kvm_lapic_irq irq;
Sheng Yang79950e12009-02-10 13:57:06 +0800119
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300120 if (!level)
121 return -1;
122
Gleb Natapov1000ff82009-07-07 16:00:57 +0300123 trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data);
124
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200125 irq.dest_id = (e->msi.address_lo &
Sheng Yang116191b2009-02-11 16:03:37 +0800126 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200127 irq.vector = (e->msi.data &
Sheng Yang116191b2009-02-11 16:03:37 +0800128 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200129 irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
130 irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
131 irq.delivery_mode = e->msi.data & 0x700;
132 irq.level = 1;
133 irq.shorthand = 0;
Sheng Yang116191b2009-02-11 16:03:37 +0800134
135 /* TODO Deal with RH bit of MSI message address */
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200136 return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
Sheng Yang79950e12009-02-10 13:57:06 +0800137}
138
Gleb Natapov680b3642009-08-24 11:54:26 +0300139/*
Gleb Natapov49256632009-02-04 17:28:14 +0200140 * Return value:
141 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
142 * = 0 Interrupt was coalesced (previous irq is still pending)
143 * > 0 Number of CPUs interrupt was delivered to
144 */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300145int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800146{
Gleb Natapoveba02262009-08-24 11:54:25 +0300147 struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
148 int ret = -1, i = 0;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300149 struct kvm_irq_routing_table *irq_rt;
150 struct hlist_node *n;
Sheng Yang5550af42008-10-15 20:15:06 +0800151
Avi Kivityae8c1c42009-07-01 12:09:41 +0300152 trace_kvm_set_irq(irq, level, irq_source_id);
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300153
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800154 /* Not possible to detect if the guest uses the PIC or the
155 * IOAPIC. So set the bit in both. The guest will ignore
156 * writes to the unused one.
157 */
Gleb Natapove42bba92009-08-24 11:54:22 +0300158 rcu_read_lock();
159 irq_rt = rcu_dereference(kvm->irq_routing);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300160 if (irq < irq_rt->nr_rt_entries)
Gleb Natapoveba02262009-08-24 11:54:25 +0300161 hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
162 irq_set[i++] = *e;
Gleb Natapove42bba92009-08-24 11:54:22 +0300163 rcu_read_unlock();
Gleb Natapoveba02262009-08-24 11:54:25 +0300164
165 while(i--) {
166 int r;
167 r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level);
168 if (r < 0)
169 continue;
170
171 ret = r + ((ret < 0) ? 0 : ret);
172 }
173
Gleb Natapov49256632009-02-04 17:28:14 +0200174 return ret;
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800175}
176
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200177void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800178{
179 struct kvm_irq_ack_notifier *kian;
180 struct hlist_node *n;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300181 int gsi;
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200182
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300183 trace_kvm_ack_irq(irqchip, pin);
184
Gleb Natapove42bba92009-08-24 11:54:22 +0300185 rcu_read_lock();
186 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
Gleb Natapov3e71f882009-08-24 11:54:21 +0300187 if (gsi != -1)
Gleb Natapov280aa172009-08-24 11:54:24 +0300188 hlist_for_each_entry_rcu(kian, n, &kvm->irq_ack_notifier_list,
189 link)
Gleb Natapov3e71f882009-08-24 11:54:21 +0300190 if (kian->gsi == gsi)
191 kian->irq_acked(kian);
Gleb Natapov280aa172009-08-24 11:54:24 +0300192 rcu_read_unlock();
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800193}
194
195void kvm_register_irq_ack_notifier(struct kvm *kvm,
196 struct kvm_irq_ack_notifier *kian)
197{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300198 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300199 hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300200 mutex_unlock(&kvm->irq_lock);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800201}
202
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300203void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
204 struct kvm_irq_ack_notifier *kian)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800205{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300206 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300207 hlist_del_init_rcu(&kian->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300208 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300209 synchronize_rcu();
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800210}
Sheng Yang5550af42008-10-15 20:15:06 +0800211
Sheng Yang5550af42008-10-15 20:15:06 +0800212int kvm_request_irq_source_id(struct kvm *kvm)
213{
214 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300215 int irq_source_id;
216
217 mutex_lock(&kvm->irq_lock);
218 irq_source_id = find_first_zero_bit(bitmap,
Sheng Yang5550af42008-10-15 20:15:06 +0800219 sizeof(kvm->arch.irq_sources_bitmap));
Mark McLoughlin61552362008-12-01 13:57:48 +0000220
Sheng Yang5550af42008-10-15 20:15:06 +0800221 if (irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
222 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 ||
Sheng Yang5550af42008-10-15 20:15:06 +0800243 irq_source_id >= sizeof(kvm->arch.irq_sources_bitmap)) {
244 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 }
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300247 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++) {
248 clear_bit(irq_source_id, &kvm->arch.vioapic->irq_states[i]);
249 if (i >= 16)
250 continue;
251#ifdef CONFIG_X86
252 clear_bit(irq_source_id, &pic_irqchip(kvm)->irq_states[i]);
253#endif
254 }
Sheng Yang5550af42008-10-15 20:15:06 +0800255 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200256unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300257 mutex_unlock(&kvm->irq_lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800258}
Avi Kivity75858a82009-01-04 17:10:50 +0200259
260void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
261 struct kvm_irq_mask_notifier *kimn)
262{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300263 mutex_lock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200264 kimn->irq = irq;
Gleb Natapov280aa172009-08-24 11:54:24 +0300265 hlist_add_head_rcu(&kimn->link, &kvm->mask_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300266 mutex_unlock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200267}
268
269void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
270 struct kvm_irq_mask_notifier *kimn)
271{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300272 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300273 hlist_del_rcu(&kimn->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300274 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300275 synchronize_rcu();
Avi Kivity75858a82009-01-04 17:10:50 +0200276}
277
278void kvm_fire_mask_notifiers(struct kvm *kvm, int irq, bool mask)
279{
280 struct kvm_irq_mask_notifier *kimn;
281 struct hlist_node *n;
282
Gleb Natapov280aa172009-08-24 11:54:24 +0300283 rcu_read_lock();
284 hlist_for_each_entry_rcu(kimn, n, &kvm->mask_notifier_list, link)
Avi Kivity75858a82009-01-04 17:10:50 +0200285 if (kimn->irq == irq)
286 kimn->func(kimn, mask);
Gleb Natapov280aa172009-08-24 11:54:24 +0300287 rcu_read_unlock();
Avi Kivity75858a82009-01-04 17:10:50 +0200288}
289
Avi Kivity399ec802008-11-19 13:58:46 +0200290void kvm_free_irq_routing(struct kvm *kvm)
291{
Gleb Natapove42bba92009-08-24 11:54:22 +0300292 /* Called only during vm destruction. Nobody can use the pointer
293 at this stage */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300294 kfree(kvm->irq_routing);
Avi Kivity399ec802008-11-19 13:58:46 +0200295}
296
Gleb Natapov46e624b2009-08-24 11:54:20 +0300297static int setup_routing_entry(struct kvm_irq_routing_table *rt,
298 struct kvm_kernel_irq_routing_entry *e,
Hannes Edercded19f2009-02-21 02:19:13 +0100299 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200300{
301 int r = -EINVAL;
302 int delta;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300303 struct kvm_kernel_irq_routing_entry *ei;
304 struct hlist_node *n;
305
306 /*
307 * Do not allow GSI to be mapped to the same irqchip more than once.
308 * Allow only one to one mapping between GSI and MSI.
309 */
310 hlist_for_each_entry(ei, n, &rt->map[ue->gsi], link)
311 if (ei->type == KVM_IRQ_ROUTING_MSI ||
312 ue->u.irqchip.irqchip == ei->irqchip.irqchip)
313 return r;
Avi Kivity399ec802008-11-19 13:58:46 +0200314
315 e->gsi = ue->gsi;
Michael S. Tsirkin5116d8f2009-07-26 17:10:01 +0300316 e->type = ue->type;
Avi Kivity399ec802008-11-19 13:58:46 +0200317 switch (ue->type) {
318 case KVM_IRQ_ROUTING_IRQCHIP:
319 delta = 0;
320 switch (ue->u.irqchip.irqchip) {
321 case KVM_IRQCHIP_PIC_MASTER:
322 e->set = kvm_set_pic_irq;
323 break;
324 case KVM_IRQCHIP_PIC_SLAVE:
Gleb Natapov49256632009-02-04 17:28:14 +0200325 e->set = kvm_set_pic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200326 delta = 8;
327 break;
328 case KVM_IRQCHIP_IOAPIC:
Chris Wrightefbc1002009-05-01 14:15:43 -0700329 e->set = kvm_set_ioapic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200330 break;
331 default:
332 goto out;
333 }
334 e->irqchip.irqchip = ue->u.irqchip.irqchip;
335 e->irqchip.pin = ue->u.irqchip.pin + delta;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300336 if (e->irqchip.pin >= KVM_IOAPIC_NUM_PINS)
337 goto out;
338 rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi;
Avi Kivity399ec802008-11-19 13:58:46 +0200339 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800340 case KVM_IRQ_ROUTING_MSI:
341 e->set = kvm_set_msi;
342 e->msi.address_lo = ue->u.msi.address_lo;
343 e->msi.address_hi = ue->u.msi.address_hi;
344 e->msi.data = ue->u.msi.data;
345 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200346 default:
347 goto out;
348 }
Gleb Natapov46e624b2009-08-24 11:54:20 +0300349
350 hlist_add_head(&e->link, &rt->map[e->gsi]);
Avi Kivity399ec802008-11-19 13:58:46 +0200351 r = 0;
352out:
353 return r;
354}
355
356
357int kvm_set_irq_routing(struct kvm *kvm,
358 const struct kvm_irq_routing_entry *ue,
359 unsigned nr,
360 unsigned flags)
361{
Gleb Natapov46e624b2009-08-24 11:54:20 +0300362 struct kvm_irq_routing_table *new, *old;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300363 u32 i, j, nr_rt_entries = 0;
Avi Kivity399ec802008-11-19 13:58:46 +0200364 int r;
365
366 for (i = 0; i < nr; ++i) {
Gleb Natapov46e624b2009-08-24 11:54:20 +0300367 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
368 return -EINVAL;
369 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
370 }
371
372 nr_rt_entries += 1;
373
374 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
375 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
376 GFP_KERNEL);
377
378 if (!new)
379 return -ENOMEM;
380
381 new->rt_entries = (void *)&new->map[nr_rt_entries];
382
383 new->nr_rt_entries = nr_rt_entries;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300384 for (i = 0; i < 3; i++)
385 for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++)
386 new->chip[i][j] = -1;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300387
388 for (i = 0; i < nr; ++i) {
Avi Kivity399ec802008-11-19 13:58:46 +0200389 r = -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200390 if (ue->flags)
391 goto out;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300392 r = setup_routing_entry(new, &new->rt_entries[i], ue);
Avi Kivity399ec802008-11-19 13:58:46 +0200393 if (r)
394 goto out;
395 ++ue;
Avi Kivity399ec802008-11-19 13:58:46 +0200396 }
397
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300398 mutex_lock(&kvm->irq_lock);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300399 old = kvm->irq_routing;
Gleb Natapove42bba92009-08-24 11:54:22 +0300400 rcu_assign_pointer(kvm->irq_routing, new);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300401 mutex_unlock(&kvm->irq_lock);
Gleb Natapove42bba92009-08-24 11:54:22 +0300402 synchronize_rcu();
Avi Kivity399ec802008-11-19 13:58:46 +0200403
Gleb Natapov46e624b2009-08-24 11:54:20 +0300404 new = old;
Avi Kivity399ec802008-11-19 13:58:46 +0200405 r = 0;
406
407out:
Gleb Natapov46e624b2009-08-24 11:54:20 +0300408 kfree(new);
Avi Kivity399ec802008-11-19 13:58:46 +0200409 return r;
410}
411
412#define IOAPIC_ROUTING_ENTRY(irq) \
413 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
414 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
415#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
416
417#ifdef CONFIG_X86
Avi Kivity399ec802008-11-19 13:58:46 +0200418# define PIC_ROUTING_ENTRY(irq) \
419 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
420 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
421# define ROUTING_ENTRY2(irq) \
422 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
423#else
424# define ROUTING_ENTRY2(irq) \
425 IOAPIC_ROUTING_ENTRY(irq)
426#endif
427
428static const struct kvm_irq_routing_entry default_routing[] = {
429 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
430 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
431 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
432 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
433 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
434 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
435 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
436 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
437 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
438 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
439 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
440 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
441#ifdef CONFIG_IA64
442 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
443 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
444 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
445 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
446 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
447 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
448 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
449 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
450 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
451 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
452 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
453 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
454#endif
455};
456
457int kvm_setup_default_irq_routing(struct kvm *kvm)
458{
459 return kvm_set_irq_routing(kvm, default_routing,
460 ARRAY_SIZE(default_routing), 0);
461}