blob: 4d308410b60eb2512d45cc1270353dfa45a579bf [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
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -070074/*
75 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
76 * lock and dbs_mutex. cpu_hotplug lock should always be held before
77 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
78 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
79 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
80 * is recursive for the same process. -Venki
81 */
Dave Jones32ee8c32006-02-28 00:43:23 -050082static DEFINE_MUTEX (dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
84
Andi Kleen6810b542006-05-08 15:17:31 +020085static struct workqueue_struct *dbs_workq;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -050088 unsigned int sampling_rate;
89 unsigned int sampling_down_factor;
90 unsigned int up_threshold;
91 unsigned int ignore_nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94static struct dbs_tuners dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -050095 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
96 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
Eric Piel9cbad612006-03-10 11:35:27 +020097 .ignore_nice = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700100static inline unsigned int get_cpu_idle_time(unsigned int cpu)
101{
102 return kstat_cpu(cpu).cpustat.idle +
103 kstat_cpu(cpu).cpustat.iowait +
Alexander Clouter001893c2005-12-01 01:09:25 -0800104 ( dbs_tuners_ins.ignore_nice ?
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700105 kstat_cpu(cpu).cpustat.nice :
106 0);
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/************************** sysfs interface ************************/
110static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
111{
112 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
113}
114
115static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
116{
117 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
118}
119
Dave Jones32ee8c32006-02-28 00:43:23 -0500120#define define_one_ro(_name) \
121static struct freq_attr _name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122__ATTR(_name, 0444, show_##_name, NULL)
123
124define_one_ro(sampling_rate_max);
125define_one_ro(sampling_rate_min);
126
127/* cpufreq_ondemand Governor Tunables */
128#define show_one(file_name, object) \
129static ssize_t show_##file_name \
130(struct cpufreq_policy *unused, char *buf) \
131{ \
132 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
133}
134show_one(sampling_rate, sampling_rate);
135show_one(sampling_down_factor, sampling_down_factor);
136show_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_down_factor(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 if (ret != 1 )
146 return -EINVAL;
147
Dave Jonese1318322005-05-31 19:03:50 -0700148 if (input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
149 return -EINVAL;
150
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800151 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 dbs_tuners_ins.sampling_down_factor = 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_sampling_rate(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800167 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -EINVAL;
169 }
170
171 dbs_tuners_ins.sampling_rate = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800172 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 return count;
175}
176
Dave Jones32ee8c32006-02-28 00:43:23 -0500177static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 const char *buf, size_t count)
179{
180 unsigned int input;
181 int ret;
182 ret = sscanf (buf, "%u", &input);
183
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800184 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500185 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700186 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800187 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EINVAL;
189 }
190
191 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800192 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 return count;
195}
196
Alexander Clouter001893c2005-12-01 01:09:25 -0800197static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700198 const char *buf, size_t count)
199{
200 unsigned int input;
201 int ret;
202
203 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500204
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700205 ret = sscanf (buf, "%u", &input);
206 if ( ret != 1 )
207 return -EINVAL;
208
209 if ( input > 1 )
210 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500211
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800212 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700213 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800214 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700215 return count;
216 }
217 dbs_tuners_ins.ignore_nice = input;
218
219 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700220 for_each_online_cpu(j) {
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700221 struct cpu_dbs_info_s *j_dbs_info;
222 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700223 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700224 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
225 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800226 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700227
228 return count;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#define define_one_rw(_name) \
232static struct freq_attr _name = \
233__ATTR(_name, 0644, show_##_name, store_##_name)
234
235define_one_rw(sampling_rate);
236define_one_rw(sampling_down_factor);
237define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800238define_one_rw(ignore_nice_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240static struct attribute * dbs_attributes[] = {
241 &sampling_rate_max.attr,
242 &sampling_rate_min.attr,
243 &sampling_rate.attr,
244 &sampling_down_factor.attr,
245 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800246 &ignore_nice_load.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 NULL
248};
249
250static struct attribute_group dbs_attr_group = {
251 .attrs = dbs_attributes,
252 .name = "ondemand",
253};
254
255/************************** sysfs end ************************/
256
257static void dbs_check_cpu(int cpu)
258{
Dave Jonesc29f1402005-05-31 19:03:50 -0700259 unsigned int idle_ticks, up_idle_ticks, total_ticks;
260 unsigned int freq_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 unsigned int freq_down_sampling_rate;
262 static int down_skip[NR_CPUS];
263 struct cpu_dbs_info_s *this_dbs_info;
264
265 struct cpufreq_policy *policy;
266 unsigned int j;
267
268 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
269 if (!this_dbs_info->enable)
270 return;
271
272 policy = this_dbs_info->cur_policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500273 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700274 * Every sampling_rate, we check, if current idle time is less
275 * than 20% (default), then we try to increase frequency
276 * Every sampling_rate*sampling_down_factor, we look for a the lowest
277 * frequency which can sustain the load while keeping idle time over
278 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500280 * Any frequency increase takes it to the maximum frequency.
281 * Frequency reduction happens at minimum steps of
282 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
284
285 /* Check for frequency increase */
Dave Jones9c7d2692005-05-31 19:03:49 -0700286 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 for_each_cpu_mask(j, policy->cpus) {
Dave Jones9c7d2692005-05-31 19:03:49 -0700288 unsigned int tmp_idle_ticks, total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct cpu_dbs_info_s *j_dbs_info;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700292 total_idle_ticks = get_cpu_idle_time(j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 tmp_idle_ticks = total_idle_ticks -
294 j_dbs_info->prev_cpu_idle_up;
295 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
296
297 if (tmp_idle_ticks < idle_ticks)
298 idle_ticks = tmp_idle_ticks;
299 }
300
301 /* Scale idle ticks by 100 and compare with up and down ticks */
302 idle_ticks *= 100;
303 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
Dave Jones6fe71162005-05-31 19:03:44 -0700304 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (idle_ticks < up_idle_ticks) {
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700307 down_skip[cpu] = 0;
Dave Jones790d76f2005-05-31 19:03:49 -0700308 for_each_cpu_mask(j, policy->cpus) {
309 struct cpu_dbs_info_s *j_dbs_info;
310
311 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jones32ee8c32006-02-28 00:43:23 -0500312 j_dbs_info->prev_cpu_idle_down =
Dave Jones790d76f2005-05-31 19:03:49 -0700313 j_dbs_info->prev_cpu_idle_up;
314 }
Dave Jonesc11420a2005-05-31 19:03:48 -0700315 /* if we are already at full speed then break out early */
316 if (policy->cur == policy->max)
317 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500318
319 __cpufreq_driver_target(policy, policy->max,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return;
322 }
323
324 /* Check for frequency decrease */
325 down_skip[cpu]++;
326 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
327 return;
328
Dave Jones9c7d2692005-05-31 19:03:49 -0700329 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 for_each_cpu_mask(j, policy->cpus) {
Dave Jones9c7d2692005-05-31 19:03:49 -0700331 unsigned int tmp_idle_ticks, total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct cpu_dbs_info_s *j_dbs_info;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700335 /* Check for frequency decrease */
336 total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 tmp_idle_ticks = total_idle_ticks -
338 j_dbs_info->prev_cpu_idle_down;
339 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
340
341 if (tmp_idle_ticks < idle_ticks)
342 idle_ticks = tmp_idle_ticks;
343 }
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 down_skip[cpu] = 0;
Dave Jonesc29f1402005-05-31 19:03:50 -0700346 /* if we cannot reduce the frequency anymore, break out early */
347 if (policy->cur == policy->min)
348 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Dave Jonesc29f1402005-05-31 19:03:50 -0700350 /* Compute how many ticks there are between two measurements */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
352 dbs_tuners_ins.sampling_down_factor;
Dave Jonesc29f1402005-05-31 19:03:50 -0700353 total_ticks = usecs_to_jiffies(freq_down_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Dave Jonesc29f1402005-05-31 19:03:50 -0700355 /*
356 * The optimal frequency is the frequency that is the lowest that
357 * can support the current CPU usage without triggering the up
358 * policy. To be safe, we focus 10 points under the threshold.
359 */
360 freq_next = ((total_ticks - idle_ticks) * 100) / total_ticks;
Dave Jones32ee8c32006-02-28 00:43:23 -0500361 freq_next = (freq_next * policy->cur) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700362 (dbs_tuners_ins.up_threshold - 10);
Dave Jones1206aaa2005-05-31 19:03:48 -0700363
Dominik Brodowski7c9d8c02006-03-26 11:11:03 +0200364 if (freq_next < policy->min)
365 freq_next = policy->min;
366
Dave Jonesc29f1402005-05-31 19:03:50 -0700367 if (freq_next <= ((policy->cur * 95) / 100))
368 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371static void do_dbs_timer(void *data)
Dave Jones32ee8c32006-02-28 00:43:23 -0500372{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 int i;
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700374 lock_cpu_hotplug();
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800375 mutex_lock(&dbs_mutex);
Dave Jones6fe71162005-05-31 19:03:44 -0700376 for_each_online_cpu(i)
377 dbs_check_cpu(i);
Andi Kleen6810b542006-05-08 15:17:31 +0200378 queue_delayed_work(dbs_workq, &dbs_work,
379 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800380 mutex_unlock(&dbs_mutex);
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700381 unlock_cpu_hotplug();
Dave Jones32ee8c32006-02-28 00:43:23 -0500382}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384static inline void dbs_timer_init(void)
385{
386 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
Andi Kleen6810b542006-05-08 15:17:31 +0200387 if (!dbs_workq)
388 dbs_workq = create_singlethread_workqueue("ondemand");
389 if (!dbs_workq) {
390 printk(KERN_ERR "ondemand: Cannot initialize kernel thread\n");
391 return;
392 }
393 queue_delayed_work(dbs_workq, &dbs_work,
394 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return;
396}
397
398static inline void dbs_timer_exit(void)
399{
Andi Kleen6810b542006-05-08 15:17:31 +0200400 if (dbs_workq)
401 cancel_rearming_delayed_workqueue(dbs_workq, &dbs_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
405 unsigned int event)
406{
407 unsigned int cpu = policy->cpu;
408 struct cpu_dbs_info_s *this_dbs_info;
409 unsigned int j;
410
411 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
412
413 switch (event) {
414 case CPUFREQ_GOV_START:
Dave Jones32ee8c32006-02-28 00:43:23 -0500415 if ((!cpu_online(cpu)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 (!policy->cur))
417 return -EINVAL;
418
419 if (policy->cpuinfo.transition_latency >
Eric Pielff8c2882006-03-10 11:34:16 +0200420 (TRANSITION_LATENCY_LIMIT * 1000)) {
421 printk(KERN_WARNING "ondemand governor failed to load "
422 "due to too long transition latency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -EINVAL;
Eric Pielff8c2882006-03-10 11:34:16 +0200424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (this_dbs_info->enable) /* Already enabled */
426 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500427
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800428 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 for_each_cpu_mask(j, policy->cpus) {
430 struct cpu_dbs_info_s *j_dbs_info;
431 j_dbs_info = &per_cpu(cpu_dbs_info, j);
432 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500433
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700434 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700435 j_dbs_info->prev_cpu_idle_down
436 = j_dbs_info->prev_cpu_idle_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 this_dbs_info->enable = 1;
439 sysfs_create_group(&policy->kobj, &dbs_attr_group);
440 dbs_enable++;
441 /*
442 * Start the timerschedule work, when this governor
443 * is used for first time
444 */
445 if (dbs_enable == 1) {
446 unsigned int latency;
447 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700448 latency = policy->cpuinfo.transition_latency / 1000;
449 if (latency == 0)
450 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700452 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700454
455 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
456 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 dbs_timer_init();
460 }
Dave Jones32ee8c32006-02-28 00:43:23 -0500461
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800462 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464
465 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800466 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 this_dbs_info->enable = 0;
468 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
469 dbs_enable--;
470 /*
471 * Stop the timerschedule work, when this governor
472 * is used for first time
473 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500474 if (dbs_enable == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 dbs_timer_exit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500476
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800477 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 break;
480
481 case CPUFREQ_GOV_LIMITS:
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700482 lock_cpu_hotplug();
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800483 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (policy->max < this_dbs_info->cur_policy->cur)
485 __cpufreq_driver_target(
486 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500487 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 else if (policy->min > this_dbs_info->cur_policy->cur)
489 __cpufreq_driver_target(
490 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500491 policy->min, CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800492 mutex_unlock(&dbs_mutex);
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700493 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 break;
495 }
496 return 0;
497}
498
Dave Jones7f335d42005-05-31 19:03:46 -0700499static struct cpufreq_governor cpufreq_gov_dbs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 .name = "ondemand",
501 .governor = cpufreq_governor_dbs,
502 .owner = THIS_MODULE,
503};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505static int __init cpufreq_gov_dbs_init(void)
506{
507 return cpufreq_register_governor(&cpufreq_gov_dbs);
508}
509
510static void __exit cpufreq_gov_dbs_exit(void)
511{
Andi Kleen6810b542006-05-08 15:17:31 +0200512 /* Make sure that the scheduled work is indeed not running.
513 Assumes the timer has been cancelled first. */
514 if (dbs_workq) {
515 flush_workqueue(dbs_workq);
516 destroy_workqueue(dbs_workq);
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 cpufreq_unregister_governor(&cpufreq_gov_dbs);
520}
521
522
523MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
524MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
525 "Low Latency Frequency Transition capable processors");
526MODULE_LICENSE ("GPL");
527
528module_init(cpufreq_gov_dbs_init);
529module_exit(cpufreq_gov_dbs_exit);