smpboot_32.c 22 KB

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