smpboot_32.c 25 KB

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