blob: a6a0365475edafc1935e46c04297931eadb8663c [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 *
Nicolas Kaiser9611c182010-10-06 14:23:22 +020020 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080021 */
22
23#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030025#include <trace/events/kvm.h>
Sheng Yang79950e12009-02-10 13:57:06 +080026
Sheng Yang79950e12009-02-10 13:57:06 +080027#include <asm/msidef.h>
Gleb Natapov58c2dde2009-03-05 16:35:04 +020028#ifdef CONFIG_IA64
29#include <asm/iosapic.h>
30#endif
Sheng Yang79950e12009-02-10 13:57:06 +080031
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080032#include "irq.h"
33
34#include "ioapic.h"
35
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030036static inline int kvm_irq_line_state(unsigned long *irq_state,
37 int irq_source_id, int level)
38{
39 /* Logical OR for level trig interrupt */
40 if (level)
41 set_bit(irq_source_id, irq_state);
42 else
43 clear_bit(irq_source_id, irq_state);
44
45 return !!(*irq_state);
46}
47
Gleb Natapov49256632009-02-04 17:28:14 +020048static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030049 struct kvm *kvm, int irq_source_id, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020050{
51#ifdef CONFIG_X86
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030052 struct kvm_pic *pic = pic_irqchip(kvm);
53 level = kvm_irq_line_state(&pic->irq_states[e->irqchip.pin],
54 irq_source_id, level);
55 return kvm_pic_set_irq(pic, e->irqchip.pin, level);
Gleb Natapov49256632009-02-04 17:28:14 +020056#else
57 return -1;
Avi Kivity399ec802008-11-19 13:58:46 +020058#endif
59}
60
Gleb Natapov49256632009-02-04 17:28:14 +020061static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030062 struct kvm *kvm, int irq_source_id, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020063{
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030064 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
65 level = kvm_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
66 irq_source_id, level);
67
68 return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
Avi Kivity399ec802008-11-19 13:58:46 +020069}
70
Gleb Natapov58c2dde2009-03-05 16:35:04 +020071inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
Sheng Yang116191b2009-02-11 16:03:37 +080072{
Gleb Natapov58c2dde2009-03-05 16:35:04 +020073#ifdef CONFIG_IA64
74 return irq->delivery_mode ==
75 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
76#else
77 return irq->delivery_mode == APIC_DM_LOWEST;
78#endif
79}
Sheng Yang116191b2009-02-11 16:03:37 +080080
Gleb Natapov58c2dde2009-03-05 16:35:04 +020081int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
82 struct kvm_lapic_irq *irq)
83{
84 int i, r = -1;
85 struct kvm_vcpu *vcpu, *lowest = NULL;
86
87 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
88 kvm_is_dm_lowest_prio(irq))
Gleb Natapov343f94f2009-03-05 16:34:54 +020089 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
90
Gleb Natapov988a2ca2009-06-09 15:56:29 +030091 kvm_for_each_vcpu(i, vcpu, kvm) {
92 if (!kvm_apic_present(vcpu))
Gleb Natapov343f94f2009-03-05 16:34:54 +020093 continue;
Sheng Yang74a3a8f2009-03-04 13:33:02 +080094
Gleb Natapov58c2dde2009-03-05 16:35:04 +020095 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
96 irq->dest_id, irq->dest_mode))
Gleb Natapov343f94f2009-03-05 16:34:54 +020097 continue;
98
Gleb Natapov58c2dde2009-03-05 16:35:04 +020099 if (!kvm_is_dm_lowest_prio(irq)) {
100 if (r < 0)
101 r = 0;
102 r += kvm_apic_set_irq(vcpu, irq);
Chris Lalancetteaefd18f2010-06-16 17:11:13 -0400103 } else if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200104 if (!lowest)
105 lowest = vcpu;
106 else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
107 lowest = vcpu;
Gleb Natapove1035712009-03-05 16:34:59 +0200108 }
Gleb Natapov343f94f2009-03-05 16:34:54 +0200109 }
110
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200111 if (lowest)
112 r = kvm_apic_set_irq(lowest, irq);
113
114 return r;
Sheng Yang116191b2009-02-11 16:03:37 +0800115}
116
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200117int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
118 struct kvm *kvm, int irq_source_id, int level)
Sheng Yang79950e12009-02-10 13:57:06 +0800119{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200120 struct kvm_lapic_irq irq;
Sheng Yang79950e12009-02-10 13:57:06 +0800121
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300122 if (!level)
123 return -1;
124
Gleb Natapov1000ff82009-07-07 16:00:57 +0300125 trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data);
126
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200127 irq.dest_id = (e->msi.address_lo &
Sheng Yang116191b2009-02-11 16:03:37 +0800128 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200129 irq.vector = (e->msi.data &
Sheng Yang116191b2009-02-11 16:03:37 +0800130 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200131 irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
132 irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
133 irq.delivery_mode = e->msi.data & 0x700;
134 irq.level = 1;
135 irq.shorthand = 0;
Sheng Yang116191b2009-02-11 16:03:37 +0800136
137 /* TODO Deal with RH bit of MSI message address */
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200138 return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
Sheng Yang79950e12009-02-10 13:57:06 +0800139}
140
Jan Kiszka07975ad2012-03-29 21:14:12 +0200141int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
142{
143 struct kvm_kernel_irq_routing_entry route;
144
145 if (!irqchip_in_kernel(kvm) || msi->flags != 0)
146 return -EINVAL;
147
148 route.msi.address_lo = msi->address_lo;
149 route.msi.address_hi = msi->address_hi;
150 route.msi.data = msi->data;
151
152 return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1);
153}
154
Gleb Natapov680b3642009-08-24 11:54:26 +0300155/*
Gleb Natapov49256632009-02-04 17:28:14 +0200156 * Return value:
157 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
158 * = 0 Interrupt was coalesced (previous irq is still pending)
159 * > 0 Number of CPUs interrupt was delivered to
160 */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300161int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800162{
Gleb Natapoveba02262009-08-24 11:54:25 +0300163 struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
164 int ret = -1, i = 0;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300165 struct kvm_irq_routing_table *irq_rt;
166 struct hlist_node *n;
Sheng Yang5550af42008-10-15 20:15:06 +0800167
Avi Kivityae8c1c42009-07-01 12:09:41 +0300168 trace_kvm_set_irq(irq, level, irq_source_id);
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300169
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800170 /* Not possible to detect if the guest uses the PIC or the
171 * IOAPIC. So set the bit in both. The guest will ignore
172 * writes to the unused one.
173 */
Gleb Natapove42bba92009-08-24 11:54:22 +0300174 rcu_read_lock();
175 irq_rt = rcu_dereference(kvm->irq_routing);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300176 if (irq < irq_rt->nr_rt_entries)
Gleb Natapoveba02262009-08-24 11:54:25 +0300177 hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
178 irq_set[i++] = *e;
Gleb Natapove42bba92009-08-24 11:54:22 +0300179 rcu_read_unlock();
Gleb Natapoveba02262009-08-24 11:54:25 +0300180
181 while(i--) {
182 int r;
183 r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level);
184 if (r < 0)
185 continue;
186
187 ret = r + ((ret < 0) ? 0 : ret);
188 }
189
Gleb Natapov49256632009-02-04 17:28:14 +0200190 return ret;
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800191}
192
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200193void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800194{
195 struct kvm_irq_ack_notifier *kian;
196 struct hlist_node *n;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300197 int gsi;
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200198
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300199 trace_kvm_ack_irq(irqchip, pin);
200
Gleb Natapove42bba92009-08-24 11:54:22 +0300201 rcu_read_lock();
202 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
Gleb Natapov3e71f882009-08-24 11:54:21 +0300203 if (gsi != -1)
Gleb Natapov280aa172009-08-24 11:54:24 +0300204 hlist_for_each_entry_rcu(kian, n, &kvm->irq_ack_notifier_list,
205 link)
Gleb Natapov3e71f882009-08-24 11:54:21 +0300206 if (kian->gsi == gsi)
207 kian->irq_acked(kian);
Gleb Natapov280aa172009-08-24 11:54:24 +0300208 rcu_read_unlock();
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800209}
210
211void kvm_register_irq_ack_notifier(struct kvm *kvm,
212 struct kvm_irq_ack_notifier *kian)
213{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300214 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300215 hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300216 mutex_unlock(&kvm->irq_lock);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800217}
218
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300219void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
220 struct kvm_irq_ack_notifier *kian)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800221{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300222 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300223 hlist_del_init_rcu(&kian->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300224 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300225 synchronize_rcu();
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800226}
Sheng Yang5550af42008-10-15 20:15:06 +0800227
Sheng Yang5550af42008-10-15 20:15:06 +0800228int kvm_request_irq_source_id(struct kvm *kvm)
229{
230 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300231 int irq_source_id;
232
233 mutex_lock(&kvm->irq_lock);
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300234 irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
Mark McLoughlin61552362008-12-01 13:57:48 +0000235
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300236 if (irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800237 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200238 irq_source_id = -EFAULT;
239 goto unlock;
Mark McLoughlin61552362008-12-01 13:57:48 +0000240 }
241
242 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
243 set_bit(irq_source_id, bitmap);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200244unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300245 mutex_unlock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000246
Sheng Yang5550af42008-10-15 20:15:06 +0800247 return irq_source_id;
248}
249
250void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
251{
252 int i;
253
Mark McLoughlin61552362008-12-01 13:57:48 +0000254 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
255
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300256 mutex_lock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000257 if (irq_source_id < 0 ||
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300258 irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800259 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200260 goto unlock;
Sheng Yang5550af42008-10-15 20:15:06 +0800261 }
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200262 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
263 if (!irqchip_in_kernel(kvm))
264 goto unlock;
265
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300266 for (i = 0; i < KVM_IOAPIC_NUM_PINS; i++) {
267 clear_bit(irq_source_id, &kvm->arch.vioapic->irq_states[i]);
268 if (i >= 16)
269 continue;
270#ifdef CONFIG_X86
271 clear_bit(irq_source_id, &pic_irqchip(kvm)->irq_states[i]);
272#endif
273 }
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200274unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300275 mutex_unlock(&kvm->irq_lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800276}
Avi Kivity75858a82009-01-04 17:10:50 +0200277
278void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
279 struct kvm_irq_mask_notifier *kimn)
280{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300281 mutex_lock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200282 kimn->irq = irq;
Gleb Natapov280aa172009-08-24 11:54:24 +0300283 hlist_add_head_rcu(&kimn->link, &kvm->mask_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300284 mutex_unlock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200285}
286
287void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
288 struct kvm_irq_mask_notifier *kimn)
289{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300290 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300291 hlist_del_rcu(&kimn->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300292 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300293 synchronize_rcu();
Avi Kivity75858a82009-01-04 17:10:50 +0200294}
295
Gleb Natapov4a994352010-07-11 15:32:23 +0300296void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
297 bool mask)
Avi Kivity75858a82009-01-04 17:10:50 +0200298{
299 struct kvm_irq_mask_notifier *kimn;
300 struct hlist_node *n;
Gleb Natapov4a994352010-07-11 15:32:23 +0300301 int gsi;
Avi Kivity75858a82009-01-04 17:10:50 +0200302
Gleb Natapov280aa172009-08-24 11:54:24 +0300303 rcu_read_lock();
Gleb Natapov4a994352010-07-11 15:32:23 +0300304 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
305 if (gsi != -1)
306 hlist_for_each_entry_rcu(kimn, n, &kvm->mask_notifier_list, link)
307 if (kimn->irq == gsi)
308 kimn->func(kimn, mask);
Gleb Natapov280aa172009-08-24 11:54:24 +0300309 rcu_read_unlock();
Avi Kivity75858a82009-01-04 17:10:50 +0200310}
311
Avi Kivity399ec802008-11-19 13:58:46 +0200312void kvm_free_irq_routing(struct kvm *kvm)
313{
Gleb Natapove42bba92009-08-24 11:54:22 +0300314 /* Called only during vm destruction. Nobody can use the pointer
315 at this stage */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300316 kfree(kvm->irq_routing);
Avi Kivity399ec802008-11-19 13:58:46 +0200317}
318
Gleb Natapov46e624b2009-08-24 11:54:20 +0300319static int setup_routing_entry(struct kvm_irq_routing_table *rt,
320 struct kvm_kernel_irq_routing_entry *e,
Hannes Edercded19f2009-02-21 02:19:13 +0100321 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200322{
323 int r = -EINVAL;
324 int delta;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200325 unsigned max_pin;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300326 struct kvm_kernel_irq_routing_entry *ei;
327 struct hlist_node *n;
328
329 /*
330 * Do not allow GSI to be mapped to the same irqchip more than once.
331 * Allow only one to one mapping between GSI and MSI.
332 */
333 hlist_for_each_entry(ei, n, &rt->map[ue->gsi], link)
334 if (ei->type == KVM_IRQ_ROUTING_MSI ||
335 ue->u.irqchip.irqchip == ei->irqchip.irqchip)
336 return r;
Avi Kivity399ec802008-11-19 13:58:46 +0200337
338 e->gsi = ue->gsi;
Michael S. Tsirkin5116d8f2009-07-26 17:10:01 +0300339 e->type = ue->type;
Avi Kivity399ec802008-11-19 13:58:46 +0200340 switch (ue->type) {
341 case KVM_IRQ_ROUTING_IRQCHIP:
342 delta = 0;
343 switch (ue->u.irqchip.irqchip) {
344 case KVM_IRQCHIP_PIC_MASTER:
345 e->set = kvm_set_pic_irq;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200346 max_pin = 16;
Avi Kivity399ec802008-11-19 13:58:46 +0200347 break;
348 case KVM_IRQCHIP_PIC_SLAVE:
Gleb Natapov49256632009-02-04 17:28:14 +0200349 e->set = kvm_set_pic_irq;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200350 max_pin = 16;
Avi Kivity399ec802008-11-19 13:58:46 +0200351 delta = 8;
352 break;
353 case KVM_IRQCHIP_IOAPIC:
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200354 max_pin = KVM_IOAPIC_NUM_PINS;
Chris Wrightefbc1002009-05-01 14:15:43 -0700355 e->set = kvm_set_ioapic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200356 break;
357 default:
358 goto out;
359 }
360 e->irqchip.irqchip = ue->u.irqchip.irqchip;
361 e->irqchip.pin = ue->u.irqchip.pin + delta;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200362 if (e->irqchip.pin >= max_pin)
Gleb Natapov3e71f882009-08-24 11:54:21 +0300363 goto out;
364 rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi;
Avi Kivity399ec802008-11-19 13:58:46 +0200365 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800366 case KVM_IRQ_ROUTING_MSI:
367 e->set = kvm_set_msi;
368 e->msi.address_lo = ue->u.msi.address_lo;
369 e->msi.address_hi = ue->u.msi.address_hi;
370 e->msi.data = ue->u.msi.data;
371 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200372 default:
373 goto out;
374 }
Gleb Natapov46e624b2009-08-24 11:54:20 +0300375
376 hlist_add_head(&e->link, &rt->map[e->gsi]);
Avi Kivity399ec802008-11-19 13:58:46 +0200377 r = 0;
378out:
379 return r;
380}
381
382
383int kvm_set_irq_routing(struct kvm *kvm,
384 const struct kvm_irq_routing_entry *ue,
385 unsigned nr,
386 unsigned flags)
387{
Gleb Natapov46e624b2009-08-24 11:54:20 +0300388 struct kvm_irq_routing_table *new, *old;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300389 u32 i, j, nr_rt_entries = 0;
Avi Kivity399ec802008-11-19 13:58:46 +0200390 int r;
391
392 for (i = 0; i < nr; ++i) {
Gleb Natapov46e624b2009-08-24 11:54:20 +0300393 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
394 return -EINVAL;
395 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
396 }
397
398 nr_rt_entries += 1;
399
400 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
401 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
402 GFP_KERNEL);
403
404 if (!new)
405 return -ENOMEM;
406
407 new->rt_entries = (void *)&new->map[nr_rt_entries];
408
409 new->nr_rt_entries = nr_rt_entries;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300410 for (i = 0; i < 3; i++)
411 for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++)
412 new->chip[i][j] = -1;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300413
414 for (i = 0; i < nr; ++i) {
Avi Kivity399ec802008-11-19 13:58:46 +0200415 r = -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200416 if (ue->flags)
417 goto out;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300418 r = setup_routing_entry(new, &new->rt_entries[i], ue);
Avi Kivity399ec802008-11-19 13:58:46 +0200419 if (r)
420 goto out;
421 ++ue;
Avi Kivity399ec802008-11-19 13:58:46 +0200422 }
423
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300424 mutex_lock(&kvm->irq_lock);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300425 old = kvm->irq_routing;
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200426 kvm_irq_routing_update(kvm, new);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300427 mutex_unlock(&kvm->irq_lock);
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200428
Gleb Natapove42bba92009-08-24 11:54:22 +0300429 synchronize_rcu();
Avi Kivity399ec802008-11-19 13:58:46 +0200430
Gleb Natapov46e624b2009-08-24 11:54:20 +0300431 new = old;
Avi Kivity399ec802008-11-19 13:58:46 +0200432 r = 0;
433
434out:
Gleb Natapov46e624b2009-08-24 11:54:20 +0300435 kfree(new);
Avi Kivity399ec802008-11-19 13:58:46 +0200436 return r;
437}
438
439#define IOAPIC_ROUTING_ENTRY(irq) \
440 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
441 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
442#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
443
444#ifdef CONFIG_X86
Avi Kivity399ec802008-11-19 13:58:46 +0200445# define PIC_ROUTING_ENTRY(irq) \
446 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
447 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
448# define ROUTING_ENTRY2(irq) \
449 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
450#else
451# define ROUTING_ENTRY2(irq) \
452 IOAPIC_ROUTING_ENTRY(irq)
453#endif
454
455static const struct kvm_irq_routing_entry default_routing[] = {
456 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
457 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
458 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
459 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
460 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
461 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
462 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
463 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
464 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
465 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
466 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
467 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
468#ifdef CONFIG_IA64
469 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
470 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
471 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
472 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
473 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
474 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
475 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
476 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
477 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
478 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
479 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
480 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
481#endif
482};
483
484int kvm_setup_default_irq_routing(struct kvm *kvm)
485{
486 return kvm_set_irq_routing(kvm, default_routing,
487 ARRAY_SIZE(default_routing), 0);
488}