smpboot_64.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. * Copyright 2001 Andi Kleen, SuSE Labs.
  7. *
  8. * Much of the core SMP work is based on previous work by Thomas Radke, to
  9. * whom a great many thanks are extended.
  10. *
  11. * Thanks to Intel for making available several different Pentium,
  12. * Pentium Pro and Pentium-II/Xeon MP machines.
  13. * Original development of Linux SMP code supported by Caldera.
  14. *
  15. * This code is released under the GNU General Public License version 2
  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 BogoMIP 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. * Andi Kleen : Changed for SMP boot into long mode.
  33. * Rusty Russell : Hacked into shape for new "hotplug" boot process.
  34. * Andi Kleen : Converted to new state machine.
  35. * Various cleanups.
  36. * Probably mostly hotplug CPU ready now.
  37. * Ashok Raj : CPU hotplug support
  38. */
  39. #include <linux/init.h>
  40. #include <linux/mm.h>
  41. #include <linux/kernel_stat.h>
  42. #include <linux/bootmem.h>
  43. #include <linux/thread_info.h>
  44. #include <linux/module.h>
  45. #include <linux/delay.h>
  46. #include <linux/mc146818rtc.h>
  47. #include <linux/smp.h>
  48. #include <linux/kdebug.h>
  49. #include <asm/mtrr.h>
  50. #include <asm/pgalloc.h>
  51. #include <asm/desc.h>
  52. #include <asm/tlbflush.h>
  53. #include <asm/proto.h>
  54. #include <asm/nmi.h>
  55. #include <asm/irq.h>
  56. #include <asm/hw_irq.h>
  57. #include <asm/numa.h>
  58. /* Set when the idlers are all forked */
  59. int smp_threads_ready;
  60. /* State of each CPU */
  61. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  62. /*
  63. * Store all idle threads, this can be reused instead of creating
  64. * a new thread. Also avoids complicated thread destroy functionality
  65. * for idle threads.
  66. */
  67. #ifdef CONFIG_HOTPLUG_CPU
  68. /*
  69. * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
  70. * removed after init for !CONFIG_HOTPLUG_CPU.
  71. */
  72. static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
  73. #define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
  74. #define set_idle_for_cpu(x,p) (per_cpu(idle_thread_array, x) = (p))
  75. #else
  76. struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
  77. #define get_idle_for_cpu(x) (idle_thread_array[(x)])
  78. #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
  79. #endif
  80. static inline void wait_for_init_deassert(atomic_t *deassert)
  81. {
  82. while (!atomic_read(deassert))
  83. cpu_relax();
  84. return;
  85. }
  86. static atomic_t init_deasserted __cpuinitdata;
  87. /*
  88. * Report back to the Boot Processor.
  89. * Running on AP.
  90. */
  91. void __cpuinit smp_callin(void)
  92. {
  93. int cpuid, phys_id;
  94. unsigned long timeout;
  95. /*
  96. * If waken up by an INIT in an 82489DX configuration
  97. * we may get here before an INIT-deassert IPI reaches
  98. * our local APIC. We have to wait for the IPI or we'll
  99. * lock up on an APIC access.
  100. */
  101. wait_for_init_deassert(&init_deasserted);
  102. /*
  103. * (This works even if the APIC is not enabled.)
  104. */
  105. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  106. cpuid = smp_processor_id();
  107. if (cpu_isset(cpuid, cpu_callin_map)) {
  108. panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
  109. phys_id, cpuid);
  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. panic("smp_callin: CPU%d started up but did not get a callout!\n",
  133. cpuid);
  134. }
  135. /*
  136. * the boot CPU has finished the init stage and is spinning
  137. * on callin_map until we finish. We are free to set up this
  138. * CPU, first the APIC. (this is probably redundant on most
  139. * boards)
  140. */
  141. Dprintk("CALLIN, before setup_local_APIC().\n");
  142. setup_local_APIC();
  143. end_local_APIC_setup();
  144. /*
  145. * Get our bogomips.
  146. *
  147. * Need to enable IRQs because it can take longer and then
  148. * the NMI watchdog might kill us.
  149. */
  150. local_irq_enable();
  151. calibrate_delay();
  152. local_irq_disable();
  153. Dprintk("Stack at about %p\n",&cpuid);
  154. /*
  155. * Save our processor parameters
  156. */
  157. smp_store_cpu_info(cpuid);
  158. /*
  159. * Allow the master to continue.
  160. */
  161. cpu_set(cpuid, cpu_callin_map);
  162. }
  163. /*
  164. * Setup code on secondary processor (after comming out of the trampoline)
  165. */
  166. void __cpuinit start_secondary(void)
  167. {
  168. /*
  169. * Dont put anything before smp_callin(), SMP
  170. * booting is too fragile that we want to limit the
  171. * things done here to the most necessary things.
  172. */
  173. cpu_init();
  174. preempt_disable();
  175. smp_callin();
  176. /* otherwise gcc will move up the smp_processor_id before the cpu_init */
  177. barrier();
  178. /*
  179. * Check TSC sync first:
  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. /*
  188. * The sibling maps must be set before turing the online map on for
  189. * this cpu
  190. */
  191. set_cpu_sibling_map(smp_processor_id());
  192. /*
  193. * We need to hold call_lock, so there is no inconsistency
  194. * between the time smp_call_function() determines number of
  195. * IPI recipients, and the time when the determination is made
  196. * for which cpus receive the IPI in genapic_flat.c. Holding this
  197. * lock helps us to not include this cpu in a currently in progress
  198. * smp_call_function().
  199. */
  200. lock_ipi_call_lock();
  201. spin_lock(&vector_lock);
  202. /* Setup the per cpu irq handling data structures */
  203. __setup_vector_irq(smp_processor_id());
  204. /*
  205. * Allow the master to continue.
  206. */
  207. spin_unlock(&vector_lock);
  208. cpu_set(smp_processor_id(), cpu_online_map);
  209. unlock_ipi_call_lock();
  210. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  211. setup_secondary_clock();
  212. cpu_idle();
  213. }
  214. extern volatile unsigned long init_rsp;
  215. extern void (*initial_code)(void);
  216. #ifdef APIC_DEBUG
  217. static void inquire_remote_apic(int apicid)
  218. {
  219. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  220. char *names[] = { "ID", "VERSION", "SPIV" };
  221. int timeout;
  222. u32 status;
  223. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  224. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  225. printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
  226. /*
  227. * Wait for idle.
  228. */
  229. status = safe_apic_wait_icr_idle();
  230. if (status)
  231. printk(KERN_CONT
  232. "a previous APIC delivery may have failed\n");
  233. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  234. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  235. timeout = 0;
  236. do {
  237. udelay(100);
  238. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  239. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  240. switch (status) {
  241. case APIC_ICR_RR_VALID:
  242. status = apic_read(APIC_RRR);
  243. printk(KERN_CONT "%08x\n", status);
  244. break;
  245. default:
  246. printk(KERN_CONT "failed\n");
  247. }
  248. }
  249. }
  250. #endif
  251. /*
  252. * Kick the secondary to wake up.
  253. */
  254. static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
  255. {
  256. unsigned long send_status, accept_status = 0;
  257. int maxlvt, num_starts, j;
  258. /*
  259. * Be paranoid about clearing APIC errors.
  260. */
  261. if (APIC_INTEGRATED(apic_version[phys_apicid])) {
  262. apic_read_around(APIC_SPIV);
  263. apic_write(APIC_ESR, 0);
  264. apic_read(APIC_ESR);
  265. }
  266. Dprintk("Asserting INIT.\n");
  267. /*
  268. * Turn INIT on target chip
  269. */
  270. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  271. /*
  272. * Send IPI
  273. */
  274. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  275. | APIC_DM_INIT);
  276. Dprintk("Waiting for send to finish...\n");
  277. send_status = safe_apic_wait_icr_idle();
  278. mdelay(10);
  279. Dprintk("Deasserting INIT.\n");
  280. /* Target chip */
  281. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  282. /* Send IPI */
  283. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  284. Dprintk("Waiting for send to finish...\n");
  285. send_status = safe_apic_wait_icr_idle();
  286. mb();
  287. atomic_set(&init_deasserted, 1);
  288. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  289. num_starts = 2;
  290. else
  291. num_starts = 0;
  292. /*
  293. * Paravirt / VMI wants a startup IPI hook here to set up the
  294. * target processor state.
  295. */
  296. startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
  297. (unsigned long) init_rsp);
  298. /*
  299. * Run STARTUP IPI loop.
  300. */
  301. Dprintk("#startup loops: %d.\n", num_starts);
  302. maxlvt = lapic_get_maxlvt();
  303. for (j = 1; j <= num_starts; j++) {
  304. Dprintk("Sending STARTUP #%d.\n",j);
  305. apic_read_around(APIC_SPIV);
  306. apic_write(APIC_ESR, 0);
  307. apic_read(APIC_ESR);
  308. Dprintk("After apic_write.\n");
  309. /*
  310. * STARTUP IPI
  311. */
  312. /* Target chip */
  313. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  314. /* Boot on the stack */
  315. /* Kick the second */
  316. apic_write_around(APIC_ICR, APIC_DM_STARTUP | (start_rip>>12));
  317. /*
  318. * Give the other CPU some time to accept the IPI.
  319. */
  320. udelay(300);
  321. Dprintk("Startup point 1.\n");
  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. if (maxlvt > 3) {
  332. apic_read_around(APIC_SPIV);
  333. apic_write(APIC_ESR, 0);
  334. }
  335. accept_status = (apic_read(APIC_ESR) & 0xEF);
  336. if (send_status || accept_status)
  337. break;
  338. }
  339. Dprintk("After Startup.\n");
  340. if (send_status)
  341. printk(KERN_ERR "APIC never delivered???\n");
  342. if (accept_status)
  343. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  344. return (send_status | accept_status);
  345. }
  346. struct create_idle {
  347. struct work_struct work;
  348. struct task_struct *idle;
  349. struct completion done;
  350. int cpu;
  351. };
  352. static void __cpuinit do_fork_idle(struct work_struct *work)
  353. {
  354. struct create_idle *c_idle =
  355. container_of(work, struct create_idle, work);
  356. c_idle->idle = fork_idle(c_idle->cpu);
  357. complete(&c_idle->done);
  358. }
  359. /*
  360. * Boot one CPU.
  361. */
  362. static int __cpuinit do_boot_cpu(int cpu, int apicid)
  363. {
  364. unsigned long boot_error;
  365. int timeout;
  366. unsigned long start_rip;
  367. struct create_idle c_idle = {
  368. .cpu = cpu,
  369. .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
  370. };
  371. INIT_WORK(&c_idle.work, do_fork_idle);
  372. /* allocate memory for gdts of secondary cpus. Hotplug is considered */
  373. if (!cpu_gdt_descr[cpu].address &&
  374. !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
  375. printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
  376. return -1;
  377. }
  378. /* Allocate node local memory for AP pdas */
  379. if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
  380. struct x8664_pda *newpda, *pda;
  381. int node = cpu_to_node(cpu);
  382. pda = cpu_pda(cpu);
  383. newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
  384. node);
  385. if (newpda) {
  386. memcpy(newpda, pda, sizeof (struct x8664_pda));
  387. cpu_pda(cpu) = newpda;
  388. } else
  389. printk(KERN_ERR
  390. "Could not allocate node local PDA for CPU %d on node %d\n",
  391. cpu, node);
  392. }
  393. alternatives_smp_switch(1);
  394. c_idle.idle = get_idle_for_cpu(cpu);
  395. if (c_idle.idle) {
  396. c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
  397. (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
  398. init_idle(c_idle.idle, cpu);
  399. goto do_rest;
  400. }
  401. /*
  402. * During cold boot process, keventd thread is not spun up yet.
  403. * When we do cpu hot-add, we create idle threads on the fly, we should
  404. * not acquire any attributes from the calling context. Hence the clean
  405. * way to create kernel_threads() is to do that from keventd().
  406. * We do the current_is_keventd() due to the fact that ACPI notifier
  407. * was also queuing to keventd() and when the caller is already running
  408. * in context of keventd(), we would end up with locking up the keventd
  409. * thread.
  410. */
  411. if (!keventd_up() || current_is_keventd())
  412. c_idle.work.func(&c_idle.work);
  413. else {
  414. schedule_work(&c_idle.work);
  415. wait_for_completion(&c_idle.done);
  416. }
  417. if (IS_ERR(c_idle.idle)) {
  418. printk("failed fork for CPU %d\n", cpu);
  419. return PTR_ERR(c_idle.idle);
  420. }
  421. set_idle_for_cpu(cpu, c_idle.idle);
  422. do_rest:
  423. cpu_pda(cpu)->pcurrent = c_idle.idle;
  424. start_rip = setup_trampoline();
  425. init_rsp = c_idle.idle->thread.sp;
  426. load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
  427. initial_code = start_secondary;
  428. clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
  429. printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
  430. cpus_weight(cpu_present_map),
  431. apicid);
  432. /*
  433. * This grunge runs the startup process for
  434. * the targeted processor.
  435. */
  436. atomic_set(&init_deasserted, 0);
  437. Dprintk("Setting warm reset code and vector.\n");
  438. CMOS_WRITE(0xa, 0xf);
  439. local_flush_tlb();
  440. Dprintk("1.\n");
  441. *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
  442. Dprintk("2.\n");
  443. *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
  444. Dprintk("3.\n");
  445. /*
  446. * Be paranoid about clearing APIC errors.
  447. */
  448. apic_write(APIC_ESR, 0);
  449. apic_read(APIC_ESR);
  450. /*
  451. * Status is now clean
  452. */
  453. boot_error = 0;
  454. /*
  455. * Starting actual IPI sequence...
  456. */
  457. boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
  458. if (!boot_error) {
  459. /*
  460. * allow APs to start initializing.
  461. */
  462. Dprintk("Before Callout %d.\n", cpu);
  463. cpu_set(cpu, cpu_callout_map);
  464. Dprintk("After Callout %d.\n", cpu);
  465. /*
  466. * Wait 5s total for a response
  467. */
  468. for (timeout = 0; timeout < 50000; timeout++) {
  469. if (cpu_isset(cpu, cpu_callin_map))
  470. break; /* It has booted */
  471. udelay(100);
  472. }
  473. if (cpu_isset(cpu, cpu_callin_map)) {
  474. /* number CPUs logically, starting from 1 (BSP is 0) */
  475. Dprintk("CPU has booted.\n");
  476. printk(KERN_INFO "CPU%d: ", cpu);
  477. print_cpu_info(&cpu_data(cpu));
  478. } else {
  479. boot_error = 1;
  480. if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
  481. == 0xA5)
  482. /* trampoline started but...? */
  483. printk("Stuck ??\n");
  484. else
  485. /* trampoline code not run */
  486. printk("Not responding.\n");
  487. #ifdef APIC_DEBUG
  488. inquire_remote_apic(apicid);
  489. #endif
  490. }
  491. }
  492. if (boot_error) {
  493. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  494. clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
  495. clear_node_cpumask(cpu); /* was set by numa_add_cpu */
  496. cpu_clear(cpu, cpu_present_map);
  497. cpu_clear(cpu, cpu_possible_map);
  498. per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
  499. return -EIO;
  500. }
  501. return 0;
  502. }
  503. cycles_t cacheflush_time;
  504. unsigned long cache_decay_ticks;
  505. /*
  506. * Cleanup possible dangling ends...
  507. */
  508. static __cpuinit void smp_cleanup_boot(void)
  509. {
  510. /*
  511. * Paranoid: Set warm reset code and vector here back
  512. * to default values.
  513. */
  514. CMOS_WRITE(0, 0xf);
  515. /*
  516. * Reset trampoline flag
  517. */
  518. *((volatile int *) phys_to_virt(0x467)) = 0;
  519. }
  520. /*
  521. * Fall back to non SMP mode after errors.
  522. *
  523. * RED-PEN audit/test this more. I bet there is more state messed up here.
  524. */
  525. static __init void disable_smp(void)
  526. {
  527. cpu_present_map = cpumask_of_cpu(0);
  528. cpu_possible_map = cpumask_of_cpu(0);
  529. if (smp_found_config)
  530. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  531. else
  532. phys_cpu_present_map = physid_mask_of_physid(0);
  533. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  534. cpu_set(0, per_cpu(cpu_core_map, 0));
  535. }
  536. /*
  537. * Various sanity checks.
  538. */
  539. static int __init smp_sanity_check(unsigned max_cpus)
  540. {
  541. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  542. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  543. hard_smp_processor_id());
  544. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  545. }
  546. /*
  547. * If we couldn't find an SMP configuration at boot time,
  548. * get out of here now!
  549. */
  550. if (!smp_found_config) {
  551. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  552. disable_smp();
  553. if (APIC_init_uniprocessor())
  554. printk(KERN_NOTICE "Local APIC not detected."
  555. " Using dummy APIC emulation.\n");
  556. return -1;
  557. }
  558. /*
  559. * Should not be necessary because the MP table should list the boot
  560. * CPU too, but we do it for the sake of robustness anyway.
  561. */
  562. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  563. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  564. boot_cpu_id);
  565. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  566. }
  567. /*
  568. * If we couldn't find a local APIC, then get out of here now!
  569. */
  570. if (!cpu_has_apic) {
  571. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  572. boot_cpu_id);
  573. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  574. nr_ioapics = 0;
  575. return -1;
  576. }
  577. /*
  578. * If SMP should be disabled, then really disable it!
  579. */
  580. if (!max_cpus) {
  581. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  582. nr_ioapics = 0;
  583. return -1;
  584. }
  585. return 0;
  586. }
  587. static void __init smp_cpu_index_default(void)
  588. {
  589. int i;
  590. struct cpuinfo_x86 *c;
  591. for_each_cpu_mask(i, cpu_possible_map) {
  592. c = &cpu_data(i);
  593. /* mark all to hotplug */
  594. c->cpu_index = NR_CPUS;
  595. }
  596. }
  597. /*
  598. * Prepare for SMP bootup. The MP table or ACPI has been read
  599. * earlier. Just do some sanity checking here and enable APIC mode.
  600. */
  601. void __init native_smp_prepare_cpus(unsigned int max_cpus)
  602. {
  603. nmi_watchdog_default();
  604. smp_cpu_index_default();
  605. current_cpu_data = boot_cpu_data;
  606. current_thread_info()->cpu = 0; /* needed? */
  607. set_cpu_sibling_map(0);
  608. if (smp_sanity_check(max_cpus) < 0) {
  609. printk(KERN_INFO "SMP disabled\n");
  610. disable_smp();
  611. return;
  612. }
  613. /*
  614. * Switch from PIC to APIC mode.
  615. */
  616. setup_local_APIC();
  617. /*
  618. * Enable IO APIC before setting up error vector
  619. */
  620. if (!skip_ioapic_setup && nr_ioapics)
  621. enable_IO_APIC();
  622. end_local_APIC_setup();
  623. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
  624. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  625. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
  626. /* Or can we switch back to PIC here? */
  627. }
  628. /*
  629. * Now start the IO-APICs
  630. */
  631. if (!skip_ioapic_setup && nr_ioapics)
  632. setup_IO_APIC();
  633. else
  634. nr_ioapics = 0;
  635. /*
  636. * Set up local APIC timer on boot CPU.
  637. */
  638. setup_boot_clock();
  639. printk(KERN_INFO "CPU%d: ", 0);
  640. print_cpu_info(&cpu_data(0));
  641. }
  642. /*
  643. * Early setup to make printk work.
  644. */
  645. void __init native_smp_prepare_boot_cpu(void)
  646. {
  647. int me = smp_processor_id();
  648. /* already set me in cpu_online_map in boot_cpu_init() */
  649. cpu_set(me, cpu_callout_map);
  650. per_cpu(cpu_state, me) = CPU_ONLINE;
  651. }
  652. /*
  653. * Entry point to boot a CPU.
  654. */
  655. int __cpuinit native_cpu_up(unsigned int cpu)
  656. {
  657. int apicid = cpu_present_to_apicid(cpu);
  658. unsigned long flags;
  659. int err;
  660. WARN_ON(irqs_disabled());
  661. Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  662. if (apicid == BAD_APICID || apicid == boot_cpu_id ||
  663. !physid_isset(apicid, phys_cpu_present_map)) {
  664. printk("__cpu_up: bad cpu %d\n", cpu);
  665. return -EINVAL;
  666. }
  667. /*
  668. * Already booted CPU?
  669. */
  670. if (cpu_isset(cpu, cpu_callin_map)) {
  671. Dprintk("do_boot_cpu %d Already started\n", cpu);
  672. return -ENOSYS;
  673. }
  674. /*
  675. * Save current MTRR state in case it was changed since early boot
  676. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  677. */
  678. mtrr_save_state();
  679. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  680. /* Boot it! */
  681. err = do_boot_cpu(cpu, apicid);
  682. if (err < 0) {
  683. Dprintk("do_boot_cpu failed %d\n", err);
  684. return err;
  685. }
  686. /* Unleash the CPU! */
  687. Dprintk("waiting for cpu %d\n", cpu);
  688. /*
  689. * Make sure and check TSC sync:
  690. */
  691. local_irq_save(flags);
  692. check_tsc_sync_source(cpu);
  693. local_irq_restore(flags);
  694. while (!cpu_isset(cpu, cpu_online_map))
  695. cpu_relax();
  696. err = 0;
  697. return err;
  698. }
  699. /*
  700. * Finish the SMP boot.
  701. */
  702. void __init native_smp_cpus_done(unsigned int max_cpus)
  703. {
  704. smp_cleanup_boot();
  705. setup_ioapic_dest();
  706. check_nmi_watchdog();
  707. }