blob: a480834c962781b20068922eca6a098561f9d4b3 [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 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053044#define MIN_STAT_SAMPLING_RATE \
45 (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
46#define MIN_SAMPLING_RATE \
47 (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
49#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define TRANSITION_LATENCY_LIMIT (10 * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David Howellsc4028952006-11-22 14:57:56 +000052static void do_dbs_timer(struct work_struct *work);
53
54/* Sampling types */
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080055enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57struct cpu_dbs_info_s {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070058 cputime64_t prev_cpu_idle;
59 cputime64_t prev_cpu_wall;
Dave Jones32ee8c32006-02-28 00:43:23 -050060 struct cpufreq_policy *cur_policy;
David Howellsc4028952006-11-22 14:57:56 +000061 struct delayed_work work;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040062 struct cpufreq_frequency_table *freq_table;
63 unsigned int freq_lo;
64 unsigned int freq_lo_jiffies;
65 unsigned int freq_hi_jiffies;
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080066 int cpu;
67 unsigned int enable:1,
68 sample_type:1;
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 */
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -070082static DEFINE_MUTEX(dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -070084static struct workqueue_struct *kondemand_wq;
Andi Kleen6810b542006-05-08 15:17:31 +020085
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040086static struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -050087 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -050088 unsigned int up_threshold;
89 unsigned int ignore_nice;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040090 unsigned int powersave_bias;
91} dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -050092 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
Eric Piel9cbad612006-03-10 11:35:27 +020093 .ignore_nice = 0,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040094 .powersave_bias = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070097static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
Dave Jonesdac1c1a2005-05-31 19:03:49 -070098{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070099 cputime64_t retval;
100
101 retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
102 kstat_cpu(cpu).cpustat.iowait);
103
104 if (dbs_tuners_ins.ignore_nice)
105 retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
106
107 return retval;
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700108}
109
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400110/*
111 * Find right freq to be set now with powersave_bias on.
112 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
113 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
114 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +0200115static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
116 unsigned int freq_next,
117 unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400118{
119 unsigned int freq_req, freq_reduc, freq_avg;
120 unsigned int freq_hi, freq_lo;
121 unsigned int index = 0;
122 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
123 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu);
124
125 if (!dbs_info->freq_table) {
126 dbs_info->freq_lo = 0;
127 dbs_info->freq_lo_jiffies = 0;
128 return freq_next;
129 }
130
131 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
132 relation, &index);
133 freq_req = dbs_info->freq_table[index].frequency;
134 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
135 freq_avg = freq_req - freq_reduc;
136
137 /* Find freq bounds for freq_avg in freq_table */
138 index = 0;
139 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
140 CPUFREQ_RELATION_H, &index);
141 freq_lo = dbs_info->freq_table[index].frequency;
142 index = 0;
143 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
144 CPUFREQ_RELATION_L, &index);
145 freq_hi = dbs_info->freq_table[index].frequency;
146
147 /* Find out how long we have to be in hi and lo freqs */
148 if (freq_hi == freq_lo) {
149 dbs_info->freq_lo = 0;
150 dbs_info->freq_lo_jiffies = 0;
151 return freq_lo;
152 }
153 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
154 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
155 jiffies_hi += ((freq_hi - freq_lo) / 2);
156 jiffies_hi /= (freq_hi - freq_lo);
157 jiffies_lo = jiffies_total - jiffies_hi;
158 dbs_info->freq_lo = freq_lo;
159 dbs_info->freq_lo_jiffies = jiffies_lo;
160 dbs_info->freq_hi_jiffies = jiffies_hi;
161 return freq_hi;
162}
163
164static void ondemand_powersave_bias_init(void)
165{
166 int i;
167 for_each_online_cpu(i) {
168 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i);
169 dbs_info->freq_table = cpufreq_frequency_get_table(i);
170 dbs_info->freq_lo = 0;
171 }
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/************************** sysfs interface ************************/
175static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
176{
177 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
178}
179
180static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
181{
182 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
183}
184
Dave Jones32ee8c32006-02-28 00:43:23 -0500185#define define_one_ro(_name) \
186static struct freq_attr _name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187__ATTR(_name, 0444, show_##_name, NULL)
188
189define_one_ro(sampling_rate_max);
190define_one_ro(sampling_rate_min);
191
192/* cpufreq_ondemand Governor Tunables */
193#define show_one(file_name, object) \
194static ssize_t show_##file_name \
195(struct cpufreq_policy *unused, char *buf) \
196{ \
197 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
198}
199show_one(sampling_rate, sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200show_one(up_threshold, up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800201show_one(ignore_nice_load, ignore_nice);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400202show_one(powersave_bias, powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Dave Jones32ee8c32006-02-28 00:43:23 -0500204static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 const char *buf, size_t count)
206{
207 unsigned int input;
208 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700209 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800211 mutex_lock(&dbs_mutex);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530212 if (ret != 1 || input > MAX_SAMPLING_RATE
213 || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800214 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -EINVAL;
216 }
217
218 dbs_tuners_ins.sampling_rate = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800219 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 return count;
222}
223
Dave Jones32ee8c32006-02-28 00:43:23 -0500224static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 const char *buf, size_t count)
226{
227 unsigned int input;
228 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700229 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800231 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500232 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700233 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800234 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EINVAL;
236 }
237
238 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800239 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 return count;
242}
243
Alexander Clouter001893c2005-12-01 01:09:25 -0800244static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700245 const char *buf, size_t count)
246{
247 unsigned int input;
248 int ret;
249
250 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500251
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700252 ret = sscanf(buf, "%u", &input);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700253 if ( ret != 1 )
254 return -EINVAL;
255
256 if ( input > 1 )
257 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500258
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800259 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700260 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800261 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700262 return count;
263 }
264 dbs_tuners_ins.ignore_nice = input;
265
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700266 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700267 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700268 struct cpu_dbs_info_s *dbs_info;
269 dbs_info = &per_cpu(cpu_dbs_info, j);
270 dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
271 dbs_info->prev_cpu_wall = get_jiffies_64();
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700272 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800273 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700274
275 return count;
276}
277
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400278static ssize_t store_powersave_bias(struct cpufreq_policy *unused,
279 const char *buf, size_t count)
280{
281 unsigned int input;
282 int ret;
283 ret = sscanf(buf, "%u", &input);
284
285 if (ret != 1)
286 return -EINVAL;
287
288 if (input > 1000)
289 input = 1000;
290
291 mutex_lock(&dbs_mutex);
292 dbs_tuners_ins.powersave_bias = input;
293 ondemand_powersave_bias_init();
294 mutex_unlock(&dbs_mutex);
295
296 return count;
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#define define_one_rw(_name) \
300static struct freq_attr _name = \
301__ATTR(_name, 0644, show_##_name, store_##_name)
302
303define_one_rw(sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800305define_one_rw(ignore_nice_load);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400306define_one_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308static struct attribute * dbs_attributes[] = {
309 &sampling_rate_max.attr,
310 &sampling_rate_min.attr,
311 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800313 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400314 &powersave_bias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 NULL
316};
317
318static struct attribute_group dbs_attr_group = {
319 .attrs = dbs_attributes,
320 .name = "ondemand",
321};
322
323/************************** sysfs end ************************/
324
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700325static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700327 unsigned int idle_ticks, total_ticks;
328 unsigned int load;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700329 cputime64_t cur_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 struct cpufreq_policy *policy;
332 unsigned int j;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!this_dbs_info->enable)
335 return;
336
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400337 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 policy = this_dbs_info->cur_policy;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700339 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
340 total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
341 this_dbs_info->prev_cpu_wall);
342 this_dbs_info->prev_cpu_wall = cur_jiffies;
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700343 if (!total_ticks)
344 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500345 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700346 * Every sampling_rate, we check, if current idle time is less
347 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700348 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700349 * frequency which can sustain the load while keeping idle time over
350 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500352 * Any frequency increase takes it to the maximum frequency.
353 * Frequency reduction happens at minimum steps of
354 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
356
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700357 /* Get Idle Time */
Dave Jones9c7d2692005-05-31 19:03:49 -0700358 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 for_each_cpu_mask(j, policy->cpus) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700360 cputime64_t total_idle_ticks;
361 unsigned int tmp_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct cpu_dbs_info_s *j_dbs_info;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700365 total_idle_ticks = get_cpu_idle_time(j);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700366 tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
367 j_dbs_info->prev_cpu_idle);
368 j_dbs_info->prev_cpu_idle = total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (tmp_idle_ticks < idle_ticks)
371 idle_ticks = tmp_idle_ticks;
372 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700373 load = (100 * (total_ticks - idle_ticks)) / total_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700375 /* Check for frequency increase */
376 if (load > dbs_tuners_ins.up_threshold) {
Dave Jonesc11420a2005-05-31 19:03:48 -0700377 /* if we are already at full speed then break out early */
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400378 if (!dbs_tuners_ins.powersave_bias) {
379 if (policy->cur == policy->max)
380 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500381
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400382 __cpufreq_driver_target(policy, policy->max,
383 CPUFREQ_RELATION_H);
384 } else {
385 int freq = powersave_bias_target(policy, policy->max,
386 CPUFREQ_RELATION_H);
387 __cpufreq_driver_target(policy, freq,
388 CPUFREQ_RELATION_L);
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return;
391 }
392
393 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700394 /* if we cannot reduce the frequency anymore, break out early */
395 if (policy->cur == policy->min)
396 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Dave Jonesc29f1402005-05-31 19:03:50 -0700398 /*
399 * The optimal frequency is the frequency that is the lowest that
400 * can support the current CPU usage without triggering the up
401 * policy. To be safe, we focus 10 points under the threshold.
402 */
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700403 if (load < (dbs_tuners_ins.up_threshold - 10)) {
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700404 unsigned int freq_next, freq_cur;
405
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800406 freq_cur = __cpufreq_driver_getavg(policy);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700407 if (!freq_cur)
408 freq_cur = policy->cur;
409
410 freq_next = (freq_cur * load) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700411 (dbs_tuners_ins.up_threshold - 10);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700412
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400413 if (!dbs_tuners_ins.powersave_bias) {
414 __cpufreq_driver_target(policy, freq_next,
415 CPUFREQ_RELATION_L);
416 } else {
417 int freq = powersave_bias_target(policy, freq_next,
418 CPUFREQ_RELATION_L);
419 __cpufreq_driver_target(policy, freq,
420 CPUFREQ_RELATION_L);
421 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
David Howellsc4028952006-11-22 14:57:56 +0000425static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500426{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800427 struct cpu_dbs_info_s *dbs_info =
428 container_of(work, struct cpu_dbs_info_s, work.work);
429 unsigned int cpu = dbs_info->cpu;
430 int sample_type = dbs_info->sample_type;
431
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400432 /* We want all CPUs to do sampling nearly on same jiffy */
433 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
David Howellsc4028952006-11-22 14:57:56 +0000434
435 /* Permit rescheduling of this work item */
436 work_release(work);
437
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400438 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700439
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700440 if (!dbs_info->enable)
441 return;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400442 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000443 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400444 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000445 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400446 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400447 if (dbs_info->freq_lo) {
448 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000449 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400450 delay = dbs_info->freq_hi_jiffies;
451 }
452 } else {
453 __cpufreq_driver_target(dbs_info->cur_policy,
454 dbs_info->freq_lo,
455 CPUFREQ_RELATION_H);
456 }
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400457 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
Dave Jones32ee8c32006-02-28 00:43:23 -0500458}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800460static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400462 /* We want all CPUs to do sampling nearly on same jiffy */
463 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
464 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700465
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400466 ondemand_powersave_bias_init();
David Howellsc4028952006-11-22 14:57:56 +0000467 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800468 INIT_DELAYED_WORK_NAR(&dbs_info->work, do_dbs_timer);
469 queue_delayed_work_on(dbs_info->cpu, kondemand_wq, &dbs_info->work,
470 delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700473static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700475 dbs_info->enable = 0;
476 cancel_delayed_work(&dbs_info->work);
477 flush_workqueue(kondemand_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
481 unsigned int event)
482{
483 unsigned int cpu = policy->cpu;
484 struct cpu_dbs_info_s *this_dbs_info;
485 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700486 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
489
490 switch (event) {
491 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700492 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -EINVAL;
494
495 if (policy->cpuinfo.transition_latency >
Eric Pielff8c2882006-03-10 11:34:16 +0200496 (TRANSITION_LATENCY_LIMIT * 1000)) {
497 printk(KERN_WARNING "ondemand governor failed to load "
498 "due to too long transition latency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return -EINVAL;
Eric Pielff8c2882006-03-10 11:34:16 +0200500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (this_dbs_info->enable) /* Already enabled */
502 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500503
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800504 mutex_lock(&dbs_mutex);
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700505 dbs_enable++;
506 if (dbs_enable == 1) {
507 kondemand_wq = create_workqueue("kondemand");
508 if (!kondemand_wq) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530509 printk(KERN_ERR
510 "Creation of kondemand failed\n");
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700511 dbs_enable--;
512 mutex_unlock(&dbs_mutex);
513 return -ENOSPC;
514 }
515 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700516
517 rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
518 if (rc) {
519 if (dbs_enable == 1)
520 destroy_workqueue(kondemand_wq);
521 dbs_enable--;
522 mutex_unlock(&dbs_mutex);
523 return rc;
524 }
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 for_each_cpu_mask(j, policy->cpus) {
527 struct cpu_dbs_info_s *j_dbs_info;
528 j_dbs_info = &per_cpu(cpu_dbs_info, j);
529 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500530
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700531 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
532 j_dbs_info->prev_cpu_wall = get_jiffies_64();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800534 this_dbs_info->cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 this_dbs_info->enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /*
537 * Start the timerschedule work, when this governor
538 * is used for first time
539 */
540 if (dbs_enable == 1) {
541 unsigned int latency;
542 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700543 latency = policy->cpuinfo.transition_latency / 1000;
544 if (latency == 0)
545 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700547 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700549
550 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
551 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800555 dbs_timer_init(this_dbs_info);
Dave Jones32ee8c32006-02-28 00:43:23 -0500556
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800557 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559
560 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800561 mutex_lock(&dbs_mutex);
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700562 dbs_timer_exit(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
564 dbs_enable--;
Dave Jones32ee8c32006-02-28 00:43:23 -0500565 if (dbs_enable == 0)
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700566 destroy_workqueue(kondemand_wq);
Dave Jones32ee8c32006-02-28 00:43:23 -0500567
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800568 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 break;
571
572 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800573 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700575 __cpufreq_driver_target(this_dbs_info->cur_policy,
576 policy->max,
577 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700579 __cpufreq_driver_target(this_dbs_info->cur_policy,
580 policy->min,
581 CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800582 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
584 }
585 return 0;
586}
587
Dave Jones7f335d42005-05-31 19:03:46 -0700588static struct cpufreq_governor cpufreq_gov_dbs = {
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700589 .name = "ondemand",
590 .governor = cpufreq_governor_dbs,
591 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594static int __init cpufreq_gov_dbs_init(void)
595{
596 return cpufreq_register_governor(&cpufreq_gov_dbs);
597}
598
599static void __exit cpufreq_gov_dbs_exit(void)
600{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 cpufreq_unregister_governor(&cpufreq_gov_dbs);
602}
603
604
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700605MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
606MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
607MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
608 "Low Latency Frequency Transition capable processors");
609MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611module_init(cpufreq_gov_dbs_init);
612module_exit(cpufreq_gov_dbs_exit);