x86/cpu/topology: Assign hotpluggable CPUIDs during init
[sfrench/cifs-2.6.git] / arch / x86 / kernel / cpu / topology.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * CPU/APIC topology
4  *
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:
8  *
9  * [PACKAGE][DIEGRP][DIE][TILE][MODULE][CORE][THREAD]
10  *
11  * The root space contains the package (socket) IDs.
12  *
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.
16  *
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
19  * the core domain.
20  *
21  * This allows a unified view on the system independent of the enumerated
22  * domain levels without requiring any conditionals in the code.
23  */
24 #define pr_fmt(fmt) "CPU topo: " fmt
25 #include <linux/cpu.h>
26
27 #include <xen/xen.h>
28
29 #include <asm/apic.h>
30 #include <asm/io_apic.h>
31 #include <asm/mpspec.h>
32 #include <asm/smp.h>
33
34 #include "cpu.h"
35
36 /*
37  * Map cpu index to physical APIC ID
38  */
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);
43
44 /* Bitmap of physically present CPUs. */
45 DECLARE_BITMAP(phys_cpu_present_map, MAX_LOCAL_APIC) __read_mostly;
46
47 /* Used for CPU number allocation and parallel CPU bringup */
48 u32 cpuid_to_apicid[] __ro_after_init = { [0 ... NR_CPUS - 1] = BAD_APICID, };
49
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;
52
53 /*
54  * Keep track of assigned, disabled and rejected CPUs. Present assigned
55  * with 1 as CPU #0 is reserved for the boot CPU.
56  */
57 static struct {
58         unsigned int            nr_assigned_cpus;
59         unsigned int            nr_disabled_cpus;
60         unsigned int            nr_rejected_cpus;
61         u32                     boot_cpu_apic_id;
62         u32                     real_bsp_apic_id;
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,
67 };
68
69 #define domain_weight(_dom)     bitmap_weight(apic_maps[_dom].map, MAX_LOCAL_APIC)
70
71 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
72 {
73         return phys_id == (u64)cpuid_to_apicid[cpu];
74 }
75
76 #ifdef CONFIG_SMP
77 static void cpu_mark_primary_thread(unsigned int cpu, unsigned int apicid)
78 {
79         /* Isolate the SMT bit(s) in the APICID and check for 0 */
80         u32 mask = (1U << (fls(smp_num_siblings) - 1)) - 1;
81
82         if (smp_num_siblings == 1 || !(apicid & mask))
83                 cpumask_set_cpu(cpu, &__cpu_primary_thread_mask);
84 }
85
86 /*
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
89  * bringup.
90  */
91 static int __init smp_init_primary_thread_mask(void)
92 {
93         unsigned int cpu;
94
95         /*
96          * XEN/PV provides either none or useless topology information.
97          * Pretend that all vCPUs are primary threads.
98          */
99         if (xen_pv_domain()) {
100                 cpumask_copy(&__cpu_primary_thread_mask, cpu_possible_mask);
101                 return 0;
102         }
103
104         for (cpu = 0; cpu < topo_info.nr_assigned_cpus; cpu++)
105                 cpu_mark_primary_thread(cpu, cpuid_to_apicid[cpu]);
106         return 0;
107 }
108 early_initcall(smp_init_primary_thread_mask);
109 #else
110 static inline void cpu_mark_primary_thread(unsigned int cpu, unsigned int apicid) { }
111 #endif
112
113 /*
114  * Convert the APIC ID to a domain level ID by masking out the low bits
115  * below the domain level @dom.
116  */
117 static inline u32 topo_apicid(u32 apicid, enum x86_topology_domains dom)
118 {
119         if (dom == TOPO_SMT_DOMAIN)
120                 return apicid;
121         return apicid & (UINT_MAX << x86_topo_system.dom_shifts[dom - 1]);
122 }
123
124 static int topo_lookup_cpuid(u32 apic_id)
125 {
126         int i;
127
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)
131                         return i;
132         }
133         return -ENODEV;
134 }
135
136 static __init int topo_get_cpunr(u32 apic_id)
137 {
138         int cpu = topo_lookup_cpuid(apic_id);
139
140         if (cpu >= 0)
141                 return cpu;
142
143         return topo_info.nr_assigned_cpus++;
144 }
145
146 static void topo_set_cpuids(unsigned int cpu, u32 apic_id, u32 acpi_id)
147 {
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;
151 #endif
152         set_cpu_possible(cpu, true);
153         set_cpu_present(cpu, true);
154
155         if (system_state != SYSTEM_BOOTING)
156                 cpu_mark_primary_thread(cpu, apic_id);
157 }
158
159 static __init bool check_for_real_bsp(u32 apic_id)
160 {
161         /*
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
168          * system.
169          *
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.
173          */
174         if (topo_info.real_bsp_apic_id != BAD_APICID)
175                 return false;
176
177         if (apic_id == topo_info.boot_cpu_apic_id) {
178                 topo_info.real_bsp_apic_id = apic_id;
179                 return false;
180         }
181
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");
185
186         topo_info.real_bsp_apic_id = apic_id;
187         return true;
188 }
189
190 static __init void topo_register_apic(u32 apic_id, u32 acpi_id, bool present)
191 {
192         int cpu, dom;
193
194         if (present) {
195                 set_bit(apic_id, phys_cpu_present_map);
196
197                 /*
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
201                  * mechanisms.
202                  */
203                 if (apic_id == topo_info.boot_cpu_apic_id)
204                         cpu = 0;
205                 else
206                         cpu = topo_get_cpunr(apic_id);
207
208                 cpuid_to_apicid[cpu] = apic_id;
209                 topo_set_cpuids(cpu, apic_id, acpi_id);
210         } else {
211                 topo_info.nr_disabled_cpus++;
212         }
213
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);
217 }
218
219 /**
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
224  */
225 void __init topology_register_apic(u32 apic_id, u32 acpi_id, bool present)
226 {
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++;
230                 return;
231         }
232
233         if (check_for_real_bsp(apic_id)) {
234                 topo_info.nr_rejected_cpus++;
235                 return;
236         }
237
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++;
242                 return;
243         }
244
245         topo_register_apic(apic_id, acpi_id, present);
246 }
247
248 /**
249  * topology_register_boot_apic - Register the boot CPU APIC
250  * @apic_id:    The APIC ID to set up
251  *
252  * Separate so CPU #0 can be assigned
253  */
254 void __init topology_register_boot_apic(u32 apic_id)
255 {
256         WARN_ON_ONCE(topo_info.boot_cpu_apic_id != BAD_APICID);
257
258         topo_info.boot_cpu_apic_id = apic_id;
259         topo_register_apic(apic_id, CPU_ACPIID_INVALID, true);
260 }
261
262 #ifdef CONFIG_ACPI_HOTPLUG_CPU
263 /**
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
267  */
268 int topology_hotplug_apic(u32 apic_id, u32 acpi_id)
269 {
270         int cpu;
271
272         if (apic_id >= MAX_LOCAL_APIC)
273                 return -EINVAL;
274
275         /* Reject if the APIC ID was not registered during enumeration. */
276         if (!test_bit(apic_id, apic_maps[TOPO_SMT_DOMAIN].map))
277                 return -ENODEV;
278
279         cpu = topo_lookup_cpuid(apic_id);
280         if (cpu < 0)
281                 return -ENOSPC;
282
283         set_bit(apic_id, phys_cpu_present_map);
284         topo_set_cpuids(cpu, apic_id, acpi_id);
285         return cpu;
286 }
287
288 /**
289  * topology_hotunplug_apic - Remove a physical hotplugged APIC after boot
290  * @cpu:        The CPU number for which the APIC ID is removed
291  */
292 void topology_hotunplug_apic(unsigned int cpu)
293 {
294         u32 apic_id = cpuid_to_apicid[cpu];
295
296         if (apic_id == BAD_APICID)
297                 return;
298
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);
302 }
303 #endif
304
305 #ifdef CONFIG_SMP
306 static unsigned int max_possible_cpus __initdata = NR_CPUS;
307
308 /**
309  * topology_apply_cmdline_limits_early - Apply topology command line limits early
310  *
311  * Ensure that command line limits are in effect before firmware parsing
312  * takes place.
313  */
314 void __init topology_apply_cmdline_limits_early(void)
315 {
316         unsigned int possible = nr_cpu_ids;
317
318         /* 'maxcpus=0' 'nosmp' 'nolapic' 'disableapic' 'noapic' */
319         if (!setup_max_cpus || ioapic_is_disabled || apic_is_disabled)
320                 possible = 1;
321
322         /* 'possible_cpus=N' */
323         possible = min_t(unsigned int, max_possible_cpus, possible);
324
325         if (possible < nr_cpu_ids) {
326                 pr_info("Limiting to %u possible CPUs\n", possible);
327                 set_nr_cpu_ids(possible);
328         }
329 }
330
331 static __init bool restrict_to_up(void)
332 {
333         if (!smp_found_config || ioapic_is_disabled)
334                 return true;
335         /*
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
339          * here.
340          */
341         if (xen_pv_domain())
342                 return false;
343
344         return apic_is_disabled;
345 }
346
347 void __init topology_init_possible_cpus(void)
348 {
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;
353         u32 apicid;
354
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;
359                 }
360                 allowed = min_t(unsigned int, total, nr_cpu_ids);
361         }
362
363         if (total > allowed)
364                 pr_warn("%u possible CPUs exceed the limit of %u\n", total, allowed);
365
366         assigned = min_t(unsigned int, allowed, assigned);
367         disabled = allowed - assigned;
368
369         topo_info.nr_assigned_cpus = assigned;
370         topo_info.nr_disabled_cpus = disabled;
371
372         total_cpus = allowed;
373         set_nr_cpu_ids(allowed);
374
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);
378
379         init_cpu_present(cpumask_of(0));
380         init_cpu_possible(cpumask_of(0));
381
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)
387                         break;
388                 cpuid_to_apicid[topo_info.nr_assigned_cpus++] = apicid;
389         }
390
391         for (cpu = 0; cpu < allowed; cpu++) {
392                 apicid = cpuid_to_apicid[cpu];
393
394                 set_cpu_possible(cpu, true);
395
396                 if (apicid == BAD_APICID)
397                         continue;
398
399                 set_cpu_present(cpu, test_bit(apicid, phys_cpu_present_map));
400         }
401 }
402
403 /*
404  * Late SMP disable after sizing CPU masks when APIC/IOAPIC setup failed.
405  */
406 void __init topology_reset_possible_cpus_up(void)
407 {
408         init_cpu_present(cpumask_of(0));
409         init_cpu_possible(cpumask_of(0));
410
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);
414 }
415
416 static int __init setup_possible_cpus(char *str)
417 {
418         get_option(&str, &max_possible_cpus);
419         return 0;
420 }
421 early_param("possible_cpus", setup_possible_cpus);
422 #endif