0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
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
0087 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
0088 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
0089
0090
0091 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
0092 EXPORT_PER_CPU_SYMBOL(cpu_core_map);
0093
0094
0095 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_die_map);
0096 EXPORT_PER_CPU_SYMBOL(cpu_die_map);
0097
0098
0099 DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
0100 EXPORT_PER_CPU_SYMBOL(cpu_info);
0101
0102
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
0109 int __read_mostly __max_smt_threads = 1;
0110
0111
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
0141
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
0152
0153
0154 static void smp_callin(void)
0155 {
0156 int cpuid;
0157
0158
0159
0160
0161
0162
0163
0164 cpuid = smp_processor_id();
0165
0166
0167
0168
0169
0170
0171
0172 apic_ap_setup();
0173
0174
0175
0176
0177
0178 smp_store_cpu_info(cpuid);
0179
0180
0181
0182
0183
0184 set_cpu_sibling_map(raw_smp_processor_id());
0185
0186 ap_init_aperfmperf();
0187
0188
0189
0190
0191
0192
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
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
0212
0213 static void notrace start_secondary(void *unused)
0214 {
0215
0216
0217
0218
0219
0220 cr4_init();
0221
0222 #ifdef CONFIG_X86_32
0223
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
0235 barrier();
0236
0237
0238
0239 check_tsc_sync_target();
0240
0241 speculative_store_bypass_ht_init();
0242
0243
0244
0245
0246
0247
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
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
0267
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
0276
0277 bool topology_smt_supported(void)
0278 {
0279 return smp_num_siblings > 1;
0280 }
0281
0282
0283
0284
0285
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
0302
0303
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
0323
0324
0325
0326 int topology_update_package_map(unsigned int pkg, unsigned int cpu)
0327 {
0328 int new;
0329
0330
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
0346
0347
0348
0349 int topology_update_die_map(unsigned int die, unsigned int cpu)
0350 {
0351 int new;
0352
0353
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;
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
0382
0383
0384 void smp_store_cpu_info(int id)
0385 {
0386 struct cpuinfo_x86 *c = &cpu_data(id);
0387
0388
0389 if (!c->initialized)
0390 *c = boot_cpu_data;
0391 c->cpu_index = id;
0392
0393
0394
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
0464 if (per_cpu(cpu_l2c_id, cpu1) == BAD_APICID)
0465 return match_smt(c, o);
0466
0467
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
0476
0477
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
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500 static const struct x86_cpu_id intel_cod_cpu[] = {
0501 X86_MATCH_INTEL_FAM6_MODEL(HASWELL_X, 0),
0502 X86_MATCH_INTEL_FAM6_MODEL(BROADWELL_X, 0),
0503 X86_MATCH_INTEL_FAM6_MODEL(ANY, 1),
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
0514 if (per_cpu(cpu_llc_id, cpu1) == BAD_APICID)
0515 return false;
0516
0517
0518 if (per_cpu(cpu_llc_id, cpu1) != per_cpu(cpu_llc_id, cpu2))
0519 return false;
0520
0521
0522
0523
0524
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
0599
0600
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
0652
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
0662
0663 if (threads == 1) {
0664
0665
0666
0667
0668 if (cpumask_first(
0669 topology_sibling_cpumask(i)) == i)
0670 c->booted_cores++;
0671
0672
0673
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
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
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
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
0753
0754
0755
0756
0757
0758
0759
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
0776 if (init_udelay != UINT_MAX)
0777 return;
0778
0779
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
0787 init_udelay = UDELAY_10MS_DEFAULT;
0788 }
0789
0790
0791
0792
0793
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
0803
0804
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
0812
0813 udelay(200);
0814 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
0815 maxlvt = lapic_get_maxlvt();
0816 if (maxlvt > 3)
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
0840
0841 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
0842 if (maxlvt > 3)
0843 apic_write(APIC_ESR, 0);
0844 apic_read(APIC_ESR);
0845 }
0846
0847 pr_debug("Asserting INIT\n");
0848
0849
0850
0851
0852
0853
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
0866
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
0876
0877
0878
0879
0880 if (APIC_INTEGRATED(boot_cpu_apic_version))
0881 num_starts = 2;
0882 else
0883 num_starts = 0;
0884
0885
0886
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)
0893 apic_write(APIC_ESR, 0);
0894 apic_read(APIC_ESR);
0895 pr_debug("After apic_write\n");
0896
0897
0898
0899
0900
0901
0902
0903
0904 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
0905 phys_apicid);
0906
0907
0908
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
0922
0923 if (init_udelay == 0)
0924 udelay(10);
0925 else
0926 udelay(200);
0927
0928 if (maxlvt > 3)
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
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;
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
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
0994
0995
0996
0997
0998
0999
1000
1001
1002
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
1015
1016 if (cpu) {
1017 boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
1018 goto out;
1019 }
1020
1021
1022
1023
1024
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
1047 alternatives_enable_smp();
1048
1049 per_cpu(current_task, cpu) = idle;
1050 cpu_init_stack_canary(cpu, idle);
1051
1052
1053 ret = irq_init_percpu_irqstack(cpu);
1054 if (ret)
1055 return ret;
1056
1057 #ifdef CONFIG_X86_32
1058
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
1068
1069
1070
1071
1072 static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,
1073 int *cpu0_nmi_registered)
1074 {
1075
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
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
1092 init_espfix_ap(cpu);
1093
1094
1095 announce_cpu(cpu, apicid);
1096
1097
1098
1099
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
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
1118
1119
1120
1121
1122 cpumask_clear_cpu(cpu, cpu_initialized_mask);
1123 smp_mb();
1124
1125
1126
1127
1128
1129
1130
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
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
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
1162
1163 while (!cpumask_test_cpu(cpu, cpu_callin_mask)) {
1164
1165
1166
1167
1168
1169
1170 schedule();
1171 }
1172 }
1173
1174 if (x86_platform.legacy.warm_reset) {
1175
1176
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
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
1212
1213
1214 mtrr_save_state();
1215
1216
1217 err = cpu_check_up_prepare(cpu);
1218 if (err && err != -EBUSY)
1219 return err;
1220
1221
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
1237
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
1251
1252
1253 if (cpu0_nmi_registered)
1254 unregister_nmi_handler(NMI_LOCAL, "wake_cpu0");
1255
1256 return ret;
1257 }
1258
1259
1260
1261
1262 void arch_disable_smp_support(void)
1263 {
1264 disable_ioapic_support();
1265 }
1266
1267
1268
1269
1270
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
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
1332
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
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
1370
1371 smp_store_boot_cpu_info();
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
1385
1386
1387
1388
1389
1390 set_sched_topology(x86_topology);
1391
1392 set_cpu_sibling_map(0);
1393 }
1394
1395
1396
1397
1398
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
1414 x86_init.timers.setup_percpu_clockev();
1415 return;
1416 case APIC_VIRTUAL_WIRE:
1417 case APIC_SYMMETRIC_IO:
1418 break;
1419 }
1420
1421
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
1452
1453 void __init native_smp_prepare_boot_cpu(void)
1454 {
1455 int me = smp_processor_id();
1456 switch_to_new_gdt(me);
1457
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
1469
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
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
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519 __init void prefill_possible_map(void)
1520 {
1521 int i, possible;
1522
1523
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
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
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
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
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
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
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
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
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
1700
1701
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
1723 (void)cpu_report_death();
1724
1725
1726
1727
1728 local_irq_disable();
1729 }
1730
1731
1732
1733
1734
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
1745
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
1771
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
1789
1790
1791
1792 mwait_ptr = ¤t_thread_info()->flags;
1793
1794 wbinvd();
1795
1796 while (1) {
1797
1798
1799
1800
1801
1802
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();
1833 if (cpuidle_play_dead())
1834 hlt_play_dead();
1835 }
1836
1837 #else
1838 int native_cpu_disable(void)
1839 {
1840 return -ENOSYS;
1841 }
1842
1843 void native_cpu_die(unsigned int cpu)
1844 {
1845
1846 BUG();
1847 }
1848
1849 void native_play_dead(void)
1850 {
1851 BUG();
1852 }
1853
1854 #endif