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