]> err.no Git - linux-2.6/blob - arch/x86/kernel/smpboot_64.c
x86: integrate do_boot_cpu
[linux-2.6] / arch / x86 / kernel / smpboot_64.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  *      Copyright 2001 Andi Kleen, SuSE Labs.
7  *
8  *      Much of the core SMP work is based on previous work by Thomas Radke, to
9  *      whom a great many thanks are extended.
10  *
11  *      Thanks to Intel for making available several different Pentium,
12  *      Pentium Pro and Pentium-II/Xeon MP machines.
13  *      Original development of Linux SMP code supported by Caldera.
14  *
15  *      This code is released under the GNU General Public License version 2
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 BogoMIP 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  *      Andi Kleen              :       Changed for SMP boot into long mode.
33  *              Rusty Russell   :       Hacked into shape for new "hotplug" boot process.
34  *      Andi Kleen              :       Converted to new state machine.
35  *                                      Various cleanups.
36  *                                      Probably mostly hotplug CPU ready now.
37  *      Ashok Raj                       : CPU hotplug support
38  */
39
40
41 #include <linux/init.h>
42
43 #include <linux/mm.h>
44 #include <linux/kernel_stat.h>
45 #include <linux/bootmem.h>
46 #include <linux/thread_info.h>
47 #include <linux/module.h>
48 #include <linux/delay.h>
49 #include <linux/mc146818rtc.h>
50 #include <linux/smp.h>
51 #include <linux/kdebug.h>
52
53 #include <asm/mtrr.h>
54 #include <asm/pgalloc.h>
55 #include <asm/desc.h>
56 #include <asm/tlbflush.h>
57 #include <asm/proto.h>
58 #include <asm/nmi.h>
59 #include <asm/irq.h>
60 #include <asm/hw_irq.h>
61 #include <asm/numa.h>
62
63 #include <mach_wakecpu.h>
64 #include <mach_apic.h>
65 #include <smpboot_hooks.h>
66 #include <mach_apic.h>
67
68 /* Set when the idlers are all forked */
69 int smp_threads_ready;
70
71 /* State of each CPU */
72 DEFINE_PER_CPU(int, cpu_state) = { 0 };
73
74 extern void smp_callin(void);
75 /*
76  * Setup code on secondary processor (after comming out of the trampoline)
77  */
78 void __cpuinit start_secondary(void)
79 {
80         /*
81          * Dont put anything before smp_callin(), SMP
82          * booting is too fragile that we want to limit the
83          * things done here to the most necessary things.
84          */
85         cpu_init();
86         preempt_disable();
87         smp_callin();
88
89         /* otherwise gcc will move up the smp_processor_id before the cpu_init */
90         barrier();
91
92         /*
93          * Check TSC sync first:
94          */
95         check_tsc_sync_target();
96
97         if (nmi_watchdog == NMI_IO_APIC) {
98                 disable_8259A_irq(0);
99                 enable_NMI_through_LVT0();
100                 enable_8259A_irq(0);
101         }
102
103         /*
104          * The sibling maps must be set before turing the online map on for
105          * this cpu
106          */
107         set_cpu_sibling_map(smp_processor_id());
108
109         /*
110          * We need to hold call_lock, so there is no inconsistency
111          * between the time smp_call_function() determines number of
112          * IPI recipients, and the time when the determination is made
113          * for which cpus receive the IPI in genapic_flat.c. Holding this
114          * lock helps us to not include this cpu in a currently in progress
115          * smp_call_function().
116          */
117         lock_ipi_call_lock();
118         spin_lock(&vector_lock);
119
120         /* Setup the per cpu irq handling data structures */
121         __setup_vector_irq(smp_processor_id());
122         /*
123          * Allow the master to continue.
124          */
125         spin_unlock(&vector_lock);
126         cpu_set(smp_processor_id(), cpu_online_map);
127         unlock_ipi_call_lock();
128
129         per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
130
131         setup_secondary_clock();
132
133         wmb();
134         cpu_idle();
135 }
136
137 cycles_t cacheflush_time;
138 unsigned long cache_decay_ticks;
139
140 /*
141  * Fall back to non SMP mode after errors.
142  *
143  * RED-PEN audit/test this more. I bet there is more state messed up here.
144  */
145 static __init void disable_smp(void)
146 {
147         cpu_present_map = cpumask_of_cpu(0);
148         cpu_possible_map = cpumask_of_cpu(0);
149         if (smp_found_config)
150                 phys_cpu_present_map =
151                                 physid_mask_of_physid(boot_cpu_physical_apicid);
152         else
153                 phys_cpu_present_map = physid_mask_of_physid(0);
154         cpu_set(0, per_cpu(cpu_sibling_map, 0));
155         cpu_set(0, per_cpu(cpu_core_map, 0));
156 }
157
158 /*
159  * Various sanity checks.
160  */
161 static int __init smp_sanity_check(unsigned max_cpus)
162 {
163         if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
164                 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
165                        hard_smp_processor_id());
166                 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
167         }
168
169         /*
170          * If we couldn't find an SMP configuration at boot time,
171          * get out of here now!
172          */
173         if (!smp_found_config) {
174                 printk(KERN_NOTICE "SMP motherboard not detected.\n");
175                 disable_smp();
176                 if (APIC_init_uniprocessor())
177                         printk(KERN_NOTICE "Local APIC not detected."
178                                            " Using dummy APIC emulation.\n");
179                 return -1;
180         }
181
182         /*
183          * Should not be necessary because the MP table should list the boot
184          * CPU too, but we do it for the sake of robustness anyway.
185          */
186         if (!physid_isset(boot_cpu_physical_apicid, phys_cpu_present_map)) {
187                 printk(KERN_NOTICE
188                         "weird, boot CPU (#%d) not listed by the BIOS.\n",
189                         boot_cpu_physical_apicid);
190                 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
191         }
192
193         /*
194          * If we couldn't find a local APIC, then get out of here now!
195          */
196         if (!cpu_has_apic) {
197                 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
198                         boot_cpu_physical_apicid);
199                 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
200                 nr_ioapics = 0;
201                 return -1;
202         }
203
204         /*
205          * If SMP should be disabled, then really disable it!
206          */
207         if (!max_cpus) {
208                 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
209                 nr_ioapics = 0;
210                 return -1;
211         }
212
213         return 0;
214 }
215
216 static void __init smp_cpu_index_default(void)
217 {
218         int i;
219         struct cpuinfo_x86 *c;
220
221         for_each_cpu_mask(i, cpu_possible_map) {
222                 c = &cpu_data(i);
223                 /* mark all to hotplug */
224                 c->cpu_index = NR_CPUS;
225         }
226 }
227
228 /*
229  * Prepare for SMP bootup.  The MP table or ACPI has been read
230  * earlier.  Just do some sanity checking here and enable APIC mode.
231  */
232 void __init native_smp_prepare_cpus(unsigned int max_cpus)
233 {
234         nmi_watchdog_default();
235         smp_cpu_index_default();
236         current_cpu_data = boot_cpu_data;
237         current_thread_info()->cpu = 0;  /* needed? */
238         set_cpu_sibling_map(0);
239
240         if (smp_sanity_check(max_cpus) < 0) {
241                 printk(KERN_INFO "SMP disabled\n");
242                 disable_smp();
243                 return;
244         }
245
246
247         /*
248          * Switch from PIC to APIC mode.
249          */
250         setup_local_APIC();
251
252         /*
253          * Enable IO APIC before setting up error vector
254          */
255         if (!skip_ioapic_setup && nr_ioapics)
256                 enable_IO_APIC();
257         end_local_APIC_setup();
258
259         if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_physical_apicid) {
260                 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
261                      GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_physical_apicid);
262                 /* Or can we switch back to PIC here? */
263         }
264
265         /*
266          * Now start the IO-APICs
267          */
268         if (!skip_ioapic_setup && nr_ioapics)
269                 setup_IO_APIC();
270         else
271                 nr_ioapics = 0;
272
273         /*
274          * Set up local APIC timer on boot CPU.
275          */
276
277         setup_boot_clock();
278         printk(KERN_INFO "CPU%d: ", 0);
279         print_cpu_info(&cpu_data(0));
280 }
281
282 /*
283  * Early setup to make printk work.
284  */
285 void __init native_smp_prepare_boot_cpu(void)
286 {
287         int me = smp_processor_id();
288         /* already set me in cpu_online_map in boot_cpu_init() */
289         cpu_set(me, cpu_callout_map);
290         per_cpu(cpu_state, me) = CPU_ONLINE;
291 }
292
293 extern void impress_friends(void);
294 extern void smp_checks(void);
295
296 /*
297  * Finish the SMP boot.
298  */
299 void __init native_smp_cpus_done(unsigned int max_cpus)
300 {
301         smpboot_restore_warm_reset_vector();
302
303         Dprintk("Boot done.\n");
304
305         impress_friends();
306         smp_checks();
307         setup_ioapic_dest();
308         check_nmi_watchdog();
309 }