1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * x86 SMP booting functions
4 *
5 * (c) 1995 Alan Cox, Building #3 <[email protected]>
6 * (c) 1998, 1999, 2000, 2009 Ingo Molnar <[email protected]>
7 * Copyright 2001 Andi Kleen, SuSE Labs.
8 *
9 * Much of the core SMP work is based on previous work by Thomas Radke, to
10 * whom a great many thanks are extended.
11 *
12 * Thanks to Intel for making available several different Pentium,
13 * Pentium Pro and Pentium-II/Xeon MP machines.
14 * Original development of Linux SMP code supported by Caldera.
15 *
16 * Fixes
17 * Felix Koop : NR_CPUS used properly
18 * Jose Renau : Handle single CPU case.
19 * Alan Cox : By repeated request 8) - Total BogoMIPS report.
20 * Greg Wright : Fix for kernel stacks panic.
21 * Erich Boleyn : MP v1.4 and additional changes.
22 * Matthias Sattler : Changes for 2.1 kernel map.
23 * Michel Lespinasse : Changes for 2.1 kernel map.
24 * Michael Chastain : Change trampoline.S to gnu as.
25 * Alan Cox : Dumb bug: 'B' step PPro's are fine
26 * Ingo Molnar : Added APIC timers, based on code
27 * from Jose Renau
28 * Ingo Molnar : various cleanups and rewrites
29 * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
30 * Maciej W. Rozycki : Bits for genuine 82489DX APICs
31 * Andi Kleen : Changed for SMP boot into long mode.
32 * Martin J. Bligh : Added support for multi-quad systems
33 * Dave Jones : Report invalid combinations of Athlon CPUs.
34 * Rusty Russell : Hacked into shape for new "hotplug" boot process.
35 * Andi Kleen : Converted to new state machine.
36 * Ashok Raj : CPU hotplug support
37 * Glauber Costa : i386 and x86_64 integration
38 */
39
40 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41
42 #include <linux/init.h>
43 #include <linux/smp.h>
44 #include <linux/export.h>
45 #include <linux/sched.h>
46 #include <linux/sched/topology.h>
47 #include <linux/sched/hotplug.h>
48 #include <linux/sched/task_stack.h>
49 #include <linux/percpu.h>
50 #include <linux/memblock.h>
51 #include <linux/err.h>
52 #include <linux/nmi.h>
53 #include <linux/tboot.h>
54 #include <linux/gfp.h>
55 #include <linux/cpuidle.h>
56 #include <linux/kexec.h>
57 #include <linux/numa.h>
58 #include <linux/pgtable.h>
59 #include <linux/overflow.h>
60 #include <linux/stackprotector.h>
61 #include <linux/cpuhotplug.h>
62 #include <linux/mc146818rtc.h>
63 #include <linux/acpi.h>
64
65 #include <asm/acpi.h>
66 #include <asm/cacheinfo.h>
67 #include <asm/cpuid.h>
68 #include <asm/desc.h>
69 #include <asm/nmi.h>
70 #include <asm/irq.h>
71 #include <asm/realmode.h>
72 #include <asm/cpu.h>
73 #include <asm/numa.h>
74 #include <asm/tlbflush.h>
75 #include <asm/mtrr.h>
76 #include <asm/mwait.h>
77 #include <asm/apic.h>
78 #include <asm/io_apic.h>
79 #include <asm/fpu/api.h>
80 #include <asm/setup.h>
81 #include <asm/uv/uv.h>
82 #include <asm/microcode.h>
83 #include <asm/i8259.h>
84 #include <asm/misc.h>
85 #include <asm/qspinlock.h>
86 #include <asm/intel-family.h>
87 #include <asm/cpu_device_id.h>
88 #include <asm/spec-ctrl.h>
89 #include <asm/hw_irq.h>
90 #include <asm/stackprotector.h>
91 #include <asm/sev.h>
92 #include <asm/spec-ctrl.h>
93
94 /* representing HT siblings of each logical CPU */
95 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
96 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
97
98 /* representing HT and core siblings of each logical CPU */
99 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
100 EXPORT_PER_CPU_SYMBOL(cpu_core_map);
101
102 /* representing HT, core, and die siblings of each logical CPU */
103 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_die_map);
104 EXPORT_PER_CPU_SYMBOL(cpu_die_map);
105
106 /* CPUs which are the primary SMT threads */
107 struct cpumask __cpu_primary_thread_mask __read_mostly;
108
109 /* Representing CPUs for which sibling maps can be computed */
110 static cpumask_var_t cpu_sibling_setup_mask;
111
112 struct mwait_cpu_dead {
113 unsigned int control;
114 unsigned int status;
115 };
116
117 #define CPUDEAD_MWAIT_WAIT 0xDEADBEEF
118 #define CPUDEAD_MWAIT_KEXEC_HLT 0x4A17DEAD
119
120 /*
121 * Cache line aligned data for mwait_play_dead(). Separate on purpose so
122 * that it's unlikely to be touched by other CPUs.
123 */
124 static DEFINE_PER_CPU_ALIGNED(struct mwait_cpu_dead, mwait_cpu_dead);
125
126 /* Maximum number of SMT threads on any online core */
127 int __read_mostly __max_smt_threads = 1;
128
129 /* Flag to indicate if a complete sched domain rebuild is required */
130 bool x86_topology_update;
131
arch_update_cpu_topology(void)132 int arch_update_cpu_topology(void)
133 {
134 int retval = x86_topology_update;
135
136 x86_topology_update = false;
137 return retval;
138 }
139
140 static unsigned int smpboot_warm_reset_vector_count;
141
smpboot_setup_warm_reset_vector(unsigned long start_eip)142 static inline void smpboot_setup_warm_reset_vector(unsigned long start_eip)
143 {
144 unsigned long flags;
145
146 spin_lock_irqsave(&rtc_lock, flags);
147 if (!smpboot_warm_reset_vector_count++) {
148 CMOS_WRITE(0xa, 0xf);
149 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_HIGH)) = start_eip >> 4;
150 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) = start_eip & 0xf;
151 }
152 spin_unlock_irqrestore(&rtc_lock, flags);
153 }
154
smpboot_restore_warm_reset_vector(void)155 static inline void smpboot_restore_warm_reset_vector(void)
156 {
157 unsigned long flags;
158
159 /*
160 * Paranoid: Set warm reset code and vector here back
161 * to default values.
162 */
163 spin_lock_irqsave(&rtc_lock, flags);
164 if (!--smpboot_warm_reset_vector_count) {
165 CMOS_WRITE(0, 0xf);
166 *((volatile u32 *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) = 0;
167 }
168 spin_unlock_irqrestore(&rtc_lock, flags);
169
170 }
171
172 /* Run the next set of setup steps for the upcoming CPU */
ap_starting(void)173 static void ap_starting(void)
174 {
175 int cpuid = smp_processor_id();
176
177 /* Mop up eventual mwait_play_dead() wreckage */
178 this_cpu_write(mwait_cpu_dead.status, 0);
179 this_cpu_write(mwait_cpu_dead.control, 0);
180
181 /*
182 * If woken up by an INIT in an 82489DX configuration the alive
183 * synchronization guarantees that the CPU does not reach this
184 * point before an INIT_deassert IPI reaches the local APIC, so it
185 * is now safe to touch the local APIC.
186 *
187 * Set up this CPU, first the APIC, which is probably redundant on
188 * most boards.
189 */
190 apic_ap_setup();
191
192 /* Save the processor parameters. */
193 smp_store_cpu_info(cpuid);
194
195 /*
196 * The topology information must be up to date before
197 * notify_cpu_starting().
198 */
199 set_cpu_sibling_map(cpuid);
200
201 ap_init_aperfmperf();
202
203 pr_debug("Stack at about %p\n", &cpuid);
204
205 wmb();
206
207 /*
208 * This runs the AP through all the cpuhp states to its target
209 * state CPUHP_ONLINE.
210 */
211 notify_cpu_starting(cpuid);
212 }
213
ap_calibrate_delay(void)214 static void ap_calibrate_delay(void)
215 {
216 /*
217 * Calibrate the delay loop and update loops_per_jiffy in cpu_data.
218 * smp_store_cpu_info() stored a value that is close but not as
219 * accurate as the value just calculated.
220 *
221 * As this is invoked after the TSC synchronization check,
222 * calibrate_delay_is_known() will skip the calibration routine
223 * when TSC is synchronized across sockets.
224 */
225 calibrate_delay();
226 cpu_data(smp_processor_id()).loops_per_jiffy = loops_per_jiffy;
227 }
228
229 /*
230 * Activate a secondary processor.
231 */
start_secondary(void * unused)232 static void notrace start_secondary(void *unused)
233 {
234 /*
235 * Don't put *anything* except direct CPU state initialization
236 * before cpu_init(), SMP booting is too fragile that we want to
237 * limit the things done here to the most necessary things.
238 */
239 cr4_init();
240
241 /*
242 * 32-bit specific. 64-bit reaches this code with the correct page
243 * table established. Yet another historical divergence.
244 */
245 if (IS_ENABLED(CONFIG_X86_32)) {
246 /* switch away from the initial page table */
247 load_cr3(swapper_pg_dir);
248 __flush_tlb_all();
249 }
250
251 cpu_init_exception_handling(false);
252
253 /*
254 * Load the microcode before reaching the AP alive synchronization
255 * point below so it is not part of the full per CPU serialized
256 * bringup part when "parallel" bringup is enabled.
257 *
258 * That's even safe when hyperthreading is enabled in the CPU as
259 * the core code starts the primary threads first and leaves the
260 * secondary threads waiting for SIPI. Loading microcode on
261 * physical cores concurrently is a safe operation.
262 *
263 * This covers both the Intel specific issue that concurrent
264 * microcode loading on SMT siblings must be prohibited and the
265 * vendor independent issue`that microcode loading which changes
266 * CPUID, MSRs etc. must be strictly serialized to maintain
267 * software state correctness.
268 */
269 load_ucode_ap();
270
271 /*
272 * Synchronization point with the hotplug core. Sets this CPUs
273 * synchronization state to ALIVE and spin-waits for the control CPU to
274 * release this CPU for further bringup.
275 */
276 cpuhp_ap_sync_alive();
277
278 cpu_init();
279 fpu__init_cpu();
280 rcutree_report_cpu_starting(raw_smp_processor_id());
281 x86_cpuinit.early_percpu_clock_init();
282
283 ap_starting();
284
285 /* Check TSC synchronization with the control CPU. */
286 check_tsc_sync_target();
287
288 /*
289 * Calibrate the delay loop after the TSC synchronization check.
290 * This allows to skip the calibration when TSC is synchronized
291 * across sockets.
292 */
293 ap_calibrate_delay();
294
295 speculative_store_bypass_ht_init();
296
297 /*
298 * Lock vector_lock, set CPU online and bring the vector
299 * allocator online. Online must be set with vector_lock held
300 * to prevent a concurrent irq setup/teardown from seeing a
301 * half valid vector space.
302 */
303 lock_vector_lock();
304 set_cpu_online(smp_processor_id(), true);
305 lapic_online();
306 unlock_vector_lock();
307 x86_platform.nmi_init();
308
309 /* enable local interrupts */
310 local_irq_enable();
311
312 x86_cpuinit.setup_percpu_clockev();
313
314 wmb();
315 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
316 }
317
318 /*
319 * The bootstrap kernel entry code has set these up. Save them for
320 * a given CPU
321 */
smp_store_cpu_info(int id)322 void smp_store_cpu_info(int id)
323 {
324 struct cpuinfo_x86 *c = &cpu_data(id);
325
326 /* Copy boot_cpu_data only on the first bringup */
327 if (!c->initialized)
328 *c = boot_cpu_data;
329 c->cpu_index = id;
330 /*
331 * During boot time, CPU0 has this setup already. Save the info when
332 * bringing up an AP.
333 */
334 identify_secondary_cpu(c);
335 c->initialized = true;
336 }
337
338 static bool
topology_same_node(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)339 topology_same_node(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
340 {
341 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
342
343 return (cpu_to_node(cpu1) == cpu_to_node(cpu2));
344 }
345
346 static bool
topology_sane(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o,const char * name)347 topology_sane(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o, const char *name)
348 {
349 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
350
351 return !WARN_ONCE(!topology_same_node(c, o),
352 "sched: CPU #%d's %s-sibling CPU #%d is not on the same node! "
353 "[node: %d != %d]. Ignoring dependency.\n",
354 cpu1, name, cpu2, cpu_to_node(cpu1), cpu_to_node(cpu2));
355 }
356
357 #define link_mask(mfunc, c1, c2) \
358 do { \
359 cpumask_set_cpu((c1), mfunc(c2)); \
360 cpumask_set_cpu((c2), mfunc(c1)); \
361 } while (0)
362
match_smt(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)363 static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
364 {
365 if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
366 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
367
368 if (c->topo.pkg_id == o->topo.pkg_id &&
369 c->topo.die_id == o->topo.die_id &&
370 c->topo.amd_node_id == o->topo.amd_node_id &&
371 per_cpu_llc_id(cpu1) == per_cpu_llc_id(cpu2)) {
372 if (c->topo.core_id == o->topo.core_id)
373 return topology_sane(c, o, "smt");
374
375 if ((c->topo.cu_id != 0xff) &&
376 (o->topo.cu_id != 0xff) &&
377 (c->topo.cu_id == o->topo.cu_id))
378 return topology_sane(c, o, "smt");
379 }
380
381 } else if (c->topo.pkg_id == o->topo.pkg_id &&
382 c->topo.die_id == o->topo.die_id &&
383 c->topo.core_id == o->topo.core_id) {
384 return topology_sane(c, o, "smt");
385 }
386
387 return false;
388 }
389
match_die(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)390 static bool match_die(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
391 {
392 if (c->topo.pkg_id != o->topo.pkg_id || c->topo.die_id != o->topo.die_id)
393 return false;
394
395 if (cpu_feature_enabled(X86_FEATURE_TOPOEXT) && topology_amd_nodes_per_pkg() > 1)
396 return c->topo.amd_node_id == o->topo.amd_node_id;
397
398 return true;
399 }
400
match_l2c(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)401 static bool match_l2c(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
402 {
403 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
404
405 /* If the arch didn't set up l2c_id, fall back to SMT */
406 if (per_cpu_l2c_id(cpu1) == BAD_APICID)
407 return match_smt(c, o);
408
409 /* Do not match if L2 cache id does not match: */
410 if (per_cpu_l2c_id(cpu1) != per_cpu_l2c_id(cpu2))
411 return false;
412
413 return topology_sane(c, o, "l2c");
414 }
415
416 /*
417 * Unlike the other levels, we do not enforce keeping a
418 * multicore group inside a NUMA node. If this happens, we will
419 * discard the MC level of the topology later.
420 */
match_pkg(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)421 static bool match_pkg(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
422 {
423 if (c->topo.pkg_id == o->topo.pkg_id)
424 return true;
425 return false;
426 }
427
428 /*
429 * Define intel_cod_cpu[] for Intel COD (Cluster-on-Die) CPUs.
430 *
431 * Any Intel CPU that has multiple nodes per package and does not
432 * match intel_cod_cpu[] has the SNC (Sub-NUMA Cluster) topology.
433 *
434 * When in SNC mode, these CPUs enumerate an LLC that is shared
435 * by multiple NUMA nodes. The LLC is shared for off-package data
436 * access but private to the NUMA node (half of the package) for
437 * on-package access. CPUID (the source of the information about
438 * the LLC) can only enumerate the cache as shared or unshared,
439 * but not this particular configuration.
440 */
441
442 static const struct x86_cpu_id intel_cod_cpu[] = {
443 X86_MATCH_VFM(INTEL_HASWELL_X, 0), /* COD */
444 X86_MATCH_VFM(INTEL_BROADWELL_X, 0), /* COD */
445 X86_MATCH_VFM(INTEL_ANY, 1), /* SNC */
446 {}
447 };
448
match_llc(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)449 static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
450 {
451 const struct x86_cpu_id *id = x86_match_cpu(intel_cod_cpu);
452 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
453 bool intel_snc = id && id->driver_data;
454
455 /* Do not match if we do not have a valid APICID for cpu: */
456 if (per_cpu_llc_id(cpu1) == BAD_APICID)
457 return false;
458
459 /* Do not match if LLC id does not match: */
460 if (per_cpu_llc_id(cpu1) != per_cpu_llc_id(cpu2))
461 return false;
462
463 /*
464 * Allow the SNC topology without warning. Return of false
465 * means 'c' does not share the LLC of 'o'. This will be
466 * reflected to userspace.
467 */
468 if (match_pkg(c, o) && !topology_same_node(c, o) && intel_snc)
469 return false;
470
471 return topology_sane(c, o, "llc");
472 }
473
474
x86_sched_itmt_flags(void)475 static inline int x86_sched_itmt_flags(void)
476 {
477 return sysctl_sched_itmt_enabled ? SD_ASYM_PACKING : 0;
478 }
479
480 #ifdef CONFIG_SCHED_MC
x86_core_flags(void)481 static int x86_core_flags(void)
482 {
483 return cpu_core_flags() | x86_sched_itmt_flags();
484 }
485 #endif
486 #ifdef CONFIG_SCHED_CLUSTER
x86_cluster_flags(void)487 static int x86_cluster_flags(void)
488 {
489 return cpu_cluster_flags() | x86_sched_itmt_flags();
490 }
491 #endif
492
493 /*
494 * Set if a package/die has multiple NUMA nodes inside.
495 * AMD Magny-Cours, Intel Cluster-on-Die, and Intel
496 * Sub-NUMA Clustering have this.
497 */
498 static bool x86_has_numa_in_package;
499
500 static struct sched_domain_topology_level x86_topology[6];
501
build_sched_topology(void)502 static void __init build_sched_topology(void)
503 {
504 int i = 0;
505
506 #ifdef CONFIG_SCHED_SMT
507 x86_topology[i++] = (struct sched_domain_topology_level){
508 cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT)
509 };
510 #endif
511 #ifdef CONFIG_SCHED_CLUSTER
512 x86_topology[i++] = (struct sched_domain_topology_level){
513 cpu_clustergroup_mask, x86_cluster_flags, SD_INIT_NAME(CLS)
514 };
515 #endif
516 #ifdef CONFIG_SCHED_MC
517 x86_topology[i++] = (struct sched_domain_topology_level){
518 cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC)
519 };
520 #endif
521 /*
522 * When there is NUMA topology inside the package skip the PKG domain
523 * since the NUMA domains will auto-magically create the right spanning
524 * domains based on the SLIT.
525 */
526 if (!x86_has_numa_in_package) {
527 x86_topology[i++] = (struct sched_domain_topology_level){
528 cpu_cpu_mask, x86_sched_itmt_flags, SD_INIT_NAME(PKG)
529 };
530 }
531
532 /*
533 * There must be one trailing NULL entry left.
534 */
535 BUG_ON(i >= ARRAY_SIZE(x86_topology)-1);
536
537 set_sched_topology(x86_topology);
538 }
539
set_cpu_sibling_map(int cpu)540 void set_cpu_sibling_map(int cpu)
541 {
542 bool has_smt = __max_threads_per_core > 1;
543 bool has_mp = has_smt || topology_num_cores_per_package() > 1;
544 struct cpuinfo_x86 *c = &cpu_data(cpu);
545 struct cpuinfo_x86 *o;
546 int i, threads;
547
548 cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
549
550 if (!has_mp) {
551 cpumask_set_cpu(cpu, topology_sibling_cpumask(cpu));
552 cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
553 cpumask_set_cpu(cpu, cpu_l2c_shared_mask(cpu));
554 cpumask_set_cpu(cpu, topology_core_cpumask(cpu));
555 cpumask_set_cpu(cpu, topology_die_cpumask(cpu));
556 c->booted_cores = 1;
557 return;
558 }
559
560 for_each_cpu(i, cpu_sibling_setup_mask) {
561 o = &cpu_data(i);
562
563 if (match_pkg(c, o) && !topology_same_node(c, o))
564 x86_has_numa_in_package = true;
565
566 if ((i == cpu) || (has_smt && match_smt(c, o)))
567 link_mask(topology_sibling_cpumask, cpu, i);
568
569 if ((i == cpu) || (has_mp && match_llc(c, o)))
570 link_mask(cpu_llc_shared_mask, cpu, i);
571
572 if ((i == cpu) || (has_mp && match_l2c(c, o)))
573 link_mask(cpu_l2c_shared_mask, cpu, i);
574
575 if ((i == cpu) || (has_mp && match_die(c, o)))
576 link_mask(topology_die_cpumask, cpu, i);
577 }
578
579 threads = cpumask_weight(topology_sibling_cpumask(cpu));
580 if (threads > __max_smt_threads)
581 __max_smt_threads = threads;
582
583 for_each_cpu(i, topology_sibling_cpumask(cpu))
584 cpu_data(i).smt_active = threads > 1;
585
586 /*
587 * This needs a separate iteration over the cpus because we rely on all
588 * topology_sibling_cpumask links to be set-up.
589 */
590 for_each_cpu(i, cpu_sibling_setup_mask) {
591 o = &cpu_data(i);
592
593 if ((i == cpu) || (has_mp && match_pkg(c, o))) {
594 link_mask(topology_core_cpumask, cpu, i);
595
596 /*
597 * Does this new cpu bringup a new core?
598 */
599 if (threads == 1) {
600 /*
601 * for each core in package, increment
602 * the booted_cores for this new cpu
603 */
604 if (cpumask_first(
605 topology_sibling_cpumask(i)) == i)
606 c->booted_cores++;
607 /*
608 * increment the core count for all
609 * the other cpus in this package
610 */
611 if (i != cpu)
612 cpu_data(i).booted_cores++;
613 } else if (i != cpu && !c->booted_cores)
614 c->booted_cores = cpu_data(i).booted_cores;
615 }
616 }
617 }
618
619 /* maps the cpu to the sched domain representing multi-core */
cpu_coregroup_mask(int cpu)620 const struct cpumask *cpu_coregroup_mask(int cpu)
621 {
622 return cpu_llc_shared_mask(cpu);
623 }
624
cpu_clustergroup_mask(int cpu)625 const struct cpumask *cpu_clustergroup_mask(int cpu)
626 {
627 return cpu_l2c_shared_mask(cpu);
628 }
629 EXPORT_SYMBOL_GPL(cpu_clustergroup_mask);
630
impress_friends(void)631 static void impress_friends(void)
632 {
633 int cpu;
634 unsigned long bogosum = 0;
635 /*
636 * Allow the user to impress friends.
637 */
638 pr_debug("Before bogomips\n");
639 for_each_online_cpu(cpu)
640 bogosum += cpu_data(cpu).loops_per_jiffy;
641
642 pr_info("Total of %d processors activated (%lu.%02lu BogoMIPS)\n",
643 num_online_cpus(),
644 bogosum/(500000/HZ),
645 (bogosum/(5000/HZ))%100);
646
647 pr_debug("Before bogocount - setting activated=1\n");
648 }
649
650 /*
651 * The Multiprocessor Specification 1.4 (1997) example code suggests
652 * that there should be a 10ms delay between the BSP asserting INIT
653 * and de-asserting INIT, when starting a remote processor.
654 * But that slows boot and resume on modern processors, which include
655 * many cores and don't require that delay.
656 *
657 * Cmdline "init_cpu_udelay=" is available to over-ride this delay.
658 * Modern processor families are quirked to remove the delay entirely.
659 */
660 #define UDELAY_10MS_DEFAULT 10000
661
662 static unsigned int init_udelay = UINT_MAX;
663
cpu_init_udelay(char * str)664 static int __init cpu_init_udelay(char *str)
665 {
666 get_option(&str, &init_udelay);
667
668 return 0;
669 }
670 early_param("cpu_init_udelay", cpu_init_udelay);
671
smp_quirk_init_udelay(void)672 static void __init smp_quirk_init_udelay(void)
673 {
674 /* if cmdline changed it from default, leave it alone */
675 if (init_udelay != UINT_MAX)
676 return;
677
678 /* if modern processor, use no delay */
679 if (((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 6)) ||
680 ((boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) && (boot_cpu_data.x86 >= 0x18)) ||
681 ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && (boot_cpu_data.x86 >= 0xF))) {
682 init_udelay = 0;
683 return;
684 }
685 /* else, use legacy delay */
686 init_udelay = UDELAY_10MS_DEFAULT;
687 }
688
689 /*
690 * Wake up AP by INIT, INIT, STARTUP sequence.
691 */
send_init_sequence(u32 phys_apicid)692 static void send_init_sequence(u32 phys_apicid)
693 {
694 int maxlvt = lapic_get_maxlvt();
695
696 /* Be paranoid about clearing APIC errors. */
697 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
698 /* Due to the Pentium erratum 3AP. */
699 if (maxlvt > 3)
700 apic_write(APIC_ESR, 0);
701 apic_read(APIC_ESR);
702 }
703
704 /* Assert INIT on the target CPU */
705 apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT, phys_apicid);
706 safe_apic_wait_icr_idle();
707
708 udelay(init_udelay);
709
710 /* Deassert INIT on the target CPU */
711 apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
712 safe_apic_wait_icr_idle();
713 }
714
715 /*
716 * Wake up AP by INIT, INIT, STARTUP sequence.
717 */
wakeup_secondary_cpu_via_init(u32 phys_apicid,unsigned long start_eip)718 static int wakeup_secondary_cpu_via_init(u32 phys_apicid, unsigned long start_eip)
719 {
720 unsigned long send_status = 0, accept_status = 0;
721 int num_starts, j, maxlvt;
722
723 preempt_disable();
724 maxlvt = lapic_get_maxlvt();
725 send_init_sequence(phys_apicid);
726
727 mb();
728
729 /*
730 * Should we send STARTUP IPIs ?
731 *
732 * Determine this based on the APIC version.
733 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
734 */
735 if (APIC_INTEGRATED(boot_cpu_apic_version))
736 num_starts = 2;
737 else
738 num_starts = 0;
739
740 /*
741 * Run STARTUP IPI loop.
742 */
743 pr_debug("#startup loops: %d\n", num_starts);
744
745 for (j = 1; j <= num_starts; j++) {
746 pr_debug("Sending STARTUP #%d\n", j);
747 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
748 apic_write(APIC_ESR, 0);
749 apic_read(APIC_ESR);
750 pr_debug("After apic_write\n");
751
752 /*
753 * STARTUP IPI
754 */
755
756 /* Target chip */
757 /* Boot on the stack */
758 /* Kick the second */
759 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
760 phys_apicid);
761
762 /*
763 * Give the other CPU some time to accept the IPI.
764 */
765 if (init_udelay == 0)
766 udelay(10);
767 else
768 udelay(300);
769
770 pr_debug("Startup point 1\n");
771
772 pr_debug("Waiting for send to finish...\n");
773 send_status = safe_apic_wait_icr_idle();
774
775 /*
776 * Give the other CPU some time to accept the IPI.
777 */
778 if (init_udelay == 0)
779 udelay(10);
780 else
781 udelay(200);
782
783 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
784 apic_write(APIC_ESR, 0);
785 accept_status = (apic_read(APIC_ESR) & 0xEF);
786 if (send_status || accept_status)
787 break;
788 }
789 pr_debug("After Startup\n");
790
791 if (send_status)
792 pr_err("APIC never delivered???\n");
793 if (accept_status)
794 pr_err("APIC delivery error (%lx)\n", accept_status);
795
796 preempt_enable();
797 return (send_status | accept_status);
798 }
799
800 /* reduce the number of lines printed when booting a large cpu count system */
announce_cpu(int cpu,int apicid)801 static void announce_cpu(int cpu, int apicid)
802 {
803 static int width, node_width, first = 1;
804 static int current_node = NUMA_NO_NODE;
805 int node = early_cpu_to_node(cpu);
806
807 if (!width)
808 width = num_digits(num_possible_cpus()) + 1; /* + '#' sign */
809
810 if (!node_width)
811 node_width = num_digits(num_possible_nodes()) + 1; /* + '#' */
812
813 if (system_state < SYSTEM_RUNNING) {
814 if (first)
815 pr_info("x86: Booting SMP configuration:\n");
816
817 if (node != current_node) {
818 if (current_node > (-1))
819 pr_cont("\n");
820 current_node = node;
821
822 printk(KERN_INFO ".... node %*s#%d, CPUs: ",
823 node_width - num_digits(node), " ", node);
824 }
825
826 /* Add padding for the BSP */
827 if (first)
828 pr_cont("%*s", width + 1, " ");
829 first = 0;
830
831 pr_cont("%*s#%d", width - num_digits(cpu), " ", cpu);
832 } else
833 pr_info("Booting Node %d Processor %d APIC 0x%x\n",
834 node, cpu, apicid);
835 }
836
common_cpu_up(unsigned int cpu,struct task_struct * idle)837 int common_cpu_up(unsigned int cpu, struct task_struct *idle)
838 {
839 int ret;
840
841 /* Just in case we booted with a single CPU. */
842 alternatives_enable_smp();
843
844 per_cpu(pcpu_hot.current_task, cpu) = idle;
845 cpu_init_stack_canary(cpu, idle);
846
847 /* Initialize the interrupt stack(s) */
848 ret = irq_init_percpu_irqstack(cpu);
849 if (ret)
850 return ret;
851
852 #ifdef CONFIG_X86_32
853 /* Stack for startup_32 can be just as for start_secondary onwards */
854 per_cpu(pcpu_hot.top_of_stack, cpu) = task_top_of_stack(idle);
855 #endif
856 return 0;
857 }
858
859 /*
860 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
861 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
862 * Returns zero if startup was successfully sent, else error code from
863 * ->wakeup_secondary_cpu.
864 */
do_boot_cpu(u32 apicid,int cpu,struct task_struct * idle)865 static int do_boot_cpu(u32 apicid, int cpu, struct task_struct *idle)
866 {
867 unsigned long start_ip = real_mode_header->trampoline_start;
868 int ret;
869
870 #ifdef CONFIG_X86_64
871 /* If 64-bit wakeup method exists, use the 64-bit mode trampoline IP */
872 if (apic->wakeup_secondary_cpu_64)
873 start_ip = real_mode_header->trampoline_start64;
874 #endif
875 idle->thread.sp = (unsigned long)task_pt_regs(idle);
876 initial_code = (unsigned long)start_secondary;
877
878 if (IS_ENABLED(CONFIG_X86_32)) {
879 early_gdt_descr.address = (unsigned long)get_cpu_gdt_rw(cpu);
880 initial_stack = idle->thread.sp;
881 } else if (!(smpboot_control & STARTUP_PARALLEL_MASK)) {
882 smpboot_control = cpu;
883 }
884
885 /* Enable the espfix hack for this CPU */
886 init_espfix_ap(cpu);
887
888 /* So we see what's up */
889 announce_cpu(cpu, apicid);
890
891 /*
892 * This grunge runs the startup process for
893 * the targeted processor.
894 */
895 if (x86_platform.legacy.warm_reset) {
896
897 pr_debug("Setting warm reset code and vector.\n");
898
899 smpboot_setup_warm_reset_vector(start_ip);
900 /*
901 * Be paranoid about clearing APIC errors.
902 */
903 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
904 apic_write(APIC_ESR, 0);
905 apic_read(APIC_ESR);
906 }
907 }
908
909 smp_mb();
910
911 /*
912 * Wake up a CPU in difference cases:
913 * - Use a method from the APIC driver if one defined, with wakeup
914 * straight to 64-bit mode preferred over wakeup to RM.
915 * Otherwise,
916 * - Use an INIT boot APIC message
917 */
918 if (apic->wakeup_secondary_cpu_64)
919 ret = apic->wakeup_secondary_cpu_64(apicid, start_ip);
920 else if (apic->wakeup_secondary_cpu)
921 ret = apic->wakeup_secondary_cpu(apicid, start_ip);
922 else
923 ret = wakeup_secondary_cpu_via_init(apicid, start_ip);
924
925 /* If the wakeup mechanism failed, cleanup the warm reset vector */
926 if (ret)
927 arch_cpuhp_cleanup_kick_cpu(cpu);
928 return ret;
929 }
930
native_kick_ap(unsigned int cpu,struct task_struct * tidle)931 int native_kick_ap(unsigned int cpu, struct task_struct *tidle)
932 {
933 u32 apicid = apic->cpu_present_to_apicid(cpu);
934 int err;
935
936 lockdep_assert_irqs_enabled();
937
938 pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
939
940 if (apicid == BAD_APICID || !apic_id_valid(apicid)) {
941 pr_err("CPU %u has invalid APIC ID %x. Aborting bringup\n", cpu, apicid);
942 return -EINVAL;
943 }
944
945 if (!test_bit(apicid, phys_cpu_present_map)) {
946 pr_err("CPU %u APIC ID %x is not present. Aborting bringup\n", cpu, apicid);
947 return -EINVAL;
948 }
949
950 /*
951 * Save current MTRR state in case it was changed since early boot
952 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
953 */
954 mtrr_save_state();
955
956 /* the FPU context is blank, nobody can own it */
957 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
958
959 err = common_cpu_up(cpu, tidle);
960 if (err)
961 return err;
962
963 err = do_boot_cpu(apicid, cpu, tidle);
964 if (err)
965 pr_err("do_boot_cpu failed(%d) to wakeup CPU#%u\n", err, cpu);
966
967 return err;
968 }
969
arch_cpuhp_kick_ap_alive(unsigned int cpu,struct task_struct * tidle)970 int arch_cpuhp_kick_ap_alive(unsigned int cpu, struct task_struct *tidle)
971 {
972 return smp_ops.kick_ap_alive(cpu, tidle);
973 }
974
arch_cpuhp_cleanup_kick_cpu(unsigned int cpu)975 void arch_cpuhp_cleanup_kick_cpu(unsigned int cpu)
976 {
977 /* Cleanup possible dangling ends... */
978 if (smp_ops.kick_ap_alive == native_kick_ap && x86_platform.legacy.warm_reset)
979 smpboot_restore_warm_reset_vector();
980 }
981
arch_cpuhp_cleanup_dead_cpu(unsigned int cpu)982 void arch_cpuhp_cleanup_dead_cpu(unsigned int cpu)
983 {
984 if (smp_ops.cleanup_dead_cpu)
985 smp_ops.cleanup_dead_cpu(cpu);
986
987 if (system_state == SYSTEM_RUNNING)
988 pr_info("CPU %u is now offline\n", cpu);
989 }
990
arch_cpuhp_sync_state_poll(void)991 void arch_cpuhp_sync_state_poll(void)
992 {
993 if (smp_ops.poll_sync_state)
994 smp_ops.poll_sync_state();
995 }
996
997 /**
998 * arch_disable_smp_support() - Disables SMP support for x86 at boottime
999 */
arch_disable_smp_support(void)1000 void __init arch_disable_smp_support(void)
1001 {
1002 disable_ioapic_support();
1003 }
1004
1005 /*
1006 * Fall back to non SMP mode after errors.
1007 *
1008 * RED-PEN audit/test this more. I bet there is more state messed up here.
1009 */
disable_smp(void)1010 static __init void disable_smp(void)
1011 {
1012 pr_info("SMP disabled\n");
1013
1014 disable_ioapic_support();
1015 topology_reset_possible_cpus_up();
1016
1017 cpumask_set_cpu(0, topology_sibling_cpumask(0));
1018 cpumask_set_cpu(0, topology_core_cpumask(0));
1019 cpumask_set_cpu(0, topology_die_cpumask(0));
1020 }
1021
smp_prepare_cpus_common(void)1022 void __init smp_prepare_cpus_common(void)
1023 {
1024 unsigned int cpu, node;
1025
1026 /* Mark all except the boot CPU as hotpluggable */
1027 for_each_possible_cpu(cpu) {
1028 if (cpu)
1029 per_cpu(cpu_info.cpu_index, cpu) = nr_cpu_ids;
1030 }
1031
1032 for_each_possible_cpu(cpu) {
1033 node = cpu_to_node(cpu);
1034
1035 zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map, cpu), GFP_KERNEL, node);
1036 zalloc_cpumask_var_node(&per_cpu(cpu_core_map, cpu), GFP_KERNEL, node);
1037 zalloc_cpumask_var_node(&per_cpu(cpu_die_map, cpu), GFP_KERNEL, node);
1038 zalloc_cpumask_var_node(&per_cpu(cpu_llc_shared_map, cpu), GFP_KERNEL, node);
1039 zalloc_cpumask_var_node(&per_cpu(cpu_l2c_shared_map, cpu), GFP_KERNEL, node);
1040 }
1041
1042 set_cpu_sibling_map(0);
1043 }
1044
smp_prepare_boot_cpu(void)1045 void __init smp_prepare_boot_cpu(void)
1046 {
1047 smp_ops.smp_prepare_boot_cpu();
1048 }
1049
1050 #ifdef CONFIG_X86_64
1051 /* Establish whether parallel bringup can be supported. */
arch_cpuhp_init_parallel_bringup(void)1052 bool __init arch_cpuhp_init_parallel_bringup(void)
1053 {
1054 if (!x86_cpuinit.parallel_bringup) {
1055 pr_info("Parallel CPU startup disabled by the platform\n");
1056 return false;
1057 }
1058
1059 smpboot_control = STARTUP_READ_APICID;
1060 pr_debug("Parallel CPU startup enabled: 0x%08x\n", smpboot_control);
1061 return true;
1062 }
1063 #endif
1064
1065 /*
1066 * Prepare for SMP bootup.
1067 * @max_cpus: configured maximum number of CPUs, It is a legacy parameter
1068 * for common interface support.
1069 */
native_smp_prepare_cpus(unsigned int max_cpus)1070 void __init native_smp_prepare_cpus(unsigned int max_cpus)
1071 {
1072 smp_prepare_cpus_common();
1073
1074 switch (apic_intr_mode) {
1075 case APIC_PIC:
1076 case APIC_VIRTUAL_WIRE_NO_CONFIG:
1077 disable_smp();
1078 return;
1079 case APIC_SYMMETRIC_IO_NO_ROUTING:
1080 disable_smp();
1081 /* Setup local timer */
1082 x86_init.timers.setup_percpu_clockev();
1083 return;
1084 case APIC_VIRTUAL_WIRE:
1085 case APIC_SYMMETRIC_IO:
1086 break;
1087 }
1088
1089 /* Setup local timer */
1090 x86_init.timers.setup_percpu_clockev();
1091
1092 pr_info("CPU0: ");
1093 print_cpu_info(&cpu_data(0));
1094
1095 uv_system_init();
1096
1097 smp_quirk_init_udelay();
1098
1099 speculative_store_bypass_ht_init();
1100
1101 snp_set_wakeup_secondary_cpu();
1102 }
1103
arch_thaw_secondary_cpus_begin(void)1104 void arch_thaw_secondary_cpus_begin(void)
1105 {
1106 set_cache_aps_delayed_init(true);
1107 }
1108
arch_thaw_secondary_cpus_end(void)1109 void arch_thaw_secondary_cpus_end(void)
1110 {
1111 cache_aps_init();
1112 }
1113
1114 /*
1115 * Early setup to make printk work.
1116 */
native_smp_prepare_boot_cpu(void)1117 void __init native_smp_prepare_boot_cpu(void)
1118 {
1119 int me = smp_processor_id();
1120
1121 /* SMP handles this from setup_per_cpu_areas() */
1122 if (!IS_ENABLED(CONFIG_SMP))
1123 switch_gdt_and_percpu_base(me);
1124
1125 native_pv_lock_init();
1126 }
1127
native_smp_cpus_done(unsigned int max_cpus)1128 void __init native_smp_cpus_done(unsigned int max_cpus)
1129 {
1130 pr_debug("Boot done\n");
1131
1132 build_sched_topology();
1133 nmi_selftest();
1134 impress_friends();
1135 cache_aps_init();
1136 }
1137
1138 /* correctly size the local cpu masks */
setup_cpu_local_masks(void)1139 void __init setup_cpu_local_masks(void)
1140 {
1141 alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
1142 }
1143
1144 #ifdef CONFIG_HOTPLUG_CPU
1145
1146 /* Recompute SMT state for all CPUs on offline */
recompute_smt_state(void)1147 static void recompute_smt_state(void)
1148 {
1149 int max_threads, cpu;
1150
1151 max_threads = 0;
1152 for_each_online_cpu (cpu) {
1153 int threads = cpumask_weight(topology_sibling_cpumask(cpu));
1154
1155 if (threads > max_threads)
1156 max_threads = threads;
1157 }
1158 __max_smt_threads = max_threads;
1159 }
1160
remove_siblinginfo(int cpu)1161 static void remove_siblinginfo(int cpu)
1162 {
1163 int sibling;
1164 struct cpuinfo_x86 *c = &cpu_data(cpu);
1165
1166 for_each_cpu(sibling, topology_core_cpumask(cpu)) {
1167 cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
1168 /*/
1169 * last thread sibling in this cpu core going down
1170 */
1171 if (cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
1172 cpu_data(sibling).booted_cores--;
1173 }
1174
1175 for_each_cpu(sibling, topology_die_cpumask(cpu))
1176 cpumask_clear_cpu(cpu, topology_die_cpumask(sibling));
1177
1178 for_each_cpu(sibling, topology_sibling_cpumask(cpu)) {
1179 cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
1180 if (cpumask_weight(topology_sibling_cpumask(sibling)) == 1)
1181 cpu_data(sibling).smt_active = false;
1182 }
1183
1184 for_each_cpu(sibling, cpu_llc_shared_mask(cpu))
1185 cpumask_clear_cpu(cpu, cpu_llc_shared_mask(sibling));
1186 for_each_cpu(sibling, cpu_l2c_shared_mask(cpu))
1187 cpumask_clear_cpu(cpu, cpu_l2c_shared_mask(sibling));
1188 cpumask_clear(cpu_llc_shared_mask(cpu));
1189 cpumask_clear(cpu_l2c_shared_mask(cpu));
1190 cpumask_clear(topology_sibling_cpumask(cpu));
1191 cpumask_clear(topology_core_cpumask(cpu));
1192 cpumask_clear(topology_die_cpumask(cpu));
1193 c->topo.core_id = 0;
1194 c->booted_cores = 0;
1195 cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
1196 recompute_smt_state();
1197 }
1198
remove_cpu_from_maps(int cpu)1199 static void remove_cpu_from_maps(int cpu)
1200 {
1201 set_cpu_online(cpu, false);
1202 numa_remove_cpu(cpu);
1203 }
1204
cpu_disable_common(void)1205 void cpu_disable_common(void)
1206 {
1207 int cpu = smp_processor_id();
1208
1209 remove_siblinginfo(cpu);
1210
1211 /* It's now safe to remove this processor from the online map */
1212 lock_vector_lock();
1213 remove_cpu_from_maps(cpu);
1214 unlock_vector_lock();
1215 fixup_irqs();
1216 lapic_offline();
1217 }
1218
native_cpu_disable(void)1219 int native_cpu_disable(void)
1220 {
1221 int ret;
1222
1223 ret = lapic_can_unplug_cpu();
1224 if (ret)
1225 return ret;
1226
1227 cpu_disable_common();
1228
1229 /*
1230 * Disable the local APIC. Otherwise IPI broadcasts will reach
1231 * it. It still responds normally to INIT, NMI, SMI, and SIPI
1232 * messages.
1233 *
1234 * Disabling the APIC must happen after cpu_disable_common()
1235 * which invokes fixup_irqs().
1236 *
1237 * Disabling the APIC preserves already set bits in IRR, but
1238 * an interrupt arriving after disabling the local APIC does not
1239 * set the corresponding IRR bit.
1240 *
1241 * fixup_irqs() scans IRR for set bits so it can raise a not
1242 * yet handled interrupt on the new destination CPU via an IPI
1243 * but obviously it can't do so for IRR bits which are not set.
1244 * IOW, interrupts arriving after disabling the local APIC will
1245 * be lost.
1246 */
1247 apic_soft_disable();
1248
1249 return 0;
1250 }
1251
play_dead_common(void)1252 void play_dead_common(void)
1253 {
1254 idle_task_exit();
1255
1256 cpuhp_ap_report_dead();
1257
1258 local_irq_disable();
1259 }
1260
1261 /*
1262 * We need to flush the caches before going to sleep, lest we have
1263 * dirty data in our caches when we come back up.
1264 */
mwait_play_dead(void)1265 static inline void mwait_play_dead(void)
1266 {
1267 struct mwait_cpu_dead *md = this_cpu_ptr(&mwait_cpu_dead);
1268 unsigned int eax, ebx, ecx, edx;
1269 unsigned int highest_cstate = 0;
1270 unsigned int highest_subcstate = 0;
1271 int i;
1272
1273 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
1274 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
1275 return;
1276 if (!this_cpu_has(X86_FEATURE_MWAIT))
1277 return;
1278 if (!this_cpu_has(X86_FEATURE_CLFLUSH))
1279 return;
1280
1281 eax = CPUID_LEAF_MWAIT;
1282 ecx = 0;
1283 native_cpuid(&eax, &ebx, &ecx, &edx);
1284
1285 /*
1286 * eax will be 0 if EDX enumeration is not valid.
1287 * Initialized below to cstate, sub_cstate value when EDX is valid.
1288 */
1289 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
1290 eax = 0;
1291 } else {
1292 edx >>= MWAIT_SUBSTATE_SIZE;
1293 for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
1294 if (edx & MWAIT_SUBSTATE_MASK) {
1295 highest_cstate = i;
1296 highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
1297 }
1298 }
1299 eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
1300 (highest_subcstate - 1);
1301 }
1302
1303 /* Set up state for the kexec() hack below */
1304 md->status = CPUDEAD_MWAIT_WAIT;
1305 md->control = CPUDEAD_MWAIT_WAIT;
1306
1307 wbinvd();
1308
1309 while (1) {
1310 /*
1311 * The CLFLUSH is a workaround for erratum AAI65 for
1312 * the Xeon 7400 series. It's not clear it is actually
1313 * needed, but it should be harmless in either case.
1314 * The WBINVD is insufficient due to the spurious-wakeup
1315 * case where we return around the loop.
1316 */
1317 mb();
1318 clflush(md);
1319 mb();
1320 __monitor(md, 0, 0);
1321 mb();
1322 __mwait(eax, 0);
1323
1324 if (READ_ONCE(md->control) == CPUDEAD_MWAIT_KEXEC_HLT) {
1325 /*
1326 * Kexec is about to happen. Don't go back into mwait() as
1327 * the kexec kernel might overwrite text and data including
1328 * page tables and stack. So mwait() would resume when the
1329 * monitor cache line is written to and then the CPU goes
1330 * south due to overwritten text, page tables and stack.
1331 *
1332 * Note: This does _NOT_ protect against a stray MCE, NMI,
1333 * SMI. They will resume execution at the instruction
1334 * following the HLT instruction and run into the problem
1335 * which this is trying to prevent.
1336 */
1337 WRITE_ONCE(md->status, CPUDEAD_MWAIT_KEXEC_HLT);
1338 while(1)
1339 native_halt();
1340 }
1341 }
1342 }
1343
1344 /*
1345 * Kick all "offline" CPUs out of mwait on kexec(). See comment in
1346 * mwait_play_dead().
1347 */
smp_kick_mwait_play_dead(void)1348 void smp_kick_mwait_play_dead(void)
1349 {
1350 u32 newstate = CPUDEAD_MWAIT_KEXEC_HLT;
1351 struct mwait_cpu_dead *md;
1352 unsigned int cpu, i;
1353
1354 for_each_cpu_andnot(cpu, cpu_present_mask, cpu_online_mask) {
1355 md = per_cpu_ptr(&mwait_cpu_dead, cpu);
1356
1357 /* Does it sit in mwait_play_dead() ? */
1358 if (READ_ONCE(md->status) != CPUDEAD_MWAIT_WAIT)
1359 continue;
1360
1361 /* Wait up to 5ms */
1362 for (i = 0; READ_ONCE(md->status) != newstate && i < 1000; i++) {
1363 /* Bring it out of mwait */
1364 WRITE_ONCE(md->control, newstate);
1365 udelay(5);
1366 }
1367
1368 if (READ_ONCE(md->status) != newstate)
1369 pr_err_once("CPU%u is stuck in mwait_play_dead()\n", cpu);
1370 }
1371 }
1372
hlt_play_dead(void)1373 void __noreturn hlt_play_dead(void)
1374 {
1375 if (__this_cpu_read(cpu_info.x86) >= 4)
1376 wbinvd();
1377
1378 while (1)
1379 native_halt();
1380 }
1381
1382 /*
1383 * native_play_dead() is essentially a __noreturn function, but it can't
1384 * be marked as such as the compiler may complain about it.
1385 */
native_play_dead(void)1386 void native_play_dead(void)
1387 {
1388 if (cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS))
1389 __update_spec_ctrl(0);
1390
1391 play_dead_common();
1392 tboot_shutdown(TB_SHUTDOWN_WFS);
1393
1394 mwait_play_dead();
1395 if (cpuidle_play_dead())
1396 hlt_play_dead();
1397 }
1398
1399 #else /* ... !CONFIG_HOTPLUG_CPU */
native_cpu_disable(void)1400 int native_cpu_disable(void)
1401 {
1402 return -ENOSYS;
1403 }
1404
native_play_dead(void)1405 void native_play_dead(void)
1406 {
1407 BUG();
1408 }
1409
1410 #endif
1411