0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/smp.h>
0017 #include <linux/sched.h>
0018 #include <linux/cpufreq.h>
0019 #include <linux/compiler.h>
0020 #include <linux/dmi.h>
0021 #include <linux/slab.h>
0022
0023 #include <linux/acpi.h>
0024 #include <linux/io.h>
0025 #include <linux/delay.h>
0026 #include <linux/uaccess.h>
0027
0028 #include <acpi/processor.h>
0029 #include <acpi/cppc_acpi.h>
0030
0031 #include <asm/msr.h>
0032 #include <asm/processor.h>
0033 #include <asm/cpufeature.h>
0034 #include <asm/cpu_device_id.h>
0035
0036 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
0037 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
0038 MODULE_LICENSE("GPL");
0039
0040 enum {
0041 UNDEFINED_CAPABLE = 0,
0042 SYSTEM_INTEL_MSR_CAPABLE,
0043 SYSTEM_AMD_MSR_CAPABLE,
0044 SYSTEM_IO_CAPABLE,
0045 };
0046
0047 #define INTEL_MSR_RANGE (0xffff)
0048 #define AMD_MSR_RANGE (0x7)
0049 #define HYGON_MSR_RANGE (0x7)
0050
0051 #define MSR_K7_HWCR_CPB_DIS (1ULL << 25)
0052
0053 struct acpi_cpufreq_data {
0054 unsigned int resume;
0055 unsigned int cpu_feature;
0056 unsigned int acpi_perf_cpu;
0057 cpumask_var_t freqdomain_cpus;
0058 void (*cpu_freq_write)(struct acpi_pct_register *reg, u32 val);
0059 u32 (*cpu_freq_read)(struct acpi_pct_register *reg);
0060 };
0061
0062
0063 static struct acpi_processor_performance __percpu *acpi_perf_data;
0064
0065 static inline struct acpi_processor_performance *to_perf_data(struct acpi_cpufreq_data *data)
0066 {
0067 return per_cpu_ptr(acpi_perf_data, data->acpi_perf_cpu);
0068 }
0069
0070 static struct cpufreq_driver acpi_cpufreq_driver;
0071
0072 static unsigned int acpi_pstate_strict;
0073
0074 static bool boost_state(unsigned int cpu)
0075 {
0076 u32 lo, hi;
0077 u64 msr;
0078
0079 switch (boot_cpu_data.x86_vendor) {
0080 case X86_VENDOR_INTEL:
0081 case X86_VENDOR_CENTAUR:
0082 case X86_VENDOR_ZHAOXIN:
0083 rdmsr_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &lo, &hi);
0084 msr = lo | ((u64)hi << 32);
0085 return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
0086 case X86_VENDOR_HYGON:
0087 case X86_VENDOR_AMD:
0088 rdmsr_on_cpu(cpu, MSR_K7_HWCR, &lo, &hi);
0089 msr = lo | ((u64)hi << 32);
0090 return !(msr & MSR_K7_HWCR_CPB_DIS);
0091 }
0092 return false;
0093 }
0094
0095 static int boost_set_msr(bool enable)
0096 {
0097 u32 msr_addr;
0098 u64 msr_mask, val;
0099
0100 switch (boot_cpu_data.x86_vendor) {
0101 case X86_VENDOR_INTEL:
0102 case X86_VENDOR_CENTAUR:
0103 case X86_VENDOR_ZHAOXIN:
0104 msr_addr = MSR_IA32_MISC_ENABLE;
0105 msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
0106 break;
0107 case X86_VENDOR_HYGON:
0108 case X86_VENDOR_AMD:
0109 msr_addr = MSR_K7_HWCR;
0110 msr_mask = MSR_K7_HWCR_CPB_DIS;
0111 break;
0112 default:
0113 return -EINVAL;
0114 }
0115
0116 rdmsrl(msr_addr, val);
0117
0118 if (enable)
0119 val &= ~msr_mask;
0120 else
0121 val |= msr_mask;
0122
0123 wrmsrl(msr_addr, val);
0124 return 0;
0125 }
0126
0127 static void boost_set_msr_each(void *p_en)
0128 {
0129 bool enable = (bool) p_en;
0130
0131 boost_set_msr(enable);
0132 }
0133
0134 static int set_boost(struct cpufreq_policy *policy, int val)
0135 {
0136 on_each_cpu_mask(policy->cpus, boost_set_msr_each,
0137 (void *)(long)val, 1);
0138 pr_debug("CPU %*pbl: Core Boosting %sabled.\n",
0139 cpumask_pr_args(policy->cpus), val ? "en" : "dis");
0140
0141 return 0;
0142 }
0143
0144 static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf)
0145 {
0146 struct acpi_cpufreq_data *data = policy->driver_data;
0147
0148 if (unlikely(!data))
0149 return -ENODEV;
0150
0151 return cpufreq_show_cpus(data->freqdomain_cpus, buf);
0152 }
0153
0154 cpufreq_freq_attr_ro(freqdomain_cpus);
0155
0156 #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
0157 static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf,
0158 size_t count)
0159 {
0160 int ret;
0161 unsigned int val = 0;
0162
0163 if (!acpi_cpufreq_driver.set_boost)
0164 return -EINVAL;
0165
0166 ret = kstrtouint(buf, 10, &val);
0167 if (ret || val > 1)
0168 return -EINVAL;
0169
0170 cpus_read_lock();
0171 set_boost(policy, val);
0172 cpus_read_unlock();
0173
0174 return count;
0175 }
0176
0177 static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf)
0178 {
0179 return sprintf(buf, "%u\n", acpi_cpufreq_driver.boost_enabled);
0180 }
0181
0182 cpufreq_freq_attr_rw(cpb);
0183 #endif
0184
0185 static int check_est_cpu(unsigned int cpuid)
0186 {
0187 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
0188
0189 return cpu_has(cpu, X86_FEATURE_EST);
0190 }
0191
0192 static int check_amd_hwpstate_cpu(unsigned int cpuid)
0193 {
0194 struct cpuinfo_x86 *cpu = &cpu_data(cpuid);
0195
0196 return cpu_has(cpu, X86_FEATURE_HW_PSTATE);
0197 }
0198
0199 static unsigned extract_io(struct cpufreq_policy *policy, u32 value)
0200 {
0201 struct acpi_cpufreq_data *data = policy->driver_data;
0202 struct acpi_processor_performance *perf;
0203 int i;
0204
0205 perf = to_perf_data(data);
0206
0207 for (i = 0; i < perf->state_count; i++) {
0208 if (value == perf->states[i].status)
0209 return policy->freq_table[i].frequency;
0210 }
0211 return 0;
0212 }
0213
0214 static unsigned extract_msr(struct cpufreq_policy *policy, u32 msr)
0215 {
0216 struct acpi_cpufreq_data *data = policy->driver_data;
0217 struct cpufreq_frequency_table *pos;
0218 struct acpi_processor_performance *perf;
0219
0220 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
0221 msr &= AMD_MSR_RANGE;
0222 else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
0223 msr &= HYGON_MSR_RANGE;
0224 else
0225 msr &= INTEL_MSR_RANGE;
0226
0227 perf = to_perf_data(data);
0228
0229 cpufreq_for_each_entry(pos, policy->freq_table)
0230 if (msr == perf->states[pos->driver_data].status)
0231 return pos->frequency;
0232 return policy->freq_table[0].frequency;
0233 }
0234
0235 static unsigned extract_freq(struct cpufreq_policy *policy, u32 val)
0236 {
0237 struct acpi_cpufreq_data *data = policy->driver_data;
0238
0239 switch (data->cpu_feature) {
0240 case SYSTEM_INTEL_MSR_CAPABLE:
0241 case SYSTEM_AMD_MSR_CAPABLE:
0242 return extract_msr(policy, val);
0243 case SYSTEM_IO_CAPABLE:
0244 return extract_io(policy, val);
0245 default:
0246 return 0;
0247 }
0248 }
0249
0250 static u32 cpu_freq_read_intel(struct acpi_pct_register *not_used)
0251 {
0252 u32 val, dummy __always_unused;
0253
0254 rdmsr(MSR_IA32_PERF_CTL, val, dummy);
0255 return val;
0256 }
0257
0258 static void cpu_freq_write_intel(struct acpi_pct_register *not_used, u32 val)
0259 {
0260 u32 lo, hi;
0261
0262 rdmsr(MSR_IA32_PERF_CTL, lo, hi);
0263 lo = (lo & ~INTEL_MSR_RANGE) | (val & INTEL_MSR_RANGE);
0264 wrmsr(MSR_IA32_PERF_CTL, lo, hi);
0265 }
0266
0267 static u32 cpu_freq_read_amd(struct acpi_pct_register *not_used)
0268 {
0269 u32 val, dummy __always_unused;
0270
0271 rdmsr(MSR_AMD_PERF_CTL, val, dummy);
0272 return val;
0273 }
0274
0275 static void cpu_freq_write_amd(struct acpi_pct_register *not_used, u32 val)
0276 {
0277 wrmsr(MSR_AMD_PERF_CTL, val, 0);
0278 }
0279
0280 static u32 cpu_freq_read_io(struct acpi_pct_register *reg)
0281 {
0282 u32 val;
0283
0284 acpi_os_read_port(reg->address, &val, reg->bit_width);
0285 return val;
0286 }
0287
0288 static void cpu_freq_write_io(struct acpi_pct_register *reg, u32 val)
0289 {
0290 acpi_os_write_port(reg->address, val, reg->bit_width);
0291 }
0292
0293 struct drv_cmd {
0294 struct acpi_pct_register *reg;
0295 u32 val;
0296 union {
0297 void (*write)(struct acpi_pct_register *reg, u32 val);
0298 u32 (*read)(struct acpi_pct_register *reg);
0299 } func;
0300 };
0301
0302
0303 static void do_drv_read(void *_cmd)
0304 {
0305 struct drv_cmd *cmd = _cmd;
0306
0307 cmd->val = cmd->func.read(cmd->reg);
0308 }
0309
0310 static u32 drv_read(struct acpi_cpufreq_data *data, const struct cpumask *mask)
0311 {
0312 struct acpi_processor_performance *perf = to_perf_data(data);
0313 struct drv_cmd cmd = {
0314 .reg = &perf->control_register,
0315 .func.read = data->cpu_freq_read,
0316 };
0317 int err;
0318
0319 err = smp_call_function_any(mask, do_drv_read, &cmd, 1);
0320 WARN_ON_ONCE(err);
0321 return cmd.val;
0322 }
0323
0324
0325 static void do_drv_write(void *_cmd)
0326 {
0327 struct drv_cmd *cmd = _cmd;
0328
0329 cmd->func.write(cmd->reg, cmd->val);
0330 }
0331
0332 static void drv_write(struct acpi_cpufreq_data *data,
0333 const struct cpumask *mask, u32 val)
0334 {
0335 struct acpi_processor_performance *perf = to_perf_data(data);
0336 struct drv_cmd cmd = {
0337 .reg = &perf->control_register,
0338 .val = val,
0339 .func.write = data->cpu_freq_write,
0340 };
0341 int this_cpu;
0342
0343 this_cpu = get_cpu();
0344 if (cpumask_test_cpu(this_cpu, mask))
0345 do_drv_write(&cmd);
0346
0347 smp_call_function_many(mask, do_drv_write, &cmd, 1);
0348 put_cpu();
0349 }
0350
0351 static u32 get_cur_val(const struct cpumask *mask, struct acpi_cpufreq_data *data)
0352 {
0353 u32 val;
0354
0355 if (unlikely(cpumask_empty(mask)))
0356 return 0;
0357
0358 val = drv_read(data, mask);
0359
0360 pr_debug("%s = %u\n", __func__, val);
0361
0362 return val;
0363 }
0364
0365 static unsigned int get_cur_freq_on_cpu(unsigned int cpu)
0366 {
0367 struct acpi_cpufreq_data *data;
0368 struct cpufreq_policy *policy;
0369 unsigned int freq;
0370 unsigned int cached_freq;
0371
0372 pr_debug("%s (%d)\n", __func__, cpu);
0373
0374 policy = cpufreq_cpu_get_raw(cpu);
0375 if (unlikely(!policy))
0376 return 0;
0377
0378 data = policy->driver_data;
0379 if (unlikely(!data || !policy->freq_table))
0380 return 0;
0381
0382 cached_freq = policy->freq_table[to_perf_data(data)->state].frequency;
0383 freq = extract_freq(policy, get_cur_val(cpumask_of(cpu), data));
0384 if (freq != cached_freq) {
0385
0386
0387
0388
0389 data->resume = 1;
0390 }
0391
0392 pr_debug("cur freq = %u\n", freq);
0393
0394 return freq;
0395 }
0396
0397 static unsigned int check_freqs(struct cpufreq_policy *policy,
0398 const struct cpumask *mask, unsigned int freq)
0399 {
0400 struct acpi_cpufreq_data *data = policy->driver_data;
0401 unsigned int cur_freq;
0402 unsigned int i;
0403
0404 for (i = 0; i < 100; i++) {
0405 cur_freq = extract_freq(policy, get_cur_val(mask, data));
0406 if (cur_freq == freq)
0407 return 1;
0408 udelay(10);
0409 }
0410 return 0;
0411 }
0412
0413 static int acpi_cpufreq_target(struct cpufreq_policy *policy,
0414 unsigned int index)
0415 {
0416 struct acpi_cpufreq_data *data = policy->driver_data;
0417 struct acpi_processor_performance *perf;
0418 const struct cpumask *mask;
0419 unsigned int next_perf_state = 0;
0420 int result = 0;
0421
0422 if (unlikely(!data)) {
0423 return -ENODEV;
0424 }
0425
0426 perf = to_perf_data(data);
0427 next_perf_state = policy->freq_table[index].driver_data;
0428 if (perf->state == next_perf_state) {
0429 if (unlikely(data->resume)) {
0430 pr_debug("Called after resume, resetting to P%d\n",
0431 next_perf_state);
0432 data->resume = 0;
0433 } else {
0434 pr_debug("Already at target state (P%d)\n",
0435 next_perf_state);
0436 return 0;
0437 }
0438 }
0439
0440
0441
0442
0443
0444 mask = policy->shared_type == CPUFREQ_SHARED_TYPE_ANY ?
0445 cpumask_of(policy->cpu) : policy->cpus;
0446
0447 drv_write(data, mask, perf->states[next_perf_state].control);
0448
0449 if (acpi_pstate_strict) {
0450 if (!check_freqs(policy, mask,
0451 policy->freq_table[index].frequency)) {
0452 pr_debug("%s (%d)\n", __func__, policy->cpu);
0453 result = -EAGAIN;
0454 }
0455 }
0456
0457 if (!result)
0458 perf->state = next_perf_state;
0459
0460 return result;
0461 }
0462
0463 static unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy,
0464 unsigned int target_freq)
0465 {
0466 struct acpi_cpufreq_data *data = policy->driver_data;
0467 struct acpi_processor_performance *perf;
0468 struct cpufreq_frequency_table *entry;
0469 unsigned int next_perf_state, next_freq, index;
0470
0471
0472
0473
0474 if (policy->cached_target_freq == target_freq)
0475 index = policy->cached_resolved_idx;
0476 else
0477 index = cpufreq_table_find_index_dl(policy, target_freq,
0478 false);
0479
0480 entry = &policy->freq_table[index];
0481 next_freq = entry->frequency;
0482 next_perf_state = entry->driver_data;
0483
0484 perf = to_perf_data(data);
0485 if (perf->state == next_perf_state) {
0486 if (unlikely(data->resume))
0487 data->resume = 0;
0488 else
0489 return next_freq;
0490 }
0491
0492 data->cpu_freq_write(&perf->control_register,
0493 perf->states[next_perf_state].control);
0494 perf->state = next_perf_state;
0495 return next_freq;
0496 }
0497
0498 static unsigned long
0499 acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu)
0500 {
0501 struct acpi_processor_performance *perf;
0502
0503 perf = to_perf_data(data);
0504 if (cpu_khz) {
0505
0506 unsigned int i;
0507 unsigned long freq;
0508 unsigned long freqn = perf->states[0].core_frequency * 1000;
0509
0510 for (i = 0; i < (perf->state_count-1); i++) {
0511 freq = freqn;
0512 freqn = perf->states[i+1].core_frequency * 1000;
0513 if ((2 * cpu_khz) > (freqn + freq)) {
0514 perf->state = i;
0515 return freq;
0516 }
0517 }
0518 perf->state = perf->state_count-1;
0519 return freqn;
0520 } else {
0521
0522 perf->state = 0;
0523 return perf->states[0].core_frequency * 1000;
0524 }
0525 }
0526
0527 static void free_acpi_perf_data(void)
0528 {
0529 unsigned int i;
0530
0531
0532 for_each_possible_cpu(i)
0533 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
0534 ->shared_cpu_map);
0535 free_percpu(acpi_perf_data);
0536 }
0537
0538 static int cpufreq_boost_online(unsigned int cpu)
0539 {
0540
0541
0542
0543
0544 return boost_set_msr(acpi_cpufreq_driver.boost_enabled);
0545 }
0546
0547 static int cpufreq_boost_down_prep(unsigned int cpu)
0548 {
0549
0550
0551
0552
0553 return boost_set_msr(1);
0554 }
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564 static int __init acpi_cpufreq_early_init(void)
0565 {
0566 unsigned int i;
0567 pr_debug("%s\n", __func__);
0568
0569 acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
0570 if (!acpi_perf_data) {
0571 pr_debug("Memory allocation error for acpi_perf_data.\n");
0572 return -ENOMEM;
0573 }
0574 for_each_possible_cpu(i) {
0575 if (!zalloc_cpumask_var_node(
0576 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
0577 GFP_KERNEL, cpu_to_node(i))) {
0578
0579
0580 free_acpi_perf_data();
0581 return -ENOMEM;
0582 }
0583 }
0584
0585
0586 acpi_processor_preregister_performance(acpi_perf_data);
0587 return 0;
0588 }
0589
0590 #ifdef CONFIG_SMP
0591
0592
0593
0594
0595
0596
0597 static int bios_with_sw_any_bug;
0598
0599 static int sw_any_bug_found(const struct dmi_system_id *d)
0600 {
0601 bios_with_sw_any_bug = 1;
0602 return 0;
0603 }
0604
0605 static const struct dmi_system_id sw_any_bug_dmi_table[] = {
0606 {
0607 .callback = sw_any_bug_found,
0608 .ident = "Supermicro Server X6DLP",
0609 .matches = {
0610 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
0611 DMI_MATCH(DMI_BIOS_VERSION, "080010"),
0612 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
0613 },
0614 },
0615 { }
0616 };
0617
0618 static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c)
0619 {
0620
0621
0622
0623
0624
0625 if (c->x86_vendor == X86_VENDOR_INTEL) {
0626 if ((c->x86 == 15) &&
0627 (c->x86_model == 6) &&
0628 (c->x86_stepping == 8)) {
0629 pr_info("Intel(R) Xeon(R) 7100 Errata AL30, processors may lock up on frequency changes: disabling acpi-cpufreq\n");
0630 return -ENODEV;
0631 }
0632 }
0633 return 0;
0634 }
0635 #endif
0636
0637 #ifdef CONFIG_ACPI_CPPC_LIB
0638 static u64 get_max_boost_ratio(unsigned int cpu)
0639 {
0640 struct cppc_perf_caps perf_caps;
0641 u64 highest_perf, nominal_perf;
0642 int ret;
0643
0644 if (acpi_pstate_strict)
0645 return 0;
0646
0647 ret = cppc_get_perf_caps(cpu, &perf_caps);
0648 if (ret) {
0649 pr_debug("CPU%d: Unable to get performance capabilities (%d)\n",
0650 cpu, ret);
0651 return 0;
0652 }
0653
0654 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
0655 highest_perf = amd_get_highest_perf();
0656 else
0657 highest_perf = perf_caps.highest_perf;
0658
0659 nominal_perf = perf_caps.nominal_perf;
0660
0661 if (!highest_perf || !nominal_perf) {
0662 pr_debug("CPU%d: highest or nominal performance missing\n", cpu);
0663 return 0;
0664 }
0665
0666 if (highest_perf < nominal_perf) {
0667 pr_debug("CPU%d: nominal performance above highest\n", cpu);
0668 return 0;
0669 }
0670
0671 return div_u64(highest_perf << SCHED_CAPACITY_SHIFT, nominal_perf);
0672 }
0673 #else
0674 static inline u64 get_max_boost_ratio(unsigned int cpu) { return 0; }
0675 #endif
0676
0677 static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy)
0678 {
0679 struct cpufreq_frequency_table *freq_table;
0680 struct acpi_processor_performance *perf;
0681 struct acpi_cpufreq_data *data;
0682 unsigned int cpu = policy->cpu;
0683 struct cpuinfo_x86 *c = &cpu_data(cpu);
0684 unsigned int valid_states = 0;
0685 unsigned int result = 0;
0686 u64 max_boost_ratio;
0687 unsigned int i;
0688 #ifdef CONFIG_SMP
0689 static int blacklisted;
0690 #endif
0691
0692 pr_debug("%s\n", __func__);
0693
0694 #ifdef CONFIG_SMP
0695 if (blacklisted)
0696 return blacklisted;
0697 blacklisted = acpi_cpufreq_blacklist(c);
0698 if (blacklisted)
0699 return blacklisted;
0700 #endif
0701
0702 data = kzalloc(sizeof(*data), GFP_KERNEL);
0703 if (!data)
0704 return -ENOMEM;
0705
0706 if (!zalloc_cpumask_var(&data->freqdomain_cpus, GFP_KERNEL)) {
0707 result = -ENOMEM;
0708 goto err_free;
0709 }
0710
0711 perf = per_cpu_ptr(acpi_perf_data, cpu);
0712 data->acpi_perf_cpu = cpu;
0713 policy->driver_data = data;
0714
0715 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC))
0716 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
0717
0718 result = acpi_processor_register_performance(perf, cpu);
0719 if (result)
0720 goto err_free_mask;
0721
0722 policy->shared_type = perf->shared_type;
0723
0724
0725
0726
0727
0728 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
0729 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
0730 cpumask_copy(policy->cpus, perf->shared_cpu_map);
0731 }
0732 cpumask_copy(data->freqdomain_cpus, perf->shared_cpu_map);
0733
0734 #ifdef CONFIG_SMP
0735 dmi_check_system(sw_any_bug_dmi_table);
0736 if (bios_with_sw_any_bug && !policy_is_shared(policy)) {
0737 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
0738 cpumask_copy(policy->cpus, topology_core_cpumask(cpu));
0739 }
0740
0741 if (check_amd_hwpstate_cpu(cpu) && boot_cpu_data.x86 < 0x19 &&
0742 !acpi_pstate_strict) {
0743 cpumask_clear(policy->cpus);
0744 cpumask_set_cpu(cpu, policy->cpus);
0745 cpumask_copy(data->freqdomain_cpus,
0746 topology_sibling_cpumask(cpu));
0747 policy->shared_type = CPUFREQ_SHARED_TYPE_HW;
0748 pr_info_once("overriding BIOS provided _PSD data\n");
0749 }
0750 #endif
0751
0752
0753 if (perf->state_count <= 1) {
0754 pr_debug("No P-States\n");
0755 result = -ENODEV;
0756 goto err_unreg;
0757 }
0758
0759 if (perf->control_register.space_id != perf->status_register.space_id) {
0760 result = -ENODEV;
0761 goto err_unreg;
0762 }
0763
0764 switch (perf->control_register.space_id) {
0765 case ACPI_ADR_SPACE_SYSTEM_IO:
0766 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
0767 boot_cpu_data.x86 == 0xf) {
0768 pr_debug("AMD K8 systems must use native drivers.\n");
0769 result = -ENODEV;
0770 goto err_unreg;
0771 }
0772 pr_debug("SYSTEM IO addr space\n");
0773 data->cpu_feature = SYSTEM_IO_CAPABLE;
0774 data->cpu_freq_read = cpu_freq_read_io;
0775 data->cpu_freq_write = cpu_freq_write_io;
0776 break;
0777 case ACPI_ADR_SPACE_FIXED_HARDWARE:
0778 pr_debug("HARDWARE addr space\n");
0779 if (check_est_cpu(cpu)) {
0780 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE;
0781 data->cpu_freq_read = cpu_freq_read_intel;
0782 data->cpu_freq_write = cpu_freq_write_intel;
0783 break;
0784 }
0785 if (check_amd_hwpstate_cpu(cpu)) {
0786 data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE;
0787 data->cpu_freq_read = cpu_freq_read_amd;
0788 data->cpu_freq_write = cpu_freq_write_amd;
0789 break;
0790 }
0791 result = -ENODEV;
0792 goto err_unreg;
0793 default:
0794 pr_debug("Unknown addr space %d\n",
0795 (u32) (perf->control_register.space_id));
0796 result = -ENODEV;
0797 goto err_unreg;
0798 }
0799
0800 freq_table = kcalloc(perf->state_count + 1, sizeof(*freq_table),
0801 GFP_KERNEL);
0802 if (!freq_table) {
0803 result = -ENOMEM;
0804 goto err_unreg;
0805 }
0806
0807
0808 policy->cpuinfo.transition_latency = 0;
0809 for (i = 0; i < perf->state_count; i++) {
0810 if ((perf->states[i].transition_latency * 1000) >
0811 policy->cpuinfo.transition_latency)
0812 policy->cpuinfo.transition_latency =
0813 perf->states[i].transition_latency * 1000;
0814 }
0815
0816
0817 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE &&
0818 policy->cpuinfo.transition_latency > 20 * 1000) {
0819 policy->cpuinfo.transition_latency = 20 * 1000;
0820 pr_info_once("P-state transition latency capped at 20 uS\n");
0821 }
0822
0823
0824 for (i = 0; i < perf->state_count; i++) {
0825 if (i > 0 && perf->states[i].core_frequency >=
0826 freq_table[valid_states-1].frequency / 1000)
0827 continue;
0828
0829 freq_table[valid_states].driver_data = i;
0830 freq_table[valid_states].frequency =
0831 perf->states[i].core_frequency * 1000;
0832 valid_states++;
0833 }
0834 freq_table[valid_states].frequency = CPUFREQ_TABLE_END;
0835
0836 max_boost_ratio = get_max_boost_ratio(cpu);
0837 if (max_boost_ratio) {
0838 unsigned int freq = freq_table[0].frequency;
0839
0840
0841
0842
0843
0844
0845
0846 policy->cpuinfo.max_freq = freq * max_boost_ratio >> SCHED_CAPACITY_SHIFT;
0847 } else {
0848
0849
0850
0851
0852
0853
0854 arch_set_max_freq_ratio(true);
0855 }
0856
0857 policy->freq_table = freq_table;
0858 perf->state = 0;
0859
0860 switch (perf->control_register.space_id) {
0861 case ACPI_ADR_SPACE_SYSTEM_IO:
0862
0863
0864
0865
0866
0867
0868 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
0869 break;
0870 case ACPI_ADR_SPACE_FIXED_HARDWARE:
0871 acpi_cpufreq_driver.get = get_cur_freq_on_cpu;
0872 break;
0873 default:
0874 break;
0875 }
0876
0877
0878 acpi_processor_notify_smm(THIS_MODULE);
0879
0880 pr_debug("CPU%u - ACPI performance management activated.\n", cpu);
0881 for (i = 0; i < perf->state_count; i++)
0882 pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
0883 (i == perf->state ? '*' : ' '), i,
0884 (u32) perf->states[i].core_frequency,
0885 (u32) perf->states[i].power,
0886 (u32) perf->states[i].transition_latency);
0887
0888
0889
0890
0891
0892 data->resume = 1;
0893
0894 policy->fast_switch_possible = !acpi_pstate_strict &&
0895 !(policy_is_shared(policy) && policy->shared_type != CPUFREQ_SHARED_TYPE_ANY);
0896
0897 if (perf->states[0].core_frequency * 1000 != freq_table[0].frequency)
0898 pr_warn(FW_WARN "P-state 0 is not max freq\n");
0899
0900 return result;
0901
0902 err_unreg:
0903 acpi_processor_unregister_performance(cpu);
0904 err_free_mask:
0905 free_cpumask_var(data->freqdomain_cpus);
0906 err_free:
0907 kfree(data);
0908 policy->driver_data = NULL;
0909
0910 return result;
0911 }
0912
0913 static int acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy)
0914 {
0915 struct acpi_cpufreq_data *data = policy->driver_data;
0916
0917 pr_debug("%s\n", __func__);
0918
0919 policy->fast_switch_possible = false;
0920 policy->driver_data = NULL;
0921 acpi_processor_unregister_performance(data->acpi_perf_cpu);
0922 free_cpumask_var(data->freqdomain_cpus);
0923 kfree(policy->freq_table);
0924 kfree(data);
0925
0926 return 0;
0927 }
0928
0929 static int acpi_cpufreq_resume(struct cpufreq_policy *policy)
0930 {
0931 struct acpi_cpufreq_data *data = policy->driver_data;
0932
0933 pr_debug("%s\n", __func__);
0934
0935 data->resume = 1;
0936
0937 return 0;
0938 }
0939
0940 static struct freq_attr *acpi_cpufreq_attr[] = {
0941 &cpufreq_freq_attr_scaling_available_freqs,
0942 &freqdomain_cpus,
0943 #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
0944 &cpb,
0945 #endif
0946 NULL,
0947 };
0948
0949 static struct cpufreq_driver acpi_cpufreq_driver = {
0950 .verify = cpufreq_generic_frequency_table_verify,
0951 .target_index = acpi_cpufreq_target,
0952 .fast_switch = acpi_cpufreq_fast_switch,
0953 .bios_limit = acpi_processor_get_bios_limit,
0954 .init = acpi_cpufreq_cpu_init,
0955 .exit = acpi_cpufreq_cpu_exit,
0956 .resume = acpi_cpufreq_resume,
0957 .name = "acpi-cpufreq",
0958 .attr = acpi_cpufreq_attr,
0959 };
0960
0961 static enum cpuhp_state acpi_cpufreq_online;
0962
0963 static void __init acpi_cpufreq_boost_init(void)
0964 {
0965 int ret;
0966
0967 if (!(boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA))) {
0968 pr_debug("Boost capabilities not present in the processor\n");
0969 return;
0970 }
0971
0972 acpi_cpufreq_driver.set_boost = set_boost;
0973 acpi_cpufreq_driver.boost_enabled = boost_state(0);
0974
0975
0976
0977
0978
0979 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpufreq/acpi:online",
0980 cpufreq_boost_online, cpufreq_boost_down_prep);
0981 if (ret < 0) {
0982 pr_err("acpi_cpufreq: failed to register hotplug callbacks\n");
0983 return;
0984 }
0985 acpi_cpufreq_online = ret;
0986 }
0987
0988 static void acpi_cpufreq_boost_exit(void)
0989 {
0990 if (acpi_cpufreq_online > 0)
0991 cpuhp_remove_state_nocalls(acpi_cpufreq_online);
0992 }
0993
0994 static int __init acpi_cpufreq_init(void)
0995 {
0996 int ret;
0997
0998 if (acpi_disabled)
0999 return -ENODEV;
1000
1001
1002 if (cpufreq_get_current_driver())
1003 return -EEXIST;
1004
1005 pr_debug("%s\n", __func__);
1006
1007 ret = acpi_cpufreq_early_init();
1008 if (ret)
1009 return ret;
1010
1011 #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB
1012
1013
1014
1015
1016
1017
1018 if (!check_amd_hwpstate_cpu(0)) {
1019 struct freq_attr **attr;
1020
1021 pr_debug("CPB unsupported, do not expose it\n");
1022
1023 for (attr = acpi_cpufreq_attr; *attr; attr++)
1024 if (*attr == &cpb) {
1025 *attr = NULL;
1026 break;
1027 }
1028 }
1029 #endif
1030 acpi_cpufreq_boost_init();
1031
1032 ret = cpufreq_register_driver(&acpi_cpufreq_driver);
1033 if (ret) {
1034 free_acpi_perf_data();
1035 acpi_cpufreq_boost_exit();
1036 }
1037 return ret;
1038 }
1039
1040 static void __exit acpi_cpufreq_exit(void)
1041 {
1042 pr_debug("%s\n", __func__);
1043
1044 acpi_cpufreq_boost_exit();
1045
1046 cpufreq_unregister_driver(&acpi_cpufreq_driver);
1047
1048 free_acpi_perf_data();
1049 }
1050
1051 module_param(acpi_pstate_strict, uint, 0644);
1052 MODULE_PARM_DESC(acpi_pstate_strict,
1053 "value 0 or non-zero. non-zero -> strict ACPI checks are "
1054 "performed during frequency changes.");
1055
1056 late_initcall(acpi_cpufreq_init);
1057 module_exit(acpi_cpufreq_exit);
1058
1059 static const struct x86_cpu_id __maybe_unused acpi_cpufreq_ids[] = {
1060 X86_MATCH_FEATURE(X86_FEATURE_ACPI, NULL),
1061 X86_MATCH_FEATURE(X86_FEATURE_HW_PSTATE, NULL),
1062 {}
1063 };
1064 MODULE_DEVICE_TABLE(x86cpu, acpi_cpufreq_ids);
1065
1066 static const struct acpi_device_id __maybe_unused processor_device_ids[] = {
1067 {ACPI_PROCESSOR_OBJECT_HID, },
1068 {ACPI_PROCESSOR_DEVICE_HID, },
1069 {},
1070 };
1071 MODULE_DEVICE_TABLE(acpi, processor_device_ids);
1072
1073 MODULE_ALIAS("acpi");