hpsa: fix bad -ENOMEM return value in hpsa_big_passthru_ioctl
[linux/fpc-iii.git] / drivers / cpufreq / intel_pstate.c
blobf033fadb58e6b535573ac7cc5654290798a0a8cd
1 /*
2 * intel_pstate.c: Native P state management for Intel processors
4 * (C) Copyright 2012 Intel Corporation
5 * Author: Dirk Brandewie <dirk.j.brandewie@intel.com>
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.
13 #include <linux/kernel.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/module.h>
16 #include <linux/ktime.h>
17 #include <linux/hrtimer.h>
18 #include <linux/tick.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include <linux/list.h>
22 #include <linux/cpu.h>
23 #include <linux/cpufreq.h>
24 #include <linux/sysfs.h>
25 #include <linux/types.h>
26 #include <linux/fs.h>
27 #include <linux/debugfs.h>
28 #include <trace/events/power.h>
30 #include <asm/div64.h>
31 #include <asm/msr.h>
32 #include <asm/cpu_device_id.h>
34 #define SAMPLE_COUNT 3
36 #define FRAC_BITS 8
37 #define int_tofp(X) ((int64_t)(X) << FRAC_BITS)
38 #define fp_toint(X) ((X) >> FRAC_BITS)
40 static inline int32_t mul_fp(int32_t x, int32_t y)
42 return ((int64_t)x * (int64_t)y) >> FRAC_BITS;
45 static inline int32_t div_fp(int32_t x, int32_t y)
47 return div_s64((int64_t)x << FRAC_BITS, (int64_t)y);
50 struct sample {
51 int32_t core_pct_busy;
52 u64 aperf;
53 u64 mperf;
54 int freq;
57 struct pstate_data {
58 int current_pstate;
59 int min_pstate;
60 int max_pstate;
61 int turbo_pstate;
64 struct _pid {
65 int setpoint;
66 int32_t integral;
67 int32_t p_gain;
68 int32_t i_gain;
69 int32_t d_gain;
70 int deadband;
71 int32_t last_err;
74 struct cpudata {
75 int cpu;
77 char name[64];
79 struct timer_list timer;
81 struct pstate_adjust_policy *pstate_policy;
82 struct pstate_data pstate;
83 struct _pid pid;
85 int min_pstate_count;
87 u64 prev_aperf;
88 u64 prev_mperf;
89 int sample_ptr;
90 struct sample samples[SAMPLE_COUNT];
93 static struct cpudata **all_cpu_data;
94 struct pstate_adjust_policy {
95 int sample_rate_ms;
96 int deadband;
97 int setpoint;
98 int p_gain_pct;
99 int d_gain_pct;
100 int i_gain_pct;
103 static struct pstate_adjust_policy default_policy = {
104 .sample_rate_ms = 10,
105 .deadband = 0,
106 .setpoint = 97,
107 .p_gain_pct = 20,
108 .d_gain_pct = 0,
109 .i_gain_pct = 0,
112 struct perf_limits {
113 int no_turbo;
114 int max_perf_pct;
115 int min_perf_pct;
116 int32_t max_perf;
117 int32_t min_perf;
118 int max_policy_pct;
119 int max_sysfs_pct;
122 static struct perf_limits limits = {
123 .no_turbo = 0,
124 .max_perf_pct = 100,
125 .max_perf = int_tofp(1),
126 .min_perf_pct = 0,
127 .min_perf = 0,
128 .max_policy_pct = 100,
129 .max_sysfs_pct = 100,
132 static inline void pid_reset(struct _pid *pid, int setpoint, int busy,
133 int deadband, int integral) {
134 pid->setpoint = setpoint;
135 pid->deadband = deadband;
136 pid->integral = int_tofp(integral);
137 pid->last_err = setpoint - busy;
140 static inline void pid_p_gain_set(struct _pid *pid, int percent)
142 pid->p_gain = div_fp(int_tofp(percent), int_tofp(100));
145 static inline void pid_i_gain_set(struct _pid *pid, int percent)
147 pid->i_gain = div_fp(int_tofp(percent), int_tofp(100));
150 static inline void pid_d_gain_set(struct _pid *pid, int percent)
153 pid->d_gain = div_fp(int_tofp(percent), int_tofp(100));
156 static signed int pid_calc(struct _pid *pid, int32_t busy)
158 signed int result;
159 int32_t pterm, dterm, fp_error;
160 int32_t integral_limit;
162 fp_error = int_tofp(pid->setpoint) - busy;
164 if (abs(fp_error) <= int_tofp(pid->deadband))
165 return 0;
167 pterm = mul_fp(pid->p_gain, fp_error);
169 pid->integral += fp_error;
171 /* limit the integral term */
172 integral_limit = int_tofp(30);
173 if (pid->integral > integral_limit)
174 pid->integral = integral_limit;
175 if (pid->integral < -integral_limit)
176 pid->integral = -integral_limit;
178 dterm = mul_fp(pid->d_gain, fp_error - pid->last_err);
179 pid->last_err = fp_error;
181 result = pterm + mul_fp(pid->integral, pid->i_gain) + dterm;
183 return (signed int)fp_toint(result);
186 static inline void intel_pstate_busy_pid_reset(struct cpudata *cpu)
188 pid_p_gain_set(&cpu->pid, cpu->pstate_policy->p_gain_pct);
189 pid_d_gain_set(&cpu->pid, cpu->pstate_policy->d_gain_pct);
190 pid_i_gain_set(&cpu->pid, cpu->pstate_policy->i_gain_pct);
192 pid_reset(&cpu->pid,
193 cpu->pstate_policy->setpoint,
194 100,
195 cpu->pstate_policy->deadband,
199 static inline void intel_pstate_reset_all_pid(void)
201 unsigned int cpu;
202 for_each_online_cpu(cpu) {
203 if (all_cpu_data[cpu])
204 intel_pstate_busy_pid_reset(all_cpu_data[cpu]);
208 /************************** debugfs begin ************************/
209 static int pid_param_set(void *data, u64 val)
211 *(u32 *)data = val;
212 intel_pstate_reset_all_pid();
213 return 0;
215 static int pid_param_get(void *data, u64 *val)
217 *val = *(u32 *)data;
218 return 0;
220 DEFINE_SIMPLE_ATTRIBUTE(fops_pid_param, pid_param_get,
221 pid_param_set, "%llu\n");
223 struct pid_param {
224 char *name;
225 void *value;
228 static struct pid_param pid_files[] = {
229 {"sample_rate_ms", &default_policy.sample_rate_ms},
230 {"d_gain_pct", &default_policy.d_gain_pct},
231 {"i_gain_pct", &default_policy.i_gain_pct},
232 {"deadband", &default_policy.deadband},
233 {"setpoint", &default_policy.setpoint},
234 {"p_gain_pct", &default_policy.p_gain_pct},
235 {NULL, NULL}
238 static struct dentry *debugfs_parent;
239 static void intel_pstate_debug_expose_params(void)
241 int i = 0;
243 debugfs_parent = debugfs_create_dir("pstate_snb", NULL);
244 if (IS_ERR_OR_NULL(debugfs_parent))
245 return;
246 while (pid_files[i].name) {
247 debugfs_create_file(pid_files[i].name, 0660,
248 debugfs_parent, pid_files[i].value,
249 &fops_pid_param);
250 i++;
254 /************************** debugfs end ************************/
256 /************************** sysfs begin ************************/
257 #define show_one(file_name, object) \
258 static ssize_t show_##file_name \
259 (struct kobject *kobj, struct attribute *attr, char *buf) \
261 return sprintf(buf, "%u\n", limits.object); \
264 static ssize_t store_no_turbo(struct kobject *a, struct attribute *b,
265 const char *buf, size_t count)
267 unsigned int input;
268 int ret;
269 ret = sscanf(buf, "%u", &input);
270 if (ret != 1)
271 return -EINVAL;
272 limits.no_turbo = clamp_t(int, input, 0 , 1);
274 return count;
277 static ssize_t store_max_perf_pct(struct kobject *a, struct attribute *b,
278 const char *buf, size_t count)
280 unsigned int input;
281 int ret;
282 ret = sscanf(buf, "%u", &input);
283 if (ret != 1)
284 return -EINVAL;
286 limits.max_sysfs_pct = clamp_t(int, input, 0 , 100);
287 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
288 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
289 return count;
292 static ssize_t store_min_perf_pct(struct kobject *a, struct attribute *b,
293 const char *buf, size_t count)
295 unsigned int input;
296 int ret;
297 ret = sscanf(buf, "%u", &input);
298 if (ret != 1)
299 return -EINVAL;
300 limits.min_perf_pct = clamp_t(int, input, 0 , 100);
301 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
303 return count;
306 show_one(no_turbo, no_turbo);
307 show_one(max_perf_pct, max_perf_pct);
308 show_one(min_perf_pct, min_perf_pct);
310 define_one_global_rw(no_turbo);
311 define_one_global_rw(max_perf_pct);
312 define_one_global_rw(min_perf_pct);
314 static struct attribute *intel_pstate_attributes[] = {
315 &no_turbo.attr,
316 &max_perf_pct.attr,
317 &min_perf_pct.attr,
318 NULL
321 static struct attribute_group intel_pstate_attr_group = {
322 .attrs = intel_pstate_attributes,
324 static struct kobject *intel_pstate_kobject;
326 static void intel_pstate_sysfs_expose_params(void)
328 int rc;
330 intel_pstate_kobject = kobject_create_and_add("intel_pstate",
331 &cpu_subsys.dev_root->kobj);
332 BUG_ON(!intel_pstate_kobject);
333 rc = sysfs_create_group(intel_pstate_kobject,
334 &intel_pstate_attr_group);
335 BUG_ON(rc);
338 /************************** sysfs end ************************/
340 static int intel_pstate_min_pstate(void)
342 u64 value;
343 rdmsrl(MSR_PLATFORM_INFO, value);
344 return (value >> 40) & 0xFF;
347 static int intel_pstate_max_pstate(void)
349 u64 value;
350 rdmsrl(MSR_PLATFORM_INFO, value);
351 return (value >> 8) & 0xFF;
354 static int intel_pstate_turbo_pstate(void)
356 u64 value;
357 int nont, ret;
358 rdmsrl(MSR_NHM_TURBO_RATIO_LIMIT, value);
359 nont = intel_pstate_max_pstate();
360 ret = ((value) & 255);
361 if (ret <= nont)
362 ret = nont;
363 return ret;
366 static void intel_pstate_get_min_max(struct cpudata *cpu, int *min, int *max)
368 int max_perf = cpu->pstate.turbo_pstate;
369 int max_perf_adj;
370 int min_perf;
371 if (limits.no_turbo)
372 max_perf = cpu->pstate.max_pstate;
374 max_perf_adj = fp_toint(mul_fp(int_tofp(max_perf), limits.max_perf));
375 *max = clamp_t(int, max_perf_adj,
376 cpu->pstate.min_pstate, cpu->pstate.turbo_pstate);
378 min_perf = fp_toint(mul_fp(int_tofp(max_perf), limits.min_perf));
379 *min = clamp_t(int, min_perf,
380 cpu->pstate.min_pstate, max_perf);
383 static void intel_pstate_set_pstate(struct cpudata *cpu, int pstate)
385 int max_perf, min_perf;
386 u64 val;
388 intel_pstate_get_min_max(cpu, &min_perf, &max_perf);
390 pstate = clamp_t(int, pstate, min_perf, max_perf);
392 if (pstate == cpu->pstate.current_pstate)
393 return;
395 trace_cpu_frequency(pstate * 100000, cpu->cpu);
397 cpu->pstate.current_pstate = pstate;
398 val = pstate << 8;
399 if (limits.no_turbo)
400 val |= (u64)1 << 32;
402 wrmsrl(MSR_IA32_PERF_CTL, val);
405 static inline void intel_pstate_pstate_increase(struct cpudata *cpu, int steps)
407 int target;
408 target = cpu->pstate.current_pstate + steps;
410 intel_pstate_set_pstate(cpu, target);
413 static inline void intel_pstate_pstate_decrease(struct cpudata *cpu, int steps)
415 int target;
416 target = cpu->pstate.current_pstate - steps;
417 intel_pstate_set_pstate(cpu, target);
420 static void intel_pstate_get_cpu_pstates(struct cpudata *cpu)
422 sprintf(cpu->name, "Intel 2nd generation core");
424 cpu->pstate.min_pstate = intel_pstate_min_pstate();
425 cpu->pstate.max_pstate = intel_pstate_max_pstate();
426 cpu->pstate.turbo_pstate = intel_pstate_turbo_pstate();
429 * goto max pstate so we don't slow up boot if we are built-in if we are
430 * a module we will take care of it during normal operation
432 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
435 static inline void intel_pstate_calc_busy(struct cpudata *cpu,
436 struct sample *sample)
438 u64 core_pct;
439 core_pct = div64_u64(int_tofp(sample->aperf * 100),
440 sample->mperf);
441 sample->freq = fp_toint(cpu->pstate.max_pstate * core_pct * 1000);
443 sample->core_pct_busy = core_pct;
446 static inline void intel_pstate_sample(struct cpudata *cpu)
448 u64 aperf, mperf;
450 rdmsrl(MSR_IA32_APERF, aperf);
451 rdmsrl(MSR_IA32_MPERF, mperf);
452 cpu->sample_ptr = (cpu->sample_ptr + 1) % SAMPLE_COUNT;
453 cpu->samples[cpu->sample_ptr].aperf = aperf;
454 cpu->samples[cpu->sample_ptr].mperf = mperf;
455 cpu->samples[cpu->sample_ptr].aperf -= cpu->prev_aperf;
456 cpu->samples[cpu->sample_ptr].mperf -= cpu->prev_mperf;
458 intel_pstate_calc_busy(cpu, &cpu->samples[cpu->sample_ptr]);
460 cpu->prev_aperf = aperf;
461 cpu->prev_mperf = mperf;
464 static inline void intel_pstate_set_sample_time(struct cpudata *cpu)
466 int sample_time, delay;
468 sample_time = cpu->pstate_policy->sample_rate_ms;
469 delay = msecs_to_jiffies(sample_time);
470 mod_timer_pinned(&cpu->timer, jiffies + delay);
473 static inline int32_t intel_pstate_get_scaled_busy(struct cpudata *cpu)
475 int32_t core_busy, max_pstate, current_pstate;
477 core_busy = cpu->samples[cpu->sample_ptr].core_pct_busy;
478 max_pstate = int_tofp(cpu->pstate.max_pstate);
479 current_pstate = int_tofp(cpu->pstate.current_pstate);
480 return mul_fp(core_busy, div_fp(max_pstate, current_pstate));
483 static inline void intel_pstate_adjust_busy_pstate(struct cpudata *cpu)
485 int32_t busy_scaled;
486 struct _pid *pid;
487 signed int ctl = 0;
488 int steps;
490 pid = &cpu->pid;
491 busy_scaled = intel_pstate_get_scaled_busy(cpu);
493 ctl = pid_calc(pid, busy_scaled);
495 steps = abs(ctl);
496 if (ctl < 0)
497 intel_pstate_pstate_increase(cpu, steps);
498 else
499 intel_pstate_pstate_decrease(cpu, steps);
502 static void intel_pstate_timer_func(unsigned long __data)
504 struct cpudata *cpu = (struct cpudata *) __data;
506 intel_pstate_sample(cpu);
507 intel_pstate_adjust_busy_pstate(cpu);
509 if (cpu->pstate.current_pstate == cpu->pstate.min_pstate) {
510 cpu->min_pstate_count++;
511 if (!(cpu->min_pstate_count % 5)) {
512 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
514 } else
515 cpu->min_pstate_count = 0;
517 intel_pstate_set_sample_time(cpu);
520 #define ICPU(model, policy) \
521 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_APERFMPERF,\
522 (unsigned long)&policy }
524 static const struct x86_cpu_id intel_pstate_cpu_ids[] = {
525 ICPU(0x2a, default_policy),
526 ICPU(0x2d, default_policy),
527 ICPU(0x3a, default_policy),
528 ICPU(0x3c, default_policy),
529 ICPU(0x3e, default_policy),
530 ICPU(0x3f, default_policy),
531 ICPU(0x45, default_policy),
532 ICPU(0x46, default_policy),
535 MODULE_DEVICE_TABLE(x86cpu, intel_pstate_cpu_ids);
537 static int intel_pstate_init_cpu(unsigned int cpunum)
540 const struct x86_cpu_id *id;
541 struct cpudata *cpu;
543 id = x86_match_cpu(intel_pstate_cpu_ids);
544 if (!id)
545 return -ENODEV;
547 all_cpu_data[cpunum] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
548 if (!all_cpu_data[cpunum])
549 return -ENOMEM;
551 cpu = all_cpu_data[cpunum];
553 cpu->cpu = cpunum;
554 intel_pstate_get_cpu_pstates(cpu);
555 if (!cpu->pstate.current_pstate) {
556 all_cpu_data[cpunum] = NULL;
557 kfree(cpu);
558 return -ENODATA;
561 cpu->pstate_policy =
562 (struct pstate_adjust_policy *)id->driver_data;
563 init_timer_deferrable(&cpu->timer);
564 cpu->timer.function = intel_pstate_timer_func;
565 cpu->timer.data =
566 (unsigned long)cpu;
567 cpu->timer.expires = jiffies + HZ/100;
568 intel_pstate_busy_pid_reset(cpu);
569 intel_pstate_sample(cpu);
570 intel_pstate_set_pstate(cpu, cpu->pstate.max_pstate);
572 add_timer_on(&cpu->timer, cpunum);
574 pr_info("Intel pstate controlling: cpu %d\n", cpunum);
576 return 0;
579 static unsigned int intel_pstate_get(unsigned int cpu_num)
581 struct sample *sample;
582 struct cpudata *cpu;
584 cpu = all_cpu_data[cpu_num];
585 if (!cpu)
586 return 0;
587 sample = &cpu->samples[cpu->sample_ptr];
588 return sample->freq;
591 static int intel_pstate_set_policy(struct cpufreq_policy *policy)
593 struct cpudata *cpu;
595 cpu = all_cpu_data[policy->cpu];
597 if (!policy->cpuinfo.max_freq)
598 return -ENODEV;
600 if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
601 limits.min_perf_pct = 100;
602 limits.min_perf = int_tofp(1);
603 limits.max_perf_pct = 100;
604 limits.max_perf = int_tofp(1);
605 limits.no_turbo = 0;
606 return 0;
608 limits.min_perf_pct = (policy->min * 100) / policy->cpuinfo.max_freq;
609 limits.min_perf_pct = clamp_t(int, limits.min_perf_pct, 0 , 100);
610 limits.min_perf = div_fp(int_tofp(limits.min_perf_pct), int_tofp(100));
612 limits.max_policy_pct = policy->max * 100 / policy->cpuinfo.max_freq;
613 limits.max_policy_pct = clamp_t(int, limits.max_policy_pct, 0 , 100);
614 limits.max_perf_pct = min(limits.max_policy_pct, limits.max_sysfs_pct);
615 limits.max_perf = div_fp(int_tofp(limits.max_perf_pct), int_tofp(100));
617 return 0;
620 static int intel_pstate_verify_policy(struct cpufreq_policy *policy)
622 cpufreq_verify_within_limits(policy,
623 policy->cpuinfo.min_freq,
624 policy->cpuinfo.max_freq);
626 if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
627 (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
628 return -EINVAL;
630 return 0;
633 static int intel_pstate_cpu_exit(struct cpufreq_policy *policy)
635 int cpu = policy->cpu;
637 del_timer(&all_cpu_data[cpu]->timer);
638 kfree(all_cpu_data[cpu]);
639 all_cpu_data[cpu] = NULL;
640 return 0;
643 static int intel_pstate_cpu_init(struct cpufreq_policy *policy)
645 struct cpudata *cpu;
646 int rc;
648 rc = intel_pstate_init_cpu(policy->cpu);
649 if (rc)
650 return rc;
652 cpu = all_cpu_data[policy->cpu];
654 if (!limits.no_turbo &&
655 limits.min_perf_pct == 100 && limits.max_perf_pct == 100)
656 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
657 else
658 policy->policy = CPUFREQ_POLICY_POWERSAVE;
660 policy->min = cpu->pstate.min_pstate * 100000;
661 policy->max = cpu->pstate.turbo_pstate * 100000;
663 /* cpuinfo and default policy values */
664 policy->cpuinfo.min_freq = cpu->pstate.min_pstate * 100000;
665 policy->cpuinfo.max_freq = cpu->pstate.turbo_pstate * 100000;
666 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
667 cpumask_set_cpu(policy->cpu, policy->cpus);
669 return 0;
672 static struct cpufreq_driver intel_pstate_driver = {
673 .flags = CPUFREQ_CONST_LOOPS,
674 .verify = intel_pstate_verify_policy,
675 .setpolicy = intel_pstate_set_policy,
676 .get = intel_pstate_get,
677 .init = intel_pstate_cpu_init,
678 .exit = intel_pstate_cpu_exit,
679 .name = "intel_pstate",
682 static int __initdata no_load;
684 static int intel_pstate_msrs_not_valid(void)
686 /* Check that all the msr's we are using are valid. */
687 u64 aperf, mperf, tmp;
689 rdmsrl(MSR_IA32_APERF, aperf);
690 rdmsrl(MSR_IA32_MPERF, mperf);
692 if (!intel_pstate_min_pstate() ||
693 !intel_pstate_max_pstate() ||
694 !intel_pstate_turbo_pstate())
695 return -ENODEV;
697 rdmsrl(MSR_IA32_APERF, tmp);
698 if (!(tmp - aperf))
699 return -ENODEV;
701 rdmsrl(MSR_IA32_MPERF, tmp);
702 if (!(tmp - mperf))
703 return -ENODEV;
705 return 0;
707 static int __init intel_pstate_init(void)
709 int cpu, rc = 0;
710 const struct x86_cpu_id *id;
712 if (no_load)
713 return -ENODEV;
715 id = x86_match_cpu(intel_pstate_cpu_ids);
716 if (!id)
717 return -ENODEV;
719 if (intel_pstate_msrs_not_valid())
720 return -ENODEV;
722 pr_info("Intel P-state driver initializing.\n");
724 all_cpu_data = vzalloc(sizeof(void *) * num_possible_cpus());
725 if (!all_cpu_data)
726 return -ENOMEM;
728 rc = cpufreq_register_driver(&intel_pstate_driver);
729 if (rc)
730 goto out;
732 intel_pstate_debug_expose_params();
733 intel_pstate_sysfs_expose_params();
734 return rc;
735 out:
736 get_online_cpus();
737 for_each_online_cpu(cpu) {
738 if (all_cpu_data[cpu]) {
739 del_timer_sync(&all_cpu_data[cpu]->timer);
740 kfree(all_cpu_data[cpu]);
744 put_online_cpus();
745 vfree(all_cpu_data);
746 return -ENODEV;
748 device_initcall(intel_pstate_init);
750 static int __init intel_pstate_setup(char *str)
752 if (!str)
753 return -EINVAL;
755 if (!strcmp(str, "disable"))
756 no_load = 1;
757 return 0;
759 early_param("intel_pstate", intel_pstate_setup);
761 MODULE_AUTHOR("Dirk Brandewie <dirk.j.brandewie@intel.com>");
762 MODULE_DESCRIPTION("'intel_pstate' - P state driver Intel Core processors");
763 MODULE_LICENSE("GPL");