]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - virt/kvm/arm/vgic/vgic-v2.c
Merge tag 'ktest-v4.17' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[mirror_ubuntu-eoan-kernel.git] / virt / kvm / arm / vgic / vgic-v2.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/irqchip/arm-gic.h>
18 #include <linux/kvm.h>
19 #include <linux/kvm_host.h>
20 #include <kvm/arm_vgic.h>
21 #include <asm/kvm_mmu.h>
22
23 #include "vgic.h"
24
25 static inline void vgic_v2_write_lr(int lr, u32 val)
26 {
27 void __iomem *base = kvm_vgic_global_state.vctrl_base;
28
29 writel_relaxed(val, base + GICH_LR0 + (lr * 4));
30 }
31
32 void vgic_v2_init_lrs(void)
33 {
34 int i;
35
36 for (i = 0; i < kvm_vgic_global_state.nr_lr; i++)
37 vgic_v2_write_lr(i, 0);
38 }
39
40 void vgic_v2_set_npie(struct kvm_vcpu *vcpu)
41 {
42 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
43
44 cpuif->vgic_hcr |= GICH_HCR_NPIE;
45 }
46
47 void vgic_v2_set_underflow(struct kvm_vcpu *vcpu)
48 {
49 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
50
51 cpuif->vgic_hcr |= GICH_HCR_UIE;
52 }
53
54 static bool lr_signals_eoi_mi(u32 lr_val)
55 {
56 return !(lr_val & GICH_LR_STATE) && (lr_val & GICH_LR_EOI) &&
57 !(lr_val & GICH_LR_HW);
58 }
59
60 /*
61 * transfer the content of the LRs back into the corresponding ap_list:
62 * - active bit is transferred as is
63 * - pending bit is
64 * - transferred as is in case of edge sensitive IRQs
65 * - set to the line-level (resample time) for level sensitive IRQs
66 */
67 void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu)
68 {
69 struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
70 struct vgic_v2_cpu_if *cpuif = &vgic_cpu->vgic_v2;
71 int lr;
72 unsigned long flags;
73
74 cpuif->vgic_hcr &= ~(GICH_HCR_UIE | GICH_HCR_NPIE);
75
76 for (lr = 0; lr < vgic_cpu->used_lrs; lr++) {
77 u32 val = cpuif->vgic_lr[lr];
78 u32 intid = val & GICH_LR_VIRTUALID;
79 struct vgic_irq *irq;
80
81 /* Notify fds when the guest EOI'ed a level-triggered SPI */
82 if (lr_signals_eoi_mi(val) && vgic_valid_spi(vcpu->kvm, intid))
83 kvm_notify_acked_irq(vcpu->kvm, 0,
84 intid - VGIC_NR_PRIVATE_IRQS);
85
86 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
87
88 spin_lock_irqsave(&irq->irq_lock, flags);
89
90 /* Always preserve the active bit */
91 irq->active = !!(val & GICH_LR_ACTIVE_BIT);
92
93 /* Edge is the only case where we preserve the pending bit */
94 if (irq->config == VGIC_CONFIG_EDGE &&
95 (val & GICH_LR_PENDING_BIT)) {
96 irq->pending_latch = true;
97
98 if (vgic_irq_is_sgi(intid)) {
99 u32 cpuid = val & GICH_LR_PHYSID_CPUID;
100
101 cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
102 irq->source |= (1 << cpuid);
103 }
104 }
105
106 /*
107 * Clear soft pending state when level irqs have been acked.
108 */
109 if (irq->config == VGIC_CONFIG_LEVEL && !(val & GICH_LR_STATE))
110 irq->pending_latch = false;
111
112 /*
113 * Level-triggered mapped IRQs are special because we only
114 * observe rising edges as input to the VGIC.
115 *
116 * If the guest never acked the interrupt we have to sample
117 * the physical line and set the line level, because the
118 * device state could have changed or we simply need to
119 * process the still pending interrupt later.
120 *
121 * If this causes us to lower the level, we have to also clear
122 * the physical active state, since we will otherwise never be
123 * told when the interrupt becomes asserted again.
124 */
125 if (vgic_irq_is_mapped_level(irq) && (val & GICH_LR_PENDING_BIT)) {
126 irq->line_level = vgic_get_phys_line_level(irq);
127
128 if (!irq->line_level)
129 vgic_irq_set_phys_active(irq, false);
130 }
131
132 spin_unlock_irqrestore(&irq->irq_lock, flags);
133 vgic_put_irq(vcpu->kvm, irq);
134 }
135
136 vgic_cpu->used_lrs = 0;
137 }
138
139 /*
140 * Populates the particular LR with the state of a given IRQ:
141 * - for an edge sensitive IRQ the pending state is cleared in struct vgic_irq
142 * - for a level sensitive IRQ the pending state value is unchanged;
143 * it is dictated directly by the input level
144 *
145 * If @irq describes an SGI with multiple sources, we choose the
146 * lowest-numbered source VCPU and clear that bit in the source bitmap.
147 *
148 * The irq_lock must be held by the caller.
149 */
150 void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
151 {
152 u32 val = irq->intid;
153 bool allow_pending = true;
154
155 if (irq->active)
156 val |= GICH_LR_ACTIVE_BIT;
157
158 if (irq->hw) {
159 val |= GICH_LR_HW;
160 val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT;
161 /*
162 * Never set pending+active on a HW interrupt, as the
163 * pending state is kept at the physical distributor
164 * level.
165 */
166 if (irq->active)
167 allow_pending = false;
168 } else {
169 if (irq->config == VGIC_CONFIG_LEVEL) {
170 val |= GICH_LR_EOI;
171
172 /*
173 * Software resampling doesn't work very well
174 * if we allow P+A, so let's not do that.
175 */
176 if (irq->active)
177 allow_pending = false;
178 }
179 }
180
181 if (allow_pending && irq_is_pending(irq)) {
182 val |= GICH_LR_PENDING_BIT;
183
184 if (irq->config == VGIC_CONFIG_EDGE)
185 irq->pending_latch = false;
186
187 if (vgic_irq_is_sgi(irq->intid)) {
188 u32 src = ffs(irq->source);
189
190 BUG_ON(!src);
191 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
192 irq->source &= ~(1 << (src - 1));
193 if (irq->source)
194 irq->pending_latch = true;
195 }
196 }
197
198 /*
199 * Level-triggered mapped IRQs are special because we only observe
200 * rising edges as input to the VGIC. We therefore lower the line
201 * level here, so that we can take new virtual IRQs. See
202 * vgic_v2_fold_lr_state for more info.
203 */
204 if (vgic_irq_is_mapped_level(irq) && (val & GICH_LR_PENDING_BIT))
205 irq->line_level = false;
206
207 /* The GICv2 LR only holds five bits of priority. */
208 val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT;
209
210 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = val;
211 }
212
213 void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr)
214 {
215 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = 0;
216 }
217
218 void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
219 {
220 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
221 u32 vmcr;
222
223 vmcr = (vmcrp->grpen0 << GICH_VMCR_ENABLE_GRP0_SHIFT) &
224 GICH_VMCR_ENABLE_GRP0_MASK;
225 vmcr |= (vmcrp->grpen1 << GICH_VMCR_ENABLE_GRP1_SHIFT) &
226 GICH_VMCR_ENABLE_GRP1_MASK;
227 vmcr |= (vmcrp->ackctl << GICH_VMCR_ACK_CTL_SHIFT) &
228 GICH_VMCR_ACK_CTL_MASK;
229 vmcr |= (vmcrp->fiqen << GICH_VMCR_FIQ_EN_SHIFT) &
230 GICH_VMCR_FIQ_EN_MASK;
231 vmcr |= (vmcrp->cbpr << GICH_VMCR_CBPR_SHIFT) &
232 GICH_VMCR_CBPR_MASK;
233 vmcr |= (vmcrp->eoim << GICH_VMCR_EOI_MODE_SHIFT) &
234 GICH_VMCR_EOI_MODE_MASK;
235 vmcr |= (vmcrp->abpr << GICH_VMCR_ALIAS_BINPOINT_SHIFT) &
236 GICH_VMCR_ALIAS_BINPOINT_MASK;
237 vmcr |= (vmcrp->bpr << GICH_VMCR_BINPOINT_SHIFT) &
238 GICH_VMCR_BINPOINT_MASK;
239 vmcr |= ((vmcrp->pmr >> GICV_PMR_PRIORITY_SHIFT) <<
240 GICH_VMCR_PRIMASK_SHIFT) & GICH_VMCR_PRIMASK_MASK;
241
242 cpu_if->vgic_vmcr = vmcr;
243 }
244
245 void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
246 {
247 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
248 u32 vmcr;
249
250 vmcr = cpu_if->vgic_vmcr;
251
252 vmcrp->grpen0 = (vmcr & GICH_VMCR_ENABLE_GRP0_MASK) >>
253 GICH_VMCR_ENABLE_GRP0_SHIFT;
254 vmcrp->grpen1 = (vmcr & GICH_VMCR_ENABLE_GRP1_MASK) >>
255 GICH_VMCR_ENABLE_GRP1_SHIFT;
256 vmcrp->ackctl = (vmcr & GICH_VMCR_ACK_CTL_MASK) >>
257 GICH_VMCR_ACK_CTL_SHIFT;
258 vmcrp->fiqen = (vmcr & GICH_VMCR_FIQ_EN_MASK) >>
259 GICH_VMCR_FIQ_EN_SHIFT;
260 vmcrp->cbpr = (vmcr & GICH_VMCR_CBPR_MASK) >>
261 GICH_VMCR_CBPR_SHIFT;
262 vmcrp->eoim = (vmcr & GICH_VMCR_EOI_MODE_MASK) >>
263 GICH_VMCR_EOI_MODE_SHIFT;
264
265 vmcrp->abpr = (vmcr & GICH_VMCR_ALIAS_BINPOINT_MASK) >>
266 GICH_VMCR_ALIAS_BINPOINT_SHIFT;
267 vmcrp->bpr = (vmcr & GICH_VMCR_BINPOINT_MASK) >>
268 GICH_VMCR_BINPOINT_SHIFT;
269 vmcrp->pmr = ((vmcr & GICH_VMCR_PRIMASK_MASK) >>
270 GICH_VMCR_PRIMASK_SHIFT) << GICV_PMR_PRIORITY_SHIFT;
271 }
272
273 void vgic_v2_enable(struct kvm_vcpu *vcpu)
274 {
275 /*
276 * By forcing VMCR to zero, the GIC will restore the binary
277 * points to their reset values. Anything else resets to zero
278 * anyway.
279 */
280 vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = 0;
281
282 /* Get the show on the road... */
283 vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr = GICH_HCR_EN;
284 }
285
286 /* check for overlapping regions and for regions crossing the end of memory */
287 static bool vgic_v2_check_base(gpa_t dist_base, gpa_t cpu_base)
288 {
289 if (dist_base + KVM_VGIC_V2_DIST_SIZE < dist_base)
290 return false;
291 if (cpu_base + KVM_VGIC_V2_CPU_SIZE < cpu_base)
292 return false;
293
294 if (dist_base + KVM_VGIC_V2_DIST_SIZE <= cpu_base)
295 return true;
296 if (cpu_base + KVM_VGIC_V2_CPU_SIZE <= dist_base)
297 return true;
298
299 return false;
300 }
301
302 int vgic_v2_map_resources(struct kvm *kvm)
303 {
304 struct vgic_dist *dist = &kvm->arch.vgic;
305 int ret = 0;
306
307 if (vgic_ready(kvm))
308 goto out;
309
310 if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base) ||
311 IS_VGIC_ADDR_UNDEF(dist->vgic_cpu_base)) {
312 kvm_err("Need to set vgic cpu and dist addresses first\n");
313 ret = -ENXIO;
314 goto out;
315 }
316
317 if (!vgic_v2_check_base(dist->vgic_dist_base, dist->vgic_cpu_base)) {
318 kvm_err("VGIC CPU and dist frames overlap\n");
319 ret = -EINVAL;
320 goto out;
321 }
322
323 /*
324 * Initialize the vgic if this hasn't already been done on demand by
325 * accessing the vgic state from userspace.
326 */
327 ret = vgic_init(kvm);
328 if (ret) {
329 kvm_err("Unable to initialize VGIC dynamic data structures\n");
330 goto out;
331 }
332
333 ret = vgic_register_dist_iodev(kvm, dist->vgic_dist_base, VGIC_V2);
334 if (ret) {
335 kvm_err("Unable to register VGIC MMIO regions\n");
336 goto out;
337 }
338
339 if (!static_branch_unlikely(&vgic_v2_cpuif_trap)) {
340 ret = kvm_phys_addr_ioremap(kvm, dist->vgic_cpu_base,
341 kvm_vgic_global_state.vcpu_base,
342 KVM_VGIC_V2_CPU_SIZE, true);
343 if (ret) {
344 kvm_err("Unable to remap VGIC CPU to VCPU\n");
345 goto out;
346 }
347 }
348
349 dist->ready = true;
350
351 out:
352 return ret;
353 }
354
355 DEFINE_STATIC_KEY_FALSE(vgic_v2_cpuif_trap);
356
357 /**
358 * vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT
359 * @node: pointer to the DT node
360 *
361 * Returns 0 if a GICv2 has been found, returns an error code otherwise
362 */
363 int vgic_v2_probe(const struct gic_kvm_info *info)
364 {
365 int ret;
366 u32 vtr;
367
368 if (!info->vctrl.start) {
369 kvm_err("GICH not present in the firmware table\n");
370 return -ENXIO;
371 }
372
373 if (!PAGE_ALIGNED(info->vcpu.start) ||
374 !PAGE_ALIGNED(resource_size(&info->vcpu))) {
375 kvm_info("GICV region size/alignment is unsafe, using trapping (reduced performance)\n");
376
377 ret = create_hyp_io_mappings(info->vcpu.start,
378 resource_size(&info->vcpu),
379 &kvm_vgic_global_state.vcpu_base_va,
380 &kvm_vgic_global_state.vcpu_hyp_va);
381 if (ret) {
382 kvm_err("Cannot map GICV into hyp\n");
383 goto out;
384 }
385
386 static_branch_enable(&vgic_v2_cpuif_trap);
387 }
388
389 ret = create_hyp_io_mappings(info->vctrl.start,
390 resource_size(&info->vctrl),
391 &kvm_vgic_global_state.vctrl_base,
392 &kvm_vgic_global_state.vctrl_hyp);
393 if (ret) {
394 kvm_err("Cannot map VCTRL into hyp\n");
395 goto out;
396 }
397
398 vtr = readl_relaxed(kvm_vgic_global_state.vctrl_base + GICH_VTR);
399 kvm_vgic_global_state.nr_lr = (vtr & 0x3f) + 1;
400
401 ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2);
402 if (ret) {
403 kvm_err("Cannot register GICv2 KVM device\n");
404 goto out;
405 }
406
407 kvm_vgic_global_state.can_emulate_gicv2 = true;
408 kvm_vgic_global_state.vcpu_base = info->vcpu.start;
409 kvm_vgic_global_state.type = VGIC_V2;
410 kvm_vgic_global_state.max_gic_vcpus = VGIC_V2_MAX_CPUS;
411
412 kvm_debug("vgic-v2@%llx\n", info->vctrl.start);
413
414 return 0;
415 out:
416 if (kvm_vgic_global_state.vctrl_base)
417 iounmap(kvm_vgic_global_state.vctrl_base);
418 if (kvm_vgic_global_state.vcpu_base_va)
419 iounmap(kvm_vgic_global_state.vcpu_base_va);
420
421 return ret;
422 }
423
424 static void save_lrs(struct kvm_vcpu *vcpu, void __iomem *base)
425 {
426 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
427 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
428 u64 elrsr;
429 int i;
430
431 elrsr = readl_relaxed(base + GICH_ELRSR0);
432 if (unlikely(used_lrs > 32))
433 elrsr |= ((u64)readl_relaxed(base + GICH_ELRSR1)) << 32;
434
435 for (i = 0; i < used_lrs; i++) {
436 if (elrsr & (1UL << i))
437 cpu_if->vgic_lr[i] &= ~GICH_LR_STATE;
438 else
439 cpu_if->vgic_lr[i] = readl_relaxed(base + GICH_LR0 + (i * 4));
440
441 writel_relaxed(0, base + GICH_LR0 + (i * 4));
442 }
443 }
444
445 void vgic_v2_save_state(struct kvm_vcpu *vcpu)
446 {
447 void __iomem *base = kvm_vgic_global_state.vctrl_base;
448 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
449
450 if (!base)
451 return;
452
453 if (used_lrs) {
454 save_lrs(vcpu, base);
455 writel_relaxed(0, base + GICH_HCR);
456 }
457 }
458
459 void vgic_v2_restore_state(struct kvm_vcpu *vcpu)
460 {
461 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
462 void __iomem *base = kvm_vgic_global_state.vctrl_base;
463 u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
464 int i;
465
466 if (!base)
467 return;
468
469 if (used_lrs) {
470 writel_relaxed(cpu_if->vgic_hcr, base + GICH_HCR);
471 for (i = 0; i < used_lrs; i++) {
472 writel_relaxed(cpu_if->vgic_lr[i],
473 base + GICH_LR0 + (i * 4));
474 }
475 }
476 }
477
478 void vgic_v2_load(struct kvm_vcpu *vcpu)
479 {
480 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
481
482 writel_relaxed(cpu_if->vgic_vmcr,
483 kvm_vgic_global_state.vctrl_base + GICH_VMCR);
484 writel_relaxed(cpu_if->vgic_apr,
485 kvm_vgic_global_state.vctrl_base + GICH_APR);
486 }
487
488 void vgic_v2_put(struct kvm_vcpu *vcpu)
489 {
490 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
491
492 cpu_if->vgic_vmcr = readl_relaxed(kvm_vgic_global_state.vctrl_base + GICH_VMCR);
493 cpu_if->vgic_apr = readl_relaxed(kvm_vgic_global_state.vctrl_base + GICH_APR);
494 }