1 // SPDX-License-Identifier: GPL-2.0
3 * Check for extended topology enumeration cpuid leaf 0xb and if it
4 * exists, use it for populating initial_apicid and cpu topology
10 #include <asm/memtype.h>
11 #include <asm/processor.h>
15 /* leaf 0xb SMT level */
18 /* extended topology sub-leaf types */
19 #define INVALID_TYPE 0
24 #define LEAFB_SUBTYPE(ecx) (((ecx) >> 8) & 0xff)
25 #define BITS_SHIFT_NEXT_LEVEL(eax) ((eax) & 0x1f)
26 #define LEVEL_MAX_SIBLINGS(ebx) ((ebx) & 0xffff)
30 * Check if given CPUID extended topology "leaf" is implemented
32 static int check_extended_topology_leaf(int leaf)
34 unsigned int eax, ebx, ecx, edx;
36 cpuid_count(leaf, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
38 if (ebx == 0 || (LEAFB_SUBTYPE(ecx) != SMT_TYPE))
44 * Return best CPUID Extended Topology Leaf supported
46 static int detect_extended_topology_leaf(struct cpuinfo_x86 *c)
48 if (c->cpuid_level >= 0x1f) {
49 if (check_extended_topology_leaf(0x1f) == 0)
53 if (c->cpuid_level >= 0xb) {
54 if (check_extended_topology_leaf(0xb) == 0)
62 int detect_extended_topology_early(struct cpuinfo_x86 *c)
65 unsigned int eax, ebx, ecx, edx;
68 leaf = detect_extended_topology_leaf(c);
72 set_cpu_cap(c, X86_FEATURE_XTOPOLOGY);
74 cpuid_count(leaf, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
76 * initial apic id, which also represents 32-bit extended x2apic id.
78 c->topo.initial_apicid = edx;
79 smp_num_siblings = max_t(int, smp_num_siblings, LEVEL_MAX_SIBLINGS(ebx));
85 * Check for extended topology enumeration cpuid leaf, and if it
86 * exists, use it for populating initial_apicid and cpu topology
89 int detect_extended_topology(struct cpuinfo_x86 *c)
92 unsigned int eax, ebx, ecx, edx, sub_index;
93 unsigned int ht_mask_width, core_plus_mask_width, die_plus_mask_width;
94 unsigned int core_select_mask, core_level_siblings;
95 unsigned int die_select_mask, die_level_siblings;
96 unsigned int pkg_mask_width;
97 bool die_level_present = false;
100 leaf = detect_extended_topology_leaf(c);
105 * Populate HT related information from sub-leaf level 0.
107 cpuid_count(leaf, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
108 c->topo.initial_apicid = edx;
109 core_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
110 smp_num_siblings = max_t(int, smp_num_siblings, LEVEL_MAX_SIBLINGS(ebx));
111 core_plus_mask_width = ht_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
112 die_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
113 pkg_mask_width = die_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
117 cpuid_count(leaf, sub_index, &eax, &ebx, &ecx, &edx);
120 * Check for the Core type in the implemented sub leaves.
122 if (LEAFB_SUBTYPE(ecx) == CORE_TYPE) {
123 core_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
124 core_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
125 die_level_siblings = core_level_siblings;
126 die_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
128 if (LEAFB_SUBTYPE(ecx) == DIE_TYPE) {
129 die_level_present = true;
130 die_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
131 die_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
134 if (LEAFB_SUBTYPE(ecx) != INVALID_TYPE)
135 pkg_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
142 core_select_mask = (~(-1 << pkg_mask_width)) >> ht_mask_width;
143 die_select_mask = (~(-1 << die_plus_mask_width)) >>
144 core_plus_mask_width;
146 c->topo.core_id = apic->phys_pkg_id(c->topo.initial_apicid,
147 ht_mask_width) & core_select_mask;
149 if (die_level_present) {
150 c->topo.die_id = apic->phys_pkg_id(c->topo.initial_apicid,
151 core_plus_mask_width) & die_select_mask;
154 c->topo.pkg_id = apic->phys_pkg_id(c->topo.initial_apicid, pkg_mask_width);
156 * Reinit the apicid, now that we have extended initial_apicid.
158 c->topo.apicid = apic->phys_pkg_id(c->topo.initial_apicid, 0);
160 c->x86_max_cores = (core_level_siblings / smp_num_siblings);
161 __max_die_per_package = (die_level_siblings / core_level_siblings);