• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015, 2016 ARM Ltd.
4  */
5 
6 #include <linux/uaccess.h>
7 #include <linux/interrupt.h>
8 #include <linux/cpu.h>
9 #include <linux/kvm_host.h>
10 #include <kvm/arm_vgic.h>
11 #include <asm/kvm_emulate.h>
12 #include <asm/kvm_mmu.h>
13 #include "vgic.h"
14 
15 /*
16  * Initialization rules: there are multiple stages to the vgic
17  * initialization, both for the distributor and the CPU interfaces.  The basic
18  * idea is that even though the VGIC is not functional or not requested from
19  * user space, the critical path of the run loop can still call VGIC functions
20  * that just won't do anything, without them having to check additional
21  * initialization flags to ensure they don't look at uninitialized data
22  * structures.
23  *
24  * Distributor:
25  *
26  * - kvm_vgic_early_init(): initialization of static data that doesn't
27  *   depend on any sizing information or emulation type. No allocation
28  *   is allowed there.
29  *
30  * - vgic_init(): allocation and initialization of the generic data
31  *   structures that depend on sizing information (number of CPUs,
32  *   number of interrupts). Also initializes the vcpu specific data
33  *   structures. Can be executed lazily for GICv2.
34  *
35  * CPU Interface:
36  *
37  * - kvm_vgic_vcpu_init(): initialization of static data that
38  *   doesn't depend on any sizing information or emulation type. No
39  *   allocation is allowed there.
40  */
41 
42 /* EARLY INIT */
43 
44 /**
45  * kvm_vgic_early_init() - Initialize static VGIC VCPU data structures
46  * @kvm: The VM whose VGIC districutor should be initialized
47  *
48  * Only do initialization of static structures that don't require any
49  * allocation or sizing information from userspace.  vgic_init() called
50  * kvm_vgic_dist_init() which takes care of the rest.
51  */
kvm_vgic_early_init(struct kvm * kvm)52 void kvm_vgic_early_init(struct kvm *kvm)
53 {
54 	struct vgic_dist *dist = &kvm->arch.vgic;
55 
56 	INIT_LIST_HEAD(&dist->lpi_list_head);
57 	INIT_LIST_HEAD(&dist->lpi_translation_cache);
58 	raw_spin_lock_init(&dist->lpi_list_lock);
59 }
60 
61 /* CREATION */
62 
63 /**
64  * kvm_vgic_create: triggered by the instantiation of the VGIC device by
65  * user space, either through the legacy KVM_CREATE_IRQCHIP ioctl (v2 only)
66  * or through the generic KVM_CREATE_DEVICE API ioctl.
67  * irqchip_in_kernel() tells you if this function succeeded or not.
68  * @kvm: kvm struct pointer
69  * @type: KVM_DEV_TYPE_ARM_VGIC_V[23]
70  */
kvm_vgic_create(struct kvm * kvm,u32 type)71 int kvm_vgic_create(struct kvm *kvm, u32 type)
72 {
73 	int i, vcpu_lock_idx = -1, ret;
74 	struct kvm_vcpu *vcpu;
75 
76 	if (irqchip_in_kernel(kvm))
77 		return -EEXIST;
78 
79 	/*
80 	 * This function is also called by the KVM_CREATE_IRQCHIP handler,
81 	 * which had no chance yet to check the availability of the GICv2
82 	 * emulation. So check this here again. KVM_CREATE_DEVICE does
83 	 * the proper checks already.
84 	 */
85 	if (type == KVM_DEV_TYPE_ARM_VGIC_V2 &&
86 		!kvm_vgic_global_state.can_emulate_gicv2)
87 		return -ENODEV;
88 
89 	/*
90 	 * Any time a vcpu is run, vcpu_load is called which tries to grab the
91 	 * vcpu->mutex.  By grabbing the vcpu->mutex of all VCPUs we ensure
92 	 * that no other VCPUs are run while we create the vgic.
93 	 */
94 	ret = -EBUSY;
95 	kvm_for_each_vcpu(i, vcpu, kvm) {
96 		if (!mutex_trylock(&vcpu->mutex))
97 			goto out_unlock;
98 		vcpu_lock_idx = i;
99 	}
100 
101 	kvm_for_each_vcpu(i, vcpu, kvm) {
102 		if (vcpu->arch.has_run_once)
103 			goto out_unlock;
104 	}
105 	ret = 0;
106 
107 	if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
108 		kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS;
109 	else
110 		kvm->arch.max_vcpus = VGIC_V3_MAX_CPUS;
111 
112 	if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) {
113 		ret = -E2BIG;
114 		goto out_unlock;
115 	}
116 
117 	kvm->arch.vgic.in_kernel = true;
118 	kvm->arch.vgic.vgic_model = type;
119 
120 	kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
121 
122 	if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
123 		kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
124 	else
125 		INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
126 
127 out_unlock:
128 	for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
129 		vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
130 		mutex_unlock(&vcpu->mutex);
131 	}
132 	return ret;
133 }
134 
135 /* INIT/DESTROY */
136 
137 /**
138  * kvm_vgic_dist_init: initialize the dist data structures
139  * @kvm: kvm struct pointer
140  * @nr_spis: number of spis, frozen by caller
141  */
kvm_vgic_dist_init(struct kvm * kvm,unsigned int nr_spis)142 static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis)
143 {
144 	struct vgic_dist *dist = &kvm->arch.vgic;
145 	struct kvm_vcpu *vcpu0 = kvm_get_vcpu(kvm, 0);
146 	int i;
147 
148 	dist->spis = kcalloc(nr_spis, sizeof(struct vgic_irq), GFP_KERNEL);
149 	if (!dist->spis)
150 		return  -ENOMEM;
151 
152 	/*
153 	 * In the following code we do not take the irq struct lock since
154 	 * no other action on irq structs can happen while the VGIC is
155 	 * not initialized yet:
156 	 * If someone wants to inject an interrupt or does a MMIO access, we
157 	 * require prior initialization in case of a virtual GICv3 or trigger
158 	 * initialization when using a virtual GICv2.
159 	 */
160 	for (i = 0; i < nr_spis; i++) {
161 		struct vgic_irq *irq = &dist->spis[i];
162 
163 		irq->intid = i + VGIC_NR_PRIVATE_IRQS;
164 		INIT_LIST_HEAD(&irq->ap_list);
165 		raw_spin_lock_init(&irq->irq_lock);
166 		irq->vcpu = NULL;
167 		irq->target_vcpu = vcpu0;
168 		kref_init(&irq->refcount);
169 		switch (dist->vgic_model) {
170 		case KVM_DEV_TYPE_ARM_VGIC_V2:
171 			irq->targets = 0;
172 			irq->group = 0;
173 			break;
174 		case KVM_DEV_TYPE_ARM_VGIC_V3:
175 			irq->mpidr = 0;
176 			irq->group = 1;
177 			break;
178 		default:
179 			kfree(dist->spis);
180 			return -EINVAL;
181 		}
182 	}
183 	return 0;
184 }
185 
186 /**
187  * kvm_vgic_vcpu_init() - Initialize static VGIC VCPU data
188  * structures and register VCPU-specific KVM iodevs
189  *
190  * @vcpu: pointer to the VCPU being created and initialized
191  *
192  * Only do initialization, but do not actually enable the
193  * VGIC CPU interface
194  */
kvm_vgic_vcpu_init(struct kvm_vcpu * vcpu)195 int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
196 {
197 	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
198 	struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
199 	int ret = 0;
200 	int i;
201 
202 	vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF;
203 
204 	INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
205 	raw_spin_lock_init(&vgic_cpu->ap_list_lock);
206 
207 	/*
208 	 * Enable and configure all SGIs to be edge-triggered and
209 	 * configure all PPIs as level-triggered.
210 	 */
211 	for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
212 		struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
213 
214 		INIT_LIST_HEAD(&irq->ap_list);
215 		raw_spin_lock_init(&irq->irq_lock);
216 		irq->intid = i;
217 		irq->vcpu = NULL;
218 		irq->target_vcpu = vcpu;
219 		kref_init(&irq->refcount);
220 		if (vgic_irq_is_sgi(i)) {
221 			/* SGIs */
222 			irq->enabled = 1;
223 			irq->config = VGIC_CONFIG_EDGE;
224 		} else {
225 			/* PPIs */
226 			irq->config = VGIC_CONFIG_LEVEL;
227 		}
228 	}
229 
230 	if (!irqchip_in_kernel(vcpu->kvm))
231 		return 0;
232 
233 	/*
234 	 * If we are creating a VCPU with a GICv3 we must also register the
235 	 * KVM io device for the redistributor that belongs to this VCPU.
236 	 */
237 	if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
238 		mutex_lock(&vcpu->kvm->lock);
239 		ret = vgic_register_redist_iodev(vcpu);
240 		mutex_unlock(&vcpu->kvm->lock);
241 	}
242 	return ret;
243 }
244 
kvm_vgic_vcpu_enable(struct kvm_vcpu * vcpu)245 static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu)
246 {
247 	if (kvm_vgic_global_state.type == VGIC_V2)
248 		vgic_v2_enable(vcpu);
249 	else
250 		vgic_v3_enable(vcpu);
251 }
252 
253 /*
254  * vgic_init: allocates and initializes dist and vcpu data structures
255  * depending on two dimensioning parameters:
256  * - the number of spis
257  * - the number of vcpus
258  * The function is generally called when nr_spis has been explicitly set
259  * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
260  * vgic_initialized() returns true when this function has succeeded.
261  * Must be called with kvm->lock held!
262  */
vgic_init(struct kvm * kvm)263 int vgic_init(struct kvm *kvm)
264 {
265 	struct vgic_dist *dist = &kvm->arch.vgic;
266 	struct kvm_vcpu *vcpu;
267 	int ret = 0, i, idx;
268 
269 	if (vgic_initialized(kvm))
270 		return 0;
271 
272 	/* Are we also in the middle of creating a VCPU? */
273 	if (kvm->created_vcpus != atomic_read(&kvm->online_vcpus))
274 		return -EBUSY;
275 
276 	/* freeze the number of spis */
277 	if (!dist->nr_spis)
278 		dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
279 
280 	ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
281 	if (ret)
282 		goto out;
283 
284 	/* Initialize groups on CPUs created before the VGIC type was known */
285 	kvm_for_each_vcpu(idx, vcpu, kvm) {
286 		struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
287 
288 		for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
289 			struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
290 			switch (dist->vgic_model) {
291 			case KVM_DEV_TYPE_ARM_VGIC_V3:
292 				irq->group = 1;
293 				irq->mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
294 				break;
295 			case KVM_DEV_TYPE_ARM_VGIC_V2:
296 				irq->group = 0;
297 				irq->targets = 1U << idx;
298 				break;
299 			default:
300 				ret = -EINVAL;
301 				goto out;
302 			}
303 		}
304 	}
305 
306 	if (vgic_has_its(kvm)) {
307 		vgic_lpi_translation_cache_init(kvm);
308 		ret = vgic_v4_init(kvm);
309 		if (ret)
310 			goto out;
311 	}
312 
313 	kvm_for_each_vcpu(i, vcpu, kvm)
314 		kvm_vgic_vcpu_enable(vcpu);
315 
316 	ret = kvm_vgic_setup_default_irq_routing(kvm);
317 	if (ret)
318 		goto out;
319 
320 	vgic_debug_init(kvm);
321 
322 	dist->implementation_rev = 2;
323 	dist->initialized = true;
324 
325 out:
326 	return ret;
327 }
328 
kvm_vgic_dist_destroy(struct kvm * kvm)329 static void kvm_vgic_dist_destroy(struct kvm *kvm)
330 {
331 	struct vgic_dist *dist = &kvm->arch.vgic;
332 	struct vgic_redist_region *rdreg, *next;
333 
334 	dist->ready = false;
335 	dist->initialized = false;
336 
337 	kfree(dist->spis);
338 	dist->spis = NULL;
339 	dist->nr_spis = 0;
340 
341 	if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
342 		list_for_each_entry_safe(rdreg, next, &dist->rd_regions, list) {
343 			list_del(&rdreg->list);
344 			kfree(rdreg);
345 		}
346 		INIT_LIST_HEAD(&dist->rd_regions);
347 	}
348 
349 	if (vgic_has_its(kvm))
350 		vgic_lpi_translation_cache_destroy(kvm);
351 
352 	if (vgic_supports_direct_msis(kvm))
353 		vgic_v4_teardown(kvm);
354 }
355 
kvm_vgic_vcpu_destroy(struct kvm_vcpu * vcpu)356 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
357 {
358 	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
359 
360 	INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
361 }
362 
363 /* To be called with kvm->lock held */
__kvm_vgic_destroy(struct kvm * kvm)364 static void __kvm_vgic_destroy(struct kvm *kvm)
365 {
366 	struct kvm_vcpu *vcpu;
367 	int i;
368 
369 	vgic_debug_destroy(kvm);
370 
371 	kvm_vgic_dist_destroy(kvm);
372 
373 	kvm_for_each_vcpu(i, vcpu, kvm)
374 		kvm_vgic_vcpu_destroy(vcpu);
375 }
376 
kvm_vgic_destroy(struct kvm * kvm)377 void kvm_vgic_destroy(struct kvm *kvm)
378 {
379 	mutex_lock(&kvm->lock);
380 	__kvm_vgic_destroy(kvm);
381 	mutex_unlock(&kvm->lock);
382 }
383 
384 /**
385  * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
386  * is a GICv2. A GICv3 must be explicitly initialized by the guest using the
387  * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
388  * @kvm: kvm struct pointer
389  */
vgic_lazy_init(struct kvm * kvm)390 int vgic_lazy_init(struct kvm *kvm)
391 {
392 	int ret = 0;
393 
394 	if (unlikely(!vgic_initialized(kvm))) {
395 		/*
396 		 * We only provide the automatic initialization of the VGIC
397 		 * for the legacy case of a GICv2. Any other type must
398 		 * be explicitly initialized once setup with the respective
399 		 * KVM device call.
400 		 */
401 		if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
402 			return -EBUSY;
403 
404 		mutex_lock(&kvm->lock);
405 		ret = vgic_init(kvm);
406 		mutex_unlock(&kvm->lock);
407 	}
408 
409 	return ret;
410 }
411 
412 /* RESOURCE MAPPING */
413 
414 /**
415  * Map the MMIO regions depending on the VGIC model exposed to the guest
416  * called on the first VCPU run.
417  * Also map the virtual CPU interface into the VM.
418  * v2/v3 derivatives call vgic_init if not already done.
419  * vgic_ready() returns true if this function has succeeded.
420  * @kvm: kvm struct pointer
421  */
kvm_vgic_map_resources(struct kvm * kvm)422 int kvm_vgic_map_resources(struct kvm *kvm)
423 {
424 	struct vgic_dist *dist = &kvm->arch.vgic;
425 	int ret = 0;
426 
427 	mutex_lock(&kvm->lock);
428 	if (!irqchip_in_kernel(kvm))
429 		goto out;
430 
431 	if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
432 		ret = vgic_v2_map_resources(kvm);
433 	else
434 		ret = vgic_v3_map_resources(kvm);
435 
436 	if (ret)
437 		__kvm_vgic_destroy(kvm);
438 
439 out:
440 	mutex_unlock(&kvm->lock);
441 	return ret;
442 }
443 
444 /* GENERIC PROBE */
445 
vgic_init_cpu_starting(unsigned int cpu)446 static int vgic_init_cpu_starting(unsigned int cpu)
447 {
448 	enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
449 	return 0;
450 }
451 
452 
vgic_init_cpu_dying(unsigned int cpu)453 static int vgic_init_cpu_dying(unsigned int cpu)
454 {
455 	disable_percpu_irq(kvm_vgic_global_state.maint_irq);
456 	return 0;
457 }
458 
vgic_maintenance_handler(int irq,void * data)459 static irqreturn_t vgic_maintenance_handler(int irq, void *data)
460 {
461 	/*
462 	 * We cannot rely on the vgic maintenance interrupt to be
463 	 * delivered synchronously. This means we can only use it to
464 	 * exit the VM, and we perform the handling of EOIed
465 	 * interrupts on the exit path (see vgic_fold_lr_state).
466 	 */
467 	return IRQ_HANDLED;
468 }
469 
470 /**
471  * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
472  *
473  * For a specific CPU, initialize the GIC VE hardware.
474  */
kvm_vgic_init_cpu_hardware(void)475 void kvm_vgic_init_cpu_hardware(void)
476 {
477 	BUG_ON(preemptible());
478 
479 	/*
480 	 * We want to make sure the list registers start out clear so that we
481 	 * only have the program the used registers.
482 	 */
483 	if (kvm_vgic_global_state.type == VGIC_V2)
484 		vgic_v2_init_lrs();
485 	else
486 		kvm_call_hyp(__vgic_v3_init_lrs);
487 }
488 
489 /**
490  * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
491  * according to the host GIC model. Accordingly calls either
492  * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
493  * instantiated by a guest later on .
494  */
kvm_vgic_hyp_init(void)495 int kvm_vgic_hyp_init(void)
496 {
497 	const struct gic_kvm_info *gic_kvm_info;
498 	int ret;
499 
500 	gic_kvm_info = gic_get_kvm_info();
501 	if (!gic_kvm_info)
502 		return -ENODEV;
503 
504 	if (!gic_kvm_info->maint_irq) {
505 		kvm_err("No vgic maintenance irq\n");
506 		return -ENXIO;
507 	}
508 
509 	switch (gic_kvm_info->type) {
510 	case GIC_V2:
511 		ret = vgic_v2_probe(gic_kvm_info);
512 		break;
513 	case GIC_V3:
514 		ret = vgic_v3_probe(gic_kvm_info);
515 		if (!ret) {
516 			static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
517 			kvm_info("GIC system register CPU interface enabled\n");
518 		}
519 		break;
520 	default:
521 		ret = -ENODEV;
522 	}
523 
524 	if (ret)
525 		return ret;
526 
527 	kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
528 	ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
529 				 vgic_maintenance_handler,
530 				 "vgic", kvm_get_running_vcpus());
531 	if (ret) {
532 		kvm_err("Cannot register interrupt %d\n",
533 			kvm_vgic_global_state.maint_irq);
534 		return ret;
535 	}
536 
537 	ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
538 				"kvm/arm/vgic:starting",
539 				vgic_init_cpu_starting, vgic_init_cpu_dying);
540 	if (ret) {
541 		kvm_err("Cannot register vgic CPU notifier\n");
542 		goto out_free_irq;
543 	}
544 
545 	kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
546 	return 0;
547 
548 out_free_irq:
549 	free_percpu_irq(kvm_vgic_global_state.maint_irq,
550 			kvm_get_running_vcpus());
551 	return ret;
552 }
553