smpboot.c 37 KB

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