smpboot.c 35 KB

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