2 * drivers/cpufreq/cpufreq_conservative.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com>
7 * (C) 2004 Alexander Clouter <alex-kernel@digriz.org.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/smp.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/ctype.h>
20 #include <linux/cpufreq.h>
21 #include <linux/sysctl.h>
22 #include <linux/types.h>
24 #include <linux/sysfs.h>
25 #include <linux/sched.h>
26 #include <linux/kmod.h>
27 #include <linux/workqueue.h>
28 #include <linux/jiffies.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/percpu.h>
33 * dbs is used in this file as a shortform for demandbased switching
34 * It helps to keep variable names smaller, simpler
37 #define DEF_FREQUENCY_UP_THRESHOLD (80)
38 #define MIN_FREQUENCY_UP_THRESHOLD (0)
39 #define MAX_FREQUENCY_UP_THRESHOLD (100)
41 #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
42 #define MIN_FREQUENCY_DOWN_THRESHOLD (0)
43 #define MAX_FREQUENCY_DOWN_THRESHOLD (100)
46 * The polling frequency of this governor depends on the capability of
47 * the processor. Default polling frequency is 1000 times the transition
48 * latency of the processor. The governor will work on any processor with
49 * transition latency <= 10mS, using appropriate sampling
51 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
52 * this governor will not work.
53 * All times here are in uS.
55 static unsigned int def_sampling_rate;
56 #define MIN_SAMPLING_RATE (def_sampling_rate / 2)
57 #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
58 #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (100000)
59 #define DEF_SAMPLING_DOWN_FACTOR (5)
60 #define TRANSITION_LATENCY_LIMIT (10 * 1000)
62 static void do_dbs_timer(void *data);
64 struct cpu_dbs_info_s {
65 struct cpufreq_policy *cur_policy;
66 unsigned int prev_cpu_idle_up;
67 unsigned int prev_cpu_idle_down;
70 static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
72 static unsigned int dbs_enable; /* number of CPUs using this policy */
74 static DECLARE_MUTEX (dbs_sem);
75 static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
78 unsigned int sampling_rate;
79 unsigned int sampling_down_factor;
80 unsigned int up_threshold;
81 unsigned int down_threshold;
82 unsigned int ignore_nice;
83 unsigned int freq_step;
86 static struct dbs_tuners dbs_tuners_ins = {
87 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
88 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
89 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
92 static inline unsigned int get_cpu_idle_time(unsigned int cpu)
94 return kstat_cpu(cpu).cpustat.idle +
95 kstat_cpu(cpu).cpustat.iowait +
96 ( !dbs_tuners_ins.ignore_nice ?
97 kstat_cpu(cpu).cpustat.nice :
101 /************************** sysfs interface ************************/
102 static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
104 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
107 static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
109 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
112 #define define_one_ro(_name) \
113 static struct freq_attr _name = \
114 __ATTR(_name, 0444, show_##_name, NULL)
116 define_one_ro(sampling_rate_max);
117 define_one_ro(sampling_rate_min);
119 /* cpufreq_conservative Governor Tunables */
120 #define show_one(file_name, object) \
121 static ssize_t show_##file_name \
122 (struct cpufreq_policy *unused, char *buf) \
124 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
126 show_one(sampling_rate, sampling_rate);
127 show_one(sampling_down_factor, sampling_down_factor);
128 show_one(up_threshold, up_threshold);
129 show_one(down_threshold, down_threshold);
130 show_one(ignore_nice, ignore_nice);
131 show_one(freq_step, freq_step);
133 static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
134 const char *buf, size_t count)
138 ret = sscanf (buf, "%u", &input);
143 dbs_tuners_ins.sampling_down_factor = input;
149 static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
150 const char *buf, size_t count)
154 ret = sscanf (buf, "%u", &input);
157 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
162 dbs_tuners_ins.sampling_rate = input;
168 static ssize_t store_up_threshold(struct cpufreq_policy *unused,
169 const char *buf, size_t count)
173 ret = sscanf (buf, "%u", &input);
176 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
177 input < MIN_FREQUENCY_UP_THRESHOLD ||
178 input <= dbs_tuners_ins.down_threshold) {
183 dbs_tuners_ins.up_threshold = input;
189 static ssize_t store_down_threshold(struct cpufreq_policy *unused,
190 const char *buf, size_t count)
194 ret = sscanf (buf, "%u", &input);
197 if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
198 input < MIN_FREQUENCY_DOWN_THRESHOLD ||
199 input >= dbs_tuners_ins.up_threshold) {
204 dbs_tuners_ins.down_threshold = input;
210 static ssize_t store_ignore_nice(struct cpufreq_policy *policy,
211 const char *buf, size_t count)
218 ret = sscanf (buf, "%u", &input);
226 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
230 dbs_tuners_ins.ignore_nice = input;
232 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
233 for_each_online_cpu(j) {
234 struct cpu_dbs_info_s *j_dbs_info;
235 j_dbs_info = &per_cpu(cpu_dbs_info, j);
236 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
237 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
244 static ssize_t store_freq_step(struct cpufreq_policy *policy,
245 const char *buf, size_t count)
250 ret = sscanf (buf, "%u", &input);
258 /* no need to test here if freq_step is zero as the user might actually
259 * want this, they would be crazy though :) */
261 dbs_tuners_ins.freq_step = input;
267 #define define_one_rw(_name) \
268 static struct freq_attr _name = \
269 __ATTR(_name, 0644, show_##_name, store_##_name)
271 define_one_rw(sampling_rate);
272 define_one_rw(sampling_down_factor);
273 define_one_rw(up_threshold);
274 define_one_rw(down_threshold);
275 define_one_rw(ignore_nice);
276 define_one_rw(freq_step);
278 static struct attribute * dbs_attributes[] = {
279 &sampling_rate_max.attr,
280 &sampling_rate_min.attr,
282 &sampling_down_factor.attr,
284 &down_threshold.attr,
290 static struct attribute_group dbs_attr_group = {
291 .attrs = dbs_attributes,
292 .name = "conservative",
295 /************************** sysfs end ************************/
297 static void dbs_check_cpu(int cpu)
299 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
300 unsigned int total_idle_ticks;
301 unsigned int freq_step;
302 unsigned int freq_down_sampling_rate;
303 static int down_skip[NR_CPUS];
304 static int requested_freq[NR_CPUS];
305 static unsigned short init_flag = 0;
306 struct cpu_dbs_info_s *this_dbs_info;
307 struct cpu_dbs_info_s *dbs_info;
309 struct cpufreq_policy *policy;
312 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
313 if (!this_dbs_info->enable)
316 policy = this_dbs_info->cur_policy;
318 if ( init_flag == 0 ) {
319 for ( /* NULL */; init_flag < NR_CPUS; init_flag++ ) {
320 dbs_info = &per_cpu(cpu_dbs_info, init_flag);
321 requested_freq[cpu] = dbs_info->cur_policy->cur;
327 * The default safe range is 20% to 80%
328 * Every sampling_rate, we check
329 * - If current idle time is less than 20%, then we try to
331 * Every sampling_rate*sampling_down_factor, we check
332 * - If current idle time is more than 80%, then we try to
335 * Any frequency increase takes it to the maximum frequency.
336 * Frequency reduction happens at minimum steps of
337 * 5% (default) of max_frequency
340 /* Check for frequency increase */
341 total_idle_ticks = get_cpu_idle_time(cpu);
342 idle_ticks = total_idle_ticks -
343 this_dbs_info->prev_cpu_idle_up;
344 this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
347 for_each_cpu_mask(j, policy->cpus) {
348 unsigned int tmp_idle_ticks;
349 struct cpu_dbs_info_s *j_dbs_info;
354 j_dbs_info = &per_cpu(cpu_dbs_info, j);
355 /* Check for frequency increase */
356 total_idle_ticks = get_cpu_idle_time(j);
357 tmp_idle_ticks = total_idle_ticks -
358 j_dbs_info->prev_cpu_idle_up;
359 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
361 if (tmp_idle_ticks < idle_ticks)
362 idle_ticks = tmp_idle_ticks;
365 /* Scale idle ticks by 100 and compare with up and down ticks */
367 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
368 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
370 if (idle_ticks < up_idle_ticks) {
372 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
373 /* if we are already at full speed then break out early */
374 if (requested_freq[cpu] == policy->max)
377 freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
379 /* max freq cannot be less than 100. But who knows.... */
380 if (unlikely(freq_step == 0))
383 requested_freq[cpu] += freq_step;
384 if (requested_freq[cpu] > policy->max)
385 requested_freq[cpu] = policy->max;
387 __cpufreq_driver_target(policy, requested_freq[cpu],
392 /* Check for frequency decrease */
394 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
397 total_idle_ticks = this_dbs_info->prev_cpu_idle_up;
398 idle_ticks = total_idle_ticks -
399 this_dbs_info->prev_cpu_idle_down;
400 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
402 for_each_cpu_mask(j, policy->cpus) {
403 unsigned int tmp_idle_ticks;
404 struct cpu_dbs_info_s *j_dbs_info;
409 j_dbs_info = &per_cpu(cpu_dbs_info, j);
410 /* Check for frequency increase */
411 total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
412 tmp_idle_ticks = total_idle_ticks -
413 j_dbs_info->prev_cpu_idle_down;
414 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
416 if (tmp_idle_ticks < idle_ticks)
417 idle_ticks = tmp_idle_ticks;
420 /* Scale idle ticks by 100 and compare with up and down ticks */
424 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
425 dbs_tuners_ins.sampling_down_factor;
426 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
427 usecs_to_jiffies(freq_down_sampling_rate);
429 if (idle_ticks > down_idle_ticks ) {
430 /* if we are already at the lowest speed then break out early
431 * or if we 'cannot' reduce the speed as the user might want
432 * freq_step to be zero */
433 if (requested_freq[cpu] == policy->min
434 || dbs_tuners_ins.freq_step == 0)
437 freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
439 /* max freq cannot be less than 100. But who knows.... */
440 if (unlikely(freq_step == 0))
443 requested_freq[cpu] -= freq_step;
444 if (requested_freq[cpu] < policy->min)
445 requested_freq[cpu] = policy->min;
447 __cpufreq_driver_target(policy,
454 static void do_dbs_timer(void *data)
458 for_each_online_cpu(i)
460 schedule_delayed_work(&dbs_work,
461 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
465 static inline void dbs_timer_init(void)
467 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
468 schedule_delayed_work(&dbs_work,
469 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
473 static inline void dbs_timer_exit(void)
475 cancel_delayed_work(&dbs_work);
479 static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
482 unsigned int cpu = policy->cpu;
483 struct cpu_dbs_info_s *this_dbs_info;
486 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
489 case CPUFREQ_GOV_START:
490 if ((!cpu_online(cpu)) ||
494 if (policy->cpuinfo.transition_latency >
495 (TRANSITION_LATENCY_LIMIT * 1000))
497 if (this_dbs_info->enable) /* Already enabled */
501 for_each_cpu_mask(j, policy->cpus) {
502 struct cpu_dbs_info_s *j_dbs_info;
503 j_dbs_info = &per_cpu(cpu_dbs_info, j);
504 j_dbs_info->cur_policy = policy;
506 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
507 j_dbs_info->prev_cpu_idle_down
508 = j_dbs_info->prev_cpu_idle_up;
510 this_dbs_info->enable = 1;
511 sysfs_create_group(&policy->kobj, &dbs_attr_group);
514 * Start the timerschedule work, when this governor
515 * is used for first time
517 if (dbs_enable == 1) {
518 unsigned int latency;
519 /* policy latency is in nS. Convert it to uS first */
521 latency = policy->cpuinfo.transition_latency;
525 def_sampling_rate = (latency / 1000) *
526 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
527 dbs_tuners_ins.sampling_rate = def_sampling_rate;
528 dbs_tuners_ins.ignore_nice = 0;
529 dbs_tuners_ins.freq_step = 5;
537 case CPUFREQ_GOV_STOP:
539 this_dbs_info->enable = 0;
540 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
543 * Stop the timerschedule work, when this governor
544 * is used for first time
553 case CPUFREQ_GOV_LIMITS:
555 if (policy->max < this_dbs_info->cur_policy->cur)
556 __cpufreq_driver_target(
557 this_dbs_info->cur_policy,
558 policy->max, CPUFREQ_RELATION_H);
559 else if (policy->min > this_dbs_info->cur_policy->cur)
560 __cpufreq_driver_target(
561 this_dbs_info->cur_policy,
562 policy->min, CPUFREQ_RELATION_L);
569 static struct cpufreq_governor cpufreq_gov_dbs = {
570 .name = "conservative",
571 .governor = cpufreq_governor_dbs,
572 .owner = THIS_MODULE,
575 static int __init cpufreq_gov_dbs_init(void)
577 return cpufreq_register_governor(&cpufreq_gov_dbs);
580 static void __exit cpufreq_gov_dbs_exit(void)
582 /* Make sure that the scheduled work is indeed not running */
583 flush_scheduled_work();
585 cpufreq_unregister_governor(&cpufreq_gov_dbs);
589 MODULE_AUTHOR ("Alexander Clouter <alex-kernel@digriz.org.uk>");
590 MODULE_DESCRIPTION ("'cpufreq_conservative' - A dynamic cpufreq governor for "
591 "Low Latency Frequency Transition capable processors "
592 "optimised for use in a battery environment");
593 MODULE_LICENSE ("GPL");
595 module_init(cpufreq_gov_dbs_init);
596 module_exit(cpufreq_gov_dbs_exit);