smpboot.c 39 KB

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