smpboot.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:23k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  */
  35. #include <linux/config.h>
  36. #include <linux/init.h>
  37. #include <linux/mm.h>
  38. #include <linux/kernel_stat.h>
  39. #include <linux/smp_lock.h>
  40. #include <linux/irq.h>
  41. #include <linux/bootmem.h>
  42. #include <linux/delay.h>
  43. #include <linux/mc146818rtc.h>
  44. #include <asm/mtrr.h>
  45. #include <asm/pgalloc.h>
  46. #include <asm/desc.h>
  47. #include <asm/kdebug.h>
  48. #include <asm/timex.h>
  49. #include <asm/proto.h>
  50. /* Setup configured maximum number of CPUs to activate */
  51. static int max_cpus = -1;
  52. static int cpu_mask = -1; 
  53. /* Total count of live CPUs */
  54. int smp_num_cpus = 1;
  55. /* Bitmask of currently online CPUs */
  56. unsigned long cpu_online_map;
  57. /* which CPU (physical APIC ID) maps to which logical CPU number */
  58. volatile int x86_apicid_to_cpu[NR_CPUS];
  59. /* which logical CPU number maps to which CPU (physical APIC ID) */
  60. volatile int x86_cpu_to_apicid[NR_CPUS];
  61. static volatile unsigned long cpu_callin_map;
  62. static volatile unsigned long cpu_callout_map;
  63. /* Per CPU bogomips and other parameters */
  64. struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
  65. /* Set when the idlers are all forked */
  66. int smp_threads_ready;
  67. /*
  68.  * Setup routine for controlling SMP activation
  69.  *
  70.  * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
  71.  * activation entirely (the MPS table probe still happens, though).
  72.  *
  73.  * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
  74.  * greater than 0, limits the maximum number of CPUs activated in
  75.  * SMP mode to <NUM>.
  76.  */
  77. static int __init nosmp(char *str)
  78. {
  79. max_cpus = 0;
  80. return 1;
  81. }
  82. __setup("nosmp", nosmp);
  83. static int __init maxcpus(char *str)
  84. {
  85. get_option(&str, &max_cpus);
  86. return 1;
  87. }
  88. __setup("maxcpus=", maxcpus);
  89. static int __init cpumask(char *str)
  90. {
  91. get_option(&str, &cpu_mask);
  92. return 1;
  93. }
  94. __setup("cpumask=", cpumask); 
  95. /*
  96.  * Trampoline 80x86 program as an array.
  97.  */
  98. extern unsigned char trampoline_data [];
  99. extern unsigned char trampoline_end  [];
  100. static unsigned char *trampoline_base;
  101. /*
  102.  * Currently trivial. Write the real->protected mode
  103.  * bootstrap into the page concerned. The caller
  104.  * has made sure it's suitably aligned.
  105.  */
  106. static unsigned long __init setup_trampoline(void)
  107. {
  108. extern volatile __u32 tramp_gdt_ptr; 
  109. tramp_gdt_ptr = __pa_symbol(&gdt_table); 
  110. memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
  111. return virt_to_phys(trampoline_base);
  112. }
  113. /*
  114.  * We are called very early to get the low memory for the
  115.  * SMP bootup trampoline page.
  116.  */
  117. void __init smp_alloc_memory(void)
  118. {
  119. trampoline_base = __va(0x6000); /* reserved in setup.c */
  120. }
  121. /*
  122.  * The bootstrap kernel entry code has set these up. Save them for
  123.  * a given CPU
  124.  */
  125. void __init smp_store_cpu_info(int id)
  126. {
  127. struct cpuinfo_x86 *c = cpu_data + id;
  128. *c = boot_cpu_data;
  129. identify_cpu(c);
  130. }
  131. /*
  132.  * Architecture specific routine called by the kernel just before init is
  133.  * fired off. This allows the BP to have everything in order [we hope].
  134.  * At the end of this all the APs will hit the system scheduling and off
  135.  * we go. Each AP will load the system gdt's and jump through the kernel
  136.  * init into idle(). At this point the scheduler will one day take over
  137.  * and give them jobs to do. smp_callin is a standard routine
  138.  * we use to track CPUs as they power up.
  139.  */
  140. static atomic_t smp_commenced = ATOMIC_INIT(0);
  141. void __init smp_commence(void)
  142. {
  143. /*
  144.  * Lets the callins below out of their loop.
  145.  */
  146. Dprintk("Setting commenced=1, go go gon");
  147. wmb();
  148. atomic_set(&smp_commenced,1);
  149. }
  150. /*
  151.  * TSC synchronization.
  152.  *
  153.  * We first check wether all CPUs have their TSC's synchronized,
  154.  * then we print a warning if not, and always resync.
  155.  */
  156. static atomic_t tsc_start_flag = ATOMIC_INIT(0);
  157. static atomic_t tsc_count_start = ATOMIC_INIT(0);
  158. static atomic_t tsc_count_stop = ATOMIC_INIT(0);
  159. static unsigned long long tsc_values[NR_CPUS];
  160. #define NR_LOOPS 5
  161. static inline unsigned long long div64 (unsigned long long a, unsigned long b)
  162. {
  163. return a/b;
  164. }
  165. static void __init synchronize_tsc_bp (void)
  166. {
  167. int i;
  168. unsigned long long t0;
  169. unsigned long long sum, avg;
  170. long long delta;
  171. unsigned long one_usec;
  172. int buggy = 0;
  173. printk("checking TSC synchronization across CPUs: ");
  174. one_usec = cpu_khz / 1000;
  175. atomic_set(&tsc_start_flag, 1);
  176. wmb();
  177. /*
  178.  * We loop a few times to get a primed instruction cache,
  179.  * then the last pass is more or less synchronized and
  180.  * the BP and APs set their cycle counters to zero all at
  181.  * once. This reduces the chance of having random offsets
  182.  * between the processors, and guarantees that the maximum
  183.  * delay between the cycle counters is never bigger than
  184.  * the latency of information-passing (cachelines) between
  185.  * two CPUs.
  186.  */
  187. for (i = 0; i < NR_LOOPS; i++) {
  188. /*
  189.  * all APs synchronize but they loop on '== num_cpus'
  190.  */
  191. while (atomic_read(&tsc_count_start) != smp_num_cpus-1) mb();
  192. atomic_set(&tsc_count_stop, 0);
  193. wmb();
  194. /*
  195.  * this lets the APs save their current TSC:
  196.  */
  197. atomic_inc(&tsc_count_start);
  198. rdtscll(tsc_values[smp_processor_id()]);
  199. /*
  200.  * We clear the TSC in the last loop:
  201.  */
  202. if (i == NR_LOOPS-1)
  203. write_tsc(0, 0);
  204. /*
  205.  * Wait for all APs to leave the synchronization point:
  206.  */
  207. while (atomic_read(&tsc_count_stop) != smp_num_cpus-1) mb();
  208. atomic_set(&tsc_count_start, 0);
  209. wmb();
  210. atomic_inc(&tsc_count_stop);
  211. }
  212. sum = 0;
  213. for (i = 0; i < smp_num_cpus; i++) {
  214. t0 = tsc_values[i];
  215. sum += t0;
  216. }
  217. avg = div64(sum, smp_num_cpus);
  218. sum = 0;
  219. for (i = 0; i < smp_num_cpus; i++) {
  220. delta = tsc_values[i] - avg;
  221. if (delta < 0)
  222. delta = -delta;
  223. /*
  224.  * We report bigger than 2 microseconds clock differences.
  225.  */
  226. if (delta > 2*one_usec) {
  227. long realdelta;
  228. if (!buggy) {
  229. buggy = 1;
  230. printk("n");
  231. }
  232. realdelta = div64(delta, one_usec);
  233. if (tsc_values[i] < avg)
  234. realdelta = -realdelta;
  235. printk("BIOS BUG: CPU#%d improperly initialized, has %ld usecs TSC skew! FIXED.n",
  236. i, realdelta);
  237. }
  238. sum += delta;
  239. }
  240. if (!buggy)
  241. printk("passed.n");
  242. }
  243. static void __init synchronize_tsc_ap (void)
  244. {
  245. int i;
  246. /*
  247.  * smp_num_cpus is not necessarily known at the time
  248.  * this gets called, so we first wait for the BP to
  249.  * finish SMP initialization:
  250.  */
  251. while (!atomic_read(&tsc_start_flag)) mb();
  252. for (i = 0; i < NR_LOOPS; i++) {
  253. atomic_inc(&tsc_count_start);
  254. while (atomic_read(&tsc_count_start) != smp_num_cpus) mb();
  255. rdtscll(tsc_values[smp_processor_id()]);
  256. if (i == NR_LOOPS-1)
  257. write_tsc(0, 0);
  258. atomic_inc(&tsc_count_stop);
  259. while (atomic_read(&tsc_count_stop) != smp_num_cpus) mb();
  260. }
  261. }
  262. #undef NR_LOOPS
  263. extern void calibrate_delay(void);
  264. static atomic_t init_deasserted;
  265. void __init smp_callin(void)
  266. {
  267. int cpuid, phys_id;
  268. unsigned long timeout;
  269. /*
  270.  * If waken up by an INIT in an 82489DX configuration
  271.  * we may get here before an INIT-deassert IPI reaches
  272.  * our local APIC.  We have to wait for the IPI or we'll
  273.  * lock up on an APIC access.
  274.  */
  275. while (!atomic_read(&init_deasserted));
  276. /*
  277.  * (This works even if the APIC is not enabled.)
  278.  */
  279. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  280. cpuid = current->processor;
  281. if (test_and_set_bit(cpuid, &cpu_online_map)) {
  282. printk("huh, phys CPU#%d, CPU#%d already present??n",
  283. phys_id, cpuid);
  284. BUG();
  285. }
  286. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUTn", cpuid, phys_id);
  287. /*
  288.  * STARTUP IPIs are fragile beasts as they might sometimes
  289.  * trigger some glue motherboard logic. Complete APIC bus
  290.  * silence for 1 second, this overestimates the time the
  291.  * boot CPU is spending to send the up to 2 STARTUP IPIs
  292.  * by a factor of two. This should be enough.
  293.  */
  294. /*
  295.  * Waiting 2s total for startup (udelay is not yet working)
  296.  */
  297. timeout = jiffies + 2*HZ;
  298. while (time_before(jiffies, timeout)) {
  299. /*
  300.  * Has the boot CPU finished it's STARTUP sequence?
  301.  */
  302. if (test_bit(cpuid, &cpu_callout_map))
  303. break;
  304. rep_nop();
  305. }
  306. if (!time_before(jiffies, timeout)) {
  307. printk("BUG: CPU%d started up but did not get a callout!n",
  308. cpuid);
  309. BUG();
  310. }
  311. /*
  312.  * the boot CPU has finished the init stage and is spinning
  313.  * on callin_map until we finish. We are free to set up this
  314.  * CPU, first the APIC. (this is probably redundant on most
  315.  * boards)
  316.  */
  317. Dprintk("CALLIN, before setup_local_APIC().n");
  318. setup_local_APIC();
  319. if (nmi_watchdog == NMI_IO_APIC) {
  320. disable_8259A_irq(0);
  321. enable_NMI_through_LVT0(NULL);
  322. enable_8259A_irq(0);
  323. }
  324. sti();
  325. #ifdef CONFIG_MTRR
  326. /*
  327.  * Must be done before calibration delay is computed
  328.  */
  329. mtrr_init_secondary_cpu ();
  330. #endif
  331. /*
  332.  * Get our bogomips.
  333.  */
  334. calibrate_delay();
  335. Dprintk("Stack at about %pn",&cpuid);
  336. /*
  337.  * Save our processor parameters
  338.  */
  339.   smp_store_cpu_info(cpuid);
  340. notify_die(DIE_CPUINIT, "cpuinit", NULL, 0);
  341. /*
  342.  * Allow the master to continue.
  343.  */
  344. set_bit(cpuid, &cpu_callin_map);
  345. /*
  346.  *      Synchronize the TSC with the BP
  347.  */
  348. if (cpu_has_tsc)
  349. synchronize_tsc_ap();
  350. }
  351. int cpucount;
  352. /*
  353.  * Activate a secondary processor.
  354.  */
  355. int __init start_secondary(void *unused)
  356. {
  357. /*
  358.  * Dont put anything before smp_callin(), SMP
  359.  * booting is too fragile that we want to limit the
  360.  * things done here to the most necessary things.
  361.  */
  362. cpu_init();
  363. smp_callin();
  364. while (!atomic_read(&smp_commenced))
  365. rep_nop();
  366. /*
  367.  * low-memory mappings have been cleared, flush them from
  368.  * the local TLBs too.
  369.  */
  370. local_flush_tlb();
  371. cpu_idle();
  372. return 0;
  373. }
  374. /*
  375.  * Everything has been set up for the secondary
  376.  * CPUs - they just need to reload everything
  377.  * from the task structure
  378.  * This function must not return.
  379.  */
  380. void __init initialize_secondary(void)
  381. {
  382. struct task_struct *me = stack_current();
  383. /*
  384.  * We don't actually need to load the full TSS,
  385.  * basically just the stack pointer and the eip.
  386.  */
  387. asm volatile(
  388. "movq %0,%%rspnt"
  389. "jmp *%1"
  390. :
  391. :"r" (me->thread.rsp),"r" (me->thread.rip));
  392. }
  393. extern volatile void *init_rsp; 
  394. extern void (*initial_code)(void);
  395. static int __init fork_by_hand(void)
  396. {
  397. struct pt_regs regs;
  398. /*
  399.  * don't care about the eip and regs settings since
  400.  * we'll never reschedule the forked task.
  401.  */
  402. return do_fork(CLONE_VM|CLONE_PID, 0, &regs, 0);
  403. }
  404. #if APIC_DEBUG
  405. static inline void inquire_remote_apic(int apicid)
  406. {
  407. int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  408. char *names[] = { "ID", "VERSION", "SPIV" };
  409. int timeout, status;
  410. printk("Inquiring remote APIC #%d...n", apicid);
  411. for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
  412. printk("... APIC #%d %s: ", apicid, names[i]);
  413. /*
  414.  * Wait for idle.
  415.  */
  416. apic_wait_icr_idle();
  417. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  418. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  419. timeout = 0;
  420. do {
  421. udelay(100);
  422. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  423. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  424. switch (status) {
  425. case APIC_ICR_RR_VALID:
  426. status = apic_read(APIC_RRR);
  427. printk("%08xn", status);
  428. break;
  429. default:
  430. printk("failedn");
  431. }
  432. }
  433. }
  434. #endif
  435. static int __init do_boot_cpu (int apicid)
  436. {
  437. struct task_struct *idle;
  438. unsigned long send_status, accept_status, boot_status, maxlvt;
  439. int timeout, num_starts, j, cpu;
  440. unsigned long start_eip;
  441. cpu = ++cpucount;
  442. /*
  443.  * We can't use kernel_thread since we must avoid to
  444.  * reschedule the child.
  445.  */
  446. if (fork_by_hand() < 0)
  447. panic("failed fork for CPU %d", cpu);
  448. /*
  449.  * We remove it from the pidhash and the runqueue
  450.  * once we got the process:
  451.  */
  452. idle = init_task.prev_task;
  453. if (!idle)
  454. panic("No idle process for CPU %d", cpu);
  455. idle->processor = cpu;
  456. x86_cpu_to_apicid[cpu] = apicid;
  457. x86_apicid_to_cpu[apicid] = cpu;
  458. idle->cpus_runnable = 1<<cpu; 
  459. idle->cpus_allowed = 1<<cpu;
  460. idle->thread.rip = (unsigned long)start_secondary;
  461. idle->thread.rsp = (unsigned long)idle + THREAD_SIZE - 8;
  462. del_from_runqueue(idle);
  463. unhash_process(idle);
  464. cpu_pda[cpu].pcurrent = init_tasks[cpu] = idle;
  465. /* start_eip had better be page-aligned! */
  466. start_eip = setup_trampoline();
  467. /* So we see what's up   */
  468. printk("Booting processor %d/%d rip %lx page %pn", cpu, apicid, start_eip, idle);
  469. init_rsp = (void *) (THREAD_SIZE + (char *)idle - 16);
  470. initial_code = initialize_secondary; 
  471. /*
  472.  * This grunge runs the startup process for
  473.  * the targeted processor.
  474.  */
  475. atomic_set(&init_deasserted, 0);
  476. Dprintk("Setting warm reset code and vector.n");
  477. CMOS_WRITE(0xa, 0xf);
  478. local_flush_tlb();
  479. Dprintk("1.n");
  480. *((volatile unsigned short *) phys_to_virt(0x469)) = start_eip >> 4;
  481. Dprintk("2.n");
  482. *((volatile unsigned short *) phys_to_virt(0x467)) = start_eip & 0xf;
  483. Dprintk("3.n");
  484. /*
  485.  * Be paranoid about clearing APIC errors.
  486.  */
  487. if (APIC_INTEGRATED(apic_version[apicid])) {
  488. apic_read_around(APIC_SPIV);
  489. apic_write(APIC_ESR, 0);
  490. apic_read(APIC_ESR);
  491. }
  492. /*
  493.  * Status is now clean
  494.  */
  495. send_status = 0;
  496. accept_status = 0;
  497. boot_status = 0;
  498. /*
  499.  * Starting actual IPI sequence...
  500.  */
  501. Dprintk("Asserting INIT.n");
  502. /*
  503.  * Turn INIT on target chip
  504.  */
  505. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  506. /*
  507.  * Send IPI
  508.  */
  509. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  510. | APIC_DM_INIT);
  511. Dprintk("Waiting for send to finish...n");
  512. timeout = 0;
  513. do {
  514. Dprintk("+");
  515. udelay(100);
  516. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  517. } while (send_status && (timeout++ < 1000));
  518. mdelay(10);
  519. Dprintk("Deasserting INIT.n");
  520. /* Target chip */
  521. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  522. /* Send IPI */
  523. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  524. Dprintk("Waiting for send to finish...n");
  525. timeout = 0;
  526. do {
  527. Dprintk("+");
  528. udelay(100);
  529. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  530. } while (send_status && (timeout++ < 1000));
  531. atomic_set(&init_deasserted, 1);
  532. /*
  533.  * Should we send STARTUP IPIs ?
  534.  *
  535.  * Determine this based on the APIC version.
  536.  * If we don't have an integrated APIC, don't
  537.  * send the STARTUP IPIs.
  538.  */
  539. if (APIC_INTEGRATED(apic_version[apicid]))
  540. num_starts = 2;
  541. else
  542. num_starts = 0;
  543. /*
  544.  * Run STARTUP IPI loop.
  545.  */
  546. Dprintk("#startup loops: %d.n", num_starts);
  547. maxlvt = get_maxlvt();
  548. for (j = 1; j <= num_starts; j++) {
  549. Dprintk("Sending STARTUP #%d.n",j);
  550. apic_read_around(APIC_SPIV);
  551. apic_write(APIC_ESR, 0);
  552. apic_read(APIC_ESR);
  553. Dprintk("After apic_write.n");
  554. /*
  555.  * STARTUP IPI
  556.  */
  557. /* Target chip */
  558. Dprintk("target apic %xn", SET_APIC_DEST_FIELD(apicid));
  559. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  560. Dprintk("after target chipn"); 
  561. /* Boot on the stack */
  562. /* Kick the second */
  563. apic_write_around(APIC_ICR, APIC_DM_STARTUP
  564. | (start_eip >> 12));
  565. Dprintk("after eip writen"); 
  566. /*
  567.  * Give the other CPU some time to accept the IPI.
  568.  */
  569. udelay(300);
  570. Dprintk("Startup point 1.n");
  571. Dprintk("Waiting for send to finish...n");
  572. timeout = 0;
  573. do {
  574. Dprintk("+");
  575. udelay(100);
  576. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  577. } while (send_status && (timeout++ < 1000));
  578. /*
  579.  * Give the other CPU some time to accept the IPI.
  580.  */
  581. udelay(200);
  582. /*
  583.  * Due to the Pentium erratum 3AP.
  584.  */
  585. if (maxlvt > 3) {
  586. apic_read_around(APIC_SPIV);
  587. apic_write(APIC_ESR, 0);
  588. }
  589. accept_status = (apic_read(APIC_ESR) & 0xEF);
  590. if (send_status || accept_status)
  591. break;
  592. }
  593. Dprintk("After Startup.n");
  594. if (send_status)
  595. printk("APIC never delivered???n");
  596. if (accept_status)
  597. printk("APIC delivery error (%lx).n", accept_status);
  598. if (!send_status && !accept_status) {
  599. /*
  600.  * allow APs to start initializing.
  601.  */
  602. Dprintk("Before Callout %d.n", cpu);
  603. set_bit(cpu, &cpu_callout_map);
  604. Dprintk("After Callout %d.n", cpu);
  605. /*
  606.  * Wait 5s total for a response
  607.  */
  608. for (timeout = 0; timeout < 50000; timeout++) {
  609. if (test_bit(cpu, &cpu_callin_map))
  610. break; /* It has booted */
  611. udelay(100);
  612. }
  613. if (test_bit(cpu, &cpu_callin_map)) {
  614. /* number CPUs logically, starting from 1 (BSP is 0) */
  615. Dprintk("OK.n");
  616. printk("CPU%d: ", cpu);
  617. print_cpu_info(&cpu_data[cpu]);
  618. Dprintk("CPU has booted.n");
  619. } else {
  620. boot_status = 1;
  621. if (*((volatile unsigned char *)phys_to_virt(8192))
  622. == 0xA5)
  623. /* trampoline started but...? */
  624. printk("Stuck ??n");
  625. else
  626. /* trampoline code not run */
  627. printk("Not responding.n");
  628. #if APIC_DEBUG
  629. inquire_remote_apic(apicid);
  630. #endif
  631. }
  632. }
  633. if (send_status || accept_status || boot_status) {
  634. x86_cpu_to_apicid[cpu] = -1;
  635. x86_apicid_to_cpu[apicid] = -1;
  636. cpucount--;
  637. }
  638. /* mark "stuck" area as not stuck */
  639. *((volatile unsigned int *)phys_to_virt(8192)) = 0;
  640. return cpu; 
  641. }
  642. cycles_t cacheflush_time;
  643. static __init void smp_tune_scheduling (void)
  644. {
  645. unsigned long cachesize;       /* kB   */
  646. unsigned long bandwidth = 350; /* MB/s */
  647. /*
  648.  * Rough estimation for SMP scheduling, this is the number of
  649.  * cycles it takes for a fully memory-limited process to flush
  650.  * the SMP-local cache.
  651.  *
  652.  * (For a P5 this pretty much means we will choose another idle
  653.  *  CPU almost always at wakeup time (this is due to the small
  654.  *  L1 cache), on PIIs it's around 50-100 usecs, depending on
  655.  *  the cache size)
  656.  */
  657. if (!cpu_khz) {
  658. /*
  659.  * this basically disables processor-affinity
  660.  * scheduling on SMP without a TSC.
  661.  */
  662. cacheflush_time = 0;
  663. return;
  664. } else {
  665. cachesize = boot_cpu_data.x86_cache_size;
  666. if (cachesize == -1) {
  667. cachesize = 16; /* Pentiums, 2x8kB cache */
  668. bandwidth = 100;
  669. }
  670. cacheflush_time = (cpu_khz>>10) * (cachesize<<10) / bandwidth;
  671. }
  672. printk("per-CPU timeslice cutoff: %ld.%02ld usecs.n",
  673. (long)cacheflush_time/(cpu_khz/1000),
  674. ((long)cacheflush_time*100/(cpu_khz/1000)) % 100);
  675. }
  676. /*
  677.  * Cycle through the processors sending APIC IPIs to boot each.
  678.  */
  679. extern int prof_multiplier[NR_CPUS];
  680. extern int prof_old_multiplier[NR_CPUS];
  681. extern int prof_counter[NR_CPUS];
  682. void __init smp_boot_cpus(void)
  683. {
  684. int apicid, cpu, maxcpu;
  685. #ifdef CONFIG_MTRR
  686. /*  Must be done before other processors booted  */
  687. mtrr_init_boot_cpu ();
  688. #endif
  689. /*
  690.  * Initialize the logical to physical CPU number mapping
  691.  * and the per-CPU profiling counter/multiplier
  692.  */
  693. for (apicid = 0; apicid < NR_CPUS; apicid++) {
  694. x86_apicid_to_cpu[apicid] = -1;
  695. prof_counter[apicid] = 1;
  696. prof_old_multiplier[apicid] = 1;
  697. prof_multiplier[apicid] = 1;
  698. }
  699. /*
  700.  * Setup boot CPU information
  701.  */
  702. smp_store_cpu_info(0); /* Final full version of the data */
  703. printk("CPU%d: ", 0);
  704. print_cpu_info(&cpu_data[0]);
  705. /*
  706.  * We have the boot CPU online for sure.
  707.  */
  708. set_bit(0, &cpu_online_map);
  709. x86_apicid_to_cpu[boot_cpu_id] = 0;
  710. x86_cpu_to_apicid[0] = boot_cpu_id;
  711. global_irq_holder = 0;
  712. current->processor = 0;
  713. init_idle();
  714. smp_tune_scheduling();
  715. /*
  716.  * If we couldnt find an SMP configuration at boot time,
  717.  * get out of here now!
  718.  */
  719. if (!smp_found_config) {
  720. printk(KERN_NOTICE "SMP motherboard not detected.n");
  721. io_apic_irqs = 0;
  722. cpu_online_map = phys_cpu_present_map = 1;
  723. smp_num_cpus = 1;
  724. if (APIC_init_uniprocessor())
  725. printk(KERN_NOTICE "Local APIC not detected."
  726.    " Using dummy APIC emulation.n");
  727. goto smp_done;
  728. }
  729. /*
  730.  * Should not be necessary because the MP table should list the boot
  731.  * CPU too, but we do it for the sake of robustness anyway.
  732.  */
  733. if (!test_bit(boot_cpu_id, &phys_cpu_present_map)) {
  734. printk("weird, boot CPU (#%d) not listed by the BIOS.n",
  735.  boot_cpu_id);
  736. phys_cpu_present_map |= (1 << hard_smp_processor_id());
  737. }
  738. /*
  739.  * If we couldn't find a local APIC, then get out of here now!
  740.  */
  741. if (APIC_INTEGRATED(apic_version[boot_cpu_id]) &&
  742.     !test_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability)) {
  743. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...n",
  744. boot_cpu_id);
  745. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)n");
  746. io_apic_irqs = 0;
  747. cpu_online_map = phys_cpu_present_map = 1;
  748. smp_num_cpus = 1;
  749. goto smp_done;
  750. }
  751. verify_local_APIC();
  752. /*
  753.  * If SMP should be disabled, then really disable it!
  754.  */
  755. if (!max_cpus) {
  756. smp_found_config = 0;
  757. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.n");
  758. io_apic_irqs = 0;
  759. cpu_online_map = phys_cpu_present_map = 1;
  760. smp_num_cpus = 1;
  761. goto smp_done;
  762. }
  763. connect_bsp_APIC();
  764. setup_local_APIC();
  765. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id)
  766. BUG();
  767. /*
  768.  * Now scan the CPU present map and fire up the other CPUs.
  769.  */
  770. Dprintk("CPU present map: %lxn", phys_cpu_present_map);
  771. maxcpu = 0;
  772. for (apicid = 0; apicid < NR_CPUS; apicid++) {
  773. /*
  774.  * Don't even attempt to start the boot CPU!
  775.  */
  776. if (apicid == boot_cpu_id)
  777. continue;
  778. if (!(phys_cpu_present_map & (1 << apicid)))
  779. continue;
  780. if (((1<<apicid) & cpu_mask) == 0) 
  781. continue;
  782. if ((max_cpus >= 0) && (max_cpus <= cpucount+1))
  783. continue;
  784. cpu = do_boot_cpu(apicid);
  785. /*
  786.  * Make sure we unmap all failed CPUs
  787.  */
  788. if ((x86_apicid_to_cpu[apicid] == -1) &&
  789. (phys_cpu_present_map & (1 << apicid)))
  790. printk("phys CPU #%d not responding - cannot use it.n",apicid);
  791. else if (cpu > maxcpu) 
  792. maxcpu = cpu; 
  793. }
  794. /*
  795.  * Cleanup possible dangling ends...
  796.  */
  797. {
  798. /*
  799.  * Install writable page 0 entry to set BIOS data area.
  800.  */
  801. local_flush_tlb();
  802. /*
  803.  * Paranoid:  Set warm reset code and vector here back
  804.  * to default values.
  805.  */
  806. CMOS_WRITE(0, 0xf);
  807. *((volatile int *) phys_to_virt(0x467)) = 0;
  808. }
  809. /*
  810.  * Allow the user to impress friends.
  811.  */
  812. Dprintk("Before bogomips.n");
  813. if (!cpucount) {
  814. printk(KERN_ERR "Only one processor found.n");
  815. } else {
  816. unsigned long bogosum = 0;
  817. for (cpu = 0; cpu < NR_CPUS; cpu++)
  818. if (cpu_online_map & (1<<cpu))
  819. bogosum += cpu_data[cpu].loops_per_jiffy;
  820. printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).n",
  821. cpucount+1,
  822. bogosum/(500000/HZ),
  823. (bogosum/(5000/HZ))%100);
  824. Dprintk("Before bogocount - setting activated=1.n");
  825. }
  826. smp_num_cpus = maxcpu + 1;
  827. Dprintk("Boot done.n");
  828. /*
  829.  * Here we can be sure that there is an IO-APIC in the system. Let's
  830.  * go and set it up:
  831.  */
  832. if (!skip_ioapic_setup && nr_ioapics)
  833. setup_IO_APIC();
  834. /*
  835.  * Set up all local APIC timers in the system:
  836.  */
  837. setup_APIC_clocks();
  838. /*
  839.  * Synchronize the TSC with the AP
  840.  */
  841. if (cpu_has_tsc && cpucount)
  842. synchronize_tsc_bp();
  843. smp_done:
  844. zap_low_mappings();
  845. }