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