KVM: arm/arm64: Check vcpu redist base before registering an iodev
[linux/fpc-iii.git] / virt / kvm / arm / vgic / vgic-init.c
blob2673efce65f34ac95511f555377981e78119f248
1 /*
2 * Copyright (C) 2015, 2016 ARM Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <linux/uaccess.h>
18 #include <linux/interrupt.h>
19 #include <linux/cpu.h>
20 #include <linux/kvm_host.h>
21 #include <kvm/arm_vgic.h>
22 #include <asm/kvm_mmu.h>
23 #include "vgic.h"
26 * Initialization rules: there are multiple stages to the vgic
27 * initialization, both for the distributor and the CPU interfaces. The basic
28 * idea is that even though the VGIC is not functional or not requested from
29 * user space, the critical path of the run loop can still call VGIC functions
30 * that just won't do anything, without them having to check additional
31 * initialization flags to ensure they don't look at uninitialized data
32 * structures.
34 * Distributor:
36 * - kvm_vgic_early_init(): initialization of static data that doesn't
37 * depend on any sizing information or emulation type. No allocation
38 * is allowed there.
40 * - vgic_init(): allocation and initialization of the generic data
41 * structures that depend on sizing information (number of CPUs,
42 * number of interrupts). Also initializes the vcpu specific data
43 * structures. Can be executed lazily for GICv2.
45 * CPU Interface:
47 * - kvm_vgic_vcpu_init(): initialization of static data that
48 * doesn't depend on any sizing information or emulation type. No
49 * allocation is allowed there.
52 /* EARLY INIT */
54 /**
55 * kvm_vgic_early_init() - Initialize static VGIC VCPU data structures
56 * @kvm: The VM whose VGIC districutor should be initialized
58 * Only do initialization of static structures that don't require any
59 * allocation or sizing information from userspace. vgic_init() called
60 * kvm_vgic_dist_init() which takes care of the rest.
62 void kvm_vgic_early_init(struct kvm *kvm)
64 struct vgic_dist *dist = &kvm->arch.vgic;
66 INIT_LIST_HEAD(&dist->lpi_list_head);
67 spin_lock_init(&dist->lpi_list_lock);
70 /* CREATION */
72 /**
73 * kvm_vgic_create: triggered by the instantiation of the VGIC device by
74 * user space, either through the legacy KVM_CREATE_IRQCHIP ioctl (v2 only)
75 * or through the generic KVM_CREATE_DEVICE API ioctl.
76 * irqchip_in_kernel() tells you if this function succeeded or not.
77 * @kvm: kvm struct pointer
78 * @type: KVM_DEV_TYPE_ARM_VGIC_V[23]
80 int kvm_vgic_create(struct kvm *kvm, u32 type)
82 int i, vcpu_lock_idx = -1, ret;
83 struct kvm_vcpu *vcpu;
85 if (irqchip_in_kernel(kvm))
86 return -EEXIST;
89 * This function is also called by the KVM_CREATE_IRQCHIP handler,
90 * which had no chance yet to check the availability of the GICv2
91 * emulation. So check this here again. KVM_CREATE_DEVICE does
92 * the proper checks already.
94 if (type == KVM_DEV_TYPE_ARM_VGIC_V2 &&
95 !kvm_vgic_global_state.can_emulate_gicv2)
96 return -ENODEV;
99 * Any time a vcpu is run, vcpu_load is called which tries to grab the
100 * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
101 * that no other VCPUs are run while we create the vgic.
103 ret = -EBUSY;
104 kvm_for_each_vcpu(i, vcpu, kvm) {
105 if (!mutex_trylock(&vcpu->mutex))
106 goto out_unlock;
107 vcpu_lock_idx = i;
110 kvm_for_each_vcpu(i, vcpu, kvm) {
111 if (vcpu->arch.has_run_once)
112 goto out_unlock;
114 ret = 0;
116 if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
117 kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS;
118 else
119 kvm->arch.max_vcpus = VGIC_V3_MAX_CPUS;
121 if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) {
122 ret = -E2BIG;
123 goto out_unlock;
126 kvm->arch.vgic.in_kernel = true;
127 kvm->arch.vgic.vgic_model = type;
129 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
131 if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
132 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
133 else
134 INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
136 out_unlock:
137 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
138 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
139 mutex_unlock(&vcpu->mutex);
141 return ret;
144 /* INIT/DESTROY */
147 * kvm_vgic_dist_init: initialize the dist data structures
148 * @kvm: kvm struct pointer
149 * @nr_spis: number of spis, frozen by caller
151 static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis)
153 struct vgic_dist *dist = &kvm->arch.vgic;
154 struct kvm_vcpu *vcpu0 = kvm_get_vcpu(kvm, 0);
155 int i;
157 dist->spis = kcalloc(nr_spis, sizeof(struct vgic_irq), GFP_KERNEL);
158 if (!dist->spis)
159 return -ENOMEM;
162 * In the following code we do not take the irq struct lock since
163 * no other action on irq structs can happen while the VGIC is
164 * not initialized yet:
165 * If someone wants to inject an interrupt or does a MMIO access, we
166 * require prior initialization in case of a virtual GICv3 or trigger
167 * initialization when using a virtual GICv2.
169 for (i = 0; i < nr_spis; i++) {
170 struct vgic_irq *irq = &dist->spis[i];
172 irq->intid = i + VGIC_NR_PRIVATE_IRQS;
173 INIT_LIST_HEAD(&irq->ap_list);
174 spin_lock_init(&irq->irq_lock);
175 irq->vcpu = NULL;
176 irq->target_vcpu = vcpu0;
177 kref_init(&irq->refcount);
178 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
179 irq->targets = 0;
180 else
181 irq->mpidr = 0;
183 return 0;
187 * kvm_vgic_vcpu_init() - Initialize static VGIC VCPU data
188 * structures and register VCPU-specific KVM iodevs
190 * @vcpu: pointer to the VCPU being created and initialized
192 * Only do initialization, but do not actually enable the
193 * VGIC CPU interface
195 int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
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;
202 vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF;
203 vgic_cpu->sgi_iodev.base_addr = VGIC_ADDR_UNDEF;
205 INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
206 spin_lock_init(&vgic_cpu->ap_list_lock);
209 * Enable and configure all SGIs to be edge-triggered and
210 * configure all PPIs as level-triggered.
212 for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
213 struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
215 INIT_LIST_HEAD(&irq->ap_list);
216 spin_lock_init(&irq->irq_lock);
217 irq->intid = i;
218 irq->vcpu = NULL;
219 irq->target_vcpu = vcpu;
220 irq->targets = 1U << vcpu->vcpu_id;
221 kref_init(&irq->refcount);
222 if (vgic_irq_is_sgi(i)) {
223 /* SGIs */
224 irq->enabled = 1;
225 irq->config = VGIC_CONFIG_EDGE;
226 } else {
227 /* PPIs */
228 irq->config = VGIC_CONFIG_LEVEL;
232 if (!irqchip_in_kernel(vcpu->kvm))
233 return 0;
236 * If we are creating a VCPU with a GICv3 we must also register the
237 * KVM io device for the redistributor that belongs to this VCPU.
239 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
240 mutex_lock(&vcpu->kvm->lock);
241 ret = vgic_register_redist_iodev(vcpu);
242 mutex_unlock(&vcpu->kvm->lock);
244 return ret;
247 static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu)
249 if (kvm_vgic_global_state.type == VGIC_V2)
250 vgic_v2_enable(vcpu);
251 else
252 vgic_v3_enable(vcpu);
256 * vgic_init: allocates and initializes dist and vcpu data structures
257 * depending on two dimensioning parameters:
258 * - the number of spis
259 * - the number of vcpus
260 * The function is generally called when nr_spis has been explicitly set
261 * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
262 * vgic_initialized() returns true when this function has succeeded.
263 * Must be called with kvm->lock held!
265 int vgic_init(struct kvm *kvm)
267 struct vgic_dist *dist = &kvm->arch.vgic;
268 struct kvm_vcpu *vcpu;
269 int ret = 0, i;
271 if (vgic_initialized(kvm))
272 return 0;
274 /* freeze the number of spis */
275 if (!dist->nr_spis)
276 dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
278 ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
279 if (ret)
280 goto out;
282 if (vgic_has_its(kvm)) {
283 ret = vgic_v4_init(kvm);
284 if (ret)
285 goto out;
288 kvm_for_each_vcpu(i, vcpu, kvm)
289 kvm_vgic_vcpu_enable(vcpu);
291 ret = kvm_vgic_setup_default_irq_routing(kvm);
292 if (ret)
293 goto out;
295 vgic_debug_init(kvm);
297 dist->initialized = true;
299 out:
300 return ret;
303 static void kvm_vgic_dist_destroy(struct kvm *kvm)
305 struct vgic_dist *dist = &kvm->arch.vgic;
306 struct vgic_redist_region *rdreg, *next;
308 dist->ready = false;
309 dist->initialized = false;
311 kfree(dist->spis);
312 dist->spis = NULL;
313 dist->nr_spis = 0;
315 if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
316 list_for_each_entry_safe(rdreg, next, &dist->rd_regions, list) {
317 list_del(&rdreg->list);
318 kfree(rdreg);
320 INIT_LIST_HEAD(&dist->rd_regions);
323 if (vgic_supports_direct_msis(kvm))
324 vgic_v4_teardown(kvm);
327 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
329 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
331 INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
334 /* To be called with kvm->lock held */
335 static void __kvm_vgic_destroy(struct kvm *kvm)
337 struct kvm_vcpu *vcpu;
338 int i;
340 vgic_debug_destroy(kvm);
342 kvm_vgic_dist_destroy(kvm);
344 kvm_for_each_vcpu(i, vcpu, kvm)
345 kvm_vgic_vcpu_destroy(vcpu);
348 void kvm_vgic_destroy(struct kvm *kvm)
350 mutex_lock(&kvm->lock);
351 __kvm_vgic_destroy(kvm);
352 mutex_unlock(&kvm->lock);
356 * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
357 * is a GICv2. A GICv3 must be explicitly initialized by the guest using the
358 * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
359 * @kvm: kvm struct pointer
361 int vgic_lazy_init(struct kvm *kvm)
363 int ret = 0;
365 if (unlikely(!vgic_initialized(kvm))) {
367 * We only provide the automatic initialization of the VGIC
368 * for the legacy case of a GICv2. Any other type must
369 * be explicitly initialized once setup with the respective
370 * KVM device call.
372 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
373 return -EBUSY;
375 mutex_lock(&kvm->lock);
376 ret = vgic_init(kvm);
377 mutex_unlock(&kvm->lock);
380 return ret;
383 /* RESOURCE MAPPING */
386 * Map the MMIO regions depending on the VGIC model exposed to the guest
387 * called on the first VCPU run.
388 * Also map the virtual CPU interface into the VM.
389 * v2/v3 derivatives call vgic_init if not already done.
390 * vgic_ready() returns true if this function has succeeded.
391 * @kvm: kvm struct pointer
393 int kvm_vgic_map_resources(struct kvm *kvm)
395 struct vgic_dist *dist = &kvm->arch.vgic;
396 int ret = 0;
398 mutex_lock(&kvm->lock);
399 if (!irqchip_in_kernel(kvm))
400 goto out;
402 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
403 ret = vgic_v2_map_resources(kvm);
404 else
405 ret = vgic_v3_map_resources(kvm);
407 if (ret)
408 __kvm_vgic_destroy(kvm);
410 out:
411 mutex_unlock(&kvm->lock);
412 return ret;
415 /* GENERIC PROBE */
417 static int vgic_init_cpu_starting(unsigned int cpu)
419 enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
420 return 0;
424 static int vgic_init_cpu_dying(unsigned int cpu)
426 disable_percpu_irq(kvm_vgic_global_state.maint_irq);
427 return 0;
430 static irqreturn_t vgic_maintenance_handler(int irq, void *data)
433 * We cannot rely on the vgic maintenance interrupt to be
434 * delivered synchronously. This means we can only use it to
435 * exit the VM, and we perform the handling of EOIed
436 * interrupts on the exit path (see vgic_fold_lr_state).
438 return IRQ_HANDLED;
442 * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
444 * For a specific CPU, initialize the GIC VE hardware.
446 void kvm_vgic_init_cpu_hardware(void)
448 BUG_ON(preemptible());
451 * We want to make sure the list registers start out clear so that we
452 * only have the program the used registers.
454 if (kvm_vgic_global_state.type == VGIC_V2)
455 vgic_v2_init_lrs();
456 else
457 kvm_call_hyp(__vgic_v3_init_lrs);
461 * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
462 * according to the host GIC model. Accordingly calls either
463 * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
464 * instantiated by a guest later on .
466 int kvm_vgic_hyp_init(void)
468 const struct gic_kvm_info *gic_kvm_info;
469 int ret;
471 gic_kvm_info = gic_get_kvm_info();
472 if (!gic_kvm_info)
473 return -ENODEV;
475 if (!gic_kvm_info->maint_irq) {
476 kvm_err("No vgic maintenance irq\n");
477 return -ENXIO;
480 switch (gic_kvm_info->type) {
481 case GIC_V2:
482 ret = vgic_v2_probe(gic_kvm_info);
483 break;
484 case GIC_V3:
485 ret = vgic_v3_probe(gic_kvm_info);
486 if (!ret) {
487 static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
488 kvm_info("GIC system register CPU interface enabled\n");
490 break;
491 default:
492 ret = -ENODEV;
495 if (ret)
496 return ret;
498 kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
499 ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
500 vgic_maintenance_handler,
501 "vgic", kvm_get_running_vcpus());
502 if (ret) {
503 kvm_err("Cannot register interrupt %d\n",
504 kvm_vgic_global_state.maint_irq);
505 return ret;
508 ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
509 "kvm/arm/vgic:starting",
510 vgic_init_cpu_starting, vgic_init_cpu_dying);
511 if (ret) {
512 kvm_err("Cannot register vgic CPU notifier\n");
513 goto out_free_irq;
516 kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
517 return 0;
519 out_free_irq:
520 free_percpu_irq(kvm_vgic_global_state.maint_irq,
521 kvm_get_running_vcpus());
522 return ret;