In generic PMU testing, it is very common to initialize the test env by resetting counters registers. Add helpers to reset all PMU counters for code reusability, and reset all counters during PMU initialization for good measure. Signed-off-by: Like Xu <likexu@xxxxxxxxxxx> Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx> --- lib/x86/pmu.c | 2 ++ lib/x86/pmu.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/x86/pmu.c b/lib/x86/pmu.c index c73f802a..fb9a121e 100644 --- a/lib/x86/pmu.c +++ b/lib/x86/pmu.c @@ -24,4 +24,6 @@ void pmu_init(void) pmu.perf_cap = rdmsr(MSR_IA32_PERF_CAPABILITIES); pmu.msr_gp_counter_base = MSR_IA32_PERFCTR0; pmu.msr_gp_event_select_base = MSR_P6_EVNTSEL0; + + pmu_reset_all_counters(); } diff --git a/lib/x86/pmu.h b/lib/x86/pmu.h index 091e61b3..cd81f557 100644 --- a/lib/x86/pmu.h +++ b/lib/x86/pmu.h @@ -96,4 +96,32 @@ static inline u32 MSR_PERF_FIXED_CTRx(unsigned int i) return MSR_CORE_PERF_FIXED_CTR0 + i; } +static inline void pmu_reset_all_gp_counters(void) +{ + unsigned int idx; + + for (idx = 0; idx < pmu.nr_gp_counters; idx++) { + wrmsr(MSR_GP_EVENT_SELECTx(idx), 0); + wrmsr(MSR_GP_COUNTERx(idx), 0); + } +} + +static inline void pmu_reset_all_fixed_counters(void) +{ + unsigned int idx; + + if (!pmu.nr_fixed_counters) + return; + + wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, 0); + for (idx = 0; idx < pmu.nr_fixed_counters; idx++) + wrmsr(MSR_PERF_FIXED_CTRx(idx), 0); +} + +static inline void pmu_reset_all_counters(void) +{ + pmu_reset_all_gp_counters(); + pmu_reset_all_fixed_counters(); +} + #endif /* _X86_PMU_H_ */ -- 2.38.1.431.g37b22c650d-goog