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>
31 #include <linux/mutex.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 DEF_FREQUENCY_DOWN_THRESHOLD (20)
41 * The polling frequency of this governor depends on the capability of
42 * the processor. Default polling frequency is 1000 times the transition
43 * latency of the processor. The governor will work on any processor with
44 * transition latency <= 10mS, using appropriate sampling
46 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
47 * this governor will not work.
48 * All times here are in uS.
50 static unsigned int def_sampling_rate;
51 #define MIN_SAMPLING_RATE_RATIO (2)
52 /* for correct statistics, we need at least 10 ticks between each measure */
53 #define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
54 #define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
55 #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
56 #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
57 #define DEF_SAMPLING_DOWN_FACTOR (1)
58 #define MAX_SAMPLING_DOWN_FACTOR (10)
59 #define TRANSITION_LATENCY_LIMIT (10 * 1000)
61 static void do_dbs_timer(void *data);
63 struct cpu_dbs_info_s {
64 struct cpufreq_policy *cur_policy;
65 unsigned int prev_cpu_idle_up;
66 unsigned int prev_cpu_idle_down;
69 static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
71 static unsigned int dbs_enable; /* number of CPUs using this policy */
73 static DEFINE_MUTEX (dbs_mutex);
74 static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
77 unsigned int sampling_rate;
78 unsigned int sampling_down_factor;
79 unsigned int up_threshold;
80 unsigned int down_threshold;
81 unsigned int ignore_nice;
82 unsigned int freq_step;
85 static struct dbs_tuners dbs_tuners_ins = {
86 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
87 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
88 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
91 static inline unsigned int get_cpu_idle_time(unsigned int cpu)
93 return kstat_cpu(cpu).cpustat.idle +
94 kstat_cpu(cpu).cpustat.iowait +
95 ( dbs_tuners_ins.ignore_nice ?
96 kstat_cpu(cpu).cpustat.nice :
100 /************************** sysfs interface ************************/
101 static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
103 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
106 static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
108 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
111 #define define_one_ro(_name) \
112 static struct freq_attr _name = \
113 __ATTR(_name, 0444, show_##_name, NULL)
115 define_one_ro(sampling_rate_max);
116 define_one_ro(sampling_rate_min);
118 /* cpufreq_conservative Governor Tunables */
119 #define show_one(file_name, object) \
120 static ssize_t show_##file_name \
121 (struct cpufreq_policy *unused, char *buf) \
123 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
125 show_one(sampling_rate, sampling_rate);
126 show_one(sampling_down_factor, sampling_down_factor);
127 show_one(up_threshold, up_threshold);
128 show_one(down_threshold, down_threshold);
129 show_one(ignore_nice_load, ignore_nice);
130 show_one(freq_step, freq_step);
132 static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
133 const char *buf, size_t count)
137 ret = sscanf (buf, "%u", &input);
138 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
141 mutex_lock(&dbs_mutex);
142 dbs_tuners_ins.sampling_down_factor = input;
143 mutex_unlock(&dbs_mutex);
148 static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
149 const char *buf, size_t count)
153 ret = sscanf (buf, "%u", &input);
155 mutex_lock(&dbs_mutex);
156 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
157 mutex_unlock(&dbs_mutex);
161 dbs_tuners_ins.sampling_rate = input;
162 mutex_unlock(&dbs_mutex);
167 static ssize_t store_up_threshold(struct cpufreq_policy *unused,
168 const char *buf, size_t count)
172 ret = sscanf (buf, "%u", &input);
174 mutex_lock(&dbs_mutex);
175 if (ret != 1 || input > 100 || input < 0 ||
176 input <= dbs_tuners_ins.down_threshold) {
177 mutex_unlock(&dbs_mutex);
181 dbs_tuners_ins.up_threshold = input;
182 mutex_unlock(&dbs_mutex);
187 static ssize_t store_down_threshold(struct cpufreq_policy *unused,
188 const char *buf, size_t count)
192 ret = sscanf (buf, "%u", &input);
194 mutex_lock(&dbs_mutex);
195 if (ret != 1 || input > 100 || input < 0 ||
196 input >= dbs_tuners_ins.up_threshold) {
197 mutex_unlock(&dbs_mutex);
201 dbs_tuners_ins.down_threshold = input;
202 mutex_unlock(&dbs_mutex);
207 static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
208 const char *buf, size_t count)
215 ret = sscanf (buf, "%u", &input);
222 mutex_lock(&dbs_mutex);
223 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
224 mutex_unlock(&dbs_mutex);
227 dbs_tuners_ins.ignore_nice = input;
229 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
230 for_each_online_cpu(j) {
231 struct cpu_dbs_info_s *j_dbs_info;
232 j_dbs_info = &per_cpu(cpu_dbs_info, j);
233 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
234 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
236 mutex_unlock(&dbs_mutex);
241 static ssize_t store_freq_step(struct cpufreq_policy *policy,
242 const char *buf, size_t count)
247 ret = sscanf (buf, "%u", &input);
255 /* no need to test here if freq_step is zero as the user might actually
256 * want this, they would be crazy though :) */
257 mutex_lock(&dbs_mutex);
258 dbs_tuners_ins.freq_step = input;
259 mutex_unlock(&dbs_mutex);
264 #define define_one_rw(_name) \
265 static struct freq_attr _name = \
266 __ATTR(_name, 0644, show_##_name, store_##_name)
268 define_one_rw(sampling_rate);
269 define_one_rw(sampling_down_factor);
270 define_one_rw(up_threshold);
271 define_one_rw(down_threshold);
272 define_one_rw(ignore_nice_load);
273 define_one_rw(freq_step);
275 static struct attribute * dbs_attributes[] = {
276 &sampling_rate_max.attr,
277 &sampling_rate_min.attr,
279 &sampling_down_factor.attr,
281 &down_threshold.attr,
282 &ignore_nice_load.attr,
287 static struct attribute_group dbs_attr_group = {
288 .attrs = dbs_attributes,
289 .name = "conservative",
292 /************************** sysfs end ************************/
294 static void dbs_check_cpu(int cpu)
296 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
297 unsigned int freq_step;
298 unsigned int freq_down_sampling_rate;
299 static int down_skip[NR_CPUS];
300 static int requested_freq[NR_CPUS];
301 static unsigned short init_flag = 0;
302 struct cpu_dbs_info_s *this_dbs_info;
303 struct cpu_dbs_info_s *dbs_info;
305 struct cpufreq_policy *policy;
308 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
309 if (!this_dbs_info->enable)
312 policy = this_dbs_info->cur_policy;
314 if ( init_flag == 0 ) {
315 for_each_online_cpu(j) {
316 dbs_info = &per_cpu(cpu_dbs_info, j);
317 requested_freq[j] = dbs_info->cur_policy->cur;
323 * The default safe range is 20% to 80%
324 * Every sampling_rate, we check
325 * - If current idle time is less than 20%, then we try to
327 * Every sampling_rate*sampling_down_factor, we check
328 * - If current idle time is more than 80%, then we try to
331 * Any frequency increase takes it to the maximum frequency.
332 * Frequency reduction happens at minimum steps of
333 * 5% (default) of max_frequency
336 /* Check for frequency increase */
337 idle_ticks = UINT_MAX;
338 for_each_cpu_mask(j, policy->cpus) {
339 unsigned int tmp_idle_ticks, total_idle_ticks;
340 struct cpu_dbs_info_s *j_dbs_info;
342 j_dbs_info = &per_cpu(cpu_dbs_info, j);
343 /* Check for frequency increase */
344 total_idle_ticks = get_cpu_idle_time(j);
345 tmp_idle_ticks = total_idle_ticks -
346 j_dbs_info->prev_cpu_idle_up;
347 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
349 if (tmp_idle_ticks < idle_ticks)
350 idle_ticks = tmp_idle_ticks;
353 /* Scale idle ticks by 100 and compare with up and down ticks */
355 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
356 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
358 if (idle_ticks < up_idle_ticks) {
360 for_each_cpu_mask(j, policy->cpus) {
361 struct cpu_dbs_info_s *j_dbs_info;
363 j_dbs_info = &per_cpu(cpu_dbs_info, j);
364 j_dbs_info->prev_cpu_idle_down =
365 j_dbs_info->prev_cpu_idle_up;
367 /* if we are already at full speed then break out early */
368 if (requested_freq[cpu] == policy->max)
371 freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
373 /* max freq cannot be less than 100. But who knows.... */
374 if (unlikely(freq_step == 0))
377 requested_freq[cpu] += freq_step;
378 if (requested_freq[cpu] > policy->max)
379 requested_freq[cpu] = policy->max;
381 __cpufreq_driver_target(policy, requested_freq[cpu],
386 /* Check for frequency decrease */
388 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
391 idle_ticks = UINT_MAX;
392 for_each_cpu_mask(j, policy->cpus) {
393 unsigned int tmp_idle_ticks, total_idle_ticks;
394 struct cpu_dbs_info_s *j_dbs_info;
396 j_dbs_info = &per_cpu(cpu_dbs_info, j);
397 /* Check for frequency decrease */
398 total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
399 tmp_idle_ticks = total_idle_ticks -
400 j_dbs_info->prev_cpu_idle_down;
401 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
403 if (tmp_idle_ticks < idle_ticks)
404 idle_ticks = tmp_idle_ticks;
407 /* Scale idle ticks by 100 and compare with up and down ticks */
411 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
412 dbs_tuners_ins.sampling_down_factor;
413 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
414 usecs_to_jiffies(freq_down_sampling_rate);
416 if (idle_ticks > down_idle_ticks) {
418 * if we are already at the lowest speed then break out early
419 * or if we 'cannot' reduce the speed as the user might want
420 * freq_step to be zero
422 if (requested_freq[cpu] == policy->min
423 || dbs_tuners_ins.freq_step == 0)
426 freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
428 /* max freq cannot be less than 100. But who knows.... */
429 if (unlikely(freq_step == 0))
432 requested_freq[cpu] -= freq_step;
433 if (requested_freq[cpu] < policy->min)
434 requested_freq[cpu] = policy->min;
436 __cpufreq_driver_target(policy, requested_freq[cpu],
442 static void do_dbs_timer(void *data)
445 mutex_lock(&dbs_mutex);
446 for_each_online_cpu(i)
448 schedule_delayed_work(&dbs_work,
449 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
450 mutex_unlock(&dbs_mutex);
453 static inline void dbs_timer_init(void)
455 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
456 schedule_delayed_work(&dbs_work,
457 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
461 static inline void dbs_timer_exit(void)
463 cancel_delayed_work(&dbs_work);
467 static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
470 unsigned int cpu = policy->cpu;
471 struct cpu_dbs_info_s *this_dbs_info;
474 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
477 case CPUFREQ_GOV_START:
478 if ((!cpu_online(cpu)) ||
482 if (policy->cpuinfo.transition_latency >
483 (TRANSITION_LATENCY_LIMIT * 1000))
485 if (this_dbs_info->enable) /* Already enabled */
488 mutex_lock(&dbs_mutex);
489 for_each_cpu_mask(j, policy->cpus) {
490 struct cpu_dbs_info_s *j_dbs_info;
491 j_dbs_info = &per_cpu(cpu_dbs_info, j);
492 j_dbs_info->cur_policy = policy;
494 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
495 j_dbs_info->prev_cpu_idle_down
496 = j_dbs_info->prev_cpu_idle_up;
498 this_dbs_info->enable = 1;
499 sysfs_create_group(&policy->kobj, &dbs_attr_group);
502 * Start the timerschedule work, when this governor
503 * is used for first time
505 if (dbs_enable == 1) {
506 unsigned int latency;
507 /* policy latency is in nS. Convert it to uS first */
508 latency = policy->cpuinfo.transition_latency / 1000;
512 def_sampling_rate = 10 * latency *
513 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
515 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
516 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
518 dbs_tuners_ins.sampling_rate = def_sampling_rate;
519 dbs_tuners_ins.ignore_nice = 0;
520 dbs_tuners_ins.freq_step = 5;
525 mutex_unlock(&dbs_mutex);
528 case CPUFREQ_GOV_STOP:
529 mutex_lock(&dbs_mutex);
530 this_dbs_info->enable = 0;
531 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
534 * Stop the timerschedule work, when this governor
535 * is used for first time
540 mutex_unlock(&dbs_mutex);
544 case CPUFREQ_GOV_LIMITS:
545 mutex_lock(&dbs_mutex);
546 if (policy->max < this_dbs_info->cur_policy->cur)
547 __cpufreq_driver_target(
548 this_dbs_info->cur_policy,
549 policy->max, CPUFREQ_RELATION_H);
550 else if (policy->min > this_dbs_info->cur_policy->cur)
551 __cpufreq_driver_target(
552 this_dbs_info->cur_policy,
553 policy->min, CPUFREQ_RELATION_L);
554 mutex_unlock(&dbs_mutex);
560 static struct cpufreq_governor cpufreq_gov_dbs = {
561 .name = "conservative",
562 .governor = cpufreq_governor_dbs,
563 .owner = THIS_MODULE,
566 static int __init cpufreq_gov_dbs_init(void)
568 return cpufreq_register_governor(&cpufreq_gov_dbs);
571 static void __exit cpufreq_gov_dbs_exit(void)
573 /* Make sure that the scheduled work is indeed not running */
574 flush_scheduled_work();
576 cpufreq_unregister_governor(&cpufreq_gov_dbs);
580 MODULE_AUTHOR ("Alexander Clouter <alex-kernel@digriz.org.uk>");
581 MODULE_DESCRIPTION ("'cpufreq_conservative' - A dynamic cpufreq governor for "
582 "Low Latency Frequency Transition capable processors "
583 "optimised for use in a battery environment");
584 MODULE_LICENSE ("GPL");
586 module_init(cpufreq_gov_dbs_init);
587 module_exit(cpufreq_gov_dbs_exit);