blob: c737ea0a310a732cc6f878c57877aa3086e67280 [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
27 * initialization, both for the distributor and the CPU interfaces.
28 *
29 * Distributor:
30 *
31 * - kvm_vgic_early_init(): initialization of static data that doesn't
32 * depend on any sizing information or emulation type. No allocation
33 * is allowed there.
34 *
35 * - vgic_init(): allocation and initialization of the generic data
36 * structures that depend on sizing information (number of CPUs,
37 * number of interrupts). Also initializes the vcpu specific data
38 * structures. Can be executed lazily for GICv2.
39 *
40 * CPU Interface:
41 *
42 * - kvm_vgic_cpu_early_init(): initialization of static data that
43 * doesn't depend on any sizing information or emulation type. No
44 * allocation is allowed there.
45 */
46
47/* EARLY INIT */
48
49/*
50 * Those 2 functions should not be needed anymore but they
51 * still are called from arm.c
52 */
53void kvm_vgic_early_init(struct kvm *kvm)
54{
55}
56
57void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu)
58{
59}
60
Eric Auger5e6431d2015-12-21 14:50:50 +010061/* 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.
Eric Augerad275b8b2015-12-21 18:09:38 +010068 * @kvm: kvm struct pointer
69 * @type: KVM_DEV_TYPE_ARM_VGIC_V[23]
Eric Auger5e6431d2015-12-21 14:50:50 +010070 */
71int kvm_vgic_create(struct kvm *kvm, u32 type)
72{
73 int i, vcpu_lock_idx = -1, ret;
74 struct kvm_vcpu *vcpu;
75
Christoffer Dalla28ebea2016-08-09 19:13:01 +020076 if (irqchip_in_kernel(kvm))
77 return -EEXIST;
Eric Auger5e6431d2015-12-21 14:50:50 +010078
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 &&
Christoffer Dalla28ebea2016-08-09 19:13:01 +020086 !kvm_vgic_global_state.can_emulate_gicv2)
87 return -ENODEV;
Eric Auger5e6431d2015-12-21 14:50:50 +010088
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 /*
121 * kvm_vgic_global_state.vctrl_base is set on vgic probe (kvm_arch_init)
122 * it is stored in distributor struct for asm save/restore purpose
123 */
124 kvm->arch.vgic.vctrl_base = kvm_vgic_global_state.vctrl_base;
125
126 kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
127 kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
128 kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF;
129
130out_unlock:
131 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
132 vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
133 mutex_unlock(&vcpu->mutex);
134 }
Eric Auger5e6431d2015-12-21 14:50:50 +0100135 return ret;
136}
137
Eric Augerad275b8b2015-12-21 18:09:38 +0100138/* INIT/DESTROY */
139
140/**
141 * kvm_vgic_dist_init: initialize the dist data structures
142 * @kvm: kvm struct pointer
143 * @nr_spis: number of spis, frozen by caller
144 */
145static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis)
146{
147 struct vgic_dist *dist = &kvm->arch.vgic;
148 struct kvm_vcpu *vcpu0 = kvm_get_vcpu(kvm, 0);
149 int i;
150
Andre Przywara38024112016-07-15 12:43:33 +0100151 INIT_LIST_HEAD(&dist->lpi_list_head);
152 spin_lock_init(&dist->lpi_list_lock);
153
Eric Augerad275b8b2015-12-21 18:09:38 +0100154 dist->spis = kcalloc(nr_spis, sizeof(struct vgic_irq), GFP_KERNEL);
155 if (!dist->spis)
156 return -ENOMEM;
157
158 /*
159 * In the following code we do not take the irq struct lock since
160 * no other action on irq structs can happen while the VGIC is
161 * not initialized yet:
162 * If someone wants to inject an interrupt or does a MMIO access, we
163 * require prior initialization in case of a virtual GICv3 or trigger
164 * initialization when using a virtual GICv2.
165 */
166 for (i = 0; i < nr_spis; i++) {
167 struct vgic_irq *irq = &dist->spis[i];
168
169 irq->intid = i + VGIC_NR_PRIVATE_IRQS;
170 INIT_LIST_HEAD(&irq->ap_list);
171 spin_lock_init(&irq->irq_lock);
172 irq->vcpu = NULL;
173 irq->target_vcpu = vcpu0;
Andre Przywara5dd4b922016-07-15 12:43:27 +0100174 kref_init(&irq->refcount);
Eric Augerad275b8b2015-12-21 18:09:38 +0100175 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
176 irq->targets = 0;
177 else
178 irq->mpidr = 0;
179 }
180 return 0;
181}
182
183/**
184 * kvm_vgic_vcpu_init: initialize the vcpu data structures and
185 * enable the VCPU interface
186 * @vcpu: the VCPU which's VGIC should be initialized
187 */
188static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
189{
190 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
191 int i;
192
193 INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
194 spin_lock_init(&vgic_cpu->ap_list_lock);
195
196 /*
197 * Enable and configure all SGIs to be edge-triggered and
198 * configure all PPIs as level-triggered.
199 */
200 for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
201 struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
202
203 INIT_LIST_HEAD(&irq->ap_list);
204 spin_lock_init(&irq->irq_lock);
205 irq->intid = i;
206 irq->vcpu = NULL;
207 irq->target_vcpu = vcpu;
208 irq->targets = 1U << vcpu->vcpu_id;
Andre Przywara5dd4b922016-07-15 12:43:27 +0100209 kref_init(&irq->refcount);
Eric Augerad275b8b2015-12-21 18:09:38 +0100210 if (vgic_irq_is_sgi(i)) {
211 /* SGIs */
212 irq->enabled = 1;
213 irq->config = VGIC_CONFIG_EDGE;
214 } else {
215 /* PPIs */
216 irq->config = VGIC_CONFIG_LEVEL;
217 }
218 }
219 if (kvm_vgic_global_state.type == VGIC_V2)
220 vgic_v2_enable(vcpu);
221 else
222 vgic_v3_enable(vcpu);
223}
224
225/*
226 * vgic_init: allocates and initializes dist and vcpu data structures
227 * depending on two dimensioning parameters:
228 * - the number of spis
229 * - the number of vcpus
230 * The function is generally called when nr_spis has been explicitly set
231 * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
232 * vgic_initialized() returns true when this function has succeeded.
233 * Must be called with kvm->lock held!
234 */
235int vgic_init(struct kvm *kvm)
236{
237 struct vgic_dist *dist = &kvm->arch.vgic;
238 struct kvm_vcpu *vcpu;
239 int ret = 0, i;
240
241 if (vgic_initialized(kvm))
242 return 0;
243
244 /* freeze the number of spis */
245 if (!dist->nr_spis)
246 dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
247
248 ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
249 if (ret)
250 goto out;
251
Andre Przywara0e4e82f2016-07-15 12:43:38 +0100252 if (vgic_has_its(kvm))
253 dist->msis_require_devid = true;
254
Eric Augerad275b8b2015-12-21 18:09:38 +0100255 kvm_for_each_vcpu(i, vcpu, kvm)
256 kvm_vgic_vcpu_init(vcpu);
257
Eric Auger180ae7b2016-07-22 16:20:41 +0000258 ret = kvm_vgic_setup_default_irq_routing(kvm);
259 if (ret)
260 goto out;
261
Eric Augerad275b8b2015-12-21 18:09:38 +0100262 dist->initialized = true;
263out:
264 return ret;
265}
266
267static void kvm_vgic_dist_destroy(struct kvm *kvm)
268{
269 struct vgic_dist *dist = &kvm->arch.vgic;
270
Eric Augerad275b8b2015-12-21 18:09:38 +0100271 dist->ready = false;
272 dist->initialized = false;
273
274 kfree(dist->spis);
Eric Augerad275b8b2015-12-21 18:09:38 +0100275 dist->nr_spis = 0;
Eric Augerad275b8b2015-12-21 18:09:38 +0100276}
277
278void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
279{
280 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
281
282 INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
283}
284
Marc Zyngier1193e6a2017-01-12 09:21:56 +0000285/* To be called with kvm->lock held */
286static void __kvm_vgic_destroy(struct kvm *kvm)
Eric Augerad275b8b2015-12-21 18:09:38 +0100287{
288 struct kvm_vcpu *vcpu;
289 int i;
290
291 kvm_vgic_dist_destroy(kvm);
292
293 kvm_for_each_vcpu(i, vcpu, kvm)
294 kvm_vgic_vcpu_destroy(vcpu);
295}
296
Marc Zyngier1193e6a2017-01-12 09:21:56 +0000297void kvm_vgic_destroy(struct kvm *kvm)
298{
299 mutex_lock(&kvm->lock);
300 __kvm_vgic_destroy(kvm);
301 mutex_unlock(&kvm->lock);
302}
303
Eric Augerad275b8b2015-12-21 18:09:38 +0100304/**
305 * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
306 * is a GICv2. A GICv3 must be explicitly initialized by the guest using the
307 * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
308 * @kvm: kvm struct pointer
309 */
310int vgic_lazy_init(struct kvm *kvm)
311{
312 int ret = 0;
313
314 if (unlikely(!vgic_initialized(kvm))) {
315 /*
316 * We only provide the automatic initialization of the VGIC
317 * for the legacy case of a GICv2. Any other type must
318 * be explicitly initialized once setup with the respective
319 * KVM device call.
320 */
321 if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
322 return -EBUSY;
323
324 mutex_lock(&kvm->lock);
325 ret = vgic_init(kvm);
326 mutex_unlock(&kvm->lock);
327 }
328
329 return ret;
330}
331
Eric Augerb0442ee2015-12-21 15:04:42 +0100332/* RESOURCE MAPPING */
333
334/**
335 * Map the MMIO regions depending on the VGIC model exposed to the guest
336 * called on the first VCPU run.
337 * Also map the virtual CPU interface into the VM.
338 * v2/v3 derivatives call vgic_init if not already done.
339 * vgic_ready() returns true if this function has succeeded.
340 * @kvm: kvm struct pointer
341 */
342int kvm_vgic_map_resources(struct kvm *kvm)
343{
344 struct vgic_dist *dist = &kvm->arch.vgic;
345 int ret = 0;
346
347 mutex_lock(&kvm->lock);
348 if (!irqchip_in_kernel(kvm))
349 goto out;
350
351 if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
352 ret = vgic_v2_map_resources(kvm);
353 else
354 ret = vgic_v3_map_resources(kvm);
Marc Zyngier1193e6a2017-01-12 09:21:56 +0000355
356 if (ret)
357 __kvm_vgic_destroy(kvm);
358
Eric Augerb0442ee2015-12-21 15:04:42 +0100359out:
360 mutex_unlock(&kvm->lock);
361 return ret;
362}
363
Eric Auger90977732015-12-01 15:02:35 +0100364/* GENERIC PROBE */
365
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000366static int vgic_init_cpu_starting(unsigned int cpu)
Eric Auger90977732015-12-01 15:02:35 +0100367{
368 enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000369 return 0;
Eric Auger90977732015-12-01 15:02:35 +0100370}
371
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000372
373static int vgic_init_cpu_dying(unsigned int cpu)
Eric Auger90977732015-12-01 15:02:35 +0100374{
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000375 disable_percpu_irq(kvm_vgic_global_state.maint_irq);
376 return 0;
Eric Auger90977732015-12-01 15:02:35 +0100377}
378
Eric Auger90977732015-12-01 15:02:35 +0100379static irqreturn_t vgic_maintenance_handler(int irq, void *data)
380{
381 /*
382 * We cannot rely on the vgic maintenance interrupt to be
383 * delivered synchronously. This means we can only use it to
384 * exit the VM, and we perform the handling of EOIed
385 * interrupts on the exit path (see vgic_process_maintenance).
386 */
387 return IRQ_HANDLED;
388}
389
390/**
391 * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
392 * according to the host GIC model. Accordingly calls either
393 * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
394 * instantiated by a guest later on .
395 */
396int kvm_vgic_hyp_init(void)
397{
398 const struct gic_kvm_info *gic_kvm_info;
399 int ret;
400
401 gic_kvm_info = gic_get_kvm_info();
402 if (!gic_kvm_info)
403 return -ENODEV;
404
405 if (!gic_kvm_info->maint_irq) {
406 kvm_err("No vgic maintenance irq\n");
407 return -ENXIO;
408 }
409
410 switch (gic_kvm_info->type) {
411 case GIC_V2:
412 ret = vgic_v2_probe(gic_kvm_info);
413 break;
414 case GIC_V3:
415 ret = vgic_v3_probe(gic_kvm_info);
Vladimir Murzin5a7a8422016-09-12 15:49:15 +0100416 if (!ret) {
417 static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
418 kvm_info("GIC system register CPU interface enabled\n");
419 }
Eric Auger90977732015-12-01 15:02:35 +0100420 break;
421 default:
422 ret = -ENODEV;
423 };
424
425 if (ret)
426 return ret;
427
428 kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
429 ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
430 vgic_maintenance_handler,
431 "vgic", kvm_get_running_vcpus());
432 if (ret) {
433 kvm_err("Cannot register interrupt %d\n",
434 kvm_vgic_global_state.maint_irq);
435 return ret;
436 }
437
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000438 ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100439 "kvm/arm/vgic:starting",
Anna-Maria Gleixner15d7e3d2016-07-13 17:17:02 +0000440 vgic_init_cpu_starting, vgic_init_cpu_dying);
Eric Auger90977732015-12-01 15:02:35 +0100441 if (ret) {
442 kvm_err("Cannot register vgic CPU notifier\n");
443 goto out_free_irq;
444 }
445
Eric Auger90977732015-12-01 15:02:35 +0100446 kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
447 return 0;
448
449out_free_irq:
450 free_percpu_irq(kvm_vgic_global_state.maint_irq,
451 kvm_get_running_vcpus());
452 return ret;
453}