blob: f4d85c2ae7b18de7d986504e3eb089944f90c431 [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
13#include <linux/kernel.h>
14#include <linux/kernel_stat.h>
15#include <linux/module.h>
16#include <linux/ktime.h>
17#include <linux/hrtimer.h>
18#include <linux/tick.h>
19#include <linux/slab.h>
20#include <linux/sched.h>
21#include <linux/list.h>
22#include <linux/cpu.h>
23#include <linux/cpufreq.h>
24#include <linux/sysfs.h>
25#include <linux/types.h>
26#include <linux/fs.h>
27#include <linux/debugfs.h>
Adrian Huangfbbcdc02013-10-31 23:24:05 +080028#include <linux/acpi.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100029#include <linux/vmalloc.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080030#include <trace/events/power.h>
31
32#include <asm/div64.h>
33#include <asm/msr.h>
34#include <asm/cpu_device_id.h>
Borislav Petkov64df1fd2015-04-03 15:19:53 +020035#include <asm/cpufeature.h>
Dirk Brandewie93f08222013-02-06 09:02:13 -080036
Philippe Longepe938d21a2015-11-09 17:40:46 -080037#define ATOM_RATIOS 0x66a
38#define ATOM_VIDS 0x66b
39#define ATOM_TURBO_RATIOS 0x66c
40#define ATOM_TURBO_VIDS 0x66d
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -080041
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070042#define FRAC_BITS 8
Dirk Brandewie93f08222013-02-06 09:02:13 -080043#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
44#define fp_toint(X) ((X) >> FRAC_BITS)
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070045
Dirk Brandewie93f08222013-02-06 09:02:13 -080046static inline int32_t mul_fp(int32_t x, int32_t y)
47{
48 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
49}
50
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040051static inline int32_t div_fp(s64 x, s64 y)
Dirk Brandewie93f08222013-02-06 09:02:13 -080052{
Prarit Bhargava7180ddd2015-06-15 13:43:29 -040053 return div64_s64((int64_t)x << FRAC_BITS, y);
Dirk Brandewie93f08222013-02-06 09:02:13 -080054}
55
Dirk Brandewied022a652014-10-13 08:37:44 -070056static inline int ceiling_fp(int32_t x)
57{
58 int mask, ret;
59
60 ret = fp_toint(x);
61 mask = (1 << FRAC_BITS) - 1;
62 if (x & mask)
63 ret += 1;
64 return ret;
65}
66
Dirk Brandewie93f08222013-02-06 09:02:13 -080067struct sample {
Brennan Shacklettd253d2a2013-10-21 09:20:32 -070068 int32_t core_pct_busy;
Philippe Longepe157386b2015-12-04 17:40:30 +010069 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -080070 u64 aperf;
71 u64 mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -070072 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -080073 int freq;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +010074 u64 time;
Dirk Brandewie93f08222013-02-06 09:02:13 -080075};
76
77struct pstate_data {
78 int current_pstate;
79 int min_pstate;
80 int max_pstate;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -070081 int max_pstate_physical;
Dirk Brandewieb27580b2014-10-13 08:37:43 -070082 int scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -080083 int turbo_pstate;
84};
85
Dirk Brandewie007bea02013-12-18 10:32:39 -080086struct vid_data {
Dirk Brandewie21855ff2014-05-08 12:57:23 -070087 int min;
88 int max;
89 int turbo;
Dirk Brandewie007bea02013-12-18 10:32:39 -080090 int32_t ratio;
91};
92
Dirk Brandewie93f08222013-02-06 09:02:13 -080093struct _pid {
94 int setpoint;
95 int32_t integral;
96 int32_t p_gain;
97 int32_t i_gain;
98 int32_t d_gain;
99 int deadband;
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700100 int32_t last_err;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800101};
102
103struct cpudata {
104 int cpu;
105
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100106 struct update_util_data update_util;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800107
Dirk Brandewie93f08222013-02-06 09:02:13 -0800108 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800109 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800110 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800111
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100112 u64 last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800113 u64 prev_aperf;
114 u64 prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700115 u64 prev_tsc;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100116 u64 prev_cummulative_iowait;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800117 struct sample sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800118};
119
120static struct cpudata **all_cpu_data;
121struct pstate_adjust_policy {
122 int sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100123 s64 sample_rate_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800124 int deadband;
125 int setpoint;
126 int p_gain_pct;
127 int d_gain_pct;
128 int i_gain_pct;
129};
130
Dirk Brandewie016c8152013-10-21 09:20:34 -0700131struct pstate_funcs {
132 int (*get_max)(void);
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700133 int (*get_max_physical)(void);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700134 int (*get_min)(void);
135 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700136 int (*get_scaling)(void);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800137 void (*set)(struct cpudata*, int pstate);
138 void (*get_vid)(struct cpudata *);
Philippe Longepe157386b2015-12-04 17:40:30 +0100139 int32_t (*get_target_pstate)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800140};
141
Dirk Brandewie016c8152013-10-21 09:20:34 -0700142struct cpu_defaults {
143 struct pstate_adjust_policy pid_policy;
144 struct pstate_funcs funcs;
145};
146
Philippe Longepe157386b2015-12-04 17:40:30 +0100147static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu);
Philippe Longepee70eed22015-12-04 17:40:32 +0100148static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu);
Philippe Longepe157386b2015-12-04 17:40:30 +0100149
Dirk Brandewie016c8152013-10-21 09:20:34 -0700150static struct pstate_adjust_policy pid_params;
151static struct pstate_funcs pstate_funcs;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800152static int hwp_active;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700153
Dirk Brandewie93f08222013-02-06 09:02:13 -0800154struct perf_limits {
155 int no_turbo;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700156 int turbo_disabled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800157 int max_perf_pct;
158 int min_perf_pct;
159 int32_t max_perf;
160 int32_t min_perf;
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700161 int max_policy_pct;
162 int max_sysfs_pct;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800163 int min_policy_pct;
164 int min_sysfs_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800165};
166
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400167static struct perf_limits performance_limits = {
168 .no_turbo = 0,
169 .turbo_disabled = 0,
170 .max_perf_pct = 100,
171 .max_perf = int_tofp(1),
172 .min_perf_pct = 100,
173 .min_perf = int_tofp(1),
174 .max_policy_pct = 100,
175 .max_sysfs_pct = 100,
176 .min_policy_pct = 0,
177 .min_sysfs_pct = 0,
178};
179
180static struct perf_limits powersave_limits = {
Dirk Brandewie93f08222013-02-06 09:02:13 -0800181 .no_turbo = 0,
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700182 .turbo_disabled = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800183 .max_perf_pct = 100,
184 .max_perf = int_tofp(1),
185 .min_perf_pct = 0,
186 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700187 .max_policy_pct = 100,
188 .max_sysfs_pct = 100,
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800189 .min_policy_pct = 0,
190 .min_sysfs_pct = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800191};
192
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400193#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
194static struct perf_limits *limits = &performance_limits;
195#else
196static struct perf_limits *limits = &powersave_limits;
197#endif
198
Dirk Brandewie93f08222013-02-06 09:02:13 -0800199static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700200 int deadband, int integral) {
Dirk Brandewie93f08222013-02-06 09:02:13 -0800201 pid->setpoint = setpoint;
202 pid->deadband = deadband;
203 pid->integral = int_tofp(integral);
Dirk Brandewied98d0992014-02-12 10:01:05 -0800204 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800205}
206
207static inline void pid_p_gain_set(struct _pid *pid, int percent)
208{
209 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
210}
211
212static inline void pid_i_gain_set(struct _pid *pid, int percent)
213{
214 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
215}
216
217static inline void pid_d_gain_set(struct _pid *pid, int percent)
218{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800219 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
220}
221
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700222static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800223{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700224 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800225 int32_t pterm, dterm, fp_error;
226 int32_t integral_limit;
227
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700228 fp_error = int_tofp(pid->setpoint) - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800229
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700230 if (abs(fp_error) <= int_tofp(pid->deadband))
Dirk Brandewie93f08222013-02-06 09:02:13 -0800231 return 0;
232
233 pterm = mul_fp(pid->p_gain, fp_error);
234
235 pid->integral += fp_error;
236
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800237 /*
238 * We limit the integral here so that it will never
239 * get higher than 30. This prevents it from becoming
240 * too large an input over long periods of time and allows
241 * it to get factored out sooner.
242 *
243 * The value of 30 was chosen through experimentation.
244 */
Dirk Brandewie93f08222013-02-06 09:02:13 -0800245 integral_limit = int_tofp(30);
246 if (pid->integral > integral_limit)
247 pid->integral = integral_limit;
248 if (pid->integral < -integral_limit)
249 pid->integral = -integral_limit;
250
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700251 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
252 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800253
254 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
Doug Smythies51d211e2014-06-17 13:36:10 -0700255 result = result + (1 << (FRAC_BITS-1));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800256 return (signed int)fp_toint(result);
257}
258
259static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
260{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700261 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
262 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
263 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800264
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700265 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800266}
267
Dirk Brandewie93f08222013-02-06 09:02:13 -0800268static inline void intel_pstate_reset_all_pid(void)
269{
270 unsigned int cpu;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700271
Dirk Brandewie93f08222013-02-06 09:02:13 -0800272 for_each_online_cpu(cpu) {
273 if (all_cpu_data[cpu])
274 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
275 }
276}
277
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700278static inline void update_turbo_state(void)
279{
280 u64 misc_en;
281 struct cpudata *cpu;
282
283 cpu = all_cpu_data[0];
284 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400285 limits->turbo_disabled =
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700286 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
287 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
288}
289
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800290static void intel_pstate_hwp_set(void)
291{
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700292 int min, hw_min, max, hw_max, cpu, range, adj_range;
293 u64 value, cap;
294
295 rdmsrl(MSR_HWP_CAPABILITIES, cap);
296 hw_min = HWP_LOWEST_PERF(cap);
297 hw_max = HWP_HIGHEST_PERF(cap);
298 range = hw_max - hw_min;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800299
300 get_online_cpus();
301
302 for_each_online_cpu(cpu) {
303 rdmsrl_on_cpu(cpu, MSR_HWP_REQUEST, &value);
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400304 adj_range = limits->min_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700305 min = hw_min + adj_range;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800306 value &= ~HWP_MIN_PERF(~0L);
307 value |= HWP_MIN_PERF(min);
308
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400309 adj_range = limits->max_perf_pct * range / 100;
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700310 max = hw_min + adj_range;
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400311 if (limits->no_turbo) {
Kristen Carlson Accardi74da56c2015-09-09 11:41:22 -0700312 hw_max = HWP_GUARANTEED_PERF(cap);
313 if (hw_max < max)
314 max = hw_max;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800315 }
316
317 value &= ~HWP_MAX_PERF(~0L);
318 value |= HWP_MAX_PERF(max);
319 wrmsrl_on_cpu(cpu, MSR_HWP_REQUEST, value);
320 }
321
322 put_online_cpus();
323}
324
Dirk Brandewie93f08222013-02-06 09:02:13 -0800325/************************** debugfs begin ************************/
326static int pid_param_set(void *data, u64 val)
327{
328 *(u32 *)data = val;
329 intel_pstate_reset_all_pid();
330 return 0;
331}
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700332
Dirk Brandewie93f08222013-02-06 09:02:13 -0800333static int pid_param_get(void *data, u64 *val)
334{
335 *val = *(u32 *)data;
336 return 0;
337}
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700338DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -0800339
340struct pid_param {
341 char *name;
342 void *value;
343};
344
345static struct pid_param pid_files[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700346 {"sample_rate_ms", &pid_params.sample_rate_ms},
347 {"d_gain_pct", &pid_params.d_gain_pct},
348 {"i_gain_pct", &pid_params.i_gain_pct},
349 {"deadband", &pid_params.deadband},
350 {"setpoint", &pid_params.setpoint},
351 {"p_gain_pct", &pid_params.p_gain_pct},
Dirk Brandewie93f08222013-02-06 09:02:13 -0800352 {NULL, NULL}
353};
354
Stratos Karafotis317dd502014-07-18 08:37:17 -0700355static void __init intel_pstate_debug_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800356{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700357 struct dentry *debugfs_parent;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800358 int i = 0;
359
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800360 if (hwp_active)
361 return;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800362 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
363 if (IS_ERR_OR_NULL(debugfs_parent))
364 return;
365 while (pid_files[i].name) {
366 debugfs_create_file(pid_files[i].name, 0660,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700367 debugfs_parent, pid_files[i].value,
368 &fops_pid_param);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800369 i++;
370 }
371}
372
373/************************** debugfs end ************************/
374
375/************************** sysfs begin ************************/
376#define show_one(file_name, object) \
377 static ssize_t show_##file_name \
378 (struct kobject *kobj, struct attribute *attr, char *buf) \
379 { \
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400380 return sprintf(buf, "%u\n", limits->object); \
Dirk Brandewie93f08222013-02-06 09:02:13 -0800381 }
382
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800383static ssize_t show_turbo_pct(struct kobject *kobj,
384 struct attribute *attr, char *buf)
385{
386 struct cpudata *cpu;
387 int total, no_turbo, turbo_pct;
388 uint32_t turbo_fp;
389
390 cpu = all_cpu_data[0];
391
392 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
393 no_turbo = cpu->pstate.max_pstate - cpu->pstate.min_pstate + 1;
394 turbo_fp = div_fp(int_tofp(no_turbo), int_tofp(total));
395 turbo_pct = 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100)));
396 return sprintf(buf, "%u\n", turbo_pct);
397}
398
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800399static ssize_t show_num_pstates(struct kobject *kobj,
400 struct attribute *attr, char *buf)
401{
402 struct cpudata *cpu;
403 int total;
404
405 cpu = all_cpu_data[0];
406 total = cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1;
407 return sprintf(buf, "%u\n", total);
408}
409
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700410static ssize_t show_no_turbo(struct kobject *kobj,
411 struct attribute *attr, char *buf)
412{
413 ssize_t ret;
414
415 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400416 if (limits->turbo_disabled)
417 ret = sprintf(buf, "%u\n", limits->turbo_disabled);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700418 else
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400419 ret = sprintf(buf, "%u\n", limits->no_turbo);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700420
421 return ret;
422}
423
Dirk Brandewie93f08222013-02-06 09:02:13 -0800424static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700425 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800426{
427 unsigned int input;
428 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700429
Dirk Brandewie93f08222013-02-06 09:02:13 -0800430 ret = sscanf(buf, "%u", &input);
431 if (ret != 1)
432 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700433
434 update_turbo_state();
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400435 if (limits->turbo_disabled) {
Doug Smythiesf16255e2015-05-31 07:46:47 -0700436 pr_warn("intel_pstate: Turbo disabled by BIOS or unavailable on processor\n");
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700437 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700438 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800439
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400440 limits->no_turbo = clamp_t(int, input, 0, 1);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700441
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800442 if (hwp_active)
443 intel_pstate_hwp_set();
444
Dirk Brandewie93f08222013-02-06 09:02:13 -0800445 return count;
446}
447
448static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700449 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800450{
451 unsigned int input;
452 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700453
Dirk Brandewie93f08222013-02-06 09:02:13 -0800454 ret = sscanf(buf, "%u", &input);
455 if (ret != 1)
456 return -EINVAL;
457
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400458 limits->max_sysfs_pct = clamp_t(int, input, 0 , 100);
459 limits->max_perf_pct = min(limits->max_policy_pct,
460 limits->max_sysfs_pct);
461 limits->max_perf_pct = max(limits->min_policy_pct,
462 limits->max_perf_pct);
463 limits->max_perf_pct = max(limits->min_perf_pct,
464 limits->max_perf_pct);
465 limits->max_perf = div_fp(int_tofp(limits->max_perf_pct),
466 int_tofp(100));
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700467
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800468 if (hwp_active)
469 intel_pstate_hwp_set();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800470 return count;
471}
472
473static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700474 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800475{
476 unsigned int input;
477 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700478
Dirk Brandewie93f08222013-02-06 09:02:13 -0800479 ret = sscanf(buf, "%u", &input);
480 if (ret != 1)
481 return -EINVAL;
Kristen Carlson Accardia0475992015-01-29 13:03:52 -0800482
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400483 limits->min_sysfs_pct = clamp_t(int, input, 0 , 100);
484 limits->min_perf_pct = max(limits->min_policy_pct,
485 limits->min_sysfs_pct);
486 limits->min_perf_pct = min(limits->max_policy_pct,
487 limits->min_perf_pct);
488 limits->min_perf_pct = min(limits->max_perf_pct,
489 limits->min_perf_pct);
490 limits->min_perf = div_fp(int_tofp(limits->min_perf_pct),
491 int_tofp(100));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800492
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800493 if (hwp_active)
494 intel_pstate_hwp_set();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800495 return count;
496}
497
Dirk Brandewie93f08222013-02-06 09:02:13 -0800498show_one(max_perf_pct, max_perf_pct);
499show_one(min_perf_pct, min_perf_pct);
500
501define_one_global_rw(no_turbo);
502define_one_global_rw(max_perf_pct);
503define_one_global_rw(min_perf_pct);
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800504define_one_global_ro(turbo_pct);
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800505define_one_global_ro(num_pstates);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800506
507static struct attribute *intel_pstate_attributes[] = {
508 &no_turbo.attr,
509 &max_perf_pct.attr,
510 &min_perf_pct.attr,
Kristen Carlson Accardid01b1f42015-01-28 15:03:27 -0800511 &turbo_pct.attr,
Kristen Carlson Accardi05224242015-01-28 15:03:28 -0800512 &num_pstates.attr,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800513 NULL
514};
515
516static struct attribute_group intel_pstate_attr_group = {
517 .attrs = intel_pstate_attributes,
518};
Dirk Brandewie93f08222013-02-06 09:02:13 -0800519
Stratos Karafotis317dd502014-07-18 08:37:17 -0700520static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800521{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700522 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800523 int rc;
524
525 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
526 &cpu_subsys.dev_root->kobj);
527 BUG_ON(!intel_pstate_kobject);
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700528 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800529 BUG_ON(rc);
530}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800531/************************** sysfs end ************************/
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800532
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -0700533static void intel_pstate_hwp_enable(struct cpudata *cpudata)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800534{
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -0700535 wrmsrl_on_cpu(cpudata->cpu, MSR_PM_ENABLE, 0x1);
Dirk Brandewie2f86dc42014-11-06 09:40:47 -0800536}
537
Philippe Longepe938d21a2015-11-09 17:40:46 -0800538static int atom_get_min_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700539{
540 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700541
Philippe Longepe938d21a2015-11-09 17:40:46 -0800542 rdmsrl(ATOM_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700543 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700544}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800545
Philippe Longepe938d21a2015-11-09 17:40:46 -0800546static int atom_get_max_pstate(void)
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700547{
548 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700549
Philippe Longepe938d21a2015-11-09 17:40:46 -0800550 rdmsrl(ATOM_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700551 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700552}
553
Philippe Longepe938d21a2015-11-09 17:40:46 -0800554static int atom_get_turbo_pstate(void)
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800555{
556 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700557
Philippe Longepe938d21a2015-11-09 17:40:46 -0800558 rdmsrl(ATOM_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700559 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800560}
561
Philippe Longepe938d21a2015-11-09 17:40:46 -0800562static void atom_set_pstate(struct cpudata *cpudata, int pstate)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800563{
564 u64 val;
565 int32_t vid_fp;
566 u32 vid;
567
Chen Yu144c8e12015-07-29 23:53:10 +0800568 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400569 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800570 val |= (u64)1 << 32;
571
572 vid_fp = cpudata->vid.min + mul_fp(
573 int_tofp(pstate - cpudata->pstate.min_pstate),
574 cpudata->vid.ratio);
575
576 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
Dirk Brandewied022a652014-10-13 08:37:44 -0700577 vid = ceiling_fp(vid_fp);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800578
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700579 if (pstate > cpudata->pstate.max_pstate)
580 vid = cpudata->vid.turbo;
581
Dirk Brandewie007bea02013-12-18 10:32:39 -0800582 val |= vid;
583
Joe Konno0dd23f92015-05-12 07:59:42 -0700584 wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800585}
586
Philippe Longepe1421df62015-11-09 17:40:47 -0800587static int silvermont_get_scaling(void)
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700588{
589 u64 value;
590 int i;
Philippe Longepe1421df62015-11-09 17:40:47 -0800591 /* Defined in Table 35-6 from SDM (Sept 2015) */
592 static int silvermont_freq_table[] = {
593 83300, 100000, 133300, 116700, 80000};
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700594
595 rdmsrl(MSR_FSB_FREQ, value);
Philippe Longepe1421df62015-11-09 17:40:47 -0800596 i = value & 0x7;
597 WARN_ON(i > 4);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700598
Philippe Longepe1421df62015-11-09 17:40:47 -0800599 return silvermont_freq_table[i];
600}
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700601
Philippe Longepe1421df62015-11-09 17:40:47 -0800602static int airmont_get_scaling(void)
603{
604 u64 value;
605 int i;
606 /* Defined in Table 35-10 from SDM (Sept 2015) */
607 static int airmont_freq_table[] = {
608 83300, 100000, 133300, 116700, 80000,
609 93300, 90000, 88900, 87500};
610
611 rdmsrl(MSR_FSB_FREQ, value);
612 i = value & 0xF;
613 WARN_ON(i > 8);
614
615 return airmont_freq_table[i];
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700616}
617
Philippe Longepe938d21a2015-11-09 17:40:46 -0800618static void atom_get_vid(struct cpudata *cpudata)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800619{
620 u64 value;
621
Philippe Longepe938d21a2015-11-09 17:40:46 -0800622 rdmsrl(ATOM_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700623 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
624 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800625 cpudata->vid.ratio = div_fp(
626 cpudata->vid.max - cpudata->vid.min,
627 int_tofp(cpudata->pstate.max_pstate -
628 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700629
Philippe Longepe938d21a2015-11-09 17:40:46 -0800630 rdmsrl(ATOM_TURBO_VIDS, value);
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700631 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800632}
633
Dirk Brandewie016c8152013-10-21 09:20:34 -0700634static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800635{
636 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700637
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000638 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800639 return (value >> 40) & 0xFF;
640}
641
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700642static int core_get_max_pstate_physical(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800643{
644 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700645
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000646 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800647 return (value >> 8) & 0xFF;
648}
649
Dirk Brandewie93f08222013-02-06 09:02:13 -0800650static int core_get_max_pstate(void)
651{
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -0700652 u64 tar;
653 u64 plat_info;
654 int max_pstate;
655 int err;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800656
Srinivas Pandruvada6a35fc22015-10-14 16:11:59 -0700657 rdmsrl(MSR_PLATFORM_INFO, plat_info);
658 max_pstate = (plat_info >> 8) & 0xFF;
659
660 err = rdmsrl_safe(MSR_TURBO_ACTIVATION_RATIO, &tar);
661 if (!err) {
662 /* Do some sanity checking for safety */
663 if (plat_info & 0x600000000) {
664 u64 tdp_ctrl;
665 u64 tdp_ratio;
666 int tdp_msr;
667
668 err = rdmsrl_safe(MSR_CONFIG_TDP_CONTROL, &tdp_ctrl);
669 if (err)
670 goto skip_tar;
671
672 tdp_msr = MSR_CONFIG_TDP_NOMINAL + tdp_ctrl;
673 err = rdmsrl_safe(tdp_msr, &tdp_ratio);
674 if (err)
675 goto skip_tar;
676
677 if (tdp_ratio - 1 == tar) {
678 max_pstate = tar;
679 pr_debug("max_pstate=TAC %x\n", max_pstate);
680 } else {
681 goto skip_tar;
682 }
683 }
684 }
685
686skip_tar:
687 return max_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800688}
689
Dirk Brandewie016c8152013-10-21 09:20:34 -0700690static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800691{
692 u64 value;
693 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700694
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000695 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700696 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -0700697 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800698 if (ret <= nont)
699 ret = nont;
700 return ret;
701}
702
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700703static inline int core_get_scaling(void)
704{
705 return 100000;
706}
707
Dirk Brandewie007bea02013-12-18 10:32:39 -0800708static void core_set_pstate(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700709{
710 u64 val;
711
Chen Yu144c8e12015-07-29 23:53:10 +0800712 val = (u64)pstate << 8;
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400713 if (limits->no_turbo && !limits->turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700714 val |= (u64)1 << 32;
715
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100716 wrmsrl(MSR_IA32_PERF_CTL, val);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700717}
718
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -0700719static int knl_get_turbo_pstate(void)
720{
721 u64 value;
722 int nont, ret;
723
724 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
725 nont = core_get_max_pstate();
726 ret = (((value) >> 8) & 0xFF);
727 if (ret <= nont)
728 ret = nont;
729 return ret;
730}
731
Dirk Brandewie016c8152013-10-21 09:20:34 -0700732static struct cpu_defaults core_params = {
733 .pid_policy = {
734 .sample_rate_ms = 10,
735 .deadband = 0,
736 .setpoint = 97,
737 .p_gain_pct = 20,
738 .d_gain_pct = 0,
739 .i_gain_pct = 0,
740 },
741 .funcs = {
742 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700743 .get_max_physical = core_get_max_pstate_physical,
Dirk Brandewie016c8152013-10-21 09:20:34 -0700744 .get_min = core_get_min_pstate,
745 .get_turbo = core_get_turbo_pstate,
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700746 .get_scaling = core_get_scaling,
Dirk Brandewie016c8152013-10-21 09:20:34 -0700747 .set = core_set_pstate,
Philippe Longepe157386b2015-12-04 17:40:30 +0100748 .get_target_pstate = get_target_pstate_use_performance,
Dirk Brandewie016c8152013-10-21 09:20:34 -0700749 },
750};
751
Philippe Longepe1421df62015-11-09 17:40:47 -0800752static struct cpu_defaults silvermont_params = {
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700753 .pid_policy = {
754 .sample_rate_ms = 10,
755 .deadband = 0,
Kristen Carlson Accardi6a82ba62015-04-10 11:06:43 -0700756 .setpoint = 60,
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700757 .p_gain_pct = 14,
758 .d_gain_pct = 0,
759 .i_gain_pct = 4,
760 },
761 .funcs = {
Philippe Longepe938d21a2015-11-09 17:40:46 -0800762 .get_max = atom_get_max_pstate,
763 .get_max_physical = atom_get_max_pstate,
764 .get_min = atom_get_min_pstate,
765 .get_turbo = atom_get_turbo_pstate,
766 .set = atom_set_pstate,
Philippe Longepe1421df62015-11-09 17:40:47 -0800767 .get_scaling = silvermont_get_scaling,
768 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +0100769 .get_target_pstate = get_target_pstate_use_cpu_load,
Philippe Longepe1421df62015-11-09 17:40:47 -0800770 },
771};
772
773static struct cpu_defaults airmont_params = {
774 .pid_policy = {
775 .sample_rate_ms = 10,
776 .deadband = 0,
777 .setpoint = 60,
778 .p_gain_pct = 14,
779 .d_gain_pct = 0,
780 .i_gain_pct = 4,
781 },
782 .funcs = {
783 .get_max = atom_get_max_pstate,
784 .get_max_physical = atom_get_max_pstate,
785 .get_min = atom_get_min_pstate,
786 .get_turbo = atom_get_turbo_pstate,
787 .set = atom_set_pstate,
788 .get_scaling = airmont_get_scaling,
Philippe Longepe938d21a2015-11-09 17:40:46 -0800789 .get_vid = atom_get_vid,
Philippe Longepee70eed22015-12-04 17:40:32 +0100790 .get_target_pstate = get_target_pstate_use_cpu_load,
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700791 },
792};
793
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -0700794static struct cpu_defaults knl_params = {
795 .pid_policy = {
796 .sample_rate_ms = 10,
797 .deadband = 0,
798 .setpoint = 97,
799 .p_gain_pct = 20,
800 .d_gain_pct = 0,
801 .i_gain_pct = 0,
802 },
803 .funcs = {
804 .get_max = core_get_max_pstate,
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700805 .get_max_physical = core_get_max_pstate_physical,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -0700806 .get_min = core_get_min_pstate,
807 .get_turbo = knl_get_turbo_pstate,
Lukasz Anaczkowski69cefc22015-07-21 10:41:13 +0200808 .get_scaling = core_get_scaling,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -0700809 .set = core_set_pstate,
Philippe Longepe157386b2015-12-04 17:40:30 +0100810 .get_target_pstate = get_target_pstate_use_performance,
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -0700811 },
812};
813
Dirk Brandewie93f08222013-02-06 09:02:13 -0800814static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
815{
816 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie7244cb62013-10-21 09:20:33 -0700817 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800818 int min_perf;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700819
Prarit Bhargava51443fb2015-10-15 07:34:15 -0400820 if (limits->no_turbo || limits->turbo_disabled)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800821 max_perf = cpu->pstate.max_pstate;
822
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800823 /*
824 * performance can be limited by user through sysfs, by cpufreq
825 * policy, or by cpu specific default values determined through
826 * experimentation.
827 */
Rafael J. Wysocki799281a2015-11-18 23:29:56 +0100828 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits->max_perf));
829 *max = clamp_t(int, max_perf_adj,
830 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800831
Rafael J. Wysocki799281a2015-11-18 23:29:56 +0100832 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits->min_perf));
833 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800834}
835
Doug Smythies6c1e4592015-06-01 21:12:34 -0700836static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate, bool force)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800837{
838 int max_perf, min_perf;
839
Doug Smythies6c1e4592015-06-01 21:12:34 -0700840 if (force) {
841 update_turbo_state();
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700842
Doug Smythies6c1e4592015-06-01 21:12:34 -0700843 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800844
Doug Smythies6c1e4592015-06-01 21:12:34 -0700845 pstate = clamp_t(int, pstate, min_perf, max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800846
Doug Smythies6c1e4592015-06-01 21:12:34 -0700847 if (pstate == cpu->pstate.current_pstate)
848 return;
849 }
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700850 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
Dirk Brandewie35363e92013-05-07 08:20:30 -0700851
Dirk Brandewie93f08222013-02-06 09:02:13 -0800852 cpu->pstate.current_pstate = pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800853
Dirk Brandewie007bea02013-12-18 10:32:39 -0800854 pstate_funcs.set(cpu, pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800855}
856
Dirk Brandewie93f08222013-02-06 09:02:13 -0800857static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
858{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700859 cpu->pstate.min_pstate = pstate_funcs.get_min();
860 cpu->pstate.max_pstate = pstate_funcs.get_max();
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700861 cpu->pstate.max_pstate_physical = pstate_funcs.get_max_physical();
Dirk Brandewie016c8152013-10-21 09:20:34 -0700862 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700863 cpu->pstate.scaling = pstate_funcs.get_scaling();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800864
Dirk Brandewie007bea02013-12-18 10:32:39 -0800865 if (pstate_funcs.get_vid)
866 pstate_funcs.get_vid(cpu);
Doug Smythies6c1e4592015-06-01 21:12:34 -0700867 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate, false);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800868}
869
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300870static inline void intel_pstate_calc_busy(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800871{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300872 struct sample *sample = &cpu->sample;
Doug Smythiesbf810222014-05-30 10:10:57 -0700873 int64_t core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800874
Doug Smythiesbf810222014-05-30 10:10:57 -0700875 core_pct = int_tofp(sample->aperf) * int_tofp(100);
Stratos Karafotis78e27082014-07-18 08:37:27 -0700876 core_pct = div64_u64(core_pct, int_tofp(sample->mperf));
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800877
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800878 sample->freq = fp_toint(
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700879 mul_fp(int_tofp(
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700880 cpu->pstate.max_pstate_physical *
881 cpu->pstate.scaling / 100),
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700882 core_pct));
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800883
Doug Smythiesbf810222014-05-30 10:10:57 -0700884 sample->core_pct_busy = (int32_t)core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800885}
886
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100887static inline void intel_pstate_sample(struct cpudata *cpu, u64 time)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800888{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800889 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -0700890 unsigned long flags;
Doug Smythies4055fad2015-04-11 21:10:26 -0700891 u64 tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800892
Stratos Karafotis4ab60c32014-07-18 08:37:24 -0700893 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800894 rdmsrl(MSR_IA32_APERF, aperf);
895 rdmsrl(MSR_IA32_MPERF, mperf);
Philippe Longepee70eed22015-12-04 17:40:32 +0100896 tsc = rdtsc();
897 if ((cpu->prev_mperf == mperf) || (cpu->prev_tsc == tsc)) {
Srinivas Pandruvada8e601a92015-10-15 12:34:21 -0700898 local_irq_restore(flags);
899 return;
900 }
Stratos Karafotis4ab60c32014-07-18 08:37:24 -0700901 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800902
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700903 cpu->last_sample_time = cpu->sample.time;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100904 cpu->sample.time = time;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800905 cpu->sample.aperf = aperf;
906 cpu->sample.mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700907 cpu->sample.tsc = tsc;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800908 cpu->sample.aperf -= cpu->prev_aperf;
909 cpu->sample.mperf -= cpu->prev_mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700910 cpu->sample.tsc -= cpu->prev_tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800911
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300912 intel_pstate_calc_busy(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800913
Dirk Brandewie93f08222013-02-06 09:02:13 -0800914 cpu->prev_aperf = aperf;
915 cpu->prev_mperf = mperf;
Doug Smythies4055fad2015-04-11 21:10:26 -0700916 cpu->prev_tsc = tsc;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800917}
918
Philippe Longepee70eed22015-12-04 17:40:32 +0100919static inline int32_t get_target_pstate_use_cpu_load(struct cpudata *cpu)
920{
921 struct sample *sample = &cpu->sample;
Philippe Longepe63d1d652015-12-04 17:40:35 +0100922 u64 cummulative_iowait, delta_iowait_us;
923 u64 delta_iowait_mperf;
924 u64 mperf, now;
Philippe Longepee70eed22015-12-04 17:40:32 +0100925 int32_t cpu_load;
926
Philippe Longepe63d1d652015-12-04 17:40:35 +0100927 cummulative_iowait = get_cpu_iowait_time_us(cpu->cpu, &now);
928
929 /*
930 * Convert iowait time into number of IO cycles spent at max_freq.
931 * IO is considered as busy only for the cpu_load algorithm. For
932 * performance this is not needed since we always try to reach the
933 * maximum P-State, so we are already boosting the IOs.
934 */
935 delta_iowait_us = cummulative_iowait - cpu->prev_cummulative_iowait;
936 delta_iowait_mperf = div64_u64(delta_iowait_us * cpu->pstate.scaling *
937 cpu->pstate.max_pstate, MSEC_PER_SEC);
938
939 mperf = cpu->sample.mperf + delta_iowait_mperf;
940 cpu->prev_cummulative_iowait = cummulative_iowait;
941
942
Philippe Longepee70eed22015-12-04 17:40:32 +0100943 /*
944 * The load can be estimated as the ratio of the mperf counter
945 * running at a constant frequency during active periods
946 * (C0) and the time stamp counter running at the same frequency
947 * also during C-states.
948 */
Philippe Longepe63d1d652015-12-04 17:40:35 +0100949 cpu_load = div64_u64(int_tofp(100) * mperf, sample->tsc);
Philippe Longepee70eed22015-12-04 17:40:32 +0100950 cpu->sample.busy_scaled = cpu_load;
951
952 return cpu->pstate.current_pstate - pid_calc(&cpu->pid, cpu_load);
953}
954
Philippe Longepe157386b2015-12-04 17:40:30 +0100955static inline int32_t get_target_pstate_use_performance(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800956{
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700957 int32_t core_busy, max_pstate, current_pstate, sample_ratio;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100958 u64 duration_ns;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800959
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800960 /*
961 * core_busy is the ratio of actual performance to max
962 * max_pstate is the max non turbo pstate available
963 * current_pstate was the pstate that was requested during
964 * the last sample period.
965 *
966 * We normalize core_busy, which was our actual percent
967 * performance to what we requested during the last sample
968 * period. The result will be a percentage of busy at a
969 * specified pstate.
970 */
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800971 core_busy = cpu->sample.core_pct_busy;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -0700972 max_pstate = int_tofp(cpu->pstate.max_pstate_physical);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800973 current_pstate = int_tofp(cpu->pstate.current_pstate);
Dirk Brandewiee66c1762014-02-25 10:35:37 -0800974 core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700975
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800976 /*
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100977 * Since our utilization update callback will not run unless we are
978 * in C0, check if the actual elapsed time is significantly greater (3x)
979 * than our sample interval. If it is, then we were idle for a long
980 * enough period of time to adjust our busyness.
Kristen Carlson Accardie0d4c8f2014-12-10 12:39:38 -0800981 */
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +0100982 duration_ns = cpu->sample.time - cpu->last_sample_time;
983 if ((s64)duration_ns > pid_params.sample_rate_ns * 3
984 && cpu->last_sample_time > 0) {
985 sample_ratio = div_fp(int_tofp(pid_params.sample_rate_ns),
986 int_tofp(duration_ns));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700987 core_busy = mul_fp(core_busy, sample_ratio);
988 }
989
Philippe Longepe157386b2015-12-04 17:40:30 +0100990 cpu->sample.busy_scaled = core_busy;
991 return cpu->pstate.current_pstate - pid_calc(&cpu->pid, core_busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800992}
993
994static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
995{
Philippe Longepe157386b2015-12-04 17:40:30 +0100996 int from, target_pstate;
Doug Smythies4055fad2015-04-11 21:10:26 -0700997 struct sample *sample;
998
999 from = cpu->pstate.current_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001000
Philippe Longepe157386b2015-12-04 17:40:30 +01001001 target_pstate = pstate_funcs.get_target_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001002
Philippe Longepe157386b2015-12-04 17:40:30 +01001003 intel_pstate_set_pstate(cpu, target_pstate, true);
Doug Smythies4055fad2015-04-11 21:10:26 -07001004
1005 sample = &cpu->sample;
1006 trace_pstate_sample(fp_toint(sample->core_pct_busy),
Philippe Longepe157386b2015-12-04 17:40:30 +01001007 fp_toint(sample->busy_scaled),
Doug Smythies4055fad2015-04-11 21:10:26 -07001008 from,
1009 cpu->pstate.current_pstate,
1010 sample->mperf,
1011 sample->aperf,
1012 sample->tsc,
1013 sample->freq);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001014}
1015
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001016static void intel_pstate_update_util(struct update_util_data *data, u64 time,
1017 unsigned long util, unsigned long max)
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001018{
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001019 struct cpudata *cpu = container_of(data, struct cpudata, update_util);
1020 u64 delta_ns = time - cpu->sample.time;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001021
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001022 if ((s64)delta_ns >= pid_params.sample_rate_ns) {
1023 intel_pstate_sample(cpu, time);
1024 if (!hwp_active)
1025 intel_pstate_adjust_busy_pstate(cpu);
1026 }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001027}
1028
1029#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -08001030 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
1031 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -08001032
1033static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -07001034 ICPU(0x2a, core_params),
1035 ICPU(0x2d, core_params),
Philippe Longepe1421df62015-11-09 17:40:47 -08001036 ICPU(0x37, silvermont_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -07001037 ICPU(0x3a, core_params),
1038 ICPU(0x3c, core_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -07001039 ICPU(0x3d, core_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -07001040 ICPU(0x3e, core_params),
1041 ICPU(0x3f, core_params),
1042 ICPU(0x45, core_params),
1043 ICPU(0x46, core_params),
Dirk Brandewie43f8a962014-11-06 09:50:45 -08001044 ICPU(0x47, core_params),
Philippe Longepe1421df62015-11-09 17:40:47 -08001045 ICPU(0x4c, airmont_params),
Kristen Carlson Accardi7ab02562015-01-28 13:53:28 -08001046 ICPU(0x4e, core_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -07001047 ICPU(0x4f, core_params),
Kristen Carlson Accardi1c939122015-08-05 12:47:14 -07001048 ICPU(0x5e, core_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -07001049 ICPU(0x56, core_params),
Dasaratharaman Chandramoulib34ef932015-04-10 10:22:18 -07001050 ICPU(0x57, knl_params),
Dirk Brandewie93f08222013-02-06 09:02:13 -08001051 {}
1052};
1053MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
1054
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001055static const struct x86_cpu_id intel_pstate_cpu_oob_ids[] = {
1056 ICPU(0x56, core_params),
1057 {}
1058};
1059
Dirk Brandewie93f08222013-02-06 09:02:13 -08001060static int intel_pstate_init_cpu(unsigned int cpunum)
1061{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001062 struct cpudata *cpu;
1063
Dirk Brandewiec0348712014-10-13 08:37:42 -07001064 if (!all_cpu_data[cpunum])
1065 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
1066 GFP_KERNEL);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001067 if (!all_cpu_data[cpunum])
1068 return -ENOMEM;
1069
1070 cpu = all_cpu_data[cpunum];
1071
Dirk Brandewie93f08222013-02-06 09:02:13 -08001072 cpu->cpu = cpunum;
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001073
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001074 if (hwp_active) {
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001075 intel_pstate_hwp_enable(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001076 pid_params.sample_rate_ms = 50;
1077 pid_params.sample_rate_ns = 50 * NSEC_PER_MSEC;
1078 }
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001079
Vincent Minet179e8472014-07-05 01:51:33 +02001080 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001081
Dirk Brandewie93f08222013-02-06 09:02:13 -08001082 intel_pstate_busy_pid_reset(cpu);
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001083 intel_pstate_sample(cpu, 0);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001084
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001085 cpu->update_util.func = intel_pstate_update_util;
1086 cpufreq_set_update_util_data(cpunum, &cpu->update_util);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001087
Doug Smythiesf16255e2015-05-31 07:46:47 -07001088 pr_debug("intel_pstate: controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001089
1090 return 0;
1091}
1092
1093static unsigned int intel_pstate_get(unsigned int cpu_num)
1094{
1095 struct sample *sample;
1096 struct cpudata *cpu;
1097
1098 cpu = all_cpu_data[cpu_num];
1099 if (!cpu)
1100 return 0;
Dirk Brandewied37e2b72014-02-12 10:01:04 -08001101 sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001102 return sample->freq;
1103}
1104
1105static int intel_pstate_set_policy(struct cpufreq_policy *policy)
1106{
Dirk Brandewied3929b82013-03-05 14:15:26 -08001107 if (!policy->cpuinfo.max_freq)
1108 return -ENODEV;
1109
Srinivas Pandruvada630ec282015-01-29 12:17:13 -08001110 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE &&
1111 policy->max >= policy->cpuinfo.max_freq) {
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001112 pr_debug("intel_pstate: set performance\n");
1113 limits = &performance_limits;
Alexandra Yates584ee3d2015-11-18 14:58:40 -08001114 if (hwp_active)
1115 intel_pstate_hwp_set();
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +00001116 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001117 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001118
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001119 pr_debug("intel_pstate: set powersave\n");
1120 limits = &powersave_limits;
1121 limits->min_policy_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
1122 limits->min_policy_pct = clamp_t(int, limits->min_policy_pct, 0 , 100);
Prarit Bhargava8478f532015-11-20 18:47:56 -05001123 limits->max_policy_pct = DIV_ROUND_UP(policy->max * 100,
1124 policy->cpuinfo.max_freq);
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001125 limits->max_policy_pct = clamp_t(int, limits->max_policy_pct, 0 , 100);
Chen Yu43717aa2015-09-09 18:27:31 +08001126
1127 /* Normalize user input to [min_policy_pct, max_policy_pct] */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001128 limits->min_perf_pct = max(limits->min_policy_pct,
1129 limits->min_sysfs_pct);
1130 limits->min_perf_pct = min(limits->max_policy_pct,
1131 limits->min_perf_pct);
1132 limits->max_perf_pct = min(limits->max_policy_pct,
1133 limits->max_sysfs_pct);
1134 limits->max_perf_pct = max(limits->min_policy_pct,
1135 limits->max_perf_pct);
Prarit Bhargava88b7b7c2015-12-08 13:44:59 -05001136 limits->max_perf = round_up(limits->max_perf, FRAC_BITS);
Chen Yu43717aa2015-09-09 18:27:31 +08001137
1138 /* Make sure min_perf_pct <= max_perf_pct */
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001139 limits->min_perf_pct = min(limits->max_perf_pct, limits->min_perf_pct);
Chen Yu43717aa2015-09-09 18:27:31 +08001140
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001141 limits->min_perf = div_fp(int_tofp(limits->min_perf_pct),
1142 int_tofp(100));
1143 limits->max_perf = div_fp(int_tofp(limits->max_perf_pct),
1144 int_tofp(100));
Dirk Brandewie93f08222013-02-06 09:02:13 -08001145
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001146 if (hwp_active)
1147 intel_pstate_hwp_set();
1148
Dirk Brandewie93f08222013-02-06 09:02:13 -08001149 return 0;
1150}
1151
1152static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
1153{
Viresh Kumarbe49e342013-10-02 14:13:19 +05301154 cpufreq_verify_within_cpu_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001155
Stratos Karafotis285cb992014-07-18 08:37:21 -07001156 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -07001157 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001158 return -EINVAL;
1159
1160 return 0;
1161}
1162
Dirk Brandewiebb180082014-03-19 08:45:54 -07001163static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001164{
Dirk Brandewiebb180082014-03-19 08:45:54 -07001165 int cpu_num = policy->cpu;
1166 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -08001167
Doug Smythiesf16255e2015-05-31 07:46:47 -07001168 pr_debug("intel_pstate: CPU %d exiting\n", cpu_num);
Dirk Brandewiebb180082014-03-19 08:45:54 -07001169
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001170 cpufreq_set_update_util_data(cpu_num, NULL);
1171 synchronize_rcu();
1172
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001173 if (hwp_active)
1174 return;
1175
Doug Smythies6c1e4592015-06-01 21:12:34 -07001176 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate, false);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001177}
1178
Paul Gortmaker27609842013-06-19 13:54:04 -04001179static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001180{
Dirk Brandewie93f08222013-02-06 09:02:13 -08001181 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -07001182 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001183
1184 rc = intel_pstate_init_cpu(policy->cpu);
1185 if (rc)
1186 return rc;
1187
1188 cpu = all_cpu_data[policy->cpu];
1189
Prarit Bhargava51443fb2015-10-15 07:34:15 -04001190 if (limits->min_perf_pct == 100 && limits->max_perf_pct == 100)
Dirk Brandewie93f08222013-02-06 09:02:13 -08001191 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
1192 else
1193 policy->policy = CPUFREQ_POLICY_POWERSAVE;
1194
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001195 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
1196 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001197
1198 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001199 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
1200 policy->cpuinfo.max_freq =
1201 cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001202 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
1203 cpumask_set_cpu(policy->cpu, policy->cpus);
1204
1205 return 0;
1206}
1207
1208static struct cpufreq_driver intel_pstate_driver = {
1209 .flags = CPUFREQ_CONST_LOOPS,
1210 .verify = intel_pstate_verify_policy,
1211 .setpolicy = intel_pstate_set_policy,
1212 .get = intel_pstate_get,
1213 .init = intel_pstate_cpu_init,
Dirk Brandewiebb180082014-03-19 08:45:54 -07001214 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -08001215 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -08001216};
1217
Dirk Brandewie6be26492013-02-15 22:55:10 +01001218static int __initdata no_load;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001219static int __initdata no_hwp;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08001220static int __initdata hwp_only;
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09001221static unsigned int force_load;
Dirk Brandewie6be26492013-02-15 22:55:10 +01001222
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001223static int intel_pstate_msrs_not_valid(void)
1224{
Dirk Brandewie016c8152013-10-21 09:20:34 -07001225 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -07001226 !pstate_funcs.get_min() ||
1227 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001228 return -ENODEV;
1229
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001230 return 0;
1231}
Dirk Brandewie016c8152013-10-21 09:20:34 -07001232
Dirk Brandewiee0a261a2013-10-30 08:38:32 -07001233static void copy_pid_params(struct pstate_adjust_policy *policy)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001234{
1235 pid_params.sample_rate_ms = policy->sample_rate_ms;
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001236 pid_params.sample_rate_ns = pid_params.sample_rate_ms * NSEC_PER_MSEC;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001237 pid_params.p_gain_pct = policy->p_gain_pct;
1238 pid_params.i_gain_pct = policy->i_gain_pct;
1239 pid_params.d_gain_pct = policy->d_gain_pct;
1240 pid_params.deadband = policy->deadband;
1241 pid_params.setpoint = policy->setpoint;
1242}
1243
Dirk Brandewiee0a261a2013-10-30 08:38:32 -07001244static void copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -07001245{
1246 pstate_funcs.get_max = funcs->get_max;
Srinivas Pandruvada3bcc6fa2015-10-14 16:12:00 -07001247 pstate_funcs.get_max_physical = funcs->get_max_physical;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001248 pstate_funcs.get_min = funcs->get_min;
1249 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -07001250 pstate_funcs.get_scaling = funcs->get_scaling;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001251 pstate_funcs.set = funcs->set;
Dirk Brandewie007bea02013-12-18 10:32:39 -08001252 pstate_funcs.get_vid = funcs->get_vid;
Philippe Longepe157386b2015-12-04 17:40:30 +01001253 pstate_funcs.get_target_pstate = funcs->get_target_pstate;
1254
Dirk Brandewie016c8152013-10-21 09:20:34 -07001255}
1256
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001257#if IS_ENABLED(CONFIG_ACPI)
Rafael J. Wysocki6ee11e412015-11-19 00:20:42 +01001258#include <acpi/processor.h>
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001259
1260static bool intel_pstate_no_acpi_pss(void)
1261{
1262 int i;
1263
1264 for_each_possible_cpu(i) {
1265 acpi_status status;
1266 union acpi_object *pss;
1267 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1268 struct acpi_processor *pr = per_cpu(processors, i);
1269
1270 if (!pr)
1271 continue;
1272
1273 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
1274 if (ACPI_FAILURE(status))
1275 continue;
1276
1277 pss = buffer.pointer;
1278 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
1279 kfree(pss);
1280 return false;
1281 }
1282
1283 kfree(pss);
1284 }
1285
1286 return true;
1287}
1288
ethan zhao966916e2014-12-01 11:32:08 +09001289static bool intel_pstate_has_acpi_ppc(void)
1290{
1291 int i;
1292
1293 for_each_possible_cpu(i) {
1294 struct acpi_processor *pr = per_cpu(processors, i);
1295
1296 if (!pr)
1297 continue;
1298 if (acpi_has_method(pr->handle, "_PPC"))
1299 return true;
1300 }
1301 return false;
1302}
1303
1304enum {
1305 PSS,
1306 PPC,
1307};
1308
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001309struct hw_vendor_info {
1310 u16 valid;
1311 char oem_id[ACPI_OEM_ID_SIZE];
1312 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
ethan zhao966916e2014-12-01 11:32:08 +09001313 int oem_pwr_table;
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001314};
1315
1316/* Hardware vendor-specific info that has its own power management modes */
1317static struct hw_vendor_info vendor_info[] = {
ethan zhao966916e2014-12-01 11:32:08 +09001318 {1, "HP ", "ProLiant", PSS},
1319 {1, "ORACLE", "X4-2 ", PPC},
1320 {1, "ORACLE", "X4-2L ", PPC},
1321 {1, "ORACLE", "X4-2B ", PPC},
1322 {1, "ORACLE", "X3-2 ", PPC},
1323 {1, "ORACLE", "X3-2L ", PPC},
1324 {1, "ORACLE", "X3-2B ", PPC},
1325 {1, "ORACLE", "X4470M2 ", PPC},
1326 {1, "ORACLE", "X4270M3 ", PPC},
1327 {1, "ORACLE", "X4270M2 ", PPC},
1328 {1, "ORACLE", "X4170M2 ", PPC},
Ethan Zhao5aecc3c2015-08-05 09:28:50 +09001329 {1, "ORACLE", "X4170 M3", PPC},
1330 {1, "ORACLE", "X4275 M3", PPC},
1331 {1, "ORACLE", "X6-2 ", PPC},
1332 {1, "ORACLE", "Sudbury ", PPC},
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001333 {0, "", ""},
1334};
1335
1336static bool intel_pstate_platform_pwr_mgmt_exists(void)
1337{
1338 struct acpi_table_header hdr;
1339 struct hw_vendor_info *v_info;
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001340 const struct x86_cpu_id *id;
1341 u64 misc_pwr;
1342
1343 id = x86_match_cpu(intel_pstate_cpu_oob_ids);
1344 if (id) {
1345 rdmsrl(MSR_MISC_PWR_MGMT, misc_pwr);
1346 if ( misc_pwr & (1 << 8))
1347 return true;
1348 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001349
Stratos Karafotisc4108332014-07-18 08:37:23 -07001350 if (acpi_disabled ||
1351 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001352 return false;
1353
1354 for (v_info = vendor_info; v_info->valid; v_info++) {
Stratos Karafotisc4108332014-07-18 08:37:23 -07001355 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
ethan zhao966916e2014-12-01 11:32:08 +09001356 !strncmp(hdr.oem_table_id, v_info->oem_table_id,
1357 ACPI_OEM_TABLE_ID_SIZE))
1358 switch (v_info->oem_pwr_table) {
1359 case PSS:
1360 return intel_pstate_no_acpi_pss();
1361 case PPC:
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09001362 return intel_pstate_has_acpi_ppc() &&
1363 (!force_load);
ethan zhao966916e2014-12-01 11:32:08 +09001364 }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001365 }
1366
1367 return false;
1368}
1369#else /* CONFIG_ACPI not enabled */
1370static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
ethan zhao966916e2014-12-01 11:32:08 +09001371static inline bool intel_pstate_has_acpi_ppc(void) { return false; }
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001372#endif /* CONFIG_ACPI */
1373
Dirk Brandewie93f08222013-02-06 09:02:13 -08001374static int __init intel_pstate_init(void)
1375{
Dirk Brandewie907cc902013-03-05 14:15:27 -08001376 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001377 const struct x86_cpu_id *id;
Borislav Petkov64df1fd2015-04-03 15:19:53 +02001378 struct cpu_defaults *cpu_def;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001379
Dirk Brandewie6be26492013-02-15 22:55:10 +01001380 if (no_load)
1381 return -ENODEV;
1382
Dirk Brandewie93f08222013-02-06 09:02:13 -08001383 id = x86_match_cpu(intel_pstate_cpu_ids);
1384 if (!id)
1385 return -ENODEV;
1386
Adrian Huangfbbcdc02013-10-31 23:24:05 +08001387 /*
1388 * The Intel pstate driver will be ignored if the platform
1389 * firmware has its own power management modes.
1390 */
1391 if (intel_pstate_platform_pwr_mgmt_exists())
1392 return -ENODEV;
1393
Borislav Petkov64df1fd2015-04-03 15:19:53 +02001394 cpu_def = (struct cpu_defaults *)id->driver_data;
Dirk Brandewie016c8152013-10-21 09:20:34 -07001395
Borislav Petkov64df1fd2015-04-03 15:19:53 +02001396 copy_pid_params(&cpu_def->pid_policy);
1397 copy_cpu_funcs(&cpu_def->funcs);
Dirk Brandewie016c8152013-10-21 09:20:34 -07001398
Dirk Brandewieb563b4e2013-03-22 01:29:28 +01001399 if (intel_pstate_msrs_not_valid())
1400 return -ENODEV;
1401
Dirk Brandewie93f08222013-02-06 09:02:13 -08001402 pr_info("Intel P-state driver initializing.\n");
1403
Wei Yongjunb57ffac2013-05-13 08:03:43 +00001404 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08001405 if (!all_cpu_data)
1406 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001407
Prarit Bhargava539342f2015-10-22 09:43:31 -04001408 if (static_cpu_has_safe(X86_FEATURE_HWP) && !no_hwp) {
1409 pr_info("intel_pstate: HWP enabled\n");
Kristen Carlson Accardiba88d432015-07-14 09:46:23 -07001410 hwp_active++;
Prarit Bhargava539342f2015-10-22 09:43:31 -04001411 }
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001412
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08001413 if (!hwp_active && hwp_only)
1414 goto out;
1415
Dirk Brandewie93f08222013-02-06 09:02:13 -08001416 rc = cpufreq_register_driver(&intel_pstate_driver);
1417 if (rc)
1418 goto out;
1419
1420 intel_pstate_debug_expose_params();
1421 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001422
Dirk Brandewie93f08222013-02-06 09:02:13 -08001423 return rc;
1424out:
Dirk Brandewie907cc902013-03-05 14:15:27 -08001425 get_online_cpus();
1426 for_each_online_cpu(cpu) {
1427 if (all_cpu_data[cpu]) {
Rafael J. Wysockia4675fb2016-02-05 01:45:30 +01001428 cpufreq_set_update_util_data(cpu, NULL);
1429 synchronize_rcu();
Dirk Brandewie907cc902013-03-05 14:15:27 -08001430 kfree(all_cpu_data[cpu]);
1431 }
1432 }
1433
1434 put_online_cpus();
1435 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001436 return -ENODEV;
1437}
1438device_initcall(intel_pstate_init);
1439
Dirk Brandewie6be26492013-02-15 22:55:10 +01001440static int __init intel_pstate_setup(char *str)
1441{
1442 if (!str)
1443 return -EINVAL;
1444
1445 if (!strcmp(str, "disable"))
1446 no_load = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04001447 if (!strcmp(str, "no_hwp")) {
1448 pr_info("intel_pstate: HWP disabled\n");
Dirk Brandewie2f86dc42014-11-06 09:40:47 -08001449 no_hwp = 1;
Prarit Bhargava539342f2015-10-22 09:43:31 -04001450 }
Ethan Zhaoaa4ea342014-12-09 10:43:19 +09001451 if (!strcmp(str, "force"))
1452 force_load = 1;
Kristen Carlson Accardid64c3b02015-02-06 13:41:55 -08001453 if (!strcmp(str, "hwp_only"))
1454 hwp_only = 1;
Dirk Brandewie6be26492013-02-15 22:55:10 +01001455 return 0;
1456}
1457early_param("intel_pstate", intel_pstate_setup);
1458
Dirk Brandewie93f08222013-02-06 09:02:13 -08001459MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
1460MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
1461MODULE_LICENSE("GPL");