smpboot.c 36 KB

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