Merge with /home/shaggy/git/linus-clean/
[pandora-kernel.git] / arch / i386 / 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  *
7  *      Much of the core SMP work is based on previous work by Thomas Radke, to
8  *      whom a great many thanks are extended.
9  *
10  *      Thanks to Intel for making available several different Pentium,
11  *      Pentium Pro and Pentium-II/Xeon MP machines.
12  *      Original development of Linux SMP code supported by Caldera.
13  *
14  *      This code is released under the GNU General Public License version 2 or
15  *      later.
16  *
17  *      Fixes
18  *              Felix Koop      :       NR_CPUS used properly
19  *              Jose Renau      :       Handle single CPU case.
20  *              Alan Cox        :       By repeated request 8) - Total BogoMIPS report.
21  *              Greg Wright     :       Fix for kernel stacks panic.
22  *              Erich Boleyn    :       MP v1.4 and additional changes.
23  *      Matthias Sattler        :       Changes for 2.1 kernel map.
24  *      Michel Lespinasse       :       Changes for 2.1 kernel map.
25  *      Michael Chastain        :       Change trampoline.S to gnu as.
26  *              Alan Cox        :       Dumb bug: 'B' step PPro's are fine
27  *              Ingo Molnar     :       Added APIC timers, based on code
28  *                                      from Jose Renau
29  *              Ingo Molnar     :       various cleanups and rewrites
30  *              Tigran Aivazian :       fixed "0.00 in /proc/uptime on SMP" bug.
31  *      Maciej W. Rozycki       :       Bits for genuine 82489DX APICs
32  *              Martin J. Bligh :       Added support for multi-quad systems
33  *              Dave Jones      :       Report invalid combinations of Athlon CPUs.
34 *               Rusty Russell   :       Hacked into shape for new "hotplug" boot process. */
35
36 #include <linux/module.h>
37 #include <linux/config.h>
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40
41 #include <linux/mm.h>
42 #include <linux/sched.h>
43 #include <linux/kernel_stat.h>
44 #include <linux/smp_lock.h>
45 #include <linux/irq.h>
46 #include <linux/bootmem.h>
47
48 #include <linux/delay.h>
49 #include <linux/mc146818rtc.h>
50 #include <asm/tlbflush.h>
51 #include <asm/desc.h>
52 #include <asm/arch_hooks.h>
53
54 #include <mach_apic.h>
55 #include <mach_wakecpu.h>
56 #include <smpboot_hooks.h>
57
58 /* Set if we find a B stepping CPU */
59 static int __initdata smp_b_stepping;
60
61 /* Number of siblings per CPU package */
62 int smp_num_siblings = 1;
63 int phys_proc_id[NR_CPUS]; /* Package ID of each logical CPU */
64 EXPORT_SYMBOL(phys_proc_id);
65 int cpu_core_id[NR_CPUS]; /* Core ID of each logical CPU */
66 EXPORT_SYMBOL(cpu_core_id);
67
68 /* bitmap of online cpus */
69 cpumask_t cpu_online_map;
70
71 cpumask_t cpu_callin_map;
72 cpumask_t cpu_callout_map;
73 static cpumask_t smp_commenced_mask;
74
75 /* Per CPU bogomips and other parameters */
76 struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
77
78 u8 x86_cpu_to_apicid[NR_CPUS] =
79                         { [0 ... NR_CPUS-1] = 0xff };
80 EXPORT_SYMBOL(x86_cpu_to_apicid);
81
82 /*
83  * Trampoline 80x86 program as an array.
84  */
85
86 extern unsigned char trampoline_data [];
87 extern unsigned char trampoline_end  [];
88 static unsigned char *trampoline_base;
89 static int trampoline_exec;
90
91 static void map_cpu_to_logical_apicid(void);
92
93 /*
94  * Currently trivial. Write the real->protected mode
95  * bootstrap into the page concerned. The caller
96  * has made sure it's suitably aligned.
97  */
98
99 static unsigned long __init setup_trampoline(void)
100 {
101         memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
102         return virt_to_phys(trampoline_base);
103 }
104
105 /*
106  * We are called very early to get the low memory for the
107  * SMP bootup trampoline page.
108  */
109 void __init smp_alloc_memory(void)
110 {
111         trampoline_base = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
112         /*
113          * Has to be in very low memory so we can execute
114          * real-mode AP code.
115          */
116         if (__pa(trampoline_base) >= 0x9F000)
117                 BUG();
118         /*
119          * Make the SMP trampoline executable:
120          */
121         trampoline_exec = set_kernel_exec((unsigned long)trampoline_base, 1);
122 }
123
124 /*
125  * The bootstrap kernel entry code has set these up. Save them for
126  * a given CPU
127  */
128
129 static void __init smp_store_cpu_info(int id)
130 {
131         struct cpuinfo_x86 *c = cpu_data + id;
132
133         *c = boot_cpu_data;
134         if (id!=0)
135                 identify_cpu(c);
136         /*
137          * Mask B, Pentium, but not Pentium MMX
138          */
139         if (c->x86_vendor == X86_VENDOR_INTEL &&
140             c->x86 == 5 &&
141             c->x86_mask >= 1 && c->x86_mask <= 4 &&
142             c->x86_model <= 3)
143                 /*
144                  * Remember we have B step Pentia with bugs
145                  */
146                 smp_b_stepping = 1;
147
148         /*
149          * Certain Athlons might work (for various values of 'work') in SMP
150          * but they are not certified as MP capable.
151          */
152         if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
153
154                 /* Athlon 660/661 is valid. */  
155                 if ((c->x86_model==6) && ((c->x86_mask==0) || (c->x86_mask==1)))
156                         goto valid_k7;
157
158                 /* Duron 670 is valid */
159                 if ((c->x86_model==7) && (c->x86_mask==0))
160                         goto valid_k7;
161
162                 /*
163                  * Athlon 662, Duron 671, and Athlon >model 7 have capability bit.
164                  * It's worth noting that the A5 stepping (662) of some Athlon XP's
165                  * have the MP bit set.
166                  * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for more.
167                  */
168                 if (((c->x86_model==6) && (c->x86_mask>=2)) ||
169                     ((c->x86_model==7) && (c->x86_mask>=1)) ||
170                      (c->x86_model> 7))
171                         if (cpu_has_mp)
172                                 goto valid_k7;
173
174                 /* If we get here, it's not a certified SMP capable AMD system. */
175                 tainted |= TAINT_UNSAFE_SMP;
176         }
177
178 valid_k7:
179         ;
180 }
181
182 /*
183  * TSC synchronization.
184  *
185  * We first check whether all CPUs have their TSC's synchronized,
186  * then we print a warning if not, and always resync.
187  */
188
189 static atomic_t tsc_start_flag = ATOMIC_INIT(0);
190 static atomic_t tsc_count_start = ATOMIC_INIT(0);
191 static atomic_t tsc_count_stop = ATOMIC_INIT(0);
192 static unsigned long long tsc_values[NR_CPUS];
193
194 #define NR_LOOPS 5
195
196 static void __init synchronize_tsc_bp (void)
197 {
198         int i;
199         unsigned long long t0;
200         unsigned long long sum, avg;
201         long long delta;
202         unsigned long one_usec;
203         int buggy = 0;
204
205         printk(KERN_INFO "checking TSC synchronization across %u CPUs: ", num_booting_cpus());
206
207         /* convert from kcyc/sec to cyc/usec */
208         one_usec = cpu_khz / 1000;
209
210         atomic_set(&tsc_start_flag, 1);
211         wmb();
212
213         /*
214          * We loop a few times to get a primed instruction cache,
215          * then the last pass is more or less synchronized and
216          * the BP and APs set their cycle counters to zero all at
217          * once. This reduces the chance of having random offsets
218          * between the processors, and guarantees that the maximum
219          * delay between the cycle counters is never bigger than
220          * the latency of information-passing (cachelines) between
221          * two CPUs.
222          */
223         for (i = 0; i < NR_LOOPS; i++) {
224                 /*
225                  * all APs synchronize but they loop on '== num_cpus'
226                  */
227                 while (atomic_read(&tsc_count_start) != num_booting_cpus()-1)
228                         mb();
229                 atomic_set(&tsc_count_stop, 0);
230                 wmb();
231                 /*
232                  * this lets the APs save their current TSC:
233                  */
234                 atomic_inc(&tsc_count_start);
235
236                 rdtscll(tsc_values[smp_processor_id()]);
237                 /*
238                  * We clear the TSC in the last loop:
239                  */
240                 if (i == NR_LOOPS-1)
241                         write_tsc(0, 0);
242
243                 /*
244                  * Wait for all APs to leave the synchronization point:
245                  */
246                 while (atomic_read(&tsc_count_stop) != num_booting_cpus()-1)
247                         mb();
248                 atomic_set(&tsc_count_start, 0);
249                 wmb();
250                 atomic_inc(&tsc_count_stop);
251         }
252
253         sum = 0;
254         for (i = 0; i < NR_CPUS; i++) {
255                 if (cpu_isset(i, cpu_callout_map)) {
256                         t0 = tsc_values[i];
257                         sum += t0;
258                 }
259         }
260         avg = sum;
261         do_div(avg, num_booting_cpus());
262
263         sum = 0;
264         for (i = 0; i < NR_CPUS; i++) {
265                 if (!cpu_isset(i, cpu_callout_map))
266                         continue;
267                 delta = tsc_values[i] - avg;
268                 if (delta < 0)
269                         delta = -delta;
270                 /*
271                  * We report bigger than 2 microseconds clock differences.
272                  */
273                 if (delta > 2*one_usec) {
274                         long realdelta;
275                         if (!buggy) {
276                                 buggy = 1;
277                                 printk("\n");
278                         }
279                         realdelta = delta;
280                         do_div(realdelta, one_usec);
281                         if (tsc_values[i] < avg)
282                                 realdelta = -realdelta;
283
284                         printk(KERN_INFO "CPU#%d had %ld usecs TSC skew, fixed it up.\n", i, realdelta);
285                 }
286
287                 sum += delta;
288         }
289         if (!buggy)
290                 printk("passed.\n");
291 }
292
293 static void __init synchronize_tsc_ap (void)
294 {
295         int i;
296
297         /*
298          * Not every cpu is online at the time
299          * this gets called, so we first wait for the BP to
300          * finish SMP initialization:
301          */
302         while (!atomic_read(&tsc_start_flag)) mb();
303
304         for (i = 0; i < NR_LOOPS; i++) {
305                 atomic_inc(&tsc_count_start);
306                 while (atomic_read(&tsc_count_start) != num_booting_cpus())
307                         mb();
308
309                 rdtscll(tsc_values[smp_processor_id()]);
310                 if (i == NR_LOOPS-1)
311                         write_tsc(0, 0);
312
313                 atomic_inc(&tsc_count_stop);
314                 while (atomic_read(&tsc_count_stop) != num_booting_cpus()) mb();
315         }
316 }
317 #undef NR_LOOPS
318
319 extern void calibrate_delay(void);
320
321 static atomic_t init_deasserted;
322
323 static void __init smp_callin(void)
324 {
325         int cpuid, phys_id;
326         unsigned long timeout;
327
328         /*
329          * If waken up by an INIT in an 82489DX configuration
330          * we may get here before an INIT-deassert IPI reaches
331          * our local APIC.  We have to wait for the IPI or we'll
332          * lock up on an APIC access.
333          */
334         wait_for_init_deassert(&init_deasserted);
335
336         /*
337          * (This works even if the APIC is not enabled.)
338          */
339         phys_id = GET_APIC_ID(apic_read(APIC_ID));
340         cpuid = smp_processor_id();
341         if (cpu_isset(cpuid, cpu_callin_map)) {
342                 printk("huh, phys CPU#%d, CPU#%d already present??\n",
343                                         phys_id, cpuid);
344                 BUG();
345         }
346         Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
347
348         /*
349          * STARTUP IPIs are fragile beasts as they might sometimes
350          * trigger some glue motherboard logic. Complete APIC bus
351          * silence for 1 second, this overestimates the time the
352          * boot CPU is spending to send the up to 2 STARTUP IPIs
353          * by a factor of two. This should be enough.
354          */
355
356         /*
357          * Waiting 2s total for startup (udelay is not yet working)
358          */
359         timeout = jiffies + 2*HZ;
360         while (time_before(jiffies, timeout)) {
361                 /*
362                  * Has the boot CPU finished it's STARTUP sequence?
363                  */
364                 if (cpu_isset(cpuid, cpu_callout_map))
365                         break;
366                 rep_nop();
367         }
368
369         if (!time_before(jiffies, timeout)) {
370                 printk("BUG: CPU%d started up but did not get a callout!\n",
371                         cpuid);
372                 BUG();
373         }
374
375         /*
376          * the boot CPU has finished the init stage and is spinning
377          * on callin_map until we finish. We are free to set up this
378          * CPU, first the APIC. (this is probably redundant on most
379          * boards)
380          */
381
382         Dprintk("CALLIN, before setup_local_APIC().\n");
383         smp_callin_clear_local_apic();
384         setup_local_APIC();
385         map_cpu_to_logical_apicid();
386
387         /*
388          * Get our bogomips.
389          */
390         calibrate_delay();
391         Dprintk("Stack at about %p\n",&cpuid);
392
393         /*
394          * Save our processor parameters
395          */
396         smp_store_cpu_info(cpuid);
397
398         disable_APIC_timer();
399
400         /*
401          * Allow the master to continue.
402          */
403         cpu_set(cpuid, cpu_callin_map);
404
405         /*
406          *      Synchronize the TSC with the BP
407          */
408         if (cpu_has_tsc && cpu_khz)
409                 synchronize_tsc_ap();
410 }
411
412 static int cpucount;
413
414 /*
415  * Activate a secondary processor.
416  */
417 static void __init start_secondary(void *unused)
418 {
419         /*
420          * Dont put anything before smp_callin(), SMP
421          * booting is too fragile that we want to limit the
422          * things done here to the most necessary things.
423          */
424         cpu_init();
425         smp_callin();
426         while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
427                 rep_nop();
428         setup_secondary_APIC_clock();
429         if (nmi_watchdog == NMI_IO_APIC) {
430                 disable_8259A_irq(0);
431                 enable_NMI_through_LVT0(NULL);
432                 enable_8259A_irq(0);
433         }
434         enable_APIC_timer();
435         /*
436          * low-memory mappings have been cleared, flush them from
437          * the local TLBs too.
438          */
439         local_flush_tlb();
440         cpu_set(smp_processor_id(), cpu_online_map);
441
442         /* We can take interrupts now: we're officially "up". */
443         local_irq_enable();
444
445         wmb();
446         cpu_idle();
447 }
448
449 /*
450  * Everything has been set up for the secondary
451  * CPUs - they just need to reload everything
452  * from the task structure
453  * This function must not return.
454  */
455 void __init initialize_secondary(void)
456 {
457         /*
458          * We don't actually need to load the full TSS,
459          * basically just the stack pointer and the eip.
460          */
461
462         asm volatile(
463                 "movl %0,%%esp\n\t"
464                 "jmp *%1"
465                 :
466                 :"r" (current->thread.esp),"r" (current->thread.eip));
467 }
468
469 extern struct {
470         void * esp;
471         unsigned short ss;
472 } stack_start;
473
474 #ifdef CONFIG_NUMA
475
476 /* which logical CPUs are on which nodes */
477 cpumask_t node_2_cpu_mask[MAX_NUMNODES] =
478                                 { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
479 /* which node each logical CPU is on */
480 int cpu_2_node[NR_CPUS] = { [0 ... NR_CPUS-1] = 0 };
481 EXPORT_SYMBOL(cpu_2_node);
482
483 /* set up a mapping between cpu and node. */
484 static inline void map_cpu_to_node(int cpu, int node)
485 {
486         printk("Mapping cpu %d to node %d\n", cpu, node);
487         cpu_set(cpu, node_2_cpu_mask[node]);
488         cpu_2_node[cpu] = node;
489 }
490
491 /* undo a mapping between cpu and node. */
492 static inline void unmap_cpu_to_node(int cpu)
493 {
494         int node;
495
496         printk("Unmapping cpu %d from all nodes\n", cpu);
497         for (node = 0; node < MAX_NUMNODES; node ++)
498                 cpu_clear(cpu, node_2_cpu_mask[node]);
499         cpu_2_node[cpu] = 0;
500 }
501 #else /* !CONFIG_NUMA */
502
503 #define map_cpu_to_node(cpu, node)      ({})
504 #define unmap_cpu_to_node(cpu)  ({})
505
506 #endif /* CONFIG_NUMA */
507
508 u8 cpu_2_logical_apicid[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
509
510 static void map_cpu_to_logical_apicid(void)
511 {
512         int cpu = smp_processor_id();
513         int apicid = logical_smp_processor_id();
514
515         cpu_2_logical_apicid[cpu] = apicid;
516         map_cpu_to_node(cpu, apicid_to_node(apicid));
517 }
518
519 static void unmap_cpu_to_logical_apicid(int cpu)
520 {
521         cpu_2_logical_apicid[cpu] = BAD_APICID;
522         unmap_cpu_to_node(cpu);
523 }
524
525 #if APIC_DEBUG
526 static inline void __inquire_remote_apic(int apicid)
527 {
528         int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
529         char *names[] = { "ID", "VERSION", "SPIV" };
530         int timeout, status;
531
532         printk("Inquiring remote APIC #%d...\n", apicid);
533
534         for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
535                 printk("... APIC #%d %s: ", apicid, names[i]);
536
537                 /*
538                  * Wait for idle.
539                  */
540                 apic_wait_icr_idle();
541
542                 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
543                 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
544
545                 timeout = 0;
546                 do {
547                         udelay(100);
548                         status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
549                 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
550
551                 switch (status) {
552                 case APIC_ICR_RR_VALID:
553                         status = apic_read(APIC_RRR);
554                         printk("%08x\n", status);
555                         break;
556                 default:
557                         printk("failed\n");
558                 }
559         }
560 }
561 #endif
562
563 #ifdef WAKE_SECONDARY_VIA_NMI
564 /* 
565  * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
566  * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
567  * won't ... remember to clear down the APIC, etc later.
568  */
569 static int __init
570 wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
571 {
572         unsigned long send_status = 0, accept_status = 0;
573         int timeout, maxlvt;
574
575         /* Target chip */
576         apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
577
578         /* Boot on the stack */
579         /* Kick the second */
580         apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
581
582         Dprintk("Waiting for send to finish...\n");
583         timeout = 0;
584         do {
585                 Dprintk("+");
586                 udelay(100);
587                 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
588         } while (send_status && (timeout++ < 1000));
589
590         /*
591          * Give the other CPU some time to accept the IPI.
592          */
593         udelay(200);
594         /*
595          * Due to the Pentium erratum 3AP.
596          */
597         maxlvt = get_maxlvt();
598         if (maxlvt > 3) {
599                 apic_read_around(APIC_SPIV);
600                 apic_write(APIC_ESR, 0);
601         }
602         accept_status = (apic_read(APIC_ESR) & 0xEF);
603         Dprintk("NMI sent.\n");
604
605         if (send_status)
606                 printk("APIC never delivered???\n");
607         if (accept_status)
608                 printk("APIC delivery error (%lx).\n", accept_status);
609
610         return (send_status | accept_status);
611 }
612 #endif  /* WAKE_SECONDARY_VIA_NMI */
613
614 #ifdef WAKE_SECONDARY_VIA_INIT
615 static int __init
616 wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
617 {
618         unsigned long send_status = 0, accept_status = 0;
619         int maxlvt, timeout, num_starts, j;
620
621         /*
622          * Be paranoid about clearing APIC errors.
623          */
624         if (APIC_INTEGRATED(apic_version[phys_apicid])) {
625                 apic_read_around(APIC_SPIV);
626                 apic_write(APIC_ESR, 0);
627                 apic_read(APIC_ESR);
628         }
629
630         Dprintk("Asserting INIT.\n");
631
632         /*
633          * Turn INIT on target chip
634          */
635         apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
636
637         /*
638          * Send IPI
639          */
640         apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
641                                 | APIC_DM_INIT);
642
643         Dprintk("Waiting for send to finish...\n");
644         timeout = 0;
645         do {
646                 Dprintk("+");
647                 udelay(100);
648                 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
649         } while (send_status && (timeout++ < 1000));
650
651         mdelay(10);
652
653         Dprintk("Deasserting INIT.\n");
654
655         /* Target chip */
656         apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
657
658         /* Send IPI */
659         apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
660
661         Dprintk("Waiting for send to finish...\n");
662         timeout = 0;
663         do {
664                 Dprintk("+");
665                 udelay(100);
666                 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
667         } while (send_status && (timeout++ < 1000));
668
669         atomic_set(&init_deasserted, 1);
670
671         /*
672          * Should we send STARTUP IPIs ?
673          *
674          * Determine this based on the APIC version.
675          * If we don't have an integrated APIC, don't send the STARTUP IPIs.
676          */
677         if (APIC_INTEGRATED(apic_version[phys_apicid]))
678                 num_starts = 2;
679         else
680                 num_starts = 0;
681
682         /*
683          * Run STARTUP IPI loop.
684          */
685         Dprintk("#startup loops: %d.\n", num_starts);
686
687         maxlvt = get_maxlvt();
688
689         for (j = 1; j <= num_starts; j++) {
690                 Dprintk("Sending STARTUP #%d.\n",j);
691                 apic_read_around(APIC_SPIV);
692                 apic_write(APIC_ESR, 0);
693                 apic_read(APIC_ESR);
694                 Dprintk("After apic_write.\n");
695
696                 /*
697                  * STARTUP IPI
698                  */
699
700                 /* Target chip */
701                 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
702
703                 /* Boot on the stack */
704                 /* Kick the second */
705                 apic_write_around(APIC_ICR, APIC_DM_STARTUP
706                                         | (start_eip >> 12));
707
708                 /*
709                  * Give the other CPU some time to accept the IPI.
710                  */
711                 udelay(300);
712
713                 Dprintk("Startup point 1.\n");
714
715                 Dprintk("Waiting for send to finish...\n");
716                 timeout = 0;
717                 do {
718                         Dprintk("+");
719                         udelay(100);
720                         send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
721                 } while (send_status && (timeout++ < 1000));
722
723                 /*
724                  * Give the other CPU some time to accept the IPI.
725                  */
726                 udelay(200);
727                 /*
728                  * Due to the Pentium erratum 3AP.
729                  */
730                 if (maxlvt > 3) {
731                         apic_read_around(APIC_SPIV);
732                         apic_write(APIC_ESR, 0);
733                 }
734                 accept_status = (apic_read(APIC_ESR) & 0xEF);
735                 if (send_status || accept_status)
736                         break;
737         }
738         Dprintk("After Startup.\n");
739
740         if (send_status)
741                 printk("APIC never delivered???\n");
742         if (accept_status)
743                 printk("APIC delivery error (%lx).\n", accept_status);
744
745         return (send_status | accept_status);
746 }
747 #endif  /* WAKE_SECONDARY_VIA_INIT */
748
749 extern cpumask_t cpu_initialized;
750
751 static int __init do_boot_cpu(int apicid)
752 /*
753  * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
754  * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
755  * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
756  */
757 {
758         struct task_struct *idle;
759         unsigned long boot_error;
760         int timeout, cpu;
761         unsigned long start_eip;
762         unsigned short nmi_high = 0, nmi_low = 0;
763
764         cpu = ++cpucount;
765         /*
766          * We can't use kernel_thread since we must avoid to
767          * reschedule the child.
768          */
769         idle = fork_idle(cpu);
770         if (IS_ERR(idle))
771                 panic("failed fork for CPU %d", cpu);
772         idle->thread.eip = (unsigned long) start_secondary;
773         /* start_eip had better be page-aligned! */
774         start_eip = setup_trampoline();
775
776         /* So we see what's up   */
777         printk("Booting processor %d/%d eip %lx\n", cpu, apicid, start_eip);
778         /* Stack for startup_32 can be just as for start_secondary onwards */
779         stack_start.esp = (void *) idle->thread.esp;
780
781         irq_ctx_init(cpu);
782
783         /*
784          * This grunge runs the startup process for
785          * the targeted processor.
786          */
787
788         atomic_set(&init_deasserted, 0);
789
790         Dprintk("Setting warm reset code and vector.\n");
791
792         store_NMI_vector(&nmi_high, &nmi_low);
793
794         smpboot_setup_warm_reset_vector(start_eip);
795
796         /*
797          * Starting actual IPI sequence...
798          */
799         boot_error = wakeup_secondary_cpu(apicid, start_eip);
800
801         if (!boot_error) {
802                 /*
803                  * allow APs to start initializing.
804                  */
805                 Dprintk("Before Callout %d.\n", cpu);
806                 cpu_set(cpu, cpu_callout_map);
807                 Dprintk("After Callout %d.\n", cpu);
808
809                 /*
810                  * Wait 5s total for a response
811                  */
812                 for (timeout = 0; timeout < 50000; timeout++) {
813                         if (cpu_isset(cpu, cpu_callin_map))
814                                 break;  /* It has booted */
815                         udelay(100);
816                 }
817
818                 if (cpu_isset(cpu, cpu_callin_map)) {
819                         /* number CPUs logically, starting from 1 (BSP is 0) */
820                         Dprintk("OK.\n");
821                         printk("CPU%d: ", cpu);
822                         print_cpu_info(&cpu_data[cpu]);
823                         Dprintk("CPU has booted.\n");
824                 } else {
825                         boot_error= 1;
826                         if (*((volatile unsigned char *)trampoline_base)
827                                         == 0xA5)
828                                 /* trampoline started but...? */
829                                 printk("Stuck ??\n");
830                         else
831                                 /* trampoline code not run */
832                                 printk("Not responding.\n");
833                         inquire_remote_apic(apicid);
834                 }
835         }
836         x86_cpu_to_apicid[cpu] = apicid;
837         if (boot_error) {
838                 /* Try to put things back the way they were before ... */
839                 unmap_cpu_to_logical_apicid(cpu);
840                 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
841                 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
842                 cpucount--;
843         }
844
845         /* mark "stuck" area as not stuck */
846         *((volatile unsigned long *)trampoline_base) = 0;
847
848         return boot_error;
849 }
850
851 static void smp_tune_scheduling (void)
852 {
853         unsigned long cachesize;       /* kB   */
854         unsigned long bandwidth = 350; /* MB/s */
855         /*
856          * Rough estimation for SMP scheduling, this is the number of
857          * cycles it takes for a fully memory-limited process to flush
858          * the SMP-local cache.
859          *
860          * (For a P5 this pretty much means we will choose another idle
861          *  CPU almost always at wakeup time (this is due to the small
862          *  L1 cache), on PIIs it's around 50-100 usecs, depending on
863          *  the cache size)
864          */
865
866         if (!cpu_khz) {
867                 /*
868                  * this basically disables processor-affinity
869                  * scheduling on SMP without a TSC.
870                  */
871                 return;
872         } else {
873                 cachesize = boot_cpu_data.x86_cache_size;
874                 if (cachesize == -1) {
875                         cachesize = 16; /* Pentiums, 2x8kB cache */
876                         bandwidth = 100;
877                 }
878         }
879 }
880
881 /*
882  * Cycle through the processors sending APIC IPIs to boot each.
883  */
884
885 static int boot_cpu_logical_apicid;
886 /* Where the IO area was mapped on multiquad, always 0 otherwise */
887 void *xquad_portio;
888
889 cpumask_t cpu_sibling_map[NR_CPUS] __cacheline_aligned;
890 cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
891 EXPORT_SYMBOL(cpu_core_map);
892
893 static void __init smp_boot_cpus(unsigned int max_cpus)
894 {
895         int apicid, cpu, bit, kicked;
896         unsigned long bogosum = 0;
897
898         /*
899          * Setup boot CPU information
900          */
901         smp_store_cpu_info(0); /* Final full version of the data */
902         printk("CPU%d: ", 0);
903         print_cpu_info(&cpu_data[0]);
904
905         boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
906         boot_cpu_logical_apicid = logical_smp_processor_id();
907         x86_cpu_to_apicid[0] = boot_cpu_physical_apicid;
908
909         current_thread_info()->cpu = 0;
910         smp_tune_scheduling();
911         cpus_clear(cpu_sibling_map[0]);
912         cpu_set(0, cpu_sibling_map[0]);
913
914         cpus_clear(cpu_core_map[0]);
915         cpu_set(0, cpu_core_map[0]);
916
917         /*
918          * If we couldn't find an SMP configuration at boot time,
919          * get out of here now!
920          */
921         if (!smp_found_config && !acpi_lapic) {
922                 printk(KERN_NOTICE "SMP motherboard not detected.\n");
923                 smpboot_clear_io_apic_irqs();
924                 phys_cpu_present_map = physid_mask_of_physid(0);
925                 if (APIC_init_uniprocessor())
926                         printk(KERN_NOTICE "Local APIC not detected."
927                                            " Using dummy APIC emulation.\n");
928                 map_cpu_to_logical_apicid();
929                 cpu_set(0, cpu_sibling_map[0]);
930                 cpu_set(0, cpu_core_map[0]);
931                 return;
932         }
933
934         /*
935          * Should not be necessary because the MP table should list the boot
936          * CPU too, but we do it for the sake of robustness anyway.
937          * Makes no sense to do this check in clustered apic mode, so skip it
938          */
939         if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
940                 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
941                                 boot_cpu_physical_apicid);
942                 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
943         }
944
945         /*
946          * If we couldn't find a local APIC, then get out of here now!
947          */
948         if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
949                 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
950                         boot_cpu_physical_apicid);
951                 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
952                 smpboot_clear_io_apic_irqs();
953                 phys_cpu_present_map = physid_mask_of_physid(0);
954                 cpu_set(0, cpu_sibling_map[0]);
955                 cpu_set(0, cpu_core_map[0]);
956                 return;
957         }
958
959         verify_local_APIC();
960
961         /*
962          * If SMP should be disabled, then really disable it!
963          */
964         if (!max_cpus) {
965                 smp_found_config = 0;
966                 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
967                 smpboot_clear_io_apic_irqs();
968                 phys_cpu_present_map = physid_mask_of_physid(0);
969                 cpu_set(0, cpu_sibling_map[0]);
970                 cpu_set(0, cpu_core_map[0]);
971                 return;
972         }
973
974         connect_bsp_APIC();
975         setup_local_APIC();
976         map_cpu_to_logical_apicid();
977
978
979         setup_portio_remap();
980
981         /*
982          * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
983          *
984          * In clustered apic mode, phys_cpu_present_map is a constructed thus:
985          * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the 
986          * clustered apic ID.
987          */
988         Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
989
990         kicked = 1;
991         for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
992                 apicid = cpu_present_to_apicid(bit);
993                 /*
994                  * Don't even attempt to start the boot CPU!
995                  */
996                 if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
997                         continue;
998
999                 if (!check_apicid_present(bit))
1000                         continue;
1001                 if (max_cpus <= cpucount+1)
1002                         continue;
1003
1004                 if (do_boot_cpu(apicid))
1005                         printk("CPU #%d not responding - cannot use it.\n",
1006                                                                 apicid);
1007                 else
1008                         ++kicked;
1009         }
1010
1011         /*
1012          * Cleanup possible dangling ends...
1013          */
1014         smpboot_restore_warm_reset_vector();
1015
1016         /*
1017          * Allow the user to impress friends.
1018          */
1019         Dprintk("Before bogomips.\n");
1020         for (cpu = 0; cpu < NR_CPUS; cpu++)
1021                 if (cpu_isset(cpu, cpu_callout_map))
1022                         bogosum += cpu_data[cpu].loops_per_jiffy;
1023         printk(KERN_INFO
1024                 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
1025                 cpucount+1,
1026                 bogosum/(500000/HZ),
1027                 (bogosum/(5000/HZ))%100);
1028         
1029         Dprintk("Before bogocount - setting activated=1.\n");
1030
1031         if (smp_b_stepping)
1032                 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
1033
1034         /*
1035          * Don't taint if we are running SMP kernel on a single non-MP
1036          * approved Athlon
1037          */
1038         if (tainted & TAINT_UNSAFE_SMP) {
1039                 if (cpucount)
1040                         printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
1041                 else
1042                         tainted &= ~TAINT_UNSAFE_SMP;
1043         }
1044
1045         Dprintk("Boot done.\n");
1046
1047         /*
1048          * construct cpu_sibling_map[], so that we can tell sibling CPUs
1049          * efficiently.
1050          */
1051         for (cpu = 0; cpu < NR_CPUS; cpu++) {
1052                 cpus_clear(cpu_sibling_map[cpu]);
1053                 cpus_clear(cpu_core_map[cpu]);
1054         }
1055
1056         for (cpu = 0; cpu < NR_CPUS; cpu++) {
1057                 struct cpuinfo_x86 *c = cpu_data + cpu;
1058                 int siblings = 0;
1059                 int i;
1060                 if (!cpu_isset(cpu, cpu_callout_map))
1061                         continue;
1062
1063                 if (smp_num_siblings > 1) {
1064                         for (i = 0; i < NR_CPUS; i++) {
1065                                 if (!cpu_isset(i, cpu_callout_map))
1066                                         continue;
1067                                 if (cpu_core_id[cpu] == cpu_core_id[i]) {
1068                                         siblings++;
1069                                         cpu_set(i, cpu_sibling_map[cpu]);
1070                                 }
1071                         }
1072                 } else {
1073                         siblings++;
1074                         cpu_set(cpu, cpu_sibling_map[cpu]);
1075                 }
1076
1077                 if (siblings != smp_num_siblings) {
1078                         printk(KERN_WARNING "WARNING: %d siblings found for CPU%d, should be %d\n", siblings, cpu, smp_num_siblings);
1079                         smp_num_siblings = siblings;
1080                 }
1081
1082                 if (c->x86_num_cores > 1) {
1083                         for (i = 0; i < NR_CPUS; i++) {
1084                                 if (!cpu_isset(i, cpu_callout_map))
1085                                         continue;
1086                                 if (phys_proc_id[cpu] == phys_proc_id[i]) {
1087                                         cpu_set(i, cpu_core_map[cpu]);
1088                                 }
1089                         }
1090                 } else {
1091                         cpu_core_map[cpu] = cpu_sibling_map[cpu];
1092                 }
1093         }
1094
1095         smpboot_setup_io_apic();
1096
1097         setup_boot_APIC_clock();
1098
1099         /*
1100          * Synchronize the TSC with the AP
1101          */
1102         if (cpu_has_tsc && cpucount && cpu_khz)
1103                 synchronize_tsc_bp();
1104 }
1105
1106 /* These are wrappers to interface to the new boot process.  Someone
1107    who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
1108 void __init smp_prepare_cpus(unsigned int max_cpus)
1109 {
1110         smp_boot_cpus(max_cpus);
1111 }
1112
1113 void __devinit smp_prepare_boot_cpu(void)
1114 {
1115         cpu_set(smp_processor_id(), cpu_online_map);
1116         cpu_set(smp_processor_id(), cpu_callout_map);
1117 }
1118
1119 int __devinit __cpu_up(unsigned int cpu)
1120 {
1121         /* This only works at boot for x86.  See "rewrite" above. */
1122         if (cpu_isset(cpu, smp_commenced_mask)) {
1123                 local_irq_enable();
1124                 return -ENOSYS;
1125         }
1126
1127         /* In case one didn't come up */
1128         if (!cpu_isset(cpu, cpu_callin_map)) {
1129                 local_irq_enable();
1130                 return -EIO;
1131         }
1132
1133         local_irq_enable();
1134         /* Unleash the CPU! */
1135         cpu_set(cpu, smp_commenced_mask);
1136         while (!cpu_isset(cpu, cpu_online_map))
1137                 mb();
1138         return 0;
1139 }
1140
1141 void __init smp_cpus_done(unsigned int max_cpus)
1142 {
1143 #ifdef CONFIG_X86_IO_APIC
1144         setup_ioapic_dest();
1145 #endif
1146         zap_low_mappings();
1147         /*
1148          * Disable executability of the SMP trampoline:
1149          */
1150         set_kernel_exec((unsigned long)trampoline_base, trampoline_exec);
1151 }
1152
1153 void __init smp_intr_init(void)
1154 {
1155         /*
1156          * IRQ0 must be given a fixed assignment and initialized,
1157          * because it's used before the IO-APIC is set up.
1158          */
1159         set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1160
1161         /*
1162          * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1163          * IPI, driven by wakeup.
1164          */
1165         set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1166
1167         /* IPI for invalidation */
1168         set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1169
1170         /* IPI for generic function call */
1171         set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1172 }