smpboot_64.c 22 KB

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