smpboot.c 36 KB

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