blob: 83402d74a767bec214d2c2467cceb3a271a6be44 [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 Natapov49256632009-02-04 17:28:14 +020036static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030037 struct kvm *kvm, int irq_source_id, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020038{
39#ifdef CONFIG_X86
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030040 struct kvm_pic *pic = pic_irqchip(kvm);
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +030041 return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level);
Gleb Natapov49256632009-02-04 17:28:14 +020042#else
43 return -1;
Avi Kivity399ec802008-11-19 13:58:46 +020044#endif
45}
46
Gleb Natapov49256632009-02-04 17:28:14 +020047static int kvm_set_ioapic_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{
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030050 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +030051 return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level);
Avi Kivity399ec802008-11-19 13:58:46 +020052}
53
Gleb Natapov58c2dde2009-03-05 16:35:04 +020054inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
Sheng Yang116191b2009-02-11 16:03:37 +080055{
Gleb Natapov58c2dde2009-03-05 16:35:04 +020056#ifdef CONFIG_IA64
57 return irq->delivery_mode ==
58 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
59#else
60 return irq->delivery_mode == APIC_DM_LOWEST;
61#endif
62}
Sheng Yang116191b2009-02-11 16:03:37 +080063
Gleb Natapov58c2dde2009-03-05 16:35:04 +020064int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
65 struct kvm_lapic_irq *irq)
66{
67 int i, r = -1;
68 struct kvm_vcpu *vcpu, *lowest = NULL;
69
70 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
71 kvm_is_dm_lowest_prio(irq))
Gleb Natapov343f94f2009-03-05 16:34:54 +020072 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
73
Gleb Natapov988a2ca2009-06-09 15:56:29 +030074 kvm_for_each_vcpu(i, vcpu, kvm) {
75 if (!kvm_apic_present(vcpu))
Gleb Natapov343f94f2009-03-05 16:34:54 +020076 continue;
Sheng Yang74a3a8f2009-03-04 13:33:02 +080077
Gleb Natapov58c2dde2009-03-05 16:35:04 +020078 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
79 irq->dest_id, irq->dest_mode))
Gleb Natapov343f94f2009-03-05 16:34:54 +020080 continue;
81
Gleb Natapov58c2dde2009-03-05 16:35:04 +020082 if (!kvm_is_dm_lowest_prio(irq)) {
83 if (r < 0)
84 r = 0;
85 r += kvm_apic_set_irq(vcpu, irq);
Chris Lalancetteaefd18f2010-06-16 17:11:13 -040086 } else if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov58c2dde2009-03-05 16:35:04 +020087 if (!lowest)
88 lowest = vcpu;
89 else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
90 lowest = vcpu;
Gleb Natapove1035712009-03-05 16:34:59 +020091 }
Gleb Natapov343f94f2009-03-05 16:34:54 +020092 }
93
Gleb Natapov58c2dde2009-03-05 16:35:04 +020094 if (lowest)
95 r = kvm_apic_set_irq(lowest, irq);
96
97 return r;
Sheng Yang116191b2009-02-11 16:03:37 +080098}
99
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200100int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
101 struct kvm *kvm, int irq_source_id, int level)
Sheng Yang79950e12009-02-10 13:57:06 +0800102{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200103 struct kvm_lapic_irq irq;
Sheng Yang79950e12009-02-10 13:57:06 +0800104
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300105 if (!level)
106 return -1;
107
Gleb Natapov1000ff82009-07-07 16:00:57 +0300108 trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data);
109
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200110 irq.dest_id = (e->msi.address_lo &
Sheng Yang116191b2009-02-11 16:03:37 +0800111 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200112 irq.vector = (e->msi.data &
Sheng Yang116191b2009-02-11 16:03:37 +0800113 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200114 irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
115 irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
116 irq.delivery_mode = e->msi.data & 0x700;
117 irq.level = 1;
118 irq.shorthand = 0;
Sheng Yang116191b2009-02-11 16:03:37 +0800119
120 /* TODO Deal with RH bit of MSI message address */
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200121 return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
Sheng Yang79950e12009-02-10 13:57:06 +0800122}
123
Jan Kiszka07975ad2012-03-29 21:14:12 +0200124int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
125{
126 struct kvm_kernel_irq_routing_entry route;
127
128 if (!irqchip_in_kernel(kvm) || msi->flags != 0)
129 return -EINVAL;
130
131 route.msi.address_lo = msi->address_lo;
132 route.msi.address_hi = msi->address_hi;
133 route.msi.data = msi->data;
134
135 return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1);
136}
137
Gleb Natapov680b3642009-08-24 11:54:26 +0300138/*
Gleb Natapov49256632009-02-04 17:28:14 +0200139 * Return value:
140 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
141 * = 0 Interrupt was coalesced (previous irq is still pending)
142 * > 0 Number of CPUs interrupt was delivered to
143 */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300144int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800145{
Gleb Natapoveba02262009-08-24 11:54:25 +0300146 struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
147 int ret = -1, i = 0;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300148 struct kvm_irq_routing_table *irq_rt;
149 struct hlist_node *n;
Sheng Yang5550af42008-10-15 20:15:06 +0800150
Avi Kivityae8c1c42009-07-01 12:09:41 +0300151 trace_kvm_set_irq(irq, level, irq_source_id);
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300152
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800153 /* Not possible to detect if the guest uses the PIC or the
154 * IOAPIC. So set the bit in both. The guest will ignore
155 * writes to the unused one.
156 */
Gleb Natapove42bba92009-08-24 11:54:22 +0300157 rcu_read_lock();
158 irq_rt = rcu_dereference(kvm->irq_routing);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300159 if (irq < irq_rt->nr_rt_entries)
Gleb Natapoveba02262009-08-24 11:54:25 +0300160 hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
161 irq_set[i++] = *e;
Gleb Natapove42bba92009-08-24 11:54:22 +0300162 rcu_read_unlock();
Gleb Natapoveba02262009-08-24 11:54:25 +0300163
164 while(i--) {
165 int r;
166 r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level);
167 if (r < 0)
168 continue;
169
170 ret = r + ((ret < 0) ? 0 : ret);
171 }
172
Gleb Natapov49256632009-02-04 17:28:14 +0200173 return ret;
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800174}
175
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200176void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800177{
178 struct kvm_irq_ack_notifier *kian;
179 struct hlist_node *n;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300180 int gsi;
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200181
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300182 trace_kvm_ack_irq(irqchip, pin);
183
Gleb Natapove42bba92009-08-24 11:54:22 +0300184 rcu_read_lock();
185 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
Gleb Natapov3e71f882009-08-24 11:54:21 +0300186 if (gsi != -1)
Gleb Natapov280aa172009-08-24 11:54:24 +0300187 hlist_for_each_entry_rcu(kian, n, &kvm->irq_ack_notifier_list,
188 link)
Gleb Natapov3e71f882009-08-24 11:54:21 +0300189 if (kian->gsi == gsi)
190 kian->irq_acked(kian);
Gleb Natapov280aa172009-08-24 11:54:24 +0300191 rcu_read_unlock();
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800192}
193
194void kvm_register_irq_ack_notifier(struct kvm *kvm,
195 struct kvm_irq_ack_notifier *kian)
196{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300197 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300198 hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300199 mutex_unlock(&kvm->irq_lock);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800200}
201
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300202void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
203 struct kvm_irq_ack_notifier *kian)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800204{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300205 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300206 hlist_del_init_rcu(&kian->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300207 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300208 synchronize_rcu();
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800209}
Sheng Yang5550af42008-10-15 20:15:06 +0800210
Sheng Yang5550af42008-10-15 20:15:06 +0800211int kvm_request_irq_source_id(struct kvm *kvm)
212{
213 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300214 int irq_source_id;
215
216 mutex_lock(&kvm->irq_lock);
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300217 irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
Mark McLoughlin61552362008-12-01 13:57:48 +0000218
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300219 if (irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800220 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200221 irq_source_id = -EFAULT;
222 goto unlock;
Mark McLoughlin61552362008-12-01 13:57:48 +0000223 }
224
225 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
226 set_bit(irq_source_id, bitmap);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200227unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300228 mutex_unlock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000229
Sheng Yang5550af42008-10-15 20:15:06 +0800230 return irq_source_id;
231}
232
233void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
234{
Mark McLoughlin61552362008-12-01 13:57:48 +0000235 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
236
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300237 mutex_lock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000238 if (irq_source_id < 0 ||
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300239 irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800240 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200241 goto unlock;
Sheng Yang5550af42008-10-15 20:15:06 +0800242 }
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200243 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
244 if (!irqchip_in_kernel(kvm))
245 goto unlock;
246
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300247 kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300248#ifdef CONFIG_X86
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300249 kvm_pic_clear_all(pic_irqchip(kvm), irq_source_id);
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300250#endif
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200251unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300252 mutex_unlock(&kvm->irq_lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800253}
Avi Kivity75858a82009-01-04 17:10:50 +0200254
255void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
256 struct kvm_irq_mask_notifier *kimn)
257{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300258 mutex_lock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200259 kimn->irq = irq;
Gleb Natapov280aa172009-08-24 11:54:24 +0300260 hlist_add_head_rcu(&kimn->link, &kvm->mask_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300261 mutex_unlock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200262}
263
264void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
265 struct kvm_irq_mask_notifier *kimn)
266{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300267 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300268 hlist_del_rcu(&kimn->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300269 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300270 synchronize_rcu();
Avi Kivity75858a82009-01-04 17:10:50 +0200271}
272
Gleb Natapov4a994352010-07-11 15:32:23 +0300273void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
274 bool mask)
Avi Kivity75858a82009-01-04 17:10:50 +0200275{
276 struct kvm_irq_mask_notifier *kimn;
277 struct hlist_node *n;
Gleb Natapov4a994352010-07-11 15:32:23 +0300278 int gsi;
Avi Kivity75858a82009-01-04 17:10:50 +0200279
Gleb Natapov280aa172009-08-24 11:54:24 +0300280 rcu_read_lock();
Gleb Natapov4a994352010-07-11 15:32:23 +0300281 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
282 if (gsi != -1)
283 hlist_for_each_entry_rcu(kimn, n, &kvm->mask_notifier_list, link)
284 if (kimn->irq == gsi)
285 kimn->func(kimn, mask);
Gleb Natapov280aa172009-08-24 11:54:24 +0300286 rcu_read_unlock();
Avi Kivity75858a82009-01-04 17:10:50 +0200287}
288
Avi Kivity399ec802008-11-19 13:58:46 +0200289void kvm_free_irq_routing(struct kvm *kvm)
290{
Gleb Natapove42bba92009-08-24 11:54:22 +0300291 /* Called only during vm destruction. Nobody can use the pointer
292 at this stage */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300293 kfree(kvm->irq_routing);
Avi Kivity399ec802008-11-19 13:58:46 +0200294}
295
Gleb Natapov46e624b2009-08-24 11:54:20 +0300296static int setup_routing_entry(struct kvm_irq_routing_table *rt,
297 struct kvm_kernel_irq_routing_entry *e,
Hannes Edercded19f2009-02-21 02:19:13 +0100298 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200299{
300 int r = -EINVAL;
301 int delta;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200302 unsigned max_pin;
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 ||
Avi Kivityf2ebd422012-04-22 17:02:11 +0300312 ue->type == KVM_IRQ_ROUTING_MSI ||
Gleb Natapov46e624b2009-08-24 11:54:20 +0300313 ue->u.irqchip.irqchip == ei->irqchip.irqchip)
314 return r;
Avi Kivity399ec802008-11-19 13:58:46 +0200315
316 e->gsi = ue->gsi;
Michael S. Tsirkin5116d8f2009-07-26 17:10:01 +0300317 e->type = ue->type;
Avi Kivity399ec802008-11-19 13:58:46 +0200318 switch (ue->type) {
319 case KVM_IRQ_ROUTING_IRQCHIP:
320 delta = 0;
321 switch (ue->u.irqchip.irqchip) {
322 case KVM_IRQCHIP_PIC_MASTER:
323 e->set = kvm_set_pic_irq;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200324 max_pin = 16;
Avi Kivity399ec802008-11-19 13:58:46 +0200325 break;
326 case KVM_IRQCHIP_PIC_SLAVE:
Gleb Natapov49256632009-02-04 17:28:14 +0200327 e->set = kvm_set_pic_irq;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200328 max_pin = 16;
Avi Kivity399ec802008-11-19 13:58:46 +0200329 delta = 8;
330 break;
331 case KVM_IRQCHIP_IOAPIC:
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200332 max_pin = KVM_IOAPIC_NUM_PINS;
Chris Wrightefbc1002009-05-01 14:15:43 -0700333 e->set = kvm_set_ioapic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200334 break;
335 default:
336 goto out;
337 }
338 e->irqchip.irqchip = ue->u.irqchip.irqchip;
339 e->irqchip.pin = ue->u.irqchip.pin + delta;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200340 if (e->irqchip.pin >= max_pin)
Gleb Natapov3e71f882009-08-24 11:54:21 +0300341 goto out;
342 rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi;
Avi Kivity399ec802008-11-19 13:58:46 +0200343 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800344 case KVM_IRQ_ROUTING_MSI:
345 e->set = kvm_set_msi;
346 e->msi.address_lo = ue->u.msi.address_lo;
347 e->msi.address_hi = ue->u.msi.address_hi;
348 e->msi.data = ue->u.msi.data;
349 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200350 default:
351 goto out;
352 }
Gleb Natapov46e624b2009-08-24 11:54:20 +0300353
354 hlist_add_head(&e->link, &rt->map[e->gsi]);
Avi Kivity399ec802008-11-19 13:58:46 +0200355 r = 0;
356out:
357 return r;
358}
359
360
361int kvm_set_irq_routing(struct kvm *kvm,
362 const struct kvm_irq_routing_entry *ue,
363 unsigned nr,
364 unsigned flags)
365{
Gleb Natapov46e624b2009-08-24 11:54:20 +0300366 struct kvm_irq_routing_table *new, *old;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300367 u32 i, j, nr_rt_entries = 0;
Avi Kivity399ec802008-11-19 13:58:46 +0200368 int r;
369
370 for (i = 0; i < nr; ++i) {
Gleb Natapov46e624b2009-08-24 11:54:20 +0300371 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
372 return -EINVAL;
373 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
374 }
375
376 nr_rt_entries += 1;
377
378 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
379 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
380 GFP_KERNEL);
381
382 if (!new)
383 return -ENOMEM;
384
385 new->rt_entries = (void *)&new->map[nr_rt_entries];
386
387 new->nr_rt_entries = nr_rt_entries;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300388 for (i = 0; i < 3; i++)
389 for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++)
390 new->chip[i][j] = -1;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300391
392 for (i = 0; i < nr; ++i) {
Avi Kivity399ec802008-11-19 13:58:46 +0200393 r = -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200394 if (ue->flags)
395 goto out;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300396 r = setup_routing_entry(new, &new->rt_entries[i], ue);
Avi Kivity399ec802008-11-19 13:58:46 +0200397 if (r)
398 goto out;
399 ++ue;
Avi Kivity399ec802008-11-19 13:58:46 +0200400 }
401
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300402 mutex_lock(&kvm->irq_lock);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300403 old = kvm->irq_routing;
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200404 kvm_irq_routing_update(kvm, new);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300405 mutex_unlock(&kvm->irq_lock);
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200406
Gleb Natapove42bba92009-08-24 11:54:22 +0300407 synchronize_rcu();
Avi Kivity399ec802008-11-19 13:58:46 +0200408
Gleb Natapov46e624b2009-08-24 11:54:20 +0300409 new = old;
Avi Kivity399ec802008-11-19 13:58:46 +0200410 r = 0;
411
412out:
Gleb Natapov46e624b2009-08-24 11:54:20 +0300413 kfree(new);
Avi Kivity399ec802008-11-19 13:58:46 +0200414 return r;
415}
416
417#define IOAPIC_ROUTING_ENTRY(irq) \
418 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
419 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
420#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
421
422#ifdef CONFIG_X86
Avi Kivity399ec802008-11-19 13:58:46 +0200423# define PIC_ROUTING_ENTRY(irq) \
424 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
425 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
426# define ROUTING_ENTRY2(irq) \
427 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
428#else
429# define ROUTING_ENTRY2(irq) \
430 IOAPIC_ROUTING_ENTRY(irq)
431#endif
432
433static const struct kvm_irq_routing_entry default_routing[] = {
434 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
435 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
436 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
437 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
438 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
439 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
440 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
441 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
442 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
443 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
444 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
445 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
446#ifdef CONFIG_IA64
447 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
448 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
449 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
450 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
451 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
452 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
453 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
454 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
455 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
456 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
457 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
458 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
459#endif
460};
461
462int kvm_setup_default_irq_routing(struct kvm *kvm)
463{
464 return kvm_set_irq_routing(kvm, default_routing,
465 ARRAY_SIZE(default_routing), 0);
466}