blob: d7f774bb49dd3f9aa699e5b328c46b5da2e828f5 [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>
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070021#include <linux/hrtimer.h>
22#include <linux/tick.h>
23#include <linux/ktime.h>
Thomas Renninger9411b4e2009-02-04 11:54:04 +010024#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * dbs is used in this file as a shortform for demandbased switching
28 * It helps to keep variable names smaller, simpler
29 */
30
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -070031#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define DEF_FREQUENCY_UP_THRESHOLD (80)
David C Niemi3f78a9f2010-10-06 16:54:24 -040033#define DEF_SAMPLING_DOWN_FACTOR (1)
34#define MAX_SAMPLING_DOWN_FACTOR (100000)
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070035#define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
36#define MICRO_FREQUENCY_UP_THRESHOLD (95)
Thomas Renningercef96152009-04-22 13:48:29 +020037#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
Dave Jonesc29f1402005-05-31 19:03:50 -070038#define MIN_FREQUENCY_UP_THRESHOLD (11)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define MAX_FREQUENCY_UP_THRESHOLD (100)
40
Dave Jones32ee8c32006-02-28 00:43:23 -050041/*
42 * The polling frequency of this governor depends on the capability of
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * the processor. Default polling frequency is 1000 times the transition
Dave Jones32ee8c32006-02-28 00:43:23 -050044 * latency of the processor. The governor will work on any processor with
45 * transition latency <= 10mS, using appropriate sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * rate.
47 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
48 * this governor will not work.
49 * All times here are in uS.
50 */
Dave Jonesdf8b59b2005-09-20 12:39:35 -070051#define MIN_SAMPLING_RATE_RATIO (2)
Thomas Renninger112124a2009-02-04 11:55:12 +010052
Thomas Renningercef96152009-04-22 13:48:29 +020053static unsigned int min_sampling_rate;
54
Thomas Renninger112124a2009-02-04 11:55:12 +010055#define LATENCY_MULTIPLIER (1000)
Thomas Renningercef96152009-04-22 13:48:29 +020056#define MIN_LATENCY_MULTIPLIER (100)
Thomas Renninger1c256242007-10-02 13:28:12 -070057#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Howellsc4028952006-11-22 14:57:56 +000059static void do_dbs_timer(struct work_struct *work);
Thomas Renninger0e625ac2009-07-24 15:25:06 +020060static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
61 unsigned int event);
62
63#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
64static
65#endif
66struct cpufreq_governor cpufreq_gov_ondemand = {
67 .name = "ondemand",
68 .governor = cpufreq_governor_dbs,
69 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
70 .owner = THIS_MODULE,
71};
David Howellsc4028952006-11-22 14:57:56 +000072
73/* Sampling types */
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080074enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76struct cpu_dbs_info_s {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070077 cputime64_t prev_cpu_idle;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070078 cputime64_t prev_cpu_iowait;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070079 cputime64_t prev_cpu_wall;
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070080 cputime64_t prev_cpu_nice;
Dave Jones32ee8c32006-02-28 00:43:23 -050081 struct cpufreq_policy *cur_policy;
Dave Jones2b03f892009-01-18 01:43:44 -050082 struct delayed_work work;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040083 struct cpufreq_frequency_table *freq_table;
84 unsigned int freq_lo;
85 unsigned int freq_lo_jiffies;
86 unsigned int freq_hi_jiffies;
David C Niemi3f78a9f2010-10-06 16:54:24 -040087 unsigned int rate_mult;
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080088 int cpu;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -070089 unsigned int sample_type:1;
90 /*
91 * percpu mutex that serializes governor limit change with
92 * do_dbs_timer invocation. We do not want do_dbs_timer to run
93 * when user is changing the governor or limits.
94 */
95 struct mutex timer_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
Tejun Heo245b2e72009-06-24 15:13:48 +090097static DEFINE_PER_CPU(struct cpu_dbs_info_s, od_cpu_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99static unsigned int dbs_enable; /* number of CPUs using this policy */
100
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700101/*
Thomas Renninger326c86d2011-03-03 21:31:27 +0100102 * dbs_mutex protects dbs_enable in governor start/stop.
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700103 */
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700104static DEFINE_MUTEX(dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400106static struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -0500107 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -0500108 unsigned int up_threshold;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700109 unsigned int down_differential;
Dave Jones32ee8c32006-02-28 00:43:23 -0500110 unsigned int ignore_nice;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400111 unsigned int sampling_down_factor;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400112 unsigned int powersave_bias;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700113 unsigned int io_is_busy;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400114} dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -0500115 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400116 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700117 .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
Eric Piel9cbad612006-03-10 11:35:27 +0200118 .ignore_nice = 0,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400119 .powersave_bias = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700122static inline cputime64_t get_cpu_iowait_time(unsigned int cpu, cputime64_t *wall)
123{
124 u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
125
126 if (iowait_time == -1ULL)
127 return 0;
128
129 return iowait_time;
130}
131
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400132/*
133 * Find right freq to be set now with powersave_bias on.
134 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
135 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
136 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +0200137static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
138 unsigned int freq_next,
139 unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400140{
141 unsigned int freq_req, freq_reduc, freq_avg;
142 unsigned int freq_hi, freq_lo;
143 unsigned int index = 0;
144 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
Tejun Heo245b2e72009-06-24 15:13:48 +0900145 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
146 policy->cpu);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400147
148 if (!dbs_info->freq_table) {
149 dbs_info->freq_lo = 0;
150 dbs_info->freq_lo_jiffies = 0;
151 return freq_next;
152 }
153
154 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
155 relation, &index);
156 freq_req = dbs_info->freq_table[index].frequency;
157 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
158 freq_avg = freq_req - freq_reduc;
159
160 /* Find freq bounds for freq_avg in freq_table */
161 index = 0;
162 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
163 CPUFREQ_RELATION_H, &index);
164 freq_lo = dbs_info->freq_table[index].frequency;
165 index = 0;
166 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
167 CPUFREQ_RELATION_L, &index);
168 freq_hi = dbs_info->freq_table[index].frequency;
169
170 /* Find out how long we have to be in hi and lo freqs */
171 if (freq_hi == freq_lo) {
172 dbs_info->freq_lo = 0;
173 dbs_info->freq_lo_jiffies = 0;
174 return freq_lo;
175 }
176 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
177 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
178 jiffies_hi += ((freq_hi - freq_lo) / 2);
179 jiffies_hi /= (freq_hi - freq_lo);
180 jiffies_lo = jiffies_total - jiffies_hi;
181 dbs_info->freq_lo = freq_lo;
182 dbs_info->freq_lo_jiffies = jiffies_lo;
183 dbs_info->freq_hi_jiffies = jiffies_hi;
184 return freq_hi;
185}
186
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700187static void ondemand_powersave_bias_init_cpu(int cpu)
188{
Tejun Heo384be2b2009-08-14 14:41:02 +0900189 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700190 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
191 dbs_info->freq_lo = 0;
192}
193
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400194static void ondemand_powersave_bias_init(void)
195{
196 int i;
197 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700198 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400199 }
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/************************** sysfs interface ************************/
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200203
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200204static ssize_t show_sampling_rate_min(struct kobject *kobj,
205 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Thomas Renningercef96152009-04-22 13:48:29 +0200207 return sprintf(buf, "%u\n", min_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200210define_one_global_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/* cpufreq_ondemand Governor Tunables */
213#define show_one(file_name, object) \
214static ssize_t show_##file_name \
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200215(struct kobject *kobj, struct attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{ \
217 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
218}
219show_one(sampling_rate, sampling_rate);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700220show_one(io_is_busy, io_is_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221show_one(up_threshold, up_threshold);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400222show_one(sampling_down_factor, sampling_down_factor);
Alexander Clouter001893c2005-12-01 01:09:25 -0800223show_one(ignore_nice_load, ignore_nice);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400224show_one(powersave_bias, powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900226/**
227 * update_sampling_rate - update sampling rate effective immediately if needed.
228 * @new_rate: new sampling rate
229 *
230 * If new rate is smaller than the old, simply updaing
231 * dbs_tuners_int.sampling_rate might not be appropriate. For example,
232 * if the original sampling_rate was 1 second and the requested new sampling
233 * rate is 10 ms because the user needs immediate reaction from ondemand
234 * governor, but not sure if higher frequency will be required or not,
235 * then, the governor may change the sampling rate too late; up to 1 second
236 * later. Thus, if we are reducing the sampling rate, we need to make the
237 * new value effective immediately.
238 */
239static void update_sampling_rate(unsigned int new_rate)
240{
241 int cpu;
242
243 dbs_tuners_ins.sampling_rate = new_rate
244 = max(new_rate, min_sampling_rate);
245
246 for_each_online_cpu(cpu) {
247 struct cpufreq_policy *policy;
248 struct cpu_dbs_info_s *dbs_info;
249 unsigned long next_sampling, appointed_at;
250
251 policy = cpufreq_cpu_get(cpu);
252 if (!policy)
253 continue;
254 dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
255 cpufreq_cpu_put(policy);
256
257 mutex_lock(&dbs_info->timer_mutex);
258
259 if (!delayed_work_pending(&dbs_info->work)) {
260 mutex_unlock(&dbs_info->timer_mutex);
261 continue;
262 }
263
264 next_sampling = jiffies + usecs_to_jiffies(new_rate);
265 appointed_at = dbs_info->work.timer.expires;
266
267
268 if (time_before(next_sampling, appointed_at)) {
269
270 mutex_unlock(&dbs_info->timer_mutex);
271 cancel_delayed_work_sync(&dbs_info->work);
272 mutex_lock(&dbs_info->timer_mutex);
273
274 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work,
275 usecs_to_jiffies(new_rate));
276
277 }
278 mutex_unlock(&dbs_info->timer_mutex);
279 }
280}
281
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200282static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
283 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 unsigned int input;
286 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700287 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700288 if (ret != 1)
289 return -EINVAL;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900290 update_sampling_rate(input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return count;
292}
293
Arjan van de Ven19379b12010-05-09 08:26:51 -0700294static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
295 const char *buf, size_t count)
296{
297 unsigned int input;
298 int ret;
299
300 ret = sscanf(buf, "%u", &input);
301 if (ret != 1)
302 return -EINVAL;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700303 dbs_tuners_ins.io_is_busy = !!input;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700304 return count;
305}
306
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200307static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
308 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 unsigned int input;
311 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700312 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dave Jones32ee8c32006-02-28 00:43:23 -0500314 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700315 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EINVAL;
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 dbs_tuners_ins.up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return count;
320}
321
David C Niemi3f78a9f2010-10-06 16:54:24 -0400322static ssize_t store_sampling_down_factor(struct kobject *a,
323 struct attribute *b, const char *buf, size_t count)
324{
325 unsigned int input, j;
326 int ret;
327 ret = sscanf(buf, "%u", &input);
328
329 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
330 return -EINVAL;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400331 dbs_tuners_ins.sampling_down_factor = input;
332
333 /* Reset down sampling multiplier in case it was active */
334 for_each_online_cpu(j) {
335 struct cpu_dbs_info_s *dbs_info;
336 dbs_info = &per_cpu(od_cpu_dbs_info, j);
337 dbs_info->rate_mult = 1;
338 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400339 return count;
340}
341
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200342static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
343 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700344{
345 unsigned int input;
346 int ret;
347
348 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500349
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700350 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500351 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700352 return -EINVAL;
353
Dave Jones2b03f892009-01-18 01:43:44 -0500354 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700355 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500356
Dave Jones2b03f892009-01-18 01:43:44 -0500357 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700358 return count;
359 }
360 dbs_tuners_ins.ignore_nice = input;
361
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700362 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700363 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700364 struct cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900365 dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700366 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
367 &dbs_info->prev_cpu_wall);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500368 if (dbs_tuners_ins.ignore_nice)
Glauber Costa3292beb2011-11-28 14:45:17 -0200369 dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500370
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700371 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700372 return count;
373}
374
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200375static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
376 const char *buf, size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400377{
378 unsigned int input;
379 int ret;
380 ret = sscanf(buf, "%u", &input);
381
382 if (ret != 1)
383 return -EINVAL;
384
385 if (input > 1000)
386 input = 1000;
387
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400388 dbs_tuners_ins.powersave_bias = input;
389 ondemand_powersave_bias_init();
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400390 return count;
391}
392
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200393define_one_global_rw(sampling_rate);
Linus Torvalds07d77752010-05-18 08:49:13 -0700394define_one_global_rw(io_is_busy);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200395define_one_global_rw(up_threshold);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400396define_one_global_rw(sampling_down_factor);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200397define_one_global_rw(ignore_nice_load);
398define_one_global_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Dave Jones2b03f892009-01-18 01:43:44 -0500400static struct attribute *dbs_attributes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 &sampling_rate_min.attr,
402 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 &up_threshold.attr,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400404 &sampling_down_factor.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800405 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400406 &powersave_bias.attr,
Arjan van de Ven19379b12010-05-09 08:26:51 -0700407 &io_is_busy.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 NULL
409};
410
411static struct attribute_group dbs_attr_group = {
412 .attrs = dbs_attributes,
413 .name = "ondemand",
414};
415
416/************************** sysfs end ************************/
417
Mike Chan00e299f2010-01-26 17:06:47 -0800418static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
419{
420 if (dbs_tuners_ins.powersave_bias)
421 freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
422 else if (p->cur == p->max)
423 return;
424
425 __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
426 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
427}
428
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700429static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700431 unsigned int max_load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 struct cpufreq_policy *policy;
434 unsigned int j;
435
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400436 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 policy = this_dbs_info->cur_policy;
Venki Pallipadiea487612007-06-20 14:26:24 -0700438
Dave Jones32ee8c32006-02-28 00:43:23 -0500439 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700440 * Every sampling_rate, we check, if current idle time is less
441 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700442 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700443 * frequency which can sustain the load while keeping idle time over
444 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500446 * Any frequency increase takes it to the maximum frequency.
447 * Frequency reduction happens at minimum steps of
448 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
450
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700451 /* Get Absolute Load - in terms of freq */
452 max_load_freq = 0;
453
Rusty Russell835481d2009-01-04 05:18:06 -0800454 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct cpu_dbs_info_s *j_dbs_info;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700456 cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
457 unsigned int idle_time, wall_time, iowait_time;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700458 unsigned int load, load_freq;
459 int freq_avg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Tejun Heo245b2e72009-06-24 15:13:48 +0900461 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700462
463 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700464 cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700465
Martin Schwidefsky64861632011-12-15 14:56:09 +0100466 wall_time = (unsigned int)
467 (cur_wall_time - j_dbs_info->prev_cpu_wall);
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700468 j_dbs_info->prev_cpu_wall = cur_wall_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Martin Schwidefsky64861632011-12-15 14:56:09 +0100470 idle_time = (unsigned int)
471 (cur_idle_time - j_dbs_info->prev_cpu_idle);
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700472 j_dbs_info->prev_cpu_idle = cur_idle_time;
473
Martin Schwidefsky64861632011-12-15 14:56:09 +0100474 iowait_time = (unsigned int)
475 (cur_iowait_time - j_dbs_info->prev_cpu_iowait);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700476 j_dbs_info->prev_cpu_iowait = cur_iowait_time;
477
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500478 if (dbs_tuners_ins.ignore_nice) {
Glauber Costa3292beb2011-11-28 14:45:17 -0200479 u64 cur_nice;
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500480 unsigned long cur_nice_jiffies;
481
Glauber Costa3292beb2011-11-28 14:45:17 -0200482 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
483 j_dbs_info->prev_cpu_nice;
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500484 /*
485 * Assumption: nice time between sampling periods will
486 * be less than 2^32 jiffies for 32 bit sys
487 */
488 cur_nice_jiffies = (unsigned long)
489 cputime64_to_jiffies64(cur_nice);
490
Glauber Costa3292beb2011-11-28 14:45:17 -0200491 j_dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500492 idle_time += jiffies_to_usecs(cur_nice_jiffies);
493 }
494
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700495 /*
496 * For the purpose of ondemand, waiting for disk IO is an
497 * indication that you're performance critical, and not that
498 * the system is actually idle. So subtract the iowait time
499 * from the cpu idle time.
500 */
501
Arjan van de Ven19379b12010-05-09 08:26:51 -0700502 if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700503 idle_time -= iowait_time;
504
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700505 if (unlikely(!wall_time || wall_time < idle_time))
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700506 continue;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700507
508 load = 100 * (wall_time - idle_time) / wall_time;
509
510 freq_avg = __cpufreq_driver_getavg(policy, j);
511 if (freq_avg <= 0)
512 freq_avg = policy->cur;
513
514 load_freq = load * freq_avg;
515 if (load_freq > max_load_freq)
516 max_load_freq = load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700519 /* Check for frequency increase */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700520 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
David C Niemi3f78a9f2010-10-06 16:54:24 -0400521 /* If switching to max speed, apply sampling_down_factor */
522 if (policy->cur < policy->max)
523 this_dbs_info->rate_mult =
524 dbs_tuners_ins.sampling_down_factor;
Mike Chan00e299f2010-01-26 17:06:47 -0800525 dbs_freq_increase(policy, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return;
527 }
528
529 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700530 /* if we cannot reduce the frequency anymore, break out early */
531 if (policy->cur == policy->min)
532 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Dave Jonesc29f1402005-05-31 19:03:50 -0700534 /*
535 * The optimal frequency is the frequency that is the lowest that
536 * can support the current CPU usage without triggering the up
537 * policy. To be safe, we focus 10 points under the threshold.
538 */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700539 if (max_load_freq <
540 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
541 policy->cur) {
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700542 unsigned int freq_next;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700543 freq_next = max_load_freq /
544 (dbs_tuners_ins.up_threshold -
545 dbs_tuners_ins.down_differential);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700546
David C Niemi3f78a9f2010-10-06 16:54:24 -0400547 /* No longer fully busy, reset rate_mult */
548 this_dbs_info->rate_mult = 1;
549
Nagananda.Chumbalkar@hp.com1dbf5882009-12-21 23:40:52 +0100550 if (freq_next < policy->min)
551 freq_next = policy->min;
552
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400553 if (!dbs_tuners_ins.powersave_bias) {
554 __cpufreq_driver_target(policy, freq_next,
555 CPUFREQ_RELATION_L);
556 } else {
557 int freq = powersave_bias_target(policy, freq_next,
558 CPUFREQ_RELATION_L);
559 __cpufreq_driver_target(policy, freq,
560 CPUFREQ_RELATION_L);
561 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
David Howellsc4028952006-11-22 14:57:56 +0000565static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500566{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800567 struct cpu_dbs_info_s *dbs_info =
568 container_of(work, struct cpu_dbs_info_s, work.work);
569 unsigned int cpu = dbs_info->cpu;
570 int sample_type = dbs_info->sample_type;
571
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100572 int delay;
Jocelyn Falempea665df92010-03-11 14:01:11 -0800573
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700574 mutex_lock(&dbs_info->timer_mutex);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800575
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400576 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000577 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400578 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000579 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400580 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400581 if (dbs_info->freq_lo) {
582 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000583 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400584 delay = dbs_info->freq_hi_jiffies;
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100585 } else {
586 /* We want all CPUs to do sampling nearly on
587 * same jiffy
588 */
589 delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
590 * dbs_info->rate_mult);
591
592 if (num_online_cpus() > 1)
593 delay -= jiffies % delay;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400594 }
595 } else {
596 __cpufreq_driver_target(dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500597 dbs_info->freq_lo, CPUFREQ_RELATION_H);
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100598 delay = dbs_info->freq_lo_jiffies;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400599 }
Tejun Heo57df5572011-01-26 12:12:50 +0100600 schedule_delayed_work_on(cpu, &dbs_info->work, delay);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700601 mutex_unlock(&dbs_info->timer_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500602}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800604static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400606 /* We want all CPUs to do sampling nearly on same jiffy */
607 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Jocelyn Falempea665df92010-03-11 14:01:11 -0800608
609 if (num_online_cpus() > 1)
610 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700611
David Howellsc4028952006-11-22 14:57:56 +0000612 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Tejun Heo203b42f2012-08-21 13:18:23 -0700613 INIT_DEFERRABLE_WORK(&dbs_info->work, do_dbs_timer);
Tejun Heo57df5572011-01-26 12:12:50 +0100614 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700617static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Mathieu Desnoyersb14893a2009-05-17 10:30:45 -0400619 cancel_delayed_work_sync(&dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Arjan van de Ven19379b12010-05-09 08:26:51 -0700622/*
623 * Not all CPUs want IO time to be accounted as busy; this dependson how
624 * efficient idling at a higher frequency/voltage is.
625 * Pavel Machek says this is not so for various generations of AMD and old
626 * Intel systems.
627 * Mike Chan (androidlcom) calis this is also not true for ARM.
628 * Because of this, whitelist specific known (series) of CPUs by default, and
629 * leave all others up to the user.
630 */
631static int should_io_be_busy(void)
632{
633#if defined(CONFIG_X86)
634 /*
635 * For Intel, Core 2 (model 15) andl later have an efficient idle.
636 */
637 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
638 boot_cpu_data.x86 == 6 &&
639 boot_cpu_data.x86_model >= 15)
640 return 1;
641#endif
642 return 0;
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
646 unsigned int event)
647{
648 unsigned int cpu = policy->cpu;
649 struct cpu_dbs_info_s *this_dbs_info;
650 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700651 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Tejun Heo245b2e72009-06-24 15:13:48 +0900653 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 switch (event) {
656 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700657 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EINVAL;
659
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800660 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700661
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700662 dbs_enable++;
Rusty Russell835481d2009-01-04 05:18:06 -0800663 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct cpu_dbs_info_s *j_dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900665 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500667
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700668 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
669 &j_dbs_info->prev_cpu_wall);
Glauber Costa3292beb2011-11-28 14:45:17 -0200670 if (dbs_tuners_ins.ignore_nice)
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500671 j_dbs_info->prev_cpu_nice =
Glauber Costa3292beb2011-11-28 14:45:17 -0200672 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800674 this_dbs_info->cpu = cpu;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400675 this_dbs_info->rate_mult = 1;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700676 ondemand_powersave_bias_init_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * Start the timerschedule work, when this governor
679 * is used for first time
680 */
681 if (dbs_enable == 1) {
682 unsigned int latency;
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200683
684 rc = sysfs_create_group(cpufreq_global_kobject,
685 &dbs_attr_group);
686 if (rc) {
687 mutex_unlock(&dbs_mutex);
688 return rc;
689 }
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700692 latency = policy->cpuinfo.transition_latency / 1000;
693 if (latency == 0)
694 latency = 1;
Thomas Renningercef96152009-04-22 13:48:29 +0200695 /* Bring kernel and HW constraints together */
696 min_sampling_rate = max(min_sampling_rate,
697 MIN_LATENCY_MULTIPLIER * latency);
698 dbs_tuners_ins.sampling_rate =
699 max(min_sampling_rate,
700 latency * LATENCY_MULTIPLIER);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700701 dbs_tuners_ins.io_is_busy = should_io_be_busy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800703 mutex_unlock(&dbs_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700704
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200705 mutex_init(&this_dbs_info->timer_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700706 dbs_timer_init(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708
709 case CPUFREQ_GOV_STOP:
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700710 dbs_timer_exit(this_dbs_info);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700711
712 mutex_lock(&dbs_mutex);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700713 mutex_destroy(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 dbs_enable--;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800715 mutex_unlock(&dbs_mutex);
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200716 if (!dbs_enable)
717 sysfs_remove_group(cpufreq_global_kobject,
718 &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 break;
721
722 case CPUFREQ_GOV_LIMITS:
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700723 mutex_lock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700725 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500726 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700728 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500729 policy->min, CPUFREQ_RELATION_L);
Michal Peciof2636512012-09-14 21:07:39 +0200730 dbs_check_cpu(this_dbs_info);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700731 mutex_unlock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 }
734 return 0;
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static int __init cpufreq_gov_dbs_init(void)
738{
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000739 u64 idle_time;
740 int cpu = get_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700741
Kamalesh Babulal21f2e3c2011-12-09 16:18:42 +0530742 idle_time = get_cpu_idle_time_us(cpu, NULL);
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000743 put_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700744 if (idle_time != -1ULL) {
745 /* Idle micro accounting is supported. Use finer thresholds */
746 dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
747 dbs_tuners_ins.down_differential =
748 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
Thomas Renningercef96152009-04-22 13:48:29 +0200749 /*
Paul Bollebd74b322011-08-06 14:33:43 +0200750 * In nohz/micro accounting case we set the minimum frequency
Thomas Renningercef96152009-04-22 13:48:29 +0200751 * not depending on HZ, but fixed (very low). The deferred
752 * timer might skip some samples if idle/sleeping as needed.
753 */
754 min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
755 } else {
756 /* For correct statistics, we need 10 ticks for each measure */
757 min_sampling_rate =
758 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700759 }
Akinobu Mita888a7942008-07-14 12:00:45 +0900760
Tejun Heo57df5572011-01-26 12:12:50 +0100761 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764static void __exit cpufreq_gov_dbs_exit(void)
765{
Thomas Renninger1c256242007-10-02 13:28:12 -0700766 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700770MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
771MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
772MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -0500773 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700774MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Johannes Weiner69157192008-01-17 15:21:08 -0800776#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
777fs_initcall(cpufreq_gov_dbs_init);
778#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800780#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781module_exit(cpufreq_gov_dbs_exit);