smpboot.c 36 KB

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