]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/cpufreq/intel_pstate.c
intel_pstate: Fix BYT frequency reporting
[karo-tx-linux.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 <linux/acpi.h>
29 #include <trace/events/power.h>
30
31 #include <asm/div64.h>
32 #include <asm/msr.h>
33 #include <asm/cpu_device_id.h>
34
35 #define BYT_RATIOS              0x66a
36 #define BYT_VIDS                0x66b
37 #define BYT_TURBO_RATIOS        0x66c
38 #define BYT_TURBO_VIDS          0x66d
39
40 #define FRAC_BITS 8
41 #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
42 #define fp_toint(X) ((X) >> FRAC_BITS)
43
44
45 static inline int32_t mul_fp(int32_t x, int32_t y)
46 {
47         return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
48 }
49
50 static inline int32_t div_fp(int32_t x, int32_t y)
51 {
52         return div_s64((int64_t)x << FRAC_BITS, y);
53 }
54
55 struct sample {
56         int32_t core_pct_busy;
57         u64 aperf;
58         u64 mperf;
59         int freq;
60         ktime_t time;
61 };
62
63 struct pstate_data {
64         int     current_pstate;
65         int     min_pstate;
66         int     max_pstate;
67         int     scaling;
68         int     turbo_pstate;
69 };
70
71 struct vid_data {
72         int min;
73         int max;
74         int turbo;
75         int32_t ratio;
76 };
77
78 struct _pid {
79         int setpoint;
80         int32_t integral;
81         int32_t p_gain;
82         int32_t i_gain;
83         int32_t d_gain;
84         int deadband;
85         int32_t last_err;
86 };
87
88 struct cpudata {
89         int cpu;
90
91         struct timer_list timer;
92
93         struct pstate_data pstate;
94         struct vid_data vid;
95         struct _pid pid;
96
97         ktime_t last_sample_time;
98         u64     prev_aperf;
99         u64     prev_mperf;
100         struct sample sample;
101 };
102
103 static struct cpudata **all_cpu_data;
104 struct pstate_adjust_policy {
105         int sample_rate_ms;
106         int deadband;
107         int setpoint;
108         int p_gain_pct;
109         int d_gain_pct;
110         int i_gain_pct;
111 };
112
113 struct pstate_funcs {
114         int (*get_max)(void);
115         int (*get_min)(void);
116         int (*get_turbo)(void);
117         int (*get_scaling)(void);
118         void (*set)(struct cpudata*, int pstate);
119         void (*get_vid)(struct cpudata *);
120 };
121
122 struct cpu_defaults {
123         struct pstate_adjust_policy pid_policy;
124         struct pstate_funcs funcs;
125 };
126
127 static struct pstate_adjust_policy pid_params;
128 static struct pstate_funcs pstate_funcs;
129
130 struct perf_limits {
131         int no_turbo;
132         int turbo_disabled;
133         int max_perf_pct;
134         int min_perf_pct;
135         int32_t max_perf;
136         int32_t min_perf;
137         int max_policy_pct;
138         int max_sysfs_pct;
139 };
140
141 static struct perf_limits limits = {
142         .no_turbo = 0,
143         .turbo_disabled = 0,
144         .max_perf_pct = 100,
145         .max_perf = int_tofp(1),
146         .min_perf_pct = 0,
147         .min_perf = 0,
148         .max_policy_pct = 100,
149         .max_sysfs_pct = 100,
150 };
151
152 static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
153                              int deadband, int integral) {
154         pid->setpoint = setpoint;
155         pid->deadband  = deadband;
156         pid->integral  = int_tofp(integral);
157         pid->last_err  = int_tofp(setpoint) - int_tofp(busy);
158 }
159
160 static inline void pid_p_gain_set(struct _pid *pid, int percent)
161 {
162         pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
163 }
164
165 static inline void pid_i_gain_set(struct _pid *pid, int percent)
166 {
167         pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
168 }
169
170 static inline void pid_d_gain_set(struct _pid *pid, int percent)
171 {
172         pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
173 }
174
175 static signed int pid_calc(struct _pid *pid, int32_t busy)
176 {
177         signed int result;
178         int32_t pterm, dterm, fp_error;
179         int32_t integral_limit;
180
181         fp_error = int_tofp(pid->setpoint) - busy;
182
183         if (abs(fp_error) <= int_tofp(pid->deadband))
184                 return 0;
185
186         pterm = mul_fp(pid->p_gain, fp_error);
187
188         pid->integral += fp_error;
189
190         /* limit the integral term */
191         integral_limit = int_tofp(30);
192         if (pid->integral > integral_limit)
193                 pid->integral = integral_limit;
194         if (pid->integral < -integral_limit)
195                 pid->integral = -integral_limit;
196
197         dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
198         pid->last_err = fp_error;
199
200         result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
201         result = result + (1 << (FRAC_BITS-1));
202         return (signed int)fp_toint(result);
203 }
204
205 static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
206 {
207         pid_p_gain_set(&cpu->pid, pid_params.p_gain_pct);
208         pid_d_gain_set(&cpu->pid, pid_params.d_gain_pct);
209         pid_i_gain_set(&cpu->pid, pid_params.i_gain_pct);
210
211         pid_reset(&cpu->pid, pid_params.setpoint, 100, pid_params.deadband, 0);
212 }
213
214 static inline void intel_pstate_reset_all_pid(void)
215 {
216         unsigned int cpu;
217
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 static inline void update_turbo_state(void)
225 {
226         u64 misc_en;
227         struct cpudata *cpu;
228
229         cpu = all_cpu_data[0];
230         rdmsrl(MSR_IA32_MISC_ENABLE, misc_en);
231         limits.turbo_disabled =
232                 (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ||
233                  cpu->pstate.max_pstate == cpu->pstate.turbo_pstate);
234 }
235
236 /************************** debugfs begin ************************/
237 static int pid_param_set(void *data, u64 val)
238 {
239         *(u32 *)data = val;
240         intel_pstate_reset_all_pid();
241         return 0;
242 }
243
244 static int pid_param_get(void *data, u64 *val)
245 {
246         *val = *(u32 *)data;
247         return 0;
248 }
249 DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get, pid_param_set, "%llu\n");
250
251 struct pid_param {
252         char *name;
253         void *value;
254 };
255
256 static struct pid_param pid_files[] = {
257         {"sample_rate_ms", &pid_params.sample_rate_ms},
258         {"d_gain_pct", &pid_params.d_gain_pct},
259         {"i_gain_pct", &pid_params.i_gain_pct},
260         {"deadband", &pid_params.deadband},
261         {"setpoint", &pid_params.setpoint},
262         {"p_gain_pct", &pid_params.p_gain_pct},
263         {NULL, NULL}
264 };
265
266 static void __init intel_pstate_debug_expose_params(void)
267 {
268         struct dentry *debugfs_parent;
269         int i = 0;
270
271         debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
272         if (IS_ERR_OR_NULL(debugfs_parent))
273                 return;
274         while (pid_files[i].name) {
275                 debugfs_create_file(pid_files[i].name, 0660,
276                                     debugfs_parent, pid_files[i].value,
277                                     &fops_pid_param);
278                 i++;
279         }
280 }
281
282 /************************** debugfs end ************************/
283
284 /************************** sysfs begin ************************/
285 #define show_one(file_name, object)                                     \
286         static ssize_t show_##file_name                                 \
287         (struct kobject *kobj, struct attribute *attr, char *buf)       \
288         {                                                               \
289                 return sprintf(buf, "%u\n", limits.object);             \
290         }
291
292 static ssize_t show_no_turbo(struct kobject *kobj,
293                              struct attribute *attr, char *buf)
294 {
295         ssize_t ret;
296
297         update_turbo_state();
298         if (limits.turbo_disabled)
299                 ret = sprintf(buf, "%u\n", limits.turbo_disabled);
300         else
301                 ret = sprintf(buf, "%u\n", limits.no_turbo);
302
303         return ret;
304 }
305
306 static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
307                               const char *buf, size_t count)
308 {
309         unsigned int input;
310         int ret;
311
312         ret = sscanf(buf, "%u", &input);
313         if (ret != 1)
314                 return -EINVAL;
315
316         update_turbo_state();
317         if (limits.turbo_disabled) {
318                 pr_warn("Turbo disabled by BIOS or unavailable on processor\n");
319                 return -EPERM;
320         }
321         limits.no_turbo = clamp_t(int, input, 0, 1);
322
323         return count;
324 }
325
326 static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
327                                   const char *buf, size_t count)
328 {
329         unsigned int input;
330         int ret;
331
332         ret = sscanf(buf, "%u", &input);
333         if (ret != 1)
334                 return -EINVAL;
335
336         limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
337         limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
338         limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
339
340         return count;
341 }
342
343 static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
344                                   const char *buf, size_t count)
345 {
346         unsigned int input;
347         int ret;
348
349         ret = sscanf(buf, "%u", &input);
350         if (ret != 1)
351                 return -EINVAL;
352         limits.min_perf_pct = clamp_t(int, input, 0 , 100);
353         limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
354
355         return count;
356 }
357
358 show_one(max_perf_pct, max_perf_pct);
359 show_one(min_perf_pct, min_perf_pct);
360
361 define_one_global_rw(no_turbo);
362 define_one_global_rw(max_perf_pct);
363 define_one_global_rw(min_perf_pct);
364
365 static struct attribute *intel_pstate_attributes[] = {
366         &no_turbo.attr,
367         &max_perf_pct.attr,
368         &min_perf_pct.attr,
369         NULL
370 };
371
372 static struct attribute_group intel_pstate_attr_group = {
373         .attrs = intel_pstate_attributes,
374 };
375
376 static void __init intel_pstate_sysfs_expose_params(void)
377 {
378         struct kobject *intel_pstate_kobject;
379         int rc;
380
381         intel_pstate_kobject = kobject_create_and_add("intel_pstate",
382                                                 &cpu_subsys.dev_root->kobj);
383         BUG_ON(!intel_pstate_kobject);
384         rc = sysfs_create_group(intel_pstate_kobject, &intel_pstate_attr_group);
385         BUG_ON(rc);
386 }
387
388 /************************** sysfs end ************************/
389 static int byt_get_min_pstate(void)
390 {
391         u64 value;
392
393         rdmsrl(BYT_RATIOS, value);
394         return (value >> 8) & 0x7F;
395 }
396
397 static int byt_get_max_pstate(void)
398 {
399         u64 value;
400
401         rdmsrl(BYT_RATIOS, value);
402         return (value >> 16) & 0x7F;
403 }
404
405 static int byt_get_turbo_pstate(void)
406 {
407         u64 value;
408
409         rdmsrl(BYT_TURBO_RATIOS, value);
410         return value & 0x7F;
411 }
412
413 static void byt_set_pstate(struct cpudata *cpudata, int pstate)
414 {
415         u64 val;
416         int32_t vid_fp;
417         u32 vid;
418
419         val = pstate << 8;
420         if (limits.no_turbo && !limits.turbo_disabled)
421                 val |= (u64)1 << 32;
422
423         vid_fp = cpudata->vid.min + mul_fp(
424                 int_tofp(pstate - cpudata->pstate.min_pstate),
425                 cpudata->vid.ratio);
426
427         vid_fp = clamp_t(int32_t, vid_fp, cpudata->vid.min, cpudata->vid.max);
428         vid = fp_toint(vid_fp);
429
430         if (pstate > cpudata->pstate.max_pstate)
431                 vid = cpudata->vid.turbo;
432
433         val |= vid;
434
435         wrmsrl(MSR_IA32_PERF_CTL, val);
436 }
437
438 #define BYT_BCLK_FREQS 5
439 static int byt_freq_table[BYT_BCLK_FREQS] = { 833, 1000, 1333, 1167, 800};
440
441 static int byt_get_scaling(void)
442 {
443         u64 value;
444         int i;
445
446         rdmsrl(MSR_FSB_FREQ, value);
447         i = value & 0x3;
448
449         BUG_ON(i > BYT_BCLK_FREQS);
450
451         return byt_freq_table[i] * 100;
452 }
453
454 static void byt_get_vid(struct cpudata *cpudata)
455 {
456         u64 value;
457
458         rdmsrl(BYT_VIDS, value);
459         cpudata->vid.min = int_tofp((value >> 8) & 0x7f);
460         cpudata->vid.max = int_tofp((value >> 16) & 0x7f);
461         cpudata->vid.ratio = div_fp(
462                 cpudata->vid.max - cpudata->vid.min,
463                 int_tofp(cpudata->pstate.max_pstate -
464                         cpudata->pstate.min_pstate));
465
466         rdmsrl(BYT_TURBO_VIDS, value);
467         cpudata->vid.turbo = value & 0x7f;
468 }
469
470 static int core_get_min_pstate(void)
471 {
472         u64 value;
473
474         rdmsrl(MSR_PLATFORM_INFO, value);
475         return (value >> 40) & 0xFF;
476 }
477
478 static int core_get_max_pstate(void)
479 {
480         u64 value;
481
482         rdmsrl(MSR_PLATFORM_INFO, value);
483         return (value >> 8) & 0xFF;
484 }
485
486 static int core_get_turbo_pstate(void)
487 {
488         u64 value;
489         int nont, ret;
490
491         rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
492         nont = core_get_max_pstate();
493         ret = (value) & 255;
494         if (ret <= nont)
495                 ret = nont;
496         return ret;
497 }
498
499 static inline int core_get_scaling(void)
500 {
501         return 100000;
502 }
503
504 static void core_set_pstate(struct cpudata *cpudata, int pstate)
505 {
506         u64 val;
507
508         val = pstate << 8;
509         if (limits.no_turbo && !limits.turbo_disabled)
510                 val |= (u64)1 << 32;
511
512         wrmsrl_on_cpu(cpudata->cpu, MSR_IA32_PERF_CTL, val);
513 }
514
515 static struct cpu_defaults core_params = {
516         .pid_policy = {
517                 .sample_rate_ms = 10,
518                 .deadband = 0,
519                 .setpoint = 97,
520                 .p_gain_pct = 20,
521                 .d_gain_pct = 0,
522                 .i_gain_pct = 0,
523         },
524         .funcs = {
525                 .get_max = core_get_max_pstate,
526                 .get_min = core_get_min_pstate,
527                 .get_turbo = core_get_turbo_pstate,
528                 .get_scaling = core_get_scaling,
529                 .set = core_set_pstate,
530         },
531 };
532
533 static struct cpu_defaults byt_params = {
534         .pid_policy = {
535                 .sample_rate_ms = 10,
536                 .deadband = 0,
537                 .setpoint = 97,
538                 .p_gain_pct = 14,
539                 .d_gain_pct = 0,
540                 .i_gain_pct = 4,
541         },
542         .funcs = {
543                 .get_max = byt_get_max_pstate,
544                 .get_min = byt_get_min_pstate,
545                 .get_turbo = byt_get_turbo_pstate,
546                 .set = byt_set_pstate,
547                 .get_scaling = byt_get_scaling,
548                 .get_vid = byt_get_vid,
549         },
550 };
551
552 static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
553 {
554         int max_perf = cpu->pstate.turbo_pstate;
555         int max_perf_adj;
556         int min_perf;
557
558         if (limits.no_turbo || limits.turbo_disabled)
559                 max_perf = cpu->pstate.max_pstate;
560
561         max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
562         *max = clamp_t(int, max_perf_adj,
563                         cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
564
565         min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
566         *min = clamp_t(int, min_perf, cpu->pstate.min_pstate, max_perf);
567 }
568
569 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
570 {
571         int max_perf, min_perf;
572
573         update_turbo_state();
574
575         intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
576
577         pstate = clamp_t(int, pstate, min_perf, max_perf);
578
579         if (pstate == cpu->pstate.current_pstate)
580                 return;
581
582         trace_cpu_frequency(pstate * cpu->pstate.scaling, cpu->cpu);
583
584         cpu->pstate.current_pstate = pstate;
585
586         pstate_funcs.set(cpu, pstate);
587 }
588
589 static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
590 {
591         cpu->pstate.min_pstate = pstate_funcs.get_min();
592         cpu->pstate.max_pstate = pstate_funcs.get_max();
593         cpu->pstate.turbo_pstate = pstate_funcs.get_turbo();
594         cpu->pstate.scaling = pstate_funcs.get_scaling();
595
596         if (pstate_funcs.get_vid)
597                 pstate_funcs.get_vid(cpu);
598         intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
599 }
600
601 static inline void intel_pstate_calc_busy(struct cpudata *cpu)
602 {
603         struct sample *sample = &cpu->sample;
604         int64_t core_pct;
605
606         core_pct = int_tofp(sample->aperf) * int_tofp(100);
607         core_pct = div64_u64(core_pct, int_tofp(sample->mperf));
608
609         sample->freq = fp_toint(
610                 mul_fp(int_tofp(
611                         cpu->pstate.max_pstate * cpu->pstate.scaling / 100),
612                         core_pct));
613
614         sample->core_pct_busy = (int32_t)core_pct;
615 }
616
617 static inline void intel_pstate_sample(struct cpudata *cpu)
618 {
619         u64 aperf, mperf;
620         unsigned long flags;
621
622         local_irq_save(flags);
623         rdmsrl(MSR_IA32_APERF, aperf);
624         rdmsrl(MSR_IA32_MPERF, mperf);
625         local_irq_restore(flags);
626
627         cpu->last_sample_time = cpu->sample.time;
628         cpu->sample.time = ktime_get();
629         cpu->sample.aperf = aperf;
630         cpu->sample.mperf = mperf;
631         cpu->sample.aperf -= cpu->prev_aperf;
632         cpu->sample.mperf -= cpu->prev_mperf;
633
634         intel_pstate_calc_busy(cpu);
635
636         cpu->prev_aperf = aperf;
637         cpu->prev_mperf = mperf;
638 }
639
640 static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
641 {
642         int delay;
643
644         delay = msecs_to_jiffies(pid_params.sample_rate_ms);
645         mod_timer_pinned(&cpu->timer, jiffies + delay);
646 }
647
648 static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
649 {
650         int32_t core_busy, max_pstate, current_pstate, sample_ratio;
651         u32 duration_us;
652         u32 sample_time;
653
654         core_busy = cpu->sample.core_pct_busy;
655         max_pstate = int_tofp(cpu->pstate.max_pstate);
656         current_pstate = int_tofp(cpu->pstate.current_pstate);
657         core_busy = mul_fp(core_busy, div_fp(max_pstate, current_pstate));
658
659         sample_time = pid_params.sample_rate_ms  * USEC_PER_MSEC;
660         duration_us = (u32) ktime_us_delta(cpu->sample.time,
661                                            cpu->last_sample_time);
662         if (duration_us > sample_time * 3) {
663                 sample_ratio = div_fp(int_tofp(sample_time),
664                                       int_tofp(duration_us));
665                 core_busy = mul_fp(core_busy, sample_ratio);
666         }
667
668         return core_busy;
669 }
670
671 static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
672 {
673         int32_t busy_scaled;
674         struct _pid *pid;
675         signed int ctl;
676
677         pid = &cpu->pid;
678         busy_scaled = intel_pstate_get_scaled_busy(cpu);
679
680         ctl = pid_calc(pid, busy_scaled);
681
682         /* Negative values of ctl increase the pstate and vice versa */
683         intel_pstate_set_pstate(cpu, cpu->pstate.current_pstate - ctl);
684 }
685
686 static void intel_pstate_timer_func(unsigned long __data)
687 {
688         struct cpudata *cpu = (struct cpudata *) __data;
689         struct sample *sample;
690
691         intel_pstate_sample(cpu);
692
693         sample = &cpu->sample;
694
695         intel_pstate_adjust_busy_pstate(cpu);
696
697         trace_pstate_sample(fp_toint(sample->core_pct_busy),
698                         fp_toint(intel_pstate_get_scaled_busy(cpu)),
699                         cpu->pstate.current_pstate,
700                         sample->mperf,
701                         sample->aperf,
702                         sample->freq);
703
704         intel_pstate_set_sample_time(cpu);
705 }
706
707 #define ICPU(model, policy) \
708         { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
709                         (unsigned long)&policy }
710
711 static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
712         ICPU(0x2a, core_params),
713         ICPU(0x2d, core_params),
714         ICPU(0x37, byt_params),
715         ICPU(0x3a, core_params),
716         ICPU(0x3c, core_params),
717         ICPU(0x3d, core_params),
718         ICPU(0x3e, core_params),
719         ICPU(0x3f, core_params),
720         ICPU(0x45, core_params),
721         ICPU(0x46, core_params),
722         ICPU(0x4c, byt_params),
723         ICPU(0x4f, core_params),
724         ICPU(0x56, core_params),
725         {}
726 };
727 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
728
729 static int intel_pstate_init_cpu(unsigned int cpunum)
730 {
731         struct cpudata *cpu;
732
733         if (!all_cpu_data[cpunum])
734                 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata),
735                                                GFP_KERNEL);
736         if (!all_cpu_data[cpunum])
737                 return -ENOMEM;
738
739         cpu = all_cpu_data[cpunum];
740
741         cpu->cpu = cpunum;
742         intel_pstate_get_cpu_pstates(cpu);
743
744         init_timer_deferrable(&cpu->timer);
745         cpu->timer.function = intel_pstate_timer_func;
746         cpu->timer.data = (unsigned long)cpu;
747         cpu->timer.expires = jiffies + HZ/100;
748         intel_pstate_busy_pid_reset(cpu);
749         intel_pstate_sample(cpu);
750
751         add_timer_on(&cpu->timer, cpunum);
752
753         pr_debug("Intel pstate controlling: cpu %d\n", cpunum);
754
755         return 0;
756 }
757
758 static unsigned int intel_pstate_get(unsigned int cpu_num)
759 {
760         struct sample *sample;
761         struct cpudata *cpu;
762
763         cpu = all_cpu_data[cpu_num];
764         if (!cpu)
765                 return 0;
766         sample = &cpu->sample;
767         return sample->freq;
768 }
769
770 static int intel_pstate_set_policy(struct cpufreq_policy *policy)
771 {
772         if (!policy->cpuinfo.max_freq)
773                 return -ENODEV;
774
775         if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
776                 limits.min_perf_pct = 100;
777                 limits.min_perf = int_tofp(1);
778                 limits.max_policy_pct = 100;
779                 limits.max_perf_pct = 100;
780                 limits.max_perf = int_tofp(1);
781                 limits.no_turbo = 0;
782                 return 0;
783         }
784         limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
785         limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
786         limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
787
788         limits.max_policy_pct = (policy->max * 100) / policy->cpuinfo.max_freq;
789         limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
790         limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
791         limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
792
793         return 0;
794 }
795
796 static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
797 {
798         cpufreq_verify_within_cpu_limits(policy);
799
800         if (policy->policy != CPUFREQ_POLICY_POWERSAVE &&
801             policy->policy != CPUFREQ_POLICY_PERFORMANCE)
802                 return -EINVAL;
803
804         return 0;
805 }
806
807 static void intel_pstate_stop_cpu(struct cpufreq_policy *policy)
808 {
809         int cpu_num = policy->cpu;
810         struct cpudata *cpu = all_cpu_data[cpu_num];
811
812         pr_info("intel_pstate CPU %d exiting\n", cpu_num);
813
814         del_timer_sync(&all_cpu_data[cpu_num]->timer);
815         intel_pstate_set_pstate(cpu, cpu->pstate.min_pstate);
816 }
817
818 static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
819 {
820         struct cpudata *cpu;
821         int rc;
822
823         rc = intel_pstate_init_cpu(policy->cpu);
824         if (rc)
825                 return rc;
826
827         cpu = all_cpu_data[policy->cpu];
828
829         if (limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
830                 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
831         else
832                 policy->policy = CPUFREQ_POLICY_POWERSAVE;
833
834         policy->min = cpu->pstate.min_pstate * cpu->pstate.scaling;
835         policy->max = cpu->pstate.turbo_pstate * cpu->pstate.scaling;
836
837         /* cpuinfo and default policy values */
838         policy->cpuinfo.min_freq = cpu->pstate.min_pstate * cpu->pstate.scaling;
839         policy->cpuinfo.max_freq =
840                 cpu->pstate.turbo_pstate * cpu->pstate.scaling;
841         policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
842         cpumask_set_cpu(policy->cpu, policy->cpus);
843
844         return 0;
845 }
846
847 static struct cpufreq_driver intel_pstate_driver = {
848         .flags          = CPUFREQ_CONST_LOOPS,
849         .verify         = intel_pstate_verify_policy,
850         .setpolicy      = intel_pstate_set_policy,
851         .get            = intel_pstate_get,
852         .init           = intel_pstate_cpu_init,
853         .stop_cpu       = intel_pstate_stop_cpu,
854         .name           = "intel_pstate",
855 };
856
857 static int __initdata no_load;
858
859 static int intel_pstate_msrs_not_valid(void)
860 {
861         /* Check that all the msr's we are using are valid. */
862         u64 aperf, mperf, tmp;
863
864         rdmsrl(MSR_IA32_APERF, aperf);
865         rdmsrl(MSR_IA32_MPERF, mperf);
866
867         if (!pstate_funcs.get_max() ||
868             !pstate_funcs.get_min() ||
869             !pstate_funcs.get_turbo())
870                 return -ENODEV;
871
872         rdmsrl(MSR_IA32_APERF, tmp);
873         if (!(tmp - aperf))
874                 return -ENODEV;
875
876         rdmsrl(MSR_IA32_MPERF, tmp);
877         if (!(tmp - mperf))
878                 return -ENODEV;
879
880         return 0;
881 }
882
883 static void copy_pid_params(struct pstate_adjust_policy *policy)
884 {
885         pid_params.sample_rate_ms = policy->sample_rate_ms;
886         pid_params.p_gain_pct = policy->p_gain_pct;
887         pid_params.i_gain_pct = policy->i_gain_pct;
888         pid_params.d_gain_pct = policy->d_gain_pct;
889         pid_params.deadband = policy->deadband;
890         pid_params.setpoint = policy->setpoint;
891 }
892
893 static void copy_cpu_funcs(struct pstate_funcs *funcs)
894 {
895         pstate_funcs.get_max   = funcs->get_max;
896         pstate_funcs.get_min   = funcs->get_min;
897         pstate_funcs.get_turbo = funcs->get_turbo;
898         pstate_funcs.get_scaling = funcs->get_scaling;
899         pstate_funcs.set       = funcs->set;
900         pstate_funcs.get_vid   = funcs->get_vid;
901 }
902
903 #if IS_ENABLED(CONFIG_ACPI)
904 #include <acpi/processor.h>
905
906 static bool intel_pstate_no_acpi_pss(void)
907 {
908         int i;
909
910         for_each_possible_cpu(i) {
911                 acpi_status status;
912                 union acpi_object *pss;
913                 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
914                 struct acpi_processor *pr = per_cpu(processors, i);
915
916                 if (!pr)
917                         continue;
918
919                 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
920                 if (ACPI_FAILURE(status))
921                         continue;
922
923                 pss = buffer.pointer;
924                 if (pss && pss->type == ACPI_TYPE_PACKAGE) {
925                         kfree(pss);
926                         return false;
927                 }
928
929                 kfree(pss);
930         }
931
932         return true;
933 }
934
935 struct hw_vendor_info {
936         u16  valid;
937         char oem_id[ACPI_OEM_ID_SIZE];
938         char oem_table_id[ACPI_OEM_TABLE_ID_SIZE];
939 };
940
941 /* Hardware vendor-specific info that has its own power management modes */
942 static struct hw_vendor_info vendor_info[] = {
943         {1, "HP    ", "ProLiant"},
944         {0, "", ""},
945 };
946
947 static bool intel_pstate_platform_pwr_mgmt_exists(void)
948 {
949         struct acpi_table_header hdr;
950         struct hw_vendor_info *v_info;
951
952         if (acpi_disabled ||
953             ACPI_FAILURE(acpi_get_table_header(ACPI_SIG_FADT, 0, &hdr)))
954                 return false;
955
956         for (v_info = vendor_info; v_info->valid; v_info++) {
957                 if (!strncmp(hdr.oem_id, v_info->oem_id, ACPI_OEM_ID_SIZE) &&
958                     !strncmp(hdr.oem_table_id, v_info->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
959                     intel_pstate_no_acpi_pss())
960                         return true;
961         }
962
963         return false;
964 }
965 #else /* CONFIG_ACPI not enabled */
966 static inline bool intel_pstate_platform_pwr_mgmt_exists(void) { return false; }
967 #endif /* CONFIG_ACPI */
968
969 static int __init intel_pstate_init(void)
970 {
971         int cpu, rc = 0;
972         const struct x86_cpu_id *id;
973         struct cpu_defaults *cpu_info;
974
975         if (no_load)
976                 return -ENODEV;
977
978         id = x86_match_cpu(intel_pstate_cpu_ids);
979         if (!id)
980                 return -ENODEV;
981
982         /*
983          * The Intel pstate driver will be ignored if the platform
984          * firmware has its own power management modes.
985          */
986         if (intel_pstate_platform_pwr_mgmt_exists())
987                 return -ENODEV;
988
989         cpu_info = (struct cpu_defaults *)id->driver_data;
990
991         copy_pid_params(&cpu_info->pid_policy);
992         copy_cpu_funcs(&cpu_info->funcs);
993
994         if (intel_pstate_msrs_not_valid())
995                 return -ENODEV;
996
997         pr_info("Intel P-state driver initializing.\n");
998
999         all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
1000         if (!all_cpu_data)
1001                 return -ENOMEM;
1002
1003         rc = cpufreq_register_driver(&intel_pstate_driver);
1004         if (rc)
1005                 goto out;
1006
1007         intel_pstate_debug_expose_params();
1008         intel_pstate_sysfs_expose_params();
1009
1010         return rc;
1011 out:
1012         get_online_cpus();
1013         for_each_online_cpu(cpu) {
1014                 if (all_cpu_data[cpu]) {
1015                         del_timer_sync(&all_cpu_data[cpu]->timer);
1016                         kfree(all_cpu_data[cpu]);
1017                 }
1018         }
1019
1020         put_online_cpus();
1021         vfree(all_cpu_data);
1022         return -ENODEV;
1023 }
1024 device_initcall(intel_pstate_init);
1025
1026 static int __init intel_pstate_setup(char *str)
1027 {
1028         if (!str)
1029                 return -EINVAL;
1030
1031         if (!strcmp(str, "disable"))
1032                 no_load = 1;
1033         return 0;
1034 }
1035 early_param("intel_pstate", intel_pstate_setup);
1036
1037 MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
1038 MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
1039 MODULE_LICENSE("GPL");