Merge tag 'kvmarm-5.5' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm...
[sfrench/cifs-2.6.git] / virt / kvm / arm / vgic / vgic-v4.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 ARM Ltd.
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/kvm_host.h>
11 #include <linux/irqchip/arm-gic-v3.h>
12
13 #include "vgic.h"
14
15 /*
16  * How KVM uses GICv4 (insert rude comments here):
17  *
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
22  *
23  * The configuration of VLPIs is triggered by a callback from VFIO,
24  * instructing KVM that a PCI device has been configured to deliver
25  * MSIs to a vITS.
26  *
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.
31  *
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.
39  *
40  * When the PCI endpoint is deconfigured, this operation is reversed
41  * with VFIO calling kvm_vgic_v4_unset_forwarding().
42  *
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.
57  *
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.
64  *
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
73  * vcpu.
74  *
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.
80  */
81
82 #define DB_IRQ_FLAGS    (IRQ_NOAUTOEN | IRQ_DISABLE_UNLAZY | IRQ_NO_BALANCING)
83
84 static irqreturn_t vgic_v4_doorbell_handler(int irq, void *info)
85 {
86         struct kvm_vcpu *vcpu = info;
87
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);
91
92         vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last = true;
93         kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
94         kvm_vcpu_kick(vcpu);
95
96         return IRQ_HANDLED;
97 }
98
99 /**
100  * vgic_v4_init - Initialize the GICv4 data structures
101  * @kvm:        Pointer to the VM being initialized
102  *
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
106  * fixed.
107  */
108 int vgic_v4_init(struct kvm *kvm)
109 {
110         struct vgic_dist *dist = &kvm->arch.vgic;
111         struct kvm_vcpu *vcpu;
112         int i, nr_vcpus, ret;
113
114         if (!kvm_vgic_global_state.has_gicv4)
115                 return 0; /* Nothing to see here... move along. */
116
117         if (dist->its_vm.vpes)
118                 return 0;
119
120         nr_vcpus = atomic_read(&kvm->online_vcpus);
121
122         dist->its_vm.vpes = kcalloc(nr_vcpus, sizeof(*dist->its_vm.vpes),
123                                     GFP_KERNEL);
124         if (!dist->its_vm.vpes)
125                 return -ENOMEM;
126
127         dist->its_vm.nr_vpes = nr_vcpus;
128
129         kvm_for_each_vcpu(i, vcpu, kvm)
130                 dist->its_vm.vpes[i] = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
131
132         ret = its_alloc_vcpu_irqs(&dist->its_vm);
133         if (ret < 0) {
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;
138                 return ret;
139         }
140
141         kvm_for_each_vcpu(i, vcpu, kvm) {
142                 int irq = dist->its_vm.vpes[i]->irq;
143
144                 /*
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
149                  * early...
150                  */
151                 irq_set_status_flags(irq, DB_IRQ_FLAGS);
152                 ret = request_irq(irq, vgic_v4_doorbell_handler,
153                                   0, "vcpu", vcpu);
154                 if (ret) {
155                         kvm_err("failed to allocate vcpu IRQ%d\n", irq);
156                         /*
157                          * Trick: adjust the number of vpes so we know
158                          * how many to nuke on teardown...
159                          */
160                         dist->its_vm.nr_vpes = i;
161                         break;
162                 }
163         }
164
165         if (ret)
166                 vgic_v4_teardown(kvm);
167
168         return ret;
169 }
170
171 /**
172  * vgic_v4_teardown - Free the GICv4 data structures
173  * @kvm:        Pointer to the VM being destroyed
174  *
175  * Relies on kvm->lock to be held.
176  */
177 void vgic_v4_teardown(struct kvm *kvm)
178 {
179         struct its_vm *its_vm = &kvm->arch.vgic.its_vm;
180         int i;
181
182         if (!its_vm->vpes)
183                 return;
184
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;
188
189                 irq_clear_status_flags(irq, DB_IRQ_FLAGS);
190                 free_irq(irq, vcpu);
191         }
192
193         its_free_vcpu_irqs(its_vm);
194         kfree(its_vm->vpes);
195         its_vm->nr_vpes = 0;
196         its_vm->vpes = NULL;
197 }
198
199 int vgic_v4_put(struct kvm_vcpu *vcpu, bool need_db)
200 {
201         struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
202         struct irq_desc *desc = irq_to_desc(vpe->irq);
203
204         if (!vgic_supports_direct_msis(vcpu->kvm) || !vpe->resident)
205                 return 0;
206
207         /*
208          * If blocking, a doorbell is required. Undo the nested
209          * disable_irq() calls...
210          */
211         while (need_db && irqd_irq_disabled(&desc->irq_data))
212                 enable_irq(vpe->irq);
213
214         return its_schedule_vpe(vpe, false);
215 }
216
217 int vgic_v4_load(struct kvm_vcpu *vcpu)
218 {
219         struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
220         int err;
221
222         if (!vgic_supports_direct_msis(vcpu->kvm) || vpe->resident)
223                 return 0;
224
225         /*
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.
230          */
231         err = irq_set_affinity(vpe->irq, cpumask_of(smp_processor_id()));
232         if (err)
233                 return err;
234
235         /* Disabled the doorbell, as we're about to enter the guest */
236         disable_irq_nosync(vpe->irq);
237
238         err = its_schedule_vpe(vpe, true);
239         if (err)
240                 return err;
241
242         /*
243          * Now that the VPE is resident, let's get rid of a potential
244          * doorbell interrupt that would still be pending.
245          */
246         return irq_set_irqchip_state(vpe->irq, IRQCHIP_STATE_PENDING, false);
247 }
248
249 static struct vgic_its *vgic_get_its(struct kvm *kvm,
250                                      struct kvm_kernel_irq_routing_entry *irq_entry)
251 {
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,
258         };
259
260         return vgic_msi_to_its(kvm, &msi);
261 }
262
263 int kvm_vgic_v4_set_forwarding(struct kvm *kvm, int virq,
264                                struct kvm_kernel_irq_routing_entry *irq_entry)
265 {
266         struct vgic_its *its;
267         struct vgic_irq *irq;
268         struct its_vlpi_map map;
269         int ret;
270
271         if (!vgic_supports_direct_msis(kvm))
272                 return 0;
273
274         /*
275          * Get the ITS, and escape early on error (not a valid
276          * doorbell for any of our vITSs).
277          */
278         its = vgic_get_its(kvm, irq_entry);
279         if (IS_ERR(its))
280                 return 0;
281
282         mutex_lock(&its->its_lock);
283
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);
287         if (ret)
288                 goto out;
289
290         /*
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
294          * target vcpu.
295          */
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) |
302                                    LPI_PROP_GROUP1),
303                 .db_enabled     = true,
304         };
305
306         ret = its_map_vlpi(virq, &map);
307         if (ret)
308                 goto out;
309
310         irq->hw         = true;
311         irq->host_irq   = virq;
312         atomic_inc(&map.vpe->vlpi_count);
313
314 out:
315         mutex_unlock(&its->its_lock);
316         return ret;
317 }
318
319 int kvm_vgic_v4_unset_forwarding(struct kvm *kvm, int virq,
320                                  struct kvm_kernel_irq_routing_entry *irq_entry)
321 {
322         struct vgic_its *its;
323         struct vgic_irq *irq;
324         int ret;
325
326         if (!vgic_supports_direct_msis(kvm))
327                 return 0;
328
329         /*
330          * Get the ITS, and escape early on error (not a valid
331          * doorbell for any of our vITSs).
332          */
333         its = vgic_get_its(kvm, irq_entry);
334         if (IS_ERR(its))
335                 return 0;
336
337         mutex_lock(&its->its_lock);
338
339         ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
340                                    irq_entry->msi.data, &irq);
341         if (ret)
342                 goto out;
343
344         WARN_ON(!(irq->hw && irq->host_irq == virq));
345         if (irq->hw) {
346                 atomic_dec(&irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count);
347                 irq->hw = false;
348                 ret = its_unmap_vlpi(virq);
349         }
350
351 out:
352         mutex_unlock(&its->its_lock);
353         return ret;
354 }