blob: e9f3048eb6f22c00b2311e189cede421a743e440 [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,
141 .max_perf_pct = 100,
142 .max_perf = int_tofp(1),
143 .min_perf_pct = 0,
144 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700145 .max_policy_pct = 100,
146 .max_sysfs_pct = 100,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800147};
148
149static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
150 int deadband, int integral) {
151 pid->setpoint = setpoint;
152 pid->deadband = deadband;
153 pid->integral = int_tofp(integral);
Dirk Brandewied98d0992014-02-12 10:01:05 -0800154 pid->last_err = int_tofp(setpoint) - int_tofp(busy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800155}
156
157static inline void pid_p_gain_set(struct _pid *pid, int percent)
158{
159 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
160}
161
162static inline void pid_i_gain_set(struct _pid *pid, int percent)
163{
164 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
165}
166
167static inline void pid_d_gain_set(struct _pid *pid, int percent)
168{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800169 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
170}
171
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700172static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800173{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700174 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800175 int32_t pterm, dterm, fp_error;
176 int32_t integral_limit;
177
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700178 fp_error = int_tofp(pid->setpoint) - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800179
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700180 if (abs(fp_error) <= int_tofp(pid->deadband))
Dirk Brandewie93f08222013-02-06 09:02:13 -0800181 return 0;
182
183 pterm = mul_fp(pid->p_gain, fp_error);
184
185 pid->integral += fp_error;
186
187 /* limit the integral term */
188 integral_limit = int_tofp(30);
189 if (pid->integral > integral_limit)
190 pid->integral = integral_limit;
191 if (pid->integral < -integral_limit)
192 pid->integral = -integral_limit;
193
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700194 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
195 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800196
197 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
Doug Smythies51d211e2014-06-17 13:36:10 -0700198 result = result + (1 << (FRAC_BITS-1));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800199 return (signed int)fp_toint(result);
200}
201
202static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
203{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700204 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
205 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
206 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800207
208 pid_reset(&cpu->pid,
Dirk Brandewie016c8152013-10-21 09:20:34 -0700209 pid_params.setpoint,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800210 100,
Dirk Brandewie016c8152013-10-21 09:20:34 -0700211 pid_params.deadband,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800212 0);
213}
214
Dirk Brandewie93f08222013-02-06 09:02:13 -0800215static inline void intel_pstate_reset_all_pid(void)
216{
217 unsigned int cpu;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700218
Dirk Brandewie93f08222013-02-06 09:02:13 -0800219 for_each_online_cpu(cpu) {
220 if (all_cpu_data[cpu])
221 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
222 }
223}
224
225/************************** debugfs begin ************************/
226static int pid_param_set(void *data, u64 val)
227{
228 *(u32 *)data = val;
229 intel_pstate_reset_all_pid();
230 return 0;
231}
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700232
Dirk Brandewie93f08222013-02-06 09:02:13 -0800233static int pid_param_get(void *data, u64 *val)
234{
235 *val = *(u32 *)data;
236 return 0;
237}
238DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get,
239 pid_param_set, "%llu\n");
240
241struct pid_param {
242 char *name;
243 void *value;
244};
245
246static struct pid_param pid_files[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700247 {"sample_rate_ms", &pid_params.sample_rate_ms},
248 {"d_gain_pct", &pid_params.d_gain_pct},
249 {"i_gain_pct", &pid_params.i_gain_pct},
250 {"deadband", &pid_params.deadband},
251 {"setpoint", &pid_params.setpoint},
252 {"p_gain_pct", &pid_params.p_gain_pct},
Dirk Brandewie93f08222013-02-06 09:02:13 -0800253 {NULL, NULL}
254};
255
Stratos Karafotis317dd502014-07-18 08:37:17 -0700256static void __init intel_pstate_debug_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800257{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700258 struct dentry *debugfs_parent;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800259 int i = 0;
260
261 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
262 if (IS_ERR_OR_NULL(debugfs_parent))
263 return;
264 while (pid_files[i].name) {
265 debugfs_create_file(pid_files[i].name, 0660,
266 debugfs_parent, pid_files[i].value,
267 &fops_pid_param);
268 i++;
269 }
270}
271
272/************************** debugfs end ************************/
273
274/************************** sysfs begin ************************/
275#define show_one(file_name, object) \
276 static ssize_t show_##file_name \
277 (struct kobject *kobj, struct attribute *attr, char *buf) \
278 { \
279 return sprintf(buf, "%u\n", limits.object); \
280 }
281
282static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
283 const char *buf, size_t count)
284{
285 unsigned int input;
286 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700287
Dirk Brandewie93f08222013-02-06 09:02:13 -0800288 ret = sscanf(buf, "%u", &input);
289 if (ret != 1)
290 return -EINVAL;
291 limits.no_turbo = clamp_t(int, input, 0 , 1);
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700292 if (limits.turbo_disabled) {
293 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
294 limits.no_turbo = limits.turbo_disabled;
295 }
Dirk Brandewie93f08222013-02-06 09:02:13 -0800296 return count;
297}
298
299static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
300 const char *buf, size_t count)
301{
302 unsigned int input;
303 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700304
Dirk Brandewie93f08222013-02-06 09:02:13 -0800305 ret = sscanf(buf, "%u", &input);
306 if (ret != 1)
307 return -EINVAL;
308
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700309 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
310 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800311 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700312
Dirk Brandewie93f08222013-02-06 09:02:13 -0800313 return count;
314}
315
316static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
317 const char *buf, size_t count)
318{
319 unsigned int input;
320 int ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700321
Dirk Brandewie93f08222013-02-06 09:02:13 -0800322 ret = sscanf(buf, "%u", &input);
323 if (ret != 1)
324 return -EINVAL;
325 limits.min_perf_pct = clamp_t(int, input, 0 , 100);
326 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
327
328 return count;
329}
330
331show_one(no_turbo, no_turbo);
332show_one(max_perf_pct, max_perf_pct);
333show_one(min_perf_pct, min_perf_pct);
334
335define_one_global_rw(no_turbo);
336define_one_global_rw(max_perf_pct);
337define_one_global_rw(min_perf_pct);
338
339static struct attribute *intel_pstate_attributes[] = {
340 &no_turbo.attr,
341 &max_perf_pct.attr,
342 &min_perf_pct.attr,
343 NULL
344};
345
346static struct attribute_group intel_pstate_attr_group = {
347 .attrs = intel_pstate_attributes,
348};
Dirk Brandewie93f08222013-02-06 09:02:13 -0800349
Stratos Karafotis317dd502014-07-18 08:37:17 -0700350static void __init intel_pstate_sysfs_expose_params(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800351{
Stratos Karafotis317dd502014-07-18 08:37:17 -0700352 struct kobject *intel_pstate_kobject;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800353 int rc;
354
355 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
356 &cpu_subsys.dev_root->kobj);
357 BUG_ON(!intel_pstate_kobject);
358 rc = sysfs_create_group(intel_pstate_kobject,
359 &intel_pstate_attr_group);
360 BUG_ON(rc);
361}
362
363/************************** sysfs end ************************/
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700364static int byt_get_min_pstate(void)
365{
366 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700367
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700368 rdmsrl(BYT_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700369 return (value >> 8) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700370}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800371
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700372static int byt_get_max_pstate(void)
373{
374 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700375
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700376 rdmsrl(BYT_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700377 return (value >> 16) & 0x7F;
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700378}
379
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800380static int byt_get_turbo_pstate(void)
381{
382 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700383
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800384 rdmsrl(BYT_TURBO_RATIOS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700385 return value & 0x7F;
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800386}
387
Dirk Brandewie007bea02013-12-18 10:32:39 -0800388static void byt_set_pstate(struct cpudata *cpudata, int pstate)
389{
390 u64 val;
391 int32_t vid_fp;
392 u32 vid;
393
394 val = pstate << 8;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700395 if (limits.no_turbo && !limits.turbo_disabled)
Dirk Brandewie007bea02013-12-18 10:32:39 -0800396 val |= (u64)1 << 32;
397
398 vid_fp = cpudata->vid.min + mul_fp(
399 int_tofp(pstate - cpudata->pstate.min_pstate),
400 cpudata->vid.ratio);
401
402 vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
403 vid = fp_toint(vid_fp);
404
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700405 if (pstate > cpudata->pstate.max_pstate)
406 vid = cpudata->vid.turbo;
407
Dirk Brandewie007bea02013-12-18 10:32:39 -0800408 val |= vid;
409
410 wrmsrl(MSR_IA32_PERF_CTL, val);
411}
412
413static void byt_get_vid(struct cpudata *cpudata)
414{
415 u64 value;
416
417 rdmsrl(BYT_VIDS, value);
Dirk Brandewiec16ed062014-06-20 07:27:58 -0700418 cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
419 cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
Dirk Brandewie007bea02013-12-18 10:32:39 -0800420 cpudata->vid.ratio = div_fp(
421 cpudata->vid.max - cpudata->vid.min,
422 int_tofp(cpudata->pstate.max_pstate -
423 cpudata->pstate.min_pstate));
Dirk Brandewie21855ff2014-05-08 12:57:23 -0700424
425 rdmsrl(BYT_TURBO_VIDS, value);
426 cpudata->vid.turbo = value & 0x7f;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800427}
428
Dirk Brandewie016c8152013-10-21 09:20:34 -0700429static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800430{
431 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700432
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000433 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800434 return (value >> 40) & 0xFF;
435}
436
Dirk Brandewie016c8152013-10-21 09:20:34 -0700437static int core_get_max_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800438{
439 u64 value;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700440
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000441 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800442 return (value >> 8) & 0xFF;
443}
444
Dirk Brandewie016c8152013-10-21 09:20:34 -0700445static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800446{
447 u64 value;
448 int nont, ret;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700449
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000450 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700451 nont = core_get_max_pstate();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800452 ret = ((value) & 255);
453 if (ret <= nont)
454 ret = nont;
455 return ret;
456}
457
Dirk Brandewie007bea02013-12-18 10:32:39 -0800458static void core_set_pstate(struct cpudata *cpudata, int pstate)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700459{
460 u64 val;
461
462 val = pstate << 8;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700463 if (limits.no_turbo && !limits.turbo_disabled)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700464 val |= (u64)1 << 32;
465
Dirk Brandewiebb180082014-03-19 08:45:54 -0700466 wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700467}
468
469static struct cpu_defaults core_params = {
470 .pid_policy = {
471 .sample_rate_ms = 10,
472 .deadband = 0,
473 .setpoint = 97,
474 .p_gain_pct = 20,
475 .d_gain_pct = 0,
476 .i_gain_pct = 0,
477 },
478 .funcs = {
479 .get_max = core_get_max_pstate,
480 .get_min = core_get_min_pstate,
481 .get_turbo = core_get_turbo_pstate,
482 .set = core_set_pstate,
483 },
484};
485
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700486static struct cpu_defaults byt_params = {
487 .pid_policy = {
488 .sample_rate_ms = 10,
489 .deadband = 0,
490 .setpoint = 97,
491 .p_gain_pct = 14,
492 .d_gain_pct = 0,
493 .i_gain_pct = 4,
494 },
495 .funcs = {
496 .get_max = byt_get_max_pstate,
497 .get_min = byt_get_min_pstate,
Dirk Brandewie61d8d2a2014-02-12 10:01:07 -0800498 .get_turbo = byt_get_turbo_pstate,
Dirk Brandewie007bea02013-12-18 10:32:39 -0800499 .set = byt_set_pstate,
500 .get_vid = byt_get_vid,
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700501 },
502};
503
Dirk Brandewie93f08222013-02-06 09:02:13 -0800504static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
505{
506 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie7244cb62013-10-21 09:20:33 -0700507 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800508 int min_perf;
Stratos Karafotis845c1cb2014-07-18 08:37:19 -0700509
Dirk Brandewie93f08222013-02-06 09:02:13 -0800510 if (limits.no_turbo)
511 max_perf = cpu->pstate.max_pstate;
512
Dirk Brandewie7244cb62013-10-21 09:20:33 -0700513 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
514 *max = clamp_t(int, max_perf_adj,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800515 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
516
517 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
518 *min = clamp_t(int, min_perf,
519 cpu->pstate.min_pstate, max_perf);
520}
521
522static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
523{
524 int max_perf, min_perf;
525
526 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
527
528 pstate = clamp_t(int, pstate, min_perf, max_perf);
529
530 if (pstate == cpu->pstate.current_pstate)
531 return;
532
Dirk Brandewie93f08222013-02-06 09:02:13 -0800533 trace_cpu_frequency(pstate * 100000, cpu->cpu);
Dirk Brandewie35363e92013-05-07 08:20:30 -0700534
Dirk Brandewie93f08222013-02-06 09:02:13 -0800535 cpu->pstate.current_pstate = pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800536
Dirk Brandewie007bea02013-12-18 10:32:39 -0800537 pstate_funcs.set(cpu, pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800538}
539
540static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps)
541{
542 int target;
543 target = cpu->pstate.current_pstate + steps;
544
545 intel_pstate_set_pstate(cpu, target);
546}
547
548static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps)
549{
550 int target;
551 target = cpu->pstate.current_pstate - steps;
552 intel_pstate_set_pstate(cpu, target);
553}
554
555static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
556{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700557 cpu->pstate.min_pstate = pstate_funcs.get_min();
558 cpu->pstate.max_pstate = pstate_funcs.get_max();
559 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800560
Dirk Brandewie007bea02013-12-18 10:32:39 -0800561 if (pstate_funcs.get_vid)
562 pstate_funcs.get_vid(cpu);
Dirk Brandewied40a63c2014-05-08 12:57:24 -0700563 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800564}
565
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300566static inline void intel_pstate_calc_busy(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800567{
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300568 struct sample *sample = &cpu->sample;
Doug Smythiesbf810222014-05-30 10:10:57 -0700569 int64_t core_pct;
570 int32_t rem;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800571
Doug Smythiesbf810222014-05-30 10:10:57 -0700572 core_pct = int_tofp(sample->aperf) * int_tofp(100);
573 core_pct = div_u64_rem(core_pct, int_tofp(sample->mperf), &rem);
574
575 if ((rem << 1) >= int_tofp(sample->mperf))
576 core_pct += 1;
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800577
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800578 sample->freq = fp_toint(
Dirk Brandewiee66c1762014-02-25 10:35:37 -0800579 mul_fp(int_tofp(cpu->pstate.max_pstate * 1000), core_pct));
Dirk Brandewiefcb6a152014-02-03 08:55:31 -0800580
Doug Smythiesbf810222014-05-30 10:10:57 -0700581 sample->core_pct_busy = (int32_t)core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800582}
583
584static inline void intel_pstate_sample(struct cpudata *cpu)
585{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800586 u64 aperf, mperf;
587
Dirk Brandewie93f08222013-02-06 09:02:13 -0800588 rdmsrl(MSR_IA32_APERF, aperf);
589 rdmsrl(MSR_IA32_MPERF, mperf);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800590
Dirk Brandewiee66c1762014-02-25 10:35:37 -0800591 aperf = aperf >> FRAC_BITS;
592 mperf = mperf >> FRAC_BITS;
Dirk Brandewiee66c1762014-02-25 10:35:37 -0800593
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700594 cpu->last_sample_time = cpu->sample.time;
595 cpu->sample.time = ktime_get();
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800596 cpu->sample.aperf = aperf;
597 cpu->sample.mperf = mperf;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800598 cpu->sample.aperf -= cpu->prev_aperf;
599 cpu->sample.mperf -= cpu->prev_mperf;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800600
Stratos Karafotis6b17ddb2014-04-29 20:53:49 +0300601 intel_pstate_calc_busy(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800602
Dirk Brandewie93f08222013-02-06 09:02:13 -0800603 cpu->prev_aperf = aperf;
604 cpu->prev_mperf = mperf;
605}
606
607static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
608{
609 int sample_time, delay;
610
Dirk Brandewie016c8152013-10-21 09:20:34 -0700611 sample_time = pid_params.sample_rate_ms;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800612 delay = msecs_to_jiffies(sample_time);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800613 mod_timer_pinned(&cpu->timer, jiffies + delay);
614}
615
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700616static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800617{
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700618 int32_t core_busy, max_pstate, current_pstate, sample_ratio;
619 u32 duration_us;
620 u32 sample_time;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800621
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800622 core_busy = cpu->sample.core_pct_busy;
Dirk Brandewie2134ed42013-07-18 08:48:42 -0700623 max_pstate = int_tofp(cpu->pstate.max_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800624 current_pstate = int_tofp(cpu->pstate.current_pstate);
Dirk Brandewiee66c1762014-02-25 10:35:37 -0800625 core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
Dirk Brandewiec4ee8412014-05-29 09:32:24 -0700626
627 sample_time = (pid_params.sample_rate_ms * USEC_PER_MSEC);
628 duration_us = (u32) ktime_us_delta(cpu->sample.time,
629 cpu->last_sample_time);
630 if (duration_us > sample_time * 3) {
631 sample_ratio = div_fp(int_tofp(sample_time),
632 int_tofp(duration_us));
633 core_busy = mul_fp(core_busy, sample_ratio);
634 }
635
Dirk Brandewief0fe3cd2014-05-29 09:32:23 -0700636 return core_busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800637}
638
639static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
640{
Brennan Shacklettd253d2a2013-10-21 09:20:32 -0700641 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800642 struct _pid *pid;
643 signed int ctl = 0;
644 int steps;
645
646 pid = &cpu->pid;
647 busy_scaled = intel_pstate_get_scaled_busy(cpu);
648
649 ctl = pid_calc(pid, busy_scaled);
650
651 steps = abs(ctl);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800652
Dirk Brandewie93f08222013-02-06 09:02:13 -0800653 if (ctl < 0)
654 intel_pstate_pstate_increase(cpu, steps);
655 else
656 intel_pstate_pstate_decrease(cpu, steps);
657}
658
Dirk Brandewie93f08222013-02-06 09:02:13 -0800659static void intel_pstate_timer_func(unsigned long __data)
660{
661 struct cpudata *cpu = (struct cpudata *) __data;
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800662 struct sample *sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800663
664 intel_pstate_sample(cpu);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800665
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800666 sample = &cpu->sample;
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800667
Dirk Brandewieca182ae2013-05-07 08:20:27 -0700668 intel_pstate_adjust_busy_pstate(cpu);
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800669
670 trace_pstate_sample(fp_toint(sample->core_pct_busy),
671 fp_toint(intel_pstate_get_scaled_busy(cpu)),
672 cpu->pstate.current_pstate,
673 sample->mperf,
674 sample->aperf,
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800675 sample->freq);
676
Dirk Brandewie93f08222013-02-06 09:02:13 -0800677 intel_pstate_set_sample_time(cpu);
678}
679
680#define ICPU(model, policy) \
Dirk Brandewie6cbd7ee2014-01-06 10:59:16 -0800681 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
682 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -0800683
684static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700685 ICPU(0x2a, core_params),
686 ICPU(0x2d, core_params),
Dirk Brandewie19e77c22013-10-21 09:20:35 -0700687 ICPU(0x37, byt_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -0700688 ICPU(0x3a, core_params),
689 ICPU(0x3c, core_params),
Dirk Brandewiec7e241d2014-05-08 12:57:27 -0700690 ICPU(0x3d, core_params),
Dirk Brandewie016c8152013-10-21 09:20:34 -0700691 ICPU(0x3e, core_params),
692 ICPU(0x3f, core_params),
693 ICPU(0x45, core_params),
694 ICPU(0x46, core_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;
716 cpu->timer.data =
717 (unsigned long)cpu;
718 cpu->timer.expires = jiffies + HZ/100;
719 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800720 intel_pstate_sample(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800721
722 add_timer_on(&cpu->timer, cpunum);
723
724 pr_info("Intel pstate controlling: cpu %d\n", cpunum);
725
726 return 0;
727}
728
729static unsigned int intel_pstate_get(unsigned int cpu_num)
730{
731 struct sample *sample;
732 struct cpudata *cpu;
733
734 cpu = all_cpu_data[cpu_num];
735 if (!cpu)
736 return 0;
Dirk Brandewied37e2b72014-02-12 10:01:04 -0800737 sample = &cpu->sample;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800738 return sample->freq;
739}
740
741static int intel_pstate_set_policy(struct cpufreq_policy *policy)
742{
743 struct cpudata *cpu;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800744
745 cpu = all_cpu_data[policy->cpu];
746
Dirk Brandewied3929b82013-03-05 14:15:26 -0800747 if (!policy->cpuinfo.max_freq)
748 return -ENODEV;
749
Dirk Brandewie93f08222013-02-06 09:02:13 -0800750 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
751 limits.min_perf_pct = 100;
752 limits.min_perf = int_tofp(1);
753 limits.max_perf_pct = 100;
754 limits.max_perf = int_tofp(1);
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700755 limits.no_turbo = limits.turbo_disabled;
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000756 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800757 }
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000758 limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
759 limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
760 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
761
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700762 limits.max_policy_pct = policy->max * 100 / policy->cpuinfo.max_freq;
763 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
764 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000765 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800766
767 return 0;
768}
769
770static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
771{
Viresh Kumarbe49e342013-10-02 14:13:19 +0530772 cpufreq_verify_within_cpu_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800773
774 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
775 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
776 return -EINVAL;
777
778 return 0;
779}
780
Dirk Brandewiebb180082014-03-19 08:45:54 -0700781static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800782{
Dirk Brandewiebb180082014-03-19 08:45:54 -0700783 int cpu_num = policy->cpu;
784 struct cpudata *cpu = all_cpu_data[cpu_num];
Dirk Brandewie93f08222013-02-06 09:02:13 -0800785
Dirk Brandewiebb180082014-03-19 08:45:54 -0700786 pr_info("intel_pstate CPU %d exiting\n", cpu_num);
787
Dirk Brandewiec2294a22014-03-24 07:41:29 -0700788 del_timer_sync(&all_cpu_data[cpu_num]->timer);
Dirk Brandewiebb180082014-03-19 08:45:54 -0700789 intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
790 kfree(all_cpu_data[cpu_num]);
791 all_cpu_data[cpu_num] = NULL;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800792}
793
Paul Gortmaker27609842013-06-19 13:54:04 -0400794static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800795{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800796 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -0700797 int rc;
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700798 u64 misc_en;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800799
800 rc = intel_pstate_init_cpu(policy->cpu);
801 if (rc)
802 return rc;
803
804 cpu = all_cpu_data[policy->cpu];
805
Dirk Brandewiedd5fbf72014-06-20 07:27:59 -0700806 rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
807 if (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
808 cpu->pstate.max_pstate == cpu->pstate.turbo_pstate) {
809 limits.turbo_disabled = 1;
810 limits.no_turbo = 1;
811 }
812 if (limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800813 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
814 else
815 policy->policy = CPUFREQ_POLICY_POWERSAVE;
816
Dirk Brandewie52e0a502013-10-15 11:06:14 -0700817 policy->min = cpu->pstate.min_pstate * 100000;
818 policy->max = cpu->pstate.turbo_pstate * 100000;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800819
820 /* cpuinfo and default policy values */
821 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
822 policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
823 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
824 cpumask_set_cpu(policy->cpu, policy->cpus);
825
826 return 0;
827}
828
829static struct cpufreq_driver intel_pstate_driver = {
830 .flags = CPUFREQ_CONST_LOOPS,
831 .verify = intel_pstate_verify_policy,
832 .setpolicy = intel_pstate_set_policy,
833 .get = intel_pstate_get,
834 .init = intel_pstate_cpu_init,
Dirk Brandewiebb180082014-03-19 08:45:54 -0700835 .stop_cpu = intel_pstate_stop_cpu,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800836 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -0800837};
838
Dirk Brandewie6be26492013-02-15 22:55:10 +0100839static int __initdata no_load;
840
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100841static int intel_pstate_msrs_not_valid(void)
842{
843 /* Check that all the msr's we are using are valid. */
844 u64 aperf, mperf, tmp;
845
846 rdmsrl(MSR_IA32_APERF, aperf);
847 rdmsrl(MSR_IA32_MPERF, mperf);
848
Dirk Brandewie016c8152013-10-21 09:20:34 -0700849 if (!pstate_funcs.get_max() ||
850 !pstate_funcs.get_min() ||
851 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100852 return -ENODEV;
853
854 rdmsrl(MSR_IA32_APERF, tmp);
855 if (!(tmp - aperf))
856 return -ENODEV;
857
858 rdmsrl(MSR_IA32_MPERF, tmp);
859 if (!(tmp - mperf))
860 return -ENODEV;
861
862 return 0;
863}
Dirk Brandewie016c8152013-10-21 09:20:34 -0700864
Dirk Brandewiee0a261a2013-10-30 08:38:32 -0700865static void copy_pid_params(struct pstate_adjust_policy *policy)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700866{
867 pid_params.sample_rate_ms = policy->sample_rate_ms;
868 pid_params.p_gain_pct = policy->p_gain_pct;
869 pid_params.i_gain_pct = policy->i_gain_pct;
870 pid_params.d_gain_pct = policy->d_gain_pct;
871 pid_params.deadband = policy->deadband;
872 pid_params.setpoint = policy->setpoint;
873}
874
Dirk Brandewiee0a261a2013-10-30 08:38:32 -0700875static void copy_cpu_funcs(struct pstate_funcs *funcs)
Dirk Brandewie016c8152013-10-21 09:20:34 -0700876{
877 pstate_funcs.get_max = funcs->get_max;
878 pstate_funcs.get_min = funcs->get_min;
879 pstate_funcs.get_turbo = funcs->get_turbo;
880 pstate_funcs.set = funcs->set;
Dirk Brandewie007bea02013-12-18 10:32:39 -0800881 pstate_funcs.get_vid = funcs->get_vid;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700882}
883
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800884#if IS_ENABLED(CONFIG_ACPI)
885#include <acpi/processor.h>
886
887static bool intel_pstate_no_acpi_pss(void)
888{
889 int i;
890
891 for_each_possible_cpu(i) {
892 acpi_status status;
893 union acpi_object *pss;
894 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
895 struct acpi_processor *pr = per_cpu(processors, i);
896
897 if (!pr)
898 continue;
899
900 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
901 if (ACPI_FAILURE(status))
902 continue;
903
904 pss = buffer.pointer;
905 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
906 kfree(pss);
907 return false;
908 }
909
910 kfree(pss);
911 }
912
913 return true;
914}
915
916struct hw_vendor_info {
917 u16 valid;
918 char oem_id[ACPI_OEM_ID_SIZE];
919 char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
920};
921
922/* Hardware vendor-specific info that has its own power management modes */
923static struct hw_vendor_info vendor_info[] = {
924 {1, "HP ", "ProLiant"},
925 {0, "", ""},
926};
927
928static bool intel_pstate_platform_pwr_mgmt_exists(void)
929{
930 struct acpi_table_header hdr;
931 struct hw_vendor_info *v_info;
932
933 if (acpi_disabled
934 || ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
935 return false;
936
937 for (v_info = vendor_info; v_info->valid; v_info++) {
938 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE)
939 && !strncmp(hdr.oem_table_id, v_info->oem_table_id, ACPI_OEM_TABLE_ID_SIZE)
940 && intel_pstate_no_acpi_pss())
941 return true;
942 }
943
944 return false;
945}
946#else /* CONFIG_ACPI not enabled */
947static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
948#endif /* CONFIG_ACPI */
949
Dirk Brandewie93f08222013-02-06 09:02:13 -0800950static int __init intel_pstate_init(void)
951{
Dirk Brandewie907cc902013-03-05 14:15:27 -0800952 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800953 const struct x86_cpu_id *id;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700954 struct cpu_defaults *cpu_info;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800955
Dirk Brandewie6be26492013-02-15 22:55:10 +0100956 if (no_load)
957 return -ENODEV;
958
Dirk Brandewie93f08222013-02-06 09:02:13 -0800959 id = x86_match_cpu(intel_pstate_cpu_ids);
960 if (!id)
961 return -ENODEV;
962
Adrian Huangfbbcdc02013-10-31 23:24:05 +0800963 /*
964 * The Intel pstate driver will be ignored if the platform
965 * firmware has its own power management modes.
966 */
967 if (intel_pstate_platform_pwr_mgmt_exists())
968 return -ENODEV;
969
Dirk Brandewie016c8152013-10-21 09:20:34 -0700970 cpu_info = (struct cpu_defaults *)id->driver_data;
971
972 copy_pid_params(&cpu_info->pid_policy);
973 copy_cpu_funcs(&cpu_info->funcs);
974
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100975 if (intel_pstate_msrs_not_valid())
976 return -ENODEV;
977
Dirk Brandewie93f08222013-02-06 09:02:13 -0800978 pr_info("Intel P-state driver initializing.\n");
979
Wei Yongjunb57ffac2013-05-13 08:03:43 +0000980 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -0800981 if (!all_cpu_data)
982 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800983
984 rc = cpufreq_register_driver(&intel_pstate_driver);
985 if (rc)
986 goto out;
987
988 intel_pstate_debug_expose_params();
989 intel_pstate_sysfs_expose_params();
Dirk Brandewieb69880f2014-01-16 10:32:25 -0800990
Dirk Brandewie93f08222013-02-06 09:02:13 -0800991 return rc;
992out:
Dirk Brandewie907cc902013-03-05 14:15:27 -0800993 get_online_cpus();
994 for_each_online_cpu(cpu) {
995 if (all_cpu_data[cpu]) {
996 del_timer_sync(&all_cpu_data[cpu]->timer);
997 kfree(all_cpu_data[cpu]);
998 }
999 }
1000
1001 put_online_cpus();
1002 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -08001003 return -ENODEV;
1004}
1005device_initcall(intel_pstate_init);
1006
Dirk Brandewie6be26492013-02-15 22:55:10 +01001007static int __init intel_pstate_setup(char *str)
1008{
1009 if (!str)
1010 return -EINVAL;
1011
1012 if (!strcmp(str, "disable"))
1013 no_load = 1;
1014 return 0;
1015}
1016early_param("intel_pstate", intel_pstate_setup);
1017
Dirk Brandewie93f08222013-02-06 09:02:13 -08001018MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
1019MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
1020MODULE_LICENSE("GPL");