2 * Copyright (C) 2012-2015 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
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.
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.
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/>.
18 #include <linux/compiler.h>
19 #include <linux/irqchip/arm-gic-v3.h>
20 #include <linux/kvm_host.h>
22 #include <asm/kvm_emulate.h>
23 #include <asm/kvm_hyp.h>
25 #define vtr_to_max_lr_idx(v) ((v) & 0xf)
26 #define vtr_to_nr_pre_bits(v) ((((u32)(v) >> 26) & 7) + 1)
28 static u64 __hyp_text
__gic_v3_get_lr(unsigned int lr
)
32 return read_gicreg(ICH_LR0_EL2
);
34 return read_gicreg(ICH_LR1_EL2
);
36 return read_gicreg(ICH_LR2_EL2
);
38 return read_gicreg(ICH_LR3_EL2
);
40 return read_gicreg(ICH_LR4_EL2
);
42 return read_gicreg(ICH_LR5_EL2
);
44 return read_gicreg(ICH_LR6_EL2
);
46 return read_gicreg(ICH_LR7_EL2
);
48 return read_gicreg(ICH_LR8_EL2
);
50 return read_gicreg(ICH_LR9_EL2
);
52 return read_gicreg(ICH_LR10_EL2
);
54 return read_gicreg(ICH_LR11_EL2
);
56 return read_gicreg(ICH_LR12_EL2
);
58 return read_gicreg(ICH_LR13_EL2
);
60 return read_gicreg(ICH_LR14_EL2
);
62 return read_gicreg(ICH_LR15_EL2
);
68 static void __hyp_text
__gic_v3_set_lr(u64 val
, int lr
)
72 write_gicreg(val
, ICH_LR0_EL2
);
75 write_gicreg(val
, ICH_LR1_EL2
);
78 write_gicreg(val
, ICH_LR2_EL2
);
81 write_gicreg(val
, ICH_LR3_EL2
);
84 write_gicreg(val
, ICH_LR4_EL2
);
87 write_gicreg(val
, ICH_LR5_EL2
);
90 write_gicreg(val
, ICH_LR6_EL2
);
93 write_gicreg(val
, ICH_LR7_EL2
);
96 write_gicreg(val
, ICH_LR8_EL2
);
99 write_gicreg(val
, ICH_LR9_EL2
);
102 write_gicreg(val
, ICH_LR10_EL2
);
105 write_gicreg(val
, ICH_LR11_EL2
);
108 write_gicreg(val
, ICH_LR12_EL2
);
111 write_gicreg(val
, ICH_LR13_EL2
);
114 write_gicreg(val
, ICH_LR14_EL2
);
117 write_gicreg(val
, ICH_LR15_EL2
);
122 static void __hyp_text
save_maint_int_state(struct kvm_vcpu
*vcpu
, int nr_lr
)
124 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
128 expect_mi
= !!(cpu_if
->vgic_hcr
& ICH_HCR_UIE
);
130 for (i
= 0; i
< nr_lr
; i
++) {
131 if (!(vcpu
->arch
.vgic_cpu
.live_lrs
& (1UL << i
)))
134 expect_mi
|= (!(cpu_if
->vgic_lr
[i
] & ICH_LR_HW
) &&
135 (cpu_if
->vgic_lr
[i
] & ICH_LR_EOI
));
139 cpu_if
->vgic_misr
= read_gicreg(ICH_MISR_EL2
);
141 if (cpu_if
->vgic_misr
& ICH_MISR_EOI
)
142 cpu_if
->vgic_eisr
= read_gicreg(ICH_EISR_EL2
);
144 cpu_if
->vgic_eisr
= 0;
146 cpu_if
->vgic_misr
= 0;
147 cpu_if
->vgic_eisr
= 0;
151 static void __hyp_text
__vgic_v3_write_ap0rn(u32 val
, int n
)
155 write_gicreg(val
, ICH_AP0R0_EL2
);
158 write_gicreg(val
, ICH_AP0R1_EL2
);
161 write_gicreg(val
, ICH_AP0R2_EL2
);
164 write_gicreg(val
, ICH_AP0R3_EL2
);
169 static void __hyp_text
__vgic_v3_write_ap1rn(u32 val
, int n
)
173 write_gicreg(val
, ICH_AP1R0_EL2
);
176 write_gicreg(val
, ICH_AP1R1_EL2
);
179 write_gicreg(val
, ICH_AP1R2_EL2
);
182 write_gicreg(val
, ICH_AP1R3_EL2
);
187 static u32 __hyp_text
__vgic_v3_read_ap0rn(int n
)
193 val
= read_gicreg(ICH_AP0R0_EL2
);
196 val
= read_gicreg(ICH_AP0R1_EL2
);
199 val
= read_gicreg(ICH_AP0R2_EL2
);
202 val
= read_gicreg(ICH_AP0R3_EL2
);
211 static u32 __hyp_text
__vgic_v3_read_ap1rn(int n
)
217 val
= read_gicreg(ICH_AP1R0_EL2
);
220 val
= read_gicreg(ICH_AP1R1_EL2
);
223 val
= read_gicreg(ICH_AP1R2_EL2
);
226 val
= read_gicreg(ICH_AP1R3_EL2
);
235 void __hyp_text
__vgic_v3_save_state(struct kvm_vcpu
*vcpu
)
237 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
241 * Make sure stores to the GIC via the memory mapped interface
242 * are now visible to the system register interface.
244 if (!cpu_if
->vgic_sre
)
247 cpu_if
->vgic_vmcr
= read_gicreg(ICH_VMCR_EL2
);
249 if (vcpu
->arch
.vgic_cpu
.live_lrs
) {
251 u32 max_lr_idx
, nr_pre_bits
;
253 cpu_if
->vgic_elrsr
= read_gicreg(ICH_ELSR_EL2
);
255 write_gicreg(0, ICH_HCR_EL2
);
256 val
= read_gicreg(ICH_VTR_EL2
);
257 max_lr_idx
= vtr_to_max_lr_idx(val
);
258 nr_pre_bits
= vtr_to_nr_pre_bits(val
);
260 save_maint_int_state(vcpu
, max_lr_idx
+ 1);
262 for (i
= 0; i
<= max_lr_idx
; i
++) {
263 if (!(vcpu
->arch
.vgic_cpu
.live_lrs
& (1UL << i
)))
266 if (cpu_if
->vgic_elrsr
& (1 << i
))
267 cpu_if
->vgic_lr
[i
] &= ~ICH_LR_STATE
;
269 cpu_if
->vgic_lr
[i
] = __gic_v3_get_lr(i
);
271 __gic_v3_set_lr(0, i
);
274 switch (nr_pre_bits
) {
276 cpu_if
->vgic_ap0r
[3] = __vgic_v3_read_ap0rn(3);
277 cpu_if
->vgic_ap0r
[2] = __vgic_v3_read_ap0rn(2);
279 cpu_if
->vgic_ap0r
[1] = __vgic_v3_read_ap0rn(1);
281 cpu_if
->vgic_ap0r
[0] = __vgic_v3_read_ap0rn(0);
284 switch (nr_pre_bits
) {
286 cpu_if
->vgic_ap1r
[3] = __vgic_v3_read_ap1rn(3);
287 cpu_if
->vgic_ap1r
[2] = __vgic_v3_read_ap1rn(2);
289 cpu_if
->vgic_ap1r
[1] = __vgic_v3_read_ap1rn(1);
291 cpu_if
->vgic_ap1r
[0] = __vgic_v3_read_ap1rn(0);
294 vcpu
->arch
.vgic_cpu
.live_lrs
= 0;
296 cpu_if
->vgic_misr
= 0;
297 cpu_if
->vgic_eisr
= 0;
298 cpu_if
->vgic_elrsr
= 0xffff;
299 cpu_if
->vgic_ap0r
[0] = 0;
300 cpu_if
->vgic_ap0r
[1] = 0;
301 cpu_if
->vgic_ap0r
[2] = 0;
302 cpu_if
->vgic_ap0r
[3] = 0;
303 cpu_if
->vgic_ap1r
[0] = 0;
304 cpu_if
->vgic_ap1r
[1] = 0;
305 cpu_if
->vgic_ap1r
[2] = 0;
306 cpu_if
->vgic_ap1r
[3] = 0;
309 val
= read_gicreg(ICC_SRE_EL2
);
310 write_gicreg(val
| ICC_SRE_EL2_ENABLE
, ICC_SRE_EL2
);
312 if (!cpu_if
->vgic_sre
) {
313 /* Make sure ENABLE is set at EL2 before setting SRE at EL1 */
315 write_gicreg(1, ICC_SRE_EL1
);
319 void __hyp_text
__vgic_v3_restore_state(struct kvm_vcpu
*vcpu
)
321 struct vgic_v3_cpu_if
*cpu_if
= &vcpu
->arch
.vgic_cpu
.vgic_v3
;
323 u32 max_lr_idx
, nr_pre_bits
;
328 * VFIQEn is RES1 if ICC_SRE_EL1.SRE is 1. This causes a
329 * Group0 interrupt (as generated in GICv2 mode) to be
330 * delivered as a FIQ to the guest, with potentially fatal
331 * consequences. So we must make sure that ICC_SRE_EL1 has
332 * been actually programmed with the value we want before
333 * starting to mess with the rest of the GIC.
335 if (!cpu_if
->vgic_sre
) {
336 write_gicreg(0, ICC_SRE_EL1
);
340 val
= read_gicreg(ICH_VTR_EL2
);
341 max_lr_idx
= vtr_to_max_lr_idx(val
);
342 nr_pre_bits
= vtr_to_nr_pre_bits(val
);
344 for (i
= 0; i
<= max_lr_idx
; i
++) {
345 if (cpu_if
->vgic_lr
[i
] & ICH_LR_STATE
)
346 live_lrs
|= (1 << i
);
349 write_gicreg(cpu_if
->vgic_vmcr
, ICH_VMCR_EL2
);
352 write_gicreg(cpu_if
->vgic_hcr
, ICH_HCR_EL2
);
354 switch (nr_pre_bits
) {
356 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[3], 3);
357 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[2], 2);
359 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[1], 1);
361 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[0], 0);
364 switch (nr_pre_bits
) {
366 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[3], 3);
367 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[2], 2);
369 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[1], 1);
371 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[0], 0);
374 for (i
= 0; i
<= max_lr_idx
; i
++) {
375 if (!(live_lrs
& (1 << i
)))
378 __gic_v3_set_lr(cpu_if
->vgic_lr
[i
], i
);
383 * Ensures that the above will have reached the
384 * (re)distributors. This ensure the guest will read the
385 * correct values from the memory-mapped interface.
387 if (!cpu_if
->vgic_sre
) {
391 vcpu
->arch
.vgic_cpu
.live_lrs
= live_lrs
;
394 * Prevent the guest from touching the GIC system registers if
395 * SRE isn't enabled for GICv3 emulation.
397 write_gicreg(read_gicreg(ICC_SRE_EL2
) & ~ICC_SRE_EL2_ENABLE
,
401 void __hyp_text
__vgic_v3_init_lrs(void)
403 int max_lr_idx
= vtr_to_max_lr_idx(read_gicreg(ICH_VTR_EL2
));
406 for (i
= 0; i
<= max_lr_idx
; i
++)
407 __gic_v3_set_lr(0, i
);
410 u64 __hyp_text
__vgic_v3_get_ich_vtr_el2(void)
412 return read_gicreg(ICH_VTR_EL2
);
415 u64 __hyp_text
__vgic_v3_read_vmcr(void)
417 return read_gicreg(ICH_VMCR_EL2
);
420 void __hyp_text
__vgic_v3_write_vmcr(u32 vmcr
)
422 write_gicreg(vmcr
, ICH_VMCR_EL2
);
427 static int __hyp_text
__vgic_v3_bpr_min(void)
429 /* See Pseudocode for VPriorityGroup */
430 return 8 - vtr_to_nr_pre_bits(read_gicreg(ICH_VTR_EL2
));
433 static unsigned int __hyp_text
__vgic_v3_get_bpr0(u32 vmcr
)
435 return (vmcr
& ICH_VMCR_BPR0_MASK
) >> ICH_VMCR_BPR0_SHIFT
;
438 static unsigned int __hyp_text
__vgic_v3_get_bpr1(u32 vmcr
)
442 if (vmcr
& ICH_VMCR_CBPR_MASK
) {
443 bpr
= __vgic_v3_get_bpr0(vmcr
);
447 bpr
= (vmcr
& ICH_VMCR_BPR1_MASK
) >> ICH_VMCR_BPR1_SHIFT
;
453 static void __hyp_text
__vgic_v3_read_igrpen1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
455 vcpu_set_reg(vcpu
, rt
, !!(vmcr
& ICH_VMCR_ENG1_MASK
));
458 static void __hyp_text
__vgic_v3_write_igrpen1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
460 u64 val
= vcpu_get_reg(vcpu
, rt
);
463 vmcr
|= ICH_VMCR_ENG1_MASK
;
465 vmcr
&= ~ICH_VMCR_ENG1_MASK
;
467 __vgic_v3_write_vmcr(vmcr
);
470 static void __hyp_text
__vgic_v3_read_bpr1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
472 vcpu_set_reg(vcpu
, rt
, __vgic_v3_get_bpr1(vmcr
));
475 static void __hyp_text
__vgic_v3_write_bpr1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
477 u64 val
= vcpu_get_reg(vcpu
, rt
);
478 u8 bpr_min
= __vgic_v3_bpr_min();
480 if (vmcr
& ICH_VMCR_CBPR_MASK
)
483 /* Enforce BPR limiting */
487 val
<<= ICH_VMCR_BPR1_SHIFT
;
488 val
&= ICH_VMCR_BPR1_MASK
;
489 vmcr
&= ~ICH_VMCR_BPR1_MASK
;
492 __vgic_v3_write_vmcr(vmcr
);
495 int __hyp_text
__vgic_v3_perform_cpuif_access(struct kvm_vcpu
*vcpu
)
500 void (*fn
)(struct kvm_vcpu
*, u32
, int);
504 esr
= kvm_vcpu_get_hsr(vcpu
);
505 if (vcpu_mode_is_32bit(vcpu
)) {
506 if (!kvm_condition_valid(vcpu
))
509 sysreg
= esr_cp15_to_sysreg(esr
);
511 sysreg
= esr_sys64_to_sysreg(esr
);
514 is_read
= (esr
& ESR_ELx_SYS64_ISS_DIR_MASK
) == ESR_ELx_SYS64_ISS_DIR_READ
;
519 fn
= __vgic_v3_read_igrpen1
;
521 fn
= __vgic_v3_write_igrpen1
;
525 fn
= __vgic_v3_read_bpr1
;
527 fn
= __vgic_v3_write_bpr1
;
533 vmcr
= __vgic_v3_read_vmcr();
534 rt
= kvm_vcpu_sys_get_rt(vcpu
);