smpboot.c 41 KB

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