blob: a2add11e56f1c67941f7270f65135060c5336451 [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>
Andrew Morton138a01282006-06-23 03:31:19 -070024#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080031#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
34 * dbs is used in this file as a shortform for demandbased switching
35 * It helps to keep variable names smaller, simpler
36 */
37
38#define DEF_FREQUENCY_UP_THRESHOLD (80)
Dave Jonesc29f1402005-05-31 19:03:50 -070039#define MIN_FREQUENCY_UP_THRESHOLD (11)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define MAX_FREQUENCY_UP_THRESHOLD (100)
41
Dave Jones32ee8c32006-02-28 00:43:23 -050042/*
43 * The polling frequency of this governor depends on the capability of
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * the processor. Default polling frequency is 1000 times the transition
Dave Jones32ee8c32006-02-28 00:43:23 -050045 * latency of the processor. The governor will work on any processor with
46 * transition latency <= 10mS, using appropriate sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * rate.
48 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
49 * this governor will not work.
50 * All times here are in uS.
51 */
Dave Jones32ee8c32006-02-28 00:43:23 -050052static unsigned int def_sampling_rate;
Dave Jonesdf8b59b2005-09-20 12:39:35 -070053#define MIN_SAMPLING_RATE_RATIO (2)
54/* for correct statistics, we need at least 10 ticks between each measure */
55#define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
56#define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
58#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define TRANSITION_LATENCY_LIMIT (10 * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static void do_dbs_timer(void *data);
62
63struct cpu_dbs_info_s {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070064 cputime64_t prev_cpu_idle;
65 cputime64_t prev_cpu_wall;
Dave Jones32ee8c32006-02-28 00:43:23 -050066 struct cpufreq_policy *cur_policy;
Dave Jones32ee8c32006-02-28 00:43:23 -050067 unsigned int enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
70
71static unsigned int dbs_enable; /* number of CPUs using this policy */
72
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -070073/*
74 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
75 * lock and dbs_mutex. cpu_hotplug lock should always be held before
76 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
77 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
78 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
79 * is recursive for the same process. -Venki
80 */
Dave Jones32ee8c32006-02-28 00:43:23 -050081static DEFINE_MUTEX (dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
83
Andi Kleen6810b542006-05-08 15:17:31 +020084static struct workqueue_struct *dbs_workq;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -050087 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -050088 unsigned int up_threshold;
89 unsigned int ignore_nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
92static struct dbs_tuners dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -050093 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
Eric Piel9cbad612006-03-10 11:35:27 +020094 .ignore_nice = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070097static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
Dave Jonesdac1c1a2005-05-31 19:03:49 -070098{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070099 cputime64_t retval;
100
101 retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
102 kstat_cpu(cpu).cpustat.iowait);
103
104 if (dbs_tuners_ins.ignore_nice)
105 retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
106
107 return retval;
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/************************** sysfs interface ************************/
111static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
112{
113 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
114}
115
116static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
117{
118 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
119}
120
Dave Jones32ee8c32006-02-28 00:43:23 -0500121#define define_one_ro(_name) \
122static struct freq_attr _name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123__ATTR(_name, 0444, show_##_name, NULL)
124
125define_one_ro(sampling_rate_max);
126define_one_ro(sampling_rate_min);
127
128/* cpufreq_ondemand Governor Tunables */
129#define show_one(file_name, object) \
130static ssize_t show_##file_name \
131(struct cpufreq_policy *unused, char *buf) \
132{ \
133 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
134}
135show_one(sampling_rate, sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136show_one(up_threshold, up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800137show_one(ignore_nice_load, ignore_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Dave Jones32ee8c32006-02-28 00:43:23 -0500139static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 const char *buf, size_t count)
141{
142 unsigned int input;
143 int ret;
144 ret = sscanf (buf, "%u", &input);
145
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800146 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800148 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -EINVAL;
150 }
151
152 dbs_tuners_ins.sampling_rate = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800153 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return count;
156}
157
Dave Jones32ee8c32006-02-28 00:43:23 -0500158static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 const char *buf, size_t count)
160{
161 unsigned int input;
162 int ret;
163 ret = sscanf (buf, "%u", &input);
164
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800165 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500166 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700167 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800168 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -EINVAL;
170 }
171
172 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800173 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 return count;
176}
177
Alexander Clouter001893c2005-12-01 01:09:25 -0800178static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700179 const char *buf, size_t count)
180{
181 unsigned int input;
182 int ret;
183
184 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500185
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700186 ret = sscanf (buf, "%u", &input);
187 if ( ret != 1 )
188 return -EINVAL;
189
190 if ( input > 1 )
191 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500192
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800193 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700194 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800195 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700196 return count;
197 }
198 dbs_tuners_ins.ignore_nice = input;
199
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700200 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700201 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700202 struct cpu_dbs_info_s *dbs_info;
203 dbs_info = &per_cpu(cpu_dbs_info, j);
204 dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
205 dbs_info->prev_cpu_wall = get_jiffies_64();
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700206 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800207 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700208
209 return count;
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#define define_one_rw(_name) \
213static struct freq_attr _name = \
214__ATTR(_name, 0644, show_##_name, store_##_name)
215
216define_one_rw(sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800218define_one_rw(ignore_nice_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220static struct attribute * dbs_attributes[] = {
221 &sampling_rate_max.attr,
222 &sampling_rate_min.attr,
223 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800225 &ignore_nice_load.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 NULL
227};
228
229static struct attribute_group dbs_attr_group = {
230 .attrs = dbs_attributes,
231 .name = "ondemand",
232};
233
234/************************** sysfs end ************************/
235
236static void dbs_check_cpu(int cpu)
237{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700238 unsigned int idle_ticks, total_ticks;
239 unsigned int load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct cpu_dbs_info_s *this_dbs_info;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700241 cputime64_t cur_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 struct cpufreq_policy *policy;
244 unsigned int j;
245
246 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
247 if (!this_dbs_info->enable)
248 return;
249
250 policy = this_dbs_info->cur_policy;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700251 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
252 total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
253 this_dbs_info->prev_cpu_wall);
254 this_dbs_info->prev_cpu_wall = cur_jiffies;
Dave Jones32ee8c32006-02-28 00:43:23 -0500255 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700256 * Every sampling_rate, we check, if current idle time is less
257 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700258 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700259 * frequency which can sustain the load while keeping idle time over
260 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500262 * Any frequency increase takes it to the maximum frequency.
263 * Frequency reduction happens at minimum steps of
264 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
266
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700267 /* Get Idle Time */
Dave Jones9c7d2692005-05-31 19:03:49 -0700268 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 for_each_cpu_mask(j, policy->cpus) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700270 cputime64_t total_idle_ticks;
271 unsigned int tmp_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct cpu_dbs_info_s *j_dbs_info;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700275 total_idle_ticks = get_cpu_idle_time(j);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700276 tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
277 j_dbs_info->prev_cpu_idle);
278 j_dbs_info->prev_cpu_idle = total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (tmp_idle_ticks < idle_ticks)
281 idle_ticks = tmp_idle_ticks;
282 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700283 load = (100 * (total_ticks - idle_ticks)) / total_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700285 /* Check for frequency increase */
286 if (load > dbs_tuners_ins.up_threshold) {
Dave Jonesc11420a2005-05-31 19:03:48 -0700287 /* if we are already at full speed then break out early */
288 if (policy->cur == policy->max)
289 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500290
291 __cpufreq_driver_target(policy, policy->max,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return;
294 }
295
296 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700297 /* if we cannot reduce the frequency anymore, break out early */
298 if (policy->cur == policy->min)
299 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Dave Jonesc29f1402005-05-31 19:03:50 -0700301 /*
302 * The optimal frequency is the frequency that is the lowest that
303 * can support the current CPU usage without triggering the up
304 * policy. To be safe, we focus 10 points under the threshold.
305 */
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700306 if (load < (dbs_tuners_ins.up_threshold - 10)) {
307 unsigned int freq_next;
308 freq_next = (policy->cur * load) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700309 (dbs_tuners_ins.up_threshold - 10);
Dave Jones1206aaa2005-05-31 19:03:48 -0700310
Dave Jonesc29f1402005-05-31 19:03:50 -0700311 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315static void do_dbs_timer(void *data)
Dave Jones32ee8c32006-02-28 00:43:23 -0500316{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int i;
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700318 lock_cpu_hotplug();
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800319 mutex_lock(&dbs_mutex);
Dave Jones6fe71162005-05-31 19:03:44 -0700320 for_each_online_cpu(i)
321 dbs_check_cpu(i);
Andi Kleen6810b542006-05-08 15:17:31 +0200322 queue_delayed_work(dbs_workq, &dbs_work,
323 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800324 mutex_unlock(&dbs_mutex);
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700325 unlock_cpu_hotplug();
Dave Jones32ee8c32006-02-28 00:43:23 -0500326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328static inline void dbs_timer_init(void)
329{
330 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
Andi Kleen6810b542006-05-08 15:17:31 +0200331 if (!dbs_workq)
332 dbs_workq = create_singlethread_workqueue("ondemand");
333 if (!dbs_workq) {
334 printk(KERN_ERR "ondemand: Cannot initialize kernel thread\n");
335 return;
336 }
337 queue_delayed_work(dbs_workq, &dbs_work,
338 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return;
340}
341
342static inline void dbs_timer_exit(void)
343{
Andi Kleen6810b542006-05-08 15:17:31 +0200344 if (dbs_workq)
345 cancel_rearming_delayed_workqueue(dbs_workq, &dbs_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
349 unsigned int event)
350{
351 unsigned int cpu = policy->cpu;
352 struct cpu_dbs_info_s *this_dbs_info;
353 unsigned int j;
354
355 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
356
357 switch (event) {
358 case CPUFREQ_GOV_START:
Dave Jones32ee8c32006-02-28 00:43:23 -0500359 if ((!cpu_online(cpu)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 (!policy->cur))
361 return -EINVAL;
362
363 if (policy->cpuinfo.transition_latency >
Eric Pielff8c2882006-03-10 11:34:16 +0200364 (TRANSITION_LATENCY_LIMIT * 1000)) {
365 printk(KERN_WARNING "ondemand governor failed to load "
366 "due to too long transition latency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -EINVAL;
Eric Pielff8c2882006-03-10 11:34:16 +0200368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (this_dbs_info->enable) /* Already enabled */
370 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500371
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800372 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 for_each_cpu_mask(j, policy->cpus) {
374 struct cpu_dbs_info_s *j_dbs_info;
375 j_dbs_info = &per_cpu(cpu_dbs_info, j);
376 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500377
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700378 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
379 j_dbs_info->prev_cpu_wall = get_jiffies_64();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 this_dbs_info->enable = 1;
382 sysfs_create_group(&policy->kobj, &dbs_attr_group);
383 dbs_enable++;
384 /*
385 * Start the timerschedule work, when this governor
386 * is used for first time
387 */
388 if (dbs_enable == 1) {
389 unsigned int latency;
390 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700391 latency = policy->cpuinfo.transition_latency / 1000;
392 if (latency == 0)
393 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700395 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700397
398 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
399 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 dbs_timer_init();
403 }
Dave Jones32ee8c32006-02-28 00:43:23 -0500404
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800405 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 break;
407
408 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800409 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 this_dbs_info->enable = 0;
411 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
412 dbs_enable--;
413 /*
414 * Stop the timerschedule work, when this governor
415 * is used for first time
416 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500417 if (dbs_enable == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 dbs_timer_exit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500419
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800420 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 break;
423
424 case CPUFREQ_GOV_LIMITS:
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700425 lock_cpu_hotplug();
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800426 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (policy->max < this_dbs_info->cur_policy->cur)
428 __cpufreq_driver_target(
429 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500430 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 else if (policy->min > this_dbs_info->cur_policy->cur)
432 __cpufreq_driver_target(
433 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500434 policy->min, CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800435 mutex_unlock(&dbs_mutex);
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700436 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438 }
439 return 0;
440}
441
Dave Jones7f335d42005-05-31 19:03:46 -0700442static struct cpufreq_governor cpufreq_gov_dbs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 .name = "ondemand",
444 .governor = cpufreq_governor_dbs,
445 .owner = THIS_MODULE,
446};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448static int __init cpufreq_gov_dbs_init(void)
449{
450 return cpufreq_register_governor(&cpufreq_gov_dbs);
451}
452
453static void __exit cpufreq_gov_dbs_exit(void)
454{
Andi Kleen6810b542006-05-08 15:17:31 +0200455 /* Make sure that the scheduled work is indeed not running.
456 Assumes the timer has been cancelled first. */
457 if (dbs_workq) {
458 flush_workqueue(dbs_workq);
459 destroy_workqueue(dbs_workq);
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 cpufreq_unregister_governor(&cpufreq_gov_dbs);
463}
464
465
466MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
467MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
468 "Low Latency Frequency Transition capable processors");
469MODULE_LICENSE ("GPL");
470
471module_init(cpufreq_gov_dbs_init);
472module_exit(cpufreq_gov_dbs_exit);