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