blob: 8a59fa80f8832cd58810b3e5a46c03232d946e9d [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
Glauber de Oliveira Costa8d770102008-03-19 14:25:31 -030063#include <mach_wakecpu.h>
Glauber de Oliveira Costaf6bc4022008-03-19 14:25:53 -030064#include <mach_apic.h>
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -030065#include <smpboot_hooks.h>
Glauber de Oliveira Costa8d770102008-03-19 14:25:31 -030066
Andi Kleena8ab26f2005-04-16 15:25:19 -070067/* Set when the idlers are all forked */
68int smp_threads_ready;
69
Ashok Raj76e4f662005-06-25 14:55:00 -070070/* State of each CPU */
71DEFINE_PER_CPU(int, cpu_state) = { 0 };
72
73/*
74 * Store all idle threads, this can be reused instead of creating
75 * a new thread. Also avoids complicated thread destroy functionality
76 * for idle threads.
77 */
travis@sgi.com24b0d222008-01-30 13:33:11 +010078#ifdef CONFIG_HOTPLUG_CPU
79/*
80 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
81 * removed after init for !CONFIG_HOTPLUG_CPU.
82 */
83static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
84#define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
85#define set_idle_for_cpu(x,p) (per_cpu(idle_thread_array, x) = (p))
86#else
Ashok Raj76e4f662005-06-25 14:55:00 -070087struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
Ashok Raj76e4f662005-06-25 14:55:00 -070088#define get_idle_for_cpu(x) (idle_thread_array[(x)])
89#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
travis@sgi.com24b0d222008-01-30 13:33:11 +010090#endif
91
Andi Kleena8ab26f2005-04-16 15:25:19 -070092static atomic_t init_deasserted __cpuinitdata;
93
Glauber de Oliveira Costaea0cadb2008-03-19 14:25:47 -030094#define smp_callin_clear_local_apic() do {} while (0)
95#define map_cpu_to_logical_apicid() do {} while (0)
96
Andi Kleena8ab26f2005-04-16 15:25:19 -070097/*
98 * Report back to the Boot Processor.
99 * Running on AP.
100 */
101void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 int cpuid, phys_id;
104 unsigned long timeout;
105
106 /*
107 * If waken up by an INIT in an 82489DX configuration
108 * we may get here before an INIT-deassert IPI reaches
109 * our local APIC. We have to wait for the IPI or we'll
110 * lock up on an APIC access.
111 */
Glauber Costae90009b2008-03-03 14:13:13 -0300112 wait_for_init_deassert(&init_deasserted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 /*
115 * (This works even if the APIC is not enabled.)
116 */
117 phys_id = GET_APIC_ID(apic_read(APIC_ID));
118 cpuid = smp_processor_id();
119 if (cpu_isset(cpuid, cpu_callin_map)) {
120 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
121 phys_id, cpuid);
122 }
123 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
124
125 /*
126 * STARTUP IPIs are fragile beasts as they might sometimes
127 * trigger some glue motherboard logic. Complete APIC bus
128 * silence for 1 second, this overestimates the time the
129 * boot CPU is spending to send the up to 2 STARTUP IPIs
130 * by a factor of two. This should be enough.
131 */
132
133 /*
134 * Waiting 2s total for startup (udelay is not yet working)
135 */
136 timeout = jiffies + 2*HZ;
137 while (time_before(jiffies, timeout)) {
138 /*
139 * Has the boot CPU finished it's STARTUP sequence?
140 */
141 if (cpu_isset(cpuid, cpu_callout_map))
142 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700143 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
146 if (!time_before(jiffies, timeout)) {
147 panic("smp_callin: CPU%d started up but did not get a callout!\n",
148 cpuid);
149 }
150
151 /*
152 * the boot CPU has finished the init stage and is spinning
153 * on callin_map until we finish. We are free to set up this
154 * CPU, first the APIC. (this is probably redundant on most
155 * boards)
156 */
157
158 Dprintk("CALLIN, before setup_local_APIC().\n");
Glauber de Oliveira Costaea0cadb2008-03-19 14:25:47 -0300159 smp_callin_clear_local_apic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 setup_local_APIC();
Andi Kleen739f33b2008-01-30 13:30:40 +0100161 end_local_APIC_setup();
Glauber de Oliveira Costaea0cadb2008-03-19 14:25:47 -0300162 map_cpu_to_logical_apicid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200166 *
167 * Need to enable IRQs because it can take longer and then
168 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Andi Kleenb4452212005-09-12 18:49:24 +0200170 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200172 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 Dprintk("Stack at about %p\n",&cpuid);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /*
176 * Save our processor parameters
177 */
178 smp_store_cpu_info(cpuid);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /*
181 * Allow the master to continue.
182 */
183 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700187 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700189void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 /*
192 * Dont put anything before smp_callin(), SMP
193 * booting is too fragile that we want to limit the
194 * things done here to the most necessary things.
195 */
196 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800197 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 smp_callin();
199
200 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
201 barrier();
202
Ingo Molnar95492e42007-02-16 01:27:34 -0800203 /*
204 * Check TSC sync first:
205 */
206 check_tsc_sync_target();
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (nmi_watchdog == NMI_IO_APIC) {
209 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100210 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 enable_8259A_irq(0);
212 }
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700215 * The sibling maps must be set before turing the online map on for
216 * this cpu
217 */
218 set_cpu_sibling_map(smp_processor_id());
219
220 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700221 * We need to hold call_lock, so there is no inconsistency
222 * between the time smp_call_function() determines number of
Simon Arlott676b1852007-10-20 01:25:36 +0200223 * IPI recipients, and the time when the determination is made
Ashok Raj884d9e42005-06-25 14:55:02 -0700224 * for which cpus receive the IPI in genapic_flat.c. Holding this
225 * lock helps us to not include this cpu in a currently in progress
226 * smp_call_function().
227 */
228 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200229 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700230
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200231 /* Setup the per cpu irq handling data structures */
232 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700233 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700234 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
Glauber Costafc25da92008-03-03 14:13:04 -0300236 spin_unlock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700238 unlock_ipi_call_lock();
239
Glauber de Oliveira Costa5733f622008-03-19 14:25:09 -0300240 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
241
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100242 setup_secondary_clock();
Thomas Gleixner3ac508b2007-10-14 22:57:45 +0200243
Glauber de Oliveira Costafa8004d2008-03-19 14:25:12 -0300244 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 cpu_idle();
246}
247
Andi Kleena8ab26f2005-04-16 15:25:19 -0700248extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249extern void (*initial_code)(void);
250
Olaf Hering44456d32005-07-27 11:45:17 -0700251#ifdef APIC_DEBUG
Glauber de Oliveira Costa8d770102008-03-19 14:25:31 -0300252static void __inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
255 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200256 int timeout;
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100257 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
260
Alejandro Martinez Ruiz4d022ad2007-10-17 14:38:58 +0200261 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100262 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
265 * Wait for idle.
266 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200267 status = safe_apic_wait_icr_idle();
268 if (status)
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100269 printk(KERN_CONT
270 "a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300272 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
273 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 timeout = 0;
276 do {
277 udelay(100);
278 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
279 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
280
281 switch (status) {
282 case APIC_ICR_RR_VALID:
283 status = apic_read(APIC_RRR);
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100284 printk(KERN_CONT "%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286 default:
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100287 printk(KERN_CONT "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 }
290}
291#endif
292
Andi Kleena8ab26f2005-04-16 15:25:19 -0700293/*
294 * Kick the secondary to wake up.
295 */
296static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200298 unsigned long send_status, accept_status = 0;
299 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300301 /*
302 * Be paranoid about clearing APIC errors.
303 */
304 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
305 apic_read_around(APIC_SPIV);
306 apic_write(APIC_ESR, 0);
307 apic_read(APIC_ESR);
308 }
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 Dprintk("Asserting INIT.\n");
311
312 /*
313 * Turn INIT on target chip
314 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300315 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /*
318 * Send IPI
319 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300320 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 | APIC_DM_INIT);
322
323 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200324 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 mdelay(10);
327
328 Dprintk("Deasserting INIT.\n");
329
330 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300331 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Send IPI */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300334 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200337 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100339 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 atomic_set(&init_deasserted, 1);
341
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300342 if (APIC_INTEGRATED(apic_version[phys_apicid]))
343 num_starts = 2;
344 else
345 num_starts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /*
Glauber de Oliveira Costad0173ae2008-03-19 14:24:59 -0300348 * Paravirt / VMI wants a startup IPI hook here to set up the
349 * target processor state.
350 */
351 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
352 (unsigned long) init_rsp);
353
354
355 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * Run STARTUP IPI loop.
357 */
358 Dprintk("#startup loops: %d.\n", num_starts);
359
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100360 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 for (j = 1; j <= num_starts; j++) {
363 Dprintk("Sending STARTUP #%d.\n",j);
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300364 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 apic_write(APIC_ESR, 0);
366 apic_read(APIC_ESR);
367 Dprintk("After apic_write.\n");
368
369 /*
370 * STARTUP IPI
371 */
372
373 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300374 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* Boot on the stack */
377 /* Kick the second */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300378 apic_write_around(APIC_ICR, APIC_DM_STARTUP | (start_rip>>12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /*
381 * Give the other CPU some time to accept the IPI.
382 */
383 udelay(300);
384
385 Dprintk("Startup point 1.\n");
386
387 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200388 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /*
391 * Give the other CPU some time to accept the IPI.
392 */
393 udelay(200);
394 /*
395 * Due to the Pentium erratum 3AP.
396 */
397 if (maxlvt > 3) {
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300398 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 apic_write(APIC_ESR, 0);
400 }
401 accept_status = (apic_read(APIC_ESR) & 0xEF);
402 if (send_status || accept_status)
403 break;
404 }
405 Dprintk("After Startup.\n");
406
407 if (send_status)
408 printk(KERN_ERR "APIC never delivered???\n");
409 if (accept_status)
410 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
411
412 return (send_status | accept_status);
413}
414
Ashok Raj76e4f662005-06-25 14:55:00 -0700415struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000416 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700417 struct task_struct *idle;
418 struct completion done;
419 int cpu;
420};
421
Thomas Gleixnera2b484a2008-01-09 00:18:28 +0100422static void __cpuinit do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700423{
David Howells65f27f32006-11-22 14:55:48 +0000424 struct create_idle *c_idle =
425 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700426
427 c_idle->idle = fork_idle(c_idle->cpu);
428 complete(&c_idle->done);
429}
430
Andi Kleena8ab26f2005-04-16 15:25:19 -0700431/*
432 * Boot one CPU.
433 */
434static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Glauber de Oliveira Costa6becedb2008-03-19 14:25:51 -0300436 unsigned long boot_error = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700437 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700439 struct create_idle c_idle = {
440 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700441 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700442 };
Glauber Costa2b775a22008-02-22 12:09:29 -0300443 INIT_WORK(&c_idle.work, do_fork_idle);
Ashok Raj76e4f662005-06-25 14:55:00 -0700444
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100445 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
446 if (!cpu_gdt_descr[cpu].address &&
447 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
448 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
449 return -1;
450 }
451
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100452 /* Allocate node local memory for AP pdas */
453 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
454 struct x8664_pda *newpda, *pda;
455 int node = cpu_to_node(cpu);
456 pda = cpu_pda(cpu);
457 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
458 node);
459 if (newpda) {
460 memcpy(newpda, pda, sizeof (struct x8664_pda));
461 cpu_pda(cpu) = newpda;
462 } else
463 printk(KERN_ERR
464 "Could not allocate node local PDA for CPU %d on node %d\n",
465 cpu, node);
466 }
467
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200468 alternatives_smp_switch(1);
469
Ashok Raj76e4f662005-06-25 14:55:00 -0700470 c_idle.idle = get_idle_for_cpu(cpu);
471
472 if (c_idle.idle) {
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100473 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800474 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700475 init_idle(c_idle.idle, cpu);
476 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Ashok Raj76e4f662005-06-25 14:55:00 -0700479 /*
480 * During cold boot process, keventd thread is not spun up yet.
481 * When we do cpu hot-add, we create idle threads on the fly, we should
482 * not acquire any attributes from the calling context. Hence the clean
483 * way to create kernel_threads() is to do that from keventd().
484 * We do the current_is_keventd() due to the fact that ACPI notifier
485 * was also queuing to keventd() and when the caller is already running
486 * in context of keventd(), we would end up with locking up the keventd
487 * thread.
488 */
489 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000490 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700491 else {
David Howells65f27f32006-11-22 14:55:48 +0000492 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700493 wait_for_completion(&c_idle.done);
494 }
495
496 if (IS_ERR(c_idle.idle)) {
497 printk("failed fork for CPU %d\n", cpu);
498 return PTR_ERR(c_idle.idle);
499 }
500
501 set_idle_for_cpu(cpu, c_idle.idle);
502
503do_rest:
504
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100505 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 start_rip = setup_trampoline();
508
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100509 init_rsp = c_idle.idle->thread.sp;
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100510 load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800512 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Andi Kleende04f322005-07-28 21:15:29 -0700514 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
515 cpus_weight(cpu_present_map),
516 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /*
519 * This grunge runs the startup process for
520 * the targeted processor.
521 */
522
523 atomic_set(&init_deasserted, 0);
524
525 Dprintk("Setting warm reset code and vector.\n");
526
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -0300527 smpboot_setup_warm_reset_vector(start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /*
529 * Be paranoid about clearing APIC errors.
530 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100531 apic_write(APIC_ESR, 0);
532 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * Starting actual IPI sequence...
536 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700537 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if (!boot_error) {
540 /*
541 * allow APs to start initializing.
542 */
543 Dprintk("Before Callout %d.\n", cpu);
544 cpu_set(cpu, cpu_callout_map);
545 Dprintk("After Callout %d.\n", cpu);
546
547 /*
548 * Wait 5s total for a response
549 */
550 for (timeout = 0; timeout < 50000; timeout++) {
551 if (cpu_isset(cpu, cpu_callin_map))
552 break; /* It has booted */
553 udelay(100);
554 }
555
556 if (cpu_isset(cpu, cpu_callin_map)) {
557 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 Dprintk("CPU has booted.\n");
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300559 printk(KERN_INFO "CPU%d: ", cpu);
560 print_cpu_info(&cpu_data(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else {
562 boot_error = 1;
Glauber de Oliveira Costa6becedb2008-03-19 14:25:51 -0300563 if (*((volatile unsigned char *)trampoline_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 == 0xA5)
565 /* trampoline started but...? */
566 printk("Stuck ??\n");
567 else
568 /* trampoline code not run */
569 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700570#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 inquire_remote_apic(apicid);
572#endif
573 }
574 }
575 if (boot_error) {
576 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
Jeremy Fitzhardinge5548fec2008-01-30 13:30:55 +0100577 clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800578 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700579 cpu_clear(cpu, cpu_present_map);
580 cpu_clear(cpu, cpu_possible_map);
Mike Travis71fff5e2007-10-19 20:35:03 +0200581 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700582 }
583
Glauber de Oliveira Costa6becedb2008-03-19 14:25:51 -0300584 /* mark "stuck" area as not stuck */
585 *((volatile unsigned long *)trampoline_base) = 0;
586
587 return boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700588}
589
590cycles_t cacheflush_time;
591unsigned long cache_decay_ticks;
592
593/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700594 * Fall back to non SMP mode after errors.
595 *
596 * RED-PEN audit/test this more. I bet there is more state messed up here.
597 */
Ashok Raje6982c62005-06-25 14:54:58 -0700598static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700599{
600 cpu_present_map = cpumask_of_cpu(0);
601 cpu_possible_map = cpumask_of_cpu(0);
602 if (smp_found_config)
603 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
604 else
605 phys_cpu_present_map = physid_mask_of_physid(0);
Mike Travisd5a74302007-10-16 01:24:05 -0700606 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700607 cpu_set(0, per_cpu(cpu_core_map, 0));
Andi Kleena8ab26f2005-04-16 15:25:19 -0700608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700611 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Ashok Raje6982c62005-06-25 14:54:58 -0700613static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
616 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
617 hard_smp_processor_id());
618 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
619 }
620
621 /*
622 * If we couldn't find an SMP configuration at boot time,
623 * get out of here now!
624 */
625 if (!smp_found_config) {
626 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700627 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (APIC_init_uniprocessor())
629 printk(KERN_NOTICE "Local APIC not detected."
630 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700631 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 /*
635 * Should not be necessary because the MP table should list the boot
636 * CPU too, but we do it for the sake of robustness anyway.
637 */
638 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
639 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
640 boot_cpu_id);
641 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
642 }
643
644 /*
645 * If we couldn't find a local APIC, then get out of here now!
646 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100647 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
649 boot_cpu_id);
650 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700651 nr_ioapics = 0;
652 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /*
656 * If SMP should be disabled, then really disable it!
657 */
658 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700660 nr_ioapics = 0;
661 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
Andi Kleena8ab26f2005-04-16 15:25:19 -0700664 return 0;
665}
666
Yinghai Lu949ec322008-01-30 13:30:46 +0100667static void __init smp_cpu_index_default(void)
668{
669 int i;
670 struct cpuinfo_x86 *c;
671
672 for_each_cpu_mask(i, cpu_possible_map) {
673 c = &cpu_data(i);
674 /* mark all to hotplug */
675 c->cpu_index = NR_CPUS;
676 }
677}
678
Mike Travis71fff5e2007-10-19 20:35:03 +0200679/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700680 * Prepare for SMP bootup. The MP table or ACPI has been read
681 * earlier. Just do some sanity checking here and enable APIC mode.
682 */
Glauber Costa7557da62008-03-03 14:12:38 -0300683void __init native_smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700684{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700685 nmi_watchdog_default();
Yinghai Lu949ec322008-01-30 13:30:46 +0100686 smp_cpu_index_default();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700687 current_cpu_data = boot_cpu_data;
688 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100689 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700690
Andi Kleena8ab26f2005-04-16 15:25:19 -0700691 if (smp_sanity_check(max_cpus) < 0) {
692 printk(KERN_INFO "SMP disabled\n");
693 disable_smp();
694 return;
695 }
696
697
698 /*
699 * Switch from PIC to APIC mode.
700 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 setup_local_APIC();
702
Andi Kleen739f33b2008-01-30 13:30:40 +0100703 /*
704 * Enable IO APIC before setting up error vector
705 */
706 if (!skip_ioapic_setup && nr_ioapics)
707 enable_IO_APIC();
708 end_local_APIC_setup();
709
Andi Kleena8ab26f2005-04-16 15:25:19 -0700710 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
711 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
712 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
713 /* Or can we switch back to PIC here? */
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700717 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
719 if (!skip_ioapic_setup && nr_ioapics)
720 setup_IO_APIC();
721 else
722 nr_ioapics = 0;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700725 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100728 setup_boot_clock();
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300729 printk(KERN_INFO "CPU%d: ", 0);
730 print_cpu_info(&cpu_data(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Andi Kleena8ab26f2005-04-16 15:25:19 -0700733/*
734 * Early setup to make printk work.
735 */
Glauber Costa1e3fac82008-03-03 14:12:37 -0300736void __init native_smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700738 int me = smp_processor_id();
Yinghai Lu23916d42008-01-30 13:33:39 +0100739 /* already set me in cpu_online_map in boot_cpu_init() */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700740 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700741 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Andi Kleena8ab26f2005-04-16 15:25:19 -0700744/*
745 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700746 */
Glauber Costa71d19542008-03-03 14:12:36 -0300747int __cpuinit native_cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700749 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100750 unsigned long flags;
751 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Andi Kleena8ab26f2005-04-16 15:25:19 -0700753 WARN_ON(irqs_disabled());
754
755 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
756
757 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
758 !physid_isset(apicid, phys_cpu_present_map)) {
759 printk("__cpu_up: bad cpu %d\n", cpu);
760 return -EINVAL;
761 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700762
Ashok Raj76e4f662005-06-25 14:55:00 -0700763 /*
764 * Already booted CPU?
765 */
766 if (cpu_isset(cpu, cpu_callin_map)) {
767 Dprintk("do_boot_cpu %d Already started\n", cpu);
768 return -ENOSYS;
769 }
770
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200771 /*
772 * Save current MTRR state in case it was changed since early boot
773 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
774 */
775 mtrr_save_state();
776
Ashok Raj884d9e42005-06-25 14:55:02 -0700777 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700778 /* Boot it! */
779 err = do_boot_cpu(cpu, apicid);
780 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700781 Dprintk("do_boot_cpu failed %d\n", err);
782 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* Unleash the CPU! */
786 Dprintk("waiting for cpu %d\n", cpu);
787
Ingo Molnar95492e42007-02-16 01:27:34 -0800788 /*
789 * Make sure and check TSC sync:
790 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100791 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800792 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100793 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700796 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700797 err = 0;
798
799 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300802extern void impress_friends(void);
803extern void smp_checks(void);
804
Andi Kleena8ab26f2005-04-16 15:25:19 -0700805/*
806 * Finish the SMP boot.
807 */
Glauber Costac5597642008-03-03 14:12:39 -0300808void __init native_smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -0300810 smpboot_restore_warm_reset_vector();
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300811
812 Dprintk("Boot done.\n");
813
814 impress_friends();
815 smp_checks();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700817 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700818}