blob: a18cfbf021b330e95ad818b6cf6d688829b8d676 [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
140 if (!this_dbs_info->enable)
141 return 0;
142
143 this_dbs_info->requested_freq = freq->new;
144
145 return 0;
146}
147
148static struct notifier_block dbs_cpufreq_notifier_block = {
149 .notifier_call = dbs_cpufreq_notifier
150};
151
Dave Jonesb9170832005-05-31 19:03:47 -0700152/************************** sysfs interface ************************/
153static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
154{
Thomas Renninger9411b4e2009-02-04 11:54:04 +0100155 static int print_once;
156
157 if (!print_once) {
158 printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
159 "sysfs file is deprecated - used by: %s\n",
160 current->comm);
161 print_once = 1;
162 }
Dave Jones9acef482009-01-18 01:39:51 -0500163 return sprintf(buf, "%u\n", MAX_SAMPLING_RATE);
Dave Jonesb9170832005-05-31 19:03:47 -0700164}
165
166static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
167{
Thomas Renninger9411b4e2009-02-04 11:54:04 +0100168 static int print_once;
169
170 if (!print_once) {
171 printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
172 "sysfs file is deprecated - used by: %s\n", current->comm);
173 print_once = 1;
174 }
Dave Jones9acef482009-01-18 01:39:51 -0500175 return sprintf(buf, "%u\n", MIN_SAMPLING_RATE);
Dave Jonesb9170832005-05-31 19:03:47 -0700176}
177
Dave Jones18a72472007-10-22 16:49:09 -0400178#define define_one_ro(_name) \
179static struct freq_attr _name = \
Dave Jonesb9170832005-05-31 19:03:47 -0700180__ATTR(_name, 0444, show_##_name, NULL)
181
182define_one_ro(sampling_rate_max);
183define_one_ro(sampling_rate_min);
184
185/* cpufreq_conservative Governor Tunables */
186#define show_one(file_name, object) \
187static ssize_t show_##file_name \
188(struct cpufreq_policy *unused, char *buf) \
189{ \
190 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
191}
192show_one(sampling_rate, sampling_rate);
193show_one(sampling_down_factor, sampling_down_factor);
194show_one(up_threshold, up_threshold);
195show_one(down_threshold, down_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800196show_one(ignore_nice_load, ignore_nice);
Dave Jonesb9170832005-05-31 19:03:47 -0700197show_one(freq_step, freq_step);
198
Dave Jones18a72472007-10-22 16:49:09 -0400199static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
Dave Jonesb9170832005-05-31 19:03:47 -0700200 const char *buf, size_t count)
201{
202 unsigned int input;
203 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500204 ret = sscanf(buf, "%u", &input);
Alexander Clouter2c906b32006-03-22 09:54:10 +0000205 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700206 return -EINVAL;
207
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800208 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700209 dbs_tuners_ins.sampling_down_factor = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800210 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700211
212 return count;
213}
214
Dave Jones18a72472007-10-22 16:49:09 -0400215static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Dave Jonesb9170832005-05-31 19:03:47 -0700216 const char *buf, size_t count)
217{
218 unsigned int input;
219 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500220 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700221
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800222 mutex_lock(&dbs_mutex);
Thomas Renninger112124a2009-02-04 11:55:12 +0100223 if (ret != 1) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800224 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700225 return -EINVAL;
226 }
Thomas Renninger112124a2009-02-04 11:55:12 +0100227 dbs_tuners_ins.sampling_rate = max(input, minimum_sampling_rate());
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800228 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700229
230 return count;
231}
232
Dave Jones18a72472007-10-22 16:49:09 -0400233static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Dave Jonesb9170832005-05-31 19:03:47 -0700234 const char *buf, size_t count)
235{
236 unsigned int input;
237 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500238 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700239
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800240 mutex_lock(&dbs_mutex);
Dave Jones9acef482009-01-18 01:39:51 -0500241 if (ret != 1 || input > 100 ||
242 input <= dbs_tuners_ins.down_threshold) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800243 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700244 return -EINVAL;
245 }
246
247 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800248 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700249
250 return count;
251}
252
Dave Jones18a72472007-10-22 16:49:09 -0400253static ssize_t store_down_threshold(struct cpufreq_policy *unused,
Dave Jonesb9170832005-05-31 19:03:47 -0700254 const char *buf, size_t count)
255{
256 unsigned int input;
257 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500258 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700259
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800260 mutex_lock(&dbs_mutex);
Dave Jonesb82fbe62006-04-01 22:07:07 -0500261 if (ret != 1 || input > 100 || input >= dbs_tuners_ins.up_threshold) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800262 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700263 return -EINVAL;
264 }
265
266 dbs_tuners_ins.down_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800267 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700268
269 return count;
270}
271
Alexander Clouter001893c2005-12-01 01:09:25 -0800272static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jonesb9170832005-05-31 19:03:47 -0700273 const char *buf, size_t count)
274{
275 unsigned int input;
276 int ret;
277
278 unsigned int j;
Dave Jones18a72472007-10-22 16:49:09 -0400279
280 ret = sscanf(buf, "%u", &input);
281 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700282 return -EINVAL;
283
Dave Jones18a72472007-10-22 16:49:09 -0400284 if (input > 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700285 input = 1;
Dave Jones18a72472007-10-22 16:49:09 -0400286
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800287 mutex_lock(&dbs_mutex);
Dave Jones18a72472007-10-22 16:49:09 -0400288 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800289 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700290 return count;
291 }
292 dbs_tuners_ins.ignore_nice = input;
293
294 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700295 for_each_online_cpu(j) {
Dave Jonesb9170832005-05-31 19:03:47 -0700296 struct cpu_dbs_info_s *j_dbs_info;
297 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700298 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jonesb9170832005-05-31 19:03:47 -0700299 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
300 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800301 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700302
303 return count;
304}
305
306static ssize_t store_freq_step(struct cpufreq_policy *policy,
307 const char *buf, size_t count)
308{
309 unsigned int input;
310 int ret;
311
Dave Jones18a72472007-10-22 16:49:09 -0400312 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700313
Dave Jones18a72472007-10-22 16:49:09 -0400314 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700315 return -EINVAL;
316
Dave Jones18a72472007-10-22 16:49:09 -0400317 if (input > 100)
Dave Jonesb9170832005-05-31 19:03:47 -0700318 input = 100;
Dave Jones18a72472007-10-22 16:49:09 -0400319
Dave Jonesb9170832005-05-31 19:03:47 -0700320 /* no need to test here if freq_step is zero as the user might actually
321 * want this, they would be crazy though :) */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800322 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700323 dbs_tuners_ins.freq_step = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800324 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700325
326 return count;
327}
328
329#define define_one_rw(_name) \
330static struct freq_attr _name = \
331__ATTR(_name, 0644, show_##_name, store_##_name)
332
333define_one_rw(sampling_rate);
334define_one_rw(sampling_down_factor);
335define_one_rw(up_threshold);
336define_one_rw(down_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800337define_one_rw(ignore_nice_load);
Dave Jonesb9170832005-05-31 19:03:47 -0700338define_one_rw(freq_step);
339
Dave Jones9acef482009-01-18 01:39:51 -0500340static struct attribute *dbs_attributes[] = {
Dave Jonesb9170832005-05-31 19:03:47 -0700341 &sampling_rate_max.attr,
342 &sampling_rate_min.attr,
343 &sampling_rate.attr,
344 &sampling_down_factor.attr,
345 &up_threshold.attr,
346 &down_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800347 &ignore_nice_load.attr,
Dave Jonesb9170832005-05-31 19:03:47 -0700348 &freq_step.attr,
349 NULL
350};
351
352static struct attribute_group dbs_attr_group = {
353 .attrs = dbs_attributes,
354 .name = "conservative",
355};
356
357/************************** sysfs end ************************/
358
359static void dbs_check_cpu(int cpu)
360{
361 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
Alexander Clouter08a28e22006-03-22 09:59:16 +0000362 unsigned int tmp_idle_ticks, total_idle_ticks;
Dave Jonesf068c042008-07-30 12:59:56 -0400363 unsigned int freq_target;
Dave Jonesb9170832005-05-31 19:03:47 -0700364 unsigned int freq_down_sampling_rate;
Alexander Clouter08a28e22006-03-22 09:59:16 +0000365 struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
Dave Jonesb9170832005-05-31 19:03:47 -0700366 struct cpufreq_policy *policy;
Dave Jonesb9170832005-05-31 19:03:47 -0700367
Dave Jonesb9170832005-05-31 19:03:47 -0700368 if (!this_dbs_info->enable)
369 return;
370
Alexander Clouter08a28e22006-03-22 09:59:16 +0000371 policy = this_dbs_info->cur_policy;
372
Dave Jones18a72472007-10-22 16:49:09 -0400373 /*
374 * The default safe range is 20% to 80%
Dave Jonesb9170832005-05-31 19:03:47 -0700375 * Every sampling_rate, we check
Dave Jones18a72472007-10-22 16:49:09 -0400376 * - If current idle time is less than 20%, then we try to
377 * increase frequency
Dave Jonesb9170832005-05-31 19:03:47 -0700378 * Every sampling_rate*sampling_down_factor, we check
Dave Jones18a72472007-10-22 16:49:09 -0400379 * - If current idle time is more than 80%, then we try to
380 * decrease frequency
Dave Jonesb9170832005-05-31 19:03:47 -0700381 *
Dave Jones18a72472007-10-22 16:49:09 -0400382 * Any frequency increase takes it to the maximum frequency.
383 * Frequency reduction happens at minimum steps of
384 * 5% (default) of max_frequency
Dave Jonesb9170832005-05-31 19:03:47 -0700385 */
386
387 /* Check for frequency increase */
Dave Jones9c7d2692005-05-31 19:03:49 -0700388 idle_ticks = UINT_MAX;
Dave Jonesb9170832005-05-31 19:03:47 -0700389
Alexander Clouter08a28e22006-03-22 09:59:16 +0000390 /* Check for frequency increase */
391 total_idle_ticks = get_cpu_idle_time(cpu);
392 tmp_idle_ticks = total_idle_ticks -
393 this_dbs_info->prev_cpu_idle_up;
394 this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
Dave Jonesb9170832005-05-31 19:03:47 -0700395
Alexander Clouter08a28e22006-03-22 09:59:16 +0000396 if (tmp_idle_ticks < idle_ticks)
397 idle_ticks = tmp_idle_ticks;
Dave Jonesb9170832005-05-31 19:03:47 -0700398
399 /* Scale idle ticks by 100 and compare with up and down ticks */
400 idle_ticks *= 100;
401 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
Alexander Clouter2c906b32006-03-22 09:54:10 +0000402 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Dave Jonesb9170832005-05-31 19:03:47 -0700403
404 if (idle_ticks < up_idle_ticks) {
Alexander Cloutera159b822006-03-22 10:00:18 +0000405 this_dbs_info->down_skip = 0;
Alexander Clouter08a28e22006-03-22 09:59:16 +0000406 this_dbs_info->prev_cpu_idle_down =
407 this_dbs_info->prev_cpu_idle_up;
Dave Jones790d76f2005-05-31 19:03:49 -0700408
Dave Jonesb9170832005-05-31 19:03:47 -0700409 /* if we are already at full speed then break out early */
Alexander Cloutera159b822006-03-22 10:00:18 +0000410 if (this_dbs_info->requested_freq == policy->max)
Dave Jonesb9170832005-05-31 19:03:47 -0700411 return;
Dave Jones18a72472007-10-22 16:49:09 -0400412
Dave Jonesf068c042008-07-30 12:59:56 -0400413 freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
Dave Jonesb9170832005-05-31 19:03:47 -0700414
415 /* max freq cannot be less than 100. But who knows.... */
Dave Jonesf068c042008-07-30 12:59:56 -0400416 if (unlikely(freq_target == 0))
417 freq_target = 5;
Dave Jones18a72472007-10-22 16:49:09 -0400418
Dave Jonesf068c042008-07-30 12:59:56 -0400419 this_dbs_info->requested_freq += freq_target;
Alexander Cloutera159b822006-03-22 10:00:18 +0000420 if (this_dbs_info->requested_freq > policy->max)
421 this_dbs_info->requested_freq = policy->max;
Dave Jonesb9170832005-05-31 19:03:47 -0700422
Alexander Cloutera159b822006-03-22 10:00:18 +0000423 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
Dave Jonesb9170832005-05-31 19:03:47 -0700424 CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700425 return;
426 }
427
428 /* Check for frequency decrease */
Alexander Cloutera159b822006-03-22 10:00:18 +0000429 this_dbs_info->down_skip++;
430 if (this_dbs_info->down_skip < dbs_tuners_ins.sampling_down_factor)
Dave Jonesb9170832005-05-31 19:03:47 -0700431 return;
432
Alexander Clouter08a28e22006-03-22 09:59:16 +0000433 /* Check for frequency decrease */
434 total_idle_ticks = this_dbs_info->prev_cpu_idle_up;
435 tmp_idle_ticks = total_idle_ticks -
436 this_dbs_info->prev_cpu_idle_down;
437 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
Dave Jonesb9170832005-05-31 19:03:47 -0700438
Alexander Clouter08a28e22006-03-22 09:59:16 +0000439 if (tmp_idle_ticks < idle_ticks)
440 idle_ticks = tmp_idle_ticks;
Dave Jonesb9170832005-05-31 19:03:47 -0700441
442 /* Scale idle ticks by 100 and compare with up and down ticks */
443 idle_ticks *= 100;
Alexander Cloutera159b822006-03-22 10:00:18 +0000444 this_dbs_info->down_skip = 0;
Dave Jonesb9170832005-05-31 19:03:47 -0700445
446 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
447 dbs_tuners_ins.sampling_down_factor;
448 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
Alexander Clouter2c906b32006-03-22 09:54:10 +0000449 usecs_to_jiffies(freq_down_sampling_rate);
Dave Jonesb9170832005-05-31 19:03:47 -0700450
Dave Jones9c7d2692005-05-31 19:03:49 -0700451 if (idle_ticks > down_idle_ticks) {
Alexander Clouter2c906b32006-03-22 09:54:10 +0000452 /*
453 * if we are already at the lowest speed then break out early
Dave Jonesb9170832005-05-31 19:03:47 -0700454 * or if we 'cannot' reduce the speed as the user might want
Dave Jonesf068c042008-07-30 12:59:56 -0400455 * freq_target to be zero
Alexander Clouter2c906b32006-03-22 09:54:10 +0000456 */
Alexander Cloutera159b822006-03-22 10:00:18 +0000457 if (this_dbs_info->requested_freq == policy->min
Dave Jonesb9170832005-05-31 19:03:47 -0700458 || dbs_tuners_ins.freq_step == 0)
459 return;
460
Dave Jonesf068c042008-07-30 12:59:56 -0400461 freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
Dave Jonesb9170832005-05-31 19:03:47 -0700462
463 /* max freq cannot be less than 100. But who knows.... */
Dave Jonesf068c042008-07-30 12:59:56 -0400464 if (unlikely(freq_target == 0))
465 freq_target = 5;
Dave Jonesb9170832005-05-31 19:03:47 -0700466
Dave Jonesf068c042008-07-30 12:59:56 -0400467 this_dbs_info->requested_freq -= freq_target;
Alexander Cloutera159b822006-03-22 10:00:18 +0000468 if (this_dbs_info->requested_freq < policy->min)
469 this_dbs_info->requested_freq = policy->min;
Dave Jonesb9170832005-05-31 19:03:47 -0700470
Alexander Cloutera159b822006-03-22 10:00:18 +0000471 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
Alexander Clouter2c906b32006-03-22 09:54:10 +0000472 CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700473 return;
474 }
475}
476
David Howellsc4028952006-11-22 14:57:56 +0000477static void do_dbs_timer(struct work_struct *work)
Dave Jones18a72472007-10-22 16:49:09 -0400478{
Dave Jonesb9170832005-05-31 19:03:47 -0700479 int i;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800480 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700481 for_each_online_cpu(i)
482 dbs_check_cpu(i);
Dave Jones18a72472007-10-22 16:49:09 -0400483 schedule_delayed_work(&dbs_work,
Dave Jonesb9170832005-05-31 19:03:47 -0700484 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800485 mutex_unlock(&dbs_mutex);
Dave Jones18a72472007-10-22 16:49:09 -0400486}
Dave Jonesb9170832005-05-31 19:03:47 -0700487
488static inline void dbs_timer_init(void)
489{
Ben Slusky8217e4f2008-07-07 13:16:20 -0400490 init_timer_deferrable(&dbs_work.timer);
Dave Jonesb9170832005-05-31 19:03:47 -0700491 schedule_delayed_work(&dbs_work,
492 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
493 return;
494}
495
496static inline void dbs_timer_exit(void)
497{
498 cancel_delayed_work(&dbs_work);
499 return;
500}
501
502static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
503 unsigned int event)
504{
505 unsigned int cpu = policy->cpu;
506 struct cpu_dbs_info_s *this_dbs_info;
507 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700508 int rc;
Dave Jonesb9170832005-05-31 19:03:47 -0700509
510 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
511
512 switch (event) {
513 case CPUFREQ_GOV_START:
Dave Jones18a72472007-10-22 16:49:09 -0400514 if ((!cpu_online(cpu)) || (!policy->cur))
Dave Jonesb9170832005-05-31 19:03:47 -0700515 return -EINVAL;
516
Dave Jonesb9170832005-05-31 19:03:47 -0700517 if (this_dbs_info->enable) /* Already enabled */
518 break;
Dave Jones18a72472007-10-22 16:49:09 -0400519
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800520 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700521
522 rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
523 if (rc) {
524 mutex_unlock(&dbs_mutex);
525 return rc;
526 }
527
Rusty Russell835481d2009-01-04 05:18:06 -0800528 for_each_cpu(j, policy->cpus) {
Dave Jonesb9170832005-05-31 19:03:47 -0700529 struct cpu_dbs_info_s *j_dbs_info;
530 j_dbs_info = &per_cpu(cpu_dbs_info, j);
531 j_dbs_info->cur_policy = policy;
Dave Jones18a72472007-10-22 16:49:09 -0400532
Alexander Clouter08a28e22006-03-22 09:59:16 +0000533 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu);
Dave Jonesb9170832005-05-31 19:03:47 -0700534 j_dbs_info->prev_cpu_idle_down
535 = j_dbs_info->prev_cpu_idle_up;
536 }
537 this_dbs_info->enable = 1;
Alexander Cloutera159b822006-03-22 10:00:18 +0000538 this_dbs_info->down_skip = 0;
539 this_dbs_info->requested_freq = policy->cur;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700540
Dave Jonesb9170832005-05-31 19:03:47 -0700541 dbs_enable++;
542 /*
543 * Start the timerschedule work, when this governor
544 * is used for first time
545 */
546 if (dbs_enable == 1) {
547 unsigned int latency;
548 /* policy latency is in nS. Convert it to uS first */
Alexander Clouter2c906b32006-03-22 09:54:10 +0000549 latency = policy->cpuinfo.transition_latency / 1000;
550 if (latency == 0)
551 latency = 1;
Dave Jonesb9170832005-05-31 19:03:47 -0700552
Thomas Renninger112124a2009-02-04 11:55:12 +0100553 def_sampling_rate =
554 max(10 * latency * LATENCY_MULTIPLIER,
555 MIN_STAT_SAMPLING_RATE);
Alexander Clouter2c906b32006-03-22 09:54:10 +0000556
Dave Jonesb9170832005-05-31 19:03:47 -0700557 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Dave Jonesb9170832005-05-31 19:03:47 -0700558
559 dbs_timer_init();
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200560 cpufreq_register_notifier(
561 &dbs_cpufreq_notifier_block,
562 CPUFREQ_TRANSITION_NOTIFIER);
Dave Jonesb9170832005-05-31 19:03:47 -0700563 }
Dave Jones18a72472007-10-22 16:49:09 -0400564
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800565 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700566 break;
567
568 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800569 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700570 this_dbs_info->enable = 0;
571 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
572 dbs_enable--;
573 /*
574 * Stop the timerschedule work, when this governor
575 * is used for first time
576 */
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200577 if (dbs_enable == 0) {
Dave Jonesb9170832005-05-31 19:03:47 -0700578 dbs_timer_exit();
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200579 cpufreq_unregister_notifier(
580 &dbs_cpufreq_notifier_block,
581 CPUFREQ_TRANSITION_NOTIFIER);
582 }
583
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800584 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700585
586 break;
587
588 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800589 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700590 if (policy->max < this_dbs_info->cur_policy->cur)
591 __cpufreq_driver_target(
592 this_dbs_info->cur_policy,
Dave Jones18a72472007-10-22 16:49:09 -0400593 policy->max, CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700594 else if (policy->min > this_dbs_info->cur_policy->cur)
595 __cpufreq_driver_target(
596 this_dbs_info->cur_policy,
Dave Jones18a72472007-10-22 16:49:09 -0400597 policy->min, CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800598 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700599 break;
600 }
601 return 0;
602}
603
Sven Wegenerc4d14bc2008-09-20 16:50:08 +0200604#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
605static
606#endif
Thomas Renninger1c256242007-10-02 13:28:12 -0700607struct cpufreq_governor cpufreq_gov_conservative = {
608 .name = "conservative",
609 .governor = cpufreq_governor_dbs,
610 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
611 .owner = THIS_MODULE,
Dave Jonesb9170832005-05-31 19:03:47 -0700612};
613
614static int __init cpufreq_gov_dbs_init(void)
615{
Thomas Renninger1c256242007-10-02 13:28:12 -0700616 return cpufreq_register_governor(&cpufreq_gov_conservative);
Dave Jonesb9170832005-05-31 19:03:47 -0700617}
618
619static void __exit cpufreq_gov_dbs_exit(void)
620{
621 /* Make sure that the scheduled work is indeed not running */
622 flush_scheduled_work();
623
Thomas Renninger1c256242007-10-02 13:28:12 -0700624 cpufreq_unregister_governor(&cpufreq_gov_conservative);
Dave Jonesb9170832005-05-31 19:03:47 -0700625}
626
627
Alexander Clouter11a80a9c762009-02-13 19:01:01 +0000628MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
Dave Jones9acef482009-01-18 01:39:51 -0500629MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
Dave Jonesb9170832005-05-31 19:03:47 -0700630 "Low Latency Frequency Transition capable processors "
631 "optimised for use in a battery environment");
Dave Jones9acef482009-01-18 01:39:51 -0500632MODULE_LICENSE("GPL");
Dave Jonesb9170832005-05-31 19:03:47 -0700633
Johannes Weiner69157192008-01-17 15:21:08 -0800634#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
635fs_initcall(cpufreq_gov_dbs_init);
636#else
Dave Jonesb9170832005-05-31 19:03:47 -0700637module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800638#endif
Dave Jonesb9170832005-05-31 19:03:47 -0700639module_exit(cpufreq_gov_dbs_exit);