smpboot_64.c 20 KB

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