blob: 956d121cb16151c2c3f564c4ccabde21250afbac [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,
Eric Piel9cbad612006-03-10 11:35:27 +020087 .ignore_nice = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Dave Jonesdac1c1a2005-05-31 19:03:49 -070090static inline unsigned int get_cpu_idle_time(unsigned int cpu)
91{
92 return kstat_cpu(cpu).cpustat.idle +
93 kstat_cpu(cpu).cpustat.iowait +
Alexander Clouter001893c2005-12-01 01:09:25 -080094 ( dbs_tuners_ins.ignore_nice ?
Dave Jonesdac1c1a2005-05-31 19:03:49 -070095 kstat_cpu(cpu).cpustat.nice :
96 0);
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/************************** sysfs interface ************************/
100static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
101{
102 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
103}
104
105static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
106{
107 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
108}
109
Dave Jones32ee8c32006-02-28 00:43:23 -0500110#define define_one_ro(_name) \
111static struct freq_attr _name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112__ATTR(_name, 0444, show_##_name, NULL)
113
114define_one_ro(sampling_rate_max);
115define_one_ro(sampling_rate_min);
116
117/* cpufreq_ondemand Governor Tunables */
118#define show_one(file_name, object) \
119static ssize_t show_##file_name \
120(struct cpufreq_policy *unused, char *buf) \
121{ \
122 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
123}
124show_one(sampling_rate, sampling_rate);
125show_one(sampling_down_factor, sampling_down_factor);
126show_one(up_threshold, up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800127show_one(ignore_nice_load, ignore_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Dave Jones32ee8c32006-02-28 00:43:23 -0500129static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 const char *buf, size_t count)
131{
132 unsigned int input;
133 int ret;
134 ret = sscanf (buf, "%u", &input);
135 if (ret != 1 )
136 return -EINVAL;
137
Dave Jonese1318322005-05-31 19:03:50 -0700138 if (input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
139 return -EINVAL;
140
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800141 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 dbs_tuners_ins.sampling_down_factor = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800143 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 return count;
146}
147
Dave Jones32ee8c32006-02-28 00:43:23 -0500148static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 const char *buf, size_t count)
150{
151 unsigned int input;
152 int ret;
153 ret = sscanf (buf, "%u", &input);
154
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800155 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800157 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -EINVAL;
159 }
160
161 dbs_tuners_ins.sampling_rate = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800162 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return count;
165}
166
Dave Jones32ee8c32006-02-28 00:43:23 -0500167static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 const char *buf, size_t count)
169{
170 unsigned int input;
171 int ret;
172 ret = sscanf (buf, "%u", &input);
173
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800174 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500175 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700176 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800177 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return -EINVAL;
179 }
180
181 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800182 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 return count;
185}
186
Alexander Clouter001893c2005-12-01 01:09:25 -0800187static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700188 const char *buf, size_t count)
189{
190 unsigned int input;
191 int ret;
192
193 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500194
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700195 ret = sscanf (buf, "%u", &input);
196 if ( ret != 1 )
197 return -EINVAL;
198
199 if ( input > 1 )
200 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500201
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800202 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700203 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800204 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700205 return count;
206 }
207 dbs_tuners_ins.ignore_nice = input;
208
209 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700210 for_each_online_cpu(j) {
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700211 struct cpu_dbs_info_s *j_dbs_info;
212 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700213 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700214 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
215 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800216 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700217
218 return count;
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#define define_one_rw(_name) \
222static struct freq_attr _name = \
223__ATTR(_name, 0644, show_##_name, store_##_name)
224
225define_one_rw(sampling_rate);
226define_one_rw(sampling_down_factor);
227define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800228define_one_rw(ignore_nice_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230static struct attribute * dbs_attributes[] = {
231 &sampling_rate_max.attr,
232 &sampling_rate_min.attr,
233 &sampling_rate.attr,
234 &sampling_down_factor.attr,
235 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800236 &ignore_nice_load.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 NULL
238};
239
240static struct attribute_group dbs_attr_group = {
241 .attrs = dbs_attributes,
242 .name = "ondemand",
243};
244
245/************************** sysfs end ************************/
246
247static void dbs_check_cpu(int cpu)
248{
Dave Jonesc29f1402005-05-31 19:03:50 -0700249 unsigned int idle_ticks, up_idle_ticks, total_ticks;
250 unsigned int freq_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned int freq_down_sampling_rate;
252 static int down_skip[NR_CPUS];
253 struct cpu_dbs_info_s *this_dbs_info;
254
255 struct cpufreq_policy *policy;
256 unsigned int j;
257
258 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
259 if (!this_dbs_info->enable)
260 return;
261
262 policy = this_dbs_info->cur_policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500263 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700264 * Every sampling_rate, we check, if current idle time is less
265 * than 20% (default), then we try to increase frequency
266 * Every sampling_rate*sampling_down_factor, we look for a the lowest
267 * frequency which can sustain the load while keeping idle time over
268 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500270 * Any frequency increase takes it to the maximum frequency.
271 * Frequency reduction happens at minimum steps of
272 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
274
275 /* Check for frequency increase */
Dave Jones9c7d2692005-05-31 19:03:49 -0700276 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 for_each_cpu_mask(j, policy->cpus) {
Dave Jones9c7d2692005-05-31 19:03:49 -0700278 unsigned int tmp_idle_ticks, total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct cpu_dbs_info_s *j_dbs_info;
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700282 total_idle_ticks = get_cpu_idle_time(j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 tmp_idle_ticks = total_idle_ticks -
284 j_dbs_info->prev_cpu_idle_up;
285 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
286
287 if (tmp_idle_ticks < idle_ticks)
288 idle_ticks = tmp_idle_ticks;
289 }
290
291 /* Scale idle ticks by 100 and compare with up and down ticks */
292 idle_ticks *= 100;
293 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
Dave Jones6fe71162005-05-31 19:03:44 -0700294 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (idle_ticks < up_idle_ticks) {
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700297 down_skip[cpu] = 0;
Dave Jones790d76f2005-05-31 19:03:49 -0700298 for_each_cpu_mask(j, policy->cpus) {
299 struct cpu_dbs_info_s *j_dbs_info;
300
301 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jones32ee8c32006-02-28 00:43:23 -0500302 j_dbs_info->prev_cpu_idle_down =
Dave Jones790d76f2005-05-31 19:03:49 -0700303 j_dbs_info->prev_cpu_idle_up;
304 }
Dave Jonesc11420a2005-05-31 19:03:48 -0700305 /* if we are already at full speed then break out early */
306 if (policy->cur == policy->max)
307 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500308
309 __cpufreq_driver_target(policy, policy->max,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return;
312 }
313
314 /* Check for frequency decrease */
315 down_skip[cpu]++;
316 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
317 return;
318
Dave Jones9c7d2692005-05-31 19:03:49 -0700319 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 for_each_cpu_mask(j, policy->cpus) {
Dave Jones9c7d2692005-05-31 19:03:49 -0700321 unsigned int tmp_idle_ticks, total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct cpu_dbs_info_s *j_dbs_info;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700325 /* Check for frequency decrease */
326 total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 tmp_idle_ticks = total_idle_ticks -
328 j_dbs_info->prev_cpu_idle_down;
329 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
330
331 if (tmp_idle_ticks < idle_ticks)
332 idle_ticks = tmp_idle_ticks;
333 }
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 down_skip[cpu] = 0;
Dave Jonesc29f1402005-05-31 19:03:50 -0700336 /* if we cannot reduce the frequency anymore, break out early */
337 if (policy->cur == policy->min)
338 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Dave Jonesc29f1402005-05-31 19:03:50 -0700340 /* Compute how many ticks there are between two measurements */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
342 dbs_tuners_ins.sampling_down_factor;
Dave Jonesc29f1402005-05-31 19:03:50 -0700343 total_ticks = usecs_to_jiffies(freq_down_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Dave Jonesc29f1402005-05-31 19:03:50 -0700345 /*
346 * The optimal frequency is the frequency that is the lowest that
347 * can support the current CPU usage without triggering the up
348 * policy. To be safe, we focus 10 points under the threshold.
349 */
350 freq_next = ((total_ticks - idle_ticks) * 100) / total_ticks;
Dave Jones32ee8c32006-02-28 00:43:23 -0500351 freq_next = (freq_next * policy->cur) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700352 (dbs_tuners_ins.up_threshold - 10);
Dave Jones1206aaa2005-05-31 19:03:48 -0700353
Dominik Brodowski7c9d8c02006-03-26 11:11:03 +0200354 if (freq_next < policy->min)
355 freq_next = policy->min;
356
Dave Jonesc29f1402005-05-31 19:03:50 -0700357 if (freq_next <= ((policy->cur * 95) / 100))
358 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361static void do_dbs_timer(void *data)
Dave Jones32ee8c32006-02-28 00:43:23 -0500362{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int i;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800364 mutex_lock(&dbs_mutex);
Dave Jones6fe71162005-05-31 19:03:44 -0700365 for_each_online_cpu(i)
366 dbs_check_cpu(i);
Dave Jones32ee8c32006-02-28 00:43:23 -0500367 schedule_delayed_work(&dbs_work,
Dave Jones6fe71162005-05-31 19:03:44 -0700368 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800369 mutex_unlock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372static inline void dbs_timer_init(void)
373{
374 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
375 schedule_delayed_work(&dbs_work,
Dave Jones6fe71162005-05-31 19:03:44 -0700376 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return;
378}
379
380static inline void dbs_timer_exit(void)
381{
382 cancel_delayed_work(&dbs_work);
383 return;
384}
385
386static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
387 unsigned int event)
388{
389 unsigned int cpu = policy->cpu;
390 struct cpu_dbs_info_s *this_dbs_info;
391 unsigned int j;
392
393 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
394
395 switch (event) {
396 case CPUFREQ_GOV_START:
Dave Jones32ee8c32006-02-28 00:43:23 -0500397 if ((!cpu_online(cpu)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 (!policy->cur))
399 return -EINVAL;
400
401 if (policy->cpuinfo.transition_latency >
Eric Pielff8c2882006-03-10 11:34:16 +0200402 (TRANSITION_LATENCY_LIMIT * 1000)) {
403 printk(KERN_WARNING "ondemand governor failed to load "
404 "due to too long transition latency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -EINVAL;
Eric Pielff8c2882006-03-10 11:34:16 +0200406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (this_dbs_info->enable) /* Already enabled */
408 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500409
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800410 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 for_each_cpu_mask(j, policy->cpus) {
412 struct cpu_dbs_info_s *j_dbs_info;
413 j_dbs_info = &per_cpu(cpu_dbs_info, j);
414 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500415
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700416 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700417 j_dbs_info->prev_cpu_idle_down
418 = j_dbs_info->prev_cpu_idle_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 this_dbs_info->enable = 1;
421 sysfs_create_group(&policy->kobj, &dbs_attr_group);
422 dbs_enable++;
423 /*
424 * Start the timerschedule work, when this governor
425 * is used for first time
426 */
427 if (dbs_enable == 1) {
428 unsigned int latency;
429 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700430 latency = policy->cpuinfo.transition_latency / 1000;
431 if (latency == 0)
432 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700434 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700436
437 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
438 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 dbs_timer_init();
442 }
Dave Jones32ee8c32006-02-28 00:43:23 -0500443
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800444 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446
447 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800448 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 this_dbs_info->enable = 0;
450 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
451 dbs_enable--;
452 /*
453 * Stop the timerschedule work, when this governor
454 * is used for first time
455 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500456 if (dbs_enable == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 dbs_timer_exit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500458
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800459 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 break;
462
463 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800464 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (policy->max < this_dbs_info->cur_policy->cur)
466 __cpufreq_driver_target(
467 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500468 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 else if (policy->min > this_dbs_info->cur_policy->cur)
470 __cpufreq_driver_target(
471 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500472 policy->min, CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800473 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 }
476 return 0;
477}
478
Dave Jones7f335d42005-05-31 19:03:46 -0700479static struct cpufreq_governor cpufreq_gov_dbs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 .name = "ondemand",
481 .governor = cpufreq_governor_dbs,
482 .owner = THIS_MODULE,
483};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485static int __init cpufreq_gov_dbs_init(void)
486{
487 return cpufreq_register_governor(&cpufreq_gov_dbs);
488}
489
490static void __exit cpufreq_gov_dbs_exit(void)
491{
492 /* Make sure that the scheduled work is indeed not running */
493 flush_scheduled_work();
494
495 cpufreq_unregister_governor(&cpufreq_gov_dbs);
496}
497
498
499MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
500MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
501 "Low Latency Frequency Transition capable processors");
502MODULE_LICENSE ("GPL");
503
504module_init(cpufreq_gov_dbs_init);
505module_exit(cpufreq_gov_dbs_exit);