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