mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / x86 / kernel / smpboot.c
blob66f2a950935a51cbff1d834aaec575b00e832d20
1 /*
2 * x86 SMP booting functions
4 * (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
5 * (c) 1998, 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
6 * Copyright 2001 Andi Kleen, SuSE Labs.
8 * Much of the core SMP work is based on previous work by Thomas Radke, to
9 * whom a great many thanks are extended.
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.
15 * This code is released under the GNU General Public License version 2 or
16 * later.
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
42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44 #include <linux/init.h>
45 #include <linux/smp.h>
46 #include <linux/export.h>
47 #include <linux/sched.h>
48 #include <linux/sched/topology.h>
49 #include <linux/sched/hotplug.h>
50 #include <linux/sched/task_stack.h>
51 #include <linux/percpu.h>
52 #include <linux/bootmem.h>
53 #include <linux/err.h>
54 #include <linux/nmi.h>
55 #include <linux/tboot.h>
56 #include <linux/stackprotector.h>
57 #include <linux/gfp.h>
58 #include <linux/cpuidle.h>
60 #include <asm/acpi.h>
61 #include <asm/desc.h>
62 #include <asm/nmi.h>
63 #include <asm/irq.h>
64 #include <asm/realmode.h>
65 #include <asm/cpu.h>
66 #include <asm/numa.h>
67 #include <asm/pgtable.h>
68 #include <asm/tlbflush.h>
69 #include <asm/mtrr.h>
70 #include <asm/mwait.h>
71 #include <asm/apic.h>
72 #include <asm/io_apic.h>
73 #include <asm/fpu/internal.h>
74 #include <asm/setup.h>
75 #include <asm/uv/uv.h>
76 #include <linux/mc146818rtc.h>
77 #include <asm/i8259.h>
78 #include <asm/realmode.h>
79 #include <asm/misc.h>
80 #include <asm/spec-ctrl.h>
81 #include <asm/hw_irq.h>
83 /* representing HT siblings of each logical CPU */
84 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
85 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
87 /* representing HT and core siblings of each logical CPU */
88 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
89 EXPORT_PER_CPU_SYMBOL(cpu_core_map);
91 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
93 /* Per CPU bogomips and other parameters */
94 DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
95 EXPORT_PER_CPU_SYMBOL(cpu_info);
97 /* Logical package management. We might want to allocate that dynamically */
98 static int *physical_to_logical_pkg __read_mostly;
99 static unsigned long *physical_package_map __read_mostly;;
100 static unsigned int max_physical_pkg_id __read_mostly;
101 unsigned int __max_logical_packages __read_mostly;
102 EXPORT_SYMBOL(__max_logical_packages);
103 static unsigned int logical_packages __read_mostly;
105 /* Maximum number of SMT threads on any online core */
106 int __max_smt_threads __read_mostly;
108 /* Flag to indicate if a complete sched domain rebuild is required */
109 bool x86_topology_update;
111 int arch_update_cpu_topology(void)
113 int retval = x86_topology_update;
115 x86_topology_update = false;
116 return retval;
119 static inline void smpboot_setup_warm_reset_vector(unsigned long start_eip)
121 unsigned long flags;
123 spin_lock_irqsave(&rtc_lock, flags);
124 CMOS_WRITE(0xa, 0xf);
125 spin_unlock_irqrestore(&rtc_lock, flags);
126 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_HIGH)) =
127 start_eip >> 4;
128 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) =
129 start_eip & 0xf;
132 static inline void smpboot_restore_warm_reset_vector(void)
134 unsigned long flags;
137 * Paranoid: Set warm reset code and vector here back
138 * to default values.
140 spin_lock_irqsave(&rtc_lock, flags);
141 CMOS_WRITE(0, 0xf);
142 spin_unlock_irqrestore(&rtc_lock, flags);
144 *((volatile u32 *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) = 0;
148 * Report back to the Boot Processor during boot time or to the caller processor
149 * during CPU online.
151 static void smp_callin(void)
153 int cpuid, phys_id;
156 * If waken up by an INIT in an 82489DX configuration
157 * cpu_callout_mask guarantees we don't get here before
158 * an INIT_deassert IPI reaches our local APIC, so it is
159 * now safe to touch our local APIC.
161 cpuid = smp_processor_id();
164 * (This works even if the APIC is not enabled.)
166 phys_id = read_apic_id();
169 * the boot CPU has finished the init stage and is spinning
170 * on callin_map until we finish. We are free to set up this
171 * CPU, first the APIC. (this is probably redundant on most
172 * boards)
174 apic_ap_setup();
177 * Save our processor parameters. Note: this information
178 * is needed for clock calibration.
180 smp_store_cpu_info(cpuid);
183 * The topology information must be up to date before
184 * calibrate_delay() and notify_cpu_starting().
186 set_cpu_sibling_map(raw_smp_processor_id());
189 * Get our bogomips.
190 * Update loops_per_jiffy in cpu_data. Previous call to
191 * smp_store_cpu_info() stored a value that is close but not as
192 * accurate as the value just calculated.
194 calibrate_delay();
195 cpu_data(cpuid).loops_per_jiffy = loops_per_jiffy;
196 pr_debug("Stack at about %p\n", &cpuid);
198 wmb();
200 notify_cpu_starting(cpuid);
203 * Allow the master to continue.
205 cpumask_set_cpu(cpuid, cpu_callin_mask);
208 static int cpu0_logical_apicid;
209 static int enable_start_cpu0;
211 * Activate a secondary processor.
213 static void notrace start_secondary(void *unused)
216 * Don't put *anything* except direct CPU state initialization
217 * before cpu_init(), SMP booting is too fragile that we want to
218 * limit the things done here to the most necessary things.
220 if (boot_cpu_has(X86_FEATURE_PCID))
221 __write_cr4(__read_cr4() | X86_CR4_PCIDE);
223 #ifdef CONFIG_X86_32
224 /* switch away from the initial page table */
225 load_cr3(swapper_pg_dir);
227 * Initialize the CR4 shadow before doing anything that could
228 * try to read it.
230 cr4_init_shadow();
231 __flush_tlb_all();
232 #endif
233 load_current_idt();
234 cpu_init();
235 x86_cpuinit.early_percpu_clock_init();
236 preempt_disable();
237 smp_callin();
239 enable_start_cpu0 = 0;
241 /* otherwise gcc will move up smp_processor_id before the cpu_init */
242 barrier();
244 * Check TSC synchronization with the BP:
246 check_tsc_sync_target();
248 speculative_store_bypass_ht_init();
251 * Lock vector_lock and initialize the vectors on this cpu
252 * before setting the cpu online. We must set it online with
253 * vector_lock held to prevent a concurrent setup/teardown
254 * from seeing a half valid vector space.
256 lock_vector_lock();
257 setup_vector_irq(smp_processor_id());
258 set_cpu_online(smp_processor_id(), true);
259 unlock_vector_lock();
260 cpu_set_state_online(smp_processor_id());
261 x86_platform.nmi_init();
263 /* enable local interrupts */
264 local_irq_enable();
266 /* to prevent fake stack check failure in clock setup */
267 boot_init_stack_canary();
269 x86_cpuinit.setup_percpu_clockev();
271 wmb();
272 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
275 * Prevent tail call to cpu_startup_entry() because the stack protector
276 * guard has been changed a couple of function calls up, in
277 * boot_init_stack_canary() and must not be checked before tail calling
278 * another function.
280 prevent_tail_call_optimization();
284 * topology_update_package_map - Update the physical to logical package map
285 * @pkg: The physical package id as retrieved via CPUID
286 * @cpu: The cpu for which this is updated
288 int topology_update_package_map(unsigned int pkg, unsigned int cpu)
290 unsigned int new;
292 /* Called from early boot ? */
293 if (!physical_package_map)
294 return 0;
296 if (pkg >= max_physical_pkg_id)
297 return -EINVAL;
299 /* Set the logical package id */
300 if (test_and_set_bit(pkg, physical_package_map))
301 goto found;
303 if (logical_packages >= __max_logical_packages) {
304 pr_warn("Package %u of CPU %u exceeds BIOS package data %u.\n",
305 logical_packages, cpu, __max_logical_packages);
306 return -ENOSPC;
309 new = logical_packages++;
310 if (new != pkg) {
311 pr_info("CPU %u Converting physical %u to logical package %u\n",
312 cpu, pkg, new);
314 physical_to_logical_pkg[pkg] = new;
316 found:
317 cpu_data(cpu).logical_proc_id = physical_to_logical_pkg[pkg];
318 return 0;
322 * topology_is_primary_thread - Check whether CPU is the primary SMT thread
323 * @cpu: CPU to check
325 bool topology_is_primary_thread(unsigned int cpu)
327 return apic_id_is_primary_thread(per_cpu(x86_cpu_to_apicid, cpu));
331 * topology_smt_supported - Check whether SMT is supported by the CPUs
333 bool topology_smt_supported(void)
335 return smp_num_siblings > 1;
339 * topology_phys_to_logical_pkg - Map a physical package id to a logical
341 * Returns logical package id or -1 if not found
343 int topology_phys_to_logical_pkg(unsigned int phys_pkg)
345 if (phys_pkg >= max_physical_pkg_id)
346 return -1;
347 return physical_to_logical_pkg[phys_pkg];
349 EXPORT_SYMBOL(topology_phys_to_logical_pkg);
351 static void __init smp_init_package_map(struct cpuinfo_x86 *c, unsigned int cpu)
353 unsigned int ncpus;
354 size_t size;
357 * Today neither Intel nor AMD support heterogenous systems. That
358 * might change in the future....
360 * While ideally we'd want '* smp_num_siblings' in the below @ncpus
361 * computation, this won't actually work since some Intel BIOSes
362 * report inconsistent HT data when they disable HT.
364 * In particular, they reduce the APIC-IDs to only include the cores,
365 * but leave the CPUID topology to say there are (2) siblings.
366 * This means we don't know how many threads there will be until
367 * after the APIC enumeration.
369 * By not including this we'll sometimes over-estimate the number of
370 * logical packages by the amount of !present siblings, but this is
371 * still better than MAX_LOCAL_APIC.
373 * We use total_cpus not nr_cpu_ids because nr_cpu_ids can be limited
374 * on the command line leading to a similar issue as the HT disable
375 * problem because the hyperthreads are usually enumerated after the
376 * primary cores.
378 ncpus = boot_cpu_data.x86_max_cores;
379 if (!ncpus) {
380 pr_warn("x86_max_cores == zero !?!?");
381 ncpus = 1;
384 __max_logical_packages = DIV_ROUND_UP(total_cpus, ncpus);
385 logical_packages = 0;
388 * Possibly larger than what we need as the number of apic ids per
389 * package can be smaller than the actual used apic ids.
391 max_physical_pkg_id = DIV_ROUND_UP(MAX_LOCAL_APIC, ncpus);
392 size = max_physical_pkg_id * sizeof(unsigned int);
393 physical_to_logical_pkg = kmalloc(size, GFP_KERNEL);
394 memset(physical_to_logical_pkg, 0xff, size);
395 size = BITS_TO_LONGS(max_physical_pkg_id) * sizeof(unsigned long);
396 physical_package_map = kzalloc(size, GFP_KERNEL);
398 pr_info("Max logical packages: %u\n", __max_logical_packages);
400 topology_update_package_map(c->phys_proc_id, cpu);
403 void __init smp_store_boot_cpu_info(void)
405 int id = 0; /* CPU 0 */
406 struct cpuinfo_x86 *c = &cpu_data(id);
408 *c = boot_cpu_data;
409 c->cpu_index = id;
410 smp_init_package_map(c, id);
414 * The bootstrap kernel entry code has set these up. Save them for
415 * a given CPU
417 void smp_store_cpu_info(int id)
419 struct cpuinfo_x86 *c = &cpu_data(id);
421 *c = boot_cpu_data;
422 c->cpu_index = id;
424 * During boot time, CPU0 has this setup already. Save the info when
425 * bringing up AP or offlined CPU0.
427 identify_secondary_cpu(c);
430 static bool
431 topology_same_node(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
433 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
435 return (cpu_to_node(cpu1) == cpu_to_node(cpu2));
438 static bool
439 topology_sane(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o, const char *name)
441 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
443 return !WARN_ONCE(!topology_same_node(c, o),
444 "sched: CPU #%d's %s-sibling CPU #%d is not on the same node! "
445 "[node: %d != %d]. Ignoring dependency.\n",
446 cpu1, name, cpu2, cpu_to_node(cpu1), cpu_to_node(cpu2));
449 #define link_mask(mfunc, c1, c2) \
450 do { \
451 cpumask_set_cpu((c1), mfunc(c2)); \
452 cpumask_set_cpu((c2), mfunc(c1)); \
453 } while (0)
455 static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
457 if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
458 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
460 if (c->phys_proc_id == o->phys_proc_id &&
461 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2)) {
462 if (c->cpu_core_id == o->cpu_core_id)
463 return topology_sane(c, o, "smt");
465 if ((c->cu_id != 0xff) &&
466 (o->cu_id != 0xff) &&
467 (c->cu_id == o->cu_id))
468 return topology_sane(c, o, "smt");
471 } else if (c->phys_proc_id == o->phys_proc_id &&
472 c->cpu_core_id == o->cpu_core_id) {
473 return topology_sane(c, o, "smt");
476 return false;
479 static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
481 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
483 if (per_cpu(cpu_llc_id, cpu1) != BAD_APICID &&
484 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2))
485 return topology_sane(c, o, "llc");
487 return false;
491 * Unlike the other levels, we do not enforce keeping a
492 * multicore group inside a NUMA node. If this happens, we will
493 * discard the MC level of the topology later.
495 static bool match_die(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
497 if (c->phys_proc_id == o->phys_proc_id)
498 return true;
499 return false;
502 #if defined(CONFIG_SCHED_SMT) || defined(CONFIG_SCHED_MC)
503 static inline int x86_sched_itmt_flags(void)
505 return sysctl_sched_itmt_enabled ? SD_ASYM_PACKING : 0;
508 #ifdef CONFIG_SCHED_MC
509 static int x86_core_flags(void)
511 return cpu_core_flags() | x86_sched_itmt_flags();
513 #endif
514 #ifdef CONFIG_SCHED_SMT
515 static int x86_smt_flags(void)
517 return cpu_smt_flags() | x86_sched_itmt_flags();
519 #endif
520 #endif
522 static struct sched_domain_topology_level x86_numa_in_package_topology[] = {
523 #ifdef CONFIG_SCHED_SMT
524 { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
525 #endif
526 #ifdef CONFIG_SCHED_MC
527 { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
528 #endif
529 { NULL, },
532 static struct sched_domain_topology_level x86_topology[] = {
533 #ifdef CONFIG_SCHED_SMT
534 { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
535 #endif
536 #ifdef CONFIG_SCHED_MC
537 { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
538 #endif
539 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
540 { NULL, },
544 * Set if a package/die has multiple NUMA nodes inside.
545 * AMD Magny-Cours and Intel Cluster-on-Die have this.
547 static bool x86_has_numa_in_package;
549 void set_cpu_sibling_map(int cpu)
551 bool has_smt = smp_num_siblings > 1;
552 bool has_mp = has_smt || boot_cpu_data.x86_max_cores > 1;
553 struct cpuinfo_x86 *c = &cpu_data(cpu);
554 struct cpuinfo_x86 *o;
555 int i, threads;
557 cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
559 if (!has_mp) {
560 cpumask_set_cpu(cpu, topology_sibling_cpumask(cpu));
561 cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
562 cpumask_set_cpu(cpu, topology_core_cpumask(cpu));
563 c->booted_cores = 1;
564 return;
567 for_each_cpu(i, cpu_sibling_setup_mask) {
568 o = &cpu_data(i);
570 if ((i == cpu) || (has_smt && match_smt(c, o)))
571 link_mask(topology_sibling_cpumask, cpu, i);
573 if ((i == cpu) || (has_mp && match_llc(c, o)))
574 link_mask(cpu_llc_shared_mask, cpu, i);
579 * This needs a separate iteration over the cpus because we rely on all
580 * topology_sibling_cpumask links to be set-up.
582 for_each_cpu(i, cpu_sibling_setup_mask) {
583 o = &cpu_data(i);
585 if ((i == cpu) || (has_mp && match_die(c, o))) {
586 link_mask(topology_core_cpumask, cpu, i);
589 * Does this new cpu bringup a new core?
591 if (cpumask_weight(
592 topology_sibling_cpumask(cpu)) == 1) {
594 * for each core in package, increment
595 * the booted_cores for this new cpu
597 if (cpumask_first(
598 topology_sibling_cpumask(i)) == i)
599 c->booted_cores++;
601 * increment the core count for all
602 * the other cpus in this package
604 if (i != cpu)
605 cpu_data(i).booted_cores++;
606 } else if (i != cpu && !c->booted_cores)
607 c->booted_cores = cpu_data(i).booted_cores;
609 if (match_die(c, o) && !topology_same_node(c, o))
610 x86_has_numa_in_package = true;
613 threads = cpumask_weight(topology_sibling_cpumask(cpu));
614 if (threads > __max_smt_threads)
615 __max_smt_threads = threads;
618 /* maps the cpu to the sched domain representing multi-core */
619 const struct cpumask *cpu_coregroup_mask(int cpu)
621 return cpu_llc_shared_mask(cpu);
624 static void impress_friends(void)
626 int cpu;
627 unsigned long bogosum = 0;
629 * Allow the user to impress friends.
631 pr_debug("Before bogomips\n");
632 for_each_possible_cpu(cpu)
633 if (cpumask_test_cpu(cpu, cpu_callout_mask))
634 bogosum += cpu_data(cpu).loops_per_jiffy;
635 pr_info("Total of %d processors activated (%lu.%02lu BogoMIPS)\n",
636 num_online_cpus(),
637 bogosum/(500000/HZ),
638 (bogosum/(5000/HZ))%100);
640 pr_debug("Before bogocount - setting activated=1\n");
643 void __inquire_remote_apic(int apicid)
645 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
646 const char * const names[] = { "ID", "VERSION", "SPIV" };
647 int timeout;
648 u32 status;
650 pr_info("Inquiring remote APIC 0x%x...\n", apicid);
652 for (i = 0; i < ARRAY_SIZE(regs); i++) {
653 pr_info("... APIC 0x%x %s: ", apicid, names[i]);
656 * Wait for idle.
658 status = safe_apic_wait_icr_idle();
659 if (status)
660 pr_cont("a previous APIC delivery may have failed\n");
662 apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
664 timeout = 0;
665 do {
666 udelay(100);
667 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
668 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
670 switch (status) {
671 case APIC_ICR_RR_VALID:
672 status = apic_read(APIC_RRR);
673 pr_cont("%08x\n", status);
674 break;
675 default:
676 pr_cont("failed\n");
682 * The Multiprocessor Specification 1.4 (1997) example code suggests
683 * that there should be a 10ms delay between the BSP asserting INIT
684 * and de-asserting INIT, when starting a remote processor.
685 * But that slows boot and resume on modern processors, which include
686 * many cores and don't require that delay.
688 * Cmdline "init_cpu_udelay=" is available to over-ride this delay.
689 * Modern processor families are quirked to remove the delay entirely.
691 #define UDELAY_10MS_DEFAULT 10000
693 static unsigned int init_udelay = UINT_MAX;
695 static int __init cpu_init_udelay(char *str)
697 get_option(&str, &init_udelay);
699 return 0;
701 early_param("cpu_init_udelay", cpu_init_udelay);
703 static void __init smp_quirk_init_udelay(void)
705 /* if cmdline changed it from default, leave it alone */
706 if (init_udelay != UINT_MAX)
707 return;
709 /* if modern processor, use no delay */
710 if (((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 6)) ||
711 ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && (boot_cpu_data.x86 >= 0xF))) {
712 init_udelay = 0;
713 return;
715 /* else, use legacy delay */
716 init_udelay = UDELAY_10MS_DEFAULT;
720 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
721 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
722 * won't ... remember to clear down the APIC, etc later.
725 wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip)
727 unsigned long send_status, accept_status = 0;
728 int maxlvt;
730 /* Target chip */
731 /* Boot on the stack */
732 /* Kick the second */
733 apic_icr_write(APIC_DM_NMI | apic->dest_logical, apicid);
735 pr_debug("Waiting for send to finish...\n");
736 send_status = safe_apic_wait_icr_idle();
739 * Give the other CPU some time to accept the IPI.
741 udelay(200);
742 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
743 maxlvt = lapic_get_maxlvt();
744 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
745 apic_write(APIC_ESR, 0);
746 accept_status = (apic_read(APIC_ESR) & 0xEF);
748 pr_debug("NMI sent\n");
750 if (send_status)
751 pr_err("APIC never delivered???\n");
752 if (accept_status)
753 pr_err("APIC delivery error (%lx)\n", accept_status);
755 return (send_status | accept_status);
758 static int
759 wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
761 unsigned long send_status = 0, accept_status = 0;
762 int maxlvt, num_starts, j;
764 maxlvt = lapic_get_maxlvt();
767 * Be paranoid about clearing APIC errors.
769 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
770 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
771 apic_write(APIC_ESR, 0);
772 apic_read(APIC_ESR);
775 pr_debug("Asserting INIT\n");
778 * Turn INIT on target chip
781 * Send IPI
783 apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
784 phys_apicid);
786 pr_debug("Waiting for send to finish...\n");
787 send_status = safe_apic_wait_icr_idle();
789 udelay(init_udelay);
791 pr_debug("Deasserting INIT\n");
793 /* Target chip */
794 /* Send IPI */
795 apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
797 pr_debug("Waiting for send to finish...\n");
798 send_status = safe_apic_wait_icr_idle();
800 mb();
803 * Should we send STARTUP IPIs ?
805 * Determine this based on the APIC version.
806 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
808 if (APIC_INTEGRATED(boot_cpu_apic_version))
809 num_starts = 2;
810 else
811 num_starts = 0;
814 * Run STARTUP IPI loop.
816 pr_debug("#startup loops: %d\n", num_starts);
818 for (j = 1; j <= num_starts; j++) {
819 pr_debug("Sending STARTUP #%d\n", j);
820 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
821 apic_write(APIC_ESR, 0);
822 apic_read(APIC_ESR);
823 pr_debug("After apic_write\n");
826 * STARTUP IPI
829 /* Target chip */
830 /* Boot on the stack */
831 /* Kick the second */
832 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
833 phys_apicid);
836 * Give the other CPU some time to accept the IPI.
838 if (init_udelay == 0)
839 udelay(10);
840 else
841 udelay(300);
843 pr_debug("Startup point 1\n");
845 pr_debug("Waiting for send to finish...\n");
846 send_status = safe_apic_wait_icr_idle();
849 * Give the other CPU some time to accept the IPI.
851 if (init_udelay == 0)
852 udelay(10);
853 else
854 udelay(200);
856 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
857 apic_write(APIC_ESR, 0);
858 accept_status = (apic_read(APIC_ESR) & 0xEF);
859 if (send_status || accept_status)
860 break;
862 pr_debug("After Startup\n");
864 if (send_status)
865 pr_err("APIC never delivered???\n");
866 if (accept_status)
867 pr_err("APIC delivery error (%lx)\n", accept_status);
869 return (send_status | accept_status);
872 /* reduce the number of lines printed when booting a large cpu count system */
873 static void announce_cpu(int cpu, int apicid)
875 static int current_node = -1;
876 int node = early_cpu_to_node(cpu);
877 static int width, node_width;
879 if (!width)
880 width = num_digits(num_possible_cpus()) + 1; /* + '#' sign */
882 if (!node_width)
883 node_width = num_digits(num_possible_nodes()) + 1; /* + '#' */
885 if (cpu == 1)
886 printk(KERN_INFO "x86: Booting SMP configuration:\n");
888 if (system_state < SYSTEM_RUNNING) {
889 if (node != current_node) {
890 if (current_node > (-1))
891 pr_cont("\n");
892 current_node = node;
894 printk(KERN_INFO ".... node %*s#%d, CPUs: ",
895 node_width - num_digits(node), " ", node);
898 /* Add padding for the BSP */
899 if (cpu == 1)
900 pr_cont("%*s", width + 1, " ");
902 pr_cont("%*s#%d", width - num_digits(cpu), " ", cpu);
904 } else
905 pr_info("Booting Node %d Processor %d APIC 0x%x\n",
906 node, cpu, apicid);
909 static int wakeup_cpu0_nmi(unsigned int cmd, struct pt_regs *regs)
911 int cpu;
913 cpu = smp_processor_id();
914 if (cpu == 0 && !cpu_online(cpu) && enable_start_cpu0)
915 return NMI_HANDLED;
917 return NMI_DONE;
921 * Wake up AP by INIT, INIT, STARTUP sequence.
923 * Instead of waiting for STARTUP after INITs, BSP will execute the BIOS
924 * boot-strap code which is not a desired behavior for waking up BSP. To
925 * void the boot-strap code, wake up CPU0 by NMI instead.
927 * This works to wake up soft offlined CPU0 only. If CPU0 is hard offlined
928 * (i.e. physically hot removed and then hot added), NMI won't wake it up.
929 * We'll change this code in the future to wake up hard offlined CPU0 if
930 * real platform and request are available.
932 static int
933 wakeup_cpu_via_init_nmi(int cpu, unsigned long start_ip, int apicid,
934 int *cpu0_nmi_registered)
936 int id;
937 int boot_error;
939 preempt_disable();
942 * Wake up AP by INIT, INIT, STARTUP sequence.
944 if (cpu) {
945 boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
946 goto out;
950 * Wake up BSP by nmi.
952 * Register a NMI handler to help wake up CPU0.
954 boot_error = register_nmi_handler(NMI_LOCAL,
955 wakeup_cpu0_nmi, 0, "wake_cpu0");
957 if (!boot_error) {
958 enable_start_cpu0 = 1;
959 *cpu0_nmi_registered = 1;
960 if (apic->dest_logical == APIC_DEST_LOGICAL)
961 id = cpu0_logical_apicid;
962 else
963 id = apicid;
964 boot_error = wakeup_secondary_cpu_via_nmi(id, start_ip);
967 out:
968 preempt_enable();
970 return boot_error;
973 void common_cpu_up(unsigned int cpu, struct task_struct *idle)
975 /* Just in case we booted with a single CPU. */
976 alternatives_enable_smp();
978 per_cpu(current_task, cpu) = idle;
980 #ifdef CONFIG_X86_32
981 /* Stack for startup_32 can be just as for start_secondary onwards */
982 irq_ctx_init(cpu);
983 per_cpu(cpu_current_top_of_stack, cpu) = task_top_of_stack(idle);
984 #else
985 initial_gs = per_cpu_offset(cpu);
986 #endif
990 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
991 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
992 * Returns zero if CPU booted OK, else error code from
993 * ->wakeup_secondary_cpu.
995 static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,
996 int *cpu0_nmi_registered)
998 volatile u32 *trampoline_status =
999 (volatile u32 *) __va(real_mode_header->trampoline_status);
1000 /* start_ip had better be page-aligned! */
1001 unsigned long start_ip = real_mode_header->trampoline_start;
1003 unsigned long boot_error = 0;
1004 unsigned long timeout;
1006 idle->thread.sp = (unsigned long)task_pt_regs(idle);
1007 early_gdt_descr.address = (unsigned long)get_cpu_gdt_rw(cpu);
1008 initial_code = (unsigned long)start_secondary;
1009 initial_stack = idle->thread.sp;
1011 /* Enable the espfix hack for this CPU */
1012 init_espfix_ap(cpu);
1014 /* So we see what's up */
1015 announce_cpu(cpu, apicid);
1018 * This grunge runs the startup process for
1019 * the targeted processor.
1022 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
1024 pr_debug("Setting warm reset code and vector.\n");
1026 smpboot_setup_warm_reset_vector(start_ip);
1028 * Be paranoid about clearing APIC errors.
1030 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
1031 apic_write(APIC_ESR, 0);
1032 apic_read(APIC_ESR);
1037 * AP might wait on cpu_callout_mask in cpu_init() with
1038 * cpu_initialized_mask set if previous attempt to online
1039 * it timed-out. Clear cpu_initialized_mask so that after
1040 * INIT/SIPI it could start with a clean state.
1042 cpumask_clear_cpu(cpu, cpu_initialized_mask);
1043 smp_mb();
1046 * Wake up a CPU in difference cases:
1047 * - Use the method in the APIC driver if it's defined
1048 * Otherwise,
1049 * - Use an INIT boot APIC message for APs or NMI for BSP.
1051 if (apic->wakeup_secondary_cpu)
1052 boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
1053 else
1054 boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
1055 cpu0_nmi_registered);
1057 if (!boot_error) {
1059 * Wait 10s total for first sign of life from AP
1061 boot_error = -1;
1062 timeout = jiffies + 10*HZ;
1063 while (time_before(jiffies, timeout)) {
1064 if (cpumask_test_cpu(cpu, cpu_initialized_mask)) {
1066 * Tell AP to proceed with initialization
1068 cpumask_set_cpu(cpu, cpu_callout_mask);
1069 boot_error = 0;
1070 break;
1072 schedule();
1076 if (!boot_error) {
1078 * Wait till AP completes initial initialization
1080 while (!cpumask_test_cpu(cpu, cpu_callin_mask)) {
1082 * Allow other tasks to run while we wait for the
1083 * AP to come online. This also gives a chance
1084 * for the MTRR work(triggered by the AP coming online)
1085 * to be completed in the stop machine context.
1087 schedule();
1091 /* mark "stuck" area as not stuck */
1092 *trampoline_status = 0;
1094 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
1096 * Cleanup possible dangling ends...
1098 smpboot_restore_warm_reset_vector();
1101 return boot_error;
1104 int native_cpu_up(unsigned int cpu, struct task_struct *tidle)
1106 int apicid = apic->cpu_present_to_apicid(cpu);
1107 int cpu0_nmi_registered = 0;
1108 unsigned long flags;
1109 int err, ret = 0;
1111 WARN_ON(irqs_disabled());
1113 pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
1115 if (apicid == BAD_APICID ||
1116 !physid_isset(apicid, phys_cpu_present_map) ||
1117 !apic->apic_id_valid(apicid)) {
1118 pr_err("%s: bad cpu %d\n", __func__, cpu);
1119 return -EINVAL;
1123 * Already booted CPU?
1125 if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
1126 pr_debug("do_boot_cpu %d Already started\n", cpu);
1127 return -ENOSYS;
1131 * Save current MTRR state in case it was changed since early boot
1132 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
1134 mtrr_save_state();
1136 /* x86 CPUs take themselves offline, so delayed offline is OK. */
1137 err = cpu_check_up_prepare(cpu);
1138 if (err && err != -EBUSY)
1139 return err;
1141 /* the FPU context is blank, nobody can own it */
1142 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
1144 common_cpu_up(cpu, tidle);
1146 err = do_boot_cpu(apicid, cpu, tidle, &cpu0_nmi_registered);
1147 if (err) {
1148 pr_err("do_boot_cpu failed(%d) to wakeup CPU#%u\n", err, cpu);
1149 ret = -EIO;
1150 goto unreg_nmi;
1154 * Check TSC synchronization with the AP (keep irqs disabled
1155 * while doing so):
1157 local_irq_save(flags);
1158 check_tsc_sync_source(cpu);
1159 local_irq_restore(flags);
1161 while (!cpu_online(cpu)) {
1162 cpu_relax();
1163 touch_nmi_watchdog();
1166 unreg_nmi:
1168 * Clean up the nmi handler. Do this after the callin and callout sync
1169 * to avoid impact of possible long unregister time.
1171 if (cpu0_nmi_registered)
1172 unregister_nmi_handler(NMI_LOCAL, "wake_cpu0");
1174 return ret;
1178 * arch_disable_smp_support() - disables SMP support for x86 at runtime
1180 void arch_disable_smp_support(void)
1182 disable_ioapic_support();
1186 * Fall back to non SMP mode after errors.
1188 * RED-PEN audit/test this more. I bet there is more state messed up here.
1190 static __init void disable_smp(void)
1192 pr_info("SMP disabled\n");
1194 disable_ioapic_support();
1196 init_cpu_present(cpumask_of(0));
1197 init_cpu_possible(cpumask_of(0));
1199 if (smp_found_config)
1200 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1201 else
1202 physid_set_mask_of_physid(0, &phys_cpu_present_map);
1203 cpumask_set_cpu(0, topology_sibling_cpumask(0));
1204 cpumask_set_cpu(0, topology_core_cpumask(0));
1207 enum {
1208 SMP_OK,
1209 SMP_NO_CONFIG,
1210 SMP_NO_APIC,
1211 SMP_FORCE_UP,
1215 * Various sanity checks.
1217 static int __init smp_sanity_check(unsigned max_cpus)
1219 preempt_disable();
1221 #if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
1222 if (def_to_bigsmp && nr_cpu_ids > 8) {
1223 unsigned int cpu;
1224 unsigned nr;
1226 pr_warn("More than 8 CPUs detected - skipping them\n"
1227 "Use CONFIG_X86_BIGSMP\n");
1229 nr = 0;
1230 for_each_present_cpu(cpu) {
1231 if (nr >= 8)
1232 set_cpu_present(cpu, false);
1233 nr++;
1236 nr = 0;
1237 for_each_possible_cpu(cpu) {
1238 if (nr >= 8)
1239 set_cpu_possible(cpu, false);
1240 nr++;
1243 nr_cpu_ids = 8;
1245 #endif
1247 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
1248 pr_warn("weird, boot CPU (#%d) not listed by the BIOS\n",
1249 hard_smp_processor_id());
1251 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1255 * If we couldn't find an SMP configuration at boot time,
1256 * get out of here now!
1258 if (!smp_found_config && !acpi_lapic) {
1259 preempt_enable();
1260 pr_notice("SMP motherboard not detected\n");
1261 return SMP_NO_CONFIG;
1265 * Should not be necessary because the MP table should list the boot
1266 * CPU too, but we do it for the sake of robustness anyway.
1268 if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
1269 pr_notice("weird, boot CPU (#%d) not listed by the BIOS\n",
1270 boot_cpu_physical_apicid);
1271 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1273 preempt_enable();
1276 * If we couldn't find a local APIC, then get out of here now!
1278 if (APIC_INTEGRATED(boot_cpu_apic_version) &&
1279 !boot_cpu_has(X86_FEATURE_APIC)) {
1280 if (!disable_apic) {
1281 pr_err("BIOS bug, local APIC #%d not detected!...\n",
1282 boot_cpu_physical_apicid);
1283 pr_err("... forcing use of dummy APIC emulation (tell your hw vendor)\n");
1285 return SMP_NO_APIC;
1289 * If SMP should be disabled, then really disable it!
1291 if (!max_cpus) {
1292 pr_info("SMP mode deactivated\n");
1293 return SMP_FORCE_UP;
1296 return SMP_OK;
1299 static void __init smp_cpu_index_default(void)
1301 int i;
1302 struct cpuinfo_x86 *c;
1304 for_each_possible_cpu(i) {
1305 c = &cpu_data(i);
1306 /* mark all to hotplug */
1307 c->cpu_index = nr_cpu_ids;
1312 * Prepare for SMP bootup. The MP table or ACPI has been read
1313 * earlier. Just do some sanity checking here and enable APIC mode.
1315 void __init native_smp_prepare_cpus(unsigned int max_cpus)
1317 unsigned int i;
1319 smp_cpu_index_default();
1322 * Setup boot CPU information
1324 smp_store_boot_cpu_info(); /* Final full version of the data */
1325 cpumask_copy(cpu_callin_mask, cpumask_of(0));
1326 mb();
1328 for_each_possible_cpu(i) {
1329 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
1330 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
1331 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
1335 * Set 'default' x86 topology, this matches default_topology() in that
1336 * it has NUMA nodes as a topology level. See also
1337 * native_smp_cpus_done().
1339 * Must be done before set_cpus_sibling_map() is ran.
1341 set_sched_topology(x86_topology);
1343 set_cpu_sibling_map(0);
1345 switch (smp_sanity_check(max_cpus)) {
1346 case SMP_NO_CONFIG:
1347 disable_smp();
1348 if (APIC_init_uniprocessor())
1349 pr_notice("Local APIC not detected. Using dummy APIC emulation.\n");
1350 return;
1351 case SMP_NO_APIC:
1352 disable_smp();
1353 return;
1354 case SMP_FORCE_UP:
1355 disable_smp();
1356 apic_bsp_setup(false);
1357 return;
1358 case SMP_OK:
1359 break;
1362 if (read_apic_id() != boot_cpu_physical_apicid) {
1363 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
1364 read_apic_id(), boot_cpu_physical_apicid);
1365 /* Or can we switch back to PIC here? */
1368 default_setup_apic_routing();
1369 cpu0_logical_apicid = apic_bsp_setup(false);
1371 pr_info("CPU0: ");
1372 print_cpu_info(&cpu_data(0));
1374 uv_system_init();
1376 set_mtrr_aps_delayed_init();
1378 smp_quirk_init_udelay();
1380 speculative_store_bypass_ht_init();
1383 void arch_enable_nonboot_cpus_begin(void)
1385 set_mtrr_aps_delayed_init();
1388 void arch_enable_nonboot_cpus_end(void)
1390 mtrr_aps_init();
1394 * Early setup to make printk work.
1396 void __init native_smp_prepare_boot_cpu(void)
1398 int me = smp_processor_id();
1399 switch_to_new_gdt(me);
1400 /* already set me in cpu_online_mask in boot_cpu_init() */
1401 cpumask_set_cpu(me, cpu_callout_mask);
1402 cpu_set_state_online(me);
1405 void __init native_smp_cpus_done(unsigned int max_cpus)
1407 pr_debug("Boot done\n");
1409 if (x86_has_numa_in_package)
1410 set_sched_topology(x86_numa_in_package_topology);
1412 nmi_selftest();
1413 impress_friends();
1414 setup_ioapic_dest();
1415 mtrr_aps_init();
1418 static int __initdata setup_possible_cpus = -1;
1419 static int __init _setup_possible_cpus(char *str)
1421 get_option(&str, &setup_possible_cpus);
1422 return 0;
1424 early_param("possible_cpus", _setup_possible_cpus);
1428 * cpu_possible_mask should be static, it cannot change as cpu's
1429 * are onlined, or offlined. The reason is per-cpu data-structures
1430 * are allocated by some modules at init time, and dont expect to
1431 * do this dynamically on cpu arrival/departure.
1432 * cpu_present_mask on the other hand can change dynamically.
1433 * In case when cpu_hotplug is not compiled, then we resort to current
1434 * behaviour, which is cpu_possible == cpu_present.
1435 * - Ashok Raj
1437 * Three ways to find out the number of additional hotplug CPUs:
1438 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
1439 * - The user can overwrite it with possible_cpus=NUM
1440 * - Otherwise don't reserve additional CPUs.
1441 * We do this because additional CPUs waste a lot of memory.
1442 * -AK
1444 __init void prefill_possible_map(void)
1446 int i, possible;
1448 /* No boot processor was found in mptable or ACPI MADT */
1449 if (!num_processors) {
1450 if (boot_cpu_has(X86_FEATURE_APIC)) {
1451 int apicid = boot_cpu_physical_apicid;
1452 int cpu = hard_smp_processor_id();
1454 pr_warn("Boot CPU (id %d) not listed by BIOS\n", cpu);
1456 /* Make sure boot cpu is enumerated */
1457 if (apic->cpu_present_to_apicid(0) == BAD_APICID &&
1458 apic->apic_id_valid(apicid))
1459 generic_processor_info(apicid, boot_cpu_apic_version);
1462 if (!num_processors)
1463 num_processors = 1;
1466 i = setup_max_cpus ?: 1;
1467 if (setup_possible_cpus == -1) {
1468 possible = num_processors;
1469 #ifdef CONFIG_HOTPLUG_CPU
1470 if (setup_max_cpus)
1471 possible += disabled_cpus;
1472 #else
1473 if (possible > i)
1474 possible = i;
1475 #endif
1476 } else
1477 possible = setup_possible_cpus;
1479 total_cpus = max_t(int, possible, num_processors + disabled_cpus);
1481 /* nr_cpu_ids could be reduced via nr_cpus= */
1482 if (possible > nr_cpu_ids) {
1483 pr_warn("%d Processors exceeds NR_CPUS limit of %u\n",
1484 possible, nr_cpu_ids);
1485 possible = nr_cpu_ids;
1488 #ifdef CONFIG_HOTPLUG_CPU
1489 if (!setup_max_cpus)
1490 #endif
1491 if (possible > i) {
1492 pr_warn("%d Processors exceeds max_cpus limit of %u\n",
1493 possible, setup_max_cpus);
1494 possible = i;
1497 nr_cpu_ids = possible;
1499 pr_info("Allowing %d CPUs, %d hotplug CPUs\n",
1500 possible, max_t(int, possible - num_processors, 0));
1502 reset_cpu_possible_mask();
1504 for (i = 0; i < possible; i++)
1505 set_cpu_possible(i, true);
1508 #ifdef CONFIG_HOTPLUG_CPU
1510 /* Recompute SMT state for all CPUs on offline */
1511 static void recompute_smt_state(void)
1513 int max_threads, cpu;
1515 max_threads = 0;
1516 for_each_online_cpu (cpu) {
1517 int threads = cpumask_weight(topology_sibling_cpumask(cpu));
1519 if (threads > max_threads)
1520 max_threads = threads;
1522 __max_smt_threads = max_threads;
1525 static void remove_siblinginfo(int cpu)
1527 int sibling;
1528 struct cpuinfo_x86 *c = &cpu_data(cpu);
1530 for_each_cpu(sibling, topology_core_cpumask(cpu)) {
1531 cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
1533 * last thread sibling in this cpu core going down
1535 if (cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
1536 cpu_data(sibling).booted_cores--;
1539 for_each_cpu(sibling, topology_sibling_cpumask(cpu))
1540 cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
1541 for_each_cpu(sibling, cpu_llc_shared_mask(cpu))
1542 cpumask_clear_cpu(cpu, cpu_llc_shared_mask(sibling));
1543 cpumask_clear(cpu_llc_shared_mask(cpu));
1544 cpumask_clear(topology_sibling_cpumask(cpu));
1545 cpumask_clear(topology_core_cpumask(cpu));
1546 c->phys_proc_id = 0;
1547 c->cpu_core_id = 0;
1548 c->booted_cores = 0;
1549 cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
1550 recompute_smt_state();
1553 static void remove_cpu_from_maps(int cpu)
1555 set_cpu_online(cpu, false);
1556 cpumask_clear_cpu(cpu, cpu_callout_mask);
1557 cpumask_clear_cpu(cpu, cpu_callin_mask);
1558 /* was set by cpu_init() */
1559 cpumask_clear_cpu(cpu, cpu_initialized_mask);
1560 numa_remove_cpu(cpu);
1563 void cpu_disable_common(void)
1565 int cpu = smp_processor_id();
1567 remove_siblinginfo(cpu);
1569 /* It's now safe to remove this processor from the online map */
1570 lock_vector_lock();
1571 remove_cpu_from_maps(cpu);
1572 unlock_vector_lock();
1573 fixup_irqs();
1576 int native_cpu_disable(void)
1578 int ret;
1580 ret = check_irq_vectors_for_cpu_disable();
1581 if (ret)
1582 return ret;
1584 clear_local_APIC();
1585 cpu_disable_common();
1587 return 0;
1590 int common_cpu_die(unsigned int cpu)
1592 int ret = 0;
1594 /* We don't do anything here: idle task is faking death itself. */
1596 /* They ack this in play_dead() by setting CPU_DEAD */
1597 if (cpu_wait_death(cpu, 5)) {
1598 if (system_state == SYSTEM_RUNNING)
1599 pr_info("CPU %u is now offline\n", cpu);
1600 } else {
1601 pr_err("CPU %u didn't die...\n", cpu);
1602 ret = -1;
1605 return ret;
1608 void native_cpu_die(unsigned int cpu)
1610 common_cpu_die(cpu);
1613 void play_dead_common(void)
1615 idle_task_exit();
1617 /* Ack it */
1618 (void)cpu_report_death();
1621 * With physical CPU hotplug, we should halt the cpu
1623 local_irq_disable();
1626 static bool wakeup_cpu0(void)
1628 if (smp_processor_id() == 0 && enable_start_cpu0)
1629 return true;
1631 return false;
1635 * We need to flush the caches before going to sleep, lest we have
1636 * dirty data in our caches when we come back up.
1638 static inline void mwait_play_dead(void)
1640 unsigned int eax, ebx, ecx, edx;
1641 unsigned int highest_cstate = 0;
1642 unsigned int highest_subcstate = 0;
1643 void *mwait_ptr;
1644 int i;
1646 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1647 return;
1648 if (!this_cpu_has(X86_FEATURE_MWAIT))
1649 return;
1650 if (!this_cpu_has(X86_FEATURE_CLFLUSH))
1651 return;
1652 if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
1653 return;
1655 eax = CPUID_MWAIT_LEAF;
1656 ecx = 0;
1657 native_cpuid(&eax, &ebx, &ecx, &edx);
1660 * eax will be 0 if EDX enumeration is not valid.
1661 * Initialized below to cstate, sub_cstate value when EDX is valid.
1663 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
1664 eax = 0;
1665 } else {
1666 edx >>= MWAIT_SUBSTATE_SIZE;
1667 for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
1668 if (edx & MWAIT_SUBSTATE_MASK) {
1669 highest_cstate = i;
1670 highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
1673 eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
1674 (highest_subcstate - 1);
1678 * This should be a memory location in a cache line which is
1679 * unlikely to be touched by other processors. The actual
1680 * content is immaterial as it is not actually modified in any way.
1682 mwait_ptr = &current_thread_info()->flags;
1684 wbinvd();
1686 while (1) {
1688 * The CLFLUSH is a workaround for erratum AAI65 for
1689 * the Xeon 7400 series. It's not clear it is actually
1690 * needed, but it should be harmless in either case.
1691 * The WBINVD is insufficient due to the spurious-wakeup
1692 * case where we return around the loop.
1694 mb();
1695 clflush(mwait_ptr);
1696 mb();
1697 __monitor(mwait_ptr, 0, 0);
1698 mb();
1699 __mwait(eax, 0);
1701 * If NMI wants to wake up CPU0, start CPU0.
1703 if (wakeup_cpu0())
1704 start_cpu0();
1708 void hlt_play_dead(void)
1710 if (__this_cpu_read(cpu_info.x86) >= 4)
1711 wbinvd();
1713 while (1) {
1714 native_halt();
1716 * If NMI wants to wake up CPU0, start CPU0.
1718 if (wakeup_cpu0())
1719 start_cpu0();
1723 void native_play_dead(void)
1725 play_dead_common();
1726 tboot_shutdown(TB_SHUTDOWN_WFS);
1728 mwait_play_dead(); /* Only returns on failure */
1729 if (cpuidle_play_dead())
1730 hlt_play_dead();
1733 #else /* ... !CONFIG_HOTPLUG_CPU */
1734 int native_cpu_disable(void)
1736 return -ENOSYS;
1739 void native_cpu_die(unsigned int cpu)
1741 /* We said "no" in __cpu_disable */
1742 BUG();
1745 void native_play_dead(void)
1747 BUG();
1750 #endif