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