blob: a7ed42d6f3662d86680430ee2c3cfec0b591aa09 [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>
Ingo Molnar55687da2017-02-08 18:51:31 +010022#include <linux/sched/cpufreq.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080023#include <linux/list.h>
24#include <linux/cpu.h>
25#include <linux/cpufreq.h>
26#include <linux/sysfs.h>
27#include <linux/types.h>
28#include <linux/fs.h>
29#include <linux/debugfs.h>
Adrian Huangfbbcdc02013-10-31 23:24:05 +080030#include <linux/acpi.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100031#include <linux/vmalloc.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080032#include <trace/events/power.h>
33
34#include <asm/div64.h>
35#include <asm/msr.h>
36#include <asm/cpu_device_id.h>
Borislav Petkov64df1fd2015-04-03 15:19:53 +020037#include <asm/cpufeature.h>
Dave Hansen5b20c942016-06-02 17:19:45 -070038#include <asm/intel-family.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080039
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +010040#define INTEL_CPUFREQ_TRANSITION_LATENCY 20000
41
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -070042#ifdef CONFIG_ACPI
43#include <acpi/processor.h>
Rafael J. Wysocki17669002016-11-22 12:24:00 -080044#include <acpi/cppc_acpi.h>
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -070045#endif
46
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070047#define FRAC_BITS 8
Dirk Brandewie93f08222013-02-06 09:02:13 -080048#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
49#define fp_toint(X) ((X) >> FRAC_BITS)
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070050
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020051#define EXT_BITS 6
52#define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
Srinivas Pandruvadad5dd33d2016-11-21 16:33:20 -080053#define fp_ext_toint(X) ((X) >> EXT_FRAC_BITS)
54#define int_ext_tofp(X) ((int64_t)(X) << EXT_FRAC_BITS)
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020055
Dirk Brandewie93f08222013-02-06 09:02:13 -080056static inline int32_t mul_fp(int32_t x, int32_t y)
57{
58 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
59}
60
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040061static inline int32_t div_fp(s64 x, s64 y)
Dirk Brandewie93f08222013-02-06 09:02:13 -080062{
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040063 return div64_s64((int64_t)x << FRAC_BITS, y);
Dirk Brandewie93f08222013-02-06 09:02:13 -080064}
65
Dirk Brandewied022a652014-10-13 08:37:44 -070066static inline int ceiling_fp(int32_t x)
67{
68 int mask, ret;
69
70 ret = fp_toint(x);
71 mask = (1 << FRAC_BITS) - 1;
72 if (x & mask)
73 ret += 1;
74 return ret;
75}
76
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020077static inline u64 mul_ext_fp(u64 x, u64 y)
78{
79 return (x * y) >> EXT_FRAC_BITS;
80}
81
82static inline u64 div_ext_fp(u64 x, u64 y)
83{
84 return div64_u64(x << EXT_FRAC_BITS, y);
85}
86
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +010087static inline int32_t percent_ext_fp(int percent)
88{
89 return div_ext_fp(percent, 100);
90}
91
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070092/**
93 * struct sample - Store performance sample
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020094 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070095 * performance during last sample period
96 * @busy_scaled: Scaled busy value which is used to calculate next
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020097 * P state. This can be different than core_avg_perf
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070098 * to account for cpu idle period
99 * @aperf: Difference of actual performance frequency clock count
100 * read from APERF MSR between last and current sample
101 * @mperf: Difference of maximum performance frequency clock count
102 * read from MPERF MSR between last and current sample
103 * @tsc: Difference of time stamp counter between last and
104 * current sample
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700105 * @time: Current time from scheduler
106 *
107 * This structure is used in the cpudata structure to store performance sample
108 * data for choosing next P State.
109 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800110struct sample {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +0200111 int32_t core_avg_perf;
Philippe Longepe157386b2015-12-04 17:40:30 +0100112 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800113 u64 aperf;
114 u64 mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700115 u64 tsc;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100116 u64 time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800117};
118
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700119/**
120 * struct pstate_data - Store P state data
121 * @current_pstate: Current requested P state
122 * @min_pstate: Min P state possible for this platform
123 * @max_pstate: Max P state possible for this platform
124 * @max_pstate_physical:This is physical Max P state for a processor
125 * This can be higher than the max_pstate which can
126 * be limited by platform thermal design power limits
127 * @scaling: Scaling factor to convert frequency to cpufreq
128 * frequency units
129 * @turbo_pstate: Max Turbo P state possible for this platform
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +0100130 * @max_freq: @max_pstate frequency in cpufreq units
131 * @turbo_freq: @turbo_pstate frequency in cpufreq units
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700132 *
133 * Stores the per cpu model P state limits and current P state.
134 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800135struct pstate_data {
136 int current_pstate;
137 int min_pstate;
138 int max_pstate;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700139 int max_pstate_physical;
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700140 int scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800141 int turbo_pstate;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +0100142 unsigned int max_freq;
143 unsigned int turbo_freq;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800144};
145
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700146/**
147 * struct vid_data - Stores voltage information data
148 * @min: VID data for this platform corresponding to
149 * the lowest P state
150 * @max: VID data corresponding to the highest P State.
151 * @turbo: VID data for turbo P state
152 * @ratio: Ratio of (vid max - vid min) /
153 * (max P state - Min P State)
154 *
155 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
156 * This data is used in Atom platforms, where in addition to target P state,
157 * the voltage data needs to be specified to select next P State.
158 */
Dirk Brandewie007bea02013-12-18 10:32:39 -0800159struct vid_data {
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700160 int min;
161 int max;
162 int turbo;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800163 int32_t ratio;
164};
165
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700166/**
167 * struct _pid - Stores PID data
168 * @setpoint: Target set point for busyness or performance
169 * @integral: Storage for accumulated error values
170 * @p_gain: PID proportional gain
171 * @i_gain: PID integral gain
172 * @d_gain: PID derivative gain
173 * @deadband: PID deadband
174 * @last_err: Last error storage for integral part of PID calculation
175 *
176 * Stores PID coefficients and last error for PID controller.
177 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800178struct _pid {
179 int setpoint;
180 int32_t integral;
181 int32_t p_gain;
182 int32_t i_gain;
183 int32_t d_gain;
184 int deadband;
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700185 int32_t last_err;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800186};
187
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700188/**
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100189 * struct global_params - Global parameters, mostly tunable via sysfs.
190 * @no_turbo: Whether or not to use turbo P-states.
191 * @turbo_disabled: Whethet or not turbo P-states are available at all,
192 * based on the MSR_IA32_MISC_ENABLE value and whether or
193 * not the maximum reported turbo P-state is different from
194 * the maximum reported non-turbo one.
195 * @min_perf_pct: Minimum capacity limit in percent of the maximum turbo
196 * P-state capacity.
197 * @max_perf_pct: Maximum capacity limit in percent of the maximum turbo
198 * P-state capacity.
199 */
200struct global_params {
201 bool no_turbo;
202 bool turbo_disabled;
203 int max_perf_pct;
204 int min_perf_pct;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700205};
206
207/**
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700208 * struct cpudata - Per CPU instance data storage
209 * @cpu: CPU number for this instance data
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200210 * @policy: CPUFreq policy value
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700211 * @update_util: CPUFreq utility callback information
Chen Yu4578ee7e2016-05-11 14:33:08 +0800212 * @update_util_set: CPUFreq utility callback is set
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200213 * @iowait_boost: iowait-related boost fraction
214 * @last_update: Time of the last update.
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700215 * @pstate: Stores P state limits for this CPU
216 * @vid: Stores VID limits for this CPU
217 * @pid: Stores PID parameters for this CPU
218 * @last_sample_time: Last Sample time
219 * @prev_aperf: Last APERF value read from APERF MSR
220 * @prev_mperf: Last MPERF value read from MPERF MSR
221 * @prev_tsc: Last timestamp counter (TSC) value
222 * @prev_cummulative_iowait: IO Wait time difference from last and
223 * current sample
224 * @sample: Storage for storing last Sample data
Rafael J. Wysockie14cf882017-03-28 00:03:20 +0200225 * @min_perf: Minimum capacity limit as a fraction of the maximum
226 * turbo P-state capacity.
227 * @max_perf: Maximum capacity limit as a fraction of the maximum
228 * turbo P-state capacity.
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700229 * @acpi_perf_data: Stores ACPI perf information read from _PSS
230 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800231 * @epp_powersave: Last saved HWP energy performance preference
232 * (EPP) or energy performance bias (EPB),
233 * when policy switched to performance
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800234 * @epp_policy: Last saved policy used to set EPP/EPB
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800235 * @epp_default: Power on default HWP energy performance
236 * preference/bias
237 * @epp_saved: Saved EPP/EPB during system suspend or CPU offline
238 * operation
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700239 *
240 * This structure stores per CPU instance data for all CPUs.
241 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800242struct cpudata {
243 int cpu;
244
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +0200245 unsigned int policy;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100246 struct update_util_data update_util;
Chen Yu4578ee7e2016-05-11 14:33:08 +0800247 bool update_util_set;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800248
Dirk Brandewie93f08222013-02-06 09:02:13 -0800249 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800250 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800251 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800252
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200253 u64 last_update;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100254 u64 last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800255 u64 prev_aperf;
256 u64 prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700257 u64 prev_tsc;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100258 u64 prev_cummulative_iowait;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800259 struct sample sample;
Rafael J. Wysockie14cf882017-03-28 00:03:20 +0200260 int32_t min_perf;
261 int32_t max_perf;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700262#ifdef CONFIG_ACPI
263 struct acpi_processor_performance acpi_perf_data;
264 bool valid_pss_table;
265#endif
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +0200266 unsigned int iowait_boost;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800267 s16 epp_powersave;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800268 s16 epp_policy;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800269 s16 epp_default;
270 s16 epp_saved;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800271};
272
273static struct cpudata **all_cpu_data;
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700274
275/**
Rafael J. Wysocki39545172016-10-11 23:07:38 +0200276 * struct pstate_adjust_policy - Stores static PID configuration data
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700277 * @sample_rate_ms: PID calculation sample rate in ms
278 * @sample_rate_ns: Sample rate calculation in ns
279 * @deadband: PID deadband
280 * @setpoint: PID Setpoint
281 * @p_gain_pct: PID proportional gain
282 * @i_gain_pct: PID integral gain
283 * @d_gain_pct: PID derivative gain
284 *
285 * Stores per CPU model static PID configuration data.
286 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800287struct pstate_adjust_policy {
288 int sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100289 s64 sample_rate_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800290 int deadband;
291 int setpoint;
292 int p_gain_pct;
293 int d_gain_pct;
294 int i_gain_pct;
295};
296
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700297/**
298 * struct pstate_funcs - Per CPU model specific callbacks
299 * @get_max: Callback to get maximum non turbo effective P state
300 * @get_max_physical: Callback to get maximum non turbo physical P state
301 * @get_min: Callback to get minimum P state
302 * @get_turbo: Callback to get turbo P state
303 * @get_scaling: Callback to get frequency scaling factor
304 * @get_val: Callback to convert P state to actual MSR write value
305 * @get_vid: Callback to get VID data for Atom platforms
306 * @get_target_pstate: Callback to a function to calculate next P state to use
307 *
308 * Core and Atom CPU models have different way to get P State limits. This
309 * structure is used to store those callbacks.
310 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700311struct pstate_funcs {
312 int (*get_max)(void);
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700313 int (*get_max_physical)(void);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700314 int (*get_min)(void);
315 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700316 int (*get_scaling)(void);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100317 u64 (*get_val)(struct cpudata*, int pstate);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800318 void (*get_vid)(struct cpudata *);
Philippe Longepe157386b2015-12-04 17:40:30 +0100319 int32_t (*get_target_pstate)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800320};
321
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700322/**
323 * struct cpu_defaults- Per CPU model default config data
324 * @pid_policy: PID config data
325 * @funcs: Callback function data
326 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700327struct cpu_defaults {
328 struct pstate_adjust_policy pid_policy;
329 struct pstate_funcs funcs;
330};
331
Philippe Longepe157386b2015-12-04 17:40:30 +0100332static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
Philippe Longepee70eed22015-12-04 17:40:32 +0100333static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
Philippe Longepe157386b2015-12-04 17:40:30 +0100334
Jisheng Zhang4a7cb7a2016-06-27 18:07:18 +0800335static struct pstate_adjust_policy pid_params __read_mostly;
336static struct pstate_funcs pstate_funcs __read_mostly;
337static int hwp_active __read_mostly;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700338static bool per_cpu_limits __read_mostly;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700339
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100340static bool driver_registered __read_mostly;
341
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700342#ifdef CONFIG_ACPI
343static bool acpi_ppc;
344#endif
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700345
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100346static struct global_params global;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800347
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +0100348static DEFINE_MUTEX(intel_pstate_driver_lock);
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -0700349static DEFINE_MUTEX(intel_pstate_limits_lock);
350
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700351#ifdef CONFIG_ACPI
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700352
353static bool intel_pstate_get_ppc_enable_status(void)
354{
355 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
356 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
357 return true;
358
359 return acpi_ppc;
360}
361
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800362#ifdef CONFIG_ACPI_CPPC_LIB
363
364/* The work item is needed to avoid CPU hotplug locking issues */
365static void intel_pstste_sched_itmt_work_fn(struct work_struct *work)
366{
367 sched_set_itmt_support();
368}
369
370static DECLARE_WORK(sched_itmt_work, intel_pstste_sched_itmt_work_fn);
371
372static void intel_pstate_set_itmt_prio(int cpu)
373{
374 struct cppc_perf_caps cppc_perf;
375 static u32 max_highest_perf = 0, min_highest_perf = U32_MAX;
376 int ret;
377
378 ret = cppc_get_perf_caps(cpu, &cppc_perf);
379 if (ret)
380 return;
381
382 /*
383 * The priorities can be set regardless of whether or not
384 * sched_set_itmt_support(true) has been called and it is valid to
385 * update them at any time after it has been called.
386 */
387 sched_set_itmt_core_prio(cppc_perf.highest_perf, cpu);
388
389 if (max_highest_perf <= min_highest_perf) {
390 if (cppc_perf.highest_perf > max_highest_perf)
391 max_highest_perf = cppc_perf.highest_perf;
392
393 if (cppc_perf.highest_perf < min_highest_perf)
394 min_highest_perf = cppc_perf.highest_perf;
395
396 if (max_highest_perf > min_highest_perf) {
397 /*
398 * This code can be run during CPU online under the
399 * CPU hotplug locks, so sched_set_itmt_support()
400 * cannot be called from here. Queue up a work item
401 * to invoke it.
402 */
403 schedule_work(&sched_itmt_work);
404 }
405 }
406}
407#else
408static void intel_pstate_set_itmt_prio(int cpu)
409{
410}
411#endif
412
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700413static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
414{
415 struct cpudata *cpu;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700416 int ret;
417 int i;
418
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800419 if (hwp_active) {
420 intel_pstate_set_itmt_prio(policy->cpu);
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700421 return;
Rafael J. Wysocki17669002016-11-22 12:24:00 -0800422 }
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700423
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700424 if (!intel_pstate_get_ppc_enable_status())
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700425 return;
426
427 cpu = all_cpu_data[policy->cpu];
428
429 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
430 policy->cpu);
431 if (ret)
432 return;
433
434 /*
435 * Check if the control value in _PSS is for PERF_CTL MSR, which should
436 * guarantee that the states returned by it map to the states in our
437 * list directly.
438 */
439 if (cpu->acpi_perf_data.control_register.space_id !=
440 ACPI_ADR_SPACE_FIXED_HARDWARE)
441 goto err;
442
443 /*
444 * If there is only one entry _PSS, simply ignore _PSS and continue as
445 * usual without taking _PSS into account
446 */
447 if (cpu->acpi_perf_data.state_count < 2)
448 goto err;
449
450 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
451 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
452 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
453 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
454 (u32) cpu->acpi_perf_data.states[i].core_frequency,
455 (u32) cpu->acpi_perf_data.states[i].power,
456 (u32) cpu->acpi_perf_data.states[i].control);
457 }
458
459 /*
460 * The _PSS table doesn't contain whole turbo frequency range.
461 * This just contains +1 MHZ above the max non turbo frequency,
462 * with control value corresponding to max turbo ratio. But
463 * when cpufreq set policy is called, it will call with this
464 * max frequency, which will cause a reduced performance as
465 * this driver uses real max turbo frequency as the max
466 * frequency. So correct this frequency in _PSS table to
Srinivas Pandruvadab00345d2016-06-14 23:12:59 -0700467 * correct max turbo frequency based on the turbo state.
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700468 * Also need to convert to MHz as _PSS freq is in MHz.
469 */
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100470 if (!global.turbo_disabled)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700471 cpu->acpi_perf_data.states[0].core_frequency =
472 policy->cpuinfo.max_freq / 1000;
473 cpu->valid_pss_table = true;
Srinivas Pandruvada6cacd112016-05-29 23:31:23 -0700474 pr_debug("_PPC limits will be enforced\n");
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700475
476 return;
477
478 err:
479 cpu->valid_pss_table = false;
480 acpi_processor_unregister_performance(policy->cpu);
481}
482
483static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
484{
485 struct cpudata *cpu;
486
487 cpu = all_cpu_data[policy->cpu];
488 if (!cpu->valid_pss_table)
489 return;
490
491 acpi_processor_unregister_performance(policy->cpu);
492}
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700493#else
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100494static inline void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700495{
496}
497
Arnd Bergmann7a3ba762016-11-25 17:50:20 +0100498static inline void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700499{
500}
501#endif
502
Dirk Brandewie93f08222013-02-06 09:02:13 -0800503static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700504 int deadband, int integral) {
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100505 pid->setpoint = int_tofp(setpoint);
506 pid->deadband = int_tofp(deadband);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800507 pid->integral = int_tofp(integral);
Dirk Brandewied98d0992014-02-12 10:01:05 -0800508 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800509}
510
511static inline void pid_p_gain_set(struct _pid *pid, int percent)
512{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200513 pid->p_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800514}
515
516static inline void pid_i_gain_set(struct _pid *pid, int percent)
517{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200518 pid->i_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800519}
520
521static inline void pid_d_gain_set(struct _pid *pid, int percent)
522{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200523 pid->d_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800524}
525
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700526static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800527{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700528 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800529 int32_t pterm, dterm, fp_error;
530 int32_t integral_limit;
531
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100532 fp_error = pid->setpoint - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800533
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100534 if (abs(fp_error) <= pid->deadband)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800535 return 0;
536
537 pterm = mul_fp(pid->p_gain, fp_error);
538
539 pid->integral += fp_error;
540
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800541 /*
542 * We limit the integral here so that it will never
543 * get higher than 30. This prevents it from becoming
544 * too large an input over long periods of time and allows
545 * it to get factored out sooner.
546 *
547 * The value of 30 was chosen through experimentation.
548 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800549 integral_limit = int_tofp(30);
550 if (pid->integral > integral_limit)
551 pid->integral = integral_limit;
552 if (pid->integral < -integral_limit)
553 pid->integral = -integral_limit;
554
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700555 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
556 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800557
558 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
Doug Smythies51d211e2014-06-17 13:36:10 -0700559 result = result + (1 << (FRAC_BITS-1));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800560 return (signed int)fp_toint(result);
561}
562
563static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
564{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700565 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
566 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
567 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800568
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700569 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800570}
571
Dirk Brandewie93f08222013-02-06 09:02:13 -0800572static inline void intel_pstate_reset_all_pid(void)
573{
574 unsigned int cpu;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700575
Dirk Brandewie93f08222013-02-06 09:02:13 -0800576 for_each_online_cpu(cpu) {
577 if (all_cpu_data[cpu])
578 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
579 }
580}
581
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700582static inline void update_turbo_state(void)
583{
584 u64 misc_en;
585 struct cpudata *cpu;
586
587 cpu = all_cpu_data[0];
588 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100589 global.turbo_disabled =
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700590 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
591 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
592}
593
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +0100594static int min_perf_pct_min(void)
595{
596 struct cpudata *cpu = all_cpu_data[0];
597
598 return DIV_ROUND_UP(cpu->pstate.min_pstate * 100,
599 cpu->pstate.turbo_pstate);
600}
601
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800602static s16 intel_pstate_get_epb(struct cpudata *cpu_data)
603{
604 u64 epb;
605 int ret;
606
607 if (!static_cpu_has(X86_FEATURE_EPB))
608 return -ENXIO;
609
610 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
611 if (ret)
612 return (s16)ret;
613
614 return (s16)(epb & 0x0f);
615}
616
617static s16 intel_pstate_get_epp(struct cpudata *cpu_data, u64 hwp_req_data)
618{
619 s16 epp;
620
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800621 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
622 /*
623 * When hwp_req_data is 0, means that caller didn't read
624 * MSR_HWP_REQUEST, so need to read and get EPP.
625 */
626 if (!hwp_req_data) {
627 epp = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST,
628 &hwp_req_data);
629 if (epp)
630 return epp;
631 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800632 epp = (hwp_req_data >> 24) & 0xff;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800633 } else {
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800634 /* When there is no EPP present, HWP uses EPB settings */
635 epp = intel_pstate_get_epb(cpu_data);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800636 }
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800637
638 return epp;
639}
640
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800641static int intel_pstate_set_epb(int cpu, s16 pref)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800642{
643 u64 epb;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800644 int ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800645
646 if (!static_cpu_has(X86_FEATURE_EPB))
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800647 return -ENXIO;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800648
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800649 ret = rdmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, &epb);
650 if (ret)
651 return ret;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800652
653 epb = (epb & ~0x0f) | pref;
654 wrmsrl_on_cpu(cpu, MSR_IA32_ENERGY_PERF_BIAS, epb);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800655
656 return 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800657}
658
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800659/*
660 * EPP/EPB display strings corresponding to EPP index in the
661 * energy_perf_strings[]
662 * index String
663 *-------------------------------------
664 * 0 default
665 * 1 performance
666 * 2 balance_performance
667 * 3 balance_power
668 * 4 power
669 */
670static const char * const energy_perf_strings[] = {
671 "default",
672 "performance",
673 "balance_performance",
674 "balance_power",
675 "power",
676 NULL
677};
678
679static int intel_pstate_get_energy_pref_index(struct cpudata *cpu_data)
680{
681 s16 epp;
682 int index = -EINVAL;
683
684 epp = intel_pstate_get_epp(cpu_data, 0);
685 if (epp < 0)
686 return epp;
687
688 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
689 /*
690 * Range:
691 * 0x00-0x3F : Performance
692 * 0x40-0x7F : Balance performance
693 * 0x80-0xBF : Balance power
694 * 0xC0-0xFF : Power
695 * The EPP is a 8 bit value, but our ranges restrict the
696 * value which can be set. Here only using top two bits
697 * effectively.
698 */
699 index = (epp >> 6) + 1;
700 } else if (static_cpu_has(X86_FEATURE_EPB)) {
701 /*
702 * Range:
703 * 0x00-0x03 : Performance
704 * 0x04-0x07 : Balance performance
705 * 0x08-0x0B : Balance power
706 * 0x0C-0x0F : Power
707 * The EPB is a 4 bit value, but our ranges restrict the
708 * value which can be set. Here only using top two bits
709 * effectively.
710 */
711 index = (epp >> 2) + 1;
712 }
713
714 return index;
715}
716
717static int intel_pstate_set_energy_pref_index(struct cpudata *cpu_data,
718 int pref_index)
719{
720 int epp = -EINVAL;
721 int ret;
722
723 if (!pref_index)
724 epp = cpu_data->epp_default;
725
726 mutex_lock(&intel_pstate_limits_lock);
727
728 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
729 u64 value;
730
731 ret = rdmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, &value);
732 if (ret)
733 goto return_pref;
734
735 value &= ~GENMASK_ULL(31, 24);
736
737 /*
738 * If epp is not default, convert from index into
739 * energy_perf_strings to epp value, by shifting 6
740 * bits left to use only top two bits in epp.
741 * The resultant epp need to shifted by 24 bits to
742 * epp position in MSR_HWP_REQUEST.
743 */
744 if (epp == -EINVAL)
745 epp = (pref_index - 1) << 6;
746
747 value |= (u64)epp << 24;
748 ret = wrmsrl_on_cpu(cpu_data->cpu, MSR_HWP_REQUEST, value);
749 } else {
750 if (epp == -EINVAL)
751 epp = (pref_index - 1) << 2;
752 ret = intel_pstate_set_epb(cpu_data->cpu, epp);
753 }
754return_pref:
755 mutex_unlock(&intel_pstate_limits_lock);
756
757 return ret;
758}
759
760static ssize_t show_energy_performance_available_preferences(
761 struct cpufreq_policy *policy, char *buf)
762{
763 int i = 0;
764 int ret = 0;
765
766 while (energy_perf_strings[i] != NULL)
767 ret += sprintf(&buf[ret], "%s ", energy_perf_strings[i++]);
768
769 ret += sprintf(&buf[ret], "\n");
770
771 return ret;
772}
773
774cpufreq_freq_attr_ro(energy_performance_available_preferences);
775
776static ssize_t store_energy_performance_preference(
777 struct cpufreq_policy *policy, const char *buf, size_t count)
778{
779 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
780 char str_preference[21];
781 int ret, i = 0;
782
783 ret = sscanf(buf, "%20s", str_preference);
784 if (ret != 1)
785 return -EINVAL;
786
787 while (energy_perf_strings[i] != NULL) {
788 if (!strcmp(str_preference, energy_perf_strings[i])) {
789 intel_pstate_set_energy_pref_index(cpu_data, i);
790 return count;
791 }
792 ++i;
793 }
794
795 return -EINVAL;
796}
797
798static ssize_t show_energy_performance_preference(
799 struct cpufreq_policy *policy, char *buf)
800{
801 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
802 int preference;
803
804 preference = intel_pstate_get_energy_pref_index(cpu_data);
805 if (preference < 0)
806 return preference;
807
808 return sprintf(buf, "%s\n", energy_perf_strings[preference]);
809}
810
811cpufreq_freq_attr_rw(energy_performance_preference);
812
813static struct freq_attr *hwp_cpufreq_attrs[] = {
814 &energy_performance_preference,
815 &energy_performance_available_preferences,
816 NULL,
817};
818
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100819static void intel_pstate_hwp_set(struct cpufreq_policy *policy)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800820{
Srinivas Pandruvada3f8ed542017-03-13 18:30:12 -0700821 int min, hw_min, max, hw_max, cpu;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700822 u64 value, cap;
823
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100824 for_each_cpu(cpu, policy->cpus) {
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800825 struct cpudata *cpu_data = all_cpu_data[cpu];
826 s16 epp;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700827
Srinivas Pandruvadaf9f48722016-10-08 12:42:38 -0700828 rdmsrl_on_cpu(cpu, MSR_HWP_CAPABILITIES, &cap);
829 hw_min = HWP_LOWEST_PERF(cap);
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100830 if (global.no_turbo)
Srinivas Pandruvada4e5d3f72017-01-18 10:48:24 -0800831 hw_max = HWP_GUARANTEED_PERF(cap);
832 else
833 hw_max = HWP_HIGHEST_PERF(cap);
Srinivas Pandruvadaf9f48722016-10-08 12:42:38 -0700834
Rafael J. Wysockie14cf882017-03-28 00:03:20 +0200835 max = fp_ext_toint(hw_max * cpu_data->max_perf);
Rafael J. Wysocki7de32552017-03-18 00:57:39 +0100836 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE)
837 min = max;
838 else
Rafael J. Wysockie14cf882017-03-28 00:03:20 +0200839 min = fp_ext_toint(hw_max * cpu_data->min_perf);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -0700840
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800841 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
Srinivas Pandruvada3f8ed542017-03-13 18:30:12 -0700842
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800843 value &= ~HWP_MIN_PERF(~0L);
844 value |= HWP_MIN_PERF(min);
845
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800846 value &= ~HWP_MAX_PERF(~0L);
847 value |= HWP_MAX_PERF(max);
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800848
849 if (cpu_data->epp_policy == cpu_data->policy)
850 goto skip_epp;
851
852 cpu_data->epp_policy = cpu_data->policy;
853
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800854 if (cpu_data->epp_saved >= 0) {
855 epp = cpu_data->epp_saved;
856 cpu_data->epp_saved = -EINVAL;
857 goto update_epp;
858 }
859
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800860 if (cpu_data->policy == CPUFREQ_POLICY_PERFORMANCE) {
861 epp = intel_pstate_get_epp(cpu_data, value);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800862 cpu_data->epp_powersave = epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800863 /* If EPP read was failed, then don't try to write */
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800864 if (epp < 0)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800865 goto skip_epp;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800866
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800867
868 epp = 0;
869 } else {
870 /* skip setting EPP, when saved value is invalid */
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800871 if (cpu_data->epp_powersave < 0)
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800872 goto skip_epp;
873
874 /*
875 * No need to restore EPP when it is not zero. This
876 * means:
877 * - Policy is not changed
878 * - user has manually changed
879 * - Error reading EPB
880 */
881 epp = intel_pstate_get_epp(cpu_data, value);
882 if (epp)
883 goto skip_epp;
884
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800885 epp = cpu_data->epp_powersave;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800886 }
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800887update_epp:
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800888 if (static_cpu_has(X86_FEATURE_HWP_EPP)) {
889 value &= ~GENMASK_ULL(31, 24);
890 value |= (u64)epp << 24;
891 } else {
892 intel_pstate_set_epb(cpu, epp);
893 }
894skip_epp:
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800895 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
896 }
Viresh Kumar41cfd642016-02-22 10:27:46 +0530897}
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800898
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -0800899static int intel_pstate_hwp_save_state(struct cpufreq_policy *policy)
900{
901 struct cpudata *cpu_data = all_cpu_data[policy->cpu];
902
903 if (!hwp_active)
904 return 0;
905
906 cpu_data->epp_saved = intel_pstate_get_epp(cpu_data, 0);
907
908 return 0;
909}
910
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800911static int intel_pstate_resume(struct cpufreq_policy *policy)
912{
913 if (!hwp_active)
914 return 0;
915
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100916 mutex_lock(&intel_pstate_limits_lock);
917
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800918 all_cpu_data[policy->cpu]->epp_policy = 0;
Rafael J. Wysocki5f98ced2017-03-09 16:30:38 +0100919 intel_pstate_hwp_set(policy);
Rafael J. Wysockiaa439242016-12-30 15:56:14 +0100920
921 mutex_unlock(&intel_pstate_limits_lock);
922
Rafael J. Wysocki5f98ced2017-03-09 16:30:38 +0100923 return 0;
Srinivas Pandruvada84428852016-11-24 16:07:10 -0800924}
925
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100926static void intel_pstate_update_policies(void)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530927{
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +0100928 int cpu;
929
930 for_each_possible_cpu(cpu)
931 cpufreq_update_policy(cpu);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800932}
933
Dirk Brandewie93f08222013-02-06 09:02:13 -0800934/************************** debugfs begin ************************/
935static int pid_param_set(void *data, u64 val)
936{
937 *(u32 *)data = val;
Rafael J. Wysocki6e7408a2017-03-12 18:12:56 +0100938 pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800939 intel_pstate_reset_all_pid();
940 return 0;
941}
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700942
Dirk Brandewie93f08222013-02-06 09:02:13 -0800943static int pid_param_get(void *data, u64 *val)
944{
945 *val = *(u32 *)data;
946 return 0;
947}
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700948DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -0800949
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100950static struct dentry *debugfs_parent;
951
Dirk Brandewie93f08222013-02-06 09:02:13 -0800952struct pid_param {
953 char *name;
954 void *value;
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100955 struct dentry *dentry;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800956};
957
958static struct pid_param pid_files[] = {
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100959 {"sample_rate_ms", &pid_params.sample_rate_ms, },
960 {"d_gain_pct", &pid_params.d_gain_pct, },
961 {"i_gain_pct", &pid_params.i_gain_pct, },
962 {"deadband", &pid_params.deadband, },
963 {"setpoint", &pid_params.setpoint, },
964 {"p_gain_pct", &pid_params.p_gain_pct, },
965 {NULL, NULL, }
Dirk Brandewie93f08222013-02-06 09:02:13 -0800966};
967
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100968static void intel_pstate_debug_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800969{
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100970 int i;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800971
972 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
973 if (IS_ERR_OR_NULL(debugfs_parent))
974 return;
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100975
976 for (i = 0; pid_files[i].name; i++) {
977 struct dentry *dentry;
978
979 dentry = debugfs_create_file(pid_files[i].name, 0660,
980 debugfs_parent, pid_files[i].value,
981 &fops_pid_param);
982 if (!IS_ERR(dentry))
983 pid_files[i].dentry = dentry;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800984 }
985}
986
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +0100987static void intel_pstate_debug_hide_params(void)
988{
989 int i;
990
991 if (IS_ERR_OR_NULL(debugfs_parent))
992 return;
993
994 for (i = 0; pid_files[i].name; i++) {
995 debugfs_remove(pid_files[i].dentry);
996 pid_files[i].dentry = NULL;
997 }
998
999 debugfs_remove(debugfs_parent);
1000 debugfs_parent = NULL;
1001}
1002
Dirk Brandewie93f08222013-02-06 09:02:13 -08001003/************************** debugfs end ************************/
1004
1005/************************** sysfs begin ************************/
1006#define show_one(file_name, object) \
1007 static ssize_t show_##file_name \
1008 (struct kobject *kobj, struct attribute *attr, char *buf) \
1009 { \
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001010 return sprintf(buf, "%u\n", global.object); \
Dirk Brandewie93f08222013-02-06 09:02:13 -08001011 }
1012
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01001013static ssize_t intel_pstate_show_status(char *buf);
1014static int intel_pstate_update_status(const char *buf, size_t size);
1015
1016static ssize_t show_status(struct kobject *kobj,
1017 struct attribute *attr, char *buf)
1018{
1019 ssize_t ret;
1020
1021 mutex_lock(&intel_pstate_driver_lock);
1022 ret = intel_pstate_show_status(buf);
1023 mutex_unlock(&intel_pstate_driver_lock);
1024
1025 return ret;
1026}
1027
1028static ssize_t store_status(struct kobject *a, struct attribute *b,
1029 const char *buf, size_t count)
1030{
1031 char *p = memchr(buf, '\n', count);
1032 int ret;
1033
1034 mutex_lock(&intel_pstate_driver_lock);
1035 ret = intel_pstate_update_status(buf, p ? p - buf : count);
1036 mutex_unlock(&intel_pstate_driver_lock);
1037
1038 return ret < 0 ? ret : count;
1039}
1040
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001041static ssize_t show_turbo_pct(struct kobject *kobj,
1042 struct attribute *attr, char *buf)
1043{
1044 struct cpudata *cpu;
1045 int total, no_turbo, turbo_pct;
1046 uint32_t turbo_fp;
1047
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001048 mutex_lock(&intel_pstate_driver_lock);
1049
1050 if (!driver_registered) {
1051 mutex_unlock(&intel_pstate_driver_lock);
1052 return -EAGAIN;
1053 }
1054
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001055 cpu = all_cpu_data[0];
1056
1057 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
1058 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001059 turbo_fp = div_fp(no_turbo, total);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001060 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001061
1062 mutex_unlock(&intel_pstate_driver_lock);
1063
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001064 return sprintf(buf, "%u\n", turbo_pct);
1065}
1066
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001067static ssize_t show_num_pstates(struct kobject *kobj,
1068 struct attribute *attr, char *buf)
1069{
1070 struct cpudata *cpu;
1071 int total;
1072
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001073 mutex_lock(&intel_pstate_driver_lock);
1074
1075 if (!driver_registered) {
1076 mutex_unlock(&intel_pstate_driver_lock);
1077 return -EAGAIN;
1078 }
1079
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001080 cpu = all_cpu_data[0];
1081 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001082
1083 mutex_unlock(&intel_pstate_driver_lock);
1084
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001085 return sprintf(buf, "%u\n", total);
1086}
1087
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001088static ssize_t show_no_turbo(struct kobject *kobj,
1089 struct attribute *attr, char *buf)
1090{
1091 ssize_t ret;
1092
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001093 mutex_lock(&intel_pstate_driver_lock);
1094
1095 if (!driver_registered) {
1096 mutex_unlock(&intel_pstate_driver_lock);
1097 return -EAGAIN;
1098 }
1099
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001100 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001101 if (global.turbo_disabled)
1102 ret = sprintf(buf, "%u\n", global.turbo_disabled);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001103 else
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001104 ret = sprintf(buf, "%u\n", global.no_turbo);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001105
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001106 mutex_unlock(&intel_pstate_driver_lock);
1107
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001108 return ret;
1109}
1110
Dirk Brandewie93f08222013-02-06 09:02:13 -08001111static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -07001112 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001113{
1114 unsigned int input;
1115 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001116
Dirk Brandewie93f08222013-02-06 09:02:13 -08001117 ret = sscanf(buf, "%u", &input);
1118 if (ret != 1)
1119 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001120
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001121 mutex_lock(&intel_pstate_driver_lock);
1122
1123 if (!driver_registered) {
1124 mutex_unlock(&intel_pstate_driver_lock);
1125 return -EAGAIN;
1126 }
1127
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -07001128 mutex_lock(&intel_pstate_limits_lock);
1129
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001130 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001131 if (global.turbo_disabled) {
Joe Perches4836df12016-04-05 13:28:23 -07001132 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -07001133 mutex_unlock(&intel_pstate_limits_lock);
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001134 mutex_unlock(&intel_pstate_driver_lock);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -07001135 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -07001136 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001137
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001138 global.no_turbo = clamp_t(int, input, 0, 1);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +01001139
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001140 if (global.no_turbo) {
1141 struct cpudata *cpu = all_cpu_data[0];
1142 int pct = cpu->pstate.max_pstate * 100 / cpu->pstate.turbo_pstate;
1143
1144 /* Squash the global minimum into the permitted range. */
1145 if (global.min_perf_pct > pct)
1146 global.min_perf_pct = pct;
1147 }
1148
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +01001149 mutex_unlock(&intel_pstate_limits_lock);
1150
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001151 intel_pstate_update_policies();
1152
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001153 mutex_unlock(&intel_pstate_driver_lock);
1154
Dirk Brandewie93f08222013-02-06 09:02:13 -08001155 return count;
1156}
1157
1158static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -07001159 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001160{
1161 unsigned int input;
1162 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001163
Dirk Brandewie93f08222013-02-06 09:02:13 -08001164 ret = sscanf(buf, "%u", &input);
1165 if (ret != 1)
1166 return -EINVAL;
1167
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001168 mutex_lock(&intel_pstate_driver_lock);
1169
1170 if (!driver_registered) {
1171 mutex_unlock(&intel_pstate_driver_lock);
1172 return -EAGAIN;
1173 }
1174
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -07001175 mutex_lock(&intel_pstate_limits_lock);
1176
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001177 global.max_perf_pct = clamp_t(int, input, global.min_perf_pct, 100);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +01001178
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +01001179 mutex_unlock(&intel_pstate_limits_lock);
1180
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001181 intel_pstate_update_policies();
1182
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001183 mutex_unlock(&intel_pstate_driver_lock);
1184
Dirk Brandewie93f08222013-02-06 09:02:13 -08001185 return count;
1186}
1187
1188static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -07001189 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001190{
1191 unsigned int input;
1192 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001193
Dirk Brandewie93f08222013-02-06 09:02:13 -08001194 ret = sscanf(buf, "%u", &input);
1195 if (ret != 1)
1196 return -EINVAL;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -08001197
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001198 mutex_lock(&intel_pstate_driver_lock);
1199
1200 if (!driver_registered) {
1201 mutex_unlock(&intel_pstate_driver_lock);
1202 return -EAGAIN;
1203 }
1204
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -07001205 mutex_lock(&intel_pstate_limits_lock);
1206
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001207 global.min_perf_pct = clamp_t(int, input,
1208 min_perf_pct_min(), global.max_perf_pct);
Rafael J. Wysocki111b8b32016-12-30 15:58:21 +01001209
Rafael J. Wysockicd59b4be2017-03-01 00:07:36 +01001210 mutex_unlock(&intel_pstate_limits_lock);
1211
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001212 intel_pstate_update_policies();
1213
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01001214 mutex_unlock(&intel_pstate_driver_lock);
1215
Dirk Brandewie93f08222013-02-06 09:02:13 -08001216 return count;
1217}
1218
Dirk Brandewie93f08222013-02-06 09:02:13 -08001219show_one(max_perf_pct, max_perf_pct);
1220show_one(min_perf_pct, min_perf_pct);
1221
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01001222define_one_global_rw(status);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001223define_one_global_rw(no_turbo);
1224define_one_global_rw(max_perf_pct);
1225define_one_global_rw(min_perf_pct);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001226define_one_global_ro(turbo_pct);
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001227define_one_global_ro(num_pstates);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001228
1229static struct attribute *intel_pstate_attributes[] = {
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01001230 &status.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001231 &no_turbo.attr,
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -08001232 &turbo_pct.attr,
Kristen Carlson Accardi05224242015-01-28 15:03:28 -08001233 &num_pstates.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001234 NULL
1235};
1236
1237static struct attribute_group intel_pstate_attr_group = {
1238 .attrs = intel_pstate_attributes,
1239};
Dirk Brandewie93f08222013-02-06 09:02:13 -08001240
Stratos Karafotis317dd502014-07-18 08:37:17 -07001241static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001242{
Stratos Karafotis317dd502014-07-18 08:37:17 -07001243 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001244 int rc;
1245
1246 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
1247 &cpu_subsys.dev_root->kobj);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001248 if (WARN_ON(!intel_pstate_kobject))
1249 return;
1250
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -07001251 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001252 if (WARN_ON(rc))
1253 return;
1254
1255 /*
1256 * If per cpu limits are enforced there are no global limits, so
1257 * return without creating max/min_perf_pct attributes
1258 */
1259 if (per_cpu_limits)
1260 return;
1261
1262 rc = sysfs_create_file(intel_pstate_kobject, &max_perf_pct.attr);
1263 WARN_ON(rc);
1264
1265 rc = sysfs_create_file(intel_pstate_kobject, &min_perf_pct.attr);
1266 WARN_ON(rc);
1267
Dirk Brandewie93f08222013-02-06 09:02:13 -08001268}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001269/************************** sysfs end ************************/
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001270
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001271static void intel_pstate_hwp_enable(struct cpudata *cpudata)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001272{
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001273 /* First disable HWP notification interrupt as we don't process them */
Srinivas Pandruvadada7de912016-07-19 16:52:01 -07001274 if (static_cpu_has(X86_FEATURE_HWP_NOTIFY))
1275 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -08001276
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001277 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
Srinivas Pandruvada84428852016-11-24 16:07:10 -08001278 cpudata->epp_policy = 0;
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001279 if (cpudata->epp_default == -EINVAL)
1280 cpudata->epp_default = intel_pstate_get_epp(cpudata, 0);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001281}
1282
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001283#define MSR_IA32_POWER_CTL_BIT_EE 19
1284
1285/* Disable energy efficiency optimization */
1286static void intel_pstate_disable_ee(int cpu)
1287{
1288 u64 power_ctl;
1289 int ret;
1290
1291 ret = rdmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, &power_ctl);
1292 if (ret)
1293 return;
1294
1295 if (!(power_ctl & BIT(MSR_IA32_POWER_CTL_BIT_EE))) {
1296 pr_info("Disabling energy efficiency optimization\n");
1297 power_ctl |= BIT(MSR_IA32_POWER_CTL_BIT_EE);
1298 wrmsrl_on_cpu(cpu, MSR_IA32_POWER_CTL, power_ctl);
1299 }
1300}
1301
Philippe Longepe938d21a2015-11-09 17:40:46 -08001302static int atom_get_min_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001303{
1304 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001305
Len Brown92134bd2017-02-25 16:55:17 -05001306 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001307 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001308}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001309
Philippe Longepe938d21a2015-11-09 17:40:46 -08001310static int atom_get_max_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001311{
1312 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001313
Len Brown92134bd2017-02-25 16:55:17 -05001314 rdmsrl(MSR_ATOM_CORE_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001315 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001316}
1317
Philippe Longepe938d21a2015-11-09 17:40:46 -08001318static int atom_get_turbo_pstate(void)
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001319{
1320 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001321
Len Brown92134bd2017-02-25 16:55:17 -05001322 rdmsrl(MSR_ATOM_CORE_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001323 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -08001324}
1325
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001326static u64 atom_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001327{
1328 u64 val;
1329 int32_t vid_fp;
1330 u32 vid;
1331
Chen Yu144c8e12015-07-29 23:53:10 +08001332 val = (u64)pstate << 8;
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001333 if (global.no_turbo && !global.turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001334 val |= (u64)1 << 32;
1335
1336 vid_fp = cpudata->vid.min + mul_fp(
1337 int_tofp(pstate - cpudata->pstate.min_pstate),
1338 cpudata->vid.ratio);
1339
1340 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
Dirk Brandewied022a652014-10-13 08:37:44 -07001341 vid = ceiling_fp(vid_fp);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001342
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001343 if (pstate > cpudata->pstate.max_pstate)
1344 vid = cpudata->vid.turbo;
1345
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001346 return val | vid;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001347}
1348
Philippe Longepe1421df62015-11-09 17:40:47 -08001349static int silvermont_get_scaling(void)
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001350{
1351 u64 value;
1352 int i;
Philippe Longepe1421df62015-11-09 17:40:47 -08001353 /* Defined in Table 35-6 from SDM (Sept 2015) */
1354 static int silvermont_freq_table[] = {
1355 83300, 100000, 133300, 116700, 80000};
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001356
1357 rdmsrl(MSR_FSB_FREQ, value);
Philippe Longepe1421df62015-11-09 17:40:47 -08001358 i = value & 0x7;
1359 WARN_ON(i > 4);
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001360
Philippe Longepe1421df62015-11-09 17:40:47 -08001361 return silvermont_freq_table[i];
1362}
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001363
Philippe Longepe1421df62015-11-09 17:40:47 -08001364static int airmont_get_scaling(void)
1365{
1366 u64 value;
1367 int i;
1368 /* Defined in Table 35-10 from SDM (Sept 2015) */
1369 static int airmont_freq_table[] = {
1370 83300, 100000, 133300, 116700, 80000,
1371 93300, 90000, 88900, 87500};
1372
1373 rdmsrl(MSR_FSB_FREQ, value);
1374 i = value & 0xF;
1375 WARN_ON(i > 8);
1376
1377 return airmont_freq_table[i];
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001378}
1379
Philippe Longepe938d21a2015-11-09 17:40:46 -08001380static void atom_get_vid(struct cpudata *cpudata)
Dirk Brandewie007bea02013-12-18 10:32:39 -08001381{
1382 u64 value;
1383
Len Brown92134bd2017-02-25 16:55:17 -05001384 rdmsrl(MSR_ATOM_CORE_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -07001385 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
1386 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -08001387 cpudata->vid.ratio = div_fp(
1388 cpudata->vid.max - cpudata->vid.min,
1389 int_tofp(cpudata->pstate.max_pstate -
1390 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001391
Len Brown92134bd2017-02-25 16:55:17 -05001392 rdmsrl(MSR_ATOM_CORE_TURBO_VIDS, value);
Dirk Brandewie21855ff2014-05-08 12:57:23 -07001393 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001394}
1395
Dirk Brandewie016c8152013-10-21 09:20:34 -07001396static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001397{
1398 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001399
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001400 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001401 return (value >> 40) & 0xFF;
1402}
1403
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001404static int core_get_max_pstate_physical(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001405{
1406 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001407
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +00001408 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001409 return (value >> 8) & 0xFF;
1410}
1411
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001412static int core_get_tdp_ratio(u64 plat_info)
1413{
1414 /* Check how many TDP levels present */
1415 if (plat_info & 0x600000000) {
1416 u64 tdp_ctrl;
1417 u64 tdp_ratio;
1418 int tdp_msr;
1419 int err;
1420
1421 /* Get the TDP level (0, 1, 2) to get ratios */
1422 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
1423 if (err)
1424 return err;
1425
1426 /* TDP MSR are continuous starting at 0x648 */
1427 tdp_msr = MSR_CONFIG_TDP_NOMINAL + (tdp_ctrl & 0x03);
1428 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
1429 if (err)
1430 return err;
1431
1432 /* For level 1 and 2, bits[23:16] contain the ratio */
1433 if (tdp_ctrl & 0x03)
1434 tdp_ratio >>= 16;
1435
1436 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
1437 pr_debug("tdp_ratio %x\n", (int)tdp_ratio);
1438
1439 return (int)tdp_ratio;
1440 }
1441
1442 return -ENXIO;
1443}
1444
Dirk Brandewie93f08222013-02-06 09:02:13 -08001445static int core_get_max_pstate(void)
1446{
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001447 u64 tar;
1448 u64 plat_info;
1449 int max_pstate;
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001450 int tdp_ratio;
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001451 int err;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001452
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001453 rdmsrl(MSR_PLATFORM_INFO, plat_info);
1454 max_pstate = (plat_info >> 8) & 0xFF;
1455
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001456 tdp_ratio = core_get_tdp_ratio(plat_info);
1457 if (tdp_ratio <= 0)
1458 return max_pstate;
1459
1460 if (hwp_active) {
1461 /* Turbo activation ratio is not used on HWP platforms */
1462 return tdp_ratio;
1463 }
1464
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001465 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
1466 if (!err) {
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001467 int tar_levels;
1468
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001469 /* Do some sanity checking for safety */
Srinivas Pandruvada8fc75542017-01-19 15:03:14 -08001470 tar_levels = tar & 0xff;
1471 if (tdp_ratio - 1 == tar_levels) {
1472 max_pstate = tar_levels;
1473 pr_debug("max_pstate=TAC %x\n", max_pstate);
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001474 }
1475 }
1476
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -07001477 return max_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001478}
1479
Dirk Brandewie016c8152013-10-21 09:20:34 -07001480static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001481{
1482 u64 value;
1483 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001484
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001485 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001486 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -07001487 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001488 if (ret <= nont)
1489 ret = nont;
1490 return ret;
1491}
1492
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001493static inline int core_get_scaling(void)
1494{
1495 return 100000;
1496}
1497
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001498static u64 core_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001499{
1500 u64 val;
1501
Chen Yu144c8e12015-07-29 23:53:10 +08001502 val = (u64)pstate << 8;
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001503 if (global.no_turbo && !global.turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001504 val |= (u64)1 << 32;
1505
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001506 return val;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001507}
1508
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001509static int knl_get_turbo_pstate(void)
1510{
1511 u64 value;
1512 int nont, ret;
1513
Srinivas Pandruvada100cf6f2016-07-06 16:07:55 -07001514 rdmsrl(MSR_TURBO_RATIO_LIMIT, value);
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001515 nont = core_get_max_pstate();
1516 ret = (((value) >> 8) & 0xFF);
1517 if (ret <= nont)
1518 ret = nont;
1519 return ret;
1520}
1521
Dirk Brandewie016c8152013-10-21 09:20:34 -07001522static struct cpu_defaults core_params = {
1523 .pid_policy = {
1524 .sample_rate_ms = 10,
1525 .deadband = 0,
1526 .setpoint = 97,
1527 .p_gain_pct = 20,
1528 .d_gain_pct = 0,
1529 .i_gain_pct = 0,
1530 },
1531 .funcs = {
1532 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001533 .get_max_physical = core_get_max_pstate_physical,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001534 .get_min = core_get_min_pstate,
1535 .get_turbo = core_get_turbo_pstate,
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001536 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001537 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001538 .get_target_pstate = get_target_pstate_use_performance,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001539 },
1540};
1541
Julia Lawall42ce8922016-09-11 15:06:01 +02001542static const struct cpu_defaults silvermont_params = {
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001543 .pid_policy = {
1544 .sample_rate_ms = 10,
1545 .deadband = 0,
Kristen Carlson Accardi6a82ba62015-04-10 11:06:43 -07001546 .setpoint = 60,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001547 .p_gain_pct = 14,
1548 .d_gain_pct = 0,
1549 .i_gain_pct = 4,
1550 },
1551 .funcs = {
Philippe Longepe938d21a2015-11-09 17:40:46 -08001552 .get_max = atom_get_max_pstate,
1553 .get_max_physical = atom_get_max_pstate,
1554 .get_min = atom_get_min_pstate,
1555 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001556 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001557 .get_scaling = silvermont_get_scaling,
1558 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001559 .get_target_pstate = get_target_pstate_use_cpu_load,
Philippe Longepe1421df62015-11-09 17:40:47 -08001560 },
1561};
1562
Julia Lawall42ce8922016-09-11 15:06:01 +02001563static const struct cpu_defaults airmont_params = {
Philippe Longepe1421df62015-11-09 17:40:47 -08001564 .pid_policy = {
1565 .sample_rate_ms = 10,
1566 .deadband = 0,
1567 .setpoint = 60,
1568 .p_gain_pct = 14,
1569 .d_gain_pct = 0,
1570 .i_gain_pct = 4,
1571 },
1572 .funcs = {
1573 .get_max = atom_get_max_pstate,
1574 .get_max_physical = atom_get_max_pstate,
1575 .get_min = atom_get_min_pstate,
1576 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001577 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001578 .get_scaling = airmont_get_scaling,
Philippe Longepe938d21a2015-11-09 17:40:46 -08001579 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001580 .get_target_pstate = get_target_pstate_use_cpu_load,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001581 },
1582};
1583
Julia Lawall42ce8922016-09-11 15:06:01 +02001584static const struct cpu_defaults knl_params = {
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001585 .pid_policy = {
1586 .sample_rate_ms = 10,
1587 .deadband = 0,
1588 .setpoint = 97,
1589 .p_gain_pct = 20,
1590 .d_gain_pct = 0,
1591 .i_gain_pct = 0,
1592 },
1593 .funcs = {
1594 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001595 .get_max_physical = core_get_max_pstate_physical,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001596 .get_min = core_get_min_pstate,
1597 .get_turbo = knl_get_turbo_pstate,
Lukasz Anaczkowski69cefc22015-07-21 10:41:13 +02001598 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001599 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001600 .get_target_pstate = get_target_pstate_use_performance,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001601 },
1602};
1603
Julia Lawall42ce8922016-09-11 15:06:01 +02001604static const struct cpu_defaults bxt_params = {
Srinivas Pandruvada41bad472016-06-09 15:34:41 -07001605 .pid_policy = {
1606 .sample_rate_ms = 10,
1607 .deadband = 0,
1608 .setpoint = 60,
1609 .p_gain_pct = 14,
1610 .d_gain_pct = 0,
1611 .i_gain_pct = 4,
1612 },
1613 .funcs = {
1614 .get_max = core_get_max_pstate,
1615 .get_max_physical = core_get_max_pstate_physical,
1616 .get_min = core_get_min_pstate,
1617 .get_turbo = core_get_turbo_pstate,
1618 .get_scaling = core_get_scaling,
1619 .get_val = core_get_val,
1620 .get_target_pstate = get_target_pstate_use_cpu_load,
1621 },
1622};
1623
Dirk Brandewie93f08222013-02-06 09:02:13 -08001624static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
1625{
1626 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie7244cb62013-10-21 09:20:33 -07001627 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001628 int min_perf;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001629
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001630 if (global.no_turbo || global.turbo_disabled)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001631 max_perf = cpu->pstate.max_pstate;
1632
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001633 /*
1634 * performance can be limited by user through sysfs, by cpufreq
1635 * policy, or by cpu specific default values determined through
1636 * experimentation.
1637 */
Rafael J. Wysockie14cf882017-03-28 00:03:20 +02001638 max_perf_adj = fp_ext_toint(max_perf * cpu->max_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001639 *max = clamp_t(int, max_perf_adj,
1640 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001641
Rafael J. Wysockie14cf882017-03-28 00:03:20 +02001642 min_perf = fp_ext_toint(max_perf * cpu->min_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001643 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001644}
1645
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001646static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001647{
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001648 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
1649 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001650 /*
1651 * Generally, there is no guarantee that this code will always run on
1652 * the CPU being updated, so force the register update to run on the
1653 * right CPU.
1654 */
1655 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1656 pstate_funcs.get_val(cpu, pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001657}
1658
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02001659static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1660{
1661 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
1662}
1663
1664static void intel_pstate_max_within_limits(struct cpudata *cpu)
1665{
1666 int min_pstate, max_pstate;
1667
1668 update_turbo_state();
1669 intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate);
1670 intel_pstate_set_pstate(cpu, max_pstate);
1671}
1672
Dirk Brandewie93f08222013-02-06 09:02:13 -08001673static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1674{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001675 cpu->pstate.min_pstate = pstate_funcs.get_min();
1676 cpu->pstate.max_pstate = pstate_funcs.get_max();
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001677 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
Dirk Brandewie016c8152013-10-21 09:20:34 -07001678 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001679 cpu->pstate.scaling = pstate_funcs.get_scaling();
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001680 cpu->pstate.max_freq = cpu->pstate.max_pstate * cpu->pstate.scaling;
1681 cpu->pstate.turbo_freq = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001682
Dirk Brandewie007bea02013-12-18 10:32:39 -08001683 if (pstate_funcs.get_vid)
1684 pstate_funcs.get_vid(cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001685
1686 intel_pstate_set_min_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001687}
1688
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001689static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001690{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +03001691 struct sample *sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001692
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001693 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001694}
1695
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001696static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001697{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001698 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001699 unsigned long flags;
Doug Smythies4055fad2015-04-11 21:10:26 -07001700 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001701
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001702 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001703 rdmsrl(MSR_IA32_APERF, aperf);
1704 rdmsrl(MSR_IA32_MPERF, mperf);
Philippe Longepee70eed22015-12-04 17:40:32 +01001705 tsc = rdtsc();
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001706 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001707 local_irq_restore(flags);
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001708 return false;
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001709 }
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001710 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001711
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001712 cpu->last_sample_time = cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001713 cpu->sample.time = time;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001714 cpu->sample.aperf = aperf;
1715 cpu->sample.mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001716 cpu->sample.tsc = tsc;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001717 cpu->sample.aperf -= cpu->prev_aperf;
1718 cpu->sample.mperf -= cpu->prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001719 cpu->sample.tsc -= cpu->prev_tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001720
Dirk Brandewie93f08222013-02-06 09:02:13 -08001721 cpu->prev_aperf = aperf;
1722 cpu->prev_mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001723 cpu->prev_tsc = tsc;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001724 /*
1725 * First time this function is invoked in a given cycle, all of the
1726 * previous sample data fields are equal to zero or stale and they must
1727 * be populated with meaningful numbers for things to work, so assume
1728 * that sample.time will always be reset before setting the utilization
1729 * update hook and make the caller skip the sample then.
1730 */
1731 return !!cpu->last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001732}
1733
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001734static inline int32_t get_avg_frequency(struct cpudata *cpu)
1735{
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001736 return mul_ext_fp(cpu->sample.core_avg_perf,
1737 cpu->pstate.max_pstate_physical * cpu->pstate.scaling);
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001738}
1739
Philippe Longepebdcaa232016-04-22 11:46:09 -07001740static inline int32_t get_avg_pstate(struct cpudata *cpu)
1741{
Rafael J. Wysocki8edb0a62016-05-11 19:10:42 +02001742 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1743 cpu->sample.core_avg_perf);
Philippe Longepebdcaa232016-04-22 11:46:09 -07001744}
1745
Philippe Longepee70eed22015-12-04 17:40:32 +01001746static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
1747{
1748 struct sample *sample = &cpu->sample;
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001749 int32_t busy_frac, boost;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001750 int target, avg_pstate;
Philippe Longepee70eed22015-12-04 17:40:32 +01001751
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001752 busy_frac = div_fp(sample->mperf, sample->tsc);
Philippe Longepe63d1d652015-12-04 17:40:35 +01001753
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001754 boost = cpu->iowait_boost;
1755 cpu->iowait_boost >>= 1;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001756
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001757 if (busy_frac < boost)
1758 busy_frac = boost;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001759
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001760 sample->busy_scaled = busy_frac * 100;
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001761
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01001762 target = global.no_turbo || global.turbo_disabled ?
Rafael J. Wysocki0843e832016-10-06 14:07:51 +02001763 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
1764 target += target >> 2;
1765 target = mul_fp(target, busy_frac);
1766 if (target < cpu->pstate.min_pstate)
1767 target = cpu->pstate.min_pstate;
1768
1769 /*
1770 * If the average P-state during the previous cycle was higher than the
1771 * current target, add 50% of the difference to the target to reduce
1772 * possible performance oscillations and offset possible performance
1773 * loss related to moving the workload from one CPU to another within
1774 * a package/module.
1775 */
1776 avg_pstate = get_avg_pstate(cpu);
1777 if (avg_pstate > target)
1778 target += (avg_pstate - target) >> 1;
1779
1780 return target;
Philippe Longepee70eed22015-12-04 17:40:32 +01001781}
1782
Philippe Longepe157386b2015-12-04 17:40:30 +01001783static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001784{
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001785 int32_t perf_scaled, max_pstate, current_pstate, sample_ratio;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001786 u64 duration_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001787
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001788 /*
Rafael J. Wysockif00593a2016-09-30 03:13:34 +02001789 * perf_scaled is the ratio of the average P-state during the last
1790 * sampling period to the P-state requested last time (in percent).
1791 *
1792 * That measures the system's response to the previous P-state
1793 * selection.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001794 */
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001795 max_pstate = cpu->pstate.max_pstate_physical;
1796 current_pstate = cpu->pstate.current_pstate;
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001797 perf_scaled = mul_ext_fp(cpu->sample.core_avg_perf,
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001798 div_fp(100 * max_pstate, current_pstate));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001799
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001800 /*
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001801 * Since our utilization update callback will not run unless we are
1802 * in C0, check if the actual elapsed time is significantly greater (3x)
1803 * than our sample interval. If it is, then we were idle for a long
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001804 * enough period of time to adjust our performance metric.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001805 */
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001806 duration_ns = cpu->sample.time - cpu->last_sample_time;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001807 if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001808 sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns);
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001809 perf_scaled = mul_fp(perf_scaled, sample_ratio);
Rafael J. Wysockiffb81052016-04-10 05:59:10 +02001810 } else {
1811 sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc);
1812 if (sample_ratio < int_tofp(1))
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001813 perf_scaled = 0;
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001814 }
1815
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001816 cpu->sample.busy_scaled = perf_scaled;
1817 return cpu->pstate.current_pstate - pid_calc(&cpu->pid, perf_scaled);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001818}
1819
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001820static int intel_pstate_prepare_request(struct cpudata *cpu, int pstate)
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001821{
1822 int max_perf, min_perf;
1823
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001824 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
1825 pstate = clamp_t(int, pstate, min_perf, max_perf);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001826 return pstate;
1827}
1828
1829static void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1830{
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001831 if (pstate == cpu->pstate.current_pstate)
1832 return;
1833
Rafael J. Wysockibc95a452016-07-19 15:10:37 +02001834 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001835 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1836}
1837
Dirk Brandewie93f08222013-02-06 09:02:13 -08001838static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
1839{
Philippe Longepe157386b2015-12-04 17:40:30 +01001840 int from, target_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001841 struct sample *sample;
1842
1843 from = cpu->pstate.current_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001844
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02001845 target_pstate = cpu->policy == CPUFREQ_POLICY_PERFORMANCE ?
1846 cpu->pstate.turbo_pstate : pstate_funcs.get_target_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001847
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01001848 update_turbo_state();
1849
Rafael J. Wysocki64078292017-03-03 23:51:31 +01001850 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
1851 trace_cpu_frequency(target_pstate * cpu->pstate.scaling, cpu->cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001852 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies4055fad2015-04-11 21:10:26 -07001853
1854 sample = &cpu->sample;
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001855 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
Philippe Longepe157386b2015-12-04 17:40:30 +01001856 fp_toint(sample->busy_scaled),
Doug Smythies4055fad2015-04-11 21:10:26 -07001857 from,
1858 cpu->pstate.current_pstate,
1859 sample->mperf,
1860 sample->aperf,
1861 sample->tsc,
Srinivas Pandruvada3ba7bca2016-09-13 17:41:33 -07001862 get_avg_frequency(cpu),
1863 fp_toint(cpu->iowait_boost * 100));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001864}
1865
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001866static void intel_pstate_update_util(struct update_util_data *data, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02001867 unsigned int flags)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001868{
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001869 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001870 u64 delta_ns;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001871
Rafael J. Wysocki1d298152016-10-19 02:53:26 +02001872 if (pstate_funcs.get_target_pstate == get_target_pstate_use_cpu_load) {
Rafael J. Wysocki09c448d2016-09-14 02:28:13 +02001873 if (flags & SCHED_CPUFREQ_IOWAIT) {
1874 cpu->iowait_boost = int_tofp(1);
1875 } else if (cpu->iowait_boost) {
1876 /* Clear iowait_boost if the CPU may have been idle. */
1877 delta_ns = time - cpu->last_update;
1878 if (delta_ns > TICK_NSEC)
1879 cpu->iowait_boost = 0;
1880 }
1881 cpu->last_update = time;
1882 }
1883
1884 delta_ns = time - cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001885 if ((s64)delta_ns >= pid_params.sample_rate_ns) {
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001886 bool sample_taken = intel_pstate_sample(cpu, time);
1887
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001888 if (sample_taken) {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001889 intel_pstate_calc_avg_perf(cpu);
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001890 if (!hwp_active)
1891 intel_pstate_adjust_busy_pstate(cpu);
1892 }
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001893 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001894}
1895
1896#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -08001897 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1898 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001899
1900static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dave Hansen5b20c942016-06-02 17:19:45 -07001901 ICPU(INTEL_FAM6_SANDYBRIDGE, core_params),
1902 ICPU(INTEL_FAM6_SANDYBRIDGE_X, core_params),
1903 ICPU(INTEL_FAM6_ATOM_SILVERMONT1, silvermont_params),
1904 ICPU(INTEL_FAM6_IVYBRIDGE, core_params),
1905 ICPU(INTEL_FAM6_HASWELL_CORE, core_params),
1906 ICPU(INTEL_FAM6_BROADWELL_CORE, core_params),
1907 ICPU(INTEL_FAM6_IVYBRIDGE_X, core_params),
1908 ICPU(INTEL_FAM6_HASWELL_X, core_params),
1909 ICPU(INTEL_FAM6_HASWELL_ULT, core_params),
1910 ICPU(INTEL_FAM6_HASWELL_GT3E, core_params),
1911 ICPU(INTEL_FAM6_BROADWELL_GT3E, core_params),
1912 ICPU(INTEL_FAM6_ATOM_AIRMONT, airmont_params),
1913 ICPU(INTEL_FAM6_SKYLAKE_MOBILE, core_params),
1914 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1915 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP, core_params),
1916 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
1917 ICPU(INTEL_FAM6_XEON_PHI_KNL, knl_params),
Piotr Luc58bf4542016-10-13 17:31:00 +02001918 ICPU(INTEL_FAM6_XEON_PHI_KNM, knl_params),
Srinivas Pandruvada41bad472016-06-09 15:34:41 -07001919 ICPU(INTEL_FAM6_ATOM_GOLDMONT, bxt_params),
Dirk Brandewie93f08222013-02-06 09:02:13 -08001920 {}
1921};
1922MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1923
Jisheng Zhang29327c82016-06-27 18:07:17 +08001924static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] __initconst = {
Dave Hansen5b20c942016-06-02 17:19:45 -07001925 ICPU(INTEL_FAM6_BROADWELL_XEON_D, core_params),
Srinivas Pandruvada65c12622016-07-23 15:12:06 -07001926 ICPU(INTEL_FAM6_BROADWELL_X, core_params),
1927 ICPU(INTEL_FAM6_SKYLAKE_X, core_params),
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001928 {}
1929};
1930
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001931static const struct x86_cpu_id intel_pstate_cpu_ee_disable_ids[] = {
1932 ICPU(INTEL_FAM6_KABYLAKE_DESKTOP, core_params),
1933 {}
1934};
1935
Dirk Brandewie93f08222013-02-06 09:02:13 -08001936static int intel_pstate_init_cpu(unsigned int cpunum)
1937{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001938 struct cpudata *cpu;
1939
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001940 cpu = all_cpu_data[cpunum];
1941
1942 if (!cpu) {
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01001943 cpu = kzalloc(sizeof(*cpu), GFP_KERNEL);
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001944 if (!cpu)
1945 return -ENOMEM;
1946
1947 all_cpu_data[cpunum] = cpu;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001948
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08001949 cpu->epp_default = -EINVAL;
1950 cpu->epp_powersave = -EINVAL;
1951 cpu->epp_saved = -EINVAL;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07001952 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001953
1954 cpu = all_cpu_data[cpunum];
1955
Dirk Brandewie93f08222013-02-06 09:02:13 -08001956 cpu->cpu = cpunum;
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001957
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001958 if (hwp_active) {
Srinivas Pandruvada6e978b22017-02-03 14:18:39 -08001959 const struct x86_cpu_id *id;
1960
1961 id = x86_match_cpu(intel_pstate_cpu_ee_disable_ids);
1962 if (id)
1963 intel_pstate_disable_ee(cpunum);
1964
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001965 intel_pstate_hwp_enable(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001966 pid_params.sample_rate_ms = 50;
1967 pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
1968 }
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001969
Vincent Minet179e8472014-07-05 01:51:33 +02001970 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001971
Dirk Brandewie93f08222013-02-06 09:02:13 -08001972 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001973
Joe Perches4836df12016-04-05 13:28:23 -07001974 pr_debug("controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001975
1976 return 0;
1977}
1978
1979static unsigned int intel_pstate_get(unsigned int cpu_num)
1980{
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001981 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -08001982
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001983 return cpu ? get_avg_frequency(cpu) : 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001984}
1985
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001986static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001987{
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001988 struct cpudata *cpu = all_cpu_data[cpu_num];
1989
Rafael J. Wysocki5ab666e2016-06-27 23:47:15 +02001990 if (cpu->update_util_set)
1991 return;
1992
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001993 /* Prevent intel_pstate_update_util() from using stale data. */
1994 cpu->sample.time = 0;
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001995 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
1996 intel_pstate_update_util);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001997 cpu->update_util_set = true;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001998}
1999
2000static void intel_pstate_clear_update_util_hook(unsigned int cpu)
2001{
Chen Yu4578ee7e2016-05-11 14:33:08 +08002002 struct cpudata *cpu_data = all_cpu_data[cpu];
2003
2004 if (!cpu_data->update_util_set)
2005 return;
2006
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02002007 cpufreq_remove_update_util_hook(cpu);
Chen Yu4578ee7e2016-05-11 14:33:08 +08002008 cpu_data->update_util_set = false;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02002009 synchronize_sched();
2010}
2011
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002012static int intel_pstate_get_max_freq(struct cpudata *cpu)
2013{
2014 return global.turbo_disabled || global.no_turbo ?
2015 cpu->pstate.max_freq : cpu->pstate.turbo_freq;
2016}
2017
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002018static void intel_pstate_update_perf_limits(struct cpufreq_policy *policy,
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002019 struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08002020{
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002021 int max_freq = intel_pstate_get_max_freq(cpu);
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01002022 int32_t max_policy_perf, min_policy_perf;
Srinivas Pandruvadaa410c032016-10-28 10:44:52 -07002023
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002024 max_policy_perf = div_ext_fp(policy->max, max_freq);
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01002025 max_policy_perf = clamp_t(int32_t, max_policy_perf, 0, int_ext_tofp(1));
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07002026 if (policy->max == policy->min) {
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01002027 min_policy_perf = max_policy_perf;
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07002028 } else {
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002029 min_policy_perf = div_ext_fp(policy->min, max_freq);
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01002030 min_policy_perf = clamp_t(int32_t, min_policy_perf,
2031 0, max_policy_perf);
Srinivas Pandruvada5879f872016-10-25 13:20:41 -07002032 }
Chen Yu43717aa2015-09-09 18:27:31 +08002033
Rafael J. Wysockie4c204c2017-03-14 16:18:34 +01002034 /* Normalize user input to [min_perf, max_perf] */
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002035 if (per_cpu_limits) {
Rafael J. Wysockie14cf882017-03-28 00:03:20 +02002036 cpu->min_perf = min_policy_perf;
2037 cpu->max_perf = max_policy_perf;
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002038 } else {
2039 int32_t global_min, global_max;
Chen Yu43717aa2015-09-09 18:27:31 +08002040
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002041 /* Global limits are in percent of the maximum turbo P-state. */
2042 global_max = percent_ext_fp(global.max_perf_pct);
2043 global_min = percent_ext_fp(global.min_perf_pct);
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002044 if (max_freq != cpu->pstate.turbo_freq) {
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002045 int32_t turbo_factor;
2046
2047 turbo_factor = div_ext_fp(cpu->pstate.turbo_pstate,
2048 cpu->pstate.max_pstate);
2049 global_min = mul_ext_fp(global_min, turbo_factor);
2050 global_max = mul_ext_fp(global_max, turbo_factor);
2051 }
2052 global_min = clamp_t(int32_t, global_min, 0, global_max);
2053
Rafael J. Wysockie14cf882017-03-28 00:03:20 +02002054 cpu->min_perf = max(min_policy_perf, global_min);
2055 cpu->min_perf = min(cpu->min_perf, max_policy_perf);
2056 cpu->max_perf = min(max_policy_perf, global_max);
2057 cpu->max_perf = max(min_policy_perf, cpu->max_perf);
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002058
2059 /* Make sure min_perf <= max_perf */
Rafael J. Wysockie14cf882017-03-28 00:03:20 +02002060 cpu->min_perf = min(cpu->min_perf, cpu->max_perf);
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002061 }
Chen Yu43717aa2015-09-09 18:27:31 +08002062
Rafael J. Wysockie14cf882017-03-28 00:03:20 +02002063 cpu->max_perf = round_up(cpu->max_perf, EXT_FRAC_BITS);
2064 cpu->min_perf = round_up(cpu->min_perf, EXT_FRAC_BITS);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002065
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002066 pr_debug("cpu:%d max_perf_pct:%d min_perf_pct:%d\n", policy->cpu,
Rafael J. Wysockie14cf882017-03-28 00:03:20 +02002067 fp_ext_toint(cpu->max_perf * 100),
2068 fp_ext_toint(cpu->min_perf * 100));
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002069}
2070
Dirk Brandewie93f08222013-02-06 09:02:13 -08002071static int intel_pstate_set_policy(struct cpufreq_policy *policy)
2072{
2073 struct cpudata *cpu;
2074
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002075 if (!policy->cpuinfo.max_freq)
2076 return -ENODEV;
2077
Dirk Brandewie93f08222013-02-06 09:02:13 -08002078 pr_debug("set_policy cpuinfo.max %u policy->max %u\n",
2079 policy->cpuinfo.max_freq, policy->max);
2080
2081 cpu = all_cpu_data[policy->cpu];
2082 cpu->policy = policy->policy;
Viresh Kumarbe49e342013-10-02 14:13:19 +05302083
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08002084 mutex_lock(&intel_pstate_limits_lock);
2085
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002086 intel_pstate_update_perf_limits(policy, cpu);
Rafael J. Wysockia240c4a2017-03-02 23:29:12 +01002087
Rafael J. Wysocki2f1d4072016-10-24 23:20:25 +02002088 if (cpu->policy == CPUFREQ_POLICY_PERFORMANCE) {
Rafael J. Wysockia6c6ead2016-10-19 02:57:22 +02002089 /*
2090 * NOHZ_FULL CPUs need this as the governor callback may not
2091 * be invoked on them.
2092 */
2093 intel_pstate_clear_update_util_hook(policy->cpu);
2094 intel_pstate_max_within_limits(cpu);
2095 }
2096
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02002097 intel_pstate_set_update_util_hook(policy->cpu);
2098
Rafael J. Wysocki5f98ced2017-03-09 16:30:38 +01002099 if (hwp_active)
2100 intel_pstate_hwp_set(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002101
Srinivas Pandruvadab59fe542016-12-06 13:32:15 -08002102 mutex_unlock(&intel_pstate_limits_lock);
2103
Dirk Brandewie93f08222013-02-06 09:02:13 -08002104 return 0;
2105}
2106
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002107static void intel_pstate_adjust_policy_max(struct cpufreq_policy *policy,
2108 struct cpudata *cpu)
2109{
2110 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
2111 policy->max < policy->cpuinfo.max_freq &&
2112 policy->max > cpu->pstate.max_freq) {
2113 pr_debug("policy->max > max non turbo frequency\n");
2114 policy->max = policy->cpuinfo.max_freq;
2115 }
2116}
2117
Dirk Brandewie93f08222013-02-06 09:02:13 -08002118static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
2119{
Srinivas Pandruvada7d9a8a92017-01-18 10:48:23 -08002120 struct cpudata *cpu = all_cpu_data[policy->cpu];
Srinivas Pandruvada7d9a8a92017-01-18 10:48:23 -08002121
2122 update_turbo_state();
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002123 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2124 intel_pstate_get_max_freq(cpu));
Dirk Brandewie93f08222013-02-06 09:02:13 -08002125
Stratos Karafotis285cb992014-07-18 08:37:21 -07002126 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -07002127 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -08002128 return -EINVAL;
2129
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002130 intel_pstate_adjust_policy_max(policy, cpu);
2131
Dirk Brandewie93f08222013-02-06 09:02:13 -08002132 return 0;
2133}
2134
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002135static void intel_cpufreq_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08002136{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002137 intel_pstate_set_min_pstate(all_cpu_data[policy->cpu]);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002138}
2139
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002140static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
2141{
2142 pr_debug("CPU %d exiting\n", policy->cpu);
2143
2144 intel_pstate_clear_update_util_hook(policy->cpu);
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08002145 if (hwp_active)
2146 intel_pstate_hwp_save_state(policy);
2147 else
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002148 intel_cpufreq_stop_cpu(policy);
2149}
2150
2151static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
2152{
2153 intel_pstate_exit_perf_limits(policy);
2154
2155 policy->fast_switch_possible = false;
2156
2157 return 0;
2158}
2159
2160static int __intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08002161{
Dirk Brandewie93f08222013-02-06 09:02:13 -08002162 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -07002163 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002164
2165 rc = intel_pstate_init_cpu(policy->cpu);
2166 if (rc)
2167 return rc;
2168
2169 cpu = all_cpu_data[policy->cpu];
2170
Rafael J. Wysockie14cf882017-03-28 00:03:20 +02002171 cpu->max_perf = int_ext_tofp(1);
2172 cpu->min_perf = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002173
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002174 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
2175 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002176
2177 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002178 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07002179 update_turbo_state();
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01002180 policy->cpuinfo.max_freq = global.turbo_disabled ?
Srinivas Pandruvada983e6002016-06-07 17:38:53 -07002181 cpu->pstate.max_pstate : cpu->pstate.turbo_pstate;
2182 policy->cpuinfo.max_freq *= cpu->pstate.scaling;
2183
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002184 intel_pstate_init_acpi_perf_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08002185 cpumask_set_cpu(policy->cpu, policy->cpus);
2186
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002187 policy->fast_switch_possible = true;
2188
Dirk Brandewie93f08222013-02-06 09:02:13 -08002189 return 0;
2190}
2191
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002192static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002193{
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002194 int ret = __intel_pstate_cpu_init(policy);
2195
2196 if (ret)
2197 return ret;
2198
2199 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
Rafael J. Wysocki7de32552017-03-18 00:57:39 +01002200 if (IS_ENABLED(CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE))
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002201 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
2202 else
2203 policy->policy = CPUFREQ_POLICY_POWERSAVE;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002204
2205 return 0;
2206}
2207
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002208static struct cpufreq_driver intel_pstate = {
Dirk Brandewie93f08222013-02-06 09:02:13 -08002209 .flags = CPUFREQ_CONST_LOOPS,
2210 .verify = intel_pstate_verify_policy,
2211 .setpolicy = intel_pstate_set_policy,
Srinivas Pandruvada984edbd2016-12-06 13:32:16 -08002212 .suspend = intel_pstate_hwp_save_state,
Srinivas Pandruvada84428852016-11-24 16:07:10 -08002213 .resume = intel_pstate_resume,
Dirk Brandewie93f08222013-02-06 09:02:13 -08002214 .get = intel_pstate_get,
2215 .init = intel_pstate_cpu_init,
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002216 .exit = intel_pstate_cpu_exit,
Dirk Brandewiebb180082014-03-19 08:45:54 -07002217 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -08002218 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -08002219};
2220
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002221static int intel_cpufreq_verify_policy(struct cpufreq_policy *policy)
2222{
2223 struct cpudata *cpu = all_cpu_data[policy->cpu];
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002224
2225 update_turbo_state();
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002226 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
2227 intel_pstate_get_max_freq(cpu));
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002228
Rafael J. Wysocki80b120c2017-03-23 00:00:47 +01002229 intel_pstate_adjust_policy_max(policy, cpu);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002230
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002231 intel_pstate_update_perf_limits(policy, cpu);
2232
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002233 return 0;
2234}
2235
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002236static int intel_cpufreq_target(struct cpufreq_policy *policy,
2237 unsigned int target_freq,
2238 unsigned int relation)
2239{
2240 struct cpudata *cpu = all_cpu_data[policy->cpu];
2241 struct cpufreq_freqs freqs;
2242 int target_pstate;
2243
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01002244 update_turbo_state();
2245
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002246 freqs.old = policy->cur;
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01002247 freqs.new = target_freq;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002248
2249 cpufreq_freq_transition_begin(policy, &freqs);
2250 switch (relation) {
2251 case CPUFREQ_RELATION_L:
2252 target_pstate = DIV_ROUND_UP(freqs.new, cpu->pstate.scaling);
2253 break;
2254 case CPUFREQ_RELATION_H:
2255 target_pstate = freqs.new / cpu->pstate.scaling;
2256 break;
2257 default:
2258 target_pstate = DIV_ROUND_CLOSEST(freqs.new, cpu->pstate.scaling);
2259 break;
2260 }
2261 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
2262 if (target_pstate != cpu->pstate.current_pstate) {
2263 cpu->pstate.current_pstate = target_pstate;
2264 wrmsrl_on_cpu(policy->cpu, MSR_IA32_PERF_CTL,
2265 pstate_funcs.get_val(cpu, target_pstate));
2266 }
Rafael J. Wysocki64078292017-03-03 23:51:31 +01002267 freqs.new = target_pstate * cpu->pstate.scaling;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002268 cpufreq_freq_transition_end(policy, &freqs, false);
2269
2270 return 0;
2271}
2272
2273static unsigned int intel_cpufreq_fast_switch(struct cpufreq_policy *policy,
2274 unsigned int target_freq)
2275{
2276 struct cpudata *cpu = all_cpu_data[policy->cpu];
2277 int target_pstate;
2278
Rafael J. Wysocki64897b22017-03-21 22:19:07 +01002279 update_turbo_state();
2280
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002281 target_pstate = DIV_ROUND_UP(target_freq, cpu->pstate.scaling);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01002282 target_pstate = intel_pstate_prepare_request(cpu, target_pstate);
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002283 intel_pstate_update_pstate(cpu, target_pstate);
Rafael J. Wysocki64078292017-03-03 23:51:31 +01002284 return target_pstate * cpu->pstate.scaling;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002285}
2286
2287static int intel_cpufreq_cpu_init(struct cpufreq_policy *policy)
2288{
2289 int ret = __intel_pstate_cpu_init(policy);
2290
2291 if (ret)
2292 return ret;
2293
2294 policy->cpuinfo.transition_latency = INTEL_CPUFREQ_TRANSITION_LATENCY;
2295 /* This reflects the intel_pstate_get_cpu_pstates() setting. */
2296 policy->cur = policy->cpuinfo.min_freq;
2297
2298 return 0;
2299}
2300
2301static struct cpufreq_driver intel_cpufreq = {
2302 .flags = CPUFREQ_CONST_LOOPS,
2303 .verify = intel_cpufreq_verify_policy,
2304 .target = intel_cpufreq_target,
2305 .fast_switch = intel_cpufreq_fast_switch,
2306 .init = intel_cpufreq_cpu_init,
2307 .exit = intel_pstate_cpu_exit,
2308 .stop_cpu = intel_cpufreq_stop_cpu,
2309 .name = "intel_cpufreq",
2310};
2311
2312static struct cpufreq_driver *intel_pstate_driver = &intel_pstate;
2313
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002314static void intel_pstate_driver_cleanup(void)
2315{
2316 unsigned int cpu;
2317
2318 get_online_cpus();
2319 for_each_online_cpu(cpu) {
2320 if (all_cpu_data[cpu]) {
2321 if (intel_pstate_driver == &intel_pstate)
2322 intel_pstate_clear_update_util_hook(cpu);
2323
2324 kfree(all_cpu_data[cpu]);
2325 all_cpu_data[cpu] = NULL;
2326 }
2327 }
2328 put_online_cpus();
2329}
2330
2331static int intel_pstate_register_driver(void)
2332{
2333 int ret;
2334
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002335 memset(&global, 0, sizeof(global));
2336 global.max_perf_pct = 100;
Rafael J. Wysockic3a49c82017-02-28 00:05:01 +01002337
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002338 ret = cpufreq_register_driver(intel_pstate_driver);
2339 if (ret) {
2340 intel_pstate_driver_cleanup();
2341 return ret;
2342 }
2343
Rafael J. Wysockic5a2ee72017-03-22 23:58:57 +01002344 global.min_perf_pct = min_perf_pct_min();
2345
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002346 mutex_lock(&intel_pstate_limits_lock);
2347 driver_registered = true;
2348 mutex_unlock(&intel_pstate_limits_lock);
2349
2350 if (intel_pstate_driver == &intel_pstate && !hwp_active &&
2351 pstate_funcs.get_target_pstate != get_target_pstate_use_cpu_load)
2352 intel_pstate_debug_expose_params();
2353
2354 return 0;
2355}
2356
2357static int intel_pstate_unregister_driver(void)
2358{
2359 if (hwp_active)
2360 return -EBUSY;
2361
2362 if (intel_pstate_driver == &intel_pstate && !hwp_active &&
2363 pstate_funcs.get_target_pstate != get_target_pstate_use_cpu_load)
2364 intel_pstate_debug_hide_params();
2365
2366 mutex_lock(&intel_pstate_limits_lock);
2367 driver_registered = false;
2368 mutex_unlock(&intel_pstate_limits_lock);
2369
2370 cpufreq_unregister_driver(intel_pstate_driver);
2371 intel_pstate_driver_cleanup();
2372
2373 return 0;
2374}
2375
2376static ssize_t intel_pstate_show_status(char *buf)
2377{
2378 if (!driver_registered)
2379 return sprintf(buf, "off\n");
2380
2381 return sprintf(buf, "%s\n", intel_pstate_driver == &intel_pstate ?
2382 "active" : "passive");
2383}
2384
2385static int intel_pstate_update_status(const char *buf, size_t size)
2386{
2387 int ret;
2388
2389 if (size == 3 && !strncmp(buf, "off", size))
2390 return driver_registered ?
2391 intel_pstate_unregister_driver() : -EINVAL;
2392
2393 if (size == 6 && !strncmp(buf, "active", size)) {
2394 if (driver_registered) {
2395 if (intel_pstate_driver == &intel_pstate)
2396 return 0;
2397
2398 ret = intel_pstate_unregister_driver();
2399 if (ret)
2400 return ret;
2401 }
2402
2403 intel_pstate_driver = &intel_pstate;
2404 return intel_pstate_register_driver();
2405 }
2406
2407 if (size == 7 && !strncmp(buf, "passive", size)) {
2408 if (driver_registered) {
2409 if (intel_pstate_driver != &intel_pstate)
2410 return 0;
2411
2412 ret = intel_pstate_unregister_driver();
2413 if (ret)
2414 return ret;
2415 }
2416
2417 intel_pstate_driver = &intel_cpufreq;
2418 return intel_pstate_register_driver();
2419 }
2420
2421 return -EINVAL;
2422}
2423
Jisheng Zhangeed43602016-06-27 18:07:16 +08002424static int no_load __initdata;
2425static int no_hwp __initdata;
2426static int hwp_only __initdata;
Jisheng Zhang29327c82016-06-27 18:07:17 +08002427static unsigned int force_load __initdata;
Dirk Brandewie6be26492013-02-15 22:55:10 +01002428
Jisheng Zhang29327c82016-06-27 18:07:17 +08002429static int __init intel_pstate_msrs_not_valid(void)
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002430{
Dirk Brandewie016c8152013-10-21 09:20:34 -07002431 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -07002432 !pstate_funcs.get_min() ||
2433 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002434 return -ENODEV;
2435
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002436 return 0;
2437}
Dirk Brandewie016c8152013-10-21 09:20:34 -07002438
Jisheng Zhang29327c82016-06-27 18:07:17 +08002439static void __init copy_pid_params(struct pstate_adjust_policy *policy)
Dirk Brandewie016c8152013-10-21 09:20:34 -07002440{
2441 pid_params.sample_rate_ms = policy->sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01002442 pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002443 pid_params.p_gain_pct = policy->p_gain_pct;
2444 pid_params.i_gain_pct = policy->i_gain_pct;
2445 pid_params.d_gain_pct = policy->d_gain_pct;
2446 pid_params.deadband = policy->deadband;
2447 pid_params.setpoint = policy->setpoint;
2448}
2449
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08002450#ifdef CONFIG_ACPI
2451static void intel_pstate_use_acpi_profile(void)
2452{
Rafael J. Wysocki55395342017-03-22 23:53:54 +01002453 switch (acpi_gbl_FADT.preferred_profile) {
2454 case PM_MOBILE:
2455 case PM_TABLET:
2456 case PM_APPLIANCE_PC:
2457 case PM_DESKTOP:
2458 case PM_WORKSTATION:
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08002459 pstate_funcs.get_target_pstate =
2460 get_target_pstate_use_cpu_load;
Rafael J. Wysocki55395342017-03-22 23:53:54 +01002461 }
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08002462}
2463#else
2464static void intel_pstate_use_acpi_profile(void)
2465{
2466}
2467#endif
2468
Jisheng Zhang29327c82016-06-27 18:07:17 +08002469static void __init copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -07002470{
2471 pstate_funcs.get_max = funcs->get_max;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07002472 pstate_funcs.get_max_physical = funcs->get_max_physical;
Dirk Brandewie016c8152013-10-21 09:20:34 -07002473 pstate_funcs.get_min = funcs->get_min;
2474 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -07002475 pstate_funcs.get_scaling = funcs->get_scaling;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01002476 pstate_funcs.get_val = funcs->get_val;
Dirk Brandewie007bea02013-12-18 10:32:39 -08002477 pstate_funcs.get_vid = funcs->get_vid;
Philippe Longepe157386b2015-12-04 17:40:30 +01002478 pstate_funcs.get_target_pstate = funcs->get_target_pstate;
2479
Srinivas Pandruvada7f7a5162016-11-14 10:31:11 -08002480 intel_pstate_use_acpi_profile();
Dirk Brandewie016c8152013-10-21 09:20:34 -07002481}
2482
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002483#ifdef CONFIG_ACPI
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002484
Jisheng Zhang29327c82016-06-27 18:07:17 +08002485static bool __init intel_pstate_no_acpi_pss(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002486{
2487 int i;
2488
2489 for_each_possible_cpu(i) {
2490 acpi_status status;
2491 union acpi_object *pss;
2492 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2493 struct acpi_processor *pr = per_cpu(processors, i);
2494
2495 if (!pr)
2496 continue;
2497
2498 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
2499 if (ACPI_FAILURE(status))
2500 continue;
2501
2502 pss = buffer.pointer;
2503 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
2504 kfree(pss);
2505 return false;
2506 }
2507
2508 kfree(pss);
2509 }
2510
2511 return true;
2512}
2513
Jisheng Zhang29327c82016-06-27 18:07:17 +08002514static bool __init intel_pstate_has_acpi_ppc(void)
ethan zhao966916e2014-12-01 11:32:08 +09002515{
2516 int i;
2517
2518 for_each_possible_cpu(i) {
2519 struct acpi_processor *pr = per_cpu(processors, i);
2520
2521 if (!pr)
2522 continue;
2523 if (acpi_has_method(pr->handle, "_PPC"))
2524 return true;
2525 }
2526 return false;
2527}
2528
2529enum {
2530 PSS,
2531 PPC,
2532};
2533
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002534struct hw_vendor_info {
2535 u16 valid;
2536 char oem_id[ACPI_OEM_ID_SIZE];
2537 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
ethan zhao966916e2014-12-01 11:32:08 +09002538 int oem_pwr_table;
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002539};
2540
2541/* Hardware vendor-specific info that has its own power management modes */
Jisheng Zhang29327c82016-06-27 18:07:17 +08002542static struct hw_vendor_info vendor_info[] __initdata = {
ethan zhao966916e2014-12-01 11:32:08 +09002543 {1, "HP ", "ProLiant", PSS},
2544 {1, "ORACLE", "X4-2 ", PPC},
2545 {1, "ORACLE", "X4-2L ", PPC},
2546 {1, "ORACLE", "X4-2B ", PPC},
2547 {1, "ORACLE", "X3-2 ", PPC},
2548 {1, "ORACLE", "X3-2L ", PPC},
2549 {1, "ORACLE", "X3-2B ", PPC},
2550 {1, "ORACLE", "X4470M2 ", PPC},
2551 {1, "ORACLE", "X4270M3 ", PPC},
2552 {1, "ORACLE", "X4270M2 ", PPC},
2553 {1, "ORACLE", "X4170M2 ", PPC},
Ethan Zhao5aecc3c2015-08-05 09:28:50 +09002554 {1, "ORACLE", "X4170 M3", PPC},
2555 {1, "ORACLE", "X4275 M3", PPC},
2556 {1, "ORACLE", "X6-2 ", PPC},
2557 {1, "ORACLE", "Sudbury ", PPC},
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002558 {0, "", ""},
2559};
2560
Jisheng Zhang29327c82016-06-27 18:07:17 +08002561static bool __init intel_pstate_platform_pwr_mgmt_exists(void)
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002562{
2563 struct acpi_table_header hdr;
2564 struct hw_vendor_info *v_info;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002565 const struct x86_cpu_id *id;
2566 u64 misc_pwr;
2567
2568 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
2569 if (id) {
2570 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
2571 if ( misc_pwr & (1 << 8))
2572 return true;
2573 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002574
Stratos Karafotisc4108332014-07-18 08:37:23 -07002575 if (acpi_disabled ||
2576 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002577 return false;
2578
2579 for (v_info = vendor_info; v_info->valid; v_info++) {
Stratos Karafotisc4108332014-07-18 08:37:23 -07002580 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
ethan zhao966916e2014-12-01 11:32:08 +09002581 !strncmp(hdr.oem_table_id, v_info->oem_table_id,
2582 ACPI_OEM_TABLE_ID_SIZE))
2583 switch (v_info->oem_pwr_table) {
2584 case PSS:
2585 return intel_pstate_no_acpi_pss();
2586 case PPC:
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002587 return intel_pstate_has_acpi_ppc() &&
2588 (!force_load);
ethan zhao966916e2014-12-01 11:32:08 +09002589 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002590 }
2591
2592 return false;
2593}
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002594
2595static void intel_pstate_request_control_from_smm(void)
2596{
2597 /*
2598 * It may be unsafe to request P-states control from SMM if _PPC support
2599 * has not been enabled.
2600 */
2601 if (acpi_ppc)
2602 acpi_processor_pstate_control();
2603}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002604#else /* CONFIG_ACPI not enabled */
2605static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
ethan zhao966916e2014-12-01 11:32:08 +09002606static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002607static inline void intel_pstate_request_control_from_smm(void) {}
Adrian Huangfbbcdc02013-10-31 23:24:05 +08002608#endif /* CONFIG_ACPI */
2609
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002610static const struct x86_cpu_id hwp_support_ids[] __initconst = {
2611 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
2612 {}
2613};
2614
Dirk Brandewie93f08222013-02-06 09:02:13 -08002615static int __init intel_pstate_init(void)
2616{
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002617 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002618
Dirk Brandewie6be26492013-02-15 22:55:10 +01002619 if (no_load)
2620 return -ENODEV;
2621
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002622 if (x86_match_cpu(hwp_support_ids)) {
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002623 copy_cpu_funcs(&core_params.funcs);
Rafael J. Wysockieb5139d2017-03-22 23:52:18 +01002624 if (no_hwp) {
2625 pstate_funcs.get_target_pstate = get_target_pstate_use_cpu_load;
2626 } else {
2627 hwp_active++;
2628 intel_pstate.attr = hwp_cpufreq_attrs;
2629 goto hwp_cpu_matched;
2630 }
2631 } else {
2632 const struct x86_cpu_id *id;
2633 struct cpu_defaults *cpu_def;
2634
2635 id = x86_match_cpu(intel_pstate_cpu_ids);
2636 if (!id)
2637 return -ENODEV;
2638
2639 cpu_def = (struct cpu_defaults *)id->driver_data;
2640
2641 copy_pid_params(&cpu_def->pid_policy);
2642 copy_cpu_funcs(&cpu_def->funcs);
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002643 }
2644
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01002645 if (intel_pstate_msrs_not_valid())
2646 return -ENODEV;
2647
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08002648hwp_cpu_matched:
2649 /*
2650 * The Intel pstate driver will be ignored if the platform
2651 * firmware has its own power management modes.
2652 */
2653 if (intel_pstate_platform_pwr_mgmt_exists())
2654 return -ENODEV;
2655
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002656 if (!hwp_active && hwp_only)
2657 return -ENOTSUPP;
2658
Joe Perches4836df12016-04-05 13:28:23 -07002659 pr_info("Intel P-state driver initializing\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08002660
Wei Yongjunb57ffac2013-05-13 08:03:43 +00002661 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08002662 if (!all_cpu_data)
2663 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002664
Rafael J. Wysockid0ea59e2016-11-17 22:47:47 +01002665 intel_pstate_request_control_from_smm();
2666
Dirk Brandewie93f08222013-02-06 09:02:13 -08002667 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08002668
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01002669 mutex_lock(&intel_pstate_driver_lock);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002670 rc = intel_pstate_register_driver();
Rafael J. Wysocki0c30b652017-01-11 04:12:16 +01002671 mutex_unlock(&intel_pstate_driver_lock);
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002672 if (rc)
2673 return rc;
Dirk Brandewie907cc902013-03-05 14:15:27 -08002674
Dirk Brandewie907cc902013-03-05 14:15:27 -08002675 if (hwp_active)
2676 pr_info("HWP enabled\n");
2677
Rafael J. Wysockifb1fe102017-01-05 02:53:12 +01002678 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08002679}
2680device_initcall(intel_pstate_init);
2681
Dirk Brandewie6be26492013-02-15 22:55:10 +01002682static int __init intel_pstate_setup(char *str)
2683{
2684 if (!str)
2685 return -EINVAL;
2686
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002687 if (!strcmp(str, "disable")) {
Dirk Brandewie6be26492013-02-15 22:55:10 +01002688 no_load = 1;
Rafael J. Wysocki001c76f2016-11-17 23:34:17 +01002689 } else if (!strcmp(str, "passive")) {
2690 pr_info("Passive mode enabled\n");
2691 intel_pstate_driver = &intel_cpufreq;
2692 no_hwp = 1;
2693 }
Prarit Bhargava539342f2015-10-22 09:43:31 -04002694 if (!strcmp(str, "no_hwp")) {
Joe Perches4836df12016-04-05 13:28:23 -07002695 pr_info("HWP disabled\n");
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08002696 no_hwp = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04002697 }
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09002698 if (!strcmp(str, "force"))
2699 force_load = 1;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08002700 if (!strcmp(str, "hwp_only"))
2701 hwp_only = 1;
Srinivas Pandruvadaeae48f02016-10-25 13:20:40 -07002702 if (!strcmp(str, "per_cpu_perf_limits"))
2703 per_cpu_limits = true;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07002704
2705#ifdef CONFIG_ACPI
2706 if (!strcmp(str, "support_acpi_ppc"))
2707 acpi_ppc = true;
2708#endif
2709
Dirk Brandewie6be26492013-02-15 22:55:10 +01002710 return 0;
2711}
2712early_param("intel_pstate", intel_pstate_setup);
2713
Dirk Brandewie93f08222013-02-06 09:02:13 -08002714MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
2715MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
2716MODULE_LICENSE("GPL");