blob: 2f241e026c8f38410a4189dd127e46530c0c5ddd [file] [log] [blame]
Marc Zyngier140b0862015-11-26 17:19:25 +00001/*
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>
Eric Auger90977732015-12-01 15:02:35 +010020#include <kvm/arm_vgic.h>
21#include <asm/kvm_mmu.h>
Marc Zyngier140b0862015-11-26 17:19:25 +000022
23#include "vgic.h"
24
25/*
26 * Call this function to convert a u64 value to an unsigned long * bitmask
27 * in a way that works on both 32-bit and 64-bit LE and BE platforms.
28 *
29 * Warning: Calling this function may modify *val.
30 */
31static unsigned long *u64_to_bitmask(u64 *val)
32{
33#if defined(CONFIG_CPU_BIG_ENDIAN) && BITS_PER_LONG == 32
34 *val = (*val >> 32) | (*val << 32);
35#endif
36 return (unsigned long *)val;
37}
38
39void vgic_v2_process_maintenance(struct kvm_vcpu *vcpu)
40{
41 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
42
43 if (cpuif->vgic_misr & GICH_MISR_EOI) {
44 u64 eisr = cpuif->vgic_eisr;
45 unsigned long *eisr_bmap = u64_to_bitmask(&eisr);
46 int lr;
47
48 for_each_set_bit(lr, eisr_bmap, kvm_vgic_global_state.nr_lr) {
49 u32 intid = cpuif->vgic_lr[lr] & GICH_LR_VIRTUALID;
50
51 WARN_ON(cpuif->vgic_lr[lr] & GICH_LR_STATE);
52
Marc Zyngier8ca18ee2016-11-23 10:11:21 +000053 /* Only SPIs require notification */
54 if (vgic_valid_spi(vcpu->kvm, intid))
55 kvm_notify_acked_irq(vcpu->kvm, 0,
56 intid - VGIC_NR_PRIVATE_IRQS);
Marc Zyngier140b0862015-11-26 17:19:25 +000057 }
58 }
59
60 /* check and disable underflow maintenance IRQ */
61 cpuif->vgic_hcr &= ~GICH_HCR_UIE;
62
63 /*
64 * In the next iterations of the vcpu loop, if we sync the
65 * vgic state after flushing it, but before entering the guest
66 * (this happens for pending signals and vmid rollovers), then
67 * make sure we don't pick up any old maintenance interrupts
68 * here.
69 */
70 cpuif->vgic_eisr = 0;
71}
72
73void vgic_v2_set_underflow(struct kvm_vcpu *vcpu)
74{
75 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
76
77 cpuif->vgic_hcr |= GICH_HCR_UIE;
78}
79
80/*
81 * transfer the content of the LRs back into the corresponding ap_list:
82 * - active bit is transferred as is
83 * - pending bit is
84 * - transferred as is in case of edge sensitive IRQs
85 * - set to the line-level (resample time) for level sensitive IRQs
86 */
87void vgic_v2_fold_lr_state(struct kvm_vcpu *vcpu)
88{
89 struct vgic_v2_cpu_if *cpuif = &vcpu->arch.vgic_cpu.vgic_v2;
90 int lr;
91
92 for (lr = 0; lr < vcpu->arch.vgic_cpu.used_lrs; lr++) {
93 u32 val = cpuif->vgic_lr[lr];
94 u32 intid = val & GICH_LR_VIRTUALID;
95 struct vgic_irq *irq;
96
97 irq = vgic_get_irq(vcpu->kvm, vcpu, intid);
98
99 spin_lock(&irq->irq_lock);
100
101 /* Always preserve the active bit */
102 irq->active = !!(val & GICH_LR_ACTIVE_BIT);
103
104 /* Edge is the only case where we preserve the pending bit */
105 if (irq->config == VGIC_CONFIG_EDGE &&
106 (val & GICH_LR_PENDING_BIT)) {
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100107 irq->pending_latch = true;
Marc Zyngier140b0862015-11-26 17:19:25 +0000108
109 if (vgic_irq_is_sgi(intid)) {
110 u32 cpuid = val & GICH_LR_PHYSID_CPUID;
111
112 cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
113 irq->source |= (1 << cpuid);
114 }
115 }
116
Marc Zyngierdf7942d2016-05-25 15:26:35 +0100117 /*
118 * Clear soft pending state when level irqs have been acked.
119 * Always regenerate the pending state.
120 */
121 if (irq->config == VGIC_CONFIG_LEVEL) {
122 if (!(val & GICH_LR_PENDING_BIT))
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100123 irq->pending_latch = false;
Marc Zyngier140b0862015-11-26 17:19:25 +0000124 }
125
126 spin_unlock(&irq->irq_lock);
Andre Przywara5dd4b922016-07-15 12:43:27 +0100127 vgic_put_irq(vcpu->kvm, irq);
Marc Zyngier140b0862015-11-26 17:19:25 +0000128 }
129}
130
131/*
132 * Populates the particular LR with the state of a given IRQ:
133 * - for an edge sensitive IRQ the pending state is cleared in struct vgic_irq
134 * - for a level sensitive IRQ the pending state value is unchanged;
135 * it is dictated directly by the input level
136 *
137 * If @irq describes an SGI with multiple sources, we choose the
138 * lowest-numbered source VCPU and clear that bit in the source bitmap.
139 *
140 * The irq_lock must be held by the caller.
141 */
142void vgic_v2_populate_lr(struct kvm_vcpu *vcpu, struct vgic_irq *irq, int lr)
143{
144 u32 val = irq->intid;
145
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100146 if (irq_is_pending(irq)) {
Marc Zyngier140b0862015-11-26 17:19:25 +0000147 val |= GICH_LR_PENDING_BIT;
148
149 if (irq->config == VGIC_CONFIG_EDGE)
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100150 irq->pending_latch = false;
Marc Zyngier140b0862015-11-26 17:19:25 +0000151
152 if (vgic_irq_is_sgi(irq->intid)) {
153 u32 src = ffs(irq->source);
154
155 BUG_ON(!src);
156 val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
157 irq->source &= ~(1 << (src - 1));
158 if (irq->source)
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100159 irq->pending_latch = true;
Marc Zyngier140b0862015-11-26 17:19:25 +0000160 }
161 }
162
163 if (irq->active)
164 val |= GICH_LR_ACTIVE_BIT;
165
166 if (irq->hw) {
167 val |= GICH_LR_HW;
168 val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT;
169 } else {
170 if (irq->config == VGIC_CONFIG_LEVEL)
171 val |= GICH_LR_EOI;
172 }
173
174 /* The GICv2 LR only holds five bits of priority. */
175 val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT;
176
177 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = val;
178}
179
180void vgic_v2_clear_lr(struct kvm_vcpu *vcpu, int lr)
181{
182 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = 0;
183}
Andre Przywarae4823a72015-12-03 11:47:37 +0000184
185void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
186{
Christoffer Dall328e56642016-03-24 11:21:04 +0100187 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
Andre Przywarae4823a72015-12-03 11:47:37 +0000188 u32 vmcr;
189
190 vmcr = (vmcrp->ctlr << GICH_VMCR_CTRL_SHIFT) & GICH_VMCR_CTRL_MASK;
191 vmcr |= (vmcrp->abpr << GICH_VMCR_ALIAS_BINPOINT_SHIFT) &
192 GICH_VMCR_ALIAS_BINPOINT_MASK;
193 vmcr |= (vmcrp->bpr << GICH_VMCR_BINPOINT_SHIFT) &
194 GICH_VMCR_BINPOINT_MASK;
195 vmcr |= (vmcrp->pmr << GICH_VMCR_PRIMASK_SHIFT) &
196 GICH_VMCR_PRIMASK_MASK;
197
Christoffer Dall328e56642016-03-24 11:21:04 +0100198 cpu_if->vgic_vmcr = vmcr;
Andre Przywarae4823a72015-12-03 11:47:37 +0000199}
200
201void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
202{
Christoffer Dall328e56642016-03-24 11:21:04 +0100203 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
204 u32 vmcr;
205
206 vmcr = cpu_if->vgic_vmcr;
Andre Przywarae4823a72015-12-03 11:47:37 +0000207
208 vmcrp->ctlr = (vmcr & GICH_VMCR_CTRL_MASK) >>
209 GICH_VMCR_CTRL_SHIFT;
210 vmcrp->abpr = (vmcr & GICH_VMCR_ALIAS_BINPOINT_MASK) >>
211 GICH_VMCR_ALIAS_BINPOINT_SHIFT;
212 vmcrp->bpr = (vmcr & GICH_VMCR_BINPOINT_MASK) >>
213 GICH_VMCR_BINPOINT_SHIFT;
214 vmcrp->pmr = (vmcr & GICH_VMCR_PRIMASK_MASK) >>
215 GICH_VMCR_PRIMASK_SHIFT;
216}
Eric Auger90977732015-12-01 15:02:35 +0100217
Eric Augerad275b8b2015-12-21 18:09:38 +0100218void vgic_v2_enable(struct kvm_vcpu *vcpu)
219{
Eric Augerf7b69852015-12-02 10:30:13 +0100220 /*
221 * By forcing VMCR to zero, the GIC will restore the binary
222 * points to their reset values. Anything else resets to zero
223 * anyway.
224 */
225 vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = 0;
226 vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr = ~0;
227
228 /* Get the show on the road... */
229 vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr = GICH_HCR_EN;
Eric Augerad275b8b2015-12-21 18:09:38 +0100230}
231
Eric Augerb0442ee2015-12-21 15:04:42 +0100232/* check for overlapping regions and for regions crossing the end of memory */
233static bool vgic_v2_check_base(gpa_t dist_base, gpa_t cpu_base)
234{
235 if (dist_base + KVM_VGIC_V2_DIST_SIZE < dist_base)
236 return false;
237 if (cpu_base + KVM_VGIC_V2_CPU_SIZE < cpu_base)
238 return false;
239
240 if (dist_base + KVM_VGIC_V2_DIST_SIZE <= cpu_base)
241 return true;
242 if (cpu_base + KVM_VGIC_V2_CPU_SIZE <= dist_base)
243 return true;
244
245 return false;
246}
247
248int vgic_v2_map_resources(struct kvm *kvm)
249{
250 struct vgic_dist *dist = &kvm->arch.vgic;
251 int ret = 0;
252
253 if (vgic_ready(kvm))
254 goto out;
255
256 if (IS_VGIC_ADDR_UNDEF(dist->vgic_dist_base) ||
257 IS_VGIC_ADDR_UNDEF(dist->vgic_cpu_base)) {
258 kvm_err("Need to set vgic cpu and dist addresses first\n");
259 ret = -ENXIO;
260 goto out;
261 }
262
263 if (!vgic_v2_check_base(dist->vgic_dist_base, dist->vgic_cpu_base)) {
264 kvm_err("VGIC CPU and dist frames overlap\n");
265 ret = -EINVAL;
266 goto out;
267 }
268
269 /*
270 * Initialize the vgic if this hasn't already been done on demand by
271 * accessing the vgic state from userspace.
272 */
273 ret = vgic_init(kvm);
274 if (ret) {
275 kvm_err("Unable to initialize VGIC dynamic data structures\n");
276 goto out;
277 }
278
279 ret = vgic_register_dist_iodev(kvm, dist->vgic_dist_base, VGIC_V2);
280 if (ret) {
281 kvm_err("Unable to register VGIC MMIO regions\n");
282 goto out;
283 }
284
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100285 if (!static_branch_unlikely(&vgic_v2_cpuif_trap)) {
286 ret = kvm_phys_addr_ioremap(kvm, dist->vgic_cpu_base,
287 kvm_vgic_global_state.vcpu_base,
288 KVM_VGIC_V2_CPU_SIZE, true);
289 if (ret) {
290 kvm_err("Unable to remap VGIC CPU to VCPU\n");
291 goto out;
292 }
Eric Augerb0442ee2015-12-21 15:04:42 +0100293 }
294
295 dist->ready = true;
296
297out:
Eric Augerb0442ee2015-12-21 15:04:42 +0100298 return ret;
299}
300
Marc Zyngierfb5ee362016-09-06 09:28:45 +0100301DEFINE_STATIC_KEY_FALSE(vgic_v2_cpuif_trap);
302
Eric Auger90977732015-12-01 15:02:35 +0100303/**
304 * vgic_v2_probe - probe for a GICv2 compatible interrupt controller in DT
305 * @node: pointer to the DT node
306 *
307 * Returns 0 if a GICv2 has been found, returns an error code otherwise
308 */
309int vgic_v2_probe(const struct gic_kvm_info *info)
310{
311 int ret;
312 u32 vtr;
313
314 if (!info->vctrl.start) {
315 kvm_err("GICH not present in the firmware table\n");
316 return -ENXIO;
317 }
318
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100319 if (!PAGE_ALIGNED(info->vcpu.start) ||
320 !PAGE_ALIGNED(resource_size(&info->vcpu))) {
321 kvm_info("GICV region size/alignment is unsafe, using trapping (reduced performance)\n");
322 kvm_vgic_global_state.vcpu_base_va = ioremap(info->vcpu.start,
323 resource_size(&info->vcpu));
324 if (!kvm_vgic_global_state.vcpu_base_va) {
325 kvm_err("Cannot ioremap GICV\n");
326 return -ENOMEM;
327 }
Eric Auger90977732015-12-01 15:02:35 +0100328
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100329 ret = create_hyp_io_mappings(kvm_vgic_global_state.vcpu_base_va,
330 kvm_vgic_global_state.vcpu_base_va + resource_size(&info->vcpu),
331 info->vcpu.start);
332 if (ret) {
333 kvm_err("Cannot map GICV into hyp\n");
334 goto out;
335 }
336
337 static_branch_enable(&vgic_v2_cpuif_trap);
Eric Auger90977732015-12-01 15:02:35 +0100338 }
339
340 kvm_vgic_global_state.vctrl_base = ioremap(info->vctrl.start,
341 resource_size(&info->vctrl));
342 if (!kvm_vgic_global_state.vctrl_base) {
343 kvm_err("Cannot ioremap GICH\n");
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100344 ret = -ENOMEM;
345 goto out;
Eric Auger90977732015-12-01 15:02:35 +0100346 }
347
348 vtr = readl_relaxed(kvm_vgic_global_state.vctrl_base + GICH_VTR);
349 kvm_vgic_global_state.nr_lr = (vtr & 0x3f) + 1;
350
351 ret = create_hyp_io_mappings(kvm_vgic_global_state.vctrl_base,
352 kvm_vgic_global_state.vctrl_base +
353 resource_size(&info->vctrl),
354 info->vctrl.start);
Eric Auger90977732015-12-01 15:02:35 +0100355 if (ret) {
356 kvm_err("Cannot map VCTRL into hyp\n");
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100357 goto out;
358 }
359
360 ret = kvm_register_vgic_device(KVM_DEV_TYPE_ARM_VGIC_V2);
361 if (ret) {
362 kvm_err("Cannot register GICv2 KVM device\n");
363 goto out;
Eric Auger90977732015-12-01 15:02:35 +0100364 }
365
366 kvm_vgic_global_state.can_emulate_gicv2 = true;
Eric Auger90977732015-12-01 15:02:35 +0100367 kvm_vgic_global_state.vcpu_base = info->vcpu.start;
368 kvm_vgic_global_state.type = VGIC_V2;
369 kvm_vgic_global_state.max_gic_vcpus = VGIC_V2_MAX_CPUS;
370
371 kvm_info("vgic-v2@%llx\n", info->vctrl.start);
372
373 return 0;
Marc Zyngiera07d3b02016-09-06 09:28:47 +0100374out:
375 if (kvm_vgic_global_state.vctrl_base)
376 iounmap(kvm_vgic_global_state.vctrl_base);
377 if (kvm_vgic_global_state.vcpu_base_va)
378 iounmap(kvm_vgic_global_state.vcpu_base_va);
379
380 return ret;
Eric Auger90977732015-12-01 15:02:35 +0100381}
Christoffer Dall328e56642016-03-24 11:21:04 +0100382
383void vgic_v2_load(struct kvm_vcpu *vcpu)
384{
385 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
386 struct vgic_dist *vgic = &vcpu->kvm->arch.vgic;
387
388 writel_relaxed(cpu_if->vgic_vmcr, vgic->vctrl_base + GICH_VMCR);
389}
390
391void vgic_v2_put(struct kvm_vcpu *vcpu)
392{
393 struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
394 struct vgic_dist *vgic = &vcpu->kvm->arch.vgic;
395
396 cpu_if->vgic_vmcr = readl_relaxed(vgic->vctrl_base + GICH_VMCR);
397}