blob: 0d159b513469111572c615a2bda79fce5b8ba150 [file] [log] [blame]
Dirk Brandewie93f08222013-02-06 09:02:13 -08001/*
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00002 * intel_pstate.c: Native P state management for Intel processors
Dirk Brandewie93f08222013-02-06 09:02:13 -08003 *
4 * (C) Copyright 2012 Intel Corporation
5 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
11 */
12
Joe Perches4836df12016-04-05 13:28:23 -070013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Dirk Brandewie93f08222013-02-06 09:02:13 -080015#include <linux/kernel.h>
16#include <linux/kernel_stat.h>
17#include <linux/module.h>
18#include <linux/ktime.h>
19#include <linux/hrtimer.h>
20#include <linux/tick.h>
21#include <linux/slab.h>
22#include <linux/sched.h>
23#include <linux/list.h>
24#include <linux/cpu.h>
25#include <linux/cpufreq.h>
26#include <linux/sysfs.h>
27#include <linux/types.h>
28#include <linux/fs.h>
29#include <linux/debugfs.h>
Adrian Huangfbbcdc02013-10-31 23:24:05 +080030#include <linux/acpi.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100031#include <linux/vmalloc.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080032#include <trace/events/power.h>
33
34#include <asm/div64.h>
35#include <asm/msr.h>
36#include <asm/cpu_device_id.h>
Borislav Petkov64df1fd2015-04-03 15:19:53 +020037#include <asm/cpufeature.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080038
Philippe Longepe938d21a2015-11-09 17:40:46 -080039#define ATOM_RATIOS 0x66a
40#define ATOM_VIDS 0x66b
41#define ATOM_TURBO_RATIOS 0x66c
42#define ATOM_TURBO_VIDS 0x66d
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -080043
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -070044#ifdef CONFIG_ACPI
45#include <acpi/processor.h>
46#endif
47
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070048#define FRAC_BITS 8
Dirk Brandewie93f08222013-02-06 09:02:13 -080049#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
50#define fp_toint(X) ((X) >> FRAC_BITS)
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070051
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020052#define EXT_BITS 6
53#define EXT_FRAC_BITS (EXT_BITS + FRAC_BITS)
54
Dirk Brandewie93f08222013-02-06 09:02:13 -080055static inline int32_t mul_fp(int32_t x, int32_t y)
56{
57 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
58}
59
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040060static inline int32_t div_fp(s64 x, s64 y)
Dirk Brandewie93f08222013-02-06 09:02:13 -080061{
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040062 return div64_s64((int64_t)x << FRAC_BITS, y);
Dirk Brandewie93f08222013-02-06 09:02:13 -080063}
64
Dirk Brandewied022a652014-10-13 08:37:44 -070065static inline int ceiling_fp(int32_t x)
66{
67 int mask, ret;
68
69 ret = fp_toint(x);
70 mask = (1 << FRAC_BITS) - 1;
71 if (x & mask)
72 ret += 1;
73 return ret;
74}
75
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020076static inline u64 mul_ext_fp(u64 x, u64 y)
77{
78 return (x * y) >> EXT_FRAC_BITS;
79}
80
81static inline u64 div_ext_fp(u64 x, u64 y)
82{
83 return div64_u64(x << EXT_FRAC_BITS, y);
84}
85
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070086/**
87 * struct sample - Store performance sample
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020088 * @core_avg_perf: Ratio of APERF/MPERF which is the actual average
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070089 * performance during last sample period
90 * @busy_scaled: Scaled busy value which is used to calculate next
Rafael J. Wysockia1c97872016-05-11 19:09:12 +020091 * P state. This can be different than core_avg_perf
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -070092 * to account for cpu idle period
93 * @aperf: Difference of actual performance frequency clock count
94 * read from APERF MSR between last and current sample
95 * @mperf: Difference of maximum performance frequency clock count
96 * read from MPERF MSR between last and current sample
97 * @tsc: Difference of time stamp counter between last and
98 * current sample
99 * @freq: Effective frequency calculated from APERF/MPERF
100 * @time: Current time from scheduler
101 *
102 * This structure is used in the cpudata structure to store performance sample
103 * data for choosing next P State.
104 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800105struct sample {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +0200106 int32_t core_avg_perf;
Philippe Longepe157386b2015-12-04 17:40:30 +0100107 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800108 u64 aperf;
109 u64 mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700110 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800111 int freq;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100112 u64 time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800113};
114
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700115/**
116 * struct pstate_data - Store P state data
117 * @current_pstate: Current requested P state
118 * @min_pstate: Min P state possible for this platform
119 * @max_pstate: Max P state possible for this platform
120 * @max_pstate_physical:This is physical Max P state for a processor
121 * This can be higher than the max_pstate which can
122 * be limited by platform thermal design power limits
123 * @scaling: Scaling factor to convert frequency to cpufreq
124 * frequency units
125 * @turbo_pstate: Max Turbo P state possible for this platform
126 *
127 * Stores the per cpu model P state limits and current P state.
128 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800129struct pstate_data {
130 int current_pstate;
131 int min_pstate;
132 int max_pstate;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700133 int max_pstate_physical;
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700134 int scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800135 int turbo_pstate;
136};
137
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700138/**
139 * struct vid_data - Stores voltage information data
140 * @min: VID data for this platform corresponding to
141 * the lowest P state
142 * @max: VID data corresponding to the highest P State.
143 * @turbo: VID data for turbo P state
144 * @ratio: Ratio of (vid max - vid min) /
145 * (max P state - Min P State)
146 *
147 * Stores the voltage data for DVFS (Dynamic Voltage and Frequency Scaling)
148 * This data is used in Atom platforms, where in addition to target P state,
149 * the voltage data needs to be specified to select next P State.
150 */
Dirk Brandewie007bea02013-12-18 10:32:39 -0800151struct vid_data {
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700152 int min;
153 int max;
154 int turbo;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800155 int32_t ratio;
156};
157
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700158/**
159 * struct _pid - Stores PID data
160 * @setpoint: Target set point for busyness or performance
161 * @integral: Storage for accumulated error values
162 * @p_gain: PID proportional gain
163 * @i_gain: PID integral gain
164 * @d_gain: PID derivative gain
165 * @deadband: PID deadband
166 * @last_err: Last error storage for integral part of PID calculation
167 *
168 * Stores PID coefficients and last error for PID controller.
169 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800170struct _pid {
171 int setpoint;
172 int32_t integral;
173 int32_t p_gain;
174 int32_t i_gain;
175 int32_t d_gain;
176 int deadband;
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700177 int32_t last_err;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800178};
179
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700180/**
181 * struct cpudata - Per CPU instance data storage
182 * @cpu: CPU number for this instance data
183 * @update_util: CPUFreq utility callback information
Chen Yu4578ee7e2016-05-11 14:33:08 +0800184 * @update_util_set: CPUFreq utility callback is set
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700185 * @pstate: Stores P state limits for this CPU
186 * @vid: Stores VID limits for this CPU
187 * @pid: Stores PID parameters for this CPU
188 * @last_sample_time: Last Sample time
189 * @prev_aperf: Last APERF value read from APERF MSR
190 * @prev_mperf: Last MPERF value read from MPERF MSR
191 * @prev_tsc: Last timestamp counter (TSC) value
192 * @prev_cummulative_iowait: IO Wait time difference from last and
193 * current sample
194 * @sample: Storage for storing last Sample data
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700195 * @acpi_perf_data: Stores ACPI perf information read from _PSS
196 * @valid_pss_table: Set to true for valid ACPI _PSS entries found
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700197 *
198 * This structure stores per CPU instance data for all CPUs.
199 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800200struct cpudata {
201 int cpu;
202
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100203 struct update_util_data update_util;
Chen Yu4578ee7e2016-05-11 14:33:08 +0800204 bool update_util_set;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800205
Dirk Brandewie93f08222013-02-06 09:02:13 -0800206 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800207 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800208 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800209
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100210 u64 last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800211 u64 prev_aperf;
212 u64 prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700213 u64 prev_tsc;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100214 u64 prev_cummulative_iowait;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800215 struct sample sample;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700216#ifdef CONFIG_ACPI
217 struct acpi_processor_performance acpi_perf_data;
218 bool valid_pss_table;
219#endif
Dirk Brandewie93f08222013-02-06 09:02:13 -0800220};
221
222static struct cpudata **all_cpu_data;
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700223
224/**
225 * struct pid_adjust_policy - Stores static PID configuration data
226 * @sample_rate_ms: PID calculation sample rate in ms
227 * @sample_rate_ns: Sample rate calculation in ns
228 * @deadband: PID deadband
229 * @setpoint: PID Setpoint
230 * @p_gain_pct: PID proportional gain
231 * @i_gain_pct: PID integral gain
232 * @d_gain_pct: PID derivative gain
233 *
234 * Stores per CPU model static PID configuration data.
235 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800236struct pstate_adjust_policy {
237 int sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100238 s64 sample_rate_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800239 int deadband;
240 int setpoint;
241 int p_gain_pct;
242 int d_gain_pct;
243 int i_gain_pct;
244};
245
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700246/**
247 * struct pstate_funcs - Per CPU model specific callbacks
248 * @get_max: Callback to get maximum non turbo effective P state
249 * @get_max_physical: Callback to get maximum non turbo physical P state
250 * @get_min: Callback to get minimum P state
251 * @get_turbo: Callback to get turbo P state
252 * @get_scaling: Callback to get frequency scaling factor
253 * @get_val: Callback to convert P state to actual MSR write value
254 * @get_vid: Callback to get VID data for Atom platforms
255 * @get_target_pstate: Callback to a function to calculate next P state to use
256 *
257 * Core and Atom CPU models have different way to get P State limits. This
258 * structure is used to store those callbacks.
259 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700260struct pstate_funcs {
261 int (*get_max)(void);
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700262 int (*get_max_physical)(void);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700263 int (*get_min)(void);
264 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700265 int (*get_scaling)(void);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100266 u64 (*get_val)(struct cpudata*, int pstate);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800267 void (*get_vid)(struct cpudata *);
Philippe Longepe157386b2015-12-04 17:40:30 +0100268 int32_t (*get_target_pstate)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800269};
270
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700271/**
272 * struct cpu_defaults- Per CPU model default config data
273 * @pid_policy: PID config data
274 * @funcs: Callback function data
275 */
Dirk Brandewie016c8152013-10-21 09:20:34 -0700276struct cpu_defaults {
277 struct pstate_adjust_policy pid_policy;
278 struct pstate_funcs funcs;
279};
280
Philippe Longepe157386b2015-12-04 17:40:30 +0100281static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
Philippe Longepee70eed22015-12-04 17:40:32 +0100282static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
Philippe Longepe157386b2015-12-04 17:40:30 +0100283
Dirk Brandewie016c8152013-10-21 09:20:34 -0700284static struct pstate_adjust_policy pid_params;
285static struct pstate_funcs pstate_funcs;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800286static int hwp_active;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700287
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700288#ifdef CONFIG_ACPI
289static bool acpi_ppc;
290#endif
Srinivas Pandruvada13ad7702016-04-03 13:06:46 -0700291
292/**
293 * struct perf_limits - Store user and policy limits
294 * @no_turbo: User requested turbo state from intel_pstate sysfs
295 * @turbo_disabled: Platform turbo status either from msr
296 * MSR_IA32_MISC_ENABLE or when maximum available pstate
297 * matches the maximum turbo pstate
298 * @max_perf_pct: Effective maximum performance limit in percentage, this
299 * is minimum of either limits enforced by cpufreq policy
300 * or limits from user set limits via intel_pstate sysfs
301 * @min_perf_pct: Effective minimum performance limit in percentage, this
302 * is maximum of either limits enforced by cpufreq policy
303 * or limits from user set limits via intel_pstate sysfs
304 * @max_perf: This is a scaled value between 0 to 255 for max_perf_pct
305 * This value is used to limit max pstate
306 * @min_perf: This is a scaled value between 0 to 255 for min_perf_pct
307 * This value is used to limit min pstate
308 * @max_policy_pct: The maximum performance in percentage enforced by
309 * cpufreq setpolicy interface
310 * @max_sysfs_pct: The maximum performance in percentage enforced by
311 * intel pstate sysfs interface
312 * @min_policy_pct: The minimum performance in percentage enforced by
313 * cpufreq setpolicy interface
314 * @min_sysfs_pct: The minimum performance in percentage enforced by
315 * intel pstate sysfs interface
316 *
317 * Storage for user and policy defined limits.
318 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800319struct perf_limits {
320 int no_turbo;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700321 int turbo_disabled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800322 int max_perf_pct;
323 int min_perf_pct;
324 int32_t max_perf;
325 int32_t min_perf;
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700326 int max_policy_pct;
327 int max_sysfs_pct;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800328 int min_policy_pct;
329 int min_sysfs_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800330};
331
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400332static struct perf_limits performance_limits = {
333 .no_turbo = 0,
334 .turbo_disabled = 0,
335 .max_perf_pct = 100,
336 .max_perf = int_tofp(1),
337 .min_perf_pct = 100,
338 .min_perf = int_tofp(1),
339 .max_policy_pct = 100,
340 .max_sysfs_pct = 100,
341 .min_policy_pct = 0,
342 .min_sysfs_pct = 0,
343};
344
345static struct perf_limits powersave_limits = {
Dirk Brandewie93f08222013-02-06 09:02:13 -0800346 .no_turbo = 0,
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700347 .turbo_disabled = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800348 .max_perf_pct = 100,
349 .max_perf = int_tofp(1),
350 .min_perf_pct = 0,
351 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700352 .max_policy_pct = 100,
353 .max_sysfs_pct = 100,
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800354 .min_policy_pct = 0,
355 .min_sysfs_pct = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800356};
357
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400358#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
359static struct perf_limits *limits = &performance_limits;
360#else
361static struct perf_limits *limits = &powersave_limits;
362#endif
363
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700364#ifdef CONFIG_ACPI
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700365
366static bool intel_pstate_get_ppc_enable_status(void)
367{
368 if (acpi_gbl_FADT.preferred_profile == PM_ENTERPRISE_SERVER ||
369 acpi_gbl_FADT.preferred_profile == PM_PERFORMANCE_SERVER)
370 return true;
371
372 return acpi_ppc;
373}
374
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700375/*
376 * The max target pstate ratio is a 8 bit value in both PLATFORM_INFO MSR and
377 * in TURBO_RATIO_LIMIT MSR, which pstate driver stores in max_pstate and
378 * max_turbo_pstate fields. The PERF_CTL MSR contains 16 bit value for P state
379 * ratio, out of it only high 8 bits are used. For example 0x1700 is setting
380 * target ratio 0x17. The _PSS control value stores in a format which can be
381 * directly written to PERF_CTL MSR. But in intel_pstate driver this shift
382 * occurs during write to PERF_CTL (E.g. for cores core_set_pstate()).
383 * This function converts the _PSS control value to intel pstate driver format
384 * for comparison and assignment.
385 */
386static int convert_to_native_pstate_format(struct cpudata *cpu, int index)
387{
388 return cpu->acpi_perf_data.states[index].control >> 8;
389}
390
391static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
392{
393 struct cpudata *cpu;
394 int turbo_pss_ctl;
395 int ret;
396 int i;
397
Srinivas Pandruvadae59a8f72016-05-04 15:07:34 -0700398 if (hwp_active)
399 return;
400
Srinivas Pandruvada2b3ec762016-04-27 15:48:08 -0700401 if (!intel_pstate_get_ppc_enable_status())
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700402 return;
403
404 cpu = all_cpu_data[policy->cpu];
405
406 ret = acpi_processor_register_performance(&cpu->acpi_perf_data,
407 policy->cpu);
408 if (ret)
409 return;
410
411 /*
412 * Check if the control value in _PSS is for PERF_CTL MSR, which should
413 * guarantee that the states returned by it map to the states in our
414 * list directly.
415 */
416 if (cpu->acpi_perf_data.control_register.space_id !=
417 ACPI_ADR_SPACE_FIXED_HARDWARE)
418 goto err;
419
420 /*
421 * If there is only one entry _PSS, simply ignore _PSS and continue as
422 * usual without taking _PSS into account
423 */
424 if (cpu->acpi_perf_data.state_count < 2)
425 goto err;
426
427 pr_debug("CPU%u - ACPI _PSS perf data\n", policy->cpu);
428 for (i = 0; i < cpu->acpi_perf_data.state_count; i++) {
429 pr_debug(" %cP%d: %u MHz, %u mW, 0x%x\n",
430 (i == cpu->acpi_perf_data.state ? '*' : ' '), i,
431 (u32) cpu->acpi_perf_data.states[i].core_frequency,
432 (u32) cpu->acpi_perf_data.states[i].power,
433 (u32) cpu->acpi_perf_data.states[i].control);
434 }
435
436 /*
437 * The _PSS table doesn't contain whole turbo frequency range.
438 * This just contains +1 MHZ above the max non turbo frequency,
439 * with control value corresponding to max turbo ratio. But
440 * when cpufreq set policy is called, it will call with this
441 * max frequency, which will cause a reduced performance as
442 * this driver uses real max turbo frequency as the max
443 * frequency. So correct this frequency in _PSS table to
444 * correct max turbo frequency based on the turbo ratio.
445 * Also need to convert to MHz as _PSS freq is in MHz.
446 */
447 turbo_pss_ctl = convert_to_native_pstate_format(cpu, 0);
448 if (turbo_pss_ctl > cpu->pstate.max_pstate)
449 cpu->acpi_perf_data.states[0].core_frequency =
450 policy->cpuinfo.max_freq / 1000;
451 cpu->valid_pss_table = true;
Srinivas Pandruvada6cacd112016-05-29 23:31:23 -0700452 pr_debug("_PPC limits will be enforced\n");
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -0700453
454 return;
455
456 err:
457 cpu->valid_pss_table = false;
458 acpi_processor_unregister_performance(policy->cpu);
459}
460
461static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
462{
463 struct cpudata *cpu;
464
465 cpu = all_cpu_data[policy->cpu];
466 if (!cpu->valid_pss_table)
467 return;
468
469 acpi_processor_unregister_performance(policy->cpu);
470}
471
472#else
473static void intel_pstate_init_acpi_perf_limits(struct cpufreq_policy *policy)
474{
475}
476
477static void intel_pstate_exit_perf_limits(struct cpufreq_policy *policy)
478{
479}
480#endif
481
Dirk Brandewie93f08222013-02-06 09:02:13 -0800482static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700483 int deadband, int integral) {
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100484 pid->setpoint = int_tofp(setpoint);
485 pid->deadband = int_tofp(deadband);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800486 pid->integral = int_tofp(integral);
Dirk Brandewied98d0992014-02-12 10:01:05 -0800487 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800488}
489
490static inline void pid_p_gain_set(struct _pid *pid, int percent)
491{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200492 pid->p_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800493}
494
495static inline void pid_i_gain_set(struct _pid *pid, int percent)
496{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200497 pid->i_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800498}
499
500static inline void pid_d_gain_set(struct _pid *pid, int percent)
501{
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200502 pid->d_gain = div_fp(percent, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800503}
504
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700505static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800506{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700507 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800508 int32_t pterm, dterm, fp_error;
509 int32_t integral_limit;
510
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100511 fp_error = pid->setpoint - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800512
Philippe Longepeb54a0df2016-03-08 10:31:14 +0100513 if (abs(fp_error) <= pid->deadband)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800514 return 0;
515
516 pterm = mul_fp(pid->p_gain, fp_error);
517
518 pid->integral += fp_error;
519
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800520 /*
521 * We limit the integral here so that it will never
522 * get higher than 30. This prevents it from becoming
523 * too large an input over long periods of time and allows
524 * it to get factored out sooner.
525 *
526 * The value of 30 was chosen through experimentation.
527 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800528 integral_limit = int_tofp(30);
529 if (pid->integral > integral_limit)
530 pid->integral = integral_limit;
531 if (pid->integral < -integral_limit)
532 pid->integral = -integral_limit;
533
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700534 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
535 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800536
537 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
Doug Smythies51d211e2014-06-17 13:36:10 -0700538 result = result + (1 << (FRAC_BITS-1));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800539 return (signed int)fp_toint(result);
540}
541
542static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
543{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700544 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
545 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
546 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800547
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700548 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800549}
550
Dirk Brandewie93f08222013-02-06 09:02:13 -0800551static inline void intel_pstate_reset_all_pid(void)
552{
553 unsigned int cpu;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700554
Dirk Brandewie93f08222013-02-06 09:02:13 -0800555 for_each_online_cpu(cpu) {
556 if (all_cpu_data[cpu])
557 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
558 }
559}
560
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700561static inline void update_turbo_state(void)
562{
563 u64 misc_en;
564 struct cpudata *cpu;
565
566 cpu = all_cpu_data[0];
567 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400568 limits->turbo_disabled =
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700569 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
570 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
571}
572
Viresh Kumar41cfd642016-02-22 10:27:46 +0530573static void intel_pstate_hwp_set(const struct cpumask *cpumask)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800574{
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700575 int min, hw_min, max, hw_max, cpu, range, adj_range;
576 u64 value, cap;
577
578 rdmsrl(MSR_HWP_CAPABILITIES, cap);
579 hw_min = HWP_LOWEST_PERF(cap);
580 hw_max = HWP_HIGHEST_PERF(cap);
581 range = hw_max - hw_min;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800582
Viresh Kumar41cfd642016-02-22 10:27:46 +0530583 for_each_cpu(cpu, cpumask) {
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800584 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400585 adj_range = limits->min_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700586 min = hw_min + adj_range;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800587 value &= ~HWP_MIN_PERF(~0L);
588 value |= HWP_MIN_PERF(min);
589
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400590 adj_range = limits->max_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700591 max = hw_min + adj_range;
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400592 if (limits->no_turbo) {
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700593 hw_max = HWP_GUARANTEED_PERF(cap);
594 if (hw_max < max)
595 max = hw_max;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800596 }
597
598 value &= ~HWP_MAX_PERF(~0L);
599 value |= HWP_MAX_PERF(max);
600 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
601 }
Viresh Kumar41cfd642016-02-22 10:27:46 +0530602}
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800603
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +0200604static int intel_pstate_hwp_set_policy(struct cpufreq_policy *policy)
605{
606 if (hwp_active)
607 intel_pstate_hwp_set(policy->cpus);
608
609 return 0;
610}
611
Viresh Kumar41cfd642016-02-22 10:27:46 +0530612static void intel_pstate_hwp_set_online_cpus(void)
613{
614 get_online_cpus();
615 intel_pstate_hwp_set(cpu_online_mask);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800616 put_online_cpus();
617}
618
Dirk Brandewie93f08222013-02-06 09:02:13 -0800619/************************** debugfs begin ************************/
620static int pid_param_set(void *data, u64 val)
621{
622 *(u32 *)data = val;
623 intel_pstate_reset_all_pid();
624 return 0;
625}
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700626
Dirk Brandewie93f08222013-02-06 09:02:13 -0800627static int pid_param_get(void *data, u64 *val)
628{
629 *val = *(u32 *)data;
630 return 0;
631}
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700632DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -0800633
634struct pid_param {
635 char *name;
636 void *value;
637};
638
639static struct pid_param pid_files[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700640 {"sample_rate_ms", &pid_params.sample_rate_ms},
641 {"d_gain_pct", &pid_params.d_gain_pct},
642 {"i_gain_pct", &pid_params.i_gain_pct},
643 {"deadband", &pid_params.deadband},
644 {"setpoint", &pid_params.setpoint},
645 {"p_gain_pct", &pid_params.p_gain_pct},
Dirk Brandewie93f08222013-02-06 09:02:13 -0800646 {NULL, NULL}
647};
648
Stratos Karafotis317dd502014-07-18 08:37:17 -0700649static void __init intel_pstate_debug_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800650{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700651 struct dentry *debugfs_parent;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800652 int i = 0;
653
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800654 if (hwp_active)
655 return;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800656 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
657 if (IS_ERR_OR_NULL(debugfs_parent))
658 return;
659 while (pid_files[i].name) {
660 debugfs_create_file(pid_files[i].name, 0660,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700661 debugfs_parent, pid_files[i].value,
662 &fops_pid_param);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800663 i++;
664 }
665}
666
667/************************** debugfs end ************************/
668
669/************************** sysfs begin ************************/
670#define show_one(file_name, object) \
671 static ssize_t show_##file_name \
672 (struct kobject *kobj, struct attribute *attr, char *buf) \
673 { \
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400674 return sprintf(buf, "%u\n", limits->object); \
Dirk Brandewie93f08222013-02-06 09:02:13 -0800675 }
676
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800677static ssize_t show_turbo_pct(struct kobject *kobj,
678 struct attribute *attr, char *buf)
679{
680 struct cpudata *cpu;
681 int total, no_turbo, turbo_pct;
682 uint32_t turbo_fp;
683
684 cpu = all_cpu_data[0];
685
686 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
687 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200688 turbo_fp = div_fp(no_turbo, total);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800689 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
690 return sprintf(buf, "%u\n", turbo_pct);
691}
692
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800693static ssize_t show_num_pstates(struct kobject *kobj,
694 struct attribute *attr, char *buf)
695{
696 struct cpudata *cpu;
697 int total;
698
699 cpu = all_cpu_data[0];
700 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
701 return sprintf(buf, "%u\n", total);
702}
703
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700704static ssize_t show_no_turbo(struct kobject *kobj,
705 struct attribute *attr, char *buf)
706{
707 ssize_t ret;
708
709 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400710 if (limits->turbo_disabled)
711 ret = sprintf(buf, "%u\n", limits->turbo_disabled);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700712 else
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400713 ret = sprintf(buf, "%u\n", limits->no_turbo);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700714
715 return ret;
716}
717
Dirk Brandewie93f08222013-02-06 09:02:13 -0800718static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700719 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800720{
721 unsigned int input;
722 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700723
Dirk Brandewie93f08222013-02-06 09:02:13 -0800724 ret = sscanf(buf, "%u", &input);
725 if (ret != 1)
726 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700727
728 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400729 if (limits->turbo_disabled) {
Joe Perches4836df12016-04-05 13:28:23 -0700730 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700731 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700732 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800733
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400734 limits->no_turbo = clamp_t(int, input, 0, 1);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700735
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800736 if (hwp_active)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530737 intel_pstate_hwp_set_online_cpus();
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800738
Dirk Brandewie93f08222013-02-06 09:02:13 -0800739 return count;
740}
741
742static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700743 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800744{
745 unsigned int input;
746 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700747
Dirk Brandewie93f08222013-02-06 09:02:13 -0800748 ret = sscanf(buf, "%u", &input);
749 if (ret != 1)
750 return -EINVAL;
751
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400752 limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
753 limits->max_perf_pct = min(limits->max_policy_pct,
754 limits->max_sysfs_pct);
755 limits->max_perf_pct = max(limits->min_policy_pct,
756 limits->max_perf_pct);
757 limits->max_perf_pct = max(limits->min_perf_pct,
758 limits->max_perf_pct);
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200759 limits->max_perf = div_fp(limits->max_perf_pct, 100);
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700760
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800761 if (hwp_active)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530762 intel_pstate_hwp_set_online_cpus();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800763 return count;
764}
765
766static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700767 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800768{
769 unsigned int input;
770 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700771
Dirk Brandewie93f08222013-02-06 09:02:13 -0800772 ret = sscanf(buf, "%u", &input);
773 if (ret != 1)
774 return -EINVAL;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800775
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400776 limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
777 limits->min_perf_pct = max(limits->min_policy_pct,
778 limits->min_sysfs_pct);
779 limits->min_perf_pct = min(limits->max_policy_pct,
780 limits->min_perf_pct);
781 limits->min_perf_pct = min(limits->max_perf_pct,
782 limits->min_perf_pct);
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +0200783 limits->min_perf = div_fp(limits->min_perf_pct, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800784
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800785 if (hwp_active)
Viresh Kumar41cfd642016-02-22 10:27:46 +0530786 intel_pstate_hwp_set_online_cpus();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800787 return count;
788}
789
Dirk Brandewie93f08222013-02-06 09:02:13 -0800790show_one(max_perf_pct, max_perf_pct);
791show_one(min_perf_pct, min_perf_pct);
792
793define_one_global_rw(no_turbo);
794define_one_global_rw(max_perf_pct);
795define_one_global_rw(min_perf_pct);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800796define_one_global_ro(turbo_pct);
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800797define_one_global_ro(num_pstates);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800798
799static struct attribute *intel_pstate_attributes[] = {
800 &no_turbo.attr,
801 &max_perf_pct.attr,
802 &min_perf_pct.attr,
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800803 &turbo_pct.attr,
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800804 &num_pstates.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800805 NULL
806};
807
808static struct attribute_group intel_pstate_attr_group = {
809 .attrs = intel_pstate_attributes,
810};
Dirk Brandewie93f08222013-02-06 09:02:13 -0800811
Stratos Karafotis317dd502014-07-18 08:37:17 -0700812static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800813{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700814 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800815 int rc;
816
817 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
818 &cpu_subsys.dev_root->kobj);
819 BUG_ON(!intel_pstate_kobject);
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700820 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800821 BUG_ON(rc);
822}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800823/************************** sysfs end ************************/
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800824
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -0700825static void intel_pstate_hwp_enable(struct cpudata *cpudata)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800826{
Srinivas Pandruvadaf05c9662016-02-25 15:09:31 -0800827 /* First disable HWP notification interrupt as we don't process them */
828 wrmsrl_on_cpu(cpudata->cpu, MSR_HWP_INTERRUPT, 0x00);
829
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -0700830 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800831}
832
Philippe Longepe938d21a2015-11-09 17:40:46 -0800833static int atom_get_min_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700834{
835 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700836
Philippe Longepe938d21a2015-11-09 17:40:46 -0800837 rdmsrl(ATOM_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700838 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700839}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800840
Philippe Longepe938d21a2015-11-09 17:40:46 -0800841static int atom_get_max_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700842{
843 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700844
Philippe Longepe938d21a2015-11-09 17:40:46 -0800845 rdmsrl(ATOM_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700846 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700847}
848
Philippe Longepe938d21a2015-11-09 17:40:46 -0800849static int atom_get_turbo_pstate(void)
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800850{
851 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700852
Philippe Longepe938d21a2015-11-09 17:40:46 -0800853 rdmsrl(ATOM_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700854 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800855}
856
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100857static u64 atom_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800858{
859 u64 val;
860 int32_t vid_fp;
861 u32 vid;
862
Chen Yu144c8e12015-07-29 23:53:10 +0800863 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400864 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800865 val |= (u64)1 << 32;
866
867 vid_fp = cpudata->vid.min + mul_fp(
868 int_tofp(pstate - cpudata->pstate.min_pstate),
869 cpudata->vid.ratio);
870
871 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
Dirk Brandewied022a652014-10-13 08:37:44 -0700872 vid = ceiling_fp(vid_fp);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800873
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700874 if (pstate > cpudata->pstate.max_pstate)
875 vid = cpudata->vid.turbo;
876
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +0100877 return val | vid;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800878}
879
Philippe Longepe1421df62015-11-09 17:40:47 -0800880static int silvermont_get_scaling(void)
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700881{
882 u64 value;
883 int i;
Philippe Longepe1421df62015-11-09 17:40:47 -0800884 /* Defined in Table 35-6 from SDM (Sept 2015) */
885 static int silvermont_freq_table[] = {
886 83300, 100000, 133300, 116700, 80000};
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700887
888 rdmsrl(MSR_FSB_FREQ, value);
Philippe Longepe1421df62015-11-09 17:40:47 -0800889 i = value & 0x7;
890 WARN_ON(i > 4);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700891
Philippe Longepe1421df62015-11-09 17:40:47 -0800892 return silvermont_freq_table[i];
893}
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700894
Philippe Longepe1421df62015-11-09 17:40:47 -0800895static int airmont_get_scaling(void)
896{
897 u64 value;
898 int i;
899 /* Defined in Table 35-10 from SDM (Sept 2015) */
900 static int airmont_freq_table[] = {
901 83300, 100000, 133300, 116700, 80000,
902 93300, 90000, 88900, 87500};
903
904 rdmsrl(MSR_FSB_FREQ, value);
905 i = value & 0xF;
906 WARN_ON(i > 8);
907
908 return airmont_freq_table[i];
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700909}
910
Philippe Longepe938d21a2015-11-09 17:40:46 -0800911static void atom_get_vid(struct cpudata *cpudata)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800912{
913 u64 value;
914
Philippe Longepe938d21a2015-11-09 17:40:46 -0800915 rdmsrl(ATOM_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700916 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
917 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800918 cpudata->vid.ratio = div_fp(
919 cpudata->vid.max - cpudata->vid.min,
920 int_tofp(cpudata->pstate.max_pstate -
921 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700922
Philippe Longepe938d21a2015-11-09 17:40:46 -0800923 rdmsrl(ATOM_TURBO_VIDS, value);
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700924 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800925}
926
Dirk Brandewie016c8152013-10-21 09:20:34 -0700927static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800928{
929 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700930
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000931 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800932 return (value >> 40) & 0xFF;
933}
934
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700935static int core_get_max_pstate_physical(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800936{
937 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700938
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000939 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800940 return (value >> 8) & 0xFF;
941}
942
Dirk Brandewie93f08222013-02-06 09:02:13 -0800943static int core_get_max_pstate(void)
944{
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -0700945 u64 tar;
946 u64 plat_info;
947 int max_pstate;
948 int err;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800949
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -0700950 rdmsrl(MSR_PLATFORM_INFO, plat_info);
951 max_pstate = (plat_info >> 8) & 0xFF;
952
953 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
954 if (!err) {
955 /* Do some sanity checking for safety */
956 if (plat_info & 0x600000000) {
957 u64 tdp_ctrl;
958 u64 tdp_ratio;
959 int tdp_msr;
960
961 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
962 if (err)
963 goto skip_tar;
964
965 tdp_msr = MSR_CONFIG_TDP_NOMINAL + tdp_ctrl;
966 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
967 if (err)
968 goto skip_tar;
969
Srinivas Pandruvada1becf032016-04-22 19:53:59 -0700970 /* For level 1 and 2, bits[23:16] contain the ratio */
971 if (tdp_ctrl)
972 tdp_ratio >>= 16;
973
974 tdp_ratio &= 0xff; /* ratios are only 8 bits long */
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -0700975 if (tdp_ratio - 1 == tar) {
976 max_pstate = tar;
977 pr_debug("max_pstate=TAC %x\n", max_pstate);
978 } else {
979 goto skip_tar;
980 }
981 }
982 }
983
984skip_tar:
985 return max_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800986}
987
Dirk Brandewie016c8152013-10-21 09:20:34 -0700988static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800989{
990 u64 value;
991 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700992
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000993 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700994 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -0700995 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800996 if (ret <= nont)
997 ret = nont;
998 return ret;
999}
1000
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001001static inline int core_get_scaling(void)
1002{
1003 return 100000;
1004}
1005
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001006static u64 core_get_val(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001007{
1008 u64 val;
1009
Chen Yu144c8e12015-07-29 23:53:10 +08001010 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001011 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001012 val |= (u64)1 << 32;
1013
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001014 return val;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001015}
1016
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001017static int knl_get_turbo_pstate(void)
1018{
1019 u64 value;
1020 int nont, ret;
1021
1022 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
1023 nont = core_get_max_pstate();
1024 ret = (((value) >> 8) & 0xFF);
1025 if (ret <= nont)
1026 ret = nont;
1027 return ret;
1028}
1029
Dirk Brandewie016c8152013-10-21 09:20:34 -07001030static struct cpu_defaults core_params = {
1031 .pid_policy = {
1032 .sample_rate_ms = 10,
1033 .deadband = 0,
1034 .setpoint = 97,
1035 .p_gain_pct = 20,
1036 .d_gain_pct = 0,
1037 .i_gain_pct = 0,
1038 },
1039 .funcs = {
1040 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001041 .get_max_physical = core_get_max_pstate_physical,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001042 .get_min = core_get_min_pstate,
1043 .get_turbo = core_get_turbo_pstate,
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001044 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001045 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001046 .get_target_pstate = get_target_pstate_use_performance,
Dirk Brandewie016c8152013-10-21 09:20:34 -07001047 },
1048};
1049
Philippe Longepe1421df62015-11-09 17:40:47 -08001050static struct cpu_defaults silvermont_params = {
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001051 .pid_policy = {
1052 .sample_rate_ms = 10,
1053 .deadband = 0,
Kristen Carlson Accardi6a82ba62015-04-10 11:06:43 -07001054 .setpoint = 60,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001055 .p_gain_pct = 14,
1056 .d_gain_pct = 0,
1057 .i_gain_pct = 4,
1058 },
1059 .funcs = {
Philippe Longepe938d21a2015-11-09 17:40:46 -08001060 .get_max = atom_get_max_pstate,
1061 .get_max_physical = atom_get_max_pstate,
1062 .get_min = atom_get_min_pstate,
1063 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001064 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001065 .get_scaling = silvermont_get_scaling,
1066 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001067 .get_target_pstate = get_target_pstate_use_cpu_load,
Philippe Longepe1421df62015-11-09 17:40:47 -08001068 },
1069};
1070
1071static struct cpu_defaults airmont_params = {
1072 .pid_policy = {
1073 .sample_rate_ms = 10,
1074 .deadband = 0,
1075 .setpoint = 60,
1076 .p_gain_pct = 14,
1077 .d_gain_pct = 0,
1078 .i_gain_pct = 4,
1079 },
1080 .funcs = {
1081 .get_max = atom_get_max_pstate,
1082 .get_max_physical = atom_get_max_pstate,
1083 .get_min = atom_get_min_pstate,
1084 .get_turbo = atom_get_turbo_pstate,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001085 .get_val = atom_get_val,
Philippe Longepe1421df62015-11-09 17:40:47 -08001086 .get_scaling = airmont_get_scaling,
Philippe Longepe938d21a2015-11-09 17:40:46 -08001087 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +01001088 .get_target_pstate = get_target_pstate_use_cpu_load,
Dirk Brandewie19e77c22013-10-21 09:20:35 -07001089 },
1090};
1091
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001092static struct cpu_defaults knl_params = {
1093 .pid_policy = {
1094 .sample_rate_ms = 10,
1095 .deadband = 0,
1096 .setpoint = 97,
1097 .p_gain_pct = 20,
1098 .d_gain_pct = 0,
1099 .i_gain_pct = 0,
1100 },
1101 .funcs = {
1102 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001103 .get_max_physical = core_get_max_pstate_physical,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001104 .get_min = core_get_min_pstate,
1105 .get_turbo = knl_get_turbo_pstate,
Lukasz Anaczkowski69cefc22015-07-21 10:41:13 +02001106 .get_scaling = core_get_scaling,
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001107 .get_val = core_get_val,
Philippe Longepe157386b2015-12-04 17:40:30 +01001108 .get_target_pstate = get_target_pstate_use_performance,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001109 },
1110};
1111
Dirk Brandewie93f08222013-02-06 09:02:13 -08001112static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
1113{
1114 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie7244cb62013-10-21 09:20:33 -07001115 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001116 int min_perf;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -07001117
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001118 if (limits->no_turbo || limits->turbo_disabled)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001119 max_perf = cpu->pstate.max_pstate;
1120
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001121 /*
1122 * performance can be limited by user through sysfs, by cpufreq
1123 * policy, or by cpu specific default values determined through
1124 * experimentation.
1125 */
Philippe Longepea158bed2016-03-06 08:34:04 +01001126 max_perf_adj = fp_toint(max_perf * limits->max_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001127 *max = clamp_t(int, max_perf_adj,
1128 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001129
Philippe Longepea158bed2016-03-06 08:34:04 +01001130 min_perf = fp_toint(max_perf * limits->min_perf);
Rafael J. Wysocki799281a2015-11-18 23:29:56 +01001131 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001132}
1133
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001134static inline void intel_pstate_record_pstate(struct cpudata *cpu, int pstate)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001135{
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001136 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001137 cpu->pstate.current_pstate = pstate;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001138}
Dirk Brandewie93f08222013-02-06 09:02:13 -08001139
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001140static void intel_pstate_set_min_pstate(struct cpudata *cpu)
1141{
1142 int pstate = cpu->pstate.min_pstate;
1143
1144 intel_pstate_record_pstate(cpu, pstate);
1145 /*
1146 * Generally, there is no guarantee that this code will always run on
1147 * the CPU being updated, so force the register update to run on the
1148 * right CPU.
1149 */
1150 wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL,
1151 pstate_funcs.get_val(cpu, pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001152}
1153
Dirk Brandewie93f08222013-02-06 09:02:13 -08001154static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
1155{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001156 cpu->pstate.min_pstate = pstate_funcs.get_min();
1157 cpu->pstate.max_pstate = pstate_funcs.get_max();
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001158 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
Dirk Brandewie016c8152013-10-21 09:20:34 -07001159 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001160 cpu->pstate.scaling = pstate_funcs.get_scaling();
Dirk Brandewie93f08222013-02-06 09:02:13 -08001161
Dirk Brandewie007bea02013-12-18 10:32:39 -08001162 if (pstate_funcs.get_vid)
1163 pstate_funcs.get_vid(cpu);
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001164
1165 intel_pstate_set_min_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001166}
1167
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001168static inline void intel_pstate_calc_avg_perf(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001169{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +03001170 struct sample *sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001171
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001172 sample->core_avg_perf = div_ext_fp(sample->aperf, sample->mperf);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001173}
1174
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001175static inline bool intel_pstate_sample(struct cpudata *cpu, u64 time)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001176{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001177 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001178 unsigned long flags;
Doug Smythies4055fad2015-04-11 21:10:26 -07001179 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001180
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001181 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001182 rdmsrl(MSR_IA32_APERF, aperf);
1183 rdmsrl(MSR_IA32_MPERF, mperf);
Philippe Longepee70eed22015-12-04 17:40:32 +01001184 tsc = rdtsc();
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001185 if (cpu->prev_mperf == mperf || cpu->prev_tsc == tsc) {
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001186 local_irq_restore(flags);
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001187 return false;
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -07001188 }
Stratos Karafotis4ab60c32014-07-18 08:37:24 -07001189 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001190
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001191 cpu->last_sample_time = cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001192 cpu->sample.time = time;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001193 cpu->sample.aperf = aperf;
1194 cpu->sample.mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001195 cpu->sample.tsc = tsc;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001196 cpu->sample.aperf -= cpu->prev_aperf;
1197 cpu->sample.mperf -= cpu->prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001198 cpu->sample.tsc -= cpu->prev_tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001199
Dirk Brandewie93f08222013-02-06 09:02:13 -08001200 cpu->prev_aperf = aperf;
1201 cpu->prev_mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -07001202 cpu->prev_tsc = tsc;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001203 /*
1204 * First time this function is invoked in a given cycle, all of the
1205 * previous sample data fields are equal to zero or stale and they must
1206 * be populated with meaningful numbers for things to work, so assume
1207 * that sample.time will always be reset before setting the utilization
1208 * update hook and make the caller skip the sample then.
1209 */
1210 return !!cpu->last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001211}
1212
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001213static inline int32_t get_avg_frequency(struct cpudata *cpu)
1214{
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001215 return mul_ext_fp(cpu->sample.core_avg_perf,
1216 cpu->pstate.max_pstate_physical * cpu->pstate.scaling);
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001217}
1218
Philippe Longepebdcaa232016-04-22 11:46:09 -07001219static inline int32_t get_avg_pstate(struct cpudata *cpu)
1220{
Rafael J. Wysocki8edb0a62016-05-11 19:10:42 +02001221 return mul_ext_fp(cpu->pstate.max_pstate_physical,
1222 cpu->sample.core_avg_perf);
Philippe Longepebdcaa232016-04-22 11:46:09 -07001223}
1224
Philippe Longepee70eed22015-12-04 17:40:32 +01001225static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
1226{
1227 struct sample *sample = &cpu->sample;
Philippe Longepe63d1d652015-12-04 17:40:35 +01001228 u64 cummulative_iowait, delta_iowait_us;
1229 u64 delta_iowait_mperf;
1230 u64 mperf, now;
Philippe Longepee70eed22015-12-04 17:40:32 +01001231 int32_t cpu_load;
1232
Philippe Longepe63d1d652015-12-04 17:40:35 +01001233 cummulative_iowait = get_cpu_iowait_time_us(cpu->cpu, &now);
1234
1235 /*
1236 * Convert iowait time into number of IO cycles spent at max_freq.
1237 * IO is considered as busy only for the cpu_load algorithm. For
1238 * performance this is not needed since we always try to reach the
1239 * maximum P-State, so we are already boosting the IOs.
1240 */
1241 delta_iowait_us = cummulative_iowait - cpu->prev_cummulative_iowait;
1242 delta_iowait_mperf = div64_u64(delta_iowait_us * cpu->pstate.scaling *
1243 cpu->pstate.max_pstate, MSEC_PER_SEC);
1244
1245 mperf = cpu->sample.mperf + delta_iowait_mperf;
1246 cpu->prev_cummulative_iowait = cummulative_iowait;
1247
Philippe Longepee70eed22015-12-04 17:40:32 +01001248 /*
1249 * The load can be estimated as the ratio of the mperf counter
1250 * running at a constant frequency during active periods
1251 * (C0) and the time stamp counter running at the same frequency
1252 * also during C-states.
1253 */
Philippe Longepe63d1d652015-12-04 17:40:35 +01001254 cpu_load = div64_u64(int_tofp(100) * mperf, sample->tsc);
Philippe Longepee70eed22015-12-04 17:40:32 +01001255 cpu->sample.busy_scaled = cpu_load;
1256
Philippe Longepebdcaa232016-04-22 11:46:09 -07001257 return get_avg_pstate(cpu) - pid_calc(&cpu->pid, cpu_load);
Philippe Longepee70eed22015-12-04 17:40:32 +01001258}
1259
Philippe Longepe157386b2015-12-04 17:40:30 +01001260static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001261{
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001262 int32_t perf_scaled, max_pstate, current_pstate, sample_ratio;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001263 u64 duration_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001264
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001265 /*
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001266 * perf_scaled is the average performance during the last sampling
1267 * period scaled by the ratio of the maximum P-state to the P-state
1268 * requested last time (in percent). That measures the system's
1269 * response to the previous P-state selection.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001270 */
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001271 max_pstate = cpu->pstate.max_pstate_physical;
1272 current_pstate = cpu->pstate.current_pstate;
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001273 perf_scaled = mul_ext_fp(cpu->sample.core_avg_perf,
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001274 div_fp(100 * max_pstate, current_pstate));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001275
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001276 /*
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001277 * Since our utilization update callback will not run unless we are
1278 * in C0, check if the actual elapsed time is significantly greater (3x)
1279 * than our sample interval. If it is, then we were idle for a long
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001280 * enough period of time to adjust our performance metric.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -08001281 */
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001282 duration_ns = cpu->sample.time - cpu->last_sample_time;
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001283 if ((s64)duration_ns > pid_params.sample_rate_ns * 3) {
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001284 sample_ratio = div_fp(pid_params.sample_rate_ns, duration_ns);
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001285 perf_scaled = mul_fp(perf_scaled, sample_ratio);
Rafael J. Wysockiffb81052016-04-10 05:59:10 +02001286 } else {
1287 sample_ratio = div_fp(100 * cpu->sample.mperf, cpu->sample.tsc);
1288 if (sample_ratio < int_tofp(1))
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001289 perf_scaled = 0;
Dirk Brandewiec4ee8412014-05-29 09:32:24 -07001290 }
1291
Rafael J. Wysocki1aa7a6e2016-05-11 19:11:26 +02001292 cpu->sample.busy_scaled = perf_scaled;
1293 return cpu->pstate.current_pstate - pid_calc(&cpu->pid, perf_scaled);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001294}
1295
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001296static inline void intel_pstate_update_pstate(struct cpudata *cpu, int pstate)
1297{
1298 int max_perf, min_perf;
1299
1300 update_turbo_state();
1301
1302 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
1303 pstate = clamp_t(int, pstate, min_perf, max_perf);
1304 if (pstate == cpu->pstate.current_pstate)
1305 return;
1306
1307 intel_pstate_record_pstate(cpu, pstate);
1308 wrmsrl(MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));
1309}
1310
Dirk Brandewie93f08222013-02-06 09:02:13 -08001311static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
1312{
Philippe Longepe157386b2015-12-04 17:40:30 +01001313 int from, target_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -07001314 struct sample *sample;
1315
1316 from = cpu->pstate.current_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001317
Philippe Longepe157386b2015-12-04 17:40:30 +01001318 target_pstate = pstate_funcs.get_target_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001319
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001320 intel_pstate_update_pstate(cpu, target_pstate);
Doug Smythies4055fad2015-04-11 21:10:26 -07001321
1322 sample = &cpu->sample;
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001323 trace_pstate_sample(mul_ext_fp(100, sample->core_avg_perf),
Philippe Longepe157386b2015-12-04 17:40:30 +01001324 fp_toint(sample->busy_scaled),
Doug Smythies4055fad2015-04-11 21:10:26 -07001325 from,
1326 cpu->pstate.current_pstate,
1327 sample->mperf,
1328 sample->aperf,
1329 sample->tsc,
Philippe Longepe8fa520a2016-03-06 08:34:06 +01001330 get_avg_frequency(cpu));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001331}
1332
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001333static void intel_pstate_update_util(struct update_util_data *data, u64 time,
1334 unsigned long util, unsigned long max)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001335{
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001336 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
1337 u64 delta_ns = time - cpu->sample.time;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001338
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001339 if ((s64)delta_ns >= pid_params.sample_rate_ns) {
Rafael J. Wysocki4fec7ad2016-03-10 23:45:19 +01001340 bool sample_taken = intel_pstate_sample(cpu, time);
1341
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001342 if (sample_taken) {
Rafael J. Wysockia1c97872016-05-11 19:09:12 +02001343 intel_pstate_calc_avg_perf(cpu);
Rafael J. Wysocki6d45b712016-05-04 14:01:10 +02001344 if (!hwp_active)
1345 intel_pstate_adjust_busy_pstate(cpu);
1346 }
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001347 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001348}
1349
1350#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -08001351 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1352 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001353
1354static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -07001355 ICPU(0x2a, core_params),
1356 ICPU(0x2d, core_params),
Philippe Longepe1421df62015-11-09 17:40:47 -08001357 ICPU(0x37, silvermont_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -07001358 ICPU(0x3a, core_params),
1359 ICPU(0x3c, core_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -07001360 ICPU(0x3d, core_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -07001361 ICPU(0x3e, core_params),
1362 ICPU(0x3f, core_params),
1363 ICPU(0x45, core_params),
1364 ICPU(0x46, core_params),
Dirk Brandewie43f8a962014-11-06 09:50:45 -08001365 ICPU(0x47, core_params),
Philippe Longepe1421df62015-11-09 17:40:47 -08001366 ICPU(0x4c, airmont_params),
Kristen Carlson Accardi7ab02562015-01-28 13:53:28 -08001367 ICPU(0x4e, core_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -07001368 ICPU(0x4f, core_params),
Kristen Carlson Accardi1c939122015-08-05 12:47:14 -07001369 ICPU(0x5e, core_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -07001370 ICPU(0x56, core_params),
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001371 ICPU(0x57, knl_params),
Dirk Brandewie93f08222013-02-06 09:02:13 -08001372 {}
1373};
1374MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1375
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001376static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] = {
1377 ICPU(0x56, core_params),
1378 {}
1379};
1380
Dirk Brandewie93f08222013-02-06 09:02:13 -08001381static int intel_pstate_init_cpu(unsigned int cpunum)
1382{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001383 struct cpudata *cpu;
1384
Dirk Brandewiec0348712014-10-13 08:37:42 -07001385 if (!all_cpu_data[cpunum])
1386 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
1387 GFP_KERNEL);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001388 if (!all_cpu_data[cpunum])
1389 return -ENOMEM;
1390
1391 cpu = all_cpu_data[cpunum];
1392
Dirk Brandewie93f08222013-02-06 09:02:13 -08001393 cpu->cpu = cpunum;
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001394
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001395 if (hwp_active) {
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001396 intel_pstate_hwp_enable(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001397 pid_params.sample_rate_ms = 50;
1398 pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
1399 }
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001400
Vincent Minet179e8472014-07-05 01:51:33 +02001401 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001402
Dirk Brandewie93f08222013-02-06 09:02:13 -08001403 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001404
Joe Perches4836df12016-04-05 13:28:23 -07001405 pr_debug("controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001406
1407 return 0;
1408}
1409
1410static unsigned int intel_pstate_get(unsigned int cpu_num)
1411{
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001412 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -08001413
Rafael J. Wysockif96fd0c2016-05-07 02:24:48 +02001414 return cpu ? get_avg_frequency(cpu) : 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001415}
1416
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001417static void intel_pstate_set_update_util_hook(unsigned int cpu_num)
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001418{
Rafael J. Wysockifebce402016-04-02 01:06:21 +02001419 struct cpudata *cpu = all_cpu_data[cpu_num];
1420
1421 /* Prevent intel_pstate_update_util() from using stale data. */
1422 cpu->sample.time = 0;
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001423 cpufreq_add_update_util_hook(cpu_num, &cpu->update_util,
1424 intel_pstate_update_util);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001425 cpu->update_util_set = true;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001426}
1427
1428static void intel_pstate_clear_update_util_hook(unsigned int cpu)
1429{
Chen Yu4578ee7e2016-05-11 14:33:08 +08001430 struct cpudata *cpu_data = all_cpu_data[cpu];
1431
1432 if (!cpu_data->update_util_set)
1433 return;
1434
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +02001435 cpufreq_remove_update_util_hook(cpu);
Chen Yu4578ee7e2016-05-11 14:33:08 +08001436 cpu_data->update_util_set = false;
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001437 synchronize_sched();
1438}
1439
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001440static void intel_pstate_set_performance_limits(struct perf_limits *limits)
1441{
1442 limits->no_turbo = 0;
1443 limits->turbo_disabled = 0;
1444 limits->max_perf_pct = 100;
1445 limits->max_perf = int_tofp(1);
1446 limits->min_perf_pct = 100;
1447 limits->min_perf = int_tofp(1);
1448 limits->max_policy_pct = 100;
1449 limits->max_sysfs_pct = 100;
1450 limits->min_policy_pct = 0;
1451 limits->min_sysfs_pct = 0;
1452}
1453
Dirk Brandewie93f08222013-02-06 09:02:13 -08001454static int intel_pstate_set_policy(struct cpufreq_policy *policy)
1455{
Srinivas Pandruvada3be92002016-04-27 15:48:07 -07001456 struct cpudata *cpu;
1457
Dirk Brandewied3929b82013-03-05 14:15:26 -08001458 if (!policy->cpuinfo.max_freq)
1459 return -ENODEV;
1460
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001461 intel_pstate_clear_update_util_hook(policy->cpu);
1462
Srinivas Pandruvada3be92002016-04-27 15:48:07 -07001463 cpu = all_cpu_data[0];
Rafael J. Wysockic749c642016-05-12 00:46:45 +02001464 if (cpu->pstate.max_pstate_physical > cpu->pstate.max_pstate &&
1465 policy->max < policy->cpuinfo.max_freq &&
1466 policy->max > cpu->pstate.max_pstate * cpu->pstate.scaling) {
1467 pr_debug("policy->max > max non turbo frequency\n");
1468 policy->max = policy->cpuinfo.max_freq;
Srinivas Pandruvada3be92002016-04-27 15:48:07 -07001469 }
1470
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001471 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001472 limits = &performance_limits;
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001473 if (policy->max >= policy->cpuinfo.max_freq) {
Joe Perches4836df12016-04-05 13:28:23 -07001474 pr_debug("set performance\n");
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001475 intel_pstate_set_performance_limits(limits);
1476 goto out;
1477 }
1478 } else {
Joe Perches4836df12016-04-05 13:28:23 -07001479 pr_debug("set powersave\n");
Srinivas Pandruvada30a39152016-04-03 19:42:11 -07001480 limits = &powersave_limits;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001481 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001482
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001483 limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
1484 limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
Prarit Bhargava8478f532015-11-20 18:47:56 -05001485 limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
1486 policy->cpuinfo.max_freq);
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001487 limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
Chen Yu43717aa2015-09-09 18:27:31 +08001488
1489 /* Normalize user input to [min_policy_pct, max_policy_pct] */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001490 limits->min_perf_pct = max(limits->min_policy_pct,
1491 limits->min_sysfs_pct);
1492 limits->min_perf_pct = min(limits->max_policy_pct,
1493 limits->min_perf_pct);
1494 limits->max_perf_pct = min(limits->max_policy_pct,
1495 limits->max_sysfs_pct);
1496 limits->max_perf_pct = max(limits->min_policy_pct,
1497 limits->max_perf_pct);
Prarit Bhargava88b7b7c2015-12-08 13:44:59 -05001498 limits->max_perf = round_up(limits->max_perf, FRAC_BITS);
Chen Yu43717aa2015-09-09 18:27:31 +08001499
1500 /* Make sure min_perf_pct <= max_perf_pct */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001501 limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
Chen Yu43717aa2015-09-09 18:27:31 +08001502
Rafael J. Wysocki22590ef2016-04-09 01:25:58 +02001503 limits->min_perf = div_fp(limits->min_perf_pct, 100);
1504 limits->max_perf = div_fp(limits->max_perf_pct, 100);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001505
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001506 out:
1507 intel_pstate_set_update_util_hook(policy->cpu);
1508
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001509 intel_pstate_hwp_set_policy(policy);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001510
Dirk Brandewie93f08222013-02-06 09:02:13 -08001511 return 0;
1512}
1513
1514static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
1515{
Viresh Kumarbe49e342013-10-02 14:13:19 +05301516 cpufreq_verify_within_cpu_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001517
Stratos Karafotis285cb992014-07-18 08:37:21 -07001518 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -07001519 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001520 return -EINVAL;
1521
1522 return 0;
1523}
1524
Dirk Brandewiebb180082014-03-19 08:45:54 -07001525static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001526{
Dirk Brandewiebb180082014-03-19 08:45:54 -07001527 int cpu_num = policy->cpu;
1528 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -08001529
Joe Perches4836df12016-04-05 13:28:23 -07001530 pr_debug("CPU %d exiting\n", cpu_num);
Dirk Brandewiebb180082014-03-19 08:45:54 -07001531
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001532 intel_pstate_clear_update_util_hook(cpu_num);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001533
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001534 if (hwp_active)
1535 return;
1536
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001537 intel_pstate_set_min_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001538}
1539
Paul Gortmaker27609842013-06-19 13:54:04 -04001540static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001541{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001542 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -07001543 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001544
1545 rc = intel_pstate_init_cpu(policy->cpu);
1546 if (rc)
1547 return rc;
1548
1549 cpu = all_cpu_data[policy->cpu];
1550
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001551 if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001552 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1553 else
1554 policy->policy = CPUFREQ_POLICY_POWERSAVE;
1555
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001556 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
1557 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001558
1559 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001560 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
1561 policy->cpuinfo.max_freq =
1562 cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001563 intel_pstate_init_acpi_perf_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001564 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
1565 cpumask_set_cpu(policy->cpu, policy->cpus);
1566
1567 return 0;
1568}
1569
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001570static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
1571{
1572 intel_pstate_exit_perf_limits(policy);
1573
1574 return 0;
1575}
1576
Dirk Brandewie93f08222013-02-06 09:02:13 -08001577static struct cpufreq_driver intel_pstate_driver = {
1578 .flags = CPUFREQ_CONST_LOOPS,
1579 .verify = intel_pstate_verify_policy,
1580 .setpolicy = intel_pstate_set_policy,
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001581 .resume = intel_pstate_hwp_set_policy,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001582 .get = intel_pstate_get,
1583 .init = intel_pstate_cpu_init,
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001584 .exit = intel_pstate_cpu_exit,
Dirk Brandewiebb180082014-03-19 08:45:54 -07001585 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001586 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -08001587};
1588
Dirk Brandewie6be26492013-02-15 22:55:10 +01001589static int __initdata no_load;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001590static int __initdata no_hwp;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08001591static int __initdata hwp_only;
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09001592static unsigned int force_load;
Dirk Brandewie6be26492013-02-15 22:55:10 +01001593
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001594static int intel_pstate_msrs_not_valid(void)
1595{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001596 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -07001597 !pstate_funcs.get_min() ||
1598 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001599 return -ENODEV;
1600
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001601 return 0;
1602}
Dirk Brandewie016c8152013-10-21 09:20:34 -07001603
Dirk Brandewiee0a261a2013-10-30 08:38:32 -07001604static void copy_pid_params(struct pstate_adjust_policy *policy)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001605{
1606 pid_params.sample_rate_ms = policy->sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001607 pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001608 pid_params.p_gain_pct = policy->p_gain_pct;
1609 pid_params.i_gain_pct = policy->i_gain_pct;
1610 pid_params.d_gain_pct = policy->d_gain_pct;
1611 pid_params.deadband = policy->deadband;
1612 pid_params.setpoint = policy->setpoint;
1613}
1614
Dirk Brandewiee0a261a2013-10-30 08:38:32 -07001615static void copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001616{
1617 pstate_funcs.get_max = funcs->get_max;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001618 pstate_funcs.get_max_physical = funcs->get_max_physical;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001619 pstate_funcs.get_min = funcs->get_min;
1620 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001621 pstate_funcs.get_scaling = funcs->get_scaling;
Rafael J. Wysockifdfdb2b2016-03-18 23:20:02 +01001622 pstate_funcs.get_val = funcs->get_val;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001623 pstate_funcs.get_vid = funcs->get_vid;
Philippe Longepe157386b2015-12-04 17:40:30 +01001624 pstate_funcs.get_target_pstate = funcs->get_target_pstate;
1625
Dirk Brandewie016c8152013-10-21 09:20:34 -07001626}
1627
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001628#ifdef CONFIG_ACPI
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001629
1630static bool intel_pstate_no_acpi_pss(void)
1631{
1632 int i;
1633
1634 for_each_possible_cpu(i) {
1635 acpi_status status;
1636 union acpi_object *pss;
1637 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1638 struct acpi_processor *pr = per_cpu(processors, i);
1639
1640 if (!pr)
1641 continue;
1642
1643 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
1644 if (ACPI_FAILURE(status))
1645 continue;
1646
1647 pss = buffer.pointer;
1648 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
1649 kfree(pss);
1650 return false;
1651 }
1652
1653 kfree(pss);
1654 }
1655
1656 return true;
1657}
1658
ethan zhao966916e2014-12-01 11:32:08 +09001659static bool intel_pstate_has_acpi_ppc(void)
1660{
1661 int i;
1662
1663 for_each_possible_cpu(i) {
1664 struct acpi_processor *pr = per_cpu(processors, i);
1665
1666 if (!pr)
1667 continue;
1668 if (acpi_has_method(pr->handle, "_PPC"))
1669 return true;
1670 }
1671 return false;
1672}
1673
1674enum {
1675 PSS,
1676 PPC,
1677};
1678
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001679struct hw_vendor_info {
1680 u16 valid;
1681 char oem_id[ACPI_OEM_ID_SIZE];
1682 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
ethan zhao966916e2014-12-01 11:32:08 +09001683 int oem_pwr_table;
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001684};
1685
1686/* Hardware vendor-specific info that has its own power management modes */
1687static struct hw_vendor_info vendor_info[] = {
ethan zhao966916e2014-12-01 11:32:08 +09001688 {1, "HP ", "ProLiant", PSS},
1689 {1, "ORACLE", "X4-2 ", PPC},
1690 {1, "ORACLE", "X4-2L ", PPC},
1691 {1, "ORACLE", "X4-2B ", PPC},
1692 {1, "ORACLE", "X3-2 ", PPC},
1693 {1, "ORACLE", "X3-2L ", PPC},
1694 {1, "ORACLE", "X3-2B ", PPC},
1695 {1, "ORACLE", "X4470M2 ", PPC},
1696 {1, "ORACLE", "X4270M3 ", PPC},
1697 {1, "ORACLE", "X4270M2 ", PPC},
1698 {1, "ORACLE", "X4170M2 ", PPC},
Ethan Zhao5aecc3c2015-08-05 09:28:50 +09001699 {1, "ORACLE", "X4170 M3", PPC},
1700 {1, "ORACLE", "X4275 M3", PPC},
1701 {1, "ORACLE", "X6-2 ", PPC},
1702 {1, "ORACLE", "Sudbury ", PPC},
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001703 {0, "", ""},
1704};
1705
1706static bool intel_pstate_platform_pwr_mgmt_exists(void)
1707{
1708 struct acpi_table_header hdr;
1709 struct hw_vendor_info *v_info;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001710 const struct x86_cpu_id *id;
1711 u64 misc_pwr;
1712
1713 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
1714 if (id) {
1715 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
1716 if ( misc_pwr & (1 << 8))
1717 return true;
1718 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001719
Stratos Karafotisc4108332014-07-18 08:37:23 -07001720 if (acpi_disabled ||
1721 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001722 return false;
1723
1724 for (v_info = vendor_info; v_info->valid; v_info++) {
Stratos Karafotisc4108332014-07-18 08:37:23 -07001725 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
ethan zhao966916e2014-12-01 11:32:08 +09001726 !strncmp(hdr.oem_table_id, v_info->oem_table_id,
1727 ACPI_OEM_TABLE_ID_SIZE))
1728 switch (v_info->oem_pwr_table) {
1729 case PSS:
1730 return intel_pstate_no_acpi_pss();
1731 case PPC:
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09001732 return intel_pstate_has_acpi_ppc() &&
1733 (!force_load);
ethan zhao966916e2014-12-01 11:32:08 +09001734 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001735 }
1736
1737 return false;
1738}
1739#else /* CONFIG_ACPI not enabled */
1740static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
ethan zhao966916e2014-12-01 11:32:08 +09001741static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001742#endif /* CONFIG_ACPI */
1743
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08001744static const struct x86_cpu_id hwp_support_ids[] __initconst = {
1745 { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_HWP },
1746 {}
1747};
1748
Dirk Brandewie93f08222013-02-06 09:02:13 -08001749static int __init intel_pstate_init(void)
1750{
Dirk Brandewie907cc902013-03-05 14:15:27 -08001751 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001752 const struct x86_cpu_id *id;
Borislav Petkov64df1fd2015-04-03 15:19:53 +02001753 struct cpu_defaults *cpu_def;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001754
Dirk Brandewie6be26492013-02-15 22:55:10 +01001755 if (no_load)
1756 return -ENODEV;
1757
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08001758 if (x86_match_cpu(hwp_support_ids) && !no_hwp) {
1759 copy_cpu_funcs(&core_params.funcs);
1760 hwp_active++;
1761 goto hwp_cpu_matched;
1762 }
1763
Dirk Brandewie93f08222013-02-06 09:02:13 -08001764 id = x86_match_cpu(intel_pstate_cpu_ids);
1765 if (!id)
1766 return -ENODEV;
1767
Borislav Petkov64df1fd2015-04-03 15:19:53 +02001768 cpu_def = (struct cpu_defaults *)id->driver_data;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001769
Borislav Petkov64df1fd2015-04-03 15:19:53 +02001770 copy_pid_params(&cpu_def->pid_policy);
1771 copy_cpu_funcs(&cpu_def->funcs);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001772
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001773 if (intel_pstate_msrs_not_valid())
1774 return -ENODEV;
1775
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08001776hwp_cpu_matched:
1777 /*
1778 * The Intel pstate driver will be ignored if the platform
1779 * firmware has its own power management modes.
1780 */
1781 if (intel_pstate_platform_pwr_mgmt_exists())
1782 return -ENODEV;
1783
Joe Perches4836df12016-04-05 13:28:23 -07001784 pr_info("Intel P-state driver initializing\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -08001785
Wei Yongjunb57ffac2013-05-13 08:03:43 +00001786 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08001787 if (!all_cpu_data)
1788 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001789
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08001790 if (!hwp_active && hwp_only)
1791 goto out;
1792
Dirk Brandewie93f08222013-02-06 09:02:13 -08001793 rc = cpufreq_register_driver(&intel_pstate_driver);
1794 if (rc)
1795 goto out;
1796
1797 intel_pstate_debug_expose_params();
1798 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001799
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08001800 if (hwp_active)
Joe Perches4836df12016-04-05 13:28:23 -07001801 pr_info("HWP enabled\n");
Srinivas Pandruvada7791e4a2016-02-25 15:09:19 -08001802
Dirk Brandewie93f08222013-02-06 09:02:13 -08001803 return rc;
1804out:
Dirk Brandewie907cc902013-03-05 14:15:27 -08001805 get_online_cpus();
1806 for_each_online_cpu(cpu) {
1807 if (all_cpu_data[cpu]) {
Rafael J. Wysockibb6ab522016-03-31 17:42:15 +02001808 intel_pstate_clear_update_util_hook(cpu);
Dirk Brandewie907cc902013-03-05 14:15:27 -08001809 kfree(all_cpu_data[cpu]);
1810 }
1811 }
1812
1813 put_online_cpus();
1814 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001815 return -ENODEV;
1816}
1817device_initcall(intel_pstate_init);
1818
Dirk Brandewie6be26492013-02-15 22:55:10 +01001819static int __init intel_pstate_setup(char *str)
1820{
1821 if (!str)
1822 return -EINVAL;
1823
1824 if (!strcmp(str, "disable"))
1825 no_load = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04001826 if (!strcmp(str, "no_hwp")) {
Joe Perches4836df12016-04-05 13:28:23 -07001827 pr_info("HWP disabled\n");
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001828 no_hwp = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04001829 }
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09001830 if (!strcmp(str, "force"))
1831 force_load = 1;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08001832 if (!strcmp(str, "hwp_only"))
1833 hwp_only = 1;
Srinivas Pandruvada9522a2f2016-04-27 15:48:06 -07001834
1835#ifdef CONFIG_ACPI
1836 if (!strcmp(str, "support_acpi_ppc"))
1837 acpi_ppc = true;
1838#endif
1839
Dirk Brandewie6be26492013-02-15 22:55:10 +01001840 return 0;
1841}
1842early_param("intel_pstate", intel_pstate_setup);
1843
Dirk Brandewie93f08222013-02-06 09:02:13 -08001844MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
1845MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
1846MODULE_LICENSE("GPL");