smpboot_64.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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. wmb();
  213. cpu_idle();
  214. }
  215. extern volatile unsigned long init_rsp;
  216. extern void (*initial_code)(void);
  217. #ifdef APIC_DEBUG
  218. static void inquire_remote_apic(int apicid)
  219. {
  220. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  221. char *names[] = { "ID", "VERSION", "SPIV" };
  222. int timeout;
  223. u32 status;
  224. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  225. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  226. printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
  227. /*
  228. * Wait for idle.
  229. */
  230. status = safe_apic_wait_icr_idle();
  231. if (status)
  232. printk(KERN_CONT
  233. "a previous APIC delivery may have failed\n");
  234. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  235. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  236. timeout = 0;
  237. do {
  238. udelay(100);
  239. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  240. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  241. switch (status) {
  242. case APIC_ICR_RR_VALID:
  243. status = apic_read(APIC_RRR);
  244. printk(KERN_CONT "%08x\n", status);
  245. break;
  246. default:
  247. printk(KERN_CONT "failed\n");
  248. }
  249. }
  250. }
  251. #endif
  252. /*
  253. * Kick the secondary to wake up.
  254. */
  255. static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
  256. {
  257. unsigned long send_status, accept_status = 0;
  258. int maxlvt, num_starts, j;
  259. /*
  260. * Be paranoid about clearing APIC errors.
  261. */
  262. if (APIC_INTEGRATED(apic_version[phys_apicid])) {
  263. apic_read_around(APIC_SPIV);
  264. apic_write(APIC_ESR, 0);
  265. apic_read(APIC_ESR);
  266. }
  267. Dprintk("Asserting INIT.\n");
  268. /*
  269. * Turn INIT on target chip
  270. */
  271. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  272. /*
  273. * Send IPI
  274. */
  275. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  276. | APIC_DM_INIT);
  277. Dprintk("Waiting for send to finish...\n");
  278. send_status = safe_apic_wait_icr_idle();
  279. mdelay(10);
  280. Dprintk("Deasserting INIT.\n");
  281. /* Target chip */
  282. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  283. /* Send IPI */
  284. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  285. Dprintk("Waiting for send to finish...\n");
  286. send_status = safe_apic_wait_icr_idle();
  287. mb();
  288. atomic_set(&init_deasserted, 1);
  289. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  290. num_starts = 2;
  291. else
  292. num_starts = 0;
  293. /*
  294. * Paravirt / VMI wants a startup IPI hook here to set up the
  295. * target processor state.
  296. */
  297. startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
  298. (unsigned long) init_rsp);
  299. /*
  300. * Run STARTUP IPI loop.
  301. */
  302. Dprintk("#startup loops: %d.\n", num_starts);
  303. maxlvt = lapic_get_maxlvt();
  304. for (j = 1; j <= num_starts; j++) {
  305. Dprintk("Sending STARTUP #%d.\n",j);
  306. apic_read_around(APIC_SPIV);
  307. apic_write(APIC_ESR, 0);
  308. apic_read(APIC_ESR);
  309. Dprintk("After apic_write.\n");
  310. /*
  311. * STARTUP IPI
  312. */
  313. /* Target chip */
  314. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  315. /* Boot on the stack */
  316. /* Kick the second */
  317. apic_write_around(APIC_ICR, APIC_DM_STARTUP | (start_rip>>12));
  318. /*
  319. * Give the other CPU some time to accept the IPI.
  320. */
  321. udelay(300);
  322. Dprintk("Startup point 1.\n");
  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. 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. if (send_status || accept_status)
  338. break;
  339. }
  340. Dprintk("After Startup.\n");
  341. if (send_status)
  342. printk(KERN_ERR "APIC never delivered???\n");
  343. if (accept_status)
  344. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  345. return (send_status | accept_status);
  346. }
  347. struct create_idle {
  348. struct work_struct work;
  349. struct task_struct *idle;
  350. struct completion done;
  351. int cpu;
  352. };
  353. static void __cpuinit do_fork_idle(struct work_struct *work)
  354. {
  355. struct create_idle *c_idle =
  356. container_of(work, struct create_idle, work);
  357. c_idle->idle = fork_idle(c_idle->cpu);
  358. complete(&c_idle->done);
  359. }
  360. /*
  361. * Boot one CPU.
  362. */
  363. static int __cpuinit do_boot_cpu(int cpu, int apicid)
  364. {
  365. unsigned long boot_error;
  366. int timeout;
  367. unsigned long start_rip;
  368. struct create_idle c_idle = {
  369. .cpu = cpu,
  370. .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
  371. };
  372. INIT_WORK(&c_idle.work, do_fork_idle);
  373. /* allocate memory for gdts of secondary cpus. Hotplug is considered */
  374. if (!cpu_gdt_descr[cpu].address &&
  375. !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
  376. printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
  377. return -1;
  378. }
  379. /* Allocate node local memory for AP pdas */
  380. if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
  381. struct x8664_pda *newpda, *pda;
  382. int node = cpu_to_node(cpu);
  383. pda = cpu_pda(cpu);
  384. newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
  385. node);
  386. if (newpda) {
  387. memcpy(newpda, pda, sizeof (struct x8664_pda));
  388. cpu_pda(cpu) = newpda;
  389. } else
  390. printk(KERN_ERR
  391. "Could not allocate node local PDA for CPU %d on node %d\n",
  392. cpu, node);
  393. }
  394. alternatives_smp_switch(1);
  395. c_idle.idle = get_idle_for_cpu(cpu);
  396. if (c_idle.idle) {
  397. c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
  398. (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
  399. init_idle(c_idle.idle, cpu);
  400. goto do_rest;
  401. }
  402. /*
  403. * During cold boot process, keventd thread is not spun up yet.
  404. * When we do cpu hot-add, we create idle threads on the fly, we should
  405. * not acquire any attributes from the calling context. Hence the clean
  406. * way to create kernel_threads() is to do that from keventd().
  407. * We do the current_is_keventd() due to the fact that ACPI notifier
  408. * was also queuing to keventd() and when the caller is already running
  409. * in context of keventd(), we would end up with locking up the keventd
  410. * thread.
  411. */
  412. if (!keventd_up() || current_is_keventd())
  413. c_idle.work.func(&c_idle.work);
  414. else {
  415. schedule_work(&c_idle.work);
  416. wait_for_completion(&c_idle.done);
  417. }
  418. if (IS_ERR(c_idle.idle)) {
  419. printk("failed fork for CPU %d\n", cpu);
  420. return PTR_ERR(c_idle.idle);
  421. }
  422. set_idle_for_cpu(cpu, c_idle.idle);
  423. do_rest:
  424. cpu_pda(cpu)->pcurrent = c_idle.idle;
  425. start_rip = setup_trampoline();
  426. init_rsp = c_idle.idle->thread.sp;
  427. load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
  428. initial_code = start_secondary;
  429. clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
  430. printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
  431. cpus_weight(cpu_present_map),
  432. apicid);
  433. /*
  434. * This grunge runs the startup process for
  435. * the targeted processor.
  436. */
  437. atomic_set(&init_deasserted, 0);
  438. Dprintk("Setting warm reset code and vector.\n");
  439. CMOS_WRITE(0xa, 0xf);
  440. local_flush_tlb();
  441. Dprintk("1.\n");
  442. *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
  443. Dprintk("2.\n");
  444. *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
  445. Dprintk("3.\n");
  446. /*
  447. * Be paranoid about clearing APIC errors.
  448. */
  449. apic_write(APIC_ESR, 0);
  450. apic_read(APIC_ESR);
  451. /*
  452. * Status is now clean
  453. */
  454. boot_error = 0;
  455. /*
  456. * Starting actual IPI sequence...
  457. */
  458. boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
  459. if (!boot_error) {
  460. /*
  461. * allow APs to start initializing.
  462. */
  463. Dprintk("Before Callout %d.\n", cpu);
  464. cpu_set(cpu, cpu_callout_map);
  465. Dprintk("After Callout %d.\n", cpu);
  466. /*
  467. * Wait 5s total for a response
  468. */
  469. for (timeout = 0; timeout < 50000; timeout++) {
  470. if (cpu_isset(cpu, cpu_callin_map))
  471. break; /* It has booted */
  472. udelay(100);
  473. }
  474. if (cpu_isset(cpu, cpu_callin_map)) {
  475. /* number CPUs logically, starting from 1 (BSP is 0) */
  476. Dprintk("CPU has booted.\n");
  477. printk(KERN_INFO "CPU%d: ", cpu);
  478. print_cpu_info(&cpu_data(cpu));
  479. } else {
  480. boot_error = 1;
  481. if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
  482. == 0xA5)
  483. /* trampoline started but...? */
  484. printk("Stuck ??\n");
  485. else
  486. /* trampoline code not run */
  487. printk("Not responding.\n");
  488. #ifdef APIC_DEBUG
  489. inquire_remote_apic(apicid);
  490. #endif
  491. }
  492. }
  493. if (boot_error) {
  494. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  495. clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
  496. clear_node_cpumask(cpu); /* was set by numa_add_cpu */
  497. cpu_clear(cpu, cpu_present_map);
  498. cpu_clear(cpu, cpu_possible_map);
  499. per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
  500. return -EIO;
  501. }
  502. return 0;
  503. }
  504. cycles_t cacheflush_time;
  505. unsigned long cache_decay_ticks;
  506. /*
  507. * Cleanup possible dangling ends...
  508. */
  509. static __cpuinit void smp_cleanup_boot(void)
  510. {
  511. /*
  512. * Paranoid: Set warm reset code and vector here back
  513. * to default values.
  514. */
  515. CMOS_WRITE(0, 0xf);
  516. /*
  517. * Reset trampoline flag
  518. */
  519. *((volatile int *) phys_to_virt(0x467)) = 0;
  520. }
  521. /*
  522. * Fall back to non SMP mode after errors.
  523. *
  524. * RED-PEN audit/test this more. I bet there is more state messed up here.
  525. */
  526. static __init void disable_smp(void)
  527. {
  528. cpu_present_map = cpumask_of_cpu(0);
  529. cpu_possible_map = cpumask_of_cpu(0);
  530. if (smp_found_config)
  531. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  532. else
  533. phys_cpu_present_map = physid_mask_of_physid(0);
  534. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  535. cpu_set(0, per_cpu(cpu_core_map, 0));
  536. }
  537. /*
  538. * Various sanity checks.
  539. */
  540. static int __init smp_sanity_check(unsigned max_cpus)
  541. {
  542. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  543. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  544. hard_smp_processor_id());
  545. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  546. }
  547. /*
  548. * If we couldn't find an SMP configuration at boot time,
  549. * get out of here now!
  550. */
  551. if (!smp_found_config) {
  552. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  553. disable_smp();
  554. if (APIC_init_uniprocessor())
  555. printk(KERN_NOTICE "Local APIC not detected."
  556. " Using dummy APIC emulation.\n");
  557. return -1;
  558. }
  559. /*
  560. * Should not be necessary because the MP table should list the boot
  561. * CPU too, but we do it for the sake of robustness anyway.
  562. */
  563. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  564. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  565. boot_cpu_id);
  566. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  567. }
  568. /*
  569. * If we couldn't find a local APIC, then get out of here now!
  570. */
  571. if (!cpu_has_apic) {
  572. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  573. boot_cpu_id);
  574. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  575. nr_ioapics = 0;
  576. return -1;
  577. }
  578. /*
  579. * If SMP should be disabled, then really disable it!
  580. */
  581. if (!max_cpus) {
  582. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  583. nr_ioapics = 0;
  584. return -1;
  585. }
  586. return 0;
  587. }
  588. static void __init smp_cpu_index_default(void)
  589. {
  590. int i;
  591. struct cpuinfo_x86 *c;
  592. for_each_cpu_mask(i, cpu_possible_map) {
  593. c = &cpu_data(i);
  594. /* mark all to hotplug */
  595. c->cpu_index = NR_CPUS;
  596. }
  597. }
  598. /*
  599. * Prepare for SMP bootup. The MP table or ACPI has been read
  600. * earlier. Just do some sanity checking here and enable APIC mode.
  601. */
  602. void __init native_smp_prepare_cpus(unsigned int max_cpus)
  603. {
  604. nmi_watchdog_default();
  605. smp_cpu_index_default();
  606. current_cpu_data = boot_cpu_data;
  607. current_thread_info()->cpu = 0; /* needed? */
  608. set_cpu_sibling_map(0);
  609. if (smp_sanity_check(max_cpus) < 0) {
  610. printk(KERN_INFO "SMP disabled\n");
  611. disable_smp();
  612. return;
  613. }
  614. /*
  615. * Switch from PIC to APIC mode.
  616. */
  617. setup_local_APIC();
  618. /*
  619. * Enable IO APIC before setting up error vector
  620. */
  621. if (!skip_ioapic_setup && nr_ioapics)
  622. enable_IO_APIC();
  623. end_local_APIC_setup();
  624. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
  625. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  626. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
  627. /* Or can we switch back to PIC here? */
  628. }
  629. /*
  630. * Now start the IO-APICs
  631. */
  632. if (!skip_ioapic_setup && nr_ioapics)
  633. setup_IO_APIC();
  634. else
  635. nr_ioapics = 0;
  636. /*
  637. * Set up local APIC timer on boot CPU.
  638. */
  639. setup_boot_clock();
  640. printk(KERN_INFO "CPU%d: ", 0);
  641. print_cpu_info(&cpu_data(0));
  642. }
  643. /*
  644. * Early setup to make printk work.
  645. */
  646. void __init native_smp_prepare_boot_cpu(void)
  647. {
  648. int me = smp_processor_id();
  649. /* already set me in cpu_online_map in boot_cpu_init() */
  650. cpu_set(me, cpu_callout_map);
  651. per_cpu(cpu_state, me) = CPU_ONLINE;
  652. }
  653. /*
  654. * Entry point to boot a CPU.
  655. */
  656. int __cpuinit native_cpu_up(unsigned int cpu)
  657. {
  658. int apicid = cpu_present_to_apicid(cpu);
  659. unsigned long flags;
  660. int err;
  661. WARN_ON(irqs_disabled());
  662. Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  663. if (apicid == BAD_APICID || apicid == boot_cpu_id ||
  664. !physid_isset(apicid, phys_cpu_present_map)) {
  665. printk("__cpu_up: bad cpu %d\n", cpu);
  666. return -EINVAL;
  667. }
  668. /*
  669. * Already booted CPU?
  670. */
  671. if (cpu_isset(cpu, cpu_callin_map)) {
  672. Dprintk("do_boot_cpu %d Already started\n", cpu);
  673. return -ENOSYS;
  674. }
  675. /*
  676. * Save current MTRR state in case it was changed since early boot
  677. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  678. */
  679. mtrr_save_state();
  680. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  681. /* Boot it! */
  682. err = do_boot_cpu(cpu, apicid);
  683. if (err < 0) {
  684. Dprintk("do_boot_cpu failed %d\n", err);
  685. return err;
  686. }
  687. /* Unleash the CPU! */
  688. Dprintk("waiting for cpu %d\n", cpu);
  689. /*
  690. * Make sure and check TSC sync:
  691. */
  692. local_irq_save(flags);
  693. check_tsc_sync_source(cpu);
  694. local_irq_restore(flags);
  695. while (!cpu_isset(cpu, cpu_online_map))
  696. cpu_relax();
  697. err = 0;
  698. return err;
  699. }
  700. /*
  701. * Finish the SMP boot.
  702. */
  703. void __init native_smp_cpus_done(unsigned int max_cpus)
  704. {
  705. smp_cleanup_boot();
  706. setup_ioapic_dest();
  707. check_nmi_watchdog();
  708. }