smpboot_32.c 22 KB

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