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