blob: 69aa1db8336cd3eb1064728487d28f171cf5e68f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/cpufreq/cpufreq_ondemand.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/smp.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/ctype.h>
19#include <linux/cpufreq.h>
20#include <linux/sysctl.h>
21#include <linux/types.h>
22#include <linux/fs.h>
23#include <linux/sysfs.h>
24#include <linux/sched.h>
25#include <linux/kmod.h>
26#include <linux/workqueue.h>
27#include <linux/jiffies.h>
28#include <linux/kernel_stat.h>
29#include <linux/percpu.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
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 Jonesc29f1402005-05-31 19:03:50 -070038#define MIN_FREQUENCY_UP_THRESHOLD (11)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define MAX_FREQUENCY_UP_THRESHOLD (100)
40
Dave Jones32ee8c32006-02-28 00:43:23 -050041/*
42 * The polling frequency of this governor depends on the capability of
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * the processor. Default polling frequency is 1000 times the transition
Dave Jones32ee8c32006-02-28 00:43:23 -050044 * latency of the processor. The governor will work on any processor with
45 * transition latency <= 10mS, using appropriate sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * rate.
47 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
48 * this governor will not work.
49 * All times here are in uS.
50 */
Dave Jones32ee8c32006-02-28 00:43:23 -050051static unsigned int def_sampling_rate;
Dave Jonesdf8b59b2005-09-20 12:39:35 -070052#define MIN_SAMPLING_RATE_RATIO (2)
53/* for correct statistics, we need at least 10 ticks between each measure */
54#define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
55#define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
57#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
Dave Jonese1318322005-05-31 19:03:50 -070058#define DEF_SAMPLING_DOWN_FACTOR (1)
59#define MAX_SAMPLING_DOWN_FACTOR (10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define TRANSITION_LATENCY_LIMIT (10 * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static void do_dbs_timer(void *data);
63
64struct cpu_dbs_info_s {
Dave Jones32ee8c32006-02-28 00:43:23 -050065 struct cpufreq_policy *cur_policy;
66 unsigned int prev_cpu_idle_up;
67 unsigned int prev_cpu_idle_down;
68 unsigned int enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
71
72static unsigned int dbs_enable; /* number of CPUs using this policy */
73
Dave Jones32ee8c32006-02-28 00:43:23 -050074static DEFINE_MUTEX (dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
76
77struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -050078 unsigned int sampling_rate;
79 unsigned int sampling_down_factor;
80 unsigned int up_threshold;
81 unsigned int ignore_nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
84static struct dbs_tuners dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -050085 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
86 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
Dave Jonesdac1c1a2005-05-31 19:03:49 -070089static inline unsigned int get_cpu_idle_time(unsigned int cpu)
90{
91 return kstat_cpu(cpu).cpustat.idle +
92 kstat_cpu(cpu).cpustat.iowait +
Alexander Clouter001893c2005-12-01 01:09:25 -080093 ( dbs_tuners_ins.ignore_nice ?
Dave Jonesdac1c1a2005-05-31 19:03:49 -070094 kstat_cpu(cpu).cpustat.nice :
95 0);
96}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/************************** sysfs interface ************************/
99static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
100{
101 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
102}
103
104static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
105{
106 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
107}
108
Dave Jones32ee8c32006-02-28 00:43:23 -0500109#define define_one_ro(_name) \
110static struct freq_attr _name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111__ATTR(_name, 0444, show_##_name, NULL)
112
113define_one_ro(sampling_rate_max);
114define_one_ro(sampling_rate_min);
115
116/* cpufreq_ondemand Governor Tunables */
117#define show_one(file_name, object) \
118static ssize_t show_##file_name \
119(struct cpufreq_policy *unused, char *buf) \
120{ \
121 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
122}
123show_one(sampling_rate, sampling_rate);
124show_one(sampling_down_factor, sampling_down_factor);
125show_one(up_threshold, up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800126show_one(ignore_nice_load, ignore_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Dave Jones32ee8c32006-02-28 00:43:23 -0500128static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 const char *buf, size_t count)
130{
131 unsigned int input;
132 int ret;
133 ret = sscanf (buf, "%u", &input);
134 if (ret != 1 )
135 return -EINVAL;
136
Dave Jonese1318322005-05-31 19:03:50 -0700137 if (input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
138 return -EINVAL;
139
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800140 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 dbs_tuners_ins.sampling_down_factor = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800142 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return count;
145}
146
Dave Jones32ee8c32006-02-28 00:43:23 -0500147static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *buf, size_t count)
149{
150 unsigned int input;
151 int ret;
152 ret = sscanf (buf, "%u", &input);
153
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800154 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800156 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EINVAL;
158 }
159
160 dbs_tuners_ins.sampling_rate = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800161 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 return count;
164}
165
Dave Jones32ee8c32006-02-28 00:43:23 -0500166static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 const char *buf, size_t count)
168{
169 unsigned int input;
170 int ret;
171 ret = sscanf (buf, "%u", &input);
172
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800173 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500174 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700175 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800176 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return -EINVAL;
178 }
179
180 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800181 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 return count;
184}
185
Alexander Clouter001893c2005-12-01 01:09:25 -0800186static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700187 const char *buf, size_t count)
188{
189 unsigned int input;
190 int ret;
191
192 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500193
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700194 ret = sscanf (buf, "%u", &input);
195 if ( ret != 1 )
196 return -EINVAL;
197
198 if ( input > 1 )
199 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500200
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800201 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700202 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800203 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700204 return count;
205 }
206 dbs_tuners_ins.ignore_nice = input;
207
208 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700209 for_each_online_cpu(j) {
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700210 struct cpu_dbs_info_s *j_dbs_info;
211 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700212 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700213 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
214 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800215 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700216
217 return count;
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define define_one_rw(_name) \
221static struct freq_attr _name = \
222__ATTR(_name, 0644, show_##_name, store_##_name)
223
224define_one_rw(sampling_rate);
225define_one_rw(sampling_down_factor);
226define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800227define_one_rw(ignore_nice_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229static struct attribute * dbs_attributes[] = {
230 &sampling_rate_max.attr,
231 &sampling_rate_min.attr,
232 &sampling_rate.attr,
233 &sampling_down_factor.attr,
234 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800235 &ignore_nice_load.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 NULL
237};
238
239static struct attribute_group dbs_attr_group = {
240 .attrs = dbs_attributes,
241 .name = "ondemand",
242};
243
244/************************** sysfs end ************************/
245
246static void dbs_check_cpu(int cpu)
247{
Dave Jonesc29f1402005-05-31 19:03:50 -0700248 unsigned int idle_ticks, up_idle_ticks, total_ticks;
249 unsigned int freq_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 unsigned int freq_down_sampling_rate;
251 static int down_skip[NR_CPUS];
252 struct cpu_dbs_info_s *this_dbs_info;
253
254 struct cpufreq_policy *policy;
255 unsigned int j;
256
257 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
258 if (!this_dbs_info->enable)
259 return;
260
261 policy = this_dbs_info->cur_policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500262 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700263 * Every sampling_rate, we check, if current idle time is less
264 * than 20% (default), then we try to increase frequency
265 * Every sampling_rate*sampling_down_factor, we look for a the lowest
266 * frequency which can sustain the load while keeping idle time over
267 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500269 * Any frequency increase takes it to the maximum frequency.
270 * Frequency reduction happens at minimum steps of
271 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
273
274 /* Check for frequency increase */
Dave Jones9c7d2692005-05-31 19:03:49 -0700275 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 for_each_cpu_mask(j, policy->cpus) {
Dave Jones9c7d2692005-05-31 19:03:49 -0700277 unsigned int tmp_idle_ticks, total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct cpu_dbs_info_s *j_dbs_info;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700281 total_idle_ticks = get_cpu_idle_time(j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 tmp_idle_ticks = total_idle_ticks -
283 j_dbs_info->prev_cpu_idle_up;
284 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
285
286 if (tmp_idle_ticks < idle_ticks)
287 idle_ticks = tmp_idle_ticks;
288 }
289
290 /* Scale idle ticks by 100 and compare with up and down ticks */
291 idle_ticks *= 100;
292 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
Dave Jones6fe71162005-05-31 19:03:44 -0700293 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (idle_ticks < up_idle_ticks) {
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700296 down_skip[cpu] = 0;
Dave Jones790d76f2005-05-31 19:03:49 -0700297 for_each_cpu_mask(j, policy->cpus) {
298 struct cpu_dbs_info_s *j_dbs_info;
299
300 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jones32ee8c32006-02-28 00:43:23 -0500301 j_dbs_info->prev_cpu_idle_down =
Dave Jones790d76f2005-05-31 19:03:49 -0700302 j_dbs_info->prev_cpu_idle_up;
303 }
Dave Jonesc11420a2005-05-31 19:03:48 -0700304 /* if we are already at full speed then break out early */
305 if (policy->cur == policy->max)
306 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500307
308 __cpufreq_driver_target(policy, policy->max,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return;
311 }
312
313 /* Check for frequency decrease */
314 down_skip[cpu]++;
315 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
316 return;
317
Dave Jones9c7d2692005-05-31 19:03:49 -0700318 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 for_each_cpu_mask(j, policy->cpus) {
Dave Jones9c7d2692005-05-31 19:03:49 -0700320 unsigned int tmp_idle_ticks, total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct cpu_dbs_info_s *j_dbs_info;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700324 /* Check for frequency decrease */
325 total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 tmp_idle_ticks = total_idle_ticks -
327 j_dbs_info->prev_cpu_idle_down;
328 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
329
330 if (tmp_idle_ticks < idle_ticks)
331 idle_ticks = tmp_idle_ticks;
332 }
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 down_skip[cpu] = 0;
Dave Jonesc29f1402005-05-31 19:03:50 -0700335 /* if we cannot reduce the frequency anymore, break out early */
336 if (policy->cur == policy->min)
337 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dave Jonesc29f1402005-05-31 19:03:50 -0700339 /* Compute how many ticks there are between two measurements */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
341 dbs_tuners_ins.sampling_down_factor;
Dave Jonesc29f1402005-05-31 19:03:50 -0700342 total_ticks = usecs_to_jiffies(freq_down_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Dave Jonesc29f1402005-05-31 19:03:50 -0700344 /*
345 * The optimal frequency is the frequency that is the lowest that
346 * can support the current CPU usage without triggering the up
347 * policy. To be safe, we focus 10 points under the threshold.
348 */
349 freq_next = ((total_ticks - idle_ticks) * 100) / total_ticks;
Dave Jones32ee8c32006-02-28 00:43:23 -0500350 freq_next = (freq_next * policy->cur) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700351 (dbs_tuners_ins.up_threshold - 10);
Dave Jones1206aaa2005-05-31 19:03:48 -0700352
Dave Jonesc29f1402005-05-31 19:03:50 -0700353 if (freq_next <= ((policy->cur * 95) / 100))
354 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static void do_dbs_timer(void *data)
Dave Jones32ee8c32006-02-28 00:43:23 -0500358{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int i;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800360 mutex_lock(&dbs_mutex);
Dave Jones6fe71162005-05-31 19:03:44 -0700361 for_each_online_cpu(i)
362 dbs_check_cpu(i);
Dave Jones32ee8c32006-02-28 00:43:23 -0500363 schedule_delayed_work(&dbs_work,
Dave Jones6fe71162005-05-31 19:03:44 -0700364 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800365 mutex_unlock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500366}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368static inline void dbs_timer_init(void)
369{
370 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
371 schedule_delayed_work(&dbs_work,
Dave Jones6fe71162005-05-31 19:03:44 -0700372 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return;
374}
375
376static inline void dbs_timer_exit(void)
377{
378 cancel_delayed_work(&dbs_work);
379 return;
380}
381
382static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
383 unsigned int event)
384{
385 unsigned int cpu = policy->cpu;
386 struct cpu_dbs_info_s *this_dbs_info;
387 unsigned int j;
388
389 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
390
391 switch (event) {
392 case CPUFREQ_GOV_START:
Dave Jones32ee8c32006-02-28 00:43:23 -0500393 if ((!cpu_online(cpu)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 (!policy->cur))
395 return -EINVAL;
396
397 if (policy->cpuinfo.transition_latency >
398 (TRANSITION_LATENCY_LIMIT * 1000))
399 return -EINVAL;
400 if (this_dbs_info->enable) /* Already enabled */
401 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500402
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800403 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 for_each_cpu_mask(j, policy->cpus) {
405 struct cpu_dbs_info_s *j_dbs_info;
406 j_dbs_info = &per_cpu(cpu_dbs_info, j);
407 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500408
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700409 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700410 j_dbs_info->prev_cpu_idle_down
411 = j_dbs_info->prev_cpu_idle_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 this_dbs_info->enable = 1;
414 sysfs_create_group(&policy->kobj, &dbs_attr_group);
415 dbs_enable++;
416 /*
417 * Start the timerschedule work, when this governor
418 * is used for first time
419 */
420 if (dbs_enable == 1) {
421 unsigned int latency;
422 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700423 latency = policy->cpuinfo.transition_latency / 1000;
424 if (latency == 0)
425 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700427 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700429
430 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
431 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700434 dbs_tuners_ins.ignore_nice = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 dbs_timer_init();
437 }
Dave Jones32ee8c32006-02-28 00:43:23 -0500438
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800439 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441
442 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800443 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 this_dbs_info->enable = 0;
445 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
446 dbs_enable--;
447 /*
448 * Stop the timerschedule work, when this governor
449 * is used for first time
450 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500451 if (dbs_enable == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 dbs_timer_exit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500453
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800454 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 break;
457
458 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800459 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (policy->max < this_dbs_info->cur_policy->cur)
461 __cpufreq_driver_target(
462 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500463 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 else if (policy->min > this_dbs_info->cur_policy->cur)
465 __cpufreq_driver_target(
466 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500467 policy->min, CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800468 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
470 }
471 return 0;
472}
473
Dave Jones7f335d42005-05-31 19:03:46 -0700474static struct cpufreq_governor cpufreq_gov_dbs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 .name = "ondemand",
476 .governor = cpufreq_governor_dbs,
477 .owner = THIS_MODULE,
478};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480static int __init cpufreq_gov_dbs_init(void)
481{
482 return cpufreq_register_governor(&cpufreq_gov_dbs);
483}
484
485static void __exit cpufreq_gov_dbs_exit(void)
486{
487 /* Make sure that the scheduled work is indeed not running */
488 flush_scheduled_work();
489
490 cpufreq_unregister_governor(&cpufreq_gov_dbs);
491}
492
493
494MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
495MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
496 "Low Latency Frequency Transition capable processors");
497MODULE_LICENSE ("GPL");
498
499module_init(cpufreq_gov_dbs_init);
500module_exit(cpufreq_gov_dbs_exit);