blob: 13ab1123d1d266c0057e6de90eec1c273f7667e4 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * The bootstrap kernel entry code has set these up. Save them for
90 * a given CPU
91 */
92
Andi Kleena8ab26f2005-04-16 15:25:19 -070093static void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Mike Travis92cb7612007-10-19 20:35:04 +020095 struct cpuinfo_x86 *c = &cpu_data(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 *c = boot_cpu_data;
Mike Travisfbdcf182007-12-19 23:20:19 +010098 c->cpu_index = id;
Linus Torvalds00684412007-12-25 20:16:16 -080099 identify_cpu(c);
Andi Kleendda50e72005-05-16 21:53:25 -0700100 print_cpu_info(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Glauber Costae90009b2008-03-03 14:13:13 -0300103static inline void wait_for_init_deassert(atomic_t *deassert)
104{
105 while (!atomic_read(deassert))
106 cpu_relax();
107 return;
108}
109
Andi Kleena8ab26f2005-04-16 15:25:19 -0700110static atomic_t init_deasserted __cpuinitdata;
111
112/*
113 * Report back to the Boot Processor.
114 * Running on AP.
115 */
116void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 int cpuid, phys_id;
119 unsigned long timeout;
120
121 /*
122 * If waken up by an INIT in an 82489DX configuration
123 * we may get here before an INIT-deassert IPI reaches
124 * our local APIC. We have to wait for the IPI or we'll
125 * lock up on an APIC access.
126 */
Glauber Costae90009b2008-03-03 14:13:13 -0300127 wait_for_init_deassert(&init_deasserted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /*
130 * (This works even if the APIC is not enabled.)
131 */
132 phys_id = GET_APIC_ID(apic_read(APIC_ID));
133 cpuid = smp_processor_id();
134 if (cpu_isset(cpuid, cpu_callin_map)) {
135 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
136 phys_id, cpuid);
137 }
138 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
139
140 /*
141 * STARTUP IPIs are fragile beasts as they might sometimes
142 * trigger some glue motherboard logic. Complete APIC bus
143 * silence for 1 second, this overestimates the time the
144 * boot CPU is spending to send the up to 2 STARTUP IPIs
145 * by a factor of two. This should be enough.
146 */
147
148 /*
149 * Waiting 2s total for startup (udelay is not yet working)
150 */
151 timeout = jiffies + 2*HZ;
152 while (time_before(jiffies, timeout)) {
153 /*
154 * Has the boot CPU finished it's STARTUP sequence?
155 */
156 if (cpu_isset(cpuid, cpu_callout_map))
157 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700158 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161 if (!time_before(jiffies, timeout)) {
162 panic("smp_callin: CPU%d started up but did not get a callout!\n",
163 cpuid);
164 }
165
166 /*
167 * the boot CPU has finished the init stage and is spinning
168 * on callin_map until we finish. We are free to set up this
169 * CPU, first the APIC. (this is probably redundant on most
170 * boards)
171 */
172
173 Dprintk("CALLIN, before setup_local_APIC().\n");
174 setup_local_APIC();
Andi Kleen739f33b2008-01-30 13:30:40 +0100175 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /*
178 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200179 *
180 * Need to enable IRQs because it can take longer and then
181 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 */
Andi Kleenb4452212005-09-12 18:49:24 +0200183 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200185 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 Dprintk("Stack at about %p\n",&cpuid);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /*
189 * Save our processor parameters
190 */
191 smp_store_cpu_info(cpuid);
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
194 * Allow the master to continue.
195 */
196 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700200 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700202void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 /*
205 * Dont put anything before smp_callin(), SMP
206 * booting is too fragile that we want to limit the
207 * things done here to the most necessary things.
208 */
209 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800210 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 smp_callin();
212
213 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
214 barrier();
215
Ingo Molnar95492e42007-02-16 01:27:34 -0800216 /*
217 * Check TSC sync first:
218 */
219 check_tsc_sync_target();
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (nmi_watchdog == NMI_IO_APIC) {
222 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100223 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 enable_8259A_irq(0);
225 }
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700228 * The sibling maps must be set before turing the online map on for
229 * this cpu
230 */
231 set_cpu_sibling_map(smp_processor_id());
232
233 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700234 * We need to hold call_lock, so there is no inconsistency
235 * between the time smp_call_function() determines number of
Simon Arlott676b1852007-10-20 01:25:36 +0200236 * IPI recipients, and the time when the determination is made
Ashok Raj884d9e42005-06-25 14:55:02 -0700237 * for which cpus receive the IPI in genapic_flat.c. Holding this
238 * lock helps us to not include this cpu in a currently in progress
239 * smp_call_function().
240 */
241 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200242 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700243
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200244 /* Setup the per cpu irq handling data structures */
245 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700246 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700247 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Glauber Costafc25da92008-03-03 14:13:04 -0300249 spin_unlock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700251 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Ingo Molnar95492e42007-02-16 01:27:34 -0800252
Ashok Raj884d9e42005-06-25 14:55:02 -0700253 unlock_ipi_call_lock();
254
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100255 setup_secondary_clock();
Thomas Gleixner3ac508b2007-10-14 22:57:45 +0200256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 cpu_idle();
258}
259
Andi Kleena8ab26f2005-04-16 15:25:19 -0700260extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261extern void (*initial_code)(void);
262
Olaf Hering44456d32005-07-27 11:45:17 -0700263#ifdef APIC_DEBUG
Andi Kleena8ab26f2005-04-16 15:25:19 -0700264static void inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
267 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200268 int timeout;
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100269 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
272
Alejandro Martinez Ruiz4d022ad2007-10-17 14:38:58 +0200273 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100274 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /*
277 * Wait for idle.
278 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200279 status = safe_apic_wait_icr_idle();
280 if (status)
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100281 printk(KERN_CONT
282 "a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300284 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
285 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 timeout = 0;
288 do {
289 udelay(100);
290 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
291 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
292
293 switch (status) {
294 case APIC_ICR_RR_VALID:
295 status = apic_read(APIC_RRR);
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100296 printk(KERN_CONT "%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
298 default:
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100299 printk(KERN_CONT "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 }
302}
303#endif
304
Andi Kleena8ab26f2005-04-16 15:25:19 -0700305/*
306 * Kick the secondary to wake up.
307 */
308static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200310 unsigned long send_status, accept_status = 0;
311 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 Dprintk("Asserting INIT.\n");
314
315 /*
316 * Turn INIT on target chip
317 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300318 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /*
321 * Send IPI
322 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300323 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 | APIC_DM_INIT);
325
326 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200327 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 mdelay(10);
330
331 Dprintk("Deasserting INIT.\n");
332
333 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300334 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* Send IPI */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300337 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200340 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100342 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 atomic_set(&init_deasserted, 1);
344
Andi Kleen5a40b7c2005-09-12 18:49:24 +0200345 num_starts = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /*
Glauber de Oliveira Costad0173ae2008-03-19 14:24:59 -0300348 * Paravirt / VMI wants a startup IPI hook here to set up the
349 * target processor state.
350 */
351 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
352 (unsigned long) init_rsp);
353
354
355 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * Run STARTUP IPI loop.
357 */
358 Dprintk("#startup loops: %d.\n", num_starts);
359
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100360 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 for (j = 1; j <= num_starts; j++) {
363 Dprintk("Sending STARTUP #%d.\n",j);
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300364 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 apic_write(APIC_ESR, 0);
366 apic_read(APIC_ESR);
367 Dprintk("After apic_write.\n");
368
369 /*
370 * STARTUP IPI
371 */
372
373 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300374 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* Boot on the stack */
377 /* Kick the second */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300378 apic_write_around(APIC_ICR, APIC_DM_STARTUP | (start_rip>>12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /*
381 * Give the other CPU some time to accept the IPI.
382 */
383 udelay(300);
384
385 Dprintk("Startup point 1.\n");
386
387 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200388 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /*
391 * Give the other CPU some time to accept the IPI.
392 */
393 udelay(200);
394 /*
395 * Due to the Pentium erratum 3AP.
396 */
397 if (maxlvt > 3) {
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300398 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 apic_write(APIC_ESR, 0);
400 }
401 accept_status = (apic_read(APIC_ESR) & 0xEF);
402 if (send_status || accept_status)
403 break;
404 }
405 Dprintk("After Startup.\n");
406
407 if (send_status)
408 printk(KERN_ERR "APIC never delivered???\n");
409 if (accept_status)
410 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
411
412 return (send_status | accept_status);
413}
414
Ashok Raj76e4f662005-06-25 14:55:00 -0700415struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000416 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700417 struct task_struct *idle;
418 struct completion done;
419 int cpu;
420};
421
Thomas Gleixnera2b484a2008-01-09 00:18:28 +0100422static void __cpuinit do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700423{
David Howells65f27f32006-11-22 14:55:48 +0000424 struct create_idle *c_idle =
425 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700426
427 c_idle->idle = fork_idle(c_idle->cpu);
428 complete(&c_idle->done);
429}
430
Andi Kleena8ab26f2005-04-16 15:25:19 -0700431/*
432 * Boot one CPU.
433 */
434static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned long boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700437 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700439 struct create_idle c_idle = {
440 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700441 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700442 };
Glauber Costa2b775a22008-02-22 12:09:29 -0300443 INIT_WORK(&c_idle.work, do_fork_idle);
Ashok Raj76e4f662005-06-25 14:55:00 -0700444
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100445 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
446 if (!cpu_gdt_descr[cpu].address &&
447 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
448 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
449 return -1;
450 }
451
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100452 /* Allocate node local memory for AP pdas */
453 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
454 struct x8664_pda *newpda, *pda;
455 int node = cpu_to_node(cpu);
456 pda = cpu_pda(cpu);
457 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
458 node);
459 if (newpda) {
460 memcpy(newpda, pda, sizeof (struct x8664_pda));
461 cpu_pda(cpu) = newpda;
462 } else
463 printk(KERN_ERR
464 "Could not allocate node local PDA for CPU %d on node %d\n",
465 cpu, node);
466 }
467
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200468 alternatives_smp_switch(1);
469
Ashok Raj76e4f662005-06-25 14:55:00 -0700470 c_idle.idle = get_idle_for_cpu(cpu);
471
472 if (c_idle.idle) {
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100473 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800474 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700475 init_idle(c_idle.idle, cpu);
476 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Ashok Raj76e4f662005-06-25 14:55:00 -0700479 /*
480 * During cold boot process, keventd thread is not spun up yet.
481 * When we do cpu hot-add, we create idle threads on the fly, we should
482 * not acquire any attributes from the calling context. Hence the clean
483 * way to create kernel_threads() is to do that from keventd().
484 * We do the current_is_keventd() due to the fact that ACPI notifier
485 * was also queuing to keventd() and when the caller is already running
486 * in context of keventd(), we would end up with locking up the keventd
487 * thread.
488 */
489 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000490 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700491 else {
David Howells65f27f32006-11-22 14:55:48 +0000492 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700493 wait_for_completion(&c_idle.done);
494 }
495
496 if (IS_ERR(c_idle.idle)) {
497 printk("failed fork for CPU %d\n", cpu);
498 return PTR_ERR(c_idle.idle);
499 }
500
501 set_idle_for_cpu(cpu, c_idle.idle);
502
503do_rest:
504
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100505 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 start_rip = setup_trampoline();
508
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100509 init_rsp = c_idle.idle->thread.sp;
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100510 load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800512 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Andi Kleende04f322005-07-28 21:15:29 -0700514 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
515 cpus_weight(cpu_present_map),
516 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /*
519 * This grunge runs the startup process for
520 * the targeted processor.
521 */
522
523 atomic_set(&init_deasserted, 0);
524
525 Dprintk("Setting warm reset code and vector.\n");
526
527 CMOS_WRITE(0xa, 0xf);
528 local_flush_tlb();
529 Dprintk("1.\n");
530 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
531 Dprintk("2.\n");
532 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
533 Dprintk("3.\n");
534
535 /*
536 * Be paranoid about clearing APIC errors.
537 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100538 apic_write(APIC_ESR, 0);
539 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /*
542 * Status is now clean
543 */
544 boot_error = 0;
545
546 /*
547 * Starting actual IPI sequence...
548 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700549 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (!boot_error) {
552 /*
553 * allow APs to start initializing.
554 */
555 Dprintk("Before Callout %d.\n", cpu);
556 cpu_set(cpu, cpu_callout_map);
557 Dprintk("After Callout %d.\n", cpu);
558
559 /*
560 * Wait 5s total for a response
561 */
562 for (timeout = 0; timeout < 50000; timeout++) {
563 if (cpu_isset(cpu, cpu_callin_map))
564 break; /* It has booted */
565 udelay(100);
566 }
567
568 if (cpu_isset(cpu, cpu_callin_map)) {
569 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 Dprintk("CPU has booted.\n");
571 } else {
572 boot_error = 1;
573 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
574 == 0xA5)
575 /* trampoline started but...? */
576 printk("Stuck ??\n");
577 else
578 /* trampoline code not run */
579 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700580#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 inquire_remote_apic(apicid);
582#endif
583 }
584 }
585 if (boot_error) {
586 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
Jeremy Fitzhardinge5548fec2008-01-30 13:30:55 +0100587 clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800588 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700589 cpu_clear(cpu, cpu_present_map);
590 cpu_clear(cpu, cpu_possible_map);
Mike Travis71fff5e2007-10-19 20:35:03 +0200591 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700592 return -EIO;
593 }
594
595 return 0;
596}
597
598cycles_t cacheflush_time;
599unsigned long cache_decay_ticks;
600
601/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700602 * Cleanup possible dangling ends...
603 */
604static __cpuinit void smp_cleanup_boot(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700607 * Paranoid: Set warm reset code and vector here back
608 * to default values.
609 */
610 CMOS_WRITE(0, 0xf);
611
612 /*
613 * Reset trampoline flag
614 */
615 *((volatile int *) phys_to_virt(0x467)) = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700616}
617
618/*
619 * Fall back to non SMP mode after errors.
620 *
621 * RED-PEN audit/test this more. I bet there is more state messed up here.
622 */
Ashok Raje6982c62005-06-25 14:54:58 -0700623static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700624{
625 cpu_present_map = cpumask_of_cpu(0);
626 cpu_possible_map = cpumask_of_cpu(0);
627 if (smp_found_config)
628 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
629 else
630 phys_cpu_present_map = physid_mask_of_physid(0);
Mike Travisd5a74302007-10-16 01:24:05 -0700631 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700632 cpu_set(0, per_cpu(cpu_core_map, 0));
Andi Kleena8ab26f2005-04-16 15:25:19 -0700633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700636 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Ashok Raje6982c62005-06-25 14:54:58 -0700638static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
641 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
642 hard_smp_processor_id());
643 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
644 }
645
646 /*
647 * If we couldn't find an SMP configuration at boot time,
648 * get out of here now!
649 */
650 if (!smp_found_config) {
651 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700652 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (APIC_init_uniprocessor())
654 printk(KERN_NOTICE "Local APIC not detected."
655 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700656 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
659 /*
660 * Should not be necessary because the MP table should list the boot
661 * CPU too, but we do it for the sake of robustness anyway.
662 */
663 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
664 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
665 boot_cpu_id);
666 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
667 }
668
669 /*
670 * If we couldn't find a local APIC, then get out of here now!
671 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100672 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
674 boot_cpu_id);
675 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700676 nr_ioapics = 0;
677 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /*
681 * If SMP should be disabled, then really disable it!
682 */
683 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700685 nr_ioapics = 0;
686 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
Andi Kleena8ab26f2005-04-16 15:25:19 -0700689 return 0;
690}
691
Yinghai Lu949ec322008-01-30 13:30:46 +0100692static void __init smp_cpu_index_default(void)
693{
694 int i;
695 struct cpuinfo_x86 *c;
696
697 for_each_cpu_mask(i, cpu_possible_map) {
698 c = &cpu_data(i);
699 /* mark all to hotplug */
700 c->cpu_index = NR_CPUS;
701 }
702}
703
Mike Travis71fff5e2007-10-19 20:35:03 +0200704/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700705 * Prepare for SMP bootup. The MP table or ACPI has been read
706 * earlier. Just do some sanity checking here and enable APIC mode.
707 */
Glauber Costa7557da62008-03-03 14:12:38 -0300708void __init native_smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700709{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700710 nmi_watchdog_default();
Yinghai Lu949ec322008-01-30 13:30:46 +0100711 smp_cpu_index_default();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700712 current_cpu_data = boot_cpu_data;
713 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100714 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700715
Andi Kleena8ab26f2005-04-16 15:25:19 -0700716 if (smp_sanity_check(max_cpus) < 0) {
717 printk(KERN_INFO "SMP disabled\n");
718 disable_smp();
719 return;
720 }
721
722
723 /*
724 * Switch from PIC to APIC mode.
725 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 setup_local_APIC();
727
Andi Kleen739f33b2008-01-30 13:30:40 +0100728 /*
729 * Enable IO APIC before setting up error vector
730 */
731 if (!skip_ioapic_setup && nr_ioapics)
732 enable_IO_APIC();
733 end_local_APIC_setup();
734
Andi Kleena8ab26f2005-04-16 15:25:19 -0700735 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
736 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
737 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
738 /* Or can we switch back to PIC here? */
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700742 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
744 if (!skip_ioapic_setup && nr_ioapics)
745 setup_IO_APIC();
746 else
747 nr_ioapics = 0;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700750 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100753 setup_boot_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Andi Kleena8ab26f2005-04-16 15:25:19 -0700756/*
757 * Early setup to make printk work.
758 */
Glauber Costa1e3fac82008-03-03 14:12:37 -0300759void __init native_smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700761 int me = smp_processor_id();
Yinghai Lu23916d42008-01-30 13:33:39 +0100762 /* already set me in cpu_online_map in boot_cpu_init() */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700763 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700764 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Andi Kleena8ab26f2005-04-16 15:25:19 -0700767/*
768 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700769 */
Glauber Costa71d19542008-03-03 14:12:36 -0300770int __cpuinit native_cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700772 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100773 unsigned long flags;
774 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Andi Kleena8ab26f2005-04-16 15:25:19 -0700776 WARN_ON(irqs_disabled());
777
778 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
779
780 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
781 !physid_isset(apicid, phys_cpu_present_map)) {
782 printk("__cpu_up: bad cpu %d\n", cpu);
783 return -EINVAL;
784 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700785
Ashok Raj76e4f662005-06-25 14:55:00 -0700786 /*
787 * Already booted CPU?
788 */
789 if (cpu_isset(cpu, cpu_callin_map)) {
790 Dprintk("do_boot_cpu %d Already started\n", cpu);
791 return -ENOSYS;
792 }
793
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200794 /*
795 * Save current MTRR state in case it was changed since early boot
796 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
797 */
798 mtrr_save_state();
799
Ashok Raj884d9e42005-06-25 14:55:02 -0700800 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700801 /* Boot it! */
802 err = do_boot_cpu(cpu, apicid);
803 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700804 Dprintk("do_boot_cpu failed %d\n", err);
805 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* Unleash the CPU! */
809 Dprintk("waiting for cpu %d\n", cpu);
810
Ingo Molnar95492e42007-02-16 01:27:34 -0800811 /*
812 * Make sure and check TSC sync:
813 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100814 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800815 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100816 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700819 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700820 err = 0;
821
822 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Andi Kleena8ab26f2005-04-16 15:25:19 -0700825/*
826 * Finish the SMP boot.
827 */
Glauber Costac5597642008-03-03 14:12:39 -0300828void __init native_smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700830 smp_cleanup_boot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700832 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700833}