blob: 87de048fe147268e2c2c6b4a6bbdc02ba930106f [file] [log] [blame]
Eric Auger90977732015-12-01 15:02:35 +01001/*
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
Eric Augerad275b8b2015-12-21 18:09:38 +010025/*
26 * Initialization rules: there are multiple stages to the vgic
Christoffer Dall966e0142017-03-18 13:40:37 +010027 * 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.
Eric Augerad275b8b2015-12-21 18:09:38 +010033 *
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 *
Christoffer Dall966e0142017-03-18 13:40:37 +010047 * - kvm_vgic_vcpu_early_init(): initialization of static data that
Eric Augerad275b8b2015-12-21 18:09:38 +010048 * doesn't depend on any sizing information or emulation type. No
49 * allocation is allowed there.
50 */
51
52/* EARLY INIT */
53
Christoffer Dall966e0142017-03-18 13:40:37 +010054/**
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.
Eric Augerad275b8b2015-12-21 18:09:38 +010061 */
62void kvm_vgic_early_init(struct kvm *kvm)
63{
Christoffer Dall966e0142017-03-18 13:40:37 +010064 struct vgic_dist *dist = &kvm->arch.vgic;
65
66 INIT_LIST_HEAD(&dist->lpi_list_head);
67 spin_lock_init(&dist->lpi_list_lock);
Eric Augerad275b8b2015-12-21 18:09:38 +010068}
69
Christoffer Dall966e0142017-03-18 13:40:37 +010070/**
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 */
Eric Augerad275b8b2015-12-21 18:09:38 +010077void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu)
78{
Christoffer Dall966e0142017-03-18 13:40:37 +010079 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 }
Eric Augerad275b8b2015-12-21 18:09:38 +0100108}
109
Eric Auger5e6431d2015-12-21 14:50:50 +0100110/* 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.
Eric Augerad275b8b2015-12-21 18:09:38 +0100117 * @kvm: kvm struct pointer
118 * @type: KVM_DEV_TYPE_ARM_VGIC_V[23]
Eric Auger5e6431d2015-12-21 14:50:50 +0100119 */
120int kvm_vgic_create(struct kvm *kvm, u32 type)
121{
122 int i, vcpu_lock_idx = -1, ret;
123 struct kvm_vcpu *vcpu;
124
Christoffer Dalla28ebea2016-08-09 19:13:01 +0200125 if (irqchip_in_kernel(kvm))
126 return -EEXIST;
Eric Auger5e6431d2015-12-21 14:50:50 +0100127
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 &&
Christoffer Dalla28ebea2016-08-09 19:13:01 +0200135 !kvm_vgic_global_state.can_emulate_gicv2)
136 return -ENODEV;
Eric Auger5e6431d2015-12-21 14:50:50 +0100137
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
179out_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 }
Eric Auger5e6431d2015-12-21 14:50:50 +0100184 return ret;
185}
186
Eric Augerad275b8b2015-12-21 18:09:38 +0100187/* 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 */
194static 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;
Andre Przywara5dd4b922016-07-15 12:43:27 +0100220 kref_init(&irq->refcount);
Eric Augerad275b8b2015-12-21 18:09:38 +0100221 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/**
Christoffer Dall966e0142017-03-18 13:40:37 +0100230 * kvm_vgic_vcpu_init() - Enable the VCPU interface
231 * @vcpu: the VCPU which's VGIC should be enabled
Eric Augerad275b8b2015-12-21 18:09:38 +0100232 */
233static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
234{
Eric Augerad275b8b2015-12-21 18:09:38 +0100235 if (kvm_vgic_global_state.type == VGIC_V2)
236 vgic_v2_enable(vcpu);
237 else
238 vgic_v3_enable(vcpu);
239}
240
241/*
242 * vgic_init: allocates and initializes dist and vcpu data structures
243 * depending on two dimensioning parameters:
244 * - the number of spis
245 * - the number of vcpus
246 * The function is generally called when nr_spis has been explicitly set
247 * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
248 * vgic_initialized() returns true when this function has succeeded.
249 * Must be called with kvm->lock held!
250 */
251int vgic_init(struct kvm *kvm)
252{
253 struct vgic_dist *dist = &kvm->arch.vgic;
254 struct kvm_vcpu *vcpu;
255 int ret = 0, i;
256
257 if (vgic_initialized(kvm))
258 return 0;
259
260 /* freeze the number of spis */
261 if (!dist->nr_spis)
262 dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
263
264 ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
265 if (ret)
266 goto out;
267
Andre Przywara0e4e82f2016-07-15 12:43:38 +0100268 if (vgic_has_its(kvm))
269 dist->msis_require_devid = true;
270
Eric Augerad275b8b2015-12-21 18:09:38 +0100271 kvm_for_each_vcpu(i, vcpu, kvm)
272 kvm_vgic_vcpu_init(vcpu);
273
Eric Auger180ae7b2016-07-22 16:20:41 +0000274 ret = kvm_vgic_setup_default_irq_routing(kvm);
275 if (ret)
276 goto out;
277
Christoffer Dall10f92c42017-01-17 23:09:13 +0100278 vgic_debug_init(kvm);
279
Eric Augerad275b8b2015-12-21 18:09:38 +0100280 dist->initialized = true;
Christoffer Dall328e5662016-03-24 11:21:04 +0100281
282 /*
283 * If we're initializing GICv2 on-demand when first running the VCPU
284 * then we need to load the VGIC state onto the CPU. We can detect
285 * this easily by checking if we are in between vcpu_load and vcpu_put
286 * when we just initialized the VGIC.
287 */
288 preempt_disable();
289 vcpu = kvm_arm_get_running_vcpu();
290 if (vcpu)
291 kvm_vgic_load(vcpu);
292 preempt_enable();
Eric Augerad275b8b2015-12-21 18:09:38 +0100293out:
294 return ret;
295}
296
297static void kvm_vgic_dist_destroy(struct kvm *kvm)
298{
299 struct vgic_dist *dist = &kvm->arch.vgic;
300
Eric Augerad275b8b2015-12-21 18:09:38 +0100301 dist->ready = false;
302 dist->initialized = false;
303
304 kfree(dist->spis);
Eric Augerad275b8b2015-12-21 18:09:38 +0100305 dist->nr_spis = 0;
Eric Augerad275b8b2015-12-21 18:09:38 +0100306}
307
308void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
309{
310 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
311
312 INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
313}
314
Marc Zyngier1193e6a2017-01-12 09:21:56 +0000315/* To be called with kvm->lock held */
316static void __kvm_vgic_destroy(struct kvm *kvm)
Eric Augerad275b8b2015-12-21 18:09:38 +0100317{
318 struct kvm_vcpu *vcpu;
319 int i;
320
Christoffer Dall10f92c42017-01-17 23:09:13 +0100321 vgic_debug_destroy(kvm);
322
Eric Augerad275b8b2015-12-21 18:09:38 +0100323 kvm_vgic_dist_destroy(kvm);
324
325 kvm_for_each_vcpu(i, vcpu, kvm)
326 kvm_vgic_vcpu_destroy(vcpu);
327}
328
Marc Zyngier1193e6a2017-01-12 09:21:56 +0000329void kvm_vgic_destroy(struct kvm *kvm)
330{
331 mutex_lock(&kvm->lock);
332 __kvm_vgic_destroy(kvm);
333 mutex_unlock(&kvm->lock);
334}
335
Eric Augerad275b8b2015-12-21 18:09:38 +0100336/**
337 * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
338 * is a GICv2. A GICv3 must be explicitly initialized by the guest using the
339 * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
340 * @kvm: kvm struct pointer
341 */
342int vgic_lazy_init(struct kvm *kvm)
343{
344 int ret = 0;
345
346 if (unlikely(!vgic_initialized(kvm))) {
347 /*
348 * We only provide the automatic initialization of the VGIC
349 * for the legacy case of a GICv2. Any other type must
350 * be explicitly initialized once setup with the respective
351 * KVM device call.
352 */
353 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
354 return -EBUSY;
355
356 mutex_lock(&kvm->lock);
357 ret = vgic_init(kvm);
358 mutex_unlock(&kvm->lock);
359 }
360
361 return ret;
362}
363
Eric Augerb0442ee2015-12-21 15:04:42 +0100364/* RESOURCE MAPPING */
365
366/**
367 * Map the MMIO regions depending on the VGIC model exposed to the guest
368 * called on the first VCPU run.
369 * Also map the virtual CPU interface into the VM.
370 * v2/v3 derivatives call vgic_init if not already done.
371 * vgic_ready() returns true if this function has succeeded.
372 * @kvm: kvm struct pointer
373 */
374int kvm_vgic_map_resources(struct kvm *kvm)
375{
376 struct vgic_dist *dist = &kvm->arch.vgic;
377 int ret = 0;
378
379 mutex_lock(&kvm->lock);
380 if (!irqchip_in_kernel(kvm))
381 goto out;
382
383 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
384 ret = vgic_v2_map_resources(kvm);
385 else
386 ret = vgic_v3_map_resources(kvm);
Marc Zyngier1193e6a2017-01-12 09:21:56 +0000387
388 if (ret)
389 __kvm_vgic_destroy(kvm);
390
Eric Augerb0442ee2015-12-21 15:04:42 +0100391out:
392 mutex_unlock(&kvm->lock);
393 return ret;
394}
395
Eric Auger90977732015-12-01 15:02:35 +0100396/* GENERIC PROBE */
397
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000398static int vgic_init_cpu_starting(unsigned int cpu)
Eric Auger90977732015-12-01 15:02:35 +0100399{
400 enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000401 return 0;
Eric Auger90977732015-12-01 15:02:35 +0100402}
403
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000404
405static int vgic_init_cpu_dying(unsigned int cpu)
Eric Auger90977732015-12-01 15:02:35 +0100406{
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000407 disable_percpu_irq(kvm_vgic_global_state.maint_irq);
408 return 0;
Eric Auger90977732015-12-01 15:02:35 +0100409}
410
Eric Auger90977732015-12-01 15:02:35 +0100411static irqreturn_t vgic_maintenance_handler(int irq, void *data)
412{
413 /*
414 * We cannot rely on the vgic maintenance interrupt to be
415 * delivered synchronously. This means we can only use it to
416 * exit the VM, and we perform the handling of EOIed
417 * interrupts on the exit path (see vgic_process_maintenance).
418 */
419 return IRQ_HANDLED;
420}
421
422/**
423 * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
424 * according to the host GIC model. Accordingly calls either
425 * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
426 * instantiated by a guest later on .
427 */
428int kvm_vgic_hyp_init(void)
429{
430 const struct gic_kvm_info *gic_kvm_info;
431 int ret;
432
433 gic_kvm_info = gic_get_kvm_info();
434 if (!gic_kvm_info)
435 return -ENODEV;
436
437 if (!gic_kvm_info->maint_irq) {
438 kvm_err("No vgic maintenance irq\n");
439 return -ENXIO;
440 }
441
442 switch (gic_kvm_info->type) {
443 case GIC_V2:
444 ret = vgic_v2_probe(gic_kvm_info);
445 break;
446 case GIC_V3:
447 ret = vgic_v3_probe(gic_kvm_info);
Vladimir Murzin5a7a8422016-09-12 15:49:15 +0100448 if (!ret) {
449 static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
450 kvm_info("GIC system register CPU interface enabled\n");
451 }
Eric Auger90977732015-12-01 15:02:35 +0100452 break;
453 default:
454 ret = -ENODEV;
455 };
456
457 if (ret)
458 return ret;
459
460 kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
461 ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
462 vgic_maintenance_handler,
463 "vgic", kvm_get_running_vcpus());
464 if (ret) {
465 kvm_err("Cannot register interrupt %d\n",
466 kvm_vgic_global_state.maint_irq);
467 return ret;
468 }
469
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000470 ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100471 "kvm/arm/vgic:starting",
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000472 vgic_init_cpu_starting, vgic_init_cpu_dying);
Eric Auger90977732015-12-01 15:02:35 +0100473 if (ret) {
474 kvm_err("Cannot register vgic CPU notifier\n");
475 goto out_free_irq;
476 }
477
Eric Auger90977732015-12-01 15:02:35 +0100478 kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
479 return 0;
480
481out_free_irq:
482 free_percpu_irq(kvm_vgic_global_state.maint_irq,
483 kvm_get_running_vcpus());
484 return ret;
485}