smpboot.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. */
  38. #include <linux/config.h>
  39. #include <linux/init.h>
  40. #include <linux/mm.h>
  41. #include <linux/kernel_stat.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/irq.h>
  44. #include <linux/bootmem.h>
  45. #include <linux/thread_info.h>
  46. #include <linux/module.h>
  47. #include <linux/delay.h>
  48. #include <linux/mc146818rtc.h>
  49. #include <asm/mtrr.h>
  50. #include <asm/pgalloc.h>
  51. #include <asm/desc.h>
  52. #include <asm/kdebug.h>
  53. #include <asm/tlbflush.h>
  54. #include <asm/proto.h>
  55. /* Change for real CPU hotplug. Note other files need to be fixed
  56. first too. */
  57. #define __cpuinit __init
  58. #define __cpuinitdata __initdata
  59. /* Number of siblings per CPU package */
  60. int smp_num_siblings = 1;
  61. /* Package ID of each logical CPU */
  62. u8 phys_proc_id[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
  63. u8 cpu_core_id[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
  64. EXPORT_SYMBOL(phys_proc_id);
  65. EXPORT_SYMBOL(cpu_core_id);
  66. /* Bitmask of currently online CPUs */
  67. cpumask_t cpu_online_map;
  68. EXPORT_SYMBOL(cpu_online_map);
  69. /*
  70. * Private maps to synchronize booting between AP and BP.
  71. * Probably not needed anymore, but it makes for easier debugging. -AK
  72. */
  73. cpumask_t cpu_callin_map;
  74. cpumask_t cpu_callout_map;
  75. cpumask_t cpu_possible_map;
  76. EXPORT_SYMBOL(cpu_possible_map);
  77. /* Per CPU bogomips and other parameters */
  78. struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
  79. /* Set when the idlers are all forked */
  80. int smp_threads_ready;
  81. cpumask_t cpu_sibling_map[NR_CPUS] __cacheline_aligned;
  82. cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
  83. /*
  84. * Trampoline 80x86 program as an array.
  85. */
  86. extern unsigned char trampoline_data[];
  87. extern unsigned char trampoline_end[];
  88. /*
  89. * Currently trivial. Write the real->protected mode
  90. * bootstrap into the page concerned. The caller
  91. * has made sure it's suitably aligned.
  92. */
  93. static unsigned long __cpuinit setup_trampoline(void)
  94. {
  95. void *tramp = __va(SMP_TRAMPOLINE_BASE);
  96. memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
  97. return virt_to_phys(tramp);
  98. }
  99. /*
  100. * The bootstrap kernel entry code has set these up. Save them for
  101. * a given CPU
  102. */
  103. static void __cpuinit smp_store_cpu_info(int id)
  104. {
  105. struct cpuinfo_x86 *c = cpu_data + id;
  106. *c = boot_cpu_data;
  107. identify_cpu(c);
  108. }
  109. /*
  110. * Synchronize TSCs of CPUs
  111. *
  112. * This new algorithm is less accurate than the old "zero TSCs"
  113. * one, but we cannot zero TSCs anymore in the new hotplug CPU
  114. * model.
  115. */
  116. static atomic_t __cpuinitdata tsc_flag;
  117. static __cpuinitdata DEFINE_SPINLOCK(tsc_sync_lock);
  118. static unsigned long long __cpuinitdata bp_tsc, ap_tsc;
  119. #define NR_LOOPS 5
  120. static void __cpuinit sync_tsc_bp_init(int init)
  121. {
  122. if (init)
  123. _raw_spin_lock(&tsc_sync_lock);
  124. else
  125. _raw_spin_unlock(&tsc_sync_lock);
  126. atomic_set(&tsc_flag, 0);
  127. }
  128. /*
  129. * Synchronize TSC on AP with BP.
  130. */
  131. static void __cpuinit __sync_tsc_ap(void)
  132. {
  133. if (!cpu_has_tsc)
  134. return;
  135. Dprintk("AP %d syncing TSC\n", smp_processor_id());
  136. while (atomic_read(&tsc_flag) != 0)
  137. cpu_relax();
  138. atomic_inc(&tsc_flag);
  139. mb();
  140. _raw_spin_lock(&tsc_sync_lock);
  141. wrmsrl(MSR_IA32_TSC, bp_tsc);
  142. _raw_spin_unlock(&tsc_sync_lock);
  143. rdtscll(ap_tsc);
  144. mb();
  145. atomic_inc(&tsc_flag);
  146. mb();
  147. }
  148. static void __cpuinit sync_tsc_ap(void)
  149. {
  150. int i;
  151. for (i = 0; i < NR_LOOPS; i++)
  152. __sync_tsc_ap();
  153. }
  154. /*
  155. * Synchronize TSC from BP to AP.
  156. */
  157. static void __cpuinit __sync_tsc_bp(int cpu)
  158. {
  159. if (!cpu_has_tsc)
  160. return;
  161. /* Wait for AP */
  162. while (atomic_read(&tsc_flag) == 0)
  163. cpu_relax();
  164. /* Save BPs TSC */
  165. sync_core();
  166. rdtscll(bp_tsc);
  167. /* Don't do the sync core here to avoid too much latency. */
  168. mb();
  169. /* Start the AP */
  170. _raw_spin_unlock(&tsc_sync_lock);
  171. /* Wait for AP again */
  172. while (atomic_read(&tsc_flag) < 2)
  173. cpu_relax();
  174. rdtscl(bp_tsc);
  175. barrier();
  176. }
  177. static void __cpuinit sync_tsc_bp(int cpu)
  178. {
  179. int i;
  180. for (i = 0; i < NR_LOOPS - 1; i++) {
  181. __sync_tsc_bp(cpu);
  182. sync_tsc_bp_init(1);
  183. }
  184. __sync_tsc_bp(cpu);
  185. printk(KERN_INFO "Synced TSC of CPU %d difference %Ld\n",
  186. cpu, ap_tsc - bp_tsc);
  187. }
  188. static atomic_t init_deasserted __cpuinitdata;
  189. /*
  190. * Report back to the Boot Processor.
  191. * Running on AP.
  192. */
  193. void __cpuinit smp_callin(void)
  194. {
  195. int cpuid, phys_id;
  196. unsigned long timeout;
  197. /*
  198. * If waken up by an INIT in an 82489DX configuration
  199. * we may get here before an INIT-deassert IPI reaches
  200. * our local APIC. We have to wait for the IPI or we'll
  201. * lock up on an APIC access.
  202. */
  203. while (!atomic_read(&init_deasserted))
  204. cpu_relax();
  205. /*
  206. * (This works even if the APIC is not enabled.)
  207. */
  208. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  209. cpuid = smp_processor_id();
  210. if (cpu_isset(cpuid, cpu_callin_map)) {
  211. panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
  212. phys_id, cpuid);
  213. }
  214. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  215. /*
  216. * STARTUP IPIs are fragile beasts as they might sometimes
  217. * trigger some glue motherboard logic. Complete APIC bus
  218. * silence for 1 second, this overestimates the time the
  219. * boot CPU is spending to send the up to 2 STARTUP IPIs
  220. * by a factor of two. This should be enough.
  221. */
  222. /*
  223. * Waiting 2s total for startup (udelay is not yet working)
  224. */
  225. timeout = jiffies + 2*HZ;
  226. while (time_before(jiffies, timeout)) {
  227. /*
  228. * Has the boot CPU finished it's STARTUP sequence?
  229. */
  230. if (cpu_isset(cpuid, cpu_callout_map))
  231. break;
  232. cpu_relax();
  233. }
  234. if (!time_before(jiffies, timeout)) {
  235. panic("smp_callin: CPU%d started up but did not get a callout!\n",
  236. cpuid);
  237. }
  238. /*
  239. * the boot CPU has finished the init stage and is spinning
  240. * on callin_map until we finish. We are free to set up this
  241. * CPU, first the APIC. (this is probably redundant on most
  242. * boards)
  243. */
  244. Dprintk("CALLIN, before setup_local_APIC().\n");
  245. setup_local_APIC();
  246. /*
  247. * Get our bogomips.
  248. */
  249. calibrate_delay();
  250. Dprintk("Stack at about %p\n",&cpuid);
  251. disable_APIC_timer();
  252. /*
  253. * Save our processor parameters
  254. */
  255. smp_store_cpu_info(cpuid);
  256. /*
  257. * Allow the master to continue.
  258. */
  259. cpu_set(cpuid, cpu_callin_map);
  260. }
  261. /*
  262. * Setup code on secondary processor (after comming out of the trampoline)
  263. */
  264. void __cpuinit start_secondary(void)
  265. {
  266. /*
  267. * Dont put anything before smp_callin(), SMP
  268. * booting is too fragile that we want to limit the
  269. * things done here to the most necessary things.
  270. */
  271. cpu_init();
  272. smp_callin();
  273. /*
  274. * Synchronize the TSC with the BP
  275. */
  276. sync_tsc_ap();
  277. /* otherwise gcc will move up the smp_processor_id before the cpu_init */
  278. barrier();
  279. Dprintk("cpu %d: setting up apic clock\n", smp_processor_id());
  280. setup_secondary_APIC_clock();
  281. Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
  282. if (nmi_watchdog == NMI_IO_APIC) {
  283. disable_8259A_irq(0);
  284. enable_NMI_through_LVT0(NULL);
  285. enable_8259A_irq(0);
  286. }
  287. enable_APIC_timer();
  288. /*
  289. * Allow the master to continue.
  290. */
  291. cpu_set(smp_processor_id(), cpu_online_map);
  292. mb();
  293. cpu_idle();
  294. }
  295. extern volatile unsigned long init_rsp;
  296. extern void (*initial_code)(void);
  297. #if APIC_DEBUG
  298. static void inquire_remote_apic(int apicid)
  299. {
  300. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  301. char *names[] = { "ID", "VERSION", "SPIV" };
  302. int timeout, status;
  303. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  304. for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
  305. printk("... APIC #%d %s: ", apicid, names[i]);
  306. /*
  307. * Wait for idle.
  308. */
  309. apic_wait_icr_idle();
  310. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  311. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  312. timeout = 0;
  313. do {
  314. udelay(100);
  315. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  316. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  317. switch (status) {
  318. case APIC_ICR_RR_VALID:
  319. status = apic_read(APIC_RRR);
  320. printk("%08x\n", status);
  321. break;
  322. default:
  323. printk("failed\n");
  324. }
  325. }
  326. }
  327. #endif
  328. /*
  329. * Kick the secondary to wake up.
  330. */
  331. static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
  332. {
  333. unsigned long send_status = 0, accept_status = 0;
  334. int maxlvt, timeout, num_starts, j;
  335. Dprintk("Asserting INIT.\n");
  336. /*
  337. * Turn INIT on target chip
  338. */
  339. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  340. /*
  341. * Send IPI
  342. */
  343. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  344. | APIC_DM_INIT);
  345. Dprintk("Waiting for send to finish...\n");
  346. timeout = 0;
  347. do {
  348. Dprintk("+");
  349. udelay(100);
  350. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  351. } while (send_status && (timeout++ < 1000));
  352. mdelay(10);
  353. Dprintk("Deasserting INIT.\n");
  354. /* Target chip */
  355. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  356. /* Send IPI */
  357. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  358. Dprintk("Waiting for send to finish...\n");
  359. timeout = 0;
  360. do {
  361. Dprintk("+");
  362. udelay(100);
  363. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  364. } while (send_status && (timeout++ < 1000));
  365. atomic_set(&init_deasserted, 1);
  366. /*
  367. * Should we send STARTUP IPIs ?
  368. *
  369. * Determine this based on the APIC version.
  370. * If we don't have an integrated APIC, don't send the STARTUP IPIs.
  371. */
  372. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  373. num_starts = 2;
  374. else
  375. num_starts = 0;
  376. /*
  377. * Run STARTUP IPI loop.
  378. */
  379. Dprintk("#startup loops: %d.\n", num_starts);
  380. maxlvt = get_maxlvt();
  381. for (j = 1; j <= num_starts; j++) {
  382. Dprintk("Sending STARTUP #%d.\n",j);
  383. apic_read_around(APIC_SPIV);
  384. apic_write(APIC_ESR, 0);
  385. apic_read(APIC_ESR);
  386. Dprintk("After apic_write.\n");
  387. /*
  388. * STARTUP IPI
  389. */
  390. /* Target chip */
  391. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  392. /* Boot on the stack */
  393. /* Kick the second */
  394. apic_write_around(APIC_ICR, APIC_DM_STARTUP
  395. | (start_rip >> 12));
  396. /*
  397. * Give the other CPU some time to accept the IPI.
  398. */
  399. udelay(300);
  400. Dprintk("Startup point 1.\n");
  401. Dprintk("Waiting for send to finish...\n");
  402. timeout = 0;
  403. do {
  404. Dprintk("+");
  405. udelay(100);
  406. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  407. } while (send_status && (timeout++ < 1000));
  408. /*
  409. * Give the other CPU some time to accept the IPI.
  410. */
  411. udelay(200);
  412. /*
  413. * Due to the Pentium erratum 3AP.
  414. */
  415. if (maxlvt > 3) {
  416. apic_read_around(APIC_SPIV);
  417. apic_write(APIC_ESR, 0);
  418. }
  419. accept_status = (apic_read(APIC_ESR) & 0xEF);
  420. if (send_status || accept_status)
  421. break;
  422. }
  423. Dprintk("After Startup.\n");
  424. if (send_status)
  425. printk(KERN_ERR "APIC never delivered???\n");
  426. if (accept_status)
  427. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  428. return (send_status | accept_status);
  429. }
  430. /*
  431. * Boot one CPU.
  432. */
  433. static int __cpuinit do_boot_cpu(int cpu, int apicid)
  434. {
  435. struct task_struct *idle;
  436. unsigned long boot_error;
  437. int timeout;
  438. unsigned long start_rip;
  439. /*
  440. * We can't use kernel_thread since we must avoid to
  441. * reschedule the child.
  442. */
  443. idle = fork_idle(cpu);
  444. if (IS_ERR(idle)) {
  445. printk("failed fork for CPU %d\n", cpu);
  446. return PTR_ERR(idle);
  447. }
  448. x86_cpu_to_apicid[cpu] = apicid;
  449. cpu_pda[cpu].pcurrent = idle;
  450. start_rip = setup_trampoline();
  451. init_rsp = idle->thread.rsp;
  452. per_cpu(init_tss,cpu).rsp0 = init_rsp;
  453. initial_code = start_secondary;
  454. clear_ti_thread_flag(idle->thread_info, TIF_FORK);
  455. printk(KERN_INFO "Booting processor %d/%d rip %lx rsp %lx\n", cpu, apicid,
  456. start_rip, init_rsp);
  457. /*
  458. * This grunge runs the startup process for
  459. * the targeted processor.
  460. */
  461. atomic_set(&init_deasserted, 0);
  462. Dprintk("Setting warm reset code and vector.\n");
  463. CMOS_WRITE(0xa, 0xf);
  464. local_flush_tlb();
  465. Dprintk("1.\n");
  466. *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
  467. Dprintk("2.\n");
  468. *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
  469. Dprintk("3.\n");
  470. /*
  471. * Be paranoid about clearing APIC errors.
  472. */
  473. if (APIC_INTEGRATED(apic_version[apicid])) {
  474. apic_read_around(APIC_SPIV);
  475. apic_write(APIC_ESR, 0);
  476. apic_read(APIC_ESR);
  477. }
  478. /*
  479. * Status is now clean
  480. */
  481. boot_error = 0;
  482. /*
  483. * Starting actual IPI sequence...
  484. */
  485. boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
  486. if (!boot_error) {
  487. /*
  488. * allow APs to start initializing.
  489. */
  490. Dprintk("Before Callout %d.\n", cpu);
  491. cpu_set(cpu, cpu_callout_map);
  492. Dprintk("After Callout %d.\n", cpu);
  493. /*
  494. * Wait 5s total for a response
  495. */
  496. for (timeout = 0; timeout < 50000; timeout++) {
  497. if (cpu_isset(cpu, cpu_callin_map))
  498. break; /* It has booted */
  499. udelay(100);
  500. }
  501. if (cpu_isset(cpu, cpu_callin_map)) {
  502. /* number CPUs logically, starting from 1 (BSP is 0) */
  503. Dprintk("OK.\n");
  504. print_cpu_info(&cpu_data[cpu]);
  505. Dprintk("CPU has booted.\n");
  506. } else {
  507. boot_error = 1;
  508. if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
  509. == 0xA5)
  510. /* trampoline started but...? */
  511. printk("Stuck ??\n");
  512. else
  513. /* trampoline code not run */
  514. printk("Not responding.\n");
  515. #if APIC_DEBUG
  516. inquire_remote_apic(apicid);
  517. #endif
  518. }
  519. }
  520. if (boot_error) {
  521. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  522. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  523. cpu_clear(cpu, cpu_present_map);
  524. cpu_clear(cpu, cpu_possible_map);
  525. x86_cpu_to_apicid[cpu] = BAD_APICID;
  526. x86_cpu_to_log_apicid[cpu] = BAD_APICID;
  527. return -EIO;
  528. }
  529. return 0;
  530. }
  531. cycles_t cacheflush_time;
  532. unsigned long cache_decay_ticks;
  533. /*
  534. * Construct cpu_sibling_map[], so that we can tell the sibling CPU
  535. * on SMT systems efficiently.
  536. */
  537. static __cpuinit void detect_siblings(void)
  538. {
  539. int cpu;
  540. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  541. cpus_clear(cpu_sibling_map[cpu]);
  542. cpus_clear(cpu_core_map[cpu]);
  543. }
  544. for_each_online_cpu (cpu) {
  545. struct cpuinfo_x86 *c = cpu_data + cpu;
  546. int siblings = 0;
  547. int i;
  548. if (smp_num_siblings > 1) {
  549. for_each_online_cpu (i) {
  550. if (cpu_core_id[cpu] == cpu_core_id[i]) {
  551. siblings++;
  552. cpu_set(i, cpu_sibling_map[cpu]);
  553. }
  554. }
  555. } else {
  556. siblings++;
  557. cpu_set(cpu, cpu_sibling_map[cpu]);
  558. }
  559. if (siblings != smp_num_siblings) {
  560. printk(KERN_WARNING
  561. "WARNING: %d siblings found for CPU%d, should be %d\n",
  562. siblings, cpu, smp_num_siblings);
  563. smp_num_siblings = siblings;
  564. }
  565. if (c->x86_num_cores > 1) {
  566. for_each_online_cpu(i) {
  567. if (phys_proc_id[cpu] == phys_proc_id[i])
  568. cpu_set(i, cpu_core_map[cpu]);
  569. }
  570. } else
  571. cpu_core_map[cpu] = cpu_sibling_map[cpu];
  572. }
  573. }
  574. /*
  575. * Cleanup possible dangling ends...
  576. */
  577. static __cpuinit void smp_cleanup_boot(void)
  578. {
  579. /*
  580. * Paranoid: Set warm reset code and vector here back
  581. * to default values.
  582. */
  583. CMOS_WRITE(0, 0xf);
  584. /*
  585. * Reset trampoline flag
  586. */
  587. *((volatile int *) phys_to_virt(0x467)) = 0;
  588. #ifndef CONFIG_HOTPLUG_CPU
  589. /*
  590. * Free pages reserved for SMP bootup.
  591. * When you add hotplug CPU support later remove this
  592. * Note there is more work to be done for later CPU bootup.
  593. */
  594. free_page((unsigned long) __va(PAGE_SIZE));
  595. free_page((unsigned long) __va(SMP_TRAMPOLINE_BASE));
  596. #endif
  597. }
  598. /*
  599. * Fall back to non SMP mode after errors.
  600. *
  601. * RED-PEN audit/test this more. I bet there is more state messed up here.
  602. */
  603. static __cpuinit void disable_smp(void)
  604. {
  605. cpu_present_map = cpumask_of_cpu(0);
  606. cpu_possible_map = cpumask_of_cpu(0);
  607. if (smp_found_config)
  608. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  609. else
  610. phys_cpu_present_map = physid_mask_of_physid(0);
  611. cpu_set(0, cpu_sibling_map[0]);
  612. cpu_set(0, cpu_core_map[0]);
  613. }
  614. /*
  615. * Handle user cpus=... parameter.
  616. */
  617. static __cpuinit void enforce_max_cpus(unsigned max_cpus)
  618. {
  619. int i, k;
  620. k = 0;
  621. for (i = 0; i < NR_CPUS; i++) {
  622. if (!cpu_possible(i))
  623. continue;
  624. if (++k > max_cpus) {
  625. cpu_clear(i, cpu_possible_map);
  626. cpu_clear(i, cpu_present_map);
  627. }
  628. }
  629. }
  630. /*
  631. * Various sanity checks.
  632. */
  633. static int __cpuinit smp_sanity_check(unsigned max_cpus)
  634. {
  635. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  636. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  637. hard_smp_processor_id());
  638. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  639. }
  640. /*
  641. * If we couldn't find an SMP configuration at boot time,
  642. * get out of here now!
  643. */
  644. if (!smp_found_config) {
  645. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  646. disable_smp();
  647. if (APIC_init_uniprocessor())
  648. printk(KERN_NOTICE "Local APIC not detected."
  649. " Using dummy APIC emulation.\n");
  650. return -1;
  651. }
  652. /*
  653. * Should not be necessary because the MP table should list the boot
  654. * CPU too, but we do it for the sake of robustness anyway.
  655. */
  656. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  657. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  658. boot_cpu_id);
  659. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  660. }
  661. /*
  662. * If we couldn't find a local APIC, then get out of here now!
  663. */
  664. if (APIC_INTEGRATED(apic_version[boot_cpu_id]) && !cpu_has_apic) {
  665. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  666. boot_cpu_id);
  667. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  668. nr_ioapics = 0;
  669. return -1;
  670. }
  671. /*
  672. * If SMP should be disabled, then really disable it!
  673. */
  674. if (!max_cpus) {
  675. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  676. nr_ioapics = 0;
  677. return -1;
  678. }
  679. return 0;
  680. }
  681. /*
  682. * Prepare for SMP bootup. The MP table or ACPI has been read
  683. * earlier. Just do some sanity checking here and enable APIC mode.
  684. */
  685. void __cpuinit smp_prepare_cpus(unsigned int max_cpus)
  686. {
  687. int i;
  688. nmi_watchdog_default();
  689. current_cpu_data = boot_cpu_data;
  690. current_thread_info()->cpu = 0; /* needed? */
  691. enforce_max_cpus(max_cpus);
  692. /*
  693. * Fill in cpu_present_mask
  694. */
  695. for (i = 0; i < NR_CPUS; i++) {
  696. int apicid = cpu_present_to_apicid(i);
  697. if (physid_isset(apicid, phys_cpu_present_map)) {
  698. cpu_set(i, cpu_present_map);
  699. /* possible map would be different if we supported real
  700. CPU hotplug. */
  701. cpu_set(i, cpu_possible_map);
  702. }
  703. }
  704. if (smp_sanity_check(max_cpus) < 0) {
  705. printk(KERN_INFO "SMP disabled\n");
  706. disable_smp();
  707. return;
  708. }
  709. /*
  710. * Switch from PIC to APIC mode.
  711. */
  712. connect_bsp_APIC();
  713. setup_local_APIC();
  714. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
  715. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  716. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
  717. /* Or can we switch back to PIC here? */
  718. }
  719. x86_cpu_to_apicid[0] = boot_cpu_id;
  720. /*
  721. * Now start the IO-APICs
  722. */
  723. if (!skip_ioapic_setup && nr_ioapics)
  724. setup_IO_APIC();
  725. else
  726. nr_ioapics = 0;
  727. /*
  728. * Set up local APIC timer on boot CPU.
  729. */
  730. setup_boot_APIC_clock();
  731. }
  732. /*
  733. * Early setup to make printk work.
  734. */
  735. void __init smp_prepare_boot_cpu(void)
  736. {
  737. int me = smp_processor_id();
  738. cpu_set(me, cpu_online_map);
  739. cpu_set(me, cpu_callout_map);
  740. }
  741. /*
  742. * Entry point to boot a CPU.
  743. *
  744. * This is all __cpuinit, not __devinit for now because we don't support
  745. * CPU hotplug (yet).
  746. */
  747. int __cpuinit __cpu_up(unsigned int cpu)
  748. {
  749. int err;
  750. int apicid = cpu_present_to_apicid(cpu);
  751. WARN_ON(irqs_disabled());
  752. Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  753. if (apicid == BAD_APICID || apicid == boot_cpu_id ||
  754. !physid_isset(apicid, phys_cpu_present_map)) {
  755. printk("__cpu_up: bad cpu %d\n", cpu);
  756. return -EINVAL;
  757. }
  758. sync_tsc_bp_init(1);
  759. /* Boot it! */
  760. err = do_boot_cpu(cpu, apicid);
  761. if (err < 0) {
  762. sync_tsc_bp_init(0);
  763. Dprintk("do_boot_cpu failed %d\n", err);
  764. return err;
  765. }
  766. sync_tsc_bp(cpu);
  767. /* Unleash the CPU! */
  768. Dprintk("waiting for cpu %d\n", cpu);
  769. while (!cpu_isset(cpu, cpu_online_map))
  770. cpu_relax();
  771. return 0;
  772. }
  773. /*
  774. * Finish the SMP boot.
  775. */
  776. void __cpuinit smp_cpus_done(unsigned int max_cpus)
  777. {
  778. zap_low_mappings();
  779. smp_cleanup_boot();
  780. #ifdef CONFIG_X86_IO_APIC
  781. setup_ioapic_dest();
  782. #endif
  783. detect_siblings();
  784. time_init_gtod();
  785. }