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