blob: 7159dbde016063be2d1dac67c950d76cd38311a1 [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 Pandruvada13ad7702016-04-03 13:06:46 -0700252 *
253 * This structure stores per CPU instance data for all CPUs.
254 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800255struct cpudata {
256 int cpu;
257
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200258 unsigned int policy;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100259 struct update_util_data update_util;
Chen Yu4578ee7e2016-05-11 14:33:08 +0800260 bool update_util_set;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800261
Dirk Brandewie93f08222013-02-06 09:02:13 -0800262 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800263 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800264 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800265
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200266 u64 last_update;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100267 u64 last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800268 u64 prev_aperf;
269 u64 prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700270 u64 prev_tsc;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100271 u64 prev_cummulative_iowait;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800272 struct sample sample;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700273 struct perf_limits *perf_limits;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700274#ifdef CONFIG_ACPI
275 struct acpi_processor_performance acpi_perf_data;
276 bool valid_pss_table;
277#endif
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200278 unsigned int iowait_boost;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800279};
280
281static struct cpudata **all_cpu_data;
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700282
283/**
Rafael J. Wysocki39545172016-10-11 23:07:38 +0200284 * struct pstate_adjust_policy - Stores static PID configuration data
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700285 * @sample_rate_ms: PID calculation sample rate in ms
286 * @sample_rate_ns: Sample rate calculation in ns
287 * @deadband: PID deadband
288 * @setpoint: PID Setpoint
289 * @p_gain_pct: PID proportional gain
290 * @i_gain_pct: PID integral gain
291 * @d_gain_pct: PID derivative gain
292 *
293 * Stores per CPU model static PID configuration data.
294 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800295struct pstate_adjust_policy {
296 int sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100297 s64 sample_rate_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800298 int deadband;
299 int setpoint;
300 int p_gain_pct;
301 int d_gain_pct;
302 int i_gain_pct;
303};
304
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700305/**
306 * struct pstate_funcs - Per CPU model specific callbacks
307 * @get_max: Callback to get maximum non turbo effective P state
308 * @get_max_physical: Callback to get maximum non turbo physical P state
309 * @get_min: Callback to get minimum P state
310 * @get_turbo: Callback to get turbo P state
311 * @get_scaling: Callback to get frequency scaling factor
312 * @get_val: Callback to convert P state to actual MSR write value
313 * @get_vid: Callback to get VID data for Atom platforms
314 * @get_target_pstate: Callback to a function to calculate next P state to use
315 *
316 * Core and Atom CPU models have different way to get P State limits. This
317 * structure is used to store those callbacks.
318 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700319struct pstate_funcs {
320 int (*get_max)(void);
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700321 int (*get_max_physical)(void);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700322 int (*get_min)(void);
323 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700324 int (*get_scaling)(void);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100325 u64 (*get_val)(struct cpudata*, int pstate);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800326 void (*get_vid)(struct cpudata *);
Philippe Longepe157386b2015-12-04 17:40:30 +0100327 int32_t (*get_target_pstate)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800328};
329
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700330/**
331 * struct cpu_defaults- Per CPU model default config data
332 * @pid_policy: PID config data
333 * @funcs: Callback function data
334 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700335struct cpu_defaults {
336 struct pstate_adjust_policy pid_policy;
337 struct pstate_funcs funcs;
338};
339
Philippe Longepe157386b2015-12-04 17:40:30 +0100340static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
Philippe Longepee70eed22015-12-04 17:40:32 +0100341static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
Philippe Longepe157386b2015-12-04 17:40:30 +0100342
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800343static struct pstate_adjust_policy pid_params __read_mostly;
344static struct pstate_funcs pstate_funcs __read_mostly;
345static int hwp_active __read_mostly;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700346static bool per_cpu_limits __read_mostly;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700347
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700348#ifdef CONFIG_ACPI
349static bool acpi_ppc;
350#endif
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700351
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400352static struct perf_limits performance_limits = {
353 .no_turbo = 0,
354 .turbo_disabled = 0,
355 .max_perf_pct = 100,
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800356 .max_perf = int_ext_tofp(1),
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400357 .min_perf_pct = 100,
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800358 .min_perf = int_ext_tofp(1),
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400359 .max_policy_pct = 100,
360 .max_sysfs_pct = 100,
361 .min_policy_pct = 0,
362 .min_sysfs_pct = 0,
363};
364
365static struct perf_limits powersave_limits = {
Dirk Brandewie93f08222013-02-06 09:02:13 -0800366 .no_turbo = 0,
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700367 .turbo_disabled = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800368 .max_perf_pct = 100,
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800369 .max_perf = int_ext_tofp(1),
Dirk Brandewie93f08222013-02-06 09:02:13 -0800370 .min_perf_pct = 0,
371 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700372 .max_policy_pct = 100,
373 .max_sysfs_pct = 100,
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800374 .min_policy_pct = 0,
375 .min_sysfs_pct = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800376};
377
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400378#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
379static struct perf_limits *limits = &performance_limits;
380#else
381static struct perf_limits *limits = &powersave_limits;
382#endif
383
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -0700384static DEFINE_MUTEX(intel_pstate_limits_lock);
385
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700386#ifdef CONFIG_ACPI
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700387
388static bool intel_pstate_get_ppc_enable_status(void)
389{
390 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
391 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
392 return true;
393
394 return acpi_ppc;
395}
396
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700397static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
398{
399 struct cpudata *cpu;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700400 int ret;
401 int i;
402
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700403 if (hwp_active)
404 return;
405
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700406 if (!intel_pstate_get_ppc_enable_status())
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700407 return;
408
409 cpu = all_cpu_data[policy->cpu];
410
411 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
412 policy->cpu);
413 if (ret)
414 return;
415
416 /*
417 * Check if the control value in _PSS is for PERF_CTL MSR, which should
418 * guarantee that the states returned by it map to the states in our
419 * list directly.
420 */
421 if (cpu->acpi_perf_data.control_register.space_id !=
422 ACPI_ADR_SPACE_FIXED_HARDWARE)
423 goto err;
424
425 /*
426 * If there is only one entry _PSS, simply ignore _PSS and continue as
427 * usual without taking _PSS into account
428 */
429 if (cpu->acpi_perf_data.state_count < 2)
430 goto err;
431
432 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
433 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
434 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
435 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
436 (u32) cpu->acpi_perf_data.states[i].core_frequency,
437 (u32) cpu->acpi_perf_data.states[i].power,
438 (u32) cpu->acpi_perf_data.states[i].control);
439 }
440
441 /*
442 * The _PSS table doesn't contain whole turbo frequency range.
443 * This just contains +1 MHZ above the max non turbo frequency,
444 * with control value corresponding to max turbo ratio. But
445 * when cpufreq set policy is called, it will call with this
446 * max frequency, which will cause a reduced performance as
447 * this driver uses real max turbo frequency as the max
448 * frequency. So correct this frequency in _PSS table to
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700449 * correct max turbo frequency based on the turbo state.
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700450 * Also need to convert to MHz as _PSS freq is in MHz.
451 */
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700452 if (!limits->turbo_disabled)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700453 cpu->acpi_perf_data.states[0].core_frequency =
454 policy->cpuinfo.max_freq / 1000;
455 cpu->valid_pss_table = true;
Srinivas Pandruvada6cacd112016-05-29 23:31:23 -0700456 pr_debug("_PPC limits will be enforced\n");
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700457
458 return;
459
460 err:
461 cpu->valid_pss_table = false;
462 acpi_processor_unregister_performance(policy->cpu);
463}
464
465static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
466{
467 struct cpudata *cpu;
468
469 cpu = all_cpu_data[policy->cpu];
470 if (!cpu->valid_pss_table)
471 return;
472
473 acpi_processor_unregister_performance(policy->cpu);
474}
475
476#else
477static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
478{
479}
480
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +0100481static inline int intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700482{
483}
484#endif
485
Dirk Brandewie93f08222013-02-06 09:02:13 -0800486static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700487 int deadband, int integral) {
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100488 pid->setpoint = int_tofp(setpoint);
489 pid->deadband = int_tofp(deadband);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800490 pid->integral = int_tofp(integral);
Dirk Brandewied98d0992014-02-12 10:01:05 -0800491 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800492}
493
494static inline void pid_p_gain_set(struct _pid *pid, int percent)
495{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200496 pid->p_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800497}
498
499static inline void pid_i_gain_set(struct _pid *pid, int percent)
500{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200501 pid->i_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800502}
503
504static inline void pid_d_gain_set(struct _pid *pid, int percent)
505{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200506 pid->d_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800507}
508
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700509static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800510{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700511 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800512 int32_t pterm, dterm, fp_error;
513 int32_t integral_limit;
514
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100515 fp_error = pid->setpoint - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800516
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100517 if (abs(fp_error) <= pid->deadband)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800518 return 0;
519
520 pterm = mul_fp(pid->p_gain, fp_error);
521
522 pid->integral += fp_error;
523
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800524 /*
525 * We limit the integral here so that it will never
526 * get higher than 30. This prevents it from becoming
527 * too large an input over long periods of time and allows
528 * it to get factored out sooner.
529 *
530 * The value of 30 was chosen through experimentation.
531 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800532 integral_limit = int_tofp(30);
533 if (pid->integral > integral_limit)
534 pid->integral = integral_limit;
535 if (pid->integral < -integral_limit)
536 pid->integral = -integral_limit;
537
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700538 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
539 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800540
541 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
Doug Smythies51d211e2014-06-17 13:36:10 -0700542 result = result + (1 << (FRAC_BITS-1));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800543 return (signed int)fp_toint(result);
544}
545
546static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
547{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700548 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
549 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
550 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800551
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700552 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800553}
554
Dirk Brandewie93f08222013-02-06 09:02:13 -0800555static inline void intel_pstate_reset_all_pid(void)
556{
557 unsigned int cpu;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700558
Dirk Brandewie93f08222013-02-06 09:02:13 -0800559 for_each_online_cpu(cpu) {
560 if (all_cpu_data[cpu])
561 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
562 }
563}
564
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700565static inline void update_turbo_state(void)
566{
567 u64 misc_en;
568 struct cpudata *cpu;
569
570 cpu = all_cpu_data[0];
571 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400572 limits->turbo_disabled =
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700573 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
574 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
575}
576
Viresh Kumar41cfd642016-02-22 10:27:46 +0530577static void intel_pstate_hwp_set(const struct cpumask *cpumask)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800578{
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700579 int min, hw_min, max, hw_max, cpu, range, adj_range;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700580 struct perf_limits *perf_limits = limits;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700581 u64 value, cap;
582
Viresh Kumar41cfd642016-02-22 10:27:46 +0530583 for_each_cpu(cpu, cpumask) {
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700584 int max_perf_pct, min_perf_pct;
585
586 if (per_cpu_limits)
587 perf_limits = all_cpu_data[cpu]->perf_limits;
588
Srinivas Pandruvadaf9f48722016-10-08 12:42:38 -0700589 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
590 hw_min = HWP_LOWEST_PERF(cap);
591 hw_max = HWP_HIGHEST_PERF(cap);
592 range = hw_max - hw_min;
593
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700594 max_perf_pct = perf_limits->max_perf_pct;
595 min_perf_pct = perf_limits->min_perf_pct;
596
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800597 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700598 adj_range = min_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700599 min = hw_min + adj_range;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800600 value &= ~HWP_MIN_PERF(~0L);
601 value |= HWP_MIN_PERF(min);
602
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700603 adj_range = max_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700604 max = hw_min + adj_range;
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400605 if (limits->no_turbo) {
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700606 hw_max = HWP_GUARANTEED_PERF(cap);
607 if (hw_max < max)
608 max = hw_max;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800609 }
610
611 value &= ~HWP_MAX_PERF(~0L);
612 value |= HWP_MAX_PERF(max);
613 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
614 }
Viresh Kumar41cfd642016-02-22 10:27:46 +0530615}
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800616
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +0200617static int intel_pstate_hwp_set_policy(struct cpufreq_policy *policy)
618{
619 if (hwp_active)
620 intel_pstate_hwp_set(policy->cpus);
621
622 return 0;
623}
624
Viresh Kumar41cfd642016-02-22 10:27:46 +0530625static void intel_pstate_hwp_set_online_cpus(void)
626{
627 get_online_cpus();
628 intel_pstate_hwp_set(cpu_online_mask);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800629 put_online_cpus();
630}
631
Dirk Brandewie93f08222013-02-06 09:02:13 -0800632/************************** debugfs begin ************************/
633static int pid_param_set(void *data, u64 val)
634{
635 *(u32 *)data = val;
636 intel_pstate_reset_all_pid();
637 return 0;
638}
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700639
Dirk Brandewie93f08222013-02-06 09:02:13 -0800640static int pid_param_get(void *data, u64 *val)
641{
642 *val = *(u32 *)data;
643 return 0;
644}
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700645DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -0800646
647struct pid_param {
648 char *name;
649 void *value;
650};
651
652static struct pid_param pid_files[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700653 {"sample_rate_ms", &pid_params.sample_rate_ms},
654 {"d_gain_pct", &pid_params.d_gain_pct},
655 {"i_gain_pct", &pid_params.i_gain_pct},
656 {"deadband", &pid_params.deadband},
657 {"setpoint", &pid_params.setpoint},
658 {"p_gain_pct", &pid_params.p_gain_pct},
Dirk Brandewie93f08222013-02-06 09:02:13 -0800659 {NULL, NULL}
660};
661
Stratos Karafotis317dd502014-07-18 08:37:17 -0700662static void __init intel_pstate_debug_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800663{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700664 struct dentry *debugfs_parent;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800665 int i = 0;
666
Srinivas Pandruvada185d8242016-10-21 09:38:20 -0700667 if (hwp_active ||
668 pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800669 return;
Srinivas Pandruvada185d8242016-10-21 09:38:20 -0700670
Dirk Brandewie93f08222013-02-06 09:02:13 -0800671 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
672 if (IS_ERR_OR_NULL(debugfs_parent))
673 return;
674 while (pid_files[i].name) {
675 debugfs_create_file(pid_files[i].name, 0660,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700676 debugfs_parent, pid_files[i].value,
677 &fops_pid_param);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800678 i++;
679 }
680}
681
682/************************** debugfs end ************************/
683
684/************************** sysfs begin ************************/
685#define show_one(file_name, object) \
686 static ssize_t show_##file_name \
687 (struct kobject *kobj, struct attribute *attr, char *buf) \
688 { \
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400689 return sprintf(buf, "%u\n", limits->object); \
Dirk Brandewie93f08222013-02-06 09:02:13 -0800690 }
691
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800692static ssize_t show_turbo_pct(struct kobject *kobj,
693 struct attribute *attr, char *buf)
694{
695 struct cpudata *cpu;
696 int total, no_turbo, turbo_pct;
697 uint32_t turbo_fp;
698
699 cpu = all_cpu_data[0];
700
701 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
702 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200703 turbo_fp = div_fp(no_turbo, total);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800704 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
705 return sprintf(buf, "%u\n", turbo_pct);
706}
707
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800708static ssize_t show_num_pstates(struct kobject *kobj,
709 struct attribute *attr, char *buf)
710{
711 struct cpudata *cpu;
712 int total;
713
714 cpu = all_cpu_data[0];
715 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
716 return sprintf(buf, "%u\n", total);
717}
718
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700719static ssize_t show_no_turbo(struct kobject *kobj,
720 struct attribute *attr, char *buf)
721{
722 ssize_t ret;
723
724 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400725 if (limits->turbo_disabled)
726 ret = sprintf(buf, "%u\n", limits->turbo_disabled);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700727 else
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400728 ret = sprintf(buf, "%u\n", limits->no_turbo);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700729
730 return ret;
731}
732
Dirk Brandewie93f08222013-02-06 09:02:13 -0800733static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700734 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800735{
736 unsigned int input;
737 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700738
Dirk Brandewie93f08222013-02-06 09:02:13 -0800739 ret = sscanf(buf, "%u", &input);
740 if (ret != 1)
741 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700742
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -0700743 mutex_lock(&intel_pstate_limits_lock);
744
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700745 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400746 if (limits->turbo_disabled) {
Joe Perches4836df12016-04-05 13:28:23 -0700747 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -0700748 mutex_unlock(&intel_pstate_limits_lock);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700749 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700750 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800751
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400752 limits->no_turbo = clamp_t(int, input, 0, 1);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700753
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -0700754 mutex_unlock(&intel_pstate_limits_lock);
755
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800756 if (hwp_active)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530757 intel_pstate_hwp_set_online_cpus();
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800758
Dirk Brandewie93f08222013-02-06 09:02:13 -0800759 return count;
760}
761
762static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700763 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800764{
765 unsigned int input;
766 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700767
Dirk Brandewie93f08222013-02-06 09:02:13 -0800768 ret = sscanf(buf, "%u", &input);
769 if (ret != 1)
770 return -EINVAL;
771
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -0700772 mutex_lock(&intel_pstate_limits_lock);
773
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400774 limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
775 limits->max_perf_pct = min(limits->max_policy_pct,
776 limits->max_sysfs_pct);
777 limits->max_perf_pct = max(limits->min_policy_pct,
778 limits->max_perf_pct);
779 limits->max_perf_pct = max(limits->min_perf_pct,
780 limits->max_perf_pct);
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800781 limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700782
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -0700783 mutex_unlock(&intel_pstate_limits_lock);
784
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800785 if (hwp_active)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530786 intel_pstate_hwp_set_online_cpus();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800787 return count;
788}
789
790static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700791 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800792{
793 unsigned int input;
794 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700795
Dirk Brandewie93f08222013-02-06 09:02:13 -0800796 ret = sscanf(buf, "%u", &input);
797 if (ret != 1)
798 return -EINVAL;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800799
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -0700800 mutex_lock(&intel_pstate_limits_lock);
801
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400802 limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
803 limits->min_perf_pct = max(limits->min_policy_pct,
804 limits->min_sysfs_pct);
805 limits->min_perf_pct = min(limits->max_policy_pct,
806 limits->min_perf_pct);
807 limits->min_perf_pct = min(limits->max_perf_pct,
808 limits->min_perf_pct);
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -0800809 limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800810
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -0700811 mutex_unlock(&intel_pstate_limits_lock);
812
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800813 if (hwp_active)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530814 intel_pstate_hwp_set_online_cpus();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800815 return count;
816}
817
Dirk Brandewie93f08222013-02-06 09:02:13 -0800818show_one(max_perf_pct, max_perf_pct);
819show_one(min_perf_pct, min_perf_pct);
820
821define_one_global_rw(no_turbo);
822define_one_global_rw(max_perf_pct);
823define_one_global_rw(min_perf_pct);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800824define_one_global_ro(turbo_pct);
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800825define_one_global_ro(num_pstates);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800826
827static struct attribute *intel_pstate_attributes[] = {
828 &no_turbo.attr,
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800829 &turbo_pct.attr,
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800830 &num_pstates.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800831 NULL
832};
833
834static struct attribute_group intel_pstate_attr_group = {
835 .attrs = intel_pstate_attributes,
836};
Dirk Brandewie93f08222013-02-06 09:02:13 -0800837
Stratos Karafotis317dd502014-07-18 08:37:17 -0700838static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800839{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700840 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800841 int rc;
842
843 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
844 &cpu_subsys.dev_root->kobj);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700845 if (WARN_ON(!intel_pstate_kobject))
846 return;
847
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700848 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700849 if (WARN_ON(rc))
850 return;
851
852 /*
853 * If per cpu limits are enforced there are no global limits, so
854 * return without creating max/min_perf_pct attributes
855 */
856 if (per_cpu_limits)
857 return;
858
859 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
860 WARN_ON(rc);
861
862 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
863 WARN_ON(rc);
864
Dirk Brandewie93f08222013-02-06 09:02:13 -0800865}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800866/************************** sysfs end ************************/
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800867
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -0700868static void intel_pstate_hwp_enable(struct cpudata *cpudata)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800869{
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -0800870 /* First disable HWP notification interrupt as we don't process them */
Srinivas Pandruvadada7de912016-07-19 16:52:01 -0700871 if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
872 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -0800873
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -0700874 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800875}
876
Philippe Longepe938d21a2015-11-09 17:40:46 -0800877static int atom_get_min_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700878{
879 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700880
Philippe Longepe938d21a2015-11-09 17:40:46 -0800881 rdmsrl(ATOM_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700882 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700883}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800884
Philippe Longepe938d21a2015-11-09 17:40:46 -0800885static int atom_get_max_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700886{
887 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700888
Philippe Longepe938d21a2015-11-09 17:40:46 -0800889 rdmsrl(ATOM_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700890 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700891}
892
Philippe Longepe938d21a2015-11-09 17:40:46 -0800893static int atom_get_turbo_pstate(void)
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800894{
895 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700896
Philippe Longepe938d21a2015-11-09 17:40:46 -0800897 rdmsrl(ATOM_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700898 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800899}
900
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100901static u64 atom_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800902{
903 u64 val;
904 int32_t vid_fp;
905 u32 vid;
906
Chen Yu144c8e12015-07-29 23:53:10 +0800907 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400908 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800909 val |= (u64)1 << 32;
910
911 vid_fp = cpudata->vid.min + mul_fp(
912 int_tofp(pstate - cpudata->pstate.min_pstate),
913 cpudata->vid.ratio);
914
915 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
Dirk Brandewied022a652014-10-13 08:37:44 -0700916 vid = ceiling_fp(vid_fp);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800917
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700918 if (pstate > cpudata->pstate.max_pstate)
919 vid = cpudata->vid.turbo;
920
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100921 return val | vid;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800922}
923
Philippe Longepe1421df62015-11-09 17:40:47 -0800924static int silvermont_get_scaling(void)
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700925{
926 u64 value;
927 int i;
Philippe Longepe1421df62015-11-09 17:40:47 -0800928 /* Defined in Table 35-6 from SDM (Sept 2015) */
929 static int silvermont_freq_table[] = {
930 83300, 100000, 133300, 116700, 80000};
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700931
932 rdmsrl(MSR_FSB_FREQ, value);
Philippe Longepe1421df62015-11-09 17:40:47 -0800933 i = value & 0x7;
934 WARN_ON(i > 4);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700935
Philippe Longepe1421df62015-11-09 17:40:47 -0800936 return silvermont_freq_table[i];
937}
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700938
Philippe Longepe1421df62015-11-09 17:40:47 -0800939static int airmont_get_scaling(void)
940{
941 u64 value;
942 int i;
943 /* Defined in Table 35-10 from SDM (Sept 2015) */
944 static int airmont_freq_table[] = {
945 83300, 100000, 133300, 116700, 80000,
946 93300, 90000, 88900, 87500};
947
948 rdmsrl(MSR_FSB_FREQ, value);
949 i = value & 0xF;
950 WARN_ON(i > 8);
951
952 return airmont_freq_table[i];
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700953}
954
Philippe Longepe938d21a2015-11-09 17:40:46 -0800955static void atom_get_vid(struct cpudata *cpudata)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800956{
957 u64 value;
958
Philippe Longepe938d21a2015-11-09 17:40:46 -0800959 rdmsrl(ATOM_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700960 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
961 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800962 cpudata->vid.ratio = div_fp(
963 cpudata->vid.max - cpudata->vid.min,
964 int_tofp(cpudata->pstate.max_pstate -
965 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700966
Philippe Longepe938d21a2015-11-09 17:40:46 -0800967 rdmsrl(ATOM_TURBO_VIDS, value);
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700968 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800969}
970
Dirk Brandewie016c8152013-10-21 09:20:34 -0700971static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800972{
973 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700974
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000975 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800976 return (value >> 40) & 0xFF;
977}
978
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700979static int core_get_max_pstate_physical(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800980{
981 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700982
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000983 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800984 return (value >> 8) & 0xFF;
985}
986
Dirk Brandewie93f08222013-02-06 09:02:13 -0800987static int core_get_max_pstate(void)
988{
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -0700989 u64 tar;
990 u64 plat_info;
991 int max_pstate;
992 int err;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800993
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -0700994 rdmsrl(MSR_PLATFORM_INFO, plat_info);
995 max_pstate = (plat_info >> 8) & 0xFF;
996
997 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
998 if (!err) {
999 /* Do some sanity checking for safety */
1000 if (plat_info & 0x600000000) {
1001 u64 tdp_ctrl;
1002 u64 tdp_ratio;
1003 int tdp_msr;
1004
1005 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1006 if (err)
1007 goto skip_tar;
1008
Jan Kiszka5fc8f7072016-07-08 20:42:04 +02001009 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x3);
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001010 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1011 if (err)
1012 goto skip_tar;
1013
Srinivas Pandruvada1becf032016-04-22 19:53:59 -07001014 /* For level 1 and 2, bits[23:16] contain the ratio */
1015 if (tdp_ctrl)
1016 tdp_ratio >>= 16;
1017
1018 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001019 if (tdp_ratio - 1 == tar) {
1020 max_pstate = tar;
1021 pr_debug("max_pstate=TAC %x\n", max_pstate);
1022 } else {
1023 goto skip_tar;
1024 }
1025 }
1026 }
1027
1028skip_tar:
1029 return max_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001030}
1031
Dirk Brandewie016c8152013-10-21 09:20:34 -07001032static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001033{
1034 u64 value;
1035 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001036
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001037 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001038 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -07001039 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001040 if (ret <= nont)
1041 ret = nont;
1042 return ret;
1043}
1044
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001045static inline int core_get_scaling(void)
1046{
1047 return 100000;
1048}
1049
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001050static u64 core_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001051{
1052 u64 val;
1053
Chen Yu144c8e12015-07-29 23:53:10 +08001054 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001055 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001056 val |= (u64)1 << 32;
1057
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001058 return val;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001059}
1060
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001061static int knl_get_turbo_pstate(void)
1062{
1063 u64 value;
1064 int nont, ret;
1065
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001066 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001067 nont = core_get_max_pstate();
1068 ret = (((value) >> 8) & 0xFF);
1069 if (ret <= nont)
1070 ret = nont;
1071 return ret;
1072}
1073
Dirk Brandewie016c8152013-10-21 09:20:34 -07001074static struct cpu_defaults core_params = {
1075 .pid_policy = {
1076 .sample_rate_ms = 10,
1077 .deadband = 0,
1078 .setpoint = 97,
1079 .p_gain_pct = 20,
1080 .d_gain_pct = 0,
1081 .i_gain_pct = 0,
1082 },
1083 .funcs = {
1084 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001085 .get_max_physical = core_get_max_pstate_physical,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001086 .get_min = core_get_min_pstate,
1087 .get_turbo = core_get_turbo_pstate,
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001088 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001089 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001090 .get_target_pstate = get_target_pstate_use_performance,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001091 },
1092};
1093
Julia Lawall42ce8922016-09-11 15:06:01 +02001094static const struct cpu_defaults silvermont_params = {
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001095 .pid_policy = {
1096 .sample_rate_ms = 10,
1097 .deadband = 0,
Kristen Carlson Accardi6a82ba62015-04-10 11:06:43 -07001098 .setpoint = 60,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001099 .p_gain_pct = 14,
1100 .d_gain_pct = 0,
1101 .i_gain_pct = 4,
1102 },
1103 .funcs = {
Philippe Longepe938d21a2015-11-09 17:40:46 -08001104 .get_max = atom_get_max_pstate,
1105 .get_max_physical = atom_get_max_pstate,
1106 .get_min = atom_get_min_pstate,
1107 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001108 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001109 .get_scaling = silvermont_get_scaling,
1110 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001111 .get_target_pstate = get_target_pstate_use_cpu_load,
Philippe Longepe1421df62015-11-09 17:40:47 -08001112 },
1113};
1114
Julia Lawall42ce8922016-09-11 15:06:01 +02001115static const struct cpu_defaults airmont_params = {
Philippe Longepe1421df62015-11-09 17:40:47 -08001116 .pid_policy = {
1117 .sample_rate_ms = 10,
1118 .deadband = 0,
1119 .setpoint = 60,
1120 .p_gain_pct = 14,
1121 .d_gain_pct = 0,
1122 .i_gain_pct = 4,
1123 },
1124 .funcs = {
1125 .get_max = atom_get_max_pstate,
1126 .get_max_physical = atom_get_max_pstate,
1127 .get_min = atom_get_min_pstate,
1128 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001129 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001130 .get_scaling = airmont_get_scaling,
Philippe Longepe938d21a2015-11-09 17:40:46 -08001131 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001132 .get_target_pstate = get_target_pstate_use_cpu_load,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001133 },
1134};
1135
Julia Lawall42ce8922016-09-11 15:06:01 +02001136static const struct cpu_defaults knl_params = {
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001137 .pid_policy = {
1138 .sample_rate_ms = 10,
1139 .deadband = 0,
1140 .setpoint = 97,
1141 .p_gain_pct = 20,
1142 .d_gain_pct = 0,
1143 .i_gain_pct = 0,
1144 },
1145 .funcs = {
1146 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001147 .get_max_physical = core_get_max_pstate_physical,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001148 .get_min = core_get_min_pstate,
1149 .get_turbo = knl_get_turbo_pstate,
Lukasz Anaczkowski69cefc22015-07-21 10:41:13 +02001150 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001151 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001152 .get_target_pstate = get_target_pstate_use_performance,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001153 },
1154};
1155
Julia Lawall42ce8922016-09-11 15:06:01 +02001156static const struct cpu_defaults bxt_params = {
Srinivas Pandruvada41bad472016-06-09 15:34:41 -07001157 .pid_policy = {
1158 .sample_rate_ms = 10,
1159 .deadband = 0,
1160 .setpoint = 60,
1161 .p_gain_pct = 14,
1162 .d_gain_pct = 0,
1163 .i_gain_pct = 4,
1164 },
1165 .funcs = {
1166 .get_max = core_get_max_pstate,
1167 .get_max_physical = core_get_max_pstate_physical,
1168 .get_min = core_get_min_pstate,
1169 .get_turbo = core_get_turbo_pstate,
1170 .get_scaling = core_get_scaling,
1171 .get_val = core_get_val,
1172 .get_target_pstate = get_target_pstate_use_cpu_load,
1173 },
1174};
1175
Dirk Brandewie93f08222013-02-06 09:02:13 -08001176static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
1177{
1178 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie7244cb62013-10-21 09:20:33 -07001179 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001180 int min_perf;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001181 struct perf_limits *perf_limits = limits;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001182
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001183 if (limits->no_turbo || limits->turbo_disabled)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001184 max_perf = cpu->pstate.max_pstate;
1185
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001186 if (per_cpu_limits)
1187 perf_limits = cpu->perf_limits;
1188
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001189 /*
1190 * performance can be limited by user through sysfs, by cpufreq
1191 * policy, or by cpu specific default values determined through
1192 * experimentation.
1193 */
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001194 max_perf_adj = fp_ext_toint(max_perf * perf_limits->max_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001195 *max = clamp_t(int, max_perf_adj,
1196 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001197
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001198 min_perf = fp_ext_toint(max_perf * perf_limits->min_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001199 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001200}
1201
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001202static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001203{
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001204 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1205 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001206 /*
1207 * Generally, there is no guarantee that this code will always run on
1208 * the CPU being updated, so force the register update to run on the
1209 * right CPU.
1210 */
1211 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1212 pstate_funcs.get_val(cpu, pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001213}
1214
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001215static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1216{
1217 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1218}
1219
1220static void intel_pstate_max_within_limits(struct cpudata *cpu)
1221{
1222 int min_pstate, max_pstate;
1223
1224 update_turbo_state();
1225 intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate);
1226 intel_pstate_set_pstate(cpu, max_pstate);
1227}
1228
Dirk Brandewie93f08222013-02-06 09:02:13 -08001229static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1230{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001231 cpu->pstate.min_pstate = pstate_funcs.get_min();
1232 cpu->pstate.max_pstate = pstate_funcs.get_max();
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001233 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
Dirk Brandewie016c8152013-10-21 09:20:34 -07001234 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001235 cpu->pstate.scaling = pstate_funcs.get_scaling();
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001236 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1237 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001238
Dirk Brandewie007bea02013-12-18 10:32:39 -08001239 if (pstate_funcs.get_vid)
1240 pstate_funcs.get_vid(cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001241
1242 intel_pstate_set_min_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001243}
1244
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001245static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001246{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +03001247 struct sample *sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001248
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001249 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001250}
1251
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001252static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001253{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001254 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001255 unsigned long flags;
Doug Smythies4055fad2015-04-11 21:10:26 -07001256 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001257
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001258 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001259 rdmsrl(MSR_IA32_APERF, aperf);
1260 rdmsrl(MSR_IA32_MPERF, mperf);
Philippe Longepee70eed22015-12-04 17:40:32 +01001261 tsc = rdtsc();
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001262 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001263 local_irq_restore(flags);
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001264 return false;
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001265 }
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001266 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001267
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001268 cpu->last_sample_time = cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001269 cpu->sample.time = time;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001270 cpu->sample.aperf = aperf;
1271 cpu->sample.mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001272 cpu->sample.tsc = tsc;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001273 cpu->sample.aperf -= cpu->prev_aperf;
1274 cpu->sample.mperf -= cpu->prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001275 cpu->sample.tsc -= cpu->prev_tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001276
Dirk Brandewie93f08222013-02-06 09:02:13 -08001277 cpu->prev_aperf = aperf;
1278 cpu->prev_mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001279 cpu->prev_tsc = tsc;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001280 /*
1281 * First time this function is invoked in a given cycle, all of the
1282 * previous sample data fields are equal to zero or stale and they must
1283 * be populated with meaningful numbers for things to work, so assume
1284 * that sample.time will always be reset before setting the utilization
1285 * update hook and make the caller skip the sample then.
1286 */
1287 return !!cpu->last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001288}
1289
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001290static inline int32_t get_avg_frequency(struct cpudata *cpu)
1291{
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001292 return mul_ext_fp(cpu->sample.core_avg_perf,
1293 cpu->pstate.max_pstate_physical * cpu->pstate.scaling);
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001294}
1295
Philippe Longepebdcaa232016-04-22 11:46:09 -07001296static inline int32_t get_avg_pstate(struct cpudata *cpu)
1297{
Rafael J. Wysocki8edb0a62016-05-11 19:10:42 +02001298 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1299 cpu->sample.core_avg_perf);
Philippe Longepebdcaa232016-04-22 11:46:09 -07001300}
1301
Philippe Longepee70eed22015-12-04 17:40:32 +01001302static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
1303{
1304 struct sample *sample = &cpu->sample;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001305 int32_t busy_frac, boost;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001306 int target, avg_pstate;
Philippe Longepee70eed22015-12-04 17:40:32 +01001307
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001308 busy_frac = div_fp(sample->mperf, sample->tsc);
Philippe Longepe63d1d652015-12-04 17:40:35 +01001309
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001310 boost = cpu->iowait_boost;
1311 cpu->iowait_boost >>= 1;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001312
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001313 if (busy_frac < boost)
1314 busy_frac = boost;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001315
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001316 sample->busy_scaled = busy_frac * 100;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001317
1318 target = limits->no_turbo || limits->turbo_disabled ?
1319 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1320 target += target >> 2;
1321 target = mul_fp(target, busy_frac);
1322 if (target < cpu->pstate.min_pstate)
1323 target = cpu->pstate.min_pstate;
1324
1325 /*
1326 * If the average P-state during the previous cycle was higher than the
1327 * current target, add 50% of the difference to the target to reduce
1328 * possible performance oscillations and offset possible performance
1329 * loss related to moving the workload from one CPU to another within
1330 * a package/module.
1331 */
1332 avg_pstate = get_avg_pstate(cpu);
1333 if (avg_pstate > target)
1334 target += (avg_pstate - target) >> 1;
1335
1336 return target;
Philippe Longepee70eed22015-12-04 17:40:32 +01001337}
1338
Philippe Longepe157386b2015-12-04 17:40:30 +01001339static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001340{
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001341 int32_t perf_scaled, max_pstate, current_pstate, sample_ratio;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001342 u64 duration_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001343
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001344 /*
Rafael J. Wysockif00593a2016-09-30 03:13:34 +02001345 * perf_scaled is the ratio of the average P-state during the last
1346 * sampling period to the P-state requested last time (in percent).
1347 *
1348 * That measures the system's response to the previous P-state
1349 * selection.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001350 */
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001351 max_pstate = cpu->pstate.max_pstate_physical;
1352 current_pstate = cpu->pstate.current_pstate;
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001353 perf_scaled = mul_ext_fp(cpu->sample.core_avg_perf,
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001354 div_fp(100 * max_pstate, current_pstate));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001355
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001356 /*
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001357 * Since our utilization update callback will not run unless we are
1358 * in C0, check if the actual elapsed time is significantly greater (3x)
1359 * than our sample interval. If it is, then we were idle for a long
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001360 * enough period of time to adjust our performance metric.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001361 */
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001362 duration_ns = cpu->sample.time - cpu->last_sample_time;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001363 if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001364 sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns);
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001365 perf_scaled = mul_fp(perf_scaled, sample_ratio);
Rafael J. Wysockiffb81052016-04-10 05:59:10 +02001366 } else {
1367 sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc);
1368 if (sample_ratio < int_tofp(1))
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001369 perf_scaled = 0;
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001370 }
1371
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001372 cpu->sample.busy_scaled = perf_scaled;
1373 return cpu->pstate.current_pstate - pid_calc(&cpu->pid, perf_scaled);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001374}
1375
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001376static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001377{
1378 int max_perf, min_perf;
1379
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001380 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
1381 pstate = clamp_t(int, pstate, min_perf, max_perf);
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001382 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001383 return pstate;
1384}
1385
1386static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1387{
1388 pstate = intel_pstate_prepare_request(cpu, pstate);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001389 if (pstate == cpu->pstate.current_pstate)
1390 return;
1391
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001392 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001393 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1394}
1395
Dirk Brandewie93f08222013-02-06 09:02:13 -08001396static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
1397{
Philippe Longepe157386b2015-12-04 17:40:30 +01001398 int from, target_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001399 struct sample *sample;
1400
1401 from = cpu->pstate.current_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001402
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02001403 target_pstate = cpu->policy == CPUFREQ_POLICY_PERFORMANCE ?
1404 cpu->pstate.turbo_pstate : pstate_funcs.get_target_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001405
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001406 update_turbo_state();
1407
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001408 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies4055fad2015-04-11 21:10:26 -07001409
1410 sample = &cpu->sample;
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001411 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
Philippe Longepe157386b2015-12-04 17:40:30 +01001412 fp_toint(sample->busy_scaled),
Doug Smythies4055fad2015-04-11 21:10:26 -07001413 from,
1414 cpu->pstate.current_pstate,
1415 sample->mperf,
1416 sample->aperf,
1417 sample->tsc,
Srinivas Pandruvada3ba7bca2016-09-13 17:41:33 -07001418 get_avg_frequency(cpu),
1419 fp_toint(cpu->iowait_boost * 100));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001420}
1421
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001422static void intel_pstate_update_util(struct update_util_data *data, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02001423 unsigned int flags)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001424{
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001425 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001426 u64 delta_ns;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001427
Rafael J. Wysocki1d298152016-10-19 02:53:26 +02001428 if (pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load) {
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001429 if (flags & SCHED_CPUFREQ_IOWAIT) {
1430 cpu->iowait_boost = int_tofp(1);
1431 } else if (cpu->iowait_boost) {
1432 /* Clear iowait_boost if the CPU may have been idle. */
1433 delta_ns = time - cpu->last_update;
1434 if (delta_ns > TICK_NSEC)
1435 cpu->iowait_boost = 0;
1436 }
1437 cpu->last_update = time;
1438 }
1439
1440 delta_ns = time - cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001441 if ((s64)delta_ns >= pid_params.sample_rate_ns) {
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001442 bool sample_taken = intel_pstate_sample(cpu, time);
1443
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001444 if (sample_taken) {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001445 intel_pstate_calc_avg_perf(cpu);
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001446 if (!hwp_active)
1447 intel_pstate_adjust_busy_pstate(cpu);
1448 }
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001449 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001450}
1451
1452#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -08001453 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1454 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001455
1456static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dave Hansen5b20c942016-06-02 17:19:45 -07001457 ICPU(INTEL_FAM6_SANDYBRIDGE, core_params),
1458 ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_params),
1459 ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_params),
1460 ICPU(INTEL_FAM6_IVYBRIDGE, core_params),
1461 ICPU(INTEL_FAM6_HASWELL_CORE, core_params),
1462 ICPU(INTEL_FAM6_BROADWELL_CORE, core_params),
1463 ICPU(INTEL_FAM6_IVYBRIDGE_X, core_params),
1464 ICPU(INTEL_FAM6_HASWELL_X, core_params),
1465 ICPU(INTEL_FAM6_HASWELL_ULT, core_params),
1466 ICPU(INTEL_FAM6_HASWELL_GT3E, core_params),
1467 ICPU(INTEL_FAM6_BROADWELL_GT3E, core_params),
1468 ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_params),
1469 ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_params),
1470 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1471 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_params),
1472 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
1473 ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_params),
Srinivas Pandruvada41bad472016-06-09 15:34:41 -07001474 ICPU(INTEL_FAM6_ATOM_GOLDMONT, bxt_params),
Dirk Brandewie93f08222013-02-06 09:02:13 -08001475 {}
1476};
1477MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1478
Jisheng Zhang29327c82016-06-27 18:07:17 +08001479static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
Dave Hansen5b20c942016-06-02 17:19:45 -07001480 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
Srinivas Pandruvada65c12622016-07-23 15:12:06 -07001481 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1482 ICPU(INTEL_FAM6_SKYLAKE_X, core_params),
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001483 {}
1484};
1485
Dirk Brandewie93f08222013-02-06 09:02:13 -08001486static int intel_pstate_init_cpu(unsigned int cpunum)
1487{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001488 struct cpudata *cpu;
1489
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001490 cpu = all_cpu_data[cpunum];
1491
1492 if (!cpu) {
1493 unsigned int size = sizeof(struct cpudata);
1494
1495 if (per_cpu_limits)
1496 size += sizeof(struct perf_limits);
1497
1498 cpu = kzalloc(size, GFP_KERNEL);
1499 if (!cpu)
1500 return -ENOMEM;
1501
1502 all_cpu_data[cpunum] = cpu;
1503 if (per_cpu_limits)
1504 cpu->perf_limits = (struct perf_limits *)(cpu + 1);
1505
1506 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001507
1508 cpu = all_cpu_data[cpunum];
1509
Dirk Brandewie93f08222013-02-06 09:02:13 -08001510 cpu->cpu = cpunum;
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001511
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001512 if (hwp_active) {
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001513 intel_pstate_hwp_enable(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001514 pid_params.sample_rate_ms = 50;
1515 pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
1516 }
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001517
Vincent Minet179e8472014-07-05 01:51:33 +02001518 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001519
Dirk Brandewie93f08222013-02-06 09:02:13 -08001520 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001521
Joe Perches4836df12016-04-05 13:28:23 -07001522 pr_debug("controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001523
1524 return 0;
1525}
1526
1527static unsigned int intel_pstate_get(unsigned int cpu_num)
1528{
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001529 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -08001530
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001531 return cpu ? get_avg_frequency(cpu) : 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001532}
1533
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001534static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001535{
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001536 struct cpudata *cpu = all_cpu_data[cpu_num];
1537
Rafael J. Wysocki5ab666e2016-06-27 23:47:15 +02001538 if (cpu->update_util_set)
1539 return;
1540
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001541 /* Prevent intel_pstate_update_util() from using stale data. */
1542 cpu->sample.time = 0;
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001543 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
1544 intel_pstate_update_util);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001545 cpu->update_util_set = true;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001546}
1547
1548static void intel_pstate_clear_update_util_hook(unsigned int cpu)
1549{
Chen Yu4578ee7e2016-05-11 14:33:08 +08001550 struct cpudata *cpu_data = all_cpu_data[cpu];
1551
1552 if (!cpu_data->update_util_set)
1553 return;
1554
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001555 cpufreq_remove_update_util_hook(cpu);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001556 cpu_data->update_util_set = false;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001557 synchronize_sched();
1558}
1559
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001560static void intel_pstate_set_performance_limits(struct perf_limits *limits)
1561{
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -07001562 mutex_lock(&intel_pstate_limits_lock);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001563 limits->no_turbo = 0;
1564 limits->turbo_disabled = 0;
1565 limits->max_perf_pct = 100;
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001566 limits->max_perf = int_ext_tofp(1);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001567 limits->min_perf_pct = 100;
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001568 limits->min_perf = int_ext_tofp(1);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001569 limits->max_policy_pct = 100;
1570 limits->max_sysfs_pct = 100;
1571 limits->min_policy_pct = 0;
1572 limits->min_sysfs_pct = 0;
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -07001573 mutex_unlock(&intel_pstate_limits_lock);
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001574}
1575
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001576static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
1577 struct perf_limits *limits)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001578{
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -07001579
1580 mutex_lock(&intel_pstate_limits_lock);
1581
Prarit Bhargava8478f532015-11-20 18:47:56 -05001582 limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
1583 policy->cpuinfo.max_freq);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001584 limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0, 100);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001585 if (policy->max == policy->min) {
1586 limits->min_policy_pct = limits->max_policy_pct;
1587 } else {
Srinivas Pandruvada46992d62016-11-21 16:33:19 -08001588 limits->min_policy_pct = DIV_ROUND_UP(policy->min * 100,
1589 policy->cpuinfo.max_freq);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07001590 limits->min_policy_pct = clamp_t(int, limits->min_policy_pct,
1591 0, 100);
1592 }
Chen Yu43717aa2015-09-09 18:27:31 +08001593
1594 /* Normalize user input to [min_policy_pct, max_policy_pct] */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001595 limits->min_perf_pct = max(limits->min_policy_pct,
1596 limits->min_sysfs_pct);
1597 limits->min_perf_pct = min(limits->max_policy_pct,
1598 limits->min_perf_pct);
1599 limits->max_perf_pct = min(limits->max_policy_pct,
1600 limits->max_sysfs_pct);
1601 limits->max_perf_pct = max(limits->min_policy_pct,
1602 limits->max_perf_pct);
Chen Yu43717aa2015-09-09 18:27:31 +08001603
1604 /* Make sure min_perf_pct <= max_perf_pct */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001605 limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
Chen Yu43717aa2015-09-09 18:27:31 +08001606
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -08001607 limits->min_perf = div_ext_fp(limits->min_perf_pct, 100);
1608 limits->max_perf = div_ext_fp(limits->max_perf_pct, 100);
1609 limits->max_perf = round_up(limits->max_perf, EXT_FRAC_BITS);
1610 limits->min_perf = round_up(limits->min_perf, EXT_FRAC_BITS);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001611
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -07001612 mutex_unlock(&intel_pstate_limits_lock);
1613
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001614 pr_debug("cpu:%d max_perf_pct:%d min_perf_pct:%d\n", policy->cpu,
1615 limits->max_perf_pct, limits->min_perf_pct);
1616}
1617
1618static int intel_pstate_set_policy(struct cpufreq_policy *policy)
1619{
1620 struct cpudata *cpu;
1621 struct perf_limits *perf_limits = NULL;
1622
1623 if (!policy->cpuinfo.max_freq)
1624 return -ENODEV;
1625
1626 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
1627 policy->cpuinfo.max_freq, policy->max);
1628
1629 cpu = all_cpu_data[policy->cpu];
1630 cpu->policy = policy->policy;
1631
1632 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
1633 policy->max < policy->cpuinfo.max_freq &&
1634 policy->max > cpu->pstate.max_pstate * cpu->pstate.scaling) {
1635 pr_debug("policy->max > max non turbo frequency\n");
1636 policy->max = policy->cpuinfo.max_freq;
1637 }
1638
1639 if (per_cpu_limits)
1640 perf_limits = cpu->perf_limits;
1641
1642 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
1643 if (!perf_limits) {
1644 limits = &performance_limits;
1645 perf_limits = limits;
1646 }
1647 if (policy->max >= policy->cpuinfo.max_freq) {
1648 pr_debug("set performance\n");
1649 intel_pstate_set_performance_limits(perf_limits);
1650 goto out;
1651 }
1652 } else {
1653 pr_debug("set powersave\n");
1654 if (!perf_limits) {
1655 limits = &powersave_limits;
1656 perf_limits = limits;
1657 }
1658
1659 }
1660
1661 intel_pstate_update_perf_limits(policy, perf_limits);
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001662 out:
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02001663 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001664 /*
1665 * NOHZ_FULL CPUs need this as the governor callback may not
1666 * be invoked on them.
1667 */
1668 intel_pstate_clear_update_util_hook(policy->cpu);
1669 intel_pstate_max_within_limits(cpu);
1670 }
1671
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001672 intel_pstate_set_update_util_hook(policy->cpu);
1673
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001674 intel_pstate_hwp_set_policy(policy);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001675
Dirk Brandewie93f08222013-02-06 09:02:13 -08001676 return 0;
1677}
1678
1679static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
1680{
Viresh Kumarbe49e342013-10-02 14:13:19 +05301681 cpufreq_verify_within_cpu_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001682
Stratos Karafotis285cb992014-07-18 08:37:21 -07001683 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -07001684 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001685 return -EINVAL;
1686
1687 return 0;
1688}
1689
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001690static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001691{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001692 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001693}
1694
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001695static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
1696{
1697 pr_debug("CPU %d exiting\n", policy->cpu);
1698
1699 intel_pstate_clear_update_util_hook(policy->cpu);
1700 if (!hwp_active)
1701 intel_cpufreq_stop_cpu(policy);
1702}
1703
1704static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
1705{
1706 intel_pstate_exit_perf_limits(policy);
1707
1708 policy->fast_switch_possible = false;
1709
1710 return 0;
1711}
1712
1713static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001714{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001715 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -07001716 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001717
1718 rc = intel_pstate_init_cpu(policy->cpu);
1719 if (rc)
1720 return rc;
1721
1722 cpu = all_cpu_data[policy->cpu];
1723
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001724 /*
1725 * We need sane value in the cpu->perf_limits, so inherit from global
1726 * perf_limits limits, which are seeded with values based on the
1727 * CONFIG_CPU_FREQ_DEFAULT_GOV_*, during boot up.
1728 */
1729 if (per_cpu_limits)
1730 memcpy(cpu->perf_limits, limits, sizeof(struct perf_limits));
1731
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001732 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
1733 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001734
1735 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001736 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07001737 update_turbo_state();
1738 policy->cpuinfo.max_freq = limits->turbo_disabled ?
1739 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1740 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
1741
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001742 intel_pstate_init_acpi_perf_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001743 cpumask_set_cpu(policy->cpu, policy->cpus);
1744
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001745 policy->fast_switch_possible = true;
1746
Dirk Brandewie93f08222013-02-06 09:02:13 -08001747 return 0;
1748}
1749
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001750static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001751{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001752 int ret = __intel_pstate_cpu_init(policy);
1753
1754 if (ret)
1755 return ret;
1756
1757 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
1758 if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
1759 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1760 else
1761 policy->policy = CPUFREQ_POLICY_POWERSAVE;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001762
1763 return 0;
1764}
1765
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001766static struct cpufreq_driver intel_pstate = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08001767 .flags = CPUFREQ_CONST_LOOPS,
1768 .verify = intel_pstate_verify_policy,
1769 .setpolicy = intel_pstate_set_policy,
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001770 .resume = intel_pstate_hwp_set_policy,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001771 .get = intel_pstate_get,
1772 .init = intel_pstate_cpu_init,
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001773 .exit = intel_pstate_cpu_exit,
Dirk Brandewiebb180082014-03-19 08:45:54 -07001774 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001775 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -08001776};
1777
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001778static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
1779{
1780 struct cpudata *cpu = all_cpu_data[policy->cpu];
1781 struct perf_limits *perf_limits = limits;
1782
1783 update_turbo_state();
1784 policy->cpuinfo.max_freq = limits->turbo_disabled ?
1785 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
1786
1787 cpufreq_verify_within_cpu_limits(policy);
1788
1789 if (per_cpu_limits)
1790 perf_limits = cpu->perf_limits;
1791
1792 intel_pstate_update_perf_limits(policy, perf_limits);
1793
1794 return 0;
1795}
1796
1797static unsigned int intel_cpufreq_turbo_update(struct cpudata *cpu,
1798 struct cpufreq_policy *policy,
1799 unsigned int target_freq)
1800{
1801 unsigned int max_freq;
1802
1803 update_turbo_state();
1804
1805 max_freq = limits->no_turbo || limits->turbo_disabled ?
1806 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
1807 policy->cpuinfo.max_freq = max_freq;
1808 if (policy->max > max_freq)
1809 policy->max = max_freq;
1810
1811 if (target_freq > max_freq)
1812 target_freq = max_freq;
1813
1814 return target_freq;
1815}
1816
1817static int intel_cpufreq_target(struct cpufreq_policy *policy,
1818 unsigned int target_freq,
1819 unsigned int relation)
1820{
1821 struct cpudata *cpu = all_cpu_data[policy->cpu];
1822 struct cpufreq_freqs freqs;
1823 int target_pstate;
1824
1825 freqs.old = policy->cur;
1826 freqs.new = intel_cpufreq_turbo_update(cpu, policy, target_freq);
1827
1828 cpufreq_freq_transition_begin(policy, &freqs);
1829 switch (relation) {
1830 case CPUFREQ_RELATION_L:
1831 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
1832 break;
1833 case CPUFREQ_RELATION_H:
1834 target_pstate = freqs.new / cpu->pstate.scaling;
1835 break;
1836 default:
1837 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
1838 break;
1839 }
1840 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1841 if (target_pstate != cpu->pstate.current_pstate) {
1842 cpu->pstate.current_pstate = target_pstate;
1843 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
1844 pstate_funcs.get_val(cpu, target_pstate));
1845 }
1846 cpufreq_freq_transition_end(policy, &freqs, false);
1847
1848 return 0;
1849}
1850
1851static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
1852 unsigned int target_freq)
1853{
1854 struct cpudata *cpu = all_cpu_data[policy->cpu];
1855 int target_pstate;
1856
1857 target_freq = intel_cpufreq_turbo_update(cpu, policy, target_freq);
1858 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
1859 intel_pstate_update_pstate(cpu, target_pstate);
1860 return target_freq;
1861}
1862
1863static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
1864{
1865 int ret = __intel_pstate_cpu_init(policy);
1866
1867 if (ret)
1868 return ret;
1869
1870 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
1871 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
1872 policy->cur = policy->cpuinfo.min_freq;
1873
1874 return 0;
1875}
1876
1877static struct cpufreq_driver intel_cpufreq = {
1878 .flags = CPUFREQ_CONST_LOOPS,
1879 .verify = intel_cpufreq_verify_policy,
1880 .target = intel_cpufreq_target,
1881 .fast_switch = intel_cpufreq_fast_switch,
1882 .init = intel_cpufreq_cpu_init,
1883 .exit = intel_pstate_cpu_exit,
1884 .stop_cpu = intel_cpufreq_stop_cpu,
1885 .name = "intel_cpufreq",
1886};
1887
1888static struct cpufreq_driver *intel_pstate_driver = &intel_pstate;
1889
Jisheng Zhangeed43602016-06-27 18:07:16 +08001890static int no_load __initdata;
1891static int no_hwp __initdata;
1892static int hwp_only __initdata;
Jisheng Zhang29327c82016-06-27 18:07:17 +08001893static unsigned int force_load __initdata;
Dirk Brandewie6be26492013-02-15 22:55:10 +01001894
Jisheng Zhang29327c82016-06-27 18:07:17 +08001895static int __init intel_pstate_msrs_not_valid(void)
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001896{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001897 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -07001898 !pstate_funcs.get_min() ||
1899 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001900 return -ENODEV;
1901
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001902 return 0;
1903}
Dirk Brandewie016c8152013-10-21 09:20:34 -07001904
Jisheng Zhang29327c82016-06-27 18:07:17 +08001905static void __init copy_pid_params(struct pstate_adjust_policy *policy)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001906{
1907 pid_params.sample_rate_ms = policy->sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001908 pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001909 pid_params.p_gain_pct = policy->p_gain_pct;
1910 pid_params.i_gain_pct = policy->i_gain_pct;
1911 pid_params.d_gain_pct = policy->d_gain_pct;
1912 pid_params.deadband = policy->deadband;
1913 pid_params.setpoint = policy->setpoint;
1914}
1915
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08001916#ifdef CONFIG_ACPI
1917static void intel_pstate_use_acpi_profile(void)
1918{
1919 if (acpi_gbl_FADT.preferred_profile == PM_MOBILE)
1920 pstate_funcs.get_target_pstate =
1921 get_target_pstate_use_cpu_load;
1922}
1923#else
1924static void intel_pstate_use_acpi_profile(void)
1925{
1926}
1927#endif
1928
Jisheng Zhang29327c82016-06-27 18:07:17 +08001929static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001930{
1931 pstate_funcs.get_max = funcs->get_max;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001932 pstate_funcs.get_max_physical = funcs->get_max_physical;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001933 pstate_funcs.get_min = funcs->get_min;
1934 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001935 pstate_funcs.get_scaling = funcs->get_scaling;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001936 pstate_funcs.get_val = funcs->get_val;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001937 pstate_funcs.get_vid = funcs->get_vid;
Philippe Longepe157386b2015-12-04 17:40:30 +01001938 pstate_funcs.get_target_pstate = funcs->get_target_pstate;
1939
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08001940 intel_pstate_use_acpi_profile();
Dirk Brandewie016c8152013-10-21 09:20:34 -07001941}
1942
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001943#ifdef CONFIG_ACPI
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001944
Jisheng Zhang29327c82016-06-27 18:07:17 +08001945static bool __init intel_pstate_no_acpi_pss(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001946{
1947 int i;
1948
1949 for_each_possible_cpu(i) {
1950 acpi_status status;
1951 union acpi_object *pss;
1952 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1953 struct acpi_processor *pr = per_cpu(processors, i);
1954
1955 if (!pr)
1956 continue;
1957
1958 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
1959 if (ACPI_FAILURE(status))
1960 continue;
1961
1962 pss = buffer.pointer;
1963 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
1964 kfree(pss);
1965 return false;
1966 }
1967
1968 kfree(pss);
1969 }
1970
1971 return true;
1972}
1973
Jisheng Zhang29327c82016-06-27 18:07:17 +08001974static bool __init intel_pstate_has_acpi_ppc(void)
ethan zhao966916e2014-12-01 11:32:08 +09001975{
1976 int i;
1977
1978 for_each_possible_cpu(i) {
1979 struct acpi_processor *pr = per_cpu(processors, i);
1980
1981 if (!pr)
1982 continue;
1983 if (acpi_has_method(pr->handle, "_PPC"))
1984 return true;
1985 }
1986 return false;
1987}
1988
1989enum {
1990 PSS,
1991 PPC,
1992};
1993
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001994struct hw_vendor_info {
1995 u16 valid;
1996 char oem_id[ACPI_OEM_ID_SIZE];
1997 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
ethan zhao966916e2014-12-01 11:32:08 +09001998 int oem_pwr_table;
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001999};
2000
2001/* Hardware vendor-specific info that has its own power management modes */
Jisheng Zhang29327c82016-06-27 18:07:17 +08002002static struct hw_vendor_info vendor_info[] __initdata = {
ethan zhao966916e2014-12-01 11:32:08 +09002003 {1, "HP ", "ProLiant", PSS},
2004 {1, "ORACLE", "X4-2 ", PPC},
2005 {1, "ORACLE", "X4-2L ", PPC},
2006 {1, "ORACLE", "X4-2B ", PPC},
2007 {1, "ORACLE", "X3-2 ", PPC},
2008 {1, "ORACLE", "X3-2L ", PPC},
2009 {1, "ORACLE", "X3-2B ", PPC},
2010 {1, "ORACLE", "X4470M2 ", PPC},
2011 {1, "ORACLE", "X4270M3 ", PPC},
2012 {1, "ORACLE", "X4270M2 ", PPC},
2013 {1, "ORACLE", "X4170M2 ", PPC},
Ethan Zhao5aecc3c2015-08-05 09:28:50 +09002014 {1, "ORACLE", "X4170 M3", PPC},
2015 {1, "ORACLE", "X4275 M3", PPC},
2016 {1, "ORACLE", "X6-2 ", PPC},
2017 {1, "ORACLE", "Sudbury ", PPC},
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002018 {0, "", ""},
2019};
2020
Jisheng Zhang29327c82016-06-27 18:07:17 +08002021static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002022{
2023 struct acpi_table_header hdr;
2024 struct hw_vendor_info *v_info;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002025 const struct x86_cpu_id *id;
2026 u64 misc_pwr;
2027
2028 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2029 if (id) {
2030 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2031 if ( misc_pwr & (1 << 8))
2032 return true;
2033 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002034
Stratos Karafotisc4108332014-07-18 08:37:23 -07002035 if (acpi_disabled ||
2036 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002037 return false;
2038
2039 for (v_info = vendor_info; v_info->valid; v_info++) {
Stratos Karafotisc4108332014-07-18 08:37:23 -07002040 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
ethan zhao966916e2014-12-01 11:32:08 +09002041 !strncmp(hdr.oem_table_id, v_info->oem_table_id,
2042 ACPI_OEM_TABLE_ID_SIZE))
2043 switch (v_info->oem_pwr_table) {
2044 case PSS:
2045 return intel_pstate_no_acpi_pss();
2046 case PPC:
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002047 return intel_pstate_has_acpi_ppc() &&
2048 (!force_load);
ethan zhao966916e2014-12-01 11:32:08 +09002049 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002050 }
2051
2052 return false;
2053}
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002054
2055static void intel_pstate_request_control_from_smm(void)
2056{
2057 /*
2058 * It may be unsafe to request P-states control from SMM if _PPC support
2059 * has not been enabled.
2060 */
2061 if (acpi_ppc)
2062 acpi_processor_pstate_control();
2063}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002064#else /* CONFIG_ACPI not enabled */
2065static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
ethan zhao966916e2014-12-01 11:32:08 +09002066static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002067static inline void intel_pstate_request_control_from_smm(void) {}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002068#endif /* CONFIG_ACPI */
2069
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002070static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2071 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
2072 {}
2073};
2074
Dirk Brandewie93f08222013-02-06 09:02:13 -08002075static int __init intel_pstate_init(void)
2076{
Dirk Brandewie907cc902013-03-05 14:15:27 -08002077 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002078 const struct x86_cpu_id *id;
Borislav Petkov64df1fd2015-04-03 15:19:53 +02002079 struct cpu_defaults *cpu_def;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002080
Dirk Brandewie6be26492013-02-15 22:55:10 +01002081 if (no_load)
2082 return -ENODEV;
2083
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002084 if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
2085 copy_cpu_funcs(&core_params.funcs);
2086 hwp_active++;
2087 goto hwp_cpu_matched;
2088 }
2089
Dirk Brandewie93f08222013-02-06 09:02:13 -08002090 id = x86_match_cpu(intel_pstate_cpu_ids);
2091 if (!id)
2092 return -ENODEV;
2093
Borislav Petkov64df1fd2015-04-03 15:19:53 +02002094 cpu_def = (struct cpu_defaults *)id->driver_data;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002095
Borislav Petkov64df1fd2015-04-03 15:19:53 +02002096 copy_pid_params(&cpu_def->pid_policy);
2097 copy_cpu_funcs(&cpu_def->funcs);
Dirk Brandewie016c8152013-10-21 09:20:34 -07002098
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002099 if (intel_pstate_msrs_not_valid())
2100 return -ENODEV;
2101
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002102hwp_cpu_matched:
2103 /*
2104 * The Intel pstate driver will be ignored if the platform
2105 * firmware has its own power management modes.
2106 */
2107 if (intel_pstate_platform_pwr_mgmt_exists())
2108 return -ENODEV;
2109
Joe Perches4836df12016-04-05 13:28:23 -07002110 pr_info("Intel P-state driver initializing\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08002111
Wei Yongjunb57ffac2013-05-13 08:03:43 +00002112 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08002113 if (!all_cpu_data)
2114 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002115
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002116 if (!hwp_active && hwp_only)
2117 goto out;
2118
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002119 intel_pstate_request_control_from_smm();
2120
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002121 rc = cpufreq_register_driver(intel_pstate_driver);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002122 if (rc)
2123 goto out;
2124
2125 intel_pstate_debug_expose_params();
2126 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08002127
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002128 if (hwp_active)
Joe Perches4836df12016-04-05 13:28:23 -07002129 pr_info("HWP enabled\n");
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002130
Dirk Brandewie93f08222013-02-06 09:02:13 -08002131 return rc;
2132out:
Dirk Brandewie907cc902013-03-05 14:15:27 -08002133 get_online_cpus();
2134 for_each_online_cpu(cpu) {
2135 if (all_cpu_data[cpu]) {
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002136 if (intel_pstate_driver == &intel_pstate)
2137 intel_pstate_clear_update_util_hook(cpu);
2138
Dirk Brandewie907cc902013-03-05 14:15:27 -08002139 kfree(all_cpu_data[cpu]);
2140 }
2141 }
2142
2143 put_online_cpus();
2144 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002145 return -ENODEV;
2146}
2147device_initcall(intel_pstate_init);
2148
Dirk Brandewie6be26492013-02-15 22:55:10 +01002149static int __init intel_pstate_setup(char *str)
2150{
2151 if (!str)
2152 return -EINVAL;
2153
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002154 if (!strcmp(str, "disable")) {
Dirk Brandewie6be26492013-02-15 22:55:10 +01002155 no_load = 1;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002156 } else if (!strcmp(str, "passive")) {
2157 pr_info("Passive mode enabled\n");
2158 intel_pstate_driver = &intel_cpufreq;
2159 no_hwp = 1;
2160 }
Prarit Bhargava539342f2015-10-22 09:43:31 -04002161 if (!strcmp(str, "no_hwp")) {
Joe Perches4836df12016-04-05 13:28:23 -07002162 pr_info("HWP disabled\n");
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002163 no_hwp = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04002164 }
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002165 if (!strcmp(str, "force"))
2166 force_load = 1;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002167 if (!strcmp(str, "hwp_only"))
2168 hwp_only = 1;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002169 if (!strcmp(str, "per_cpu_perf_limits"))
2170 per_cpu_limits = true;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002171
2172#ifdef CONFIG_ACPI
2173 if (!strcmp(str, "support_acpi_ppc"))
2174 acpi_ppc = true;
2175#endif
2176
Dirk Brandewie6be26492013-02-15 22:55:10 +01002177 return 0;
2178}
2179early_param("intel_pstate", intel_pstate_setup);
2180
Dirk Brandewie93f08222013-02-06 09:02:13 -08002181MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2182MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2183MODULE_LICENSE("GPL");