1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * intel_pstate.c: Native P state management for Intel processors 4 * 5 * (C) Copyright 2012 Intel Corporation 6 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com> 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/kernel.h> 12 #include <linux/kernel_stat.h> 13 #include <linux/module.h> 14 #include <linux/ktime.h> 15 #include <linux/hrtimer.h> 16 #include <linux/tick.h> 17 #include <linux/slab.h> 18 #include <linux/sched/cpufreq.h> 19 #include <linux/list.h> 20 #include <linux/cpu.h> 21 #include <linux/cpufreq.h> 22 #include <linux/sysfs.h> 23 #include <linux/types.h> 24 #include <linux/fs.h> 25 #include <linux/acpi.h> 26 #include <linux/vmalloc.h> 27 #include <linux/pm_qos.h> 28 #include <linux/bitfield.h> 29 #include <trace/events/power.h> 30 31 #include <asm/cpu.h> 32 #include <asm/div64.h> 33 #include <asm/msr.h> 34 #include <asm/cpu_device_id.h> 35 #include <asm/cpufeature.h> 36 #include <asm/intel-family.h> 37 #include "../drivers/thermal/intel/thermal_interrupt.h" 38 39 #define INTEL_PSTATE_SAMPLING_INTERVAL (10 * NSEC_PER_MSEC) 40 41 #define INTEL_CPUFREQ_TRANSITION_LATENCY 20000 42 #define INTEL_CPUFREQ_TRANSITION_DELAY_HWP 5000 43 #define INTEL_CPUFREQ_TRANSITION_DELAY 500 44 45 #ifdef CONFIG_ACPI 46 #include <acpi/processor.h> 47 #include <acpi/cppc_acpi.h> 48 #endif 49 50 #define FRAC_BITS 8 51 #define int_tofp(X) ((int64_t)(X) << FRAC_BITS) 52 #define fp_toint(X) ((X) >> FRAC_BITS) 53 54 #define ONE_EIGHTH_FP ((int64_t)1 << (FRAC_BITS - 3)) 55 56 #define EXT_BITS 6 57 #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS) 58 #define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS) 59 #define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS) 60 61 static inline int32_t mul_fp(int32_t x, int32_t y) 62 { 63 return ((int64_t)x * (int64_t)y) >> FRAC_BITS; 64 } 65 66 static inline int32_t div_fp(s64 x, s64 y) 67 { 68 return div64_s64((int64_t)x << FRAC_BITS, y); 69 } 70 71 static inline int ceiling_fp(int32_t x) 72 { 73 int mask, ret; 74 75 ret = fp_toint(x); 76 mask = (1 << FRAC_BITS) - 1; 77 if (x & mask) 78 ret += 1; 79 return ret; 80 } 81 82 static inline u64 mul_ext_fp(u64 x, u64 y) 83 { 84 return (x * y) >> EXT_FRAC_BITS; 85 } 86 87 static inline u64 div_ext_fp(u64 x, u64 y) 88 { 89 return div64_u64(x << EXT_FRAC_BITS, y); 90 } 91 92 /** 93 * struct sample - Store performance sample 94 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average 95 * performance during last sample period 96 * @busy_scaled: Scaled busy value which is used to calculate next 97 * P state. This can be different than core_avg_perf 98 * to account for cpu idle period 99 * @aperf: Difference of actual performance frequency clock count 100 * read from APERF MSR between last and current sample 101 * @mperf: Difference of maximum performance frequency clock count 102 * read from MPERF MSR between last and current sample 103 * @tsc: Difference of time stamp counter between last and 104 * current sample 105 * @time: Current time from scheduler 106 * 107 * This structure is used in the cpudata structure to store performance sample 108 * data for choosing next P State. 109 */ 110 struct sample { 111 int32_t core_avg_perf; 112 int32_t busy_scaled; 113 u64 aperf; 114 u64 mperf; 115 u64 tsc; 116 u64 time; 117 }; 118 119 /** 120 * struct pstate_data - Store P state data 121 * @current_pstate: Current requested P state 122 * @min_pstate: Min P state possible for this platform 123 * @max_pstate: Max P state possible for this platform 124 * @max_pstate_physical:This is physical Max P state for a processor 125 * This can be higher than the max_pstate which can 126 * be limited by platform thermal design power limits 127 * @perf_ctl_scaling: PERF_CTL P-state to frequency scaling factor 128 * @scaling: Scaling factor between performance and frequency 129 * @turbo_pstate: Max Turbo P state possible for this platform 130 * @min_freq: @min_pstate frequency in cpufreq units 131 * @max_freq: @max_pstate frequency in cpufreq units 132 * @turbo_freq: @turbo_pstate frequency in cpufreq units 133 * 134 * Stores the per cpu model P state limits and current P state. 135 */ 136 struct pstate_data { 137 int current_pstate; 138 int min_pstate; 139 int max_pstate; 140 int max_pstate_physical; 141 int perf_ctl_scaling; 142 int scaling; 143 int turbo_pstate; 144 unsigned int min_freq; 145 unsigned int max_freq; 146 unsigned int turbo_freq; 147 }; 148 149 /** 150 * struct vid_data - Stores voltage information data 151 * @min: VID data for this platform corresponding to 152 * the lowest P state 153 * @max: VID data corresponding to the highest P State. 154 * @turbo: VID data for turbo P state 155 * @ratio: Ratio of (vid max - vid min) / 156 * (max P state - Min P State) 157 * 158 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling) 159 * This data is used in Atom platforms, where in addition to target P state, 160 * the voltage data needs to be specified to select next P State. 161 */ 162 struct vid_data { 163 int min; 164 int max; 165 int turbo; 166 int32_t ratio; 167 }; 168 169 /** 170 * struct global_params - Global parameters, mostly tunable via sysfs. 171 * @no_turbo: Whether or not to use turbo P-states. 172 * @turbo_disabled: Whether or not turbo P-states are available at all, 173 * based on the MSR_IA32_MISC_ENABLE value and whether or 174 * not the maximum reported turbo P-state is different from 175 * the maximum reported non-turbo one. 176 * @min_perf_pct: Minimum capacity limit in percent of the maximum turbo 177 * P-state capacity. 178 * @max_perf_pct: Maximum capacity limit in percent of the maximum turbo 179 * P-state capacity. 180 */ 181 struct global_params { 182 bool no_turbo; 183 bool turbo_disabled; 184 int max_perf_pct; 185 int min_perf_pct; 186 }; 187 188 /** 189 * struct cpudata - Per CPU instance data storage 190 * @cpu: CPU number for this instance data 191 * @policy: CPUFreq policy value 192 * @update_util: CPUFreq utility callback information 193 * @update_util_set: CPUFreq utility callback is set 194 * @iowait_boost: iowait-related boost fraction 195 * @last_update: Time of the last update. 196 * @pstate: Stores P state limits for this CPU 197 * @vid: Stores VID limits for this CPU 198 * @last_sample_time: Last Sample time 199 * @aperf_mperf_shift: APERF vs MPERF counting frequency difference 200 * @prev_aperf: Last APERF value read from APERF MSR 201 * @prev_mperf: Last MPERF value read from MPERF MSR 202 * @prev_tsc: Last timestamp counter (TSC) value 203 * @sample: Storage for storing last Sample data 204 * @min_perf_ratio: Minimum capacity in terms of PERF or HWP ratios 205 * @max_perf_ratio: Maximum capacity in terms of PERF or HWP ratios 206 * @acpi_perf_data: Stores ACPI perf information read from _PSS 207 * @valid_pss_table: Set to true for valid ACPI _PSS entries found 208 * @epp_powersave: Last saved HWP energy performance preference 209 * (EPP) or energy performance bias (EPB), 210 * when policy switched to performance 211 * @epp_policy: Last saved policy used to set EPP/EPB 212 * @epp_default: Power on default HWP energy performance 213 * preference/bias 214 * @epp_cached: Cached HWP energy-performance preference value 215 * @hwp_req_cached: Cached value of the last HWP Request MSR 216 * @hwp_cap_cached: Cached value of the last HWP Capabilities MSR 217 * @last_io_update: Last time when IO wake flag was set 218 * @sched_flags: Store scheduler flags for possible cross CPU update 219 * @hwp_boost_min: Last HWP boosted min performance 220 * @suspended: Whether or not the driver has been suspended. 221 * @hwp_notify_work: workqueue for HWP notifications. 222 * 223 * This structure stores per CPU instance data for all CPUs. 224 */ 225 struct cpudata { 226 int cpu; 227 228 unsigned int policy; 229 struct update_util_data update_util; 230 bool update_util_set; 231 232 struct pstate_data pstate; 233 struct vid_data vid; 234 235 u64 last_update; 236 u64 last_sample_time; 237 u64 aperf_mperf_shift; 238 u64 prev_aperf; 239 u64 prev_mperf; 240 u64 prev_tsc; 241 struct sample sample; 242 int32_t min_perf_ratio; 243 int32_t max_perf_ratio; 244 #ifdef CONFIG_ACPI 245 struct acpi_processor_performance acpi_perf_data; 246 bool valid_pss_table; 247 #endif 248 unsigned int iowait_boost; 249 s16 epp_powersave; 250 s16 epp_policy; 251 s16 epp_default; 252 s16 epp_cached; 253 u64 hwp_req_cached; 254 u64 hwp_cap_cached; 255 u64 last_io_update; 256 unsigned int sched_flags; 257 u32 hwp_boost_min; 258 bool suspended; 259 struct delayed_work hwp_notify_work; 260 }; 261 262 static struct cpudata **all_cpu_data; 263 264 /** 265 * struct pstate_funcs - Per CPU model specific callbacks 266 * @get_max: Callback to get maximum non turbo effective P state 267 * @get_max_physical: Callback to get maximum non turbo physical P state 268 * @get_min: Callback to get minimum P state 269 * @get_turbo: Callback to get turbo P state 270 * @get_scaling: Callback to get frequency scaling factor 271 * @get_cpu_scaling: Get frequency scaling factor for a given cpu 272 * @get_aperf_mperf_shift: Callback to get the APERF vs MPERF frequency difference 273 * @get_val: Callback to convert P state to actual MSR write value 274 * @get_vid: Callback to get VID data for Atom platforms 275 * 276 * Core and Atom CPU models have different way to get P State limits. This 277 * structure is used to store those callbacks. 278 */ 279 struct pstate_funcs { 280 int (*get_max)(int cpu); 281 int (*get_max_physical)(int cpu); 282 int (*get_min)(int cpu); 283 int (*get_turbo)(int cpu); 284 int (*get_scaling)(void); 285 int (*get_cpu_scaling)(int cpu); 286 int (*get_aperf_mperf_shift)(void); 287 u64 (*get_val)(struct cpudata*, int pstate); 288 void (*get_vid)(struct cpudata *); 289 }; 290 291 static struct pstate_funcs pstate_funcs __read_mostly; 292 293 static bool hwp_active __ro_after_init; 294 static int hwp_mode_bdw __ro_after_init; 295 static bool per_cpu_limits __ro_after_init; 296 static bool hwp_forced __ro_after_init; 297 static bool hwp_boost __read_mostly; 298 299 static struct cpufreq_driver *intel_pstate_driver __read_mostly; 300 301 #define HYBRID_SCALING_FACTOR 78741 302 #define HYBRID_SCALING_FACTOR_MTL 80000 303 304 static int hybrid_scaling_factor = HYBRID_SCALING_FACTOR; 305 306 static inline int core_get_scaling(void) 307 { 308 return 100000; 309 } 310 311 #ifdef CONFIG_ACPI 312 static bool acpi_ppc; 313 #endif 314 315 static struct global_params global; 316 317 static DEFINE_MUTEX(intel_pstate_driver_lock); 318 static DEFINE_MUTEX(intel_pstate_limits_lock); 319 320 #ifdef CONFIG_ACPI 321 322 static bool intel_pstate_acpi_pm_profile_server(void) 323 { 324 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER || 325 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER) 326 return true; 327 328 return false; 329 } 330 331 static bool intel_pstate_get_ppc_enable_status(void) 332 { 333 if (intel_pstate_acpi_pm_profile_server()) 334 return true; 335 336 return acpi_ppc; 337 } 338 339 #ifdef CONFIG_ACPI_CPPC_LIB 340 341 /* The work item is needed to avoid CPU hotplug locking issues */ 342 static void intel_pstste_sched_itmt_work_fn(struct work_struct *work) 343 { 344 sched_set_itmt_support(); 345 } 346 347 static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn); 348 349 #define CPPC_MAX_PERF U8_MAX 350 351 static void intel_pstate_set_itmt_prio(int cpu) 352 { 353 struct cppc_perf_caps cppc_perf; 354 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX; 355 int ret; 356 357 ret = cppc_get_perf_caps(cpu, &cppc_perf); 358 if (ret) 359 return; 360 361 /* 362 * On some systems with overclocking enabled, CPPC.highest_perf is hardcoded to 0xff. 363 * In this case we can't use CPPC.highest_perf to enable ITMT. 364 * In this case we can look at MSR_HWP_CAPABILITIES bits [8:0] to decide. 365 */ 366 if (cppc_perf.highest_perf == CPPC_MAX_PERF) 367 cppc_perf.highest_perf = HWP_HIGHEST_PERF(READ_ONCE(all_cpu_data[cpu]->hwp_cap_cached)); 368 369 /* 370 * The priorities can be set regardless of whether or not 371 * sched_set_itmt_support(true) has been called and it is valid to 372 * update them at any time after it has been called. 373 */ 374 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu); 375 376 if (max_highest_perf <= min_highest_perf) { 377 if (cppc_perf.highest_perf > max_highest_perf) 378 max_highest_perf = cppc_perf.highest_perf; 379 380 if (cppc_perf.highest_perf < min_highest_perf) 381 min_highest_perf = cppc_perf.highest_perf; 382 383 if (max_highest_perf > min_highest_perf) { 384 /* 385 * This code can be run during CPU online under the 386 * CPU hotplug locks, so sched_set_itmt_support() 387 * cannot be called from here. Queue up a work item 388 * to invoke it. 389 */ 390 schedule_work(&sched_itmt_work); 391 } 392 } 393 } 394 395 static int intel_pstate_get_cppc_guaranteed(int cpu) 396 { 397 struct cppc_perf_caps cppc_perf; 398 int ret; 399 400 ret = cppc_get_perf_caps(cpu, &cppc_perf); 401 if (ret) 402 return ret; 403 404 if (cppc_perf.guaranteed_perf) 405 return cppc_perf.guaranteed_perf; 406 407 return cppc_perf.nominal_perf; 408 } 409 410 static int intel_pstate_cppc_get_scaling(int cpu) 411 { 412 struct cppc_perf_caps cppc_perf; 413 int ret; 414 415 ret = cppc_get_perf_caps(cpu, &cppc_perf); 416 417 /* 418 * If the nominal frequency and the nominal performance are not 419 * zero and the ratio between them is not 100, return the hybrid 420 * scaling factor. 421 */ 422 if (!ret && cppc_perf.nominal_perf && cppc_perf.nominal_freq && 423 cppc_perf.nominal_perf * 100 != cppc_perf.nominal_freq) 424 return hybrid_scaling_factor; 425 426 return core_get_scaling(); 427 } 428 429 #else /* CONFIG_ACPI_CPPC_LIB */ 430 static inline void intel_pstate_set_itmt_prio(int cpu) 431 { 432 } 433 #endif /* CONFIG_ACPI_CPPC_LIB */ 434 435 static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy) 436 { 437 struct cpudata *cpu; 438 int ret; 439 int i; 440 441 if (hwp_active) { 442 intel_pstate_set_itmt_prio(policy->cpu); 443 return; 444 } 445 446 if (!intel_pstate_get_ppc_enable_status()) 447 return; 448 449 cpu = all_cpu_data[policy->cpu]; 450 451 ret = acpi_processor_register_performance(&cpu->acpi_perf_data, 452 policy->cpu); 453 if (ret) 454 return; 455 456 /* 457 * Check if the control value in _PSS is for PERF_CTL MSR, which should 458 * guarantee that the states returned by it map to the states in our 459 * list directly. 460 */ 461 if (cpu->acpi_perf_data.control_register.space_id != 462 ACPI_ADR_SPACE_FIXED_HARDWARE) 463 goto err; 464 465 /* 466 * If there is only one entry _PSS, simply ignore _PSS and continue as 467 * usual without taking _PSS into account 468 */ 469 if (cpu->acpi_perf_data.state_count < 2) 470 goto err; 471 472 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu); 473 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) { 474 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n", 475 (i == cpu->acpi_perf_data.state ? '*' : ' '), i, 476 (u32) cpu->acpi_perf_data.states[i].core_frequency, 477 (u32) cpu->acpi_perf_data.states[i].power, 478 (u32) cpu->acpi_perf_data.states[i].control); 479 } 480 481 cpu->valid_pss_table = true; 482 pr_debug("_PPC limits will be enforced\n"); 483 484 return; 485 486 err: 487 cpu->valid_pss_table = false; 488 acpi_processor_unregister_performance(policy->cpu); 489 } 490 491 static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy) 492 { 493 struct cpudata *cpu; 494 495 cpu = all_cpu_data[policy->cpu]; 496 if (!cpu->valid_pss_table) 497 return; 498 499 acpi_processor_unregister_performance(policy->cpu); 500 } 501 #else /* CONFIG_ACPI */ 502 static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy) 503 { 504 } 505 506 static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy) 507 { 508 } 509 510 static inline bool intel_pstate_acpi_pm_profile_server(void) 511 { 512 return false; 513 } 514 #endif /* CONFIG_ACPI */ 515 516 #ifndef CONFIG_ACPI_CPPC_LIB 517 static inline int intel_pstate_get_cppc_guaranteed(int cpu) 518 { 519 return -ENOTSUPP; 520 } 521 522 static int intel_pstate_cppc_get_scaling(int cpu) 523 { 524 return core_get_scaling(); 525 } 526 #endif /* CONFIG_ACPI_CPPC_LIB */ 527 528 static int intel_pstate_freq_to_hwp_rel(struct cpudata *cpu, int freq, 529 unsigned int relation) 530 { 531 if (freq == cpu->pstate.turbo_freq) 532 return cpu->pstate.turbo_pstate; 533 534 if (freq == cpu->pstate.max_freq) 535 return cpu->pstate.max_pstate; 536 537 switch (relation) { 538 case CPUFREQ_RELATION_H: 539 return freq / cpu->pstate.scaling; 540 case CPUFREQ_RELATION_C: 541 return DIV_ROUND_CLOSEST(freq, cpu->pstate.scaling); 542 } 543 544 return DIV_ROUND_UP(freq, cpu->pstate.scaling); 545 } 546 547 static int intel_pstate_freq_to_hwp(struct cpudata *cpu, int freq) 548 { 549 return intel_pstate_freq_to_hwp_rel(cpu, freq, CPUFREQ_RELATION_L); 550 } 551 552 /** 553 * intel_pstate_hybrid_hwp_adjust - Calibrate HWP performance levels. 554 * @cpu: Target CPU. 555 * 556 * On hybrid processors, HWP may expose more performance levels than there are 557 * P-states accessible through the PERF_CTL interface. If that happens, the 558 * scaling factor between HWP performance levels and CPU frequency will be less 559 * than the scaling factor between P-state values and CPU frequency. 560 * 561 * In that case, adjust the CPU parameters used in computations accordingly. 562 */ 563 static void intel_pstate_hybrid_hwp_adjust(struct cpudata *cpu) 564 { 565 int perf_ctl_max_phys = cpu->pstate.max_pstate_physical; 566 int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling; 567 int perf_ctl_turbo = pstate_funcs.get_turbo(cpu->cpu); 568 int scaling = cpu->pstate.scaling; 569 int freq; 570 571 pr_debug("CPU%d: perf_ctl_max_phys = %d\n", cpu->cpu, perf_ctl_max_phys); 572 pr_debug("CPU%d: perf_ctl_turbo = %d\n", cpu->cpu, perf_ctl_turbo); 573 pr_debug("CPU%d: perf_ctl_scaling = %d\n", cpu->cpu, perf_ctl_scaling); 574 pr_debug("CPU%d: HWP_CAP guaranteed = %d\n", cpu->cpu, cpu->pstate.max_pstate); 575 pr_debug("CPU%d: HWP_CAP highest = %d\n", cpu->cpu, cpu->pstate.turbo_pstate); 576 pr_debug("CPU%d: HWP-to-frequency scaling factor: %d\n", cpu->cpu, scaling); 577 578 cpu->pstate.turbo_freq = rounddown(cpu->pstate.turbo_pstate * scaling, 579 perf_ctl_scaling); 580 cpu->pstate.max_freq = rounddown(cpu->pstate.max_pstate * scaling, 581 perf_ctl_scaling); 582 583 freq = perf_ctl_max_phys * perf_ctl_scaling; 584 cpu->pstate.max_pstate_physical = intel_pstate_freq_to_hwp(cpu, freq); 585 586 freq = cpu->pstate.min_pstate * perf_ctl_scaling; 587 cpu->pstate.min_freq = freq; 588 /* 589 * Cast the min P-state value retrieved via pstate_funcs.get_min() to 590 * the effective range of HWP performance levels. 591 */ 592 cpu->pstate.min_pstate = intel_pstate_freq_to_hwp(cpu, freq); 593 } 594 595 static bool turbo_is_disabled(void) 596 { 597 u64 misc_en; 598 599 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en); 600 601 return !!(misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE); 602 } 603 604 static int min_perf_pct_min(void) 605 { 606 struct cpudata *cpu = all_cpu_data[0]; 607 int turbo_pstate = cpu->pstate.turbo_pstate; 608 609 return turbo_pstate ? 610 (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0; 611 } 612 613 static s16 intel_pstate_get_epb(struct cpudata *cpu_data) 614 { 615 u64 epb; 616 int ret; 617 618 if (!boot_cpu_has(X86_FEATURE_EPB)) 619 return -ENXIO; 620 621 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb); 622 if (ret) 623 return (s16)ret; 624 625 return (s16)(epb & 0x0f); 626 } 627 628 static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data) 629 { 630 s16 epp; 631 632 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) { 633 /* 634 * When hwp_req_data is 0, means that caller didn't read 635 * MSR_HWP_REQUEST, so need to read and get EPP. 636 */ 637 if (!hwp_req_data) { 638 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, 639 &hwp_req_data); 640 if (epp) 641 return epp; 642 } 643 epp = (hwp_req_data >> 24) & 0xff; 644 } else { 645 /* When there is no EPP present, HWP uses EPB settings */ 646 epp = intel_pstate_get_epb(cpu_data); 647 } 648 649 return epp; 650 } 651 652 static int intel_pstate_set_epb(int cpu, s16 pref) 653 { 654 u64 epb; 655 int ret; 656 657 if (!boot_cpu_has(X86_FEATURE_EPB)) 658 return -ENXIO; 659 660 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb); 661 if (ret) 662 return ret; 663 664 epb = (epb & ~0x0f) | pref; 665 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb); 666 667 return 0; 668 } 669 670 /* 671 * EPP/EPB display strings corresponding to EPP index in the 672 * energy_perf_strings[] 673 * index String 674 *------------------------------------- 675 * 0 default 676 * 1 performance 677 * 2 balance_performance 678 * 3 balance_power 679 * 4 power 680 */ 681 682 enum energy_perf_value_index { 683 EPP_INDEX_DEFAULT = 0, 684 EPP_INDEX_PERFORMANCE, 685 EPP_INDEX_BALANCE_PERFORMANCE, 686 EPP_INDEX_BALANCE_POWERSAVE, 687 EPP_INDEX_POWERSAVE, 688 }; 689 690 static const char * const energy_perf_strings[] = { 691 [EPP_INDEX_DEFAULT] = "default", 692 [EPP_INDEX_PERFORMANCE] = "performance", 693 [EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance", 694 [EPP_INDEX_BALANCE_POWERSAVE] = "balance_power", 695 [EPP_INDEX_POWERSAVE] = "power", 696 NULL 697 }; 698 static unsigned int epp_values[] = { 699 [EPP_INDEX_DEFAULT] = 0, /* Unused index */ 700 [EPP_INDEX_PERFORMANCE] = HWP_EPP_PERFORMANCE, 701 [EPP_INDEX_BALANCE_PERFORMANCE] = HWP_EPP_BALANCE_PERFORMANCE, 702 [EPP_INDEX_BALANCE_POWERSAVE] = HWP_EPP_BALANCE_POWERSAVE, 703 [EPP_INDEX_POWERSAVE] = HWP_EPP_POWERSAVE, 704 }; 705 706 static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data, int *raw_epp) 707 { 708 s16 epp; 709 int index = -EINVAL; 710 711 *raw_epp = 0; 712 epp = intel_pstate_get_epp(cpu_data, 0); 713 if (epp < 0) 714 return epp; 715 716 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) { 717 if (epp == epp_values[EPP_INDEX_PERFORMANCE]) 718 return EPP_INDEX_PERFORMANCE; 719 if (epp == epp_values[EPP_INDEX_BALANCE_PERFORMANCE]) 720 return EPP_INDEX_BALANCE_PERFORMANCE; 721 if (epp == epp_values[EPP_INDEX_BALANCE_POWERSAVE]) 722 return EPP_INDEX_BALANCE_POWERSAVE; 723 if (epp == epp_values[EPP_INDEX_POWERSAVE]) 724 return EPP_INDEX_POWERSAVE; 725 *raw_epp = epp; 726 return 0; 727 } else if (boot_cpu_has(X86_FEATURE_EPB)) { 728 /* 729 * Range: 730 * 0x00-0x03 : Performance 731 * 0x04-0x07 : Balance performance 732 * 0x08-0x0B : Balance power 733 * 0x0C-0x0F : Power 734 * The EPB is a 4 bit value, but our ranges restrict the 735 * value which can be set. Here only using top two bits 736 * effectively. 737 */ 738 index = (epp >> 2) + 1; 739 } 740 741 return index; 742 } 743 744 static int intel_pstate_set_epp(struct cpudata *cpu, u32 epp) 745 { 746 int ret; 747 748 /* 749 * Use the cached HWP Request MSR value, because in the active mode the 750 * register itself may be updated by intel_pstate_hwp_boost_up() or 751 * intel_pstate_hwp_boost_down() at any time. 752 */ 753 u64 value = READ_ONCE(cpu->hwp_req_cached); 754 755 value &= ~GENMASK_ULL(31, 24); 756 value |= (u64)epp << 24; 757 /* 758 * The only other updater of hwp_req_cached in the active mode, 759 * intel_pstate_hwp_set(), is called under the same lock as this 760 * function, so it cannot run in parallel with the update below. 761 */ 762 WRITE_ONCE(cpu->hwp_req_cached, value); 763 ret = wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value); 764 if (!ret) 765 cpu->epp_cached = epp; 766 767 return ret; 768 } 769 770 static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data, 771 int pref_index, bool use_raw, 772 u32 raw_epp) 773 { 774 int epp = -EINVAL; 775 int ret; 776 777 if (!pref_index) 778 epp = cpu_data->epp_default; 779 780 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) { 781 if (use_raw) 782 epp = raw_epp; 783 else if (epp == -EINVAL) 784 epp = epp_values[pref_index]; 785 786 /* 787 * To avoid confusion, refuse to set EPP to any values different 788 * from 0 (performance) if the current policy is "performance", 789 * because those values would be overridden. 790 */ 791 if (epp > 0 && cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) 792 return -EBUSY; 793 794 ret = intel_pstate_set_epp(cpu_data, epp); 795 } else { 796 if (epp == -EINVAL) 797 epp = (pref_index - 1) << 2; 798 ret = intel_pstate_set_epb(cpu_data->cpu, epp); 799 } 800 801 return ret; 802 } 803 804 static ssize_t show_energy_performance_available_preferences( 805 struct cpufreq_policy *policy, char *buf) 806 { 807 int i = 0; 808 int ret = 0; 809 810 while (energy_perf_strings[i] != NULL) 811 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]); 812 813 ret += sprintf(&buf[ret], "\n"); 814 815 return ret; 816 } 817 818 cpufreq_freq_attr_ro(energy_performance_available_preferences); 819 820 static struct cpufreq_driver intel_pstate; 821 822 static ssize_t store_energy_performance_preference( 823 struct cpufreq_policy *policy, const char *buf, size_t count) 824 { 825 struct cpudata *cpu = all_cpu_data[policy->cpu]; 826 char str_preference[21]; 827 bool raw = false; 828 ssize_t ret; 829 u32 epp = 0; 830 831 ret = sscanf(buf, "%20s", str_preference); 832 if (ret != 1) 833 return -EINVAL; 834 835 ret = match_string(energy_perf_strings, -1, str_preference); 836 if (ret < 0) { 837 if (!boot_cpu_has(X86_FEATURE_HWP_EPP)) 838 return ret; 839 840 ret = kstrtouint(buf, 10, &epp); 841 if (ret) 842 return ret; 843 844 if (epp > 255) 845 return -EINVAL; 846 847 raw = true; 848 } 849 850 /* 851 * This function runs with the policy R/W semaphore held, which 852 * guarantees that the driver pointer will not change while it is 853 * running. 854 */ 855 if (!intel_pstate_driver) 856 return -EAGAIN; 857 858 mutex_lock(&intel_pstate_limits_lock); 859 860 if (intel_pstate_driver == &intel_pstate) { 861 ret = intel_pstate_set_energy_pref_index(cpu, ret, raw, epp); 862 } else { 863 /* 864 * In the passive mode the governor needs to be stopped on the 865 * target CPU before the EPP update and restarted after it, 866 * which is super-heavy-weight, so make sure it is worth doing 867 * upfront. 868 */ 869 if (!raw) 870 epp = ret ? epp_values[ret] : cpu->epp_default; 871 872 if (cpu->epp_cached != epp) { 873 int err; 874 875 cpufreq_stop_governor(policy); 876 ret = intel_pstate_set_epp(cpu, epp); 877 err = cpufreq_start_governor(policy); 878 if (!ret) 879 ret = err; 880 } else { 881 ret = 0; 882 } 883 } 884 885 mutex_unlock(&intel_pstate_limits_lock); 886 887 return ret ?: count; 888 } 889 890 static ssize_t show_energy_performance_preference( 891 struct cpufreq_policy *policy, char *buf) 892 { 893 struct cpudata *cpu_data = all_cpu_data[policy->cpu]; 894 int preference, raw_epp; 895 896 preference = intel_pstate_get_energy_pref_index(cpu_data, &raw_epp); 897 if (preference < 0) 898 return preference; 899 900 if (raw_epp) 901 return sprintf(buf, "%d\n", raw_epp); 902 else 903 return sprintf(buf, "%s\n", energy_perf_strings[preference]); 904 } 905 906 cpufreq_freq_attr_rw(energy_performance_preference); 907 908 static ssize_t show_base_frequency(struct cpufreq_policy *policy, char *buf) 909 { 910 struct cpudata *cpu = all_cpu_data[policy->cpu]; 911 int ratio, freq; 912 913 ratio = intel_pstate_get_cppc_guaranteed(policy->cpu); 914 if (ratio <= 0) { 915 u64 cap; 916 917 rdmsrl_on_cpu(policy->cpu, MSR_HWP_CAPABILITIES, &cap); 918 ratio = HWP_GUARANTEED_PERF(cap); 919 } 920 921 freq = ratio * cpu->pstate.scaling; 922 if (cpu->pstate.scaling != cpu->pstate.perf_ctl_scaling) 923 freq = rounddown(freq, cpu->pstate.perf_ctl_scaling); 924 925 return sprintf(buf, "%d\n", freq); 926 } 927 928 cpufreq_freq_attr_ro(base_frequency); 929 930 static struct freq_attr *hwp_cpufreq_attrs[] = { 931 &energy_performance_preference, 932 &energy_performance_available_preferences, 933 &base_frequency, 934 NULL, 935 }; 936 937 static void __intel_pstate_get_hwp_cap(struct cpudata *cpu) 938 { 939 u64 cap; 940 941 rdmsrl_on_cpu(cpu->cpu, MSR_HWP_CAPABILITIES, &cap); 942 WRITE_ONCE(cpu->hwp_cap_cached, cap); 943 cpu->pstate.max_pstate = HWP_GUARANTEED_PERF(cap); 944 cpu->pstate.turbo_pstate = HWP_HIGHEST_PERF(cap); 945 } 946 947 static void intel_pstate_get_hwp_cap(struct cpudata *cpu) 948 { 949 int scaling = cpu->pstate.scaling; 950 951 __intel_pstate_get_hwp_cap(cpu); 952 953 cpu->pstate.max_freq = cpu->pstate.max_pstate * scaling; 954 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * scaling; 955 if (scaling != cpu->pstate.perf_ctl_scaling) { 956 int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling; 957 958 cpu->pstate.max_freq = rounddown(cpu->pstate.max_freq, 959 perf_ctl_scaling); 960 cpu->pstate.turbo_freq = rounddown(cpu->pstate.turbo_freq, 961 perf_ctl_scaling); 962 } 963 } 964 965 static void intel_pstate_hwp_set(unsigned int cpu) 966 { 967 struct cpudata *cpu_data = all_cpu_data[cpu]; 968 int max, min; 969 u64 value; 970 s16 epp; 971 972 max = cpu_data->max_perf_ratio; 973 min = cpu_data->min_perf_ratio; 974 975 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) 976 min = max; 977 978 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value); 979 980 value &= ~HWP_MIN_PERF(~0L); 981 value |= HWP_MIN_PERF(min); 982 983 value &= ~HWP_MAX_PERF(~0L); 984 value |= HWP_MAX_PERF(max); 985 986 if (cpu_data->epp_policy == cpu_data->policy) 987 goto skip_epp; 988 989 cpu_data->epp_policy = cpu_data->policy; 990 991 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) { 992 epp = intel_pstate_get_epp(cpu_data, value); 993 cpu_data->epp_powersave = epp; 994 /* If EPP read was failed, then don't try to write */ 995 if (epp < 0) 996 goto skip_epp; 997 998 epp = 0; 999 } else { 1000 /* skip setting EPP, when saved value is invalid */ 1001 if (cpu_data->epp_powersave < 0) 1002 goto skip_epp; 1003 1004 /* 1005 * No need to restore EPP when it is not zero. This 1006 * means: 1007 * - Policy is not changed 1008 * - user has manually changed 1009 * - Error reading EPB 1010 */ 1011 epp = intel_pstate_get_epp(cpu_data, value); 1012 if (epp) 1013 goto skip_epp; 1014 1015 epp = cpu_data->epp_powersave; 1016 } 1017 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) { 1018 value &= ~GENMASK_ULL(31, 24); 1019 value |= (u64)epp << 24; 1020 } else { 1021 intel_pstate_set_epb(cpu, epp); 1022 } 1023 skip_epp: 1024 WRITE_ONCE(cpu_data->hwp_req_cached, value); 1025 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value); 1026 } 1027 1028 static void intel_pstate_disable_hwp_interrupt(struct cpudata *cpudata); 1029 1030 static void intel_pstate_hwp_offline(struct cpudata *cpu) 1031 { 1032 u64 value = READ_ONCE(cpu->hwp_req_cached); 1033 int min_perf; 1034 1035 intel_pstate_disable_hwp_interrupt(cpu); 1036 1037 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) { 1038 /* 1039 * In case the EPP has been set to "performance" by the 1040 * active mode "performance" scaling algorithm, replace that 1041 * temporary value with the cached EPP one. 1042 */ 1043 value &= ~GENMASK_ULL(31, 24); 1044 value |= HWP_ENERGY_PERF_PREFERENCE(cpu->epp_cached); 1045 /* 1046 * However, make sure that EPP will be set to "performance" when 1047 * the CPU is brought back online again and the "performance" 1048 * scaling algorithm is still in effect. 1049 */ 1050 cpu->epp_policy = CPUFREQ_POLICY_UNKNOWN; 1051 } 1052 1053 /* 1054 * Clear the desired perf field in the cached HWP request value to 1055 * prevent nonzero desired values from being leaked into the active 1056 * mode. 1057 */ 1058 value &= ~HWP_DESIRED_PERF(~0L); 1059 WRITE_ONCE(cpu->hwp_req_cached, value); 1060 1061 value &= ~GENMASK_ULL(31, 0); 1062 min_perf = HWP_LOWEST_PERF(READ_ONCE(cpu->hwp_cap_cached)); 1063 1064 /* Set hwp_max = hwp_min */ 1065 value |= HWP_MAX_PERF(min_perf); 1066 value |= HWP_MIN_PERF(min_perf); 1067 1068 /* Set EPP to min */ 1069 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) 1070 value |= HWP_ENERGY_PERF_PREFERENCE(HWP_EPP_POWERSAVE); 1071 1072 wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value); 1073 } 1074 1075 #define POWER_CTL_EE_ENABLE 1 1076 #define POWER_CTL_EE_DISABLE 2 1077 1078 static int power_ctl_ee_state; 1079 1080 static void set_power_ctl_ee_state(bool input) 1081 { 1082 u64 power_ctl; 1083 1084 mutex_lock(&intel_pstate_driver_lock); 1085 rdmsrl(MSR_IA32_POWER_CTL, power_ctl); 1086 if (input) { 1087 power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE); 1088 power_ctl_ee_state = POWER_CTL_EE_ENABLE; 1089 } else { 1090 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE); 1091 power_ctl_ee_state = POWER_CTL_EE_DISABLE; 1092 } 1093 wrmsrl(MSR_IA32_POWER_CTL, power_ctl); 1094 mutex_unlock(&intel_pstate_driver_lock); 1095 } 1096 1097 static void intel_pstate_hwp_enable(struct cpudata *cpudata); 1098 1099 static void intel_pstate_hwp_reenable(struct cpudata *cpu) 1100 { 1101 intel_pstate_hwp_enable(cpu); 1102 wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, READ_ONCE(cpu->hwp_req_cached)); 1103 } 1104 1105 static int intel_pstate_suspend(struct cpufreq_policy *policy) 1106 { 1107 struct cpudata *cpu = all_cpu_data[policy->cpu]; 1108 1109 pr_debug("CPU %d suspending\n", cpu->cpu); 1110 1111 cpu->suspended = true; 1112 1113 /* disable HWP interrupt and cancel any pending work */ 1114 intel_pstate_disable_hwp_interrupt(cpu); 1115 1116 return 0; 1117 } 1118 1119 static int intel_pstate_resume(struct cpufreq_policy *policy) 1120 { 1121 struct cpudata *cpu = all_cpu_data[policy->cpu]; 1122 1123 pr_debug("CPU %d resuming\n", cpu->cpu); 1124 1125 /* Only restore if the system default is changed */ 1126 if (power_ctl_ee_state == POWER_CTL_EE_ENABLE) 1127 set_power_ctl_ee_state(true); 1128 else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE) 1129 set_power_ctl_ee_state(false); 1130 1131 if (cpu->suspended && hwp_active) { 1132 mutex_lock(&intel_pstate_limits_lock); 1133 1134 /* Re-enable HWP, because "online" has not done that. */ 1135 intel_pstate_hwp_reenable(cpu); 1136 1137 mutex_unlock(&intel_pstate_limits_lock); 1138 } 1139 1140 cpu->suspended = false; 1141 1142 return 0; 1143 } 1144 1145 static void intel_pstate_update_policies(void) 1146 { 1147 int cpu; 1148 1149 for_each_possible_cpu(cpu) 1150 cpufreq_update_policy(cpu); 1151 } 1152 1153 static void __intel_pstate_update_max_freq(struct cpudata *cpudata, 1154 struct cpufreq_policy *policy) 1155 { 1156 if (hwp_active) 1157 intel_pstate_get_hwp_cap(cpudata); 1158 1159 policy->cpuinfo.max_freq = READ_ONCE(global.no_turbo) ? 1160 cpudata->pstate.max_freq : cpudata->pstate.turbo_freq; 1161 1162 refresh_frequency_limits(policy); 1163 } 1164 1165 static void intel_pstate_update_limits(unsigned int cpu) 1166 { 1167 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu); 1168 1169 if (!policy) 1170 return; 1171 1172 __intel_pstate_update_max_freq(all_cpu_data[cpu], policy); 1173 1174 cpufreq_cpu_release(policy); 1175 } 1176 1177 static void intel_pstate_update_limits_for_all(void) 1178 { 1179 int cpu; 1180 1181 for_each_possible_cpu(cpu) 1182 intel_pstate_update_limits(cpu); 1183 } 1184 1185 /************************** sysfs begin ************************/ 1186 #define show_one(file_name, object) \ 1187 static ssize_t show_##file_name \ 1188 (struct kobject *kobj, struct kobj_attribute *attr, char *buf) \ 1189 { \ 1190 return sprintf(buf, "%u\n", global.object); \ 1191 } 1192 1193 static ssize_t intel_pstate_show_status(char *buf); 1194 static int intel_pstate_update_status(const char *buf, size_t size); 1195 1196 static ssize_t show_status(struct kobject *kobj, 1197 struct kobj_attribute *attr, char *buf) 1198 { 1199 ssize_t ret; 1200 1201 mutex_lock(&intel_pstate_driver_lock); 1202 ret = intel_pstate_show_status(buf); 1203 mutex_unlock(&intel_pstate_driver_lock); 1204 1205 return ret; 1206 } 1207 1208 static ssize_t store_status(struct kobject *a, struct kobj_attribute *b, 1209 const char *buf, size_t count) 1210 { 1211 char *p = memchr(buf, '\n', count); 1212 int ret; 1213 1214 mutex_lock(&intel_pstate_driver_lock); 1215 ret = intel_pstate_update_status(buf, p ? p - buf : count); 1216 mutex_unlock(&intel_pstate_driver_lock); 1217 1218 return ret < 0 ? ret : count; 1219 } 1220 1221 static ssize_t show_turbo_pct(struct kobject *kobj, 1222 struct kobj_attribute *attr, char *buf) 1223 { 1224 struct cpudata *cpu; 1225 int total, no_turbo, turbo_pct; 1226 uint32_t turbo_fp; 1227 1228 mutex_lock(&intel_pstate_driver_lock); 1229 1230 if (!intel_pstate_driver) { 1231 mutex_unlock(&intel_pstate_driver_lock); 1232 return -EAGAIN; 1233 } 1234 1235 cpu = all_cpu_data[0]; 1236 1237 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1; 1238 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1; 1239 turbo_fp = div_fp(no_turbo, total); 1240 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100))); 1241 1242 mutex_unlock(&intel_pstate_driver_lock); 1243 1244 return sprintf(buf, "%u\n", turbo_pct); 1245 } 1246 1247 static ssize_t show_num_pstates(struct kobject *kobj, 1248 struct kobj_attribute *attr, char *buf) 1249 { 1250 struct cpudata *cpu; 1251 int total; 1252 1253 mutex_lock(&intel_pstate_driver_lock); 1254 1255 if (!intel_pstate_driver) { 1256 mutex_unlock(&intel_pstate_driver_lock); 1257 return -EAGAIN; 1258 } 1259 1260 cpu = all_cpu_data[0]; 1261 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1; 1262 1263 mutex_unlock(&intel_pstate_driver_lock); 1264 1265 return sprintf(buf, "%u\n", total); 1266 } 1267 1268 static ssize_t show_no_turbo(struct kobject *kobj, 1269 struct kobj_attribute *attr, char *buf) 1270 { 1271 ssize_t ret; 1272 1273 mutex_lock(&intel_pstate_driver_lock); 1274 1275 if (!intel_pstate_driver) { 1276 mutex_unlock(&intel_pstate_driver_lock); 1277 return -EAGAIN; 1278 } 1279 1280 ret = sprintf(buf, "%u\n", global.no_turbo); 1281 1282 mutex_unlock(&intel_pstate_driver_lock); 1283 1284 return ret; 1285 } 1286 1287 static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b, 1288 const char *buf, size_t count) 1289 { 1290 unsigned int input; 1291 bool no_turbo; 1292 1293 if (sscanf(buf, "%u", &input) != 1) 1294 return -EINVAL; 1295 1296 mutex_lock(&intel_pstate_driver_lock); 1297 1298 if (!intel_pstate_driver) { 1299 count = -EAGAIN; 1300 goto unlock_driver; 1301 } 1302 1303 no_turbo = !!clamp_t(int, input, 0, 1); 1304 1305 WRITE_ONCE(global.turbo_disabled, turbo_is_disabled()); 1306 if (global.turbo_disabled && !no_turbo) { 1307 pr_notice("Turbo disabled by BIOS or unavailable on processor\n"); 1308 count = -EPERM; 1309 if (global.no_turbo) 1310 goto unlock_driver; 1311 else 1312 no_turbo = 1; 1313 } 1314 1315 if (no_turbo == global.no_turbo) { 1316 goto unlock_driver; 1317 } 1318 1319 WRITE_ONCE(global.no_turbo, no_turbo); 1320 1321 mutex_lock(&intel_pstate_limits_lock); 1322 1323 if (no_turbo) { 1324 struct cpudata *cpu = all_cpu_data[0]; 1325 int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate; 1326 1327 /* Squash the global minimum into the permitted range. */ 1328 if (global.min_perf_pct > pct) 1329 global.min_perf_pct = pct; 1330 } 1331 1332 mutex_unlock(&intel_pstate_limits_lock); 1333 1334 intel_pstate_update_limits_for_all(); 1335 arch_set_max_freq_ratio(no_turbo); 1336 1337 unlock_driver: 1338 mutex_unlock(&intel_pstate_driver_lock); 1339 1340 return count; 1341 } 1342 1343 static void update_qos_request(enum freq_qos_req_type type) 1344 { 1345 struct freq_qos_request *req; 1346 struct cpufreq_policy *policy; 1347 int i; 1348 1349 for_each_possible_cpu(i) { 1350 struct cpudata *cpu = all_cpu_data[i]; 1351 unsigned int freq, perf_pct; 1352 1353 policy = cpufreq_cpu_get(i); 1354 if (!policy) 1355 continue; 1356 1357 req = policy->driver_data; 1358 cpufreq_cpu_put(policy); 1359 1360 if (!req) 1361 continue; 1362 1363 if (hwp_active) 1364 intel_pstate_get_hwp_cap(cpu); 1365 1366 if (type == FREQ_QOS_MIN) { 1367 perf_pct = global.min_perf_pct; 1368 } else { 1369 req++; 1370 perf_pct = global.max_perf_pct; 1371 } 1372 1373 freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * perf_pct, 100); 1374 1375 if (freq_qos_update_request(req, freq) < 0) 1376 pr_warn("Failed to update freq constraint: CPU%d\n", i); 1377 } 1378 } 1379 1380 static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b, 1381 const char *buf, size_t count) 1382 { 1383 unsigned int input; 1384 int ret; 1385 1386 ret = sscanf(buf, "%u", &input); 1387 if (ret != 1) 1388 return -EINVAL; 1389 1390 mutex_lock(&intel_pstate_driver_lock); 1391 1392 if (!intel_pstate_driver) { 1393 mutex_unlock(&intel_pstate_driver_lock); 1394 return -EAGAIN; 1395 } 1396 1397 mutex_lock(&intel_pstate_limits_lock); 1398 1399 global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100); 1400 1401 mutex_unlock(&intel_pstate_limits_lock); 1402 1403 if (intel_pstate_driver == &intel_pstate) 1404 intel_pstate_update_policies(); 1405 else 1406 update_qos_request(FREQ_QOS_MAX); 1407 1408 mutex_unlock(&intel_pstate_driver_lock); 1409 1410 return count; 1411 } 1412 1413 static ssize_t store_min_perf_pct(struct kobject *a, struct kobj_attribute *b, 1414 const char *buf, size_t count) 1415 { 1416 unsigned int input; 1417 int ret; 1418 1419 ret = sscanf(buf, "%u", &input); 1420 if (ret != 1) 1421 return -EINVAL; 1422 1423 mutex_lock(&intel_pstate_driver_lock); 1424 1425 if (!intel_pstate_driver) { 1426 mutex_unlock(&intel_pstate_driver_lock); 1427 return -EAGAIN; 1428 } 1429 1430 mutex_lock(&intel_pstate_limits_lock); 1431 1432 global.min_perf_pct = clamp_t(int, input, 1433 min_perf_pct_min(), global.max_perf_pct); 1434 1435 mutex_unlock(&intel_pstate_limits_lock); 1436 1437 if (intel_pstate_driver == &intel_pstate) 1438 intel_pstate_update_policies(); 1439 else 1440 update_qos_request(FREQ_QOS_MIN); 1441 1442 mutex_unlock(&intel_pstate_driver_lock); 1443 1444 return count; 1445 } 1446 1447 static ssize_t show_hwp_dynamic_boost(struct kobject *kobj, 1448 struct kobj_attribute *attr, char *buf) 1449 { 1450 return sprintf(buf, "%u\n", hwp_boost); 1451 } 1452 1453 static ssize_t store_hwp_dynamic_boost(struct kobject *a, 1454 struct kobj_attribute *b, 1455 const char *buf, size_t count) 1456 { 1457 unsigned int input; 1458 int ret; 1459 1460 ret = kstrtouint(buf, 10, &input); 1461 if (ret) 1462 return ret; 1463 1464 mutex_lock(&intel_pstate_driver_lock); 1465 hwp_boost = !!input; 1466 intel_pstate_update_policies(); 1467 mutex_unlock(&intel_pstate_driver_lock); 1468 1469 return count; 1470 } 1471 1472 static ssize_t show_energy_efficiency(struct kobject *kobj, struct kobj_attribute *attr, 1473 char *buf) 1474 { 1475 u64 power_ctl; 1476 int enable; 1477 1478 rdmsrl(MSR_IA32_POWER_CTL, power_ctl); 1479 enable = !!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE)); 1480 return sprintf(buf, "%d\n", !enable); 1481 } 1482 1483 static ssize_t store_energy_efficiency(struct kobject *a, struct kobj_attribute *b, 1484 const char *buf, size_t count) 1485 { 1486 bool input; 1487 int ret; 1488 1489 ret = kstrtobool(buf, &input); 1490 if (ret) 1491 return ret; 1492 1493 set_power_ctl_ee_state(input); 1494 1495 return count; 1496 } 1497 1498 show_one(max_perf_pct, max_perf_pct); 1499 show_one(min_perf_pct, min_perf_pct); 1500 1501 define_one_global_rw(status); 1502 define_one_global_rw(no_turbo); 1503 define_one_global_rw(max_perf_pct); 1504 define_one_global_rw(min_perf_pct); 1505 define_one_global_ro(turbo_pct); 1506 define_one_global_ro(num_pstates); 1507 define_one_global_rw(hwp_dynamic_boost); 1508 define_one_global_rw(energy_efficiency); 1509 1510 static struct attribute *intel_pstate_attributes[] = { 1511 &status.attr, 1512 &no_turbo.attr, 1513 NULL 1514 }; 1515 1516 static const struct attribute_group intel_pstate_attr_group = { 1517 .attrs = intel_pstate_attributes, 1518 }; 1519 1520 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[]; 1521 1522 static struct kobject *intel_pstate_kobject; 1523 1524 static void __init intel_pstate_sysfs_expose_params(void) 1525 { 1526 struct device *dev_root = bus_get_dev_root(&cpu_subsys); 1527 int rc; 1528 1529 if (dev_root) { 1530 intel_pstate_kobject = kobject_create_and_add("intel_pstate", &dev_root->kobj); 1531 put_device(dev_root); 1532 } 1533 if (WARN_ON(!intel_pstate_kobject)) 1534 return; 1535 1536 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group); 1537 if (WARN_ON(rc)) 1538 return; 1539 1540 if (!boot_cpu_has(X86_FEATURE_HYBRID_CPU)) { 1541 rc = sysfs_create_file(intel_pstate_kobject, &turbo_pct.attr); 1542 WARN_ON(rc); 1543 1544 rc = sysfs_create_file(intel_pstate_kobject, &num_pstates.attr); 1545 WARN_ON(rc); 1546 } 1547 1548 /* 1549 * If per cpu limits are enforced there are no global limits, so 1550 * return without creating max/min_perf_pct attributes 1551 */ 1552 if (per_cpu_limits) 1553 return; 1554 1555 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr); 1556 WARN_ON(rc); 1557 1558 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr); 1559 WARN_ON(rc); 1560 1561 if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids)) { 1562 rc = sysfs_create_file(intel_pstate_kobject, &energy_efficiency.attr); 1563 WARN_ON(rc); 1564 } 1565 } 1566 1567 static void __init intel_pstate_sysfs_remove(void) 1568 { 1569 if (!intel_pstate_kobject) 1570 return; 1571 1572 sysfs_remove_group(intel_pstate_kobject, &intel_pstate_attr_group); 1573 1574 if (!boot_cpu_has(X86_FEATURE_HYBRID_CPU)) { 1575 sysfs_remove_file(intel_pstate_kobject, &num_pstates.attr); 1576 sysfs_remove_file(intel_pstate_kobject, &turbo_pct.attr); 1577 } 1578 1579 if (!per_cpu_limits) { 1580 sysfs_remove_file(intel_pstate_kobject, &max_perf_pct.attr); 1581 sysfs_remove_file(intel_pstate_kobject, &min_perf_pct.attr); 1582 1583 if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids)) 1584 sysfs_remove_file(intel_pstate_kobject, &energy_efficiency.attr); 1585 } 1586 1587 kobject_put(intel_pstate_kobject); 1588 } 1589 1590 static void intel_pstate_sysfs_expose_hwp_dynamic_boost(void) 1591 { 1592 int rc; 1593 1594 if (!hwp_active) 1595 return; 1596 1597 rc = sysfs_create_file(intel_pstate_kobject, &hwp_dynamic_boost.attr); 1598 WARN_ON_ONCE(rc); 1599 } 1600 1601 static void intel_pstate_sysfs_hide_hwp_dynamic_boost(void) 1602 { 1603 if (!hwp_active) 1604 return; 1605 1606 sysfs_remove_file(intel_pstate_kobject, &hwp_dynamic_boost.attr); 1607 } 1608 1609 /************************** sysfs end ************************/ 1610 1611 static void intel_pstate_notify_work(struct work_struct *work) 1612 { 1613 struct cpudata *cpudata = 1614 container_of(to_delayed_work(work), struct cpudata, hwp_notify_work); 1615 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpudata->cpu); 1616 1617 if (policy) { 1618 __intel_pstate_update_max_freq(cpudata, policy); 1619 1620 cpufreq_cpu_release(policy); 1621 } 1622 1623 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_STATUS, 0); 1624 } 1625 1626 static DEFINE_SPINLOCK(hwp_notify_lock); 1627 static cpumask_t hwp_intr_enable_mask; 1628 1629 void notify_hwp_interrupt(void) 1630 { 1631 unsigned int this_cpu = smp_processor_id(); 1632 unsigned long flags; 1633 u64 value; 1634 1635 if (!hwp_active || !boot_cpu_has(X86_FEATURE_HWP_NOTIFY)) 1636 return; 1637 1638 rdmsrl_safe(MSR_HWP_STATUS, &value); 1639 if (!(value & 0x01)) 1640 return; 1641 1642 spin_lock_irqsave(&hwp_notify_lock, flags); 1643 1644 if (!cpumask_test_cpu(this_cpu, &hwp_intr_enable_mask)) 1645 goto ack_intr; 1646 1647 schedule_delayed_work(&all_cpu_data[this_cpu]->hwp_notify_work, 1648 msecs_to_jiffies(10)); 1649 1650 spin_unlock_irqrestore(&hwp_notify_lock, flags); 1651 1652 return; 1653 1654 ack_intr: 1655 wrmsrl_safe(MSR_HWP_STATUS, 0); 1656 spin_unlock_irqrestore(&hwp_notify_lock, flags); 1657 } 1658 1659 static void intel_pstate_disable_hwp_interrupt(struct cpudata *cpudata) 1660 { 1661 bool cancel_work; 1662 1663 if (!boot_cpu_has(X86_FEATURE_HWP_NOTIFY)) 1664 return; 1665 1666 /* wrmsrl_on_cpu has to be outside spinlock as this can result in IPC */ 1667 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00); 1668 1669 spin_lock_irq(&hwp_notify_lock); 1670 cancel_work = cpumask_test_and_clear_cpu(cpudata->cpu, &hwp_intr_enable_mask); 1671 spin_unlock_irq(&hwp_notify_lock); 1672 1673 if (cancel_work) 1674 cancel_delayed_work_sync(&cpudata->hwp_notify_work); 1675 } 1676 1677 static void intel_pstate_enable_hwp_interrupt(struct cpudata *cpudata) 1678 { 1679 /* Enable HWP notification interrupt for guaranteed performance change */ 1680 if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY)) { 1681 spin_lock_irq(&hwp_notify_lock); 1682 INIT_DELAYED_WORK(&cpudata->hwp_notify_work, intel_pstate_notify_work); 1683 cpumask_set_cpu(cpudata->cpu, &hwp_intr_enable_mask); 1684 spin_unlock_irq(&hwp_notify_lock); 1685 1686 /* wrmsrl_on_cpu has to be outside spinlock as this can result in IPC */ 1687 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x01); 1688 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_STATUS, 0); 1689 } 1690 } 1691 1692 static void intel_pstate_update_epp_defaults(struct cpudata *cpudata) 1693 { 1694 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0); 1695 1696 /* 1697 * If the EPP is set by firmware, which means that firmware enabled HWP 1698 * - Is equal or less than 0x80 (default balance_perf EPP) 1699 * - But less performance oriented than performance EPP 1700 * then use this as new balance_perf EPP. 1701 */ 1702 if (hwp_forced && cpudata->epp_default <= HWP_EPP_BALANCE_PERFORMANCE && 1703 cpudata->epp_default > HWP_EPP_PERFORMANCE) { 1704 epp_values[EPP_INDEX_BALANCE_PERFORMANCE] = cpudata->epp_default; 1705 return; 1706 } 1707 1708 /* 1709 * If this CPU gen doesn't call for change in balance_perf 1710 * EPP return. 1711 */ 1712 if (epp_values[EPP_INDEX_BALANCE_PERFORMANCE] == HWP_EPP_BALANCE_PERFORMANCE) 1713 return; 1714 1715 /* 1716 * Use hard coded value per gen to update the balance_perf 1717 * and default EPP. 1718 */ 1719 cpudata->epp_default = epp_values[EPP_INDEX_BALANCE_PERFORMANCE]; 1720 intel_pstate_set_epp(cpudata, cpudata->epp_default); 1721 } 1722 1723 static void intel_pstate_hwp_enable(struct cpudata *cpudata) 1724 { 1725 /* First disable HWP notification interrupt till we activate again */ 1726 if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY)) 1727 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00); 1728 1729 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1); 1730 1731 intel_pstate_enable_hwp_interrupt(cpudata); 1732 1733 if (cpudata->epp_default >= 0) 1734 return; 1735 1736 intel_pstate_update_epp_defaults(cpudata); 1737 } 1738 1739 static int atom_get_min_pstate(int not_used) 1740 { 1741 u64 value; 1742 1743 rdmsrl(MSR_ATOM_CORE_RATIOS, value); 1744 return (value >> 8) & 0x7F; 1745 } 1746 1747 static int atom_get_max_pstate(int not_used) 1748 { 1749 u64 value; 1750 1751 rdmsrl(MSR_ATOM_CORE_RATIOS, value); 1752 return (value >> 16) & 0x7F; 1753 } 1754 1755 static int atom_get_turbo_pstate(int not_used) 1756 { 1757 u64 value; 1758 1759 rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value); 1760 return value & 0x7F; 1761 } 1762 1763 static u64 atom_get_val(struct cpudata *cpudata, int pstate) 1764 { 1765 u64 val; 1766 int32_t vid_fp; 1767 u32 vid; 1768 1769 val = (u64)pstate << 8; 1770 if (READ_ONCE(global.no_turbo) && !READ_ONCE(global.turbo_disabled)) 1771 val |= (u64)1 << 32; 1772 1773 vid_fp = cpudata->vid.min + mul_fp( 1774 int_tofp(pstate - cpudata->pstate.min_pstate), 1775 cpudata->vid.ratio); 1776 1777 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max); 1778 vid = ceiling_fp(vid_fp); 1779 1780 if (pstate > cpudata->pstate.max_pstate) 1781 vid = cpudata->vid.turbo; 1782 1783 return val | vid; 1784 } 1785 1786 static int silvermont_get_scaling(void) 1787 { 1788 u64 value; 1789 int i; 1790 /* Defined in Table 35-6 from SDM (Sept 2015) */ 1791 static int silvermont_freq_table[] = { 1792 83300, 100000, 133300, 116700, 80000}; 1793 1794 rdmsrl(MSR_FSB_FREQ, value); 1795 i = value & 0x7; 1796 WARN_ON(i > 4); 1797 1798 return silvermont_freq_table[i]; 1799 } 1800 1801 static int airmont_get_scaling(void) 1802 { 1803 u64 value; 1804 int i; 1805 /* Defined in Table 35-10 from SDM (Sept 2015) */ 1806 static int airmont_freq_table[] = { 1807 83300, 100000, 133300, 116700, 80000, 1808 93300, 90000, 88900, 87500}; 1809 1810 rdmsrl(MSR_FSB_FREQ, value); 1811 i = value & 0xF; 1812 WARN_ON(i > 8); 1813 1814 return airmont_freq_table[i]; 1815 } 1816 1817 static void atom_get_vid(struct cpudata *cpudata) 1818 { 1819 u64 value; 1820 1821 rdmsrl(MSR_ATOM_CORE_VIDS, value); 1822 cpudata->vid.min = int_tofp((value >> 8) & 0x7f); 1823 cpudata->vid.max = int_tofp((value >> 16) & 0x7f); 1824 cpudata->vid.ratio = div_fp( 1825 cpudata->vid.max - cpudata->vid.min, 1826 int_tofp(cpudata->pstate.max_pstate - 1827 cpudata->pstate.min_pstate)); 1828 1829 rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value); 1830 cpudata->vid.turbo = value & 0x7f; 1831 } 1832 1833 static int core_get_min_pstate(int cpu) 1834 { 1835 u64 value; 1836 1837 rdmsrl_on_cpu(cpu, MSR_PLATFORM_INFO, &value); 1838 return (value >> 40) & 0xFF; 1839 } 1840 1841 static int core_get_max_pstate_physical(int cpu) 1842 { 1843 u64 value; 1844 1845 rdmsrl_on_cpu(cpu, MSR_PLATFORM_INFO, &value); 1846 return (value >> 8) & 0xFF; 1847 } 1848 1849 static int core_get_tdp_ratio(int cpu, u64 plat_info) 1850 { 1851 /* Check how many TDP levels present */ 1852 if (plat_info & 0x600000000) { 1853 u64 tdp_ctrl; 1854 u64 tdp_ratio; 1855 int tdp_msr; 1856 int err; 1857 1858 /* Get the TDP level (0, 1, 2) to get ratios */ 1859 err = rdmsrl_safe_on_cpu(cpu, MSR_CONFIG_TDP_CONTROL, &tdp_ctrl); 1860 if (err) 1861 return err; 1862 1863 /* TDP MSR are continuous starting at 0x648 */ 1864 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03); 1865 err = rdmsrl_safe_on_cpu(cpu, tdp_msr, &tdp_ratio); 1866 if (err) 1867 return err; 1868 1869 /* For level 1 and 2, bits[23:16] contain the ratio */ 1870 if (tdp_ctrl & 0x03) 1871 tdp_ratio >>= 16; 1872 1873 tdp_ratio &= 0xff; /* ratios are only 8 bits long */ 1874 pr_debug("tdp_ratio %x\n", (int)tdp_ratio); 1875 1876 return (int)tdp_ratio; 1877 } 1878 1879 return -ENXIO; 1880 } 1881 1882 static int core_get_max_pstate(int cpu) 1883 { 1884 u64 tar; 1885 u64 plat_info; 1886 int max_pstate; 1887 int tdp_ratio; 1888 int err; 1889 1890 rdmsrl_on_cpu(cpu, MSR_PLATFORM_INFO, &plat_info); 1891 max_pstate = (plat_info >> 8) & 0xFF; 1892 1893 tdp_ratio = core_get_tdp_ratio(cpu, plat_info); 1894 if (tdp_ratio <= 0) 1895 return max_pstate; 1896 1897 if (hwp_active) { 1898 /* Turbo activation ratio is not used on HWP platforms */ 1899 return tdp_ratio; 1900 } 1901 1902 err = rdmsrl_safe_on_cpu(cpu, MSR_TURBO_ACTIVATION_RATIO, &tar); 1903 if (!err) { 1904 int tar_levels; 1905 1906 /* Do some sanity checking for safety */ 1907 tar_levels = tar & 0xff; 1908 if (tdp_ratio - 1 == tar_levels) { 1909 max_pstate = tar_levels; 1910 pr_debug("max_pstate=TAC %x\n", max_pstate); 1911 } 1912 } 1913 1914 return max_pstate; 1915 } 1916 1917 static int core_get_turbo_pstate(int cpu) 1918 { 1919 u64 value; 1920 int nont, ret; 1921 1922 rdmsrl_on_cpu(cpu, MSR_TURBO_RATIO_LIMIT, &value); 1923 nont = core_get_max_pstate(cpu); 1924 ret = (value) & 255; 1925 if (ret <= nont) 1926 ret = nont; 1927 return ret; 1928 } 1929 1930 static u64 core_get_val(struct cpudata *cpudata, int pstate) 1931 { 1932 u64 val; 1933 1934 val = (u64)pstate << 8; 1935 if (READ_ONCE(global.no_turbo) && !READ_ONCE(global.turbo_disabled)) 1936 val |= (u64)1 << 32; 1937 1938 return val; 1939 } 1940 1941 static int knl_get_aperf_mperf_shift(void) 1942 { 1943 return 10; 1944 } 1945 1946 static int knl_get_turbo_pstate(int cpu) 1947 { 1948 u64 value; 1949 int nont, ret; 1950 1951 rdmsrl_on_cpu(cpu, MSR_TURBO_RATIO_LIMIT, &value); 1952 nont = core_get_max_pstate(cpu); 1953 ret = (((value) >> 8) & 0xFF); 1954 if (ret <= nont) 1955 ret = nont; 1956 return ret; 1957 } 1958 1959 static void hybrid_get_type(void *data) 1960 { 1961 u8 *cpu_type = data; 1962 1963 *cpu_type = get_this_hybrid_cpu_type(); 1964 } 1965 1966 static int hwp_get_cpu_scaling(int cpu) 1967 { 1968 u8 cpu_type = 0; 1969 1970 smp_call_function_single(cpu, hybrid_get_type, &cpu_type, 1); 1971 /* P-cores have a smaller perf level-to-freqency scaling factor. */ 1972 if (cpu_type == 0x40) 1973 return hybrid_scaling_factor; 1974 1975 /* Use default core scaling for E-cores */ 1976 if (cpu_type == 0x20) 1977 return core_get_scaling(); 1978 1979 /* 1980 * If reached here, this system is either non-hybrid (like Tiger 1981 * Lake) or hybrid-capable (like Alder Lake or Raptor Lake) with 1982 * no E cores (in which case CPUID for hybrid support is 0). 1983 * 1984 * The CPPC nominal_frequency field is 0 for non-hybrid systems, 1985 * so the default core scaling will be used for them. 1986 */ 1987 return intel_pstate_cppc_get_scaling(cpu); 1988 } 1989 1990 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate) 1991 { 1992 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu); 1993 cpu->pstate.current_pstate = pstate; 1994 /* 1995 * Generally, there is no guarantee that this code will always run on 1996 * the CPU being updated, so force the register update to run on the 1997 * right CPU. 1998 */ 1999 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL, 2000 pstate_funcs.get_val(cpu, pstate)); 2001 } 2002 2003 static void intel_pstate_set_min_pstate(struct cpudata *cpu) 2004 { 2005 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate); 2006 } 2007 2008 static void intel_pstate_get_cpu_pstates(struct cpudata *cpu) 2009 { 2010 int perf_ctl_max_phys = pstate_funcs.get_max_physical(cpu->cpu); 2011 int perf_ctl_scaling = pstate_funcs.get_scaling(); 2012 2013 cpu->pstate.min_pstate = pstate_funcs.get_min(cpu->cpu); 2014 cpu->pstate.max_pstate_physical = perf_ctl_max_phys; 2015 cpu->pstate.perf_ctl_scaling = perf_ctl_scaling; 2016 2017 if (hwp_active && !hwp_mode_bdw) { 2018 __intel_pstate_get_hwp_cap(cpu); 2019 2020 if (pstate_funcs.get_cpu_scaling) { 2021 cpu->pstate.scaling = pstate_funcs.get_cpu_scaling(cpu->cpu); 2022 if (cpu->pstate.scaling != perf_ctl_scaling) 2023 intel_pstate_hybrid_hwp_adjust(cpu); 2024 } else { 2025 cpu->pstate.scaling = perf_ctl_scaling; 2026 } 2027 } else { 2028 cpu->pstate.scaling = perf_ctl_scaling; 2029 cpu->pstate.max_pstate = pstate_funcs.get_max(cpu->cpu); 2030 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo(cpu->cpu); 2031 } 2032 2033 if (cpu->pstate.scaling == perf_ctl_scaling) { 2034 cpu->pstate.min_freq = cpu->pstate.min_pstate * perf_ctl_scaling; 2035 cpu->pstate.max_freq = cpu->pstate.max_pstate * perf_ctl_scaling; 2036 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * perf_ctl_scaling; 2037 } 2038 2039 if (pstate_funcs.get_aperf_mperf_shift) 2040 cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift(); 2041 2042 if (pstate_funcs.get_vid) 2043 pstate_funcs.get_vid(cpu); 2044 2045 intel_pstate_set_min_pstate(cpu); 2046 } 2047 2048 /* 2049 * Long hold time will keep high perf limits for long time, 2050 * which negatively impacts perf/watt for some workloads, 2051 * like specpower. 3ms is based on experiements on some 2052 * workoads. 2053 */ 2054 static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC; 2055 2056 static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu) 2057 { 2058 u64 hwp_req = READ_ONCE(cpu->hwp_req_cached); 2059 u64 hwp_cap = READ_ONCE(cpu->hwp_cap_cached); 2060 u32 max_limit = (hwp_req & 0xff00) >> 8; 2061 u32 min_limit = (hwp_req & 0xff); 2062 u32 boost_level1; 2063 2064 /* 2065 * Cases to consider (User changes via sysfs or boot time): 2066 * If, P0 (Turbo max) = P1 (Guaranteed max) = min: 2067 * No boost, return. 2068 * If, P0 (Turbo max) > P1 (Guaranteed max) = min: 2069 * Should result in one level boost only for P0. 2070 * If, P0 (Turbo max) = P1 (Guaranteed max) > min: 2071 * Should result in two level boost: 2072 * (min + p1)/2 and P1. 2073 * If, P0 (Turbo max) > P1 (Guaranteed max) > min: 2074 * Should result in three level boost: 2075 * (min + p1)/2, P1 and P0. 2076 */ 2077 2078 /* If max and min are equal or already at max, nothing to boost */ 2079 if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit) 2080 return; 2081 2082 if (!cpu->hwp_boost_min) 2083 cpu->hwp_boost_min = min_limit; 2084 2085 /* level at half way mark between min and guranteed */ 2086 boost_level1 = (HWP_GUARANTEED_PERF(hwp_cap) + min_limit) >> 1; 2087 2088 if (cpu->hwp_boost_min < boost_level1) 2089 cpu->hwp_boost_min = boost_level1; 2090 else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(hwp_cap)) 2091 cpu->hwp_boost_min = HWP_GUARANTEED_PERF(hwp_cap); 2092 else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(hwp_cap) && 2093 max_limit != HWP_GUARANTEED_PERF(hwp_cap)) 2094 cpu->hwp_boost_min = max_limit; 2095 else 2096 return; 2097 2098 hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min; 2099 wrmsrl(MSR_HWP_REQUEST, hwp_req); 2100 cpu->last_update = cpu->sample.time; 2101 } 2102 2103 static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu) 2104 { 2105 if (cpu->hwp_boost_min) { 2106 bool expired; 2107 2108 /* Check if we are idle for hold time to boost down */ 2109 expired = time_after64(cpu->sample.time, cpu->last_update + 2110 hwp_boost_hold_time_ns); 2111 if (expired) { 2112 wrmsrl(MSR_HWP_REQUEST, cpu->hwp_req_cached); 2113 cpu->hwp_boost_min = 0; 2114 } 2115 } 2116 cpu->last_update = cpu->sample.time; 2117 } 2118 2119 static inline void intel_pstate_update_util_hwp_local(struct cpudata *cpu, 2120 u64 time) 2121 { 2122 cpu->sample.time = time; 2123 2124 if (cpu->sched_flags & SCHED_CPUFREQ_IOWAIT) { 2125 bool do_io = false; 2126 2127 cpu->sched_flags = 0; 2128 /* 2129 * Set iowait_boost flag and update time. Since IO WAIT flag 2130 * is set all the time, we can't just conclude that there is 2131 * some IO bound activity is scheduled on this CPU with just 2132 * one occurrence. If we receive at least two in two 2133 * consecutive ticks, then we treat as boost candidate. 2134 */ 2135 if (time_before64(time, cpu->last_io_update + 2 * TICK_NSEC)) 2136 do_io = true; 2137 2138 cpu->last_io_update = time; 2139 2140 if (do_io) 2141 intel_pstate_hwp_boost_up(cpu); 2142 2143 } else { 2144 intel_pstate_hwp_boost_down(cpu); 2145 } 2146 } 2147 2148 static inline void intel_pstate_update_util_hwp(struct update_util_data *data, 2149 u64 time, unsigned int flags) 2150 { 2151 struct cpudata *cpu = container_of(data, struct cpudata, update_util); 2152 2153 cpu->sched_flags |= flags; 2154 2155 if (smp_processor_id() == cpu->cpu) 2156 intel_pstate_update_util_hwp_local(cpu, time); 2157 } 2158 2159 static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu) 2160 { 2161 struct sample *sample = &cpu->sample; 2162 2163 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf); 2164 } 2165 2166 static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time) 2167 { 2168 u64 aperf, mperf; 2169 unsigned long flags; 2170 u64 tsc; 2171 2172 local_irq_save(flags); 2173 rdmsrl(MSR_IA32_APERF, aperf); 2174 rdmsrl(MSR_IA32_MPERF, mperf); 2175 tsc = rdtsc(); 2176 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) { 2177 local_irq_restore(flags); 2178 return false; 2179 } 2180 local_irq_restore(flags); 2181 2182 cpu->last_sample_time = cpu->sample.time; 2183 cpu->sample.time = time; 2184 cpu->sample.aperf = aperf; 2185 cpu->sample.mperf = mperf; 2186 cpu->sample.tsc = tsc; 2187 cpu->sample.aperf -= cpu->prev_aperf; 2188 cpu->sample.mperf -= cpu->prev_mperf; 2189 cpu->sample.tsc -= cpu->prev_tsc; 2190 2191 cpu->prev_aperf = aperf; 2192 cpu->prev_mperf = mperf; 2193 cpu->prev_tsc = tsc; 2194 /* 2195 * First time this function is invoked in a given cycle, all of the 2196 * previous sample data fields are equal to zero or stale and they must 2197 * be populated with meaningful numbers for things to work, so assume 2198 * that sample.time will always be reset before setting the utilization 2199 * update hook and make the caller skip the sample then. 2200 */ 2201 if (cpu->last_sample_time) { 2202 intel_pstate_calc_avg_perf(cpu); 2203 return true; 2204 } 2205 return false; 2206 } 2207 2208 static inline int32_t get_avg_frequency(struct cpudata *cpu) 2209 { 2210 return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz); 2211 } 2212 2213 static inline int32_t get_avg_pstate(struct cpudata *cpu) 2214 { 2215 return mul_ext_fp(cpu->pstate.max_pstate_physical, 2216 cpu->sample.core_avg_perf); 2217 } 2218 2219 static inline int32_t get_target_pstate(struct cpudata *cpu) 2220 { 2221 struct sample *sample = &cpu->sample; 2222 int32_t busy_frac; 2223 int target, avg_pstate; 2224 2225 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift, 2226 sample->tsc); 2227 2228 if (busy_frac < cpu->iowait_boost) 2229 busy_frac = cpu->iowait_boost; 2230 2231 sample->busy_scaled = busy_frac * 100; 2232 2233 target = READ_ONCE(global.no_turbo) ? 2234 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate; 2235 target += target >> 2; 2236 target = mul_fp(target, busy_frac); 2237 if (target < cpu->pstate.min_pstate) 2238 target = cpu->pstate.min_pstate; 2239 2240 /* 2241 * If the average P-state during the previous cycle was higher than the 2242 * current target, add 50% of the difference to the target to reduce 2243 * possible performance oscillations and offset possible performance 2244 * loss related to moving the workload from one CPU to another within 2245 * a package/module. 2246 */ 2247 avg_pstate = get_avg_pstate(cpu); 2248 if (avg_pstate > target) 2249 target += (avg_pstate - target) >> 1; 2250 2251 return target; 2252 } 2253 2254 static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate) 2255 { 2256 int min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio); 2257 int max_pstate = max(min_pstate, cpu->max_perf_ratio); 2258 2259 return clamp_t(int, pstate, min_pstate, max_pstate); 2260 } 2261 2262 static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate) 2263 { 2264 if (pstate == cpu->pstate.current_pstate) 2265 return; 2266 2267 cpu->pstate.current_pstate = pstate; 2268 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate)); 2269 } 2270 2271 static void intel_pstate_adjust_pstate(struct cpudata *cpu) 2272 { 2273 int from = cpu->pstate.current_pstate; 2274 struct sample *sample; 2275 int target_pstate; 2276 2277 target_pstate = get_target_pstate(cpu); 2278 target_pstate = intel_pstate_prepare_request(cpu, target_pstate); 2279 trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu); 2280 intel_pstate_update_pstate(cpu, target_pstate); 2281 2282 sample = &cpu->sample; 2283 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf), 2284 fp_toint(sample->busy_scaled), 2285 from, 2286 cpu->pstate.current_pstate, 2287 sample->mperf, 2288 sample->aperf, 2289 sample->tsc, 2290 get_avg_frequency(cpu), 2291 fp_toint(cpu->iowait_boost * 100)); 2292 } 2293 2294 static void intel_pstate_update_util(struct update_util_data *data, u64 time, 2295 unsigned int flags) 2296 { 2297 struct cpudata *cpu = container_of(data, struct cpudata, update_util); 2298 u64 delta_ns; 2299 2300 /* Don't allow remote callbacks */ 2301 if (smp_processor_id() != cpu->cpu) 2302 return; 2303 2304 delta_ns = time - cpu->last_update; 2305 if (flags & SCHED_CPUFREQ_IOWAIT) { 2306 /* Start over if the CPU may have been idle. */ 2307 if (delta_ns > TICK_NSEC) { 2308 cpu->iowait_boost = ONE_EIGHTH_FP; 2309 } else if (cpu->iowait_boost >= ONE_EIGHTH_FP) { 2310 cpu->iowait_boost <<= 1; 2311 if (cpu->iowait_boost > int_tofp(1)) 2312 cpu->iowait_boost = int_tofp(1); 2313 } else { 2314 cpu->iowait_boost = ONE_EIGHTH_FP; 2315 } 2316 } else if (cpu->iowait_boost) { 2317 /* Clear iowait_boost if the CPU may have been idle. */ 2318 if (delta_ns > TICK_NSEC) 2319 cpu->iowait_boost = 0; 2320 else 2321 cpu->iowait_boost >>= 1; 2322 } 2323 cpu->last_update = time; 2324 delta_ns = time - cpu->sample.time; 2325 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL) 2326 return; 2327 2328 if (intel_pstate_sample(cpu, time)) 2329 intel_pstate_adjust_pstate(cpu); 2330 } 2331 2332 static struct pstate_funcs core_funcs = { 2333 .get_max = core_get_max_pstate, 2334 .get_max_physical = core_get_max_pstate_physical, 2335 .get_min = core_get_min_pstate, 2336 .get_turbo = core_get_turbo_pstate, 2337 .get_scaling = core_get_scaling, 2338 .get_val = core_get_val, 2339 }; 2340 2341 static const struct pstate_funcs silvermont_funcs = { 2342 .get_max = atom_get_max_pstate, 2343 .get_max_physical = atom_get_max_pstate, 2344 .get_min = atom_get_min_pstate, 2345 .get_turbo = atom_get_turbo_pstate, 2346 .get_val = atom_get_val, 2347 .get_scaling = silvermont_get_scaling, 2348 .get_vid = atom_get_vid, 2349 }; 2350 2351 static const struct pstate_funcs airmont_funcs = { 2352 .get_max = atom_get_max_pstate, 2353 .get_max_physical = atom_get_max_pstate, 2354 .get_min = atom_get_min_pstate, 2355 .get_turbo = atom_get_turbo_pstate, 2356 .get_val = atom_get_val, 2357 .get_scaling = airmont_get_scaling, 2358 .get_vid = atom_get_vid, 2359 }; 2360 2361 static const struct pstate_funcs knl_funcs = { 2362 .get_max = core_get_max_pstate, 2363 .get_max_physical = core_get_max_pstate_physical, 2364 .get_min = core_get_min_pstate, 2365 .get_turbo = knl_get_turbo_pstate, 2366 .get_aperf_mperf_shift = knl_get_aperf_mperf_shift, 2367 .get_scaling = core_get_scaling, 2368 .get_val = core_get_val, 2369 }; 2370 2371 #define X86_MATCH(model, policy) \ 2372 X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \ 2373 X86_FEATURE_APERFMPERF, &policy) 2374 2375 static const struct x86_cpu_id intel_pstate_cpu_ids[] = { 2376 X86_MATCH(SANDYBRIDGE, core_funcs), 2377 X86_MATCH(SANDYBRIDGE_X, core_funcs), 2378 X86_MATCH(ATOM_SILVERMONT, silvermont_funcs), 2379 X86_MATCH(IVYBRIDGE, core_funcs), 2380 X86_MATCH(HASWELL, core_funcs), 2381 X86_MATCH(BROADWELL, core_funcs), 2382 X86_MATCH(IVYBRIDGE_X, core_funcs), 2383 X86_MATCH(HASWELL_X, core_funcs), 2384 X86_MATCH(HASWELL_L, core_funcs), 2385 X86_MATCH(HASWELL_G, core_funcs), 2386 X86_MATCH(BROADWELL_G, core_funcs), 2387 X86_MATCH(ATOM_AIRMONT, airmont_funcs), 2388 X86_MATCH(SKYLAKE_L, core_funcs), 2389 X86_MATCH(BROADWELL_X, core_funcs), 2390 X86_MATCH(SKYLAKE, core_funcs), 2391 X86_MATCH(BROADWELL_D, core_funcs), 2392 X86_MATCH(XEON_PHI_KNL, knl_funcs), 2393 X86_MATCH(XEON_PHI_KNM, knl_funcs), 2394 X86_MATCH(ATOM_GOLDMONT, core_funcs), 2395 X86_MATCH(ATOM_GOLDMONT_PLUS, core_funcs), 2396 X86_MATCH(SKYLAKE_X, core_funcs), 2397 X86_MATCH(COMETLAKE, core_funcs), 2398 X86_MATCH(ICELAKE_X, core_funcs), 2399 X86_MATCH(TIGERLAKE, core_funcs), 2400 X86_MATCH(SAPPHIRERAPIDS_X, core_funcs), 2401 X86_MATCH(EMERALDRAPIDS_X, core_funcs), 2402 {} 2403 }; 2404 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids); 2405 2406 #ifdef CONFIG_ACPI 2407 static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = { 2408 X86_MATCH(BROADWELL_D, core_funcs), 2409 X86_MATCH(BROADWELL_X, core_funcs), 2410 X86_MATCH(SKYLAKE_X, core_funcs), 2411 X86_MATCH(ICELAKE_X, core_funcs), 2412 X86_MATCH(SAPPHIRERAPIDS_X, core_funcs), 2413 {} 2414 }; 2415 #endif 2416 2417 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = { 2418 X86_MATCH(KABYLAKE, core_funcs), 2419 {} 2420 }; 2421 2422 static int intel_pstate_init_cpu(unsigned int cpunum) 2423 { 2424 struct cpudata *cpu; 2425 2426 cpu = all_cpu_data[cpunum]; 2427 2428 if (!cpu) { 2429 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL); 2430 if (!cpu) 2431 return -ENOMEM; 2432 2433 WRITE_ONCE(all_cpu_data[cpunum], cpu); 2434 2435 cpu->cpu = cpunum; 2436 2437 cpu->epp_default = -EINVAL; 2438 2439 if (hwp_active) { 2440 intel_pstate_hwp_enable(cpu); 2441 2442 if (intel_pstate_acpi_pm_profile_server()) 2443 hwp_boost = true; 2444 } 2445 } else if (hwp_active) { 2446 /* 2447 * Re-enable HWP in case this happens after a resume from ACPI 2448 * S3 if the CPU was offline during the whole system/resume 2449 * cycle. 2450 */ 2451 intel_pstate_hwp_reenable(cpu); 2452 } 2453 2454 cpu->epp_powersave = -EINVAL; 2455 cpu->epp_policy = 0; 2456 2457 intel_pstate_get_cpu_pstates(cpu); 2458 2459 pr_debug("controlling: cpu %d\n", cpunum); 2460 2461 return 0; 2462 } 2463 2464 static void intel_pstate_set_update_util_hook(unsigned int cpu_num) 2465 { 2466 struct cpudata *cpu = all_cpu_data[cpu_num]; 2467 2468 if (hwp_active && !hwp_boost) 2469 return; 2470 2471 if (cpu->update_util_set) 2472 return; 2473 2474 /* Prevent intel_pstate_update_util() from using stale data. */ 2475 cpu->sample.time = 0; 2476 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util, 2477 (hwp_active ? 2478 intel_pstate_update_util_hwp : 2479 intel_pstate_update_util)); 2480 cpu->update_util_set = true; 2481 } 2482 2483 static void intel_pstate_clear_update_util_hook(unsigned int cpu) 2484 { 2485 struct cpudata *cpu_data = all_cpu_data[cpu]; 2486 2487 if (!cpu_data->update_util_set) 2488 return; 2489 2490 cpufreq_remove_update_util_hook(cpu); 2491 cpu_data->update_util_set = false; 2492 synchronize_rcu(); 2493 } 2494 2495 static int intel_pstate_get_max_freq(struct cpudata *cpu) 2496 { 2497 return READ_ONCE(global.no_turbo) ? 2498 cpu->pstate.max_freq : cpu->pstate.turbo_freq; 2499 } 2500 2501 static void intel_pstate_update_perf_limits(struct cpudata *cpu, 2502 unsigned int policy_min, 2503 unsigned int policy_max) 2504 { 2505 int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling; 2506 int32_t max_policy_perf, min_policy_perf; 2507 2508 max_policy_perf = policy_max / perf_ctl_scaling; 2509 if (policy_max == policy_min) { 2510 min_policy_perf = max_policy_perf; 2511 } else { 2512 min_policy_perf = policy_min / perf_ctl_scaling; 2513 min_policy_perf = clamp_t(int32_t, min_policy_perf, 2514 0, max_policy_perf); 2515 } 2516 2517 /* 2518 * HWP needs some special consideration, because HWP_REQUEST uses 2519 * abstract values to represent performance rather than pure ratios. 2520 */ 2521 if (hwp_active && cpu->pstate.scaling != perf_ctl_scaling) { 2522 int freq; 2523 2524 freq = max_policy_perf * perf_ctl_scaling; 2525 max_policy_perf = intel_pstate_freq_to_hwp(cpu, freq); 2526 freq = min_policy_perf * perf_ctl_scaling; 2527 min_policy_perf = intel_pstate_freq_to_hwp(cpu, freq); 2528 } 2529 2530 pr_debug("cpu:%d min_policy_perf:%d max_policy_perf:%d\n", 2531 cpu->cpu, min_policy_perf, max_policy_perf); 2532 2533 /* Normalize user input to [min_perf, max_perf] */ 2534 if (per_cpu_limits) { 2535 cpu->min_perf_ratio = min_policy_perf; 2536 cpu->max_perf_ratio = max_policy_perf; 2537 } else { 2538 int turbo_max = cpu->pstate.turbo_pstate; 2539 int32_t global_min, global_max; 2540 2541 /* Global limits are in percent of the maximum turbo P-state. */ 2542 global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100); 2543 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100); 2544 global_min = clamp_t(int32_t, global_min, 0, global_max); 2545 2546 pr_debug("cpu:%d global_min:%d global_max:%d\n", cpu->cpu, 2547 global_min, global_max); 2548 2549 cpu->min_perf_ratio = max(min_policy_perf, global_min); 2550 cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf); 2551 cpu->max_perf_ratio = min(max_policy_perf, global_max); 2552 cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio); 2553 2554 /* Make sure min_perf <= max_perf */ 2555 cpu->min_perf_ratio = min(cpu->min_perf_ratio, 2556 cpu->max_perf_ratio); 2557 2558 } 2559 pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", cpu->cpu, 2560 cpu->max_perf_ratio, 2561 cpu->min_perf_ratio); 2562 } 2563 2564 static int intel_pstate_set_policy(struct cpufreq_policy *policy) 2565 { 2566 struct cpudata *cpu; 2567 2568 if (!policy->cpuinfo.max_freq) 2569 return -ENODEV; 2570 2571 pr_debug("set_policy cpuinfo.max %u policy->max %u\n", 2572 policy->cpuinfo.max_freq, policy->max); 2573 2574 cpu = all_cpu_data[policy->cpu]; 2575 cpu->policy = policy->policy; 2576 2577 mutex_lock(&intel_pstate_limits_lock); 2578 2579 intel_pstate_update_perf_limits(cpu, policy->min, policy->max); 2580 2581 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) { 2582 int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio); 2583 2584 /* 2585 * NOHZ_FULL CPUs need this as the governor callback may not 2586 * be invoked on them. 2587 */ 2588 intel_pstate_clear_update_util_hook(policy->cpu); 2589 intel_pstate_set_pstate(cpu, pstate); 2590 } else { 2591 intel_pstate_set_update_util_hook(policy->cpu); 2592 } 2593 2594 if (hwp_active) { 2595 /* 2596 * When hwp_boost was active before and dynamically it 2597 * was turned off, in that case we need to clear the 2598 * update util hook. 2599 */ 2600 if (!hwp_boost) 2601 intel_pstate_clear_update_util_hook(policy->cpu); 2602 intel_pstate_hwp_set(policy->cpu); 2603 } 2604 /* 2605 * policy->cur is never updated with the intel_pstate driver, but it 2606 * is used as a stale frequency value. So, keep it within limits. 2607 */ 2608 policy->cur = policy->min; 2609 2610 mutex_unlock(&intel_pstate_limits_lock); 2611 2612 return 0; 2613 } 2614 2615 static void intel_pstate_adjust_policy_max(struct cpudata *cpu, 2616 struct cpufreq_policy_data *policy) 2617 { 2618 if (!hwp_active && 2619 cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate && 2620 policy->max < policy->cpuinfo.max_freq && 2621 policy->max > cpu->pstate.max_freq) { 2622 pr_debug("policy->max > max non turbo frequency\n"); 2623 policy->max = policy->cpuinfo.max_freq; 2624 } 2625 } 2626 2627 static void intel_pstate_verify_cpu_policy(struct cpudata *cpu, 2628 struct cpufreq_policy_data *policy) 2629 { 2630 int max_freq; 2631 2632 if (hwp_active) { 2633 intel_pstate_get_hwp_cap(cpu); 2634 max_freq = READ_ONCE(global.no_turbo) ? 2635 cpu->pstate.max_freq : cpu->pstate.turbo_freq; 2636 } else { 2637 max_freq = intel_pstate_get_max_freq(cpu); 2638 } 2639 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, max_freq); 2640 2641 intel_pstate_adjust_policy_max(cpu, policy); 2642 } 2643 2644 static int intel_pstate_verify_policy(struct cpufreq_policy_data *policy) 2645 { 2646 intel_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy); 2647 2648 return 0; 2649 } 2650 2651 static int intel_cpufreq_cpu_offline(struct cpufreq_policy *policy) 2652 { 2653 struct cpudata *cpu = all_cpu_data[policy->cpu]; 2654 2655 pr_debug("CPU %d going offline\n", cpu->cpu); 2656 2657 if (cpu->suspended) 2658 return 0; 2659 2660 /* 2661 * If the CPU is an SMT thread and it goes offline with the performance 2662 * settings different from the minimum, it will prevent its sibling 2663 * from getting to lower performance levels, so force the minimum 2664 * performance on CPU offline to prevent that from happening. 2665 */ 2666 if (hwp_active) 2667 intel_pstate_hwp_offline(cpu); 2668 else 2669 intel_pstate_set_min_pstate(cpu); 2670 2671 intel_pstate_exit_perf_limits(policy); 2672 2673 return 0; 2674 } 2675 2676 static int intel_pstate_cpu_online(struct cpufreq_policy *policy) 2677 { 2678 struct cpudata *cpu = all_cpu_data[policy->cpu]; 2679 2680 pr_debug("CPU %d going online\n", cpu->cpu); 2681 2682 intel_pstate_init_acpi_perf_limits(policy); 2683 2684 if (hwp_active) { 2685 /* 2686 * Re-enable HWP and clear the "suspended" flag to let "resume" 2687 * know that it need not do that. 2688 */ 2689 intel_pstate_hwp_reenable(cpu); 2690 cpu->suspended = false; 2691 } 2692 2693 return 0; 2694 } 2695 2696 static int intel_pstate_cpu_offline(struct cpufreq_policy *policy) 2697 { 2698 intel_pstate_clear_update_util_hook(policy->cpu); 2699 2700 return intel_cpufreq_cpu_offline(policy); 2701 } 2702 2703 static int intel_pstate_cpu_exit(struct cpufreq_policy *policy) 2704 { 2705 pr_debug("CPU %d exiting\n", policy->cpu); 2706 2707 policy->fast_switch_possible = false; 2708 2709 return 0; 2710 } 2711 2712 static int __intel_pstate_cpu_init(struct cpufreq_policy *policy) 2713 { 2714 struct cpudata *cpu; 2715 int rc; 2716 2717 rc = intel_pstate_init_cpu(policy->cpu); 2718 if (rc) 2719 return rc; 2720 2721 cpu = all_cpu_data[policy->cpu]; 2722 2723 cpu->max_perf_ratio = 0xFF; 2724 cpu->min_perf_ratio = 0; 2725 2726 /* cpuinfo and default policy values */ 2727 policy->cpuinfo.min_freq = cpu->pstate.min_freq; 2728 policy->cpuinfo.max_freq = READ_ONCE(global.no_turbo) ? 2729 cpu->pstate.max_freq : cpu->pstate.turbo_freq; 2730 2731 policy->min = policy->cpuinfo.min_freq; 2732 policy->max = policy->cpuinfo.max_freq; 2733 2734 intel_pstate_init_acpi_perf_limits(policy); 2735 2736 policy->fast_switch_possible = true; 2737 2738 return 0; 2739 } 2740 2741 static int intel_pstate_cpu_init(struct cpufreq_policy *policy) 2742 { 2743 int ret = __intel_pstate_cpu_init(policy); 2744 2745 if (ret) 2746 return ret; 2747 2748 /* 2749 * Set the policy to powersave to provide a valid fallback value in case 2750 * the default cpufreq governor is neither powersave nor performance. 2751 */ 2752 policy->policy = CPUFREQ_POLICY_POWERSAVE; 2753 2754 if (hwp_active) { 2755 struct cpudata *cpu = all_cpu_data[policy->cpu]; 2756 2757 cpu->epp_cached = intel_pstate_get_epp(cpu, 0); 2758 } 2759 2760 return 0; 2761 } 2762 2763 static struct cpufreq_driver intel_pstate = { 2764 .flags = CPUFREQ_CONST_LOOPS, 2765 .verify = intel_pstate_verify_policy, 2766 .setpolicy = intel_pstate_set_policy, 2767 .suspend = intel_pstate_suspend, 2768 .resume = intel_pstate_resume, 2769 .init = intel_pstate_cpu_init, 2770 .exit = intel_pstate_cpu_exit, 2771 .offline = intel_pstate_cpu_offline, 2772 .online = intel_pstate_cpu_online, 2773 .update_limits = intel_pstate_update_limits, 2774 .name = "intel_pstate", 2775 }; 2776 2777 static int intel_cpufreq_verify_policy(struct cpufreq_policy_data *policy) 2778 { 2779 struct cpudata *cpu = all_cpu_data[policy->cpu]; 2780 2781 intel_pstate_verify_cpu_policy(cpu, policy); 2782 intel_pstate_update_perf_limits(cpu, policy->min, policy->max); 2783 2784 return 0; 2785 } 2786 2787 /* Use of trace in passive mode: 2788 * 2789 * In passive mode the trace core_busy field (also known as the 2790 * performance field, and lablelled as such on the graphs; also known as 2791 * core_avg_perf) is not needed and so is re-assigned to indicate if the 2792 * driver call was via the normal or fast switch path. Various graphs 2793 * output from the intel_pstate_tracer.py utility that include core_busy 2794 * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%, 2795 * so we use 10 to indicate the normal path through the driver, and 2796 * 90 to indicate the fast switch path through the driver. 2797 * The scaled_busy field is not used, and is set to 0. 2798 */ 2799 2800 #define INTEL_PSTATE_TRACE_TARGET 10 2801 #define INTEL_PSTATE_TRACE_FAST_SWITCH 90 2802 2803 static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate) 2804 { 2805 struct sample *sample; 2806 2807 if (!trace_pstate_sample_enabled()) 2808 return; 2809 2810 if (!intel_pstate_sample(cpu, ktime_get())) 2811 return; 2812 2813 sample = &cpu->sample; 2814 trace_pstate_sample(trace_type, 2815 0, 2816 old_pstate, 2817 cpu->pstate.current_pstate, 2818 sample->mperf, 2819 sample->aperf, 2820 sample->tsc, 2821 get_avg_frequency(cpu), 2822 fp_toint(cpu->iowait_boost * 100)); 2823 } 2824 2825 static void intel_cpufreq_hwp_update(struct cpudata *cpu, u32 min, u32 max, 2826 u32 desired, bool fast_switch) 2827 { 2828 u64 prev = READ_ONCE(cpu->hwp_req_cached), value = prev; 2829 2830 value &= ~HWP_MIN_PERF(~0L); 2831 value |= HWP_MIN_PERF(min); 2832 2833 value &= ~HWP_MAX_PERF(~0L); 2834 value |= HWP_MAX_PERF(max); 2835 2836 value &= ~HWP_DESIRED_PERF(~0L); 2837 value |= HWP_DESIRED_PERF(desired); 2838 2839 if (value == prev) 2840 return; 2841 2842 WRITE_ONCE(cpu->hwp_req_cached, value); 2843 if (fast_switch) 2844 wrmsrl(MSR_HWP_REQUEST, value); 2845 else 2846 wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value); 2847 } 2848 2849 static void intel_cpufreq_perf_ctl_update(struct cpudata *cpu, 2850 u32 target_pstate, bool fast_switch) 2851 { 2852 if (fast_switch) 2853 wrmsrl(MSR_IA32_PERF_CTL, 2854 pstate_funcs.get_val(cpu, target_pstate)); 2855 else 2856 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL, 2857 pstate_funcs.get_val(cpu, target_pstate)); 2858 } 2859 2860 static int intel_cpufreq_update_pstate(struct cpufreq_policy *policy, 2861 int target_pstate, bool fast_switch) 2862 { 2863 struct cpudata *cpu = all_cpu_data[policy->cpu]; 2864 int old_pstate = cpu->pstate.current_pstate; 2865 2866 target_pstate = intel_pstate_prepare_request(cpu, target_pstate); 2867 if (hwp_active) { 2868 int max_pstate = policy->strict_target ? 2869 target_pstate : cpu->max_perf_ratio; 2870 2871 intel_cpufreq_hwp_update(cpu, target_pstate, max_pstate, 0, 2872 fast_switch); 2873 } else if (target_pstate != old_pstate) { 2874 intel_cpufreq_perf_ctl_update(cpu, target_pstate, fast_switch); 2875 } 2876 2877 cpu->pstate.current_pstate = target_pstate; 2878 2879 intel_cpufreq_trace(cpu, fast_switch ? INTEL_PSTATE_TRACE_FAST_SWITCH : 2880 INTEL_PSTATE_TRACE_TARGET, old_pstate); 2881 2882 return target_pstate; 2883 } 2884 2885 static int intel_cpufreq_target(struct cpufreq_policy *policy, 2886 unsigned int target_freq, 2887 unsigned int relation) 2888 { 2889 struct cpudata *cpu = all_cpu_data[policy->cpu]; 2890 struct cpufreq_freqs freqs; 2891 int target_pstate; 2892 2893 freqs.old = policy->cur; 2894 freqs.new = target_freq; 2895 2896 cpufreq_freq_transition_begin(policy, &freqs); 2897 2898 target_pstate = intel_pstate_freq_to_hwp_rel(cpu, freqs.new, relation); 2899 target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, false); 2900 2901 freqs.new = target_pstate * cpu->pstate.scaling; 2902 2903 cpufreq_freq_transition_end(policy, &freqs, false); 2904 2905 return 0; 2906 } 2907 2908 static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy, 2909 unsigned int target_freq) 2910 { 2911 struct cpudata *cpu = all_cpu_data[policy->cpu]; 2912 int target_pstate; 2913 2914 target_pstate = intel_pstate_freq_to_hwp(cpu, target_freq); 2915 2916 target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, true); 2917 2918 return target_pstate * cpu->pstate.scaling; 2919 } 2920 2921 static void intel_cpufreq_adjust_perf(unsigned int cpunum, 2922 unsigned long min_perf, 2923 unsigned long target_perf, 2924 unsigned long capacity) 2925 { 2926 struct cpudata *cpu = all_cpu_data[cpunum]; 2927 u64 hwp_cap = READ_ONCE(cpu->hwp_cap_cached); 2928 int old_pstate = cpu->pstate.current_pstate; 2929 int cap_pstate, min_pstate, max_pstate, target_pstate; 2930 2931 cap_pstate = READ_ONCE(global.no_turbo) ? 2932 HWP_GUARANTEED_PERF(hwp_cap) : 2933 HWP_HIGHEST_PERF(hwp_cap); 2934 2935 /* Optimization: Avoid unnecessary divisions. */ 2936 2937 target_pstate = cap_pstate; 2938 if (target_perf < capacity) 2939 target_pstate = DIV_ROUND_UP(cap_pstate * target_perf, capacity); 2940 2941 min_pstate = cap_pstate; 2942 if (min_perf < capacity) 2943 min_pstate = DIV_ROUND_UP(cap_pstate * min_perf, capacity); 2944 2945 if (min_pstate < cpu->pstate.min_pstate) 2946 min_pstate = cpu->pstate.min_pstate; 2947 2948 if (min_pstate < cpu->min_perf_ratio) 2949 min_pstate = cpu->min_perf_ratio; 2950 2951 if (min_pstate > cpu->max_perf_ratio) 2952 min_pstate = cpu->max_perf_ratio; 2953 2954 max_pstate = min(cap_pstate, cpu->max_perf_ratio); 2955 if (max_pstate < min_pstate) 2956 max_pstate = min_pstate; 2957 2958 target_pstate = clamp_t(int, target_pstate, min_pstate, max_pstate); 2959 2960 intel_cpufreq_hwp_update(cpu, min_pstate, max_pstate, target_pstate, true); 2961 2962 cpu->pstate.current_pstate = target_pstate; 2963 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate); 2964 } 2965 2966 static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy) 2967 { 2968 struct freq_qos_request *req; 2969 struct cpudata *cpu; 2970 struct device *dev; 2971 int ret, freq; 2972 2973 dev = get_cpu_device(policy->cpu); 2974 if (!dev) 2975 return -ENODEV; 2976 2977 ret = __intel_pstate_cpu_init(policy); 2978 if (ret) 2979 return ret; 2980 2981 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY; 2982 /* This reflects the intel_pstate_get_cpu_pstates() setting. */ 2983 policy->cur = policy->cpuinfo.min_freq; 2984 2985 req = kcalloc(2, sizeof(*req), GFP_KERNEL); 2986 if (!req) { 2987 ret = -ENOMEM; 2988 goto pstate_exit; 2989 } 2990 2991 cpu = all_cpu_data[policy->cpu]; 2992 2993 if (hwp_active) { 2994 u64 value; 2995 2996 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY_HWP; 2997 2998 intel_pstate_get_hwp_cap(cpu); 2999 3000 rdmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, &value); 3001 WRITE_ONCE(cpu->hwp_req_cached, value); 3002 3003 cpu->epp_cached = intel_pstate_get_epp(cpu, value); 3004 } else { 3005 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY; 3006 } 3007 3008 freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.min_perf_pct, 100); 3009 3010 ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN, 3011 freq); 3012 if (ret < 0) { 3013 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret); 3014 goto free_req; 3015 } 3016 3017 freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.max_perf_pct, 100); 3018 3019 ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX, 3020 freq); 3021 if (ret < 0) { 3022 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret); 3023 goto remove_min_req; 3024 } 3025 3026 policy->driver_data = req; 3027 3028 return 0; 3029 3030 remove_min_req: 3031 freq_qos_remove_request(req); 3032 free_req: 3033 kfree(req); 3034 pstate_exit: 3035 intel_pstate_exit_perf_limits(policy); 3036 3037 return ret; 3038 } 3039 3040 static int intel_cpufreq_cpu_exit(struct cpufreq_policy *policy) 3041 { 3042 struct freq_qos_request *req; 3043 3044 req = policy->driver_data; 3045 3046 freq_qos_remove_request(req + 1); 3047 freq_qos_remove_request(req); 3048 kfree(req); 3049 3050 return intel_pstate_cpu_exit(policy); 3051 } 3052 3053 static int intel_cpufreq_suspend(struct cpufreq_policy *policy) 3054 { 3055 intel_pstate_suspend(policy); 3056 3057 if (hwp_active) { 3058 struct cpudata *cpu = all_cpu_data[policy->cpu]; 3059 u64 value = READ_ONCE(cpu->hwp_req_cached); 3060 3061 /* 3062 * Clear the desired perf field in MSR_HWP_REQUEST in case 3063 * intel_cpufreq_adjust_perf() is in use and the last value 3064 * written by it may not be suitable. 3065 */ 3066 value &= ~HWP_DESIRED_PERF(~0L); 3067 wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value); 3068 WRITE_ONCE(cpu->hwp_req_cached, value); 3069 } 3070 3071 return 0; 3072 } 3073 3074 static struct cpufreq_driver intel_cpufreq = { 3075 .flags = CPUFREQ_CONST_LOOPS, 3076 .verify = intel_cpufreq_verify_policy, 3077 .target = intel_cpufreq_target, 3078 .fast_switch = intel_cpufreq_fast_switch, 3079 .init = intel_cpufreq_cpu_init, 3080 .exit = intel_cpufreq_cpu_exit, 3081 .offline = intel_cpufreq_cpu_offline, 3082 .online = intel_pstate_cpu_online, 3083 .suspend = intel_cpufreq_suspend, 3084 .resume = intel_pstate_resume, 3085 .update_limits = intel_pstate_update_limits, 3086 .name = "intel_cpufreq", 3087 }; 3088 3089 static struct cpufreq_driver *default_driver; 3090 3091 static void intel_pstate_driver_cleanup(void) 3092 { 3093 unsigned int cpu; 3094 3095 cpus_read_lock(); 3096 for_each_online_cpu(cpu) { 3097 if (all_cpu_data[cpu]) { 3098 if (intel_pstate_driver == &intel_pstate) 3099 intel_pstate_clear_update_util_hook(cpu); 3100 3101 kfree(all_cpu_data[cpu]); 3102 WRITE_ONCE(all_cpu_data[cpu], NULL); 3103 } 3104 } 3105 cpus_read_unlock(); 3106 3107 intel_pstate_driver = NULL; 3108 } 3109 3110 static int intel_pstate_register_driver(struct cpufreq_driver *driver) 3111 { 3112 int ret; 3113 3114 if (driver == &intel_pstate) 3115 intel_pstate_sysfs_expose_hwp_dynamic_boost(); 3116 3117 memset(&global, 0, sizeof(global)); 3118 global.max_perf_pct = 100; 3119 global.turbo_disabled = turbo_is_disabled(); 3120 global.no_turbo = global.turbo_disabled; 3121 3122 arch_set_max_freq_ratio(global.turbo_disabled); 3123 3124 intel_pstate_driver = driver; 3125 ret = cpufreq_register_driver(intel_pstate_driver); 3126 if (ret) { 3127 intel_pstate_driver_cleanup(); 3128 return ret; 3129 } 3130 3131 global.min_perf_pct = min_perf_pct_min(); 3132 3133 return 0; 3134 } 3135 3136 static ssize_t intel_pstate_show_status(char *buf) 3137 { 3138 if (!intel_pstate_driver) 3139 return sprintf(buf, "off\n"); 3140 3141 return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ? 3142 "active" : "passive"); 3143 } 3144 3145 static int intel_pstate_update_status(const char *buf, size_t size) 3146 { 3147 if (size == 3 && !strncmp(buf, "off", size)) { 3148 if (!intel_pstate_driver) 3149 return -EINVAL; 3150 3151 if (hwp_active) 3152 return -EBUSY; 3153 3154 cpufreq_unregister_driver(intel_pstate_driver); 3155 intel_pstate_driver_cleanup(); 3156 return 0; 3157 } 3158 3159 if (size == 6 && !strncmp(buf, "active", size)) { 3160 if (intel_pstate_driver) { 3161 if (intel_pstate_driver == &intel_pstate) 3162 return 0; 3163 3164 cpufreq_unregister_driver(intel_pstate_driver); 3165 } 3166 3167 return intel_pstate_register_driver(&intel_pstate); 3168 } 3169 3170 if (size == 7 && !strncmp(buf, "passive", size)) { 3171 if (intel_pstate_driver) { 3172 if (intel_pstate_driver == &intel_cpufreq) 3173 return 0; 3174 3175 cpufreq_unregister_driver(intel_pstate_driver); 3176 intel_pstate_sysfs_hide_hwp_dynamic_boost(); 3177 } 3178 3179 return intel_pstate_register_driver(&intel_cpufreq); 3180 } 3181 3182 return -EINVAL; 3183 } 3184 3185 static int no_load __initdata; 3186 static int no_hwp __initdata; 3187 static int hwp_only __initdata; 3188 static unsigned int force_load __initdata; 3189 3190 static int __init intel_pstate_msrs_not_valid(void) 3191 { 3192 if (!pstate_funcs.get_max(0) || 3193 !pstate_funcs.get_min(0) || 3194 !pstate_funcs.get_turbo(0)) 3195 return -ENODEV; 3196 3197 return 0; 3198 } 3199 3200 static void __init copy_cpu_funcs(struct pstate_funcs *funcs) 3201 { 3202 pstate_funcs.get_max = funcs->get_max; 3203 pstate_funcs.get_max_physical = funcs->get_max_physical; 3204 pstate_funcs.get_min = funcs->get_min; 3205 pstate_funcs.get_turbo = funcs->get_turbo; 3206 pstate_funcs.get_scaling = funcs->get_scaling; 3207 pstate_funcs.get_val = funcs->get_val; 3208 pstate_funcs.get_vid = funcs->get_vid; 3209 pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift; 3210 } 3211 3212 #ifdef CONFIG_ACPI 3213 3214 static bool __init intel_pstate_no_acpi_pss(void) 3215 { 3216 int i; 3217 3218 for_each_possible_cpu(i) { 3219 acpi_status status; 3220 union acpi_object *pss; 3221 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 3222 struct acpi_processor *pr = per_cpu(processors, i); 3223 3224 if (!pr) 3225 continue; 3226 3227 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer); 3228 if (ACPI_FAILURE(status)) 3229 continue; 3230 3231 pss = buffer.pointer; 3232 if (pss && pss->type == ACPI_TYPE_PACKAGE) { 3233 kfree(pss); 3234 return false; 3235 } 3236 3237 kfree(pss); 3238 } 3239 3240 pr_debug("ACPI _PSS not found\n"); 3241 return true; 3242 } 3243 3244 static bool __init intel_pstate_no_acpi_pcch(void) 3245 { 3246 acpi_status status; 3247 acpi_handle handle; 3248 3249 status = acpi_get_handle(NULL, "\\_SB", &handle); 3250 if (ACPI_FAILURE(status)) 3251 goto not_found; 3252 3253 if (acpi_has_method(handle, "PCCH")) 3254 return false; 3255 3256 not_found: 3257 pr_debug("ACPI PCCH not found\n"); 3258 return true; 3259 } 3260 3261 static bool __init intel_pstate_has_acpi_ppc(void) 3262 { 3263 int i; 3264 3265 for_each_possible_cpu(i) { 3266 struct acpi_processor *pr = per_cpu(processors, i); 3267 3268 if (!pr) 3269 continue; 3270 if (acpi_has_method(pr->handle, "_PPC")) 3271 return true; 3272 } 3273 pr_debug("ACPI _PPC not found\n"); 3274 return false; 3275 } 3276 3277 enum { 3278 PSS, 3279 PPC, 3280 }; 3281 3282 /* Hardware vendor-specific info that has its own power management modes */ 3283 static struct acpi_platform_list plat_info[] __initdata = { 3284 {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, NULL, PSS}, 3285 {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3286 {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3287 {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3288 {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3289 {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3290 {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3291 {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3292 {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3293 {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3294 {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3295 {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3296 {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3297 {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3298 {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC}, 3299 { } /* End */ 3300 }; 3301 3302 #define BITMASK_OOB (BIT(8) | BIT(18)) 3303 3304 static bool __init intel_pstate_platform_pwr_mgmt_exists(void) 3305 { 3306 const struct x86_cpu_id *id; 3307 u64 misc_pwr; 3308 int idx; 3309 3310 id = x86_match_cpu(intel_pstate_cpu_oob_ids); 3311 if (id) { 3312 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr); 3313 if (misc_pwr & BITMASK_OOB) { 3314 pr_debug("Bit 8 or 18 in the MISC_PWR_MGMT MSR set\n"); 3315 pr_debug("P states are controlled in Out of Band mode by the firmware/hardware\n"); 3316 return true; 3317 } 3318 } 3319 3320 idx = acpi_match_platform_list(plat_info); 3321 if (idx < 0) 3322 return false; 3323 3324 switch (plat_info[idx].data) { 3325 case PSS: 3326 if (!intel_pstate_no_acpi_pss()) 3327 return false; 3328 3329 return intel_pstate_no_acpi_pcch(); 3330 case PPC: 3331 return intel_pstate_has_acpi_ppc() && !force_load; 3332 } 3333 3334 return false; 3335 } 3336 3337 static void intel_pstate_request_control_from_smm(void) 3338 { 3339 /* 3340 * It may be unsafe to request P-states control from SMM if _PPC support 3341 * has not been enabled. 3342 */ 3343 if (acpi_ppc) 3344 acpi_processor_pstate_control(); 3345 } 3346 #else /* CONFIG_ACPI not enabled */ 3347 static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; } 3348 static inline bool intel_pstate_has_acpi_ppc(void) { return false; } 3349 static inline void intel_pstate_request_control_from_smm(void) {} 3350 #endif /* CONFIG_ACPI */ 3351 3352 #define INTEL_PSTATE_HWP_BROADWELL 0x01 3353 3354 #define X86_MATCH_HWP(model, hwp_mode) \ 3355 X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \ 3356 X86_FEATURE_HWP, hwp_mode) 3357 3358 static const struct x86_cpu_id hwp_support_ids[] __initconst = { 3359 X86_MATCH_HWP(BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL), 3360 X86_MATCH_HWP(BROADWELL_D, INTEL_PSTATE_HWP_BROADWELL), 3361 X86_MATCH_HWP(ANY, 0), 3362 {} 3363 }; 3364 3365 static bool intel_pstate_hwp_is_enabled(void) 3366 { 3367 u64 value; 3368 3369 rdmsrl(MSR_PM_ENABLE, value); 3370 return !!(value & 0x1); 3371 } 3372 3373 #define POWERSAVE_MASK GENMASK(7, 0) 3374 #define BALANCE_POWER_MASK GENMASK(15, 8) 3375 #define BALANCE_PERFORMANCE_MASK GENMASK(23, 16) 3376 #define PERFORMANCE_MASK GENMASK(31, 24) 3377 3378 #define HWP_SET_EPP_VALUES(powersave, balance_power, balance_perf, performance) \ 3379 (FIELD_PREP_CONST(POWERSAVE_MASK, powersave) |\ 3380 FIELD_PREP_CONST(BALANCE_POWER_MASK, balance_power) |\ 3381 FIELD_PREP_CONST(BALANCE_PERFORMANCE_MASK, balance_perf) |\ 3382 FIELD_PREP_CONST(PERFORMANCE_MASK, performance)) 3383 3384 #define HWP_SET_DEF_BALANCE_PERF_EPP(balance_perf) \ 3385 (HWP_SET_EPP_VALUES(HWP_EPP_POWERSAVE, HWP_EPP_BALANCE_POWERSAVE,\ 3386 balance_perf, HWP_EPP_PERFORMANCE)) 3387 3388 static const struct x86_cpu_id intel_epp_default[] = { 3389 /* 3390 * Set EPP value as 102, this is the max suggested EPP 3391 * which can result in one core turbo frequency for 3392 * AlderLake Mobile CPUs. 3393 */ 3394 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_L, HWP_SET_DEF_BALANCE_PERF_EPP(102)), 3395 X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, HWP_SET_DEF_BALANCE_PERF_EPP(32)), 3396 X86_MATCH_INTEL_FAM6_MODEL(METEORLAKE_L, HWP_SET_EPP_VALUES(HWP_EPP_POWERSAVE, 3397 HWP_EPP_BALANCE_POWERSAVE, 115, 16)), 3398 {} 3399 }; 3400 3401 static const struct x86_cpu_id intel_hybrid_scaling_factor[] = { 3402 X86_MATCH_INTEL_FAM6_MODEL(METEORLAKE_L, HYBRID_SCALING_FACTOR_MTL), 3403 {} 3404 }; 3405 3406 static int __init intel_pstate_init(void) 3407 { 3408 static struct cpudata **_all_cpu_data; 3409 const struct x86_cpu_id *id; 3410 int rc; 3411 3412 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) 3413 return -ENODEV; 3414 3415 id = x86_match_cpu(hwp_support_ids); 3416 if (id) { 3417 hwp_forced = intel_pstate_hwp_is_enabled(); 3418 3419 if (hwp_forced) 3420 pr_info("HWP enabled by BIOS\n"); 3421 else if (no_load) 3422 return -ENODEV; 3423 3424 copy_cpu_funcs(&core_funcs); 3425 /* 3426 * Avoid enabling HWP for processors without EPP support, 3427 * because that means incomplete HWP implementation which is a 3428 * corner case and supporting it is generally problematic. 3429 * 3430 * If HWP is enabled already, though, there is no choice but to 3431 * deal with it. 3432 */ 3433 if ((!no_hwp && boot_cpu_has(X86_FEATURE_HWP_EPP)) || hwp_forced) { 3434 hwp_active = true; 3435 hwp_mode_bdw = id->driver_data; 3436 intel_pstate.attr = hwp_cpufreq_attrs; 3437 intel_cpufreq.attr = hwp_cpufreq_attrs; 3438 intel_cpufreq.flags |= CPUFREQ_NEED_UPDATE_LIMITS; 3439 intel_cpufreq.adjust_perf = intel_cpufreq_adjust_perf; 3440 if (!default_driver) 3441 default_driver = &intel_pstate; 3442 3443 pstate_funcs.get_cpu_scaling = hwp_get_cpu_scaling; 3444 3445 goto hwp_cpu_matched; 3446 } 3447 pr_info("HWP not enabled\n"); 3448 } else { 3449 if (no_load) 3450 return -ENODEV; 3451 3452 id = x86_match_cpu(intel_pstate_cpu_ids); 3453 if (!id) { 3454 pr_info("CPU model not supported\n"); 3455 return -ENODEV; 3456 } 3457 3458 copy_cpu_funcs((struct pstate_funcs *)id->driver_data); 3459 } 3460 3461 if (intel_pstate_msrs_not_valid()) { 3462 pr_info("Invalid MSRs\n"); 3463 return -ENODEV; 3464 } 3465 /* Without HWP start in the passive mode. */ 3466 if (!default_driver) 3467 default_driver = &intel_cpufreq; 3468 3469 hwp_cpu_matched: 3470 /* 3471 * The Intel pstate driver will be ignored if the platform 3472 * firmware has its own power management modes. 3473 */ 3474 if (intel_pstate_platform_pwr_mgmt_exists()) { 3475 pr_info("P-states controlled by the platform\n"); 3476 return -ENODEV; 3477 } 3478 3479 if (!hwp_active && hwp_only) 3480 return -ENOTSUPP; 3481 3482 pr_info("Intel P-state driver initializing\n"); 3483 3484 _all_cpu_data = vzalloc(array_size(sizeof(void *), num_possible_cpus())); 3485 if (!_all_cpu_data) 3486 return -ENOMEM; 3487 3488 WRITE_ONCE(all_cpu_data, _all_cpu_data); 3489 3490 intel_pstate_request_control_from_smm(); 3491 3492 intel_pstate_sysfs_expose_params(); 3493 3494 if (hwp_active) { 3495 const struct x86_cpu_id *id = x86_match_cpu(intel_epp_default); 3496 const struct x86_cpu_id *hybrid_id = x86_match_cpu(intel_hybrid_scaling_factor); 3497 3498 if (id) { 3499 epp_values[EPP_INDEX_POWERSAVE] = 3500 FIELD_GET(POWERSAVE_MASK, id->driver_data); 3501 epp_values[EPP_INDEX_BALANCE_POWERSAVE] = 3502 FIELD_GET(BALANCE_POWER_MASK, id->driver_data); 3503 epp_values[EPP_INDEX_BALANCE_PERFORMANCE] = 3504 FIELD_GET(BALANCE_PERFORMANCE_MASK, id->driver_data); 3505 epp_values[EPP_INDEX_PERFORMANCE] = 3506 FIELD_GET(PERFORMANCE_MASK, id->driver_data); 3507 pr_debug("Updated EPPs powersave:%x balanced power:%x balanced perf:%x performance:%x\n", 3508 epp_values[EPP_INDEX_POWERSAVE], 3509 epp_values[EPP_INDEX_BALANCE_POWERSAVE], 3510 epp_values[EPP_INDEX_BALANCE_PERFORMANCE], 3511 epp_values[EPP_INDEX_PERFORMANCE]); 3512 } 3513 3514 if (hybrid_id) { 3515 hybrid_scaling_factor = hybrid_id->driver_data; 3516 pr_debug("hybrid scaling factor: %d\n", hybrid_scaling_factor); 3517 } 3518 3519 } 3520 3521 mutex_lock(&intel_pstate_driver_lock); 3522 rc = intel_pstate_register_driver(default_driver); 3523 mutex_unlock(&intel_pstate_driver_lock); 3524 if (rc) { 3525 intel_pstate_sysfs_remove(); 3526 return rc; 3527 } 3528 3529 if (hwp_active) { 3530 const struct x86_cpu_id *id; 3531 3532 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids); 3533 if (id) { 3534 set_power_ctl_ee_state(false); 3535 pr_info("Disabling energy efficiency optimization\n"); 3536 } 3537 3538 pr_info("HWP enabled\n"); 3539 } else if (boot_cpu_has(X86_FEATURE_HYBRID_CPU)) { 3540 pr_warn("Problematic setup: Hybrid processor with disabled HWP\n"); 3541 } 3542 3543 return 0; 3544 } 3545 device_initcall(intel_pstate_init); 3546 3547 static int __init intel_pstate_setup(char *str) 3548 { 3549 if (!str) 3550 return -EINVAL; 3551 3552 if (!strcmp(str, "disable")) 3553 no_load = 1; 3554 else if (!strcmp(str, "active")) 3555 default_driver = &intel_pstate; 3556 else if (!strcmp(str, "passive")) 3557 default_driver = &intel_cpufreq; 3558 3559 if (!strcmp(str, "no_hwp")) 3560 no_hwp = 1; 3561 3562 if (!strcmp(str, "force")) 3563 force_load = 1; 3564 if (!strcmp(str, "hwp_only")) 3565 hwp_only = 1; 3566 if (!strcmp(str, "per_cpu_perf_limits")) 3567 per_cpu_limits = true; 3568 3569 #ifdef CONFIG_ACPI 3570 if (!strcmp(str, "support_acpi_ppc")) 3571 acpi_ppc = true; 3572 #endif 3573 3574 return 0; 3575 } 3576 early_param("intel_pstate", intel_pstate_setup); 3577 3578 MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>"); 3579 MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors"); 3580