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