blob: 4079850c50072f4c5b34c90c66fa864a4fd4e5d6 [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include <linux/input.h>
26#include <linux/workqueue.h>
27#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * dbs is used in this file as a shortform for demandbased switching
31 * It helps to keep variable names smaller, simpler
32 */
33
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -070034#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define DEF_FREQUENCY_UP_THRESHOLD (80)
David C Niemi3f78a9f2010-10-06 16:54:24 -040036#define DEF_SAMPLING_DOWN_FACTOR (1)
37#define MAX_SAMPLING_DOWN_FACTOR (100000)
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070038#define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
39#define MICRO_FREQUENCY_UP_THRESHOLD (95)
Thomas Renningercef96152009-04-22 13:48:29 +020040#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
Dave Jonesc29f1402005-05-31 19:03:50 -070041#define MIN_FREQUENCY_UP_THRESHOLD (11)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define MAX_FREQUENCY_UP_THRESHOLD (100)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043#define MIN_FREQUENCY_DOWN_DIFFERENTIAL (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Dave Jones32ee8c32006-02-28 00:43:23 -050045/*
46 * The polling frequency of this governor depends on the capability of
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * the processor. Default polling frequency is 1000 times the transition
Dave Jones32ee8c32006-02-28 00:43:23 -050048 * latency of the processor. The governor will work on any processor with
49 * transition latency <= 10mS, using appropriate sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * rate.
51 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
52 * this governor will not work.
53 * All times here are in uS.
54 */
Dave Jonesdf8b59b2005-09-20 12:39:35 -070055#define MIN_SAMPLING_RATE_RATIO (2)
Thomas Renninger112124a2009-02-04 11:55:12 +010056
Thomas Renningercef96152009-04-22 13:48:29 +020057static unsigned int min_sampling_rate;
58
Thomas Renninger112124a2009-02-04 11:55:12 +010059#define LATENCY_MULTIPLIER (1000)
Thomas Renningercef96152009-04-22 13:48:29 +020060#define MIN_LATENCY_MULTIPLIER (100)
Thomas Renninger1c256242007-10-02 13:28:12 -070061#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
David Howellsc4028952006-11-22 14:57:56 +000063static void do_dbs_timer(struct work_struct *work);
Thomas Renninger0e625ac2009-07-24 15:25:06 +020064static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
65 unsigned int event);
66
67#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
68static
69#endif
70struct cpufreq_governor cpufreq_gov_ondemand = {
71 .name = "ondemand",
72 .governor = cpufreq_governor_dbs,
73 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
74 .owner = THIS_MODULE,
75};
David Howellsc4028952006-11-22 14:57:56 +000076
77/* Sampling types */
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080078enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80struct cpu_dbs_info_s {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070081 cputime64_t prev_cpu_idle;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070082 cputime64_t prev_cpu_iowait;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070083 cputime64_t prev_cpu_wall;
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070084 cputime64_t prev_cpu_nice;
Dave Jones32ee8c32006-02-28 00:43:23 -050085 struct cpufreq_policy *cur_policy;
Dave Jones2b03f892009-01-18 01:43:44 -050086 struct delayed_work work;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040087 struct cpufreq_frequency_table *freq_table;
88 unsigned int freq_lo;
89 unsigned int freq_lo_jiffies;
90 unsigned int freq_hi_jiffies;
David C Niemi3f78a9f2010-10-06 16:54:24 -040091 unsigned int rate_mult;
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080092 int cpu;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -070093 unsigned int sample_type:1;
94 /*
95 * percpu mutex that serializes governor limit change with
96 * do_dbs_timer invocation. We do not want do_dbs_timer to run
97 * when user is changing the governor or limits.
98 */
99 struct mutex timer_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
Tejun Heo245b2e72009-06-24 15:13:48 +0900101static DEFINE_PER_CPU(struct cpu_dbs_info_s, od_cpu_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103static unsigned int dbs_enable; /* number of CPUs using this policy */
104
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700105/*
Thomas Renninger326c86d2011-03-03 21:31:27 +0100106 * dbs_mutex protects dbs_enable in governor start/stop.
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700107 */
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700108static DEFINE_MUTEX(dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110static struct workqueue_struct *input_wq;
111
112static DEFINE_PER_CPU(struct work_struct, dbs_refresh_work);
113
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400114static struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -0500115 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -0500116 unsigned int up_threshold;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700117 unsigned int down_differential;
Dave Jones32ee8c32006-02-28 00:43:23 -0500118 unsigned int ignore_nice;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400119 unsigned int sampling_down_factor;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400120 unsigned int powersave_bias;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700121 unsigned int io_is_busy;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400122} dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -0500123 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400124 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700125 .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
Eric Piel9cbad612006-03-10 11:35:27 +0200126 .ignore_nice = 0,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400127 .powersave_bias = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700130static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
131 cputime64_t *wall)
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700132{
Venki Pallipadiea487612007-06-20 14:26:24 -0700133 cputime64_t idle_time;
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700134 cputime64_t cur_wall_time;
Venki Pallipadiea487612007-06-20 14:26:24 -0700135 cputime64_t busy_time;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700136
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700137 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
Venki Pallipadiea487612007-06-20 14:26:24 -0700138 busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
139 kstat_cpu(cpu).cpustat.system);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700140
Venki Pallipadiea487612007-06-20 14:26:24 -0700141 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
142 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
143 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500144 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice);
Venki Pallipadiea487612007-06-20 14:26:24 -0700145
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700146 idle_time = cputime64_sub(cur_wall_time, busy_time);
147 if (wall)
Pallipadi, Venkatesh54c9a352009-11-11 16:50:29 -0800148 *wall = (cputime64_t)jiffies_to_usecs(cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700149
Pallipadi, Venkatesh54c9a352009-11-11 16:50:29 -0800150 return (cputime64_t)jiffies_to_usecs(idle_time);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700151}
152
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700153static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
154{
155 u64 idle_time = get_cpu_idle_time_us(cpu, wall);
156
157 if (idle_time == -1ULL)
158 return get_cpu_idle_time_jiffy(cpu, wall);
159
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700160 return idle_time;
161}
162
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700163static inline cputime64_t get_cpu_iowait_time(unsigned int cpu, cputime64_t *wall)
164{
165 u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
166
167 if (iowait_time == -1ULL)
168 return 0;
169
170 return iowait_time;
171}
172
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400173/*
174 * Find right freq to be set now with powersave_bias on.
175 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
176 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
177 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +0200178static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
179 unsigned int freq_next,
180 unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400181{
182 unsigned int freq_req, freq_reduc, freq_avg;
183 unsigned int freq_hi, freq_lo;
184 unsigned int index = 0;
185 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
Tejun Heo245b2e72009-06-24 15:13:48 +0900186 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
187 policy->cpu);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400188
189 if (!dbs_info->freq_table) {
190 dbs_info->freq_lo = 0;
191 dbs_info->freq_lo_jiffies = 0;
192 return freq_next;
193 }
194
195 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
196 relation, &index);
197 freq_req = dbs_info->freq_table[index].frequency;
198 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
199 freq_avg = freq_req - freq_reduc;
200
201 /* Find freq bounds for freq_avg in freq_table */
202 index = 0;
203 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
204 CPUFREQ_RELATION_H, &index);
205 freq_lo = dbs_info->freq_table[index].frequency;
206 index = 0;
207 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
208 CPUFREQ_RELATION_L, &index);
209 freq_hi = dbs_info->freq_table[index].frequency;
210
211 /* Find out how long we have to be in hi and lo freqs */
212 if (freq_hi == freq_lo) {
213 dbs_info->freq_lo = 0;
214 dbs_info->freq_lo_jiffies = 0;
215 return freq_lo;
216 }
217 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
218 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
219 jiffies_hi += ((freq_hi - freq_lo) / 2);
220 jiffies_hi /= (freq_hi - freq_lo);
221 jiffies_lo = jiffies_total - jiffies_hi;
222 dbs_info->freq_lo = freq_lo;
223 dbs_info->freq_lo_jiffies = jiffies_lo;
224 dbs_info->freq_hi_jiffies = jiffies_hi;
225 return freq_hi;
226}
227
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700228static void ondemand_powersave_bias_init_cpu(int cpu)
229{
Tejun Heo384be2b2009-08-14 14:41:02 +0900230 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700231 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
232 dbs_info->freq_lo = 0;
233}
234
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400235static void ondemand_powersave_bias_init(void)
236{
237 int i;
238 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700239 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400240 }
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/************************** sysfs interface ************************/
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200244
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200245static ssize_t show_sampling_rate_min(struct kobject *kobj,
246 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Thomas Renningercef96152009-04-22 13:48:29 +0200248 return sprintf(buf, "%u\n", min_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200251define_one_global_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/* cpufreq_ondemand Governor Tunables */
254#define show_one(file_name, object) \
255static ssize_t show_##file_name \
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200256(struct kobject *kobj, struct attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{ \
258 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
259}
260show_one(sampling_rate, sampling_rate);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700261show_one(io_is_busy, io_is_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262show_one(up_threshold, up_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263show_one(down_differential, down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400264show_one(sampling_down_factor, sampling_down_factor);
Alexander Clouter001893c2005-12-01 01:09:25 -0800265show_one(ignore_nice_load, ignore_nice);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400266show_one(powersave_bias, powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200268static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
269 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 unsigned int input;
272 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700273 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700274 if (ret != 1)
275 return -EINVAL;
Thomas Renningercef96152009-04-22 13:48:29 +0200276 dbs_tuners_ins.sampling_rate = max(input, min_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return count;
278}
279
Arjan van de Ven19379b12010-05-09 08:26:51 -0700280static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
281 const char *buf, size_t count)
282{
283 unsigned int input;
284 int ret;
285
286 ret = sscanf(buf, "%u", &input);
287 if (ret != 1)
288 return -EINVAL;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700289 dbs_tuners_ins.io_is_busy = !!input;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700290 return count;
291}
292
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200293static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
294 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 unsigned int input;
297 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700298 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Dave Jones32ee8c32006-02-28 00:43:23 -0500300 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700301 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -EINVAL;
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dbs_tuners_ins.up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return count;
306}
307
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308static ssize_t store_down_differential(struct kobject *a, struct attribute *b,
309 const char *buf, size_t count)
310{
311 unsigned int input;
312 int ret;
313 ret = sscanf(buf, "%u", &input);
314
315 if (ret != 1 || input >= dbs_tuners_ins.up_threshold ||
316 input < MIN_FREQUENCY_DOWN_DIFFERENTIAL) {
317 return -EINVAL;
318 }
319
320 dbs_tuners_ins.down_differential = input;
321
322 return count;
323}
324
David C Niemi3f78a9f2010-10-06 16:54:24 -0400325static ssize_t store_sampling_down_factor(struct kobject *a,
326 struct attribute *b, const char *buf, size_t count)
327{
328 unsigned int input, j;
329 int ret;
330 ret = sscanf(buf, "%u", &input);
331
332 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
333 return -EINVAL;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400334 dbs_tuners_ins.sampling_down_factor = input;
335
336 /* Reset down sampling multiplier in case it was active */
337 for_each_online_cpu(j) {
338 struct cpu_dbs_info_s *dbs_info;
339 dbs_info = &per_cpu(od_cpu_dbs_info, j);
340 dbs_info->rate_mult = 1;
341 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400342 return count;
343}
344
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200345static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
346 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700347{
348 unsigned int input;
349 int ret;
350
351 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500352
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700353 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500354 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700355 return -EINVAL;
356
Dave Jones2b03f892009-01-18 01:43:44 -0500357 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700358 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500359
Dave Jones2b03f892009-01-18 01:43:44 -0500360 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700361 return count;
362 }
363 dbs_tuners_ins.ignore_nice = input;
364
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700365 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700366 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700367 struct cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900368 dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700369 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
370 &dbs_info->prev_cpu_wall);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500371 if (dbs_tuners_ins.ignore_nice)
372 dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
373
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700374 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700375 return count;
376}
377
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200378static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
379 const char *buf, size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400380{
381 unsigned int input;
382 int ret;
383 ret = sscanf(buf, "%u", &input);
384
385 if (ret != 1)
386 return -EINVAL;
387
388 if (input > 1000)
389 input = 1000;
390
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400391 dbs_tuners_ins.powersave_bias = input;
392 ondemand_powersave_bias_init();
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400393 return count;
394}
395
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200396define_one_global_rw(sampling_rate);
Linus Torvalds07d77752010-05-18 08:49:13 -0700397define_one_global_rw(io_is_busy);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200398define_one_global_rw(up_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399define_one_global_rw(down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400400define_one_global_rw(sampling_down_factor);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200401define_one_global_rw(ignore_nice_load);
402define_one_global_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Dave Jones2b03f892009-01-18 01:43:44 -0500404static struct attribute *dbs_attributes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 &sampling_rate_min.attr,
406 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 &up_threshold.attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 &down_differential.attr,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400409 &sampling_down_factor.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800410 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400411 &powersave_bias.attr,
Arjan van de Ven19379b12010-05-09 08:26:51 -0700412 &io_is_busy.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 NULL
414};
415
416static struct attribute_group dbs_attr_group = {
417 .attrs = dbs_attributes,
418 .name = "ondemand",
419};
420
421/************************** sysfs end ************************/
422
Mike Chan00e299f2010-01-26 17:06:47 -0800423static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
424{
425 if (dbs_tuners_ins.powersave_bias)
426 freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
427 else if (p->cur == p->max)
428 return;
429
430 __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
431 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
432}
433
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700434static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700436 unsigned int max_load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 struct cpufreq_policy *policy;
439 unsigned int j;
440
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400441 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 policy = this_dbs_info->cur_policy;
Venki Pallipadiea487612007-06-20 14:26:24 -0700443
Dave Jones32ee8c32006-02-28 00:43:23 -0500444 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700445 * Every sampling_rate, we check, if current idle time is less
446 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700447 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700448 * frequency which can sustain the load while keeping idle time over
449 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500451 * Any frequency increase takes it to the maximum frequency.
452 * Frequency reduction happens at minimum steps of
453 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 */
455
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700456 /* Get Absolute Load - in terms of freq */
457 max_load_freq = 0;
458
Rusty Russell835481d2009-01-04 05:18:06 -0800459 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct cpu_dbs_info_s *j_dbs_info;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700461 cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
462 unsigned int idle_time, wall_time, iowait_time;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700463 unsigned int load, load_freq;
464 int freq_avg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Tejun Heo245b2e72009-06-24 15:13:48 +0900466 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700467
468 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700469 cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700470
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700471 wall_time = (unsigned int) cputime64_sub(cur_wall_time,
472 j_dbs_info->prev_cpu_wall);
473 j_dbs_info->prev_cpu_wall = cur_wall_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700475 idle_time = (unsigned int) cputime64_sub(cur_idle_time,
476 j_dbs_info->prev_cpu_idle);
477 j_dbs_info->prev_cpu_idle = cur_idle_time;
478
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700479 iowait_time = (unsigned int) cputime64_sub(cur_iowait_time,
480 j_dbs_info->prev_cpu_iowait);
481 j_dbs_info->prev_cpu_iowait = cur_iowait_time;
482
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500483 if (dbs_tuners_ins.ignore_nice) {
484 cputime64_t cur_nice;
485 unsigned long cur_nice_jiffies;
486
487 cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
488 j_dbs_info->prev_cpu_nice);
489 /*
490 * Assumption: nice time between sampling periods will
491 * be less than 2^32 jiffies for 32 bit sys
492 */
493 cur_nice_jiffies = (unsigned long)
494 cputime64_to_jiffies64(cur_nice);
495
496 j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
497 idle_time += jiffies_to_usecs(cur_nice_jiffies);
498 }
499
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700500 /*
501 * For the purpose of ondemand, waiting for disk IO is an
502 * indication that you're performance critical, and not that
503 * the system is actually idle. So subtract the iowait time
504 * from the cpu idle time.
505 */
506
Arjan van de Ven19379b12010-05-09 08:26:51 -0700507 if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700508 idle_time -= iowait_time;
509
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700510 if (unlikely(!wall_time || wall_time < idle_time))
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700511 continue;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700512
513 load = 100 * (wall_time - idle_time) / wall_time;
514
515 freq_avg = __cpufreq_driver_getavg(policy, j);
516 if (freq_avg <= 0)
517 freq_avg = policy->cur;
518
519 load_freq = load * freq_avg;
520 if (load_freq > max_load_freq)
521 max_load_freq = load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700524 /* Check for frequency increase */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700525 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
David C Niemi3f78a9f2010-10-06 16:54:24 -0400526 /* If switching to max speed, apply sampling_down_factor */
527 if (policy->cur < policy->max)
528 this_dbs_info->rate_mult =
529 dbs_tuners_ins.sampling_down_factor;
Mike Chan00e299f2010-01-26 17:06:47 -0800530 dbs_freq_increase(policy, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return;
532 }
533
534 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700535 /* if we cannot reduce the frequency anymore, break out early */
536 if (policy->cur == policy->min)
537 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dave Jonesc29f1402005-05-31 19:03:50 -0700539 /*
540 * The optimal frequency is the frequency that is the lowest that
541 * can support the current CPU usage without triggering the up
542 * policy. To be safe, we focus 10 points under the threshold.
543 */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700544 if (max_load_freq <
545 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
546 policy->cur) {
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700547 unsigned int freq_next;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700548 freq_next = max_load_freq /
549 (dbs_tuners_ins.up_threshold -
550 dbs_tuners_ins.down_differential);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700551
David C Niemi3f78a9f2010-10-06 16:54:24 -0400552 /* No longer fully busy, reset rate_mult */
553 this_dbs_info->rate_mult = 1;
554
Nagananda.Chumbalkar@hp.com1dbf5882009-12-21 23:40:52 +0100555 if (freq_next < policy->min)
556 freq_next = policy->min;
557
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400558 if (!dbs_tuners_ins.powersave_bias) {
559 __cpufreq_driver_target(policy, freq_next,
560 CPUFREQ_RELATION_L);
561 } else {
562 int freq = powersave_bias_target(policy, freq_next,
563 CPUFREQ_RELATION_L);
564 __cpufreq_driver_target(policy, freq,
565 CPUFREQ_RELATION_L);
566 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
David Howellsc4028952006-11-22 14:57:56 +0000570static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500571{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800572 struct cpu_dbs_info_s *dbs_info =
573 container_of(work, struct cpu_dbs_info_s, work.work);
574 unsigned int cpu = dbs_info->cpu;
575 int sample_type = dbs_info->sample_type;
576
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100577 int delay;
Jocelyn Falempea665df92010-03-11 14:01:11 -0800578
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700579 mutex_lock(&dbs_info->timer_mutex);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800580
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400581 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000582 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400583 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000584 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400585 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400586 if (dbs_info->freq_lo) {
587 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000588 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400589 delay = dbs_info->freq_hi_jiffies;
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100590 } else {
591 /* We want all CPUs to do sampling nearly on
592 * same jiffy
593 */
594 delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
595 * dbs_info->rate_mult);
596
597 if (num_online_cpus() > 1)
598 delay -= jiffies % delay;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400599 }
600 } else {
601 __cpufreq_driver_target(dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500602 dbs_info->freq_lo, CPUFREQ_RELATION_H);
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100603 delay = dbs_info->freq_lo_jiffies;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400604 }
Tejun Heo57df5572011-01-26 12:12:50 +0100605 schedule_delayed_work_on(cpu, &dbs_info->work, delay);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700606 mutex_unlock(&dbs_info->timer_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500607}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800609static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400611 /* We want all CPUs to do sampling nearly on same jiffy */
612 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Jocelyn Falempea665df92010-03-11 14:01:11 -0800613
614 if (num_online_cpus() > 1)
615 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700616
David Howellsc4028952006-11-22 14:57:56 +0000617 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Venki Pallipadi28287032007-05-08 00:27:47 -0700618 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
Tejun Heo57df5572011-01-26 12:12:50 +0100619 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700622static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Mathieu Desnoyersb14893a2009-05-17 10:30:45 -0400624 cancel_delayed_work_sync(&dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Arjan van de Ven19379b12010-05-09 08:26:51 -0700627/*
628 * Not all CPUs want IO time to be accounted as busy; this dependson how
629 * efficient idling at a higher frequency/voltage is.
630 * Pavel Machek says this is not so for various generations of AMD and old
631 * Intel systems.
632 * Mike Chan (androidlcom) calis this is also not true for ARM.
633 * Because of this, whitelist specific known (series) of CPUs by default, and
634 * leave all others up to the user.
635 */
636static int should_io_be_busy(void)
637{
638#if defined(CONFIG_X86)
639 /*
640 * For Intel, Core 2 (model 15) andl later have an efficient idle.
641 */
642 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
643 boot_cpu_data.x86 == 6 &&
644 boot_cpu_data.x86_model >= 15)
645 return 1;
646#endif
647 return 0;
648}
649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650static void dbs_refresh_callback(struct work_struct *unused)
651{
652 struct cpufreq_policy *policy;
653 struct cpu_dbs_info_s *this_dbs_info;
654 unsigned int cpu = smp_processor_id();
655
656 if (lock_policy_rwsem_write(cpu) < 0)
657 return;
658
659 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
660 policy = this_dbs_info->cur_policy;
David Ng4a0a0232011-08-03 14:04:43 -0700661 if (!policy) {
662 /* CPU not using ondemand governor */
663 unlock_policy_rwsem_write(cpu);
664 return;
665 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666
667 if (policy->cur < policy->max) {
668 policy->cur = policy->max;
669
670 __cpufreq_driver_target(policy, policy->max,
671 CPUFREQ_RELATION_L);
672 this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu,
673 &this_dbs_info->prev_cpu_wall);
674 }
675 unlock_policy_rwsem_write(cpu);
676}
677
678static void dbs_input_event(struct input_handle *handle, unsigned int type,
679 unsigned int code, int value)
680{
681 int i;
682
683 for_each_online_cpu(i) {
684 queue_work_on(i, input_wq, &per_cpu(dbs_refresh_work, i));
685 }
686}
687
688static int dbs_input_connect(struct input_handler *handler,
689 struct input_dev *dev, const struct input_device_id *id)
690{
691 struct input_handle *handle;
692 int error;
693
694 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
695 if (!handle)
696 return -ENOMEM;
697
698 handle->dev = dev;
699 handle->handler = handler;
700 handle->name = "cpufreq";
701
702 error = input_register_handle(handle);
703 if (error)
704 goto err2;
705
706 error = input_open_device(handle);
707 if (error)
708 goto err1;
709
710 return 0;
711err1:
712 input_unregister_handle(handle);
713err2:
714 kfree(handle);
715 return error;
716}
717
718static void dbs_input_disconnect(struct input_handle *handle)
719{
720 input_close_device(handle);
721 input_unregister_handle(handle);
722 kfree(handle);
723}
724
725static const struct input_device_id dbs_ids[] = {
726 { .driver_info = 1 },
727 { },
728};
729
730static struct input_handler dbs_input_handler = {
731 .event = dbs_input_event,
732 .connect = dbs_input_connect,
733 .disconnect = dbs_input_disconnect,
734 .name = "cpufreq_ond",
735 .id_table = dbs_ids,
736};
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
739 unsigned int event)
740{
741 unsigned int cpu = policy->cpu;
742 struct cpu_dbs_info_s *this_dbs_info;
743 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700744 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Tejun Heo245b2e72009-06-24 15:13:48 +0900746 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 switch (event) {
749 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700750 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return -EINVAL;
752
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800753 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700754
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700755 dbs_enable++;
Rusty Russell835481d2009-01-04 05:18:06 -0800756 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct cpu_dbs_info_s *j_dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900758 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500760
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700761 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
762 &j_dbs_info->prev_cpu_wall);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500763 if (dbs_tuners_ins.ignore_nice) {
764 j_dbs_info->prev_cpu_nice =
765 kstat_cpu(j).cpustat.nice;
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800768 this_dbs_info->cpu = cpu;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400769 this_dbs_info->rate_mult = 1;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700770 ondemand_powersave_bias_init_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /*
772 * Start the timerschedule work, when this governor
773 * is used for first time
774 */
775 if (dbs_enable == 1) {
776 unsigned int latency;
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200777
778 rc = sysfs_create_group(cpufreq_global_kobject,
779 &dbs_attr_group);
780 if (rc) {
781 mutex_unlock(&dbs_mutex);
782 return rc;
783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700786 latency = policy->cpuinfo.transition_latency / 1000;
787 if (latency == 0)
788 latency = 1;
Thomas Renningercef96152009-04-22 13:48:29 +0200789 /* Bring kernel and HW constraints together */
790 min_sampling_rate = max(min_sampling_rate,
791 MIN_LATENCY_MULTIPLIER * latency);
792 dbs_tuners_ins.sampling_rate =
793 max(min_sampling_rate,
794 latency * LATENCY_MULTIPLIER);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700795 dbs_tuners_ins.io_is_busy = should_io_be_busy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 if (!cpu)
798 rc = input_register_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800799 mutex_unlock(&dbs_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700800
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200801 mutex_init(&this_dbs_info->timer_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700802 dbs_timer_init(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 break;
804
805 case CPUFREQ_GOV_STOP:
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700806 dbs_timer_exit(this_dbs_info);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700807
808 mutex_lock(&dbs_mutex);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700809 mutex_destroy(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 dbs_enable--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 if (!cpu)
812 input_unregister_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800813 mutex_unlock(&dbs_mutex);
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200814 if (!dbs_enable)
815 sysfs_remove_group(cpufreq_global_kobject,
816 &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 break;
819
820 case CPUFREQ_GOV_LIMITS:
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700821 mutex_lock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700823 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500824 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700826 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500827 policy->min, CPUFREQ_RELATION_L);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700828 mutex_unlock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 break;
830 }
831 return 0;
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834static int __init cpufreq_gov_dbs_init(void)
835{
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700836 cputime64_t wall;
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000837 u64 idle_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 unsigned int i;
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000839 int cpu = get_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700840
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000841 idle_time = get_cpu_idle_time_us(cpu, &wall);
842 put_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700843 if (idle_time != -1ULL) {
844 /* Idle micro accounting is supported. Use finer thresholds */
845 dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
846 dbs_tuners_ins.down_differential =
847 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
Thomas Renningercef96152009-04-22 13:48:29 +0200848 /*
849 * In no_hz/micro accounting case we set the minimum frequency
850 * not depending on HZ, but fixed (very low). The deferred
851 * timer might skip some samples if idle/sleeping as needed.
852 */
853 min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
854 } else {
855 /* For correct statistics, we need 10 ticks for each measure */
856 min_sampling_rate =
857 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700858 }
Akinobu Mita888a7942008-07-14 12:00:45 +0900859
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 input_wq = create_workqueue("iewq");
861 if (!input_wq) {
862 printk(KERN_ERR "Failed to create iewq workqueue\n");
863 return -EFAULT;
864 }
865 for_each_possible_cpu(i) {
866 INIT_WORK(&per_cpu(dbs_refresh_work, i), dbs_refresh_callback);
867 }
868
Tejun Heo57df5572011-01-26 12:12:50 +0100869 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872static void __exit cpufreq_gov_dbs_exit(void)
873{
Thomas Renninger1c256242007-10-02 13:28:12 -0700874 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875 destroy_workqueue(input_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700879MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
880MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
881MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -0500882 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700883MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Johannes Weiner69157192008-01-17 15:21:08 -0800885#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
886fs_initcall(cpufreq_gov_dbs_init);
887#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800889#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890module_exit(cpufreq_gov_dbs_exit);