smpboot.c 35 KB

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