blob: 082f478fb77798c1eee85a3a306eeacd1407d7f6 [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" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200394 int timeout;
395 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
398
399 for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
400 printk("... APIC #%d %s: ", apicid, names[i]);
401
402 /*
403 * Wait for idle.
404 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200405 status = safe_apic_wait_icr_idle();
406 if (status)
407 printk("a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Andi Kleenc1507eb2005-09-12 18:49:23 +0200409 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
410 apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 timeout = 0;
413 do {
414 udelay(100);
415 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
416 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
417
418 switch (status) {
419 case APIC_ICR_RR_VALID:
420 status = apic_read(APIC_RRR);
421 printk("%08x\n", status);
422 break;
423 default:
424 printk("failed\n");
425 }
426 }
427}
428#endif
429
Andi Kleena8ab26f2005-04-16 15:25:19 -0700430/*
431 * Kick the secondary to wake up.
432 */
433static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200435 unsigned long send_status, accept_status = 0;
436 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 Dprintk("Asserting INIT.\n");
439
440 /*
441 * Turn INIT on target chip
442 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200443 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /*
446 * Send IPI
447 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200448 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 | APIC_DM_INIT);
450
451 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200452 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 mdelay(10);
455
456 Dprintk("Deasserting INIT.\n");
457
458 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200459 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* Send IPI */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200462 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200465 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100467 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 atomic_set(&init_deasserted, 1);
469
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200470 num_starts = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /*
473 * Run STARTUP IPI loop.
474 */
475 Dprintk("#startup loops: %d.\n", num_starts);
476
477 maxlvt = get_maxlvt();
478
479 for (j = 1; j <= num_starts; j++) {
480 Dprintk("Sending STARTUP #%d.\n",j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 apic_write(APIC_ESR, 0);
482 apic_read(APIC_ESR);
483 Dprintk("After apic_write.\n");
484
485 /*
486 * STARTUP IPI
487 */
488
489 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200490 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* Boot on the stack */
493 /* Kick the second */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200494 apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /*
497 * Give the other CPU some time to accept the IPI.
498 */
499 udelay(300);
500
501 Dprintk("Startup point 1.\n");
502
503 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200504 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /*
507 * Give the other CPU some time to accept the IPI.
508 */
509 udelay(200);
510 /*
511 * Due to the Pentium erratum 3AP.
512 */
513 if (maxlvt > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 apic_write(APIC_ESR, 0);
515 }
516 accept_status = (apic_read(APIC_ESR) & 0xEF);
517 if (send_status || accept_status)
518 break;
519 }
520 Dprintk("After Startup.\n");
521
522 if (send_status)
523 printk(KERN_ERR "APIC never delivered???\n");
524 if (accept_status)
525 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
526
527 return (send_status | accept_status);
528}
529
Ashok Raj76e4f662005-06-25 14:55:00 -0700530struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000531 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700532 struct task_struct *idle;
533 struct completion done;
534 int cpu;
535};
536
David Howells65f27f32006-11-22 14:55:48 +0000537void do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700538{
David Howells65f27f32006-11-22 14:55:48 +0000539 struct create_idle *c_idle =
540 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700541
542 c_idle->idle = fork_idle(c_idle->cpu);
543 complete(&c_idle->done);
544}
545
Andi Kleena8ab26f2005-04-16 15:25:19 -0700546/*
547 * Boot one CPU.
548 */
549static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700552 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700554 struct create_idle c_idle = {
David Howells65f27f32006-11-22 14:55:48 +0000555 .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
Ashok Raj76e4f662005-06-25 14:55:00 -0700556 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700557 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700558 };
Ashok Raj76e4f662005-06-25 14:55:00 -0700559
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100560 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
561 if (!cpu_gdt_descr[cpu].address &&
562 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
563 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
564 return -1;
565 }
566
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100567 /* Allocate node local memory for AP pdas */
568 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
569 struct x8664_pda *newpda, *pda;
570 int node = cpu_to_node(cpu);
571 pda = cpu_pda(cpu);
572 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
573 node);
574 if (newpda) {
575 memcpy(newpda, pda, sizeof (struct x8664_pda));
576 cpu_pda(cpu) = newpda;
577 } else
578 printk(KERN_ERR
579 "Could not allocate node local PDA for CPU %d on node %d\n",
580 cpu, node);
581 }
582
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200583 alternatives_smp_switch(1);
584
Ashok Raj76e4f662005-06-25 14:55:00 -0700585 c_idle.idle = get_idle_for_cpu(cpu);
586
587 if (c_idle.idle) {
588 c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800589 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700590 init_idle(c_idle.idle, cpu);
591 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Ashok Raj76e4f662005-06-25 14:55:00 -0700594 /*
595 * During cold boot process, keventd thread is not spun up yet.
596 * When we do cpu hot-add, we create idle threads on the fly, we should
597 * not acquire any attributes from the calling context. Hence the clean
598 * way to create kernel_threads() is to do that from keventd().
599 * We do the current_is_keventd() due to the fact that ACPI notifier
600 * was also queuing to keventd() and when the caller is already running
601 * in context of keventd(), we would end up with locking up the keventd
602 * thread.
603 */
604 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000605 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700606 else {
David Howells65f27f32006-11-22 14:55:48 +0000607 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700608 wait_for_completion(&c_idle.done);
609 }
610
611 if (IS_ERR(c_idle.idle)) {
612 printk("failed fork for CPU %d\n", cpu);
613 return PTR_ERR(c_idle.idle);
614 }
615
616 set_idle_for_cpu(cpu, c_idle.idle);
617
618do_rest:
619
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100620 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 start_rip = setup_trampoline();
623
Ashok Raj76e4f662005-06-25 14:55:00 -0700624 init_rsp = c_idle.idle->thread.rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 per_cpu(init_tss,cpu).rsp0 = init_rsp;
626 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800627 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Andi Kleende04f322005-07-28 21:15:29 -0700629 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
630 cpus_weight(cpu_present_map),
631 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /*
634 * This grunge runs the startup process for
635 * the targeted processor.
636 */
637
638 atomic_set(&init_deasserted, 0);
639
640 Dprintk("Setting warm reset code and vector.\n");
641
642 CMOS_WRITE(0xa, 0xf);
643 local_flush_tlb();
644 Dprintk("1.\n");
645 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
646 Dprintk("2.\n");
647 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
648 Dprintk("3.\n");
649
650 /*
651 * Be paranoid about clearing APIC errors.
652 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100653 apic_write(APIC_ESR, 0);
654 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /*
657 * Status is now clean
658 */
659 boot_error = 0;
660
661 /*
662 * Starting actual IPI sequence...
663 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700664 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (!boot_error) {
667 /*
668 * allow APs to start initializing.
669 */
670 Dprintk("Before Callout %d.\n", cpu);
671 cpu_set(cpu, cpu_callout_map);
672 Dprintk("After Callout %d.\n", cpu);
673
674 /*
675 * Wait 5s total for a response
676 */
677 for (timeout = 0; timeout < 50000; timeout++) {
678 if (cpu_isset(cpu, cpu_callin_map))
679 break; /* It has booted */
680 udelay(100);
681 }
682
683 if (cpu_isset(cpu, cpu_callin_map)) {
684 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 Dprintk("CPU has booted.\n");
686 } else {
687 boot_error = 1;
688 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
689 == 0xA5)
690 /* trampoline started but...? */
691 printk("Stuck ??\n");
692 else
693 /* trampoline code not run */
694 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700695#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 inquire_remote_apic(apicid);
697#endif
698 }
699 }
700 if (boot_error) {
701 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
702 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800703 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700704 cpu_clear(cpu, cpu_present_map);
705 cpu_clear(cpu, cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 x86_cpu_to_apicid[cpu] = BAD_APICID;
707 x86_cpu_to_log_apicid[cpu] = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700708 return -EIO;
709 }
710
711 return 0;
712}
713
714cycles_t cacheflush_time;
715unsigned long cache_decay_ticks;
716
717/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700718 * Cleanup possible dangling ends...
719 */
720static __cpuinit void smp_cleanup_boot(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700723 * Paranoid: Set warm reset code and vector here back
724 * to default values.
725 */
726 CMOS_WRITE(0, 0xf);
727
728 /*
729 * Reset trampoline flag
730 */
731 *((volatile int *) phys_to_virt(0x467)) = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700732}
733
734/*
735 * Fall back to non SMP mode after errors.
736 *
737 * RED-PEN audit/test this more. I bet there is more state messed up here.
738 */
Ashok Raje6982c62005-06-25 14:54:58 -0700739static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700740{
741 cpu_present_map = cpumask_of_cpu(0);
742 cpu_possible_map = cpumask_of_cpu(0);
743 if (smp_found_config)
744 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
745 else
746 phys_cpu_present_map = physid_mask_of_physid(0);
747 cpu_set(0, cpu_sibling_map[0]);
748 cpu_set(0, cpu_core_map[0]);
749}
750
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700751#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen420f8f62005-11-05 17:25:54 +0100752
753int additional_cpus __initdata = -1;
754
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700755/*
756 * cpu_possible_map should be static, it cannot change as cpu's
757 * are onlined, or offlined. The reason is per-cpu data-structures
758 * are allocated by some modules at init time, and dont expect to
759 * do this dynamically on cpu arrival/departure.
760 * cpu_present_map on the other hand can change dynamically.
761 * In case when cpu_hotplug is not compiled, then we resort to current
762 * behaviour, which is cpu_possible == cpu_present.
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700763 * - Ashok Raj
Andi Kleen420f8f62005-11-05 17:25:54 +0100764 *
765 * Three ways to find out the number of additional hotplug CPUs:
766 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Andi Kleen420f8f62005-11-05 17:25:54 +0100767 * - The user can overwrite it with additional_cpus=NUM
Andi Kleenf62a91f2006-01-11 22:42:35 +0100768 * - Otherwise don't reserve additional CPUs.
Andi Kleen420f8f62005-11-05 17:25:54 +0100769 * We do this because additional CPUs waste a lot of memory.
770 * -AK
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700771 */
Andi Kleen421c7ce2005-10-10 22:32:45 +0200772__init void prefill_possible_map(void)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700773{
774 int i;
Andi Kleen420f8f62005-11-05 17:25:54 +0100775 int possible;
776
777 if (additional_cpus == -1) {
Andi Kleenf62a91f2006-01-11 22:42:35 +0100778 if (disabled_cpus > 0)
Andi Kleen420f8f62005-11-05 17:25:54 +0100779 additional_cpus = disabled_cpus;
Andi Kleenf62a91f2006-01-11 22:42:35 +0100780 else
781 additional_cpus = 0;
Andi Kleen420f8f62005-11-05 17:25:54 +0100782 }
783 possible = num_processors + additional_cpus;
784 if (possible > NR_CPUS)
785 possible = NR_CPUS;
786
787 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
788 possible,
789 max_t(int, possible - num_processors, 0));
790
791 for (i = 0; i < possible; i++)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700792 cpu_set(i, cpu_possible_map);
793}
794#endif
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700797 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
Ashok Raje6982c62005-06-25 14:54:58 -0700799static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
802 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
803 hard_smp_processor_id());
804 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
805 }
806
807 /*
808 * If we couldn't find an SMP configuration at boot time,
809 * get out of here now!
810 */
811 if (!smp_found_config) {
812 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700813 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (APIC_init_uniprocessor())
815 printk(KERN_NOTICE "Local APIC not detected."
816 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700817 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
820 /*
821 * Should not be necessary because the MP table should list the boot
822 * CPU too, but we do it for the sake of robustness anyway.
823 */
824 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
825 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
826 boot_cpu_id);
827 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
828 }
829
830 /*
831 * If we couldn't find a local APIC, then get out of here now!
832 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100833 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
835 boot_cpu_id);
836 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700837 nr_ioapics = 0;
838 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /*
842 * If SMP should be disabled, then really disable it!
843 */
844 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700846 nr_ioapics = 0;
847 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Andi Kleena8ab26f2005-04-16 15:25:19 -0700850 return 0;
851}
852
853/*
854 * Prepare for SMP bootup. The MP table or ACPI has been read
855 * earlier. Just do some sanity checking here and enable APIC mode.
856 */
Ashok Raje6982c62005-06-25 14:54:58 -0700857void __init smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700858{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700859 nmi_watchdog_default();
860 current_cpu_data = boot_cpu_data;
861 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100862 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700863
Andi Kleena8ab26f2005-04-16 15:25:19 -0700864 if (smp_sanity_check(max_cpus) < 0) {
865 printk(KERN_INFO "SMP disabled\n");
866 disable_smp();
867 return;
868 }
869
870
871 /*
872 * Switch from PIC to APIC mode.
873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 setup_local_APIC();
875
Andi Kleena8ab26f2005-04-16 15:25:19 -0700876 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
877 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
878 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
879 /* Or can we switch back to PIC here? */
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700883 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
885 if (!skip_ioapic_setup && nr_ioapics)
886 setup_IO_APIC();
887 else
888 nr_ioapics = 0;
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700891 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Andi Kleena8ab26f2005-04-16 15:25:19 -0700894 setup_boot_APIC_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Andi Kleena8ab26f2005-04-16 15:25:19 -0700897/*
898 * Early setup to make printk work.
899 */
900void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700902 int me = smp_processor_id();
903 cpu_set(me, cpu_online_map);
904 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700905 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Andi Kleena8ab26f2005-04-16 15:25:19 -0700908/*
909 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700910 */
911int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700913 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100914 unsigned long flags;
915 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Andi Kleena8ab26f2005-04-16 15:25:19 -0700917 WARN_ON(irqs_disabled());
918
919 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
920
921 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
922 !physid_isset(apicid, phys_cpu_present_map)) {
923 printk("__cpu_up: bad cpu %d\n", cpu);
924 return -EINVAL;
925 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700926
Ashok Raj76e4f662005-06-25 14:55:00 -0700927 /*
928 * Already booted CPU?
929 */
930 if (cpu_isset(cpu, cpu_callin_map)) {
931 Dprintk("do_boot_cpu %d Already started\n", cpu);
932 return -ENOSYS;
933 }
934
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200935 /*
936 * Save current MTRR state in case it was changed since early boot
937 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
938 */
939 mtrr_save_state();
940
Ashok Raj884d9e42005-06-25 14:55:02 -0700941 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700942 /* Boot it! */
943 err = do_boot_cpu(cpu, apicid);
944 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700945 Dprintk("do_boot_cpu failed %d\n", err);
946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /* Unleash the CPU! */
950 Dprintk("waiting for cpu %d\n", cpu);
951
Ingo Molnar95492e42007-02-16 01:27:34 -0800952 /*
953 * Make sure and check TSC sync:
954 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100955 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800956 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100957 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700960 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700961 err = 0;
962
963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Andi Kleena8ab26f2005-04-16 15:25:19 -0700966/*
967 * Finish the SMP boot.
968 */
Ashok Raje6982c62005-06-25 14:54:58 -0700969void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700971 smp_cleanup_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700973 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700974}
Ashok Raj76e4f662005-06-25 14:55:00 -0700975
976#ifdef CONFIG_HOTPLUG_CPU
977
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700978static void remove_siblinginfo(int cpu)
Ashok Raj76e4f662005-06-25 14:55:00 -0700979{
980 int sibling;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100981 struct cpuinfo_x86 *c = cpu_data;
Ashok Raj76e4f662005-06-25 14:55:00 -0700982
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100983 for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
984 cpu_clear(cpu, cpu_core_map[sibling]);
985 /*
986 * last thread sibling in this cpu core going down
987 */
988 if (cpus_weight(cpu_sibling_map[cpu]) == 1)
989 c[sibling].booted_cores--;
990 }
991
Ashok Raj76e4f662005-06-25 14:55:00 -0700992 for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
993 cpu_clear(cpu, cpu_sibling_map[sibling]);
Ashok Raj76e4f662005-06-25 14:55:00 -0700994 cpus_clear(cpu_sibling_map[cpu]);
995 cpus_clear(cpu_core_map[cpu]);
Rohit Sethf3fa8eb2006-06-26 13:58:17 +0200996 c[cpu].phys_proc_id = 0;
997 c[cpu].cpu_core_id = 0;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100998 cpu_clear(cpu, cpu_sibling_setup_map);
Ashok Raj76e4f662005-06-25 14:55:00 -0700999}
1000
1001void remove_cpu_from_maps(void)
1002{
1003 int cpu = smp_processor_id();
1004
1005 cpu_clear(cpu, cpu_callout_map);
1006 cpu_clear(cpu, cpu_callin_map);
1007 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -08001008 clear_node_cpumask(cpu);
Ashok Raj76e4f662005-06-25 14:55:00 -07001009}
1010
1011int __cpu_disable(void)
1012{
1013 int cpu = smp_processor_id();
1014
1015 /*
1016 * Perhaps use cpufreq to drop frequency, but that could go
1017 * into generic code.
1018 *
1019 * We won't take down the boot processor on i386 due to some
1020 * interrupts only being able to be serviced by the BSP.
1021 * Especially so if we're not using an IOAPIC -zwane
1022 */
1023 if (cpu == 0)
1024 return -EBUSY;
1025
Shaohua Li4038f902006-09-26 10:52:27 +02001026 if (nmi_watchdog == NMI_LOCAL_APIC)
1027 stop_apic_nmi_watchdog(NULL);
Shaohua Li5e9ef022005-12-12 22:17:08 -08001028 clear_local_APIC();
Ashok Raj76e4f662005-06-25 14:55:00 -07001029
1030 /*
1031 * HACK:
1032 * Allow any queued timer interrupts to get serviced
1033 * This is only a temporary solution until we cleanup
1034 * fixup_irqs as we do for IA64.
1035 */
1036 local_irq_enable();
1037 mdelay(1);
1038
1039 local_irq_disable();
1040 remove_siblinginfo(cpu);
1041
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001042 spin_lock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001043 /* It's now safe to remove this processor from the online map */
1044 cpu_clear(cpu, cpu_online_map);
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001045 spin_unlock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001046 remove_cpu_from_maps();
1047 fixup_irqs(cpu_online_map);
1048 return 0;
1049}
1050
1051void __cpu_die(unsigned int cpu)
1052{
1053 /* We don't do anything here: idle task is faking death itself. */
1054 unsigned int i;
1055
1056 for (i = 0; i < 10; i++) {
1057 /* They ack this in play_dead by setting CPU_DEAD */
Ashok Raj884d9e42005-06-25 14:55:02 -07001058 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1059 printk ("CPU %d is now offline\n", cpu);
Gerd Hoffmannd167a512006-06-26 13:56:16 +02001060 if (1 == num_online_cpus())
1061 alternatives_smp_switch(0);
Ashok Raj76e4f662005-06-25 14:55:00 -07001062 return;
Ashok Raj884d9e42005-06-25 14:55:02 -07001063 }
Nishanth Aravamudanef6e5252005-07-28 21:15:53 -07001064 msleep(100);
Ashok Raj76e4f662005-06-25 14:55:00 -07001065 }
1066 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
1067}
1068
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001069static __init int setup_additional_cpus(char *s)
Andi Kleen420f8f62005-11-05 17:25:54 +01001070{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001071 return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
Andi Kleen420f8f62005-11-05 17:25:54 +01001072}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001073early_param("additional_cpus", setup_additional_cpus);
Andi Kleen420f8f62005-11-05 17:25:54 +01001074
Ashok Raj76e4f662005-06-25 14:55:00 -07001075#else /* ... !CONFIG_HOTPLUG_CPU */
1076
1077int __cpu_disable(void)
1078{
1079 return -ENOSYS;
1080}
1081
1082void __cpu_die(unsigned int cpu)
1083{
1084 /* We said "no" in __cpu_disable */
1085 BUG();
1086}
1087#endif /* CONFIG_HOTPLUG_CPU */