smpboot.c 36 KB

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