1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2016,2017 ARM Limited, All Rights Reserved.
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
5 */
6
7 #include <linux/interrupt.h>
8 #include <linux/irq.h>
9 #include <linux/irqdomain.h>
10 #include <linux/msi.h>
11 #include <linux/sched.h>
12
13 #include <linux/irqchip/arm-gic-v4.h>
14
15 /*
16 * WARNING: The blurb below assumes that you understand the
17 * intricacies of GICv3, GICv4, and how a guest's view of a GICv3 gets
18 * translated into GICv4 commands. So it effectively targets at most
19 * two individuals. You know who you are.
20 *
21 * The core GICv4 code is designed to *avoid* exposing too much of the
22 * core GIC code (that would in turn leak into the hypervisor code),
23 * and instead provide a hypervisor agnostic interface to the HW (of
24 * course, the astute reader will quickly realize that hypervisor
25 * agnostic actually means KVM-specific - what were you thinking?).
26 *
27 * In order to achieve a modicum of isolation, we try to hide most of
28 * the GICv4 "stuff" behind normal irqchip operations:
29 *
30 * - Any guest-visible VLPI is backed by a Linux interrupt (and a
31 * physical LPI which gets unmapped when the guest maps the
32 * VLPI). This allows the same DevID/EventID pair to be either
33 * mapped to the LPI (host) or the VLPI (guest). Note that this is
34 * exclusive, and you cannot have both.
35 *
36 * - Enabling/disabling a VLPI is done by issuing mask/unmask calls.
37 *
38 * - Guest INT/CLEAR commands are implemented through
39 * irq_set_irqchip_state().
40 *
41 * - The *bizarre* stuff (mapping/unmapping an interrupt to a VLPI, or
42 * issuing an INV after changing a priority) gets shoved into the
43 * irq_set_vcpu_affinity() method. While this is quite horrible
44 * (let's face it, this is the irqchip version of an ioctl), it
45 * confines the crap to a single location. And map/unmap really is
46 * about setting the affinity of a VLPI to a vcpu, so only INV is
47 * majorly out of place. So there.
48 *
49 * A number of commands are simply not provided by this interface, as
50 * they do not make direct sense. For example, MAPD is purely local to
51 * the virtual ITS (because it references a virtual device, and the
52 * physical ITS is still very much in charge of the physical
53 * device). Same goes for things like MAPC (the physical ITS deals
54 * with the actual vPE affinity, and not the braindead concept of
55 * collection). SYNC is not provided either, as each and every command
56 * is followed by a VSYNC. This could be relaxed in the future, should
57 * this be seen as a bottleneck (yes, this means *never*).
58 *
59 * But handling VLPIs is only one side of the job of the GICv4
60 * code. The other (darker) side is to take care of the doorbell
61 * interrupts which are delivered when a VLPI targeting a non-running
62 * vcpu is being made pending.
63 *
64 * The choice made here is that each vcpu (VPE in old northern GICv4
65 * dialect) gets a single doorbell LPI, no matter how many interrupts
66 * are targeting it. This has a nice property, which is that the
67 * interrupt becomes a handle for the VPE, and that the hypervisor
68 * code can manipulate it through the normal interrupt API:
69 *
70 * - VMs (or rather the VM abstraction that matters to the GIC)
71 * contain an irq domain where each interrupt maps to a VPE. In
72 * turn, this domain sits on top of the normal LPI allocator, and a
73 * specially crafted irq_chip implementation.
74 *
75 * - mask/unmask do what is expected on the doorbell interrupt.
76 *
77 * - irq_set_affinity is used to move a VPE from one redistributor to
78 * another.
79 *
80 * - irq_set_vcpu_affinity once again gets hijacked for the purpose of
81 * creating a new sub-API, namely scheduling/descheduling a VPE
82 * (which involves programming GICR_V{PROP,PEND}BASER) and
83 * performing INVALL operations.
84 */
85 #define IRQ_DOMAIN_WORD_SIZE 16
86
87 static struct irq_domain *gic_domain;
88 static const struct irq_domain_ops *vpe_domain_ops;
89 static const struct irq_domain_ops *sgi_domain_ops;
90
has_v4_1(void)91 static bool has_v4_1(void)
92 {
93 return !!sgi_domain_ops;
94 }
95
its_alloc_vcpu_sgis(struct its_vpe * vpe,int idx)96 static int its_alloc_vcpu_sgis(struct its_vpe *vpe, int idx)
97 {
98 char *name;
99 int sgi_base;
100
101 if (!has_v4_1()) {
102 return 0;
103 }
104
105 name = kasprintf(GFP_KERNEL, "GICv4-sgi-%d", task_pid_nr(current));
106 if (!name) {
107 goto err;
108 }
109
110 vpe->fwnode = irq_domain_alloc_named_id_fwnode(name, idx);
111 if (!vpe->fwnode) {
112 goto err;
113 }
114
115 kfree(name);
116 name = NULL;
117
118 vpe->sgi_domain = irq_domain_create_linear(vpe->fwnode, IRQ_DOMAIN_WORD_SIZE, sgi_domain_ops, vpe);
119 if (!vpe->sgi_domain) {
120 goto err;
121 }
122
123 sgi_base = __irq_domain_alloc_irqs(vpe->sgi_domain, -1, IRQ_DOMAIN_WORD_SIZE, NUMA_NO_NODE, vpe, false, NULL);
124 if (sgi_base <= 0) {
125 goto err;
126 }
127
128 return 0;
129
130 err:
131 if (vpe->sgi_domain) {
132 irq_domain_remove(vpe->sgi_domain);
133 }
134 if (vpe->fwnode) {
135 irq_domain_free_fwnode(vpe->fwnode);
136 }
137 kfree(name);
138 return -ENOMEM;
139 }
140
its_alloc_vcpu_irqs(struct its_vm * vm)141 int its_alloc_vcpu_irqs(struct its_vm *vm)
142 {
143 int vpe_base_irq, i;
144
145 vm->fwnode = irq_domain_alloc_named_id_fwnode("GICv4-vpe", task_pid_nr(current));
146 if (!vm->fwnode) {
147 goto err;
148 }
149
150 vm->domain = irq_domain_create_hierarchy(gic_domain, 0, vm->nr_vpes, vm->fwnode, vpe_domain_ops, vm);
151 if (!vm->domain) {
152 goto err;
153 }
154
155 for (i = 0; i < vm->nr_vpes; i++) {
156 vm->vpes[i]->its_vm = vm;
157 vm->vpes[i]->idai = true;
158 }
159
160 vpe_base_irq = __irq_domain_alloc_irqs(vm->domain, -1, vm->nr_vpes, NUMA_NO_NODE, vm, false, NULL);
161 if (vpe_base_irq <= 0) {
162 goto err;
163 }
164
165 for (i = 0; i < vm->nr_vpes; i++) {
166 int ret;
167 vm->vpes[i]->irq = vpe_base_irq + i;
168 ret = its_alloc_vcpu_sgis(vm->vpes[i], i);
169 if (ret) {
170 goto err;
171 }
172 }
173
174 return 0;
175
176 err:
177 if (vm->domain) {
178 irq_domain_remove(vm->domain);
179 }
180 if (vm->fwnode) {
181 irq_domain_free_fwnode(vm->fwnode);
182 }
183
184 return -ENOMEM;
185 }
186
its_free_sgi_irqs(struct its_vm * vm)187 static void its_free_sgi_irqs(struct its_vm *vm)
188 {
189 int i;
190
191 if (!has_v4_1()) {
192 return;
193 }
194
195 for (i = 0; i < vm->nr_vpes; i++) {
196 unsigned int irq = irq_find_mapping(vm->vpes[i]->sgi_domain, 0);
197 if (WARN_ON(!irq)) {
198 continue;
199 }
200
201 irq_domain_free_irqs(irq, IRQ_DOMAIN_WORD_SIZE);
202 irq_domain_remove(vm->vpes[i]->sgi_domain);
203 irq_domain_free_fwnode(vm->vpes[i]->fwnode);
204 }
205 }
206
its_free_vcpu_irqs(struct its_vm * vm)207 void its_free_vcpu_irqs(struct its_vm *vm)
208 {
209 its_free_sgi_irqs(vm);
210 irq_domain_free_irqs(vm->vpes[0]->irq, vm->nr_vpes);
211 irq_domain_remove(vm->domain);
212 irq_domain_free_fwnode(vm->fwnode);
213 }
214
its_send_vpe_cmd(struct its_vpe * vpe,struct its_cmd_info * info)215 static int its_send_vpe_cmd(struct its_vpe *vpe, struct its_cmd_info *info)
216 {
217 return irq_set_vcpu_affinity(vpe->irq, info);
218 }
219
its_make_vpe_non_resident(struct its_vpe * vpe,bool db)220 int its_make_vpe_non_resident(struct its_vpe *vpe, bool db)
221 {
222 struct irq_desc *desc = irq_to_desc(vpe->irq);
223 struct its_cmd_info info = {};
224 int ret;
225
226 WARN_ON(preemptible());
227
228 info.cmd_type = DESCHEDULE_VPE;
229 if (has_v4_1()) {
230 /* GICv4.1 can directly deal with doorbells */
231 info.req_db = db;
232 } else {
233 /* Undo the nested disable_irq() calls... */
234 while (db && irqd_irq_disabled(&desc->irq_data)) {
235 enable_irq(vpe->irq);
236 }
237 }
238
239 ret = its_send_vpe_cmd(vpe, &info);
240 if (!ret) {
241 vpe->resident = false;
242 }
243
244 vpe->ready = false;
245
246 return ret;
247 }
248
its_make_vpe_resident(struct its_vpe * vpe,bool g0en,bool g1en)249 int its_make_vpe_resident(struct its_vpe *vpe, bool g0en, bool g1en)
250 {
251 struct its_cmd_info info = {};
252 int ret;
253
254 WARN_ON(preemptible());
255
256 info.cmd_type = SCHEDULE_VPE;
257 if (has_v4_1()) {
258 info.g0en = g0en;
259 info.g1en = g1en;
260 } else {
261 /* Disabled the doorbell, as we're about to enter the guest */
262 disable_irq_nosync(vpe->irq);
263 }
264
265 ret = its_send_vpe_cmd(vpe, &info);
266 if (!ret) {
267 vpe->resident = true;
268 }
269
270 return ret;
271 }
272
its_commit_vpe(struct its_vpe * vpe)273 int its_commit_vpe(struct its_vpe *vpe)
274 {
275 struct its_cmd_info info = {
276 .cmd_type = COMMIT_VPE,
277 };
278 int ret;
279
280 WARN_ON(preemptible());
281
282 ret = its_send_vpe_cmd(vpe, &info);
283 if (!ret) {
284 vpe->ready = true;
285 }
286
287 return ret;
288 }
289
its_invall_vpe(struct its_vpe * vpe)290 int its_invall_vpe(struct its_vpe *vpe)
291 {
292 struct its_cmd_info info = {
293 .cmd_type = INVALL_VPE,
294 };
295
296 return its_send_vpe_cmd(vpe, &info);
297 }
298
its_map_vlpi(int irq,struct its_vlpi_map * map)299 int its_map_vlpi(int irq, struct its_vlpi_map *map)
300 {
301 struct its_cmd_info info = {
302 .cmd_type = MAP_VLPI,
303 {
304 .map = map,
305 },
306 };
307 int ret;
308
309 /*
310 * The host will never see that interrupt firing again, so it
311 * is vital that we don't do any lazy masking.
312 */
313 irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
314
315 ret = irq_set_vcpu_affinity(irq, &info);
316 if (ret) {
317 irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY);
318 }
319
320 return ret;
321 }
322
its_get_vlpi(int irq,struct its_vlpi_map * map)323 int its_get_vlpi(int irq, struct its_vlpi_map *map)
324 {
325 struct its_cmd_info info = {
326 .cmd_type = GET_VLPI,
327 {
328 .map = map,
329 },
330 };
331
332 return irq_set_vcpu_affinity(irq, &info);
333 }
334
its_unmap_vlpi(int irq)335 int its_unmap_vlpi(int irq)
336 {
337 irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY);
338 return irq_set_vcpu_affinity(irq, NULL);
339 }
340
its_prop_update_vlpi(int irq,u8 config,bool inv)341 int its_prop_update_vlpi(int irq, u8 config, bool inv)
342 {
343 struct its_cmd_info info = {
344 .cmd_type = inv ? PROP_UPDATE_AND_INV_VLPI : PROP_UPDATE_VLPI,
345 {
346 .config = config,
347 },
348 };
349
350 return irq_set_vcpu_affinity(irq, &info);
351 }
352
its_prop_update_vsgi(int irq,u8 priority,bool group)353 int its_prop_update_vsgi(int irq, u8 priority, bool group)
354 {
355 struct its_cmd_info info = {
356 .cmd_type = PROP_UPDATE_VSGI,
357 {
358 .priority = priority,
359 .group = group,
360 },
361 };
362
363 return irq_set_vcpu_affinity(irq, &info);
364 }
365
its_init_v4(struct irq_domain * domain,const struct irq_domain_ops * vpe_ops,const struct irq_domain_ops * sgi_ops)366 int its_init_v4(struct irq_domain *domain, const struct irq_domain_ops *vpe_ops, const struct irq_domain_ops *sgi_ops)
367 {
368 if (domain) {
369 pr_info("ITS: Enabling GICv4 support\n");
370 gic_domain = domain;
371 vpe_domain_ops = vpe_ops;
372 sgi_domain_ops = sgi_ops;
373 return 0;
374 }
375
376 pr_err("ITS: No GICv4 VPE domain allocated\n");
377 return -ENODEV;
378 }
379