xref: /openbmc/linux/arch/arm64/kvm/vgic/vgic-v4.c (revision 745b7908)
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_make_vpe_resident().
71  * You must be in a non-preemptible context. On exit, a call to
72  * its_make_vpe_non_resident() tells the redistributor that we're done
73  * with the 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 (!kvm_vgic_global_state.has_gicv4_1 &&
90 	    !irqd_irq_disabled(&irq_to_desc(irq)->irq_data))
91 		disable_irq_nosync(irq);
92 
93 	/*
94 	 * The v4.1 doorbell can fire concurrently with the vPE being
95 	 * made non-resident. Ensure we only update pending_last
96 	 * *after* the non-residency sequence has completed.
97 	 */
98 	raw_spin_lock(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vpe_lock);
99 	vcpu->arch.vgic_cpu.vgic_v3.its_vpe.pending_last = true;
100 	raw_spin_unlock(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vpe_lock);
101 
102 	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
103 	kvm_vcpu_kick(vcpu);
104 
105 	return IRQ_HANDLED;
106 }
107 
108 static void vgic_v4_sync_sgi_config(struct its_vpe *vpe, struct vgic_irq *irq)
109 {
110 	vpe->sgi_config[irq->intid].enabled	= irq->enabled;
111 	vpe->sgi_config[irq->intid].group 	= irq->group;
112 	vpe->sgi_config[irq->intid].priority	= irq->priority;
113 }
114 
115 static void vgic_v4_enable_vsgis(struct kvm_vcpu *vcpu)
116 {
117 	struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
118 	int i;
119 
120 	/*
121 	 * With GICv4.1, every virtual SGI can be directly injected. So
122 	 * let's pretend that they are HW interrupts, tied to a host
123 	 * IRQ. The SGI code will do its magic.
124 	 */
125 	for (i = 0; i < VGIC_NR_SGIS; i++) {
126 		struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, i);
127 		struct irq_desc *desc;
128 		unsigned long flags;
129 		int ret;
130 
131 		raw_spin_lock_irqsave(&irq->irq_lock, flags);
132 
133 		if (irq->hw)
134 			goto unlock;
135 
136 		irq->hw = true;
137 		irq->host_irq = irq_find_mapping(vpe->sgi_domain, i);
138 
139 		/* Transfer the full irq state to the vPE */
140 		vgic_v4_sync_sgi_config(vpe, irq);
141 		desc = irq_to_desc(irq->host_irq);
142 		ret = irq_domain_activate_irq(irq_desc_get_irq_data(desc),
143 					      false);
144 		if (!WARN_ON(ret)) {
145 			/* Transfer pending state */
146 			ret = irq_set_irqchip_state(irq->host_irq,
147 						    IRQCHIP_STATE_PENDING,
148 						    irq->pending_latch);
149 			WARN_ON(ret);
150 			irq->pending_latch = false;
151 		}
152 	unlock:
153 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
154 		vgic_put_irq(vcpu->kvm, irq);
155 	}
156 }
157 
158 static void vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
159 {
160 	int i;
161 
162 	for (i = 0; i < VGIC_NR_SGIS; i++) {
163 		struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, i);
164 		struct irq_desc *desc;
165 		unsigned long flags;
166 		int ret;
167 
168 		raw_spin_lock_irqsave(&irq->irq_lock, flags);
169 
170 		if (!irq->hw)
171 			goto unlock;
172 
173 		irq->hw = false;
174 		ret = irq_get_irqchip_state(irq->host_irq,
175 					    IRQCHIP_STATE_PENDING,
176 					    &irq->pending_latch);
177 		WARN_ON(ret);
178 
179 		desc = irq_to_desc(irq->host_irq);
180 		irq_domain_deactivate_irq(irq_desc_get_irq_data(desc));
181 	unlock:
182 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
183 		vgic_put_irq(vcpu->kvm, irq);
184 	}
185 }
186 
187 /* Must be called with the kvm lock held */
188 void vgic_v4_configure_vsgis(struct kvm *kvm)
189 {
190 	struct vgic_dist *dist = &kvm->arch.vgic;
191 	struct kvm_vcpu *vcpu;
192 	unsigned long i;
193 
194 	kvm_arm_halt_guest(kvm);
195 
196 	kvm_for_each_vcpu(i, vcpu, kvm) {
197 		if (dist->nassgireq)
198 			vgic_v4_enable_vsgis(vcpu);
199 		else
200 			vgic_v4_disable_vsgis(vcpu);
201 	}
202 
203 	kvm_arm_resume_guest(kvm);
204 }
205 
206 /*
207  * Must be called with GICv4.1 and the vPE unmapped, which
208  * indicates the invalidation of any VPT caches associated
209  * with the vPE, thus we can get the VLPI state by peeking
210  * at the VPT.
211  */
212 void vgic_v4_get_vlpi_state(struct vgic_irq *irq, bool *val)
213 {
214 	struct its_vpe *vpe = &irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
215 	int mask = BIT(irq->intid % BITS_PER_BYTE);
216 	void *va;
217 	u8 *ptr;
218 
219 	va = page_address(vpe->vpt_page);
220 	ptr = va + irq->intid / BITS_PER_BYTE;
221 
222 	*val = !!(*ptr & mask);
223 }
224 
225 int vgic_v4_request_vpe_irq(struct kvm_vcpu *vcpu, int irq)
226 {
227 	return request_irq(irq, vgic_v4_doorbell_handler, 0, "vcpu", vcpu);
228 }
229 
230 /**
231  * vgic_v4_init - Initialize the GICv4 data structures
232  * @kvm:	Pointer to the VM being initialized
233  *
234  * We may be called each time a vITS is created, or when the
235  * vgic is initialized. In both cases, the number of vcpus
236  * should now be fixed.
237  */
238 int vgic_v4_init(struct kvm *kvm)
239 {
240 	struct vgic_dist *dist = &kvm->arch.vgic;
241 	struct kvm_vcpu *vcpu;
242 	int nr_vcpus, ret;
243 	unsigned long i;
244 
245 	lockdep_assert_held(&kvm->arch.config_lock);
246 
247 	if (!kvm_vgic_global_state.has_gicv4)
248 		return 0; /* Nothing to see here... move along. */
249 
250 	if (dist->its_vm.vpes)
251 		return 0;
252 
253 	nr_vcpus = atomic_read(&kvm->online_vcpus);
254 
255 	dist->its_vm.vpes = kcalloc(nr_vcpus, sizeof(*dist->its_vm.vpes),
256 				    GFP_KERNEL_ACCOUNT);
257 	if (!dist->its_vm.vpes)
258 		return -ENOMEM;
259 
260 	dist->its_vm.nr_vpes = nr_vcpus;
261 
262 	kvm_for_each_vcpu(i, vcpu, kvm)
263 		dist->its_vm.vpes[i] = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
264 
265 	ret = its_alloc_vcpu_irqs(&dist->its_vm);
266 	if (ret < 0) {
267 		kvm_err("VPE IRQ allocation failure\n");
268 		kfree(dist->its_vm.vpes);
269 		dist->its_vm.nr_vpes = 0;
270 		dist->its_vm.vpes = NULL;
271 		return ret;
272 	}
273 
274 	kvm_for_each_vcpu(i, vcpu, kvm) {
275 		int irq = dist->its_vm.vpes[i]->irq;
276 		unsigned long irq_flags = DB_IRQ_FLAGS;
277 
278 		/*
279 		 * Don't automatically enable the doorbell, as we're
280 		 * flipping it back and forth when the vcpu gets
281 		 * blocked. Also disable the lazy disabling, as the
282 		 * doorbell could kick us out of the guest too
283 		 * early...
284 		 *
285 		 * On GICv4.1, the doorbell is managed in HW and must
286 		 * be left enabled.
287 		 */
288 		if (kvm_vgic_global_state.has_gicv4_1)
289 			irq_flags &= ~IRQ_NOAUTOEN;
290 		irq_set_status_flags(irq, irq_flags);
291 
292 		ret = vgic_v4_request_vpe_irq(vcpu, irq);
293 		if (ret) {
294 			kvm_err("failed to allocate vcpu IRQ%d\n", irq);
295 			/*
296 			 * Trick: adjust the number of vpes so we know
297 			 * how many to nuke on teardown...
298 			 */
299 			dist->its_vm.nr_vpes = i;
300 			break;
301 		}
302 	}
303 
304 	if (ret)
305 		vgic_v4_teardown(kvm);
306 
307 	return ret;
308 }
309 
310 /**
311  * vgic_v4_teardown - Free the GICv4 data structures
312  * @kvm:	Pointer to the VM being destroyed
313  */
314 void vgic_v4_teardown(struct kvm *kvm)
315 {
316 	struct its_vm *its_vm = &kvm->arch.vgic.its_vm;
317 	int i;
318 
319 	lockdep_assert_held(&kvm->arch.config_lock);
320 
321 	if (!its_vm->vpes)
322 		return;
323 
324 	for (i = 0; i < its_vm->nr_vpes; i++) {
325 		struct kvm_vcpu *vcpu = kvm_get_vcpu(kvm, i);
326 		int irq = its_vm->vpes[i]->irq;
327 
328 		irq_clear_status_flags(irq, DB_IRQ_FLAGS);
329 		free_irq(irq, vcpu);
330 	}
331 
332 	its_free_vcpu_irqs(its_vm);
333 	kfree(its_vm->vpes);
334 	its_vm->nr_vpes = 0;
335 	its_vm->vpes = NULL;
336 }
337 
338 int vgic_v4_put(struct kvm_vcpu *vcpu, bool need_db)
339 {
340 	struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
341 
342 	if (!vgic_supports_direct_msis(vcpu->kvm) || !vpe->resident)
343 		return 0;
344 
345 	return its_make_vpe_non_resident(vpe, need_db);
346 }
347 
348 int vgic_v4_load(struct kvm_vcpu *vcpu)
349 {
350 	struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
351 	int err;
352 
353 	if (!vgic_supports_direct_msis(vcpu->kvm) || vpe->resident)
354 		return 0;
355 
356 	/*
357 	 * Before making the VPE resident, make sure the redistributor
358 	 * corresponding to our current CPU expects us here. See the
359 	 * doc in drivers/irqchip/irq-gic-v4.c to understand how this
360 	 * turns into a VMOVP command at the ITS level.
361 	 */
362 	err = irq_set_affinity(vpe->irq, cpumask_of(smp_processor_id()));
363 	if (err)
364 		return err;
365 
366 	err = its_make_vpe_resident(vpe, false, vcpu->kvm->arch.vgic.enabled);
367 	if (err)
368 		return err;
369 
370 	/*
371 	 * Now that the VPE is resident, let's get rid of a potential
372 	 * doorbell interrupt that would still be pending. This is a
373 	 * GICv4.0 only "feature"...
374 	 */
375 	if (!kvm_vgic_global_state.has_gicv4_1)
376 		err = irq_set_irqchip_state(vpe->irq, IRQCHIP_STATE_PENDING, false);
377 
378 	return err;
379 }
380 
381 void vgic_v4_commit(struct kvm_vcpu *vcpu)
382 {
383 	struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
384 
385 	/*
386 	 * No need to wait for the vPE to be ready across a shallow guest
387 	 * exit, as only a vcpu_put will invalidate it.
388 	 */
389 	if (!vpe->ready)
390 		its_commit_vpe(vpe);
391 }
392 
393 static struct vgic_its *vgic_get_its(struct kvm *kvm,
394 				     struct kvm_kernel_irq_routing_entry *irq_entry)
395 {
396 	struct kvm_msi msi  = (struct kvm_msi) {
397 		.address_lo	= irq_entry->msi.address_lo,
398 		.address_hi	= irq_entry->msi.address_hi,
399 		.data		= irq_entry->msi.data,
400 		.flags		= irq_entry->msi.flags,
401 		.devid		= irq_entry->msi.devid,
402 	};
403 
404 	return vgic_msi_to_its(kvm, &msi);
405 }
406 
407 int kvm_vgic_v4_set_forwarding(struct kvm *kvm, int virq,
408 			       struct kvm_kernel_irq_routing_entry *irq_entry)
409 {
410 	struct vgic_its *its;
411 	struct vgic_irq *irq;
412 	struct its_vlpi_map map;
413 	unsigned long flags;
414 	int ret;
415 
416 	if (!vgic_supports_direct_msis(kvm))
417 		return 0;
418 
419 	/*
420 	 * Get the ITS, and escape early on error (not a valid
421 	 * doorbell for any of our vITSs).
422 	 */
423 	its = vgic_get_its(kvm, irq_entry);
424 	if (IS_ERR(its))
425 		return 0;
426 
427 	mutex_lock(&its->its_lock);
428 
429 	/* Perform the actual DevID/EventID -> LPI translation. */
430 	ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
431 				   irq_entry->msi.data, &irq);
432 	if (ret)
433 		goto out;
434 
435 	/*
436 	 * Emit the mapping request. If it fails, the ITS probably
437 	 * isn't v4 compatible, so let's silently bail out. Holding
438 	 * the ITS lock should ensure that nothing can modify the
439 	 * target vcpu.
440 	 */
441 	map = (struct its_vlpi_map) {
442 		.vm		= &kvm->arch.vgic.its_vm,
443 		.vpe		= &irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe,
444 		.vintid		= irq->intid,
445 		.properties	= ((irq->priority & 0xfc) |
446 				   (irq->enabled ? LPI_PROP_ENABLED : 0) |
447 				   LPI_PROP_GROUP1),
448 		.db_enabled	= true,
449 	};
450 
451 	ret = its_map_vlpi(virq, &map);
452 	if (ret)
453 		goto out;
454 
455 	irq->hw		= true;
456 	irq->host_irq	= virq;
457 	atomic_inc(&map.vpe->vlpi_count);
458 
459 	/* Transfer pending state */
460 	raw_spin_lock_irqsave(&irq->irq_lock, flags);
461 	if (irq->pending_latch) {
462 		ret = irq_set_irqchip_state(irq->host_irq,
463 					    IRQCHIP_STATE_PENDING,
464 					    irq->pending_latch);
465 		WARN_RATELIMIT(ret, "IRQ %d", irq->host_irq);
466 
467 		/*
468 		 * Clear pending_latch and communicate this state
469 		 * change via vgic_queue_irq_unlock.
470 		 */
471 		irq->pending_latch = false;
472 		vgic_queue_irq_unlock(kvm, irq, flags);
473 	} else {
474 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
475 	}
476 
477 out:
478 	mutex_unlock(&its->its_lock);
479 	return ret;
480 }
481 
482 int kvm_vgic_v4_unset_forwarding(struct kvm *kvm, int virq,
483 				 struct kvm_kernel_irq_routing_entry *irq_entry)
484 {
485 	struct vgic_its *its;
486 	struct vgic_irq *irq;
487 	int ret;
488 
489 	if (!vgic_supports_direct_msis(kvm))
490 		return 0;
491 
492 	/*
493 	 * Get the ITS, and escape early on error (not a valid
494 	 * doorbell for any of our vITSs).
495 	 */
496 	its = vgic_get_its(kvm, irq_entry);
497 	if (IS_ERR(its))
498 		return 0;
499 
500 	mutex_lock(&its->its_lock);
501 
502 	ret = vgic_its_resolve_lpi(kvm, its, irq_entry->msi.devid,
503 				   irq_entry->msi.data, &irq);
504 	if (ret)
505 		goto out;
506 
507 	WARN_ON(!(irq->hw && irq->host_irq == virq));
508 	if (irq->hw) {
509 		atomic_dec(&irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count);
510 		irq->hw = false;
511 		ret = its_unmap_vlpi(virq);
512 	}
513 
514 out:
515 	mutex_unlock(&its->its_lock);
516 	return ret;
517 }
518