1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * acpi-cpufreq.c - ACPI Processor P-States Driver 4 * 5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> 6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 7 * Copyright (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de> 8 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com> 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/init.h> 16 #include <linux/smp.h> 17 #include <linux/sched.h> 18 #include <linux/cpufreq.h> 19 #include <linux/compiler.h> 20 #include <linux/dmi.h> 21 #include <linux/slab.h> 22 #include <linux/string_helpers.h> 23 #include <linux/platform_device.h> 24 25 #include <linux/acpi.h> 26 #include <linux/io.h> 27 #include <linux/delay.h> 28 #include <linux/uaccess.h> 29 30 #include <acpi/processor.h> 31 #include <acpi/cppc_acpi.h> 32 33 #include <asm/msr.h> 34 #include <asm/processor.h> 35 #include <asm/cpufeature.h> 36 #include <asm/cpu_device_id.h> 37 38 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski"); 39 MODULE_DESCRIPTION("ACPI Processor P-States Driver"); 40 MODULE_LICENSE("GPL"); 41 42 enum { 43 UNDEFINED_CAPABLE = 0, 44 SYSTEM_INTEL_MSR_CAPABLE, 45 SYSTEM_AMD_MSR_CAPABLE, 46 SYSTEM_IO_CAPABLE, 47 }; 48 49 #define INTEL_MSR_RANGE (0xffff) 50 #define AMD_MSR_RANGE (0x7) 51 #define HYGON_MSR_RANGE (0x7) 52 53 struct acpi_cpufreq_data { 54 unsigned int resume; 55 unsigned int cpu_feature; 56 unsigned int acpi_perf_cpu; 57 cpumask_var_t freqdomain_cpus; 58 void (*cpu_freq_write)(struct acpi_pct_register *reg, u32 val); 59 u32 (*cpu_freq_read)(struct acpi_pct_register *reg); 60 }; 61 62 /* acpi_perf_data is a pointer to percpu data. */ 63 static struct acpi_processor_performance __percpu *acpi_perf_data; 64 65 static inline struct acpi_processor_performance *to_perf_data(struct acpi_cpufreq_data *data) 66 { 67 return per_cpu_ptr(acpi_perf_data, data->acpi_perf_cpu); 68 } 69 70 static struct cpufreq_driver acpi_cpufreq_driver; 71 72 static unsigned int acpi_pstate_strict; 73 74 static bool boost_state(unsigned int cpu) 75 { 76 u64 msr; 77 78 switch (boot_cpu_data.x86_vendor) { 79 case X86_VENDOR_INTEL: 80 case X86_VENDOR_CENTAUR: 81 case X86_VENDOR_ZHAOXIN: 82 rdmsrq_on_cpu(cpu, MSR_IA32_MISC_ENABLE, &msr); 83 return !(msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE); 84 case X86_VENDOR_HYGON: 85 case X86_VENDOR_AMD: 86 rdmsrq_on_cpu(cpu, MSR_K7_HWCR, &msr); 87 return !(msr & MSR_K7_HWCR_CPB_DIS); 88 } 89 return false; 90 } 91 92 static int boost_set_msr(bool enable) 93 { 94 u32 msr_addr; 95 u64 msr_mask, val; 96 97 switch (boot_cpu_data.x86_vendor) { 98 case X86_VENDOR_INTEL: 99 case X86_VENDOR_CENTAUR: 100 case X86_VENDOR_ZHAOXIN: 101 msr_addr = MSR_IA32_MISC_ENABLE; 102 msr_mask = MSR_IA32_MISC_ENABLE_TURBO_DISABLE; 103 break; 104 case X86_VENDOR_HYGON: 105 case X86_VENDOR_AMD: 106 msr_addr = MSR_K7_HWCR; 107 msr_mask = MSR_K7_HWCR_CPB_DIS; 108 break; 109 default: 110 return -EINVAL; 111 } 112 113 rdmsrq(msr_addr, val); 114 115 if (enable) 116 val &= ~msr_mask; 117 else 118 val |= msr_mask; 119 120 wrmsrq(msr_addr, val); 121 return 0; 122 } 123 124 static void boost_set_msr_each(void *p_en) 125 { 126 bool enable = (bool) p_en; 127 128 boost_set_msr(enable); 129 } 130 131 static int set_boost(struct cpufreq_policy *policy, int val) 132 { 133 on_each_cpu_mask(policy->cpus, boost_set_msr_each, 134 (void *)(long)val, 1); 135 pr_debug("CPU %*pbl: Core Boosting %s.\n", 136 cpumask_pr_args(policy->cpus), str_enabled_disabled(val)); 137 138 return 0; 139 } 140 141 static ssize_t show_freqdomain_cpus(struct cpufreq_policy *policy, char *buf) 142 { 143 struct acpi_cpufreq_data *data = policy->driver_data; 144 145 if (unlikely(!data)) 146 return -ENODEV; 147 148 return cpufreq_show_cpus(data->freqdomain_cpus, buf); 149 } 150 151 cpufreq_freq_attr_ro(freqdomain_cpus); 152 153 #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB 154 static ssize_t store_cpb(struct cpufreq_policy *policy, const char *buf, 155 size_t count) 156 { 157 int ret; 158 unsigned int val = 0; 159 160 if (!acpi_cpufreq_driver.set_boost) 161 return -EINVAL; 162 163 ret = kstrtouint(buf, 10, &val); 164 if (ret || val > 1) 165 return -EINVAL; 166 167 cpus_read_lock(); 168 set_boost(policy, val); 169 cpus_read_unlock(); 170 171 return count; 172 } 173 174 static ssize_t show_cpb(struct cpufreq_policy *policy, char *buf) 175 { 176 return sprintf(buf, "%u\n", acpi_cpufreq_driver.boost_enabled); 177 } 178 179 cpufreq_freq_attr_rw(cpb); 180 #endif 181 182 static int check_est_cpu(unsigned int cpuid) 183 { 184 struct cpuinfo_x86 *cpu = &cpu_data(cpuid); 185 186 return cpu_has(cpu, X86_FEATURE_EST); 187 } 188 189 static int check_amd_hwpstate_cpu(unsigned int cpuid) 190 { 191 struct cpuinfo_x86 *cpu = &cpu_data(cpuid); 192 193 return cpu_has(cpu, X86_FEATURE_HW_PSTATE); 194 } 195 196 static unsigned extract_io(struct cpufreq_policy *policy, u32 value) 197 { 198 struct acpi_cpufreq_data *data = policy->driver_data; 199 struct acpi_processor_performance *perf; 200 struct cpufreq_frequency_table *pos; 201 202 perf = to_perf_data(data); 203 204 cpufreq_for_each_entry(pos, policy->freq_table) 205 if (value == perf->states[pos->driver_data].status) 206 return pos->frequency; 207 return 0; 208 } 209 210 static unsigned extract_msr(struct cpufreq_policy *policy, u32 msr) 211 { 212 struct acpi_cpufreq_data *data = policy->driver_data; 213 struct cpufreq_frequency_table *pos; 214 struct acpi_processor_performance *perf; 215 216 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) 217 msr &= AMD_MSR_RANGE; 218 else if (boot_cpu_data.x86_vendor == X86_VENDOR_HYGON) 219 msr &= HYGON_MSR_RANGE; 220 else 221 msr &= INTEL_MSR_RANGE; 222 223 perf = to_perf_data(data); 224 225 cpufreq_for_each_entry(pos, policy->freq_table) 226 if (msr == perf->states[pos->driver_data].status) 227 return pos->frequency; 228 return policy->freq_table[0].frequency; 229 } 230 231 static unsigned extract_freq(struct cpufreq_policy *policy, u32 val) 232 { 233 struct acpi_cpufreq_data *data = policy->driver_data; 234 235 switch (data->cpu_feature) { 236 case SYSTEM_INTEL_MSR_CAPABLE: 237 case SYSTEM_AMD_MSR_CAPABLE: 238 return extract_msr(policy, val); 239 case SYSTEM_IO_CAPABLE: 240 return extract_io(policy, val); 241 default: 242 return 0; 243 } 244 } 245 246 static u32 cpu_freq_read_intel(struct acpi_pct_register *not_used) 247 { 248 u64 val; 249 250 rdmsrq(MSR_IA32_PERF_CTL, val); 251 return (u32)val; 252 } 253 254 static void cpu_freq_write_intel(struct acpi_pct_register *not_used, u32 val) 255 { 256 u64 msrval; 257 258 rdmsrq(MSR_IA32_PERF_CTL, msrval); 259 msrval = (msrval & ~(u64)INTEL_MSR_RANGE) | (val & INTEL_MSR_RANGE); 260 wrmsrq(MSR_IA32_PERF_CTL, msrval); 261 } 262 263 static u32 cpu_freq_read_amd(struct acpi_pct_register *not_used) 264 { 265 u64 val; 266 267 rdmsrq(MSR_AMD_PERF_CTL, val); 268 return (u32)val; 269 } 270 271 static void cpu_freq_write_amd(struct acpi_pct_register *not_used, u32 val) 272 { 273 wrmsrq(MSR_AMD_PERF_CTL, val); 274 } 275 276 static u32 cpu_freq_read_io(struct acpi_pct_register *reg) 277 { 278 u32 val; 279 280 acpi_os_read_port(reg->address, &val, reg->bit_width); 281 return val; 282 } 283 284 static void cpu_freq_write_io(struct acpi_pct_register *reg, u32 val) 285 { 286 acpi_os_write_port(reg->address, val, reg->bit_width); 287 } 288 289 struct drv_cmd { 290 struct acpi_pct_register *reg; 291 u32 val; 292 union { 293 void (*write)(struct acpi_pct_register *reg, u32 val); 294 u32 (*read)(struct acpi_pct_register *reg); 295 } func; 296 }; 297 298 /* Called via smp_call_function_single(), on the target CPU */ 299 static void do_drv_read(void *_cmd) 300 { 301 struct drv_cmd *cmd = _cmd; 302 303 cmd->val = cmd->func.read(cmd->reg); 304 } 305 306 static u32 drv_read(struct acpi_cpufreq_data *data, const struct cpumask *mask) 307 { 308 struct acpi_processor_performance *perf = to_perf_data(data); 309 struct drv_cmd cmd = { 310 .reg = &perf->control_register, 311 .func.read = data->cpu_freq_read, 312 }; 313 int err; 314 315 err = smp_call_function_any(mask, do_drv_read, &cmd, 1); 316 WARN_ON_ONCE(err); /* smp_call_function_any() was buggy? */ 317 return cmd.val; 318 } 319 320 static void do_drv_write(void *_cmd) 321 { 322 struct drv_cmd *cmd = _cmd; 323 324 cmd->func.write(cmd->reg, cmd->val); 325 } 326 327 static void drv_write(struct acpi_cpufreq_data *data, 328 const struct cpumask *mask, u32 val) 329 { 330 struct acpi_processor_performance *perf = to_perf_data(data); 331 struct drv_cmd cmd = { 332 .reg = &perf->control_register, 333 .val = val, 334 .func.write = data->cpu_freq_write, 335 }; 336 337 on_each_cpu_mask(mask, do_drv_write, &cmd, true); 338 } 339 340 static u32 get_cur_val(const struct cpumask *mask, struct acpi_cpufreq_data *data) 341 { 342 u32 val; 343 344 if (unlikely(cpumask_empty(mask))) 345 return 0; 346 347 val = drv_read(data, mask); 348 349 pr_debug("%s = %u\n", __func__, val); 350 351 return val; 352 } 353 354 static unsigned int get_cur_freq_on_cpu(unsigned int cpu) 355 { 356 struct acpi_cpufreq_data *data; 357 struct cpufreq_policy *policy; 358 unsigned int freq; 359 unsigned int cached_freq; 360 361 pr_debug("%s (%d)\n", __func__, cpu); 362 363 policy = cpufreq_cpu_get_raw(cpu); 364 if (unlikely(!policy)) 365 return 0; 366 367 data = policy->driver_data; 368 if (unlikely(!data || !policy->freq_table)) 369 return 0; 370 371 cached_freq = policy->freq_table[to_perf_data(data)->state].frequency; 372 freq = extract_freq(policy, get_cur_val(cpumask_of(cpu), data)); 373 if (freq != cached_freq) { 374 /* 375 * The dreaded BIOS frequency change behind our back. 376 * Force set the frequency on next target call. 377 */ 378 data->resume = 1; 379 } 380 381 pr_debug("cur freq = %u\n", freq); 382 383 return freq; 384 } 385 386 static unsigned int check_freqs(struct cpufreq_policy *policy, 387 const struct cpumask *mask, unsigned int freq) 388 { 389 struct acpi_cpufreq_data *data = policy->driver_data; 390 unsigned int cur_freq; 391 unsigned int i; 392 393 for (i = 0; i < 100; i++) { 394 cur_freq = extract_freq(policy, get_cur_val(mask, data)); 395 if (cur_freq == freq) 396 return 1; 397 usleep_range(10, 15); 398 } 399 return 0; 400 } 401 402 static int acpi_cpufreq_target(struct cpufreq_policy *policy, 403 unsigned int index) 404 { 405 struct acpi_cpufreq_data *data = policy->driver_data; 406 struct acpi_processor_performance *perf; 407 const struct cpumask *mask; 408 unsigned int next_perf_state = 0; /* Index into perf table */ 409 int result = 0; 410 411 if (unlikely(!data)) { 412 return -ENODEV; 413 } 414 415 perf = to_perf_data(data); 416 next_perf_state = policy->freq_table[index].driver_data; 417 if (perf->state == next_perf_state) { 418 if (unlikely(data->resume)) { 419 pr_debug("Called after resume, resetting to P%d\n", 420 next_perf_state); 421 data->resume = 0; 422 } else { 423 pr_debug("Already at target state (P%d)\n", 424 next_perf_state); 425 return 0; 426 } 427 } 428 429 /* 430 * The core won't allow CPUs to go away until the governor has been 431 * stopped, so we can rely on the stability of policy->cpus. 432 */ 433 mask = policy->shared_type == CPUFREQ_SHARED_TYPE_ANY ? 434 cpumask_of(policy->cpu) : policy->cpus; 435 436 drv_write(data, mask, perf->states[next_perf_state].control); 437 438 if (acpi_pstate_strict) { 439 if (!check_freqs(policy, mask, 440 policy->freq_table[index].frequency)) { 441 pr_debug("%s (%d)\n", __func__, policy->cpu); 442 result = -EAGAIN; 443 } 444 } 445 446 if (!result) 447 perf->state = next_perf_state; 448 449 return result; 450 } 451 452 static unsigned int acpi_cpufreq_fast_switch(struct cpufreq_policy *policy, 453 unsigned int target_freq) 454 { 455 struct acpi_cpufreq_data *data = policy->driver_data; 456 struct acpi_processor_performance *perf; 457 struct cpufreq_frequency_table *entry; 458 unsigned int next_perf_state, next_freq, index; 459 460 /* 461 * Find the closest frequency above target_freq. 462 */ 463 if (policy->cached_target_freq == target_freq) 464 index = policy->cached_resolved_idx; 465 else 466 index = cpufreq_table_find_index_dl(policy, target_freq, 467 false); 468 469 entry = &policy->freq_table[index]; 470 next_freq = entry->frequency; 471 next_perf_state = entry->driver_data; 472 473 perf = to_perf_data(data); 474 if (perf->state == next_perf_state) { 475 if (unlikely(data->resume)) 476 data->resume = 0; 477 else 478 return next_freq; 479 } 480 481 data->cpu_freq_write(&perf->control_register, 482 perf->states[next_perf_state].control); 483 perf->state = next_perf_state; 484 return next_freq; 485 } 486 487 static unsigned long 488 acpi_cpufreq_guess_freq(struct acpi_cpufreq_data *data, unsigned int cpu) 489 { 490 struct acpi_processor_performance *perf; 491 492 perf = to_perf_data(data); 493 if (cpu_khz) { 494 /* search the closest match to cpu_khz */ 495 unsigned int i; 496 unsigned long freq; 497 unsigned long freqn = perf->states[0].core_frequency * 1000; 498 499 for (i = 0; i < (perf->state_count-1); i++) { 500 freq = freqn; 501 freqn = perf->states[i+1].core_frequency * 1000; 502 if ((2 * cpu_khz) > (freqn + freq)) { 503 perf->state = i; 504 return freq; 505 } 506 } 507 perf->state = perf->state_count-1; 508 return freqn; 509 } else { 510 /* assume CPU is at P0... */ 511 perf->state = 0; 512 return perf->states[0].core_frequency * 1000; 513 } 514 } 515 516 static void free_acpi_perf_data(void) 517 { 518 unsigned int i; 519 520 /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */ 521 for_each_possible_cpu(i) 522 free_cpumask_var(per_cpu_ptr(acpi_perf_data, i) 523 ->shared_cpu_map); 524 free_percpu(acpi_perf_data); 525 } 526 527 static int cpufreq_boost_down_prep(unsigned int cpu) 528 { 529 /* 530 * Clear the boost-disable bit on the CPU_DOWN path so that 531 * this cpu cannot block the remaining ones from boosting. 532 */ 533 return boost_set_msr(1); 534 } 535 536 /* 537 * acpi_cpufreq_early_init - initialize ACPI P-States library 538 * 539 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c) 540 * in order to determine correct frequency and voltage pairings. We can 541 * do _PDC and _PSD and find out the processor dependency for the 542 * actual init that will happen later... 543 */ 544 static int __init acpi_cpufreq_early_init(void) 545 { 546 unsigned int i; 547 pr_debug("%s\n", __func__); 548 549 acpi_perf_data = alloc_percpu(struct acpi_processor_performance); 550 if (!acpi_perf_data) { 551 pr_debug("Memory allocation error for acpi_perf_data.\n"); 552 return -ENOMEM; 553 } 554 for_each_possible_cpu(i) { 555 if (!zalloc_cpumask_var_node( 556 &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map, 557 GFP_KERNEL, cpu_to_node(i))) { 558 559 /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */ 560 free_acpi_perf_data(); 561 return -ENOMEM; 562 } 563 } 564 565 /* Do initialization in ACPI core */ 566 acpi_processor_preregister_performance(acpi_perf_data); 567 return 0; 568 } 569 570 #ifdef CONFIG_SMP 571 /* 572 * Some BIOSes do SW_ANY coordination internally, either set it up in hw 573 * or do it in BIOS firmware and won't inform about it to OS. If not 574 * detected, this has a side effect of making CPU run at a different speed 575 * than OS intended it to run at. Detect it and handle it cleanly. 576 */ 577 static int bios_with_sw_any_bug; 578 579 static int sw_any_bug_found(const struct dmi_system_id *d) 580 { 581 bios_with_sw_any_bug = 1; 582 return 0; 583 } 584 585 static const struct dmi_system_id sw_any_bug_dmi_table[] = { 586 { 587 .callback = sw_any_bug_found, 588 .ident = "Supermicro Server X6DLP", 589 .matches = { 590 DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"), 591 DMI_MATCH(DMI_BIOS_VERSION, "080010"), 592 DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"), 593 }, 594 }, 595 { } 596 }; 597 598 static int acpi_cpufreq_blacklist(struct cpuinfo_x86 *c) 599 { 600 /* Intel Xeon Processor 7100 Series Specification Update 601 * https://www.intel.com/Assets/PDF/specupdate/314554.pdf 602 * AL30: A Machine Check Exception (MCE) Occurring during an 603 * Enhanced Intel SpeedStep Technology Ratio Change May Cause 604 * Both Processor Cores to Lock Up. */ 605 if (c->x86_vendor == X86_VENDOR_INTEL) { 606 if ((c->x86 == 15) && 607 (c->x86_model == 6) && 608 (c->x86_stepping == 8)) { 609 pr_info("Intel(R) Xeon(R) 7100 Errata AL30, processors may lock up on frequency changes: disabling acpi-cpufreq\n"); 610 return -ENODEV; 611 } 612 } 613 return 0; 614 } 615 #endif 616 617 #ifdef CONFIG_ACPI_CPPC_LIB 618 /* 619 * get_max_boost_ratio: Computes the max_boost_ratio as the ratio 620 * between the highest_perf and the nominal_perf. 621 * 622 * Returns the max_boost_ratio for @cpu. Returns the CPPC nominal 623 * frequency via @nominal_freq if it is non-NULL pointer. 624 */ 625 static u64 get_max_boost_ratio(unsigned int cpu, u64 *nominal_freq) 626 { 627 struct cppc_perf_caps perf_caps; 628 u64 numerator, denominator; 629 int ret; 630 631 if (acpi_pstate_strict) 632 return 0; 633 634 ret = cppc_get_perf_caps(cpu, &perf_caps); 635 if (ret) { 636 pr_debug("CPU%d: Unable to get performance capabilities (%d)\n", 637 cpu, ret); 638 return 0; 639 } 640 641 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) { 642 ret = amd_get_boost_ratio(cpu, &numerator, &denominator); 643 if (ret) { 644 pr_debug("CPU%d: Unable to get boost ratio (%d)\n", 645 cpu, ret); 646 return 0; 647 } 648 } else { 649 numerator = perf_caps.highest_perf; 650 denominator = perf_caps.nominal_perf; 651 } 652 653 if (nominal_freq) 654 *nominal_freq = perf_caps.nominal_freq * 1000; 655 656 if (!numerator || !denominator) { 657 pr_debug("CPU%d: highest or nominal performance missing\n", cpu); 658 return 0; 659 } 660 661 if (numerator < denominator) { 662 pr_debug("CPU%d: nominal performance above highest\n", cpu); 663 return 0; 664 } 665 666 return div_u64(numerator << SCHED_CAPACITY_SHIFT, denominator); 667 } 668 669 #else 670 static inline u64 get_max_boost_ratio(unsigned int cpu, u64 *nominal_freq) 671 { 672 return 0; 673 } 674 #endif 675 676 static void acpi_cpufreq_resolve_max_freq(struct cpufreq_policy *policy, 677 unsigned int pss_max_freq) 678 { 679 #ifdef CONFIG_ACPI_CPPC_LIB 680 u64 max_speed = cppc_get_dmi_max_khz(); 681 /* 682 * Use DMI "Max Speed" if it looks plausible: must be 683 * above _PSS P0 frequency and within 2x of it. 684 */ 685 if (max_speed > pss_max_freq && max_speed < pss_max_freq * 2) { 686 policy->cpuinfo.max_freq = max_speed; 687 return; 688 } 689 #endif 690 /* 691 * If the maximum "boost" frequency is unknown, ask the arch 692 * scale-invariance code to use the "nominal" performance for 693 * CPU utilization scaling so as to prevent the schedutil 694 * governor from selecting inadequate CPU frequencies. 695 */ 696 arch_set_max_freq_ratio(true); 697 } 698 699 static int acpi_cpufreq_cpu_init(struct cpufreq_policy *policy) 700 { 701 struct cpufreq_frequency_table *freq_table; 702 struct acpi_processor_performance *perf; 703 struct acpi_cpufreq_data *data; 704 unsigned int cpu = policy->cpu; 705 struct cpuinfo_x86 *c = &cpu_data(cpu); 706 u64 max_boost_ratio, nominal_freq = 0; 707 unsigned int valid_states = 0; 708 unsigned int result = 0; 709 unsigned int i; 710 #ifdef CONFIG_SMP 711 static int blacklisted; 712 #endif 713 714 pr_debug("%s\n", __func__); 715 716 #ifdef CONFIG_SMP 717 if (blacklisted) 718 return blacklisted; 719 blacklisted = acpi_cpufreq_blacklist(c); 720 if (blacklisted) 721 return blacklisted; 722 #endif 723 724 data = kzalloc_obj(*data); 725 if (!data) 726 return -ENOMEM; 727 728 if (!zalloc_cpumask_var(&data->freqdomain_cpus, GFP_KERNEL)) { 729 result = -ENOMEM; 730 goto err_free; 731 } 732 733 perf = per_cpu_ptr(acpi_perf_data, cpu); 734 data->acpi_perf_cpu = cpu; 735 policy->driver_data = data; 736 737 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) 738 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS; 739 740 result = acpi_processor_register_performance(perf, cpu); 741 if (result) 742 goto err_free_mask; 743 744 policy->shared_type = perf->shared_type; 745 746 /* 747 * Will let policy->cpus know about dependency only when software 748 * coordination is required. 749 */ 750 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL || 751 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) { 752 cpumask_copy(policy->cpus, perf->shared_cpu_map); 753 } 754 cpumask_copy(data->freqdomain_cpus, perf->shared_cpu_map); 755 756 #ifdef CONFIG_SMP 757 dmi_check_system(sw_any_bug_dmi_table); 758 if (bios_with_sw_any_bug && !policy_is_shared(policy)) { 759 policy->shared_type = CPUFREQ_SHARED_TYPE_ALL; 760 cpumask_copy(policy->cpus, topology_core_cpumask(cpu)); 761 } 762 763 if (check_amd_hwpstate_cpu(cpu) && boot_cpu_data.x86 < 0x19 && 764 !acpi_pstate_strict) { 765 cpumask_clear(policy->cpus); 766 cpumask_set_cpu(cpu, policy->cpus); 767 cpumask_copy(data->freqdomain_cpus, 768 topology_sibling_cpumask(cpu)); 769 policy->shared_type = CPUFREQ_SHARED_TYPE_HW; 770 pr_info_once("overriding BIOS provided _PSD data\n"); 771 } 772 #endif 773 774 /* capability check */ 775 if (perf->state_count <= 1) { 776 pr_debug("No P-States\n"); 777 result = -ENODEV; 778 goto err_unreg; 779 } 780 781 if (perf->control_register.space_id != perf->status_register.space_id) { 782 result = -ENODEV; 783 goto err_unreg; 784 } 785 786 switch (perf->control_register.space_id) { 787 case ACPI_ADR_SPACE_SYSTEM_IO: 788 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD && 789 boot_cpu_data.x86 == 0xf) { 790 pr_debug("AMD K8 systems must use native drivers.\n"); 791 result = -ENODEV; 792 goto err_unreg; 793 } 794 pr_debug("SYSTEM IO addr space\n"); 795 data->cpu_feature = SYSTEM_IO_CAPABLE; 796 data->cpu_freq_read = cpu_freq_read_io; 797 data->cpu_freq_write = cpu_freq_write_io; 798 break; 799 case ACPI_ADR_SPACE_FIXED_HARDWARE: 800 pr_debug("HARDWARE addr space\n"); 801 if (check_est_cpu(cpu)) { 802 data->cpu_feature = SYSTEM_INTEL_MSR_CAPABLE; 803 data->cpu_freq_read = cpu_freq_read_intel; 804 data->cpu_freq_write = cpu_freq_write_intel; 805 break; 806 } 807 if (check_amd_hwpstate_cpu(cpu)) { 808 data->cpu_feature = SYSTEM_AMD_MSR_CAPABLE; 809 data->cpu_freq_read = cpu_freq_read_amd; 810 data->cpu_freq_write = cpu_freq_write_amd; 811 break; 812 } 813 result = -ENODEV; 814 goto err_unreg; 815 default: 816 pr_debug("Unknown addr space %d\n", 817 (u32) (perf->control_register.space_id)); 818 result = -ENODEV; 819 goto err_unreg; 820 } 821 822 freq_table = kzalloc_objs(*freq_table, perf->state_count + 1); 823 if (!freq_table) { 824 result = -ENOMEM; 825 goto err_unreg; 826 } 827 828 /* detect transition latency */ 829 policy->cpuinfo.transition_latency = 0; 830 for (i = 0; i < perf->state_count; i++) { 831 if ((perf->states[i].transition_latency * 1000) > 832 policy->cpuinfo.transition_latency) 833 policy->cpuinfo.transition_latency = 834 perf->states[i].transition_latency * 1000; 835 } 836 837 /* Check for high latency (>20uS) from buggy BIOSes, like on T42 */ 838 if (perf->control_register.space_id == ACPI_ADR_SPACE_FIXED_HARDWARE && 839 policy->cpuinfo.transition_latency > 20 * 1000) { 840 policy->cpuinfo.transition_latency = 20 * 1000; 841 pr_info_once("P-state transition latency capped at 20 uS\n"); 842 } 843 844 /* table init */ 845 for (i = 0; i < perf->state_count; i++) { 846 if (i > 0 && perf->states[i].core_frequency >= 847 freq_table[valid_states-1].frequency / 1000) 848 continue; 849 850 freq_table[valid_states].driver_data = i; 851 freq_table[valid_states].frequency = 852 perf->states[i].core_frequency * 1000; 853 valid_states++; 854 } 855 freq_table[valid_states].frequency = CPUFREQ_TABLE_END; 856 857 max_boost_ratio = get_max_boost_ratio(cpu, &nominal_freq); 858 if (max_boost_ratio) { 859 unsigned int freq = nominal_freq; 860 861 /* 862 * The loop above sorts the freq_table entries in the 863 * descending order. If ACPI CPPC has not advertised 864 * the nominal frequency (this is possible in CPPC 865 * revisions prior to 3), then use the first entry in 866 * the pstate table as a proxy for nominal frequency. 867 */ 868 if (!freq) 869 freq = freq_table[0].frequency; 870 871 policy->cpuinfo.max_freq = freq * max_boost_ratio >> SCHED_CAPACITY_SHIFT; 872 } else { 873 acpi_cpufreq_resolve_max_freq(policy, freq_table[0].frequency); 874 } 875 876 policy->freq_table = freq_table; 877 perf->state = 0; 878 879 switch (perf->control_register.space_id) { 880 case ACPI_ADR_SPACE_SYSTEM_IO: 881 /* 882 * The core will not set policy->cur, because 883 * cpufreq_driver->get is NULL, so we need to set it here. 884 * However, we have to guess it, because the current speed is 885 * unknown and not detectable via IO ports. 886 */ 887 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu); 888 break; 889 case ACPI_ADR_SPACE_FIXED_HARDWARE: 890 acpi_cpufreq_driver.get = get_cur_freq_on_cpu; 891 break; 892 default: 893 break; 894 } 895 896 /* notify BIOS that we exist */ 897 acpi_processor_notify_smm(THIS_MODULE); 898 899 pr_debug("CPU%u - ACPI performance management activated.\n", cpu); 900 for (i = 0; i < perf->state_count; i++) 901 pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n", 902 (i == perf->state ? '*' : ' '), i, 903 (u32) perf->states[i].core_frequency, 904 (u32) perf->states[i].power, 905 (u32) perf->states[i].transition_latency); 906 907 /* 908 * the first call to ->target() should result in us actually 909 * writing something to the appropriate registers. 910 */ 911 data->resume = 1; 912 913 policy->fast_switch_possible = !acpi_pstate_strict && 914 !(policy_is_shared(policy) && policy->shared_type != CPUFREQ_SHARED_TYPE_ANY); 915 916 if (perf->states[0].core_frequency * 1000 != freq_table[0].frequency) 917 pr_warn(FW_WARN "P-state 0 is not max freq\n"); 918 919 if (acpi_cpufreq_driver.set_boost) { 920 if (policy->boost_supported) { 921 /* 922 * The firmware may have altered boost state while the 923 * CPU was offline (for example during a suspend-resume 924 * cycle). 925 */ 926 if (policy->boost_enabled != boost_state(cpu)) 927 set_boost(policy, policy->boost_enabled); 928 } else { 929 policy->boost_supported = true; 930 } 931 } 932 933 return result; 934 935 err_unreg: 936 acpi_processor_unregister_performance(cpu); 937 err_free_mask: 938 free_cpumask_var(data->freqdomain_cpus); 939 err_free: 940 kfree(data); 941 policy->driver_data = NULL; 942 943 return result; 944 } 945 946 static void acpi_cpufreq_cpu_exit(struct cpufreq_policy *policy) 947 { 948 struct acpi_cpufreq_data *data = policy->driver_data; 949 950 pr_debug("%s\n", __func__); 951 952 cpufreq_boost_down_prep(policy->cpu); 953 policy->fast_switch_possible = false; 954 policy->driver_data = NULL; 955 acpi_processor_unregister_performance(data->acpi_perf_cpu); 956 free_cpumask_var(data->freqdomain_cpus); 957 kfree(policy->freq_table); 958 kfree(data); 959 } 960 961 static int acpi_cpufreq_resume(struct cpufreq_policy *policy) 962 { 963 struct acpi_cpufreq_data *data = policy->driver_data; 964 965 pr_debug("%s\n", __func__); 966 967 data->resume = 1; 968 969 return 0; 970 } 971 972 static struct freq_attr *acpi_cpufreq_attr[] = { 973 &freqdomain_cpus, 974 #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB 975 &cpb, 976 #endif 977 NULL, 978 }; 979 980 static struct cpufreq_driver acpi_cpufreq_driver = { 981 .verify = cpufreq_generic_frequency_table_verify, 982 .target_index = acpi_cpufreq_target, 983 .fast_switch = acpi_cpufreq_fast_switch, 984 .bios_limit = acpi_processor_get_bios_limit, 985 .init = acpi_cpufreq_cpu_init, 986 .exit = acpi_cpufreq_cpu_exit, 987 .resume = acpi_cpufreq_resume, 988 .name = "acpi-cpufreq", 989 .attr = acpi_cpufreq_attr, 990 }; 991 992 static void __init acpi_cpufreq_boost_init(void) 993 { 994 if (!(boot_cpu_has(X86_FEATURE_CPB) || boot_cpu_has(X86_FEATURE_IDA))) { 995 pr_debug("Boost capabilities not present in the processor\n"); 996 return; 997 } 998 999 acpi_cpufreq_driver.set_boost = set_boost; 1000 acpi_cpufreq_driver.boost_enabled = boost_state(0); 1001 } 1002 1003 static int __init acpi_cpufreq_probe(struct platform_device *pdev) 1004 { 1005 int ret; 1006 1007 if (acpi_disabled) 1008 return -ENODEV; 1009 1010 /* don't keep reloading if cpufreq_driver exists */ 1011 if (cpufreq_get_current_driver()) 1012 return -ENODEV; 1013 1014 pr_debug("%s\n", __func__); 1015 1016 ret = acpi_cpufreq_early_init(); 1017 if (ret) 1018 return ret; 1019 1020 #ifdef CONFIG_X86_ACPI_CPUFREQ_CPB 1021 /* this is a sysfs file with a strange name and an even stranger 1022 * semantic - per CPU instantiation, but system global effect. 1023 * Lets enable it only on AMD CPUs for compatibility reasons and 1024 * only if configured. This is considered legacy code, which 1025 * will probably be removed at some point in the future. 1026 */ 1027 if (!check_amd_hwpstate_cpu(0)) { 1028 struct freq_attr **attr; 1029 1030 pr_debug("CPB unsupported, do not expose it\n"); 1031 1032 for (attr = acpi_cpufreq_attr; *attr; attr++) 1033 if (*attr == &cpb) { 1034 *attr = NULL; 1035 break; 1036 } 1037 } 1038 #endif 1039 acpi_cpufreq_boost_init(); 1040 1041 ret = cpufreq_register_driver(&acpi_cpufreq_driver); 1042 if (ret) { 1043 free_acpi_perf_data(); 1044 } 1045 return ret; 1046 } 1047 1048 static void acpi_cpufreq_remove(struct platform_device *pdev) 1049 { 1050 pr_debug("%s\n", __func__); 1051 1052 cpufreq_unregister_driver(&acpi_cpufreq_driver); 1053 1054 free_acpi_perf_data(); 1055 } 1056 1057 static struct platform_driver acpi_cpufreq_platdrv = { 1058 .driver = { 1059 .name = "acpi-cpufreq", 1060 }, 1061 .remove = acpi_cpufreq_remove, 1062 }; 1063 1064 static int __init acpi_cpufreq_init(void) 1065 { 1066 return platform_driver_probe(&acpi_cpufreq_platdrv, acpi_cpufreq_probe); 1067 } 1068 1069 static void __exit acpi_cpufreq_exit(void) 1070 { 1071 platform_driver_unregister(&acpi_cpufreq_platdrv); 1072 } 1073 1074 module_param(acpi_pstate_strict, uint, 0644); 1075 MODULE_PARM_DESC(acpi_pstate_strict, 1076 "value 0 or non-zero. non-zero -> strict ACPI checks are " 1077 "performed during frequency changes."); 1078 1079 late_initcall(acpi_cpufreq_init); 1080 module_exit(acpi_cpufreq_exit); 1081 1082 MODULE_ALIAS("platform:acpi-cpufreq"); 1083