smpboot.c 42 KB

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