Skip to content

Commit 24422df

Browse files
committed
Merge tag 'kvmarm-fixes-6.6-2' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm into HEAD
KVM/arm64 fixes for 6.6, take #2 - Fix the handling of the phycal timer offset when FEAT_ECV and CNTPOFF_EL2 are implemented. - Restore the functionnality of Permission Indirection that was broken by the Fine Grained Trapping rework - Cleanup some PMU event sharing code
2 parents e2145c9 + 9404673 commit 24422df

File tree

7 files changed

+62
-16
lines changed

7 files changed

+62
-16
lines changed

arch/arm64/include/asm/kvm_arm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,14 @@
344344
*/
345345
#define __HFGRTR_EL2_RES0 (GENMASK(63, 56) | GENMASK(53, 51))
346346
#define __HFGRTR_EL2_MASK GENMASK(49, 0)
347-
#define __HFGRTR_EL2_nMASK (GENMASK(55, 54) | BIT(50))
347+
#define __HFGRTR_EL2_nMASK (GENMASK(58, 57) | GENMASK(55, 54) | BIT(50))
348348

349349
#define __HFGWTR_EL2_RES0 (GENMASK(63, 56) | GENMASK(53, 51) | \
350350
BIT(46) | BIT(42) | BIT(40) | BIT(28) | \
351351
GENMASK(26, 25) | BIT(21) | BIT(18) | \
352352
GENMASK(15, 14) | GENMASK(10, 9) | BIT(2))
353353
#define __HFGWTR_EL2_MASK GENMASK(49, 0)
354-
#define __HFGWTR_EL2_nMASK (GENMASK(55, 54) | BIT(50))
354+
#define __HFGWTR_EL2_nMASK (GENMASK(58, 57) | GENMASK(55, 54) | BIT(50))
355355

356356
#define __HFGITR_EL2_RES0 GENMASK(63, 57)
357357
#define __HFGITR_EL2_MASK GENMASK(54, 0)

arch/arm64/kvm/arch_timer.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ static struct irq_ops arch_timer_irq_ops = {
5555
.get_input_level = kvm_arch_timer_get_input_level,
5656
};
5757

58-
static bool has_cntpoff(void)
59-
{
60-
return (has_vhe() && cpus_have_final_cap(ARM64_HAS_ECV_CNTPOFF));
61-
}
62-
6358
static int nr_timers(struct kvm_vcpu *vcpu)
6459
{
6560
if (!vcpu_has_nv(vcpu))
@@ -180,7 +175,7 @@ u64 kvm_phys_timer_read(void)
180175
return timecounter->cc->read(timecounter->cc);
181176
}
182177

183-
static void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
178+
void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
184179
{
185180
if (vcpu_has_nv(vcpu)) {
186181
if (is_hyp_ctxt(vcpu)) {
@@ -548,8 +543,7 @@ static void timer_save_state(struct arch_timer_context *ctx)
548543
timer_set_ctl(ctx, read_sysreg_el0(SYS_CNTP_CTL));
549544
cval = read_sysreg_el0(SYS_CNTP_CVAL);
550545

551-
if (!has_cntpoff())
552-
cval -= timer_get_offset(ctx);
546+
cval -= timer_get_offset(ctx);
553547

554548
timer_set_cval(ctx, cval);
555549

@@ -636,8 +630,7 @@ static void timer_restore_state(struct arch_timer_context *ctx)
636630
cval = timer_get_cval(ctx);
637631
offset = timer_get_offset(ctx);
638632
set_cntpoff(offset);
639-
if (!has_cntpoff())
640-
cval += offset;
633+
cval += offset;
641634
write_sysreg_el0(cval, SYS_CNTP_CVAL);
642635
isb();
643636
write_sysreg_el0(timer_get_ctl(ctx), SYS_CNTP_CTL);

arch/arm64/kvm/emulate-nested.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,8 @@ enum fg_filter_id {
977977

978978
static const struct encoding_to_trap_config encoding_to_fgt[] __initconst = {
979979
/* HFGRTR_EL2, HFGWTR_EL2 */
980+
SR_FGT(SYS_PIR_EL1, HFGxTR, nPIR_EL1, 0),
981+
SR_FGT(SYS_PIRE0_EL1, HFGxTR, nPIRE0_EL1, 0),
980982
SR_FGT(SYS_TPIDR2_EL0, HFGxTR, nTPIDR2_EL0, 0),
981983
SR_FGT(SYS_SMPRI_EL1, HFGxTR, nSMPRI_EL1, 0),
982984
SR_FGT(SYS_ACCDATA_EL1, HFGxTR, nACCDATA_EL1, 0),

arch/arm64/kvm/hyp/vhe/switch.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
3939

4040
___activate_traps(vcpu);
4141

42+
if (has_cntpoff()) {
43+
struct timer_map map;
44+
45+
get_timer_map(vcpu, &map);
46+
47+
/*
48+
* We're entrering the guest. Reload the correct
49+
* values from memory now that TGE is clear.
50+
*/
51+
if (map.direct_ptimer == vcpu_ptimer(vcpu))
52+
val = __vcpu_sys_reg(vcpu, CNTP_CVAL_EL0);
53+
if (map.direct_ptimer == vcpu_hptimer(vcpu))
54+
val = __vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2);
55+
56+
if (map.direct_ptimer) {
57+
write_sysreg_el0(val, SYS_CNTP_CVAL);
58+
isb();
59+
}
60+
}
61+
4262
val = read_sysreg(cpacr_el1);
4363
val |= CPACR_ELx_TTA;
4464
val &= ~(CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN |
@@ -77,6 +97,30 @@ static void __deactivate_traps(struct kvm_vcpu *vcpu)
7797

7898
write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
7999

100+
if (has_cntpoff()) {
101+
struct timer_map map;
102+
u64 val, offset;
103+
104+
get_timer_map(vcpu, &map);
105+
106+
/*
107+
* We're exiting the guest. Save the latest CVAL value
108+
* to memory and apply the offset now that TGE is set.
109+
*/
110+
val = read_sysreg_el0(SYS_CNTP_CVAL);
111+
if (map.direct_ptimer == vcpu_ptimer(vcpu))
112+
__vcpu_sys_reg(vcpu, CNTP_CVAL_EL0) = val;
113+
if (map.direct_ptimer == vcpu_hptimer(vcpu))
114+
__vcpu_sys_reg(vcpu, CNTHP_CVAL_EL2) = val;
115+
116+
offset = read_sysreg_s(SYS_CNTPOFF_EL2);
117+
118+
if (map.direct_ptimer && offset) {
119+
write_sysreg_el0(val + offset, SYS_CNTP_CVAL);
120+
isb();
121+
}
122+
}
123+
80124
/*
81125
* ARM errata 1165522 and 1530923 require the actual execution of the
82126
* above before we can switch to the EL2/EL0 translation regime used by

arch/arm64/kvm/pmu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr)
3939
{
4040
struct kvm_pmu_events *pmu = kvm_get_pmu_events();
4141

42-
if (!kvm_arm_support_pmu_v3() || !pmu || !kvm_pmu_switch_needed(attr))
42+
if (!kvm_arm_support_pmu_v3() || !kvm_pmu_switch_needed(attr))
4343
return;
4444

4545
if (!attr->exclude_host)
@@ -55,7 +55,7 @@ void kvm_clr_pmu_events(u32 clr)
5555
{
5656
struct kvm_pmu_events *pmu = kvm_get_pmu_events();
5757

58-
if (!kvm_arm_support_pmu_v3() || !pmu)
58+
if (!kvm_arm_support_pmu_v3())
5959
return;
6060

6161
pmu->events_host &= ~clr;

arch/arm64/kvm/sys_regs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,8 +2122,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
21222122
{ SYS_DESC(SYS_PMMIR_EL1), trap_raz_wi },
21232123

21242124
{ SYS_DESC(SYS_MAIR_EL1), access_vm_reg, reset_unknown, MAIR_EL1 },
2125-
{ SYS_DESC(SYS_PIRE0_EL1), access_vm_reg, reset_unknown, PIRE0_EL1 },
2126-
{ SYS_DESC(SYS_PIR_EL1), access_vm_reg, reset_unknown, PIR_EL1 },
2125+
{ SYS_DESC(SYS_PIRE0_EL1), NULL, reset_unknown, PIRE0_EL1 },
2126+
{ SYS_DESC(SYS_PIR_EL1), NULL, reset_unknown, PIR_EL1 },
21272127
{ SYS_DESC(SYS_AMAIR_EL1), access_vm_reg, reset_amair_el1, AMAIR_EL1 },
21282128

21292129
{ SYS_DESC(SYS_LORSA_EL1), trap_loregion },

include/kvm/arm_arch_timer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct timer_map {
8282
struct arch_timer_context *emul_ptimer;
8383
};
8484

85+
void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map);
86+
8587
struct arch_timer_cpu {
8688
struct arch_timer_context timers[NR_KVM_TIMERS];
8789

@@ -145,4 +147,9 @@ u64 timer_get_cval(struct arch_timer_context *ctxt);
145147
void kvm_timer_cpu_up(void);
146148
void kvm_timer_cpu_down(void);
147149

150+
static inline bool has_cntpoff(void)
151+
{
152+
return (has_vhe() && cpus_have_final_cap(ARM64_HAS_ECV_CNTPOFF));
153+
}
154+
148155
#endif

0 commit comments

Comments
 (0)