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