Linux 4.13.16
[linux/fpc-iii.git] / arch / arm64 / kvm / vgic-sys-reg-v3.c
blob116786d2e8e8fdbe7c2dfc4056b87918e4bf25bf
1 /*
2 * VGIC system registers handling functions for AArch64 mode
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.
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.
14 #include <linux/irqchip/arm-gic-v3.h>
15 #include <linux/kvm.h>
16 #include <linux/kvm_host.h>
17 #include <asm/kvm_emulate.h>
18 #include "vgic.h"
19 #include "sys_regs.h"
21 static bool access_gic_ctlr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
22 const struct sys_reg_desc *r)
24 u32 host_pri_bits, host_id_bits, host_seis, host_a3v, seis, a3v;
25 struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
26 struct vgic_vmcr vmcr;
27 u64 val;
29 vgic_get_vmcr(vcpu, &vmcr);
30 if (p->is_write) {
31 val = p->regval;
34 * Disallow restoring VM state if not supported by this
35 * hardware.
37 host_pri_bits = ((val & ICC_CTLR_EL1_PRI_BITS_MASK) >>
38 ICC_CTLR_EL1_PRI_BITS_SHIFT) + 1;
39 if (host_pri_bits > vgic_v3_cpu->num_pri_bits)
40 return false;
42 vgic_v3_cpu->num_pri_bits = host_pri_bits;
44 host_id_bits = (val & ICC_CTLR_EL1_ID_BITS_MASK) >>
45 ICC_CTLR_EL1_ID_BITS_SHIFT;
46 if (host_id_bits > vgic_v3_cpu->num_id_bits)
47 return false;
49 vgic_v3_cpu->num_id_bits = host_id_bits;
51 host_seis = ((kvm_vgic_global_state.ich_vtr_el2 &
52 ICH_VTR_SEIS_MASK) >> ICH_VTR_SEIS_SHIFT);
53 seis = (val & ICC_CTLR_EL1_SEIS_MASK) >>
54 ICC_CTLR_EL1_SEIS_SHIFT;
55 if (host_seis != seis)
56 return false;
58 host_a3v = ((kvm_vgic_global_state.ich_vtr_el2 &
59 ICH_VTR_A3V_MASK) >> ICH_VTR_A3V_SHIFT);
60 a3v = (val & ICC_CTLR_EL1_A3V_MASK) >> ICC_CTLR_EL1_A3V_SHIFT;
61 if (host_a3v != a3v)
62 return false;
65 * Here set VMCR.CTLR in ICC_CTLR_EL1 layout.
66 * The vgic_set_vmcr() will convert to ICH_VMCR layout.
68 vmcr.cbpr = (val & ICC_CTLR_EL1_CBPR_MASK) >> ICC_CTLR_EL1_CBPR_SHIFT;
69 vmcr.eoim = (val & ICC_CTLR_EL1_EOImode_MASK) >> ICC_CTLR_EL1_EOImode_SHIFT;
70 vgic_set_vmcr(vcpu, &vmcr);
71 } else {
72 val = 0;
73 val |= (vgic_v3_cpu->num_pri_bits - 1) <<
74 ICC_CTLR_EL1_PRI_BITS_SHIFT;
75 val |= vgic_v3_cpu->num_id_bits << ICC_CTLR_EL1_ID_BITS_SHIFT;
76 val |= ((kvm_vgic_global_state.ich_vtr_el2 &
77 ICH_VTR_SEIS_MASK) >> ICH_VTR_SEIS_SHIFT) <<
78 ICC_CTLR_EL1_SEIS_SHIFT;
79 val |= ((kvm_vgic_global_state.ich_vtr_el2 &
80 ICH_VTR_A3V_MASK) >> ICH_VTR_A3V_SHIFT) <<
81 ICC_CTLR_EL1_A3V_SHIFT;
83 * The VMCR.CTLR value is in ICC_CTLR_EL1 layout.
84 * Extract it directly using ICC_CTLR_EL1 reg definitions.
86 val |= (vmcr.cbpr << ICC_CTLR_EL1_CBPR_SHIFT) & ICC_CTLR_EL1_CBPR_MASK;
87 val |= (vmcr.eoim << ICC_CTLR_EL1_EOImode_SHIFT) & ICC_CTLR_EL1_EOImode_MASK;
89 p->regval = val;
92 return true;
95 static bool access_gic_pmr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
96 const struct sys_reg_desc *r)
98 struct vgic_vmcr vmcr;
100 vgic_get_vmcr(vcpu, &vmcr);
101 if (p->is_write) {
102 vmcr.pmr = (p->regval & ICC_PMR_EL1_MASK) >> ICC_PMR_EL1_SHIFT;
103 vgic_set_vmcr(vcpu, &vmcr);
104 } else {
105 p->regval = (vmcr.pmr << ICC_PMR_EL1_SHIFT) & ICC_PMR_EL1_MASK;
108 return true;
111 static bool access_gic_bpr0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
112 const struct sys_reg_desc *r)
114 struct vgic_vmcr vmcr;
116 vgic_get_vmcr(vcpu, &vmcr);
117 if (p->is_write) {
118 vmcr.bpr = (p->regval & ICC_BPR0_EL1_MASK) >>
119 ICC_BPR0_EL1_SHIFT;
120 vgic_set_vmcr(vcpu, &vmcr);
121 } else {
122 p->regval = (vmcr.bpr << ICC_BPR0_EL1_SHIFT) &
123 ICC_BPR0_EL1_MASK;
126 return true;
129 static bool access_gic_bpr1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
130 const struct sys_reg_desc *r)
132 struct vgic_vmcr vmcr;
134 if (!p->is_write)
135 p->regval = 0;
137 vgic_get_vmcr(vcpu, &vmcr);
138 if (!vmcr.cbpr) {
139 if (p->is_write) {
140 vmcr.abpr = (p->regval & ICC_BPR1_EL1_MASK) >>
141 ICC_BPR1_EL1_SHIFT;
142 vgic_set_vmcr(vcpu, &vmcr);
143 } else {
144 p->regval = (vmcr.abpr << ICC_BPR1_EL1_SHIFT) &
145 ICC_BPR1_EL1_MASK;
147 } else {
148 if (!p->is_write)
149 p->regval = min((vmcr.bpr + 1), 7U);
152 return true;
155 static bool access_gic_grpen0(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
156 const struct sys_reg_desc *r)
158 struct vgic_vmcr vmcr;
160 vgic_get_vmcr(vcpu, &vmcr);
161 if (p->is_write) {
162 vmcr.grpen0 = (p->regval & ICC_IGRPEN0_EL1_MASK) >>
163 ICC_IGRPEN0_EL1_SHIFT;
164 vgic_set_vmcr(vcpu, &vmcr);
165 } else {
166 p->regval = (vmcr.grpen0 << ICC_IGRPEN0_EL1_SHIFT) &
167 ICC_IGRPEN0_EL1_MASK;
170 return true;
173 static bool access_gic_grpen1(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
174 const struct sys_reg_desc *r)
176 struct vgic_vmcr vmcr;
178 vgic_get_vmcr(vcpu, &vmcr);
179 if (p->is_write) {
180 vmcr.grpen1 = (p->regval & ICC_IGRPEN1_EL1_MASK) >>
181 ICC_IGRPEN1_EL1_SHIFT;
182 vgic_set_vmcr(vcpu, &vmcr);
183 } else {
184 p->regval = (vmcr.grpen1 << ICC_IGRPEN1_EL1_SHIFT) &
185 ICC_IGRPEN1_EL1_MASK;
188 return true;
191 static void vgic_v3_access_apr_reg(struct kvm_vcpu *vcpu,
192 struct sys_reg_params *p, u8 apr, u8 idx)
194 struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3;
195 uint32_t *ap_reg;
197 if (apr)
198 ap_reg = &vgicv3->vgic_ap1r[idx];
199 else
200 ap_reg = &vgicv3->vgic_ap0r[idx];
202 if (p->is_write)
203 *ap_reg = p->regval;
204 else
205 p->regval = *ap_reg;
208 static bool access_gic_aprn(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
209 const struct sys_reg_desc *r, u8 apr)
211 struct vgic_cpu *vgic_v3_cpu = &vcpu->arch.vgic_cpu;
212 u8 idx = r->Op2 & 3;
215 * num_pri_bits are initialized with HW supported values.
216 * We can rely safely on num_pri_bits even if VM has not
217 * restored ICC_CTLR_EL1 before restoring APnR registers.
219 switch (vgic_v3_cpu->num_pri_bits) {
220 case 7:
221 vgic_v3_access_apr_reg(vcpu, p, apr, idx);
222 break;
223 case 6:
224 if (idx > 1)
225 goto err;
226 vgic_v3_access_apr_reg(vcpu, p, apr, idx);
227 break;
228 default:
229 if (idx > 0)
230 goto err;
231 vgic_v3_access_apr_reg(vcpu, p, apr, idx);
234 return true;
235 err:
236 if (!p->is_write)
237 p->regval = 0;
239 return false;
242 static bool access_gic_ap0r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
243 const struct sys_reg_desc *r)
246 return access_gic_aprn(vcpu, p, r, 0);
249 static bool access_gic_ap1r(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
250 const struct sys_reg_desc *r)
252 return access_gic_aprn(vcpu, p, r, 1);
255 static bool access_gic_sre(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
256 const struct sys_reg_desc *r)
258 struct vgic_v3_cpu_if *vgicv3 = &vcpu->arch.vgic_cpu.vgic_v3;
260 /* Validate SRE bit */
261 if (p->is_write) {
262 if (!(p->regval & ICC_SRE_EL1_SRE))
263 return false;
264 } else {
265 p->regval = vgicv3->vgic_sre;
268 return true;
270 static const struct sys_reg_desc gic_v3_icc_reg_descs[] = {
271 { SYS_DESC(SYS_ICC_PMR_EL1), access_gic_pmr },
272 { SYS_DESC(SYS_ICC_BPR0_EL1), access_gic_bpr0 },
273 { SYS_DESC(SYS_ICC_AP0R0_EL1), access_gic_ap0r },
274 { SYS_DESC(SYS_ICC_AP0R1_EL1), access_gic_ap0r },
275 { SYS_DESC(SYS_ICC_AP0R2_EL1), access_gic_ap0r },
276 { SYS_DESC(SYS_ICC_AP0R3_EL1), access_gic_ap0r },
277 { SYS_DESC(SYS_ICC_AP1R0_EL1), access_gic_ap1r },
278 { SYS_DESC(SYS_ICC_AP1R1_EL1), access_gic_ap1r },
279 { SYS_DESC(SYS_ICC_AP1R2_EL1), access_gic_ap1r },
280 { SYS_DESC(SYS_ICC_AP1R3_EL1), access_gic_ap1r },
281 { SYS_DESC(SYS_ICC_BPR1_EL1), access_gic_bpr1 },
282 { SYS_DESC(SYS_ICC_CTLR_EL1), access_gic_ctlr },
283 { SYS_DESC(SYS_ICC_SRE_EL1), access_gic_sre },
284 { SYS_DESC(SYS_ICC_IGRPEN0_EL1), access_gic_grpen0 },
285 { SYS_DESC(SYS_ICC_IGRPEN1_EL1), access_gic_grpen1 },
288 int vgic_v3_has_cpu_sysregs_attr(struct kvm_vcpu *vcpu, bool is_write, u64 id,
289 u64 *reg)
291 struct sys_reg_params params;
292 u64 sysreg = (id & KVM_DEV_ARM_VGIC_SYSREG_MASK) | KVM_REG_SIZE_U64;
294 params.regval = *reg;
295 params.is_write = is_write;
296 params.is_aarch32 = false;
297 params.is_32bit = false;
299 if (find_reg_by_id(sysreg, &params, gic_v3_icc_reg_descs,
300 ARRAY_SIZE(gic_v3_icc_reg_descs)))
301 return 0;
303 return -ENXIO;
306 int vgic_v3_cpu_sysregs_uaccess(struct kvm_vcpu *vcpu, bool is_write, u64 id,
307 u64 *reg)
309 struct sys_reg_params params;
310 const struct sys_reg_desc *r;
311 u64 sysreg = (id & KVM_DEV_ARM_VGIC_SYSREG_MASK) | KVM_REG_SIZE_U64;
313 if (is_write)
314 params.regval = *reg;
315 params.is_write = is_write;
316 params.is_aarch32 = false;
317 params.is_32bit = false;
319 r = find_reg_by_id(sysreg, &params, gic_v3_icc_reg_descs,
320 ARRAY_SIZE(gic_v3_icc_reg_descs));
321 if (!r)
322 return -ENXIO;
324 if (!r->access(vcpu, &params, r))
325 return -EINVAL;
327 if (!is_write)
328 *reg = params.regval;
330 return 0;