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 dist->spis = NULL;
181 return -EINVAL;
182 }
183 }
184 return 0;
185 }
186
187 /**
188 * kvm_vgic_vcpu_init() - Initialize static VGIC VCPU data
189 * structures and register VCPU-specific KVM iodevs
190 *
191 * @vcpu: pointer to the VCPU being created and initialized
192 *
193 * Only do initialization, but do not actually enable the
194 * VGIC CPU interface
195 */
kvm_vgic_vcpu_init(struct kvm_vcpu * vcpu)196 int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
197 {
198 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
199 struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
200 int ret = 0;
201 int i;
202
203 vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF;
204
205 INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
206 raw_spin_lock_init(&vgic_cpu->ap_list_lock);
207
208 /*
209 * Enable and configure all SGIs to be edge-triggered and
210 * configure all PPIs as level-triggered.
211 */
212 for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
213 struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
214
215 INIT_LIST_HEAD(&irq->ap_list);
216 raw_spin_lock_init(&irq->irq_lock);
217 irq->intid = i;
218 irq->vcpu = NULL;
219 irq->target_vcpu = vcpu;
220 kref_init(&irq->refcount);
221 if (vgic_irq_is_sgi(i)) {
222 /* SGIs */
223 irq->enabled = 1;
224 irq->config = VGIC_CONFIG_EDGE;
225 } else {
226 /* PPIs */
227 irq->config = VGIC_CONFIG_LEVEL;
228 }
229 }
230
231 if (!irqchip_in_kernel(vcpu->kvm))
232 return 0;
233
234 /*
235 * If we are creating a VCPU with a GICv3 we must also register the
236 * KVM io device for the redistributor that belongs to this VCPU.
237 */
238 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
239 mutex_lock(&vcpu->kvm->lock);
240 ret = vgic_register_redist_iodev(vcpu);
241 mutex_unlock(&vcpu->kvm->lock);
242 }
243 return ret;
244 }
245
kvm_vgic_vcpu_enable(struct kvm_vcpu * vcpu)246 static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu)
247 {
248 if (kvm_vgic_global_state.type == VGIC_V2)
249 vgic_v2_enable(vcpu);
250 else
251 vgic_v3_enable(vcpu);
252 }
253
254 /*
255 * vgic_init: allocates and initializes dist and vcpu data structures
256 * depending on two dimensioning parameters:
257 * - the number of spis
258 * - the number of vcpus
259 * The function is generally called when nr_spis has been explicitly set
260 * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
261 * vgic_initialized() returns true when this function has succeeded.
262 * Must be called with kvm->lock held!
263 */
vgic_init(struct kvm * kvm)264 int vgic_init(struct kvm *kvm)
265 {
266 struct vgic_dist *dist = &kvm->arch.vgic;
267 struct kvm_vcpu *vcpu;
268 int ret = 0, i, idx;
269
270 if (vgic_initialized(kvm))
271 return 0;
272
273 /* Are we also in the middle of creating a VCPU? */
274 if (kvm->created_vcpus != atomic_read(&kvm->online_vcpus))
275 return -EBUSY;
276
277 /* freeze the number of spis */
278 if (!dist->nr_spis)
279 dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
280
281 ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
282 if (ret)
283 goto out;
284
285 /* Initialize groups on CPUs created before the VGIC type was known */
286 kvm_for_each_vcpu(idx, vcpu, kvm) {
287 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
288
289 for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
290 struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
291 switch (dist->vgic_model) {
292 case KVM_DEV_TYPE_ARM_VGIC_V3:
293 irq->group = 1;
294 irq->mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
295 break;
296 case KVM_DEV_TYPE_ARM_VGIC_V2:
297 irq->group = 0;
298 irq->targets = 1U << idx;
299 break;
300 default:
301 ret = -EINVAL;
302 goto out;
303 }
304 }
305 }
306
307 if (vgic_has_its(kvm)) {
308 vgic_lpi_translation_cache_init(kvm);
309 ret = vgic_v4_init(kvm);
310 if (ret)
311 goto out;
312 }
313
314 kvm_for_each_vcpu(i, vcpu, kvm)
315 kvm_vgic_vcpu_enable(vcpu);
316
317 ret = kvm_vgic_setup_default_irq_routing(kvm);
318 if (ret)
319 goto out;
320
321 vgic_debug_init(kvm);
322
323 dist->implementation_rev = 2;
324 dist->initialized = true;
325
326 out:
327 return ret;
328 }
329
kvm_vgic_dist_destroy(struct kvm * kvm)330 static void kvm_vgic_dist_destroy(struct kvm *kvm)
331 {
332 struct vgic_dist *dist = &kvm->arch.vgic;
333 struct vgic_redist_region *rdreg, *next;
334
335 dist->ready = false;
336 dist->initialized = false;
337
338 kfree(dist->spis);
339 dist->spis = NULL;
340 dist->nr_spis = 0;
341
342 if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
343 list_for_each_entry_safe(rdreg, next, &dist->rd_regions, list) {
344 list_del(&rdreg->list);
345 kfree(rdreg);
346 }
347 INIT_LIST_HEAD(&dist->rd_regions);
348 }
349
350 if (vgic_has_its(kvm))
351 vgic_lpi_translation_cache_destroy(kvm);
352
353 if (vgic_supports_direct_msis(kvm))
354 vgic_v4_teardown(kvm);
355 }
356
kvm_vgic_vcpu_destroy(struct kvm_vcpu * vcpu)357 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
358 {
359 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
360
361 /*
362 * Retire all pending LPIs on this vcpu anyway as we're
363 * going to destroy it.
364 */
365 vgic_flush_pending_lpis(vcpu);
366
367 INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
368 }
369
370 /* To be called with kvm->lock held */
__kvm_vgic_destroy(struct kvm * kvm)371 static void __kvm_vgic_destroy(struct kvm *kvm)
372 {
373 struct kvm_vcpu *vcpu;
374 int i;
375
376 vgic_debug_destroy(kvm);
377
378 kvm_for_each_vcpu(i, vcpu, kvm)
379 kvm_vgic_vcpu_destroy(vcpu);
380
381 kvm_vgic_dist_destroy(kvm);
382 }
383
kvm_vgic_destroy(struct kvm * kvm)384 void kvm_vgic_destroy(struct kvm *kvm)
385 {
386 mutex_lock(&kvm->lock);
387 __kvm_vgic_destroy(kvm);
388 mutex_unlock(&kvm->lock);
389 }
390
391 /**
392 * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
393 * is a GICv2. A GICv3 must be explicitly initialized by the guest using the
394 * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
395 * @kvm: kvm struct pointer
396 */
vgic_lazy_init(struct kvm * kvm)397 int vgic_lazy_init(struct kvm *kvm)
398 {
399 int ret = 0;
400
401 if (unlikely(!vgic_initialized(kvm))) {
402 /*
403 * We only provide the automatic initialization of the VGIC
404 * for the legacy case of a GICv2. Any other type must
405 * be explicitly initialized once setup with the respective
406 * KVM device call.
407 */
408 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
409 return -EBUSY;
410
411 mutex_lock(&kvm->lock);
412 ret = vgic_init(kvm);
413 mutex_unlock(&kvm->lock);
414 }
415
416 return ret;
417 }
418
419 /* RESOURCE MAPPING */
420
421 /**
422 * Map the MMIO regions depending on the VGIC model exposed to the guest
423 * called on the first VCPU run.
424 * Also map the virtual CPU interface into the VM.
425 * v2/v3 derivatives call vgic_init if not already done.
426 * vgic_ready() returns true if this function has succeeded.
427 * @kvm: kvm struct pointer
428 */
kvm_vgic_map_resources(struct kvm * kvm)429 int kvm_vgic_map_resources(struct kvm *kvm)
430 {
431 struct vgic_dist *dist = &kvm->arch.vgic;
432 int ret = 0;
433
434 mutex_lock(&kvm->lock);
435 if (!irqchip_in_kernel(kvm))
436 goto out;
437
438 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
439 ret = vgic_v2_map_resources(kvm);
440 else
441 ret = vgic_v3_map_resources(kvm);
442
443 if (ret)
444 __kvm_vgic_destroy(kvm);
445
446 out:
447 mutex_unlock(&kvm->lock);
448 return ret;
449 }
450
451 /* GENERIC PROBE */
452
vgic_init_cpu_starting(unsigned int cpu)453 static int vgic_init_cpu_starting(unsigned int cpu)
454 {
455 enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
456 return 0;
457 }
458
459
vgic_init_cpu_dying(unsigned int cpu)460 static int vgic_init_cpu_dying(unsigned int cpu)
461 {
462 disable_percpu_irq(kvm_vgic_global_state.maint_irq);
463 return 0;
464 }
465
vgic_maintenance_handler(int irq,void * data)466 static irqreturn_t vgic_maintenance_handler(int irq, void *data)
467 {
468 /*
469 * We cannot rely on the vgic maintenance interrupt to be
470 * delivered synchronously. This means we can only use it to
471 * exit the VM, and we perform the handling of EOIed
472 * interrupts on the exit path (see vgic_fold_lr_state).
473 */
474 return IRQ_HANDLED;
475 }
476
477 /**
478 * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
479 *
480 * For a specific CPU, initialize the GIC VE hardware.
481 */
kvm_vgic_init_cpu_hardware(void)482 void kvm_vgic_init_cpu_hardware(void)
483 {
484 BUG_ON(preemptible());
485
486 /*
487 * We want to make sure the list registers start out clear so that we
488 * only have the program the used registers.
489 */
490 if (kvm_vgic_global_state.type == VGIC_V2)
491 vgic_v2_init_lrs();
492 else
493 kvm_call_hyp(__vgic_v3_init_lrs);
494 }
495
496 /**
497 * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
498 * according to the host GIC model. Accordingly calls either
499 * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
500 * instantiated by a guest later on .
501 */
kvm_vgic_hyp_init(void)502 int kvm_vgic_hyp_init(void)
503 {
504 const struct gic_kvm_info *gic_kvm_info;
505 int ret;
506
507 gic_kvm_info = gic_get_kvm_info();
508 if (!gic_kvm_info)
509 return -ENODEV;
510
511 if (!gic_kvm_info->maint_irq) {
512 kvm_err("No vgic maintenance irq\n");
513 return -ENXIO;
514 }
515
516 switch (gic_kvm_info->type) {
517 case GIC_V2:
518 ret = vgic_v2_probe(gic_kvm_info);
519 break;
520 case GIC_V3:
521 ret = vgic_v3_probe(gic_kvm_info);
522 if (!ret) {
523 static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
524 kvm_info("GIC system register CPU interface enabled\n");
525 }
526 break;
527 default:
528 ret = -ENODEV;
529 }
530
531 if (ret)
532 return ret;
533
534 kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
535 ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
536 vgic_maintenance_handler,
537 "vgic", kvm_get_running_vcpus());
538 if (ret) {
539 kvm_err("Cannot register interrupt %d\n",
540 kvm_vgic_global_state.maint_irq);
541 return ret;
542 }
543
544 ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
545 "kvm/arm/vgic:starting",
546 vgic_init_cpu_starting, vgic_init_cpu_dying);
547 if (ret) {
548 kvm_err("Cannot register vgic CPU notifier\n");
549 goto out_free_irq;
550 }
551
552 kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
553 return 0;
554
555 out_free_irq:
556 free_percpu_irq(kvm_vgic_global_state.maint_irq,
557 kvm_get_running_vcpus());
558 return ret;
559 }
560