]> err.no Git - linux-2.6/blob - arch/x86/kernel/smpboot_64.c
x86: call identify_secondary_cpu in smp_store_cpu_info
[linux-2.6] / arch / x86 / kernel / smpboot_64.c
1 /*
2  *      x86 SMP booting functions
3  *
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.
7  *
8  *      Much of the core SMP work is based on previous work by Thomas Radke, to
9  *      whom a great many thanks are extended.
10  *
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.
14  *
15  *      This code is released under the GNU General Public License version 2
16  *
17  *      Fixes
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
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  *      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.
35  *                                      Various cleanups.
36  *                                      Probably mostly hotplug CPU ready now.
37  *      Ashok Raj                       : CPU hotplug support
38  */
39
40
41 #include <linux/init.h>
42
43 #include <linux/mm.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>
52
53 #include <asm/mtrr.h>
54 #include <asm/pgalloc.h>
55 #include <asm/desc.h>
56 #include <asm/tlbflush.h>
57 #include <asm/proto.h>
58 #include <asm/nmi.h>
59 #include <asm/irq.h>
60 #include <asm/hw_irq.h>
61 #include <asm/numa.h>
62
63 /* Set when the idlers are all forked */
64 int smp_threads_ready;
65
66 /* State of each CPU */
67 DEFINE_PER_CPU(int, cpu_state) = { 0 };
68
69 /*
70  * Store all idle threads, this can be reused instead of creating
71  * a new thread. Also avoids complicated thread destroy functionality
72  * for idle threads.
73  */
74 #ifdef CONFIG_HOTPLUG_CPU
75 /*
76  * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
77  * removed after init for !CONFIG_HOTPLUG_CPU.
78  */
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))
82 #else
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))
86 #endif
87
88 /*
89  * The bootstrap kernel entry code has set these up. Save them for
90  * a given CPU
91  */
92
93 static void __cpuinit smp_store_cpu_info(int id)
94 {
95         struct cpuinfo_x86 *c = &cpu_data(id);
96
97         *c = boot_cpu_data;
98         c->cpu_index = id;
99         if (id != 0)
100                 identify_secondary_cpu(c);
101 }
102
103 static inline void wait_for_init_deassert(atomic_t *deassert)
104 {
105         while (!atomic_read(deassert))
106                 cpu_relax();
107         return;
108 }
109
110 static atomic_t init_deasserted __cpuinitdata;
111
112 /*
113  * Report back to the Boot Processor.
114  * Running on AP.
115  */
116 void __cpuinit smp_callin(void)
117 {
118         int cpuid, phys_id;
119         unsigned long timeout;
120
121         /*
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.
126          */
127         wait_for_init_deassert(&init_deasserted);
128
129         /*
130          * (This works even if the APIC is not enabled.)
131          */
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",
136                                         phys_id, cpuid);
137         }
138         Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
139
140         /*
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.
146          */
147
148         /*
149          * Waiting 2s total for startup (udelay is not yet working)
150          */
151         timeout = jiffies + 2*HZ;
152         while (time_before(jiffies, timeout)) {
153                 /*
154                  * Has the boot CPU finished it's STARTUP sequence?
155                  */
156                 if (cpu_isset(cpuid, cpu_callout_map))
157                         break;
158                 cpu_relax();
159         }
160
161         if (!time_before(jiffies, timeout)) {
162                 panic("smp_callin: CPU%d started up but did not get a callout!\n",
163                         cpuid);
164         }
165
166         /*
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
170          * boards)
171          */
172
173         Dprintk("CALLIN, before setup_local_APIC().\n");
174         setup_local_APIC();
175         end_local_APIC_setup();
176
177         /*
178          * Get our bogomips.
179          *
180          * Need to enable IRQs because it can take longer and then
181          * the NMI watchdog might kill us.
182          */
183         local_irq_enable();
184         calibrate_delay();
185         local_irq_disable();
186         Dprintk("Stack at about %p\n",&cpuid);
187
188         /*
189          * Save our processor parameters
190          */
191         smp_store_cpu_info(cpuid);
192
193         /*
194          * Allow the master to continue.
195          */
196         cpu_set(cpuid, cpu_callin_map);
197 }
198
199 /*
200  * Setup code on secondary processor (after comming out of the trampoline)
201  */
202 void __cpuinit start_secondary(void)
203 {
204         /*
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.
208          */
209         cpu_init();
210         preempt_disable();
211         smp_callin();
212
213         /* otherwise gcc will move up the smp_processor_id before the cpu_init */
214         barrier();
215
216         /*
217          * Check TSC sync first:
218          */
219         check_tsc_sync_target();
220
221         if (nmi_watchdog == NMI_IO_APIC) {
222                 disable_8259A_irq(0);
223                 enable_NMI_through_LVT0();
224                 enable_8259A_irq(0);
225         }
226
227         /*
228          * The sibling maps must be set before turing the online map on for
229          * this cpu
230          */
231         set_cpu_sibling_map(smp_processor_id());
232
233         /*
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().
240          */
241         lock_ipi_call_lock();
242         spin_lock(&vector_lock);
243
244         /* Setup the per cpu irq handling data structures */
245         __setup_vector_irq(smp_processor_id());
246         /*
247          * Allow the master to continue.
248          */
249         spin_unlock(&vector_lock);
250         cpu_set(smp_processor_id(), cpu_online_map);
251         per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
252
253         unlock_ipi_call_lock();
254
255         setup_secondary_clock();
256
257         cpu_idle();
258 }
259
260 extern volatile unsigned long init_rsp;
261 extern void (*initial_code)(void);
262
263 #ifdef APIC_DEBUG
264 static void inquire_remote_apic(int apicid)
265 {
266         unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
267         char *names[] = { "ID", "VERSION", "SPIV" };
268         int timeout;
269         u32 status;
270
271         printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
272
273         for (i = 0; i < ARRAY_SIZE(regs); i++) {
274                 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
275
276                 /*
277                  * Wait for idle.
278                  */
279                 status = safe_apic_wait_icr_idle();
280                 if (status)
281                         printk(KERN_CONT
282                                "a previous APIC delivery may have failed\n");
283
284                 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
285                 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
286
287                 timeout = 0;
288                 do {
289                         udelay(100);
290                         status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
291                 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
292
293                 switch (status) {
294                 case APIC_ICR_RR_VALID:
295                         status = apic_read(APIC_RRR);
296                         printk(KERN_CONT "%08x\n", status);
297                         break;
298                 default:
299                         printk(KERN_CONT "failed\n");
300                 }
301         }
302 }
303 #endif
304
305 /*
306  * Kick the secondary to wake up.
307  */
308 static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
309 {
310         unsigned long send_status, accept_status = 0;
311         int maxlvt, num_starts, j;
312
313         Dprintk("Asserting INIT.\n");
314
315         /*
316          * Turn INIT on target chip
317          */
318         apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
319
320         /*
321          * Send IPI
322          */
323         apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
324                                 | APIC_DM_INIT);
325
326         Dprintk("Waiting for send to finish...\n");
327         send_status = safe_apic_wait_icr_idle();
328
329         mdelay(10);
330
331         Dprintk("Deasserting INIT.\n");
332
333         /* Target chip */
334         apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
335
336         /* Send IPI */
337         apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
338
339         Dprintk("Waiting for send to finish...\n");
340         send_status = safe_apic_wait_icr_idle();
341
342         mb();
343         atomic_set(&init_deasserted, 1);
344
345         num_starts = 2;
346
347         /*
348          * Paravirt / VMI wants a startup IPI hook here to set up the
349          * target processor state.
350          */
351         startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
352                         (unsigned long) init_rsp);
353
354
355         /*
356          * Run STARTUP IPI loop.
357          */
358         Dprintk("#startup loops: %d.\n", num_starts);
359
360         maxlvt = lapic_get_maxlvt();
361
362         for (j = 1; j <= num_starts; j++) {
363                 Dprintk("Sending STARTUP #%d.\n",j);
364                 apic_read_around(APIC_SPIV);
365                 apic_write(APIC_ESR, 0);
366                 apic_read(APIC_ESR);
367                 Dprintk("After apic_write.\n");
368
369                 /*
370                  * STARTUP IPI
371                  */
372
373                 /* Target chip */
374                 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
375
376                 /* Boot on the stack */
377                 /* Kick the second */
378                 apic_write_around(APIC_ICR, APIC_DM_STARTUP | (start_rip>>12));
379
380                 /*
381                  * Give the other CPU some time to accept the IPI.
382                  */
383                 udelay(300);
384
385                 Dprintk("Startup point 1.\n");
386
387                 Dprintk("Waiting for send to finish...\n");
388                 send_status = safe_apic_wait_icr_idle();
389
390                 /*
391                  * Give the other CPU some time to accept the IPI.
392                  */
393                 udelay(200);
394                 /*
395                  * Due to the Pentium erratum 3AP.
396                  */
397                 if (maxlvt > 3) {
398                         apic_read_around(APIC_SPIV);
399                         apic_write(APIC_ESR, 0);
400                 }
401                 accept_status = (apic_read(APIC_ESR) & 0xEF);
402                 if (send_status || accept_status)
403                         break;
404         }
405         Dprintk("After Startup.\n");
406
407         if (send_status)
408                 printk(KERN_ERR "APIC never delivered???\n");
409         if (accept_status)
410                 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
411
412         return (send_status | accept_status);
413 }
414
415 struct create_idle {
416         struct work_struct work;
417         struct task_struct *idle;
418         struct completion done;
419         int cpu;
420 };
421
422 static void __cpuinit do_fork_idle(struct work_struct *work)
423 {
424         struct create_idle *c_idle =
425                 container_of(work, struct create_idle, work);
426
427         c_idle->idle = fork_idle(c_idle->cpu);
428         complete(&c_idle->done);
429 }
430
431 /*
432  * Boot one CPU.
433  */
434 static int __cpuinit do_boot_cpu(int cpu, int apicid)
435 {
436         unsigned long boot_error;
437         int timeout;
438         unsigned long start_rip;
439         struct create_idle c_idle = {
440                 .cpu = cpu,
441                 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
442         };
443         INIT_WORK(&c_idle.work, do_fork_idle);
444
445         /* allocate memory for gdts of secondary cpus. Hotplug is considered */
446         if (!cpu_gdt_descr[cpu].address &&
447                 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
448                 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
449                 return -1;
450         }
451
452         /* Allocate node local memory for AP pdas */
453         if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
454                 struct x8664_pda *newpda, *pda;
455                 int node = cpu_to_node(cpu);
456                 pda = cpu_pda(cpu);
457                 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
458                                       node);
459                 if (newpda) {
460                         memcpy(newpda, pda, sizeof (struct x8664_pda));
461                         cpu_pda(cpu) = newpda;
462                 } else
463                         printk(KERN_ERR
464                 "Could not allocate node local PDA for CPU %d on node %d\n",
465                                 cpu, node);
466         }
467
468         alternatives_smp_switch(1);
469
470         c_idle.idle = get_idle_for_cpu(cpu);
471
472         if (c_idle.idle) {
473                 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
474                         (THREAD_SIZE +  task_stack_page(c_idle.idle))) - 1);
475                 init_idle(c_idle.idle, cpu);
476                 goto do_rest;
477         }
478
479         /*
480          * During cold boot process, keventd thread is not spun up yet.
481          * When we do cpu hot-add, we create idle threads on the fly, we should
482          * not acquire any attributes from the calling context. Hence the clean
483          * way to create kernel_threads() is to do that from keventd().
484          * We do the current_is_keventd() due to the fact that ACPI notifier
485          * was also queuing to keventd() and when the caller is already running
486          * in context of keventd(), we would end up with locking up the keventd
487          * thread.
488          */
489         if (!keventd_up() || current_is_keventd())
490                 c_idle.work.func(&c_idle.work);
491         else {
492                 schedule_work(&c_idle.work);
493                 wait_for_completion(&c_idle.done);
494         }
495
496         if (IS_ERR(c_idle.idle)) {
497                 printk("failed fork for CPU %d\n", cpu);
498                 return PTR_ERR(c_idle.idle);
499         }
500
501         set_idle_for_cpu(cpu, c_idle.idle);
502
503 do_rest:
504
505         cpu_pda(cpu)->pcurrent = c_idle.idle;
506
507         start_rip = setup_trampoline();
508
509         init_rsp = c_idle.idle->thread.sp;
510         load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
511         initial_code = start_secondary;
512         clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
513
514         printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
515                 cpus_weight(cpu_present_map),
516                 apicid);
517
518         /*
519          * This grunge runs the startup process for
520          * the targeted processor.
521          */
522
523         atomic_set(&init_deasserted, 0);
524
525         Dprintk("Setting warm reset code and vector.\n");
526
527         CMOS_WRITE(0xa, 0xf);
528         local_flush_tlb();
529         Dprintk("1.\n");
530         *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
531         Dprintk("2.\n");
532         *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
533         Dprintk("3.\n");
534
535         /*
536          * Be paranoid about clearing APIC errors.
537          */
538         apic_write(APIC_ESR, 0);
539         apic_read(APIC_ESR);
540
541         /*
542          * Status is now clean
543          */
544         boot_error = 0;
545
546         /*
547          * Starting actual IPI sequence...
548          */
549         boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
550
551         if (!boot_error) {
552                 /*
553                  * allow APs to start initializing.
554                  */
555                 Dprintk("Before Callout %d.\n", cpu);
556                 cpu_set(cpu, cpu_callout_map);
557                 Dprintk("After Callout %d.\n", cpu);
558
559                 /*
560                  * Wait 5s total for a response
561                  */
562                 for (timeout = 0; timeout < 50000; timeout++) {
563                         if (cpu_isset(cpu, cpu_callin_map))
564                                 break;  /* It has booted */
565                         udelay(100);
566                 }
567
568                 if (cpu_isset(cpu, cpu_callin_map)) {
569                         /* number CPUs logically, starting from 1 (BSP is 0) */
570                         Dprintk("CPU has booted.\n");
571                         printk(KERN_INFO "CPU%d: ", cpu);
572                         print_cpu_info(&cpu_data(cpu));
573                 } else {
574                         boot_error = 1;
575                         if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
576                                         == 0xA5)
577                                 /* trampoline started but...? */
578                                 printk("Stuck ??\n");
579                         else
580                                 /* trampoline code not run */
581                                 printk("Not responding.\n");
582 #ifdef APIC_DEBUG
583                         inquire_remote_apic(apicid);
584 #endif
585                 }
586         }
587         if (boot_error) {
588                 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
589                 clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
590                 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
591                 cpu_clear(cpu, cpu_present_map);
592                 cpu_clear(cpu, cpu_possible_map);
593                 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
594                 return -EIO;
595         }
596
597         return 0;
598 }
599
600 cycles_t cacheflush_time;
601 unsigned long cache_decay_ticks;
602
603 /*
604  * Cleanup possible dangling ends...
605  */
606 static __cpuinit void smp_cleanup_boot(void)
607 {
608         /*
609          * Paranoid:  Set warm reset code and vector here back
610          * to default values.
611          */
612         CMOS_WRITE(0, 0xf);
613
614         /*
615          * Reset trampoline flag
616          */
617         *((volatile int *) phys_to_virt(0x467)) = 0;
618 }
619
620 /*
621  * Fall back to non SMP mode after errors.
622  *
623  * RED-PEN audit/test this more. I bet there is more state messed up here.
624  */
625 static __init void disable_smp(void)
626 {
627         cpu_present_map = cpumask_of_cpu(0);
628         cpu_possible_map = cpumask_of_cpu(0);
629         if (smp_found_config)
630                 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
631         else
632                 phys_cpu_present_map = physid_mask_of_physid(0);
633         cpu_set(0, per_cpu(cpu_sibling_map, 0));
634         cpu_set(0, per_cpu(cpu_core_map, 0));
635 }
636
637 /*
638  * Various sanity checks.
639  */
640 static int __init smp_sanity_check(unsigned max_cpus)
641 {
642         if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
643                 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
644                        hard_smp_processor_id());
645                 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
646         }
647
648         /*
649          * If we couldn't find an SMP configuration at boot time,
650          * get out of here now!
651          */
652         if (!smp_found_config) {
653                 printk(KERN_NOTICE "SMP motherboard not detected.\n");
654                 disable_smp();
655                 if (APIC_init_uniprocessor())
656                         printk(KERN_NOTICE "Local APIC not detected."
657                                            " Using dummy APIC emulation.\n");
658                 return -1;
659         }
660
661         /*
662          * Should not be necessary because the MP table should list the boot
663          * CPU too, but we do it for the sake of robustness anyway.
664          */
665         if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
666                 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
667                                                                  boot_cpu_id);
668                 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
669         }
670
671         /*
672          * If we couldn't find a local APIC, then get out of here now!
673          */
674         if (!cpu_has_apic) {
675                 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
676                         boot_cpu_id);
677                 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
678                 nr_ioapics = 0;
679                 return -1;
680         }
681
682         /*
683          * If SMP should be disabled, then really disable it!
684          */
685         if (!max_cpus) {
686                 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
687                 nr_ioapics = 0;
688                 return -1;
689         }
690
691         return 0;
692 }
693
694 static void __init smp_cpu_index_default(void)
695 {
696         int i;
697         struct cpuinfo_x86 *c;
698
699         for_each_cpu_mask(i, cpu_possible_map) {
700                 c = &cpu_data(i);
701                 /* mark all to hotplug */
702                 c->cpu_index = NR_CPUS;
703         }
704 }
705
706 /*
707  * Prepare for SMP bootup.  The MP table or ACPI has been read
708  * earlier.  Just do some sanity checking here and enable APIC mode.
709  */
710 void __init native_smp_prepare_cpus(unsigned int max_cpus)
711 {
712         nmi_watchdog_default();
713         smp_cpu_index_default();
714         current_cpu_data = boot_cpu_data;
715         current_thread_info()->cpu = 0;  /* needed? */
716         set_cpu_sibling_map(0);
717
718         if (smp_sanity_check(max_cpus) < 0) {
719                 printk(KERN_INFO "SMP disabled\n");
720                 disable_smp();
721                 return;
722         }
723
724
725         /*
726          * Switch from PIC to APIC mode.
727          */
728         setup_local_APIC();
729
730         /*
731          * Enable IO APIC before setting up error vector
732          */
733         if (!skip_ioapic_setup && nr_ioapics)
734                 enable_IO_APIC();
735         end_local_APIC_setup();
736
737         if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
738                 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
739                       GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
740                 /* Or can we switch back to PIC here? */
741         }
742
743         /*
744          * Now start the IO-APICs
745          */
746         if (!skip_ioapic_setup && nr_ioapics)
747                 setup_IO_APIC();
748         else
749                 nr_ioapics = 0;
750
751         /*
752          * Set up local APIC timer on boot CPU.
753          */
754
755         setup_boot_clock();
756         printk(KERN_INFO "CPU%d: ", 0);
757         print_cpu_info(&cpu_data(0));
758 }
759
760 /*
761  * Early setup to make printk work.
762  */
763 void __init native_smp_prepare_boot_cpu(void)
764 {
765         int me = smp_processor_id();
766         /* already set me in cpu_online_map in boot_cpu_init() */
767         cpu_set(me, cpu_callout_map);
768         per_cpu(cpu_state, me) = CPU_ONLINE;
769 }
770
771 /*
772  * Entry point to boot a CPU.
773  */
774 int __cpuinit native_cpu_up(unsigned int cpu)
775 {
776         int apicid = cpu_present_to_apicid(cpu);
777         unsigned long flags;
778         int err;
779
780         WARN_ON(irqs_disabled());
781
782         Dprintk("++++++++++++++++++++=_---CPU UP  %u\n", cpu);
783
784         if (apicid == BAD_APICID || apicid == boot_cpu_id ||
785             !physid_isset(apicid, phys_cpu_present_map)) {
786                 printk("__cpu_up: bad cpu %d\n", cpu);
787                 return -EINVAL;
788         }
789
790         /*
791          * Already booted CPU?
792          */
793         if (cpu_isset(cpu, cpu_callin_map)) {
794                 Dprintk("do_boot_cpu %d Already started\n", cpu);
795                 return -ENOSYS;
796         }
797
798         /*
799          * Save current MTRR state in case it was changed since early boot
800          * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
801          */
802         mtrr_save_state();
803
804         per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
805         /* Boot it! */
806         err = do_boot_cpu(cpu, apicid);
807         if (err < 0) {
808                 Dprintk("do_boot_cpu failed %d\n", err);
809                 return err;
810         }
811
812         /* Unleash the CPU! */
813         Dprintk("waiting for cpu %d\n", cpu);
814
815         /*
816          * Make sure and check TSC sync:
817          */
818         local_irq_save(flags);
819         check_tsc_sync_source(cpu);
820         local_irq_restore(flags);
821
822         while (!cpu_isset(cpu, cpu_online_map))
823                 cpu_relax();
824         err = 0;
825
826         return err;
827 }
828
829 /*
830  * Finish the SMP boot.
831  */
832 void __init native_smp_cpus_done(unsigned int max_cpus)
833 {
834         smp_cleanup_boot();
835         setup_ioapic_dest();
836         check_nmi_watchdog();
837 }