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/init.h>
38 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/kernel_stat.h>
43 #include <linux/bootmem.h>
44 #include <linux/notifier.h>
45 #include <linux/cpu.h>
46 #include <linux/percpu.h>
47 #include <linux/nmi.h>
49 #include <linux/delay.h>
50 #include <linux/mc146818rtc.h>
51 #include <asm/tlbflush.h>
53 #include <asm/arch_hooks.h>
56 #include <mach_apic.h>
57 #include <mach_wakecpu.h>
58 #include <smpboot_hooks.h>
62 /* which logical CPU number maps to which CPU (physical APIC ID) */
63 u16 x86_cpu_to_apicid_init[NR_CPUS] __initdata =
64 { [0 ... NR_CPUS-1] = BAD_APICID };
65 void *x86_cpu_to_apicid_early_ptr;
66 DEFINE_PER_CPU(u16, x86_cpu_to_apicid) = BAD_APICID;
67 EXPORT_PER_CPU_SYMBOL(x86_cpu_to_apicid);
69 u16 x86_bios_cpu_apicid_init[NR_CPUS] __initdata
70 = { [0 ... NR_CPUS-1] = BAD_APICID };
71 void *x86_bios_cpu_apicid_early_ptr;
72 DEFINE_PER_CPU(u16, x86_bios_cpu_apicid) = BAD_APICID;
73 EXPORT_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
75 u8 apicid_2_node[MAX_APICID];
77 extern void map_cpu_to_logical_apicid(void);
78 extern void unmap_cpu_to_logical_apicid(int cpu);
80 /* State of each CPU. */
81 DEFINE_PER_CPU(int, cpu_state) = { 0 };
83 /* Store all idle threads, this can be reused instead of creating
84 * a new thread. Also avoids complicated thread destroy functionality
87 #ifdef CONFIG_HOTPLUG_CPU
89 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
90 * removed after init for !CONFIG_HOTPLUG_CPU.
92 static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
93 #define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
94 #define set_idle_for_cpu(x, p) (per_cpu(idle_thread_array, x) = (p))
96 struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
97 #define get_idle_for_cpu(x) (idle_thread_array[(x)])
98 #define set_idle_for_cpu(x, p) (idle_thread_array[(x)] = (p))
101 static atomic_t init_deasserted;
103 static void __cpuinit smp_callin(void)
106 unsigned long timeout;
109 * If waken up by an INIT in an 82489DX configuration
110 * we may get here before an INIT-deassert IPI reaches
111 * our local APIC. We have to wait for the IPI or we'll
112 * lock up on an APIC access.
114 wait_for_init_deassert(&init_deasserted);
117 * (This works even if the APIC is not enabled.)
119 phys_id = GET_APIC_ID(apic_read(APIC_ID));
120 cpuid = smp_processor_id();
121 if (cpu_isset(cpuid, cpu_callin_map)) {
122 printk("huh, phys CPU#%d, CPU#%d already present??\n",
126 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
129 * STARTUP IPIs are fragile beasts as they might sometimes
130 * trigger some glue motherboard logic. Complete APIC bus
131 * silence for 1 second, this overestimates the time the
132 * boot CPU is spending to send the up to 2 STARTUP IPIs
133 * by a factor of two. This should be enough.
137 * Waiting 2s total for startup (udelay is not yet working)
139 timeout = jiffies + 2*HZ;
140 while (time_before(jiffies, timeout)) {
142 * Has the boot CPU finished it's STARTUP sequence?
144 if (cpu_isset(cpuid, cpu_callout_map))
149 if (!time_before(jiffies, timeout)) {
150 printk("BUG: CPU%d started up but did not get a callout!\n",
156 * the boot CPU has finished the init stage and is spinning
157 * on callin_map until we finish. We are free to set up this
158 * CPU, first the APIC. (this is probably redundant on most
162 Dprintk("CALLIN, before setup_local_APIC().\n");
163 smp_callin_clear_local_apic();
165 end_local_APIC_setup();
166 map_cpu_to_logical_apicid();
174 Dprintk("Stack at about %p\n",&cpuid);
177 * Save our processor parameters
179 smp_store_cpu_info(cpuid);
182 * Allow the master to continue.
184 cpu_set(cpuid, cpu_callin_map);
188 * Activate a secondary processor.
190 static void __cpuinit start_secondary(void *unused)
193 * Don't put *anything* before cpu_init(), SMP booting is too
194 * fragile that we want to limit the things done here to the
195 * most necessary things.
204 /* otherwise gcc will move up smp_processor_id before the cpu_init */
207 * Check TSC synchronization with the BP:
209 check_tsc_sync_target();
211 if (nmi_watchdog == NMI_IO_APIC) {
212 disable_8259A_irq(0);
213 enable_NMI_through_LVT0();
217 /* This must be done before setting cpu_online_map */
218 set_cpu_sibling_map(raw_smp_processor_id());
222 * We need to hold call_lock, so there is no inconsistency
223 * between the time smp_call_function() determines number of
224 * IPI recipients, and the time when the determination is made
225 * for which cpus receive the IPI. Holding this
226 * lock helps us to not include this cpu in a currently in progress
227 * smp_call_function().
229 lock_ipi_call_lock();
230 cpu_set(smp_processor_id(), cpu_online_map);
231 unlock_ipi_call_lock();
232 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
234 setup_secondary_clock();
241 * Everything has been set up for the secondary
242 * CPUs - they just need to reload everything
243 * from the task structure
244 * This function must not return.
246 void __devinit initialize_secondary(void)
249 * We don't actually need to load the full TSS,
250 * basically just the stack pointer and the ip.
257 :"m" (current->thread.sp),"m" (current->thread.ip));
260 static inline void __inquire_remote_apic(int apicid)
262 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
263 char *names[] = { "ID", "VERSION", "SPIV" };
267 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
269 for (i = 0; i < ARRAY_SIZE(regs); i++) {
270 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
275 status = safe_apic_wait_icr_idle();
278 "a previous APIC delivery may have failed\n");
280 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
281 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
286 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
287 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
290 case APIC_ICR_RR_VALID:
291 status = apic_read(APIC_RRR);
292 printk(KERN_CONT "%08x\n", status);
295 printk(KERN_CONT "failed\n");
300 #ifdef WAKE_SECONDARY_VIA_NMI
302 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
303 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
304 * won't ... remember to clear down the APIC, etc later.
307 wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
309 unsigned long send_status, accept_status = 0;
313 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
315 /* Boot on the stack */
316 /* Kick the second */
317 apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
319 Dprintk("Waiting for send to finish...\n");
320 send_status = safe_apic_wait_icr_idle();
323 * Give the other CPU some time to accept the IPI.
327 * Due to the Pentium erratum 3AP.
329 maxlvt = lapic_get_maxlvt();
331 apic_read_around(APIC_SPIV);
332 apic_write(APIC_ESR, 0);
334 accept_status = (apic_read(APIC_ESR) & 0xEF);
335 Dprintk("NMI sent.\n");
338 printk("APIC never delivered???\n");
340 printk("APIC delivery error (%lx).\n", accept_status);
342 return (send_status | accept_status);
344 #endif /* WAKE_SECONDARY_VIA_NMI */
346 #ifdef WAKE_SECONDARY_VIA_INIT
348 wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
350 unsigned long send_status, accept_status = 0;
351 int maxlvt, num_starts, j;
354 * Be paranoid about clearing APIC errors.
356 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
357 apic_read_around(APIC_SPIV);
358 apic_write(APIC_ESR, 0);
362 Dprintk("Asserting INIT.\n");
365 * Turn INIT on target chip
367 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
372 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
375 Dprintk("Waiting for send to finish...\n");
376 send_status = safe_apic_wait_icr_idle();
380 Dprintk("Deasserting INIT.\n");
383 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
386 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
388 Dprintk("Waiting for send to finish...\n");
389 send_status = safe_apic_wait_icr_idle();
392 atomic_set(&init_deasserted, 1);
395 * Should we send STARTUP IPIs ?
397 * Determine this based on the APIC version.
398 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
400 if (APIC_INTEGRATED(apic_version[phys_apicid]))
406 * Paravirt / VMI wants a startup IPI hook here to set up the
407 * target processor state.
409 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
410 (unsigned long) stack_start.sp);
413 * Run STARTUP IPI loop.
415 Dprintk("#startup loops: %d.\n", num_starts);
417 maxlvt = lapic_get_maxlvt();
419 for (j = 1; j <= num_starts; j++) {
420 Dprintk("Sending STARTUP #%d.\n",j);
421 apic_read_around(APIC_SPIV);
422 apic_write(APIC_ESR, 0);
424 Dprintk("After apic_write.\n");
431 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
433 /* Boot on the stack */
434 /* Kick the second */
435 apic_write_around(APIC_ICR, APIC_DM_STARTUP
436 | (start_eip >> 12));
439 * Give the other CPU some time to accept the IPI.
443 Dprintk("Startup point 1.\n");
445 Dprintk("Waiting for send to finish...\n");
446 send_status = safe_apic_wait_icr_idle();
449 * Give the other CPU some time to accept the IPI.
453 * Due to the Pentium erratum 3AP.
456 apic_read_around(APIC_SPIV);
457 apic_write(APIC_ESR, 0);
459 accept_status = (apic_read(APIC_ESR) & 0xEF);
460 if (send_status || accept_status)
463 Dprintk("After Startup.\n");
466 printk("APIC never delivered???\n");
468 printk("APIC delivery error (%lx).\n", accept_status);
470 return (send_status | accept_status);
472 #endif /* WAKE_SECONDARY_VIA_INIT */
474 extern cpumask_t cpu_initialized;
477 struct work_struct work;
478 struct task_struct *idle;
479 struct completion done;
483 static void __cpuinit do_fork_idle(struct work_struct *work)
485 struct create_idle *c_idle =
486 container_of(work, struct create_idle, work);
488 c_idle->idle = fork_idle(c_idle->cpu);
489 complete(&c_idle->done);
491 static int __cpuinit do_boot_cpu(int apicid, int cpu)
493 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
494 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
495 * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
498 unsigned long boot_error = 0;
500 unsigned long start_eip;
501 unsigned short nmi_high = 0, nmi_low = 0;
502 struct create_idle c_idle = {
504 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
506 INIT_WORK(&c_idle.work, do_fork_idle);
508 alternatives_smp_switch(1);
510 c_idle.idle = get_idle_for_cpu(cpu);
513 * We can't use kernel_thread since we must avoid to
514 * reschedule the child.
517 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
518 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
519 init_idle(c_idle.idle, cpu);
523 if (!keventd_up() || current_is_keventd())
524 c_idle.work.func(&c_idle.work);
526 schedule_work(&c_idle.work);
527 wait_for_completion(&c_idle.done);
530 if (IS_ERR(c_idle.idle)) {
531 printk(KERN_ERR "failed fork for CPU %d\n", cpu);
532 return PTR_ERR(c_idle.idle);
535 set_idle_for_cpu(cpu, c_idle.idle);
537 per_cpu(current_task, cpu) = c_idle.idle;
539 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
541 c_idle.idle->thread.ip = (unsigned long) start_secondary;
542 /* start_eip had better be page-aligned! */
543 start_eip = setup_trampoline();
545 /* So we see what's up */
546 printk("Booting processor %d/%d ip %lx\n", cpu, apicid, start_eip);
547 /* Stack for startup_32 can be just as for start_secondary onwards */
548 stack_start.sp = (void *) c_idle.idle->thread.sp;
553 * This grunge runs the startup process for
554 * the targeted processor.
557 atomic_set(&init_deasserted, 0);
559 Dprintk("Setting warm reset code and vector.\n");
561 store_NMI_vector(&nmi_high, &nmi_low);
563 smpboot_setup_warm_reset_vector(start_eip);
565 * Be paranoid about clearing APIC errors.
567 apic_write(APIC_ESR, 0);
572 * Starting actual IPI sequence...
574 boot_error = wakeup_secondary_cpu(apicid, start_eip);
578 * allow APs to start initializing.
580 Dprintk("Before Callout %d.\n", cpu);
581 cpu_set(cpu, cpu_callout_map);
582 Dprintk("After Callout %d.\n", cpu);
585 * Wait 5s total for a response
587 for (timeout = 0; timeout < 50000; timeout++) {
588 if (cpu_isset(cpu, cpu_callin_map))
589 break; /* It has booted */
593 if (cpu_isset(cpu, cpu_callin_map)) {
594 /* number CPUs logically, starting from 1 (BSP is 0) */
596 printk("CPU%d: ", cpu);
597 print_cpu_info(&cpu_data(cpu));
598 Dprintk("CPU has booted.\n");
601 if (*((volatile unsigned char *)trampoline_base)
603 /* trampoline started but...? */
604 printk("Stuck ??\n");
606 /* trampoline code not run */
607 printk("Not responding.\n");
608 inquire_remote_apic(apicid);
613 /* Try to put things back the way they were before ... */
614 unmap_cpu_to_logical_apicid(cpu);
615 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
616 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
617 cpu_clear(cpu, cpu_possible_map);
618 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
621 /* mark "stuck" area as not stuck */
622 *((volatile unsigned long *)trampoline_base) = 0;
627 #ifdef CONFIG_HOTPLUG_CPU
628 void cpu_exit_clear(void)
630 int cpu = raw_smp_processor_id();
637 cpu_clear(cpu, cpu_callout_map);
638 cpu_clear(cpu, cpu_callin_map);
640 unmap_cpu_to_logical_apicid(cpu);
644 static int boot_cpu_logical_apicid;
645 /* Where the IO area was mapped on multiquad, always 0 otherwise */
647 #ifdef CONFIG_X86_NUMAQ
648 EXPORT_SYMBOL(xquad_portio);
651 static void __init disable_smp(void)
653 cpu_possible_map = cpumask_of_cpu(0);
654 cpu_present_map = cpumask_of_cpu(0);
655 smpboot_clear_io_apic_irqs();
656 phys_cpu_present_map = physid_mask_of_physid(0);
657 map_cpu_to_logical_apicid();
658 cpu_set(0, per_cpu(cpu_sibling_map, 0));
659 cpu_set(0, per_cpu(cpu_core_map, 0));
662 static int __init smp_sanity_check(unsigned max_cpus)
665 * If we couldn't find an SMP configuration at boot time,
666 * get out of here now!
668 if (!smp_found_config && !acpi_lapic) {
669 printk(KERN_NOTICE "SMP motherboard not detected.\n");
671 if (APIC_init_uniprocessor())
672 printk(KERN_NOTICE "Local APIC not detected."
673 " Using dummy APIC emulation.\n");
678 * Should not be necessary because the MP table should list the boot
679 * CPU too, but we do it for the sake of robustness anyway.
680 * Makes no sense to do this check in clustered apic mode, so skip it
682 if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
683 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
684 boot_cpu_physical_apicid);
685 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
689 * If we couldn't find a local APIC, then get out of here now!
691 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
692 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
693 boot_cpu_physical_apicid);
694 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
701 * If SMP should be disabled, then really disable it!
704 smp_found_config = 0;
705 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
707 if (nmi_watchdog == NMI_LOCAL_APIC) {
708 printk(KERN_INFO "activating minimal APIC for NMI watchdog use.\n");
711 end_local_APIC_setup();
719 * Cycle through the processors sending APIC IPIs to boot each.
721 static void __init smp_boot_cpus(unsigned int max_cpus)
724 * Setup boot CPU information
726 smp_store_cpu_info(0); /* Final full version of the data */
727 printk(KERN_INFO "CPU%d: ", 0);
728 print_cpu_info(&cpu_data(0));
730 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
731 boot_cpu_logical_apicid = logical_smp_processor_id();
733 current_thread_info()->cpu = 0;
735 set_cpu_sibling_map(0);
737 if (smp_sanity_check(max_cpus) < 0) {
738 printk(KERN_INFO "SMP disabled\n");
745 end_local_APIC_setup();
746 map_cpu_to_logical_apicid();
749 setup_portio_remap();
751 smpboot_setup_io_apic();
756 /* These are wrappers to interface to the new boot process. Someone
757 who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
758 void __init native_smp_prepare_cpus(unsigned int max_cpus)
760 nmi_watchdog_default();
761 cpu_callin_map = cpumask_of_cpu(0);
763 smp_boot_cpus(max_cpus);
766 void __init native_smp_prepare_boot_cpu(void)
768 unsigned int cpu = smp_processor_id();
773 cpu_set(cpu, cpu_callout_map);
774 __get_cpu_var(cpu_state) = CPU_ONLINE;
777 int __cpuinit native_cpu_up(unsigned int cpu)
779 int apicid = cpu_present_to_apicid(cpu);
783 WARN_ON(irqs_disabled());
785 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
787 if (apicid == BAD_APICID || apicid == boot_cpu_physical_apicid ||
788 !physid_isset(apicid, phys_cpu_present_map)) {
789 printk(KERN_ERR "%s: bad cpu %d\n", __func__, cpu);
794 * Already booted CPU?
796 if (cpu_isset(cpu, cpu_callin_map)) {
797 Dprintk("do_boot_cpu %d Already started\n", cpu);
802 * Save current MTRR state in case it was changed since early boot
803 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
807 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
809 /* init low mem mapping */
810 clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
811 min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
814 err = do_boot_cpu(apicid, cpu);
816 Dprintk("do_boot_cpu failed %d\n", err);
821 * Check TSC synchronization with the AP (keep irqs disabled
824 local_irq_save(flags);
825 check_tsc_sync_source(cpu);
826 local_irq_restore(flags);
828 while (!cpu_isset(cpu, cpu_online_map)) {
830 touch_nmi_watchdog();
836 extern void impress_friends(void);
837 extern void smp_checks(void);
839 void __init native_smp_cpus_done(unsigned int max_cpus)
842 * Cleanup possible dangling ends...
844 smpboot_restore_warm_reset_vector();
846 Dprintk("Boot done.\n");
850 #ifdef CONFIG_X86_IO_APIC
853 check_nmi_watchdog();