blob: 34874c2f188533bce24e0499db852da860c1dfeb [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;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040058 struct cpufreq_frequency_table *freq_table;
59 unsigned int freq_lo;
60 unsigned int freq_lo_jiffies;
61 unsigned int freq_hi_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
64
65static unsigned int dbs_enable; /* number of CPUs using this policy */
66
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -070067/*
68 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
69 * lock and dbs_mutex. cpu_hotplug lock should always be held before
70 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
71 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
72 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
73 * is recursive for the same process. -Venki
74 */
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -070075static DEFINE_MUTEX(dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -070077static struct workqueue_struct *kondemand_wq;
Andi Kleen6810b542006-05-08 15:17:31 +020078
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040079static struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -050080 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -050081 unsigned int up_threshold;
82 unsigned int ignore_nice;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040083 unsigned int powersave_bias;
84} dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -050085 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
Eric Piel9cbad612006-03-10 11:35:27 +020086 .ignore_nice = 0,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040087 .powersave_bias = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070090static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
Dave Jonesdac1c1a2005-05-31 19:03:49 -070091{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070092 cputime64_t retval;
93
94 retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
95 kstat_cpu(cpu).cpustat.iowait);
96
97 if (dbs_tuners_ins.ignore_nice)
98 retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
99
100 return retval;
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700101}
102
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400103/*
104 * Find right freq to be set now with powersave_bias on.
105 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
106 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
107 */
108unsigned int powersave_bias_target(struct cpufreq_policy *policy,
109 unsigned int freq_next, unsigned int relation)
110{
111 unsigned int freq_req, freq_reduc, freq_avg;
112 unsigned int freq_hi, freq_lo;
113 unsigned int index = 0;
114 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
115 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu);
116
117 if (!dbs_info->freq_table) {
118 dbs_info->freq_lo = 0;
119 dbs_info->freq_lo_jiffies = 0;
120 return freq_next;
121 }
122
123 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
124 relation, &index);
125 freq_req = dbs_info->freq_table[index].frequency;
126 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
127 freq_avg = freq_req - freq_reduc;
128
129 /* Find freq bounds for freq_avg in freq_table */
130 index = 0;
131 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
132 CPUFREQ_RELATION_H, &index);
133 freq_lo = dbs_info->freq_table[index].frequency;
134 index = 0;
135 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
136 CPUFREQ_RELATION_L, &index);
137 freq_hi = dbs_info->freq_table[index].frequency;
138
139 /* Find out how long we have to be in hi and lo freqs */
140 if (freq_hi == freq_lo) {
141 dbs_info->freq_lo = 0;
142 dbs_info->freq_lo_jiffies = 0;
143 return freq_lo;
144 }
145 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
146 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
147 jiffies_hi += ((freq_hi - freq_lo) / 2);
148 jiffies_hi /= (freq_hi - freq_lo);
149 jiffies_lo = jiffies_total - jiffies_hi;
150 dbs_info->freq_lo = freq_lo;
151 dbs_info->freq_lo_jiffies = jiffies_lo;
152 dbs_info->freq_hi_jiffies = jiffies_hi;
153 return freq_hi;
154}
155
156static void ondemand_powersave_bias_init(void)
157{
158 int i;
159 for_each_online_cpu(i) {
160 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i);
161 dbs_info->freq_table = cpufreq_frequency_get_table(i);
162 dbs_info->freq_lo = 0;
163 }
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/************************** sysfs interface ************************/
167static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
168{
169 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
170}
171
172static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
173{
174 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
175}
176
Dave Jones32ee8c32006-02-28 00:43:23 -0500177#define define_one_ro(_name) \
178static struct freq_attr _name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179__ATTR(_name, 0444, show_##_name, NULL)
180
181define_one_ro(sampling_rate_max);
182define_one_ro(sampling_rate_min);
183
184/* cpufreq_ondemand Governor Tunables */
185#define show_one(file_name, object) \
186static ssize_t show_##file_name \
187(struct cpufreq_policy *unused, char *buf) \
188{ \
189 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
190}
191show_one(sampling_rate, sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192show_one(up_threshold, up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800193show_one(ignore_nice_load, ignore_nice);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400194show_one(powersave_bias, powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dave Jones32ee8c32006-02-28 00:43:23 -0500196static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 const char *buf, size_t count)
198{
199 unsigned int input;
200 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700201 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800203 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800205 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return -EINVAL;
207 }
208
209 dbs_tuners_ins.sampling_rate = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800210 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 return count;
213}
214
Dave Jones32ee8c32006-02-28 00:43:23 -0500215static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 const char *buf, size_t count)
217{
218 unsigned int input;
219 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700220 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800222 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500223 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700224 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800225 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -EINVAL;
227 }
228
229 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800230 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 return count;
233}
234
Alexander Clouter001893c2005-12-01 01:09:25 -0800235static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700236 const char *buf, size_t count)
237{
238 unsigned int input;
239 int ret;
240
241 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500242
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700243 ret = sscanf(buf, "%u", &input);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700244 if ( ret != 1 )
245 return -EINVAL;
246
247 if ( input > 1 )
248 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500249
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800250 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700251 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800252 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700253 return count;
254 }
255 dbs_tuners_ins.ignore_nice = input;
256
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700257 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700258 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700259 struct cpu_dbs_info_s *dbs_info;
260 dbs_info = &per_cpu(cpu_dbs_info, j);
261 dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
262 dbs_info->prev_cpu_wall = get_jiffies_64();
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700263 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800264 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700265
266 return count;
267}
268
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400269static ssize_t store_powersave_bias(struct cpufreq_policy *unused,
270 const char *buf, size_t count)
271{
272 unsigned int input;
273 int ret;
274 ret = sscanf(buf, "%u", &input);
275
276 if (ret != 1)
277 return -EINVAL;
278
279 if (input > 1000)
280 input = 1000;
281
282 mutex_lock(&dbs_mutex);
283 dbs_tuners_ins.powersave_bias = input;
284 ondemand_powersave_bias_init();
285 mutex_unlock(&dbs_mutex);
286
287 return count;
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#define define_one_rw(_name) \
291static struct freq_attr _name = \
292__ATTR(_name, 0644, show_##_name, store_##_name)
293
294define_one_rw(sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800296define_one_rw(ignore_nice_load);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400297define_one_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299static struct attribute * dbs_attributes[] = {
300 &sampling_rate_max.attr,
301 &sampling_rate_min.attr,
302 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800304 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400305 &powersave_bias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 NULL
307};
308
309static struct attribute_group dbs_attr_group = {
310 .attrs = dbs_attributes,
311 .name = "ondemand",
312};
313
314/************************** sysfs end ************************/
315
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700316static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700318 unsigned int idle_ticks, total_ticks;
319 unsigned int load;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700320 cputime64_t cur_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 struct cpufreq_policy *policy;
323 unsigned int j;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (!this_dbs_info->enable)
326 return;
327
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400328 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 policy = this_dbs_info->cur_policy;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700330 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
331 total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
332 this_dbs_info->prev_cpu_wall);
333 this_dbs_info->prev_cpu_wall = cur_jiffies;
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700334 if (!total_ticks)
335 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500336 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700337 * Every sampling_rate, we check, if current idle time is less
338 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700339 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700340 * frequency which can sustain the load while keeping idle time over
341 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500343 * Any frequency increase takes it to the maximum frequency.
344 * Frequency reduction happens at minimum steps of
345 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
347
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700348 /* Get Idle Time */
Dave Jones9c7d2692005-05-31 19:03:49 -0700349 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 for_each_cpu_mask(j, policy->cpus) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700351 cputime64_t total_idle_ticks;
352 unsigned int tmp_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct cpu_dbs_info_s *j_dbs_info;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700356 total_idle_ticks = get_cpu_idle_time(j);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700357 tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
358 j_dbs_info->prev_cpu_idle);
359 j_dbs_info->prev_cpu_idle = total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (tmp_idle_ticks < idle_ticks)
362 idle_ticks = tmp_idle_ticks;
363 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700364 load = (100 * (total_ticks - idle_ticks)) / total_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700366 /* Check for frequency increase */
367 if (load > dbs_tuners_ins.up_threshold) {
Dave Jonesc11420a2005-05-31 19:03:48 -0700368 /* if we are already at full speed then break out early */
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400369 if (!dbs_tuners_ins.powersave_bias) {
370 if (policy->cur == policy->max)
371 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500372
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400373 __cpufreq_driver_target(policy, policy->max,
374 CPUFREQ_RELATION_H);
375 } else {
376 int freq = powersave_bias_target(policy, policy->max,
377 CPUFREQ_RELATION_H);
378 __cpufreq_driver_target(policy, freq,
379 CPUFREQ_RELATION_L);
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return;
382 }
383
384 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700385 /* if we cannot reduce the frequency anymore, break out early */
386 if (policy->cur == policy->min)
387 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Dave Jonesc29f1402005-05-31 19:03:50 -0700389 /*
390 * The optimal frequency is the frequency that is the lowest that
391 * can support the current CPU usage without triggering the up
392 * policy. To be safe, we focus 10 points under the threshold.
393 */
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700394 if (load < (dbs_tuners_ins.up_threshold - 10)) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400395 unsigned int freq_next = (policy->cur * load) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700396 (dbs_tuners_ins.up_threshold - 10);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400397 if (!dbs_tuners_ins.powersave_bias) {
398 __cpufreq_driver_target(policy, freq_next,
399 CPUFREQ_RELATION_L);
400 } else {
401 int freq = powersave_bias_target(policy, freq_next,
402 CPUFREQ_RELATION_L);
403 __cpufreq_driver_target(policy, freq,
404 CPUFREQ_RELATION_L);
405 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400409/* Sampling types */
410enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static void do_dbs_timer(void *data)
Dave Jones32ee8c32006-02-28 00:43:23 -0500413{
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700414 unsigned int cpu = smp_processor_id();
415 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400416 /* We want all CPUs to do sampling nearly on same jiffy */
417 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
418 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700419
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700420 if (!dbs_info->enable)
421 return;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400422 /* Common NORMAL_SAMPLE setup */
423 INIT_WORK(&dbs_info->work, do_dbs_timer, (void *)DBS_NORMAL_SAMPLE);
424 if (!dbs_tuners_ins.powersave_bias ||
425 (unsigned long) data == DBS_NORMAL_SAMPLE) {
426 lock_cpu_hotplug();
427 dbs_check_cpu(dbs_info);
428 unlock_cpu_hotplug();
429 if (dbs_info->freq_lo) {
430 /* Setup timer for SUB_SAMPLE */
431 INIT_WORK(&dbs_info->work, do_dbs_timer,
432 (void *)DBS_SUB_SAMPLE);
433 delay = dbs_info->freq_hi_jiffies;
434 }
435 } else {
436 __cpufreq_driver_target(dbs_info->cur_policy,
437 dbs_info->freq_lo,
438 CPUFREQ_RELATION_H);
439 }
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400440 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
Dave Jones32ee8c32006-02-28 00:43:23 -0500441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700443static inline void dbs_timer_init(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700445 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400446 /* We want all CPUs to do sampling nearly on same jiffy */
447 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
448 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700449
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400450 ondemand_powersave_bias_init();
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700451 INIT_WORK(&dbs_info->work, do_dbs_timer, 0);
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400452 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700455static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700457 dbs_info->enable = 0;
458 cancel_delayed_work(&dbs_info->work);
459 flush_workqueue(kondemand_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
463 unsigned int event)
464{
465 unsigned int cpu = policy->cpu;
466 struct cpu_dbs_info_s *this_dbs_info;
467 unsigned int j;
468
469 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
470
471 switch (event) {
472 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700473 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -EINVAL;
475
476 if (policy->cpuinfo.transition_latency >
Eric Pielff8c2882006-03-10 11:34:16 +0200477 (TRANSITION_LATENCY_LIMIT * 1000)) {
478 printk(KERN_WARNING "ondemand governor failed to load "
479 "due to too long transition latency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -EINVAL;
Eric Pielff8c2882006-03-10 11:34:16 +0200481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (this_dbs_info->enable) /* Already enabled */
483 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500484
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800485 mutex_lock(&dbs_mutex);
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700486 dbs_enable++;
487 if (dbs_enable == 1) {
488 kondemand_wq = create_workqueue("kondemand");
489 if (!kondemand_wq) {
490 printk(KERN_ERR "Creation of kondemand failed\n");
491 dbs_enable--;
492 mutex_unlock(&dbs_mutex);
493 return -ENOSPC;
494 }
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 for_each_cpu_mask(j, policy->cpus) {
497 struct cpu_dbs_info_s *j_dbs_info;
498 j_dbs_info = &per_cpu(cpu_dbs_info, j);
499 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500500
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700501 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
502 j_dbs_info->prev_cpu_wall = get_jiffies_64();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 this_dbs_info->enable = 1;
505 sysfs_create_group(&policy->kobj, &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * Start the timerschedule work, when this governor
508 * is used for first time
509 */
510 if (dbs_enable == 1) {
511 unsigned int latency;
512 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700513 latency = policy->cpuinfo.transition_latency / 1000;
514 if (latency == 0)
515 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700517 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700519
520 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
521 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700525 dbs_timer_init(policy->cpu);
Dave Jones32ee8c32006-02-28 00:43:23 -0500526
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800527 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529
530 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800531 mutex_lock(&dbs_mutex);
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700532 dbs_timer_exit(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
534 dbs_enable--;
Dave Jones32ee8c32006-02-28 00:43:23 -0500535 if (dbs_enable == 0)
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700536 destroy_workqueue(kondemand_wq);
Dave Jones32ee8c32006-02-28 00:43:23 -0500537
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800538 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 break;
541
542 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800543 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700545 __cpufreq_driver_target(this_dbs_info->cur_policy,
546 policy->max,
547 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700549 __cpufreq_driver_target(this_dbs_info->cur_policy,
550 policy->min,
551 CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800552 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 }
555 return 0;
556}
557
Dave Jones7f335d42005-05-31 19:03:46 -0700558static struct cpufreq_governor cpufreq_gov_dbs = {
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700559 .name = "ondemand",
560 .governor = cpufreq_governor_dbs,
561 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564static int __init cpufreq_gov_dbs_init(void)
565{
566 return cpufreq_register_governor(&cpufreq_gov_dbs);
567}
568
569static void __exit cpufreq_gov_dbs_exit(void)
570{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 cpufreq_unregister_governor(&cpufreq_gov_dbs);
572}
573
574
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700575MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
576MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
577MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
578 "Low Latency Frequency Transition capable processors");
579MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581module_init(cpufreq_gov_dbs_init);
582module_exit(cpufreq_gov_dbs_exit);