smpboot.c 36 KB

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