blob: 98593e0344c521c1d19fb57c2c64988386a90edf [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>
Dirk Brandewie93f08222013-02-06 09:02:13 -080029#include <trace/events/power.h>
30
31#include <asm/div64.h>
32#include <asm/msr.h>
33#include <asm/cpu_device_id.h>
34
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -080035#define BYT_RATIOS 0x66a
36#define BYT_VIDS 0x66b
37#define BYT_TURBO_RATIOS 0x66c
Dirk Brandewie21855ff2014-05-08 12:57:23 -070038#define BYT_TURBO_VIDS 0x66d
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -080039
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070040#define FRAC_BITS 8
Dirk Brandewie93f08222013-02-06 09:02:13 -080041#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
42#define fp_toint(X) ((X) >> FRAC_BITS)
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -070043
Dirk Brandewie93f08222013-02-06 09:02:13 -080044
45static inline int32_t mul_fp(int32_t x, int32_t y)
46{
47 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
48}
49
50static inline int32_t div_fp(int32_t x, int32_t y)
51{
Stratos Karafotisfa30dff2014-07-18 08:37:18 -070052 return div_s64((int64_t)x << FRAC_BITS, y);
Dirk Brandewie93f08222013-02-06 09:02:13 -080053}
54
55struct sample {
Brennan Shacklettd253d2a2013-10-21 09:20:32 -070056 int32_t core_pct_busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -080057 u64 aperf;
58 u64 mperf;
59 int freq;
Dirk Brandewiec4ee8412014-05-29 09:32:24 -070060 ktime_t time;
Dirk Brandewie93f08222013-02-06 09:02:13 -080061};
62
63struct pstate_data {
64 int current_pstate;
65 int min_pstate;
66 int max_pstate;
Dirk Brandewieb27580b2014-10-13 08:37:43 -070067 int scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -080068 int turbo_pstate;
69};
70
Dirk Brandewie007bea02013-12-18 10:32:39 -080071struct vid_data {
Dirk Brandewie21855ff2014-05-08 12:57:23 -070072 int min;
73 int max;
74 int turbo;
Dirk Brandewie007bea02013-12-18 10:32:39 -080075 int32_t ratio;
76};
77
Dirk Brandewie93f08222013-02-06 09:02:13 -080078struct _pid {
79 int setpoint;
80 int32_t integral;
81 int32_t p_gain;
82 int32_t i_gain;
83 int32_t d_gain;
84 int deadband;
Brennan Shacklettd253d2a2013-10-21 09:20:32 -070085 int32_t last_err;
Dirk Brandewie93f08222013-02-06 09:02:13 -080086};
87
88struct cpudata {
89 int cpu;
90
Dirk Brandewie93f08222013-02-06 09:02:13 -080091 struct timer_list timer;
92
Dirk Brandewie93f08222013-02-06 09:02:13 -080093 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -080094 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -080095 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -080096
Dirk Brandewiec4ee8412014-05-29 09:32:24 -070097 ktime_t last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -080098 u64 prev_aperf;
99 u64 prev_mperf;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800100 struct sample sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800101};
102
103static struct cpudata **all_cpu_data;
104struct pstate_adjust_policy {
105 int sample_rate_ms;
106 int deadband;
107 int setpoint;
108 int p_gain_pct;
109 int d_gain_pct;
110 int i_gain_pct;
111};
112
Dirk Brandewie016c8152013-10-21 09:20:34 -0700113struct pstate_funcs {
114 int (*get_max)(void);
115 int (*get_min)(void);
116 int (*get_turbo)(void);
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700117 int (*get_scaling)(void);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800118 void (*set)(struct cpudata*, int pstate);
119 void (*get_vid)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800120};
121
Dirk Brandewie016c8152013-10-21 09:20:34 -0700122struct cpu_defaults {
123 struct pstate_adjust_policy pid_policy;
124 struct pstate_funcs funcs;
125};
126
127static struct pstate_adjust_policy pid_params;
128static struct pstate_funcs pstate_funcs;
129
Dirk Brandewie93f08222013-02-06 09:02:13 -0800130struct perf_limits {
131 int no_turbo;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700132 int turbo_disabled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800133 int max_perf_pct;
134 int min_perf_pct;
135 int32_t max_perf;
136 int32_t min_perf;
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700137 int max_policy_pct;
138 int max_sysfs_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800139};
140
141static struct perf_limits limits = {
142 .no_turbo = 0,
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700143 .turbo_disabled = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800144 .max_perf_pct = 100,
145 .max_perf = int_tofp(1),
146 .min_perf_pct = 0,
147 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700148 .max_policy_pct = 100,
149 .max_sysfs_pct = 100,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800150};
151
152static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700153 int deadband, int integral) {
Dirk Brandewie93f08222013-02-06 09:02:13 -0800154 pid->setpoint = setpoint;
155 pid->deadband = deadband;
156 pid->integral = int_tofp(integral);
Dirk Brandewied98d0992014-02-12 10:01:05 -0800157 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800158}
159
160static inline void pid_p_gain_set(struct _pid *pid, int percent)
161{
162 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
163}
164
165static inline void pid_i_gain_set(struct _pid *pid, int percent)
166{
167 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
168}
169
170static inline void pid_d_gain_set(struct _pid *pid, int percent)
171{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800172 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
173}
174
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700175static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800176{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700177 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800178 int32_t pterm, dterm, fp_error;
179 int32_t integral_limit;
180
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700181 fp_error = int_tofp(pid->setpoint) - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800182
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700183 if (abs(fp_error) <= int_tofp(pid->deadband))
Dirk Brandewie93f08222013-02-06 09:02:13 -0800184 return 0;
185
186 pterm = mul_fp(pid->p_gain, fp_error);
187
188 pid->integral += fp_error;
189
190 /* limit the integral term */
191 integral_limit = int_tofp(30);
192 if (pid->integral > integral_limit)
193 pid->integral = integral_limit;
194 if (pid->integral < -integral_limit)
195 pid->integral = -integral_limit;
196
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700197 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
198 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800199
200 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
Doug Smythies51d211e2014-06-17 13:36:10 -0700201 result = result + (1 << (FRAC_BITS-1));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800202 return (signed int)fp_toint(result);
203}
204
205static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
206{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700207 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
208 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
209 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800210
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700211 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800212}
213
Dirk Brandewie93f08222013-02-06 09:02:13 -0800214static inline void intel_pstate_reset_all_pid(void)
215{
216 unsigned int cpu;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700217
Dirk Brandewie93f08222013-02-06 09:02:13 -0800218 for_each_online_cpu(cpu) {
219 if (all_cpu_data[cpu])
220 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
221 }
222}
223
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700224static inline void update_turbo_state(void)
225{
226 u64 misc_en;
227 struct cpudata *cpu;
228
229 cpu = all_cpu_data[0];
230 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
231 limits.turbo_disabled =
232 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
233 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
234}
235
Dirk Brandewie93f08222013-02-06 09:02:13 -0800236/************************** debugfs begin ************************/
237static int pid_param_set(void *data, u64 val)
238{
239 *(u32 *)data = val;
240 intel_pstate_reset_all_pid();
241 return 0;
242}
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700243
Dirk Brandewie93f08222013-02-06 09:02:13 -0800244static int pid_param_get(void *data, u64 *val)
245{
246 *val = *(u32 *)data;
247 return 0;
248}
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700249DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -0800250
251struct pid_param {
252 char *name;
253 void *value;
254};
255
256static struct pid_param pid_files[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700257 {"sample_rate_ms", &pid_params.sample_rate_ms},
258 {"d_gain_pct", &pid_params.d_gain_pct},
259 {"i_gain_pct", &pid_params.i_gain_pct},
260 {"deadband", &pid_params.deadband},
261 {"setpoint", &pid_params.setpoint},
262 {"p_gain_pct", &pid_params.p_gain_pct},
Dirk Brandewie93f08222013-02-06 09:02:13 -0800263 {NULL, NULL}
264};
265
Stratos Karafotis317dd502014-07-18 08:37:17 -0700266static void __init intel_pstate_debug_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800267{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700268 struct dentry *debugfs_parent;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800269 int i = 0;
270
271 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
272 if (IS_ERR_OR_NULL(debugfs_parent))
273 return;
274 while (pid_files[i].name) {
275 debugfs_create_file(pid_files[i].name, 0660,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700276 debugfs_parent, pid_files[i].value,
277 &fops_pid_param);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800278 i++;
279 }
280}
281
282/************************** debugfs end ************************/
283
284/************************** sysfs begin ************************/
285#define show_one(file_name, object) \
286 static ssize_t show_##file_name \
287 (struct kobject *kobj, struct attribute *attr, char *buf) \
288 { \
289 return sprintf(buf, "%u\n", limits.object); \
290 }
291
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700292static ssize_t show_no_turbo(struct kobject *kobj,
293 struct attribute *attr, char *buf)
294{
295 ssize_t ret;
296
297 update_turbo_state();
298 if (limits.turbo_disabled)
299 ret = sprintf(buf, "%u\n", limits.turbo_disabled);
300 else
301 ret = sprintf(buf, "%u\n", limits.no_turbo);
302
303 return ret;
304}
305
Dirk Brandewie93f08222013-02-06 09:02:13 -0800306static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700307 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800308{
309 unsigned int input;
310 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700311
Dirk Brandewie93f08222013-02-06 09:02:13 -0800312 ret = sscanf(buf, "%u", &input);
313 if (ret != 1)
314 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700315
316 update_turbo_state();
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700317 if (limits.turbo_disabled) {
318 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700319 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700320 }
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700321 limits.no_turbo = clamp_t(int, input, 0, 1);
322
Dirk Brandewie93f08222013-02-06 09:02:13 -0800323 return count;
324}
325
326static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700327 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800328{
329 unsigned int input;
330 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700331
Dirk Brandewie93f08222013-02-06 09:02:13 -0800332 ret = sscanf(buf, "%u", &input);
333 if (ret != 1)
334 return -EINVAL;
335
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700336 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
337 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800338 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700339
Dirk Brandewie93f08222013-02-06 09:02:13 -0800340 return count;
341}
342
343static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700344 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800345{
346 unsigned int input;
347 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700348
Dirk Brandewie93f08222013-02-06 09:02:13 -0800349 ret = sscanf(buf, "%u", &input);
350 if (ret != 1)
351 return -EINVAL;
352 limits.min_perf_pct = clamp_t(int, input, 0 , 100);
353 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
354
355 return count;
356}
357
Dirk Brandewie93f08222013-02-06 09:02:13 -0800358show_one(max_perf_pct, max_perf_pct);
359show_one(min_perf_pct, min_perf_pct);
360
361define_one_global_rw(no_turbo);
362define_one_global_rw(max_perf_pct);
363define_one_global_rw(min_perf_pct);
364
365static struct attribute *intel_pstate_attributes[] = {
366 &no_turbo.attr,
367 &max_perf_pct.attr,
368 &min_perf_pct.attr,
369 NULL
370};
371
372static struct attribute_group intel_pstate_attr_group = {
373 .attrs = intel_pstate_attributes,
374};
Dirk Brandewie93f08222013-02-06 09:02:13 -0800375
Stratos Karafotis317dd502014-07-18 08:37:17 -0700376static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800377{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700378 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800379 int rc;
380
381 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
382 &cpu_subsys.dev_root->kobj);
383 BUG_ON(!intel_pstate_kobject);
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700384 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800385 BUG_ON(rc);
386}
387
388/************************** sysfs end ************************/
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700389static int byt_get_min_pstate(void)
390{
391 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700392
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700393 rdmsrl(BYT_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700394 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700395}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800396
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700397static int byt_get_max_pstate(void)
398{
399 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700400
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700401 rdmsrl(BYT_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700402 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700403}
404
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800405static int byt_get_turbo_pstate(void)
406{
407 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700408
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800409 rdmsrl(BYT_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700410 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800411}
412
Dirk Brandewie007bea02013-12-18 10:32:39 -0800413static void byt_set_pstate(struct cpudata *cpudata, int pstate)
414{
415 u64 val;
416 int32_t vid_fp;
417 u32 vid;
418
419 val = pstate << 8;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700420 if (limits.no_turbo && !limits.turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800421 val |= (u64)1 << 32;
422
423 vid_fp = cpudata->vid.min + mul_fp(
424 int_tofp(pstate - cpudata->pstate.min_pstate),
425 cpudata->vid.ratio);
426
427 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
428 vid = fp_toint(vid_fp);
429
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700430 if (pstate > cpudata->pstate.max_pstate)
431 vid = cpudata->vid.turbo;
432
Dirk Brandewie007bea02013-12-18 10:32:39 -0800433 val |= vid;
434
435 wrmsrl(MSR_IA32_PERF_CTL, val);
436}
437
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700438#define BYT_BCLK_FREQS 5
439static int byt_freq_table[BYT_BCLK_FREQS] = { 833, 1000, 1333, 1167, 800};
440
441static int byt_get_scaling(void)
442{
443 u64 value;
444 int i;
445
446 rdmsrl(MSR_FSB_FREQ, value);
447 i = value & 0x3;
448
449 BUG_ON(i > BYT_BCLK_FREQS);
450
451 return byt_freq_table[i] * 100;
452}
453
Dirk Brandewie007bea02013-12-18 10:32:39 -0800454static void byt_get_vid(struct cpudata *cpudata)
455{
456 u64 value;
457
458 rdmsrl(BYT_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700459 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
460 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800461 cpudata->vid.ratio = div_fp(
462 cpudata->vid.max - cpudata->vid.min,
463 int_tofp(cpudata->pstate.max_pstate -
464 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700465
466 rdmsrl(BYT_TURBO_VIDS, value);
467 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800468}
469
Dirk Brandewie016c8152013-10-21 09:20:34 -0700470static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800471{
472 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700473
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000474 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800475 return (value >> 40) & 0xFF;
476}
477
Dirk Brandewie016c8152013-10-21 09:20:34 -0700478static int core_get_max_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800479{
480 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700481
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000482 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800483 return (value >> 8) & 0xFF;
484}
485
Dirk Brandewie016c8152013-10-21 09:20:34 -0700486static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800487{
488 u64 value;
489 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700490
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000491 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700492 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -0700493 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800494 if (ret <= nont)
495 ret = nont;
496 return ret;
497}
498
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700499static inline int core_get_scaling(void)
500{
501 return 100000;
502}
503
Dirk Brandewie007bea02013-12-18 10:32:39 -0800504static void core_set_pstate(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700505{
506 u64 val;
507
508 val = pstate << 8;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700509 if (limits.no_turbo && !limits.turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700510 val |= (u64)1 << 32;
511
Dirk Brandewiebb180082014-03-19 08:45:54 -0700512 wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700513}
514
515static struct cpu_defaults core_params = {
516 .pid_policy = {
517 .sample_rate_ms = 10,
518 .deadband = 0,
519 .setpoint = 97,
520 .p_gain_pct = 20,
521 .d_gain_pct = 0,
522 .i_gain_pct = 0,
523 },
524 .funcs = {
525 .get_max = core_get_max_pstate,
526 .get_min = core_get_min_pstate,
527 .get_turbo = core_get_turbo_pstate,
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700528 .get_scaling = core_get_scaling,
Dirk Brandewie016c8152013-10-21 09:20:34 -0700529 .set = core_set_pstate,
530 },
531};
532
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700533static struct cpu_defaults byt_params = {
534 .pid_policy = {
535 .sample_rate_ms = 10,
536 .deadband = 0,
537 .setpoint = 97,
538 .p_gain_pct = 14,
539 .d_gain_pct = 0,
540 .i_gain_pct = 4,
541 },
542 .funcs = {
543 .get_max = byt_get_max_pstate,
544 .get_min = byt_get_min_pstate,
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800545 .get_turbo = byt_get_turbo_pstate,
Dirk Brandewie007bea02013-12-18 10:32:39 -0800546 .set = byt_set_pstate,
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700547 .get_scaling = byt_get_scaling,
Dirk Brandewie007bea02013-12-18 10:32:39 -0800548 .get_vid = byt_get_vid,
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700549 },
550};
551
Dirk Brandewie93f08222013-02-06 09:02:13 -0800552static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
553{
554 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie7244cb62013-10-21 09:20:33 -0700555 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800556 int min_perf;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700557
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700558 if (limits.no_turbo || limits.turbo_disabled)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800559 max_perf = cpu->pstate.max_pstate;
560
Dirk Brandewie7244cb62013-10-21 09:20:33 -0700561 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
562 *max = clamp_t(int, max_perf_adj,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800563 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
564
565 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700566 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800567}
568
569static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
570{
571 int max_perf, min_perf;
572
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700573 update_turbo_state();
574
Dirk Brandewie93f08222013-02-06 09:02:13 -0800575 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
576
577 pstate = clamp_t(int, pstate, min_perf, max_perf);
578
579 if (pstate == cpu->pstate.current_pstate)
580 return;
581
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700582 trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
Dirk Brandewie35363e92013-05-07 08:20:30 -0700583
Dirk Brandewie93f08222013-02-06 09:02:13 -0800584 cpu->pstate.current_pstate = pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800585
Dirk Brandewie007bea02013-12-18 10:32:39 -0800586 pstate_funcs.set(cpu, pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800587}
588
Dirk Brandewie93f08222013-02-06 09:02:13 -0800589static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
590{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700591 cpu->pstate.min_pstate = pstate_funcs.get_min();
592 cpu->pstate.max_pstate = pstate_funcs.get_max();
593 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700594 cpu->pstate.scaling = pstate_funcs.get_scaling();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800595
Dirk Brandewie007bea02013-12-18 10:32:39 -0800596 if (pstate_funcs.get_vid)
597 pstate_funcs.get_vid(cpu);
Dirk Brandewied40a63c2014-05-08 12:57:24 -0700598 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800599}
600
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300601static inline void intel_pstate_calc_busy(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800602{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300603 struct sample *sample = &cpu->sample;
Doug Smythiesbf810222014-05-30 10:10:57 -0700604 int64_t core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800605
Doug Smythiesbf810222014-05-30 10:10:57 -0700606 core_pct = int_tofp(sample->aperf) * int_tofp(100);
Stratos Karafotis78e27082014-07-18 08:37:27 -0700607 core_pct = div64_u64(core_pct, int_tofp(sample->mperf));
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800608
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800609 sample->freq = fp_toint(
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700610 mul_fp(int_tofp(
611 cpu->pstate.max_pstate * cpu->pstate.scaling / 100),
612 core_pct));
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800613
Doug Smythiesbf810222014-05-30 10:10:57 -0700614 sample->core_pct_busy = (int32_t)core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800615}
616
617static inline void intel_pstate_sample(struct cpudata *cpu)
618{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800619 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -0700620 unsigned long flags;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800621
Stratos Karafotis4ab60c32014-07-18 08:37:24 -0700622 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800623 rdmsrl(MSR_IA32_APERF, aperf);
624 rdmsrl(MSR_IA32_MPERF, mperf);
Stratos Karafotis4ab60c32014-07-18 08:37:24 -0700625 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800626
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700627 cpu->last_sample_time = cpu->sample.time;
628 cpu->sample.time = ktime_get();
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800629 cpu->sample.aperf = aperf;
630 cpu->sample.mperf = mperf;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800631 cpu->sample.aperf -= cpu->prev_aperf;
632 cpu->sample.mperf -= cpu->prev_mperf;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800633
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300634 intel_pstate_calc_busy(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800635
Dirk Brandewie93f08222013-02-06 09:02:13 -0800636 cpu->prev_aperf = aperf;
637 cpu->prev_mperf = mperf;
638}
639
640static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
641{
Stratos Karafotisabf013b2014-07-18 08:37:22 -0700642 int delay;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800643
Stratos Karafotisabf013b2014-07-18 08:37:22 -0700644 delay = msecs_to_jiffies(pid_params.sample_rate_ms);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800645 mod_timer_pinned(&cpu->timer, jiffies + delay);
646}
647
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700648static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800649{
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700650 int32_t core_busy, max_pstate, current_pstate, sample_ratio;
651 u32 duration_us;
652 u32 sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800653
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800654 core_busy = cpu->sample.core_pct_busy;
Dirk Brandewie2134ed42013-07-18 08:48:42 -0700655 max_pstate = int_tofp(cpu->pstate.max_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800656 current_pstate = int_tofp(cpu->pstate.current_pstate);
Dirk Brandewiee66c1762014-02-25 10:35:37 -0800657 core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700658
Stratos Karafotis285cb992014-07-18 08:37:21 -0700659 sample_time = pid_params.sample_rate_ms * USEC_PER_MSEC;
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700660 duration_us = (u32) ktime_us_delta(cpu->sample.time,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700661 cpu->last_sample_time);
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700662 if (duration_us > sample_time * 3) {
663 sample_ratio = div_fp(int_tofp(sample_time),
Stratos Karafotisc4108332014-07-18 08:37:23 -0700664 int_tofp(duration_us));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700665 core_busy = mul_fp(core_busy, sample_ratio);
666 }
667
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -0700668 return core_busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800669}
670
671static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
672{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700673 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800674 struct _pid *pid;
Stratos Karafotis4b707c82014-07-18 08:37:26 -0700675 signed int ctl;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800676
677 pid = &cpu->pid;
678 busy_scaled = intel_pstate_get_scaled_busy(cpu);
679
680 ctl = pid_calc(pid, busy_scaled);
681
Stratos Karafotis4b707c82014-07-18 08:37:26 -0700682 /* Negative values of ctl increase the pstate and vice versa */
683 intel_pstate_set_pstate(cpu, cpu->pstate.current_pstate - ctl);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800684}
685
Dirk Brandewie93f08222013-02-06 09:02:13 -0800686static void intel_pstate_timer_func(unsigned long __data)
687{
688 struct cpudata *cpu = (struct cpudata *) __data;
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800689 struct sample *sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800690
691 intel_pstate_sample(cpu);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800692
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800693 sample = &cpu->sample;
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800694
Dirk Brandewieca182ae2013-05-07 08:20:27 -0700695 intel_pstate_adjust_busy_pstate(cpu);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800696
697 trace_pstate_sample(fp_toint(sample->core_pct_busy),
698 fp_toint(intel_pstate_get_scaled_busy(cpu)),
699 cpu->pstate.current_pstate,
700 sample->mperf,
701 sample->aperf,
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800702 sample->freq);
703
Dirk Brandewie93f08222013-02-06 09:02:13 -0800704 intel_pstate_set_sample_time(cpu);
705}
706
707#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -0800708 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
709 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -0800710
711static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700712 ICPU(0x2a, core_params),
713 ICPU(0x2d, core_params),
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700714 ICPU(0x37, byt_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -0700715 ICPU(0x3a, core_params),
716 ICPU(0x3c, core_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -0700717 ICPU(0x3d, core_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -0700718 ICPU(0x3e, core_params),
719 ICPU(0x3f, core_params),
720 ICPU(0x45, core_params),
721 ICPU(0x46, core_params),
Mika Westerberg16405f92014-08-22 13:05:44 +0300722 ICPU(0x4c, byt_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -0700723 ICPU(0x4f, core_params),
724 ICPU(0x56, core_params),
Dirk Brandewie93f08222013-02-06 09:02:13 -0800725 {}
726};
727MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
728
729static int intel_pstate_init_cpu(unsigned int cpunum)
730{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800731 struct cpudata *cpu;
732
Dirk Brandewiec0348712014-10-13 08:37:42 -0700733 if (!all_cpu_data[cpunum])
734 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
735 GFP_KERNEL);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800736 if (!all_cpu_data[cpunum])
737 return -ENOMEM;
738
739 cpu = all_cpu_data[cpunum];
740
Dirk Brandewie93f08222013-02-06 09:02:13 -0800741 cpu->cpu = cpunum;
Vincent Minet179e8472014-07-05 01:51:33 +0200742 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700743
Dirk Brandewie93f08222013-02-06 09:02:13 -0800744 init_timer_deferrable(&cpu->timer);
745 cpu->timer.function = intel_pstate_timer_func;
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700746 cpu->timer.data = (unsigned long)cpu;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800747 cpu->timer.expires = jiffies + HZ/100;
748 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800749 intel_pstate_sample(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800750
751 add_timer_on(&cpu->timer, cpunum);
752
Andi Kleence717612014-08-27 10:17:08 -0700753 pr_debug("Intel pstate controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800754
755 return 0;
756}
757
758static unsigned int intel_pstate_get(unsigned int cpu_num)
759{
760 struct sample *sample;
761 struct cpudata *cpu;
762
763 cpu = all_cpu_data[cpu_num];
764 if (!cpu)
765 return 0;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800766 sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800767 return sample->freq;
768}
769
770static int intel_pstate_set_policy(struct cpufreq_policy *policy)
771{
Dirk Brandewied3929b82013-03-05 14:15:26 -0800772 if (!policy->cpuinfo.max_freq)
773 return -ENODEV;
774
Dirk Brandewie93f08222013-02-06 09:02:13 -0800775 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
776 limits.min_perf_pct = 100;
777 limits.min_perf = int_tofp(1);
Pali Rohár36b4bed2014-10-16 01:16:51 +0200778 limits.max_policy_pct = 100;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800779 limits.max_perf_pct = 100;
780 limits.max_perf = int_tofp(1);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700781 limits.no_turbo = 0;
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000782 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800783 }
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000784 limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
785 limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
786 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
787
Stratos Karafotis285cb992014-07-18 08:37:21 -0700788 limits.max_policy_pct = (policy->max * 100) / policy->cpuinfo.max_freq;
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700789 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
790 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000791 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800792
793 return 0;
794}
795
796static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
797{
Viresh Kumarbe49e342013-10-02 14:13:19 +0530798 cpufreq_verify_within_cpu_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800799
Stratos Karafotis285cb992014-07-18 08:37:21 -0700800 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -0700801 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800802 return -EINVAL;
803
804 return 0;
805}
806
Dirk Brandewiebb180082014-03-19 08:45:54 -0700807static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800808{
Dirk Brandewiebb180082014-03-19 08:45:54 -0700809 int cpu_num = policy->cpu;
810 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -0800811
Dirk Brandewiebb180082014-03-19 08:45:54 -0700812 pr_info("intel_pstate CPU %d exiting\n", cpu_num);
813
Dirk Brandewiec2294a22014-03-24 07:41:29 -0700814 del_timer_sync(&all_cpu_data[cpu_num]->timer);
Dirk Brandewiebb180082014-03-19 08:45:54 -0700815 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800816}
817
Paul Gortmaker27609842013-06-19 13:54:04 -0400818static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800819{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800820 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -0700821 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800822
823 rc = intel_pstate_init_cpu(policy->cpu);
824 if (rc)
825 return rc;
826
827 cpu = all_cpu_data[policy->cpu];
828
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700829 if (limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800830 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
831 else
832 policy->policy = CPUFREQ_POLICY_POWERSAVE;
833
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700834 policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
835 policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800836
837 /* cpuinfo and default policy values */
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700838 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
839 policy->cpuinfo.max_freq =
840 cpu->pstate.turbo_pstate * cpu->pstate.scaling;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800841 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
842 cpumask_set_cpu(policy->cpu, policy->cpus);
843
844 return 0;
845}
846
847static struct cpufreq_driver intel_pstate_driver = {
848 .flags = CPUFREQ_CONST_LOOPS,
849 .verify = intel_pstate_verify_policy,
850 .setpolicy = intel_pstate_set_policy,
851 .get = intel_pstate_get,
852 .init = intel_pstate_cpu_init,
Dirk Brandewiebb180082014-03-19 08:45:54 -0700853 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800854 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -0800855};
856
Dirk Brandewie6be26492013-02-15 22:55:10 +0100857static int __initdata no_load;
858
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100859static int intel_pstate_msrs_not_valid(void)
860{
861 /* Check that all the msr's we are using are valid. */
862 u64 aperf, mperf, tmp;
863
864 rdmsrl(MSR_IA32_APERF, aperf);
865 rdmsrl(MSR_IA32_MPERF, mperf);
866
Dirk Brandewie016c8152013-10-21 09:20:34 -0700867 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -0700868 !pstate_funcs.get_min() ||
869 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100870 return -ENODEV;
871
872 rdmsrl(MSR_IA32_APERF, tmp);
873 if (!(tmp - aperf))
874 return -ENODEV;
875
876 rdmsrl(MSR_IA32_MPERF, tmp);
877 if (!(tmp - mperf))
878 return -ENODEV;
879
880 return 0;
881}
Dirk Brandewie016c8152013-10-21 09:20:34 -0700882
Dirk Brandewiee0a261a2013-10-30 08:38:32 -0700883static void copy_pid_params(struct pstate_adjust_policy *policy)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700884{
885 pid_params.sample_rate_ms = policy->sample_rate_ms;
886 pid_params.p_gain_pct = policy->p_gain_pct;
887 pid_params.i_gain_pct = policy->i_gain_pct;
888 pid_params.d_gain_pct = policy->d_gain_pct;
889 pid_params.deadband = policy->deadband;
890 pid_params.setpoint = policy->setpoint;
891}
892
Dirk Brandewiee0a261a2013-10-30 08:38:32 -0700893static void copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700894{
895 pstate_funcs.get_max = funcs->get_max;
896 pstate_funcs.get_min = funcs->get_min;
897 pstate_funcs.get_turbo = funcs->get_turbo;
Dirk Brandewieb27580b2014-10-13 08:37:43 -0700898 pstate_funcs.get_scaling = funcs->get_scaling;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700899 pstate_funcs.set = funcs->set;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800900 pstate_funcs.get_vid = funcs->get_vid;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700901}
902
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800903#if IS_ENABLED(CONFIG_ACPI)
904#include <acpi/processor.h>
905
906static bool intel_pstate_no_acpi_pss(void)
907{
908 int i;
909
910 for_each_possible_cpu(i) {
911 acpi_status status;
912 union acpi_object *pss;
913 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
914 struct acpi_processor *pr = per_cpu(processors, i);
915
916 if (!pr)
917 continue;
918
919 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
920 if (ACPI_FAILURE(status))
921 continue;
922
923 pss = buffer.pointer;
924 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
925 kfree(pss);
926 return false;
927 }
928
929 kfree(pss);
930 }
931
932 return true;
933}
934
935struct hw_vendor_info {
936 u16 valid;
937 char oem_id[ACPI_OEM_ID_SIZE];
938 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
939};
940
941/* Hardware vendor-specific info that has its own power management modes */
942static struct hw_vendor_info vendor_info[] = {
943 {1, "HP ", "ProLiant"},
944 {0, "", ""},
945};
946
947static bool intel_pstate_platform_pwr_mgmt_exists(void)
948{
949 struct acpi_table_header hdr;
950 struct hw_vendor_info *v_info;
951
Stratos Karafotisc4108332014-07-18 08:37:23 -0700952 if (acpi_disabled ||
953 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800954 return false;
955
956 for (v_info = vendor_info; v_info->valid; v_info++) {
Stratos Karafotisc4108332014-07-18 08:37:23 -0700957 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
958 !strncmp(hdr.oem_table_id, v_info->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
959 intel_pstate_no_acpi_pss())
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800960 return true;
961 }
962
963 return false;
964}
965#else /* CONFIG_ACPI not enabled */
966static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
967#endif /* CONFIG_ACPI */
968
Dirk Brandewie93f08222013-02-06 09:02:13 -0800969static int __init intel_pstate_init(void)
970{
Dirk Brandewie907cc902013-03-05 14:15:27 -0800971 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800972 const struct x86_cpu_id *id;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700973 struct cpu_defaults *cpu_info;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800974
Dirk Brandewie6be26492013-02-15 22:55:10 +0100975 if (no_load)
976 return -ENODEV;
977
Dirk Brandewie93f08222013-02-06 09:02:13 -0800978 id = x86_match_cpu(intel_pstate_cpu_ids);
979 if (!id)
980 return -ENODEV;
981
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800982 /*
983 * The Intel pstate driver will be ignored if the platform
984 * firmware has its own power management modes.
985 */
986 if (intel_pstate_platform_pwr_mgmt_exists())
987 return -ENODEV;
988
Dirk Brandewie016c8152013-10-21 09:20:34 -0700989 cpu_info = (struct cpu_defaults *)id->driver_data;
990
991 copy_pid_params(&cpu_info->pid_policy);
992 copy_cpu_funcs(&cpu_info->funcs);
993
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100994 if (intel_pstate_msrs_not_valid())
995 return -ENODEV;
996
Dirk Brandewie93f08222013-02-06 09:02:13 -0800997 pr_info("Intel P-state driver initializing.\n");
998
Wei Yongjunb57ffac2013-05-13 08:03:43 +0000999 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -08001000 if (!all_cpu_data)
1001 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -08001002
1003 rc = cpufreq_register_driver(&intel_pstate_driver);
1004 if (rc)
1005 goto out;
1006
1007 intel_pstate_debug_expose_params();
1008 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -08001009
Dirk Brandewie93f08222013-02-06 09:02:13 -08001010 return rc;
1011out:
Dirk Brandewie907cc902013-03-05 14:15:27 -08001012 get_online_cpus();
1013 for_each_online_cpu(cpu) {
1014 if (all_cpu_data[cpu]) {
1015 del_timer_sync(&all_cpu_data[cpu]->timer);
1016 kfree(all_cpu_data[cpu]);
1017 }
1018 }
1019
1020 put_online_cpus();
1021 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001022 return -ENODEV;
1023}
1024device_initcall(intel_pstate_init);
1025
Dirk Brandewie6be26492013-02-15 22:55:10 +01001026static int __init intel_pstate_setup(char *str)
1027{
1028 if (!str)
1029 return -EINVAL;
1030
1031 if (!strcmp(str, "disable"))
1032 no_load = 1;
1033 return 0;
1034}
1035early_param("intel_pstate", intel_pstate_setup);
1036
Dirk Brandewie93f08222013-02-06 09:02:13 -08001037MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
1038MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
1039MODULE_LICENSE("GPL");