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: Whethet 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: Number of clock cycles after aperf, merf is incremented
205 * This shift is a multiplier to mperf delta to
206 * calculate CPU busy.
207 * @prev_aperf: Last APERF value read from APERF MSR
208 * @prev_mperf: Last MPERF value read from MPERF MSR
209 * @prev_tsc: Last timestamp counter (TSC) value
210 * @prev_cummulative_iowait: IO Wait time difference from last and
212 * @sample: Storage for storing last Sample data
213 * @min_perf_ratio: Minimum capacity in terms of PERF or HWP ratios
214 * @max_perf_ratio: Maximum capacity in terms of PERF or HWP ratios
215 * @acpi_perf_data: Stores ACPI perf information read from _PSS
216 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
217 * @epp_powersave: Last saved HWP energy performance preference
218 * (EPP) or energy performance bias (EPB),
219 * when policy switched to performance
220 * @epp_policy: Last saved policy used to set EPP/EPB
221 * @epp_default: Power on default HWP energy performance
223 * @epp_saved: Saved EPP/EPB during system suspend or CPU offline
225 * @hwp_req_cached: Cached value of the last HWP Request MSR
226 * @hwp_cap_cached: Cached value of the last HWP Capabilities MSR
227 * @last_io_update: Last time when IO wake flag was set
228 * @sched_flags: Store scheduler flags for possible cross CPU update
229 * @hwp_boost_min: Last HWP boosted min performance
231 * This structure stores per CPU instance data for all CPUs.
237 struct update_util_data update_util;
238 bool update_util_set;
240 struct pstate_data pstate;
244 u64 last_sample_time;
245 u64 aperf_mperf_shift;
249 u64 prev_cummulative_iowait;
250 struct sample sample;
251 int32_t min_perf_ratio;
252 int32_t max_perf_ratio;
254 struct acpi_processor_performance acpi_perf_data;
255 bool valid_pss_table;
257 unsigned int iowait_boost;
265 unsigned int sched_flags;
269 static struct cpudata **all_cpu_data;
272 * struct pstate_funcs - Per CPU model specific callbacks
273 * @get_max: Callback to get maximum non turbo effective P state
274 * @get_max_physical: Callback to get maximum non turbo physical P state
275 * @get_min: Callback to get minimum P state
276 * @get_turbo: Callback to get turbo P state
277 * @get_scaling: Callback to get frequency scaling factor
278 * @get_val: Callback to convert P state to actual MSR write value
279 * @get_vid: Callback to get VID data for Atom platforms
281 * Core and Atom CPU models have different way to get P State limits. This
282 * structure is used to store those callbacks.
284 struct pstate_funcs {
285 int (*get_max)(void);
286 int (*get_max_physical)(void);
287 int (*get_min)(void);
288 int (*get_turbo)(void);
289 int (*get_scaling)(void);
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;
302 static struct cpufreq_driver *intel_pstate_driver __read_mostly;
305 static bool acpi_ppc;
308 static struct global_params global;
310 static DEFINE_MUTEX(intel_pstate_driver_lock);
311 static DEFINE_MUTEX(intel_pstate_limits_lock);
315 static bool intel_pstate_acpi_pm_profile_server(void)
317 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
318 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
324 static bool intel_pstate_get_ppc_enable_status(void)
326 if (intel_pstate_acpi_pm_profile_server())
332 #ifdef CONFIG_ACPI_CPPC_LIB
334 /* The work item is needed to avoid CPU hotplug locking issues */
335 static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
337 sched_set_itmt_support();
340 static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
342 static void intel_pstate_set_itmt_prio(int cpu)
344 struct cppc_perf_caps cppc_perf;
345 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
348 ret = cppc_get_perf_caps(cpu, &cppc_perf);
353 * The priorities can be set regardless of whether or not
354 * sched_set_itmt_support(true) has been called and it is valid to
355 * update them at any time after it has been called.
357 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
359 if (max_highest_perf <= min_highest_perf) {
360 if (cppc_perf.highest_perf > max_highest_perf)
361 max_highest_perf = cppc_perf.highest_perf;
363 if (cppc_perf.highest_perf < min_highest_perf)
364 min_highest_perf = cppc_perf.highest_perf;
366 if (max_highest_perf > min_highest_perf) {
368 * This code can be run during CPU online under the
369 * CPU hotplug locks, so sched_set_itmt_support()
370 * cannot be called from here. Queue up a work item
373 schedule_work(&sched_itmt_work);
378 static int intel_pstate_get_cppc_guranteed(int cpu)
380 struct cppc_perf_caps cppc_perf;
383 ret = cppc_get_perf_caps(cpu, &cppc_perf);
387 if (cppc_perf.guaranteed_perf)
388 return cppc_perf.guaranteed_perf;
390 return cppc_perf.nominal_perf;
393 #else /* CONFIG_ACPI_CPPC_LIB */
394 static void intel_pstate_set_itmt_prio(int cpu)
397 #endif /* CONFIG_ACPI_CPPC_LIB */
399 static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
406 intel_pstate_set_itmt_prio(policy->cpu);
410 if (!intel_pstate_get_ppc_enable_status())
413 cpu = all_cpu_data[policy->cpu];
415 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
421 * Check if the control value in _PSS is for PERF_CTL MSR, which should
422 * guarantee that the states returned by it map to the states in our
425 if (cpu->acpi_perf_data.control_register.space_id !=
426 ACPI_ADR_SPACE_FIXED_HARDWARE)
430 * If there is only one entry _PSS, simply ignore _PSS and continue as
431 * usual without taking _PSS into account
433 if (cpu->acpi_perf_data.state_count < 2)
436 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
437 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
438 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
439 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
440 (u32) cpu->acpi_perf_data.states[i].core_frequency,
441 (u32) cpu->acpi_perf_data.states[i].power,
442 (u32) cpu->acpi_perf_data.states[i].control);
446 * The _PSS table doesn't contain whole turbo frequency range.
447 * This just contains +1 MHZ above the max non turbo frequency,
448 * with control value corresponding to max turbo ratio. But
449 * when cpufreq set policy is called, it will call with this
450 * max frequency, which will cause a reduced performance as
451 * this driver uses real max turbo frequency as the max
452 * frequency. So correct this frequency in _PSS table to
453 * correct max turbo frequency based on the turbo state.
454 * Also need to convert to MHz as _PSS freq is in MHz.
456 if (!global.turbo_disabled)
457 cpu->acpi_perf_data.states[0].core_frequency =
458 policy->cpuinfo.max_freq / 1000;
459 cpu->valid_pss_table = true;
460 pr_debug("_PPC limits will be enforced\n");
465 cpu->valid_pss_table = false;
466 acpi_processor_unregister_performance(policy->cpu);
469 static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
473 cpu = all_cpu_data[policy->cpu];
474 if (!cpu->valid_pss_table)
477 acpi_processor_unregister_performance(policy->cpu);
479 #else /* CONFIG_ACPI */
480 static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
484 static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
488 static inline bool intel_pstate_acpi_pm_profile_server(void)
492 #endif /* CONFIG_ACPI */
494 #ifndef CONFIG_ACPI_CPPC_LIB
495 static int intel_pstate_get_cppc_guranteed(int cpu)
499 #endif /* CONFIG_ACPI_CPPC_LIB */
501 static inline void update_turbo_state(void)
506 cpu = all_cpu_data[0];
507 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
508 global.turbo_disabled =
509 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
510 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
513 static int min_perf_pct_min(void)
515 struct cpudata *cpu = all_cpu_data[0];
516 int turbo_pstate = cpu->pstate.turbo_pstate;
518 return turbo_pstate ?
519 (cpu->pstate.min_pstate * 100 / turbo_pstate) : 0;
522 static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
527 if (!boot_cpu_has(X86_FEATURE_EPB))
530 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
534 return (s16)(epb & 0x0f);
537 static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
541 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
543 * When hwp_req_data is 0, means that caller didn't read
544 * MSR_HWP_REQUEST, so need to read and get EPP.
547 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
552 epp = (hwp_req_data >> 24) & 0xff;
554 /* When there is no EPP present, HWP uses EPB settings */
555 epp = intel_pstate_get_epb(cpu_data);
561 static int intel_pstate_set_epb(int cpu, s16 pref)
566 if (!boot_cpu_has(X86_FEATURE_EPB))
569 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
573 epb = (epb & ~0x0f) | pref;
574 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
580 * EPP/EPB display strings corresponding to EPP index in the
581 * energy_perf_strings[]
583 *-------------------------------------
586 * 2 balance_performance
590 static const char * const energy_perf_strings[] = {
593 "balance_performance",
598 static const unsigned int epp_values[] = {
600 HWP_EPP_BALANCE_PERFORMANCE,
601 HWP_EPP_BALANCE_POWERSAVE,
605 static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
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)
623 } else if (boot_cpu_has(X86_FEATURE_EPB)) {
626 * 0x00-0x03 : Performance
627 * 0x04-0x07 : Balance performance
628 * 0x08-0x0B : Balance power
630 * The EPB is a 4 bit value, but our ranges restrict the
631 * value which can be set. Here only using top two bits
634 index = (epp >> 2) + 1;
640 static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
647 epp = cpu_data->epp_default;
649 mutex_lock(&intel_pstate_limits_lock);
651 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
654 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
658 value &= ~GENMASK_ULL(31, 24);
661 epp = epp_values[pref_index - 1];
663 value |= (u64)epp << 24;
664 ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
667 epp = (pref_index - 1) << 2;
668 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
671 mutex_unlock(&intel_pstate_limits_lock);
676 static ssize_t show_energy_performance_available_preferences(
677 struct cpufreq_policy *policy, char *buf)
682 while (energy_perf_strings[i] != NULL)
683 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
685 ret += sprintf(&buf[ret], "\n");
690 cpufreq_freq_attr_ro(energy_performance_available_preferences);
692 static ssize_t store_energy_performance_preference(
693 struct cpufreq_policy *policy, const char *buf, size_t count)
695 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
696 char str_preference[21];
699 ret = sscanf(buf, "%20s", str_preference);
703 ret = match_string(energy_perf_strings, -1, str_preference);
707 intel_pstate_set_energy_pref_index(cpu_data, ret);
711 static ssize_t show_energy_performance_preference(
712 struct cpufreq_policy *policy, char *buf)
714 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
717 preference = intel_pstate_get_energy_pref_index(cpu_data);
721 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
724 cpufreq_freq_attr_rw(energy_performance_preference);
726 static ssize_t show_base_frequency(struct cpufreq_policy *policy, char *buf)
732 ratio = intel_pstate_get_cppc_guranteed(policy->cpu);
734 rdmsrl_on_cpu(policy->cpu, MSR_HWP_CAPABILITIES, &cap);
735 ratio = HWP_GUARANTEED_PERF(cap);
738 cpu = all_cpu_data[policy->cpu];
740 return sprintf(buf, "%d\n", ratio * cpu->pstate.scaling);
743 cpufreq_freq_attr_ro(base_frequency);
745 static struct freq_attr *hwp_cpufreq_attrs[] = {
746 &energy_performance_preference,
747 &energy_performance_available_preferences,
752 static void intel_pstate_get_hwp_max(unsigned int cpu, int *phy_max,
757 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
758 WRITE_ONCE(all_cpu_data[cpu]->hwp_cap_cached, cap);
760 *current_max = HWP_GUARANTEED_PERF(cap);
762 *current_max = HWP_HIGHEST_PERF(cap);
764 *phy_max = HWP_HIGHEST_PERF(cap);
767 static void intel_pstate_hwp_set(unsigned int cpu)
769 struct cpudata *cpu_data = all_cpu_data[cpu];
774 max = cpu_data->max_perf_ratio;
775 min = cpu_data->min_perf_ratio;
777 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
780 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
782 value &= ~HWP_MIN_PERF(~0L);
783 value |= HWP_MIN_PERF(min);
785 value &= ~HWP_MAX_PERF(~0L);
786 value |= HWP_MAX_PERF(max);
788 if (cpu_data->epp_policy == cpu_data->policy)
791 cpu_data->epp_policy = cpu_data->policy;
793 if (cpu_data->epp_saved >= 0) {
794 epp = cpu_data->epp_saved;
795 cpu_data->epp_saved = -EINVAL;
799 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
800 epp = intel_pstate_get_epp(cpu_data, value);
801 cpu_data->epp_powersave = epp;
802 /* If EPP read was failed, then don't try to write */
808 /* skip setting EPP, when saved value is invalid */
809 if (cpu_data->epp_powersave < 0)
813 * No need to restore EPP when it is not zero. This
815 * - Policy is not changed
816 * - user has manually changed
817 * - Error reading EPB
819 epp = intel_pstate_get_epp(cpu_data, value);
823 epp = cpu_data->epp_powersave;
826 if (boot_cpu_has(X86_FEATURE_HWP_EPP)) {
827 value &= ~GENMASK_ULL(31, 24);
828 value |= (u64)epp << 24;
830 intel_pstate_set_epb(cpu, epp);
833 WRITE_ONCE(cpu_data->hwp_req_cached, value);
834 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
837 static void intel_pstate_hwp_force_min_perf(int cpu)
842 value = all_cpu_data[cpu]->hwp_req_cached;
843 value &= ~GENMASK_ULL(31, 0);
844 min_perf = HWP_LOWEST_PERF(all_cpu_data[cpu]->hwp_cap_cached);
846 /* Set hwp_max = hwp_min */
847 value |= HWP_MAX_PERF(min_perf);
848 value |= HWP_MIN_PERF(min_perf);
850 /* Set EPP/EPB to min */
851 if (boot_cpu_has(X86_FEATURE_HWP_EPP))
852 value |= HWP_ENERGY_PERF_PREFERENCE(HWP_EPP_POWERSAVE);
854 intel_pstate_set_epb(cpu, HWP_EPP_BALANCE_POWERSAVE);
856 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
859 static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
861 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
866 cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
871 static void intel_pstate_hwp_enable(struct cpudata *cpudata);
873 static int intel_pstate_resume(struct cpufreq_policy *policy)
878 mutex_lock(&intel_pstate_limits_lock);
880 if (policy->cpu == 0)
881 intel_pstate_hwp_enable(all_cpu_data[policy->cpu]);
883 all_cpu_data[policy->cpu]->epp_policy = 0;
884 intel_pstate_hwp_set(policy->cpu);
886 mutex_unlock(&intel_pstate_limits_lock);
891 static void intel_pstate_update_policies(void)
895 for_each_possible_cpu(cpu)
896 cpufreq_update_policy(cpu);
899 static void intel_pstate_update_max_freq(unsigned int cpu)
901 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
902 struct cpudata *cpudata;
907 cpudata = all_cpu_data[cpu];
908 policy->cpuinfo.max_freq = global.turbo_disabled_mf ?
909 cpudata->pstate.max_freq : cpudata->pstate.turbo_freq;
911 refresh_frequency_limits(policy);
913 cpufreq_cpu_release(policy);
916 static void intel_pstate_update_limits(unsigned int cpu)
918 mutex_lock(&intel_pstate_driver_lock);
920 update_turbo_state();
922 * If turbo has been turned on or off globally, policy limits for
923 * all CPUs need to be updated to reflect that.
925 if (global.turbo_disabled_mf != global.turbo_disabled) {
926 global.turbo_disabled_mf = global.turbo_disabled;
927 for_each_possible_cpu(cpu)
928 intel_pstate_update_max_freq(cpu);
930 cpufreq_update_policy(cpu);
933 mutex_unlock(&intel_pstate_driver_lock);
936 /************************** sysfs begin ************************/
937 #define show_one(file_name, object) \
938 static ssize_t show_##file_name \
939 (struct kobject *kobj, struct kobj_attribute *attr, char *buf) \
941 return sprintf(buf, "%u\n", global.object); \
944 static ssize_t intel_pstate_show_status(char *buf);
945 static int intel_pstate_update_status(const char *buf, size_t size);
947 static ssize_t show_status(struct kobject *kobj,
948 struct kobj_attribute *attr, char *buf)
952 mutex_lock(&intel_pstate_driver_lock);
953 ret = intel_pstate_show_status(buf);
954 mutex_unlock(&intel_pstate_driver_lock);
959 static ssize_t store_status(struct kobject *a, struct kobj_attribute *b,
960 const char *buf, size_t count)
962 char *p = memchr(buf, '\n', count);
965 mutex_lock(&intel_pstate_driver_lock);
966 ret = intel_pstate_update_status(buf, p ? p - buf : count);
967 mutex_unlock(&intel_pstate_driver_lock);
969 return ret < 0 ? ret : count;
972 static ssize_t show_turbo_pct(struct kobject *kobj,
973 struct kobj_attribute *attr, char *buf)
976 int total, no_turbo, turbo_pct;
979 mutex_lock(&intel_pstate_driver_lock);
981 if (!intel_pstate_driver) {
982 mutex_unlock(&intel_pstate_driver_lock);
986 cpu = all_cpu_data[0];
988 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
989 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
990 turbo_fp = div_fp(no_turbo, total);
991 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
993 mutex_unlock(&intel_pstate_driver_lock);
995 return sprintf(buf, "%u\n", turbo_pct);
998 static ssize_t show_num_pstates(struct kobject *kobj,
999 struct kobj_attribute *attr, char *buf)
1001 struct cpudata *cpu;
1004 mutex_lock(&intel_pstate_driver_lock);
1006 if (!intel_pstate_driver) {
1007 mutex_unlock(&intel_pstate_driver_lock);
1011 cpu = all_cpu_data[0];
1012 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1014 mutex_unlock(&intel_pstate_driver_lock);
1016 return sprintf(buf, "%u\n", total);
1019 static ssize_t show_no_turbo(struct kobject *kobj,
1020 struct kobj_attribute *attr, char *buf)
1024 mutex_lock(&intel_pstate_driver_lock);
1026 if (!intel_pstate_driver) {
1027 mutex_unlock(&intel_pstate_driver_lock);
1031 update_turbo_state();
1032 if (global.turbo_disabled)
1033 ret = sprintf(buf, "%u\n", global.turbo_disabled);
1035 ret = sprintf(buf, "%u\n", global.no_turbo);
1037 mutex_unlock(&intel_pstate_driver_lock);
1042 static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b,
1043 const char *buf, size_t count)
1048 ret = sscanf(buf, "%u", &input);
1052 mutex_lock(&intel_pstate_driver_lock);
1054 if (!intel_pstate_driver) {
1055 mutex_unlock(&intel_pstate_driver_lock);
1059 mutex_lock(&intel_pstate_limits_lock);
1061 update_turbo_state();
1062 if (global.turbo_disabled) {
1063 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
1064 mutex_unlock(&intel_pstate_limits_lock);
1065 mutex_unlock(&intel_pstate_driver_lock);
1069 global.no_turbo = clamp_t(int, input, 0, 1);
1071 if (global.no_turbo) {
1072 struct cpudata *cpu = all_cpu_data[0];
1073 int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
1075 /* Squash the global minimum into the permitted range. */
1076 if (global.min_perf_pct > pct)
1077 global.min_perf_pct = pct;
1080 mutex_unlock(&intel_pstate_limits_lock);
1082 intel_pstate_update_policies();
1084 mutex_unlock(&intel_pstate_driver_lock);
1089 static struct cpufreq_driver intel_pstate;
1091 static void update_qos_request(enum freq_qos_req_type type)
1093 int max_state, turbo_max, freq, i, perf_pct;
1094 struct freq_qos_request *req;
1095 struct cpufreq_policy *policy;
1097 for_each_possible_cpu(i) {
1098 struct cpudata *cpu = all_cpu_data[i];
1100 policy = cpufreq_cpu_get(i);
1104 req = policy->driver_data;
1105 cpufreq_cpu_put(policy);
1111 intel_pstate_get_hwp_max(i, &turbo_max, &max_state);
1113 turbo_max = cpu->pstate.turbo_pstate;
1115 if (type == FREQ_QOS_MIN) {
1116 perf_pct = global.min_perf_pct;
1119 perf_pct = global.max_perf_pct;
1122 freq = DIV_ROUND_UP(turbo_max * perf_pct, 100);
1123 freq *= cpu->pstate.scaling;
1125 if (freq_qos_update_request(req, freq) < 0)
1126 pr_warn("Failed to update freq constraint: CPU%d\n", i);
1130 static ssize_t store_max_perf_pct(struct kobject *a, struct kobj_attribute *b,
1131 const char *buf, size_t count)
1136 ret = sscanf(buf, "%u", &input);
1140 mutex_lock(&intel_pstate_driver_lock);
1142 if (!intel_pstate_driver) {
1143 mutex_unlock(&intel_pstate_driver_lock);
1147 mutex_lock(&intel_pstate_limits_lock);
1149 global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
1151 mutex_unlock(&intel_pstate_limits_lock);
1153 if (intel_pstate_driver == &intel_pstate)
1154 intel_pstate_update_policies();
1156 update_qos_request(FREQ_QOS_MAX);
1158 mutex_unlock(&intel_pstate_driver_lock);
1163 static ssize_t store_min_perf_pct(struct kobject *a, struct kobj_attribute *b,
1164 const char *buf, size_t count)
1169 ret = sscanf(buf, "%u", &input);
1173 mutex_lock(&intel_pstate_driver_lock);
1175 if (!intel_pstate_driver) {
1176 mutex_unlock(&intel_pstate_driver_lock);
1180 mutex_lock(&intel_pstate_limits_lock);
1182 global.min_perf_pct = clamp_t(int, input,
1183 min_perf_pct_min(), global.max_perf_pct);
1185 mutex_unlock(&intel_pstate_limits_lock);
1187 if (intel_pstate_driver == &intel_pstate)
1188 intel_pstate_update_policies();
1190 update_qos_request(FREQ_QOS_MIN);
1192 mutex_unlock(&intel_pstate_driver_lock);
1197 static ssize_t show_hwp_dynamic_boost(struct kobject *kobj,
1198 struct kobj_attribute *attr, char *buf)
1200 return sprintf(buf, "%u\n", hwp_boost);
1203 static ssize_t store_hwp_dynamic_boost(struct kobject *a,
1204 struct kobj_attribute *b,
1205 const char *buf, size_t count)
1210 ret = kstrtouint(buf, 10, &input);
1214 mutex_lock(&intel_pstate_driver_lock);
1215 hwp_boost = !!input;
1216 intel_pstate_update_policies();
1217 mutex_unlock(&intel_pstate_driver_lock);
1222 show_one(max_perf_pct, max_perf_pct);
1223 show_one(min_perf_pct, min_perf_pct);
1225 define_one_global_rw(status);
1226 define_one_global_rw(no_turbo);
1227 define_one_global_rw(max_perf_pct);
1228 define_one_global_rw(min_perf_pct);
1229 define_one_global_ro(turbo_pct);
1230 define_one_global_ro(num_pstates);
1231 define_one_global_rw(hwp_dynamic_boost);
1233 static struct attribute *intel_pstate_attributes[] = {
1241 static const struct attribute_group intel_pstate_attr_group = {
1242 .attrs = intel_pstate_attributes,
1245 static void __init intel_pstate_sysfs_expose_params(void)
1247 struct kobject *intel_pstate_kobject;
1250 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
1251 &cpu_subsys.dev_root->kobj);
1252 if (WARN_ON(!intel_pstate_kobject))
1255 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
1260 * If per cpu limits are enforced there are no global limits, so
1261 * return without creating max/min_perf_pct attributes
1266 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1269 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1273 rc = sysfs_create_file(intel_pstate_kobject,
1274 &hwp_dynamic_boost.attr);
1278 /************************** sysfs end ************************/
1280 static void intel_pstate_hwp_enable(struct cpudata *cpudata)
1282 /* First disable HWP notification interrupt as we don't process them */
1283 if (boot_cpu_has(X86_FEATURE_HWP_NOTIFY))
1284 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
1286 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
1287 cpudata->epp_policy = 0;
1288 if (cpudata->epp_default == -EINVAL)
1289 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
1292 #define MSR_IA32_POWER_CTL_BIT_EE 19
1294 /* Disable energy efficiency optimization */
1295 static void intel_pstate_disable_ee(int cpu)
1300 ret = rdmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, &power_ctl);
1304 if (!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE))) {
1305 pr_info("Disabling energy efficiency optimization\n");
1306 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
1307 wrmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, power_ctl);
1311 static int atom_get_min_pstate(void)
1315 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
1316 return (value >> 8) & 0x7F;
1319 static int atom_get_max_pstate(void)
1323 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
1324 return (value >> 16) & 0x7F;
1327 static int atom_get_turbo_pstate(void)
1331 rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
1332 return value & 0x7F;
1335 static u64 atom_get_val(struct cpudata *cpudata, int pstate)
1341 val = (u64)pstate << 8;
1342 if (global.no_turbo && !global.turbo_disabled)
1343 val |= (u64)1 << 32;
1345 vid_fp = cpudata->vid.min + mul_fp(
1346 int_tofp(pstate - cpudata->pstate.min_pstate),
1347 cpudata->vid.ratio);
1349 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
1350 vid = ceiling_fp(vid_fp);
1352 if (pstate > cpudata->pstate.max_pstate)
1353 vid = cpudata->vid.turbo;
1358 static int silvermont_get_scaling(void)
1362 /* Defined in Table 35-6 from SDM (Sept 2015) */
1363 static int silvermont_freq_table[] = {
1364 83300, 100000, 133300, 116700, 80000};
1366 rdmsrl(MSR_FSB_FREQ, value);
1370 return silvermont_freq_table[i];
1373 static int airmont_get_scaling(void)
1377 /* Defined in Table 35-10 from SDM (Sept 2015) */
1378 static int airmont_freq_table[] = {
1379 83300, 100000, 133300, 116700, 80000,
1380 93300, 90000, 88900, 87500};
1382 rdmsrl(MSR_FSB_FREQ, value);
1386 return airmont_freq_table[i];
1389 static void atom_get_vid(struct cpudata *cpudata)
1393 rdmsrl(MSR_ATOM_CORE_VIDS, value);
1394 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1395 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
1396 cpudata->vid.ratio = div_fp(
1397 cpudata->vid.max - cpudata->vid.min,
1398 int_tofp(cpudata->pstate.max_pstate -
1399 cpudata->pstate.min_pstate));
1401 rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
1402 cpudata->vid.turbo = value & 0x7f;
1405 static int core_get_min_pstate(void)
1409 rdmsrl(MSR_PLATFORM_INFO, value);
1410 return (value >> 40) & 0xFF;
1413 static int core_get_max_pstate_physical(void)
1417 rdmsrl(MSR_PLATFORM_INFO, value);
1418 return (value >> 8) & 0xFF;
1421 static int core_get_tdp_ratio(u64 plat_info)
1423 /* Check how many TDP levels present */
1424 if (plat_info & 0x600000000) {
1430 /* Get the TDP level (0, 1, 2) to get ratios */
1431 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1435 /* TDP MSR are continuous starting at 0x648 */
1436 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
1437 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1441 /* For level 1 and 2, bits[23:16] contain the ratio */
1442 if (tdp_ctrl & 0x03)
1445 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
1446 pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
1448 return (int)tdp_ratio;
1454 static int core_get_max_pstate(void)
1462 rdmsrl(MSR_PLATFORM_INFO, plat_info);
1463 max_pstate = (plat_info >> 8) & 0xFF;
1465 tdp_ratio = core_get_tdp_ratio(plat_info);
1470 /* Turbo activation ratio is not used on HWP platforms */
1474 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1478 /* Do some sanity checking for safety */
1479 tar_levels = tar & 0xff;
1480 if (tdp_ratio - 1 == tar_levels) {
1481 max_pstate = tar_levels;
1482 pr_debug("max_pstate=TAC %x\n", max_pstate);
1489 static int core_get_turbo_pstate(void)
1494 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
1495 nont = core_get_max_pstate();
1496 ret = (value) & 255;
1502 static inline int core_get_scaling(void)
1507 static u64 core_get_val(struct cpudata *cpudata, int pstate)
1511 val = (u64)pstate << 8;
1512 if (global.no_turbo && !global.turbo_disabled)
1513 val |= (u64)1 << 32;
1518 static int knl_get_aperf_mperf_shift(void)
1523 static int knl_get_turbo_pstate(void)
1528 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
1529 nont = core_get_max_pstate();
1530 ret = (((value) >> 8) & 0xFF);
1536 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
1538 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1539 cpu->pstate.current_pstate = pstate;
1541 * Generally, there is no guarantee that this code will always run on
1542 * the CPU being updated, so force the register update to run on the
1545 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1546 pstate_funcs.get_val(cpu, pstate));
1549 static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1551 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1554 static void intel_pstate_max_within_limits(struct cpudata *cpu)
1556 int pstate = max(cpu->pstate.min_pstate, cpu->max_perf_ratio);
1558 update_turbo_state();
1559 intel_pstate_set_pstate(cpu, pstate);
1562 static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1564 cpu->pstate.min_pstate = pstate_funcs.get_min();
1565 cpu->pstate.max_pstate = pstate_funcs.get_max();
1566 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
1567 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
1568 cpu->pstate.scaling = pstate_funcs.get_scaling();
1569 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1571 if (hwp_active && !hwp_mode_bdw) {
1572 unsigned int phy_max, current_max;
1574 intel_pstate_get_hwp_max(cpu->cpu, &phy_max, ¤t_max);
1575 cpu->pstate.turbo_freq = phy_max * cpu->pstate.scaling;
1577 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
1580 if (pstate_funcs.get_aperf_mperf_shift)
1581 cpu->aperf_mperf_shift = pstate_funcs.get_aperf_mperf_shift();
1583 if (pstate_funcs.get_vid)
1584 pstate_funcs.get_vid(cpu);
1586 intel_pstate_set_min_pstate(cpu);
1590 * Long hold time will keep high perf limits for long time,
1591 * which negatively impacts perf/watt for some workloads,
1592 * like specpower. 3ms is based on experiements on some
1595 static int hwp_boost_hold_time_ns = 3 * NSEC_PER_MSEC;
1597 static inline void intel_pstate_hwp_boost_up(struct cpudata *cpu)
1599 u64 hwp_req = READ_ONCE(cpu->hwp_req_cached);
1600 u32 max_limit = (hwp_req & 0xff00) >> 8;
1601 u32 min_limit = (hwp_req & 0xff);
1605 * Cases to consider (User changes via sysfs or boot time):
1606 * If, P0 (Turbo max) = P1 (Guaranteed max) = min:
1608 * If, P0 (Turbo max) > P1 (Guaranteed max) = min:
1609 * Should result in one level boost only for P0.
1610 * If, P0 (Turbo max) = P1 (Guaranteed max) > min:
1611 * Should result in two level boost:
1612 * (min + p1)/2 and P1.
1613 * If, P0 (Turbo max) > P1 (Guaranteed max) > min:
1614 * Should result in three level boost:
1615 * (min + p1)/2, P1 and P0.
1618 /* If max and min are equal or already at max, nothing to boost */
1619 if (max_limit == min_limit || cpu->hwp_boost_min >= max_limit)
1622 if (!cpu->hwp_boost_min)
1623 cpu->hwp_boost_min = min_limit;
1625 /* level at half way mark between min and guranteed */
1626 boost_level1 = (HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) + min_limit) >> 1;
1628 if (cpu->hwp_boost_min < boost_level1)
1629 cpu->hwp_boost_min = boost_level1;
1630 else if (cpu->hwp_boost_min < HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1631 cpu->hwp_boost_min = HWP_GUARANTEED_PERF(cpu->hwp_cap_cached);
1632 else if (cpu->hwp_boost_min == HWP_GUARANTEED_PERF(cpu->hwp_cap_cached) &&
1633 max_limit != HWP_GUARANTEED_PERF(cpu->hwp_cap_cached))
1634 cpu->hwp_boost_min = max_limit;
1638 hwp_req = (hwp_req & ~GENMASK_ULL(7, 0)) | cpu->hwp_boost_min;
1639 wrmsrl(MSR_HWP_REQUEST, hwp_req);
1640 cpu->last_update = cpu->sample.time;
1643 static inline void intel_pstate_hwp_boost_down(struct cpudata *cpu)
1645 if (cpu->hwp_boost_min) {
1648 /* Check if we are idle for hold time to boost down */
1649 expired = time_after64(cpu->sample.time, cpu->last_update +
1650 hwp_boost_hold_time_ns);
1652 wrmsrl(MSR_HWP_REQUEST, cpu->hwp_req_cached);
1653 cpu->hwp_boost_min = 0;
1656 cpu->last_update = cpu->sample.time;
1659 static inline void intel_pstate_update_util_hwp_local(struct cpudata *cpu,
1662 cpu->sample.time = time;
1664 if (cpu->sched_flags & SCHED_CPUFREQ_IOWAIT) {
1667 cpu->sched_flags = 0;
1669 * Set iowait_boost flag and update time. Since IO WAIT flag
1670 * is set all the time, we can't just conclude that there is
1671 * some IO bound activity is scheduled on this CPU with just
1672 * one occurrence. If we receive at least two in two
1673 * consecutive ticks, then we treat as boost candidate.
1675 if (time_before64(time, cpu->last_io_update + 2 * TICK_NSEC))
1678 cpu->last_io_update = time;
1681 intel_pstate_hwp_boost_up(cpu);
1684 intel_pstate_hwp_boost_down(cpu);
1688 static inline void intel_pstate_update_util_hwp(struct update_util_data *data,
1689 u64 time, unsigned int flags)
1691 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
1693 cpu->sched_flags |= flags;
1695 if (smp_processor_id() == cpu->cpu)
1696 intel_pstate_update_util_hwp_local(cpu, time);
1699 static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
1701 struct sample *sample = &cpu->sample;
1703 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
1706 static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
1709 unsigned long flags;
1712 local_irq_save(flags);
1713 rdmsrl(MSR_IA32_APERF, aperf);
1714 rdmsrl(MSR_IA32_MPERF, mperf);
1716 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
1717 local_irq_restore(flags);
1720 local_irq_restore(flags);
1722 cpu->last_sample_time = cpu->sample.time;
1723 cpu->sample.time = time;
1724 cpu->sample.aperf = aperf;
1725 cpu->sample.mperf = mperf;
1726 cpu->sample.tsc = tsc;
1727 cpu->sample.aperf -= cpu->prev_aperf;
1728 cpu->sample.mperf -= cpu->prev_mperf;
1729 cpu->sample.tsc -= cpu->prev_tsc;
1731 cpu->prev_aperf = aperf;
1732 cpu->prev_mperf = mperf;
1733 cpu->prev_tsc = tsc;
1735 * First time this function is invoked in a given cycle, all of the
1736 * previous sample data fields are equal to zero or stale and they must
1737 * be populated with meaningful numbers for things to work, so assume
1738 * that sample.time will always be reset before setting the utilization
1739 * update hook and make the caller skip the sample then.
1741 if (cpu->last_sample_time) {
1742 intel_pstate_calc_avg_perf(cpu);
1748 static inline int32_t get_avg_frequency(struct cpudata *cpu)
1750 return mul_ext_fp(cpu->sample.core_avg_perf, cpu_khz);
1753 static inline int32_t get_avg_pstate(struct cpudata *cpu)
1755 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1756 cpu->sample.core_avg_perf);
1759 static inline int32_t get_target_pstate(struct cpudata *cpu)
1761 struct sample *sample = &cpu->sample;
1763 int target, avg_pstate;
1765 busy_frac = div_fp(sample->mperf << cpu->aperf_mperf_shift,
1768 if (busy_frac < cpu->iowait_boost)
1769 busy_frac = cpu->iowait_boost;
1771 sample->busy_scaled = busy_frac * 100;
1773 target = global.no_turbo || global.turbo_disabled ?
1774 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1775 target += target >> 2;
1776 target = mul_fp(target, busy_frac);
1777 if (target < cpu->pstate.min_pstate)
1778 target = cpu->pstate.min_pstate;
1781 * If the average P-state during the previous cycle was higher than the
1782 * current target, add 50% of the difference to the target to reduce
1783 * possible performance oscillations and offset possible performance
1784 * loss related to moving the workload from one CPU to another within
1787 avg_pstate = get_avg_pstate(cpu);
1788 if (avg_pstate > target)
1789 target += (avg_pstate - target) >> 1;
1794 static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
1796 int min_pstate = max(cpu->pstate.min_pstate, cpu->min_perf_ratio);
1797 int max_pstate = max(min_pstate, cpu->max_perf_ratio);
1799 return clamp_t(int, pstate, min_pstate, max_pstate);
1802 static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1804 if (pstate == cpu->pstate.current_pstate)
1807 cpu->pstate.current_pstate = pstate;
1808 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1811 static void intel_pstate_adjust_pstate(struct cpudata *cpu)
1813 int from = cpu->pstate.current_pstate;
1814 struct sample *sample;
1817 update_turbo_state();
1819 target_pstate = get_target_pstate(cpu);
1820 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1821 trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
1822 intel_pstate_update_pstate(cpu, target_pstate);
1824 sample = &cpu->sample;
1825 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
1826 fp_toint(sample->busy_scaled),
1828 cpu->pstate.current_pstate,
1832 get_avg_frequency(cpu),
1833 fp_toint(cpu->iowait_boost * 100));
1836 static void intel_pstate_update_util(struct update_util_data *data, u64 time,
1839 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
1842 /* Don't allow remote callbacks */
1843 if (smp_processor_id() != cpu->cpu)
1846 delta_ns = time - cpu->last_update;
1847 if (flags & SCHED_CPUFREQ_IOWAIT) {
1848 /* Start over if the CPU may have been idle. */
1849 if (delta_ns > TICK_NSEC) {
1850 cpu->iowait_boost = ONE_EIGHTH_FP;
1851 } else if (cpu->iowait_boost >= ONE_EIGHTH_FP) {
1852 cpu->iowait_boost <<= 1;
1853 if (cpu->iowait_boost > int_tofp(1))
1854 cpu->iowait_boost = int_tofp(1);
1856 cpu->iowait_boost = ONE_EIGHTH_FP;
1858 } else if (cpu->iowait_boost) {
1859 /* Clear iowait_boost if the CPU may have been idle. */
1860 if (delta_ns > TICK_NSEC)
1861 cpu->iowait_boost = 0;
1863 cpu->iowait_boost >>= 1;
1865 cpu->last_update = time;
1866 delta_ns = time - cpu->sample.time;
1867 if ((s64)delta_ns < INTEL_PSTATE_SAMPLING_INTERVAL)
1870 if (intel_pstate_sample(cpu, time))
1871 intel_pstate_adjust_pstate(cpu);
1874 static struct pstate_funcs core_funcs = {
1875 .get_max = core_get_max_pstate,
1876 .get_max_physical = core_get_max_pstate_physical,
1877 .get_min = core_get_min_pstate,
1878 .get_turbo = core_get_turbo_pstate,
1879 .get_scaling = core_get_scaling,
1880 .get_val = core_get_val,
1883 static const struct pstate_funcs silvermont_funcs = {
1884 .get_max = atom_get_max_pstate,
1885 .get_max_physical = atom_get_max_pstate,
1886 .get_min = atom_get_min_pstate,
1887 .get_turbo = atom_get_turbo_pstate,
1888 .get_val = atom_get_val,
1889 .get_scaling = silvermont_get_scaling,
1890 .get_vid = atom_get_vid,
1893 static const struct pstate_funcs airmont_funcs = {
1894 .get_max = atom_get_max_pstate,
1895 .get_max_physical = atom_get_max_pstate,
1896 .get_min = atom_get_min_pstate,
1897 .get_turbo = atom_get_turbo_pstate,
1898 .get_val = atom_get_val,
1899 .get_scaling = airmont_get_scaling,
1900 .get_vid = atom_get_vid,
1903 static const struct pstate_funcs knl_funcs = {
1904 .get_max = core_get_max_pstate,
1905 .get_max_physical = core_get_max_pstate_physical,
1906 .get_min = core_get_min_pstate,
1907 .get_turbo = knl_get_turbo_pstate,
1908 .get_aperf_mperf_shift = knl_get_aperf_mperf_shift,
1909 .get_scaling = core_get_scaling,
1910 .get_val = core_get_val,
1913 #define ICPU(model, policy) \
1914 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1915 (unsigned long)&policy }
1917 static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
1918 ICPU(INTEL_FAM6_SANDYBRIDGE, core_funcs),
1919 ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_funcs),
1920 ICPU(INTEL_FAM6_ATOM_SILVERMONT, silvermont_funcs),
1921 ICPU(INTEL_FAM6_IVYBRIDGE, core_funcs),
1922 ICPU(INTEL_FAM6_HASWELL, core_funcs),
1923 ICPU(INTEL_FAM6_BROADWELL, core_funcs),
1924 ICPU(INTEL_FAM6_IVYBRIDGE_X, core_funcs),
1925 ICPU(INTEL_FAM6_HASWELL_X, core_funcs),
1926 ICPU(INTEL_FAM6_HASWELL_L, core_funcs),
1927 ICPU(INTEL_FAM6_HASWELL_G, core_funcs),
1928 ICPU(INTEL_FAM6_BROADWELL_G, core_funcs),
1929 ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_funcs),
1930 ICPU(INTEL_FAM6_SKYLAKE_L, core_funcs),
1931 ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1932 ICPU(INTEL_FAM6_SKYLAKE, core_funcs),
1933 ICPU(INTEL_FAM6_BROADWELL_D, core_funcs),
1934 ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_funcs),
1935 ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_funcs),
1936 ICPU(INTEL_FAM6_ATOM_GOLDMONT, core_funcs),
1937 ICPU(INTEL_FAM6_ATOM_GOLDMONT_PLUS, core_funcs),
1938 ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
1941 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1943 static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
1944 ICPU(INTEL_FAM6_BROADWELL_D, core_funcs),
1945 ICPU(INTEL_FAM6_BROADWELL_X, core_funcs),
1946 ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
1950 static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
1951 ICPU(INTEL_FAM6_KABYLAKE, core_funcs),
1955 static const struct x86_cpu_id intel_pstate_hwp_boost_ids[] = {
1956 ICPU(INTEL_FAM6_SKYLAKE_X, core_funcs),
1957 ICPU(INTEL_FAM6_SKYLAKE, core_funcs),
1961 static int intel_pstate_init_cpu(unsigned int cpunum)
1963 struct cpudata *cpu;
1965 cpu = all_cpu_data[cpunum];
1968 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
1972 all_cpu_data[cpunum] = cpu;
1974 cpu->epp_default = -EINVAL;
1975 cpu->epp_powersave = -EINVAL;
1976 cpu->epp_saved = -EINVAL;
1979 cpu = all_cpu_data[cpunum];
1984 const struct x86_cpu_id *id;
1986 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
1988 intel_pstate_disable_ee(cpunum);
1990 intel_pstate_hwp_enable(cpu);
1992 id = x86_match_cpu(intel_pstate_hwp_boost_ids);
1993 if (id && intel_pstate_acpi_pm_profile_server())
1997 intel_pstate_get_cpu_pstates(cpu);
1999 pr_debug("controlling: cpu %d\n", cpunum);
2004 static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
2006 struct cpudata *cpu = all_cpu_data[cpu_num];
2008 if (hwp_active && !hwp_boost)
2011 if (cpu->update_util_set)
2014 /* Prevent intel_pstate_update_util() from using stale data. */
2015 cpu->sample.time = 0;
2016 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
2018 intel_pstate_update_util_hwp :
2019 intel_pstate_update_util));
2020 cpu->update_util_set = true;
2023 static void intel_pstate_clear_update_util_hook(unsigned int cpu)
2025 struct cpudata *cpu_data = all_cpu_data[cpu];
2027 if (!cpu_data->update_util_set)
2030 cpufreq_remove_update_util_hook(cpu);
2031 cpu_data->update_util_set = false;
2035 static int intel_pstate_get_max_freq(struct cpudata *cpu)
2037 return global.turbo_disabled || global.no_turbo ?
2038 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2041 static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
2042 struct cpudata *cpu)
2044 int max_freq = intel_pstate_get_max_freq(cpu);
2045 int32_t max_policy_perf, min_policy_perf;
2046 int max_state, turbo_max;
2049 * HWP needs some special consideration, because on BDX the
2050 * HWP_REQUEST uses abstract value to represent performance
2051 * rather than pure ratios.
2054 intel_pstate_get_hwp_max(cpu->cpu, &turbo_max, &max_state);
2056 max_state = global.no_turbo || global.turbo_disabled ?
2057 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2058 turbo_max = cpu->pstate.turbo_pstate;
2061 max_policy_perf = max_state * policy->max / max_freq;
2062 if (policy->max == policy->min) {
2063 min_policy_perf = max_policy_perf;
2065 min_policy_perf = max_state * policy->min / max_freq;
2066 min_policy_perf = clamp_t(int32_t, min_policy_perf,
2067 0, max_policy_perf);
2070 pr_debug("cpu:%d max_state %d min_policy_perf:%d max_policy_perf:%d\n",
2071 policy->cpu, max_state,
2072 min_policy_perf, max_policy_perf);
2074 /* Normalize user input to [min_perf, max_perf] */
2075 if (per_cpu_limits) {
2076 cpu->min_perf_ratio = min_policy_perf;
2077 cpu->max_perf_ratio = max_policy_perf;
2079 int32_t global_min, global_max;
2081 /* Global limits are in percent of the maximum turbo P-state. */
2082 global_max = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2083 global_min = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2084 global_min = clamp_t(int32_t, global_min, 0, global_max);
2086 pr_debug("cpu:%d global_min:%d global_max:%d\n", policy->cpu,
2087 global_min, global_max);
2089 cpu->min_perf_ratio = max(min_policy_perf, global_min);
2090 cpu->min_perf_ratio = min(cpu->min_perf_ratio, max_policy_perf);
2091 cpu->max_perf_ratio = min(max_policy_perf, global_max);
2092 cpu->max_perf_ratio = max(min_policy_perf, cpu->max_perf_ratio);
2094 /* Make sure min_perf <= max_perf */
2095 cpu->min_perf_ratio = min(cpu->min_perf_ratio,
2096 cpu->max_perf_ratio);
2099 pr_debug("cpu:%d max_perf_ratio:%d min_perf_ratio:%d\n", policy->cpu,
2100 cpu->max_perf_ratio,
2101 cpu->min_perf_ratio);
2104 static int intel_pstate_set_policy(struct cpufreq_policy *policy)
2106 struct cpudata *cpu;
2108 if (!policy->cpuinfo.max_freq)
2111 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
2112 policy->cpuinfo.max_freq, policy->max);
2114 cpu = all_cpu_data[policy->cpu];
2115 cpu->policy = policy->policy;
2117 mutex_lock(&intel_pstate_limits_lock);
2119 intel_pstate_update_perf_limits(policy, cpu);
2121 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
2123 * NOHZ_FULL CPUs need this as the governor callback may not
2124 * be invoked on them.
2126 intel_pstate_clear_update_util_hook(policy->cpu);
2127 intel_pstate_max_within_limits(cpu);
2129 intel_pstate_set_update_util_hook(policy->cpu);
2134 * When hwp_boost was active before and dynamically it
2135 * was turned off, in that case we need to clear the
2139 intel_pstate_clear_update_util_hook(policy->cpu);
2140 intel_pstate_hwp_set(policy->cpu);
2143 mutex_unlock(&intel_pstate_limits_lock);
2148 static void intel_pstate_adjust_policy_max(struct cpufreq_policy *policy,
2149 struct cpudata *cpu)
2152 cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
2153 policy->max < policy->cpuinfo.max_freq &&
2154 policy->max > cpu->pstate.max_freq) {
2155 pr_debug("policy->max > max non turbo frequency\n");
2156 policy->max = policy->cpuinfo.max_freq;
2160 static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
2162 struct cpudata *cpu = all_cpu_data[policy->cpu];
2164 update_turbo_state();
2165 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2166 intel_pstate_get_max_freq(cpu));
2168 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
2169 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
2172 intel_pstate_adjust_policy_max(policy, cpu);
2177 static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
2179 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
2182 static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
2184 pr_debug("CPU %d exiting\n", policy->cpu);
2186 intel_pstate_clear_update_util_hook(policy->cpu);
2188 intel_pstate_hwp_save_state(policy);
2189 intel_pstate_hwp_force_min_perf(policy->cpu);
2191 intel_cpufreq_stop_cpu(policy);
2195 static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
2197 intel_pstate_exit_perf_limits(policy);
2199 policy->fast_switch_possible = false;
2204 static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
2206 struct cpudata *cpu;
2209 rc = intel_pstate_init_cpu(policy->cpu);
2213 cpu = all_cpu_data[policy->cpu];
2215 cpu->max_perf_ratio = 0xFF;
2216 cpu->min_perf_ratio = 0;
2218 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
2219 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
2221 /* cpuinfo and default policy values */
2222 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
2223 update_turbo_state();
2224 global.turbo_disabled_mf = global.turbo_disabled;
2225 policy->cpuinfo.max_freq = global.turbo_disabled ?
2226 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2227 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
2230 unsigned int max_freq;
2232 max_freq = global.turbo_disabled ?
2233 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2234 if (max_freq < policy->cpuinfo.max_freq)
2235 policy->cpuinfo.max_freq = max_freq;
2238 intel_pstate_init_acpi_perf_limits(policy);
2240 policy->fast_switch_possible = true;
2245 static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
2247 int ret = __intel_pstate_cpu_init(policy);
2252 if (IS_ENABLED(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE))
2253 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
2255 policy->policy = CPUFREQ_POLICY_POWERSAVE;
2260 static struct cpufreq_driver intel_pstate = {
2261 .flags = CPUFREQ_CONST_LOOPS,
2262 .verify = intel_pstate_verify_policy,
2263 .setpolicy = intel_pstate_set_policy,
2264 .suspend = intel_pstate_hwp_save_state,
2265 .resume = intel_pstate_resume,
2266 .init = intel_pstate_cpu_init,
2267 .exit = intel_pstate_cpu_exit,
2268 .stop_cpu = intel_pstate_stop_cpu,
2269 .update_limits = intel_pstate_update_limits,
2270 .name = "intel_pstate",
2273 static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
2275 struct cpudata *cpu = all_cpu_data[policy->cpu];
2277 update_turbo_state();
2278 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2279 intel_pstate_get_max_freq(cpu));
2281 intel_pstate_adjust_policy_max(policy, cpu);
2283 intel_pstate_update_perf_limits(policy, cpu);
2288 /* Use of trace in passive mode:
2290 * In passive mode the trace core_busy field (also known as the
2291 * performance field, and lablelled as such on the graphs; also known as
2292 * core_avg_perf) is not needed and so is re-assigned to indicate if the
2293 * driver call was via the normal or fast switch path. Various graphs
2294 * output from the intel_pstate_tracer.py utility that include core_busy
2295 * (or performance or core_avg_perf) have a fixed y-axis from 0 to 100%,
2296 * so we use 10 to indicate the the normal path through the driver, and
2297 * 90 to indicate the fast switch path through the driver.
2298 * The scaled_busy field is not used, and is set to 0.
2301 #define INTEL_PSTATE_TRACE_TARGET 10
2302 #define INTEL_PSTATE_TRACE_FAST_SWITCH 90
2304 static void intel_cpufreq_trace(struct cpudata *cpu, unsigned int trace_type, int old_pstate)
2306 struct sample *sample;
2308 if (!trace_pstate_sample_enabled())
2311 if (!intel_pstate_sample(cpu, ktime_get()))
2314 sample = &cpu->sample;
2315 trace_pstate_sample(trace_type,
2318 cpu->pstate.current_pstate,
2322 get_avg_frequency(cpu),
2323 fp_toint(cpu->iowait_boost * 100));
2326 static int intel_cpufreq_target(struct cpufreq_policy *policy,
2327 unsigned int target_freq,
2328 unsigned int relation)
2330 struct cpudata *cpu = all_cpu_data[policy->cpu];
2331 struct cpufreq_freqs freqs;
2332 int target_pstate, old_pstate;
2334 update_turbo_state();
2336 freqs.old = policy->cur;
2337 freqs.new = target_freq;
2339 cpufreq_freq_transition_begin(policy, &freqs);
2341 case CPUFREQ_RELATION_L:
2342 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
2344 case CPUFREQ_RELATION_H:
2345 target_pstate = freqs.new / cpu->pstate.scaling;
2348 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
2351 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2352 old_pstate = cpu->pstate.current_pstate;
2353 if (target_pstate != cpu->pstate.current_pstate) {
2354 cpu->pstate.current_pstate = target_pstate;
2355 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
2356 pstate_funcs.get_val(cpu, target_pstate));
2358 freqs.new = target_pstate * cpu->pstate.scaling;
2359 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_TARGET, old_pstate);
2360 cpufreq_freq_transition_end(policy, &freqs, false);
2365 static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
2366 unsigned int target_freq)
2368 struct cpudata *cpu = all_cpu_data[policy->cpu];
2369 int target_pstate, old_pstate;
2371 update_turbo_state();
2373 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
2374 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2375 old_pstate = cpu->pstate.current_pstate;
2376 intel_pstate_update_pstate(cpu, target_pstate);
2377 intel_cpufreq_trace(cpu, INTEL_PSTATE_TRACE_FAST_SWITCH, old_pstate);
2378 return target_pstate * cpu->pstate.scaling;
2381 static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
2383 int max_state, turbo_max, min_freq, max_freq, ret;
2384 struct freq_qos_request *req;
2385 struct cpudata *cpu;
2388 dev = get_cpu_device(policy->cpu);
2392 ret = __intel_pstate_cpu_init(policy);
2396 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
2397 policy->transition_delay_us = INTEL_CPUFREQ_TRANSITION_DELAY;
2398 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
2399 policy->cur = policy->cpuinfo.min_freq;
2401 req = kcalloc(2, sizeof(*req), GFP_KERNEL);
2407 cpu = all_cpu_data[policy->cpu];
2410 intel_pstate_get_hwp_max(policy->cpu, &turbo_max, &max_state);
2412 turbo_max = cpu->pstate.turbo_pstate;
2414 min_freq = DIV_ROUND_UP(turbo_max * global.min_perf_pct, 100);
2415 min_freq *= cpu->pstate.scaling;
2416 max_freq = DIV_ROUND_UP(turbo_max * global.max_perf_pct, 100);
2417 max_freq *= cpu->pstate.scaling;
2419 ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MIN,
2422 dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
2426 ret = freq_qos_add_request(&policy->constraints, req + 1, FREQ_QOS_MAX,
2429 dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
2430 goto remove_min_req;
2433 policy->driver_data = req;
2438 freq_qos_remove_request(req);
2442 intel_pstate_exit_perf_limits(policy);
2447 static int intel_cpufreq_cpu_exit(struct cpufreq_policy *policy)
2449 struct freq_qos_request *req;
2451 req = policy->driver_data;
2453 freq_qos_remove_request(req + 1);
2454 freq_qos_remove_request(req);
2457 return intel_pstate_cpu_exit(policy);
2460 static struct cpufreq_driver intel_cpufreq = {
2461 .flags = CPUFREQ_CONST_LOOPS,
2462 .verify = intel_cpufreq_verify_policy,
2463 .target = intel_cpufreq_target,
2464 .fast_switch = intel_cpufreq_fast_switch,
2465 .init = intel_cpufreq_cpu_init,
2466 .exit = intel_cpufreq_cpu_exit,
2467 .stop_cpu = intel_cpufreq_stop_cpu,
2468 .update_limits = intel_pstate_update_limits,
2469 .name = "intel_cpufreq",
2472 static struct cpufreq_driver *default_driver = &intel_pstate;
2474 static void intel_pstate_driver_cleanup(void)
2479 for_each_online_cpu(cpu) {
2480 if (all_cpu_data[cpu]) {
2481 if (intel_pstate_driver == &intel_pstate)
2482 intel_pstate_clear_update_util_hook(cpu);
2484 kfree(all_cpu_data[cpu]);
2485 all_cpu_data[cpu] = NULL;
2489 intel_pstate_driver = NULL;
2492 static int intel_pstate_register_driver(struct cpufreq_driver *driver)
2496 memset(&global, 0, sizeof(global));
2497 global.max_perf_pct = 100;
2499 intel_pstate_driver = driver;
2500 ret = cpufreq_register_driver(intel_pstate_driver);
2502 intel_pstate_driver_cleanup();
2506 global.min_perf_pct = min_perf_pct_min();
2511 static int intel_pstate_unregister_driver(void)
2516 cpufreq_unregister_driver(intel_pstate_driver);
2517 intel_pstate_driver_cleanup();
2522 static ssize_t intel_pstate_show_status(char *buf)
2524 if (!intel_pstate_driver)
2525 return sprintf(buf, "off\n");
2527 return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
2528 "active" : "passive");
2531 static int intel_pstate_update_status(const char *buf, size_t size)
2535 if (size == 3 && !strncmp(buf, "off", size))
2536 return intel_pstate_driver ?
2537 intel_pstate_unregister_driver() : -EINVAL;
2539 if (size == 6 && !strncmp(buf, "active", size)) {
2540 if (intel_pstate_driver) {
2541 if (intel_pstate_driver == &intel_pstate)
2544 ret = intel_pstate_unregister_driver();
2549 return intel_pstate_register_driver(&intel_pstate);
2552 if (size == 7 && !strncmp(buf, "passive", size)) {
2553 if (intel_pstate_driver) {
2554 if (intel_pstate_driver == &intel_cpufreq)
2557 ret = intel_pstate_unregister_driver();
2562 return intel_pstate_register_driver(&intel_cpufreq);
2568 static int no_load __initdata;
2569 static int no_hwp __initdata;
2570 static int hwp_only __initdata;
2571 static unsigned int force_load __initdata;
2573 static int __init intel_pstate_msrs_not_valid(void)
2575 if (!pstate_funcs.get_max() ||
2576 !pstate_funcs.get_min() ||
2577 !pstate_funcs.get_turbo())
2583 static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
2585 pstate_funcs.get_max = funcs->get_max;
2586 pstate_funcs.get_max_physical = funcs->get_max_physical;
2587 pstate_funcs.get_min = funcs->get_min;
2588 pstate_funcs.get_turbo = funcs->get_turbo;
2589 pstate_funcs.get_scaling = funcs->get_scaling;
2590 pstate_funcs.get_val = funcs->get_val;
2591 pstate_funcs.get_vid = funcs->get_vid;
2592 pstate_funcs.get_aperf_mperf_shift = funcs->get_aperf_mperf_shift;
2597 static bool __init intel_pstate_no_acpi_pss(void)
2601 for_each_possible_cpu(i) {
2603 union acpi_object *pss;
2604 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2605 struct acpi_processor *pr = per_cpu(processors, i);
2610 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2611 if (ACPI_FAILURE(status))
2614 pss = buffer.pointer;
2615 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2623 pr_debug("ACPI _PSS not found\n");
2627 static bool __init intel_pstate_no_acpi_pcch(void)
2632 status = acpi_get_handle(NULL, "\\_SB", &handle);
2633 if (ACPI_FAILURE(status))
2636 if (acpi_has_method(handle, "PCCH"))
2640 pr_debug("ACPI PCCH not found\n");
2644 static bool __init intel_pstate_has_acpi_ppc(void)
2648 for_each_possible_cpu(i) {
2649 struct acpi_processor *pr = per_cpu(processors, i);
2653 if (acpi_has_method(pr->handle, "_PPC"))
2656 pr_debug("ACPI _PPC not found\n");
2665 /* Hardware vendor-specific info that has its own power management modes */
2666 static struct acpi_platform_list plat_info[] __initdata = {
2667 {"HP ", "ProLiant", 0, ACPI_SIG_FADT, all_versions, 0, PSS},
2668 {"ORACLE", "X4-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2669 {"ORACLE", "X4-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2670 {"ORACLE", "X4-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2671 {"ORACLE", "X3-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2672 {"ORACLE", "X3-2L ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2673 {"ORACLE", "X3-2B ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2674 {"ORACLE", "X4470M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2675 {"ORACLE", "X4270M3 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2676 {"ORACLE", "X4270M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2677 {"ORACLE", "X4170M2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2678 {"ORACLE", "X4170 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2679 {"ORACLE", "X4275 M3", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2680 {"ORACLE", "X6-2 ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2681 {"ORACLE", "Sudbury ", 0, ACPI_SIG_FADT, all_versions, 0, PPC},
2685 static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
2687 const struct x86_cpu_id *id;
2691 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2693 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2694 if (misc_pwr & (1 << 8)) {
2695 pr_debug("Bit 8 in the MISC_PWR_MGMT MSR set\n");
2700 idx = acpi_match_platform_list(plat_info);
2704 switch (plat_info[idx].data) {
2706 if (!intel_pstate_no_acpi_pss())
2709 return intel_pstate_no_acpi_pcch();
2711 return intel_pstate_has_acpi_ppc() && !force_load;
2717 static void intel_pstate_request_control_from_smm(void)
2720 * It may be unsafe to request P-states control from SMM if _PPC support
2721 * has not been enabled.
2724 acpi_processor_pstate_control();
2726 #else /* CONFIG_ACPI not enabled */
2727 static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
2728 static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
2729 static inline void intel_pstate_request_control_from_smm(void) {}
2730 #endif /* CONFIG_ACPI */
2732 #define INTEL_PSTATE_HWP_BROADWELL 0x01
2734 #define ICPU_HWP(model, hwp_mode) \
2735 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_HWP, hwp_mode }
2737 static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2738 ICPU_HWP(INTEL_FAM6_BROADWELL_X, INTEL_PSTATE_HWP_BROADWELL),
2739 ICPU_HWP(INTEL_FAM6_BROADWELL_D, INTEL_PSTATE_HWP_BROADWELL),
2740 ICPU_HWP(X86_MODEL_ANY, 0),
2744 static int __init intel_pstate_init(void)
2746 const struct x86_cpu_id *id;
2749 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
2755 id = x86_match_cpu(hwp_support_ids);
2757 copy_cpu_funcs(&core_funcs);
2760 hwp_mode_bdw = id->driver_data;
2761 intel_pstate.attr = hwp_cpufreq_attrs;
2762 goto hwp_cpu_matched;
2765 id = x86_match_cpu(intel_pstate_cpu_ids);
2767 pr_info("CPU model not supported\n");
2771 copy_cpu_funcs((struct pstate_funcs *)id->driver_data);
2774 if (intel_pstate_msrs_not_valid()) {
2775 pr_info("Invalid MSRs\n");
2781 * The Intel pstate driver will be ignored if the platform
2782 * firmware has its own power management modes.
2784 if (intel_pstate_platform_pwr_mgmt_exists()) {
2785 pr_info("P-states controlled by the platform\n");
2789 if (!hwp_active && hwp_only)
2792 pr_info("Intel P-state driver initializing\n");
2794 all_cpu_data = vzalloc(array_size(sizeof(void *), num_possible_cpus()));
2798 intel_pstate_request_control_from_smm();
2800 intel_pstate_sysfs_expose_params();
2802 mutex_lock(&intel_pstate_driver_lock);
2803 rc = intel_pstate_register_driver(default_driver);
2804 mutex_unlock(&intel_pstate_driver_lock);
2809 pr_info("HWP enabled\n");
2813 device_initcall(intel_pstate_init);
2815 static int __init intel_pstate_setup(char *str)
2820 if (!strcmp(str, "disable")) {
2822 } else if (!strcmp(str, "passive")) {
2823 pr_info("Passive mode enabled\n");
2824 default_driver = &intel_cpufreq;
2827 if (!strcmp(str, "no_hwp")) {
2828 pr_info("HWP disabled\n");
2831 if (!strcmp(str, "force"))
2833 if (!strcmp(str, "hwp_only"))
2835 if (!strcmp(str, "per_cpu_perf_limits"))
2836 per_cpu_limits = true;
2839 if (!strcmp(str, "support_acpi_ppc"))
2845 early_param("intel_pstate", intel_pstate_setup);
2847 MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2848 MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2849 MODULE_LICENSE("GPL");