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>
6 * Copyright 2001 Andi Kleen, SuSE Labs.
8 * Much of the core SMP work is based on previous work by Thomas Radke, to
9 * whom a great many thanks are extended.
11 * Thanks to Intel for making available several different Pentium,
12 * Pentium Pro and Pentium-II/Xeon MP machines.
13 * Original development of Linux SMP code supported by Caldera.
15 * This code is released under the GNU General Public License version 2
18 * Felix Koop : NR_CPUS used properly
19 * Jose Renau : Handle single CPU case.
20 * Alan Cox : By repeated request 8) - Total BogoMIP 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 * Andi Kleen : Changed for SMP boot into long mode.
33 * Rusty Russell : Hacked into shape for new "hotplug" boot process.
34 * Andi Kleen : Converted to new state machine.
36 * Probably mostly hotplug CPU ready now.
37 * Ashok Raj : CPU hotplug support
41 #include <linux/init.h>
44 #include <linux/kernel_stat.h>
45 #include <linux/bootmem.h>
46 #include <linux/thread_info.h>
47 #include <linux/module.h>
48 #include <linux/delay.h>
49 #include <linux/mc146818rtc.h>
50 #include <linux/smp.h>
51 #include <linux/kdebug.h>
54 #include <asm/pgalloc.h>
56 #include <asm/tlbflush.h>
57 #include <asm/proto.h>
60 #include <asm/hw_irq.h>
63 /* Set when the idlers are all forked */
64 int smp_threads_ready
;
66 /* State of each CPU */
67 DEFINE_PER_CPU(int, cpu_state
) = { 0 };
70 * Store all idle threads, this can be reused instead of creating
71 * a new thread. Also avoids complicated thread destroy functionality
74 #ifdef CONFIG_HOTPLUG_CPU
76 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
77 * removed after init for !CONFIG_HOTPLUG_CPU.
79 static DEFINE_PER_CPU(struct task_struct
*, idle_thread_array
);
80 #define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
81 #define set_idle_for_cpu(x,p) (per_cpu(idle_thread_array, x) = (p))
83 struct task_struct
*idle_thread_array
[NR_CPUS
] __cpuinitdata
;
84 #define get_idle_for_cpu(x) (idle_thread_array[(x)])
85 #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
89 * The bootstrap kernel entry code has set these up. Save them for
93 static void __cpuinit
smp_store_cpu_info(int id
)
95 struct cpuinfo_x86
*c
= &cpu_data(id
);
103 static inline void wait_for_init_deassert(atomic_t
*deassert
)
105 while (!atomic_read(deassert
))
110 static atomic_t init_deasserted __cpuinitdata
;
113 * Report back to the Boot Processor.
116 void __cpuinit
smp_callin(void)
119 unsigned long timeout
;
122 * If waken up by an INIT in an 82489DX configuration
123 * we may get here before an INIT-deassert IPI reaches
124 * our local APIC. We have to wait for the IPI or we'll
125 * lock up on an APIC access.
127 wait_for_init_deassert(&init_deasserted
);
130 * (This works even if the APIC is not enabled.)
132 phys_id
= GET_APIC_ID(apic_read(APIC_ID
));
133 cpuid
= smp_processor_id();
134 if (cpu_isset(cpuid
, cpu_callin_map
)) {
135 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
138 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid
, phys_id
);
141 * STARTUP IPIs are fragile beasts as they might sometimes
142 * trigger some glue motherboard logic. Complete APIC bus
143 * silence for 1 second, this overestimates the time the
144 * boot CPU is spending to send the up to 2 STARTUP IPIs
145 * by a factor of two. This should be enough.
149 * Waiting 2s total for startup (udelay is not yet working)
151 timeout
= jiffies
+ 2*HZ
;
152 while (time_before(jiffies
, timeout
)) {
154 * Has the boot CPU finished it's STARTUP sequence?
156 if (cpu_isset(cpuid
, cpu_callout_map
))
161 if (!time_before(jiffies
, timeout
)) {
162 panic("smp_callin: CPU%d started up but did not get a callout!\n",
167 * the boot CPU has finished the init stage and is spinning
168 * on callin_map until we finish. We are free to set up this
169 * CPU, first the APIC. (this is probably redundant on most
173 Dprintk("CALLIN, before setup_local_APIC().\n");
175 end_local_APIC_setup();
180 * Need to enable IRQs because it can take longer and then
181 * the NMI watchdog might kill us.
186 Dprintk("Stack at about %p\n",&cpuid
);
189 * Save our processor parameters
191 smp_store_cpu_info(cpuid
);
194 * Allow the master to continue.
196 cpu_set(cpuid
, cpu_callin_map
);
200 * Setup code on secondary processor (after comming out of the trampoline)
202 void __cpuinit
start_secondary(void)
205 * Dont put anything before smp_callin(), SMP
206 * booting is too fragile that we want to limit the
207 * things done here to the most necessary things.
213 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
217 * Check TSC sync first:
219 check_tsc_sync_target();
221 if (nmi_watchdog
== NMI_IO_APIC
) {
222 disable_8259A_irq(0);
223 enable_NMI_through_LVT0();
228 * The sibling maps must be set before turing the online map on for
231 set_cpu_sibling_map(smp_processor_id());
234 * We need to hold call_lock, so there is no inconsistency
235 * between the time smp_call_function() determines number of
236 * IPI recipients, and the time when the determination is made
237 * for which cpus receive the IPI in genapic_flat.c. Holding this
238 * lock helps us to not include this cpu in a currently in progress
239 * smp_call_function().
241 lock_ipi_call_lock();
242 spin_lock(&vector_lock
);
244 /* Setup the per cpu irq handling data structures */
245 __setup_vector_irq(smp_processor_id());
247 * Allow the master to continue.
249 spin_unlock(&vector_lock
);
250 cpu_set(smp_processor_id(), cpu_online_map
);
251 per_cpu(cpu_state
, smp_processor_id()) = CPU_ONLINE
;
253 unlock_ipi_call_lock();
255 setup_secondary_clock();
260 extern volatile unsigned long init_rsp
;
261 extern void (*initial_code
)(void);
264 static void inquire_remote_apic(int apicid
)
266 unsigned i
, regs
[] = { APIC_ID
>> 4, APIC_LVR
>> 4, APIC_SPIV
>> 4 };
267 char *names
[] = { "ID", "VERSION", "SPIV" };
271 printk(KERN_INFO
"Inquiring remote APIC #%d...\n", apicid
);
273 for (i
= 0; i
< ARRAY_SIZE(regs
); i
++) {
274 printk(KERN_INFO
"... APIC #%d %s: ", apicid
, names
[i
]);
279 status
= safe_apic_wait_icr_idle();
282 "a previous APIC delivery may have failed\n");
284 apic_write_around(APIC_ICR2
, SET_APIC_DEST_FIELD(apicid
));
285 apic_write_around(APIC_ICR
, APIC_DM_REMRD
| regs
[i
]);
290 status
= apic_read(APIC_ICR
) & APIC_ICR_RR_MASK
;
291 } while (status
== APIC_ICR_RR_INPROG
&& timeout
++ < 1000);
294 case APIC_ICR_RR_VALID
:
295 status
= apic_read(APIC_RRR
);
296 printk(KERN_CONT
"%08x\n", status
);
299 printk(KERN_CONT
"failed\n");
306 * Kick the secondary to wake up.
308 static int __cpuinit
wakeup_secondary_via_INIT(int phys_apicid
, unsigned int start_rip
)
310 unsigned long send_status
, accept_status
= 0;
311 int maxlvt
, num_starts
, j
;
313 Dprintk("Asserting INIT.\n");
316 * Turn INIT on target chip
318 apic_write_around(APIC_ICR2
, SET_APIC_DEST_FIELD(phys_apicid
));
323 apic_write_around(APIC_ICR
, APIC_INT_LEVELTRIG
| APIC_INT_ASSERT
326 Dprintk("Waiting for send to finish...\n");
327 send_status
= safe_apic_wait_icr_idle();
331 Dprintk("Deasserting INIT.\n");
334 apic_write_around(APIC_ICR2
, SET_APIC_DEST_FIELD(phys_apicid
));
337 apic_write_around(APIC_ICR
, APIC_INT_LEVELTRIG
| APIC_DM_INIT
);
339 Dprintk("Waiting for send to finish...\n");
340 send_status
= safe_apic_wait_icr_idle();
343 atomic_set(&init_deasserted
, 1);
348 * Run STARTUP IPI loop.
350 Dprintk("#startup loops: %d.\n", num_starts
);
352 maxlvt
= lapic_get_maxlvt();
354 for (j
= 1; j
<= num_starts
; j
++) {
355 Dprintk("Sending STARTUP #%d.\n",j
);
356 apic_read_around(APIC_SPIV
);
357 apic_write(APIC_ESR
, 0);
359 Dprintk("After apic_write.\n");
366 apic_write_around(APIC_ICR2
, SET_APIC_DEST_FIELD(phys_apicid
));
368 /* Boot on the stack */
369 /* Kick the second */
370 apic_write_around(APIC_ICR
, APIC_DM_STARTUP
| (start_rip
>>12));
373 * Give the other CPU some time to accept the IPI.
377 Dprintk("Startup point 1.\n");
379 Dprintk("Waiting for send to finish...\n");
380 send_status
= safe_apic_wait_icr_idle();
383 * Give the other CPU some time to accept the IPI.
387 * Due to the Pentium erratum 3AP.
390 apic_read_around(APIC_SPIV
);
391 apic_write(APIC_ESR
, 0);
393 accept_status
= (apic_read(APIC_ESR
) & 0xEF);
394 if (send_status
|| accept_status
)
397 Dprintk("After Startup.\n");
400 printk(KERN_ERR
"APIC never delivered???\n");
402 printk(KERN_ERR
"APIC delivery error (%lx).\n", accept_status
);
404 return (send_status
| accept_status
);
408 struct work_struct work
;
409 struct task_struct
*idle
;
410 struct completion done
;
414 static void __cpuinit
do_fork_idle(struct work_struct
*work
)
416 struct create_idle
*c_idle
=
417 container_of(work
, struct create_idle
, work
);
419 c_idle
->idle
= fork_idle(c_idle
->cpu
);
420 complete(&c_idle
->done
);
426 static int __cpuinit
do_boot_cpu(int cpu
, int apicid
)
428 unsigned long boot_error
;
430 unsigned long start_rip
;
431 struct create_idle c_idle
= {
433 .done
= COMPLETION_INITIALIZER_ONSTACK(c_idle
.done
),
435 INIT_WORK(&c_idle
.work
, do_fork_idle
);
437 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
438 if (!cpu_gdt_descr
[cpu
].address
&&
439 !(cpu_gdt_descr
[cpu
].address
= get_zeroed_page(GFP_KERNEL
))) {
440 printk(KERN_ERR
"Failed to allocate GDT for CPU %d\n", cpu
);
444 /* Allocate node local memory for AP pdas */
445 if (cpu_pda(cpu
) == &boot_cpu_pda
[cpu
]) {
446 struct x8664_pda
*newpda
, *pda
;
447 int node
= cpu_to_node(cpu
);
449 newpda
= kmalloc_node(sizeof (struct x8664_pda
), GFP_ATOMIC
,
452 memcpy(newpda
, pda
, sizeof (struct x8664_pda
));
453 cpu_pda(cpu
) = newpda
;
456 "Could not allocate node local PDA for CPU %d on node %d\n",
460 alternatives_smp_switch(1);
462 c_idle
.idle
= get_idle_for_cpu(cpu
);
465 c_idle
.idle
->thread
.sp
= (unsigned long) (((struct pt_regs
*)
466 (THREAD_SIZE
+ task_stack_page(c_idle
.idle
))) - 1);
467 init_idle(c_idle
.idle
, cpu
);
472 * During cold boot process, keventd thread is not spun up yet.
473 * When we do cpu hot-add, we create idle threads on the fly, we should
474 * not acquire any attributes from the calling context. Hence the clean
475 * way to create kernel_threads() is to do that from keventd().
476 * We do the current_is_keventd() due to the fact that ACPI notifier
477 * was also queuing to keventd() and when the caller is already running
478 * in context of keventd(), we would end up with locking up the keventd
481 if (!keventd_up() || current_is_keventd())
482 c_idle
.work
.func(&c_idle
.work
);
484 schedule_work(&c_idle
.work
);
485 wait_for_completion(&c_idle
.done
);
488 if (IS_ERR(c_idle
.idle
)) {
489 printk("failed fork for CPU %d\n", cpu
);
490 return PTR_ERR(c_idle
.idle
);
493 set_idle_for_cpu(cpu
, c_idle
.idle
);
497 cpu_pda(cpu
)->pcurrent
= c_idle
.idle
;
499 start_rip
= setup_trampoline();
501 init_rsp
= c_idle
.idle
->thread
.sp
;
502 load_sp0(&per_cpu(init_tss
, cpu
), &c_idle
.idle
->thread
);
503 initial_code
= start_secondary
;
504 clear_tsk_thread_flag(c_idle
.idle
, TIF_FORK
);
506 printk(KERN_INFO
"Booting processor %d/%d APIC 0x%x\n", cpu
,
507 cpus_weight(cpu_present_map
),
511 * This grunge runs the startup process for
512 * the targeted processor.
515 atomic_set(&init_deasserted
, 0);
517 Dprintk("Setting warm reset code and vector.\n");
519 CMOS_WRITE(0xa, 0xf);
522 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip
>> 4;
524 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip
& 0xf;
528 * Be paranoid about clearing APIC errors.
530 apic_write(APIC_ESR
, 0);
534 * Status is now clean
539 * Starting actual IPI sequence...
541 boot_error
= wakeup_secondary_via_INIT(apicid
, start_rip
);
545 * allow APs to start initializing.
547 Dprintk("Before Callout %d.\n", cpu
);
548 cpu_set(cpu
, cpu_callout_map
);
549 Dprintk("After Callout %d.\n", cpu
);
552 * Wait 5s total for a response
554 for (timeout
= 0; timeout
< 50000; timeout
++) {
555 if (cpu_isset(cpu
, cpu_callin_map
))
556 break; /* It has booted */
560 if (cpu_isset(cpu
, cpu_callin_map
)) {
561 /* number CPUs logically, starting from 1 (BSP is 0) */
562 Dprintk("CPU has booted.\n");
565 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE
))
567 /* trampoline started but...? */
568 printk("Stuck ??\n");
570 /* trampoline code not run */
571 printk("Not responding.\n");
573 inquire_remote_apic(apicid
);
578 cpu_clear(cpu
, cpu_callout_map
); /* was set here (do_boot_cpu()) */
579 clear_bit(cpu
, (unsigned long *)&cpu_initialized
); /* was set by cpu_init() */
580 clear_node_cpumask(cpu
); /* was set by numa_add_cpu */
581 cpu_clear(cpu
, cpu_present_map
);
582 cpu_clear(cpu
, cpu_possible_map
);
583 per_cpu(x86_cpu_to_apicid
, cpu
) = BAD_APICID
;
590 cycles_t cacheflush_time
;
591 unsigned long cache_decay_ticks
;
594 * Cleanup possible dangling ends...
596 static __cpuinit
void smp_cleanup_boot(void)
599 * Paranoid: Set warm reset code and vector here back
605 * Reset trampoline flag
607 *((volatile int *) phys_to_virt(0x467)) = 0;
611 * Fall back to non SMP mode after errors.
613 * RED-PEN audit/test this more. I bet there is more state messed up here.
615 static __init
void disable_smp(void)
617 cpu_present_map
= cpumask_of_cpu(0);
618 cpu_possible_map
= cpumask_of_cpu(0);
619 if (smp_found_config
)
620 phys_cpu_present_map
= physid_mask_of_physid(boot_cpu_id
);
622 phys_cpu_present_map
= physid_mask_of_physid(0);
623 cpu_set(0, per_cpu(cpu_sibling_map
, 0));
624 cpu_set(0, per_cpu(cpu_core_map
, 0));
628 * Various sanity checks.
630 static int __init
smp_sanity_check(unsigned max_cpus
)
632 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map
)) {
633 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
634 hard_smp_processor_id());
635 physid_set(hard_smp_processor_id(), phys_cpu_present_map
);
639 * If we couldn't find an SMP configuration at boot time,
640 * get out of here now!
642 if (!smp_found_config
) {
643 printk(KERN_NOTICE
"SMP motherboard not detected.\n");
645 if (APIC_init_uniprocessor())
646 printk(KERN_NOTICE
"Local APIC not detected."
647 " Using dummy APIC emulation.\n");
652 * Should not be necessary because the MP table should list the boot
653 * CPU too, but we do it for the sake of robustness anyway.
655 if (!physid_isset(boot_cpu_id
, phys_cpu_present_map
)) {
656 printk(KERN_NOTICE
"weird, boot CPU (#%d) not listed by the BIOS.\n",
658 physid_set(hard_smp_processor_id(), phys_cpu_present_map
);
662 * If we couldn't find a local APIC, then get out of here now!
665 printk(KERN_ERR
"BIOS bug, local APIC #%d not detected!...\n",
667 printk(KERN_ERR
"... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
673 * If SMP should be disabled, then really disable it!
676 printk(KERN_INFO
"SMP mode deactivated, forcing use of dummy APIC emulation.\n");
684 static void __init
smp_cpu_index_default(void)
687 struct cpuinfo_x86
*c
;
689 for_each_cpu_mask(i
, cpu_possible_map
) {
691 /* mark all to hotplug */
692 c
->cpu_index
= NR_CPUS
;
697 * Prepare for SMP bootup. The MP table or ACPI has been read
698 * earlier. Just do some sanity checking here and enable APIC mode.
700 void __init
native_smp_prepare_cpus(unsigned int max_cpus
)
702 nmi_watchdog_default();
703 smp_cpu_index_default();
704 current_cpu_data
= boot_cpu_data
;
705 current_thread_info()->cpu
= 0; /* needed? */
706 set_cpu_sibling_map(0);
708 if (smp_sanity_check(max_cpus
) < 0) {
709 printk(KERN_INFO
"SMP disabled\n");
716 * Switch from PIC to APIC mode.
721 * Enable IO APIC before setting up error vector
723 if (!skip_ioapic_setup
&& nr_ioapics
)
725 end_local_APIC_setup();
727 if (GET_APIC_ID(apic_read(APIC_ID
)) != boot_cpu_id
) {
728 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
729 GET_APIC_ID(apic_read(APIC_ID
)), boot_cpu_id
);
730 /* Or can we switch back to PIC here? */
734 * Now start the IO-APICs
736 if (!skip_ioapic_setup
&& nr_ioapics
)
742 * Set up local APIC timer on boot CPU.
749 * Early setup to make printk work.
751 void __init
native_smp_prepare_boot_cpu(void)
753 int me
= smp_processor_id();
754 /* already set me in cpu_online_map in boot_cpu_init() */
755 cpu_set(me
, cpu_callout_map
);
756 per_cpu(cpu_state
, me
) = CPU_ONLINE
;
760 * Entry point to boot a CPU.
762 int __cpuinit
native_cpu_up(unsigned int cpu
)
764 int apicid
= cpu_present_to_apicid(cpu
);
768 WARN_ON(irqs_disabled());
770 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu
);
772 if (apicid
== BAD_APICID
|| apicid
== boot_cpu_id
||
773 !physid_isset(apicid
, phys_cpu_present_map
)) {
774 printk("__cpu_up: bad cpu %d\n", cpu
);
779 * Already booted CPU?
781 if (cpu_isset(cpu
, cpu_callin_map
)) {
782 Dprintk("do_boot_cpu %d Already started\n", cpu
);
787 * Save current MTRR state in case it was changed since early boot
788 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
792 per_cpu(cpu_state
, cpu
) = CPU_UP_PREPARE
;
794 err
= do_boot_cpu(cpu
, apicid
);
796 Dprintk("do_boot_cpu failed %d\n", err
);
800 /* Unleash the CPU! */
801 Dprintk("waiting for cpu %d\n", cpu
);
804 * Make sure and check TSC sync:
806 local_irq_save(flags
);
807 check_tsc_sync_source(cpu
);
808 local_irq_restore(flags
);
810 while (!cpu_isset(cpu
, cpu_online_map
))
818 * Finish the SMP boot.
820 void __init
native_smp_cpus_done(unsigned int max_cpus
)
824 check_nmi_watchdog();