smpboot.c 41 KB

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