blob: 6b37c9ff1d64bb6d39ab91c6e0b5377f7f48a8e5 [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 {
Dirk Brandewie93f08222013-02-06 09:02:13 -080051 int 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;
71 int last_err;
72};
73
74struct cpudata {
75 int cpu;
76
77 char name[64];
78
79 struct timer_list timer;
80
Dirk Brandewie93f08222013-02-06 09:02:13 -080081 struct pstate_data pstate;
82 struct _pid pid;
Dirk Brandewie93f08222013-02-06 09:02:13 -080083
84 int min_pstate_count;
Dirk Brandewie93f08222013-02-06 09:02:13 -080085
Dirk Brandewie93f08222013-02-06 09:02:13 -080086 u64 prev_aperf;
87 u64 prev_mperf;
88 int sample_ptr;
89 struct sample samples[SAMPLE_COUNT];
90};
91
92static struct cpudata **all_cpu_data;
93struct pstate_adjust_policy {
94 int sample_rate_ms;
95 int deadband;
96 int setpoint;
97 int p_gain_pct;
98 int d_gain_pct;
99 int i_gain_pct;
100};
101
Dirk Brandewie016c8152013-10-21 09:20:34 -0700102struct pstate_funcs {
103 int (*get_max)(void);
104 int (*get_min)(void);
105 int (*get_turbo)(void);
106 void (*set)(int pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800107};
108
Dirk Brandewie016c8152013-10-21 09:20:34 -0700109struct cpu_defaults {
110 struct pstate_adjust_policy pid_policy;
111 struct pstate_funcs funcs;
112};
113
114static struct pstate_adjust_policy pid_params;
115static struct pstate_funcs pstate_funcs;
116
Dirk Brandewie93f08222013-02-06 09:02:13 -0800117struct perf_limits {
118 int no_turbo;
119 int max_perf_pct;
120 int min_perf_pct;
121 int32_t max_perf;
122 int32_t min_perf;
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700123 int max_policy_pct;
124 int max_sysfs_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800125};
126
127static struct perf_limits limits = {
128 .no_turbo = 0,
129 .max_perf_pct = 100,
130 .max_perf = int_tofp(1),
131 .min_perf_pct = 0,
132 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700133 .max_policy_pct = 100,
134 .max_sysfs_pct = 100,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800135};
136
137static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
138 int deadband, int integral) {
139 pid->setpoint = setpoint;
140 pid->deadband = deadband;
141 pid->integral = int_tofp(integral);
142 pid->last_err = setpoint - busy;
143}
144
145static inline void pid_p_gain_set(struct _pid *pid, int percent)
146{
147 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
148}
149
150static inline void pid_i_gain_set(struct _pid *pid, int percent)
151{
152 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
153}
154
155static inline void pid_d_gain_set(struct _pid *pid, int percent)
156{
157
158 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
159}
160
161static signed int pid_calc(struct _pid *pid, int busy)
162{
163 signed int err, result;
164 int32_t pterm, dterm, fp_error;
165 int32_t integral_limit;
166
167 err = pid->setpoint - busy;
168 fp_error = int_tofp(err);
169
170 if (abs(err) <= pid->deadband)
171 return 0;
172
173 pterm = mul_fp(pid->p_gain, fp_error);
174
175 pid->integral += fp_error;
176
177 /* limit the integral term */
178 integral_limit = int_tofp(30);
179 if (pid->integral > integral_limit)
180 pid->integral = integral_limit;
181 if (pid->integral < -integral_limit)
182 pid->integral = -integral_limit;
183
184 dterm = mul_fp(pid->d_gain, (err - pid->last_err));
185 pid->last_err = err;
186
187 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
188
189 return (signed int)fp_toint(result);
190}
191
192static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
193{
Dirk Brandewie016c8152013-10-21 09:20:34 -0700194 pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
195 pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
196 pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800197
198 pid_reset(&cpu->pid,
Dirk Brandewie016c8152013-10-21 09:20:34 -0700199 pid_params.setpoint,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800200 100,
Dirk Brandewie016c8152013-10-21 09:20:34 -0700201 pid_params.deadband,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800202 0);
203}
204
Dirk Brandewie93f08222013-02-06 09:02:13 -0800205static inline void intel_pstate_reset_all_pid(void)
206{
207 unsigned int cpu;
208 for_each_online_cpu(cpu) {
209 if (all_cpu_data[cpu])
210 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
211 }
212}
213
214/************************** debugfs begin ************************/
215static int pid_param_set(void *data, u64 val)
216{
217 *(u32 *)data = val;
218 intel_pstate_reset_all_pid();
219 return 0;
220}
221static int pid_param_get(void *data, u64 *val)
222{
223 *val = *(u32 *)data;
224 return 0;
225}
226DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get,
227 pid_param_set, "%llu\n");
228
229struct pid_param {
230 char *name;
231 void *value;
232};
233
234static struct pid_param pid_files[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700235 {"sample_rate_ms", &pid_params.sample_rate_ms},
236 {"d_gain_pct", &pid_params.d_gain_pct},
237 {"i_gain_pct", &pid_params.i_gain_pct},
238 {"deadband", &pid_params.deadband},
239 {"setpoint", &pid_params.setpoint},
240 {"p_gain_pct", &pid_params.p_gain_pct},
Dirk Brandewie93f08222013-02-06 09:02:13 -0800241 {NULL, NULL}
242};
243
244static struct dentry *debugfs_parent;
245static void intel_pstate_debug_expose_params(void)
246{
247 int i = 0;
248
249 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
250 if (IS_ERR_OR_NULL(debugfs_parent))
251 return;
252 while (pid_files[i].name) {
253 debugfs_create_file(pid_files[i].name, 0660,
254 debugfs_parent, pid_files[i].value,
255 &fops_pid_param);
256 i++;
257 }
258}
259
260/************************** debugfs end ************************/
261
262/************************** sysfs begin ************************/
263#define show_one(file_name, object) \
264 static ssize_t show_##file_name \
265 (struct kobject *kobj, struct attribute *attr, char *buf) \
266 { \
267 return sprintf(buf, "%u\n", limits.object); \
268 }
269
270static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
271 const char *buf, size_t count)
272{
273 unsigned int input;
274 int ret;
275 ret = sscanf(buf, "%u", &input);
276 if (ret != 1)
277 return -EINVAL;
278 limits.no_turbo = clamp_t(int, input, 0 , 1);
279
280 return count;
281}
282
283static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
284 const char *buf, size_t count)
285{
286 unsigned int input;
287 int ret;
288 ret = sscanf(buf, "%u", &input);
289 if (ret != 1)
290 return -EINVAL;
291
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700292 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
293 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800294 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
295 return count;
296}
297
298static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
299 const char *buf, size_t count)
300{
301 unsigned int input;
302 int ret;
303 ret = sscanf(buf, "%u", &input);
304 if (ret != 1)
305 return -EINVAL;
306 limits.min_perf_pct = clamp_t(int, input, 0 , 100);
307 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
308
309 return count;
310}
311
312show_one(no_turbo, no_turbo);
313show_one(max_perf_pct, max_perf_pct);
314show_one(min_perf_pct, min_perf_pct);
315
316define_one_global_rw(no_turbo);
317define_one_global_rw(max_perf_pct);
318define_one_global_rw(min_perf_pct);
319
320static struct attribute *intel_pstate_attributes[] = {
321 &no_turbo.attr,
322 &max_perf_pct.attr,
323 &min_perf_pct.attr,
324 NULL
325};
326
327static struct attribute_group intel_pstate_attr_group = {
328 .attrs = intel_pstate_attributes,
329};
330static struct kobject *intel_pstate_kobject;
331
332static void intel_pstate_sysfs_expose_params(void)
333{
334 int rc;
335
336 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
337 &cpu_subsys.dev_root->kobj);
338 BUG_ON(!intel_pstate_kobject);
339 rc = sysfs_create_group(intel_pstate_kobject,
340 &intel_pstate_attr_group);
341 BUG_ON(rc);
342}
343
344/************************** sysfs end ************************/
Dirk Brandewie016c8152013-10-21 09:20:34 -0700345static int core_get_min_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800346{
347 u64 value;
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000348 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800349 return (value >> 40) & 0xFF;
350}
351
Dirk Brandewie016c8152013-10-21 09:20:34 -0700352static int core_get_max_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800353{
354 u64 value;
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000355 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800356 return (value >> 8) & 0xFF;
357}
358
Dirk Brandewie016c8152013-10-21 09:20:34 -0700359static int core_get_turbo_pstate(void)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800360{
361 u64 value;
362 int nont, ret;
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000363 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
Dirk Brandewie016c8152013-10-21 09:20:34 -0700364 nont = core_get_max_pstate();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800365 ret = ((value) & 255);
366 if (ret <= nont)
367 ret = nont;
368 return ret;
369}
370
Dirk Brandewie016c8152013-10-21 09:20:34 -0700371static void core_set_pstate(int pstate)
372{
373 u64 val;
374
375 val = pstate << 8;
376 if (limits.no_turbo)
377 val |= (u64)1 << 32;
378
379 wrmsrl(MSR_IA32_PERF_CTL, val);
380}
381
382static struct cpu_defaults core_params = {
383 .pid_policy = {
384 .sample_rate_ms = 10,
385 .deadband = 0,
386 .setpoint = 97,
387 .p_gain_pct = 20,
388 .d_gain_pct = 0,
389 .i_gain_pct = 0,
390 },
391 .funcs = {
392 .get_max = core_get_max_pstate,
393 .get_min = core_get_min_pstate,
394 .get_turbo = core_get_turbo_pstate,
395 .set = core_set_pstate,
396 },
397};
398
Dirk Brandewie93f08222013-02-06 09:02:13 -0800399static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
400{
401 int max_perf = cpu->pstate.turbo_pstate;
402 int min_perf;
403 if (limits.no_turbo)
404 max_perf = cpu->pstate.max_pstate;
405
406 max_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
407 *max = clamp_t(int, max_perf,
408 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
409
410 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
411 *min = clamp_t(int, min_perf,
412 cpu->pstate.min_pstate, max_perf);
413}
414
415static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
416{
417 int max_perf, min_perf;
418
419 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
420
421 pstate = clamp_t(int, pstate, min_perf, max_perf);
422
423 if (pstate == cpu->pstate.current_pstate)
424 return;
425
Dirk Brandewie93f08222013-02-06 09:02:13 -0800426 trace_cpu_frequency(pstate * 100000, cpu->cpu);
Dirk Brandewie35363e92013-05-07 08:20:30 -0700427
Dirk Brandewie93f08222013-02-06 09:02:13 -0800428 cpu->pstate.current_pstate = pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800429
Dirk Brandewie016c8152013-10-21 09:20:34 -0700430 pstate_funcs.set(pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800431}
432
433static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps)
434{
435 int target;
436 target = cpu->pstate.current_pstate + steps;
437
438 intel_pstate_set_pstate(cpu, target);
439}
440
441static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps)
442{
443 int target;
444 target = cpu->pstate.current_pstate - steps;
445 intel_pstate_set_pstate(cpu, target);
446}
447
448static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
449{
450 sprintf(cpu->name, "Intel 2nd generation core");
451
Dirk Brandewie016c8152013-10-21 09:20:34 -0700452 cpu->pstate.min_pstate = pstate_funcs.get_min();
453 cpu->pstate.max_pstate = pstate_funcs.get_max();
454 cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
Dirk Brandewie93f08222013-02-06 09:02:13 -0800455
456 /*
457 * goto max pstate so we don't slow up boot if we are built-in if we are
458 * a module we will take care of it during normal operation
459 */
460 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
461}
462
463static inline void intel_pstate_calc_busy(struct cpudata *cpu,
464 struct sample *sample)
465{
466 u64 core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800467 core_pct = div64_u64(sample->aperf * 100, sample->mperf);
Dirk Brandewiee6f3eb22013-03-24 00:54:39 +0100468 sample->freq = cpu->pstate.max_pstate * core_pct * 1000;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800469
Dirk Brandewie1abc4b22013-05-07 08:20:25 -0700470 sample->core_pct_busy = core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800471}
472
473static inline void intel_pstate_sample(struct cpudata *cpu)
474{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800475 u64 aperf, mperf;
476
Dirk Brandewie93f08222013-02-06 09:02:13 -0800477 rdmsrl(MSR_IA32_APERF, aperf);
478 rdmsrl(MSR_IA32_MPERF, mperf);
Dirk Brandewie1abc4b22013-05-07 08:20:25 -0700479 cpu->sample_ptr = (cpu->sample_ptr + 1) % SAMPLE_COUNT;
480 cpu->samples[cpu->sample_ptr].aperf = aperf;
481 cpu->samples[cpu->sample_ptr].mperf = mperf;
482 cpu->samples[cpu->sample_ptr].aperf -= cpu->prev_aperf;
483 cpu->samples[cpu->sample_ptr].mperf -= cpu->prev_mperf;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800484
Dirk Brandewie1abc4b22013-05-07 08:20:25 -0700485 intel_pstate_calc_busy(cpu, &cpu->samples[cpu->sample_ptr]);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800486
Dirk Brandewie93f08222013-02-06 09:02:13 -0800487 cpu->prev_aperf = aperf;
488 cpu->prev_mperf = mperf;
489}
490
491static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
492{
493 int sample_time, delay;
494
Dirk Brandewie016c8152013-10-21 09:20:34 -0700495 sample_time = pid_params.sample_rate_ms;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800496 delay = msecs_to_jiffies(sample_time);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800497 mod_timer_pinned(&cpu->timer, jiffies + delay);
498}
499
Dirk Brandewie93f08222013-02-06 09:02:13 -0800500static inline int intel_pstate_get_scaled_busy(struct cpudata *cpu)
501{
502 int32_t busy_scaled;
Dirk Brandewie2134ed42013-07-18 08:48:42 -0700503 int32_t core_busy, max_pstate, current_pstate;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800504
505 core_busy = int_tofp(cpu->samples[cpu->sample_ptr].core_pct_busy);
Dirk Brandewie2134ed42013-07-18 08:48:42 -0700506 max_pstate = int_tofp(cpu->pstate.max_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800507 current_pstate = int_tofp(cpu->pstate.current_pstate);
Dirk Brandewie2134ed42013-07-18 08:48:42 -0700508 busy_scaled = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800509
510 return fp_toint(busy_scaled);
511}
512
513static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
514{
515 int busy_scaled;
516 struct _pid *pid;
517 signed int ctl = 0;
518 int steps;
519
520 pid = &cpu->pid;
521 busy_scaled = intel_pstate_get_scaled_busy(cpu);
522
523 ctl = pid_calc(pid, busy_scaled);
524
525 steps = abs(ctl);
526 if (ctl < 0)
527 intel_pstate_pstate_increase(cpu, steps);
528 else
529 intel_pstate_pstate_decrease(cpu, steps);
530}
531
Dirk Brandewie93f08222013-02-06 09:02:13 -0800532static void intel_pstate_timer_func(unsigned long __data)
533{
534 struct cpudata *cpu = (struct cpudata *) __data;
535
536 intel_pstate_sample(cpu);
Dirk Brandewieca182ae2013-05-07 08:20:27 -0700537 intel_pstate_adjust_busy_pstate(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800538
Dirk Brandewie93f08222013-02-06 09:02:13 -0800539 if (cpu->pstate.current_pstate == cpu->pstate.min_pstate) {
540 cpu->min_pstate_count++;
541 if (!(cpu->min_pstate_count % 5)) {
542 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800543 }
544 } else
545 cpu->min_pstate_count = 0;
Dirk Brandewieca182ae2013-05-07 08:20:27 -0700546
Dirk Brandewie93f08222013-02-06 09:02:13 -0800547 intel_pstate_set_sample_time(cpu);
548}
549
550#define ICPU(model, policy) \
551 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&policy }
552
553static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
Dirk Brandewie016c8152013-10-21 09:20:34 -0700554 ICPU(0x2a, core_params),
555 ICPU(0x2d, core_params),
556 ICPU(0x3a, core_params),
557 ICPU(0x3c, core_params),
558 ICPU(0x3e, core_params),
559 ICPU(0x3f, core_params),
560 ICPU(0x45, core_params),
561 ICPU(0x46, core_params),
Dirk Brandewie93f08222013-02-06 09:02:13 -0800562 {}
563};
564MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
565
566static int intel_pstate_init_cpu(unsigned int cpunum)
567{
568
569 const struct x86_cpu_id *id;
570 struct cpudata *cpu;
571
572 id = x86_match_cpu(intel_pstate_cpu_ids);
573 if (!id)
574 return -ENODEV;
575
576 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
577 if (!all_cpu_data[cpunum])
578 return -ENOMEM;
579
580 cpu = all_cpu_data[cpunum];
581
582 intel_pstate_get_cpu_pstates(cpu);
583
584 cpu->cpu = cpunum;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700585
Dirk Brandewie93f08222013-02-06 09:02:13 -0800586 init_timer_deferrable(&cpu->timer);
587 cpu->timer.function = intel_pstate_timer_func;
588 cpu->timer.data =
589 (unsigned long)cpu;
590 cpu->timer.expires = jiffies + HZ/100;
591 intel_pstate_busy_pid_reset(cpu);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800592 intel_pstate_sample(cpu);
593 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
594
595 add_timer_on(&cpu->timer, cpunum);
596
597 pr_info("Intel pstate controlling: cpu %d\n", cpunum);
598
599 return 0;
600}
601
602static unsigned int intel_pstate_get(unsigned int cpu_num)
603{
604 struct sample *sample;
605 struct cpudata *cpu;
606
607 cpu = all_cpu_data[cpu_num];
608 if (!cpu)
609 return 0;
610 sample = &cpu->samples[cpu->sample_ptr];
611 return sample->freq;
612}
613
614static int intel_pstate_set_policy(struct cpufreq_policy *policy)
615{
616 struct cpudata *cpu;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800617
618 cpu = all_cpu_data[policy->cpu];
619
Dirk Brandewied3929b82013-03-05 14:15:26 -0800620 if (!policy->cpuinfo.max_freq)
621 return -ENODEV;
622
Dirk Brandewie93f08222013-02-06 09:02:13 -0800623 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
624 limits.min_perf_pct = 100;
625 limits.min_perf = int_tofp(1);
626 limits.max_perf_pct = 100;
627 limits.max_perf = int_tofp(1);
628 limits.no_turbo = 0;
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000629 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800630 }
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000631 limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
632 limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
633 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
634
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700635 limits.max_policy_pct = policy->max * 100 / policy->cpuinfo.max_freq;
636 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
637 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000638 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800639
640 return 0;
641}
642
643static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
644{
Viresh Kumarbe49e342013-10-02 14:13:19 +0530645 cpufreq_verify_within_cpu_limits(policy);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800646
647 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
648 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
649 return -EINVAL;
650
651 return 0;
652}
653
Paul Gortmaker27609842013-06-19 13:54:04 -0400654static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800655{
656 int cpu = policy->cpu;
657
658 del_timer(&all_cpu_data[cpu]->timer);
659 kfree(all_cpu_data[cpu]);
660 all_cpu_data[cpu] = NULL;
661 return 0;
662}
663
Paul Gortmaker27609842013-06-19 13:54:04 -0400664static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
Dirk Brandewie93f08222013-02-06 09:02:13 -0800665{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800666 struct cpudata *cpu;
Dirk Brandewie52e0a502013-10-15 11:06:14 -0700667 int rc;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800668
669 rc = intel_pstate_init_cpu(policy->cpu);
670 if (rc)
671 return rc;
672
673 cpu = all_cpu_data[policy->cpu];
674
675 if (!limits.no_turbo &&
676 limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
677 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
678 else
679 policy->policy = CPUFREQ_POLICY_POWERSAVE;
680
Dirk Brandewie52e0a502013-10-15 11:06:14 -0700681 policy->min = cpu->pstate.min_pstate * 100000;
682 policy->max = cpu->pstate.turbo_pstate * 100000;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800683
684 /* cpuinfo and default policy values */
685 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
686 policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
687 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
688 cpumask_set_cpu(policy->cpu, policy->cpus);
689
690 return 0;
691}
692
693static struct cpufreq_driver intel_pstate_driver = {
694 .flags = CPUFREQ_CONST_LOOPS,
695 .verify = intel_pstate_verify_policy,
696 .setpolicy = intel_pstate_set_policy,
697 .get = intel_pstate_get,
698 .init = intel_pstate_cpu_init,
699 .exit = intel_pstate_cpu_exit,
700 .name = "intel_pstate",
Dirk Brandewie93f08222013-02-06 09:02:13 -0800701};
702
Dirk Brandewie6be26492013-02-15 22:55:10 +0100703static int __initdata no_load;
704
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100705static int intel_pstate_msrs_not_valid(void)
706{
707 /* Check that all the msr's we are using are valid. */
708 u64 aperf, mperf, tmp;
709
710 rdmsrl(MSR_IA32_APERF, aperf);
711 rdmsrl(MSR_IA32_MPERF, mperf);
712
Dirk Brandewie016c8152013-10-21 09:20:34 -0700713 if (!pstate_funcs.get_max() ||
714 !pstate_funcs.get_min() ||
715 !pstate_funcs.get_turbo())
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100716 return -ENODEV;
717
718 rdmsrl(MSR_IA32_APERF, tmp);
719 if (!(tmp - aperf))
720 return -ENODEV;
721
722 rdmsrl(MSR_IA32_MPERF, tmp);
723 if (!(tmp - mperf))
724 return -ENODEV;
725
726 return 0;
727}
Dirk Brandewie016c8152013-10-21 09:20:34 -0700728
729void copy_pid_params(struct pstate_adjust_policy *policy)
730{
731 pid_params.sample_rate_ms = policy->sample_rate_ms;
732 pid_params.p_gain_pct = policy->p_gain_pct;
733 pid_params.i_gain_pct = policy->i_gain_pct;
734 pid_params.d_gain_pct = policy->d_gain_pct;
735 pid_params.deadband = policy->deadband;
736 pid_params.setpoint = policy->setpoint;
737}
738
739void copy_cpu_funcs(struct pstate_funcs *funcs)
740{
741 pstate_funcs.get_max = funcs->get_max;
742 pstate_funcs.get_min = funcs->get_min;
743 pstate_funcs.get_turbo = funcs->get_turbo;
744 pstate_funcs.set = funcs->set;
745}
746
Dirk Brandewie93f08222013-02-06 09:02:13 -0800747static int __init intel_pstate_init(void)
748{
Dirk Brandewie907cc902013-03-05 14:15:27 -0800749 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800750 const struct x86_cpu_id *id;
Dirk Brandewie016c8152013-10-21 09:20:34 -0700751 struct cpu_defaults *cpu_info;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800752
Dirk Brandewie6be26492013-02-15 22:55:10 +0100753 if (no_load)
754 return -ENODEV;
755
Dirk Brandewie93f08222013-02-06 09:02:13 -0800756 id = x86_match_cpu(intel_pstate_cpu_ids);
757 if (!id)
758 return -ENODEV;
759
Dirk Brandewie016c8152013-10-21 09:20:34 -0700760 cpu_info = (struct cpu_defaults *)id->driver_data;
761
762 copy_pid_params(&cpu_info->pid_policy);
763 copy_cpu_funcs(&cpu_info->funcs);
764
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100765 if (intel_pstate_msrs_not_valid())
766 return -ENODEV;
767
Dirk Brandewie93f08222013-02-06 09:02:13 -0800768 pr_info("Intel P-state driver initializing.\n");
769
Wei Yongjunb57ffac2013-05-13 08:03:43 +0000770 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
Dirk Brandewie93f08222013-02-06 09:02:13 -0800771 if (!all_cpu_data)
772 return -ENOMEM;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800773
774 rc = cpufreq_register_driver(&intel_pstate_driver);
775 if (rc)
776 goto out;
777
778 intel_pstate_debug_expose_params();
779 intel_pstate_sysfs_expose_params();
780 return rc;
781out:
Dirk Brandewie907cc902013-03-05 14:15:27 -0800782 get_online_cpus();
783 for_each_online_cpu(cpu) {
784 if (all_cpu_data[cpu]) {
785 del_timer_sync(&all_cpu_data[cpu]->timer);
786 kfree(all_cpu_data[cpu]);
787 }
788 }
789
790 put_online_cpus();
791 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800792 return -ENODEV;
793}
794device_initcall(intel_pstate_init);
795
Dirk Brandewie6be26492013-02-15 22:55:10 +0100796static int __init intel_pstate_setup(char *str)
797{
798 if (!str)
799 return -EINVAL;
800
801 if (!strcmp(str, "disable"))
802 no_load = 1;
803 return 0;
804}
805early_param("intel_pstate", intel_pstate_setup);
806
Dirk Brandewie93f08222013-02-06 09:02:13 -0800807MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
808MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
809MODULE_LICENSE("GPL");