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