smpboot.c 36 KB

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