blob: e5f08de9db889b4fa0d9e2afcd94c9623f0f1924 [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 */
68u8 cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
69
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 Kleen2ee60e172006-06-26 13:59:44 +020081EXPORT_SYMBOL(cpu_callout_map);
Andi Kleena8ab26f2005-04-16 15:25:19 -070082
83cpumask_t cpu_possible_map;
84EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* Per CPU bogomips and other parameters */
87struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
Andi Kleen2ee60e172006-06-26 13:59:44 +020088EXPORT_SYMBOL(cpu_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Andi Kleena8ab26f2005-04-16 15:25:19 -070090/* Set when the idlers are all forked */
91int smp_threads_ready;
92
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010093/* representing HT siblings of each logical CPU */
Mike Travisd5a74302007-10-16 01:24:05 -070094DEFINE_PER_CPU(cpumask_t, cpu_sibling_map);
95EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010096
97/* representing HT and core siblings of each logical CPU */
Mike Travis08357612007-10-16 01:24:04 -070098DEFINE_PER_CPU(cpumask_t, cpu_core_map);
99EXPORT_PER_CPU_SYMBOL(cpu_core_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
102 * Trampoline 80x86 program as an array.
103 */
104
Jan Beulich121d7bf2007-10-17 18:04:37 +0200105extern const unsigned char trampoline_data[];
106extern const unsigned char trampoline_end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Ashok Raj76e4f662005-06-25 14:55:00 -0700108/* State of each CPU */
109DEFINE_PER_CPU(int, cpu_state) = { 0 };
110
111/*
112 * Store all idle threads, this can be reused instead of creating
113 * a new thread. Also avoids complicated thread destroy functionality
114 * for idle threads.
115 */
116struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
117
118#define get_idle_for_cpu(x) (idle_thread_array[(x)])
119#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
120
121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * Currently trivial. Write the real->protected mode
123 * bootstrap into the page concerned. The caller
124 * has made sure it's suitably aligned.
125 */
126
Andi Kleena8ab26f2005-04-16 15:25:19 -0700127static unsigned long __cpuinit setup_trampoline(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 void *tramp = __va(SMP_TRAMPOLINE_BASE);
130 memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
131 return virt_to_phys(tramp);
132}
133
134/*
135 * The bootstrap kernel entry code has set these up. Save them for
136 * a given CPU
137 */
138
Andi Kleena8ab26f2005-04-16 15:25:19 -0700139static void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct cpuinfo_x86 *c = cpu_data + id;
142
143 *c = boot_cpu_data;
144 identify_cpu(c);
Andi Kleendda50e72005-05-16 21:53:25 -0700145 print_cpu_info(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Andi Kleena8ab26f2005-04-16 15:25:19 -0700148static atomic_t init_deasserted __cpuinitdata;
149
150/*
151 * Report back to the Boot Processor.
152 * Running on AP.
153 */
154void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 int cpuid, phys_id;
157 unsigned long timeout;
158
159 /*
160 * If waken up by an INIT in an 82489DX configuration
161 * we may get here before an INIT-deassert IPI reaches
162 * our local APIC. We have to wait for the IPI or we'll
163 * lock up on an APIC access.
164 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700165 while (!atomic_read(&init_deasserted))
166 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
169 * (This works even if the APIC is not enabled.)
170 */
171 phys_id = GET_APIC_ID(apic_read(APIC_ID));
172 cpuid = smp_processor_id();
173 if (cpu_isset(cpuid, cpu_callin_map)) {
174 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
175 phys_id, cpuid);
176 }
177 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
178
179 /*
180 * STARTUP IPIs are fragile beasts as they might sometimes
181 * trigger some glue motherboard logic. Complete APIC bus
182 * silence for 1 second, this overestimates the time the
183 * boot CPU is spending to send the up to 2 STARTUP IPIs
184 * by a factor of two. This should be enough.
185 */
186
187 /*
188 * Waiting 2s total for startup (udelay is not yet working)
189 */
190 timeout = jiffies + 2*HZ;
191 while (time_before(jiffies, timeout)) {
192 /*
193 * Has the boot CPU finished it's STARTUP sequence?
194 */
195 if (cpu_isset(cpuid, cpu_callout_map))
196 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700197 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
200 if (!time_before(jiffies, timeout)) {
201 panic("smp_callin: CPU%d started up but did not get a callout!\n",
202 cpuid);
203 }
204
205 /*
206 * the boot CPU has finished the init stage and is spinning
207 * on callin_map until we finish. We are free to set up this
208 * CPU, first the APIC. (this is probably redundant on most
209 * boards)
210 */
211
212 Dprintk("CALLIN, before setup_local_APIC().\n");
213 setup_local_APIC();
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /*
216 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200217 *
218 * Need to enable IRQs because it can take longer and then
219 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
Andi Kleenb4452212005-09-12 18:49:24 +0200221 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200223 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 Dprintk("Stack at about %p\n",&cpuid);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /*
227 * Save our processor parameters
228 */
229 smp_store_cpu_info(cpuid);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /*
232 * Allow the master to continue.
233 */
234 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800237/* maps the cpu to the sched domain representing multi-core */
238cpumask_t cpu_coregroup_map(int cpu)
239{
240 struct cpuinfo_x86 *c = cpu_data + cpu;
241 /*
242 * For perf, we return last level cache shared map.
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700243 * And for power savings, we return cpu_core_map
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800244 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700245 if (sched_mc_power_savings || sched_smt_power_savings)
Mike Travis08357612007-10-16 01:24:04 -0700246 return per_cpu(cpu_core_map, cpu);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700247 else
248 return c->llc_shared_map;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800249}
250
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100251/* representing cpus for which sibling maps can be computed */
252static cpumask_t cpu_sibling_setup_map;
253
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700254static inline void set_cpu_sibling_map(int cpu)
255{
256 int i;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100257 struct cpuinfo_x86 *c = cpu_data;
258
259 cpu_set(cpu, cpu_sibling_setup_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700260
261 if (smp_num_siblings > 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100262 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Rohit Sethf3fa8eb2006-06-26 13:58:17 +0200263 if (c[cpu].phys_proc_id == c[i].phys_proc_id &&
264 c[cpu].cpu_core_id == c[i].cpu_core_id) {
Mike Travisd5a74302007-10-16 01:24:05 -0700265 cpu_set(i, per_cpu(cpu_sibling_map, cpu));
266 cpu_set(cpu, per_cpu(cpu_sibling_map, i));
Mike Travis08357612007-10-16 01:24:04 -0700267 cpu_set(i, per_cpu(cpu_core_map, cpu));
268 cpu_set(cpu, per_cpu(cpu_core_map, i));
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800269 cpu_set(i, c[cpu].llc_shared_map);
270 cpu_set(cpu, c[i].llc_shared_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700271 }
272 }
273 } else {
Mike Travisd5a74302007-10-16 01:24:05 -0700274 cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700275 }
276
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800277 cpu_set(cpu, c[cpu].llc_shared_map);
278
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100279 if (current_cpu_data.x86_max_cores == 1) {
Mike Travisd5a74302007-10-16 01:24:05 -0700280 per_cpu(cpu_core_map, cpu) = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100281 c[cpu].booted_cores = 1;
282 return;
283 }
284
285 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800286 if (cpu_llc_id[cpu] != BAD_APICID &&
287 cpu_llc_id[cpu] == cpu_llc_id[i]) {
288 cpu_set(i, c[cpu].llc_shared_map);
289 cpu_set(cpu, c[i].llc_shared_map);
290 }
Rohit Sethf3fa8eb2006-06-26 13:58:17 +0200291 if (c[cpu].phys_proc_id == c[i].phys_proc_id) {
Mike Travis08357612007-10-16 01:24:04 -0700292 cpu_set(i, per_cpu(cpu_core_map, cpu));
293 cpu_set(cpu, per_cpu(cpu_core_map, i));
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100294 /*
295 * Does this new cpu bringup a new core?
296 */
Mike Travisd5a74302007-10-16 01:24:05 -0700297 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100298 /*
299 * for each core in package, increment
300 * the booted_cores for this new cpu
301 */
Mike Travisd5a74302007-10-16 01:24:05 -0700302 if (first_cpu(per_cpu(cpu_sibling_map, i)) == i)
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100303 c[cpu].booted_cores++;
304 /*
305 * increment the core count for all
306 * the other cpus in this package
307 */
308 if (i != cpu)
309 c[i].booted_cores++;
310 } else if (i != cpu && !c[cpu].booted_cores)
311 c[cpu].booted_cores = c[i].booted_cores;
312 }
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700313 }
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700317 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700319void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 /*
322 * Dont put anything before smp_callin(), SMP
323 * booting is too fragile that we want to limit the
324 * things done here to the most necessary things.
325 */
326 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800327 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 smp_callin();
329
330 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
331 barrier();
332
Ingo Molnar95492e42007-02-16 01:27:34 -0800333 /*
334 * Check TSC sync first:
335 */
336 check_tsc_sync_target();
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (nmi_watchdog == NMI_IO_APIC) {
339 disable_8259A_irq(0);
340 enable_NMI_through_LVT0(NULL);
341 enable_8259A_irq(0);
342 }
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700345 * The sibling maps must be set before turing the online map on for
346 * this cpu
347 */
348 set_cpu_sibling_map(smp_processor_id());
349
350 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700351 * We need to hold call_lock, so there is no inconsistency
352 * between the time smp_call_function() determines number of
353 * IPI receipients, and the time when the determination is made
354 * for which cpus receive the IPI in genapic_flat.c. Holding this
355 * lock helps us to not include this cpu in a currently in progress
356 * smp_call_function().
357 */
358 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200359 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700360
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200361 /* Setup the per cpu irq handling data structures */
362 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700363 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700364 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700367 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200368 spin_unlock(&vector_lock);
Ingo Molnar95492e42007-02-16 01:27:34 -0800369
Ashok Raj884d9e42005-06-25 14:55:02 -0700370 unlock_ipi_call_lock();
371
Thomas Gleixner3ac508b2007-10-14 22:57:45 +0200372 setup_secondary_APIC_clock();
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 cpu_idle();
375}
376
Andi Kleena8ab26f2005-04-16 15:25:19 -0700377extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378extern void (*initial_code)(void);
379
Olaf Hering44456d32005-07-27 11:45:17 -0700380#ifdef APIC_DEBUG
Andi Kleena8ab26f2005-04-16 15:25:19 -0700381static void inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
384 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200385 int timeout;
386 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
389
390 for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
391 printk("... APIC #%d %s: ", apicid, names[i]);
392
393 /*
394 * Wait for idle.
395 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200396 status = safe_apic_wait_icr_idle();
397 if (status)
398 printk("a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Andi Kleenc1507eb2005-09-12 18:49:23 +0200400 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
401 apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 timeout = 0;
404 do {
405 udelay(100);
406 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
407 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
408
409 switch (status) {
410 case APIC_ICR_RR_VALID:
411 status = apic_read(APIC_RRR);
412 printk("%08x\n", status);
413 break;
414 default:
415 printk("failed\n");
416 }
417 }
418}
419#endif
420
Andi Kleena8ab26f2005-04-16 15:25:19 -0700421/*
422 * Kick the secondary to wake up.
423 */
424static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200426 unsigned long send_status, accept_status = 0;
427 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 Dprintk("Asserting INIT.\n");
430
431 /*
432 * Turn INIT on target chip
433 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200434 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * Send IPI
438 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200439 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 | APIC_DM_INIT);
441
442 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200443 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 mdelay(10);
446
447 Dprintk("Deasserting INIT.\n");
448
449 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200450 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* Send IPI */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200453 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200456 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100458 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 atomic_set(&init_deasserted, 1);
460
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200461 num_starts = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /*
464 * Run STARTUP IPI loop.
465 */
466 Dprintk("#startup loops: %d.\n", num_starts);
467
468 maxlvt = get_maxlvt();
469
470 for (j = 1; j <= num_starts; j++) {
471 Dprintk("Sending STARTUP #%d.\n",j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 apic_write(APIC_ESR, 0);
473 apic_read(APIC_ESR);
474 Dprintk("After apic_write.\n");
475
476 /*
477 * STARTUP IPI
478 */
479
480 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200481 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* Boot on the stack */
484 /* Kick the second */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200485 apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
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");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200495 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 apic_write(APIC_ESR, 0);
506 }
507 accept_status = (apic_read(APIC_ESR) & 0xEF);
508 if (send_status || accept_status)
509 break;
510 }
511 Dprintk("After Startup.\n");
512
513 if (send_status)
514 printk(KERN_ERR "APIC never delivered???\n");
515 if (accept_status)
516 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
517
518 return (send_status | accept_status);
519}
520
Ashok Raj76e4f662005-06-25 14:55:00 -0700521struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000522 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700523 struct task_struct *idle;
524 struct completion done;
525 int cpu;
526};
527
David Howells65f27f32006-11-22 14:55:48 +0000528void do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700529{
David Howells65f27f32006-11-22 14:55:48 +0000530 struct create_idle *c_idle =
531 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700532
533 c_idle->idle = fork_idle(c_idle->cpu);
534 complete(&c_idle->done);
535}
536
Andi Kleena8ab26f2005-04-16 15:25:19 -0700537/*
538 * Boot one CPU.
539 */
540static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700543 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700545 struct create_idle c_idle = {
David Howells65f27f32006-11-22 14:55:48 +0000546 .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
Ashok Raj76e4f662005-06-25 14:55:00 -0700547 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700548 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700549 };
Ashok Raj76e4f662005-06-25 14:55:00 -0700550
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100551 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
552 if (!cpu_gdt_descr[cpu].address &&
553 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
554 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
555 return -1;
556 }
557
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100558 /* Allocate node local memory for AP pdas */
559 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
560 struct x8664_pda *newpda, *pda;
561 int node = cpu_to_node(cpu);
562 pda = cpu_pda(cpu);
563 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
564 node);
565 if (newpda) {
566 memcpy(newpda, pda, sizeof (struct x8664_pda));
567 cpu_pda(cpu) = newpda;
568 } else
569 printk(KERN_ERR
570 "Could not allocate node local PDA for CPU %d on node %d\n",
571 cpu, node);
572 }
573
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200574 alternatives_smp_switch(1);
575
Ashok Raj76e4f662005-06-25 14:55:00 -0700576 c_idle.idle = get_idle_for_cpu(cpu);
577
578 if (c_idle.idle) {
579 c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800580 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700581 init_idle(c_idle.idle, cpu);
582 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Ashok Raj76e4f662005-06-25 14:55:00 -0700585 /*
586 * During cold boot process, keventd thread is not spun up yet.
587 * When we do cpu hot-add, we create idle threads on the fly, we should
588 * not acquire any attributes from the calling context. Hence the clean
589 * way to create kernel_threads() is to do that from keventd().
590 * We do the current_is_keventd() due to the fact that ACPI notifier
591 * was also queuing to keventd() and when the caller is already running
592 * in context of keventd(), we would end up with locking up the keventd
593 * thread.
594 */
595 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000596 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700597 else {
David Howells65f27f32006-11-22 14:55:48 +0000598 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700599 wait_for_completion(&c_idle.done);
600 }
601
602 if (IS_ERR(c_idle.idle)) {
603 printk("failed fork for CPU %d\n", cpu);
604 return PTR_ERR(c_idle.idle);
605 }
606
607 set_idle_for_cpu(cpu, c_idle.idle);
608
609do_rest:
610
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100611 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 start_rip = setup_trampoline();
614
Ashok Raj76e4f662005-06-25 14:55:00 -0700615 init_rsp = c_idle.idle->thread.rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 per_cpu(init_tss,cpu).rsp0 = init_rsp;
617 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800618 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Andi Kleende04f322005-07-28 21:15:29 -0700620 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
621 cpus_weight(cpu_present_map),
622 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /*
625 * This grunge runs the startup process for
626 * the targeted processor.
627 */
628
629 atomic_set(&init_deasserted, 0);
630
631 Dprintk("Setting warm reset code and vector.\n");
632
633 CMOS_WRITE(0xa, 0xf);
634 local_flush_tlb();
635 Dprintk("1.\n");
636 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
637 Dprintk("2.\n");
638 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
639 Dprintk("3.\n");
640
641 /*
642 * Be paranoid about clearing APIC errors.
643 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100644 apic_write(APIC_ESR, 0);
645 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /*
648 * Status is now clean
649 */
650 boot_error = 0;
651
652 /*
653 * Starting actual IPI sequence...
654 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700655 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 if (!boot_error) {
658 /*
659 * allow APs to start initializing.
660 */
661 Dprintk("Before Callout %d.\n", cpu);
662 cpu_set(cpu, cpu_callout_map);
663 Dprintk("After Callout %d.\n", cpu);
664
665 /*
666 * Wait 5s total for a response
667 */
668 for (timeout = 0; timeout < 50000; timeout++) {
669 if (cpu_isset(cpu, cpu_callin_map))
670 break; /* It has booted */
671 udelay(100);
672 }
673
674 if (cpu_isset(cpu, cpu_callin_map)) {
675 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 Dprintk("CPU has booted.\n");
677 } else {
678 boot_error = 1;
679 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
680 == 0xA5)
681 /* trampoline started but...? */
682 printk("Stuck ??\n");
683 else
684 /* trampoline code not run */
685 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700686#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 inquire_remote_apic(apicid);
688#endif
689 }
690 }
691 if (boot_error) {
692 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
693 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800694 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700695 cpu_clear(cpu, cpu_present_map);
696 cpu_clear(cpu, cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 x86_cpu_to_apicid[cpu] = BAD_APICID;
698 x86_cpu_to_log_apicid[cpu] = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700699 return -EIO;
700 }
701
702 return 0;
703}
704
705cycles_t cacheflush_time;
706unsigned long cache_decay_ticks;
707
708/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700709 * Cleanup possible dangling ends...
710 */
711static __cpuinit void smp_cleanup_boot(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700714 * Paranoid: Set warm reset code and vector here back
715 * to default values.
716 */
717 CMOS_WRITE(0, 0xf);
718
719 /*
720 * Reset trampoline flag
721 */
722 *((volatile int *) phys_to_virt(0x467)) = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700723}
724
725/*
726 * Fall back to non SMP mode after errors.
727 *
728 * RED-PEN audit/test this more. I bet there is more state messed up here.
729 */
Ashok Raje6982c62005-06-25 14:54:58 -0700730static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700731{
732 cpu_present_map = cpumask_of_cpu(0);
733 cpu_possible_map = cpumask_of_cpu(0);
734 if (smp_found_config)
735 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
736 else
737 phys_cpu_present_map = physid_mask_of_physid(0);
Mike Travisd5a74302007-10-16 01:24:05 -0700738 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700739 cpu_set(0, per_cpu(cpu_core_map, 0));
Andi Kleena8ab26f2005-04-16 15:25:19 -0700740}
741
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700742#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen420f8f62005-11-05 17:25:54 +0100743
744int additional_cpus __initdata = -1;
745
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700746/*
747 * cpu_possible_map should be static, it cannot change as cpu's
748 * are onlined, or offlined. The reason is per-cpu data-structures
749 * are allocated by some modules at init time, and dont expect to
750 * do this dynamically on cpu arrival/departure.
751 * cpu_present_map on the other hand can change dynamically.
752 * In case when cpu_hotplug is not compiled, then we resort to current
753 * behaviour, which is cpu_possible == cpu_present.
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700754 * - Ashok Raj
Andi Kleen420f8f62005-11-05 17:25:54 +0100755 *
756 * Three ways to find out the number of additional hotplug CPUs:
757 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Andi Kleen420f8f62005-11-05 17:25:54 +0100758 * - The user can overwrite it with additional_cpus=NUM
Andi Kleenf62a91f2006-01-11 22:42:35 +0100759 * - Otherwise don't reserve additional CPUs.
Andi Kleen420f8f62005-11-05 17:25:54 +0100760 * We do this because additional CPUs waste a lot of memory.
761 * -AK
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700762 */
Andi Kleen421c7ce2005-10-10 22:32:45 +0200763__init void prefill_possible_map(void)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700764{
765 int i;
Andi Kleen420f8f62005-11-05 17:25:54 +0100766 int possible;
767
768 if (additional_cpus == -1) {
Andi Kleenf62a91f2006-01-11 22:42:35 +0100769 if (disabled_cpus > 0)
Andi Kleen420f8f62005-11-05 17:25:54 +0100770 additional_cpus = disabled_cpus;
Andi Kleenf62a91f2006-01-11 22:42:35 +0100771 else
772 additional_cpus = 0;
Andi Kleen420f8f62005-11-05 17:25:54 +0100773 }
774 possible = num_processors + additional_cpus;
775 if (possible > NR_CPUS)
776 possible = NR_CPUS;
777
778 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
779 possible,
780 max_t(int, possible - num_processors, 0));
781
782 for (i = 0; i < possible; i++)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700783 cpu_set(i, cpu_possible_map);
784}
785#endif
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700788 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Ashok Raje6982c62005-06-25 14:54:58 -0700790static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
793 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
794 hard_smp_processor_id());
795 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
796 }
797
798 /*
799 * If we couldn't find an SMP configuration at boot time,
800 * get out of here now!
801 */
802 if (!smp_found_config) {
803 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700804 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (APIC_init_uniprocessor())
806 printk(KERN_NOTICE "Local APIC not detected."
807 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700808 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 /*
812 * Should not be necessary because the MP table should list the boot
813 * CPU too, but we do it for the sake of robustness anyway.
814 */
815 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
816 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
817 boot_cpu_id);
818 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
819 }
820
821 /*
822 * If we couldn't find a local APIC, then get out of here now!
823 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100824 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
826 boot_cpu_id);
827 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700828 nr_ioapics = 0;
829 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * If SMP should be disabled, then really disable it!
834 */
835 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700837 nr_ioapics = 0;
838 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Andi Kleena8ab26f2005-04-16 15:25:19 -0700841 return 0;
842}
843
844/*
845 * Prepare for SMP bootup. The MP table or ACPI has been read
846 * earlier. Just do some sanity checking here and enable APIC mode.
847 */
Ashok Raje6982c62005-06-25 14:54:58 -0700848void __init smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700849{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700850 nmi_watchdog_default();
851 current_cpu_data = boot_cpu_data;
852 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100853 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700854
Andi Kleena8ab26f2005-04-16 15:25:19 -0700855 if (smp_sanity_check(max_cpus) < 0) {
856 printk(KERN_INFO "SMP disabled\n");
857 disable_smp();
858 return;
859 }
860
861
862 /*
863 * Switch from PIC to APIC mode.
864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 setup_local_APIC();
866
Andi Kleena8ab26f2005-04-16 15:25:19 -0700867 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
868 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
869 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
870 /* Or can we switch back to PIC here? */
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700874 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
876 if (!skip_ioapic_setup && nr_ioapics)
877 setup_IO_APIC();
878 else
879 nr_ioapics = 0;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700882 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Andi Kleena8ab26f2005-04-16 15:25:19 -0700885 setup_boot_APIC_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Andi Kleena8ab26f2005-04-16 15:25:19 -0700888/*
889 * Early setup to make printk work.
890 */
891void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700893 int me = smp_processor_id();
894 cpu_set(me, cpu_online_map);
895 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700896 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Andi Kleena8ab26f2005-04-16 15:25:19 -0700899/*
900 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700901 */
902int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700904 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100905 unsigned long flags;
906 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Andi Kleena8ab26f2005-04-16 15:25:19 -0700908 WARN_ON(irqs_disabled());
909
910 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
911
912 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
913 !physid_isset(apicid, phys_cpu_present_map)) {
914 printk("__cpu_up: bad cpu %d\n", cpu);
915 return -EINVAL;
916 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700917
Ashok Raj76e4f662005-06-25 14:55:00 -0700918 /*
919 * Already booted CPU?
920 */
921 if (cpu_isset(cpu, cpu_callin_map)) {
922 Dprintk("do_boot_cpu %d Already started\n", cpu);
923 return -ENOSYS;
924 }
925
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200926 /*
927 * Save current MTRR state in case it was changed since early boot
928 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
929 */
930 mtrr_save_state();
931
Ashok Raj884d9e42005-06-25 14:55:02 -0700932 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700933 /* Boot it! */
934 err = do_boot_cpu(cpu, apicid);
935 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700936 Dprintk("do_boot_cpu failed %d\n", err);
937 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* Unleash the CPU! */
941 Dprintk("waiting for cpu %d\n", cpu);
942
Ingo Molnar95492e42007-02-16 01:27:34 -0800943 /*
944 * Make sure and check TSC sync:
945 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100946 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800947 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100948 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700951 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700952 err = 0;
953
954 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Andi Kleena8ab26f2005-04-16 15:25:19 -0700957/*
958 * Finish the SMP boot.
959 */
Ashok Raje6982c62005-06-25 14:54:58 -0700960void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700962 smp_cleanup_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700964 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700965}
Ashok Raj76e4f662005-06-25 14:55:00 -0700966
967#ifdef CONFIG_HOTPLUG_CPU
968
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700969static void remove_siblinginfo(int cpu)
Ashok Raj76e4f662005-06-25 14:55:00 -0700970{
971 int sibling;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100972 struct cpuinfo_x86 *c = cpu_data;
Ashok Raj76e4f662005-06-25 14:55:00 -0700973
Mike Travis08357612007-10-16 01:24:04 -0700974 for_each_cpu_mask(sibling, per_cpu(cpu_core_map, cpu)) {
975 cpu_clear(cpu, per_cpu(cpu_core_map, sibling));
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100976 /*
977 * last thread sibling in this cpu core going down
978 */
Mike Travisd5a74302007-10-16 01:24:05 -0700979 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1)
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100980 c[sibling].booted_cores--;
981 }
982
Mike Travisd5a74302007-10-16 01:24:05 -0700983 for_each_cpu_mask(sibling, per_cpu(cpu_sibling_map, cpu))
984 cpu_clear(cpu, per_cpu(cpu_sibling_map, sibling));
985 cpus_clear(per_cpu(cpu_sibling_map, cpu));
Mike Travis08357612007-10-16 01:24:04 -0700986 cpus_clear(per_cpu(cpu_core_map, cpu));
Rohit Sethf3fa8eb2006-06-26 13:58:17 +0200987 c[cpu].phys_proc_id = 0;
988 c[cpu].cpu_core_id = 0;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100989 cpu_clear(cpu, cpu_sibling_setup_map);
Ashok Raj76e4f662005-06-25 14:55:00 -0700990}
991
992void remove_cpu_from_maps(void)
993{
994 int cpu = smp_processor_id();
995
996 cpu_clear(cpu, cpu_callout_map);
997 cpu_clear(cpu, cpu_callin_map);
998 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800999 clear_node_cpumask(cpu);
Ashok Raj76e4f662005-06-25 14:55:00 -07001000}
1001
1002int __cpu_disable(void)
1003{
1004 int cpu = smp_processor_id();
1005
1006 /*
1007 * Perhaps use cpufreq to drop frequency, but that could go
1008 * into generic code.
1009 *
1010 * We won't take down the boot processor on i386 due to some
1011 * interrupts only being able to be serviced by the BSP.
1012 * Especially so if we're not using an IOAPIC -zwane
1013 */
1014 if (cpu == 0)
1015 return -EBUSY;
1016
Shaohua Li4038f902006-09-26 10:52:27 +02001017 if (nmi_watchdog == NMI_LOCAL_APIC)
1018 stop_apic_nmi_watchdog(NULL);
Shaohua Li5e9ef022005-12-12 22:17:08 -08001019 clear_local_APIC();
Ashok Raj76e4f662005-06-25 14:55:00 -07001020
1021 /*
1022 * HACK:
1023 * Allow any queued timer interrupts to get serviced
1024 * This is only a temporary solution until we cleanup
1025 * fixup_irqs as we do for IA64.
1026 */
1027 local_irq_enable();
1028 mdelay(1);
1029
1030 local_irq_disable();
1031 remove_siblinginfo(cpu);
1032
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001033 spin_lock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001034 /* It's now safe to remove this processor from the online map */
1035 cpu_clear(cpu, cpu_online_map);
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001036 spin_unlock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001037 remove_cpu_from_maps();
1038 fixup_irqs(cpu_online_map);
1039 return 0;
1040}
1041
1042void __cpu_die(unsigned int cpu)
1043{
1044 /* We don't do anything here: idle task is faking death itself. */
1045 unsigned int i;
1046
1047 for (i = 0; i < 10; i++) {
1048 /* They ack this in play_dead by setting CPU_DEAD */
Ashok Raj884d9e42005-06-25 14:55:02 -07001049 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1050 printk ("CPU %d is now offline\n", cpu);
Gerd Hoffmannd167a512006-06-26 13:56:16 +02001051 if (1 == num_online_cpus())
1052 alternatives_smp_switch(0);
Ashok Raj76e4f662005-06-25 14:55:00 -07001053 return;
Ashok Raj884d9e42005-06-25 14:55:02 -07001054 }
Nishanth Aravamudanef6e5252005-07-28 21:15:53 -07001055 msleep(100);
Ashok Raj76e4f662005-06-25 14:55:00 -07001056 }
1057 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
1058}
1059
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001060static __init int setup_additional_cpus(char *s)
Andi Kleen420f8f62005-11-05 17:25:54 +01001061{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001062 return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
Andi Kleen420f8f62005-11-05 17:25:54 +01001063}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001064early_param("additional_cpus", setup_additional_cpus);
Andi Kleen420f8f62005-11-05 17:25:54 +01001065
Ashok Raj76e4f662005-06-25 14:55:00 -07001066#else /* ... !CONFIG_HOTPLUG_CPU */
1067
1068int __cpu_disable(void)
1069{
1070 return -ENOSYS;
1071}
1072
1073void __cpu_die(unsigned int cpu)
1074{
1075 /* We said "no" in __cpu_disable */
1076 BUG();
1077}
1078#endif /* CONFIG_HOTPLUG_CPU */