blob: a16a5b8c1dc56da7b7277fbd2c342654957b2342 [file] [log] [blame]
Dave Jonesb9170832005-05-31 19:03:47 -07001/*
2 * drivers/cpufreq/cpufreq_conservative.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com>
Alexander Clouter11a80a9c762009-02-13 19:01:01 +00007 * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
Dave Jonesb9170832005-05-31 19:03:47 -07008 *
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.
12 */
13
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>
23#include <linux/fs.h>
24#include <linux/sysfs.h>
Andrew Morton138a01282006-06-23 03:31:19 -070025#include <linux/cpu.h>
Dave Jonesb9170832005-05-31 19:03:47 -070026#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>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080031#include <linux/mutex.h>
Dave Jonesb9170832005-05-31 19:03:47 -070032/*
33 * dbs is used in this file as a shortform for demandbased switching
34 * It helps to keep variable names smaller, simpler
35 */
36
37#define DEF_FREQUENCY_UP_THRESHOLD (80)
Dave Jonesb9170832005-05-31 19:03:47 -070038#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
Dave Jonesb9170832005-05-31 19:03:47 -070039
Dave Jones18a72472007-10-22 16:49:09 -040040/*
41 * The polling frequency of this governor depends on the capability of
Dave Jonesb9170832005-05-31 19:03:47 -070042 * the processor. Default polling frequency is 1000 times the transition
Dave Jones18a72472007-10-22 16:49:09 -040043 * latency of the processor. The governor will work on any processor with
44 * transition latency <= 10mS, using appropriate sampling
Dave Jonesb9170832005-05-31 19:03:47 -070045 * rate.
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053046 * For CPUs with transition latency > 10mS (mostly drivers
47 * with CPUFREQ_ETERNAL), this governor will not work.
Dave Jonesb9170832005-05-31 19:03:47 -070048 * All times here are in uS.
49 */
Dave Jones18a72472007-10-22 16:49:09 -040050static unsigned int def_sampling_rate;
Alexander Clouter2c906b32006-03-22 09:54:10 +000051#define MIN_SAMPLING_RATE_RATIO (2)
52/* for correct statistics, we need at least 10 ticks between each measure */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053053#define MIN_STAT_SAMPLING_RATE \
54 (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
55#define MIN_SAMPLING_RATE \
56 (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
Thomas Renninger112124a2009-02-04 11:55:12 +010057/* Above MIN_SAMPLING_RATE will vanish with its sysfs file soon
58 * Define the minimal settable sampling rate to the greater of:
59 * - "HW transition latency" * 100 (same as default sampling / 10)
60 * - MIN_STAT_SAMPLING_RATE
61 * To avoid that userspace shoots itself.
62*/
63static unsigned int minimum_sampling_rate(void)
64{
65 return max(def_sampling_rate / 10, MIN_STAT_SAMPLING_RATE);
66}
67
68/* This will also vanish soon with removing sampling_rate_max */
Dave Jonesb9170832005-05-31 19:03:47 -070069#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
Thomas Renninger112124a2009-02-04 11:55:12 +010070#define LATENCY_MULTIPLIER (1000)
Alexander Clouter2c906b32006-03-22 09:54:10 +000071#define DEF_SAMPLING_DOWN_FACTOR (1)
72#define MAX_SAMPLING_DOWN_FACTOR (10)
Thomas Renninger1c256242007-10-02 13:28:12 -070073#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
Dave Jonesb9170832005-05-31 19:03:47 -070074
David Howellsc4028952006-11-22 14:57:56 +000075static void do_dbs_timer(struct work_struct *work);
Dave Jonesb9170832005-05-31 19:03:47 -070076
77struct cpu_dbs_info_s {
Dave Jones18a72472007-10-22 16:49:09 -040078 struct cpufreq_policy *cur_policy;
79 unsigned int prev_cpu_idle_up;
80 unsigned int prev_cpu_idle_down;
81 unsigned int enable;
82 unsigned int down_skip;
83 unsigned int requested_freq;
Dave Jonesb9170832005-05-31 19:03:47 -070084};
85static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
86
87static unsigned int dbs_enable; /* number of CPUs using this policy */
88
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -070089/*
90 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
91 * lock and dbs_mutex. cpu_hotplug lock should always be held before
92 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
93 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
94 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
95 * is recursive for the same process. -Venki
96 */
Dave Jones9acef482009-01-18 01:39:51 -050097static DEFINE_MUTEX(dbs_mutex);
David Howellsc4028952006-11-22 14:57:56 +000098static DECLARE_DELAYED_WORK(dbs_work, do_dbs_timer);
Dave Jonesb9170832005-05-31 19:03:47 -070099
100struct dbs_tuners {
Dave Jones18a72472007-10-22 16:49:09 -0400101 unsigned int sampling_rate;
102 unsigned int sampling_down_factor;
103 unsigned int up_threshold;
104 unsigned int down_threshold;
105 unsigned int ignore_nice;
106 unsigned int freq_step;
Dave Jonesb9170832005-05-31 19:03:47 -0700107};
108
109static struct dbs_tuners dbs_tuners_ins = {
Dave Jones18a72472007-10-22 16:49:09 -0400110 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
111 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
112 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
113 .ignore_nice = 0,
114 .freq_step = 5,
Dave Jonesb9170832005-05-31 19:03:47 -0700115};
116
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700117static inline unsigned int get_cpu_idle_time(unsigned int cpu)
118{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530119 unsigned int add_nice = 0, ret;
120
121 if (dbs_tuners_ins.ignore_nice)
122 add_nice = kstat_cpu(cpu).cpustat.nice;
123
Dave Jones18a72472007-10-22 16:49:09 -0400124 ret = kstat_cpu(cpu).cpustat.idle +
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700125 kstat_cpu(cpu).cpustat.iowait +
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530126 add_nice;
127
128 return ret;
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700129}
130
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200131/* keep track of frequency transitions */
132static int
133dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
134 void *data)
135{
136 struct cpufreq_freqs *freq = data;
137 struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info,
138 freq->cpu);
139
Alexander Clouterf407a082009-02-13 19:01:51 +0000140 struct cpufreq_policy *policy;
141
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200142 if (!this_dbs_info->enable)
143 return 0;
144
Alexander Clouterf407a082009-02-13 19:01:51 +0000145 policy = this_dbs_info->cur_policy;
146
147 /*
148 * we only care if our internally tracked freq moves outside
149 * the 'valid' ranges of freqency available to us otherwise
150 * we do not change it
151 */
152 if (this_dbs_info->requested_freq > policy->max
153 || this_dbs_info->requested_freq < policy->min)
154 this_dbs_info->requested_freq = freq->new;
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200155
156 return 0;
157}
158
159static struct notifier_block dbs_cpufreq_notifier_block = {
160 .notifier_call = dbs_cpufreq_notifier
161};
162
Dave Jonesb9170832005-05-31 19:03:47 -0700163/************************** sysfs interface ************************/
164static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
165{
Thomas Renninger9411b4e2009-02-04 11:54:04 +0100166 static int print_once;
167
168 if (!print_once) {
169 printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
170 "sysfs file is deprecated - used by: %s\n",
171 current->comm);
172 print_once = 1;
173 }
Dave Jones9acef482009-01-18 01:39:51 -0500174 return sprintf(buf, "%u\n", MAX_SAMPLING_RATE);
Dave Jonesb9170832005-05-31 19:03:47 -0700175}
176
177static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
178{
Thomas Renninger9411b4e2009-02-04 11:54:04 +0100179 static int print_once;
180
181 if (!print_once) {
182 printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
183 "sysfs file is deprecated - used by: %s\n", current->comm);
184 print_once = 1;
185 }
Dave Jones9acef482009-01-18 01:39:51 -0500186 return sprintf(buf, "%u\n", MIN_SAMPLING_RATE);
Dave Jonesb9170832005-05-31 19:03:47 -0700187}
188
Dave Jones18a72472007-10-22 16:49:09 -0400189#define define_one_ro(_name) \
190static struct freq_attr _name = \
Dave Jonesb9170832005-05-31 19:03:47 -0700191__ATTR(_name, 0444, show_##_name, NULL)
192
193define_one_ro(sampling_rate_max);
194define_one_ro(sampling_rate_min);
195
196/* cpufreq_conservative Governor Tunables */
197#define show_one(file_name, object) \
198static ssize_t show_##file_name \
199(struct cpufreq_policy *unused, char *buf) \
200{ \
201 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
202}
203show_one(sampling_rate, sampling_rate);
204show_one(sampling_down_factor, sampling_down_factor);
205show_one(up_threshold, up_threshold);
206show_one(down_threshold, down_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800207show_one(ignore_nice_load, ignore_nice);
Dave Jonesb9170832005-05-31 19:03:47 -0700208show_one(freq_step, freq_step);
209
Dave Jones18a72472007-10-22 16:49:09 -0400210static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
Dave Jonesb9170832005-05-31 19:03:47 -0700211 const char *buf, size_t count)
212{
213 unsigned int input;
214 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500215 ret = sscanf(buf, "%u", &input);
Alexander Clouter2c906b32006-03-22 09:54:10 +0000216 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700217 return -EINVAL;
218
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800219 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700220 dbs_tuners_ins.sampling_down_factor = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800221 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700222
223 return count;
224}
225
Dave Jones18a72472007-10-22 16:49:09 -0400226static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Dave Jonesb9170832005-05-31 19:03:47 -0700227 const char *buf, size_t count)
228{
229 unsigned int input;
230 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500231 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700232
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800233 mutex_lock(&dbs_mutex);
Thomas Renninger112124a2009-02-04 11:55:12 +0100234 if (ret != 1) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800235 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700236 return -EINVAL;
237 }
Thomas Renninger112124a2009-02-04 11:55:12 +0100238 dbs_tuners_ins.sampling_rate = max(input, minimum_sampling_rate());
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800239 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700240
241 return count;
242}
243
Dave Jones18a72472007-10-22 16:49:09 -0400244static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Dave Jonesb9170832005-05-31 19:03:47 -0700245 const char *buf, size_t count)
246{
247 unsigned int input;
248 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500249 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700250
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800251 mutex_lock(&dbs_mutex);
Dave Jones9acef482009-01-18 01:39:51 -0500252 if (ret != 1 || input > 100 ||
253 input <= dbs_tuners_ins.down_threshold) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800254 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700255 return -EINVAL;
256 }
257
258 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800259 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700260
261 return count;
262}
263
Dave Jones18a72472007-10-22 16:49:09 -0400264static ssize_t store_down_threshold(struct cpufreq_policy *unused,
Dave Jonesb9170832005-05-31 19:03:47 -0700265 const char *buf, size_t count)
266{
267 unsigned int input;
268 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500269 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700270
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800271 mutex_lock(&dbs_mutex);
Dave Jonesb82fbe62006-04-01 22:07:07 -0500272 if (ret != 1 || input > 100 || input >= dbs_tuners_ins.up_threshold) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800273 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700274 return -EINVAL;
275 }
276
277 dbs_tuners_ins.down_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800278 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700279
280 return count;
281}
282
Alexander Clouter001893c2005-12-01 01:09:25 -0800283static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jonesb9170832005-05-31 19:03:47 -0700284 const char *buf, size_t count)
285{
286 unsigned int input;
287 int ret;
288
289 unsigned int j;
Dave Jones18a72472007-10-22 16:49:09 -0400290
291 ret = sscanf(buf, "%u", &input);
292 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700293 return -EINVAL;
294
Dave Jones18a72472007-10-22 16:49:09 -0400295 if (input > 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700296 input = 1;
Dave Jones18a72472007-10-22 16:49:09 -0400297
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800298 mutex_lock(&dbs_mutex);
Dave Jones18a72472007-10-22 16:49:09 -0400299 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800300 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700301 return count;
302 }
303 dbs_tuners_ins.ignore_nice = input;
304
305 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700306 for_each_online_cpu(j) {
Dave Jonesb9170832005-05-31 19:03:47 -0700307 struct cpu_dbs_info_s *j_dbs_info;
308 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700309 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jonesb9170832005-05-31 19:03:47 -0700310 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
311 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800312 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700313
314 return count;
315}
316
317static ssize_t store_freq_step(struct cpufreq_policy *policy,
318 const char *buf, size_t count)
319{
320 unsigned int input;
321 int ret;
322
Dave Jones18a72472007-10-22 16:49:09 -0400323 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700324
Dave Jones18a72472007-10-22 16:49:09 -0400325 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700326 return -EINVAL;
327
Dave Jones18a72472007-10-22 16:49:09 -0400328 if (input > 100)
Dave Jonesb9170832005-05-31 19:03:47 -0700329 input = 100;
Dave Jones18a72472007-10-22 16:49:09 -0400330
Dave Jonesb9170832005-05-31 19:03:47 -0700331 /* no need to test here if freq_step is zero as the user might actually
332 * want this, they would be crazy though :) */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800333 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700334 dbs_tuners_ins.freq_step = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800335 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700336
337 return count;
338}
339
340#define define_one_rw(_name) \
341static struct freq_attr _name = \
342__ATTR(_name, 0644, show_##_name, store_##_name)
343
344define_one_rw(sampling_rate);
345define_one_rw(sampling_down_factor);
346define_one_rw(up_threshold);
347define_one_rw(down_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800348define_one_rw(ignore_nice_load);
Dave Jonesb9170832005-05-31 19:03:47 -0700349define_one_rw(freq_step);
350
Dave Jones9acef482009-01-18 01:39:51 -0500351static struct attribute *dbs_attributes[] = {
Dave Jonesb9170832005-05-31 19:03:47 -0700352 &sampling_rate_max.attr,
353 &sampling_rate_min.attr,
354 &sampling_rate.attr,
355 &sampling_down_factor.attr,
356 &up_threshold.attr,
357 &down_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800358 &ignore_nice_load.attr,
Dave Jonesb9170832005-05-31 19:03:47 -0700359 &freq_step.attr,
360 NULL
361};
362
363static struct attribute_group dbs_attr_group = {
364 .attrs = dbs_attributes,
365 .name = "conservative",
366};
367
368/************************** sysfs end ************************/
369
370static void dbs_check_cpu(int cpu)
371{
372 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
Alexander Clouter08a28e22006-03-22 09:59:16 +0000373 unsigned int tmp_idle_ticks, total_idle_ticks;
Dave Jonesf068c042008-07-30 12:59:56 -0400374 unsigned int freq_target;
Dave Jonesb9170832005-05-31 19:03:47 -0700375 unsigned int freq_down_sampling_rate;
Alexander Clouter08a28e22006-03-22 09:59:16 +0000376 struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
Dave Jonesb9170832005-05-31 19:03:47 -0700377 struct cpufreq_policy *policy;
Dave Jonesb9170832005-05-31 19:03:47 -0700378
Dave Jonesb9170832005-05-31 19:03:47 -0700379 if (!this_dbs_info->enable)
380 return;
381
Alexander Clouter08a28e22006-03-22 09:59:16 +0000382 policy = this_dbs_info->cur_policy;
383
Dave Jones18a72472007-10-22 16:49:09 -0400384 /*
385 * The default safe range is 20% to 80%
Dave Jonesb9170832005-05-31 19:03:47 -0700386 * Every sampling_rate, we check
Dave Jones18a72472007-10-22 16:49:09 -0400387 * - If current idle time is less than 20%, then we try to
388 * increase frequency
Dave Jonesb9170832005-05-31 19:03:47 -0700389 * Every sampling_rate*sampling_down_factor, we check
Dave Jones18a72472007-10-22 16:49:09 -0400390 * - If current idle time is more than 80%, then we try to
391 * decrease frequency
Dave Jonesb9170832005-05-31 19:03:47 -0700392 *
Dave Jones18a72472007-10-22 16:49:09 -0400393 * Any frequency increase takes it to the maximum frequency.
394 * Frequency reduction happens at minimum steps of
395 * 5% (default) of max_frequency
Dave Jonesb9170832005-05-31 19:03:47 -0700396 */
397
398 /* Check for frequency increase */
Dave Jones9c7d2692005-05-31 19:03:49 -0700399 idle_ticks = UINT_MAX;
Dave Jonesb9170832005-05-31 19:03:47 -0700400
Alexander Clouter08a28e22006-03-22 09:59:16 +0000401 /* Check for frequency increase */
402 total_idle_ticks = get_cpu_idle_time(cpu);
403 tmp_idle_ticks = total_idle_ticks -
404 this_dbs_info->prev_cpu_idle_up;
405 this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
Dave Jonesb9170832005-05-31 19:03:47 -0700406
Alexander Clouter08a28e22006-03-22 09:59:16 +0000407 if (tmp_idle_ticks < idle_ticks)
408 idle_ticks = tmp_idle_ticks;
Dave Jonesb9170832005-05-31 19:03:47 -0700409
410 /* Scale idle ticks by 100 and compare with up and down ticks */
411 idle_ticks *= 100;
412 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
Alexander Clouter2c906b32006-03-22 09:54:10 +0000413 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Dave Jonesb9170832005-05-31 19:03:47 -0700414
415 if (idle_ticks < up_idle_ticks) {
Alexander Cloutera159b822006-03-22 10:00:18 +0000416 this_dbs_info->down_skip = 0;
Alexander Clouter08a28e22006-03-22 09:59:16 +0000417 this_dbs_info->prev_cpu_idle_down =
418 this_dbs_info->prev_cpu_idle_up;
Dave Jones790d76f2005-05-31 19:03:49 -0700419
Dave Jonesb9170832005-05-31 19:03:47 -0700420 /* if we are already at full speed then break out early */
Alexander Cloutera159b822006-03-22 10:00:18 +0000421 if (this_dbs_info->requested_freq == policy->max)
Dave Jonesb9170832005-05-31 19:03:47 -0700422 return;
Dave Jones18a72472007-10-22 16:49:09 -0400423
Dave Jonesf068c042008-07-30 12:59:56 -0400424 freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
Dave Jonesb9170832005-05-31 19:03:47 -0700425
426 /* max freq cannot be less than 100. But who knows.... */
Dave Jonesf068c042008-07-30 12:59:56 -0400427 if (unlikely(freq_target == 0))
428 freq_target = 5;
Dave Jones18a72472007-10-22 16:49:09 -0400429
Dave Jonesf068c042008-07-30 12:59:56 -0400430 this_dbs_info->requested_freq += freq_target;
Alexander Cloutera159b822006-03-22 10:00:18 +0000431 if (this_dbs_info->requested_freq > policy->max)
432 this_dbs_info->requested_freq = policy->max;
Dave Jonesb9170832005-05-31 19:03:47 -0700433
Alexander Cloutera159b822006-03-22 10:00:18 +0000434 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
Dave Jonesb9170832005-05-31 19:03:47 -0700435 CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700436 return;
437 }
438
439 /* Check for frequency decrease */
Alexander Cloutera159b822006-03-22 10:00:18 +0000440 this_dbs_info->down_skip++;
441 if (this_dbs_info->down_skip < dbs_tuners_ins.sampling_down_factor)
Dave Jonesb9170832005-05-31 19:03:47 -0700442 return;
443
Alexander Clouter08a28e22006-03-22 09:59:16 +0000444 /* Check for frequency decrease */
445 total_idle_ticks = this_dbs_info->prev_cpu_idle_up;
446 tmp_idle_ticks = total_idle_ticks -
447 this_dbs_info->prev_cpu_idle_down;
448 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
Dave Jonesb9170832005-05-31 19:03:47 -0700449
Alexander Clouter08a28e22006-03-22 09:59:16 +0000450 if (tmp_idle_ticks < idle_ticks)
451 idle_ticks = tmp_idle_ticks;
Dave Jonesb9170832005-05-31 19:03:47 -0700452
453 /* Scale idle ticks by 100 and compare with up and down ticks */
454 idle_ticks *= 100;
Alexander Cloutera159b822006-03-22 10:00:18 +0000455 this_dbs_info->down_skip = 0;
Dave Jonesb9170832005-05-31 19:03:47 -0700456
457 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
458 dbs_tuners_ins.sampling_down_factor;
459 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
Alexander Clouter2c906b32006-03-22 09:54:10 +0000460 usecs_to_jiffies(freq_down_sampling_rate);
Dave Jonesb9170832005-05-31 19:03:47 -0700461
Dave Jones9c7d2692005-05-31 19:03:49 -0700462 if (idle_ticks > down_idle_ticks) {
Alexander Clouter2c906b32006-03-22 09:54:10 +0000463 /*
464 * if we are already at the lowest speed then break out early
Dave Jonesb9170832005-05-31 19:03:47 -0700465 * or if we 'cannot' reduce the speed as the user might want
Dave Jonesf068c042008-07-30 12:59:56 -0400466 * freq_target to be zero
Alexander Clouter2c906b32006-03-22 09:54:10 +0000467 */
Alexander Cloutera159b822006-03-22 10:00:18 +0000468 if (this_dbs_info->requested_freq == policy->min
Dave Jonesb9170832005-05-31 19:03:47 -0700469 || dbs_tuners_ins.freq_step == 0)
470 return;
471
Dave Jonesf068c042008-07-30 12:59:56 -0400472 freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
Dave Jonesb9170832005-05-31 19:03:47 -0700473
474 /* max freq cannot be less than 100. But who knows.... */
Dave Jonesf068c042008-07-30 12:59:56 -0400475 if (unlikely(freq_target == 0))
476 freq_target = 5;
Dave Jonesb9170832005-05-31 19:03:47 -0700477
Dave Jonesf068c042008-07-30 12:59:56 -0400478 this_dbs_info->requested_freq -= freq_target;
Alexander Cloutera159b822006-03-22 10:00:18 +0000479 if (this_dbs_info->requested_freq < policy->min)
480 this_dbs_info->requested_freq = policy->min;
Dave Jonesb9170832005-05-31 19:03:47 -0700481
Alexander Cloutera159b822006-03-22 10:00:18 +0000482 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
Alexander Clouter2c906b32006-03-22 09:54:10 +0000483 CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700484 return;
485 }
486}
487
David Howellsc4028952006-11-22 14:57:56 +0000488static void do_dbs_timer(struct work_struct *work)
Dave Jones18a72472007-10-22 16:49:09 -0400489{
Dave Jonesb9170832005-05-31 19:03:47 -0700490 int i;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800491 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700492 for_each_online_cpu(i)
493 dbs_check_cpu(i);
Dave Jones18a72472007-10-22 16:49:09 -0400494 schedule_delayed_work(&dbs_work,
Dave Jonesb9170832005-05-31 19:03:47 -0700495 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800496 mutex_unlock(&dbs_mutex);
Dave Jones18a72472007-10-22 16:49:09 -0400497}
Dave Jonesb9170832005-05-31 19:03:47 -0700498
499static inline void dbs_timer_init(void)
500{
Ben Slusky8217e4f2008-07-07 13:16:20 -0400501 init_timer_deferrable(&dbs_work.timer);
Dave Jonesb9170832005-05-31 19:03:47 -0700502 schedule_delayed_work(&dbs_work,
503 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
504 return;
505}
506
507static inline void dbs_timer_exit(void)
508{
509 cancel_delayed_work(&dbs_work);
510 return;
511}
512
513static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
514 unsigned int event)
515{
516 unsigned int cpu = policy->cpu;
517 struct cpu_dbs_info_s *this_dbs_info;
518 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700519 int rc;
Dave Jonesb9170832005-05-31 19:03:47 -0700520
521 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
522
523 switch (event) {
524 case CPUFREQ_GOV_START:
Dave Jones18a72472007-10-22 16:49:09 -0400525 if ((!cpu_online(cpu)) || (!policy->cur))
Dave Jonesb9170832005-05-31 19:03:47 -0700526 return -EINVAL;
527
Dave Jonesb9170832005-05-31 19:03:47 -0700528 if (this_dbs_info->enable) /* Already enabled */
529 break;
Dave Jones18a72472007-10-22 16:49:09 -0400530
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800531 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700532
533 rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
534 if (rc) {
535 mutex_unlock(&dbs_mutex);
536 return rc;
537 }
538
Rusty Russell835481d2009-01-04 05:18:06 -0800539 for_each_cpu(j, policy->cpus) {
Dave Jonesb9170832005-05-31 19:03:47 -0700540 struct cpu_dbs_info_s *j_dbs_info;
541 j_dbs_info = &per_cpu(cpu_dbs_info, j);
542 j_dbs_info->cur_policy = policy;
Dave Jones18a72472007-10-22 16:49:09 -0400543
Alexander Clouter08a28e22006-03-22 09:59:16 +0000544 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu);
Dave Jonesb9170832005-05-31 19:03:47 -0700545 j_dbs_info->prev_cpu_idle_down
546 = j_dbs_info->prev_cpu_idle_up;
547 }
548 this_dbs_info->enable = 1;
Alexander Cloutera159b822006-03-22 10:00:18 +0000549 this_dbs_info->down_skip = 0;
550 this_dbs_info->requested_freq = policy->cur;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700551
Dave Jonesb9170832005-05-31 19:03:47 -0700552 dbs_enable++;
553 /*
554 * Start the timerschedule work, when this governor
555 * is used for first time
556 */
557 if (dbs_enable == 1) {
558 unsigned int latency;
559 /* policy latency is in nS. Convert it to uS first */
Alexander Clouter2c906b32006-03-22 09:54:10 +0000560 latency = policy->cpuinfo.transition_latency / 1000;
561 if (latency == 0)
562 latency = 1;
Dave Jonesb9170832005-05-31 19:03:47 -0700563
Thomas Renninger112124a2009-02-04 11:55:12 +0100564 def_sampling_rate =
565 max(10 * latency * LATENCY_MULTIPLIER,
566 MIN_STAT_SAMPLING_RATE);
Alexander Clouter2c906b32006-03-22 09:54:10 +0000567
Dave Jonesb9170832005-05-31 19:03:47 -0700568 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Dave Jonesb9170832005-05-31 19:03:47 -0700569
570 dbs_timer_init();
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200571 cpufreq_register_notifier(
572 &dbs_cpufreq_notifier_block,
573 CPUFREQ_TRANSITION_NOTIFIER);
Dave Jonesb9170832005-05-31 19:03:47 -0700574 }
Dave Jones18a72472007-10-22 16:49:09 -0400575
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800576 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700577 break;
578
579 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800580 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700581 this_dbs_info->enable = 0;
582 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
583 dbs_enable--;
584 /*
585 * Stop the timerschedule work, when this governor
586 * is used for first time
587 */
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200588 if (dbs_enable == 0) {
Dave Jonesb9170832005-05-31 19:03:47 -0700589 dbs_timer_exit();
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200590 cpufreq_unregister_notifier(
591 &dbs_cpufreq_notifier_block,
592 CPUFREQ_TRANSITION_NOTIFIER);
593 }
594
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800595 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700596
597 break;
598
599 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800600 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700601 if (policy->max < this_dbs_info->cur_policy->cur)
602 __cpufreq_driver_target(
603 this_dbs_info->cur_policy,
Dave Jones18a72472007-10-22 16:49:09 -0400604 policy->max, CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700605 else if (policy->min > this_dbs_info->cur_policy->cur)
606 __cpufreq_driver_target(
607 this_dbs_info->cur_policy,
Dave Jones18a72472007-10-22 16:49:09 -0400608 policy->min, CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800609 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700610 break;
611 }
612 return 0;
613}
614
Sven Wegenerc4d14bc2008-09-20 16:50:08 +0200615#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
616static
617#endif
Thomas Renninger1c256242007-10-02 13:28:12 -0700618struct cpufreq_governor cpufreq_gov_conservative = {
619 .name = "conservative",
620 .governor = cpufreq_governor_dbs,
621 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
622 .owner = THIS_MODULE,
Dave Jonesb9170832005-05-31 19:03:47 -0700623};
624
625static int __init cpufreq_gov_dbs_init(void)
626{
Thomas Renninger1c256242007-10-02 13:28:12 -0700627 return cpufreq_register_governor(&cpufreq_gov_conservative);
Dave Jonesb9170832005-05-31 19:03:47 -0700628}
629
630static void __exit cpufreq_gov_dbs_exit(void)
631{
632 /* Make sure that the scheduled work is indeed not running */
633 flush_scheduled_work();
634
Thomas Renninger1c256242007-10-02 13:28:12 -0700635 cpufreq_unregister_governor(&cpufreq_gov_conservative);
Dave Jonesb9170832005-05-31 19:03:47 -0700636}
637
638
Alexander Clouter11a80a9c762009-02-13 19:01:01 +0000639MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
Dave Jones9acef482009-01-18 01:39:51 -0500640MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
Dave Jonesb9170832005-05-31 19:03:47 -0700641 "Low Latency Frequency Transition capable processors "
642 "optimised for use in a battery environment");
Dave Jones9acef482009-01-18 01:39:51 -0500643MODULE_LICENSE("GPL");
Dave Jonesb9170832005-05-31 19:03:47 -0700644
Johannes Weiner69157192008-01-17 15:21:08 -0800645#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
646fs_initcall(cpufreq_gov_dbs_init);
647#else
Dave Jonesb9170832005-05-31 19:03:47 -0700648module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800649#endif
Dave Jonesb9170832005-05-31 19:03:47 -0700650module_exit(cpufreq_gov_dbs_exit);