]>
Commit | Line | Data |
---|---|---|
90977732 EA |
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 | ||
ad275b8b EA |
25 | /* |
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 | */ | |
53 | void kvm_vgic_early_init(struct kvm *kvm) | |
54 | { | |
55 | } | |
56 | ||
57 | void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu) | |
58 | { | |
59 | } | |
60 | ||
5e6431da EA |
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. | |
ad275b8b EA |
68 | * @kvm: kvm struct pointer |
69 | * @type: KVM_DEV_TYPE_ARM_VGIC_V[23] | |
5e6431da EA |
70 | */ |
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 | mutex_lock(&kvm->lock); | |
77 | ||
78 | if (irqchip_in_kernel(kvm)) { | |
79 | ret = -EEXIST; | |
80 | goto out; | |
81 | } | |
82 | ||
83 | /* | |
84 | * This function is also called by the KVM_CREATE_IRQCHIP handler, | |
85 | * which had no chance yet to check the availability of the GICv2 | |
86 | * emulation. So check this here again. KVM_CREATE_DEVICE does | |
87 | * the proper checks already. | |
88 | */ | |
89 | if (type == KVM_DEV_TYPE_ARM_VGIC_V2 && | |
90 | !kvm_vgic_global_state.can_emulate_gicv2) { | |
91 | ret = -ENODEV; | |
92 | goto out; | |
93 | } | |
94 | ||
95 | /* | |
96 | * Any time a vcpu is run, vcpu_load is called which tries to grab the | |
97 | * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure | |
98 | * that no other VCPUs are run while we create the vgic. | |
99 | */ | |
100 | ret = -EBUSY; | |
101 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
102 | if (!mutex_trylock(&vcpu->mutex)) | |
103 | goto out_unlock; | |
104 | vcpu_lock_idx = i; | |
105 | } | |
106 | ||
107 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
108 | if (vcpu->arch.has_run_once) | |
109 | goto out_unlock; | |
110 | } | |
111 | ret = 0; | |
112 | ||
113 | if (type == KVM_DEV_TYPE_ARM_VGIC_V2) | |
114 | kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS; | |
115 | else | |
116 | kvm->arch.max_vcpus = VGIC_V3_MAX_CPUS; | |
117 | ||
118 | if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) { | |
119 | ret = -E2BIG; | |
120 | goto out_unlock; | |
121 | } | |
122 | ||
123 | kvm->arch.vgic.in_kernel = true; | |
124 | kvm->arch.vgic.vgic_model = type; | |
125 | ||
126 | /* | |
127 | * kvm_vgic_global_state.vctrl_base is set on vgic probe (kvm_arch_init) | |
128 | * it is stored in distributor struct for asm save/restore purpose | |
129 | */ | |
130 | kvm->arch.vgic.vctrl_base = kvm_vgic_global_state.vctrl_base; | |
131 | ||
132 | kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF; | |
133 | kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF; | |
134 | kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF; | |
135 | ||
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); | |
140 | } | |
141 | ||
142 | out: | |
143 | mutex_unlock(&kvm->lock); | |
144 | return ret; | |
145 | } | |
146 | ||
ad275b8b EA |
147 | /* INIT/DESTROY */ |
148 | ||
149 | /** | |
150 | * kvm_vgic_dist_init: initialize the dist data structures | |
151 | * @kvm: kvm struct pointer | |
152 | * @nr_spis: number of spis, frozen by caller | |
153 | */ | |
154 | static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis) | |
155 | { | |
156 | struct vgic_dist *dist = &kvm->arch.vgic; | |
157 | struct kvm_vcpu *vcpu0 = kvm_get_vcpu(kvm, 0); | |
158 | int i; | |
159 | ||
160 | dist->spis = kcalloc(nr_spis, sizeof(struct vgic_irq), GFP_KERNEL); | |
161 | if (!dist->spis) | |
162 | return -ENOMEM; | |
163 | ||
164 | /* | |
165 | * In the following code we do not take the irq struct lock since | |
166 | * no other action on irq structs can happen while the VGIC is | |
167 | * not initialized yet: | |
168 | * If someone wants to inject an interrupt or does a MMIO access, we | |
169 | * require prior initialization in case of a virtual GICv3 or trigger | |
170 | * initialization when using a virtual GICv2. | |
171 | */ | |
172 | for (i = 0; i < nr_spis; i++) { | |
173 | struct vgic_irq *irq = &dist->spis[i]; | |
174 | ||
175 | irq->intid = i + VGIC_NR_PRIVATE_IRQS; | |
176 | INIT_LIST_HEAD(&irq->ap_list); | |
177 | spin_lock_init(&irq->irq_lock); | |
178 | irq->vcpu = NULL; | |
179 | irq->target_vcpu = vcpu0; | |
180 | if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2) | |
181 | irq->targets = 0; | |
182 | else | |
183 | irq->mpidr = 0; | |
184 | } | |
185 | return 0; | |
186 | } | |
187 | ||
188 | /** | |
189 | * kvm_vgic_vcpu_init: initialize the vcpu data structures and | |
190 | * enable the VCPU interface | |
191 | * @vcpu: the VCPU which's VGIC should be initialized | |
192 | */ | |
193 | static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu) | |
194 | { | |
195 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; | |
196 | int i; | |
197 | ||
198 | INIT_LIST_HEAD(&vgic_cpu->ap_list_head); | |
199 | spin_lock_init(&vgic_cpu->ap_list_lock); | |
200 | ||
201 | /* | |
202 | * Enable and configure all SGIs to be edge-triggered and | |
203 | * configure all PPIs as level-triggered. | |
204 | */ | |
205 | for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) { | |
206 | struct vgic_irq *irq = &vgic_cpu->private_irqs[i]; | |
207 | ||
208 | INIT_LIST_HEAD(&irq->ap_list); | |
209 | spin_lock_init(&irq->irq_lock); | |
210 | irq->intid = i; | |
211 | irq->vcpu = NULL; | |
212 | irq->target_vcpu = vcpu; | |
213 | irq->targets = 1U << vcpu->vcpu_id; | |
214 | if (vgic_irq_is_sgi(i)) { | |
215 | /* SGIs */ | |
216 | irq->enabled = 1; | |
217 | irq->config = VGIC_CONFIG_EDGE; | |
218 | } else { | |
219 | /* PPIs */ | |
220 | irq->config = VGIC_CONFIG_LEVEL; | |
221 | } | |
222 | } | |
223 | if (kvm_vgic_global_state.type == VGIC_V2) | |
224 | vgic_v2_enable(vcpu); | |
225 | else | |
226 | vgic_v3_enable(vcpu); | |
227 | } | |
228 | ||
229 | /* | |
230 | * vgic_init: allocates and initializes dist and vcpu data structures | |
231 | * depending on two dimensioning parameters: | |
232 | * - the number of spis | |
233 | * - the number of vcpus | |
234 | * The function is generally called when nr_spis has been explicitly set | |
235 | * by the guest through the KVM DEVICE API. If not nr_spis is set to 256. | |
236 | * vgic_initialized() returns true when this function has succeeded. | |
237 | * Must be called with kvm->lock held! | |
238 | */ | |
239 | int vgic_init(struct kvm *kvm) | |
240 | { | |
241 | struct vgic_dist *dist = &kvm->arch.vgic; | |
242 | struct kvm_vcpu *vcpu; | |
243 | int ret = 0, i; | |
244 | ||
245 | if (vgic_initialized(kvm)) | |
246 | return 0; | |
247 | ||
248 | /* freeze the number of spis */ | |
249 | if (!dist->nr_spis) | |
250 | dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS; | |
251 | ||
252 | ret = kvm_vgic_dist_init(kvm, dist->nr_spis); | |
253 | if (ret) | |
254 | goto out; | |
255 | ||
256 | kvm_for_each_vcpu(i, vcpu, kvm) | |
257 | kvm_vgic_vcpu_init(vcpu); | |
258 | ||
259 | dist->initialized = true; | |
260 | out: | |
261 | return ret; | |
262 | } | |
263 | ||
264 | static void kvm_vgic_dist_destroy(struct kvm *kvm) | |
265 | { | |
266 | struct vgic_dist *dist = &kvm->arch.vgic; | |
267 | ||
268 | mutex_lock(&kvm->lock); | |
269 | ||
270 | dist->ready = false; | |
271 | dist->initialized = false; | |
272 | ||
273 | kfree(dist->spis); | |
274 | kfree(dist->redist_iodevs); | |
275 | dist->nr_spis = 0; | |
276 | ||
277 | mutex_unlock(&kvm->lock); | |
278 | } | |
279 | ||
280 | void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) | |
281 | { | |
282 | struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; | |
283 | ||
284 | INIT_LIST_HEAD(&vgic_cpu->ap_list_head); | |
285 | } | |
286 | ||
287 | void kvm_vgic_destroy(struct kvm *kvm) | |
288 | { | |
289 | struct kvm_vcpu *vcpu; | |
290 | int i; | |
291 | ||
292 | kvm_vgic_dist_destroy(kvm); | |
293 | ||
294 | kvm_for_each_vcpu(i, vcpu, kvm) | |
295 | kvm_vgic_vcpu_destroy(vcpu); | |
296 | } | |
297 | ||
298 | /** | |
299 | * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest | |
300 | * is a GICv2. A GICv3 must be explicitly initialized by the guest using the | |
301 | * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group. | |
302 | * @kvm: kvm struct pointer | |
303 | */ | |
304 | int vgic_lazy_init(struct kvm *kvm) | |
305 | { | |
306 | int ret = 0; | |
307 | ||
308 | if (unlikely(!vgic_initialized(kvm))) { | |
309 | /* | |
310 | * We only provide the automatic initialization of the VGIC | |
311 | * for the legacy case of a GICv2. Any other type must | |
312 | * be explicitly initialized once setup with the respective | |
313 | * KVM device call. | |
314 | */ | |
315 | if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2) | |
316 | return -EBUSY; | |
317 | ||
318 | mutex_lock(&kvm->lock); | |
319 | ret = vgic_init(kvm); | |
320 | mutex_unlock(&kvm->lock); | |
321 | } | |
322 | ||
323 | return ret; | |
324 | } | |
325 | ||
90977732 EA |
326 | /* GENERIC PROBE */ |
327 | ||
328 | static void vgic_init_maintenance_interrupt(void *info) | |
329 | { | |
330 | enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0); | |
331 | } | |
332 | ||
333 | static int vgic_cpu_notify(struct notifier_block *self, | |
334 | unsigned long action, void *cpu) | |
335 | { | |
336 | switch (action) { | |
337 | case CPU_STARTING: | |
338 | case CPU_STARTING_FROZEN: | |
339 | vgic_init_maintenance_interrupt(NULL); | |
340 | break; | |
341 | case CPU_DYING: | |
342 | case CPU_DYING_FROZEN: | |
343 | disable_percpu_irq(kvm_vgic_global_state.maint_irq); | |
344 | break; | |
345 | } | |
346 | ||
347 | return NOTIFY_OK; | |
348 | } | |
349 | ||
350 | static struct notifier_block vgic_cpu_nb = { | |
351 | .notifier_call = vgic_cpu_notify, | |
352 | }; | |
353 | ||
354 | static irqreturn_t vgic_maintenance_handler(int irq, void *data) | |
355 | { | |
356 | /* | |
357 | * We cannot rely on the vgic maintenance interrupt to be | |
358 | * delivered synchronously. This means we can only use it to | |
359 | * exit the VM, and we perform the handling of EOIed | |
360 | * interrupts on the exit path (see vgic_process_maintenance). | |
361 | */ | |
362 | return IRQ_HANDLED; | |
363 | } | |
364 | ||
365 | /** | |
366 | * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable | |
367 | * according to the host GIC model. Accordingly calls either | |
368 | * vgic_v2/v3_probe which registers the KVM_DEVICE that can be | |
369 | * instantiated by a guest later on . | |
370 | */ | |
371 | int kvm_vgic_hyp_init(void) | |
372 | { | |
373 | const struct gic_kvm_info *gic_kvm_info; | |
374 | int ret; | |
375 | ||
376 | gic_kvm_info = gic_get_kvm_info(); | |
377 | if (!gic_kvm_info) | |
378 | return -ENODEV; | |
379 | ||
380 | if (!gic_kvm_info->maint_irq) { | |
381 | kvm_err("No vgic maintenance irq\n"); | |
382 | return -ENXIO; | |
383 | } | |
384 | ||
385 | switch (gic_kvm_info->type) { | |
386 | case GIC_V2: | |
387 | ret = vgic_v2_probe(gic_kvm_info); | |
388 | break; | |
389 | case GIC_V3: | |
390 | ret = vgic_v3_probe(gic_kvm_info); | |
391 | break; | |
392 | default: | |
393 | ret = -ENODEV; | |
394 | }; | |
395 | ||
396 | if (ret) | |
397 | return ret; | |
398 | ||
399 | kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq; | |
400 | ret = request_percpu_irq(kvm_vgic_global_state.maint_irq, | |
401 | vgic_maintenance_handler, | |
402 | "vgic", kvm_get_running_vcpus()); | |
403 | if (ret) { | |
404 | kvm_err("Cannot register interrupt %d\n", | |
405 | kvm_vgic_global_state.maint_irq); | |
406 | return ret; | |
407 | } | |
408 | ||
409 | ret = __register_cpu_notifier(&vgic_cpu_nb); | |
410 | if (ret) { | |
411 | kvm_err("Cannot register vgic CPU notifier\n"); | |
412 | goto out_free_irq; | |
413 | } | |
414 | ||
415 | on_each_cpu(vgic_init_maintenance_interrupt, NULL, 1); | |
416 | ||
417 | kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq); | |
418 | return 0; | |
419 | ||
420 | out_free_irq: | |
421 | free_percpu_irq(kvm_vgic_global_state.maint_irq, | |
422 | kvm_get_running_vcpus()); | |
423 | return ret; | |
424 | } |