1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012-2015 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #include <hyp/adjust_pc.h>
9 #include <linux/compiler.h>
10 #include <linux/irqchip/arm-gic-v3.h>
11 #include <linux/kvm_host.h>
13 #include <asm/kvm_emulate.h>
14 #include <asm/kvm_hyp.h>
15 #include <asm/kvm_mmu.h>
17 #define vtr_to_max_lr_idx(v) ((v) & 0xf)
18 #define vtr_to_nr_pre_bits(v) ((((u32)(v) >> 26) & 7) + 1)
19 #define vtr_to_nr_apr_regs(v) (1 << (vtr_to_nr_pre_bits(v) - 5))
21 static u64
__gic_v3_get_lr(unsigned int lr
)
25 return read_gicreg(ICH_LR0_EL2
);
27 return read_gicreg(ICH_LR1_EL2
);
29 return read_gicreg(ICH_LR2_EL2
);
31 return read_gicreg(ICH_LR3_EL2
);
33 return read_gicreg(ICH_LR4_EL2
);
35 return read_gicreg(ICH_LR5_EL2
);
37 return read_gicreg(ICH_LR6_EL2
);
39 return read_gicreg(ICH_LR7_EL2
);
41 return read_gicreg(ICH_LR8_EL2
);
43 return read_gicreg(ICH_LR9_EL2
);
45 return read_gicreg(ICH_LR10_EL2
);
47 return read_gicreg(ICH_LR11_EL2
);
49 return read_gicreg(ICH_LR12_EL2
);
51 return read_gicreg(ICH_LR13_EL2
);
53 return read_gicreg(ICH_LR14_EL2
);
55 return read_gicreg(ICH_LR15_EL2
);
61 static void __gic_v3_set_lr(u64 val
, int lr
)
65 write_gicreg(val
, ICH_LR0_EL2
);
68 write_gicreg(val
, ICH_LR1_EL2
);
71 write_gicreg(val
, ICH_LR2_EL2
);
74 write_gicreg(val
, ICH_LR3_EL2
);
77 write_gicreg(val
, ICH_LR4_EL2
);
80 write_gicreg(val
, ICH_LR5_EL2
);
83 write_gicreg(val
, ICH_LR6_EL2
);
86 write_gicreg(val
, ICH_LR7_EL2
);
89 write_gicreg(val
, ICH_LR8_EL2
);
92 write_gicreg(val
, ICH_LR9_EL2
);
95 write_gicreg(val
, ICH_LR10_EL2
);
98 write_gicreg(val
, ICH_LR11_EL2
);
101 write_gicreg(val
, ICH_LR12_EL2
);
104 write_gicreg(val
, ICH_LR13_EL2
);
107 write_gicreg(val
, ICH_LR14_EL2
);
110 write_gicreg(val
, ICH_LR15_EL2
);
115 static void __vgic_v3_write_ap0rn(u32 val
, int n
)
119 write_gicreg(val
, ICH_AP0R0_EL2
);
122 write_gicreg(val
, ICH_AP0R1_EL2
);
125 write_gicreg(val
, ICH_AP0R2_EL2
);
128 write_gicreg(val
, ICH_AP0R3_EL2
);
133 static void __vgic_v3_write_ap1rn(u32 val
, int n
)
137 write_gicreg(val
, ICH_AP1R0_EL2
);
140 write_gicreg(val
, ICH_AP1R1_EL2
);
143 write_gicreg(val
, ICH_AP1R2_EL2
);
146 write_gicreg(val
, ICH_AP1R3_EL2
);
151 static u32
__vgic_v3_read_ap0rn(int n
)
157 val
= read_gicreg(ICH_AP0R0_EL2
);
160 val
= read_gicreg(ICH_AP0R1_EL2
);
163 val
= read_gicreg(ICH_AP0R2_EL2
);
166 val
= read_gicreg(ICH_AP0R3_EL2
);
175 static u32
__vgic_v3_read_ap1rn(int n
)
181 val
= read_gicreg(ICH_AP1R0_EL2
);
184 val
= read_gicreg(ICH_AP1R1_EL2
);
187 val
= read_gicreg(ICH_AP1R2_EL2
);
190 val
= read_gicreg(ICH_AP1R3_EL2
);
199 void __vgic_v3_save_state(struct vgic_v3_cpu_if
*cpu_if
)
201 u64 used_lrs
= cpu_if
->used_lrs
;
204 * Make sure stores to the GIC via the memory mapped interface
205 * are now visible to the system register interface when reading the
206 * LRs, and when reading back the VMCR on non-VHE systems.
208 if (used_lrs
|| !has_vhe()) {
209 if (!cpu_if
->vgic_sre
) {
215 if (used_lrs
|| cpu_if
->its_vpe
.its_vm
) {
219 elrsr
= read_gicreg(ICH_ELRSR_EL2
);
221 write_gicreg(cpu_if
->vgic_hcr
& ~ICH_HCR_EN
, ICH_HCR_EL2
);
223 for (i
= 0; i
< used_lrs
; i
++) {
224 if (elrsr
& (1 << i
))
225 cpu_if
->vgic_lr
[i
] &= ~ICH_LR_STATE
;
227 cpu_if
->vgic_lr
[i
] = __gic_v3_get_lr(i
);
229 __gic_v3_set_lr(0, i
);
234 void __vgic_v3_restore_state(struct vgic_v3_cpu_if
*cpu_if
)
236 u64 used_lrs
= cpu_if
->used_lrs
;
239 if (used_lrs
|| cpu_if
->its_vpe
.its_vm
) {
240 write_gicreg(cpu_if
->vgic_hcr
, ICH_HCR_EL2
);
242 for (i
= 0; i
< used_lrs
; i
++)
243 __gic_v3_set_lr(cpu_if
->vgic_lr
[i
], i
);
247 * Ensure that writes to the LRs, and on non-VHE systems ensure that
248 * the write to the VMCR in __vgic_v3_activate_traps(), will have
249 * reached the (re)distributors. This ensure the guest will read the
250 * correct values from the memory-mapped interface.
252 if (used_lrs
|| !has_vhe()) {
253 if (!cpu_if
->vgic_sre
) {
260 void __vgic_v3_activate_traps(struct vgic_v3_cpu_if
*cpu_if
)
263 * VFIQEn is RES1 if ICC_SRE_EL1.SRE is 1. This causes a
264 * Group0 interrupt (as generated in GICv2 mode) to be
265 * delivered as a FIQ to the guest, with potentially fatal
266 * consequences. So we must make sure that ICC_SRE_EL1 has
267 * been actually programmed with the value we want before
268 * starting to mess with the rest of the GIC, and VMCR_EL2 in
269 * particular. This logic must be called before
270 * __vgic_v3_restore_state().
272 * However, if the vgic is disabled (ICH_HCR_EL2.EN==0), no GIC is
273 * provisioned at all. In order to prevent illegal accesses to the
274 * system registers to trap to EL1 (duh), force ICC_SRE_EL1.SRE to 1
275 * so that the trap bits can take effect. Yes, we *loves* the GIC.
277 if (!(cpu_if
->vgic_hcr
& ICH_HCR_EN
)) {
278 write_gicreg(ICC_SRE_EL1_SRE
, ICC_SRE_EL1
);
280 } else if (!cpu_if
->vgic_sre
) {
281 write_gicreg(0, ICC_SRE_EL1
);
283 write_gicreg(cpu_if
->vgic_vmcr
, ICH_VMCR_EL2
);
288 * Ensure that the write to the VMCR will have reached
289 * the (re)distributors. This ensure the guest will
290 * read the correct values from the memory-mapped
299 * Prevent the guest from touching the ICC_SRE_EL1 system
300 * register. Note that this may not have any effect, as
301 * ICC_SRE_EL2.Enable being RAO/WI is a valid implementation.
303 write_gicreg(read_gicreg(ICC_SRE_EL2
) & ~ICC_SRE_EL2_ENABLE
,
307 * If we need to trap system registers, we must write
308 * ICH_HCR_EL2 anyway, even if no interrupts are being
309 * injected. Note that this also applies if we don't expect
310 * any system register access (no vgic at all).
312 if (static_branch_unlikely(&vgic_v3_cpuif_trap
) ||
313 cpu_if
->its_vpe
.its_vm
|| !cpu_if
->vgic_sre
)
314 write_gicreg(cpu_if
->vgic_hcr
, ICH_HCR_EL2
);
317 void __vgic_v3_deactivate_traps(struct vgic_v3_cpu_if
*cpu_if
)
321 if (!cpu_if
->vgic_sre
) {
322 cpu_if
->vgic_vmcr
= read_gicreg(ICH_VMCR_EL2
);
325 val
= read_gicreg(ICC_SRE_EL2
);
326 write_gicreg(val
| ICC_SRE_EL2_ENABLE
, ICC_SRE_EL2
);
328 if (!cpu_if
->vgic_sre
) {
329 /* Make sure ENABLE is set at EL2 before setting SRE at EL1 */
331 write_gicreg(1, ICC_SRE_EL1
);
335 * If we were trapping system registers, we enabled the VGIC even if
336 * no interrupts were being injected, and we disable it again here.
338 if (static_branch_unlikely(&vgic_v3_cpuif_trap
) ||
339 cpu_if
->its_vpe
.its_vm
|| !cpu_if
->vgic_sre
)
340 write_gicreg(0, ICH_HCR_EL2
);
343 static void __vgic_v3_save_aprs(struct vgic_v3_cpu_if
*cpu_if
)
348 val
= read_gicreg(ICH_VTR_EL2
);
349 nr_pre_bits
= vtr_to_nr_pre_bits(val
);
351 switch (nr_pre_bits
) {
353 cpu_if
->vgic_ap0r
[3] = __vgic_v3_read_ap0rn(3);
354 cpu_if
->vgic_ap0r
[2] = __vgic_v3_read_ap0rn(2);
357 cpu_if
->vgic_ap0r
[1] = __vgic_v3_read_ap0rn(1);
360 cpu_if
->vgic_ap0r
[0] = __vgic_v3_read_ap0rn(0);
363 switch (nr_pre_bits
) {
365 cpu_if
->vgic_ap1r
[3] = __vgic_v3_read_ap1rn(3);
366 cpu_if
->vgic_ap1r
[2] = __vgic_v3_read_ap1rn(2);
369 cpu_if
->vgic_ap1r
[1] = __vgic_v3_read_ap1rn(1);
372 cpu_if
->vgic_ap1r
[0] = __vgic_v3_read_ap1rn(0);
376 static void __vgic_v3_restore_aprs(struct vgic_v3_cpu_if
*cpu_if
)
381 val
= read_gicreg(ICH_VTR_EL2
);
382 nr_pre_bits
= vtr_to_nr_pre_bits(val
);
384 switch (nr_pre_bits
) {
386 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[3], 3);
387 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[2], 2);
390 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[1], 1);
393 __vgic_v3_write_ap0rn(cpu_if
->vgic_ap0r
[0], 0);
396 switch (nr_pre_bits
) {
398 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[3], 3);
399 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[2], 2);
402 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[1], 1);
405 __vgic_v3_write_ap1rn(cpu_if
->vgic_ap1r
[0], 0);
409 void __vgic_v3_init_lrs(void)
411 int max_lr_idx
= vtr_to_max_lr_idx(read_gicreg(ICH_VTR_EL2
));
414 for (i
= 0; i
<= max_lr_idx
; i
++)
415 __gic_v3_set_lr(0, i
);
419 * Return the GIC CPU configuration:
420 * - [31:0] ICH_VTR_EL2
422 * - [63] MMIO (GICv2) capable
424 u64
__vgic_v3_get_gic_config(void)
426 u64 val
, sre
= read_gicreg(ICC_SRE_EL1
);
427 unsigned long flags
= 0;
430 * To check whether we have a MMIO-based (GICv2 compatible)
431 * CPU interface, we need to disable the system register
432 * view. To do that safely, we have to prevent any interrupt
433 * from firing (which would be deadly).
435 * Note that this only makes sense on VHE, as interrupts are
436 * already masked for nVHE as part of the exception entry to
440 flags
= local_daif_save();
443 * Table 11-2 "Permitted ICC_SRE_ELx.SRE settings" indicates
444 * that to be able to set ICC_SRE_EL1.SRE to 0, all the
445 * interrupt overrides must be set. You've got to love this.
447 sysreg_clear_set(hcr_el2
, 0, HCR_AMO
| HCR_FMO
| HCR_IMO
);
449 write_gicreg(0, ICC_SRE_EL1
);
452 val
= read_gicreg(ICC_SRE_EL1
);
454 write_gicreg(sre
, ICC_SRE_EL1
);
456 sysreg_clear_set(hcr_el2
, HCR_AMO
| HCR_FMO
| HCR_IMO
, 0);
460 local_daif_restore(flags
);
462 val
= (val
& ICC_SRE_EL1_SRE
) ? 0 : (1ULL << 63);
463 val
|= read_gicreg(ICH_VTR_EL2
);
468 static u64
__vgic_v3_read_vmcr(void)
470 return read_gicreg(ICH_VMCR_EL2
);
473 static void __vgic_v3_write_vmcr(u32 vmcr
)
475 write_gicreg(vmcr
, ICH_VMCR_EL2
);
478 void __vgic_v3_save_vmcr_aprs(struct vgic_v3_cpu_if
*cpu_if
)
480 __vgic_v3_save_aprs(cpu_if
);
481 if (cpu_if
->vgic_sre
)
482 cpu_if
->vgic_vmcr
= __vgic_v3_read_vmcr();
485 void __vgic_v3_restore_vmcr_aprs(struct vgic_v3_cpu_if
*cpu_if
)
488 * If dealing with a GICv2 emulation on GICv3, VMCR_EL2.VFIQen
489 * is dependent on ICC_SRE_EL1.SRE, and we have to perform the
490 * VMCR_EL2 save/restore in the world switch.
492 if (cpu_if
->vgic_sre
)
493 __vgic_v3_write_vmcr(cpu_if
->vgic_vmcr
);
494 __vgic_v3_restore_aprs(cpu_if
);
497 static int __vgic_v3_bpr_min(void)
499 /* See Pseudocode for VPriorityGroup */
500 return 8 - vtr_to_nr_pre_bits(read_gicreg(ICH_VTR_EL2
));
503 static int __vgic_v3_get_group(struct kvm_vcpu
*vcpu
)
505 u64 esr
= kvm_vcpu_get_esr(vcpu
);
506 u8 crm
= (esr
& ESR_ELx_SYS64_ISS_CRM_MASK
) >> ESR_ELx_SYS64_ISS_CRM_SHIFT
;
511 #define GICv3_IDLE_PRIORITY 0xff
513 static int __vgic_v3_highest_priority_lr(struct kvm_vcpu
*vcpu
, u32 vmcr
,
516 unsigned int used_lrs
= vcpu
->arch
.vgic_cpu
.vgic_v3
.used_lrs
;
517 u8 priority
= GICv3_IDLE_PRIORITY
;
520 for (i
= 0; i
< used_lrs
; i
++) {
521 u64 val
= __gic_v3_get_lr(i
);
522 u8 lr_prio
= (val
& ICH_LR_PRIORITY_MASK
) >> ICH_LR_PRIORITY_SHIFT
;
524 /* Not pending in the state? */
525 if ((val
& ICH_LR_STATE
) != ICH_LR_PENDING_BIT
)
528 /* Group-0 interrupt, but Group-0 disabled? */
529 if (!(val
& ICH_LR_GROUP
) && !(vmcr
& ICH_VMCR_ENG0_MASK
))
532 /* Group-1 interrupt, but Group-1 disabled? */
533 if ((val
& ICH_LR_GROUP
) && !(vmcr
& ICH_VMCR_ENG1_MASK
))
536 /* Not the highest priority? */
537 if (lr_prio
>= priority
)
540 /* This is a candidate */
547 *lr_val
= ICC_IAR1_EL1_SPURIOUS
;
552 static int __vgic_v3_find_active_lr(struct kvm_vcpu
*vcpu
, int intid
,
555 unsigned int used_lrs
= vcpu
->arch
.vgic_cpu
.vgic_v3
.used_lrs
;
558 for (i
= 0; i
< used_lrs
; i
++) {
559 u64 val
= __gic_v3_get_lr(i
);
561 if ((val
& ICH_LR_VIRTUAL_ID_MASK
) == intid
&&
562 (val
& ICH_LR_ACTIVE_BIT
)) {
568 *lr_val
= ICC_IAR1_EL1_SPURIOUS
;
572 static int __vgic_v3_get_highest_active_priority(void)
574 u8 nr_apr_regs
= vtr_to_nr_apr_regs(read_gicreg(ICH_VTR_EL2
));
578 for (i
= 0; i
< nr_apr_regs
; i
++) {
582 * The ICH_AP0Rn_EL2 and ICH_AP1Rn_EL2 registers
583 * contain the active priority levels for this VCPU
584 * for the maximum number of supported priority
585 * levels, and we return the full priority level only
586 * if the BPR is programmed to its minimum, otherwise
587 * we return a combination of the priority level and
588 * subpriority, as determined by the setting of the
589 * BPR, but without the full subpriority.
591 val
= __vgic_v3_read_ap0rn(i
);
592 val
|= __vgic_v3_read_ap1rn(i
);
598 return (hap
+ __ffs(val
)) << __vgic_v3_bpr_min();
601 return GICv3_IDLE_PRIORITY
;
604 static unsigned int __vgic_v3_get_bpr0(u32 vmcr
)
606 return (vmcr
& ICH_VMCR_BPR0_MASK
) >> ICH_VMCR_BPR0_SHIFT
;
609 static unsigned int __vgic_v3_get_bpr1(u32 vmcr
)
613 if (vmcr
& ICH_VMCR_CBPR_MASK
) {
614 bpr
= __vgic_v3_get_bpr0(vmcr
);
618 bpr
= (vmcr
& ICH_VMCR_BPR1_MASK
) >> ICH_VMCR_BPR1_SHIFT
;
625 * Convert a priority to a preemption level, taking the relevant BPR
626 * into account by zeroing the sub-priority bits.
628 static u8
__vgic_v3_pri_to_pre(u8 pri
, u32 vmcr
, int grp
)
633 bpr
= __vgic_v3_get_bpr0(vmcr
) + 1;
635 bpr
= __vgic_v3_get_bpr1(vmcr
);
637 return pri
& (GENMASK(7, 0) << bpr
);
641 * The priority value is independent of any of the BPR values, so we
642 * normalize it using the minimal BPR value. This guarantees that no
643 * matter what the guest does with its BPR, we can always set/get the
644 * same value of a priority.
646 static void __vgic_v3_set_active_priority(u8 pri
, u32 vmcr
, int grp
)
652 pre
= __vgic_v3_pri_to_pre(pri
, vmcr
, grp
);
653 ap
= pre
>> __vgic_v3_bpr_min();
657 val
= __vgic_v3_read_ap0rn(apr
);
658 __vgic_v3_write_ap0rn(val
| BIT(ap
% 32), apr
);
660 val
= __vgic_v3_read_ap1rn(apr
);
661 __vgic_v3_write_ap1rn(val
| BIT(ap
% 32), apr
);
665 static int __vgic_v3_clear_highest_active_priority(void)
667 u8 nr_apr_regs
= vtr_to_nr_apr_regs(read_gicreg(ICH_VTR_EL2
));
671 for (i
= 0; i
< nr_apr_regs
; i
++) {
675 ap0
= __vgic_v3_read_ap0rn(i
);
676 ap1
= __vgic_v3_read_ap1rn(i
);
682 c0
= ap0
? __ffs(ap0
) : 32;
683 c1
= ap1
? __ffs(ap1
) : 32;
685 /* Always clear the LSB, which is the highest priority */
688 __vgic_v3_write_ap0rn(ap0
, i
);
692 __vgic_v3_write_ap1rn(ap1
, i
);
696 /* Rescale to 8 bits of priority */
697 return hap
<< __vgic_v3_bpr_min();
700 return GICv3_IDLE_PRIORITY
;
703 static void __vgic_v3_read_iar(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
709 grp
= __vgic_v3_get_group(vcpu
);
711 lr
= __vgic_v3_highest_priority_lr(vcpu
, vmcr
, &lr_val
);
715 if (grp
!= !!(lr_val
& ICH_LR_GROUP
))
718 pmr
= (vmcr
& ICH_VMCR_PMR_MASK
) >> ICH_VMCR_PMR_SHIFT
;
719 lr_prio
= (lr_val
& ICH_LR_PRIORITY_MASK
) >> ICH_LR_PRIORITY_SHIFT
;
723 if (__vgic_v3_get_highest_active_priority() <= __vgic_v3_pri_to_pre(lr_prio
, vmcr
, grp
))
726 lr_val
&= ~ICH_LR_STATE
;
727 lr_val
|= ICH_LR_ACTIVE_BIT
;
728 __gic_v3_set_lr(lr_val
, lr
);
729 __vgic_v3_set_active_priority(lr_prio
, vmcr
, grp
);
730 vcpu_set_reg(vcpu
, rt
, lr_val
& ICH_LR_VIRTUAL_ID_MASK
);
734 vcpu_set_reg(vcpu
, rt
, ICC_IAR1_EL1_SPURIOUS
);
737 static void __vgic_v3_clear_active_lr(int lr
, u64 lr_val
)
739 lr_val
&= ~ICH_LR_ACTIVE_BIT
;
740 if (lr_val
& ICH_LR_HW
) {
743 pid
= (lr_val
& ICH_LR_PHYS_ID_MASK
) >> ICH_LR_PHYS_ID_SHIFT
;
747 __gic_v3_set_lr(lr_val
, lr
);
750 static void __vgic_v3_bump_eoicount(void)
754 hcr
= read_gicreg(ICH_HCR_EL2
);
755 hcr
+= 1 << ICH_HCR_EOIcount_SHIFT
;
756 write_gicreg(hcr
, ICH_HCR_EL2
);
759 static void __vgic_v3_write_dir(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
761 u32 vid
= vcpu_get_reg(vcpu
, rt
);
765 /* EOImode == 0, nothing to be done here */
766 if (!(vmcr
& ICH_VMCR_EOIM_MASK
))
769 /* No deactivate to be performed on an LPI */
770 if (vid
>= VGIC_MIN_LPI
)
773 lr
= __vgic_v3_find_active_lr(vcpu
, vid
, &lr_val
);
775 __vgic_v3_bump_eoicount();
779 __vgic_v3_clear_active_lr(lr
, lr_val
);
782 static void __vgic_v3_write_eoir(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
784 u32 vid
= vcpu_get_reg(vcpu
, rt
);
786 u8 lr_prio
, act_prio
;
789 grp
= __vgic_v3_get_group(vcpu
);
791 /* Drop priority in any case */
792 act_prio
= __vgic_v3_clear_highest_active_priority();
794 lr
= __vgic_v3_find_active_lr(vcpu
, vid
, &lr_val
);
796 /* Do not bump EOIcount for LPIs that aren't in the LRs */
797 if (!(vid
>= VGIC_MIN_LPI
))
798 __vgic_v3_bump_eoicount();
802 /* EOImode == 1 and not an LPI, nothing to be done here */
803 if ((vmcr
& ICH_VMCR_EOIM_MASK
) && !(vid
>= VGIC_MIN_LPI
))
806 lr_prio
= (lr_val
& ICH_LR_PRIORITY_MASK
) >> ICH_LR_PRIORITY_SHIFT
;
808 /* If priorities or group do not match, the guest has fscked-up. */
809 if (grp
!= !!(lr_val
& ICH_LR_GROUP
) ||
810 __vgic_v3_pri_to_pre(lr_prio
, vmcr
, grp
) != act_prio
)
813 /* Let's now perform the deactivation */
814 __vgic_v3_clear_active_lr(lr
, lr_val
);
817 static void __vgic_v3_read_igrpen0(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
819 vcpu_set_reg(vcpu
, rt
, !!(vmcr
& ICH_VMCR_ENG0_MASK
));
822 static void __vgic_v3_read_igrpen1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
824 vcpu_set_reg(vcpu
, rt
, !!(vmcr
& ICH_VMCR_ENG1_MASK
));
827 static void __vgic_v3_write_igrpen0(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
829 u64 val
= vcpu_get_reg(vcpu
, rt
);
832 vmcr
|= ICH_VMCR_ENG0_MASK
;
834 vmcr
&= ~ICH_VMCR_ENG0_MASK
;
836 __vgic_v3_write_vmcr(vmcr
);
839 static void __vgic_v3_write_igrpen1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
841 u64 val
= vcpu_get_reg(vcpu
, rt
);
844 vmcr
|= ICH_VMCR_ENG1_MASK
;
846 vmcr
&= ~ICH_VMCR_ENG1_MASK
;
848 __vgic_v3_write_vmcr(vmcr
);
851 static void __vgic_v3_read_bpr0(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
853 vcpu_set_reg(vcpu
, rt
, __vgic_v3_get_bpr0(vmcr
));
856 static void __vgic_v3_read_bpr1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
858 vcpu_set_reg(vcpu
, rt
, __vgic_v3_get_bpr1(vmcr
));
861 static void __vgic_v3_write_bpr0(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
863 u64 val
= vcpu_get_reg(vcpu
, rt
);
864 u8 bpr_min
= __vgic_v3_bpr_min() - 1;
866 /* Enforce BPR limiting */
870 val
<<= ICH_VMCR_BPR0_SHIFT
;
871 val
&= ICH_VMCR_BPR0_MASK
;
872 vmcr
&= ~ICH_VMCR_BPR0_MASK
;
875 __vgic_v3_write_vmcr(vmcr
);
878 static void __vgic_v3_write_bpr1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
880 u64 val
= vcpu_get_reg(vcpu
, rt
);
881 u8 bpr_min
= __vgic_v3_bpr_min();
883 if (vmcr
& ICH_VMCR_CBPR_MASK
)
886 /* Enforce BPR limiting */
890 val
<<= ICH_VMCR_BPR1_SHIFT
;
891 val
&= ICH_VMCR_BPR1_MASK
;
892 vmcr
&= ~ICH_VMCR_BPR1_MASK
;
895 __vgic_v3_write_vmcr(vmcr
);
898 static void __vgic_v3_read_apxrn(struct kvm_vcpu
*vcpu
, int rt
, int n
)
902 if (!__vgic_v3_get_group(vcpu
))
903 val
= __vgic_v3_read_ap0rn(n
);
905 val
= __vgic_v3_read_ap1rn(n
);
907 vcpu_set_reg(vcpu
, rt
, val
);
910 static void __vgic_v3_write_apxrn(struct kvm_vcpu
*vcpu
, int rt
, int n
)
912 u32 val
= vcpu_get_reg(vcpu
, rt
);
914 if (!__vgic_v3_get_group(vcpu
))
915 __vgic_v3_write_ap0rn(val
, n
);
917 __vgic_v3_write_ap1rn(val
, n
);
920 static void __vgic_v3_read_apxr0(struct kvm_vcpu
*vcpu
,
923 __vgic_v3_read_apxrn(vcpu
, rt
, 0);
926 static void __vgic_v3_read_apxr1(struct kvm_vcpu
*vcpu
,
929 __vgic_v3_read_apxrn(vcpu
, rt
, 1);
932 static void __vgic_v3_read_apxr2(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
934 __vgic_v3_read_apxrn(vcpu
, rt
, 2);
937 static void __vgic_v3_read_apxr3(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
939 __vgic_v3_read_apxrn(vcpu
, rt
, 3);
942 static void __vgic_v3_write_apxr0(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
944 __vgic_v3_write_apxrn(vcpu
, rt
, 0);
947 static void __vgic_v3_write_apxr1(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
949 __vgic_v3_write_apxrn(vcpu
, rt
, 1);
952 static void __vgic_v3_write_apxr2(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
954 __vgic_v3_write_apxrn(vcpu
, rt
, 2);
957 static void __vgic_v3_write_apxr3(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
959 __vgic_v3_write_apxrn(vcpu
, rt
, 3);
962 static void __vgic_v3_read_hppir(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
967 grp
= __vgic_v3_get_group(vcpu
);
969 lr
= __vgic_v3_highest_priority_lr(vcpu
, vmcr
, &lr_val
);
973 lr_grp
= !!(lr_val
& ICH_LR_GROUP
);
975 lr_val
= ICC_IAR1_EL1_SPURIOUS
;
978 vcpu_set_reg(vcpu
, rt
, lr_val
& ICH_LR_VIRTUAL_ID_MASK
);
981 static void __vgic_v3_read_pmr(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
983 vmcr
&= ICH_VMCR_PMR_MASK
;
984 vmcr
>>= ICH_VMCR_PMR_SHIFT
;
985 vcpu_set_reg(vcpu
, rt
, vmcr
);
988 static void __vgic_v3_write_pmr(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
990 u32 val
= vcpu_get_reg(vcpu
, rt
);
992 val
<<= ICH_VMCR_PMR_SHIFT
;
993 val
&= ICH_VMCR_PMR_MASK
;
994 vmcr
&= ~ICH_VMCR_PMR_MASK
;
997 write_gicreg(vmcr
, ICH_VMCR_EL2
);
1000 static void __vgic_v3_read_rpr(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
1002 u32 val
= __vgic_v3_get_highest_active_priority();
1003 vcpu_set_reg(vcpu
, rt
, val
);
1006 static void __vgic_v3_read_ctlr(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
1010 vtr
= read_gicreg(ICH_VTR_EL2
);
1012 val
= ((vtr
>> 29) & 7) << ICC_CTLR_EL1_PRI_BITS_SHIFT
;
1014 val
|= ((vtr
>> 23) & 7) << ICC_CTLR_EL1_ID_BITS_SHIFT
;
1016 val
|= ((vtr
>> 21) & 1) << ICC_CTLR_EL1_A3V_SHIFT
;
1018 val
|= ((vmcr
& ICH_VMCR_EOIM_MASK
) >> ICH_VMCR_EOIM_SHIFT
) << ICC_CTLR_EL1_EOImode_SHIFT
;
1020 val
|= (vmcr
& ICH_VMCR_CBPR_MASK
) >> ICH_VMCR_CBPR_SHIFT
;
1022 vcpu_set_reg(vcpu
, rt
, val
);
1025 static void __vgic_v3_write_ctlr(struct kvm_vcpu
*vcpu
, u32 vmcr
, int rt
)
1027 u32 val
= vcpu_get_reg(vcpu
, rt
);
1029 if (val
& ICC_CTLR_EL1_CBPR_MASK
)
1030 vmcr
|= ICH_VMCR_CBPR_MASK
;
1032 vmcr
&= ~ICH_VMCR_CBPR_MASK
;
1034 if (val
& ICC_CTLR_EL1_EOImode_MASK
)
1035 vmcr
|= ICH_VMCR_EOIM_MASK
;
1037 vmcr
&= ~ICH_VMCR_EOIM_MASK
;
1039 write_gicreg(vmcr
, ICH_VMCR_EL2
);
1042 static bool __vgic_v3_check_trap_forwarding(struct kvm_vcpu
*vcpu
,
1043 u32 sysreg
, bool is_read
)
1047 if (!vcpu_has_nv(vcpu
) || is_hyp_ctxt(vcpu
))
1050 ich_hcr
= __vcpu_sys_reg(vcpu
, ICH_HCR_EL2
);
1053 case SYS_ICC_IGRPEN0_EL1
:
1055 (__vcpu_sys_reg(vcpu
, HFGRTR_EL2
) & HFGxTR_EL2_ICC_IGRPENn_EL1
))
1059 (__vcpu_sys_reg(vcpu
, HFGWTR_EL2
) & HFGxTR_EL2_ICC_IGRPENn_EL1
))
1064 case SYS_ICC_AP0Rn_EL1(0):
1065 case SYS_ICC_AP0Rn_EL1(1):
1066 case SYS_ICC_AP0Rn_EL1(2):
1067 case SYS_ICC_AP0Rn_EL1(3):
1068 case SYS_ICC_BPR0_EL1
:
1069 case SYS_ICC_EOIR0_EL1
:
1070 case SYS_ICC_HPPIR0_EL1
:
1071 case SYS_ICC_IAR0_EL1
:
1072 return ich_hcr
& ICH_HCR_TALL0
;
1074 case SYS_ICC_IGRPEN1_EL1
:
1076 (__vcpu_sys_reg(vcpu
, HFGRTR_EL2
) & HFGxTR_EL2_ICC_IGRPENn_EL1
))
1080 (__vcpu_sys_reg(vcpu
, HFGWTR_EL2
) & HFGxTR_EL2_ICC_IGRPENn_EL1
))
1085 case SYS_ICC_AP1Rn_EL1(0):
1086 case SYS_ICC_AP1Rn_EL1(1):
1087 case SYS_ICC_AP1Rn_EL1(2):
1088 case SYS_ICC_AP1Rn_EL1(3):
1089 case SYS_ICC_BPR1_EL1
:
1090 case SYS_ICC_EOIR1_EL1
:
1091 case SYS_ICC_HPPIR1_EL1
:
1092 case SYS_ICC_IAR1_EL1
:
1093 return ich_hcr
& ICH_HCR_TALL1
;
1095 case SYS_ICC_DIR_EL1
:
1096 if (ich_hcr
& ICH_HCR_TDIR
)
1101 case SYS_ICC_RPR_EL1
:
1102 case SYS_ICC_CTLR_EL1
:
1103 case SYS_ICC_PMR_EL1
:
1104 return ich_hcr
& ICH_HCR_TC
;
1111 int __vgic_v3_perform_cpuif_access(struct kvm_vcpu
*vcpu
)
1116 void (*fn
)(struct kvm_vcpu
*, u32
, int);
1120 if (kern_hyp_va(vcpu
->kvm
)->arch
.vgic
.vgic_model
!= KVM_DEV_TYPE_ARM_VGIC_V3
)
1123 esr
= kvm_vcpu_get_esr(vcpu
);
1124 if (vcpu_mode_is_32bit(vcpu
)) {
1125 if (!kvm_condition_valid(vcpu
)) {
1126 __kvm_skip_instr(vcpu
);
1130 sysreg
= esr_cp15_to_sysreg(esr
);
1132 sysreg
= esr_sys64_to_sysreg(esr
);
1135 is_read
= (esr
& ESR_ELx_SYS64_ISS_DIR_MASK
) == ESR_ELx_SYS64_ISS_DIR_READ
;
1137 if (__vgic_v3_check_trap_forwarding(vcpu
, sysreg
, is_read
))
1141 case SYS_ICC_IAR0_EL1
:
1142 case SYS_ICC_IAR1_EL1
:
1143 if (unlikely(!is_read
))
1145 fn
= __vgic_v3_read_iar
;
1147 case SYS_ICC_EOIR0_EL1
:
1148 case SYS_ICC_EOIR1_EL1
:
1149 if (unlikely(is_read
))
1151 fn
= __vgic_v3_write_eoir
;
1153 case SYS_ICC_IGRPEN1_EL1
:
1155 fn
= __vgic_v3_read_igrpen1
;
1157 fn
= __vgic_v3_write_igrpen1
;
1159 case SYS_ICC_BPR1_EL1
:
1161 fn
= __vgic_v3_read_bpr1
;
1163 fn
= __vgic_v3_write_bpr1
;
1165 case SYS_ICC_AP0Rn_EL1(0):
1166 case SYS_ICC_AP1Rn_EL1(0):
1168 fn
= __vgic_v3_read_apxr0
;
1170 fn
= __vgic_v3_write_apxr0
;
1172 case SYS_ICC_AP0Rn_EL1(1):
1173 case SYS_ICC_AP1Rn_EL1(1):
1175 fn
= __vgic_v3_read_apxr1
;
1177 fn
= __vgic_v3_write_apxr1
;
1179 case SYS_ICC_AP0Rn_EL1(2):
1180 case SYS_ICC_AP1Rn_EL1(2):
1182 fn
= __vgic_v3_read_apxr2
;
1184 fn
= __vgic_v3_write_apxr2
;
1186 case SYS_ICC_AP0Rn_EL1(3):
1187 case SYS_ICC_AP1Rn_EL1(3):
1189 fn
= __vgic_v3_read_apxr3
;
1191 fn
= __vgic_v3_write_apxr3
;
1193 case SYS_ICC_HPPIR0_EL1
:
1194 case SYS_ICC_HPPIR1_EL1
:
1195 if (unlikely(!is_read
))
1197 fn
= __vgic_v3_read_hppir
;
1199 case SYS_ICC_IGRPEN0_EL1
:
1201 fn
= __vgic_v3_read_igrpen0
;
1203 fn
= __vgic_v3_write_igrpen0
;
1205 case SYS_ICC_BPR0_EL1
:
1207 fn
= __vgic_v3_read_bpr0
;
1209 fn
= __vgic_v3_write_bpr0
;
1211 case SYS_ICC_DIR_EL1
:
1212 if (unlikely(is_read
))
1214 fn
= __vgic_v3_write_dir
;
1216 case SYS_ICC_RPR_EL1
:
1217 if (unlikely(!is_read
))
1219 fn
= __vgic_v3_read_rpr
;
1221 case SYS_ICC_CTLR_EL1
:
1223 fn
= __vgic_v3_read_ctlr
;
1225 fn
= __vgic_v3_write_ctlr
;
1227 case SYS_ICC_PMR_EL1
:
1229 fn
= __vgic_v3_read_pmr
;
1231 fn
= __vgic_v3_write_pmr
;
1237 vmcr
= __vgic_v3_read_vmcr();
1238 rt
= kvm_vcpu_sys_get_rt(vcpu
);
1241 __kvm_skip_instr(vcpu
);