[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / arch / i386 / kernel / smpboot.c
blobcbea7ac582e57a09fe919e4cc444c53a6a525ba4
1 /*
2 * x86 SMP booting functions
4 * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
5 * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
7 * Much of the core SMP work is based on previous work by Thomas Radke, to
8 * whom a great many thanks are extended.
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.
14 * This code is released under the GNU General Public License version 2 or
15 * later.
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. */
36 #include <linux/module.h>
37 #include <linux/config.h>
38 #include <linux/init.h>
39 #include <linux/kernel.h>
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>
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>
54 #include <mach_apic.h>
55 #include <mach_wakecpu.h>
56 #include <smpboot_hooks.h>
58 /* Set if we find a B stepping CPU */
59 static int __initdata smp_b_stepping;
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);
68 /* bitmap of online cpus */
69 cpumask_t cpu_online_map;
71 cpumask_t cpu_callin_map;
72 cpumask_t cpu_callout_map;
73 static cpumask_t smp_commenced_mask;
75 /* Per CPU bogomips and other parameters */
76 struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
78 u8 x86_cpu_to_apicid[NR_CPUS] =
79 { [0 ... NR_CPUS-1] = 0xff };
80 EXPORT_SYMBOL(x86_cpu_to_apicid);
83 * Trampoline 80x86 program as an array.
86 extern unsigned char trampoline_data [];
87 extern unsigned char trampoline_end [];
88 static unsigned char *trampoline_base;
89 static int trampoline_exec;
91 static void map_cpu_to_logical_apicid(void);
94 * Currently trivial. Write the real->protected mode
95 * bootstrap into the page concerned. The caller
96 * has made sure it's suitably aligned.
99 static unsigned long __init setup_trampoline(void)
101 memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
102 return virt_to_phys(trampoline_base);
106 * We are called very early to get the low memory for the
107 * SMP bootup trampoline page.
109 void __init smp_alloc_memory(void)
111 trampoline_base = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
113 * Has to be in very low memory so we can execute
114 * real-mode AP code.
116 if (__pa(trampoline_base) >= 0x9F000)
117 BUG();
119 * Make the SMP trampoline executable:
121 trampoline_exec = set_kernel_exec((unsigned long)trampoline_base, 1);
125 * The bootstrap kernel entry code has set these up. Save them for
126 * a given CPU
129 static void __init smp_store_cpu_info(int id)
131 struct cpuinfo_x86 *c = cpu_data + id;
133 *c = boot_cpu_data;
134 if (id!=0)
135 identify_cpu(c);
137 * Mask B, Pentium, but not Pentium MMX
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)
144 * Remember we have B step Pentia with bugs
146 smp_b_stepping = 1;
149 * Certain Athlons might work (for various values of 'work') in SMP
150 * but they are not certified as MP capable.
152 if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
154 /* Athlon 660/661 is valid. */
155 if ((c->x86_model==6) && ((c->x86_mask==0) || (c->x86_mask==1)))
156 goto valid_k7;
158 /* Duron 670 is valid */
159 if ((c->x86_model==7) && (c->x86_mask==0))
160 goto valid_k7;
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.
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;
174 /* If we get here, it's not a certified SMP capable AMD system. */
175 tainted |= TAINT_UNSAFE_SMP;
178 valid_k7:
183 * TSC synchronization.
185 * We first check whether all CPUs have their TSC's synchronized,
186 * then we print a warning if not, and always resync.
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];
194 #define NR_LOOPS 5
196 static void __init synchronize_tsc_bp (void)
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;
205 printk(KERN_INFO "checking TSC synchronization across %u CPUs: ", num_booting_cpus());
207 /* convert from kcyc/sec to cyc/usec */
208 one_usec = cpu_khz / 1000;
210 atomic_set(&tsc_start_flag, 1);
211 wmb();
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.
223 for (i = 0; i < NR_LOOPS; i++) {
225 * all APs synchronize but they loop on '== num_cpus'
227 while (atomic_read(&tsc_count_start) != num_booting_cpus()-1)
228 mb();
229 atomic_set(&tsc_count_stop, 0);
230 wmb();
232 * this lets the APs save their current TSC:
234 atomic_inc(&tsc_count_start);
236 rdtscll(tsc_values[smp_processor_id()]);
238 * We clear the TSC in the last loop:
240 if (i == NR_LOOPS-1)
241 write_tsc(0, 0);
244 * Wait for all APs to leave the synchronization point:
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);
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;
260 avg = sum;
261 do_div(avg, num_booting_cpus());
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;
271 * We report bigger than 2 microseconds clock differences.
273 if (delta > 2*one_usec) {
274 long realdelta;
275 if (!buggy) {
276 buggy = 1;
277 printk("\n");
279 realdelta = delta;
280 do_div(realdelta, one_usec);
281 if (tsc_values[i] < avg)
282 realdelta = -realdelta;
284 printk(KERN_INFO "CPU#%d had %ld usecs TSC skew, fixed it up.\n", i, realdelta);
287 sum += delta;
289 if (!buggy)
290 printk("passed.\n");
293 static void __init synchronize_tsc_ap (void)
295 int i;
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:
302 while (!atomic_read(&tsc_start_flag)) mb();
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();
309 rdtscll(tsc_values[smp_processor_id()]);
310 if (i == NR_LOOPS-1)
311 write_tsc(0, 0);
313 atomic_inc(&tsc_count_stop);
314 while (atomic_read(&tsc_count_stop) != num_booting_cpus()) mb();
317 #undef NR_LOOPS
319 extern void calibrate_delay(void);
321 static atomic_t init_deasserted;
323 static void __init smp_callin(void)
325 int cpuid, phys_id;
326 unsigned long timeout;
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.
334 wait_for_init_deassert(&init_deasserted);
337 * (This works even if the APIC is not enabled.)
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();
346 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
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.
357 * Waiting 2s total for startup (udelay is not yet working)
359 timeout = jiffies + 2*HZ;
360 while (time_before(jiffies, timeout)) {
362 * Has the boot CPU finished it's STARTUP sequence?
364 if (cpu_isset(cpuid, cpu_callout_map))
365 break;
366 rep_nop();
369 if (!time_before(jiffies, timeout)) {
370 printk("BUG: CPU%d started up but did not get a callout!\n",
371 cpuid);
372 BUG();
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)
382 Dprintk("CALLIN, before setup_local_APIC().\n");
383 smp_callin_clear_local_apic();
384 setup_local_APIC();
385 map_cpu_to_logical_apicid();
388 * Get our bogomips.
390 calibrate_delay();
391 Dprintk("Stack at about %p\n",&cpuid);
394 * Save our processor parameters
396 smp_store_cpu_info(cpuid);
398 disable_APIC_timer();
401 * Allow the master to continue.
403 cpu_set(cpuid, cpu_callin_map);
406 * Synchronize the TSC with the BP
408 if (cpu_has_tsc && cpu_khz)
409 synchronize_tsc_ap();
412 static int cpucount;
415 * Activate a secondary processor.
417 static void __init start_secondary(void *unused)
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.
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);
434 enable_APIC_timer();
436 * low-memory mappings have been cleared, flush them from
437 * the local TLBs too.
439 local_flush_tlb();
440 cpu_set(smp_processor_id(), cpu_online_map);
442 /* We can take interrupts now: we're officially "up". */
443 local_irq_enable();
445 wmb();
446 cpu_idle();
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.
455 void __init initialize_secondary(void)
458 * We don't actually need to load the full TSS,
459 * basically just the stack pointer and the eip.
462 asm volatile(
463 "movl %0,%%esp\n\t"
464 "jmp *%1"
466 :"r" (current->thread.esp),"r" (current->thread.eip));
469 extern struct {
470 void * esp;
471 unsigned short ss;
472 } stack_start;
474 #ifdef CONFIG_NUMA
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);
483 /* set up a mapping between cpu and node. */
484 static inline void map_cpu_to_node(int cpu, int node)
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;
491 /* undo a mapping between cpu and node. */
492 static inline void unmap_cpu_to_node(int cpu)
494 int node;
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;
501 #else /* !CONFIG_NUMA */
503 #define map_cpu_to_node(cpu, node) ({})
504 #define unmap_cpu_to_node(cpu) ({})
506 #endif /* CONFIG_NUMA */
508 u8 cpu_2_logical_apicid[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
510 static void map_cpu_to_logical_apicid(void)
512 int cpu = smp_processor_id();
513 int apicid = logical_smp_processor_id();
515 cpu_2_logical_apicid[cpu] = apicid;
516 map_cpu_to_node(cpu, apicid_to_node(apicid));
519 static void unmap_cpu_to_logical_apicid(int cpu)
521 cpu_2_logical_apicid[cpu] = BAD_APICID;
522 unmap_cpu_to_node(cpu);
525 #if APIC_DEBUG
526 static inline void __inquire_remote_apic(int apicid)
528 int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
529 char *names[] = { "ID", "VERSION", "SPIV" };
530 int timeout, status;
532 printk("Inquiring remote APIC #%d...\n", apicid);
534 for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
535 printk("... APIC #%d %s: ", apicid, names[i]);
538 * Wait for idle.
540 apic_wait_icr_idle();
542 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
543 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
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);
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");
561 #endif
563 #ifdef WAKE_SECONDARY_VIA_NMI
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.
569 static int __init
570 wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
572 unsigned long send_status = 0, accept_status = 0;
573 int timeout, maxlvt;
575 /* Target chip */
576 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
578 /* Boot on the stack */
579 /* Kick the second */
580 apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
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));
591 * Give the other CPU some time to accept the IPI.
593 udelay(200);
595 * Due to the Pentium erratum 3AP.
597 maxlvt = get_maxlvt();
598 if (maxlvt > 3) {
599 apic_read_around(APIC_SPIV);
600 apic_write(APIC_ESR, 0);
602 accept_status = (apic_read(APIC_ESR) & 0xEF);
603 Dprintk("NMI sent.\n");
605 if (send_status)
606 printk("APIC never delivered???\n");
607 if (accept_status)
608 printk("APIC delivery error (%lx).\n", accept_status);
610 return (send_status | accept_status);
612 #endif /* WAKE_SECONDARY_VIA_NMI */
614 #ifdef WAKE_SECONDARY_VIA_INIT
615 static int __init
616 wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
618 unsigned long send_status = 0, accept_status = 0;
619 int maxlvt, timeout, num_starts, j;
622 * Be paranoid about clearing APIC errors.
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);
630 Dprintk("Asserting INIT.\n");
633 * Turn INIT on target chip
635 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
638 * Send IPI
640 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
641 | APIC_DM_INIT);
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));
651 mdelay(10);
653 Dprintk("Deasserting INIT.\n");
655 /* Target chip */
656 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
658 /* Send IPI */
659 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
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));
669 atomic_set(&init_deasserted, 1);
672 * Should we send STARTUP IPIs ?
674 * Determine this based on the APIC version.
675 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
677 if (APIC_INTEGRATED(apic_version[phys_apicid]))
678 num_starts = 2;
679 else
680 num_starts = 0;
683 * Run STARTUP IPI loop.
685 Dprintk("#startup loops: %d.\n", num_starts);
687 maxlvt = get_maxlvt();
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");
697 * STARTUP IPI
700 /* Target chip */
701 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
703 /* Boot on the stack */
704 /* Kick the second */
705 apic_write_around(APIC_ICR, APIC_DM_STARTUP
706 | (start_eip >> 12));
709 * Give the other CPU some time to accept the IPI.
711 udelay(300);
713 Dprintk("Startup point 1.\n");
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));
724 * Give the other CPU some time to accept the IPI.
726 udelay(200);
728 * Due to the Pentium erratum 3AP.
730 if (maxlvt > 3) {
731 apic_read_around(APIC_SPIV);
732 apic_write(APIC_ESR, 0);
734 accept_status = (apic_read(APIC_ESR) & 0xEF);
735 if (send_status || accept_status)
736 break;
738 Dprintk("After Startup.\n");
740 if (send_status)
741 printk("APIC never delivered???\n");
742 if (accept_status)
743 printk("APIC delivery error (%lx).\n", accept_status);
745 return (send_status | accept_status);
747 #endif /* WAKE_SECONDARY_VIA_INIT */
749 extern cpumask_t cpu_initialized;
751 static int __init do_boot_cpu(int apicid)
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.
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;
764 cpu = ++cpucount;
766 * We can't use kernel_thread since we must avoid to
767 * reschedule the child.
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();
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;
781 irq_ctx_init(cpu);
784 * This grunge runs the startup process for
785 * the targeted processor.
788 atomic_set(&init_deasserted, 0);
790 Dprintk("Setting warm reset code and vector.\n");
792 store_NMI_vector(&nmi_high, &nmi_low);
794 smpboot_setup_warm_reset_vector(start_eip);
797 * Starting actual IPI sequence...
799 boot_error = wakeup_secondary_cpu(apicid, start_eip);
801 if (!boot_error) {
803 * allow APs to start initializing.
805 Dprintk("Before Callout %d.\n", cpu);
806 cpu_set(cpu, cpu_callout_map);
807 Dprintk("After Callout %d.\n", cpu);
810 * Wait 5s total for a response
812 for (timeout = 0; timeout < 50000; timeout++) {
813 if (cpu_isset(cpu, cpu_callin_map))
814 break; /* It has booted */
815 udelay(100);
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);
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--;
845 /* mark "stuck" area as not stuck */
846 *((volatile unsigned long *)trampoline_base) = 0;
848 return boot_error;
851 static void smp_tune_scheduling (void)
853 unsigned long cachesize; /* kB */
854 unsigned long bandwidth = 350; /* MB/s */
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.
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)
866 if (!cpu_khz) {
868 * this basically disables processor-affinity
869 * scheduling on SMP without a TSC.
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;
882 * Cycle through the processors sending APIC IPIs to boot each.
885 static int boot_cpu_logical_apicid;
886 /* Where the IO area was mapped on multiquad, always 0 otherwise */
887 void *xquad_portio;
889 cpumask_t cpu_sibling_map[NR_CPUS] __cacheline_aligned;
890 cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
892 static void __init smp_boot_cpus(unsigned int max_cpus)
894 int apicid, cpu, bit, kicked;
895 unsigned long bogosum = 0;
898 * Setup boot CPU information
900 smp_store_cpu_info(0); /* Final full version of the data */
901 printk("CPU%d: ", 0);
902 print_cpu_info(&cpu_data[0]);
904 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
905 boot_cpu_logical_apicid = logical_smp_processor_id();
906 x86_cpu_to_apicid[0] = boot_cpu_physical_apicid;
908 current_thread_info()->cpu = 0;
909 smp_tune_scheduling();
910 cpus_clear(cpu_sibling_map[0]);
911 cpu_set(0, cpu_sibling_map[0]);
913 cpus_clear(cpu_core_map[0]);
914 cpu_set(0, cpu_core_map[0]);
917 * If we couldn't find an SMP configuration at boot time,
918 * get out of here now!
920 if (!smp_found_config && !acpi_lapic) {
921 printk(KERN_NOTICE "SMP motherboard not detected.\n");
922 smpboot_clear_io_apic_irqs();
923 phys_cpu_present_map = physid_mask_of_physid(0);
924 if (APIC_init_uniprocessor())
925 printk(KERN_NOTICE "Local APIC not detected."
926 " Using dummy APIC emulation.\n");
927 map_cpu_to_logical_apicid();
928 cpu_set(0, cpu_sibling_map[0]);
929 cpu_set(0, cpu_core_map[0]);
930 return;
934 * Should not be necessary because the MP table should list the boot
935 * CPU too, but we do it for the sake of robustness anyway.
936 * Makes no sense to do this check in clustered apic mode, so skip it
938 if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
939 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
940 boot_cpu_physical_apicid);
941 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
945 * If we couldn't find a local APIC, then get out of here now!
947 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
948 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
949 boot_cpu_physical_apicid);
950 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
951 smpboot_clear_io_apic_irqs();
952 phys_cpu_present_map = physid_mask_of_physid(0);
953 cpu_set(0, cpu_sibling_map[0]);
954 cpu_set(0, cpu_core_map[0]);
955 return;
958 verify_local_APIC();
961 * If SMP should be disabled, then really disable it!
963 if (!max_cpus) {
964 smp_found_config = 0;
965 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
966 smpboot_clear_io_apic_irqs();
967 phys_cpu_present_map = physid_mask_of_physid(0);
968 cpu_set(0, cpu_sibling_map[0]);
969 cpu_set(0, cpu_core_map[0]);
970 return;
973 connect_bsp_APIC();
974 setup_local_APIC();
975 map_cpu_to_logical_apicid();
978 setup_portio_remap();
981 * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
983 * In clustered apic mode, phys_cpu_present_map is a constructed thus:
984 * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
985 * clustered apic ID.
987 Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
989 kicked = 1;
990 for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
991 apicid = cpu_present_to_apicid(bit);
993 * Don't even attempt to start the boot CPU!
995 if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
996 continue;
998 if (!check_apicid_present(bit))
999 continue;
1000 if (max_cpus <= cpucount+1)
1001 continue;
1003 if (do_boot_cpu(apicid))
1004 printk("CPU #%d not responding - cannot use it.\n",
1005 apicid);
1006 else
1007 ++kicked;
1011 * Cleanup possible dangling ends...
1013 smpboot_restore_warm_reset_vector();
1016 * Allow the user to impress friends.
1018 Dprintk("Before bogomips.\n");
1019 for (cpu = 0; cpu < NR_CPUS; cpu++)
1020 if (cpu_isset(cpu, cpu_callout_map))
1021 bogosum += cpu_data[cpu].loops_per_jiffy;
1022 printk(KERN_INFO
1023 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
1024 cpucount+1,
1025 bogosum/(500000/HZ),
1026 (bogosum/(5000/HZ))%100);
1028 Dprintk("Before bogocount - setting activated=1.\n");
1030 if (smp_b_stepping)
1031 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
1034 * Don't taint if we are running SMP kernel on a single non-MP
1035 * approved Athlon
1037 if (tainted & TAINT_UNSAFE_SMP) {
1038 if (cpucount)
1039 printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
1040 else
1041 tainted &= ~TAINT_UNSAFE_SMP;
1044 Dprintk("Boot done.\n");
1047 * construct cpu_sibling_map[], so that we can tell sibling CPUs
1048 * efficiently.
1050 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1051 cpus_clear(cpu_sibling_map[cpu]);
1052 cpus_clear(cpu_core_map[cpu]);
1055 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1056 struct cpuinfo_x86 *c = cpu_data + cpu;
1057 int siblings = 0;
1058 int i;
1059 if (!cpu_isset(cpu, cpu_callout_map))
1060 continue;
1062 if (smp_num_siblings > 1) {
1063 for (i = 0; i < NR_CPUS; i++) {
1064 if (!cpu_isset(i, cpu_callout_map))
1065 continue;
1066 if (cpu_core_id[cpu] == cpu_core_id[i]) {
1067 siblings++;
1068 cpu_set(i, cpu_sibling_map[cpu]);
1071 } else {
1072 siblings++;
1073 cpu_set(cpu, cpu_sibling_map[cpu]);
1076 if (siblings != smp_num_siblings)
1077 printk(KERN_WARNING "WARNING: %d siblings found for CPU%d, should be %d\n", siblings, cpu, smp_num_siblings);
1079 if (c->x86_num_cores > 1) {
1080 for (i = 0; i < NR_CPUS; i++) {
1081 if (!cpu_isset(i, cpu_callout_map))
1082 continue;
1083 if (phys_proc_id[cpu] == phys_proc_id[i]) {
1084 cpu_set(i, cpu_core_map[cpu]);
1087 } else {
1088 cpu_core_map[cpu] = cpu_sibling_map[cpu];
1092 smpboot_setup_io_apic();
1094 setup_boot_APIC_clock();
1097 * Synchronize the TSC with the AP
1099 if (cpu_has_tsc && cpucount && cpu_khz)
1100 synchronize_tsc_bp();
1103 /* These are wrappers to interface to the new boot process. Someone
1104 who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
1105 void __init smp_prepare_cpus(unsigned int max_cpus)
1107 smp_boot_cpus(max_cpus);
1110 void __devinit smp_prepare_boot_cpu(void)
1112 cpu_set(smp_processor_id(), cpu_online_map);
1113 cpu_set(smp_processor_id(), cpu_callout_map);
1116 int __devinit __cpu_up(unsigned int cpu)
1118 /* This only works at boot for x86. See "rewrite" above. */
1119 if (cpu_isset(cpu, smp_commenced_mask)) {
1120 local_irq_enable();
1121 return -ENOSYS;
1124 /* In case one didn't come up */
1125 if (!cpu_isset(cpu, cpu_callin_map)) {
1126 local_irq_enable();
1127 return -EIO;
1130 local_irq_enable();
1131 /* Unleash the CPU! */
1132 cpu_set(cpu, smp_commenced_mask);
1133 while (!cpu_isset(cpu, cpu_online_map))
1134 mb();
1135 return 0;
1138 void __init smp_cpus_done(unsigned int max_cpus)
1140 #ifdef CONFIG_X86_IO_APIC
1141 setup_ioapic_dest();
1142 #endif
1143 zap_low_mappings();
1145 * Disable executability of the SMP trampoline:
1147 set_kernel_exec((unsigned long)trampoline_base, trampoline_exec);
1150 void __init smp_intr_init(void)
1153 * IRQ0 must be given a fixed assignment and initialized,
1154 * because it's used before the IO-APIC is set up.
1156 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1159 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1160 * IPI, driven by wakeup.
1162 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1164 /* IPI for invalidation */
1165 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1167 /* IPI for generic function call */
1168 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);