x86/asm/boot/64: Use __BOOT_TSS instead of literal $0x20
[linux-2.6-block.git] / arch / x86 / kernel / smpboot.c
CommitLineData
c767a54b 1 /*
4cedb334
GOC
2 * x86 SMP booting functions
3 *
87c6fe26 4 * (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
8f47e163 5 * (c) 1998, 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
4cedb334
GOC
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 *
15 * This code is released under the GNU General Public License version 2 or
16 * later.
17 *
18 * Fixes
19 * Felix Koop : NR_CPUS used properly
20 * Jose Renau : Handle single CPU case.
21 * Alan Cox : By repeated request 8) - Total BogoMIPS report.
22 * Greg Wright : Fix for kernel stacks panic.
23 * Erich Boleyn : MP v1.4 and additional changes.
24 * Matthias Sattler : Changes for 2.1 kernel map.
25 * Michel Lespinasse : Changes for 2.1 kernel map.
26 * Michael Chastain : Change trampoline.S to gnu as.
27 * Alan Cox : Dumb bug: 'B' step PPro's are fine
28 * Ingo Molnar : Added APIC timers, based on code
29 * from Jose Renau
30 * Ingo Molnar : various cleanups and rewrites
31 * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
32 * Maciej W. Rozycki : Bits for genuine 82489DX APICs
33 * Andi Kleen : Changed for SMP boot into long mode.
34 * Martin J. Bligh : Added support for multi-quad systems
35 * Dave Jones : Report invalid combinations of Athlon CPUs.
36 * Rusty Russell : Hacked into shape for new "hotplug" boot process.
37 * Andi Kleen : Converted to new state machine.
38 * Ashok Raj : CPU hotplug support
39 * Glauber Costa : i386 and x86_64 integration
40 */
41
c767a54b
JP
42#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
68a1c3f8
GC
44#include <linux/init.h>
45#include <linux/smp.h>
a355352b 46#include <linux/module.h>
70708a18 47#include <linux/sched.h>
69c18c15 48#include <linux/percpu.h>
91718e8d 49#include <linux/bootmem.h>
cb3c8b90
GOC
50#include <linux/err.h>
51#include <linux/nmi.h>
69575d38 52#include <linux/tboot.h>
35f720c5 53#include <linux/stackprotector.h>
5a0e3ad6 54#include <linux/gfp.h>
1a022e3f 55#include <linux/cpuidle.h>
69c18c15 56
8aef135c 57#include <asm/acpi.h>
cb3c8b90 58#include <asm/desc.h>
69c18c15
GC
59#include <asm/nmi.h>
60#include <asm/irq.h>
07bbc16a 61#include <asm/idle.h>
48927bbb 62#include <asm/realmode.h>
69c18c15
GC
63#include <asm/cpu.h>
64#include <asm/numa.h>
cb3c8b90
GOC
65#include <asm/pgtable.h>
66#include <asm/tlbflush.h>
67#include <asm/mtrr.h>
ea530692 68#include <asm/mwait.h>
7b6aa335 69#include <asm/apic.h>
7167d08e 70#include <asm/io_apic.h>
644c1541
VP
71#include <asm/i387.h>
72#include <asm/fpu-internal.h>
569712b2 73#include <asm/setup.h>
bdbcdd48 74#include <asm/uv/uv.h>
cb3c8b90 75#include <linux/mc146818rtc.h>
b81bb373 76#include <asm/i8259.h>
48927bbb 77#include <asm/realmode.h>
646e29a1 78#include <asm/misc.h>
48927bbb 79
a8db8453
GOC
80/* State of each CPU */
81DEFINE_PER_CPU(int, cpu_state) = { 0 };
82
a355352b
GC
83/* Number of siblings per CPU package */
84int smp_num_siblings = 1;
85EXPORT_SYMBOL(smp_num_siblings);
86
87/* Last level cache ID of each logical CPU */
0816b0f0 88DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
a355352b 89
a355352b 90/* representing HT siblings of each logical CPU */
0816b0f0 91DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
a355352b
GC
92EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
93
94/* representing HT and core siblings of each logical CPU */
0816b0f0 95DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
a355352b
GC
96EXPORT_PER_CPU_SYMBOL(cpu_core_map);
97
0816b0f0 98DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
b3d7336d 99
a355352b 100/* Per CPU bogomips and other parameters */
2c773dd3 101DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
a355352b 102EXPORT_PER_CPU_SYMBOL(cpu_info);
768d9505 103
2b6163bf 104atomic_t init_deasserted;
cb3c8b90 105
f77aa308
TG
106static inline void smpboot_setup_warm_reset_vector(unsigned long start_eip)
107{
108 unsigned long flags;
109
110 spin_lock_irqsave(&rtc_lock, flags);
111 CMOS_WRITE(0xa, 0xf);
112 spin_unlock_irqrestore(&rtc_lock, flags);
113 local_flush_tlb();
114 pr_debug("1.\n");
115 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_HIGH)) =
116 start_eip >> 4;
117 pr_debug("2.\n");
118 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) =
119 start_eip & 0xf;
120 pr_debug("3.\n");
121}
122
123static inline void smpboot_restore_warm_reset_vector(void)
124{
125 unsigned long flags;
126
127 /*
128 * Install writable page 0 entry to set BIOS data area.
129 */
130 local_flush_tlb();
131
132 /*
133 * Paranoid: Set warm reset code and vector here back
134 * to default values.
135 */
136 spin_lock_irqsave(&rtc_lock, flags);
137 CMOS_WRITE(0, 0xf);
138 spin_unlock_irqrestore(&rtc_lock, flags);
139
140 *((volatile u32 *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) = 0;
141}
142
cb3c8b90 143/*
30106c17
FY
144 * Report back to the Boot Processor during boot time or to the caller processor
145 * during CPU online.
cb3c8b90 146 */
148f9bb8 147static void smp_callin(void)
cb3c8b90
GOC
148{
149 int cpuid, phys_id;
cb3c8b90
GOC
150
151 /*
152 * If waken up by an INIT in an 82489DX configuration
153 * we may get here before an INIT-deassert IPI reaches
154 * our local APIC. We have to wait for the IPI or we'll
155 * lock up on an APIC access.
e1c467e6
FY
156 *
157 * Since CPU0 is not wakened up by INIT, it doesn't wait for the IPI.
cb3c8b90 158 */
e1c467e6 159 cpuid = smp_processor_id();
465822cf
DR
160 if (apic->wait_for_init_deassert && cpuid)
161 while (!atomic_read(&init_deasserted))
162 cpu_relax();
cb3c8b90
GOC
163
164 /*
165 * (This works even if the APIC is not enabled.)
166 */
4c9961d5 167 phys_id = read_apic_id();
cb3c8b90
GOC
168
169 /*
170 * the boot CPU has finished the init stage and is spinning
171 * on callin_map until we finish. We are free to set up this
172 * CPU, first the APIC. (this is probably redundant on most
173 * boards)
174 */
05f7e46d 175 apic_ap_setup();
cb3c8b90 176
9d133e5d
SS
177 /*
178 * Need to setup vector mappings before we enable interrupts.
179 */
36e9e1ea 180 setup_vector_irq(smp_processor_id());
b565201c
JS
181
182 /*
183 * Save our processor parameters. Note: this information
184 * is needed for clock calibration.
185 */
186 smp_store_cpu_info(cpuid);
187
cb3c8b90
GOC
188 /*
189 * Get our bogomips.
b565201c
JS
190 * Update loops_per_jiffy in cpu_data. Previous call to
191 * smp_store_cpu_info() stored a value that is close but not as
192 * accurate as the value just calculated.
cb3c8b90 193 */
cb3c8b90 194 calibrate_delay();
b565201c 195 cpu_data(cpuid).loops_per_jiffy = loops_per_jiffy;
cfc1b9a6 196 pr_debug("Stack at about %p\n", &cpuid);
cb3c8b90 197
5ef428c4
AK
198 /*
199 * This must be done before setting cpu_online_mask
200 * or calling notify_cpu_starting.
201 */
202 set_cpu_sibling_map(raw_smp_processor_id());
203 wmb();
204
85257024
PZ
205 notify_cpu_starting(cpuid);
206
cb3c8b90
GOC
207 /*
208 * Allow the master to continue.
209 */
c2d1cec1 210 cpumask_set_cpu(cpuid, cpu_callin_mask);
cb3c8b90
GOC
211}
212
e1c467e6
FY
213static int cpu0_logical_apicid;
214static int enable_start_cpu0;
bbc2ff6a
GOC
215/*
216 * Activate a secondary processor.
217 */
148f9bb8 218static void notrace start_secondary(void *unused)
bbc2ff6a
GOC
219{
220 /*
221 * Don't put *anything* before cpu_init(), SMP booting is too
222 * fragile that we want to limit the things done here to the
223 * most necessary things.
224 */
b40827fa 225 cpu_init();
df156f90 226 x86_cpuinit.early_percpu_clock_init();
b40827fa
BP
227 preempt_disable();
228 smp_callin();
fd89a137 229
e1c467e6
FY
230 enable_start_cpu0 = 0;
231
fd89a137 232#ifdef CONFIG_X86_32
b40827fa 233 /* switch away from the initial page table */
fd89a137
JR
234 load_cr3(swapper_pg_dir);
235 __flush_tlb_all();
236#endif
237
bbc2ff6a
GOC
238 /* otherwise gcc will move up smp_processor_id before the cpu_init */
239 barrier();
240 /*
241 * Check TSC synchronization with the BP:
242 */
243 check_tsc_sync_target();
244
3891a04a
PA
245 /*
246 * Enable the espfix hack for this CPU
247 */
197725de 248#ifdef CONFIG_X86_ESPFIX64
3891a04a
PA
249 init_espfix_ap();
250#endif
251
bbc2ff6a 252 /*
d388e5fd
EB
253 * We need to hold vector_lock so there the set of online cpus
254 * does not change while we are assigning vectors to cpus. Holding
255 * this lock ensures we don't half assign or remove an irq from a cpu.
bbc2ff6a 256 */
d388e5fd 257 lock_vector_lock();
c2d1cec1 258 set_cpu_online(smp_processor_id(), true);
d388e5fd 259 unlock_vector_lock();
bbc2ff6a 260 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
78c06176 261 x86_platform.nmi_init();
bbc2ff6a 262
0cefa5b9
MS
263 /* enable local interrupts */
264 local_irq_enable();
265
35f720c5
JP
266 /* to prevent fake stack check failure in clock setup */
267 boot_init_stack_canary();
0cefa5b9 268
736decac 269 x86_cpuinit.setup_percpu_clockev();
bbc2ff6a
GOC
270
271 wmb();
7d1a9417 272 cpu_startup_entry(CPUHP_ONLINE);
bbc2ff6a
GOC
273}
274
30106c17
FY
275void __init smp_store_boot_cpu_info(void)
276{
277 int id = 0; /* CPU 0 */
278 struct cpuinfo_x86 *c = &cpu_data(id);
279
280 *c = boot_cpu_data;
281 c->cpu_index = id;
282}
283
1d89a7f0
GOC
284/*
285 * The bootstrap kernel entry code has set these up. Save them for
286 * a given CPU
287 */
148f9bb8 288void smp_store_cpu_info(int id)
1d89a7f0
GOC
289{
290 struct cpuinfo_x86 *c = &cpu_data(id);
291
b3d7336d 292 *c = boot_cpu_data;
1d89a7f0 293 c->cpu_index = id;
30106c17
FY
294 /*
295 * During boot time, CPU0 has this setup already. Save the info when
296 * bringing up AP or offlined CPU0.
297 */
298 identify_secondary_cpu(c);
1d89a7f0
GOC
299}
300
cebf15eb
DH
301static bool
302topology_same_node(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
303{
304 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
305
306 return (cpu_to_node(cpu1) == cpu_to_node(cpu2));
307}
308
148f9bb8 309static bool
316ad248 310topology_sane(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o, const char *name)
d4fbe4f0 311{
316ad248
PZ
312 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
313
cebf15eb 314 return !WARN_ONCE(!topology_same_node(c, o),
316ad248
PZ
315 "sched: CPU #%d's %s-sibling CPU #%d is not on the same node! "
316 "[node: %d != %d]. Ignoring dependency.\n",
317 cpu1, name, cpu2, cpu_to_node(cpu1), cpu_to_node(cpu2));
318}
319
320#define link_mask(_m, c1, c2) \
321do { \
322 cpumask_set_cpu((c1), cpu_##_m##_mask(c2)); \
323 cpumask_set_cpu((c2), cpu_##_m##_mask(c1)); \
324} while (0)
325
148f9bb8 326static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
316ad248 327{
193f3fcb 328 if (cpu_has_topoext) {
316ad248
PZ
329 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
330
331 if (c->phys_proc_id == o->phys_proc_id &&
332 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2) &&
333 c->compute_unit_id == o->compute_unit_id)
334 return topology_sane(c, o, "smt");
335
336 } else if (c->phys_proc_id == o->phys_proc_id &&
337 c->cpu_core_id == o->cpu_core_id) {
338 return topology_sane(c, o, "smt");
339 }
340
341 return false;
342}
343
148f9bb8 344static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
316ad248
PZ
345{
346 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
347
348 if (per_cpu(cpu_llc_id, cpu1) != BAD_APICID &&
349 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2))
350 return topology_sane(c, o, "llc");
351
352 return false;
d4fbe4f0
AH
353}
354
cebf15eb
DH
355/*
356 * Unlike the other levels, we do not enforce keeping a
357 * multicore group inside a NUMA node. If this happens, we will
358 * discard the MC level of the topology later.
359 */
360static bool match_die(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
316ad248 361{
cebf15eb
DH
362 if (c->phys_proc_id == o->phys_proc_id)
363 return true;
316ad248
PZ
364 return false;
365}
1d89a7f0 366
cebf15eb
DH
367static struct sched_domain_topology_level numa_inside_package_topology[] = {
368#ifdef CONFIG_SCHED_SMT
369 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
370#endif
371#ifdef CONFIG_SCHED_MC
372 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
373#endif
374 { NULL, },
375};
376/*
377 * set_sched_topology() sets the topology internal to a CPU. The
378 * NUMA topologies are layered on top of it to build the full
379 * system topology.
380 *
381 * If NUMA nodes are observed to occur within a CPU package, this
382 * function should be called. It forces the sched domain code to
383 * only use the SMT level for the CPU portion of the topology.
384 * This essentially falls back to relying on NUMA information
385 * from the SRAT table to describe the entire system topology
386 * (except for hyperthreads).
387 */
388static void primarily_use_numa_for_topology(void)
389{
390 set_sched_topology(numa_inside_package_topology);
391}
392
148f9bb8 393void set_cpu_sibling_map(int cpu)
768d9505 394{
316ad248 395 bool has_smt = smp_num_siblings > 1;
b0bc225d 396 bool has_mp = has_smt || boot_cpu_data.x86_max_cores > 1;
768d9505 397 struct cpuinfo_x86 *c = &cpu_data(cpu);
316ad248
PZ
398 struct cpuinfo_x86 *o;
399 int i;
768d9505 400
c2d1cec1 401 cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
768d9505 402
b0bc225d 403 if (!has_mp) {
c2d1cec1 404 cpumask_set_cpu(cpu, cpu_sibling_mask(cpu));
316ad248
PZ
405 cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
406 cpumask_set_cpu(cpu, cpu_core_mask(cpu));
768d9505
GC
407 c->booted_cores = 1;
408 return;
409 }
410
c2d1cec1 411 for_each_cpu(i, cpu_sibling_setup_mask) {
316ad248
PZ
412 o = &cpu_data(i);
413
414 if ((i == cpu) || (has_smt && match_smt(c, o)))
415 link_mask(sibling, cpu, i);
416
b0bc225d 417 if ((i == cpu) || (has_mp && match_llc(c, o)))
316ad248
PZ
418 link_mask(llc_shared, cpu, i);
419
ceb1cbac
KB
420 }
421
422 /*
423 * This needs a separate iteration over the cpus because we rely on all
424 * cpu_sibling_mask links to be set-up.
425 */
426 for_each_cpu(i, cpu_sibling_setup_mask) {
427 o = &cpu_data(i);
428
cebf15eb 429 if ((i == cpu) || (has_mp && match_die(c, o))) {
316ad248
PZ
430 link_mask(core, cpu, i);
431
768d9505
GC
432 /*
433 * Does this new cpu bringup a new core?
434 */
c2d1cec1 435 if (cpumask_weight(cpu_sibling_mask(cpu)) == 1) {
768d9505
GC
436 /*
437 * for each core in package, increment
438 * the booted_cores for this new cpu
439 */
c2d1cec1 440 if (cpumask_first(cpu_sibling_mask(i)) == i)
768d9505
GC
441 c->booted_cores++;
442 /*
443 * increment the core count for all
444 * the other cpus in this package
445 */
446 if (i != cpu)
447 cpu_data(i).booted_cores++;
448 } else if (i != cpu && !c->booted_cores)
449 c->booted_cores = cpu_data(i).booted_cores;
450 }
728e5653 451 if (match_die(c, o) && !topology_same_node(c, o))
cebf15eb 452 primarily_use_numa_for_topology();
768d9505
GC
453 }
454}
455
70708a18 456/* maps the cpu to the sched domain representing multi-core */
030bb203 457const struct cpumask *cpu_coregroup_mask(int cpu)
70708a18 458{
9f646389 459 return cpu_llc_shared_mask(cpu);
030bb203
RR
460}
461
a4928cff 462static void impress_friends(void)
904541e2
GOC
463{
464 int cpu;
465 unsigned long bogosum = 0;
466 /*
467 * Allow the user to impress friends.
468 */
c767a54b 469 pr_debug("Before bogomips\n");
904541e2 470 for_each_possible_cpu(cpu)
c2d1cec1 471 if (cpumask_test_cpu(cpu, cpu_callout_mask))
904541e2 472 bogosum += cpu_data(cpu).loops_per_jiffy;
c767a54b 473 pr_info("Total of %d processors activated (%lu.%02lu BogoMIPS)\n",
f68e00a3 474 num_online_cpus(),
904541e2
GOC
475 bogosum/(500000/HZ),
476 (bogosum/(5000/HZ))%100);
477
c767a54b 478 pr_debug("Before bogocount - setting activated=1\n");
904541e2
GOC
479}
480
569712b2 481void __inquire_remote_apic(int apicid)
cb3c8b90
GOC
482{
483 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
a6c23905 484 const char * const names[] = { "ID", "VERSION", "SPIV" };
cb3c8b90
GOC
485 int timeout;
486 u32 status;
487
c767a54b 488 pr_info("Inquiring remote APIC 0x%x...\n", apicid);
cb3c8b90
GOC
489
490 for (i = 0; i < ARRAY_SIZE(regs); i++) {
c767a54b 491 pr_info("... APIC 0x%x %s: ", apicid, names[i]);
cb3c8b90
GOC
492
493 /*
494 * Wait for idle.
495 */
496 status = safe_apic_wait_icr_idle();
497 if (status)
c767a54b 498 pr_cont("a previous APIC delivery may have failed\n");
cb3c8b90 499
1b374e4d 500 apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
cb3c8b90
GOC
501
502 timeout = 0;
503 do {
504 udelay(100);
505 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
506 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
507
508 switch (status) {
509 case APIC_ICR_RR_VALID:
510 status = apic_read(APIC_RRR);
c767a54b 511 pr_cont("%08x\n", status);
cb3c8b90
GOC
512 break;
513 default:
c767a54b 514 pr_cont("failed\n");
cb3c8b90
GOC
515 }
516 }
517}
518
cb3c8b90
GOC
519/*
520 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
521 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
522 * won't ... remember to clear down the APIC, etc later.
523 */
148f9bb8 524int
e1c467e6 525wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip)
cb3c8b90
GOC
526{
527 unsigned long send_status, accept_status = 0;
528 int maxlvt;
529
530 /* Target chip */
cb3c8b90
GOC
531 /* Boot on the stack */
532 /* Kick the second */
e1c467e6 533 apic_icr_write(APIC_DM_NMI | apic->dest_logical, apicid);
cb3c8b90 534
cfc1b9a6 535 pr_debug("Waiting for send to finish...\n");
cb3c8b90
GOC
536 send_status = safe_apic_wait_icr_idle();
537
538 /*
539 * Give the other CPU some time to accept the IPI.
540 */
541 udelay(200);
569712b2 542 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
59ef48a5
CG
543 maxlvt = lapic_get_maxlvt();
544 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
545 apic_write(APIC_ESR, 0);
546 accept_status = (apic_read(APIC_ESR) & 0xEF);
547 }
c767a54b 548 pr_debug("NMI sent\n");
cb3c8b90
GOC
549
550 if (send_status)
c767a54b 551 pr_err("APIC never delivered???\n");
cb3c8b90 552 if (accept_status)
c767a54b 553 pr_err("APIC delivery error (%lx)\n", accept_status);
cb3c8b90
GOC
554
555 return (send_status | accept_status);
556}
cb3c8b90 557
148f9bb8 558static int
569712b2 559wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
cb3c8b90
GOC
560{
561 unsigned long send_status, accept_status = 0;
562 int maxlvt, num_starts, j;
563
593f4a78
MR
564 maxlvt = lapic_get_maxlvt();
565
cb3c8b90
GOC
566 /*
567 * Be paranoid about clearing APIC errors.
568 */
569 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
593f4a78
MR
570 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
571 apic_write(APIC_ESR, 0);
cb3c8b90
GOC
572 apic_read(APIC_ESR);
573 }
574
c767a54b 575 pr_debug("Asserting INIT\n");
cb3c8b90
GOC
576
577 /*
578 * Turn INIT on target chip
579 */
cb3c8b90
GOC
580 /*
581 * Send IPI
582 */
1b374e4d
SS
583 apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
584 phys_apicid);
cb3c8b90 585
cfc1b9a6 586 pr_debug("Waiting for send to finish...\n");
cb3c8b90
GOC
587 send_status = safe_apic_wait_icr_idle();
588
589 mdelay(10);
590
c767a54b 591 pr_debug("Deasserting INIT\n");
cb3c8b90
GOC
592
593 /* Target chip */
cb3c8b90 594 /* Send IPI */
1b374e4d 595 apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
cb3c8b90 596
cfc1b9a6 597 pr_debug("Waiting for send to finish...\n");
cb3c8b90
GOC
598 send_status = safe_apic_wait_icr_idle();
599
600 mb();
601 atomic_set(&init_deasserted, 1);
602
603 /*
604 * Should we send STARTUP IPIs ?
605 *
606 * Determine this based on the APIC version.
607 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
608 */
609 if (APIC_INTEGRATED(apic_version[phys_apicid]))
610 num_starts = 2;
611 else
612 num_starts = 0;
613
614 /*
615 * Paravirt / VMI wants a startup IPI hook here to set up the
616 * target processor state.
617 */
618 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
11d4c3f9 619 stack_start);
cb3c8b90
GOC
620
621 /*
622 * Run STARTUP IPI loop.
623 */
c767a54b 624 pr_debug("#startup loops: %d\n", num_starts);
cb3c8b90 625
cb3c8b90 626 for (j = 1; j <= num_starts; j++) {
c767a54b 627 pr_debug("Sending STARTUP #%d\n", j);
593f4a78
MR
628 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
629 apic_write(APIC_ESR, 0);
cb3c8b90 630 apic_read(APIC_ESR);
c767a54b 631 pr_debug("After apic_write\n");
cb3c8b90
GOC
632
633 /*
634 * STARTUP IPI
635 */
636
637 /* Target chip */
cb3c8b90
GOC
638 /* Boot on the stack */
639 /* Kick the second */
1b374e4d
SS
640 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
641 phys_apicid);
cb3c8b90
GOC
642
643 /*
644 * Give the other CPU some time to accept the IPI.
645 */
646 udelay(300);
647
c767a54b 648 pr_debug("Startup point 1\n");
cb3c8b90 649
cfc1b9a6 650 pr_debug("Waiting for send to finish...\n");
cb3c8b90
GOC
651 send_status = safe_apic_wait_icr_idle();
652
653 /*
654 * Give the other CPU some time to accept the IPI.
655 */
656 udelay(200);
593f4a78 657 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
cb3c8b90 658 apic_write(APIC_ESR, 0);
cb3c8b90
GOC
659 accept_status = (apic_read(APIC_ESR) & 0xEF);
660 if (send_status || accept_status)
661 break;
662 }
c767a54b 663 pr_debug("After Startup\n");
cb3c8b90
GOC
664
665 if (send_status)
c767a54b 666 pr_err("APIC never delivered???\n");
cb3c8b90 667 if (accept_status)
c767a54b 668 pr_err("APIC delivery error (%lx)\n", accept_status);
cb3c8b90
GOC
669
670 return (send_status | accept_status);
671}
cb3c8b90 672
a17bce4d
BP
673void smp_announce(void)
674{
675 int num_nodes = num_online_nodes();
676
677 printk(KERN_INFO "x86: Booted up %d node%s, %d CPUs\n",
678 num_nodes, (num_nodes > 1 ? "s" : ""), num_online_cpus());
679}
680
2eaad1fd 681/* reduce the number of lines printed when booting a large cpu count system */
148f9bb8 682static void announce_cpu(int cpu, int apicid)
2eaad1fd
MT
683{
684 static int current_node = -1;
4adc8b71 685 int node = early_cpu_to_node(cpu);
a17bce4d 686 static int width, node_width;
646e29a1
BP
687
688 if (!width)
689 width = num_digits(num_possible_cpus()) + 1; /* + '#' sign */
2eaad1fd 690
a17bce4d
BP
691 if (!node_width)
692 node_width = num_digits(num_possible_nodes()) + 1; /* + '#' */
693
694 if (cpu == 1)
695 printk(KERN_INFO "x86: Booting SMP configuration:\n");
696
2eaad1fd
MT
697 if (system_state == SYSTEM_BOOTING) {
698 if (node != current_node) {
699 if (current_node > (-1))
a17bce4d 700 pr_cont("\n");
2eaad1fd 701 current_node = node;
a17bce4d
BP
702
703 printk(KERN_INFO ".... node %*s#%d, CPUs: ",
704 node_width - num_digits(node), " ", node);
2eaad1fd 705 }
646e29a1
BP
706
707 /* Add padding for the BSP */
708 if (cpu == 1)
709 pr_cont("%*s", width + 1, " ");
710
711 pr_cont("%*s#%d", width - num_digits(cpu), " ", cpu);
712
2eaad1fd
MT
713 } else
714 pr_info("Booting Node %d Processor %d APIC 0x%x\n",
715 node, cpu, apicid);
716}
717
e1c467e6
FY
718static int wakeup_cpu0_nmi(unsigned int cmd, struct pt_regs *regs)
719{
720 int cpu;
721
722 cpu = smp_processor_id();
723 if (cpu == 0 && !cpu_online(cpu) && enable_start_cpu0)
724 return NMI_HANDLED;
725
726 return NMI_DONE;
727}
728
729/*
730 * Wake up AP by INIT, INIT, STARTUP sequence.
731 *
732 * Instead of waiting for STARTUP after INITs, BSP will execute the BIOS
733 * boot-strap code which is not a desired behavior for waking up BSP. To
734 * void the boot-strap code, wake up CPU0 by NMI instead.
735 *
736 * This works to wake up soft offlined CPU0 only. If CPU0 is hard offlined
737 * (i.e. physically hot removed and then hot added), NMI won't wake it up.
738 * We'll change this code in the future to wake up hard offlined CPU0 if
739 * real platform and request are available.
740 */
148f9bb8 741static int
e1c467e6
FY
742wakeup_cpu_via_init_nmi(int cpu, unsigned long start_ip, int apicid,
743 int *cpu0_nmi_registered)
744{
745 int id;
746 int boot_error;
747
ea7bdc65
JK
748 preempt_disable();
749
e1c467e6
FY
750 /*
751 * Wake up AP by INIT, INIT, STARTUP sequence.
752 */
ea7bdc65
JK
753 if (cpu) {
754 boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
755 goto out;
756 }
e1c467e6
FY
757
758 /*
759 * Wake up BSP by nmi.
760 *
761 * Register a NMI handler to help wake up CPU0.
762 */
763 boot_error = register_nmi_handler(NMI_LOCAL,
764 wakeup_cpu0_nmi, 0, "wake_cpu0");
765
766 if (!boot_error) {
767 enable_start_cpu0 = 1;
768 *cpu0_nmi_registered = 1;
769 if (apic->dest_logical == APIC_DEST_LOGICAL)
770 id = cpu0_logical_apicid;
771 else
772 id = apicid;
773 boot_error = wakeup_secondary_cpu_via_nmi(id, start_ip);
774 }
ea7bdc65
JK
775
776out:
777 preempt_enable();
e1c467e6
FY
778
779 return boot_error;
780}
781
cb3c8b90
GOC
782/*
783 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
784 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
1f5bcabf
IM
785 * Returns zero if CPU booted OK, else error code from
786 * ->wakeup_secondary_cpu.
cb3c8b90 787 */
148f9bb8 788static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle)
cb3c8b90 789{
48927bbb 790 volatile u32 *trampoline_status =
b429dbf6 791 (volatile u32 *) __va(real_mode_header->trampoline_status);
48927bbb 792 /* start_ip had better be page-aligned! */
f37240f1 793 unsigned long start_ip = real_mode_header->trampoline_start;
48927bbb 794
cb3c8b90 795 unsigned long boot_error = 0;
e1c467e6 796 int cpu0_nmi_registered = 0;
ce4b1b16 797 unsigned long timeout;
cb3c8b90 798
816afe4f
RR
799 /* Just in case we booted with a single CPU. */
800 alternatives_enable_smp();
cb3c8b90 801
7eb43a6d
TG
802 idle->thread.sp = (unsigned long) (((struct pt_regs *)
803 (THREAD_SIZE + task_stack_page(idle))) - 1);
804 per_cpu(current_task, cpu) = idle;
cb3c8b90 805
c6f5e0ac 806#ifdef CONFIG_X86_32
cb3c8b90 807 /* Stack for startup_32 can be just as for start_secondary onwards */
cb3c8b90 808 irq_ctx_init(cpu);
a7fcf28d
AL
809 per_cpu(cpu_current_top_of_stack, cpu) =
810 (unsigned long)task_stack_page(idle) + THREAD_SIZE;
cb3c8b90 811#else
7eb43a6d 812 clear_tsk_thread_flag(idle, TIF_FORK);
004aa322 813 initial_gs = per_cpu_offset(cpu);
198d208d 814#endif
9af45651 815 per_cpu(kernel_stack, cpu) =
ef593260 816 (unsigned long)task_stack_page(idle) + THREAD_SIZE;
a939098a 817 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
3e970473 818 initial_code = (unsigned long)start_secondary;
7eb43a6d 819 stack_start = idle->thread.sp;
cb3c8b90 820
2eaad1fd
MT
821 /* So we see what's up */
822 announce_cpu(cpu, apicid);
cb3c8b90
GOC
823
824 /*
825 * This grunge runs the startup process for
826 * the targeted processor.
827 */
828
829 atomic_set(&init_deasserted, 0);
830
34d05591 831 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
cb3c8b90 832
cfc1b9a6 833 pr_debug("Setting warm reset code and vector.\n");
cb3c8b90 834
34d05591
JS
835 smpboot_setup_warm_reset_vector(start_ip);
836 /*
837 * Be paranoid about clearing APIC errors.
db96b0a0
CG
838 */
839 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
840 apic_write(APIC_ESR, 0);
841 apic_read(APIC_ESR);
842 }
34d05591 843 }
cb3c8b90 844
ce4b1b16
IM
845 /*
846 * AP might wait on cpu_callout_mask in cpu_init() with
847 * cpu_initialized_mask set if previous attempt to online
848 * it timed-out. Clear cpu_initialized_mask so that after
849 * INIT/SIPI it could start with a clean state.
850 */
851 cpumask_clear_cpu(cpu, cpu_initialized_mask);
852 smp_mb();
853
cb3c8b90 854 /*
e1c467e6
FY
855 * Wake up a CPU in difference cases:
856 * - Use the method in the APIC driver if it's defined
857 * Otherwise,
858 * - Use an INIT boot APIC message for APs or NMI for BSP.
cb3c8b90 859 */
1f5bcabf
IM
860 if (apic->wakeup_secondary_cpu)
861 boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
862 else
e1c467e6
FY
863 boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
864 &cpu0_nmi_registered);
cb3c8b90
GOC
865
866 if (!boot_error) {
867 /*
ce4b1b16 868 * Wait 10s total for a response from AP
cb3c8b90 869 */
ce4b1b16
IM
870 boot_error = -1;
871 timeout = jiffies + 10*HZ;
872 while (time_before(jiffies, timeout)) {
873 if (cpumask_test_cpu(cpu, cpu_initialized_mask)) {
874 /*
875 * Tell AP to proceed with initialization
876 */
877 cpumask_set_cpu(cpu, cpu_callout_mask);
878 boot_error = 0;
879 break;
880 }
881 udelay(100);
882 schedule();
883 }
884 }
cb3c8b90 885
ce4b1b16 886 if (!boot_error) {
cb3c8b90 887 /*
ce4b1b16 888 * Wait till AP completes initial initialization
cb3c8b90 889 */
ce4b1b16 890 while (!cpumask_test_cpu(cpu, cpu_callin_mask)) {
68f202e4
SS
891 /*
892 * Allow other tasks to run while we wait for the
893 * AP to come online. This also gives a chance
894 * for the MTRR work(triggered by the AP coming online)
895 * to be completed in the stop machine context.
896 */
ce4b1b16 897 udelay(100);
68f202e4 898 schedule();
cb3c8b90 899 }
cb3c8b90
GOC
900 }
901
902 /* mark "stuck" area as not stuck */
48927bbb 903 *trampoline_status = 0;
cb3c8b90 904
02421f98
YL
905 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
906 /*
907 * Cleanup possible dangling ends...
908 */
909 smpboot_restore_warm_reset_vector();
910 }
e1c467e6
FY
911 /*
912 * Clean up the nmi handler. Do this after the callin and callout sync
913 * to avoid impact of possible long unregister time.
914 */
915 if (cpu0_nmi_registered)
916 unregister_nmi_handler(NMI_LOCAL, "wake_cpu0");
917
cb3c8b90
GOC
918 return boot_error;
919}
920
148f9bb8 921int native_cpu_up(unsigned int cpu, struct task_struct *tidle)
cb3c8b90 922{
a21769a4 923 int apicid = apic->cpu_present_to_apicid(cpu);
cb3c8b90
GOC
924 unsigned long flags;
925 int err;
926
927 WARN_ON(irqs_disabled());
928
cfc1b9a6 929 pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
cb3c8b90 930
30106c17 931 if (apicid == BAD_APICID ||
c284b42a 932 !physid_isset(apicid, phys_cpu_present_map) ||
fa63030e 933 !apic->apic_id_valid(apicid)) {
c767a54b 934 pr_err("%s: bad cpu %d\n", __func__, cpu);
cb3c8b90
GOC
935 return -EINVAL;
936 }
937
938 /*
939 * Already booted CPU?
940 */
c2d1cec1 941 if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
cfc1b9a6 942 pr_debug("do_boot_cpu %d Already started\n", cpu);
cb3c8b90
GOC
943 return -ENOSYS;
944 }
945
946 /*
947 * Save current MTRR state in case it was changed since early boot
948 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
949 */
950 mtrr_save_state();
951
952 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
953
644c1541
VP
954 /* the FPU context is blank, nobody can own it */
955 __cpu_disable_lazy_restore(cpu);
956
7eb43a6d 957 err = do_boot_cpu(apicid, cpu, tidle);
61165d7a 958 if (err) {
feef1e8e 959 pr_err("do_boot_cpu failed(%d) to wakeup CPU#%u\n", err, cpu);
61165d7a 960 return -EIO;
cb3c8b90
GOC
961 }
962
963 /*
964 * Check TSC synchronization with the AP (keep irqs disabled
965 * while doing so):
966 */
967 local_irq_save(flags);
968 check_tsc_sync_source(cpu);
969 local_irq_restore(flags);
970
7c04e64a 971 while (!cpu_online(cpu)) {
cb3c8b90
GOC
972 cpu_relax();
973 touch_nmi_watchdog();
974 }
975
976 return 0;
977}
978
7167d08e
HK
979/**
980 * arch_disable_smp_support() - disables SMP support for x86 at runtime
981 */
982void arch_disable_smp_support(void)
983{
984 disable_ioapic_support();
985}
986
8aef135c
GOC
987/*
988 * Fall back to non SMP mode after errors.
989 *
990 * RED-PEN audit/test this more. I bet there is more state messed up here.
991 */
992static __init void disable_smp(void)
993{
613c25ef
TG
994 pr_info("SMP disabled\n");
995
ef4c59a4
TG
996 disable_ioapic_support();
997
4f062896
RR
998 init_cpu_present(cpumask_of(0));
999 init_cpu_possible(cpumask_of(0));
0f385d1d 1000
8aef135c 1001 if (smp_found_config)
b6df1b8b 1002 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
8aef135c 1003 else
b6df1b8b 1004 physid_set_mask_of_physid(0, &phys_cpu_present_map);
c2d1cec1
MT
1005 cpumask_set_cpu(0, cpu_sibling_mask(0));
1006 cpumask_set_cpu(0, cpu_core_mask(0));
8aef135c
GOC
1007}
1008
613c25ef
TG
1009enum {
1010 SMP_OK,
1011 SMP_NO_CONFIG,
1012 SMP_NO_APIC,
1013 SMP_FORCE_UP,
1014};
1015
8aef135c
GOC
1016/*
1017 * Various sanity checks.
1018 */
1019static int __init smp_sanity_check(unsigned max_cpus)
1020{
ac23d4ee 1021 preempt_disable();
a58f03b0 1022
1ff2f20d 1023#if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
a58f03b0
YL
1024 if (def_to_bigsmp && nr_cpu_ids > 8) {
1025 unsigned int cpu;
1026 unsigned nr;
1027
c767a54b
JP
1028 pr_warn("More than 8 CPUs detected - skipping them\n"
1029 "Use CONFIG_X86_BIGSMP\n");
a58f03b0
YL
1030
1031 nr = 0;
1032 for_each_present_cpu(cpu) {
1033 if (nr >= 8)
c2d1cec1 1034 set_cpu_present(cpu, false);
a58f03b0
YL
1035 nr++;
1036 }
1037
1038 nr = 0;
1039 for_each_possible_cpu(cpu) {
1040 if (nr >= 8)
c2d1cec1 1041 set_cpu_possible(cpu, false);
a58f03b0
YL
1042 nr++;
1043 }
1044
1045 nr_cpu_ids = 8;
1046 }
1047#endif
1048
8aef135c 1049 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
c767a54b 1050 pr_warn("weird, boot CPU (#%d) not listed by the BIOS\n",
55c395b4
MT
1051 hard_smp_processor_id());
1052
8aef135c
GOC
1053 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1054 }
1055
1056 /*
1057 * If we couldn't find an SMP configuration at boot time,
1058 * get out of here now!
1059 */
1060 if (!smp_found_config && !acpi_lapic) {
ac23d4ee 1061 preempt_enable();
c767a54b 1062 pr_notice("SMP motherboard not detected\n");
613c25ef 1063 return SMP_NO_CONFIG;
8aef135c
GOC
1064 }
1065
1066 /*
1067 * Should not be necessary because the MP table should list the boot
1068 * CPU too, but we do it for the sake of robustness anyway.
1069 */
a27a6210 1070 if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
c767a54b
JP
1071 pr_notice("weird, boot CPU (#%d) not listed by the BIOS\n",
1072 boot_cpu_physical_apicid);
8aef135c
GOC
1073 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1074 }
ac23d4ee 1075 preempt_enable();
8aef135c
GOC
1076
1077 /*
1078 * If we couldn't find a local APIC, then get out of here now!
1079 */
1080 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) &&
1081 !cpu_has_apic) {
103428e5
CG
1082 if (!disable_apic) {
1083 pr_err("BIOS bug, local APIC #%d not detected!...\n",
1084 boot_cpu_physical_apicid);
c767a54b 1085 pr_err("... forcing use of dummy APIC emulation (tell your hw vendor)\n");
103428e5 1086 }
613c25ef 1087 return SMP_NO_APIC;
8aef135c
GOC
1088 }
1089
1090 verify_local_APIC();
1091
1092 /*
1093 * If SMP should be disabled, then really disable it!
1094 */
1095 if (!max_cpus) {
c767a54b 1096 pr_info("SMP mode deactivated\n");
613c25ef 1097 return SMP_FORCE_UP;
8aef135c
GOC
1098 }
1099
613c25ef 1100 return SMP_OK;
8aef135c
GOC
1101}
1102
1103static void __init smp_cpu_index_default(void)
1104{
1105 int i;
1106 struct cpuinfo_x86 *c;
1107
7c04e64a 1108 for_each_possible_cpu(i) {
8aef135c
GOC
1109 c = &cpu_data(i);
1110 /* mark all to hotplug */
9628937d 1111 c->cpu_index = nr_cpu_ids;
8aef135c
GOC
1112 }
1113}
1114
1115/*
1116 * Prepare for SMP bootup. The MP table or ACPI has been read
1117 * earlier. Just do some sanity checking here and enable APIC mode.
1118 */
1119void __init native_smp_prepare_cpus(unsigned int max_cpus)
1120{
7ad728f9
RR
1121 unsigned int i;
1122
8aef135c 1123 smp_cpu_index_default();
792363d2 1124
8aef135c
GOC
1125 /*
1126 * Setup boot CPU information
1127 */
30106c17 1128 smp_store_boot_cpu_info(); /* Final full version of the data */
792363d2
YL
1129 cpumask_copy(cpu_callin_mask, cpumask_of(0));
1130 mb();
bd22a2f1 1131
8aef135c 1132 current_thread_info()->cpu = 0; /* needed? */
7ad728f9 1133 for_each_possible_cpu(i) {
79f55997
LZ
1134 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
1135 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
b3d7336d 1136 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
7ad728f9 1137 }
8aef135c
GOC
1138 set_cpu_sibling_map(0);
1139
613c25ef
TG
1140 switch (smp_sanity_check(max_cpus)) {
1141 case SMP_NO_CONFIG:
8aef135c 1142 disable_smp();
613c25ef
TG
1143 if (APIC_init_uniprocessor())
1144 pr_notice("Local APIC not detected. Using dummy APIC emulation.\n");
1145 return;
1146 case SMP_NO_APIC:
1147 disable_smp();
1148 return;
1149 case SMP_FORCE_UP:
1150 disable_smp();
374aab33 1151 apic_bsp_setup(false);
250a1ac6 1152 return;
613c25ef
TG
1153 case SMP_OK:
1154 break;
8aef135c
GOC
1155 }
1156
fa47f7e5
SS
1157 default_setup_apic_routing();
1158
4c9961d5 1159 if (read_apic_id() != boot_cpu_physical_apicid) {
8aef135c 1160 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
4c9961d5 1161 read_apic_id(), boot_cpu_physical_apicid);
8aef135c
GOC
1162 /* Or can we switch back to PIC here? */
1163 }
1164
374aab33 1165 cpu0_logical_apicid = apic_bsp_setup(false);
ef4c59a4 1166
c767a54b 1167 pr_info("CPU%d: ", 0);
8aef135c 1168 print_cpu_info(&cpu_data(0));
c4bd1fda
MS
1169
1170 if (is_uv_system())
1171 uv_system_init();
d0af9eed
SS
1172
1173 set_mtrr_aps_delayed_init();
8aef135c 1174}
d0af9eed
SS
1175
1176void arch_enable_nonboot_cpus_begin(void)
1177{
1178 set_mtrr_aps_delayed_init();
1179}
1180
1181void arch_enable_nonboot_cpus_end(void)
1182{
1183 mtrr_aps_init();
1184}
1185
a8db8453
GOC
1186/*
1187 * Early setup to make printk work.
1188 */
1189void __init native_smp_prepare_boot_cpu(void)
1190{
1191 int me = smp_processor_id();
552be871 1192 switch_to_new_gdt(me);
c2d1cec1
MT
1193 /* already set me in cpu_online_mask in boot_cpu_init() */
1194 cpumask_set_cpu(me, cpu_callout_mask);
a8db8453
GOC
1195 per_cpu(cpu_state, me) = CPU_ONLINE;
1196}
1197
83f7eb9c
GOC
1198void __init native_smp_cpus_done(unsigned int max_cpus)
1199{
c767a54b 1200 pr_debug("Boot done\n");
83f7eb9c 1201
99e8b9ca 1202 nmi_selftest();
83f7eb9c 1203 impress_friends();
83f7eb9c 1204 setup_ioapic_dest();
d0af9eed 1205 mtrr_aps_init();
83f7eb9c
GOC
1206}
1207
3b11ce7f
MT
1208static int __initdata setup_possible_cpus = -1;
1209static int __init _setup_possible_cpus(char *str)
1210{
1211 get_option(&str, &setup_possible_cpus);
1212 return 0;
1213}
1214early_param("possible_cpus", _setup_possible_cpus);
1215
1216
68a1c3f8 1217/*
4f062896 1218 * cpu_possible_mask should be static, it cannot change as cpu's
68a1c3f8
GC
1219 * are onlined, or offlined. The reason is per-cpu data-structures
1220 * are allocated by some modules at init time, and dont expect to
1221 * do this dynamically on cpu arrival/departure.
4f062896 1222 * cpu_present_mask on the other hand can change dynamically.
68a1c3f8
GC
1223 * In case when cpu_hotplug is not compiled, then we resort to current
1224 * behaviour, which is cpu_possible == cpu_present.
1225 * - Ashok Raj
1226 *
1227 * Three ways to find out the number of additional hotplug CPUs:
1228 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
3b11ce7f 1229 * - The user can overwrite it with possible_cpus=NUM
68a1c3f8
GC
1230 * - Otherwise don't reserve additional CPUs.
1231 * We do this because additional CPUs waste a lot of memory.
1232 * -AK
1233 */
1234__init void prefill_possible_map(void)
1235{
cb48bb59 1236 int i, possible;
68a1c3f8 1237
329513a3
YL
1238 /* no processor from mptable or madt */
1239 if (!num_processors)
1240 num_processors = 1;
1241
5f2eb550
JB
1242 i = setup_max_cpus ?: 1;
1243 if (setup_possible_cpus == -1) {
1244 possible = num_processors;
1245#ifdef CONFIG_HOTPLUG_CPU
1246 if (setup_max_cpus)
1247 possible += disabled_cpus;
1248#else
1249 if (possible > i)
1250 possible = i;
1251#endif
1252 } else
3b11ce7f
MT
1253 possible = setup_possible_cpus;
1254
730cf272
MT
1255 total_cpus = max_t(int, possible, num_processors + disabled_cpus);
1256
2b633e3f
YL
1257 /* nr_cpu_ids could be reduced via nr_cpus= */
1258 if (possible > nr_cpu_ids) {
c767a54b 1259 pr_warn("%d Processors exceeds NR_CPUS limit of %d\n",
2b633e3f
YL
1260 possible, nr_cpu_ids);
1261 possible = nr_cpu_ids;
3b11ce7f 1262 }
68a1c3f8 1263
5f2eb550
JB
1264#ifdef CONFIG_HOTPLUG_CPU
1265 if (!setup_max_cpus)
1266#endif
1267 if (possible > i) {
c767a54b 1268 pr_warn("%d Processors exceeds max_cpus limit of %u\n",
5f2eb550
JB
1269 possible, setup_max_cpus);
1270 possible = i;
1271 }
1272
c767a54b 1273 pr_info("Allowing %d CPUs, %d hotplug CPUs\n",
68a1c3f8
GC
1274 possible, max_t(int, possible - num_processors, 0));
1275
1276 for (i = 0; i < possible; i++)
c2d1cec1 1277 set_cpu_possible(i, true);
5f2eb550
JB
1278 for (; i < NR_CPUS; i++)
1279 set_cpu_possible(i, false);
3461b0af
MT
1280
1281 nr_cpu_ids = possible;
68a1c3f8 1282}
69c18c15 1283
14adf855
CE
1284#ifdef CONFIG_HOTPLUG_CPU
1285
1286static void remove_siblinginfo(int cpu)
1287{
1288 int sibling;
1289 struct cpuinfo_x86 *c = &cpu_data(cpu);
1290
c2d1cec1
MT
1291 for_each_cpu(sibling, cpu_core_mask(cpu)) {
1292 cpumask_clear_cpu(cpu, cpu_core_mask(sibling));
14adf855
CE
1293 /*/
1294 * last thread sibling in this cpu core going down
1295 */
c2d1cec1 1296 if (cpumask_weight(cpu_sibling_mask(cpu)) == 1)
14adf855
CE
1297 cpu_data(sibling).booted_cores--;
1298 }
1299
c2d1cec1
MT
1300 for_each_cpu(sibling, cpu_sibling_mask(cpu))
1301 cpumask_clear_cpu(cpu, cpu_sibling_mask(sibling));
03bd4e1f
WL
1302 for_each_cpu(sibling, cpu_llc_shared_mask(cpu))
1303 cpumask_clear_cpu(cpu, cpu_llc_shared_mask(sibling));
1304 cpumask_clear(cpu_llc_shared_mask(cpu));
c2d1cec1
MT
1305 cpumask_clear(cpu_sibling_mask(cpu));
1306 cpumask_clear(cpu_core_mask(cpu));
14adf855
CE
1307 c->phys_proc_id = 0;
1308 c->cpu_core_id = 0;
c2d1cec1 1309 cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
14adf855
CE
1310}
1311
69c18c15
GC
1312static void __ref remove_cpu_from_maps(int cpu)
1313{
c2d1cec1
MT
1314 set_cpu_online(cpu, false);
1315 cpumask_clear_cpu(cpu, cpu_callout_mask);
1316 cpumask_clear_cpu(cpu, cpu_callin_mask);
69c18c15 1317 /* was set by cpu_init() */
c2d1cec1 1318 cpumask_clear_cpu(cpu, cpu_initialized_mask);
23ca4bba 1319 numa_remove_cpu(cpu);
69c18c15
GC
1320}
1321
54279552
BO
1322static DEFINE_PER_CPU(struct completion, die_complete);
1323
8227dce7 1324void cpu_disable_common(void)
69c18c15
GC
1325{
1326 int cpu = smp_processor_id();
69c18c15 1327
54279552
BO
1328 init_completion(&per_cpu(die_complete, smp_processor_id()));
1329
69c18c15
GC
1330 remove_siblinginfo(cpu);
1331
1332 /* It's now safe to remove this processor from the online map */
d388e5fd 1333 lock_vector_lock();
69c18c15 1334 remove_cpu_from_maps(cpu);
d388e5fd 1335 unlock_vector_lock();
d7b381bb 1336 fixup_irqs();
8227dce7
AN
1337}
1338
1339int native_cpu_disable(void)
1340{
da6139e4
PB
1341 int ret;
1342
1343 ret = check_irq_vectors_for_cpu_disable();
1344 if (ret)
1345 return ret;
1346
8227dce7 1347 clear_local_APIC();
8227dce7 1348 cpu_disable_common();
2ed53c0d 1349
69c18c15
GC
1350 return 0;
1351}
1352
54279552
BO
1353void cpu_die_common(unsigned int cpu)
1354{
1355 wait_for_completion_timeout(&per_cpu(die_complete, cpu), HZ);
1356}
1357
93be71b6 1358void native_cpu_die(unsigned int cpu)
69c18c15
GC
1359{
1360 /* We don't do anything here: idle task is faking death itself. */
54279552
BO
1361
1362 cpu_die_common(cpu);
2ed53c0d
LT
1363
1364 /* They ack this in play_dead() by setting CPU_DEAD */
1365 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1366 if (system_state == SYSTEM_RUNNING)
1367 pr_info("CPU %u is now offline\n", cpu);
1368 } else {
1369 pr_err("CPU %u didn't die...\n", cpu);
69c18c15 1370 }
69c18c15 1371}
a21f5d88
AN
1372
1373void play_dead_common(void)
1374{
1375 idle_task_exit();
1376 reset_lazy_tlbstate();
02c68a02 1377 amd_e400_remove_cpu(raw_smp_processor_id());
a21f5d88
AN
1378
1379 mb();
1380 /* Ack it */
0a3aee0d 1381 __this_cpu_write(cpu_state, CPU_DEAD);
2ed53c0d 1382 complete(&per_cpu(die_complete, smp_processor_id()));
a21f5d88
AN
1383
1384 /*
1385 * With physical CPU hotplug, we should halt the cpu
1386 */
1387 local_irq_disable();
1388}
1389
e1c467e6
FY
1390static bool wakeup_cpu0(void)
1391{
1392 if (smp_processor_id() == 0 && enable_start_cpu0)
1393 return true;
1394
1395 return false;
1396}
1397
ea530692
PA
1398/*
1399 * We need to flush the caches before going to sleep, lest we have
1400 * dirty data in our caches when we come back up.
1401 */
1402static inline void mwait_play_dead(void)
1403{
1404 unsigned int eax, ebx, ecx, edx;
1405 unsigned int highest_cstate = 0;
1406 unsigned int highest_subcstate = 0;
ce5f6824 1407 void *mwait_ptr;
576cfb40 1408 int i;
ea530692 1409
69fb3676 1410 if (!this_cpu_has(X86_FEATURE_MWAIT))
ea530692 1411 return;
840d2830 1412 if (!this_cpu_has(X86_FEATURE_CLFLUSH))
ce5f6824 1413 return;
7b543a53 1414 if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
ea530692
PA
1415 return;
1416
1417 eax = CPUID_MWAIT_LEAF;
1418 ecx = 0;
1419 native_cpuid(&eax, &ebx, &ecx, &edx);
1420
1421 /*
1422 * eax will be 0 if EDX enumeration is not valid.
1423 * Initialized below to cstate, sub_cstate value when EDX is valid.
1424 */
1425 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
1426 eax = 0;
1427 } else {
1428 edx >>= MWAIT_SUBSTATE_SIZE;
1429 for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
1430 if (edx & MWAIT_SUBSTATE_MASK) {
1431 highest_cstate = i;
1432 highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
1433 }
1434 }
1435 eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
1436 (highest_subcstate - 1);
1437 }
1438
ce5f6824
PA
1439 /*
1440 * This should be a memory location in a cache line which is
1441 * unlikely to be touched by other processors. The actual
1442 * content is immaterial as it is not actually modified in any way.
1443 */
1444 mwait_ptr = &current_thread_info()->flags;
1445
a68e5c94
PA
1446 wbinvd();
1447
ea530692 1448 while (1) {
ce5f6824
PA
1449 /*
1450 * The CLFLUSH is a workaround for erratum AAI65 for
1451 * the Xeon 7400 series. It's not clear it is actually
1452 * needed, but it should be harmless in either case.
1453 * The WBINVD is insufficient due to the spurious-wakeup
1454 * case where we return around the loop.
1455 */
7d590cca 1456 mb();
ce5f6824 1457 clflush(mwait_ptr);
7d590cca 1458 mb();
ce5f6824 1459 __monitor(mwait_ptr, 0, 0);
ea530692
PA
1460 mb();
1461 __mwait(eax, 0);
e1c467e6
FY
1462 /*
1463 * If NMI wants to wake up CPU0, start CPU0.
1464 */
1465 if (wakeup_cpu0())
1466 start_cpu0();
ea530692
PA
1467 }
1468}
1469
1470static inline void hlt_play_dead(void)
1471{
7b543a53 1472 if (__this_cpu_read(cpu_info.x86) >= 4)
a68e5c94
PA
1473 wbinvd();
1474
ea530692 1475 while (1) {
ea530692 1476 native_halt();
e1c467e6
FY
1477 /*
1478 * If NMI wants to wake up CPU0, start CPU0.
1479 */
1480 if (wakeup_cpu0())
1481 start_cpu0();
ea530692
PA
1482 }
1483}
1484
a21f5d88
AN
1485void native_play_dead(void)
1486{
1487 play_dead_common();
86886e55 1488 tboot_shutdown(TB_SHUTDOWN_WFS);
ea530692
PA
1489
1490 mwait_play_dead(); /* Only returns on failure */
1a022e3f
BO
1491 if (cpuidle_play_dead())
1492 hlt_play_dead();
a21f5d88
AN
1493}
1494
69c18c15 1495#else /* ... !CONFIG_HOTPLUG_CPU */
93be71b6 1496int native_cpu_disable(void)
69c18c15
GC
1497{
1498 return -ENOSYS;
1499}
1500
93be71b6 1501void native_cpu_die(unsigned int cpu)
69c18c15
GC
1502{
1503 /* We said "no" in __cpu_disable */
1504 BUG();
1505}
a21f5d88
AN
1506
1507void native_play_dead(void)
1508{
1509 BUG();
1510}
1511
68a1c3f8 1512#endif