blob: 334cd7a891066d6dbef3af2944204fe3000c3d81 [file] [log] [blame]
Marc Zyngier8f186d52014-02-04 18:13:03 +00001/*
2 * Copyright (C) 2012,2013 ARM Limited, All Rights Reserved.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/cpu.h>
19#include <linux/kvm.h>
20#include <linux/kvm_host.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
Marc Zyngier8f186d52014-02-04 18:13:03 +000023
24#include <linux/irqchip/arm-gic.h>
25
26#include <asm/kvm_emulate.h>
27#include <asm/kvm_arm.h>
28#include <asm/kvm_mmu.h>
29
30static struct vgic_lr vgic_v2_get_lr(const struct kvm_vcpu *vcpu, int lr)
31{
32 struct vgic_lr lr_desc;
33 u32 val = vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr];
34
35 lr_desc.irq = val & GICH_LR_VIRTUALID;
36 if (lr_desc.irq <= 15)
37 lr_desc.source = (val >> GICH_LR_PHYSID_CPUID_SHIFT) & 0x7;
38 else
39 lr_desc.source = 0;
40 lr_desc.state = 0;
41
42 if (val & GICH_LR_PENDING_BIT)
43 lr_desc.state |= LR_STATE_PENDING;
44 if (val & GICH_LR_ACTIVE_BIT)
45 lr_desc.state |= LR_STATE_ACTIVE;
46 if (val & GICH_LR_EOI)
47 lr_desc.state |= LR_EOI_INT;
Marc Zyngierfb182cf2015-06-08 15:37:26 +010048 if (val & GICH_LR_HW) {
49 lr_desc.state |= LR_HW;
50 lr_desc.hwirq = (val & GICH_LR_PHYSID_CPUID) >> GICH_LR_PHYSID_CPUID_SHIFT;
51 }
Marc Zyngier8f186d52014-02-04 18:13:03 +000052
53 return lr_desc;
54}
55
56static void vgic_v2_set_lr(struct kvm_vcpu *vcpu, int lr,
57 struct vgic_lr lr_desc)
58{
Marc Zyngierfb182cf2015-06-08 15:37:26 +010059 u32 lr_val;
60
61 lr_val = lr_desc.irq;
Marc Zyngier8f186d52014-02-04 18:13:03 +000062
63 if (lr_desc.state & LR_STATE_PENDING)
64 lr_val |= GICH_LR_PENDING_BIT;
65 if (lr_desc.state & LR_STATE_ACTIVE)
66 lr_val |= GICH_LR_ACTIVE_BIT;
67 if (lr_desc.state & LR_EOI_INT)
68 lr_val |= GICH_LR_EOI;
69
Marc Zyngierfb182cf2015-06-08 15:37:26 +010070 if (lr_desc.state & LR_HW) {
71 lr_val |= GICH_LR_HW;
72 lr_val |= (u32)lr_desc.hwirq << GICH_LR_PHYSID_CPUID_SHIFT;
73 }
74
75 if (lr_desc.irq < VGIC_NR_SGIS)
76 lr_val |= (lr_desc.source << GICH_LR_PHYSID_CPUID_SHIFT);
77
Marc Zyngier8f186d52014-02-04 18:13:03 +000078 vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = lr_val;
Marc Zyngier8f186d52014-02-04 18:13:03 +000079
Marc Zyngier8f186d52014-02-04 18:13:03 +000080 if (!(lr_desc.state & LR_STATE_MASK))
Christoffer Dall2df36a52014-09-28 16:04:26 +020081 vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr |= (1ULL << lr);
Christoffer Dallae705932015-03-13 17:02:56 +000082 else
83 vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr &= ~(1ULL << lr);
Marc Zyngier8f186d52014-02-04 18:13:03 +000084}
85
86static u64 vgic_v2_get_elrsr(const struct kvm_vcpu *vcpu)
87{
Christoffer Dall2df36a52014-09-28 16:04:26 +020088 return vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr;
Marc Zyngier8f186d52014-02-04 18:13:03 +000089}
90
91static u64 vgic_v2_get_eisr(const struct kvm_vcpu *vcpu)
92{
Christoffer Dall2df36a52014-09-28 16:04:26 +020093 return vcpu->arch.vgic_cpu.vgic_v2.vgic_eisr;
Marc Zyngier8f186d52014-02-04 18:13:03 +000094}
95
Christoffer Dallae705932015-03-13 17:02:56 +000096static void vgic_v2_clear_eisr(struct kvm_vcpu *vcpu)
97{
98 vcpu->arch.vgic_cpu.vgic_v2.vgic_eisr = 0;
99}
100
Marc Zyngier8f186d52014-02-04 18:13:03 +0000101static u32 vgic_v2_get_interrupt_status(const struct kvm_vcpu *vcpu)
102{
103 u32 misr = vcpu->arch.vgic_cpu.vgic_v2.vgic_misr;
104 u32 ret = 0;
105
106 if (misr & GICH_MISR_EOI)
107 ret |= INT_STATUS_EOI;
108 if (misr & GICH_MISR_U)
109 ret |= INT_STATUS_UNDERFLOW;
110
111 return ret;
112}
113
114static void vgic_v2_enable_underflow(struct kvm_vcpu *vcpu)
115{
116 vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr |= GICH_HCR_UIE;
117}
118
119static void vgic_v2_disable_underflow(struct kvm_vcpu *vcpu)
120{
121 vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr &= ~GICH_HCR_UIE;
122}
123
124static void vgic_v2_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
125{
126 u32 vmcr = vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr;
127
128 vmcrp->ctlr = (vmcr & GICH_VMCR_CTRL_MASK) >> GICH_VMCR_CTRL_SHIFT;
129 vmcrp->abpr = (vmcr & GICH_VMCR_ALIAS_BINPOINT_MASK) >> GICH_VMCR_ALIAS_BINPOINT_SHIFT;
130 vmcrp->bpr = (vmcr & GICH_VMCR_BINPOINT_MASK) >> GICH_VMCR_BINPOINT_SHIFT;
131 vmcrp->pmr = (vmcr & GICH_VMCR_PRIMASK_MASK) >> GICH_VMCR_PRIMASK_SHIFT;
132}
133
134static void vgic_v2_set_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcrp)
135{
136 u32 vmcr;
137
138 vmcr = (vmcrp->ctlr << GICH_VMCR_CTRL_SHIFT) & GICH_VMCR_CTRL_MASK;
139 vmcr |= (vmcrp->abpr << GICH_VMCR_ALIAS_BINPOINT_SHIFT) & GICH_VMCR_ALIAS_BINPOINT_MASK;
140 vmcr |= (vmcrp->bpr << GICH_VMCR_BINPOINT_SHIFT) & GICH_VMCR_BINPOINT_MASK;
141 vmcr |= (vmcrp->pmr << GICH_VMCR_PRIMASK_SHIFT) & GICH_VMCR_PRIMASK_MASK;
142
143 vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = vmcr;
144}
145
146static void vgic_v2_enable(struct kvm_vcpu *vcpu)
147{
148 /*
149 * By forcing VMCR to zero, the GIC will restore the binary
150 * points to their reset values. Anything else resets to zero
151 * anyway.
152 */
153 vcpu->arch.vgic_cpu.vgic_v2.vgic_vmcr = 0;
Pavel Fedinc4cd4c12015-10-27 11:37:29 +0300154 vcpu->arch.vgic_cpu.vgic_v2.vgic_elrsr = ~0;
Marc Zyngier8f186d52014-02-04 18:13:03 +0000155
156 /* Get the show on the road... */
157 vcpu->arch.vgic_cpu.vgic_v2.vgic_hcr = GICH_HCR_EN;
158}
159
160static const struct vgic_ops vgic_v2_ops = {
161 .get_lr = vgic_v2_get_lr,
162 .set_lr = vgic_v2_set_lr,
Marc Zyngier8f186d52014-02-04 18:13:03 +0000163 .get_elrsr = vgic_v2_get_elrsr,
164 .get_eisr = vgic_v2_get_eisr,
Christoffer Dallae705932015-03-13 17:02:56 +0000165 .clear_eisr = vgic_v2_clear_eisr,
Marc Zyngier8f186d52014-02-04 18:13:03 +0000166 .get_interrupt_status = vgic_v2_get_interrupt_status,
167 .enable_underflow = vgic_v2_enable_underflow,
168 .disable_underflow = vgic_v2_disable_underflow,
169 .get_vmcr = vgic_v2_get_vmcr,
170 .set_vmcr = vgic_v2_set_vmcr,
171 .enable = vgic_v2_enable,
172};
173
Christoffer Dall2db4c102016-04-06 14:48:53 +0200174struct vgic_params __section(.hyp.text) vgic_v2_params;
Marc Zyngier8f186d52014-02-04 18:13:03 +0000175
Marc Zyngierd6400d72016-03-03 15:43:58 +0000176static void vgic_cpu_init_lrs(void *params)
177{
178 struct vgic_params *vgic = params;
179 int i;
180
181 for (i = 0; i < vgic->nr_lr; i++)
182 writel_relaxed(0, vgic->vctrl_base + GICH_LR0 + (i * 4));
183}
184
Marc Zyngier8f186d52014-02-04 18:13:03 +0000185/**
Julien Grall503a6282016-04-11 16:32:59 +0100186 * vgic_v2_probe - probe for a GICv2 compatible interrupt controller
187 * @gic_kvm_info: pointer to the GIC description
188 * @ops: address of a pointer to the GICv2 operations
189 * @params: address of a pointer to HW-specific parameters
Marc Zyngier8f186d52014-02-04 18:13:03 +0000190 *
191 * Returns 0 if a GICv2 has been found, with the low level operations
192 * in *ops and the HW parameters in *params. Returns an error code
193 * otherwise.
194 */
Julien Grall503a6282016-04-11 16:32:59 +0100195int vgic_v2_probe(const struct gic_kvm_info *gic_kvm_info,
196 const struct vgic_ops **ops,
197 const struct vgic_params **params)
Marc Zyngier8f186d52014-02-04 18:13:03 +0000198{
199 int ret;
Marc Zyngier8f186d52014-02-04 18:13:03 +0000200 struct vgic_params *vgic = &vgic_v2_params;
Julien Grall503a6282016-04-11 16:32:59 +0100201 const struct resource *vctrl_res = &gic_kvm_info->vctrl;
202 const struct resource *vcpu_res = &gic_kvm_info->vcpu;
Marc Zyngier8f186d52014-02-04 18:13:03 +0000203
Christoffer Dall2db4c102016-04-06 14:48:53 +0200204 memset(vgic, 0, sizeof(*vgic));
205
Julien Grall503a6282016-04-11 16:32:59 +0100206 if (!gic_kvm_info->maint_irq) {
207 kvm_err("error getting vgic maintenance irq\n");
208 ret = -ENXIO;
209 goto out;
210 }
211 vgic->maint_irq = gic_kvm_info->maint_irq;
212
213 if (!gic_kvm_info->vctrl.start) {
214 kvm_err("GICH not present in the firmware table\n");
Marc Zyngier8f186d52014-02-04 18:13:03 +0000215 ret = -ENXIO;
216 goto out;
217 }
218
Julien Grall503a6282016-04-11 16:32:59 +0100219 vgic->vctrl_base = ioremap(gic_kvm_info->vctrl.start,
220 resource_size(&gic_kvm_info->vctrl));
Marc Zyngier8f186d52014-02-04 18:13:03 +0000221 if (!vgic->vctrl_base) {
222 kvm_err("Cannot ioremap GICH\n");
223 ret = -ENOMEM;
224 goto out;
225 }
226
227 vgic->nr_lr = readl_relaxed(vgic->vctrl_base + GICH_VTR);
228 vgic->nr_lr = (vgic->nr_lr & 0x3f) + 1;
229
230 ret = create_hyp_io_mappings(vgic->vctrl_base,
Julien Grall503a6282016-04-11 16:32:59 +0100231 vgic->vctrl_base + resource_size(vctrl_res),
232 vctrl_res->start);
Marc Zyngier8f186d52014-02-04 18:13:03 +0000233 if (ret) {
234 kvm_err("Cannot map VCTRL into hyp\n");
235 goto out_unmap;
236 }
237
Julien Grall503a6282016-04-11 16:32:59 +0100238 if (!PAGE_ALIGNED(vcpu_res->start)) {
Paolo Bonzini5d576862014-08-05 09:47:45 +0200239 kvm_err("GICV physical address 0x%llx not page aligned\n",
Julien Grall503a6282016-04-11 16:32:59 +0100240 (unsigned long long)vcpu_res->start);
Paolo Bonzini5d576862014-08-05 09:47:45 +0200241 ret = -ENXIO;
242 goto out_unmap;
243 }
244
Julien Grall503a6282016-04-11 16:32:59 +0100245 if (!PAGE_ALIGNED(resource_size(vcpu_res))) {
Paolo Bonzini5d576862014-08-05 09:47:45 +0200246 kvm_err("GICV size 0x%llx not a multiple of page size 0x%lx\n",
Julien Grall503a6282016-04-11 16:32:59 +0100247 (unsigned long long)resource_size(vcpu_res),
Paolo Bonzini5d576862014-08-05 09:47:45 +0200248 PAGE_SIZE);
249 ret = -ENXIO;
250 goto out_unmap;
251 }
252
Andre Przywarab5d84ff2014-06-03 10:26:03 +0200253 vgic->can_emulate_gicv2 = true;
Andre Przywaraea2f83a2014-10-26 23:17:00 +0000254 kvm_register_device_ops(&kvm_arm_vgic_v2_ops, KVM_DEV_TYPE_ARM_VGIC_V2);
255
Julien Grall503a6282016-04-11 16:32:59 +0100256 vgic->vcpu_base = vcpu_res->start;
Marc Zyngier8f186d52014-02-04 18:13:03 +0000257
Julien Grall503a6282016-04-11 16:32:59 +0100258 kvm_info("GICH base=0x%llx, GICV base=0x%llx, IRQ=%d\n",
259 gic_kvm_info->vctrl.start, vgic->vcpu_base, vgic->maint_irq);
Marc Zyngier8f186d52014-02-04 18:13:03 +0000260
Marc Zyngier1a9b1302013-06-21 11:57:56 +0100261 vgic->type = VGIC_V2;
Andre Przywara3caa2d82014-06-02 16:26:01 +0200262 vgic->max_gic_vcpus = VGIC_V2_MAX_CPUS;
Marc Zyngierd6400d72016-03-03 15:43:58 +0000263
264 on_each_cpu(vgic_cpu_init_lrs, vgic, 1);
265
Marc Zyngier8f186d52014-02-04 18:13:03 +0000266 *ops = &vgic_v2_ops;
267 *params = vgic;
268 goto out;
269
270out_unmap:
271 iounmap(vgic->vctrl_base);
272out:
Marc Zyngier8f186d52014-02-04 18:13:03 +0000273 return ret;
274}