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