smpboot.c 37 KB

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