smpboot.c 36 KB

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