x86: Leaf 0x2 and leaf 0x4 refactorings
Subsystems (8)
Ahmed S. Darwish
reviewerv2 cover x86: Leaf 0x2 and leaf 0x4 refactorings
Hi,
This is a v2 of the leaf 0x2 and leaf 0x4 code paths cleanup, in preparation for the x86-cpuid-db CPUID model on top of this series.
Changelog v2
============
Twelve patches got merged from v1, and they're now at tip/x86/core (thanks a lot). Thus, this is on top of tip/x86/core as well.
Apply Ingo Molnar's review remarks:
- New <asm/cpuid/> header structure matching what is at <asm/fpu/>:
<asm/cpuid/> | +-- api.h +-- leaf_0x2_api.h `-- types.h
- Standardize words usage and header file references across commit logs and comments (CPUID, vendor names, <header.h>, etc.)
- Use cpuid_ prefix for all new functions at <asm/cpuid/api.h> and
<asm/cpuid/leaf_0x2_api.h>.
Note, by the end of this series, leaf 0x2 call sites become:
const struct leaf_0x2_table *entry;
union leaf_0x2_regs regs;
u8 *ptr;
cpuid_get_leaf_0x2_regs(®s);
for_each_leaf_0x2_entry(regs, ptr, entry) {
switch (entry->c_type) {
...
}
}Changelog v1
============
https://lkml.kernel.org/r/[email protected]
As part of the onging x86-cpuid work [*], we've found that the handling of leaf 0x2 and leaf 0x4 code paths is difficult to work with in its current state. This was mostly due to the organic incremental growth of the x86/cpu and x86/cacheinfo logic since the very early Linux days.
This series cleans up and refactors these code paths in preparation for the new x86-cpuid model.
Summary:
- Patches 1 to 3 are independent bugfixes that were discovered during this refactoring work.
- Patches 4 to 10 are x86/cpu refactorings for code size and readability.
- Patch 10 adds standardized and kernel-doc documented logic for accessing leaf 0x2 one byte descriptors.
This makes the leaf 0x2 sanitization logic centralized in one place. x86/cpu and x86/cacheinfo is modified to use such macros afterwards.
- Patches 11 to 28 refactors the x86/cacheinfo code.
Beside readability, some of the unrelated logic (e.g. AMD northbridge cache_disable sysfs code) was first splitted from the generic leaf 0x4 code paths, at the structure relationships level, then gutted-out into their own files.
- Patches 29 to 31 consolidate the existing (loop-based lookup) leaf 0x2 cache and TLB descriptor tables into one hash-based lookup table. This reduces code size while still keeping rodata size in check.
Standardized macros for accessing this consolidated table are also added. Call sites can now just do:
const struct leaf_0x2_table *entry;
union leaf_0x2_regs regs;
u8 *ptr;
get_leaf_0x2_regs(®s);
for_each_leaf_0x2_entry(regs, ptr, entry) {
switch (entry->c_type) {
...
}
}without need to worry about sanitizing registers, skipping certain descriptors, etc.
- Patches 32 and 33 uses the consolidated table above for x86/cpu and x86/cacheinfo.
- Patches 34 to 40 provide the final set of x86/refactorings.
This series is based on -rc5. It also applies cleanly on top of tip/x86/core.
Note, testing was done by comparing below files:
/proc/cpuinfo /sys/devices/system/cpu/ /sys/kernel/debug/x86/topo/ dmesg --notime | grep 'Last level [id]TLB entries'
before and after on various old and new x86 machine configurations.
[*] https://gitlab.com/x86-cpuid.org/x86-cpuid-db
https://x86-cpuid.org
8<-----
Ahmed S. Darwish (25):
x86: treewide: Introduce x86_vendor_amd_or_hygon() x86/cpuid: Refactor <asm/cpuid.h>
x86/cpu: Remove leaf 0x2 parsing loop and add helpers x86/cacheinfo: Use CPUID leaf 0x2 parsing helpers x86/cacheinfo: Constify _cpuid4_info_regs instances x86/cacheinfo: Align ci_info_init() assignment expressions x86/cacheinfo: Standardize _cpuid4_info_regs instance naming x86/cacheinfo: Consolidate AMD/Hygon leaf 0x8000001d calls x86/cacheinfo: Separate amd_northbridge from _cpuid4_info_regs x86/cacheinfo: Move AMD cache_disable_0/1 handling to separate file x86/cacheinfo: Use sysfs_emit() for sysfs attributes show() x86/cacheinfo: Separate Intel and AMD leaf 0x4 code paths x86/cacheinfo: Rename _cpuid4_info_regs to _cpuid4_info x86/cacheinfo: Clarify type markers for leaf 0x2 cache descriptors x86/cacheinfo: Use enums for cache descriptor types x86/cpu: Use enums for TLB descriptor types x86/cacheinfo: Use consolidated leaf 0x2 descriptor table x86/cpu: Use consolidated leaf 0x2 descriptor table x86/cacheinfo: Separate leaf 0x2 handling and post-processing logic x86/cacheinfo: Separate Intel leaf 0x4 handling x86/cacheinfo: Extract out cache level topology ID calculation x86/cacheinfo: Extract out cache self-snoop checks x86/cacheinfo: Relocate leaf 0x4 cache_type mapping x86/cacheinfo: Introduce cpuid_amd_hygon_has_l3_cache() x86/cacheinfo: Apply maintainer-tip coding style fixes
Thomas Gleixner (4):
x86/cacheinfo: Refactor leaf 0x2 cache descriptor lookup x86/cacheinfo: Properly name amd_cpuid4()'s first parameter x86/cacheinfo: Use proper name for cacheinfo instances x86/cpu: Consolidate CPUID leaf 0x2 tables
arch/x86/events/amd/uncore.c | 3 +- arch/x86/events/rapl.c | 3 +- arch/x86/include/asm/cpuid.h | 218 +---- arch/x86/include/asm/cpuid/api.h | 217 +++++ arch/x86/include/asm/cpuid/leaf_0x2_api.h | 96 ++ arch/x86/include/asm/cpuid/types.h | 121 +++ arch/x86/include/asm/processor.h | 5 + arch/x86/kernel/amd_nb.c | 16 +- arch/x86/kernel/cpu/Makefile | 5 +- arch/x86/kernel/cpu/amd_cache_disable.c | 301 ++++++ arch/x86/kernel/cpu/bugs.c | 12 +- arch/x86/kernel/cpu/cacheinfo.c | 1041 +++++++-------------- arch/x86/kernel/cpu/cpu.h | 9 + arch/x86/kernel/cpu/cpuid_0x2_table.c | 128 +++ arch/x86/kernel/cpu/intel.c | 123 +-- arch/x86/kernel/cpu/mce/core.c | 4 +- arch/x86/kernel/cpu/mce/severity.c | 3 +- arch/x86/kernel/cpu/mtrr/cleanup.c | 3 +- arch/x86/kvm/svm/svm.c | 3 +- arch/x86/pci/amd_bus.c | 3 +- arch/x86/xen/enlighten.c | 15 +- arch/x86/xen/pmu.c | 3 +- 22 files changed, 1241 insertions(+), 1091 deletions(-) create mode 100644 arch/x86/include/asm/cpuid/api.h create mode 100644 arch/x86/include/asm/cpuid/leaf_0x2_api.h create mode 100644 arch/x86/include/asm/cpuid/types.h create mode 100644 arch/x86/kernel/cpu/amd_cache_disable.c create mode 100644 arch/x86/kernel/cpu/cpuid_0x2_table.c
base-commit: 6d536cad0d55e71442b6d65500f74eb85544269e --
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 1/29 x86: treewide: Introduce x86_vendor_amd_or_hygon()
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce x86_vendor_amd_or_hygon() at <asm/processor.h> and use it across the x86 tree.
--- arch/x86/events/amd/uncore.c | 3 +-- arch/x86/events/rapl.c | 3 +-- arch/x86/include/asm/processor.h | 5 +++++ arch/x86/kernel/amd_nb.c | 9 +++------ arch/x86/kernel/cpu/bugs.c | 12 ++++-------- arch/x86/kernel/cpu/cacheinfo.c | 6 ++---- arch/x86/kernel/cpu/mce/core.c | 4 ++-- arch/x86/kernel/cpu/mce/severity.c | 3 +-- arch/x86/kernel/cpu/mtrr/cleanup.c | 3 +-- arch/x86/kvm/svm/svm.c | 3 +-- arch/x86/pci/amd_bus.c | 3 +-- arch/x86/xen/enlighten.c | 15 +++++---------- arch/x86/xen/pmu.c | 3 +-- 13 files changed, 28 insertions(+), 44 deletions(-)
arch/x86/events/amd/uncore.c +1 -2
@@ -1023,8 +1023,7 @@ static int __init amd_uncore_init(void)int ret = -ENODEV ;int i; -if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return -ENODEV ;if (!boot_cpu_has (X86_FEATURE_TOPOEXT ))
arch/x86/events/rapl.c +1 -2
@@ -123,8 +123,7 @@ static struct perf_pmu_events_attr event_attr_##v = { \* them as die-scope. */ #define rapl_pkg_pmu_is_pkg_scope () \ - (boot_cpu_data.x86_vendor == X86_VENDOR_AMD || \ - boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) + x86_vendor_amd_or_hygon(boot_cpu_data.x86_vendor)struct rapl_pmu {raw_spinlock_t lock;
arch/x86/include/asm/processor.h +5 -0
@@ -203,6 +203,11 @@ struct cpuinfo_x86 {#define X86_VENDOR_UNKNOWN 0xff +static inline bool x86_vendor_amd_or_hygon (u8 vendor) +{ +return (vendor ==X86_VENDOR_AMD || vendor ==X86_VENDOR_HYGON ); +} +/* * capabilities of CPUs */
arch/x86/kernel/amd_nb.c +3 -6
@@ -127,8 +127,7 @@ bool __init early_is_amd_nb(u32 device)const struct pci_device_id *id;u32 vendor = device &0xffff ; -if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return false;if (cpu_feature_enabled (X86_FEATURE_ZEN ))
@@ -147,8 +146,7 @@ struct resource *amd_get_mmconfig_range(struct resource *res)u64 base, msr;unsigned int segn_busn_bits; -if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return NULL ;/* assume all cpus from fam10h have mmconfig */
@@ -320,8 +318,7 @@ static __init void fix_erratum_688(void)static __init int init_amd_nbs (void ) { -if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return 0 ;amd_cache_northbridges ();
arch/x86/kernel/cpu/bugs.c +4 -8
@@ -1081,8 +1081,7 @@ static void __init retbleed_select_mitigation(void) do_cmd_auto:case RETBLEED_CMD_AUTO : -if (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD || - boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON ) { +if (x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor)) {if (IS_ENABLED (CONFIG_MITIGATION_UNRET_ENTRY )) retbleed_mitigation =RETBLEED_MITIGATION_UNRET ; else if (IS_ENABLED(CONFIG_MITIGATION_IBPB_ENTRY) &&
@@ -1106,8 +1105,7 @@ static void __init retbleed_select_mitigation(void) x86_return_thunk = retbleed_return_thunk; -if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))pr_err (RETBLEED_UNTRAIN_MSG ); mitigate_smt = true;
@@ -1872,8 +1870,7 @@ static void __init spectre_v2_select_mitigation(void)*/ if (boot_cpu_has_bug (X86_BUG_RETBLEED ) &&boot_cpu_has (X86_FEATURE_IBPB ) && - (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD || - boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON )) { +x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor)) {if (retbleed_cmd !=RETBLEED_CMD_IBPB ) {setup_force_cpu_cap (X86_FEATURE_USE_IBPB_FW );
@@ -2903,8 +2900,7 @@ static ssize_t retbleed_show_state(char *buf) {if (retbleed_mitigation ==RETBLEED_MITIGATION_UNRET || retbleed_mitigation ==RETBLEED_MITIGATION_IBPB ) { -if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return sysfs_emit (buf,"Vulnerable: untrained return thunk / IBPB on non-AMD based uarch\n" ); return sysfs_emit(buf, "%s; SMT %s\n", retbleed_strings[retbleed_mitigation],
arch/x86/kernel/cpu/cacheinfo.c +2 -4
@@ -637,8 +637,7 @@ static int find_num_cache_leaves(struct cpuinfo_x86 *c)union _cpuid4_leaf_eax cache_eax;int i =-1 ; -if (c->x86_vendor ==X86_VENDOR_AMD || - c->x86_vendor ==X86_VENDOR_HYGON ) +if (x86_vendor_amd_or_hygon (c->x86_vendor)) op =0x8000001d ;else op =4 ;
@@ -930,8 +929,7 @@ static void __cache_cpumap_setup(unsigned int cpu, int index,int index_msb, i;struct cpuinfo_x86 *c = &cpu_data (cpu); -if (c->x86_vendor ==X86_VENDOR_AMD || - c->x86_vendor ==X86_VENDOR_HYGON ) { +if (x86_vendor_amd_or_hygon (c->x86_vendor)) {if (__cache_amd_cpumap_setup (cpu, index, base))return ; }
arch/x86/kernel/cpu/mce/core.c +2 -2
@@ -227,7 +227,7 @@ static void print_mce(struct mce_hw_err *err)__print_mce (err); -if (m->cpuvendor !=X86_VENDOR_AMD && m->cpuvendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (m->cpuvendor))pr_emerg_ratelimited (HW_ERR "Run the above through 'mcelog --ascii'\n" ); }
@@ -2060,7 +2060,7 @@ static bool __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)*/ static void __mcheck_cpu_init_early (struct cpuinfo_x86 *c) { -if (c->x86_vendor ==X86_VENDOR_AMD || c->x86_vendor ==X86_VENDOR_HYGON ) { +if (x86_vendor_amd_or_hygon (c->x86_vendor)) { mce_flags.overflow_recov = !!cpu_has (c,X86_FEATURE_OVERFLOW_RECOV ); mce_flags.succor = !!cpu_has (c,X86_FEATURE_SUCCOR ); mce_flags.smca = !!cpu_has (c,X86_FEATURE_SMCA );
arch/x86/kernel/cpu/mce/severity.c +1 -2
@@ -413,8 +413,7 @@ static noinstr int mce_severity_intel(struct mce *m, struct pt_regs *regs, char int noinstr mce_severity(struct mce *m, struct pt_regs *regs, char **msg, bool is_excp) { -if (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD || - boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON ) +if (x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return mce_severity_amd (m, regs, msg, is_excp);else return mce_severity_intel (m, regs, msg, is_excp);
arch/x86/kernel/cpu/mtrr/cleanup.c +1 -2
@@ -820,8 +820,7 @@ int __init amd_special_default_mtrr(void) {u32 l, h; -if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return 0 ;if (boot_cpu_data.x86 <0xf )return 0 ;
arch/x86/kvm/svm/svm.c +1 -2
@@ -523,8 +523,7 @@ static bool __kvm_is_svm_supported(void)int cpu =smp_processor_id ();struct cpuinfo_x86 *c = &cpu_data (cpu); -if (c->x86_vendor !=X86_VENDOR_AMD && - c->x86_vendor !=X86_VENDOR_HYGON ) { +if (!x86_vendor_amd_or_hygon (c->x86_vendor)) {pr_err ("CPU %d isn't AMD or Hygon\n" , cpu);return false; }
arch/x86/pci/amd_bus.c +1 -2
@@ -399,8 +399,7 @@ static int __init pci_io_ecs_init(void)static int __init amd_postcore_init (void ) { -if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return 0 ;early_root_info_init ();
arch/x86/xen/enlighten.c +5 -10
@@ -82,11 +82,9 @@ void xen_hypercall_setfunc(void)if (static_call_query (xen_hypercall) != xen_hypercall_hvm)return ; -if ((boot_cpu_data.x86_vendor ==X86_VENDOR_AMD || - boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON )) -static_call_update (xen_hypercall, xen_hypercall_amd); -else -static_call_update (xen_hypercall, xen_hypercall_intel); +static_call_update (xen_hypercall, +x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor) ? + xen_hypercall_amd : xen_hypercall_intel); }/*
@@ -118,11 +116,8 @@ noinstr void *__xen_hypercall_setfunc(void)if (!boot_cpu_has (X86_FEATURE_CPUID ))xen_get_vendor (); -if ((boot_cpu_data.x86_vendor ==X86_VENDOR_AMD || - boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON )) - func = xen_hypercall_amd; -else - func = xen_hypercall_intel; + func =x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor) ? + xen_hypercall_amd : xen_hypercall_intel;static_call_update_early (xen_hypercall, func);
arch/x86/xen/pmu.c +1 -2
@@ -130,8 +130,7 @@ static inline uint32_t get_fam15h_addr(u32 addr)static inline bool is_amd_pmu_msr (unsigned int msr) { -if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return false; if ((msr >= MSR_F15H_PERF_CTL &&
Signature · 1 line
2.48.1
Borislav Petkov
maintainerRe: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
Quoted 2 lines · Ahmed S. Darwish
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce
x86_vendor_amd_or_hygon()at <asm/processor.h>
So if we need to check "intel too", we do
x86_vendor_amd_or_hygon_or_intel?
Nah, this is silly.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePeter Zijlstra
maintainerRe: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
Quoted 9 lines · Borislav Petkov
Quoted 2 lines · Ahmed S. Darwish
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce
x86_vendor_amd_or_hygon()at <asm/processor.h>So if we need to check "intel too", we do
x86_vendor_amd_or_hygon_or_intel?Nah, this is silly.
Would it make more sense to have a Zen1 feature and check that instead?
Because, IIRC Hygon is simply a Zen1 copy.
Re: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Borislav Petkov
Quoted 2 lines · Ahmed S. Darwish
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce
x86_vendor_amd_or_hygon()at <asm/processor.h>
Quoted 8 lines
Quoted 5 lines
So if we need to check "intel too", we do
x86_vendor_amd_or_hygon_or_intel?Nah, this is silly.
Would it make more sense to have a Zen1 feature and check that instead?
Because, IIRC Hygon is simply a Zen1 copy.
AMD and Hygon the most often compared together, because the Hygon CPUs are so close to Zen1/2.
One trick you can do to make this easier is to define vendors IDs as (1U << $N). This leaves all existing code with equality checks working, but also lets you check "vendor & (AMD | HYGON)" rather more efficiently.
~Andrew
H. Peter Anvin
reviewerRe: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
Quoted 14 lines · Andrew Cooper
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Borislav Petkov
Quoted 2 lines · Ahmed S. Darwish
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce
x86_vendor_amd_or_hygon()at <asm/processor.h>Quoted 8 lines
Quoted 5 lines
So if we need to check "intel too", we do
x86_vendor_amd_or_hygon_or_intel?Nah, this is silly.
Would it make more sense to have a Zen1 feature and check that instead?
Because, IIRC Hygon is simply a Zen1 copy.
Quoted 8 lines
AMD and Hygon the most often compared together, because the Hygon CPUs are so close to Zen1/2.
One trick you can do to make this easier is to define vendors IDs as (1U << $N). This leaves all existing code with equality checks working, but also lets you check "vendor & (AMD | HYGON)" rather more efficiently.
~Andrew
Jinx.
Re: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
In reply to Peter ZijlstraQuoted 4 lines · Peter Zijlstra
Quoted 3 lines · Borislav Petkov
Quoted 2 lines · Ahmed S. Darwish
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce
x86_vendor_amd_or_hygon()at <asm/processor.h>
Quoted 8 lines
Quoted 5 lines
So if we need to check "intel too", we do
x86_vendor_amd_or_hygon_or_intel?Nah, this is silly.
Would it make more sense to have a Zen1 feature and check that instead?
Because, IIRC Hygon is simply a Zen1 copy.
Some of them can just go away, I think. This, for instance:
if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
return false;
if (cpu_feature_enabled(X86_FEATURE_ZEN))
return false;Do we even need a vendor check if we're checking X86_FEATURE_ZEN? Is someone setting X86_FEATURE_ZEN on Intel? ;)
H. Peter Anvin
reviewerRe: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
Quoted 23 lines · Dave Hansen
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Borislav Petkov
Quoted 2 lines · Ahmed S. Darwish
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce
x86_vendor_amd_or_hygon()at <asm/processor.h>Quoted 8 lines
Quoted 5 lines
So if we need to check "intel too", we do
x86_vendor_amd_or_hygon_or_intel?Nah, this is silly.
Would it make more sense to have a Zen1 feature and check that instead?
Because, IIRC Hygon is simply a Zen1 copy.
Some of them can just go away, I think. This, for instance:
if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD && boot_cpu_data.x86_vendor != X86_VENDOR_HYGON) return false; if (cpu_feature_enabled(X86_FEATURE_ZEN)) return false;
Quoted 2 lines
Do we even need a vendor check if we're checking
X86_FEATURE_ZEN? Is someone settingX86_FEATURE_ZENon Intel? ;)
Maybe having a vendor bitmask instead of (or as an alternative to) an enumeration wouldn't be a horrible idea. That way multiple arbitrary vendors can be tested with a single test instruction. It would be pretty crazy to have 32 x86 vendors, never mind 64, without a *dramatic* shift in the ecosystem (there are currently fewer than 32 registered RISC-V G implementations; that doesn't include the myriad of microcontroller class implementations that don't implement the full G [general purpose] feature set – but those are irrelevant for Linux.)
-hpa
Borislav Petkov
maintainerRe: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
Quoted 21 lines · H. Peter Anvin
Quoted 20 lines · Dave Hansen
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Borislav Petkov
Quoted 2 lines · Ahmed S. Darwish
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce
x86_vendor_amd_or_hygon()at <asm/processor.h>Quoted 8 lines
Quoted 5 lines
So if we need to check "intel too", we do
x86_vendor_amd_or_hygon_or_intel?Nah, this is silly.
Would it make more sense to have a Zen1 feature and check that instead?
Because, IIRC Hygon is simply a Zen1 copy.
Some of them can just go away, I think. This, for instance:
if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD && boot_cpu_data.x86_vendor != X86_VENDOR_HYGON) return false;
Quoted 7 lines
Quoted 5 lines
if (cpu_feature_enabled(X86_FEATURE_ZEN)) return false;Do we even need a vendor check if we're checking
X86_FEATURE_ZEN? Is someone settingX86_FEATURE_ZENon Intel? ;)Maybe having a vendor bitmask instead of (or as an alternative to) an enumeration wouldn't be a horrible idea. That way multiple arbitrary vendors can be tested with a single test instruction. It would be pretty crazy to have 32 x86 vendors, never mind 64, without a *dramatic* shift in the ecosystem (there are currently fewer than 32 registered RISC-V G implementations; that doesn't include the myriad of microcontroller class implementations that don't implement the full G [general purpose] feature set – but those are irrelevant for Linux.)
All y'all - you do realize that all that is a bit too much.
What's the problem with having a two-vendor test instead of a silly wrapper around it?
Or is the need for such a wrapper so important that we absolutely need it?
Did that thing cause a bug somewhere I didn't catch or so and we absolutely need a wrapper so that we don't forget one or the other?
Also, no one has Hygon hw so we can't just lump those together, strictly speaking. Whoever lumps them, needs to test on both.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAhmed S. Darwish
reviewerRe: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
In reply to Borislav PetkovHi,
Quoted 11 lines · Borislav Petkov
Quoted 2 lines · Ahmed S. Darwish
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce
x86_vendor_amd_or_hygon()at <asm/processor.h>So if we need to check "intel too", we do
x86_vendor_amd_or_hygon_or_intel?Nah, this is silly.
I needed this while refactoring the cacheinfo.c leaf 0x8000001d code at patch 11/29 ("x86/cacheinfo: Consolidate AMD/Hygon leaf 0x8000001d calls") as the combined check was done multiple times.
Then I found that there are 28 other cases in the x86 tree where the AMD/Hygon CPU vendor check is also combined. So I did that macro and it also made a number the affected sites more succinct; e.g.:
| diff --git a/arch/x86/xen/enlighten.c b/arch/x86/xen/enlighten.c | index 43dcd8c7badc..13df4917d7d8 100644 | --- a/arch/x86/xen/enlighten.c | +++ b/arch/x86/xen/enlighten.c | @@ -82,11 +82,9 @@ void xen_hypercall_setfunc(void) | if (static_call_query(xen_hypercall) != xen_hypercall_hvm) | return; | | - if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD || | - boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) | - static_call_update(xen_hypercall, xen_hypercall_amd); | - else | - static_call_update(xen_hypercall, xen_hypercall_intel); | + static_call_update(xen_hypercall, | + x86_vendor_amd_or_hygon(boot_cpu_data.x86_vendor) ? | + xen_hypercall_amd : xen_hypercall_intel); | } | | /* | @@ -118,11 +116,8 @@ noinstr void *__xen_hypercall_setfunc(void) | if (!boot_cpu_has(X86_FEATURE_CPUID)) | xen_get_vendor(); | | - if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD || | - boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) | - func = xen_hypercall_amd; | - else | - func = xen_hypercall_intel; | + func = x86_vendor_amd_or_hygon(boot_cpu_data.x86_vendor) ? | + xen_hypercall_amd : xen_hypercall_intel; | | static_call_update_early(xen_hypercall, func);
Nonetheless, I've seen your other emails in the thread, and I'll drop the patch.
Thanks!
--
Ahmed S. Darwish
Linutronix GmbH
H. Peter Anvin
reviewerRe: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
Quoted 56 lines · Ahmed S. Darwish
Hi,
Quoted 11 lines · Borislav Petkov
Quoted 2 lines · Ahmed S. Darwish
The pattern to check if an x86 vendor is AMD or HYGON (or not both) is pretty common. Introduce
x86_vendor_amd_or_hygon()at <asm/processor.h>So if we need to check "intel too", we do
x86_vendor_amd_or_hygon_or_intel?Nah, this is silly.
I needed this while refactoring the
cacheinfo.cleaf0x8000001dcode at patch 11/29 ("x86/cacheinfo: Consolidate AMD/Hygon leaf0x8000001dcalls") as the combined check was done multiple times.Then I found that there are 28 other cases in the x86 tree where the AMD/Hygon CPU vendor check is also combined. So I did that macro and it also made a number the affected sites more succinct; e.g.:
| diff --git a/arch/x86/xen/enlighten.c b/arch/x86/xen/enlighten.c | index 43dcd8c7badc..13df4917d7d8 100644 | --- a/arch/x86/xen/enlighten.c | +++ b/arch/x86/xen/enlighten.c | @@ -82,11 +82,9 @@ void xen_hypercall_setfunc(void) | if (static_call_query(xen_hypercall) != xen_hypercall_hvm) | return; | | - if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD || | - boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) | - static_call_update(xen_hypercall, xen_hypercall_amd); | - else | - static_call_update(xen_hypercall, xen_hypercall_intel); | + static_call_update(xen_hypercall, | + x86_vendor_amd_or_hygon(boot_cpu_data.x86_vendor) ? | + xen_hypercall_amd : xen_hypercall_intel); | } | | /* | @@ -118,11 +116,8 @@ noinstr void *__xen_hypercall_setfunc(void) | if (!boot_cpu_has(X86_FEATURE_CPUID)) | xen_get_vendor(); | | - if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD || | - boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)) | - func = xen_hypercall_amd; | - else | - func = xen_hypercall_intel; | + func = x86_vendor_amd_or_hygon(boot_cpu_data.x86_vendor) ? | + xen_hypercall_amd : xen_hypercall_intel; | | static_call_update_early(xen_hypercall, func);
Quoted 8 lines
Nonetheless, I've seen your other emails in the thread, and I'll drop the patch.
Thanks!
--
Ahmed S. Darwish
Linutronix GmbH
I would agree that abstracting this into something higher level makes sense, but have you considered whether or not it is actually necessary to do this in the first place? In the case of level 0x8000001d for example, that should be handled by the end bracket from leaf 0x80000000.
In general, VFMS checks are not a good thing.
Ahmed S. Darwish
reviewerRe: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
Quoted 9 lines · H. Peter Anvin
Quoted 6 lines · Ahmed S. Darwish
Then I found that there are 28 other cases in the x86 tree where the AMD/Hygon CPU vendor check is also combined. So I did that macro and it also made a number the affected sites more succinct; e.g.:
...
Quoted 8 lines
I would agree that abstracting this into something higher level makes sense, but have you considered whether or not it is actually necessary to do this in the first place? In the case of level
0x8000001dfor example, that should be handled by the end bracket from leaf0x80000000.In general, VFMS checks are not a good thing.
Great point, I'll consider that when I (soon) touch these areas again.
In the next PQ, wherea cached CPUID table built on early boot, "CPUID truth" is centralized. This way, the VMFS checks can indeed be reduced.
Thanks!
--
Ahmed S. Darwish
Linutronix GmbH
Re: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
In reply to Ahmed S. Darwisharch/x86/events/amd/uncore.c static int __init amd_uncore_init(void) · line 1027
Quoted 4 lines · Ahmed S. Darwish
arch/x86/events/amd/uncore.c +1
-2-if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return -ENODEV ;
Could we just not do this?
The above vendor check may or may not be equivalent X86_FEATURE_ZEN1, which we could first assert via a single boot-time check:
WARN_ON_ONCE(boot_cpu_has(X86_FEATURE_ZEN1) != ((boot_cpu_data.x86_vendor != X86_VENDOR_AMD && boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)));
And then replace all the above with the much simpler construct of:
if (!boot_cpu_has(X86_FEATURE_ZEN1)) return -ENODEV;
But I'd suggest keeping this patch at the tail of the series, because if it regresses it would be easier to drop/revert.
Thanks,
Ingo
Borislav Petkov
maintainerRe: [PATCH v2 01/29] x86: treewide: Introduce x86_vendor_amd_or_hygon()
Quoted 10 lines · Ingo Molnar
arch/x86/events/amd/uncore.c static int __init amd_uncore_init(void) · line 1027
Quoted 4 lines · Ahmed S. Darwish
arch/x86/events/amd/uncore.c +1
-2-if (boot_cpu_data.x86_vendor !=X86_VENDOR_AMD && - boot_cpu_data.x86_vendor !=X86_VENDOR_HYGON ) +if (!x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor))return -ENODEV ;Could we just not do this?
Quoted 6 lines
The above vendor check may or may not be equivalent
X86_FEATURE_ZEN1, which we could first assert via a single boot-time check:WARN_ON_ONCE(boot_cpu_has(X86_FEATURE_ZEN1) != ((boot_cpu_data.x86_vendor != X86_VENDOR_AMD && boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)));
No, it's not.
Hygon is family 0x18 AFAIK and ZEN1 is not.
Just drop this patch.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAhmed S. Darwish
reviewerPATCH v2 2/29 x86/cpuid: Refactor <asm/cpuid.h>
In reply to Ahmed S. DarwishIn preparation for next commits where CPUID headers will be expanded, refactor the CPUID header <asm/cpuid.h> into:
asm/cpuid/
├── api.h
└── types.h
Move the CPUID data structures into <asm/cpuid/types.h> and the access APIs into <asm/cpuid/api.h>. Let <asm/cpuid.h> be just an include of <asm/cpuid/api.h> so that existing call sites do not break.
--- arch/x86/include/asm/cpuid.h | 217 +---------------------------- arch/x86/include/asm/cpuid/api.h | 208 +++++++++++++++++++++++++++ arch/x86/include/asm/cpuid/types.h | 29 ++++ 3 files changed, 238 insertions(+), 216 deletions(-) create mode 100644 arch/x86/include/asm/cpuid/api.h create mode 100644 arch/x86/include/asm/cpuid/types.h
arch/x86/include/asm/cpuid.h +1 -216
@@ -1,223 +1,8 @@/* SPDX-License-Identifier: GPL-2.0 */ -/* -* CPUID-related helpers/definitions -*/ #ifndef _ASM_X86_CPUID_H#define _ASM_X86_CPUID_H -#include <linux/build_bug.h> -#include <linux/types.h> - -#include <asm/string.h> - -struct cpuid_regs { -u32 eax, ebx, ecx, edx; -}; - -enum cpuid_regs_idx { -CPUID_EAX =0 , -CPUID_EBX , -CPUID_ECX , -CPUID_EDX , -}; - -#define CPUID_LEAF_MWAIT 0x5 -#define CPUID_LEAF_DCA 0x9 -#define CPUID_LEAF_XSTATE 0x0d -#define CPUID_LEAF_TSC 0x15 -#define CPUID_LEAF_FREQ 0x16 -#define CPUID_LEAF_TILE 0x1d - -#ifdef CONFIG_X86_32 -bool have_cpuid_p (void ); -#else -static inline bool have_cpuid_p (void ) -{ -return true; -} -#endif -static inline void native_cpuid (unsigned int *eax,unsigned int *ebx, -unsigned int *ecx,unsigned int *edx) -{ -/* ecx is often an input as well as an output. */ - asmvolatile ("cpuid" - :"=a" (*eax), -"=b" (*ebx), -"=c" (*ecx), -"=d" (*edx) - :"0" (*eax),"2" (*ecx) - :"memory" ); -} - -#define native_cpuid_reg (reg) \ -static inline unsigned int native_cpuid_##reg(unsigned int op) \ -{ \ - unsigned int eax = op, ebx, ecx = 0, edx; \ - \ - native_cpuid(&eax, &ebx, &ecx, &edx); \ - \ - return reg; \ -} - -/* -* Native CPUID functions returning a single datum. -*/ -native_cpuid_reg(eax) -native_cpuid_reg(ebx) -native_cpuid_reg(ecx) -native_cpuid_reg(edx) - -#ifdef CONFIG_PARAVIRT_XXL -#include <asm/paravirt.h> -#else -#define __cpuid native_cpuid -#endif - -/* -* Generic CPUID function -* clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx -* resulting in stale register contents being returned. -*/ -static inline void cpuid (unsigned int op, -unsigned int *eax,unsigned int *ebx, -unsigned int *ecx,unsigned int *edx) -{ - *eax = op; - *ecx =0 ; -__cpuid (eax, ebx, ecx, edx); -} - -/* Some CPUID calls want 'count' to be placed in ecx */ -static inline void cpuid_count (unsigned int op,int count, -unsigned int *eax,unsigned int *ebx, -unsigned int *ecx,unsigned int *edx) -{ - *eax = op; - *ecx = count; -__cpuid (eax, ebx, ecx, edx); -} - -/* -* CPUID functions returning a single datum -*/ -static inline unsigned int cpuid_eax (unsigned int op) -{ -unsigned int eax, ebx, ecx, edx; - -cpuid (op, &eax, &ebx, &ecx, &edx); - -return eax; -} - -static inline unsigned int cpuid_ebx (unsigned int op) -{ -unsigned int eax, ebx, ecx, edx; - -cpuid (op, &eax, &ebx, &ecx, &edx); - -return ebx; -} - -static inline unsigned int cpuid_ecx (unsigned int op) -{ -unsigned int eax, ebx, ecx, edx; - -cpuid (op, &eax, &ebx, &ecx, &edx); - -return ecx; -} - -static inline unsigned int cpuid_edx (unsigned int op) -{ -unsigned int eax, ebx, ecx, edx; - -cpuid (op, &eax, &ebx, &ecx, &edx); - -return edx; -} - -static inline void __cpuid_read (unsigned int leaf,unsigned int subleaf,u32 *regs) -{ - regs[CPUID_EAX ] = leaf; - regs[CPUID_ECX ] = subleaf; -__cpuid (regs +CPUID_EAX , regs +CPUID_EBX , regs +CPUID_ECX , regs +CPUID_EDX ); -} - -#define cpuid_subleaf (leaf, subleaf, regs) { \ - static_assert(sizeof(*(regs)) == 16); \ - __cpuid_read(leaf, subleaf, (u32 *)(regs)); \ -} - -#define cpuid_leaf (leaf, regs) { \ - static_assert(sizeof(*(regs)) == 16); \ - __cpuid_read(leaf, 0, (u32 *)(regs)); \ -} - -static inline void __cpuid_read_reg (unsigned int leaf,unsigned int subleaf, -enum cpuid_regs_idx regidx,u32 *reg) -{ -u32 regs[4 ]; - -__cpuid_read (leaf, subleaf, regs); - *reg = regs[regidx]; -} - -#define cpuid_subleaf_reg (leaf, subleaf, regidx, reg) { \ - static_assert(sizeof(*(reg)) == 4); \ - __cpuid_read_reg(leaf, subleaf, regidx, (u32 *)(reg)); \ -} - -#define cpuid_leaf_reg (leaf, regidx, reg) { \ - static_assert(sizeof(*(reg)) == 4); \ - __cpuid_read_reg(leaf, 0, regidx, (u32 *)(reg)); \ -} - -static __always_inline bool cpuid_function_is_indexed(u32 function) -{ -switch (function) { -case 4 : -case 7 : -case 0xb : -case 0xd : -case 0xf : -case 0x10 : -case 0x12 : -case 0x14 : -case 0x17 : -case 0x18 : -case 0x1d : -case 0x1e : -case 0x1f : -case 0x24 : -case 0x8000001d : -return true; - } - -return false; -} - -#define for_each_possible_hypervisor_cpuid_base (function) \ - for (function = 0x40000000; function < 0x40010000; function += 0x100) - -static inline uint32_t hypervisor_cpuid_base (const char *sig,uint32_t leaves) -{ -uint32_t base, eax, signature[3 ]; - -for_each_possible_hypervisor_cpuid_base (base) { -cpuid (base, &eax, &signature[0 ], &signature[1 ], &signature[2 ]); - -/* -* This must not compile to "call memcmp" because it's called -* from PVH early boot code before instrumentation is set up -* and memcmp() itself may be instrumented. -*/ -if (!__builtin_memcmp (sig, signature,12 ) && - (leaves ==0 || ((eax - base) >= leaves))) -return base; - } - -return 0 ; -} +#include <asm/cpuid/api.h> #endif /* _ASM_X86_CPUID_H */
arch/x86/include/asm/cpuid/api.h new file +208 -0
208 lines not on this page · raw message
arch/x86/include/asm/cpuid/types.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_CPUID_TYPES_H +#define _ASM_X86_CPUID_TYPES_H + +#include <linux/types.h> + +/* +* Types for raw CPUID access +*/ + +struct cpuid_regs { +u32 eax, ebx, ecx, edx; +}; + +enum cpuid_regs_idx { +CPUID_EAX =0 , +CPUID_EBX , +CPUID_ECX , +CPUID_EDX , +}; + +#define CPUID_LEAF_MWAIT 0x5 +#define CPUID_LEAF_DCA 0x9 +#define CPUID_LEAF_XSTATE 0x0d +#define CPUID_LEAF_TSC 0x15 +#define CPUID_LEAF_FREQ 0x16 +#define CPUID_LEAF_TILE 0x1d + +#endif /* _ASM_X86_CPUID_TYPES_H */
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 3/29 x86/cpu: Remove leaf 0x2 parsing loop and add helpers
In reply to Ahmed S. DarwishLeaf 0x2 output includes a "query count" byte where it was supposed to specify the number of repeated CPUID leaf 0x2 subleaf 0 queries needed to extract all of the hardware's cache and TLB descriptors.
Per current Intel manuals, all CPUs supporting this leaf "will always" return an iteration count of 1.
Remove the leaf 0x2 query count loop and just query the hardware once. Parse the output with C99 bitfields instead of ugly bitwise operations.
Provide leaf 0x2 parsing helpers with all the above. Use such helpers at x86/cpu intel.c. Further commits will use them for x86/cacheinfo.
--- arch/x86/include/asm/cpuid.h | 1 + arch/x86/include/asm/cpuid/leaf_0x2_api.h | 65 +++++++++++++++++++++++ arch/x86/include/asm/cpuid/types.h | 16 ++++++ arch/x86/kernel/cpu/intel.c | 24 +++------ 4 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 arch/x86/include/asm/cpuid/leaf_0x2_api.h
arch/x86/include/asm/cpuid.h +1 -0
@@ -4,5 +4,6 @@#define _ASM_X86_CPUID_H#include <asm/cpuid/api.h> +#include <asm/cpuid/leaf_0x2_api.h> #endif /* _ASM_X86_CPUID_H */
arch/x86/include/asm/cpuid/leaf_0x2_api.h new file +65 -0
65 lines not on this page · raw message
arch/x86/include/asm/cpuid/types.h +16 -0
@@ -26,4 +26,20 @@ enum cpuid_regs_idx {#define CPUID_LEAF_FREQ 0x16#define CPUID_LEAF_TILE 0x1d +/* +* Types for CPUID(0x2) parsing +* Check <asm/cpuid/leaf_0x2_api.h> +*/ + +struct leaf_0x2_reg { + u32 : 31, + invalid :1 ; +}; + +union leaf_0x2_regs { +struct leaf_0x2_reg reg[4 ]; +u32 regv[4 ]; +u8 desc[16 ]; +}; +#endif /* _ASM_X86_CPUID_TYPES_H */
arch/x86/kernel/cpu/intel.c +6 -18
@@ -15,6 +15,7 @@#include <asm/cpu_device_id.h> #include <asm/cpufeature.h> #include <asm/cpu.h> +#include <asm/cpuid.h> #include <asm/hwcap2.h> #include <asm/intel-family.h> #include <asm/microcode.h>
@@ -779,28 +780,15 @@ static void intel_tlb_lookup(const unsigned char desc)static void intel_detect_tlb (struct cpuinfo_x86 *c) { -int i, j, n; -unsigned int regs[4 ]; -unsigned char *desc = (unsigned char *)regs; +union leaf_0x2_regs regs; +u8 *desc;if (c->cpuid_level <2 )return ; -/* Number of times to iterate */ - n =cpuid_eax (2 ) &0xFF ; - -for (i =0 ; i < n ; i++) { -cpuid (2 , ®s[0 ], ®s[1 ], ®s[2 ], ®s[3 ]); - -/* If bit 31 is set, this is an unknown format */ -for (j =0 ; j <4 ; j++) -if (regs[j] & (1 <<31 )) - regs[j] =0 ; - -/* Byte 0 is level count, not a descriptor */ -for (j =1 ; j <16 ; j++) -intel_tlb_lookup (desc[j]); - } +cpuid_get_leaf_0x2_regs (®s); +for_each_leaf_0x2_desc (regs, desc) +intel_tlb_lookup (*desc); }static const struct cpu_dev intel_cpu_dev = {
Signature · 1 line
2.48.1
Re: [PATCH v2 03/29] x86/cpu: Remove leaf 0x2 parsing loop and add helpers
Quoted 15 lines · Ahmed S. Darwish
Leaf
0x2output includes a "query count" byte where it was supposed to specify the number of repeated CPUID leaf0x2subleaf 0 queries needed to extract all of the hardware's cache and TLB descriptors.Per current Intel manuals, all CPUs supporting this leaf "will always" return an iteration count of 1.
Remove the leaf
0x2query count loop and just query the hardware once. Parse the output with C99 bitfields instead of ugly bitwise operations.Provide leaf
0x2parsing helpers with all the above. Use such helpers at x86/cpuintel.c. Further commits will use them for x86/cacheinfo.Suggested-by Thomas Gleixner <[email protected]>Signed-off-by Ahmed S. Darwish <[email protected]>
Quoted 7 lines
--- arch/x86/include/asm/cpuid.h | 1 + arch/x86/include/asm/cpuid/leaf_0x2_api.h | 65 +++++++++++++++++++++++ arch/x86/include/asm/cpuid/types.h | 16 ++++++ arch/x86/kernel/cpu/intel.c | 24 +++------ 4 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 arch/x86/include/asm/cpuid/leaf_0x2_api.h
Could you please turn this into at least two patches:
- One does the functional change - this patch should be as small as possible. This patch turns a piece of 'Intel documents' property into 'actual code' that differs from the previous code, which may or may not regress in practice.
- The other does the factoring out and the introduction of
<asm/cpuid/leaf_0x2_api.h>.
Thanks,
Ingo
Ahmed S. Darwish
reviewerRe: [PATCH v2 03/29] x86/cpu: Remove leaf 0x2 parsing loop and add helpers
Quoted 11 lines · Ingo Molnar
Could you please turn this into at least two patches:
- One does the functional change - this patch should be as small as possible. This patch turns a piece of 'Intel documents' property into 'actual code' that differs from the previous code, which may or may not regress in practice.
- The other does the factoring out and the introduction of
<asm/cpuid/leaf_0x2_api.h>.
Sure, will do.
Thanks!
Ahmed
Ahmed S. Darwish
reviewerPATCH v2 4/29 x86/cacheinfo: Use CPUID leaf 0x2 parsing helpers
In reply to Ahmed S. DarwishUse the CPUID leaf 0x2 parsing helpers added in previous commits, which queries the CPUID leaf just once. This also makes the same leaf 0x2 parsing logic used by both x86/cacheinfo and x86/cpu intel.c
--- arch/x86/kernel/cpu/cacheinfo.c | 66 +++++++++++++-------------------- 1 file changed, 26 insertions(+), 40 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +26 -40
@@ -19,6 +19,7 @@#include <asm/amd_nb.h> #include <asm/cacheinfo.h> #include <asm/cpufeature.h> +#include <asm/cpuid.h> #include <asm/mtrr.h> #include <asm/smp.h> #include <asm/tlbflush.h>
@@ -782,50 +783,35 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)/* Don't use CPUID(2) if CPUID(4) is supported. */ if (!ci->num_leaves && c->cpuid_level >1 ) { -/* supports eax=2 call */ -int j, n; -unsigned int regs[4 ]; -unsigned char *dp = (unsigned char *)regs; - -/* Number of times to iterate */ - n =cpuid_eax (2 ) &0xFF ; - -for (i =0 ; i < n ; i++) { -cpuid (2 , ®s[0 ], ®s[1 ], ®s[2 ], ®s[3 ]); - -/* If bit 31 is set, this is an unknown format */ -for (j =0 ; j <4 ; j++) -if (regs[j] & (1 <<31 )) - regs[j] =0 ; - -/* Byte 0 is level count, not a descriptor */ -for (j =1 ; j <16 ; j++) { -unsigned char des = dp[j]; -unsigned char k =0 ; - -/* look up this descriptor in the table */ -while (cache_table[k].descriptor !=0 ) { -if (cache_table[k].descriptor == des) { -switch (cache_table[k].cache_type) { -case LVL_1_INST : - l1i += cache_table[k].size; -break ; -case LVL_1_DATA : - l1d += cache_table[k].size; -break ; -case LVL_2 : - l2 += cache_table[k].size; -break ; -case LVL_3 : - l3 += cache_table[k].size; -break ; - } - +union leaf_0x2_regs regs; +u8 *desc; + +cpuid_get_leaf_0x2_regs (®s); +for_each_leaf_0x2_desc (regs, desc) { +unsigned char k =0 ; + +/* look up this descriptor in the table */ +while (cache_table[k].descriptor !=0 ) { +if (cache_table[k].descriptor == *desc) { +switch (cache_table[k].cache_type) { +case LVL_1_INST : + l1i += cache_table[k].size; +break ; +case LVL_1_DATA : + l1d += cache_table[k].size; +break ; +case LVL_2 : + l2 += cache_table[k].size; +break ; +case LVL_3 : + l3 += cache_table[k].size;break ; } - k++; +break ; } + + k++; } } }
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 5/29 x86/cacheinfo: Refactor leaf 0x2 cache descriptor lookup
In reply to Ahmed S. DarwishFrom: Thomas Gleixner <[email protected]>
Extract the cache descriptor lookup logic out of the leaf 0x2 parsing code and into a dedicated function. This disentangles such lookup from the deeply nested leaf 0x2 parsing loop.
Remove the cache table termination entry, as it is no longer needed after the ARRAY_SIZE()-based lookup.
[ darwi: Move refactoring logic into this separate commit + commit log.
Remove the cache table termination entry. ]
--- arch/x86/kernel/cpu/cacheinfo.c | 45 +++++++++++++++------------------ 1 file changed, 20 insertions(+), 25 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +20 -25
@@ -123,7 +123,6 @@ static const struct _cache_table cache_table[] = { 0xea, LVL_3, MB(12) }, /* 24-way set assoc, 64 byte line size */ { 0xeb, LVL_3, MB(18) }, /* 24-way set assoc, 64 byte line size */ { 0xec, LVL_3, MB(24) }, /* 24-way set assoc, 64 byte line size */ - { 0x00, 0, 0} };
@@ -727,6 +726,16 @@ void init_hygon_cacheinfo(struct cpuinfo_x86 *c) ci->num_leaves =find_num_cache_leaves (c); } +static const struct _cache_table *cache_table_get (u8 desc) +{ +for (int i =0 ; i <ARRAY_SIZE (cache_table); i++) { +if (cache_table[i].descriptor == desc) +return &cache_table[i]; + } + +return NULL ; +} +void init_intel_cacheinfo (struct cpuinfo_x86 *c) {/* Cache sizes */
@@ -783,35 +792,21 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)/* Don't use CPUID(2) if CPUID(4) is supported. */ if (!ci->num_leaves && c->cpuid_level >1 ) { +const struct _cache_table *entry;union leaf_0x2_regs regs;u8 *desc;cpuid_get_leaf_0x2_regs (®s);for_each_leaf_0x2_desc (regs, desc) { -unsigned char k =0 ; - -/* look up this descriptor in the table */ -while (cache_table[k].descriptor !=0 ) { -if (cache_table[k].descriptor == *desc) { -switch (cache_table[k].cache_type) { -case LVL_1_INST : - l1i += cache_table[k].size; -break ; -case LVL_1_DATA : - l1d += cache_table[k].size; -break ; -case LVL_2 : - l2 += cache_table[k].size; -break ; -case LVL_3 : - l3 += cache_table[k].size; -break ; - } - -break ; - } - - k++; + entry =cache_table_get (*desc); +if (!entry) +continue ; + +switch (entry->cache_type) { +case LVL_1_INST : l1i += entry->size;break ; +case LVL_1_DATA : l1d += entry->size;break ; +case LVL_2 : l2 += entry->size;break ; +case LVL_3 : l3 += entry->size;break ; } } }
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 6/29 x86/cacheinfo: Properly name amd_cpuid4()'s first parameter
In reply to Ahmed S. DarwishFrom: Thomas Gleixner <[email protected]>
amd_cpuid4()'s first parameter, "leaf", is not a CPUID leaf as the name implies. Rather, it's an index emulating CPUID(4)'s subleaf semantics; i.e. an ID for the cache object currently enumerated. Rename that parameter to "index".
Apply minor coding style fixes to the rest of the function as well.
[ darwi: Move into a separate commit and write commit log.
Use "index" instead of "subleaf" for amd_cpuid4() first param, as that's the name typically used at the whole of cacheinfo.c. ]
--- arch/x86/kernel/cpu/cacheinfo.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +6 -9
@@ -233,12 +233,10 @@ static const enum cache_type cache_type_map[] = { };static void -amd_cpuid4 (int leaf,union _cpuid4_leaf_eax *eax, -union _cpuid4_leaf_ebx *ebx, -union _cpuid4_leaf_ecx *ecx) +amd_cpuid4 (int index,union _cpuid4_leaf_eax *eax, +union _cpuid4_leaf_ebx *ebx,union _cpuid4_leaf_ecx *ecx) { -unsigned dummy; -unsigned line_size, lines_per_tag, assoc, size_in_kb; +unsigned int dummy, line_size, lines_per_tag, assoc, size_in_kb;union l1_cache l1i, l1d;union l2_cache l2;union l3_cache l3;
@@ -251,7 +249,7 @@ amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax,cpuid (0x80000005 , &dummy, &dummy, &l1d.val, &l1i.val);cpuid (0x80000006 , &dummy, &dummy, &l2.val, &l3.val); -switch (leaf) { +switch (index) {case 1 : l1 = &l1i; fallthrough;
@@ -289,12 +287,11 @@ amd_cpuid4(int leaf, union _cpuid4_leaf_eax *eax, } eax->split.is_self_initializing =1 ; - eax->split.type = types[leaf]; - eax->split.level = levels[leaf]; + eax->split.type = types[index]; + eax->split.level = levels[index]; eax->split.num_threads_sharing =0 ; eax->split.num_cores_on_die =topology_num_cores_per_package (); -if (assoc ==0xffff ) eax->split.is_fully_associative =1 ; ebx->split.coherency_line_size = line_size -1 ;
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 7/29 x86/cacheinfo: Use proper name for cacheinfo instances
In reply to Ahmed S. DarwishFrom: Thomas Gleixner <[email protected]>
The cacheinfo structure defined at <include/linux/cacheinfo.h> is a generic cache info object representation.
Calling its instances at x86 cacheinfo.c "leaf" confuses it with a CPUID leaf -- especially that multiple CPUID calls are already sprinkled across that file. Most of such instances also have a redundant "this_" prefix.
Rename all of the cacheinfo "this_leaf" instances to just "ci".
[ darwi: Move into separate commit and write commit log ]
--- arch/x86/kernel/cpu/cacheinfo.c | 89 ++++++++++++++++----------------- 1 file changed, 43 insertions(+), 46 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +43 -46
@@ -349,11 +349,10 @@ static int amd_get_l3_disable_slot(struct amd_northbridge *nb, unsigned slot)return -1 ; } -static ssize_t show_cache_disable (struct cacheinfo *this_leaf,char *buf, -unsigned int slot) +static ssize_t show_cache_disable (struct cacheinfo *ci,char *buf,unsigned int slot) {int index; -struct amd_northbridge *nb = this_leaf->priv; +struct amd_northbridge *nb = ci->priv; index =amd_get_l3_disable_slot (nb, slot);if (index >=0 )
@@ -367,8 +366,8 @@ static ssize_t \ cache_disable_##slot##_show(struct device *dev, \struct device_attribute *attr,char *buf) \ { \ -struct cacheinfo *this_leaf =dev_get_drvdata (dev); \ -return show_cache_disable (this_leaf, buf, slot); \ +struct cacheinfo *ci =dev_get_drvdata (dev); \ +return show_cache_disable (ci, buf, slot); \ } SHOW_CACHE_DISABLE(0)SHOW_CACHE_DISABLE (1 )
@@ -435,18 +434,17 @@ static int amd_set_l3_disable_slot(struct amd_northbridge *nb, int cpu,return 0 ; } -static ssize_t store_cache_disable (struct cacheinfo *this_leaf, -const char *buf,size_t count, -unsigned int slot) +static ssize_t store_cache_disable (struct cacheinfo *ci,const char *buf, +size_t count,unsigned int slot) {unsigned long val =0 ;int cpu, err =0 ; -struct amd_northbridge *nb = this_leaf->priv; +struct amd_northbridge *nb = ci->priv;if (!capable (CAP_SYS_ADMIN ))return -EPERM ; - cpu =cpumask_first (&this_leaf->shared_cpu_map); + cpu =cpumask_first (&ci->shared_cpu_map);if (kstrtoul (buf,10 , &val) <0 )return -EINVAL ;
@@ -467,8 +465,8 @@ cache_disable_##slot##_store(struct device *dev, \struct device_attribute *attr, \ const char *buf, size_t count) \ { \ - struct cacheinfo *this_leaf = dev_get_drvdata(dev); \ -return store_cache_disable (this_leaf, buf, count, slot); \ + struct cacheinfo *ci = dev_get_drvdata(dev); \ +return store_cache_disable (ci, buf, count, slot); \ } STORE_CACHE_DISABLE(0)STORE_CACHE_DISABLE (1 )
@@ -476,8 +474,8 @@ STORE_CACHE_DISABLE(1)static ssize_t subcaches_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct cacheinfo *this_leaf =dev_get_drvdata (dev); -int cpu =cpumask_first (&this_leaf->shared_cpu_map); +struct cacheinfo *ci =dev_get_drvdata (dev); +int cpu =cpumask_first (&ci->shared_cpu_map);return sprintf (buf,"%x\n" ,amd_get_subcaches (cpu)); }
@@ -486,8 +484,8 @@ static ssize_t subcaches_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t count) { - struct cacheinfo *this_leaf = dev_get_drvdata(dev); -int cpu =cpumask_first (&this_leaf->shared_cpu_map); + struct cacheinfo *ci = dev_get_drvdata(dev); +int cpu =cpumask_first (&ci->shared_cpu_map);unsigned long val;if (!capable (CAP_SYS_ADMIN ))
@@ -511,10 +509,10 @@ cache_private_attrs_is_visible(struct kobject *kobj, struct attribute *attr, int unused) {struct device *dev =kobj_to_dev (kobj); -struct cacheinfo *this_leaf =dev_get_drvdata (dev); +struct cacheinfo *ci =dev_get_drvdata (dev);umode_t mode = attr->mode; -if (!this_leaf->priv) +if (!ci->priv)return 0 ; if ((attr == &dev_attr_subcaches.attr) &&
@@ -562,11 +560,11 @@ static void init_amd_l3_attrs(void) }const struct attribute_group * -cache_get_priv_group (struct cacheinfo *this_leaf) +cache_get_priv_group (struct cacheinfo *ci) { -struct amd_northbridge *nb = this_leaf->priv; +struct amd_northbridge *nb = ci->priv; -if (this_leaf->level <3 || !nb) +if (ci->level <3 || !nb)return NULL ;if (nb && nb->l3_cache.indices)
@@ -845,7 +843,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index, struct _cpuid4_info_regs *base) {struct cpu_cacheinfo *this_cpu_ci; -struct cacheinfo *this_leaf; +struct cacheinfo *ci;int i, sibling;/*
@@ -857,12 +855,12 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index, this_cpu_ci =get_cpu_cacheinfo (i);if (!this_cpu_ci->info_list)continue ; - this_leaf = this_cpu_ci->info_list + index; + ci = this_cpu_ci->info_list + index;for_each_cpu (sibling,cpu_llc_shared_mask (cpu)) {if (!cpu_online (sibling))continue ;cpumask_set_cpu (sibling, - &this_leaf->shared_cpu_map); + &ci->shared_cpu_map); } } }else if (boot_cpu_has (X86_FEATURE_TOPOEXT )) {
@@ -882,14 +880,14 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index,if ((apicid < first) || (apicid > last))continue ; - this_leaf = this_cpu_ci->info_list + index; + ci = this_cpu_ci->info_list + index;for_each_online_cpu (sibling) { apicid =cpu_data (sibling).topo.apicid;if ((apicid < first) || (apicid > last))continue ;cpumask_set_cpu (sibling, - &this_leaf->shared_cpu_map); + &ci->shared_cpu_map); } } }else
@@ -902,7 +900,7 @@ static void __cache_cpumap_setup(unsigned int cpu, int index, struct _cpuid4_info_regs *base) {struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu); -struct cacheinfo *this_leaf, *sibling_leaf; +struct cacheinfo *ci, *sibling_ci;unsigned long num_threads_sharing;int index_msb, i;struct cpuinfo_x86 *c = &cpu_data (cpu);
@@ -912,10 +910,10 @@ static void __cache_cpumap_setup(unsigned int cpu, int index,return ; } - this_leaf = this_cpu_ci->info_list + index; + ci = this_cpu_ci->info_list + index; num_threads_sharing =1 + base->eax.split.num_threads_sharing; -cpumask_set_cpu (cpu, &this_leaf->shared_cpu_map); +cpumask_set_cpu (cpu, &ci->shared_cpu_map);if (num_threads_sharing ==1 )return ;
@@ -927,28 +925,27 @@ static void __cache_cpumap_setup(unsigned int cpu, int index,if (i == cpu || !sib_cpu_ci->info_list)continue ;/* skip if itself or no cacheinfo */ - sibling_leaf = sib_cpu_ci->info_list + index; -cpumask_set_cpu (i, &this_leaf->shared_cpu_map); -cpumask_set_cpu (cpu, &sibling_leaf->shared_cpu_map); + sibling_ci = sib_cpu_ci->info_list + index; +cpumask_set_cpu (i, &ci->shared_cpu_map); +cpumask_set_cpu (cpu, &sibling_ci->shared_cpu_map); } } -static void ci_leaf_init (struct cacheinfo *this_leaf, -struct _cpuid4_info_regs *base) +static void ci_info_init (struct cacheinfo *ci,struct _cpuid4_info_regs *base) { - this_leaf->id = base->id; - this_leaf->attributes =CACHE_ID ; - this_leaf->level = base->eax.split.level; - this_leaf->type = cache_type_map[base->eax.split.type]; - this_leaf->coherency_line_size = + ci->id = base->id; + ci->attributes =CACHE_ID ; + ci->level = base->eax.split.level; + ci->type = cache_type_map[base->eax.split.type]; + ci->coherency_line_size = base->ebx.split.coherency_line_size +1 ; - this_leaf->ways_of_associativity = + ci->ways_of_associativity = base->ebx.split.ways_of_associativity +1 ; - this_leaf->size = base->size; - this_leaf->number_of_sets = base->ecx.split.number_of_sets +1 ; - this_leaf->physical_line_partition = + ci->size = base->size; + ci->number_of_sets = base->ecx.split.number_of_sets +1 ; + ci->physical_line_partition = base->ebx.split.physical_line_partition +1 ; - this_leaf->priv = base->nb; + ci->priv = base->nb; }int init_cache_level (unsigned int cpu)
@@ -982,7 +979,7 @@ int populate_cache_leaves(unsigned int cpu) {unsigned int idx, ret;struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu); -struct cacheinfo *this_leaf = this_cpu_ci->info_list; +struct cacheinfo *ci = this_cpu_ci->info_list;struct _cpuid4_info_regs id4_regs = {};for (idx =0 ; idx < this_cpu_ci->num_leaves; idx++) {
@@ -990,7 +987,7 @@ int populate_cache_leaves(unsigned int cpu)if (ret)return ret;get_cache_id (cpu, &id4_regs); -ci_leaf_init (this_leaf++, &id4_regs); +ci_info_init (ci++, &id4_regs);__cache_cpumap_setup (cpu, idx, &id4_regs); } this_cpu_ci->cpu_map_populated = true;
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 8/29 x86/cacheinfo: Constify _cpuid4_info_regs instances
In reply to Ahmed S. Darwish_cpuid4_info_regs instances are passed through a large number of functions at cacheinfo.c. For clarity, constify the instance parameters where _cpuid4_info_regs is only read from.
--- arch/x86/kernel/cpu/cacheinfo.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +4 -3
@@ -840,7 +840,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) }static int __cache_amd_cpumap_setup (unsigned int cpu,int index, -struct _cpuid4_info_regs *base) +const struct _cpuid4_info_regs *base) {struct cpu_cacheinfo *this_cpu_ci;struct cacheinfo *ci;
@@ -897,7 +897,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index, }static void __cache_cpumap_setup (unsigned int cpu,int index, -struct _cpuid4_info_regs *base) +const struct _cpuid4_info_regs *base) {struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu);struct cacheinfo *ci, *sibling_ci;
@@ -931,7 +931,8 @@ static void __cache_cpumap_setup(unsigned int cpu, int index, } } -static void ci_info_init (struct cacheinfo *ci,struct _cpuid4_info_regs *base) +static void ci_info_init (struct cacheinfo *ci, +const struct _cpuid4_info_regs *base) { ci->id = base->id; ci->attributes =CACHE_ID ;
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 9/29 x86/cacheinfo: Align ci_info_init() assignment expressions
In reply to Ahmed S. DarwishThe ci_info_init() function initializes 10 members of a struct cacheinfo instance using passed data from CPUID leaf 0x4.
Such assignment expressions are difficult to read in their current form. Align them for clarity.
--- arch/x86/kernel/cpu/cacheinfo.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +10 -13
@@ -934,19 +934,16 @@ static void __cache_cpumap_setup(unsigned int cpu, int index,static void ci_info_init (struct cacheinfo *ci,const struct _cpuid4_info_regs *base) { - ci->id = base->id; - ci->attributes =CACHE_ID ; - ci->level = base->eax.split.level; - ci->type = cache_type_map[base->eax.split.type]; - ci->coherency_line_size = - base->ebx.split.coherency_line_size +1 ; - ci->ways_of_associativity = - base->ebx.split.ways_of_associativity +1 ; - ci->size = base->size; - ci->number_of_sets = base->ecx.split.number_of_sets +1 ; - ci->physical_line_partition = - base->ebx.split.physical_line_partition +1 ; - ci->priv = base->nb; + ci->id = base->id; + ci->attributes =CACHE_ID ; + ci->level = base->eax.split.level; + ci->type = cache_type_map[base->eax.split.type]; + ci->coherency_line_size = base->ebx.split.coherency_line_size +1 ; + ci->ways_of_associativity = base->ebx.split.ways_of_associativity +1 ; + ci->size = base->size; + ci->number_of_sets = base->ecx.split.number_of_sets +1 ; + ci->physical_line_partition = base->ebx.split.physical_line_partition +1 ; + ci->priv = base->nb; }int init_cache_level (unsigned int cpu)
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 10/29 x86/cacheinfo: Standardize _cpuid4_info_regs instance naming
In reply to Ahmed S. DarwishThe cacheinfo code frequently uses the output registers from CPUID leaf 0x4. Such registers are cached at struct _cpuid4_info_regs, augmented with related information, and are then passed across functions.
The naming of these _cpuid4_info_regs instances is confusing at best.
Some instances are called "this_leaf", which is vague as "this" lacks context and "leaf" is overly generic given that other CPUID leaves are also processed within cacheinfo. Other _cpuid4_info_regs instances are just called "base", adding further ambiguity.
Standardize on id4 for all instances.
--- arch/x86/kernel/cpu/cacheinfo.c | 97 +++++++++++++++++---------------- 1 file changed, 49 insertions(+), 48 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +49 -48
@@ -573,7 +573,7 @@ cache_get_priv_group(struct cacheinfo *ci)return &cache_private_group; } -static void amd_init_l3_cache (struct _cpuid4_info_regs *this_leaf,int index) +static void amd_init_l3_cache (struct _cpuid4_info_regs *id4,int index) {int node;
@@ -582,16 +582,16 @@ static void amd_init_l3_cache(struct _cpuid4_info_regs *this_leaf, int index)return ; node =topology_amd_node_id (smp_processor_id ()); - this_leaf->nb =node_to_amd_nb (node); -if (this_leaf->nb && !this_leaf->nb->l3_cache.indices) - amd_calc_l3_indices(this_leaf->nb); + id4->nb =node_to_amd_nb (node); +if (id4->nb && !id4->nb->l3_cache.indices) + amd_calc_l3_indices(id4->nb); }#else #define amd_init_l3_cache (x, y)#endif /* CONFIG_AMD_NB && CONFIG_SYSFS */ static int -cpuid4_cache_lookup_regs (int index,struct _cpuid4_info_regs *this_leaf) +cpuid4_cache_lookup_regs (int index,struct _cpuid4_info_regs *id4) {union _cpuid4_leaf_eax eax;union _cpuid4_leaf_ebx ebx;
@@ -604,11 +604,11 @@ cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *this_leaf) &ebx.full, &ecx.full, &edx); else amd_cpuid4(index, &eax, &ebx, &ecx); -amd_init_l3_cache (this_leaf, index); +amd_init_l3_cache (id4, index); }else if (boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON ) {cpuid_count (0x8000001d , index, &eax.full, &ebx.full, &ecx.full, &edx); -amd_init_l3_cache (this_leaf, index); +amd_init_l3_cache (id4, index); }else {cpuid_count (4 , index, &eax.full, &ebx.full, &ecx.full, &edx); }
@@ -616,13 +616,14 @@ cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *this_leaf)if (eax.split.type ==CTYPE_NULL )return -EIO ;/* better error ? */ - this_leaf->eax = eax; - this_leaf->ebx = ebx; - this_leaf->ecx = ecx; - this_leaf->size = (ecx.split.number_of_sets +1 ) * - (ebx.split.coherency_line_size +1 ) * - (ebx.split.physical_line_partition +1 ) * - (ebx.split.ways_of_associativity +1 ); + id4->eax = eax; + id4->ebx = ebx; + id4->ecx = ecx; + id4->size = (ecx.split.number_of_sets +1 ) * + (ebx.split.coherency_line_size +1 ) * + (ebx.split.physical_line_partition +1 ) * + (ebx.split.ways_of_associativity +1 ); +return 0 ; }
@@ -753,29 +754,29 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)* parameters cpuid leaf to find the cache details */ for (i =0 ; i < ci->num_leaves; i++) { -struct _cpuid4_info_regs this_leaf = {}; +struct _cpuid4_info_regs id4 = {};int retval; - retval =cpuid4_cache_lookup_regs (i, &this_leaf); + retval =cpuid4_cache_lookup_regs (i, &id4);if (retval <0 )continue ; -switch (this_leaf.eax.split.level) { +switch (id4.eax.split.level) {case 1 : -if (this_leaf.eax.split.type ==CTYPE_DATA ) - new_l1d = this_leaf.size/1024 ; -else if (this_leaf.eax.split.type ==CTYPE_INST ) - new_l1i = this_leaf.size/1024 ; +if (id4.eax.split.type ==CTYPE_DATA ) + new_l1d = id4.size/1024 ; +else if (id4.eax.split.type ==CTYPE_INST ) + new_l1i = id4.size/1024 ;break ;case 2 : - new_l2 = this_leaf.size/1024 ; - num_threads_sharing =1 + this_leaf.eax.split.num_threads_sharing; + new_l2 = id4.size/1024 ; + num_threads_sharing =1 + id4.eax.split.num_threads_sharing; index_msb =get_count_order (num_threads_sharing); l2_id = c->topo.apicid & ~((1 << index_msb) -1 );break ;case 3 : - new_l3 = this_leaf.size/1024 ; - num_threads_sharing =1 + this_leaf.eax.split.num_threads_sharing; + new_l3 = id4.size/1024 ; + num_threads_sharing =1 + id4.eax.split.num_threads_sharing; index_msb =get_count_order (num_threads_sharing); l3_id = c->topo.apicid & ~((1 << index_msb) -1 );break ;
@@ -840,7 +841,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) }static int __cache_amd_cpumap_setup (unsigned int cpu,int index, -const struct _cpuid4_info_regs *base) +const struct _cpuid4_info_regs *id4) {struct cpu_cacheinfo *this_cpu_ci;struct cacheinfo *ci;
@@ -866,7 +867,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index, }else if (boot_cpu_has (X86_FEATURE_TOPOEXT )) {unsigned int apicid, nshared, first, last; - nshared = base->eax.split.num_threads_sharing +1 ; + nshared = id4->eax.split.num_threads_sharing +1 ; apicid =cpu_data (cpu).topo.apicid; first = apicid - (apicid % nshared); last = first + nshared -1 ;
@@ -897,7 +898,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index, }static void __cache_cpumap_setup (unsigned int cpu,int index, -const struct _cpuid4_info_regs *base) +const struct _cpuid4_info_regs *id4) {struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu);struct cacheinfo *ci, *sibling_ci;
@@ -906,12 +907,12 @@ static void __cache_cpumap_setup(unsigned int cpu, int index,struct cpuinfo_x86 *c = &cpu_data (cpu);if (x86_vendor_amd_or_hygon (c->x86_vendor)) { -if (__cache_amd_cpumap_setup (cpu, index, base)) +if (__cache_amd_cpumap_setup (cpu, index, id4))return ; } ci = this_cpu_ci->info_list + index; - num_threads_sharing =1 + base->eax.split.num_threads_sharing; + num_threads_sharing =1 + id4->eax.split.num_threads_sharing;cpumask_set_cpu (cpu, &ci->shared_cpu_map);if (num_threads_sharing ==1 )
@@ -932,18 +933,18 @@ static void __cache_cpumap_setup(unsigned int cpu, int index, }static void ci_info_init (struct cacheinfo *ci, -const struct _cpuid4_info_regs *base) +const struct _cpuid4_info_regs *id4) { - ci->id = base->id; + ci->id = id4->id; ci->attributes =CACHE_ID ; - ci->level = base->eax.split.level; - ci->type = cache_type_map[base->eax.split.type]; - ci->coherency_line_size = base->ebx.split.coherency_line_size +1 ; - ci->ways_of_associativity = base->ebx.split.ways_of_associativity +1 ; - ci->size = base->size; - ci->number_of_sets = base->ecx.split.number_of_sets +1 ; - ci->physical_line_partition = base->ebx.split.physical_line_partition +1 ; - ci->priv = base->nb; + ci->level = id4->eax.split.level; + ci->type = cache_type_map[id4->eax.split.type]; + ci->coherency_line_size = id4->ebx.split.coherency_line_size +1 ; + ci->ways_of_associativity = id4->ebx.split.ways_of_associativity +1 ; + ci->size = id4->size; + ci->number_of_sets = id4->ecx.split.number_of_sets +1 ; + ci->physical_line_partition = id4->ebx.split.physical_line_partition +1 ; + ci->priv = id4->nb; }int init_cache_level (unsigned int cpu)
@@ -962,15 +963,15 @@ int init_cache_level(unsigned int cpu)* ECX as cache index. Then right shift apicid by the number's order to get * cache id for this cache node. */ -static void get_cache_id (int cpu,struct _cpuid4_info_regs *id4_regs) +static void get_cache_id (int cpu,struct _cpuid4_info_regs *id4) {struct cpuinfo_x86 *c = &cpu_data (cpu);unsigned long num_threads_sharing;int index_msb; - num_threads_sharing =1 + id4_regs->eax.split.num_threads_sharing; + num_threads_sharing =1 + id4->eax.split.num_threads_sharing; index_msb =get_count_order (num_threads_sharing); - id4_regs->id = c->topo.apicid >> index_msb; + id4->id = c->topo.apicid >> index_msb; }int populate_cache_leaves (unsigned int cpu)
@@ -978,15 +979,15 @@ int populate_cache_leaves(unsigned int cpu)unsigned int idx, ret;struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu);struct cacheinfo *ci = this_cpu_ci->info_list; -struct _cpuid4_info_regs id4_regs = {}; +struct _cpuid4_info_regs id4 = {};for (idx =0 ; idx < this_cpu_ci->num_leaves; idx++) { - ret =cpuid4_cache_lookup_regs (idx, &id4_regs); + ret =cpuid4_cache_lookup_regs (idx, &id4);if (ret)return ret; -get_cache_id (cpu, &id4_regs); -ci_info_init (ci++, &id4_regs); -__cache_cpumap_setup (cpu, idx, &id4_regs); +get_cache_id (cpu, &id4); +ci_info_init (ci++, &id4); +__cache_cpumap_setup (cpu, idx, &id4); } this_cpu_ci->cpu_map_populated = true;
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 11/29 x86/cacheinfo: Consolidate AMD/Hygon leaf 0x8000001d calls
In reply to Ahmed S. DarwishWhile gathering CPU cache info, CPUID leaf 0x8000001d is invoked in two separate if blocks: one for Hygon CPUs and one for AMDs with topology extensions. After each invocation, amd_init_l3_cache() is called.
Merge the two if blocks into a single condition, thus removing the duplicated code. Future commits will expand these if blocks, so combining them now is both cleaner and more maintainable.
Note, while at it, remove a useless "better error?" comment that was within the same function since the 2005 commit e2cac78935ff ("[PATCH]x86_64: When running cpuid4 need to run on the correct CPU").
--- arch/x86/kernel/cpu/cacheinfo.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +9 -8
@@ -598,23 +598,24 @@ cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *id4)union _cpuid4_leaf_ecx ecx;unsigned edx; -if (boot_cpu_data.x86_vendor ==X86_VENDOR_AMD ) { -if (boot_cpu_has (X86_FEATURE_TOPOEXT )) +if (x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor)) { +if (boot_cpu_has (X86_FEATURE_TOPOEXT ) || + boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON ) { +/* AMD with TOPOEXT, or HYGON */ cpuid_count (0x8000001d , index, &eax.full, &ebx.full, &ecx.full, &edx); -else + }else { +/* Legacy AMD fallback */ amd_cpuid4 (index, &eax, &ebx, &ecx); -amd_init_l3_cache (id4, index); - }else if (boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON ) { -cpuid_count (0x8000001d , index, &eax.full, - &ebx.full, &ecx.full, &edx); + }amd_init_l3_cache (id4, index); }else { +/* Intel */ cpuid_count (4 , index, &eax.full, &ebx.full, &ecx.full, &edx); }if (eax.split.type ==CTYPE_NULL ) -return -EIO ;/* better error ? */ +return -EIO ; id4->eax = eax; id4->ebx = ebx;
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 12/29 x86/cacheinfo: Separate amd_northbridge from _cpuid4_info_regs
In reply to Ahmed S. DarwishThe _cpuid4_info_regs structure is meant to hold the CPUID leaf 0x4 output registers (EAX, EBX, and ECX), as well as derived information such as the cache node ID and size.
It also contains a reference to amd_northbridge, which is there only to be "parked" until ci_info_init() can store it in the priv pointer of the <linux/cacheinfo.h> API. That priv pointer is then used by AMD-specific L3 cache_disable_0/1 sysfs attributes.
Decouple amd_northbridge from _cpuid4_info_regs and pass it explicitly through the functions at x86/cacheinfo. Doing so clarifies when amd_northbridge is actually needed (AMD-only code) and when it is not (Intel-specific code). It also prepares for moving the AMD-specific L3 cache_disable_0/1 sysfs code into its own file in next commit.
--- arch/x86/kernel/cpu/cacheinfo.c | 45 +++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 16 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +29 -16
@@ -168,7 +168,6 @@ struct _cpuid4_info_regs {union _cpuid4_leaf_ecx ecx;unsigned int id;unsigned long size; -struct amd_northbridge *nb; };/* AMD doesn't have CPUID4. Emulate it here to report the same
@@ -573,25 +572,36 @@ cache_get_priv_group(struct cacheinfo *ci) return &cache_private_group; } -static void amd_init_l3_cache (struct _cpuid4_info_regs *id4,int index) +static struct amd_northbridge *amd_init_l3_cache (int index) { +struct amd_northbridge *nb;int node;/* only for L3, and not in virtualized environments */ if (index <3 ) -return ; +return NULL ; node =topology_amd_node_id (smp_processor_id ()); - id4->nb =node_to_amd_nb (node); -if (id4->nb && !id4->nb->l3_cache.indices) -amd_calc_l3_indices (id4->nb); + nb =node_to_amd_nb (node); +if (nb && !nb->l3_cache.indices) +amd_calc_l3_indices (nb); + +return nb; }#else -#define amd_init_l3_cache (x, y) +static struct amd_northbridge *amd_init_l3_cache (int index) +{ +return NULL ; +}#endif /* CONFIG_AMD_NB && CONFIG_SYSFS */ -static int -cpuid4_cache_lookup_regs (int index,struct _cpuid4_info_regs *id4) +/* +* Fill passed _cpuid4_info_regs structure. +* Intel-only code paths should pass NULL for the amd_northbridge +* return pointer. +*/ +static int cpuid4_cache_lookup_regs (int index,struct _cpuid4_info_regs *id4, +struct amd_northbridge **nb) {union _cpuid4_leaf_eax eax;union _cpuid4_leaf_ebx ebx;
@@ -608,7 +618,9 @@ cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *id4)/* Legacy AMD fallback */ amd_cpuid4 (index, &eax, &ebx, &ecx); } -amd_init_l3_cache (id4, index); + +if (nb) + *nb =amd_init_l3_cache (index); }else {/* Intel */ cpuid_count (4 , index, &eax.full, &ebx.full, &ecx.full, &edx);
@@ -758,7 +770,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)struct _cpuid4_info_regs id4 = {};int retval; - retval =cpuid4_cache_lookup_regs (i, &id4); + retval =cpuid4_cache_lookup_regs (i, &id4,NULL );if (retval <0 )continue ;
@@ -933,8 +945,8 @@ static void __cache_cpumap_setup(unsigned int cpu, int index, } } -static void ci_info_init (struct cacheinfo *ci, -const struct _cpuid4_info_regs *id4) +static void ci_info_init (struct cacheinfo *ci,const struct _cpuid4_info_regs *id4, +struct amd_northbridge *nb) { ci->id = id4->id; ci->attributes =CACHE_ID ;
@@ -945,7 +957,7 @@ static void ci_info_init(struct cacheinfo *ci, ci->size = id4->size; ci->number_of_sets = id4->ecx.split.number_of_sets +1 ; ci->physical_line_partition = id4->ebx.split.physical_line_partition +1 ; - ci->priv = id4->nb; + ci->priv = nb; }int init_cache_level (unsigned int cpu)
@@ -981,13 +993,14 @@ int populate_cache_leaves(unsigned int cpu)struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu);struct cacheinfo *ci = this_cpu_ci->info_list;struct _cpuid4_info_regs id4 = {}; +struct amd_northbridge *nb;for (idx =0 ; idx < this_cpu_ci->num_leaves; idx++) { - ret =cpuid4_cache_lookup_regs (idx, &id4); + ret =cpuid4_cache_lookup_regs (idx, &id4, &nb);if (ret)return ret;get_cache_id (cpu, &id4); -ci_info_init (ci++, &id4); +ci_info_init (ci++, &id4, nb);__cache_cpumap_setup (cpu, idx, &id4); } this_cpu_ci->cpu_map_populated = true;
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 13/29 x86/cacheinfo: Move AMD cache_disable_0/1 handling to separate file
In reply to Ahmed S. DarwishParent commit decoupled amd_northbridge out of _cpuid4_info_regs, where it was merely "parked" there until ci_info_init() can store it in the private pointer of the <linux/cacheinfo.h> API.
Given that decoupling, move the AMD-specific L3 cache_disable_0/1 sysfs code from the generic (and already extremely convoluted) x86/cacheinfo code into its own file.
Compile the file only if CONFIG_AMD_NB and CONFIG_SYSFS are both enabled, which mirrors the existing logic.
--- arch/x86/kernel/cpu/Makefile | 3 + arch/x86/kernel/cpu/amd_cache_disable.c | 301 ++++++++++++++++++++++++ arch/x86/kernel/cpu/cacheinfo.c | 298 ----------------------- arch/x86/kernel/cpu/cpu.h | 9 + 4 files changed, 313 insertions(+), 298 deletions(-) create mode 100644 arch/x86/kernel/cpu/amd_cache_disable.c
arch/x86/kernel/cpu/Makefile +3 -0
@@ -38,6 +38,9 @@ obj-y += intel.o tsx.o obj-$(CONFIG_PM) += intel_epb.o endif obj-$(CONFIG_CPU_SUP_AMD) += amd.o +ifeq ($(CONFIG_AMD_NB)$(CONFIG_SYSFS),yy) +obj-y += amd_cache_disable.o +endif obj-$(CONFIG_CPU_SUP_HYGON) += hygon.o obj-$(CONFIG_CPU_SUP_CYRIX_32) += cyrix.o obj-$(CONFIG_CPU_SUP_CENTAUR) += centaur.o
arch/x86/kernel/cpu/amd_cache_disable.c new file +301 -0
301 lines not on this page · raw message
arch/x86/kernel/cpu/cacheinfo.c +0 -298
313 lines not on this page · raw message
arch/x86/kernel/cpu/cpu.h +9 -0
@@ -75,6 +75,15 @@ extern void check_null_seg_clears_base(struct cpuinfo_x86 *c);void cacheinfo_amd_init_llc_id (struct cpuinfo_x86 *c,u16 die_id);void cacheinfo_hygon_init_llc_id (struct cpuinfo_x86 *c); +#if defined(CONFIG_AMD_NB ) && defined(CONFIG_SYSFS ) +struct amd_northbridge *amd_init_l3_cache (int index); +#else +static inline struct amd_northbridge *amd_init_l3_cache (int index) +{ +return NULL ; +} +#endif +unsigned int aperfmperf_get_khz (int cpu);void cpu_select_mitigations (void );
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 14/29 x86/cacheinfo: Use sysfs_emit() for sysfs attributes show()
In reply to Ahmed S. DarwishPer Documentation/filesystems/sysfs.rst, a sysfs attribute's show() method should only use sysfs_emit() or sysfs_emit_at() when returning values to user space.
Use sysfs_emit() for the AMD L3 cache sysfs attributes cache_disable_0, cache_disable_1, and subcaches.
--- arch/x86/kernel/cpu/amd_cache_disable.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/amd_cache_disable.c +3 -3
@@ -66,9 +66,9 @@ static ssize_t show_cache_disable(struct cacheinfo *ci, char *buf, unsigned int index =amd_get_l3_disable_slot (nb, slot);if (index >=0 ) -return sprintf (buf,"%d\n" , index); +return sysfs_emit (buf,"%d\n" , index); -return sprintf (buf,"FREE\n" ); +return sysfs_emit (buf,"FREE\n" ); }#define SHOW_CACHE_DISABLE (slot) \
@@ -189,7 +189,7 @@ static ssize_t subcaches_show(struct device *dev, struct device_attribute *attr,struct cacheinfo *ci =dev_get_drvdata (dev);int cpu =cpumask_first (&ci->shared_cpu_map); -return sprintf (buf,"%x\n" ,amd_get_subcaches (cpu)); +return sysfs_emit (buf,"%x\n" ,amd_get_subcaches (cpu)); } static ssize_t subcaches_store(struct device *dev,
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 15/29 x86/cacheinfo: Separate Intel and AMD leaf 0x4 code paths
In reply to Ahmed S. DarwishThe leaf 0x4 parsing code at cpuid4_cache_lookup_regs() is ugly and convoluted. It's tangled with multiple nested conditions to handle:
- AMD with TOPEXT, or Hygon CPUs via leaf 0x8000001d
- Legacy AMD fallback via leaf 0x4 emulation
- Intel CPUs via the actual CPUID leaf 0x4
AMD L3 northbridge initialization is also awkwardly placed alongside the CPUID calls of the first two scenarios.
Refactor all of that as follows:
- Update AMD's leaf 0x4 emulation comment to represent current state.
- Clearly label the AMD leaf 0x4 emulation function as a fallback.
- Split AMD/Hygon and Intel code paths into separate functions.
- Move AMD L3 northbridge initialization out of leaf 0x4 code, and into populate_cache_leaves() where it belongs. There, ci_info_init() can directly store the initialized object in the private pointer of the <linux/cacheinfo.h> API.
--- arch/x86/kernel/cpu/cacheinfo.c | 93 ++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 42 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +51 -42
@@ -167,12 +167,11 @@ struct _cpuid4_info_regs {unsigned long size; }; -/* AMD doesn't have CPUID4. Emulate it here to report the same -information to the user. This makes some assumptions about the machine: -L2 not shared, no SMT etc. that is currently true on AMD CPUs. +/* +* Fallback AMD CPUID(4) emulation +* AMD CPUs with TOPOEXT can just use CPUID(0x8000001d) +*/ -In theory the TLBs could be reported as fake type (they are in "dummy"). -Maybe later */ union l1_cache { struct { unsigned line_size:8;
@@ -228,9 +227,8 @@ static const enum cache_type cache_type_map[] = { [CTYPE_UNIFIED] = CACHE_TYPE_UNIFIED, }; -static void -amd_cpuid4 (int index,union _cpuid4_leaf_eax *eax, -union _cpuid4_leaf_ebx *ebx,union _cpuid4_leaf_ecx *ecx) +static void legacy_amd_cpuid4 (int index,union _cpuid4_leaf_eax *eax, +union _cpuid4_leaf_ebx *ebx,union _cpuid4_leaf_ecx *ecx) {unsigned int dummy, line_size, lines_per_tag, assoc, size_in_kb;union l1_cache l1i, l1d;
@@ -297,37 +295,9 @@ amd_cpuid4(int index, union _cpuid4_leaf_eax *eax, (ebx->split.ways_of_associativity +1 ) -1 ; } -/* -* Fill passed _cpuid4_info_regs structure. -* Intel-only code paths should pass NULL for the amd_northbridge -* return pointer. -*/ -static int cpuid4_cache_lookup_regs (int index,struct _cpuid4_info_regs *id4, -struct amd_northbridge **nb) +static int cpuid4_info_fill_done (struct _cpuid4_info_regs *id4,union _cpuid4_leaf_eax eax, +union _cpuid4_leaf_ebx ebx,union _cpuid4_leaf_ecx ecx) { -union _cpuid4_leaf_eax eax; -union _cpuid4_leaf_ebx ebx; -union _cpuid4_leaf_ecx ecx; -unsigned edx; - -if (x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor)) { -if (boot_cpu_has (X86_FEATURE_TOPOEXT ) || - boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON ) { -/* AMD with TOPOEXT, or HYGON */ -cpuid_count (0x8000001d , index, &eax.full, - &ebx.full, &ecx.full, &edx); - }else { -/* Legacy AMD fallback */ -amd_cpuid4 (index, &eax, &ebx, &ecx); - } - -if (nb) - *nb =amd_init_l3_cache (index); - }else { -/* Intel */ -cpuid_count (4 , index, &eax.full, &ebx.full, &ecx.full, &edx); - } -if (eax.split.type ==CTYPE_NULL )return -EIO ;
@@ -342,6 +312,40 @@ static int cpuid4_cache_lookup_regs(int index, struct _cpuid4_info_regs *id4,return 0 ; } +static int amd_fill_cpuid4_info (int index,struct _cpuid4_info_regs *id4) +{ +union _cpuid4_leaf_eax eax; +union _cpuid4_leaf_ebx ebx; +union _cpuid4_leaf_ecx ecx; +unsigned int ignored; + +if (boot_cpu_has (X86_FEATURE_TOPOEXT ) || boot_cpu_data.x86_vendor ==X86_VENDOR_HYGON ) +cpuid_count (0x8000001d , index, &eax.full, &ebx.full, &ecx.full, &ignored); +else +legacy_amd_cpuid4 (index, &eax, &ebx, &ecx); + +return cpuid4_info_fill_done (id4, eax, ebx, ecx); +} + +static int intel_fill_cpuid4_info (int index,struct _cpuid4_info_regs *id4) +{ +union _cpuid4_leaf_eax eax; +union _cpuid4_leaf_ebx ebx; +union _cpuid4_leaf_ecx ecx; +unsigned int ignored; + +cpuid_count (4 , index, &eax.full, &ebx.full, &ecx.full, &ignored); + +return cpuid4_info_fill_done (id4, eax, ebx, ecx); +} + +static int fill_cpuid4_info (int index,struct _cpuid4_info_regs *id4) +{ +return x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor) ? +amd_fill_cpuid4_info (index, id4) : +intel_fill_cpuid4_info (index, id4); +} +static int find_num_cache_leaves (struct cpuinfo_x86 *c) {unsigned int eax, ebx, ecx, edx, op;
@@ -472,7 +476,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)struct _cpuid4_info_regs id4 = {};int retval; - retval =cpuid4_cache_lookup_regs (i, &id4,NULL ); + retval =intel_fill_cpuid4_info (i, &id4);if (retval <0 )continue ;
@@ -691,17 +695,22 @@ static void get_cache_id(int cpu, struct _cpuid4_info_regs *id4)int populate_cache_leaves (unsigned int cpu) { -unsigned int idx, ret;struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu);struct cacheinfo *ci = this_cpu_ci->info_list;struct _cpuid4_info_regs id4 = {}; -struct amd_northbridge *nb; +struct amd_northbridge *nb =NULL ; +int idx, ret;for (idx =0 ; idx < this_cpu_ci->num_leaves; idx++) { - ret =cpuid4_cache_lookup_regs (idx, &id4, &nb); + ret =fill_cpuid4_info (idx, &id4);if (ret)return ret; +get_cache_id (cpu, &id4); + +if (x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor)) + nb =amd_init_l3_cache (idx); +ci_info_init (ci++, &id4, nb);__cache_cpumap_setup (cpu, idx, &id4); }
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 16/29 x86/cacheinfo: Rename _cpuid4_info_regs to _cpuid4_info
In reply to Ahmed S. DarwishParent commits decoupled amd_northbridge from _cpuid4_info_regs, moved AMD L3 northbridge cache_disable_0/1 sysfs code to its own file, and splitted AMD vs. Intel leaf 0x4 handling into:
amd_fill_cpuid4_info()
intel_fill_cpuid4_info()
fill_cpuid4_info()After doing all that, the "_cpuid4_info_regs" name becomes a mouthful. It is also not totally accurate, as the structure holds cpuid4 derived information like cache node ID and size -- not just regs.
Rename struct _cpuid4_info_regs to _cpuid4_info. That new name also better matches the AMD/Intel leaf 0x4 functions mentioned above.
--- arch/x86/kernel/cpu/cacheinfo.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +11 -11
@@ -159,7 +159,7 @@ union _cpuid4_leaf_ecx {u32 full; }; -struct _cpuid4_info_regs { +struct _cpuid4_info {union _cpuid4_leaf_eax eax;union _cpuid4_leaf_ebx ebx;union _cpuid4_leaf_ecx ecx;
@@ -295,7 +295,7 @@ static void legacy_amd_cpuid4(int index, union _cpuid4_leaf_eax *eax, (ebx->split.ways_of_associativity +1 ) -1 ; } -static int cpuid4_info_fill_done (struct _cpuid4_info_regs *id4,union _cpuid4_leaf_eax eax, +static int cpuid4_info_fill_done (struct _cpuid4_info *id4,union _cpuid4_leaf_eax eax,union _cpuid4_leaf_ebx ebx,union _cpuid4_leaf_ecx ecx) {if (eax.split.type ==CTYPE_NULL )
@@ -312,7 +312,7 @@ static int cpuid4_info_fill_done(struct _cpuid4_info_regs *id4, union _cpuid4_lereturn 0 ; } -static int amd_fill_cpuid4_info (int index,struct _cpuid4_info_regs *id4) +static int amd_fill_cpuid4_info (int index,struct _cpuid4_info *id4) {union _cpuid4_leaf_eax eax;union _cpuid4_leaf_ebx ebx;
@@ -327,7 +327,7 @@ static int amd_fill_cpuid4_info(int index, struct _cpuid4_info_regs *id4)return cpuid4_info_fill_done (id4, eax, ebx, ecx); } -static int intel_fill_cpuid4_info (int index,struct _cpuid4_info_regs *id4) +static int intel_fill_cpuid4_info (int index,struct _cpuid4_info *id4) {union _cpuid4_leaf_eax eax;union _cpuid4_leaf_ebx ebx;
@@ -339,7 +339,7 @@ static int intel_fill_cpuid4_info(int index, struct _cpuid4_info_regs *id4)return cpuid4_info_fill_done (id4, eax, ebx, ecx); } -static int fill_cpuid4_info (int index,struct _cpuid4_info_regs *id4) +static int fill_cpuid4_info (int index,struct _cpuid4_info *id4) {return x86_vendor_amd_or_hygon (boot_cpu_data.x86_vendor) ? amd_fill_cpuid4_info(index, id4) :
@@ -473,7 +473,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)* parameters cpuid leaf to find the cache details */ for (i =0 ; i < ci->num_leaves; i++) { -struct _cpuid4_info_regs id4 = {}; +struct _cpuid4_info id4 = {};int retval; retval =intel_fill_cpuid4_info (i, &id4);
@@ -560,7 +560,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c) }static int __cache_amd_cpumap_setup (unsigned int cpu,int index, -const struct _cpuid4_info_regs *id4) +const struct _cpuid4_info *id4) {struct cpu_cacheinfo *this_cpu_ci;struct cacheinfo *ci;
@@ -617,7 +617,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index, }static void __cache_cpumap_setup (unsigned int cpu,int index, -const struct _cpuid4_info_regs *id4) +const struct _cpuid4_info *id4) {struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu);struct cacheinfo *ci, *sibling_ci;
@@ -651,7 +651,7 @@ static void __cache_cpumap_setup(unsigned int cpu, int index, } } -static void ci_info_init (struct cacheinfo *ci,const struct _cpuid4_info_regs *id4, +static void ci_info_init (struct cacheinfo *ci,const struct _cpuid4_info *id4,struct amd_northbridge *nb) { ci->id = id4->id;
@@ -682,7 +682,7 @@ int init_cache_level(unsigned int cpu)* ECX as cache index. Then right shift apicid by the number's order to get * cache id for this cache node. */ -static void get_cache_id (int cpu,struct _cpuid4_info_regs *id4) +static void get_cache_id (int cpu,struct _cpuid4_info *id4) {struct cpuinfo_x86 *c = &cpu_data (cpu);unsigned long num_threads_sharing;
@@ -697,8 +697,8 @@ int populate_cache_leaves(unsigned int cpu) {struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu);struct cacheinfo *ci = this_cpu_ci->info_list; -struct _cpuid4_info_regs id4 = {};struct amd_northbridge *nb =NULL ; +struct _cpuid4_info id4 = {};int idx, ret;for (idx =0 ; idx < this_cpu_ci->num_leaves; idx++) {
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 17/29 x86/cacheinfo: Clarify type markers for leaf 0x2 cache descriptors
In reply to Ahmed S. DarwishCPUID leaf 0x2 output is a stream of one-byte descriptors, each implying certain details about the CPU's cache and TLB entries.
Two separate tables exist for interpreting these descriptors: one for TLBs at intel.c and one for caches at cacheinfo.c. These mapping tables will be merged in further commits, among other improvements to their model.
In preparation for this, use more descriptive type names for the leaf 0x2 descriptors associated with cpu caches. Namely:
LVL_1_INST => CACHE_L1_INST LVL_1_DATA => CACHE_L1_DATA LVL_2 => CACHE_L2 LVL_3 => CACHE_L3
After the TLB and cache descriptors mapping tables are merged, this will make it clear that such descriptors correspond to cpu caches.
--- arch/x86/kernel/cpu/cacheinfo.c | 152 ++++++++++++++++---------------- 1 file changed, 76 insertions(+), 76 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +76 -76
@@ -23,10 +23,10 @@#include "cpu.h" -#define LVL_1_INST 1 -#define LVL_1_DATA 2 -#define LVL_2 3 -#define LVL_3 4 +#define CACHE_L1_INST 1 +#define CACHE_L1_DATA 2 +#define CACHE_L2 3 +#define CACHE_L3 4/* Shared last level cache maps */ DEFINE_PER_CPU_READ_MOSTLY (cpumask_var_t, cpu_llc_shared_map);
@@ -52,74 +52,74 @@ struct _cache_table {static const struct _cache_table cache_table[] = { - {0x06 ,LVL_1_INST ,8 },/* 4-way set assoc, 32 byte line size */ - {0x08 ,LVL_1_INST ,16 },/* 4-way set assoc, 32 byte line size */ - {0x09 ,LVL_1_INST ,32 },/* 4-way set assoc, 64 byte line size */ - {0x0a ,LVL_1_DATA ,8 },/* 2 way set assoc, 32 byte line size */ - {0x0c ,LVL_1_DATA ,16 },/* 4-way set assoc, 32 byte line size */ - {0x0d ,LVL_1_DATA ,16 },/* 4-way set assoc, 64 byte line size */ - {0x0e ,LVL_1_DATA ,24 },/* 6-way set assoc, 64 byte line size */ - {0x21 ,LVL_2 ,256 },/* 8-way set assoc, 64 byte line size */ - {0x22 ,LVL_3 ,512 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x23 ,LVL_3 ,MB (1 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x25 ,LVL_3 ,MB (2 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x29 ,LVL_3 ,MB (4 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x2c ,LVL_1_DATA ,32 },/* 8-way set assoc, 64 byte line size */ - {0x30 ,LVL_1_INST ,32 },/* 8-way set assoc, 64 byte line size */ - {0x39 ,LVL_2 ,128 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x3a ,LVL_2 ,192 },/* 6-way set assoc, sectored cache, 64 byte line size */ - {0x3b ,LVL_2 ,128 },/* 2-way set assoc, sectored cache, 64 byte line size */ - {0x3c ,LVL_2 ,256 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x3d ,LVL_2 ,384 },/* 6-way set assoc, sectored cache, 64 byte line size */ - {0x3e ,LVL_2 ,512 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x3f ,LVL_2 ,256 },/* 2-way set assoc, 64 byte line size */ - {0x41 ,LVL_2 ,128 },/* 4-way set assoc, 32 byte line size */ - {0x42 ,LVL_2 ,256 },/* 4-way set assoc, 32 byte line size */ - {0x43 ,LVL_2 ,512 },/* 4-way set assoc, 32 byte line size */ - {0x44 ,LVL_2 ,MB (1 ) },/* 4-way set assoc, 32 byte line size */ - {0x45 ,LVL_2 ,MB (2 ) },/* 4-way set assoc, 32 byte line size */ - {0x46 ,LVL_3 ,MB (4 ) },/* 4-way set assoc, 64 byte line size */ - {0x47 ,LVL_3 ,MB (8 ) },/* 8-way set assoc, 64 byte line size */ - {0x48 ,LVL_2 ,MB (3 ) },/* 12-way set assoc, 64 byte line size */ - {0x49 ,LVL_3 ,MB (4 ) },/* 16-way set assoc, 64 byte line size */ - {0x4a ,LVL_3 ,MB (6 ) },/* 12-way set assoc, 64 byte line size */ - {0x4b ,LVL_3 ,MB (8 ) },/* 16-way set assoc, 64 byte line size */ - {0x4c ,LVL_3 ,MB (12 ) },/* 12-way set assoc, 64 byte line size */ - {0x4d ,LVL_3 ,MB (16 ) },/* 16-way set assoc, 64 byte line size */ - {0x4e ,LVL_2 ,MB (6 ) },/* 24-way set assoc, 64 byte line size */ - {0x60 ,LVL_1_DATA ,16 },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x66 ,LVL_1_DATA ,8 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x67 ,LVL_1_DATA ,16 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x68 ,LVL_1_DATA ,32 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x78 ,LVL_2 ,MB (1 ) },/* 4-way set assoc, 64 byte line size */ - {0x79 ,LVL_2 ,128 },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x7a ,LVL_2 ,256 },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x7b ,LVL_2 ,512 },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x7c ,LVL_2 ,MB (1 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x7d ,LVL_2 ,MB (2 ) },/* 8-way set assoc, 64 byte line size */ - {0x7f ,LVL_2 ,512 },/* 2-way set assoc, 64 byte line size */ - {0x80 ,LVL_2 ,512 },/* 8-way set assoc, 64 byte line size */ - {0x82 ,LVL_2 ,256 },/* 8-way set assoc, 32 byte line size */ - {0x83 ,LVL_2 ,512 },/* 8-way set assoc, 32 byte line size */ - {0x84 ,LVL_2 ,MB (1 ) },/* 8-way set assoc, 32 byte line size */ - {0x85 ,LVL_2 ,MB (2 ) },/* 8-way set assoc, 32 byte line size */ - {0x86 ,LVL_2 ,512 },/* 4-way set assoc, 64 byte line size */ - {0x87 ,LVL_2 ,MB (1 ) },/* 8-way set assoc, 64 byte line size */ - {0xd0 ,LVL_3 ,512 },/* 4-way set assoc, 64 byte line size */ - {0xd1 ,LVL_3 ,MB (1 ) },/* 4-way set assoc, 64 byte line size */ - {0xd2 ,LVL_3 ,MB (2 ) },/* 4-way set assoc, 64 byte line size */ - {0xd6 ,LVL_3 ,MB (1 ) },/* 8-way set assoc, 64 byte line size */ - {0xd7 ,LVL_3 ,MB (2 ) },/* 8-way set assoc, 64 byte line size */ - {0xd8 ,LVL_3 ,MB (4 ) },/* 12-way set assoc, 64 byte line size */ - {0xdc ,LVL_3 ,MB (2 ) },/* 12-way set assoc, 64 byte line size */ - {0xdd ,LVL_3 ,MB (4 ) },/* 12-way set assoc, 64 byte line size */ - {0xde ,LVL_3 ,MB (8 ) },/* 12-way set assoc, 64 byte line size */ - {0xe2 ,LVL_3 ,MB (2 ) },/* 16-way set assoc, 64 byte line size */ - {0xe3 ,LVL_3 ,MB (4 ) },/* 16-way set assoc, 64 byte line size */ - {0xe4 ,LVL_3 ,MB (8 ) },/* 16-way set assoc, 64 byte line size */ - {0xea ,LVL_3 ,MB (12 ) },/* 24-way set assoc, 64 byte line size */ - {0xeb ,LVL_3 ,MB (18 ) },/* 24-way set assoc, 64 byte line size */ - {0xec ,LVL_3 ,MB (24 ) },/* 24-way set assoc, 64 byte line size */ + {0x06 ,CACHE_L1_INST ,8 },/* 4-way set assoc, 32 byte line size */ + {0x08 ,CACHE_L1_INST ,16 },/* 4-way set assoc, 32 byte line size */ + {0x09 ,CACHE_L1_INST ,32 },/* 4-way set assoc, 64 byte line size */ + {0x0a ,CACHE_L1_DATA ,8 },/* 2 way set assoc, 32 byte line size */ + {0x0c ,CACHE_L1_DATA ,16 },/* 4-way set assoc, 32 byte line size */ + {0x0d ,CACHE_L1_DATA ,16 },/* 4-way set assoc, 64 byte line size */ + {0x0e ,CACHE_L1_DATA ,24 },/* 6-way set assoc, 64 byte line size */ + {0x21 ,CACHE_L2 ,256 },/* 8-way set assoc, 64 byte line size */ + {0x22 ,CACHE_L3 ,512 },/* 4-way set assoc, sectored cache, 64 byte line size */ + {0x23 ,CACHE_L3 ,MB (1 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ + {0x25 ,CACHE_L3 ,MB (2 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ + {0x29 ,CACHE_L3 ,MB (4 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ + {0x2c ,CACHE_L1_DATA ,32 },/* 8-way set assoc, 64 byte line size */ + {0x30 ,CACHE_L1_INST ,32 },/* 8-way set assoc, 64 byte line size */ + {0x39 ,CACHE_L2 ,128 },/* 4-way set assoc, sectored cache, 64 byte line size */ + {0x3a ,CACHE_L2 ,192 },/* 6-way set assoc, sectored cache, 64 byte line size */ + {0x3b ,CACHE_L2 ,128 },/* 2-way set assoc, sectored cache, 64 byte line size */ + {0x3c ,CACHE_L2 ,256 },/* 4-way set assoc, sectored cache, 64 byte line size */ + {0x3d ,CACHE_L2 ,384 },/* 6-way set assoc, sectored cache, 64 byte line size */ + {0x3e ,CACHE_L2 ,512 },/* 4-way set assoc, sectored cache, 64 byte line size */ + {0x3f ,CACHE_L2 ,256 },/* 2-way set assoc, 64 byte line size */ + {0x41 ,CACHE_L2 ,128 },/* 4-way set assoc, 32 byte line size */ + {0x42 ,CACHE_L2 ,256 },/* 4-way set assoc, 32 byte line size */ + {0x43 ,CACHE_L2 ,512 },/* 4-way set assoc, 32 byte line size */ + {0x44 ,CACHE_L2 ,MB (1 ) },/* 4-way set assoc, 32 byte line size */ + {0x45 ,CACHE_L2 ,MB (2 ) },/* 4-way set assoc, 32 byte line size */ + {0x46 ,CACHE_L3 ,MB (4 ) },/* 4-way set assoc, 64 byte line size */ + {0x47 ,CACHE_L3 ,MB (8 ) },/* 8-way set assoc, 64 byte line size */ + {0x48 ,CACHE_L2 ,MB (3 ) },/* 12-way set assoc, 64 byte line size */ + {0x49 ,CACHE_L3 ,MB (4 ) },/* 16-way set assoc, 64 byte line size */ + {0x4a ,CACHE_L3 ,MB (6 ) },/* 12-way set assoc, 64 byte line size */ + {0x4b ,CACHE_L3 ,MB (8 ) },/* 16-way set assoc, 64 byte line size */ + {0x4c ,CACHE_L3 ,MB (12 ) },/* 12-way set assoc, 64 byte line size */ + {0x4d ,CACHE_L3 ,MB (16 ) },/* 16-way set assoc, 64 byte line size */ + {0x4e ,CACHE_L2 ,MB (6 ) },/* 24-way set assoc, 64 byte line size */ + {0x60 ,CACHE_L1_DATA ,16 },/* 8-way set assoc, sectored cache, 64 byte line size */ + {0x66 ,CACHE_L1_DATA ,8 },/* 4-way set assoc, sectored cache, 64 byte line size */ + {0x67 ,CACHE_L1_DATA ,16 },/* 4-way set assoc, sectored cache, 64 byte line size */ + {0x68 ,CACHE_L1_DATA ,32 },/* 4-way set assoc, sectored cache, 64 byte line size */ + {0x78 ,CACHE_L2 ,MB (1 ) },/* 4-way set assoc, 64 byte line size */ + {0x79 ,CACHE_L2 ,128 },/* 8-way set assoc, sectored cache, 64 byte line size */ + {0x7a ,CACHE_L2 ,256 },/* 8-way set assoc, sectored cache, 64 byte line size */ + {0x7b ,CACHE_L2 ,512 },/* 8-way set assoc, sectored cache, 64 byte line size */ + {0x7c ,CACHE_L2 ,MB (1 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ + {0x7d ,CACHE_L2 ,MB (2 ) },/* 8-way set assoc, 64 byte line size */ + {0x7f ,CACHE_L2 ,512 },/* 2-way set assoc, 64 byte line size */ + {0x80 ,CACHE_L2 ,512 },/* 8-way set assoc, 64 byte line size */ + {0x82 ,CACHE_L2 ,256 },/* 8-way set assoc, 32 byte line size */ + {0x83 ,CACHE_L2 ,512 },/* 8-way set assoc, 32 byte line size */ + {0x84 ,CACHE_L2 ,MB (1 ) },/* 8-way set assoc, 32 byte line size */ + {0x85 ,CACHE_L2 ,MB (2 ) },/* 8-way set assoc, 32 byte line size */ + {0x86 ,CACHE_L2 ,512 },/* 4-way set assoc, 64 byte line size */ + {0x87 ,CACHE_L2 ,MB (1 ) },/* 8-way set assoc, 64 byte line size */ + {0xd0 ,CACHE_L3 ,512 },/* 4-way set assoc, 64 byte line size */ + {0xd1 ,CACHE_L3 ,MB (1 ) },/* 4-way set assoc, 64 byte line size */ + {0xd2 ,CACHE_L3 ,MB (2 ) },/* 4-way set assoc, 64 byte line size */ + {0xd6 ,CACHE_L3 ,MB (1 ) },/* 8-way set assoc, 64 byte line size */ + {0xd7 ,CACHE_L3 ,MB (2 ) },/* 8-way set assoc, 64 byte line size */ + {0xd8 ,CACHE_L3 ,MB (4 ) },/* 12-way set assoc, 64 byte line size */ + {0xdc ,CACHE_L3 ,MB (2 ) },/* 12-way set assoc, 64 byte line size */ + {0xdd ,CACHE_L3 ,MB (4 ) },/* 12-way set assoc, 64 byte line size */ + {0xde ,CACHE_L3 ,MB (8 ) },/* 12-way set assoc, 64 byte line size */ + {0xe2 ,CACHE_L3 ,MB (2 ) },/* 16-way set assoc, 64 byte line size */ + {0xe3 ,CACHE_L3 ,MB (4 ) },/* 16-way set assoc, 64 byte line size */ + {0xe4 ,CACHE_L3 ,MB (8 ) },/* 16-way set assoc, 64 byte line size */ + {0xea ,CACHE_L3 ,MB (12 ) },/* 24-way set assoc, 64 byte line size */ + {0xeb ,CACHE_L3 ,MB (18 ) },/* 24-way set assoc, 64 byte line size */ + {0xec ,CACHE_L3 ,MB (24 ) },/* 24-way set assoc, 64 byte line size */ };
@@ -518,10 +518,10 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)continue ;switch (entry->cache_type) { -case LVL_1_INST : l1i += entry->size;break ; -case LVL_1_DATA : l1d += entry->size;break ; -case LVL_2 : l2 += entry->size;break ; -case LVL_3 : l3 += entry->size;break ; +case CACHE_L1_INST : l1i += entry->size;break ; +case CACHE_L1_DATA : l1d += entry->size;break ; +case CACHE_L2 : l2 += entry->size;break ; +case CACHE_L3 : l3 += entry->size;break ; } } }
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 18/29 x86/cacheinfo: Use enums for cache descriptor types
In reply to Ahmed S. DarwishThe leaf 0x2 one-byte cache descriptor types:
CACHE_L1_INST
CACHE_L1_DATA
CACHE_L2
CACHE_L3
are just discriminators to be used within the cache_table[] mapping. Their specific values are irrelevant.
Use enums for such types.
Make the enum packed and static assert that its values remain within a single byte so that the cache_table[] array size do not go out of hand.
--- arch/x86/include/asm/cpuid/types.h | 13 +++++++++++++ arch/x86/kernel/cpu/cacheinfo.c | 9 ++------- 2 files changed, 15 insertions(+), 7 deletions(-)
arch/x86/include/asm/cpuid/types.h +13 -0
@@ -2,6 +2,7 @@#ifndef _ASM_X86_CPUID_TYPES_H#define _ASM_X86_CPUID_TYPES_H +#include <linux/build_bug.h> #include <linux/types.h> /*
@@ -42,4 +43,16 @@ union leaf_0x2_regs {u8 desc[16 ]; }; +/* +* Leaf 0x2 1-byte descriptors' cache types +* To be used for their mappings at cache_table[] +*/ +enum _cache_table_type { +CACHE_L1_INST , +CACHE_L1_DATA , +CACHE_L2 , +CACHE_L3 +} __packed; +static_assert (sizeof (enum _cache_table_type ) ==1 ); +#endif /* _ASM_X86_CPUID_TYPES_H */
arch/x86/kernel/cpu/cacheinfo.c +2 -7
@@ -23,11 +23,6 @@#include "cpu.h" -#define CACHE_L1_INST 1 -#define CACHE_L1_DATA 2 -#define CACHE_L2 3 -#define CACHE_L3 4 -/* Shared last level cache maps */ DEFINE_PER_CPU_READ_MOSTLY (cpumask_var_t, cpu_llc_shared_map);
@@ -41,7 +36,7 @@ unsigned int memory_caching_control __ro_after_init;struct _cache_table {unsigned char descriptor; -char cache_type; +enum _cache_table_type type;short size; };
@@ -517,7 +512,7 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)if (!entry)continue ; -switch (entry->cache_type) { +switch (entry->type) {case CACHE_L1_INST : l1i += entry->size;break ;case CACHE_L1_DATA : l1d += entry->size;break ;case CACHE_L2 : l2 += entry->size;break ;
Signature · 1 line
2.48.1
kernel test robot
build botRe: [PATCH v2 18/29] x86/cacheinfo: Use enums for cache descriptor types
kernel test robot noticed the following build warnings:
Hi Ahmed,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 6d536cad0d55e71442b6d65500f74eb85544269e]
url: https://github.com/intel-lab-lkp/linux/commits/Ahmed-S-Darwish/x86-treewide-Introduce-x86_vendor_amd_or_hygon/20250318-011153 base: 6d536cad0d55e71442b6d65500f74eb85544269e patch link: https://lore.kernel.org/r/20250317164745.4754-19-darwi%40linutronix.de patch subject: [PATCH v2 18/29] x86/cacheinfo: Use enums for cache descriptor types config: i386-randconfig-061-20250319 (https://download.01.org/0day-ci/archive/20250319/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250319/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
drivers/cpuidle/poll_state.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...):
>> arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" -- drivers/cpuidle/cpuidle-haltpoll.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): >> arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" -- drivers/cpuidle/sysfs.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): >> arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" -- drivers/cpuidle/governor.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): >> arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" -- drivers/cpuidle/driver.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): >> arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" -- drivers/cpuidle/cpuidle.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): >> arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" -- drivers/cpuidle/governors/menu.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): >> arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" -- drivers/cpuidle/governors/haltpoll.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): >> arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1"
vim +56 arch/x86/include/asm/cpuid/types.h
45
46 /*
47 * Leaf 0x2 1-byte descriptors' cache types
48 * To be used for their mappings at cache_table[]
49 */
50 enum _cache_table_type {
51 CACHE_L1_INST,
52 CACHE_L1_DATA,
53 CACHE_L2,
54 CACHE_L3
55 } __packed;
> 56 static_assert(sizeof(enum _cache_table_type) == 1);
57 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Ahmed S. Darwish
reviewerRe: [PATCH v2 18/29] x86/cacheinfo: Use enums for cache descriptor types
Hi,
Quoted 4 lines · kernel test robot
arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse:
static assertion failed: "sizeof(enum _cache_table_type)== 1"
I've checked the error report and reproduced it on my machine:
https://download.01.org/0day-ci/archive/20250319/[email protected]/reproduce
After cloning sparse git repo and playing around with its validation test suite, I've realized that it does not understand __attribiute__((packed)) on enums.
Namely, applying below diff on top of sparse's git repo:
| diff --git a/validation/enum-min-size.c b/validation/enum-min-size.c
| index e8bd9fb1..e691b332 100644
| --- a/validation/enum-min-size.c
| +++ b/validation/enum-min-size.c
| @@ -1,5 +1,9 @@
| enum i { I = 1 };
| _Static_assert(sizeof(enum i) == sizeof(int), "int");
| +
| +enum k { X = 0} __attribute__((packed));
| +_Static_assert(sizeof(enum k) == sizeof(char), "char");
| +
| enum u { U = 1U };
| _Static_assert(sizeof(enum u) == sizeof(int), "uint");Then running the modified test:
sparse/$ cd validation sparse/validation$ ./test-suite enum-min-size.c
leads to the same error:
TEST enum-min-size (enum-min-size.c)
+enum-min-size.c:5:31: error: static assertion failed: "char"
error: FAIL: test 'enum-min-size.c' failed
After checking other kernel code which have __packed on enums, I found this at <linux/rw_hint.h>, which is included by core SCSI code:
/* Sparse ignores __packed annotations on enums, hence the #ifndef below. */
#ifndef __CHECKER__
static_assert(sizeof(enum rw_hint) == 1);
#endifSo I'll add a similar CPP guard (in v4, since I've already posted v3.)
Thanks!
--
Ahmed S. Darwish
Linutronix GmbH
Ahmed S. Darwish
reviewerPATCH v2 19/29 x86/cpu: Use enums for TLB descriptor types
In reply to Ahmed S. DarwishThe leaf 0x2 one-byte TLB descriptor types:
TLB_INST_4K TLB_INST_4M TLB_INST_2M_4M ...
are just discriminators to be used within the intel_tlb_table[] mapping. Their specific values are irrelevant.
Use enums for such types.
Make the enum packed and static assert that its values remain within a single byte so that the intel_tlb_table[] size do not go out of hand.
--- arch/x86/include/asm/cpuid/types.h | 29 +++++++++++++++++++++++++++++ arch/x86/kernel/cpu/intel.c | 28 +++------------------------- 2 files changed, 32 insertions(+), 25 deletions(-)
arch/x86/include/asm/cpuid/types.h +29 -0
@@ -55,4 +55,33 @@ enum _cache_table_type { } __packed;static_assert (sizeof (enum _cache_table_type ) ==1 ); +/* +* Leaf 0x2 1-byte descriptors' TLB types +* To be used for their mappings at intel_tlb_table[] +* +* Start at 1 since type 0 is reserved for HW byte descriptors which are +* not recognized by the kernel; i.e., those without an explicit mapping. +*/ +enum _tlb_table_type { +TLB_INST_4K =1 , +TLB_INST_4M , +TLB_INST_2M_4M , +TLB_INST_ALL , + +TLB_DATA_4K , +TLB_DATA_4M , +TLB_DATA_2M_4M , +TLB_DATA_4K_4M , +TLB_DATA_1G , +TLB_DATA_1G_2M_4M , + +TLB_DATA0_4K , +TLB_DATA0_4M , +TLB_DATA0_2M_4M , + +STLB_4K , +STLB_4K_2M , +} __packed; +static_assert (sizeof (enum _tlb_table_type ) ==1 ); +#endif /* _ASM_X86_CPUID_TYPES_H */
arch/x86/kernel/cpu/intel.c +3 -25
@@ -627,28 +627,6 @@ static unsigned int intel_size_cache(struct cpuinfo_x86 *c, unsigned int size) }#endif -#define TLB_INST_4K 0x01 -#define TLB_INST_4M 0x02 -#define TLB_INST_2M_4M 0x03 - -#define TLB_INST_ALL 0x05 -#define TLB_INST_1G 0x06 - -#define TLB_DATA_4K 0x11 -#define TLB_DATA_4M 0x12 -#define TLB_DATA_2M_4M 0x13 -#define TLB_DATA_4K_4M 0x14 - -#define TLB_DATA_1G 0x16 -#define TLB_DATA_1G_2M_4M 0x17 - -#define TLB_DATA0_4K 0x21 -#define TLB_DATA0_4M 0x22 -#define TLB_DATA0_2M_4M 0x23 - -#define STLB_4K 0x41 -#define STLB_4K_2M 0x42 -/* * All of leaf 0x2's one-byte TLB descriptors implies the same number of * entries for their respective TLB types. The 0x63 descriptor is an
@@ -661,7 +639,7 @@ static unsigned int intel_size_cache(struct cpuinfo_x86 *c, unsigned int size)struct _tlb_table {unsigned char descriptor; -char tlb_type; +enum _tlb_table_type type;unsigned int entries; };
@@ -719,11 +697,11 @@ static void intel_tlb_lookup(const unsigned char desc) intel_tlb_table[k].descriptor != 0; k++) ; -if (intel_tlb_table[k].tlb_type ==0 ) +if (intel_tlb_table[k].type ==0 )return ; entries = intel_tlb_table[k].entries; -switch (intel_tlb_table[k].tlb_type) { +switch (intel_tlb_table[k].type) {case STLB_4K : tlb_lli_4k =max (tlb_lli_4k, entries); tlb_lld_4k =max (tlb_lld_4k, entries);
Signature · 1 line
2.48.1
kernel test robot
build botRe: [PATCH v2 19/29] x86/cpu: Use enums for TLB descriptor types
kernel test robot noticed the following build warnings:
Hi Ahmed,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 6d536cad0d55e71442b6d65500f74eb85544269e]
url: https://github.com/intel-lab-lkp/linux/commits/Ahmed-S-Darwish/x86-treewide-Introduce-x86_vendor_amd_or_hygon/20250318-011153 base: 6d536cad0d55e71442b6d65500f74eb85544269e patch link: https://lore.kernel.org/r/20250317164745.4754-20-darwi%40linutronix.de patch subject: [PATCH v2 19/29] x86/cpu: Use enums for TLB descriptor types config: i386-randconfig-061-20250319 (https://download.01.org/0day-ci/archive/20250319/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250319/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
drivers/cpuidle/poll_state.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...):
arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1"
>> arch/x86/include/asm/cpuid/types.h:85:1: sparse: sparse: static assertion failed: "sizeof(enum _tlb_table_type) == 1" -- drivers/cpuidle/governor.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" >> arch/x86/include/asm/cpuid/types.h:85:1: sparse: sparse: static assertion failed: "sizeof(enum _tlb_table_type) == 1" -- drivers/cpuidle/cpuidle-haltpoll.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" >> arch/x86/include/asm/cpuid/types.h:85:1: sparse: sparse: static assertion failed: "sizeof(enum _tlb_table_type) == 1" -- drivers/cpuidle/driver.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" >> arch/x86/include/asm/cpuid/types.h:85:1: sparse: sparse: static assertion failed: "sizeof(enum _tlb_table_type) == 1" -- drivers/cpuidle/sysfs.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" >> arch/x86/include/asm/cpuid/types.h:85:1: sparse: sparse: static assertion failed: "sizeof(enum _tlb_table_type) == 1" -- drivers/cpuidle/cpuidle.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" >> arch/x86/include/asm/cpuid/types.h:85:1: sparse: sparse: static assertion failed: "sizeof(enum _tlb_table_type) == 1" -- drivers/cpuidle/governors/menu.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" >> arch/x86/include/asm/cpuid/types.h:85:1: sparse: sparse: static assertion failed: "sizeof(enum _tlb_table_type) == 1" -- drivers/cpuidle/governors/haltpoll.c: note: in included file (through arch/x86/include/asm/cpuid/api.h, arch/x86/include/asm/cpuid.h, arch/x86/include/asm/processor.h, ...): arch/x86/include/asm/cpuid/types.h:56:1: sparse: sparse: static assertion failed: "sizeof(enum _cache_table_type) == 1" >> arch/x86/include/asm/cpuid/types.h:85:1: sparse: sparse: static assertion failed: "sizeof(enum _tlb_table_type) == 1"
vim +85 arch/x86/include/asm/cpuid/types.h
45
46 /*
47 * Leaf 0x2 1-byte descriptors' cache types
48 * To be used for their mappings at cache_table[]
49 */
50 enum _cache_table_type {
51 CACHE_L1_INST,
52 CACHE_L1_DATA,
53 CACHE_L2,
54 CACHE_L3
55 } __packed;
> 56 static_assert(sizeof(enum _cache_table_type) == 1);
57
58 /*
59 * Leaf 0x2 1-byte descriptors' TLB types
60 * To be used for their mappings at intel_tlb_table[]
61 *
62 * Start at 1 since type 0 is reserved for HW byte descriptors which are
63 * not recognized by the kernel; i.e., those without an explicit mapping.
64 */
65 enum _tlb_table_type {
66 TLB_INST_4K = 1,
67 TLB_INST_4M,
68 TLB_INST_2M_4M,
69 TLB_INST_ALL,
70
71 TLB_DATA_4K,
72 TLB_DATA_4M,
73 TLB_DATA_2M_4M,
74 TLB_DATA_4K_4M,
75 TLB_DATA_1G,
76 TLB_DATA_1G_2M_4M,
77
78 TLB_DATA0_4K,
79 TLB_DATA0_4M,
80 TLB_DATA0_2M_4M,
81
82 STLB_4K,
83 STLB_4K_2M,
84 } __packed;
> 85 static_assert(sizeof(enum _tlb_table_type) == 1);
86 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Ahmed S. Darwish
reviewerRe: [PATCH v2 19/29] x86/cpu: Use enums for TLB descriptor types
Quoted 4 lines · kernel test robot
arch/x86/include/asm/cpuid/types.h:56:1:
sparse: static assertion failed: "sizeof(enum _cache_table_type)== 1"
I'll fix it in v4, as detailed in the other email:
https://lore.kernel.org/x86-cpuid/Z9rsTirs9lLfEPD9@lx-t490
Thanks,
Ahmed
Ahmed S. Darwish
reviewerPATCH v2 20/29 x86/cpu: Consolidate CPUID leaf 0x2 tables
In reply to Ahmed S. DarwishFrom: Thomas Gleixner <[email protected]>
CPUID leaf 0x2 describes TLBs and caches. So there are two tables with the respective descriptor constants in intel.c and cacheinfo.c. The tables occupy almost 600 byte and require a loop based lookup for each variant.
Combining them into one table occupies exactly 1k rodata and allows to get rid of the loop based lookup by just using the descriptor byte provided by CPUID leaf 0x2 as index into the table, which simplifies the code and reduces text size.
The conversion of the intel.c and cacheinfo.c code is done separately.
[ darwi: Actually define struct leaf_0x2_table.
Tab-align all of cpuid_0x2_table[] mapping entries.
Define needed SZ_* macros at <linux/sizes.h> instead (merged commit.) Use CACHE_L1_{INST,DATA} as names for L1 cache descriptor types. Set descriptor 0x63 type as TLB_DATA_1G_2M_4M and explain why. Use enums for cache and TLB descriptor types (parent commits.) Start enum types at 1 since type 0 is reserved for unknown descriptors. Ensure that cache and TLB enum type values do not intersect. Add leaf 0x2 table accessor for_each_leaf_0x2_entry() + documentation. ]
--- arch/x86/include/asm/cpuid/leaf_0x2_api.h | 33 +++++- arch/x86/include/asm/cpuid/types.h | 38 +++++-- arch/x86/kernel/cpu/Makefile | 2 +- arch/x86/kernel/cpu/cpuid_0x2_table.c | 128 ++++++++++++++++++++++ 4 files changed, 192 insertions(+), 9 deletions(-) create mode 100644 arch/x86/kernel/cpu/cpuid_0x2_table.c
arch/x86/include/asm/cpuid/leaf_0x2_api.h +32 -1
@@ -13,7 +13,8 @@* invalid 1-byte descriptor returned by the hardware to zero (the NULL * cache/TLB descriptor) before returning it to the caller. * -* Use for_each_leaf_0x2_desc() to iterate over the returned output. +* Use for_each_leaf_0x2_entry() to iterate over the register output in +* parsed form. */ static inline void cpuid_get_leaf_0x2_regs (union leaf_0x2_regs *regs) {
@@ -62,4 +63,34 @@ static inline void cpuid_get_leaf_0x2_regs(union leaf_0x2_regs *regs)#define for_each_leaf_0x2_desc (regs, desc) \ for (desc = &(regs).desc[1]; desc < &(regs).desc[16]; desc++) +/** +* for_each_leaf_0x2_entry() - Iterator for parsed leaf 0x2 descriptors +* @regs: Leaf 0x2 register output, returned by cpuid_get_leaf_0x2_regs() +* @__ptr: u8 pointer, for macro internal use only +* @entry: Pointer to parsed descriptor information at each iteration +* +* Loop over the 1-byte descriptors in the passed leaf 0x2 output registers +* @regs. Provide the parsed information for each descriptor through @entry. +* +* To handle cache-specific descriptors, switch on @entry->c_type. For TLB +* descriptors, switch on @entry->t_type. +* +* Example usage for cache descriptors:: +* +* const struct leaf_0x2_table *entry; +* union leaf_0x2_regs regs; +* u8 *ptr; +* +* cpuid_get_leaf_0x2_regs(®s); +* for_each_leaf_0x2_entry(regs, ptr, entry) { +* switch (entry->c_type) { +* ... +* } +* } +*/ +#define for_each_leaf_0x2_entry (regs, __ptr, entry) \ + for (__ptr = &(regs).desc[1], entry = &cpuid_0x2_table[*__ptr]; \ + __ptr < &(regs).desc[16]; \ + __ptr++, entry = &cpuid_0x2_table[*__ptr]) +#endif /* _ASM_X86_CPUID_LEAF_0x2_API_H */
arch/x86/include/asm/cpuid/types.h +31 -7
@@ -45,25 +45,32 @@ union leaf_0x2_regs {/* * Leaf 0x2 1-byte descriptors' cache types -* To be used for their mappings at cache_table[] +* To be used for their mappings at cpuid_0x2_table[] +* +* Start at 1 since type 0 is reserved for HW byte descriptors which are +* not recognized by the kernel; i.e., those without an explicit mapping. */ enum _cache_table_type { -CACHE_L1_INST , +CACHE_L1_INST =1 ,CACHE_L1_DATA ,CACHE_L2 ,CACHE_L3 +/* Adjust __TLB_TABLE_TYPE_BEGIN before adding more types */ } __packed;static_assert (sizeof (enum _cache_table_type ) ==1 ); +/* +* Ensure that leaf 0x2 cache and TLB type values do not intersect, +* since they share the same type field at struct cpuid_0x2_table. +*/ +#define __TLB_TABLE_TYPE_BEGIN (CACHE_L3 + 1) +/* * Leaf 0x2 1-byte descriptors' TLB types -* To be used for their mappings at intel_tlb_table[] -* -* Start at 1 since type 0 is reserved for HW byte descriptors which are -* not recognized by the kernel; i.e., those without an explicit mapping. +* To be used for their mappings at cpuid_0x2_table[] */ enum _tlb_table_type { -TLB_INST_4K =1 , +TLB_INST_4K = __TLB_TABLE_TYPE_BEGIN,TLB_INST_4M ,TLB_INST_2M_4M ,TLB_INST_ALL ,
@@ -84,4 +91,21 @@ enum _tlb_table_type { } __packed;static_assert (sizeof (enum _tlb_table_type ) ==1 ); +/* +* Combined parsing table for leaf 0x2 cache and TLB descriptors. +*/ + +struct leaf_0x2_table { +union { +enum _cache_table_type c_type; +enum _tlb_table_type t_type; + }; +union { +short c_size; +short entries; + }; +}; + +extern const struct leaf_0x2_table cpuid_0x2_table[256 ]; +#endif /* _ASM_X86_CPUID_TYPES_H */
arch/x86/kernel/cpu/Makefile +1 -1
@@ -24,7 +24,7 @@ obj-y += rdrand.o obj-y += match.o obj-y += bugs.o obj-y += aperfmperf.o -obj-y += cpuid-deps.o +obj-y += cpuid-deps.o cpuid_0x2_table.o obj-y += umwait.o obj-y += capflags.o powerflags.o
arch/x86/kernel/cpu/cpuid_0x2_table.c new file +128 -0
128 lines not on this page · raw message
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 21/29 x86/cacheinfo: Use consolidated leaf 0x2 descriptor table
In reply to Ahmed S. DarwishCPUID leaf 0x2 output is a stream of one-byte descriptors, each implying certain details about the CPU's cache and TLB entries.
At previous commits, the mapping tables for such descriptors were merged into one consolidated table. The mapping was also transformed into a hash lookup instead of a loop-based lookup for each descriptor.
Use the new consolidated table and its hash-based lookup through the for_each_leaf_0x2_tlb_entry() accessor. Remove the old cache-specific mapping, cache_table[], as it is no longer used.
--- arch/x86/kernel/cpu/cacheinfo.c | 114 +++----------------------------- 1 file changed, 8 insertions(+), 106 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +8 -106
@@ -34,90 +34,6 @@ static cpumask_var_t cpu_cacheinfo_mask;/* Kernel controls MTRR and/or PAT MSRs. */ unsigned int memory_caching_control__ro_after_init ; -struct _cache_table { -unsigned char descriptor; -enum _cache_table_type type; -short size; -}; - -#define MB (x) ((x) * 1024) - -/* All the cache descriptor types we care about (no TLB or -trace cache entries) */ - -static const struct _cache_table cache_table[] = -{ - {0x06 ,CACHE_L1_INST ,8 },/* 4-way set assoc, 32 byte line size */ - {0x08 ,CACHE_L1_INST ,16 },/* 4-way set assoc, 32 byte line size */ - {0x09 ,CACHE_L1_INST ,32 },/* 4-way set assoc, 64 byte line size */ - {0x0a ,CACHE_L1_DATA ,8 },/* 2 way set assoc, 32 byte line size */ - {0x0c ,CACHE_L1_DATA ,16 },/* 4-way set assoc, 32 byte line size */ - {0x0d ,CACHE_L1_DATA ,16 },/* 4-way set assoc, 64 byte line size */ - {0x0e ,CACHE_L1_DATA ,24 },/* 6-way set assoc, 64 byte line size */ - {0x21 ,CACHE_L2 ,256 },/* 8-way set assoc, 64 byte line size */ - {0x22 ,CACHE_L3 ,512 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x23 ,CACHE_L3 ,MB (1 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x25 ,CACHE_L3 ,MB (2 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x29 ,CACHE_L3 ,MB (4 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x2c ,CACHE_L1_DATA ,32 },/* 8-way set assoc, 64 byte line size */ - {0x30 ,CACHE_L1_INST ,32 },/* 8-way set assoc, 64 byte line size */ - {0x39 ,CACHE_L2 ,128 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x3a ,CACHE_L2 ,192 },/* 6-way set assoc, sectored cache, 64 byte line size */ - {0x3b ,CACHE_L2 ,128 },/* 2-way set assoc, sectored cache, 64 byte line size */ - {0x3c ,CACHE_L2 ,256 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x3d ,CACHE_L2 ,384 },/* 6-way set assoc, sectored cache, 64 byte line size */ - {0x3e ,CACHE_L2 ,512 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x3f ,CACHE_L2 ,256 },/* 2-way set assoc, 64 byte line size */ - {0x41 ,CACHE_L2 ,128 },/* 4-way set assoc, 32 byte line size */ - {0x42 ,CACHE_L2 ,256 },/* 4-way set assoc, 32 byte line size */ - {0x43 ,CACHE_L2 ,512 },/* 4-way set assoc, 32 byte line size */ - {0x44 ,CACHE_L2 ,MB (1 ) },/* 4-way set assoc, 32 byte line size */ - {0x45 ,CACHE_L2 ,MB (2 ) },/* 4-way set assoc, 32 byte line size */ - {0x46 ,CACHE_L3 ,MB (4 ) },/* 4-way set assoc, 64 byte line size */ - {0x47 ,CACHE_L3 ,MB (8 ) },/* 8-way set assoc, 64 byte line size */ - {0x48 ,CACHE_L2 ,MB (3 ) },/* 12-way set assoc, 64 byte line size */ - {0x49 ,CACHE_L3 ,MB (4 ) },/* 16-way set assoc, 64 byte line size */ - {0x4a ,CACHE_L3 ,MB (6 ) },/* 12-way set assoc, 64 byte line size */ - {0x4b ,CACHE_L3 ,MB (8 ) },/* 16-way set assoc, 64 byte line size */ - {0x4c ,CACHE_L3 ,MB (12 ) },/* 12-way set assoc, 64 byte line size */ - {0x4d ,CACHE_L3 ,MB (16 ) },/* 16-way set assoc, 64 byte line size */ - {0x4e ,CACHE_L2 ,MB (6 ) },/* 24-way set assoc, 64 byte line size */ - {0x60 ,CACHE_L1_DATA ,16 },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x66 ,CACHE_L1_DATA ,8 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x67 ,CACHE_L1_DATA ,16 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x68 ,CACHE_L1_DATA ,32 },/* 4-way set assoc, sectored cache, 64 byte line size */ - {0x78 ,CACHE_L2 ,MB (1 ) },/* 4-way set assoc, 64 byte line size */ - {0x79 ,CACHE_L2 ,128 },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x7a ,CACHE_L2 ,256 },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x7b ,CACHE_L2 ,512 },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x7c ,CACHE_L2 ,MB (1 ) },/* 8-way set assoc, sectored cache, 64 byte line size */ - {0x7d ,CACHE_L2 ,MB (2 ) },/* 8-way set assoc, 64 byte line size */ - {0x7f ,CACHE_L2 ,512 },/* 2-way set assoc, 64 byte line size */ - {0x80 ,CACHE_L2 ,512 },/* 8-way set assoc, 64 byte line size */ - {0x82 ,CACHE_L2 ,256 },/* 8-way set assoc, 32 byte line size */ - {0x83 ,CACHE_L2 ,512 },/* 8-way set assoc, 32 byte line size */ - {0x84 ,CACHE_L2 ,MB (1 ) },/* 8-way set assoc, 32 byte line size */ - {0x85 ,CACHE_L2 ,MB (2 ) },/* 8-way set assoc, 32 byte line size */ - {0x86 ,CACHE_L2 ,512 },/* 4-way set assoc, 64 byte line size */ - {0x87 ,CACHE_L2 ,MB (1 ) },/* 8-way set assoc, 64 byte line size */ - {0xd0 ,CACHE_L3 ,512 },/* 4-way set assoc, 64 byte line size */ - {0xd1 ,CACHE_L3 ,MB (1 ) },/* 4-way set assoc, 64 byte line size */ - {0xd2 ,CACHE_L3 ,MB (2 ) },/* 4-way set assoc, 64 byte line size */ - {0xd6 ,CACHE_L3 ,MB (1 ) },/* 8-way set assoc, 64 byte line size */ - {0xd7 ,CACHE_L3 ,MB (2 ) },/* 8-way set assoc, 64 byte line size */ - {0xd8 ,CACHE_L3 ,MB (4 ) },/* 12-way set assoc, 64 byte line size */ - {0xdc ,CACHE_L3 ,MB (2 ) },/* 12-way set assoc, 64 byte line size */ - {0xdd ,CACHE_L3 ,MB (4 ) },/* 12-way set assoc, 64 byte line size */ - {0xde ,CACHE_L3 ,MB (8 ) },/* 12-way set assoc, 64 byte line size */ - {0xe2 ,CACHE_L3 ,MB (2 ) },/* 16-way set assoc, 64 byte line size */ - {0xe3 ,CACHE_L3 ,MB (4 ) },/* 16-way set assoc, 64 byte line size */ - {0xe4 ,CACHE_L3 ,MB (8 ) },/* 16-way set assoc, 64 byte line size */ - {0xea ,CACHE_L3 ,MB (12 ) },/* 24-way set assoc, 64 byte line size */ - {0xeb ,CACHE_L3 ,MB (18 ) },/* 24-way set assoc, 64 byte line size */ - {0xec ,CACHE_L3 ,MB (24 ) },/* 24-way set assoc, 64 byte line size */ -}; - -enum _cache_type {CTYPE_NULL =0 ,CTYPE_DATA =1 ,
@@ -436,16 +352,6 @@ void init_hygon_cacheinfo(struct cpuinfo_x86 *c) ci->num_leaves =find_num_cache_leaves (c); } -static const struct _cache_table *cache_table_get (u8 desc) -{ -for (int i =0 ; i <ARRAY_SIZE (cache_table); i++) { -if (cache_table[i].descriptor == desc) -return &cache_table[i]; - } - -return NULL ; -} -void init_intel_cacheinfo (struct cpuinfo_x86 *c) {/* Cache sizes */
@@ -502,21 +408,17 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)/* Don't use CPUID(2) if CPUID(4) is supported. */ if (!ci->num_leaves && c->cpuid_level >1 ) { -const struct _cache_table *entry; +const struct leaf_0x2_table *entry;union leaf_0x2_regs regs; -u8 *desc; +u8 *ptr;cpuid_get_leaf_0x2_regs (®s); -for_each_leaf_0x2_desc (regs, desc) { - entry =cache_table_get (*desc); -if (!entry) -continue ; - -switch (entry->type) { -case CACHE_L1_INST : l1i += entry->size;break ; -case CACHE_L1_DATA : l1d += entry->size;break ; -case CACHE_L2 : l2 += entry->size;break ; -case CACHE_L3 : l3 += entry->size;break ; +for_each_leaf_0x2_entry (regs, ptr, entry) { +switch (entry->c_type) { +case CACHE_L1_INST : l1i += entry->c_size;break ; +case CACHE_L1_DATA : l1d += entry->c_size;break ; +case CACHE_L2 : l2 += entry->c_size;break ; +case CACHE_L3 : l3 += entry->c_size;break ; } } }
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 22/29 x86/cpu: Use consolidated leaf 0x2 descriptor table
In reply to Ahmed S. DarwishCPUID leaf 0x2 output is a stream of one-byte descriptors, each implying certain details about the CPU's cache and TLB entries.
At previous commits, the mapping tables for such descriptors were merged into one consolidated table. The mapping was also transformed into a hash lookup instead of a loop-based lookup for each descriptor.
Use the new consolidated table and its hash-based lookup through the for_each_leaf_0x2_tlb_entry() accessor.
Remove the TLB-specific mapping, intel_tlb_table[], as it is now no longer used. Remove the <cpuid/types.h> macro, for_each_leaf_0x2_desc(), since the converted code was its last user.
--- arch/x86/include/asm/cpuid/types.h | 10 ++++ arch/x86/kernel/cpu/intel.c | 83 +++--------------------------- 2 files changed, 17 insertions(+), 76 deletions(-)
arch/x86/include/asm/cpuid/types.h +10 -0
@@ -108,4 +108,14 @@ struct leaf_0x2_table {extern const struct leaf_0x2_table cpuid_0x2_table[256 ]; +/* +* All of leaf 0x2's one-byte TLB descriptors implies the same number of entries +* for their respective TLB types. TLB descriptor 0x63 is an exception: it +* implies 4 dTLB entries for 1GB pages and 32 dTLB entries for 2MB or 4MB pages. +* +* Encode that descriptor's dTLB entry count for 2MB/4MB pages here, as the entry +* count for dTLB 1GB pages is already encoded at the cpuid_0x2_table[]'s mapping. +*/ +#define TLB_0x63_2M_4M_ENTRIES 32 +#endif /* _ASM_X86_CPUID_TYPES_H */
arch/x86/kernel/cpu/intel.c +7 -76
103 lines not on this page · raw message
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 23/29 x86/cacheinfo: Separate leaf 0x2 handling and post-processing logic
In reply to Ahmed S. DarwishThe logic of init_intel_cacheinfo() is quite convoluted: it mixes leaf 0x4 parsing, leaf 0x2 parsing, plus some post-processing, in a single place.
Begin simplifying its logic by extracting the leaf 0x2 parsing code, and the post-processing logic, into their own functions. While at it, rework the SMT LLC topology ID comment for clarity.
--- arch/x86/kernel/cpu/cacheinfo.c | 106 +++++++++++++++++--------------- 1 file changed, 58 insertions(+), 48 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +58 -48
@@ -352,14 +352,56 @@ void init_hygon_cacheinfo(struct cpuinfo_x86 *c) ci->num_leaves =find_num_cache_leaves (c); } -void init_intel_cacheinfo (struct cpuinfo_x86 *c) +static void intel_cacheinfo_done (struct cpuinfo_x86 *c,unsigned int l3, +unsigned int l2,unsigned int l1i,unsigned int l1d) +{ +/* +* If llc_id is still unset, then cpuid_level < 4, which implies +* that the only possibility left is SMT. Since CPUID(2) doesn't +* specify any shared caches and SMT shares all caches, we can +* unconditionally set LLC ID to the package ID so that all +* threads share it. +*/ +if (c->topo.llc_id ==BAD_APICID ) + c->topo.llc_id = c->topo.pkg_id; + + c->x86_cache_size = l3 ? l3 : (l2 ? l2 : l1i + l1d); + +if (!l2) +cpu_detect_cache_sizes (c); +} + +/* +* Legacy Intel CPUID(2) path if CPUID(4) is not available. +*/ +static void intel_cacheinfo_0x2 (struct cpuinfo_x86 *c) { -/* Cache sizes */ unsigned int l1i =0 , l1d =0 , l2 =0 , l3 =0 ; -unsigned int new_l1d =0 , new_l1i =0 ;/* Cache sizes from cpuid(4) */ -unsigned int new_l2 =0 , new_l3 =0 , i;/* Cache sizes from cpuid(4) */ -unsigned int l2_id =0 , l3_id =0 , num_threads_sharing, index_msb; +const struct leaf_0x2_table *entry; +union leaf_0x2_regs regs; +u8 *ptr; + +if (c->cpuid_level <2 ) +return ; + +cpuid_get_leaf_0x2_regs (®s); +for_each_leaf_0x2_entry (regs, ptr, entry) { +switch (entry->c_type) { +case CACHE_L1_INST : l1i += entry->c_size;break ; +case CACHE_L1_DATA : l1d += entry->c_size;break ; +case CACHE_L2 : l2 += entry->c_size;break ; +case CACHE_L3 : l3 += entry->c_size;break ; + } + } + +intel_cacheinfo_done (c, l3, l2, l1i, l1d); +} + +void init_intel_cacheinfo (struct cpuinfo_x86 *c) +{struct cpu_cacheinfo *ci =get_cpu_cacheinfo (c->cpu_index); +unsigned int l1i =0 , l1d =0 , l2 =0 , l3 =0 ; +unsigned int l2_id =0 , l3_id =0 ;if (c->cpuid_level >3 ) {/*
@@ -373,7 +415,8 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)* Whenever possible use cpuid(4), deterministic cache * parameters cpuid leaf to find the cache details */ -for (i =0 ; i < ci->num_leaves; i++) { +for (int i =0 ; i < ci->num_leaves; i++) { +unsigned int num_threads_sharing, index_msb;struct _cpuid4_info id4 = {};int retval;
@@ -384,18 +427,18 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)switch (id4.eax.split.level) {case 1 :if (id4.eax.split.type ==CTYPE_DATA ) - new_l1d = id4.size/1024 ; + l1d = id4.size /1024 ;else if (id4.eax.split.type ==CTYPE_INST ) - new_l1i = id4.size/1024 ; + l1i = id4.size /1024 ;break ;case 2 : - new_l2 = id4.size/1024 ; + l2 = id4.size /1024 ; num_threads_sharing =1 + id4.eax.split.num_threads_sharing; index_msb =get_count_order (num_threads_sharing); l2_id = c->topo.apicid & ~((1 << index_msb) -1 );break ;case 3 : - new_l3 = id4.size/1024 ; + l3 = id4.size /1024 ; num_threads_sharing =1 + id4.eax.split.num_threads_sharing; index_msb =get_count_order (num_threads_sharing); l3_id = c->topo.apicid & ~((1 << index_msb) -1 );
@@ -408,52 +451,19 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)/* Don't use CPUID(2) if CPUID(4) is supported. */ if (!ci->num_leaves && c->cpuid_level >1 ) { -const struct leaf_0x2_table *entry; -union leaf_0x2_regs regs; -u8 *ptr; - -cpuid_get_leaf_0x2_regs (®s); -for_each_leaf_0x2_entry (regs, ptr, entry) { -switch (entry->c_type) { -case CACHE_L1_INST : l1i += entry->c_size;break ; -case CACHE_L1_DATA : l1d += entry->c_size;break ; -case CACHE_L2 : l2 += entry->c_size;break ; -case CACHE_L3 : l3 += entry->c_size;break ; - } - } +intel_cacheinfo_0x2 (c); +return ; } -if (new_l1d) - l1d = new_l1d; - -if (new_l1i) - l1i = new_l1i; - -if (new_l2) { - l2 = new_l2; +if (l2) { c->topo.llc_id = l2_id; c->topo.l2c_id = l2_id; } -if (new_l3) { - l3 = new_l3; +if (l3) c->topo.llc_id = l3_id; - } -/* -* If llc_id is not yet set, this means cpuid_level < 4 which in -* turns means that the only possibility is SMT (as indicated in -* cpuid1). Since cpuid2 doesn't specify shared caches, and we know -* that SMT shares all caches, we can unconditionally set cpu_llc_id to -* c->topo.pkg_id. -*/ -if (c->topo.llc_id ==BAD_APICID ) - c->topo.llc_id = c->topo.pkg_id; - - c->x86_cache_size = l3 ? l3 : (l2 ? l2 : (l1i+l1d)); - -if (!l2) -cpu_detect_cache_sizes (c); +intel_cacheinfo_done (c, l3, l2, l1i, l1d); } static int __cache_amd_cpumap_setup(unsigned int cpu, int index,
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 24/29 x86/cacheinfo: Separate Intel leaf 0x4 handling
In reply to Ahmed S. Darwishinit_intel_cacheinfo() was overly complex. It parsed leaf 0x4 data, leaf 0x2 data, and performed post-processing, all within one function. Parent commit moved leaf 0x2 parsing and the post-processing logic into their own functions.
Continue the refactoring by extracting leaf 0x4 parsing into its own function. Initialize local L2/L3 topology ID variables to BAD_APICID by default, thus ensuring they can be used unconditionally.
--- arch/x86/kernel/cpu/cacheinfo.c | 110 ++++++++++++++++---------------- 1 file changed, 54 insertions(+), 56 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +54 -56
@@ -397,73 +397,71 @@ static void intel_cacheinfo_0x2(struct cpuinfo_x86 *c)intel_cacheinfo_done (c, l3, l2, l1i, l1d); } -void init_intel_cacheinfo (struct cpuinfo_x86 *c) +static bool intel_cacheinfo_0x4 (struct cpuinfo_x86 *c) {struct cpu_cacheinfo *ci =get_cpu_cacheinfo (c->cpu_index); -unsigned int l1i =0 , l1d =0 , l2 =0 , l3 =0 ; -unsigned int l2_id =0 , l3_id =0 ; - -if (c->cpuid_level >3 ) { -/* -* There should be at least one leaf. A non-zero value means -* that the number of leaves has been initialized. -*/ -if (!ci->num_leaves) - ci->num_leaves =find_num_cache_leaves (c); +unsigned int l2_id =BAD_APICID , l3_id =BAD_APICID ; +unsigned int l1d =0 , l1i =0 , l2 =0 , l3 =0 ; -/* -* Whenever possible use cpuid(4), deterministic cache -* parameters cpuid leaf to find the cache details -*/ -for (int i =0 ; i < ci->num_leaves; i++) { -unsigned int num_threads_sharing, index_msb; -struct _cpuid4_info id4 = {}; -int retval; +if (c->cpuid_level <4 ) +return false; - retval =intel_fill_cpuid4_info (i, &id4); -if (retval <0 ) -continue ; +/* +* There should be at least one leaf. A non-zero value means +* that the number of leaves has been previously initialized. +*/ +if (!ci->num_leaves) + ci->num_leaves =find_num_cache_leaves (c); -switch (id4.eax.split.level) { -case 1 : -if (id4.eax.split.type ==CTYPE_DATA ) - l1d = id4.size /1024 ; -else if (id4.eax.split.type ==CTYPE_INST ) - l1i = id4.size /1024 ; -break ; -case 2 : - l2 = id4.size /1024 ; - num_threads_sharing =1 + id4.eax.split.num_threads_sharing; - index_msb =get_count_order (num_threads_sharing); - l2_id = c->topo.apicid & ~((1 << index_msb) -1 ); -break ; -case 3 : - l3 = id4.size /1024 ; - num_threads_sharing =1 + id4.eax.split.num_threads_sharing; - index_msb =get_count_order (num_threads_sharing); - l3_id = c->topo.apicid & ~((1 << index_msb) -1 ); -break ; -default : -break ; - } +if (!ci->num_leaves) +return false; + +for (int i =0 ; i < ci->num_leaves; i++) { +unsigned int num_threads_sharing, index_msb; +struct _cpuid4_info id4 = {}; +int ret; + + ret =intel_fill_cpuid4_info (i, &id4); +if (ret <0 ) +continue ; + +switch (id4.eax.split.level) { +case 1 : +if (id4.eax.split.type ==CTYPE_DATA ) + l1d = id4.size /1024 ; +else if (id4.eax.split.type ==CTYPE_INST ) + l1i = id4.size /1024 ; +break ; +case 2 : + l2 = id4.size /1024 ; + num_threads_sharing =1 + id4.eax.split.num_threads_sharing; + index_msb =get_count_order (num_threads_sharing); + l2_id = c->topo.apicid & ~((1 << index_msb) -1 ); +break ; +case 3 : + l3 = id4.size /1024 ; + num_threads_sharing =1 + id4.eax.split.num_threads_sharing; + index_msb =get_count_order (num_threads_sharing); + l3_id = c->topo.apicid & ~((1 << index_msb) -1 ); +break ; +default : +break ; } } + c->topo.l2c_id = l2_id; + c->topo.llc_id = (l3_id ==BAD_APICID ) ? l2_id : l3_id; +intel_cacheinfo_done (c, l3, l2, l1i, l1d); +return true; +} + +void init_intel_cacheinfo (struct cpuinfo_x86 *c) +{/* Don't use CPUID(2) if CPUID(4) is supported. */ -if (!ci->num_leaves && c->cpuid_level >1 ) { -intel_cacheinfo_0x2 (c); +if (intel_cacheinfo_0x4 (c))return ; - } - -if (l2) { - c->topo.llc_id = l2_id; - c->topo.l2c_id = l2_id; - } - -if (l3) - c->topo.llc_id = l3_id; -intel_cacheinfo_done (c, l3, l2, l1i, l1d); +intel_cacheinfo_0x2 (c); } static int __cache_amd_cpumap_setup(unsigned int cpu, int index,
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 25/29 x86/cacheinfo: Extract out cache level topology ID calculation
In reply to Ahmed S. DarwishFor Intel leaf 0x4 parsing, refactor the cache level topology ID calculation code into its own method instead of repeating the same logic twice for L2 and L3.
--- arch/x86/kernel/cpu/cacheinfo.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +12 -7
@@ -397,6 +397,16 @@ static void intel_cacheinfo_0x2(struct cpuinfo_x86 *c)intel_cacheinfo_done (c, l3, l2, l1i, l1d); } +static unsigned int calc_cache_topo_id (struct cpuinfo_x86 *c,const struct _cpuid4_info *id4) +{ +unsigned int num_threads_sharing; +int index_msb; + + num_threads_sharing =1 + id4->eax.split.num_threads_sharing; + index_msb =get_count_order (num_threads_sharing); +return c->topo.apicid & ~((1 << index_msb) -1 ); +} +static bool intel_cacheinfo_0x4 (struct cpuinfo_x86 *c) {struct cpu_cacheinfo *ci =get_cpu_cacheinfo (c->cpu_index);
@@ -417,7 +427,6 @@ static bool intel_cacheinfo_0x4(struct cpuinfo_x86 *c)return false;for (int i =0 ; i < ci->num_leaves; i++) { -unsigned int num_threads_sharing, index_msb;struct _cpuid4_info id4 = {};int ret;
@@ -434,15 +443,11 @@ static bool intel_cacheinfo_0x4(struct cpuinfo_x86 *c)break ;case 2 : l2 = id4.size /1024 ; - num_threads_sharing =1 + id4.eax.split.num_threads_sharing; - index_msb =get_count_order (num_threads_sharing); - l2_id = c->topo.apicid & ~((1 << index_msb) -1 ); + l2_id =calc_cache_topo_id (c, &id4);break ;case 3 : l3 = id4.size /1024 ; - num_threads_sharing =1 + id4.eax.split.num_threads_sharing; - index_msb =get_count_order (num_threads_sharing); - l3_id = c->topo.apicid & ~((1 << index_msb) -1 ); + l3_id =calc_cache_topo_id (c, &id4);break ;default :break ;
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 26/29 x86/cacheinfo: Extract out cache self-snoop checks
In reply to Ahmed S. DarwishThe logic of not doing a cache flush if the CPU declares cache self snooping support is repeated across the x86/cacheinfo code. Extract it into its own function.
--- arch/x86/kernel/cpu/cacheinfo.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +13 -11
@@ -641,6 +641,17 @@ int populate_cache_leaves(unsigned int cpu)static unsigned long saved_cr4;static DEFINE_RAW_SPINLOCK (cache_disable_lock); +/* +* Cache flushing is the most time-consuming step when programming the +* MTRRs. On many Intel CPUs without known erratas, it can be skipped +* if the CPU declares cache self-snooping support. +*/ +static void maybe_flush_caches (void ) +{ +if (!static_cpu_has (X86_FEATURE_SELFSNOOP )) +wbinvd (); +} +void cache_disable (void )__acquires (cache_disable_lock) {unsigned long cr0;
@@ -658,14 +669,7 @@ void cache_disable(void) __acquires(cache_disable_lock) cr0 =read_cr0 () |X86_CR0_CD ;write_cr0 (cr0); -/* -* Cache flushing is the most time-consuming step when programming -* the MTRRs. Fortunately, as per the Intel Software Development -* Manual, we can skip it if the processor supports cache self- -* snooping. -*/ -if (!static_cpu_has (X86_FEATURE_SELFSNOOP )) -wbinvd (); +maybe_flush_caches ();/* Save value of CR4 and clear Page Global Enable (bit 7) */ if (cpu_feature_enabled (X86_FEATURE_PGE )) {
@@ -680,9 +684,7 @@ void cache_disable(void) __acquires(cache_disable_lock)if (cpu_feature_enabled (X86_FEATURE_MTRR ))mtrr_disable (); -/* Again, only flush caches if we have to. */ -if (!static_cpu_has (X86_FEATURE_SELFSNOOP )) -wbinvd (); +maybe_flush_caches (); } void cache_enable(void) __releases(cache_disable_lock)
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 27/29 x86/cacheinfo: Relocate leaf 0x4 cache_type mapping
In reply to Ahmed S. DarwishThe cache_type_map[] array is used to map Intel leaf 0x4 cache_type values to their corresponding types at <linux/cacheinfo.h>.
Move that array's definition after the actual CPUID leaf 0x4 structures, instead of having it in the middle of AMD leaf 0x4 emulation code.
--- arch/x86/kernel/cpu/cacheinfo.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +8 -7
@@ -78,6 +78,14 @@ struct _cpuid4_info {unsigned long size; }; +/* Map CPUID(4) EAX.cache_type to linux/cacheinfo.h types */ +static const enum cache_type cache_type_map[] = { + [CTYPE_NULL ] =CACHE_TYPE_NOCACHE , + [CTYPE_DATA ] =CACHE_TYPE_DATA , + [CTYPE_INST ] =CACHE_TYPE_INST , + [CTYPE_UNIFIED ] =CACHE_TYPE_UNIFIED , +}; +/* * Fallback AMD CPUID(4) emulation * AMD CPUs with TOPOEXT can just use CPUID(0x8000001d)
@@ -131,13 +139,6 @@ static const unsigned short assocs[] = {static const unsigned char levels[] = {1 ,1 ,2 ,3 };static const unsigned char types[] = {1 ,2 ,3 ,3 }; -static const enum cache_type cache_type_map[] = { - [CTYPE_NULL ] =CACHE_TYPE_NOCACHE , - [CTYPE_DATA ] =CACHE_TYPE_DATA , - [CTYPE_INST ] =CACHE_TYPE_INST , - [CTYPE_UNIFIED ] =CACHE_TYPE_UNIFIED , -}; -static void legacy_amd_cpuid4 (int index,union _cpuid4_leaf_eax *eax,union _cpuid4_leaf_ebx *ebx,union _cpuid4_leaf_ecx *ecx) {
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 28/29 x86/cacheinfo: Introduce cpuid_amd_hygon_has_l3_cache()
In reply to Ahmed S. DarwishMultiple code paths at cacheinfo.c and amd_nb.c check for AMD/Hygon CPUs L3 cache presensce by directly checking leaf 0x80000006 EDX output.
Extract that logic into its own function. While at it, rework the AMD/Hygon LLC topology ID caclculation comments for clarity.
--- arch/x86/include/asm/cpuid/api.h | 9 +++++++++ arch/x86/kernel/amd_nb.c | 7 +++---- arch/x86/kernel/cpu/cacheinfo.c | 32 ++++++++++++++------------------ 3 files changed, 26 insertions(+), 22 deletions(-)
arch/x86/include/asm/cpuid/api.h +9 -0
@@ -205,4 +205,13 @@ static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves) return 0; } +/* +* CPUID(0x80000006) parsing helpers +*/ + +static inline bool cpuid_amd_hygon_has_l3_cache (void ) +{ +return cpuid_edx (0x80000006 ); +} +#endif /* _ASM_X86_CPUID_API_H */
arch/x86/kernel/amd_nb.c +3 -4
@@ -13,7 +13,9 @@#include <linux/export.h> #include <linux/spinlock.h> #include <linux/pci_ids.h> +#include <asm/amd_nb.h> +#include <asm/cpuid.h> static u32 *flush_words;
@@ -92,10 +94,7 @@ static int amd_cache_northbridges(void)if (amd_gart_present ()) amd_northbridges.flags |=AMD_NB_GART ; -/* -* Check for L3 cache presence. -*/ -if (!cpuid_edx (0x80000006 )) +if (!cpuid_amd_hygon_has_l3_cache ())return 0 ;/*
arch/x86/kernel/cpu/cacheinfo.c +14 -18
@@ -278,29 +278,29 @@ static int find_num_cache_leaves(struct cpuinfo_x86 *c)return i; } +/* +* AMD/Hygon CPUs may have multiple LLCs if L3 caches exist. +*/ +void cacheinfo_amd_init_llc_id (struct cpuinfo_x86 *c,u16 die_id) { -/* -* We may have multiple LLCs if L3 caches exist, so check if we -* have an L3 cache by looking at the L3 cache CPUID leaf. -*/ -if (!cpuid_edx (0x80000006 )) +if (!cpuid_amd_hygon_has_l3_cache ())return ;if (c->x86 <0x17 ) { -/* LLC is at the node level. */ +/* Pre-Zen: LLC is at the node level */ c->topo.llc_id = die_id; }else if (c->x86 ==0x17 && c->x86_model <=0x1F ) {/* -* LLC is at the core complex level. -* Core complex ID is ApicId[3] for these processors. +* Family 17h up to 1F models: LLC is at the core +* complex level. Core complex ID is ApicId[3]. */ c->topo.llc_id = c->topo.apicid >>3 ; }else {/* -* LLC ID is calculated from the number of threads sharing the -* cache. -* */ +* Newer families: LLC ID is calculated from the number +* of threads sharing the L3 cache. +*/ u32 eax, ebx, ecx, edx, num_sharing_cache =0 ;u32 llc_index =find_num_cache_leaves (c) -1 ;
@@ -318,16 +318,12 @@ void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, u16 die_id)void cacheinfo_hygon_init_llc_id (struct cpuinfo_x86 *c) { -/* -* We may have multiple LLCs if L3 caches exist, so check if we -* have an L3 cache by looking at the L3 cache CPUID leaf. -*/ -if (!cpuid_edx (0x80000006 )) +if (!cpuid_amd_hygon_has_l3_cache ())return ;/* -* LLC is at the core complex level. -* Core complex ID is ApicId[3] for these processors. +* Hygons are similar to AMD Family 17h up to 1F models: LLC is +* at the core complex level. Core complex ID is ApicId[3]. */ c->topo.llc_id = c->topo.apicid >>3 ; }
Signature · 1 line
2.48.1
Ahmed S. Darwish
reviewerPATCH v2 29/29 x86/cacheinfo: Apply maintainer-tip coding style fixes
In reply to Ahmed S. DarwishThe x86/cacheinfo code has been heavily refactored and fleshed out at parent commits, where any necessary coding style fixes were also done in place.
Apply maintainer-tip.rst coding style fixes to the rest of the code, and align its assignment expressions for readability.
At cacheinfo_amd_init_llc_id(), rename variable msb to index_msb as this is how it's called at the rest of cacheinfo.c code.
--- arch/x86/kernel/cpu/cacheinfo.c | 211 ++++++++++++++++---------------- 1 file changed, 106 insertions(+), 105 deletions(-)
arch/x86/kernel/cpu/cacheinfo.c +106 -105
@@ -1,11 +1,11 @@// SPDX-License-Identifier: GPL-2.0 /* -* Routines to identify caches on Intel CPU. +* x86 CPU caches detection and configuration * -* Changes: -* Venkatesh Pallipadi : Adding cache identification through cpuid(4) -* Ashok Raj <[email protected]>: Work with CPU hotplug infrastructure. -* Andi Kleen / Andreas Herrmann : CPUID4 emulation on AMD. +* Previous changes +* - Venkatesh Pallipadi: Cache identification through CPUID(4) +* - Ashok Raj <[email protected]>: Work with CPU hotplug infrastructure +* - Andi Kleen / Andreas Herrmann: CPUID(4) emulation on AMD */ #include <linux/cacheinfo.h>
@@ -35,37 +35,37 @@ static cpumask_var_t cpu_cacheinfo_mask;unsigned int memory_caching_control__ro_after_init ;enum _cache_type { -CTYPE_NULL =0 , -CTYPE_DATA =1 , -CTYPE_INST =2 , -CTYPE_UNIFIED =3 +CTYPE_NULL =0 , +CTYPE_DATA =1 , +CTYPE_INST =2 , +CTYPE_UNIFIED =3 };union _cpuid4_leaf_eax {struct { -enum _cache_type type:5 ; -unsigned int level:3 ; -unsigned int is_self_initializing:1 ; -unsigned int is_fully_associative:1 ; -unsigned int reserved:4 ; -unsigned int num_threads_sharing:12 ; -unsigned int num_cores_on_die:6 ; +enum _cache_type type :5 ; +unsigned int level :3 ; +unsigned int is_self_initializing :1 ; +unsigned int is_fully_associative :1 ; +unsigned int reserved :4 ; +unsigned int num_threads_sharing :12 ; +unsigned int num_cores_on_die :6 ; } split;u32 full; };union _cpuid4_leaf_ebx {struct { -unsigned int coherency_line_size:12 ; -unsigned int physical_line_partition:10 ; -unsigned int ways_of_associativity:10 ; +unsigned int coherency_line_size :12 ; +unsigned int physical_line_partition :10 ; +unsigned int ways_of_associativity :10 ; } split;u32 full; };union _cpuid4_leaf_ecx {struct { -unsigned int number_of_sets:32 ; +unsigned int number_of_sets :32 ; } split;u32 full; };
@@ -93,60 +93,59 @@ static const enum cache_type cache_type_map[] = {union l1_cache {struct { -unsigned line_size:8 ; -unsigned lines_per_tag:8 ; -unsigned assoc:8 ; -unsigned size_in_kb:8 ; +unsigned line_size :8 ; +unsigned lines_per_tag :8 ; +unsigned assoc :8 ; +unsigned size_in_kb :8 ; }; -unsigned val; +unsigned int val; };union l2_cache {struct { -unsigned line_size:8 ; -unsigned lines_per_tag:4 ; -unsigned assoc:4 ; -unsigned size_in_kb:16 ; +unsigned line_size :8 ; +unsigned lines_per_tag :4 ; +unsigned assoc :4 ; +unsigned size_in_kb :16 ; }; -unsigned val; +unsigned int val; };union l3_cache {struct { -unsigned line_size:8 ; -unsigned lines_per_tag:4 ; -unsigned assoc:4 ; -unsigned res:2 ; -unsigned size_encoded:14 ; +unsigned line_size :8 ; +unsigned lines_per_tag :4 ; +unsigned assoc :4 ; +unsigned res :2 ; +unsigned size_encoded :14 ; }; -unsigned val; +unsigned int val; };static const unsigned short assocs[] = { - [1 ] =1 , - [2 ] =2 , - [4 ] =4 , - [6 ] =8 , - [8 ] =16 , - [0xa ] =32 , - [0xb ] =48 , - [0xc ] =64 , - [0xd ] =96 , - [0xe ] =128 , - [0xf ] =0xffff /* fully associative - no way to show this currently */ + [1 ] =1 , + [2 ] =2 , + [4 ] =4 , + [6 ] =8 , + [8 ] =16 , + [0xa ] =32 , + [0xb ] =48 , + [0xc ] =64 , + [0xd ] =96 , + [0xe ] =128 , + [0xf ] =0xffff /* Fully associative */ };static const unsigned char levels[] = {1 ,1 ,2 ,3 }; -static const unsigned char types[] = {1 ,2 ,3 ,3 }; +static const unsigned char types[] = {1 ,2 ,3 ,3 };static void legacy_amd_cpuid4 (int index,union _cpuid4_leaf_eax *eax,union _cpuid4_leaf_ebx *ebx,union _cpuid4_leaf_ecx *ecx) {unsigned int dummy, line_size, lines_per_tag, assoc, size_in_kb; -union l1_cache l1i, l1d; +union l1_cache l1i, l1d, *l1;union l2_cache l2;union l3_cache l3; -union l1_cache *l1 = &l1d; eax->full =0 ; ebx->full =0 ;
@@ -155,6 +154,7 @@ static void legacy_amd_cpuid4(int index, union _cpuid4_leaf_eax *eax,cpuid (0x80000005 , &dummy, &dummy, &l1d.val, &l1i.val);cpuid (0x80000006 , &dummy, &dummy, &l2.val, &l3.val); + l1 = &l1d;switch (index) {case 1 : l1 = &l1i;
@@ -162,48 +162,52 @@ static void legacy_amd_cpuid4(int index, union _cpuid4_leaf_eax *eax,case 0 :if (!l1->val)return ; - assoc = assocs[l1->assoc]; - line_size = l1->line_size; - lines_per_tag = l1->lines_per_tag; - size_in_kb = l1->size_in_kb; + + assoc = assocs[l1->assoc]; + line_size = l1->line_size; + lines_per_tag = l1->lines_per_tag; + size_in_kb = l1->size_in_kb;break ;case 2 :if (!l2.val)return ; - assoc = assocs[l2.assoc]; - line_size = l2.line_size; - lines_per_tag = l2.lines_per_tag; -/* cpu_data has errata corrections for K7 applied */ - size_in_kb =__this_cpu_read (cpu_info.x86_cache_size); + +/* Use x86_cache_size as it might have K7 errata fixes */ + assoc = assocs[l2.assoc]; + line_size = l2.line_size; + lines_per_tag = l2.lines_per_tag; + size_in_kb =__this_cpu_read (cpu_info.x86_cache_size);break ;case 3 :if (!l3.val)return ; - assoc = assocs[l3.assoc]; - line_size = l3.line_size; - lines_per_tag = l3.lines_per_tag; - size_in_kb = l3.size_encoded *512 ; + + assoc = assocs[l3.assoc]; + line_size = l3.line_size; + lines_per_tag = l3.lines_per_tag; + size_in_kb = l3.size_encoded *512 ;if (boot_cpu_has (X86_FEATURE_AMD_DCM )) { - size_in_kb = size_in_kb >>1 ; - assoc = assoc >>1 ; + size_in_kb = size_in_kb >>1 ; + assoc = assoc >>1 ; }break ;default :return ; } - eax->split.is_self_initializing =1 ; - eax->split.type = types[index]; - eax->split.level = levels[index]; - eax->split.num_threads_sharing =0 ; - eax->split.num_cores_on_die =topology_num_cores_per_package (); + eax->split.is_self_initializing =1 ; + eax->split.type = types[index]; + eax->split.level = levels[index]; + eax->split.num_threads_sharing =0 ; + eax->split.num_cores_on_die =topology_num_cores_per_package ();if (assoc ==0xffff ) eax->split.is_fully_associative =1 ; - ebx->split.coherency_line_size = line_size -1 ; - ebx->split.ways_of_associativity = assoc -1 ; - ebx->split.physical_line_partition = lines_per_tag -1 ; - ecx->split.number_of_sets = (size_in_kb *1024 ) / line_size / + + ebx->split.coherency_line_size = line_size -1 ; + ebx->split.ways_of_associativity = assoc -1 ; + ebx->split.physical_line_partition = lines_per_tag -1 ; + ecx->split.number_of_sets = (size_in_kb *1024 ) / line_size / (ebx->split.ways_of_associativity +1 ) -1 ; }
@@ -260,18 +264,14 @@ static int fill_cpuid4_info(int index, struct _cpuid4_info *id4)static int find_num_cache_leaves (struct cpuinfo_x86 *c) { -unsigned int eax, ebx, ecx, edx, op; -union _cpuid4_leaf_eax cache_eax; -int i =-1 ; - -if (x86_vendor_amd_or_hygon (c->x86_vendor)) - op =0x8000001d ; -else - op =4 ; +unsigned int eax, ebx, ecx, edx, op; +union _cpuid4_leaf_eax cache_eax; +int i =-1 ; +/* Do a CPUID(op) loop to calculate num_cache_leaves */ + op =x86_vendor_amd_or_hygon (c->x86_vendor) ?0x8000001d :4 ;do { ++i; -/* Do cpuid(op) loop to find out num_cache_leaves */ cpuid_count (op, i, &eax, &ebx, &ecx, &edx); cache_eax.full = eax; }while (cache_eax.split.type !=CTYPE_NULL );
@@ -309,9 +309,9 @@ void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, u16 die_id) num_sharing_cache = ((eax >>14 ) &0xfff ) +1 ;if (num_sharing_cache) { -int bits =get_count_order (num_sharing_cache); +int index_msb =get_count_order (num_sharing_cache); - c->topo.llc_id = c->topo.apicid >> bits; + c->topo.llc_id = c->topo.apicid >> index_msb; } } }
@@ -332,14 +332,10 @@ void init_amd_cacheinfo(struct cpuinfo_x86 *c) {struct cpu_cacheinfo *ci =get_cpu_cacheinfo (c->cpu_index); -if (boot_cpu_has (X86_FEATURE_TOPOEXT )) { +if (boot_cpu_has (X86_FEATURE_TOPOEXT )) ci->num_leaves =find_num_cache_leaves (c); - }else if (c->extended_cpuid_level >=0x80000006 ) { -if (cpuid_edx (0x80000006 ) &0xf000 ) - ci->num_leaves =4 ; -else - ci->num_leaves =3 ; - } +else if (c->extended_cpuid_level >=0x80000006 ) + ci->num_leaves = (cpuid_edx (0x80000006 ) &0xf000 ) ?4 :3 ; }void init_hygon_cacheinfo (struct cpuinfo_x86 *c)
@@ -466,6 +462,9 @@ void init_intel_cacheinfo(struct cpuinfo_x86 *c)intel_cacheinfo_0x2 (c); } +/* +* linux/cacheinfo.h shared_cpu_map setup, AMD/Hygon +*/ static int __cache_amd_cpumap_setup (unsigned int cpu,int index,const struct _cpuid4_info *id4) {
@@ -482,12 +481,12 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index, this_cpu_ci =get_cpu_cacheinfo (i);if (!this_cpu_ci->info_list)continue ; + ci = this_cpu_ci->info_list + index;for_each_cpu (sibling,cpu_llc_shared_mask (cpu)) {if (!cpu_online (sibling))continue ; -cpumask_set_cpu (sibling, - &ci->shared_cpu_map); +cpumask_set_cpu (sibling, &ci->shared_cpu_map); } } }else if (boot_cpu_has (X86_FEATURE_TOPOEXT )) {
@@ -513,8 +512,7 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index, apicid =cpu_data (sibling).topo.apicid;if ((apicid < first) || (apicid > last))continue ; -cpumask_set_cpu (sibling, - &ci->shared_cpu_map); +cpumask_set_cpu (sibling, &ci->shared_cpu_map); } } }else
@@ -523,14 +521,17 @@ static int __cache_amd_cpumap_setup(unsigned int cpu, int index,return 1 ; } +/* +* linux/cacheinfo.h shared_cpu_map setup, Intel + fallback AMD/Hygon +*/ static void __cache_cpumap_setup (unsigned int cpu,int index,const struct _cpuid4_info *id4) {struct cpu_cacheinfo *this_cpu_ci =get_cpu_cacheinfo (cpu); +struct cpuinfo_x86 *c = &cpu_data (cpu);struct cacheinfo *ci, *sibling_ci;unsigned long num_threads_sharing;int index_msb, i; -struct cpuinfo_x86 *c = &cpu_data (cpu);if (x86_vendor_amd_or_hygon (c->x86_vendor)) {if (__cache_amd_cpumap_setup (cpu, index, id4))
@@ -550,8 +551,10 @@ static void __cache_cpumap_setup(unsigned int cpu, int index,if (cpu_data (i).topo.apicid >> index_msb == c->topo.apicid >> index_msb) {struct cpu_cacheinfo *sib_cpu_ci =get_cpu_cacheinfo (i); +/* Skip if itself or no cacheinfo */ if (i == cpu || !sib_cpu_ci->info_list) -continue ;/* skip if itself or no cacheinfo */ +continue ; + sibling_ci = sib_cpu_ci->info_list + index;cpumask_set_cpu (i, &ci->shared_cpu_map);cpumask_set_cpu (cpu, &sibling_ci->shared_cpu_map);
@@ -585,7 +588,7 @@ int init_cache_level(unsigned int cpu) }/* -* The max shared threads number comes from CPUID.4:EAX[25-14] with input +* The max shared threads number comes from CPUID(4) EAX[25-14] with input * ECX as cache index. Then right shift apicid by the number's order to get * cache id for this cache node. */
@@ -621,8 +624,8 @@ int populate_cache_leaves(unsigned int cpu)ci_info_init (ci++, &id4, nb);__cache_cpumap_setup (cpu, idx, &id4); } - this_cpu_ci->cpu_map_populated = true; + this_cpu_ci->cpu_map_populated = true;return 0 ; }
@@ -654,12 +657,10 @@ void cache_disable(void) __acquires(cache_disable_lock)unsigned long cr0;/* -* Note that this is not ideal -* since the cache is only flushed/disabled for this CPU while the -* MTRRs are changed, but changing this requires more invasive -* changes to the way the kernel boots +* This is not ideal since the cache is only flushed/disabled +* for this CPU while the MTRRs are changed, but changing this +* requires more invasive changes to the way the kernel boots. */ -raw_spin_lock (&cache_disable_lock);/* Enter the no-fill (CD=1, NW=0) cache mode and flush caches. */
Signature · 1 line
2.48.1