smpboot_32.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. *
  7. * Much of the core SMP work is based on previous work by Thomas Radke, to
  8. * whom a great many thanks are extended.
  9. *
  10. * Thanks to Intel for making available several different Pentium,
  11. * Pentium Pro and Pentium-II/Xeon MP machines.
  12. * Original development of Linux SMP code supported by Caldera.
  13. *
  14. * This code is released under the GNU General Public License version 2 or
  15. * later.
  16. *
  17. * Fixes
  18. * Felix Koop : NR_CPUS used properly
  19. * Jose Renau : Handle single CPU case.
  20. * Alan Cox : By repeated request 8) - Total BogoMIPS report.
  21. * Greg Wright : Fix for kernel stacks panic.
  22. * Erich Boleyn : MP v1.4 and additional changes.
  23. * Matthias Sattler : Changes for 2.1 kernel map.
  24. * Michel Lespinasse : Changes for 2.1 kernel map.
  25. * Michael Chastain : Change trampoline.S to gnu as.
  26. * Alan Cox : Dumb bug: 'B' step PPro's are fine
  27. * Ingo Molnar : Added APIC timers, based on code
  28. * from Jose Renau
  29. * Ingo Molnar : various cleanups and rewrites
  30. * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
  31. * Maciej W. Rozycki : Bits for genuine 82489DX APICs
  32. * Martin J. Bligh : Added support for multi-quad systems
  33. * Dave Jones : Report invalid combinations of Athlon CPUs.
  34. * Rusty Russell : Hacked into shape for new "hotplug" boot process. */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/kernel.h>
  38. #include <linux/mm.h>
  39. #include <linux/sched.h>
  40. #include <linux/kernel_stat.h>
  41. #include <linux/bootmem.h>
  42. #include <linux/notifier.h>
  43. #include <linux/cpu.h>
  44. #include <linux/percpu.h>
  45. #include <linux/nmi.h>
  46. #include <linux/delay.h>
  47. #include <linux/mc146818rtc.h>
  48. #include <asm/tlbflush.h>
  49. #include <asm/desc.h>
  50. #include <asm/arch_hooks.h>
  51. #include <asm/nmi.h>
  52. #include <mach_apic.h>
  53. #include <mach_wakecpu.h>
  54. #include <smpboot_hooks.h>
  55. #include <asm/vmi.h>
  56. #include <asm/mtrr.h>
  57. /* Set if we find a B stepping CPU */
  58. static int __cpuinitdata smp_b_stepping;
  59. static cpumask_t smp_commenced_mask;
  60. /* which logical CPU number maps to which CPU (physical APIC ID) */
  61. u16 x86_cpu_to_apicid_init[NR_CPUS] __initdata =
  62. { [0 ... NR_CPUS-1] = BAD_APICID };
  63. void *x86_cpu_to_apicid_early_ptr;
  64. DEFINE_PER_CPU(u16, x86_cpu_to_apicid) = BAD_APICID;
  65. EXPORT_PER_CPU_SYMBOL(x86_cpu_to_apicid);
  66. u8 apicid_2_node[MAX_APICID];
  67. /*
  68. * Trampoline 80x86 program as an array.
  69. */
  70. extern const unsigned char trampoline_data [];
  71. extern const unsigned char trampoline_end [];
  72. static unsigned char *trampoline_base;
  73. static void map_cpu_to_logical_apicid(void);
  74. /* State of each CPU. */
  75. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  76. /*
  77. * Currently trivial. Write the real->protected mode
  78. * bootstrap into the page concerned. The caller
  79. * has made sure it's suitably aligned.
  80. */
  81. static unsigned long __cpuinit setup_trampoline(void)
  82. {
  83. memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
  84. return virt_to_phys(trampoline_base);
  85. }
  86. /*
  87. * We are called very early to get the low memory for the
  88. * SMP bootup trampoline page.
  89. */
  90. void __init smp_alloc_memory(void)
  91. {
  92. trampoline_base = alloc_bootmem_low_pages(PAGE_SIZE);
  93. /*
  94. * Has to be in very low memory so we can execute
  95. * real-mode AP code.
  96. */
  97. if (__pa(trampoline_base) >= 0x9F000)
  98. BUG();
  99. }
  100. /*
  101. * The bootstrap kernel entry code has set these up. Save them for
  102. * a given CPU
  103. */
  104. void __cpuinit smp_store_cpu_info(int id)
  105. {
  106. struct cpuinfo_x86 *c = &cpu_data(id);
  107. *c = boot_cpu_data;
  108. c->cpu_index = id;
  109. if (id!=0)
  110. identify_secondary_cpu(c);
  111. /*
  112. * Mask B, Pentium, but not Pentium MMX
  113. */
  114. if (c->x86_vendor == X86_VENDOR_INTEL &&
  115. c->x86 == 5 &&
  116. c->x86_mask >= 1 && c->x86_mask <= 4 &&
  117. c->x86_model <= 3)
  118. /*
  119. * Remember we have B step Pentia with bugs
  120. */
  121. smp_b_stepping = 1;
  122. /*
  123. * Certain Athlons might work (for various values of 'work') in SMP
  124. * but they are not certified as MP capable.
  125. */
  126. if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
  127. if (num_possible_cpus() == 1)
  128. goto valid_k7;
  129. /* Athlon 660/661 is valid. */
  130. if ((c->x86_model==6) && ((c->x86_mask==0) || (c->x86_mask==1)))
  131. goto valid_k7;
  132. /* Duron 670 is valid */
  133. if ((c->x86_model==7) && (c->x86_mask==0))
  134. goto valid_k7;
  135. /*
  136. * Athlon 662, Duron 671, and Athlon >model 7 have capability bit.
  137. * It's worth noting that the A5 stepping (662) of some Athlon XP's
  138. * have the MP bit set.
  139. * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for more.
  140. */
  141. if (((c->x86_model==6) && (c->x86_mask>=2)) ||
  142. ((c->x86_model==7) && (c->x86_mask>=1)) ||
  143. (c->x86_model> 7))
  144. if (cpu_has_mp)
  145. goto valid_k7;
  146. /* If we get here, it's not a certified SMP capable AMD system. */
  147. add_taint(TAINT_UNSAFE_SMP);
  148. }
  149. valid_k7:
  150. ;
  151. }
  152. static atomic_t init_deasserted;
  153. static void __cpuinit smp_callin(void)
  154. {
  155. int cpuid, phys_id;
  156. unsigned long timeout;
  157. /*
  158. * If waken up by an INIT in an 82489DX configuration
  159. * we may get here before an INIT-deassert IPI reaches
  160. * our local APIC. We have to wait for the IPI or we'll
  161. * lock up on an APIC access.
  162. */
  163. wait_for_init_deassert(&init_deasserted);
  164. /*
  165. * (This works even if the APIC is not enabled.)
  166. */
  167. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  168. cpuid = smp_processor_id();
  169. if (cpu_isset(cpuid, cpu_callin_map)) {
  170. printk("huh, phys CPU#%d, CPU#%d already present??\n",
  171. phys_id, cpuid);
  172. BUG();
  173. }
  174. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  175. /*
  176. * STARTUP IPIs are fragile beasts as they might sometimes
  177. * trigger some glue motherboard logic. Complete APIC bus
  178. * silence for 1 second, this overestimates the time the
  179. * boot CPU is spending to send the up to 2 STARTUP IPIs
  180. * by a factor of two. This should be enough.
  181. */
  182. /*
  183. * Waiting 2s total for startup (udelay is not yet working)
  184. */
  185. timeout = jiffies + 2*HZ;
  186. while (time_before(jiffies, timeout)) {
  187. /*
  188. * Has the boot CPU finished it's STARTUP sequence?
  189. */
  190. if (cpu_isset(cpuid, cpu_callout_map))
  191. break;
  192. rep_nop();
  193. }
  194. if (!time_before(jiffies, timeout)) {
  195. printk("BUG: CPU%d started up but did not get a callout!\n",
  196. cpuid);
  197. BUG();
  198. }
  199. /*
  200. * the boot CPU has finished the init stage and is spinning
  201. * on callin_map until we finish. We are free to set up this
  202. * CPU, first the APIC. (this is probably redundant on most
  203. * boards)
  204. */
  205. Dprintk("CALLIN, before setup_local_APIC().\n");
  206. smp_callin_clear_local_apic();
  207. setup_local_APIC();
  208. map_cpu_to_logical_apicid();
  209. /*
  210. * Get our bogomips.
  211. */
  212. calibrate_delay();
  213. Dprintk("Stack at about %p\n",&cpuid);
  214. /*
  215. * Save our processor parameters
  216. */
  217. smp_store_cpu_info(cpuid);
  218. /*
  219. * Allow the master to continue.
  220. */
  221. cpu_set(cpuid, cpu_callin_map);
  222. }
  223. static int cpucount;
  224. /*
  225. * Activate a secondary processor.
  226. */
  227. static void __cpuinit start_secondary(void *unused)
  228. {
  229. /*
  230. * Don't put *anything* before cpu_init(), SMP booting is too
  231. * fragile that we want to limit the things done here to the
  232. * most necessary things.
  233. */
  234. #ifdef CONFIG_VMI
  235. vmi_bringup();
  236. #endif
  237. cpu_init();
  238. preempt_disable();
  239. smp_callin();
  240. while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
  241. rep_nop();
  242. /*
  243. * Check TSC synchronization with the BP:
  244. */
  245. check_tsc_sync_target();
  246. setup_secondary_clock();
  247. if (nmi_watchdog == NMI_IO_APIC) {
  248. disable_8259A_irq(0);
  249. enable_NMI_through_LVT0();
  250. enable_8259A_irq(0);
  251. }
  252. /*
  253. * low-memory mappings have been cleared, flush them from
  254. * the local TLBs too.
  255. */
  256. local_flush_tlb();
  257. /* This must be done before setting cpu_online_map */
  258. set_cpu_sibling_map(raw_smp_processor_id());
  259. wmb();
  260. /*
  261. * We need to hold call_lock, so there is no inconsistency
  262. * between the time smp_call_function() determines number of
  263. * IPI recipients, and the time when the determination is made
  264. * for which cpus receive the IPI. Holding this
  265. * lock helps us to not include this cpu in a currently in progress
  266. * smp_call_function().
  267. */
  268. lock_ipi_call_lock();
  269. cpu_set(smp_processor_id(), cpu_online_map);
  270. unlock_ipi_call_lock();
  271. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  272. /* We can take interrupts now: we're officially "up". */
  273. local_irq_enable();
  274. wmb();
  275. cpu_idle();
  276. }
  277. /*
  278. * Everything has been set up for the secondary
  279. * CPUs - they just need to reload everything
  280. * from the task structure
  281. * This function must not return.
  282. */
  283. void __devinit initialize_secondary(void)
  284. {
  285. /*
  286. * We don't actually need to load the full TSS,
  287. * basically just the stack pointer and the ip.
  288. */
  289. asm volatile(
  290. "movl %0,%%esp\n\t"
  291. "jmp *%1"
  292. :
  293. :"m" (current->thread.sp),"m" (current->thread.ip));
  294. }
  295. /* Static state in head.S used to set up a CPU */
  296. extern struct {
  297. void * sp;
  298. unsigned short ss;
  299. } stack_start;
  300. #ifdef CONFIG_NUMA
  301. /* which logical CPUs are on which nodes */
  302. cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
  303. { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
  304. EXPORT_SYMBOL(node_to_cpumask_map);
  305. /* which node each logical CPU is on */
  306. int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
  307. EXPORT_SYMBOL(cpu_to_node_map);
  308. /* set up a mapping between cpu and node. */
  309. static inline void map_cpu_to_node(int cpu, int node)
  310. {
  311. printk("Mapping cpu %d to node %d\n", cpu, node);
  312. cpu_set(cpu, node_to_cpumask_map[node]);
  313. cpu_to_node_map[cpu] = node;
  314. }
  315. /* undo a mapping between cpu and node. */
  316. static inline void unmap_cpu_to_node(int cpu)
  317. {
  318. int node;
  319. printk("Unmapping cpu %d from all nodes\n", cpu);
  320. for (node = 0; node < MAX_NUMNODES; node ++)
  321. cpu_clear(cpu, node_to_cpumask_map[node]);
  322. cpu_to_node_map[cpu] = 0;
  323. }
  324. #else /* !CONFIG_NUMA */
  325. #define map_cpu_to_node(cpu, node) ({})
  326. #define unmap_cpu_to_node(cpu) ({})
  327. #endif /* CONFIG_NUMA */
  328. u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
  329. static void map_cpu_to_logical_apicid(void)
  330. {
  331. int cpu = smp_processor_id();
  332. int apicid = logical_smp_processor_id();
  333. int node = apicid_to_node(apicid);
  334. if (!node_online(node))
  335. node = first_online_node;
  336. cpu_2_logical_apicid[cpu] = apicid;
  337. map_cpu_to_node(cpu, node);
  338. }
  339. static void unmap_cpu_to_logical_apicid(int cpu)
  340. {
  341. cpu_2_logical_apicid[cpu] = BAD_APICID;
  342. unmap_cpu_to_node(cpu);
  343. }
  344. static inline void __inquire_remote_apic(int apicid)
  345. {
  346. int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  347. char *names[] = { "ID", "VERSION", "SPIV" };
  348. int timeout;
  349. unsigned long status;
  350. printk("Inquiring remote APIC #%d...\n", apicid);
  351. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  352. printk("... APIC #%d %s: ", apicid, names[i]);
  353. /*
  354. * Wait for idle.
  355. */
  356. status = safe_apic_wait_icr_idle();
  357. if (status)
  358. printk("a previous APIC delivery may have failed\n");
  359. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  360. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  361. timeout = 0;
  362. do {
  363. udelay(100);
  364. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  365. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  366. switch (status) {
  367. case APIC_ICR_RR_VALID:
  368. status = apic_read(APIC_RRR);
  369. printk("%lx\n", status);
  370. break;
  371. default:
  372. printk("failed\n");
  373. }
  374. }
  375. }
  376. #ifdef WAKE_SECONDARY_VIA_NMI
  377. /*
  378. * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
  379. * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
  380. * won't ... remember to clear down the APIC, etc later.
  381. */
  382. static int __devinit
  383. wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
  384. {
  385. unsigned long send_status, accept_status = 0;
  386. int maxlvt;
  387. /* Target chip */
  388. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
  389. /* Boot on the stack */
  390. /* Kick the second */
  391. apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
  392. Dprintk("Waiting for send to finish...\n");
  393. send_status = safe_apic_wait_icr_idle();
  394. /*
  395. * Give the other CPU some time to accept the IPI.
  396. */
  397. udelay(200);
  398. /*
  399. * Due to the Pentium erratum 3AP.
  400. */
  401. maxlvt = lapic_get_maxlvt();
  402. if (maxlvt > 3) {
  403. apic_read_around(APIC_SPIV);
  404. apic_write(APIC_ESR, 0);
  405. }
  406. accept_status = (apic_read(APIC_ESR) & 0xEF);
  407. Dprintk("NMI sent.\n");
  408. if (send_status)
  409. printk("APIC never delivered???\n");
  410. if (accept_status)
  411. printk("APIC delivery error (%lx).\n", accept_status);
  412. return (send_status | accept_status);
  413. }
  414. #endif /* WAKE_SECONDARY_VIA_NMI */
  415. #ifdef WAKE_SECONDARY_VIA_INIT
  416. static int __devinit
  417. wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
  418. {
  419. unsigned long send_status, accept_status = 0;
  420. int maxlvt, num_starts, j;
  421. /*
  422. * Be paranoid about clearing APIC errors.
  423. */
  424. if (APIC_INTEGRATED(apic_version[phys_apicid])) {
  425. apic_read_around(APIC_SPIV);
  426. apic_write(APIC_ESR, 0);
  427. apic_read(APIC_ESR);
  428. }
  429. Dprintk("Asserting INIT.\n");
  430. /*
  431. * Turn INIT on target chip
  432. */
  433. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  434. /*
  435. * Send IPI
  436. */
  437. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  438. | APIC_DM_INIT);
  439. Dprintk("Waiting for send to finish...\n");
  440. send_status = safe_apic_wait_icr_idle();
  441. mdelay(10);
  442. Dprintk("Deasserting INIT.\n");
  443. /* Target chip */
  444. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  445. /* Send IPI */
  446. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  447. Dprintk("Waiting for send to finish...\n");
  448. send_status = safe_apic_wait_icr_idle();
  449. atomic_set(&init_deasserted, 1);
  450. /*
  451. * Should we send STARTUP IPIs ?
  452. *
  453. * Determine this based on the APIC version.
  454. * If we don't have an integrated APIC, don't send the STARTUP IPIs.
  455. */
  456. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  457. num_starts = 2;
  458. else
  459. num_starts = 0;
  460. /*
  461. * Paravirt / VMI wants a startup IPI hook here to set up the
  462. * target processor state.
  463. */
  464. startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
  465. (unsigned long) stack_start.sp);
  466. /*
  467. * Run STARTUP IPI loop.
  468. */
  469. Dprintk("#startup loops: %d.\n", num_starts);
  470. maxlvt = lapic_get_maxlvt();
  471. for (j = 1; j <= num_starts; j++) {
  472. Dprintk("Sending STARTUP #%d.\n",j);
  473. apic_read_around(APIC_SPIV);
  474. apic_write(APIC_ESR, 0);
  475. apic_read(APIC_ESR);
  476. Dprintk("After apic_write.\n");
  477. /*
  478. * STARTUP IPI
  479. */
  480. /* Target chip */
  481. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  482. /* Boot on the stack */
  483. /* Kick the second */
  484. apic_write_around(APIC_ICR, APIC_DM_STARTUP
  485. | (start_eip >> 12));
  486. /*
  487. * Give the other CPU some time to accept the IPI.
  488. */
  489. udelay(300);
  490. Dprintk("Startup point 1.\n");
  491. Dprintk("Waiting for send to finish...\n");
  492. send_status = safe_apic_wait_icr_idle();
  493. /*
  494. * Give the other CPU some time to accept the IPI.
  495. */
  496. udelay(200);
  497. /*
  498. * Due to the Pentium erratum 3AP.
  499. */
  500. if (maxlvt > 3) {
  501. apic_read_around(APIC_SPIV);
  502. apic_write(APIC_ESR, 0);
  503. }
  504. accept_status = (apic_read(APIC_ESR) & 0xEF);
  505. if (send_status || accept_status)
  506. break;
  507. }
  508. Dprintk("After Startup.\n");
  509. if (send_status)
  510. printk("APIC never delivered???\n");
  511. if (accept_status)
  512. printk("APIC delivery error (%lx).\n", accept_status);
  513. return (send_status | accept_status);
  514. }
  515. #endif /* WAKE_SECONDARY_VIA_INIT */
  516. extern cpumask_t cpu_initialized;
  517. static inline int alloc_cpu_id(void)
  518. {
  519. cpumask_t tmp_map;
  520. int cpu;
  521. cpus_complement(tmp_map, cpu_present_map);
  522. cpu = first_cpu(tmp_map);
  523. if (cpu >= NR_CPUS)
  524. return -ENODEV;
  525. return cpu;
  526. }
  527. #ifdef CONFIG_HOTPLUG_CPU
  528. static struct task_struct * __cpuinitdata cpu_idle_tasks[NR_CPUS];
  529. static inline struct task_struct * __cpuinit alloc_idle_task(int cpu)
  530. {
  531. struct task_struct *idle;
  532. if ((idle = cpu_idle_tasks[cpu]) != NULL) {
  533. /* initialize thread_struct. we really want to avoid destroy
  534. * idle tread
  535. */
  536. idle->thread.sp = (unsigned long)task_pt_regs(idle);
  537. init_idle(idle, cpu);
  538. return idle;
  539. }
  540. idle = fork_idle(cpu);
  541. if (!IS_ERR(idle))
  542. cpu_idle_tasks[cpu] = idle;
  543. return idle;
  544. }
  545. #else
  546. #define alloc_idle_task(cpu) fork_idle(cpu)
  547. #endif
  548. static int __cpuinit do_boot_cpu(int apicid, int cpu)
  549. /*
  550. * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
  551. * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
  552. * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
  553. */
  554. {
  555. struct task_struct *idle;
  556. unsigned long boot_error;
  557. int timeout;
  558. unsigned long start_eip;
  559. unsigned short nmi_high = 0, nmi_low = 0;
  560. /*
  561. * Save current MTRR state in case it was changed since early boot
  562. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  563. */
  564. mtrr_save_state();
  565. /*
  566. * We can't use kernel_thread since we must avoid to
  567. * reschedule the child.
  568. */
  569. idle = alloc_idle_task(cpu);
  570. if (IS_ERR(idle))
  571. panic("failed fork for CPU %d", cpu);
  572. init_gdt(cpu);
  573. per_cpu(current_task, cpu) = idle;
  574. early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
  575. idle->thread.ip = (unsigned long) start_secondary;
  576. /* start_eip had better be page-aligned! */
  577. start_eip = setup_trampoline();
  578. ++cpucount;
  579. alternatives_smp_switch(1);
  580. /* So we see what's up */
  581. printk("Booting processor %d/%d ip %lx\n", cpu, apicid, start_eip);
  582. /* Stack for startup_32 can be just as for start_secondary onwards */
  583. stack_start.sp = (void *) idle->thread.sp;
  584. irq_ctx_init(cpu);
  585. per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  586. /*
  587. * This grunge runs the startup process for
  588. * the targeted processor.
  589. */
  590. atomic_set(&init_deasserted, 0);
  591. Dprintk("Setting warm reset code and vector.\n");
  592. store_NMI_vector(&nmi_high, &nmi_low);
  593. smpboot_setup_warm_reset_vector(start_eip);
  594. /*
  595. * Starting actual IPI sequence...
  596. */
  597. boot_error = wakeup_secondary_cpu(apicid, start_eip);
  598. if (!boot_error) {
  599. /*
  600. * allow APs to start initializing.
  601. */
  602. Dprintk("Before Callout %d.\n", cpu);
  603. cpu_set(cpu, cpu_callout_map);
  604. Dprintk("After Callout %d.\n", cpu);
  605. /*
  606. * Wait 5s total for a response
  607. */
  608. for (timeout = 0; timeout < 50000; timeout++) {
  609. if (cpu_isset(cpu, cpu_callin_map))
  610. break; /* It has booted */
  611. udelay(100);
  612. }
  613. if (cpu_isset(cpu, cpu_callin_map)) {
  614. /* number CPUs logically, starting from 1 (BSP is 0) */
  615. Dprintk("OK.\n");
  616. printk("CPU%d: ", cpu);
  617. print_cpu_info(&cpu_data(cpu));
  618. Dprintk("CPU has booted.\n");
  619. } else {
  620. boot_error= 1;
  621. if (*((volatile unsigned char *)trampoline_base)
  622. == 0xA5)
  623. /* trampoline started but...? */
  624. printk("Stuck ??\n");
  625. else
  626. /* trampoline code not run */
  627. printk("Not responding.\n");
  628. inquire_remote_apic(apicid);
  629. }
  630. }
  631. if (boot_error) {
  632. /* Try to put things back the way they were before ... */
  633. unmap_cpu_to_logical_apicid(cpu);
  634. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  635. cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
  636. cpucount--;
  637. } else {
  638. per_cpu(x86_cpu_to_apicid, cpu) = apicid;
  639. cpu_set(cpu, cpu_present_map);
  640. }
  641. /* mark "stuck" area as not stuck */
  642. *((volatile unsigned long *)trampoline_base) = 0;
  643. return boot_error;
  644. }
  645. #ifdef CONFIG_HOTPLUG_CPU
  646. void cpu_exit_clear(void)
  647. {
  648. int cpu = raw_smp_processor_id();
  649. idle_task_exit();
  650. cpucount --;
  651. cpu_uninit();
  652. irq_ctx_exit(cpu);
  653. cpu_clear(cpu, cpu_callout_map);
  654. cpu_clear(cpu, cpu_callin_map);
  655. cpu_clear(cpu, smp_commenced_mask);
  656. unmap_cpu_to_logical_apicid(cpu);
  657. }
  658. struct warm_boot_cpu_info {
  659. struct completion *complete;
  660. struct work_struct task;
  661. int apicid;
  662. int cpu;
  663. };
  664. static void __cpuinit do_warm_boot_cpu(struct work_struct *work)
  665. {
  666. struct warm_boot_cpu_info *info =
  667. container_of(work, struct warm_boot_cpu_info, task);
  668. do_boot_cpu(info->apicid, info->cpu);
  669. complete(info->complete);
  670. }
  671. static int __cpuinit __smp_prepare_cpu(int cpu)
  672. {
  673. DECLARE_COMPLETION_ONSTACK(done);
  674. struct warm_boot_cpu_info info;
  675. int apicid, ret;
  676. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  677. if (apicid == BAD_APICID) {
  678. ret = -ENODEV;
  679. goto exit;
  680. }
  681. info.complete = &done;
  682. info.apicid = apicid;
  683. info.cpu = cpu;
  684. INIT_WORK(&info.task, do_warm_boot_cpu);
  685. /* init low mem mapping */
  686. clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
  687. min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
  688. flush_tlb_all();
  689. schedule_work(&info.task);
  690. wait_for_completion(&done);
  691. zap_low_mappings();
  692. ret = 0;
  693. exit:
  694. return ret;
  695. }
  696. #endif
  697. /*
  698. * Cycle through the processors sending APIC IPIs to boot each.
  699. */
  700. static int boot_cpu_logical_apicid;
  701. /* Where the IO area was mapped on multiquad, always 0 otherwise */
  702. void *xquad_portio;
  703. #ifdef CONFIG_X86_NUMAQ
  704. EXPORT_SYMBOL(xquad_portio);
  705. #endif
  706. static void __init smp_boot_cpus(unsigned int max_cpus)
  707. {
  708. int apicid, cpu, bit, kicked;
  709. unsigned long bogosum = 0;
  710. /*
  711. * Setup boot CPU information
  712. */
  713. smp_store_cpu_info(0); /* Final full version of the data */
  714. printk("CPU%d: ", 0);
  715. print_cpu_info(&cpu_data(0));
  716. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  717. boot_cpu_logical_apicid = logical_smp_processor_id();
  718. per_cpu(x86_cpu_to_apicid, 0) = boot_cpu_physical_apicid;
  719. current_thread_info()->cpu = 0;
  720. set_cpu_sibling_map(0);
  721. /*
  722. * If we couldn't find an SMP configuration at boot time,
  723. * get out of here now!
  724. */
  725. if (!smp_found_config && !acpi_lapic) {
  726. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  727. smpboot_clear_io_apic_irqs();
  728. phys_cpu_present_map = physid_mask_of_physid(0);
  729. if (APIC_init_uniprocessor())
  730. printk(KERN_NOTICE "Local APIC not detected."
  731. " Using dummy APIC emulation.\n");
  732. map_cpu_to_logical_apicid();
  733. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  734. cpu_set(0, per_cpu(cpu_core_map, 0));
  735. return;
  736. }
  737. /*
  738. * Should not be necessary because the MP table should list the boot
  739. * CPU too, but we do it for the sake of robustness anyway.
  740. * Makes no sense to do this check in clustered apic mode, so skip it
  741. */
  742. if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
  743. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  744. boot_cpu_physical_apicid);
  745. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  746. }
  747. /*
  748. * If we couldn't find a local APIC, then get out of here now!
  749. */
  750. if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
  751. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  752. boot_cpu_physical_apicid);
  753. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  754. smpboot_clear_io_apic_irqs();
  755. phys_cpu_present_map = physid_mask_of_physid(0);
  756. map_cpu_to_logical_apicid();
  757. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  758. cpu_set(0, per_cpu(cpu_core_map, 0));
  759. return;
  760. }
  761. verify_local_APIC();
  762. /*
  763. * If SMP should be disabled, then really disable it!
  764. */
  765. if (!max_cpus) {
  766. smp_found_config = 0;
  767. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  768. if (nmi_watchdog == NMI_LOCAL_APIC) {
  769. printk(KERN_INFO "activating minimal APIC for NMI watchdog use.\n");
  770. connect_bsp_APIC();
  771. setup_local_APIC();
  772. }
  773. smpboot_clear_io_apic_irqs();
  774. phys_cpu_present_map = physid_mask_of_physid(0);
  775. map_cpu_to_logical_apicid();
  776. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  777. cpu_set(0, per_cpu(cpu_core_map, 0));
  778. return;
  779. }
  780. connect_bsp_APIC();
  781. setup_local_APIC();
  782. map_cpu_to_logical_apicid();
  783. setup_portio_remap();
  784. /*
  785. * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
  786. *
  787. * In clustered apic mode, phys_cpu_present_map is a constructed thus:
  788. * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
  789. * clustered apic ID.
  790. */
  791. Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
  792. kicked = 1;
  793. for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
  794. apicid = cpu_present_to_apicid(bit);
  795. /*
  796. * Don't even attempt to start the boot CPU!
  797. */
  798. if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
  799. continue;
  800. if (!check_apicid_present(bit))
  801. continue;
  802. if (max_cpus <= cpucount+1)
  803. continue;
  804. if (((cpu = alloc_cpu_id()) <= 0) || do_boot_cpu(apicid, cpu))
  805. printk("CPU #%d not responding - cannot use it.\n",
  806. apicid);
  807. else
  808. ++kicked;
  809. }
  810. /*
  811. * Cleanup possible dangling ends...
  812. */
  813. smpboot_restore_warm_reset_vector();
  814. /*
  815. * Allow the user to impress friends.
  816. */
  817. Dprintk("Before bogomips.\n");
  818. for_each_possible_cpu(cpu)
  819. if (cpu_isset(cpu, cpu_callout_map))
  820. bogosum += cpu_data(cpu).loops_per_jiffy;
  821. printk(KERN_INFO
  822. "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  823. cpucount+1,
  824. bogosum/(500000/HZ),
  825. (bogosum/(5000/HZ))%100);
  826. Dprintk("Before bogocount - setting activated=1.\n");
  827. if (smp_b_stepping)
  828. printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
  829. /*
  830. * Don't taint if we are running SMP kernel on a single non-MP
  831. * approved Athlon
  832. */
  833. if (tainted & TAINT_UNSAFE_SMP) {
  834. if (cpucount)
  835. printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
  836. else
  837. tainted &= ~TAINT_UNSAFE_SMP;
  838. }
  839. Dprintk("Boot done.\n");
  840. /*
  841. * construct cpu_sibling_map, so that we can tell sibling CPUs
  842. * efficiently.
  843. */
  844. for_each_possible_cpu(cpu) {
  845. cpus_clear(per_cpu(cpu_sibling_map, cpu));
  846. cpus_clear(per_cpu(cpu_core_map, cpu));
  847. }
  848. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  849. cpu_set(0, per_cpu(cpu_core_map, 0));
  850. smpboot_setup_io_apic();
  851. setup_boot_clock();
  852. }
  853. /* These are wrappers to interface to the new boot process. Someone
  854. who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
  855. void __init native_smp_prepare_cpus(unsigned int max_cpus)
  856. {
  857. smp_commenced_mask = cpumask_of_cpu(0);
  858. cpu_callin_map = cpumask_of_cpu(0);
  859. mb();
  860. smp_boot_cpus(max_cpus);
  861. }
  862. void __init native_smp_prepare_boot_cpu(void)
  863. {
  864. unsigned int cpu = smp_processor_id();
  865. init_gdt(cpu);
  866. switch_to_new_gdt();
  867. cpu_set(cpu, cpu_online_map);
  868. cpu_set(cpu, cpu_callout_map);
  869. cpu_set(cpu, cpu_present_map);
  870. cpu_set(cpu, cpu_possible_map);
  871. __get_cpu_var(cpu_state) = CPU_ONLINE;
  872. }
  873. #ifdef CONFIG_HOTPLUG_CPU
  874. static void __ref remove_cpu_from_maps(int cpu)
  875. {
  876. cpu_clear(cpu, cpu_online_map);
  877. }
  878. int __cpu_disable(void)
  879. {
  880. cpumask_t map = cpu_online_map;
  881. int cpu = smp_processor_id();
  882. /*
  883. * Perhaps use cpufreq to drop frequency, but that could go
  884. * into generic code.
  885. *
  886. * We won't take down the boot processor on i386 due to some
  887. * interrupts only being able to be serviced by the BSP.
  888. * Especially so if we're not using an IOAPIC -zwane
  889. */
  890. if (cpu == 0)
  891. return -EBUSY;
  892. if (nmi_watchdog == NMI_LOCAL_APIC)
  893. stop_apic_nmi_watchdog(NULL);
  894. clear_local_APIC();
  895. /* Allow any queued timer interrupts to get serviced */
  896. local_irq_enable();
  897. mdelay(1);
  898. local_irq_disable();
  899. remove_siblinginfo(cpu);
  900. remove_cpu_from_maps(cpu);
  901. fixup_irqs(map);
  902. return 0;
  903. }
  904. void __cpu_die(unsigned int cpu)
  905. {
  906. /* We don't do anything here: idle task is faking death itself. */
  907. unsigned int i;
  908. for (i = 0; i < 10; i++) {
  909. /* They ack this in play_dead by setting CPU_DEAD */
  910. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  911. printk ("CPU %d is now offline\n", cpu);
  912. if (1 == num_online_cpus())
  913. alternatives_smp_switch(0);
  914. return;
  915. }
  916. msleep(100);
  917. }
  918. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  919. }
  920. #else /* ... !CONFIG_HOTPLUG_CPU */
  921. int __cpu_disable(void)
  922. {
  923. return -ENOSYS;
  924. }
  925. void __cpu_die(unsigned int cpu)
  926. {
  927. /* We said "no" in __cpu_disable */
  928. BUG();
  929. }
  930. #endif /* CONFIG_HOTPLUG_CPU */
  931. int __cpuinit native_cpu_up(unsigned int cpu)
  932. {
  933. unsigned long flags;
  934. #ifdef CONFIG_HOTPLUG_CPU
  935. int ret = 0;
  936. /*
  937. * We do warm boot only on cpus that had booted earlier
  938. * Otherwise cold boot is all handled from smp_boot_cpus().
  939. * cpu_callin_map is set during AP kickstart process. Its reset
  940. * when a cpu is taken offline from cpu_exit_clear().
  941. */
  942. if (!cpu_isset(cpu, cpu_callin_map))
  943. ret = __smp_prepare_cpu(cpu);
  944. if (ret)
  945. return -EIO;
  946. #endif
  947. /* In case one didn't come up */
  948. if (!cpu_isset(cpu, cpu_callin_map)) {
  949. printk(KERN_DEBUG "skipping cpu%d, didn't come online\n", cpu);
  950. return -EIO;
  951. }
  952. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  953. /* Unleash the CPU! */
  954. cpu_set(cpu, smp_commenced_mask);
  955. /*
  956. * Check TSC synchronization with the AP (keep irqs disabled
  957. * while doing so):
  958. */
  959. local_irq_save(flags);
  960. check_tsc_sync_source(cpu);
  961. local_irq_restore(flags);
  962. while (!cpu_isset(cpu, cpu_online_map)) {
  963. cpu_relax();
  964. touch_nmi_watchdog();
  965. }
  966. return 0;
  967. }
  968. void __init native_smp_cpus_done(unsigned int max_cpus)
  969. {
  970. #ifdef CONFIG_X86_IO_APIC
  971. setup_ioapic_dest();
  972. #endif
  973. zap_low_mappings();
  974. }
  975. void __init smp_intr_init(void)
  976. {
  977. /*
  978. * IRQ0 must be given a fixed assignment and initialized,
  979. * because it's used before the IO-APIC is set up.
  980. */
  981. set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
  982. /*
  983. * The reschedule interrupt is a CPU-to-CPU reschedule-helper
  984. * IPI, driven by wakeup.
  985. */
  986. set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
  987. /* IPI for invalidation */
  988. set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
  989. /* IPI for generic function call */
  990. set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
  991. }
  992. /*
  993. * If the BIOS enumerates physical processors before logical,
  994. * maxcpus=N at enumeration-time can be used to disable HT.
  995. */
  996. static int __init parse_maxcpus(char *arg)
  997. {
  998. extern unsigned int maxcpus;
  999. maxcpus = simple_strtoul(arg, NULL, 0);
  1000. return 0;
  1001. }
  1002. early_param("maxcpus", parse_maxcpus);