blob: ddc392bee243df3bb51ca064b5937a67ad832d14 [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>
45#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/bootmem.h>
47#include <linux/thread_info.h>
48#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/delay.h>
50#include <linux/mc146818rtc.h>
Andrew Mortona3bc0db2006-09-25 23:32:33 -070051#include <linux/smp.h>
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/mtrr.h>
54#include <asm/pgalloc.h>
55#include <asm/desc.h>
56#include <asm/kdebug.h>
57#include <asm/tlbflush.h>
58#include <asm/proto.h>
Andi Kleen75152112005-05-16 21:53:34 -070059#include <asm/nmi.h>
Al Viro9cdd3042005-09-12 18:49:25 +020060#include <asm/irq.h>
61#include <asm/hw_irq.h>
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -080062#include <asm/numa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/* Number of siblings per CPU package */
65int smp_num_siblings = 1;
Andi Kleen2ee60e172006-06-26 13:59:44 +020066EXPORT_SYMBOL(smp_num_siblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -080068/* Last level cache ID of each logical CPU */
69u8 cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
Andi Kleen2ee60e172006-06-26 13:59:44 +020070EXPORT_SYMBOL(cpu_llc_id);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -080071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Bitmask of currently online CPUs */
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070073cpumask_t cpu_online_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Andi Kleena8ab26f2005-04-16 15:25:19 -070075EXPORT_SYMBOL(cpu_online_map);
76
77/*
78 * Private maps to synchronize booting between AP and BP.
79 * Probably not needed anymore, but it makes for easier debugging. -AK
80 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081cpumask_t cpu_callin_map;
82cpumask_t cpu_callout_map;
Andi Kleen2ee60e172006-06-26 13:59:44 +020083EXPORT_SYMBOL(cpu_callout_map);
Andi Kleena8ab26f2005-04-16 15:25:19 -070084
85cpumask_t cpu_possible_map;
86EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/* Per CPU bogomips and other parameters */
89struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
Andi Kleen2ee60e172006-06-26 13:59:44 +020090EXPORT_SYMBOL(cpu_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Andi Kleena8ab26f2005-04-16 15:25:19 -070092/* Set when the idlers are all forked */
93int smp_threads_ready;
94
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010095/* representing HT siblings of each logical CPU */
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070096cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
Andi Kleen2ee60e172006-06-26 13:59:44 +020097EXPORT_SYMBOL(cpu_sibling_map);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010098
99/* representing HT and core siblings of each logical CPU */
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700100cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
Andi Kleen2df9fa32005-05-20 14:27:59 -0700101EXPORT_SYMBOL(cpu_core_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
104 * Trampoline 80x86 program as an array.
105 */
106
Andi Kleena8ab26f2005-04-16 15:25:19 -0700107extern unsigned char trampoline_data[];
108extern unsigned char trampoline_end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Ashok Raj76e4f662005-06-25 14:55:00 -0700110/* State of each CPU */
111DEFINE_PER_CPU(int, cpu_state) = { 0 };
112
113/*
114 * Store all idle threads, this can be reused instead of creating
115 * a new thread. Also avoids complicated thread destroy functionality
116 * for idle threads.
117 */
118struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
119
120#define get_idle_for_cpu(x) (idle_thread_array[(x)])
121#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
122
123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * Currently trivial. Write the real->protected mode
125 * bootstrap into the page concerned. The caller
126 * has made sure it's suitably aligned.
127 */
128
Andi Kleena8ab26f2005-04-16 15:25:19 -0700129static unsigned long __cpuinit setup_trampoline(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 void *tramp = __va(SMP_TRAMPOLINE_BASE);
132 memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
133 return virt_to_phys(tramp);
134}
135
136/*
137 * The bootstrap kernel entry code has set these up. Save them for
138 * a given CPU
139 */
140
Andi Kleena8ab26f2005-04-16 15:25:19 -0700141static void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct cpuinfo_x86 *c = cpu_data + id;
144
145 *c = boot_cpu_data;
146 identify_cpu(c);
Andi Kleendda50e72005-05-16 21:53:25 -0700147 print_cpu_info(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Andi Kleena8ab26f2005-04-16 15:25:19 -0700150static atomic_t init_deasserted __cpuinitdata;
151
152/*
153 * Report back to the Boot Processor.
154 * Running on AP.
155 */
156void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 int cpuid, phys_id;
159 unsigned long timeout;
160
161 /*
162 * If waken up by an INIT in an 82489DX configuration
163 * we may get here before an INIT-deassert IPI reaches
164 * our local APIC. We have to wait for the IPI or we'll
165 * lock up on an APIC access.
166 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700167 while (!atomic_read(&init_deasserted))
168 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /*
171 * (This works even if the APIC is not enabled.)
172 */
173 phys_id = GET_APIC_ID(apic_read(APIC_ID));
174 cpuid = smp_processor_id();
175 if (cpu_isset(cpuid, cpu_callin_map)) {
176 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
177 phys_id, cpuid);
178 }
179 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
180
181 /*
182 * STARTUP IPIs are fragile beasts as they might sometimes
183 * trigger some glue motherboard logic. Complete APIC bus
184 * silence for 1 second, this overestimates the time the
185 * boot CPU is spending to send the up to 2 STARTUP IPIs
186 * by a factor of two. This should be enough.
187 */
188
189 /*
190 * Waiting 2s total for startup (udelay is not yet working)
191 */
192 timeout = jiffies + 2*HZ;
193 while (time_before(jiffies, timeout)) {
194 /*
195 * Has the boot CPU finished it's STARTUP sequence?
196 */
197 if (cpu_isset(cpuid, cpu_callout_map))
198 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700199 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
202 if (!time_before(jiffies, timeout)) {
203 panic("smp_callin: CPU%d started up but did not get a callout!\n",
204 cpuid);
205 }
206
207 /*
208 * the boot CPU has finished the init stage and is spinning
209 * on callin_map until we finish. We are free to set up this
210 * CPU, first the APIC. (this is probably redundant on most
211 * boards)
212 */
213
214 Dprintk("CALLIN, before setup_local_APIC().\n");
215 setup_local_APIC();
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /*
218 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200219 *
220 * Need to enable IRQs because it can take longer and then
221 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Andi Kleenb4452212005-09-12 18:49:24 +0200223 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200225 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 Dprintk("Stack at about %p\n",&cpuid);
227
228 disable_APIC_timer();
229
230 /*
231 * Save our processor parameters
232 */
233 smp_store_cpu_info(cpuid);
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /*
236 * Allow the master to continue.
237 */
238 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800241/* maps the cpu to the sched domain representing multi-core */
242cpumask_t cpu_coregroup_map(int cpu)
243{
244 struct cpuinfo_x86 *c = cpu_data + cpu;
245 /*
246 * For perf, we return last level cache shared map.
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700247 * And for power savings, we return cpu_core_map
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800248 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700249 if (sched_mc_power_savings || sched_smt_power_savings)
250 return cpu_core_map[cpu];
251 else
252 return c->llc_shared_map;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800253}
254
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100255/* representing cpus for which sibling maps can be computed */
256static cpumask_t cpu_sibling_setup_map;
257
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700258static inline void set_cpu_sibling_map(int cpu)
259{
260 int i;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100261 struct cpuinfo_x86 *c = cpu_data;
262
263 cpu_set(cpu, cpu_sibling_setup_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700264
265 if (smp_num_siblings > 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100266 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Rohit Sethf3fa8eb2006-06-26 13:58:17 +0200267 if (c[cpu].phys_proc_id == c[i].phys_proc_id &&
268 c[cpu].cpu_core_id == c[i].cpu_core_id) {
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700269 cpu_set(i, cpu_sibling_map[cpu]);
270 cpu_set(cpu, cpu_sibling_map[i]);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100271 cpu_set(i, cpu_core_map[cpu]);
272 cpu_set(cpu, cpu_core_map[i]);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800273 cpu_set(i, c[cpu].llc_shared_map);
274 cpu_set(cpu, c[i].llc_shared_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700275 }
276 }
277 } else {
278 cpu_set(cpu, cpu_sibling_map[cpu]);
279 }
280
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800281 cpu_set(cpu, c[cpu].llc_shared_map);
282
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100283 if (current_cpu_data.x86_max_cores == 1) {
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700284 cpu_core_map[cpu] = cpu_sibling_map[cpu];
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100285 c[cpu].booted_cores = 1;
286 return;
287 }
288
289 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800290 if (cpu_llc_id[cpu] != BAD_APICID &&
291 cpu_llc_id[cpu] == cpu_llc_id[i]) {
292 cpu_set(i, c[cpu].llc_shared_map);
293 cpu_set(cpu, c[i].llc_shared_map);
294 }
Rohit Sethf3fa8eb2006-06-26 13:58:17 +0200295 if (c[cpu].phys_proc_id == c[i].phys_proc_id) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100296 cpu_set(i, cpu_core_map[cpu]);
297 cpu_set(cpu, cpu_core_map[i]);
298 /*
299 * Does this new cpu bringup a new core?
300 */
301 if (cpus_weight(cpu_sibling_map[cpu]) == 1) {
302 /*
303 * for each core in package, increment
304 * the booted_cores for this new cpu
305 */
306 if (first_cpu(cpu_sibling_map[i]) == i)
307 c[cpu].booted_cores++;
308 /*
309 * increment the core count for all
310 * the other cpus in this package
311 */
312 if (i != cpu)
313 c[i].booted_cores++;
314 } else if (i != cpu && !c[cpu].booted_cores)
315 c[cpu].booted_cores = c[i].booted_cores;
316 }
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700317 }
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700321 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700323void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 /*
326 * Dont put anything before smp_callin(), SMP
327 * booting is too fragile that we want to limit the
328 * things done here to the most necessary things.
329 */
330 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800331 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 smp_callin();
333
334 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
335 barrier();
336
Ingo Molnar95492e42007-02-16 01:27:34 -0800337 /*
338 * Check TSC sync first:
339 */
340 check_tsc_sync_target();
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 Dprintk("cpu %d: setting up apic clock\n", smp_processor_id());
343 setup_secondary_APIC_clock();
344
Andi Kleena8ab26f2005-04-16 15:25:19 -0700345 Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (nmi_watchdog == NMI_IO_APIC) {
348 disable_8259A_irq(0);
349 enable_NMI_through_LVT0(NULL);
350 enable_8259A_irq(0);
351 }
352
Andi Kleena8ab26f2005-04-16 15:25:19 -0700353 enable_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700356 * The sibling maps must be set before turing the online map on for
357 * this cpu
358 */
359 set_cpu_sibling_map(smp_processor_id());
360
361 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700362 * We need to hold call_lock, so there is no inconsistency
363 * between the time smp_call_function() determines number of
364 * IPI receipients, and the time when the determination is made
365 * for which cpus receive the IPI in genapic_flat.c. Holding this
366 * lock helps us to not include this cpu in a currently in progress
367 * smp_call_function().
368 */
369 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200370 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700371
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200372 /* Setup the per cpu irq handling data structures */
373 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700374 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700375 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700378 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200379 spin_unlock(&vector_lock);
Ingo Molnar95492e42007-02-16 01:27:34 -0800380
Ashok Raj884d9e42005-06-25 14:55:02 -0700381 unlock_ipi_call_lock();
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 cpu_idle();
384}
385
Andi Kleena8ab26f2005-04-16 15:25:19 -0700386extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387extern void (*initial_code)(void);
388
Olaf Hering44456d32005-07-27 11:45:17 -0700389#ifdef APIC_DEBUG
Andi Kleena8ab26f2005-04-16 15:25:19 -0700390static void inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
393 char *names[] = { "ID", "VERSION", "SPIV" };
394 int timeout, status;
395
396 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
397
398 for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
399 printk("... APIC #%d %s: ", apicid, names[i]);
400
401 /*
402 * Wait for idle.
403 */
404 apic_wait_icr_idle();
405
Andi Kleenc1507eb2005-09-12 18:49:23 +0200406 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
407 apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 timeout = 0;
410 do {
411 udelay(100);
412 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
413 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
414
415 switch (status) {
416 case APIC_ICR_RR_VALID:
417 status = apic_read(APIC_RRR);
418 printk("%08x\n", status);
419 break;
420 default:
421 printk("failed\n");
422 }
423 }
424}
425#endif
426
Andi Kleena8ab26f2005-04-16 15:25:19 -0700427/*
428 * Kick the secondary to wake up.
429 */
430static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 unsigned long send_status = 0, accept_status = 0;
433 int maxlvt, timeout, num_starts, j;
434
435 Dprintk("Asserting INIT.\n");
436
437 /*
438 * Turn INIT on target chip
439 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200440 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /*
443 * Send IPI
444 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200445 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 | APIC_DM_INIT);
447
448 Dprintk("Waiting for send to finish...\n");
449 timeout = 0;
450 do {
451 Dprintk("+");
452 udelay(100);
453 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
454 } while (send_status && (timeout++ < 1000));
455
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");
467 timeout = 0;
468 do {
469 Dprintk("+");
470 udelay(100);
471 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
472 } while (send_status && (timeout++ < 1000));
473
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100474 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 atomic_set(&init_deasserted, 1);
476
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200477 num_starts = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /*
480 * Run STARTUP IPI loop.
481 */
482 Dprintk("#startup loops: %d.\n", num_starts);
483
484 maxlvt = get_maxlvt();
485
486 for (j = 1; j <= num_starts; j++) {
487 Dprintk("Sending STARTUP #%d.\n",j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 apic_write(APIC_ESR, 0);
489 apic_read(APIC_ESR);
490 Dprintk("After apic_write.\n");
491
492 /*
493 * STARTUP IPI
494 */
495
496 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200497 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* Boot on the stack */
500 /* Kick the second */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200501 apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /*
504 * Give the other CPU some time to accept the IPI.
505 */
506 udelay(300);
507
508 Dprintk("Startup point 1.\n");
509
510 Dprintk("Waiting for send to finish...\n");
511 timeout = 0;
512 do {
513 Dprintk("+");
514 udelay(100);
515 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
516 } while (send_status && (timeout++ < 1000));
517
518 /*
519 * Give the other CPU some time to accept the IPI.
520 */
521 udelay(200);
522 /*
523 * Due to the Pentium erratum 3AP.
524 */
525 if (maxlvt > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 apic_write(APIC_ESR, 0);
527 }
528 accept_status = (apic_read(APIC_ESR) & 0xEF);
529 if (send_status || accept_status)
530 break;
531 }
532 Dprintk("After Startup.\n");
533
534 if (send_status)
535 printk(KERN_ERR "APIC never delivered???\n");
536 if (accept_status)
537 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
538
539 return (send_status | accept_status);
540}
541
Ashok Raj76e4f662005-06-25 14:55:00 -0700542struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000543 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700544 struct task_struct *idle;
545 struct completion done;
546 int cpu;
547};
548
David Howells65f27f32006-11-22 14:55:48 +0000549void do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700550{
David Howells65f27f32006-11-22 14:55:48 +0000551 struct create_idle *c_idle =
552 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700553
554 c_idle->idle = fork_idle(c_idle->cpu);
555 complete(&c_idle->done);
556}
557
Andi Kleena8ab26f2005-04-16 15:25:19 -0700558/*
559 * Boot one CPU.
560 */
561static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700564 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700566 struct create_idle c_idle = {
David Howells65f27f32006-11-22 14:55:48 +0000567 .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
Ashok Raj76e4f662005-06-25 14:55:00 -0700568 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700569 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700570 };
Ashok Raj76e4f662005-06-25 14:55:00 -0700571
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100572 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
573 if (!cpu_gdt_descr[cpu].address &&
574 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
575 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
576 return -1;
577 }
578
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100579 /* Allocate node local memory for AP pdas */
580 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
581 struct x8664_pda *newpda, *pda;
582 int node = cpu_to_node(cpu);
583 pda = cpu_pda(cpu);
584 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
585 node);
586 if (newpda) {
587 memcpy(newpda, pda, sizeof (struct x8664_pda));
588 cpu_pda(cpu) = newpda;
589 } else
590 printk(KERN_ERR
591 "Could not allocate node local PDA for CPU %d on node %d\n",
592 cpu, node);
593 }
594
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200595 alternatives_smp_switch(1);
596
Ashok Raj76e4f662005-06-25 14:55:00 -0700597 c_idle.idle = get_idle_for_cpu(cpu);
598
599 if (c_idle.idle) {
600 c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800601 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700602 init_idle(c_idle.idle, cpu);
603 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Ashok Raj76e4f662005-06-25 14:55:00 -0700606 /*
607 * During cold boot process, keventd thread is not spun up yet.
608 * When we do cpu hot-add, we create idle threads on the fly, we should
609 * not acquire any attributes from the calling context. Hence the clean
610 * way to create kernel_threads() is to do that from keventd().
611 * We do the current_is_keventd() due to the fact that ACPI notifier
612 * was also queuing to keventd() and when the caller is already running
613 * in context of keventd(), we would end up with locking up the keventd
614 * thread.
615 */
616 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000617 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700618 else {
David Howells65f27f32006-11-22 14:55:48 +0000619 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700620 wait_for_completion(&c_idle.done);
621 }
622
623 if (IS_ERR(c_idle.idle)) {
624 printk("failed fork for CPU %d\n", cpu);
625 return PTR_ERR(c_idle.idle);
626 }
627
628 set_idle_for_cpu(cpu, c_idle.idle);
629
630do_rest:
631
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100632 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 start_rip = setup_trampoline();
635
Ashok Raj76e4f662005-06-25 14:55:00 -0700636 init_rsp = c_idle.idle->thread.rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 per_cpu(init_tss,cpu).rsp0 = init_rsp;
638 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800639 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Andi Kleende04f322005-07-28 21:15:29 -0700641 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
642 cpus_weight(cpu_present_map),
643 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /*
646 * This grunge runs the startup process for
647 * the targeted processor.
648 */
649
650 atomic_set(&init_deasserted, 0);
651
652 Dprintk("Setting warm reset code and vector.\n");
653
654 CMOS_WRITE(0xa, 0xf);
655 local_flush_tlb();
656 Dprintk("1.\n");
657 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
658 Dprintk("2.\n");
659 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
660 Dprintk("3.\n");
661
662 /*
663 * Be paranoid about clearing APIC errors.
664 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100665 apic_write(APIC_ESR, 0);
666 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /*
669 * Status is now clean
670 */
671 boot_error = 0;
672
673 /*
674 * Starting actual IPI sequence...
675 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700676 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (!boot_error) {
679 /*
680 * allow APs to start initializing.
681 */
682 Dprintk("Before Callout %d.\n", cpu);
683 cpu_set(cpu, cpu_callout_map);
684 Dprintk("After Callout %d.\n", cpu);
685
686 /*
687 * Wait 5s total for a response
688 */
689 for (timeout = 0; timeout < 50000; timeout++) {
690 if (cpu_isset(cpu, cpu_callin_map))
691 break; /* It has booted */
692 udelay(100);
693 }
694
695 if (cpu_isset(cpu, cpu_callin_map)) {
696 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 Dprintk("CPU has booted.\n");
698 } else {
699 boot_error = 1;
700 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
701 == 0xA5)
702 /* trampoline started but...? */
703 printk("Stuck ??\n");
704 else
705 /* trampoline code not run */
706 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700707#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 inquire_remote_apic(apicid);
709#endif
710 }
711 }
712 if (boot_error) {
713 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
714 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800715 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700716 cpu_clear(cpu, cpu_present_map);
717 cpu_clear(cpu, cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 x86_cpu_to_apicid[cpu] = BAD_APICID;
719 x86_cpu_to_log_apicid[cpu] = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700720 return -EIO;
721 }
722
723 return 0;
724}
725
726cycles_t cacheflush_time;
727unsigned long cache_decay_ticks;
728
729/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700730 * Cleanup possible dangling ends...
731 */
732static __cpuinit void smp_cleanup_boot(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700735 * Paranoid: Set warm reset code and vector here back
736 * to default values.
737 */
738 CMOS_WRITE(0, 0xf);
739
740 /*
741 * Reset trampoline flag
742 */
743 *((volatile int *) phys_to_virt(0x467)) = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700744}
745
746/*
747 * Fall back to non SMP mode after errors.
748 *
749 * RED-PEN audit/test this more. I bet there is more state messed up here.
750 */
Ashok Raje6982c62005-06-25 14:54:58 -0700751static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700752{
753 cpu_present_map = cpumask_of_cpu(0);
754 cpu_possible_map = cpumask_of_cpu(0);
755 if (smp_found_config)
756 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
757 else
758 phys_cpu_present_map = physid_mask_of_physid(0);
759 cpu_set(0, cpu_sibling_map[0]);
760 cpu_set(0, cpu_core_map[0]);
761}
762
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700763#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen420f8f62005-11-05 17:25:54 +0100764
765int additional_cpus __initdata = -1;
766
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700767/*
768 * cpu_possible_map should be static, it cannot change as cpu's
769 * are onlined, or offlined. The reason is per-cpu data-structures
770 * are allocated by some modules at init time, and dont expect to
771 * do this dynamically on cpu arrival/departure.
772 * cpu_present_map on the other hand can change dynamically.
773 * In case when cpu_hotplug is not compiled, then we resort to current
774 * behaviour, which is cpu_possible == cpu_present.
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700775 * - Ashok Raj
Andi Kleen420f8f62005-11-05 17:25:54 +0100776 *
777 * Three ways to find out the number of additional hotplug CPUs:
778 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Andi Kleen420f8f62005-11-05 17:25:54 +0100779 * - The user can overwrite it with additional_cpus=NUM
Andi Kleenf62a91f2006-01-11 22:42:35 +0100780 * - Otherwise don't reserve additional CPUs.
Andi Kleen420f8f62005-11-05 17:25:54 +0100781 * We do this because additional CPUs waste a lot of memory.
782 * -AK
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700783 */
Andi Kleen421c7ce2005-10-10 22:32:45 +0200784__init void prefill_possible_map(void)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700785{
786 int i;
Andi Kleen420f8f62005-11-05 17:25:54 +0100787 int possible;
788
789 if (additional_cpus == -1) {
Andi Kleenf62a91f2006-01-11 22:42:35 +0100790 if (disabled_cpus > 0)
Andi Kleen420f8f62005-11-05 17:25:54 +0100791 additional_cpus = disabled_cpus;
Andi Kleenf62a91f2006-01-11 22:42:35 +0100792 else
793 additional_cpus = 0;
Andi Kleen420f8f62005-11-05 17:25:54 +0100794 }
795 possible = num_processors + additional_cpus;
796 if (possible > NR_CPUS)
797 possible = NR_CPUS;
798
799 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
800 possible,
801 max_t(int, possible - num_processors, 0));
802
803 for (i = 0; i < possible; i++)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700804 cpu_set(i, cpu_possible_map);
805}
806#endif
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700809 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
Ashok Raje6982c62005-06-25 14:54:58 -0700811static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
814 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
815 hard_smp_processor_id());
816 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
817 }
818
819 /*
820 * If we couldn't find an SMP configuration at boot time,
821 * get out of here now!
822 */
823 if (!smp_found_config) {
824 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700825 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (APIC_init_uniprocessor())
827 printk(KERN_NOTICE "Local APIC not detected."
828 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700829 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 /*
833 * Should not be necessary because the MP table should list the boot
834 * CPU too, but we do it for the sake of robustness anyway.
835 */
836 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
837 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
838 boot_cpu_id);
839 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
840 }
841
842 /*
843 * If we couldn't find a local APIC, then get out of here now!
844 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100845 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
847 boot_cpu_id);
848 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700849 nr_ioapics = 0;
850 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * If SMP should be disabled, then really disable it!
855 */
856 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700858 nr_ioapics = 0;
859 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
Andi Kleena8ab26f2005-04-16 15:25:19 -0700862 return 0;
863}
864
865/*
866 * Prepare for SMP bootup. The MP table or ACPI has been read
867 * earlier. Just do some sanity checking here and enable APIC mode.
868 */
Ashok Raje6982c62005-06-25 14:54:58 -0700869void __init smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700870{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700871 nmi_watchdog_default();
872 current_cpu_data = boot_cpu_data;
873 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100874 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700875
Andi Kleena8ab26f2005-04-16 15:25:19 -0700876 if (smp_sanity_check(max_cpus) < 0) {
877 printk(KERN_INFO "SMP disabled\n");
878 disable_smp();
879 return;
880 }
881
882
883 /*
884 * Switch from PIC to APIC mode.
885 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 setup_local_APIC();
887
Andi Kleena8ab26f2005-04-16 15:25:19 -0700888 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
889 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
890 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
891 /* Or can we switch back to PIC here? */
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700895 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 */
897 if (!skip_ioapic_setup && nr_ioapics)
898 setup_IO_APIC();
899 else
900 nr_ioapics = 0;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700903 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Andi Kleena8ab26f2005-04-16 15:25:19 -0700906 setup_boot_APIC_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Andi Kleena8ab26f2005-04-16 15:25:19 -0700909/*
910 * Early setup to make printk work.
911 */
912void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700914 int me = smp_processor_id();
915 cpu_set(me, cpu_online_map);
916 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700917 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Andi Kleena8ab26f2005-04-16 15:25:19 -0700920/*
921 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700922 */
923int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700925 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100926 unsigned long flags;
927 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Andi Kleena8ab26f2005-04-16 15:25:19 -0700929 WARN_ON(irqs_disabled());
930
931 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
932
933 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
934 !physid_isset(apicid, phys_cpu_present_map)) {
935 printk("__cpu_up: bad cpu %d\n", cpu);
936 return -EINVAL;
937 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700938
Ashok Raj76e4f662005-06-25 14:55:00 -0700939 /*
940 * Already booted CPU?
941 */
942 if (cpu_isset(cpu, cpu_callin_map)) {
943 Dprintk("do_boot_cpu %d Already started\n", cpu);
944 return -ENOSYS;
945 }
946
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200947 /*
948 * Save current MTRR state in case it was changed since early boot
949 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
950 */
951 mtrr_save_state();
952
Ashok Raj884d9e42005-06-25 14:55:02 -0700953 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700954 /* Boot it! */
955 err = do_boot_cpu(cpu, apicid);
956 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700957 Dprintk("do_boot_cpu failed %d\n", err);
958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* Unleash the CPU! */
962 Dprintk("waiting for cpu %d\n", cpu);
963
Ingo Molnar95492e42007-02-16 01:27:34 -0800964 /*
965 * Make sure and check TSC sync:
966 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100967 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800968 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100969 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700972 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700973 err = 0;
974
975 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Andi Kleena8ab26f2005-04-16 15:25:19 -0700978/*
979 * Finish the SMP boot.
980 */
Ashok Raje6982c62005-06-25 14:54:58 -0700981void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700983 smp_cleanup_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700985 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700986}
Ashok Raj76e4f662005-06-25 14:55:00 -0700987
988#ifdef CONFIG_HOTPLUG_CPU
989
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700990static void remove_siblinginfo(int cpu)
Ashok Raj76e4f662005-06-25 14:55:00 -0700991{
992 int sibling;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100993 struct cpuinfo_x86 *c = cpu_data;
Ashok Raj76e4f662005-06-25 14:55:00 -0700994
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100995 for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
996 cpu_clear(cpu, cpu_core_map[sibling]);
997 /*
998 * last thread sibling in this cpu core going down
999 */
1000 if (cpus_weight(cpu_sibling_map[cpu]) == 1)
1001 c[sibling].booted_cores--;
1002 }
1003
Ashok Raj76e4f662005-06-25 14:55:00 -07001004 for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
1005 cpu_clear(cpu, cpu_sibling_map[sibling]);
Ashok Raj76e4f662005-06-25 14:55:00 -07001006 cpus_clear(cpu_sibling_map[cpu]);
1007 cpus_clear(cpu_core_map[cpu]);
Rohit Sethf3fa8eb2006-06-26 13:58:17 +02001008 c[cpu].phys_proc_id = 0;
1009 c[cpu].cpu_core_id = 0;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001010 cpu_clear(cpu, cpu_sibling_setup_map);
Ashok Raj76e4f662005-06-25 14:55:00 -07001011}
1012
1013void remove_cpu_from_maps(void)
1014{
1015 int cpu = smp_processor_id();
1016
1017 cpu_clear(cpu, cpu_callout_map);
1018 cpu_clear(cpu, cpu_callin_map);
1019 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -08001020 clear_node_cpumask(cpu);
Ashok Raj76e4f662005-06-25 14:55:00 -07001021}
1022
1023int __cpu_disable(void)
1024{
1025 int cpu = smp_processor_id();
1026
1027 /*
1028 * Perhaps use cpufreq to drop frequency, but that could go
1029 * into generic code.
1030 *
1031 * We won't take down the boot processor on i386 due to some
1032 * interrupts only being able to be serviced by the BSP.
1033 * Especially so if we're not using an IOAPIC -zwane
1034 */
1035 if (cpu == 0)
1036 return -EBUSY;
1037
Shaohua Li4038f902006-09-26 10:52:27 +02001038 if (nmi_watchdog == NMI_LOCAL_APIC)
1039 stop_apic_nmi_watchdog(NULL);
Shaohua Li5e9ef022005-12-12 22:17:08 -08001040 clear_local_APIC();
Ashok Raj76e4f662005-06-25 14:55:00 -07001041
1042 /*
1043 * HACK:
1044 * Allow any queued timer interrupts to get serviced
1045 * This is only a temporary solution until we cleanup
1046 * fixup_irqs as we do for IA64.
1047 */
1048 local_irq_enable();
1049 mdelay(1);
1050
1051 local_irq_disable();
1052 remove_siblinginfo(cpu);
1053
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001054 spin_lock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001055 /* It's now safe to remove this processor from the online map */
1056 cpu_clear(cpu, cpu_online_map);
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001057 spin_unlock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001058 remove_cpu_from_maps();
1059 fixup_irqs(cpu_online_map);
1060 return 0;
1061}
1062
1063void __cpu_die(unsigned int cpu)
1064{
1065 /* We don't do anything here: idle task is faking death itself. */
1066 unsigned int i;
1067
1068 for (i = 0; i < 10; i++) {
1069 /* They ack this in play_dead by setting CPU_DEAD */
Ashok Raj884d9e42005-06-25 14:55:02 -07001070 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1071 printk ("CPU %d is now offline\n", cpu);
Gerd Hoffmannd167a512006-06-26 13:56:16 +02001072 if (1 == num_online_cpus())
1073 alternatives_smp_switch(0);
Ashok Raj76e4f662005-06-25 14:55:00 -07001074 return;
Ashok Raj884d9e42005-06-25 14:55:02 -07001075 }
Nishanth Aravamudanef6e5252005-07-28 21:15:53 -07001076 msleep(100);
Ashok Raj76e4f662005-06-25 14:55:00 -07001077 }
1078 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
1079}
1080
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001081static __init int setup_additional_cpus(char *s)
Andi Kleen420f8f62005-11-05 17:25:54 +01001082{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001083 return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
Andi Kleen420f8f62005-11-05 17:25:54 +01001084}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001085early_param("additional_cpus", setup_additional_cpus);
Andi Kleen420f8f62005-11-05 17:25:54 +01001086
Ashok Raj76e4f662005-06-25 14:55:00 -07001087#else /* ... !CONFIG_HOTPLUG_CPU */
1088
1089int __cpu_disable(void)
1090{
1091 return -ENOSYS;
1092}
1093
1094void __cpu_die(unsigned int cpu)
1095{
1096 /* We said "no" in __cpu_disable */
1097 BUG();
1098}
1099#endif /* CONFIG_HOTPLUG_CPU */