blob: 5f5f5ed116919ab573729617d970f0ce67efe4c1 [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;
67 int turbo_pstate;
68};
69
Dirk Brandewie007bea02013-12-18 10:32:39 -080070struct vid_data {
Dirk Brandewie21855ff2014-05-08 12:57:23 -070071 int min;
72 int max;
73 int turbo;
Dirk Brandewie007bea02013-12-18 10:32:39 -080074 int32_t ratio;
75};
76
Dirk Brandewie93f08222013-02-06 09:02:13 -080077struct _pid {
78 int setpoint;
79 int32_t integral;
80 int32_t p_gain;
81 int32_t i_gain;
82 int32_t d_gain;
83 int deadband;
Brennan Shacklettd253d2a2013-10-21 09:20:32 -070084 int32_t last_err;
Dirk Brandewie93f08222013-02-06 09:02:13 -080085};
86
87struct cpudata {
88 int cpu;
89
Dirk Brandewie93f08222013-02-06 09:02:13 -080090 struct timer_list timer;
91
Dirk Brandewie93f08222013-02-06 09:02:13 -080092 struct pstate_data pstate;
Dirk Brandewie007bea02013-12-18 10:32:39 -080093 struct vid_data vid;
Dirk Brandewie93f08222013-02-06 09:02:13 -080094 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -080095
Dirk Brandewiec4ee8412014-05-29 09:32:24 -070096 ktime_t last_sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -080097 u64 prev_aperf;
98 u64 prev_mperf;
Dirk Brandewied37e2b72014-02-12 10:01:04 -080099 struct sample sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800100};
101
102static struct cpudata **all_cpu_data;
103struct pstate_adjust_policy {
104 int sample_rate_ms;
105 int deadband;
106 int setpoint;
107 int p_gain_pct;
108 int d_gain_pct;
109 int i_gain_pct;
110};
111
Dirk Brandewie016c8152013-10-21 09:20:34 -0700112struct pstate_funcs {
113 int (*get_max)(void);
114 int (*get_min)(void);
115 int (*get_turbo)(void);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800116 void (*set)(struct cpudata*, int pstate);
117 void (*get_vid)(struct cpudata *);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800118};
119
Dirk Brandewie016c8152013-10-21 09:20:34 -0700120struct cpu_defaults {
121 struct pstate_adjust_policy pid_policy;
122 struct pstate_funcs funcs;
123};
124
125static struct pstate_adjust_policy pid_params;
126static struct pstate_funcs pstate_funcs;
127
Dirk Brandewie93f08222013-02-06 09:02:13 -0800128struct perf_limits {
129 int no_turbo;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700130 int turbo_disabled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800131 int max_perf_pct;
132 int min_perf_pct;
133 int32_t max_perf;
134 int32_t min_perf;
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700135 int max_policy_pct;
136 int max_sysfs_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800137};
138
139static struct perf_limits limits = {
140 .no_turbo = 0,
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700141 .turbo_disabled = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800142 .max_perf_pct = 100,
143 .max_perf = int_tofp(1),
144 .min_perf_pct = 0,
145 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700146 .max_policy_pct = 100,
147 .max_sysfs_pct = 100,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800148};
149
150static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700151 int deadband, int integral) {
Dirk Brandewie93f08222013-02-06 09:02:13 -0800152 pid->setpoint = setpoint;
153 pid->deadband = deadband;
154 pid->integral = int_tofp(integral);
Dirk Brandewied98d0992014-02-12 10:01:05 -0800155 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800156}
157
158static inline void pid_p_gain_set(struct _pid *pid, int percent)
159{
160 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
161}
162
163static inline void pid_i_gain_set(struct _pid *pid, int percent)
164{
165 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
166}
167
168static inline void pid_d_gain_set(struct _pid *pid, int percent)
169{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800170 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
171}
172
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700173static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800174{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700175 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800176 int32_t pterm, dterm, fp_error;
177 int32_t integral_limit;
178
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700179 fp_error = int_tofp(pid->setpoint) - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800180
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700181 if (abs(fp_error) <= int_tofp(pid->deadband))
Dirk Brandewie93f08222013-02-06 09:02:13 -0800182 return 0;
183
184 pterm = mul_fp(pid->p_gain, fp_error);
185
186 pid->integral += fp_error;
187
188 /* limit the integral term */
189 integral_limit = int_tofp(30);
190 if (pid->integral > integral_limit)
191 pid->integral = integral_limit;
192 if (pid->integral < -integral_limit)
193 pid->integral = -integral_limit;
194
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700195 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
196 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800197
198 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
Doug Smythies51d211e2014-06-17 13:36:10 -0700199 result = result + (1 << (FRAC_BITS-1));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800200 return (signed int)fp_toint(result);
201}
202
203static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
204{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700205 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
206 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
207 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800208
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700209 pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800210}
211
Dirk Brandewie93f08222013-02-06 09:02:13 -0800212static inline void intel_pstate_reset_all_pid(void)
213{
214 unsigned int cpu;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700215
Dirk Brandewie93f08222013-02-06 09:02:13 -0800216 for_each_online_cpu(cpu) {
217 if (all_cpu_data[cpu])
218 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
219 }
220}
221
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700222static inline void update_turbo_state(void)
223{
224 u64 misc_en;
225 struct cpudata *cpu;
226
227 cpu = all_cpu_data[0];
228 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
229 limits.turbo_disabled =
230 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
231 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
232}
233
Dirk Brandewie93f08222013-02-06 09:02:13 -0800234/************************** debugfs begin ************************/
235static int pid_param_set(void *data, u64 val)
236{
237 *(u32 *)data = val;
238 intel_pstate_reset_all_pid();
239 return 0;
240}
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700241
Dirk Brandewie93f08222013-02-06 09:02:13 -0800242static int pid_param_get(void *data, u64 *val)
243{
244 *val = *(u32 *)data;
245 return 0;
246}
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700247DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
Dirk Brandewie93f08222013-02-06 09:02:13 -0800248
249struct pid_param {
250 char *name;
251 void *value;
252};
253
254static struct pid_param pid_files[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700255 {"sample_rate_ms", &pid_params.sample_rate_ms},
256 {"d_gain_pct", &pid_params.d_gain_pct},
257 {"i_gain_pct", &pid_params.i_gain_pct},
258 {"deadband", &pid_params.deadband},
259 {"setpoint", &pid_params.setpoint},
260 {"p_gain_pct", &pid_params.p_gain_pct},
Dirk Brandewie93f08222013-02-06 09:02:13 -0800261 {NULL, NULL}
262};
263
Stratos Karafotis317dd502014-07-18 08:37:17 -0700264static void __init intel_pstate_debug_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800265{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700266 struct dentry *debugfs_parent;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800267 int i = 0;
268
269 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
270 if (IS_ERR_OR_NULL(debugfs_parent))
271 return;
272 while (pid_files[i].name) {
273 debugfs_create_file(pid_files[i].name, 0660,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700274 debugfs_parent, pid_files[i].value,
275 &fops_pid_param);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800276 i++;
277 }
278}
279
280/************************** debugfs end ************************/
281
282/************************** sysfs begin ************************/
283#define show_one(file_name, object) \
284 static ssize_t show_##file_name \
285 (struct kobject *kobj, struct attribute *attr, char *buf) \
286 { \
287 return sprintf(buf, "%u\n", limits.object); \
288 }
289
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700290static ssize_t show_no_turbo(struct kobject *kobj,
291 struct attribute *attr, char *buf)
292{
293 ssize_t ret;
294
295 update_turbo_state();
296 if (limits.turbo_disabled)
297 ret = sprintf(buf, "%u\n", limits.turbo_disabled);
298 else
299 ret = sprintf(buf, "%u\n", limits.no_turbo);
300
301 return ret;
302}
303
Dirk Brandewie93f08222013-02-06 09:02:13 -0800304static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700305 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800306{
307 unsigned int input;
308 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700309
Dirk Brandewie93f08222013-02-06 09:02:13 -0800310 ret = sscanf(buf, "%u", &input);
311 if (ret != 1)
312 return -EINVAL;
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700313
314 update_turbo_state();
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700315 if (limits.turbo_disabled) {
316 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700317 return -EPERM;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700318 }
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700319 limits.no_turbo = clamp_t(int, input, 0, 1);
320
Dirk Brandewie93f08222013-02-06 09:02:13 -0800321 return count;
322}
323
324static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700325 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800326{
327 unsigned int input;
328 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700329
Dirk Brandewie93f08222013-02-06 09:02:13 -0800330 ret = sscanf(buf, "%u", &input);
331 if (ret != 1)
332 return -EINVAL;
333
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700334 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
335 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800336 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700337
Dirk Brandewie93f08222013-02-06 09:02:13 -0800338 return count;
339}
340
341static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700342 const char *buf, size_t count)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800343{
344 unsigned int input;
345 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700346
Dirk Brandewie93f08222013-02-06 09:02:13 -0800347 ret = sscanf(buf, "%u", &input);
348 if (ret != 1)
349 return -EINVAL;
350 limits.min_perf_pct = clamp_t(int, input, 0 , 100);
351 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
352
353 return count;
354}
355
Dirk Brandewie93f08222013-02-06 09:02:13 -0800356show_one(max_perf_pct, max_perf_pct);
357show_one(min_perf_pct, min_perf_pct);
358
359define_one_global_rw(no_turbo);
360define_one_global_rw(max_perf_pct);
361define_one_global_rw(min_perf_pct);
362
363static struct attribute *intel_pstate_attributes[] = {
364 &no_turbo.attr,
365 &max_perf_pct.attr,
366 &min_perf_pct.attr,
367 NULL
368};
369
370static struct attribute_group intel_pstate_attr_group = {
371 .attrs = intel_pstate_attributes,
372};
Dirk Brandewie93f08222013-02-06 09:02:13 -0800373
Stratos Karafotis317dd502014-07-18 08:37:17 -0700374static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800375{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700376 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800377 int rc;
378
379 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
380 &cpu_subsys.dev_root->kobj);
381 BUG_ON(!intel_pstate_kobject);
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700382 rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800383 BUG_ON(rc);
384}
385
386/************************** sysfs end ************************/
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700387static int byt_get_min_pstate(void)
388{
389 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700390
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700391 rdmsrl(BYT_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700392 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700393}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800394
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700395static int byt_get_max_pstate(void)
396{
397 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700398
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700399 rdmsrl(BYT_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700400 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700401}
402
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800403static int byt_get_turbo_pstate(void)
404{
405 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700406
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800407 rdmsrl(BYT_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700408 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800409}
410
Dirk Brandewie007bea02013-12-18 10:32:39 -0800411static void byt_set_pstate(struct cpudata *cpudata, int pstate)
412{
413 u64 val;
414 int32_t vid_fp;
415 u32 vid;
416
417 val = pstate << 8;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700418 if (limits.no_turbo && !limits.turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800419 val |= (u64)1 << 32;
420
421 vid_fp = cpudata->vid.min + mul_fp(
422 int_tofp(pstate - cpudata->pstate.min_pstate),
423 cpudata->vid.ratio);
424
425 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
426 vid = fp_toint(vid_fp);
427
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700428 if (pstate > cpudata->pstate.max_pstate)
429 vid = cpudata->vid.turbo;
430
Dirk Brandewie007bea02013-12-18 10:32:39 -0800431 val |= vid;
432
433 wrmsrl(MSR_IA32_PERF_CTL, val);
434}
435
436static void byt_get_vid(struct cpudata *cpudata)
437{
438 u64 value;
439
440 rdmsrl(BYT_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700441 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
442 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800443 cpudata->vid.ratio = div_fp(
444 cpudata->vid.max - cpudata->vid.min,
445 int_tofp(cpudata->pstate.max_pstate -
446 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700447
448 rdmsrl(BYT_TURBO_VIDS, value);
449 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800450}
451
Dirk Brandewie016c8152013-10-21 09:20:34 -0700452static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800453{
454 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700455
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000456 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800457 return (value >> 40) & 0xFF;
458}
459
Dirk Brandewie016c8152013-10-21 09:20:34 -0700460static int core_get_max_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800461{
462 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700463
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000464 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800465 return (value >> 8) & 0xFF;
466}
467
Dirk Brandewie016c8152013-10-21 09:20:34 -0700468static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800469{
470 u64 value;
471 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700472
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000473 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700474 nont = core_get_max_pstate();
Stratos Karafotis285cb992014-07-18 08:37:21 -0700475 ret = (value) & 255;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800476 if (ret <= nont)
477 ret = nont;
478 return ret;
479}
480
Dirk Brandewie007bea02013-12-18 10:32:39 -0800481static void core_set_pstate(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700482{
483 u64 val;
484
485 val = pstate << 8;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700486 if (limits.no_turbo && !limits.turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700487 val |= (u64)1 << 32;
488
Dirk Brandewiebb180082014-03-19 08:45:54 -0700489 wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700490}
491
492static struct cpu_defaults core_params = {
493 .pid_policy = {
494 .sample_rate_ms = 10,
495 .deadband = 0,
496 .setpoint = 97,
497 .p_gain_pct = 20,
498 .d_gain_pct = 0,
499 .i_gain_pct = 0,
500 },
501 .funcs = {
502 .get_max = core_get_max_pstate,
503 .get_min = core_get_min_pstate,
504 .get_turbo = core_get_turbo_pstate,
505 .set = core_set_pstate,
506 },
507};
508
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700509static struct cpu_defaults byt_params = {
510 .pid_policy = {
511 .sample_rate_ms = 10,
512 .deadband = 0,
513 .setpoint = 97,
514 .p_gain_pct = 14,
515 .d_gain_pct = 0,
516 .i_gain_pct = 4,
517 },
518 .funcs = {
519 .get_max = byt_get_max_pstate,
520 .get_min = byt_get_min_pstate,
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800521 .get_turbo = byt_get_turbo_pstate,
Dirk Brandewie007bea02013-12-18 10:32:39 -0800522 .set = byt_set_pstate,
523 .get_vid = byt_get_vid,
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700524 },
525};
526
Dirk Brandewie93f08222013-02-06 09:02:13 -0800527static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
528{
529 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie7244cb62013-10-21 09:20:33 -0700530 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800531 int min_perf;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700532
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700533 if (limits.no_turbo || limits.turbo_disabled)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800534 max_perf = cpu->pstate.max_pstate;
535
Dirk Brandewie7244cb62013-10-21 09:20:33 -0700536 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
537 *max = clamp_t(int, max_perf_adj,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800538 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
539
540 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700541 *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800542}
543
544static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
545{
546 int max_perf, min_perf;
547
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700548 update_turbo_state();
549
Dirk Brandewie93f08222013-02-06 09:02:13 -0800550 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
551
552 pstate = clamp_t(int, pstate, min_perf, max_perf);
553
554 if (pstate == cpu->pstate.current_pstate)
555 return;
556
Dirk Brandewie93f08222013-02-06 09:02:13 -0800557 trace_cpu_frequency(pstate * 100000, cpu->cpu);
Dirk Brandewie35363e92013-05-07 08:20:30 -0700558
Dirk Brandewie93f08222013-02-06 09:02:13 -0800559 cpu->pstate.current_pstate = pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800560
Dirk Brandewie007bea02013-12-18 10:32:39 -0800561 pstate_funcs.set(cpu, pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800562}
563
Dirk Brandewie93f08222013-02-06 09:02:13 -0800564static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
565{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700566 cpu->pstate.min_pstate = pstate_funcs.get_min();
567 cpu->pstate.max_pstate = pstate_funcs.get_max();
568 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800569
Dirk Brandewie007bea02013-12-18 10:32:39 -0800570 if (pstate_funcs.get_vid)
571 pstate_funcs.get_vid(cpu);
Dirk Brandewied40a63c2014-05-08 12:57:24 -0700572 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800573}
574
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300575static inline void intel_pstate_calc_busy(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800576{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300577 struct sample *sample = &cpu->sample;
Doug Smythiesbf810222014-05-30 10:10:57 -0700578 int64_t core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800579
Doug Smythiesbf810222014-05-30 10:10:57 -0700580 core_pct = int_tofp(sample->aperf) * int_tofp(100);
Stratos Karafotis78e27082014-07-18 08:37:27 -0700581 core_pct = div64_u64(core_pct, int_tofp(sample->mperf));
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800582
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800583 sample->freq = fp_toint(
Dirk Brandewiee66c1762014-02-25 10:35:37 -0800584 mul_fp(int_tofp(cpu->pstate.max_pstate * 1000), core_pct));
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800585
Doug Smythiesbf810222014-05-30 10:10:57 -0700586 sample->core_pct_busy = (int32_t)core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800587}
588
589static inline void intel_pstate_sample(struct cpudata *cpu)
590{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800591 u64 aperf, mperf;
Stratos Karafotis4ab60c32014-07-18 08:37:24 -0700592 unsigned long flags;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800593
Stratos Karafotis4ab60c32014-07-18 08:37:24 -0700594 local_irq_save(flags);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800595 rdmsrl(MSR_IA32_APERF, aperf);
596 rdmsrl(MSR_IA32_MPERF, mperf);
Stratos Karafotis4ab60c32014-07-18 08:37:24 -0700597 local_irq_restore(flags);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800598
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700599 cpu->last_sample_time = cpu->sample.time;
600 cpu->sample.time = ktime_get();
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800601 cpu->sample.aperf = aperf;
602 cpu->sample.mperf = mperf;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800603 cpu->sample.aperf -= cpu->prev_aperf;
604 cpu->sample.mperf -= cpu->prev_mperf;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800605
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300606 intel_pstate_calc_busy(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800607
Dirk Brandewie93f08222013-02-06 09:02:13 -0800608 cpu->prev_aperf = aperf;
609 cpu->prev_mperf = mperf;
610}
611
612static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
613{
Stratos Karafotisabf013b2014-07-18 08:37:22 -0700614 int delay;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800615
Stratos Karafotisabf013b2014-07-18 08:37:22 -0700616 delay = msecs_to_jiffies(pid_params.sample_rate_ms);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800617 mod_timer_pinned(&cpu->timer, jiffies + delay);
618}
619
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700620static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800621{
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700622 int32_t core_busy, max_pstate, current_pstate, sample_ratio;
623 u32 duration_us;
624 u32 sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800625
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800626 core_busy = cpu->sample.core_pct_busy;
Dirk Brandewie2134ed42013-07-18 08:48:42 -0700627 max_pstate = int_tofp(cpu->pstate.max_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800628 current_pstate = int_tofp(cpu->pstate.current_pstate);
Dirk Brandewiee66c1762014-02-25 10:35:37 -0800629 core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700630
Stratos Karafotis285cb992014-07-18 08:37:21 -0700631 sample_time = pid_params.sample_rate_ms * USEC_PER_MSEC;
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700632 duration_us = (u32) ktime_us_delta(cpu->sample.time,
Stratos Karafotisc4108332014-07-18 08:37:23 -0700633 cpu->last_sample_time);
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700634 if (duration_us > sample_time * 3) {
635 sample_ratio = div_fp(int_tofp(sample_time),
Stratos Karafotisc4108332014-07-18 08:37:23 -0700636 int_tofp(duration_us));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700637 core_busy = mul_fp(core_busy, sample_ratio);
638 }
639
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -0700640 return core_busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800641}
642
643static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
644{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700645 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800646 struct _pid *pid;
Stratos Karafotis4b707c82014-07-18 08:37:26 -0700647 signed int ctl;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800648
649 pid = &cpu->pid;
650 busy_scaled = intel_pstate_get_scaled_busy(cpu);
651
652 ctl = pid_calc(pid, busy_scaled);
653
Stratos Karafotis4b707c82014-07-18 08:37:26 -0700654 /* Negative values of ctl increase the pstate and vice versa */
655 intel_pstate_set_pstate(cpu, cpu->pstate.current_pstate - ctl);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800656}
657
Dirk Brandewie93f08222013-02-06 09:02:13 -0800658static void intel_pstate_timer_func(unsigned long __data)
659{
660 struct cpudata *cpu = (struct cpudata *) __data;
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800661 struct sample *sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800662
663 intel_pstate_sample(cpu);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800664
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800665 sample = &cpu->sample;
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800666
Dirk Brandewieca182ae2013-05-07 08:20:27 -0700667 intel_pstate_adjust_busy_pstate(cpu);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800668
669 trace_pstate_sample(fp_toint(sample->core_pct_busy),
670 fp_toint(intel_pstate_get_scaled_busy(cpu)),
671 cpu->pstate.current_pstate,
672 sample->mperf,
673 sample->aperf,
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800674 sample->freq);
675
Dirk Brandewie93f08222013-02-06 09:02:13 -0800676 intel_pstate_set_sample_time(cpu);
677}
678
679#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -0800680 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
681 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -0800682
683static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700684 ICPU(0x2a, core_params),
685 ICPU(0x2d, core_params),
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700686 ICPU(0x37, byt_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -0700687 ICPU(0x3a, core_params),
688 ICPU(0x3c, core_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -0700689 ICPU(0x3d, core_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -0700690 ICPU(0x3e, core_params),
691 ICPU(0x3f, core_params),
692 ICPU(0x45, core_params),
693 ICPU(0x46, core_params),
Mika Westerberg16405f92014-08-22 13:05:44 +0300694 ICPU(0x4c, byt_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -0700695 ICPU(0x4f, core_params),
696 ICPU(0x56, core_params),
Dirk Brandewie93f08222013-02-06 09:02:13 -0800697 {}
698};
699MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
700
701static int intel_pstate_init_cpu(unsigned int cpunum)
702{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800703 struct cpudata *cpu;
704
Dirk Brandewie93f08222013-02-06 09:02:13 -0800705 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
706 if (!all_cpu_data[cpunum])
707 return -ENOMEM;
708
709 cpu = all_cpu_data[cpunum];
710
Dirk Brandewie93f08222013-02-06 09:02:13 -0800711 cpu->cpu = cpunum;
Vincent Minet179e8472014-07-05 01:51:33 +0200712 intel_pstate_get_cpu_pstates(cpu);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700713
Dirk Brandewie93f08222013-02-06 09:02:13 -0800714 init_timer_deferrable(&cpu->timer);
715 cpu->timer.function = intel_pstate_timer_func;
Stratos Karafotis2d8d1f12014-07-18 08:37:20 -0700716 cpu->timer.data = (unsigned long)cpu;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800717 cpu->timer.expires = jiffies + HZ/100;
718 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800719 intel_pstate_sample(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800720
721 add_timer_on(&cpu->timer, cpunum);
722
Andi Kleence717612014-08-27 10:17:08 -0700723 pr_debug("Intel pstate controlling: cpu %d\n", cpunum);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800724
725 return 0;
726}
727
728static unsigned int intel_pstate_get(unsigned int cpu_num)
729{
730 struct sample *sample;
731 struct cpudata *cpu;
732
733 cpu = all_cpu_data[cpu_num];
734 if (!cpu)
735 return 0;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800736 sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800737 return sample->freq;
738}
739
740static int intel_pstate_set_policy(struct cpufreq_policy *policy)
741{
Dirk Brandewied3929b82013-03-05 14:15:26 -0800742 if (!policy->cpuinfo.max_freq)
743 return -ENODEV;
744
Dirk Brandewie93f08222013-02-06 09:02:13 -0800745 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
746 limits.min_perf_pct = 100;
747 limits.min_perf = int_tofp(1);
Pali Rohár36b4bed2014-10-16 01:16:51 +0200748 limits.max_policy_pct = 100;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800749 limits.max_perf_pct = 100;
750 limits.max_perf = int_tofp(1);
Gabriele Mazzotta4521e1a02014-10-13 08:37:41 -0700751 limits.no_turbo = 0;
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000752 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800753 }
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000754 limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
755 limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
756 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
757
Stratos Karafotis285cb992014-07-18 08:37:21 -0700758 limits.max_policy_pct = (policy->max * 100) / policy->cpuinfo.max_freq;
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700759 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
760 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000761 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800762
763 return 0;
764}
765
766static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
767{
Viresh Kumarbe49e342013-10-02 14:13:19 +0530768 cpufreq_verify_within_cpu_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800769
Stratos Karafotis285cb992014-07-18 08:37:21 -0700770 if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
Stratos Karafotisc4108332014-07-18 08:37:23 -0700771 policy->policy != CPUFREQ_POLICY_PERFORMANCE)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800772 return -EINVAL;
773
774 return 0;
775}
776
Dirk Brandewiebb180082014-03-19 08:45:54 -0700777static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800778{
Dirk Brandewiebb180082014-03-19 08:45:54 -0700779 int cpu_num = policy->cpu;
780 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -0800781
Dirk Brandewiebb180082014-03-19 08:45:54 -0700782 pr_info("intel_pstate CPU %d exiting\n", cpu_num);
783
Dirk Brandewiec2294a22014-03-24 07:41:29 -0700784 del_timer_sync(&all_cpu_data[cpu_num]->timer);
Dirk Brandewiebb180082014-03-19 08:45:54 -0700785 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
786 kfree(all_cpu_data[cpu_num]);
787 all_cpu_data[cpu_num] = NULL;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800788}
789
Paul Gortmaker27609842013-06-19 13:54:04 -0400790static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800791{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800792 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -0700793 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800794
795 rc = intel_pstate_init_cpu(policy->cpu);
796 if (rc)
797 return rc;
798
799 cpu = all_cpu_data[policy->cpu];
800
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700801 if (limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800802 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
803 else
804 policy->policy = CPUFREQ_POLICY_POWERSAVE;
805
Dirk Brandewie52e0a502013-10-15 11:06:14 -0700806 policy->min = cpu->pstate.min_pstate * 100000;
807 policy->max = cpu->pstate.turbo_pstate * 100000;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800808
809 /* cpuinfo and default policy values */
810 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
811 policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
812 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
813 cpumask_set_cpu(policy->cpu, policy->cpus);
814
815 return 0;
816}
817
818static struct cpufreq_driver intel_pstate_driver = {
819 .flags = CPUFREQ_CONST_LOOPS,
820 .verify = intel_pstate_verify_policy,
821 .setpolicy = intel_pstate_set_policy,
822 .get = intel_pstate_get,
823 .init = intel_pstate_cpu_init,
Dirk Brandewiebb180082014-03-19 08:45:54 -0700824 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800825 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -0800826};
827
Dirk Brandewie6be26492013-02-15 22:55:10 +0100828static int __initdata no_load;
829
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100830static int intel_pstate_msrs_not_valid(void)
831{
832 /* Check that all the msr's we are using are valid. */
833 u64 aperf, mperf, tmp;
834
835 rdmsrl(MSR_IA32_APERF, aperf);
836 rdmsrl(MSR_IA32_MPERF, mperf);
837
Dirk Brandewie016c8152013-10-21 09:20:34 -0700838 if (!pstate_funcs.get_max() ||
Stratos Karafotisc4108332014-07-18 08:37:23 -0700839 !pstate_funcs.get_min() ||
840 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100841 return -ENODEV;
842
843 rdmsrl(MSR_IA32_APERF, tmp);
844 if (!(tmp - aperf))
845 return -ENODEV;
846
847 rdmsrl(MSR_IA32_MPERF, tmp);
848 if (!(tmp - mperf))
849 return -ENODEV;
850
851 return 0;
852}
Dirk Brandewie016c8152013-10-21 09:20:34 -0700853
Dirk Brandewiee0a261a2013-10-30 08:38:32 -0700854static void copy_pid_params(struct pstate_adjust_policy *policy)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700855{
856 pid_params.sample_rate_ms = policy->sample_rate_ms;
857 pid_params.p_gain_pct = policy->p_gain_pct;
858 pid_params.i_gain_pct = policy->i_gain_pct;
859 pid_params.d_gain_pct = policy->d_gain_pct;
860 pid_params.deadband = policy->deadband;
861 pid_params.setpoint = policy->setpoint;
862}
863
Dirk Brandewiee0a261a2013-10-30 08:38:32 -0700864static void copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700865{
866 pstate_funcs.get_max = funcs->get_max;
867 pstate_funcs.get_min = funcs->get_min;
868 pstate_funcs.get_turbo = funcs->get_turbo;
869 pstate_funcs.set = funcs->set;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800870 pstate_funcs.get_vid = funcs->get_vid;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700871}
872
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800873#if IS_ENABLED(CONFIG_ACPI)
874#include <acpi/processor.h>
875
876static bool intel_pstate_no_acpi_pss(void)
877{
878 int i;
879
880 for_each_possible_cpu(i) {
881 acpi_status status;
882 union acpi_object *pss;
883 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
884 struct acpi_processor *pr = per_cpu(processors, i);
885
886 if (!pr)
887 continue;
888
889 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
890 if (ACPI_FAILURE(status))
891 continue;
892
893 pss = buffer.pointer;
894 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
895 kfree(pss);
896 return false;
897 }
898
899 kfree(pss);
900 }
901
902 return true;
903}
904
905struct hw_vendor_info {
906 u16 valid;
907 char oem_id[ACPI_OEM_ID_SIZE];
908 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
909};
910
911/* Hardware vendor-specific info that has its own power management modes */
912static struct hw_vendor_info vendor_info[] = {
913 {1, "HP ", "ProLiant"},
914 {0, "", ""},
915};
916
917static bool intel_pstate_platform_pwr_mgmt_exists(void)
918{
919 struct acpi_table_header hdr;
920 struct hw_vendor_info *v_info;
921
Stratos Karafotisc4108332014-07-18 08:37:23 -0700922 if (acpi_disabled ||
923 ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800924 return false;
925
926 for (v_info = vendor_info; v_info->valid; v_info++) {
Stratos Karafotisc4108332014-07-18 08:37:23 -0700927 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
928 !strncmp(hdr.oem_table_id, v_info->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
929 intel_pstate_no_acpi_pss())
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800930 return true;
931 }
932
933 return false;
934}
935#else /* CONFIG_ACPI not enabled */
936static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
937#endif /* CONFIG_ACPI */
938
Dirk Brandewie93f08222013-02-06 09:02:13 -0800939static int __init intel_pstate_init(void)
940{
Dirk Brandewie907cc902013-03-05 14:15:27 -0800941 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800942 const struct x86_cpu_id *id;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700943 struct cpu_defaults *cpu_info;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800944
Dirk Brandewie6be26492013-02-15 22:55:10 +0100945 if (no_load)
946 return -ENODEV;
947
Dirk Brandewie93f08222013-02-06 09:02:13 -0800948 id = x86_match_cpu(intel_pstate_cpu_ids);
949 if (!id)
950 return -ENODEV;
951
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800952 /*
953 * The Intel pstate driver will be ignored if the platform
954 * firmware has its own power management modes.
955 */
956 if (intel_pstate_platform_pwr_mgmt_exists())
957 return -ENODEV;
958
Dirk Brandewie016c8152013-10-21 09:20:34 -0700959 cpu_info = (struct cpu_defaults *)id->driver_data;
960
961 copy_pid_params(&cpu_info->pid_policy);
962 copy_cpu_funcs(&cpu_info->funcs);
963
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100964 if (intel_pstate_msrs_not_valid())
965 return -ENODEV;
966
Dirk Brandewie93f08222013-02-06 09:02:13 -0800967 pr_info("Intel P-state driver initializing.\n");
968
Wei Yongjunb57ffac2013-05-13 08:03:43 +0000969 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -0800970 if (!all_cpu_data)
971 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800972
973 rc = cpufreq_register_driver(&intel_pstate_driver);
974 if (rc)
975 goto out;
976
977 intel_pstate_debug_expose_params();
978 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800979
Dirk Brandewie93f08222013-02-06 09:02:13 -0800980 return rc;
981out:
Dirk Brandewie907cc902013-03-05 14:15:27 -0800982 get_online_cpus();
983 for_each_online_cpu(cpu) {
984 if (all_cpu_data[cpu]) {
985 del_timer_sync(&all_cpu_data[cpu]->timer);
986 kfree(all_cpu_data[cpu]);
987 }
988 }
989
990 put_online_cpus();
991 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800992 return -ENODEV;
993}
994device_initcall(intel_pstate_init);
995
Dirk Brandewie6be26492013-02-15 22:55:10 +0100996static int __init intel_pstate_setup(char *str)
997{
998 if (!str)
999 return -EINVAL;
1000
1001 if (!strcmp(str, "disable"))
1002 no_load = 1;
1003 return 0;
1004}
1005early_param("intel_pstate", intel_pstate_setup);
1006
Dirk Brandewie93f08222013-02-06 09:02:13 -08001007MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
1008MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
1009MODULE_LICENSE("GPL");