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