blob: b9384b3af017003035155772d493c4b525701bc4 [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
93/*
94 * Report back to the Boot Processor.
95 * Running on AP.
96 */
97void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int cpuid, phys_id;
100 unsigned long timeout;
101
102 /*
103 * If waken up by an INIT in an 82489DX configuration
104 * we may get here before an INIT-deassert IPI reaches
105 * our local APIC. We have to wait for the IPI or we'll
106 * lock up on an APIC access.
107 */
Glauber Costae90009b2008-03-03 14:13:13 -0300108 wait_for_init_deassert(&init_deasserted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /*
111 * (This works even if the APIC is not enabled.)
112 */
113 phys_id = GET_APIC_ID(apic_read(APIC_ID));
114 cpuid = smp_processor_id();
115 if (cpu_isset(cpuid, cpu_callin_map)) {
116 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
117 phys_id, cpuid);
118 }
119 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
120
121 /*
122 * STARTUP IPIs are fragile beasts as they might sometimes
123 * trigger some glue motherboard logic. Complete APIC bus
124 * silence for 1 second, this overestimates the time the
125 * boot CPU is spending to send the up to 2 STARTUP IPIs
126 * by a factor of two. This should be enough.
127 */
128
129 /*
130 * Waiting 2s total for startup (udelay is not yet working)
131 */
132 timeout = jiffies + 2*HZ;
133 while (time_before(jiffies, timeout)) {
134 /*
135 * Has the boot CPU finished it's STARTUP sequence?
136 */
137 if (cpu_isset(cpuid, cpu_callout_map))
138 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700139 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141
142 if (!time_before(jiffies, timeout)) {
143 panic("smp_callin: CPU%d started up but did not get a callout!\n",
144 cpuid);
145 }
146
147 /*
148 * the boot CPU has finished the init stage and is spinning
149 * on callin_map until we finish. We are free to set up this
150 * CPU, first the APIC. (this is probably redundant on most
151 * boards)
152 */
153
154 Dprintk("CALLIN, before setup_local_APIC().\n");
155 setup_local_APIC();
Andi Kleen739f33b2008-01-30 13:30:40 +0100156 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /*
159 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200160 *
161 * Need to enable IRQs because it can take longer and then
162 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 */
Andi Kleenb4452212005-09-12 18:49:24 +0200164 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200166 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 Dprintk("Stack at about %p\n",&cpuid);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /*
170 * Save our processor parameters
171 */
172 smp_store_cpu_info(cpuid);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /*
175 * Allow the master to continue.
176 */
177 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700181 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700183void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 /*
186 * Dont put anything before smp_callin(), SMP
187 * booting is too fragile that we want to limit the
188 * things done here to the most necessary things.
189 */
190 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800191 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 smp_callin();
193
194 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
195 barrier();
196
Ingo Molnar95492e42007-02-16 01:27:34 -0800197 /*
198 * Check TSC sync first:
199 */
200 check_tsc_sync_target();
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (nmi_watchdog == NMI_IO_APIC) {
203 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100204 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 enable_8259A_irq(0);
206 }
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700209 * The sibling maps must be set before turing the online map on for
210 * this cpu
211 */
212 set_cpu_sibling_map(smp_processor_id());
213
214 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700215 * We need to hold call_lock, so there is no inconsistency
216 * between the time smp_call_function() determines number of
Simon Arlott676b1852007-10-20 01:25:36 +0200217 * IPI recipients, and the time when the determination is made
Ashok Raj884d9e42005-06-25 14:55:02 -0700218 * for which cpus receive the IPI in genapic_flat.c. Holding this
219 * lock helps us to not include this cpu in a currently in progress
220 * smp_call_function().
221 */
222 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200223 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700224
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200225 /* Setup the per cpu irq handling data structures */
226 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700227 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700228 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
Glauber Costafc25da92008-03-03 14:13:04 -0300230 spin_unlock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700232 unlock_ipi_call_lock();
233
Glauber de Oliveira Costa5733f622008-03-19 14:25:09 -0300234 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
235
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100236 setup_secondary_clock();
Thomas Gleixner3ac508b2007-10-14 22:57:45 +0200237
Glauber de Oliveira Costafa8004d2008-03-19 14:25:12 -0300238 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 cpu_idle();
240}
241
Andi Kleena8ab26f2005-04-16 15:25:19 -0700242extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243extern void (*initial_code)(void);
244
Olaf Hering44456d32005-07-27 11:45:17 -0700245#ifdef APIC_DEBUG
Glauber de Oliveira Costa8d770102008-03-19 14:25:31 -0300246static void __inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
249 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200250 int timeout;
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100251 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
254
Alejandro Martinez Ruiz4d022ad2007-10-17 14:38:58 +0200255 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100256 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /*
259 * Wait for idle.
260 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200261 status = safe_apic_wait_icr_idle();
262 if (status)
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100263 printk(KERN_CONT
264 "a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300266 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
267 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 timeout = 0;
270 do {
271 udelay(100);
272 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
273 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
274
275 switch (status) {
276 case APIC_ICR_RR_VALID:
277 status = apic_read(APIC_RRR);
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100278 printk(KERN_CONT "%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 break;
280 default:
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100281 printk(KERN_CONT "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 }
284}
285#endif
286
Andi Kleena8ab26f2005-04-16 15:25:19 -0700287/*
288 * Kick the secondary to wake up.
289 */
290static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200292 unsigned long send_status, accept_status = 0;
293 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300295 /*
296 * Be paranoid about clearing APIC errors.
297 */
298 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
299 apic_read_around(APIC_SPIV);
300 apic_write(APIC_ESR, 0);
301 apic_read(APIC_ESR);
302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 Dprintk("Asserting INIT.\n");
305
306 /*
307 * Turn INIT on target chip
308 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300309 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /*
312 * Send IPI
313 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300314 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 | APIC_DM_INIT);
316
317 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200318 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 mdelay(10);
321
322 Dprintk("Deasserting INIT.\n");
323
324 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300325 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* Send IPI */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300328 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200331 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100333 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 atomic_set(&init_deasserted, 1);
335
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300336 if (APIC_INTEGRATED(apic_version[phys_apicid]))
337 num_starts = 2;
338 else
339 num_starts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /*
Glauber de Oliveira Costad0173ae2008-03-19 14:24:59 -0300342 * Paravirt / VMI wants a startup IPI hook here to set up the
343 * target processor state.
344 */
345 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
346 (unsigned long) init_rsp);
347
348
349 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * Run STARTUP IPI loop.
351 */
352 Dprintk("#startup loops: %d.\n", num_starts);
353
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100354 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 for (j = 1; j <= num_starts; j++) {
357 Dprintk("Sending STARTUP #%d.\n",j);
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300358 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 apic_write(APIC_ESR, 0);
360 apic_read(APIC_ESR);
361 Dprintk("After apic_write.\n");
362
363 /*
364 * STARTUP IPI
365 */
366
367 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300368 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* Boot on the stack */
371 /* Kick the second */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300372 apic_write_around(APIC_ICR, APIC_DM_STARTUP | (start_rip>>12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /*
375 * Give the other CPU some time to accept the IPI.
376 */
377 udelay(300);
378
379 Dprintk("Startup point 1.\n");
380
381 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200382 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /*
385 * Give the other CPU some time to accept the IPI.
386 */
387 udelay(200);
388 /*
389 * Due to the Pentium erratum 3AP.
390 */
391 if (maxlvt > 3) {
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300392 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 apic_write(APIC_ESR, 0);
394 }
395 accept_status = (apic_read(APIC_ESR) & 0xEF);
396 if (send_status || accept_status)
397 break;
398 }
399 Dprintk("After Startup.\n");
400
401 if (send_status)
402 printk(KERN_ERR "APIC never delivered???\n");
403 if (accept_status)
404 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
405
406 return (send_status | accept_status);
407}
408
Ashok Raj76e4f662005-06-25 14:55:00 -0700409struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000410 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700411 struct task_struct *idle;
412 struct completion done;
413 int cpu;
414};
415
Thomas Gleixnera2b484a2008-01-09 00:18:28 +0100416static void __cpuinit do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700417{
David Howells65f27f32006-11-22 14:55:48 +0000418 struct create_idle *c_idle =
419 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700420
421 c_idle->idle = fork_idle(c_idle->cpu);
422 complete(&c_idle->done);
423}
424
Andi Kleena8ab26f2005-04-16 15:25:19 -0700425/*
426 * Boot one CPU.
427 */
428static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700431 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700433 struct create_idle c_idle = {
434 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700435 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700436 };
Glauber Costa2b775a22008-02-22 12:09:29 -0300437 INIT_WORK(&c_idle.work, do_fork_idle);
Ashok Raj76e4f662005-06-25 14:55:00 -0700438
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100439 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
440 if (!cpu_gdt_descr[cpu].address &&
441 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
442 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
443 return -1;
444 }
445
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100446 /* Allocate node local memory for AP pdas */
447 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
448 struct x8664_pda *newpda, *pda;
449 int node = cpu_to_node(cpu);
450 pda = cpu_pda(cpu);
451 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
452 node);
453 if (newpda) {
454 memcpy(newpda, pda, sizeof (struct x8664_pda));
455 cpu_pda(cpu) = newpda;
456 } else
457 printk(KERN_ERR
458 "Could not allocate node local PDA for CPU %d on node %d\n",
459 cpu, node);
460 }
461
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200462 alternatives_smp_switch(1);
463
Ashok Raj76e4f662005-06-25 14:55:00 -0700464 c_idle.idle = get_idle_for_cpu(cpu);
465
466 if (c_idle.idle) {
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100467 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800468 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700469 init_idle(c_idle.idle, cpu);
470 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Ashok Raj76e4f662005-06-25 14:55:00 -0700473 /*
474 * During cold boot process, keventd thread is not spun up yet.
475 * When we do cpu hot-add, we create idle threads on the fly, we should
476 * not acquire any attributes from the calling context. Hence the clean
477 * way to create kernel_threads() is to do that from keventd().
478 * We do the current_is_keventd() due to the fact that ACPI notifier
479 * was also queuing to keventd() and when the caller is already running
480 * in context of keventd(), we would end up with locking up the keventd
481 * thread.
482 */
483 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000484 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700485 else {
David Howells65f27f32006-11-22 14:55:48 +0000486 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700487 wait_for_completion(&c_idle.done);
488 }
489
490 if (IS_ERR(c_idle.idle)) {
491 printk("failed fork for CPU %d\n", cpu);
492 return PTR_ERR(c_idle.idle);
493 }
494
495 set_idle_for_cpu(cpu, c_idle.idle);
496
497do_rest:
498
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100499 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 start_rip = setup_trampoline();
502
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100503 init_rsp = c_idle.idle->thread.sp;
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100504 load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800506 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Andi Kleende04f322005-07-28 21:15:29 -0700508 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
509 cpus_weight(cpu_present_map),
510 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /*
513 * This grunge runs the startup process for
514 * the targeted processor.
515 */
516
517 atomic_set(&init_deasserted, 0);
518
519 Dprintk("Setting warm reset code and vector.\n");
520
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -0300521 smpboot_setup_warm_reset_vector(start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /*
523 * Be paranoid about clearing APIC errors.
524 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100525 apic_write(APIC_ESR, 0);
526 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /*
529 * Status is now clean
530 */
531 boot_error = 0;
532
533 /*
534 * Starting actual IPI sequence...
535 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700536 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (!boot_error) {
539 /*
540 * allow APs to start initializing.
541 */
542 Dprintk("Before Callout %d.\n", cpu);
543 cpu_set(cpu, cpu_callout_map);
544 Dprintk("After Callout %d.\n", cpu);
545
546 /*
547 * Wait 5s total for a response
548 */
549 for (timeout = 0; timeout < 50000; timeout++) {
550 if (cpu_isset(cpu, cpu_callin_map))
551 break; /* It has booted */
552 udelay(100);
553 }
554
555 if (cpu_isset(cpu, cpu_callin_map)) {
556 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 Dprintk("CPU has booted.\n");
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300558 printk(KERN_INFO "CPU%d: ", cpu);
559 print_cpu_info(&cpu_data(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 } else {
561 boot_error = 1;
562 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
563 == 0xA5)
564 /* trampoline started but...? */
565 printk("Stuck ??\n");
566 else
567 /* trampoline code not run */
568 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700569#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 inquire_remote_apic(apicid);
571#endif
572 }
573 }
574 if (boot_error) {
575 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
Jeremy Fitzhardinge5548fec2008-01-30 13:30:55 +0100576 clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800577 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700578 cpu_clear(cpu, cpu_present_map);
579 cpu_clear(cpu, cpu_possible_map);
Mike Travis71fff5e2007-10-19 20:35:03 +0200580 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700581 return -EIO;
582 }
583
584 return 0;
585}
586
587cycles_t cacheflush_time;
588unsigned long cache_decay_ticks;
589
590/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700591 * Fall back to non SMP mode after errors.
592 *
593 * RED-PEN audit/test this more. I bet there is more state messed up here.
594 */
Ashok Raje6982c62005-06-25 14:54:58 -0700595static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700596{
597 cpu_present_map = cpumask_of_cpu(0);
598 cpu_possible_map = cpumask_of_cpu(0);
599 if (smp_found_config)
600 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
601 else
602 phys_cpu_present_map = physid_mask_of_physid(0);
Mike Travisd5a74302007-10-16 01:24:05 -0700603 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700604 cpu_set(0, per_cpu(cpu_core_map, 0));
Andi Kleena8ab26f2005-04-16 15:25:19 -0700605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700608 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Ashok Raje6982c62005-06-25 14:54:58 -0700610static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
613 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
614 hard_smp_processor_id());
615 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
616 }
617
618 /*
619 * If we couldn't find an SMP configuration at boot time,
620 * get out of here now!
621 */
622 if (!smp_found_config) {
623 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700624 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (APIC_init_uniprocessor())
626 printk(KERN_NOTICE "Local APIC not detected."
627 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700628 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
631 /*
632 * Should not be necessary because the MP table should list the boot
633 * CPU too, but we do it for the sake of robustness anyway.
634 */
635 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
636 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
637 boot_cpu_id);
638 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
639 }
640
641 /*
642 * If we couldn't find a local APIC, then get out of here now!
643 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100644 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
646 boot_cpu_id);
647 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700648 nr_ioapics = 0;
649 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * If SMP should be disabled, then really disable it!
654 */
655 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700657 nr_ioapics = 0;
658 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Andi Kleena8ab26f2005-04-16 15:25:19 -0700661 return 0;
662}
663
Yinghai Lu949ec322008-01-30 13:30:46 +0100664static void __init smp_cpu_index_default(void)
665{
666 int i;
667 struct cpuinfo_x86 *c;
668
669 for_each_cpu_mask(i, cpu_possible_map) {
670 c = &cpu_data(i);
671 /* mark all to hotplug */
672 c->cpu_index = NR_CPUS;
673 }
674}
675
Mike Travis71fff5e2007-10-19 20:35:03 +0200676/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700677 * Prepare for SMP bootup. The MP table or ACPI has been read
678 * earlier. Just do some sanity checking here and enable APIC mode.
679 */
Glauber Costa7557da62008-03-03 14:12:38 -0300680void __init native_smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700681{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700682 nmi_watchdog_default();
Yinghai Lu949ec322008-01-30 13:30:46 +0100683 smp_cpu_index_default();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700684 current_cpu_data = boot_cpu_data;
685 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100686 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700687
Andi Kleena8ab26f2005-04-16 15:25:19 -0700688 if (smp_sanity_check(max_cpus) < 0) {
689 printk(KERN_INFO "SMP disabled\n");
690 disable_smp();
691 return;
692 }
693
694
695 /*
696 * Switch from PIC to APIC mode.
697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 setup_local_APIC();
699
Andi Kleen739f33b2008-01-30 13:30:40 +0100700 /*
701 * Enable IO APIC before setting up error vector
702 */
703 if (!skip_ioapic_setup && nr_ioapics)
704 enable_IO_APIC();
705 end_local_APIC_setup();
706
Andi Kleena8ab26f2005-04-16 15:25:19 -0700707 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
708 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
709 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
710 /* Or can we switch back to PIC here? */
711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700714 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
716 if (!skip_ioapic_setup && nr_ioapics)
717 setup_IO_APIC();
718 else
719 nr_ioapics = 0;
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700722 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100725 setup_boot_clock();
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300726 printk(KERN_INFO "CPU%d: ", 0);
727 print_cpu_info(&cpu_data(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Andi Kleena8ab26f2005-04-16 15:25:19 -0700730/*
731 * Early setup to make printk work.
732 */
Glauber Costa1e3fac82008-03-03 14:12:37 -0300733void __init native_smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700735 int me = smp_processor_id();
Yinghai Lu23916d42008-01-30 13:33:39 +0100736 /* already set me in cpu_online_map in boot_cpu_init() */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700737 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700738 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Andi Kleena8ab26f2005-04-16 15:25:19 -0700741/*
742 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700743 */
Glauber Costa71d19542008-03-03 14:12:36 -0300744int __cpuinit native_cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700746 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100747 unsigned long flags;
748 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Andi Kleena8ab26f2005-04-16 15:25:19 -0700750 WARN_ON(irqs_disabled());
751
752 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
753
754 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
755 !physid_isset(apicid, phys_cpu_present_map)) {
756 printk("__cpu_up: bad cpu %d\n", cpu);
757 return -EINVAL;
758 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700759
Ashok Raj76e4f662005-06-25 14:55:00 -0700760 /*
761 * Already booted CPU?
762 */
763 if (cpu_isset(cpu, cpu_callin_map)) {
764 Dprintk("do_boot_cpu %d Already started\n", cpu);
765 return -ENOSYS;
766 }
767
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200768 /*
769 * Save current MTRR state in case it was changed since early boot
770 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
771 */
772 mtrr_save_state();
773
Ashok Raj884d9e42005-06-25 14:55:02 -0700774 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700775 /* Boot it! */
776 err = do_boot_cpu(cpu, apicid);
777 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700778 Dprintk("do_boot_cpu failed %d\n", err);
779 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* Unleash the CPU! */
783 Dprintk("waiting for cpu %d\n", cpu);
784
Ingo Molnar95492e42007-02-16 01:27:34 -0800785 /*
786 * Make sure and check TSC sync:
787 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100788 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800789 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100790 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700793 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700794 err = 0;
795
796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300799extern void impress_friends(void);
800extern void smp_checks(void);
801
Andi Kleena8ab26f2005-04-16 15:25:19 -0700802/*
803 * Finish the SMP boot.
804 */
Glauber Costac5597642008-03-03 14:12:39 -0300805void __init native_smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -0300807 smpboot_restore_warm_reset_vector();
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300808
809 Dprintk("Boot done.\n");
810
811 impress_friends();
812 smp_checks();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700814 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700815}