1 // SPDX-License-Identifier: GPL-2.0
7 #include <asm/processor.h>
12 struct x86_topology_system x86_topo_system __ro_after_init;
13 EXPORT_SYMBOL_GPL(x86_topo_system);
15 unsigned int __amd_nodes_per_pkg __ro_after_init;
16 EXPORT_SYMBOL_GPL(__amd_nodes_per_pkg);
18 void topology_set_dom(struct topo_scan *tscan, enum x86_topology_domains dom,
19 unsigned int shift, unsigned int ncpus)
21 topology_update_dom(tscan, dom, shift, ncpus);
23 /* Propagate to the upper levels */
24 for (dom++; dom < TOPO_MAX_DOMAIN; dom++) {
25 tscan->dom_shifts[dom] = tscan->dom_shifts[dom - 1];
26 tscan->dom_ncpus[dom] = tscan->dom_ncpus[dom - 1];
30 static unsigned int __maybe_unused parse_num_cores_legacy(struct cpuinfo_x86 *c)
38 if (c->cpuid_level < 4)
41 cpuid_subleaf_reg(4, 0, CPUID_EAX, &eax);
45 return eax.ncores + 1;
48 static void parse_legacy(struct topo_scan *tscan)
50 unsigned int cores, core_shift, smt_shift = 0;
51 struct cpuinfo_x86 *c = tscan->c;
53 cores = parse_num_cores_legacy(c);
54 core_shift = get_count_order(cores);
56 if (cpu_has(c, X86_FEATURE_HT)) {
57 if (!WARN_ON_ONCE(tscan->ebx1_nproc_shift < core_shift))
58 smt_shift = tscan->ebx1_nproc_shift - core_shift;
60 * The parser expects leaf 0xb/0x1f format, which means
61 * the number of logical processors at core level is
64 core_shift += smt_shift;
68 topology_set_dom(tscan, TOPO_SMT_DOMAIN, smt_shift, 1U << smt_shift);
69 topology_set_dom(tscan, TOPO_CORE_DOMAIN, core_shift, cores);
72 static bool fake_topology(struct topo_scan *tscan)
75 * Preset the CORE level shift for CPUID less systems and XEN_PV,
76 * which has useless CPUID information.
78 topology_set_dom(tscan, TOPO_SMT_DOMAIN, 0, 1);
79 topology_set_dom(tscan, TOPO_CORE_DOMAIN, 0, 1);
81 return tscan->c->cpuid_level < 1;
84 static void parse_topology(struct topo_scan *tscan, bool early)
86 const struct cpuinfo_topology topo_defaults = {
91 struct cpuinfo_x86 *c = tscan->c;
98 c->topo = topo_defaults;
100 if (fake_topology(tscan))
103 /* Preset Initial APIC ID from CPUID leaf 1 */
104 cpuid_leaf_reg(1, CPUID_EBX, &ebx);
105 c->topo.initial_apicid = ebx.apicid;
108 * The initial invocation from early_identify_cpu() happens before
109 * the APIC is mapped or X2APIC enabled. For establishing the
110 * topology, that's not required. Use the initial APIC ID.
113 c->topo.apicid = c->topo.initial_apicid;
115 c->topo.apicid = read_apic_id();
117 /* The above is sufficient for UP */
118 if (!IS_ENABLED(CONFIG_SMP))
121 tscan->ebx1_nproc_shift = get_count_order(ebx.nproc);
123 switch (c->x86_vendor) {
125 if (IS_ENABLED(CONFIG_CPU_SUP_AMD))
126 cpu_parse_topology_amd(tscan);
128 case X86_VENDOR_CENTAUR:
129 case X86_VENDOR_ZHAOXIN:
132 case X86_VENDOR_INTEL:
133 if (!IS_ENABLED(CONFIG_CPU_SUP_INTEL) || !cpu_parse_topology_ext(tscan))
136 case X86_VENDOR_HYGON:
137 if (IS_ENABLED(CONFIG_CPU_SUP_HYGON))
138 cpu_parse_topology_amd(tscan);
143 static void topo_set_ids(struct topo_scan *tscan, bool early)
145 struct cpuinfo_x86 *c = tscan->c;
146 u32 apicid = c->topo.apicid;
148 c->topo.pkg_id = topo_shift_apicid(apicid, TOPO_PKG_DOMAIN);
149 c->topo.die_id = topo_shift_apicid(apicid, TOPO_DIE_DOMAIN);
152 c->topo.logical_pkg_id = topology_get_logical_id(apicid, TOPO_PKG_DOMAIN);
153 c->topo.logical_die_id = topology_get_logical_id(apicid, TOPO_DIE_DOMAIN);
156 /* Package relative core ID */
157 c->topo.core_id = (apicid & topo_domain_mask(TOPO_PKG_DOMAIN)) >>
158 x86_topo_system.dom_shifts[TOPO_SMT_DOMAIN];
160 c->topo.amd_node_id = tscan->amd_node_id;
162 if (c->x86_vendor == X86_VENDOR_AMD)
163 cpu_topology_fixup_amd(tscan);
166 void cpu_parse_topology(struct cpuinfo_x86 *c)
168 unsigned int dom, cpu = smp_processor_id();
169 struct topo_scan tscan = { .c = c, };
171 parse_topology(&tscan, false);
173 if (IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
174 if (c->topo.initial_apicid != c->topo.apicid) {
175 pr_err(FW_BUG "CPU%4u: APIC ID mismatch. CPUID: 0x%04x APIC: 0x%04x\n",
176 cpu, c->topo.initial_apicid, c->topo.apicid);
179 if (c->topo.apicid != cpuid_to_apicid[cpu]) {
180 pr_err(FW_BUG "CPU%4u: APIC ID mismatch. Firmware: 0x%04x APIC: 0x%04x\n",
181 cpu, cpuid_to_apicid[cpu], c->topo.apicid);
185 for (dom = TOPO_SMT_DOMAIN; dom < TOPO_MAX_DOMAIN; dom++) {
186 if (tscan.dom_shifts[dom] == x86_topo_system.dom_shifts[dom])
188 pr_err(FW_BUG "CPU%d: Topology domain %u shift %u != %u\n", cpu, dom,
189 tscan.dom_shifts[dom], x86_topo_system.dom_shifts[dom]);
192 topo_set_ids(&tscan, false);
195 void __init cpu_init_topology(struct cpuinfo_x86 *c)
197 struct topo_scan tscan = { .c = c, };
198 unsigned int dom, sft;
200 parse_topology(&tscan, true);
202 /* Copy the shift values and calculate the unit sizes. */
203 memcpy(x86_topo_system.dom_shifts, tscan.dom_shifts, sizeof(x86_topo_system.dom_shifts));
205 dom = TOPO_SMT_DOMAIN;
206 x86_topo_system.dom_size[dom] = 1U << x86_topo_system.dom_shifts[dom];
208 for (dom++; dom < TOPO_MAX_DOMAIN; dom++) {
209 sft = x86_topo_system.dom_shifts[dom] - x86_topo_system.dom_shifts[dom - 1];
210 x86_topo_system.dom_size[dom] = 1U << sft;
213 topo_set_ids(&tscan, true);
216 * AMD systems have Nodes per package which cannot be mapped to
219 __amd_nodes_per_pkg = tscan.amd_nodes_per_pkg;