smpboot_32.c 25 KB

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