smpboot.c 36 KB

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