blob: d7b59d6c6963275baaefe65b59a0f9f5006d7b8f [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
Andi Kleena8ab26f2005-04-16 15:25:19 -070063/* Set when the idlers are all forked */
64int smp_threads_ready;
65
Ashok Raj76e4f662005-06-25 14:55:00 -070066/* State of each CPU */
67DEFINE_PER_CPU(int, cpu_state) = { 0 };
68
69/*
70 * Store all idle threads, this can be reused instead of creating
71 * a new thread. Also avoids complicated thread destroy functionality
72 * for idle threads.
73 */
travis@sgi.com24b0d222008-01-30 13:33:11 +010074#ifdef CONFIG_HOTPLUG_CPU
75/*
76 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
77 * removed after init for !CONFIG_HOTPLUG_CPU.
78 */
79static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
80#define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
81#define set_idle_for_cpu(x,p) (per_cpu(idle_thread_array, x) = (p))
82#else
Ashok Raj76e4f662005-06-25 14:55:00 -070083struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
Ashok Raj76e4f662005-06-25 14:55:00 -070084#define get_idle_for_cpu(x) (idle_thread_array[(x)])
85#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
travis@sgi.com24b0d222008-01-30 13:33:11 +010086#endif
87
Glauber Costae90009b2008-03-03 14:13:13 -030088static inline void wait_for_init_deassert(atomic_t *deassert)
89{
90 while (!atomic_read(deassert))
91 cpu_relax();
92 return;
93}
94
Andi Kleena8ab26f2005-04-16 15:25:19 -070095static atomic_t init_deasserted __cpuinitdata;
96
97/*
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");
159 setup_local_APIC();
Andi Kleen739f33b2008-01-30 13:30:40 +0100160 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200164 *
165 * Need to enable IRQs because it can take longer and then
166 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Andi Kleenb4452212005-09-12 18:49:24 +0200168 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200170 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 Dprintk("Stack at about %p\n",&cpuid);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /*
174 * Save our processor parameters
175 */
176 smp_store_cpu_info(cpuid);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /*
179 * Allow the master to continue.
180 */
181 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700185 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700187void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 /*
190 * Dont put anything before smp_callin(), SMP
191 * booting is too fragile that we want to limit the
192 * things done here to the most necessary things.
193 */
194 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800195 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 smp_callin();
197
198 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
199 barrier();
200
Ingo Molnar95492e42007-02-16 01:27:34 -0800201 /*
202 * Check TSC sync first:
203 */
204 check_tsc_sync_target();
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (nmi_watchdog == NMI_IO_APIC) {
207 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100208 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 enable_8259A_irq(0);
210 }
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700213 * The sibling maps must be set before turing the online map on for
214 * this cpu
215 */
216 set_cpu_sibling_map(smp_processor_id());
217
218 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700219 * We need to hold call_lock, so there is no inconsistency
220 * between the time smp_call_function() determines number of
Simon Arlott676b1852007-10-20 01:25:36 +0200221 * IPI recipients, and the time when the determination is made
Ashok Raj884d9e42005-06-25 14:55:02 -0700222 * for which cpus receive the IPI in genapic_flat.c. Holding this
223 * lock helps us to not include this cpu in a currently in progress
224 * smp_call_function().
225 */
226 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200227 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700228
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200229 /* Setup the per cpu irq handling data structures */
230 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700231 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700232 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Glauber Costafc25da92008-03-03 14:13:04 -0300234 spin_unlock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700236 unlock_ipi_call_lock();
237
Glauber de Oliveira Costa5733f622008-03-19 14:25:09 -0300238 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
239
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100240 setup_secondary_clock();
Thomas Gleixner3ac508b2007-10-14 22:57:45 +0200241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 cpu_idle();
243}
244
Andi Kleena8ab26f2005-04-16 15:25:19 -0700245extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246extern void (*initial_code)(void);
247
Olaf Hering44456d32005-07-27 11:45:17 -0700248#ifdef APIC_DEBUG
Andi Kleena8ab26f2005-04-16 15:25:19 -0700249static void inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
252 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200253 int timeout;
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100254 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
257
Alejandro Martinez Ruiz4d022ad2007-10-17 14:38:58 +0200258 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100259 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /*
262 * Wait for idle.
263 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200264 status = safe_apic_wait_icr_idle();
265 if (status)
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100266 printk(KERN_CONT
267 "a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300269 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
270 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 timeout = 0;
273 do {
274 udelay(100);
275 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
276 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
277
278 switch (status) {
279 case APIC_ICR_RR_VALID:
280 status = apic_read(APIC_RRR);
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100281 printk(KERN_CONT "%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 default:
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100284 printk(KERN_CONT "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 }
287}
288#endif
289
Andi Kleena8ab26f2005-04-16 15:25:19 -0700290/*
291 * Kick the secondary to wake up.
292 */
293static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200295 unsigned long send_status, accept_status = 0;
296 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300298 /*
299 * Be paranoid about clearing APIC errors.
300 */
301 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
302 apic_read_around(APIC_SPIV);
303 apic_write(APIC_ESR, 0);
304 apic_read(APIC_ESR);
305 }
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 Dprintk("Asserting INIT.\n");
308
309 /*
310 * Turn INIT on target chip
311 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300312 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /*
315 * Send IPI
316 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300317 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 | APIC_DM_INIT);
319
320 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200321 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 mdelay(10);
324
325 Dprintk("Deasserting INIT.\n");
326
327 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300328 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Send IPI */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300331 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200334 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100336 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 atomic_set(&init_deasserted, 1);
338
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300339 if (APIC_INTEGRATED(apic_version[phys_apicid]))
340 num_starts = 2;
341 else
342 num_starts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
Glauber de Oliveira Costad0173ae2008-03-19 14:24:59 -0300345 * Paravirt / VMI wants a startup IPI hook here to set up the
346 * target processor state.
347 */
348 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
349 (unsigned long) init_rsp);
350
351
352 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * Run STARTUP IPI loop.
354 */
355 Dprintk("#startup loops: %d.\n", num_starts);
356
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100357 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 for (j = 1; j <= num_starts; j++) {
360 Dprintk("Sending STARTUP #%d.\n",j);
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300361 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 apic_write(APIC_ESR, 0);
363 apic_read(APIC_ESR);
364 Dprintk("After apic_write.\n");
365
366 /*
367 * STARTUP IPI
368 */
369
370 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300371 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /* Boot on the stack */
374 /* Kick the second */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300375 apic_write_around(APIC_ICR, APIC_DM_STARTUP | (start_rip>>12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /*
378 * Give the other CPU some time to accept the IPI.
379 */
380 udelay(300);
381
382 Dprintk("Startup point 1.\n");
383
384 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200385 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /*
388 * Give the other CPU some time to accept the IPI.
389 */
390 udelay(200);
391 /*
392 * Due to the Pentium erratum 3AP.
393 */
394 if (maxlvt > 3) {
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300395 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 apic_write(APIC_ESR, 0);
397 }
398 accept_status = (apic_read(APIC_ESR) & 0xEF);
399 if (send_status || accept_status)
400 break;
401 }
402 Dprintk("After Startup.\n");
403
404 if (send_status)
405 printk(KERN_ERR "APIC never delivered???\n");
406 if (accept_status)
407 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
408
409 return (send_status | accept_status);
410}
411
Ashok Raj76e4f662005-06-25 14:55:00 -0700412struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000413 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700414 struct task_struct *idle;
415 struct completion done;
416 int cpu;
417};
418
Thomas Gleixnera2b484a2008-01-09 00:18:28 +0100419static void __cpuinit do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700420{
David Howells65f27f32006-11-22 14:55:48 +0000421 struct create_idle *c_idle =
422 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700423
424 c_idle->idle = fork_idle(c_idle->cpu);
425 complete(&c_idle->done);
426}
427
Andi Kleena8ab26f2005-04-16 15:25:19 -0700428/*
429 * Boot one CPU.
430 */
431static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700434 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700436 struct create_idle c_idle = {
437 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700438 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700439 };
Glauber Costa2b775a22008-02-22 12:09:29 -0300440 INIT_WORK(&c_idle.work, do_fork_idle);
Ashok Raj76e4f662005-06-25 14:55:00 -0700441
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100442 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
443 if (!cpu_gdt_descr[cpu].address &&
444 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
445 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
446 return -1;
447 }
448
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100449 /* Allocate node local memory for AP pdas */
450 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
451 struct x8664_pda *newpda, *pda;
452 int node = cpu_to_node(cpu);
453 pda = cpu_pda(cpu);
454 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
455 node);
456 if (newpda) {
457 memcpy(newpda, pda, sizeof (struct x8664_pda));
458 cpu_pda(cpu) = newpda;
459 } else
460 printk(KERN_ERR
461 "Could not allocate node local PDA for CPU %d on node %d\n",
462 cpu, node);
463 }
464
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200465 alternatives_smp_switch(1);
466
Ashok Raj76e4f662005-06-25 14:55:00 -0700467 c_idle.idle = get_idle_for_cpu(cpu);
468
469 if (c_idle.idle) {
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100470 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800471 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700472 init_idle(c_idle.idle, cpu);
473 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Ashok Raj76e4f662005-06-25 14:55:00 -0700476 /*
477 * During cold boot process, keventd thread is not spun up yet.
478 * When we do cpu hot-add, we create idle threads on the fly, we should
479 * not acquire any attributes from the calling context. Hence the clean
480 * way to create kernel_threads() is to do that from keventd().
481 * We do the current_is_keventd() due to the fact that ACPI notifier
482 * was also queuing to keventd() and when the caller is already running
483 * in context of keventd(), we would end up with locking up the keventd
484 * thread.
485 */
486 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000487 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700488 else {
David Howells65f27f32006-11-22 14:55:48 +0000489 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700490 wait_for_completion(&c_idle.done);
491 }
492
493 if (IS_ERR(c_idle.idle)) {
494 printk("failed fork for CPU %d\n", cpu);
495 return PTR_ERR(c_idle.idle);
496 }
497
498 set_idle_for_cpu(cpu, c_idle.idle);
499
500do_rest:
501
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100502 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 start_rip = setup_trampoline();
505
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100506 init_rsp = c_idle.idle->thread.sp;
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100507 load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800509 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Andi Kleende04f322005-07-28 21:15:29 -0700511 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
512 cpus_weight(cpu_present_map),
513 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /*
516 * This grunge runs the startup process for
517 * the targeted processor.
518 */
519
520 atomic_set(&init_deasserted, 0);
521
522 Dprintk("Setting warm reset code and vector.\n");
523
524 CMOS_WRITE(0xa, 0xf);
525 local_flush_tlb();
526 Dprintk("1.\n");
527 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
528 Dprintk("2.\n");
529 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
530 Dprintk("3.\n");
531
532 /*
533 * Be paranoid about clearing APIC errors.
534 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100535 apic_write(APIC_ESR, 0);
536 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /*
539 * Status is now clean
540 */
541 boot_error = 0;
542
543 /*
544 * Starting actual IPI sequence...
545 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700546 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (!boot_error) {
549 /*
550 * allow APs to start initializing.
551 */
552 Dprintk("Before Callout %d.\n", cpu);
553 cpu_set(cpu, cpu_callout_map);
554 Dprintk("After Callout %d.\n", cpu);
555
556 /*
557 * Wait 5s total for a response
558 */
559 for (timeout = 0; timeout < 50000; timeout++) {
560 if (cpu_isset(cpu, cpu_callin_map))
561 break; /* It has booted */
562 udelay(100);
563 }
564
565 if (cpu_isset(cpu, cpu_callin_map)) {
566 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 Dprintk("CPU has booted.\n");
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300568 printk(KERN_INFO "CPU%d: ", cpu);
569 print_cpu_info(&cpu_data(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 } else {
571 boot_error = 1;
572 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
573 == 0xA5)
574 /* trampoline started but...? */
575 printk("Stuck ??\n");
576 else
577 /* trampoline code not run */
578 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700579#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 inquire_remote_apic(apicid);
581#endif
582 }
583 }
584 if (boot_error) {
585 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
Jeremy Fitzhardinge5548fec2008-01-30 13:30:55 +0100586 clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800587 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700588 cpu_clear(cpu, cpu_present_map);
589 cpu_clear(cpu, cpu_possible_map);
Mike Travis71fff5e2007-10-19 20:35:03 +0200590 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700591 return -EIO;
592 }
593
594 return 0;
595}
596
597cycles_t cacheflush_time;
598unsigned long cache_decay_ticks;
599
600/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700601 * Cleanup possible dangling ends...
602 */
603static __cpuinit void smp_cleanup_boot(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700606 * Paranoid: Set warm reset code and vector here back
607 * to default values.
608 */
609 CMOS_WRITE(0, 0xf);
610
611 /*
612 * Reset trampoline flag
613 */
614 *((volatile int *) phys_to_virt(0x467)) = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700615}
616
617/*
618 * Fall back to non SMP mode after errors.
619 *
620 * RED-PEN audit/test this more. I bet there is more state messed up here.
621 */
Ashok Raje6982c62005-06-25 14:54:58 -0700622static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700623{
624 cpu_present_map = cpumask_of_cpu(0);
625 cpu_possible_map = cpumask_of_cpu(0);
626 if (smp_found_config)
627 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
628 else
629 phys_cpu_present_map = physid_mask_of_physid(0);
Mike Travisd5a74302007-10-16 01:24:05 -0700630 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700631 cpu_set(0, per_cpu(cpu_core_map, 0));
Andi Kleena8ab26f2005-04-16 15:25:19 -0700632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700635 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Ashok Raje6982c62005-06-25 14:54:58 -0700637static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
640 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
641 hard_smp_processor_id());
642 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
643 }
644
645 /*
646 * If we couldn't find an SMP configuration at boot time,
647 * get out of here now!
648 */
649 if (!smp_found_config) {
650 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700651 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (APIC_init_uniprocessor())
653 printk(KERN_NOTICE "Local APIC not detected."
654 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700655 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
658 /*
659 * Should not be necessary because the MP table should list the boot
660 * CPU too, but we do it for the sake of robustness anyway.
661 */
662 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
663 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
664 boot_cpu_id);
665 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
666 }
667
668 /*
669 * If we couldn't find a local APIC, then get out of here now!
670 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100671 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
673 boot_cpu_id);
674 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700675 nr_ioapics = 0;
676 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
680 * If SMP should be disabled, then really disable it!
681 */
682 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700684 nr_ioapics = 0;
685 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
Andi Kleena8ab26f2005-04-16 15:25:19 -0700688 return 0;
689}
690
Yinghai Lu949ec322008-01-30 13:30:46 +0100691static void __init smp_cpu_index_default(void)
692{
693 int i;
694 struct cpuinfo_x86 *c;
695
696 for_each_cpu_mask(i, cpu_possible_map) {
697 c = &cpu_data(i);
698 /* mark all to hotplug */
699 c->cpu_index = NR_CPUS;
700 }
701}
702
Mike Travis71fff5e2007-10-19 20:35:03 +0200703/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700704 * Prepare for SMP bootup. The MP table or ACPI has been read
705 * earlier. Just do some sanity checking here and enable APIC mode.
706 */
Glauber Costa7557da62008-03-03 14:12:38 -0300707void __init native_smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700708{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700709 nmi_watchdog_default();
Yinghai Lu949ec322008-01-30 13:30:46 +0100710 smp_cpu_index_default();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700711 current_cpu_data = boot_cpu_data;
712 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100713 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700714
Andi Kleena8ab26f2005-04-16 15:25:19 -0700715 if (smp_sanity_check(max_cpus) < 0) {
716 printk(KERN_INFO "SMP disabled\n");
717 disable_smp();
718 return;
719 }
720
721
722 /*
723 * Switch from PIC to APIC mode.
724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 setup_local_APIC();
726
Andi Kleen739f33b2008-01-30 13:30:40 +0100727 /*
728 * Enable IO APIC before setting up error vector
729 */
730 if (!skip_ioapic_setup && nr_ioapics)
731 enable_IO_APIC();
732 end_local_APIC_setup();
733
Andi Kleena8ab26f2005-04-16 15:25:19 -0700734 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
735 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
736 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
737 /* Or can we switch back to PIC here? */
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700741 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
743 if (!skip_ioapic_setup && nr_ioapics)
744 setup_IO_APIC();
745 else
746 nr_ioapics = 0;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700749 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100752 setup_boot_clock();
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300753 printk(KERN_INFO "CPU%d: ", 0);
754 print_cpu_info(&cpu_data(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Andi Kleena8ab26f2005-04-16 15:25:19 -0700757/*
758 * Early setup to make printk work.
759 */
Glauber Costa1e3fac82008-03-03 14:12:37 -0300760void __init native_smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700762 int me = smp_processor_id();
Yinghai Lu23916d42008-01-30 13:33:39 +0100763 /* already set me in cpu_online_map in boot_cpu_init() */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700764 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700765 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Andi Kleena8ab26f2005-04-16 15:25:19 -0700768/*
769 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700770 */
Glauber Costa71d19542008-03-03 14:12:36 -0300771int __cpuinit native_cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700773 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100774 unsigned long flags;
775 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Andi Kleena8ab26f2005-04-16 15:25:19 -0700777 WARN_ON(irqs_disabled());
778
779 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
780
781 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
782 !physid_isset(apicid, phys_cpu_present_map)) {
783 printk("__cpu_up: bad cpu %d\n", cpu);
784 return -EINVAL;
785 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700786
Ashok Raj76e4f662005-06-25 14:55:00 -0700787 /*
788 * Already booted CPU?
789 */
790 if (cpu_isset(cpu, cpu_callin_map)) {
791 Dprintk("do_boot_cpu %d Already started\n", cpu);
792 return -ENOSYS;
793 }
794
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200795 /*
796 * Save current MTRR state in case it was changed since early boot
797 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
798 */
799 mtrr_save_state();
800
Ashok Raj884d9e42005-06-25 14:55:02 -0700801 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700802 /* Boot it! */
803 err = do_boot_cpu(cpu, apicid);
804 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700805 Dprintk("do_boot_cpu failed %d\n", err);
806 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /* Unleash the CPU! */
810 Dprintk("waiting for cpu %d\n", cpu);
811
Ingo Molnar95492e42007-02-16 01:27:34 -0800812 /*
813 * Make sure and check TSC sync:
814 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100815 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800816 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100817 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700820 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700821 err = 0;
822
823 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Andi Kleena8ab26f2005-04-16 15:25:19 -0700826/*
827 * Finish the SMP boot.
828 */
Glauber Costac5597642008-03-03 14:12:39 -0300829void __init native_smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700831 smp_cleanup_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700833 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700834}