blob: 18b016ea5f4877cb10cc663ef8752b430079bc8c [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;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -070067 struct work_struct work;
Dave Jones32ee8c32006-02-28 00:43:23 -050068 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
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -070085static struct workqueue_struct *kondemand_wq;
Andi Kleen6810b542006-05-08 15:17:31 +020086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -050088 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -050089 unsigned int up_threshold;
90 unsigned int ignore_nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
93static struct dbs_tuners dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -050094 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
Eric Piel9cbad612006-03-10 11:35:27 +020095 .ignore_nice = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070098static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
Dave Jonesdac1c1a2005-05-31 19:03:49 -070099{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700100 cputime64_t retval;
101
102 retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
103 kstat_cpu(cpu).cpustat.iowait);
104
105 if (dbs_tuners_ins.ignore_nice)
106 retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
107
108 return retval;
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/************************** sysfs interface ************************/
112static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
113{
114 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
115}
116
117static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
118{
119 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
120}
121
Dave Jones32ee8c32006-02-28 00:43:23 -0500122#define define_one_ro(_name) \
123static struct freq_attr _name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124__ATTR(_name, 0444, show_##_name, NULL)
125
126define_one_ro(sampling_rate_max);
127define_one_ro(sampling_rate_min);
128
129/* cpufreq_ondemand Governor Tunables */
130#define show_one(file_name, object) \
131static ssize_t show_##file_name \
132(struct cpufreq_policy *unused, char *buf) \
133{ \
134 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
135}
136show_one(sampling_rate, sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137show_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_rate(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
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800147 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800149 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return -EINVAL;
151 }
152
153 dbs_tuners_ins.sampling_rate = 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_up_threshold(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);
Dave Jones32ee8c32006-02-28 00:43:23 -0500167 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700168 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800169 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EINVAL;
171 }
172
173 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800174 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return count;
177}
178
Alexander Clouter001893c2005-12-01 01:09:25 -0800179static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700180 const char *buf, size_t count)
181{
182 unsigned int input;
183 int ret;
184
185 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500186
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700187 ret = sscanf (buf, "%u", &input);
188 if ( ret != 1 )
189 return -EINVAL;
190
191 if ( input > 1 )
192 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500193
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800194 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700195 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800196 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700197 return count;
198 }
199 dbs_tuners_ins.ignore_nice = input;
200
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700201 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700202 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700203 struct cpu_dbs_info_s *dbs_info;
204 dbs_info = &per_cpu(cpu_dbs_info, j);
205 dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
206 dbs_info->prev_cpu_wall = get_jiffies_64();
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700207 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800208 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700209
210 return count;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define define_one_rw(_name) \
214static struct freq_attr _name = \
215__ATTR(_name, 0644, show_##_name, store_##_name)
216
217define_one_rw(sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800219define_one_rw(ignore_nice_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221static struct attribute * dbs_attributes[] = {
222 &sampling_rate_max.attr,
223 &sampling_rate_min.attr,
224 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800226 &ignore_nice_load.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 NULL
228};
229
230static struct attribute_group dbs_attr_group = {
231 .attrs = dbs_attributes,
232 .name = "ondemand",
233};
234
235/************************** sysfs end ************************/
236
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700237static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700239 unsigned int idle_ticks, total_ticks;
240 unsigned int load;
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!this_dbs_info->enable)
247 return;
248
249 policy = this_dbs_info->cur_policy;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700250 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
251 total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
252 this_dbs_info->prev_cpu_wall);
253 this_dbs_info->prev_cpu_wall = cur_jiffies;
Dave Jones32ee8c32006-02-28 00:43:23 -0500254 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700255 * Every sampling_rate, we check, if current idle time is less
256 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700257 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700258 * frequency which can sustain the load while keeping idle time over
259 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500261 * Any frequency increase takes it to the maximum frequency.
262 * Frequency reduction happens at minimum steps of
263 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
265
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700266 /* Get Idle Time */
Dave Jones9c7d2692005-05-31 19:03:49 -0700267 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 for_each_cpu_mask(j, policy->cpus) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700269 cputime64_t total_idle_ticks;
270 unsigned int tmp_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct cpu_dbs_info_s *j_dbs_info;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700274 total_idle_ticks = get_cpu_idle_time(j);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700275 tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
276 j_dbs_info->prev_cpu_idle);
277 j_dbs_info->prev_cpu_idle = total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (tmp_idle_ticks < idle_ticks)
280 idle_ticks = tmp_idle_ticks;
281 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700282 load = (100 * (total_ticks - idle_ticks)) / total_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700284 /* Check for frequency increase */
285 if (load > dbs_tuners_ins.up_threshold) {
Dave Jonesc11420a2005-05-31 19:03:48 -0700286 /* if we are already at full speed then break out early */
287 if (policy->cur == policy->max)
288 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500289
290 __cpufreq_driver_target(policy, policy->max,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return;
293 }
294
295 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700296 /* if we cannot reduce the frequency anymore, break out early */
297 if (policy->cur == policy->min)
298 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Dave Jonesc29f1402005-05-31 19:03:50 -0700300 /*
301 * The optimal frequency is the frequency that is the lowest that
302 * can support the current CPU usage without triggering the up
303 * policy. To be safe, we focus 10 points under the threshold.
304 */
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700305 if (load < (dbs_tuners_ins.up_threshold - 10)) {
306 unsigned int freq_next;
307 freq_next = (policy->cur * load) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700308 (dbs_tuners_ins.up_threshold - 10);
Dave Jones1206aaa2005-05-31 19:03:48 -0700309
Dave Jonesc29f1402005-05-31 19:03:50 -0700310 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314static void do_dbs_timer(void *data)
Dave Jones32ee8c32006-02-28 00:43:23 -0500315{
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700316 unsigned int cpu = smp_processor_id();
317 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
318
319 dbs_check_cpu(dbs_info);
320 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work,
321 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Dave Jones32ee8c32006-02-28 00:43:23 -0500322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700324static inline void dbs_timer_init(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700326 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
327
328 INIT_WORK(&dbs_info->work, do_dbs_timer, 0);
329 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work,
330 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return;
332}
333
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700334static inline void dbs_timer_exit(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700336 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
337
338 cancel_rearming_delayed_workqueue(kondemand_wq, &dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
342 unsigned int event)
343{
344 unsigned int cpu = policy->cpu;
345 struct cpu_dbs_info_s *this_dbs_info;
346 unsigned int j;
347
348 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
349
350 switch (event) {
351 case CPUFREQ_GOV_START:
Dave Jones32ee8c32006-02-28 00:43:23 -0500352 if ((!cpu_online(cpu)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 (!policy->cur))
354 return -EINVAL;
355
356 if (policy->cpuinfo.transition_latency >
Eric Pielff8c2882006-03-10 11:34:16 +0200357 (TRANSITION_LATENCY_LIMIT * 1000)) {
358 printk(KERN_WARNING "ondemand governor failed to load "
359 "due to too long transition latency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -EINVAL;
Eric Pielff8c2882006-03-10 11:34:16 +0200361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (this_dbs_info->enable) /* Already enabled */
363 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500364
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800365 mutex_lock(&dbs_mutex);
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700366 dbs_enable++;
367 if (dbs_enable == 1) {
368 kondemand_wq = create_workqueue("kondemand");
369 if (!kondemand_wq) {
370 printk(KERN_ERR "Creation of kondemand failed\n");
371 dbs_enable--;
372 mutex_unlock(&dbs_mutex);
373 return -ENOSPC;
374 }
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 for_each_cpu_mask(j, policy->cpus) {
377 struct cpu_dbs_info_s *j_dbs_info;
378 j_dbs_info = &per_cpu(cpu_dbs_info, j);
379 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500380
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700381 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
382 j_dbs_info->prev_cpu_wall = get_jiffies_64();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 this_dbs_info->enable = 1;
385 sysfs_create_group(&policy->kobj, &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
387 * Start the timerschedule work, when this governor
388 * is used for first time
389 */
390 if (dbs_enable == 1) {
391 unsigned int latency;
392 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700393 latency = policy->cpuinfo.transition_latency / 1000;
394 if (latency == 0)
395 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700397 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700399
400 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
401 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700405 dbs_timer_init(policy->cpu);
Dave Jones32ee8c32006-02-28 00:43:23 -0500406
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800407 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
409
410 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800411 mutex_lock(&dbs_mutex);
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700412 dbs_timer_exit(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 this_dbs_info->enable = 0;
414 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
415 dbs_enable--;
Dave Jones32ee8c32006-02-28 00:43:23 -0500416 if (dbs_enable == 0)
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700417 destroy_workqueue(kondemand_wq);
Dave Jones32ee8c32006-02-28 00:43:23 -0500418
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800419 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 break;
422
423 case CPUFREQ_GOV_LIMITS:
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700424 lock_cpu_hotplug();
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800425 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (policy->max < this_dbs_info->cur_policy->cur)
427 __cpufreq_driver_target(
428 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500429 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 else if (policy->min > this_dbs_info->cur_policy->cur)
431 __cpufreq_driver_target(
432 this_dbs_info->cur_policy,
Dave Jones32ee8c32006-02-28 00:43:23 -0500433 policy->min, CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800434 mutex_unlock(&dbs_mutex);
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700435 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 break;
437 }
438 return 0;
439}
440
Dave Jones7f335d42005-05-31 19:03:46 -0700441static struct cpufreq_governor cpufreq_gov_dbs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 .name = "ondemand",
443 .governor = cpufreq_governor_dbs,
444 .owner = THIS_MODULE,
445};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447static int __init cpufreq_gov_dbs_init(void)
448{
449 return cpufreq_register_governor(&cpufreq_gov_dbs);
450}
451
452static void __exit cpufreq_gov_dbs_exit(void)
453{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 cpufreq_unregister_governor(&cpufreq_gov_dbs);
455}
456
457
458MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
459MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
460 "Low Latency Frequency Transition capable processors");
461MODULE_LICENSE ("GPL");
462
463module_init(cpufreq_gov_dbs_init);
464module_exit(cpufreq_gov_dbs_exit);