From 7b697fbe3606959754258013f1b96021ad1a5e15 Mon Sep 17 00:00:00 2001 From: Xiaochen Shen Date: Thu, 13 Nov 2025 15:04:25 +0800 Subject: [PATCH] anolis: x86/resctrl: Fix memory bandwidth counter width for Hygon CPUs ANBZ: #27057 The memory bandwidth calculation relies on reading the hardware counter and measuring the delta between samples. To ensure accurate measurement, the software reads the counter frequently enough to prevent it from rolling over twice between reads. The default base counter width is 24. Currently, Hygon CPUs do not support the CPUID 0xF.[ECX=1]:EAX to adjust the counter width. But the Hygon CPUs support wider bandwidth counter with the default width of 32 bits. Fix the issue by setting the default width to 32 bits (adjusting the offset to 8 bits) for Hygon CPUs. Hygon-SIG: commit none hygon anolis: x86/resctrl: Fix memory bandwidth counter width for Hygon CPUs Fixes: 93ea79ade1df ("anolis: x86/resctrl: Add Hygon QoS support") Signed-off-by: Xiaochen Shen Cc: hygon-arch@list.openanolis.cn --- arch/x86/kernel/cpu/resctrl/core.c | 18 +++++++++++++----- arch/x86/kernel/cpu/resctrl/internal.h | 2 ++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c index 6a32e3247d75..87ddf1f5caf7 100644 --- a/arch/x86/kernel/cpu/resctrl/core.c +++ b/arch/x86/kernel/cpu/resctrl/core.c @@ -997,11 +997,19 @@ void resctrl_cpu_detect(struct cpuinfo_x86 *c) c->x86_cache_occ_scale = ebx; c->x86_cache_mbm_width_offset = eax & 0xff; - if ((c->x86_vendor == X86_VENDOR_AMD || - c->x86_vendor == X86_VENDOR_HYGON) && - !c->x86_cache_mbm_width_offset) - c->x86_cache_mbm_width_offset = - MBM_CNTR_WIDTH_OFFSET_AMD; + if (!c->x86_cache_mbm_width_offset) { + switch (c->x86_vendor) { + case X86_VENDOR_AMD: + c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_AMD; + break; + case X86_VENDOR_HYGON: + c->x86_cache_mbm_width_offset = MBM_CNTR_WIDTH_OFFSET_HYGON; + break; + default: + /* Leave c->x86_cache_mbm_width_offset as 0 */ + break; + } + } } } diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h index b4a6323f63a7..c8e6a57c036f 100644 --- a/arch/x86/kernel/cpu/resctrl/internal.h +++ b/arch/x86/kernel/cpu/resctrl/internal.h @@ -37,6 +37,8 @@ #define MBA_MAX_MBPS U32_MAX #define MAX_MBA_BW_AMD 0x800 #define MBM_CNTR_WIDTH_OFFSET_AMD 20 +/* Hygon MBM counter width as an offset from MBM_CNTR_WIDTH_BASE */ +#define MBM_CNTR_WIDTH_OFFSET_HYGON 8 #define RMID_VAL_ERROR BIT_ULL(63) #define RMID_VAL_UNAVAIL BIT_ULL(62) -- Gitee