blob: f2a234b390e8ad88af7a6be8dc445cce2c912c6a [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{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200432 unsigned long send_status, accept_status = 0;
433 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
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");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200449 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 mdelay(10);
452
453 Dprintk("Deasserting INIT.\n");
454
455 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200456 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* Send IPI */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200459 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200462 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100464 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 atomic_set(&init_deasserted, 1);
466
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200467 num_starts = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /*
470 * Run STARTUP IPI loop.
471 */
472 Dprintk("#startup loops: %d.\n", num_starts);
473
474 maxlvt = get_maxlvt();
475
476 for (j = 1; j <= num_starts; j++) {
477 Dprintk("Sending STARTUP #%d.\n",j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 apic_write(APIC_ESR, 0);
479 apic_read(APIC_ESR);
480 Dprintk("After apic_write.\n");
481
482 /*
483 * STARTUP IPI
484 */
485
486 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200487 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* Boot on the stack */
490 /* Kick the second */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200491 apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * Give the other CPU some time to accept the IPI.
495 */
496 udelay(300);
497
498 Dprintk("Startup point 1.\n");
499
500 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200501 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /*
504 * Give the other CPU some time to accept the IPI.
505 */
506 udelay(200);
507 /*
508 * Due to the Pentium erratum 3AP.
509 */
510 if (maxlvt > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 apic_write(APIC_ESR, 0);
512 }
513 accept_status = (apic_read(APIC_ESR) & 0xEF);
514 if (send_status || accept_status)
515 break;
516 }
517 Dprintk("After Startup.\n");
518
519 if (send_status)
520 printk(KERN_ERR "APIC never delivered???\n");
521 if (accept_status)
522 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
523
524 return (send_status | accept_status);
525}
526
Ashok Raj76e4f662005-06-25 14:55:00 -0700527struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000528 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700529 struct task_struct *idle;
530 struct completion done;
531 int cpu;
532};
533
David Howells65f27f32006-11-22 14:55:48 +0000534void do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700535{
David Howells65f27f32006-11-22 14:55:48 +0000536 struct create_idle *c_idle =
537 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700538
539 c_idle->idle = fork_idle(c_idle->cpu);
540 complete(&c_idle->done);
541}
542
Andi Kleena8ab26f2005-04-16 15:25:19 -0700543/*
544 * Boot one CPU.
545 */
546static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700549 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700551 struct create_idle c_idle = {
David Howells65f27f32006-11-22 14:55:48 +0000552 .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
Ashok Raj76e4f662005-06-25 14:55:00 -0700553 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700554 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700555 };
Ashok Raj76e4f662005-06-25 14:55:00 -0700556
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100557 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
558 if (!cpu_gdt_descr[cpu].address &&
559 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
560 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
561 return -1;
562 }
563
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100564 /* Allocate node local memory for AP pdas */
565 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
566 struct x8664_pda *newpda, *pda;
567 int node = cpu_to_node(cpu);
568 pda = cpu_pda(cpu);
569 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
570 node);
571 if (newpda) {
572 memcpy(newpda, pda, sizeof (struct x8664_pda));
573 cpu_pda(cpu) = newpda;
574 } else
575 printk(KERN_ERR
576 "Could not allocate node local PDA for CPU %d on node %d\n",
577 cpu, node);
578 }
579
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200580 alternatives_smp_switch(1);
581
Ashok Raj76e4f662005-06-25 14:55:00 -0700582 c_idle.idle = get_idle_for_cpu(cpu);
583
584 if (c_idle.idle) {
585 c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800586 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700587 init_idle(c_idle.idle, cpu);
588 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Ashok Raj76e4f662005-06-25 14:55:00 -0700591 /*
592 * During cold boot process, keventd thread is not spun up yet.
593 * When we do cpu hot-add, we create idle threads on the fly, we should
594 * not acquire any attributes from the calling context. Hence the clean
595 * way to create kernel_threads() is to do that from keventd().
596 * We do the current_is_keventd() due to the fact that ACPI notifier
597 * was also queuing to keventd() and when the caller is already running
598 * in context of keventd(), we would end up with locking up the keventd
599 * thread.
600 */
601 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000602 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700603 else {
David Howells65f27f32006-11-22 14:55:48 +0000604 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700605 wait_for_completion(&c_idle.done);
606 }
607
608 if (IS_ERR(c_idle.idle)) {
609 printk("failed fork for CPU %d\n", cpu);
610 return PTR_ERR(c_idle.idle);
611 }
612
613 set_idle_for_cpu(cpu, c_idle.idle);
614
615do_rest:
616
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100617 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 start_rip = setup_trampoline();
620
Ashok Raj76e4f662005-06-25 14:55:00 -0700621 init_rsp = c_idle.idle->thread.rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 per_cpu(init_tss,cpu).rsp0 = init_rsp;
623 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800624 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Andi Kleende04f322005-07-28 21:15:29 -0700626 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
627 cpus_weight(cpu_present_map),
628 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 /*
631 * This grunge runs the startup process for
632 * the targeted processor.
633 */
634
635 atomic_set(&init_deasserted, 0);
636
637 Dprintk("Setting warm reset code and vector.\n");
638
639 CMOS_WRITE(0xa, 0xf);
640 local_flush_tlb();
641 Dprintk("1.\n");
642 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
643 Dprintk("2.\n");
644 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
645 Dprintk("3.\n");
646
647 /*
648 * Be paranoid about clearing APIC errors.
649 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100650 apic_write(APIC_ESR, 0);
651 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /*
654 * Status is now clean
655 */
656 boot_error = 0;
657
658 /*
659 * Starting actual IPI sequence...
660 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700661 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (!boot_error) {
664 /*
665 * allow APs to start initializing.
666 */
667 Dprintk("Before Callout %d.\n", cpu);
668 cpu_set(cpu, cpu_callout_map);
669 Dprintk("After Callout %d.\n", cpu);
670
671 /*
672 * Wait 5s total for a response
673 */
674 for (timeout = 0; timeout < 50000; timeout++) {
675 if (cpu_isset(cpu, cpu_callin_map))
676 break; /* It has booted */
677 udelay(100);
678 }
679
680 if (cpu_isset(cpu, cpu_callin_map)) {
681 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 Dprintk("CPU has booted.\n");
683 } else {
684 boot_error = 1;
685 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
686 == 0xA5)
687 /* trampoline started but...? */
688 printk("Stuck ??\n");
689 else
690 /* trampoline code not run */
691 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700692#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 inquire_remote_apic(apicid);
694#endif
695 }
696 }
697 if (boot_error) {
698 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
699 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800700 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700701 cpu_clear(cpu, cpu_present_map);
702 cpu_clear(cpu, cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 x86_cpu_to_apicid[cpu] = BAD_APICID;
704 x86_cpu_to_log_apicid[cpu] = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700705 return -EIO;
706 }
707
708 return 0;
709}
710
711cycles_t cacheflush_time;
712unsigned long cache_decay_ticks;
713
714/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700715 * Cleanup possible dangling ends...
716 */
717static __cpuinit void smp_cleanup_boot(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700720 * Paranoid: Set warm reset code and vector here back
721 * to default values.
722 */
723 CMOS_WRITE(0, 0xf);
724
725 /*
726 * Reset trampoline flag
727 */
728 *((volatile int *) phys_to_virt(0x467)) = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700729}
730
731/*
732 * Fall back to non SMP mode after errors.
733 *
734 * RED-PEN audit/test this more. I bet there is more state messed up here.
735 */
Ashok Raje6982c62005-06-25 14:54:58 -0700736static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700737{
738 cpu_present_map = cpumask_of_cpu(0);
739 cpu_possible_map = cpumask_of_cpu(0);
740 if (smp_found_config)
741 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
742 else
743 phys_cpu_present_map = physid_mask_of_physid(0);
744 cpu_set(0, cpu_sibling_map[0]);
745 cpu_set(0, cpu_core_map[0]);
746}
747
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700748#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen420f8f62005-11-05 17:25:54 +0100749
750int additional_cpus __initdata = -1;
751
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700752/*
753 * cpu_possible_map should be static, it cannot change as cpu's
754 * are onlined, or offlined. The reason is per-cpu data-structures
755 * are allocated by some modules at init time, and dont expect to
756 * do this dynamically on cpu arrival/departure.
757 * cpu_present_map on the other hand can change dynamically.
758 * In case when cpu_hotplug is not compiled, then we resort to current
759 * behaviour, which is cpu_possible == cpu_present.
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700760 * - Ashok Raj
Andi Kleen420f8f62005-11-05 17:25:54 +0100761 *
762 * Three ways to find out the number of additional hotplug CPUs:
763 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Andi Kleen420f8f62005-11-05 17:25:54 +0100764 * - The user can overwrite it with additional_cpus=NUM
Andi Kleenf62a91f2006-01-11 22:42:35 +0100765 * - Otherwise don't reserve additional CPUs.
Andi Kleen420f8f62005-11-05 17:25:54 +0100766 * We do this because additional CPUs waste a lot of memory.
767 * -AK
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700768 */
Andi Kleen421c7ce2005-10-10 22:32:45 +0200769__init void prefill_possible_map(void)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700770{
771 int i;
Andi Kleen420f8f62005-11-05 17:25:54 +0100772 int possible;
773
774 if (additional_cpus == -1) {
Andi Kleenf62a91f2006-01-11 22:42:35 +0100775 if (disabled_cpus > 0)
Andi Kleen420f8f62005-11-05 17:25:54 +0100776 additional_cpus = disabled_cpus;
Andi Kleenf62a91f2006-01-11 22:42:35 +0100777 else
778 additional_cpus = 0;
Andi Kleen420f8f62005-11-05 17:25:54 +0100779 }
780 possible = num_processors + additional_cpus;
781 if (possible > NR_CPUS)
782 possible = NR_CPUS;
783
784 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
785 possible,
786 max_t(int, possible - num_processors, 0));
787
788 for (i = 0; i < possible; i++)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700789 cpu_set(i, cpu_possible_map);
790}
791#endif
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700794 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Ashok Raje6982c62005-06-25 14:54:58 -0700796static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
799 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
800 hard_smp_processor_id());
801 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
802 }
803
804 /*
805 * If we couldn't find an SMP configuration at boot time,
806 * get out of here now!
807 */
808 if (!smp_found_config) {
809 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700810 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (APIC_init_uniprocessor())
812 printk(KERN_NOTICE "Local APIC not detected."
813 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700814 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
817 /*
818 * Should not be necessary because the MP table should list the boot
819 * CPU too, but we do it for the sake of robustness anyway.
820 */
821 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
822 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
823 boot_cpu_id);
824 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
825 }
826
827 /*
828 * If we couldn't find a local APIC, then get out of here now!
829 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100830 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
832 boot_cpu_id);
833 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700834 nr_ioapics = 0;
835 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * If SMP should be disabled, then really disable it!
840 */
841 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700843 nr_ioapics = 0;
844 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
Andi Kleena8ab26f2005-04-16 15:25:19 -0700847 return 0;
848}
849
850/*
851 * Prepare for SMP bootup. The MP table or ACPI has been read
852 * earlier. Just do some sanity checking here and enable APIC mode.
853 */
Ashok Raje6982c62005-06-25 14:54:58 -0700854void __init smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700855{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700856 nmi_watchdog_default();
857 current_cpu_data = boot_cpu_data;
858 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100859 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700860
Andi Kleena8ab26f2005-04-16 15:25:19 -0700861 if (smp_sanity_check(max_cpus) < 0) {
862 printk(KERN_INFO "SMP disabled\n");
863 disable_smp();
864 return;
865 }
866
867
868 /*
869 * Switch from PIC to APIC mode.
870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 setup_local_APIC();
872
Andi Kleena8ab26f2005-04-16 15:25:19 -0700873 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
874 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
875 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
876 /* Or can we switch back to PIC here? */
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700880 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
882 if (!skip_ioapic_setup && nr_ioapics)
883 setup_IO_APIC();
884 else
885 nr_ioapics = 0;
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700888 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Andi Kleena8ab26f2005-04-16 15:25:19 -0700891 setup_boot_APIC_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Andi Kleena8ab26f2005-04-16 15:25:19 -0700894/*
895 * Early setup to make printk work.
896 */
897void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700899 int me = smp_processor_id();
900 cpu_set(me, cpu_online_map);
901 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700902 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Andi Kleena8ab26f2005-04-16 15:25:19 -0700905/*
906 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700907 */
908int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700910 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100911 unsigned long flags;
912 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Andi Kleena8ab26f2005-04-16 15:25:19 -0700914 WARN_ON(irqs_disabled());
915
916 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
917
918 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
919 !physid_isset(apicid, phys_cpu_present_map)) {
920 printk("__cpu_up: bad cpu %d\n", cpu);
921 return -EINVAL;
922 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700923
Ashok Raj76e4f662005-06-25 14:55:00 -0700924 /*
925 * Already booted CPU?
926 */
927 if (cpu_isset(cpu, cpu_callin_map)) {
928 Dprintk("do_boot_cpu %d Already started\n", cpu);
929 return -ENOSYS;
930 }
931
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200932 /*
933 * Save current MTRR state in case it was changed since early boot
934 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
935 */
936 mtrr_save_state();
937
Ashok Raj884d9e42005-06-25 14:55:02 -0700938 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700939 /* Boot it! */
940 err = do_boot_cpu(cpu, apicid);
941 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700942 Dprintk("do_boot_cpu failed %d\n", err);
943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* Unleash the CPU! */
947 Dprintk("waiting for cpu %d\n", cpu);
948
Ingo Molnar95492e42007-02-16 01:27:34 -0800949 /*
950 * Make sure and check TSC sync:
951 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100952 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800953 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100954 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700957 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700958 err = 0;
959
960 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Andi Kleena8ab26f2005-04-16 15:25:19 -0700963/*
964 * Finish the SMP boot.
965 */
Ashok Raje6982c62005-06-25 14:54:58 -0700966void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700968 smp_cleanup_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700970 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700971}
Ashok Raj76e4f662005-06-25 14:55:00 -0700972
973#ifdef CONFIG_HOTPLUG_CPU
974
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700975static void remove_siblinginfo(int cpu)
Ashok Raj76e4f662005-06-25 14:55:00 -0700976{
977 int sibling;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100978 struct cpuinfo_x86 *c = cpu_data;
Ashok Raj76e4f662005-06-25 14:55:00 -0700979
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100980 for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
981 cpu_clear(cpu, cpu_core_map[sibling]);
982 /*
983 * last thread sibling in this cpu core going down
984 */
985 if (cpus_weight(cpu_sibling_map[cpu]) == 1)
986 c[sibling].booted_cores--;
987 }
988
Ashok Raj76e4f662005-06-25 14:55:00 -0700989 for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
990 cpu_clear(cpu, cpu_sibling_map[sibling]);
Ashok Raj76e4f662005-06-25 14:55:00 -0700991 cpus_clear(cpu_sibling_map[cpu]);
992 cpus_clear(cpu_core_map[cpu]);
Rohit Sethf3fa8eb2006-06-26 13:58:17 +0200993 c[cpu].phys_proc_id = 0;
994 c[cpu].cpu_core_id = 0;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100995 cpu_clear(cpu, cpu_sibling_setup_map);
Ashok Raj76e4f662005-06-25 14:55:00 -0700996}
997
998void remove_cpu_from_maps(void)
999{
1000 int cpu = smp_processor_id();
1001
1002 cpu_clear(cpu, cpu_callout_map);
1003 cpu_clear(cpu, cpu_callin_map);
1004 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -08001005 clear_node_cpumask(cpu);
Ashok Raj76e4f662005-06-25 14:55:00 -07001006}
1007
1008int __cpu_disable(void)
1009{
1010 int cpu = smp_processor_id();
1011
1012 /*
1013 * Perhaps use cpufreq to drop frequency, but that could go
1014 * into generic code.
1015 *
1016 * We won't take down the boot processor on i386 due to some
1017 * interrupts only being able to be serviced by the BSP.
1018 * Especially so if we're not using an IOAPIC -zwane
1019 */
1020 if (cpu == 0)
1021 return -EBUSY;
1022
Shaohua Li4038f902006-09-26 10:52:27 +02001023 if (nmi_watchdog == NMI_LOCAL_APIC)
1024 stop_apic_nmi_watchdog(NULL);
Shaohua Li5e9ef022005-12-12 22:17:08 -08001025 clear_local_APIC();
Ashok Raj76e4f662005-06-25 14:55:00 -07001026
1027 /*
1028 * HACK:
1029 * Allow any queued timer interrupts to get serviced
1030 * This is only a temporary solution until we cleanup
1031 * fixup_irqs as we do for IA64.
1032 */
1033 local_irq_enable();
1034 mdelay(1);
1035
1036 local_irq_disable();
1037 remove_siblinginfo(cpu);
1038
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001039 spin_lock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001040 /* It's now safe to remove this processor from the online map */
1041 cpu_clear(cpu, cpu_online_map);
Eric W. Biederman70a0a532006-10-25 01:00:23 +02001042 spin_unlock(&vector_lock);
Ashok Raj76e4f662005-06-25 14:55:00 -07001043 remove_cpu_from_maps();
1044 fixup_irqs(cpu_online_map);
1045 return 0;
1046}
1047
1048void __cpu_die(unsigned int cpu)
1049{
1050 /* We don't do anything here: idle task is faking death itself. */
1051 unsigned int i;
1052
1053 for (i = 0; i < 10; i++) {
1054 /* They ack this in play_dead by setting CPU_DEAD */
Ashok Raj884d9e42005-06-25 14:55:02 -07001055 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1056 printk ("CPU %d is now offline\n", cpu);
Gerd Hoffmannd167a512006-06-26 13:56:16 +02001057 if (1 == num_online_cpus())
1058 alternatives_smp_switch(0);
Ashok Raj76e4f662005-06-25 14:55:00 -07001059 return;
Ashok Raj884d9e42005-06-25 14:55:02 -07001060 }
Nishanth Aravamudanef6e5252005-07-28 21:15:53 -07001061 msleep(100);
Ashok Raj76e4f662005-06-25 14:55:00 -07001062 }
1063 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
1064}
1065
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001066static __init int setup_additional_cpus(char *s)
Andi Kleen420f8f62005-11-05 17:25:54 +01001067{
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001068 return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
Andi Kleen420f8f62005-11-05 17:25:54 +01001069}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02001070early_param("additional_cpus", setup_additional_cpus);
Andi Kleen420f8f62005-11-05 17:25:54 +01001071
Ashok Raj76e4f662005-06-25 14:55:00 -07001072#else /* ... !CONFIG_HOTPLUG_CPU */
1073
1074int __cpu_disable(void)
1075{
1076 return -ENOSYS;
1077}
1078
1079void __cpu_die(unsigned int cpu)
1080{
1081 /* We said "no" in __cpu_disable */
1082 BUG();
1083}
1084#endif /* CONFIG_HOTPLUG_CPU */