x86: use create_idle struct in do_boot_cpu
[linux-2.6/mini2440.git] / arch / x86 / kernel / smpboot_32.c
blobfc1eb5255f66f0a12ac8250f0f9636499461fb8e
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/init.h>
38 #include <linux/kernel.h>
40 #include <linux/mm.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>
52 #include <asm/desc.h>
53 #include <asm/arch_hooks.h>
54 #include <asm/nmi.h>
56 #include <mach_apic.h>
57 #include <mach_wakecpu.h>
58 #include <smpboot_hooks.h>
59 #include <asm/vmi.h>
60 #include <asm/mtrr.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 static void map_cpu_to_logical_apicid(void);
79 /* State of each CPU. */
80 DEFINE_PER_CPU(int, cpu_state) = { 0 };
82 /* Store all idle threads, this can be reused instead of creating
83 * a new thread. Also avoids complicated thread destroy functionality
84 * for idle threads.
86 #ifdef CONFIG_HOTPLUG_CPU
88 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
89 * removed after init for !CONFIG_HOTPLUG_CPU.
91 static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
92 #define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
93 #define set_idle_for_cpu(x, p) (per_cpu(idle_thread_array, x) = (p))
94 #else
95 struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
96 #define get_idle_for_cpu(x) (idle_thread_array[(x)])
97 #define set_idle_for_cpu(x, p) (idle_thread_array[(x)] = (p))
98 #endif
100 static atomic_t init_deasserted;
102 static void __cpuinit smp_callin(void)
104 int cpuid, phys_id;
105 unsigned long timeout;
108 * If waken up by an INIT in an 82489DX configuration
109 * we may get here before an INIT-deassert IPI reaches
110 * our local APIC. We have to wait for the IPI or we'll
111 * lock up on an APIC access.
113 wait_for_init_deassert(&init_deasserted);
116 * (This works even if the APIC is not enabled.)
118 phys_id = GET_APIC_ID(apic_read(APIC_ID));
119 cpuid = smp_processor_id();
120 if (cpu_isset(cpuid, cpu_callin_map)) {
121 printk("huh, phys CPU#%d, CPU#%d already present??\n",
122 phys_id, cpuid);
123 BUG();
125 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
128 * STARTUP IPIs are fragile beasts as they might sometimes
129 * trigger some glue motherboard logic. Complete APIC bus
130 * silence for 1 second, this overestimates the time the
131 * boot CPU is spending to send the up to 2 STARTUP IPIs
132 * by a factor of two. This should be enough.
136 * Waiting 2s total for startup (udelay is not yet working)
138 timeout = jiffies + 2*HZ;
139 while (time_before(jiffies, timeout)) {
141 * Has the boot CPU finished it's STARTUP sequence?
143 if (cpu_isset(cpuid, cpu_callout_map))
144 break;
145 cpu_relax();
148 if (!time_before(jiffies, timeout)) {
149 printk("BUG: CPU%d started up but did not get a callout!\n",
150 cpuid);
151 BUG();
155 * the boot CPU has finished the init stage and is spinning
156 * on callin_map until we finish. We are free to set up this
157 * CPU, first the APIC. (this is probably redundant on most
158 * boards)
161 Dprintk("CALLIN, before setup_local_APIC().\n");
162 smp_callin_clear_local_apic();
163 setup_local_APIC();
164 map_cpu_to_logical_apicid();
167 * Get our bogomips.
169 calibrate_delay();
170 Dprintk("Stack at about %p\n",&cpuid);
173 * Save our processor parameters
175 smp_store_cpu_info(cpuid);
178 * Allow the master to continue.
180 cpu_set(cpuid, cpu_callin_map);
184 * Activate a secondary processor.
186 static void __cpuinit start_secondary(void *unused)
189 * Don't put *anything* before cpu_init(), SMP booting is too
190 * fragile that we want to limit the things done here to the
191 * most necessary things.
193 #ifdef CONFIG_VMI
194 vmi_bringup();
195 #endif
196 cpu_init();
197 preempt_disable();
198 smp_callin();
200 /* otherwise gcc will move up smp_processor_id before the cpu_init */
201 barrier();
203 * Check TSC synchronization with the BP:
205 check_tsc_sync_target();
207 if (nmi_watchdog == NMI_IO_APIC) {
208 disable_8259A_irq(0);
209 enable_NMI_through_LVT0();
210 enable_8259A_irq(0);
213 /* This must be done before setting cpu_online_map */
214 set_cpu_sibling_map(raw_smp_processor_id());
215 wmb();
218 * We need to hold call_lock, so there is no inconsistency
219 * between the time smp_call_function() determines number of
220 * IPI recipients, and the time when the determination is made
221 * for which cpus receive the IPI. Holding this
222 * lock helps us to not include this cpu in a currently in progress
223 * smp_call_function().
225 lock_ipi_call_lock();
226 cpu_set(smp_processor_id(), cpu_online_map);
227 unlock_ipi_call_lock();
228 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
230 setup_secondary_clock();
232 wmb();
233 cpu_idle();
237 * Everything has been set up for the secondary
238 * CPUs - they just need to reload everything
239 * from the task structure
240 * This function must not return.
242 void __devinit initialize_secondary(void)
245 * We don't actually need to load the full TSS,
246 * basically just the stack pointer and the ip.
249 asm volatile(
250 "movl %0,%%esp\n\t"
251 "jmp *%1"
253 :"m" (current->thread.sp),"m" (current->thread.ip));
256 /* Static state in head.S used to set up a CPU */
257 extern struct {
258 void * sp;
259 unsigned short ss;
260 } stack_start;
262 #ifdef CONFIG_NUMA
264 /* which logical CPUs are on which nodes */
265 cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
266 { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
267 EXPORT_SYMBOL(node_to_cpumask_map);
268 /* which node each logical CPU is on */
269 int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
270 EXPORT_SYMBOL(cpu_to_node_map);
272 /* set up a mapping between cpu and node. */
273 static inline void map_cpu_to_node(int cpu, int node)
275 printk("Mapping cpu %d to node %d\n", cpu, node);
276 cpu_set(cpu, node_to_cpumask_map[node]);
277 cpu_to_node_map[cpu] = node;
280 /* undo a mapping between cpu and node. */
281 static inline void unmap_cpu_to_node(int cpu)
283 int node;
285 printk("Unmapping cpu %d from all nodes\n", cpu);
286 for (node = 0; node < MAX_NUMNODES; node ++)
287 cpu_clear(cpu, node_to_cpumask_map[node]);
288 cpu_to_node_map[cpu] = 0;
290 #else /* !CONFIG_NUMA */
292 #define map_cpu_to_node(cpu, node) ({})
293 #define unmap_cpu_to_node(cpu) ({})
295 #endif /* CONFIG_NUMA */
297 u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
299 static void map_cpu_to_logical_apicid(void)
301 int cpu = smp_processor_id();
302 int apicid = logical_smp_processor_id();
303 int node = apicid_to_node(apicid);
305 if (!node_online(node))
306 node = first_online_node;
308 cpu_2_logical_apicid[cpu] = apicid;
309 map_cpu_to_node(cpu, node);
312 static void unmap_cpu_to_logical_apicid(int cpu)
314 cpu_2_logical_apicid[cpu] = BAD_APICID;
315 unmap_cpu_to_node(cpu);
318 static inline void __inquire_remote_apic(int apicid)
320 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
321 char *names[] = { "ID", "VERSION", "SPIV" };
322 int timeout;
323 u32 status;
325 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
327 for (i = 0; i < ARRAY_SIZE(regs); i++) {
328 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
331 * Wait for idle.
333 status = safe_apic_wait_icr_idle();
334 if (status)
335 printk(KERN_CONT
336 "a previous APIC delivery may have failed\n");
338 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
339 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
341 timeout = 0;
342 do {
343 udelay(100);
344 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
345 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
347 switch (status) {
348 case APIC_ICR_RR_VALID:
349 status = apic_read(APIC_RRR);
350 printk(KERN_CONT "%08x\n", status);
351 break;
352 default:
353 printk(KERN_CONT "failed\n");
358 #ifdef WAKE_SECONDARY_VIA_NMI
360 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
361 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
362 * won't ... remember to clear down the APIC, etc later.
364 static int __devinit
365 wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
367 unsigned long send_status, accept_status = 0;
368 int maxlvt;
370 /* Target chip */
371 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
373 /* Boot on the stack */
374 /* Kick the second */
375 apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
377 Dprintk("Waiting for send to finish...\n");
378 send_status = safe_apic_wait_icr_idle();
381 * Give the other CPU some time to accept the IPI.
383 udelay(200);
385 * Due to the Pentium erratum 3AP.
387 maxlvt = lapic_get_maxlvt();
388 if (maxlvt > 3) {
389 apic_read_around(APIC_SPIV);
390 apic_write(APIC_ESR, 0);
392 accept_status = (apic_read(APIC_ESR) & 0xEF);
393 Dprintk("NMI sent.\n");
395 if (send_status)
396 printk("APIC never delivered???\n");
397 if (accept_status)
398 printk("APIC delivery error (%lx).\n", accept_status);
400 return (send_status | accept_status);
402 #endif /* WAKE_SECONDARY_VIA_NMI */
404 #ifdef WAKE_SECONDARY_VIA_INIT
405 static int __devinit
406 wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
408 unsigned long send_status, accept_status = 0;
409 int maxlvt, num_starts, j;
412 * Be paranoid about clearing APIC errors.
414 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
415 apic_read_around(APIC_SPIV);
416 apic_write(APIC_ESR, 0);
417 apic_read(APIC_ESR);
420 Dprintk("Asserting INIT.\n");
423 * Turn INIT on target chip
425 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
428 * Send IPI
430 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
431 | APIC_DM_INIT);
433 Dprintk("Waiting for send to finish...\n");
434 send_status = safe_apic_wait_icr_idle();
436 mdelay(10);
438 Dprintk("Deasserting INIT.\n");
440 /* Target chip */
441 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
443 /* Send IPI */
444 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
446 Dprintk("Waiting for send to finish...\n");
447 send_status = safe_apic_wait_icr_idle();
449 mb();
450 atomic_set(&init_deasserted, 1);
453 * Should we send STARTUP IPIs ?
455 * Determine this based on the APIC version.
456 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
458 if (APIC_INTEGRATED(apic_version[phys_apicid]))
459 num_starts = 2;
460 else
461 num_starts = 0;
464 * Paravirt / VMI wants a startup IPI hook here to set up the
465 * target processor state.
467 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
468 (unsigned long) stack_start.sp);
471 * Run STARTUP IPI loop.
473 Dprintk("#startup loops: %d.\n", num_starts);
475 maxlvt = lapic_get_maxlvt();
477 for (j = 1; j <= num_starts; j++) {
478 Dprintk("Sending STARTUP #%d.\n",j);
479 apic_read_around(APIC_SPIV);
480 apic_write(APIC_ESR, 0);
481 apic_read(APIC_ESR);
482 Dprintk("After apic_write.\n");
485 * STARTUP IPI
488 /* Target chip */
489 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
491 /* Boot on the stack */
492 /* Kick the second */
493 apic_write_around(APIC_ICR, APIC_DM_STARTUP
494 | (start_eip >> 12));
497 * Give the other CPU some time to accept the IPI.
499 udelay(300);
501 Dprintk("Startup point 1.\n");
503 Dprintk("Waiting for send to finish...\n");
504 send_status = safe_apic_wait_icr_idle();
507 * Give the other CPU some time to accept the IPI.
509 udelay(200);
511 * Due to the Pentium erratum 3AP.
513 if (maxlvt > 3) {
514 apic_read_around(APIC_SPIV);
515 apic_write(APIC_ESR, 0);
517 accept_status = (apic_read(APIC_ESR) & 0xEF);
518 if (send_status || accept_status)
519 break;
521 Dprintk("After Startup.\n");
523 if (send_status)
524 printk("APIC never delivered???\n");
525 if (accept_status)
526 printk("APIC delivery error (%lx).\n", accept_status);
528 return (send_status | accept_status);
530 #endif /* WAKE_SECONDARY_VIA_INIT */
532 extern cpumask_t cpu_initialized;
534 struct create_idle {
535 struct work_struct work;
536 struct task_struct *idle;
537 struct completion done;
538 int cpu;
541 static void __cpuinit do_fork_idle(struct work_struct *work)
543 struct create_idle *c_idle =
544 container_of(work, struct create_idle, work);
546 c_idle->idle = fork_idle(c_idle->cpu);
547 complete(&c_idle->done);
549 static int __cpuinit do_boot_cpu(int apicid, int cpu)
551 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
552 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
553 * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
556 unsigned long boot_error;
557 int timeout;
558 unsigned long start_eip;
559 unsigned short nmi_high = 0, nmi_low = 0;
560 struct create_idle c_idle = {
561 .cpu = cpu,
562 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
564 INIT_WORK(&c_idle.work, do_fork_idle);
567 * Save current MTRR state in case it was changed since early boot
568 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
570 mtrr_save_state();
572 c_idle.idle = get_idle_for_cpu(cpu);
575 * We can't use kernel_thread since we must avoid to
576 * reschedule the child.
578 if (c_idle.idle) {
579 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
580 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
581 init_idle(c_idle.idle, cpu);
582 goto do_rest;
585 if (!keventd_up() || current_is_keventd())
586 c_idle.work.func(&c_idle.work);
587 else {
588 schedule_work(&c_idle.work);
589 wait_for_completion(&c_idle.done);
592 if (IS_ERR(c_idle.idle)) {
593 printk(KERN_ERR "failed fork for CPU %d\n", cpu);
594 return PTR_ERR(c_idle.idle);
597 set_idle_for_cpu(cpu, c_idle.idle);
598 do_rest:
599 per_cpu(current_task, cpu) = c_idle.idle;
600 init_gdt(cpu);
601 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
603 c_idle.idle->thread.ip = (unsigned long) start_secondary;
604 /* start_eip had better be page-aligned! */
605 start_eip = setup_trampoline();
607 alternatives_smp_switch(1);
609 /* So we see what's up */
610 printk("Booting processor %d/%d ip %lx\n", cpu, apicid, start_eip);
611 /* Stack for startup_32 can be just as for start_secondary onwards */
612 stack_start.sp = (void *) c_idle.idle->thread.sp;
614 irq_ctx_init(cpu);
617 * This grunge runs the startup process for
618 * the targeted processor.
621 atomic_set(&init_deasserted, 0);
623 Dprintk("Setting warm reset code and vector.\n");
625 store_NMI_vector(&nmi_high, &nmi_low);
627 smpboot_setup_warm_reset_vector(start_eip);
630 * Starting actual IPI sequence...
632 boot_error = wakeup_secondary_cpu(apicid, start_eip);
634 if (!boot_error) {
636 * allow APs to start initializing.
638 Dprintk("Before Callout %d.\n", cpu);
639 cpu_set(cpu, cpu_callout_map);
640 Dprintk("After Callout %d.\n", cpu);
643 * Wait 5s total for a response
645 for (timeout = 0; timeout < 50000; timeout++) {
646 if (cpu_isset(cpu, cpu_callin_map))
647 break; /* It has booted */
648 udelay(100);
651 if (cpu_isset(cpu, cpu_callin_map)) {
652 /* number CPUs logically, starting from 1 (BSP is 0) */
653 Dprintk("OK.\n");
654 printk("CPU%d: ", cpu);
655 print_cpu_info(&cpu_data(cpu));
656 Dprintk("CPU has booted.\n");
657 } else {
658 boot_error= 1;
659 if (*((volatile unsigned char *)trampoline_base)
660 == 0xA5)
661 /* trampoline started but...? */
662 printk("Stuck ??\n");
663 else
664 /* trampoline code not run */
665 printk("Not responding.\n");
666 inquire_remote_apic(apicid);
670 if (boot_error) {
671 /* Try to put things back the way they were before ... */
672 unmap_cpu_to_logical_apicid(cpu);
673 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
674 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
675 cpu_clear(cpu, cpu_possible_map);
676 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
679 /* mark "stuck" area as not stuck */
680 *((volatile unsigned long *)trampoline_base) = 0;
682 return boot_error;
685 #ifdef CONFIG_HOTPLUG_CPU
686 void cpu_exit_clear(void)
688 int cpu = raw_smp_processor_id();
690 idle_task_exit();
692 cpu_uninit();
693 irq_ctx_exit(cpu);
695 cpu_clear(cpu, cpu_callout_map);
696 cpu_clear(cpu, cpu_callin_map);
698 unmap_cpu_to_logical_apicid(cpu);
700 #endif
702 struct warm_boot_cpu_info {
703 struct completion *complete;
704 struct work_struct task;
705 int apicid;
706 int cpu;
709 static void __cpuinit do_warm_boot_cpu(struct work_struct *work)
711 struct warm_boot_cpu_info *info =
712 container_of(work, struct warm_boot_cpu_info, task);
713 do_boot_cpu(info->apicid, info->cpu);
714 complete(info->complete);
717 static void __cpuinit __smp_prepare_cpu(int cpu)
719 DECLARE_COMPLETION_ONSTACK(done);
720 struct warm_boot_cpu_info info;
721 int apicid;
723 apicid = per_cpu(x86_cpu_to_apicid, cpu);
725 info.complete = &done;
726 info.apicid = apicid;
727 info.cpu = cpu;
728 INIT_WORK(&info.task, do_warm_boot_cpu);
730 /* init low mem mapping */
731 clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
732 min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
733 flush_tlb_all();
734 if (!keventd_up() || current_is_keventd())
735 info.task.func(&info.task);
736 else {
737 schedule_work(&info.task);
738 wait_for_completion(&done);
742 static int boot_cpu_logical_apicid;
743 /* Where the IO area was mapped on multiquad, always 0 otherwise */
744 void *xquad_portio;
745 #ifdef CONFIG_X86_NUMAQ
746 EXPORT_SYMBOL(xquad_portio);
747 #endif
749 static void __init disable_smp(void)
751 cpu_possible_map = cpumask_of_cpu(0);
752 cpu_present_map = cpumask_of_cpu(0);
753 smpboot_clear_io_apic_irqs();
754 phys_cpu_present_map = physid_mask_of_physid(0);
755 map_cpu_to_logical_apicid();
756 cpu_set(0, per_cpu(cpu_sibling_map, 0));
757 cpu_set(0, per_cpu(cpu_core_map, 0));
760 static int __init smp_sanity_check(unsigned max_cpus)
763 * If we couldn't find an SMP configuration at boot time,
764 * get out of here now!
766 if (!smp_found_config && !acpi_lapic) {
767 printk(KERN_NOTICE "SMP motherboard not detected.\n");
768 disable_smp();
769 if (APIC_init_uniprocessor())
770 printk(KERN_NOTICE "Local APIC not detected."
771 " Using dummy APIC emulation.\n");
772 return -1;
776 * Should not be necessary because the MP table should list the boot
777 * CPU too, but we do it for the sake of robustness anyway.
778 * Makes no sense to do this check in clustered apic mode, so skip it
780 if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
781 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
782 boot_cpu_physical_apicid);
783 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
787 * If we couldn't find a local APIC, then get out of here now!
789 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
790 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
791 boot_cpu_physical_apicid);
792 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
793 return -1;
796 verify_local_APIC();
799 * If SMP should be disabled, then really disable it!
801 if (!max_cpus) {
802 smp_found_config = 0;
803 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
805 if (nmi_watchdog == NMI_LOCAL_APIC) {
806 printk(KERN_INFO "activating minimal APIC for NMI watchdog use.\n");
807 connect_bsp_APIC();
808 setup_local_APIC();
810 return -1;
812 return 0;
816 * Cycle through the processors sending APIC IPIs to boot each.
818 static void __init smp_boot_cpus(unsigned int max_cpus)
821 * Setup boot CPU information
823 smp_store_cpu_info(0); /* Final full version of the data */
824 printk(KERN_INFO "CPU%d: ", 0);
825 print_cpu_info(&cpu_data(0));
827 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
828 boot_cpu_logical_apicid = logical_smp_processor_id();
830 current_thread_info()->cpu = 0;
832 set_cpu_sibling_map(0);
834 if (smp_sanity_check(max_cpus) < 0) {
835 printk(KERN_INFO "SMP disabled\n");
836 disable_smp();
837 return;
840 connect_bsp_APIC();
841 setup_local_APIC();
842 map_cpu_to_logical_apicid();
845 setup_portio_remap();
847 smpboot_setup_io_apic();
849 setup_boot_clock();
852 /* These are wrappers to interface to the new boot process. Someone
853 who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
854 void __init native_smp_prepare_cpus(unsigned int max_cpus)
856 nmi_watchdog_default();
857 cpu_callin_map = cpumask_of_cpu(0);
858 mb();
859 smp_boot_cpus(max_cpus);
862 void __init native_smp_prepare_boot_cpu(void)
864 unsigned int cpu = smp_processor_id();
866 init_gdt(cpu);
867 switch_to_new_gdt();
869 cpu_set(cpu, cpu_callout_map);
870 __get_cpu_var(cpu_state) = CPU_ONLINE;
873 int __cpuinit native_cpu_up(unsigned int cpu)
875 int apicid = cpu_present_to_apicid(cpu);
876 unsigned long flags;
878 WARN_ON(irqs_disabled());
880 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
882 if (apicid == BAD_APICID || apicid == boot_cpu_physical_apicid ||
883 !physid_isset(apicid, phys_cpu_present_map)) {
884 printk(KERN_ERR "%s: bad cpu %d\n", __func__, cpu);
885 return -EINVAL;
888 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
890 __smp_prepare_cpu(cpu);
892 /* In case one didn't come up */
893 if (!cpu_isset(cpu, cpu_callin_map)) {
894 printk(KERN_DEBUG "skipping cpu%d, didn't come online\n", cpu);
895 return -EIO;
900 * Check TSC synchronization with the AP (keep irqs disabled
901 * while doing so):
903 local_irq_save(flags);
904 check_tsc_sync_source(cpu);
905 local_irq_restore(flags);
907 while (!cpu_isset(cpu, cpu_online_map)) {
908 cpu_relax();
909 touch_nmi_watchdog();
912 return 0;
915 extern void impress_friends(void);
916 extern void smp_checks(void);
918 void __init native_smp_cpus_done(unsigned int max_cpus)
921 * Cleanup possible dangling ends...
923 smpboot_restore_warm_reset_vector();
925 Dprintk("Boot done.\n");
927 impress_friends();
928 smp_checks();
929 #ifdef CONFIG_X86_IO_APIC
930 setup_ioapic_dest();
931 #endif
932 check_nmi_watchdog();
933 zap_low_mappings();