]> err.no Git - linux-2.6/blob - arch/x86/kernel/smpboot_32.c
x86: make __smp_prepare_cpu void
[linux-2.6] / arch / x86 / kernel / smpboot_32.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  *
7  *      Much of the core SMP work is based on previous work by Thomas Radke, to
8  *      whom a great many thanks are extended.
9  *
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.
13  *
14  *      This code is released under the GNU General Public License version 2 or
15  *      later.
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 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. */
35
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/kernel.h>
39
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>
48
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>
55
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>
61
62 extern int smp_b_stepping;
63
64 static cpumask_t smp_commenced_mask;
65
66 /* which logical CPU number maps to which CPU (physical APIC ID) */
67 u16 x86_cpu_to_apicid_init[NR_CPUS] __initdata =
68                         { [0 ... NR_CPUS-1] = BAD_APICID };
69 void *x86_cpu_to_apicid_early_ptr;
70 DEFINE_PER_CPU(u16, x86_cpu_to_apicid) = BAD_APICID;
71 EXPORT_PER_CPU_SYMBOL(x86_cpu_to_apicid);
72
73 u8 apicid_2_node[MAX_APICID];
74
75 static void map_cpu_to_logical_apicid(void);
76
77 /* State of each CPU. */
78 DEFINE_PER_CPU(int, cpu_state) = { 0 };
79
80 static atomic_t init_deasserted;
81
82 static void __cpuinit smp_callin(void)
83 {
84         int cpuid, phys_id;
85         unsigned long timeout;
86
87         /*
88          * If waken up by an INIT in an 82489DX configuration
89          * we may get here before an INIT-deassert IPI reaches
90          * our local APIC.  We have to wait for the IPI or we'll
91          * lock up on an APIC access.
92          */
93         wait_for_init_deassert(&init_deasserted);
94
95         /*
96          * (This works even if the APIC is not enabled.)
97          */
98         phys_id = GET_APIC_ID(apic_read(APIC_ID));
99         cpuid = smp_processor_id();
100         if (cpu_isset(cpuid, cpu_callin_map)) {
101                 printk("huh, phys CPU#%d, CPU#%d already present??\n",
102                                         phys_id, cpuid);
103                 BUG();
104         }
105         Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
106
107         /*
108          * STARTUP IPIs are fragile beasts as they might sometimes
109          * trigger some glue motherboard logic. Complete APIC bus
110          * silence for 1 second, this overestimates the time the
111          * boot CPU is spending to send the up to 2 STARTUP IPIs
112          * by a factor of two. This should be enough.
113          */
114
115         /*
116          * Waiting 2s total for startup (udelay is not yet working)
117          */
118         timeout = jiffies + 2*HZ;
119         while (time_before(jiffies, timeout)) {
120                 /*
121                  * Has the boot CPU finished it's STARTUP sequence?
122                  */
123                 if (cpu_isset(cpuid, cpu_callout_map))
124                         break;
125                 cpu_relax();
126         }
127
128         if (!time_before(jiffies, timeout)) {
129                 printk("BUG: CPU%d started up but did not get a callout!\n",
130                         cpuid);
131                 BUG();
132         }
133
134         /*
135          * the boot CPU has finished the init stage and is spinning
136          * on callin_map until we finish. We are free to set up this
137          * CPU, first the APIC. (this is probably redundant on most
138          * boards)
139          */
140
141         Dprintk("CALLIN, before setup_local_APIC().\n");
142         smp_callin_clear_local_apic();
143         setup_local_APIC();
144         map_cpu_to_logical_apicid();
145
146         /*
147          * Get our bogomips.
148          */
149         calibrate_delay();
150         Dprintk("Stack at about %p\n",&cpuid);
151
152         /*
153          * Save our processor parameters
154          */
155         smp_store_cpu_info(cpuid);
156
157         /*
158          * Allow the master to continue.
159          */
160         cpu_set(cpuid, cpu_callin_map);
161 }
162
163 static int cpucount;
164
165 /*
166  * Activate a secondary processor.
167  */
168 static void __cpuinit start_secondary(void *unused)
169 {
170         /*
171          * Don't put *anything* before cpu_init(), SMP booting is too
172          * fragile that we want to limit the things done here to the
173          * most necessary things.
174          */
175 #ifdef CONFIG_VMI
176         vmi_bringup();
177 #endif
178         cpu_init();
179         preempt_disable();
180         smp_callin();
181         while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
182                 cpu_relax();
183
184         /* otherwise gcc will move up smp_processor_id before the cpu_init */
185         barrier();
186         /*
187          * Check TSC synchronization with the BP:
188          */
189         check_tsc_sync_target();
190
191         if (nmi_watchdog == NMI_IO_APIC) {
192                 disable_8259A_irq(0);
193                 enable_NMI_through_LVT0();
194                 enable_8259A_irq(0);
195         }
196         /*
197          * low-memory mappings have been cleared, flush them from
198          * the local TLBs too.
199          */
200         local_flush_tlb();
201
202         /* This must be done before setting cpu_online_map */
203         set_cpu_sibling_map(raw_smp_processor_id());
204         wmb();
205
206         /*
207          * We need to hold call_lock, so there is no inconsistency
208          * between the time smp_call_function() determines number of
209          * IPI recipients, and the time when the determination is made
210          * for which cpus receive the IPI. Holding this
211          * lock helps us to not include this cpu in a currently in progress
212          * smp_call_function().
213          */
214         lock_ipi_call_lock();
215         cpu_set(smp_processor_id(), cpu_online_map);
216         unlock_ipi_call_lock();
217         per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
218
219         setup_secondary_clock();
220
221         wmb();
222         cpu_idle();
223 }
224
225 /*
226  * Everything has been set up for the secondary
227  * CPUs - they just need to reload everything
228  * from the task structure
229  * This function must not return.
230  */
231 void __devinit initialize_secondary(void)
232 {
233         /*
234          * We don't actually need to load the full TSS,
235          * basically just the stack pointer and the ip.
236          */
237
238         asm volatile(
239                 "movl %0,%%esp\n\t"
240                 "jmp *%1"
241                 :
242                 :"m" (current->thread.sp),"m" (current->thread.ip));
243 }
244
245 /* Static state in head.S used to set up a CPU */
246 extern struct {
247         void * sp;
248         unsigned short ss;
249 } stack_start;
250
251 #ifdef CONFIG_NUMA
252
253 /* which logical CPUs are on which nodes */
254 cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
255                                 { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
256 EXPORT_SYMBOL(node_to_cpumask_map);
257 /* which node each logical CPU is on */
258 int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
259 EXPORT_SYMBOL(cpu_to_node_map);
260
261 /* set up a mapping between cpu and node. */
262 static inline void map_cpu_to_node(int cpu, int node)
263 {
264         printk("Mapping cpu %d to node %d\n", cpu, node);
265         cpu_set(cpu, node_to_cpumask_map[node]);
266         cpu_to_node_map[cpu] = node;
267 }
268
269 /* undo a mapping between cpu and node. */
270 static inline void unmap_cpu_to_node(int cpu)
271 {
272         int node;
273
274         printk("Unmapping cpu %d from all nodes\n", cpu);
275         for (node = 0; node < MAX_NUMNODES; node ++)
276                 cpu_clear(cpu, node_to_cpumask_map[node]);
277         cpu_to_node_map[cpu] = 0;
278 }
279 #else /* !CONFIG_NUMA */
280
281 #define map_cpu_to_node(cpu, node)      ({})
282 #define unmap_cpu_to_node(cpu)  ({})
283
284 #endif /* CONFIG_NUMA */
285
286 u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
287
288 static void map_cpu_to_logical_apicid(void)
289 {
290         int cpu = smp_processor_id();
291         int apicid = logical_smp_processor_id();
292         int node = apicid_to_node(apicid);
293
294         if (!node_online(node))
295                 node = first_online_node;
296
297         cpu_2_logical_apicid[cpu] = apicid;
298         map_cpu_to_node(cpu, node);
299 }
300
301 static void unmap_cpu_to_logical_apicid(int cpu)
302 {
303         cpu_2_logical_apicid[cpu] = BAD_APICID;
304         unmap_cpu_to_node(cpu);
305 }
306
307 static inline void __inquire_remote_apic(int apicid)
308 {
309         unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
310         char *names[] = { "ID", "VERSION", "SPIV" };
311         int timeout;
312         u32 status;
313
314         printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
315
316         for (i = 0; i < ARRAY_SIZE(regs); i++) {
317                 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
318
319                 /*
320                  * Wait for idle.
321                  */
322                 status = safe_apic_wait_icr_idle();
323                 if (status)
324                         printk(KERN_CONT
325                                "a previous APIC delivery may have failed\n");
326
327                 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
328                 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
329
330                 timeout = 0;
331                 do {
332                         udelay(100);
333                         status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
334                 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
335
336                 switch (status) {
337                 case APIC_ICR_RR_VALID:
338                         status = apic_read(APIC_RRR);
339                         printk(KERN_CONT "%08x\n", status);
340                         break;
341                 default:
342                         printk(KERN_CONT "failed\n");
343                 }
344         }
345 }
346
347 #ifdef WAKE_SECONDARY_VIA_NMI
348 /* 
349  * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
350  * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
351  * won't ... remember to clear down the APIC, etc later.
352  */
353 static int __devinit
354 wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
355 {
356         unsigned long send_status, accept_status = 0;
357         int maxlvt;
358
359         /* Target chip */
360         apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
361
362         /* Boot on the stack */
363         /* Kick the second */
364         apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
365
366         Dprintk("Waiting for send to finish...\n");
367         send_status = safe_apic_wait_icr_idle();
368
369         /*
370          * Give the other CPU some time to accept the IPI.
371          */
372         udelay(200);
373         /*
374          * Due to the Pentium erratum 3AP.
375          */
376         maxlvt = lapic_get_maxlvt();
377         if (maxlvt > 3) {
378                 apic_read_around(APIC_SPIV);
379                 apic_write(APIC_ESR, 0);
380         }
381         accept_status = (apic_read(APIC_ESR) & 0xEF);
382         Dprintk("NMI sent.\n");
383
384         if (send_status)
385                 printk("APIC never delivered???\n");
386         if (accept_status)
387                 printk("APIC delivery error (%lx).\n", accept_status);
388
389         return (send_status | accept_status);
390 }
391 #endif  /* WAKE_SECONDARY_VIA_NMI */
392
393 #ifdef WAKE_SECONDARY_VIA_INIT
394 static int __devinit
395 wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
396 {
397         unsigned long send_status, accept_status = 0;
398         int maxlvt, num_starts, j;
399
400         /*
401          * Be paranoid about clearing APIC errors.
402          */
403         if (APIC_INTEGRATED(apic_version[phys_apicid])) {
404                 apic_read_around(APIC_SPIV);
405                 apic_write(APIC_ESR, 0);
406                 apic_read(APIC_ESR);
407         }
408
409         Dprintk("Asserting INIT.\n");
410
411         /*
412          * Turn INIT on target chip
413          */
414         apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
415
416         /*
417          * Send IPI
418          */
419         apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
420                                 | APIC_DM_INIT);
421
422         Dprintk("Waiting for send to finish...\n");
423         send_status = safe_apic_wait_icr_idle();
424
425         mdelay(10);
426
427         Dprintk("Deasserting INIT.\n");
428
429         /* Target chip */
430         apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
431
432         /* Send IPI */
433         apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
434
435         Dprintk("Waiting for send to finish...\n");
436         send_status = safe_apic_wait_icr_idle();
437
438         mb();
439         atomic_set(&init_deasserted, 1);
440
441         /*
442          * Should we send STARTUP IPIs ?
443          *
444          * Determine this based on the APIC version.
445          * If we don't have an integrated APIC, don't send the STARTUP IPIs.
446          */
447         if (APIC_INTEGRATED(apic_version[phys_apicid]))
448                 num_starts = 2;
449         else
450                 num_starts = 0;
451
452         /*
453          * Paravirt / VMI wants a startup IPI hook here to set up the
454          * target processor state.
455          */
456         startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
457                          (unsigned long) stack_start.sp);
458
459         /*
460          * Run STARTUP IPI loop.
461          */
462         Dprintk("#startup loops: %d.\n", num_starts);
463
464         maxlvt = lapic_get_maxlvt();
465
466         for (j = 1; j <= num_starts; j++) {
467                 Dprintk("Sending STARTUP #%d.\n",j);
468                 apic_read_around(APIC_SPIV);
469                 apic_write(APIC_ESR, 0);
470                 apic_read(APIC_ESR);
471                 Dprintk("After apic_write.\n");
472
473                 /*
474                  * STARTUP IPI
475                  */
476
477                 /* Target chip */
478                 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
479
480                 /* Boot on the stack */
481                 /* Kick the second */
482                 apic_write_around(APIC_ICR, APIC_DM_STARTUP
483                                         | (start_eip >> 12));
484
485                 /*
486                  * Give the other CPU some time to accept the IPI.
487                  */
488                 udelay(300);
489
490                 Dprintk("Startup point 1.\n");
491
492                 Dprintk("Waiting for send to finish...\n");
493                 send_status = safe_apic_wait_icr_idle();
494
495                 /*
496                  * Give the other CPU some time to accept the IPI.
497                  */
498                 udelay(200);
499                 /*
500                  * Due to the Pentium erratum 3AP.
501                  */
502                 if (maxlvt > 3) {
503                         apic_read_around(APIC_SPIV);
504                         apic_write(APIC_ESR, 0);
505                 }
506                 accept_status = (apic_read(APIC_ESR) & 0xEF);
507                 if (send_status || accept_status)
508                         break;
509         }
510         Dprintk("After Startup.\n");
511
512         if (send_status)
513                 printk("APIC never delivered???\n");
514         if (accept_status)
515                 printk("APIC delivery error (%lx).\n", accept_status);
516
517         return (send_status | accept_status);
518 }
519 #endif  /* WAKE_SECONDARY_VIA_INIT */
520
521 extern cpumask_t cpu_initialized;
522 static inline int alloc_cpu_id(void)
523 {
524         cpumask_t       tmp_map;
525         int cpu;
526         cpus_complement(tmp_map, cpu_present_map);
527         cpu = first_cpu(tmp_map);
528         if (cpu >= NR_CPUS)
529                 return -ENODEV;
530         return cpu;
531 }
532
533 #ifdef CONFIG_HOTPLUG_CPU
534 static struct task_struct * __cpuinitdata cpu_idle_tasks[NR_CPUS];
535 static inline struct task_struct * __cpuinit alloc_idle_task(int cpu)
536 {
537         struct task_struct *idle;
538
539         if ((idle = cpu_idle_tasks[cpu]) != NULL) {
540                 /* initialize thread_struct.  we really want to avoid destroy
541                  * idle tread
542                  */
543                 idle->thread.sp = (unsigned long)task_pt_regs(idle);
544                 init_idle(idle, cpu);
545                 return idle;
546         }
547         idle = fork_idle(cpu);
548
549         if (!IS_ERR(idle))
550                 cpu_idle_tasks[cpu] = idle;
551         return idle;
552 }
553 #else
554 #define alloc_idle_task(cpu) fork_idle(cpu)
555 #endif
556
557 static int __cpuinit do_boot_cpu(int apicid, int cpu)
558 /*
559  * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
560  * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
561  * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
562  */
563 {
564         struct task_struct *idle;
565         unsigned long boot_error;
566         int timeout;
567         unsigned long start_eip;
568         unsigned short nmi_high = 0, nmi_low = 0;
569
570         /*
571          * Save current MTRR state in case it was changed since early boot
572          * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
573          */
574         mtrr_save_state();
575
576         /*
577          * We can't use kernel_thread since we must avoid to
578          * reschedule the child.
579          */
580         idle = alloc_idle_task(cpu);
581         if (IS_ERR(idle))
582                 panic("failed fork for CPU %d", cpu);
583
584         init_gdt(cpu);
585         per_cpu(current_task, cpu) = idle;
586         early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
587
588         idle->thread.ip = (unsigned long) start_secondary;
589         /* start_eip had better be page-aligned! */
590         start_eip = setup_trampoline();
591
592         ++cpucount;
593         alternatives_smp_switch(1);
594
595         /* So we see what's up   */
596         printk("Booting processor %d/%d ip %lx\n", cpu, apicid, start_eip);
597         /* Stack for startup_32 can be just as for start_secondary onwards */
598         stack_start.sp = (void *) idle->thread.sp;
599
600         irq_ctx_init(cpu);
601
602         per_cpu(x86_cpu_to_apicid, cpu) = apicid;
603         /*
604          * This grunge runs the startup process for
605          * the targeted processor.
606          */
607
608         atomic_set(&init_deasserted, 0);
609
610         Dprintk("Setting warm reset code and vector.\n");
611
612         store_NMI_vector(&nmi_high, &nmi_low);
613
614         smpboot_setup_warm_reset_vector(start_eip);
615
616         /*
617          * Starting actual IPI sequence...
618          */
619         boot_error = wakeup_secondary_cpu(apicid, start_eip);
620
621         if (!boot_error) {
622                 /*
623                  * allow APs to start initializing.
624                  */
625                 Dprintk("Before Callout %d.\n", cpu);
626                 cpu_set(cpu, cpu_callout_map);
627                 Dprintk("After Callout %d.\n", cpu);
628
629                 /*
630                  * Wait 5s total for a response
631                  */
632                 for (timeout = 0; timeout < 50000; timeout++) {
633                         if (cpu_isset(cpu, cpu_callin_map))
634                                 break;  /* It has booted */
635                         udelay(100);
636                 }
637
638                 if (cpu_isset(cpu, cpu_callin_map)) {
639                         /* number CPUs logically, starting from 1 (BSP is 0) */
640                         Dprintk("OK.\n");
641                         printk("CPU%d: ", cpu);
642                         print_cpu_info(&cpu_data(cpu));
643                         Dprintk("CPU has booted.\n");
644                 } else {
645                         boot_error= 1;
646                         if (*((volatile unsigned char *)trampoline_base)
647                                         == 0xA5)
648                                 /* trampoline started but...? */
649                                 printk("Stuck ??\n");
650                         else
651                                 /* trampoline code not run */
652                                 printk("Not responding.\n");
653                         inquire_remote_apic(apicid);
654                 }
655         }
656
657         if (boot_error) {
658                 /* Try to put things back the way they were before ... */
659                 unmap_cpu_to_logical_apicid(cpu);
660                 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
661                 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
662                 cpucount--;
663         } else {
664                 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
665                 cpu_set(cpu, cpu_present_map);
666         }
667
668         /* mark "stuck" area as not stuck */
669         *((volatile unsigned long *)trampoline_base) = 0;
670
671         return boot_error;
672 }
673
674 #ifdef CONFIG_HOTPLUG_CPU
675 void cpu_exit_clear(void)
676 {
677         int cpu = raw_smp_processor_id();
678
679         idle_task_exit();
680
681         cpucount --;
682         cpu_uninit();
683         irq_ctx_exit(cpu);
684
685         cpu_clear(cpu, cpu_callout_map);
686         cpu_clear(cpu, cpu_callin_map);
687
688         cpu_clear(cpu, smp_commenced_mask);
689         unmap_cpu_to_logical_apicid(cpu);
690 }
691
692 struct warm_boot_cpu_info {
693         struct completion *complete;
694         struct work_struct task;
695         int apicid;
696         int cpu;
697 };
698
699 static void __cpuinit do_warm_boot_cpu(struct work_struct *work)
700 {
701         struct warm_boot_cpu_info *info =
702                 container_of(work, struct warm_boot_cpu_info, task);
703         do_boot_cpu(info->apicid, info->cpu);
704         complete(info->complete);
705 }
706
707 static void __cpuinit __smp_prepare_cpu(int cpu)
708 {
709         DECLARE_COMPLETION_ONSTACK(done);
710         struct warm_boot_cpu_info info;
711         int     apicid;
712
713         apicid = per_cpu(x86_cpu_to_apicid, cpu);
714
715         info.complete = &done;
716         info.apicid = apicid;
717         info.cpu = cpu;
718         INIT_WORK(&info.task, do_warm_boot_cpu);
719
720         /* init low mem mapping */
721         clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
722                         min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
723         flush_tlb_all();
724         schedule_work(&info.task);
725         wait_for_completion(&done);
726
727         zap_low_mappings();
728 }
729 #endif
730
731 static int boot_cpu_logical_apicid;
732 /* Where the IO area was mapped on multiquad, always 0 otherwise */
733 void *xquad_portio;
734 #ifdef CONFIG_X86_NUMAQ
735 EXPORT_SYMBOL(xquad_portio);
736 #endif
737
738 static void __init disable_smp(void)
739 {
740         smpboot_clear_io_apic_irqs();
741         phys_cpu_present_map = physid_mask_of_physid(0);
742         map_cpu_to_logical_apicid();
743         cpu_set(0, per_cpu(cpu_sibling_map, 0));
744         cpu_set(0, per_cpu(cpu_core_map, 0));
745 }
746
747 static int __init smp_sanity_check(unsigned max_cpus)
748 {
749         /*
750          * If we couldn't find an SMP configuration at boot time,
751          * get out of here now!
752          */
753         if (!smp_found_config && !acpi_lapic) {
754                 printk(KERN_NOTICE "SMP motherboard not detected.\n");
755                 disable_smp();
756                 if (APIC_init_uniprocessor())
757                         printk(KERN_NOTICE "Local APIC not detected."
758                                            " Using dummy APIC emulation.\n");
759                 return -1;
760         }
761
762         /*
763          * Should not be necessary because the MP table should list the boot
764          * CPU too, but we do it for the sake of robustness anyway.
765          * Makes no sense to do this check in clustered apic mode, so skip it
766          */
767         if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
768                 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
769                                 boot_cpu_physical_apicid);
770                 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
771         }
772
773         /*
774          * If we couldn't find a local APIC, then get out of here now!
775          */
776         if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
777                 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
778                         boot_cpu_physical_apicid);
779                 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
780                 return -1;
781         }
782
783         verify_local_APIC();
784
785         /*
786          * If SMP should be disabled, then really disable it!
787          */
788         if (!max_cpus) {
789                 smp_found_config = 0;
790                 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
791
792                 if (nmi_watchdog == NMI_LOCAL_APIC) {
793                         printk(KERN_INFO "activating minimal APIC for NMI watchdog use.\n");
794                         connect_bsp_APIC();
795                         setup_local_APIC();
796                 }
797                 return -1;
798         }
799         return 0;
800 }
801
802
803 /*
804  * Cycle through the processors sending APIC IPIs to boot each.
805  */
806 static void __init smp_boot_cpus(unsigned int max_cpus)
807 {
808         int apicid, cpu, bit, kicked;
809         unsigned long bogosum = 0;
810
811         /*
812          * Setup boot CPU information
813          */
814         smp_store_cpu_info(0); /* Final full version of the data */
815         printk(KERN_INFO "CPU%d: ", 0);
816         print_cpu_info(&cpu_data(0));
817
818         boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
819         boot_cpu_logical_apicid = logical_smp_processor_id();
820         per_cpu(x86_cpu_to_apicid, 0) = boot_cpu_physical_apicid;
821
822         current_thread_info()->cpu = 0;
823
824         set_cpu_sibling_map(0);
825
826         if (smp_sanity_check(max_cpus) < 0) {
827                 printk(KERN_INFO "SMP disabled\n");
828                 disable_smp();
829                 return;
830         }
831
832         connect_bsp_APIC();
833         setup_local_APIC();
834         map_cpu_to_logical_apicid();
835
836
837         setup_portio_remap();
838
839         /*
840          * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
841          *
842          * In clustered apic mode, phys_cpu_present_map is a constructed thus:
843          * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the 
844          * clustered apic ID.
845          */
846         Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
847
848         kicked = 1;
849         for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
850                 apicid = cpu_present_to_apicid(bit);
851                 /*
852                  * Don't even attempt to start the boot CPU!
853                  */
854                 if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
855                         continue;
856
857                 if (!check_apicid_present(bit))
858                         continue;
859                 if (max_cpus <= cpucount+1)
860                         continue;
861
862                 if (((cpu = alloc_cpu_id()) <= 0) || do_boot_cpu(apicid, cpu))
863                         printk("CPU #%d not responding - cannot use it.\n",
864                                                                 apicid);
865                 else
866                         ++kicked;
867         }
868
869         /*
870          * Cleanup possible dangling ends...
871          */
872         smpboot_restore_warm_reset_vector();
873
874         /*
875          * Allow the user to impress friends.
876          */
877         Dprintk("Before bogomips.\n");
878         for_each_possible_cpu(cpu)
879                 if (cpu_isset(cpu, cpu_callout_map))
880                         bogosum += cpu_data(cpu).loops_per_jiffy;
881         printk(KERN_INFO
882                 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
883                 cpucount+1,
884                 bogosum/(500000/HZ),
885                 (bogosum/(5000/HZ))%100);
886         
887         Dprintk("Before bogocount - setting activated=1.\n");
888
889         if (smp_b_stepping)
890                 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
891
892         /*
893          * Don't taint if we are running SMP kernel on a single non-MP
894          * approved Athlon
895          */
896         if (tainted & TAINT_UNSAFE_SMP) {
897                 if (cpucount)
898                         printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
899                 else
900                         tainted &= ~TAINT_UNSAFE_SMP;
901         }
902
903         Dprintk("Boot done.\n");
904
905         /*
906          * construct cpu_sibling_map, so that we can tell sibling CPUs
907          * efficiently.
908          */
909         for_each_possible_cpu(cpu) {
910                 cpus_clear(per_cpu(cpu_sibling_map, cpu));
911                 cpus_clear(per_cpu(cpu_core_map, cpu));
912         }
913
914         cpu_set(0, per_cpu(cpu_sibling_map, 0));
915         cpu_set(0, per_cpu(cpu_core_map, 0));
916
917         smpboot_setup_io_apic();
918
919         setup_boot_clock();
920 }
921
922 /* These are wrappers to interface to the new boot process.  Someone
923    who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
924 void __init native_smp_prepare_cpus(unsigned int max_cpus)
925 {
926         smp_commenced_mask = cpumask_of_cpu(0);
927         cpu_callin_map = cpumask_of_cpu(0);
928         mb();
929         smp_boot_cpus(max_cpus);
930 }
931
932 void __init native_smp_prepare_boot_cpu(void)
933 {
934         unsigned int cpu = smp_processor_id();
935
936         init_gdt(cpu);
937         switch_to_new_gdt();
938
939         cpu_set(cpu, cpu_online_map);
940         cpu_set(cpu, cpu_callout_map);
941         cpu_set(cpu, cpu_present_map);
942         cpu_set(cpu, cpu_possible_map);
943         __get_cpu_var(cpu_state) = CPU_ONLINE;
944 }
945
946 int __cpuinit native_cpu_up(unsigned int cpu)
947 {
948         int apicid = cpu_present_to_apicid(cpu);
949         unsigned long flags;
950
951         WARN_ON(irqs_disabled());
952
953         Dprintk("++++++++++++++++++++=_---CPU UP  %u\n", cpu);
954
955         if (apicid == BAD_APICID || apicid == boot_cpu_physical_apicid ||
956             !physid_isset(apicid, phys_cpu_present_map)) {
957                 printk(KERN_ERR "%s: bad cpu %d\n", __func__, cpu);
958                 return -EINVAL;
959         }
960
961 #ifdef CONFIG_HOTPLUG_CPU
962
963         /*
964          * We do warm boot only on cpus that had booted earlier
965          * Otherwise cold boot is all handled from smp_boot_cpus().
966          * cpu_callin_map is set during AP kickstart process. Its reset
967          * when a cpu is taken offline from cpu_exit_clear().
968          */
969         if (!cpu_isset(cpu, cpu_callin_map))
970                 __smp_prepare_cpu(cpu);
971 #endif
972
973         /* In case one didn't come up */
974         if (!cpu_isset(cpu, cpu_callin_map)) {
975                 printk(KERN_DEBUG "skipping cpu%d, didn't come online\n", cpu);
976                 return -EIO;
977         }
978
979         per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
980         /* Unleash the CPU! */
981         cpu_set(cpu, smp_commenced_mask);
982
983         /*
984          * Check TSC synchronization with the AP (keep irqs disabled
985          * while doing so):
986          */
987         local_irq_save(flags);
988         check_tsc_sync_source(cpu);
989         local_irq_restore(flags);
990
991         while (!cpu_isset(cpu, cpu_online_map)) {
992                 cpu_relax();
993                 touch_nmi_watchdog();
994         }
995
996         return 0;
997 }
998
999 void __init native_smp_cpus_done(unsigned int max_cpus)
1000 {
1001 #ifdef CONFIG_X86_IO_APIC
1002         setup_ioapic_dest();
1003 #endif
1004         zap_low_mappings();
1005 }
1006
1007 void __init smp_intr_init(void)
1008 {
1009         /*
1010          * IRQ0 must be given a fixed assignment and initialized,
1011          * because it's used before the IO-APIC is set up.
1012          */
1013         set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1014
1015         /*
1016          * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1017          * IPI, driven by wakeup.
1018          */
1019         set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1020
1021         /* IPI for invalidation */
1022         set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1023
1024         /* IPI for generic function call */
1025         set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1026 }