blob: 62d828433c3015943fe307fff2b275b5c1e59791 [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>
Siddha, Suresh Bb0d0a4b2006-12-07 02:14:10 +010063#include <asm/genapic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* Number of siblings per CPU package */
66int smp_num_siblings = 1;
Andi Kleen2ee60e172006-06-26 13:59:44 +020067EXPORT_SYMBOL(smp_num_siblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -080069/* Last level cache ID of each logical CPU */
70u8 cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
Andi Kleen2ee60e172006-06-26 13:59:44 +020071EXPORT_SYMBOL(cpu_llc_id);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -080072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Bitmask of currently online CPUs */
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070074cpumask_t cpu_online_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Andi Kleena8ab26f2005-04-16 15:25:19 -070076EXPORT_SYMBOL(cpu_online_map);
77
78/*
79 * Private maps to synchronize booting between AP and BP.
80 * Probably not needed anymore, but it makes for easier debugging. -AK
81 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082cpumask_t cpu_callin_map;
83cpumask_t cpu_callout_map;
Andi Kleen2ee60e172006-06-26 13:59:44 +020084EXPORT_SYMBOL(cpu_callout_map);
Andi Kleena8ab26f2005-04-16 15:25:19 -070085
86cpumask_t cpu_possible_map;
87EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* Per CPU bogomips and other parameters */
90struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
Andi Kleen2ee60e172006-06-26 13:59:44 +020091EXPORT_SYMBOL(cpu_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Andi Kleena8ab26f2005-04-16 15:25:19 -070093/* Set when the idlers are all forked */
94int smp_threads_ready;
95
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010096/* representing HT siblings of each logical CPU */
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070097cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
Andi Kleen2ee60e172006-06-26 13:59:44 +020098EXPORT_SYMBOL(cpu_sibling_map);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010099
100/* representing HT and core siblings of each logical CPU */
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700101cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
Andi Kleen2df9fa32005-05-20 14:27:59 -0700102EXPORT_SYMBOL(cpu_core_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/*
105 * Trampoline 80x86 program as an array.
106 */
107
Andi Kleena8ab26f2005-04-16 15:25:19 -0700108extern unsigned char trampoline_data[];
109extern unsigned char trampoline_end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Ashok Raj76e4f662005-06-25 14:55:00 -0700111/* State of each CPU */
112DEFINE_PER_CPU(int, cpu_state) = { 0 };
113
114/*
115 * Store all idle threads, this can be reused instead of creating
116 * a new thread. Also avoids complicated thread destroy functionality
117 * for idle threads.
118 */
119struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
120
121#define get_idle_for_cpu(x) (idle_thread_array[(x)])
122#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
123
124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * Currently trivial. Write the real->protected mode
126 * bootstrap into the page concerned. The caller
127 * has made sure it's suitably aligned.
128 */
129
Andi Kleena8ab26f2005-04-16 15:25:19 -0700130static unsigned long __cpuinit setup_trampoline(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 void *tramp = __va(SMP_TRAMPOLINE_BASE);
133 memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
134 return virt_to_phys(tramp);
135}
136
137/*
138 * The bootstrap kernel entry code has set these up. Save them for
139 * a given CPU
140 */
141
Andi Kleena8ab26f2005-04-16 15:25:19 -0700142static void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct cpuinfo_x86 *c = cpu_data + id;
145
146 *c = boot_cpu_data;
147 identify_cpu(c);
Andi Kleendda50e72005-05-16 21:53:25 -0700148 print_cpu_info(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Andi Kleena8ab26f2005-04-16 15:25:19 -0700151static atomic_t init_deasserted __cpuinitdata;
152
153/*
154 * Report back to the Boot Processor.
155 * Running on AP.
156 */
157void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 int cpuid, phys_id;
160 unsigned long timeout;
161
162 /*
163 * If waken up by an INIT in an 82489DX configuration
164 * we may get here before an INIT-deassert IPI reaches
165 * our local APIC. We have to wait for the IPI or we'll
166 * lock up on an APIC access.
167 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700168 while (!atomic_read(&init_deasserted))
169 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /*
172 * (This works even if the APIC is not enabled.)
173 */
174 phys_id = GET_APIC_ID(apic_read(APIC_ID));
175 cpuid = smp_processor_id();
176 if (cpu_isset(cpuid, cpu_callin_map)) {
177 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
178 phys_id, cpuid);
179 }
180 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
181
182 /*
183 * STARTUP IPIs are fragile beasts as they might sometimes
184 * trigger some glue motherboard logic. Complete APIC bus
185 * silence for 1 second, this overestimates the time the
186 * boot CPU is spending to send the up to 2 STARTUP IPIs
187 * by a factor of two. This should be enough.
188 */
189
190 /*
191 * Waiting 2s total for startup (udelay is not yet working)
192 */
193 timeout = jiffies + 2*HZ;
194 while (time_before(jiffies, timeout)) {
195 /*
196 * Has the boot CPU finished it's STARTUP sequence?
197 */
198 if (cpu_isset(cpuid, cpu_callout_map))
199 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700200 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
203 if (!time_before(jiffies, timeout)) {
204 panic("smp_callin: CPU%d started up but did not get a callout!\n",
205 cpuid);
206 }
207
208 /*
209 * the boot CPU has finished the init stage and is spinning
210 * on callin_map until we finish. We are free to set up this
211 * CPU, first the APIC. (this is probably redundant on most
212 * boards)
213 */
214
215 Dprintk("CALLIN, before setup_local_APIC().\n");
216 setup_local_APIC();
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /*
219 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200220 *
221 * Need to enable IRQs because it can take longer and then
222 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Andi Kleenb4452212005-09-12 18:49:24 +0200224 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200226 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 Dprintk("Stack at about %p\n",&cpuid);
228
229 disable_APIC_timer();
230
231 /*
232 * Save our processor parameters
233 */
234 smp_store_cpu_info(cpuid);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /*
237 * Allow the master to continue.
238 */
239 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800242/* maps the cpu to the sched domain representing multi-core */
243cpumask_t cpu_coregroup_map(int cpu)
244{
245 struct cpuinfo_x86 *c = cpu_data + cpu;
246 /*
247 * For perf, we return last level cache shared map.
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700248 * And for power savings, we return cpu_core_map
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800249 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700250 if (sched_mc_power_savings || sched_smt_power_savings)
251 return cpu_core_map[cpu];
252 else
253 return c->llc_shared_map;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800254}
255
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100256/* representing cpus for which sibling maps can be computed */
257static cpumask_t cpu_sibling_setup_map;
258
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700259static inline void set_cpu_sibling_map(int cpu)
260{
261 int i;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100262 struct cpuinfo_x86 *c = cpu_data;
263
264 cpu_set(cpu, cpu_sibling_setup_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700265
266 if (smp_num_siblings > 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100267 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Rohit Sethf3fa8eb2006-06-26 13:58:17 +0200268 if (c[cpu].phys_proc_id == c[i].phys_proc_id &&
269 c[cpu].cpu_core_id == c[i].cpu_core_id) {
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700270 cpu_set(i, cpu_sibling_map[cpu]);
271 cpu_set(cpu, cpu_sibling_map[i]);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100272 cpu_set(i, cpu_core_map[cpu]);
273 cpu_set(cpu, cpu_core_map[i]);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800274 cpu_set(i, c[cpu].llc_shared_map);
275 cpu_set(cpu, c[i].llc_shared_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700276 }
277 }
278 } else {
279 cpu_set(cpu, cpu_sibling_map[cpu]);
280 }
281
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800282 cpu_set(cpu, c[cpu].llc_shared_map);
283
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100284 if (current_cpu_data.x86_max_cores == 1) {
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700285 cpu_core_map[cpu] = cpu_sibling_map[cpu];
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100286 c[cpu].booted_cores = 1;
287 return;
288 }
289
290 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800291 if (cpu_llc_id[cpu] != BAD_APICID &&
292 cpu_llc_id[cpu] == cpu_llc_id[i]) {
293 cpu_set(i, c[cpu].llc_shared_map);
294 cpu_set(cpu, c[i].llc_shared_map);
295 }
Rohit Sethf3fa8eb2006-06-26 13:58:17 +0200296 if (c[cpu].phys_proc_id == c[i].phys_proc_id) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100297 cpu_set(i, cpu_core_map[cpu]);
298 cpu_set(cpu, cpu_core_map[i]);
299 /*
300 * Does this new cpu bringup a new core?
301 */
302 if (cpus_weight(cpu_sibling_map[cpu]) == 1) {
303 /*
304 * for each core in package, increment
305 * the booted_cores for this new cpu
306 */
307 if (first_cpu(cpu_sibling_map[i]) == i)
308 c[cpu].booted_cores++;
309 /*
310 * increment the core count for all
311 * the other cpus in this package
312 */
313 if (i != cpu)
314 c[i].booted_cores++;
315 } else if (i != cpu && !c[cpu].booted_cores)
316 c[cpu].booted_cores = c[i].booted_cores;
317 }
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700318 }
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700322 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700324void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 /*
327 * Dont put anything before smp_callin(), SMP
328 * booting is too fragile that we want to limit the
329 * things done here to the most necessary things.
330 */
331 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800332 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 smp_callin();
334
335 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
336 barrier();
337
Ingo Molnar95492e42007-02-16 01:27:34 -0800338 /*
339 * Check TSC sync first:
340 */
341 check_tsc_sync_target();
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 Dprintk("cpu %d: setting up apic clock\n", smp_processor_id());
344 setup_secondary_APIC_clock();
345
Andi Kleena8ab26f2005-04-16 15:25:19 -0700346 Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (nmi_watchdog == NMI_IO_APIC) {
349 disable_8259A_irq(0);
350 enable_NMI_through_LVT0(NULL);
351 enable_8259A_irq(0);
352 }
353
Andi Kleena8ab26f2005-04-16 15:25:19 -0700354 enable_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700357 * The sibling maps must be set before turing the online map on for
358 * this cpu
359 */
360 set_cpu_sibling_map(smp_processor_id());
361
362 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700363 * We need to hold call_lock, so there is no inconsistency
364 * between the time smp_call_function() determines number of
365 * IPI receipients, and the time when the determination is made
366 * for which cpus receive the IPI in genapic_flat.c. Holding this
367 * lock helps us to not include this cpu in a currently in progress
368 * smp_call_function().
369 */
370 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200371 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700372
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200373 /* Setup the per cpu irq handling data structures */
374 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700375 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700376 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700379 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200380 spin_unlock(&vector_lock);
Ingo Molnar95492e42007-02-16 01:27:34 -0800381
Ashok Raj884d9e42005-06-25 14:55:02 -0700382 unlock_ipi_call_lock();
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 cpu_idle();
385}
386
Andi Kleena8ab26f2005-04-16 15:25:19 -0700387extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388extern void (*initial_code)(void);
389
Olaf Hering44456d32005-07-27 11:45:17 -0700390#ifdef APIC_DEBUG
Andi Kleena8ab26f2005-04-16 15:25:19 -0700391static void inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
394 char *names[] = { "ID", "VERSION", "SPIV" };
395 int timeout, status;
396
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 */
405 apic_wait_icr_idle();
406
Andi Kleenc1507eb2005-09-12 18:49:23 +0200407 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
408 apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 timeout = 0;
411 do {
412 udelay(100);
413 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
414 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
415
416 switch (status) {
417 case APIC_ICR_RR_VALID:
418 status = apic_read(APIC_RRR);
419 printk("%08x\n", status);
420 break;
421 default:
422 printk("failed\n");
423 }
424 }
425}
426#endif
427
Andi Kleena8ab26f2005-04-16 15:25:19 -0700428/*
429 * Kick the secondary to wake up.
430 */
431static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 unsigned long send_status = 0, accept_status = 0;
434 int maxlvt, timeout, num_starts, j;
435
436 Dprintk("Asserting INIT.\n");
437
438 /*
439 * Turn INIT on target chip
440 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200441 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /*
444 * Send IPI
445 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200446 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 | APIC_DM_INIT);
448
449 Dprintk("Waiting for send to finish...\n");
450 timeout = 0;
451 do {
452 Dprintk("+");
453 udelay(100);
454 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
455 } while (send_status && (timeout++ < 1000));
456
457 mdelay(10);
458
459 Dprintk("Deasserting INIT.\n");
460
461 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200462 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* Send IPI */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200465 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 Dprintk("Waiting for send to finish...\n");
468 timeout = 0;
469 do {
470 Dprintk("+");
471 udelay(100);
472 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
473 } while (send_status && (timeout++ < 1000));
474
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100475 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 atomic_set(&init_deasserted, 1);
477
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200478 num_starts = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 /*
481 * Run STARTUP IPI loop.
482 */
483 Dprintk("#startup loops: %d.\n", num_starts);
484
485 maxlvt = get_maxlvt();
486
487 for (j = 1; j <= num_starts; j++) {
488 Dprintk("Sending STARTUP #%d.\n",j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 apic_write(APIC_ESR, 0);
490 apic_read(APIC_ESR);
491 Dprintk("After apic_write.\n");
492
493 /*
494 * STARTUP IPI
495 */
496
497 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200498 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* Boot on the stack */
501 /* Kick the second */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200502 apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 /*
505 * Give the other CPU some time to accept the IPI.
506 */
507 udelay(300);
508
509 Dprintk("Startup point 1.\n");
510
511 Dprintk("Waiting for send to finish...\n");
512 timeout = 0;
513 do {
514 Dprintk("+");
515 udelay(100);
516 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
517 } while (send_status && (timeout++ < 1000));
518
519 /*
520 * Give the other CPU some time to accept the IPI.
521 */
522 udelay(200);
523 /*
524 * Due to the Pentium erratum 3AP.
525 */
526 if (maxlvt > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 apic_write(APIC_ESR, 0);
528 }
529 accept_status = (apic_read(APIC_ESR) & 0xEF);
530 if (send_status || accept_status)
531 break;
532 }
533 Dprintk("After Startup.\n");
534
535 if (send_status)
536 printk(KERN_ERR "APIC never delivered???\n");
537 if (accept_status)
538 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
539
540 return (send_status | accept_status);
541}
542
Ashok Raj76e4f662005-06-25 14:55:00 -0700543struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000544 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700545 struct task_struct *idle;
546 struct completion done;
547 int cpu;
548};
549
David Howells65f27f32006-11-22 14:55:48 +0000550void do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700551{
David Howells65f27f32006-11-22 14:55:48 +0000552 struct create_idle *c_idle =
553 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700554
555 c_idle->idle = fork_idle(c_idle->cpu);
556 complete(&c_idle->done);
557}
558
Andi Kleena8ab26f2005-04-16 15:25:19 -0700559/*
560 * Boot one CPU.
561 */
562static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700565 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700567 struct create_idle c_idle = {
David Howells65f27f32006-11-22 14:55:48 +0000568 .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
Ashok Raj76e4f662005-06-25 14:55:00 -0700569 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700570 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700571 };
Ashok Raj76e4f662005-06-25 14:55:00 -0700572
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100573 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
574 if (!cpu_gdt_descr[cpu].address &&
575 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
576 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
577 return -1;
578 }
579
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100580 /* Allocate node local memory for AP pdas */
581 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
582 struct x8664_pda *newpda, *pda;
583 int node = cpu_to_node(cpu);
584 pda = cpu_pda(cpu);
585 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
586 node);
587 if (newpda) {
588 memcpy(newpda, pda, sizeof (struct x8664_pda));
589 cpu_pda(cpu) = newpda;
590 } else
591 printk(KERN_ERR
592 "Could not allocate node local PDA for CPU %d on node %d\n",
593 cpu, node);
594 }
595
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200596 alternatives_smp_switch(1);
597
Ashok Raj76e4f662005-06-25 14:55:00 -0700598 c_idle.idle = get_idle_for_cpu(cpu);
599
600 if (c_idle.idle) {
601 c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800602 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700603 init_idle(c_idle.idle, cpu);
604 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Ashok Raj76e4f662005-06-25 14:55:00 -0700607 /*
608 * During cold boot process, keventd thread is not spun up yet.
609 * When we do cpu hot-add, we create idle threads on the fly, we should
610 * not acquire any attributes from the calling context. Hence the clean
611 * way to create kernel_threads() is to do that from keventd().
612 * We do the current_is_keventd() due to the fact that ACPI notifier
613 * was also queuing to keventd() and when the caller is already running
614 * in context of keventd(), we would end up with locking up the keventd
615 * thread.
616 */
617 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000618 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700619 else {
David Howells65f27f32006-11-22 14:55:48 +0000620 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700621 wait_for_completion(&c_idle.done);
622 }
623
624 if (IS_ERR(c_idle.idle)) {
625 printk("failed fork for CPU %d\n", cpu);
626 return PTR_ERR(c_idle.idle);
627 }
628
629 set_idle_for_cpu(cpu, c_idle.idle);
630
631do_rest:
632
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100633 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 start_rip = setup_trampoline();
636
Ashok Raj76e4f662005-06-25 14:55:00 -0700637 init_rsp = c_idle.idle->thread.rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 per_cpu(init_tss,cpu).rsp0 = init_rsp;
639 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800640 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Andi Kleende04f322005-07-28 21:15:29 -0700642 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
643 cpus_weight(cpu_present_map),
644 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /*
647 * This grunge runs the startup process for
648 * the targeted processor.
649 */
650
651 atomic_set(&init_deasserted, 0);
652
653 Dprintk("Setting warm reset code and vector.\n");
654
655 CMOS_WRITE(0xa, 0xf);
656 local_flush_tlb();
657 Dprintk("1.\n");
658 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
659 Dprintk("2.\n");
660 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
661 Dprintk("3.\n");
662
663 /*
664 * Be paranoid about clearing APIC errors.
665 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100666 apic_write(APIC_ESR, 0);
667 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /*
670 * Status is now clean
671 */
672 boot_error = 0;
673
674 /*
675 * Starting actual IPI sequence...
676 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700677 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (!boot_error) {
680 /*
681 * allow APs to start initializing.
682 */
683 Dprintk("Before Callout %d.\n", cpu);
684 cpu_set(cpu, cpu_callout_map);
685 Dprintk("After Callout %d.\n", cpu);
686
687 /*
688 * Wait 5s total for a response
689 */
690 for (timeout = 0; timeout < 50000; timeout++) {
691 if (cpu_isset(cpu, cpu_callin_map))
692 break; /* It has booted */
693 udelay(100);
694 }
695
696 if (cpu_isset(cpu, cpu_callin_map)) {
697 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 Dprintk("CPU has booted.\n");
699 } else {
700 boot_error = 1;
701 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
702 == 0xA5)
703 /* trampoline started but...? */
704 printk("Stuck ??\n");
705 else
706 /* trampoline code not run */
707 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700708#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 inquire_remote_apic(apicid);
710#endif
711 }
712 }
713 if (boot_error) {
714 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
715 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800716 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700717 cpu_clear(cpu, cpu_present_map);
718 cpu_clear(cpu, cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 x86_cpu_to_apicid[cpu] = BAD_APICID;
720 x86_cpu_to_log_apicid[cpu] = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700721 return -EIO;
722 }
723
724 return 0;
725}
726
727cycles_t cacheflush_time;
728unsigned long cache_decay_ticks;
729
730/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700731 * Cleanup possible dangling ends...
732 */
733static __cpuinit void smp_cleanup_boot(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700736 * Paranoid: Set warm reset code and vector here back
737 * to default values.
738 */
739 CMOS_WRITE(0, 0xf);
740
741 /*
742 * Reset trampoline flag
743 */
744 *((volatile int *) phys_to_virt(0x467)) = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700745}
746
747/*
748 * Fall back to non SMP mode after errors.
749 *
750 * RED-PEN audit/test this more. I bet there is more state messed up here.
751 */
Ashok Raje6982c62005-06-25 14:54:58 -0700752static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700753{
754 cpu_present_map = cpumask_of_cpu(0);
755 cpu_possible_map = cpumask_of_cpu(0);
756 if (smp_found_config)
757 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
758 else
759 phys_cpu_present_map = physid_mask_of_physid(0);
760 cpu_set(0, cpu_sibling_map[0]);
761 cpu_set(0, cpu_core_map[0]);
762}
763
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700764#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen420f8f62005-11-05 17:25:54 +0100765
766int additional_cpus __initdata = -1;
767
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700768/*
769 * cpu_possible_map should be static, it cannot change as cpu's
770 * are onlined, or offlined. The reason is per-cpu data-structures
771 * are allocated by some modules at init time, and dont expect to
772 * do this dynamically on cpu arrival/departure.
773 * cpu_present_map on the other hand can change dynamically.
774 * In case when cpu_hotplug is not compiled, then we resort to current
775 * behaviour, which is cpu_possible == cpu_present.
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700776 * - Ashok Raj
Andi Kleen420f8f62005-11-05 17:25:54 +0100777 *
778 * Three ways to find out the number of additional hotplug CPUs:
779 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Andi Kleen420f8f62005-11-05 17:25:54 +0100780 * - The user can overwrite it with additional_cpus=NUM
Andi Kleenf62a91f2006-01-11 22:42:35 +0100781 * - Otherwise don't reserve additional CPUs.
Andi Kleen420f8f62005-11-05 17:25:54 +0100782 * We do this because additional CPUs waste a lot of memory.
783 * -AK
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700784 */
Andi Kleen421c7ce2005-10-10 22:32:45 +0200785__init void prefill_possible_map(void)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700786{
787 int i;
Andi Kleen420f8f62005-11-05 17:25:54 +0100788 int possible;
789
790 if (additional_cpus == -1) {
Andi Kleenf62a91f2006-01-11 22:42:35 +0100791 if (disabled_cpus > 0)
Andi Kleen420f8f62005-11-05 17:25:54 +0100792 additional_cpus = disabled_cpus;
Andi Kleenf62a91f2006-01-11 22:42:35 +0100793 else
794 additional_cpus = 0;
Andi Kleen420f8f62005-11-05 17:25:54 +0100795 }
796 possible = num_processors + additional_cpus;
797 if (possible > NR_CPUS)
798 possible = NR_CPUS;
799
800 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
801 possible,
802 max_t(int, possible - num_processors, 0));
803
804 for (i = 0; i < possible; i++)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700805 cpu_set(i, cpu_possible_map);
806}
807#endif
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700810 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
Ashok Raje6982c62005-06-25 14:54:58 -0700812static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
815 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
816 hard_smp_processor_id());
817 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
818 }
819
820 /*
821 * If we couldn't find an SMP configuration at boot time,
822 * get out of here now!
823 */
824 if (!smp_found_config) {
825 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700826 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (APIC_init_uniprocessor())
828 printk(KERN_NOTICE "Local APIC not detected."
829 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700830 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 /*
834 * Should not be necessary because the MP table should list the boot
835 * CPU too, but we do it for the sake of robustness anyway.
836 */
837 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
838 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
839 boot_cpu_id);
840 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
841 }
842
843 /*
844 * If we couldn't find a local APIC, then get out of here now!
845 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100846 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
848 boot_cpu_id);
849 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700850 nr_ioapics = 0;
851 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /*
855 * If SMP should be disabled, then really disable it!
856 */
857 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700859 nr_ioapics = 0;
860 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
Andi Kleena8ab26f2005-04-16 15:25:19 -0700863 return 0;
864}
865
866/*
867 * Prepare for SMP bootup. The MP table or ACPI has been read
868 * earlier. Just do some sanity checking here and enable APIC mode.
869 */
Ashok Raje6982c62005-06-25 14:54:58 -0700870void __init smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700871{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700872 nmi_watchdog_default();
873 current_cpu_data = boot_cpu_data;
874 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100875 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700876
Andi Kleena8ab26f2005-04-16 15:25:19 -0700877 if (smp_sanity_check(max_cpus) < 0) {
878 printk(KERN_INFO "SMP disabled\n");
879 disable_smp();
880 return;
881 }
882
883
884 /*
885 * Switch from PIC to APIC mode.
886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 setup_local_APIC();
888
Andi Kleena8ab26f2005-04-16 15:25:19 -0700889 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
890 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
891 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
892 /* Or can we switch back to PIC here? */
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700896 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 */
898 if (!skip_ioapic_setup && nr_ioapics)
899 setup_IO_APIC();
900 else
901 nr_ioapics = 0;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700904 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Andi Kleena8ab26f2005-04-16 15:25:19 -0700907 setup_boot_APIC_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Andi Kleena8ab26f2005-04-16 15:25:19 -0700910/*
911 * Early setup to make printk work.
912 */
913void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700915 int me = smp_processor_id();
916 cpu_set(me, cpu_online_map);
917 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700918 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Andi Kleena8ab26f2005-04-16 15:25:19 -0700921/*
922 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700923 */
924int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700926 int err;
927 int apicid = cpu_present_to_apicid(cpu);
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
Ashok Raj884d9e42005-06-25 14:55:02 -0700947 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700948 /* Boot it! */
949 err = do_boot_cpu(cpu, apicid);
950 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700951 Dprintk("do_boot_cpu failed %d\n", err);
952 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* Unleash the CPU! */
956 Dprintk("waiting for cpu %d\n", cpu);
957
Ingo Molnar95492e42007-02-16 01:27:34 -0800958 /*
959 * Make sure and check TSC sync:
960 */
961 check_tsc_sync_source(cpu);
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700964 cpu_relax();
Siddha, Suresh Bb0d0a4b2006-12-07 02:14:10 +0100965
966 if (num_online_cpus() > 8 && genapic == &apic_flat) {
967 printk(KERN_WARNING
968 "flat APIC routing can't be used with > 8 cpus\n");
969 BUG();
970 }
971
Ashok Raj76e4f662005-06-25 14:55:00 -0700972 err = 0;
973
974 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Andi Kleena8ab26f2005-04-16 15:25:19 -0700977/*
978 * Finish the SMP boot.
979 */
Ashok Raje6982c62005-06-25 14:54:58 -0700980void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700982 smp_cleanup_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700984 check_nmi_watchdog();
Vojtech Pavlika670fad2006-09-26 10:52:28 +0200985 time_init_gtod();
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 */