smpboot_32.c 26 KB

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