blob: 693e540481b4cb88095664c0d2b6f55979d17a10 [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)
Dave Jonese1318322005-05-31 19:03:50 -070059#define DEF_SAMPLING_DOWN_FACTOR (1)
60#define MAX_SAMPLING_DOWN_FACTOR (10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define TRANSITION_LATENCY_LIMIT (10 * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63static void do_dbs_timer(void *data);
64
65struct cpu_dbs_info_s {
Dave Jones32ee8c32006-02-28 00:43:23 -050066 struct cpufreq_policy *cur_policy;
67 unsigned int prev_cpu_idle_up;
68 unsigned int prev_cpu_idle_down;
69 unsigned int enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
72
73static unsigned int dbs_enable; /* number of CPUs using this policy */
74
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -070075/*
76 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
77 * lock and dbs_mutex. cpu_hotplug lock should always be held before
78 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
79 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
80 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
81 * is recursive for the same process. -Venki
82 */
Dave Jones32ee8c32006-02-28 00:43:23 -050083static DEFINE_MUTEX (dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
85
Andi Kleen6810b542006-05-08 15:17:31 +020086static struct workqueue_struct *dbs_workq;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -050089 unsigned int sampling_rate;
90 unsigned int sampling_down_factor;
91 unsigned int up_threshold;
92 unsigned int ignore_nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95static struct dbs_tuners dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -050096 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
97 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
Eric Piel9cbad612006-03-10 11:35:27 +020098 .ignore_nice = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700101static inline unsigned int get_cpu_idle_time(unsigned int cpu)
102{
103 return kstat_cpu(cpu).cpustat.idle +
104 kstat_cpu(cpu).cpustat.iowait +
Alexander Clouter001893c2005-12-01 01:09:25 -0800105 ( dbs_tuners_ins.ignore_nice ?
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700106 kstat_cpu(cpu).cpustat.nice :
107 0);
108}
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);
136show_one(sampling_down_factor, sampling_down_factor);
137show_one(up_threshold, up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800138show_one(ignore_nice_load, ignore_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Dave Jones32ee8c32006-02-28 00:43:23 -0500140static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 const char *buf, size_t count)
142{
143 unsigned int input;
144 int ret;
145 ret = sscanf (buf, "%u", &input);
146 if (ret != 1 )
147 return -EINVAL;
148
Dave Jonese1318322005-05-31 19:03:50 -0700149 if (input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
150 return -EINVAL;
151
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800152 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 dbs_tuners_ins.sampling_down_factor = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800154 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return count;
157}
158
Dave Jones32ee8c32006-02-28 00:43:23 -0500159static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 const char *buf, size_t count)
161{
162 unsigned int input;
163 int ret;
164 ret = sscanf (buf, "%u", &input);
165
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800166 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
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.sampling_rate = 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
Dave Jones32ee8c32006-02-28 00:43:23 -0500178static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 const char *buf, size_t count)
180{
181 unsigned int input;
182 int ret;
183 ret = sscanf (buf, "%u", &input);
184
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800185 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500186 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700187 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800188 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EINVAL;
190 }
191
192 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800193 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 return count;
196}
197
Alexander Clouter001893c2005-12-01 01:09:25 -0800198static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700199 const char *buf, size_t count)
200{
201 unsigned int input;
202 int ret;
203
204 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500205
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700206 ret = sscanf (buf, "%u", &input);
207 if ( ret != 1 )
208 return -EINVAL;
209
210 if ( input > 1 )
211 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500212
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800213 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700214 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800215 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700216 return count;
217 }
218 dbs_tuners_ins.ignore_nice = input;
219
220 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700221 for_each_online_cpu(j) {
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700222 struct cpu_dbs_info_s *j_dbs_info;
223 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700224 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700225 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
226 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800227 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700228
229 return count;
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#define define_one_rw(_name) \
233static struct freq_attr _name = \
234__ATTR(_name, 0644, show_##_name, store_##_name)
235
236define_one_rw(sampling_rate);
237define_one_rw(sampling_down_factor);
238define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800239define_one_rw(ignore_nice_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static struct attribute * dbs_attributes[] = {
242 &sampling_rate_max.attr,
243 &sampling_rate_min.attr,
244 &sampling_rate.attr,
245 &sampling_down_factor.attr,
246 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800247 &ignore_nice_load.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 NULL
249};
250
251static struct attribute_group dbs_attr_group = {
252 .attrs = dbs_attributes,
253 .name = "ondemand",
254};
255
256/************************** sysfs end ************************/
257
258static void dbs_check_cpu(int cpu)
259{
Dave Jonesc29f1402005-05-31 19:03:50 -0700260 unsigned int idle_ticks, up_idle_ticks, total_ticks;
261 unsigned int freq_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned int freq_down_sampling_rate;
263 static int down_skip[NR_CPUS];
264 struct cpu_dbs_info_s *this_dbs_info;
265
266 struct cpufreq_policy *policy;
267 unsigned int j;
268
269 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
270 if (!this_dbs_info->enable)
271 return;
272
273 policy = this_dbs_info->cur_policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500274 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700275 * Every sampling_rate, we check, if current idle time is less
276 * than 20% (default), then we try to increase frequency
277 * Every sampling_rate*sampling_down_factor, we look for a the lowest
278 * frequency which can sustain the load while keeping idle time over
279 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500281 * Any frequency increase takes it to the maximum frequency.
282 * Frequency reduction happens at minimum steps of
283 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285
286 /* Check for frequency increase */
Dave Jones9c7d2692005-05-31 19:03:49 -0700287 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 for_each_cpu_mask(j, policy->cpus) {
Dave Jones9c7d2692005-05-31 19:03:49 -0700289 unsigned int tmp_idle_ticks, total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct cpu_dbs_info_s *j_dbs_info;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700293 total_idle_ticks = get_cpu_idle_time(j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 tmp_idle_ticks = total_idle_ticks -
295 j_dbs_info->prev_cpu_idle_up;
296 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
297
298 if (tmp_idle_ticks < idle_ticks)
299 idle_ticks = tmp_idle_ticks;
300 }
301
302 /* Scale idle ticks by 100 and compare with up and down ticks */
303 idle_ticks *= 100;
304 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
Dave Jones6fe71162005-05-31 19:03:44 -0700305 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (idle_ticks < up_idle_ticks) {
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700308 down_skip[cpu] = 0;
Dave Jones790d76f2005-05-31 19:03:49 -0700309 for_each_cpu_mask(j, policy->cpus) {
310 struct cpu_dbs_info_s *j_dbs_info;
311
312 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jones32ee8c32006-02-28 00:43:23 -0500313 j_dbs_info->prev_cpu_idle_down =
Dave Jones790d76f2005-05-31 19:03:49 -0700314 j_dbs_info->prev_cpu_idle_up;
315 }
Dave Jonesc11420a2005-05-31 19:03:48 -0700316 /* if we are already at full speed then break out early */
317 if (policy->cur == policy->max)
318 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500319
320 __cpufreq_driver_target(policy, policy->max,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return;
323 }
324
325 /* Check for frequency decrease */
326 down_skip[cpu]++;
327 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
328 return;
329
Dave Jones9c7d2692005-05-31 19:03:49 -0700330 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 for_each_cpu_mask(j, policy->cpus) {
Dave Jones9c7d2692005-05-31 19:03:49 -0700332 unsigned int tmp_idle_ticks, total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct cpu_dbs_info_s *j_dbs_info;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700336 /* Check for frequency decrease */
337 total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 tmp_idle_ticks = total_idle_ticks -
339 j_dbs_info->prev_cpu_idle_down;
340 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
341
342 if (tmp_idle_ticks < idle_ticks)
343 idle_ticks = tmp_idle_ticks;
344 }
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 down_skip[cpu] = 0;
Dave Jonesc29f1402005-05-31 19:03:50 -0700347 /* if we cannot reduce the frequency anymore, break out early */
348 if (policy->cur == policy->min)
349 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dave Jonesc29f1402005-05-31 19:03:50 -0700351 /* Compute how many ticks there are between two measurements */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
353 dbs_tuners_ins.sampling_down_factor;
Dave Jonesc29f1402005-05-31 19:03:50 -0700354 total_ticks = usecs_to_jiffies(freq_down_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Dave Jonesc29f1402005-05-31 19:03:50 -0700356 /*
357 * The optimal frequency is the frequency that is the lowest that
358 * can support the current CPU usage without triggering the up
359 * policy. To be safe, we focus 10 points under the threshold.
360 */
361 freq_next = ((total_ticks - idle_ticks) * 100) / total_ticks;
Dave Jones32ee8c32006-02-28 00:43:23 -0500362 freq_next = (freq_next * policy->cur) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700363 (dbs_tuners_ins.up_threshold - 10);
Dave Jones1206aaa2005-05-31 19:03:48 -0700364
Dominik Brodowski7c9d8c02006-03-26 11:11:03 +0200365 if (freq_next < policy->min)
366 freq_next = policy->min;
367
Dave Jonesc29f1402005-05-31 19:03:50 -0700368 if (freq_next <= ((policy->cur * 95) / 100))
369 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static void do_dbs_timer(void *data)
Dave Jones32ee8c32006-02-28 00:43:23 -0500373{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int i;
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700375 lock_cpu_hotplug();
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800376 mutex_lock(&dbs_mutex);
Dave Jones6fe71162005-05-31 19:03:44 -0700377 for_each_online_cpu(i)
378 dbs_check_cpu(i);
Andi Kleen6810b542006-05-08 15:17:31 +0200379 queue_delayed_work(dbs_workq, &dbs_work,
380 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800381 mutex_unlock(&dbs_mutex);
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700382 unlock_cpu_hotplug();
Dave Jones32ee8c32006-02-28 00:43:23 -0500383}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385static inline void dbs_timer_init(void)
386{
387 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
Andi Kleen6810b542006-05-08 15:17:31 +0200388 if (!dbs_workq)
389 dbs_workq = create_singlethread_workqueue("ondemand");
390 if (!dbs_workq) {
391 printk(KERN_ERR "ondemand: Cannot initialize kernel thread\n");
392 return;
393 }
394 queue_delayed_work(dbs_workq, &dbs_work,
395 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return;
397}
398
399static inline void dbs_timer_exit(void)
400{
Andi Kleen6810b542006-05-08 15:17:31 +0200401 if (dbs_workq)
402 cancel_rearming_delayed_workqueue(dbs_workq, &dbs_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
406 unsigned int event)
407{
408 unsigned int cpu = policy->cpu;
409 struct cpu_dbs_info_s *this_dbs_info;
410 unsigned int j;
411
412 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
413
414 switch (event) {
415 case CPUFREQ_GOV_START:
Dave Jones32ee8c32006-02-28 00:43:23 -0500416 if ((!cpu_online(cpu)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 (!policy->cur))
418 return -EINVAL;
419
420 if (policy->cpuinfo.transition_latency >
Eric Pielff8c2882006-03-10 11:34:16 +0200421 (TRANSITION_LATENCY_LIMIT * 1000)) {
422 printk(KERN_WARNING "ondemand governor failed to load "
423 "due to too long transition latency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -EINVAL;
Eric Pielff8c2882006-03-10 11:34:16 +0200425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (this_dbs_info->enable) /* Already enabled */
427 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500428
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800429 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 for_each_cpu_mask(j, policy->cpus) {
431 struct cpu_dbs_info_s *j_dbs_info;
432 j_dbs_info = &per_cpu(cpu_dbs_info, j);
433 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500434
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700435 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700436 j_dbs_info->prev_cpu_idle_down
437 = j_dbs_info->prev_cpu_idle_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 this_dbs_info->enable = 1;
440 sysfs_create_group(&policy->kobj, &dbs_attr_group);
441 dbs_enable++;
442 /*
443 * Start the timerschedule work, when this governor
444 * is used for first time
445 */
446 if (dbs_enable == 1) {
447 unsigned int latency;
448 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700449 latency = policy->cpuinfo.transition_latency / 1000;
450 if (latency == 0)
451 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700453 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700455
456 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
457 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 dbs_timer_init();
461 }
Dave Jones32ee8c32006-02-28 00:43:23 -0500462
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800463 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465
466 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800467 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 this_dbs_info->enable = 0;
469 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
470 dbs_enable--;
471 /*
472 * Stop the timerschedule work, when this governor
473 * is used for first time
474 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500475 if (dbs_enable == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 dbs_timer_exit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500477
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800478 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 break;
481
482 case CPUFREQ_GOV_LIMITS:
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700483 lock_cpu_hotplug();
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800484 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (policy->max < this_dbs_info->cur_policy->cur)
486 __cpufreq_driver_target(
487 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500488 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 else if (policy->min > this_dbs_info->cur_policy->cur)
490 __cpufreq_driver_target(
491 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500492 policy->min, CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800493 mutex_unlock(&dbs_mutex);
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700494 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496 }
497 return 0;
498}
499
Dave Jones7f335d42005-05-31 19:03:46 -0700500static struct cpufreq_governor cpufreq_gov_dbs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 .name = "ondemand",
502 .governor = cpufreq_governor_dbs,
503 .owner = THIS_MODULE,
504};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506static int __init cpufreq_gov_dbs_init(void)
507{
508 return cpufreq_register_governor(&cpufreq_gov_dbs);
509}
510
511static void __exit cpufreq_gov_dbs_exit(void)
512{
Andi Kleen6810b542006-05-08 15:17:31 +0200513 /* Make sure that the scheduled work is indeed not running.
514 Assumes the timer has been cancelled first. */
515 if (dbs_workq) {
516 flush_workqueue(dbs_workq);
517 destroy_workqueue(dbs_workq);
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 cpufreq_unregister_governor(&cpufreq_gov_dbs);
521}
522
523
524MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
525MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
526 "Low Latency Frequency Transition capable processors");
527MODULE_LICENSE ("GPL");
528
529module_init(cpufreq_gov_dbs_init);
530module_exit(cpufreq_gov_dbs_exit);