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 /* We got the message, no need to fire again */
89 if (!irqd_irq_disabled(&irq_to_desc(irq)->irq_data))
90 disable_irq_nosync(irq);
92 vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last = true;
93 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
100 * vgic_v4_init - Initialize the GICv4 data structures
101 * @kvm: Pointer to the VM being initialized
103 * We may be called each time a vITS is created, or when the
104 * vgic is initialized. This relies on kvm->lock to be
105 * held. In both cases, the number of vcpus should now be
108 int vgic_v4_init(struct kvm *kvm)
110 struct vgic_dist *dist = &kvm->arch.vgic;
111 struct kvm_vcpu *vcpu;
112 int i, nr_vcpus, ret;
114 if (!kvm_vgic_global_state.has_gicv4)
115 return 0; /* Nothing to see here... move along. */
117 if (dist->its_vm.vpes)
120 nr_vcpus = atomic_read(&kvm->online_vcpus);
122 dist->its_vm.vpes = kcalloc(nr_vcpus, sizeof(*dist->its_vm.vpes),
124 if (!dist->its_vm.vpes)
127 dist->its_vm.nr_vpes = nr_vcpus;
129 kvm_for_each_vcpu(i, vcpu, kvm)
130 dist->its_vm.vpes[i] = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
132 ret = its_alloc_vcpu_irqs(&dist->its_vm);
134 kvm_err("VPE IRQ allocation failure\n");
135 kfree(dist->its_vm.vpes);
136 dist->its_vm.nr_vpes = 0;
137 dist->its_vm.vpes = NULL;
141 kvm_for_each_vcpu(i, vcpu, kvm) {
142 int irq = dist->its_vm.vpes[i]->irq;
145 * Don't automatically enable the doorbell, as we're
146 * flipping it back and forth when the vcpu gets
147 * blocked. Also disable the lazy disabling, as the
148 * doorbell could kick us out of the guest too
151 irq_set_status_flags(irq, DB_IRQ_FLAGS);
152 ret = request_irq(irq, vgic_v4_doorbell_handler,
155 kvm_err("failed to allocate vcpu IRQ%d\n", irq);
157 * Trick: adjust the number of vpes so we know
158 * how many to nuke on teardown...
160 dist->its_vm.nr_vpes = i;
166 vgic_v4_teardown(kvm);
172 * vgic_v4_teardown - Free the GICv4 data structures
173 * @kvm: Pointer to the VM being destroyed
175 * Relies on kvm->lock to be held.
177 void vgic_v4_teardown(struct kvm *kvm)
179 struct its_vm *its_vm = &kvm->arch.vgic.its_vm;
185 for (i = 0; i < its_vm->nr_vpes; i++) {
186 struct kvm_vcpu *vcpu = kvm_get_vcpu(kvm, i);
187 int irq = its_vm->vpes[i]->irq;
189 irq_clear_status_flags(irq, DB_IRQ_FLAGS);
193 its_free_vcpu_irqs(its_vm);
199 int vgic_v4_put(struct kvm_vcpu *vcpu, bool need_db)
201 struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
202 struct irq_desc *desc = irq_to_desc(vpe->irq);
204 if (!vgic_supports_direct_msis(vcpu->kvm) || !vpe->resident)
208 * If blocking, a doorbell is required. Undo the nested
209 * disable_irq() calls...
211 while (need_db && irqd_irq_disabled(&desc->irq_data))
212 enable_irq(vpe->irq);
214 return its_schedule_vpe(vpe, false);
217 int vgic_v4_load(struct kvm_vcpu *vcpu)
219 struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
222 if (!vgic_supports_direct_msis(vcpu->kvm) || vpe->resident)
226 * Before making the VPE resident, make sure the redistributor
227 * corresponding to our current CPU expects us here. See the
228 * doc in drivers/irqchip/irq-gic-v4.c to understand how this
229 * turns into a VMOVP command at the ITS level.
231 err = irq_set_affinity(vpe->irq, cpumask_of(smp_processor_id()));
235 /* Disabled the doorbell, as we're about to enter the guest */
236 disable_irq_nosync(vpe->irq);
238 err = its_schedule_vpe(vpe, true);
243 * Now that the VPE is resident, let's get rid of a potential
244 * doorbell interrupt that would still be pending.
246 return irq_set_irqchip_state(vpe->irq, IRQCHIP_STATE_PENDING, false);
249 static struct vgic_its *vgic_get_its(struct kvm *kvm,
250 struct kvm_kernel_irq_routing_entry *irq_entry)
252 struct kvm_msi msi = (struct kvm_msi) {
253 .address_lo = irq_entry->msi.address_lo,
254 .address_hi = irq_entry->msi.address_hi,
255 .data = irq_entry->msi.data,
256 .flags = irq_entry->msi.flags,
257 .devid = irq_entry->msi.devid,
260 return vgic_msi_to_its(kvm, &msi);
263 int kvm_vgic_v4_set_forwarding(struct kvm *kvm, int virq,
264 struct kvm_kernel_irq_routing_entry *irq_entry)
266 struct vgic_its *its;
267 struct vgic_irq *irq;
268 struct its_vlpi_map map;
271 if (!vgic_supports_direct_msis(kvm))
275 * Get the ITS, and escape early on error (not a valid
276 * doorbell for any of our vITSs).
278 its = vgic_get_its(kvm, irq_entry);
282 mutex_lock(&its->its_lock);
284 /* Perform the actual DevID/EventID -> LPI translation. */
285 ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
286 irq_entry->msi.data, &irq);
291 * Emit the mapping request. If it fails, the ITS probably
292 * isn't v4 compatible, so let's silently bail out. Holding
293 * the ITS lock should ensure that nothing can modify the
296 map = (struct its_vlpi_map) {
297 .vm = &kvm->arch.vgic.its_vm,
298 .vpe = &irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe,
299 .vintid = irq->intid,
300 .properties = ((irq->priority & 0xfc) |
301 (irq->enabled ? LPI_PROP_ENABLED : 0) |
306 ret = its_map_vlpi(virq, &map);
311 irq->host_irq = virq;
312 atomic_inc(&map.vpe->vlpi_count);
315 mutex_unlock(&its->its_lock);
319 int kvm_vgic_v4_unset_forwarding(struct kvm *kvm, int virq,
320 struct kvm_kernel_irq_routing_entry *irq_entry)
322 struct vgic_its *its;
323 struct vgic_irq *irq;
326 if (!vgic_supports_direct_msis(kvm))
330 * Get the ITS, and escape early on error (not a valid
331 * doorbell for any of our vITSs).
333 its = vgic_get_its(kvm, irq_entry);
337 mutex_lock(&its->its_lock);
339 ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
340 irq_entry->msi.data, &irq);
344 WARN_ON(!(irq->hw && irq->host_irq == virq));
346 atomic_dec(&irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count);
348 ret = its_unmap_vlpi(virq);
352 mutex_unlock(&its->its_lock);