blob: e93fff42ec32222e0a7b1b026af69b1c55aa081d [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 Costaeb44d0a2008-03-19 14:25:32 -030064#include <smpboot_hooks.h>
Glauber de Oliveira Costa8d770102008-03-19 14:25:31 -030065
Andi Kleena8ab26f2005-04-16 15:25:19 -070066/* Set when the idlers are all forked */
67int smp_threads_ready;
68
Ashok Raj76e4f662005-06-25 14:55:00 -070069/* State of each CPU */
70DEFINE_PER_CPU(int, cpu_state) = { 0 };
71
72/*
73 * Store all idle threads, this can be reused instead of creating
74 * a new thread. Also avoids complicated thread destroy functionality
75 * for idle threads.
76 */
travis@sgi.com24b0d222008-01-30 13:33:11 +010077#ifdef CONFIG_HOTPLUG_CPU
78/*
79 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
80 * removed after init for !CONFIG_HOTPLUG_CPU.
81 */
82static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
83#define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
84#define set_idle_for_cpu(x,p) (per_cpu(idle_thread_array, x) = (p))
85#else
Ashok Raj76e4f662005-06-25 14:55:00 -070086struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
Ashok Raj76e4f662005-06-25 14:55:00 -070087#define get_idle_for_cpu(x) (idle_thread_array[(x)])
88#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
travis@sgi.com24b0d222008-01-30 13:33:11 +010089#endif
90
Andi Kleena8ab26f2005-04-16 15:25:19 -070091static atomic_t init_deasserted __cpuinitdata;
92
Glauber de Oliveira Costaea0cadb2008-03-19 14:25:47 -030093#define smp_callin_clear_local_apic() do {} while (0)
94#define map_cpu_to_logical_apicid() do {} while (0)
95
Andi Kleena8ab26f2005-04-16 15:25:19 -070096/*
97 * Report back to the Boot Processor.
98 * Running on AP.
99 */
100void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 int cpuid, phys_id;
103 unsigned long timeout;
104
105 /*
106 * If waken up by an INIT in an 82489DX configuration
107 * we may get here before an INIT-deassert IPI reaches
108 * our local APIC. We have to wait for the IPI or we'll
109 * lock up on an APIC access.
110 */
Glauber Costae90009b2008-03-03 14:13:13 -0300111 wait_for_init_deassert(&init_deasserted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /*
114 * (This works even if the APIC is not enabled.)
115 */
116 phys_id = GET_APIC_ID(apic_read(APIC_ID));
117 cpuid = smp_processor_id();
118 if (cpu_isset(cpuid, cpu_callin_map)) {
119 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
120 phys_id, cpuid);
121 }
122 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
123
124 /*
125 * STARTUP IPIs are fragile beasts as they might sometimes
126 * trigger some glue motherboard logic. Complete APIC bus
127 * silence for 1 second, this overestimates the time the
128 * boot CPU is spending to send the up to 2 STARTUP IPIs
129 * by a factor of two. This should be enough.
130 */
131
132 /*
133 * Waiting 2s total for startup (udelay is not yet working)
134 */
135 timeout = jiffies + 2*HZ;
136 while (time_before(jiffies, timeout)) {
137 /*
138 * Has the boot CPU finished it's STARTUP sequence?
139 */
140 if (cpu_isset(cpuid, cpu_callout_map))
141 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700142 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
145 if (!time_before(jiffies, timeout)) {
146 panic("smp_callin: CPU%d started up but did not get a callout!\n",
147 cpuid);
148 }
149
150 /*
151 * the boot CPU has finished the init stage and is spinning
152 * on callin_map until we finish. We are free to set up this
153 * CPU, first the APIC. (this is probably redundant on most
154 * boards)
155 */
156
157 Dprintk("CALLIN, before setup_local_APIC().\n");
Glauber de Oliveira Costaea0cadb2008-03-19 14:25:47 -0300158 smp_callin_clear_local_apic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 setup_local_APIC();
Andi Kleen739f33b2008-01-30 13:30:40 +0100160 end_local_APIC_setup();
Glauber de Oliveira Costaea0cadb2008-03-19 14:25:47 -0300161 map_cpu_to_logical_apicid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /*
164 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200165 *
166 * Need to enable IRQs because it can take longer and then
167 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
Andi Kleenb4452212005-09-12 18:49:24 +0200169 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200171 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 Dprintk("Stack at about %p\n",&cpuid);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /*
175 * Save our processor parameters
176 */
177 smp_store_cpu_info(cpuid);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /*
180 * Allow the master to continue.
181 */
182 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700186 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700188void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 /*
191 * Dont put anything before smp_callin(), SMP
192 * booting is too fragile that we want to limit the
193 * things done here to the most necessary things.
194 */
195 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800196 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 smp_callin();
198
199 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
200 barrier();
201
Ingo Molnar95492e42007-02-16 01:27:34 -0800202 /*
203 * Check TSC sync first:
204 */
205 check_tsc_sync_target();
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (nmi_watchdog == NMI_IO_APIC) {
208 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100209 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 enable_8259A_irq(0);
211 }
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700214 * The sibling maps must be set before turing the online map on for
215 * this cpu
216 */
217 set_cpu_sibling_map(smp_processor_id());
218
219 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700220 * We need to hold call_lock, so there is no inconsistency
221 * between the time smp_call_function() determines number of
Simon Arlott676b1852007-10-20 01:25:36 +0200222 * IPI recipients, and the time when the determination is made
Ashok Raj884d9e42005-06-25 14:55:02 -0700223 * for which cpus receive the IPI in genapic_flat.c. Holding this
224 * lock helps us to not include this cpu in a currently in progress
225 * smp_call_function().
226 */
227 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200228 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700229
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200230 /* Setup the per cpu irq handling data structures */
231 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700232 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700233 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Glauber Costafc25da92008-03-03 14:13:04 -0300235 spin_unlock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700237 unlock_ipi_call_lock();
238
Glauber de Oliveira Costa5733f622008-03-19 14:25:09 -0300239 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
240
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100241 setup_secondary_clock();
Thomas Gleixner3ac508b2007-10-14 22:57:45 +0200242
Glauber de Oliveira Costafa8004d2008-03-19 14:25:12 -0300243 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 cpu_idle();
245}
246
Andi Kleena8ab26f2005-04-16 15:25:19 -0700247extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248extern void (*initial_code)(void);
249
Olaf Hering44456d32005-07-27 11:45:17 -0700250#ifdef APIC_DEBUG
Glauber de Oliveira Costa8d770102008-03-19 14:25:31 -0300251static void __inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
254 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200255 int timeout;
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100256 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
259
Alejandro Martinez Ruiz4d022ad2007-10-17 14:38:58 +0200260 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100261 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /*
264 * Wait for idle.
265 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200266 status = safe_apic_wait_icr_idle();
267 if (status)
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100268 printk(KERN_CONT
269 "a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300271 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
272 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 timeout = 0;
275 do {
276 udelay(100);
277 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
278 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
279
280 switch (status) {
281 case APIC_ICR_RR_VALID:
282 status = apic_read(APIC_RRR);
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100283 printk(KERN_CONT "%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285 default:
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100286 printk(KERN_CONT "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 }
289}
290#endif
291
Andi Kleena8ab26f2005-04-16 15:25:19 -0700292/*
293 * Kick the secondary to wake up.
294 */
295static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200297 unsigned long send_status, accept_status = 0;
298 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300300 /*
301 * Be paranoid about clearing APIC errors.
302 */
303 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
304 apic_read_around(APIC_SPIV);
305 apic_write(APIC_ESR, 0);
306 apic_read(APIC_ESR);
307 }
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 Dprintk("Asserting INIT.\n");
310
311 /*
312 * Turn INIT on target chip
313 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300314 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /*
317 * Send IPI
318 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300319 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 | APIC_DM_INIT);
321
322 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200323 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 mdelay(10);
326
327 Dprintk("Deasserting INIT.\n");
328
329 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300330 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Send IPI */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300333 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200336 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100338 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 atomic_set(&init_deasserted, 1);
340
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300341 if (APIC_INTEGRATED(apic_version[phys_apicid]))
342 num_starts = 2;
343 else
344 num_starts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /*
Glauber de Oliveira Costad0173ae2008-03-19 14:24:59 -0300347 * Paravirt / VMI wants a startup IPI hook here to set up the
348 * target processor state.
349 */
350 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
351 (unsigned long) init_rsp);
352
353
354 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * Run STARTUP IPI loop.
356 */
357 Dprintk("#startup loops: %d.\n", num_starts);
358
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100359 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 for (j = 1; j <= num_starts; j++) {
362 Dprintk("Sending STARTUP #%d.\n",j);
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300363 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 apic_write(APIC_ESR, 0);
365 apic_read(APIC_ESR);
366 Dprintk("After apic_write.\n");
367
368 /*
369 * STARTUP IPI
370 */
371
372 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300373 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* Boot on the stack */
376 /* Kick the second */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300377 apic_write_around(APIC_ICR, APIC_DM_STARTUP | (start_rip>>12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /*
380 * Give the other CPU some time to accept the IPI.
381 */
382 udelay(300);
383
384 Dprintk("Startup point 1.\n");
385
386 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200387 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /*
390 * Give the other CPU some time to accept the IPI.
391 */
392 udelay(200);
393 /*
394 * Due to the Pentium erratum 3AP.
395 */
396 if (maxlvt > 3) {
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300397 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 apic_write(APIC_ESR, 0);
399 }
400 accept_status = (apic_read(APIC_ESR) & 0xEF);
401 if (send_status || accept_status)
402 break;
403 }
404 Dprintk("After Startup.\n");
405
406 if (send_status)
407 printk(KERN_ERR "APIC never delivered???\n");
408 if (accept_status)
409 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
410
411 return (send_status | accept_status);
412}
413
Ashok Raj76e4f662005-06-25 14:55:00 -0700414struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000415 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700416 struct task_struct *idle;
417 struct completion done;
418 int cpu;
419};
420
Thomas Gleixnera2b484a2008-01-09 00:18:28 +0100421static void __cpuinit do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700422{
David Howells65f27f32006-11-22 14:55:48 +0000423 struct create_idle *c_idle =
424 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700425
426 c_idle->idle = fork_idle(c_idle->cpu);
427 complete(&c_idle->done);
428}
429
Andi Kleena8ab26f2005-04-16 15:25:19 -0700430/*
431 * Boot one CPU.
432 */
433static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700436 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700438 struct create_idle c_idle = {
439 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700440 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700441 };
Glauber Costa2b775a22008-02-22 12:09:29 -0300442 INIT_WORK(&c_idle.work, do_fork_idle);
Ashok Raj76e4f662005-06-25 14:55:00 -0700443
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100444 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
445 if (!cpu_gdt_descr[cpu].address &&
446 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
447 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
448 return -1;
449 }
450
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100451 /* Allocate node local memory for AP pdas */
452 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
453 struct x8664_pda *newpda, *pda;
454 int node = cpu_to_node(cpu);
455 pda = cpu_pda(cpu);
456 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
457 node);
458 if (newpda) {
459 memcpy(newpda, pda, sizeof (struct x8664_pda));
460 cpu_pda(cpu) = newpda;
461 } else
462 printk(KERN_ERR
463 "Could not allocate node local PDA for CPU %d on node %d\n",
464 cpu, node);
465 }
466
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200467 alternatives_smp_switch(1);
468
Ashok Raj76e4f662005-06-25 14:55:00 -0700469 c_idle.idle = get_idle_for_cpu(cpu);
470
471 if (c_idle.idle) {
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100472 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800473 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700474 init_idle(c_idle.idle, cpu);
475 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Ashok Raj76e4f662005-06-25 14:55:00 -0700478 /*
479 * During cold boot process, keventd thread is not spun up yet.
480 * When we do cpu hot-add, we create idle threads on the fly, we should
481 * not acquire any attributes from the calling context. Hence the clean
482 * way to create kernel_threads() is to do that from keventd().
483 * We do the current_is_keventd() due to the fact that ACPI notifier
484 * was also queuing to keventd() and when the caller is already running
485 * in context of keventd(), we would end up with locking up the keventd
486 * thread.
487 */
488 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000489 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700490 else {
David Howells65f27f32006-11-22 14:55:48 +0000491 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700492 wait_for_completion(&c_idle.done);
493 }
494
495 if (IS_ERR(c_idle.idle)) {
496 printk("failed fork for CPU %d\n", cpu);
497 return PTR_ERR(c_idle.idle);
498 }
499
500 set_idle_for_cpu(cpu, c_idle.idle);
501
502do_rest:
503
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100504 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 start_rip = setup_trampoline();
507
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100508 init_rsp = c_idle.idle->thread.sp;
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100509 load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800511 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Andi Kleende04f322005-07-28 21:15:29 -0700513 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
514 cpus_weight(cpu_present_map),
515 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /*
518 * This grunge runs the startup process for
519 * the targeted processor.
520 */
521
522 atomic_set(&init_deasserted, 0);
523
524 Dprintk("Setting warm reset code and vector.\n");
525
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -0300526 smpboot_setup_warm_reset_vector(start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
528 * Be paranoid about clearing APIC errors.
529 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100530 apic_write(APIC_ESR, 0);
531 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /*
534 * Status is now clean
535 */
536 boot_error = 0;
537
538 /*
539 * Starting actual IPI sequence...
540 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700541 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 if (!boot_error) {
544 /*
545 * allow APs to start initializing.
546 */
547 Dprintk("Before Callout %d.\n", cpu);
548 cpu_set(cpu, cpu_callout_map);
549 Dprintk("After Callout %d.\n", cpu);
550
551 /*
552 * Wait 5s total for a response
553 */
554 for (timeout = 0; timeout < 50000; timeout++) {
555 if (cpu_isset(cpu, cpu_callin_map))
556 break; /* It has booted */
557 udelay(100);
558 }
559
560 if (cpu_isset(cpu, cpu_callin_map)) {
561 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 Dprintk("CPU has booted.\n");
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300563 printk(KERN_INFO "CPU%d: ", cpu);
564 print_cpu_info(&cpu_data(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 } else {
566 boot_error = 1;
567 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
568 == 0xA5)
569 /* trampoline started but...? */
570 printk("Stuck ??\n");
571 else
572 /* trampoline code not run */
573 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700574#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 inquire_remote_apic(apicid);
576#endif
577 }
578 }
579 if (boot_error) {
580 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
Jeremy Fitzhardinge5548fec2008-01-30 13:30:55 +0100581 clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800582 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700583 cpu_clear(cpu, cpu_present_map);
584 cpu_clear(cpu, cpu_possible_map);
Mike Travis71fff5e2007-10-19 20:35:03 +0200585 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700586 return -EIO;
587 }
588
589 return 0;
590}
591
592cycles_t cacheflush_time;
593unsigned long cache_decay_ticks;
594
595/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700596 * Fall back to non SMP mode after errors.
597 *
598 * RED-PEN audit/test this more. I bet there is more state messed up here.
599 */
Ashok Raje6982c62005-06-25 14:54:58 -0700600static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700601{
602 cpu_present_map = cpumask_of_cpu(0);
603 cpu_possible_map = cpumask_of_cpu(0);
604 if (smp_found_config)
605 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
606 else
607 phys_cpu_present_map = physid_mask_of_physid(0);
Mike Travisd5a74302007-10-16 01:24:05 -0700608 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700609 cpu_set(0, per_cpu(cpu_core_map, 0));
Andi Kleena8ab26f2005-04-16 15:25:19 -0700610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700613 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Ashok Raje6982c62005-06-25 14:54:58 -0700615static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
618 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
619 hard_smp_processor_id());
620 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
621 }
622
623 /*
624 * If we couldn't find an SMP configuration at boot time,
625 * get out of here now!
626 */
627 if (!smp_found_config) {
628 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700629 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (APIC_init_uniprocessor())
631 printk(KERN_NOTICE "Local APIC not detected."
632 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700633 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 /*
637 * Should not be necessary because the MP table should list the boot
638 * CPU too, but we do it for the sake of robustness anyway.
639 */
640 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
641 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
642 boot_cpu_id);
643 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
644 }
645
646 /*
647 * If we couldn't find a local APIC, then get out of here now!
648 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100649 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
651 boot_cpu_id);
652 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700653 nr_ioapics = 0;
654 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * If SMP should be disabled, then really disable it!
659 */
660 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700662 nr_ioapics = 0;
663 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
Andi Kleena8ab26f2005-04-16 15:25:19 -0700666 return 0;
667}
668
Yinghai Lu949ec322008-01-30 13:30:46 +0100669static void __init smp_cpu_index_default(void)
670{
671 int i;
672 struct cpuinfo_x86 *c;
673
674 for_each_cpu_mask(i, cpu_possible_map) {
675 c = &cpu_data(i);
676 /* mark all to hotplug */
677 c->cpu_index = NR_CPUS;
678 }
679}
680
Mike Travis71fff5e2007-10-19 20:35:03 +0200681/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700682 * Prepare for SMP bootup. The MP table or ACPI has been read
683 * earlier. Just do some sanity checking here and enable APIC mode.
684 */
Glauber Costa7557da62008-03-03 14:12:38 -0300685void __init native_smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700686{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700687 nmi_watchdog_default();
Yinghai Lu949ec322008-01-30 13:30:46 +0100688 smp_cpu_index_default();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700689 current_cpu_data = boot_cpu_data;
690 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100691 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700692
Andi Kleena8ab26f2005-04-16 15:25:19 -0700693 if (smp_sanity_check(max_cpus) < 0) {
694 printk(KERN_INFO "SMP disabled\n");
695 disable_smp();
696 return;
697 }
698
699
700 /*
701 * Switch from PIC to APIC mode.
702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 setup_local_APIC();
704
Andi Kleen739f33b2008-01-30 13:30:40 +0100705 /*
706 * Enable IO APIC before setting up error vector
707 */
708 if (!skip_ioapic_setup && nr_ioapics)
709 enable_IO_APIC();
710 end_local_APIC_setup();
711
Andi Kleena8ab26f2005-04-16 15:25:19 -0700712 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
713 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
714 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
715 /* Or can we switch back to PIC here? */
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700719 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
721 if (!skip_ioapic_setup && nr_ioapics)
722 setup_IO_APIC();
723 else
724 nr_ioapics = 0;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700727 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100730 setup_boot_clock();
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300731 printk(KERN_INFO "CPU%d: ", 0);
732 print_cpu_info(&cpu_data(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Andi Kleena8ab26f2005-04-16 15:25:19 -0700735/*
736 * Early setup to make printk work.
737 */
Glauber Costa1e3fac82008-03-03 14:12:37 -0300738void __init native_smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700740 int me = smp_processor_id();
Yinghai Lu23916d42008-01-30 13:33:39 +0100741 /* already set me in cpu_online_map in boot_cpu_init() */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700742 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700743 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Andi Kleena8ab26f2005-04-16 15:25:19 -0700746/*
747 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700748 */
Glauber Costa71d19542008-03-03 14:12:36 -0300749int __cpuinit native_cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700751 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100752 unsigned long flags;
753 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Andi Kleena8ab26f2005-04-16 15:25:19 -0700755 WARN_ON(irqs_disabled());
756
757 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
758
759 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
760 !physid_isset(apicid, phys_cpu_present_map)) {
761 printk("__cpu_up: bad cpu %d\n", cpu);
762 return -EINVAL;
763 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700764
Ashok Raj76e4f662005-06-25 14:55:00 -0700765 /*
766 * Already booted CPU?
767 */
768 if (cpu_isset(cpu, cpu_callin_map)) {
769 Dprintk("do_boot_cpu %d Already started\n", cpu);
770 return -ENOSYS;
771 }
772
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200773 /*
774 * Save current MTRR state in case it was changed since early boot
775 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
776 */
777 mtrr_save_state();
778
Ashok Raj884d9e42005-06-25 14:55:02 -0700779 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700780 /* Boot it! */
781 err = do_boot_cpu(cpu, apicid);
782 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700783 Dprintk("do_boot_cpu failed %d\n", err);
784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Unleash the CPU! */
788 Dprintk("waiting for cpu %d\n", cpu);
789
Ingo Molnar95492e42007-02-16 01:27:34 -0800790 /*
791 * Make sure and check TSC sync:
792 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100793 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800794 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100795 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700798 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700799 err = 0;
800
801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300804extern void impress_friends(void);
805extern void smp_checks(void);
806
Andi Kleena8ab26f2005-04-16 15:25:19 -0700807/*
808 * Finish the SMP boot.
809 */
Glauber Costac5597642008-03-03 14:12:39 -0300810void __init native_smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -0300812 smpboot_restore_warm_reset_vector();
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300813
814 Dprintk("Boot done.\n");
815
816 impress_friends();
817 smp_checks();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700819 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700820}