blob: a7f597a476f5355128fdbff357b358a02c252966 [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
Philippe Longepe938d21a2015-11-09 17:40:46 -080042#define ATOM_RATIOS 0x66a
43#define ATOM_VIDS 0x66b
44#define ATOM_TURBO_RATIOS 0x66c
45#define ATOM_TURBO_VIDS 0x66d
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -080046
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -070047#ifdef CONFIG_ACPI
48#include <acpi/processor.h>
49#endif
50
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070051#define FRAC_BITS 8
Dirk Brandewie93f08222013-02-06 09:02:13 -080052#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
53#define fp_toint(X) ((X) >> FRAC_BITS)
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070054
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020055#define EXT_BITS 6
56#define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -080057#define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
58#define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020059
Dirk Brandewie93f08222013-02-06 09:02:13 -080060static inline int32_t mul_fp(int32_t x, int32_t y)
61{
62 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
63}
64
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040065static inline int32_t div_fp(s64 x, s64 y)
Dirk Brandewie93f08222013-02-06 09:02:13 -080066{
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040067 return div64_s64((int64_t)x << FRAC_BITS, y);
Dirk Brandewie93f08222013-02-06 09:02:13 -080068}
69
Dirk Brandewied022a652014-10-13 08:37:44 -070070static inline int ceiling_fp(int32_t x)
71{
72 int mask, ret;
73
74 ret = fp_toint(x);
75 mask = (1 << FRAC_BITS) - 1;
76 if (x & mask)
77 ret += 1;
78 return ret;
79}
80
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020081static inline u64 mul_ext_fp(u64 x, u64 y)
82{
83 return (x * y) >> EXT_FRAC_BITS;
84}
85
86static inline u64 div_ext_fp(u64 x, u64 y)
87{
88 return div64_u64(x << EXT_FRAC_BITS, y);
89}
90
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070091/**
92 * struct sample - Store performance sample
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020093 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070094 * performance during last sample period
95 * @busy_scaled: Scaled busy value which is used to calculate next
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020096 * P state. This can be different than core_avg_perf
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070097 * to account for cpu idle period
98 * @aperf: Difference of actual performance frequency clock count
99 * read from APERF MSR between last and current sample
100 * @mperf: Difference of maximum performance frequency clock count
101 * read from MPERF MSR between last and current sample
102 * @tsc: Difference of time stamp counter between last and
103 * current sample
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700104 * @time: Current time from scheduler
105 *
106 * This structure is used in the cpudata structure to store performance sample
107 * data for choosing next P State.
108 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800109struct sample {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +0200110 int32_t core_avg_perf;
Philippe Longepe157386b2015-12-04 17:40:30 +0100111 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800112 u64 aperf;
113 u64 mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700114 u64 tsc;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100115 u64 time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800116};
117
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700118/**
119 * struct pstate_data - Store P state data
120 * @current_pstate: Current requested P state
121 * @min_pstate: Min P state possible for this platform
122 * @max_pstate: Max P state possible for this platform
123 * @max_pstate_physical:This is physical Max P state for a processor
124 * This can be higher than the max_pstate which can
125 * be limited by platform thermal design power limits
126 * @scaling: Scaling factor to convert frequency to cpufreq
127 * frequency units
128 * @turbo_pstate: Max Turbo P state possible for this platform
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +0100129 * @max_freq: @max_pstate frequency in cpufreq units
130 * @turbo_freq: @turbo_pstate frequency in cpufreq units
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700131 *
132 * Stores the per cpu model P state limits and current P state.
133 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800134struct pstate_data {
135 int current_pstate;
136 int min_pstate;
137 int max_pstate;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700138 int max_pstate_physical;
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700139 int scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800140 int turbo_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +0100141 unsigned int max_freq;
142 unsigned int turbo_freq;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800143};
144
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700145/**
146 * struct vid_data - Stores voltage information data
147 * @min: VID data for this platform corresponding to
148 * the lowest P state
149 * @max: VID data corresponding to the highest P State.
150 * @turbo: VID data for turbo P state
151 * @ratio: Ratio of (vid max - vid min) /
152 * (max P state - Min P State)
153 *
154 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
155 * This data is used in Atom platforms, where in addition to target P state,
156 * the voltage data needs to be specified to select next P State.
157 */
Dirk Brandewie007bea02013-12-18 10:32:39 -0800158struct vid_data {
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700159 int min;
160 int max;
161 int turbo;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800162 int32_t ratio;
163};
164
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700165/**
166 * struct _pid - Stores PID data
167 * @setpoint: Target set point for busyness or performance
168 * @integral: Storage for accumulated error values
169 * @p_gain: PID proportional gain
170 * @i_gain: PID integral gain
171 * @d_gain: PID derivative gain
172 * @deadband: PID deadband
173 * @last_err: Last error storage for integral part of PID calculation
174 *
175 * Stores PID coefficients and last error for PID controller.
176 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800177struct _pid {
178 int setpoint;
179 int32_t integral;
180 int32_t p_gain;
181 int32_t i_gain;
182 int32_t d_gain;
183 int deadband;
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700184 int32_t last_err;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800185};
186
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700187/**
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700188 * struct perf_limits - Store user and policy limits
189 * @no_turbo: User requested turbo state from intel_pstate sysfs
190 * @turbo_disabled: Platform turbo status either from msr
191 * MSR_IA32_MISC_ENABLE or when maximum available pstate
192 * matches the maximum turbo pstate
193 * @max_perf_pct: Effective maximum performance limit in percentage, this
194 * is minimum of either limits enforced by cpufreq policy
195 * or limits from user set limits via intel_pstate sysfs
196 * @min_perf_pct: Effective minimum performance limit in percentage, this
197 * is maximum of either limits enforced by cpufreq policy
198 * or limits from user set limits via intel_pstate sysfs
199 * @max_perf: This is a scaled value between 0 to 255 for max_perf_pct
200 * This value is used to limit max pstate
201 * @min_perf: This is a scaled value between 0 to 255 for min_perf_pct
202 * This value is used to limit min pstate
203 * @max_policy_pct: The maximum performance in percentage enforced by
204 * cpufreq setpolicy interface
205 * @max_sysfs_pct: The maximum performance in percentage enforced by
206 * intel pstate sysfs interface, unused when per cpu
207 * controls are enforced
208 * @min_policy_pct: The minimum performance in percentage enforced by
209 * cpufreq setpolicy interface
210 * @min_sysfs_pct: The minimum performance in percentage enforced by
211 * intel pstate sysfs interface, unused when per cpu
212 * controls are enforced
213 *
214 * Storage for user and policy defined limits.
215 */
216struct perf_limits {
217 int no_turbo;
218 int turbo_disabled;
219 int max_perf_pct;
220 int min_perf_pct;
221 int32_t max_perf;
222 int32_t min_perf;
223 int max_policy_pct;
224 int max_sysfs_pct;
225 int min_policy_pct;
226 int min_sysfs_pct;
227};
228
229/**
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700230 * struct cpudata - Per CPU instance data storage
231 * @cpu: CPU number for this instance data
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200232 * @policy: CPUFreq policy value
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700233 * @update_util: CPUFreq utility callback information
Chen Yu4578ee7e2016-05-11 14:33:08 +0800234 * @update_util_set: CPUFreq utility callback is set
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200235 * @iowait_boost: iowait-related boost fraction
236 * @last_update: Time of the last update.
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700237 * @pstate: Stores P state limits for this CPU
238 * @vid: Stores VID limits for this CPU
239 * @pid: Stores PID parameters for this CPU
240 * @last_sample_time: Last Sample time
241 * @prev_aperf: Last APERF value read from APERF MSR
242 * @prev_mperf: Last MPERF value read from MPERF MSR
243 * @prev_tsc: Last timestamp counter (TSC) value
244 * @prev_cummulative_iowait: IO Wait time difference from last and
245 * current sample
246 * @sample: Storage for storing last Sample data
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700247 * @perf_limits: Pointer to perf_limit unique to this CPU
248 * Not all field in the structure are applicable
249 * when per cpu controls are enforced
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700250 * @acpi_perf_data: Stores ACPI perf information read from _PSS
251 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800252 * @epp_saved: Last saved HWP energy performance preference
253 * (EPP) or energy performance bias (EPB)
254 * @epp_policy: Last saved policy used to set EPP/EPB
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700255 *
256 * This structure stores per CPU instance data for all CPUs.
257 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800258struct cpudata {
259 int cpu;
260
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200261 unsigned int policy;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100262 struct update_util_data update_util;
Chen Yu4578ee7e2016-05-11 14:33:08 +0800263 bool update_util_set;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800264
Dirk Brandewie93f08222013-02-06 09:02:13 -0800265 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800266 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800267 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800268
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200269 u64 last_update;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100270 u64 last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800271 u64 prev_aperf;
272 u64 prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700273 u64 prev_tsc;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100274 u64 prev_cummulative_iowait;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800275 struct sample sample;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700276 struct perf_limits *perf_limits;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700277#ifdef CONFIG_ACPI
278 struct acpi_processor_performance acpi_perf_data;
279 bool valid_pss_table;
280#endif
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200281 unsigned int iowait_boost;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800282 s16 epp_saved;
283 s16 epp_policy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800284};
285
286static struct cpudata **all_cpu_data;
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700287
288/**
Rafael J. Wysocki39545172016-10-11 23:07:38 +0200289 * struct pstate_adjust_policy - Stores static PID configuration data
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700290 * @sample_rate_ms: PID calculation sample rate in ms
291 * @sample_rate_ns: Sample rate calculation in ns
292 * @deadband: PID deadband
293 * @setpoint: PID Setpoint
294 * @p_gain_pct: PID proportional gain
295 * @i_gain_pct: PID integral gain
296 * @d_gain_pct: PID derivative gain
297 *
298 * Stores per CPU model static PID configuration data.
299 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800300struct pstate_adjust_policy {
301 int sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100302 s64 sample_rate_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800303 int deadband;
304 int setpoint;
305 int p_gain_pct;
306 int d_gain_pct;
307 int i_gain_pct;
308};
309
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700310/**
311 * struct pstate_funcs - Per CPU model specific callbacks
312 * @get_max: Callback to get maximum non turbo effective P state
313 * @get_max_physical: Callback to get maximum non turbo physical P state
314 * @get_min: Callback to get minimum P state
315 * @get_turbo: Callback to get turbo P state
316 * @get_scaling: Callback to get frequency scaling factor
317 * @get_val: Callback to convert P state to actual MSR write value
318 * @get_vid: Callback to get VID data for Atom platforms
319 * @get_target_pstate: Callback to a function to calculate next P state to use
320 *
321 * Core and Atom CPU models have different way to get P State limits. This
322 * structure is used to store those callbacks.
323 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700324struct pstate_funcs {
325 int (*get_max)(void);
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700326 int (*get_max_physical)(void);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700327 int (*get_min)(void);
328 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700329 int (*get_scaling)(void);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100330 u64 (*get_val)(struct cpudata*, int pstate);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800331 void (*get_vid)(struct cpudata *);
Philippe Longepe157386b2015-12-04 17:40:30 +0100332 int32_t (*get_target_pstate)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800333};
334
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700335/**
336 * struct cpu_defaults- Per CPU model default config data
337 * @pid_policy: PID config data
338 * @funcs: Callback function data
339 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700340struct cpu_defaults {
341 struct pstate_adjust_policy pid_policy;
342 struct pstate_funcs funcs;
343};
344
Philippe Longepe157386b2015-12-04 17:40:30 +0100345static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
Philippe Longepee70eed22015-12-04 17:40:32 +0100346static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
Philippe Longepe157386b2015-12-04 17:40:30 +0100347
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800348static struct pstate_adjust_policy pid_params __read_mostly;
349static struct pstate_funcs pstate_funcs __read_mostly;
350static int hwp_active __read_mostly;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700351static bool per_cpu_limits __read_mostly;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700352
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700353#ifdef CONFIG_ACPI
354static bool acpi_ppc;
355#endif
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700356
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400357static struct perf_limits performance_limits = {
358 .no_turbo = 0,
359 .turbo_disabled = 0,
360 .max_perf_pct = 100,
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800361 .max_perf = int_ext_tofp(1),
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400362 .min_perf_pct = 100,
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800363 .min_perf = int_ext_tofp(1),
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400364 .max_policy_pct = 100,
365 .max_sysfs_pct = 100,
366 .min_policy_pct = 0,
367 .min_sysfs_pct = 0,
368};
369
370static struct perf_limits powersave_limits = {
Dirk Brandewie93f08222013-02-06 09:02:13 -0800371 .no_turbo = 0,
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700372 .turbo_disabled = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800373 .max_perf_pct = 100,
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800374 .max_perf = int_ext_tofp(1),
Dirk Brandewie93f08222013-02-06 09:02:13 -0800375 .min_perf_pct = 0,
376 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700377 .max_policy_pct = 100,
378 .max_sysfs_pct = 100,
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800379 .min_policy_pct = 0,
380 .min_sysfs_pct = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800381};
382
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400383#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
384static struct perf_limits *limits = &performance_limits;
385#else
386static struct perf_limits *limits = &powersave_limits;
387#endif
388
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700389static DEFINE_MUTEX(intel_pstate_limits_lock);
390
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700391#ifdef CONFIG_ACPI
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700392
393static bool intel_pstate_get_ppc_enable_status(void)
394{
395 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
396 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
397 return true;
398
399 return acpi_ppc;
400}
401
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700402static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
403{
404 struct cpudata *cpu;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700405 int ret;
406 int i;
407
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700408 if (hwp_active)
409 return;
410
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700411 if (!intel_pstate_get_ppc_enable_status())
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700412 return;
413
414 cpu = all_cpu_data[policy->cpu];
415
416 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
417 policy->cpu);
418 if (ret)
419 return;
420
421 /*
422 * Check if the control value in _PSS is for PERF_CTL MSR, which should
423 * guarantee that the states returned by it map to the states in our
424 * list directly.
425 */
426 if (cpu->acpi_perf_data.control_register.space_id !=
427 ACPI_ADR_SPACE_FIXED_HARDWARE)
428 goto err;
429
430 /*
431 * If there is only one entry _PSS, simply ignore _PSS and continue as
432 * usual without taking _PSS into account
433 */
434 if (cpu->acpi_perf_data.state_count < 2)
435 goto err;
436
437 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
438 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
439 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
440 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
441 (u32) cpu->acpi_perf_data.states[i].core_frequency,
442 (u32) cpu->acpi_perf_data.states[i].power,
443 (u32) cpu->acpi_perf_data.states[i].control);
444 }
445
446 /*
447 * The _PSS table doesn't contain whole turbo frequency range.
448 * This just contains +1 MHZ above the max non turbo frequency,
449 * with control value corresponding to max turbo ratio. But
450 * when cpufreq set policy is called, it will call with this
451 * max frequency, which will cause a reduced performance as
452 * this driver uses real max turbo frequency as the max
453 * frequency. So correct this frequency in _PSS table to
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700454 * correct max turbo frequency based on the turbo state.
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700455 * Also need to convert to MHz as _PSS freq is in MHz.
456 */
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700457 if (!limits->turbo_disabled)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700458 cpu->acpi_perf_data.states[0].core_frequency =
459 policy->cpuinfo.max_freq / 1000;
460 cpu->valid_pss_table = true;
Srinivas Pandruvada6cacd112016-05-29 23:31:23 -0700461 pr_debug("_PPC limits will be enforced\n");
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700462
463 return;
464
465 err:
466 cpu->valid_pss_table = false;
467 acpi_processor_unregister_performance(policy->cpu);
468}
469
470static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
471{
472 struct cpudata *cpu;
473
474 cpu = all_cpu_data[policy->cpu];
475 if (!cpu->valid_pss_table)
476 return;
477
478 acpi_processor_unregister_performance(policy->cpu);
479}
480
481#else
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100482static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700483{
484}
485
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100486static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700487{
488}
489#endif
490
Dirk Brandewie93f08222013-02-06 09:02:13 -0800491static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700492 int deadband, int integral) {
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100493 pid->setpoint = int_tofp(setpoint);
494 pid->deadband = int_tofp(deadband);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800495 pid->integral = int_tofp(integral);
Dirk Brandewied98d0992014-02-12 10:01:05 -0800496 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800497}
498
499static inline void pid_p_gain_set(struct _pid *pid, int percent)
500{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200501 pid->p_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800502}
503
504static inline void pid_i_gain_set(struct _pid *pid, int percent)
505{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200506 pid->i_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800507}
508
509static inline void pid_d_gain_set(struct _pid *pid, int percent)
510{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200511 pid->d_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800512}
513
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700514static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800515{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700516 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800517 int32_t pterm, dterm, fp_error;
518 int32_t integral_limit;
519
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100520 fp_error = pid->setpoint - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800521
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100522 if (abs(fp_error) <= pid->deadband)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800523 return 0;
524
525 pterm = mul_fp(pid->p_gain, fp_error);
526
527 pid->integral += fp_error;
528
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800529 /*
530 * We limit the integral here so that it will never
531 * get higher than 30. This prevents it from becoming
532 * too large an input over long periods of time and allows
533 * it to get factored out sooner.
534 *
535 * The value of 30 was chosen through experimentation.
536 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800537 integral_limit = int_tofp(30);
538 if (pid->integral > integral_limit)
539 pid->integral = integral_limit;
540 if (pid->integral < -integral_limit)
541 pid->integral = -integral_limit;
542
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700543 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
544 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800545
546 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
Doug Smythies51d211e2014-06-17 13:36:10 -0700547 result = result + (1 << (FRAC_BITS-1));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800548 return (signed int)fp_toint(result);
549}
550
551static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
552{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700553 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
554 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
555 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800556
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700557 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800558}
559
Dirk Brandewie93f08222013-02-06 09:02:13 -0800560static inline void intel_pstate_reset_all_pid(void)
561{
562 unsigned int cpu;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700563
Dirk Brandewie93f08222013-02-06 09:02:13 -0800564 for_each_online_cpu(cpu) {
565 if (all_cpu_data[cpu])
566 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
567 }
568}
569
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700570static inline void update_turbo_state(void)
571{
572 u64 misc_en;
573 struct cpudata *cpu;
574
575 cpu = all_cpu_data[0];
576 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400577 limits->turbo_disabled =
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700578 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
579 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
580}
581
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800582static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
583{
584 u64 epb;
585 int ret;
586
587 if (!static_cpu_has(X86_FEATURE_EPB))
588 return -ENXIO;
589
590 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
591 if (ret)
592 return (s16)ret;
593
594 return (s16)(epb & 0x0f);
595}
596
597static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
598{
599 s16 epp;
600
601 if (static_cpu_has(X86_FEATURE_HWP_EPP))
602 epp = (hwp_req_data >> 24) & 0xff;
603 else
604 /* When there is no EPP present, HWP uses EPB settings */
605 epp = intel_pstate_get_epb(cpu_data);
606
607 return epp;
608}
609
610static void intel_pstate_set_epb(int cpu, s16 pref)
611{
612 u64 epb;
613
614 if (!static_cpu_has(X86_FEATURE_EPB))
615 return;
616
617 if (rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb))
618 return;
619
620 epb = (epb & ~0x0f) | pref;
621 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
622}
623
Viresh Kumar41cfd642016-02-22 10:27:46 +0530624static void intel_pstate_hwp_set(const struct cpumask *cpumask)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800625{
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700626 int min, hw_min, max, hw_max, cpu, range, adj_range;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700627 struct perf_limits *perf_limits = limits;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700628 u64 value, cap;
629
Viresh Kumar41cfd642016-02-22 10:27:46 +0530630 for_each_cpu(cpu, cpumask) {
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700631 int max_perf_pct, min_perf_pct;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800632 struct cpudata *cpu_data = all_cpu_data[cpu];
633 s16 epp;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700634
635 if (per_cpu_limits)
636 perf_limits = all_cpu_data[cpu]->perf_limits;
637
Srinivas Pandruvadaf9f48722016-10-08 12:42:38 -0700638 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
639 hw_min = HWP_LOWEST_PERF(cap);
640 hw_max = HWP_HIGHEST_PERF(cap);
641 range = hw_max - hw_min;
642
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700643 max_perf_pct = perf_limits->max_perf_pct;
644 min_perf_pct = perf_limits->min_perf_pct;
645
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800646 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700647 adj_range = min_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700648 min = hw_min + adj_range;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800649 value &= ~HWP_MIN_PERF(~0L);
650 value |= HWP_MIN_PERF(min);
651
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700652 adj_range = max_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700653 max = hw_min + adj_range;
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400654 if (limits->no_turbo) {
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700655 hw_max = HWP_GUARANTEED_PERF(cap);
656 if (hw_max < max)
657 max = hw_max;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800658 }
659
660 value &= ~HWP_MAX_PERF(~0L);
661 value |= HWP_MAX_PERF(max);
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800662
663 if (cpu_data->epp_policy == cpu_data->policy)
664 goto skip_epp;
665
666 cpu_data->epp_policy = cpu_data->policy;
667
668 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
669 epp = intel_pstate_get_epp(cpu_data, value);
670 /* If EPP read was failed, then don't try to write */
671 if (epp < 0) {
672 cpu_data->epp_saved = epp;
673 goto skip_epp;
674 }
675
676 cpu_data->epp_saved = epp;
677
678 epp = 0;
679 } else {
680 /* skip setting EPP, when saved value is invalid */
681 if (cpu_data->epp_saved < 0)
682 goto skip_epp;
683
684 /*
685 * No need to restore EPP when it is not zero. This
686 * means:
687 * - Policy is not changed
688 * - user has manually changed
689 * - Error reading EPB
690 */
691 epp = intel_pstate_get_epp(cpu_data, value);
692 if (epp)
693 goto skip_epp;
694
695 epp = cpu_data->epp_saved;
696 }
697 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
698 value &= ~GENMASK_ULL(31, 24);
699 value |= (u64)epp << 24;
700 } else {
701 intel_pstate_set_epb(cpu, epp);
702 }
703skip_epp:
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800704 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
705 }
Viresh Kumar41cfd642016-02-22 10:27:46 +0530706}
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800707
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +0200708static int intel_pstate_hwp_set_policy(struct cpufreq_policy *policy)
709{
710 if (hwp_active)
711 intel_pstate_hwp_set(policy->cpus);
712
713 return 0;
714}
715
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800716static int intel_pstate_resume(struct cpufreq_policy *policy)
717{
718 if (!hwp_active)
719 return 0;
720
721 all_cpu_data[policy->cpu]->epp_policy = 0;
722 all_cpu_data[policy->cpu]->epp_saved = -EINVAL;
723
724 return intel_pstate_hwp_set_policy(policy);
725}
726
Viresh Kumar41cfd642016-02-22 10:27:46 +0530727static void intel_pstate_hwp_set_online_cpus(void)
728{
729 get_online_cpus();
730 intel_pstate_hwp_set(cpu_online_mask);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800731 put_online_cpus();
732}
733
Dirk Brandewie93f08222013-02-06 09:02:13 -0800734/************************** debugfs begin ************************/
735static int pid_param_set(void *data, u64 val)
736{
737 *(u32 *)data = val;
738 intel_pstate_reset_all_pid();
739 return 0;
740}
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700741
Dirk Brandewie93f08222013-02-06 09:02:13 -0800742static int pid_param_get(void *data, u64 *val)
743{
744 *val = *(u32 *)data;
745 return 0;
746}
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700747DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -0800748
749struct pid_param {
750 char *name;
751 void *value;
752};
753
754static struct pid_param pid_files[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700755 {"sample_rate_ms", &pid_params.sample_rate_ms},
756 {"d_gain_pct", &pid_params.d_gain_pct},
757 {"i_gain_pct", &pid_params.i_gain_pct},
758 {"deadband", &pid_params.deadband},
759 {"setpoint", &pid_params.setpoint},
760 {"p_gain_pct", &pid_params.p_gain_pct},
Dirk Brandewie93f08222013-02-06 09:02:13 -0800761 {NULL, NULL}
762};
763
Stratos Karafotis317dd502014-07-18 08:37:17 -0700764static void __init intel_pstate_debug_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800765{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700766 struct dentry *debugfs_parent;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800767 int i = 0;
768
Srinivas Pandruvada185d8242016-10-21 09:38:20 -0700769 if (hwp_active ||
770 pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800771 return;
Srinivas Pandruvada185d8242016-10-21 09:38:20 -0700772
Dirk Brandewie93f08222013-02-06 09:02:13 -0800773 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
774 if (IS_ERR_OR_NULL(debugfs_parent))
775 return;
776 while (pid_files[i].name) {
777 debugfs_create_file(pid_files[i].name, 0660,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700778 debugfs_parent, pid_files[i].value,
779 &fops_pid_param);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800780 i++;
781 }
782}
783
784/************************** debugfs end ************************/
785
786/************************** sysfs begin ************************/
787#define show_one(file_name, object) \
788 static ssize_t show_##file_name \
789 (struct kobject *kobj, struct attribute *attr, char *buf) \
790 { \
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400791 return sprintf(buf, "%u\n", limits->object); \
Dirk Brandewie93f08222013-02-06 09:02:13 -0800792 }
793
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800794static ssize_t show_turbo_pct(struct kobject *kobj,
795 struct attribute *attr, char *buf)
796{
797 struct cpudata *cpu;
798 int total, no_turbo, turbo_pct;
799 uint32_t turbo_fp;
800
801 cpu = all_cpu_data[0];
802
803 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
804 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200805 turbo_fp = div_fp(no_turbo, total);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800806 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
807 return sprintf(buf, "%u\n", turbo_pct);
808}
809
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800810static ssize_t show_num_pstates(struct kobject *kobj,
811 struct attribute *attr, char *buf)
812{
813 struct cpudata *cpu;
814 int total;
815
816 cpu = all_cpu_data[0];
817 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
818 return sprintf(buf, "%u\n", total);
819}
820
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700821static ssize_t show_no_turbo(struct kobject *kobj,
822 struct attribute *attr, char *buf)
823{
824 ssize_t ret;
825
826 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400827 if (limits->turbo_disabled)
828 ret = sprintf(buf, "%u\n", limits->turbo_disabled);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700829 else
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400830 ret = sprintf(buf, "%u\n", limits->no_turbo);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700831
832 return ret;
833}
834
Dirk Brandewie93f08222013-02-06 09:02:13 -0800835static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700836 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800837{
838 unsigned int input;
839 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700840
Dirk Brandewie93f08222013-02-06 09:02:13 -0800841 ret = sscanf(buf, "%u", &input);
842 if (ret != 1)
843 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700844
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700845 mutex_lock(&intel_pstate_limits_lock);
846
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700847 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400848 if (limits->turbo_disabled) {
Joe Perches4836df12016-04-05 13:28:23 -0700849 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700850 mutex_unlock(&intel_pstate_limits_lock);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700851 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700852 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800853
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400854 limits->no_turbo = clamp_t(int, input, 0, 1);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700855
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700856 mutex_unlock(&intel_pstate_limits_lock);
857
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800858 if (hwp_active)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530859 intel_pstate_hwp_set_online_cpus();
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800860
Dirk Brandewie93f08222013-02-06 09:02:13 -0800861 return count;
862}
863
864static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700865 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800866{
867 unsigned int input;
868 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700869
Dirk Brandewie93f08222013-02-06 09:02:13 -0800870 ret = sscanf(buf, "%u", &input);
871 if (ret != 1)
872 return -EINVAL;
873
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700874 mutex_lock(&intel_pstate_limits_lock);
875
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400876 limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
877 limits->max_perf_pct = min(limits->max_policy_pct,
878 limits->max_sysfs_pct);
879 limits->max_perf_pct = max(limits->min_policy_pct,
880 limits->max_perf_pct);
881 limits->max_perf_pct = max(limits->min_perf_pct,
882 limits->max_perf_pct);
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800883 limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700884
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700885 mutex_unlock(&intel_pstate_limits_lock);
886
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800887 if (hwp_active)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530888 intel_pstate_hwp_set_online_cpus();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800889 return count;
890}
891
892static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700893 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800894{
895 unsigned int input;
896 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700897
Dirk Brandewie93f08222013-02-06 09:02:13 -0800898 ret = sscanf(buf, "%u", &input);
899 if (ret != 1)
900 return -EINVAL;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800901
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700902 mutex_lock(&intel_pstate_limits_lock);
903
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400904 limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
905 limits->min_perf_pct = max(limits->min_policy_pct,
906 limits->min_sysfs_pct);
907 limits->min_perf_pct = min(limits->max_policy_pct,
908 limits->min_perf_pct);
909 limits->min_perf_pct = min(limits->max_perf_pct,
910 limits->min_perf_pct);
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800911 limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800912
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -0700913 mutex_unlock(&intel_pstate_limits_lock);
914
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800915 if (hwp_active)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530916 intel_pstate_hwp_set_online_cpus();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800917 return count;
918}
919
Dirk Brandewie93f08222013-02-06 09:02:13 -0800920show_one(max_perf_pct, max_perf_pct);
921show_one(min_perf_pct, min_perf_pct);
922
923define_one_global_rw(no_turbo);
924define_one_global_rw(max_perf_pct);
925define_one_global_rw(min_perf_pct);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800926define_one_global_ro(turbo_pct);
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800927define_one_global_ro(num_pstates);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800928
929static struct attribute *intel_pstate_attributes[] = {
930 &no_turbo.attr,
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800931 &turbo_pct.attr,
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800932 &num_pstates.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800933 NULL
934};
935
936static struct attribute_group intel_pstate_attr_group = {
937 .attrs = intel_pstate_attributes,
938};
Dirk Brandewie93f08222013-02-06 09:02:13 -0800939
Stratos Karafotis317dd502014-07-18 08:37:17 -0700940static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800941{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700942 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800943 int rc;
944
945 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
946 &cpu_subsys.dev_root->kobj);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700947 if (WARN_ON(!intel_pstate_kobject))
948 return;
949
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700950 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700951 if (WARN_ON(rc))
952 return;
953
954 /*
955 * If per cpu limits are enforced there are no global limits, so
956 * return without creating max/min_perf_pct attributes
957 */
958 if (per_cpu_limits)
959 return;
960
961 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
962 WARN_ON(rc);
963
964 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
965 WARN_ON(rc);
966
Dirk Brandewie93f08222013-02-06 09:02:13 -0800967}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800968/************************** sysfs end ************************/
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800969
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -0700970static void intel_pstate_hwp_enable(struct cpudata *cpudata)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800971{
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -0800972 /* First disable HWP notification interrupt as we don't process them */
Srinivas Pandruvadada7de912016-07-19 16:52:01 -0700973 if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
974 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -0800975
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -0700976 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800977 cpudata->epp_policy = 0;
978 cpudata->epp_saved = -EINVAL;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800979}
980
Philippe Longepe938d21a2015-11-09 17:40:46 -0800981static int atom_get_min_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700982{
983 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700984
Philippe Longepe938d21a2015-11-09 17:40:46 -0800985 rdmsrl(ATOM_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700986 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700987}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800988
Philippe Longepe938d21a2015-11-09 17:40:46 -0800989static int atom_get_max_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700990{
991 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700992
Philippe Longepe938d21a2015-11-09 17:40:46 -0800993 rdmsrl(ATOM_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700994 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700995}
996
Philippe Longepe938d21a2015-11-09 17:40:46 -0800997static int atom_get_turbo_pstate(void)
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800998{
999 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001000
Philippe Longepe938d21a2015-11-09 17:40:46 -08001001 rdmsrl(ATOM_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001002 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001003}
1004
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001005static u64 atom_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001006{
1007 u64 val;
1008 int32_t vid_fp;
1009 u32 vid;
1010
Chen Yu144c8e12015-07-29 23:53:10 +08001011 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001012 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001013 val |= (u64)1 << 32;
1014
1015 vid_fp = cpudata->vid.min + mul_fp(
1016 int_tofp(pstate - cpudata->pstate.min_pstate),
1017 cpudata->vid.ratio);
1018
1019 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
Dirk Brandewied022a652014-10-13 08:37:44 -07001020 vid = ceiling_fp(vid_fp);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001021
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001022 if (pstate > cpudata->pstate.max_pstate)
1023 vid = cpudata->vid.turbo;
1024
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001025 return val | vid;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001026}
1027
Philippe Longepe1421df62015-11-09 17:40:47 -08001028static int silvermont_get_scaling(void)
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001029{
1030 u64 value;
1031 int i;
Philippe Longepe1421df62015-11-09 17:40:47 -08001032 /* Defined in Table 35-6 from SDM (Sept 2015) */
1033 static int silvermont_freq_table[] = {
1034 83300, 100000, 133300, 116700, 80000};
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001035
1036 rdmsrl(MSR_FSB_FREQ, value);
Philippe Longepe1421df62015-11-09 17:40:47 -08001037 i = value & 0x7;
1038 WARN_ON(i > 4);
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001039
Philippe Longepe1421df62015-11-09 17:40:47 -08001040 return silvermont_freq_table[i];
1041}
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001042
Philippe Longepe1421df62015-11-09 17:40:47 -08001043static int airmont_get_scaling(void)
1044{
1045 u64 value;
1046 int i;
1047 /* Defined in Table 35-10 from SDM (Sept 2015) */
1048 static int airmont_freq_table[] = {
1049 83300, 100000, 133300, 116700, 80000,
1050 93300, 90000, 88900, 87500};
1051
1052 rdmsrl(MSR_FSB_FREQ, value);
1053 i = value & 0xF;
1054 WARN_ON(i > 8);
1055
1056 return airmont_freq_table[i];
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001057}
1058
Philippe Longepe938d21a2015-11-09 17:40:46 -08001059static void atom_get_vid(struct cpudata *cpudata)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001060{
1061 u64 value;
1062
Philippe Longepe938d21a2015-11-09 17:40:46 -08001063 rdmsrl(ATOM_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001064 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1065 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001066 cpudata->vid.ratio = div_fp(
1067 cpudata->vid.max - cpudata->vid.min,
1068 int_tofp(cpudata->pstate.max_pstate -
1069 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001070
Philippe Longepe938d21a2015-11-09 17:40:46 -08001071 rdmsrl(ATOM_TURBO_VIDS, value);
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001072 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001073}
1074
Dirk Brandewie016c8152013-10-21 09:20:34 -07001075static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001076{
1077 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001078
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001079 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001080 return (value >> 40) & 0xFF;
1081}
1082
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001083static int core_get_max_pstate_physical(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001084{
1085 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001086
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001087 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001088 return (value >> 8) & 0xFF;
1089}
1090
Dirk Brandewie93f08222013-02-06 09:02:13 -08001091static int core_get_max_pstate(void)
1092{
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001093 u64 tar;
1094 u64 plat_info;
1095 int max_pstate;
1096 int err;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001097
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001098 rdmsrl(MSR_PLATFORM_INFO, plat_info);
1099 max_pstate = (plat_info >> 8) & 0xFF;
1100
1101 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1102 if (!err) {
1103 /* Do some sanity checking for safety */
1104 if (plat_info & 0x600000000) {
1105 u64 tdp_ctrl;
1106 u64 tdp_ratio;
1107 int tdp_msr;
1108
1109 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1110 if (err)
1111 goto skip_tar;
1112
Jan Kiszka5fc8f7072016-07-08 20:42:04 +02001113 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x3);
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001114 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1115 if (err)
1116 goto skip_tar;
1117
Srinivas Pandruvada1becf032016-04-22 19:53:59 -07001118 /* For level 1 and 2, bits[23:16] contain the ratio */
1119 if (tdp_ctrl)
1120 tdp_ratio >>= 16;
1121
1122 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001123 if (tdp_ratio - 1 == tar) {
1124 max_pstate = tar;
1125 pr_debug("max_pstate=TAC %x\n", max_pstate);
1126 } else {
1127 goto skip_tar;
1128 }
1129 }
1130 }
1131
1132skip_tar:
1133 return max_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001134}
1135
Dirk Brandewie016c8152013-10-21 09:20:34 -07001136static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001137{
1138 u64 value;
1139 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001140
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001141 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001142 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -07001143 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001144 if (ret <= nont)
1145 ret = nont;
1146 return ret;
1147}
1148
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001149static inline int core_get_scaling(void)
1150{
1151 return 100000;
1152}
1153
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001154static u64 core_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001155{
1156 u64 val;
1157
Chen Yu144c8e12015-07-29 23:53:10 +08001158 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001159 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001160 val |= (u64)1 << 32;
1161
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001162 return val;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001163}
1164
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001165static int knl_get_turbo_pstate(void)
1166{
1167 u64 value;
1168 int nont, ret;
1169
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001170 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001171 nont = core_get_max_pstate();
1172 ret = (((value) >> 8) & 0xFF);
1173 if (ret <= nont)
1174 ret = nont;
1175 return ret;
1176}
1177
Dirk Brandewie016c8152013-10-21 09:20:34 -07001178static struct cpu_defaults core_params = {
1179 .pid_policy = {
1180 .sample_rate_ms = 10,
1181 .deadband = 0,
1182 .setpoint = 97,
1183 .p_gain_pct = 20,
1184 .d_gain_pct = 0,
1185 .i_gain_pct = 0,
1186 },
1187 .funcs = {
1188 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001189 .get_max_physical = core_get_max_pstate_physical,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001190 .get_min = core_get_min_pstate,
1191 .get_turbo = core_get_turbo_pstate,
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001192 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001193 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001194 .get_target_pstate = get_target_pstate_use_performance,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001195 },
1196};
1197
Julia Lawall42ce8922016-09-11 15:06:01 +02001198static const struct cpu_defaults silvermont_params = {
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001199 .pid_policy = {
1200 .sample_rate_ms = 10,
1201 .deadband = 0,
Kristen Carlson Accardi6a82ba62015-04-10 11:06:43 -07001202 .setpoint = 60,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001203 .p_gain_pct = 14,
1204 .d_gain_pct = 0,
1205 .i_gain_pct = 4,
1206 },
1207 .funcs = {
Philippe Longepe938d21a2015-11-09 17:40:46 -08001208 .get_max = atom_get_max_pstate,
1209 .get_max_physical = atom_get_max_pstate,
1210 .get_min = atom_get_min_pstate,
1211 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001212 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001213 .get_scaling = silvermont_get_scaling,
1214 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001215 .get_target_pstate = get_target_pstate_use_cpu_load,
Philippe Longepe1421df62015-11-09 17:40:47 -08001216 },
1217};
1218
Julia Lawall42ce8922016-09-11 15:06:01 +02001219static const struct cpu_defaults airmont_params = {
Philippe Longepe1421df62015-11-09 17:40:47 -08001220 .pid_policy = {
1221 .sample_rate_ms = 10,
1222 .deadband = 0,
1223 .setpoint = 60,
1224 .p_gain_pct = 14,
1225 .d_gain_pct = 0,
1226 .i_gain_pct = 4,
1227 },
1228 .funcs = {
1229 .get_max = atom_get_max_pstate,
1230 .get_max_physical = atom_get_max_pstate,
1231 .get_min = atom_get_min_pstate,
1232 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001233 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001234 .get_scaling = airmont_get_scaling,
Philippe Longepe938d21a2015-11-09 17:40:46 -08001235 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001236 .get_target_pstate = get_target_pstate_use_cpu_load,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001237 },
1238};
1239
Julia Lawall42ce8922016-09-11 15:06:01 +02001240static const struct cpu_defaults knl_params = {
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001241 .pid_policy = {
1242 .sample_rate_ms = 10,
1243 .deadband = 0,
1244 .setpoint = 97,
1245 .p_gain_pct = 20,
1246 .d_gain_pct = 0,
1247 .i_gain_pct = 0,
1248 },
1249 .funcs = {
1250 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001251 .get_max_physical = core_get_max_pstate_physical,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001252 .get_min = core_get_min_pstate,
1253 .get_turbo = knl_get_turbo_pstate,
Lukasz Anaczkowski69cefc22015-07-21 10:41:13 +02001254 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001255 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001256 .get_target_pstate = get_target_pstate_use_performance,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001257 },
1258};
1259
Julia Lawall42ce8922016-09-11 15:06:01 +02001260static const struct cpu_defaults bxt_params = {
Srinivas Pandruvada41bad472016-06-09 15:34:41 -07001261 .pid_policy = {
1262 .sample_rate_ms = 10,
1263 .deadband = 0,
1264 .setpoint = 60,
1265 .p_gain_pct = 14,
1266 .d_gain_pct = 0,
1267 .i_gain_pct = 4,
1268 },
1269 .funcs = {
1270 .get_max = core_get_max_pstate,
1271 .get_max_physical = core_get_max_pstate_physical,
1272 .get_min = core_get_min_pstate,
1273 .get_turbo = core_get_turbo_pstate,
1274 .get_scaling = core_get_scaling,
1275 .get_val = core_get_val,
1276 .get_target_pstate = get_target_pstate_use_cpu_load,
1277 },
1278};
1279
Dirk Brandewie93f08222013-02-06 09:02:13 -08001280static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
1281{
1282 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie7244cb62013-10-21 09:20:33 -07001283 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001284 int min_perf;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001285 struct perf_limits *perf_limits = limits;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001286
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001287 if (limits->no_turbo || limits->turbo_disabled)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001288 max_perf = cpu->pstate.max_pstate;
1289
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001290 if (per_cpu_limits)
1291 perf_limits = cpu->perf_limits;
1292
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001293 /*
1294 * performance can be limited by user through sysfs, by cpufreq
1295 * policy, or by cpu specific default values determined through
1296 * experimentation.
1297 */
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001298 max_perf_adj = fp_ext_toint(max_perf * perf_limits->max_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001299 *max = clamp_t(int, max_perf_adj,
1300 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001301
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001302 min_perf = fp_ext_toint(max_perf * perf_limits->min_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001303 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001304}
1305
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001306static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001307{
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001308 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1309 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001310 /*
1311 * Generally, there is no guarantee that this code will always run on
1312 * the CPU being updated, so force the register update to run on the
1313 * right CPU.
1314 */
1315 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1316 pstate_funcs.get_val(cpu, pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001317}
1318
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001319static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1320{
1321 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1322}
1323
1324static void intel_pstate_max_within_limits(struct cpudata *cpu)
1325{
1326 int min_pstate, max_pstate;
1327
1328 update_turbo_state();
1329 intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate);
1330 intel_pstate_set_pstate(cpu, max_pstate);
1331}
1332
Dirk Brandewie93f08222013-02-06 09:02:13 -08001333static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1334{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001335 cpu->pstate.min_pstate = pstate_funcs.get_min();
1336 cpu->pstate.max_pstate = pstate_funcs.get_max();
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001337 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
Dirk Brandewie016c8152013-10-21 09:20:34 -07001338 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001339 cpu->pstate.scaling = pstate_funcs.get_scaling();
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001340 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1341 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001342
Dirk Brandewie007bea02013-12-18 10:32:39 -08001343 if (pstate_funcs.get_vid)
1344 pstate_funcs.get_vid(cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001345
1346 intel_pstate_set_min_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001347}
1348
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001349static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001350{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +03001351 struct sample *sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001352
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001353 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001354}
1355
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001356static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001357{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001358 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001359 unsigned long flags;
Doug Smythies4055fad2015-04-11 21:10:26 -07001360 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001361
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001362 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001363 rdmsrl(MSR_IA32_APERF, aperf);
1364 rdmsrl(MSR_IA32_MPERF, mperf);
Philippe Longepee70eed22015-12-04 17:40:32 +01001365 tsc = rdtsc();
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001366 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001367 local_irq_restore(flags);
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001368 return false;
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001369 }
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001370 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001371
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001372 cpu->last_sample_time = cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001373 cpu->sample.time = time;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001374 cpu->sample.aperf = aperf;
1375 cpu->sample.mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001376 cpu->sample.tsc = tsc;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001377 cpu->sample.aperf -= cpu->prev_aperf;
1378 cpu->sample.mperf -= cpu->prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001379 cpu->sample.tsc -= cpu->prev_tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001380
Dirk Brandewie93f08222013-02-06 09:02:13 -08001381 cpu->prev_aperf = aperf;
1382 cpu->prev_mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001383 cpu->prev_tsc = tsc;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001384 /*
1385 * First time this function is invoked in a given cycle, all of the
1386 * previous sample data fields are equal to zero or stale and they must
1387 * be populated with meaningful numbers for things to work, so assume
1388 * that sample.time will always be reset before setting the utilization
1389 * update hook and make the caller skip the sample then.
1390 */
1391 return !!cpu->last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001392}
1393
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001394static inline int32_t get_avg_frequency(struct cpudata *cpu)
1395{
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001396 return mul_ext_fp(cpu->sample.core_avg_perf,
1397 cpu->pstate.max_pstate_physical * cpu->pstate.scaling);
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001398}
1399
Philippe Longepebdcaa232016-04-22 11:46:09 -07001400static inline int32_t get_avg_pstate(struct cpudata *cpu)
1401{
Rafael J. Wysocki8edb0a62016-05-11 19:10:42 +02001402 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1403 cpu->sample.core_avg_perf);
Philippe Longepebdcaa232016-04-22 11:46:09 -07001404}
1405
Philippe Longepee70eed22015-12-04 17:40:32 +01001406static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
1407{
1408 struct sample *sample = &cpu->sample;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001409 int32_t busy_frac, boost;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001410 int target, avg_pstate;
Philippe Longepee70eed22015-12-04 17:40:32 +01001411
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001412 busy_frac = div_fp(sample->mperf, sample->tsc);
Philippe Longepe63d1d652015-12-04 17:40:35 +01001413
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001414 boost = cpu->iowait_boost;
1415 cpu->iowait_boost >>= 1;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001416
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001417 if (busy_frac < boost)
1418 busy_frac = boost;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001419
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001420 sample->busy_scaled = busy_frac * 100;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001421
1422 target = limits->no_turbo || limits->turbo_disabled ?
1423 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1424 target += target >> 2;
1425 target = mul_fp(target, busy_frac);
1426 if (target < cpu->pstate.min_pstate)
1427 target = cpu->pstate.min_pstate;
1428
1429 /*
1430 * If the average P-state during the previous cycle was higher than the
1431 * current target, add 50% of the difference to the target to reduce
1432 * possible performance oscillations and offset possible performance
1433 * loss related to moving the workload from one CPU to another within
1434 * a package/module.
1435 */
1436 avg_pstate = get_avg_pstate(cpu);
1437 if (avg_pstate > target)
1438 target += (avg_pstate - target) >> 1;
1439
1440 return target;
Philippe Longepee70eed22015-12-04 17:40:32 +01001441}
1442
Philippe Longepe157386b2015-12-04 17:40:30 +01001443static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001444{
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001445 int32_t perf_scaled, max_pstate, current_pstate, sample_ratio;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001446 u64 duration_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001447
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001448 /*
Rafael J. Wysockif00593a2016-09-30 03:13:34 +02001449 * perf_scaled is the ratio of the average P-state during the last
1450 * sampling period to the P-state requested last time (in percent).
1451 *
1452 * That measures the system's response to the previous P-state
1453 * selection.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001454 */
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001455 max_pstate = cpu->pstate.max_pstate_physical;
1456 current_pstate = cpu->pstate.current_pstate;
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001457 perf_scaled = mul_ext_fp(cpu->sample.core_avg_perf,
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001458 div_fp(100 * max_pstate, current_pstate));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001459
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001460 /*
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001461 * Since our utilization update callback will not run unless we are
1462 * in C0, check if the actual elapsed time is significantly greater (3x)
1463 * than our sample interval. If it is, then we were idle for a long
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001464 * enough period of time to adjust our performance metric.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001465 */
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001466 duration_ns = cpu->sample.time - cpu->last_sample_time;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001467 if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001468 sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns);
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001469 perf_scaled = mul_fp(perf_scaled, sample_ratio);
Rafael J. Wysockiffb81052016-04-10 05:59:10 +02001470 } else {
1471 sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc);
1472 if (sample_ratio < int_tofp(1))
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001473 perf_scaled = 0;
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001474 }
1475
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001476 cpu->sample.busy_scaled = perf_scaled;
1477 return cpu->pstate.current_pstate - pid_calc(&cpu->pid, perf_scaled);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001478}
1479
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001480static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001481{
1482 int max_perf, min_perf;
1483
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001484 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
1485 pstate = clamp_t(int, pstate, min_perf, max_perf);
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001486 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001487 return pstate;
1488}
1489
1490static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1491{
1492 pstate = intel_pstate_prepare_request(cpu, pstate);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001493 if (pstate == cpu->pstate.current_pstate)
1494 return;
1495
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001496 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001497 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1498}
1499
Dirk Brandewie93f08222013-02-06 09:02:13 -08001500static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
1501{
Philippe Longepe157386b2015-12-04 17:40:30 +01001502 int from, target_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001503 struct sample *sample;
1504
1505 from = cpu->pstate.current_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001506
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02001507 target_pstate = cpu->policy == CPUFREQ_POLICY_PERFORMANCE ?
1508 cpu->pstate.turbo_pstate : pstate_funcs.get_target_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001509
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001510 update_turbo_state();
1511
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001512 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies4055fad2015-04-11 21:10:26 -07001513
1514 sample = &cpu->sample;
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001515 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
Philippe Longepe157386b2015-12-04 17:40:30 +01001516 fp_toint(sample->busy_scaled),
Doug Smythies4055fad2015-04-11 21:10:26 -07001517 from,
1518 cpu->pstate.current_pstate,
1519 sample->mperf,
1520 sample->aperf,
1521 sample->tsc,
Srinivas Pandruvada3ba7bca2016-09-13 17:41:33 -07001522 get_avg_frequency(cpu),
1523 fp_toint(cpu->iowait_boost * 100));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001524}
1525
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001526static void intel_pstate_update_util(struct update_util_data *data, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02001527 unsigned int flags)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001528{
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001529 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001530 u64 delta_ns;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001531
Rafael J. Wysocki1d298152016-10-19 02:53:26 +02001532 if (pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load) {
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001533 if (flags & SCHED_CPUFREQ_IOWAIT) {
1534 cpu->iowait_boost = int_tofp(1);
1535 } else if (cpu->iowait_boost) {
1536 /* Clear iowait_boost if the CPU may have been idle. */
1537 delta_ns = time - cpu->last_update;
1538 if (delta_ns > TICK_NSEC)
1539 cpu->iowait_boost = 0;
1540 }
1541 cpu->last_update = time;
1542 }
1543
1544 delta_ns = time - cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001545 if ((s64)delta_ns >= pid_params.sample_rate_ns) {
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001546 bool sample_taken = intel_pstate_sample(cpu, time);
1547
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001548 if (sample_taken) {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001549 intel_pstate_calc_avg_perf(cpu);
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001550 if (!hwp_active)
1551 intel_pstate_adjust_busy_pstate(cpu);
1552 }
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001553 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001554}
1555
1556#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -08001557 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1558 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001559
1560static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dave Hansen5b20c942016-06-02 17:19:45 -07001561 ICPU(INTEL_FAM6_SANDYBRIDGE, core_params),
1562 ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_params),
1563 ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_params),
1564 ICPU(INTEL_FAM6_IVYBRIDGE, core_params),
1565 ICPU(INTEL_FAM6_HASWELL_CORE, core_params),
1566 ICPU(INTEL_FAM6_BROADWELL_CORE, core_params),
1567 ICPU(INTEL_FAM6_IVYBRIDGE_X, core_params),
1568 ICPU(INTEL_FAM6_HASWELL_X, core_params),
1569 ICPU(INTEL_FAM6_HASWELL_ULT, core_params),
1570 ICPU(INTEL_FAM6_HASWELL_GT3E, core_params),
1571 ICPU(INTEL_FAM6_BROADWELL_GT3E, core_params),
1572 ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_params),
1573 ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_params),
1574 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1575 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_params),
1576 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
1577 ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_params),
Piotr Luc58bf4542016-10-13 17:31:00 +02001578 ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_params),
Srinivas Pandruvada41bad472016-06-09 15:34:41 -07001579 ICPU(INTEL_FAM6_ATOM_GOLDMONT, bxt_params),
Dirk Brandewie93f08222013-02-06 09:02:13 -08001580 {}
1581};
1582MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1583
Jisheng Zhang29327c82016-06-27 18:07:17 +08001584static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
Dave Hansen5b20c942016-06-02 17:19:45 -07001585 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
Srinivas Pandruvada65c12622016-07-23 15:12:06 -07001586 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1587 ICPU(INTEL_FAM6_SKYLAKE_X, core_params),
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001588 {}
1589};
1590
Dirk Brandewie93f08222013-02-06 09:02:13 -08001591static int intel_pstate_init_cpu(unsigned int cpunum)
1592{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001593 struct cpudata *cpu;
1594
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001595 cpu = all_cpu_data[cpunum];
1596
1597 if (!cpu) {
1598 unsigned int size = sizeof(struct cpudata);
1599
1600 if (per_cpu_limits)
1601 size += sizeof(struct perf_limits);
1602
1603 cpu = kzalloc(size, GFP_KERNEL);
1604 if (!cpu)
1605 return -ENOMEM;
1606
1607 all_cpu_data[cpunum] = cpu;
1608 if (per_cpu_limits)
1609 cpu->perf_limits = (struct perf_limits *)(cpu + 1);
1610
1611 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001612
1613 cpu = all_cpu_data[cpunum];
1614
Dirk Brandewie93f08222013-02-06 09:02:13 -08001615 cpu->cpu = cpunum;
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001616
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001617 if (hwp_active) {
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001618 intel_pstate_hwp_enable(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001619 pid_params.sample_rate_ms = 50;
1620 pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
1621 }
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001622
Vincent Minet179e8472014-07-05 01:51:33 +02001623 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001624
Dirk Brandewie93f08222013-02-06 09:02:13 -08001625 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001626
Joe Perches4836df12016-04-05 13:28:23 -07001627 pr_debug("controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001628
1629 return 0;
1630}
1631
1632static unsigned int intel_pstate_get(unsigned int cpu_num)
1633{
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001634 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -08001635
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001636 return cpu ? get_avg_frequency(cpu) : 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001637}
1638
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001639static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001640{
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001641 struct cpudata *cpu = all_cpu_data[cpu_num];
1642
Rafael J. Wysocki5ab666e2016-06-27 23:47:15 +02001643 if (cpu->update_util_set)
1644 return;
1645
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001646 /* Prevent intel_pstate_update_util() from using stale data. */
1647 cpu->sample.time = 0;
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001648 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
1649 intel_pstate_update_util);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001650 cpu->update_util_set = true;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001651}
1652
1653static void intel_pstate_clear_update_util_hook(unsigned int cpu)
1654{
Chen Yu4578ee7e2016-05-11 14:33:08 +08001655 struct cpudata *cpu_data = all_cpu_data[cpu];
1656
1657 if (!cpu_data->update_util_set)
1658 return;
1659
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001660 cpufreq_remove_update_util_hook(cpu);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001661 cpu_data->update_util_set = false;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001662 synchronize_sched();
1663}
1664
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001665static void intel_pstate_set_performance_limits(struct perf_limits *limits)
1666{
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001667 mutex_lock(&intel_pstate_limits_lock);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001668 limits->no_turbo = 0;
1669 limits->turbo_disabled = 0;
1670 limits->max_perf_pct = 100;
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001671 limits->max_perf = int_ext_tofp(1);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001672 limits->min_perf_pct = 100;
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001673 limits->min_perf = int_ext_tofp(1);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001674 limits->max_policy_pct = 100;
1675 limits->max_sysfs_pct = 100;
1676 limits->min_policy_pct = 0;
1677 limits->min_sysfs_pct = 0;
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001678 mutex_unlock(&intel_pstate_limits_lock);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001679}
1680
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001681static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
1682 struct perf_limits *limits)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001683{
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001684
1685 mutex_lock(&intel_pstate_limits_lock);
1686
Prarit Bhargava8478f532015-11-20 18:47:56 -05001687 limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
1688 policy->cpuinfo.max_freq);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001689 limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0, 100);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001690 if (policy->max == policy->min) {
1691 limits->min_policy_pct = limits->max_policy_pct;
1692 } else {
Srinivas Pandruvada46992d62016-11-21 16:33:19 -08001693 limits->min_policy_pct = DIV_ROUND_UP(policy->min * 100,
1694 policy->cpuinfo.max_freq);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001695 limits->min_policy_pct = clamp_t(int, limits->min_policy_pct,
1696 0, 100);
1697 }
Chen Yu43717aa2015-09-09 18:27:31 +08001698
1699 /* Normalize user input to [min_policy_pct, max_policy_pct] */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001700 limits->min_perf_pct = max(limits->min_policy_pct,
1701 limits->min_sysfs_pct);
1702 limits->min_perf_pct = min(limits->max_policy_pct,
1703 limits->min_perf_pct);
1704 limits->max_perf_pct = min(limits->max_policy_pct,
1705 limits->max_sysfs_pct);
1706 limits->max_perf_pct = max(limits->min_policy_pct,
1707 limits->max_perf_pct);
Chen Yu43717aa2015-09-09 18:27:31 +08001708
1709 /* Make sure min_perf_pct <= max_perf_pct */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001710 limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
Chen Yu43717aa2015-09-09 18:27:31 +08001711
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001712 limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
1713 limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
1714 limits->max_perf = round_up(limits->max_perf, EXT_FRAC_BITS);
1715 limits->min_perf = round_up(limits->min_perf, EXT_FRAC_BITS);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001716
Srinivas Pandruvadaa410c03d2016-10-28 10:44:52 -07001717 mutex_unlock(&intel_pstate_limits_lock);
1718
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001719 pr_debug("cpu:%d max_perf_pct:%d min_perf_pct:%d\n", policy->cpu,
1720 limits->max_perf_pct, limits->min_perf_pct);
1721}
1722
1723static int intel_pstate_set_policy(struct cpufreq_policy *policy)
1724{
1725 struct cpudata *cpu;
1726 struct perf_limits *perf_limits = NULL;
1727
1728 if (!policy->cpuinfo.max_freq)
1729 return -ENODEV;
1730
1731 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
1732 policy->cpuinfo.max_freq, policy->max);
1733
1734 cpu = all_cpu_data[policy->cpu];
1735 cpu->policy = policy->policy;
1736
1737 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
1738 policy->max < policy->cpuinfo.max_freq &&
1739 policy->max > cpu->pstate.max_pstate * cpu->pstate.scaling) {
1740 pr_debug("policy->max > max non turbo frequency\n");
1741 policy->max = policy->cpuinfo.max_freq;
1742 }
1743
1744 if (per_cpu_limits)
1745 perf_limits = cpu->perf_limits;
1746
1747 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
1748 if (!perf_limits) {
1749 limits = &performance_limits;
1750 perf_limits = limits;
1751 }
1752 if (policy->max >= policy->cpuinfo.max_freq) {
1753 pr_debug("set performance\n");
1754 intel_pstate_set_performance_limits(perf_limits);
1755 goto out;
1756 }
1757 } else {
1758 pr_debug("set powersave\n");
1759 if (!perf_limits) {
1760 limits = &powersave_limits;
1761 perf_limits = limits;
1762 }
1763
1764 }
1765
1766 intel_pstate_update_perf_limits(policy, perf_limits);
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001767 out:
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02001768 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001769 /*
1770 * NOHZ_FULL CPUs need this as the governor callback may not
1771 * be invoked on them.
1772 */
1773 intel_pstate_clear_update_util_hook(policy->cpu);
1774 intel_pstate_max_within_limits(cpu);
1775 }
1776
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001777 intel_pstate_set_update_util_hook(policy->cpu);
1778
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001779 intel_pstate_hwp_set_policy(policy);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001780
Dirk Brandewie93f08222013-02-06 09:02:13 -08001781 return 0;
1782}
1783
1784static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
1785{
Viresh Kumarbe49e342013-10-02 14:13:19 +05301786 cpufreq_verify_within_cpu_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001787
Stratos Karafotis285cb992014-07-18 08:37:21 -07001788 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -07001789 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001790 return -EINVAL;
1791
1792 return 0;
1793}
1794
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001795static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001796{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001797 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001798}
1799
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001800static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
1801{
1802 pr_debug("CPU %d exiting\n", policy->cpu);
1803
1804 intel_pstate_clear_update_util_hook(policy->cpu);
1805 if (!hwp_active)
1806 intel_cpufreq_stop_cpu(policy);
1807}
1808
1809static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
1810{
1811 intel_pstate_exit_perf_limits(policy);
1812
1813 policy->fast_switch_possible = false;
1814
1815 return 0;
1816}
1817
1818static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001819{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001820 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -07001821 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001822
1823 rc = intel_pstate_init_cpu(policy->cpu);
1824 if (rc)
1825 return rc;
1826
1827 cpu = all_cpu_data[policy->cpu];
1828
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001829 /*
1830 * We need sane value in the cpu->perf_limits, so inherit from global
1831 * perf_limits limits, which are seeded with values based on the
1832 * CONFIG_CPU_FREQ_DEFAULT_GOV_*, during boot up.
1833 */
1834 if (per_cpu_limits)
1835 memcpy(cpu->perf_limits, limits, sizeof(struct perf_limits));
1836
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001837 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
1838 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001839
1840 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001841 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07001842 update_turbo_state();
1843 policy->cpuinfo.max_freq = limits->turbo_disabled ?
1844 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1845 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
1846
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001847 intel_pstate_init_acpi_perf_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001848 cpumask_set_cpu(policy->cpu, policy->cpus);
1849
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001850 policy->fast_switch_possible = true;
1851
Dirk Brandewie93f08222013-02-06 09:02:13 -08001852 return 0;
1853}
1854
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001855static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001856{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001857 int ret = __intel_pstate_cpu_init(policy);
1858
1859 if (ret)
1860 return ret;
1861
1862 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
1863 if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
1864 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1865 else
1866 policy->policy = CPUFREQ_POLICY_POWERSAVE;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001867
1868 return 0;
1869}
1870
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001871static struct cpufreq_driver intel_pstate = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08001872 .flags = CPUFREQ_CONST_LOOPS,
1873 .verify = intel_pstate_verify_policy,
1874 .setpolicy = intel_pstate_set_policy,
Srinivas Pandruvada84428852016-11-24 16:07:10 -08001875 .resume = intel_pstate_resume,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001876 .get = intel_pstate_get,
1877 .init = intel_pstate_cpu_init,
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001878 .exit = intel_pstate_cpu_exit,
Dirk Brandewiebb180082014-03-19 08:45:54 -07001879 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001880 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -08001881};
1882
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001883static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
1884{
1885 struct cpudata *cpu = all_cpu_data[policy->cpu];
1886 struct perf_limits *perf_limits = limits;
1887
1888 update_turbo_state();
1889 policy->cpuinfo.max_freq = limits->turbo_disabled ?
1890 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
1891
1892 cpufreq_verify_within_cpu_limits(policy);
1893
1894 if (per_cpu_limits)
1895 perf_limits = cpu->perf_limits;
1896
1897 intel_pstate_update_perf_limits(policy, perf_limits);
1898
1899 return 0;
1900}
1901
1902static unsigned int intel_cpufreq_turbo_update(struct cpudata *cpu,
1903 struct cpufreq_policy *policy,
1904 unsigned int target_freq)
1905{
1906 unsigned int max_freq;
1907
1908 update_turbo_state();
1909
1910 max_freq = limits->no_turbo || limits->turbo_disabled ?
1911 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
1912 policy->cpuinfo.max_freq = max_freq;
1913 if (policy->max > max_freq)
1914 policy->max = max_freq;
1915
1916 if (target_freq > max_freq)
1917 target_freq = max_freq;
1918
1919 return target_freq;
1920}
1921
1922static int intel_cpufreq_target(struct cpufreq_policy *policy,
1923 unsigned int target_freq,
1924 unsigned int relation)
1925{
1926 struct cpudata *cpu = all_cpu_data[policy->cpu];
1927 struct cpufreq_freqs freqs;
1928 int target_pstate;
1929
1930 freqs.old = policy->cur;
1931 freqs.new = intel_cpufreq_turbo_update(cpu, policy, target_freq);
1932
1933 cpufreq_freq_transition_begin(policy, &freqs);
1934 switch (relation) {
1935 case CPUFREQ_RELATION_L:
1936 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
1937 break;
1938 case CPUFREQ_RELATION_H:
1939 target_pstate = freqs.new / cpu->pstate.scaling;
1940 break;
1941 default:
1942 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
1943 break;
1944 }
1945 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1946 if (target_pstate != cpu->pstate.current_pstate) {
1947 cpu->pstate.current_pstate = target_pstate;
1948 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
1949 pstate_funcs.get_val(cpu, target_pstate));
1950 }
1951 cpufreq_freq_transition_end(policy, &freqs, false);
1952
1953 return 0;
1954}
1955
1956static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
1957 unsigned int target_freq)
1958{
1959 struct cpudata *cpu = all_cpu_data[policy->cpu];
1960 int target_pstate;
1961
1962 target_freq = intel_cpufreq_turbo_update(cpu, policy, target_freq);
1963 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
1964 intel_pstate_update_pstate(cpu, target_pstate);
1965 return target_freq;
1966}
1967
1968static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
1969{
1970 int ret = __intel_pstate_cpu_init(policy);
1971
1972 if (ret)
1973 return ret;
1974
1975 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
1976 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
1977 policy->cur = policy->cpuinfo.min_freq;
1978
1979 return 0;
1980}
1981
1982static struct cpufreq_driver intel_cpufreq = {
1983 .flags = CPUFREQ_CONST_LOOPS,
1984 .verify = intel_cpufreq_verify_policy,
1985 .target = intel_cpufreq_target,
1986 .fast_switch = intel_cpufreq_fast_switch,
1987 .init = intel_cpufreq_cpu_init,
1988 .exit = intel_pstate_cpu_exit,
1989 .stop_cpu = intel_cpufreq_stop_cpu,
1990 .name = "intel_cpufreq",
1991};
1992
1993static struct cpufreq_driver *intel_pstate_driver = &intel_pstate;
1994
Jisheng Zhangeed43602016-06-27 18:07:16 +08001995static int no_load __initdata;
1996static int no_hwp __initdata;
1997static int hwp_only __initdata;
Jisheng Zhang29327c82016-06-27 18:07:17 +08001998static unsigned int force_load __initdata;
Dirk Brandewie6be26492013-02-15 22:55:10 +01001999
Jisheng Zhang29327c82016-06-27 18:07:17 +08002000static int __init intel_pstate_msrs_not_valid(void)
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002001{
Dirk Brandewie016c8152013-10-21 09:20:34 -07002002 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -07002003 !pstate_funcs.get_min() ||
2004 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002005 return -ENODEV;
2006
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002007 return 0;
2008}
Dirk Brandewie016c8152013-10-21 09:20:34 -07002009
Jisheng Zhang29327c82016-06-27 18:07:17 +08002010static void __init copy_pid_params(struct pstate_adjust_policy *policy)
Dirk Brandewie016c8152013-10-21 09:20:34 -07002011{
2012 pid_params.sample_rate_ms = policy->sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01002013 pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002014 pid_params.p_gain_pct = policy->p_gain_pct;
2015 pid_params.i_gain_pct = policy->i_gain_pct;
2016 pid_params.d_gain_pct = policy->d_gain_pct;
2017 pid_params.deadband = policy->deadband;
2018 pid_params.setpoint = policy->setpoint;
2019}
2020
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08002021#ifdef CONFIG_ACPI
2022static void intel_pstate_use_acpi_profile(void)
2023{
2024 if (acpi_gbl_FADT.preferred_profile == PM_MOBILE)
2025 pstate_funcs.get_target_pstate =
2026 get_target_pstate_use_cpu_load;
2027}
2028#else
2029static void intel_pstate_use_acpi_profile(void)
2030{
2031}
2032#endif
2033
Jisheng Zhang29327c82016-06-27 18:07:17 +08002034static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -07002035{
2036 pstate_funcs.get_max = funcs->get_max;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07002037 pstate_funcs.get_max_physical = funcs->get_max_physical;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002038 pstate_funcs.get_min = funcs->get_min;
2039 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002040 pstate_funcs.get_scaling = funcs->get_scaling;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01002041 pstate_funcs.get_val = funcs->get_val;
Dirk Brandewie007bea02013-12-18 10:32:39 -08002042 pstate_funcs.get_vid = funcs->get_vid;
Philippe Longepe157386b2015-12-04 17:40:30 +01002043 pstate_funcs.get_target_pstate = funcs->get_target_pstate;
2044
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08002045 intel_pstate_use_acpi_profile();
Dirk Brandewie016c8152013-10-21 09:20:34 -07002046}
2047
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002048#ifdef CONFIG_ACPI
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002049
Jisheng Zhang29327c82016-06-27 18:07:17 +08002050static bool __init intel_pstate_no_acpi_pss(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002051{
2052 int i;
2053
2054 for_each_possible_cpu(i) {
2055 acpi_status status;
2056 union acpi_object *pss;
2057 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2058 struct acpi_processor *pr = per_cpu(processors, i);
2059
2060 if (!pr)
2061 continue;
2062
2063 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2064 if (ACPI_FAILURE(status))
2065 continue;
2066
2067 pss = buffer.pointer;
2068 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2069 kfree(pss);
2070 return false;
2071 }
2072
2073 kfree(pss);
2074 }
2075
2076 return true;
2077}
2078
Jisheng Zhang29327c82016-06-27 18:07:17 +08002079static bool __init intel_pstate_has_acpi_ppc(void)
ethan zhao966916e2014-12-01 11:32:08 +09002080{
2081 int i;
2082
2083 for_each_possible_cpu(i) {
2084 struct acpi_processor *pr = per_cpu(processors, i);
2085
2086 if (!pr)
2087 continue;
2088 if (acpi_has_method(pr->handle, "_PPC"))
2089 return true;
2090 }
2091 return false;
2092}
2093
2094enum {
2095 PSS,
2096 PPC,
2097};
2098
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002099struct hw_vendor_info {
2100 u16 valid;
2101 char oem_id[ACPI_OEM_ID_SIZE];
2102 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
ethan zhao966916e2014-12-01 11:32:08 +09002103 int oem_pwr_table;
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002104};
2105
2106/* Hardware vendor-specific info that has its own power management modes */
Jisheng Zhang29327c82016-06-27 18:07:17 +08002107static struct hw_vendor_info vendor_info[] __initdata = {
ethan zhao966916e2014-12-01 11:32:08 +09002108 {1, "HP ", "ProLiant", PSS},
2109 {1, "ORACLE", "X4-2 ", PPC},
2110 {1, "ORACLE", "X4-2L ", PPC},
2111 {1, "ORACLE", "X4-2B ", PPC},
2112 {1, "ORACLE", "X3-2 ", PPC},
2113 {1, "ORACLE", "X3-2L ", PPC},
2114 {1, "ORACLE", "X3-2B ", PPC},
2115 {1, "ORACLE", "X4470M2 ", PPC},
2116 {1, "ORACLE", "X4270M3 ", PPC},
2117 {1, "ORACLE", "X4270M2 ", PPC},
2118 {1, "ORACLE", "X4170M2 ", PPC},
Ethan Zhao5aecc3c2015-08-05 09:28:50 +09002119 {1, "ORACLE", "X4170 M3", PPC},
2120 {1, "ORACLE", "X4275 M3", PPC},
2121 {1, "ORACLE", "X6-2 ", PPC},
2122 {1, "ORACLE", "Sudbury ", PPC},
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002123 {0, "", ""},
2124};
2125
Jisheng Zhang29327c82016-06-27 18:07:17 +08002126static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002127{
2128 struct acpi_table_header hdr;
2129 struct hw_vendor_info *v_info;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002130 const struct x86_cpu_id *id;
2131 u64 misc_pwr;
2132
2133 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2134 if (id) {
2135 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2136 if ( misc_pwr & (1 << 8))
2137 return true;
2138 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002139
Stratos Karafotisc4108332014-07-18 08:37:23 -07002140 if (acpi_disabled ||
2141 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002142 return false;
2143
2144 for (v_info = vendor_info; v_info->valid; v_info++) {
Stratos Karafotisc4108332014-07-18 08:37:23 -07002145 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
ethan zhao966916e2014-12-01 11:32:08 +09002146 !strncmp(hdr.oem_table_id, v_info->oem_table_id,
2147 ACPI_OEM_TABLE_ID_SIZE))
2148 switch (v_info->oem_pwr_table) {
2149 case PSS:
2150 return intel_pstate_no_acpi_pss();
2151 case PPC:
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002152 return intel_pstate_has_acpi_ppc() &&
2153 (!force_load);
ethan zhao966916e2014-12-01 11:32:08 +09002154 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002155 }
2156
2157 return false;
2158}
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002159
2160static void intel_pstate_request_control_from_smm(void)
2161{
2162 /*
2163 * It may be unsafe to request P-states control from SMM if _PPC support
2164 * has not been enabled.
2165 */
2166 if (acpi_ppc)
2167 acpi_processor_pstate_control();
2168}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002169#else /* CONFIG_ACPI not enabled */
2170static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
ethan zhao966916e2014-12-01 11:32:08 +09002171static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002172static inline void intel_pstate_request_control_from_smm(void) {}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002173#endif /* CONFIG_ACPI */
2174
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002175static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2176 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
2177 {}
2178};
2179
Dirk Brandewie93f08222013-02-06 09:02:13 -08002180static int __init intel_pstate_init(void)
2181{
Dirk Brandewie907cc902013-03-05 14:15:27 -08002182 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002183 const struct x86_cpu_id *id;
Borislav Petkov64df1fd2015-04-03 15:19:53 +02002184 struct cpu_defaults *cpu_def;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002185
Dirk Brandewie6be26492013-02-15 22:55:10 +01002186 if (no_load)
2187 return -ENODEV;
2188
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002189 if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
2190 copy_cpu_funcs(&core_params.funcs);
2191 hwp_active++;
2192 goto hwp_cpu_matched;
2193 }
2194
Dirk Brandewie93f08222013-02-06 09:02:13 -08002195 id = x86_match_cpu(intel_pstate_cpu_ids);
2196 if (!id)
2197 return -ENODEV;
2198
Borislav Petkov64df1fd2015-04-03 15:19:53 +02002199 cpu_def = (struct cpu_defaults *)id->driver_data;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002200
Borislav Petkov64df1fd2015-04-03 15:19:53 +02002201 copy_pid_params(&cpu_def->pid_policy);
2202 copy_cpu_funcs(&cpu_def->funcs);
Dirk Brandewie016c8152013-10-21 09:20:34 -07002203
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002204 if (intel_pstate_msrs_not_valid())
2205 return -ENODEV;
2206
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002207hwp_cpu_matched:
2208 /*
2209 * The Intel pstate driver will be ignored if the platform
2210 * firmware has its own power management modes.
2211 */
2212 if (intel_pstate_platform_pwr_mgmt_exists())
2213 return -ENODEV;
2214
Joe Perches4836df12016-04-05 13:28:23 -07002215 pr_info("Intel P-state driver initializing\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08002216
Wei Yongjunb57ffac2013-05-13 08:03:43 +00002217 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08002218 if (!all_cpu_data)
2219 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002220
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002221 if (!hwp_active && hwp_only)
2222 goto out;
2223
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002224 intel_pstate_request_control_from_smm();
2225
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002226 rc = cpufreq_register_driver(intel_pstate_driver);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002227 if (rc)
2228 goto out;
2229
2230 intel_pstate_debug_expose_params();
2231 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08002232
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002233 if (hwp_active)
Joe Perches4836df12016-04-05 13:28:23 -07002234 pr_info("HWP enabled\n");
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002235
Dirk Brandewie93f08222013-02-06 09:02:13 -08002236 return rc;
2237out:
Dirk Brandewie907cc902013-03-05 14:15:27 -08002238 get_online_cpus();
2239 for_each_online_cpu(cpu) {
2240 if (all_cpu_data[cpu]) {
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002241 if (intel_pstate_driver == &intel_pstate)
2242 intel_pstate_clear_update_util_hook(cpu);
2243
Dirk Brandewie907cc902013-03-05 14:15:27 -08002244 kfree(all_cpu_data[cpu]);
2245 }
2246 }
2247
2248 put_online_cpus();
2249 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002250 return -ENODEV;
2251}
2252device_initcall(intel_pstate_init);
2253
Dirk Brandewie6be26492013-02-15 22:55:10 +01002254static int __init intel_pstate_setup(char *str)
2255{
2256 if (!str)
2257 return -EINVAL;
2258
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002259 if (!strcmp(str, "disable")) {
Dirk Brandewie6be26492013-02-15 22:55:10 +01002260 no_load = 1;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002261 } else if (!strcmp(str, "passive")) {
2262 pr_info("Passive mode enabled\n");
2263 intel_pstate_driver = &intel_cpufreq;
2264 no_hwp = 1;
2265 }
Prarit Bhargava539342f2015-10-22 09:43:31 -04002266 if (!strcmp(str, "no_hwp")) {
Joe Perches4836df12016-04-05 13:28:23 -07002267 pr_info("HWP disabled\n");
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002268 no_hwp = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04002269 }
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002270 if (!strcmp(str, "force"))
2271 force_load = 1;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002272 if (!strcmp(str, "hwp_only"))
2273 hwp_only = 1;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002274 if (!strcmp(str, "per_cpu_perf_limits"))
2275 per_cpu_limits = true;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002276
2277#ifdef CONFIG_ACPI
2278 if (!strcmp(str, "support_acpi_ppc"))
2279 acpi_ppc = true;
2280#endif
2281
Dirk Brandewie6be26492013-02-15 22:55:10 +01002282 return 0;
2283}
2284early_param("intel_pstate", intel_pstate_setup);
2285
Dirk Brandewie93f08222013-02-06 09:02:13 -08002286MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2287MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2288MODULE_LICENSE("GPL");