blob: 4a437ffc51867ee2c801a8356d486797cb38697f [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
81 struct pstate_adjust_policy *pstate_policy;
82 struct pstate_data pstate;
83 struct _pid pid;
84 struct _pid idle_pid;
85
86 int min_pstate_count;
87 int idle_mode;
88
Dirk Brandewie93f08222013-02-06 09:02:13 -080089 u64 prev_aperf;
90 u64 prev_mperf;
91 int sample_ptr;
92 struct sample samples[SAMPLE_COUNT];
93};
94
95static struct cpudata **all_cpu_data;
96struct pstate_adjust_policy {
97 int sample_rate_ms;
98 int deadband;
99 int setpoint;
100 int p_gain_pct;
101 int d_gain_pct;
102 int i_gain_pct;
103};
104
105static struct pstate_adjust_policy default_policy = {
106 .sample_rate_ms = 10,
107 .deadband = 0,
108 .setpoint = 109,
109 .p_gain_pct = 17,
110 .d_gain_pct = 0,
111 .i_gain_pct = 4,
112};
113
114struct perf_limits {
115 int no_turbo;
116 int max_perf_pct;
117 int min_perf_pct;
118 int32_t max_perf;
119 int32_t min_perf;
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700120 int max_policy_pct;
121 int max_sysfs_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800122};
123
124static struct perf_limits limits = {
125 .no_turbo = 0,
126 .max_perf_pct = 100,
127 .max_perf = int_tofp(1),
128 .min_perf_pct = 0,
129 .min_perf = 0,
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700130 .max_policy_pct = 100,
131 .max_sysfs_pct = 100,
Dirk Brandewie93f08222013-02-06 09:02:13 -0800132};
133
134static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
135 int deadband, int integral) {
136 pid->setpoint = setpoint;
137 pid->deadband = deadband;
138 pid->integral = int_tofp(integral);
139 pid->last_err = setpoint - busy;
140}
141
142static inline void pid_p_gain_set(struct _pid *pid, int percent)
143{
144 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
145}
146
147static inline void pid_i_gain_set(struct _pid *pid, int percent)
148{
149 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
150}
151
152static inline void pid_d_gain_set(struct _pid *pid, int percent)
153{
154
155 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
156}
157
158static signed int pid_calc(struct _pid *pid, int busy)
159{
160 signed int err, result;
161 int32_t pterm, dterm, fp_error;
162 int32_t integral_limit;
163
164 err = pid->setpoint - busy;
165 fp_error = int_tofp(err);
166
167 if (abs(err) <= pid->deadband)
168 return 0;
169
170 pterm = mul_fp(pid->p_gain, fp_error);
171
172 pid->integral += fp_error;
173
174 /* limit the integral term */
175 integral_limit = int_tofp(30);
176 if (pid->integral > integral_limit)
177 pid->integral = integral_limit;
178 if (pid->integral < -integral_limit)
179 pid->integral = -integral_limit;
180
181 dterm = mul_fp(pid->d_gain, (err - pid->last_err));
182 pid->last_err = err;
183
184 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
185
186 return (signed int)fp_toint(result);
187}
188
189static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
190{
191 pid_p_gain_set(&cpu->pid, cpu->pstate_policy->p_gain_pct);
192 pid_d_gain_set(&cpu->pid, cpu->pstate_policy->d_gain_pct);
193 pid_i_gain_set(&cpu->pid, cpu->pstate_policy->i_gain_pct);
194
195 pid_reset(&cpu->pid,
196 cpu->pstate_policy->setpoint,
197 100,
198 cpu->pstate_policy->deadband,
199 0);
200}
201
202static inline void intel_pstate_idle_pid_reset(struct cpudata *cpu)
203{
204 pid_p_gain_set(&cpu->idle_pid, cpu->pstate_policy->p_gain_pct);
205 pid_d_gain_set(&cpu->idle_pid, cpu->pstate_policy->d_gain_pct);
206 pid_i_gain_set(&cpu->idle_pid, cpu->pstate_policy->i_gain_pct);
207
208 pid_reset(&cpu->idle_pid,
209 75,
210 50,
211 cpu->pstate_policy->deadband,
212 0);
213}
214
215static inline void intel_pstate_reset_all_pid(void)
216{
217 unsigned int cpu;
218 for_each_online_cpu(cpu) {
219 if (all_cpu_data[cpu])
220 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
221 }
222}
223
224/************************** debugfs begin ************************/
225static int pid_param_set(void *data, u64 val)
226{
227 *(u32 *)data = val;
228 intel_pstate_reset_all_pid();
229 return 0;
230}
231static int pid_param_get(void *data, u64 *val)
232{
233 *val = *(u32 *)data;
234 return 0;
235}
236DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get,
237 pid_param_set, "%llu\n");
238
239struct pid_param {
240 char *name;
241 void *value;
242};
243
244static struct pid_param pid_files[] = {
245 {"sample_rate_ms", &default_policy.sample_rate_ms},
246 {"d_gain_pct", &default_policy.d_gain_pct},
247 {"i_gain_pct", &default_policy.i_gain_pct},
248 {"deadband", &default_policy.deadband},
249 {"setpoint", &default_policy.setpoint},
250 {"p_gain_pct", &default_policy.p_gain_pct},
251 {NULL, NULL}
252};
253
254static struct dentry *debugfs_parent;
255static void intel_pstate_debug_expose_params(void)
256{
257 int i = 0;
258
259 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
260 if (IS_ERR_OR_NULL(debugfs_parent))
261 return;
262 while (pid_files[i].name) {
263 debugfs_create_file(pid_files[i].name, 0660,
264 debugfs_parent, pid_files[i].value,
265 &fops_pid_param);
266 i++;
267 }
268}
269
270/************************** debugfs end ************************/
271
272/************************** sysfs begin ************************/
273#define show_one(file_name, object) \
274 static ssize_t show_##file_name \
275 (struct kobject *kobj, struct attribute *attr, char *buf) \
276 { \
277 return sprintf(buf, "%u\n", limits.object); \
278 }
279
280static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
281 const char *buf, size_t count)
282{
283 unsigned int input;
284 int ret;
285 ret = sscanf(buf, "%u", &input);
286 if (ret != 1)
287 return -EINVAL;
288 limits.no_turbo = clamp_t(int, input, 0 , 1);
289
290 return count;
291}
292
293static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
294 const char *buf, size_t count)
295{
296 unsigned int input;
297 int ret;
298 ret = sscanf(buf, "%u", &input);
299 if (ret != 1)
300 return -EINVAL;
301
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700302 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
303 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800304 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
305 return count;
306}
307
308static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
309 const char *buf, size_t count)
310{
311 unsigned int input;
312 int ret;
313 ret = sscanf(buf, "%u", &input);
314 if (ret != 1)
315 return -EINVAL;
316 limits.min_perf_pct = clamp_t(int, input, 0 , 100);
317 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
318
319 return count;
320}
321
322show_one(no_turbo, no_turbo);
323show_one(max_perf_pct, max_perf_pct);
324show_one(min_perf_pct, min_perf_pct);
325
326define_one_global_rw(no_turbo);
327define_one_global_rw(max_perf_pct);
328define_one_global_rw(min_perf_pct);
329
330static struct attribute *intel_pstate_attributes[] = {
331 &no_turbo.attr,
332 &max_perf_pct.attr,
333 &min_perf_pct.attr,
334 NULL
335};
336
337static struct attribute_group intel_pstate_attr_group = {
338 .attrs = intel_pstate_attributes,
339};
340static struct kobject *intel_pstate_kobject;
341
342static void intel_pstate_sysfs_expose_params(void)
343{
344 int rc;
345
346 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
347 &cpu_subsys.dev_root->kobj);
348 BUG_ON(!intel_pstate_kobject);
349 rc = sysfs_create_group(intel_pstate_kobject,
350 &intel_pstate_attr_group);
351 BUG_ON(rc);
352}
353
354/************************** sysfs end ************************/
355
356static int intel_pstate_min_pstate(void)
357{
358 u64 value;
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000359 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800360 return (value >> 40) & 0xFF;
361}
362
363static int intel_pstate_max_pstate(void)
364{
365 u64 value;
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000366 rdmsrl(MSR_PLATFORM_INFO, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800367 return (value >> 8) & 0xFF;
368}
369
370static int intel_pstate_turbo_pstate(void)
371{
372 u64 value;
373 int nont, ret;
Konrad Rzeszutek Wilk05e99c8cf2013-03-20 14:21:10 +0000374 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800375 nont = intel_pstate_max_pstate();
376 ret = ((value) & 255);
377 if (ret <= nont)
378 ret = nont;
379 return ret;
380}
381
382static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
383{
384 int max_perf = cpu->pstate.turbo_pstate;
385 int min_perf;
386 if (limits.no_turbo)
387 max_perf = cpu->pstate.max_pstate;
388
389 max_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
390 *max = clamp_t(int, max_perf,
391 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
392
393 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
394 *min = clamp_t(int, min_perf,
395 cpu->pstate.min_pstate, max_perf);
396}
397
398static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
399{
400 int max_perf, min_perf;
401
402 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
403
404 pstate = clamp_t(int, pstate, min_perf, max_perf);
405
406 if (pstate == cpu->pstate.current_pstate)
407 return;
408
409#ifndef MODULE
410 trace_cpu_frequency(pstate * 100000, cpu->cpu);
411#endif
412 cpu->pstate.current_pstate = pstate;
413 wrmsrl(MSR_IA32_PERF_CTL, pstate << 8);
414
415}
416
417static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps)
418{
419 int target;
420 target = cpu->pstate.current_pstate + steps;
421
422 intel_pstate_set_pstate(cpu, target);
423}
424
425static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps)
426{
427 int target;
428 target = cpu->pstate.current_pstate - steps;
429 intel_pstate_set_pstate(cpu, target);
430}
431
432static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
433{
434 sprintf(cpu->name, "Intel 2nd generation core");
435
436 cpu->pstate.min_pstate = intel_pstate_min_pstate();
437 cpu->pstate.max_pstate = intel_pstate_max_pstate();
438 cpu->pstate.turbo_pstate = intel_pstate_turbo_pstate();
439
440 /*
441 * goto max pstate so we don't slow up boot if we are built-in if we are
442 * a module we will take care of it during normal operation
443 */
444 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
445}
446
447static inline void intel_pstate_calc_busy(struct cpudata *cpu,
448 struct sample *sample)
449{
450 u64 core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800451 core_pct = div64_u64(sample->aperf * 100, sample->mperf);
Dirk Brandewiee6f3eb22013-03-24 00:54:39 +0100452 sample->freq = cpu->pstate.max_pstate * core_pct * 1000;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800453
Dirk Brandewie1abc4b22013-05-07 08:20:25 -0700454 sample->core_pct_busy = core_pct;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800455}
456
457static inline void intel_pstate_sample(struct cpudata *cpu)
458{
Dirk Brandewie93f08222013-02-06 09:02:13 -0800459 u64 aperf, mperf;
460
Dirk Brandewie93f08222013-02-06 09:02:13 -0800461 rdmsrl(MSR_IA32_APERF, aperf);
462 rdmsrl(MSR_IA32_MPERF, mperf);
Dirk Brandewie1abc4b22013-05-07 08:20:25 -0700463 cpu->sample_ptr = (cpu->sample_ptr + 1) % SAMPLE_COUNT;
464 cpu->samples[cpu->sample_ptr].aperf = aperf;
465 cpu->samples[cpu->sample_ptr].mperf = mperf;
466 cpu->samples[cpu->sample_ptr].aperf -= cpu->prev_aperf;
467 cpu->samples[cpu->sample_ptr].mperf -= cpu->prev_mperf;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800468
Dirk Brandewie1abc4b22013-05-07 08:20:25 -0700469 intel_pstate_calc_busy(cpu, &cpu->samples[cpu->sample_ptr]);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800470
Dirk Brandewie93f08222013-02-06 09:02:13 -0800471 cpu->prev_aperf = aperf;
472 cpu->prev_mperf = mperf;
473}
474
475static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
476{
477 int sample_time, delay;
478
479 sample_time = cpu->pstate_policy->sample_rate_ms;
480 delay = msecs_to_jiffies(sample_time);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800481 mod_timer_pinned(&cpu->timer, jiffies + delay);
482}
483
484static inline void intel_pstate_idle_mode(struct cpudata *cpu)
485{
486 cpu->idle_mode = 1;
487}
488
489static inline void intel_pstate_normal_mode(struct cpudata *cpu)
490{
491 cpu->idle_mode = 0;
492}
493
494static inline int intel_pstate_get_scaled_busy(struct cpudata *cpu)
495{
496 int32_t busy_scaled;
497 int32_t core_busy, turbo_pstate, current_pstate;
498
499 core_busy = int_tofp(cpu->samples[cpu->sample_ptr].core_pct_busy);
500 turbo_pstate = int_tofp(cpu->pstate.turbo_pstate);
501 current_pstate = int_tofp(cpu->pstate.current_pstate);
502 busy_scaled = mul_fp(core_busy, div_fp(turbo_pstate, current_pstate));
503
504 return fp_toint(busy_scaled);
505}
506
507static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
508{
509 int busy_scaled;
510 struct _pid *pid;
511 signed int ctl = 0;
512 int steps;
513
514 pid = &cpu->pid;
515 busy_scaled = intel_pstate_get_scaled_busy(cpu);
516
517 ctl = pid_calc(pid, busy_scaled);
518
519 steps = abs(ctl);
520 if (ctl < 0)
521 intel_pstate_pstate_increase(cpu, steps);
522 else
523 intel_pstate_pstate_decrease(cpu, steps);
524}
525
526static inline void intel_pstate_adjust_idle_pstate(struct cpudata *cpu)
527{
528 int busy_scaled;
529 struct _pid *pid;
530 int ctl = 0;
531 int steps;
532
533 pid = &cpu->idle_pid;
534
535 busy_scaled = intel_pstate_get_scaled_busy(cpu);
536
537 ctl = pid_calc(pid, 100 - busy_scaled);
538
539 steps = abs(ctl);
540 if (ctl < 0)
541 intel_pstate_pstate_decrease(cpu, steps);
542 else
543 intel_pstate_pstate_increase(cpu, steps);
544
545 if (cpu->pstate.current_pstate == cpu->pstate.min_pstate)
546 intel_pstate_normal_mode(cpu);
547}
548
549static void intel_pstate_timer_func(unsigned long __data)
550{
551 struct cpudata *cpu = (struct cpudata *) __data;
552
553 intel_pstate_sample(cpu);
554
555 if (!cpu->idle_mode)
556 intel_pstate_adjust_busy_pstate(cpu);
557 else
558 intel_pstate_adjust_idle_pstate(cpu);
559
560#if defined(XPERF_FIX)
561 if (cpu->pstate.current_pstate == cpu->pstate.min_pstate) {
562 cpu->min_pstate_count++;
563 if (!(cpu->min_pstate_count % 5)) {
564 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
565 intel_pstate_idle_mode(cpu);
566 }
567 } else
568 cpu->min_pstate_count = 0;
569#endif
570 intel_pstate_set_sample_time(cpu);
571}
572
573#define ICPU(model, policy) \
574 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&policy }
575
576static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
577 ICPU(0x2a, default_policy),
578 ICPU(0x2d, default_policy),
579 {}
580};
581MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
582
583static int intel_pstate_init_cpu(unsigned int cpunum)
584{
585
586 const struct x86_cpu_id *id;
587 struct cpudata *cpu;
588
589 id = x86_match_cpu(intel_pstate_cpu_ids);
590 if (!id)
591 return -ENODEV;
592
593 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
594 if (!all_cpu_data[cpunum])
595 return -ENOMEM;
596
597 cpu = all_cpu_data[cpunum];
598
599 intel_pstate_get_cpu_pstates(cpu);
600
601 cpu->cpu = cpunum;
602 cpu->pstate_policy =
603 (struct pstate_adjust_policy *)id->driver_data;
604 init_timer_deferrable(&cpu->timer);
605 cpu->timer.function = intel_pstate_timer_func;
606 cpu->timer.data =
607 (unsigned long)cpu;
608 cpu->timer.expires = jiffies + HZ/100;
609 intel_pstate_busy_pid_reset(cpu);
610 intel_pstate_idle_pid_reset(cpu);
611 intel_pstate_sample(cpu);
612 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
613
614 add_timer_on(&cpu->timer, cpunum);
615
616 pr_info("Intel pstate controlling: cpu %d\n", cpunum);
617
618 return 0;
619}
620
621static unsigned int intel_pstate_get(unsigned int cpu_num)
622{
623 struct sample *sample;
624 struct cpudata *cpu;
625
626 cpu = all_cpu_data[cpu_num];
627 if (!cpu)
628 return 0;
629 sample = &cpu->samples[cpu->sample_ptr];
630 return sample->freq;
631}
632
633static int intel_pstate_set_policy(struct cpufreq_policy *policy)
634{
635 struct cpudata *cpu;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800636
637 cpu = all_cpu_data[policy->cpu];
638
Dirk Brandewied3929b82013-03-05 14:15:26 -0800639 if (!policy->cpuinfo.max_freq)
640 return -ENODEV;
641
Dirk Brandewie93f08222013-02-06 09:02:13 -0800642 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
643 limits.min_perf_pct = 100;
644 limits.min_perf = int_tofp(1);
645 limits.max_perf_pct = 100;
646 limits.max_perf = int_tofp(1);
647 limits.no_turbo = 0;
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000648 return 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800649 }
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000650 limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
651 limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
652 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
653
Dirk Brandewied8f469e2013-05-07 08:20:26 -0700654 limits.max_policy_pct = policy->max * 100 / policy->cpuinfo.max_freq;
655 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
656 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
Srinivas Pandruvadad1b68482013-04-09 22:38:18 +0000657 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
Dirk Brandewie93f08222013-02-06 09:02:13 -0800658
659 return 0;
660}
661
662static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
663{
664 cpufreq_verify_within_limits(policy,
665 policy->cpuinfo.min_freq,
666 policy->cpuinfo.max_freq);
667
668 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
669 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
670 return -EINVAL;
671
672 return 0;
673}
674
675static int __cpuinit intel_pstate_cpu_exit(struct cpufreq_policy *policy)
676{
677 int cpu = policy->cpu;
678
679 del_timer(&all_cpu_data[cpu]->timer);
680 kfree(all_cpu_data[cpu]);
681 all_cpu_data[cpu] = NULL;
682 return 0;
683}
684
685static int __cpuinit intel_pstate_cpu_init(struct cpufreq_policy *policy)
686{
687 int rc, min_pstate, max_pstate;
688 struct cpudata *cpu;
689
690 rc = intel_pstate_init_cpu(policy->cpu);
691 if (rc)
692 return rc;
693
694 cpu = all_cpu_data[policy->cpu];
695
696 if (!limits.no_turbo &&
697 limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
698 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
699 else
700 policy->policy = CPUFREQ_POLICY_POWERSAVE;
701
702 intel_pstate_get_min_max(cpu, &min_pstate, &max_pstate);
703 policy->min = min_pstate * 100000;
704 policy->max = max_pstate * 100000;
705
706 /* cpuinfo and default policy values */
707 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
708 policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
709 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
710 cpumask_set_cpu(policy->cpu, policy->cpus);
711
712 return 0;
713}
714
715static struct cpufreq_driver intel_pstate_driver = {
716 .flags = CPUFREQ_CONST_LOOPS,
717 .verify = intel_pstate_verify_policy,
718 .setpolicy = intel_pstate_set_policy,
719 .get = intel_pstate_get,
720 .init = intel_pstate_cpu_init,
721 .exit = intel_pstate_cpu_exit,
722 .name = "intel_pstate",
723 .owner = THIS_MODULE,
724};
725
Dirk Brandewie6be26492013-02-15 22:55:10 +0100726static int __initdata no_load;
727
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100728static int intel_pstate_msrs_not_valid(void)
729{
730 /* Check that all the msr's we are using are valid. */
731 u64 aperf, mperf, tmp;
732
733 rdmsrl(MSR_IA32_APERF, aperf);
734 rdmsrl(MSR_IA32_MPERF, mperf);
735
736 if (!intel_pstate_min_pstate() ||
737 !intel_pstate_max_pstate() ||
738 !intel_pstate_turbo_pstate())
739 return -ENODEV;
740
741 rdmsrl(MSR_IA32_APERF, tmp);
742 if (!(tmp - aperf))
743 return -ENODEV;
744
745 rdmsrl(MSR_IA32_MPERF, tmp);
746 if (!(tmp - mperf))
747 return -ENODEV;
748
749 return 0;
750}
Dirk Brandewie93f08222013-02-06 09:02:13 -0800751static int __init intel_pstate_init(void)
752{
Dirk Brandewie907cc902013-03-05 14:15:27 -0800753 int cpu, rc = 0;
Dirk Brandewie93f08222013-02-06 09:02:13 -0800754 const struct x86_cpu_id *id;
755
Dirk Brandewie6be26492013-02-15 22:55:10 +0100756 if (no_load)
757 return -ENODEV;
758
Dirk Brandewie93f08222013-02-06 09:02:13 -0800759 id = x86_match_cpu(intel_pstate_cpu_ids);
760 if (!id)
761 return -ENODEV;
762
Dirk Brandewieb563b4e2013-03-22 01:29:28 +0100763 if (intel_pstate_msrs_not_valid())
764 return -ENODEV;
765
Dirk Brandewie93f08222013-02-06 09:02:13 -0800766 pr_info("Intel P-state driver initializing.\n");
767
768 all_cpu_data = vmalloc(sizeof(void *) * num_possible_cpus());
769 if (!all_cpu_data)
770 return -ENOMEM;
771 memset(all_cpu_data, 0, sizeof(void *) * num_possible_cpus());
772
773 rc = cpufreq_register_driver(&intel_pstate_driver);
774 if (rc)
775 goto out;
776
777 intel_pstate_debug_expose_params();
778 intel_pstate_sysfs_expose_params();
779 return rc;
780out:
Dirk Brandewie907cc902013-03-05 14:15:27 -0800781 get_online_cpus();
782 for_each_online_cpu(cpu) {
783 if (all_cpu_data[cpu]) {
784 del_timer_sync(&all_cpu_data[cpu]->timer);
785 kfree(all_cpu_data[cpu]);
786 }
787 }
788
789 put_online_cpus();
790 vfree(all_cpu_data);
Dirk Brandewie93f08222013-02-06 09:02:13 -0800791 return -ENODEV;
792}
793device_initcall(intel_pstate_init);
794
Dirk Brandewie6be26492013-02-15 22:55:10 +0100795static int __init intel_pstate_setup(char *str)
796{
797 if (!str)
798 return -EINVAL;
799
800 if (!strcmp(str, "disable"))
801 no_load = 1;
802 return 0;
803}
804early_param("intel_pstate", intel_pstate_setup);
805
Dirk Brandewie93f08222013-02-06 09:02:13 -0800806MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
807MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
808MODULE_LICENSE("GPL");