1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pstate.c: Native P state management for Intel processors
5 * (C) Copyright 2012 Intel Corporation
6 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
25 #include <linux/acpi.h>
26 #include <linux/vmalloc.h>
27 #include <linux/pm_qos.h>
28 #include <trace/events/power.h>
31 #include <asm/div64.h>
33 #include <asm/cpu_device_id.h>
34 #include <asm/cpufeature.h>
35 #include <asm/intel-family.h>
36 #include "../drivers/thermal/intel/thermal_interrupt.h"
38 #define INTEL_PSTATE_SAMPLING_INTERVAL (10 * NSEC_PER_MSEC)
40 #define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
41 #define INTEL_CPUFREQ_TRANSITION_DELAY_HWP 5000
42 #define INTEL_CPUFREQ_TRANSITION_DELAY 500
45 #include <acpi/processor.h>
46 #include <acpi/cppc_acpi.h>
50 #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
51 #define fp_toint(X) ((X) >> FRAC_BITS)
53 #define ONE_EIGHTH_FP ((int64_t)1 << (FRAC_BITS - 3))
56 #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
57 #define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
58 #define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
60 static inline int32_t mul_fp(int32_t x, int32_t y)
62 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
65 static inline int32_t div_fp(s64 x, s64 y)
67 return div64_s64((int64_t)x << FRAC_BITS, y);
70 static inline int ceiling_fp(int32_t x)
75 mask = (1 << FRAC_BITS) - 1;
81 static inline u64 mul_ext_fp(u64 x, u64 y)
83 return (x * y) >> EXT_FRAC_BITS;
86 static inline u64 div_ext_fp(u64 x, u64 y)
88 return div64_u64(x << EXT_FRAC_BITS, y);
92 * struct sample - Store performance sample
93 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
94 * performance during last sample period
95 * @busy_scaled: Scaled busy value which is used to calculate next
96 * P state. This can be different than core_avg_perf
97 * to account for cpu idle period
98 * @aperf: Difference of actual performance frequency clock count
99 * read from APERF MSR between last and current sample
100 * @mperf: Difference of maximum performance frequency clock count
101 * read from MPERF MSR between last and current sample
102 * @tsc: Difference of time stamp counter between last and
104 * @time: Current time from scheduler
106 * This structure is used in the cpudata structure to store performance sample
107 * data for choosing next P State.
110 int32_t core_avg_perf;
119 * struct pstate_data - Store P state data
120 * @current_pstate: Current requested P state
121 * @min_pstate: Min P state possible for this platform
122 * @max_pstate: Max P state possible for this platform
123 * @max_pstate_physical:This is physical Max P state for a processor
124 * This can be higher than the max_pstate which can
125 * be limited by platform thermal design power limits
126 * @perf_ctl_scaling: PERF_CTL P-state to frequency scaling factor
127 * @scaling: Scaling factor between performance and frequency
128 * @turbo_pstate: Max Turbo P state possible for this platform
129 * @min_freq: @min_pstate frequency in cpufreq units
130 * @max_freq: @max_pstate frequency in cpufreq units
131 * @turbo_freq: @turbo_pstate frequency in cpufreq units
133 * Stores the per cpu model P state limits and current P state.
139 int max_pstate_physical;
140 int perf_ctl_scaling;
143 unsigned int min_freq;
144 unsigned int max_freq;
145 unsigned int turbo_freq;
149 * struct vid_data - Stores voltage information data
150 * @min: VID data for this platform corresponding to
152 * @max: VID data corresponding to the highest P State.
153 * @turbo: VID data for turbo P state
154 * @ratio: Ratio of (vid max - vid min) /
155 * (max P state - Min P State)
157 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
158 * This data is used in Atom platforms, where in addition to target P state,
159 * the voltage data needs to be specified to select next P State.
169 * struct global_params - Global parameters, mostly tunable via sysfs.
170 * @no_turbo: Whether or not to use turbo P-states.
171 * @turbo_disabled: Whether or not turbo P-states are available at all,
172 * based on the MSR_IA32_MISC_ENABLE value and whether or
173 * not the maximum reported turbo P-state is different from
174 * the maximum reported non-turbo one.
175 * @turbo_disabled_mf: The @turbo_disabled value reflected by cpuinfo.max_freq.
176 * @min_perf_pct: Minimum capacity limit in percent of the maximum turbo
178 * @max_perf_pct: Maximum capacity limit in percent of the maximum turbo
181 struct global_params {
184 bool turbo_disabled_mf;
190 * struct cpudata - Per CPU instance data storage
191 * @cpu: CPU number for this instance data
192 * @policy: CPUFreq policy value
193 * @update_util: CPUFreq utility callback information
194 * @update_util_set: CPUFreq utility callback is set
195 * @iowait_boost: iowait-related boost fraction
196 * @last_update: Time of the last update.
197 * @pstate: Stores P state limits for this CPU
198 * @vid: Stores VID limits for this CPU
199 * @last_sample_time: Last Sample time
200 * @aperf_mperf_shift: APERF vs MPERF counting frequency difference
201 * @prev_aperf: Last APERF value read from APERF MSR
202 * @prev_mperf: Last MPERF value read from MPERF MSR
203 * @prev_tsc: Last timestamp counter (TSC) value
204 * @prev_cummulative_iowait: IO Wait time difference from last and
206 * @sample: Storage for storing last Sample data
207 * @min_perf_ratio: Minimum capacity in terms of PERF or HWP ratios
208 * @max_perf_ratio: Maximum capacity in terms of PERF or HWP ratios
209 * @acpi_perf_data: Stores ACPI perf information read from _PSS
210 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
211 * @epp_powersave: Last saved HWP energy performance preference
212 * (EPP) or energy performance bias (EPB),
213 * when policy switched to performance
214 * @epp_policy: Last saved policy used to set EPP/EPB
215 * @epp_default: Power on default HWP energy performance
217 * @epp_cached Cached HWP energy-performance preference value
218 * @hwp_req_cached: Cached value of the last HWP Request MSR
219 * @hwp_cap_cached: Cached value of the last HWP Capabilities MSR
220 * @last_io_update: Last time when IO wake flag was set
221 * @sched_flags: Store scheduler flags for possible cross CPU update
222 * @hwp_boost_min: Last HWP boosted min performance
223 * @suspended: Whether or not the driver has been suspended.
224 * @hwp_notify_work: workqueue for HWP notifications.
226 * This structure stores per CPU instance data for all CPUs.
232 struct update_util_data update_util;
233 bool update_util_set;
235 struct pstate_data pstate;
239 u64 last_sample_time;
240 u64 aperf_mperf_shift;
244 u64 prev_cummulative_iowait;
245 struct sample sample;
246 int32_t min_perf_ratio;
247 int32_t max_perf_ratio;
249 struct acpi_processor_performance acpi_perf_data;
250 bool valid_pss_table;
252 unsigned int iowait_boost;
260 unsigned int sched_flags;
263 struct delayed_work hwp_notify_work;
266 static struct cpudata **all_cpu_data;
269 * struct pstate_funcs - Per CPU model specific callbacks
270 * @get_max: Callback to get maximum non turbo effective P state
271 * @get_max_physical: Callback to get maximum non turbo physical P state
272 * @get_min: Callback to get minimum P state
273 * @get_turbo: Callback to get turbo P state
274 * @get_scaling: Callback to get frequency scaling factor
275 * @get_cpu_scaling: Get frequency scaling factor for a given cpu
276 * @get_aperf_mperf_shift: Callback to get the APERF vs MPERF frequency difference
277 * @get_val: Callback to convert P state to actual MSR write value
278 * @get_vid: Callback to get VID data for Atom platforms
280 * Core and Atom CPU models have different way to get P State limits. This
281 * structure is used to store those callbacks.
283 struct pstate_funcs {
284 int (*get_max)(int cpu);
285 int (*get_max_physical)(int cpu);
286 int (*get_min)(int cpu);
287 int (*get_turbo)(int cpu);
288 int (*get_scaling)(void);
289 int (*get_cpu_scaling)(int cpu);
290 int (*get_aperf_mperf_shift)(void);
291 u64 (*get_val)(struct cpudata*, int pstate);
292 void (*get_vid)(struct cpudata *);
295 static struct pstate_funcs pstate_funcs __read_mostly;
297 static int hwp_active __read_mostly;
298 static int hwp_mode_bdw __read_mostly;
299 static bool per_cpu_limits __read_mostly;
300 static bool hwp_boost __read_mostly;
301 static bool hwp_forced __read_mostly;
303 static struct cpufreq_driver *intel_pstate_driver __read_mostly;
305 #define HYBRID_SCALING_FACTOR 78741
307 static inline int core_get_scaling(void)
313 static bool acpi_ppc;
316 static struct global_params global;
318 static DEFINE_MUTEX(intel_pstate_driver_lock);
319 static DEFINE_MUTEX(intel_pstate_limits_lock);
323 static bool intel_pstate_acpi_pm_profile_server(void)
325 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
326 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
332 static bool intel_pstate_get_ppc_enable_status(void)
334 if (intel_pstate_acpi_pm_profile_server())
340 #ifdef CONFIG_ACPI_CPPC_LIB
342 /* The work item is needed to avoid CPU hotplug locking issues */
343 static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
345 sched_set_itmt_support();
348 static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
350 #define CPPC_MAX_PERF U8_MAX
352 static void intel_pstate_set_itmt_prio(int cpu)
354 struct cppc_perf_caps cppc_perf;
355 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
358 ret = cppc_get_perf_caps(cpu, &cppc_perf);
363 * On some systems with overclocking enabled, CPPC.highest_perf is hardcoded to 0xff.
364 * In this case we can't use CPPC.highest_perf to enable ITMT.
365 * In this case we can look at MSR_HWP_CAPABILITIES bits [8:0] to decide.
367 if (cppc_perf.highest_perf == CPPC_MAX_PERF)
368 cppc_perf.highest_perf = HWP_HIGHEST_PERF(READ_ONCE(all_cpu_data[cpu]->hwp_cap_cached));
371 * The priorities can be set regardless of whether or not
372 * sched_set_itmt_support(true) has been called and it is valid to
373 * update them at any time after it has been called.
375 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
377 if (max_highest_perf <= min_highest_perf) {
378 if (cppc_perf.highest_perf > max_highest_perf)
379 max_highest_perf = cppc_perf.highest_perf;
381 if (cppc_perf.highest_perf < min_highest_perf)
382 min_highest_perf = cppc_perf.highest_perf;
384 if (max_highest_perf > min_highest_perf) {
386 * This code can be run during CPU online under the
387 * CPU hotplug locks, so sched_set_itmt_support()
388 * cannot be called from here. Queue up a work item
391 schedule_work(&sched_itmt_work);
396 static int intel_pstate_get_cppc_guaranteed(int cpu)
398 struct cppc_perf_caps cppc_perf;
401 ret = cppc_get_perf_caps(cpu, &cppc_perf);
405 if (cppc_perf.guaranteed_perf)
406 return cppc_perf.guaranteed_perf;
408 return cppc_perf.nominal_perf;
411 static int intel_pstate_cppc_get_scaling(int cpu)
413 struct cppc_perf_caps cppc_perf;
416 ret = cppc_get_perf_caps(cpu, &cppc_perf);
419 * If the nominal frequency and the nominal performance are not
420 * zero and the ratio between them is not 100, return the hybrid
423 if (!ret && cppc_perf.nominal_perf && cppc_perf.nominal_freq &&
424 cppc_perf.nominal_perf * 100 != cppc_perf.nominal_freq)
425 return HYBRID_SCALING_FACTOR;
427 return core_get_scaling();
430 #else /* CONFIG_ACPI_CPPC_LIB */
431 static inline void intel_pstate_set_itmt_prio(int cpu)
434 #endif /* CONFIG_ACPI_CPPC_LIB */
436 static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
443 intel_pstate_set_itmt_prio(policy->cpu);
447 if (!intel_pstate_get_ppc_enable_status())
450 cpu = all_cpu_data[policy->cpu];
452 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
458 * Check if the control value in _PSS is for PERF_CTL MSR, which should
459 * guarantee that the states returned by it map to the states in our
462 if (cpu->acpi_perf_data.control_register.space_id !=
463 ACPI_ADR_SPACE_FIXED_HARDWARE)
467 * If there is only one entry _PSS, simply ignore _PSS and continue as
468 * usual without taking _PSS into account
470 if (cpu->acpi_perf_data.state_count < 2)
473 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
474 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
475 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
476 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
477 (u32) cpu->acpi_perf_data.states[i].core_frequency,
478 (u32) cpu->acpi_perf_data.states[i].power,
479 (u32) cpu->acpi_perf_data.states[i].control);
482 cpu->valid_pss_table = true;
483 pr_debug("_PPC limits will be enforced\n");
488 cpu->valid_pss_table = false;
489 acpi_processor_unregister_performance(policy->cpu);
492 static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
496 cpu = all_cpu_data[policy->cpu];
497 if (!cpu->valid_pss_table)
500 acpi_processor_unregister_performance(policy->cpu);
502 #else /* CONFIG_ACPI */
503 static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
507 static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
511 static inline bool intel_pstate_acpi_pm_profile_server(void)
515 #endif /* CONFIG_ACPI */
517 #ifndef CONFIG_ACPI_CPPC_LIB
518 static inline int intel_pstate_get_cppc_guaranteed(int cpu)
523 static int intel_pstate_cppc_get_scaling(int cpu)
525 return core_get_scaling();
527 #endif /* CONFIG_ACPI_CPPC_LIB */
530 * intel_pstate_hybrid_hwp_adjust - Calibrate HWP performance levels.
533 * On hybrid processors, HWP may expose more performance levels than there are
534 * P-states accessible through the PERF_CTL interface. If that happens, the
535 * scaling factor between HWP performance levels and CPU frequency will be less
536 * than the scaling factor between P-state values and CPU frequency.
538 * In that case, adjust the CPU parameters used in computations accordingly.
540 static void intel_pstate_hybrid_hwp_adjust(struct cpudata *cpu)
542 int perf_ctl_max_phys = cpu->pstate.max_pstate_physical;
543 int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
544 int perf_ctl_turbo = pstate_funcs.get_turbo(cpu->cpu);
545 int scaling = cpu->pstate.scaling;
547 pr_debug("CPU%d: perf_ctl_max_phys = %d\n", cpu->cpu, perf_ctl_max_phys);
548 pr_debug("CPU%d: perf_ctl_turbo = %d\n", cpu->cpu, perf_ctl_turbo);
549 pr_debug("CPU%d: perf_ctl_scaling = %d\n", cpu->cpu, perf_ctl_scaling);
550 pr_debug("CPU%d: HWP_CAP guaranteed = %d\n", cpu->cpu, cpu->pstate.max_pstate);
551 pr_debug("CPU%d: HWP_CAP highest = %d\n", cpu->cpu, cpu->pstate.turbo_pstate);
552 pr_debug("CPU%d: HWP-to-frequency scaling factor: %d\n", cpu->cpu, scaling);
554 cpu->pstate.turbo_freq = rounddown(cpu->pstate.turbo_pstate * scaling,
556 cpu->pstate.max_freq = rounddown(cpu->pstate.max_pstate * scaling,
559 cpu->pstate.max_pstate_physical =
560 DIV_ROUND_UP(perf_ctl_max_phys * perf_ctl_scaling,
563 cpu->pstate.min_freq = cpu->pstate.min_pstate * perf_ctl_scaling;
565 * Cast the min P-state value retrieved via pstate_funcs.get_min() to
566 * the effective range of HWP performance levels.
568 cpu->pstate.min_pstate = DIV_ROUND_UP(cpu->pstate.min_freq, scaling);
571 static inline void update_turbo_state(void)
575 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
576 global.turbo_disabled = misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE;
579 static int min_perf_pct_min(void)
581 struct cpudata *cpu = all_cpu_data[0];
582 int turbo_pstate = cpu->pstate.turbo_pstate;
584 return turbo_pstate ?
585 (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
588 static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
593 if (!boot_cpu_has(X86_FEATURE_EPB))
596 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
600 return (s16)(epb & 0x0f);
603 static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
607 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
609 * When hwp_req_data is 0, means that caller didn't read
610 * MSR_HWP_REQUEST, so need to read and get EPP.
613 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
618 epp = (hwp_req_data >> 24) & 0xff;
620 /* When there is no EPP present, HWP uses EPB settings */
621 epp = intel_pstate_get_epb(cpu_data);
627 static int intel_pstate_set_epb(int cpu, s16 pref)
632 if (!boot_cpu_has(X86_FEATURE_EPB))
635 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
639 epb = (epb & ~0x0f) | pref;
640 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
646 * EPP/EPB display strings corresponding to EPP index in the
647 * energy_perf_strings[]
649 *-------------------------------------
652 * 2 balance_performance
657 enum energy_perf_value_index {
658 EPP_INDEX_DEFAULT = 0,
659 EPP_INDEX_PERFORMANCE,
660 EPP_INDEX_BALANCE_PERFORMANCE,
661 EPP_INDEX_BALANCE_POWERSAVE,
665 static const char * const energy_perf_strings[] = {
666 [EPP_INDEX_DEFAULT] = "default",
667 [EPP_INDEX_PERFORMANCE] = "performance",
668 [EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
669 [EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
670 [EPP_INDEX_POWERSAVE] = "power",
673 static unsigned int epp_values[] = {
674 [EPP_INDEX_DEFAULT] = 0, /* Unused index */
675 [EPP_INDEX_PERFORMANCE] = HWP_EPP_PERFORMANCE,
676 [EPP_INDEX_BALANCE_PERFORMANCE] = HWP_EPP_BALANCE_PERFORMANCE,
677 [EPP_INDEX_BALANCE_POWERSAVE] = HWP_EPP_BALANCE_POWERSAVE,
678 [EPP_INDEX_POWERSAVE] = HWP_EPP_POWERSAVE,
681 static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data, int *raw_epp)
687 epp = intel_pstate_get_epp(cpu_data, 0);
691 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
692 if (epp == epp_values[EPP_INDEX_PERFORMANCE])
693 return EPP_INDEX_PERFORMANCE;
694 if (epp == epp_values[EPP_INDEX_BALANCE_PERFORMANCE])
695 return EPP_INDEX_BALANCE_PERFORMANCE;
696 if (epp == epp_values[EPP_INDEX_BALANCE_POWERSAVE])
697 return EPP_INDEX_BALANCE_POWERSAVE;
698 if (epp == epp_values[EPP_INDEX_POWERSAVE])
699 return EPP_INDEX_POWERSAVE;
702 } else if (boot_cpu_has(X86_FEATURE_EPB)) {
705 * 0x00-0x03 : Performance
706 * 0x04-0x07 : Balance performance
707 * 0x08-0x0B : Balance power
709 * The EPB is a 4 bit value, but our ranges restrict the
710 * value which can be set. Here only using top two bits
713 index = (epp >> 2) + 1;
719 static int intel_pstate_set_epp(struct cpudata *cpu, u32 epp)
724 * Use the cached HWP Request MSR value, because in the active mode the
725 * register itself may be updated by intel_pstate_hwp_boost_up() or
726 * intel_pstate_hwp_boost_down() at any time.
728 u64 value = READ_ONCE(cpu->hwp_req_cached);
730 value &= ~GENMASK_ULL(31, 24);
731 value |= (u64)epp << 24;
733 * The only other updater of hwp_req_cached in the active mode,
734 * intel_pstate_hwp_set(), is called under the same lock as this
735 * function, so it cannot run in parallel with the update below.
737 WRITE_ONCE(cpu->hwp_req_cached, value);
738 ret = wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
740 cpu->epp_cached = epp;
745 static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
746 int pref_index, bool use_raw,
753 epp = cpu_data->epp_default;
755 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
758 else if (epp == -EINVAL)
759 epp = epp_values[pref_index];
762 * To avoid confusion, refuse to set EPP to any values different
763 * from 0 (performance) if the current policy is "performance",
764 * because those values would be overridden.
766 if (epp > 0 && cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
769 ret = intel_pstate_set_epp(cpu_data, epp);
772 epp = (pref_index - 1) << 2;
773 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
779 static ssize_t show_energy_performance_available_preferences(
780 struct cpufreq_policy *policy, char *buf)
785 while (energy_perf_strings[i] != NULL)
786 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
788 ret += sprintf(&buf[ret], "\n");
793 cpufreq_freq_attr_ro(energy_performance_available_preferences);
795 static struct cpufreq_driver intel_pstate;
797 static ssize_t store_energy_performance_preference(
798 struct cpufreq_policy *policy, const char *buf, size_t count)
800 struct cpudata *cpu = all_cpu_data[policy->cpu];
801 char str_preference[21];
806 ret = sscanf(buf, "%20s", str_preference);
810 ret = match_string(energy_perf_strings, -1, str_preference);
812 if (!boot_cpu_has(X86_FEATURE_HWP_EPP))
815 ret = kstrtouint(buf, 10, &epp);
826 * This function runs with the policy R/W semaphore held, which
827 * guarantees that the driver pointer will not change while it is
830 if (!intel_pstate_driver)
833 mutex_lock(&intel_pstate_limits_lock);
835 if (intel_pstate_driver == &intel_pstate) {
836 ret = intel_pstate_set_energy_pref_index(cpu, ret, raw, epp);
839 * In the passive mode the governor needs to be stopped on the
840 * target CPU before the EPP update and restarted after it,
841 * which is super-heavy-weight, so make sure it is worth doing
845 epp = ret ? epp_values[ret] : cpu->epp_default;
847 if (cpu->epp_cached != epp) {
850 cpufreq_stop_governor(policy);
851 ret = intel_pstate_set_epp(cpu, epp);
852 err = cpufreq_start_governor(policy);
860 mutex_unlock(&intel_pstate_limits_lock);
865 static ssize_t show_energy_performance_preference(
866 struct cpufreq_policy *policy, char *buf)
868 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
869 int preference, raw_epp;
871 preference = intel_pstate_get_energy_pref_index(cpu_data, &raw_epp);
876 return sprintf(buf, "%d\n", raw_epp);
878 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
881 cpufreq_freq_attr_rw(energy_performance_preference);
883 static ssize_t show_base_frequency(struct cpufreq_policy *policy, char *buf)
885 struct cpudata *cpu = all_cpu_data[policy->cpu];
888 ratio = intel_pstate_get_cppc_guaranteed(policy->cpu);
892 rdmsrl_on_cpu(policy->cpu, MSR_HWP_CAPABILITIES, &cap);
893 ratio = HWP_GUARANTEED_PERF(cap);
896 freq = ratio * cpu->pstate.scaling;
897 if (cpu->pstate.scaling != cpu->pstate.perf_ctl_scaling)
898 freq = rounddown(freq, cpu->pstate.perf_ctl_scaling);
900 return sprintf(buf, "%d\n", freq);
903 cpufreq_freq_attr_ro(base_frequency);
905 static struct freq_attr *hwp_cpufreq_attrs[] = {
906 &energy_performance_preference,
907 &energy_performance_available_preferences,
912 static void __intel_pstate_get_hwp_cap(struct cpudata *cpu)
916 rdmsrl_on_cpu(cpu->cpu, MSR_HWP_CAPABILITIES, &cap);
917 WRITE_ONCE(cpu->hwp_cap_cached, cap);
918 cpu->pstate.max_pstate = HWP_GUARANTEED_PERF(cap);
919 cpu->pstate.turbo_pstate = HWP_HIGHEST_PERF(cap);
922 static void intel_pstate_get_hwp_cap(struct cpudata *cpu)
924 int scaling = cpu->pstate.scaling;
926 __intel_pstate_get_hwp_cap(cpu);
928 cpu->pstate.max_freq = cpu->pstate.max_pstate * scaling;
929 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * scaling;
930 if (scaling != cpu->pstate.perf_ctl_scaling) {
931 int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
933 cpu->pstate.max_freq = rounddown(cpu->pstate.max_freq,
935 cpu->pstate.turbo_freq = rounddown(cpu->pstate.turbo_freq,
940 static void intel_pstate_hwp_set(unsigned int cpu)
942 struct cpudata *cpu_data = all_cpu_data[cpu];
947 max = cpu_data->max_perf_ratio;
948 min = cpu_data->min_perf_ratio;
950 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
953 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
955 value &= ~HWP_MIN_PERF(~0L);
956 value |= HWP_MIN_PERF(min);
958 value &= ~HWP_MAX_PERF(~0L);
959 value |= HWP_MAX_PERF(max);
961 if (cpu_data->epp_policy == cpu_data->policy)
964 cpu_data->epp_policy = cpu_data->policy;
966 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
967 epp = intel_pstate_get_epp(cpu_data, value);
968 cpu_data->epp_powersave = epp;
969 /* If EPP read was failed, then don't try to write */
975 /* skip setting EPP, when saved value is invalid */
976 if (cpu_data->epp_powersave < 0)
980 * No need to restore EPP when it is not zero. This
982 * - Policy is not changed
983 * - user has manually changed
984 * - Error reading EPB
986 epp = intel_pstate_get_epp(cpu_data, value);
990 epp = cpu_data->epp_powersave;
992 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
993 value &= ~GENMASK_ULL(31, 24);
994 value |= (u64)epp << 24;
996 intel_pstate_set_epb(cpu, epp);
999 WRITE_ONCE(cpu_data->hwp_req_cached, value);
1000 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
1003 static void intel_pstate_disable_hwp_interrupt(struct cpudata *cpudata);
1005 static void intel_pstate_hwp_offline(struct cpudata *cpu)
1007 u64 value = READ_ONCE(cpu->hwp_req_cached);
1010 intel_pstate_disable_hwp_interrupt(cpu);
1012 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
1014 * In case the EPP has been set to "performance" by the
1015 * active mode "performance" scaling algorithm, replace that
1016 * temporary value with the cached EPP one.
1018 value &= ~GENMASK_ULL(31, 24);
1019 value |= HWP_ENERGY_PERF_PREFERENCE(cpu->epp_cached);
1021 * However, make sure that EPP will be set to "performance" when
1022 * the CPU is brought back online again and the "performance"
1023 * scaling algorithm is still in effect.
1025 cpu->epp_policy = CPUFREQ_POLICY_UNKNOWN;
1029 * Clear the desired perf field in the cached HWP request value to
1030 * prevent nonzero desired values from being leaked into the active
1033 value &= ~HWP_DESIRED_PERF(~0L);
1034 WRITE_ONCE(cpu->hwp_req_cached, value);
1036 value &= ~GENMASK_ULL(31, 0);
1037 min_perf = HWP_LOWEST_PERF(READ_ONCE(cpu->hwp_cap_cached));
1039 /* Set hwp_max = hwp_min */
1040 value |= HWP_MAX_PERF(min_perf);
1041 value |= HWP_MIN_PERF(min_perf);
1043 /* Set EPP to min */
1044 if (boot_cpu_has(X86_FEATURE_HWP_EPP))
1045 value |= HWP_ENERGY_PERF_PREFERENCE(HWP_EPP_POWERSAVE);
1047 wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
1050 #define POWER_CTL_EE_ENABLE 1
1051 #define POWER_CTL_EE_DISABLE 2
1053 static int power_ctl_ee_state;
1055 static void set_power_ctl_ee_state(bool input)
1059 mutex_lock(&intel_pstate_driver_lock);
1060 rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
1062 power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE);
1063 power_ctl_ee_state = POWER_CTL_EE_ENABLE;
1065 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
1066 power_ctl_ee_state = POWER_CTL_EE_DISABLE;
1068 wrmsrl(MSR_IA32_POWER_CTL, power_ctl);
1069 mutex_unlock(&intel_pstate_driver_lock);
1072 static void intel_pstate_hwp_enable(struct cpudata *cpudata);
1074 static void intel_pstate_hwp_reenable(struct cpudata *cpu)
1076 intel_pstate_hwp_enable(cpu);
1077 wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, READ_ONCE(cpu->hwp_req_cached));
1080 static int intel_pstate_suspend(struct cpufreq_policy *policy)
1082 struct cpudata *cpu = all_cpu_data[policy->cpu];
1084 pr_debug("CPU %d suspending\n", cpu->cpu);
1086 cpu->suspended = true;
1088 /* disable HWP interrupt and cancel any pending work */
1089 intel_pstate_disable_hwp_interrupt(cpu);
1094 static int intel_pstate_resume(struct cpufreq_policy *policy)
1096 struct cpudata *cpu = all_cpu_data[policy->cpu];
1098 pr_debug("CPU %d resuming\n", cpu->cpu);
1100 /* Only restore if the system default is changed */
1101 if (power_ctl_ee_state == POWER_CTL_EE_ENABLE)
1102 set_power_ctl_ee_state(true);
1103 else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE)
1104 set_power_ctl_ee_state(false);
1106 if (cpu->suspended && hwp_active) {
1107 mutex_lock(&intel_pstate_limits_lock);
1109 /* Re-enable HWP, because "online" has not done that. */
1110 intel_pstate_hwp_reenable(cpu);
1112 mutex_unlock(&intel_pstate_limits_lock);
1115 cpu->suspended = false;
1120 static void intel_pstate_update_policies(void)
1124 for_each_possible_cpu(cpu)
1125 cpufreq_update_policy(cpu);
1128 static void __intel_pstate_update_max_freq(struct cpudata *cpudata,
1129 struct cpufreq_policy *policy)
1131 policy->cpuinfo.max_freq = global.turbo_disabled_mf ?
1132 cpudata->pstate.max_freq : cpudata->pstate.turbo_freq;
1133 refresh_frequency_limits(policy);
1136 static void intel_pstate_update_max_freq(unsigned int cpu)
1138 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
1143 __intel_pstate_update_max_freq(all_cpu_data[cpu], policy);
1145 cpufreq_cpu_release(policy);
1148 static void intel_pstate_update_limits(unsigned int cpu)
1150 mutex_lock(&intel_pstate_driver_lock);
1152 update_turbo_state();
1154 * If turbo has been turned on or off globally, policy limits for
1155 * all CPUs need to be updated to reflect that.
1157 if (global.turbo_disabled_mf != global.turbo_disabled) {
1158 global.turbo_disabled_mf = global.turbo_disabled;
1159 arch_set_max_freq_ratio(global.turbo_disabled);
1160 for_each_possible_cpu(cpu)
1161 intel_pstate_update_max_freq(cpu);
1163 cpufreq_update_policy(cpu);
1166 mutex_unlock(&intel_pstate_driver_lock);
1169 /************************** sysfs begin ************************/
1170 #define show_one(file_name, object) \
1171 static ssize_t show_##file_name \
1172 (struct kobject *kobj, struct kobj_attribute *attr, char *buf) \
1174 return sprintf(buf, "%u\n", global.object); \
1177 static ssize_t intel_pstate_show_status(char *buf);
1178 static int intel_pstate_update_status(const char *buf, size_t size);
1180 static ssize_t show_status(struct kobject *kobj,
1181 struct kobj_attribute *attr, char *buf)
1185 mutex_lock(&intel_pstate_driver_lock);
1186 ret = intel_pstate_show_status(buf);
1187 mutex_unlock(&intel_pstate_driver_lock);
1192 static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
1193 const char *buf, size_t count)
1195 char *p = memchr(buf, '\n', count);
1198 mutex_lock(&intel_pstate_driver_lock);
1199 ret = intel_pstate_update_status(buf, p ? p - buf : count);
1200 mutex_unlock(&intel_pstate_driver_lock);
1202 return ret < 0 ? ret : count;
1205 static ssize_t show_turbo_pct(struct kobject *kobj,
1206 struct kobj_attribute *attr, char *buf)
1208 struct cpudata *cpu;
1209 int total, no_turbo, turbo_pct;
1212 mutex_lock(&intel_pstate_driver_lock);
1214 if (!intel_pstate_driver) {
1215 mutex_unlock(&intel_pstate_driver_lock);
1219 cpu = all_cpu_data[0];
1221 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1222 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
1223 turbo_fp = div_fp(no_turbo, total);
1224 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
1226 mutex_unlock(&intel_pstate_driver_lock);
1228 return sprintf(buf, "%u\n", turbo_pct);
1231 static ssize_t show_num_pstates(struct kobject *kobj,
1232 struct kobj_attribute *attr, char *buf)
1234 struct cpudata *cpu;
1237 mutex_lock(&intel_pstate_driver_lock);
1239 if (!intel_pstate_driver) {
1240 mutex_unlock(&intel_pstate_driver_lock);
1244 cpu = all_cpu_data[0];
1245 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1247 mutex_unlock(&intel_pstate_driver_lock);
1249 return sprintf(buf, "%u\n", total);
1252 static ssize_t show_no_turbo(struct kobject *kobj,
1253 struct kobj_attribute *attr, char *buf)
1257 mutex_lock(&intel_pstate_driver_lock);
1259 if (!intel_pstate_driver) {
1260 mutex_unlock(&intel_pstate_driver_lock);
1264 update_turbo_state();
1265 if (global.turbo_disabled)
1266 ret = sprintf(buf, "%u\n", global.turbo_disabled);
1268 ret = sprintf(buf, "%u\n", global.no_turbo);
1270 mutex_unlock(&intel_pstate_driver_lock);
1275 static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b,
1276 const char *buf, size_t count)
1281 ret = sscanf(buf, "%u", &input);
1285 mutex_lock(&intel_pstate_driver_lock);
1287 if (!intel_pstate_driver) {
1288 mutex_unlock(&intel_pstate_driver_lock);
1292 mutex_lock(&intel_pstate_limits_lock);
1294 update_turbo_state();
1295 if (global.turbo_disabled) {
1296 pr_notice_once("Turbo disabled by BIOS or unavailable on processor\n");
1297 mutex_unlock(&intel_pstate_limits_lock);
1298 mutex_unlock(&intel_pstate_driver_lock);
1302 global.no_turbo = clamp_t(int, input, 0, 1);
1304 if (global.no_turbo) {
1305 struct cpudata *cpu = all_cpu_data[0];
1306 int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
1308 /* Squash the global minimum into the permitted range. */
1309 if (global.min_perf_pct > pct)
1310 global.min_perf_pct = pct;
1313 mutex_unlock(&intel_pstate_limits_lock);
1315 intel_pstate_update_policies();
1316 arch_set_max_freq_ratio(global.no_turbo);
1318 mutex_unlock(&intel_pstate_driver_lock);
1323 static void update_qos_request(enum freq_qos_req_type type)
1325 struct freq_qos_request *req;
1326 struct cpufreq_policy *policy;
1329 for_each_possible_cpu(i) {
1330 struct cpudata *cpu = all_cpu_data[i];
1331 unsigned int freq, perf_pct;
1333 policy = cpufreq_cpu_get(i);
1337 req = policy->driver_data;
1338 cpufreq_cpu_put(policy);
1344 intel_pstate_get_hwp_cap(cpu);
1346 if (type == FREQ_QOS_MIN) {
1347 perf_pct = global.min_perf_pct;
1350 perf_pct = global.max_perf_pct;
1353 freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * perf_pct, 100);
1355 if (freq_qos_update_request(req, freq) < 0)
1356 pr_warn("Failed to update freq constraint: CPU%d\n", i);
1360 static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
1361 const char *buf, size_t count)
1366 ret = sscanf(buf, "%u", &input);
1370 mutex_lock(&intel_pstate_driver_lock);
1372 if (!intel_pstate_driver) {
1373 mutex_unlock(&intel_pstate_driver_lock);
1377 mutex_lock(&intel_pstate_limits_lock);
1379 global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
1381 mutex_unlock(&intel_pstate_limits_lock);
1383 if (intel_pstate_driver == &intel_pstate)
1384 intel_pstate_update_policies();
1386 update_qos_request(FREQ_QOS_MAX);
1388 mutex_unlock(&intel_pstate_driver_lock);
1393 static ssize_t store_min_perf_pct(struct kobject *a, struct kobj_attribute *b,
1394 const char *buf, size_t count)
1399 ret = sscanf(buf, "%u", &input);
1403 mutex_lock(&intel_pstate_driver_lock);
1405 if (!intel_pstate_driver) {
1406 mutex_unlock(&intel_pstate_driver_lock);
1410 mutex_lock(&intel_pstate_limits_lock);
1412 global.min_perf_pct = clamp_t(int, input,
1413 min_perf_pct_min(), global.max_perf_pct);
1415 mutex_unlock(&intel_pstate_limits_lock);
1417 if (intel_pstate_driver == &intel_pstate)
1418 intel_pstate_update_policies();
1420 update_qos_request(FREQ_QOS_MIN);
1422 mutex_unlock(&intel_pstate_driver_lock);
1427 static ssize_t show_hwp_dynamic_boost(struct kobject *kobj,
1428 struct kobj_attribute *attr, char *buf)
1430 return sprintf(buf, "%u\n", hwp_boost);
1433 static ssize_t store_hwp_dynamic_boost(struct kobject *a,
1434 struct kobj_attribute *b,
1435 const char *buf, size_t count)
1440 ret = kstrtouint(buf, 10, &input);
1444 mutex_lock(&intel_pstate_driver_lock);
1445 hwp_boost = !!input;
1446 intel_pstate_update_policies();
1447 mutex_unlock(&intel_pstate_driver_lock);
1452 static ssize_t show_energy_efficiency(struct kobject *kobj, struct kobj_attribute *attr,
1458 rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
1459 enable = !!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE));
1460 return sprintf(buf, "%d\n", !enable);
1463 static ssize_t store_energy_efficiency(struct kobject *a, struct kobj_attribute *b,
1464 const char *buf, size_t count)
1469 ret = kstrtobool(buf, &input);
1473 set_power_ctl_ee_state(input);
1478 show_one(max_perf_pct, max_perf_pct);
1479 show_one(min_perf_pct, min_perf_pct);
1481 define_one_global_rw(status);
1482 define_one_global_rw(no_turbo);
1483 define_one_global_rw(max_perf_pct);
1484 define_one_global_rw(min_perf_pct);
1485 define_one_global_ro(turbo_pct);
1486 define_one_global_ro(num_pstates);
1487 define_one_global_rw(hwp_dynamic_boost);
1488 define_one_global_rw(energy_efficiency);
1490 static struct attribute *intel_pstate_attributes[] = {
1496 static const struct attribute_group intel_pstate_attr_group = {
1497 .attrs = intel_pstate_attributes,
1500 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[];
1502 static struct kobject *intel_pstate_kobject;
1504 static void __init intel_pstate_sysfs_expose_params(void)
1506 struct device *dev_root = bus_get_dev_root(&cpu_subsys);
1510 intel_pstate_kobject = kobject_create_and_add("intel_pstate", &dev_root->kobj);
1511 put_device(dev_root);
1513 if (WARN_ON(!intel_pstate_kobject))
1516 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
1520 if (!boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
1521 rc = sysfs_create_file(intel_pstate_kobject, &turbo_pct.attr);
1524 rc = sysfs_create_file(intel_pstate_kobject, &num_pstates.attr);
1529 * If per cpu limits are enforced there are no global limits, so
1530 * return without creating max/min_perf_pct attributes
1535 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1538 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1541 if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids)) {
1542 rc = sysfs_create_file(intel_pstate_kobject, &energy_efficiency.attr);
1547 static void __init intel_pstate_sysfs_remove(void)
1549 if (!intel_pstate_kobject)
1552 sysfs_remove_group(intel_pstate_kobject, &intel_pstate_attr_group);
1554 if (!boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
1555 sysfs_remove_file(intel_pstate_kobject, &num_pstates.attr);
1556 sysfs_remove_file(intel_pstate_kobject, &turbo_pct.attr);
1559 if (!per_cpu_limits) {
1560 sysfs_remove_file(intel_pstate_kobject, &max_perf_pct.attr);
1561 sysfs_remove_file(intel_pstate_kobject, &min_perf_pct.attr);
1563 if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids))
1564 sysfs_remove_file(intel_pstate_kobject, &energy_efficiency.attr);
1567 kobject_put(intel_pstate_kobject);
1570 static void intel_pstate_sysfs_expose_hwp_dynamic_boost(void)
1577 rc = sysfs_create_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1581 static void intel_pstate_sysfs_hide_hwp_dynamic_boost(void)
1586 sysfs_remove_file(intel_pstate_kobject, &hwp_dynamic_boost.attr);
1589 /************************** sysfs end ************************/
1591 static void intel_pstate_notify_work(struct work_struct *work)
1593 struct cpudata *cpudata =
1594 container_of(to_delayed_work(work), struct cpudata, hwp_notify_work);
1595 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpudata->cpu);
1598 intel_pstate_get_hwp_cap(cpudata);
1599 __intel_pstate_update_max_freq(cpudata, policy);
1601 cpufreq_cpu_release(policy);
1604 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_STATUS, 0);
1607 static DEFINE_SPINLOCK(hwp_notify_lock);
1608 static cpumask_t hwp_intr_enable_mask;
1610 void notify_hwp_interrupt(void)
1612 unsigned int this_cpu = smp_processor_id();
1613 struct cpudata *cpudata;
1614 unsigned long flags;
1617 if (!READ_ONCE(hwp_active) || !boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
1620 rdmsrl_safe(MSR_HWP_STATUS, &value);
1621 if (!(value & 0x01))
1624 spin_lock_irqsave(&hwp_notify_lock, flags);
1626 if (!cpumask_test_cpu(this_cpu, &hwp_intr_enable_mask))
1630 * Currently we never free all_cpu_data. And we can't reach here
1631 * without this allocated. But for safety for future changes, added
1634 if (unlikely(!READ_ONCE(all_cpu_data)))
1638 * The free is done during cleanup, when cpufreq registry is failed.
1639 * We wouldn't be here if it fails on init or switch status. But for
1640 * future changes, added check.
1642 cpudata = READ_ONCE(all_cpu_data[this_cpu]);
1643 if (unlikely(!cpudata))
1646 schedule_delayed_work(&cpudata->hwp_notify_work, msecs_to_jiffies(10));
1648 spin_unlock_irqrestore(&hwp_notify_lock, flags);
1653 wrmsrl_safe(MSR_HWP_STATUS, 0);
1654 spin_unlock_irqrestore(&hwp_notify_lock, flags);
1657 static void intel_pstate_disable_hwp_interrupt(struct cpudata *cpudata)
1659 unsigned long flags;
1661 if (!boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
1664 /* wrmsrl_on_cpu has to be outside spinlock as this can result in IPC */
1665 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
1667 spin_lock_irqsave(&hwp_notify_lock, flags);
1668 if (cpumask_test_and_clear_cpu(cpudata->cpu, &hwp_intr_enable_mask))
1669 cancel_delayed_work(&cpudata->hwp_notify_work);
1670 spin_unlock_irqrestore(&hwp_notify_lock, flags);
1673 static void intel_pstate_enable_hwp_interrupt(struct cpudata *cpudata)
1675 /* Enable HWP notification interrupt for guaranteed performance change */
1676 if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY)) {
1677 unsigned long flags;
1679 spin_lock_irqsave(&hwp_notify_lock, flags);
1680 INIT_DELAYED_WORK(&cpudata->hwp_notify_work, intel_pstate_notify_work);
1681 cpumask_set_cpu(cpudata->cpu, &hwp_intr_enable_mask);
1682 spin_unlock_irqrestore(&hwp_notify_lock, flags);
1684 /* wrmsrl_on_cpu has to be outside spinlock as this can result in IPC */
1685 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x01);
1686 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_STATUS, 0);
1690 static void intel_pstate_update_epp_defaults(struct cpudata *cpudata)
1692 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
1695 * If this CPU gen doesn't call for change in balance_perf
1698 if (epp_values[EPP_INDEX_BALANCE_PERFORMANCE] == HWP_EPP_BALANCE_PERFORMANCE)
1702 * If the EPP is set by firmware, which means that firmware enabled HWP
1703 * - Is equal or less than 0x80 (default balance_perf EPP)
1704 * - But less performance oriented than performance EPP
1705 * then use this as new balance_perf EPP.
1707 if (hwp_forced && cpudata->epp_default <= HWP_EPP_BALANCE_PERFORMANCE &&
1708 cpudata->epp_default > HWP_EPP_PERFORMANCE) {
1709 epp_values[EPP_INDEX_BALANCE_PERFORMANCE] = cpudata->epp_default;
1714 * Use hard coded value per gen to update the balance_perf
1717 cpudata->epp_default = epp_values[EPP_INDEX_BALANCE_PERFORMANCE];
1718 intel_pstate_set_epp(cpudata, cpudata->epp_default);
1721 static void intel_pstate_hwp_enable(struct cpudata *cpudata)
1723 /* First disable HWP notification interrupt till we activate again */
1724 if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
1725 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
1727 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
1729 intel_pstate_enable_hwp_interrupt(cpudata);
1731 if (cpudata->epp_default >= 0)
1734 intel_pstate_update_epp_defaults(cpudata);
1737 static int atom_get_min_pstate(int not_used)
1741 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
1742 return (value >> 8) & 0x7F;
1745 static int atom_get_max_pstate(int not_used)
1749 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
1750 return (value >> 16) & 0x7F;
1753 static int atom_get_turbo_pstate(int not_used)
1757 rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
1758 return value & 0x7F;
1761 static u64 atom_get_val(struct cpudata *cpudata, int pstate)
1767 val = (u64)pstate << 8;
1768 if (global.no_turbo && !global.turbo_disabled)
1769 val |= (u64)1 << 32;
1771 vid_fp = cpudata->vid.min + mul_fp(
1772 int_tofp(pstate - cpudata->pstate.min_pstate),
1773 cpudata->vid.ratio);
1775 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
1776 vid = ceiling_fp(vid_fp);
1778 if (pstate > cpudata->pstate.max_pstate)
1779 vid = cpudata->vid.turbo;
1784 static int silvermont_get_scaling(void)
1788 /* Defined in Table 35-6 from SDM (Sept 2015) */
1789 static int silvermont_freq_table[] = {
1790 83300, 100000, 133300, 116700, 80000};
1792 rdmsrl(MSR_FSB_FREQ, value);
1796 return silvermont_freq_table[i];
1799 static int airmont_get_scaling(void)
1803 /* Defined in Table 35-10 from SDM (Sept 2015) */
1804 static int airmont_freq_table[] = {
1805 83300, 100000, 133300, 116700, 80000,
1806 93300, 90000, 88900, 87500};
1808 rdmsrl(MSR_FSB_FREQ, value);
1812 return airmont_freq_table[i];
1815 static void atom_get_vid(struct cpudata *cpudata)
1819 rdmsrl(MSR_ATOM_CORE_VIDS, value);
1820 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1821 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
1822 cpudata->vid.ratio = div_fp(
1823 cpudata->vid.max - cpudata->vid.min,
1824 int_tofp(cpudata->pstate.max_pstate -
1825 cpudata->pstate.min_pstate));
1827 rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
1828 cpudata->vid.turbo = value & 0x7f;
1831 static int core_get_min_pstate(int cpu)
1835 rdmsrl_on_cpu(cpu, MSR_PLATFORM_INFO, &value);
1836 return (value >> 40) & 0xFF;
1839 static int core_get_max_pstate_physical(int cpu)
1843 rdmsrl_on_cpu(cpu, MSR_PLATFORM_INFO, &value);
1844 return (value >> 8) & 0xFF;
1847 static int core_get_tdp_ratio(int cpu, u64 plat_info)
1849 /* Check how many TDP levels present */
1850 if (plat_info & 0x600000000) {
1856 /* Get the TDP level (0, 1, 2) to get ratios */
1857 err = rdmsrl_safe_on_cpu(cpu, MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1861 /* TDP MSR are continuous starting at 0x648 */
1862 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
1863 err = rdmsrl_safe_on_cpu(cpu, tdp_msr, &tdp_ratio);
1867 /* For level 1 and 2, bits[23:16] contain the ratio */
1868 if (tdp_ctrl & 0x03)
1871 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
1872 pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
1874 return (int)tdp_ratio;
1880 static int core_get_max_pstate(int cpu)
1888 rdmsrl_on_cpu(cpu, MSR_PLATFORM_INFO, &plat_info);
1889 max_pstate = (plat_info >> 8) & 0xFF;
1891 tdp_ratio = core_get_tdp_ratio(cpu, plat_info);
1896 /* Turbo activation ratio is not used on HWP platforms */
1900 err = rdmsrl_safe_on_cpu(cpu, MSR_TURBO_ACTIVATION_RATIO, &tar);
1904 /* Do some sanity checking for safety */
1905 tar_levels = tar & 0xff;
1906 if (tdp_ratio - 1 == tar_levels) {
1907 max_pstate = tar_levels;
1908 pr_debug("max_pstate=TAC %x\n", max_pstate);
1915 static int core_get_turbo_pstate(int cpu)
1920 rdmsrl_on_cpu(cpu, MSR_TURBO_RATIO_LIMIT, &value);
1921 nont = core_get_max_pstate(cpu);
1922 ret = (value) & 255;
1928 static u64 core_get_val(struct cpudata *cpudata, int pstate)
1932 val = (u64)pstate << 8;
1933 if (global.no_turbo && !global.turbo_disabled)
1934 val |= (u64)1 << 32;
1939 static int knl_get_aperf_mperf_shift(void)
1944 static int knl_get_turbo_pstate(int cpu)
1949 rdmsrl_on_cpu(cpu, MSR_TURBO_RATIO_LIMIT, &value);
1950 nont = core_get_max_pstate(cpu);
1951 ret = (((value) >> 8) & 0xFF);
1957 static void hybrid_get_type(void *data)
1959 u8 *cpu_type = data;
1961 *cpu_type = get_this_hybrid_cpu_type();
1964 static int hwp_get_cpu_scaling(int cpu)
1968 smp_call_function_single(cpu, hybrid_get_type, &cpu_type, 1);
1969 /* P-cores have a smaller perf level-to-freqency scaling factor. */
1970 if (cpu_type == 0x40)
1971 return HYBRID_SCALING_FACTOR;
1973 /* Use default core scaling for E-cores */
1974 if (cpu_type == 0x20)
1975 return core_get_scaling();
1978 * If reached here, this system is either non-hybrid (like Tiger
1979 * Lake) or hybrid-capable (like Alder Lake or Raptor Lake) with
1980 * no E cores (in which case CPUID for hybrid support is 0).
1982 * The CPPC nominal_frequency field is 0 for non-hybrid systems,
1983 * so the default core scaling will be used for them.
1985 return intel_pstate_cppc_get_scaling(cpu);
1988 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
1990 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1991 cpu->pstate.current_pstate = pstate;
1993 * Generally, there is no guarantee that this code will always run on
1994 * the CPU being updated, so force the register update to run on the
1997 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1998 pstate_funcs.get_val(cpu, pstate));
2001 static void intel_pstate_set_min_pstate(struct cpudata *cpu)
2003 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
2006 static void intel_pstate_max_within_limits(struct cpudata *cpu)
2008 int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
2010 update_turbo_state();
2011 intel_pstate_set_pstate(cpu, pstate);
2014 static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
2016 int perf_ctl_max_phys = pstate_funcs.get_max_physical(cpu->cpu);
2017 int perf_ctl_scaling = pstate_funcs.get_scaling();
2019 cpu->pstate.min_pstate = pstate_funcs.get_min(cpu->cpu);
2020 cpu->pstate.max_pstate_physical = perf_ctl_max_phys;
2021 cpu->pstate.perf_ctl_scaling = perf_ctl_scaling;
2023 if (hwp_active && !hwp_mode_bdw) {
2024 __intel_pstate_get_hwp_cap(cpu);
2026 if (pstate_funcs.get_cpu_scaling) {
2027 cpu->pstate.scaling = pstate_funcs.get_cpu_scaling(cpu->cpu);
2028 if (cpu->pstate.scaling != perf_ctl_scaling)
2029 intel_pstate_hybrid_hwp_adjust(cpu);
2031 cpu->pstate.scaling = perf_ctl_scaling;
2034 cpu->pstate.scaling = perf_ctl_scaling;
2035 cpu->pstate.max_pstate = pstate_funcs.get_max(cpu->cpu);
2036 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo(cpu->cpu);
2039 if (cpu->pstate.scaling == perf_ctl_scaling) {
2040 cpu->pstate.min_freq = cpu->pstate.min_pstate * perf_ctl_scaling;
2041 cpu->pstate.max_freq = cpu->pstate.max_pstate * perf_ctl_scaling;
2042 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * perf_ctl_scaling;
2045 if (pstate_funcs.get_aperf_mperf_shift)
2046 cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
2048 if (pstate_funcs.get_vid)
2049 pstate_funcs.get_vid(cpu);
2051 intel_pstate_set_min_pstate(cpu);
2055 * Long hold time will keep high perf limits for long time,
2056 * which negatively impacts perf/watt for some workloads,
2057 * like specpower. 3ms is based on experiements on some
2060 static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
2062 static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu)
2064 u64 hwp_req = READ_ONCE(cpu->hwp_req_cached);
2065 u64 hwp_cap = READ_ONCE(cpu->hwp_cap_cached);
2066 u32 max_limit = (hwp_req & 0xff00) >> 8;
2067 u32 min_limit = (hwp_req & 0xff);
2071 * Cases to consider (User changes via sysfs or boot time):
2072 * If, P0 (Turbo max) = P1 (Guaranteed max) = min:
2074 * If, P0 (Turbo max) > P1 (Guaranteed max) = min:
2075 * Should result in one level boost only for P0.
2076 * If, P0 (Turbo max) = P1 (Guaranteed max) > min:
2077 * Should result in two level boost:
2078 * (min + p1)/2 and P1.
2079 * If, P0 (Turbo max) > P1 (Guaranteed max) > min:
2080 * Should result in three level boost:
2081 * (min + p1)/2, P1 and P0.
2084 /* If max and min are equal or already at max, nothing to boost */
2085 if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit)
2088 if (!cpu->hwp_boost_min)
2089 cpu->hwp_boost_min = min_limit;
2091 /* level at half way mark between min and guranteed */
2092 boost_level1 = (HWP_GUARANTEED_PERF(hwp_cap) + min_limit) >> 1;
2094 if (cpu->hwp_boost_min < boost_level1)
2095 cpu->hwp_boost_min = boost_level1;
2096 else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(hwp_cap))
2097 cpu->hwp_boost_min = HWP_GUARANTEED_PERF(hwp_cap);
2098 else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(hwp_cap) &&
2099 max_limit != HWP_GUARANTEED_PERF(hwp_cap))
2100 cpu->hwp_boost_min = max_limit;
2104 hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min;
2105 wrmsrl(MSR_HWP_REQUEST, hwp_req);
2106 cpu->last_update = cpu->sample.time;
2109 static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu)
2111 if (cpu->hwp_boost_min) {
2114 /* Check if we are idle for hold time to boost down */
2115 expired = time_after64(cpu->sample.time, cpu->last_update +
2116 hwp_boost_hold_time_ns);
2118 wrmsrl(MSR_HWP_REQUEST, cpu->hwp_req_cached);
2119 cpu->hwp_boost_min = 0;
2122 cpu->last_update = cpu->sample.time;
2125 static inline void intel_pstate_update_util_hwp_local(struct cpudata *cpu,
2128 cpu->sample.time = time;
2130 if (cpu->sched_flags & SCHED_CPUFREQ_IOWAIT) {
2133 cpu->sched_flags = 0;
2135 * Set iowait_boost flag and update time. Since IO WAIT flag
2136 * is set all the time, we can't just conclude that there is
2137 * some IO bound activity is scheduled on this CPU with just
2138 * one occurrence. If we receive at least two in two
2139 * consecutive ticks, then we treat as boost candidate.
2141 if (time_before64(time, cpu->last_io_update + 2 * TICK_NSEC))
2144 cpu->last_io_update = time;
2147 intel_pstate_hwp_boost_up(cpu);
2150 intel_pstate_hwp_boost_down(cpu);
2154 static inline void intel_pstate_update_util_hwp(struct update_util_data *data,
2155 u64 time, unsigned int flags)
2157 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
2159 cpu->sched_flags |= flags;
2161 if (smp_processor_id() == cpu->cpu)
2162 intel_pstate_update_util_hwp_local(cpu, time);
2165 static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
2167 struct sample *sample = &cpu->sample;
2169 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
2172 static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
2175 unsigned long flags;
2178 local_irq_save(flags);
2179 rdmsrl(MSR_IA32_APERF, aperf);
2180 rdmsrl(MSR_IA32_MPERF, mperf);
2182 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
2183 local_irq_restore(flags);
2186 local_irq_restore(flags);
2188 cpu->last_sample_time = cpu->sample.time;
2189 cpu->sample.time = time;
2190 cpu->sample.aperf = aperf;
2191 cpu->sample.mperf = mperf;
2192 cpu->sample.tsc = tsc;
2193 cpu->sample.aperf -= cpu->prev_aperf;
2194 cpu->sample.mperf -= cpu->prev_mperf;
2195 cpu->sample.tsc -= cpu->prev_tsc;
2197 cpu->prev_aperf = aperf;
2198 cpu->prev_mperf = mperf;
2199 cpu->prev_tsc = tsc;
2201 * First time this function is invoked in a given cycle, all of the
2202 * previous sample data fields are equal to zero or stale and they must
2203 * be populated with meaningful numbers for things to work, so assume
2204 * that sample.time will always be reset before setting the utilization
2205 * update hook and make the caller skip the sample then.
2207 if (cpu->last_sample_time) {
2208 intel_pstate_calc_avg_perf(cpu);
2214 static inline int32_t get_avg_frequency(struct cpudata *cpu)
2216 return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
2219 static inline int32_t get_avg_pstate(struct cpudata *cpu)
2221 return mul_ext_fp(cpu->pstate.max_pstate_physical,
2222 cpu->sample.core_avg_perf);
2225 static inline int32_t get_target_pstate(struct cpudata *cpu)
2227 struct sample *sample = &cpu->sample;
2229 int target, avg_pstate;
2231 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
2234 if (busy_frac < cpu->iowait_boost)
2235 busy_frac = cpu->iowait_boost;
2237 sample->busy_scaled = busy_frac * 100;
2239 target = global.no_turbo || global.turbo_disabled ?
2240 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2241 target += target >> 2;
2242 target = mul_fp(target, busy_frac);
2243 if (target < cpu->pstate.min_pstate)
2244 target = cpu->pstate.min_pstate;
2247 * If the average P-state during the previous cycle was higher than the
2248 * current target, add 50% of the difference to the target to reduce
2249 * possible performance oscillations and offset possible performance
2250 * loss related to moving the workload from one CPU to another within
2253 avg_pstate = get_avg_pstate(cpu);
2254 if (avg_pstate > target)
2255 target += (avg_pstate - target) >> 1;
2260 static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
2262 int min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
2263 int max_pstate = max(min_pstate, cpu->max_perf_ratio);
2265 return clamp_t(int, pstate, min_pstate, max_pstate);
2268 static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
2270 if (pstate == cpu->pstate.current_pstate)
2273 cpu->pstate.current_pstate = pstate;
2274 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
2277 static void intel_pstate_adjust_pstate(struct cpudata *cpu)
2279 int from = cpu->pstate.current_pstate;
2280 struct sample *sample;
2283 update_turbo_state();
2285 target_pstate = get_target_pstate(cpu);
2286 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2287 trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
2288 intel_pstate_update_pstate(cpu, target_pstate);
2290 sample = &cpu->sample;
2291 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
2292 fp_toint(sample->busy_scaled),
2294 cpu->pstate.current_pstate,
2298 get_avg_frequency(cpu),
2299 fp_toint(cpu->iowait_boost * 100));
2302 static void intel_pstate_update_util(struct update_util_data *data, u64 time,
2305 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
2308 /* Don't allow remote callbacks */
2309 if (smp_processor_id() != cpu->cpu)
2312 delta_ns = time - cpu->last_update;
2313 if (flags & SCHED_CPUFREQ_IOWAIT) {
2314 /* Start over if the CPU may have been idle. */
2315 if (delta_ns > TICK_NSEC) {
2316 cpu->iowait_boost = ONE_EIGHTH_FP;
2317 } else if (cpu->iowait_boost >= ONE_EIGHTH_FP) {
2318 cpu->iowait_boost <<= 1;
2319 if (cpu->iowait_boost > int_tofp(1))
2320 cpu->iowait_boost = int_tofp(1);
2322 cpu->iowait_boost = ONE_EIGHTH_FP;
2324 } else if (cpu->iowait_boost) {
2325 /* Clear iowait_boost if the CPU may have been idle. */
2326 if (delta_ns > TICK_NSEC)
2327 cpu->iowait_boost = 0;
2329 cpu->iowait_boost >>= 1;
2331 cpu->last_update = time;
2332 delta_ns = time - cpu->sample.time;
2333 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
2336 if (intel_pstate_sample(cpu, time))
2337 intel_pstate_adjust_pstate(cpu);
2340 static struct pstate_funcs core_funcs = {
2341 .get_max = core_get_max_pstate,
2342 .get_max_physical = core_get_max_pstate_physical,
2343 .get_min = core_get_min_pstate,
2344 .get_turbo = core_get_turbo_pstate,
2345 .get_scaling = core_get_scaling,
2346 .get_val = core_get_val,
2349 static const struct pstate_funcs silvermont_funcs = {
2350 .get_max = atom_get_max_pstate,
2351 .get_max_physical = atom_get_max_pstate,
2352 .get_min = atom_get_min_pstate,
2353 .get_turbo = atom_get_turbo_pstate,
2354 .get_val = atom_get_val,
2355 .get_scaling = silvermont_get_scaling,
2356 .get_vid = atom_get_vid,
2359 static const struct pstate_funcs airmont_funcs = {
2360 .get_max = atom_get_max_pstate,
2361 .get_max_physical = atom_get_max_pstate,
2362 .get_min = atom_get_min_pstate,
2363 .get_turbo = atom_get_turbo_pstate,
2364 .get_val = atom_get_val,
2365 .get_scaling = airmont_get_scaling,
2366 .get_vid = atom_get_vid,
2369 static const struct pstate_funcs knl_funcs = {
2370 .get_max = core_get_max_pstate,
2371 .get_max_physical = core_get_max_pstate_physical,
2372 .get_min = core_get_min_pstate,
2373 .get_turbo = knl_get_turbo_pstate,
2374 .get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
2375 .get_scaling = core_get_scaling,
2376 .get_val = core_get_val,
2379 #define X86_MATCH(model, policy) \
2380 X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
2381 X86_FEATURE_APERFMPERF, &policy)
2383 static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
2384 X86_MATCH(SANDYBRIDGE, core_funcs),
2385 X86_MATCH(SANDYBRIDGE_X, core_funcs),
2386 X86_MATCH(ATOM_SILVERMONT, silvermont_funcs),
2387 X86_MATCH(IVYBRIDGE, core_funcs),
2388 X86_MATCH(HASWELL, core_funcs),
2389 X86_MATCH(BROADWELL, core_funcs),
2390 X86_MATCH(IVYBRIDGE_X, core_funcs),
2391 X86_MATCH(HASWELL_X, core_funcs),
2392 X86_MATCH(HASWELL_L, core_funcs),
2393 X86_MATCH(HASWELL_G, core_funcs),
2394 X86_MATCH(BROADWELL_G, core_funcs),
2395 X86_MATCH(ATOM_AIRMONT, airmont_funcs),
2396 X86_MATCH(SKYLAKE_L, core_funcs),
2397 X86_MATCH(BROADWELL_X, core_funcs),
2398 X86_MATCH(SKYLAKE, core_funcs),
2399 X86_MATCH(BROADWELL_D, core_funcs),
2400 X86_MATCH(XEON_PHI_KNL, knl_funcs),
2401 X86_MATCH(XEON_PHI_KNM, knl_funcs),
2402 X86_MATCH(ATOM_GOLDMONT, core_funcs),
2403 X86_MATCH(ATOM_GOLDMONT_PLUS, core_funcs),
2404 X86_MATCH(SKYLAKE_X, core_funcs),
2405 X86_MATCH(COMETLAKE, core_funcs),
2406 X86_MATCH(ICELAKE_X, core_funcs),
2407 X86_MATCH(TIGERLAKE, core_funcs),
2408 X86_MATCH(SAPPHIRERAPIDS_X, core_funcs),
2411 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
2413 static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
2414 X86_MATCH(BROADWELL_D, core_funcs),
2415 X86_MATCH(BROADWELL_X, core_funcs),
2416 X86_MATCH(SKYLAKE_X, core_funcs),
2417 X86_MATCH(ICELAKE_X, core_funcs),
2418 X86_MATCH(SAPPHIRERAPIDS_X, core_funcs),
2422 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
2423 X86_MATCH(KABYLAKE, core_funcs),
2427 static int intel_pstate_init_cpu(unsigned int cpunum)
2429 struct cpudata *cpu;
2431 cpu = all_cpu_data[cpunum];
2434 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
2438 WRITE_ONCE(all_cpu_data[cpunum], cpu);
2442 cpu->epp_default = -EINVAL;
2445 intel_pstate_hwp_enable(cpu);
2447 if (intel_pstate_acpi_pm_profile_server())
2450 } else if (hwp_active) {
2452 * Re-enable HWP in case this happens after a resume from ACPI
2453 * S3 if the CPU was offline during the whole system/resume
2456 intel_pstate_hwp_reenable(cpu);
2459 cpu->epp_powersave = -EINVAL;
2460 cpu->epp_policy = 0;
2462 intel_pstate_get_cpu_pstates(cpu);
2464 pr_debug("controlling: cpu %d\n", cpunum);
2469 static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
2471 struct cpudata *cpu = all_cpu_data[cpu_num];
2473 if (hwp_active && !hwp_boost)
2476 if (cpu->update_util_set)
2479 /* Prevent intel_pstate_update_util() from using stale data. */
2480 cpu->sample.time = 0;
2481 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
2483 intel_pstate_update_util_hwp :
2484 intel_pstate_update_util));
2485 cpu->update_util_set = true;
2488 static void intel_pstate_clear_update_util_hook(unsigned int cpu)
2490 struct cpudata *cpu_data = all_cpu_data[cpu];
2492 if (!cpu_data->update_util_set)
2495 cpufreq_remove_update_util_hook(cpu);
2496 cpu_data->update_util_set = false;
2500 static int intel_pstate_get_max_freq(struct cpudata *cpu)
2502 return global.turbo_disabled || global.no_turbo ?
2503 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2506 static void intel_pstate_update_perf_limits(struct cpudata *cpu,
2507 unsigned int policy_min,
2508 unsigned int policy_max)
2510 int perf_ctl_scaling = cpu->pstate.perf_ctl_scaling;
2511 int32_t max_policy_perf, min_policy_perf;
2513 max_policy_perf = policy_max / perf_ctl_scaling;
2514 if (policy_max == policy_min) {
2515 min_policy_perf = max_policy_perf;
2517 min_policy_perf = policy_min / perf_ctl_scaling;
2518 min_policy_perf = clamp_t(int32_t, min_policy_perf,
2519 0, max_policy_perf);
2523 * HWP needs some special consideration, because HWP_REQUEST uses
2524 * abstract values to represent performance rather than pure ratios.
2526 if (hwp_active && cpu->pstate.scaling != perf_ctl_scaling) {
2527 int scaling = cpu->pstate.scaling;
2530 freq = max_policy_perf * perf_ctl_scaling;
2531 max_policy_perf = DIV_ROUND_UP(freq, scaling);
2532 freq = min_policy_perf * perf_ctl_scaling;
2533 min_policy_perf = DIV_ROUND_UP(freq, scaling);
2536 pr_debug("cpu:%d min_policy_perf:%d max_policy_perf:%d\n",
2537 cpu->cpu, min_policy_perf, max_policy_perf);
2539 /* Normalize user input to [min_perf, max_perf] */
2540 if (per_cpu_limits) {
2541 cpu->min_perf_ratio = min_policy_perf;
2542 cpu->max_perf_ratio = max_policy_perf;
2544 int turbo_max = cpu->pstate.turbo_pstate;
2545 int32_t global_min, global_max;
2547 /* Global limits are in percent of the maximum turbo P-state. */
2548 global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2549 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2550 global_min = clamp_t(int32_t, global_min, 0, global_max);
2552 pr_debug("cpu:%d global_min:%d global_max:%d\n", cpu->cpu,
2553 global_min, global_max);
2555 cpu->min_perf_ratio = max(min_policy_perf, global_min);
2556 cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
2557 cpu->max_perf_ratio = min(max_policy_perf, global_max);
2558 cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
2560 /* Make sure min_perf <= max_perf */
2561 cpu->min_perf_ratio = min(cpu->min_perf_ratio,
2562 cpu->max_perf_ratio);
2565 pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", cpu->cpu,
2566 cpu->max_perf_ratio,
2567 cpu->min_perf_ratio);
2570 static int intel_pstate_set_policy(struct cpufreq_policy *policy)
2572 struct cpudata *cpu;
2574 if (!policy->cpuinfo.max_freq)
2577 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
2578 policy->cpuinfo.max_freq, policy->max);
2580 cpu = all_cpu_data[policy->cpu];
2581 cpu->policy = policy->policy;
2583 mutex_lock(&intel_pstate_limits_lock);
2585 intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
2587 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
2589 * NOHZ_FULL CPUs need this as the governor callback may not
2590 * be invoked on them.
2592 intel_pstate_clear_update_util_hook(policy->cpu);
2593 intel_pstate_max_within_limits(cpu);
2595 intel_pstate_set_update_util_hook(policy->cpu);
2600 * When hwp_boost was active before and dynamically it
2601 * was turned off, in that case we need to clear the
2605 intel_pstate_clear_update_util_hook(policy->cpu);
2606 intel_pstate_hwp_set(policy->cpu);
2609 * policy->cur is never updated with the intel_pstate driver, but it
2610 * is used as a stale frequency value. So, keep it within limits.
2612 policy->cur = policy->min;
2614 mutex_unlock(&intel_pstate_limits_lock);
2619 static void intel_pstate_adjust_policy_max(struct cpudata *cpu,
2620 struct cpufreq_policy_data *policy)
2623 cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
2624 policy->max < policy->cpuinfo.max_freq &&
2625 policy->max > cpu->pstate.max_freq) {
2626 pr_debug("policy->max > max non turbo frequency\n");
2627 policy->max = policy->cpuinfo.max_freq;
2631 static void intel_pstate_verify_cpu_policy(struct cpudata *cpu,
2632 struct cpufreq_policy_data *policy)
2636 update_turbo_state();
2638 intel_pstate_get_hwp_cap(cpu);
2639 max_freq = global.no_turbo || global.turbo_disabled ?
2640 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2642 max_freq = intel_pstate_get_max_freq(cpu);
2644 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, max_freq);
2646 intel_pstate_adjust_policy_max(cpu, policy);
2649 static int intel_pstate_verify_policy(struct cpufreq_policy_data *policy)
2651 intel_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
2656 static int intel_cpufreq_cpu_offline(struct cpufreq_policy *policy)
2658 struct cpudata *cpu = all_cpu_data[policy->cpu];
2660 pr_debug("CPU %d going offline\n", cpu->cpu);
2666 * If the CPU is an SMT thread and it goes offline with the performance
2667 * settings different from the minimum, it will prevent its sibling
2668 * from getting to lower performance levels, so force the minimum
2669 * performance on CPU offline to prevent that from happening.
2672 intel_pstate_hwp_offline(cpu);
2674 intel_pstate_set_min_pstate(cpu);
2676 intel_pstate_exit_perf_limits(policy);
2681 static int intel_pstate_cpu_online(struct cpufreq_policy *policy)
2683 struct cpudata *cpu = all_cpu_data[policy->cpu];
2685 pr_debug("CPU %d going online\n", cpu->cpu);
2687 intel_pstate_init_acpi_perf_limits(policy);
2691 * Re-enable HWP and clear the "suspended" flag to let "resume"
2692 * know that it need not do that.
2694 intel_pstate_hwp_reenable(cpu);
2695 cpu->suspended = false;
2701 static int intel_pstate_cpu_offline(struct cpufreq_policy *policy)
2703 intel_pstate_clear_update_util_hook(policy->cpu);
2705 return intel_cpufreq_cpu_offline(policy);
2708 static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
2710 pr_debug("CPU %d exiting\n", policy->cpu);
2712 policy->fast_switch_possible = false;
2717 static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
2719 struct cpudata *cpu;
2722 rc = intel_pstate_init_cpu(policy->cpu);
2726 cpu = all_cpu_data[policy->cpu];
2728 cpu->max_perf_ratio = 0xFF;
2729 cpu->min_perf_ratio = 0;
2731 /* cpuinfo and default policy values */
2732 policy->cpuinfo.min_freq = cpu->pstate.min_freq;
2733 update_turbo_state();
2734 global.turbo_disabled_mf = global.turbo_disabled;
2735 policy->cpuinfo.max_freq = global.turbo_disabled ?
2736 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2738 policy->min = policy->cpuinfo.min_freq;
2739 policy->max = policy->cpuinfo.max_freq;
2741 intel_pstate_init_acpi_perf_limits(policy);
2743 policy->fast_switch_possible = true;
2748 static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
2750 int ret = __intel_pstate_cpu_init(policy);
2756 * Set the policy to powersave to provide a valid fallback value in case
2757 * the default cpufreq governor is neither powersave nor performance.
2759 policy->policy = CPUFREQ_POLICY_POWERSAVE;
2762 struct cpudata *cpu = all_cpu_data[policy->cpu];
2764 cpu->epp_cached = intel_pstate_get_epp(cpu, 0);
2770 static struct cpufreq_driver intel_pstate = {
2771 .flags = CPUFREQ_CONST_LOOPS,
2772 .verify = intel_pstate_verify_policy,
2773 .setpolicy = intel_pstate_set_policy,
2774 .suspend = intel_pstate_suspend,
2775 .resume = intel_pstate_resume,
2776 .init = intel_pstate_cpu_init,
2777 .exit = intel_pstate_cpu_exit,
2778 .offline = intel_pstate_cpu_offline,
2779 .online = intel_pstate_cpu_online,
2780 .update_limits = intel_pstate_update_limits,
2781 .name = "intel_pstate",
2784 static int intel_cpufreq_verify_policy(struct cpufreq_policy_data *policy)
2786 struct cpudata *cpu = all_cpu_data[policy->cpu];
2788 intel_pstate_verify_cpu_policy(cpu, policy);
2789 intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
2794 /* Use of trace in passive mode:
2796 * In passive mode the trace core_busy field (also known as the
2797 * performance field, and lablelled as such on the graphs; also known as
2798 * core_avg_perf) is not needed and so is re-assigned to indicate if the
2799 * driver call was via the normal or fast switch path. Various graphs
2800 * output from the intel_pstate_tracer.py utility that include core_busy
2801 * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%,
2802 * so we use 10 to indicate the normal path through the driver, and
2803 * 90 to indicate the fast switch path through the driver.
2804 * The scaled_busy field is not used, and is set to 0.
2807 #define INTEL_PSTATE_TRACE_TARGET 10
2808 #define INTEL_PSTATE_TRACE_FAST_SWITCH 90
2810 static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate)
2812 struct sample *sample;
2814 if (!trace_pstate_sample_enabled())
2817 if (!intel_pstate_sample(cpu, ktime_get()))
2820 sample = &cpu->sample;
2821 trace_pstate_sample(trace_type,
2824 cpu->pstate.current_pstate,
2828 get_avg_frequency(cpu),
2829 fp_toint(cpu->iowait_boost * 100));
2832 static void intel_cpufreq_hwp_update(struct cpudata *cpu, u32 min, u32 max,
2833 u32 desired, bool fast_switch)
2835 u64 prev = READ_ONCE(cpu->hwp_req_cached), value = prev;
2837 value &= ~HWP_MIN_PERF(~0L);
2838 value |= HWP_MIN_PERF(min);
2840 value &= ~HWP_MAX_PERF(~0L);
2841 value |= HWP_MAX_PERF(max);
2843 value &= ~HWP_DESIRED_PERF(~0L);
2844 value |= HWP_DESIRED_PERF(desired);
2849 WRITE_ONCE(cpu->hwp_req_cached, value);
2851 wrmsrl(MSR_HWP_REQUEST, value);
2853 wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
2856 static void intel_cpufreq_perf_ctl_update(struct cpudata *cpu,
2857 u32 target_pstate, bool fast_switch)
2860 wrmsrl(MSR_IA32_PERF_CTL,
2861 pstate_funcs.get_val(cpu, target_pstate));
2863 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
2864 pstate_funcs.get_val(cpu, target_pstate));
2867 static int intel_cpufreq_update_pstate(struct cpufreq_policy *policy,
2868 int target_pstate, bool fast_switch)
2870 struct cpudata *cpu = all_cpu_data[policy->cpu];
2871 int old_pstate = cpu->pstate.current_pstate;
2873 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2875 int max_pstate = policy->strict_target ?
2876 target_pstate : cpu->max_perf_ratio;
2878 intel_cpufreq_hwp_update(cpu, target_pstate, max_pstate, 0,
2880 } else if (target_pstate != old_pstate) {
2881 intel_cpufreq_perf_ctl_update(cpu, target_pstate, fast_switch);
2884 cpu->pstate.current_pstate = target_pstate;
2886 intel_cpufreq_trace(cpu, fast_switch ? INTEL_PSTATE_TRACE_FAST_SWITCH :
2887 INTEL_PSTATE_TRACE_TARGET, old_pstate);
2889 return target_pstate;
2892 static int intel_cpufreq_target(struct cpufreq_policy *policy,
2893 unsigned int target_freq,
2894 unsigned int relation)
2896 struct cpudata *cpu = all_cpu_data[policy->cpu];
2897 struct cpufreq_freqs freqs;
2900 update_turbo_state();
2902 freqs.old = policy->cur;
2903 freqs.new = target_freq;
2905 cpufreq_freq_transition_begin(policy, &freqs);
2908 case CPUFREQ_RELATION_L:
2909 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
2911 case CPUFREQ_RELATION_H:
2912 target_pstate = freqs.new / cpu->pstate.scaling;
2915 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
2919 target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, false);
2921 freqs.new = target_pstate * cpu->pstate.scaling;
2923 cpufreq_freq_transition_end(policy, &freqs, false);
2928 static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
2929 unsigned int target_freq)
2931 struct cpudata *cpu = all_cpu_data[policy->cpu];
2934 update_turbo_state();
2936 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
2938 target_pstate = intel_cpufreq_update_pstate(policy, target_pstate, true);
2940 return target_pstate * cpu->pstate.scaling;
2943 static void intel_cpufreq_adjust_perf(unsigned int cpunum,
2944 unsigned long min_perf,
2945 unsigned long target_perf,
2946 unsigned long capacity)
2948 struct cpudata *cpu = all_cpu_data[cpunum];
2949 u64 hwp_cap = READ_ONCE(cpu->hwp_cap_cached);
2950 int old_pstate = cpu->pstate.current_pstate;
2951 int cap_pstate, min_pstate, max_pstate, target_pstate;
2953 update_turbo_state();
2954 cap_pstate = global.turbo_disabled ? HWP_GUARANTEED_PERF(hwp_cap) :
2955 HWP_HIGHEST_PERF(hwp_cap);
2957 /* Optimization: Avoid unnecessary divisions. */
2959 target_pstate = cap_pstate;
2960 if (target_perf < capacity)
2961 target_pstate = DIV_ROUND_UP(cap_pstate * target_perf, capacity);
2963 min_pstate = cap_pstate;
2964 if (min_perf < capacity)
2965 min_pstate = DIV_ROUND_UP(cap_pstate * min_perf, capacity);
2967 if (min_pstate < cpu->pstate.min_pstate)
2968 min_pstate = cpu->pstate.min_pstate;
2970 if (min_pstate < cpu->min_perf_ratio)
2971 min_pstate = cpu->min_perf_ratio;
2973 max_pstate = min(cap_pstate, cpu->max_perf_ratio);
2974 if (max_pstate < min_pstate)
2975 max_pstate = min_pstate;
2977 target_pstate = clamp_t(int, target_pstate, min_pstate, max_pstate);
2979 intel_cpufreq_hwp_update(cpu, min_pstate, max_pstate, target_pstate, true);
2981 cpu->pstate.current_pstate = target_pstate;
2982 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate);
2985 static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
2987 struct freq_qos_request *req;
2988 struct cpudata *cpu;
2992 dev = get_cpu_device(policy->cpu);
2996 ret = __intel_pstate_cpu_init(policy);
3000 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
3001 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
3002 policy->cur = policy->cpuinfo.min_freq;
3004 req = kcalloc(2, sizeof(*req), GFP_KERNEL);
3010 cpu = all_cpu_data[policy->cpu];
3015 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY_HWP;
3017 intel_pstate_get_hwp_cap(cpu);
3019 rdmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, &value);
3020 WRITE_ONCE(cpu->hwp_req_cached, value);
3022 cpu->epp_cached = intel_pstate_get_epp(cpu, value);
3024 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
3027 freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.min_perf_pct, 100);
3029 ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN,
3032 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
3036 freq = DIV_ROUND_UP(cpu->pstate.turbo_freq * global.max_perf_pct, 100);
3038 ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX,
3041 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
3042 goto remove_min_req;
3045 policy->driver_data = req;
3050 freq_qos_remove_request(req);
3054 intel_pstate_exit_perf_limits(policy);
3059 static int intel_cpufreq_cpu_exit(struct cpufreq_policy *policy)
3061 struct freq_qos_request *req;
3063 req = policy->driver_data;
3065 freq_qos_remove_request(req + 1);
3066 freq_qos_remove_request(req);
3069 return intel_pstate_cpu_exit(policy);
3072 static int intel_cpufreq_suspend(struct cpufreq_policy *policy)
3074 intel_pstate_suspend(policy);
3077 struct cpudata *cpu = all_cpu_data[policy->cpu];
3078 u64 value = READ_ONCE(cpu->hwp_req_cached);
3081 * Clear the desired perf field in MSR_HWP_REQUEST in case
3082 * intel_cpufreq_adjust_perf() is in use and the last value
3083 * written by it may not be suitable.
3085 value &= ~HWP_DESIRED_PERF(~0L);
3086 wrmsrl_on_cpu(cpu->cpu, MSR_HWP_REQUEST, value);
3087 WRITE_ONCE(cpu->hwp_req_cached, value);
3093 static struct cpufreq_driver intel_cpufreq = {
3094 .flags = CPUFREQ_CONST_LOOPS,
3095 .verify = intel_cpufreq_verify_policy,
3096 .target = intel_cpufreq_target,
3097 .fast_switch = intel_cpufreq_fast_switch,
3098 .init = intel_cpufreq_cpu_init,
3099 .exit = intel_cpufreq_cpu_exit,
3100 .offline = intel_cpufreq_cpu_offline,
3101 .online = intel_pstate_cpu_online,
3102 .suspend = intel_cpufreq_suspend,
3103 .resume = intel_pstate_resume,
3104 .update_limits = intel_pstate_update_limits,
3105 .name = "intel_cpufreq",
3108 static struct cpufreq_driver *default_driver;
3110 static void intel_pstate_driver_cleanup(void)
3115 for_each_online_cpu(cpu) {
3116 if (all_cpu_data[cpu]) {
3117 if (intel_pstate_driver == &intel_pstate)
3118 intel_pstate_clear_update_util_hook(cpu);
3120 spin_lock(&hwp_notify_lock);
3121 kfree(all_cpu_data[cpu]);
3122 WRITE_ONCE(all_cpu_data[cpu], NULL);
3123 spin_unlock(&hwp_notify_lock);
3128 intel_pstate_driver = NULL;
3131 static int intel_pstate_register_driver(struct cpufreq_driver *driver)
3135 if (driver == &intel_pstate)
3136 intel_pstate_sysfs_expose_hwp_dynamic_boost();
3138 memset(&global, 0, sizeof(global));
3139 global.max_perf_pct = 100;
3141 intel_pstate_driver = driver;
3142 ret = cpufreq_register_driver(intel_pstate_driver);
3144 intel_pstate_driver_cleanup();
3148 global.min_perf_pct = min_perf_pct_min();
3153 static ssize_t intel_pstate_show_status(char *buf)
3155 if (!intel_pstate_driver)
3156 return sprintf(buf, "off\n");
3158 return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
3159 "active" : "passive");
3162 static int intel_pstate_update_status(const char *buf, size_t size)
3164 if (size == 3 && !strncmp(buf, "off", size)) {
3165 if (!intel_pstate_driver)
3171 cpufreq_unregister_driver(intel_pstate_driver);
3172 intel_pstate_driver_cleanup();
3176 if (size == 6 && !strncmp(buf, "active", size)) {
3177 if (intel_pstate_driver) {
3178 if (intel_pstate_driver == &intel_pstate)
3181 cpufreq_unregister_driver(intel_pstate_driver);
3184 return intel_pstate_register_driver(&intel_pstate);
3187 if (size == 7 && !strncmp(buf, "passive", size)) {
3188 if (intel_pstate_driver) {
3189 if (intel_pstate_driver == &intel_cpufreq)
3192 cpufreq_unregister_driver(intel_pstate_driver);
3193 intel_pstate_sysfs_hide_hwp_dynamic_boost();
3196 return intel_pstate_register_driver(&intel_cpufreq);
3202 static int no_load __initdata;
3203 static int no_hwp __initdata;
3204 static int hwp_only __initdata;
3205 static unsigned int force_load __initdata;
3207 static int __init intel_pstate_msrs_not_valid(void)
3209 if (!pstate_funcs.get_max(0) ||
3210 !pstate_funcs.get_min(0) ||
3211 !pstate_funcs.get_turbo(0))
3217 static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
3219 pstate_funcs.get_max = funcs->get_max;
3220 pstate_funcs.get_max_physical = funcs->get_max_physical;
3221 pstate_funcs.get_min = funcs->get_min;
3222 pstate_funcs.get_turbo = funcs->get_turbo;
3223 pstate_funcs.get_scaling = funcs->get_scaling;
3224 pstate_funcs.get_val = funcs->get_val;
3225 pstate_funcs.get_vid = funcs->get_vid;
3226 pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
3231 static bool __init intel_pstate_no_acpi_pss(void)
3235 for_each_possible_cpu(i) {
3237 union acpi_object *pss;
3238 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3239 struct acpi_processor *pr = per_cpu(processors, i);
3244 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
3245 if (ACPI_FAILURE(status))
3248 pss = buffer.pointer;
3249 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
3257 pr_debug("ACPI _PSS not found\n");
3261 static bool __init intel_pstate_no_acpi_pcch(void)
3266 status = acpi_get_handle(NULL, "\\_SB", &handle);
3267 if (ACPI_FAILURE(status))
3270 if (acpi_has_method(handle, "PCCH"))
3274 pr_debug("ACPI PCCH not found\n");
3278 static bool __init intel_pstate_has_acpi_ppc(void)
3282 for_each_possible_cpu(i) {
3283 struct acpi_processor *pr = per_cpu(processors, i);
3287 if (acpi_has_method(pr->handle, "_PPC"))
3290 pr_debug("ACPI _PPC not found\n");
3299 /* Hardware vendor-specific info that has its own power management modes */
3300 static struct acpi_platform_list plat_info[] __initdata = {
3301 {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, NULL, PSS},
3302 {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3303 {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3304 {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3305 {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3306 {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3307 {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3308 {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3309 {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3310 {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3311 {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3312 {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3313 {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3314 {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3315 {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
3319 #define BITMASK_OOB (BIT(8) | BIT(18))
3321 static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
3323 const struct x86_cpu_id *id;
3327 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
3329 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
3330 if (misc_pwr & BITMASK_OOB) {
3331 pr_debug("Bit 8 or 18 in the MISC_PWR_MGMT MSR set\n");
3332 pr_debug("P states are controlled in Out of Band mode by the firmware/hardware\n");
3337 idx = acpi_match_platform_list(plat_info);
3341 switch (plat_info[idx].data) {
3343 if (!intel_pstate_no_acpi_pss())
3346 return intel_pstate_no_acpi_pcch();
3348 return intel_pstate_has_acpi_ppc() && !force_load;
3354 static void intel_pstate_request_control_from_smm(void)
3357 * It may be unsafe to request P-states control from SMM if _PPC support
3358 * has not been enabled.
3361 acpi_processor_pstate_control();
3363 #else /* CONFIG_ACPI not enabled */
3364 static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
3365 static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
3366 static inline void intel_pstate_request_control_from_smm(void) {}
3367 #endif /* CONFIG_ACPI */
3369 #define INTEL_PSTATE_HWP_BROADWELL 0x01
3371 #define X86_MATCH_HWP(model, hwp_mode) \
3372 X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
3373 X86_FEATURE_HWP, hwp_mode)
3375 static const struct x86_cpu_id hwp_support_ids[] __initconst = {
3376 X86_MATCH_HWP(BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL),
3377 X86_MATCH_HWP(BROADWELL_D, INTEL_PSTATE_HWP_BROADWELL),
3378 X86_MATCH_HWP(ANY, 0),
3382 static bool intel_pstate_hwp_is_enabled(void)
3386 rdmsrl(MSR_PM_ENABLE, value);
3387 return !!(value & 0x1);
3390 static const struct x86_cpu_id intel_epp_balance_perf[] = {
3392 * Set EPP value as 102, this is the max suggested EPP
3393 * which can result in one core turbo frequency for
3394 * AlderLake Mobile CPUs.
3396 X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_L, 102),
3397 X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, 32),
3401 static int __init intel_pstate_init(void)
3403 static struct cpudata **_all_cpu_data;
3404 const struct x86_cpu_id *id;
3407 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
3410 id = x86_match_cpu(hwp_support_ids);
3412 hwp_forced = intel_pstate_hwp_is_enabled();
3415 pr_info("HWP enabled by BIOS\n");
3419 copy_cpu_funcs(&core_funcs);
3421 * Avoid enabling HWP for processors without EPP support,
3422 * because that means incomplete HWP implementation which is a
3423 * corner case and supporting it is generally problematic.
3425 * If HWP is enabled already, though, there is no choice but to
3428 if ((!no_hwp && boot_cpu_has(X86_FEATURE_HWP_EPP)) || hwp_forced) {
3429 WRITE_ONCE(hwp_active, 1);
3430 hwp_mode_bdw = id->driver_data;
3431 intel_pstate.attr = hwp_cpufreq_attrs;
3432 intel_cpufreq.attr = hwp_cpufreq_attrs;
3433 intel_cpufreq.flags |= CPUFREQ_NEED_UPDATE_LIMITS;
3434 intel_cpufreq.adjust_perf = intel_cpufreq_adjust_perf;
3435 if (!default_driver)
3436 default_driver = &intel_pstate;
3438 pstate_funcs.get_cpu_scaling = hwp_get_cpu_scaling;
3440 goto hwp_cpu_matched;
3442 pr_info("HWP not enabled\n");
3447 id = x86_match_cpu(intel_pstate_cpu_ids);
3449 pr_info("CPU model not supported\n");
3453 copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
3456 if (intel_pstate_msrs_not_valid()) {
3457 pr_info("Invalid MSRs\n");
3460 /* Without HWP start in the passive mode. */
3461 if (!default_driver)
3462 default_driver = &intel_cpufreq;
3466 * The Intel pstate driver will be ignored if the platform
3467 * firmware has its own power management modes.
3469 if (intel_pstate_platform_pwr_mgmt_exists()) {
3470 pr_info("P-states controlled by the platform\n");
3474 if (!hwp_active && hwp_only)
3477 pr_info("Intel P-state driver initializing\n");
3479 _all_cpu_data = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
3483 WRITE_ONCE(all_cpu_data, _all_cpu_data);
3485 intel_pstate_request_control_from_smm();
3487 intel_pstate_sysfs_expose_params();
3490 const struct x86_cpu_id *id = x86_match_cpu(intel_epp_balance_perf);
3493 epp_values[EPP_INDEX_BALANCE_PERFORMANCE] = id->driver_data;
3496 mutex_lock(&intel_pstate_driver_lock);
3497 rc = intel_pstate_register_driver(default_driver);
3498 mutex_unlock(&intel_pstate_driver_lock);
3500 intel_pstate_sysfs_remove();
3505 const struct x86_cpu_id *id;
3507 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
3509 set_power_ctl_ee_state(false);
3510 pr_info("Disabling energy efficiency optimization\n");
3513 pr_info("HWP enabled\n");
3514 } else if (boot_cpu_has(X86_FEATURE_HYBRID_CPU)) {
3515 pr_warn("Problematic setup: Hybrid processor with disabled HWP\n");
3520 device_initcall(intel_pstate_init);
3522 static int __init intel_pstate_setup(char *str)
3527 if (!strcmp(str, "disable"))
3529 else if (!strcmp(str, "active"))
3530 default_driver = &intel_pstate;
3531 else if (!strcmp(str, "passive"))
3532 default_driver = &intel_cpufreq;
3534 if (!strcmp(str, "no_hwp"))
3537 if (!strcmp(str, "force"))
3539 if (!strcmp(str, "hwp_only"))
3541 if (!strcmp(str, "per_cpu_perf_limits"))
3542 per_cpu_limits = true;
3545 if (!strcmp(str, "support_acpi_ppc"))
3551 early_param("intel_pstate", intel_pstate_setup);
3553 MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
3554 MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");