blob: 500670c93d81d278e0402289803f5881946226b5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * x86 SMP booting functions
3 *
4 * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
5 * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
6 * Copyright 2001 Andi Kleen, SuSE Labs.
7 *
8 * Much of the core SMP work is based on previous work by Thomas Radke, to
9 * whom a great many thanks are extended.
10 *
11 * Thanks to Intel for making available several different Pentium,
12 * Pentium Pro and Pentium-II/Xeon MP machines.
13 * Original development of Linux SMP code supported by Caldera.
14 *
Andi Kleena8ab26f2005-04-16 15:25:19 -070015 * This code is released under the GNU General Public License version 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * Fixes
18 * Felix Koop : NR_CPUS used properly
19 * Jose Renau : Handle single CPU case.
20 * Alan Cox : By repeated request 8) - Total BogoMIP report.
21 * Greg Wright : Fix for kernel stacks panic.
22 * Erich Boleyn : MP v1.4 and additional changes.
23 * Matthias Sattler : Changes for 2.1 kernel map.
24 * Michel Lespinasse : Changes for 2.1 kernel map.
25 * Michael Chastain : Change trampoline.S to gnu as.
26 * Alan Cox : Dumb bug: 'B' step PPro's are fine
27 * Ingo Molnar : Added APIC timers, based on code
28 * from Jose Renau
29 * Ingo Molnar : various cleanups and rewrites
30 * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
31 * Maciej W. Rozycki : Bits for genuine 82489DX APICs
32 * Andi Kleen : Changed for SMP boot into long mode.
Andi Kleena8ab26f2005-04-16 15:25:19 -070033 * Rusty Russell : Hacked into shape for new "hotplug" boot process.
34 * Andi Kleen : Converted to new state machine.
35 * Various cleanups.
36 * Probably mostly hotplug CPU ready now.
Ashok Raj76e4f662005-06-25 14:55:00 -070037 * Ashok Raj : CPU hotplug support
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
Andi Kleena8ab26f2005-04-16 15:25:19 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/init.h>
42
43#include <linux/mm.h>
44#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/bootmem.h>
46#include <linux/thread_info.h>
47#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/delay.h>
49#include <linux/mc146818rtc.h>
Andrew Mortona3bc0db2006-09-25 23:32:33 -070050#include <linux/smp.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070051#include <linux/kdebug.h>
Andrew Mortona3bc0db2006-09-25 23:32:33 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/mtrr.h>
54#include <asm/pgalloc.h>
55#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/tlbflush.h>
57#include <asm/proto.h>
Andi Kleen75152112005-05-16 21:53:34 -070058#include <asm/nmi.h>
Al Viro9cdd3042005-09-12 18:49:25 +020059#include <asm/irq.h>
60#include <asm/hw_irq.h>
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -080061#include <asm/numa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* Number of siblings per CPU package */
64int smp_num_siblings = 1;
Andi Kleen2ee60e172006-06-26 13:59:44 +020065EXPORT_SYMBOL(smp_num_siblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -080067/* Last level cache ID of each logical CPU */
Mike Travisb6278472007-10-19 20:35:03 +020068DEFINE_PER_CPU(u8, cpu_llc_id) = BAD_APICID;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -080069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* Bitmask of currently online CPUs */
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070071cpumask_t cpu_online_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Andi Kleena8ab26f2005-04-16 15:25:19 -070073EXPORT_SYMBOL(cpu_online_map);
74
75/*
76 * Private maps to synchronize booting between AP and BP.
77 * Probably not needed anymore, but it makes for easier debugging. -AK
78 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079cpumask_t cpu_callin_map;
80cpumask_t cpu_callout_map;
Andi Kleen2ee60e172006-06-26 13:59:44 +020081EXPORT_SYMBOL(cpu_callout_map);
Andi Kleena8ab26f2005-04-16 15:25:19 -070082
83cpumask_t cpu_possible_map;
84EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* Per CPU bogomips and other parameters */
Mike Travis92cb7612007-10-19 20:35:04 +020087DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
88EXPORT_PER_CPU_SYMBOL(cpu_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Andi Kleena8ab26f2005-04-16 15:25:19 -070090/* Set when the idlers are all forked */
91int smp_threads_ready;
92
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010093/* representing HT siblings of each logical CPU */
Mike Travisd5a74302007-10-16 01:24:05 -070094DEFINE_PER_CPU(cpumask_t, cpu_sibling_map);
95EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +010096
97/* representing HT and core siblings of each logical CPU */
Mike Travis08357612007-10-16 01:24:04 -070098DEFINE_PER_CPU(cpumask_t, cpu_core_map);
99EXPORT_PER_CPU_SYMBOL(cpu_core_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
102 * Trampoline 80x86 program as an array.
103 */
104
Jan Beulich121d7bf2007-10-17 18:04:37 +0200105extern const unsigned char trampoline_data[];
106extern const unsigned char trampoline_end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Ashok Raj76e4f662005-06-25 14:55:00 -0700108/* State of each CPU */
109DEFINE_PER_CPU(int, cpu_state) = { 0 };
110
111/*
112 * Store all idle threads, this can be reused instead of creating
113 * a new thread. Also avoids complicated thread destroy functionality
114 * for idle threads.
115 */
116struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
117
118#define get_idle_for_cpu(x) (idle_thread_array[(x)])
119#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
120
121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * Currently trivial. Write the real->protected mode
123 * bootstrap into the page concerned. The caller
124 * has made sure it's suitably aligned.
125 */
126
Andi Kleena8ab26f2005-04-16 15:25:19 -0700127static unsigned long __cpuinit setup_trampoline(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 void *tramp = __va(SMP_TRAMPOLINE_BASE);
130 memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
131 return virt_to_phys(tramp);
132}
133
134/*
135 * The bootstrap kernel entry code has set these up. Save them for
136 * a given CPU
137 */
138
Andi Kleena8ab26f2005-04-16 15:25:19 -0700139static void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Mike Travis92cb7612007-10-19 20:35:04 +0200141 struct cpuinfo_x86 *c = &cpu_data(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 *c = boot_cpu_data;
Mike Travis92cb7612007-10-19 20:35:04 +0200144 c->cpu_index = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 identify_cpu(c);
Andi Kleendda50e72005-05-16 21:53:25 -0700146 print_cpu_info(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Andi Kleena8ab26f2005-04-16 15:25:19 -0700149static atomic_t init_deasserted __cpuinitdata;
150
151/*
152 * Report back to the Boot Processor.
153 * Running on AP.
154 */
155void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int cpuid, phys_id;
158 unsigned long timeout;
159
160 /*
161 * If waken up by an INIT in an 82489DX configuration
162 * we may get here before an INIT-deassert IPI reaches
163 * our local APIC. We have to wait for the IPI or we'll
164 * lock up on an APIC access.
165 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700166 while (!atomic_read(&init_deasserted))
167 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /*
170 * (This works even if the APIC is not enabled.)
171 */
172 phys_id = GET_APIC_ID(apic_read(APIC_ID));
173 cpuid = smp_processor_id();
174 if (cpu_isset(cpuid, cpu_callin_map)) {
175 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
176 phys_id, cpuid);
177 }
178 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
179
180 /*
181 * STARTUP IPIs are fragile beasts as they might sometimes
182 * trigger some glue motherboard logic. Complete APIC bus
183 * silence for 1 second, this overestimates the time the
184 * boot CPU is spending to send the up to 2 STARTUP IPIs
185 * by a factor of two. This should be enough.
186 */
187
188 /*
189 * Waiting 2s total for startup (udelay is not yet working)
190 */
191 timeout = jiffies + 2*HZ;
192 while (time_before(jiffies, timeout)) {
193 /*
194 * Has the boot CPU finished it's STARTUP sequence?
195 */
196 if (cpu_isset(cpuid, cpu_callout_map))
197 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700198 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
201 if (!time_before(jiffies, timeout)) {
202 panic("smp_callin: CPU%d started up but did not get a callout!\n",
203 cpuid);
204 }
205
206 /*
207 * the boot CPU has finished the init stage and is spinning
208 * on callin_map until we finish. We are free to set up this
209 * CPU, first the APIC. (this is probably redundant on most
210 * boards)
211 */
212
213 Dprintk("CALLIN, before setup_local_APIC().\n");
214 setup_local_APIC();
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /*
217 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200218 *
219 * Need to enable IRQs because it can take longer and then
220 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
Andi Kleenb4452212005-09-12 18:49:24 +0200222 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200224 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 Dprintk("Stack at about %p\n",&cpuid);
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /*
228 * Save our processor parameters
229 */
230 smp_store_cpu_info(cpuid);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /*
233 * Allow the master to continue.
234 */
235 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800238/* maps the cpu to the sched domain representing multi-core */
239cpumask_t cpu_coregroup_map(int cpu)
240{
Mike Travis92cb7612007-10-19 20:35:04 +0200241 struct cpuinfo_x86 *c = &cpu_data(cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800242 /*
243 * For perf, we return last level cache shared map.
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700244 * And for power savings, we return cpu_core_map
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800245 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700246 if (sched_mc_power_savings || sched_smt_power_savings)
Mike Travis08357612007-10-16 01:24:04 -0700247 return per_cpu(cpu_core_map, cpu);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700248 else
249 return c->llc_shared_map;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800250}
251
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100252/* representing cpus for which sibling maps can be computed */
253static cpumask_t cpu_sibling_setup_map;
254
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700255static inline void set_cpu_sibling_map(int cpu)
256{
257 int i;
Mike Travis92cb7612007-10-19 20:35:04 +0200258 struct cpuinfo_x86 *c = &cpu_data(cpu);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100259
260 cpu_set(cpu, cpu_sibling_setup_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700261
262 if (smp_num_siblings > 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100263 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Mike Travis92cb7612007-10-19 20:35:04 +0200264 if (c->phys_proc_id == cpu_data(i).phys_proc_id &&
265 c->cpu_core_id == cpu_data(i).cpu_core_id) {
Mike Travisd5a74302007-10-16 01:24:05 -0700266 cpu_set(i, per_cpu(cpu_sibling_map, cpu));
267 cpu_set(cpu, per_cpu(cpu_sibling_map, i));
Mike Travis08357612007-10-16 01:24:04 -0700268 cpu_set(i, per_cpu(cpu_core_map, cpu));
269 cpu_set(cpu, per_cpu(cpu_core_map, i));
Mike Travis92cb7612007-10-19 20:35:04 +0200270 cpu_set(i, c->llc_shared_map);
271 cpu_set(cpu, cpu_data(i).llc_shared_map);
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700272 }
273 }
274 } else {
Mike Travisd5a74302007-10-16 01:24:05 -0700275 cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700276 }
277
Mike Travis92cb7612007-10-19 20:35:04 +0200278 cpu_set(cpu, c->llc_shared_map);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800279
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100280 if (current_cpu_data.x86_max_cores == 1) {
Mike Travisd5a74302007-10-16 01:24:05 -0700281 per_cpu(cpu_core_map, cpu) = per_cpu(cpu_sibling_map, cpu);
Mike Travis92cb7612007-10-19 20:35:04 +0200282 c->booted_cores = 1;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100283 return;
284 }
285
286 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Mike Travisb6278472007-10-19 20:35:03 +0200287 if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
288 per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
Mike Travis92cb7612007-10-19 20:35:04 +0200289 cpu_set(i, c->llc_shared_map);
290 cpu_set(cpu, cpu_data(i).llc_shared_map);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800291 }
Mike Travis92cb7612007-10-19 20:35:04 +0200292 if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
Mike Travis08357612007-10-16 01:24:04 -0700293 cpu_set(i, per_cpu(cpu_core_map, cpu));
294 cpu_set(cpu, per_cpu(cpu_core_map, i));
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100295 /*
296 * Does this new cpu bringup a new core?
297 */
Mike Travisd5a74302007-10-16 01:24:05 -0700298 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100299 /*
300 * for each core in package, increment
301 * the booted_cores for this new cpu
302 */
Mike Travisd5a74302007-10-16 01:24:05 -0700303 if (first_cpu(per_cpu(cpu_sibling_map, i)) == i)
Mike Travis92cb7612007-10-19 20:35:04 +0200304 c->booted_cores++;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100305 /*
306 * increment the core count for all
307 * the other cpus in this package
308 */
309 if (i != cpu)
Mike Travis92cb7612007-10-19 20:35:04 +0200310 cpu_data(i).booted_cores++;
311 } else if (i != cpu && !c->booted_cores)
312 c->booted_cores = cpu_data(i).booted_cores;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100313 }
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700314 }
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700318 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700320void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 /*
323 * Dont put anything before smp_callin(), SMP
324 * booting is too fragile that we want to limit the
325 * things done here to the most necessary things.
326 */
327 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800328 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 smp_callin();
330
331 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
332 barrier();
333
Ingo Molnar95492e42007-02-16 01:27:34 -0800334 /*
335 * Check TSC sync first:
336 */
337 check_tsc_sync_target();
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (nmi_watchdog == NMI_IO_APIC) {
340 disable_8259A_irq(0);
341 enable_NMI_through_LVT0(NULL);
342 enable_8259A_irq(0);
343 }
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700346 * The sibling maps must be set before turing the online map on for
347 * this cpu
348 */
349 set_cpu_sibling_map(smp_processor_id());
350
351 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700352 * We need to hold call_lock, so there is no inconsistency
353 * between the time smp_call_function() determines number of
Simon Arlott676b1852007-10-20 01:25:36 +0200354 * IPI recipients, and the time when the determination is made
Ashok Raj884d9e42005-06-25 14:55:02 -0700355 * for which cpus receive the IPI in genapic_flat.c. Holding this
356 * lock helps us to not include this cpu in a currently in progress
357 * smp_call_function().
358 */
359 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200360 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700361
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200362 /* Setup the per cpu irq handling data structures */
363 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700364 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700365 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700368 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200369 spin_unlock(&vector_lock);
Ingo Molnar95492e42007-02-16 01:27:34 -0800370
Ashok Raj884d9e42005-06-25 14:55:02 -0700371 unlock_ipi_call_lock();
372
Thomas Gleixner3ac508b2007-10-14 22:57:45 +0200373 setup_secondary_APIC_clock();
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 cpu_idle();
376}
377
Andi Kleena8ab26f2005-04-16 15:25:19 -0700378extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379extern void (*initial_code)(void);
380
Olaf Hering44456d32005-07-27 11:45:17 -0700381#ifdef APIC_DEBUG
Andi Kleena8ab26f2005-04-16 15:25:19 -0700382static void inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
385 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200386 int timeout;
387 unsigned int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
390
Alejandro Martinez Ruiz4d022ad2007-10-17 14:38:58 +0200391 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 printk("... APIC #%d %s: ", apicid, names[i]);
393
394 /*
395 * Wait for idle.
396 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200397 status = safe_apic_wait_icr_idle();
398 if (status)
399 printk("a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Andi Kleenc1507eb2005-09-12 18:49:23 +0200401 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
402 apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 timeout = 0;
405 do {
406 udelay(100);
407 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
408 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
409
410 switch (status) {
411 case APIC_ICR_RR_VALID:
412 status = apic_read(APIC_RRR);
413 printk("%08x\n", status);
414 break;
415 default:
416 printk("failed\n");
417 }
418 }
419}
420#endif
421
Andi Kleena8ab26f2005-04-16 15:25:19 -0700422/*
423 * Kick the secondary to wake up.
424 */
425static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200427 unsigned long send_status, accept_status = 0;
428 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 Dprintk("Asserting INIT.\n");
431
432 /*
433 * Turn INIT on target chip
434 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200435 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /*
438 * Send IPI
439 */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200440 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 | APIC_DM_INIT);
442
443 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200444 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 mdelay(10);
447
448 Dprintk("Deasserting INIT.\n");
449
450 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200451 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 /* Send IPI */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200454 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200457 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100459 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 atomic_set(&init_deasserted, 1);
461
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200462 num_starts = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /*
465 * Run STARTUP IPI loop.
466 */
467 Dprintk("#startup loops: %d.\n", num_starts);
468
469 maxlvt = get_maxlvt();
470
471 for (j = 1; j <= num_starts; j++) {
472 Dprintk("Sending STARTUP #%d.\n",j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 apic_write(APIC_ESR, 0);
474 apic_read(APIC_ESR);
475 Dprintk("After apic_write.\n");
476
477 /*
478 * STARTUP IPI
479 */
480
481 /* Target chip */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200482 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 /* Boot on the stack */
485 /* Kick the second */
Andi Kleenc1507eb2005-09-12 18:49:23 +0200486 apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /*
489 * Give the other CPU some time to accept the IPI.
490 */
491 udelay(300);
492
493 Dprintk("Startup point 1.\n");
494
495 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200496 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
499 * Give the other CPU some time to accept the IPI.
500 */
501 udelay(200);
502 /*
503 * Due to the Pentium erratum 3AP.
504 */
505 if (maxlvt > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 apic_write(APIC_ESR, 0);
507 }
508 accept_status = (apic_read(APIC_ESR) & 0xEF);
509 if (send_status || accept_status)
510 break;
511 }
512 Dprintk("After Startup.\n");
513
514 if (send_status)
515 printk(KERN_ERR "APIC never delivered???\n");
516 if (accept_status)
517 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
518
519 return (send_status | accept_status);
520}
521
Ashok Raj76e4f662005-06-25 14:55:00 -0700522struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000523 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700524 struct task_struct *idle;
525 struct completion done;
526 int cpu;
527};
528
David Howells65f27f32006-11-22 14:55:48 +0000529void do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700530{
David Howells65f27f32006-11-22 14:55:48 +0000531 struct create_idle *c_idle =
532 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700533
534 c_idle->idle = fork_idle(c_idle->cpu);
535 complete(&c_idle->done);
536}
537
Andi Kleena8ab26f2005-04-16 15:25:19 -0700538/*
539 * Boot one CPU.
540 */
541static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700544 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700546 struct create_idle c_idle = {
David Howells65f27f32006-11-22 14:55:48 +0000547 .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
Ashok Raj76e4f662005-06-25 14:55:00 -0700548 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700549 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700550 };
Ashok Raj76e4f662005-06-25 14:55:00 -0700551
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100552 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
553 if (!cpu_gdt_descr[cpu].address &&
554 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
555 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
556 return -1;
557 }
558
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100559 /* Allocate node local memory for AP pdas */
560 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
561 struct x8664_pda *newpda, *pda;
562 int node = cpu_to_node(cpu);
563 pda = cpu_pda(cpu);
564 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
565 node);
566 if (newpda) {
567 memcpy(newpda, pda, sizeof (struct x8664_pda));
568 cpu_pda(cpu) = newpda;
569 } else
570 printk(KERN_ERR
571 "Could not allocate node local PDA for CPU %d on node %d\n",
572 cpu, node);
573 }
574
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200575 alternatives_smp_switch(1);
576
Ashok Raj76e4f662005-06-25 14:55:00 -0700577 c_idle.idle = get_idle_for_cpu(cpu);
578
579 if (c_idle.idle) {
580 c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800581 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700582 init_idle(c_idle.idle, cpu);
583 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Ashok Raj76e4f662005-06-25 14:55:00 -0700586 /*
587 * During cold boot process, keventd thread is not spun up yet.
588 * When we do cpu hot-add, we create idle threads on the fly, we should
589 * not acquire any attributes from the calling context. Hence the clean
590 * way to create kernel_threads() is to do that from keventd().
591 * We do the current_is_keventd() due to the fact that ACPI notifier
592 * was also queuing to keventd() and when the caller is already running
593 * in context of keventd(), we would end up with locking up the keventd
594 * thread.
595 */
596 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000597 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700598 else {
David Howells65f27f32006-11-22 14:55:48 +0000599 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700600 wait_for_completion(&c_idle.done);
601 }
602
603 if (IS_ERR(c_idle.idle)) {
604 printk("failed fork for CPU %d\n", cpu);
605 return PTR_ERR(c_idle.idle);
606 }
607
608 set_idle_for_cpu(cpu, c_idle.idle);
609
610do_rest:
611
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100612 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 start_rip = setup_trampoline();
615
Ashok Raj76e4f662005-06-25 14:55:00 -0700616 init_rsp = c_idle.idle->thread.rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 per_cpu(init_tss,cpu).rsp0 = init_rsp;
618 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800619 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Andi Kleende04f322005-07-28 21:15:29 -0700621 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
622 cpus_weight(cpu_present_map),
623 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /*
626 * This grunge runs the startup process for
627 * the targeted processor.
628 */
629
630 atomic_set(&init_deasserted, 0);
631
632 Dprintk("Setting warm reset code and vector.\n");
633
634 CMOS_WRITE(0xa, 0xf);
635 local_flush_tlb();
636 Dprintk("1.\n");
637 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
638 Dprintk("2.\n");
639 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
640 Dprintk("3.\n");
641
642 /*
643 * Be paranoid about clearing APIC errors.
644 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100645 apic_write(APIC_ESR, 0);
646 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /*
649 * Status is now clean
650 */
651 boot_error = 0;
652
653 /*
654 * Starting actual IPI sequence...
655 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700656 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (!boot_error) {
659 /*
660 * allow APs to start initializing.
661 */
662 Dprintk("Before Callout %d.\n", cpu);
663 cpu_set(cpu, cpu_callout_map);
664 Dprintk("After Callout %d.\n", cpu);
665
666 /*
667 * Wait 5s total for a response
668 */
669 for (timeout = 0; timeout < 50000; timeout++) {
670 if (cpu_isset(cpu, cpu_callin_map))
671 break; /* It has booted */
672 udelay(100);
673 }
674
675 if (cpu_isset(cpu, cpu_callin_map)) {
676 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 Dprintk("CPU has booted.\n");
678 } else {
679 boot_error = 1;
680 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
681 == 0xA5)
682 /* trampoline started but...? */
683 printk("Stuck ??\n");
684 else
685 /* trampoline code not run */
686 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700687#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 inquire_remote_apic(apicid);
689#endif
690 }
691 }
692 if (boot_error) {
693 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
694 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800695 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700696 cpu_clear(cpu, cpu_present_map);
697 cpu_clear(cpu, cpu_possible_map);
Mike Travis71fff5e2007-10-19 20:35:03 +0200698 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700699 return -EIO;
700 }
701
702 return 0;
703}
704
705cycles_t cacheflush_time;
706unsigned long cache_decay_ticks;
707
708/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700709 * Cleanup possible dangling ends...
710 */
711static __cpuinit void smp_cleanup_boot(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700714 * Paranoid: Set warm reset code and vector here back
715 * to default values.
716 */
717 CMOS_WRITE(0, 0xf);
718
719 /*
720 * Reset trampoline flag
721 */
722 *((volatile int *) phys_to_virt(0x467)) = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700723}
724
725/*
726 * Fall back to non SMP mode after errors.
727 *
728 * RED-PEN audit/test this more. I bet there is more state messed up here.
729 */
Ashok Raje6982c62005-06-25 14:54:58 -0700730static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700731{
732 cpu_present_map = cpumask_of_cpu(0);
733 cpu_possible_map = cpumask_of_cpu(0);
734 if (smp_found_config)
735 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
736 else
737 phys_cpu_present_map = physid_mask_of_physid(0);
Mike Travisd5a74302007-10-16 01:24:05 -0700738 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700739 cpu_set(0, per_cpu(cpu_core_map, 0));
Andi Kleena8ab26f2005-04-16 15:25:19 -0700740}
741
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700742#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen420f8f62005-11-05 17:25:54 +0100743
744int additional_cpus __initdata = -1;
745
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700746/*
747 * cpu_possible_map should be static, it cannot change as cpu's
748 * are onlined, or offlined. The reason is per-cpu data-structures
749 * are allocated by some modules at init time, and dont expect to
750 * do this dynamically on cpu arrival/departure.
751 * cpu_present_map on the other hand can change dynamically.
752 * In case when cpu_hotplug is not compiled, then we resort to current
753 * behaviour, which is cpu_possible == cpu_present.
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700754 * - Ashok Raj
Andi Kleen420f8f62005-11-05 17:25:54 +0100755 *
756 * Three ways to find out the number of additional hotplug CPUs:
757 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Andi Kleen420f8f62005-11-05 17:25:54 +0100758 * - The user can overwrite it with additional_cpus=NUM
Andi Kleenf62a91f2006-01-11 22:42:35 +0100759 * - Otherwise don't reserve additional CPUs.
Andi Kleen420f8f62005-11-05 17:25:54 +0100760 * We do this because additional CPUs waste a lot of memory.
761 * -AK
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700762 */
Andi Kleen421c7ce2005-10-10 22:32:45 +0200763__init void prefill_possible_map(void)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700764{
765 int i;
Andi Kleen420f8f62005-11-05 17:25:54 +0100766 int possible;
767
768 if (additional_cpus == -1) {
Andi Kleenf62a91f2006-01-11 22:42:35 +0100769 if (disabled_cpus > 0)
Andi Kleen420f8f62005-11-05 17:25:54 +0100770 additional_cpus = disabled_cpus;
Andi Kleenf62a91f2006-01-11 22:42:35 +0100771 else
772 additional_cpus = 0;
Andi Kleen420f8f62005-11-05 17:25:54 +0100773 }
774 possible = num_processors + additional_cpus;
775 if (possible > NR_CPUS)
776 possible = NR_CPUS;
777
778 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
779 possible,
780 max_t(int, possible - num_processors, 0));
781
782 for (i = 0; i < possible; i++)
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700783 cpu_set(i, cpu_possible_map);
784}
785#endif
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700788 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Ashok Raje6982c62005-06-25 14:54:58 -0700790static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
793 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
794 hard_smp_processor_id());
795 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
796 }
797
798 /*
799 * If we couldn't find an SMP configuration at boot time,
800 * get out of here now!
801 */
802 if (!smp_found_config) {
803 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700804 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (APIC_init_uniprocessor())
806 printk(KERN_NOTICE "Local APIC not detected."
807 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700808 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 /*
812 * Should not be necessary because the MP table should list the boot
813 * CPU too, but we do it for the sake of robustness anyway.
814 */
815 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
816 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
817 boot_cpu_id);
818 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
819 }
820
821 /*
822 * If we couldn't find a local APIC, then get out of here now!
823 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100824 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
826 boot_cpu_id);
827 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700828 nr_ioapics = 0;
829 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /*
833 * If SMP should be disabled, then really disable it!
834 */
835 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700837 nr_ioapics = 0;
838 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Andi Kleena8ab26f2005-04-16 15:25:19 -0700841 return 0;
842}
843
844/*
Mike Travis71fff5e2007-10-19 20:35:03 +0200845 * Copy apicid's found by MP_processor_info from initial array to the per cpu
846 * data area. The x86_cpu_to_apicid_init array is then expendable and the
847 * x86_cpu_to_apicid_ptr is zeroed indicating that the static array is no
848 * longer available.
849 */
850void __init smp_set_apicids(void)
851{
852 int cpu;
853
854 for_each_cpu_mask(cpu, cpu_possible_map) {
855 if (per_cpu_offset(cpu))
856 per_cpu(x86_cpu_to_apicid, cpu) =
857 x86_cpu_to_apicid_init[cpu];
858 }
859
860 /* indicate the static array will be going away soon */
861 x86_cpu_to_apicid_ptr = NULL;
862}
863
864/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700865 * Prepare for SMP bootup. The MP table or ACPI has been read
866 * earlier. Just do some sanity checking here and enable APIC mode.
867 */
Ashok Raje6982c62005-06-25 14:54:58 -0700868void __init smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700869{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700870 nmi_watchdog_default();
871 current_cpu_data = boot_cpu_data;
872 current_thread_info()->cpu = 0; /* needed? */
Mike Travis71fff5e2007-10-19 20:35:03 +0200873 smp_set_apicids();
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100874 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700875
Andi Kleena8ab26f2005-04-16 15:25:19 -0700876 if (smp_sanity_check(max_cpus) < 0) {
877 printk(KERN_INFO "SMP disabled\n");
878 disable_smp();
879 return;
880 }
881
882
883 /*
884 * Switch from PIC to APIC mode.
885 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 setup_local_APIC();
887
Andi Kleena8ab26f2005-04-16 15:25:19 -0700888 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
889 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
890 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
891 /* Or can we switch back to PIC here? */
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700895 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 */
897 if (!skip_ioapic_setup && nr_ioapics)
898 setup_IO_APIC();
899 else
900 nr_ioapics = 0;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700903 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Andi Kleena8ab26f2005-04-16 15:25:19 -0700906 setup_boot_APIC_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Andi Kleena8ab26f2005-04-16 15:25:19 -0700909/*
910 * Early setup to make printk work.
911 */
912void __init smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700914 int me = smp_processor_id();
915 cpu_set(me, cpu_online_map);
916 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700917 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Andi Kleena8ab26f2005-04-16 15:25:19 -0700920/*
921 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700922 */
923int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700925 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100926 unsigned long flags;
927 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Andi Kleena8ab26f2005-04-16 15:25:19 -0700929 WARN_ON(irqs_disabled());
930
931 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
932
933 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
934 !physid_isset(apicid, phys_cpu_present_map)) {
935 printk("__cpu_up: bad cpu %d\n", cpu);
936 return -EINVAL;
937 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700938
Ashok Raj76e4f662005-06-25 14:55:00 -0700939 /*
940 * Already booted CPU?
941 */
942 if (cpu_isset(cpu, cpu_callin_map)) {
943 Dprintk("do_boot_cpu %d Already started\n", cpu);
944 return -ENOSYS;
945 }
946
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200947 /*
948 * Save current MTRR state in case it was changed since early boot
949 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
950 */
951 mtrr_save_state();
952
Ashok Raj884d9e42005-06-25 14:55:02 -0700953 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700954 /* Boot it! */
955 err = do_boot_cpu(cpu, apicid);
956 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700957 Dprintk("do_boot_cpu failed %d\n", err);
958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* Unleash the CPU! */
962 Dprintk("waiting for cpu %d\n", cpu);
963
Ingo Molnar95492e42007-02-16 01:27:34 -0800964 /*
965 * Make sure and check TSC sync:
966 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100967 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800968 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100969 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700972 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700973 err = 0;
974
975 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Andi Kleena8ab26f2005-04-16 15:25:19 -0700978/*
979 * Finish the SMP boot.
980 */
Ashok Raje6982c62005-06-25 14:54:58 -0700981void __init smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700983 smp_cleanup_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700985 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700986}
Ashok Raj76e4f662005-06-25 14:55:00 -0700987
988#ifdef CONFIG_HOTPLUG_CPU
989
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700990static void remove_siblinginfo(int cpu)
Ashok Raj76e4f662005-06-25 14:55:00 -0700991{
992 int sibling;
Mike Travis92cb7612007-10-19 20:35:04 +0200993 struct cpuinfo_x86 *c = &cpu_data(cpu);
Ashok Raj76e4f662005-06-25 14:55:00 -0700994
Mike Travis08357612007-10-16 01:24:04 -0700995 for_each_cpu_mask(sibling, per_cpu(cpu_core_map, cpu)) {
996 cpu_clear(cpu, per_cpu(cpu_core_map, sibling));
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100997 /*
998 * last thread sibling in this cpu core going down
999 */
Mike Travisd5a74302007-10-16 01:24:05 -07001000 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1)
Mike Travis92cb7612007-10-19 20:35:04 +02001001 cpu_data(sibling).booted_cores--;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001002 }
1003
Mike Travisd5a74302007-10-16 01:24:05 -07001004 for_each_cpu_mask(sibling, per_cpu(cpu_sibling_map, cpu))
1005 cpu_clear(cpu, per_cpu(cpu_sibling_map, sibling));
1006 cpus_clear(per_cpu(cpu_sibling_map, cpu));
Mike Travis08357612007-10-16 01:24:04 -07001007 cpus_clear(per_cpu(cpu_core_map, cpu));
Mike Travis92cb7612007-10-19 20:35:04 +02001008 c->phys_proc_id = 0;
1009 c->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 */