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
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
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>
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>
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
116 if (__pa(trampoline_base) >= 0x9F000)
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
129 static void __init smp_store_cpu_info(int id)
131 struct cpuinfo_x86 *c = cpu_data + id;
137 * Mask B, Pentium, but not Pentium MMX
139 if (c->x86_vendor == X86_VENDOR_INTEL &&
141 c->x86_mask >= 1 && c->x86_mask <= 4 &&
144 * Remember we have B step Pentia with bugs
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)))
158 /* Duron 670 is valid */
159 if ((c->x86_model==7) && (c->x86_mask==0))
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)) ||
174 /* If we get here, it's not a certified SMP capable AMD system. */
175 tainted |= TAINT_UNSAFE_SMP;
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];
196 static void __init synchronize_tsc_bp (void)
199 unsigned long long t0;
200 unsigned long long sum, avg;
202 unsigned long one_usec;
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);
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
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)
229 atomic_set(&tsc_count_stop, 0);
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:
244 * Wait for all APs to leave the synchronization point:
246 while (atomic_read(&tsc_count_stop) != num_booting_cpus()-1)
248 atomic_set(&tsc_count_start, 0);
250 atomic_inc(&tsc_count_stop);
254 for (i = 0; i < NR_CPUS; i++) {
255 if (cpu_isset(i, cpu_callout_map)) {
261 do_div(avg, num_booting_cpus());
264 for (i = 0; i < NR_CPUS; i++) {
265 if (!cpu_isset(i, cpu_callout_map))
267 delta = tsc_values[i] - avg;
271 * We report bigger than 2 microseconds clock differences.
273 if (delta > 2*one_usec) {
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);
293 static void __init synchronize_tsc_ap (void)
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())
309 rdtscll(tsc_values[smp_processor_id()]);
313 atomic_inc(&tsc_count_stop);
314 while (atomic_read(&tsc_count_stop) != num_booting_cpus()) mb();
319 extern void calibrate_delay(void);
321 static atomic_t init_deasserted;
323 static void __init smp_callin(void)
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",
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))
369 if (!time_before(jiffies, timeout)) {
370 printk("BUG: CPU%d started up but did not get a callout!\n",
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
382 Dprintk("CALLIN, before setup_local_APIC().\n");
383 smp_callin_clear_local_apic();
385 map_cpu_to_logical_apicid();
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();
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.
426 while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
428 setup_secondary_APIC_clock();
429 if (nmi_watchdog == NMI_IO_APIC) {
430 disable_8259A_irq(0);
431 enable_NMI_through_LVT0(NULL);
436 * low-memory mappings have been cleared, flush them from
437 * the local TLBs too.
440 cpu_set(smp_processor_id(), cpu_online_map);
442 /* We can take interrupts now: we're officially "up". */
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.
466 :"r" (current->thread.esp),"r" (current->thread.eip));
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)
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]);
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);
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" };
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]);
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]);
548 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
549 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
552 case APIC_ICR_RR_VALID:
553 status = apic_read(APIC_RRR);
554 printk("%08x\n", status);
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.
570 wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
572 unsigned long send_status = 0, accept_status = 0;
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");
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.
595 * Due to the Pentium erratum 3AP.
597 maxlvt = get_maxlvt();
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");
606 printk("APIC never delivered???\n");
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
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);
630 Dprintk("Asserting INIT.\n");
633 * Turn INIT on target chip
635 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
640 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
643 Dprintk("Waiting for send to finish...\n");
648 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
649 } while (send_status && (timeout++ < 1000));
653 Dprintk("Deasserting INIT.\n");
656 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
659 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
661 Dprintk("Waiting for send to finish...\n");
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]))
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);
694 Dprintk("After apic_write.\n");
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.
713 Dprintk("Startup point 1.\n");
715 Dprintk("Waiting for send to finish...\n");
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.
728 * Due to the Pentium erratum 3AP.
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)
738 Dprintk("After Startup.\n");
741 printk("APIC never delivered???\n");
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;
761 unsigned long start_eip;
762 unsigned short nmi_high = 0, nmi_low = 0;
766 * We can't use kernel_thread since we must avoid to
767 * reschedule the child.
769 idle = fork_idle(cpu);
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;
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);
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 */
818 if (cpu_isset(cpu, cpu_callin_map)) {
819 /* number CPUs logically, starting from 1 (BSP is 0) */
821 printk("CPU%d: ", cpu);
822 print_cpu_info(&cpu_data[cpu]);
823 Dprintk("CPU has booted.\n");
826 if (*((volatile unsigned char *)trampoline_base)
828 /* trampoline started but...? */
829 printk("Stuck ??\n");
831 /* trampoline code not run */
832 printk("Not responding.\n");
833 inquire_remote_apic(apicid);
836 x86_cpu_to_apicid[cpu] = apicid;
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() */
845 /* mark "stuck" area as not stuck */
846 *((volatile unsigned long *)trampoline_base) = 0;
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
868 * this basically disables processor-affinity
869 * scheduling on SMP without a TSC.
873 cachesize = boot_cpu_data.x86_cache_size;
874 if (cachesize == -1) {
875 cachesize = 16; /* Pentiums, 2x8kB cache */
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 */
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);
893 static void __init smp_boot_cpus(unsigned int max_cpus)
895 int apicid, cpu, bit, kicked;
896 unsigned long bogosum = 0;
899 * Setup boot CPU information
901 smp_store_cpu_info(0); /* Final full version of the data */
902 printk("CPU%d: ", 0);
903 print_cpu_info(&cpu_data[0]);
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;
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]);
914 cpus_clear(cpu_core_map[0]);
915 cpu_set(0, cpu_core_map[0]);
918 * If we couldn't find an SMP configuration at boot time,
919 * get out of here now!
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]);
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
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);
946 * If we couldn't find a local APIC, then get out of here now!
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]);
962 * If SMP should be disabled, then really disable it!
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]);
976 map_cpu_to_logical_apicid();
979 setup_portio_remap();
982 * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
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
988 Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
991 for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
992 apicid = cpu_present_to_apicid(bit);
994 * Don't even attempt to start the boot CPU!
996 if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
999 if (!check_apicid_present(bit))
1001 if (max_cpus <= cpucount+1)
1004 if (do_boot_cpu(apicid))
1005 printk("CPU #%d not responding - cannot use it.\n",
1012 * Cleanup possible dangling ends...
1014 smpboot_restore_warm_reset_vector();
1017 * Allow the user to impress friends.
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;
1024 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
1026 bogosum/(500000/HZ),
1027 (bogosum/(5000/HZ))%100);
1029 Dprintk("Before bogocount - setting activated=1.\n");
1032 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
1035 * Don't taint if we are running SMP kernel on a single non-MP
1038 if (tainted & TAINT_UNSAFE_SMP) {
1040 printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
1042 tainted &= ~TAINT_UNSAFE_SMP;
1045 Dprintk("Boot done.\n");
1048 * construct cpu_sibling_map[], so that we can tell sibling CPUs
1051 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1052 cpus_clear(cpu_sibling_map[cpu]);
1053 cpus_clear(cpu_core_map[cpu]);
1056 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1057 struct cpuinfo_x86 *c = cpu_data + cpu;
1060 if (!cpu_isset(cpu, cpu_callout_map))
1063 if (smp_num_siblings > 1) {
1064 for (i = 0; i < NR_CPUS; i++) {
1065 if (!cpu_isset(i, cpu_callout_map))
1067 if (cpu_core_id[cpu] == cpu_core_id[i]) {
1069 cpu_set(i, cpu_sibling_map[cpu]);
1074 cpu_set(cpu, cpu_sibling_map[cpu]);
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;
1082 if (c->x86_num_cores > 1) {
1083 for (i = 0; i < NR_CPUS; i++) {
1084 if (!cpu_isset(i, cpu_callout_map))
1086 if (phys_proc_id[cpu] == phys_proc_id[i]) {
1087 cpu_set(i, cpu_core_map[cpu]);
1091 cpu_core_map[cpu] = cpu_sibling_map[cpu];
1095 smpboot_setup_io_apic();
1097 setup_boot_APIC_clock();
1100 * Synchronize the TSC with the AP
1102 if (cpu_has_tsc && cpucount && cpu_khz)
1103 synchronize_tsc_bp();
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)
1110 smp_boot_cpus(max_cpus);
1113 void __devinit smp_prepare_boot_cpu(void)
1115 cpu_set(smp_processor_id(), cpu_online_map);
1116 cpu_set(smp_processor_id(), cpu_callout_map);
1119 int __devinit __cpu_up(unsigned int cpu)
1121 /* This only works at boot for x86. See "rewrite" above. */
1122 if (cpu_isset(cpu, smp_commenced_mask)) {
1127 /* In case one didn't come up */
1128 if (!cpu_isset(cpu, cpu_callin_map)) {
1134 /* Unleash the CPU! */
1135 cpu_set(cpu, smp_commenced_mask);
1136 while (!cpu_isset(cpu, cpu_online_map))
1141 void __init smp_cpus_done(unsigned int max_cpus)
1143 #ifdef CONFIG_X86_IO_APIC
1144 setup_ioapic_dest();
1148 * Disable executability of the SMP trampoline:
1150 set_kernel_exec((unsigned long)trampoline_base, trampoline_exec);
1153 void __init smp_intr_init(void)
1156 * IRQ0 must be given a fixed assignment and initialized,
1157 * because it's used before the IO-APIC is set up.
1159 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1162 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1163 * IPI, driven by wakeup.
1165 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1167 /* IPI for invalidation */
1168 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1170 /* IPI for generic function call */
1171 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);