smpboot.c 41 KB

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