smpboot_32.c 26 KB

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