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