blob: 8676eec2d00f8b19a99cd8599dae5b3c0554826c [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 *
7 * Much of the core SMP work is based on previous work by Thomas Radke, to
8 * whom a great many thanks are extended.
9 *
10 * Thanks to Intel for making available several different Pentium,
11 * Pentium Pro and Pentium-II/Xeon MP machines.
12 * Original development of Linux SMP code supported by Caldera.
13 *
14 * This code is released under the GNU General Public License version 2 or
15 * later.
16 *
17 * Fixes
18 * Felix Koop : NR_CPUS used properly
19 * Jose Renau : Handle single CPU case.
20 * Alan Cox : By repeated request 8) - Total BogoMIPS 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 * Martin J. Bligh : Added support for multi-quad systems
33 * Dave Jones : Report invalid combinations of Athlon CPUs.
34* Rusty Russell : Hacked into shape for new "hotplug" boot process. */
35
36#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/kernel.h>
39
40#include <linux/mm.h>
41#include <linux/sched.h>
42#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/bootmem.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070044#include <linux/notifier.h>
45#include <linux/cpu.h>
46#include <linux/percpu.h>
Ingo Molnard04f41e2007-03-07 18:12:31 +010047#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <linux/delay.h>
50#include <linux/mc146818rtc.h>
51#include <asm/tlbflush.h>
52#include <asm/desc.h>
53#include <asm/arch_hooks.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020054#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <mach_apic.h>
57#include <mach_wakecpu.h>
58#include <smpboot_hooks.h>
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +010059#include <asm/vmi.h>
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +020060#include <asm/mtrr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* Set if we find a B stepping CPU */
Adrian Bunkf2206ec2007-12-19 23:20:18 +010063static int __cpuinitdata smp_b_stepping;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static cpumask_t smp_commenced_mask;
66
travis@sgi.com3b419082008-01-30 13:33:11 +010067/* which logical CPU number maps to which CPU (physical APIC ID) */
Glauber Costa5382e892008-03-03 14:12:57 -030068u16 x86_cpu_to_apicid_init[NR_CPUS] __initdata =
Mike Travis71fff5e2007-10-19 20:35:03 +020069 { [0 ... NR_CPUS-1] = BAD_APICID };
travis@sgi.com3b419082008-01-30 13:33:11 +010070void *x86_cpu_to_apicid_early_ptr;
Glauber Costa5382e892008-03-03 14:12:57 -030071DEFINE_PER_CPU(u16, x86_cpu_to_apicid) = BAD_APICID;
Mike Travis71fff5e2007-10-19 20:35:03 +020072EXPORT_PER_CPU_SYMBOL(x86_cpu_to_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
keith mannthey3b086062006-09-29 01:58:46 -070074u8 apicid_2_node[MAX_APICID];
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void map_cpu_to_logical_apicid(void);
77
Zwane Mwaikambof3705132005-06-25 14:54:50 -070078/* State of each CPU. */
79DEFINE_PER_CPU(int, cpu_state) = { 0 };
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * The bootstrap kernel entry code has set these up. Save them for
83 * a given CPU
84 */
85
Jeremy Fitzhardinge724faa82007-07-17 18:37:03 -070086void __cpuinit smp_store_cpu_info(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Mike Travis92cb7612007-10-19 20:35:04 +020088 struct cpuinfo_x86 *c = &cpu_data(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 *c = boot_cpu_data;
Mike Travis92cb7612007-10-19 20:35:04 +020091 c->cpu_index = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (id!=0)
Jeremy Fitzhardingea6c4e072007-05-02 19:27:12 +020093 identify_secondary_cpu(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /*
95 * Mask B, Pentium, but not Pentium MMX
96 */
97 if (c->x86_vendor == X86_VENDOR_INTEL &&
98 c->x86 == 5 &&
99 c->x86_mask >= 1 && c->x86_mask <= 4 &&
100 c->x86_model <= 3)
101 /*
102 * Remember we have B step Pentia with bugs
103 */
104 smp_b_stepping = 1;
105
106 /*
107 * Certain Athlons might work (for various values of 'work') in SMP
108 * but they are not certified as MP capable.
109 */
110 if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
111
Dave Jones3ca113e2006-09-26 10:52:34 +0200112 if (num_possible_cpus() == 1)
113 goto valid_k7;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /* Athlon 660/661 is valid. */
116 if ((c->x86_model==6) && ((c->x86_mask==0) || (c->x86_mask==1)))
117 goto valid_k7;
118
119 /* Duron 670 is valid */
120 if ((c->x86_model==7) && (c->x86_mask==0))
121 goto valid_k7;
122
123 /*
124 * Athlon 662, Duron 671, and Athlon >model 7 have capability bit.
125 * It's worth noting that the A5 stepping (662) of some Athlon XP's
126 * have the MP bit set.
127 * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for more.
128 */
129 if (((c->x86_model==6) && (c->x86_mask>=2)) ||
130 ((c->x86_model==7) && (c->x86_mask>=1)) ||
131 (c->x86_model> 7))
132 if (cpu_has_mp)
133 goto valid_k7;
134
135 /* If we get here, it's not a certified SMP capable AMD system. */
Randy Dunlap9f158332005-09-13 01:25:16 -0700136 add_taint(TAINT_UNSAFE_SMP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
139valid_k7:
140 ;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static atomic_t init_deasserted;
144
Vivek Goyal4a5d1072007-01-11 01:52:44 +0100145static void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 int cpuid, phys_id;
148 unsigned long timeout;
149
150 /*
151 * If waken up by an INIT in an 82489DX configuration
152 * we may get here before an INIT-deassert IPI reaches
153 * our local APIC. We have to wait for the IPI or we'll
154 * lock up on an APIC access.
155 */
156 wait_for_init_deassert(&init_deasserted);
157
158 /*
159 * (This works even if the APIC is not enabled.)
160 */
161 phys_id = GET_APIC_ID(apic_read(APIC_ID));
162 cpuid = smp_processor_id();
163 if (cpu_isset(cpuid, cpu_callin_map)) {
164 printk("huh, phys CPU#%d, CPU#%d already present??\n",
165 phys_id, cpuid);
166 BUG();
167 }
168 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
169
170 /*
171 * STARTUP IPIs are fragile beasts as they might sometimes
172 * trigger some glue motherboard logic. Complete APIC bus
173 * silence for 1 second, this overestimates the time the
174 * boot CPU is spending to send the up to 2 STARTUP IPIs
175 * by a factor of two. This should be enough.
176 */
177
178 /*
179 * Waiting 2s total for startup (udelay is not yet working)
180 */
181 timeout = jiffies + 2*HZ;
182 while (time_before(jiffies, timeout)) {
183 /*
184 * Has the boot CPU finished it's STARTUP sequence?
185 */
186 if (cpu_isset(cpuid, cpu_callout_map))
187 break;
Glauber Costae1043832008-03-03 14:13:14 -0300188 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
191 if (!time_before(jiffies, timeout)) {
192 printk("BUG: CPU%d started up but did not get a callout!\n",
193 cpuid);
194 BUG();
195 }
196
197 /*
198 * the boot CPU has finished the init stage and is spinning
199 * on callin_map until we finish. We are free to set up this
200 * CPU, first the APIC. (this is probably redundant on most
201 * boards)
202 */
203
204 Dprintk("CALLIN, before setup_local_APIC().\n");
205 smp_callin_clear_local_apic();
206 setup_local_APIC();
207 map_cpu_to_logical_apicid();
208
209 /*
210 * Get our bogomips.
211 */
212 calibrate_delay();
213 Dprintk("Stack at about %p\n",&cpuid);
214
215 /*
216 * Save our processor parameters
217 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800218 smp_store_cpu_info(cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /*
221 * Allow the master to continue.
222 */
223 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226static int cpucount;
227
228/*
229 * Activate a secondary processor.
230 */
Vivek Goyal4a5d1072007-01-11 01:52:44 +0100231static void __cpuinit start_secondary(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 /*
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200234 * Don't put *anything* before cpu_init(), SMP booting is too
235 * fragile that we want to limit the things done here to the
236 * most necessary things.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100238#ifdef CONFIG_VMI
239 vmi_bringup();
240#endif
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200241 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800242 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 smp_callin();
244 while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
Glauber Costae1043832008-03-03 14:13:14 -0300245 cpu_relax();
Ingo Molnar95492e42007-02-16 01:27:34 -0800246 /*
247 * Check TSC synchronization with the BP:
248 */
249 check_tsc_sync_target();
250
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100251 setup_secondary_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (nmi_watchdog == NMI_IO_APIC) {
253 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100254 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 enable_8259A_irq(0);
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /*
258 * low-memory mappings have been cleared, flush them from
259 * the local TLBs too.
260 */
261 local_flush_tlb();
Li Shaohua6fe940d2005-06-25 14:54:53 -0700262
Li Shaohuad7208032005-06-25 14:54:54 -0700263 /* This must be done before setting cpu_online_map */
264 set_cpu_sibling_map(raw_smp_processor_id());
265 wmb();
266
Li Shaohua6fe940d2005-06-25 14:54:53 -0700267 /*
268 * We need to hold call_lock, so there is no inconsistency
269 * between the time smp_call_function() determines number of
Simon Arlott27b46d72007-10-20 01:13:56 +0200270 * IPI recipients, and the time when the determination is made
Li Shaohua6fe940d2005-06-25 14:54:53 -0700271 * for which cpus receive the IPI. Holding this
272 * lock helps us to not include this cpu in a currently in progress
273 * smp_call_function().
274 */
275 lock_ipi_call_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 cpu_set(smp_processor_id(), cpu_online_map);
Li Shaohua6fe940d2005-06-25 14:54:53 -0700277 unlock_ipi_call_lock();
Li Shaohuae1367da2005-06-25 14:54:56 -0700278 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* We can take interrupts now: we're officially "up". */
281 local_irq_enable();
282
283 wmb();
284 cpu_idle();
285}
286
287/*
288 * Everything has been set up for the secondary
289 * CPUs - they just need to reload everything
290 * from the task structure
291 * This function must not return.
292 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700293void __devinit initialize_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 /*
296 * We don't actually need to load the full TSS,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100297 * basically just the stack pointer and the ip.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
299
300 asm volatile(
301 "movl %0,%%esp\n\t"
302 "jmp *%1"
303 :
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100304 :"m" (current->thread.sp),"m" (current->thread.ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100307/* Static state in head.S used to set up a CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308extern struct {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100309 void * sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unsigned short ss;
311} stack_start;
312
313#ifdef CONFIG_NUMA
314
315/* which logical CPUs are on which nodes */
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100316cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100318EXPORT_SYMBOL(node_to_cpumask_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/* which node each logical CPU is on */
travis@sgi.com602a54a2008-01-30 13:33:21 +0100320int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100321EXPORT_SYMBOL(cpu_to_node_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323/* set up a mapping between cpu and node. */
324static inline void map_cpu_to_node(int cpu, int node)
325{
326 printk("Mapping cpu %d to node %d\n", cpu, node);
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100327 cpu_set(cpu, node_to_cpumask_map[node]);
328 cpu_to_node_map[cpu] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/* undo a mapping between cpu and node. */
332static inline void unmap_cpu_to_node(int cpu)
333{
334 int node;
335
336 printk("Unmapping cpu %d from all nodes\n", cpu);
337 for (node = 0; node < MAX_NUMNODES; node ++)
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100338 cpu_clear(cpu, node_to_cpumask_map[node]);
339 cpu_to_node_map[cpu] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341#else /* !CONFIG_NUMA */
342
343#define map_cpu_to_node(cpu, node) ({})
344#define unmap_cpu_to_node(cpu) ({})
345
346#endif /* CONFIG_NUMA */
347
Christoph Lameter6c036522005-07-07 17:56:59 -0700348u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350static void map_cpu_to_logical_apicid(void)
351{
352 int cpu = smp_processor_id();
353 int apicid = logical_smp_processor_id();
Keith Mannthey78b656b2006-10-03 18:25:52 -0700354 int node = apicid_to_node(apicid);
keith manntheybfa0e9a2006-09-25 16:25:35 -0700355
356 if (!node_online(node))
357 node = first_online_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 cpu_2_logical_apicid[cpu] = apicid;
keith manntheybfa0e9a2006-09-25 16:25:35 -0700360 map_cpu_to_node(cpu, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363static void unmap_cpu_to_logical_apicid(int cpu)
364{
365 cpu_2_logical_apicid[cpu] = BAD_APICID;
366 unmap_cpu_to_node(cpu);
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static inline void __inquire_remote_apic(int apicid)
370{
Glauber de Oliveira Costa26c6b5e2008-03-19 14:24:56 -0300371 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao4312fa82007-05-02 19:27:17 +0200373 int timeout;
Glauber de Oliveira Costa26c6b5e2008-03-19 14:24:56 -0300374 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300376 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Tobias Klauser38e548e2005-11-07 00:58:31 -0800378 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300379 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /*
382 * Wait for idle.
383 */
Fernando Luis VazquezCao4312fa82007-05-02 19:27:17 +0200384 status = safe_apic_wait_icr_idle();
385 if (status)
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300386 printk(KERN_CONT
387 "a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
390 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
391
392 timeout = 0;
393 do {
394 udelay(100);
395 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
396 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
397
398 switch (status) {
399 case APIC_ICR_RR_VALID:
400 status = apic_read(APIC_RRR);
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300401 printk(KERN_CONT "%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 break;
403 default:
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300404 printk(KERN_CONT "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 }
407}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409#ifdef WAKE_SECONDARY_VIA_NMI
410/*
411 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
412 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
413 * won't ... remember to clear down the APIC, etc later.
414 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700415static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
417{
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200418 unsigned long send_status, accept_status = 0;
419 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /* Target chip */
422 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
423
424 /* Boot on the stack */
425 /* Kick the second */
426 apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
427
428 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200429 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /*
432 * Give the other CPU some time to accept the IPI.
433 */
434 udelay(200);
435 /*
436 * Due to the Pentium erratum 3AP.
437 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800438 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (maxlvt > 3) {
440 apic_read_around(APIC_SPIV);
441 apic_write(APIC_ESR, 0);
442 }
443 accept_status = (apic_read(APIC_ESR) & 0xEF);
444 Dprintk("NMI sent.\n");
445
446 if (send_status)
447 printk("APIC never delivered???\n");
448 if (accept_status)
449 printk("APIC delivery error (%lx).\n", accept_status);
450
451 return (send_status | accept_status);
452}
453#endif /* WAKE_SECONDARY_VIA_NMI */
454
455#ifdef WAKE_SECONDARY_VIA_INIT
Li Shaohua0bb31842005-06-25 14:54:55 -0700456static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
458{
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200459 unsigned long send_status, accept_status = 0;
460 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /*
463 * Be paranoid about clearing APIC errors.
464 */
465 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
466 apic_read_around(APIC_SPIV);
467 apic_write(APIC_ESR, 0);
468 apic_read(APIC_ESR);
469 }
470
471 Dprintk("Asserting INIT.\n");
472
473 /*
474 * Turn INIT on target chip
475 */
476 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
477
478 /*
479 * Send IPI
480 */
481 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
482 | APIC_DM_INIT);
483
484 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200485 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 mdelay(10);
488
489 Dprintk("Deasserting INIT.\n");
490
491 /* Target chip */
492 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
493
494 /* Send IPI */
495 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
496
497 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200498 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 atomic_set(&init_deasserted, 1);
501
502 /*
503 * Should we send STARTUP IPIs ?
504 *
505 * Determine this based on the APIC version.
506 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
507 */
508 if (APIC_INTEGRATED(apic_version[phys_apicid]))
509 num_starts = 2;
510 else
511 num_starts = 0;
512
513 /*
Zachary Amsdenae5da272007-02-13 13:26:21 +0100514 * Paravirt / VMI wants a startup IPI hook here to set up the
515 * target processor state.
516 */
517 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100518 (unsigned long) stack_start.sp);
Zachary Amsdenae5da272007-02-13 13:26:21 +0100519
520 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * Run STARTUP IPI loop.
522 */
523 Dprintk("#startup loops: %d.\n", num_starts);
524
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800525 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 for (j = 1; j <= num_starts; j++) {
528 Dprintk("Sending STARTUP #%d.\n",j);
529 apic_read_around(APIC_SPIV);
530 apic_write(APIC_ESR, 0);
531 apic_read(APIC_ESR);
532 Dprintk("After apic_write.\n");
533
534 /*
535 * STARTUP IPI
536 */
537
538 /* Target chip */
539 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
540
541 /* Boot on the stack */
542 /* Kick the second */
543 apic_write_around(APIC_ICR, APIC_DM_STARTUP
544 | (start_eip >> 12));
545
546 /*
547 * Give the other CPU some time to accept the IPI.
548 */
549 udelay(300);
550
551 Dprintk("Startup point 1.\n");
552
553 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200554 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /*
557 * Give the other CPU some time to accept the IPI.
558 */
559 udelay(200);
560 /*
561 * Due to the Pentium erratum 3AP.
562 */
563 if (maxlvt > 3) {
564 apic_read_around(APIC_SPIV);
565 apic_write(APIC_ESR, 0);
566 }
567 accept_status = (apic_read(APIC_ESR) & 0xEF);
568 if (send_status || accept_status)
569 break;
570 }
571 Dprintk("After Startup.\n");
572
573 if (send_status)
574 printk("APIC never delivered???\n");
575 if (accept_status)
576 printk("APIC delivery error (%lx).\n", accept_status);
577
578 return (send_status | accept_status);
579}
580#endif /* WAKE_SECONDARY_VIA_INIT */
581
582extern cpumask_t cpu_initialized;
Li Shaohuae1367da2005-06-25 14:54:56 -0700583static inline int alloc_cpu_id(void)
584{
585 cpumask_t tmp_map;
586 int cpu;
587 cpus_complement(tmp_map, cpu_present_map);
588 cpu = first_cpu(tmp_map);
589 if (cpu >= NR_CPUS)
590 return -ENODEV;
591 return cpu;
592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Li Shaohuae1367da2005-06-25 14:54:56 -0700594#ifdef CONFIG_HOTPLUG_CPU
Adrian Bunkf2206ec2007-12-19 23:20:18 +0100595static struct task_struct * __cpuinitdata cpu_idle_tasks[NR_CPUS];
596static inline struct task_struct * __cpuinit alloc_idle_task(int cpu)
Li Shaohuae1367da2005-06-25 14:54:56 -0700597{
598 struct task_struct *idle;
599
600 if ((idle = cpu_idle_tasks[cpu]) != NULL) {
601 /* initialize thread_struct. we really want to avoid destroy
602 * idle tread
603 */
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100604 idle->thread.sp = (unsigned long)task_pt_regs(idle);
Li Shaohuae1367da2005-06-25 14:54:56 -0700605 init_idle(idle, cpu);
606 return idle;
607 }
608 idle = fork_idle(cpu);
609
610 if (!IS_ERR(idle))
611 cpu_idle_tasks[cpu] = idle;
612 return idle;
613}
614#else
615#define alloc_idle_task(cpu) fork_idle(cpu)
616#endif
617
Vivek Goyal4a5d1072007-01-11 01:52:44 +0100618static int __cpuinit do_boot_cpu(int apicid, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
621 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
622 * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
623 */
624{
625 struct task_struct *idle;
626 unsigned long boot_error;
Li Shaohuae1367da2005-06-25 14:54:56 -0700627 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 unsigned long start_eip;
629 unsigned short nmi_high = 0, nmi_low = 0;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200632 * Save current MTRR state in case it was changed since early boot
633 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
634 */
635 mtrr_save_state();
636
637 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * We can't use kernel_thread since we must avoid to
639 * reschedule the child.
640 */
Li Shaohuae1367da2005-06-25 14:54:56 -0700641 idle = alloc_idle_task(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (IS_ERR(idle))
643 panic("failed fork for CPU %d", cpu);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100644
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200645 init_gdt(cpu);
646 per_cpu(current_task, cpu) = idle;
Rusty Russellbf5046722007-05-02 19:27:10 +0200647 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100648
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100649 idle->thread.ip = (unsigned long) start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* start_eip had better be page-aligned! */
651 start_eip = setup_trampoline();
652
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100653 ++cpucount;
654 alternatives_smp_switch(1);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* So we see what's up */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100657 printk("Booting processor %d/%d ip %lx\n", cpu, apicid, start_eip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* Stack for startup_32 can be just as for start_secondary onwards */
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100659 stack_start.sp = (void *) idle->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 irq_ctx_init(cpu);
662
Mike Travis71fff5e2007-10-19 20:35:03 +0200663 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * This grunge runs the startup process for
666 * the targeted processor.
667 */
668
669 atomic_set(&init_deasserted, 0);
670
671 Dprintk("Setting warm reset code and vector.\n");
672
673 store_NMI_vector(&nmi_high, &nmi_low);
674
675 smpboot_setup_warm_reset_vector(start_eip);
676
677 /*
678 * Starting actual IPI sequence...
679 */
680 boot_error = wakeup_secondary_cpu(apicid, start_eip);
681
682 if (!boot_error) {
683 /*
684 * allow APs to start initializing.
685 */
686 Dprintk("Before Callout %d.\n", cpu);
687 cpu_set(cpu, cpu_callout_map);
688 Dprintk("After Callout %d.\n", cpu);
689
690 /*
691 * Wait 5s total for a response
692 */
693 for (timeout = 0; timeout < 50000; timeout++) {
694 if (cpu_isset(cpu, cpu_callin_map))
695 break; /* It has booted */
696 udelay(100);
697 }
698
699 if (cpu_isset(cpu, cpu_callin_map)) {
700 /* number CPUs logically, starting from 1 (BSP is 0) */
701 Dprintk("OK.\n");
702 printk("CPU%d: ", cpu);
Mike Travis92cb7612007-10-19 20:35:04 +0200703 print_cpu_info(&cpu_data(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 Dprintk("CPU has booted.\n");
705 } else {
706 boot_error= 1;
707 if (*((volatile unsigned char *)trampoline_base)
708 == 0xA5)
709 /* trampoline started but...? */
710 printk("Stuck ??\n");
711 else
712 /* trampoline code not run */
713 printk("Not responding.\n");
714 inquire_remote_apic(apicid);
715 }
716 }
Li Shaohuae1367da2005-06-25 14:54:56 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (boot_error) {
719 /* Try to put things back the way they were before ... */
720 unmap_cpu_to_logical_apicid(cpu);
721 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
722 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
723 cpucount--;
Li Shaohuae1367da2005-06-25 14:54:56 -0700724 } else {
Mike Travis71fff5e2007-10-19 20:35:03 +0200725 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
Li Shaohuae1367da2005-06-25 14:54:56 -0700726 cpu_set(cpu, cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
729 /* mark "stuck" area as not stuck */
730 *((volatile unsigned long *)trampoline_base) = 0;
731
732 return boot_error;
733}
734
Li Shaohuae1367da2005-06-25 14:54:56 -0700735#ifdef CONFIG_HOTPLUG_CPU
736void cpu_exit_clear(void)
737{
738 int cpu = raw_smp_processor_id();
739
740 idle_task_exit();
741
742 cpucount --;
743 cpu_uninit();
744 irq_ctx_exit(cpu);
745
746 cpu_clear(cpu, cpu_callout_map);
747 cpu_clear(cpu, cpu_callin_map);
Li Shaohuae1367da2005-06-25 14:54:56 -0700748
749 cpu_clear(cpu, smp_commenced_mask);
750 unmap_cpu_to_logical_apicid(cpu);
751}
752
753struct warm_boot_cpu_info {
754 struct completion *complete;
David Howellsc4028952006-11-22 14:57:56 +0000755 struct work_struct task;
Li Shaohuae1367da2005-06-25 14:54:56 -0700756 int apicid;
757 int cpu;
758};
759
David Howellsc4028952006-11-22 14:57:56 +0000760static void __cpuinit do_warm_boot_cpu(struct work_struct *work)
Li Shaohuae1367da2005-06-25 14:54:56 -0700761{
David Howellsc4028952006-11-22 14:57:56 +0000762 struct warm_boot_cpu_info *info =
763 container_of(work, struct warm_boot_cpu_info, task);
Li Shaohuae1367da2005-06-25 14:54:56 -0700764 do_boot_cpu(info->apicid, info->cpu);
765 complete(info->complete);
766}
767
Ashok Raj34f361a2006-03-25 03:08:18 -0800768static int __cpuinit __smp_prepare_cpu(int cpu)
Li Shaohuae1367da2005-06-25 14:54:56 -0700769{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700770 DECLARE_COMPLETION_ONSTACK(done);
Li Shaohuae1367da2005-06-25 14:54:56 -0700771 struct warm_boot_cpu_info info;
Li Shaohuae1367da2005-06-25 14:54:56 -0700772 int apicid, ret;
773
Mike Travis71fff5e2007-10-19 20:35:03 +0200774 apicid = per_cpu(x86_cpu_to_apicid, cpu);
Li Shaohuae1367da2005-06-25 14:54:56 -0700775 if (apicid == BAD_APICID) {
776 ret = -ENODEV;
777 goto exit;
778 }
779
780 info.complete = &done;
781 info.apicid = apicid;
782 info.cpu = cpu;
David Howellsc4028952006-11-22 14:57:56 +0000783 INIT_WORK(&info.task, do_warm_boot_cpu);
Li Shaohuae1367da2005-06-25 14:54:56 -0700784
Li Shaohuae1367da2005-06-25 14:54:56 -0700785 /* init low mem mapping */
Zachary Amsdend7271b12005-09-03 15:56:50 -0700786 clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
Shaohua Li3b1bdf42006-12-08 02:41:13 -0800787 min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
Li Shaohuae1367da2005-06-25 14:54:56 -0700788 flush_tlb_all();
David Howellsc4028952006-11-22 14:57:56 +0000789 schedule_work(&info.task);
Li Shaohuae1367da2005-06-25 14:54:56 -0700790 wait_for_completion(&done);
791
Li Shaohuae1367da2005-06-25 14:54:56 -0700792 zap_low_mappings();
793 ret = 0;
794exit:
Li Shaohuae1367da2005-06-25 14:54:56 -0700795 return ret;
796}
797#endif
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/*
800 * Cycle through the processors sending APIC IPIs to boot each.
801 */
802
803static int boot_cpu_logical_apicid;
804/* Where the IO area was mapped on multiquad, always 0 otherwise */
805void *xquad_portio;
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700806#ifdef CONFIG_X86_NUMAQ
807EXPORT_SYMBOL(xquad_portio);
808#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static void __init smp_boot_cpus(unsigned int max_cpus)
811{
812 int apicid, cpu, bit, kicked;
813 unsigned long bogosum = 0;
814
815 /*
816 * Setup boot CPU information
817 */
818 smp_store_cpu_info(0); /* Final full version of the data */
819 printk("CPU%d: ", 0);
Mike Travis92cb7612007-10-19 20:35:04 +0200820 print_cpu_info(&cpu_data(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800822 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 boot_cpu_logical_apicid = logical_smp_processor_id();
Mike Travis71fff5e2007-10-19 20:35:03 +0200824 per_cpu(x86_cpu_to_apicid, 0) = boot_cpu_physical_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 current_thread_info()->cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100828 set_cpu_sibling_map(0);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /*
831 * If we couldn't find an SMP configuration at boot time,
832 * get out of here now!
833 */
834 if (!smp_found_config && !acpi_lapic) {
835 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800836 smpboot_clear_io_apic_irqs();
837 phys_cpu_present_map = physid_mask_of_physid(0);
838 if (APIC_init_uniprocessor())
839 printk(KERN_NOTICE "Local APIC not detected."
840 " Using dummy APIC emulation.\n");
841 map_cpu_to_logical_apicid();
Mike Travisd5a74302007-10-16 01:24:05 -0700842 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700843 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return;
845 }
846
847 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800848 * Should not be necessary because the MP table should list the boot
849 * CPU too, but we do it for the sake of robustness anyway.
850 * Makes no sense to do this check in clustered apic mode, so skip it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800852 if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
853 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
854 boot_cpu_physical_apicid);
855 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
856 }
857
858 /*
859 * If we couldn't find a local APIC, then get out of here now!
860 */
861 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
862 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
863 boot_cpu_physical_apicid);
864 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
865 smpboot_clear_io_apic_irqs();
866 phys_cpu_present_map = physid_mask_of_physid(0);
Ingo Molnar54ffaa42007-10-19 20:35:02 +0200867 map_cpu_to_logical_apicid();
Mike Travisd5a74302007-10-16 01:24:05 -0700868 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700869 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return;
871 }
872
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800873 verify_local_APIC();
874
875 /*
876 * If SMP should be disabled, then really disable it!
877 */
878 if (!max_cpus) {
879 smp_found_config = 0;
880 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Ingo Molnar3fb450a2007-10-17 18:04:34 +0200881
882 if (nmi_watchdog == NMI_LOCAL_APIC) {
883 printk(KERN_INFO "activating minimal APIC for NMI watchdog use.\n");
884 connect_bsp_APIC();
885 setup_local_APIC();
886 }
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800887 smpboot_clear_io_apic_irqs();
888 phys_cpu_present_map = physid_mask_of_physid(0);
Ingo Molnar54ffaa42007-10-19 20:35:02 +0200889 map_cpu_to_logical_apicid();
Mike Travisd5a74302007-10-16 01:24:05 -0700890 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700891 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800892 return;
893 }
894
895 connect_bsp_APIC();
896 setup_local_APIC();
897 map_cpu_to_logical_apicid();
898
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 setup_portio_remap();
901
902 /*
903 * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
904 *
905 * In clustered apic mode, phys_cpu_present_map is a constructed thus:
906 * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
907 * clustered apic ID.
908 */
909 Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
910
911 kicked = 1;
912 for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
913 apicid = cpu_present_to_apicid(bit);
914 /*
915 * Don't even attempt to start the boot CPU!
916 */
917 if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
918 continue;
919
920 if (!check_apicid_present(bit))
921 continue;
922 if (max_cpus <= cpucount+1)
923 continue;
924
Li Shaohuae1367da2005-06-25 14:54:56 -0700925 if (((cpu = alloc_cpu_id()) <= 0) || do_boot_cpu(apicid, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 printk("CPU #%d not responding - cannot use it.\n",
927 apicid);
928 else
929 ++kicked;
930 }
931
932 /*
933 * Cleanup possible dangling ends...
934 */
935 smpboot_restore_warm_reset_vector();
936
937 /*
938 * Allow the user to impress friends.
939 */
940 Dprintk("Before bogomips.\n");
Mike Travis7bf0c232008-01-30 13:30:55 +0100941 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (cpu_isset(cpu, cpu_callout_map))
Mike Travis92cb7612007-10-19 20:35:04 +0200943 bogosum += cpu_data(cpu).loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 printk(KERN_INFO
945 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
946 cpucount+1,
947 bogosum/(500000/HZ),
948 (bogosum/(5000/HZ))%100);
949
950 Dprintk("Before bogocount - setting activated=1.\n");
951
952 if (smp_b_stepping)
953 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
954
955 /*
956 * Don't taint if we are running SMP kernel on a single non-MP
957 * approved Athlon
958 */
959 if (tainted & TAINT_UNSAFE_SMP) {
960 if (cpucount)
961 printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
962 else
963 tainted &= ~TAINT_UNSAFE_SMP;
964 }
965
966 Dprintk("Boot done.\n");
967
968 /*
Mike Travisd5a74302007-10-16 01:24:05 -0700969 * construct cpu_sibling_map, so that we can tell sibling CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * efficiently.
971 */
Mike Travis7bf0c232008-01-30 13:30:55 +0100972 for_each_possible_cpu(cpu) {
Mike Travisd5a74302007-10-16 01:24:05 -0700973 cpus_clear(per_cpu(cpu_sibling_map, cpu));
Mike Travis08357612007-10-16 01:24:04 -0700974 cpus_clear(per_cpu(cpu_core_map, cpu));
Andi Kleen3dd9d512005-04-16 15:25:15 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Mike Travisd5a74302007-10-16 01:24:05 -0700977 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700978 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800980 smpboot_setup_io_apic();
981
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100982 setup_boot_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985/* These are wrappers to interface to the new boot process. Someone
986 who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +0200987void __init native_smp_prepare_cpus(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700989 smp_commenced_mask = cpumask_of_cpu(0);
990 cpu_callin_map = cpumask_of_cpu(0);
991 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 smp_boot_cpus(max_cpus);
993}
994
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +0200995void __init native_smp_prepare_boot_cpu(void)
Rusty Russellbf5046722007-05-02 19:27:10 +0200996{
997 unsigned int cpu = smp_processor_id();
998
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200999 init_gdt(cpu);
Rusty Russellbf5046722007-05-02 19:27:10 +02001000 switch_to_new_gdt();
1001
1002 cpu_set(cpu, cpu_online_map);
1003 cpu_set(cpu, cpu_callout_map);
1004 cpu_set(cpu, cpu_present_map);
1005 cpu_set(cpu, cpu_possible_map);
1006 __get_cpu_var(cpu_state) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +02001009int __cpuinit native_cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Ingo Molnard04f41e2007-03-07 18:12:31 +01001011 unsigned long flags;
Ashok Raj34f361a2006-03-25 03:08:18 -08001012#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnard04f41e2007-03-07 18:12:31 +01001013 int ret = 0;
Ashok Raj34f361a2006-03-25 03:08:18 -08001014
1015 /*
1016 * We do warm boot only on cpus that had booted earlier
1017 * Otherwise cold boot is all handled from smp_boot_cpus().
1018 * cpu_callin_map is set during AP kickstart process. Its reset
1019 * when a cpu is taken offline from cpu_exit_clear().
1020 */
1021 if (!cpu_isset(cpu, cpu_callin_map))
1022 ret = __smp_prepare_cpu(cpu);
1023
1024 if (ret)
1025 return -EIO;
1026#endif
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* In case one didn't come up */
1029 if (!cpu_isset(cpu, cpu_callin_map)) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001030 printk(KERN_DEBUG "skipping cpu%d, didn't come online\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -EIO;
1032 }
1033
Li Shaohuae1367da2005-06-25 14:54:56 -07001034 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* Unleash the CPU! */
1036 cpu_set(cpu, smp_commenced_mask);
Ingo Molnar95492e42007-02-16 01:27:34 -08001037
1038 /*
Ingo Molnard04f41e2007-03-07 18:12:31 +01001039 * Check TSC synchronization with the AP (keep irqs disabled
1040 * while doing so):
Ingo Molnar95492e42007-02-16 01:27:34 -08001041 */
Ingo Molnard04f41e2007-03-07 18:12:31 +01001042 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -08001043 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +01001044 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -08001045
Ingo Molnard04f41e2007-03-07 18:12:31 +01001046 while (!cpu_isset(cpu, cpu_online_map)) {
Andreas Mohr18698912006-06-25 05:46:52 -07001047 cpu_relax();
Ingo Molnard04f41e2007-03-07 18:12:31 +01001048 touch_nmi_watchdog();
1049 }
Siddha, Suresh Bb0d0a4b2006-12-07 02:14:10 +01001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 0;
1052}
1053
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +02001054void __init native_smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056#ifdef CONFIG_X86_IO_APIC
1057 setup_ioapic_dest();
1058#endif
1059 zap_low_mappings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062void __init smp_intr_init(void)
1063{
1064 /*
1065 * IRQ0 must be given a fixed assignment and initialized,
1066 * because it's used before the IO-APIC is set up.
1067 */
1068 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1069
1070 /*
1071 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1072 * IPI, driven by wakeup.
1073 */
1074 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1075
1076 /* IPI for invalidation */
1077 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1078
1079 /* IPI for generic function call */
1080 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1081}