smpboot.c 37 KB

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