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