blob: 6e68b556305a681cc9788dfcd14efce83d0c15e3 [file] [log] [blame]
Dirk Brandewie93f08222013-02-06 09:02:13 -08001/*
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00002 * intel_pstate.c: Native P state management for Intel processors
Dirk Brandewie93f08222013-02-06 09:02:13 -08003 *
4 * (C) Copyright 2012 Intel Corporation
5 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
11 */
12
Joe Perches4836df12016-04-05 13:28:23 -070013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Dirk Brandewie93f08222013-02-06 09:02:13 -080015#include <linux/kernel.h>
16#include <linux/kernel_stat.h>
17#include <linux/module.h>
18#include <linux/ktime.h>
19#include <linux/hrtimer.h>
20#include <linux/tick.h>
21#include <linux/slab.h>
22#include <linux/sched.h>
23#include <linux/list.h>
24#include <linux/cpu.h>
25#include <linux/cpufreq.h>
26#include <linux/sysfs.h>
27#include <linux/types.h>
28#include <linux/fs.h>
29#include <linux/debugfs.h>
Adrian Huangfbbcdc02013-10-31 23:24:05 +080030#include <linux/acpi.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100031#include <linux/vmalloc.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080032#include <trace/events/power.h>
33
34#include <asm/div64.h>
35#include <asm/msr.h>
36#include <asm/cpu_device_id.h>
Borislav Petkov64df1fd2015-04-03 15:19:53 +020037#include <asm/cpufeature.h>
Dave Hansen5b20c942016-06-02 17:19:45 -070038#include <asm/intel-family.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080039
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +010040#define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
41
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -070042#ifdef CONFIG_ACPI
43#include <acpi/processor.h>
Rafael J. Wysocki17669002016-11-22 12:24:00 -080044#include <acpi/cppc_acpi.h>
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -070045#endif
46
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070047#define FRAC_BITS 8
Dirk Brandewie93f08222013-02-06 09:02:13 -080048#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
49#define fp_toint(X) ((X) >> FRAC_BITS)
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070050
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020051#define EXT_BITS 6
52#define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -080053#define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
54#define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020055
Dirk Brandewie93f08222013-02-06 09:02:13 -080056static inline int32_t mul_fp(int32_t x, int32_t y)
57{
58 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
59}
60
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040061static inline int32_t div_fp(s64 x, s64 y)
Dirk Brandewie93f08222013-02-06 09:02:13 -080062{
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040063 return div64_s64((int64_t)x << FRAC_BITS, y);
Dirk Brandewie93f08222013-02-06 09:02:13 -080064}
65
Dirk Brandewied022a652014-10-13 08:37:44 -070066static inline int ceiling_fp(int32_t x)
67{
68 int mask, ret;
69
70 ret = fp_toint(x);
71 mask = (1 << FRAC_BITS) - 1;
72 if (x & mask)
73 ret += 1;
74 return ret;
75}
76
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020077static inline u64 mul_ext_fp(u64 x, u64 y)
78{
79 return (x * y) >> EXT_FRAC_BITS;
80}
81
82static inline u64 div_ext_fp(u64 x, u64 y)
83{
84 return div64_u64(x << EXT_FRAC_BITS, y);
85}
86
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070087/**
88 * struct sample - Store performance sample
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020089 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070090 * performance during last sample period
91 * @busy_scaled: Scaled busy value which is used to calculate next
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020092 * P state. This can be different than core_avg_perf
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070093 * to account for cpu idle period
94 * @aperf: Difference of actual performance frequency clock count
95 * read from APERF MSR between last and current sample
96 * @mperf: Difference of maximum performance frequency clock count
97 * read from MPERF MSR between last and current sample
98 * @tsc: Difference of time stamp counter between last and
99 * current sample
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700100 * @time: Current time from scheduler
101 *
102 * This structure is used in the cpudata structure to store performance sample
103 * data for choosing next P State.
104 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800105struct sample {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +0200106 int32_t core_avg_perf;
Philippe Longepe157386b2015-12-04 17:40:30 +0100107 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800108 u64 aperf;
109 u64 mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700110 u64 tsc;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100111 u64 time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800112};
113
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700114/**
115 * struct pstate_data - Store P state data
116 * @current_pstate: Current requested P state
117 * @min_pstate: Min P state possible for this platform
118 * @max_pstate: Max P state possible for this platform
119 * @max_pstate_physical:This is physical Max P state for a processor
120 * This can be higher than the max_pstate which can
121 * be limited by platform thermal design power limits
122 * @scaling: Scaling factor to convert frequency to cpufreq
123 * frequency units
124 * @turbo_pstate: Max Turbo P state possible for this platform
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +0100125 * @max_freq: @max_pstate frequency in cpufreq units
126 * @turbo_freq: @turbo_pstate frequency in cpufreq units
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700127 *
128 * Stores the per cpu model P state limits and current P state.
129 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800130struct pstate_data {
131 int current_pstate;
132 int min_pstate;
133 int max_pstate;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700134 int max_pstate_physical;
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700135 int scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800136 int turbo_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +0100137 unsigned int max_freq;
138 unsigned int turbo_freq;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800139};
140
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700141/**
142 * struct vid_data - Stores voltage information data
143 * @min: VID data for this platform corresponding to
144 * the lowest P state
145 * @max: VID data corresponding to the highest P State.
146 * @turbo: VID data for turbo P state
147 * @ratio: Ratio of (vid max - vid min) /
148 * (max P state - Min P State)
149 *
150 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
151 * This data is used in Atom platforms, where in addition to target P state,
152 * the voltage data needs to be specified to select next P State.
153 */
Dirk Brandewie007bea02013-12-18 10:32:39 -0800154struct vid_data {
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700155 int min;
156 int max;
157 int turbo;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800158 int32_t ratio;
159};
160
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700161/**
162 * struct _pid - Stores PID data
163 * @setpoint: Target set point for busyness or performance
164 * @integral: Storage for accumulated error values
165 * @p_gain: PID proportional gain
166 * @i_gain: PID integral gain
167 * @d_gain: PID derivative gain
168 * @deadband: PID deadband
169 * @last_err: Last error storage for integral part of PID calculation
170 *
171 * Stores PID coefficients and last error for PID controller.
172 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800173struct _pid {
174 int setpoint;
175 int32_t integral;
176 int32_t p_gain;
177 int32_t i_gain;
178 int32_t d_gain;
179 int deadband;
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700180 int32_t last_err;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800181};
182
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700183/**
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700184 * struct perf_limits - Store user and policy limits
185 * @no_turbo: User requested turbo state from intel_pstate sysfs
186 * @turbo_disabled: Platform turbo status either from msr
187 * MSR_IA32_MISC_ENABLE or when maximum available pstate
188 * matches the maximum turbo pstate
189 * @max_perf_pct: Effective maximum performance limit in percentage, this
190 * is minimum of either limits enforced by cpufreq policy
191 * or limits from user set limits via intel_pstate sysfs
192 * @min_perf_pct: Effective minimum performance limit in percentage, this
193 * is maximum of either limits enforced by cpufreq policy
194 * or limits from user set limits via intel_pstate sysfs
195 * @max_perf: This is a scaled value between 0 to 255 for max_perf_pct
196 * This value is used to limit max pstate
197 * @min_perf: This is a scaled value between 0 to 255 for min_perf_pct
198 * This value is used to limit min pstate
199 * @max_policy_pct: The maximum performance in percentage enforced by
200 * cpufreq setpolicy interface
201 * @max_sysfs_pct: The maximum performance in percentage enforced by
202 * intel pstate sysfs interface, unused when per cpu
203 * controls are enforced
204 * @min_policy_pct: The minimum performance in percentage enforced by
205 * cpufreq setpolicy interface
206 * @min_sysfs_pct: The minimum performance in percentage enforced by
207 * intel pstate sysfs interface, unused when per cpu
208 * controls are enforced
209 *
210 * Storage for user and policy defined limits.
211 */
212struct perf_limits {
213 int no_turbo;
214 int turbo_disabled;
215 int max_perf_pct;
216 int min_perf_pct;
217 int32_t max_perf;
218 int32_t min_perf;
219 int max_policy_pct;
220 int max_sysfs_pct;
221 int min_policy_pct;
222 int min_sysfs_pct;
223};
224
225/**
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700226 * struct cpudata - Per CPU instance data storage
227 * @cpu: CPU number for this instance data
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200228 * @policy: CPUFreq policy value
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700229 * @update_util: CPUFreq utility callback information
Chen Yu4578ee7e2016-05-11 14:33:08 +0800230 * @update_util_set: CPUFreq utility callback is set
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200231 * @iowait_boost: iowait-related boost fraction
232 * @last_update: Time of the last update.
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700233 * @pstate: Stores P state limits for this CPU
234 * @vid: Stores VID limits for this CPU
235 * @pid: Stores PID parameters for this CPU
236 * @last_sample_time: Last Sample time
237 * @prev_aperf: Last APERF value read from APERF MSR
238 * @prev_mperf: Last MPERF value read from MPERF MSR
239 * @prev_tsc: Last timestamp counter (TSC) value
240 * @prev_cummulative_iowait: IO Wait time difference from last and
241 * current sample
242 * @sample: Storage for storing last Sample data
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700243 * @perf_limits: Pointer to perf_limit unique to this CPU
244 * Not all field in the structure are applicable
245 * when per cpu controls are enforced
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700246 * @acpi_perf_data: Stores ACPI perf information read from _PSS
247 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800248 * @epp_powersave: Last saved HWP energy performance preference
249 * (EPP) or energy performance bias (EPB),
250 * when policy switched to performance
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800251 * @epp_policy: Last saved policy used to set EPP/EPB
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800252 * @epp_default: Power on default HWP energy performance
253 * preference/bias
254 * @epp_saved: Saved EPP/EPB during system suspend or CPU offline
255 * operation
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700256 *
257 * This structure stores per CPU instance data for all CPUs.
258 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800259struct cpudata {
260 int cpu;
261
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200262 unsigned int policy;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100263 struct update_util_data update_util;
Chen Yu4578ee7e2016-05-11 14:33:08 +0800264 bool update_util_set;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800265
Dirk Brandewie93f08222013-02-06 09:02:13 -0800266 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800267 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800268 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800269
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200270 u64 last_update;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100271 u64 last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800272 u64 prev_aperf;
273 u64 prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700274 u64 prev_tsc;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100275 u64 prev_cummulative_iowait;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800276 struct sample sample;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700277 struct perf_limits *perf_limits;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700278#ifdef CONFIG_ACPI
279 struct acpi_processor_performance acpi_perf_data;
280 bool valid_pss_table;
281#endif
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200282 unsigned int iowait_boost;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800283 s16 epp_powersave;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800284 s16 epp_policy;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800285 s16 epp_default;
286 s16 epp_saved;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800287};
288
289static struct cpudata **all_cpu_data;
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700290
291/**
Rafael J. Wysocki39545172016-10-11 23:07:38 +0200292 * struct pstate_adjust_policy - Stores static PID configuration data
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700293 * @sample_rate_ms: PID calculation sample rate in ms
294 * @sample_rate_ns: Sample rate calculation in ns
295 * @deadband: PID deadband
296 * @setpoint: PID Setpoint
297 * @p_gain_pct: PID proportional gain
298 * @i_gain_pct: PID integral gain
299 * @d_gain_pct: PID derivative gain
300 *
301 * Stores per CPU model static PID configuration data.
302 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800303struct pstate_adjust_policy {
304 int sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100305 s64 sample_rate_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800306 int deadband;
307 int setpoint;
308 int p_gain_pct;
309 int d_gain_pct;
310 int i_gain_pct;
311};
312
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700313/**
314 * struct pstate_funcs - Per CPU model specific callbacks
315 * @get_max: Callback to get maximum non turbo effective P state
316 * @get_max_physical: Callback to get maximum non turbo physical P state
317 * @get_min: Callback to get minimum P state
318 * @get_turbo: Callback to get turbo P state
319 * @get_scaling: Callback to get frequency scaling factor
320 * @get_val: Callback to convert P state to actual MSR write value
321 * @get_vid: Callback to get VID data for Atom platforms
322 * @get_target_pstate: Callback to a function to calculate next P state to use
323 *
324 * Core and Atom CPU models have different way to get P State limits. This
325 * structure is used to store those callbacks.
326 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700327struct pstate_funcs {
328 int (*get_max)(void);
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700329 int (*get_max_physical)(void);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700330 int (*get_min)(void);
331 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700332 int (*get_scaling)(void);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100333 u64 (*get_val)(struct cpudata*, int pstate);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800334 void (*get_vid)(struct cpudata *);
Philippe Longepe157386b2015-12-04 17:40:30 +0100335 int32_t (*get_target_pstate)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800336};
337
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700338/**
339 * struct cpu_defaults- Per CPU model default config data
340 * @pid_policy: PID config data
341 * @funcs: Callback function data
342 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700343struct cpu_defaults {
344 struct pstate_adjust_policy pid_policy;
345 struct pstate_funcs funcs;
346};
347
Philippe Longepe157386b2015-12-04 17:40:30 +0100348static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
Philippe Longepee70eed22015-12-04 17:40:32 +0100349static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
Philippe Longepe157386b2015-12-04 17:40:30 +0100350
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800351static struct pstate_adjust_policy pid_params __read_mostly;
352static struct pstate_funcs pstate_funcs __read_mostly;
353static int hwp_active __read_mostly;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700354static bool per_cpu_limits __read_mostly;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700355
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700356#ifdef CONFIG_ACPI
357static bool acpi_ppc;
358#endif
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700359
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400360static struct perf_limits performance_limits = {
361 .no_turbo = 0,
362 .turbo_disabled = 0,
363 .max_perf_pct = 100,
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800364 .max_perf = int_ext_tofp(1),
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400365 .min_perf_pct = 100,
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800366 .min_perf = int_ext_tofp(1),
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400367 .max_policy_pct = 100,
368 .max_sysfs_pct = 100,
369 .min_policy_pct = 0,
370 .min_sysfs_pct = 0,
371};
372
373static struct perf_limits powersave_limits = {
Dirk Brandewie93f08222013-02-06 09:02:13 -0800374 .no_turbo = 0,
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700375 .turbo_disabled = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800376 .max_perf_pct = 100,
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800377 .max_perf = int_ext_tofp(1),
Dirk Brandewie93f08222013-02-06 09:02:13 -0800378 .min_perf_pct = 0,
379 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700380 .max_policy_pct = 100,
381 .max_sysfs_pct = 100,
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800382 .min_policy_pct = 0,
383 .min_sysfs_pct = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800384};
385
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400386#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
387static struct perf_limits *limits = &performance_limits;
388#else
389static struct perf_limits *limits = &powersave_limits;
390#endif
391
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700392static DEFINE_MUTEX(intel_pstate_limits_lock);
393
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700394#ifdef CONFIG_ACPI
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700395
396static bool intel_pstate_get_ppc_enable_status(void)
397{
398 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
399 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
400 return true;
401
402 return acpi_ppc;
403}
404
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800405#ifdef CONFIG_ACPI_CPPC_LIB
406
407/* The work item is needed to avoid CPU hotplug locking issues */
408static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
409{
410 sched_set_itmt_support();
411}
412
413static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
414
415static void intel_pstate_set_itmt_prio(int cpu)
416{
417 struct cppc_perf_caps cppc_perf;
418 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
419 int ret;
420
421 ret = cppc_get_perf_caps(cpu, &cppc_perf);
422 if (ret)
423 return;
424
425 /*
426 * The priorities can be set regardless of whether or not
427 * sched_set_itmt_support(true) has been called and it is valid to
428 * update them at any time after it has been called.
429 */
430 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
431
432 if (max_highest_perf <= min_highest_perf) {
433 if (cppc_perf.highest_perf > max_highest_perf)
434 max_highest_perf = cppc_perf.highest_perf;
435
436 if (cppc_perf.highest_perf < min_highest_perf)
437 min_highest_perf = cppc_perf.highest_perf;
438
439 if (max_highest_perf > min_highest_perf) {
440 /*
441 * This code can be run during CPU online under the
442 * CPU hotplug locks, so sched_set_itmt_support()
443 * cannot be called from here. Queue up a work item
444 * to invoke it.
445 */
446 schedule_work(&sched_itmt_work);
447 }
448 }
449}
450#else
451static void intel_pstate_set_itmt_prio(int cpu)
452{
453}
454#endif
455
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700456static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
457{
458 struct cpudata *cpu;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700459 int ret;
460 int i;
461
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800462 if (hwp_active) {
463 intel_pstate_set_itmt_prio(policy->cpu);
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700464 return;
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800465 }
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700466
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700467 if (!intel_pstate_get_ppc_enable_status())
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700468 return;
469
470 cpu = all_cpu_data[policy->cpu];
471
472 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
473 policy->cpu);
474 if (ret)
475 return;
476
477 /*
478 * Check if the control value in _PSS is for PERF_CTL MSR, which should
479 * guarantee that the states returned by it map to the states in our
480 * list directly.
481 */
482 if (cpu->acpi_perf_data.control_register.space_id !=
483 ACPI_ADR_SPACE_FIXED_HARDWARE)
484 goto err;
485
486 /*
487 * If there is only one entry _PSS, simply ignore _PSS and continue as
488 * usual without taking _PSS into account
489 */
490 if (cpu->acpi_perf_data.state_count < 2)
491 goto err;
492
493 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
494 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
495 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
496 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
497 (u32) cpu->acpi_perf_data.states[i].core_frequency,
498 (u32) cpu->acpi_perf_data.states[i].power,
499 (u32) cpu->acpi_perf_data.states[i].control);
500 }
501
502 /*
503 * The _PSS table doesn't contain whole turbo frequency range.
504 * This just contains +1 MHZ above the max non turbo frequency,
505 * with control value corresponding to max turbo ratio. But
506 * when cpufreq set policy is called, it will call with this
507 * max frequency, which will cause a reduced performance as
508 * this driver uses real max turbo frequency as the max
509 * frequency. So correct this frequency in _PSS table to
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700510 * correct max turbo frequency based on the turbo state.
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700511 * Also need to convert to MHz as _PSS freq is in MHz.
512 */
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700513 if (!limits->turbo_disabled)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700514 cpu->acpi_perf_data.states[0].core_frequency =
515 policy->cpuinfo.max_freq / 1000;
516 cpu->valid_pss_table = true;
Srinivas Pandruvada6cacd112016-05-29 23:31:23 -0700517 pr_debug("_PPC limits will be enforced\n");
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700518
519 return;
520
521 err:
522 cpu->valid_pss_table = false;
523 acpi_processor_unregister_performance(policy->cpu);
524}
525
526static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
527{
528 struct cpudata *cpu;
529
530 cpu = all_cpu_data[policy->cpu];
531 if (!cpu->valid_pss_table)
532 return;
533
534 acpi_processor_unregister_performance(policy->cpu);
535}
536
537#else
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100538static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700539{
540}
541
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100542static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700543{
544}
545#endif
546
Dirk Brandewie93f08222013-02-06 09:02:13 -0800547static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700548 int deadband, int integral) {
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100549 pid->setpoint = int_tofp(setpoint);
550 pid->deadband = int_tofp(deadband);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800551 pid->integral = int_tofp(integral);
Dirk Brandewied98d0992014-02-12 10:01:05 -0800552 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800553}
554
555static inline void pid_p_gain_set(struct _pid *pid, int percent)
556{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200557 pid->p_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800558}
559
560static inline void pid_i_gain_set(struct _pid *pid, int percent)
561{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200562 pid->i_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800563}
564
565static inline void pid_d_gain_set(struct _pid *pid, int percent)
566{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200567 pid->d_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800568}
569
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700570static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800571{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700572 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800573 int32_t pterm, dterm, fp_error;
574 int32_t integral_limit;
575
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100576 fp_error = pid->setpoint - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800577
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100578 if (abs(fp_error) <= pid->deadband)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800579 return 0;
580
581 pterm = mul_fp(pid->p_gain, fp_error);
582
583 pid->integral += fp_error;
584
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800585 /*
586 * We limit the integral here so that it will never
587 * get higher than 30. This prevents it from becoming
588 * too large an input over long periods of time and allows
589 * it to get factored out sooner.
590 *
591 * The value of 30 was chosen through experimentation.
592 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800593 integral_limit = int_tofp(30);
594 if (pid->integral > integral_limit)
595 pid->integral = integral_limit;
596 if (pid->integral < -integral_limit)
597 pid->integral = -integral_limit;
598
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700599 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
600 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800601
602 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
Doug Smythies51d211e2014-06-17 13:36:10 -0700603 result = result + (1 << (FRAC_BITS-1));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800604 return (signed int)fp_toint(result);
605}
606
607static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
608{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700609 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
610 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
611 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800612
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700613 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800614}
615
Dirk Brandewie93f08222013-02-06 09:02:13 -0800616static inline void intel_pstate_reset_all_pid(void)
617{
618 unsigned int cpu;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700619
Dirk Brandewie93f08222013-02-06 09:02:13 -0800620 for_each_online_cpu(cpu) {
621 if (all_cpu_data[cpu])
622 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
623 }
624}
625
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700626static inline void update_turbo_state(void)
627{
628 u64 misc_en;
629 struct cpudata *cpu;
630
631 cpu = all_cpu_data[0];
632 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400633 limits->turbo_disabled =
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700634 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
635 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
636}
637
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800638static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
639{
640 u64 epb;
641 int ret;
642
643 if (!static_cpu_has(X86_FEATURE_EPB))
644 return -ENXIO;
645
646 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
647 if (ret)
648 return (s16)ret;
649
650 return (s16)(epb & 0x0f);
651}
652
653static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
654{
655 s16 epp;
656
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800657 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
658 /*
659 * When hwp_req_data is 0, means that caller didn't read
660 * MSR_HWP_REQUEST, so need to read and get EPP.
661 */
662 if (!hwp_req_data) {
663 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
664 &hwp_req_data);
665 if (epp)
666 return epp;
667 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800668 epp = (hwp_req_data >> 24) & 0xff;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800669 } else {
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800670 /* When there is no EPP present, HWP uses EPB settings */
671 epp = intel_pstate_get_epb(cpu_data);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800672 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800673
674 return epp;
675}
676
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800677static int intel_pstate_set_epb(int cpu, s16 pref)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800678{
679 u64 epb;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800680 int ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800681
682 if (!static_cpu_has(X86_FEATURE_EPB))
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800683 return -ENXIO;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800684
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800685 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
686 if (ret)
687 return ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800688
689 epb = (epb & ~0x0f) | pref;
690 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800691
692 return 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800693}
694
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800695/*
696 * EPP/EPB display strings corresponding to EPP index in the
697 * energy_perf_strings[]
698 * index String
699 *-------------------------------------
700 * 0 default
701 * 1 performance
702 * 2 balance_performance
703 * 3 balance_power
704 * 4 power
705 */
706static const char * const energy_perf_strings[] = {
707 "default",
708 "performance",
709 "balance_performance",
710 "balance_power",
711 "power",
712 NULL
713};
714
715static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
716{
717 s16 epp;
718 int index = -EINVAL;
719
720 epp = intel_pstate_get_epp(cpu_data, 0);
721 if (epp < 0)
722 return epp;
723
724 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
725 /*
726 * Range:
727 * 0x00-0x3F : Performance
728 * 0x40-0x7F : Balance performance
729 * 0x80-0xBF : Balance power
730 * 0xC0-0xFF : Power
731 * The EPP is a 8 bit value, but our ranges restrict the
732 * value which can be set. Here only using top two bits
733 * effectively.
734 */
735 index = (epp >> 6) + 1;
736 } else if (static_cpu_has(X86_FEATURE_EPB)) {
737 /*
738 * Range:
739 * 0x00-0x03 : Performance
740 * 0x04-0x07 : Balance performance
741 * 0x08-0x0B : Balance power
742 * 0x0C-0x0F : Power
743 * The EPB is a 4 bit value, but our ranges restrict the
744 * value which can be set. Here only using top two bits
745 * effectively.
746 */
747 index = (epp >> 2) + 1;
748 }
749
750 return index;
751}
752
753static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
754 int pref_index)
755{
756 int epp = -EINVAL;
757 int ret;
758
759 if (!pref_index)
760 epp = cpu_data->epp_default;
761
762 mutex_lock(&intel_pstate_limits_lock);
763
764 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
765 u64 value;
766
767 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
768 if (ret)
769 goto return_pref;
770
771 value &= ~GENMASK_ULL(31, 24);
772
773 /*
774 * If epp is not default, convert from index into
775 * energy_perf_strings to epp value, by shifting 6
776 * bits left to use only top two bits in epp.
777 * The resultant epp need to shifted by 24 bits to
778 * epp position in MSR_HWP_REQUEST.
779 */
780 if (epp == -EINVAL)
781 epp = (pref_index - 1) << 6;
782
783 value |= (u64)epp << 24;
784 ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
785 } else {
786 if (epp == -EINVAL)
787 epp = (pref_index - 1) << 2;
788 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
789 }
790return_pref:
791 mutex_unlock(&intel_pstate_limits_lock);
792
793 return ret;
794}
795
796static ssize_t show_energy_performance_available_preferences(
797 struct cpufreq_policy *policy, char *buf)
798{
799 int i = 0;
800 int ret = 0;
801
802 while (energy_perf_strings[i] != NULL)
803 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
804
805 ret += sprintf(&buf[ret], "\n");
806
807 return ret;
808}
809
810cpufreq_freq_attr_ro(energy_performance_available_preferences);
811
812static ssize_t store_energy_performance_preference(
813 struct cpufreq_policy *policy, const char *buf, size_t count)
814{
815 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
816 char str_preference[21];
817 int ret, i = 0;
818
819 ret = sscanf(buf, "%20s", str_preference);
820 if (ret != 1)
821 return -EINVAL;
822
823 while (energy_perf_strings[i] != NULL) {
824 if (!strcmp(str_preference, energy_perf_strings[i])) {
825 intel_pstate_set_energy_pref_index(cpu_data, i);
826 return count;
827 }
828 ++i;
829 }
830
831 return -EINVAL;
832}
833
834static ssize_t show_energy_performance_preference(
835 struct cpufreq_policy *policy, char *buf)
836{
837 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
838 int preference;
839
840 preference = intel_pstate_get_energy_pref_index(cpu_data);
841 if (preference < 0)
842 return preference;
843
844 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
845}
846
847cpufreq_freq_attr_rw(energy_performance_preference);
848
849static struct freq_attr *hwp_cpufreq_attrs[] = {
850 &energy_performance_preference,
851 &energy_performance_available_preferences,
852 NULL,
853};
854
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100855static void intel_pstate_hwp_set(struct cpufreq_policy *policy)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800856{
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700857 int min, hw_min, max, hw_max, cpu, range, adj_range;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700858 struct perf_limits *perf_limits = limits;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700859 u64 value, cap;
860
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100861 for_each_cpu(cpu, policy->cpus) {
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700862 int max_perf_pct, min_perf_pct;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800863 struct cpudata *cpu_data = all_cpu_data[cpu];
864 s16 epp;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700865
866 if (per_cpu_limits)
867 perf_limits = all_cpu_data[cpu]->perf_limits;
868
Srinivas Pandruvadaf9f48722016-10-08 12:42:38 -0700869 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
870 hw_min = HWP_LOWEST_PERF(cap);
871 hw_max = HWP_HIGHEST_PERF(cap);
872 range = hw_max - hw_min;
873
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700874 max_perf_pct = perf_limits->max_perf_pct;
875 min_perf_pct = perf_limits->min_perf_pct;
876
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800877 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700878 adj_range = min_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700879 min = hw_min + adj_range;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800880 value &= ~HWP_MIN_PERF(~0L);
881 value |= HWP_MIN_PERF(min);
882
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700883 adj_range = max_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700884 max = hw_min + adj_range;
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400885 if (limits->no_turbo) {
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700886 hw_max = HWP_GUARANTEED_PERF(cap);
887 if (hw_max < max)
888 max = hw_max;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800889 }
890
891 value &= ~HWP_MAX_PERF(~0L);
892 value |= HWP_MAX_PERF(max);
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800893
894 if (cpu_data->epp_policy == cpu_data->policy)
895 goto skip_epp;
896
897 cpu_data->epp_policy = cpu_data->policy;
898
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800899 if (cpu_data->epp_saved >= 0) {
900 epp = cpu_data->epp_saved;
901 cpu_data->epp_saved = -EINVAL;
902 goto update_epp;
903 }
904
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800905 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
906 epp = intel_pstate_get_epp(cpu_data, value);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800907 cpu_data->epp_powersave = epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800908 /* If EPP read was failed, then don't try to write */
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800909 if (epp < 0)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800910 goto skip_epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800911
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800912
913 epp = 0;
914 } else {
915 /* skip setting EPP, when saved value is invalid */
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800916 if (cpu_data->epp_powersave < 0)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800917 goto skip_epp;
918
919 /*
920 * No need to restore EPP when it is not zero. This
921 * means:
922 * - Policy is not changed
923 * - user has manually changed
924 * - Error reading EPB
925 */
926 epp = intel_pstate_get_epp(cpu_data, value);
927 if (epp)
928 goto skip_epp;
929
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800930 epp = cpu_data->epp_powersave;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800931 }
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800932update_epp:
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800933 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
934 value &= ~GENMASK_ULL(31, 24);
935 value |= (u64)epp << 24;
936 } else {
937 intel_pstate_set_epb(cpu, epp);
938 }
939skip_epp:
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800940 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
941 }
Viresh Kumar41cfd642016-02-22 10:27:46 +0530942}
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800943
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +0200944static int intel_pstate_hwp_set_policy(struct cpufreq_policy *policy)
945{
946 if (hwp_active)
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100947 intel_pstate_hwp_set(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +0200948
949 return 0;
950}
951
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800952static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
953{
954 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
955
956 if (!hwp_active)
957 return 0;
958
959 cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
960
961 return 0;
962}
963
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800964static int intel_pstate_resume(struct cpufreq_policy *policy)
965{
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100966 int ret;
967
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800968 if (!hwp_active)
969 return 0;
970
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100971 mutex_lock(&intel_pstate_limits_lock);
972
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800973 all_cpu_data[policy->cpu]->epp_policy = 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800974
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100975 ret = intel_pstate_hwp_set_policy(policy);
976
977 mutex_unlock(&intel_pstate_limits_lock);
978
979 return ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800980}
981
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100982static void intel_pstate_update_policies(void)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530983{
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100984 int cpu;
985
986 for_each_possible_cpu(cpu)
987 cpufreq_update_policy(cpu);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800988}
989
Dirk Brandewie93f08222013-02-06 09:02:13 -0800990/************************** debugfs begin ************************/
991static int pid_param_set(void *data, u64 val)
992{
993 *(u32 *)data = val;
994 intel_pstate_reset_all_pid();
995 return 0;
996}
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700997
Dirk Brandewie93f08222013-02-06 09:02:13 -0800998static int pid_param_get(void *data, u64 *val)
999{
1000 *val = *(u32 *)data;
1001 return 0;
1002}
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -07001003DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08001004
1005struct pid_param {
1006 char *name;
1007 void *value;
1008};
1009
1010static struct pid_param pid_files[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -07001011 {"sample_rate_ms", &pid_params.sample_rate_ms},
1012 {"d_gain_pct", &pid_params.d_gain_pct},
1013 {"i_gain_pct", &pid_params.i_gain_pct},
1014 {"deadband", &pid_params.deadband},
1015 {"setpoint", &pid_params.setpoint},
1016 {"p_gain_pct", &pid_params.p_gain_pct},
Dirk Brandewie93f08222013-02-06 09:02:13 -08001017 {NULL, NULL}
1018};
1019
Stratos Karafotis317dd502014-07-18 08:37:17 -07001020static void __init intel_pstate_debug_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001021{
Stratos Karafotis317dd502014-07-18 08:37:17 -07001022 struct dentry *debugfs_parent;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001023 int i = 0;
1024
1025 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
1026 if (IS_ERR_OR_NULL(debugfs_parent))
1027 return;
1028 while (pid_files[i].name) {
1029 debugfs_create_file(pid_files[i].name, 0660,
Stratos Karafotisc4108332014-07-18 08:37:23 -07001030 debugfs_parent, pid_files[i].value,
1031 &fops_pid_param);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001032 i++;
1033 }
1034}
1035
1036/************************** debugfs end ************************/
1037
1038/************************** sysfs begin ************************/
1039#define show_one(file_name, object) \
1040 static ssize_t show_##file_name \
1041 (struct kobject *kobj, struct attribute *attr, char *buf) \
1042 { \
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001043 return sprintf(buf, "%u\n", limits->object); \
Dirk Brandewie93f08222013-02-06 09:02:13 -08001044 }
1045
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001046static ssize_t show_turbo_pct(struct kobject *kobj,
1047 struct attribute *attr, char *buf)
1048{
1049 struct cpudata *cpu;
1050 int total, no_turbo, turbo_pct;
1051 uint32_t turbo_fp;
1052
1053 cpu = all_cpu_data[0];
1054
1055 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1056 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001057 turbo_fp = div_fp(no_turbo, total);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001058 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
1059 return sprintf(buf, "%u\n", turbo_pct);
1060}
1061
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001062static ssize_t show_num_pstates(struct kobject *kobj,
1063 struct attribute *attr, char *buf)
1064{
1065 struct cpudata *cpu;
1066 int total;
1067
1068 cpu = all_cpu_data[0];
1069 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1070 return sprintf(buf, "%u\n", total);
1071}
1072
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001073static ssize_t show_no_turbo(struct kobject *kobj,
1074 struct attribute *attr, char *buf)
1075{
1076 ssize_t ret;
1077
1078 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001079 if (limits->turbo_disabled)
1080 ret = sprintf(buf, "%u\n", limits->turbo_disabled);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001081 else
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001082 ret = sprintf(buf, "%u\n", limits->no_turbo);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001083
1084 return ret;
1085}
1086
Dirk Brandewie93f08222013-02-06 09:02:13 -08001087static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -07001088 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001089{
1090 unsigned int input;
1091 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001092
Dirk Brandewie93f08222013-02-06 09:02:13 -08001093 ret = sscanf(buf, "%u", &input);
1094 if (ret != 1)
1095 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001096
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001097 mutex_lock(&intel_pstate_limits_lock);
1098
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001099 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001100 if (limits->turbo_disabled) {
Joe Perches4836df12016-04-05 13:28:23 -07001101 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001102 mutex_unlock(&intel_pstate_limits_lock);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001103 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -07001104 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001105
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001106 limits->no_turbo = clamp_t(int, input, 0, 1);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001107
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08001108 mutex_unlock(&intel_pstate_limits_lock);
1109
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +01001110 intel_pstate_update_policies();
1111
Dirk Brandewie93f08222013-02-06 09:02:13 -08001112 return count;
1113}
1114
1115static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -07001116 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001117{
1118 unsigned int input;
1119 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001120
Dirk Brandewie93f08222013-02-06 09:02:13 -08001121 ret = sscanf(buf, "%u", &input);
1122 if (ret != 1)
1123 return -EINVAL;
1124
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001125 mutex_lock(&intel_pstate_limits_lock);
1126
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001127 limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
1128 limits->max_perf_pct = min(limits->max_policy_pct,
1129 limits->max_sysfs_pct);
1130 limits->max_perf_pct = max(limits->min_policy_pct,
1131 limits->max_perf_pct);
1132 limits->max_perf_pct = max(limits->min_perf_pct,
1133 limits->max_perf_pct);
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001134 limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001135
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08001136 mutex_unlock(&intel_pstate_limits_lock);
1137
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +01001138 intel_pstate_update_policies();
1139
Dirk Brandewie93f08222013-02-06 09:02:13 -08001140 return count;
1141}
1142
1143static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -07001144 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001145{
1146 unsigned int input;
1147 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001148
Dirk Brandewie93f08222013-02-06 09:02:13 -08001149 ret = sscanf(buf, "%u", &input);
1150 if (ret != 1)
1151 return -EINVAL;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -08001152
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001153 mutex_lock(&intel_pstate_limits_lock);
1154
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001155 limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
1156 limits->min_perf_pct = max(limits->min_policy_pct,
1157 limits->min_sysfs_pct);
1158 limits->min_perf_pct = min(limits->max_policy_pct,
1159 limits->min_perf_pct);
1160 limits->min_perf_pct = min(limits->max_perf_pct,
1161 limits->min_perf_pct);
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001162 limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001163
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08001164 mutex_unlock(&intel_pstate_limits_lock);
1165
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +01001166 intel_pstate_update_policies();
1167
Dirk Brandewie93f08222013-02-06 09:02:13 -08001168 return count;
1169}
1170
Dirk Brandewie93f08222013-02-06 09:02:13 -08001171show_one(max_perf_pct, max_perf_pct);
1172show_one(min_perf_pct, min_perf_pct);
1173
1174define_one_global_rw(no_turbo);
1175define_one_global_rw(max_perf_pct);
1176define_one_global_rw(min_perf_pct);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001177define_one_global_ro(turbo_pct);
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001178define_one_global_ro(num_pstates);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001179
1180static struct attribute *intel_pstate_attributes[] = {
1181 &no_turbo.attr,
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001182 &turbo_pct.attr,
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001183 &num_pstates.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001184 NULL
1185};
1186
1187static struct attribute_group intel_pstate_attr_group = {
1188 .attrs = intel_pstate_attributes,
1189};
Dirk Brandewie93f08222013-02-06 09:02:13 -08001190
Stratos Karafotis317dd502014-07-18 08:37:17 -07001191static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001192{
Stratos Karafotis317dd502014-07-18 08:37:17 -07001193 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001194 int rc;
1195
1196 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
1197 &cpu_subsys.dev_root->kobj);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001198 if (WARN_ON(!intel_pstate_kobject))
1199 return;
1200
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -07001201 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001202 if (WARN_ON(rc))
1203 return;
1204
1205 /*
1206 * If per cpu limits are enforced there are no global limits, so
1207 * return without creating max/min_perf_pct attributes
1208 */
1209 if (per_cpu_limits)
1210 return;
1211
1212 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1213 WARN_ON(rc);
1214
1215 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1216 WARN_ON(rc);
1217
Dirk Brandewie93f08222013-02-06 09:02:13 -08001218}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001219/************************** sysfs end ************************/
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001220
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001221static void intel_pstate_hwp_enable(struct cpudata *cpudata)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001222{
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001223 /* First disable HWP notification interrupt as we don't process them */
Srinivas Pandruvadada7de912016-07-19 16:52:01 -07001224 if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
1225 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001226
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001227 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
Srinivas Pandruvada84428852016-11-24 16:07:10 -08001228 cpudata->epp_policy = 0;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001229 if (cpudata->epp_default == -EINVAL)
1230 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001231}
1232
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001233#define MSR_IA32_POWER_CTL_BIT_EE 19
1234
1235/* Disable energy efficiency optimization */
1236static void intel_pstate_disable_ee(int cpu)
1237{
1238 u64 power_ctl;
1239 int ret;
1240
1241 ret = rdmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, &power_ctl);
1242 if (ret)
1243 return;
1244
1245 if (!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE))) {
1246 pr_info("Disabling energy efficiency optimization\n");
1247 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
1248 wrmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, power_ctl);
1249 }
1250}
1251
Philippe Longepe938d21a2015-11-09 17:40:46 -08001252static int atom_get_min_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001253{
1254 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001255
Len Brown92134bd2017-02-25 16:55:17 -05001256 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001257 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001258}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001259
Philippe Longepe938d21a2015-11-09 17:40:46 -08001260static int atom_get_max_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001261{
1262 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001263
Len Brown92134bd2017-02-25 16:55:17 -05001264 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001265 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001266}
1267
Philippe Longepe938d21a2015-11-09 17:40:46 -08001268static int atom_get_turbo_pstate(void)
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001269{
1270 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001271
Len Brown92134bd2017-02-25 16:55:17 -05001272 rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001273 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001274}
1275
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001276static u64 atom_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001277{
1278 u64 val;
1279 int32_t vid_fp;
1280 u32 vid;
1281
Chen Yu144c8e12015-07-29 23:53:10 +08001282 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001283 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001284 val |= (u64)1 << 32;
1285
1286 vid_fp = cpudata->vid.min + mul_fp(
1287 int_tofp(pstate - cpudata->pstate.min_pstate),
1288 cpudata->vid.ratio);
1289
1290 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
Dirk Brandewied022a652014-10-13 08:37:44 -07001291 vid = ceiling_fp(vid_fp);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001292
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001293 if (pstate > cpudata->pstate.max_pstate)
1294 vid = cpudata->vid.turbo;
1295
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001296 return val | vid;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001297}
1298
Philippe Longepe1421df62015-11-09 17:40:47 -08001299static int silvermont_get_scaling(void)
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001300{
1301 u64 value;
1302 int i;
Philippe Longepe1421df62015-11-09 17:40:47 -08001303 /* Defined in Table 35-6 from SDM (Sept 2015) */
1304 static int silvermont_freq_table[] = {
1305 83300, 100000, 133300, 116700, 80000};
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001306
1307 rdmsrl(MSR_FSB_FREQ, value);
Philippe Longepe1421df62015-11-09 17:40:47 -08001308 i = value & 0x7;
1309 WARN_ON(i > 4);
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001310
Philippe Longepe1421df62015-11-09 17:40:47 -08001311 return silvermont_freq_table[i];
1312}
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001313
Philippe Longepe1421df62015-11-09 17:40:47 -08001314static int airmont_get_scaling(void)
1315{
1316 u64 value;
1317 int i;
1318 /* Defined in Table 35-10 from SDM (Sept 2015) */
1319 static int airmont_freq_table[] = {
1320 83300, 100000, 133300, 116700, 80000,
1321 93300, 90000, 88900, 87500};
1322
1323 rdmsrl(MSR_FSB_FREQ, value);
1324 i = value & 0xF;
1325 WARN_ON(i > 8);
1326
1327 return airmont_freq_table[i];
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001328}
1329
Philippe Longepe938d21a2015-11-09 17:40:46 -08001330static void atom_get_vid(struct cpudata *cpudata)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001331{
1332 u64 value;
1333
Len Brown92134bd2017-02-25 16:55:17 -05001334 rdmsrl(MSR_ATOM_CORE_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001335 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1336 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001337 cpudata->vid.ratio = div_fp(
1338 cpudata->vid.max - cpudata->vid.min,
1339 int_tofp(cpudata->pstate.max_pstate -
1340 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001341
Len Brown92134bd2017-02-25 16:55:17 -05001342 rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001343 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001344}
1345
Dirk Brandewie016c8152013-10-21 09:20:34 -07001346static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001347{
1348 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001349
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001350 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001351 return (value >> 40) & 0xFF;
1352}
1353
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001354static int core_get_max_pstate_physical(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001355{
1356 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001357
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001358 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001359 return (value >> 8) & 0xFF;
1360}
1361
Dirk Brandewie93f08222013-02-06 09:02:13 -08001362static int core_get_max_pstate(void)
1363{
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001364 u64 tar;
1365 u64 plat_info;
1366 int max_pstate;
1367 int err;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001368
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001369 rdmsrl(MSR_PLATFORM_INFO, plat_info);
1370 max_pstate = (plat_info >> 8) & 0xFF;
1371
1372 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1373 if (!err) {
1374 /* Do some sanity checking for safety */
1375 if (plat_info & 0x600000000) {
1376 u64 tdp_ctrl;
1377 u64 tdp_ratio;
1378 int tdp_msr;
1379
1380 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1381 if (err)
1382 goto skip_tar;
1383
Jan Kiszka5fc8f7072016-07-08 20:42:04 +02001384 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x3);
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001385 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1386 if (err)
1387 goto skip_tar;
1388
Srinivas Pandruvada1becf032016-04-22 19:53:59 -07001389 /* For level 1 and 2, bits[23:16] contain the ratio */
1390 if (tdp_ctrl)
1391 tdp_ratio >>= 16;
1392
1393 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001394 if (tdp_ratio - 1 == tar) {
1395 max_pstate = tar;
1396 pr_debug("max_pstate=TAC %x\n", max_pstate);
1397 } else {
1398 goto skip_tar;
1399 }
1400 }
1401 }
1402
1403skip_tar:
1404 return max_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001405}
1406
Dirk Brandewie016c8152013-10-21 09:20:34 -07001407static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001408{
1409 u64 value;
1410 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001411
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001412 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001413 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -07001414 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001415 if (ret <= nont)
1416 ret = nont;
1417 return ret;
1418}
1419
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001420static inline int core_get_scaling(void)
1421{
1422 return 100000;
1423}
1424
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001425static u64 core_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001426{
1427 u64 val;
1428
Chen Yu144c8e12015-07-29 23:53:10 +08001429 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001430 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001431 val |= (u64)1 << 32;
1432
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001433 return val;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001434}
1435
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001436static int knl_get_turbo_pstate(void)
1437{
1438 u64 value;
1439 int nont, ret;
1440
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001441 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001442 nont = core_get_max_pstate();
1443 ret = (((value) >> 8) & 0xFF);
1444 if (ret <= nont)
1445 ret = nont;
1446 return ret;
1447}
1448
Dirk Brandewie016c8152013-10-21 09:20:34 -07001449static struct cpu_defaults core_params = {
1450 .pid_policy = {
1451 .sample_rate_ms = 10,
1452 .deadband = 0,
1453 .setpoint = 97,
1454 .p_gain_pct = 20,
1455 .d_gain_pct = 0,
1456 .i_gain_pct = 0,
1457 },
1458 .funcs = {
1459 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001460 .get_max_physical = core_get_max_pstate_physical,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001461 .get_min = core_get_min_pstate,
1462 .get_turbo = core_get_turbo_pstate,
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001463 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001464 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001465 .get_target_pstate = get_target_pstate_use_performance,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001466 },
1467};
1468
Julia Lawall42ce8922016-09-11 15:06:01 +02001469static const struct cpu_defaults silvermont_params = {
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001470 .pid_policy = {
1471 .sample_rate_ms = 10,
1472 .deadband = 0,
Kristen Carlson Accardi6a82ba62015-04-10 11:06:43 -07001473 .setpoint = 60,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001474 .p_gain_pct = 14,
1475 .d_gain_pct = 0,
1476 .i_gain_pct = 4,
1477 },
1478 .funcs = {
Philippe Longepe938d21a2015-11-09 17:40:46 -08001479 .get_max = atom_get_max_pstate,
1480 .get_max_physical = atom_get_max_pstate,
1481 .get_min = atom_get_min_pstate,
1482 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001483 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001484 .get_scaling = silvermont_get_scaling,
1485 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001486 .get_target_pstate = get_target_pstate_use_cpu_load,
Philippe Longepe1421df62015-11-09 17:40:47 -08001487 },
1488};
1489
Julia Lawall42ce8922016-09-11 15:06:01 +02001490static const struct cpu_defaults airmont_params = {
Philippe Longepe1421df62015-11-09 17:40:47 -08001491 .pid_policy = {
1492 .sample_rate_ms = 10,
1493 .deadband = 0,
1494 .setpoint = 60,
1495 .p_gain_pct = 14,
1496 .d_gain_pct = 0,
1497 .i_gain_pct = 4,
1498 },
1499 .funcs = {
1500 .get_max = atom_get_max_pstate,
1501 .get_max_physical = atom_get_max_pstate,
1502 .get_min = atom_get_min_pstate,
1503 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001504 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001505 .get_scaling = airmont_get_scaling,
Philippe Longepe938d21a2015-11-09 17:40:46 -08001506 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001507 .get_target_pstate = get_target_pstate_use_cpu_load,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001508 },
1509};
1510
Julia Lawall42ce8922016-09-11 15:06:01 +02001511static const struct cpu_defaults knl_params = {
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001512 .pid_policy = {
1513 .sample_rate_ms = 10,
1514 .deadband = 0,
1515 .setpoint = 97,
1516 .p_gain_pct = 20,
1517 .d_gain_pct = 0,
1518 .i_gain_pct = 0,
1519 },
1520 .funcs = {
1521 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001522 .get_max_physical = core_get_max_pstate_physical,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001523 .get_min = core_get_min_pstate,
1524 .get_turbo = knl_get_turbo_pstate,
Lukasz Anaczkowski69cefc22015-07-21 10:41:13 +02001525 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001526 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001527 .get_target_pstate = get_target_pstate_use_performance,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001528 },
1529};
1530
Julia Lawall42ce8922016-09-11 15:06:01 +02001531static const struct cpu_defaults bxt_params = {
Srinivas Pandruvada41bad472016-06-09 15:34:41 -07001532 .pid_policy = {
1533 .sample_rate_ms = 10,
1534 .deadband = 0,
1535 .setpoint = 60,
1536 .p_gain_pct = 14,
1537 .d_gain_pct = 0,
1538 .i_gain_pct = 4,
1539 },
1540 .funcs = {
1541 .get_max = core_get_max_pstate,
1542 .get_max_physical = core_get_max_pstate_physical,
1543 .get_min = core_get_min_pstate,
1544 .get_turbo = core_get_turbo_pstate,
1545 .get_scaling = core_get_scaling,
1546 .get_val = core_get_val,
1547 .get_target_pstate = get_target_pstate_use_cpu_load,
1548 },
1549};
1550
Dirk Brandewie93f08222013-02-06 09:02:13 -08001551static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
1552{
1553 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie7244cb62013-10-21 09:20:33 -07001554 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001555 int min_perf;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001556 struct perf_limits *perf_limits = limits;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001557
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001558 if (limits->no_turbo || limits->turbo_disabled)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001559 max_perf = cpu->pstate.max_pstate;
1560
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001561 if (per_cpu_limits)
1562 perf_limits = cpu->perf_limits;
1563
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001564 /*
1565 * performance can be limited by user through sysfs, by cpufreq
1566 * policy, or by cpu specific default values determined through
1567 * experimentation.
1568 */
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001569 max_perf_adj = fp_ext_toint(max_perf * perf_limits->max_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001570 *max = clamp_t(int, max_perf_adj,
1571 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001572
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001573 min_perf = fp_ext_toint(max_perf * perf_limits->min_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001574 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001575}
1576
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001577static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001578{
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001579 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1580 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001581 /*
1582 * Generally, there is no guarantee that this code will always run on
1583 * the CPU being updated, so force the register update to run on the
1584 * right CPU.
1585 */
1586 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1587 pstate_funcs.get_val(cpu, pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001588}
1589
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001590static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1591{
1592 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1593}
1594
1595static void intel_pstate_max_within_limits(struct cpudata *cpu)
1596{
1597 int min_pstate, max_pstate;
1598
1599 update_turbo_state();
1600 intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate);
1601 intel_pstate_set_pstate(cpu, max_pstate);
1602}
1603
Dirk Brandewie93f08222013-02-06 09:02:13 -08001604static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1605{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001606 cpu->pstate.min_pstate = pstate_funcs.get_min();
1607 cpu->pstate.max_pstate = pstate_funcs.get_max();
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001608 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
Dirk Brandewie016c8152013-10-21 09:20:34 -07001609 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001610 cpu->pstate.scaling = pstate_funcs.get_scaling();
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001611 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1612 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001613
Dirk Brandewie007bea02013-12-18 10:32:39 -08001614 if (pstate_funcs.get_vid)
1615 pstate_funcs.get_vid(cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001616
1617 intel_pstate_set_min_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001618}
1619
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001620static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001621{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +03001622 struct sample *sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001623
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001624 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001625}
1626
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001627static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001628{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001629 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001630 unsigned long flags;
Doug Smythies4055fad2015-04-11 21:10:26 -07001631 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001632
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001633 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001634 rdmsrl(MSR_IA32_APERF, aperf);
1635 rdmsrl(MSR_IA32_MPERF, mperf);
Philippe Longepee70eed22015-12-04 17:40:32 +01001636 tsc = rdtsc();
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001637 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001638 local_irq_restore(flags);
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001639 return false;
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001640 }
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001641 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001642
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001643 cpu->last_sample_time = cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001644 cpu->sample.time = time;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001645 cpu->sample.aperf = aperf;
1646 cpu->sample.mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001647 cpu->sample.tsc = tsc;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001648 cpu->sample.aperf -= cpu->prev_aperf;
1649 cpu->sample.mperf -= cpu->prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001650 cpu->sample.tsc -= cpu->prev_tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001651
Dirk Brandewie93f08222013-02-06 09:02:13 -08001652 cpu->prev_aperf = aperf;
1653 cpu->prev_mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001654 cpu->prev_tsc = tsc;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001655 /*
1656 * First time this function is invoked in a given cycle, all of the
1657 * previous sample data fields are equal to zero or stale and they must
1658 * be populated with meaningful numbers for things to work, so assume
1659 * that sample.time will always be reset before setting the utilization
1660 * update hook and make the caller skip the sample then.
1661 */
1662 return !!cpu->last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001663}
1664
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001665static inline int32_t get_avg_frequency(struct cpudata *cpu)
1666{
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001667 return mul_ext_fp(cpu->sample.core_avg_perf,
1668 cpu->pstate.max_pstate_physical * cpu->pstate.scaling);
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001669}
1670
Philippe Longepebdcaa232016-04-22 11:46:09 -07001671static inline int32_t get_avg_pstate(struct cpudata *cpu)
1672{
Rafael J. Wysocki8edb0a62016-05-11 19:10:42 +02001673 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1674 cpu->sample.core_avg_perf);
Philippe Longepebdcaa232016-04-22 11:46:09 -07001675}
1676
Philippe Longepee70eed22015-12-04 17:40:32 +01001677static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
1678{
1679 struct sample *sample = &cpu->sample;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001680 int32_t busy_frac, boost;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001681 int target, avg_pstate;
Philippe Longepee70eed22015-12-04 17:40:32 +01001682
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001683 busy_frac = div_fp(sample->mperf, sample->tsc);
Philippe Longepe63d1d652015-12-04 17:40:35 +01001684
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001685 boost = cpu->iowait_boost;
1686 cpu->iowait_boost >>= 1;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001687
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001688 if (busy_frac < boost)
1689 busy_frac = boost;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001690
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001691 sample->busy_scaled = busy_frac * 100;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001692
1693 target = limits->no_turbo || limits->turbo_disabled ?
1694 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1695 target += target >> 2;
1696 target = mul_fp(target, busy_frac);
1697 if (target < cpu->pstate.min_pstate)
1698 target = cpu->pstate.min_pstate;
1699
1700 /*
1701 * If the average P-state during the previous cycle was higher than the
1702 * current target, add 50% of the difference to the target to reduce
1703 * possible performance oscillations and offset possible performance
1704 * loss related to moving the workload from one CPU to another within
1705 * a package/module.
1706 */
1707 avg_pstate = get_avg_pstate(cpu);
1708 if (avg_pstate > target)
1709 target += (avg_pstate - target) >> 1;
1710
1711 return target;
Philippe Longepee70eed22015-12-04 17:40:32 +01001712}
1713
Philippe Longepe157386b2015-12-04 17:40:30 +01001714static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001715{
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001716 int32_t perf_scaled, max_pstate, current_pstate, sample_ratio;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001717 u64 duration_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001718
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001719 /*
Rafael J. Wysockif00593a2016-09-30 03:13:34 +02001720 * perf_scaled is the ratio of the average P-state during the last
1721 * sampling period to the P-state requested last time (in percent).
1722 *
1723 * That measures the system's response to the previous P-state
1724 * selection.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001725 */
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001726 max_pstate = cpu->pstate.max_pstate_physical;
1727 current_pstate = cpu->pstate.current_pstate;
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001728 perf_scaled = mul_ext_fp(cpu->sample.core_avg_perf,
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001729 div_fp(100 * max_pstate, current_pstate));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001730
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001731 /*
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001732 * Since our utilization update callback will not run unless we are
1733 * in C0, check if the actual elapsed time is significantly greater (3x)
1734 * than our sample interval. If it is, then we were idle for a long
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001735 * enough period of time to adjust our performance metric.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001736 */
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001737 duration_ns = cpu->sample.time - cpu->last_sample_time;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001738 if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001739 sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns);
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001740 perf_scaled = mul_fp(perf_scaled, sample_ratio);
Rafael J. Wysockiffb81052016-04-10 05:59:10 +02001741 } else {
1742 sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc);
1743 if (sample_ratio < int_tofp(1))
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001744 perf_scaled = 0;
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001745 }
1746
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001747 cpu->sample.busy_scaled = perf_scaled;
1748 return cpu->pstate.current_pstate - pid_calc(&cpu->pid, perf_scaled);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001749}
1750
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001751static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001752{
1753 int max_perf, min_perf;
1754
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001755 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
1756 pstate = clamp_t(int, pstate, min_perf, max_perf);
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001757 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001758 return pstate;
1759}
1760
1761static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1762{
1763 pstate = intel_pstate_prepare_request(cpu, pstate);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001764 if (pstate == cpu->pstate.current_pstate)
1765 return;
1766
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001767 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001768 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1769}
1770
Dirk Brandewie93f08222013-02-06 09:02:13 -08001771static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
1772{
Philippe Longepe157386b2015-12-04 17:40:30 +01001773 int from, target_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001774 struct sample *sample;
1775
1776 from = cpu->pstate.current_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001777
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02001778 target_pstate = cpu->policy == CPUFREQ_POLICY_PERFORMANCE ?
1779 cpu->pstate.turbo_pstate : pstate_funcs.get_target_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001780
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001781 update_turbo_state();
1782
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001783 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies4055fad2015-04-11 21:10:26 -07001784
1785 sample = &cpu->sample;
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001786 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
Philippe Longepe157386b2015-12-04 17:40:30 +01001787 fp_toint(sample->busy_scaled),
Doug Smythies4055fad2015-04-11 21:10:26 -07001788 from,
1789 cpu->pstate.current_pstate,
1790 sample->mperf,
1791 sample->aperf,
1792 sample->tsc,
Srinivas Pandruvada3ba7bca2016-09-13 17:41:33 -07001793 get_avg_frequency(cpu),
1794 fp_toint(cpu->iowait_boost * 100));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001795}
1796
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001797static void intel_pstate_update_util(struct update_util_data *data, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02001798 unsigned int flags)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001799{
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001800 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001801 u64 delta_ns;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001802
Rafael J. Wysocki1d298152016-10-19 02:53:26 +02001803 if (pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load) {
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001804 if (flags & SCHED_CPUFREQ_IOWAIT) {
1805 cpu->iowait_boost = int_tofp(1);
1806 } else if (cpu->iowait_boost) {
1807 /* Clear iowait_boost if the CPU may have been idle. */
1808 delta_ns = time - cpu->last_update;
1809 if (delta_ns > TICK_NSEC)
1810 cpu->iowait_boost = 0;
1811 }
1812 cpu->last_update = time;
1813 }
1814
1815 delta_ns = time - cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001816 if ((s64)delta_ns >= pid_params.sample_rate_ns) {
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001817 bool sample_taken = intel_pstate_sample(cpu, time);
1818
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001819 if (sample_taken) {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001820 intel_pstate_calc_avg_perf(cpu);
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001821 if (!hwp_active)
1822 intel_pstate_adjust_busy_pstate(cpu);
1823 }
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001824 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001825}
1826
1827#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -08001828 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1829 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001830
1831static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dave Hansen5b20c942016-06-02 17:19:45 -07001832 ICPU(INTEL_FAM6_SANDYBRIDGE, core_params),
1833 ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_params),
1834 ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_params),
1835 ICPU(INTEL_FAM6_IVYBRIDGE, core_params),
1836 ICPU(INTEL_FAM6_HASWELL_CORE, core_params),
1837 ICPU(INTEL_FAM6_BROADWELL_CORE, core_params),
1838 ICPU(INTEL_FAM6_IVYBRIDGE_X, core_params),
1839 ICPU(INTEL_FAM6_HASWELL_X, core_params),
1840 ICPU(INTEL_FAM6_HASWELL_ULT, core_params),
1841 ICPU(INTEL_FAM6_HASWELL_GT3E, core_params),
1842 ICPU(INTEL_FAM6_BROADWELL_GT3E, core_params),
1843 ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_params),
1844 ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_params),
1845 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1846 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_params),
1847 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
1848 ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_params),
Piotr Luc58bf4542016-10-13 17:31:00 +02001849 ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_params),
Srinivas Pandruvada41bad472016-06-09 15:34:41 -07001850 ICPU(INTEL_FAM6_ATOM_GOLDMONT, bxt_params),
Dirk Brandewie93f08222013-02-06 09:02:13 -08001851 {}
1852};
1853MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1854
Jisheng Zhang29327c82016-06-27 18:07:17 +08001855static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
Dave Hansen5b20c942016-06-02 17:19:45 -07001856 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
Srinivas Pandruvada65c12622016-07-23 15:12:06 -07001857 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1858 ICPU(INTEL_FAM6_SKYLAKE_X, core_params),
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001859 {}
1860};
1861
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001862static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
1863 ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, core_params),
1864 {}
1865};
1866
Dirk Brandewie93f08222013-02-06 09:02:13 -08001867static int intel_pstate_init_cpu(unsigned int cpunum)
1868{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001869 struct cpudata *cpu;
1870
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001871 cpu = all_cpu_data[cpunum];
1872
1873 if (!cpu) {
1874 unsigned int size = sizeof(struct cpudata);
1875
1876 if (per_cpu_limits)
1877 size += sizeof(struct perf_limits);
1878
1879 cpu = kzalloc(size, GFP_KERNEL);
1880 if (!cpu)
1881 return -ENOMEM;
1882
1883 all_cpu_data[cpunum] = cpu;
1884 if (per_cpu_limits)
1885 cpu->perf_limits = (struct perf_limits *)(cpu + 1);
1886
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001887 cpu->epp_default = -EINVAL;
1888 cpu->epp_powersave = -EINVAL;
1889 cpu->epp_saved = -EINVAL;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001890 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001891
1892 cpu = all_cpu_data[cpunum];
1893
Dirk Brandewie93f08222013-02-06 09:02:13 -08001894 cpu->cpu = cpunum;
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001895
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001896 if (hwp_active) {
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001897 const struct x86_cpu_id *id;
1898
1899 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
1900 if (id)
1901 intel_pstate_disable_ee(cpunum);
1902
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001903 intel_pstate_hwp_enable(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001904 pid_params.sample_rate_ms = 50;
1905 pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
1906 }
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001907
Vincent Minet179e8472014-07-05 01:51:33 +02001908 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001909
Dirk Brandewie93f08222013-02-06 09:02:13 -08001910 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001911
Joe Perches4836df12016-04-05 13:28:23 -07001912 pr_debug("controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001913
1914 return 0;
1915}
1916
1917static unsigned int intel_pstate_get(unsigned int cpu_num)
1918{
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001919 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -08001920
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001921 return cpu ? get_avg_frequency(cpu) : 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001922}
1923
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001924static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001925{
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001926 struct cpudata *cpu = all_cpu_data[cpu_num];
1927
Rafael J. Wysocki5ab666e2016-06-27 23:47:15 +02001928 if (cpu->update_util_set)
1929 return;
1930
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001931 /* Prevent intel_pstate_update_util() from using stale data. */
1932 cpu->sample.time = 0;
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001933 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
1934 intel_pstate_update_util);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001935 cpu->update_util_set = true;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001936}
1937
1938static void intel_pstate_clear_update_util_hook(unsigned int cpu)
1939{
Chen Yu4578ee7e2016-05-11 14:33:08 +08001940 struct cpudata *cpu_data = all_cpu_data[cpu];
1941
1942 if (!cpu_data->update_util_set)
1943 return;
1944
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001945 cpufreq_remove_update_util_hook(cpu);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001946 cpu_data->update_util_set = false;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001947 synchronize_sched();
1948}
1949
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001950static void intel_pstate_set_performance_limits(struct perf_limits *limits)
1951{
1952 limits->no_turbo = 0;
1953 limits->turbo_disabled = 0;
1954 limits->max_perf_pct = 100;
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001955 limits->max_perf = int_ext_tofp(1);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001956 limits->min_perf_pct = 100;
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001957 limits->min_perf = int_ext_tofp(1);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001958 limits->max_policy_pct = 100;
1959 limits->max_sysfs_pct = 100;
1960 limits->min_policy_pct = 0;
1961 limits->min_sysfs_pct = 0;
1962}
1963
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001964static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
1965 struct perf_limits *limits)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001966{
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001967
Prarit Bhargava8478f532015-11-20 18:47:56 -05001968 limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
1969 policy->cpuinfo.max_freq);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001970 limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0, 100);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001971 if (policy->max == policy->min) {
1972 limits->min_policy_pct = limits->max_policy_pct;
1973 } else {
Srinivas Pandruvada46992d62016-11-21 16:33:19 -08001974 limits->min_policy_pct = DIV_ROUND_UP(policy->min * 100,
1975 policy->cpuinfo.max_freq);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001976 limits->min_policy_pct = clamp_t(int, limits->min_policy_pct,
1977 0, 100);
1978 }
Chen Yu43717aa2015-09-09 18:27:31 +08001979
1980 /* Normalize user input to [min_policy_pct, max_policy_pct] */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001981 limits->min_perf_pct = max(limits->min_policy_pct,
1982 limits->min_sysfs_pct);
1983 limits->min_perf_pct = min(limits->max_policy_pct,
1984 limits->min_perf_pct);
1985 limits->max_perf_pct = min(limits->max_policy_pct,
1986 limits->max_sysfs_pct);
1987 limits->max_perf_pct = max(limits->min_policy_pct,
1988 limits->max_perf_pct);
Chen Yu43717aa2015-09-09 18:27:31 +08001989
1990 /* Make sure min_perf_pct <= max_perf_pct */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001991 limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
Chen Yu43717aa2015-09-09 18:27:31 +08001992
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001993 limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
1994 limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
1995 limits->max_perf = round_up(limits->max_perf, EXT_FRAC_BITS);
1996 limits->min_perf = round_up(limits->min_perf, EXT_FRAC_BITS);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001997
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001998 pr_debug("cpu:%d max_perf_pct:%d min_perf_pct:%d\n", policy->cpu,
1999 limits->max_perf_pct, limits->min_perf_pct);
2000}
2001
Dirk Brandewie93f08222013-02-06 09:02:13 -08002002static int intel_pstate_set_policy(struct cpufreq_policy *policy)
2003{
2004 struct cpudata *cpu;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002005 struct perf_limits *perf_limits = NULL;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002006
2007 if (!policy->cpuinfo.max_freq)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002008 return -ENODEV;
2009
2010 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
Dirk Brandewie93f08222013-02-06 09:02:13 -08002011 policy->cpuinfo.max_freq, policy->max);
2012
2013 cpu = all_cpu_data[policy->cpu];
2014 cpu->policy = policy->policy;
2015
Viresh Kumarbe49e342013-10-02 14:13:19 +05302016 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
Dirk Brandewie93f08222013-02-06 09:02:13 -08002017 policy->max < policy->cpuinfo.max_freq &&
Stratos Karafotis285cb992014-07-18 08:37:21 -07002018 policy->max > cpu->pstate.max_pstate * cpu->pstate.scaling) {
Stratos Karafotisc4108332014-07-18 08:37:23 -07002019 pr_debug("policy->max > max non turbo frequency\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08002020 policy->max = policy->cpuinfo.max_freq;
2021 }
2022
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002023 if (per_cpu_limits)
2024 perf_limits = cpu->perf_limits;
2025
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08002026 mutex_lock(&intel_pstate_limits_lock);
2027
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002028 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
2029 if (!perf_limits) {
2030 limits = &performance_limits;
2031 perf_limits = limits;
2032 }
Srinivas Pandruvada1443ebb2017-01-18 10:48:22 -08002033 if (policy->max >= policy->cpuinfo.max_freq &&
2034 !limits->no_turbo) {
Dirk Brandewie93f08222013-02-06 09:02:13 -08002035 pr_debug("set performance\n");
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002036 intel_pstate_set_performance_limits(perf_limits);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002037 goto out;
2038 }
2039 } else {
2040 pr_debug("set powersave\n");
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002041 if (!perf_limits) {
2042 limits = &powersave_limits;
2043 perf_limits = limits;
2044 }
2045
Dirk Brandewie93f08222013-02-06 09:02:13 -08002046 }
2047
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002048 intel_pstate_update_perf_limits(policy, perf_limits);
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02002049 out:
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02002050 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02002051 /*
2052 * NOHZ_FULL CPUs need this as the governor callback may not
2053 * be invoked on them.
2054 */
2055 intel_pstate_clear_update_util_hook(policy->cpu);
2056 intel_pstate_max_within_limits(cpu);
2057 }
2058
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02002059 intel_pstate_set_update_util_hook(policy->cpu);
2060
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02002061 intel_pstate_hwp_set_policy(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002062
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08002063 mutex_unlock(&intel_pstate_limits_lock);
2064
Dirk Brandewie93f08222013-02-06 09:02:13 -08002065 return 0;
2066}
2067
2068static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
2069{
2070 cpufreq_verify_within_cpu_limits(policy);
2071
2072 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
2073 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
2074 return -EINVAL;
2075
Srinivas Pandruvada1443ebb2017-01-18 10:48:22 -08002076 /* When per-CPU limits are used, sysfs limits are not used */
2077 if (!per_cpu_limits) {
2078 unsigned int max_freq, min_freq;
2079
2080 max_freq = policy->cpuinfo.max_freq *
2081 limits->max_sysfs_pct / 100;
2082 min_freq = policy->cpuinfo.max_freq *
2083 limits->min_sysfs_pct / 100;
2084 cpufreq_verify_within_limits(policy, min_freq, max_freq);
2085 }
2086
Dirk Brandewie93f08222013-02-06 09:02:13 -08002087 return 0;
2088}
2089
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002090static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08002091{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002092 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002093}
2094
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002095static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
2096{
2097 pr_debug("CPU %d exiting\n", policy->cpu);
2098
2099 intel_pstate_clear_update_util_hook(policy->cpu);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08002100 if (hwp_active)
2101 intel_pstate_hwp_save_state(policy);
2102 else
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002103 intel_cpufreq_stop_cpu(policy);
2104}
2105
2106static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
2107{
2108 intel_pstate_exit_perf_limits(policy);
2109
2110 policy->fast_switch_possible = false;
2111
2112 return 0;
2113}
2114
2115static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08002116{
Dirk Brandewie93f08222013-02-06 09:02:13 -08002117 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -07002118 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002119
2120 rc = intel_pstate_init_cpu(policy->cpu);
2121 if (rc)
2122 return rc;
2123
2124 cpu = all_cpu_data[policy->cpu];
2125
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002126 /*
2127 * We need sane value in the cpu->perf_limits, so inherit from global
2128 * perf_limits limits, which are seeded with values based on the
2129 * CONFIG_CPU_FREQ_DEFAULT_GOV_*, during boot up.
2130 */
2131 if (per_cpu_limits)
2132 memcpy(cpu->perf_limits, limits, sizeof(struct perf_limits));
Dirk Brandewie93f08222013-02-06 09:02:13 -08002133
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002134 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
2135 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002136
2137 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002138 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07002139 update_turbo_state();
2140 policy->cpuinfo.max_freq = limits->turbo_disabled ?
2141 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2142 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
2143
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002144 intel_pstate_init_acpi_perf_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002145 cpumask_set_cpu(policy->cpu, policy->cpus);
2146
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002147 policy->fast_switch_possible = true;
2148
Dirk Brandewie93f08222013-02-06 09:02:13 -08002149 return 0;
2150}
2151
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002152static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002153{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002154 int ret = __intel_pstate_cpu_init(policy);
2155
2156 if (ret)
2157 return ret;
2158
2159 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
2160 if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
2161 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
2162 else
2163 policy->policy = CPUFREQ_POLICY_POWERSAVE;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002164
2165 return 0;
2166}
2167
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002168static struct cpufreq_driver intel_pstate = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08002169 .flags = CPUFREQ_CONST_LOOPS,
2170 .verify = intel_pstate_verify_policy,
2171 .setpolicy = intel_pstate_set_policy,
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08002172 .suspend = intel_pstate_hwp_save_state,
Srinivas Pandruvada84428852016-11-24 16:07:10 -08002173 .resume = intel_pstate_resume,
Dirk Brandewie93f08222013-02-06 09:02:13 -08002174 .get = intel_pstate_get,
2175 .init = intel_pstate_cpu_init,
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002176 .exit = intel_pstate_cpu_exit,
Dirk Brandewiebb180082014-03-19 08:45:54 -07002177 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -08002178 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -08002179};
2180
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002181static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
2182{
2183 struct cpudata *cpu = all_cpu_data[policy->cpu];
2184 struct perf_limits *perf_limits = limits;
2185
2186 update_turbo_state();
2187 policy->cpuinfo.max_freq = limits->turbo_disabled ?
2188 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2189
2190 cpufreq_verify_within_cpu_limits(policy);
2191
2192 if (per_cpu_limits)
2193 perf_limits = cpu->perf_limits;
2194
Rafael J. Wysockicad30462016-12-30 15:57:11 +01002195 mutex_lock(&intel_pstate_limits_lock);
2196
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002197 intel_pstate_update_perf_limits(policy, perf_limits);
2198
Rafael J. Wysockicad30462016-12-30 15:57:11 +01002199 mutex_unlock(&intel_pstate_limits_lock);
2200
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002201 return 0;
2202}
2203
2204static unsigned int intel_cpufreq_turbo_update(struct cpudata *cpu,
2205 struct cpufreq_policy *policy,
2206 unsigned int target_freq)
2207{
2208 unsigned int max_freq;
2209
2210 update_turbo_state();
2211
2212 max_freq = limits->no_turbo || limits->turbo_disabled ?
2213 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2214 policy->cpuinfo.max_freq = max_freq;
2215 if (policy->max > max_freq)
2216 policy->max = max_freq;
2217
2218 if (target_freq > max_freq)
2219 target_freq = max_freq;
2220
2221 return target_freq;
2222}
2223
2224static int intel_cpufreq_target(struct cpufreq_policy *policy,
2225 unsigned int target_freq,
2226 unsigned int relation)
2227{
2228 struct cpudata *cpu = all_cpu_data[policy->cpu];
2229 struct cpufreq_freqs freqs;
2230 int target_pstate;
2231
2232 freqs.old = policy->cur;
2233 freqs.new = intel_cpufreq_turbo_update(cpu, policy, target_freq);
2234
2235 cpufreq_freq_transition_begin(policy, &freqs);
2236 switch (relation) {
2237 case CPUFREQ_RELATION_L:
2238 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
2239 break;
2240 case CPUFREQ_RELATION_H:
2241 target_pstate = freqs.new / cpu->pstate.scaling;
2242 break;
2243 default:
2244 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
2245 break;
2246 }
2247 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2248 if (target_pstate != cpu->pstate.current_pstate) {
2249 cpu->pstate.current_pstate = target_pstate;
2250 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
2251 pstate_funcs.get_val(cpu, target_pstate));
2252 }
2253 cpufreq_freq_transition_end(policy, &freqs, false);
2254
2255 return 0;
2256}
2257
2258static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
2259 unsigned int target_freq)
2260{
2261 struct cpudata *cpu = all_cpu_data[policy->cpu];
2262 int target_pstate;
2263
2264 target_freq = intel_cpufreq_turbo_update(cpu, policy, target_freq);
2265 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
2266 intel_pstate_update_pstate(cpu, target_pstate);
2267 return target_freq;
2268}
2269
2270static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
2271{
2272 int ret = __intel_pstate_cpu_init(policy);
2273
2274 if (ret)
2275 return ret;
2276
2277 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
2278 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
2279 policy->cur = policy->cpuinfo.min_freq;
2280
2281 return 0;
2282}
2283
2284static struct cpufreq_driver intel_cpufreq = {
2285 .flags = CPUFREQ_CONST_LOOPS,
2286 .verify = intel_cpufreq_verify_policy,
2287 .target = intel_cpufreq_target,
2288 .fast_switch = intel_cpufreq_fast_switch,
2289 .init = intel_cpufreq_cpu_init,
2290 .exit = intel_pstate_cpu_exit,
2291 .stop_cpu = intel_cpufreq_stop_cpu,
2292 .name = "intel_cpufreq",
2293};
2294
2295static struct cpufreq_driver *intel_pstate_driver = &intel_pstate;
2296
Jisheng Zhangeed43602016-06-27 18:07:16 +08002297static int no_load __initdata;
2298static int no_hwp __initdata;
2299static int hwp_only __initdata;
Jisheng Zhang29327c82016-06-27 18:07:17 +08002300static unsigned int force_load __initdata;
Dirk Brandewie6be26492013-02-15 22:55:10 +01002301
Jisheng Zhang29327c82016-06-27 18:07:17 +08002302static int __init intel_pstate_msrs_not_valid(void)
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002303{
Dirk Brandewie016c8152013-10-21 09:20:34 -07002304 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -07002305 !pstate_funcs.get_min() ||
2306 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002307 return -ENODEV;
2308
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002309 return 0;
2310}
Dirk Brandewie016c8152013-10-21 09:20:34 -07002311
Jisheng Zhang29327c82016-06-27 18:07:17 +08002312static void __init copy_pid_params(struct pstate_adjust_policy *policy)
Dirk Brandewie016c8152013-10-21 09:20:34 -07002313{
2314 pid_params.sample_rate_ms = policy->sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01002315 pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002316 pid_params.p_gain_pct = policy->p_gain_pct;
2317 pid_params.i_gain_pct = policy->i_gain_pct;
2318 pid_params.d_gain_pct = policy->d_gain_pct;
2319 pid_params.deadband = policy->deadband;
2320 pid_params.setpoint = policy->setpoint;
2321}
2322
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08002323#ifdef CONFIG_ACPI
2324static void intel_pstate_use_acpi_profile(void)
2325{
2326 if (acpi_gbl_FADT.preferred_profile == PM_MOBILE)
2327 pstate_funcs.get_target_pstate =
2328 get_target_pstate_use_cpu_load;
2329}
2330#else
2331static void intel_pstate_use_acpi_profile(void)
2332{
2333}
2334#endif
2335
Jisheng Zhang29327c82016-06-27 18:07:17 +08002336static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -07002337{
2338 pstate_funcs.get_max = funcs->get_max;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07002339 pstate_funcs.get_max_physical = funcs->get_max_physical;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002340 pstate_funcs.get_min = funcs->get_min;
2341 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002342 pstate_funcs.get_scaling = funcs->get_scaling;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01002343 pstate_funcs.get_val = funcs->get_val;
Dirk Brandewie007bea02013-12-18 10:32:39 -08002344 pstate_funcs.get_vid = funcs->get_vid;
Philippe Longepe157386b2015-12-04 17:40:30 +01002345 pstate_funcs.get_target_pstate = funcs->get_target_pstate;
2346
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08002347 intel_pstate_use_acpi_profile();
Dirk Brandewie016c8152013-10-21 09:20:34 -07002348}
2349
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002350#ifdef CONFIG_ACPI
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002351
Jisheng Zhang29327c82016-06-27 18:07:17 +08002352static bool __init intel_pstate_no_acpi_pss(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002353{
2354 int i;
2355
2356 for_each_possible_cpu(i) {
2357 acpi_status status;
2358 union acpi_object *pss;
2359 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2360 struct acpi_processor *pr = per_cpu(processors, i);
2361
2362 if (!pr)
2363 continue;
2364
2365 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2366 if (ACPI_FAILURE(status))
2367 continue;
2368
2369 pss = buffer.pointer;
2370 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2371 kfree(pss);
2372 return false;
2373 }
2374
2375 kfree(pss);
2376 }
2377
2378 return true;
2379}
2380
Jisheng Zhang29327c82016-06-27 18:07:17 +08002381static bool __init intel_pstate_has_acpi_ppc(void)
ethan zhao966916e2014-12-01 11:32:08 +09002382{
2383 int i;
2384
2385 for_each_possible_cpu(i) {
2386 struct acpi_processor *pr = per_cpu(processors, i);
2387
2388 if (!pr)
2389 continue;
2390 if (acpi_has_method(pr->handle, "_PPC"))
2391 return true;
2392 }
2393 return false;
2394}
2395
2396enum {
2397 PSS,
2398 PPC,
2399};
2400
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002401struct hw_vendor_info {
2402 u16 valid;
2403 char oem_id[ACPI_OEM_ID_SIZE];
2404 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
ethan zhao966916e2014-12-01 11:32:08 +09002405 int oem_pwr_table;
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002406};
2407
2408/* Hardware vendor-specific info that has its own power management modes */
Jisheng Zhang29327c82016-06-27 18:07:17 +08002409static struct hw_vendor_info vendor_info[] __initdata = {
ethan zhao966916e2014-12-01 11:32:08 +09002410 {1, "HP ", "ProLiant", PSS},
2411 {1, "ORACLE", "X4-2 ", PPC},
2412 {1, "ORACLE", "X4-2L ", PPC},
2413 {1, "ORACLE", "X4-2B ", PPC},
2414 {1, "ORACLE", "X3-2 ", PPC},
2415 {1, "ORACLE", "X3-2L ", PPC},
2416 {1, "ORACLE", "X3-2B ", PPC},
2417 {1, "ORACLE", "X4470M2 ", PPC},
2418 {1, "ORACLE", "X4270M3 ", PPC},
2419 {1, "ORACLE", "X4270M2 ", PPC},
2420 {1, "ORACLE", "X4170M2 ", PPC},
Ethan Zhao5aecc3c2015-08-05 09:28:50 +09002421 {1, "ORACLE", "X4170 M3", PPC},
2422 {1, "ORACLE", "X4275 M3", PPC},
2423 {1, "ORACLE", "X6-2 ", PPC},
2424 {1, "ORACLE", "Sudbury ", PPC},
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002425 {0, "", ""},
2426};
2427
Jisheng Zhang29327c82016-06-27 18:07:17 +08002428static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002429{
2430 struct acpi_table_header hdr;
2431 struct hw_vendor_info *v_info;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002432 const struct x86_cpu_id *id;
2433 u64 misc_pwr;
2434
2435 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2436 if (id) {
2437 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2438 if ( misc_pwr & (1 << 8))
2439 return true;
2440 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002441
Stratos Karafotisc4108332014-07-18 08:37:23 -07002442 if (acpi_disabled ||
2443 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002444 return false;
2445
2446 for (v_info = vendor_info; v_info->valid; v_info++) {
Stratos Karafotisc4108332014-07-18 08:37:23 -07002447 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
ethan zhao966916e2014-12-01 11:32:08 +09002448 !strncmp(hdr.oem_table_id, v_info->oem_table_id,
2449 ACPI_OEM_TABLE_ID_SIZE))
2450 switch (v_info->oem_pwr_table) {
2451 case PSS:
2452 return intel_pstate_no_acpi_pss();
2453 case PPC:
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002454 return intel_pstate_has_acpi_ppc() &&
2455 (!force_load);
ethan zhao966916e2014-12-01 11:32:08 +09002456 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002457 }
2458
2459 return false;
2460}
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002461
2462static void intel_pstate_request_control_from_smm(void)
2463{
2464 /*
2465 * It may be unsafe to request P-states control from SMM if _PPC support
2466 * has not been enabled.
2467 */
2468 if (acpi_ppc)
2469 acpi_processor_pstate_control();
2470}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002471#else /* CONFIG_ACPI not enabled */
2472static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
ethan zhao966916e2014-12-01 11:32:08 +09002473static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002474static inline void intel_pstate_request_control_from_smm(void) {}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002475#endif /* CONFIG_ACPI */
2476
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002477static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2478 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
2479 {}
2480};
2481
Dirk Brandewie93f08222013-02-06 09:02:13 -08002482static int __init intel_pstate_init(void)
2483{
Dirk Brandewie907cc902013-03-05 14:15:27 -08002484 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002485 const struct x86_cpu_id *id;
Borislav Petkov64df1fd2015-04-03 15:19:53 +02002486 struct cpu_defaults *cpu_def;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002487
Dirk Brandewie6be26492013-02-15 22:55:10 +01002488 if (no_load)
2489 return -ENODEV;
2490
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002491 if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
2492 copy_cpu_funcs(&core_params.funcs);
2493 hwp_active++;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08002494 intel_pstate.attr = hwp_cpufreq_attrs;
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002495 goto hwp_cpu_matched;
2496 }
2497
Dirk Brandewie93f08222013-02-06 09:02:13 -08002498 id = x86_match_cpu(intel_pstate_cpu_ids);
2499 if (!id)
2500 return -ENODEV;
2501
Borislav Petkov64df1fd2015-04-03 15:19:53 +02002502 cpu_def = (struct cpu_defaults *)id->driver_data;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002503
Borislav Petkov64df1fd2015-04-03 15:19:53 +02002504 copy_pid_params(&cpu_def->pid_policy);
2505 copy_cpu_funcs(&cpu_def->funcs);
Dirk Brandewie016c8152013-10-21 09:20:34 -07002506
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002507 if (intel_pstate_msrs_not_valid())
2508 return -ENODEV;
2509
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002510hwp_cpu_matched:
2511 /*
2512 * The Intel pstate driver will be ignored if the platform
2513 * firmware has its own power management modes.
2514 */
2515 if (intel_pstate_platform_pwr_mgmt_exists())
2516 return -ENODEV;
2517
Joe Perches4836df12016-04-05 13:28:23 -07002518 pr_info("Intel P-state driver initializing\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08002519
Wei Yongjunb57ffac2013-05-13 08:03:43 +00002520 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08002521 if (!all_cpu_data)
2522 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002523
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002524 if (!hwp_active && hwp_only)
2525 goto out;
2526
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002527 intel_pstate_request_control_from_smm();
2528
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002529 rc = cpufreq_register_driver(intel_pstate_driver);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002530 if (rc)
2531 goto out;
2532
Rafael J. Wysocki366430b2016-12-24 00:29:56 +01002533 if (intel_pstate_driver == &intel_pstate && !hwp_active &&
2534 pstate_funcs.get_target_pstate != get_target_pstate_use_cpu_load)
2535 intel_pstate_debug_expose_params();
2536
Dirk Brandewie93f08222013-02-06 09:02:13 -08002537 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08002538
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002539 if (hwp_active)
Joe Perches4836df12016-04-05 13:28:23 -07002540 pr_info("HWP enabled\n");
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002541
Dirk Brandewie93f08222013-02-06 09:02:13 -08002542 return rc;
2543out:
Dirk Brandewie907cc902013-03-05 14:15:27 -08002544 get_online_cpus();
2545 for_each_online_cpu(cpu) {
2546 if (all_cpu_data[cpu]) {
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002547 if (intel_pstate_driver == &intel_pstate)
2548 intel_pstate_clear_update_util_hook(cpu);
2549
Dirk Brandewie907cc902013-03-05 14:15:27 -08002550 kfree(all_cpu_data[cpu]);
2551 }
2552 }
2553
2554 put_online_cpus();
2555 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002556 return -ENODEV;
2557}
2558device_initcall(intel_pstate_init);
2559
Dirk Brandewie6be26492013-02-15 22:55:10 +01002560static int __init intel_pstate_setup(char *str)
2561{
2562 if (!str)
2563 return -EINVAL;
2564
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002565 if (!strcmp(str, "disable")) {
Dirk Brandewie6be26492013-02-15 22:55:10 +01002566 no_load = 1;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002567 } else if (!strcmp(str, "passive")) {
2568 pr_info("Passive mode enabled\n");
2569 intel_pstate_driver = &intel_cpufreq;
2570 no_hwp = 1;
2571 }
Prarit Bhargava539342f2015-10-22 09:43:31 -04002572 if (!strcmp(str, "no_hwp")) {
Joe Perches4836df12016-04-05 13:28:23 -07002573 pr_info("HWP disabled\n");
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002574 no_hwp = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04002575 }
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002576 if (!strcmp(str, "force"))
2577 force_load = 1;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002578 if (!strcmp(str, "hwp_only"))
2579 hwp_only = 1;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002580 if (!strcmp(str, "per_cpu_perf_limits"))
2581 per_cpu_limits = true;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002582
2583#ifdef CONFIG_ACPI
2584 if (!strcmp(str, "support_acpi_ppc"))
2585 acpi_ppc = true;
2586#endif
2587
Dirk Brandewie6be26492013-02-15 22:55:10 +01002588 return 0;
2589}
2590early_param("intel_pstate", intel_pstate_setup);
2591
Dirk Brandewie93f08222013-02-06 09:02:13 -08002592MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2593MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2594MODULE_LICENSE("GPL");