1 // SPDX-License-Identifier: GPL-2.0-only
5 * The APIC IDs describe the system topology in multiple domain levels.
6 * The CPUID topology parser provides the information which part of the
7 * APIC ID is associated to the individual levels:
9 * [PACKAGE][DIEGRP][DIE][TILE][MODULE][CORE][THREAD]
11 * The root space contains the package (socket) IDs.
13 * Not enumerated levels consume 0 bits space, but conceptually they are
14 * always represented. If e.g. only CORE and THREAD levels are enumerated
15 * then the DIE, MODULE and TILE have the same physical ID as the PACKAGE.
17 * If SMT is not supported, then the THREAD domain is still used. It then
18 * has the same physical ID as the CORE domain and is the only child of
21 * This allows a unified view on the system independent of the enumerated
22 * domain levels without requiring any conditionals in the code.
24 #define pr_fmt(fmt) "CPU topo: " fmt
25 #include <linux/cpu.h>
30 #include <asm/io_apic.h>
31 #include <asm/mpspec.h>
37 * Map cpu index to physical APIC ID
39 DEFINE_EARLY_PER_CPU_READ_MOSTLY(u32, x86_cpu_to_apicid, BAD_APICID);
40 DEFINE_EARLY_PER_CPU_READ_MOSTLY(u32, x86_cpu_to_acpiid, CPU_ACPIID_INVALID);
41 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
42 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_acpiid);
44 /* Bitmap of physically present CPUs. */
45 DECLARE_BITMAP(phys_cpu_present_map, MAX_LOCAL_APIC) __read_mostly;
47 /* Used for CPU number allocation and parallel CPU bringup */
48 u32 cpuid_to_apicid[] __ro_after_init = { [0 ... NR_CPUS - 1] = BAD_APICID, };
50 /* Bitmaps to mark registered APICs at each topology domain */
51 static struct { DECLARE_BITMAP(map, MAX_LOCAL_APIC); } apic_maps[TOPO_MAX_DOMAIN] __ro_after_init;
54 * Keep track of assigned, disabled and rejected CPUs. Present assigned
55 * with 1 as CPU #0 is reserved for the boot CPU.
58 unsigned int nr_assigned_cpus;
59 unsigned int nr_disabled_cpus;
60 unsigned int nr_rejected_cpus;
63 } topo_info __ro_after_init = {
64 .nr_assigned_cpus = 1,
65 .boot_cpu_apic_id = BAD_APICID,
66 .real_bsp_apic_id = BAD_APICID,
69 #define domain_weight(_dom) bitmap_weight(apic_maps[_dom].map, MAX_LOCAL_APIC)
71 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
73 return phys_id == (u64)cpuid_to_apicid[cpu];
77 static void cpu_mark_primary_thread(unsigned int cpu, unsigned int apicid)
79 /* Isolate the SMT bit(s) in the APICID and check for 0 */
80 u32 mask = (1U << (fls(smp_num_siblings) - 1)) - 1;
82 if (smp_num_siblings == 1 || !(apicid & mask))
83 cpumask_set_cpu(cpu, &__cpu_primary_thread_mask);
87 * Due to the utter mess of CPUID evaluation smp_num_siblings is not valid
88 * during early boot. Initialize the primary thread mask before SMP
91 static int __init smp_init_primary_thread_mask(void)
96 * XEN/PV provides either none or useless topology information.
97 * Pretend that all vCPUs are primary threads.
99 if (xen_pv_domain()) {
100 cpumask_copy(&__cpu_primary_thread_mask, cpu_possible_mask);
104 for (cpu = 0; cpu < topo_info.nr_assigned_cpus; cpu++)
105 cpu_mark_primary_thread(cpu, cpuid_to_apicid[cpu]);
108 early_initcall(smp_init_primary_thread_mask);
110 static inline void cpu_mark_primary_thread(unsigned int cpu, unsigned int apicid) { }
114 * Convert the APIC ID to a domain level ID by masking out the low bits
115 * below the domain level @dom.
117 static inline u32 topo_apicid(u32 apicid, enum x86_topology_domains dom)
119 if (dom == TOPO_SMT_DOMAIN)
121 return apicid & (UINT_MAX << x86_topo_system.dom_shifts[dom - 1]);
124 static int topo_lookup_cpuid(u32 apic_id)
128 /* CPU# to APICID mapping is persistent once it is established */
129 for (i = 0; i < topo_info.nr_assigned_cpus; i++) {
130 if (cpuid_to_apicid[i] == apic_id)
136 static __init int topo_get_cpunr(u32 apic_id)
138 int cpu = topo_lookup_cpuid(apic_id);
143 return topo_info.nr_assigned_cpus++;
146 static void topo_set_cpuids(unsigned int cpu, u32 apic_id, u32 acpi_id)
148 #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
149 early_per_cpu(x86_cpu_to_apicid, cpu) = apic_id;
150 early_per_cpu(x86_cpu_to_acpiid, cpu) = acpi_id;
152 set_cpu_possible(cpu, true);
153 set_cpu_present(cpu, true);
155 if (system_state != SYSTEM_BOOTING)
156 cpu_mark_primary_thread(cpu, apic_id);
159 static __init bool check_for_real_bsp(u32 apic_id)
162 * There is no real good way to detect whether this a kdump()
163 * kernel, but except on the Voyager SMP monstrosity which is not
164 * longer supported, the real BSP APIC ID is the first one which is
165 * enumerated by firmware. That allows to detect whether the boot
166 * CPU is the real BSP. If it is not, then do not register the APIC
167 * because sending INIT to the real BSP would reset the whole
170 * The first APIC ID which is enumerated by firmware is detectable
171 * because the boot CPU APIC ID is registered before that without
172 * invoking this code.
174 if (topo_info.real_bsp_apic_id != BAD_APICID)
177 if (apic_id == topo_info.boot_cpu_apic_id) {
178 topo_info.real_bsp_apic_id = apic_id;
182 pr_warn("Boot CPU APIC ID not the first enumerated APIC ID: %x > %x\n",
183 topo_info.boot_cpu_apic_id, apic_id);
184 pr_warn("Crash kernel detected. Disabling real BSP to prevent machine INIT\n");
186 topo_info.real_bsp_apic_id = apic_id;
190 static __init void topo_register_apic(u32 apic_id, u32 acpi_id, bool present)
195 set_bit(apic_id, phys_cpu_present_map);
198 * Double registration is valid in case of the boot CPU
199 * APIC because that is registered before the enumeration
200 * of the APICs via firmware parsers or VM guest
203 if (apic_id == topo_info.boot_cpu_apic_id)
206 cpu = topo_get_cpunr(apic_id);
208 cpuid_to_apicid[cpu] = apic_id;
209 topo_set_cpuids(cpu, apic_id, acpi_id);
211 topo_info.nr_disabled_cpus++;
214 /* Register present and possible CPUs in the domain maps */
215 for (dom = TOPO_SMT_DOMAIN; dom < TOPO_MAX_DOMAIN; dom++)
216 set_bit(topo_apicid(apic_id, dom), apic_maps[dom].map);
220 * topology_register_apic - Register an APIC in early topology maps
221 * @apic_id: The APIC ID to set up
222 * @acpi_id: The ACPI ID associated to the APIC
223 * @present: True if the corresponding CPU is present
225 void __init topology_register_apic(u32 apic_id, u32 acpi_id, bool present)
227 if (apic_id >= MAX_LOCAL_APIC) {
228 pr_err_once("APIC ID %x exceeds kernel limit of: %x\n", apic_id, MAX_LOCAL_APIC - 1);
229 topo_info.nr_rejected_cpus++;
233 if (check_for_real_bsp(apic_id)) {
234 topo_info.nr_rejected_cpus++;
238 /* CPU numbers exhausted? */
239 if (apic_id != topo_info.boot_cpu_apic_id && topo_info.nr_assigned_cpus >= nr_cpu_ids) {
240 pr_warn_once("CPU limit of %d reached. Ignoring further CPUs\n", nr_cpu_ids);
241 topo_info.nr_rejected_cpus++;
245 topo_register_apic(apic_id, acpi_id, present);
249 * topology_register_boot_apic - Register the boot CPU APIC
250 * @apic_id: The APIC ID to set up
252 * Separate so CPU #0 can be assigned
254 void __init topology_register_boot_apic(u32 apic_id)
256 WARN_ON_ONCE(topo_info.boot_cpu_apic_id != BAD_APICID);
258 topo_info.boot_cpu_apic_id = apic_id;
259 topo_register_apic(apic_id, CPU_ACPIID_INVALID, true);
262 #ifdef CONFIG_ACPI_HOTPLUG_CPU
264 * topology_hotplug_apic - Handle a physical hotplugged APIC after boot
265 * @apic_id: The APIC ID to set up
266 * @acpi_id: The ACPI ID associated to the APIC
268 int topology_hotplug_apic(u32 apic_id, u32 acpi_id)
272 if (apic_id >= MAX_LOCAL_APIC)
275 /* Reject if the APIC ID was not registered during enumeration. */
276 if (!test_bit(apic_id, apic_maps[TOPO_SMT_DOMAIN].map))
279 cpu = topo_lookup_cpuid(apic_id);
283 set_bit(apic_id, phys_cpu_present_map);
284 topo_set_cpuids(cpu, apic_id, acpi_id);
289 * topology_hotunplug_apic - Remove a physical hotplugged APIC after boot
290 * @cpu: The CPU number for which the APIC ID is removed
292 void topology_hotunplug_apic(unsigned int cpu)
294 u32 apic_id = cpuid_to_apicid[cpu];
296 if (apic_id == BAD_APICID)
299 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
300 clear_bit(apic_id, phys_cpu_present_map);
301 set_cpu_present(cpu, false);
306 static unsigned int max_possible_cpus __initdata = NR_CPUS;
309 * topology_apply_cmdline_limits_early - Apply topology command line limits early
311 * Ensure that command line limits are in effect before firmware parsing
314 void __init topology_apply_cmdline_limits_early(void)
316 unsigned int possible = nr_cpu_ids;
318 /* 'maxcpus=0' 'nosmp' 'nolapic' 'disableapic' 'noapic' */
319 if (!setup_max_cpus || ioapic_is_disabled || apic_is_disabled)
322 /* 'possible_cpus=N' */
323 possible = min_t(unsigned int, max_possible_cpus, possible);
325 if (possible < nr_cpu_ids) {
326 pr_info("Limiting to %u possible CPUs\n", possible);
327 set_nr_cpu_ids(possible);
331 static __init bool restrict_to_up(void)
333 if (!smp_found_config || ioapic_is_disabled)
336 * XEN PV is special as it does not advertise the local APIC
337 * properly, but provides a fake topology for it so that the
338 * infrastructure works. So don't apply the restrictions vs. APIC
344 return apic_is_disabled;
347 void __init topology_init_possible_cpus(void)
349 unsigned int assigned = topo_info.nr_assigned_cpus;
350 unsigned int disabled = topo_info.nr_disabled_cpus;
351 unsigned int total = assigned + disabled;
352 unsigned int cpu, allowed = 1;
355 if (!restrict_to_up()) {
356 if (WARN_ON_ONCE(assigned > nr_cpu_ids)) {
357 disabled += assigned - nr_cpu_ids;
358 assigned = nr_cpu_ids;
360 allowed = min_t(unsigned int, total, nr_cpu_ids);
364 pr_warn("%u possible CPUs exceed the limit of %u\n", total, allowed);
366 assigned = min_t(unsigned int, allowed, assigned);
367 disabled = allowed - assigned;
369 topo_info.nr_assigned_cpus = assigned;
370 topo_info.nr_disabled_cpus = disabled;
372 total_cpus = allowed;
373 set_nr_cpu_ids(allowed);
375 pr_info("Allowing %u present CPUs plus %u hotplug CPUs\n", assigned, disabled);
376 if (topo_info.nr_rejected_cpus)
377 pr_info("Rejected CPUs %u\n", topo_info.nr_rejected_cpus);
379 init_cpu_present(cpumask_of(0));
380 init_cpu_possible(cpumask_of(0));
382 /* Assign CPU numbers to non-present CPUs */
383 for (apicid = 0; disabled; disabled--, apicid++) {
384 apicid = find_next_andnot_bit(apic_maps[TOPO_SMT_DOMAIN].map, phys_cpu_present_map,
385 MAX_LOCAL_APIC, apicid);
386 if (apicid >= MAX_LOCAL_APIC)
388 cpuid_to_apicid[topo_info.nr_assigned_cpus++] = apicid;
391 for (cpu = 0; cpu < allowed; cpu++) {
392 apicid = cpuid_to_apicid[cpu];
394 set_cpu_possible(cpu, true);
396 if (apicid == BAD_APICID)
399 set_cpu_present(cpu, test_bit(apicid, phys_cpu_present_map));
404 * Late SMP disable after sizing CPU masks when APIC/IOAPIC setup failed.
406 void __init topology_reset_possible_cpus_up(void)
408 init_cpu_present(cpumask_of(0));
409 init_cpu_possible(cpumask_of(0));
411 bitmap_zero(phys_cpu_present_map, MAX_LOCAL_APIC);
412 if (topo_info.boot_cpu_apic_id != BAD_APICID)
413 set_bit(topo_info.boot_cpu_apic_id, phys_cpu_present_map);
416 static int __init setup_possible_cpus(char *str)
418 get_option(&str, &max_possible_cpus);
421 early_param("possible_cpus", setup_possible_cpus);