smpboot.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. /*
  2. * x86 SMP booting functions
  3. *
  4. * (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
  5. * (c) 1998, 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
  6. * Copyright 2001 Andi Kleen, SuSE Labs.
  7. *
  8. * Much of the core SMP work is based on previous work by Thomas Radke, to
  9. * whom a great many thanks are extended.
  10. *
  11. * Thanks to Intel for making available several different Pentium,
  12. * Pentium Pro and Pentium-II/Xeon MP machines.
  13. * Original development of Linux SMP code supported by Caldera.
  14. *
  15. * This code is released under the GNU General Public License version 2 or
  16. * later.
  17. *
  18. * Fixes
  19. * Felix Koop : NR_CPUS used properly
  20. * Jose Renau : Handle single CPU case.
  21. * Alan Cox : By repeated request 8) - Total BogoMIPS report.
  22. * Greg Wright : Fix for kernel stacks panic.
  23. * Erich Boleyn : MP v1.4 and additional changes.
  24. * Matthias Sattler : Changes for 2.1 kernel map.
  25. * Michel Lespinasse : Changes for 2.1 kernel map.
  26. * Michael Chastain : Change trampoline.S to gnu as.
  27. * Alan Cox : Dumb bug: 'B' step PPro's are fine
  28. * Ingo Molnar : Added APIC timers, based on code
  29. * from Jose Renau
  30. * Ingo Molnar : various cleanups and rewrites
  31. * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
  32. * Maciej W. Rozycki : Bits for genuine 82489DX APICs
  33. * Andi Kleen : Changed for SMP boot into long mode.
  34. * Martin J. Bligh : Added support for multi-quad systems
  35. * Dave Jones : Report invalid combinations of Athlon CPUs.
  36. * Rusty Russell : Hacked into shape for new "hotplug" boot process.
  37. * Andi Kleen : Converted to new state machine.
  38. * Ashok Raj : CPU hotplug support
  39. * Glauber Costa : i386 and x86_64 integration
  40. */
  41. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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/stackprotector.h>
  55. #include <linux/gfp.h>
  56. #include <linux/cpuidle.h>
  57. #include <asm/acpi.h>
  58. #include <asm/desc.h>
  59. #include <asm/nmi.h>
  60. #include <asm/irq.h>
  61. #include <asm/realmode.h>
  62. #include <asm/cpu.h>
  63. #include <asm/numa.h>
  64. #include <asm/pgtable.h>
  65. #include <asm/tlbflush.h>
  66. #include <asm/mtrr.h>
  67. #include <asm/mwait.h>
  68. #include <asm/apic.h>
  69. #include <asm/io_apic.h>
  70. #include <asm/fpu/internal.h>
  71. #include <asm/setup.h>
  72. #include <asm/uv/uv.h>
  73. #include <linux/mc146818rtc.h>
  74. #include <asm/i8259.h>
  75. #include <asm/misc.h>
  76. #include <asm/qspinlock.h>
  77. #include <asm/intel-family.h>
  78. #include <asm/cpu_device_id.h>
  79. #include <asm/spec-ctrl.h>
  80. #include <asm/hw_irq.h>
  81. /* representing HT siblings of each logical CPU */
  82. DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
  83. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  84. /* representing HT and core siblings of each logical CPU */
  85. DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
  86. EXPORT_PER_CPU_SYMBOL(cpu_core_map);
  87. DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
  88. /* Per CPU bogomips and other parameters */
  89. DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
  90. EXPORT_PER_CPU_SYMBOL(cpu_info);
  91. /* Logical package management. We might want to allocate that dynamically */
  92. unsigned int __max_logical_packages __read_mostly;
  93. EXPORT_SYMBOL(__max_logical_packages);
  94. static unsigned int logical_packages __read_mostly;
  95. /* Maximum number of SMT threads on any online core */
  96. int __read_mostly __max_smt_threads = 1;
  97. /* Flag to indicate if a complete sched domain rebuild is required */
  98. bool x86_topology_update;
  99. int arch_update_cpu_topology(void)
  100. {
  101. int retval = x86_topology_update;
  102. x86_topology_update = false;
  103. return retval;
  104. }
  105. static inline void smpboot_setup_warm_reset_vector(unsigned long start_eip)
  106. {
  107. unsigned long flags;
  108. spin_lock_irqsave(&rtc_lock, flags);
  109. CMOS_WRITE(0xa, 0xf);
  110. spin_unlock_irqrestore(&rtc_lock, flags);
  111. *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_HIGH)) =
  112. start_eip >> 4;
  113. *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) =
  114. start_eip & 0xf;
  115. }
  116. static inline void smpboot_restore_warm_reset_vector(void)
  117. {
  118. unsigned long flags;
  119. /*
  120. * Paranoid: Set warm reset code and vector here back
  121. * to default values.
  122. */
  123. spin_lock_irqsave(&rtc_lock, flags);
  124. CMOS_WRITE(0, 0xf);
  125. spin_unlock_irqrestore(&rtc_lock, flags);
  126. *((volatile u32 *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) = 0;
  127. }
  128. /*
  129. * Report back to the Boot Processor during boot time or to the caller processor
  130. * during CPU online.
  131. */
  132. static void smp_callin(void)
  133. {
  134. int cpuid, phys_id;
  135. /*
  136. * If waken up by an INIT in an 82489DX configuration
  137. * cpu_callout_mask guarantees we don't get here before
  138. * an INIT_deassert IPI reaches our local APIC, so it is
  139. * now safe to touch our local APIC.
  140. */
  141. cpuid = smp_processor_id();
  142. /*
  143. * (This works even if the APIC is not enabled.)
  144. */
  145. phys_id = read_apic_id();
  146. /*
  147. * the boot CPU has finished the init stage and is spinning
  148. * on callin_map until we finish. We are free to set up this
  149. * CPU, first the APIC. (this is probably redundant on most
  150. * boards)
  151. */
  152. apic_ap_setup();
  153. /*
  154. * Save our processor parameters. Note: this information
  155. * is needed for clock calibration.
  156. */
  157. smp_store_cpu_info(cpuid);
  158. /*
  159. * The topology information must be up to date before
  160. * calibrate_delay() and notify_cpu_starting().
  161. */
  162. set_cpu_sibling_map(raw_smp_processor_id());
  163. /*
  164. * Get our bogomips.
  165. * Update loops_per_jiffy in cpu_data. Previous call to
  166. * smp_store_cpu_info() stored a value that is close but not as
  167. * accurate as the value just calculated.
  168. */
  169. calibrate_delay();
  170. cpu_data(cpuid).loops_per_jiffy = loops_per_jiffy;
  171. pr_debug("Stack at about %p\n", &cpuid);
  172. wmb();
  173. notify_cpu_starting(cpuid);
  174. /*
  175. * Allow the master to continue.
  176. */
  177. cpumask_set_cpu(cpuid, cpu_callin_mask);
  178. }
  179. static int cpu0_logical_apicid;
  180. static int enable_start_cpu0;
  181. /*
  182. * Activate a secondary processor.
  183. */
  184. static void notrace start_secondary(void *unused)
  185. {
  186. /*
  187. * Don't put *anything* except direct CPU state initialization
  188. * before cpu_init(), SMP booting is too fragile that we want to
  189. * limit the things done here to the most necessary things.
  190. */
  191. if (boot_cpu_has(X86_FEATURE_PCID))
  192. __write_cr4(__read_cr4() | X86_CR4_PCIDE);
  193. #ifdef CONFIG_X86_32
  194. /* switch away from the initial page table */
  195. load_cr3(swapper_pg_dir);
  196. /*
  197. * Initialize the CR4 shadow before doing anything that could
  198. * try to read it.
  199. */
  200. cr4_init_shadow();
  201. __flush_tlb_all();
  202. #endif
  203. load_current_idt();
  204. cpu_init();
  205. x86_cpuinit.early_percpu_clock_init();
  206. preempt_disable();
  207. smp_callin();
  208. enable_start_cpu0 = 0;
  209. /* otherwise gcc will move up smp_processor_id before the cpu_init */
  210. barrier();
  211. /*
  212. * Check TSC synchronization with the boot CPU:
  213. */
  214. check_tsc_sync_target();
  215. speculative_store_bypass_ht_init();
  216. /*
  217. * Lock vector_lock, set CPU online and bring the vector
  218. * allocator online. Online must be set with vector_lock held
  219. * to prevent a concurrent irq setup/teardown from seeing a
  220. * half valid vector space.
  221. */
  222. lock_vector_lock();
  223. set_cpu_online(smp_processor_id(), true);
  224. lapic_online();
  225. unlock_vector_lock();
  226. cpu_set_state_online(smp_processor_id());
  227. x86_platform.nmi_init();
  228. /* enable local interrupts */
  229. local_irq_enable();
  230. /* to prevent fake stack check failure in clock setup */
  231. boot_init_stack_canary();
  232. x86_cpuinit.setup_percpu_clockev();
  233. wmb();
  234. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  235. }
  236. /**
  237. * topology_is_primary_thread - Check whether CPU is the primary SMT thread
  238. * @cpu: CPU to check
  239. */
  240. bool topology_is_primary_thread(unsigned int cpu)
  241. {
  242. return apic_id_is_primary_thread(per_cpu(x86_cpu_to_apicid, cpu));
  243. }
  244. /**
  245. * topology_smt_supported - Check whether SMT is supported by the CPUs
  246. */
  247. bool topology_smt_supported(void)
  248. {
  249. return smp_num_siblings > 1;
  250. }
  251. /**
  252. * topology_phys_to_logical_pkg - Map a physical package id to a logical
  253. *
  254. * Returns logical package id or -1 if not found
  255. */
  256. int topology_phys_to_logical_pkg(unsigned int phys_pkg)
  257. {
  258. int cpu;
  259. for_each_possible_cpu(cpu) {
  260. struct cpuinfo_x86 *c = &cpu_data(cpu);
  261. if (c->initialized && c->phys_proc_id == phys_pkg)
  262. return c->logical_proc_id;
  263. }
  264. return -1;
  265. }
  266. EXPORT_SYMBOL(topology_phys_to_logical_pkg);
  267. /**
  268. * topology_update_package_map - Update the physical to logical package map
  269. * @pkg: The physical package id as retrieved via CPUID
  270. * @cpu: The cpu for which this is updated
  271. */
  272. int topology_update_package_map(unsigned int pkg, unsigned int cpu)
  273. {
  274. int new;
  275. /* Already available somewhere? */
  276. new = topology_phys_to_logical_pkg(pkg);
  277. if (new >= 0)
  278. goto found;
  279. new = logical_packages++;
  280. if (new != pkg) {
  281. pr_info("CPU %u Converting physical %u to logical package %u\n",
  282. cpu, pkg, new);
  283. }
  284. found:
  285. cpu_data(cpu).logical_proc_id = new;
  286. return 0;
  287. }
  288. void __init smp_store_boot_cpu_info(void)
  289. {
  290. int id = 0; /* CPU 0 */
  291. struct cpuinfo_x86 *c = &cpu_data(id);
  292. *c = boot_cpu_data;
  293. c->cpu_index = id;
  294. topology_update_package_map(c->phys_proc_id, id);
  295. c->initialized = true;
  296. }
  297. /*
  298. * The bootstrap kernel entry code has set these up. Save them for
  299. * a given CPU
  300. */
  301. void smp_store_cpu_info(int id)
  302. {
  303. struct cpuinfo_x86 *c = &cpu_data(id);
  304. /* Copy boot_cpu_data only on the first bringup */
  305. if (!c->initialized)
  306. *c = boot_cpu_data;
  307. c->cpu_index = id;
  308. /*
  309. * During boot time, CPU0 has this setup already. Save the info when
  310. * bringing up AP or offlined CPU0.
  311. */
  312. identify_secondary_cpu(c);
  313. c->initialized = true;
  314. }
  315. static bool
  316. topology_same_node(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
  317. {
  318. int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
  319. return (cpu_to_node(cpu1) == cpu_to_node(cpu2));
  320. }
  321. static bool
  322. topology_sane(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o, const char *name)
  323. {
  324. int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
  325. return !WARN_ONCE(!topology_same_node(c, o),
  326. "sched: CPU #%d's %s-sibling CPU #%d is not on the same node! "
  327. "[node: %d != %d]. Ignoring dependency.\n",
  328. cpu1, name, cpu2, cpu_to_node(cpu1), cpu_to_node(cpu2));
  329. }
  330. #define link_mask(mfunc, c1, c2) \
  331. do { \
  332. cpumask_set_cpu((c1), mfunc(c2)); \
  333. cpumask_set_cpu((c2), mfunc(c1)); \
  334. } while (0)
  335. static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
  336. {
  337. if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
  338. int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
  339. if (c->phys_proc_id == o->phys_proc_id &&
  340. per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2)) {
  341. if (c->cpu_core_id == o->cpu_core_id)
  342. return topology_sane(c, o, "smt");
  343. if ((c->cu_id != 0xff) &&
  344. (o->cu_id != 0xff) &&
  345. (c->cu_id == o->cu_id))
  346. return topology_sane(c, o, "smt");
  347. }
  348. } else if (c->phys_proc_id == o->phys_proc_id &&
  349. c->cpu_core_id == o->cpu_core_id) {
  350. return topology_sane(c, o, "smt");
  351. }
  352. return false;
  353. }
  354. /*
  355. * Define snc_cpu[] for SNC (Sub-NUMA Cluster) CPUs.
  356. *
  357. * These are Intel CPUs that enumerate an LLC that is shared by
  358. * multiple NUMA nodes. The LLC on these systems is shared for
  359. * off-package data access but private to the NUMA node (half
  360. * of the package) for on-package access.
  361. *
  362. * CPUID (the source of the information about the LLC) can only
  363. * enumerate the cache as being shared *or* unshared, but not
  364. * this particular configuration. The CPU in this case enumerates
  365. * the cache to be shared across the entire package (spanning both
  366. * NUMA nodes).
  367. */
  368. static const struct x86_cpu_id snc_cpu[] = {
  369. { X86_VENDOR_INTEL, 6, INTEL_FAM6_SKYLAKE_X },
  370. {}
  371. };
  372. static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
  373. {
  374. int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
  375. /* Do not match if we do not have a valid APICID for cpu: */
  376. if (per_cpu(cpu_llc_id, cpu1) == BAD_APICID)
  377. return false;
  378. /* Do not match if LLC id does not match: */
  379. if (per_cpu(cpu_llc_id, cpu1) != per_cpu(cpu_llc_id, cpu2))
  380. return false;
  381. /*
  382. * Allow the SNC topology without warning. Return of false
  383. * means 'c' does not share the LLC of 'o'. This will be
  384. * reflected to userspace.
  385. */
  386. if (!topology_same_node(c, o) && x86_match_cpu(snc_cpu))
  387. return false;
  388. return topology_sane(c, o, "llc");
  389. }
  390. /*
  391. * Unlike the other levels, we do not enforce keeping a
  392. * multicore group inside a NUMA node. If this happens, we will
  393. * discard the MC level of the topology later.
  394. */
  395. static bool match_die(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
  396. {
  397. if (c->phys_proc_id == o->phys_proc_id)
  398. return true;
  399. return false;
  400. }
  401. #if defined(CONFIG_SCHED_SMT) || defined(CONFIG_SCHED_MC)
  402. static inline int x86_sched_itmt_flags(void)
  403. {
  404. return sysctl_sched_itmt_enabled ? SD_ASYM_PACKING : 0;
  405. }
  406. #ifdef CONFIG_SCHED_MC
  407. static int x86_core_flags(void)
  408. {
  409. return cpu_core_flags() | x86_sched_itmt_flags();
  410. }
  411. #endif
  412. #ifdef CONFIG_SCHED_SMT
  413. static int x86_smt_flags(void)
  414. {
  415. return cpu_smt_flags() | x86_sched_itmt_flags();
  416. }
  417. #endif
  418. #endif
  419. static struct sched_domain_topology_level x86_numa_in_package_topology[] = {
  420. #ifdef CONFIG_SCHED_SMT
  421. { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
  422. #endif
  423. #ifdef CONFIG_SCHED_MC
  424. { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
  425. #endif
  426. { NULL, },
  427. };
  428. static struct sched_domain_topology_level x86_topology[] = {
  429. #ifdef CONFIG_SCHED_SMT
  430. { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
  431. #endif
  432. #ifdef CONFIG_SCHED_MC
  433. { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
  434. #endif
  435. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  436. { NULL, },
  437. };
  438. /*
  439. * Set if a package/die has multiple NUMA nodes inside.
  440. * AMD Magny-Cours, Intel Cluster-on-Die, and Intel
  441. * Sub-NUMA Clustering have this.
  442. */
  443. static bool x86_has_numa_in_package;
  444. void set_cpu_sibling_map(int cpu)
  445. {
  446. bool has_smt = smp_num_siblings > 1;
  447. bool has_mp = has_smt || boot_cpu_data.x86_max_cores > 1;
  448. struct cpuinfo_x86 *c = &cpu_data(cpu);
  449. struct cpuinfo_x86 *o;
  450. int i, threads;
  451. cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
  452. if (!has_mp) {
  453. cpumask_set_cpu(cpu, topology_sibling_cpumask(cpu));
  454. cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
  455. cpumask_set_cpu(cpu, topology_core_cpumask(cpu));
  456. c->booted_cores = 1;
  457. return;
  458. }
  459. for_each_cpu(i, cpu_sibling_setup_mask) {
  460. o = &cpu_data(i);
  461. if ((i == cpu) || (has_smt && match_smt(c, o)))
  462. link_mask(topology_sibling_cpumask, cpu, i);
  463. if ((i == cpu) || (has_mp && match_llc(c, o)))
  464. link_mask(cpu_llc_shared_mask, cpu, i);
  465. }
  466. /*
  467. * This needs a separate iteration over the cpus because we rely on all
  468. * topology_sibling_cpumask links to be set-up.
  469. */
  470. for_each_cpu(i, cpu_sibling_setup_mask) {
  471. o = &cpu_data(i);
  472. if ((i == cpu) || (has_mp && match_die(c, o))) {
  473. link_mask(topology_core_cpumask, cpu, i);
  474. /*
  475. * Does this new cpu bringup a new core?
  476. */
  477. if (cpumask_weight(
  478. topology_sibling_cpumask(cpu)) == 1) {
  479. /*
  480. * for each core in package, increment
  481. * the booted_cores for this new cpu
  482. */
  483. if (cpumask_first(
  484. topology_sibling_cpumask(i)) == i)
  485. c->booted_cores++;
  486. /*
  487. * increment the core count for all
  488. * the other cpus in this package
  489. */
  490. if (i != cpu)
  491. cpu_data(i).booted_cores++;
  492. } else if (i != cpu && !c->booted_cores)
  493. c->booted_cores = cpu_data(i).booted_cores;
  494. }
  495. if (match_die(c, o) && !topology_same_node(c, o))
  496. x86_has_numa_in_package = true;
  497. }
  498. threads = cpumask_weight(topology_sibling_cpumask(cpu));
  499. if (threads > __max_smt_threads)
  500. __max_smt_threads = threads;
  501. }
  502. /* maps the cpu to the sched domain representing multi-core */
  503. const struct cpumask *cpu_coregroup_mask(int cpu)
  504. {
  505. return cpu_llc_shared_mask(cpu);
  506. }
  507. static void impress_friends(void)
  508. {
  509. int cpu;
  510. unsigned long bogosum = 0;
  511. /*
  512. * Allow the user to impress friends.
  513. */
  514. pr_debug("Before bogomips\n");
  515. for_each_possible_cpu(cpu)
  516. if (cpumask_test_cpu(cpu, cpu_callout_mask))
  517. bogosum += cpu_data(cpu).loops_per_jiffy;
  518. pr_info("Total of %d processors activated (%lu.%02lu BogoMIPS)\n",
  519. num_online_cpus(),
  520. bogosum/(500000/HZ),
  521. (bogosum/(5000/HZ))%100);
  522. pr_debug("Before bogocount - setting activated=1\n");
  523. }
  524. void __inquire_remote_apic(int apicid)
  525. {
  526. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  527. const char * const names[] = { "ID", "VERSION", "SPIV" };
  528. int timeout;
  529. u32 status;
  530. pr_info("Inquiring remote APIC 0x%x...\n", apicid);
  531. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  532. pr_info("... APIC 0x%x %s: ", apicid, names[i]);
  533. /*
  534. * Wait for idle.
  535. */
  536. status = safe_apic_wait_icr_idle();
  537. if (status)
  538. pr_cont("a previous APIC delivery may have failed\n");
  539. apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
  540. timeout = 0;
  541. do {
  542. udelay(100);
  543. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  544. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  545. switch (status) {
  546. case APIC_ICR_RR_VALID:
  547. status = apic_read(APIC_RRR);
  548. pr_cont("%08x\n", status);
  549. break;
  550. default:
  551. pr_cont("failed\n");
  552. }
  553. }
  554. }
  555. /*
  556. * The Multiprocessor Specification 1.4 (1997) example code suggests
  557. * that there should be a 10ms delay between the BSP asserting INIT
  558. * and de-asserting INIT, when starting a remote processor.
  559. * But that slows boot and resume on modern processors, which include
  560. * many cores and don't require that delay.
  561. *
  562. * Cmdline "init_cpu_udelay=" is available to over-ride this delay.
  563. * Modern processor families are quirked to remove the delay entirely.
  564. */
  565. #define UDELAY_10MS_DEFAULT 10000
  566. static unsigned int init_udelay = UINT_MAX;
  567. static int __init cpu_init_udelay(char *str)
  568. {
  569. get_option(&str, &init_udelay);
  570. return 0;
  571. }
  572. early_param("cpu_init_udelay", cpu_init_udelay);
  573. static void __init smp_quirk_init_udelay(void)
  574. {
  575. /* if cmdline changed it from default, leave it alone */
  576. if (init_udelay != UINT_MAX)
  577. return;
  578. /* if modern processor, use no delay */
  579. if (((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 6)) ||
  580. ((boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) && (boot_cpu_data.x86 >= 0x18)) ||
  581. ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && (boot_cpu_data.x86 >= 0xF))) {
  582. init_udelay = 0;
  583. return;
  584. }
  585. /* else, use legacy delay */
  586. init_udelay = UDELAY_10MS_DEFAULT;
  587. }
  588. /*
  589. * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
  590. * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
  591. * won't ... remember to clear down the APIC, etc later.
  592. */
  593. int
  594. wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip)
  595. {
  596. unsigned long send_status, accept_status = 0;
  597. int maxlvt;
  598. /* Target chip */
  599. /* Boot on the stack */
  600. /* Kick the second */
  601. apic_icr_write(APIC_DM_NMI | apic->dest_logical, apicid);
  602. pr_debug("Waiting for send to finish...\n");
  603. send_status = safe_apic_wait_icr_idle();
  604. /*
  605. * Give the other CPU some time to accept the IPI.
  606. */
  607. udelay(200);
  608. if (APIC_INTEGRATED(boot_cpu_apic_version)) {
  609. maxlvt = lapic_get_maxlvt();
  610. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  611. apic_write(APIC_ESR, 0);
  612. accept_status = (apic_read(APIC_ESR) & 0xEF);
  613. }
  614. pr_debug("NMI sent\n");
  615. if (send_status)
  616. pr_err("APIC never delivered???\n");
  617. if (accept_status)
  618. pr_err("APIC delivery error (%lx)\n", accept_status);
  619. return (send_status | accept_status);
  620. }
  621. static int
  622. wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
  623. {
  624. unsigned long send_status = 0, accept_status = 0;
  625. int maxlvt, num_starts, j;
  626. maxlvt = lapic_get_maxlvt();
  627. /*
  628. * Be paranoid about clearing APIC errors.
  629. */
  630. if (APIC_INTEGRATED(boot_cpu_apic_version)) {
  631. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  632. apic_write(APIC_ESR, 0);
  633. apic_read(APIC_ESR);
  634. }
  635. pr_debug("Asserting INIT\n");
  636. /*
  637. * Turn INIT on target chip
  638. */
  639. /*
  640. * Send IPI
  641. */
  642. apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
  643. phys_apicid);
  644. pr_debug("Waiting for send to finish...\n");
  645. send_status = safe_apic_wait_icr_idle();
  646. udelay(init_udelay);
  647. pr_debug("Deasserting INIT\n");
  648. /* Target chip */
  649. /* Send IPI */
  650. apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
  651. pr_debug("Waiting for send to finish...\n");
  652. send_status = safe_apic_wait_icr_idle();
  653. mb();
  654. /*
  655. * Should we send STARTUP IPIs ?
  656. *
  657. * Determine this based on the APIC version.
  658. * If we don't have an integrated APIC, don't send the STARTUP IPIs.
  659. */
  660. if (APIC_INTEGRATED(boot_cpu_apic_version))
  661. num_starts = 2;
  662. else
  663. num_starts = 0;
  664. /*
  665. * Run STARTUP IPI loop.
  666. */
  667. pr_debug("#startup loops: %d\n", num_starts);
  668. for (j = 1; j <= num_starts; j++) {
  669. pr_debug("Sending STARTUP #%d\n", j);
  670. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  671. apic_write(APIC_ESR, 0);
  672. apic_read(APIC_ESR);
  673. pr_debug("After apic_write\n");
  674. /*
  675. * STARTUP IPI
  676. */
  677. /* Target chip */
  678. /* Boot on the stack */
  679. /* Kick the second */
  680. apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
  681. phys_apicid);
  682. /*
  683. * Give the other CPU some time to accept the IPI.
  684. */
  685. if (init_udelay == 0)
  686. udelay(10);
  687. else
  688. udelay(300);
  689. pr_debug("Startup point 1\n");
  690. pr_debug("Waiting for send to finish...\n");
  691. send_status = safe_apic_wait_icr_idle();
  692. /*
  693. * Give the other CPU some time to accept the IPI.
  694. */
  695. if (init_udelay == 0)
  696. udelay(10);
  697. else
  698. udelay(200);
  699. if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
  700. apic_write(APIC_ESR, 0);
  701. accept_status = (apic_read(APIC_ESR) & 0xEF);
  702. if (send_status || accept_status)
  703. break;
  704. }
  705. pr_debug("After Startup\n");
  706. if (send_status)
  707. pr_err("APIC never delivered???\n");
  708. if (accept_status)
  709. pr_err("APIC delivery error (%lx)\n", accept_status);
  710. return (send_status | accept_status);
  711. }
  712. /* reduce the number of lines printed when booting a large cpu count system */
  713. static void announce_cpu(int cpu, int apicid)
  714. {
  715. static int current_node = -1;
  716. int node = early_cpu_to_node(cpu);
  717. static int width, node_width;
  718. if (!width)
  719. width = num_digits(num_possible_cpus()) + 1; /* + '#' sign */
  720. if (!node_width)
  721. node_width = num_digits(num_possible_nodes()) + 1; /* + '#' */
  722. if (cpu == 1)
  723. printk(KERN_INFO "x86: Booting SMP configuration:\n");
  724. if (system_state < SYSTEM_RUNNING) {
  725. if (node != current_node) {
  726. if (current_node > (-1))
  727. pr_cont("\n");
  728. current_node = node;
  729. printk(KERN_INFO ".... node %*s#%d, CPUs: ",
  730. node_width - num_digits(node), " ", node);
  731. }
  732. /* Add padding for the BSP */
  733. if (cpu == 1)
  734. pr_cont("%*s", width + 1, " ");
  735. pr_cont("%*s#%d", width - num_digits(cpu), " ", cpu);
  736. } else
  737. pr_info("Booting Node %d Processor %d APIC 0x%x\n",
  738. node, cpu, apicid);
  739. }
  740. static int wakeup_cpu0_nmi(unsigned int cmd, struct pt_regs *regs)
  741. {
  742. int cpu;
  743. cpu = smp_processor_id();
  744. if (cpu == 0 && !cpu_online(cpu) && enable_start_cpu0)
  745. return NMI_HANDLED;
  746. return NMI_DONE;
  747. }
  748. /*
  749. * Wake up AP by INIT, INIT, STARTUP sequence.
  750. *
  751. * Instead of waiting for STARTUP after INITs, BSP will execute the BIOS
  752. * boot-strap code which is not a desired behavior for waking up BSP. To
  753. * void the boot-strap code, wake up CPU0 by NMI instead.
  754. *
  755. * This works to wake up soft offlined CPU0 only. If CPU0 is hard offlined
  756. * (i.e. physically hot removed and then hot added), NMI won't wake it up.
  757. * We'll change this code in the future to wake up hard offlined CPU0 if
  758. * real platform and request are available.
  759. */
  760. static int
  761. wakeup_cpu_via_init_nmi(int cpu, unsigned long start_ip, int apicid,
  762. int *cpu0_nmi_registered)
  763. {
  764. int id;
  765. int boot_error;
  766. preempt_disable();
  767. /*
  768. * Wake up AP by INIT, INIT, STARTUP sequence.
  769. */
  770. if (cpu) {
  771. boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
  772. goto out;
  773. }
  774. /*
  775. * Wake up BSP by nmi.
  776. *
  777. * Register a NMI handler to help wake up CPU0.
  778. */
  779. boot_error = register_nmi_handler(NMI_LOCAL,
  780. wakeup_cpu0_nmi, 0, "wake_cpu0");
  781. if (!boot_error) {
  782. enable_start_cpu0 = 1;
  783. *cpu0_nmi_registered = 1;
  784. if (apic->dest_logical == APIC_DEST_LOGICAL)
  785. id = cpu0_logical_apicid;
  786. else
  787. id = apicid;
  788. boot_error = wakeup_secondary_cpu_via_nmi(id, start_ip);
  789. }
  790. out:
  791. preempt_enable();
  792. return boot_error;
  793. }
  794. void common_cpu_up(unsigned int cpu, struct task_struct *idle)
  795. {
  796. /* Just in case we booted with a single CPU. */
  797. alternatives_enable_smp();
  798. per_cpu(current_task, cpu) = idle;
  799. #ifdef CONFIG_X86_32
  800. /* Stack for startup_32 can be just as for start_secondary onwards */
  801. irq_ctx_init(cpu);
  802. per_cpu(cpu_current_top_of_stack, cpu) = task_top_of_stack(idle);
  803. #else
  804. initial_gs = per_cpu_offset(cpu);
  805. #endif
  806. }
  807. /*
  808. * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
  809. * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
  810. * Returns zero if CPU booted OK, else error code from
  811. * ->wakeup_secondary_cpu.
  812. */
  813. static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,
  814. int *cpu0_nmi_registered)
  815. {
  816. volatile u32 *trampoline_status =
  817. (volatile u32 *) __va(real_mode_header->trampoline_status);
  818. /* start_ip had better be page-aligned! */
  819. unsigned long start_ip = real_mode_header->trampoline_start;
  820. unsigned long boot_error = 0;
  821. unsigned long timeout;
  822. idle->thread.sp = (unsigned long)task_pt_regs(idle);
  823. early_gdt_descr.address = (unsigned long)get_cpu_gdt_rw(cpu);
  824. initial_code = (unsigned long)start_secondary;
  825. initial_stack = idle->thread.sp;
  826. /* Enable the espfix hack for this CPU */
  827. init_espfix_ap(cpu);
  828. /* So we see what's up */
  829. announce_cpu(cpu, apicid);
  830. /*
  831. * This grunge runs the startup process for
  832. * the targeted processor.
  833. */
  834. if (x86_platform.legacy.warm_reset) {
  835. pr_debug("Setting warm reset code and vector.\n");
  836. smpboot_setup_warm_reset_vector(start_ip);
  837. /*
  838. * Be paranoid about clearing APIC errors.
  839. */
  840. if (APIC_INTEGRATED(boot_cpu_apic_version)) {
  841. apic_write(APIC_ESR, 0);
  842. apic_read(APIC_ESR);
  843. }
  844. }
  845. /*
  846. * AP might wait on cpu_callout_mask in cpu_init() with
  847. * cpu_initialized_mask set if previous attempt to online
  848. * it timed-out. Clear cpu_initialized_mask so that after
  849. * INIT/SIPI it could start with a clean state.
  850. */
  851. cpumask_clear_cpu(cpu, cpu_initialized_mask);
  852. smp_mb();
  853. /*
  854. * Wake up a CPU in difference cases:
  855. * - Use the method in the APIC driver if it's defined
  856. * Otherwise,
  857. * - Use an INIT boot APIC message for APs or NMI for BSP.
  858. */
  859. if (apic->wakeup_secondary_cpu)
  860. boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
  861. else
  862. boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
  863. cpu0_nmi_registered);
  864. if (!boot_error) {
  865. /*
  866. * Wait 10s total for first sign of life from AP
  867. */
  868. boot_error = -1;
  869. timeout = jiffies + 10*HZ;
  870. while (time_before(jiffies, timeout)) {
  871. if (cpumask_test_cpu(cpu, cpu_initialized_mask)) {
  872. /*
  873. * Tell AP to proceed with initialization
  874. */
  875. cpumask_set_cpu(cpu, cpu_callout_mask);
  876. boot_error = 0;
  877. break;
  878. }
  879. schedule();
  880. }
  881. }
  882. if (!boot_error) {
  883. /*
  884. * Wait till AP completes initial initialization
  885. */
  886. while (!cpumask_test_cpu(cpu, cpu_callin_mask)) {
  887. /*
  888. * Allow other tasks to run while we wait for the
  889. * AP to come online. This also gives a chance
  890. * for the MTRR work(triggered by the AP coming online)
  891. * to be completed in the stop machine context.
  892. */
  893. schedule();
  894. }
  895. }
  896. /* mark "stuck" area as not stuck */
  897. *trampoline_status = 0;
  898. if (x86_platform.legacy.warm_reset) {
  899. /*
  900. * Cleanup possible dangling ends...
  901. */
  902. smpboot_restore_warm_reset_vector();
  903. }
  904. return boot_error;
  905. }
  906. int native_cpu_up(unsigned int cpu, struct task_struct *tidle)
  907. {
  908. int apicid = apic->cpu_present_to_apicid(cpu);
  909. int cpu0_nmi_registered = 0;
  910. unsigned long flags;
  911. int err, ret = 0;
  912. lockdep_assert_irqs_enabled();
  913. pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  914. if (apicid == BAD_APICID ||
  915. !physid_isset(apicid, phys_cpu_present_map) ||
  916. !apic->apic_id_valid(apicid)) {
  917. pr_err("%s: bad cpu %d\n", __func__, cpu);
  918. return -EINVAL;
  919. }
  920. /*
  921. * Already booted CPU?
  922. */
  923. if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
  924. pr_debug("do_boot_cpu %d Already started\n", cpu);
  925. return -ENOSYS;
  926. }
  927. /*
  928. * Save current MTRR state in case it was changed since early boot
  929. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  930. */
  931. mtrr_save_state();
  932. /* x86 CPUs take themselves offline, so delayed offline is OK. */
  933. err = cpu_check_up_prepare(cpu);
  934. if (err && err != -EBUSY)
  935. return err;
  936. /* the FPU context is blank, nobody can own it */
  937. per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
  938. common_cpu_up(cpu, tidle);
  939. err = do_boot_cpu(apicid, cpu, tidle, &cpu0_nmi_registered);
  940. if (err) {
  941. pr_err("do_boot_cpu failed(%d) to wakeup CPU#%u\n", err, cpu);
  942. ret = -EIO;
  943. goto unreg_nmi;
  944. }
  945. /*
  946. * Check TSC synchronization with the AP (keep irqs disabled
  947. * while doing so):
  948. */
  949. local_irq_save(flags);
  950. check_tsc_sync_source(cpu);
  951. local_irq_restore(flags);
  952. while (!cpu_online(cpu)) {
  953. cpu_relax();
  954. touch_nmi_watchdog();
  955. }
  956. unreg_nmi:
  957. /*
  958. * Clean up the nmi handler. Do this after the callin and callout sync
  959. * to avoid impact of possible long unregister time.
  960. */
  961. if (cpu0_nmi_registered)
  962. unregister_nmi_handler(NMI_LOCAL, "wake_cpu0");
  963. return ret;
  964. }
  965. /**
  966. * arch_disable_smp_support() - disables SMP support for x86 at runtime
  967. */
  968. void arch_disable_smp_support(void)
  969. {
  970. disable_ioapic_support();
  971. }
  972. /*
  973. * Fall back to non SMP mode after errors.
  974. *
  975. * RED-PEN audit/test this more. I bet there is more state messed up here.
  976. */
  977. static __init void disable_smp(void)
  978. {
  979. pr_info("SMP disabled\n");
  980. disable_ioapic_support();
  981. init_cpu_present(cpumask_of(0));
  982. init_cpu_possible(cpumask_of(0));
  983. if (smp_found_config)
  984. physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
  985. else
  986. physid_set_mask_of_physid(0, &phys_cpu_present_map);
  987. cpumask_set_cpu(0, topology_sibling_cpumask(0));
  988. cpumask_set_cpu(0, topology_core_cpumask(0));
  989. }
  990. /*
  991. * Various sanity checks.
  992. */
  993. static void __init smp_sanity_check(void)
  994. {
  995. preempt_disable();
  996. #if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
  997. if (def_to_bigsmp && nr_cpu_ids > 8) {
  998. unsigned int cpu;
  999. unsigned nr;
  1000. pr_warn("More than 8 CPUs detected - skipping them\n"
  1001. "Use CONFIG_X86_BIGSMP\n");
  1002. nr = 0;
  1003. for_each_present_cpu(cpu) {
  1004. if (nr >= 8)
  1005. set_cpu_present(cpu, false);
  1006. nr++;
  1007. }
  1008. nr = 0;
  1009. for_each_possible_cpu(cpu) {
  1010. if (nr >= 8)
  1011. set_cpu_possible(cpu, false);
  1012. nr++;
  1013. }
  1014. nr_cpu_ids = 8;
  1015. }
  1016. #endif
  1017. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  1018. pr_warn("weird, boot CPU (#%d) not listed by the BIOS\n",
  1019. hard_smp_processor_id());
  1020. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  1021. }
  1022. /*
  1023. * Should not be necessary because the MP table should list the boot
  1024. * CPU too, but we do it for the sake of robustness anyway.
  1025. */
  1026. if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
  1027. pr_notice("weird, boot CPU (#%d) not listed by the BIOS\n",
  1028. boot_cpu_physical_apicid);
  1029. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  1030. }
  1031. preempt_enable();
  1032. }
  1033. static void __init smp_cpu_index_default(void)
  1034. {
  1035. int i;
  1036. struct cpuinfo_x86 *c;
  1037. for_each_possible_cpu(i) {
  1038. c = &cpu_data(i);
  1039. /* mark all to hotplug */
  1040. c->cpu_index = nr_cpu_ids;
  1041. }
  1042. }
  1043. static void __init smp_get_logical_apicid(void)
  1044. {
  1045. if (x2apic_mode)
  1046. cpu0_logical_apicid = apic_read(APIC_LDR);
  1047. else
  1048. cpu0_logical_apicid = GET_APIC_LOGICAL_ID(apic_read(APIC_LDR));
  1049. }
  1050. /*
  1051. * Prepare for SMP bootup.
  1052. * @max_cpus: configured maximum number of CPUs, It is a legacy parameter
  1053. * for common interface support.
  1054. */
  1055. void __init native_smp_prepare_cpus(unsigned int max_cpus)
  1056. {
  1057. unsigned int i;
  1058. smp_cpu_index_default();
  1059. /*
  1060. * Setup boot CPU information
  1061. */
  1062. smp_store_boot_cpu_info(); /* Final full version of the data */
  1063. cpumask_copy(cpu_callin_mask, cpumask_of(0));
  1064. mb();
  1065. for_each_possible_cpu(i) {
  1066. zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
  1067. zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
  1068. zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
  1069. }
  1070. /*
  1071. * Set 'default' x86 topology, this matches default_topology() in that
  1072. * it has NUMA nodes as a topology level. See also
  1073. * native_smp_cpus_done().
  1074. *
  1075. * Must be done before set_cpus_sibling_map() is ran.
  1076. */
  1077. set_sched_topology(x86_topology);
  1078. set_cpu_sibling_map(0);
  1079. smp_sanity_check();
  1080. switch (apic_intr_mode) {
  1081. case APIC_PIC:
  1082. case APIC_VIRTUAL_WIRE_NO_CONFIG:
  1083. disable_smp();
  1084. return;
  1085. case APIC_SYMMETRIC_IO_NO_ROUTING:
  1086. disable_smp();
  1087. /* Setup local timer */
  1088. x86_init.timers.setup_percpu_clockev();
  1089. return;
  1090. case APIC_VIRTUAL_WIRE:
  1091. case APIC_SYMMETRIC_IO:
  1092. break;
  1093. }
  1094. /* Setup local timer */
  1095. x86_init.timers.setup_percpu_clockev();
  1096. smp_get_logical_apicid();
  1097. pr_info("CPU0: ");
  1098. print_cpu_info(&cpu_data(0));
  1099. native_pv_lock_init();
  1100. uv_system_init();
  1101. set_mtrr_aps_delayed_init();
  1102. smp_quirk_init_udelay();
  1103. speculative_store_bypass_ht_init();
  1104. }
  1105. void arch_enable_nonboot_cpus_begin(void)
  1106. {
  1107. set_mtrr_aps_delayed_init();
  1108. }
  1109. void arch_enable_nonboot_cpus_end(void)
  1110. {
  1111. mtrr_aps_init();
  1112. }
  1113. /*
  1114. * Early setup to make printk work.
  1115. */
  1116. void __init native_smp_prepare_boot_cpu(void)
  1117. {
  1118. int me = smp_processor_id();
  1119. switch_to_new_gdt(me);
  1120. /* already set me in cpu_online_mask in boot_cpu_init() */
  1121. cpumask_set_cpu(me, cpu_callout_mask);
  1122. cpu_set_state_online(me);
  1123. }
  1124. void __init calculate_max_logical_packages(void)
  1125. {
  1126. int ncpus;
  1127. /*
  1128. * Today neither Intel nor AMD support heterogenous systems so
  1129. * extrapolate the boot cpu's data to all packages.
  1130. */
  1131. ncpus = cpu_data(0).booted_cores * topology_max_smt_threads();
  1132. __max_logical_packages = DIV_ROUND_UP(nr_cpu_ids, ncpus);
  1133. pr_info("Max logical packages: %u\n", __max_logical_packages);
  1134. }
  1135. void __init native_smp_cpus_done(unsigned int max_cpus)
  1136. {
  1137. pr_debug("Boot done\n");
  1138. calculate_max_logical_packages();
  1139. if (x86_has_numa_in_package)
  1140. set_sched_topology(x86_numa_in_package_topology);
  1141. nmi_selftest();
  1142. impress_friends();
  1143. mtrr_aps_init();
  1144. }
  1145. static int __initdata setup_possible_cpus = -1;
  1146. static int __init _setup_possible_cpus(char *str)
  1147. {
  1148. get_option(&str, &setup_possible_cpus);
  1149. return 0;
  1150. }
  1151. early_param("possible_cpus", _setup_possible_cpus);
  1152. /*
  1153. * cpu_possible_mask should be static, it cannot change as cpu's
  1154. * are onlined, or offlined. The reason is per-cpu data-structures
  1155. * are allocated by some modules at init time, and dont expect to
  1156. * do this dynamically on cpu arrival/departure.
  1157. * cpu_present_mask on the other hand can change dynamically.
  1158. * In case when cpu_hotplug is not compiled, then we resort to current
  1159. * behaviour, which is cpu_possible == cpu_present.
  1160. * - Ashok Raj
  1161. *
  1162. * Three ways to find out the number of additional hotplug CPUs:
  1163. * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
  1164. * - The user can overwrite it with possible_cpus=NUM
  1165. * - Otherwise don't reserve additional CPUs.
  1166. * We do this because additional CPUs waste a lot of memory.
  1167. * -AK
  1168. */
  1169. __init void prefill_possible_map(void)
  1170. {
  1171. int i, possible;
  1172. /* No boot processor was found in mptable or ACPI MADT */
  1173. if (!num_processors) {
  1174. if (boot_cpu_has(X86_FEATURE_APIC)) {
  1175. int apicid = boot_cpu_physical_apicid;
  1176. int cpu = hard_smp_processor_id();
  1177. pr_warn("Boot CPU (id %d) not listed by BIOS\n", cpu);
  1178. /* Make sure boot cpu is enumerated */
  1179. if (apic->cpu_present_to_apicid(0) == BAD_APICID &&
  1180. apic->apic_id_valid(apicid))
  1181. generic_processor_info(apicid, boot_cpu_apic_version);
  1182. }
  1183. if (!num_processors)
  1184. num_processors = 1;
  1185. }
  1186. i = setup_max_cpus ?: 1;
  1187. if (setup_possible_cpus == -1) {
  1188. possible = num_processors;
  1189. #ifdef CONFIG_HOTPLUG_CPU
  1190. if (setup_max_cpus)
  1191. possible += disabled_cpus;
  1192. #else
  1193. if (possible > i)
  1194. possible = i;
  1195. #endif
  1196. } else
  1197. possible = setup_possible_cpus;
  1198. total_cpus = max_t(int, possible, num_processors + disabled_cpus);
  1199. /* nr_cpu_ids could be reduced via nr_cpus= */
  1200. if (possible > nr_cpu_ids) {
  1201. pr_warn("%d Processors exceeds NR_CPUS limit of %u\n",
  1202. possible, nr_cpu_ids);
  1203. possible = nr_cpu_ids;
  1204. }
  1205. #ifdef CONFIG_HOTPLUG_CPU
  1206. if (!setup_max_cpus)
  1207. #endif
  1208. if (possible > i) {
  1209. pr_warn("%d Processors exceeds max_cpus limit of %u\n",
  1210. possible, setup_max_cpus);
  1211. possible = i;
  1212. }
  1213. nr_cpu_ids = possible;
  1214. pr_info("Allowing %d CPUs, %d hotplug CPUs\n",
  1215. possible, max_t(int, possible - num_processors, 0));
  1216. reset_cpu_possible_mask();
  1217. for (i = 0; i < possible; i++)
  1218. set_cpu_possible(i, true);
  1219. }
  1220. #ifdef CONFIG_HOTPLUG_CPU
  1221. /* Recompute SMT state for all CPUs on offline */
  1222. static void recompute_smt_state(void)
  1223. {
  1224. int max_threads, cpu;
  1225. max_threads = 0;
  1226. for_each_online_cpu (cpu) {
  1227. int threads = cpumask_weight(topology_sibling_cpumask(cpu));
  1228. if (threads > max_threads)
  1229. max_threads = threads;
  1230. }
  1231. __max_smt_threads = max_threads;
  1232. }
  1233. static void remove_siblinginfo(int cpu)
  1234. {
  1235. int sibling;
  1236. struct cpuinfo_x86 *c = &cpu_data(cpu);
  1237. for_each_cpu(sibling, topology_core_cpumask(cpu)) {
  1238. cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
  1239. /*/
  1240. * last thread sibling in this cpu core going down
  1241. */
  1242. if (cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
  1243. cpu_data(sibling).booted_cores--;
  1244. }
  1245. for_each_cpu(sibling, topology_sibling_cpumask(cpu))
  1246. cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
  1247. for_each_cpu(sibling, cpu_llc_shared_mask(cpu))
  1248. cpumask_clear_cpu(cpu, cpu_llc_shared_mask(sibling));
  1249. cpumask_clear(cpu_llc_shared_mask(cpu));
  1250. cpumask_clear(topology_sibling_cpumask(cpu));
  1251. cpumask_clear(topology_core_cpumask(cpu));
  1252. c->cpu_core_id = 0;
  1253. c->booted_cores = 0;
  1254. cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
  1255. recompute_smt_state();
  1256. }
  1257. static void remove_cpu_from_maps(int cpu)
  1258. {
  1259. set_cpu_online(cpu, false);
  1260. cpumask_clear_cpu(cpu, cpu_callout_mask);
  1261. cpumask_clear_cpu(cpu, cpu_callin_mask);
  1262. /* was set by cpu_init() */
  1263. cpumask_clear_cpu(cpu, cpu_initialized_mask);
  1264. numa_remove_cpu(cpu);
  1265. }
  1266. void cpu_disable_common(void)
  1267. {
  1268. int cpu = smp_processor_id();
  1269. remove_siblinginfo(cpu);
  1270. /* It's now safe to remove this processor from the online map */
  1271. lock_vector_lock();
  1272. remove_cpu_from_maps(cpu);
  1273. unlock_vector_lock();
  1274. fixup_irqs();
  1275. lapic_offline();
  1276. }
  1277. int native_cpu_disable(void)
  1278. {
  1279. int ret;
  1280. ret = lapic_can_unplug_cpu();
  1281. if (ret)
  1282. return ret;
  1283. clear_local_APIC();
  1284. cpu_disable_common();
  1285. return 0;
  1286. }
  1287. int common_cpu_die(unsigned int cpu)
  1288. {
  1289. int ret = 0;
  1290. /* We don't do anything here: idle task is faking death itself. */
  1291. /* They ack this in play_dead() by setting CPU_DEAD */
  1292. if (cpu_wait_death(cpu, 5)) {
  1293. if (system_state == SYSTEM_RUNNING)
  1294. pr_info("CPU %u is now offline\n", cpu);
  1295. } else {
  1296. pr_err("CPU %u didn't die...\n", cpu);
  1297. ret = -1;
  1298. }
  1299. return ret;
  1300. }
  1301. void native_cpu_die(unsigned int cpu)
  1302. {
  1303. common_cpu_die(cpu);
  1304. }
  1305. void play_dead_common(void)
  1306. {
  1307. idle_task_exit();
  1308. /* Ack it */
  1309. (void)cpu_report_death();
  1310. /*
  1311. * With physical CPU hotplug, we should halt the cpu
  1312. */
  1313. local_irq_disable();
  1314. }
  1315. static bool wakeup_cpu0(void)
  1316. {
  1317. if (smp_processor_id() == 0 && enable_start_cpu0)
  1318. return true;
  1319. return false;
  1320. }
  1321. /*
  1322. * We need to flush the caches before going to sleep, lest we have
  1323. * dirty data in our caches when we come back up.
  1324. */
  1325. static inline void mwait_play_dead(void)
  1326. {
  1327. unsigned int eax, ebx, ecx, edx;
  1328. unsigned int highest_cstate = 0;
  1329. unsigned int highest_subcstate = 0;
  1330. void *mwait_ptr;
  1331. int i;
  1332. if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
  1333. boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
  1334. return;
  1335. if (!this_cpu_has(X86_FEATURE_MWAIT))
  1336. return;
  1337. if (!this_cpu_has(X86_FEATURE_CLFLUSH))
  1338. return;
  1339. if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
  1340. return;
  1341. eax = CPUID_MWAIT_LEAF;
  1342. ecx = 0;
  1343. native_cpuid(&eax, &ebx, &ecx, &edx);
  1344. /*
  1345. * eax will be 0 if EDX enumeration is not valid.
  1346. * Initialized below to cstate, sub_cstate value when EDX is valid.
  1347. */
  1348. if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
  1349. eax = 0;
  1350. } else {
  1351. edx >>= MWAIT_SUBSTATE_SIZE;
  1352. for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
  1353. if (edx & MWAIT_SUBSTATE_MASK) {
  1354. highest_cstate = i;
  1355. highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
  1356. }
  1357. }
  1358. eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
  1359. (highest_subcstate - 1);
  1360. }
  1361. /*
  1362. * This should be a memory location in a cache line which is
  1363. * unlikely to be touched by other processors. The actual
  1364. * content is immaterial as it is not actually modified in any way.
  1365. */
  1366. mwait_ptr = &current_thread_info()->flags;
  1367. wbinvd();
  1368. while (1) {
  1369. /*
  1370. * The CLFLUSH is a workaround for erratum AAI65 for
  1371. * the Xeon 7400 series. It's not clear it is actually
  1372. * needed, but it should be harmless in either case.
  1373. * The WBINVD is insufficient due to the spurious-wakeup
  1374. * case where we return around the loop.
  1375. */
  1376. mb();
  1377. clflush(mwait_ptr);
  1378. mb();
  1379. __monitor(mwait_ptr, 0, 0);
  1380. mb();
  1381. __mwait(eax, 0);
  1382. /*
  1383. * If NMI wants to wake up CPU0, start CPU0.
  1384. */
  1385. if (wakeup_cpu0())
  1386. start_cpu0();
  1387. }
  1388. }
  1389. void hlt_play_dead(void)
  1390. {
  1391. if (__this_cpu_read(cpu_info.x86) >= 4)
  1392. wbinvd();
  1393. while (1) {
  1394. native_halt();
  1395. /*
  1396. * If NMI wants to wake up CPU0, start CPU0.
  1397. */
  1398. if (wakeup_cpu0())
  1399. start_cpu0();
  1400. }
  1401. }
  1402. void native_play_dead(void)
  1403. {
  1404. play_dead_common();
  1405. tboot_shutdown(TB_SHUTDOWN_WFS);
  1406. mwait_play_dead(); /* Only returns on failure */
  1407. if (cpuidle_play_dead())
  1408. hlt_play_dead();
  1409. }
  1410. #else /* ... !CONFIG_HOTPLUG_CPU */
  1411. int native_cpu_disable(void)
  1412. {
  1413. return -ENOSYS;
  1414. }
  1415. void native_cpu_die(unsigned int cpu)
  1416. {
  1417. /* We said "no" in __cpu_disable */
  1418. BUG();
  1419. }
  1420. void native_play_dead(void)
  1421. {
  1422. BUG();
  1423. }
  1424. #endif