blob: f697449327c6fca54f856189801965861855f6a6 [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 */
55enum dbs_sample {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;
62 enum dbs_sample sample_type;
Dave Jones32ee8c32006-02-28 00:43:23 -050063 unsigned int enable;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040064 struct cpufreq_frequency_table *freq_table;
65 unsigned int freq_lo;
66 unsigned int freq_lo_jiffies;
67 unsigned int freq_hi_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
70
71static unsigned int dbs_enable; /* number of CPUs using this policy */
72
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -070073/*
74 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
75 * lock and dbs_mutex. cpu_hotplug lock should always be held before
76 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
77 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
78 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
79 * is recursive for the same process. -Venki
80 */
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -070081static DEFINE_MUTEX(dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -070083static struct workqueue_struct *kondemand_wq;
Andi Kleen6810b542006-05-08 15:17:31 +020084
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040085static struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -050086 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -050087 unsigned int up_threshold;
88 unsigned int ignore_nice;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040089 unsigned int powersave_bias;
90} dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -050091 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
Eric Piel9cbad612006-03-10 11:35:27 +020092 .ignore_nice = 0,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040093 .powersave_bias = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070096static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
Dave Jonesdac1c1a2005-05-31 19:03:49 -070097{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070098 cputime64_t retval;
99
100 retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
101 kstat_cpu(cpu).cpustat.iowait);
102
103 if (dbs_tuners_ins.ignore_nice)
104 retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
105
106 return retval;
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700107}
108
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400109/*
110 * Find right freq to be set now with powersave_bias on.
111 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
112 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
113 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +0200114static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
115 unsigned int freq_next,
116 unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400117{
118 unsigned int freq_req, freq_reduc, freq_avg;
119 unsigned int freq_hi, freq_lo;
120 unsigned int index = 0;
121 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
122 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu);
123
124 if (!dbs_info->freq_table) {
125 dbs_info->freq_lo = 0;
126 dbs_info->freq_lo_jiffies = 0;
127 return freq_next;
128 }
129
130 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
131 relation, &index);
132 freq_req = dbs_info->freq_table[index].frequency;
133 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
134 freq_avg = freq_req - freq_reduc;
135
136 /* Find freq bounds for freq_avg in freq_table */
137 index = 0;
138 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
139 CPUFREQ_RELATION_H, &index);
140 freq_lo = dbs_info->freq_table[index].frequency;
141 index = 0;
142 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
143 CPUFREQ_RELATION_L, &index);
144 freq_hi = dbs_info->freq_table[index].frequency;
145
146 /* Find out how long we have to be in hi and lo freqs */
147 if (freq_hi == freq_lo) {
148 dbs_info->freq_lo = 0;
149 dbs_info->freq_lo_jiffies = 0;
150 return freq_lo;
151 }
152 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
153 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
154 jiffies_hi += ((freq_hi - freq_lo) / 2);
155 jiffies_hi /= (freq_hi - freq_lo);
156 jiffies_lo = jiffies_total - jiffies_hi;
157 dbs_info->freq_lo = freq_lo;
158 dbs_info->freq_lo_jiffies = jiffies_lo;
159 dbs_info->freq_hi_jiffies = jiffies_hi;
160 return freq_hi;
161}
162
163static void ondemand_powersave_bias_init(void)
164{
165 int i;
166 for_each_online_cpu(i) {
167 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i);
168 dbs_info->freq_table = cpufreq_frequency_get_table(i);
169 dbs_info->freq_lo = 0;
170 }
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/************************** sysfs interface ************************/
174static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
175{
176 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
177}
178
179static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
180{
181 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
182}
183
Dave Jones32ee8c32006-02-28 00:43:23 -0500184#define define_one_ro(_name) \
185static struct freq_attr _name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186__ATTR(_name, 0444, show_##_name, NULL)
187
188define_one_ro(sampling_rate_max);
189define_one_ro(sampling_rate_min);
190
191/* cpufreq_ondemand Governor Tunables */
192#define show_one(file_name, object) \
193static ssize_t show_##file_name \
194(struct cpufreq_policy *unused, char *buf) \
195{ \
196 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
197}
198show_one(sampling_rate, sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199show_one(up_threshold, up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800200show_one(ignore_nice_load, ignore_nice);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400201show_one(powersave_bias, powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Dave Jones32ee8c32006-02-28 00:43:23 -0500203static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 const char *buf, size_t count)
205{
206 unsigned int input;
207 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700208 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800210 mutex_lock(&dbs_mutex);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530211 if (ret != 1 || input > MAX_SAMPLING_RATE
212 || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800213 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -EINVAL;
215 }
216
217 dbs_tuners_ins.sampling_rate = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800218 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 return count;
221}
222
Dave Jones32ee8c32006-02-28 00:43:23 -0500223static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 const char *buf, size_t count)
225{
226 unsigned int input;
227 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700228 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800230 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500231 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700232 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800233 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EINVAL;
235 }
236
237 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800238 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 return count;
241}
242
Alexander Clouter001893c2005-12-01 01:09:25 -0800243static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700244 const char *buf, size_t count)
245{
246 unsigned int input;
247 int ret;
248
249 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500250
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700251 ret = sscanf(buf, "%u", &input);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700252 if ( ret != 1 )
253 return -EINVAL;
254
255 if ( input > 1 )
256 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500257
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800258 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700259 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800260 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700261 return count;
262 }
263 dbs_tuners_ins.ignore_nice = input;
264
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700265 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700266 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700267 struct cpu_dbs_info_s *dbs_info;
268 dbs_info = &per_cpu(cpu_dbs_info, j);
269 dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
270 dbs_info->prev_cpu_wall = get_jiffies_64();
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700271 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800272 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700273
274 return count;
275}
276
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400277static ssize_t store_powersave_bias(struct cpufreq_policy *unused,
278 const char *buf, size_t count)
279{
280 unsigned int input;
281 int ret;
282 ret = sscanf(buf, "%u", &input);
283
284 if (ret != 1)
285 return -EINVAL;
286
287 if (input > 1000)
288 input = 1000;
289
290 mutex_lock(&dbs_mutex);
291 dbs_tuners_ins.powersave_bias = input;
292 ondemand_powersave_bias_init();
293 mutex_unlock(&dbs_mutex);
294
295 return count;
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#define define_one_rw(_name) \
299static struct freq_attr _name = \
300__ATTR(_name, 0644, show_##_name, store_##_name)
301
302define_one_rw(sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800304define_one_rw(ignore_nice_load);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400305define_one_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307static struct attribute * dbs_attributes[] = {
308 &sampling_rate_max.attr,
309 &sampling_rate_min.attr,
310 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800312 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400313 &powersave_bias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 NULL
315};
316
317static struct attribute_group dbs_attr_group = {
318 .attrs = dbs_attributes,
319 .name = "ondemand",
320};
321
322/************************** sysfs end ************************/
323
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700324static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700326 unsigned int idle_ticks, total_ticks;
327 unsigned int load;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700328 cputime64_t cur_jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 struct cpufreq_policy *policy;
331 unsigned int j;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!this_dbs_info->enable)
334 return;
335
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400336 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 policy = this_dbs_info->cur_policy;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700338 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
339 total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
340 this_dbs_info->prev_cpu_wall);
341 this_dbs_info->prev_cpu_wall = cur_jiffies;
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700342 if (!total_ticks)
343 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500344 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700345 * Every sampling_rate, we check, if current idle time is less
346 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700347 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700348 * frequency which can sustain the load while keeping idle time over
349 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500351 * Any frequency increase takes it to the maximum frequency.
352 * Frequency reduction happens at minimum steps of
353 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
355
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700356 /* Get Idle Time */
Dave Jones9c7d2692005-05-31 19:03:49 -0700357 idle_ticks = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 for_each_cpu_mask(j, policy->cpus) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700359 cputime64_t total_idle_ticks;
360 unsigned int tmp_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct cpu_dbs_info_s *j_dbs_info;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700364 total_idle_ticks = get_cpu_idle_time(j);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700365 tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
366 j_dbs_info->prev_cpu_idle);
367 j_dbs_info->prev_cpu_idle = total_idle_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (tmp_idle_ticks < idle_ticks)
370 idle_ticks = tmp_idle_ticks;
371 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700372 load = (100 * (total_ticks - idle_ticks)) / total_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700374 /* Check for frequency increase */
375 if (load > dbs_tuners_ins.up_threshold) {
Dave Jonesc11420a2005-05-31 19:03:48 -0700376 /* if we are already at full speed then break out early */
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400377 if (!dbs_tuners_ins.powersave_bias) {
378 if (policy->cur == policy->max)
379 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500380
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400381 __cpufreq_driver_target(policy, policy->max,
382 CPUFREQ_RELATION_H);
383 } else {
384 int freq = powersave_bias_target(policy, policy->max,
385 CPUFREQ_RELATION_H);
386 __cpufreq_driver_target(policy, freq,
387 CPUFREQ_RELATION_L);
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return;
390 }
391
392 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700393 /* if we cannot reduce the frequency anymore, break out early */
394 if (policy->cur == policy->min)
395 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Dave Jonesc29f1402005-05-31 19:03:50 -0700397 /*
398 * The optimal frequency is the frequency that is the lowest that
399 * can support the current CPU usage without triggering the up
400 * policy. To be safe, we focus 10 points under the threshold.
401 */
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700402 if (load < (dbs_tuners_ins.up_threshold - 10)) {
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700403 unsigned int freq_next, freq_cur;
404
405 freq_cur = cpufreq_driver_getavg(policy);
406 if (!freq_cur)
407 freq_cur = policy->cur;
408
409 freq_next = (freq_cur * load) /
Dave Jonesc29f1402005-05-31 19:03:50 -0700410 (dbs_tuners_ins.up_threshold - 10);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700411
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400412 if (!dbs_tuners_ins.powersave_bias) {
413 __cpufreq_driver_target(policy, freq_next,
414 CPUFREQ_RELATION_L);
415 } else {
416 int freq = powersave_bias_target(policy, freq_next,
417 CPUFREQ_RELATION_L);
418 __cpufreq_driver_target(policy, freq,
419 CPUFREQ_RELATION_L);
420 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
David Howellsc4028952006-11-22 14:57:56 +0000424static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500425{
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700426 unsigned int cpu = smp_processor_id();
427 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
David Howellsc4028952006-11-22 14:57:56 +0000428 enum dbs_sample sample_type = dbs_info->sample_type;
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400429 /* We want all CPUs to do sampling nearly on same jiffy */
430 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
David Howellsc4028952006-11-22 14:57:56 +0000431
432 /* Permit rescheduling of this work item */
433 work_release(work);
434
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400435 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700436
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700437 if (!dbs_info->enable)
438 return;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400439 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000440 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400441 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000442 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400443 lock_cpu_hotplug();
444 dbs_check_cpu(dbs_info);
445 unlock_cpu_hotplug();
446 if (dbs_info->freq_lo) {
447 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000448 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400449 delay = dbs_info->freq_hi_jiffies;
450 }
451 } else {
452 __cpufreq_driver_target(dbs_info->cur_policy,
453 dbs_info->freq_lo,
454 CPUFREQ_RELATION_H);
455 }
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400456 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
Dave Jones32ee8c32006-02-28 00:43:23 -0500457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700459static inline void dbs_timer_init(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700461 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu);
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 INIT_DELAYED_WORK_NAR(&dbs_info->work, do_dbs_timer);
468 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400469 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700472static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700474 dbs_info->enable = 0;
475 cancel_delayed_work(&dbs_info->work);
476 flush_workqueue(kondemand_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
480 unsigned int event)
481{
482 unsigned int cpu = policy->cpu;
483 struct cpu_dbs_info_s *this_dbs_info;
484 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700485 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
488
489 switch (event) {
490 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700491 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EINVAL;
493
494 if (policy->cpuinfo.transition_latency >
Eric Pielff8c2882006-03-10 11:34:16 +0200495 (TRANSITION_LATENCY_LIMIT * 1000)) {
496 printk(KERN_WARNING "ondemand governor failed to load "
497 "due to too long transition latency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -EINVAL;
Eric Pielff8c2882006-03-10 11:34:16 +0200499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (this_dbs_info->enable) /* Already enabled */
501 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500502
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800503 mutex_lock(&dbs_mutex);
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700504 dbs_enable++;
505 if (dbs_enable == 1) {
506 kondemand_wq = create_workqueue("kondemand");
507 if (!kondemand_wq) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530508 printk(KERN_ERR
509 "Creation of kondemand failed\n");
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700510 dbs_enable--;
511 mutex_unlock(&dbs_mutex);
512 return -ENOSPC;
513 }
514 }
Jeff Garzik914f7c32006-10-20 14:31:00 -0700515
516 rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
517 if (rc) {
518 if (dbs_enable == 1)
519 destroy_workqueue(kondemand_wq);
520 dbs_enable--;
521 mutex_unlock(&dbs_mutex);
522 return rc;
523 }
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 for_each_cpu_mask(j, policy->cpus) {
526 struct cpu_dbs_info_s *j_dbs_info;
527 j_dbs_info = &per_cpu(cpu_dbs_info, j);
528 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500529
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700530 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
531 j_dbs_info->prev_cpu_wall = get_jiffies_64();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 this_dbs_info->enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
535 * Start the timerschedule work, when this governor
536 * is used for first time
537 */
538 if (dbs_enable == 1) {
539 unsigned int latency;
540 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700541 latency = policy->cpuinfo.transition_latency / 1000;
542 if (latency == 0)
543 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700545 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700547
548 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
549 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700553 dbs_timer_init(policy->cpu);
Dave Jones32ee8c32006-02-28 00:43:23 -0500554
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800555 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 break;
557
558 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800559 mutex_lock(&dbs_mutex);
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700560 dbs_timer_exit(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
562 dbs_enable--;
Dave Jones32ee8c32006-02-28 00:43:23 -0500563 if (dbs_enable == 0)
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700564 destroy_workqueue(kondemand_wq);
Dave Jones32ee8c32006-02-28 00:43:23 -0500565
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800566 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 break;
569
570 case CPUFREQ_GOV_LIMITS:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800571 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700573 __cpufreq_driver_target(this_dbs_info->cur_policy,
574 policy->max,
575 CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700577 __cpufreq_driver_target(this_dbs_info->cur_policy,
578 policy->min,
579 CPUFREQ_RELATION_L);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800580 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 }
583 return 0;
584}
585
Dave Jones7f335d42005-05-31 19:03:46 -0700586static struct cpufreq_governor cpufreq_gov_dbs = {
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700587 .name = "ondemand",
588 .governor = cpufreq_governor_dbs,
589 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592static int __init cpufreq_gov_dbs_init(void)
593{
594 return cpufreq_register_governor(&cpufreq_gov_dbs);
595}
596
597static void __exit cpufreq_gov_dbs_exit(void)
598{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 cpufreq_unregister_governor(&cpufreq_gov_dbs);
600}
601
602
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700603MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
604MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
605MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
606 "Low Latency Frequency Transition capable processors");
607MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609module_init(cpufreq_gov_dbs_init);
610module_exit(cpufreq_gov_dbs_exit);