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;
64 EXPORT_SYMBOL(smp_num_siblings);
66 int phys_proc_id[NR_CPUS]; /* Package ID of each logical CPU */
67 EXPORT_SYMBOL(phys_proc_id);
68 int cpu_core_id[NR_CPUS]; /* Core ID of each logical CPU */
69 EXPORT_SYMBOL(cpu_core_id);
71 /* bitmap of online cpus */
72 cpumask_t cpu_online_map;
73 EXPORT_SYMBOL(cpu_online_map);
75 cpumask_t cpu_callin_map;
76 cpumask_t cpu_callout_map;
77 EXPORT_SYMBOL(cpu_callout_map);
78 static cpumask_t smp_commenced_mask;
80 /* Per CPU bogomips and other parameters */
81 struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
82 EXPORT_SYMBOL(cpu_data);
84 u8 x86_cpu_to_apicid[NR_CPUS] =
85 { [0 ... NR_CPUS-1] = 0xff };
86 EXPORT_SYMBOL(x86_cpu_to_apicid);
89 * Trampoline 80x86 program as an array.
92 extern unsigned char trampoline_data [];
93 extern unsigned char trampoline_end [];
94 static unsigned char *trampoline_base;
95 static int trampoline_exec;
97 static void map_cpu_to_logical_apicid(void);
100 * Currently trivial. Write the real->protected mode
101 * bootstrap into the page concerned. The caller
102 * has made sure it's suitably aligned.
105 static unsigned long __init setup_trampoline(void)
107 memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
108 return virt_to_phys(trampoline_base);
112 * We are called very early to get the low memory for the
113 * SMP bootup trampoline page.
115 void __init smp_alloc_memory(void)
117 trampoline_base = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
119 * Has to be in very low memory so we can execute
122 if (__pa(trampoline_base) >= 0x9F000)
125 * Make the SMP trampoline executable:
127 trampoline_exec = set_kernel_exec((unsigned long)trampoline_base, 1);
131 * The bootstrap kernel entry code has set these up. Save them for
135 static void __init smp_store_cpu_info(int id)
137 struct cpuinfo_x86 *c = cpu_data + id;
143 * Mask B, Pentium, but not Pentium MMX
145 if (c->x86_vendor == X86_VENDOR_INTEL &&
147 c->x86_mask >= 1 && c->x86_mask <= 4 &&
150 * Remember we have B step Pentia with bugs
155 * Certain Athlons might work (for various values of 'work') in SMP
156 * but they are not certified as MP capable.
158 if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
160 /* Athlon 660/661 is valid. */
161 if ((c->x86_model==6) && ((c->x86_mask==0) || (c->x86_mask==1)))
164 /* Duron 670 is valid */
165 if ((c->x86_model==7) && (c->x86_mask==0))
169 * Athlon 662, Duron 671, and Athlon >model 7 have capability bit.
170 * It's worth noting that the A5 stepping (662) of some Athlon XP's
171 * have the MP bit set.
172 * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for more.
174 if (((c->x86_model==6) && (c->x86_mask>=2)) ||
175 ((c->x86_model==7) && (c->x86_mask>=1)) ||
180 /* If we get here, it's not a certified SMP capable AMD system. */
181 tainted |= TAINT_UNSAFE_SMP;
189 * TSC synchronization.
191 * We first check whether all CPUs have their TSC's synchronized,
192 * then we print a warning if not, and always resync.
195 static atomic_t tsc_start_flag = ATOMIC_INIT(0);
196 static atomic_t tsc_count_start = ATOMIC_INIT(0);
197 static atomic_t tsc_count_stop = ATOMIC_INIT(0);
198 static unsigned long long tsc_values[NR_CPUS];
202 static void __init synchronize_tsc_bp (void)
205 unsigned long long t0;
206 unsigned long long sum, avg;
208 unsigned int one_usec;
211 printk(KERN_INFO "checking TSC synchronization across %u CPUs: ", num_booting_cpus());
213 /* convert from kcyc/sec to cyc/usec */
214 one_usec = cpu_khz / 1000;
216 atomic_set(&tsc_start_flag, 1);
220 * We loop a few times to get a primed instruction cache,
221 * then the last pass is more or less synchronized and
222 * the BP and APs set their cycle counters to zero all at
223 * once. This reduces the chance of having random offsets
224 * between the processors, and guarantees that the maximum
225 * delay between the cycle counters is never bigger than
226 * the latency of information-passing (cachelines) between
229 for (i = 0; i < NR_LOOPS; i++) {
231 * all APs synchronize but they loop on '== num_cpus'
233 while (atomic_read(&tsc_count_start) != num_booting_cpus()-1)
235 atomic_set(&tsc_count_stop, 0);
238 * this lets the APs save their current TSC:
240 atomic_inc(&tsc_count_start);
242 rdtscll(tsc_values[smp_processor_id()]);
244 * We clear the TSC in the last loop:
250 * Wait for all APs to leave the synchronization point:
252 while (atomic_read(&tsc_count_stop) != num_booting_cpus()-1)
254 atomic_set(&tsc_count_start, 0);
256 atomic_inc(&tsc_count_stop);
260 for (i = 0; i < NR_CPUS; i++) {
261 if (cpu_isset(i, cpu_callout_map)) {
267 do_div(avg, num_booting_cpus());
270 for (i = 0; i < NR_CPUS; i++) {
271 if (!cpu_isset(i, cpu_callout_map))
273 delta = tsc_values[i] - avg;
277 * We report bigger than 2 microseconds clock differences.
279 if (delta > 2*one_usec) {
286 do_div(realdelta, one_usec);
287 if (tsc_values[i] < avg)
288 realdelta = -realdelta;
290 printk(KERN_INFO "CPU#%d had %ld usecs TSC skew, fixed it up.\n", i, realdelta);
299 static void __init synchronize_tsc_ap (void)
304 * Not every cpu is online at the time
305 * this gets called, so we first wait for the BP to
306 * finish SMP initialization:
308 while (!atomic_read(&tsc_start_flag)) mb();
310 for (i = 0; i < NR_LOOPS; i++) {
311 atomic_inc(&tsc_count_start);
312 while (atomic_read(&tsc_count_start) != num_booting_cpus())
315 rdtscll(tsc_values[smp_processor_id()]);
319 atomic_inc(&tsc_count_stop);
320 while (atomic_read(&tsc_count_stop) != num_booting_cpus()) mb();
325 extern void calibrate_delay(void);
327 static atomic_t init_deasserted;
329 static void __init smp_callin(void)
332 unsigned long timeout;
335 * If waken up by an INIT in an 82489DX configuration
336 * we may get here before an INIT-deassert IPI reaches
337 * our local APIC. We have to wait for the IPI or we'll
338 * lock up on an APIC access.
340 wait_for_init_deassert(&init_deasserted);
343 * (This works even if the APIC is not enabled.)
345 phys_id = GET_APIC_ID(apic_read(APIC_ID));
346 cpuid = smp_processor_id();
347 if (cpu_isset(cpuid, cpu_callin_map)) {
348 printk("huh, phys CPU#%d, CPU#%d already present??\n",
352 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
355 * STARTUP IPIs are fragile beasts as they might sometimes
356 * trigger some glue motherboard logic. Complete APIC bus
357 * silence for 1 second, this overestimates the time the
358 * boot CPU is spending to send the up to 2 STARTUP IPIs
359 * by a factor of two. This should be enough.
363 * Waiting 2s total for startup (udelay is not yet working)
365 timeout = jiffies + 2*HZ;
366 while (time_before(jiffies, timeout)) {
368 * Has the boot CPU finished it's STARTUP sequence?
370 if (cpu_isset(cpuid, cpu_callout_map))
375 if (!time_before(jiffies, timeout)) {
376 printk("BUG: CPU%d started up but did not get a callout!\n",
382 * the boot CPU has finished the init stage and is spinning
383 * on callin_map until we finish. We are free to set up this
384 * CPU, first the APIC. (this is probably redundant on most
388 Dprintk("CALLIN, before setup_local_APIC().\n");
389 smp_callin_clear_local_apic();
391 map_cpu_to_logical_apicid();
397 Dprintk("Stack at about %p\n",&cpuid);
400 * Save our processor parameters
402 smp_store_cpu_info(cpuid);
404 disable_APIC_timer();
407 * Allow the master to continue.
409 cpu_set(cpuid, cpu_callin_map);
412 * Synchronize the TSC with the BP
414 if (cpu_has_tsc && cpu_khz)
415 synchronize_tsc_ap();
421 * Activate a secondary processor.
423 static void __init start_secondary(void *unused)
426 * Dont put anything before smp_callin(), SMP
427 * booting is too fragile that we want to limit the
428 * things done here to the most necessary things.
432 while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
434 setup_secondary_APIC_clock();
435 if (nmi_watchdog == NMI_IO_APIC) {
436 disable_8259A_irq(0);
437 enable_NMI_through_LVT0(NULL);
442 * low-memory mappings have been cleared, flush them from
443 * the local TLBs too.
446 cpu_set(smp_processor_id(), cpu_online_map);
448 /* We can take interrupts now: we're officially "up". */
456 * Everything has been set up for the secondary
457 * CPUs - they just need to reload everything
458 * from the task structure
459 * This function must not return.
461 void __init initialize_secondary(void)
464 * We don't actually need to load the full TSS,
465 * basically just the stack pointer and the eip.
472 :"r" (current->thread.esp),"r" (current->thread.eip));
482 /* which logical CPUs are on which nodes */
483 cpumask_t node_2_cpu_mask[MAX_NUMNODES] =
484 { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
485 /* which node each logical CPU is on */
486 int cpu_2_node[NR_CPUS] = { [0 ... NR_CPUS-1] = 0 };
487 EXPORT_SYMBOL(cpu_2_node);
489 /* set up a mapping between cpu and node. */
490 static inline void map_cpu_to_node(int cpu, int node)
492 printk("Mapping cpu %d to node %d\n", cpu, node);
493 cpu_set(cpu, node_2_cpu_mask[node]);
494 cpu_2_node[cpu] = node;
497 /* undo a mapping between cpu and node. */
498 static inline void unmap_cpu_to_node(int cpu)
502 printk("Unmapping cpu %d from all nodes\n", cpu);
503 for (node = 0; node < MAX_NUMNODES; node ++)
504 cpu_clear(cpu, node_2_cpu_mask[node]);
507 #else /* !CONFIG_NUMA */
509 #define map_cpu_to_node(cpu, node) ({})
510 #define unmap_cpu_to_node(cpu) ({})
512 #endif /* CONFIG_NUMA */
514 u8 cpu_2_logical_apicid[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
516 static void map_cpu_to_logical_apicid(void)
518 int cpu = smp_processor_id();
519 int apicid = logical_smp_processor_id();
521 cpu_2_logical_apicid[cpu] = apicid;
522 map_cpu_to_node(cpu, apicid_to_node(apicid));
525 static void unmap_cpu_to_logical_apicid(int cpu)
527 cpu_2_logical_apicid[cpu] = BAD_APICID;
528 unmap_cpu_to_node(cpu);
532 static inline void __inquire_remote_apic(int apicid)
534 int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
535 char *names[] = { "ID", "VERSION", "SPIV" };
538 printk("Inquiring remote APIC #%d...\n", apicid);
540 for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
541 printk("... APIC #%d %s: ", apicid, names[i]);
546 apic_wait_icr_idle();
548 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
549 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
554 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
555 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
558 case APIC_ICR_RR_VALID:
559 status = apic_read(APIC_RRR);
560 printk("%08x\n", status);
569 #ifdef WAKE_SECONDARY_VIA_NMI
571 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
572 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
573 * won't ... remember to clear down the APIC, etc later.
576 wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
578 unsigned long send_status = 0, accept_status = 0;
582 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
584 /* Boot on the stack */
585 /* Kick the second */
586 apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
588 Dprintk("Waiting for send to finish...\n");
593 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
594 } while (send_status && (timeout++ < 1000));
597 * Give the other CPU some time to accept the IPI.
601 * Due to the Pentium erratum 3AP.
603 maxlvt = get_maxlvt();
605 apic_read_around(APIC_SPIV);
606 apic_write(APIC_ESR, 0);
608 accept_status = (apic_read(APIC_ESR) & 0xEF);
609 Dprintk("NMI sent.\n");
612 printk("APIC never delivered???\n");
614 printk("APIC delivery error (%lx).\n", accept_status);
616 return (send_status | accept_status);
618 #endif /* WAKE_SECONDARY_VIA_NMI */
620 #ifdef WAKE_SECONDARY_VIA_INIT
622 wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
624 unsigned long send_status = 0, accept_status = 0;
625 int maxlvt, timeout, num_starts, j;
628 * Be paranoid about clearing APIC errors.
630 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
631 apic_read_around(APIC_SPIV);
632 apic_write(APIC_ESR, 0);
636 Dprintk("Asserting INIT.\n");
639 * Turn INIT on target chip
641 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
646 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
649 Dprintk("Waiting for send to finish...\n");
654 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
655 } while (send_status && (timeout++ < 1000));
659 Dprintk("Deasserting INIT.\n");
662 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
665 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
667 Dprintk("Waiting for send to finish...\n");
672 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
673 } while (send_status && (timeout++ < 1000));
675 atomic_set(&init_deasserted, 1);
678 * Should we send STARTUP IPIs ?
680 * Determine this based on the APIC version.
681 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
683 if (APIC_INTEGRATED(apic_version[phys_apicid]))
689 * Run STARTUP IPI loop.
691 Dprintk("#startup loops: %d.\n", num_starts);
693 maxlvt = get_maxlvt();
695 for (j = 1; j <= num_starts; j++) {
696 Dprintk("Sending STARTUP #%d.\n",j);
697 apic_read_around(APIC_SPIV);
698 apic_write(APIC_ESR, 0);
700 Dprintk("After apic_write.\n");
707 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
709 /* Boot on the stack */
710 /* Kick the second */
711 apic_write_around(APIC_ICR, APIC_DM_STARTUP
712 | (start_eip >> 12));
715 * Give the other CPU some time to accept the IPI.
719 Dprintk("Startup point 1.\n");
721 Dprintk("Waiting for send to finish...\n");
726 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
727 } while (send_status && (timeout++ < 1000));
730 * Give the other CPU some time to accept the IPI.
734 * Due to the Pentium erratum 3AP.
737 apic_read_around(APIC_SPIV);
738 apic_write(APIC_ESR, 0);
740 accept_status = (apic_read(APIC_ESR) & 0xEF);
741 if (send_status || accept_status)
744 Dprintk("After Startup.\n");
747 printk("APIC never delivered???\n");
749 printk("APIC delivery error (%lx).\n", accept_status);
751 return (send_status | accept_status);
753 #endif /* WAKE_SECONDARY_VIA_INIT */
755 extern cpumask_t cpu_initialized;
757 static int __init do_boot_cpu(int apicid)
759 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
760 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
761 * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
764 struct task_struct *idle;
765 unsigned long boot_error;
767 unsigned long start_eip;
768 unsigned short nmi_high = 0, nmi_low = 0;
772 * We can't use kernel_thread since we must avoid to
773 * reschedule the child.
775 idle = fork_idle(cpu);
777 panic("failed fork for CPU %d", cpu);
778 idle->thread.eip = (unsigned long) start_secondary;
779 /* start_eip had better be page-aligned! */
780 start_eip = setup_trampoline();
782 /* So we see what's up */
783 printk("Booting processor %d/%d eip %lx\n", cpu, apicid, start_eip);
784 /* Stack for startup_32 can be just as for start_secondary onwards */
785 stack_start.esp = (void *) idle->thread.esp;
790 * This grunge runs the startup process for
791 * the targeted processor.
794 atomic_set(&init_deasserted, 0);
796 Dprintk("Setting warm reset code and vector.\n");
798 store_NMI_vector(&nmi_high, &nmi_low);
800 smpboot_setup_warm_reset_vector(start_eip);
803 * Starting actual IPI sequence...
805 boot_error = wakeup_secondary_cpu(apicid, start_eip);
809 * allow APs to start initializing.
811 Dprintk("Before Callout %d.\n", cpu);
812 cpu_set(cpu, cpu_callout_map);
813 Dprintk("After Callout %d.\n", cpu);
816 * Wait 5s total for a response
818 for (timeout = 0; timeout < 50000; timeout++) {
819 if (cpu_isset(cpu, cpu_callin_map))
820 break; /* It has booted */
824 if (cpu_isset(cpu, cpu_callin_map)) {
825 /* number CPUs logically, starting from 1 (BSP is 0) */
827 printk("CPU%d: ", cpu);
828 print_cpu_info(&cpu_data[cpu]);
829 Dprintk("CPU has booted.\n");
832 if (*((volatile unsigned char *)trampoline_base)
834 /* trampoline started but...? */
835 printk("Stuck ??\n");
837 /* trampoline code not run */
838 printk("Not responding.\n");
839 inquire_remote_apic(apicid);
842 x86_cpu_to_apicid[cpu] = apicid;
844 /* Try to put things back the way they were before ... */
845 unmap_cpu_to_logical_apicid(cpu);
846 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
847 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
851 /* mark "stuck" area as not stuck */
852 *((volatile unsigned long *)trampoline_base) = 0;
857 static void smp_tune_scheduling (void)
859 unsigned long cachesize; /* kB */
860 unsigned long bandwidth = 350; /* MB/s */
862 * Rough estimation for SMP scheduling, this is the number of
863 * cycles it takes for a fully memory-limited process to flush
864 * the SMP-local cache.
866 * (For a P5 this pretty much means we will choose another idle
867 * CPU almost always at wakeup time (this is due to the small
868 * L1 cache), on PIIs it's around 50-100 usecs, depending on
874 * this basically disables processor-affinity
875 * scheduling on SMP without a TSC.
879 cachesize = boot_cpu_data.x86_cache_size;
880 if (cachesize == -1) {
881 cachesize = 16; /* Pentiums, 2x8kB cache */
888 * Cycle through the processors sending APIC IPIs to boot each.
891 static int boot_cpu_logical_apicid;
892 /* Where the IO area was mapped on multiquad, always 0 otherwise */
894 #ifdef CONFIG_X86_NUMAQ
895 EXPORT_SYMBOL(xquad_portio);
898 cpumask_t cpu_sibling_map[NR_CPUS] __cacheline_aligned;
900 EXPORT_SYMBOL(cpu_sibling_map);
902 cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
903 EXPORT_SYMBOL(cpu_core_map);
905 static void __init smp_boot_cpus(unsigned int max_cpus)
907 int apicid, cpu, bit, kicked;
908 unsigned long bogosum = 0;
911 * Setup boot CPU information
913 smp_store_cpu_info(0); /* Final full version of the data */
914 printk("CPU%d: ", 0);
915 print_cpu_info(&cpu_data[0]);
917 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
918 boot_cpu_logical_apicid = logical_smp_processor_id();
919 x86_cpu_to_apicid[0] = boot_cpu_physical_apicid;
921 current_thread_info()->cpu = 0;
922 smp_tune_scheduling();
923 cpus_clear(cpu_sibling_map[0]);
924 cpu_set(0, cpu_sibling_map[0]);
926 cpus_clear(cpu_core_map[0]);
927 cpu_set(0, cpu_core_map[0]);
930 * If we couldn't find an SMP configuration at boot time,
931 * get out of here now!
933 if (!smp_found_config && !acpi_lapic) {
934 printk(KERN_NOTICE "SMP motherboard not detected.\n");
935 smpboot_clear_io_apic_irqs();
936 phys_cpu_present_map = physid_mask_of_physid(0);
937 if (APIC_init_uniprocessor())
938 printk(KERN_NOTICE "Local APIC not detected."
939 " Using dummy APIC emulation.\n");
940 map_cpu_to_logical_apicid();
941 cpu_set(0, cpu_sibling_map[0]);
942 cpu_set(0, cpu_core_map[0]);
947 * Should not be necessary because the MP table should list the boot
948 * CPU too, but we do it for the sake of robustness anyway.
949 * Makes no sense to do this check in clustered apic mode, so skip it
951 if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
952 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
953 boot_cpu_physical_apicid);
954 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
958 * If we couldn't find a local APIC, then get out of here now!
960 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
961 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
962 boot_cpu_physical_apicid);
963 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
964 smpboot_clear_io_apic_irqs();
965 phys_cpu_present_map = physid_mask_of_physid(0);
966 cpu_set(0, cpu_sibling_map[0]);
967 cpu_set(0, cpu_core_map[0]);
974 * If SMP should be disabled, then really disable it!
977 smp_found_config = 0;
978 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
979 smpboot_clear_io_apic_irqs();
980 phys_cpu_present_map = physid_mask_of_physid(0);
981 cpu_set(0, cpu_sibling_map[0]);
982 cpu_set(0, cpu_core_map[0]);
988 map_cpu_to_logical_apicid();
991 setup_portio_remap();
994 * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
996 * In clustered apic mode, phys_cpu_present_map is a constructed thus:
997 * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
1000 Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
1003 for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
1004 apicid = cpu_present_to_apicid(bit);
1006 * Don't even attempt to start the boot CPU!
1008 if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
1011 if (!check_apicid_present(bit))
1013 if (max_cpus <= cpucount+1)
1016 if (do_boot_cpu(apicid))
1017 printk("CPU #%d not responding - cannot use it.\n",
1024 * Cleanup possible dangling ends...
1026 smpboot_restore_warm_reset_vector();
1029 * Allow the user to impress friends.
1031 Dprintk("Before bogomips.\n");
1032 for (cpu = 0; cpu < NR_CPUS; cpu++)
1033 if (cpu_isset(cpu, cpu_callout_map))
1034 bogosum += cpu_data[cpu].loops_per_jiffy;
1036 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
1038 bogosum/(500000/HZ),
1039 (bogosum/(5000/HZ))%100);
1041 Dprintk("Before bogocount - setting activated=1.\n");
1044 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
1047 * Don't taint if we are running SMP kernel on a single non-MP
1050 if (tainted & TAINT_UNSAFE_SMP) {
1052 printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
1054 tainted &= ~TAINT_UNSAFE_SMP;
1057 Dprintk("Boot done.\n");
1060 * construct cpu_sibling_map[], so that we can tell sibling CPUs
1063 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1064 cpus_clear(cpu_sibling_map[cpu]);
1065 cpus_clear(cpu_core_map[cpu]);
1068 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1069 struct cpuinfo_x86 *c = cpu_data + cpu;
1072 if (!cpu_isset(cpu, cpu_callout_map))
1075 if (smp_num_siblings > 1) {
1076 for (i = 0; i < NR_CPUS; i++) {
1077 if (!cpu_isset(i, cpu_callout_map))
1079 if (cpu_core_id[cpu] == cpu_core_id[i]) {
1081 cpu_set(i, cpu_sibling_map[cpu]);
1086 cpu_set(cpu, cpu_sibling_map[cpu]);
1089 if (siblings != smp_num_siblings) {
1090 printk(KERN_WARNING "WARNING: %d siblings found for CPU%d, should be %d\n", siblings, cpu, smp_num_siblings);
1091 smp_num_siblings = siblings;
1094 if (c->x86_num_cores > 1) {
1095 for (i = 0; i < NR_CPUS; i++) {
1096 if (!cpu_isset(i, cpu_callout_map))
1098 if (phys_proc_id[cpu] == phys_proc_id[i]) {
1099 cpu_set(i, cpu_core_map[cpu]);
1103 cpu_core_map[cpu] = cpu_sibling_map[cpu];
1107 smpboot_setup_io_apic();
1109 setup_boot_APIC_clock();
1112 * Synchronize the TSC with the AP
1114 if (cpu_has_tsc && cpucount && cpu_khz)
1115 synchronize_tsc_bp();
1118 /* These are wrappers to interface to the new boot process. Someone
1119 who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
1120 void __init smp_prepare_cpus(unsigned int max_cpus)
1122 smp_boot_cpus(max_cpus);
1125 void __devinit smp_prepare_boot_cpu(void)
1127 cpu_set(smp_processor_id(), cpu_online_map);
1128 cpu_set(smp_processor_id(), cpu_callout_map);
1131 int __devinit __cpu_up(unsigned int cpu)
1133 /* This only works at boot for x86. See "rewrite" above. */
1134 if (cpu_isset(cpu, smp_commenced_mask)) {
1139 /* In case one didn't come up */
1140 if (!cpu_isset(cpu, cpu_callin_map)) {
1146 /* Unleash the CPU! */
1147 cpu_set(cpu, smp_commenced_mask);
1148 while (!cpu_isset(cpu, cpu_online_map))
1153 void __init smp_cpus_done(unsigned int max_cpus)
1155 #ifdef CONFIG_X86_IO_APIC
1156 setup_ioapic_dest();
1160 * Disable executability of the SMP trampoline:
1162 set_kernel_exec((unsigned long)trampoline_base, trampoline_exec);
1165 void __init smp_intr_init(void)
1168 * IRQ0 must be given a fixed assignment and initialized,
1169 * because it's used before the IO-APIC is set up.
1171 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1174 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1175 * IPI, driven by wakeup.
1177 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1179 /* IPI for invalidation */
1180 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1182 /* IPI for generic function call */
1183 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);