]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/smpboot.c
x86/xen: Calculate __max_logical_packages on PV domains
[mirror_ubuntu-bionic-kernel.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>
186f4360 46#include <linux/export.h>
70708a18 47#include <linux/sched.h>
105ab3d8 48#include <linux/sched/topology.h>
ef8bd77f 49#include <linux/sched/hotplug.h>
68db0cf1 50#include <linux/sched/task_stack.h>
69c18c15 51#include <linux/percpu.h>
91718e8d 52#include <linux/bootmem.h>
cb3c8b90
GOC
53#include <linux/err.h>
54#include <linux/nmi.h>
69575d38 55#include <linux/tboot.h>
35f720c5 56#include <linux/stackprotector.h>
5a0e3ad6 57#include <linux/gfp.h>
1a022e3f 58#include <linux/cpuidle.h>
69c18c15 59
8aef135c 60#include <asm/acpi.h>
cb3c8b90 61#include <asm/desc.h>
69c18c15
GC
62#include <asm/nmi.h>
63#include <asm/irq.h>
48927bbb 64#include <asm/realmode.h>
69c18c15
GC
65#include <asm/cpu.h>
66#include <asm/numa.h>
cb3c8b90
GOC
67#include <asm/pgtable.h>
68#include <asm/tlbflush.h>
69#include <asm/mtrr.h>
ea530692 70#include <asm/mwait.h>
7b6aa335 71#include <asm/apic.h>
7167d08e 72#include <asm/io_apic.h>
78f7f1e5 73#include <asm/fpu/internal.h>
569712b2 74#include <asm/setup.h>
bdbcdd48 75#include <asm/uv/uv.h>
cb3c8b90 76#include <linux/mc146818rtc.h>
b81bb373 77#include <asm/i8259.h>
48927bbb 78#include <asm/realmode.h>
646e29a1 79#include <asm/misc.h>
9043442b 80#include <asm/qspinlock.h>
48927bbb 81
a355352b
GC
82/* Number of siblings per CPU package */
83int smp_num_siblings = 1;
84EXPORT_SYMBOL(smp_num_siblings);
85
86/* Last level cache ID of each logical CPU */
0816b0f0 87DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
a355352b 88
a355352b 89/* representing HT siblings of each logical CPU */
0816b0f0 90DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
a355352b
GC
91EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
92
93/* representing HT and core siblings of each logical CPU */
0816b0f0 94DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
a355352b
GC
95EXPORT_PER_CPU_SYMBOL(cpu_core_map);
96
0816b0f0 97DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
b3d7336d 98
a355352b 99/* Per CPU bogomips and other parameters */
2c773dd3 100DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
a355352b 101EXPORT_PER_CPU_SYMBOL(cpu_info);
768d9505 102
1f12e32f 103/* Logical package management. We might want to allocate that dynamically */
1f12e32f
TG
104unsigned int __max_logical_packages __read_mostly;
105EXPORT_SYMBOL(__max_logical_packages);
7b0501b1 106static unsigned int logical_packages __read_mostly;
1f12e32f 107
70b8301f 108/* Maximum number of SMT threads on any online core */
947134d9 109int __read_mostly __max_smt_threads = 1;
70b8301f 110
7d25127c
TC
111/* Flag to indicate if a complete sched domain rebuild is required */
112bool x86_topology_update;
113
114int arch_update_cpu_topology(void)
115{
116 int retval = x86_topology_update;
117
118 x86_topology_update = false;
119 return retval;
120}
121
f77aa308
TG
122static inline void smpboot_setup_warm_reset_vector(unsigned long start_eip)
123{
124 unsigned long flags;
125
126 spin_lock_irqsave(&rtc_lock, flags);
127 CMOS_WRITE(0xa, 0xf);
128 spin_unlock_irqrestore(&rtc_lock, flags);
f77aa308
TG
129 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_HIGH)) =
130 start_eip >> 4;
f77aa308
TG
131 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) =
132 start_eip & 0xf;
f77aa308
TG
133}
134
135static inline void smpboot_restore_warm_reset_vector(void)
136{
137 unsigned long flags;
138
f77aa308
TG
139 /*
140 * Paranoid: Set warm reset code and vector here back
141 * to default values.
142 */
143 spin_lock_irqsave(&rtc_lock, flags);
144 CMOS_WRITE(0, 0xf);
145 spin_unlock_irqrestore(&rtc_lock, flags);
146
147 *((volatile u32 *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) = 0;
148}
149
cb3c8b90 150/*
30106c17
FY
151 * Report back to the Boot Processor during boot time or to the caller processor
152 * during CPU online.
cb3c8b90 153 */
148f9bb8 154static void smp_callin(void)
cb3c8b90
GOC
155{
156 int cpuid, phys_id;
cb3c8b90
GOC
157
158 /*
159 * If waken up by an INIT in an 82489DX configuration
656bba30
LB
160 * cpu_callout_mask guarantees we don't get here before
161 * an INIT_deassert IPI reaches our local APIC, so it is
162 * now safe to touch our local APIC.
cb3c8b90 163 */
e1c467e6 164 cpuid = smp_processor_id();
cb3c8b90
GOC
165
166 /*
167 * (This works even if the APIC is not enabled.)
168 */
4c9961d5 169 phys_id = read_apic_id();
cb3c8b90
GOC
170
171 /*
172 * the boot CPU has finished the init stage and is spinning
173 * on callin_map until we finish. We are free to set up this
174 * CPU, first the APIC. (this is probably redundant on most
175 * boards)
176 */
05f7e46d 177 apic_ap_setup();
cb3c8b90 178
b565201c
JS
179 /*
180 * Save our processor parameters. Note: this information
181 * is needed for clock calibration.
182 */
183 smp_store_cpu_info(cpuid);
184
76ce7cfe
PT
185 /*
186 * The topology information must be up to date before
187 * calibrate_delay() and notify_cpu_starting().
188 */
189 set_cpu_sibling_map(raw_smp_processor_id());
190
cb3c8b90
GOC
191 /*
192 * Get our bogomips.
b565201c
JS
193 * Update loops_per_jiffy in cpu_data. Previous call to
194 * smp_store_cpu_info() stored a value that is close but not as
195 * accurate as the value just calculated.
cb3c8b90 196 */
cb3c8b90 197 calibrate_delay();
b565201c 198 cpu_data(cpuid).loops_per_jiffy = loops_per_jiffy;
cfc1b9a6 199 pr_debug("Stack at about %p\n", &cpuid);
cb3c8b90 200
5ef428c4
AK
201 wmb();
202
85257024
PZ
203 notify_cpu_starting(cpuid);
204
cb3c8b90
GOC
205 /*
206 * Allow the master to continue.
207 */
c2d1cec1 208 cpumask_set_cpu(cpuid, cpu_callin_mask);
cb3c8b90
GOC
209}
210
e1c467e6
FY
211static int cpu0_logical_apicid;
212static int enable_start_cpu0;
bbc2ff6a
GOC
213/*
214 * Activate a secondary processor.
215 */
148f9bb8 216static void notrace start_secondary(void *unused)
bbc2ff6a
GOC
217{
218 /*
c7ad5ad2
AL
219 * Don't put *anything* except direct CPU state initialization
220 * before cpu_init(), SMP booting is too fragile that we want to
221 * limit the things done here to the most necessary things.
bbc2ff6a 222 */
c7ad5ad2
AL
223 if (boot_cpu_has(X86_FEATURE_PCID))
224 __write_cr4(__read_cr4() | X86_CR4_PCIDE);
e1c467e6 225
fd89a137 226#ifdef CONFIG_X86_32
b40827fa 227 /* switch away from the initial page table */
fd89a137
JR
228 load_cr3(swapper_pg_dir);
229 __flush_tlb_all();
230#endif
55d2d0ad 231 load_current_idt();
4ba55e65
AL
232 cpu_init();
233 x86_cpuinit.early_percpu_clock_init();
234 preempt_disable();
235 smp_callin();
236
237 enable_start_cpu0 = 0;
238
bbc2ff6a
GOC
239 /* otherwise gcc will move up smp_processor_id before the cpu_init */
240 barrier();
241 /*
a1652bb8 242 * Check TSC synchronization with the boot CPU:
bbc2ff6a
GOC
243 */
244 check_tsc_sync_target();
245
bbc2ff6a 246 /*
8ed4f3e6
TG
247 * Lock vector_lock, set CPU online and bring the vector
248 * allocator online. Online must be set with vector_lock held
249 * to prevent a concurrent irq setup/teardown from seeing a
250 * half valid vector space.
bbc2ff6a 251 */
d388e5fd 252 lock_vector_lock();
c2d1cec1 253 set_cpu_online(smp_processor_id(), true);
8ed4f3e6 254 lapic_online();
d388e5fd 255 unlock_vector_lock();
2a442c9c 256 cpu_set_state_online(smp_processor_id());
78c06176 257 x86_platform.nmi_init();
bbc2ff6a 258
0cefa5b9
MS
259 /* enable local interrupts */
260 local_irq_enable();
261
35f720c5
JP
262 /* to prevent fake stack check failure in clock setup */
263 boot_init_stack_canary();
0cefa5b9 264
736decac 265 x86_cpuinit.setup_percpu_clockev();
bbc2ff6a
GOC
266
267 wmb();
fc6d73d6 268 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
bbc2ff6a
GOC
269}
270
30bb9811
AK
271/**
272 * topology_phys_to_logical_pkg - Map a physical package id to a logical
273 *
274 * Returns logical package id or -1 if not found
275 */
276int topology_phys_to_logical_pkg(unsigned int phys_pkg)
277{
278 int cpu;
279
280 for_each_possible_cpu(cpu) {
281 struct cpuinfo_x86 *c = &cpu_data(cpu);
282
283 if (c->initialized && c->phys_proc_id == phys_pkg)
284 return c->logical_proc_id;
285 }
286 return -1;
287}
288EXPORT_SYMBOL(topology_phys_to_logical_pkg);
289
9d85eb91
TG
290/**
291 * topology_update_package_map - Update the physical to logical package map
292 * @pkg: The physical package id as retrieved via CPUID
293 * @cpu: The cpu for which this is updated
294 */
295int topology_update_package_map(unsigned int pkg, unsigned int cpu)
1f12e32f 296{
30bb9811 297 int new;
1f12e32f 298
30bb9811
AK
299 /* Already available somewhere? */
300 new = topology_phys_to_logical_pkg(pkg);
301 if (new >= 0)
1f12e32f
TG
302 goto found;
303
7b0501b1 304 new = logical_packages++;
9d85eb91
TG
305 if (new != pkg) {
306 pr_info("CPU %u Converting physical %u to logical package %u\n",
307 cpu, pkg, new);
308 }
1f12e32f 309found:
30bb9811 310 cpu_data(cpu).logical_proc_id = new;
1f12e32f
TG
311 return 0;
312}
313
30106c17
FY
314void __init smp_store_boot_cpu_info(void)
315{
316 int id = 0; /* CPU 0 */
317 struct cpuinfo_x86 *c = &cpu_data(id);
318
319 *c = boot_cpu_data;
320 c->cpu_index = id;
b4c0a732 321 topology_update_package_map(c->phys_proc_id, id);
30bb9811 322 c->initialized = true;
30106c17
FY
323}
324
1d89a7f0
GOC
325/*
326 * The bootstrap kernel entry code has set these up. Save them for
327 * a given CPU
328 */
148f9bb8 329void smp_store_cpu_info(int id)
1d89a7f0
GOC
330{
331 struct cpuinfo_x86 *c = &cpu_data(id);
332
30bb9811
AK
333 /* Copy boot_cpu_data only on the first bringup */
334 if (!c->initialized)
335 *c = boot_cpu_data;
1d89a7f0 336 c->cpu_index = id;
30106c17
FY
337 /*
338 * During boot time, CPU0 has this setup already. Save the info when
339 * bringing up AP or offlined CPU0.
340 */
341 identify_secondary_cpu(c);
30bb9811 342 c->initialized = true;
1d89a7f0
GOC
343}
344
cebf15eb
DH
345static bool
346topology_same_node(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
347{
348 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
349
350 return (cpu_to_node(cpu1) == cpu_to_node(cpu2));
351}
352
148f9bb8 353static bool
316ad248 354topology_sane(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o, const char *name)
d4fbe4f0 355{
316ad248
PZ
356 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
357
cebf15eb 358 return !WARN_ONCE(!topology_same_node(c, o),
316ad248
PZ
359 "sched: CPU #%d's %s-sibling CPU #%d is not on the same node! "
360 "[node: %d != %d]. Ignoring dependency.\n",
361 cpu1, name, cpu2, cpu_to_node(cpu1), cpu_to_node(cpu2));
362}
363
7d79a7bd 364#define link_mask(mfunc, c1, c2) \
316ad248 365do { \
7d79a7bd
BG
366 cpumask_set_cpu((c1), mfunc(c2)); \
367 cpumask_set_cpu((c2), mfunc(c1)); \
316ad248
PZ
368} while (0)
369
148f9bb8 370static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
316ad248 371{
362f924b 372 if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
316ad248
PZ
373 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
374
375 if (c->phys_proc_id == o->phys_proc_id &&
79a8b9aa
BP
376 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2)) {
377 if (c->cpu_core_id == o->cpu_core_id)
378 return topology_sane(c, o, "smt");
379
380 if ((c->cu_id != 0xff) &&
381 (o->cu_id != 0xff) &&
382 (c->cu_id == o->cu_id))
383 return topology_sane(c, o, "smt");
384 }
316ad248
PZ
385
386 } else if (c->phys_proc_id == o->phys_proc_id &&
387 c->cpu_core_id == o->cpu_core_id) {
388 return topology_sane(c, o, "smt");
389 }
390
391 return false;
392}
393
148f9bb8 394static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
316ad248
PZ
395{
396 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
397
398 if (per_cpu(cpu_llc_id, cpu1) != BAD_APICID &&
399 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2))
400 return topology_sane(c, o, "llc");
401
402 return false;
d4fbe4f0
AH
403}
404
cebf15eb
DH
405/*
406 * Unlike the other levels, we do not enforce keeping a
407 * multicore group inside a NUMA node. If this happens, we will
408 * discard the MC level of the topology later.
409 */
410static bool match_die(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
316ad248 411{
cebf15eb
DH
412 if (c->phys_proc_id == o->phys_proc_id)
413 return true;
316ad248
PZ
414 return false;
415}
1d89a7f0 416
d3d37d85
TC
417#if defined(CONFIG_SCHED_SMT) || defined(CONFIG_SCHED_MC)
418static inline int x86_sched_itmt_flags(void)
419{
420 return sysctl_sched_itmt_enabled ? SD_ASYM_PACKING : 0;
421}
422
423#ifdef CONFIG_SCHED_MC
424static int x86_core_flags(void)
425{
426 return cpu_core_flags() | x86_sched_itmt_flags();
427}
428#endif
429#ifdef CONFIG_SCHED_SMT
430static int x86_smt_flags(void)
431{
432 return cpu_smt_flags() | x86_sched_itmt_flags();
433}
434#endif
435#endif
436
8f37961c 437static struct sched_domain_topology_level x86_numa_in_package_topology[] = {
cebf15eb 438#ifdef CONFIG_SCHED_SMT
d3d37d85 439 { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
cebf15eb
DH
440#endif
441#ifdef CONFIG_SCHED_MC
d3d37d85 442 { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
cebf15eb
DH
443#endif
444 { NULL, },
445};
8f37961c
TC
446
447static struct sched_domain_topology_level x86_topology[] = {
448#ifdef CONFIG_SCHED_SMT
d3d37d85 449 { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
8f37961c
TC
450#endif
451#ifdef CONFIG_SCHED_MC
d3d37d85 452 { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
8f37961c
TC
453#endif
454 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
455 { NULL, },
456};
457
cebf15eb 458/*
8f37961c
TC
459 * Set if a package/die has multiple NUMA nodes inside.
460 * AMD Magny-Cours and Intel Cluster-on-Die have this.
cebf15eb 461 */
8f37961c 462static bool x86_has_numa_in_package;
cebf15eb 463
148f9bb8 464void set_cpu_sibling_map(int cpu)
768d9505 465{
316ad248 466 bool has_smt = smp_num_siblings > 1;
b0bc225d 467 bool has_mp = has_smt || boot_cpu_data.x86_max_cores > 1;
768d9505 468 struct cpuinfo_x86 *c = &cpu_data(cpu);
316ad248 469 struct cpuinfo_x86 *o;
70b8301f 470 int i, threads;
768d9505 471
c2d1cec1 472 cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
768d9505 473
b0bc225d 474 if (!has_mp) {
7d79a7bd 475 cpumask_set_cpu(cpu, topology_sibling_cpumask(cpu));
316ad248 476 cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
7d79a7bd 477 cpumask_set_cpu(cpu, topology_core_cpumask(cpu));
768d9505
GC
478 c->booted_cores = 1;
479 return;
480 }
481
c2d1cec1 482 for_each_cpu(i, cpu_sibling_setup_mask) {
316ad248
PZ
483 o = &cpu_data(i);
484
485 if ((i == cpu) || (has_smt && match_smt(c, o)))
7d79a7bd 486 link_mask(topology_sibling_cpumask, cpu, i);
316ad248 487
b0bc225d 488 if ((i == cpu) || (has_mp && match_llc(c, o)))
7d79a7bd 489 link_mask(cpu_llc_shared_mask, cpu, i);
316ad248 490
ceb1cbac
KB
491 }
492
493 /*
494 * This needs a separate iteration over the cpus because we rely on all
7d79a7bd 495 * topology_sibling_cpumask links to be set-up.
ceb1cbac
KB
496 */
497 for_each_cpu(i, cpu_sibling_setup_mask) {
498 o = &cpu_data(i);
499
cebf15eb 500 if ((i == cpu) || (has_mp && match_die(c, o))) {
7d79a7bd 501 link_mask(topology_core_cpumask, cpu, i);
316ad248 502
768d9505
GC
503 /*
504 * Does this new cpu bringup a new core?
505 */
7d79a7bd
BG
506 if (cpumask_weight(
507 topology_sibling_cpumask(cpu)) == 1) {
768d9505
GC
508 /*
509 * for each core in package, increment
510 * the booted_cores for this new cpu
511 */
7d79a7bd
BG
512 if (cpumask_first(
513 topology_sibling_cpumask(i)) == i)
768d9505
GC
514 c->booted_cores++;
515 /*
516 * increment the core count for all
517 * the other cpus in this package
518 */
519 if (i != cpu)
520 cpu_data(i).booted_cores++;
521 } else if (i != cpu && !c->booted_cores)
522 c->booted_cores = cpu_data(i).booted_cores;
523 }
728e5653 524 if (match_die(c, o) && !topology_same_node(c, o))
8f37961c 525 x86_has_numa_in_package = true;
768d9505 526 }
70b8301f
AK
527
528 threads = cpumask_weight(topology_sibling_cpumask(cpu));
529 if (threads > __max_smt_threads)
530 __max_smt_threads = threads;
768d9505
GC
531}
532
70708a18 533/* maps the cpu to the sched domain representing multi-core */
030bb203 534const struct cpumask *cpu_coregroup_mask(int cpu)
70708a18 535{
9f646389 536 return cpu_llc_shared_mask(cpu);
030bb203
RR
537}
538
a4928cff 539static void impress_friends(void)
904541e2
GOC
540{
541 int cpu;
542 unsigned long bogosum = 0;
543 /*
544 * Allow the user to impress friends.
545 */
c767a54b 546 pr_debug("Before bogomips\n");
904541e2 547 for_each_possible_cpu(cpu)
c2d1cec1 548 if (cpumask_test_cpu(cpu, cpu_callout_mask))
904541e2 549 bogosum += cpu_data(cpu).loops_per_jiffy;
c767a54b 550 pr_info("Total of %d processors activated (%lu.%02lu BogoMIPS)\n",
f68e00a3 551 num_online_cpus(),
904541e2
GOC
552 bogosum/(500000/HZ),
553 (bogosum/(5000/HZ))%100);
554
c767a54b 555 pr_debug("Before bogocount - setting activated=1\n");
904541e2
GOC
556}
557
569712b2 558void __inquire_remote_apic(int apicid)
cb3c8b90
GOC
559{
560 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
a6c23905 561 const char * const names[] = { "ID", "VERSION", "SPIV" };
cb3c8b90
GOC
562 int timeout;
563 u32 status;
564
c767a54b 565 pr_info("Inquiring remote APIC 0x%x...\n", apicid);
cb3c8b90
GOC
566
567 for (i = 0; i < ARRAY_SIZE(regs); i++) {
c767a54b 568 pr_info("... APIC 0x%x %s: ", apicid, names[i]);
cb3c8b90
GOC
569
570 /*
571 * Wait for idle.
572 */
573 status = safe_apic_wait_icr_idle();
574 if (status)
c767a54b 575 pr_cont("a previous APIC delivery may have failed\n");
cb3c8b90 576
1b374e4d 577 apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
cb3c8b90
GOC
578
579 timeout = 0;
580 do {
581 udelay(100);
582 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
583 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
584
585 switch (status) {
586 case APIC_ICR_RR_VALID:
587 status = apic_read(APIC_RRR);
c767a54b 588 pr_cont("%08x\n", status);
cb3c8b90
GOC
589 break;
590 default:
c767a54b 591 pr_cont("failed\n");
cb3c8b90
GOC
592 }
593 }
594}
595
d68921f9
LB
596/*
597 * The Multiprocessor Specification 1.4 (1997) example code suggests
598 * that there should be a 10ms delay between the BSP asserting INIT
599 * and de-asserting INIT, when starting a remote processor.
600 * But that slows boot and resume on modern processors, which include
601 * many cores and don't require that delay.
602 *
603 * Cmdline "init_cpu_udelay=" is available to over-ride this delay.
1a744cb3 604 * Modern processor families are quirked to remove the delay entirely.
d68921f9
LB
605 */
606#define UDELAY_10MS_DEFAULT 10000
607
656279a1 608static unsigned int init_udelay = UINT_MAX;
d68921f9
LB
609
610static int __init cpu_init_udelay(char *str)
611{
612 get_option(&str, &init_udelay);
613
614 return 0;
615}
616early_param("cpu_init_udelay", cpu_init_udelay);
617
1a744cb3
LB
618static void __init smp_quirk_init_udelay(void)
619{
620 /* if cmdline changed it from default, leave it alone */
656279a1 621 if (init_udelay != UINT_MAX)
1a744cb3
LB
622 return;
623
624 /* if modern processor, use no delay */
625 if (((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 6)) ||
656279a1 626 ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && (boot_cpu_data.x86 >= 0xF))) {
1a744cb3 627 init_udelay = 0;
656279a1
LB
628 return;
629 }
f1ccd249
LB
630 /* else, use legacy delay */
631 init_udelay = UDELAY_10MS_DEFAULT;
1a744cb3
LB
632}
633
cb3c8b90
GOC
634/*
635 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
636 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
637 * won't ... remember to clear down the APIC, etc later.
638 */
148f9bb8 639int
e1c467e6 640wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip)
cb3c8b90
GOC
641{
642 unsigned long send_status, accept_status = 0;
643 int maxlvt;
644
645 /* Target chip */
cb3c8b90
GOC
646 /* Boot on the stack */
647 /* Kick the second */
e1c467e6 648 apic_icr_write(APIC_DM_NMI | apic->dest_logical, apicid);
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);
cff9ab2b 657 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
59ef48a5
CG
658 maxlvt = lapic_get_maxlvt();
659 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
660 apic_write(APIC_ESR, 0);
661 accept_status = (apic_read(APIC_ESR) & 0xEF);
662 }
c767a54b 663 pr_debug("NMI sent\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
148f9bb8 673static int
569712b2 674wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
cb3c8b90 675{
f5d6a52f 676 unsigned long send_status = 0, accept_status = 0;
cb3c8b90
GOC
677 int maxlvt, num_starts, j;
678
593f4a78
MR
679 maxlvt = lapic_get_maxlvt();
680
cb3c8b90
GOC
681 /*
682 * Be paranoid about clearing APIC errors.
683 */
cff9ab2b 684 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
593f4a78
MR
685 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
686 apic_write(APIC_ESR, 0);
cb3c8b90
GOC
687 apic_read(APIC_ESR);
688 }
689
c767a54b 690 pr_debug("Asserting INIT\n");
cb3c8b90
GOC
691
692 /*
693 * Turn INIT on target chip
694 */
cb3c8b90
GOC
695 /*
696 * Send IPI
697 */
1b374e4d
SS
698 apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
699 phys_apicid);
cb3c8b90 700
cfc1b9a6 701 pr_debug("Waiting for send to finish...\n");
cb3c8b90
GOC
702 send_status = safe_apic_wait_icr_idle();
703
7cb68598 704 udelay(init_udelay);
cb3c8b90 705
c767a54b 706 pr_debug("Deasserting INIT\n");
cb3c8b90
GOC
707
708 /* Target chip */
cb3c8b90 709 /* Send IPI */
1b374e4d 710 apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
cb3c8b90 711
cfc1b9a6 712 pr_debug("Waiting for send to finish...\n");
cb3c8b90
GOC
713 send_status = safe_apic_wait_icr_idle();
714
715 mb();
cb3c8b90
GOC
716
717 /*
718 * Should we send STARTUP IPIs ?
719 *
720 * Determine this based on the APIC version.
721 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
722 */
cff9ab2b 723 if (APIC_INTEGRATED(boot_cpu_apic_version))
cb3c8b90
GOC
724 num_starts = 2;
725 else
726 num_starts = 0;
727
cb3c8b90
GOC
728 /*
729 * Run STARTUP IPI loop.
730 */
c767a54b 731 pr_debug("#startup loops: %d\n", num_starts);
cb3c8b90 732
cb3c8b90 733 for (j = 1; j <= num_starts; j++) {
c767a54b 734 pr_debug("Sending STARTUP #%d\n", j);
593f4a78
MR
735 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
736 apic_write(APIC_ESR, 0);
cb3c8b90 737 apic_read(APIC_ESR);
c767a54b 738 pr_debug("After apic_write\n");
cb3c8b90
GOC
739
740 /*
741 * STARTUP IPI
742 */
743
744 /* Target chip */
cb3c8b90
GOC
745 /* Boot on the stack */
746 /* Kick the second */
1b374e4d
SS
747 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
748 phys_apicid);
cb3c8b90
GOC
749
750 /*
751 * Give the other CPU some time to accept the IPI.
752 */
fcafddec
LB
753 if (init_udelay == 0)
754 udelay(10);
755 else
a9bcaa02 756 udelay(300);
cb3c8b90 757
c767a54b 758 pr_debug("Startup point 1\n");
cb3c8b90 759
cfc1b9a6 760 pr_debug("Waiting for send to finish...\n");
cb3c8b90
GOC
761 send_status = safe_apic_wait_icr_idle();
762
763 /*
764 * Give the other CPU some time to accept the IPI.
765 */
fcafddec
LB
766 if (init_udelay == 0)
767 udelay(10);
768 else
a9bcaa02 769 udelay(200);
cb3c8b90 770
593f4a78 771 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
cb3c8b90 772 apic_write(APIC_ESR, 0);
cb3c8b90
GOC
773 accept_status = (apic_read(APIC_ESR) & 0xEF);
774 if (send_status || accept_status)
775 break;
776 }
c767a54b 777 pr_debug("After Startup\n");
cb3c8b90
GOC
778
779 if (send_status)
c767a54b 780 pr_err("APIC never delivered???\n");
cb3c8b90 781 if (accept_status)
c767a54b 782 pr_err("APIC delivery error (%lx)\n", accept_status);
cb3c8b90
GOC
783
784 return (send_status | accept_status);
785}
cb3c8b90 786
2eaad1fd 787/* reduce the number of lines printed when booting a large cpu count system */
148f9bb8 788static void announce_cpu(int cpu, int apicid)
2eaad1fd
MT
789{
790 static int current_node = -1;
4adc8b71 791 int node = early_cpu_to_node(cpu);
a17bce4d 792 static int width, node_width;
646e29a1
BP
793
794 if (!width)
795 width = num_digits(num_possible_cpus()) + 1; /* + '#' sign */
2eaad1fd 796
a17bce4d
BP
797 if (!node_width)
798 node_width = num_digits(num_possible_nodes()) + 1; /* + '#' */
799
800 if (cpu == 1)
801 printk(KERN_INFO "x86: Booting SMP configuration:\n");
802
719b3680 803 if (system_state < SYSTEM_RUNNING) {
2eaad1fd
MT
804 if (node != current_node) {
805 if (current_node > (-1))
a17bce4d 806 pr_cont("\n");
2eaad1fd 807 current_node = node;
a17bce4d
BP
808
809 printk(KERN_INFO ".... node %*s#%d, CPUs: ",
810 node_width - num_digits(node), " ", node);
2eaad1fd 811 }
646e29a1
BP
812
813 /* Add padding for the BSP */
814 if (cpu == 1)
815 pr_cont("%*s", width + 1, " ");
816
817 pr_cont("%*s#%d", width - num_digits(cpu), " ", cpu);
818
2eaad1fd
MT
819 } else
820 pr_info("Booting Node %d Processor %d APIC 0x%x\n",
821 node, cpu, apicid);
822}
823
e1c467e6
FY
824static int wakeup_cpu0_nmi(unsigned int cmd, struct pt_regs *regs)
825{
826 int cpu;
827
828 cpu = smp_processor_id();
829 if (cpu == 0 && !cpu_online(cpu) && enable_start_cpu0)
830 return NMI_HANDLED;
831
832 return NMI_DONE;
833}
834
835/*
836 * Wake up AP by INIT, INIT, STARTUP sequence.
837 *
838 * Instead of waiting for STARTUP after INITs, BSP will execute the BIOS
839 * boot-strap code which is not a desired behavior for waking up BSP. To
840 * void the boot-strap code, wake up CPU0 by NMI instead.
841 *
842 * This works to wake up soft offlined CPU0 only. If CPU0 is hard offlined
843 * (i.e. physically hot removed and then hot added), NMI won't wake it up.
844 * We'll change this code in the future to wake up hard offlined CPU0 if
845 * real platform and request are available.
846 */
148f9bb8 847static int
e1c467e6
FY
848wakeup_cpu_via_init_nmi(int cpu, unsigned long start_ip, int apicid,
849 int *cpu0_nmi_registered)
850{
851 int id;
852 int boot_error;
853
ea7bdc65
JK
854 preempt_disable();
855
e1c467e6
FY
856 /*
857 * Wake up AP by INIT, INIT, STARTUP sequence.
858 */
ea7bdc65
JK
859 if (cpu) {
860 boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
861 goto out;
862 }
e1c467e6
FY
863
864 /*
865 * Wake up BSP by nmi.
866 *
867 * Register a NMI handler to help wake up CPU0.
868 */
869 boot_error = register_nmi_handler(NMI_LOCAL,
870 wakeup_cpu0_nmi, 0, "wake_cpu0");
871
872 if (!boot_error) {
873 enable_start_cpu0 = 1;
874 *cpu0_nmi_registered = 1;
875 if (apic->dest_logical == APIC_DEST_LOGICAL)
876 id = cpu0_logical_apicid;
877 else
878 id = apicid;
879 boot_error = wakeup_secondary_cpu_via_nmi(id, start_ip);
880 }
ea7bdc65
JK
881
882out:
883 preempt_enable();
e1c467e6
FY
884
885 return boot_error;
886}
887
3f85483b
BO
888void common_cpu_up(unsigned int cpu, struct task_struct *idle)
889{
890 /* Just in case we booted with a single CPU. */
891 alternatives_enable_smp();
892
893 per_cpu(current_task, cpu) = idle;
894
895#ifdef CONFIG_X86_32
896 /* Stack for startup_32 can be just as for start_secondary onwards */
897 irq_ctx_init(cpu);
cd493a6d 898 per_cpu(cpu_current_top_of_stack, cpu) = task_top_of_stack(idle);
3f85483b 899#else
3f85483b
BO
900 initial_gs = per_cpu_offset(cpu);
901#endif
3f85483b
BO
902}
903
cb3c8b90
GOC
904/*
905 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
906 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
1f5bcabf
IM
907 * Returns zero if CPU booted OK, else error code from
908 * ->wakeup_secondary_cpu.
cb3c8b90 909 */
10e66760
VK
910static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,
911 int *cpu0_nmi_registered)
cb3c8b90 912{
48927bbb 913 volatile u32 *trampoline_status =
b429dbf6 914 (volatile u32 *) __va(real_mode_header->trampoline_status);
48927bbb 915 /* start_ip had better be page-aligned! */
f37240f1 916 unsigned long start_ip = real_mode_header->trampoline_start;
48927bbb 917
cb3c8b90 918 unsigned long boot_error = 0;
ce4b1b16 919 unsigned long timeout;
cb3c8b90 920
b9b1a9c3 921 idle->thread.sp = (unsigned long)task_pt_regs(idle);
69218e47 922 early_gdt_descr.address = (unsigned long)get_cpu_gdt_rw(cpu);
3e970473 923 initial_code = (unsigned long)start_secondary;
b32f96c7 924 initial_stack = idle->thread.sp;
cb3c8b90 925
613e396b 926 /* Enable the espfix hack for this CPU */
20d5e4a9 927 init_espfix_ap(cpu);
20d5e4a9 928
2eaad1fd
MT
929 /* So we see what's up */
930 announce_cpu(cpu, apicid);
cb3c8b90
GOC
931
932 /*
933 * This grunge runs the startup process for
934 * the targeted processor.
935 */
936
34d05591 937 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
cb3c8b90 938
cfc1b9a6 939 pr_debug("Setting warm reset code and vector.\n");
cb3c8b90 940
34d05591
JS
941 smpboot_setup_warm_reset_vector(start_ip);
942 /*
943 * Be paranoid about clearing APIC errors.
db96b0a0 944 */
cff9ab2b 945 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
db96b0a0
CG
946 apic_write(APIC_ESR, 0);
947 apic_read(APIC_ESR);
948 }
34d05591 949 }
cb3c8b90 950
ce4b1b16
IM
951 /*
952 * AP might wait on cpu_callout_mask in cpu_init() with
953 * cpu_initialized_mask set if previous attempt to online
954 * it timed-out. Clear cpu_initialized_mask so that after
955 * INIT/SIPI it could start with a clean state.
956 */
957 cpumask_clear_cpu(cpu, cpu_initialized_mask);
958 smp_mb();
959
cb3c8b90 960 /*
e1c467e6
FY
961 * Wake up a CPU in difference cases:
962 * - Use the method in the APIC driver if it's defined
963 * Otherwise,
964 * - Use an INIT boot APIC message for APs or NMI for BSP.
cb3c8b90 965 */
1f5bcabf
IM
966 if (apic->wakeup_secondary_cpu)
967 boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
968 else
e1c467e6 969 boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
10e66760 970 cpu0_nmi_registered);
cb3c8b90
GOC
971
972 if (!boot_error) {
973 /*
6e38f1e7 974 * Wait 10s total for first sign of life from AP
cb3c8b90 975 */
ce4b1b16
IM
976 boot_error = -1;
977 timeout = jiffies + 10*HZ;
978 while (time_before(jiffies, timeout)) {
979 if (cpumask_test_cpu(cpu, cpu_initialized_mask)) {
980 /*
981 * Tell AP to proceed with initialization
982 */
983 cpumask_set_cpu(cpu, cpu_callout_mask);
984 boot_error = 0;
985 break;
986 }
ce4b1b16
IM
987 schedule();
988 }
989 }
cb3c8b90 990
ce4b1b16 991 if (!boot_error) {
cb3c8b90 992 /*
ce4b1b16 993 * Wait till AP completes initial initialization
cb3c8b90 994 */
ce4b1b16 995 while (!cpumask_test_cpu(cpu, cpu_callin_mask)) {
68f202e4
SS
996 /*
997 * Allow other tasks to run while we wait for the
998 * AP to come online. This also gives a chance
999 * for the MTRR work(triggered by the AP coming online)
1000 * to be completed in the stop machine context.
1001 */
1002 schedule();
cb3c8b90 1003 }
cb3c8b90
GOC
1004 }
1005
1006 /* mark "stuck" area as not stuck */
48927bbb 1007 *trampoline_status = 0;
cb3c8b90 1008
02421f98
YL
1009 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
1010 /*
1011 * Cleanup possible dangling ends...
1012 */
1013 smpboot_restore_warm_reset_vector();
1014 }
e1c467e6 1015
cb3c8b90
GOC
1016 return boot_error;
1017}
1018
148f9bb8 1019int native_cpu_up(unsigned int cpu, struct task_struct *tidle)
cb3c8b90 1020{
a21769a4 1021 int apicid = apic->cpu_present_to_apicid(cpu);
10e66760 1022 int cpu0_nmi_registered = 0;
cb3c8b90 1023 unsigned long flags;
10e66760 1024 int err, ret = 0;
cb3c8b90 1025
7a10e2a9 1026 lockdep_assert_irqs_enabled();
cb3c8b90 1027
cfc1b9a6 1028 pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
cb3c8b90 1029
30106c17 1030 if (apicid == BAD_APICID ||
c284b42a 1031 !physid_isset(apicid, phys_cpu_present_map) ||
fa63030e 1032 !apic->apic_id_valid(apicid)) {
c767a54b 1033 pr_err("%s: bad cpu %d\n", __func__, cpu);
cb3c8b90
GOC
1034 return -EINVAL;
1035 }
1036
1037 /*
1038 * Already booted CPU?
1039 */
c2d1cec1 1040 if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
cfc1b9a6 1041 pr_debug("do_boot_cpu %d Already started\n", cpu);
cb3c8b90
GOC
1042 return -ENOSYS;
1043 }
1044
1045 /*
1046 * Save current MTRR state in case it was changed since early boot
1047 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
1048 */
1049 mtrr_save_state();
1050
2a442c9c
PM
1051 /* x86 CPUs take themselves offline, so delayed offline is OK. */
1052 err = cpu_check_up_prepare(cpu);
1053 if (err && err != -EBUSY)
1054 return err;
cb3c8b90 1055
644c1541 1056 /* the FPU context is blank, nobody can own it */
317b622c 1057 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
644c1541 1058
3f85483b
BO
1059 common_cpu_up(cpu, tidle);
1060
10e66760 1061 err = do_boot_cpu(apicid, cpu, tidle, &cpu0_nmi_registered);
61165d7a 1062 if (err) {
feef1e8e 1063 pr_err("do_boot_cpu failed(%d) to wakeup CPU#%u\n", err, cpu);
10e66760
VK
1064 ret = -EIO;
1065 goto unreg_nmi;
cb3c8b90
GOC
1066 }
1067
1068 /*
1069 * Check TSC synchronization with the AP (keep irqs disabled
1070 * while doing so):
1071 */
1072 local_irq_save(flags);
1073 check_tsc_sync_source(cpu);
1074 local_irq_restore(flags);
1075
7c04e64a 1076 while (!cpu_online(cpu)) {
cb3c8b90
GOC
1077 cpu_relax();
1078 touch_nmi_watchdog();
1079 }
1080
10e66760
VK
1081unreg_nmi:
1082 /*
1083 * Clean up the nmi handler. Do this after the callin and callout sync
1084 * to avoid impact of possible long unregister time.
1085 */
1086 if (cpu0_nmi_registered)
1087 unregister_nmi_handler(NMI_LOCAL, "wake_cpu0");
1088
1089 return ret;
cb3c8b90
GOC
1090}
1091
7167d08e
HK
1092/**
1093 * arch_disable_smp_support() - disables SMP support for x86 at runtime
1094 */
1095void arch_disable_smp_support(void)
1096{
1097 disable_ioapic_support();
1098}
1099
8aef135c
GOC
1100/*
1101 * Fall back to non SMP mode after errors.
1102 *
1103 * RED-PEN audit/test this more. I bet there is more state messed up here.
1104 */
1105static __init void disable_smp(void)
1106{
613c25ef
TG
1107 pr_info("SMP disabled\n");
1108
ef4c59a4
TG
1109 disable_ioapic_support();
1110
4f062896
RR
1111 init_cpu_present(cpumask_of(0));
1112 init_cpu_possible(cpumask_of(0));
0f385d1d 1113
8aef135c 1114 if (smp_found_config)
b6df1b8b 1115 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
8aef135c 1116 else
b6df1b8b 1117 physid_set_mask_of_physid(0, &phys_cpu_present_map);
7d79a7bd
BG
1118 cpumask_set_cpu(0, topology_sibling_cpumask(0));
1119 cpumask_set_cpu(0, topology_core_cpumask(0));
8aef135c
GOC
1120}
1121
1122/*
1123 * Various sanity checks.
1124 */
4f45ed9f 1125static void __init smp_sanity_check(void)
8aef135c 1126{
ac23d4ee 1127 preempt_disable();
a58f03b0 1128
1ff2f20d 1129#if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
a58f03b0
YL
1130 if (def_to_bigsmp && nr_cpu_ids > 8) {
1131 unsigned int cpu;
1132 unsigned nr;
1133
c767a54b
JP
1134 pr_warn("More than 8 CPUs detected - skipping them\n"
1135 "Use CONFIG_X86_BIGSMP\n");
a58f03b0
YL
1136
1137 nr = 0;
1138 for_each_present_cpu(cpu) {
1139 if (nr >= 8)
c2d1cec1 1140 set_cpu_present(cpu, false);
a58f03b0
YL
1141 nr++;
1142 }
1143
1144 nr = 0;
1145 for_each_possible_cpu(cpu) {
1146 if (nr >= 8)
c2d1cec1 1147 set_cpu_possible(cpu, false);
a58f03b0
YL
1148 nr++;
1149 }
1150
1151 nr_cpu_ids = 8;
1152 }
1153#endif
1154
8aef135c 1155 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
c767a54b 1156 pr_warn("weird, boot CPU (#%d) not listed by the BIOS\n",
55c395b4
MT
1157 hard_smp_processor_id());
1158
8aef135c
GOC
1159 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1160 }
1161
8aef135c
GOC
1162 /*
1163 * Should not be necessary because the MP table should list the boot
1164 * CPU too, but we do it for the sake of robustness anyway.
1165 */
a27a6210 1166 if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
c767a54b
JP
1167 pr_notice("weird, boot CPU (#%d) not listed by the BIOS\n",
1168 boot_cpu_physical_apicid);
8aef135c
GOC
1169 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1170 }
ac23d4ee 1171 preempt_enable();
8aef135c
GOC
1172}
1173
1174static void __init smp_cpu_index_default(void)
1175{
1176 int i;
1177 struct cpuinfo_x86 *c;
1178
7c04e64a 1179 for_each_possible_cpu(i) {
8aef135c
GOC
1180 c = &cpu_data(i);
1181 /* mark all to hotplug */
9628937d 1182 c->cpu_index = nr_cpu_ids;
8aef135c
GOC
1183 }
1184}
1185
4b1244b4
DL
1186static void __init smp_get_logical_apicid(void)
1187{
1188 if (x2apic_mode)
1189 cpu0_logical_apicid = apic_read(APIC_LDR);
1190 else
1191 cpu0_logical_apicid = GET_APIC_LOGICAL_ID(apic_read(APIC_LDR));
1192}
1193
8aef135c 1194/*
935356ce
DL
1195 * Prepare for SMP bootup.
1196 * @max_cpus: configured maximum number of CPUs, It is a legacy parameter
1197 * for common interface support.
8aef135c
GOC
1198 */
1199void __init native_smp_prepare_cpus(unsigned int max_cpus)
1200{
7ad728f9
RR
1201 unsigned int i;
1202
8aef135c 1203 smp_cpu_index_default();
792363d2 1204
8aef135c
GOC
1205 /*
1206 * Setup boot CPU information
1207 */
30106c17 1208 smp_store_boot_cpu_info(); /* Final full version of the data */
792363d2
YL
1209 cpumask_copy(cpu_callin_mask, cpumask_of(0));
1210 mb();
bd22a2f1 1211
7ad728f9 1212 for_each_possible_cpu(i) {
79f55997
LZ
1213 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
1214 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
b3d7336d 1215 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
7ad728f9 1216 }
8f37961c
TC
1217
1218 /*
1219 * Set 'default' x86 topology, this matches default_topology() in that
1220 * it has NUMA nodes as a topology level. See also
1221 * native_smp_cpus_done().
1222 *
1223 * Must be done before set_cpus_sibling_map() is ran.
1224 */
1225 set_sched_topology(x86_topology);
1226
8aef135c
GOC
1227 set_cpu_sibling_map(0);
1228
4f45ed9f
DL
1229 smp_sanity_check();
1230
1231 switch (apic_intr_mode) {
1232 case APIC_PIC:
1233 case APIC_VIRTUAL_WIRE_NO_CONFIG:
613c25ef
TG
1234 disable_smp();
1235 return;
4f45ed9f 1236 case APIC_SYMMETRIC_IO_NO_ROUTING:
613c25ef 1237 disable_smp();
a2510d15
DL
1238 /* Setup local timer */
1239 x86_init.timers.setup_percpu_clockev();
250a1ac6 1240 return;
4f45ed9f
DL
1241 case APIC_VIRTUAL_WIRE:
1242 case APIC_SYMMETRIC_IO:
613c25ef 1243 break;
8aef135c
GOC
1244 }
1245
a2510d15
DL
1246 /* Setup local timer */
1247 x86_init.timers.setup_percpu_clockev();
8aef135c 1248
4b1244b4 1249 smp_get_logical_apicid();
ef4c59a4 1250
d54ff31d 1251 pr_info("CPU0: ");
8aef135c 1252 print_cpu_info(&cpu_data(0));
c4bd1fda 1253
ca5d376e
DL
1254 native_pv_lock_init();
1255
9ec808a0 1256 uv_system_init();
d0af9eed
SS
1257
1258 set_mtrr_aps_delayed_init();
1a744cb3
LB
1259
1260 smp_quirk_init_udelay();
8aef135c 1261}
d0af9eed
SS
1262
1263void arch_enable_nonboot_cpus_begin(void)
1264{
1265 set_mtrr_aps_delayed_init();
1266}
1267
1268void arch_enable_nonboot_cpus_end(void)
1269{
1270 mtrr_aps_init();
1271}
1272
a8db8453
GOC
1273/*
1274 * Early setup to make printk work.
1275 */
1276void __init native_smp_prepare_boot_cpu(void)
1277{
1278 int me = smp_processor_id();
552be871 1279 switch_to_new_gdt(me);
c2d1cec1
MT
1280 /* already set me in cpu_online_mask in boot_cpu_init() */
1281 cpumask_set_cpu(me, cpu_callout_mask);
2a442c9c 1282 cpu_set_state_online(me);
a8db8453
GOC
1283}
1284
c6b99a00 1285void __init calculate_max_logical_packages(void)
83f7eb9c 1286{
b4c0a732
PB
1287 int ncpus;
1288
b4c0a732
PB
1289 /*
1290 * Today neither Intel nor AMD support heterogenous systems so
1291 * extrapolate the boot cpu's data to all packages.
1292 */
947134d9 1293 ncpus = cpu_data(0).booted_cores * topology_max_smt_threads();
b4c0a732
PB
1294 __max_logical_packages = DIV_ROUND_UP(nr_cpu_ids, ncpus);
1295 pr_info("Max logical packages: %u\n", __max_logical_packages);
c6b99a00
PB
1296}
1297
1298void __init native_smp_cpus_done(unsigned int max_cpus)
1299{
1300 pr_debug("Boot done\n");
1301
1302 calculate_max_logical_packages();
83f7eb9c 1303
8f37961c
TC
1304 if (x86_has_numa_in_package)
1305 set_sched_topology(x86_numa_in_package_topology);
1306
99e8b9ca 1307 nmi_selftest();
83f7eb9c 1308 impress_friends();
d0af9eed 1309 mtrr_aps_init();
83f7eb9c
GOC
1310}
1311
3b11ce7f
MT
1312static int __initdata setup_possible_cpus = -1;
1313static int __init _setup_possible_cpus(char *str)
1314{
1315 get_option(&str, &setup_possible_cpus);
1316 return 0;
1317}
1318early_param("possible_cpus", _setup_possible_cpus);
1319
1320
68a1c3f8 1321/*
4f062896 1322 * cpu_possible_mask should be static, it cannot change as cpu's
68a1c3f8
GC
1323 * are onlined, or offlined. The reason is per-cpu data-structures
1324 * are allocated by some modules at init time, and dont expect to
1325 * do this dynamically on cpu arrival/departure.
4f062896 1326 * cpu_present_mask on the other hand can change dynamically.
68a1c3f8
GC
1327 * In case when cpu_hotplug is not compiled, then we resort to current
1328 * behaviour, which is cpu_possible == cpu_present.
1329 * - Ashok Raj
1330 *
1331 * Three ways to find out the number of additional hotplug CPUs:
1332 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
3b11ce7f 1333 * - The user can overwrite it with possible_cpus=NUM
68a1c3f8
GC
1334 * - Otherwise don't reserve additional CPUs.
1335 * We do this because additional CPUs waste a lot of memory.
1336 * -AK
1337 */
1338__init void prefill_possible_map(void)
1339{
cb48bb59 1340 int i, possible;
68a1c3f8 1341
2a51fe08
PB
1342 /* No boot processor was found in mptable or ACPI MADT */
1343 if (!num_processors) {
ff856051
VS
1344 if (boot_cpu_has(X86_FEATURE_APIC)) {
1345 int apicid = boot_cpu_physical_apicid;
1346 int cpu = hard_smp_processor_id();
2a51fe08 1347
ff856051 1348 pr_warn("Boot CPU (id %d) not listed by BIOS\n", cpu);
2a51fe08 1349
ff856051
VS
1350 /* Make sure boot cpu is enumerated */
1351 if (apic->cpu_present_to_apicid(0) == BAD_APICID &&
1352 apic->apic_id_valid(apicid))
1353 generic_processor_info(apicid, boot_cpu_apic_version);
1354 }
2a51fe08
PB
1355
1356 if (!num_processors)
1357 num_processors = 1;
1358 }
329513a3 1359
5f2eb550
JB
1360 i = setup_max_cpus ?: 1;
1361 if (setup_possible_cpus == -1) {
1362 possible = num_processors;
1363#ifdef CONFIG_HOTPLUG_CPU
1364 if (setup_max_cpus)
1365 possible += disabled_cpus;
1366#else
1367 if (possible > i)
1368 possible = i;
1369#endif
1370 } else
3b11ce7f
MT
1371 possible = setup_possible_cpus;
1372
730cf272
MT
1373 total_cpus = max_t(int, possible, num_processors + disabled_cpus);
1374
2b633e3f
YL
1375 /* nr_cpu_ids could be reduced via nr_cpus= */
1376 if (possible > nr_cpu_ids) {
9b130ad5 1377 pr_warn("%d Processors exceeds NR_CPUS limit of %u\n",
2b633e3f
YL
1378 possible, nr_cpu_ids);
1379 possible = nr_cpu_ids;
3b11ce7f 1380 }
68a1c3f8 1381
5f2eb550
JB
1382#ifdef CONFIG_HOTPLUG_CPU
1383 if (!setup_max_cpus)
1384#endif
1385 if (possible > i) {
c767a54b 1386 pr_warn("%d Processors exceeds max_cpus limit of %u\n",
5f2eb550
JB
1387 possible, setup_max_cpus);
1388 possible = i;
1389 }
1390
427d77a3
TG
1391 nr_cpu_ids = possible;
1392
c767a54b 1393 pr_info("Allowing %d CPUs, %d hotplug CPUs\n",
68a1c3f8
GC
1394 possible, max_t(int, possible - num_processors, 0));
1395
427d77a3
TG
1396 reset_cpu_possible_mask();
1397
68a1c3f8 1398 for (i = 0; i < possible; i++)
c2d1cec1 1399 set_cpu_possible(i, true);
68a1c3f8 1400}
69c18c15 1401
14adf855
CE
1402#ifdef CONFIG_HOTPLUG_CPU
1403
70b8301f
AK
1404/* Recompute SMT state for all CPUs on offline */
1405static void recompute_smt_state(void)
1406{
1407 int max_threads, cpu;
1408
1409 max_threads = 0;
1410 for_each_online_cpu (cpu) {
1411 int threads = cpumask_weight(topology_sibling_cpumask(cpu));
1412
1413 if (threads > max_threads)
1414 max_threads = threads;
1415 }
1416 __max_smt_threads = max_threads;
1417}
1418
14adf855
CE
1419static void remove_siblinginfo(int cpu)
1420{
1421 int sibling;
1422 struct cpuinfo_x86 *c = &cpu_data(cpu);
1423
7d79a7bd
BG
1424 for_each_cpu(sibling, topology_core_cpumask(cpu)) {
1425 cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
14adf855
CE
1426 /*/
1427 * last thread sibling in this cpu core going down
1428 */
7d79a7bd 1429 if (cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
14adf855
CE
1430 cpu_data(sibling).booted_cores--;
1431 }
1432
7d79a7bd
BG
1433 for_each_cpu(sibling, topology_sibling_cpumask(cpu))
1434 cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
03bd4e1f
WL
1435 for_each_cpu(sibling, cpu_llc_shared_mask(cpu))
1436 cpumask_clear_cpu(cpu, cpu_llc_shared_mask(sibling));
1437 cpumask_clear(cpu_llc_shared_mask(cpu));
7d79a7bd
BG
1438 cpumask_clear(topology_sibling_cpumask(cpu));
1439 cpumask_clear(topology_core_cpumask(cpu));
14adf855 1440 c->cpu_core_id = 0;
c2d1cec1 1441 cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
70b8301f 1442 recompute_smt_state();
14adf855
CE
1443}
1444
4daa832d 1445static void remove_cpu_from_maps(int cpu)
69c18c15 1446{
c2d1cec1
MT
1447 set_cpu_online(cpu, false);
1448 cpumask_clear_cpu(cpu, cpu_callout_mask);
1449 cpumask_clear_cpu(cpu, cpu_callin_mask);
69c18c15 1450 /* was set by cpu_init() */
c2d1cec1 1451 cpumask_clear_cpu(cpu, cpu_initialized_mask);
23ca4bba 1452 numa_remove_cpu(cpu);
69c18c15
GC
1453}
1454
8227dce7 1455void cpu_disable_common(void)
69c18c15
GC
1456{
1457 int cpu = smp_processor_id();
69c18c15 1458
69c18c15
GC
1459 remove_siblinginfo(cpu);
1460
1461 /* It's now safe to remove this processor from the online map */
d388e5fd 1462 lock_vector_lock();
69c18c15 1463 remove_cpu_from_maps(cpu);
d388e5fd 1464 unlock_vector_lock();
d7b381bb 1465 fixup_irqs();
0fa115da 1466 lapic_offline();
8227dce7
AN
1467}
1468
1469int native_cpu_disable(void)
1470{
da6139e4
PB
1471 int ret;
1472
2cffad7b 1473 ret = lapic_can_unplug_cpu();
da6139e4
PB
1474 if (ret)
1475 return ret;
1476
8227dce7 1477 clear_local_APIC();
8227dce7 1478 cpu_disable_common();
2ed53c0d 1479
69c18c15
GC
1480 return 0;
1481}
1482
2a442c9c 1483int common_cpu_die(unsigned int cpu)
54279552 1484{
2a442c9c 1485 int ret = 0;
54279552 1486
69c18c15 1487 /* We don't do anything here: idle task is faking death itself. */
54279552 1488
2ed53c0d 1489 /* They ack this in play_dead() by setting CPU_DEAD */
2a442c9c 1490 if (cpu_wait_death(cpu, 5)) {
2ed53c0d
LT
1491 if (system_state == SYSTEM_RUNNING)
1492 pr_info("CPU %u is now offline\n", cpu);
1493 } else {
1494 pr_err("CPU %u didn't die...\n", cpu);
2a442c9c 1495 ret = -1;
69c18c15 1496 }
2a442c9c
PM
1497
1498 return ret;
1499}
1500
1501void native_cpu_die(unsigned int cpu)
1502{
1503 common_cpu_die(cpu);
69c18c15 1504}
a21f5d88
AN
1505
1506void play_dead_common(void)
1507{
1508 idle_task_exit();
a21f5d88 1509
a21f5d88 1510 /* Ack it */
2a442c9c 1511 (void)cpu_report_death();
a21f5d88
AN
1512
1513 /*
1514 * With physical CPU hotplug, we should halt the cpu
1515 */
1516 local_irq_disable();
1517}
1518
e1c467e6
FY
1519static bool wakeup_cpu0(void)
1520{
1521 if (smp_processor_id() == 0 && enable_start_cpu0)
1522 return true;
1523
1524 return false;
1525}
1526
ea530692
PA
1527/*
1528 * We need to flush the caches before going to sleep, lest we have
1529 * dirty data in our caches when we come back up.
1530 */
1531static inline void mwait_play_dead(void)
1532{
1533 unsigned int eax, ebx, ecx, edx;
1534 unsigned int highest_cstate = 0;
1535 unsigned int highest_subcstate = 0;
ce5f6824 1536 void *mwait_ptr;
576cfb40 1537 int i;
ea530692 1538
69fb3676 1539 if (!this_cpu_has(X86_FEATURE_MWAIT))
ea530692 1540 return;
840d2830 1541 if (!this_cpu_has(X86_FEATURE_CLFLUSH))
ce5f6824 1542 return;
7b543a53 1543 if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
ea530692
PA
1544 return;
1545
1546 eax = CPUID_MWAIT_LEAF;
1547 ecx = 0;
1548 native_cpuid(&eax, &ebx, &ecx, &edx);
1549
1550 /*
1551 * eax will be 0 if EDX enumeration is not valid.
1552 * Initialized below to cstate, sub_cstate value when EDX is valid.
1553 */
1554 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
1555 eax = 0;
1556 } else {
1557 edx >>= MWAIT_SUBSTATE_SIZE;
1558 for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
1559 if (edx & MWAIT_SUBSTATE_MASK) {
1560 highest_cstate = i;
1561 highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
1562 }
1563 }
1564 eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
1565 (highest_subcstate - 1);
1566 }
1567
ce5f6824
PA
1568 /*
1569 * This should be a memory location in a cache line which is
1570 * unlikely to be touched by other processors. The actual
1571 * content is immaterial as it is not actually modified in any way.
1572 */
1573 mwait_ptr = &current_thread_info()->flags;
1574
a68e5c94
PA
1575 wbinvd();
1576
ea530692 1577 while (1) {
ce5f6824
PA
1578 /*
1579 * The CLFLUSH is a workaround for erratum AAI65 for
1580 * the Xeon 7400 series. It's not clear it is actually
1581 * needed, but it should be harmless in either case.
1582 * The WBINVD is insufficient due to the spurious-wakeup
1583 * case where we return around the loop.
1584 */
7d590cca 1585 mb();
ce5f6824 1586 clflush(mwait_ptr);
7d590cca 1587 mb();
ce5f6824 1588 __monitor(mwait_ptr, 0, 0);
ea530692
PA
1589 mb();
1590 __mwait(eax, 0);
e1c467e6
FY
1591 /*
1592 * If NMI wants to wake up CPU0, start CPU0.
1593 */
1594 if (wakeup_cpu0())
1595 start_cpu0();
ea530692
PA
1596 }
1597}
1598
406f992e 1599void hlt_play_dead(void)
ea530692 1600{
7b543a53 1601 if (__this_cpu_read(cpu_info.x86) >= 4)
a68e5c94
PA
1602 wbinvd();
1603
ea530692 1604 while (1) {
ea530692 1605 native_halt();
e1c467e6
FY
1606 /*
1607 * If NMI wants to wake up CPU0, start CPU0.
1608 */
1609 if (wakeup_cpu0())
1610 start_cpu0();
ea530692
PA
1611 }
1612}
1613
a21f5d88
AN
1614void native_play_dead(void)
1615{
1616 play_dead_common();
86886e55 1617 tboot_shutdown(TB_SHUTDOWN_WFS);
ea530692
PA
1618
1619 mwait_play_dead(); /* Only returns on failure */
1a022e3f
BO
1620 if (cpuidle_play_dead())
1621 hlt_play_dead();
a21f5d88
AN
1622}
1623
69c18c15 1624#else /* ... !CONFIG_HOTPLUG_CPU */
93be71b6 1625int native_cpu_disable(void)
69c18c15
GC
1626{
1627 return -ENOSYS;
1628}
1629
93be71b6 1630void native_cpu_die(unsigned int cpu)
69c18c15
GC
1631{
1632 /* We said "no" in __cpu_disable */
1633 BUG();
1634}
a21f5d88
AN
1635
1636void native_play_dead(void)
1637{
1638 BUG();
1639}
1640
68a1c3f8 1641#endif