smpboot.c 42 KB

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