blob: 0fbc98163b4ebf3fc86d543485df2c8d3cb25166 [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 *
7 * Much of the core SMP work is based on previous work by Thomas Radke, to
8 * whom a great many thanks are extended.
9 *
10 * Thanks to Intel for making available several different Pentium,
11 * Pentium Pro and Pentium-II/Xeon MP machines.
12 * Original development of Linux SMP code supported by Caldera.
13 *
14 * This code is released under the GNU General Public License version 2 or
15 * later.
16 *
17 * Fixes
18 * Felix Koop : NR_CPUS used properly
19 * Jose Renau : Handle single CPU case.
20 * Alan Cox : By repeated request 8) - Total BogoMIPS 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 * Martin J. Bligh : Added support for multi-quad systems
33 * Dave Jones : Report invalid combinations of Athlon CPUs.
34* Rusty Russell : Hacked into shape for new "hotplug" boot process. */
35
36#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/kernel.h>
39
40#include <linux/mm.h>
41#include <linux/sched.h>
42#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/bootmem.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070044#include <linux/notifier.h>
45#include <linux/cpu.h>
46#include <linux/percpu.h>
Ingo Molnard04f41e2007-03-07 18:12:31 +010047#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <linux/delay.h>
50#include <linux/mc146818rtc.h>
51#include <asm/tlbflush.h>
52#include <asm/desc.h>
53#include <asm/arch_hooks.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020054#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <mach_apic.h>
57#include <mach_wakecpu.h>
58#include <smpboot_hooks.h>
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010059#include <asm/vmi.h>
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +020060#include <asm/mtrr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* Set if we find a B stepping CPU */
Adrian Bunkf2206ec2007-12-19 23:20:18 +010063static int __cpuinitdata smp_b_stepping;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static cpumask_t smp_commenced_mask;
66
travis@sgi.com3b419082008-01-30 13:33:11 +010067/* which logical CPU number maps to which CPU (physical APIC ID) */
Glauber Costa5382e892008-03-03 14:12:57 -030068u16 x86_cpu_to_apicid_init[NR_CPUS] __initdata =
Mike Travis71fff5e2007-10-19 20:35:03 +020069 { [0 ... NR_CPUS-1] = BAD_APICID };
travis@sgi.com3b419082008-01-30 13:33:11 +010070void *x86_cpu_to_apicid_early_ptr;
Glauber Costa5382e892008-03-03 14:12:57 -030071DEFINE_PER_CPU(u16, x86_cpu_to_apicid) = BAD_APICID;
Mike Travis71fff5e2007-10-19 20:35:03 +020072EXPORT_PER_CPU_SYMBOL(x86_cpu_to_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
keith mannthey3b086062006-09-29 01:58:46 -070074u8 apicid_2_node[MAX_APICID];
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * Trampoline 80x86 program as an array.
78 */
79
Jan Beulich121d7bf2007-10-17 18:04:37 +020080extern const unsigned char trampoline_data [];
81extern const unsigned char trampoline_end [];
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static unsigned char *trampoline_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static void map_cpu_to_logical_apicid(void);
85
Zwane Mwaikambof3705132005-06-25 14:54:50 -070086/* State of each CPU. */
87DEFINE_PER_CPU(int, cpu_state) = { 0 };
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * Currently trivial. Write the real->protected mode
91 * bootstrap into the page concerned. The caller
92 * has made sure it's suitably aligned.
93 */
94
Adrian Bunk8957eca2007-10-17 18:04:32 +020095static unsigned long __cpuinit setup_trampoline(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
98 return virt_to_phys(trampoline_base);
99}
100
101/*
102 * We are called very early to get the low memory for the
103 * SMP bootup trampoline page.
104 */
105void __init smp_alloc_memory(void)
106{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100107 trampoline_base = alloc_bootmem_low_pages(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /*
109 * Has to be in very low memory so we can execute
110 * real-mode AP code.
111 */
112 if (__pa(trampoline_base) >= 0x9F000)
113 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/*
117 * The bootstrap kernel entry code has set these up. Save them for
118 * a given CPU
119 */
120
Jeremy Fitzhardinge724faa82007-07-17 18:37:03 -0700121void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Mike Travis92cb7612007-10-19 20:35:04 +0200123 struct cpuinfo_x86 *c = &cpu_data(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 *c = boot_cpu_data;
Mike Travis92cb7612007-10-19 20:35:04 +0200126 c->cpu_index = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (id!=0)
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +0200128 identify_secondary_cpu(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /*
130 * Mask B, Pentium, but not Pentium MMX
131 */
132 if (c->x86_vendor == X86_VENDOR_INTEL &&
133 c->x86 == 5 &&
134 c->x86_mask >= 1 && c->x86_mask <= 4 &&
135 c->x86_model <= 3)
136 /*
137 * Remember we have B step Pentia with bugs
138 */
139 smp_b_stepping = 1;
140
141 /*
142 * Certain Athlons might work (for various values of 'work') in SMP
143 * but they are not certified as MP capable.
144 */
145 if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
146
Dave Jones3ca113e2006-09-26 10:52:34 +0200147 if (num_possible_cpus() == 1)
148 goto valid_k7;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* Athlon 660/661 is valid. */
151 if ((c->x86_model==6) && ((c->x86_mask==0) || (c->x86_mask==1)))
152 goto valid_k7;
153
154 /* Duron 670 is valid */
155 if ((c->x86_model==7) && (c->x86_mask==0))
156 goto valid_k7;
157
158 /*
159 * Athlon 662, Duron 671, and Athlon >model 7 have capability bit.
160 * It's worth noting that the A5 stepping (662) of some Athlon XP's
161 * have the MP bit set.
162 * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for more.
163 */
164 if (((c->x86_model==6) && (c->x86_mask>=2)) ||
165 ((c->x86_model==7) && (c->x86_mask>=1)) ||
166 (c->x86_model> 7))
167 if (cpu_has_mp)
168 goto valid_k7;
169
170 /* If we get here, it's not a certified SMP capable AMD system. */
Randy Dunlap9f158332005-09-13 01:25:16 -0700171 add_taint(TAINT_UNSAFE_SMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
174valid_k7:
175 ;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static atomic_t init_deasserted;
179
Vivek Goyal4a5d1072007-01-11 01:52:44 +0100180static void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 int cpuid, phys_id;
183 unsigned long timeout;
184
185 /*
186 * If waken up by an INIT in an 82489DX configuration
187 * we may get here before an INIT-deassert IPI reaches
188 * our local APIC. We have to wait for the IPI or we'll
189 * lock up on an APIC access.
190 */
191 wait_for_init_deassert(&init_deasserted);
192
193 /*
194 * (This works even if the APIC is not enabled.)
195 */
196 phys_id = GET_APIC_ID(apic_read(APIC_ID));
197 cpuid = smp_processor_id();
198 if (cpu_isset(cpuid, cpu_callin_map)) {
199 printk("huh, phys CPU#%d, CPU#%d already present??\n",
200 phys_id, cpuid);
201 BUG();
202 }
203 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
204
205 /*
206 * STARTUP IPIs are fragile beasts as they might sometimes
207 * trigger some glue motherboard logic. Complete APIC bus
208 * silence for 1 second, this overestimates the time the
209 * boot CPU is spending to send the up to 2 STARTUP IPIs
210 * by a factor of two. This should be enough.
211 */
212
213 /*
214 * Waiting 2s total for startup (udelay is not yet working)
215 */
216 timeout = jiffies + 2*HZ;
217 while (time_before(jiffies, timeout)) {
218 /*
219 * Has the boot CPU finished it's STARTUP sequence?
220 */
221 if (cpu_isset(cpuid, cpu_callout_map))
222 break;
223 rep_nop();
224 }
225
226 if (!time_before(jiffies, timeout)) {
227 printk("BUG: CPU%d started up but did not get a callout!\n",
228 cpuid);
229 BUG();
230 }
231
232 /*
233 * the boot CPU has finished the init stage and is spinning
234 * on callin_map until we finish. We are free to set up this
235 * CPU, first the APIC. (this is probably redundant on most
236 * boards)
237 */
238
239 Dprintk("CALLIN, before setup_local_APIC().\n");
240 smp_callin_clear_local_apic();
241 setup_local_APIC();
242 map_cpu_to_logical_apicid();
243
244 /*
245 * Get our bogomips.
246 */
247 calibrate_delay();
248 Dprintk("Stack at about %p\n",&cpuid);
249
250 /*
251 * Save our processor parameters
252 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800253 smp_store_cpu_info(cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /*
256 * Allow the master to continue.
257 */
258 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261static int cpucount;
262
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800263/* maps the cpu to the sched domain representing multi-core */
264cpumask_t cpu_coregroup_map(int cpu)
265{
Mike Travis92cb7612007-10-19 20:35:04 +0200266 struct cpuinfo_x86 *c = &cpu_data(cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800267 /*
268 * For perf, we return last level cache shared map.
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700269 * And for power savings, we return cpu_core_map
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800270 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700271 if (sched_mc_power_savings || sched_smt_power_savings)
Mike Travis08357612007-10-16 01:24:04 -0700272 return per_cpu(cpu_core_map, cpu);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700273 else
274 return c->llc_shared_map;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800275}
276
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100277/* representing cpus for which sibling maps can be computed */
278static cpumask_t cpu_sibling_setup_map;
279
Andi Kleenfbab6e72007-07-22 11:12:33 +0200280void __cpuinit set_cpu_sibling_map(int cpu)
Li Shaohuad7208032005-06-25 14:54:54 -0700281{
282 int i;
Mike Travis92cb7612007-10-19 20:35:04 +0200283 struct cpuinfo_x86 *c = &cpu_data(cpu);
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100284
285 cpu_set(cpu, cpu_sibling_setup_map);
Li Shaohuad7208032005-06-25 14:54:54 -0700286
287 if (smp_num_siblings > 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100288 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Mike Travis92cb7612007-10-19 20:35:04 +0200289 if (c->phys_proc_id == cpu_data(i).phys_proc_id &&
290 c->cpu_core_id == cpu_data(i).cpu_core_id) {
Mike Travisd5a74302007-10-16 01:24:05 -0700291 cpu_set(i, per_cpu(cpu_sibling_map, cpu));
292 cpu_set(cpu, per_cpu(cpu_sibling_map, i));
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));
Mike Travis92cb7612007-10-19 20:35:04 +0200295 cpu_set(i, c->llc_shared_map);
296 cpu_set(cpu, cpu_data(i).llc_shared_map);
Li Shaohuad7208032005-06-25 14:54:54 -0700297 }
298 }
299 } else {
Mike Travisd5a74302007-10-16 01:24:05 -0700300 cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
Li Shaohuad7208032005-06-25 14:54:54 -0700301 }
302
Mike Travis92cb7612007-10-19 20:35:04 +0200303 cpu_set(cpu, c->llc_shared_map);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800304
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100305 if (current_cpu_data.x86_max_cores == 1) {
Mike Travisd5a74302007-10-16 01:24:05 -0700306 per_cpu(cpu_core_map, cpu) = per_cpu(cpu_sibling_map, cpu);
Mike Travis92cb7612007-10-19 20:35:04 +0200307 c->booted_cores = 1;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100308 return;
309 }
310
311 for_each_cpu_mask(i, cpu_sibling_setup_map) {
Mike Travisb6278472007-10-19 20:35:03 +0200312 if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
313 per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
Mike Travis92cb7612007-10-19 20:35:04 +0200314 cpu_set(i, c->llc_shared_map);
315 cpu_set(cpu, cpu_data(i).llc_shared_map);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -0800316 }
Mike Travis92cb7612007-10-19 20:35:04 +0200317 if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
Mike Travis08357612007-10-16 01:24:04 -0700318 cpu_set(i, per_cpu(cpu_core_map, cpu));
319 cpu_set(cpu, per_cpu(cpu_core_map, i));
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100320 /*
321 * Does this new cpu bringup a new core?
322 */
Mike Travisd5a74302007-10-16 01:24:05 -0700323 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1) {
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100324 /*
325 * for each core in package, increment
326 * the booted_cores for this new cpu
327 */
Mike Travisd5a74302007-10-16 01:24:05 -0700328 if (first_cpu(per_cpu(cpu_sibling_map, i)) == i)
Mike Travis92cb7612007-10-19 20:35:04 +0200329 c->booted_cores++;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100330 /*
331 * increment the core count for all
332 * the other cpus in this package
333 */
334 if (i != cpu)
Mike Travis92cb7612007-10-19 20:35:04 +0200335 cpu_data(i).booted_cores++;
336 } else if (i != cpu && !c->booted_cores)
337 c->booted_cores = cpu_data(i).booted_cores;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100338 }
Li Shaohuad7208032005-06-25 14:54:54 -0700339 }
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/*
343 * Activate a secondary processor.
344 */
Vivek Goyal4a5d1072007-01-11 01:52:44 +0100345static void __cpuinit start_secondary(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 /*
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200348 * Don't put *anything* before cpu_init(), SMP booting is too
349 * fragile that we want to limit the things done here to the
350 * most necessary things.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100352#ifdef CONFIG_VMI
353 vmi_bringup();
354#endif
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200355 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800356 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 smp_callin();
358 while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
359 rep_nop();
Ingo Molnar95492e42007-02-16 01:27:34 -0800360 /*
361 * Check TSC synchronization with the BP:
362 */
363 check_tsc_sync_target();
364
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100365 setup_secondary_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (nmi_watchdog == NMI_IO_APIC) {
367 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100368 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 enable_8259A_irq(0);
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /*
372 * low-memory mappings have been cleared, flush them from
373 * the local TLBs too.
374 */
375 local_flush_tlb();
Li Shaohua6fe940d2005-06-25 14:54:53 -0700376
Li Shaohuad7208032005-06-25 14:54:54 -0700377 /* This must be done before setting cpu_online_map */
378 set_cpu_sibling_map(raw_smp_processor_id());
379 wmb();
380
Li Shaohua6fe940d2005-06-25 14:54:53 -0700381 /*
382 * We need to hold call_lock, so there is no inconsistency
383 * between the time smp_call_function() determines number of
Simon Arlott27b46d72007-10-20 01:13:56 +0200384 * IPI recipients, and the time when the determination is made
Li Shaohua6fe940d2005-06-25 14:54:53 -0700385 * for which cpus receive the IPI. Holding this
386 * lock helps us to not include this cpu in a currently in progress
387 * smp_call_function().
388 */
389 lock_ipi_call_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 cpu_set(smp_processor_id(), cpu_online_map);
Li Shaohua6fe940d2005-06-25 14:54:53 -0700391 unlock_ipi_call_lock();
Li Shaohuae1367da2005-06-25 14:54:56 -0700392 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /* We can take interrupts now: we're officially "up". */
395 local_irq_enable();
396
397 wmb();
398 cpu_idle();
399}
400
401/*
402 * Everything has been set up for the secondary
403 * CPUs - they just need to reload everything
404 * from the task structure
405 * This function must not return.
406 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700407void __devinit initialize_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 /*
410 * We don't actually need to load the full TSS,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100411 * basically just the stack pointer and the ip.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
413
414 asm volatile(
415 "movl %0,%%esp\n\t"
416 "jmp *%1"
417 :
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100418 :"m" (current->thread.sp),"m" (current->thread.ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100421/* Static state in head.S used to set up a CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422extern struct {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100423 void * sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned short ss;
425} stack_start;
426
427#ifdef CONFIG_NUMA
428
429/* which logical CPUs are on which nodes */
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100430cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100432EXPORT_SYMBOL(node_to_cpumask_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/* which node each logical CPU is on */
travis@sgi.com602a54a2008-01-30 13:33:21 +0100434int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100435EXPORT_SYMBOL(cpu_to_node_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437/* set up a mapping between cpu and node. */
438static inline void map_cpu_to_node(int cpu, int node)
439{
440 printk("Mapping cpu %d to node %d\n", cpu, node);
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100441 cpu_set(cpu, node_to_cpumask_map[node]);
442 cpu_to_node_map[cpu] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445/* undo a mapping between cpu and node. */
446static inline void unmap_cpu_to_node(int cpu)
447{
448 int node;
449
450 printk("Unmapping cpu %d from all nodes\n", cpu);
451 for (node = 0; node < MAX_NUMNODES; node ++)
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100452 cpu_clear(cpu, node_to_cpumask_map[node]);
453 cpu_to_node_map[cpu] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455#else /* !CONFIG_NUMA */
456
457#define map_cpu_to_node(cpu, node) ({})
458#define unmap_cpu_to_node(cpu) ({})
459
460#endif /* CONFIG_NUMA */
461
Christoph Lameter6c036522005-07-07 17:56:59 -0700462u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464static void map_cpu_to_logical_apicid(void)
465{
466 int cpu = smp_processor_id();
467 int apicid = logical_smp_processor_id();
Keith Mannthey78b656b2006-10-03 18:25:52 -0700468 int node = apicid_to_node(apicid);
keith manntheybfa0e9a2006-09-25 16:25:35 -0700469
470 if (!node_online(node))
471 node = first_online_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 cpu_2_logical_apicid[cpu] = apicid;
keith manntheybfa0e9a2006-09-25 16:25:35 -0700474 map_cpu_to_node(cpu, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477static void unmap_cpu_to_logical_apicid(int cpu)
478{
479 cpu_2_logical_apicid[cpu] = BAD_APICID;
480 unmap_cpu_to_node(cpu);
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static inline void __inquire_remote_apic(int apicid)
484{
485 int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
486 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao4312fa82007-05-02 19:27:17 +0200487 int timeout;
488 unsigned long status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 printk("Inquiring remote APIC #%d...\n", apicid);
491
Tobias Klauser38e548e2005-11-07 00:58:31 -0800492 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 printk("... APIC #%d %s: ", apicid, names[i]);
494
495 /*
496 * Wait for idle.
497 */
Fernando Luis VazquezCao4312fa82007-05-02 19:27:17 +0200498 status = safe_apic_wait_icr_idle();
499 if (status)
500 printk("a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
503 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
504
505 timeout = 0;
506 do {
507 udelay(100);
508 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
509 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
510
511 switch (status) {
512 case APIC_ICR_RR_VALID:
513 status = apic_read(APIC_RRR);
Andi Kleenec1180d2007-05-02 19:27:21 +0200514 printk("%lx\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516 default:
517 printk("failed\n");
518 }
519 }
520}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522#ifdef WAKE_SECONDARY_VIA_NMI
523/*
524 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
525 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
526 * won't ... remember to clear down the APIC, etc later.
527 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700528static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
530{
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200531 unsigned long send_status, accept_status = 0;
532 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Target chip */
535 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
536
537 /* Boot on the stack */
538 /* Kick the second */
539 apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
540
541 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200542 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /*
545 * Give the other CPU some time to accept the IPI.
546 */
547 udelay(200);
548 /*
549 * Due to the Pentium erratum 3AP.
550 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800551 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (maxlvt > 3) {
553 apic_read_around(APIC_SPIV);
554 apic_write(APIC_ESR, 0);
555 }
556 accept_status = (apic_read(APIC_ESR) & 0xEF);
557 Dprintk("NMI sent.\n");
558
559 if (send_status)
560 printk("APIC never delivered???\n");
561 if (accept_status)
562 printk("APIC delivery error (%lx).\n", accept_status);
563
564 return (send_status | accept_status);
565}
566#endif /* WAKE_SECONDARY_VIA_NMI */
567
568#ifdef WAKE_SECONDARY_VIA_INIT
Li Shaohua0bb31842005-06-25 14:54:55 -0700569static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
571{
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200572 unsigned long send_status, accept_status = 0;
573 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /*
576 * Be paranoid about clearing APIC errors.
577 */
578 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
579 apic_read_around(APIC_SPIV);
580 apic_write(APIC_ESR, 0);
581 apic_read(APIC_ESR);
582 }
583
584 Dprintk("Asserting INIT.\n");
585
586 /*
587 * Turn INIT on target chip
588 */
589 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
590
591 /*
592 * Send IPI
593 */
594 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
595 | APIC_DM_INIT);
596
597 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200598 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 mdelay(10);
601
602 Dprintk("Deasserting INIT.\n");
603
604 /* Target chip */
605 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
606
607 /* Send IPI */
608 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
609
610 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200611 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 atomic_set(&init_deasserted, 1);
614
615 /*
616 * Should we send STARTUP IPIs ?
617 *
618 * Determine this based on the APIC version.
619 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
620 */
621 if (APIC_INTEGRATED(apic_version[phys_apicid]))
622 num_starts = 2;
623 else
624 num_starts = 0;
625
626 /*
Zachary Amsdenae5da272007-02-13 13:26:21 +0100627 * Paravirt / VMI wants a startup IPI hook here to set up the
628 * target processor state.
629 */
630 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100631 (unsigned long) stack_start.sp);
Zachary Amsdenae5da272007-02-13 13:26:21 +0100632
633 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * Run STARTUP IPI loop.
635 */
636 Dprintk("#startup loops: %d.\n", num_starts);
637
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800638 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 for (j = 1; j <= num_starts; j++) {
641 Dprintk("Sending STARTUP #%d.\n",j);
642 apic_read_around(APIC_SPIV);
643 apic_write(APIC_ESR, 0);
644 apic_read(APIC_ESR);
645 Dprintk("After apic_write.\n");
646
647 /*
648 * STARTUP IPI
649 */
650
651 /* Target chip */
652 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
653
654 /* Boot on the stack */
655 /* Kick the second */
656 apic_write_around(APIC_ICR, APIC_DM_STARTUP
657 | (start_eip >> 12));
658
659 /*
660 * Give the other CPU some time to accept the IPI.
661 */
662 udelay(300);
663
664 Dprintk("Startup point 1.\n");
665
666 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200667 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /*
670 * Give the other CPU some time to accept the IPI.
671 */
672 udelay(200);
673 /*
674 * Due to the Pentium erratum 3AP.
675 */
676 if (maxlvt > 3) {
677 apic_read_around(APIC_SPIV);
678 apic_write(APIC_ESR, 0);
679 }
680 accept_status = (apic_read(APIC_ESR) & 0xEF);
681 if (send_status || accept_status)
682 break;
683 }
684 Dprintk("After Startup.\n");
685
686 if (send_status)
687 printk("APIC never delivered???\n");
688 if (accept_status)
689 printk("APIC delivery error (%lx).\n", accept_status);
690
691 return (send_status | accept_status);
692}
693#endif /* WAKE_SECONDARY_VIA_INIT */
694
695extern cpumask_t cpu_initialized;
Li Shaohuae1367da2005-06-25 14:54:56 -0700696static inline int alloc_cpu_id(void)
697{
698 cpumask_t tmp_map;
699 int cpu;
700 cpus_complement(tmp_map, cpu_present_map);
701 cpu = first_cpu(tmp_map);
702 if (cpu >= NR_CPUS)
703 return -ENODEV;
704 return cpu;
705}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Li Shaohuae1367da2005-06-25 14:54:56 -0700707#ifdef CONFIG_HOTPLUG_CPU
Adrian Bunkf2206ec2007-12-19 23:20:18 +0100708static struct task_struct * __cpuinitdata cpu_idle_tasks[NR_CPUS];
709static inline struct task_struct * __cpuinit alloc_idle_task(int cpu)
Li Shaohuae1367da2005-06-25 14:54:56 -0700710{
711 struct task_struct *idle;
712
713 if ((idle = cpu_idle_tasks[cpu]) != NULL) {
714 /* initialize thread_struct. we really want to avoid destroy
715 * idle tread
716 */
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100717 idle->thread.sp = (unsigned long)task_pt_regs(idle);
Li Shaohuae1367da2005-06-25 14:54:56 -0700718 init_idle(idle, cpu);
719 return idle;
720 }
721 idle = fork_idle(cpu);
722
723 if (!IS_ERR(idle))
724 cpu_idle_tasks[cpu] = idle;
725 return idle;
726}
727#else
728#define alloc_idle_task(cpu) fork_idle(cpu)
729#endif
730
Vivek Goyal4a5d1072007-01-11 01:52:44 +0100731static int __cpuinit do_boot_cpu(int apicid, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/*
733 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
734 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
735 * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
736 */
737{
738 struct task_struct *idle;
739 unsigned long boot_error;
Li Shaohuae1367da2005-06-25 14:54:56 -0700740 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 unsigned long start_eip;
742 unsigned short nmi_high = 0, nmi_low = 0;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /*
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200745 * Save current MTRR state in case it was changed since early boot
746 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
747 */
748 mtrr_save_state();
749
750 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * We can't use kernel_thread since we must avoid to
752 * reschedule the child.
753 */
Li Shaohuae1367da2005-06-25 14:54:56 -0700754 idle = alloc_idle_task(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (IS_ERR(idle))
756 panic("failed fork for CPU %d", cpu);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100757
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200758 init_gdt(cpu);
759 per_cpu(current_task, cpu) = idle;
Rusty Russellbf5046722007-05-02 19:27:10 +0200760 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100761
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100762 idle->thread.ip = (unsigned long) start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* start_eip had better be page-aligned! */
764 start_eip = setup_trampoline();
765
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100766 ++cpucount;
767 alternatives_smp_switch(1);
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* So we see what's up */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100770 printk("Booting processor %d/%d ip %lx\n", cpu, apicid, start_eip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* Stack for startup_32 can be just as for start_secondary onwards */
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100772 stack_start.sp = (void *) idle->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 irq_ctx_init(cpu);
775
Mike Travis71fff5e2007-10-19 20:35:03 +0200776 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /*
778 * This grunge runs the startup process for
779 * the targeted processor.
780 */
781
782 atomic_set(&init_deasserted, 0);
783
784 Dprintk("Setting warm reset code and vector.\n");
785
786 store_NMI_vector(&nmi_high, &nmi_low);
787
788 smpboot_setup_warm_reset_vector(start_eip);
789
790 /*
791 * Starting actual IPI sequence...
792 */
793 boot_error = wakeup_secondary_cpu(apicid, start_eip);
794
795 if (!boot_error) {
796 /*
797 * allow APs to start initializing.
798 */
799 Dprintk("Before Callout %d.\n", cpu);
800 cpu_set(cpu, cpu_callout_map);
801 Dprintk("After Callout %d.\n", cpu);
802
803 /*
804 * Wait 5s total for a response
805 */
806 for (timeout = 0; timeout < 50000; timeout++) {
807 if (cpu_isset(cpu, cpu_callin_map))
808 break; /* It has booted */
809 udelay(100);
810 }
811
812 if (cpu_isset(cpu, cpu_callin_map)) {
813 /* number CPUs logically, starting from 1 (BSP is 0) */
814 Dprintk("OK.\n");
815 printk("CPU%d: ", cpu);
Mike Travis92cb7612007-10-19 20:35:04 +0200816 print_cpu_info(&cpu_data(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 Dprintk("CPU has booted.\n");
818 } else {
819 boot_error= 1;
820 if (*((volatile unsigned char *)trampoline_base)
821 == 0xA5)
822 /* trampoline started but...? */
823 printk("Stuck ??\n");
824 else
825 /* trampoline code not run */
826 printk("Not responding.\n");
827 inquire_remote_apic(apicid);
828 }
829 }
Li Shaohuae1367da2005-06-25 14:54:56 -0700830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (boot_error) {
832 /* Try to put things back the way they were before ... */
833 unmap_cpu_to_logical_apicid(cpu);
834 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
835 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
836 cpucount--;
Li Shaohuae1367da2005-06-25 14:54:56 -0700837 } else {
Mike Travis71fff5e2007-10-19 20:35:03 +0200838 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
Li Shaohuae1367da2005-06-25 14:54:56 -0700839 cpu_set(cpu, cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 /* mark "stuck" area as not stuck */
843 *((volatile unsigned long *)trampoline_base) = 0;
844
845 return boot_error;
846}
847
Li Shaohuae1367da2005-06-25 14:54:56 -0700848#ifdef CONFIG_HOTPLUG_CPU
849void cpu_exit_clear(void)
850{
851 int cpu = raw_smp_processor_id();
852
853 idle_task_exit();
854
855 cpucount --;
856 cpu_uninit();
857 irq_ctx_exit(cpu);
858
859 cpu_clear(cpu, cpu_callout_map);
860 cpu_clear(cpu, cpu_callin_map);
Li Shaohuae1367da2005-06-25 14:54:56 -0700861
862 cpu_clear(cpu, smp_commenced_mask);
863 unmap_cpu_to_logical_apicid(cpu);
864}
865
866struct warm_boot_cpu_info {
867 struct completion *complete;
David Howellsc4028952006-11-22 14:57:56 +0000868 struct work_struct task;
Li Shaohuae1367da2005-06-25 14:54:56 -0700869 int apicid;
870 int cpu;
871};
872
David Howellsc4028952006-11-22 14:57:56 +0000873static void __cpuinit do_warm_boot_cpu(struct work_struct *work)
Li Shaohuae1367da2005-06-25 14:54:56 -0700874{
David Howellsc4028952006-11-22 14:57:56 +0000875 struct warm_boot_cpu_info *info =
876 container_of(work, struct warm_boot_cpu_info, task);
Li Shaohuae1367da2005-06-25 14:54:56 -0700877 do_boot_cpu(info->apicid, info->cpu);
878 complete(info->complete);
879}
880
Ashok Raj34f361a2006-03-25 03:08:18 -0800881static int __cpuinit __smp_prepare_cpu(int cpu)
Li Shaohuae1367da2005-06-25 14:54:56 -0700882{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700883 DECLARE_COMPLETION_ONSTACK(done);
Li Shaohuae1367da2005-06-25 14:54:56 -0700884 struct warm_boot_cpu_info info;
Li Shaohuae1367da2005-06-25 14:54:56 -0700885 int apicid, ret;
886
Mike Travis71fff5e2007-10-19 20:35:03 +0200887 apicid = per_cpu(x86_cpu_to_apicid, cpu);
Li Shaohuae1367da2005-06-25 14:54:56 -0700888 if (apicid == BAD_APICID) {
889 ret = -ENODEV;
890 goto exit;
891 }
892
893 info.complete = &done;
894 info.apicid = apicid;
895 info.cpu = cpu;
David Howellsc4028952006-11-22 14:57:56 +0000896 INIT_WORK(&info.task, do_warm_boot_cpu);
Li Shaohuae1367da2005-06-25 14:54:56 -0700897
Li Shaohuae1367da2005-06-25 14:54:56 -0700898 /* init low mem mapping */
Zachary Amsdend7271b12005-09-03 15:56:50 -0700899 clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
Shaohua Li3b1bdf42006-12-08 02:41:13 -0800900 min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
Li Shaohuae1367da2005-06-25 14:54:56 -0700901 flush_tlb_all();
David Howellsc4028952006-11-22 14:57:56 +0000902 schedule_work(&info.task);
Li Shaohuae1367da2005-06-25 14:54:56 -0700903 wait_for_completion(&done);
904
Li Shaohuae1367da2005-06-25 14:54:56 -0700905 zap_low_mappings();
906 ret = 0;
907exit:
Li Shaohuae1367da2005-06-25 14:54:56 -0700908 return ret;
909}
910#endif
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912/*
913 * Cycle through the processors sending APIC IPIs to boot each.
914 */
915
916static int boot_cpu_logical_apicid;
917/* Where the IO area was mapped on multiquad, always 0 otherwise */
918void *xquad_portio;
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700919#ifdef CONFIG_X86_NUMAQ
920EXPORT_SYMBOL(xquad_portio);
921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923static void __init smp_boot_cpus(unsigned int max_cpus)
924{
925 int apicid, cpu, bit, kicked;
926 unsigned long bogosum = 0;
927
928 /*
929 * Setup boot CPU information
930 */
931 smp_store_cpu_info(0); /* Final full version of the data */
932 printk("CPU%d: ", 0);
Mike Travis92cb7612007-10-19 20:35:04 +0200933 print_cpu_info(&cpu_data(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800935 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 boot_cpu_logical_apicid = logical_smp_processor_id();
Mike Travis71fff5e2007-10-19 20:35:03 +0200937 per_cpu(x86_cpu_to_apicid, 0) = boot_cpu_physical_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 current_thread_info()->cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100941 set_cpu_sibling_map(0);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /*
944 * If we couldn't find an SMP configuration at boot time,
945 * get out of here now!
946 */
947 if (!smp_found_config && !acpi_lapic) {
948 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800949 smpboot_clear_io_apic_irqs();
950 phys_cpu_present_map = physid_mask_of_physid(0);
951 if (APIC_init_uniprocessor())
952 printk(KERN_NOTICE "Local APIC not detected."
953 " Using dummy APIC emulation.\n");
954 map_cpu_to_logical_apicid();
Mike Travisd5a74302007-10-16 01:24:05 -0700955 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700956 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return;
958 }
959
960 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800961 * Should not be necessary because the MP table should list the boot
962 * CPU too, but we do it for the sake of robustness anyway.
963 * Makes no sense to do this check in clustered apic mode, so skip it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800965 if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
966 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
967 boot_cpu_physical_apicid);
968 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
969 }
970
971 /*
972 * If we couldn't find a local APIC, then get out of here now!
973 */
974 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
975 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
976 boot_cpu_physical_apicid);
977 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
978 smpboot_clear_io_apic_irqs();
979 phys_cpu_present_map = physid_mask_of_physid(0);
Ingo Molnar54ffaa42007-10-19 20:35:02 +0200980 map_cpu_to_logical_apicid();
Mike Travisd5a74302007-10-16 01:24:05 -0700981 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700982 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return;
984 }
985
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800986 verify_local_APIC();
987
988 /*
989 * If SMP should be disabled, then really disable it!
990 */
991 if (!max_cpus) {
992 smp_found_config = 0;
993 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Ingo Molnar3fb450a2007-10-17 18:04:34 +0200994
995 if (nmi_watchdog == NMI_LOCAL_APIC) {
996 printk(KERN_INFO "activating minimal APIC for NMI watchdog use.\n");
997 connect_bsp_APIC();
998 setup_local_APIC();
999 }
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001000 smpboot_clear_io_apic_irqs();
1001 phys_cpu_present_map = physid_mask_of_physid(0);
Ingo Molnar54ffaa42007-10-19 20:35:02 +02001002 map_cpu_to_logical_apicid();
Mike Travisd5a74302007-10-16 01:24:05 -07001003 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -07001004 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001005 return;
1006 }
1007
1008 connect_bsp_APIC();
1009 setup_local_APIC();
1010 map_cpu_to_logical_apicid();
1011
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 setup_portio_remap();
1014
1015 /*
1016 * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
1017 *
1018 * In clustered apic mode, phys_cpu_present_map is a constructed thus:
1019 * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
1020 * clustered apic ID.
1021 */
1022 Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
1023
1024 kicked = 1;
1025 for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
1026 apicid = cpu_present_to_apicid(bit);
1027 /*
1028 * Don't even attempt to start the boot CPU!
1029 */
1030 if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
1031 continue;
1032
1033 if (!check_apicid_present(bit))
1034 continue;
1035 if (max_cpus <= cpucount+1)
1036 continue;
1037
Li Shaohuae1367da2005-06-25 14:54:56 -07001038 if (((cpu = alloc_cpu_id()) <= 0) || do_boot_cpu(apicid, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 printk("CPU #%d not responding - cannot use it.\n",
1040 apicid);
1041 else
1042 ++kicked;
1043 }
1044
1045 /*
1046 * Cleanup possible dangling ends...
1047 */
1048 smpboot_restore_warm_reset_vector();
1049
1050 /*
1051 * Allow the user to impress friends.
1052 */
1053 Dprintk("Before bogomips.\n");
Mike Travis7bf0c232008-01-30 13:30:55 +01001054 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (cpu_isset(cpu, cpu_callout_map))
Mike Travis92cb7612007-10-19 20:35:04 +02001056 bogosum += cpu_data(cpu).loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 printk(KERN_INFO
1058 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
1059 cpucount+1,
1060 bogosum/(500000/HZ),
1061 (bogosum/(5000/HZ))%100);
1062
1063 Dprintk("Before bogocount - setting activated=1.\n");
1064
1065 if (smp_b_stepping)
1066 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
1067
1068 /*
1069 * Don't taint if we are running SMP kernel on a single non-MP
1070 * approved Athlon
1071 */
1072 if (tainted & TAINT_UNSAFE_SMP) {
1073 if (cpucount)
1074 printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
1075 else
1076 tainted &= ~TAINT_UNSAFE_SMP;
1077 }
1078
1079 Dprintk("Boot done.\n");
1080
1081 /*
Mike Travisd5a74302007-10-16 01:24:05 -07001082 * construct cpu_sibling_map, so that we can tell sibling CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * efficiently.
1084 */
Mike Travis7bf0c232008-01-30 13:30:55 +01001085 for_each_possible_cpu(cpu) {
Mike Travisd5a74302007-10-16 01:24:05 -07001086 cpus_clear(per_cpu(cpu_sibling_map, cpu));
Mike Travis08357612007-10-16 01:24:04 -07001087 cpus_clear(per_cpu(cpu_core_map, cpu));
Andi Kleen3dd9d512005-04-16 15:25:15 -07001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Mike Travisd5a74302007-10-16 01:24:05 -07001090 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -07001091 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001093 smpboot_setup_io_apic();
1094
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001095 setup_boot_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
1098/* These are wrappers to interface to the new boot process. Someone
1099 who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +02001100void __init native_smp_prepare_cpus(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001102 smp_commenced_mask = cpumask_of_cpu(0);
1103 cpu_callin_map = cpumask_of_cpu(0);
1104 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 smp_boot_cpus(max_cpus);
1106}
1107
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +02001108void __init native_smp_prepare_boot_cpu(void)
Rusty Russellbf5046722007-05-02 19:27:10 +02001109{
1110 unsigned int cpu = smp_processor_id();
1111
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +02001112 init_gdt(cpu);
Rusty Russellbf5046722007-05-02 19:27:10 +02001113 switch_to_new_gdt();
1114
1115 cpu_set(cpu, cpu_online_map);
1116 cpu_set(cpu, cpu_callout_map);
1117 cpu_set(cpu, cpu_present_map);
1118 cpu_set(cpu, cpu_possible_map);
1119 __get_cpu_var(cpu_state) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001122#ifdef CONFIG_HOTPLUG_CPU
Jeremy Fitzhardingec70df742007-07-17 18:37:03 -07001123void remove_siblinginfo(int cpu)
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001124{
Li Shaohuae1367da2005-06-25 14:54:56 -07001125 int sibling;
Mike Travis92cb7612007-10-19 20:35:04 +02001126 struct cpuinfo_x86 *c = &cpu_data(cpu);
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001127
Mike Travis08357612007-10-16 01:24:04 -07001128 for_each_cpu_mask(sibling, per_cpu(cpu_core_map, cpu)) {
1129 cpu_clear(cpu, per_cpu(cpu_core_map, sibling));
1130 /*/
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001131 * last thread sibling in this cpu core going down
1132 */
Mike Travisd5a74302007-10-16 01:24:05 -07001133 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1)
Mike Travis92cb7612007-10-19 20:35:04 +02001134 cpu_data(sibling).booted_cores--;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001135 }
1136
Mike Travisd5a74302007-10-16 01:24:05 -07001137 for_each_cpu_mask(sibling, per_cpu(cpu_sibling_map, cpu))
1138 cpu_clear(cpu, per_cpu(cpu_sibling_map, sibling));
1139 cpus_clear(per_cpu(cpu_sibling_map, cpu));
Mike Travis08357612007-10-16 01:24:04 -07001140 cpus_clear(per_cpu(cpu_core_map, cpu));
Mike Travis92cb7612007-10-19 20:35:04 +02001141 c->phys_proc_id = 0;
1142 c->cpu_core_id = 0;
Siddha, Suresh B94605ef2005-11-05 17:25:54 +01001143 cpu_clear(cpu, cpu_sibling_setup_map);
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001144}
1145
1146int __cpu_disable(void)
1147{
1148 cpumask_t map = cpu_online_map;
1149 int cpu = smp_processor_id();
1150
1151 /*
1152 * Perhaps use cpufreq to drop frequency, but that could go
1153 * into generic code.
1154 *
1155 * We won't take down the boot processor on i386 due to some
1156 * interrupts only being able to be serviced by the BSP.
1157 * Especially so if we're not using an IOAPIC -zwane
1158 */
1159 if (cpu == 0)
1160 return -EBUSY;
Shaohua Li4038f902006-09-26 10:52:27 +02001161 if (nmi_watchdog == NMI_LOCAL_APIC)
1162 stop_apic_nmi_watchdog(NULL);
Shaohua Li5e9ef022005-12-12 22:17:08 -08001163 clear_local_APIC();
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001164 /* Allow any queued timer interrupts to get serviced */
1165 local_irq_enable();
1166 mdelay(1);
1167 local_irq_disable();
1168
Li Shaohuae1367da2005-06-25 14:54:56 -07001169 remove_siblinginfo(cpu);
1170
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001171 cpu_clear(cpu, map);
1172 fixup_irqs(map);
1173 /* It's now safe to remove this processor from the online map */
1174 cpu_clear(cpu, cpu_online_map);
1175 return 0;
1176}
1177
1178void __cpu_die(unsigned int cpu)
1179{
1180 /* We don't do anything here: idle task is faking death itself. */
1181 unsigned int i;
1182
1183 for (i = 0; i < 10; i++) {
1184 /* They ack this in play_dead by setting CPU_DEAD */
Li Shaohuae1367da2005-06-25 14:54:56 -07001185 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1186 printk ("CPU %d is now offline\n", cpu);
Gerd Hoffmann9a0b5812006-03-23 02:59:32 -08001187 if (1 == num_online_cpus())
1188 alternatives_smp_switch(0);
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001189 return;
Li Shaohuae1367da2005-06-25 14:54:56 -07001190 }
Nishanth Aravamudanaeb83972005-09-10 00:26:50 -07001191 msleep(100);
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001192 }
1193 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
1194}
1195#else /* ... !CONFIG_HOTPLUG_CPU */
1196int __cpu_disable(void)
1197{
1198 return -ENOSYS;
1199}
1200
1201void __cpu_die(unsigned int cpu)
1202{
1203 /* We said "no" in __cpu_disable */
1204 BUG();
1205}
1206#endif /* CONFIG_HOTPLUG_CPU */
1207
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +02001208int __cpuinit native_cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Ingo Molnard04f41e2007-03-07 18:12:31 +01001210 unsigned long flags;
Ashok Raj34f361a2006-03-25 03:08:18 -08001211#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnard04f41e2007-03-07 18:12:31 +01001212 int ret = 0;
Ashok Raj34f361a2006-03-25 03:08:18 -08001213
1214 /*
1215 * We do warm boot only on cpus that had booted earlier
1216 * Otherwise cold boot is all handled from smp_boot_cpus().
1217 * cpu_callin_map is set during AP kickstart process. Its reset
1218 * when a cpu is taken offline from cpu_exit_clear().
1219 */
1220 if (!cpu_isset(cpu, cpu_callin_map))
1221 ret = __smp_prepare_cpu(cpu);
1222
1223 if (ret)
1224 return -EIO;
1225#endif
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* In case one didn't come up */
1228 if (!cpu_isset(cpu, cpu_callin_map)) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001229 printk(KERN_DEBUG "skipping cpu%d, didn't come online\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -EIO;
1231 }
1232
Li Shaohuae1367da2005-06-25 14:54:56 -07001233 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /* Unleash the CPU! */
1235 cpu_set(cpu, smp_commenced_mask);
Ingo Molnar95492e42007-02-16 01:27:34 -08001236
1237 /*
Ingo Molnard04f41e2007-03-07 18:12:31 +01001238 * Check TSC synchronization with the AP (keep irqs disabled
1239 * while doing so):
Ingo Molnar95492e42007-02-16 01:27:34 -08001240 */
Ingo Molnard04f41e2007-03-07 18:12:31 +01001241 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -08001242 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +01001243 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -08001244
Ingo Molnard04f41e2007-03-07 18:12:31 +01001245 while (!cpu_isset(cpu, cpu_online_map)) {
Andreas Mohr18698912006-06-25 05:46:52 -07001246 cpu_relax();
Ingo Molnard04f41e2007-03-07 18:12:31 +01001247 touch_nmi_watchdog();
1248 }
Siddha, Suresh Bb0d0a4b2006-12-07 02:14:10 +01001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251}
1252
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +02001253void __init native_smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255#ifdef CONFIG_X86_IO_APIC
1256 setup_ioapic_dest();
1257#endif
1258 zap_low_mappings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
1261void __init smp_intr_init(void)
1262{
1263 /*
1264 * IRQ0 must be given a fixed assignment and initialized,
1265 * because it's used before the IO-APIC is set up.
1266 */
1267 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1268
1269 /*
1270 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1271 * IPI, driven by wakeup.
1272 */
1273 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1274
1275 /* IPI for invalidation */
1276 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1277
1278 /* IPI for generic function call */
1279 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1280}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001281
1282/*
1283 * If the BIOS enumerates physical processors before logical,
1284 * maxcpus=N at enumeration-time can be used to disable HT.
1285 */
1286static int __init parse_maxcpus(char *arg)
1287{
1288 extern unsigned int maxcpus;
1289
1290 maxcpus = simple_strtoul(arg, NULL, 0);
1291 return 0;
1292}
1293early_param("maxcpus", parse_maxcpus);