blob: 4e5416eb42b09877a0799765e22ce28daf4c1fa0 [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
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -030062extern int smp_b_stepping;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static cpumask_t smp_commenced_mask;
65
travis@sgi.com3b419082008-01-30 13:33:11 +010066/* which logical CPU number maps to which CPU (physical APIC ID) */
Glauber Costa5382e892008-03-03 14:12:57 -030067u16 x86_cpu_to_apicid_init[NR_CPUS] __initdata =
Mike Travis71fff5e2007-10-19 20:35:03 +020068 { [0 ... NR_CPUS-1] = BAD_APICID };
travis@sgi.com3b419082008-01-30 13:33:11 +010069void *x86_cpu_to_apicid_early_ptr;
Glauber Costa5382e892008-03-03 14:12:57 -030070DEFINE_PER_CPU(u16, x86_cpu_to_apicid) = BAD_APICID;
Mike Travis71fff5e2007-10-19 20:35:03 +020071EXPORT_PER_CPU_SYMBOL(x86_cpu_to_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
keith mannthey3b086062006-09-29 01:58:46 -070073u8 apicid_2_node[MAX_APICID];
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static void map_cpu_to_logical_apicid(void);
76
Zwane Mwaikambof3705132005-06-25 14:54:50 -070077/* State of each CPU. */
78DEFINE_PER_CPU(int, cpu_state) = { 0 };
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static atomic_t init_deasserted;
81
Vivek Goyal4a5d1072007-01-11 01:52:44 +010082static void __cpuinit smp_callin(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 int cpuid, phys_id;
85 unsigned long timeout;
86
87 /*
88 * If waken up by an INIT in an 82489DX configuration
89 * we may get here before an INIT-deassert IPI reaches
90 * our local APIC. We have to wait for the IPI or we'll
91 * lock up on an APIC access.
92 */
93 wait_for_init_deassert(&init_deasserted);
94
95 /*
96 * (This works even if the APIC is not enabled.)
97 */
98 phys_id = GET_APIC_ID(apic_read(APIC_ID));
99 cpuid = smp_processor_id();
100 if (cpu_isset(cpuid, cpu_callin_map)) {
101 printk("huh, phys CPU#%d, CPU#%d already present??\n",
102 phys_id, cpuid);
103 BUG();
104 }
105 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
106
107 /*
108 * STARTUP IPIs are fragile beasts as they might sometimes
109 * trigger some glue motherboard logic. Complete APIC bus
110 * silence for 1 second, this overestimates the time the
111 * boot CPU is spending to send the up to 2 STARTUP IPIs
112 * by a factor of two. This should be enough.
113 */
114
115 /*
116 * Waiting 2s total for startup (udelay is not yet working)
117 */
118 timeout = jiffies + 2*HZ;
119 while (time_before(jiffies, timeout)) {
120 /*
121 * Has the boot CPU finished it's STARTUP sequence?
122 */
123 if (cpu_isset(cpuid, cpu_callout_map))
124 break;
Glauber Costae1043832008-03-03 14:13:14 -0300125 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
128 if (!time_before(jiffies, timeout)) {
129 printk("BUG: CPU%d started up but did not get a callout!\n",
130 cpuid);
131 BUG();
132 }
133
134 /*
135 * the boot CPU has finished the init stage and is spinning
136 * on callin_map until we finish. We are free to set up this
137 * CPU, first the APIC. (this is probably redundant on most
138 * boards)
139 */
140
141 Dprintk("CALLIN, before setup_local_APIC().\n");
142 smp_callin_clear_local_apic();
143 setup_local_APIC();
144 map_cpu_to_logical_apicid();
145
146 /*
147 * Get our bogomips.
148 */
149 calibrate_delay();
150 Dprintk("Stack at about %p\n",&cpuid);
151
152 /*
153 * Save our processor parameters
154 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800155 smp_store_cpu_info(cpuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /*
158 * Allow the master to continue.
159 */
160 cpu_set(cpuid, cpu_callin_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163static int cpucount;
164
165/*
166 * Activate a secondary processor.
167 */
Vivek Goyal4a5d1072007-01-11 01:52:44 +0100168static void __cpuinit start_secondary(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 /*
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200171 * Don't put *anything* before cpu_init(), SMP booting is too
172 * fragile that we want to limit the things done here to the
173 * most necessary things.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +0100175#ifdef CONFIG_VMI
176 vmi_bringup();
177#endif
Rusty Russelld2cbcc42007-05-02 19:27:10 +0200178 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800179 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 smp_callin();
181 while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
Glauber Costae1043832008-03-03 14:13:14 -0300182 cpu_relax();
Ingo Molnar95492e42007-02-16 01:27:34 -0800183 /*
184 * Check TSC synchronization with the BP:
185 */
186 check_tsc_sync_target();
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (nmi_watchdog == NMI_IO_APIC) {
189 disable_8259A_irq(0);
Jan Beuliche9427102008-01-30 13:31:24 +0100190 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 enable_8259A_irq(0);
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
194 * low-memory mappings have been cleared, flush them from
195 * the local TLBs too.
196 */
197 local_flush_tlb();
Li Shaohua6fe940d2005-06-25 14:54:53 -0700198
Li Shaohuad7208032005-06-25 14:54:54 -0700199 /* This must be done before setting cpu_online_map */
200 set_cpu_sibling_map(raw_smp_processor_id());
201 wmb();
202
Li Shaohua6fe940d2005-06-25 14:54:53 -0700203 /*
204 * We need to hold call_lock, so there is no inconsistency
205 * between the time smp_call_function() determines number of
Simon Arlott27b46d72007-10-20 01:13:56 +0200206 * IPI recipients, and the time when the determination is made
Li Shaohua6fe940d2005-06-25 14:54:53 -0700207 * for which cpus receive the IPI. Holding this
208 * lock helps us to not include this cpu in a currently in progress
209 * smp_call_function().
210 */
211 lock_ipi_call_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 cpu_set(smp_processor_id(), cpu_online_map);
Li Shaohua6fe940d2005-06-25 14:54:53 -0700213 unlock_ipi_call_lock();
Li Shaohuae1367da2005-06-25 14:54:56 -0700214 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Glauber de Oliveira Costa8f15b822008-03-19 14:25:08 -0300216 setup_secondary_clock();
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 wmb();
219 cpu_idle();
220}
221
222/*
223 * Everything has been set up for the secondary
224 * CPUs - they just need to reload everything
225 * from the task structure
226 * This function must not return.
227 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700228void __devinit initialize_secondary(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 /*
231 * We don't actually need to load the full TSS,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100232 * basically just the stack pointer and the ip.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
234
235 asm volatile(
236 "movl %0,%%esp\n\t"
237 "jmp *%1"
238 :
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100239 :"m" (current->thread.sp),"m" (current->thread.ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100242/* Static state in head.S used to set up a CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243extern struct {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100244 void * sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned short ss;
246} stack_start;
247
248#ifdef CONFIG_NUMA
249
250/* which logical CPUs are on which nodes */
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100251cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100253EXPORT_SYMBOL(node_to_cpumask_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/* which node each logical CPU is on */
travis@sgi.com602a54a2008-01-30 13:33:21 +0100255int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100256EXPORT_SYMBOL(cpu_to_node_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258/* set up a mapping between cpu and node. */
259static inline void map_cpu_to_node(int cpu, int node)
260{
261 printk("Mapping cpu %d to node %d\n", cpu, node);
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100262 cpu_set(cpu, node_to_cpumask_map[node]);
263 cpu_to_node_map[cpu] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/* undo a mapping between cpu and node. */
267static inline void unmap_cpu_to_node(int cpu)
268{
269 int node;
270
271 printk("Unmapping cpu %d from all nodes\n", cpu);
272 for (node = 0; node < MAX_NUMNODES; node ++)
Thomas Gleixnerfe21a442008-01-30 13:30:38 +0100273 cpu_clear(cpu, node_to_cpumask_map[node]);
274 cpu_to_node_map[cpu] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276#else /* !CONFIG_NUMA */
277
278#define map_cpu_to_node(cpu, node) ({})
279#define unmap_cpu_to_node(cpu) ({})
280
281#endif /* CONFIG_NUMA */
282
Christoph Lameter6c036522005-07-07 17:56:59 -0700283u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285static void map_cpu_to_logical_apicid(void)
286{
287 int cpu = smp_processor_id();
288 int apicid = logical_smp_processor_id();
Keith Mannthey78b656b2006-10-03 18:25:52 -0700289 int node = apicid_to_node(apicid);
keith manntheybfa0e9a2006-09-25 16:25:35 -0700290
291 if (!node_online(node))
292 node = first_online_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 cpu_2_logical_apicid[cpu] = apicid;
keith manntheybfa0e9a2006-09-25 16:25:35 -0700295 map_cpu_to_node(cpu, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298static void unmap_cpu_to_logical_apicid(int cpu)
299{
300 cpu_2_logical_apicid[cpu] = BAD_APICID;
301 unmap_cpu_to_node(cpu);
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static inline void __inquire_remote_apic(int apicid)
305{
Glauber de Oliveira Costa26c6b5e2008-03-19 14:24:56 -0300306 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 char *names[] = { "ID", "VERSION", "SPIV" };
Fernando Luis VazquezCao4312fa82007-05-02 19:27:17 +0200308 int timeout;
Glauber de Oliveira Costa26c6b5e2008-03-19 14:24:56 -0300309 u32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300311 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Tobias Klauser38e548e2005-11-07 00:58:31 -0800313 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300314 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /*
317 * Wait for idle.
318 */
Fernando Luis VazquezCao4312fa82007-05-02 19:27:17 +0200319 status = safe_apic_wait_icr_idle();
320 if (status)
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300321 printk(KERN_CONT
322 "a previous APIC delivery may have failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
325 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
326
327 timeout = 0;
328 do {
329 udelay(100);
330 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
331 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
332
333 switch (status) {
334 case APIC_ICR_RR_VALID:
335 status = apic_read(APIC_RRR);
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300336 printk(KERN_CONT "%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 break;
338 default:
Glauber de Oliveira Costaba104852008-03-19 14:24:57 -0300339 printk(KERN_CONT "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 }
342}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344#ifdef WAKE_SECONDARY_VIA_NMI
345/*
346 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
347 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
348 * won't ... remember to clear down the APIC, etc later.
349 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700350static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
352{
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200353 unsigned long send_status, accept_status = 0;
354 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* Target chip */
357 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
358
359 /* Boot on the stack */
360 /* Kick the second */
361 apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
362
363 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200364 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /*
367 * Give the other CPU some time to accept the IPI.
368 */
369 udelay(200);
370 /*
371 * Due to the Pentium erratum 3AP.
372 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800373 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (maxlvt > 3) {
375 apic_read_around(APIC_SPIV);
376 apic_write(APIC_ESR, 0);
377 }
378 accept_status = (apic_read(APIC_ESR) & 0xEF);
379 Dprintk("NMI sent.\n");
380
381 if (send_status)
382 printk("APIC never delivered???\n");
383 if (accept_status)
384 printk("APIC delivery error (%lx).\n", accept_status);
385
386 return (send_status | accept_status);
387}
388#endif /* WAKE_SECONDARY_VIA_NMI */
389
390#ifdef WAKE_SECONDARY_VIA_INIT
Li Shaohua0bb31842005-06-25 14:54:55 -0700391static int __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
393{
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200394 unsigned long send_status, accept_status = 0;
395 int maxlvt, num_starts, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /*
398 * Be paranoid about clearing APIC errors.
399 */
400 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
401 apic_read_around(APIC_SPIV);
402 apic_write(APIC_ESR, 0);
403 apic_read(APIC_ESR);
404 }
405
406 Dprintk("Asserting INIT.\n");
407
408 /*
409 * Turn INIT on target chip
410 */
411 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
412
413 /*
414 * Send IPI
415 */
416 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
417 | APIC_DM_INIT);
418
419 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200420 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 mdelay(10);
423
424 Dprintk("Deasserting INIT.\n");
425
426 /* Target chip */
427 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
428
429 /* Send IPI */
430 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
431
432 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200433 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 atomic_set(&init_deasserted, 1);
436
437 /*
438 * Should we send STARTUP IPIs ?
439 *
440 * Determine this based on the APIC version.
441 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
442 */
443 if (APIC_INTEGRATED(apic_version[phys_apicid]))
444 num_starts = 2;
445 else
446 num_starts = 0;
447
448 /*
Zachary Amsdenae5da272007-02-13 13:26:21 +0100449 * Paravirt / VMI wants a startup IPI hook here to set up the
450 * target processor state.
451 */
452 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100453 (unsigned long) stack_start.sp);
Zachary Amsdenae5da272007-02-13 13:26:21 +0100454
455 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * Run STARTUP IPI loop.
457 */
458 Dprintk("#startup loops: %d.\n", num_starts);
459
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800460 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 for (j = 1; j <= num_starts; j++) {
463 Dprintk("Sending STARTUP #%d.\n",j);
464 apic_read_around(APIC_SPIV);
465 apic_write(APIC_ESR, 0);
466 apic_read(APIC_ESR);
467 Dprintk("After apic_write.\n");
468
469 /*
470 * STARTUP IPI
471 */
472
473 /* Target chip */
474 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
475
476 /* Boot on the stack */
477 /* Kick the second */
478 apic_write_around(APIC_ICR, APIC_DM_STARTUP
479 | (start_eip >> 12));
480
481 /*
482 * Give the other CPU some time to accept the IPI.
483 */
484 udelay(300);
485
486 Dprintk("Startup point 1.\n");
487
488 Dprintk("Waiting for send to finish...\n");
Fernando Luis VazquezCaoae08e432007-05-02 19:27:17 +0200489 send_status = safe_apic_wait_icr_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /*
492 * Give the other CPU some time to accept the IPI.
493 */
494 udelay(200);
495 /*
496 * Due to the Pentium erratum 3AP.
497 */
498 if (maxlvt > 3) {
499 apic_read_around(APIC_SPIV);
500 apic_write(APIC_ESR, 0);
501 }
502 accept_status = (apic_read(APIC_ESR) & 0xEF);
503 if (send_status || accept_status)
504 break;
505 }
506 Dprintk("After Startup.\n");
507
508 if (send_status)
509 printk("APIC never delivered???\n");
510 if (accept_status)
511 printk("APIC delivery error (%lx).\n", accept_status);
512
513 return (send_status | accept_status);
514}
515#endif /* WAKE_SECONDARY_VIA_INIT */
516
517extern cpumask_t cpu_initialized;
Li Shaohuae1367da2005-06-25 14:54:56 -0700518static inline int alloc_cpu_id(void)
519{
520 cpumask_t tmp_map;
521 int cpu;
522 cpus_complement(tmp_map, cpu_present_map);
523 cpu = first_cpu(tmp_map);
524 if (cpu >= NR_CPUS)
525 return -ENODEV;
526 return cpu;
527}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Li Shaohuae1367da2005-06-25 14:54:56 -0700529#ifdef CONFIG_HOTPLUG_CPU
Adrian Bunkf2206ec2007-12-19 23:20:18 +0100530static struct task_struct * __cpuinitdata cpu_idle_tasks[NR_CPUS];
531static inline struct task_struct * __cpuinit alloc_idle_task(int cpu)
Li Shaohuae1367da2005-06-25 14:54:56 -0700532{
533 struct task_struct *idle;
534
535 if ((idle = cpu_idle_tasks[cpu]) != NULL) {
536 /* initialize thread_struct. we really want to avoid destroy
537 * idle tread
538 */
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100539 idle->thread.sp = (unsigned long)task_pt_regs(idle);
Li Shaohuae1367da2005-06-25 14:54:56 -0700540 init_idle(idle, cpu);
541 return idle;
542 }
543 idle = fork_idle(cpu);
544
545 if (!IS_ERR(idle))
546 cpu_idle_tasks[cpu] = idle;
547 return idle;
548}
549#else
550#define alloc_idle_task(cpu) fork_idle(cpu)
551#endif
552
Vivek Goyal4a5d1072007-01-11 01:52:44 +0100553static int __cpuinit do_boot_cpu(int apicid, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/*
555 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
556 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
557 * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
558 */
559{
560 struct task_struct *idle;
561 unsigned long boot_error;
Li Shaohuae1367da2005-06-25 14:54:56 -0700562 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 unsigned long start_eip;
564 unsigned short nmi_high = 0, nmi_low = 0;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /*
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +0200567 * Save current MTRR state in case it was changed since early boot
568 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
569 */
570 mtrr_save_state();
571
572 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 * We can't use kernel_thread since we must avoid to
574 * reschedule the child.
575 */
Li Shaohuae1367da2005-06-25 14:54:56 -0700576 idle = alloc_idle_task(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (IS_ERR(idle))
578 panic("failed fork for CPU %d", cpu);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100579
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200580 init_gdt(cpu);
581 per_cpu(current_task, cpu) = idle;
Rusty Russellbf5046722007-05-02 19:27:10 +0200582 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100583
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100584 idle->thread.ip = (unsigned long) start_secondary;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* start_eip had better be page-aligned! */
586 start_eip = setup_trampoline();
587
Jeremy Fitzhardinge62111192006-12-07 02:14:02 +0100588 ++cpucount;
589 alternatives_smp_switch(1);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* So we see what's up */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100592 printk("Booting processor %d/%d ip %lx\n", cpu, apicid, start_eip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* Stack for startup_32 can be just as for start_secondary onwards */
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100594 stack_start.sp = (void *) idle->thread.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 irq_ctx_init(cpu);
597
Mike Travis71fff5e2007-10-19 20:35:03 +0200598 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /*
600 * This grunge runs the startup process for
601 * the targeted processor.
602 */
603
604 atomic_set(&init_deasserted, 0);
605
606 Dprintk("Setting warm reset code and vector.\n");
607
608 store_NMI_vector(&nmi_high, &nmi_low);
609
610 smpboot_setup_warm_reset_vector(start_eip);
611
612 /*
613 * Starting actual IPI sequence...
614 */
615 boot_error = wakeup_secondary_cpu(apicid, start_eip);
616
617 if (!boot_error) {
618 /*
619 * allow APs to start initializing.
620 */
621 Dprintk("Before Callout %d.\n", cpu);
622 cpu_set(cpu, cpu_callout_map);
623 Dprintk("After Callout %d.\n", cpu);
624
625 /*
626 * Wait 5s total for a response
627 */
628 for (timeout = 0; timeout < 50000; timeout++) {
629 if (cpu_isset(cpu, cpu_callin_map))
630 break; /* It has booted */
631 udelay(100);
632 }
633
634 if (cpu_isset(cpu, cpu_callin_map)) {
635 /* number CPUs logically, starting from 1 (BSP is 0) */
636 Dprintk("OK.\n");
637 printk("CPU%d: ", cpu);
Mike Travis92cb7612007-10-19 20:35:04 +0200638 print_cpu_info(&cpu_data(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 Dprintk("CPU has booted.\n");
640 } else {
641 boot_error= 1;
642 if (*((volatile unsigned char *)trampoline_base)
643 == 0xA5)
644 /* trampoline started but...? */
645 printk("Stuck ??\n");
646 else
647 /* trampoline code not run */
648 printk("Not responding.\n");
649 inquire_remote_apic(apicid);
650 }
651 }
Li Shaohuae1367da2005-06-25 14:54:56 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (boot_error) {
654 /* Try to put things back the way they were before ... */
655 unmap_cpu_to_logical_apicid(cpu);
656 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
657 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
658 cpucount--;
Li Shaohuae1367da2005-06-25 14:54:56 -0700659 } else {
Mike Travis71fff5e2007-10-19 20:35:03 +0200660 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
Li Shaohuae1367da2005-06-25 14:54:56 -0700661 cpu_set(cpu, cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
664 /* mark "stuck" area as not stuck */
665 *((volatile unsigned long *)trampoline_base) = 0;
666
667 return boot_error;
668}
669
Li Shaohuae1367da2005-06-25 14:54:56 -0700670#ifdef CONFIG_HOTPLUG_CPU
671void cpu_exit_clear(void)
672{
673 int cpu = raw_smp_processor_id();
674
675 idle_task_exit();
676
677 cpucount --;
678 cpu_uninit();
679 irq_ctx_exit(cpu);
680
681 cpu_clear(cpu, cpu_callout_map);
682 cpu_clear(cpu, cpu_callin_map);
Li Shaohuae1367da2005-06-25 14:54:56 -0700683
684 cpu_clear(cpu, smp_commenced_mask);
685 unmap_cpu_to_logical_apicid(cpu);
686}
687
688struct warm_boot_cpu_info {
689 struct completion *complete;
David Howellsc4028952006-11-22 14:57:56 +0000690 struct work_struct task;
Li Shaohuae1367da2005-06-25 14:54:56 -0700691 int apicid;
692 int cpu;
693};
694
David Howellsc4028952006-11-22 14:57:56 +0000695static void __cpuinit do_warm_boot_cpu(struct work_struct *work)
Li Shaohuae1367da2005-06-25 14:54:56 -0700696{
David Howellsc4028952006-11-22 14:57:56 +0000697 struct warm_boot_cpu_info *info =
698 container_of(work, struct warm_boot_cpu_info, task);
Li Shaohuae1367da2005-06-25 14:54:56 -0700699 do_boot_cpu(info->apicid, info->cpu);
700 complete(info->complete);
701}
702
Ashok Raj34f361a2006-03-25 03:08:18 -0800703static int __cpuinit __smp_prepare_cpu(int cpu)
Li Shaohuae1367da2005-06-25 14:54:56 -0700704{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700705 DECLARE_COMPLETION_ONSTACK(done);
Li Shaohuae1367da2005-06-25 14:54:56 -0700706 struct warm_boot_cpu_info info;
Li Shaohuae1367da2005-06-25 14:54:56 -0700707 int apicid, ret;
708
Mike Travis71fff5e2007-10-19 20:35:03 +0200709 apicid = per_cpu(x86_cpu_to_apicid, cpu);
Li Shaohuae1367da2005-06-25 14:54:56 -0700710 if (apicid == BAD_APICID) {
711 ret = -ENODEV;
712 goto exit;
713 }
714
715 info.complete = &done;
716 info.apicid = apicid;
717 info.cpu = cpu;
David Howellsc4028952006-11-22 14:57:56 +0000718 INIT_WORK(&info.task, do_warm_boot_cpu);
Li Shaohuae1367da2005-06-25 14:54:56 -0700719
Li Shaohuae1367da2005-06-25 14:54:56 -0700720 /* init low mem mapping */
Zachary Amsdend7271b12005-09-03 15:56:50 -0700721 clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
Shaohua Li3b1bdf42006-12-08 02:41:13 -0800722 min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
Li Shaohuae1367da2005-06-25 14:54:56 -0700723 flush_tlb_all();
David Howellsc4028952006-11-22 14:57:56 +0000724 schedule_work(&info.task);
Li Shaohuae1367da2005-06-25 14:54:56 -0700725 wait_for_completion(&done);
726
Li Shaohuae1367da2005-06-25 14:54:56 -0700727 zap_low_mappings();
728 ret = 0;
729exit:
Li Shaohuae1367da2005-06-25 14:54:56 -0700730 return ret;
731}
732#endif
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/*
735 * Cycle through the processors sending APIC IPIs to boot each.
736 */
737
738static int boot_cpu_logical_apicid;
739/* Where the IO area was mapped on multiquad, always 0 otherwise */
740void *xquad_portio;
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700741#ifdef CONFIG_X86_NUMAQ
742EXPORT_SYMBOL(xquad_portio);
743#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static void __init smp_boot_cpus(unsigned int max_cpus)
746{
747 int apicid, cpu, bit, kicked;
748 unsigned long bogosum = 0;
749
750 /*
751 * Setup boot CPU information
752 */
753 smp_store_cpu_info(0); /* Final full version of the data */
754 printk("CPU%d: ", 0);
Mike Travis92cb7612007-10-19 20:35:04 +0200755 print_cpu_info(&cpu_data(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800757 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 boot_cpu_logical_apicid = logical_smp_processor_id();
Mike Travis71fff5e2007-10-19 20:35:03 +0200759 per_cpu(x86_cpu_to_apicid, 0) = boot_cpu_physical_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 current_thread_info()->cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Siddha, Suresh B94605ef2005-11-05 17:25:54 +0100763 set_cpu_sibling_map(0);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /*
766 * If we couldn't find an SMP configuration at boot time,
767 * get out of here now!
768 */
769 if (!smp_found_config && !acpi_lapic) {
770 printk(KERN_NOTICE "SMP motherboard not detected.\n");
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800771 smpboot_clear_io_apic_irqs();
772 phys_cpu_present_map = physid_mask_of_physid(0);
773 if (APIC_init_uniprocessor())
774 printk(KERN_NOTICE "Local APIC not detected."
775 " Using dummy APIC emulation.\n");
776 map_cpu_to_logical_apicid();
Mike Travisd5a74302007-10-16 01:24:05 -0700777 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700778 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return;
780 }
781
782 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800783 * Should not be necessary because the MP table should list the boot
784 * CPU too, but we do it for the sake of robustness anyway.
785 * Makes no sense to do this check in clustered apic mode, so skip it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800787 if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
788 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
789 boot_cpu_physical_apicid);
790 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
791 }
792
793 /*
794 * If we couldn't find a local APIC, then get out of here now!
795 */
796 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
797 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
798 boot_cpu_physical_apicid);
799 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
800 smpboot_clear_io_apic_irqs();
801 phys_cpu_present_map = physid_mask_of_physid(0);
Ingo Molnar54ffaa42007-10-19 20:35:02 +0200802 map_cpu_to_logical_apicid();
Mike Travisd5a74302007-10-16 01:24:05 -0700803 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700804 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return;
806 }
807
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800808 verify_local_APIC();
809
810 /*
811 * If SMP should be disabled, then really disable it!
812 */
813 if (!max_cpus) {
814 smp_found_config = 0;
815 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
Ingo Molnar3fb450a2007-10-17 18:04:34 +0200816
817 if (nmi_watchdog == NMI_LOCAL_APIC) {
818 printk(KERN_INFO "activating minimal APIC for NMI watchdog use.\n");
819 connect_bsp_APIC();
820 setup_local_APIC();
821 }
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800822 smpboot_clear_io_apic_irqs();
823 phys_cpu_present_map = physid_mask_of_physid(0);
Ingo Molnar54ffaa42007-10-19 20:35:02 +0200824 map_cpu_to_logical_apicid();
Mike Travisd5a74302007-10-16 01:24:05 -0700825 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700826 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800827 return;
828 }
829
830 connect_bsp_APIC();
831 setup_local_APIC();
832 map_cpu_to_logical_apicid();
833
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 setup_portio_remap();
836
837 /*
838 * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
839 *
840 * In clustered apic mode, phys_cpu_present_map is a constructed thus:
841 * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
842 * clustered apic ID.
843 */
844 Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
845
846 kicked = 1;
847 for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
848 apicid = cpu_present_to_apicid(bit);
849 /*
850 * Don't even attempt to start the boot CPU!
851 */
852 if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
853 continue;
854
855 if (!check_apicid_present(bit))
856 continue;
857 if (max_cpus <= cpucount+1)
858 continue;
859
Li Shaohuae1367da2005-06-25 14:54:56 -0700860 if (((cpu = alloc_cpu_id()) <= 0) || do_boot_cpu(apicid, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 printk("CPU #%d not responding - cannot use it.\n",
862 apicid);
863 else
864 ++kicked;
865 }
866
867 /*
868 * Cleanup possible dangling ends...
869 */
870 smpboot_restore_warm_reset_vector();
871
872 /*
873 * Allow the user to impress friends.
874 */
875 Dprintk("Before bogomips.\n");
Mike Travis7bf0c232008-01-30 13:30:55 +0100876 for_each_possible_cpu(cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (cpu_isset(cpu, cpu_callout_map))
Mike Travis92cb7612007-10-19 20:35:04 +0200878 bogosum += cpu_data(cpu).loops_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 printk(KERN_INFO
880 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
881 cpucount+1,
882 bogosum/(500000/HZ),
883 (bogosum/(5000/HZ))%100);
884
885 Dprintk("Before bogocount - setting activated=1.\n");
886
887 if (smp_b_stepping)
888 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
889
890 /*
891 * Don't taint if we are running SMP kernel on a single non-MP
892 * approved Athlon
893 */
894 if (tainted & TAINT_UNSAFE_SMP) {
895 if (cpucount)
896 printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
897 else
898 tainted &= ~TAINT_UNSAFE_SMP;
899 }
900
901 Dprintk("Boot done.\n");
902
903 /*
Mike Travisd5a74302007-10-16 01:24:05 -0700904 * construct cpu_sibling_map, so that we can tell sibling CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * efficiently.
906 */
Mike Travis7bf0c232008-01-30 13:30:55 +0100907 for_each_possible_cpu(cpu) {
Mike Travisd5a74302007-10-16 01:24:05 -0700908 cpus_clear(per_cpu(cpu_sibling_map, cpu));
Mike Travis08357612007-10-16 01:24:04 -0700909 cpus_clear(per_cpu(cpu_core_map, cpu));
Andi Kleen3dd9d512005-04-16 15:25:15 -0700910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Mike Travisd5a74302007-10-16 01:24:05 -0700912 cpu_set(0, per_cpu(cpu_sibling_map, 0));
Mike Travis08357612007-10-16 01:24:04 -0700913 cpu_set(0, per_cpu(cpu_core_map, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800915 smpboot_setup_io_apic();
916
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100917 setup_boot_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920/* These are wrappers to interface to the new boot process. Someone
921 who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +0200922void __init native_smp_prepare_cpus(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700924 smp_commenced_mask = cpumask_of_cpu(0);
925 cpu_callin_map = cpumask_of_cpu(0);
926 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 smp_boot_cpus(max_cpus);
928}
929
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +0200930void __init native_smp_prepare_boot_cpu(void)
Rusty Russellbf5046722007-05-02 19:27:10 +0200931{
932 unsigned int cpu = smp_processor_id();
933
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200934 init_gdt(cpu);
Rusty Russellbf5046722007-05-02 19:27:10 +0200935 switch_to_new_gdt();
936
937 cpu_set(cpu, cpu_online_map);
938 cpu_set(cpu, cpu_callout_map);
939 cpu_set(cpu, cpu_present_map);
940 cpu_set(cpu, cpu_possible_map);
941 __get_cpu_var(cpu_state) = CPU_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +0200944int __cpuinit native_cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Ingo Molnard04f41e2007-03-07 18:12:31 +0100946 unsigned long flags;
Ashok Raj34f361a2006-03-25 03:08:18 -0800947#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnard04f41e2007-03-07 18:12:31 +0100948 int ret = 0;
Ashok Raj34f361a2006-03-25 03:08:18 -0800949
950 /*
951 * We do warm boot only on cpus that had booted earlier
952 * Otherwise cold boot is all handled from smp_boot_cpus().
953 * cpu_callin_map is set during AP kickstart process. Its reset
954 * when a cpu is taken offline from cpu_exit_clear().
955 */
956 if (!cpu_isset(cpu, cpu_callin_map))
957 ret = __smp_prepare_cpu(cpu);
958
959 if (ret)
960 return -EIO;
961#endif
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* In case one didn't come up */
964 if (!cpu_isset(cpu, cpu_callin_map)) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700965 printk(KERN_DEBUG "skipping cpu%d, didn't come online\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return -EIO;
967 }
968
Li Shaohuae1367da2005-06-25 14:54:56 -0700969 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* Unleash the CPU! */
971 cpu_set(cpu, smp_commenced_mask);
Ingo Molnar95492e42007-02-16 01:27:34 -0800972
973 /*
Ingo Molnard04f41e2007-03-07 18:12:31 +0100974 * Check TSC synchronization with the AP (keep irqs disabled
975 * while doing so):
Ingo Molnar95492e42007-02-16 01:27:34 -0800976 */
Ingo Molnard04f41e2007-03-07 18:12:31 +0100977 local_irq_save(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800978 check_tsc_sync_source(cpu);
Ingo Molnard04f41e2007-03-07 18:12:31 +0100979 local_irq_restore(flags);
Ingo Molnar95492e42007-02-16 01:27:34 -0800980
Ingo Molnard04f41e2007-03-07 18:12:31 +0100981 while (!cpu_isset(cpu, cpu_online_map)) {
Andreas Mohr18698912006-06-25 05:46:52 -0700982 cpu_relax();
Ingo Molnard04f41e2007-03-07 18:12:31 +0100983 touch_nmi_watchdog();
984 }
Siddha, Suresh Bb0d0a4b2006-12-07 02:14:10 +0100985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987}
988
Jeremy Fitzhardinge01a2f432007-05-02 19:27:11 +0200989void __init native_smp_cpus_done(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991#ifdef CONFIG_X86_IO_APIC
992 setup_ioapic_dest();
993#endif
994 zap_low_mappings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997void __init smp_intr_init(void)
998{
999 /*
1000 * IRQ0 must be given a fixed assignment and initialized,
1001 * because it's used before the IO-APIC is set up.
1002 */
1003 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1004
1005 /*
1006 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1007 * IPI, driven by wakeup.
1008 */
1009 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1010
1011 /* IPI for invalidation */
1012 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1013
1014 /* IPI for generic function call */
1015 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1016}