[RFC][PATCH 21/34] x86/cpu: Consolidate CLFLUSH size setting.

Dave Hansen
2024-02-22T18:39:54Z

Right now, cpu_detect() establishes ->x86_clflush_size on each CPU
that. supports CLFLUSH. As you might have guessed, that per-cpu
value is no longer used.

Move the setting to get_cpu_address_sizes() which is only called
on the boot CPU. Consolidate all of the ->x86_clflush_size logic
into a single helper.

This removes the (cap0 & (1<<19) aka X86_FEATURE_CLFLUSH check before
reading the CLFLUSH size out of CPUID[1]. Those bits are presumably
either 0 or have actual data about CLFLUSH in them.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
---

b/arch/x86/kernel/cpu/common.c | 35 ++++++++++++++++++++++++-----------
1 file changed, 24 insertions(+), 11 deletions(-)

diff -puN arch/x86/kernel/cpu/common.c~later-clflush_size-detect arch/x86/kernel/cpu/common.c
--- a/arch/x86/kernel/cpu/common.c~later-clflush_size-detect 2024-02-22 10:08:58.728856457 -0800
+++ b/arch/x86/kernel/cpu/common.c 2024-02-22 10:08:58.732856614 -0800
@@ -935,6 +935,27 @@ static void get_cpu_vendor(struct cpuinf
this_cpu = &default_cpu;
}

+static u16 detect_clflush_size(struct cpuinfo_x86 *c)
+{
+ u16 clflush_size = 0;
+
+ if (c->cpuid_level >= 0x00000001) {
+ u32 eax, ebx, ecx, edx;
+
+ cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
+
+ clflush_size = ((ebx >> 8) & 0xff) * 8;
+ }
+
+ if (clflush_size)
+ return clflush_size;
+
+ /* Return some mostly sane defaults: */
+ if (IS_ENABLED(CONFIG_X86_64))
+ return 64;
+ return 32;
+}
+
void cpu_detect(struct cpuinfo_x86 *c)
{
/* Get vendor name */
@@ -952,11 +973,6 @@ void cpu_detect(struct cpuinfo_x86 *c)
c->x86 = x86_family(tfms);
c->x86_model = x86_model(tfms);
c->x86_stepping = x86_stepping(tfms);
-
- if (cap0 & (1<<19)) {
- if (c == &boot_cpu_data)
- c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
- }
}
}

@@ -1111,17 +1127,17 @@ void get_cpu_address_sizes(struct cpuinf
if (IS_ENABLED(CONFIG_X86_64)) {
x86_config.phys_bits = 36;
x86_config.virt_bits = 48;
- c->x86_clflush_size = 64;
} else {
x86_config.phys_bits = 32;
x86_config.virt_bits = 32;
- c->x86_clflush_size = 32;

if (cpu_has(c, X86_FEATURE_PAE) ||
cpu_has(c, X86_FEATURE_PSE36))
x86_config.phys_bits = 36;
}
}
+ c->x86_clflush_size = detect_clflush_size(c);
+
c->x86_cache_bits = x86_config.phys_bits;

c->x86_cache_alignment = x86_clflush_size();
@@ -1827,11 +1843,8 @@ static void identify_cpu(struct cpuinfo_
c->topo.cu_id = 0xff;
c->topo.llc_id = BAD_APICID;
c->topo.l2c_id = BAD_APICID;
-#ifdef CONFIG_X86_64
- c->x86_clflush_size = 64;
-#else
+#ifndef CONFIG_X86_64
c->cpuid_level = -1; /* CPUID not detected */
- c->x86_clflush_size = 32;
#endif
memset(&c->x86_capability, 0, sizeof(c->x86_capability));
#ifdef CONFIG_X86_VMX_FEATURE_NAMES
_