smpboot.c 34 KB

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