blob: 2a6e84a30a546f022331c653ec0725bd73086bc5 [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>
Glauber Costa69c18c12008-03-03 14:13:07 -030048#include <linux/percpu.h>
Glauber Costa91718e82008-03-03 14:13:12 -030049#include <linux/bootmem.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030050#include <linux/err.h>
51#include <linux/nmi.h>
Shane Wang69575d32009-09-01 18:25:07 -070052#include <linux/tboot.h>
Jacob Pan35f720c2009-09-17 07:36:43 -070053#include <linux/stackprotector.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/gfp.h>
Boris Ostrovsky1a022e32012-03-13 19:55:09 +010055#include <linux/cpuidle.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030056
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -030057#include <asm/acpi.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030058#include <asm/desc.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030059#include <asm/nmi.h>
60#include <asm/irq.h>
Ingo Molnar07bbc162008-09-23 23:26:42 +020061#include <asm/idle.h>
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030062#include <asm/realmode.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030063#include <asm/cpu.h>
64#include <asm/numa.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030065#include <asm/pgtable.h>
66#include <asm/tlbflush.h>
67#include <asm/mtrr.h>
H. Peter Anvinea530692010-09-17 15:39:11 -070068#include <asm/mwait.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010069#include <asm/apic.h>
Henrik Kretzschmar7167d082011-02-22 15:38:05 +010070#include <asm/io_apic.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020071#include <asm/fpu/internal.h>
Yinghai Lu569712b2008-11-16 03:12:49 -080072#include <asm/setup.h>
Tejun Heobdbcdd42009-01-21 17:26:06 +090073#include <asm/uv/uv.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030074#include <linux/mc146818rtc.h>
Jacob Panb81bb372009-11-09 11:27:04 -080075#include <asm/i8259.h>
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030076#include <asm/realmode.h>
Borislav Petkov646e29a2013-09-27 16:35:54 +020077#include <asm/misc.h>
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +030078
Glauber Costaa3553522008-03-03 14:12:58 -030079/* Number of siblings per CPU package */
80int smp_num_siblings = 1;
81EXPORT_SYMBOL(smp_num_siblings);
82
83/* Last level cache ID of each logical CPU */
Vlad Zolotarov0816b0f2012-06-11 12:56:52 +030084DEFINE_PER_CPU_READ_MOSTLY(u16, cpu_llc_id) = BAD_APICID;
Glauber Costaa3553522008-03-03 14:12:58 -030085
Glauber Costaa3553522008-03-03 14:12:58 -030086/* representing HT siblings of each logical CPU */
Vlad Zolotarov0816b0f2012-06-11 12:56:52 +030087DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
Glauber Costaa3553522008-03-03 14:12:58 -030088EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
89
90/* representing HT and core siblings of each logical CPU */
Vlad Zolotarov0816b0f2012-06-11 12:56:52 +030091DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
Glauber Costaa3553522008-03-03 14:12:58 -030092EXPORT_PER_CPU_SYMBOL(cpu_core_map);
93
Vlad Zolotarov0816b0f2012-06-11 12:56:52 +030094DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
Yinghai Lub3d73362011-01-21 15:29:44 -080095
Glauber Costaa3553522008-03-03 14:12:58 -030096/* Per CPU bogomips and other parameters */
Jan Beulich2c773dd2014-11-04 08:26:42 +000097DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
Glauber Costaa3553522008-03-03 14:12:58 -030098EXPORT_PER_CPU_SYMBOL(cpu_info);
Glauber Costa768d9502008-03-03 14:13:02 -030099
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000100/* Logical package management. We might want to allocate that dynamically */
101static int *physical_to_logical_pkg __read_mostly;
102static unsigned long *physical_package_map __read_mostly;;
103static unsigned long *logical_package_map __read_mostly;
104static unsigned int max_physical_pkg_id __read_mostly;
105unsigned int __max_logical_packages __read_mostly;
106EXPORT_SYMBOL(__max_logical_packages);
107
Andi Kleen70b83012016-05-19 17:09:55 -0700108/* Maximum number of SMT threads on any online core */
109int __max_smt_threads __read_mostly;
110
Thomas Gleixnerf77aa302015-01-15 21:22:29 +0000111static inline void smpboot_setup_warm_reset_vector(unsigned long start_eip)
112{
113 unsigned long flags;
114
115 spin_lock_irqsave(&rtc_lock, flags);
116 CMOS_WRITE(0xa, 0xf);
117 spin_unlock_irqrestore(&rtc_lock, flags);
118 local_flush_tlb();
119 pr_debug("1.\n");
120 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_HIGH)) =
121 start_eip >> 4;
122 pr_debug("2.\n");
123 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) =
124 start_eip & 0xf;
125 pr_debug("3.\n");
126}
127
128static inline void smpboot_restore_warm_reset_vector(void)
129{
130 unsigned long flags;
131
132 /*
133 * Install writable page 0 entry to set BIOS data area.
134 */
135 local_flush_tlb();
136
137 /*
138 * Paranoid: Set warm reset code and vector here back
139 * to default values.
140 */
141 spin_lock_irqsave(&rtc_lock, flags);
142 CMOS_WRITE(0, 0xf);
143 spin_unlock_irqrestore(&rtc_lock, flags);
144
145 *((volatile u32 *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) = 0;
146}
147
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300148/*
Fenghua Yu30106c12012-11-13 11:32:41 -0800149 * Report back to the Boot Processor during boot time or to the caller processor
150 * during CPU online.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300151 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400152static void smp_callin(void)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300153{
154 int cpuid, phys_id;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300155
156 /*
157 * If waken up by an INIT in an 82489DX configuration
Len Brown656bba32015-08-16 11:45:48 -0400158 * cpu_callout_mask guarantees we don't get here before
159 * an INIT_deassert IPI reaches our local APIC, so it is
160 * now safe to touch our local APIC.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300161 */
Fenghua Yue1c467e2012-11-14 04:36:53 -0800162 cpuid = smp_processor_id();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300163
164 /*
165 * (This works even if the APIC is not enabled.)
166 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -0700167 phys_id = read_apic_id();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300168
169 /*
170 * the boot CPU has finished the init stage and is spinning
171 * on callin_map until we finish. We are free to set up this
172 * CPU, first the APIC. (this is probably redundant on most
173 * boards)
174 */
Thomas Gleixner05f7e462015-01-15 21:22:40 +0000175 apic_ap_setup();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300176
Suresh Siddha9d133e52010-01-29 11:42:21 -0800177 /*
Jack Steinerb5652012011-11-15 15:33:56 -0800178 * Save our processor parameters. Note: this information
179 * is needed for clock calibration.
180 */
181 smp_store_cpu_info(cpuid);
182
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300183 /*
184 * Get our bogomips.
Jack Steinerb5652012011-11-15 15:33:56 -0800185 * Update loops_per_jiffy in cpu_data. Previous call to
186 * smp_store_cpu_info() stored a value that is close but not as
187 * accurate as the value just calculated.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300188 */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300189 calibrate_delay();
Jack Steinerb5652012011-11-15 15:33:56 -0800190 cpu_data(cpuid).loops_per_jiffy = loops_per_jiffy;
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200191 pr_debug("Stack at about %p\n", &cpuid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300192
193 /*
Andi Kleen5ef428c2010-11-18 11:47:31 +0100194 * This must be done before setting cpu_online_mask
195 * or calling notify_cpu_starting.
196 */
197 set_cpu_sibling_map(raw_smp_processor_id());
198 wmb();
199
Peter Zijlstra85257022010-03-23 19:30:52 +0100200 notify_cpu_starting(cpuid);
201
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300202 /*
203 * Allow the master to continue.
204 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800205 cpumask_set_cpu(cpuid, cpu_callin_mask);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300206}
207
Fenghua Yue1c467e2012-11-14 04:36:53 -0800208static int cpu0_logical_apicid;
209static int enable_start_cpu0;
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300210/*
211 * Activate a secondary processor.
212 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400213static void notrace start_secondary(void *unused)
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300214{
215 /*
216 * Don't put *anything* before cpu_init(), SMP booting is too
217 * fragile that we want to limit the things done here to the
218 * most necessary things.
219 */
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300220 cpu_init();
Igor Mammedovdf156f92012-02-07 15:52:44 +0100221 x86_cpuinit.early_percpu_clock_init();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300222 preempt_disable();
223 smp_callin();
224
Fenghua Yue1c467e2012-11-14 04:36:53 -0800225 enable_start_cpu0 = 0;
226
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200227#ifdef CONFIG_X86_32
228 /* switch away from the initial page table */
229 load_cr3(swapper_pg_dir);
230 __flush_tlb_all();
231#endif
232
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300233 /* otherwise gcc will move up smp_processor_id before the cpu_init */
234 barrier();
235 /*
236 * Check TSC synchronization with the BP:
237 */
238 check_tsc_sync_target();
239
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300240 /*
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000241 * Lock vector_lock and initialize the vectors on this cpu
242 * before setting the cpu online. We must set it online with
243 * vector_lock held to prevent a concurrent setup/teardown
244 * from seeing a half valid vector space.
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300245 */
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700246 lock_vector_lock();
Thomas Gleixner5a3f75e2015-07-05 17:12:32 +0000247 setup_vector_irq(smp_processor_id());
Mike Travisc2d1cec2009-01-04 05:18:03 -0800248 set_cpu_online(smp_processor_id(), true);
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700249 unlock_vector_lock();
Paul E. McKenney2a442c92015-02-25 11:42:15 -0800250 cpu_set_state_online(smp_processor_id());
Russ Anderson78c06172010-02-26 10:49:12 -0600251 x86_platform.nmi_init();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300252
Manfred Spraul0cefa5b2008-09-07 11:29:58 +0200253 /* enable local interrupts */
254 local_irq_enable();
255
Jacob Pan35f720c2009-09-17 07:36:43 -0700256 /* to prevent fake stack check failure in clock setup */
257 boot_init_stack_canary();
258
Thomas Gleixner736deca2009-08-19 12:35:53 +0200259 x86_cpuinit.setup_percpu_clockev();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300260
261 wmb();
Thomas Gleixnerfc6d73d2016-02-26 18:43:40 +0000262 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300263}
264
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000265int topology_update_package_map(unsigned int apicid, unsigned int cpu)
266{
267 unsigned int new, pkg = apicid >> boot_cpu_data.x86_coreid_bits;
268
269 /* Called from early boot ? */
270 if (!physical_package_map)
271 return 0;
272
273 if (pkg >= max_physical_pkg_id)
274 return -EINVAL;
275
276 /* Set the logical package id */
277 if (test_and_set_bit(pkg, physical_package_map))
278 goto found;
279
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000280 new = find_first_zero_bit(logical_package_map, __max_logical_packages);
281 if (new >= __max_logical_packages) {
282 physical_to_logical_pkg[pkg] = -1;
283 pr_warn("APIC(%x) Package %u exceeds logical package map\n",
284 apicid, pkg);
285 return -ENOSPC;
286 }
287 set_bit(new, logical_package_map);
288 pr_info("APIC(%x) Converting physical %u to logical package %u\n",
289 apicid, pkg, new);
290 physical_to_logical_pkg[pkg] = new;
291
292found:
293 cpu_data(cpu).logical_proc_id = physical_to_logical_pkg[pkg];
294 return 0;
295}
296
297/**
298 * topology_phys_to_logical_pkg - Map a physical package id to a logical
299 *
300 * Returns logical package id or -1 if not found
301 */
302int topology_phys_to_logical_pkg(unsigned int phys_pkg)
303{
304 if (phys_pkg >= max_physical_pkg_id)
305 return -1;
306 return physical_to_logical_pkg[phys_pkg];
307}
308EXPORT_SYMBOL(topology_phys_to_logical_pkg);
309
310static void __init smp_init_package_map(void)
311{
312 unsigned int ncpus, cpu;
313 size_t size;
314
315 /*
316 * Today neither Intel nor AMD support heterogenous systems. That
317 * might change in the future....
Peter Zijlstra63d1e992016-03-18 16:03:48 +0100318 *
319 * While ideally we'd want '* smp_num_siblings' in the below @ncpus
320 * computation, this won't actually work since some Intel BIOSes
321 * report inconsistent HT data when they disable HT.
322 *
323 * In particular, they reduce the APIC-IDs to only include the cores,
324 * but leave the CPUID topology to say there are (2) siblings.
325 * This means we don't know how many threads there will be until
326 * after the APIC enumeration.
327 *
328 * By not including this we'll sometimes over-estimate the number of
329 * logical packages by the amount of !present siblings, but this is
330 * still better than MAX_LOCAL_APIC.
Thomas Gleixner3e8db222016-03-18 17:20:30 +0100331 *
332 * We use total_cpus not nr_cpu_ids because nr_cpu_ids can be limited
333 * on the command line leading to a similar issue as the HT disable
334 * problem because the hyperthreads are usually enumerated after the
335 * primary cores.
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000336 */
Peter Zijlstra63d1e992016-03-18 16:03:48 +0100337 ncpus = boot_cpu_data.x86_max_cores;
Thomas Gleixner56402d62016-05-06 20:48:16 +0200338 if (!ncpus) {
339 pr_warn("x86_max_cores == zero !?!?");
340 ncpus = 1;
341 }
342
Thomas Gleixner3e8db222016-03-18 17:20:30 +0100343 __max_logical_packages = DIV_ROUND_UP(total_cpus, ncpus);
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000344
345 /*
346 * Possibly larger than what we need as the number of apic ids per
347 * package can be smaller than the actual used apic ids.
348 */
349 max_physical_pkg_id = DIV_ROUND_UP(MAX_LOCAL_APIC, ncpus);
350 size = max_physical_pkg_id * sizeof(unsigned int);
351 physical_to_logical_pkg = kmalloc(size, GFP_KERNEL);
352 memset(physical_to_logical_pkg, 0xff, size);
353 size = BITS_TO_LONGS(max_physical_pkg_id) * sizeof(unsigned long);
354 physical_package_map = kzalloc(size, GFP_KERNEL);
355 size = BITS_TO_LONGS(__max_logical_packages) * sizeof(unsigned long);
356 logical_package_map = kzalloc(size, GFP_KERNEL);
357
358 pr_info("Max logical packages: %u\n", __max_logical_packages);
359
360 for_each_present_cpu(cpu) {
361 unsigned int apicid = apic->cpu_present_to_apicid(cpu);
362
363 if (apicid == BAD_APICID || !apic->apic_id_valid(apicid))
364 continue;
365 if (!topology_update_package_map(apicid, cpu))
366 continue;
367 pr_warn("CPU %u APICId %x disabled\n", cpu, apicid);
368 per_cpu(x86_bios_cpu_apicid, cpu) = BAD_APICID;
369 set_cpu_possible(cpu, false);
370 set_cpu_present(cpu, false);
371 }
372}
373
Fenghua Yu30106c12012-11-13 11:32:41 -0800374void __init smp_store_boot_cpu_info(void)
375{
376 int id = 0; /* CPU 0 */
377 struct cpuinfo_x86 *c = &cpu_data(id);
378
379 *c = boot_cpu_data;
380 c->cpu_index = id;
Thomas Gleixner1f12e322016-02-22 22:19:15 +0000381 smp_init_package_map();
Fenghua Yu30106c12012-11-13 11:32:41 -0800382}
383
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300384/*
385 * The bootstrap kernel entry code has set these up. Save them for
386 * a given CPU
387 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400388void smp_store_cpu_info(int id)
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300389{
390 struct cpuinfo_x86 *c = &cpu_data(id);
391
Yinghai Lub3d73362011-01-21 15:29:44 -0800392 *c = boot_cpu_data;
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300393 c->cpu_index = id;
Fenghua Yu30106c12012-11-13 11:32:41 -0800394 /*
395 * During boot time, CPU0 has this setup already. Save the info when
396 * bringing up AP or offlined CPU0.
397 */
398 identify_secondary_cpu(c);
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300399}
400
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400401static bool
Dave Hansencebf15e2014-09-18 12:33:34 -0700402topology_same_node(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
403{
404 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
405
406 return (cpu_to_node(cpu1) == cpu_to_node(cpu2));
407}
408
409static bool
Peter Zijlstra316ad242012-05-11 13:05:59 +0200410topology_sane(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o, const char *name)
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200411{
Peter Zijlstra316ad242012-05-11 13:05:59 +0200412 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
413
Dave Hansencebf15e2014-09-18 12:33:34 -0700414 return !WARN_ONCE(!topology_same_node(c, o),
Peter Zijlstra316ad242012-05-11 13:05:59 +0200415 "sched: CPU #%d's %s-sibling CPU #%d is not on the same node! "
416 "[node: %d != %d]. Ignoring dependency.\n",
417 cpu1, name, cpu2, cpu_to_node(cpu1), cpu_to_node(cpu2));
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200418}
419
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200420#define link_mask(mfunc, c1, c2) \
Peter Zijlstra316ad242012-05-11 13:05:59 +0200421do { \
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200422 cpumask_set_cpu((c1), mfunc(c2)); \
423 cpumask_set_cpu((c2), mfunc(c1)); \
Peter Zijlstra316ad242012-05-11 13:05:59 +0200424} while (0)
425
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400426static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
Peter Zijlstra316ad242012-05-11 13:05:59 +0200427{
Borislav Petkov362f9242015-12-07 10:39:41 +0100428 if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
Peter Zijlstra316ad242012-05-11 13:05:59 +0200429 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
430
431 if (c->phys_proc_id == o->phys_proc_id &&
432 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2) &&
Borislav Petkov8196dab2016-03-25 15:52:36 +0100433 c->cpu_core_id == o->cpu_core_id)
Peter Zijlstra316ad242012-05-11 13:05:59 +0200434 return topology_sane(c, o, "smt");
435
436 } else if (c->phys_proc_id == o->phys_proc_id &&
437 c->cpu_core_id == o->cpu_core_id) {
438 return topology_sane(c, o, "smt");
439 }
440
441 return false;
442}
443
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400444static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
Peter Zijlstra316ad242012-05-11 13:05:59 +0200445{
446 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
447
448 if (per_cpu(cpu_llc_id, cpu1) != BAD_APICID &&
449 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2))
450 return topology_sane(c, o, "llc");
451
452 return false;
453}
454
Dave Hansencebf15e2014-09-18 12:33:34 -0700455/*
456 * Unlike the other levels, we do not enforce keeping a
457 * multicore group inside a NUMA node. If this happens, we will
458 * discard the MC level of the topology later.
459 */
460static bool match_die(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
Peter Zijlstra316ad242012-05-11 13:05:59 +0200461{
Dave Hansencebf15e2014-09-18 12:33:34 -0700462 if (c->phys_proc_id == o->phys_proc_id)
463 return true;
Peter Zijlstra316ad242012-05-11 13:05:59 +0200464 return false;
465}
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300466
Dave Hansencebf15e2014-09-18 12:33:34 -0700467static struct sched_domain_topology_level numa_inside_package_topology[] = {
468#ifdef CONFIG_SCHED_SMT
469 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
470#endif
471#ifdef CONFIG_SCHED_MC
472 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
473#endif
474 { NULL, },
475};
476/*
477 * set_sched_topology() sets the topology internal to a CPU. The
478 * NUMA topologies are layered on top of it to build the full
479 * system topology.
480 *
481 * If NUMA nodes are observed to occur within a CPU package, this
482 * function should be called. It forces the sched domain code to
483 * only use the SMT level for the CPU portion of the topology.
484 * This essentially falls back to relying on NUMA information
485 * from the SRAT table to describe the entire system topology
486 * (except for hyperthreads).
487 */
488static void primarily_use_numa_for_topology(void)
489{
490 set_sched_topology(numa_inside_package_topology);
491}
492
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400493void set_cpu_sibling_map(int cpu)
Glauber Costa768d9502008-03-03 14:13:02 -0300494{
Peter Zijlstra316ad242012-05-11 13:05:59 +0200495 bool has_smt = smp_num_siblings > 1;
Andrew Jonesb0bc2252013-05-29 14:48:15 +0200496 bool has_mp = has_smt || boot_cpu_data.x86_max_cores > 1;
Glauber Costa768d9502008-03-03 14:13:02 -0300497 struct cpuinfo_x86 *c = &cpu_data(cpu);
Peter Zijlstra316ad242012-05-11 13:05:59 +0200498 struct cpuinfo_x86 *o;
Andi Kleen70b83012016-05-19 17:09:55 -0700499 int i, threads;
Glauber Costa768d9502008-03-03 14:13:02 -0300500
Mike Travisc2d1cec2009-01-04 05:18:03 -0800501 cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
Glauber Costa768d9502008-03-03 14:13:02 -0300502
Andrew Jonesb0bc2252013-05-29 14:48:15 +0200503 if (!has_mp) {
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200504 cpumask_set_cpu(cpu, topology_sibling_cpumask(cpu));
Peter Zijlstra316ad242012-05-11 13:05:59 +0200505 cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200506 cpumask_set_cpu(cpu, topology_core_cpumask(cpu));
Glauber Costa768d9502008-03-03 14:13:02 -0300507 c->booted_cores = 1;
508 return;
509 }
510
Mike Travisc2d1cec2009-01-04 05:18:03 -0800511 for_each_cpu(i, cpu_sibling_setup_mask) {
Peter Zijlstra316ad242012-05-11 13:05:59 +0200512 o = &cpu_data(i);
Peter Zijlstra0acbb442012-04-18 19:04:09 +0200513
Peter Zijlstra316ad242012-05-11 13:05:59 +0200514 if ((i == cpu) || (has_smt && match_smt(c, o)))
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200515 link_mask(topology_sibling_cpumask, cpu, i);
Peter Zijlstra0acbb442012-04-18 19:04:09 +0200516
Andrew Jonesb0bc2252013-05-29 14:48:15 +0200517 if ((i == cpu) || (has_mp && match_llc(c, o)))
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200518 link_mask(cpu_llc_shared_mask, cpu, i);
Peter Zijlstra316ad242012-05-11 13:05:59 +0200519
Kamalesh Babulalceb1cba2012-05-31 13:07:38 +0530520 }
521
522 /*
523 * This needs a separate iteration over the cpus because we rely on all
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200524 * topology_sibling_cpumask links to be set-up.
Kamalesh Babulalceb1cba2012-05-31 13:07:38 +0530525 */
526 for_each_cpu(i, cpu_sibling_setup_mask) {
527 o = &cpu_data(i);
528
Dave Hansencebf15e2014-09-18 12:33:34 -0700529 if ((i == cpu) || (has_mp && match_die(c, o))) {
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200530 link_mask(topology_core_cpumask, cpu, i);
Peter Zijlstra316ad242012-05-11 13:05:59 +0200531
Glauber Costa768d9502008-03-03 14:13:02 -0300532 /*
533 * Does this new cpu bringup a new core?
534 */
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200535 if (cpumask_weight(
536 topology_sibling_cpumask(cpu)) == 1) {
Glauber Costa768d9502008-03-03 14:13:02 -0300537 /*
538 * for each core in package, increment
539 * the booted_cores for this new cpu
540 */
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200541 if (cpumask_first(
542 topology_sibling_cpumask(i)) == i)
Glauber Costa768d9502008-03-03 14:13:02 -0300543 c->booted_cores++;
544 /*
545 * increment the core count for all
546 * the other cpus in this package
547 */
548 if (i != cpu)
549 cpu_data(i).booted_cores++;
550 } else if (i != cpu && !c->booted_cores)
551 c->booted_cores = cpu_data(i).booted_cores;
552 }
Dave Hansen728e5652014-09-30 14:45:46 -0700553 if (match_die(c, o) && !topology_same_node(c, o))
Dave Hansencebf15e2014-09-18 12:33:34 -0700554 primarily_use_numa_for_topology();
Glauber Costa768d9502008-03-03 14:13:02 -0300555 }
Andi Kleen70b83012016-05-19 17:09:55 -0700556
557 threads = cpumask_weight(topology_sibling_cpumask(cpu));
558 if (threads > __max_smt_threads)
559 __max_smt_threads = threads;
Glauber Costa768d9502008-03-03 14:13:02 -0300560}
561
Glauber Costa70708a12008-03-03 14:13:03 -0300562/* maps the cpu to the sched domain representing multi-core */
Rusty Russell030bb202008-12-26 22:23:41 +1030563const struct cpumask *cpu_coregroup_mask(int cpu)
Glauber Costa70708a12008-03-03 14:13:03 -0300564{
Peter Zijlstra9f646382012-05-29 16:39:09 +0200565 return cpu_llc_shared_mask(cpu);
Rusty Russell030bb202008-12-26 22:23:41 +1030566}
567
Ingo Molnara4928cf2008-04-23 13:20:56 +0200568static void impress_friends(void)
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300569{
570 int cpu;
571 unsigned long bogosum = 0;
572 /*
573 * Allow the user to impress friends.
574 */
Joe Perchesc767a542012-05-21 19:50:07 -0700575 pr_debug("Before bogomips\n");
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300576 for_each_possible_cpu(cpu)
Mike Travisc2d1cec2009-01-04 05:18:03 -0800577 if (cpumask_test_cpu(cpu, cpu_callout_mask))
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300578 bogosum += cpu_data(cpu).loops_per_jiffy;
Joe Perchesc767a542012-05-21 19:50:07 -0700579 pr_info("Total of %d processors activated (%lu.%02lu BogoMIPS)\n",
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300580 num_online_cpus(),
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300581 bogosum/(500000/HZ),
582 (bogosum/(5000/HZ))%100);
583
Joe Perchesc767a542012-05-21 19:50:07 -0700584 pr_debug("Before bogocount - setting activated=1\n");
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300585}
586
Yinghai Lu569712b2008-11-16 03:12:49 -0800587void __inquire_remote_apic(int apicid)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300588{
589 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
Greg Dietschea6c239052011-06-30 20:10:53 -0500590 const char * const names[] = { "ID", "VERSION", "SPIV" };
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300591 int timeout;
592 u32 status;
593
Joe Perchesc767a542012-05-21 19:50:07 -0700594 pr_info("Inquiring remote APIC 0x%x...\n", apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300595
596 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Joe Perchesc767a542012-05-21 19:50:07 -0700597 pr_info("... APIC 0x%x %s: ", apicid, names[i]);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300598
599 /*
600 * Wait for idle.
601 */
602 status = safe_apic_wait_icr_idle();
603 if (status)
Joe Perchesc767a542012-05-21 19:50:07 -0700604 pr_cont("a previous APIC delivery may have failed\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300605
Suresh Siddha1b374e42008-07-10 11:16:49 -0700606 apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300607
608 timeout = 0;
609 do {
610 udelay(100);
611 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
612 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
613
614 switch (status) {
615 case APIC_ICR_RR_VALID:
616 status = apic_read(APIC_RRR);
Joe Perchesc767a542012-05-21 19:50:07 -0700617 pr_cont("%08x\n", status);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300618 break;
619 default:
Joe Perchesc767a542012-05-21 19:50:07 -0700620 pr_cont("failed\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300621 }
622 }
623}
624
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300625/*
Len Brownd68921f2015-05-11 17:27:09 -0400626 * The Multiprocessor Specification 1.4 (1997) example code suggests
627 * that there should be a 10ms delay between the BSP asserting INIT
628 * and de-asserting INIT, when starting a remote processor.
629 * But that slows boot and resume on modern processors, which include
630 * many cores and don't require that delay.
631 *
632 * Cmdline "init_cpu_udelay=" is available to over-ride this delay.
Len Brown1a744cb2015-05-11 17:27:10 -0400633 * Modern processor families are quirked to remove the delay entirely.
Len Brownd68921f2015-05-11 17:27:09 -0400634 */
635#define UDELAY_10MS_DEFAULT 10000
636
Len Brown656279a2015-11-22 18:16:15 -0500637static unsigned int init_udelay = UINT_MAX;
Len Brownd68921f2015-05-11 17:27:09 -0400638
639static int __init cpu_init_udelay(char *str)
640{
641 get_option(&str, &init_udelay);
642
643 return 0;
644}
645early_param("cpu_init_udelay", cpu_init_udelay);
646
Len Brown1a744cb2015-05-11 17:27:10 -0400647static void __init smp_quirk_init_udelay(void)
648{
649 /* if cmdline changed it from default, leave it alone */
Len Brown656279a2015-11-22 18:16:15 -0500650 if (init_udelay != UINT_MAX)
Len Brown1a744cb2015-05-11 17:27:10 -0400651 return;
652
653 /* if modern processor, use no delay */
654 if (((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 6)) ||
Len Brown656279a2015-11-22 18:16:15 -0500655 ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && (boot_cpu_data.x86 >= 0xF))) {
Len Brown1a744cb2015-05-11 17:27:10 -0400656 init_udelay = 0;
Len Brown656279a2015-11-22 18:16:15 -0500657 return;
658 }
Len Brownf1ccd242015-10-16 00:14:28 -0400659 /* else, use legacy delay */
660 init_udelay = UDELAY_10MS_DEFAULT;
Len Brown1a744cb2015-05-11 17:27:10 -0400661}
662
Len Brownd68921f2015-05-11 17:27:09 -0400663/*
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300664 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
665 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
666 * won't ... remember to clear down the APIC, etc later.
667 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400668int
Fenghua Yue1c467e2012-11-14 04:36:53 -0800669wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300670{
671 unsigned long send_status, accept_status = 0;
672 int maxlvt;
673
674 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300675 /* Boot on the stack */
676 /* Kick the second */
Fenghua Yue1c467e2012-11-14 04:36:53 -0800677 apic_icr_write(APIC_DM_NMI | apic->dest_logical, apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300678
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200679 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300680 send_status = safe_apic_wait_icr_idle();
681
682 /*
683 * Give the other CPU some time to accept the IPI.
684 */
685 udelay(200);
Yinghai Lu569712b2008-11-16 03:12:49 -0800686 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunov59ef48a2008-09-14 21:58:49 +0400687 maxlvt = lapic_get_maxlvt();
688 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
689 apic_write(APIC_ESR, 0);
690 accept_status = (apic_read(APIC_ESR) & 0xEF);
691 }
Joe Perchesc767a542012-05-21 19:50:07 -0700692 pr_debug("NMI sent\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300693
694 if (send_status)
Joe Perchesc767a542012-05-21 19:50:07 -0700695 pr_err("APIC never delivered???\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300696 if (accept_status)
Joe Perchesc767a542012-05-21 19:50:07 -0700697 pr_err("APIC delivery error (%lx)\n", accept_status);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300698
699 return (send_status | accept_status);
700}
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300701
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400702static int
Yinghai Lu569712b2008-11-16 03:12:49 -0800703wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300704{
Jan H. Schönherrf5d6a522015-05-04 11:42:34 +0200705 unsigned long send_status = 0, accept_status = 0;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300706 int maxlvt, num_starts, j;
707
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100708 maxlvt = lapic_get_maxlvt();
709
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300710 /*
711 * Be paranoid about clearing APIC errors.
712 */
713 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100714 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
715 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300716 apic_read(APIC_ESR);
717 }
718
Joe Perchesc767a542012-05-21 19:50:07 -0700719 pr_debug("Asserting INIT\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300720
721 /*
722 * Turn INIT on target chip
723 */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300724 /*
725 * Send IPI
726 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700727 apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
728 phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300729
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200730 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300731 send_status = safe_apic_wait_icr_idle();
732
Ingo Molnar7cb68592015-05-18 12:05:13 +0200733 udelay(init_udelay);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300734
Joe Perchesc767a542012-05-21 19:50:07 -0700735 pr_debug("Deasserting INIT\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300736
737 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300738 /* Send IPI */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700739 apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300740
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200741 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300742 send_status = safe_apic_wait_icr_idle();
743
744 mb();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300745
746 /*
747 * Should we send STARTUP IPIs ?
748 *
749 * Determine this based on the APIC version.
750 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
751 */
752 if (APIC_INTEGRATED(apic_version[phys_apicid]))
753 num_starts = 2;
754 else
755 num_starts = 0;
756
757 /*
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300758 * Run STARTUP IPI loop.
759 */
Joe Perchesc767a542012-05-21 19:50:07 -0700760 pr_debug("#startup loops: %d\n", num_starts);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300761
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300762 for (j = 1; j <= num_starts; j++) {
Joe Perchesc767a542012-05-21 19:50:07 -0700763 pr_debug("Sending STARTUP #%d\n", j);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100764 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
765 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300766 apic_read(APIC_ESR);
Joe Perchesc767a542012-05-21 19:50:07 -0700767 pr_debug("After apic_write\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300768
769 /*
770 * STARTUP IPI
771 */
772
773 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300774 /* Boot on the stack */
775 /* Kick the second */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700776 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
777 phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300778
779 /*
780 * Give the other CPU some time to accept the IPI.
781 */
Len Brownfcafdde2015-10-16 00:14:29 -0400782 if (init_udelay == 0)
783 udelay(10);
784 else
Len Browna9bcaa02015-08-16 11:45:47 -0400785 udelay(300);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300786
Joe Perchesc767a542012-05-21 19:50:07 -0700787 pr_debug("Startup point 1\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300788
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200789 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300790 send_status = safe_apic_wait_icr_idle();
791
792 /*
793 * Give the other CPU some time to accept the IPI.
794 */
Len Brownfcafdde2015-10-16 00:14:29 -0400795 if (init_udelay == 0)
796 udelay(10);
797 else
Len Browna9bcaa02015-08-16 11:45:47 -0400798 udelay(200);
Jan H. Schönherrf5d6a522015-05-04 11:42:34 +0200799
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100800 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300801 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300802 accept_status = (apic_read(APIC_ESR) & 0xEF);
803 if (send_status || accept_status)
804 break;
805 }
Joe Perchesc767a542012-05-21 19:50:07 -0700806 pr_debug("After Startup\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300807
808 if (send_status)
Joe Perchesc767a542012-05-21 19:50:07 -0700809 pr_err("APIC never delivered???\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300810 if (accept_status)
Joe Perchesc767a542012-05-21 19:50:07 -0700811 pr_err("APIC delivery error (%lx)\n", accept_status);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300812
813 return (send_status | accept_status);
814}
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300815
Borislav Petkova17bce42013-09-30 11:56:24 +0200816void smp_announce(void)
817{
818 int num_nodes = num_online_nodes();
819
820 printk(KERN_INFO "x86: Booted up %d node%s, %d CPUs\n",
821 num_nodes, (num_nodes > 1 ? "s" : ""), num_online_cpus());
822}
823
Mike Travis2eaad1f2009-12-10 17:19:36 -0800824/* reduce the number of lines printed when booting a large cpu count system */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400825static void announce_cpu(int cpu, int apicid)
Mike Travis2eaad1f2009-12-10 17:19:36 -0800826{
827 static int current_node = -1;
Borislav Petkov4adc8b72010-06-01 21:04:55 +0200828 int node = early_cpu_to_node(cpu);
Borislav Petkova17bce42013-09-30 11:56:24 +0200829 static int width, node_width;
Borislav Petkov646e29a2013-09-27 16:35:54 +0200830
831 if (!width)
832 width = num_digits(num_possible_cpus()) + 1; /* + '#' sign */
Mike Travis2eaad1f2009-12-10 17:19:36 -0800833
Borislav Petkova17bce42013-09-30 11:56:24 +0200834 if (!node_width)
835 node_width = num_digits(num_possible_nodes()) + 1; /* + '#' */
836
837 if (cpu == 1)
838 printk(KERN_INFO "x86: Booting SMP configuration:\n");
839
Mike Travis2eaad1f2009-12-10 17:19:36 -0800840 if (system_state == SYSTEM_BOOTING) {
841 if (node != current_node) {
842 if (current_node > (-1))
Borislav Petkova17bce42013-09-30 11:56:24 +0200843 pr_cont("\n");
Mike Travis2eaad1f2009-12-10 17:19:36 -0800844 current_node = node;
Borislav Petkova17bce42013-09-30 11:56:24 +0200845
846 printk(KERN_INFO ".... node %*s#%d, CPUs: ",
847 node_width - num_digits(node), " ", node);
Mike Travis2eaad1f2009-12-10 17:19:36 -0800848 }
Borislav Petkov646e29a2013-09-27 16:35:54 +0200849
850 /* Add padding for the BSP */
851 if (cpu == 1)
852 pr_cont("%*s", width + 1, " ");
853
854 pr_cont("%*s#%d", width - num_digits(cpu), " ", cpu);
855
Mike Travis2eaad1f2009-12-10 17:19:36 -0800856 } else
857 pr_info("Booting Node %d Processor %d APIC 0x%x\n",
858 node, cpu, apicid);
859}
860
Fenghua Yue1c467e2012-11-14 04:36:53 -0800861static int wakeup_cpu0_nmi(unsigned int cmd, struct pt_regs *regs)
862{
863 int cpu;
864
865 cpu = smp_processor_id();
866 if (cpu == 0 && !cpu_online(cpu) && enable_start_cpu0)
867 return NMI_HANDLED;
868
869 return NMI_DONE;
870}
871
872/*
873 * Wake up AP by INIT, INIT, STARTUP sequence.
874 *
875 * Instead of waiting for STARTUP after INITs, BSP will execute the BIOS
876 * boot-strap code which is not a desired behavior for waking up BSP. To
877 * void the boot-strap code, wake up CPU0 by NMI instead.
878 *
879 * This works to wake up soft offlined CPU0 only. If CPU0 is hard offlined
880 * (i.e. physically hot removed and then hot added), NMI won't wake it up.
881 * We'll change this code in the future to wake up hard offlined CPU0 if
882 * real platform and request are available.
883 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400884static int
Fenghua Yue1c467e2012-11-14 04:36:53 -0800885wakeup_cpu_via_init_nmi(int cpu, unsigned long start_ip, int apicid,
886 int *cpu0_nmi_registered)
887{
888 int id;
889 int boot_error;
890
Jan Kiszkaea7bdc62014-01-27 20:14:06 +0100891 preempt_disable();
892
Fenghua Yue1c467e2012-11-14 04:36:53 -0800893 /*
894 * Wake up AP by INIT, INIT, STARTUP sequence.
895 */
Jan Kiszkaea7bdc62014-01-27 20:14:06 +0100896 if (cpu) {
897 boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
898 goto out;
899 }
Fenghua Yue1c467e2012-11-14 04:36:53 -0800900
901 /*
902 * Wake up BSP by nmi.
903 *
904 * Register a NMI handler to help wake up CPU0.
905 */
906 boot_error = register_nmi_handler(NMI_LOCAL,
907 wakeup_cpu0_nmi, 0, "wake_cpu0");
908
909 if (!boot_error) {
910 enable_start_cpu0 = 1;
911 *cpu0_nmi_registered = 1;
912 if (apic->dest_logical == APIC_DEST_LOGICAL)
913 id = cpu0_logical_apicid;
914 else
915 id = apicid;
916 boot_error = wakeup_secondary_cpu_via_nmi(id, start_ip);
917 }
918
Jan Kiszkaea7bdc62014-01-27 20:14:06 +0100919out:
920 preempt_enable();
921
Fenghua Yue1c467e2012-11-14 04:36:53 -0800922 return boot_error;
923}
924
Boris Ostrovsky3f854832015-04-01 10:12:14 -0400925void common_cpu_up(unsigned int cpu, struct task_struct *idle)
926{
927 /* Just in case we booted with a single CPU. */
928 alternatives_enable_smp();
929
930 per_cpu(current_task, cpu) = idle;
931
932#ifdef CONFIG_X86_32
933 /* Stack for startup_32 can be just as for start_secondary onwards */
934 irq_ctx_init(cpu);
935 per_cpu(cpu_current_top_of_stack, cpu) =
936 (unsigned long)task_stack_page(idle) + THREAD_SIZE;
937#else
938 clear_tsk_thread_flag(idle, TIF_FORK);
939 initial_gs = per_cpu_offset(cpu);
940#endif
Boris Ostrovsky3f854832015-04-01 10:12:14 -0400941}
942
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300943/*
944 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
945 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
Ingo Molnar1f5bcab2009-02-26 13:51:40 +0100946 * Returns zero if CPU booted OK, else error code from
947 * ->wakeup_secondary_cpu.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300948 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400949static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300950{
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300951 volatile u32 *trampoline_status =
Jarkko Sakkinenb429dbf2012-05-08 21:22:41 +0300952 (volatile u32 *) __va(real_mode_header->trampoline_status);
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300953 /* start_ip had better be page-aligned! */
Jarkko Sakkinenf37240f2012-05-08 21:22:43 +0300954 unsigned long start_ip = real_mode_header->trampoline_start;
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300955
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300956 unsigned long boot_error = 0;
Fenghua Yue1c467e2012-11-14 04:36:53 -0800957 int cpu0_nmi_registered = 0;
Igor Mammedovce4b1b12014-06-20 14:23:11 +0200958 unsigned long timeout;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300959
Thomas Gleixner7eb43a62012-04-20 13:05:48 +0000960 idle->thread.sp = (unsigned long) (((struct pt_regs *)
961 (THREAD_SIZE + task_stack_page(idle))) - 1);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300962
Glauber Costaa9390982008-05-28 16:19:53 -0700963 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
Glauber Costa3e970472008-05-28 13:01:54 -0300964 initial_code = (unsigned long)start_secondary;
Thomas Gleixner7eb43a62012-04-20 13:05:48 +0000965 stack_start = idle->thread.sp;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300966
Zhu Guihua20d5e4a2015-07-03 17:37:19 +0800967 /*
968 * Enable the espfix hack for this CPU
969 */
970#ifdef CONFIG_X86_ESPFIX64
971 init_espfix_ap(cpu);
972#endif
973
Mike Travis2eaad1f2009-12-10 17:19:36 -0800974 /* So we see what's up */
975 announce_cpu(cpu, apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300976
977 /*
978 * This grunge runs the startup process for
979 * the targeted processor.
980 */
981
Jack Steiner34d05592008-04-16 11:45:15 -0500982 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300983
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200984 pr_debug("Setting warm reset code and vector.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300985
Jack Steiner34d05592008-04-16 11:45:15 -0500986 smpboot_setup_warm_reset_vector(start_ip);
987 /*
988 * Be paranoid about clearing APIC errors.
Cyrill Gorcunovdb96b0a2008-10-22 18:00:09 +0400989 */
990 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
991 apic_write(APIC_ESR, 0);
992 apic_read(APIC_ESR);
993 }
Jack Steiner34d05592008-04-16 11:45:15 -0500994 }
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300995
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300996 /*
Igor Mammedovce4b1b12014-06-20 14:23:11 +0200997 * AP might wait on cpu_callout_mask in cpu_init() with
998 * cpu_initialized_mask set if previous attempt to online
999 * it timed-out. Clear cpu_initialized_mask so that after
1000 * INIT/SIPI it could start with a clean state.
1001 */
1002 cpumask_clear_cpu(cpu, cpu_initialized_mask);
1003 smp_mb();
1004
1005 /*
Fenghua Yue1c467e2012-11-14 04:36:53 -08001006 * Wake up a CPU in difference cases:
1007 * - Use the method in the APIC driver if it's defined
1008 * Otherwise,
1009 * - Use an INIT boot APIC message for APs or NMI for BSP.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001010 */
Ingo Molnar1f5bcab2009-02-26 13:51:40 +01001011 if (apic->wakeup_secondary_cpu)
1012 boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
1013 else
Fenghua Yue1c467e2012-11-14 04:36:53 -08001014 boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
1015 &cpu0_nmi_registered);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001016
1017 if (!boot_error) {
1018 /*
Len Brown6e38f1e2015-08-16 11:45:45 -04001019 * Wait 10s total for first sign of life from AP
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001020 */
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001021 boot_error = -1;
1022 timeout = jiffies + 10*HZ;
1023 while (time_before(jiffies, timeout)) {
1024 if (cpumask_test_cpu(cpu, cpu_initialized_mask)) {
1025 /*
1026 * Tell AP to proceed with initialization
1027 */
1028 cpumask_set_cpu(cpu, cpu_callout_mask);
1029 boot_error = 0;
1030 break;
1031 }
Igor Mammedovce4b1b12014-06-20 14:23:11 +02001032 schedule();
1033 }
1034 }
1035
1036 if (!boot_error) {
1037 /*
1038 * Wait till AP completes initial initialization
1039 */
1040 while (!cpumask_test_cpu(cpu, cpu_callin_mask)) {
Suresh Siddha68f202e2010-07-30 11:46:42 -07001041 /*
1042 * Allow other tasks to run while we wait for the
1043 * AP to come online. This also gives a chance
1044 * for the MTRR work(triggered by the AP coming online)
1045 * to be completed in the stop machine context.
1046 */
1047 schedule();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001048 }
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001049 }
1050
1051 /* mark "stuck" area as not stuck */
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +03001052 *trampoline_status = 0;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001053
Yinghai Lu02421f92009-04-03 17:15:53 -07001054 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
1055 /*
1056 * Cleanup possible dangling ends...
1057 */
1058 smpboot_restore_warm_reset_vector();
1059 }
Fenghua Yue1c467e2012-11-14 04:36:53 -08001060 /*
1061 * Clean up the nmi handler. Do this after the callin and callout sync
1062 * to avoid impact of possible long unregister time.
1063 */
1064 if (cpu0_nmi_registered)
1065 unregister_nmi_handler(NMI_LOCAL, "wake_cpu0");
1066
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001067 return boot_error;
1068}
1069
Paul Gortmaker148f9bb2013-06-18 18:23:59 -04001070int native_cpu_up(unsigned int cpu, struct task_struct *tidle)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001071{
Ingo Molnara21769a42009-01-28 06:50:47 +01001072 int apicid = apic->cpu_present_to_apicid(cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001073 unsigned long flags;
1074 int err;
1075
1076 WARN_ON(irqs_disabled());
1077
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +02001078 pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001079
Fenghua Yu30106c12012-11-13 11:32:41 -08001080 if (apicid == BAD_APICID ||
Suresh Siddhac284b422011-12-21 17:45:19 -08001081 !physid_isset(apicid, phys_cpu_present_map) ||
Daniel J Bluemanfa630302012-03-14 15:17:34 +08001082 !apic->apic_id_valid(apicid)) {
Joe Perchesc767a542012-05-21 19:50:07 -07001083 pr_err("%s: bad cpu %d\n", __func__, cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001084 return -EINVAL;
1085 }
1086
1087 /*
1088 * Already booted CPU?
1089 */
Mike Travisc2d1cec2009-01-04 05:18:03 -08001090 if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +02001091 pr_debug("do_boot_cpu %d Already started\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001092 return -ENOSYS;
1093 }
1094
1095 /*
1096 * Save current MTRR state in case it was changed since early boot
1097 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
1098 */
1099 mtrr_save_state();
1100
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001101 /* x86 CPUs take themselves offline, so delayed offline is OK. */
1102 err = cpu_check_up_prepare(cpu);
1103 if (err && err != -EBUSY)
1104 return err;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001105
Vincent Palatin644c1542012-11-30 12:15:32 -08001106 /* the FPU context is blank, nobody can own it */
1107 __cpu_disable_lazy_restore(cpu);
1108
Boris Ostrovsky3f854832015-04-01 10:12:14 -04001109 common_cpu_up(cpu, tidle);
1110
Thomas Gleixnerce0d3c02015-07-14 22:03:57 +02001111 /*
1112 * We have to walk the irq descriptors to setup the vector
1113 * space for the cpu which comes online. Prevent irq
1114 * alloc/free across the bringup.
1115 */
1116 irq_lock_sparse();
1117
Thomas Gleixner7eb43a62012-04-20 13:05:48 +00001118 err = do_boot_cpu(apicid, cpu, tidle);
Thomas Gleixnerce0d3c02015-07-14 22:03:57 +02001119
Hugh Dickins61165d72008-05-13 14:26:57 +01001120 if (err) {
Thomas Gleixnerce0d3c02015-07-14 22:03:57 +02001121 irq_unlock_sparse();
Igor Mammedovfeef1e82014-06-05 15:42:44 +02001122 pr_err("do_boot_cpu failed(%d) to wakeup CPU#%u\n", err, cpu);
Hugh Dickins61165d72008-05-13 14:26:57 +01001123 return -EIO;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001124 }
1125
1126 /*
1127 * Check TSC synchronization with the AP (keep irqs disabled
1128 * while doing so):
1129 */
1130 local_irq_save(flags);
1131 check_tsc_sync_source(cpu);
1132 local_irq_restore(flags);
1133
Akinobu Mita7c04e642008-04-19 23:55:17 +09001134 while (!cpu_online(cpu)) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001135 cpu_relax();
1136 touch_nmi_watchdog();
1137 }
1138
Thomas Gleixnerce0d3c02015-07-14 22:03:57 +02001139 irq_unlock_sparse();
1140
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -03001141 return 0;
1142}
1143
Henrik Kretzschmar7167d082011-02-22 15:38:05 +01001144/**
1145 * arch_disable_smp_support() - disables SMP support for x86 at runtime
1146 */
1147void arch_disable_smp_support(void)
1148{
1149 disable_ioapic_support();
1150}
1151
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001152/*
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001153 * Fall back to non SMP mode after errors.
1154 *
1155 * RED-PEN audit/test this more. I bet there is more state messed up here.
1156 */
1157static __init void disable_smp(void)
1158{
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001159 pr_info("SMP disabled\n");
1160
Thomas Gleixneref4c59a2015-01-15 21:22:35 +00001161 disable_ioapic_support();
1162
Rusty Russell4f062892009-03-13 14:49:54 +10301163 init_cpu_present(cpumask_of(0));
1164 init_cpu_possible(cpumask_of(0));
Glauber Costa0f385d12008-05-28 17:09:53 -07001165
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001166 if (smp_found_config)
Jack Steinerb6df1b82008-06-19 21:51:05 -05001167 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001168 else
Jack Steinerb6df1b82008-06-19 21:51:05 -05001169 physid_set_mask_of_physid(0, &phys_cpu_present_map);
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001170 cpumask_set_cpu(0, topology_sibling_cpumask(0));
1171 cpumask_set_cpu(0, topology_core_cpumask(0));
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001172}
1173
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001174enum {
1175 SMP_OK,
1176 SMP_NO_CONFIG,
1177 SMP_NO_APIC,
1178 SMP_FORCE_UP,
1179};
1180
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001181/*
1182 * Various sanity checks.
1183 */
1184static int __init smp_sanity_check(unsigned max_cpus)
1185{
Jack Steinerac23d4e2008-03-28 14:12:16 -05001186 preempt_disable();
Yinghai Lua58f03b2008-08-14 02:16:30 -07001187
Yinghai Lu1ff2f202009-01-29 19:30:04 -08001188#if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
Yinghai Lua58f03b2008-08-14 02:16:30 -07001189 if (def_to_bigsmp && nr_cpu_ids > 8) {
1190 unsigned int cpu;
1191 unsigned nr;
1192
Joe Perchesc767a542012-05-21 19:50:07 -07001193 pr_warn("More than 8 CPUs detected - skipping them\n"
1194 "Use CONFIG_X86_BIGSMP\n");
Yinghai Lua58f03b2008-08-14 02:16:30 -07001195
1196 nr = 0;
1197 for_each_present_cpu(cpu) {
1198 if (nr >= 8)
Mike Travisc2d1cec2009-01-04 05:18:03 -08001199 set_cpu_present(cpu, false);
Yinghai Lua58f03b2008-08-14 02:16:30 -07001200 nr++;
1201 }
1202
1203 nr = 0;
1204 for_each_possible_cpu(cpu) {
1205 if (nr >= 8)
Mike Travisc2d1cec2009-01-04 05:18:03 -08001206 set_cpu_possible(cpu, false);
Yinghai Lua58f03b2008-08-14 02:16:30 -07001207 nr++;
1208 }
1209
1210 nr_cpu_ids = 8;
1211 }
1212#endif
1213
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001214 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
Joe Perchesc767a542012-05-21 19:50:07 -07001215 pr_warn("weird, boot CPU (#%d) not listed by the BIOS\n",
Michael Tokarev55c395b2008-12-05 14:42:20 +03001216 hard_smp_processor_id());
1217
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001218 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1219 }
1220
1221 /*
1222 * If we couldn't find an SMP configuration at boot time,
1223 * get out of here now!
1224 */
1225 if (!smp_found_config && !acpi_lapic) {
Jack Steinerac23d4e2008-03-28 14:12:16 -05001226 preempt_enable();
Joe Perchesc767a542012-05-21 19:50:07 -07001227 pr_notice("SMP motherboard not detected\n");
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001228 return SMP_NO_CONFIG;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001229 }
1230
1231 /*
1232 * Should not be necessary because the MP table should list the boot
1233 * CPU too, but we do it for the sake of robustness anyway.
1234 */
Ingo Molnara27a6212009-01-28 12:43:18 +01001235 if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
Joe Perchesc767a542012-05-21 19:50:07 -07001236 pr_notice("weird, boot CPU (#%d) not listed by the BIOS\n",
1237 boot_cpu_physical_apicid);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001238 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1239 }
Jack Steinerac23d4e2008-03-28 14:12:16 -05001240 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001241
1242 /*
1243 * If we couldn't find a local APIC, then get out of here now!
1244 */
1245 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) &&
Borislav Petkov93984fb2016-04-04 22:25:00 +02001246 !boot_cpu_has(X86_FEATURE_APIC)) {
Cyrill Gorcunov103428e2009-06-07 16:48:40 +04001247 if (!disable_apic) {
1248 pr_err("BIOS bug, local APIC #%d not detected!...\n",
1249 boot_cpu_physical_apicid);
Joe Perchesc767a542012-05-21 19:50:07 -07001250 pr_err("... forcing use of dummy APIC emulation (tell your hw vendor)\n");
Cyrill Gorcunov103428e2009-06-07 16:48:40 +04001251 }
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001252 return SMP_NO_APIC;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001253 }
1254
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001255 /*
1256 * If SMP should be disabled, then really disable it!
1257 */
1258 if (!max_cpus) {
Joe Perchesc767a542012-05-21 19:50:07 -07001259 pr_info("SMP mode deactivated\n");
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001260 return SMP_FORCE_UP;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001261 }
1262
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001263 return SMP_OK;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001264}
1265
1266static void __init smp_cpu_index_default(void)
1267{
1268 int i;
1269 struct cpuinfo_x86 *c;
1270
Akinobu Mita7c04e642008-04-19 23:55:17 +09001271 for_each_possible_cpu(i) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001272 c = &cpu_data(i);
1273 /* mark all to hotplug */
Mike Travis96289372008-12-31 18:08:46 -08001274 c->cpu_index = nr_cpu_ids;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001275 }
1276}
1277
1278/*
1279 * Prepare for SMP bootup. The MP table or ACPI has been read
1280 * earlier. Just do some sanity checking here and enable APIC mode.
1281 */
1282void __init native_smp_prepare_cpus(unsigned int max_cpus)
1283{
Rusty Russell7ad728f2009-03-13 14:49:50 +10301284 unsigned int i;
1285
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001286 smp_cpu_index_default();
Yinghai Lu792363d2011-01-21 15:29:54 -08001287
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001288 /*
1289 * Setup boot CPU information
1290 */
Fenghua Yu30106c12012-11-13 11:32:41 -08001291 smp_store_boot_cpu_info(); /* Final full version of the data */
Yinghai Lu792363d2011-01-21 15:29:54 -08001292 cpumask_copy(cpu_callin_mask, cpumask_of(0));
1293 mb();
Tejun Heobd22a2f2011-01-23 14:37:27 +01001294
Rusty Russell7ad728f2009-03-13 14:49:50 +10301295 for_each_possible_cpu(i) {
Li Zefan79f55992009-06-15 14:58:26 +08001296 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
1297 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
Yinghai Lub3d73362011-01-21 15:29:44 -08001298 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
Rusty Russell7ad728f2009-03-13 14:49:50 +10301299 }
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001300 set_cpu_sibling_map(0);
1301
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001302 switch (smp_sanity_check(max_cpus)) {
1303 case SMP_NO_CONFIG:
1304 disable_smp();
1305 if (APIC_init_uniprocessor())
1306 pr_notice("Local APIC not detected. Using dummy APIC emulation.\n");
1307 return;
1308 case SMP_NO_APIC:
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001309 disable_smp();
Thomas Gleixner250a1ac2014-12-05 08:48:29 +00001310 return;
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001311 case SMP_FORCE_UP:
1312 disable_smp();
Thomas Gleixner374aab32015-01-15 21:22:44 +00001313 apic_bsp_setup(false);
Thomas Gleixner613c25ef2015-01-15 21:22:42 +00001314 return;
1315 case SMP_OK:
1316 break;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001317 }
1318
Suresh Siddhafa47f7e2010-08-27 11:09:50 -07001319 default_setup_apic_routing();
1320
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001321 if (read_apic_id() != boot_cpu_physical_apicid) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001322 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001323 read_apic_id(), boot_cpu_physical_apicid);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001324 /* Or can we switch back to PIC here? */
1325 }
1326
Thomas Gleixner374aab32015-01-15 21:22:44 +00001327 cpu0_logical_apicid = apic_bsp_setup(false);
Thomas Gleixneref4c59a2015-01-15 21:22:35 +00001328
Joe Perchesc767a542012-05-21 19:50:07 -07001329 pr_info("CPU%d: ", 0);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001330 print_cpu_info(&cpu_data(0));
Marcin Slusarzc4bd1fd2008-08-21 20:49:05 +02001331
1332 if (is_uv_system())
1333 uv_system_init();
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001334
1335 set_mtrr_aps_delayed_init();
Len Brown1a744cb2015-05-11 17:27:10 -04001336
1337 smp_quirk_init_udelay();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001338}
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001339
1340void arch_enable_nonboot_cpus_begin(void)
1341{
1342 set_mtrr_aps_delayed_init();
1343}
1344
1345void arch_enable_nonboot_cpus_end(void)
1346{
1347 mtrr_aps_init();
1348}
1349
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001350/*
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001351 * Early setup to make printk work.
1352 */
1353void __init native_smp_prepare_boot_cpu(void)
1354{
1355 int me = smp_processor_id();
Brian Gerst552be872009-01-30 17:47:53 +09001356 switch_to_new_gdt(me);
Mike Travisc2d1cec2009-01-04 05:18:03 -08001357 /* already set me in cpu_online_mask in boot_cpu_init() */
1358 cpumask_set_cpu(me, cpu_callout_mask);
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001359 cpu_set_state_online(me);
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001360}
1361
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001362void __init native_smp_cpus_done(unsigned int max_cpus)
1363{
Joe Perchesc767a542012-05-21 19:50:07 -07001364 pr_debug("Boot done\n");
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001365
Don Zickus99e8b9c2011-10-13 15:14:26 -04001366 nmi_selftest();
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001367 impress_friends();
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001368 setup_ioapic_dest();
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001369 mtrr_aps_init();
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001370}
1371
Mike Travis3b11ce72008-12-17 15:21:39 -08001372static int __initdata setup_possible_cpus = -1;
1373static int __init _setup_possible_cpus(char *str)
1374{
1375 get_option(&str, &setup_possible_cpus);
1376 return 0;
1377}
1378early_param("possible_cpus", _setup_possible_cpus);
1379
1380
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001381/*
Rusty Russell4f062892009-03-13 14:49:54 +10301382 * cpu_possible_mask should be static, it cannot change as cpu's
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001383 * are onlined, or offlined. The reason is per-cpu data-structures
1384 * are allocated by some modules at init time, and dont expect to
1385 * do this dynamically on cpu arrival/departure.
Rusty Russell4f062892009-03-13 14:49:54 +10301386 * cpu_present_mask on the other hand can change dynamically.
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001387 * In case when cpu_hotplug is not compiled, then we resort to current
1388 * behaviour, which is cpu_possible == cpu_present.
1389 * - Ashok Raj
1390 *
1391 * Three ways to find out the number of additional hotplug CPUs:
1392 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Mike Travis3b11ce72008-12-17 15:21:39 -08001393 * - The user can overwrite it with possible_cpus=NUM
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001394 * - Otherwise don't reserve additional CPUs.
1395 * We do this because additional CPUs waste a lot of memory.
1396 * -AK
1397 */
1398__init void prefill_possible_map(void)
1399{
Thomas Gleixnercb48bb52008-10-05 17:51:52 +02001400 int i, possible;
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001401
Yinghai Lu329513a2008-07-02 18:54:40 -07001402 /* no processor from mptable or madt */
1403 if (!num_processors)
1404 num_processors = 1;
1405
Jan Beulich5f2eb552010-05-24 12:13:17 -07001406 i = setup_max_cpus ?: 1;
1407 if (setup_possible_cpus == -1) {
1408 possible = num_processors;
1409#ifdef CONFIG_HOTPLUG_CPU
1410 if (setup_max_cpus)
1411 possible += disabled_cpus;
1412#else
1413 if (possible > i)
1414 possible = i;
1415#endif
1416 } else
Mike Travis3b11ce72008-12-17 15:21:39 -08001417 possible = setup_possible_cpus;
1418
Mike Travis730cf272008-12-31 18:08:45 -08001419 total_cpus = max_t(int, possible, num_processors + disabled_cpus);
1420
Yinghai Lu2b633e32010-02-10 01:20:37 -08001421 /* nr_cpu_ids could be reduced via nr_cpus= */
1422 if (possible > nr_cpu_ids) {
Joe Perchesc767a542012-05-21 19:50:07 -07001423 pr_warn("%d Processors exceeds NR_CPUS limit of %d\n",
Yinghai Lu2b633e32010-02-10 01:20:37 -08001424 possible, nr_cpu_ids);
1425 possible = nr_cpu_ids;
Mike Travis3b11ce72008-12-17 15:21:39 -08001426 }
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001427
Jan Beulich5f2eb552010-05-24 12:13:17 -07001428#ifdef CONFIG_HOTPLUG_CPU
1429 if (!setup_max_cpus)
1430#endif
1431 if (possible > i) {
Joe Perchesc767a542012-05-21 19:50:07 -07001432 pr_warn("%d Processors exceeds max_cpus limit of %u\n",
Jan Beulich5f2eb552010-05-24 12:13:17 -07001433 possible, setup_max_cpus);
1434 possible = i;
1435 }
1436
Joe Perchesc767a542012-05-21 19:50:07 -07001437 pr_info("Allowing %d CPUs, %d hotplug CPUs\n",
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001438 possible, max_t(int, possible - num_processors, 0));
1439
1440 for (i = 0; i < possible; i++)
Mike Travisc2d1cec2009-01-04 05:18:03 -08001441 set_cpu_possible(i, true);
Jan Beulich5f2eb552010-05-24 12:13:17 -07001442 for (; i < NR_CPUS; i++)
1443 set_cpu_possible(i, false);
Mike Travis3461b0a2008-05-12 21:21:13 +02001444
1445 nr_cpu_ids = possible;
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001446}
Glauber Costa69c18c12008-03-03 14:13:07 -03001447
Chuck Ebbert14adf852008-09-29 18:29:42 -04001448#ifdef CONFIG_HOTPLUG_CPU
1449
Andi Kleen70b83012016-05-19 17:09:55 -07001450/* Recompute SMT state for all CPUs on offline */
1451static void recompute_smt_state(void)
1452{
1453 int max_threads, cpu;
1454
1455 max_threads = 0;
1456 for_each_online_cpu (cpu) {
1457 int threads = cpumask_weight(topology_sibling_cpumask(cpu));
1458
1459 if (threads > max_threads)
1460 max_threads = threads;
1461 }
1462 __max_smt_threads = max_threads;
1463}
1464
Chuck Ebbert14adf852008-09-29 18:29:42 -04001465static void remove_siblinginfo(int cpu)
1466{
1467 int sibling;
1468 struct cpuinfo_x86 *c = &cpu_data(cpu);
1469
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001470 for_each_cpu(sibling, topology_core_cpumask(cpu)) {
1471 cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
Chuck Ebbert14adf852008-09-29 18:29:42 -04001472 /*/
1473 * last thread sibling in this cpu core going down
1474 */
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001475 if (cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
Chuck Ebbert14adf852008-09-29 18:29:42 -04001476 cpu_data(sibling).booted_cores--;
1477 }
1478
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001479 for_each_cpu(sibling, topology_sibling_cpumask(cpu))
1480 cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
Wanpeng Li03bd4e12014-09-24 16:38:05 +08001481 for_each_cpu(sibling, cpu_llc_shared_mask(cpu))
1482 cpumask_clear_cpu(cpu, cpu_llc_shared_mask(sibling));
1483 cpumask_clear(cpu_llc_shared_mask(cpu));
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +02001484 cpumask_clear(topology_sibling_cpumask(cpu));
1485 cpumask_clear(topology_core_cpumask(cpu));
Chuck Ebbert14adf852008-09-29 18:29:42 -04001486 c->phys_proc_id = 0;
1487 c->cpu_core_id = 0;
Mike Travisc2d1cec2009-01-04 05:18:03 -08001488 cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
Andi Kleen70b83012016-05-19 17:09:55 -07001489 recompute_smt_state();
Chuck Ebbert14adf852008-09-29 18:29:42 -04001490}
1491
Mathias Krause4daa8322015-07-20 18:32:53 +02001492static void remove_cpu_from_maps(int cpu)
Glauber Costa69c18c12008-03-03 14:13:07 -03001493{
Mike Travisc2d1cec2009-01-04 05:18:03 -08001494 set_cpu_online(cpu, false);
1495 cpumask_clear_cpu(cpu, cpu_callout_mask);
1496 cpumask_clear_cpu(cpu, cpu_callin_mask);
Glauber Costa69c18c12008-03-03 14:13:07 -03001497 /* was set by cpu_init() */
Mike Travisc2d1cec2009-01-04 05:18:03 -08001498 cpumask_clear_cpu(cpu, cpu_initialized_mask);
Mike Travis23ca4bb2008-05-12 21:21:12 +02001499 numa_remove_cpu(cpu);
Glauber Costa69c18c12008-03-03 14:13:07 -03001500}
1501
Alex Nixon8227dce2008-08-22 11:52:14 +01001502void cpu_disable_common(void)
1503{
1504 int cpu = smp_processor_id();
Alex Nixon8227dce2008-08-22 11:52:14 +01001505
Alex Nixon8227dce2008-08-22 11:52:14 +01001506 remove_siblinginfo(cpu);
1507
1508 /* It's now safe to remove this processor from the online map */
1509 lock_vector_lock();
1510 remove_cpu_from_maps(cpu);
1511 unlock_vector_lock();
Mike Travisd7b381b2008-12-16 17:33:58 -08001512 fixup_irqs();
Alex Nixon8227dce2008-08-22 11:52:14 +01001513}
1514
Alex Nixon93be71b2008-08-22 11:52:11 +01001515int native_cpu_disable(void)
Glauber Costa69c18c12008-03-03 14:13:07 -03001516{
Prarit Bhargavada6139e2014-01-13 06:51:01 -05001517 int ret;
1518
1519 ret = check_irq_vectors_for_cpu_disable();
1520 if (ret)
1521 return ret;
1522
Glauber Costa69c18c12008-03-03 14:13:07 -03001523 clear_local_APIC();
Alex Nixon8227dce2008-08-22 11:52:14 +01001524 cpu_disable_common();
Lan Tianyu2ed53c02014-08-26 15:43:45 +08001525
Glauber Costa69c18c12008-03-03 14:13:07 -03001526 return 0;
1527}
1528
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001529int common_cpu_die(unsigned int cpu)
Boris Ostrovsky54279552014-10-31 11:49:32 -04001530{
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001531 int ret = 0;
Boris Ostrovsky54279552014-10-31 11:49:32 -04001532
Glauber Costa69c18c12008-03-03 14:13:07 -03001533 /* We don't do anything here: idle task is faking death itself. */
Boris Ostrovsky54279552014-10-31 11:49:32 -04001534
Lan Tianyu2ed53c02014-08-26 15:43:45 +08001535 /* They ack this in play_dead() by setting CPU_DEAD */
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001536 if (cpu_wait_death(cpu, 5)) {
Lan Tianyu2ed53c02014-08-26 15:43:45 +08001537 if (system_state == SYSTEM_RUNNING)
1538 pr_info("CPU %u is now offline\n", cpu);
1539 } else {
1540 pr_err("CPU %u didn't die...\n", cpu);
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001541 ret = -1;
Glauber Costa69c18c12008-03-03 14:13:07 -03001542 }
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001543
1544 return ret;
1545}
1546
1547void native_cpu_die(unsigned int cpu)
1548{
1549 common_cpu_die(cpu);
Glauber Costa69c18c12008-03-03 14:13:07 -03001550}
Alex Nixona21f5d882008-08-22 11:52:13 +01001551
1552void play_dead_common(void)
1553{
1554 idle_task_exit();
1555 reset_lazy_tlbstate();
Len Brown02c68a02011-04-01 16:59:53 -04001556 amd_e400_remove_cpu(raw_smp_processor_id());
Alex Nixona21f5d882008-08-22 11:52:13 +01001557
Alex Nixona21f5d882008-08-22 11:52:13 +01001558 /* Ack it */
Paul E. McKenney2a442c92015-02-25 11:42:15 -08001559 (void)cpu_report_death();
Alex Nixona21f5d882008-08-22 11:52:13 +01001560
1561 /*
1562 * With physical CPU hotplug, we should halt the cpu
1563 */
1564 local_irq_disable();
1565}
1566
Fenghua Yue1c467e2012-11-14 04:36:53 -08001567static bool wakeup_cpu0(void)
1568{
1569 if (smp_processor_id() == 0 && enable_start_cpu0)
1570 return true;
1571
1572 return false;
1573}
1574
H. Peter Anvinea530692010-09-17 15:39:11 -07001575/*
1576 * We need to flush the caches before going to sleep, lest we have
1577 * dirty data in our caches when we come back up.
1578 */
1579static inline void mwait_play_dead(void)
1580{
1581 unsigned int eax, ebx, ecx, edx;
1582 unsigned int highest_cstate = 0;
1583 unsigned int highest_subcstate = 0;
H. Peter Anvince5f6822010-09-20 13:04:45 -07001584 void *mwait_ptr;
Borislav Petkov576cfb42013-03-04 21:16:16 +01001585 int i;
H. Peter Anvinea530692010-09-17 15:39:11 -07001586
Len Brown69fb3672013-02-10 01:38:39 -05001587 if (!this_cpu_has(X86_FEATURE_MWAIT))
H. Peter Anvinea530692010-09-17 15:39:11 -07001588 return;
H. Peter Anvin840d2832014-02-27 08:31:30 -08001589 if (!this_cpu_has(X86_FEATURE_CLFLUSH))
H. Peter Anvince5f6822010-09-20 13:04:45 -07001590 return;
Tejun Heo7b543a52010-12-18 16:30:05 +01001591 if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
H. Peter Anvinea530692010-09-17 15:39:11 -07001592 return;
1593
1594 eax = CPUID_MWAIT_LEAF;
1595 ecx = 0;
1596 native_cpuid(&eax, &ebx, &ecx, &edx);
1597
1598 /*
1599 * eax will be 0 if EDX enumeration is not valid.
1600 * Initialized below to cstate, sub_cstate value when EDX is valid.
1601 */
1602 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
1603 eax = 0;
1604 } else {
1605 edx >>= MWAIT_SUBSTATE_SIZE;
1606 for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
1607 if (edx & MWAIT_SUBSTATE_MASK) {
1608 highest_cstate = i;
1609 highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
1610 }
1611 }
1612 eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
1613 (highest_subcstate - 1);
1614 }
1615
H. Peter Anvince5f6822010-09-20 13:04:45 -07001616 /*
1617 * This should be a memory location in a cache line which is
1618 * unlikely to be touched by other processors. The actual
1619 * content is immaterial as it is not actually modified in any way.
1620 */
1621 mwait_ptr = &current_thread_info()->flags;
1622
H. Peter Anvina68e5c92010-09-17 17:06:46 -07001623 wbinvd();
1624
H. Peter Anvinea530692010-09-17 15:39:11 -07001625 while (1) {
H. Peter Anvince5f6822010-09-20 13:04:45 -07001626 /*
1627 * The CLFLUSH is a workaround for erratum AAI65 for
1628 * the Xeon 7400 series. It's not clear it is actually
1629 * needed, but it should be harmless in either case.
1630 * The WBINVD is insufficient due to the spurious-wakeup
1631 * case where we return around the loop.
1632 */
H. Peter Anvin7d590cc2013-12-19 12:30:03 -08001633 mb();
H. Peter Anvince5f6822010-09-20 13:04:45 -07001634 clflush(mwait_ptr);
H. Peter Anvin7d590cc2013-12-19 12:30:03 -08001635 mb();
H. Peter Anvince5f6822010-09-20 13:04:45 -07001636 __monitor(mwait_ptr, 0, 0);
H. Peter Anvinea530692010-09-17 15:39:11 -07001637 mb();
1638 __mwait(eax, 0);
Fenghua Yue1c467e2012-11-14 04:36:53 -08001639 /*
1640 * If NMI wants to wake up CPU0, start CPU0.
1641 */
1642 if (wakeup_cpu0())
1643 start_cpu0();
H. Peter Anvinea530692010-09-17 15:39:11 -07001644 }
1645}
1646
Rafael J. Wysocki406f9922016-07-14 03:55:23 +02001647void hlt_play_dead(void)
H. Peter Anvinea530692010-09-17 15:39:11 -07001648{
Tejun Heo7b543a52010-12-18 16:30:05 +01001649 if (__this_cpu_read(cpu_info.x86) >= 4)
H. Peter Anvina68e5c92010-09-17 17:06:46 -07001650 wbinvd();
1651
H. Peter Anvinea530692010-09-17 15:39:11 -07001652 while (1) {
H. Peter Anvinea530692010-09-17 15:39:11 -07001653 native_halt();
Fenghua Yue1c467e2012-11-14 04:36:53 -08001654 /*
1655 * If NMI wants to wake up CPU0, start CPU0.
1656 */
1657 if (wakeup_cpu0())
1658 start_cpu0();
H. Peter Anvinea530692010-09-17 15:39:11 -07001659 }
1660}
1661
Alex Nixona21f5d882008-08-22 11:52:13 +01001662void native_play_dead(void)
1663{
1664 play_dead_common();
Joseph Cihula86886e52009-06-30 19:31:07 -07001665 tboot_shutdown(TB_SHUTDOWN_WFS);
H. Peter Anvinea530692010-09-17 15:39:11 -07001666
1667 mwait_play_dead(); /* Only returns on failure */
Boris Ostrovsky1a022e32012-03-13 19:55:09 +01001668 if (cpuidle_play_dead())
1669 hlt_play_dead();
Alex Nixona21f5d882008-08-22 11:52:13 +01001670}
1671
Glauber Costa69c18c12008-03-03 14:13:07 -03001672#else /* ... !CONFIG_HOTPLUG_CPU */
Alex Nixon93be71b2008-08-22 11:52:11 +01001673int native_cpu_disable(void)
Glauber Costa69c18c12008-03-03 14:13:07 -03001674{
1675 return -ENOSYS;
1676}
1677
Alex Nixon93be71b2008-08-22 11:52:11 +01001678void native_cpu_die(unsigned int cpu)
Glauber Costa69c18c12008-03-03 14:13:07 -03001679{
1680 /* We said "no" in __cpu_disable */
1681 BUG();
1682}
Alex Nixona21f5d882008-08-22 11:52:13 +01001683
1684void native_play_dead(void)
1685{
1686 BUG();
1687}
1688
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001689#endif