Skip to content

Commit 68091ee

Browse files
ssuthiku-amdKAGA-KOKO
authored andcommitted
x86/CPU/AMD: Calculate last level cache ID from number of sharing threads
Last Level Cache ID can be calculated from the number of threads sharing the cache, which is available from CPUID Fn0x8000001D (Cache Properties). This is used to left-shift the APIC ID to derive LLC ID. Therefore, default to this method unless the APIC ID enumeration does not follow the scheme. Signed-off-by: Suravee Suthikulpanit <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: http://lkml.kernel.org/r/[email protected]
1 parent 1d200c0 commit 68091ee

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

arch/x86/include/asm/cacheinfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_X86_CACHEINFO_H
3+
#define _ASM_X86_CACHEINFO_H
4+
5+
void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, int cpu, u8 node_id);
6+
7+
#endif /* _ASM_X86_CACHEINFO_H */

arch/x86/kernel/cpu/amd.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/random.h>
1010
#include <asm/processor.h>
1111
#include <asm/apic.h>
12+
#include <asm/cacheinfo.h>
1213
#include <asm/cpu.h>
1314
#include <asm/smp.h>
1415
#include <asm/pci-direct.h>
@@ -343,22 +344,8 @@ static void amd_get_topology(struct cpuinfo_x86 *c)
343344
c->x86_max_cores /= smp_num_siblings;
344345
}
345346

346-
/*
347-
* We may have multiple LLCs if L3 caches exist, so check if we
348-
* have an L3 cache by looking at the L3 cache CPUID leaf.
349-
*/
350-
if (cpuid_edx(0x80000006)) {
351-
if (c->x86 == 0x17) {
352-
/*
353-
* LLC is at the core complex level.
354-
* Core complex id is ApicId[3].
355-
*/
356-
per_cpu(cpu_llc_id, cpu) = c->apicid >> 3;
357-
} else {
358-
/* LLC is at the node level. */
359-
per_cpu(cpu_llc_id, cpu) = node_id;
360-
}
361-
}
347+
cacheinfo_amd_init_llc_id(c, cpu, node_id);
348+
362349
} else if (cpu_has(c, X86_FEATURE_NODEID_MSR)) {
363350
u64 value;
364351

arch/x86/kernel/cpu/cacheinfo.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,45 @@ static int find_num_cache_leaves(struct cpuinfo_x86 *c)
637637
return i;
638638
}
639639

640+
void cacheinfo_amd_init_llc_id(struct cpuinfo_x86 *c, int cpu, u8 node_id)
641+
{
642+
/*
643+
* We may have multiple LLCs if L3 caches exist, so check if we
644+
* have an L3 cache by looking at the L3 cache CPUID leaf.
645+
*/
646+
if (!cpuid_edx(0x80000006))
647+
return;
648+
649+
if (c->x86 < 0x17) {
650+
/* LLC is at the node level. */
651+
per_cpu(cpu_llc_id, cpu) = node_id;
652+
} else if (c->x86 == 0x17 &&
653+
c->x86_model >= 0 && c->x86_model <= 0x1F) {
654+
/*
655+
* LLC is at the core complex level.
656+
* Core complex ID is ApicId[3] for these processors.
657+
*/
658+
per_cpu(cpu_llc_id, cpu) = c->apicid >> 3;
659+
} else {
660+
/*
661+
* LLC ID is calculated from the number of threads sharing the
662+
* cache.
663+
* */
664+
u32 eax, ebx, ecx, edx, num_sharing_cache = 0;
665+
u32 llc_index = find_num_cache_leaves(c) - 1;
666+
667+
cpuid_count(0x8000001d, llc_index, &eax, &ebx, &ecx, &edx);
668+
if (eax)
669+
num_sharing_cache = ((eax >> 14) & 0xfff) + 1;
670+
671+
if (num_sharing_cache) {
672+
int bits = get_count_order(num_sharing_cache) - 1;
673+
674+
per_cpu(cpu_llc_id, cpu) = c->apicid >> bits;
675+
}
676+
}
677+
}
678+
640679
void init_amd_cacheinfo(struct cpuinfo_x86 *c)
641680
{
642681

0 commit comments

Comments
 (0)