Skip to content

Commit af507c6

Browse files
kudureranganathingomolnar
authored andcommitted
x86/cpu/cacheinfo: Simplify cacheinfo_amd_init_llc_id() using _cpuid4_info
struct _cpuid4_info has the same layout as the CPUID leaf 0x8000001d. Use the encoded definition and amd_fill_cpuid4_info(), get_cache_id() helpers instead of open coding masks and shifts to calculate the llc_id. cacheinfo_amd_init_llc_id() is only called on AMD systems that support X86_FEATURE_TOPOEXT and amd_fill_cpuid4_info() uses the information from CPUID leaf 0x8000001d on all these systems which is consistent with the current open coded implementation. While at it, avoid reading cpu_data() every time get_cache_id() is called and instead pass the APIC ID necessary to return the _cpuid4_info.id from get_cache_id(). No functional changes intended. [ bp: do what Ahmed suggests: merge into one patch, make id4 ptr const. ] Signed-off-by: K Prateek Nayak <kprateek.nayak@amd.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Signed-off-by: Ingo Molnar <mingo@kernel.org> Acked-by: Ahmed S. Darwish <darwi@linutronix.de> Link: https://lore.kernel.org/20250821051910.7351-2-kprateek.nayak@amd.com
1 parent 70d1d98 commit af507c6

1 file changed

Lines changed: 21 additions & 27 deletions

File tree

arch/x86/kernel/cpu/cacheinfo.c

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,22 @@ static int find_num_cache_leaves(struct cpuinfo_x86 *c)
289289
return i;
290290
}
291291

292+
/*
293+
* The max shared threads number comes from CPUID(0x4) EAX[25-14] with input
294+
* ECX as cache index. Then right shift apicid by the number's order to get
295+
* cache id for this cache node.
296+
*/
297+
static unsigned int get_cache_id(u32 apicid, const struct _cpuid4_info *id4)
298+
{
299+
unsigned long num_threads_sharing;
300+
int index_msb;
301+
302+
num_threads_sharing = 1 + id4->eax.split.num_threads_sharing;
303+
index_msb = get_count_order(num_threads_sharing);
304+
305+
return apicid >> index_msb;
306+
}
307+
292308
/*
293309
* AMD/Hygon CPUs may have multiple LLCs if L3 caches exist.
294310
*/
@@ -312,18 +328,11 @@ void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, u16 die_id)
312328
* Newer families: LLC ID is calculated from the number
313329
* of threads sharing the L3 cache.
314330
*/
315-
u32 eax, ebx, ecx, edx, num_sharing_cache = 0;
316331
u32 llc_index = find_num_cache_leaves(c) - 1;
332+
struct _cpuid4_info id4 = {};
317333

318-
cpuid_count(0x8000001d, llc_index, &eax, &ebx, &ecx, &edx);
319-
if (eax)
320-
num_sharing_cache = ((eax >> 14) & 0xfff) + 1;
321-
322-
if (num_sharing_cache) {
323-
int index_msb = get_count_order(num_sharing_cache);
324-
325-
c->topo.llc_id = c->topo.apicid >> index_msb;
326-
}
334+
if (!amd_fill_cpuid4_info(llc_index, &id4))
335+
c->topo.llc_id = get_cache_id(c->topo.apicid, &id4);
327336
}
328337
}
329338

@@ -598,27 +607,12 @@ int init_cache_level(unsigned int cpu)
598607
return 0;
599608
}
600609

601-
/*
602-
* The max shared threads number comes from CPUID(0x4) EAX[25-14] with input
603-
* ECX as cache index. Then right shift apicid by the number's order to get
604-
* cache id for this cache node.
605-
*/
606-
static void get_cache_id(int cpu, struct _cpuid4_info *id4)
607-
{
608-
struct cpuinfo_x86 *c = &cpu_data(cpu);
609-
unsigned long num_threads_sharing;
610-
int index_msb;
611-
612-
num_threads_sharing = 1 + id4->eax.split.num_threads_sharing;
613-
index_msb = get_count_order(num_threads_sharing);
614-
id4->id = c->topo.apicid >> index_msb;
615-
}
616-
617610
int populate_cache_leaves(unsigned int cpu)
618611
{
619612
struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
620613
struct cacheinfo *ci = this_cpu_ci->info_list;
621614
u8 cpu_vendor = boot_cpu_data.x86_vendor;
615+
u32 apicid = cpu_data(cpu).topo.apicid;
622616
struct amd_northbridge *nb = NULL;
623617
struct _cpuid4_info id4 = {};
624618
int idx, ret;
@@ -628,7 +622,7 @@ int populate_cache_leaves(unsigned int cpu)
628622
if (ret)
629623
return ret;
630624

631-
get_cache_id(cpu, &id4);
625+
id4.id = get_cache_id(apicid, &id4);
632626

633627
if (cpu_vendor == X86_VENDOR_AMD || cpu_vendor == X86_VENDOR_HYGON)
634628
nb = amd_init_l3_cache(idx);

0 commit comments

Comments
 (0)