1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 ARM Ltd.
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #include <linux/interrupt.h>
9 #include <linux/irqdomain.h>
10 #include <linux/kvm_host.h>
11 #include <linux/irqchip/arm-gic-v3.h>
16 * How KVM uses GICv4 (insert rude comments here):
18 * The vgic-v4 layer acts as a bridge between several entities:
19 * - The GICv4 ITS representation offered by the ITS driver
20 * - VFIO, which is in charge of the PCI endpoint
21 * - The virtual ITS, which is the only thing the guest sees
23 * The configuration of VLPIs is triggered by a callback from VFIO,
24 * instructing KVM that a PCI device has been configured to deliver
27 * kvm_vgic_v4_set_forwarding() is thus called with the routing entry,
28 * and this is used to find the corresponding vITS data structures
29 * (ITS instance, device, event and irq) using a process that is
30 * extremely similar to the injection of an MSI.
32 * At this stage, we can link the guest's view of an LPI (uniquely
33 * identified by the routing entry) and the host irq, using the GICv4
34 * driver mapping operation. Should the mapping succeed, we've then
35 * successfully upgraded the guest's LPI to a VLPI. We can then start
36 * with updating GICv4's view of the property table and generating an
37 * INValidation in order to kickstart the delivery of this VLPI to the
38 * guest directly, without software intervention. Well, almost.
40 * When the PCI endpoint is deconfigured, this operation is reversed
41 * with VFIO calling kvm_vgic_v4_unset_forwarding().
43 * Once the VLPI has been mapped, it needs to follow any change the
44 * guest performs on its LPI through the vITS. For that, a number of
45 * command handlers have hooks to communicate these changes to the HW:
46 * - Any invalidation triggers a call to its_prop_update_vlpi()
47 * - The INT command results in a irq_set_irqchip_state(), which
48 * generates an INT on the corresponding VLPI.
49 * - The CLEAR command results in a irq_set_irqchip_state(), which
50 * generates an CLEAR on the corresponding VLPI.
51 * - DISCARD translates into an unmap, similar to a call to
52 * kvm_vgic_v4_unset_forwarding().
53 * - MOVI is translated by an update of the existing mapping, changing
54 * the target vcpu, resulting in a VMOVI being generated.
55 * - MOVALL is translated by a string of mapping updates (similar to
56 * the handling of MOVI). MOVALL is horrible.
58 * Note that a DISCARD/MAPTI sequence emitted from the guest without
59 * reprogramming the PCI endpoint after MAPTI does not result in a
60 * VLPI being mapped, as there is no callback from VFIO (the guest
61 * will get the interrupt via the normal SW injection). Fixing this is
62 * not trivial, and requires some horrible messing with the VFIO
63 * internals. Not fun. Don't do that.
65 * Then there is the scheduling. Each time a vcpu is about to run on a
66 * physical CPU, KVM must tell the corresponding redistributor about
67 * it. And if we've migrated our vcpu from one CPU to another, we must
68 * tell the ITS (so that the messages reach the right redistributor).
69 * This is done in two steps: first issue a irq_set_affinity() on the
70 * irq corresponding to the vcpu, then call its_schedule_vpe(). You
71 * must be in a non-preemptible context. On exit, another call to
72 * its_schedule_vpe() tells the redistributor that we're done with the
75 * Finally, the doorbell handling: Each vcpu is allocated an interrupt
76 * which will fire each time a VLPI is made pending whilst the vcpu is
77 * not running. Each time the vcpu gets blocked, the doorbell
78 * interrupt gets enabled. When the vcpu is unblocked (for whatever
79 * reason), the doorbell interrupt is disabled.
82 #define DB_IRQ_FLAGS (IRQ_NOAUTOEN | IRQ_DISABLE_UNLAZY | IRQ_NO_BALANCING)
84 static irqreturn_t vgic_v4_doorbell_handler(int irq, void *info)
86 struct kvm_vcpu *vcpu = info;
88 vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last = true;
89 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
96 * vgic_v4_init - Initialize the GICv4 data structures
97 * @kvm: Pointer to the VM being initialized
99 * We may be called each time a vITS is created, or when the
100 * vgic is initialized. This relies on kvm->lock to be
101 * held. In both cases, the number of vcpus should now be
104 int vgic_v4_init(struct kvm *kvm)
106 struct vgic_dist *dist = &kvm->arch.vgic;
107 struct kvm_vcpu *vcpu;
108 int i, nr_vcpus, ret;
110 if (!kvm_vgic_global_state.has_gicv4)
111 return 0; /* Nothing to see here... move along. */
113 if (dist->its_vm.vpes)
116 nr_vcpus = atomic_read(&kvm->online_vcpus);
118 dist->its_vm.vpes = kcalloc(nr_vcpus, sizeof(*dist->its_vm.vpes),
120 if (!dist->its_vm.vpes)
123 dist->its_vm.nr_vpes = nr_vcpus;
125 kvm_for_each_vcpu(i, vcpu, kvm)
126 dist->its_vm.vpes[i] = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
128 ret = its_alloc_vcpu_irqs(&dist->its_vm);
130 kvm_err("VPE IRQ allocation failure\n");
131 kfree(dist->its_vm.vpes);
132 dist->its_vm.nr_vpes = 0;
133 dist->its_vm.vpes = NULL;
137 kvm_for_each_vcpu(i, vcpu, kvm) {
138 int irq = dist->its_vm.vpes[i]->irq;
141 * Don't automatically enable the doorbell, as we're
142 * flipping it back and forth when the vcpu gets
143 * blocked. Also disable the lazy disabling, as the
144 * doorbell could kick us out of the guest too
147 irq_set_status_flags(irq, DB_IRQ_FLAGS);
148 ret = request_irq(irq, vgic_v4_doorbell_handler,
151 kvm_err("failed to allocate vcpu IRQ%d\n", irq);
153 * Trick: adjust the number of vpes so we know
154 * how many to nuke on teardown...
156 dist->its_vm.nr_vpes = i;
162 vgic_v4_teardown(kvm);
168 * vgic_v4_teardown - Free the GICv4 data structures
169 * @kvm: Pointer to the VM being destroyed
171 * Relies on kvm->lock to be held.
173 void vgic_v4_teardown(struct kvm *kvm)
175 struct its_vm *its_vm = &kvm->arch.vgic.its_vm;
181 for (i = 0; i < its_vm->nr_vpes; i++) {
182 struct kvm_vcpu *vcpu = kvm_get_vcpu(kvm, i);
183 int irq = its_vm->vpes[i]->irq;
185 irq_clear_status_flags(irq, DB_IRQ_FLAGS);
189 its_free_vcpu_irqs(its_vm);
195 int vgic_v4_sync_hwstate(struct kvm_vcpu *vcpu)
197 if (!vgic_supports_direct_msis(vcpu->kvm))
200 return its_schedule_vpe(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe, false);
203 int vgic_v4_flush_hwstate(struct kvm_vcpu *vcpu)
205 int irq = vcpu->arch.vgic_cpu.vgic_v3.its_vpe.irq;
208 if (!vgic_supports_direct_msis(vcpu->kvm))
212 * Before making the VPE resident, make sure the redistributor
213 * corresponding to our current CPU expects us here. See the
214 * doc in drivers/irqchip/irq-gic-v4.c to understand how this
215 * turns into a VMOVP command at the ITS level.
217 err = irq_set_affinity(irq, cpumask_of(smp_processor_id()));
221 err = its_schedule_vpe(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe, true);
226 * Now that the VPE is resident, let's get rid of a potential
227 * doorbell interrupt that would still be pending.
229 err = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, false);
234 static struct vgic_its *vgic_get_its(struct kvm *kvm,
235 struct kvm_kernel_irq_routing_entry *irq_entry)
237 struct kvm_msi msi = (struct kvm_msi) {
238 .address_lo = irq_entry->msi.address_lo,
239 .address_hi = irq_entry->msi.address_hi,
240 .data = irq_entry->msi.data,
241 .flags = irq_entry->msi.flags,
242 .devid = irq_entry->msi.devid,
245 return vgic_msi_to_its(kvm, &msi);
248 int kvm_vgic_v4_set_forwarding(struct kvm *kvm, int virq,
249 struct kvm_kernel_irq_routing_entry *irq_entry)
251 struct vgic_its *its;
252 struct vgic_irq *irq;
253 struct its_vlpi_map map;
256 if (!vgic_supports_direct_msis(kvm))
260 * Get the ITS, and escape early on error (not a valid
261 * doorbell for any of our vITSs).
263 its = vgic_get_its(kvm, irq_entry);
267 mutex_lock(&its->its_lock);
269 /* Perform then actual DevID/EventID -> LPI translation. */
270 ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
271 irq_entry->msi.data, &irq);
276 * Emit the mapping request. If it fails, the ITS probably
277 * isn't v4 compatible, so let's silently bail out. Holding
278 * the ITS lock should ensure that nothing can modify the
281 map = (struct its_vlpi_map) {
282 .vm = &kvm->arch.vgic.its_vm,
283 .vpe = &irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe,
284 .vintid = irq->intid,
285 .properties = ((irq->priority & 0xfc) |
286 (irq->enabled ? LPI_PROP_ENABLED : 0) |
291 ret = its_map_vlpi(virq, &map);
296 irq->host_irq = virq;
299 mutex_unlock(&its->its_lock);
303 int kvm_vgic_v4_unset_forwarding(struct kvm *kvm, int virq,
304 struct kvm_kernel_irq_routing_entry *irq_entry)
306 struct vgic_its *its;
307 struct vgic_irq *irq;
310 if (!vgic_supports_direct_msis(kvm))
314 * Get the ITS, and escape early on error (not a valid
315 * doorbell for any of our vITSs).
317 its = vgic_get_its(kvm, irq_entry);
321 mutex_lock(&its->its_lock);
323 ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
324 irq_entry->msi.data, &irq);
328 WARN_ON(!(irq->hw && irq->host_irq == virq));
331 ret = its_unmap_vlpi(virq);
335 mutex_unlock(&its->its_lock);
339 void kvm_vgic_v4_enable_doorbell(struct kvm_vcpu *vcpu)
341 if (vgic_supports_direct_msis(vcpu->kvm)) {
342 int irq = vcpu->arch.vgic_cpu.vgic_v3.its_vpe.irq;
348 void kvm_vgic_v4_disable_doorbell(struct kvm_vcpu *vcpu)
350 if (vgic_supports_direct_msis(vcpu->kvm)) {
351 int irq = vcpu->arch.vgic_cpu.vgic_v3.its_vpe.irq;