blob: decf84e7194371a420a6aedacc5beb251850ed0d [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>
28#include <trace/events/power.h>
29
30#include <asm/div64.h>
31#include <asm/msr.h>
32#include <asm/cpu_device_id.h>
33
34#define SAMPLE_COUNT 3
35
36#define FRAC_BITS 8
37#define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
38#define fp_toint(X) ((X) >> FRAC_BITS)
39
40static inline int32_t mul_fp(int32_t x, int32_t y)
41{
42 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
43}
44
45static inline int32_t div_fp(int32_t x, int32_t y)
46{
47 return div_s64((int64_t)x << FRAC_BITS, (int64_t)y);
48}
49
50struct sample {
Brennan Shacklette34ce302013-10-21 09:20:32 -070051 int32_t core_pct_busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -080052 u64 aperf;
53 u64 mperf;
54 int freq;
55};
56
57struct pstate_data {
58 int current_pstate;
59 int min_pstate;
60 int max_pstate;
61 int turbo_pstate;
62};
63
64struct _pid {
65 int setpoint;
66 int32_t integral;
67 int32_t p_gain;
68 int32_t i_gain;
69 int32_t d_gain;
70 int deadband;
Brennan Shacklette34ce302013-10-21 09:20:32 -070071 int32_t last_err;
Dirk Brandewie93f08222013-02-06 09:02:13 -080072};
73
74struct cpudata {
75 int cpu;
76
77 char name[64];
78
79 struct timer_list timer;
80
81 struct pstate_adjust_policy *pstate_policy;
82 struct pstate_data pstate;
83 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -080084
85 int min_pstate_count;
Dirk Brandewie93f08222013-02-06 09:02:13 -080086
Dirk Brandewie93f08222013-02-06 09:02:13 -080087 u64 prev_aperf;
88 u64 prev_mperf;
89 int sample_ptr;
90 struct sample samples[SAMPLE_COUNT];
91};
92
93static struct cpudata **all_cpu_data;
94struct pstate_adjust_policy {
95 int sample_rate_ms;
96 int deadband;
97 int setpoint;
98 int p_gain_pct;
99 int d_gain_pct;
100 int i_gain_pct;
101};
102
103static struct pstate_adjust_policy default_policy = {
104 .sample_rate_ms = 10,
105 .deadband = 0,
Dirk Brandewiecb631ac2013-07-18 08:48:42 -0700106 .setpoint = 97,
107 .p_gain_pct = 20,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800108 .d_gain_pct = 0,
Dirk Brandewiecb631ac2013-07-18 08:48:42 -0700109 .i_gain_pct = 0,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800110};
111
112struct perf_limits {
113 int no_turbo;
114 int max_perf_pct;
115 int min_perf_pct;
116 int32_t max_perf;
117 int32_t min_perf;
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700118 int max_policy_pct;
119 int max_sysfs_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800120};
121
122static struct perf_limits limits = {
123 .no_turbo = 0,
124 .max_perf_pct = 100,
125 .max_perf = int_tofp(1),
126 .min_perf_pct = 0,
127 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700128 .max_policy_pct = 100,
129 .max_sysfs_pct = 100,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800130};
131
132static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
133 int deadband, int integral) {
134 pid->setpoint = setpoint;
135 pid->deadband = deadband;
136 pid->integral = int_tofp(integral);
137 pid->last_err = setpoint - busy;
138}
139
140static inline void pid_p_gain_set(struct _pid *pid, int percent)
141{
142 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
143}
144
145static inline void pid_i_gain_set(struct _pid *pid, int percent)
146{
147 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
148}
149
150static inline void pid_d_gain_set(struct _pid *pid, int percent)
151{
152
153 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
154}
155
Brennan Shacklette34ce302013-10-21 09:20:32 -0700156static signed int pid_calc(struct _pid *pid, int32_t busy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800157{
Brennan Shacklette34ce302013-10-21 09:20:32 -0700158 signed int result;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800159 int32_t pterm, dterm, fp_error;
160 int32_t integral_limit;
161
Brennan Shacklette34ce302013-10-21 09:20:32 -0700162 fp_error = int_tofp(pid->setpoint) - busy;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800163
Brennan Shacklette34ce302013-10-21 09:20:32 -0700164 if (abs(fp_error) <= int_tofp(pid->deadband))
Dirk Brandewie93f08222013-02-06 09:02:13 -0800165 return 0;
166
167 pterm = mul_fp(pid->p_gain, fp_error);
168
169 pid->integral += fp_error;
170
171 /* limit the integral term */
172 integral_limit = int_tofp(30);
173 if (pid->integral > integral_limit)
174 pid->integral = integral_limit;
175 if (pid->integral < -integral_limit)
176 pid->integral = -integral_limit;
177
Brennan Shacklette34ce302013-10-21 09:20:32 -0700178 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
179 pid->last_err = fp_error;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800180
181 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
182
183 return (signed int)fp_toint(result);
184}
185
186static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
187{
188 pid_p_gain_set(&cpu->pid, cpu->pstate_policy->p_gain_pct);
189 pid_d_gain_set(&cpu->pid, cpu->pstate_policy->d_gain_pct);
190 pid_i_gain_set(&cpu->pid, cpu->pstate_policy->i_gain_pct);
191
192 pid_reset(&cpu->pid,
193 cpu->pstate_policy->setpoint,
194 100,
195 cpu->pstate_policy->deadband,
196 0);
197}
198
Dirk Brandewie93f08222013-02-06 09:02:13 -0800199static inline void intel_pstate_reset_all_pid(void)
200{
201 unsigned int cpu;
202 for_each_online_cpu(cpu) {
203 if (all_cpu_data[cpu])
204 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
205 }
206}
207
208/************************** debugfs begin ************************/
209static int pid_param_set(void *data, u64 val)
210{
211 *(u32 *)data = val;
212 intel_pstate_reset_all_pid();
213 return 0;
214}
215static int pid_param_get(void *data, u64 *val)
216{
217 *val = *(u32 *)data;
218 return 0;
219}
220DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get,
221 pid_param_set, "%llu\n");
222
223struct pid_param {
224 char *name;
225 void *value;
226};
227
228static struct pid_param pid_files[] = {
229 {"sample_rate_ms", &default_policy.sample_rate_ms},
230 {"d_gain_pct", &default_policy.d_gain_pct},
231 {"i_gain_pct", &default_policy.i_gain_pct},
232 {"deadband", &default_policy.deadband},
233 {"setpoint", &default_policy.setpoint},
234 {"p_gain_pct", &default_policy.p_gain_pct},
235 {NULL, NULL}
236};
237
238static struct dentry *debugfs_parent;
239static void intel_pstate_debug_expose_params(void)
240{
241 int i = 0;
242
243 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
244 if (IS_ERR_OR_NULL(debugfs_parent))
245 return;
246 while (pid_files[i].name) {
247 debugfs_create_file(pid_files[i].name, 0660,
248 debugfs_parent, pid_files[i].value,
249 &fops_pid_param);
250 i++;
251 }
252}
253
254/************************** debugfs end ************************/
255
256/************************** sysfs begin ************************/
257#define show_one(file_name, object) \
258 static ssize_t show_##file_name \
259 (struct kobject *kobj, struct attribute *attr, char *buf) \
260 { \
261 return sprintf(buf, "%u\n", limits.object); \
262 }
263
264static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
265 const char *buf, size_t count)
266{
267 unsigned int input;
268 int ret;
269 ret = sscanf(buf, "%u", &input);
270 if (ret != 1)
271 return -EINVAL;
272 limits.no_turbo = clamp_t(int, input, 0 , 1);
273
274 return count;
275}
276
277static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
278 const char *buf, size_t count)
279{
280 unsigned int input;
281 int ret;
282 ret = sscanf(buf, "%u", &input);
283 if (ret != 1)
284 return -EINVAL;
285
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700286 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
287 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800288 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
289 return count;
290}
291
292static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
293 const char *buf, size_t count)
294{
295 unsigned int input;
296 int ret;
297 ret = sscanf(buf, "%u", &input);
298 if (ret != 1)
299 return -EINVAL;
300 limits.min_perf_pct = clamp_t(int, input, 0 , 100);
301 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
302
303 return count;
304}
305
306show_one(no_turbo, no_turbo);
307show_one(max_perf_pct, max_perf_pct);
308show_one(min_perf_pct, min_perf_pct);
309
310define_one_global_rw(no_turbo);
311define_one_global_rw(max_perf_pct);
312define_one_global_rw(min_perf_pct);
313
314static struct attribute *intel_pstate_attributes[] = {
315 &no_turbo.attr,
316 &max_perf_pct.attr,
317 &min_perf_pct.attr,
318 NULL
319};
320
321static struct attribute_group intel_pstate_attr_group = {
322 .attrs = intel_pstate_attributes,
323};
324static struct kobject *intel_pstate_kobject;
325
326static void intel_pstate_sysfs_expose_params(void)
327{
328 int rc;
329
330 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
331 &cpu_subsys.dev_root->kobj);
332 BUG_ON(!intel_pstate_kobject);
333 rc = sysfs_create_group(intel_pstate_kobject,
334 &intel_pstate_attr_group);
335 BUG_ON(rc);
336}
337
338/************************** sysfs end ************************/
339
340static int intel_pstate_min_pstate(void)
341{
342 u64 value;
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000343 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800344 return (value >> 40) & 0xFF;
345}
346
347static int intel_pstate_max_pstate(void)
348{
349 u64 value;
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000350 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800351 return (value >> 8) & 0xFF;
352}
353
354static int intel_pstate_turbo_pstate(void)
355{
356 u64 value;
357 int nont, ret;
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000358 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800359 nont = intel_pstate_max_pstate();
360 ret = ((value) & 255);
361 if (ret <= nont)
362 ret = nont;
363 return ret;
364}
365
366static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
367{
368 int max_perf = cpu->pstate.turbo_pstate;
Dirk Brandewie0df520d2013-10-21 09:20:33 -0700369 int max_perf_adj;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800370 int min_perf;
371 if (limits.no_turbo)
372 max_perf = cpu->pstate.max_pstate;
373
Dirk Brandewie0df520d2013-10-21 09:20:33 -0700374 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
375 *max = clamp_t(int, max_perf_adj,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800376 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
377
378 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
379 *min = clamp_t(int, min_perf,
380 cpu->pstate.min_pstate, max_perf);
381}
382
383static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
384{
385 int max_perf, min_perf;
386
387 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
388
389 pstate = clamp_t(int, pstate, min_perf, max_perf);
390
391 if (pstate == cpu->pstate.current_pstate)
392 return;
393
Dirk Brandewie93f08222013-02-06 09:02:13 -0800394 trace_cpu_frequency(pstate * 100000, cpu->cpu);
Dirk Brandewie35363e92013-05-07 08:20:30 -0700395
Dirk Brandewie93f08222013-02-06 09:02:13 -0800396 cpu->pstate.current_pstate = pstate;
Srinivas Pandruvada3dc642a2013-10-01 10:28:41 -0700397 if (limits.no_turbo)
398 wrmsrl(MSR_IA32_PERF_CTL, BIT(32) | (pstate << 8));
399 else
400 wrmsrl(MSR_IA32_PERF_CTL, pstate << 8);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800401
402}
403
404static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps)
405{
406 int target;
407 target = cpu->pstate.current_pstate + steps;
408
409 intel_pstate_set_pstate(cpu, target);
410}
411
412static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps)
413{
414 int target;
415 target = cpu->pstate.current_pstate - steps;
416 intel_pstate_set_pstate(cpu, target);
417}
418
419static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
420{
421 sprintf(cpu->name, "Intel 2nd generation core");
422
423 cpu->pstate.min_pstate = intel_pstate_min_pstate();
424 cpu->pstate.max_pstate = intel_pstate_max_pstate();
425 cpu->pstate.turbo_pstate = intel_pstate_turbo_pstate();
426
427 /*
428 * goto max pstate so we don't slow up boot if we are built-in if we are
429 * a module we will take care of it during normal operation
430 */
431 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
432}
433
434static inline void intel_pstate_calc_busy(struct cpudata *cpu,
435 struct sample *sample)
436{
437 u64 core_pct;
Brennan Shacklette34ce302013-10-21 09:20:32 -0700438 core_pct = div64_u64(int_tofp(sample->aperf * 100),
439 sample->mperf);
440 sample->freq = fp_toint(cpu->pstate.max_pstate * core_pct * 1000);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800441
Dirk Brandewie1abc4b22013-05-07 08:20:25 -0700442 sample->core_pct_busy = core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800443}
444
445static inline void intel_pstate_sample(struct cpudata *cpu)
446{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800447 u64 aperf, mperf;
448
Dirk Brandewie93f08222013-02-06 09:02:13 -0800449 rdmsrl(MSR_IA32_APERF, aperf);
450 rdmsrl(MSR_IA32_MPERF, mperf);
Dirk Brandewie1abc4b22013-05-07 08:20:25 -0700451 cpu->sample_ptr = (cpu->sample_ptr + 1) % SAMPLE_COUNT;
452 cpu->samples[cpu->sample_ptr].aperf = aperf;
453 cpu->samples[cpu->sample_ptr].mperf = mperf;
454 cpu->samples[cpu->sample_ptr].aperf -= cpu->prev_aperf;
455 cpu->samples[cpu->sample_ptr].mperf -= cpu->prev_mperf;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800456
Dirk Brandewie1abc4b22013-05-07 08:20:25 -0700457 intel_pstate_calc_busy(cpu, &cpu->samples[cpu->sample_ptr]);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800458
Dirk Brandewie93f08222013-02-06 09:02:13 -0800459 cpu->prev_aperf = aperf;
460 cpu->prev_mperf = mperf;
461}
462
463static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
464{
465 int sample_time, delay;
466
467 sample_time = cpu->pstate_policy->sample_rate_ms;
468 delay = msecs_to_jiffies(sample_time);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800469 mod_timer_pinned(&cpu->timer, jiffies + delay);
470}
471
Brennan Shacklette34ce302013-10-21 09:20:32 -0700472static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800473{
Dirk Brandewiecb631ac2013-07-18 08:48:42 -0700474 int32_t core_busy, max_pstate, current_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800475
Brennan Shacklette34ce302013-10-21 09:20:32 -0700476 core_busy = cpu->samples[cpu->sample_ptr].core_pct_busy;
Dirk Brandewiecb631ac2013-07-18 08:48:42 -0700477 max_pstate = int_tofp(cpu->pstate.max_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800478 current_pstate = int_tofp(cpu->pstate.current_pstate);
Brennan Shacklette34ce302013-10-21 09:20:32 -0700479 return mul_fp(core_busy, div_fp(max_pstate, current_pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800480}
481
482static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
483{
Brennan Shacklette34ce302013-10-21 09:20:32 -0700484 int32_t busy_scaled;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800485 struct _pid *pid;
486 signed int ctl = 0;
487 int steps;
488
489 pid = &cpu->pid;
490 busy_scaled = intel_pstate_get_scaled_busy(cpu);
491
492 ctl = pid_calc(pid, busy_scaled);
493
494 steps = abs(ctl);
495 if (ctl < 0)
496 intel_pstate_pstate_increase(cpu, steps);
497 else
498 intel_pstate_pstate_decrease(cpu, steps);
499}
500
Dirk Brandewie93f08222013-02-06 09:02:13 -0800501static void intel_pstate_timer_func(unsigned long __data)
502{
503 struct cpudata *cpu = (struct cpudata *) __data;
504
505 intel_pstate_sample(cpu);
Dirk Brandewieca182ae2013-05-07 08:20:27 -0700506 intel_pstate_adjust_busy_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800507
Dirk Brandewie93f08222013-02-06 09:02:13 -0800508 if (cpu->pstate.current_pstate == cpu->pstate.min_pstate) {
509 cpu->min_pstate_count++;
510 if (!(cpu->min_pstate_count % 5)) {
511 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800512 }
513 } else
514 cpu->min_pstate_count = 0;
Dirk Brandewieca182ae2013-05-07 08:20:27 -0700515
Dirk Brandewie93f08222013-02-06 09:02:13 -0800516 intel_pstate_set_sample_time(cpu);
517}
518
519#define ICPU(model, policy) \
Dirk Brandewied0ccf8a2014-01-06 10:59:16 -0800520 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
521 (unsigned long)&policy }
Dirk Brandewie93f08222013-02-06 09:02:13 -0800522
523static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
524 ICPU(0x2a, default_policy),
525 ICPU(0x2d, default_policy),
Dirk Brandewiec96d53d2013-05-17 16:10:24 +0000526 ICPU(0x3a, default_policy),
Nell Hardcastle0b977de2013-06-30 15:58:57 -0700527 ICPU(0x3c, default_policy),
528 ICPU(0x3e, default_policy),
529 ICPU(0x3f, default_policy),
530 ICPU(0x45, default_policy),
531 ICPU(0x46, default_policy),
Dirk Brandewie93f08222013-02-06 09:02:13 -0800532 {}
533};
534MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
535
536static int intel_pstate_init_cpu(unsigned int cpunum)
537{
538
539 const struct x86_cpu_id *id;
540 struct cpudata *cpu;
541
542 id = x86_match_cpu(intel_pstate_cpu_ids);
543 if (!id)
544 return -ENODEV;
545
546 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
547 if (!all_cpu_data[cpunum])
548 return -ENOMEM;
549
550 cpu = all_cpu_data[cpunum];
551
552 intel_pstate_get_cpu_pstates(cpu);
Rafael J. Wysockiec84b712013-12-31 13:37:46 +0100553 if (!cpu->pstate.current_pstate) {
554 all_cpu_data[cpunum] = NULL;
555 kfree(cpu);
556 return -ENODATA;
557 }
Dirk Brandewie93f08222013-02-06 09:02:13 -0800558
559 cpu->cpu = cpunum;
560 cpu->pstate_policy =
561 (struct pstate_adjust_policy *)id->driver_data;
562 init_timer_deferrable(&cpu->timer);
563 cpu->timer.function = intel_pstate_timer_func;
564 cpu->timer.data =
565 (unsigned long)cpu;
566 cpu->timer.expires = jiffies + HZ/100;
567 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800568 intel_pstate_sample(cpu);
569 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
570
571 add_timer_on(&cpu->timer, cpunum);
572
573 pr_info("Intel pstate controlling: cpu %d\n", cpunum);
574
575 return 0;
576}
577
578static unsigned int intel_pstate_get(unsigned int cpu_num)
579{
580 struct sample *sample;
581 struct cpudata *cpu;
582
583 cpu = all_cpu_data[cpu_num];
584 if (!cpu)
585 return 0;
586 sample = &cpu->samples[cpu->sample_ptr];
587 return sample->freq;
588}
589
590static int intel_pstate_set_policy(struct cpufreq_policy *policy)
591{
592 struct cpudata *cpu;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800593
594 cpu = all_cpu_data[policy->cpu];
595
Dirk Brandewied3929b82013-03-05 14:15:26 -0800596 if (!policy->cpuinfo.max_freq)
597 return -ENODEV;
598
Dirk Brandewie93f08222013-02-06 09:02:13 -0800599 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
600 limits.min_perf_pct = 100;
601 limits.min_perf = int_tofp(1);
Pali Rohár4bf70f92014-10-16 01:16:51 +0200602 limits.max_policy_pct = 100;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800603 limits.max_perf_pct = 100;
604 limits.max_perf = int_tofp(1);
605 limits.no_turbo = 0;
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000606 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800607 }
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000608 limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
609 limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
610 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
611
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700612 limits.max_policy_pct = policy->max * 100 / policy->cpuinfo.max_freq;
613 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
614 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000615 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800616
617 return 0;
618}
619
620static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
621{
622 cpufreq_verify_within_limits(policy,
623 policy->cpuinfo.min_freq,
624 policy->cpuinfo.max_freq);
625
626 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
627 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
628 return -EINVAL;
629
630 return 0;
631}
632
633static int __cpuinit intel_pstate_cpu_exit(struct cpufreq_policy *policy)
634{
635 int cpu = policy->cpu;
636
637 del_timer(&all_cpu_data[cpu]->timer);
638 kfree(all_cpu_data[cpu]);
639 all_cpu_data[cpu] = NULL;
640 return 0;
641}
642
643static int __cpuinit intel_pstate_cpu_init(struct cpufreq_policy *policy)
644{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800645 struct cpudata *cpu;
Dirk Brandewief606b352013-10-15 11:06:14 -0700646 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800647
648 rc = intel_pstate_init_cpu(policy->cpu);
649 if (rc)
650 return rc;
651
652 cpu = all_cpu_data[policy->cpu];
653
654 if (!limits.no_turbo &&
655 limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
656 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
657 else
658 policy->policy = CPUFREQ_POLICY_POWERSAVE;
659
Dirk Brandewief606b352013-10-15 11:06:14 -0700660 policy->min = cpu->pstate.min_pstate * 100000;
661 policy->max = cpu->pstate.turbo_pstate * 100000;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800662
663 /* cpuinfo and default policy values */
664 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
665 policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
666 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
667 cpumask_set_cpu(policy->cpu, policy->cpus);
668
669 return 0;
670}
671
672static struct cpufreq_driver intel_pstate_driver = {
673 .flags = CPUFREQ_CONST_LOOPS,
674 .verify = intel_pstate_verify_policy,
675 .setpolicy = intel_pstate_set_policy,
676 .get = intel_pstate_get,
677 .init = intel_pstate_cpu_init,
678 .exit = intel_pstate_cpu_exit,
679 .name = "intel_pstate",
680 .owner = THIS_MODULE,
681};
682
Dirk Brandewie6be26492013-02-15 22:55:10 +0100683static int __initdata no_load;
684
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100685static int intel_pstate_msrs_not_valid(void)
686{
687 /* Check that all the msr's we are using are valid. */
688 u64 aperf, mperf, tmp;
689
690 rdmsrl(MSR_IA32_APERF, aperf);
691 rdmsrl(MSR_IA32_MPERF, mperf);
692
693 if (!intel_pstate_min_pstate() ||
694 !intel_pstate_max_pstate() ||
695 !intel_pstate_turbo_pstate())
696 return -ENODEV;
697
698 rdmsrl(MSR_IA32_APERF, tmp);
699 if (!(tmp - aperf))
700 return -ENODEV;
701
702 rdmsrl(MSR_IA32_MPERF, tmp);
703 if (!(tmp - mperf))
704 return -ENODEV;
705
706 return 0;
707}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800708static int __init intel_pstate_init(void)
709{
Dirk Brandewie907cc902013-03-05 14:15:27 -0800710 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800711 const struct x86_cpu_id *id;
712
Dirk Brandewie6be26492013-02-15 22:55:10 +0100713 if (no_load)
714 return -ENODEV;
715
Dirk Brandewie93f08222013-02-06 09:02:13 -0800716 id = x86_match_cpu(intel_pstate_cpu_ids);
717 if (!id)
718 return -ENODEV;
719
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100720 if (intel_pstate_msrs_not_valid())
721 return -ENODEV;
722
Dirk Brandewie93f08222013-02-06 09:02:13 -0800723 pr_info("Intel P-state driver initializing.\n");
724
Wei Yongjunb57ffac2013-05-13 08:03:43 +0000725 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -0800726 if (!all_cpu_data)
727 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800728
729 rc = cpufreq_register_driver(&intel_pstate_driver);
730 if (rc)
731 goto out;
732
733 intel_pstate_debug_expose_params();
734 intel_pstate_sysfs_expose_params();
735 return rc;
736out:
Dirk Brandewie907cc902013-03-05 14:15:27 -0800737 get_online_cpus();
738 for_each_online_cpu(cpu) {
739 if (all_cpu_data[cpu]) {
740 del_timer_sync(&all_cpu_data[cpu]->timer);
741 kfree(all_cpu_data[cpu]);
742 }
743 }
744
745 put_online_cpus();
746 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800747 return -ENODEV;
748}
749device_initcall(intel_pstate_init);
750
Dirk Brandewie6be26492013-02-15 22:55:10 +0100751static int __init intel_pstate_setup(char *str)
752{
753 if (!str)
754 return -EINVAL;
755
756 if (!strcmp(str, "disable"))
757 no_load = 1;
758 return 0;
759}
760early_param("intel_pstate", intel_pstate_setup);
761
Dirk Brandewie93f08222013-02-06 09:02:13 -0800762MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
763MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
764MODULE_LICENSE("GPL");