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>
30 #include <asm/div64.h>
32 #include <asm/cpu_device_id.h>
33 #include <asm/cpufeature.h>
34 #include <asm/intel-family.h>
36 #define INTEL_PSTATE_SAMPLING_INTERVAL (10 * NSEC_PER_MSEC)
38 #define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
39 #define INTEL_CPUFREQ_TRANSITION_DELAY 500
42 #include <acpi/processor.h>
43 #include <acpi/cppc_acpi.h>
47 #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
48 #define fp_toint(X) ((X) >> FRAC_BITS)
50 #define ONE_EIGHTH_FP ((int64_t)1 << (FRAC_BITS - 3))
53 #define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
54 #define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
55 #define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
57 static inline int32_t mul_fp(int32_t x, int32_t y)
59 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
62 static inline int32_t div_fp(s64 x, s64 y)
64 return div64_s64((int64_t)x << FRAC_BITS, y);
67 static inline int ceiling_fp(int32_t x)
72 mask = (1 << FRAC_BITS) - 1;
78 static inline int32_t percent_fp(int percent)
80 return div_fp(percent, 100);
83 static inline u64 mul_ext_fp(u64 x, u64 y)
85 return (x * y) >> EXT_FRAC_BITS;
88 static inline u64 div_ext_fp(u64 x, u64 y)
90 return div64_u64(x << EXT_FRAC_BITS, y);
93 static inline int32_t percent_ext_fp(int percent)
95 return div_ext_fp(percent, 100);
99 * struct sample - Store performance sample
100 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
101 * performance during last sample period
102 * @busy_scaled: Scaled busy value which is used to calculate next
103 * P state. This can be different than core_avg_perf
104 * to account for cpu idle period
105 * @aperf: Difference of actual performance frequency clock count
106 * read from APERF MSR between last and current sample
107 * @mperf: Difference of maximum performance frequency clock count
108 * read from MPERF MSR between last and current sample
109 * @tsc: Difference of time stamp counter between last and
111 * @time: Current time from scheduler
113 * This structure is used in the cpudata structure to store performance sample
114 * data for choosing next P State.
117 int32_t core_avg_perf;
126 * struct pstate_data - Store P state data
127 * @current_pstate: Current requested P state
128 * @min_pstate: Min P state possible for this platform
129 * @max_pstate: Max P state possible for this platform
130 * @max_pstate_physical:This is physical Max P state for a processor
131 * This can be higher than the max_pstate which can
132 * be limited by platform thermal design power limits
133 * @scaling: Scaling factor to convert frequency to cpufreq
135 * @turbo_pstate: Max Turbo P state possible for this platform
136 * @max_freq: @max_pstate frequency in cpufreq units
137 * @turbo_freq: @turbo_pstate frequency in cpufreq units
139 * Stores the per cpu model P state limits and current P state.
145 int max_pstate_physical;
148 unsigned int max_freq;
149 unsigned int turbo_freq;
153 * struct vid_data - Stores voltage information data
154 * @min: VID data for this platform corresponding to
156 * @max: VID data corresponding to the highest P State.
157 * @turbo: VID data for turbo P state
158 * @ratio: Ratio of (vid max - vid min) /
159 * (max P state - Min P State)
161 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
162 * This data is used in Atom platforms, where in addition to target P state,
163 * the voltage data needs to be specified to select next P State.
173 * struct global_params - Global parameters, mostly tunable via sysfs.
174 * @no_turbo: Whether or not to use turbo P-states.
175 * @turbo_disabled: Whether or not turbo P-states are available at all,
176 * based on the MSR_IA32_MISC_ENABLE value and whether or
177 * not the maximum reported turbo P-state is different from
178 * the maximum reported non-turbo one.
179 * @turbo_disabled_mf: The @turbo_disabled value reflected by cpuinfo.max_freq.
180 * @min_perf_pct: Minimum capacity limit in percent of the maximum turbo
182 * @max_perf_pct: Maximum capacity limit in percent of the maximum turbo
185 struct global_params {
188 bool turbo_disabled_mf;
194 * struct cpudata - Per CPU instance data storage
195 * @cpu: CPU number for this instance data
196 * @policy: CPUFreq policy value
197 * @update_util: CPUFreq utility callback information
198 * @update_util_set: CPUFreq utility callback is set
199 * @iowait_boost: iowait-related boost fraction
200 * @last_update: Time of the last update.
201 * @pstate: Stores P state limits for this CPU
202 * @vid: Stores VID limits for this CPU
203 * @last_sample_time: Last Sample time
204 * @aperf_mperf_shift: APERF vs MPERF counting frequency difference
205 * @prev_aperf: Last APERF value read from APERF MSR
206 * @prev_mperf: Last MPERF value read from MPERF MSR
207 * @prev_tsc: Last timestamp counter (TSC) value
208 * @prev_cummulative_iowait: IO Wait time difference from last and
210 * @sample: Storage for storing last Sample data
211 * @min_perf_ratio: Minimum capacity in terms of PERF or HWP ratios
212 * @max_perf_ratio: Maximum capacity in terms of PERF or HWP ratios
213 * @acpi_perf_data: Stores ACPI perf information read from _PSS
214 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
215 * @epp_powersave: Last saved HWP energy performance preference
216 * (EPP) or energy performance bias (EPB),
217 * when policy switched to performance
218 * @epp_policy: Last saved policy used to set EPP/EPB
219 * @epp_default: Power on default HWP energy performance
221 * @epp_saved: Saved EPP/EPB during system suspend or CPU offline
223 * @hwp_req_cached: Cached value of the last HWP Request MSR
224 * @hwp_cap_cached: Cached value of the last HWP Capabilities MSR
225 * @last_io_update: Last time when IO wake flag was set
226 * @sched_flags: Store scheduler flags for possible cross CPU update
227 * @hwp_boost_min: Last HWP boosted min performance
229 * This structure stores per CPU instance data for all CPUs.
235 struct update_util_data update_util;
236 bool update_util_set;
238 struct pstate_data pstate;
242 u64 last_sample_time;
243 u64 aperf_mperf_shift;
247 u64 prev_cummulative_iowait;
248 struct sample sample;
249 int32_t min_perf_ratio;
250 int32_t max_perf_ratio;
252 struct acpi_processor_performance acpi_perf_data;
253 bool valid_pss_table;
255 unsigned int iowait_boost;
263 unsigned int sched_flags;
267 static struct cpudata **all_cpu_data;
270 * struct pstate_funcs - Per CPU model specific callbacks
271 * @get_max: Callback to get maximum non turbo effective P state
272 * @get_max_physical: Callback to get maximum non turbo physical P state
273 * @get_min: Callback to get minimum P state
274 * @get_turbo: Callback to get turbo P state
275 * @get_scaling: Callback to get frequency scaling factor
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)(void);
285 int (*get_max_physical)(void);
286 int (*get_min)(void);
287 int (*get_turbo)(void);
288 int (*get_scaling)(void);
289 int (*get_aperf_mperf_shift)(void);
290 u64 (*get_val)(struct cpudata*, int pstate);
291 void (*get_vid)(struct cpudata *);
294 static struct pstate_funcs pstate_funcs __read_mostly;
296 static int hwp_active __read_mostly;
297 static int hwp_mode_bdw __read_mostly;
298 static bool per_cpu_limits __read_mostly;
299 static bool hwp_boost __read_mostly;
301 static struct cpufreq_driver *intel_pstate_driver __read_mostly;
304 static bool acpi_ppc;
307 static struct global_params global;
309 static DEFINE_MUTEX(intel_pstate_driver_lock);
310 static DEFINE_MUTEX(intel_pstate_limits_lock);
314 static bool intel_pstate_acpi_pm_profile_server(void)
316 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
317 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
323 static bool intel_pstate_get_ppc_enable_status(void)
325 if (intel_pstate_acpi_pm_profile_server())
331 #ifdef CONFIG_ACPI_CPPC_LIB
333 /* The work item is needed to avoid CPU hotplug locking issues */
334 static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
336 sched_set_itmt_support();
339 static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
341 static void intel_pstate_set_itmt_prio(int cpu)
343 struct cppc_perf_caps cppc_perf;
344 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
347 ret = cppc_get_perf_caps(cpu, &cppc_perf);
352 * The priorities can be set regardless of whether or not
353 * sched_set_itmt_support(true) has been called and it is valid to
354 * update them at any time after it has been called.
356 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
358 if (max_highest_perf <= min_highest_perf) {
359 if (cppc_perf.highest_perf > max_highest_perf)
360 max_highest_perf = cppc_perf.highest_perf;
362 if (cppc_perf.highest_perf < min_highest_perf)
363 min_highest_perf = cppc_perf.highest_perf;
365 if (max_highest_perf > min_highest_perf) {
367 * This code can be run during CPU online under the
368 * CPU hotplug locks, so sched_set_itmt_support()
369 * cannot be called from here. Queue up a work item
372 schedule_work(&sched_itmt_work);
377 static int intel_pstate_get_cppc_guranteed(int cpu)
379 struct cppc_perf_caps cppc_perf;
382 ret = cppc_get_perf_caps(cpu, &cppc_perf);
386 if (cppc_perf.guaranteed_perf)
387 return cppc_perf.guaranteed_perf;
389 return cppc_perf.nominal_perf;
392 #else /* CONFIG_ACPI_CPPC_LIB */
393 static void intel_pstate_set_itmt_prio(int cpu)
396 #endif /* CONFIG_ACPI_CPPC_LIB */
398 static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
405 intel_pstate_set_itmt_prio(policy->cpu);
409 if (!intel_pstate_get_ppc_enable_status())
412 cpu = all_cpu_data[policy->cpu];
414 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
420 * Check if the control value in _PSS is for PERF_CTL MSR, which should
421 * guarantee that the states returned by it map to the states in our
424 if (cpu->acpi_perf_data.control_register.space_id !=
425 ACPI_ADR_SPACE_FIXED_HARDWARE)
429 * If there is only one entry _PSS, simply ignore _PSS and continue as
430 * usual without taking _PSS into account
432 if (cpu->acpi_perf_data.state_count < 2)
435 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
436 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
437 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
438 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
439 (u32) cpu->acpi_perf_data.states[i].core_frequency,
440 (u32) cpu->acpi_perf_data.states[i].power,
441 (u32) cpu->acpi_perf_data.states[i].control);
445 * The _PSS table doesn't contain whole turbo frequency range.
446 * This just contains +1 MHZ above the max non turbo frequency,
447 * with control value corresponding to max turbo ratio. But
448 * when cpufreq set policy is called, it will call with this
449 * max frequency, which will cause a reduced performance as
450 * this driver uses real max turbo frequency as the max
451 * frequency. So correct this frequency in _PSS table to
452 * correct max turbo frequency based on the turbo state.
453 * Also need to convert to MHz as _PSS freq is in MHz.
455 if (!global.turbo_disabled)
456 cpu->acpi_perf_data.states[0].core_frequency =
457 policy->cpuinfo.max_freq / 1000;
458 cpu->valid_pss_table = true;
459 pr_debug("_PPC limits will be enforced\n");
464 cpu->valid_pss_table = false;
465 acpi_processor_unregister_performance(policy->cpu);
468 static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
472 cpu = all_cpu_data[policy->cpu];
473 if (!cpu->valid_pss_table)
476 acpi_processor_unregister_performance(policy->cpu);
478 #else /* CONFIG_ACPI */
479 static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
483 static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
487 static inline bool intel_pstate_acpi_pm_profile_server(void)
491 #endif /* CONFIG_ACPI */
493 #ifndef CONFIG_ACPI_CPPC_LIB
494 static int intel_pstate_get_cppc_guranteed(int cpu)
498 #endif /* CONFIG_ACPI_CPPC_LIB */
500 static inline void update_turbo_state(void)
505 cpu = all_cpu_data[0];
506 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
507 global.turbo_disabled =
508 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
509 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
512 static int min_perf_pct_min(void)
514 struct cpudata *cpu = all_cpu_data[0];
515 int turbo_pstate = cpu->pstate.turbo_pstate;
517 return turbo_pstate ?
518 (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
521 static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
526 if (!boot_cpu_has(X86_FEATURE_EPB))
529 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
533 return (s16)(epb & 0x0f);
536 static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
540 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
542 * When hwp_req_data is 0, means that caller didn't read
543 * MSR_HWP_REQUEST, so need to read and get EPP.
546 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
551 epp = (hwp_req_data >> 24) & 0xff;
553 /* When there is no EPP present, HWP uses EPB settings */
554 epp = intel_pstate_get_epb(cpu_data);
560 static int intel_pstate_set_epb(int cpu, s16 pref)
565 if (!boot_cpu_has(X86_FEATURE_EPB))
568 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
572 epb = (epb & ~0x0f) | pref;
573 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
579 * EPP/EPB display strings corresponding to EPP index in the
580 * energy_perf_strings[]
582 *-------------------------------------
585 * 2 balance_performance
589 static const char * const energy_perf_strings[] = {
592 "balance_performance",
597 static const unsigned int epp_values[] = {
599 HWP_EPP_BALANCE_PERFORMANCE,
600 HWP_EPP_BALANCE_POWERSAVE,
604 static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data, int *raw_epp)
610 epp = intel_pstate_get_epp(cpu_data, 0);
614 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
615 if (epp == HWP_EPP_PERFORMANCE)
617 if (epp == HWP_EPP_BALANCE_PERFORMANCE)
619 if (epp == HWP_EPP_BALANCE_POWERSAVE)
621 if (epp == HWP_EPP_POWERSAVE)
625 } else if (boot_cpu_has(X86_FEATURE_EPB)) {
628 * 0x00-0x03 : Performance
629 * 0x04-0x07 : Balance performance
630 * 0x08-0x0B : Balance power
632 * The EPB is a 4 bit value, but our ranges restrict the
633 * value which can be set. Here only using top two bits
636 index = (epp >> 2) + 1;
642 static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
643 int pref_index, bool use_raw,
650 epp = cpu_data->epp_default;
652 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
654 * Use the cached HWP Request MSR value, because the register
655 * itself may be updated by intel_pstate_hwp_boost_up() or
656 * intel_pstate_hwp_boost_down() at any time.
658 u64 value = READ_ONCE(cpu_data->hwp_req_cached);
660 value &= ~GENMASK_ULL(31, 24);
664 else if (epp == -EINVAL)
665 epp = epp_values[pref_index - 1];
667 value |= (u64)epp << 24;
669 * The only other updater of hwp_req_cached in the active mode,
670 * intel_pstate_hwp_set(), is called under the same lock as this
671 * function, so it cannot run in parallel with the update below.
673 WRITE_ONCE(cpu_data->hwp_req_cached, value);
674 ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
677 epp = (pref_index - 1) << 2;
678 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
684 static ssize_t show_energy_performance_available_preferences(
685 struct cpufreq_policy *policy, char *buf)
690 while (energy_perf_strings[i] != NULL)
691 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
693 ret += sprintf(&buf[ret], "\n");
698 cpufreq_freq_attr_ro(energy_performance_available_preferences);
700 static ssize_t store_energy_performance_preference(
701 struct cpufreq_policy *policy, const char *buf, size_t count)
703 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
704 char str_preference[21];
709 ret = sscanf(buf, "%20s", str_preference);
713 ret = match_string(energy_perf_strings, -1, str_preference);
715 if (!boot_cpu_has(X86_FEATURE_HWP_EPP))
718 ret = kstrtouint(buf, 10, &epp);
728 mutex_lock(&intel_pstate_limits_lock);
730 ret = intel_pstate_set_energy_pref_index(cpu_data, ret, raw, epp);
734 mutex_unlock(&intel_pstate_limits_lock);
739 static ssize_t show_energy_performance_preference(
740 struct cpufreq_policy *policy, char *buf)
742 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
743 int preference, raw_epp;
745 preference = intel_pstate_get_energy_pref_index(cpu_data, &raw_epp);
750 return sprintf(buf, "%d\n", raw_epp);
752 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
755 cpufreq_freq_attr_rw(energy_performance_preference);
757 static ssize_t show_base_frequency(struct cpufreq_policy *policy, char *buf)
763 ratio = intel_pstate_get_cppc_guranteed(policy->cpu);
765 rdmsrl_on_cpu(policy->cpu, MSR_HWP_CAPABILITIES, &cap);
766 ratio = HWP_GUARANTEED_PERF(cap);
769 cpu = all_cpu_data[policy->cpu];
771 return sprintf(buf, "%d\n", ratio * cpu->pstate.scaling);
774 cpufreq_freq_attr_ro(base_frequency);
776 static struct freq_attr *hwp_cpufreq_attrs[] = {
777 &energy_performance_preference,
778 &energy_performance_available_preferences,
783 static void intel_pstate_get_hwp_max(unsigned int cpu, int *phy_max,
788 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
789 WRITE_ONCE(all_cpu_data[cpu]->hwp_cap_cached, cap);
791 *current_max = HWP_GUARANTEED_PERF(cap);
793 *current_max = HWP_HIGHEST_PERF(cap);
795 *phy_max = HWP_HIGHEST_PERF(cap);
798 static void intel_pstate_hwp_set(unsigned int cpu)
800 struct cpudata *cpu_data = all_cpu_data[cpu];
805 max = cpu_data->max_perf_ratio;
806 min = cpu_data->min_perf_ratio;
808 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
811 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
813 value &= ~HWP_MIN_PERF(~0L);
814 value |= HWP_MIN_PERF(min);
816 value &= ~HWP_MAX_PERF(~0L);
817 value |= HWP_MAX_PERF(max);
819 if (cpu_data->epp_policy == cpu_data->policy)
822 cpu_data->epp_policy = cpu_data->policy;
824 if (cpu_data->epp_saved >= 0) {
825 epp = cpu_data->epp_saved;
826 cpu_data->epp_saved = -EINVAL;
830 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
831 epp = intel_pstate_get_epp(cpu_data, value);
832 cpu_data->epp_powersave = epp;
833 /* If EPP read was failed, then don't try to write */
839 /* skip setting EPP, when saved value is invalid */
840 if (cpu_data->epp_powersave < 0)
844 * No need to restore EPP when it is not zero. This
846 * - Policy is not changed
847 * - user has manually changed
848 * - Error reading EPB
850 epp = intel_pstate_get_epp(cpu_data, value);
854 epp = cpu_data->epp_powersave;
857 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
858 value &= ~GENMASK_ULL(31, 24);
859 value |= (u64)epp << 24;
861 intel_pstate_set_epb(cpu, epp);
864 WRITE_ONCE(cpu_data->hwp_req_cached, value);
865 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
868 static void intel_pstate_hwp_force_min_perf(int cpu)
873 value = all_cpu_data[cpu]->hwp_req_cached;
874 value &= ~GENMASK_ULL(31, 0);
875 min_perf = HWP_LOWEST_PERF(all_cpu_data[cpu]->hwp_cap_cached);
877 /* Set hwp_max = hwp_min */
878 value |= HWP_MAX_PERF(min_perf);
879 value |= HWP_MIN_PERF(min_perf);
882 if (boot_cpu_has(X86_FEATURE_HWP_EPP))
883 value |= HWP_ENERGY_PERF_PREFERENCE(HWP_EPP_POWERSAVE);
885 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
888 static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
890 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
895 cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
900 #define POWER_CTL_EE_ENABLE 1
901 #define POWER_CTL_EE_DISABLE 2
903 static int power_ctl_ee_state;
905 static void set_power_ctl_ee_state(bool input)
909 mutex_lock(&intel_pstate_driver_lock);
910 rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
912 power_ctl &= ~BIT(MSR_IA32_POWER_CTL_BIT_EE);
913 power_ctl_ee_state = POWER_CTL_EE_ENABLE;
915 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
916 power_ctl_ee_state = POWER_CTL_EE_DISABLE;
918 wrmsrl(MSR_IA32_POWER_CTL, power_ctl);
919 mutex_unlock(&intel_pstate_driver_lock);
922 static void intel_pstate_hwp_enable(struct cpudata *cpudata);
924 static int intel_pstate_resume(struct cpufreq_policy *policy)
927 /* Only restore if the system default is changed */
928 if (power_ctl_ee_state == POWER_CTL_EE_ENABLE)
929 set_power_ctl_ee_state(true);
930 else if (power_ctl_ee_state == POWER_CTL_EE_DISABLE)
931 set_power_ctl_ee_state(false);
936 mutex_lock(&intel_pstate_limits_lock);
938 if (policy->cpu == 0)
939 intel_pstate_hwp_enable(all_cpu_data[policy->cpu]);
941 all_cpu_data[policy->cpu]->epp_policy = 0;
942 intel_pstate_hwp_set(policy->cpu);
944 mutex_unlock(&intel_pstate_limits_lock);
949 static void intel_pstate_update_policies(void)
953 for_each_possible_cpu(cpu)
954 cpufreq_update_policy(cpu);
957 static void intel_pstate_update_max_freq(unsigned int cpu)
959 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
960 struct cpudata *cpudata;
965 cpudata = all_cpu_data[cpu];
966 policy->cpuinfo.max_freq = global.turbo_disabled_mf ?
967 cpudata->pstate.max_freq : cpudata->pstate.turbo_freq;
969 refresh_frequency_limits(policy);
971 cpufreq_cpu_release(policy);
974 static void intel_pstate_update_limits(unsigned int cpu)
976 mutex_lock(&intel_pstate_driver_lock);
978 update_turbo_state();
980 * If turbo has been turned on or off globally, policy limits for
981 * all CPUs need to be updated to reflect that.
983 if (global.turbo_disabled_mf != global.turbo_disabled) {
984 global.turbo_disabled_mf = global.turbo_disabled;
985 arch_set_max_freq_ratio(global.turbo_disabled);
986 for_each_possible_cpu(cpu)
987 intel_pstate_update_max_freq(cpu);
989 cpufreq_update_policy(cpu);
992 mutex_unlock(&intel_pstate_driver_lock);
995 /************************** sysfs begin ************************/
996 #define show_one(file_name, object) \
997 static ssize_t show_##file_name \
998 (struct kobject *kobj, struct kobj_attribute *attr, char *buf) \
1000 return sprintf(buf, "%u\n", global.object); \
1003 static ssize_t intel_pstate_show_status(char *buf);
1004 static int intel_pstate_update_status(const char *buf, size_t size);
1006 static ssize_t show_status(struct kobject *kobj,
1007 struct kobj_attribute *attr, char *buf)
1011 mutex_lock(&intel_pstate_driver_lock);
1012 ret = intel_pstate_show_status(buf);
1013 mutex_unlock(&intel_pstate_driver_lock);
1018 static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
1019 const char *buf, size_t count)
1021 char *p = memchr(buf, '\n', count);
1024 mutex_lock(&intel_pstate_driver_lock);
1025 ret = intel_pstate_update_status(buf, p ? p - buf : count);
1026 mutex_unlock(&intel_pstate_driver_lock);
1028 return ret < 0 ? ret : count;
1031 static ssize_t show_turbo_pct(struct kobject *kobj,
1032 struct kobj_attribute *attr, char *buf)
1034 struct cpudata *cpu;
1035 int total, no_turbo, turbo_pct;
1038 mutex_lock(&intel_pstate_driver_lock);
1040 if (!intel_pstate_driver) {
1041 mutex_unlock(&intel_pstate_driver_lock);
1045 cpu = all_cpu_data[0];
1047 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1048 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
1049 turbo_fp = div_fp(no_turbo, total);
1050 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
1052 mutex_unlock(&intel_pstate_driver_lock);
1054 return sprintf(buf, "%u\n", turbo_pct);
1057 static ssize_t show_num_pstates(struct kobject *kobj,
1058 struct kobj_attribute *attr, char *buf)
1060 struct cpudata *cpu;
1063 mutex_lock(&intel_pstate_driver_lock);
1065 if (!intel_pstate_driver) {
1066 mutex_unlock(&intel_pstate_driver_lock);
1070 cpu = all_cpu_data[0];
1071 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1073 mutex_unlock(&intel_pstate_driver_lock);
1075 return sprintf(buf, "%u\n", total);
1078 static ssize_t show_no_turbo(struct kobject *kobj,
1079 struct kobj_attribute *attr, char *buf)
1083 mutex_lock(&intel_pstate_driver_lock);
1085 if (!intel_pstate_driver) {
1086 mutex_unlock(&intel_pstate_driver_lock);
1090 update_turbo_state();
1091 if (global.turbo_disabled)
1092 ret = sprintf(buf, "%u\n", global.turbo_disabled);
1094 ret = sprintf(buf, "%u\n", global.no_turbo);
1096 mutex_unlock(&intel_pstate_driver_lock);
1101 static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b,
1102 const char *buf, size_t count)
1107 ret = sscanf(buf, "%u", &input);
1111 mutex_lock(&intel_pstate_driver_lock);
1113 if (!intel_pstate_driver) {
1114 mutex_unlock(&intel_pstate_driver_lock);
1118 mutex_lock(&intel_pstate_limits_lock);
1120 update_turbo_state();
1121 if (global.turbo_disabled) {
1122 pr_notice_once("Turbo disabled by BIOS or unavailable on processor\n");
1123 mutex_unlock(&intel_pstate_limits_lock);
1124 mutex_unlock(&intel_pstate_driver_lock);
1128 global.no_turbo = clamp_t(int, input, 0, 1);
1130 if (global.no_turbo) {
1131 struct cpudata *cpu = all_cpu_data[0];
1132 int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
1134 /* Squash the global minimum into the permitted range. */
1135 if (global.min_perf_pct > pct)
1136 global.min_perf_pct = pct;
1139 mutex_unlock(&intel_pstate_limits_lock);
1141 intel_pstate_update_policies();
1143 mutex_unlock(&intel_pstate_driver_lock);
1148 static struct cpufreq_driver intel_pstate;
1150 static void update_qos_request(enum freq_qos_req_type type)
1152 int max_state, turbo_max, freq, i, perf_pct;
1153 struct freq_qos_request *req;
1154 struct cpufreq_policy *policy;
1156 for_each_possible_cpu(i) {
1157 struct cpudata *cpu = all_cpu_data[i];
1159 policy = cpufreq_cpu_get(i);
1163 req = policy->driver_data;
1164 cpufreq_cpu_put(policy);
1170 intel_pstate_get_hwp_max(i, &turbo_max, &max_state);
1172 turbo_max = cpu->pstate.turbo_pstate;
1174 if (type == FREQ_QOS_MIN) {
1175 perf_pct = global.min_perf_pct;
1178 perf_pct = global.max_perf_pct;
1181 freq = DIV_ROUND_UP(turbo_max * perf_pct, 100);
1182 freq *= cpu->pstate.scaling;
1184 if (freq_qos_update_request(req, freq) < 0)
1185 pr_warn("Failed to update freq constraint: CPU%d\n", i);
1189 static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
1190 const char *buf, size_t count)
1195 ret = sscanf(buf, "%u", &input);
1199 mutex_lock(&intel_pstate_driver_lock);
1201 if (!intel_pstate_driver) {
1202 mutex_unlock(&intel_pstate_driver_lock);
1206 mutex_lock(&intel_pstate_limits_lock);
1208 global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
1210 mutex_unlock(&intel_pstate_limits_lock);
1212 if (intel_pstate_driver == &intel_pstate)
1213 intel_pstate_update_policies();
1215 update_qos_request(FREQ_QOS_MAX);
1217 mutex_unlock(&intel_pstate_driver_lock);
1222 static ssize_t store_min_perf_pct(struct kobject *a, struct kobj_attribute *b,
1223 const char *buf, size_t count)
1228 ret = sscanf(buf, "%u", &input);
1232 mutex_lock(&intel_pstate_driver_lock);
1234 if (!intel_pstate_driver) {
1235 mutex_unlock(&intel_pstate_driver_lock);
1239 mutex_lock(&intel_pstate_limits_lock);
1241 global.min_perf_pct = clamp_t(int, input,
1242 min_perf_pct_min(), global.max_perf_pct);
1244 mutex_unlock(&intel_pstate_limits_lock);
1246 if (intel_pstate_driver == &intel_pstate)
1247 intel_pstate_update_policies();
1249 update_qos_request(FREQ_QOS_MIN);
1251 mutex_unlock(&intel_pstate_driver_lock);
1256 static ssize_t show_hwp_dynamic_boost(struct kobject *kobj,
1257 struct kobj_attribute *attr, char *buf)
1259 return sprintf(buf, "%u\n", hwp_boost);
1262 static ssize_t store_hwp_dynamic_boost(struct kobject *a,
1263 struct kobj_attribute *b,
1264 const char *buf, size_t count)
1269 ret = kstrtouint(buf, 10, &input);
1273 mutex_lock(&intel_pstate_driver_lock);
1274 hwp_boost = !!input;
1275 intel_pstate_update_policies();
1276 mutex_unlock(&intel_pstate_driver_lock);
1281 static ssize_t show_energy_efficiency(struct kobject *kobj, struct kobj_attribute *attr,
1287 rdmsrl(MSR_IA32_POWER_CTL, power_ctl);
1288 enable = !!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE));
1289 return sprintf(buf, "%d\n", !enable);
1292 static ssize_t store_energy_efficiency(struct kobject *a, struct kobj_attribute *b,
1293 const char *buf, size_t count)
1298 ret = kstrtobool(buf, &input);
1302 set_power_ctl_ee_state(input);
1307 show_one(max_perf_pct, max_perf_pct);
1308 show_one(min_perf_pct, min_perf_pct);
1310 define_one_global_rw(status);
1311 define_one_global_rw(no_turbo);
1312 define_one_global_rw(max_perf_pct);
1313 define_one_global_rw(min_perf_pct);
1314 define_one_global_ro(turbo_pct);
1315 define_one_global_ro(num_pstates);
1316 define_one_global_rw(hwp_dynamic_boost);
1317 define_one_global_rw(energy_efficiency);
1319 static struct attribute *intel_pstate_attributes[] = {
1327 static const struct attribute_group intel_pstate_attr_group = {
1328 .attrs = intel_pstate_attributes,
1331 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[];
1333 static void __init intel_pstate_sysfs_expose_params(void)
1335 struct kobject *intel_pstate_kobject;
1338 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
1339 &cpu_subsys.dev_root->kobj);
1340 if (WARN_ON(!intel_pstate_kobject))
1343 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
1348 * If per cpu limits are enforced there are no global limits, so
1349 * return without creating max/min_perf_pct attributes
1354 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1357 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1361 rc = sysfs_create_file(intel_pstate_kobject,
1362 &hwp_dynamic_boost.attr);
1366 if (x86_match_cpu(intel_pstate_cpu_ee_disable_ids)) {
1367 rc = sysfs_create_file(intel_pstate_kobject, &energy_efficiency.attr);
1371 /************************** sysfs end ************************/
1373 static void intel_pstate_hwp_enable(struct cpudata *cpudata)
1375 /* First disable HWP notification interrupt as we don't process them */
1376 if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
1377 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
1379 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
1380 cpudata->epp_policy = 0;
1381 if (cpudata->epp_default == -EINVAL)
1382 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
1385 static int atom_get_min_pstate(void)
1389 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
1390 return (value >> 8) & 0x7F;
1393 static int atom_get_max_pstate(void)
1397 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
1398 return (value >> 16) & 0x7F;
1401 static int atom_get_turbo_pstate(void)
1405 rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
1406 return value & 0x7F;
1409 static u64 atom_get_val(struct cpudata *cpudata, int pstate)
1415 val = (u64)pstate << 8;
1416 if (global.no_turbo && !global.turbo_disabled)
1417 val |= (u64)1 << 32;
1419 vid_fp = cpudata->vid.min + mul_fp(
1420 int_tofp(pstate - cpudata->pstate.min_pstate),
1421 cpudata->vid.ratio);
1423 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
1424 vid = ceiling_fp(vid_fp);
1426 if (pstate > cpudata->pstate.max_pstate)
1427 vid = cpudata->vid.turbo;
1432 static int silvermont_get_scaling(void)
1436 /* Defined in Table 35-6 from SDM (Sept 2015) */
1437 static int silvermont_freq_table[] = {
1438 83300, 100000, 133300, 116700, 80000};
1440 rdmsrl(MSR_FSB_FREQ, value);
1444 return silvermont_freq_table[i];
1447 static int airmont_get_scaling(void)
1451 /* Defined in Table 35-10 from SDM (Sept 2015) */
1452 static int airmont_freq_table[] = {
1453 83300, 100000, 133300, 116700, 80000,
1454 93300, 90000, 88900, 87500};
1456 rdmsrl(MSR_FSB_FREQ, value);
1460 return airmont_freq_table[i];
1463 static void atom_get_vid(struct cpudata *cpudata)
1467 rdmsrl(MSR_ATOM_CORE_VIDS, value);
1468 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1469 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
1470 cpudata->vid.ratio = div_fp(
1471 cpudata->vid.max - cpudata->vid.min,
1472 int_tofp(cpudata->pstate.max_pstate -
1473 cpudata->pstate.min_pstate));
1475 rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
1476 cpudata->vid.turbo = value & 0x7f;
1479 static int core_get_min_pstate(void)
1483 rdmsrl(MSR_PLATFORM_INFO, value);
1484 return (value >> 40) & 0xFF;
1487 static int core_get_max_pstate_physical(void)
1491 rdmsrl(MSR_PLATFORM_INFO, value);
1492 return (value >> 8) & 0xFF;
1495 static int core_get_tdp_ratio(u64 plat_info)
1497 /* Check how many TDP levels present */
1498 if (plat_info & 0x600000000) {
1504 /* Get the TDP level (0, 1, 2) to get ratios */
1505 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1509 /* TDP MSR are continuous starting at 0x648 */
1510 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
1511 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1515 /* For level 1 and 2, bits[23:16] contain the ratio */
1516 if (tdp_ctrl & 0x03)
1519 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
1520 pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
1522 return (int)tdp_ratio;
1528 static int core_get_max_pstate(void)
1536 rdmsrl(MSR_PLATFORM_INFO, plat_info);
1537 max_pstate = (plat_info >> 8) & 0xFF;
1539 tdp_ratio = core_get_tdp_ratio(plat_info);
1544 /* Turbo activation ratio is not used on HWP platforms */
1548 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1552 /* Do some sanity checking for safety */
1553 tar_levels = tar & 0xff;
1554 if (tdp_ratio - 1 == tar_levels) {
1555 max_pstate = tar_levels;
1556 pr_debug("max_pstate=TAC %x\n", max_pstate);
1563 static int core_get_turbo_pstate(void)
1568 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
1569 nont = core_get_max_pstate();
1570 ret = (value) & 255;
1576 static inline int core_get_scaling(void)
1581 static u64 core_get_val(struct cpudata *cpudata, int pstate)
1585 val = (u64)pstate << 8;
1586 if (global.no_turbo && !global.turbo_disabled)
1587 val |= (u64)1 << 32;
1592 static int knl_get_aperf_mperf_shift(void)
1597 static int knl_get_turbo_pstate(void)
1602 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
1603 nont = core_get_max_pstate();
1604 ret = (((value) >> 8) & 0xFF);
1610 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
1612 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1613 cpu->pstate.current_pstate = pstate;
1615 * Generally, there is no guarantee that this code will always run on
1616 * the CPU being updated, so force the register update to run on the
1619 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1620 pstate_funcs.get_val(cpu, pstate));
1623 static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1625 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1628 static void intel_pstate_max_within_limits(struct cpudata *cpu)
1630 int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
1632 update_turbo_state();
1633 intel_pstate_set_pstate(cpu, pstate);
1636 static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1638 cpu->pstate.min_pstate = pstate_funcs.get_min();
1639 cpu->pstate.max_pstate = pstate_funcs.get_max();
1640 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
1641 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
1642 cpu->pstate.scaling = pstate_funcs.get_scaling();
1643 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1645 if (hwp_active && !hwp_mode_bdw) {
1646 unsigned int phy_max, current_max;
1648 intel_pstate_get_hwp_max(cpu->cpu, &phy_max, ¤t_max);
1649 cpu->pstate.turbo_freq = phy_max * cpu->pstate.scaling;
1650 cpu->pstate.turbo_pstate = phy_max;
1652 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
1655 if (pstate_funcs.get_aperf_mperf_shift)
1656 cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
1658 if (pstate_funcs.get_vid)
1659 pstate_funcs.get_vid(cpu);
1661 intel_pstate_set_min_pstate(cpu);
1665 * Long hold time will keep high perf limits for long time,
1666 * which negatively impacts perf/watt for some workloads,
1667 * like specpower. 3ms is based on experiements on some
1670 static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
1672 static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu)
1674 u64 hwp_req = READ_ONCE(cpu->hwp_req_cached);
1675 u32 max_limit = (hwp_req & 0xff00) >> 8;
1676 u32 min_limit = (hwp_req & 0xff);
1680 * Cases to consider (User changes via sysfs or boot time):
1681 * If, P0 (Turbo max) = P1 (Guaranteed max) = min:
1683 * If, P0 (Turbo max) > P1 (Guaranteed max) = min:
1684 * Should result in one level boost only for P0.
1685 * If, P0 (Turbo max) = P1 (Guaranteed max) > min:
1686 * Should result in two level boost:
1687 * (min + p1)/2 and P1.
1688 * If, P0 (Turbo max) > P1 (Guaranteed max) > min:
1689 * Should result in three level boost:
1690 * (min + p1)/2, P1 and P0.
1693 /* If max and min are equal or already at max, nothing to boost */
1694 if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit)
1697 if (!cpu->hwp_boost_min)
1698 cpu->hwp_boost_min = min_limit;
1700 /* level at half way mark between min and guranteed */
1701 boost_level1 = (HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) + min_limit) >> 1;
1703 if (cpu->hwp_boost_min < boost_level1)
1704 cpu->hwp_boost_min = boost_level1;
1705 else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1706 cpu->hwp_boost_min = HWP_GUARANTEED_PERF(cpu->hwp_cap_cached);
1707 else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) &&
1708 max_limit != HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1709 cpu->hwp_boost_min = max_limit;
1713 hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min;
1714 wrmsrl(MSR_HWP_REQUEST, hwp_req);
1715 cpu->last_update = cpu->sample.time;
1718 static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu)
1720 if (cpu->hwp_boost_min) {
1723 /* Check if we are idle for hold time to boost down */
1724 expired = time_after64(cpu->sample.time, cpu->last_update +
1725 hwp_boost_hold_time_ns);
1727 wrmsrl(MSR_HWP_REQUEST, cpu->hwp_req_cached);
1728 cpu->hwp_boost_min = 0;
1731 cpu->last_update = cpu->sample.time;
1734 static inline void intel_pstate_update_util_hwp_local(struct cpudata *cpu,
1737 cpu->sample.time = time;
1739 if (cpu->sched_flags & SCHED_CPUFREQ_IOWAIT) {
1742 cpu->sched_flags = 0;
1744 * Set iowait_boost flag and update time. Since IO WAIT flag
1745 * is set all the time, we can't just conclude that there is
1746 * some IO bound activity is scheduled on this CPU with just
1747 * one occurrence. If we receive at least two in two
1748 * consecutive ticks, then we treat as boost candidate.
1750 if (time_before64(time, cpu->last_io_update + 2 * TICK_NSEC))
1753 cpu->last_io_update = time;
1756 intel_pstate_hwp_boost_up(cpu);
1759 intel_pstate_hwp_boost_down(cpu);
1763 static inline void intel_pstate_update_util_hwp(struct update_util_data *data,
1764 u64 time, unsigned int flags)
1766 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
1768 cpu->sched_flags |= flags;
1770 if (smp_processor_id() == cpu->cpu)
1771 intel_pstate_update_util_hwp_local(cpu, time);
1774 static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
1776 struct sample *sample = &cpu->sample;
1778 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
1781 static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
1784 unsigned long flags;
1787 local_irq_save(flags);
1788 rdmsrl(MSR_IA32_APERF, aperf);
1789 rdmsrl(MSR_IA32_MPERF, mperf);
1791 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
1792 local_irq_restore(flags);
1795 local_irq_restore(flags);
1797 cpu->last_sample_time = cpu->sample.time;
1798 cpu->sample.time = time;
1799 cpu->sample.aperf = aperf;
1800 cpu->sample.mperf = mperf;
1801 cpu->sample.tsc = tsc;
1802 cpu->sample.aperf -= cpu->prev_aperf;
1803 cpu->sample.mperf -= cpu->prev_mperf;
1804 cpu->sample.tsc -= cpu->prev_tsc;
1806 cpu->prev_aperf = aperf;
1807 cpu->prev_mperf = mperf;
1808 cpu->prev_tsc = tsc;
1810 * First time this function is invoked in a given cycle, all of the
1811 * previous sample data fields are equal to zero or stale and they must
1812 * be populated with meaningful numbers for things to work, so assume
1813 * that sample.time will always be reset before setting the utilization
1814 * update hook and make the caller skip the sample then.
1816 if (cpu->last_sample_time) {
1817 intel_pstate_calc_avg_perf(cpu);
1823 static inline int32_t get_avg_frequency(struct cpudata *cpu)
1825 return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
1828 static inline int32_t get_avg_pstate(struct cpudata *cpu)
1830 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1831 cpu->sample.core_avg_perf);
1834 static inline int32_t get_target_pstate(struct cpudata *cpu)
1836 struct sample *sample = &cpu->sample;
1838 int target, avg_pstate;
1840 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
1843 if (busy_frac < cpu->iowait_boost)
1844 busy_frac = cpu->iowait_boost;
1846 sample->busy_scaled = busy_frac * 100;
1848 target = global.no_turbo || global.turbo_disabled ?
1849 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1850 target += target >> 2;
1851 target = mul_fp(target, busy_frac);
1852 if (target < cpu->pstate.min_pstate)
1853 target = cpu->pstate.min_pstate;
1856 * If the average P-state during the previous cycle was higher than the
1857 * current target, add 50% of the difference to the target to reduce
1858 * possible performance oscillations and offset possible performance
1859 * loss related to moving the workload from one CPU to another within
1862 avg_pstate = get_avg_pstate(cpu);
1863 if (avg_pstate > target)
1864 target += (avg_pstate - target) >> 1;
1869 static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
1871 int min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1872 int max_pstate = max(min_pstate, cpu->max_perf_ratio);
1874 return clamp_t(int, pstate, min_pstate, max_pstate);
1877 static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1879 if (pstate == cpu->pstate.current_pstate)
1882 cpu->pstate.current_pstate = pstate;
1883 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1886 static void intel_pstate_adjust_pstate(struct cpudata *cpu)
1888 int from = cpu->pstate.current_pstate;
1889 struct sample *sample;
1892 update_turbo_state();
1894 target_pstate = get_target_pstate(cpu);
1895 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1896 trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
1897 intel_pstate_update_pstate(cpu, target_pstate);
1899 sample = &cpu->sample;
1900 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
1901 fp_toint(sample->busy_scaled),
1903 cpu->pstate.current_pstate,
1907 get_avg_frequency(cpu),
1908 fp_toint(cpu->iowait_boost * 100));
1911 static void intel_pstate_update_util(struct update_util_data *data, u64 time,
1914 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
1917 /* Don't allow remote callbacks */
1918 if (smp_processor_id() != cpu->cpu)
1921 delta_ns = time - cpu->last_update;
1922 if (flags & SCHED_CPUFREQ_IOWAIT) {
1923 /* Start over if the CPU may have been idle. */
1924 if (delta_ns > TICK_NSEC) {
1925 cpu->iowait_boost = ONE_EIGHTH_FP;
1926 } else if (cpu->iowait_boost >= ONE_EIGHTH_FP) {
1927 cpu->iowait_boost <<= 1;
1928 if (cpu->iowait_boost > int_tofp(1))
1929 cpu->iowait_boost = int_tofp(1);
1931 cpu->iowait_boost = ONE_EIGHTH_FP;
1933 } else if (cpu->iowait_boost) {
1934 /* Clear iowait_boost if the CPU may have been idle. */
1935 if (delta_ns > TICK_NSEC)
1936 cpu->iowait_boost = 0;
1938 cpu->iowait_boost >>= 1;
1940 cpu->last_update = time;
1941 delta_ns = time - cpu->sample.time;
1942 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
1945 if (intel_pstate_sample(cpu, time))
1946 intel_pstate_adjust_pstate(cpu);
1949 static struct pstate_funcs core_funcs = {
1950 .get_max = core_get_max_pstate,
1951 .get_max_physical = core_get_max_pstate_physical,
1952 .get_min = core_get_min_pstate,
1953 .get_turbo = core_get_turbo_pstate,
1954 .get_scaling = core_get_scaling,
1955 .get_val = core_get_val,
1958 static const struct pstate_funcs silvermont_funcs = {
1959 .get_max = atom_get_max_pstate,
1960 .get_max_physical = atom_get_max_pstate,
1961 .get_min = atom_get_min_pstate,
1962 .get_turbo = atom_get_turbo_pstate,
1963 .get_val = atom_get_val,
1964 .get_scaling = silvermont_get_scaling,
1965 .get_vid = atom_get_vid,
1968 static const struct pstate_funcs airmont_funcs = {
1969 .get_max = atom_get_max_pstate,
1970 .get_max_physical = atom_get_max_pstate,
1971 .get_min = atom_get_min_pstate,
1972 .get_turbo = atom_get_turbo_pstate,
1973 .get_val = atom_get_val,
1974 .get_scaling = airmont_get_scaling,
1975 .get_vid = atom_get_vid,
1978 static const struct pstate_funcs knl_funcs = {
1979 .get_max = core_get_max_pstate,
1980 .get_max_physical = core_get_max_pstate_physical,
1981 .get_min = core_get_min_pstate,
1982 .get_turbo = knl_get_turbo_pstate,
1983 .get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
1984 .get_scaling = core_get_scaling,
1985 .get_val = core_get_val,
1988 #define X86_MATCH(model, policy) \
1989 X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
1990 X86_FEATURE_APERFMPERF, &policy)
1992 static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
1993 X86_MATCH(SANDYBRIDGE, core_funcs),
1994 X86_MATCH(SANDYBRIDGE_X, core_funcs),
1995 X86_MATCH(ATOM_SILVERMONT, silvermont_funcs),
1996 X86_MATCH(IVYBRIDGE, core_funcs),
1997 X86_MATCH(HASWELL, core_funcs),
1998 X86_MATCH(BROADWELL, core_funcs),
1999 X86_MATCH(IVYBRIDGE_X, core_funcs),
2000 X86_MATCH(HASWELL_X, core_funcs),
2001 X86_MATCH(HASWELL_L, core_funcs),
2002 X86_MATCH(HASWELL_G, core_funcs),
2003 X86_MATCH(BROADWELL_G, core_funcs),
2004 X86_MATCH(ATOM_AIRMONT, airmont_funcs),
2005 X86_MATCH(SKYLAKE_L, core_funcs),
2006 X86_MATCH(BROADWELL_X, core_funcs),
2007 X86_MATCH(SKYLAKE, core_funcs),
2008 X86_MATCH(BROADWELL_D, core_funcs),
2009 X86_MATCH(XEON_PHI_KNL, knl_funcs),
2010 X86_MATCH(XEON_PHI_KNM, knl_funcs),
2011 X86_MATCH(ATOM_GOLDMONT, core_funcs),
2012 X86_MATCH(ATOM_GOLDMONT_PLUS, core_funcs),
2013 X86_MATCH(SKYLAKE_X, core_funcs),
2016 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
2018 static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
2019 X86_MATCH(BROADWELL_D, core_funcs),
2020 X86_MATCH(BROADWELL_X, core_funcs),
2021 X86_MATCH(SKYLAKE_X, core_funcs),
2025 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
2026 X86_MATCH(KABYLAKE, core_funcs),
2030 static const struct x86_cpu_id intel_pstate_hwp_boost_ids[] = {
2031 X86_MATCH(SKYLAKE_X, core_funcs),
2032 X86_MATCH(SKYLAKE, core_funcs),
2036 static int intel_pstate_init_cpu(unsigned int cpunum)
2038 struct cpudata *cpu;
2040 cpu = all_cpu_data[cpunum];
2043 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
2047 all_cpu_data[cpunum] = cpu;
2049 cpu->epp_default = -EINVAL;
2050 cpu->epp_powersave = -EINVAL;
2051 cpu->epp_saved = -EINVAL;
2054 cpu = all_cpu_data[cpunum];
2059 const struct x86_cpu_id *id;
2061 intel_pstate_hwp_enable(cpu);
2063 id = x86_match_cpu(intel_pstate_hwp_boost_ids);
2064 if (id && intel_pstate_acpi_pm_profile_server())
2068 intel_pstate_get_cpu_pstates(cpu);
2070 pr_debug("controlling: cpu %d\n", cpunum);
2075 static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
2077 struct cpudata *cpu = all_cpu_data[cpu_num];
2079 if (hwp_active && !hwp_boost)
2082 if (cpu->update_util_set)
2085 /* Prevent intel_pstate_update_util() from using stale data. */
2086 cpu->sample.time = 0;
2087 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
2089 intel_pstate_update_util_hwp :
2090 intel_pstate_update_util));
2091 cpu->update_util_set = true;
2094 static void intel_pstate_clear_update_util_hook(unsigned int cpu)
2096 struct cpudata *cpu_data = all_cpu_data[cpu];
2098 if (!cpu_data->update_util_set)
2101 cpufreq_remove_update_util_hook(cpu);
2102 cpu_data->update_util_set = false;
2106 static int intel_pstate_get_max_freq(struct cpudata *cpu)
2108 return global.turbo_disabled || global.no_turbo ?
2109 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2112 static void intel_pstate_update_perf_limits(struct cpudata *cpu,
2113 unsigned int policy_min,
2114 unsigned int policy_max)
2116 int max_freq = intel_pstate_get_max_freq(cpu);
2117 int32_t max_policy_perf, min_policy_perf;
2118 int max_state, turbo_max;
2121 * HWP needs some special consideration, because on BDX the
2122 * HWP_REQUEST uses abstract value to represent performance
2123 * rather than pure ratios.
2126 intel_pstate_get_hwp_max(cpu->cpu, &turbo_max, &max_state);
2128 max_state = global.no_turbo || global.turbo_disabled ?
2129 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2130 turbo_max = cpu->pstate.turbo_pstate;
2133 max_policy_perf = max_state * policy_max / max_freq;
2134 if (policy_max == policy_min) {
2135 min_policy_perf = max_policy_perf;
2137 min_policy_perf = max_state * policy_min / max_freq;
2138 min_policy_perf = clamp_t(int32_t, min_policy_perf,
2139 0, max_policy_perf);
2142 pr_debug("cpu:%d max_state %d min_policy_perf:%d max_policy_perf:%d\n",
2143 cpu->cpu, max_state, min_policy_perf, max_policy_perf);
2145 /* Normalize user input to [min_perf, max_perf] */
2146 if (per_cpu_limits) {
2147 cpu->min_perf_ratio = min_policy_perf;
2148 cpu->max_perf_ratio = max_policy_perf;
2150 int32_t global_min, global_max;
2152 /* Global limits are in percent of the maximum turbo P-state. */
2153 global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2154 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2155 global_min = clamp_t(int32_t, global_min, 0, global_max);
2157 pr_debug("cpu:%d global_min:%d global_max:%d\n", cpu->cpu,
2158 global_min, global_max);
2160 cpu->min_perf_ratio = max(min_policy_perf, global_min);
2161 cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
2162 cpu->max_perf_ratio = min(max_policy_perf, global_max);
2163 cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
2165 /* Make sure min_perf <= max_perf */
2166 cpu->min_perf_ratio = min(cpu->min_perf_ratio,
2167 cpu->max_perf_ratio);
2170 pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", cpu->cpu,
2171 cpu->max_perf_ratio,
2172 cpu->min_perf_ratio);
2175 static int intel_pstate_set_policy(struct cpufreq_policy *policy)
2177 struct cpudata *cpu;
2179 if (!policy->cpuinfo.max_freq)
2182 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
2183 policy->cpuinfo.max_freq, policy->max);
2185 cpu = all_cpu_data[policy->cpu];
2186 cpu->policy = policy->policy;
2188 mutex_lock(&intel_pstate_limits_lock);
2190 intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
2192 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
2194 * NOHZ_FULL CPUs need this as the governor callback may not
2195 * be invoked on them.
2197 intel_pstate_clear_update_util_hook(policy->cpu);
2198 intel_pstate_max_within_limits(cpu);
2200 intel_pstate_set_update_util_hook(policy->cpu);
2205 * When hwp_boost was active before and dynamically it
2206 * was turned off, in that case we need to clear the
2210 intel_pstate_clear_update_util_hook(policy->cpu);
2211 intel_pstate_hwp_set(policy->cpu);
2214 mutex_unlock(&intel_pstate_limits_lock);
2219 static void intel_pstate_adjust_policy_max(struct cpudata *cpu,
2220 struct cpufreq_policy_data *policy)
2223 cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
2224 policy->max < policy->cpuinfo.max_freq &&
2225 policy->max > cpu->pstate.max_freq) {
2226 pr_debug("policy->max > max non turbo frequency\n");
2227 policy->max = policy->cpuinfo.max_freq;
2231 static void intel_pstate_verify_cpu_policy(struct cpudata *cpu,
2232 struct cpufreq_policy_data *policy)
2234 update_turbo_state();
2235 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2236 intel_pstate_get_max_freq(cpu));
2238 intel_pstate_adjust_policy_max(cpu, policy);
2241 static int intel_pstate_verify_policy(struct cpufreq_policy_data *policy)
2243 intel_pstate_verify_cpu_policy(all_cpu_data[policy->cpu], policy);
2248 static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
2250 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
2253 static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
2255 pr_debug("CPU %d exiting\n", policy->cpu);
2257 intel_pstate_clear_update_util_hook(policy->cpu);
2259 intel_pstate_hwp_save_state(policy);
2260 intel_pstate_hwp_force_min_perf(policy->cpu);
2262 intel_cpufreq_stop_cpu(policy);
2266 static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
2268 intel_pstate_exit_perf_limits(policy);
2270 policy->fast_switch_possible = false;
2275 static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
2277 struct cpudata *cpu;
2280 rc = intel_pstate_init_cpu(policy->cpu);
2284 cpu = all_cpu_data[policy->cpu];
2286 cpu->max_perf_ratio = 0xFF;
2287 cpu->min_perf_ratio = 0;
2289 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
2290 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
2292 /* cpuinfo and default policy values */
2293 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
2294 update_turbo_state();
2295 global.turbo_disabled_mf = global.turbo_disabled;
2296 policy->cpuinfo.max_freq = global.turbo_disabled ?
2297 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2298 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
2301 unsigned int max_freq;
2303 max_freq = global.turbo_disabled ?
2304 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2305 if (max_freq < policy->cpuinfo.max_freq)
2306 policy->cpuinfo.max_freq = max_freq;
2309 intel_pstate_init_acpi_perf_limits(policy);
2311 policy->fast_switch_possible = true;
2316 static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
2318 int ret = __intel_pstate_cpu_init(policy);
2324 * Set the policy to powersave to provide a valid fallback value in case
2325 * the default cpufreq governor is neither powersave nor performance.
2327 policy->policy = CPUFREQ_POLICY_POWERSAVE;
2332 static struct cpufreq_driver intel_pstate = {
2333 .flags = CPUFREQ_CONST_LOOPS,
2334 .verify = intel_pstate_verify_policy,
2335 .setpolicy = intel_pstate_set_policy,
2336 .suspend = intel_pstate_hwp_save_state,
2337 .resume = intel_pstate_resume,
2338 .init = intel_pstate_cpu_init,
2339 .exit = intel_pstate_cpu_exit,
2340 .stop_cpu = intel_pstate_stop_cpu,
2341 .update_limits = intel_pstate_update_limits,
2342 .name = "intel_pstate",
2345 static int intel_cpufreq_verify_policy(struct cpufreq_policy_data *policy)
2347 struct cpudata *cpu = all_cpu_data[policy->cpu];
2349 intel_pstate_verify_cpu_policy(cpu, policy);
2350 intel_pstate_update_perf_limits(cpu, policy->min, policy->max);
2355 /* Use of trace in passive mode:
2357 * In passive mode the trace core_busy field (also known as the
2358 * performance field, and lablelled as such on the graphs; also known as
2359 * core_avg_perf) is not needed and so is re-assigned to indicate if the
2360 * driver call was via the normal or fast switch path. Various graphs
2361 * output from the intel_pstate_tracer.py utility that include core_busy
2362 * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%,
2363 * so we use 10 to indicate the the normal path through the driver, and
2364 * 90 to indicate the fast switch path through the driver.
2365 * The scaled_busy field is not used, and is set to 0.
2368 #define INTEL_PSTATE_TRACE_TARGET 10
2369 #define INTEL_PSTATE_TRACE_FAST_SWITCH 90
2371 static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate)
2373 struct sample *sample;
2375 if (!trace_pstate_sample_enabled())
2378 if (!intel_pstate_sample(cpu, ktime_get()))
2381 sample = &cpu->sample;
2382 trace_pstate_sample(trace_type,
2385 cpu->pstate.current_pstate,
2389 get_avg_frequency(cpu),
2390 fp_toint(cpu->iowait_boost * 100));
2393 static int intel_cpufreq_target(struct cpufreq_policy *policy,
2394 unsigned int target_freq,
2395 unsigned int relation)
2397 struct cpudata *cpu = all_cpu_data[policy->cpu];
2398 struct cpufreq_freqs freqs;
2399 int target_pstate, old_pstate;
2401 update_turbo_state();
2403 freqs.old = policy->cur;
2404 freqs.new = target_freq;
2406 cpufreq_freq_transition_begin(policy, &freqs);
2408 case CPUFREQ_RELATION_L:
2409 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
2411 case CPUFREQ_RELATION_H:
2412 target_pstate = freqs.new / cpu->pstate.scaling;
2415 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
2418 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2419 old_pstate = cpu->pstate.current_pstate;
2420 if (target_pstate != cpu->pstate.current_pstate) {
2421 cpu->pstate.current_pstate = target_pstate;
2422 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
2423 pstate_funcs.get_val(cpu, target_pstate));
2425 freqs.new = target_pstate * cpu->pstate.scaling;
2426 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_TARGET, old_pstate);
2427 cpufreq_freq_transition_end(policy, &freqs, false);
2432 static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
2433 unsigned int target_freq)
2435 struct cpudata *cpu = all_cpu_data[policy->cpu];
2436 int target_pstate, old_pstate;
2438 update_turbo_state();
2440 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
2441 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2442 old_pstate = cpu->pstate.current_pstate;
2443 intel_pstate_update_pstate(cpu, target_pstate);
2444 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate);
2445 return target_pstate * cpu->pstate.scaling;
2448 static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
2450 int max_state, turbo_max, min_freq, max_freq, ret;
2451 struct freq_qos_request *req;
2452 struct cpudata *cpu;
2455 dev = get_cpu_device(policy->cpu);
2459 ret = __intel_pstate_cpu_init(policy);
2463 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
2464 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
2465 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
2466 policy->cur = policy->cpuinfo.min_freq;
2468 req = kcalloc(2, sizeof(*req), GFP_KERNEL);
2474 cpu = all_cpu_data[policy->cpu];
2477 intel_pstate_get_hwp_max(policy->cpu, &turbo_max, &max_state);
2479 turbo_max = cpu->pstate.turbo_pstate;
2481 min_freq = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2482 min_freq *= cpu->pstate.scaling;
2483 max_freq = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2484 max_freq *= cpu->pstate.scaling;
2486 ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN,
2489 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
2493 ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX,
2496 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
2497 goto remove_min_req;
2500 policy->driver_data = req;
2505 freq_qos_remove_request(req);
2509 intel_pstate_exit_perf_limits(policy);
2514 static int intel_cpufreq_cpu_exit(struct cpufreq_policy *policy)
2516 struct freq_qos_request *req;
2518 req = policy->driver_data;
2520 freq_qos_remove_request(req + 1);
2521 freq_qos_remove_request(req);
2524 return intel_pstate_cpu_exit(policy);
2527 static struct cpufreq_driver intel_cpufreq = {
2528 .flags = CPUFREQ_CONST_LOOPS,
2529 .verify = intel_cpufreq_verify_policy,
2530 .target = intel_cpufreq_target,
2531 .fast_switch = intel_cpufreq_fast_switch,
2532 .init = intel_cpufreq_cpu_init,
2533 .exit = intel_cpufreq_cpu_exit,
2534 .stop_cpu = intel_cpufreq_stop_cpu,
2535 .update_limits = intel_pstate_update_limits,
2536 .name = "intel_cpufreq",
2539 static struct cpufreq_driver *default_driver;
2541 static void intel_pstate_driver_cleanup(void)
2546 for_each_online_cpu(cpu) {
2547 if (all_cpu_data[cpu]) {
2548 if (intel_pstate_driver == &intel_pstate)
2549 intel_pstate_clear_update_util_hook(cpu);
2551 kfree(all_cpu_data[cpu]);
2552 all_cpu_data[cpu] = NULL;
2556 intel_pstate_driver = NULL;
2559 static int intel_pstate_register_driver(struct cpufreq_driver *driver)
2563 memset(&global, 0, sizeof(global));
2564 global.max_perf_pct = 100;
2566 intel_pstate_driver = driver;
2567 ret = cpufreq_register_driver(intel_pstate_driver);
2569 intel_pstate_driver_cleanup();
2573 global.min_perf_pct = min_perf_pct_min();
2578 static int intel_pstate_unregister_driver(void)
2583 cpufreq_unregister_driver(intel_pstate_driver);
2584 intel_pstate_driver_cleanup();
2589 static ssize_t intel_pstate_show_status(char *buf)
2591 if (!intel_pstate_driver)
2592 return sprintf(buf, "off\n");
2594 return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
2595 "active" : "passive");
2598 static int intel_pstate_update_status(const char *buf, size_t size)
2602 if (size == 3 && !strncmp(buf, "off", size))
2603 return intel_pstate_driver ?
2604 intel_pstate_unregister_driver() : -EINVAL;
2606 if (size == 6 && !strncmp(buf, "active", size)) {
2607 if (intel_pstate_driver) {
2608 if (intel_pstate_driver == &intel_pstate)
2611 ret = intel_pstate_unregister_driver();
2616 return intel_pstate_register_driver(&intel_pstate);
2619 if (size == 7 && !strncmp(buf, "passive", size)) {
2620 if (intel_pstate_driver) {
2621 if (intel_pstate_driver == &intel_cpufreq)
2624 ret = intel_pstate_unregister_driver();
2629 return intel_pstate_register_driver(&intel_cpufreq);
2635 static int no_load __initdata;
2636 static int no_hwp __initdata;
2637 static int hwp_only __initdata;
2638 static unsigned int force_load __initdata;
2640 static int __init intel_pstate_msrs_not_valid(void)
2642 if (!pstate_funcs.get_max() ||
2643 !pstate_funcs.get_min() ||
2644 !pstate_funcs.get_turbo())
2650 static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
2652 pstate_funcs.get_max = funcs->get_max;
2653 pstate_funcs.get_max_physical = funcs->get_max_physical;
2654 pstate_funcs.get_min = funcs->get_min;
2655 pstate_funcs.get_turbo = funcs->get_turbo;
2656 pstate_funcs.get_scaling = funcs->get_scaling;
2657 pstate_funcs.get_val = funcs->get_val;
2658 pstate_funcs.get_vid = funcs->get_vid;
2659 pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
2664 static bool __init intel_pstate_no_acpi_pss(void)
2668 for_each_possible_cpu(i) {
2670 union acpi_object *pss;
2671 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2672 struct acpi_processor *pr = per_cpu(processors, i);
2677 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2678 if (ACPI_FAILURE(status))
2681 pss = buffer.pointer;
2682 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2690 pr_debug("ACPI _PSS not found\n");
2694 static bool __init intel_pstate_no_acpi_pcch(void)
2699 status = acpi_get_handle(NULL, "\\_SB", &handle);
2700 if (ACPI_FAILURE(status))
2703 if (acpi_has_method(handle, "PCCH"))
2707 pr_debug("ACPI PCCH not found\n");
2711 static bool __init intel_pstate_has_acpi_ppc(void)
2715 for_each_possible_cpu(i) {
2716 struct acpi_processor *pr = per_cpu(processors, i);
2720 if (acpi_has_method(pr->handle, "_PPC"))
2723 pr_debug("ACPI _PPC not found\n");
2732 /* Hardware vendor-specific info that has its own power management modes */
2733 static struct acpi_platform_list plat_info[] __initdata = {
2734 {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, NULL, PSS},
2735 {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2736 {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2737 {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2738 {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2739 {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2740 {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2741 {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2742 {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2743 {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2744 {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2745 {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2746 {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2747 {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2748 {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, NULL, PPC},
2752 #define BITMASK_OOB (BIT(8) | BIT(18))
2754 static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
2756 const struct x86_cpu_id *id;
2760 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2762 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2763 if (misc_pwr & BITMASK_OOB) {
2764 pr_debug("Bit 8 or 18 in the MISC_PWR_MGMT MSR set\n");
2765 pr_debug("P states are controlled in Out of Band mode by the firmware/hardware\n");
2770 idx = acpi_match_platform_list(plat_info);
2774 switch (plat_info[idx].data) {
2776 if (!intel_pstate_no_acpi_pss())
2779 return intel_pstate_no_acpi_pcch();
2781 return intel_pstate_has_acpi_ppc() && !force_load;
2787 static void intel_pstate_request_control_from_smm(void)
2790 * It may be unsafe to request P-states control from SMM if _PPC support
2791 * has not been enabled.
2794 acpi_processor_pstate_control();
2796 #else /* CONFIG_ACPI not enabled */
2797 static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
2798 static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
2799 static inline void intel_pstate_request_control_from_smm(void) {}
2800 #endif /* CONFIG_ACPI */
2802 #define INTEL_PSTATE_HWP_BROADWELL 0x01
2804 #define X86_MATCH_HWP(model, hwp_mode) \
2805 X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_##model, \
2806 X86_FEATURE_HWP, hwp_mode)
2808 static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2809 X86_MATCH_HWP(BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL),
2810 X86_MATCH_HWP(BROADWELL_D, INTEL_PSTATE_HWP_BROADWELL),
2811 X86_MATCH_HWP(ANY, 0),
2815 static int __init intel_pstate_init(void)
2817 const struct x86_cpu_id *id;
2820 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
2826 id = x86_match_cpu(hwp_support_ids);
2828 copy_cpu_funcs(&core_funcs);
2830 * Avoid enabling HWP for processors without EPP support,
2831 * because that means incomplete HWP implementation which is a
2832 * corner case and supporting it is generally problematic.
2834 if (!no_hwp && boot_cpu_has(X86_FEATURE_HWP_EPP)) {
2836 hwp_mode_bdw = id->driver_data;
2837 intel_pstate.attr = hwp_cpufreq_attrs;
2838 default_driver = &intel_pstate;
2839 goto hwp_cpu_matched;
2842 id = x86_match_cpu(intel_pstate_cpu_ids);
2844 pr_info("CPU model not supported\n");
2848 copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
2851 if (intel_pstate_msrs_not_valid()) {
2852 pr_info("Invalid MSRs\n");
2855 /* Without HWP start in the passive mode. */
2856 if (!default_driver)
2857 default_driver = &intel_cpufreq;
2861 * The Intel pstate driver will be ignored if the platform
2862 * firmware has its own power management modes.
2864 if (intel_pstate_platform_pwr_mgmt_exists()) {
2865 pr_info("P-states controlled by the platform\n");
2869 if (!hwp_active && hwp_only)
2872 pr_info("Intel P-state driver initializing\n");
2874 all_cpu_data = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
2878 intel_pstate_request_control_from_smm();
2880 intel_pstate_sysfs_expose_params();
2882 mutex_lock(&intel_pstate_driver_lock);
2883 rc = intel_pstate_register_driver(default_driver);
2884 mutex_unlock(&intel_pstate_driver_lock);
2889 const struct x86_cpu_id *id;
2891 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
2893 set_power_ctl_ee_state(false);
2894 pr_info("Disabling energy efficiency optimization\n");
2897 pr_info("HWP enabled\n");
2902 device_initcall(intel_pstate_init);
2904 static int __init intel_pstate_setup(char *str)
2909 if (!strcmp(str, "disable")) {
2911 } else if (!strcmp(str, "active")) {
2912 default_driver = &intel_pstate;
2913 } else if (!strcmp(str, "passive")) {
2914 default_driver = &intel_cpufreq;
2917 if (!strcmp(str, "no_hwp")) {
2918 pr_info("HWP disabled\n");
2921 if (!strcmp(str, "force"))
2923 if (!strcmp(str, "hwp_only"))
2925 if (!strcmp(str, "per_cpu_perf_limits"))
2926 per_cpu_limits = true;
2929 if (!strcmp(str, "support_acpi_ppc"))
2935 early_param("intel_pstate", intel_pstate_setup);
2937 MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2938 MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2939 MODULE_LICENSE("GPL");