blob: cc64b8085c2aec9f0196dd83320b1f11db0d2afa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Andi Kleena8ab26f2005-04-16 15:25:19 -070015 * This code is released under the GNU General Public License version 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
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.
Andi Kleena8ab26f2005-04-16 15:25:19 -070033 * 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.
Ashok Raj76e4f662005-06-25 14:55:00 -070037 * Ashok Raj : CPU hotplug support
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
Andi Kleena8ab26f2005-04-16 15:25:19 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/init.h>
42
43#include <linux/mm.h>
44#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/bootmem.h>
46#include <linux/thread_info.h>
47#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/delay.h>
49#include <linux/mc146818rtc.h>
Andrew Mortona3bc0db2006-09-25 23:32:33 -070050#include <linux/smp.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070051#include <linux/kdebug.h>
Andrew Mortona3bc0db2006-09-25 23:32:33 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/mtrr.h>
54#include <asm/pgalloc.h>
55#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/tlbflush.h>
57#include <asm/proto.h>
Andi Kleen75152112005-05-16 21:53:34 -070058#include <asm/nmi.h>
Al Viro9cdd3042005-09-12 18:49:25 +020059#include <asm/irq.h>
60#include <asm/hw_irq.h>
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -080061#include <asm/numa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* Number of siblings per CPU package */
64int smp_num_siblings = 1;
Andi Kleen2ee60e172006-06-26 13:59:44 +020065EXPORT_SYMBOL(smp_num_siblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -080067/* Last level cache ID of each logical CPU */
travis@sgi.comef970012008-01-30 13:33:10 +010068DEFINE_PER_CPU(u16, cpu_llc_id) = BAD_APICID;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -080069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* Bitmask of currently online CPUs */
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070071cpumask_t cpu_online_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Andi Kleena8ab26f2005-04-16 15:25:19 -070073EXPORT_SYMBOL(cpu_online_map);
74
75/*
76 * Private maps to synchronize booting between AP and BP.
77 * Probably not needed anymore, but it makes for easier debugging. -AK
78 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079cpumask_t cpu_callin_map;
80cpumask_t cpu_callout_map;
Andi Kleena8ab26f2005-04-16 15:25:19 -070081cpumask_t cpu_possible_map;
82EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* Per CPU bogomips and other parameters */
Mike Travis92cb7612007-10-19 20:35:04 +020085DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
86EXPORT_PER_CPU_SYMBOL(cpu_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Andi Kleena8ab26f2005-04-16 15:25:19 -070088/* Set when the idlers are all forked */
89int smp_threads_ready;
90
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010091/* representing HT siblings of each logical CPU */
Mike Travisd5a74302007-10-16 01:24:05 -070092DEFINE_PER_CPU(cpumask_t, cpu_sibling_map);
93EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010094
95/* representing HT and core siblings of each logical CPU */
Mike Travis08357612007-10-16 01:24:04 -070096DEFINE_PER_CPU(cpumask_t, cpu_core_map);
97EXPORT_PER_CPU_SYMBOL(cpu_core_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
100 * Trampoline 80x86 program as an array.
101 */
102
Jan Beulich121d7bf2007-10-17 18:04:37 +0200103extern const unsigned char trampoline_data[];
104extern const unsigned char trampoline_end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Ashok Raj76e4f662005-06-25 14:55:00 -0700106/* State of each CPU */
107DEFINE_PER_CPU(int, cpu_state) = { 0 };
108
109/*
110 * Store all idle threads, this can be reused instead of creating
111 * a new thread. Also avoids complicated thread destroy functionality
112 * for idle threads.
113 */
travis@sgi.com24b0d222008-01-30 13:33:11 +0100114#ifdef CONFIG_HOTPLUG_CPU
115/*
116 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
117 * removed after init for !CONFIG_HOTPLUG_CPU.
118 */
119static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
120#define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
121#define set_idle_for_cpu(x,p) (per_cpu(idle_thread_array, x) = (p))
122#else
Ashok Raj76e4f662005-06-25 14:55:00 -0700123struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
Ashok Raj76e4f662005-06-25 14:55:00 -0700124#define get_idle_for_cpu(x) (idle_thread_array[(x)])
125#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
travis@sgi.com24b0d222008-01-30 13:33:11 +0100126#endif
127
Ashok Raj76e4f662005-06-25 14:55:00 -0700128
129/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * Currently trivial. Write the real->protected mode
131 * bootstrap into the page concerned. The caller
132 * has made sure it's suitably aligned.
133 */
134
Andi Kleena8ab26f2005-04-16 15:25:19 -0700135static unsigned long __cpuinit setup_trampoline(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 void *tramp = __va(SMP_TRAMPOLINE_BASE);
138 memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
139 return virt_to_phys(tramp);
140}
141
142/*
143 * The bootstrap kernel entry code has set these up. Save them for
144 * a given CPU
145 */
146
Andi Kleena8ab26f2005-04-16 15:25:19 -0700147static void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Mike Travis92cb7612007-10-19 20:35:04 +0200149 struct cpuinfo_x86 *c = &cpu_data(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 *c = boot_cpu_data;
Mike Travisfbdcf182007-12-19 23:20:19 +0100152 c->cpu_index = id;
Linus Torvalds00684412007-12-25 20:16:16 -0800153 identify_cpu(c);
Andi Kleendda50e72005-05-16 21:53:25 -0700154 print_cpu_info(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Andi Kleena8ab26f2005-04-16 15:25:19 -0700157static atomic_t init_deasserted __cpuinitdata;
158
159/*
160 * Report back to the Boot Processor.
161 * Running on AP.
162 */
163void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int cpuid, phys_id;
166 unsigned long timeout;
167
168 /*
169 * If waken up by an INIT in an 82489DX configuration
170 * we may get here before an INIT-deassert IPI reaches
171 * our local APIC. We have to wait for the IPI or we'll
172 * lock up on an APIC access.
173 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700174 while (!atomic_read(&init_deasserted))
175 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /*
178 * (This works even if the APIC is not enabled.)
179 */
180 phys_id = GET_APIC_ID(apic_read(APIC_ID));
181 cpuid = smp_processor_id();
182 if (cpu_isset(cpuid, cpu_callin_map)) {
183 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
184 phys_id, cpuid);
185 }
186 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
187
188 /*
189 * STARTUP IPIs are fragile beasts as they might sometimes
190 * trigger some glue motherboard logic. Complete APIC bus
191 * silence for 1 second, this overestimates the time the
192 * boot CPU is spending to send the up to 2 STARTUP IPIs
193 * by a factor of two. This should be enough.
194 */
195
196 /*
197 * Waiting 2s total for startup (udelay is not yet working)
198 */
199 timeout = jiffies + 2*HZ;
200 while (time_before(jiffies, timeout)) {
201 /*
202 * Has the boot CPU finished it's STARTUP sequence?
203 */
204 if (cpu_isset(cpuid, cpu_callout_map))
205 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700206 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 if (!time_before(jiffies, timeout)) {
210 panic("smp_callin: CPU%d started up but did not get a callout!\n",
211 cpuid);
212 }
213
214 /*
215 * the boot CPU has finished the init stage and is spinning
216 * on callin_map until we finish. We are free to set up this
217 * CPU, first the APIC. (this is probably redundant on most
218 * boards)
219 */
220
221 Dprintk("CALLIN, before setup_local_APIC().\n");
222 setup_local_APIC();
Andi Kleen739f33b2008-01-30 13:30:40 +0100223 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /*
226 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200227 *
228 * Need to enable IRQs because it can take longer and then
229 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Andi Kleenb4452212005-09-12 18:49:24 +0200231 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200233 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 Dprintk("Stack at about %p\n",&cpuid);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /*
237 * Save our processor parameters
238 */
239 smp_store_cpu_info(cpuid);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /*
242 * Allow the master to continue.
243 */
244 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800247/* maps the cpu to the sched domain representing multi-core */
248cpumask_t cpu_coregroup_map(int cpu)
249{
Mike Travis92cb7612007-10-19 20:35:04 +0200250 struct cpuinfo_x86 *c = &cpu_data(cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800251 /*
252 * For perf, we return last level cache shared map.
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700253 * And for power savings, we return cpu_core_map
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800254 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700255 if (sched_mc_power_savings || sched_smt_power_savings)
Mike Travis08357612007-10-16 01:24:04 -0700256 return per_cpu(cpu_core_map, cpu);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700257 else
258 return c->llc_shared_map;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800259}
260
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100261/* representing cpus for which sibling maps can be computed */
262static cpumask_t cpu_sibling_setup_map;
263
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700264static inline void set_cpu_sibling_map(int cpu)
265{
266 int i;
Mike Travis92cb7612007-10-19 20:35:04 +0200267 struct cpuinfo_x86 *c = &cpu_data(cpu);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100268
269 cpu_set(cpu, cpu_sibling_setup_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700270
271 if (smp_num_siblings > 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100272 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Mike Travis92cb7612007-10-19 20:35:04 +0200273 if (c->phys_proc_id == cpu_data(i).phys_proc_id &&
274 c->cpu_core_id == cpu_data(i).cpu_core_id) {
Mike Travisd5a74302007-10-16 01:24:05 -0700275 cpu_set(i, per_cpu(cpu_sibling_map, cpu));
276 cpu_set(cpu, per_cpu(cpu_sibling_map, i));
Mike Travis08357612007-10-16 01:24:04 -0700277 cpu_set(i, per_cpu(cpu_core_map, cpu));
278 cpu_set(cpu, per_cpu(cpu_core_map, i));
Mike Travis92cb7612007-10-19 20:35:04 +0200279 cpu_set(i, c->llc_shared_map);
280 cpu_set(cpu, cpu_data(i).llc_shared_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700281 }
282 }
283 } else {
Mike Travisd5a74302007-10-16 01:24:05 -0700284 cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700285 }
286
Mike Travis92cb7612007-10-19 20:35:04 +0200287 cpu_set(cpu, c->llc_shared_map);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800288
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100289 if (current_cpu_data.x86_max_cores == 1) {
Mike Travisd5a74302007-10-16 01:24:05 -0700290 per_cpu(cpu_core_map, cpu) = per_cpu(cpu_sibling_map, cpu);
Mike Travis92cb7612007-10-19 20:35:04 +0200291 c->booted_cores = 1;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100292 return;
293 }
294
295 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Mike Travisb6278472007-10-19 20:35:03 +0200296 if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
297 per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
Mike Travis92cb7612007-10-19 20:35:04 +0200298 cpu_set(i, c->llc_shared_map);
299 cpu_set(cpu, cpu_data(i).llc_shared_map);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800300 }
Mike Travis92cb7612007-10-19 20:35:04 +0200301 if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
Mike Travis08357612007-10-16 01:24:04 -0700302 cpu_set(i, per_cpu(cpu_core_map, cpu));
303 cpu_set(cpu, per_cpu(cpu_core_map, i));
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100304 /*
305 * Does this new cpu bringup a new core?
306 */
Mike Travisd5a74302007-10-16 01:24:05 -0700307 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100308 /*
309 * for each core in package, increment
310 * the booted_cores for this new cpu
311 */
Mike Travisd5a74302007-10-16 01:24:05 -0700312 if (first_cpu(per_cpu(cpu_sibling_map, i)) == i)
Mike Travis92cb7612007-10-19 20:35:04 +0200313 c->booted_cores++;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100314 /*
315 * increment the core count for all
316 * the other cpus in this package
317 */
318 if (i != cpu)
Mike Travis92cb7612007-10-19 20:35:04 +0200319 cpu_data(i).booted_cores++;
320 } else if (i != cpu && !c->booted_cores)
321 c->booted_cores = cpu_data(i).booted_cores;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100322 }
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700323 }
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700327 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700329void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 /*
332 * Dont put anything before smp_callin(), SMP
333 * booting is too fragile that we want to limit the
334 * things done here to the most necessary things.
335 */
336 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800337 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 smp_callin();
339
340 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
341 barrier();
342
Ingo Molnar95492e42007-02-16 01:27:34 -0800343 /*
344 * Check TSC sync first:
345 */
346 check_tsc_sync_target();
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (nmi_watchdog == NMI_IO_APIC) {
349 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100350 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 enable_8259A_irq(0);
352 }
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700355 * The sibling maps must be set before turing the online map on for
356 * this cpu
357 */
358 set_cpu_sibling_map(smp_processor_id());
359
360 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700361 * We need to hold call_lock, so there is no inconsistency
362 * between the time smp_call_function() determines number of
Simon Arlott676b1852007-10-20 01:25:36 +0200363 * IPI recipients, and the time when the determination is made
Ashok Raj884d9e42005-06-25 14:55:02 -0700364 * for which cpus receive the IPI in genapic_flat.c. Holding this
365 * lock helps us to not include this cpu in a currently in progress
366 * smp_call_function().
367 */
368 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200369 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700370
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200371 /* Setup the per cpu irq handling data structures */
372 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700373 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700374 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700377 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200378 spin_unlock(&vector_lock);
Ingo Molnar95492e42007-02-16 01:27:34 -0800379
Ashok Raj884d9e42005-06-25 14:55:02 -0700380 unlock_ipi_call_lock();
381
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100382 setup_secondary_clock();
Thomas Gleixner3ac508b2007-10-14 22:57:45 +0200383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 cpu_idle();
385}
386
Andi Kleena8ab26f2005-04-16 15:25:19 -0700387extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388extern void (*initial_code)(void);
389
Olaf Hering44456d32005-07-27 11:45:17 -0700390#ifdef APIC_DEBUG
Andi Kleena8ab26f2005-04-16 15:25:19 -0700391static void inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
394 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200395 int timeout;
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100396 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
399
Alejandro Martinez Ruiz4d022ad2007-10-17 14:38:58 +0200400 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100401 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
404 * Wait for idle.
405 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200406 status = safe_apic_wait_icr_idle();
407 if (status)
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100408 printk(KERN_CONT
409 "a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Andi Kleenc1507eb2005-09-12 18:49:23 +0200411 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
412 apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 timeout = 0;
415 do {
416 udelay(100);
417 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
418 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
419
420 switch (status) {
421 case APIC_ICR_RR_VALID:
422 status = apic_read(APIC_RRR);
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100423 printk(KERN_CONT "%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break;
425 default:
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100426 printk(KERN_CONT "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 }
429}
430#endif
431
Andi Kleena8ab26f2005-04-16 15:25:19 -0700432/*
433 * Kick the secondary to wake up.
434 */
435static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200437 unsigned long send_status, accept_status = 0;
438 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 Dprintk("Asserting INIT.\n");
441
442 /*
443 * Turn INIT on target chip
444 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200445 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /*
448 * Send IPI
449 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200450 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 | APIC_DM_INIT);
452
453 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200454 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 mdelay(10);
457
458 Dprintk("Deasserting INIT.\n");
459
460 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200461 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* Send IPI */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200464 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200467 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100469 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 atomic_set(&init_deasserted, 1);
471
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200472 num_starts = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /*
475 * Run STARTUP IPI loop.
476 */
477 Dprintk("#startup loops: %d.\n", num_starts);
478
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100479 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 for (j = 1; j <= num_starts; j++) {
482 Dprintk("Sending STARTUP #%d.\n",j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 apic_write(APIC_ESR, 0);
484 apic_read(APIC_ESR);
485 Dprintk("After apic_write.\n");
486
487 /*
488 * STARTUP IPI
489 */
490
491 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200492 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Boot on the stack */
495 /* Kick the second */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200496 apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
499 * Give the other CPU some time to accept the IPI.
500 */
501 udelay(300);
502
503 Dprintk("Startup point 1.\n");
504
505 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200506 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /*
509 * Give the other CPU some time to accept the IPI.
510 */
511 udelay(200);
512 /*
513 * Due to the Pentium erratum 3AP.
514 */
515 if (maxlvt > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 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(KERN_ERR "APIC never delivered???\n");
526 if (accept_status)
527 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
528
529 return (send_status | accept_status);
530}
531
Ashok Raj76e4f662005-06-25 14:55:00 -0700532struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000533 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700534 struct task_struct *idle;
535 struct completion done;
536 int cpu;
537};
538
Thomas Gleixnera2b484a2008-01-09 00:18:28 +0100539static void __cpuinit do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700540{
David Howells65f27f32006-11-22 14:55:48 +0000541 struct create_idle *c_idle =
542 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700543
544 c_idle->idle = fork_idle(c_idle->cpu);
545 complete(&c_idle->done);
546}
547
Andi Kleena8ab26f2005-04-16 15:25:19 -0700548/*
549 * Boot one CPU.
550 */
551static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700554 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700556 struct create_idle c_idle = {
David Howells65f27f32006-11-22 14:55:48 +0000557 .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
Ashok Raj76e4f662005-06-25 14:55:00 -0700558 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700559 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700560 };
Ashok Raj76e4f662005-06-25 14:55:00 -0700561
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100562 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
563 if (!cpu_gdt_descr[cpu].address &&
564 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
565 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
566 return -1;
567 }
568
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100569 /* Allocate node local memory for AP pdas */
570 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
571 struct x8664_pda *newpda, *pda;
572 int node = cpu_to_node(cpu);
573 pda = cpu_pda(cpu);
574 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
575 node);
576 if (newpda) {
577 memcpy(newpda, pda, sizeof (struct x8664_pda));
578 cpu_pda(cpu) = newpda;
579 } else
580 printk(KERN_ERR
581 "Could not allocate node local PDA for CPU %d on node %d\n",
582 cpu, node);
583 }
584
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200585 alternatives_smp_switch(1);
586
Ashok Raj76e4f662005-06-25 14:55:00 -0700587 c_idle.idle = get_idle_for_cpu(cpu);
588
589 if (c_idle.idle) {
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100590 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800591 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700592 init_idle(c_idle.idle, cpu);
593 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Ashok Raj76e4f662005-06-25 14:55:00 -0700596 /*
597 * During cold boot process, keventd thread is not spun up yet.
598 * When we do cpu hot-add, we create idle threads on the fly, we should
599 * not acquire any attributes from the calling context. Hence the clean
600 * way to create kernel_threads() is to do that from keventd().
601 * We do the current_is_keventd() due to the fact that ACPI notifier
602 * was also queuing to keventd() and when the caller is already running
603 * in context of keventd(), we would end up with locking up the keventd
604 * thread.
605 */
606 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000607 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700608 else {
David Howells65f27f32006-11-22 14:55:48 +0000609 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700610 wait_for_completion(&c_idle.done);
611 }
612
613 if (IS_ERR(c_idle.idle)) {
614 printk("failed fork for CPU %d\n", cpu);
615 return PTR_ERR(c_idle.idle);
616 }
617
618 set_idle_for_cpu(cpu, c_idle.idle);
619
620do_rest:
621
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100622 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 start_rip = setup_trampoline();
625
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100626 init_rsp = c_idle.idle->thread.sp;
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100627 load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800629 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Andi Kleende04f322005-07-28 21:15:29 -0700631 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
632 cpus_weight(cpu_present_map),
633 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /*
636 * This grunge runs the startup process for
637 * the targeted processor.
638 */
639
640 atomic_set(&init_deasserted, 0);
641
642 Dprintk("Setting warm reset code and vector.\n");
643
644 CMOS_WRITE(0xa, 0xf);
645 local_flush_tlb();
646 Dprintk("1.\n");
647 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
648 Dprintk("2.\n");
649 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
650 Dprintk("3.\n");
651
652 /*
653 * Be paranoid about clearing APIC errors.
654 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100655 apic_write(APIC_ESR, 0);
656 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /*
659 * Status is now clean
660 */
661 boot_error = 0;
662
663 /*
664 * Starting actual IPI sequence...
665 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700666 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 if (!boot_error) {
669 /*
670 * allow APs to start initializing.
671 */
672 Dprintk("Before Callout %d.\n", cpu);
673 cpu_set(cpu, cpu_callout_map);
674 Dprintk("After Callout %d.\n", cpu);
675
676 /*
677 * Wait 5s total for a response
678 */
679 for (timeout = 0; timeout < 50000; timeout++) {
680 if (cpu_isset(cpu, cpu_callin_map))
681 break; /* It has booted */
682 udelay(100);
683 }
684
685 if (cpu_isset(cpu, cpu_callin_map)) {
686 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 Dprintk("CPU has booted.\n");
688 } else {
689 boot_error = 1;
690 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
691 == 0xA5)
692 /* trampoline started but...? */
693 printk("Stuck ??\n");
694 else
695 /* trampoline code not run */
696 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700697#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 inquire_remote_apic(apicid);
699#endif
700 }
701 }
702 if (boot_error) {
703 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
Jeremy Fitzhardinge5548fec2008-01-30 13:30:55 +0100704 clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800705 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700706 cpu_clear(cpu, cpu_present_map);
707 cpu_clear(cpu, cpu_possible_map);
Mike Travis71fff5e2007-10-19 20:35:03 +0200708 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700709 return -EIO;
710 }
711
712 return 0;
713}
714
715cycles_t cacheflush_time;
716unsigned long cache_decay_ticks;
717
718/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700719 * Cleanup possible dangling ends...
720 */
721static __cpuinit void smp_cleanup_boot(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700724 * Paranoid: Set warm reset code and vector here back
725 * to default values.
726 */
727 CMOS_WRITE(0, 0xf);
728
729 /*
730 * Reset trampoline flag
731 */
732 *((volatile int *) phys_to_virt(0x467)) = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700733}
734
735/*
736 * Fall back to non SMP mode after errors.
737 *
738 * RED-PEN audit/test this more. I bet there is more state messed up here.
739 */
Ashok Raje6982c62005-06-25 14:54:58 -0700740static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700741{
742 cpu_present_map = cpumask_of_cpu(0);
743 cpu_possible_map = cpumask_of_cpu(0);
744 if (smp_found_config)
745 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
746 else
747 phys_cpu_present_map = physid_mask_of_physid(0);
Mike Travisd5a74302007-10-16 01:24:05 -0700748 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700749 cpu_set(0, per_cpu(cpu_core_map, 0));
Andi Kleena8ab26f2005-04-16 15:25:19 -0700750}
751
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700752#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen420f8f62005-11-05 17:25:54 +0100753
754int additional_cpus __initdata = -1;
755
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700756/*
757 * cpu_possible_map should be static, it cannot change as cpu's
758 * are onlined, or offlined. The reason is per-cpu data-structures
759 * are allocated by some modules at init time, and dont expect to
760 * do this dynamically on cpu arrival/departure.
761 * cpu_present_map on the other hand can change dynamically.
762 * In case when cpu_hotplug is not compiled, then we resort to current
763 * behaviour, which is cpu_possible == cpu_present.
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700764 * - Ashok Raj
Andi Kleen420f8f62005-11-05 17:25:54 +0100765 *
766 * Three ways to find out the number of additional hotplug CPUs:
767 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Andi Kleen420f8f62005-11-05 17:25:54 +0100768 * - The user can overwrite it with additional_cpus=NUM
Andi Kleenf62a91f2006-01-11 22:42:35 +0100769 * - Otherwise don't reserve additional CPUs.
Andi Kleen420f8f62005-11-05 17:25:54 +0100770 * We do this because additional CPUs waste a lot of memory.
771 * -AK
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700772 */
Andi Kleen421c7ce2005-10-10 22:32:45 +0200773__init void prefill_possible_map(void)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700774{
775 int i;
Andi Kleen420f8f62005-11-05 17:25:54 +0100776 int possible;
777
778 if (additional_cpus == -1) {
Andi Kleenf62a91f2006-01-11 22:42:35 +0100779 if (disabled_cpus > 0)
Andi Kleen420f8f62005-11-05 17:25:54 +0100780 additional_cpus = disabled_cpus;
Andi Kleenf62a91f2006-01-11 22:42:35 +0100781 else
782 additional_cpus = 0;
Andi Kleen420f8f62005-11-05 17:25:54 +0100783 }
784 possible = num_processors + additional_cpus;
785 if (possible > NR_CPUS)
786 possible = NR_CPUS;
787
788 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
789 possible,
790 max_t(int, possible - num_processors, 0));
791
792 for (i = 0; i < possible; i++)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700793 cpu_set(i, cpu_possible_map);
794}
795#endif
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700798 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
Ashok Raje6982c62005-06-25 14:54:58 -0700800static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
803 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
804 hard_smp_processor_id());
805 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
806 }
807
808 /*
809 * If we couldn't find an SMP configuration at boot time,
810 * get out of here now!
811 */
812 if (!smp_found_config) {
813 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700814 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (APIC_init_uniprocessor())
816 printk(KERN_NOTICE "Local APIC not detected."
817 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700818 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
821 /*
822 * Should not be necessary because the MP table should list the boot
823 * CPU too, but we do it for the sake of robustness anyway.
824 */
825 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
826 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
827 boot_cpu_id);
828 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
829 }
830
831 /*
832 * If we couldn't find a local APIC, then get out of here now!
833 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100834 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
836 boot_cpu_id);
837 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700838 nr_ioapics = 0;
839 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /*
843 * If SMP should be disabled, then really disable it!
844 */
845 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700847 nr_ioapics = 0;
848 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
Andi Kleena8ab26f2005-04-16 15:25:19 -0700851 return 0;
852}
853
Yinghai Lu949ec322008-01-30 13:30:46 +0100854static void __init smp_cpu_index_default(void)
855{
856 int i;
857 struct cpuinfo_x86 *c;
858
859 for_each_cpu_mask(i, cpu_possible_map) {
860 c = &cpu_data(i);
861 /* mark all to hotplug */
862 c->cpu_index = NR_CPUS;
863 }
864}
865
Mike Travis71fff5e2007-10-19 20:35:03 +0200866/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700867 * Prepare for SMP bootup. The MP table or ACPI has been read
868 * earlier. Just do some sanity checking here and enable APIC mode.
869 */
Ashok Raje6982c62005-06-25 14:54:58 -0700870void __init smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700871{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700872 nmi_watchdog_default();
Yinghai Lu949ec322008-01-30 13:30:46 +0100873 smp_cpu_index_default();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700874 current_cpu_data = boot_cpu_data;
875 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100876 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700877
Andi Kleena8ab26f2005-04-16 15:25:19 -0700878 if (smp_sanity_check(max_cpus) < 0) {
879 printk(KERN_INFO "SMP disabled\n");
880 disable_smp();
881 return;
882 }
883
884
885 /*
886 * Switch from PIC to APIC mode.
887 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 setup_local_APIC();
889
Andi Kleen739f33b2008-01-30 13:30:40 +0100890 /*
891 * Enable IO APIC before setting up error vector
892 */
893 if (!skip_ioapic_setup && nr_ioapics)
894 enable_IO_APIC();
895 end_local_APIC_setup();
896
Andi Kleena8ab26f2005-04-16 15:25:19 -0700897 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
898 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
899 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
900 /* Or can we switch back to PIC here? */
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700904 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 */
906 if (!skip_ioapic_setup && nr_ioapics)
907 setup_IO_APIC();
908 else
909 nr_ioapics = 0;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700912 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100915 setup_boot_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Andi Kleena8ab26f2005-04-16 15:25:19 -0700918/*
919 * Early setup to make printk work.
920 */
921void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700923 int me = smp_processor_id();
Yinghai Lu23916d42008-01-30 13:33:39 +0100924 /* already set me in cpu_online_map in boot_cpu_init() */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700925 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700926 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Andi Kleena8ab26f2005-04-16 15:25:19 -0700929/*
930 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700931 */
932int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700934 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100935 unsigned long flags;
936 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Andi Kleena8ab26f2005-04-16 15:25:19 -0700938 WARN_ON(irqs_disabled());
939
940 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
941
942 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
943 !physid_isset(apicid, phys_cpu_present_map)) {
944 printk("__cpu_up: bad cpu %d\n", cpu);
945 return -EINVAL;
946 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700947
Ashok Raj76e4f662005-06-25 14:55:00 -0700948 /*
949 * Already booted CPU?
950 */
951 if (cpu_isset(cpu, cpu_callin_map)) {
952 Dprintk("do_boot_cpu %d Already started\n", cpu);
953 return -ENOSYS;
954 }
955
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200956 /*
957 * Save current MTRR state in case it was changed since early boot
958 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
959 */
960 mtrr_save_state();
961
Ashok Raj884d9e42005-06-25 14:55:02 -0700962 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700963 /* Boot it! */
964 err = do_boot_cpu(cpu, apicid);
965 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700966 Dprintk("do_boot_cpu failed %d\n", err);
967 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* Unleash the CPU! */
971 Dprintk("waiting for cpu %d\n", cpu);
972
Ingo Molnar95492e42007-02-16 01:27:34 -0800973 /*
974 * Make sure and check TSC sync:
975 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100976 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800977 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100978 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700981 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700982 err = 0;
983
984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Andi Kleena8ab26f2005-04-16 15:25:19 -0700987/*
988 * Finish the SMP boot.
989 */
Ashok Raje6982c62005-06-25 14:54:58 -0700990void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700992 smp_cleanup_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700994 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700995}
Ashok Raj76e4f662005-06-25 14:55:00 -0700996
997#ifdef CONFIG_HOTPLUG_CPU
998
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700999static void remove_siblinginfo(int cpu)
Ashok Raj76e4f662005-06-25 14:55:00 -07001000{
1001 int sibling;
Mike Travis92cb7612007-10-19 20:35:04 +02001002 struct cpuinfo_x86 *c = &cpu_data(cpu);
Ashok Raj76e4f662005-06-25 14:55:00 -07001003
Mike Travis08357612007-10-16 01:24:04 -07001004 for_each_cpu_mask(sibling, per_cpu(cpu_core_map, cpu)) {
1005 cpu_clear(cpu, per_cpu(cpu_core_map, sibling));
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001006 /*
1007 * last thread sibling in this cpu core going down
1008 */
Mike Travisd5a74302007-10-16 01:24:05 -07001009 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1)
Mike Travis92cb7612007-10-19 20:35:04 +02001010 cpu_data(sibling).booted_cores--;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001011 }
1012
Mike Travisd5a74302007-10-16 01:24:05 -07001013 for_each_cpu_mask(sibling, per_cpu(cpu_sibling_map, cpu))
1014 cpu_clear(cpu, per_cpu(cpu_sibling_map, sibling));
1015 cpus_clear(per_cpu(cpu_sibling_map, cpu));
Mike Travis08357612007-10-16 01:24:04 -07001016 cpus_clear(per_cpu(cpu_core_map, cpu));
Mike Travis92cb7612007-10-19 20:35:04 +02001017 c->phys_proc_id = 0;
1018 c->cpu_core_id = 0;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001019 cpu_clear(cpu, cpu_sibling_setup_map);
Ashok Raj76e4f662005-06-25 14:55:00 -07001020}
1021
1022void remove_cpu_from_maps(void)
1023{
1024 int cpu = smp_processor_id();
1025
1026 cpu_clear(cpu, cpu_callout_map);
1027 cpu_clear(cpu, cpu_callin_map);
Jeremy Fitzhardinge5548fec2008-01-30 13:30:55 +01001028 clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -08001029 clear_node_cpumask(cpu);
Ashok Raj76e4f662005-06-25 14:55:00 -07001030}
1031
1032int __cpu_disable(void)
1033{
1034 int cpu = smp_processor_id();
1035
1036 /*
1037 * Perhaps use cpufreq to drop frequency, but that could go
1038 * into generic code.
1039 *
1040 * We won't take down the boot processor on i386 due to some
1041 * interrupts only being able to be serviced by the BSP.
1042 * Especially so if we're not using an IOAPIC -zwane
1043 */
1044 if (cpu == 0)
1045 return -EBUSY;
1046
Shaohua Li4038f902006-09-26 10:52:27 +02001047 if (nmi_watchdog == NMI_LOCAL_APIC)
1048 stop_apic_nmi_watchdog(NULL);
Shaohua Li5e9ef022005-12-12 22:17:08 -08001049 clear_local_APIC();
Ashok Raj76e4f662005-06-25 14:55:00 -07001050
1051 /*
1052 * HACK:
1053 * Allow any queued timer interrupts to get serviced
1054 * This is only a temporary solution until we cleanup
1055 * fixup_irqs as we do for IA64.
1056 */
1057 local_irq_enable();
1058 mdelay(1);
1059
1060 local_irq_disable();
1061 remove_siblinginfo(cpu);
1062
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001063 spin_lock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001064 /* It's now safe to remove this processor from the online map */
1065 cpu_clear(cpu, cpu_online_map);
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001066 spin_unlock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001067 remove_cpu_from_maps();
1068 fixup_irqs(cpu_online_map);
1069 return 0;
1070}
1071
1072void __cpu_die(unsigned int cpu)
1073{
1074 /* We don't do anything here: idle task is faking death itself. */
1075 unsigned int i;
1076
1077 for (i = 0; i < 10; i++) {
1078 /* They ack this in play_dead by setting CPU_DEAD */
Ashok Raj884d9e42005-06-25 14:55:02 -07001079 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1080 printk ("CPU %d is now offline\n", cpu);
Gerd Hoffmannd167a512006-06-26 13:56:16 +02001081 if (1 == num_online_cpus())
1082 alternatives_smp_switch(0);
Ashok Raj76e4f662005-06-25 14:55:00 -07001083 return;
Ashok Raj884d9e42005-06-25 14:55:02 -07001084 }
Nishanth Aravamudanef6e5252005-07-28 21:15:53 -07001085 msleep(100);
Ashok Raj76e4f662005-06-25 14:55:00 -07001086 }
1087 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
1088}
1089
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001090static __init int setup_additional_cpus(char *s)
Andi Kleen420f8f62005-11-05 17:25:54 +01001091{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001092 return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
Andi Kleen420f8f62005-11-05 17:25:54 +01001093}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001094early_param("additional_cpus", setup_additional_cpus);
Andi Kleen420f8f62005-11-05 17:25:54 +01001095
Ashok Raj76e4f662005-06-25 14:55:00 -07001096#else /* ... !CONFIG_HOTPLUG_CPU */
1097
1098int __cpu_disable(void)
1099{
1100 return -ENOSYS;
1101}
1102
1103void __cpu_die(unsigned int cpu)
1104{
1105 /* We said "no" in __cpu_disable */
1106 BUG();
1107}
1108#endif /* CONFIG_HOTPLUG_CPU */