Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002  /*
0003  *  x86 SMP booting functions
0004  *
0005  *  (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
0006  *  (c) 1998, 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
0007  *  Copyright 2001 Andi Kleen, SuSE Labs.
0008  *
0009  *  Much of the core SMP work is based on previous work by Thomas Radke, to
0010  *  whom a great many thanks are extended.
0011  *
0012  *  Thanks to Intel for making available several different Pentium,
0013  *  Pentium Pro and Pentium-II/Xeon MP machines.
0014  *  Original development of Linux SMP code supported by Caldera.
0015  *
0016  *  Fixes
0017  *      Felix Koop  :   NR_CPUS used properly
0018  *      Jose Renau  :   Handle single CPU case.
0019  *      Alan Cox    :   By repeated request 8) - Total BogoMIPS report.
0020  *      Greg Wright :   Fix for kernel stacks panic.
0021  *      Erich Boleyn    :   MP v1.4 and additional changes.
0022  *  Matthias Sattler    :   Changes for 2.1 kernel map.
0023  *  Michel Lespinasse   :   Changes for 2.1 kernel map.
0024  *  Michael Chastain    :   Change trampoline.S to gnu as.
0025  *      Alan Cox    :   Dumb bug: 'B' step PPro's are fine
0026  *      Ingo Molnar :   Added APIC timers, based on code
0027  *                  from Jose Renau
0028  *      Ingo Molnar :   various cleanups and rewrites
0029  *      Tigran Aivazian :   fixed "0.00 in /proc/uptime on SMP" bug.
0030  *  Maciej W. Rozycki   :   Bits for genuine 82489DX APICs
0031  *  Andi Kleen      :   Changed for SMP boot into long mode.
0032  *      Martin J. Bligh :   Added support for multi-quad systems
0033  *      Dave Jones  :   Report invalid combinations of Athlon CPUs.
0034  *      Rusty Russell   :   Hacked into shape for new "hotplug" boot process.
0035  *      Andi Kleen              :       Converted to new state machine.
0036  *  Ashok Raj       :   CPU hotplug support
0037  *  Glauber Costa       :   i386 and x86_64 integration
0038  */
0039 
0040 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0041 
0042 #include <linux/init.h>
0043 #include <linux/smp.h>
0044 #include <linux/export.h>
0045 #include <linux/sched.h>
0046 #include <linux/sched/topology.h>
0047 #include <linux/sched/hotplug.h>
0048 #include <linux/sched/task_stack.h>
0049 #include <linux/percpu.h>
0050 #include <linux/memblock.h>
0051 #include <linux/err.h>
0052 #include <linux/nmi.h>
0053 #include <linux/tboot.h>
0054 #include <linux/gfp.h>
0055 #include <linux/cpuidle.h>
0056 #include <linux/numa.h>
0057 #include <linux/pgtable.h>
0058 #include <linux/overflow.h>
0059 
0060 #include <asm/acpi.h>
0061 #include <asm/desc.h>
0062 #include <asm/nmi.h>
0063 #include <asm/irq.h>
0064 #include <asm/realmode.h>
0065 #include <asm/cpu.h>
0066 #include <asm/numa.h>
0067 #include <asm/tlbflush.h>
0068 #include <asm/mtrr.h>
0069 #include <asm/mwait.h>
0070 #include <asm/apic.h>
0071 #include <asm/io_apic.h>
0072 #include <asm/fpu/api.h>
0073 #include <asm/setup.h>
0074 #include <asm/uv/uv.h>
0075 #include <linux/mc146818rtc.h>
0076 #include <asm/i8259.h>
0077 #include <asm/misc.h>
0078 #include <asm/qspinlock.h>
0079 #include <asm/intel-family.h>
0080 #include <asm/cpu_device_id.h>
0081 #include <asm/spec-ctrl.h>
0082 #include <asm/hw_irq.h>
0083 #include <asm/stackprotector.h>
0084 #include <asm/sev.h>
0085 
0086 /* representing HT siblings of each logical CPU */
0087 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
0088 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
0089 
0090 /* representing HT and core siblings of each logical CPU */
0091 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
0092 EXPORT_PER_CPU_SYMBOL(cpu_core_map);
0093 
0094 /* representing HT, core, and die siblings of each logical CPU */
0095 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_die_map);
0096 EXPORT_PER_CPU_SYMBOL(cpu_die_map);
0097 
0098 /* Per CPU bogomips and other parameters */
0099 DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
0100 EXPORT_PER_CPU_SYMBOL(cpu_info);
0101 
0102 /* Logical package management. We might want to allocate that dynamically */
0103 unsigned int __max_logical_packages __read_mostly;
0104 EXPORT_SYMBOL(__max_logical_packages);
0105 static unsigned int logical_packages __read_mostly;
0106 static unsigned int logical_die __read_mostly;
0107 
0108 /* Maximum number of SMT threads on any online core */
0109 int __read_mostly __max_smt_threads = 1;
0110 
0111 /* Flag to indicate if a complete sched domain rebuild is required */
0112 bool x86_topology_update;
0113 
0114 int arch_update_cpu_topology(void)
0115 {
0116     int retval = x86_topology_update;
0117 
0118     x86_topology_update = false;
0119     return retval;
0120 }
0121 
0122 static inline void smpboot_setup_warm_reset_vector(unsigned long start_eip)
0123 {
0124     unsigned long flags;
0125 
0126     spin_lock_irqsave(&rtc_lock, flags);
0127     CMOS_WRITE(0xa, 0xf);
0128     spin_unlock_irqrestore(&rtc_lock, flags);
0129     *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_HIGH)) =
0130                             start_eip >> 4;
0131     *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) =
0132                             start_eip & 0xf;
0133 }
0134 
0135 static inline void smpboot_restore_warm_reset_vector(void)
0136 {
0137     unsigned long flags;
0138 
0139     /*
0140      * Paranoid:  Set warm reset code and vector here back
0141      * to default values.
0142      */
0143     spin_lock_irqsave(&rtc_lock, flags);
0144     CMOS_WRITE(0, 0xf);
0145     spin_unlock_irqrestore(&rtc_lock, flags);
0146 
0147     *((volatile u32 *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) = 0;
0148 }
0149 
0150 /*
0151  * Report back to the Boot Processor during boot time or to the caller processor
0152  * during CPU online.
0153  */
0154 static void smp_callin(void)
0155 {
0156     int cpuid;
0157 
0158     /*
0159      * If waken up by an INIT in an 82489DX configuration
0160      * cpu_callout_mask guarantees we don't get here before
0161      * an INIT_deassert IPI reaches our local APIC, so it is
0162      * now safe to touch our local APIC.
0163      */
0164     cpuid = smp_processor_id();
0165 
0166     /*
0167      * the boot CPU has finished the init stage and is spinning
0168      * on callin_map until we finish. We are free to set up this
0169      * CPU, first the APIC. (this is probably redundant on most
0170      * boards)
0171      */
0172     apic_ap_setup();
0173 
0174     /*
0175      * Save our processor parameters. Note: this information
0176      * is needed for clock calibration.
0177      */
0178     smp_store_cpu_info(cpuid);
0179 
0180     /*
0181      * The topology information must be up to date before
0182      * calibrate_delay() and notify_cpu_starting().
0183      */
0184     set_cpu_sibling_map(raw_smp_processor_id());
0185 
0186     ap_init_aperfmperf();
0187 
0188     /*
0189      * Get our bogomips.
0190      * Update loops_per_jiffy in cpu_data. Previous call to
0191      * smp_store_cpu_info() stored a value that is close but not as
0192      * accurate as the value just calculated.
0193      */
0194     calibrate_delay();
0195     cpu_data(cpuid).loops_per_jiffy = loops_per_jiffy;
0196     pr_debug("Stack at about %p\n", &cpuid);
0197 
0198     wmb();
0199 
0200     notify_cpu_starting(cpuid);
0201 
0202     /*
0203      * Allow the master to continue.
0204      */
0205     cpumask_set_cpu(cpuid, cpu_callin_mask);
0206 }
0207 
0208 static int cpu0_logical_apicid;
0209 static int enable_start_cpu0;
0210 /*
0211  * Activate a secondary processor.
0212  */
0213 static void notrace start_secondary(void *unused)
0214 {
0215     /*
0216      * Don't put *anything* except direct CPU state initialization
0217      * before cpu_init(), SMP booting is too fragile that we want to
0218      * limit the things done here to the most necessary things.
0219      */
0220     cr4_init();
0221 
0222 #ifdef CONFIG_X86_32
0223     /* switch away from the initial page table */
0224     load_cr3(swapper_pg_dir);
0225     __flush_tlb_all();
0226 #endif
0227     cpu_init_secondary();
0228     rcu_cpu_starting(raw_smp_processor_id());
0229     x86_cpuinit.early_percpu_clock_init();
0230     smp_callin();
0231 
0232     enable_start_cpu0 = 0;
0233 
0234     /* otherwise gcc will move up smp_processor_id before the cpu_init */
0235     barrier();
0236     /*
0237      * Check TSC synchronization with the boot CPU:
0238      */
0239     check_tsc_sync_target();
0240 
0241     speculative_store_bypass_ht_init();
0242 
0243     /*
0244      * Lock vector_lock, set CPU online and bring the vector
0245      * allocator online. Online must be set with vector_lock held
0246      * to prevent a concurrent irq setup/teardown from seeing a
0247      * half valid vector space.
0248      */
0249     lock_vector_lock();
0250     set_cpu_online(smp_processor_id(), true);
0251     lapic_online();
0252     unlock_vector_lock();
0253     cpu_set_state_online(smp_processor_id());
0254     x86_platform.nmi_init();
0255 
0256     /* enable local interrupts */
0257     local_irq_enable();
0258 
0259     x86_cpuinit.setup_percpu_clockev();
0260 
0261     wmb();
0262     cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
0263 }
0264 
0265 /**
0266  * topology_is_primary_thread - Check whether CPU is the primary SMT thread
0267  * @cpu:    CPU to check
0268  */
0269 bool topology_is_primary_thread(unsigned int cpu)
0270 {
0271     return apic_id_is_primary_thread(per_cpu(x86_cpu_to_apicid, cpu));
0272 }
0273 
0274 /**
0275  * topology_smt_supported - Check whether SMT is supported by the CPUs
0276  */
0277 bool topology_smt_supported(void)
0278 {
0279     return smp_num_siblings > 1;
0280 }
0281 
0282 /**
0283  * topology_phys_to_logical_pkg - Map a physical package id to a logical
0284  *
0285  * Returns logical package id or -1 if not found
0286  */
0287 int topology_phys_to_logical_pkg(unsigned int phys_pkg)
0288 {
0289     int cpu;
0290 
0291     for_each_possible_cpu(cpu) {
0292         struct cpuinfo_x86 *c = &cpu_data(cpu);
0293 
0294         if (c->initialized && c->phys_proc_id == phys_pkg)
0295             return c->logical_proc_id;
0296     }
0297     return -1;
0298 }
0299 EXPORT_SYMBOL(topology_phys_to_logical_pkg);
0300 /**
0301  * topology_phys_to_logical_die - Map a physical die id to logical
0302  *
0303  * Returns logical die id or -1 if not found
0304  */
0305 int topology_phys_to_logical_die(unsigned int die_id, unsigned int cur_cpu)
0306 {
0307     int cpu;
0308     int proc_id = cpu_data(cur_cpu).phys_proc_id;
0309 
0310     for_each_possible_cpu(cpu) {
0311         struct cpuinfo_x86 *c = &cpu_data(cpu);
0312 
0313         if (c->initialized && c->cpu_die_id == die_id &&
0314             c->phys_proc_id == proc_id)
0315             return c->logical_die_id;
0316     }
0317     return -1;
0318 }
0319 EXPORT_SYMBOL(topology_phys_to_logical_die);
0320 
0321 /**
0322  * topology_update_package_map - Update the physical to logical package map
0323  * @pkg:    The physical package id as retrieved via CPUID
0324  * @cpu:    The cpu for which this is updated
0325  */
0326 int topology_update_package_map(unsigned int pkg, unsigned int cpu)
0327 {
0328     int new;
0329 
0330     /* Already available somewhere? */
0331     new = topology_phys_to_logical_pkg(pkg);
0332     if (new >= 0)
0333         goto found;
0334 
0335     new = logical_packages++;
0336     if (new != pkg) {
0337         pr_info("CPU %u Converting physical %u to logical package %u\n",
0338             cpu, pkg, new);
0339     }
0340 found:
0341     cpu_data(cpu).logical_proc_id = new;
0342     return 0;
0343 }
0344 /**
0345  * topology_update_die_map - Update the physical to logical die map
0346  * @die:    The die id as retrieved via CPUID
0347  * @cpu:    The cpu for which this is updated
0348  */
0349 int topology_update_die_map(unsigned int die, unsigned int cpu)
0350 {
0351     int new;
0352 
0353     /* Already available somewhere? */
0354     new = topology_phys_to_logical_die(die, cpu);
0355     if (new >= 0)
0356         goto found;
0357 
0358     new = logical_die++;
0359     if (new != die) {
0360         pr_info("CPU %u Converting physical %u to logical die %u\n",
0361             cpu, die, new);
0362     }
0363 found:
0364     cpu_data(cpu).logical_die_id = new;
0365     return 0;
0366 }
0367 
0368 void __init smp_store_boot_cpu_info(void)
0369 {
0370     int id = 0; /* CPU 0 */
0371     struct cpuinfo_x86 *c = &cpu_data(id);
0372 
0373     *c = boot_cpu_data;
0374     c->cpu_index = id;
0375     topology_update_package_map(c->phys_proc_id, id);
0376     topology_update_die_map(c->cpu_die_id, id);
0377     c->initialized = true;
0378 }
0379 
0380 /*
0381  * The bootstrap kernel entry code has set these up. Save them for
0382  * a given CPU
0383  */
0384 void smp_store_cpu_info(int id)
0385 {
0386     struct cpuinfo_x86 *c = &cpu_data(id);
0387 
0388     /* Copy boot_cpu_data only on the first bringup */
0389     if (!c->initialized)
0390         *c = boot_cpu_data;
0391     c->cpu_index = id;
0392     /*
0393      * During boot time, CPU0 has this setup already. Save the info when
0394      * bringing up AP or offlined CPU0.
0395      */
0396     identify_secondary_cpu(c);
0397     c->initialized = true;
0398 }
0399 
0400 static bool
0401 topology_same_node(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
0402 {
0403     int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
0404 
0405     return (cpu_to_node(cpu1) == cpu_to_node(cpu2));
0406 }
0407 
0408 static bool
0409 topology_sane(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o, const char *name)
0410 {
0411     int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
0412 
0413     return !WARN_ONCE(!topology_same_node(c, o),
0414         "sched: CPU #%d's %s-sibling CPU #%d is not on the same node! "
0415         "[node: %d != %d]. Ignoring dependency.\n",
0416         cpu1, name, cpu2, cpu_to_node(cpu1), cpu_to_node(cpu2));
0417 }
0418 
0419 #define link_mask(mfunc, c1, c2)                    \
0420 do {                                    \
0421     cpumask_set_cpu((c1), mfunc(c2));               \
0422     cpumask_set_cpu((c2), mfunc(c1));               \
0423 } while (0)
0424 
0425 static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
0426 {
0427     if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
0428         int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
0429 
0430         if (c->phys_proc_id == o->phys_proc_id &&
0431             c->cpu_die_id == o->cpu_die_id &&
0432             per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2)) {
0433             if (c->cpu_core_id == o->cpu_core_id)
0434                 return topology_sane(c, o, "smt");
0435 
0436             if ((c->cu_id != 0xff) &&
0437                 (o->cu_id != 0xff) &&
0438                 (c->cu_id == o->cu_id))
0439                 return topology_sane(c, o, "smt");
0440         }
0441 
0442     } else if (c->phys_proc_id == o->phys_proc_id &&
0443            c->cpu_die_id == o->cpu_die_id &&
0444            c->cpu_core_id == o->cpu_core_id) {
0445         return topology_sane(c, o, "smt");
0446     }
0447 
0448     return false;
0449 }
0450 
0451 static bool match_die(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
0452 {
0453     if (c->phys_proc_id == o->phys_proc_id &&
0454         c->cpu_die_id == o->cpu_die_id)
0455         return true;
0456     return false;
0457 }
0458 
0459 static bool match_l2c(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
0460 {
0461     int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
0462 
0463     /* If the arch didn't set up l2c_id, fall back to SMT */
0464     if (per_cpu(cpu_l2c_id, cpu1) == BAD_APICID)
0465         return match_smt(c, o);
0466 
0467     /* Do not match if L2 cache id does not match: */
0468     if (per_cpu(cpu_l2c_id, cpu1) != per_cpu(cpu_l2c_id, cpu2))
0469         return false;
0470 
0471     return topology_sane(c, o, "l2c");
0472 }
0473 
0474 /*
0475  * Unlike the other levels, we do not enforce keeping a
0476  * multicore group inside a NUMA node.  If this happens, we will
0477  * discard the MC level of the topology later.
0478  */
0479 static bool match_pkg(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
0480 {
0481     if (c->phys_proc_id == o->phys_proc_id)
0482         return true;
0483     return false;
0484 }
0485 
0486 /*
0487  * Define intel_cod_cpu[] for Intel COD (Cluster-on-Die) CPUs.
0488  *
0489  * Any Intel CPU that has multiple nodes per package and does not
0490  * match intel_cod_cpu[] has the SNC (Sub-NUMA Cluster) topology.
0491  *
0492  * When in SNC mode, these CPUs enumerate an LLC that is shared
0493  * by multiple NUMA nodes. The LLC is shared for off-package data
0494  * access but private to the NUMA node (half of the package) for
0495  * on-package access. CPUID (the source of the information about
0496  * the LLC) can only enumerate the cache as shared or unshared,
0497  * but not this particular configuration.
0498  */
0499 
0500 static const struct x86_cpu_id intel_cod_cpu[] = {
0501     X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, 0),   /* COD */
0502     X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, 0), /* COD */
0503     X86_MATCH_INTEL_FAM6_MODEL(ANY, 1),     /* SNC */
0504     {}
0505 };
0506 
0507 static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
0508 {
0509     const struct x86_cpu_id *id = x86_match_cpu(intel_cod_cpu);
0510     int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
0511     bool intel_snc = id && id->driver_data;
0512 
0513     /* Do not match if we do not have a valid APICID for cpu: */
0514     if (per_cpu(cpu_llc_id, cpu1) == BAD_APICID)
0515         return false;
0516 
0517     /* Do not match if LLC id does not match: */
0518     if (per_cpu(cpu_llc_id, cpu1) != per_cpu(cpu_llc_id, cpu2))
0519         return false;
0520 
0521     /*
0522      * Allow the SNC topology without warning. Return of false
0523      * means 'c' does not share the LLC of 'o'. This will be
0524      * reflected to userspace.
0525      */
0526     if (match_pkg(c, o) && !topology_same_node(c, o) && intel_snc)
0527         return false;
0528 
0529     return topology_sane(c, o, "llc");
0530 }
0531 
0532 
0533 #if defined(CONFIG_SCHED_SMT) || defined(CONFIG_SCHED_CLUSTER) || defined(CONFIG_SCHED_MC)
0534 static inline int x86_sched_itmt_flags(void)
0535 {
0536     return sysctl_sched_itmt_enabled ? SD_ASYM_PACKING : 0;
0537 }
0538 
0539 #ifdef CONFIG_SCHED_MC
0540 static int x86_core_flags(void)
0541 {
0542     return cpu_core_flags() | x86_sched_itmt_flags();
0543 }
0544 #endif
0545 #ifdef CONFIG_SCHED_SMT
0546 static int x86_smt_flags(void)
0547 {
0548     return cpu_smt_flags() | x86_sched_itmt_flags();
0549 }
0550 #endif
0551 #ifdef CONFIG_SCHED_CLUSTER
0552 static int x86_cluster_flags(void)
0553 {
0554     return cpu_cluster_flags() | x86_sched_itmt_flags();
0555 }
0556 #endif
0557 #endif
0558 
0559 static struct sched_domain_topology_level x86_numa_in_package_topology[] = {
0560 #ifdef CONFIG_SCHED_SMT
0561     { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
0562 #endif
0563 #ifdef CONFIG_SCHED_CLUSTER
0564     { cpu_clustergroup_mask, x86_cluster_flags, SD_INIT_NAME(CLS) },
0565 #endif
0566 #ifdef CONFIG_SCHED_MC
0567     { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
0568 #endif
0569     { NULL, },
0570 };
0571 
0572 static struct sched_domain_topology_level x86_hybrid_topology[] = {
0573 #ifdef CONFIG_SCHED_SMT
0574     { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
0575 #endif
0576 #ifdef CONFIG_SCHED_MC
0577     { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
0578 #endif
0579     { cpu_cpu_mask, SD_INIT_NAME(DIE) },
0580     { NULL, },
0581 };
0582 
0583 static struct sched_domain_topology_level x86_topology[] = {
0584 #ifdef CONFIG_SCHED_SMT
0585     { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
0586 #endif
0587 #ifdef CONFIG_SCHED_CLUSTER
0588     { cpu_clustergroup_mask, x86_cluster_flags, SD_INIT_NAME(CLS) },
0589 #endif
0590 #ifdef CONFIG_SCHED_MC
0591     { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
0592 #endif
0593     { cpu_cpu_mask, SD_INIT_NAME(DIE) },
0594     { NULL, },
0595 };
0596 
0597 /*
0598  * Set if a package/die has multiple NUMA nodes inside.
0599  * AMD Magny-Cours, Intel Cluster-on-Die, and Intel
0600  * Sub-NUMA Clustering have this.
0601  */
0602 static bool x86_has_numa_in_package;
0603 
0604 void set_cpu_sibling_map(int cpu)
0605 {
0606     bool has_smt = smp_num_siblings > 1;
0607     bool has_mp = has_smt || boot_cpu_data.x86_max_cores > 1;
0608     struct cpuinfo_x86 *c = &cpu_data(cpu);
0609     struct cpuinfo_x86 *o;
0610     int i, threads;
0611 
0612     cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
0613 
0614     if (!has_mp) {
0615         cpumask_set_cpu(cpu, topology_sibling_cpumask(cpu));
0616         cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
0617         cpumask_set_cpu(cpu, cpu_l2c_shared_mask(cpu));
0618         cpumask_set_cpu(cpu, topology_core_cpumask(cpu));
0619         cpumask_set_cpu(cpu, topology_die_cpumask(cpu));
0620         c->booted_cores = 1;
0621         return;
0622     }
0623 
0624     for_each_cpu(i, cpu_sibling_setup_mask) {
0625         o = &cpu_data(i);
0626 
0627         if (match_pkg(c, o) && !topology_same_node(c, o))
0628             x86_has_numa_in_package = true;
0629 
0630         if ((i == cpu) || (has_smt && match_smt(c, o)))
0631             link_mask(topology_sibling_cpumask, cpu, i);
0632 
0633         if ((i == cpu) || (has_mp && match_llc(c, o)))
0634             link_mask(cpu_llc_shared_mask, cpu, i);
0635 
0636         if ((i == cpu) || (has_mp && match_l2c(c, o)))
0637             link_mask(cpu_l2c_shared_mask, cpu, i);
0638 
0639         if ((i == cpu) || (has_mp && match_die(c, o)))
0640             link_mask(topology_die_cpumask, cpu, i);
0641     }
0642 
0643     threads = cpumask_weight(topology_sibling_cpumask(cpu));
0644     if (threads > __max_smt_threads)
0645         __max_smt_threads = threads;
0646 
0647     for_each_cpu(i, topology_sibling_cpumask(cpu))
0648         cpu_data(i).smt_active = threads > 1;
0649 
0650     /*
0651      * This needs a separate iteration over the cpus because we rely on all
0652      * topology_sibling_cpumask links to be set-up.
0653      */
0654     for_each_cpu(i, cpu_sibling_setup_mask) {
0655         o = &cpu_data(i);
0656 
0657         if ((i == cpu) || (has_mp && match_pkg(c, o))) {
0658             link_mask(topology_core_cpumask, cpu, i);
0659 
0660             /*
0661              *  Does this new cpu bringup a new core?
0662              */
0663             if (threads == 1) {
0664                 /*
0665                  * for each core in package, increment
0666                  * the booted_cores for this new cpu
0667                  */
0668                 if (cpumask_first(
0669                     topology_sibling_cpumask(i)) == i)
0670                     c->booted_cores++;
0671                 /*
0672                  * increment the core count for all
0673                  * the other cpus in this package
0674                  */
0675                 if (i != cpu)
0676                     cpu_data(i).booted_cores++;
0677             } else if (i != cpu && !c->booted_cores)
0678                 c->booted_cores = cpu_data(i).booted_cores;
0679         }
0680     }
0681 }
0682 
0683 /* maps the cpu to the sched domain representing multi-core */
0684 const struct cpumask *cpu_coregroup_mask(int cpu)
0685 {
0686     return cpu_llc_shared_mask(cpu);
0687 }
0688 
0689 const struct cpumask *cpu_clustergroup_mask(int cpu)
0690 {
0691     return cpu_l2c_shared_mask(cpu);
0692 }
0693 
0694 static void impress_friends(void)
0695 {
0696     int cpu;
0697     unsigned long bogosum = 0;
0698     /*
0699      * Allow the user to impress friends.
0700      */
0701     pr_debug("Before bogomips\n");
0702     for_each_possible_cpu(cpu)
0703         if (cpumask_test_cpu(cpu, cpu_callout_mask))
0704             bogosum += cpu_data(cpu).loops_per_jiffy;
0705     pr_info("Total of %d processors activated (%lu.%02lu BogoMIPS)\n",
0706         num_online_cpus(),
0707         bogosum/(500000/HZ),
0708         (bogosum/(5000/HZ))%100);
0709 
0710     pr_debug("Before bogocount - setting activated=1\n");
0711 }
0712 
0713 void __inquire_remote_apic(int apicid)
0714 {
0715     unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
0716     const char * const names[] = { "ID", "VERSION", "SPIV" };
0717     int timeout;
0718     u32 status;
0719 
0720     pr_info("Inquiring remote APIC 0x%x...\n", apicid);
0721 
0722     for (i = 0; i < ARRAY_SIZE(regs); i++) {
0723         pr_info("... APIC 0x%x %s: ", apicid, names[i]);
0724 
0725         /*
0726          * Wait for idle.
0727          */
0728         status = safe_apic_wait_icr_idle();
0729         if (status)
0730             pr_cont("a previous APIC delivery may have failed\n");
0731 
0732         apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
0733 
0734         timeout = 0;
0735         do {
0736             udelay(100);
0737             status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
0738         } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
0739 
0740         switch (status) {
0741         case APIC_ICR_RR_VALID:
0742             status = apic_read(APIC_RRR);
0743             pr_cont("%08x\n", status);
0744             break;
0745         default:
0746             pr_cont("failed\n");
0747         }
0748     }
0749 }
0750 
0751 /*
0752  * The Multiprocessor Specification 1.4 (1997) example code suggests
0753  * that there should be a 10ms delay between the BSP asserting INIT
0754  * and de-asserting INIT, when starting a remote processor.
0755  * But that slows boot and resume on modern processors, which include
0756  * many cores and don't require that delay.
0757  *
0758  * Cmdline "init_cpu_udelay=" is available to over-ride this delay.
0759  * Modern processor families are quirked to remove the delay entirely.
0760  */
0761 #define UDELAY_10MS_DEFAULT 10000
0762 
0763 static unsigned int init_udelay = UINT_MAX;
0764 
0765 static int __init cpu_init_udelay(char *str)
0766 {
0767     get_option(&str, &init_udelay);
0768 
0769     return 0;
0770 }
0771 early_param("cpu_init_udelay", cpu_init_udelay);
0772 
0773 static void __init smp_quirk_init_udelay(void)
0774 {
0775     /* if cmdline changed it from default, leave it alone */
0776     if (init_udelay != UINT_MAX)
0777         return;
0778 
0779     /* if modern processor, use no delay */
0780     if (((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 6)) ||
0781         ((boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) && (boot_cpu_data.x86 >= 0x18)) ||
0782         ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && (boot_cpu_data.x86 >= 0xF))) {
0783         init_udelay = 0;
0784         return;
0785     }
0786     /* else, use legacy delay */
0787     init_udelay = UDELAY_10MS_DEFAULT;
0788 }
0789 
0790 /*
0791  * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
0792  * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
0793  * won't ... remember to clear down the APIC, etc later.
0794  */
0795 int
0796 wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip)
0797 {
0798     u32 dm = apic->dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL;
0799     unsigned long send_status, accept_status = 0;
0800     int maxlvt;
0801 
0802     /* Target chip */
0803     /* Boot on the stack */
0804     /* Kick the second */
0805     apic_icr_write(APIC_DM_NMI | dm, apicid);
0806 
0807     pr_debug("Waiting for send to finish...\n");
0808     send_status = safe_apic_wait_icr_idle();
0809 
0810     /*
0811      * Give the other CPU some time to accept the IPI.
0812      */
0813     udelay(200);
0814     if (APIC_INTEGRATED(boot_cpu_apic_version)) {
0815         maxlvt = lapic_get_maxlvt();
0816         if (maxlvt > 3)         /* Due to the Pentium erratum 3AP.  */
0817             apic_write(APIC_ESR, 0);
0818         accept_status = (apic_read(APIC_ESR) & 0xEF);
0819     }
0820     pr_debug("NMI sent\n");
0821 
0822     if (send_status)
0823         pr_err("APIC never delivered???\n");
0824     if (accept_status)
0825         pr_err("APIC delivery error (%lx)\n", accept_status);
0826 
0827     return (send_status | accept_status);
0828 }
0829 
0830 static int
0831 wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
0832 {
0833     unsigned long send_status = 0, accept_status = 0;
0834     int maxlvt, num_starts, j;
0835 
0836     maxlvt = lapic_get_maxlvt();
0837 
0838     /*
0839      * Be paranoid about clearing APIC errors.
0840      */
0841     if (APIC_INTEGRATED(boot_cpu_apic_version)) {
0842         if (maxlvt > 3)     /* Due to the Pentium erratum 3AP.  */
0843             apic_write(APIC_ESR, 0);
0844         apic_read(APIC_ESR);
0845     }
0846 
0847     pr_debug("Asserting INIT\n");
0848 
0849     /*
0850      * Turn INIT on target chip
0851      */
0852     /*
0853      * Send IPI
0854      */
0855     apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
0856                phys_apicid);
0857 
0858     pr_debug("Waiting for send to finish...\n");
0859     send_status = safe_apic_wait_icr_idle();
0860 
0861     udelay(init_udelay);
0862 
0863     pr_debug("Deasserting INIT\n");
0864 
0865     /* Target chip */
0866     /* Send IPI */
0867     apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
0868 
0869     pr_debug("Waiting for send to finish...\n");
0870     send_status = safe_apic_wait_icr_idle();
0871 
0872     mb();
0873 
0874     /*
0875      * Should we send STARTUP IPIs ?
0876      *
0877      * Determine this based on the APIC version.
0878      * If we don't have an integrated APIC, don't send the STARTUP IPIs.
0879      */
0880     if (APIC_INTEGRATED(boot_cpu_apic_version))
0881         num_starts = 2;
0882     else
0883         num_starts = 0;
0884 
0885     /*
0886      * Run STARTUP IPI loop.
0887      */
0888     pr_debug("#startup loops: %d\n", num_starts);
0889 
0890     for (j = 1; j <= num_starts; j++) {
0891         pr_debug("Sending STARTUP #%d\n", j);
0892         if (maxlvt > 3)     /* Due to the Pentium erratum 3AP.  */
0893             apic_write(APIC_ESR, 0);
0894         apic_read(APIC_ESR);
0895         pr_debug("After apic_write\n");
0896 
0897         /*
0898          * STARTUP IPI
0899          */
0900 
0901         /* Target chip */
0902         /* Boot on the stack */
0903         /* Kick the second */
0904         apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
0905                    phys_apicid);
0906 
0907         /*
0908          * Give the other CPU some time to accept the IPI.
0909          */
0910         if (init_udelay == 0)
0911             udelay(10);
0912         else
0913             udelay(300);
0914 
0915         pr_debug("Startup point 1\n");
0916 
0917         pr_debug("Waiting for send to finish...\n");
0918         send_status = safe_apic_wait_icr_idle();
0919 
0920         /*
0921          * Give the other CPU some time to accept the IPI.
0922          */
0923         if (init_udelay == 0)
0924             udelay(10);
0925         else
0926             udelay(200);
0927 
0928         if (maxlvt > 3)     /* Due to the Pentium erratum 3AP.  */
0929             apic_write(APIC_ESR, 0);
0930         accept_status = (apic_read(APIC_ESR) & 0xEF);
0931         if (send_status || accept_status)
0932             break;
0933     }
0934     pr_debug("After Startup\n");
0935 
0936     if (send_status)
0937         pr_err("APIC never delivered???\n");
0938     if (accept_status)
0939         pr_err("APIC delivery error (%lx)\n", accept_status);
0940 
0941     return (send_status | accept_status);
0942 }
0943 
0944 /* reduce the number of lines printed when booting a large cpu count system */
0945 static void announce_cpu(int cpu, int apicid)
0946 {
0947     static int current_node = NUMA_NO_NODE;
0948     int node = early_cpu_to_node(cpu);
0949     static int width, node_width;
0950 
0951     if (!width)
0952         width = num_digits(num_possible_cpus()) + 1; /* + '#' sign */
0953 
0954     if (!node_width)
0955         node_width = num_digits(num_possible_nodes()) + 1; /* + '#' */
0956 
0957     if (cpu == 1)
0958         printk(KERN_INFO "x86: Booting SMP configuration:\n");
0959 
0960     if (system_state < SYSTEM_RUNNING) {
0961         if (node != current_node) {
0962             if (current_node > (-1))
0963                 pr_cont("\n");
0964             current_node = node;
0965 
0966             printk(KERN_INFO ".... node %*s#%d, CPUs:  ",
0967                    node_width - num_digits(node), " ", node);
0968         }
0969 
0970         /* Add padding for the BSP */
0971         if (cpu == 1)
0972             pr_cont("%*s", width + 1, " ");
0973 
0974         pr_cont("%*s#%d", width - num_digits(cpu), " ", cpu);
0975 
0976     } else
0977         pr_info("Booting Node %d Processor %d APIC 0x%x\n",
0978             node, cpu, apicid);
0979 }
0980 
0981 static int wakeup_cpu0_nmi(unsigned int cmd, struct pt_regs *regs)
0982 {
0983     int cpu;
0984 
0985     cpu = smp_processor_id();
0986     if (cpu == 0 && !cpu_online(cpu) && enable_start_cpu0)
0987         return NMI_HANDLED;
0988 
0989     return NMI_DONE;
0990 }
0991 
0992 /*
0993  * Wake up AP by INIT, INIT, STARTUP sequence.
0994  *
0995  * Instead of waiting for STARTUP after INITs, BSP will execute the BIOS
0996  * boot-strap code which is not a desired behavior for waking up BSP. To
0997  * void the boot-strap code, wake up CPU0 by NMI instead.
0998  *
0999  * This works to wake up soft offlined CPU0 only. If CPU0 is hard offlined
1000  * (i.e. physically hot removed and then hot added), NMI won't wake it up.
1001  * We'll change this code in the future to wake up hard offlined CPU0 if
1002  * real platform and request are available.
1003  */
1004 static int
1005 wakeup_cpu_via_init_nmi(int cpu, unsigned long start_ip, int apicid,
1006            int *cpu0_nmi_registered)
1007 {
1008     int id;
1009     int boot_error;
1010 
1011     preempt_disable();
1012 
1013     /*
1014      * Wake up AP by INIT, INIT, STARTUP sequence.
1015      */
1016     if (cpu) {
1017         boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
1018         goto out;
1019     }
1020 
1021     /*
1022      * Wake up BSP by nmi.
1023      *
1024      * Register a NMI handler to help wake up CPU0.
1025      */
1026     boot_error = register_nmi_handler(NMI_LOCAL,
1027                       wakeup_cpu0_nmi, 0, "wake_cpu0");
1028 
1029     if (!boot_error) {
1030         enable_start_cpu0 = 1;
1031         *cpu0_nmi_registered = 1;
1032         id = apic->dest_mode_logical ? cpu0_logical_apicid : apicid;
1033         boot_error = wakeup_secondary_cpu_via_nmi(id, start_ip);
1034     }
1035 
1036 out:
1037     preempt_enable();
1038 
1039     return boot_error;
1040 }
1041 
1042 int common_cpu_up(unsigned int cpu, struct task_struct *idle)
1043 {
1044     int ret;
1045 
1046     /* Just in case we booted with a single CPU. */
1047     alternatives_enable_smp();
1048 
1049     per_cpu(current_task, cpu) = idle;
1050     cpu_init_stack_canary(cpu, idle);
1051 
1052     /* Initialize the interrupt stack(s) */
1053     ret = irq_init_percpu_irqstack(cpu);
1054     if (ret)
1055         return ret;
1056 
1057 #ifdef CONFIG_X86_32
1058     /* Stack for startup_32 can be just as for start_secondary onwards */
1059     per_cpu(cpu_current_top_of_stack, cpu) = task_top_of_stack(idle);
1060 #else
1061     initial_gs = per_cpu_offset(cpu);
1062 #endif
1063     return 0;
1064 }
1065 
1066 /*
1067  * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
1068  * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
1069  * Returns zero if CPU booted OK, else error code from
1070  * ->wakeup_secondary_cpu.
1071  */
1072 static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,
1073                int *cpu0_nmi_registered)
1074 {
1075     /* start_ip had better be page-aligned! */
1076     unsigned long start_ip = real_mode_header->trampoline_start;
1077 
1078     unsigned long boot_error = 0;
1079     unsigned long timeout;
1080 
1081 #ifdef CONFIG_X86_64
1082     /* If 64-bit wakeup method exists, use the 64-bit mode trampoline IP */
1083     if (apic->wakeup_secondary_cpu_64)
1084         start_ip = real_mode_header->trampoline_start64;
1085 #endif
1086     idle->thread.sp = (unsigned long)task_pt_regs(idle);
1087     early_gdt_descr.address = (unsigned long)get_cpu_gdt_rw(cpu);
1088     initial_code = (unsigned long)start_secondary;
1089     initial_stack  = idle->thread.sp;
1090 
1091     /* Enable the espfix hack for this CPU */
1092     init_espfix_ap(cpu);
1093 
1094     /* So we see what's up */
1095     announce_cpu(cpu, apicid);
1096 
1097     /*
1098      * This grunge runs the startup process for
1099      * the targeted processor.
1100      */
1101 
1102     if (x86_platform.legacy.warm_reset) {
1103 
1104         pr_debug("Setting warm reset code and vector.\n");
1105 
1106         smpboot_setup_warm_reset_vector(start_ip);
1107         /*
1108          * Be paranoid about clearing APIC errors.
1109         */
1110         if (APIC_INTEGRATED(boot_cpu_apic_version)) {
1111             apic_write(APIC_ESR, 0);
1112             apic_read(APIC_ESR);
1113         }
1114     }
1115 
1116     /*
1117      * AP might wait on cpu_callout_mask in cpu_init() with
1118      * cpu_initialized_mask set if previous attempt to online
1119      * it timed-out. Clear cpu_initialized_mask so that after
1120      * INIT/SIPI it could start with a clean state.
1121      */
1122     cpumask_clear_cpu(cpu, cpu_initialized_mask);
1123     smp_mb();
1124 
1125     /*
1126      * Wake up a CPU in difference cases:
1127      * - Use a method from the APIC driver if one defined, with wakeup
1128      *   straight to 64-bit mode preferred over wakeup to RM.
1129      * Otherwise,
1130      * - Use an INIT boot APIC message for APs or NMI for BSP.
1131      */
1132     if (apic->wakeup_secondary_cpu_64)
1133         boot_error = apic->wakeup_secondary_cpu_64(apicid, start_ip);
1134     else if (apic->wakeup_secondary_cpu)
1135         boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
1136     else
1137         boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
1138                              cpu0_nmi_registered);
1139 
1140     if (!boot_error) {
1141         /*
1142          * Wait 10s total for first sign of life from AP
1143          */
1144         boot_error = -1;
1145         timeout = jiffies + 10*HZ;
1146         while (time_before(jiffies, timeout)) {
1147             if (cpumask_test_cpu(cpu, cpu_initialized_mask)) {
1148                 /*
1149                  * Tell AP to proceed with initialization
1150                  */
1151                 cpumask_set_cpu(cpu, cpu_callout_mask);
1152                 boot_error = 0;
1153                 break;
1154             }
1155             schedule();
1156         }
1157     }
1158 
1159     if (!boot_error) {
1160         /*
1161          * Wait till AP completes initial initialization
1162          */
1163         while (!cpumask_test_cpu(cpu, cpu_callin_mask)) {
1164             /*
1165              * Allow other tasks to run while we wait for the
1166              * AP to come online. This also gives a chance
1167              * for the MTRR work(triggered by the AP coming online)
1168              * to be completed in the stop machine context.
1169              */
1170             schedule();
1171         }
1172     }
1173 
1174     if (x86_platform.legacy.warm_reset) {
1175         /*
1176          * Cleanup possible dangling ends...
1177          */
1178         smpboot_restore_warm_reset_vector();
1179     }
1180 
1181     return boot_error;
1182 }
1183 
1184 int native_cpu_up(unsigned int cpu, struct task_struct *tidle)
1185 {
1186     int apicid = apic->cpu_present_to_apicid(cpu);
1187     int cpu0_nmi_registered = 0;
1188     unsigned long flags;
1189     int err, ret = 0;
1190 
1191     lockdep_assert_irqs_enabled();
1192 
1193     pr_debug("++++++++++++++++++++=_---CPU UP  %u\n", cpu);
1194 
1195     if (apicid == BAD_APICID ||
1196         !physid_isset(apicid, phys_cpu_present_map) ||
1197         !apic->apic_id_valid(apicid)) {
1198         pr_err("%s: bad cpu %d\n", __func__, cpu);
1199         return -EINVAL;
1200     }
1201 
1202     /*
1203      * Already booted CPU?
1204      */
1205     if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
1206         pr_debug("do_boot_cpu %d Already started\n", cpu);
1207         return -ENOSYS;
1208     }
1209 
1210     /*
1211      * Save current MTRR state in case it was changed since early boot
1212      * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
1213      */
1214     mtrr_save_state();
1215 
1216     /* x86 CPUs take themselves offline, so delayed offline is OK. */
1217     err = cpu_check_up_prepare(cpu);
1218     if (err && err != -EBUSY)
1219         return err;
1220 
1221     /* the FPU context is blank, nobody can own it */
1222     per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
1223 
1224     err = common_cpu_up(cpu, tidle);
1225     if (err)
1226         return err;
1227 
1228     err = do_boot_cpu(apicid, cpu, tidle, &cpu0_nmi_registered);
1229     if (err) {
1230         pr_err("do_boot_cpu failed(%d) to wakeup CPU#%u\n", err, cpu);
1231         ret = -EIO;
1232         goto unreg_nmi;
1233     }
1234 
1235     /*
1236      * Check TSC synchronization with the AP (keep irqs disabled
1237      * while doing so):
1238      */
1239     local_irq_save(flags);
1240     check_tsc_sync_source(cpu);
1241     local_irq_restore(flags);
1242 
1243     while (!cpu_online(cpu)) {
1244         cpu_relax();
1245         touch_nmi_watchdog();
1246     }
1247 
1248 unreg_nmi:
1249     /*
1250      * Clean up the nmi handler. Do this after the callin and callout sync
1251      * to avoid impact of possible long unregister time.
1252      */
1253     if (cpu0_nmi_registered)
1254         unregister_nmi_handler(NMI_LOCAL, "wake_cpu0");
1255 
1256     return ret;
1257 }
1258 
1259 /**
1260  * arch_disable_smp_support() - disables SMP support for x86 at runtime
1261  */
1262 void arch_disable_smp_support(void)
1263 {
1264     disable_ioapic_support();
1265 }
1266 
1267 /*
1268  * Fall back to non SMP mode after errors.
1269  *
1270  * RED-PEN audit/test this more. I bet there is more state messed up here.
1271  */
1272 static __init void disable_smp(void)
1273 {
1274     pr_info("SMP disabled\n");
1275 
1276     disable_ioapic_support();
1277 
1278     init_cpu_present(cpumask_of(0));
1279     init_cpu_possible(cpumask_of(0));
1280 
1281     if (smp_found_config)
1282         physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1283     else
1284         physid_set_mask_of_physid(0, &phys_cpu_present_map);
1285     cpumask_set_cpu(0, topology_sibling_cpumask(0));
1286     cpumask_set_cpu(0, topology_core_cpumask(0));
1287     cpumask_set_cpu(0, topology_die_cpumask(0));
1288 }
1289 
1290 /*
1291  * Various sanity checks.
1292  */
1293 static void __init smp_sanity_check(void)
1294 {
1295     preempt_disable();
1296 
1297 #if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
1298     if (def_to_bigsmp && nr_cpu_ids > 8) {
1299         unsigned int cpu;
1300         unsigned nr;
1301 
1302         pr_warn("More than 8 CPUs detected - skipping them\n"
1303             "Use CONFIG_X86_BIGSMP\n");
1304 
1305         nr = 0;
1306         for_each_present_cpu(cpu) {
1307             if (nr >= 8)
1308                 set_cpu_present(cpu, false);
1309             nr++;
1310         }
1311 
1312         nr = 0;
1313         for_each_possible_cpu(cpu) {
1314             if (nr >= 8)
1315                 set_cpu_possible(cpu, false);
1316             nr++;
1317         }
1318 
1319         nr_cpu_ids = 8;
1320     }
1321 #endif
1322 
1323     if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
1324         pr_warn("weird, boot CPU (#%d) not listed by the BIOS\n",
1325             hard_smp_processor_id());
1326 
1327         physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1328     }
1329 
1330     /*
1331      * Should not be necessary because the MP table should list the boot
1332      * CPU too, but we do it for the sake of robustness anyway.
1333      */
1334     if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
1335         pr_notice("weird, boot CPU (#%d) not listed by the BIOS\n",
1336               boot_cpu_physical_apicid);
1337         physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1338     }
1339     preempt_enable();
1340 }
1341 
1342 static void __init smp_cpu_index_default(void)
1343 {
1344     int i;
1345     struct cpuinfo_x86 *c;
1346 
1347     for_each_possible_cpu(i) {
1348         c = &cpu_data(i);
1349         /* mark all to hotplug */
1350         c->cpu_index = nr_cpu_ids;
1351     }
1352 }
1353 
1354 static void __init smp_get_logical_apicid(void)
1355 {
1356     if (x2apic_mode)
1357         cpu0_logical_apicid = apic_read(APIC_LDR);
1358     else
1359         cpu0_logical_apicid = GET_APIC_LOGICAL_ID(apic_read(APIC_LDR));
1360 }
1361 
1362 void __init smp_prepare_cpus_common(void)
1363 {
1364     unsigned int i;
1365 
1366     smp_cpu_index_default();
1367 
1368     /*
1369      * Setup boot CPU information
1370      */
1371     smp_store_boot_cpu_info(); /* Final full version of the data */
1372     cpumask_copy(cpu_callin_mask, cpumask_of(0));
1373     mb();
1374 
1375     for_each_possible_cpu(i) {
1376         zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
1377         zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
1378         zalloc_cpumask_var(&per_cpu(cpu_die_map, i), GFP_KERNEL);
1379         zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
1380         zalloc_cpumask_var(&per_cpu(cpu_l2c_shared_map, i), GFP_KERNEL);
1381     }
1382 
1383     /*
1384      * Set 'default' x86 topology, this matches default_topology() in that
1385      * it has NUMA nodes as a topology level. See also
1386      * native_smp_cpus_done().
1387      *
1388      * Must be done before set_cpus_sibling_map() is ran.
1389      */
1390     set_sched_topology(x86_topology);
1391 
1392     set_cpu_sibling_map(0);
1393 }
1394 
1395 /*
1396  * Prepare for SMP bootup.
1397  * @max_cpus: configured maximum number of CPUs, It is a legacy parameter
1398  *            for common interface support.
1399  */
1400 void __init native_smp_prepare_cpus(unsigned int max_cpus)
1401 {
1402     smp_prepare_cpus_common();
1403 
1404     smp_sanity_check();
1405 
1406     switch (apic_intr_mode) {
1407     case APIC_PIC:
1408     case APIC_VIRTUAL_WIRE_NO_CONFIG:
1409         disable_smp();
1410         return;
1411     case APIC_SYMMETRIC_IO_NO_ROUTING:
1412         disable_smp();
1413         /* Setup local timer */
1414         x86_init.timers.setup_percpu_clockev();
1415         return;
1416     case APIC_VIRTUAL_WIRE:
1417     case APIC_SYMMETRIC_IO:
1418         break;
1419     }
1420 
1421     /* Setup local timer */
1422     x86_init.timers.setup_percpu_clockev();
1423 
1424     smp_get_logical_apicid();
1425 
1426     pr_info("CPU0: ");
1427     print_cpu_info(&cpu_data(0));
1428 
1429     uv_system_init();
1430 
1431     set_mtrr_aps_delayed_init();
1432 
1433     smp_quirk_init_udelay();
1434 
1435     speculative_store_bypass_ht_init();
1436 
1437     snp_set_wakeup_secondary_cpu();
1438 }
1439 
1440 void arch_thaw_secondary_cpus_begin(void)
1441 {
1442     set_mtrr_aps_delayed_init();
1443 }
1444 
1445 void arch_thaw_secondary_cpus_end(void)
1446 {
1447     mtrr_aps_init();
1448 }
1449 
1450 /*
1451  * Early setup to make printk work.
1452  */
1453 void __init native_smp_prepare_boot_cpu(void)
1454 {
1455     int me = smp_processor_id();
1456     switch_to_new_gdt(me);
1457     /* already set me in cpu_online_mask in boot_cpu_init() */
1458     cpumask_set_cpu(me, cpu_callout_mask);
1459     cpu_set_state_online(me);
1460     native_pv_lock_init();
1461 }
1462 
1463 void __init calculate_max_logical_packages(void)
1464 {
1465     int ncpus;
1466 
1467     /*
1468      * Today neither Intel nor AMD support heterogeneous systems so
1469      * extrapolate the boot cpu's data to all packages.
1470      */
1471     ncpus = cpu_data(0).booted_cores * topology_max_smt_threads();
1472     __max_logical_packages = DIV_ROUND_UP(total_cpus, ncpus);
1473     pr_info("Max logical packages: %u\n", __max_logical_packages);
1474 }
1475 
1476 void __init native_smp_cpus_done(unsigned int max_cpus)
1477 {
1478     pr_debug("Boot done\n");
1479 
1480     calculate_max_logical_packages();
1481 
1482     /* XXX for now assume numa-in-package and hybrid don't overlap */
1483     if (x86_has_numa_in_package)
1484         set_sched_topology(x86_numa_in_package_topology);
1485     if (cpu_feature_enabled(X86_FEATURE_HYBRID_CPU))
1486         set_sched_topology(x86_hybrid_topology);
1487 
1488     nmi_selftest();
1489     impress_friends();
1490     mtrr_aps_init();
1491 }
1492 
1493 static int __initdata setup_possible_cpus = -1;
1494 static int __init _setup_possible_cpus(char *str)
1495 {
1496     get_option(&str, &setup_possible_cpus);
1497     return 0;
1498 }
1499 early_param("possible_cpus", _setup_possible_cpus);
1500 
1501 
1502 /*
1503  * cpu_possible_mask should be static, it cannot change as cpu's
1504  * are onlined, or offlined. The reason is per-cpu data-structures
1505  * are allocated by some modules at init time, and don't expect to
1506  * do this dynamically on cpu arrival/departure.
1507  * cpu_present_mask on the other hand can change dynamically.
1508  * In case when cpu_hotplug is not compiled, then we resort to current
1509  * behaviour, which is cpu_possible == cpu_present.
1510  * - Ashok Raj
1511  *
1512  * Three ways to find out the number of additional hotplug CPUs:
1513  * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
1514  * - The user can overwrite it with possible_cpus=NUM
1515  * - Otherwise don't reserve additional CPUs.
1516  * We do this because additional CPUs waste a lot of memory.
1517  * -AK
1518  */
1519 __init void prefill_possible_map(void)
1520 {
1521     int i, possible;
1522 
1523     /* No boot processor was found in mptable or ACPI MADT */
1524     if (!num_processors) {
1525         if (boot_cpu_has(X86_FEATURE_APIC)) {
1526             int apicid = boot_cpu_physical_apicid;
1527             int cpu = hard_smp_processor_id();
1528 
1529             pr_warn("Boot CPU (id %d) not listed by BIOS\n", cpu);
1530 
1531             /* Make sure boot cpu is enumerated */
1532             if (apic->cpu_present_to_apicid(0) == BAD_APICID &&
1533                 apic->apic_id_valid(apicid))
1534                 generic_processor_info(apicid, boot_cpu_apic_version);
1535         }
1536 
1537         if (!num_processors)
1538             num_processors = 1;
1539     }
1540 
1541     i = setup_max_cpus ?: 1;
1542     if (setup_possible_cpus == -1) {
1543         possible = num_processors;
1544 #ifdef CONFIG_HOTPLUG_CPU
1545         if (setup_max_cpus)
1546             possible += disabled_cpus;
1547 #else
1548         if (possible > i)
1549             possible = i;
1550 #endif
1551     } else
1552         possible = setup_possible_cpus;
1553 
1554     total_cpus = max_t(int, possible, num_processors + disabled_cpus);
1555 
1556     /* nr_cpu_ids could be reduced via nr_cpus= */
1557     if (possible > nr_cpu_ids) {
1558         pr_warn("%d Processors exceeds NR_CPUS limit of %u\n",
1559             possible, nr_cpu_ids);
1560         possible = nr_cpu_ids;
1561     }
1562 
1563 #ifdef CONFIG_HOTPLUG_CPU
1564     if (!setup_max_cpus)
1565 #endif
1566     if (possible > i) {
1567         pr_warn("%d Processors exceeds max_cpus limit of %u\n",
1568             possible, setup_max_cpus);
1569         possible = i;
1570     }
1571 
1572     nr_cpu_ids = possible;
1573 
1574     pr_info("Allowing %d CPUs, %d hotplug CPUs\n",
1575         possible, max_t(int, possible - num_processors, 0));
1576 
1577     reset_cpu_possible_mask();
1578 
1579     for (i = 0; i < possible; i++)
1580         set_cpu_possible(i, true);
1581 }
1582 
1583 #ifdef CONFIG_HOTPLUG_CPU
1584 
1585 /* Recompute SMT state for all CPUs on offline */
1586 static void recompute_smt_state(void)
1587 {
1588     int max_threads, cpu;
1589 
1590     max_threads = 0;
1591     for_each_online_cpu (cpu) {
1592         int threads = cpumask_weight(topology_sibling_cpumask(cpu));
1593 
1594         if (threads > max_threads)
1595             max_threads = threads;
1596     }
1597     __max_smt_threads = max_threads;
1598 }
1599 
1600 static void remove_siblinginfo(int cpu)
1601 {
1602     int sibling;
1603     struct cpuinfo_x86 *c = &cpu_data(cpu);
1604 
1605     for_each_cpu(sibling, topology_core_cpumask(cpu)) {
1606         cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
1607         /*/
1608          * last thread sibling in this cpu core going down
1609          */
1610         if (cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
1611             cpu_data(sibling).booted_cores--;
1612     }
1613 
1614     for_each_cpu(sibling, topology_die_cpumask(cpu))
1615         cpumask_clear_cpu(cpu, topology_die_cpumask(sibling));
1616 
1617     for_each_cpu(sibling, topology_sibling_cpumask(cpu)) {
1618         cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
1619         if (cpumask_weight(topology_sibling_cpumask(sibling)) == 1)
1620             cpu_data(sibling).smt_active = false;
1621     }
1622 
1623     for_each_cpu(sibling, cpu_llc_shared_mask(cpu))
1624         cpumask_clear_cpu(cpu, cpu_llc_shared_mask(sibling));
1625     for_each_cpu(sibling, cpu_l2c_shared_mask(cpu))
1626         cpumask_clear_cpu(cpu, cpu_l2c_shared_mask(sibling));
1627     cpumask_clear(cpu_llc_shared_mask(cpu));
1628     cpumask_clear(cpu_l2c_shared_mask(cpu));
1629     cpumask_clear(topology_sibling_cpumask(cpu));
1630     cpumask_clear(topology_core_cpumask(cpu));
1631     cpumask_clear(topology_die_cpumask(cpu));
1632     c->cpu_core_id = 0;
1633     c->booted_cores = 0;
1634     cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
1635     recompute_smt_state();
1636 }
1637 
1638 static void remove_cpu_from_maps(int cpu)
1639 {
1640     set_cpu_online(cpu, false);
1641     cpumask_clear_cpu(cpu, cpu_callout_mask);
1642     cpumask_clear_cpu(cpu, cpu_callin_mask);
1643     /* was set by cpu_init() */
1644     cpumask_clear_cpu(cpu, cpu_initialized_mask);
1645     numa_remove_cpu(cpu);
1646 }
1647 
1648 void cpu_disable_common(void)
1649 {
1650     int cpu = smp_processor_id();
1651 
1652     remove_siblinginfo(cpu);
1653 
1654     /* It's now safe to remove this processor from the online map */
1655     lock_vector_lock();
1656     remove_cpu_from_maps(cpu);
1657     unlock_vector_lock();
1658     fixup_irqs();
1659     lapic_offline();
1660 }
1661 
1662 int native_cpu_disable(void)
1663 {
1664     int ret;
1665 
1666     ret = lapic_can_unplug_cpu();
1667     if (ret)
1668         return ret;
1669 
1670     cpu_disable_common();
1671 
1672         /*
1673          * Disable the local APIC. Otherwise IPI broadcasts will reach
1674          * it. It still responds normally to INIT, NMI, SMI, and SIPI
1675          * messages.
1676          *
1677          * Disabling the APIC must happen after cpu_disable_common()
1678          * which invokes fixup_irqs().
1679          *
1680          * Disabling the APIC preserves already set bits in IRR, but
1681          * an interrupt arriving after disabling the local APIC does not
1682          * set the corresponding IRR bit.
1683          *
1684          * fixup_irqs() scans IRR for set bits so it can raise a not
1685          * yet handled interrupt on the new destination CPU via an IPI
1686          * but obviously it can't do so for IRR bits which are not set.
1687          * IOW, interrupts arriving after disabling the local APIC will
1688          * be lost.
1689          */
1690     apic_soft_disable();
1691 
1692     return 0;
1693 }
1694 
1695 int common_cpu_die(unsigned int cpu)
1696 {
1697     int ret = 0;
1698 
1699     /* We don't do anything here: idle task is faking death itself. */
1700 
1701     /* They ack this in play_dead() by setting CPU_DEAD */
1702     if (cpu_wait_death(cpu, 5)) {
1703         if (system_state == SYSTEM_RUNNING)
1704             pr_info("CPU %u is now offline\n", cpu);
1705     } else {
1706         pr_err("CPU %u didn't die...\n", cpu);
1707         ret = -1;
1708     }
1709 
1710     return ret;
1711 }
1712 
1713 void native_cpu_die(unsigned int cpu)
1714 {
1715     common_cpu_die(cpu);
1716 }
1717 
1718 void play_dead_common(void)
1719 {
1720     idle_task_exit();
1721 
1722     /* Ack it */
1723     (void)cpu_report_death();
1724 
1725     /*
1726      * With physical CPU hotplug, we should halt the cpu
1727      */
1728     local_irq_disable();
1729 }
1730 
1731 /**
1732  * cond_wakeup_cpu0 - Wake up CPU0 if needed.
1733  *
1734  * If NMI wants to wake up CPU0, start CPU0.
1735  */
1736 void cond_wakeup_cpu0(void)
1737 {
1738     if (smp_processor_id() == 0 && enable_start_cpu0)
1739         start_cpu0();
1740 }
1741 EXPORT_SYMBOL_GPL(cond_wakeup_cpu0);
1742 
1743 /*
1744  * We need to flush the caches before going to sleep, lest we have
1745  * dirty data in our caches when we come back up.
1746  */
1747 static inline void mwait_play_dead(void)
1748 {
1749     unsigned int eax, ebx, ecx, edx;
1750     unsigned int highest_cstate = 0;
1751     unsigned int highest_subcstate = 0;
1752     void *mwait_ptr;
1753     int i;
1754 
1755     if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
1756         boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
1757         return;
1758     if (!this_cpu_has(X86_FEATURE_MWAIT))
1759         return;
1760     if (!this_cpu_has(X86_FEATURE_CLFLUSH))
1761         return;
1762     if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
1763         return;
1764 
1765     eax = CPUID_MWAIT_LEAF;
1766     ecx = 0;
1767     native_cpuid(&eax, &ebx, &ecx, &edx);
1768 
1769     /*
1770      * eax will be 0 if EDX enumeration is not valid.
1771      * Initialized below to cstate, sub_cstate value when EDX is valid.
1772      */
1773     if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
1774         eax = 0;
1775     } else {
1776         edx >>= MWAIT_SUBSTATE_SIZE;
1777         for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
1778             if (edx & MWAIT_SUBSTATE_MASK) {
1779                 highest_cstate = i;
1780                 highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
1781             }
1782         }
1783         eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
1784             (highest_subcstate - 1);
1785     }
1786 
1787     /*
1788      * This should be a memory location in a cache line which is
1789      * unlikely to be touched by other processors.  The actual
1790      * content is immaterial as it is not actually modified in any way.
1791      */
1792     mwait_ptr = &current_thread_info()->flags;
1793 
1794     wbinvd();
1795 
1796     while (1) {
1797         /*
1798          * The CLFLUSH is a workaround for erratum AAI65 for
1799          * the Xeon 7400 series.  It's not clear it is actually
1800          * needed, but it should be harmless in either case.
1801          * The WBINVD is insufficient due to the spurious-wakeup
1802          * case where we return around the loop.
1803          */
1804         mb();
1805         clflush(mwait_ptr);
1806         mb();
1807         __monitor(mwait_ptr, 0, 0);
1808         mb();
1809         __mwait(eax, 0);
1810 
1811         cond_wakeup_cpu0();
1812     }
1813 }
1814 
1815 void hlt_play_dead(void)
1816 {
1817     if (__this_cpu_read(cpu_info.x86) >= 4)
1818         wbinvd();
1819 
1820     while (1) {
1821         native_halt();
1822 
1823         cond_wakeup_cpu0();
1824     }
1825 }
1826 
1827 void native_play_dead(void)
1828 {
1829     play_dead_common();
1830     tboot_shutdown(TB_SHUTDOWN_WFS);
1831 
1832     mwait_play_dead();  /* Only returns on failure */
1833     if (cpuidle_play_dead())
1834         hlt_play_dead();
1835 }
1836 
1837 #else /* ... !CONFIG_HOTPLUG_CPU */
1838 int native_cpu_disable(void)
1839 {
1840     return -ENOSYS;
1841 }
1842 
1843 void native_cpu_die(unsigned int cpu)
1844 {
1845     /* We said "no" in __cpu_disable */
1846     BUG();
1847 }
1848 
1849 void native_play_dead(void)
1850 {
1851     BUG();
1852 }
1853 
1854 #endif