smpboot.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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 or
  16. * later.
  17. *
  18. * Fixes
  19. * Felix Koop : NR_CPUS used properly
  20. * Jose Renau : Handle single CPU case.
  21. * Alan Cox : By repeated request 8) - Total BogoMIP report.
  22. * Greg Wright : Fix for kernel stacks panic.
  23. * Erich Boleyn : MP v1.4 and additional changes.
  24. * Matthias Sattler : Changes for 2.1 kernel map.
  25. * Michel Lespinasse : Changes for 2.1 kernel map.
  26. * Michael Chastain : Change trampoline.S to gnu as.
  27. * Alan Cox : Dumb bug: 'B' step PPro's are fine
  28. * Ingo Molnar : Added APIC timers, based on code
  29. * from Jose Renau
  30. * Ingo Molnar : various cleanups and rewrites
  31. * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
  32. * Maciej W. Rozycki : Bits for genuine 82489DX APICs
  33. * Andi Kleen : Changed for SMP boot into long mode.
  34. * Rusty Russell : Hacked into shape for new "hotplug" boot process.
  35. */
  36. #include <linux/config.h>
  37. #include <linux/init.h>
  38. #include <linux/mm.h>
  39. #include <linux/kernel_stat.h>
  40. #include <linux/smp_lock.h>
  41. #include <linux/irq.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 <asm/mtrr.h>
  48. #include <asm/pgalloc.h>
  49. #include <asm/desc.h>
  50. #include <asm/kdebug.h>
  51. #include <asm/tlbflush.h>
  52. #include <asm/proto.h>
  53. /* Number of siblings per CPU package */
  54. int smp_num_siblings = 1;
  55. /* Package ID of each logical CPU */
  56. u8 phys_proc_id[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
  57. /* Core ID of each logical CPU */
  58. u8 cpu_core_id[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
  59. EXPORT_SYMBOL(phys_proc_id);
  60. EXPORT_SYMBOL(cpu_core_id);
  61. /* Bitmask of currently online CPUs */
  62. cpumask_t cpu_online_map;
  63. cpumask_t cpu_callin_map;
  64. cpumask_t cpu_callout_map;
  65. static cpumask_t smp_commenced_mask;
  66. /* Per CPU bogomips and other parameters */
  67. struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
  68. cpumask_t cpu_sibling_map[NR_CPUS] __cacheline_aligned;
  69. cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
  70. /*
  71. * Trampoline 80x86 program as an array.
  72. */
  73. extern unsigned char trampoline_data [];
  74. extern unsigned char trampoline_end [];
  75. /*
  76. * Currently trivial. Write the real->protected mode
  77. * bootstrap into the page concerned. The caller
  78. * has made sure it's suitably aligned.
  79. */
  80. static unsigned long __init setup_trampoline(void)
  81. {
  82. void *tramp = __va(SMP_TRAMPOLINE_BASE);
  83. memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
  84. return virt_to_phys(tramp);
  85. }
  86. /*
  87. * The bootstrap kernel entry code has set these up. Save them for
  88. * a given CPU
  89. */
  90. static void __init smp_store_cpu_info(int id)
  91. {
  92. struct cpuinfo_x86 *c = cpu_data + id;
  93. *c = boot_cpu_data;
  94. identify_cpu(c);
  95. }
  96. /*
  97. * TSC synchronization.
  98. *
  99. * We first check whether all CPUs have their TSC's synchronized,
  100. * then we print a warning if not, and always resync.
  101. */
  102. static atomic_t tsc_start_flag = ATOMIC_INIT(0);
  103. static atomic_t tsc_count_start = ATOMIC_INIT(0);
  104. static atomic_t tsc_count_stop = ATOMIC_INIT(0);
  105. static unsigned long long tsc_values[NR_CPUS];
  106. #define NR_LOOPS 5
  107. extern unsigned int fast_gettimeoffset_quotient;
  108. static void __init synchronize_tsc_bp (void)
  109. {
  110. int i;
  111. unsigned long long t0;
  112. unsigned long long sum, avg;
  113. long long delta;
  114. long one_usec;
  115. int buggy = 0;
  116. printk(KERN_INFO "checking TSC synchronization across %u CPUs: ",num_booting_cpus());
  117. one_usec = cpu_khz;
  118. atomic_set(&tsc_start_flag, 1);
  119. wmb();
  120. /*
  121. * We loop a few times to get a primed instruction cache,
  122. * then the last pass is more or less synchronized and
  123. * the BP and APs set their cycle counters to zero all at
  124. * once. This reduces the chance of having random offsets
  125. * between the processors, and guarantees that the maximum
  126. * delay between the cycle counters is never bigger than
  127. * the latency of information-passing (cachelines) between
  128. * two CPUs.
  129. */
  130. for (i = 0; i < NR_LOOPS; i++) {
  131. /*
  132. * all APs synchronize but they loop on '== num_cpus'
  133. */
  134. while (atomic_read(&tsc_count_start) != num_booting_cpus()-1) mb();
  135. atomic_set(&tsc_count_stop, 0);
  136. wmb();
  137. /*
  138. * this lets the APs save their current TSC:
  139. */
  140. atomic_inc(&tsc_count_start);
  141. sync_core();
  142. rdtscll(tsc_values[smp_processor_id()]);
  143. /*
  144. * We clear the TSC in the last loop:
  145. */
  146. if (i == NR_LOOPS-1)
  147. write_tsc(0, 0);
  148. /*
  149. * Wait for all APs to leave the synchronization point:
  150. */
  151. while (atomic_read(&tsc_count_stop) != num_booting_cpus()-1) mb();
  152. atomic_set(&tsc_count_start, 0);
  153. wmb();
  154. atomic_inc(&tsc_count_stop);
  155. }
  156. sum = 0;
  157. for (i = 0; i < NR_CPUS; i++) {
  158. if (cpu_isset(i, cpu_callout_map)) {
  159. t0 = tsc_values[i];
  160. sum += t0;
  161. }
  162. }
  163. avg = sum / num_booting_cpus();
  164. sum = 0;
  165. for (i = 0; i < NR_CPUS; i++) {
  166. if (!cpu_isset(i, cpu_callout_map))
  167. continue;
  168. delta = tsc_values[i] - avg;
  169. if (delta < 0)
  170. delta = -delta;
  171. /*
  172. * We report bigger than 2 microseconds clock differences.
  173. */
  174. if (delta > 2*one_usec) {
  175. long realdelta;
  176. if (!buggy) {
  177. buggy = 1;
  178. printk("\n");
  179. }
  180. realdelta = delta / one_usec;
  181. if (tsc_values[i] < avg)
  182. realdelta = -realdelta;
  183. printk("BIOS BUG: CPU#%d improperly initialized, has %ld usecs TSC skew! FIXED.\n",
  184. i, realdelta);
  185. }
  186. sum += delta;
  187. }
  188. if (!buggy)
  189. printk("passed.\n");
  190. }
  191. static void __init synchronize_tsc_ap (void)
  192. {
  193. int i;
  194. /*
  195. * Not every cpu is online at the time
  196. * this gets called, so we first wait for the BP to
  197. * finish SMP initialization:
  198. */
  199. while (!atomic_read(&tsc_start_flag)) mb();
  200. for (i = 0; i < NR_LOOPS; i++) {
  201. atomic_inc(&tsc_count_start);
  202. while (atomic_read(&tsc_count_start) != num_booting_cpus()) mb();
  203. sync_core();
  204. rdtscll(tsc_values[smp_processor_id()]);
  205. if (i == NR_LOOPS-1)
  206. write_tsc(0, 0);
  207. atomic_inc(&tsc_count_stop);
  208. while (atomic_read(&tsc_count_stop) != num_booting_cpus()) mb();
  209. }
  210. }
  211. #undef NR_LOOPS
  212. static atomic_t init_deasserted;
  213. static void __init smp_callin(void)
  214. {
  215. int cpuid, phys_id;
  216. unsigned long timeout;
  217. /*
  218. * If waken up by an INIT in an 82489DX configuration
  219. * we may get here before an INIT-deassert IPI reaches
  220. * our local APIC. We have to wait for the IPI or we'll
  221. * lock up on an APIC access.
  222. */
  223. while (!atomic_read(&init_deasserted));
  224. /*
  225. * (This works even if the APIC is not enabled.)
  226. */
  227. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  228. cpuid = smp_processor_id();
  229. if (cpu_isset(cpuid, cpu_callin_map)) {
  230. panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
  231. phys_id, cpuid);
  232. }
  233. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  234. /*
  235. * STARTUP IPIs are fragile beasts as they might sometimes
  236. * trigger some glue motherboard logic. Complete APIC bus
  237. * silence for 1 second, this overestimates the time the
  238. * boot CPU is spending to send the up to 2 STARTUP IPIs
  239. * by a factor of two. This should be enough.
  240. */
  241. /*
  242. * Waiting 2s total for startup (udelay is not yet working)
  243. */
  244. timeout = jiffies + 2*HZ;
  245. while (time_before(jiffies, timeout)) {
  246. /*
  247. * Has the boot CPU finished it's STARTUP sequence?
  248. */
  249. if (cpu_isset(cpuid, cpu_callout_map))
  250. break;
  251. rep_nop();
  252. }
  253. if (!time_before(jiffies, timeout)) {
  254. panic("smp_callin: CPU%d started up but did not get a callout!\n",
  255. cpuid);
  256. }
  257. /*
  258. * the boot CPU has finished the init stage and is spinning
  259. * on callin_map until we finish. We are free to set up this
  260. * CPU, first the APIC. (this is probably redundant on most
  261. * boards)
  262. */
  263. Dprintk("CALLIN, before setup_local_APIC().\n");
  264. setup_local_APIC();
  265. /*
  266. * Get our bogomips.
  267. */
  268. calibrate_delay();
  269. Dprintk("Stack at about %p\n",&cpuid);
  270. disable_APIC_timer();
  271. /*
  272. * Save our processor parameters
  273. */
  274. smp_store_cpu_info(cpuid);
  275. /*
  276. * Allow the master to continue.
  277. */
  278. cpu_set(cpuid, cpu_callin_map);
  279. /*
  280. * Synchronize the TSC with the BP
  281. */
  282. if (cpu_has_tsc)
  283. synchronize_tsc_ap();
  284. }
  285. static int cpucount;
  286. /*
  287. * Activate a secondary processor.
  288. */
  289. void __init start_secondary(void)
  290. {
  291. /*
  292. * Dont put anything before smp_callin(), SMP
  293. * booting is too fragile that we want to limit the
  294. * things done here to the most necessary things.
  295. */
  296. cpu_init();
  297. smp_callin();
  298. /* otherwise gcc will move up the smp_processor_id before the cpu_init */
  299. barrier();
  300. Dprintk("cpu %d: waiting for commence\n", smp_processor_id());
  301. while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
  302. rep_nop();
  303. Dprintk("cpu %d: setting up apic clock\n", smp_processor_id());
  304. setup_secondary_APIC_clock();
  305. Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
  306. if (nmi_watchdog == NMI_IO_APIC) {
  307. disable_8259A_irq(0);
  308. enable_NMI_through_LVT0(NULL);
  309. enable_8259A_irq(0);
  310. }
  311. enable_APIC_timer();
  312. /*
  313. * low-memory mappings have been cleared, flush them from
  314. * the local TLBs too.
  315. */
  316. local_flush_tlb();
  317. Dprintk("cpu %d eSetting cpu_online_map\n", smp_processor_id());
  318. cpu_set(smp_processor_id(), cpu_online_map);
  319. wmb();
  320. cpu_idle();
  321. }
  322. extern volatile unsigned long init_rsp;
  323. extern void (*initial_code)(void);
  324. #if APIC_DEBUG
  325. static inline void inquire_remote_apic(int apicid)
  326. {
  327. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  328. char *names[] = { "ID", "VERSION", "SPIV" };
  329. int timeout, status;
  330. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  331. for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
  332. printk("... APIC #%d %s: ", apicid, names[i]);
  333. /*
  334. * Wait for idle.
  335. */
  336. apic_wait_icr_idle();
  337. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  338. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  339. timeout = 0;
  340. do {
  341. udelay(100);
  342. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  343. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  344. switch (status) {
  345. case APIC_ICR_RR_VALID:
  346. status = apic_read(APIC_RRR);
  347. printk("%08x\n", status);
  348. break;
  349. default:
  350. printk("failed\n");
  351. }
  352. }
  353. }
  354. #endif
  355. static int __init wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
  356. {
  357. unsigned long send_status = 0, accept_status = 0;
  358. int maxlvt, timeout, num_starts, j;
  359. Dprintk("Asserting INIT.\n");
  360. /*
  361. * Turn INIT on target chip
  362. */
  363. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  364. /*
  365. * Send IPI
  366. */
  367. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  368. | APIC_DM_INIT);
  369. Dprintk("Waiting for send to finish...\n");
  370. timeout = 0;
  371. do {
  372. Dprintk("+");
  373. udelay(100);
  374. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  375. } while (send_status && (timeout++ < 1000));
  376. mdelay(10);
  377. Dprintk("Deasserting INIT.\n");
  378. /* Target chip */
  379. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  380. /* Send IPI */
  381. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  382. Dprintk("Waiting for send to finish...\n");
  383. timeout = 0;
  384. do {
  385. Dprintk("+");
  386. udelay(100);
  387. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  388. } while (send_status && (timeout++ < 1000));
  389. atomic_set(&init_deasserted, 1);
  390. /*
  391. * Should we send STARTUP IPIs ?
  392. *
  393. * Determine this based on the APIC version.
  394. * If we don't have an integrated APIC, don't send the STARTUP IPIs.
  395. */
  396. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  397. num_starts = 2;
  398. else
  399. num_starts = 0;
  400. /*
  401. * Run STARTUP IPI loop.
  402. */
  403. Dprintk("#startup loops: %d.\n", num_starts);
  404. maxlvt = get_maxlvt();
  405. for (j = 1; j <= num_starts; j++) {
  406. Dprintk("Sending STARTUP #%d.\n",j);
  407. apic_read_around(APIC_SPIV);
  408. apic_write(APIC_ESR, 0);
  409. apic_read(APIC_ESR);
  410. Dprintk("After apic_write.\n");
  411. /*
  412. * STARTUP IPI
  413. */
  414. /* Target chip */
  415. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  416. /* Boot on the stack */
  417. /* Kick the second */
  418. apic_write_around(APIC_ICR, APIC_DM_STARTUP
  419. | (start_rip >> 12));
  420. /*
  421. * Give the other CPU some time to accept the IPI.
  422. */
  423. udelay(300);
  424. Dprintk("Startup point 1.\n");
  425. Dprintk("Waiting for send to finish...\n");
  426. timeout = 0;
  427. do {
  428. Dprintk("+");
  429. udelay(100);
  430. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  431. } while (send_status && (timeout++ < 1000));
  432. /*
  433. * Give the other CPU some time to accept the IPI.
  434. */
  435. udelay(200);
  436. /*
  437. * Due to the Pentium erratum 3AP.
  438. */
  439. if (maxlvt > 3) {
  440. apic_read_around(APIC_SPIV);
  441. apic_write(APIC_ESR, 0);
  442. }
  443. accept_status = (apic_read(APIC_ESR) & 0xEF);
  444. if (send_status || accept_status)
  445. break;
  446. }
  447. Dprintk("After Startup.\n");
  448. if (send_status)
  449. printk(KERN_ERR "APIC never delivered???\n");
  450. if (accept_status)
  451. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  452. return (send_status | accept_status);
  453. }
  454. static void __init do_boot_cpu (int apicid)
  455. {
  456. struct task_struct *idle;
  457. unsigned long boot_error;
  458. int timeout, cpu;
  459. unsigned long start_rip;
  460. cpu = ++cpucount;
  461. /*
  462. * We can't use kernel_thread since we must avoid to
  463. * reschedule the child.
  464. */
  465. idle = fork_idle(cpu);
  466. if (IS_ERR(idle))
  467. panic("failed fork for CPU %d", cpu);
  468. x86_cpu_to_apicid[cpu] = apicid;
  469. cpu_pda[cpu].pcurrent = idle;
  470. start_rip = setup_trampoline();
  471. init_rsp = idle->thread.rsp;
  472. per_cpu(init_tss,cpu).rsp0 = init_rsp;
  473. initial_code = start_secondary;
  474. clear_ti_thread_flag(idle->thread_info, TIF_FORK);
  475. printk(KERN_INFO "Booting processor %d/%d rip %lx rsp %lx\n", cpu, apicid,
  476. start_rip, init_rsp);
  477. /*
  478. * This grunge runs the startup process for
  479. * the targeted processor.
  480. */
  481. atomic_set(&init_deasserted, 0);
  482. Dprintk("Setting warm reset code and vector.\n");
  483. CMOS_WRITE(0xa, 0xf);
  484. local_flush_tlb();
  485. Dprintk("1.\n");
  486. *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
  487. Dprintk("2.\n");
  488. *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
  489. Dprintk("3.\n");
  490. /*
  491. * Be paranoid about clearing APIC errors.
  492. */
  493. if (APIC_INTEGRATED(apic_version[apicid])) {
  494. apic_read_around(APIC_SPIV);
  495. apic_write(APIC_ESR, 0);
  496. apic_read(APIC_ESR);
  497. }
  498. /*
  499. * Status is now clean
  500. */
  501. boot_error = 0;
  502. /*
  503. * Starting actual IPI sequence...
  504. */
  505. boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
  506. if (!boot_error) {
  507. /*
  508. * allow APs to start initializing.
  509. */
  510. Dprintk("Before Callout %d.\n", cpu);
  511. cpu_set(cpu, cpu_callout_map);
  512. Dprintk("After Callout %d.\n", cpu);
  513. /*
  514. * Wait 5s total for a response
  515. */
  516. for (timeout = 0; timeout < 50000; timeout++) {
  517. if (cpu_isset(cpu, cpu_callin_map))
  518. break; /* It has booted */
  519. udelay(100);
  520. }
  521. if (cpu_isset(cpu, cpu_callin_map)) {
  522. /* number CPUs logically, starting from 1 (BSP is 0) */
  523. Dprintk("OK.\n");
  524. print_cpu_info(&cpu_data[cpu]);
  525. Dprintk("CPU has booted.\n");
  526. } else {
  527. boot_error = 1;
  528. if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
  529. == 0xA5)
  530. /* trampoline started but...? */
  531. printk("Stuck ??\n");
  532. else
  533. /* trampoline code not run */
  534. printk("Not responding.\n");
  535. #if APIC_DEBUG
  536. inquire_remote_apic(apicid);
  537. #endif
  538. }
  539. }
  540. if (boot_error) {
  541. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  542. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  543. cpucount--;
  544. x86_cpu_to_apicid[cpu] = BAD_APICID;
  545. x86_cpu_to_log_apicid[cpu] = BAD_APICID;
  546. }
  547. }
  548. static void smp_tune_scheduling (void)
  549. {
  550. int cachesize; /* kB */
  551. unsigned long bandwidth = 1000; /* MB/s */
  552. /*
  553. * Rough estimation for SMP scheduling, this is the number of
  554. * cycles it takes for a fully memory-limited process to flush
  555. * the SMP-local cache.
  556. *
  557. * (For a P5 this pretty much means we will choose another idle
  558. * CPU almost always at wakeup time (this is due to the small
  559. * L1 cache), on PIIs it's around 50-100 usecs, depending on
  560. * the cache size)
  561. */
  562. if (!cpu_khz) {
  563. return;
  564. } else {
  565. cachesize = boot_cpu_data.x86_cache_size;
  566. if (cachesize == -1) {
  567. cachesize = 16; /* Pentiums, 2x8kB cache */
  568. bandwidth = 100;
  569. }
  570. }
  571. }
  572. /*
  573. * Cycle through the processors sending APIC IPIs to boot each.
  574. */
  575. static void __init smp_boot_cpus(unsigned int max_cpus)
  576. {
  577. unsigned apicid, cpu, bit, kicked;
  578. nmi_watchdog_default();
  579. /*
  580. * Setup boot CPU information
  581. */
  582. smp_store_cpu_info(0); /* Final full version of the data */
  583. printk(KERN_INFO "CPU%d: ", 0);
  584. print_cpu_info(&cpu_data[0]);
  585. current_thread_info()->cpu = 0;
  586. smp_tune_scheduling();
  587. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  588. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  589. hard_smp_processor_id());
  590. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  591. }
  592. /*
  593. * If we couldn't find an SMP configuration at boot time,
  594. * get out of here now!
  595. */
  596. if (!smp_found_config) {
  597. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  598. io_apic_irqs = 0;
  599. cpu_online_map = cpumask_of_cpu(0);
  600. cpu_set(0, cpu_sibling_map[0]);
  601. cpu_set(0, cpu_core_map[0]);
  602. phys_cpu_present_map = physid_mask_of_physid(0);
  603. if (APIC_init_uniprocessor())
  604. printk(KERN_NOTICE "Local APIC not detected."
  605. " Using dummy APIC emulation.\n");
  606. goto smp_done;
  607. }
  608. /*
  609. * Should not be necessary because the MP table should list the boot
  610. * CPU too, but we do it for the sake of robustness anyway.
  611. */
  612. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  613. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  614. boot_cpu_id);
  615. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  616. }
  617. /*
  618. * If we couldn't find a local APIC, then get out of here now!
  619. */
  620. if (APIC_INTEGRATED(apic_version[boot_cpu_id]) && !cpu_has_apic) {
  621. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  622. boot_cpu_id);
  623. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  624. io_apic_irqs = 0;
  625. cpu_online_map = cpumask_of_cpu(0);
  626. cpu_set(0, cpu_sibling_map[0]);
  627. cpu_set(0, cpu_core_map[0]);
  628. phys_cpu_present_map = physid_mask_of_physid(0);
  629. disable_apic = 1;
  630. goto smp_done;
  631. }
  632. verify_local_APIC();
  633. /*
  634. * If SMP should be disabled, then really disable it!
  635. */
  636. if (!max_cpus) {
  637. smp_found_config = 0;
  638. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  639. io_apic_irqs = 0;
  640. cpu_online_map = cpumask_of_cpu(0);
  641. cpu_set(0, cpu_sibling_map[0]);
  642. cpu_set(0, cpu_core_map[0]);
  643. phys_cpu_present_map = physid_mask_of_physid(0);
  644. disable_apic = 1;
  645. goto smp_done;
  646. }
  647. connect_bsp_APIC();
  648. setup_local_APIC();
  649. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id)
  650. BUG();
  651. x86_cpu_to_apicid[0] = boot_cpu_id;
  652. /*
  653. * Now scan the CPU present map and fire up the other CPUs.
  654. */
  655. Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
  656. kicked = 1;
  657. for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
  658. apicid = cpu_present_to_apicid(bit);
  659. /*
  660. * Don't even attempt to start the boot CPU!
  661. */
  662. if (apicid == boot_cpu_id || (apicid == BAD_APICID))
  663. continue;
  664. if (!physid_isset(apicid, phys_cpu_present_map))
  665. continue;
  666. if ((max_cpus >= 0) && (max_cpus <= cpucount+1))
  667. continue;
  668. do_boot_cpu(apicid);
  669. ++kicked;
  670. }
  671. /*
  672. * Cleanup possible dangling ends...
  673. */
  674. {
  675. /*
  676. * Install writable page 0 entry to set BIOS data area.
  677. */
  678. local_flush_tlb();
  679. /*
  680. * Paranoid: Set warm reset code and vector here back
  681. * to default values.
  682. */
  683. CMOS_WRITE(0, 0xf);
  684. *((volatile int *) phys_to_virt(0x467)) = 0;
  685. }
  686. /*
  687. * Allow the user to impress friends.
  688. */
  689. Dprintk("Before bogomips.\n");
  690. if (!cpucount) {
  691. printk(KERN_INFO "Only one processor found.\n");
  692. } else {
  693. unsigned long bogosum = 0;
  694. for (cpu = 0; cpu < NR_CPUS; cpu++)
  695. if (cpu_isset(cpu, cpu_callout_map))
  696. bogosum += cpu_data[cpu].loops_per_jiffy;
  697. printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  698. cpucount+1,
  699. bogosum/(500000/HZ),
  700. (bogosum/(5000/HZ))%100);
  701. Dprintk("Before bogocount - setting activated=1.\n");
  702. }
  703. /*
  704. * Construct cpu_sibling_map[], so that we can tell the
  705. * sibling CPU efficiently.
  706. */
  707. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  708. cpus_clear(cpu_sibling_map[cpu]);
  709. cpus_clear(cpu_core_map[cpu]);
  710. }
  711. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  712. struct cpuinfo_x86 *c = cpu_data + cpu;
  713. int siblings = 0;
  714. int i;
  715. if (!cpu_isset(cpu, cpu_callout_map))
  716. continue;
  717. if (smp_num_siblings > 1) {
  718. for (i = 0; i < NR_CPUS; i++) {
  719. if (!cpu_isset(i, cpu_callout_map))
  720. continue;
  721. if (phys_proc_id[cpu] == cpu_core_id[i]) {
  722. siblings++;
  723. cpu_set(i, cpu_sibling_map[cpu]);
  724. }
  725. }
  726. } else {
  727. siblings++;
  728. cpu_set(cpu, cpu_sibling_map[cpu]);
  729. }
  730. if (siblings != smp_num_siblings) {
  731. printk(KERN_WARNING
  732. "WARNING: %d siblings found for CPU%d, should be %d\n",
  733. siblings, cpu, smp_num_siblings);
  734. smp_num_siblings = siblings;
  735. }
  736. if (c->x86_num_cores > 1) {
  737. for (i = 0; i < NR_CPUS; i++) {
  738. if (!cpu_isset(i, cpu_callout_map))
  739. continue;
  740. if (phys_proc_id[cpu] == phys_proc_id[i]) {
  741. cpu_set(i, cpu_core_map[cpu]);
  742. }
  743. }
  744. } else
  745. cpu_core_map[cpu] = cpu_sibling_map[cpu];
  746. }
  747. Dprintk("Boot done.\n");
  748. /*
  749. * Here we can be sure that there is an IO-APIC in the system. Let's
  750. * go and set it up:
  751. */
  752. if (!skip_ioapic_setup && nr_ioapics)
  753. setup_IO_APIC();
  754. else
  755. nr_ioapics = 0;
  756. setup_boot_APIC_clock();
  757. /*
  758. * Synchronize the TSC with the AP
  759. */
  760. if (cpu_has_tsc && cpucount)
  761. synchronize_tsc_bp();
  762. smp_done:
  763. time_init_smp();
  764. }
  765. /* These are wrappers to interface to the new boot process. Someone
  766. who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
  767. void __init smp_prepare_cpus(unsigned int max_cpus)
  768. {
  769. smp_boot_cpus(max_cpus);
  770. }
  771. void __devinit smp_prepare_boot_cpu(void)
  772. {
  773. cpu_set(smp_processor_id(), cpu_online_map);
  774. cpu_set(smp_processor_id(), cpu_callout_map);
  775. }
  776. int __devinit __cpu_up(unsigned int cpu)
  777. {
  778. /* This only works at boot for x86. See "rewrite" above. */
  779. if (cpu_isset(cpu, smp_commenced_mask)) {
  780. local_irq_enable();
  781. return -ENOSYS;
  782. }
  783. /* In case one didn't come up */
  784. if (!cpu_isset(cpu, cpu_callin_map)) {
  785. local_irq_enable();
  786. return -EIO;
  787. }
  788. local_irq_enable();
  789. /* Unleash the CPU! */
  790. Dprintk("waiting for cpu %d\n", cpu);
  791. cpu_set(cpu, smp_commenced_mask);
  792. while (!cpu_isset(cpu, cpu_online_map))
  793. mb();
  794. return 0;
  795. }
  796. void __init smp_cpus_done(unsigned int max_cpus)
  797. {
  798. #ifdef CONFIG_X86_IO_APIC
  799. setup_ioapic_dest();
  800. #endif
  801. zap_low_mappings();
  802. }