blob: 7ec96218a97e912e1c491c082906954b6f651707 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * x86 SMP booting functions
3 *
4 * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
5 * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
6 * Copyright 2001 Andi Kleen, SuSE Labs.
7 *
8 * Much of the core SMP work is based on previous work by Thomas Radke, to
9 * whom a great many thanks are extended.
10 *
11 * Thanks to Intel for making available several different Pentium,
12 * Pentium Pro and Pentium-II/Xeon MP machines.
13 * Original development of Linux SMP code supported by Caldera.
14 *
Andi Kleena8ab26f2005-04-16 15:25:19 -070015 * This code is released under the GNU General Public License version 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * Fixes
18 * Felix Koop : NR_CPUS used properly
19 * Jose Renau : Handle single CPU case.
20 * Alan Cox : By repeated request 8) - Total BogoMIP report.
21 * Greg Wright : Fix for kernel stacks panic.
22 * Erich Boleyn : MP v1.4 and additional changes.
23 * Matthias Sattler : Changes for 2.1 kernel map.
24 * Michel Lespinasse : Changes for 2.1 kernel map.
25 * Michael Chastain : Change trampoline.S to gnu as.
26 * Alan Cox : Dumb bug: 'B' step PPro's are fine
27 * Ingo Molnar : Added APIC timers, based on code
28 * from Jose Renau
29 * Ingo Molnar : various cleanups and rewrites
30 * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
31 * Maciej W. Rozycki : Bits for genuine 82489DX APICs
32 * Andi Kleen : Changed for SMP boot into long mode.
Andi Kleena8ab26f2005-04-16 15:25:19 -070033 * Rusty Russell : Hacked into shape for new "hotplug" boot process.
34 * Andi Kleen : Converted to new state machine.
35 * Various cleanups.
36 * Probably mostly hotplug CPU ready now.
Ashok Raj76e4f662005-06-25 14:55:00 -070037 * Ashok Raj : CPU hotplug support
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39
Andi Kleena8ab26f2005-04-16 15:25:19 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/init.h>
42
43#include <linux/mm.h>
44#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/bootmem.h>
46#include <linux/thread_info.h>
47#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/delay.h>
49#include <linux/mc146818rtc.h>
Andrew Mortona3bc0db2006-09-25 23:32:33 -070050#include <linux/smp.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070051#include <linux/kdebug.h>
Andrew Mortona3bc0db2006-09-25 23:32:33 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/mtrr.h>
54#include <asm/pgalloc.h>
55#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/tlbflush.h>
57#include <asm/proto.h>
Andi Kleen75152112005-05-16 21:53:34 -070058#include <asm/nmi.h>
Al Viro9cdd3042005-09-12 18:49:25 +020059#include <asm/irq.h>
60#include <asm/hw_irq.h>
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -080061#include <asm/numa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Glauber de Oliveira Costa8d770102008-03-19 14:25:31 -030063#include <mach_wakecpu.h>
Glauber de Oliveira Costaf6bc4022008-03-19 14:25:53 -030064#include <mach_apic.h>
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -030065#include <smpboot_hooks.h>
Glauber de Oliveira Costa07178262008-03-19 14:25:54 -030066#include <mach_apic.h>
Glauber de Oliveira Costa8d770102008-03-19 14:25:31 -030067
Andi Kleena8ab26f2005-04-16 15:25:19 -070068/* Set when the idlers are all forked */
69int smp_threads_ready;
70
Ashok Raj76e4f662005-06-25 14:55:00 -070071/* State of each CPU */
72DEFINE_PER_CPU(int, cpu_state) = { 0 };
73
74/*
75 * Store all idle threads, this can be reused instead of creating
76 * a new thread. Also avoids complicated thread destroy functionality
77 * for idle threads.
78 */
travis@sgi.com24b0d222008-01-30 13:33:11 +010079#ifdef CONFIG_HOTPLUG_CPU
80/*
81 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
82 * removed after init for !CONFIG_HOTPLUG_CPU.
83 */
84static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
85#define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
86#define set_idle_for_cpu(x,p) (per_cpu(idle_thread_array, x) = (p))
87#else
Ashok Raj76e4f662005-06-25 14:55:00 -070088struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
Ashok Raj76e4f662005-06-25 14:55:00 -070089#define get_idle_for_cpu(x) (idle_thread_array[(x)])
90#define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
travis@sgi.com24b0d222008-01-30 13:33:11 +010091#endif
92
Andi Kleena8ab26f2005-04-16 15:25:19 -070093static atomic_t init_deasserted __cpuinitdata;
94
Glauber de Oliveira Costaea0cadb2008-03-19 14:25:47 -030095#define smp_callin_clear_local_apic() do {} while (0)
96#define map_cpu_to_logical_apicid() do {} while (0)
97
Andi Kleena8ab26f2005-04-16 15:25:19 -070098/*
99 * Report back to the Boot Processor.
100 * Running on AP.
101 */
102void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 int cpuid, phys_id;
105 unsigned long timeout;
106
107 /*
108 * If waken up by an INIT in an 82489DX configuration
109 * we may get here before an INIT-deassert IPI reaches
110 * our local APIC. We have to wait for the IPI or we'll
111 * lock up on an APIC access.
112 */
Glauber Costae90009b2008-03-03 14:13:13 -0300113 wait_for_init_deassert(&init_deasserted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /*
116 * (This works even if the APIC is not enabled.)
117 */
118 phys_id = GET_APIC_ID(apic_read(APIC_ID));
119 cpuid = smp_processor_id();
120 if (cpu_isset(cpuid, cpu_callin_map)) {
121 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
122 phys_id, cpuid);
123 }
124 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
125
126 /*
127 * STARTUP IPIs are fragile beasts as they might sometimes
128 * trigger some glue motherboard logic. Complete APIC bus
129 * silence for 1 second, this overestimates the time the
130 * boot CPU is spending to send the up to 2 STARTUP IPIs
131 * by a factor of two. This should be enough.
132 */
133
134 /*
135 * Waiting 2s total for startup (udelay is not yet working)
136 */
137 timeout = jiffies + 2*HZ;
138 while (time_before(jiffies, timeout)) {
139 /*
140 * Has the boot CPU finished it's STARTUP sequence?
141 */
142 if (cpu_isset(cpuid, cpu_callout_map))
143 break;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700144 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
147 if (!time_before(jiffies, timeout)) {
148 panic("smp_callin: CPU%d started up but did not get a callout!\n",
149 cpuid);
150 }
151
152 /*
153 * the boot CPU has finished the init stage and is spinning
154 * on callin_map until we finish. We are free to set up this
155 * CPU, first the APIC. (this is probably redundant on most
156 * boards)
157 */
158
159 Dprintk("CALLIN, before setup_local_APIC().\n");
Glauber de Oliveira Costaea0cadb2008-03-19 14:25:47 -0300160 smp_callin_clear_local_apic();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 setup_local_APIC();
Andi Kleen739f33b2008-01-30 13:30:40 +0100162 end_local_APIC_setup();
Glauber de Oliveira Costaea0cadb2008-03-19 14:25:47 -0300163 map_cpu_to_logical_apicid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /*
166 * Get our bogomips.
Andi Kleenb4452212005-09-12 18:49:24 +0200167 *
168 * Need to enable IRQs because it can take longer and then
169 * the NMI watchdog might kill us.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 */
Andi Kleenb4452212005-09-12 18:49:24 +0200171 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 calibrate_delay();
Andi Kleenb4452212005-09-12 18:49:24 +0200173 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 Dprintk("Stack at about %p\n",&cpuid);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /*
177 * Save our processor parameters
178 */
179 smp_store_cpu_info(cpuid);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
182 * Allow the master to continue.
183 */
184 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700188 * Setup code on secondary processor (after comming out of the trampoline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700190void __cpuinit start_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 /*
193 * Dont put anything before smp_callin(), SMP
194 * booting is too fragile that we want to limit the
195 * things done here to the most necessary things.
196 */
197 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800198 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 smp_callin();
200
201 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
202 barrier();
203
Ingo Molnar95492e42007-02-16 01:27:34 -0800204 /*
205 * Check TSC sync first:
206 */
207 check_tsc_sync_target();
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (nmi_watchdog == NMI_IO_APIC) {
210 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100211 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 enable_8259A_irq(0);
213 }
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /*
Ashok Rajcb0cd8d2005-06-25 14:55:01 -0700216 * The sibling maps must be set before turing the online map on for
217 * this cpu
218 */
219 set_cpu_sibling_map(smp_processor_id());
220
221 /*
Ashok Raj884d9e42005-06-25 14:55:02 -0700222 * We need to hold call_lock, so there is no inconsistency
223 * between the time smp_call_function() determines number of
Simon Arlott676b1852007-10-20 01:25:36 +0200224 * IPI recipients, and the time when the determination is made
Ashok Raj884d9e42005-06-25 14:55:02 -0700225 * for which cpus receive the IPI in genapic_flat.c. Holding this
226 * lock helps us to not include this cpu in a currently in progress
227 * smp_call_function().
228 */
229 lock_ipi_call_lock();
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200230 spin_lock(&vector_lock);
Ashok Raj884d9e42005-06-25 14:55:02 -0700231
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200232 /* Setup the per cpu irq handling data structures */
233 __setup_vector_irq(smp_processor_id());
Ashok Raj884d9e42005-06-25 14:55:02 -0700234 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700235 * Allow the master to continue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
Glauber Costafc25da92008-03-03 14:13:04 -0300237 spin_unlock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 cpu_set(smp_processor_id(), cpu_online_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700239 unlock_ipi_call_lock();
240
Glauber de Oliveira Costa5733f622008-03-19 14:25:09 -0300241 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
242
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100243 setup_secondary_clock();
Thomas Gleixner3ac508b2007-10-14 22:57:45 +0200244
Glauber de Oliveira Costafa8004d2008-03-19 14:25:12 -0300245 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 cpu_idle();
247}
248
Andi Kleena8ab26f2005-04-16 15:25:19 -0700249extern volatile unsigned long init_rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250extern void (*initial_code)(void);
251
Olaf Hering44456d32005-07-27 11:45:17 -0700252#ifdef APIC_DEBUG
Glauber de Oliveira Costa8d770102008-03-19 14:25:31 -0300253static void __inquire_remote_apic(int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
256 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200257 int timeout;
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100258 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
261
Alejandro Martinez Ruiz4d022ad2007-10-17 14:38:58 +0200262 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100263 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /*
266 * Wait for idle.
267 */
Fernando Luis VazquezCao3144c332007-05-02 19:27:17 +0200268 status = safe_apic_wait_icr_idle();
269 if (status)
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100270 printk(KERN_CONT
271 "a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300273 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
274 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 timeout = 0;
277 do {
278 udelay(100);
279 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
280 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
281
282 switch (status) {
283 case APIC_ICR_RR_VALID:
284 status = apic_read(APIC_RRR);
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100285 printk(KERN_CONT "%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 break;
287 default:
Thomas Gleixner3c6bb072008-01-30 13:30:15 +0100288 printk(KERN_CONT "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 }
291}
292#endif
293
Andi Kleena8ab26f2005-04-16 15:25:19 -0700294/*
295 * Kick the secondary to wake up.
296 */
Glauber de Oliveira Costa07178262008-03-19 14:25:54 -0300297static int __cpuinit wakeup_secondary_cpu(int phys_apicid,
298 unsigned int start_rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200300 unsigned long send_status, accept_status = 0;
301 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300303 /*
304 * Be paranoid about clearing APIC errors.
305 */
306 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
307 apic_read_around(APIC_SPIV);
308 apic_write(APIC_ESR, 0);
309 apic_read(APIC_ESR);
310 }
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 Dprintk("Asserting INIT.\n");
313
314 /*
315 * Turn INIT on target chip
316 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300317 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /*
320 * Send IPI
321 */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300322 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 | APIC_DM_INIT);
324
325 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200326 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 mdelay(10);
329
330 Dprintk("Deasserting INIT.\n");
331
332 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300333 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* Send IPI */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300336 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200339 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Benjamin LaHaisef2ecfab2006-01-11 22:43:03 +0100341 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 atomic_set(&init_deasserted, 1);
343
Glauber de Oliveira Costa148a30f2008-03-19 14:25:11 -0300344 if (APIC_INTEGRATED(apic_version[phys_apicid]))
345 num_starts = 2;
346 else
347 num_starts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 /*
Glauber de Oliveira Costad0173ae2008-03-19 14:24:59 -0300350 * Paravirt / VMI wants a startup IPI hook here to set up the
351 * target processor state.
352 */
353 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
354 (unsigned long) init_rsp);
355
356
357 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * Run STARTUP IPI loop.
359 */
360 Dprintk("#startup loops: %d.\n", num_starts);
361
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100362 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 for (j = 1; j <= num_starts; j++) {
365 Dprintk("Sending STARTUP #%d.\n",j);
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300366 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 apic_write(APIC_ESR, 0);
368 apic_read(APIC_ESR);
369 Dprintk("After apic_write.\n");
370
371 /*
372 * STARTUP IPI
373 */
374
375 /* Target chip */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300376 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /* Boot on the stack */
379 /* Kick the second */
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300380 apic_write_around(APIC_ICR, APIC_DM_STARTUP | (start_rip>>12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /*
383 * Give the other CPU some time to accept the IPI.
384 */
385 udelay(300);
386
387 Dprintk("Startup point 1.\n");
388
389 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoea8c7332007-05-02 19:27:17 +0200390 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /*
393 * Give the other CPU some time to accept the IPI.
394 */
395 udelay(200);
396 /*
397 * Due to the Pentium erratum 3AP.
398 */
399 if (maxlvt > 3) {
Glauber de Oliveira Costa1af8a0c2008-03-19 14:24:58 -0300400 apic_read_around(APIC_SPIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 apic_write(APIC_ESR, 0);
402 }
403 accept_status = (apic_read(APIC_ESR) & 0xEF);
404 if (send_status || accept_status)
405 break;
406 }
407 Dprintk("After Startup.\n");
408
409 if (send_status)
410 printk(KERN_ERR "APIC never delivered???\n");
411 if (accept_status)
412 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
413
414 return (send_status | accept_status);
415}
416
Ashok Raj76e4f662005-06-25 14:55:00 -0700417struct create_idle {
David Howells65f27f32006-11-22 14:55:48 +0000418 struct work_struct work;
Ashok Raj76e4f662005-06-25 14:55:00 -0700419 struct task_struct *idle;
420 struct completion done;
421 int cpu;
422};
423
Thomas Gleixnera2b484a2008-01-09 00:18:28 +0100424static void __cpuinit do_fork_idle(struct work_struct *work)
Ashok Raj76e4f662005-06-25 14:55:00 -0700425{
David Howells65f27f32006-11-22 14:55:48 +0000426 struct create_idle *c_idle =
427 container_of(work, struct create_idle, work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700428
429 c_idle->idle = fork_idle(c_idle->cpu);
430 complete(&c_idle->done);
431}
432
Andi Kleena8ab26f2005-04-16 15:25:19 -0700433/*
434 * Boot one CPU.
435 */
436static int __cpuinit do_boot_cpu(int cpu, int apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Glauber de Oliveira Costa6becedb2008-03-19 14:25:51 -0300438 unsigned long boot_error = 0;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700439 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 unsigned long start_rip;
Ashok Raj76e4f662005-06-25 14:55:00 -0700441 struct create_idle c_idle = {
442 .cpu = cpu,
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700443 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Ashok Raj76e4f662005-06-25 14:55:00 -0700444 };
Glauber Costa2b775a22008-02-22 12:09:29 -0300445 INIT_WORK(&c_idle.work, do_fork_idle);
Ashok Raj76e4f662005-06-25 14:55:00 -0700446
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100447 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
448 if (!cpu_gdt_descr[cpu].address &&
449 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
450 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
451 return -1;
452 }
453
Ravikiran G Thirumalai365ba912006-01-11 22:45:42 +0100454 /* Allocate node local memory for AP pdas */
455 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
456 struct x8664_pda *newpda, *pda;
457 int node = cpu_to_node(cpu);
458 pda = cpu_pda(cpu);
459 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
460 node);
461 if (newpda) {
462 memcpy(newpda, pda, sizeof (struct x8664_pda));
463 cpu_pda(cpu) = newpda;
464 } else
465 printk(KERN_ERR
466 "Could not allocate node local PDA for CPU %d on node %d\n",
467 cpu, node);
468 }
469
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200470 alternatives_smp_switch(1);
471
Ashok Raj76e4f662005-06-25 14:55:00 -0700472 c_idle.idle = get_idle_for_cpu(cpu);
473
474 if (c_idle.idle) {
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100475 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800476 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
Ashok Raj76e4f662005-06-25 14:55:00 -0700477 init_idle(c_idle.idle, cpu);
478 goto do_rest;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Ashok Raj76e4f662005-06-25 14:55:00 -0700481 /*
482 * During cold boot process, keventd thread is not spun up yet.
483 * When we do cpu hot-add, we create idle threads on the fly, we should
484 * not acquire any attributes from the calling context. Hence the clean
485 * way to create kernel_threads() is to do that from keventd().
486 * We do the current_is_keventd() due to the fact that ACPI notifier
487 * was also queuing to keventd() and when the caller is already running
488 * in context of keventd(), we would end up with locking up the keventd
489 * thread.
490 */
491 if (!keventd_up() || current_is_keventd())
David Howells65f27f32006-11-22 14:55:48 +0000492 c_idle.work.func(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700493 else {
David Howells65f27f32006-11-22 14:55:48 +0000494 schedule_work(&c_idle.work);
Ashok Raj76e4f662005-06-25 14:55:00 -0700495 wait_for_completion(&c_idle.done);
496 }
497
498 if (IS_ERR(c_idle.idle)) {
499 printk("failed fork for CPU %d\n", cpu);
500 return PTR_ERR(c_idle.idle);
501 }
502
503 set_idle_for_cpu(cpu, c_idle.idle);
504
505do_rest:
506
Ravikiran G Thirumalaidf79efd2006-01-11 22:45:39 +0100507 cpu_pda(cpu)->pcurrent = c_idle.idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 start_rip = setup_trampoline();
510
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100511 init_rsp = c_idle.idle->thread.sp;
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100512 load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 initial_code = start_secondary;
Al Viroe4f17c42006-01-12 01:05:38 -0800514 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Andi Kleende04f322005-07-28 21:15:29 -0700516 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
517 cpus_weight(cpu_present_map),
518 apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /*
521 * This grunge runs the startup process for
522 * the targeted processor.
523 */
524
525 atomic_set(&init_deasserted, 0);
526
527 Dprintk("Setting warm reset code and vector.\n");
528
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -0300529 smpboot_setup_warm_reset_vector(start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * Be paranoid about clearing APIC errors.
532 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100533 apic_write(APIC_ESR, 0);
534 apic_read(APIC_ESR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * Starting actual IPI sequence...
538 */
Glauber de Oliveira Costa07178262008-03-19 14:25:54 -0300539 boot_error = wakeup_secondary_cpu(apicid, start_rip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (!boot_error) {
542 /*
543 * allow APs to start initializing.
544 */
545 Dprintk("Before Callout %d.\n", cpu);
546 cpu_set(cpu, cpu_callout_map);
547 Dprintk("After Callout %d.\n", cpu);
548
549 /*
550 * Wait 5s total for a response
551 */
552 for (timeout = 0; timeout < 50000; timeout++) {
553 if (cpu_isset(cpu, cpu_callin_map))
554 break; /* It has booted */
555 udelay(100);
556 }
557
558 if (cpu_isset(cpu, cpu_callin_map)) {
559 /* number CPUs logically, starting from 1 (BSP is 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 Dprintk("CPU has booted.\n");
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300561 printk(KERN_INFO "CPU%d: ", cpu);
562 print_cpu_info(&cpu_data(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 } else {
564 boot_error = 1;
Glauber de Oliveira Costa6becedb2008-03-19 14:25:51 -0300565 if (*((volatile unsigned char *)trampoline_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 == 0xA5)
567 /* trampoline started but...? */
568 printk("Stuck ??\n");
569 else
570 /* trampoline code not run */
571 printk("Not responding.\n");
Olaf Hering44456d32005-07-27 11:45:17 -0700572#ifdef APIC_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 inquire_remote_apic(apicid);
574#endif
575 }
576 }
577 if (boot_error) {
578 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
Jeremy Fitzhardinge5548fec2008-01-30 13:30:55 +0100579 clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */
Ravikiran G Thirumalai488fc082006-02-07 12:58:23 -0800580 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700581 cpu_clear(cpu, cpu_present_map);
582 cpu_clear(cpu, cpu_possible_map);
Mike Travis71fff5e2007-10-19 20:35:03 +0200583 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700584 }
585
Glauber de Oliveira Costa6becedb2008-03-19 14:25:51 -0300586 /* mark "stuck" area as not stuck */
587 *((volatile unsigned long *)trampoline_base) = 0;
588
589 return boot_error;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700590}
591
592cycles_t cacheflush_time;
593unsigned long cache_decay_ticks;
594
595/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700596 * Fall back to non SMP mode after errors.
597 *
598 * RED-PEN audit/test this more. I bet there is more state messed up here.
599 */
Ashok Raje6982c62005-06-25 14:54:58 -0700600static __init void disable_smp(void)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700601{
602 cpu_present_map = cpumask_of_cpu(0);
603 cpu_possible_map = cpumask_of_cpu(0);
604 if (smp_found_config)
605 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
606 else
607 phys_cpu_present_map = physid_mask_of_physid(0);
Mike Travisd5a74302007-10-16 01:24:05 -0700608 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700609 cpu_set(0, per_cpu(cpu_core_map, 0));
Andi Kleena8ab26f2005-04-16 15:25:19 -0700610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700613 * Various sanity checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Ashok Raje6982c62005-06-25 14:54:58 -0700615static int __init smp_sanity_check(unsigned max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
618 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
619 hard_smp_processor_id());
620 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
621 }
622
623 /*
624 * If we couldn't find an SMP configuration at boot time,
625 * get out of here now!
626 */
627 if (!smp_found_config) {
628 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700629 disable_smp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (APIC_init_uniprocessor())
631 printk(KERN_NOTICE "Local APIC not detected."
632 " Using dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700633 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 /*
637 * Should not be necessary because the MP table should list the boot
638 * CPU too, but we do it for the sake of robustness anyway.
639 */
640 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
641 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
642 boot_cpu_id);
643 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
644 }
645
646 /*
647 * If we couldn't find a local APIC, then get out of here now!
648 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100649 if (!cpu_has_apic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
651 boot_cpu_id);
652 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700653 nr_ioapics = 0;
654 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * If SMP should be disabled, then really disable it!
659 */
660 if (!max_cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Andi Kleena8ab26f2005-04-16 15:25:19 -0700662 nr_ioapics = 0;
663 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
Andi Kleena8ab26f2005-04-16 15:25:19 -0700666 return 0;
667}
668
Yinghai Lu949ec322008-01-30 13:30:46 +0100669static void __init smp_cpu_index_default(void)
670{
671 int i;
672 struct cpuinfo_x86 *c;
673
674 for_each_cpu_mask(i, cpu_possible_map) {
675 c = &cpu_data(i);
676 /* mark all to hotplug */
677 c->cpu_index = NR_CPUS;
678 }
679}
680
Mike Travis71fff5e2007-10-19 20:35:03 +0200681/*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700682 * Prepare for SMP bootup. The MP table or ACPI has been read
683 * earlier. Just do some sanity checking here and enable APIC mode.
684 */
Glauber Costa7557da62008-03-03 14:12:38 -0300685void __init native_smp_prepare_cpus(unsigned int max_cpus)
Andi Kleena8ab26f2005-04-16 15:25:19 -0700686{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700687 nmi_watchdog_default();
Yinghai Lu949ec322008-01-30 13:30:46 +0100688 smp_cpu_index_default();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700689 current_cpu_data = boot_cpu_data;
690 current_thread_info()->cpu = 0; /* needed? */
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100691 set_cpu_sibling_map(0);
Andi Kleena8ab26f2005-04-16 15:25:19 -0700692
Andi Kleena8ab26f2005-04-16 15:25:19 -0700693 if (smp_sanity_check(max_cpus) < 0) {
694 printk(KERN_INFO "SMP disabled\n");
695 disable_smp();
696 return;
697 }
698
699
700 /*
701 * Switch from PIC to APIC mode.
702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 setup_local_APIC();
704
Andi Kleen739f33b2008-01-30 13:30:40 +0100705 /*
706 * Enable IO APIC before setting up error vector
707 */
708 if (!skip_ioapic_setup && nr_ioapics)
709 enable_IO_APIC();
710 end_local_APIC_setup();
711
Andi Kleena8ab26f2005-04-16 15:25:19 -0700712 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
713 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
714 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
715 /* Or can we switch back to PIC here? */
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700719 * Now start the IO-APICs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
721 if (!skip_ioapic_setup && nr_ioapics)
722 setup_IO_APIC();
723 else
724 nr_ioapics = 0;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
Andi Kleena8ab26f2005-04-16 15:25:19 -0700727 * Set up local APIC timer on boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100730 setup_boot_clock();
Glauber de Oliveira Costa4f3ab192008-03-19 14:25:01 -0300731 printk(KERN_INFO "CPU%d: ", 0);
732 print_cpu_info(&cpu_data(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Andi Kleena8ab26f2005-04-16 15:25:19 -0700735/*
736 * Early setup to make printk work.
737 */
Glauber Costa1e3fac82008-03-03 14:12:37 -0300738void __init native_smp_prepare_boot_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700740 int me = smp_processor_id();
Yinghai Lu23916d42008-01-30 13:33:39 +0100741 /* already set me in cpu_online_map in boot_cpu_init() */
Andi Kleena8ab26f2005-04-16 15:25:19 -0700742 cpu_set(me, cpu_callout_map);
Ashok Raj884d9e42005-06-25 14:55:02 -0700743 per_cpu(cpu_state, me) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Andi Kleena8ab26f2005-04-16 15:25:19 -0700746/*
747 * Entry point to boot a CPU.
Andi Kleena8ab26f2005-04-16 15:25:19 -0700748 */
Glauber Costa71d19542008-03-03 14:12:36 -0300749int __cpuinit native_cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Andi Kleena8ab26f2005-04-16 15:25:19 -0700751 int apicid = cpu_present_to_apicid(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100752 unsigned long flags;
753 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Andi Kleena8ab26f2005-04-16 15:25:19 -0700755 WARN_ON(irqs_disabled());
756
757 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
758
759 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
760 !physid_isset(apicid, phys_cpu_present_map)) {
761 printk("__cpu_up: bad cpu %d\n", cpu);
762 return -EINVAL;
763 }
Andi Kleena8ab26f2005-04-16 15:25:19 -0700764
Ashok Raj76e4f662005-06-25 14:55:00 -0700765 /*
766 * Already booted CPU?
767 */
768 if (cpu_isset(cpu, cpu_callin_map)) {
769 Dprintk("do_boot_cpu %d Already started\n", cpu);
770 return -ENOSYS;
771 }
772
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200773 /*
774 * Save current MTRR state in case it was changed since early boot
775 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
776 */
777 mtrr_save_state();
778
Ashok Raj884d9e42005-06-25 14:55:02 -0700779 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Andi Kleena8ab26f2005-04-16 15:25:19 -0700780 /* Boot it! */
781 err = do_boot_cpu(cpu, apicid);
782 if (err < 0) {
Andi Kleena8ab26f2005-04-16 15:25:19 -0700783 Dprintk("do_boot_cpu failed %d\n", err);
784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Unleash the CPU! */
788 Dprintk("waiting for cpu %d\n", cpu);
789
Ingo Molnar95492e42007-02-16 01:27:34 -0800790 /*
791 * Make sure and check TSC sync:
792 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100793 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800794 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100795 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 while (!cpu_isset(cpu, cpu_online_map))
Andi Kleena8ab26f2005-04-16 15:25:19 -0700798 cpu_relax();
Ashok Raj76e4f662005-06-25 14:55:00 -0700799 err = 0;
800
801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300804extern void impress_friends(void);
805extern void smp_checks(void);
806
Andi Kleena8ab26f2005-04-16 15:25:19 -0700807/*
808 * Finish the SMP boot.
809 */
Glauber Costac5597642008-03-03 14:12:39 -0300810void __init native_smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Glauber de Oliveira Costaeb44d0a2008-03-19 14:25:32 -0300812 smpboot_restore_warm_reset_vector();
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300813
814 Dprintk("Boot done.\n");
815
816 impress_friends();
817 smp_checks();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 setup_ioapic_dest();
Andi Kleen75152112005-05-16 21:53:34 -0700819 check_nmi_watchdog();
Andi Kleena8ab26f2005-04-16 15:25:19 -0700820}