smpboot.c 37 KB

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