blob: f4648dd94888398878ae87dac94c940e9910ced0 [file] [log] [blame]
Alexander Graf1c9f8522013-04-15 23:04:10 +02001/*
2 * irqchip.c: Common API for in kernel interrupt controllers
3 * Copyright (c) 2007, Intel Corporation.
4 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
5 * Copyright (c) 2013, Alexander Graf <agraf@suse.de>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 *
20 * This file is derived from virt/kvm/irq_comm.c.
21 *
22 * Authors:
23 * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
24 * Alexander Graf <agraf@suse.de>
25 */
26
27#include <linux/kvm_host.h>
28#include <linux/slab.h>
Christian Borntraeger719d93c2014-01-16 13:44:20 +010029#include <linux/srcu.h>
Alexander Graf1c9f8522013-04-15 23:04:10 +020030#include <linux/export.h>
31#include <trace/events/kvm.h>
32#include "irq.h"
33
Paul Mackerras8ba918d2014-06-30 20:51:10 +100034int kvm_irq_map_gsi(struct kvm_kernel_irq_routing_entry *entries,
35 struct kvm_irq_routing_table *irq_rt, int gsi)
36{
37 struct kvm_kernel_irq_routing_entry *e;
38 int n = 0;
39
40 if (gsi < irq_rt->nr_rt_entries) {
41 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
42 entries[n] = *e;
43 ++n;
44 }
45 }
46
47 return n;
48}
49
50int kvm_irq_map_chip_pin(struct kvm_irq_routing_table *irq_rt,
51 unsigned irqchip, unsigned pin)
52{
53 return irq_rt->chip[irqchip][pin];
54}
55
Alexander Graf1c9f8522013-04-15 23:04:10 +020056bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin)
57{
Paul Mackerras8ba918d2014-06-30 20:51:10 +100058 struct kvm_irq_routing_table *irq_rt;
Alexander Graf1c9f8522013-04-15 23:04:10 +020059 struct kvm_irq_ack_notifier *kian;
Christian Borntraeger719d93c2014-01-16 13:44:20 +010060 int gsi, idx;
Alexander Graf1c9f8522013-04-15 23:04:10 +020061
Christian Borntraeger719d93c2014-01-16 13:44:20 +010062 idx = srcu_read_lock(&kvm->irq_srcu);
Paul Mackerras8ba918d2014-06-30 20:51:10 +100063 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
64 gsi = kvm_irq_map_chip_pin(irq_rt, irqchip, pin);
Alexander Graf1c9f8522013-04-15 23:04:10 +020065 if (gsi != -1)
66 hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
67 link)
68 if (kian->gsi == gsi) {
Christian Borntraeger719d93c2014-01-16 13:44:20 +010069 srcu_read_unlock(&kvm->irq_srcu, idx);
Alexander Graf1c9f8522013-04-15 23:04:10 +020070 return true;
71 }
72
Christian Borntraeger719d93c2014-01-16 13:44:20 +010073 srcu_read_unlock(&kvm->irq_srcu, idx);
Alexander Graf1c9f8522013-04-15 23:04:10 +020074
75 return false;
76}
77EXPORT_SYMBOL_GPL(kvm_irq_has_notifier);
78
79void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
80{
Paul Mackerras8ba918d2014-06-30 20:51:10 +100081 struct kvm_irq_routing_table *irq_rt;
Alexander Graf1c9f8522013-04-15 23:04:10 +020082 struct kvm_irq_ack_notifier *kian;
Christian Borntraeger719d93c2014-01-16 13:44:20 +010083 int gsi, idx;
Alexander Graf1c9f8522013-04-15 23:04:10 +020084
85 trace_kvm_ack_irq(irqchip, pin);
86
Christian Borntraeger719d93c2014-01-16 13:44:20 +010087 idx = srcu_read_lock(&kvm->irq_srcu);
Paul Mackerras8ba918d2014-06-30 20:51:10 +100088 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
89 gsi = kvm_irq_map_chip_pin(irq_rt, irqchip, pin);
Alexander Graf1c9f8522013-04-15 23:04:10 +020090 if (gsi != -1)
91 hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
92 link)
93 if (kian->gsi == gsi)
94 kian->irq_acked(kian);
Christian Borntraeger719d93c2014-01-16 13:44:20 +010095 srcu_read_unlock(&kvm->irq_srcu, idx);
Alexander Graf1c9f8522013-04-15 23:04:10 +020096}
97
98void kvm_register_irq_ack_notifier(struct kvm *kvm,
99 struct kvm_irq_ack_notifier *kian)
100{
101 mutex_lock(&kvm->irq_lock);
102 hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
103 mutex_unlock(&kvm->irq_lock);
104#ifdef __KVM_HAVE_IOAPIC
105 kvm_vcpu_request_scan_ioapic(kvm);
106#endif
107}
108
109void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
110 struct kvm_irq_ack_notifier *kian)
111{
112 mutex_lock(&kvm->irq_lock);
113 hlist_del_init_rcu(&kian->link);
114 mutex_unlock(&kvm->irq_lock);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100115 synchronize_srcu(&kvm->irq_srcu);
Alexander Graf1c9f8522013-04-15 23:04:10 +0200116#ifdef __KVM_HAVE_IOAPIC
117 kvm_vcpu_request_scan_ioapic(kvm);
118#endif
119}
120
121int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
122{
123 struct kvm_kernel_irq_routing_entry route;
124
125 if (!irqchip_in_kernel(kvm) || msi->flags != 0)
126 return -EINVAL;
127
128 route.msi.address_lo = msi->address_lo;
129 route.msi.address_hi = msi->address_hi;
130 route.msi.data = msi->data;
131
132 return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, false);
133}
134
135/*
136 * Return value:
137 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
138 * = 0 Interrupt was coalesced (previous irq is still pending)
139 * > 0 Number of CPUs interrupt was delivered to
140 */
141int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
142 bool line_status)
143{
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000144 struct kvm_kernel_irq_routing_entry irq_set[KVM_NR_IRQCHIPS];
145 int ret = -1, i, idx;
Alexander Graf1c9f8522013-04-15 23:04:10 +0200146 struct kvm_irq_routing_table *irq_rt;
147
148 trace_kvm_set_irq(irq, level, irq_source_id);
149
150 /* Not possible to detect if the guest uses the PIC or the
151 * IOAPIC. So set the bit in both. The guest will ignore
152 * writes to the unused one.
153 */
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100154 idx = srcu_read_lock(&kvm->irq_srcu);
155 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000156 i = kvm_irq_map_gsi(irq_set, irq_rt, irq);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100157 srcu_read_unlock(&kvm->irq_srcu, idx);
Alexander Graf1c9f8522013-04-15 23:04:10 +0200158
159 while(i--) {
160 int r;
161 r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level,
162 line_status);
163 if (r < 0)
164 continue;
165
166 ret = r + ((ret < 0) ? 0 : ret);
167 }
168
169 return ret;
170}
171
172void kvm_free_irq_routing(struct kvm *kvm)
173{
174 /* Called only during vm destruction. Nobody can use the pointer
175 at this stage */
176 kfree(kvm->irq_routing);
177}
Alexander Grafe8cde092013-04-15 23:23:21 +0200178
179static int setup_routing_entry(struct kvm_irq_routing_table *rt,
180 struct kvm_kernel_irq_routing_entry *e,
181 const struct kvm_irq_routing_entry *ue)
182{
183 int r = -EINVAL;
184 struct kvm_kernel_irq_routing_entry *ei;
185
186 /*
187 * Do not allow GSI to be mapped to the same irqchip more than once.
188 * Allow only one to one mapping between GSI and MSI.
189 */
190 hlist_for_each_entry(ei, &rt->map[ue->gsi], link)
191 if (ei->type == KVM_IRQ_ROUTING_MSI ||
192 ue->type == KVM_IRQ_ROUTING_MSI ||
193 ue->u.irqchip.irqchip == ei->irqchip.irqchip)
194 return r;
195
196 e->gsi = ue->gsi;
197 e->type = ue->type;
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000198 r = kvm_set_routing_entry(e, ue);
Alexander Grafe8cde092013-04-15 23:23:21 +0200199 if (r)
200 goto out;
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000201 if (e->type == KVM_IRQ_ROUTING_IRQCHIP)
202 rt->chip[e->irqchip.irqchip][e->irqchip.pin] = e->gsi;
Alexander Grafe8cde092013-04-15 23:23:21 +0200203
204 hlist_add_head(&e->link, &rt->map[e->gsi]);
205 r = 0;
206out:
207 return r;
208}
209
210int kvm_set_irq_routing(struct kvm *kvm,
211 const struct kvm_irq_routing_entry *ue,
212 unsigned nr,
213 unsigned flags)
214{
215 struct kvm_irq_routing_table *new, *old;
216 u32 i, j, nr_rt_entries = 0;
217 int r;
218
219 for (i = 0; i < nr; ++i) {
220 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
221 return -EINVAL;
222 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
223 }
224
225 nr_rt_entries += 1;
226
227 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
228 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
229 GFP_KERNEL);
230
231 if (!new)
232 return -ENOMEM;
233
234 new->rt_entries = (void *)&new->map[nr_rt_entries];
235
236 new->nr_rt_entries = nr_rt_entries;
237 for (i = 0; i < KVM_NR_IRQCHIPS; i++)
238 for (j = 0; j < KVM_IRQCHIP_NUM_PINS; j++)
239 new->chip[i][j] = -1;
240
241 for (i = 0; i < nr; ++i) {
242 r = -EINVAL;
243 if (ue->flags)
244 goto out;
245 r = setup_routing_entry(new, &new->rt_entries[i], ue);
246 if (r)
247 goto out;
248 ++ue;
249 }
250
251 mutex_lock(&kvm->irq_lock);
252 old = kvm->irq_routing;
253 kvm_irq_routing_update(kvm, new);
254 mutex_unlock(&kvm->irq_lock);
255
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100256 synchronize_srcu_expedited(&kvm->irq_srcu);
Alexander Grafe8cde092013-04-15 23:23:21 +0200257
258 new = old;
259 r = 0;
260
261out:
262 kfree(new);
263 return r;
264}