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