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