smpboot.c 36 KB

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