blob: f507a869acbc029128ac52c405219d3364679086 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/cpufreq.h>
Andrew Morton138a01282006-06-23 03:31:19 -070017#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/jiffies.h>
19#include <linux/kernel_stat.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080020#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
23 * dbs is used in this file as a shortform for demandbased switching
24 * It helps to keep variable names smaller, simpler
25 */
26
27#define DEF_FREQUENCY_UP_THRESHOLD (80)
Dave Jonesc29f1402005-05-31 19:03:50 -070028#define MIN_FREQUENCY_UP_THRESHOLD (11)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define MAX_FREQUENCY_UP_THRESHOLD (100)
30
Dave Jones32ee8c32006-02-28 00:43:23 -050031/*
32 * The polling frequency of this governor depends on the capability of
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * the processor. Default polling frequency is 1000 times the transition
Dave Jones32ee8c32006-02-28 00:43:23 -050034 * latency of the processor. The governor will work on any processor with
35 * transition latency <= 10mS, using appropriate sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * rate.
37 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
38 * this governor will not work.
39 * All times here are in uS.
40 */
Dave Jones32ee8c32006-02-28 00:43:23 -050041static unsigned int def_sampling_rate;
Dave Jonesdf8b59b2005-09-20 12:39:35 -070042#define MIN_SAMPLING_RATE_RATIO (2)
43/* for correct statistics, we need at least 10 ticks between each measure */
44#define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
45#define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
47#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define TRANSITION_LATENCY_LIMIT (10 * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static void do_dbs_timer(void *data);
51
52struct cpu_dbs_info_s {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070053 cputime64_t prev_cpu_idle;
54 cputime64_t prev_cpu_wall;
Dave Jones32ee8c32006-02-28 00:43:23 -050055 struct cpufreq_policy *cur_policy;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -070056 struct work_struct work;
Dave Jones32ee8c32006-02-28 00:43:23 -050057 unsigned int enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
60
61static unsigned int dbs_enable; /* number of CPUs using this policy */
62
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -070063/*
64 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
65 * lock and dbs_mutex. cpu_hotplug lock should always be held before
66 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
67 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
68 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
69 * is recursive for the same process. -Venki
70 */
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -070071static DEFINE_MUTEX(dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -070073static struct workqueue_struct *kondemand_wq;
Andi Kleen6810b542006-05-08 15:17:31 +020074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -050076 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -050077 unsigned int up_threshold;
78 unsigned int ignore_nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81static struct dbs_tuners dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -050082 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
Eric Piel9cbad612006-03-10 11:35:27 +020083 .ignore_nice = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070086static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
Dave Jonesdac1c1a2005-05-31 19:03:49 -070087{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070088 cputime64_t retval;
89
90 retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
91 kstat_cpu(cpu).cpustat.iowait);
92
93 if (dbs_tuners_ins.ignore_nice)
94 retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
95
96 return retval;
Dave Jonesdac1c1a2005-05-31 19:03:49 -070097}
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125show_one(up_threshold, up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800126show_one(ignore_nice_load, ignore_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Dave Jones32ee8c32006-02-28 00:43:23 -0500128static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 const char *buf, size_t count)
130{
131 unsigned int input;
132 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700133 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800135 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800137 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -EINVAL;
139 }
140
141 dbs_tuners_ins.sampling_rate = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800142 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return count;
145}
146
Dave Jones32ee8c32006-02-28 00:43:23 -0500147static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *buf, size_t count)
149{
150 unsigned int input;
151 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700152 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800154 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500155 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700156 input < MIN_FREQUENCY_UP_THRESHOLD) {
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.up_threshold = 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
Alexander Clouter001893c2005-12-01 01:09:25 -0800167static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700168 const char *buf, size_t count)
169{
170 unsigned int input;
171 int ret;
172
173 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500174
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700175 ret = sscanf(buf, "%u", &input);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700176 if ( ret != 1 )
177 return -EINVAL;
178
179 if ( input > 1 )
180 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500181
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800182 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700183 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800184 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700185 return count;
186 }
187 dbs_tuners_ins.ignore_nice = input;
188
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700189 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700190 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700191 struct cpu_dbs_info_s *dbs_info;
192 dbs_info = &per_cpu(cpu_dbs_info, j);
193 dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
194 dbs_info->prev_cpu_wall = get_jiffies_64();
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700195 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800196 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700197
198 return count;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#define define_one_rw(_name) \
202static struct freq_attr _name = \
203__ATTR(_name, 0644, show_##_name, store_##_name)
204
205define_one_rw(sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800207define_one_rw(ignore_nice_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209static struct attribute * dbs_attributes[] = {
210 &sampling_rate_max.attr,
211 &sampling_rate_min.attr,
212 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800214 &ignore_nice_load.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 NULL
216};
217
218static struct attribute_group dbs_attr_group = {
219 .attrs = dbs_attributes,
220 .name = "ondemand",
221};
222
223/************************** sysfs end ************************/
224
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700225static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700227 unsigned int idle_ticks, total_ticks;
228 unsigned int load;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700229 cputime64_t cur_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 struct cpufreq_policy *policy;
232 unsigned int j;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!this_dbs_info->enable)
235 return;
236
237 policy = this_dbs_info->cur_policy;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700238 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
239 total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
240 this_dbs_info->prev_cpu_wall);
241 this_dbs_info->prev_cpu_wall = cur_jiffies;
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700242 if (!total_ticks)
243 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500244 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700245 * Every sampling_rate, we check, if current idle time is less
246 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700247 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700248 * frequency which can sustain the load while keeping idle time over
249 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500251 * Any frequency increase takes it to the maximum frequency.
252 * Frequency reduction happens at minimum steps of
253 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
255
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700256 /* Get Idle Time */
Dave Jones9c7d2692005-05-31 19:03:49 -0700257 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 for_each_cpu_mask(j, policy->cpus) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700259 cputime64_t total_idle_ticks;
260 unsigned int tmp_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct cpu_dbs_info_s *j_dbs_info;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700264 total_idle_ticks = get_cpu_idle_time(j);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700265 tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
266 j_dbs_info->prev_cpu_idle);
267 j_dbs_info->prev_cpu_idle = total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (tmp_idle_ticks < idle_ticks)
270 idle_ticks = tmp_idle_ticks;
271 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700272 load = (100 * (total_ticks - idle_ticks)) / total_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700274 /* Check for frequency increase */
275 if (load > dbs_tuners_ins.up_threshold) {
Dave Jonesc11420a2005-05-31 19:03:48 -0700276 /* if we are already at full speed then break out early */
277 if (policy->cur == policy->max)
278 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500279
280 __cpufreq_driver_target(policy, policy->max,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return;
283 }
284
285 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700286 /* if we cannot reduce the frequency anymore, break out early */
287 if (policy->cur == policy->min)
288 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Dave Jonesc29f1402005-05-31 19:03:50 -0700290 /*
291 * The optimal frequency is the frequency that is the lowest that
292 * can support the current CPU usage without triggering the up
293 * policy. To be safe, we focus 10 points under the threshold.
294 */
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700295 if (load < (dbs_tuners_ins.up_threshold - 10)) {
296 unsigned int freq_next;
297 freq_next = (policy->cur * load) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700298 (dbs_tuners_ins.up_threshold - 10);
Dave Jones1206aaa2005-05-31 19:03:48 -0700299
Dave Jonesc29f1402005-05-31 19:03:50 -0700300 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static void do_dbs_timer(void *data)
Dave Jones32ee8c32006-02-28 00:43:23 -0500305{
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700306 unsigned int cpu = smp_processor_id();
307 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400308 /* We want all CPUs to do sampling nearly on same jiffy */
309 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
310 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700311
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700312 if (!dbs_info->enable)
313 return;
314
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200315 lock_cpu_hotplug();
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700316 dbs_check_cpu(dbs_info);
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200317 unlock_cpu_hotplug();
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400318 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
Dave Jones32ee8c32006-02-28 00:43:23 -0500319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700321static inline void dbs_timer_init(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700323 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400324 /* We want all CPUs to do sampling nearly on same jiffy */
325 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
326 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700327
328 INIT_WORK(&dbs_info->work, do_dbs_timer, 0);
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400329 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700332static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700334 dbs_info->enable = 0;
335 cancel_delayed_work(&dbs_info->work);
336 flush_workqueue(kondemand_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
340 unsigned int event)
341{
342 unsigned int cpu = policy->cpu;
343 struct cpu_dbs_info_s *this_dbs_info;
344 unsigned int j;
345
346 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
347
348 switch (event) {
349 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700350 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EINVAL;
352
353 if (policy->cpuinfo.transition_latency >
Eric Pielff8c2882006-03-10 11:34:16 +0200354 (TRANSITION_LATENCY_LIMIT * 1000)) {
355 printk(KERN_WARNING "ondemand governor failed to load "
356 "due to too long transition latency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EINVAL;
Eric Pielff8c2882006-03-10 11:34:16 +0200358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (this_dbs_info->enable) /* Already enabled */
360 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500361
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800362 mutex_lock(&dbs_mutex);
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700363 dbs_enable++;
364 if (dbs_enable == 1) {
365 kondemand_wq = create_workqueue("kondemand");
366 if (!kondemand_wq) {
367 printk(KERN_ERR "Creation of kondemand failed\n");
368 dbs_enable--;
369 mutex_unlock(&dbs_mutex);
370 return -ENOSPC;
371 }
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 for_each_cpu_mask(j, policy->cpus) {
374 struct cpu_dbs_info_s *j_dbs_info;
375 j_dbs_info = &per_cpu(cpu_dbs_info, j);
376 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500377
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700378 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
379 j_dbs_info->prev_cpu_wall = get_jiffies_64();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 this_dbs_info->enable = 1;
382 sysfs_create_group(&policy->kobj, &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
384 * Start the timerschedule work, when this governor
385 * is used for first time
386 */
387 if (dbs_enable == 1) {
388 unsigned int latency;
389 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700390 latency = policy->cpuinfo.transition_latency / 1000;
391 if (latency == 0)
392 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700394 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700396
397 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
398 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700402 dbs_timer_init(policy->cpu);
Dave Jones32ee8c32006-02-28 00:43:23 -0500403
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800404 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
406
407 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800408 mutex_lock(&dbs_mutex);
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700409 dbs_timer_exit(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
411 dbs_enable--;
Dave Jones32ee8c32006-02-28 00:43:23 -0500412 if (dbs_enable == 0)
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700413 destroy_workqueue(kondemand_wq);
Dave Jones32ee8c32006-02-28 00:43:23 -0500414
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800415 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 break;
418
419 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800420 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700422 __cpufreq_driver_target(this_dbs_info->cur_policy,
423 policy->max,
424 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700426 __cpufreq_driver_target(this_dbs_info->cur_policy,
427 policy->min,
428 CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800429 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
431 }
432 return 0;
433}
434
Dave Jones7f335d42005-05-31 19:03:46 -0700435static struct cpufreq_governor cpufreq_gov_dbs = {
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700436 .name = "ondemand",
437 .governor = cpufreq_governor_dbs,
438 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441static int __init cpufreq_gov_dbs_init(void)
442{
443 return cpufreq_register_governor(&cpufreq_gov_dbs);
444}
445
446static void __exit cpufreq_gov_dbs_exit(void)
447{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 cpufreq_unregister_governor(&cpufreq_gov_dbs);
449}
450
451
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700452MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
453MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
454MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
455 "Low Latency Frequency Transition capable processors");
456MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458module_init(cpufreq_gov_dbs_init);
459module_exit(cpufreq_gov_dbs_exit);