smpboot_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. /*
  81. * The bootstrap kernel entry code has set these up. Save them for
  82. * a given CPU
  83. */
  84. static void __cpuinit smp_store_cpu_info(int id)
  85. {
  86. struct cpuinfo_x86 *c = &cpu_data(id);
  87. *c = boot_cpu_data;
  88. c->cpu_index = id;
  89. identify_cpu(c);
  90. print_cpu_info(c);
  91. }
  92. static inline void wait_for_init_deassert(atomic_t *deassert)
  93. {
  94. while (!atomic_read(deassert))
  95. cpu_relax();
  96. return;
  97. }
  98. static atomic_t init_deasserted __cpuinitdata;
  99. /*
  100. * Report back to the Boot Processor.
  101. * Running on AP.
  102. */
  103. void __cpuinit smp_callin(void)
  104. {
  105. int cpuid, phys_id;
  106. unsigned long timeout;
  107. /*
  108. * If waken up by an INIT in an 82489DX configuration
  109. * we may get here before an INIT-deassert IPI reaches
  110. * our local APIC. We have to wait for the IPI or we'll
  111. * lock up on an APIC access.
  112. */
  113. wait_for_init_deassert(&init_deasserted);
  114. /*
  115. * (This works even if the APIC is not enabled.)
  116. */
  117. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  118. cpuid = smp_processor_id();
  119. if (cpu_isset(cpuid, cpu_callin_map)) {
  120. panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
  121. phys_id, cpuid);
  122. }
  123. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  124. /*
  125. * STARTUP IPIs are fragile beasts as they might sometimes
  126. * trigger some glue motherboard logic. Complete APIC bus
  127. * silence for 1 second, this overestimates the time the
  128. * boot CPU is spending to send the up to 2 STARTUP IPIs
  129. * by a factor of two. This should be enough.
  130. */
  131. /*
  132. * Waiting 2s total for startup (udelay is not yet working)
  133. */
  134. timeout = jiffies + 2*HZ;
  135. while (time_before(jiffies, timeout)) {
  136. /*
  137. * Has the boot CPU finished it's STARTUP sequence?
  138. */
  139. if (cpu_isset(cpuid, cpu_callout_map))
  140. break;
  141. cpu_relax();
  142. }
  143. if (!time_before(jiffies, timeout)) {
  144. panic("smp_callin: CPU%d started up but did not get a callout!\n",
  145. cpuid);
  146. }
  147. /*
  148. * the boot CPU has finished the init stage and is spinning
  149. * on callin_map until we finish. We are free to set up this
  150. * CPU, first the APIC. (this is probably redundant on most
  151. * boards)
  152. */
  153. Dprintk("CALLIN, before setup_local_APIC().\n");
  154. setup_local_APIC();
  155. end_local_APIC_setup();
  156. /*
  157. * Get our bogomips.
  158. *
  159. * Need to enable IRQs because it can take longer and then
  160. * the NMI watchdog might kill us.
  161. */
  162. local_irq_enable();
  163. calibrate_delay();
  164. local_irq_disable();
  165. Dprintk("Stack at about %p\n",&cpuid);
  166. /*
  167. * Save our processor parameters
  168. */
  169. smp_store_cpu_info(cpuid);
  170. /*
  171. * Allow the master to continue.
  172. */
  173. cpu_set(cpuid, cpu_callin_map);
  174. }
  175. /*
  176. * Setup code on secondary processor (after comming out of the trampoline)
  177. */
  178. void __cpuinit start_secondary(void)
  179. {
  180. /*
  181. * Dont put anything before smp_callin(), SMP
  182. * booting is too fragile that we want to limit the
  183. * things done here to the most necessary things.
  184. */
  185. cpu_init();
  186. preempt_disable();
  187. smp_callin();
  188. /* otherwise gcc will move up the smp_processor_id before the cpu_init */
  189. barrier();
  190. /*
  191. * Check TSC sync first:
  192. */
  193. check_tsc_sync_target();
  194. if (nmi_watchdog == NMI_IO_APIC) {
  195. disable_8259A_irq(0);
  196. enable_NMI_through_LVT0();
  197. enable_8259A_irq(0);
  198. }
  199. /*
  200. * The sibling maps must be set before turing the online map on for
  201. * this cpu
  202. */
  203. set_cpu_sibling_map(smp_processor_id());
  204. /*
  205. * We need to hold call_lock, so there is no inconsistency
  206. * between the time smp_call_function() determines number of
  207. * IPI recipients, and the time when the determination is made
  208. * for which cpus receive the IPI in genapic_flat.c. Holding this
  209. * lock helps us to not include this cpu in a currently in progress
  210. * smp_call_function().
  211. */
  212. lock_ipi_call_lock();
  213. spin_lock(&vector_lock);
  214. /* Setup the per cpu irq handling data structures */
  215. __setup_vector_irq(smp_processor_id());
  216. /*
  217. * Allow the master to continue.
  218. */
  219. spin_unlock(&vector_lock);
  220. cpu_set(smp_processor_id(), cpu_online_map);
  221. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  222. unlock_ipi_call_lock();
  223. setup_secondary_clock();
  224. cpu_idle();
  225. }
  226. extern volatile unsigned long init_rsp;
  227. extern void (*initial_code)(void);
  228. #ifdef APIC_DEBUG
  229. static void inquire_remote_apic(int apicid)
  230. {
  231. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  232. char *names[] = { "ID", "VERSION", "SPIV" };
  233. int timeout;
  234. u32 status;
  235. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  236. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  237. printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
  238. /*
  239. * Wait for idle.
  240. */
  241. status = safe_apic_wait_icr_idle();
  242. if (status)
  243. printk(KERN_CONT
  244. "a previous APIC delivery may have failed\n");
  245. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  246. apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
  247. timeout = 0;
  248. do {
  249. udelay(100);
  250. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  251. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  252. switch (status) {
  253. case APIC_ICR_RR_VALID:
  254. status = apic_read(APIC_RRR);
  255. printk(KERN_CONT "%08x\n", status);
  256. break;
  257. default:
  258. printk(KERN_CONT "failed\n");
  259. }
  260. }
  261. }
  262. #endif
  263. /*
  264. * Kick the secondary to wake up.
  265. */
  266. static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
  267. {
  268. unsigned long send_status, accept_status = 0;
  269. int maxlvt, num_starts, j;
  270. Dprintk("Asserting INIT.\n");
  271. /*
  272. * Turn INIT on target chip
  273. */
  274. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  275. /*
  276. * Send IPI
  277. */
  278. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  279. | APIC_DM_INIT);
  280. Dprintk("Waiting for send to finish...\n");
  281. send_status = safe_apic_wait_icr_idle();
  282. mdelay(10);
  283. Dprintk("Deasserting INIT.\n");
  284. /* Target chip */
  285. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  286. /* Send IPI */
  287. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  288. Dprintk("Waiting for send to finish...\n");
  289. send_status = safe_apic_wait_icr_idle();
  290. mb();
  291. atomic_set(&init_deasserted, 1);
  292. num_starts = 2;
  293. /*
  294. * Run STARTUP IPI loop.
  295. */
  296. Dprintk("#startup loops: %d.\n", num_starts);
  297. maxlvt = lapic_get_maxlvt();
  298. for (j = 1; j <= num_starts; j++) {
  299. Dprintk("Sending STARTUP #%d.\n",j);
  300. apic_write(APIC_ESR, 0);
  301. apic_read(APIC_ESR);
  302. Dprintk("After apic_write.\n");
  303. /*
  304. * STARTUP IPI
  305. */
  306. /* Target chip */
  307. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  308. /* Boot on the stack */
  309. /* Kick the second */
  310. apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
  311. /*
  312. * Give the other CPU some time to accept the IPI.
  313. */
  314. udelay(300);
  315. Dprintk("Startup point 1.\n");
  316. Dprintk("Waiting for send to finish...\n");
  317. send_status = safe_apic_wait_icr_idle();
  318. /*
  319. * Give the other CPU some time to accept the IPI.
  320. */
  321. udelay(200);
  322. /*
  323. * Due to the Pentium erratum 3AP.
  324. */
  325. if (maxlvt > 3) {
  326. apic_write(APIC_ESR, 0);
  327. }
  328. accept_status = (apic_read(APIC_ESR) & 0xEF);
  329. if (send_status || accept_status)
  330. break;
  331. }
  332. Dprintk("After Startup.\n");
  333. if (send_status)
  334. printk(KERN_ERR "APIC never delivered???\n");
  335. if (accept_status)
  336. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  337. return (send_status | accept_status);
  338. }
  339. struct create_idle {
  340. struct work_struct work;
  341. struct task_struct *idle;
  342. struct completion done;
  343. int cpu;
  344. };
  345. static void __cpuinit do_fork_idle(struct work_struct *work)
  346. {
  347. struct create_idle *c_idle =
  348. container_of(work, struct create_idle, work);
  349. c_idle->idle = fork_idle(c_idle->cpu);
  350. complete(&c_idle->done);
  351. }
  352. /*
  353. * Boot one CPU.
  354. */
  355. static int __cpuinit do_boot_cpu(int cpu, int apicid)
  356. {
  357. unsigned long boot_error;
  358. int timeout;
  359. unsigned long start_rip;
  360. struct create_idle c_idle = {
  361. .cpu = cpu,
  362. .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
  363. };
  364. INIT_WORK(&c_idle.work, do_fork_idle);
  365. /* allocate memory for gdts of secondary cpus. Hotplug is considered */
  366. if (!cpu_gdt_descr[cpu].address &&
  367. !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
  368. printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
  369. return -1;
  370. }
  371. /* Allocate node local memory for AP pdas */
  372. if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
  373. struct x8664_pda *newpda, *pda;
  374. int node = cpu_to_node(cpu);
  375. pda = cpu_pda(cpu);
  376. newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
  377. node);
  378. if (newpda) {
  379. memcpy(newpda, pda, sizeof (struct x8664_pda));
  380. cpu_pda(cpu) = newpda;
  381. } else
  382. printk(KERN_ERR
  383. "Could not allocate node local PDA for CPU %d on node %d\n",
  384. cpu, node);
  385. }
  386. alternatives_smp_switch(1);
  387. c_idle.idle = get_idle_for_cpu(cpu);
  388. if (c_idle.idle) {
  389. c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
  390. (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
  391. init_idle(c_idle.idle, cpu);
  392. goto do_rest;
  393. }
  394. /*
  395. * During cold boot process, keventd thread is not spun up yet.
  396. * When we do cpu hot-add, we create idle threads on the fly, we should
  397. * not acquire any attributes from the calling context. Hence the clean
  398. * way to create kernel_threads() is to do that from keventd().
  399. * We do the current_is_keventd() due to the fact that ACPI notifier
  400. * was also queuing to keventd() and when the caller is already running
  401. * in context of keventd(), we would end up with locking up the keventd
  402. * thread.
  403. */
  404. if (!keventd_up() || current_is_keventd())
  405. c_idle.work.func(&c_idle.work);
  406. else {
  407. schedule_work(&c_idle.work);
  408. wait_for_completion(&c_idle.done);
  409. }
  410. if (IS_ERR(c_idle.idle)) {
  411. printk("failed fork for CPU %d\n", cpu);
  412. return PTR_ERR(c_idle.idle);
  413. }
  414. set_idle_for_cpu(cpu, c_idle.idle);
  415. do_rest:
  416. cpu_pda(cpu)->pcurrent = c_idle.idle;
  417. start_rip = setup_trampoline();
  418. init_rsp = c_idle.idle->thread.sp;
  419. load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
  420. initial_code = start_secondary;
  421. clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
  422. printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
  423. cpus_weight(cpu_present_map),
  424. apicid);
  425. /*
  426. * This grunge runs the startup process for
  427. * the targeted processor.
  428. */
  429. atomic_set(&init_deasserted, 0);
  430. Dprintk("Setting warm reset code and vector.\n");
  431. CMOS_WRITE(0xa, 0xf);
  432. local_flush_tlb();
  433. Dprintk("1.\n");
  434. *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
  435. Dprintk("2.\n");
  436. *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
  437. Dprintk("3.\n");
  438. /*
  439. * Be paranoid about clearing APIC errors.
  440. */
  441. apic_write(APIC_ESR, 0);
  442. apic_read(APIC_ESR);
  443. /*
  444. * Status is now clean
  445. */
  446. boot_error = 0;
  447. /*
  448. * Starting actual IPI sequence...
  449. */
  450. boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
  451. if (!boot_error) {
  452. /*
  453. * allow APs to start initializing.
  454. */
  455. Dprintk("Before Callout %d.\n", cpu);
  456. cpu_set(cpu, cpu_callout_map);
  457. Dprintk("After Callout %d.\n", cpu);
  458. /*
  459. * Wait 5s total for a response
  460. */
  461. for (timeout = 0; timeout < 50000; timeout++) {
  462. if (cpu_isset(cpu, cpu_callin_map))
  463. break; /* It has booted */
  464. udelay(100);
  465. }
  466. if (cpu_isset(cpu, cpu_callin_map)) {
  467. /* number CPUs logically, starting from 1 (BSP is 0) */
  468. Dprintk("CPU has booted.\n");
  469. } else {
  470. boot_error = 1;
  471. if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
  472. == 0xA5)
  473. /* trampoline started but...? */
  474. printk("Stuck ??\n");
  475. else
  476. /* trampoline code not run */
  477. printk("Not responding.\n");
  478. #ifdef APIC_DEBUG
  479. inquire_remote_apic(apicid);
  480. #endif
  481. }
  482. }
  483. if (boot_error) {
  484. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  485. clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
  486. clear_node_cpumask(cpu); /* was set by numa_add_cpu */
  487. cpu_clear(cpu, cpu_present_map);
  488. cpu_clear(cpu, cpu_possible_map);
  489. per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
  490. return -EIO;
  491. }
  492. return 0;
  493. }
  494. cycles_t cacheflush_time;
  495. unsigned long cache_decay_ticks;
  496. /*
  497. * Cleanup possible dangling ends...
  498. */
  499. static __cpuinit void smp_cleanup_boot(void)
  500. {
  501. /*
  502. * Paranoid: Set warm reset code and vector here back
  503. * to default values.
  504. */
  505. CMOS_WRITE(0, 0xf);
  506. /*
  507. * Reset trampoline flag
  508. */
  509. *((volatile int *) phys_to_virt(0x467)) = 0;
  510. }
  511. /*
  512. * Fall back to non SMP mode after errors.
  513. *
  514. * RED-PEN audit/test this more. I bet there is more state messed up here.
  515. */
  516. static __init void disable_smp(void)
  517. {
  518. cpu_present_map = cpumask_of_cpu(0);
  519. cpu_possible_map = cpumask_of_cpu(0);
  520. if (smp_found_config)
  521. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  522. else
  523. phys_cpu_present_map = physid_mask_of_physid(0);
  524. cpu_set(0, per_cpu(cpu_sibling_map, 0));
  525. cpu_set(0, per_cpu(cpu_core_map, 0));
  526. }
  527. /*
  528. * Various sanity checks.
  529. */
  530. static int __init smp_sanity_check(unsigned max_cpus)
  531. {
  532. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  533. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  534. hard_smp_processor_id());
  535. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  536. }
  537. /*
  538. * If we couldn't find an SMP configuration at boot time,
  539. * get out of here now!
  540. */
  541. if (!smp_found_config) {
  542. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  543. disable_smp();
  544. if (APIC_init_uniprocessor())
  545. printk(KERN_NOTICE "Local APIC not detected."
  546. " Using dummy APIC emulation.\n");
  547. return -1;
  548. }
  549. /*
  550. * Should not be necessary because the MP table should list the boot
  551. * CPU too, but we do it for the sake of robustness anyway.
  552. */
  553. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  554. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  555. boot_cpu_id);
  556. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  557. }
  558. /*
  559. * If we couldn't find a local APIC, then get out of here now!
  560. */
  561. if (!cpu_has_apic) {
  562. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  563. boot_cpu_id);
  564. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  565. nr_ioapics = 0;
  566. return -1;
  567. }
  568. /*
  569. * If SMP should be disabled, then really disable it!
  570. */
  571. if (!max_cpus) {
  572. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  573. nr_ioapics = 0;
  574. return -1;
  575. }
  576. return 0;
  577. }
  578. static void __init smp_cpu_index_default(void)
  579. {
  580. int i;
  581. struct cpuinfo_x86 *c;
  582. for_each_cpu_mask(i, cpu_possible_map) {
  583. c = &cpu_data(i);
  584. /* mark all to hotplug */
  585. c->cpu_index = NR_CPUS;
  586. }
  587. }
  588. /*
  589. * Prepare for SMP bootup. The MP table or ACPI has been read
  590. * earlier. Just do some sanity checking here and enable APIC mode.
  591. */
  592. void __init native_smp_prepare_cpus(unsigned int max_cpus)
  593. {
  594. nmi_watchdog_default();
  595. smp_cpu_index_default();
  596. current_cpu_data = boot_cpu_data;
  597. current_thread_info()->cpu = 0; /* needed? */
  598. set_cpu_sibling_map(0);
  599. if (smp_sanity_check(max_cpus) < 0) {
  600. printk(KERN_INFO "SMP disabled\n");
  601. disable_smp();
  602. return;
  603. }
  604. /*
  605. * Switch from PIC to APIC mode.
  606. */
  607. setup_local_APIC();
  608. /*
  609. * Enable IO APIC before setting up error vector
  610. */
  611. if (!skip_ioapic_setup && nr_ioapics)
  612. enable_IO_APIC();
  613. end_local_APIC_setup();
  614. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
  615. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  616. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
  617. /* Or can we switch back to PIC here? */
  618. }
  619. /*
  620. * Now start the IO-APICs
  621. */
  622. if (!skip_ioapic_setup && nr_ioapics)
  623. setup_IO_APIC();
  624. else
  625. nr_ioapics = 0;
  626. /*
  627. * Set up local APIC timer on boot CPU.
  628. */
  629. setup_boot_clock();
  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. }