blob: 04faac50cef5f89bcfccee9c33377ca767abc786 [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 Mackerras9957c862014-06-30 20:51:11 +100034struct kvm_irq_routing_table {
35 int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
36 struct kvm_kernel_irq_routing_entry *rt_entries;
37 u32 nr_rt_entries;
38 /*
39 * Array indexed by gsi. Each entry contains list of irq chips
40 * the gsi is connected to.
41 */
42 struct hlist_head map[0];
43};
44
45int kvm_irq_map_gsi(struct kvm *kvm,
46 struct kvm_kernel_irq_routing_entry *entries, int gsi)
Paul Mackerras8ba918d2014-06-30 20:51:10 +100047{
Paul Mackerras9957c862014-06-30 20:51:11 +100048 struct kvm_irq_routing_table *irq_rt;
Paul Mackerras8ba918d2014-06-30 20:51:10 +100049 struct kvm_kernel_irq_routing_entry *e;
50 int n = 0;
51
Paul Mackerras9957c862014-06-30 20:51:11 +100052 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
53 lockdep_is_held(&kvm->irq_lock));
Paul Mackerras8ba918d2014-06-30 20:51:10 +100054 if (gsi < irq_rt->nr_rt_entries) {
55 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
56 entries[n] = *e;
57 ++n;
58 }
59 }
60
61 return n;
62}
63
Paul Mackerras9957c862014-06-30 20:51:11 +100064int kvm_irq_map_chip_pin(struct kvm *kvm, unsigned irqchip, unsigned pin)
Paul Mackerras8ba918d2014-06-30 20:51:10 +100065{
Paul Mackerras9957c862014-06-30 20:51:11 +100066 struct kvm_irq_routing_table *irq_rt;
67
68 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Paul Mackerras8ba918d2014-06-30 20:51:10 +100069 return irq_rt->chip[irqchip][pin];
70}
71
Alexander Graf1c9f8522013-04-15 23:04:10 +020072bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin)
73{
74 struct kvm_irq_ack_notifier *kian;
Christian Borntraeger719d93c2014-01-16 13:44:20 +010075 int gsi, idx;
Alexander Graf1c9f8522013-04-15 23:04:10 +020076
Christian Borntraeger719d93c2014-01-16 13:44:20 +010077 idx = srcu_read_lock(&kvm->irq_srcu);
Paul Mackerras9957c862014-06-30 20:51:11 +100078 gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
Alexander Graf1c9f8522013-04-15 23:04:10 +020079 if (gsi != -1)
80 hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
81 link)
82 if (kian->gsi == gsi) {
Christian Borntraeger719d93c2014-01-16 13:44:20 +010083 srcu_read_unlock(&kvm->irq_srcu, idx);
Alexander Graf1c9f8522013-04-15 23:04:10 +020084 return true;
85 }
86
Christian Borntraeger719d93c2014-01-16 13:44:20 +010087 srcu_read_unlock(&kvm->irq_srcu, idx);
Alexander Graf1c9f8522013-04-15 23:04:10 +020088
89 return false;
90}
91EXPORT_SYMBOL_GPL(kvm_irq_has_notifier);
92
93void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
94{
95 struct kvm_irq_ack_notifier *kian;
Christian Borntraeger719d93c2014-01-16 13:44:20 +010096 int gsi, idx;
Alexander Graf1c9f8522013-04-15 23:04:10 +020097
98 trace_kvm_ack_irq(irqchip, pin);
99
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100100 idx = srcu_read_lock(&kvm->irq_srcu);
Paul Mackerras9957c862014-06-30 20:51:11 +1000101 gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
Alexander Graf1c9f8522013-04-15 23:04:10 +0200102 if (gsi != -1)
103 hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
104 link)
105 if (kian->gsi == gsi)
106 kian->irq_acked(kian);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100107 srcu_read_unlock(&kvm->irq_srcu, idx);
Alexander Graf1c9f8522013-04-15 23:04:10 +0200108}
109
110void kvm_register_irq_ack_notifier(struct kvm *kvm,
111 struct kvm_irq_ack_notifier *kian)
112{
113 mutex_lock(&kvm->irq_lock);
114 hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
115 mutex_unlock(&kvm->irq_lock);
116#ifdef __KVM_HAVE_IOAPIC
117 kvm_vcpu_request_scan_ioapic(kvm);
118#endif
119}
120
121void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
122 struct kvm_irq_ack_notifier *kian)
123{
124 mutex_lock(&kvm->irq_lock);
125 hlist_del_init_rcu(&kian->link);
126 mutex_unlock(&kvm->irq_lock);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100127 synchronize_srcu(&kvm->irq_srcu);
Alexander Graf1c9f8522013-04-15 23:04:10 +0200128#ifdef __KVM_HAVE_IOAPIC
129 kvm_vcpu_request_scan_ioapic(kvm);
130#endif
131}
132
133int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
134{
135 struct kvm_kernel_irq_routing_entry route;
136
137 if (!irqchip_in_kernel(kvm) || msi->flags != 0)
138 return -EINVAL;
139
140 route.msi.address_lo = msi->address_lo;
141 route.msi.address_hi = msi->address_hi;
142 route.msi.data = msi->data;
143
144 return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, false);
145}
146
147/*
148 * Return value:
149 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
150 * = 0 Interrupt was coalesced (previous irq is still pending)
151 * > 0 Number of CPUs interrupt was delivered to
152 */
153int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
154 bool line_status)
155{
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000156 struct kvm_kernel_irq_routing_entry irq_set[KVM_NR_IRQCHIPS];
157 int ret = -1, i, idx;
Alexander Graf1c9f8522013-04-15 23:04:10 +0200158
159 trace_kvm_set_irq(irq, level, irq_source_id);
160
161 /* Not possible to detect if the guest uses the PIC or the
162 * IOAPIC. So set the bit in both. The guest will ignore
163 * writes to the unused one.
164 */
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100165 idx = srcu_read_lock(&kvm->irq_srcu);
Paul Mackerras9957c862014-06-30 20:51:11 +1000166 i = kvm_irq_map_gsi(kvm, irq_set, irq);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100167 srcu_read_unlock(&kvm->irq_srcu, idx);
Alexander Graf1c9f8522013-04-15 23:04:10 +0200168
169 while(i--) {
170 int r;
171 r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level,
172 line_status);
173 if (r < 0)
174 continue;
175
176 ret = r + ((ret < 0) ? 0 : ret);
177 }
178
179 return ret;
180}
181
182void kvm_free_irq_routing(struct kvm *kvm)
183{
184 /* Called only during vm destruction. Nobody can use the pointer
185 at this stage */
186 kfree(kvm->irq_routing);
187}
Alexander Grafe8cde092013-04-15 23:23:21 +0200188
189static int setup_routing_entry(struct kvm_irq_routing_table *rt,
190 struct kvm_kernel_irq_routing_entry *e,
191 const struct kvm_irq_routing_entry *ue)
192{
193 int r = -EINVAL;
194 struct kvm_kernel_irq_routing_entry *ei;
195
196 /*
197 * Do not allow GSI to be mapped to the same irqchip more than once.
198 * Allow only one to one mapping between GSI and MSI.
199 */
200 hlist_for_each_entry(ei, &rt->map[ue->gsi], link)
201 if (ei->type == KVM_IRQ_ROUTING_MSI ||
202 ue->type == KVM_IRQ_ROUTING_MSI ||
203 ue->u.irqchip.irqchip == ei->irqchip.irqchip)
204 return r;
205
206 e->gsi = ue->gsi;
207 e->type = ue->type;
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000208 r = kvm_set_routing_entry(e, ue);
Alexander Grafe8cde092013-04-15 23:23:21 +0200209 if (r)
210 goto out;
Paul Mackerras8ba918d2014-06-30 20:51:10 +1000211 if (e->type == KVM_IRQ_ROUTING_IRQCHIP)
212 rt->chip[e->irqchip.irqchip][e->irqchip.pin] = e->gsi;
Alexander Grafe8cde092013-04-15 23:23:21 +0200213
214 hlist_add_head(&e->link, &rt->map[e->gsi]);
215 r = 0;
216out:
217 return r;
218}
219
220int kvm_set_irq_routing(struct kvm *kvm,
221 const struct kvm_irq_routing_entry *ue,
222 unsigned nr,
223 unsigned flags)
224{
225 struct kvm_irq_routing_table *new, *old;
226 u32 i, j, nr_rt_entries = 0;
227 int r;
228
229 for (i = 0; i < nr; ++i) {
230 if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
231 return -EINVAL;
232 nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
233 }
234
235 nr_rt_entries += 1;
236
237 new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
238 + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
239 GFP_KERNEL);
240
241 if (!new)
242 return -ENOMEM;
243
244 new->rt_entries = (void *)&new->map[nr_rt_entries];
245
246 new->nr_rt_entries = nr_rt_entries;
247 for (i = 0; i < KVM_NR_IRQCHIPS; i++)
248 for (j = 0; j < KVM_IRQCHIP_NUM_PINS; j++)
249 new->chip[i][j] = -1;
250
251 for (i = 0; i < nr; ++i) {
252 r = -EINVAL;
253 if (ue->flags)
254 goto out;
255 r = setup_routing_entry(new, &new->rt_entries[i], ue);
256 if (r)
257 goto out;
258 ++ue;
259 }
260
261 mutex_lock(&kvm->irq_lock);
262 old = kvm->irq_routing;
Paul Mackerras9957c862014-06-30 20:51:11 +1000263 rcu_assign_pointer(kvm->irq_routing, new);
264 kvm_irq_routing_update(kvm);
Alexander Grafe8cde092013-04-15 23:23:21 +0200265 mutex_unlock(&kvm->irq_lock);
266
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100267 synchronize_srcu_expedited(&kvm->irq_srcu);
Alexander Grafe8cde092013-04-15 23:23:21 +0200268
269 new = old;
270 r = 0;
271
272out:
273 kfree(new);
274 return r;
275}