blob: 3d01df7d7cf60cdbe1342fe84006405712394663 [file] [log] [blame]
Joe Perchesc767a542012-05-21 19:50:07 -07001 /*
Glauber de Oliveira Costa4cedb332008-03-19 14:26:14 -03002 * x86 SMP booting functions
3 *
Alan Cox87c6fe22009-01-05 14:08:04 +00004 * (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
Ingo Molnar8f47e162009-01-31 02:03:42 +01005 * (c) 1998, 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
Glauber de Oliveira Costa4cedb332008-03-19 14:26:14 -03006 * 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
Joe Perchesc767a542012-05-21 19:50:07 -070042#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
Glauber Costa68a1c3f2008-03-03 14:12:42 -030044#include <linux/init.h>
45#include <linux/smp.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040046#include <linux/export.h>
Glauber Costa70708a12008-03-03 14:13:03 -030047#include <linux/sched.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010048#include <linux/sched/topology.h>
Ingo Molnaref8bd772017-02-08 18:51:36 +010049#include <linux/sched/hotplug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010050#include <linux/sched/task_stack.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030051#include <linux/percpu.h>
Glauber Costa91718e82008-03-03 14:13:12 -030052#include <linux/bootmem.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030053#include <linux/err.h>
54#include <linux/nmi.h>
Shane Wang69575d32009-09-01 18:25:07 -070055#include <linux/tboot.h>
Jacob Pan35f720c2009-09-17 07:36:43 -070056#include <linux/stackprotector.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090057#include <linux/gfp.h>
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010058#include <linux/cpuidle.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030059
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -030060#include <asm/acpi.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030061#include <asm/desc.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030062#include <asm/nmi.h>
63#include <asm/irq.h>
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030064#include <asm/realmode.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030065#include <asm/cpu.h>
66#include <asm/numa.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030067#include <asm/pgtable.h>
68#include <asm/tlbflush.h>
69#include <asm/mtrr.h>
H. Peter Anvinea530692010-09-17 15:39:11 -070070#include <asm/mwait.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010071#include <asm/apic.h>
Henrik Kretzschmar7167d082011-02-22 15:38:05 +010072#include <asm/io_apic.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020073#include <asm/fpu/internal.h>
Yinghai Lu569712b2008-11-16 03:12:49 -080074#include <asm/setup.h>
Tejun Heobdbcdd42009-01-21 17:26:06 +090075#include <asm/uv/uv.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030076#include <linux/mc146818rtc.h>
Jacob Panb81bb372009-11-09 11:27:04 -080077#include <asm/i8259.h>
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030078#include <asm/realmode.h>
Borislav Petkov646e29a2013-09-27 16:35:54 +020079#include <asm/misc.h>
Juergen Gross90434422017-09-06 19:36:24 +020080#include <asm/qspinlock.h>
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030081
Glauber Costaa3553522008-03-03 14:12:58 -030082/* 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 */
Vlad Zolotarov0816b0f2012-06-11 12:56:52 +030087DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
Glauber Costaa3553522008-03-03 14:12:58 -030088
Glauber Costaa3553522008-03-03 14:12:58 -030089/* representing HT siblings of each logical CPU */
Vlad Zolotarov0816b0f2012-06-11 12:56:52 +030090DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
Glauber Costaa3553522008-03-03 14:12:58 -030091EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
92
93/* representing HT and core siblings of each logical CPU */
Vlad Zolotarov0816b0f2012-06-11 12:56:52 +030094DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
Glauber Costaa3553522008-03-03 14:12:58 -030095EXPORT_PER_CPU_SYMBOL(cpu_core_map);
96
Vlad Zolotarov0816b0f2012-06-11 12:56:52 +030097DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
Yinghai Lub3d73362011-01-21 15:29:44 -080098
Glauber Costaa3553522008-03-03 14:12:58 -030099/* Per CPU bogomips and other parameters */
Jan Beulich2c773dd2014-11-04 08:26:42 +0000100DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
Glauber Costaa3553522008-03-03 14:12:58 -0300101EXPORT_PER_CPU_SYMBOL(cpu_info);
Glauber Costa768d9502008-03-03 14:13:02 -0300102
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000103/* Logical package management. We might want to allocate that dynamically */
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000104unsigned int __max_logical_packages __read_mostly;
105EXPORT_SYMBOL(__max_logical_packages);
Jiri Olsa7b0501b2016-08-15 12:17:00 +0200106static unsigned int logical_packages __read_mostly;
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000107
Andi Kleen70b83012016-05-19 17:09:55 -0700108/* Maximum number of SMT threads on any online core */
109int __max_smt_threads __read_mostly;
110
Tim Chen7d251272016-11-22 12:23:54 -0800111/* 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
Thomas Gleixnerf77aa302015-01-15 21:22:29 +0000122static 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
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300159/*
Fenghua Yu30106c12012-11-13 11:32:41 -0800160 * Report back to the Boot Processor during boot time or to the caller processor
161 * during CPU online.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300162 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400163static void smp_callin(void)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300164{
165 int cpuid, phys_id;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300166
167 /*
168 * If waken up by an INIT in an 82489DX configuration
Len Brown656bba32015-08-16 11:45:48 -0400169 * 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.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300172 */
Fenghua Yue1c467e2012-11-14 04:36:53 -0800173 cpuid = smp_processor_id();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300174
175 /*
176 * (This works even if the APIC is not enabled.)
177 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -0700178 phys_id = read_apic_id();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300179
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 */
Thomas Gleixner05f7e462015-01-15 21:22:40 +0000186 apic_ap_setup();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300187
Suresh Siddha9d133e52010-01-29 11:42:21 -0800188 /*
Jack Steinerb5652012011-11-15 15:33:56 -0800189 * Save our processor parameters. Note: this information
190 * is needed for clock calibration.
191 */
192 smp_store_cpu_info(cpuid);
193
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300194 /*
Pavel Tatashin76ce7cf2017-10-27 20:11:00 -0400195 * 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
200 /*
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300201 * Get our bogomips.
Jack Steinerb5652012011-11-15 15:33:56 -0800202 * 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.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300205 */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300206 calibrate_delay();
Jack Steinerb5652012011-11-15 15:33:56 -0800207 cpu_data(cpuid).loops_per_jiffy = loops_per_jiffy;
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200208 pr_debug("Stack at about %p\n", &cpuid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300209
Andi Kleen5ef428c2010-11-18 11:47:31 +0100210 wmb();
211
Peter Zijlstra85257022010-03-23 19:30:52 +0100212 notify_cpu_starting(cpuid);
213
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300214 /*
215 * Allow the master to continue.
216 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800217 cpumask_set_cpu(cpuid, cpu_callin_mask);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300218}
219
Fenghua Yue1c467e2012-11-14 04:36:53 -0800220static int cpu0_logical_apicid;
221static int enable_start_cpu0;
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300222/*
223 * Activate a secondary processor.
224 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400225static void notrace start_secondary(void *unused)
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300226{
227 /*
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700228 * 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.
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300231 */
Andy Lutomirskic7ad5ad2017-09-10 17:48:27 -0700232 if (boot_cpu_has(X86_FEATURE_PCID))
233 __write_cr4(__read_cr4() | X86_CR4_PCIDE);
Fenghua Yue1c467e2012-11-14 04:36:53 -0800234
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200235#ifdef CONFIG_X86_32
236 /* switch away from the initial page table */
237 load_cr3(swapper_pg_dir);
238 __flush_tlb_all();
239#endif
240
Andy Lutomirski4ba55e62017-09-17 09:03:51 -0700241 cpu_init();
242 x86_cpuinit.early_percpu_clock_init();
243 preempt_disable();
244 smp_callin();
245
246 enable_start_cpu0 = 0;
247
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300248 /* otherwise gcc will move up smp_processor_id before the cpu_init */
249 barrier();
250 /*
Jean Delvarea1652bb2017-10-03 11:47:27 +0200251 * Check TSC synchronization with the boot CPU:
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300252 */
253 check_tsc_sync_target();
254
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300255 /*
Thomas Gleixner8ed4f3e2017-09-13 23:29:40 +0200256 * 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.
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300260 */
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700261 lock_vector_lock();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800262 set_cpu_online(smp_processor_id(), true);
Thomas Gleixner8ed4f3e2017-09-13 23:29:40 +0200263 lapic_online();
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700264 unlock_vector_lock();
Paul E. McKenney2a442c92015-02-25 11:42:15 -0800265 cpu_set_state_online(smp_processor_id());
Russ Anderson78c06172010-02-26 10:49:12 -0600266 x86_platform.nmi_init();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300267
Manfred Spraul0cefa5b2008-09-07 11:29:58 +0200268 /* enable local interrupts */
269 local_irq_enable();
270
Jacob Pan35f720c2009-09-17 07:36:43 -0700271 /* to prevent fake stack check failure in clock setup */
272 boot_init_stack_canary();
273
Thomas Gleixner736deca2009-08-19 12:35:53 +0200274 x86_cpuinit.setup_percpu_clockev();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300275
276 wmb();
Thomas Gleixnerfc6d73d2016-02-26 18:43:40 +0000277 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300278}
279
Thomas Gleixner9d85eb92016-12-12 11:04:53 +0100280/**
Andi Kleen30bb9812017-11-14 07:42:56 -0500281 * 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
299/**
Thomas Gleixner9d85eb92016-12-12 11:04:53 +0100300 * 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)
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000305{
Andi Kleen30bb9812017-11-14 07:42:56 -0500306 int new;
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000307
Andi Kleen30bb9812017-11-14 07:42:56 -0500308 /* Already available somewhere? */
309 new = topology_phys_to_logical_pkg(pkg);
310 if (new >= 0)
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000311 goto found;
312
Jiri Olsa7b0501b2016-08-15 12:17:00 +0200313 new = logical_packages++;
Thomas Gleixner9d85eb92016-12-12 11:04:53 +0100314 if (new != pkg) {
315 pr_info("CPU %u Converting physical %u to logical package %u\n",
316 cpu, pkg, new);
317 }
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000318found:
Andi Kleen30bb9812017-11-14 07:42:56 -0500319 cpu_data(cpu).logical_proc_id = new;
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000320 return 0;
321}
322
Fenghua Yu30106c12012-11-13 11:32:41 -0800323void __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;
Prarit Bhargavab4c0a732017-11-14 07:42:57 -0500330 topology_update_package_map(c->phys_proc_id, id);
Andi Kleen30bb9812017-11-14 07:42:56 -0500331 c->initialized = true;
Fenghua Yu30106c12012-11-13 11:32:41 -0800332}
333
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300334/*
335 * The bootstrap kernel entry code has set these up. Save them for
336 * a given CPU
337 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400338void smp_store_cpu_info(int id)
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300339{
340 struct cpuinfo_x86 *c = &cpu_data(id);
341
Andi Kleen30bb9812017-11-14 07:42:56 -0500342 /* Copy boot_cpu_data only on the first bringup */
343 if (!c->initialized)
344 *c = boot_cpu_data;
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300345 c->cpu_index = id;
Fenghua Yu30106c12012-11-13 11:32:41 -0800346 /*
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);
Andi Kleen30bb9812017-11-14 07:42:56 -0500351 c->initialized = true;
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300352}
353
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400354static bool
Dave Hansencebf15e2014-09-18 12:33:34 -0700355topology_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
362static bool
Peter Zijlstra316ad242012-05-11 13:05:59 +0200363topology_sane(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o, const char *name)
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200364{
Peter Zijlstra316ad242012-05-11 13:05:59 +0200365 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
366
Dave Hansencebf15e2014-09-18 12:33:34 -0700367 return !WARN_ONCE(!topology_same_node(c, o),
Peter Zijlstra316ad242012-05-11 13:05:59 +0200368 "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));
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200371}
372
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200373#define link_mask(mfunc, c1, c2) \
Peter Zijlstra316ad242012-05-11 13:05:59 +0200374do { \
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200375 cpumask_set_cpu((c1), mfunc(c2)); \
376 cpumask_set_cpu((c2), mfunc(c1)); \
Peter Zijlstra316ad242012-05-11 13:05:59 +0200377} while (0)
378
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400379static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
Peter Zijlstra316ad242012-05-11 13:05:59 +0200380{
Borislav Petkov362f9242015-12-07 10:39:41 +0100381 if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
Peter Zijlstra316ad242012-05-11 13:05:59 +0200382 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
383
384 if (c->phys_proc_id == o->phys_proc_id &&
Borislav Petkov79a8b9a2017-02-05 11:50:21 +0100385 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 }
Peter Zijlstra316ad242012-05-11 13:05:59 +0200394
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
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400403static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
Peter Zijlstra316ad242012-05-11 13:05:59 +0200404{
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;
412}
413
Dave Hansencebf15e2014-09-18 12:33:34 -0700414/*
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)
Peter Zijlstra316ad242012-05-11 13:05:59 +0200420{
Dave Hansencebf15e2014-09-18 12:33:34 -0700421 if (c->phys_proc_id == o->phys_proc_id)
422 return true;
Peter Zijlstra316ad242012-05-11 13:05:59 +0200423 return false;
424}
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300425
Tim Chend3d37d82016-11-22 12:23:57 -0800426#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
Tim Chen8f379612016-09-21 12:19:03 -0700446static struct sched_domain_topology_level x86_numa_in_package_topology[] = {
Dave Hansencebf15e2014-09-18 12:33:34 -0700447#ifdef CONFIG_SCHED_SMT
Tim Chend3d37d82016-11-22 12:23:57 -0800448 { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
Dave Hansencebf15e2014-09-18 12:33:34 -0700449#endif
450#ifdef CONFIG_SCHED_MC
Tim Chend3d37d82016-11-22 12:23:57 -0800451 { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
Dave Hansencebf15e2014-09-18 12:33:34 -0700452#endif
453 { NULL, },
454};
Tim Chen8f379612016-09-21 12:19:03 -0700455
456static struct sched_domain_topology_level x86_topology[] = {
457#ifdef CONFIG_SCHED_SMT
Tim Chend3d37d82016-11-22 12:23:57 -0800458 { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
Tim Chen8f379612016-09-21 12:19:03 -0700459#endif
460#ifdef CONFIG_SCHED_MC
Tim Chend3d37d82016-11-22 12:23:57 -0800461 { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
Tim Chen8f379612016-09-21 12:19:03 -0700462#endif
463 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
464 { NULL, },
465};
466
Dave Hansencebf15e2014-09-18 12:33:34 -0700467/*
Tim Chen8f379612016-09-21 12:19:03 -0700468 * Set if a package/die has multiple NUMA nodes inside.
469 * AMD Magny-Cours and Intel Cluster-on-Die have this.
Dave Hansencebf15e2014-09-18 12:33:34 -0700470 */
Tim Chen8f379612016-09-21 12:19:03 -0700471static bool x86_has_numa_in_package;
Dave Hansencebf15e2014-09-18 12:33:34 -0700472
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400473void set_cpu_sibling_map(int cpu)
Glauber Costa768d9502008-03-03 14:13:02 -0300474{
Peter Zijlstra316ad242012-05-11 13:05:59 +0200475 bool has_smt = smp_num_siblings > 1;
Andrew Jonesb0bc2252013-05-29 14:48:15 +0200476 bool has_mp = has_smt || boot_cpu_data.x86_max_cores > 1;
Glauber Costa768d9502008-03-03 14:13:02 -0300477 struct cpuinfo_x86 *c = &cpu_data(cpu);
Peter Zijlstra316ad242012-05-11 13:05:59 +0200478 struct cpuinfo_x86 *o;
Andi Kleen70b83012016-05-19 17:09:55 -0700479 int i, threads;
Glauber Costa768d9502008-03-03 14:13:02 -0300480
Mike Travisc2d1cec2009-01-04 05:18:03 -0800481 cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
Glauber Costa768d9502008-03-03 14:13:02 -0300482
Andrew Jonesb0bc2252013-05-29 14:48:15 +0200483 if (!has_mp) {
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200484 cpumask_set_cpu(cpu, topology_sibling_cpumask(cpu));
Peter Zijlstra316ad242012-05-11 13:05:59 +0200485 cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200486 cpumask_set_cpu(cpu, topology_core_cpumask(cpu));
Glauber Costa768d9502008-03-03 14:13:02 -0300487 c->booted_cores = 1;
488 return;
489 }
490
Mike Travisc2d1cec2009-01-04 05:18:03 -0800491 for_each_cpu(i, cpu_sibling_setup_mask) {
Peter Zijlstra316ad242012-05-11 13:05:59 +0200492 o = &cpu_data(i);
Peter Zijlstra0acbb442012-04-18 19:04:09 +0200493
Peter Zijlstra316ad242012-05-11 13:05:59 +0200494 if ((i == cpu) || (has_smt && match_smt(c, o)))
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200495 link_mask(topology_sibling_cpumask, cpu, i);
Peter Zijlstra0acbb442012-04-18 19:04:09 +0200496
Andrew Jonesb0bc2252013-05-29 14:48:15 +0200497 if ((i == cpu) || (has_mp && match_llc(c, o)))
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200498 link_mask(cpu_llc_shared_mask, cpu, i);
Peter Zijlstra316ad242012-05-11 13:05:59 +0200499
Kamalesh Babulalceb1cba2012-05-31 13:07:38 +0530500 }
501
502 /*
503 * This needs a separate iteration over the cpus because we rely on all
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200504 * topology_sibling_cpumask links to be set-up.
Kamalesh Babulalceb1cba2012-05-31 13:07:38 +0530505 */
506 for_each_cpu(i, cpu_sibling_setup_mask) {
507 o = &cpu_data(i);
508
Dave Hansencebf15e2014-09-18 12:33:34 -0700509 if ((i == cpu) || (has_mp && match_die(c, o))) {
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200510 link_mask(topology_core_cpumask, cpu, i);
Peter Zijlstra316ad242012-05-11 13:05:59 +0200511
Glauber Costa768d9502008-03-03 14:13:02 -0300512 /*
513 * Does this new cpu bringup a new core?
514 */
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200515 if (cpumask_weight(
516 topology_sibling_cpumask(cpu)) == 1) {
Glauber Costa768d9502008-03-03 14:13:02 -0300517 /*
518 * for each core in package, increment
519 * the booted_cores for this new cpu
520 */
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200521 if (cpumask_first(
522 topology_sibling_cpumask(i)) == i)
Glauber Costa768d9502008-03-03 14:13:02 -0300523 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 }
Dave Hansen728e5652014-09-30 14:45:46 -0700533 if (match_die(c, o) && !topology_same_node(c, o))
Tim Chen8f379612016-09-21 12:19:03 -0700534 x86_has_numa_in_package = true;
Glauber Costa768d9502008-03-03 14:13:02 -0300535 }
Andi Kleen70b83012016-05-19 17:09:55 -0700536
537 threads = cpumask_weight(topology_sibling_cpumask(cpu));
538 if (threads > __max_smt_threads)
539 __max_smt_threads = threads;
Glauber Costa768d9502008-03-03 14:13:02 -0300540}
541
Glauber Costa70708a12008-03-03 14:13:03 -0300542/* maps the cpu to the sched domain representing multi-core */
Rusty Russell030bb202008-12-26 22:23:41 +1030543const struct cpumask *cpu_coregroup_mask(int cpu)
Glauber Costa70708a12008-03-03 14:13:03 -0300544{
Peter Zijlstra9f646382012-05-29 16:39:09 +0200545 return cpu_llc_shared_mask(cpu);
Rusty Russell030bb202008-12-26 22:23:41 +1030546}
547
Ingo Molnara4928cf2008-04-23 13:20:56 +0200548static void impress_friends(void)
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300549{
550 int cpu;
551 unsigned long bogosum = 0;
552 /*
553 * Allow the user to impress friends.
554 */
Joe Perchesc767a542012-05-21 19:50:07 -0700555 pr_debug("Before bogomips\n");
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300556 for_each_possible_cpu(cpu)
Mike Travisc2d1cec2009-01-04 05:18:03 -0800557 if (cpumask_test_cpu(cpu, cpu_callout_mask))
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300558 bogosum += cpu_data(cpu).loops_per_jiffy;
Joe Perchesc767a542012-05-21 19:50:07 -0700559 pr_info("Total of %d processors activated (%lu.%02lu BogoMIPS)\n",
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300560 num_online_cpus(),
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300561 bogosum/(500000/HZ),
562 (bogosum/(5000/HZ))%100);
563
Joe Perchesc767a542012-05-21 19:50:07 -0700564 pr_debug("Before bogocount - setting activated=1\n");
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300565}
566
Yinghai Lu569712b2008-11-16 03:12:49 -0800567void __inquire_remote_apic(int apicid)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300568{
569 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
Greg Dietschea6c239052011-06-30 20:10:53 -0500570 const char * const names[] = { "ID", "VERSION", "SPIV" };
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300571 int timeout;
572 u32 status;
573
Joe Perchesc767a542012-05-21 19:50:07 -0700574 pr_info("Inquiring remote APIC 0x%x...\n", apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300575
576 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Joe Perchesc767a542012-05-21 19:50:07 -0700577 pr_info("... APIC 0x%x %s: ", apicid, names[i]);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300578
579 /*
580 * Wait for idle.
581 */
582 status = safe_apic_wait_icr_idle();
583 if (status)
Joe Perchesc767a542012-05-21 19:50:07 -0700584 pr_cont("a previous APIC delivery may have failed\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300585
Suresh Siddha1b374e42008-07-10 11:16:49 -0700586 apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300587
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);
Joe Perchesc767a542012-05-21 19:50:07 -0700597 pr_cont("%08x\n", status);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300598 break;
599 default:
Joe Perchesc767a542012-05-21 19:50:07 -0700600 pr_cont("failed\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300601 }
602 }
603}
604
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300605/*
Len Brownd68921f2015-05-11 17:27:09 -0400606 * 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.
Len Brown1a744cb2015-05-11 17:27:10 -0400613 * Modern processor families are quirked to remove the delay entirely.
Len Brownd68921f2015-05-11 17:27:09 -0400614 */
615#define UDELAY_10MS_DEFAULT 10000
616
Len Brown656279a2015-11-22 18:16:15 -0500617static unsigned int init_udelay = UINT_MAX;
Len Brownd68921f2015-05-11 17:27:09 -0400618
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
Len Brown1a744cb2015-05-11 17:27:10 -0400627static void __init smp_quirk_init_udelay(void)
628{
629 /* if cmdline changed it from default, leave it alone */
Len Brown656279a2015-11-22 18:16:15 -0500630 if (init_udelay != UINT_MAX)
Len Brown1a744cb2015-05-11 17:27:10 -0400631 return;
632
633 /* if modern processor, use no delay */
634 if (((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 6)) ||
Len Brown656279a2015-11-22 18:16:15 -0500635 ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && (boot_cpu_data.x86 >= 0xF))) {
Len Brown1a744cb2015-05-11 17:27:10 -0400636 init_udelay = 0;
Len Brown656279a2015-11-22 18:16:15 -0500637 return;
638 }
Len Brownf1ccd242015-10-16 00:14:28 -0400639 /* else, use legacy delay */
640 init_udelay = UDELAY_10MS_DEFAULT;
Len Brown1a744cb2015-05-11 17:27:10 -0400641}
642
Len Brownd68921f2015-05-11 17:27:09 -0400643/*
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300644 * 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 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400648int
Fenghua Yue1c467e2012-11-14 04:36:53 -0800649wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300650{
651 unsigned long send_status, accept_status = 0;
652 int maxlvt;
653
654 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300655 /* Boot on the stack */
656 /* Kick the second */
Fenghua Yue1c467e2012-11-14 04:36:53 -0800657 apic_icr_write(APIC_DM_NMI | apic->dest_logical, apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300658
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200659 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300660 send_status = safe_apic_wait_icr_idle();
661
662 /*
663 * Give the other CPU some time to accept the IPI.
664 */
665 udelay(200);
Denys Vlasenkocff9ab22016-09-13 20:12:32 +0200666 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
Cyrill Gorcunov59ef48a2008-09-14 21:58:49 +0400667 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 }
Joe Perchesc767a542012-05-21 19:50:07 -0700672 pr_debug("NMI sent\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300673
674 if (send_status)
Joe Perchesc767a542012-05-21 19:50:07 -0700675 pr_err("APIC never delivered???\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300676 if (accept_status)
Joe Perchesc767a542012-05-21 19:50:07 -0700677 pr_err("APIC delivery error (%lx)\n", accept_status);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300678
679 return (send_status | accept_status);
680}
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300681
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400682static int
Yinghai Lu569712b2008-11-16 03:12:49 -0800683wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300684{
Jan H. Schönherrf5d6a522015-05-04 11:42:34 +0200685 unsigned long send_status = 0, accept_status = 0;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300686 int maxlvt, num_starts, j;
687
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100688 maxlvt = lapic_get_maxlvt();
689
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300690 /*
691 * Be paranoid about clearing APIC errors.
692 */
Denys Vlasenkocff9ab22016-09-13 20:12:32 +0200693 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100694 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
695 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300696 apic_read(APIC_ESR);
697 }
698
Joe Perchesc767a542012-05-21 19:50:07 -0700699 pr_debug("Asserting INIT\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300700
701 /*
702 * Turn INIT on target chip
703 */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300704 /*
705 * Send IPI
706 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700707 apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
708 phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300709
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200710 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300711 send_status = safe_apic_wait_icr_idle();
712
Ingo Molnar7cb68592015-05-18 12:05:13 +0200713 udelay(init_udelay);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300714
Joe Perchesc767a542012-05-21 19:50:07 -0700715 pr_debug("Deasserting INIT\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300716
717 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300718 /* Send IPI */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700719 apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300720
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200721 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300722 send_status = safe_apic_wait_icr_idle();
723
724 mb();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300725
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 */
Denys Vlasenkocff9ab22016-09-13 20:12:32 +0200732 if (APIC_INTEGRATED(boot_cpu_apic_version))
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300733 num_starts = 2;
734 else
735 num_starts = 0;
736
737 /*
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300738 * Run STARTUP IPI loop.
739 */
Joe Perchesc767a542012-05-21 19:50:07 -0700740 pr_debug("#startup loops: %d\n", num_starts);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300741
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300742 for (j = 1; j <= num_starts; j++) {
Joe Perchesc767a542012-05-21 19:50:07 -0700743 pr_debug("Sending STARTUP #%d\n", j);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100744 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
745 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300746 apic_read(APIC_ESR);
Joe Perchesc767a542012-05-21 19:50:07 -0700747 pr_debug("After apic_write\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300748
749 /*
750 * STARTUP IPI
751 */
752
753 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300754 /* Boot on the stack */
755 /* Kick the second */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700756 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
757 phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300758
759 /*
760 * Give the other CPU some time to accept the IPI.
761 */
Len Brownfcafdde2015-10-16 00:14:29 -0400762 if (init_udelay == 0)
763 udelay(10);
764 else
Len Browna9bcaa02015-08-16 11:45:47 -0400765 udelay(300);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300766
Joe Perchesc767a542012-05-21 19:50:07 -0700767 pr_debug("Startup point 1\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300768
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200769 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300770 send_status = safe_apic_wait_icr_idle();
771
772 /*
773 * Give the other CPU some time to accept the IPI.
774 */
Len Brownfcafdde2015-10-16 00:14:29 -0400775 if (init_udelay == 0)
776 udelay(10);
777 else
Len Browna9bcaa02015-08-16 11:45:47 -0400778 udelay(200);
Jan H. Schönherrf5d6a522015-05-04 11:42:34 +0200779
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100780 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300781 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300782 accept_status = (apic_read(APIC_ESR) & 0xEF);
783 if (send_status || accept_status)
784 break;
785 }
Joe Perchesc767a542012-05-21 19:50:07 -0700786 pr_debug("After Startup\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300787
788 if (send_status)
Joe Perchesc767a542012-05-21 19:50:07 -0700789 pr_err("APIC never delivered???\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300790 if (accept_status)
Joe Perchesc767a542012-05-21 19:50:07 -0700791 pr_err("APIC delivery error (%lx)\n", accept_status);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300792
793 return (send_status | accept_status);
794}
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300795
Mike Travis2eaad1f2009-12-10 17:19:36 -0800796/* reduce the number of lines printed when booting a large cpu count system */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400797static void announce_cpu(int cpu, int apicid)
Mike Travis2eaad1f2009-12-10 17:19:36 -0800798{
799 static int current_node = -1;
Borislav Petkov4adc8b72010-06-01 21:04:55 +0200800 int node = early_cpu_to_node(cpu);
Borislav Petkova17bce42013-09-30 11:56:24 +0200801 static int width, node_width;
Borislav Petkov646e29a2013-09-27 16:35:54 +0200802
803 if (!width)
804 width = num_digits(num_possible_cpus()) + 1; /* + '#' sign */
Mike Travis2eaad1f2009-12-10 17:19:36 -0800805
Borislav Petkova17bce42013-09-30 11:56:24 +0200806 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
Thomas Gleixner719b3682017-05-16 20:42:35 +0200812 if (system_state < SYSTEM_RUNNING) {
Mike Travis2eaad1f2009-12-10 17:19:36 -0800813 if (node != current_node) {
814 if (current_node > (-1))
Borislav Petkova17bce42013-09-30 11:56:24 +0200815 pr_cont("\n");
Mike Travis2eaad1f2009-12-10 17:19:36 -0800816 current_node = node;
Borislav Petkova17bce42013-09-30 11:56:24 +0200817
818 printk(KERN_INFO ".... node %*s#%d, CPUs: ",
819 node_width - num_digits(node), " ", node);
Mike Travis2eaad1f2009-12-10 17:19:36 -0800820 }
Borislav Petkov646e29a2013-09-27 16:35:54 +0200821
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
Mike Travis2eaad1f2009-12-10 17:19:36 -0800828 } else
829 pr_info("Booting Node %d Processor %d APIC 0x%x\n",
830 node, cpu, apicid);
831}
832
Fenghua Yue1c467e2012-11-14 04:36:53 -0800833static 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 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400856static int
Fenghua Yue1c467e2012-11-14 04:36:53 -0800857wakeup_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
Jan Kiszkaea7bdc62014-01-27 20:14:06 +0100863 preempt_disable();
864
Fenghua Yue1c467e2012-11-14 04:36:53 -0800865 /*
866 * Wake up AP by INIT, INIT, STARTUP sequence.
867 */
Jan Kiszkaea7bdc62014-01-27 20:14:06 +0100868 if (cpu) {
869 boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
870 goto out;
871 }
Fenghua Yue1c467e2012-11-14 04:36:53 -0800872
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 }
890
Jan Kiszkaea7bdc62014-01-27 20:14:06 +0100891out:
892 preempt_enable();
893
Fenghua Yue1c467e2012-11-14 04:36:53 -0800894 return boot_error;
895}
896
Boris Ostrovsky3f854832015-04-01 10:12:14 -0400897void 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);
Andy Lutomirskicd493a62017-11-02 00:59:15 -0700907 per_cpu(cpu_current_top_of_stack, cpu) = task_top_of_stack(idle);
Boris Ostrovsky3f854832015-04-01 10:12:14 -0400908#else
Boris Ostrovsky3f854832015-04-01 10:12:14 -0400909 initial_gs = per_cpu_offset(cpu);
910#endif
Boris Ostrovsky3f854832015-04-01 10:12:14 -0400911}
912
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300913/*
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.
Ingo Molnar1f5bcab2009-02-26 13:51:40 +0100916 * Returns zero if CPU booted OK, else error code from
917 * ->wakeup_secondary_cpu.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300918 */
Vitaly Kuznetsov10e66762017-08-03 12:58:18 +0200919static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,
920 int *cpu0_nmi_registered)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300921{
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300922 volatile u32 *trampoline_status =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300923 (volatile u32 *) __va(real_mode_header->trampoline_status);
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300924 /* start_ip had better be page-aligned! */
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +0300925 unsigned long start_ip = real_mode_header->trampoline_start;
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300926
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300927 unsigned long boot_error = 0;
Igor Mammedovce4b1b12014-06-20 14:23:11 +0200928 unsigned long timeout;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300929
Josh Poimboeufb9b1a9c2016-09-21 16:04:03 -0500930 idle->thread.sp = (unsigned long)task_pt_regs(idle);
Thomas Garnier69218e42017-03-14 10:05:07 -0700931 early_gdt_descr.address = (unsigned long)get_cpu_gdt_rw(cpu);
Glauber Costa3e970472008-05-28 13:01:54 -0300932 initial_code = (unsigned long)start_secondary;
Josh Poimboeufb32f96c2016-08-18 10:59:03 -0500933 initial_stack = idle->thread.sp;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300934
Zhu Guihua20d5e4a2015-07-03 17:37:19 +0800935 /*
936 * Enable the espfix hack for this CPU
937 */
938#ifdef CONFIG_X86_ESPFIX64
939 init_espfix_ap(cpu);
940#endif
941
Mike Travis2eaad1f2009-12-10 17:19:36 -0800942 /* So we see what's up */
943 announce_cpu(cpu, apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300944
945 /*
946 * This grunge runs the startup process for
947 * the targeted processor.
948 */
949
Jack Steiner34d05592008-04-16 11:45:15 -0500950 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300951
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200952 pr_debug("Setting warm reset code and vector.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300953
Jack Steiner34d05592008-04-16 11:45:15 -0500954 smpboot_setup_warm_reset_vector(start_ip);
955 /*
956 * Be paranoid about clearing APIC errors.
Cyrill Gorcunovdb96b0a2008-10-22 18:00:09 +0400957 */
Denys Vlasenkocff9ab22016-09-13 20:12:32 +0200958 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
Cyrill Gorcunovdb96b0a2008-10-22 18:00:09 +0400959 apic_write(APIC_ESR, 0);
960 apic_read(APIC_ESR);
961 }
Jack Steiner34d05592008-04-16 11:45:15 -0500962 }
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300963
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300964 /*
Igor Mammedovce4b1b12014-06-20 14:23:11 +0200965 * 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
973 /*
Fenghua Yue1c467e2012-11-14 04:36:53 -0800974 * 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.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300978 */
Ingo Molnar1f5bcab2009-02-26 13:51:40 +0100979 if (apic->wakeup_secondary_cpu)
980 boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
981 else
Fenghua Yue1c467e2012-11-14 04:36:53 -0800982 boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
Vitaly Kuznetsov10e66762017-08-03 12:58:18 +0200983 cpu0_nmi_registered);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300984
985 if (!boot_error) {
986 /*
Len Brown6e38f1e2015-08-16 11:45:45 -0400987 * Wait 10s total for first sign of life from AP
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300988 */
Igor Mammedovce4b1b12014-06-20 14:23:11 +0200989 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 }
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001000 schedule();
1001 }
1002 }
1003
1004 if (!boot_error) {
1005 /*
1006 * Wait till AP completes initial initialization
1007 */
1008 while (!cpumask_test_cpu(cpu, cpu_callin_mask)) {
Suresh Siddha68f202e2010-07-30 11:46:42 -07001009 /*
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();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001016 }
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001017 }
1018
1019 /* mark "stuck" area as not stuck */
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +03001020 *trampoline_status = 0;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001021
Yinghai Lu02421f92009-04-03 17:15:53 -07001022 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
1023 /*
1024 * Cleanup possible dangling ends...
1025 */
1026 smpboot_restore_warm_reset_vector();
1027 }
Fenghua Yue1c467e2012-11-14 04:36:53 -08001028
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001029 return boot_error;
1030}
1031
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001032int native_cpu_up(unsigned int cpu, struct task_struct *tidle)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001033{
Ingo Molnara21769a42009-01-28 06:50:47 +01001034 int apicid = apic->cpu_present_to_apicid(cpu);
Vitaly Kuznetsov10e66762017-08-03 12:58:18 +02001035 int cpu0_nmi_registered = 0;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001036 unsigned long flags;
Vitaly Kuznetsov10e66762017-08-03 12:58:18 +02001037 int err, ret = 0;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001038
Frederic Weisbecker7a10e2a2017-11-06 16:01:23 +01001039 lockdep_assert_irqs_enabled();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001040
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +02001041 pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001042
Fenghua Yu30106c12012-11-13 11:32:41 -08001043 if (apicid == BAD_APICID ||
Suresh Siddhac284b422011-12-21 17:45:19 -08001044 !physid_isset(apicid, phys_cpu_present_map) ||
Daniel J Bluemanfa630302012-03-14 15:17:34 +08001045 !apic->apic_id_valid(apicid)) {
Joe Perchesc767a542012-05-21 19:50:07 -07001046 pr_err("%s: bad cpu %d\n", __func__, cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001047 return -EINVAL;
1048 }
1049
1050 /*
1051 * Already booted CPU?
1052 */
Mike Travisc2d1cec2009-01-04 05:18:03 -08001053 if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +02001054 pr_debug("do_boot_cpu %d Already started\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001055 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
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001064 /* 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;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001068
Vincent Palatin644c1542012-11-30 12:15:32 -08001069 /* the FPU context is blank, nobody can own it */
Rik van Riel317b6222016-10-14 08:15:30 -04001070 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
Vincent Palatin644c1542012-11-30 12:15:32 -08001071
Boris Ostrovsky3f854832015-04-01 10:12:14 -04001072 common_cpu_up(cpu, tidle);
1073
Vitaly Kuznetsov10e66762017-08-03 12:58:18 +02001074 err = do_boot_cpu(apicid, cpu, tidle, &cpu0_nmi_registered);
Hugh Dickins61165d72008-05-13 14:26:57 +01001075 if (err) {
Igor Mammedovfeef1e82014-06-05 15:42:44 +02001076 pr_err("do_boot_cpu failed(%d) to wakeup CPU#%u\n", err, cpu);
Vitaly Kuznetsov10e66762017-08-03 12:58:18 +02001077 ret = -EIO;
1078 goto unreg_nmi;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001079 }
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
Akinobu Mita7c04e642008-04-19 23:55:17 +09001089 while (!cpu_online(cpu)) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001090 cpu_relax();
1091 touch_nmi_watchdog();
1092 }
1093
Vitaly Kuznetsov10e66762017-08-03 12:58:18 +02001094unreg_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;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001103}
1104
Henrik Kretzschmar7167d082011-02-22 15:38:05 +01001105/**
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
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001113/*
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001114 * 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{
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001120 pr_info("SMP disabled\n");
1121
Thomas Gleixneref4c59a2015-01-15 21:22:35 +00001122 disable_ioapic_support();
1123
Rusty Russell4f062892009-03-13 14:49:54 +10301124 init_cpu_present(cpumask_of(0));
1125 init_cpu_possible(cpumask_of(0));
Glauber Costa0f385d12008-05-28 17:09:53 -07001126
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001127 if (smp_found_config)
Jack Steinerb6df1b82008-06-19 21:51:05 -05001128 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001129 else
Jack Steinerb6df1b82008-06-19 21:51:05 -05001130 physid_set_mask_of_physid(0, &phys_cpu_present_map);
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001131 cpumask_set_cpu(0, topology_sibling_cpumask(0));
1132 cpumask_set_cpu(0, topology_core_cpumask(0));
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001133}
1134
1135/*
1136 * Various sanity checks.
1137 */
Dou Liyang4f45ed92017-09-13 17:12:49 +08001138static void __init smp_sanity_check(void)
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001139{
Jack Steinerac23d4e2008-03-28 14:12:16 -05001140 preempt_disable();
Yinghai Lua58f03b2008-08-14 02:16:30 -07001141
Yinghai Lu1ff2f202009-01-29 19:30:04 -08001142#if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
Yinghai Lua58f03b2008-08-14 02:16:30 -07001143 if (def_to_bigsmp && nr_cpu_ids > 8) {
1144 unsigned int cpu;
1145 unsigned nr;
1146
Joe Perchesc767a542012-05-21 19:50:07 -07001147 pr_warn("More than 8 CPUs detected - skipping them\n"
1148 "Use CONFIG_X86_BIGSMP\n");
Yinghai Lua58f03b2008-08-14 02:16:30 -07001149
1150 nr = 0;
1151 for_each_present_cpu(cpu) {
1152 if (nr >= 8)
Mike Travisc2d1cec2009-01-04 05:18:03 -08001153 set_cpu_present(cpu, false);
Yinghai Lua58f03b2008-08-14 02:16:30 -07001154 nr++;
1155 }
1156
1157 nr = 0;
1158 for_each_possible_cpu(cpu) {
1159 if (nr >= 8)
Mike Travisc2d1cec2009-01-04 05:18:03 -08001160 set_cpu_possible(cpu, false);
Yinghai Lua58f03b2008-08-14 02:16:30 -07001161 nr++;
1162 }
1163
1164 nr_cpu_ids = 8;
1165 }
1166#endif
1167
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001168 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
Joe Perchesc767a542012-05-21 19:50:07 -07001169 pr_warn("weird, boot CPU (#%d) not listed by the BIOS\n",
Michael Tokarev55c395b2008-12-05 14:42:20 +03001170 hard_smp_processor_id());
1171
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001172 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1173 }
1174
1175 /*
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001176 * 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 */
Ingo Molnara27a6212009-01-28 12:43:18 +01001179 if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
Joe Perchesc767a542012-05-21 19:50:07 -07001180 pr_notice("weird, boot CPU (#%d) not listed by the BIOS\n",
1181 boot_cpu_physical_apicid);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001182 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1183 }
Jack Steinerac23d4e2008-03-28 14:12:16 -05001184 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001185}
1186
1187static void __init smp_cpu_index_default(void)
1188{
1189 int i;
1190 struct cpuinfo_x86 *c;
1191
Akinobu Mita7c04e642008-04-19 23:55:17 +09001192 for_each_possible_cpu(i) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001193 c = &cpu_data(i);
1194 /* mark all to hotplug */
Mike Travis96289372008-12-31 18:08:46 -08001195 c->cpu_index = nr_cpu_ids;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001196 }
1197}
1198
Dou Liyang4b1244b2017-09-13 17:12:47 +08001199static 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
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001207/*
Dou Liyang935356c2017-09-13 17:12:54 +08001208 * Prepare for SMP bootup.
1209 * @max_cpus: configured maximum number of CPUs, It is a legacy parameter
1210 * for common interface support.
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001211 */
1212void __init native_smp_prepare_cpus(unsigned int max_cpus)
1213{
Rusty Russell7ad728f2009-03-13 14:49:50 +10301214 unsigned int i;
1215
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001216 smp_cpu_index_default();
Yinghai Lu792363d2011-01-21 15:29:54 -08001217
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001218 /*
1219 * Setup boot CPU information
1220 */
Fenghua Yu30106c12012-11-13 11:32:41 -08001221 smp_store_boot_cpu_info(); /* Final full version of the data */
Yinghai Lu792363d2011-01-21 15:29:54 -08001222 cpumask_copy(cpu_callin_mask, cpumask_of(0));
1223 mb();
Tejun Heobd22a2f2011-01-23 14:37:27 +01001224
Rusty Russell7ad728f2009-03-13 14:49:50 +10301225 for_each_possible_cpu(i) {
Li Zefan79f55992009-06-15 14:58:26 +08001226 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
1227 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
Yinghai Lub3d73362011-01-21 15:29:44 -08001228 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
Rusty Russell7ad728f2009-03-13 14:49:50 +10301229 }
Tim Chen8f379612016-09-21 12:19:03 -07001230
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
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001240 set_cpu_sibling_map(0);
1241
Dou Liyang4f45ed92017-09-13 17:12:49 +08001242 smp_sanity_check();
1243
1244 switch (apic_intr_mode) {
1245 case APIC_PIC:
1246 case APIC_VIRTUAL_WIRE_NO_CONFIG:
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001247 disable_smp();
Thomas Gleixner250a1ac2014-12-05 08:48:29 +00001248 return;
Dou Liyang4f45ed92017-09-13 17:12:49 +08001249 case APIC_SYMMETRIC_IO_NO_ROUTING:
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001250 disable_smp();
Dou Liyanga2510d12017-09-13 17:12:46 +08001251 /* Setup local timer */
1252 x86_init.timers.setup_percpu_clockev();
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001253 return;
Dou Liyang4f45ed92017-09-13 17:12:49 +08001254 case APIC_VIRTUAL_WIRE:
1255 case APIC_SYMMETRIC_IO:
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001256 break;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001257 }
1258
Dou Liyanga2510d12017-09-13 17:12:46 +08001259 /* Setup local timer */
1260 x86_init.timers.setup_percpu_clockev();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001261
Dou Liyang4b1244b2017-09-13 17:12:47 +08001262 smp_get_logical_apicid();
Thomas Gleixneref4c59a2015-01-15 21:22:35 +00001263
Borislav Petkovd54ff312016-10-24 19:38:42 +02001264 pr_info("CPU0: ");
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001265 print_cpu_info(&cpu_data(0));
Marcin Slusarzc4bd1fd2008-08-21 20:49:05 +02001266
Dou Liyangca5d3762017-10-28 14:06:44 +08001267 native_pv_lock_init();
1268
travis@sgi.com9ec808a2017-01-25 10:35:23 -06001269 uv_system_init();
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001270
1271 set_mtrr_aps_delayed_init();
Len Brown1a744cb2015-05-11 17:27:10 -04001272
1273 smp_quirk_init_udelay();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001274}
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001275
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
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001286/*
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001287 * Early setup to make printk work.
1288 */
1289void __init native_smp_prepare_boot_cpu(void)
1290{
1291 int me = smp_processor_id();
Brian Gerst552be872009-01-30 17:47:53 +09001292 switch_to_new_gdt(me);
Mike Travisc2d1cec2009-01-04 05:18:03 -08001293 /* already set me in cpu_online_mask in boot_cpu_init() */
1294 cpumask_set_cpu(me, cpu_callout_mask);
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001295 cpu_set_state_online(me);
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001296}
1297
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001298void __init native_smp_cpus_done(unsigned int max_cpus)
1299{
Prarit Bhargavab4c0a732017-11-14 07:42:57 -05001300 int ncpus;
1301
Joe Perchesc767a542012-05-21 19:50:07 -07001302 pr_debug("Boot done\n");
Prarit Bhargavab4c0a732017-11-14 07:42:57 -05001303 /*
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);
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001310
Tim Chen8f379612016-09-21 12:19:03 -07001311 if (x86_has_numa_in_package)
1312 set_sched_topology(x86_numa_in_package_topology);
1313
Don Zickus99e8b9c2011-10-13 15:14:26 -04001314 nmi_selftest();
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001315 impress_friends();
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001316 mtrr_aps_init();
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001317}
1318
Mike Travis3b11ce72008-12-17 15:21:39 -08001319static 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
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001328/*
Rusty Russell4f062892009-03-13 14:49:54 +10301329 * cpu_possible_mask should be static, it cannot change as cpu's
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001330 * 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.
Rusty Russell4f062892009-03-13 14:49:54 +10301333 * cpu_present_mask on the other hand can change dynamically.
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001334 * 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.
Mike Travis3b11ce72008-12-17 15:21:39 -08001340 * - The user can overwrite it with possible_cpus=NUM
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001341 * - 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{
Thomas Gleixnercb48bb52008-10-05 17:51:52 +02001347 int i, possible;
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001348
Prarit Bhargava2a51fe02016-10-03 13:07:12 -04001349 /* No boot processor was found in mptable or ACPI MADT */
1350 if (!num_processors) {
Ville Syrjäläff856052016-10-22 05:18:04 +03001351 if (boot_cpu_has(X86_FEATURE_APIC)) {
1352 int apicid = boot_cpu_physical_apicid;
1353 int cpu = hard_smp_processor_id();
Prarit Bhargava2a51fe02016-10-03 13:07:12 -04001354
Ville Syrjäläff856052016-10-22 05:18:04 +03001355 pr_warn("Boot CPU (id %d) not listed by BIOS\n", cpu);
Prarit Bhargava2a51fe02016-10-03 13:07:12 -04001356
Ville Syrjäläff856052016-10-22 05:18:04 +03001357 /* 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 }
Prarit Bhargava2a51fe02016-10-03 13:07:12 -04001362
1363 if (!num_processors)
1364 num_processors = 1;
1365 }
Yinghai Lu329513a2008-07-02 18:54:40 -07001366
Jan Beulich5f2eb552010-05-24 12:13:17 -07001367 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
Mike Travis3b11ce72008-12-17 15:21:39 -08001378 possible = setup_possible_cpus;
1379
Mike Travis730cf272008-12-31 18:08:45 -08001380 total_cpus = max_t(int, possible, num_processors + disabled_cpus);
1381
Yinghai Lu2b633e32010-02-10 01:20:37 -08001382 /* nr_cpu_ids could be reduced via nr_cpus= */
1383 if (possible > nr_cpu_ids) {
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -07001384 pr_warn("%d Processors exceeds NR_CPUS limit of %u\n",
Yinghai Lu2b633e32010-02-10 01:20:37 -08001385 possible, nr_cpu_ids);
1386 possible = nr_cpu_ids;
Mike Travis3b11ce72008-12-17 15:21:39 -08001387 }
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001388
Jan Beulich5f2eb552010-05-24 12:13:17 -07001389#ifdef CONFIG_HOTPLUG_CPU
1390 if (!setup_max_cpus)
1391#endif
1392 if (possible > i) {
Joe Perchesc767a542012-05-21 19:50:07 -07001393 pr_warn("%d Processors exceeds max_cpus limit of %u\n",
Jan Beulich5f2eb552010-05-24 12:13:17 -07001394 possible, setup_max_cpus);
1395 possible = i;
1396 }
1397
Thomas Gleixner427d77a2016-12-13 19:32:28 +01001398 nr_cpu_ids = possible;
1399
Joe Perchesc767a542012-05-21 19:50:07 -07001400 pr_info("Allowing %d CPUs, %d hotplug CPUs\n",
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001401 possible, max_t(int, possible - num_processors, 0));
1402
Thomas Gleixner427d77a2016-12-13 19:32:28 +01001403 reset_cpu_possible_mask();
1404
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001405 for (i = 0; i < possible; i++)
Mike Travisc2d1cec2009-01-04 05:18:03 -08001406 set_cpu_possible(i, true);
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001407}
Glauber Costa69c18c12008-03-03 14:13:07 -03001408
Chuck Ebbert14adf852008-09-29 18:29:42 -04001409#ifdef CONFIG_HOTPLUG_CPU
1410
Andi Kleen70b83012016-05-19 17:09:55 -07001411/* 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
Chuck Ebbert14adf852008-09-29 18:29:42 -04001426static void remove_siblinginfo(int cpu)
1427{
1428 int sibling;
1429 struct cpuinfo_x86 *c = &cpu_data(cpu);
1430
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001431 for_each_cpu(sibling, topology_core_cpumask(cpu)) {
1432 cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
Chuck Ebbert14adf852008-09-29 18:29:42 -04001433 /*/
1434 * last thread sibling in this cpu core going down
1435 */
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001436 if (cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
Chuck Ebbert14adf852008-09-29 18:29:42 -04001437 cpu_data(sibling).booted_cores--;
1438 }
1439
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001440 for_each_cpu(sibling, topology_sibling_cpumask(cpu))
1441 cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
Wanpeng Li03bd4e12014-09-24 16:38:05 +08001442 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));
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001445 cpumask_clear(topology_sibling_cpumask(cpu));
1446 cpumask_clear(topology_core_cpumask(cpu));
Chuck Ebbert14adf852008-09-29 18:29:42 -04001447 c->phys_proc_id = 0;
1448 c->cpu_core_id = 0;
Mike Travisc2d1cec2009-01-04 05:18:03 -08001449 cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
Andi Kleen70b83012016-05-19 17:09:55 -07001450 recompute_smt_state();
Chuck Ebbert14adf852008-09-29 18:29:42 -04001451}
1452
Mathias Krause4daa8322015-07-20 18:32:53 +02001453static void remove_cpu_from_maps(int cpu)
Glauber Costa69c18c12008-03-03 14:13:07 -03001454{
Mike Travisc2d1cec2009-01-04 05:18:03 -08001455 set_cpu_online(cpu, false);
1456 cpumask_clear_cpu(cpu, cpu_callout_mask);
1457 cpumask_clear_cpu(cpu, cpu_callin_mask);
Glauber Costa69c18c12008-03-03 14:13:07 -03001458 /* was set by cpu_init() */
Mike Travisc2d1cec2009-01-04 05:18:03 -08001459 cpumask_clear_cpu(cpu, cpu_initialized_mask);
Mike Travis23ca4bb2008-05-12 21:21:12 +02001460 numa_remove_cpu(cpu);
Glauber Costa69c18c12008-03-03 14:13:07 -03001461}
1462
Alex Nixon8227dce2008-08-22 11:52:14 +01001463void cpu_disable_common(void)
1464{
1465 int cpu = smp_processor_id();
Alex Nixon8227dce2008-08-22 11:52:14 +01001466
Alex Nixon8227dce2008-08-22 11:52:14 +01001467 remove_siblinginfo(cpu);
1468
1469 /* It's now safe to remove this processor from the online map */
1470 lock_vector_lock();
1471 remove_cpu_from_maps(cpu);
1472 unlock_vector_lock();
Mike Travisd7b381b2008-12-16 17:33:58 -08001473 fixup_irqs();
Thomas Gleixner0fa115d2017-09-13 23:29:38 +02001474 lapic_offline();
Alex Nixon8227dce2008-08-22 11:52:14 +01001475}
1476
Alex Nixon93be71b2008-08-22 11:52:11 +01001477int native_cpu_disable(void)
Glauber Costa69c18c12008-03-03 14:13:07 -03001478{
Prarit Bhargavada6139e2014-01-13 06:51:01 -05001479 int ret;
1480
Thomas Gleixner2cffad72017-09-13 23:29:53 +02001481 ret = lapic_can_unplug_cpu();
Prarit Bhargavada6139e2014-01-13 06:51:01 -05001482 if (ret)
1483 return ret;
1484
Glauber Costa69c18c12008-03-03 14:13:07 -03001485 clear_local_APIC();
Alex Nixon8227dce2008-08-22 11:52:14 +01001486 cpu_disable_common();
Lan Tianyu2ed53c02014-08-26 15:43:45 +08001487
Glauber Costa69c18c12008-03-03 14:13:07 -03001488 return 0;
1489}
1490
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001491int common_cpu_die(unsigned int cpu)
Boris Ostrovsky54279552014-10-31 11:49:32 -04001492{
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001493 int ret = 0;
Boris Ostrovsky54279552014-10-31 11:49:32 -04001494
Glauber Costa69c18c12008-03-03 14:13:07 -03001495 /* We don't do anything here: idle task is faking death itself. */
Boris Ostrovsky54279552014-10-31 11:49:32 -04001496
Lan Tianyu2ed53c02014-08-26 15:43:45 +08001497 /* They ack this in play_dead() by setting CPU_DEAD */
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001498 if (cpu_wait_death(cpu, 5)) {
Lan Tianyu2ed53c02014-08-26 15:43:45 +08001499 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);
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001503 ret = -1;
Glauber Costa69c18c12008-03-03 14:13:07 -03001504 }
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001505
1506 return ret;
1507}
1508
1509void native_cpu_die(unsigned int cpu)
1510{
1511 common_cpu_die(cpu);
Glauber Costa69c18c12008-03-03 14:13:07 -03001512}
Alex Nixona21f5d882008-08-22 11:52:13 +01001513
1514void play_dead_common(void)
1515{
1516 idle_task_exit();
Alex Nixona21f5d882008-08-22 11:52:13 +01001517
Alex Nixona21f5d882008-08-22 11:52:13 +01001518 /* Ack it */
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001519 (void)cpu_report_death();
Alex Nixona21f5d882008-08-22 11:52:13 +01001520
1521 /*
1522 * With physical CPU hotplug, we should halt the cpu
1523 */
1524 local_irq_disable();
1525}
1526
Fenghua Yue1c467e2012-11-14 04:36:53 -08001527static bool wakeup_cpu0(void)
1528{
1529 if (smp_processor_id() == 0 && enable_start_cpu0)
1530 return true;
1531
1532 return false;
1533}
1534
H. Peter Anvinea530692010-09-17 15:39:11 -07001535/*
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;
H. Peter Anvince5f6822010-09-20 13:04:45 -07001544 void *mwait_ptr;
Borislav Petkov576cfb42013-03-04 21:16:16 +01001545 int i;
H. Peter Anvinea530692010-09-17 15:39:11 -07001546
Len Brown69fb3672013-02-10 01:38:39 -05001547 if (!this_cpu_has(X86_FEATURE_MWAIT))
H. Peter Anvinea530692010-09-17 15:39:11 -07001548 return;
H. Peter Anvin840d2832014-02-27 08:31:30 -08001549 if (!this_cpu_has(X86_FEATURE_CLFLUSH))
H. Peter Anvince5f6822010-09-20 13:04:45 -07001550 return;
Tejun Heo7b543a52010-12-18 16:30:05 +01001551 if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
H. Peter Anvinea530692010-09-17 15:39:11 -07001552 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
H. Peter Anvince5f6822010-09-20 13:04:45 -07001576 /*
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
H. Peter Anvina68e5c92010-09-17 17:06:46 -07001583 wbinvd();
1584
H. Peter Anvinea530692010-09-17 15:39:11 -07001585 while (1) {
H. Peter Anvince5f6822010-09-20 13:04:45 -07001586 /*
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 */
H. Peter Anvin7d590cc2013-12-19 12:30:03 -08001593 mb();
H. Peter Anvince5f6822010-09-20 13:04:45 -07001594 clflush(mwait_ptr);
H. Peter Anvin7d590cc2013-12-19 12:30:03 -08001595 mb();
H. Peter Anvince5f6822010-09-20 13:04:45 -07001596 __monitor(mwait_ptr, 0, 0);
H. Peter Anvinea530692010-09-17 15:39:11 -07001597 mb();
1598 __mwait(eax, 0);
Fenghua Yue1c467e2012-11-14 04:36:53 -08001599 /*
1600 * If NMI wants to wake up CPU0, start CPU0.
1601 */
1602 if (wakeup_cpu0())
1603 start_cpu0();
H. Peter Anvinea530692010-09-17 15:39:11 -07001604 }
1605}
1606
Rafael J. Wysocki406f9922016-07-14 03:55:23 +02001607void hlt_play_dead(void)
H. Peter Anvinea530692010-09-17 15:39:11 -07001608{
Tejun Heo7b543a52010-12-18 16:30:05 +01001609 if (__this_cpu_read(cpu_info.x86) >= 4)
H. Peter Anvina68e5c92010-09-17 17:06:46 -07001610 wbinvd();
1611
H. Peter Anvinea530692010-09-17 15:39:11 -07001612 while (1) {
H. Peter Anvinea530692010-09-17 15:39:11 -07001613 native_halt();
Fenghua Yue1c467e2012-11-14 04:36:53 -08001614 /*
1615 * If NMI wants to wake up CPU0, start CPU0.
1616 */
1617 if (wakeup_cpu0())
1618 start_cpu0();
H. Peter Anvinea530692010-09-17 15:39:11 -07001619 }
1620}
1621
Alex Nixona21f5d882008-08-22 11:52:13 +01001622void native_play_dead(void)
1623{
1624 play_dead_common();
Joseph Cihula86886e52009-06-30 19:31:07 -07001625 tboot_shutdown(TB_SHUTDOWN_WFS);
H. Peter Anvinea530692010-09-17 15:39:11 -07001626
1627 mwait_play_dead(); /* Only returns on failure */
Boris Ostrovsky1a022e32012-03-13 19:55:09 +01001628 if (cpuidle_play_dead())
1629 hlt_play_dead();
Alex Nixona21f5d882008-08-22 11:52:13 +01001630}
1631
Glauber Costa69c18c12008-03-03 14:13:07 -03001632#else /* ... !CONFIG_HOTPLUG_CPU */
Alex Nixon93be71b2008-08-22 11:52:11 +01001633int native_cpu_disable(void)
Glauber Costa69c18c12008-03-03 14:13:07 -03001634{
1635 return -ENOSYS;
1636}
1637
Alex Nixon93be71b2008-08-22 11:52:11 +01001638void native_cpu_die(unsigned int cpu)
Glauber Costa69c18c12008-03-03 14:13:07 -03001639{
1640 /* We said "no" in __cpu_disable */
1641 BUG();
1642}
Alex Nixona21f5d882008-08-22 11:52:13 +01001643
1644void native_play_dead(void)
1645{
1646 BUG();
1647}
1648
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001649#endif