1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2017 ARM Ltd.
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/kvm_host.h>
11*4882a593Smuzhiyun #include <linux/irqchip/arm-gic-v3.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include "vgic.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun * How KVM uses GICv4 (insert rude comments here):
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * The vgic-v4 layer acts as a bridge between several entities:
19*4882a593Smuzhiyun * - The GICv4 ITS representation offered by the ITS driver
20*4882a593Smuzhiyun * - VFIO, which is in charge of the PCI endpoint
21*4882a593Smuzhiyun * - The virtual ITS, which is the only thing the guest sees
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * The configuration of VLPIs is triggered by a callback from VFIO,
24*4882a593Smuzhiyun * instructing KVM that a PCI device has been configured to deliver
25*4882a593Smuzhiyun * MSIs to a vITS.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * kvm_vgic_v4_set_forwarding() is thus called with the routing entry,
28*4882a593Smuzhiyun * and this is used to find the corresponding vITS data structures
29*4882a593Smuzhiyun * (ITS instance, device, event and irq) using a process that is
30*4882a593Smuzhiyun * extremely similar to the injection of an MSI.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * At this stage, we can link the guest's view of an LPI (uniquely
33*4882a593Smuzhiyun * identified by the routing entry) and the host irq, using the GICv4
34*4882a593Smuzhiyun * driver mapping operation. Should the mapping succeed, we've then
35*4882a593Smuzhiyun * successfully upgraded the guest's LPI to a VLPI. We can then start
36*4882a593Smuzhiyun * with updating GICv4's view of the property table and generating an
37*4882a593Smuzhiyun * INValidation in order to kickstart the delivery of this VLPI to the
38*4882a593Smuzhiyun * guest directly, without software intervention. Well, almost.
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * When the PCI endpoint is deconfigured, this operation is reversed
41*4882a593Smuzhiyun * with VFIO calling kvm_vgic_v4_unset_forwarding().
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * Once the VLPI has been mapped, it needs to follow any change the
44*4882a593Smuzhiyun * guest performs on its LPI through the vITS. For that, a number of
45*4882a593Smuzhiyun * command handlers have hooks to communicate these changes to the HW:
46*4882a593Smuzhiyun * - Any invalidation triggers a call to its_prop_update_vlpi()
47*4882a593Smuzhiyun * - The INT command results in a irq_set_irqchip_state(), which
48*4882a593Smuzhiyun * generates an INT on the corresponding VLPI.
49*4882a593Smuzhiyun * - The CLEAR command results in a irq_set_irqchip_state(), which
50*4882a593Smuzhiyun * generates an CLEAR on the corresponding VLPI.
51*4882a593Smuzhiyun * - DISCARD translates into an unmap, similar to a call to
52*4882a593Smuzhiyun * kvm_vgic_v4_unset_forwarding().
53*4882a593Smuzhiyun * - MOVI is translated by an update of the existing mapping, changing
54*4882a593Smuzhiyun * the target vcpu, resulting in a VMOVI being generated.
55*4882a593Smuzhiyun * - MOVALL is translated by a string of mapping updates (similar to
56*4882a593Smuzhiyun * the handling of MOVI). MOVALL is horrible.
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * Note that a DISCARD/MAPTI sequence emitted from the guest without
59*4882a593Smuzhiyun * reprogramming the PCI endpoint after MAPTI does not result in a
60*4882a593Smuzhiyun * VLPI being mapped, as there is no callback from VFIO (the guest
61*4882a593Smuzhiyun * will get the interrupt via the normal SW injection). Fixing this is
62*4882a593Smuzhiyun * not trivial, and requires some horrible messing with the VFIO
63*4882a593Smuzhiyun * internals. Not fun. Don't do that.
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * Then there is the scheduling. Each time a vcpu is about to run on a
66*4882a593Smuzhiyun * physical CPU, KVM must tell the corresponding redistributor about
67*4882a593Smuzhiyun * it. And if we've migrated our vcpu from one CPU to another, we must
68*4882a593Smuzhiyun * tell the ITS (so that the messages reach the right redistributor).
69*4882a593Smuzhiyun * This is done in two steps: first issue a irq_set_affinity() on the
70*4882a593Smuzhiyun * irq corresponding to the vcpu, then call its_make_vpe_resident().
71*4882a593Smuzhiyun * You must be in a non-preemptible context. On exit, a call to
72*4882a593Smuzhiyun * its_make_vpe_non_resident() tells the redistributor that we're done
73*4882a593Smuzhiyun * with the vcpu.
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * Finally, the doorbell handling: Each vcpu is allocated an interrupt
76*4882a593Smuzhiyun * which will fire each time a VLPI is made pending whilst the vcpu is
77*4882a593Smuzhiyun * not running. Each time the vcpu gets blocked, the doorbell
78*4882a593Smuzhiyun * interrupt gets enabled. When the vcpu is unblocked (for whatever
79*4882a593Smuzhiyun * reason), the doorbell interrupt is disabled.
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun #define DB_IRQ_FLAGS (IRQ_NOAUTOEN | IRQ_DISABLE_UNLAZY | IRQ_NO_BALANCING)
83*4882a593Smuzhiyun
vgic_v4_doorbell_handler(int irq,void * info)84*4882a593Smuzhiyun static irqreturn_t vgic_v4_doorbell_handler(int irq, void *info)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun struct kvm_vcpu *vcpu = info;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /* We got the message, no need to fire again */
89*4882a593Smuzhiyun if (!kvm_vgic_global_state.has_gicv4_1 &&
90*4882a593Smuzhiyun !irqd_irq_disabled(&irq_to_desc(irq)->irq_data))
91*4882a593Smuzhiyun disable_irq_nosync(irq);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * The v4.1 doorbell can fire concurrently with the vPE being
95*4882a593Smuzhiyun * made non-resident. Ensure we only update pending_last
96*4882a593Smuzhiyun * *after* the non-residency sequence has completed.
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun raw_spin_lock(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vpe_lock);
99*4882a593Smuzhiyun vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last = true;
100*4882a593Smuzhiyun raw_spin_unlock(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vpe_lock);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
103*4882a593Smuzhiyun kvm_vcpu_kick(vcpu);
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun return IRQ_HANDLED;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
vgic_v4_sync_sgi_config(struct its_vpe * vpe,struct vgic_irq * irq)108*4882a593Smuzhiyun static void vgic_v4_sync_sgi_config(struct its_vpe *vpe, struct vgic_irq *irq)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun vpe->sgi_config[irq->intid].enabled = irq->enabled;
111*4882a593Smuzhiyun vpe->sgi_config[irq->intid].group = irq->group;
112*4882a593Smuzhiyun vpe->sgi_config[irq->intid].priority = irq->priority;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
vgic_v4_enable_vsgis(struct kvm_vcpu * vcpu)115*4882a593Smuzhiyun static void vgic_v4_enable_vsgis(struct kvm_vcpu *vcpu)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
118*4882a593Smuzhiyun int i;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun * With GICv4.1, every virtual SGI can be directly injected. So
122*4882a593Smuzhiyun * let's pretend that they are HW interrupts, tied to a host
123*4882a593Smuzhiyun * IRQ. The SGI code will do its magic.
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun for (i = 0; i < VGIC_NR_SGIS; i++) {
126*4882a593Smuzhiyun struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, i);
127*4882a593Smuzhiyun struct irq_desc *desc;
128*4882a593Smuzhiyun unsigned long flags;
129*4882a593Smuzhiyun int ret;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun raw_spin_lock_irqsave(&irq->irq_lock, flags);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (irq->hw)
134*4882a593Smuzhiyun goto unlock;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun irq->hw = true;
137*4882a593Smuzhiyun irq->host_irq = irq_find_mapping(vpe->sgi_domain, i);
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /* Transfer the full irq state to the vPE */
140*4882a593Smuzhiyun vgic_v4_sync_sgi_config(vpe, irq);
141*4882a593Smuzhiyun desc = irq_to_desc(irq->host_irq);
142*4882a593Smuzhiyun ret = irq_domain_activate_irq(irq_desc_get_irq_data(desc),
143*4882a593Smuzhiyun false);
144*4882a593Smuzhiyun if (!WARN_ON(ret)) {
145*4882a593Smuzhiyun /* Transfer pending state */
146*4882a593Smuzhiyun ret = irq_set_irqchip_state(irq->host_irq,
147*4882a593Smuzhiyun IRQCHIP_STATE_PENDING,
148*4882a593Smuzhiyun irq->pending_latch);
149*4882a593Smuzhiyun WARN_ON(ret);
150*4882a593Smuzhiyun irq->pending_latch = false;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun unlock:
153*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
154*4882a593Smuzhiyun vgic_put_irq(vcpu->kvm, irq);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
vgic_v4_disable_vsgis(struct kvm_vcpu * vcpu)158*4882a593Smuzhiyun static void vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun int i;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun for (i = 0; i < VGIC_NR_SGIS; i++) {
163*4882a593Smuzhiyun struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, i);
164*4882a593Smuzhiyun struct irq_desc *desc;
165*4882a593Smuzhiyun unsigned long flags;
166*4882a593Smuzhiyun int ret;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun raw_spin_lock_irqsave(&irq->irq_lock, flags);
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if (!irq->hw)
171*4882a593Smuzhiyun goto unlock;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun irq->hw = false;
174*4882a593Smuzhiyun ret = irq_get_irqchip_state(irq->host_irq,
175*4882a593Smuzhiyun IRQCHIP_STATE_PENDING,
176*4882a593Smuzhiyun &irq->pending_latch);
177*4882a593Smuzhiyun WARN_ON(ret);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun desc = irq_to_desc(irq->host_irq);
180*4882a593Smuzhiyun irq_domain_deactivate_irq(irq_desc_get_irq_data(desc));
181*4882a593Smuzhiyun unlock:
182*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
183*4882a593Smuzhiyun vgic_put_irq(vcpu->kvm, irq);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /* Must be called with the kvm lock held */
vgic_v4_configure_vsgis(struct kvm * kvm)188*4882a593Smuzhiyun void vgic_v4_configure_vsgis(struct kvm *kvm)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun struct vgic_dist *dist = &kvm->arch.vgic;
191*4882a593Smuzhiyun struct kvm_vcpu *vcpu;
192*4882a593Smuzhiyun int i;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun kvm_arm_halt_guest(kvm);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun kvm_for_each_vcpu(i, vcpu, kvm) {
197*4882a593Smuzhiyun if (dist->nassgireq)
198*4882a593Smuzhiyun vgic_v4_enable_vsgis(vcpu);
199*4882a593Smuzhiyun else
200*4882a593Smuzhiyun vgic_v4_disable_vsgis(vcpu);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun kvm_arm_resume_guest(kvm);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /**
207*4882a593Smuzhiyun * vgic_v4_init - Initialize the GICv4 data structures
208*4882a593Smuzhiyun * @kvm: Pointer to the VM being initialized
209*4882a593Smuzhiyun *
210*4882a593Smuzhiyun * We may be called each time a vITS is created, or when the
211*4882a593Smuzhiyun * vgic is initialized. This relies on kvm->lock to be
212*4882a593Smuzhiyun * held. In both cases, the number of vcpus should now be
213*4882a593Smuzhiyun * fixed.
214*4882a593Smuzhiyun */
vgic_v4_init(struct kvm * kvm)215*4882a593Smuzhiyun int vgic_v4_init(struct kvm *kvm)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun struct vgic_dist *dist = &kvm->arch.vgic;
218*4882a593Smuzhiyun struct kvm_vcpu *vcpu;
219*4882a593Smuzhiyun int i, nr_vcpus, ret;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun if (!kvm_vgic_global_state.has_gicv4)
222*4882a593Smuzhiyun return 0; /* Nothing to see here... move along. */
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun if (dist->its_vm.vpes)
225*4882a593Smuzhiyun return 0;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun nr_vcpus = atomic_read(&kvm->online_vcpus);
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun dist->its_vm.vpes = kcalloc(nr_vcpus, sizeof(*dist->its_vm.vpes),
230*4882a593Smuzhiyun GFP_KERNEL);
231*4882a593Smuzhiyun if (!dist->its_vm.vpes)
232*4882a593Smuzhiyun return -ENOMEM;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun dist->its_vm.nr_vpes = nr_vcpus;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun kvm_for_each_vcpu(i, vcpu, kvm)
237*4882a593Smuzhiyun dist->its_vm.vpes[i] = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun ret = its_alloc_vcpu_irqs(&dist->its_vm);
240*4882a593Smuzhiyun if (ret < 0) {
241*4882a593Smuzhiyun kvm_err("VPE IRQ allocation failure\n");
242*4882a593Smuzhiyun kfree(dist->its_vm.vpes);
243*4882a593Smuzhiyun dist->its_vm.nr_vpes = 0;
244*4882a593Smuzhiyun dist->its_vm.vpes = NULL;
245*4882a593Smuzhiyun return ret;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun kvm_for_each_vcpu(i, vcpu, kvm) {
249*4882a593Smuzhiyun int irq = dist->its_vm.vpes[i]->irq;
250*4882a593Smuzhiyun unsigned long irq_flags = DB_IRQ_FLAGS;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /*
253*4882a593Smuzhiyun * Don't automatically enable the doorbell, as we're
254*4882a593Smuzhiyun * flipping it back and forth when the vcpu gets
255*4882a593Smuzhiyun * blocked. Also disable the lazy disabling, as the
256*4882a593Smuzhiyun * doorbell could kick us out of the guest too
257*4882a593Smuzhiyun * early...
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * On GICv4.1, the doorbell is managed in HW and must
260*4882a593Smuzhiyun * be left enabled.
261*4882a593Smuzhiyun */
262*4882a593Smuzhiyun if (kvm_vgic_global_state.has_gicv4_1)
263*4882a593Smuzhiyun irq_flags &= ~IRQ_NOAUTOEN;
264*4882a593Smuzhiyun irq_set_status_flags(irq, irq_flags);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun ret = request_irq(irq, vgic_v4_doorbell_handler,
267*4882a593Smuzhiyun 0, "vcpu", vcpu);
268*4882a593Smuzhiyun if (ret) {
269*4882a593Smuzhiyun kvm_err("failed to allocate vcpu IRQ%d\n", irq);
270*4882a593Smuzhiyun /*
271*4882a593Smuzhiyun * Trick: adjust the number of vpes so we know
272*4882a593Smuzhiyun * how many to nuke on teardown...
273*4882a593Smuzhiyun */
274*4882a593Smuzhiyun dist->its_vm.nr_vpes = i;
275*4882a593Smuzhiyun break;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun if (ret)
280*4882a593Smuzhiyun vgic_v4_teardown(kvm);
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun return ret;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /**
286*4882a593Smuzhiyun * vgic_v4_teardown - Free the GICv4 data structures
287*4882a593Smuzhiyun * @kvm: Pointer to the VM being destroyed
288*4882a593Smuzhiyun *
289*4882a593Smuzhiyun * Relies on kvm->lock to be held.
290*4882a593Smuzhiyun */
vgic_v4_teardown(struct kvm * kvm)291*4882a593Smuzhiyun void vgic_v4_teardown(struct kvm *kvm)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun struct its_vm *its_vm = &kvm->arch.vgic.its_vm;
294*4882a593Smuzhiyun int i;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun if (!its_vm->vpes)
297*4882a593Smuzhiyun return;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun for (i = 0; i < its_vm->nr_vpes; i++) {
300*4882a593Smuzhiyun struct kvm_vcpu *vcpu = kvm_get_vcpu(kvm, i);
301*4882a593Smuzhiyun int irq = its_vm->vpes[i]->irq;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun irq_clear_status_flags(irq, DB_IRQ_FLAGS);
304*4882a593Smuzhiyun free_irq(irq, vcpu);
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun its_free_vcpu_irqs(its_vm);
308*4882a593Smuzhiyun kfree(its_vm->vpes);
309*4882a593Smuzhiyun its_vm->nr_vpes = 0;
310*4882a593Smuzhiyun its_vm->vpes = NULL;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
vgic_v4_put(struct kvm_vcpu * vcpu,bool need_db)313*4882a593Smuzhiyun int vgic_v4_put(struct kvm_vcpu *vcpu, bool need_db)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun if (!vgic_supports_direct_msis(vcpu->kvm) || !vpe->resident)
318*4882a593Smuzhiyun return 0;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun return its_make_vpe_non_resident(vpe, need_db);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
vgic_v4_load(struct kvm_vcpu * vcpu)323*4882a593Smuzhiyun int vgic_v4_load(struct kvm_vcpu *vcpu)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
326*4882a593Smuzhiyun int err;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun if (!vgic_supports_direct_msis(vcpu->kvm) || vpe->resident)
329*4882a593Smuzhiyun return 0;
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun /*
332*4882a593Smuzhiyun * Before making the VPE resident, make sure the redistributor
333*4882a593Smuzhiyun * corresponding to our current CPU expects us here. See the
334*4882a593Smuzhiyun * doc in drivers/irqchip/irq-gic-v4.c to understand how this
335*4882a593Smuzhiyun * turns into a VMOVP command at the ITS level.
336*4882a593Smuzhiyun */
337*4882a593Smuzhiyun err = irq_set_affinity(vpe->irq, cpumask_of(smp_processor_id()));
338*4882a593Smuzhiyun if (err)
339*4882a593Smuzhiyun return err;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun err = its_make_vpe_resident(vpe, false, vcpu->kvm->arch.vgic.enabled);
342*4882a593Smuzhiyun if (err)
343*4882a593Smuzhiyun return err;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun /*
346*4882a593Smuzhiyun * Now that the VPE is resident, let's get rid of a potential
347*4882a593Smuzhiyun * doorbell interrupt that would still be pending. This is a
348*4882a593Smuzhiyun * GICv4.0 only "feature"...
349*4882a593Smuzhiyun */
350*4882a593Smuzhiyun if (!kvm_vgic_global_state.has_gicv4_1)
351*4882a593Smuzhiyun err = irq_set_irqchip_state(vpe->irq, IRQCHIP_STATE_PENDING, false);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun return err;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun
vgic_v4_commit(struct kvm_vcpu * vcpu)356*4882a593Smuzhiyun void vgic_v4_commit(struct kvm_vcpu *vcpu)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun /*
361*4882a593Smuzhiyun * No need to wait for the vPE to be ready across a shallow guest
362*4882a593Smuzhiyun * exit, as only a vcpu_put will invalidate it.
363*4882a593Smuzhiyun */
364*4882a593Smuzhiyun if (!vpe->ready)
365*4882a593Smuzhiyun its_commit_vpe(vpe);
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
vgic_get_its(struct kvm * kvm,struct kvm_kernel_irq_routing_entry * irq_entry)368*4882a593Smuzhiyun static struct vgic_its *vgic_get_its(struct kvm *kvm,
369*4882a593Smuzhiyun struct kvm_kernel_irq_routing_entry *irq_entry)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun struct kvm_msi msi = (struct kvm_msi) {
372*4882a593Smuzhiyun .address_lo = irq_entry->msi.address_lo,
373*4882a593Smuzhiyun .address_hi = irq_entry->msi.address_hi,
374*4882a593Smuzhiyun .data = irq_entry->msi.data,
375*4882a593Smuzhiyun .flags = irq_entry->msi.flags,
376*4882a593Smuzhiyun .devid = irq_entry->msi.devid,
377*4882a593Smuzhiyun };
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun return vgic_msi_to_its(kvm, &msi);
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun
kvm_vgic_v4_set_forwarding(struct kvm * kvm,int virq,struct kvm_kernel_irq_routing_entry * irq_entry)382*4882a593Smuzhiyun int kvm_vgic_v4_set_forwarding(struct kvm *kvm, int virq,
383*4882a593Smuzhiyun struct kvm_kernel_irq_routing_entry *irq_entry)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun struct vgic_its *its;
386*4882a593Smuzhiyun struct vgic_irq *irq;
387*4882a593Smuzhiyun struct its_vlpi_map map;
388*4882a593Smuzhiyun int ret;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (!vgic_supports_direct_msis(kvm))
391*4882a593Smuzhiyun return 0;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun /*
394*4882a593Smuzhiyun * Get the ITS, and escape early on error (not a valid
395*4882a593Smuzhiyun * doorbell for any of our vITSs).
396*4882a593Smuzhiyun */
397*4882a593Smuzhiyun its = vgic_get_its(kvm, irq_entry);
398*4882a593Smuzhiyun if (IS_ERR(its))
399*4882a593Smuzhiyun return 0;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun mutex_lock(&its->its_lock);
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun /* Perform the actual DevID/EventID -> LPI translation. */
404*4882a593Smuzhiyun ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
405*4882a593Smuzhiyun irq_entry->msi.data, &irq);
406*4882a593Smuzhiyun if (ret)
407*4882a593Smuzhiyun goto out;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /*
410*4882a593Smuzhiyun * Emit the mapping request. If it fails, the ITS probably
411*4882a593Smuzhiyun * isn't v4 compatible, so let's silently bail out. Holding
412*4882a593Smuzhiyun * the ITS lock should ensure that nothing can modify the
413*4882a593Smuzhiyun * target vcpu.
414*4882a593Smuzhiyun */
415*4882a593Smuzhiyun map = (struct its_vlpi_map) {
416*4882a593Smuzhiyun .vm = &kvm->arch.vgic.its_vm,
417*4882a593Smuzhiyun .vpe = &irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe,
418*4882a593Smuzhiyun .vintid = irq->intid,
419*4882a593Smuzhiyun .properties = ((irq->priority & 0xfc) |
420*4882a593Smuzhiyun (irq->enabled ? LPI_PROP_ENABLED : 0) |
421*4882a593Smuzhiyun LPI_PROP_GROUP1),
422*4882a593Smuzhiyun .db_enabled = true,
423*4882a593Smuzhiyun };
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun ret = its_map_vlpi(virq, &map);
426*4882a593Smuzhiyun if (ret)
427*4882a593Smuzhiyun goto out;
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun irq->hw = true;
430*4882a593Smuzhiyun irq->host_irq = virq;
431*4882a593Smuzhiyun atomic_inc(&map.vpe->vlpi_count);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun out:
434*4882a593Smuzhiyun mutex_unlock(&its->its_lock);
435*4882a593Smuzhiyun return ret;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
kvm_vgic_v4_unset_forwarding(struct kvm * kvm,int virq,struct kvm_kernel_irq_routing_entry * irq_entry)438*4882a593Smuzhiyun int kvm_vgic_v4_unset_forwarding(struct kvm *kvm, int virq,
439*4882a593Smuzhiyun struct kvm_kernel_irq_routing_entry *irq_entry)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun struct vgic_its *its;
442*4882a593Smuzhiyun struct vgic_irq *irq;
443*4882a593Smuzhiyun int ret;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun if (!vgic_supports_direct_msis(kvm))
446*4882a593Smuzhiyun return 0;
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun /*
449*4882a593Smuzhiyun * Get the ITS, and escape early on error (not a valid
450*4882a593Smuzhiyun * doorbell for any of our vITSs).
451*4882a593Smuzhiyun */
452*4882a593Smuzhiyun its = vgic_get_its(kvm, irq_entry);
453*4882a593Smuzhiyun if (IS_ERR(its))
454*4882a593Smuzhiyun return 0;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun mutex_lock(&its->its_lock);
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
459*4882a593Smuzhiyun irq_entry->msi.data, &irq);
460*4882a593Smuzhiyun if (ret)
461*4882a593Smuzhiyun goto out;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun WARN_ON(!(irq->hw && irq->host_irq == virq));
464*4882a593Smuzhiyun if (irq->hw) {
465*4882a593Smuzhiyun atomic_dec(&irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count);
466*4882a593Smuzhiyun irq->hw = false;
467*4882a593Smuzhiyun ret = its_unmap_vlpi(virq);
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun out:
471*4882a593Smuzhiyun mutex_unlock(&its->its_lock);
472*4882a593Smuzhiyun return ret;
473*4882a593Smuzhiyun }
474