blob: e9073cf4d0406c0faaec2faf0d87015f962494ad [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>
Yang Zhangc7c9c562013-01-25 10:18:51 +080025#include <linux/export.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030026#include <trace/events/kvm.h>
Sheng Yang79950e12009-02-10 13:57:06 +080027
Sheng Yang79950e12009-02-10 13:57:06 +080028#include <asm/msidef.h>
Gleb Natapov58c2dde2009-03-05 16:35:04 +020029#ifdef CONFIG_IA64
30#include <asm/iosapic.h>
31#endif
Sheng Yang79950e12009-02-10 13:57:06 +080032
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080033#include "irq.h"
34
35#include "ioapic.h"
36
Gleb Natapov49256632009-02-04 17:28:14 +020037static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030038 struct kvm *kvm, int irq_source_id, int level)
Avi Kivity399ec802008-11-19 13:58:46 +020039{
40#ifdef CONFIG_X86
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030041 struct kvm_pic *pic = pic_irqchip(kvm);
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +030042 return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level);
Gleb Natapov49256632009-02-04 17:28:14 +020043#else
44 return -1;
Avi Kivity399ec802008-11-19 13:58:46 +020045#endif
46}
47
Gleb Natapov49256632009-02-04 17:28:14 +020048static int kvm_set_ioapic_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{
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030051 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +030052 return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level);
Avi Kivity399ec802008-11-19 13:58:46 +020053}
54
Gleb Natapov58c2dde2009-03-05 16:35:04 +020055inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
Sheng Yang116191b2009-02-11 16:03:37 +080056{
Gleb Natapov58c2dde2009-03-05 16:35:04 +020057#ifdef CONFIG_IA64
58 return irq->delivery_mode ==
59 (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
60#else
61 return irq->delivery_mode == APIC_DM_LOWEST;
62#endif
63}
Sheng Yang116191b2009-02-11 16:03:37 +080064
Gleb Natapov58c2dde2009-03-05 16:35:04 +020065int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
66 struct kvm_lapic_irq *irq)
67{
68 int i, r = -1;
69 struct kvm_vcpu *vcpu, *lowest = NULL;
70
71 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
Gleb Natapov1e08ec42012-09-13 17:19:24 +030072 kvm_is_dm_lowest_prio(irq)) {
Gleb Natapov343f94f2009-03-05 16:34:54 +020073 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
Gleb Natapov1e08ec42012-09-13 17:19:24 +030074 irq->delivery_mode = APIC_DM_FIXED;
75 }
76
77 if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r))
78 return r;
Gleb Natapov343f94f2009-03-05 16:34:54 +020079
Gleb Natapov988a2ca2009-06-09 15:56:29 +030080 kvm_for_each_vcpu(i, vcpu, kvm) {
81 if (!kvm_apic_present(vcpu))
Gleb Natapov343f94f2009-03-05 16:34:54 +020082 continue;
Sheng Yang74a3a8f2009-03-04 13:33:02 +080083
Gleb Natapov58c2dde2009-03-05 16:35:04 +020084 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
85 irq->dest_id, irq->dest_mode))
Gleb Natapov343f94f2009-03-05 16:34:54 +020086 continue;
87
Gleb Natapov58c2dde2009-03-05 16:35:04 +020088 if (!kvm_is_dm_lowest_prio(irq)) {
89 if (r < 0)
90 r = 0;
91 r += kvm_apic_set_irq(vcpu, irq);
Chris Lalancetteaefd18f2010-06-16 17:11:13 -040092 } else if (kvm_lapic_enabled(vcpu)) {
Gleb Natapov58c2dde2009-03-05 16:35:04 +020093 if (!lowest)
94 lowest = vcpu;
95 else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
96 lowest = vcpu;
Gleb Natapove1035712009-03-05 16:34:59 +020097 }
Gleb Natapov343f94f2009-03-05 16:34:54 +020098 }
99
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200100 if (lowest)
101 r = kvm_apic_set_irq(lowest, irq);
102
103 return r;
Sheng Yang116191b2009-02-11 16:03:37 +0800104}
105
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200106static inline void kvm_set_msi_irq(struct kvm_kernel_irq_routing_entry *e,
107 struct kvm_lapic_irq *irq)
108{
109 trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data);
110
111 irq->dest_id = (e->msi.address_lo &
112 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
113 irq->vector = (e->msi.data &
114 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
115 irq->dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
116 irq->trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
117 irq->delivery_mode = e->msi.data & 0x700;
118 irq->level = 1;
119 irq->shorthand = 0;
120 /* TODO Deal with RH bit of MSI message address */
121}
122
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200123int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
124 struct kvm *kvm, int irq_source_id, int level)
Sheng Yang79950e12009-02-10 13:57:06 +0800125{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200126 struct kvm_lapic_irq irq;
Sheng Yang79950e12009-02-10 13:57:06 +0800127
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300128 if (!level)
129 return -1;
130
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200131 kvm_set_msi_irq(e, &irq);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300132
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200133 return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
Sheng Yang79950e12009-02-10 13:57:06 +0800134}
135
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200136
137static int kvm_set_msi_inatomic(struct kvm_kernel_irq_routing_entry *e,
138 struct kvm *kvm)
139{
140 struct kvm_lapic_irq irq;
141 int r;
142
143 kvm_set_msi_irq(e, &irq);
144
145 if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r))
146 return r;
147 else
148 return -EWOULDBLOCK;
149}
150
Jan Kiszka07975ad2012-03-29 21:14:12 +0200151int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
152{
153 struct kvm_kernel_irq_routing_entry route;
154
155 if (!irqchip_in_kernel(kvm) || msi->flags != 0)
156 return -EINVAL;
157
158 route.msi.address_lo = msi->address_lo;
159 route.msi.address_hi = msi->address_hi;
160 route.msi.data = msi->data;
161
162 return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1);
163}
164
Gleb Natapov680b3642009-08-24 11:54:26 +0300165/*
Gleb Natapov49256632009-02-04 17:28:14 +0200166 * Return value:
167 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
168 * = 0 Interrupt was coalesced (previous irq is still pending)
169 * > 0 Number of CPUs interrupt was delivered to
170 */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300171int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800172{
Gleb Natapoveba02262009-08-24 11:54:25 +0300173 struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
174 int ret = -1, i = 0;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300175 struct kvm_irq_routing_table *irq_rt;
Sheng Yang5550af42008-10-15 20:15:06 +0800176
Avi Kivityae8c1c42009-07-01 12:09:41 +0300177 trace_kvm_set_irq(irq, level, irq_source_id);
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300178
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800179 /* Not possible to detect if the guest uses the PIC or the
180 * IOAPIC. So set the bit in both. The guest will ignore
181 * writes to the unused one.
182 */
Gleb Natapove42bba92009-08-24 11:54:22 +0300183 rcu_read_lock();
184 irq_rt = rcu_dereference(kvm->irq_routing);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300185 if (irq < irq_rt->nr_rt_entries)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800186 hlist_for_each_entry(e, &irq_rt->map[irq], link)
Gleb Natapoveba02262009-08-24 11:54:25 +0300187 irq_set[i++] = *e;
Gleb Natapove42bba92009-08-24 11:54:22 +0300188 rcu_read_unlock();
Gleb Natapoveba02262009-08-24 11:54:25 +0300189
190 while(i--) {
191 int r;
192 r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level);
193 if (r < 0)
194 continue;
195
196 ret = r + ((ret < 0) ? 0 : ret);
197 }
198
Gleb Natapov49256632009-02-04 17:28:14 +0200199 return ret;
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800200}
201
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200202/*
203 * Deliver an IRQ in an atomic context if we can, or return a failure,
204 * user can retry in a process context.
205 * Return value:
206 * -EWOULDBLOCK - Can't deliver in atomic context: retry in a process context.
207 * Other values - No need to retry.
208 */
209int kvm_set_irq_inatomic(struct kvm *kvm, int irq_source_id, u32 irq, int level)
210{
211 struct kvm_kernel_irq_routing_entry *e;
212 int ret = -EINVAL;
213 struct kvm_irq_routing_table *irq_rt;
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200214
215 trace_kvm_set_irq(irq, level, irq_source_id);
216
217 /*
218 * Injection into either PIC or IOAPIC might need to scan all CPUs,
219 * which would need to be retried from thread context; when same GSI
220 * is connected to both PIC and IOAPIC, we'd have to report a
221 * partial failure here.
222 * Since there's no easy way to do this, we only support injecting MSI
223 * which is limited to 1:1 GSI mapping.
224 */
225 rcu_read_lock();
226 irq_rt = rcu_dereference(kvm->irq_routing);
227 if (irq < irq_rt->nr_rt_entries)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800228 hlist_for_each_entry(e, &irq_rt->map[irq], link) {
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200229 if (likely(e->type == KVM_IRQ_ROUTING_MSI))
230 ret = kvm_set_msi_inatomic(e, kvm);
231 else
232 ret = -EWOULDBLOCK;
233 break;
234 }
235 rcu_read_unlock();
236 return ret;
237}
238
Yang Zhangc7c9c562013-01-25 10:18:51 +0800239bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin)
240{
241 struct kvm_irq_ack_notifier *kian;
Yang Zhangc7c9c562013-01-25 10:18:51 +0800242 int gsi;
243
244 rcu_read_lock();
245 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
246 if (gsi != -1)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800247 hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
Yang Zhangc7c9c562013-01-25 10:18:51 +0800248 link)
249 if (kian->gsi == gsi) {
250 rcu_read_unlock();
251 return true;
252 }
253
254 rcu_read_unlock();
255
256 return false;
257}
258EXPORT_SYMBOL_GPL(kvm_irq_has_notifier);
259
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200260void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800261{
262 struct kvm_irq_ack_notifier *kian;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300263 int gsi;
Marcelo Tosatti44882ee2009-01-27 15:12:38 -0200264
Marcelo Tosatti229456f2009-06-17 09:22:14 -0300265 trace_kvm_ack_irq(irqchip, pin);
266
Gleb Natapove42bba92009-08-24 11:54:22 +0300267 rcu_read_lock();
268 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
Gleb Natapov3e71f882009-08-24 11:54:21 +0300269 if (gsi != -1)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800270 hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
Gleb Natapov280aa172009-08-24 11:54:24 +0300271 link)
Gleb Natapov3e71f882009-08-24 11:54:21 +0300272 if (kian->gsi == gsi)
273 kian->irq_acked(kian);
Gleb Natapov280aa172009-08-24 11:54:24 +0300274 rcu_read_unlock();
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800275}
276
277void kvm_register_irq_ack_notifier(struct kvm *kvm,
278 struct kvm_irq_ack_notifier *kian)
279{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300280 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300281 hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300282 mutex_unlock(&kvm->irq_lock);
Yang Zhangc7c9c562013-01-25 10:18:51 +0800283 kvm_ioapic_make_eoibitmap_request(kvm);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800284}
285
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300286void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
287 struct kvm_irq_ack_notifier *kian)
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800288{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300289 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300290 hlist_del_init_rcu(&kian->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300291 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300292 synchronize_rcu();
Yang Zhangc7c9c562013-01-25 10:18:51 +0800293 kvm_ioapic_make_eoibitmap_request(kvm);
Xiantao Zhang3de42dc2008-10-06 13:48:45 +0800294}
Sheng Yang5550af42008-10-15 20:15:06 +0800295
Sheng Yang5550af42008-10-15 20:15:06 +0800296int kvm_request_irq_source_id(struct kvm *kvm)
297{
298 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300299 int irq_source_id;
300
301 mutex_lock(&kvm->irq_lock);
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300302 irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
Mark McLoughlin61552362008-12-01 13:57:48 +0000303
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300304 if (irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800305 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200306 irq_source_id = -EFAULT;
307 goto unlock;
Mark McLoughlin61552362008-12-01 13:57:48 +0000308 }
309
310 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
Alex Williamson7a844282012-09-21 11:58:03 -0600311#ifdef CONFIG_X86
312 ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
313#endif
Mark McLoughlin61552362008-12-01 13:57:48 +0000314 set_bit(irq_source_id, bitmap);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200315unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300316 mutex_unlock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000317
Sheng Yang5550af42008-10-15 20:15:06 +0800318 return irq_source_id;
319}
320
321void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
322{
Mark McLoughlin61552362008-12-01 13:57:48 +0000323 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
Alex Williamson7a844282012-09-21 11:58:03 -0600324#ifdef CONFIG_X86
325 ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
326#endif
Mark McLoughlin61552362008-12-01 13:57:48 +0000327
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300328 mutex_lock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000329 if (irq_source_id < 0 ||
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300330 irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800331 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200332 goto unlock;
Sheng Yang5550af42008-10-15 20:15:06 +0800333 }
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200334 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
335 if (!irqchip_in_kernel(kvm))
336 goto unlock;
337
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300338 kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300339#ifdef CONFIG_X86
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300340 kvm_pic_clear_all(pic_irqchip(kvm), irq_source_id);
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300341#endif
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200342unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300343 mutex_unlock(&kvm->irq_lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800344}
Avi Kivity75858a82009-01-04 17:10:50 +0200345
346void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
347 struct kvm_irq_mask_notifier *kimn)
348{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300349 mutex_lock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200350 kimn->irq = irq;
Gleb Natapov280aa172009-08-24 11:54:24 +0300351 hlist_add_head_rcu(&kimn->link, &kvm->mask_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300352 mutex_unlock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200353}
354
355void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
356 struct kvm_irq_mask_notifier *kimn)
357{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300358 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300359 hlist_del_rcu(&kimn->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300360 mutex_unlock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300361 synchronize_rcu();
Avi Kivity75858a82009-01-04 17:10:50 +0200362}
363
Gleb Natapov4a994352010-07-11 15:32:23 +0300364void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
365 bool mask)
Avi Kivity75858a82009-01-04 17:10:50 +0200366{
367 struct kvm_irq_mask_notifier *kimn;
Gleb Natapov4a994352010-07-11 15:32:23 +0300368 int gsi;
Avi Kivity75858a82009-01-04 17:10:50 +0200369
Gleb Natapov280aa172009-08-24 11:54:24 +0300370 rcu_read_lock();
Gleb Natapov4a994352010-07-11 15:32:23 +0300371 gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
372 if (gsi != -1)
Sasha Levinb67bfe02013-02-27 17:06:00 -0800373 hlist_for_each_entry_rcu(kimn, &kvm->mask_notifier_list, link)
Gleb Natapov4a994352010-07-11 15:32:23 +0300374 if (kimn->irq == gsi)
375 kimn->func(kimn, mask);
Gleb Natapov280aa172009-08-24 11:54:24 +0300376 rcu_read_unlock();
Avi Kivity75858a82009-01-04 17:10:50 +0200377}
378
Avi Kivity399ec802008-11-19 13:58:46 +0200379void kvm_free_irq_routing(struct kvm *kvm)
380{
Gleb Natapove42bba92009-08-24 11:54:22 +0300381 /* Called only during vm destruction. Nobody can use the pointer
382 at this stage */
Gleb Natapov46e624b2009-08-24 11:54:20 +0300383 kfree(kvm->irq_routing);
Avi Kivity399ec802008-11-19 13:58:46 +0200384}
385
Gleb Natapov46e624b2009-08-24 11:54:20 +0300386static int setup_routing_entry(struct kvm_irq_routing_table *rt,
387 struct kvm_kernel_irq_routing_entry *e,
Hannes Edercded19f2009-02-21 02:19:13 +0100388 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200389{
390 int r = -EINVAL;
391 int delta;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200392 unsigned max_pin;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300393 struct kvm_kernel_irq_routing_entry *ei;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300394
395 /*
396 * Do not allow GSI to be mapped to the same irqchip more than once.
397 * Allow only one to one mapping between GSI and MSI.
398 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800399 hlist_for_each_entry(ei, &rt->map[ue->gsi], link)
Gleb Natapov46e624b2009-08-24 11:54:20 +0300400 if (ei->type == KVM_IRQ_ROUTING_MSI ||
Avi Kivityf2ebd422012-04-22 17:02:11 +0300401 ue->type == KVM_IRQ_ROUTING_MSI ||
Gleb Natapov46e624b2009-08-24 11:54:20 +0300402 ue->u.irqchip.irqchip == ei->irqchip.irqchip)
403 return r;
Avi Kivity399ec802008-11-19 13:58:46 +0200404
405 e->gsi = ue->gsi;
Michael S. Tsirkin5116d8f2009-07-26 17:10:01 +0300406 e->type = ue->type;
Avi Kivity399ec802008-11-19 13:58:46 +0200407 switch (ue->type) {
408 case KVM_IRQ_ROUTING_IRQCHIP:
409 delta = 0;
410 switch (ue->u.irqchip.irqchip) {
411 case KVM_IRQCHIP_PIC_MASTER:
412 e->set = kvm_set_pic_irq;
Michael S. Tsirkin93b65472012-07-19 13:55:53 +0300413 max_pin = PIC_NUM_PINS;
Avi Kivity399ec802008-11-19 13:58:46 +0200414 break;
415 case KVM_IRQCHIP_PIC_SLAVE:
Gleb Natapov49256632009-02-04 17:28:14 +0200416 e->set = kvm_set_pic_irq;
Michael S. Tsirkin93b65472012-07-19 13:55:53 +0300417 max_pin = PIC_NUM_PINS;
Avi Kivity399ec802008-11-19 13:58:46 +0200418 delta = 8;
419 break;
420 case KVM_IRQCHIP_IOAPIC:
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200421 max_pin = KVM_IOAPIC_NUM_PINS;
Chris Wrightefbc1002009-05-01 14:15:43 -0700422 e->set = kvm_set_ioapic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200423 break;
424 default:
425 goto out;
426 }
427 e->irqchip.irqchip = ue->u.irqchip.irqchip;
428 e->irqchip.pin = ue->u.irqchip.pin + delta;
Marcelo Tosattid72118c2010-01-12 16:42:09 -0200429 if (e->irqchip.pin >= max_pin)
Gleb Natapov3e71f882009-08-24 11:54:21 +0300430 goto out;
431 rt->chip[ue->u.irqchip.irqchip][e->irqchip.pin] = ue->gsi;
Avi Kivity399ec802008-11-19 13:58:46 +0200432 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800433 case KVM_IRQ_ROUTING_MSI:
434 e->set = kvm_set_msi;
435 e->msi.address_lo = ue->u.msi.address_lo;
436 e->msi.address_hi = ue->u.msi.address_hi;
437 e->msi.data = ue->u.msi.data;
438 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200439 default:
440 goto out;
441 }
Gleb Natapov46e624b2009-08-24 11:54:20 +0300442
443 hlist_add_head(&e->link, &rt->map[e->gsi]);
Avi Kivity399ec802008-11-19 13:58:46 +0200444 r = 0;
445out:
446 return r;
447}
448
449
450int kvm_set_irq_routing(struct kvm *kvm,
451 const struct kvm_irq_routing_entry *ue,
452 unsigned nr,
453 unsigned flags)
454{
Gleb Natapov46e624b2009-08-24 11:54:20 +0300455 struct kvm_irq_routing_table *new, *old;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300456 u32 i, j, nr_rt_entries = 0;
Avi Kivity399ec802008-11-19 13:58:46 +0200457 int r;
458
459 for (i = 0; i < nr; ++i) {
Gleb Natapov46e624b2009-08-24 11:54:20 +0300460 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
461 return -EINVAL;
462 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
463 }
464
465 nr_rt_entries += 1;
466
467 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
468 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
469 GFP_KERNEL);
470
471 if (!new)
472 return -ENOMEM;
473
474 new->rt_entries = (void *)&new->map[nr_rt_entries];
475
476 new->nr_rt_entries = nr_rt_entries;
Gleb Natapov3e71f882009-08-24 11:54:21 +0300477 for (i = 0; i < 3; i++)
478 for (j = 0; j < KVM_IOAPIC_NUM_PINS; j++)
479 new->chip[i][j] = -1;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300480
481 for (i = 0; i < nr; ++i) {
Avi Kivity399ec802008-11-19 13:58:46 +0200482 r = -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200483 if (ue->flags)
484 goto out;
Gleb Natapov46e624b2009-08-24 11:54:20 +0300485 r = setup_routing_entry(new, &new->rt_entries[i], ue);
Avi Kivity399ec802008-11-19 13:58:46 +0200486 if (r)
487 goto out;
488 ++ue;
Avi Kivity399ec802008-11-19 13:58:46 +0200489 }
490
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300491 mutex_lock(&kvm->irq_lock);
Gleb Natapov46e624b2009-08-24 11:54:20 +0300492 old = kvm->irq_routing;
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200493 kvm_irq_routing_update(kvm, new);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300494 mutex_unlock(&kvm->irq_lock);
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200495
Gleb Natapove42bba92009-08-24 11:54:22 +0300496 synchronize_rcu();
Avi Kivity399ec802008-11-19 13:58:46 +0200497
Gleb Natapov46e624b2009-08-24 11:54:20 +0300498 new = old;
Avi Kivity399ec802008-11-19 13:58:46 +0200499 r = 0;
500
501out:
Gleb Natapov46e624b2009-08-24 11:54:20 +0300502 kfree(new);
Avi Kivity399ec802008-11-19 13:58:46 +0200503 return r;
504}
505
506#define IOAPIC_ROUTING_ENTRY(irq) \
507 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
508 .u.irqchip.irqchip = KVM_IRQCHIP_IOAPIC, .u.irqchip.pin = (irq) }
509#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
510
511#ifdef CONFIG_X86
Avi Kivity399ec802008-11-19 13:58:46 +0200512# define PIC_ROUTING_ENTRY(irq) \
513 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
514 .u.irqchip.irqchip = SELECT_PIC(irq), .u.irqchip.pin = (irq) % 8 }
515# define ROUTING_ENTRY2(irq) \
516 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
517#else
518# define ROUTING_ENTRY2(irq) \
519 IOAPIC_ROUTING_ENTRY(irq)
520#endif
521
522static const struct kvm_irq_routing_entry default_routing[] = {
523 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
524 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
525 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
526 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
527 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
528 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
529 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
530 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
531 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
532 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
533 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
534 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
535#ifdef CONFIG_IA64
536 ROUTING_ENTRY1(24), ROUTING_ENTRY1(25),
537 ROUTING_ENTRY1(26), ROUTING_ENTRY1(27),
538 ROUTING_ENTRY1(28), ROUTING_ENTRY1(29),
539 ROUTING_ENTRY1(30), ROUTING_ENTRY1(31),
540 ROUTING_ENTRY1(32), ROUTING_ENTRY1(33),
541 ROUTING_ENTRY1(34), ROUTING_ENTRY1(35),
542 ROUTING_ENTRY1(36), ROUTING_ENTRY1(37),
543 ROUTING_ENTRY1(38), ROUTING_ENTRY1(39),
544 ROUTING_ENTRY1(40), ROUTING_ENTRY1(41),
545 ROUTING_ENTRY1(42), ROUTING_ENTRY1(43),
546 ROUTING_ENTRY1(44), ROUTING_ENTRY1(45),
547 ROUTING_ENTRY1(46), ROUTING_ENTRY1(47),
548#endif
549};
550
551int kvm_setup_default_irq_routing(struct kvm *kvm)
552{
553 return kvm_set_irq_routing(kvm, default_routing,
554 ARRAY_SIZE(default_routing), 0);
555}