blob: b935092aab216fad666a9b8e8e301c19c53a49d8 [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)
Thomas Renninger1c256242007-10-02 13:28:12 -070050#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 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.pallipadi@intel.com34305022008-08-04 11:59:09 -070097static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
Dave Jonesdac1c1a2005-05-31 19:03:49 -070098{
Venki Pallipadiea487612007-06-20 14:26:24 -070099 cputime64_t idle_time;
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700100 cputime64_t cur_wall_time;
Venki Pallipadiea487612007-06-20 14:26:24 -0700101 cputime64_t busy_time;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700102
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700103 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
Venki Pallipadiea487612007-06-20 14:26:24 -0700104 busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
105 kstat_cpu(cpu).cpustat.system);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700106
Venki Pallipadiea487612007-06-20 14:26:24 -0700107 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
108 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
109 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700110
Venki Pallipadiea487612007-06-20 14:26:24 -0700111 if (!dbs_tuners_ins.ignore_nice) {
112 busy_time = cputime64_add(busy_time,
113 kstat_cpu(cpu).cpustat.nice);
114 }
115
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700116 idle_time = cputime64_sub(cur_wall_time, busy_time);
117 if (wall)
118 *wall = cur_wall_time;
119
Venki Pallipadiea487612007-06-20 14:26:24 -0700120 return idle_time;
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700121}
122
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400123/*
124 * Find right freq to be set now with powersave_bias on.
125 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
126 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
127 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +0200128static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
129 unsigned int freq_next,
130 unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400131{
132 unsigned int freq_req, freq_reduc, freq_avg;
133 unsigned int freq_hi, freq_lo;
134 unsigned int index = 0;
135 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
136 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu);
137
138 if (!dbs_info->freq_table) {
139 dbs_info->freq_lo = 0;
140 dbs_info->freq_lo_jiffies = 0;
141 return freq_next;
142 }
143
144 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
145 relation, &index);
146 freq_req = dbs_info->freq_table[index].frequency;
147 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
148 freq_avg = freq_req - freq_reduc;
149
150 /* Find freq bounds for freq_avg in freq_table */
151 index = 0;
152 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
153 CPUFREQ_RELATION_H, &index);
154 freq_lo = dbs_info->freq_table[index].frequency;
155 index = 0;
156 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
157 CPUFREQ_RELATION_L, &index);
158 freq_hi = dbs_info->freq_table[index].frequency;
159
160 /* Find out how long we have to be in hi and lo freqs */
161 if (freq_hi == freq_lo) {
162 dbs_info->freq_lo = 0;
163 dbs_info->freq_lo_jiffies = 0;
164 return freq_lo;
165 }
166 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
167 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
168 jiffies_hi += ((freq_hi - freq_lo) / 2);
169 jiffies_hi /= (freq_hi - freq_lo);
170 jiffies_lo = jiffies_total - jiffies_hi;
171 dbs_info->freq_lo = freq_lo;
172 dbs_info->freq_lo_jiffies = jiffies_lo;
173 dbs_info->freq_hi_jiffies = jiffies_hi;
174 return freq_hi;
175}
176
177static void ondemand_powersave_bias_init(void)
178{
179 int i;
180 for_each_online_cpu(i) {
181 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i);
182 dbs_info->freq_table = cpufreq_frequency_get_table(i);
183 dbs_info->freq_lo = 0;
184 }
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/************************** sysfs interface ************************/
188static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
189{
190 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
191}
192
193static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
194{
195 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
196}
197
Dave Jones32ee8c32006-02-28 00:43:23 -0500198#define define_one_ro(_name) \
199static struct freq_attr _name = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200__ATTR(_name, 0444, show_##_name, NULL)
201
202define_one_ro(sampling_rate_max);
203define_one_ro(sampling_rate_min);
204
205/* cpufreq_ondemand Governor Tunables */
206#define show_one(file_name, object) \
207static ssize_t show_##file_name \
208(struct cpufreq_policy *unused, char *buf) \
209{ \
210 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
211}
212show_one(sampling_rate, sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213show_one(up_threshold, up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800214show_one(ignore_nice_load, ignore_nice);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400215show_one(powersave_bias, powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Dave Jones32ee8c32006-02-28 00:43:23 -0500217static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 const char *buf, size_t count)
219{
220 unsigned int input;
221 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700222 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800224 mutex_lock(&dbs_mutex);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530225 if (ret != 1 || input > MAX_SAMPLING_RATE
226 || input < MIN_SAMPLING_RATE) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800227 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EINVAL;
229 }
230
231 dbs_tuners_ins.sampling_rate = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800232 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 return count;
235}
236
Dave Jones32ee8c32006-02-28 00:43:23 -0500237static ssize_t store_up_threshold(struct cpufreq_policy *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 const char *buf, size_t count)
239{
240 unsigned int input;
241 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700242 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800244 mutex_lock(&dbs_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500245 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700246 input < MIN_FREQUENCY_UP_THRESHOLD) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800247 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EINVAL;
249 }
250
251 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800252 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return count;
255}
256
Alexander Clouter001893c2005-12-01 01:09:25 -0800257static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700258 const char *buf, size_t count)
259{
260 unsigned int input;
261 int ret;
262
263 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500264
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700265 ret = sscanf(buf, "%u", &input);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700266 if ( ret != 1 )
267 return -EINVAL;
268
269 if ( input > 1 )
270 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500271
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800272 mutex_lock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700273 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800274 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700275 return count;
276 }
277 dbs_tuners_ins.ignore_nice = input;
278
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700279 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700280 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700281 struct cpu_dbs_info_s *dbs_info;
282 dbs_info = &per_cpu(cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700283 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
284 &dbs_info->prev_cpu_wall);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700285 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800286 mutex_unlock(&dbs_mutex);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700287
288 return count;
289}
290
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400291static ssize_t store_powersave_bias(struct cpufreq_policy *unused,
292 const char *buf, size_t count)
293{
294 unsigned int input;
295 int ret;
296 ret = sscanf(buf, "%u", &input);
297
298 if (ret != 1)
299 return -EINVAL;
300
301 if (input > 1000)
302 input = 1000;
303
304 mutex_lock(&dbs_mutex);
305 dbs_tuners_ins.powersave_bias = input;
306 ondemand_powersave_bias_init();
307 mutex_unlock(&dbs_mutex);
308
309 return count;
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312#define define_one_rw(_name) \
313static struct freq_attr _name = \
314__ATTR(_name, 0644, show_##_name, store_##_name)
315
316define_one_rw(sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317define_one_rw(up_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800318define_one_rw(ignore_nice_load);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400319define_one_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321static struct attribute * dbs_attributes[] = {
322 &sampling_rate_max.attr,
323 &sampling_rate_min.attr,
324 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 &up_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800326 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400327 &powersave_bias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 NULL
329};
330
331static struct attribute_group dbs_attr_group = {
332 .attrs = dbs_attributes,
333 .name = "ondemand",
334};
335
336/************************** sysfs end ************************/
337
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700338static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700340 unsigned int max_load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 struct cpufreq_policy *policy;
343 unsigned int j;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!this_dbs_info->enable)
346 return;
347
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400348 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 policy = this_dbs_info->cur_policy;
Venki Pallipadiea487612007-06-20 14:26:24 -0700350
Dave Jones32ee8c32006-02-28 00:43:23 -0500351 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700352 * Every sampling_rate, we check, if current idle time is less
353 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700354 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700355 * frequency which can sustain the load while keeping idle time over
356 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500358 * Any frequency increase takes it to the maximum frequency.
359 * Frequency reduction happens at minimum steps of
360 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
362
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700363 /* Get Absolute Load - in terms of freq */
364 max_load_freq = 0;
365
Mike Travis068b1272008-05-12 21:21:13 +0200366 for_each_cpu_mask_nr(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct cpu_dbs_info_s *j_dbs_info;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700368 cputime64_t cur_wall_time, cur_idle_time;
369 unsigned int idle_time, wall_time;
370 unsigned int load, load_freq;
371 int freq_avg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 j_dbs_info = &per_cpu(cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700374
375 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
376
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700377 wall_time = (unsigned int) cputime64_sub(cur_wall_time,
378 j_dbs_info->prev_cpu_wall);
379 j_dbs_info->prev_cpu_wall = cur_wall_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700381 idle_time = (unsigned int) cputime64_sub(cur_idle_time,
382 j_dbs_info->prev_cpu_idle);
383 j_dbs_info->prev_cpu_idle = cur_idle_time;
384
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700385 if (unlikely(!wall_time || wall_time < idle_time))
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700386 continue;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700387
388 load = 100 * (wall_time - idle_time) / wall_time;
389
390 freq_avg = __cpufreq_driver_getavg(policy, j);
391 if (freq_avg <= 0)
392 freq_avg = policy->cur;
393
394 load_freq = load * freq_avg;
395 if (load_freq > max_load_freq)
396 max_load_freq = load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700399 /* Check for frequency increase */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700400 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
Dave Jonesc11420a2005-05-31 19:03:48 -0700401 /* if we are already at full speed then break out early */
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400402 if (!dbs_tuners_ins.powersave_bias) {
403 if (policy->cur == policy->max)
404 return;
Dave Jones32ee8c32006-02-28 00:43:23 -0500405
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400406 __cpufreq_driver_target(policy, policy->max,
407 CPUFREQ_RELATION_H);
408 } else {
409 int freq = powersave_bias_target(policy, policy->max,
410 CPUFREQ_RELATION_H);
411 __cpufreq_driver_target(policy, freq,
412 CPUFREQ_RELATION_L);
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return;
415 }
416
417 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700418 /* if we cannot reduce the frequency anymore, break out early */
419 if (policy->cur == policy->min)
420 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Dave Jonesc29f1402005-05-31 19:03:50 -0700422 /*
423 * The optimal frequency is the frequency that is the lowest that
424 * can support the current CPU usage without triggering the up
425 * policy. To be safe, we focus 10 points under the threshold.
426 */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700427 if (max_load_freq < (dbs_tuners_ins.up_threshold - 10) * policy->cur) {
428 unsigned int freq_next;
429 freq_next = max_load_freq / (dbs_tuners_ins.up_threshold - 10);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700430
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400431 if (!dbs_tuners_ins.powersave_bias) {
432 __cpufreq_driver_target(policy, freq_next,
433 CPUFREQ_RELATION_L);
434 } else {
435 int freq = powersave_bias_target(policy, freq_next,
436 CPUFREQ_RELATION_L);
437 __cpufreq_driver_target(policy, freq,
438 CPUFREQ_RELATION_L);
439 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
David Howellsc4028952006-11-22 14:57:56 +0000443static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500444{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800445 struct cpu_dbs_info_s *dbs_info =
446 container_of(work, struct cpu_dbs_info_s, work.work);
447 unsigned int cpu = dbs_info->cpu;
448 int sample_type = dbs_info->sample_type;
449
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400450 /* We want all CPUs to do sampling nearly on same jiffy */
451 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
David Howellsc4028952006-11-22 14:57:56 +0000452
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400453 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700454
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800455 if (lock_policy_rwsem_write(cpu) < 0)
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700456 return;
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800457
458 if (!dbs_info->enable) {
459 unlock_policy_rwsem_write(cpu);
460 return;
461 }
462
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400463 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000464 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400465 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000466 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400467 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400468 if (dbs_info->freq_lo) {
469 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000470 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400471 delay = dbs_info->freq_hi_jiffies;
472 }
473 } else {
474 __cpufreq_driver_target(dbs_info->cur_policy,
475 dbs_info->freq_lo,
476 CPUFREQ_RELATION_H);
477 }
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400478 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800479 unlock_policy_rwsem_write(cpu);
Dave Jones32ee8c32006-02-28 00:43:23 -0500480}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800482static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400484 /* We want all CPUs to do sampling nearly on same jiffy */
485 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
486 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700487
Dave Jonesc18a1482007-02-10 20:03:51 -0500488 dbs_info->enable = 1;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400489 ondemand_powersave_bias_init();
David Howellsc4028952006-11-22 14:57:56 +0000490 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Venki Pallipadi28287032007-05-08 00:27:47 -0700491 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800492 queue_delayed_work_on(dbs_info->cpu, kondemand_wq, &dbs_info->work,
493 delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700496static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700498 dbs_info->enable = 0;
499 cancel_delayed_work(&dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
503 unsigned int event)
504{
505 unsigned int cpu = policy->cpu;
506 struct cpu_dbs_info_s *this_dbs_info;
507 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700508 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
511
512 switch (event) {
513 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700514 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EINVAL;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (this_dbs_info->enable) /* Already enabled */
518 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500519
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800520 mutex_lock(&dbs_mutex);
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700521 dbs_enable++;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700522
523 rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
524 if (rc) {
Jeff Garzik914f7c32006-10-20 14:31:00 -0700525 dbs_enable--;
526 mutex_unlock(&dbs_mutex);
527 return rc;
528 }
529
Mike Travis068b1272008-05-12 21:21:13 +0200530 for_each_cpu_mask_nr(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct cpu_dbs_info_s *j_dbs_info;
532 j_dbs_info = &per_cpu(cpu_dbs_info, j);
533 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500534
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700535 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
536 &j_dbs_info->prev_cpu_wall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800538 this_dbs_info->cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /*
540 * Start the timerschedule work, when this governor
541 * is used for first time
542 */
543 if (dbs_enable == 1) {
544 unsigned int latency;
545 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700546 latency = policy->cpuinfo.transition_latency / 1000;
547 if (latency == 0)
548 latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700550 def_sampling_rate = latency *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700552
553 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
554 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800558 dbs_timer_init(this_dbs_info);
Dave Jones32ee8c32006-02-28 00:43:23 -0500559
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800560 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
562
563 case CPUFREQ_GOV_STOP:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800564 mutex_lock(&dbs_mutex);
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700565 dbs_timer_exit(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
567 dbs_enable--;
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
Thomas Renninger1c256242007-10-02 13:28:12 -0700588struct cpufreq_governor cpufreq_gov_ondemand = {
589 .name = "ondemand",
590 .governor = cpufreq_governor_dbs,
591 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
592 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593};
Thomas Renninger1c256242007-10-02 13:28:12 -0700594EXPORT_SYMBOL(cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596static int __init cpufreq_gov_dbs_init(void)
597{
Akinobu Mita888a7942008-07-14 12:00:45 +0900598 int err;
599
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800600 kondemand_wq = create_workqueue("kondemand");
601 if (!kondemand_wq) {
602 printk(KERN_ERR "Creation of kondemand failed\n");
603 return -EFAULT;
604 }
Akinobu Mita888a7942008-07-14 12:00:45 +0900605 err = cpufreq_register_governor(&cpufreq_gov_ondemand);
606 if (err)
607 destroy_workqueue(kondemand_wq);
608
609 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612static void __exit cpufreq_gov_dbs_exit(void)
613{
Thomas Renninger1c256242007-10-02 13:28:12 -0700614 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800615 destroy_workqueue(kondemand_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700619MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
620MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
621MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
622 "Low Latency Frequency Transition capable processors");
623MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Johannes Weiner69157192008-01-17 15:21:08 -0800625#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
626fs_initcall(cpufreq_gov_dbs_init);
627#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800629#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630module_exit(cpufreq_gov_dbs_exit);