blob: 6aed95cdf43c9805f106f737b3137f2dbeb5e54d [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 Ng8192a2f2012-01-19 14:16:19 -080063#define POWERSAVE_BIAS_MAXLEVEL (1000)
64#define POWERSAVE_BIAS_MINLEVEL (-1000)
65
David Howellsc4028952006-11-22 14:57:56 +000066static void do_dbs_timer(struct work_struct *work);
Thomas Renninger0e625ac2009-07-24 15:25:06 +020067static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
68 unsigned int event);
69
70#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
71static
72#endif
73struct cpufreq_governor cpufreq_gov_ondemand = {
74 .name = "ondemand",
75 .governor = cpufreq_governor_dbs,
76 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
77 .owner = THIS_MODULE,
78};
David Howellsc4028952006-11-22 14:57:56 +000079
80/* Sampling types */
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080081enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83struct cpu_dbs_info_s {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070084 cputime64_t prev_cpu_idle;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070085 cputime64_t prev_cpu_iowait;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070086 cputime64_t prev_cpu_wall;
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070087 cputime64_t prev_cpu_nice;
Dave Jones32ee8c32006-02-28 00:43:23 -050088 struct cpufreq_policy *cur_policy;
Dave Jones2b03f892009-01-18 01:43:44 -050089 struct delayed_work work;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040090 struct cpufreq_frequency_table *freq_table;
91 unsigned int freq_lo;
92 unsigned int freq_lo_jiffies;
93 unsigned int freq_hi_jiffies;
David C Niemi3f78a9f2010-10-06 16:54:24 -040094 unsigned int rate_mult;
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080095 int cpu;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -070096 unsigned int sample_type:1;
97 /*
98 * percpu mutex that serializes governor limit change with
99 * do_dbs_timer invocation. We do not want do_dbs_timer to run
100 * when user is changing the governor or limits.
101 */
102 struct mutex timer_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
Tejun Heo245b2e72009-06-24 15:13:48 +0900104static DEFINE_PER_CPU(struct cpu_dbs_info_s, od_cpu_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
David Ng8192a2f2012-01-19 14:16:19 -0800106static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info);
107static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static unsigned int dbs_enable; /* number of CPUs using this policy */
110
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700111/*
Thomas Renninger326c86d2011-03-03 21:31:27 +0100112 * dbs_mutex protects dbs_enable in governor start/stop.
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700113 */
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700114static DEFINE_MUTEX(dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116static struct workqueue_struct *input_wq;
117
118static DEFINE_PER_CPU(struct work_struct, dbs_refresh_work);
119
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400120static struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -0500121 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -0500122 unsigned int up_threshold;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700123 unsigned int down_differential;
Dave Jones32ee8c32006-02-28 00:43:23 -0500124 unsigned int ignore_nice;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400125 unsigned int sampling_down_factor;
David Ng8192a2f2012-01-19 14:16:19 -0800126 int powersave_bias;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700127 unsigned int io_is_busy;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400128} dbs_tuners_ins = {
Dave Jones32ee8c32006-02-28 00:43:23 -0500129 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400130 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700131 .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
Eric Piel9cbad612006-03-10 11:35:27 +0200132 .ignore_nice = 0,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400133 .powersave_bias = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700136static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
137 cputime64_t *wall)
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700138{
Venki Pallipadiea487612007-06-20 14:26:24 -0700139 cputime64_t idle_time;
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700140 cputime64_t cur_wall_time;
Venki Pallipadiea487612007-06-20 14:26:24 -0700141 cputime64_t busy_time;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700142
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700143 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
Venki Pallipadiea487612007-06-20 14:26:24 -0700144 busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
145 kstat_cpu(cpu).cpustat.system);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700146
Venki Pallipadiea487612007-06-20 14:26:24 -0700147 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
148 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
149 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500150 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice);
Venki Pallipadiea487612007-06-20 14:26:24 -0700151
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700152 idle_time = cputime64_sub(cur_wall_time, busy_time);
153 if (wall)
Pallipadi, Venkatesh54c9a352009-11-11 16:50:29 -0800154 *wall = (cputime64_t)jiffies_to_usecs(cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700155
Pallipadi, Venkatesh54c9a352009-11-11 16:50:29 -0800156 return (cputime64_t)jiffies_to_usecs(idle_time);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700157}
158
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700159static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
160{
161 u64 idle_time = get_cpu_idle_time_us(cpu, wall);
162
163 if (idle_time == -1ULL)
164 return get_cpu_idle_time_jiffy(cpu, wall);
165
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700166 return idle_time;
167}
168
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700169static inline cputime64_t get_cpu_iowait_time(unsigned int cpu, cputime64_t *wall)
170{
171 u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
172
173 if (iowait_time == -1ULL)
174 return 0;
175
176 return iowait_time;
177}
178
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400179/*
180 * Find right freq to be set now with powersave_bias on.
181 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
182 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
183 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +0200184static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
185 unsigned int freq_next,
186 unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400187{
David Ng8192a2f2012-01-19 14:16:19 -0800188 unsigned int freq_req, freq_avg;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400189 unsigned int freq_hi, freq_lo;
190 unsigned int index = 0;
191 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
David Ng8192a2f2012-01-19 14:16:19 -0800192 int freq_reduc;
Tejun Heo245b2e72009-06-24 15:13:48 +0900193 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
194 policy->cpu);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400195
196 if (!dbs_info->freq_table) {
197 dbs_info->freq_lo = 0;
198 dbs_info->freq_lo_jiffies = 0;
199 return freq_next;
200 }
201
202 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
203 relation, &index);
204 freq_req = dbs_info->freq_table[index].frequency;
205 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
206 freq_avg = freq_req - freq_reduc;
207
208 /* Find freq bounds for freq_avg in freq_table */
209 index = 0;
210 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
211 CPUFREQ_RELATION_H, &index);
212 freq_lo = dbs_info->freq_table[index].frequency;
213 index = 0;
214 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
215 CPUFREQ_RELATION_L, &index);
216 freq_hi = dbs_info->freq_table[index].frequency;
217
218 /* Find out how long we have to be in hi and lo freqs */
219 if (freq_hi == freq_lo) {
220 dbs_info->freq_lo = 0;
221 dbs_info->freq_lo_jiffies = 0;
222 return freq_lo;
223 }
224 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
225 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
226 jiffies_hi += ((freq_hi - freq_lo) / 2);
227 jiffies_hi /= (freq_hi - freq_lo);
228 jiffies_lo = jiffies_total - jiffies_hi;
229 dbs_info->freq_lo = freq_lo;
230 dbs_info->freq_lo_jiffies = jiffies_lo;
231 dbs_info->freq_hi_jiffies = jiffies_hi;
232 return freq_hi;
233}
234
David Ng8192a2f2012-01-19 14:16:19 -0800235static int ondemand_powersave_bias_setspeed(struct cpufreq_policy *policy,
236 struct cpufreq_policy *altpolicy,
237 int level)
238{
239 if (level == POWERSAVE_BIAS_MAXLEVEL) {
240 /* maximum powersave; set to lowest frequency */
241 __cpufreq_driver_target(policy,
242 (altpolicy) ? altpolicy->min : policy->min,
243 CPUFREQ_RELATION_L);
244 return 1;
245 } else if (level == POWERSAVE_BIAS_MINLEVEL) {
246 /* minimum powersave; set to highest frequency */
247 __cpufreq_driver_target(policy,
248 (altpolicy) ? altpolicy->max : policy->max,
249 CPUFREQ_RELATION_H);
250 return 1;
251 }
252 return 0;
253}
254
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700255static void ondemand_powersave_bias_init_cpu(int cpu)
256{
Tejun Heo384be2b2009-08-14 14:41:02 +0900257 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700258 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
259 dbs_info->freq_lo = 0;
260}
261
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400262static void ondemand_powersave_bias_init(void)
263{
264 int i;
265 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700266 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400267 }
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/************************** sysfs interface ************************/
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200271
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200272static ssize_t show_sampling_rate_min(struct kobject *kobj,
273 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Thomas Renningercef96152009-04-22 13:48:29 +0200275 return sprintf(buf, "%u\n", min_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200278define_one_global_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/* cpufreq_ondemand Governor Tunables */
281#define show_one(file_name, object) \
282static ssize_t show_##file_name \
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200283(struct kobject *kobj, struct attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{ \
285 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
286}
287show_one(sampling_rate, sampling_rate);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700288show_one(io_is_busy, io_is_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289show_one(up_threshold, up_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290show_one(down_differential, down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400291show_one(sampling_down_factor, sampling_down_factor);
Alexander Clouter001893c2005-12-01 01:09:25 -0800292show_one(ignore_nice_load, ignore_nice);
David Ng8192a2f2012-01-19 14:16:19 -0800293
294static ssize_t show_powersave_bias
295(struct kobject *kobj, struct attribute *attr, char *buf)
296{
297 return snprintf(buf, PAGE_SIZE, "%d\n", dbs_tuners_ins.powersave_bias);
298}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200300static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
301 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 unsigned int input;
304 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700305 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700306 if (ret != 1)
307 return -EINVAL;
Thomas Renningercef96152009-04-22 13:48:29 +0200308 dbs_tuners_ins.sampling_rate = max(input, min_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return count;
310}
311
Arjan van de Ven19379b12010-05-09 08:26:51 -0700312static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
313 const char *buf, size_t count)
314{
315 unsigned int input;
316 int ret;
317
318 ret = sscanf(buf, "%u", &input);
319 if (ret != 1)
320 return -EINVAL;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700321 dbs_tuners_ins.io_is_busy = !!input;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700322 return count;
323}
324
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200325static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
326 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 unsigned int input;
329 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700330 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dave Jones32ee8c32006-02-28 00:43:23 -0500332 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700333 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -EINVAL;
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dbs_tuners_ins.up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return count;
338}
339
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340static ssize_t store_down_differential(struct kobject *a, struct attribute *b,
341 const char *buf, size_t count)
342{
343 unsigned int input;
344 int ret;
345 ret = sscanf(buf, "%u", &input);
346
347 if (ret != 1 || input >= dbs_tuners_ins.up_threshold ||
348 input < MIN_FREQUENCY_DOWN_DIFFERENTIAL) {
349 return -EINVAL;
350 }
351
352 dbs_tuners_ins.down_differential = input;
353
354 return count;
355}
356
David C Niemi3f78a9f2010-10-06 16:54:24 -0400357static ssize_t store_sampling_down_factor(struct kobject *a,
358 struct attribute *b, const char *buf, size_t count)
359{
360 unsigned int input, j;
361 int ret;
362 ret = sscanf(buf, "%u", &input);
363
364 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
365 return -EINVAL;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400366 dbs_tuners_ins.sampling_down_factor = input;
367
368 /* Reset down sampling multiplier in case it was active */
369 for_each_online_cpu(j) {
370 struct cpu_dbs_info_s *dbs_info;
371 dbs_info = &per_cpu(od_cpu_dbs_info, j);
372 dbs_info->rate_mult = 1;
373 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400374 return count;
375}
376
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200377static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
378 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700379{
380 unsigned int input;
381 int ret;
382
383 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500384
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700385 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500386 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700387 return -EINVAL;
388
Dave Jones2b03f892009-01-18 01:43:44 -0500389 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700390 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500391
Dave Jones2b03f892009-01-18 01:43:44 -0500392 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700393 return count;
394 }
395 dbs_tuners_ins.ignore_nice = input;
396
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700397 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700398 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700399 struct cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900400 dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700401 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
402 &dbs_info->prev_cpu_wall);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500403 if (dbs_tuners_ins.ignore_nice)
404 dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
405
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700406 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700407 return count;
408}
409
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200410static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
411 const char *buf, size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400412{
David Ng8192a2f2012-01-19 14:16:19 -0800413 int input = 0;
414 int bypass = 0;
415 int ret, cpu, reenable_timer;
416 struct cpu_dbs_info_s *dbs_info;
417
418 ret = sscanf(buf, "%d", &input);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400419
420 if (ret != 1)
421 return -EINVAL;
422
David Ng8192a2f2012-01-19 14:16:19 -0800423 if (input >= POWERSAVE_BIAS_MAXLEVEL) {
424 input = POWERSAVE_BIAS_MAXLEVEL;
425 bypass = 1;
426 } else if (input <= POWERSAVE_BIAS_MINLEVEL) {
427 input = POWERSAVE_BIAS_MINLEVEL;
428 bypass = 1;
429 }
430
431 if (input == dbs_tuners_ins.powersave_bias) {
432 /* no change */
433 return count;
434 }
435
436 reenable_timer = ((dbs_tuners_ins.powersave_bias ==
437 POWERSAVE_BIAS_MAXLEVEL) ||
438 (dbs_tuners_ins.powersave_bias ==
439 POWERSAVE_BIAS_MINLEVEL));
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400440
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400441 dbs_tuners_ins.powersave_bias = input;
David Ng8192a2f2012-01-19 14:16:19 -0800442 if (!bypass) {
443 if (reenable_timer) {
444 /* reinstate dbs timer */
445 for_each_online_cpu(cpu) {
446 if (lock_policy_rwsem_write(cpu) < 0)
447 continue;
448
449 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
450 if (dbs_info->cur_policy) {
451 /* restart dbs timer */
452 dbs_timer_init(dbs_info);
453 }
454 unlock_policy_rwsem_write(cpu);
455 }
456 }
457 ondemand_powersave_bias_init();
458 } else {
459 /* running at maximum or minimum frequencies; cancel
460 dbs timer as periodic load sampling is not necessary */
461 for_each_online_cpu(cpu) {
462 if (lock_policy_rwsem_write(cpu) < 0)
463 continue;
464
465 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
466 if (dbs_info->cur_policy) {
467 /* cpu using ondemand, cancel dbs timer */
468 mutex_lock(&dbs_info->timer_mutex);
469 dbs_timer_exit(dbs_info);
470
471 ondemand_powersave_bias_setspeed(
472 dbs_info->cur_policy,
473 NULL,
474 input);
475
476 mutex_unlock(&dbs_info->timer_mutex);
477 }
478 unlock_policy_rwsem_write(cpu);
479 }
480 }
481
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400482 return count;
483}
484
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200485define_one_global_rw(sampling_rate);
Linus Torvalds07d77752010-05-18 08:49:13 -0700486define_one_global_rw(io_is_busy);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200487define_one_global_rw(up_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488define_one_global_rw(down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400489define_one_global_rw(sampling_down_factor);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200490define_one_global_rw(ignore_nice_load);
491define_one_global_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Dave Jones2b03f892009-01-18 01:43:44 -0500493static struct attribute *dbs_attributes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 &sampling_rate_min.attr,
495 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 &up_threshold.attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 &down_differential.attr,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400498 &sampling_down_factor.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800499 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400500 &powersave_bias.attr,
Arjan van de Ven19379b12010-05-09 08:26:51 -0700501 &io_is_busy.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 NULL
503};
504
505static struct attribute_group dbs_attr_group = {
506 .attrs = dbs_attributes,
507 .name = "ondemand",
508};
509
510/************************** sysfs end ************************/
511
Mike Chan00e299f2010-01-26 17:06:47 -0800512static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
513{
514 if (dbs_tuners_ins.powersave_bias)
515 freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
516 else if (p->cur == p->max)
517 return;
518
519 __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
520 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
521}
522
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700523static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700525 unsigned int max_load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 struct cpufreq_policy *policy;
528 unsigned int j;
529
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400530 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 policy = this_dbs_info->cur_policy;
Venki Pallipadiea487612007-06-20 14:26:24 -0700532
Dave Jones32ee8c32006-02-28 00:43:23 -0500533 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700534 * Every sampling_rate, we check, if current idle time is less
535 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700536 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700537 * frequency which can sustain the load while keeping idle time over
538 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500540 * Any frequency increase takes it to the maximum frequency.
541 * Frequency reduction happens at minimum steps of
542 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
544
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700545 /* Get Absolute Load - in terms of freq */
546 max_load_freq = 0;
547
Rusty Russell835481d2009-01-04 05:18:06 -0800548 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct cpu_dbs_info_s *j_dbs_info;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700550 cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
551 unsigned int idle_time, wall_time, iowait_time;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700552 unsigned int load, load_freq;
553 int freq_avg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Tejun Heo245b2e72009-06-24 15:13:48 +0900555 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700556
557 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700558 cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700559
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700560 wall_time = (unsigned int) cputime64_sub(cur_wall_time,
561 j_dbs_info->prev_cpu_wall);
562 j_dbs_info->prev_cpu_wall = cur_wall_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700564 idle_time = (unsigned int) cputime64_sub(cur_idle_time,
565 j_dbs_info->prev_cpu_idle);
566 j_dbs_info->prev_cpu_idle = cur_idle_time;
567
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700568 iowait_time = (unsigned int) cputime64_sub(cur_iowait_time,
569 j_dbs_info->prev_cpu_iowait);
570 j_dbs_info->prev_cpu_iowait = cur_iowait_time;
571
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500572 if (dbs_tuners_ins.ignore_nice) {
573 cputime64_t cur_nice;
574 unsigned long cur_nice_jiffies;
575
576 cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
577 j_dbs_info->prev_cpu_nice);
578 /*
579 * Assumption: nice time between sampling periods will
580 * be less than 2^32 jiffies for 32 bit sys
581 */
582 cur_nice_jiffies = (unsigned long)
583 cputime64_to_jiffies64(cur_nice);
584
585 j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
586 idle_time += jiffies_to_usecs(cur_nice_jiffies);
587 }
588
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700589 /*
590 * For the purpose of ondemand, waiting for disk IO is an
591 * indication that you're performance critical, and not that
592 * the system is actually idle. So subtract the iowait time
593 * from the cpu idle time.
594 */
595
Arjan van de Ven19379b12010-05-09 08:26:51 -0700596 if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700597 idle_time -= iowait_time;
598
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700599 if (unlikely(!wall_time || wall_time < idle_time))
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700600 continue;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700601
602 load = 100 * (wall_time - idle_time) / wall_time;
603
604 freq_avg = __cpufreq_driver_getavg(policy, j);
605 if (freq_avg <= 0)
606 freq_avg = policy->cur;
607
608 load_freq = load * freq_avg;
609 if (load_freq > max_load_freq)
610 max_load_freq = load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700613 /* Check for frequency increase */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700614 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
David C Niemi3f78a9f2010-10-06 16:54:24 -0400615 /* If switching to max speed, apply sampling_down_factor */
616 if (policy->cur < policy->max)
617 this_dbs_info->rate_mult =
618 dbs_tuners_ins.sampling_down_factor;
Mike Chan00e299f2010-01-26 17:06:47 -0800619 dbs_freq_increase(policy, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return;
621 }
622
623 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700624 /* if we cannot reduce the frequency anymore, break out early */
625 if (policy->cur == policy->min)
626 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Dave Jonesc29f1402005-05-31 19:03:50 -0700628 /*
629 * The optimal frequency is the frequency that is the lowest that
630 * can support the current CPU usage without triggering the up
631 * policy. To be safe, we focus 10 points under the threshold.
632 */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700633 if (max_load_freq <
634 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
635 policy->cur) {
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700636 unsigned int freq_next;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700637 freq_next = max_load_freq /
638 (dbs_tuners_ins.up_threshold -
639 dbs_tuners_ins.down_differential);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700640
David C Niemi3f78a9f2010-10-06 16:54:24 -0400641 /* No longer fully busy, reset rate_mult */
642 this_dbs_info->rate_mult = 1;
643
Nagananda.Chumbalkar@hp.com1dbf5882009-12-21 23:40:52 +0100644 if (freq_next < policy->min)
645 freq_next = policy->min;
646
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400647 if (!dbs_tuners_ins.powersave_bias) {
648 __cpufreq_driver_target(policy, freq_next,
649 CPUFREQ_RELATION_L);
650 } else {
651 int freq = powersave_bias_target(policy, freq_next,
652 CPUFREQ_RELATION_L);
653 __cpufreq_driver_target(policy, freq,
654 CPUFREQ_RELATION_L);
655 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
David Howellsc4028952006-11-22 14:57:56 +0000659static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500660{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800661 struct cpu_dbs_info_s *dbs_info =
662 container_of(work, struct cpu_dbs_info_s, work.work);
663 unsigned int cpu = dbs_info->cpu;
664 int sample_type = dbs_info->sample_type;
665
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100666 int delay;
Jocelyn Falempea665df92010-03-11 14:01:11 -0800667
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700668 mutex_lock(&dbs_info->timer_mutex);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800669
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400670 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000671 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400672 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000673 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400674 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400675 if (dbs_info->freq_lo) {
676 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000677 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400678 delay = dbs_info->freq_hi_jiffies;
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100679 } else {
680 /* We want all CPUs to do sampling nearly on
681 * same jiffy
682 */
683 delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
684 * dbs_info->rate_mult);
685
686 if (num_online_cpus() > 1)
687 delay -= jiffies % delay;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400688 }
689 } else {
690 __cpufreq_driver_target(dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500691 dbs_info->freq_lo, CPUFREQ_RELATION_H);
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100692 delay = dbs_info->freq_lo_jiffies;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400693 }
Tejun Heo57df5572011-01-26 12:12:50 +0100694 schedule_delayed_work_on(cpu, &dbs_info->work, delay);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700695 mutex_unlock(&dbs_info->timer_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500696}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800698static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400700 /* We want all CPUs to do sampling nearly on same jiffy */
701 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Jocelyn Falempea665df92010-03-11 14:01:11 -0800702
703 if (num_online_cpus() > 1)
704 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700705
David Howellsc4028952006-11-22 14:57:56 +0000706 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Venki Pallipadi28287032007-05-08 00:27:47 -0700707 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
Tejun Heo57df5572011-01-26 12:12:50 +0100708 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700711static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Mathieu Desnoyersb14893a2009-05-17 10:30:45 -0400713 cancel_delayed_work_sync(&dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Arjan van de Ven19379b12010-05-09 08:26:51 -0700716/*
717 * Not all CPUs want IO time to be accounted as busy; this dependson how
718 * efficient idling at a higher frequency/voltage is.
719 * Pavel Machek says this is not so for various generations of AMD and old
720 * Intel systems.
721 * Mike Chan (androidlcom) calis this is also not true for ARM.
722 * Because of this, whitelist specific known (series) of CPUs by default, and
723 * leave all others up to the user.
724 */
725static int should_io_be_busy(void)
726{
727#if defined(CONFIG_X86)
728 /*
729 * For Intel, Core 2 (model 15) andl later have an efficient idle.
730 */
731 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
732 boot_cpu_data.x86 == 6 &&
733 boot_cpu_data.x86_model >= 15)
734 return 1;
735#endif
736 return 0;
737}
738
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700739static void dbs_refresh_callback(struct work_struct *unused)
740{
741 struct cpufreq_policy *policy;
742 struct cpu_dbs_info_s *this_dbs_info;
743 unsigned int cpu = smp_processor_id();
744
745 if (lock_policy_rwsem_write(cpu) < 0)
746 return;
747
748 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
749 policy = this_dbs_info->cur_policy;
David Ng4a0a0232011-08-03 14:04:43 -0700750 if (!policy) {
751 /* CPU not using ondemand governor */
752 unlock_policy_rwsem_write(cpu);
753 return;
754 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755
756 if (policy->cur < policy->max) {
757 policy->cur = policy->max;
758
759 __cpufreq_driver_target(policy, policy->max,
760 CPUFREQ_RELATION_L);
761 this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu,
762 &this_dbs_info->prev_cpu_wall);
763 }
764 unlock_policy_rwsem_write(cpu);
765}
766
767static void dbs_input_event(struct input_handle *handle, unsigned int type,
768 unsigned int code, int value)
769{
770 int i;
771
David Ng8192a2f2012-01-19 14:16:19 -0800772 if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) ||
773 (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) {
774 /* nothing to do */
775 return;
776 }
777
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 for_each_online_cpu(i) {
779 queue_work_on(i, input_wq, &per_cpu(dbs_refresh_work, i));
780 }
781}
782
783static int dbs_input_connect(struct input_handler *handler,
784 struct input_dev *dev, const struct input_device_id *id)
785{
786 struct input_handle *handle;
787 int error;
788
789 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
790 if (!handle)
791 return -ENOMEM;
792
793 handle->dev = dev;
794 handle->handler = handler;
795 handle->name = "cpufreq";
796
797 error = input_register_handle(handle);
798 if (error)
799 goto err2;
800
801 error = input_open_device(handle);
802 if (error)
803 goto err1;
804
805 return 0;
806err1:
807 input_unregister_handle(handle);
808err2:
809 kfree(handle);
810 return error;
811}
812
813static void dbs_input_disconnect(struct input_handle *handle)
814{
815 input_close_device(handle);
816 input_unregister_handle(handle);
817 kfree(handle);
818}
819
820static const struct input_device_id dbs_ids[] = {
821 { .driver_info = 1 },
822 { },
823};
824
825static struct input_handler dbs_input_handler = {
826 .event = dbs_input_event,
827 .connect = dbs_input_connect,
828 .disconnect = dbs_input_disconnect,
829 .name = "cpufreq_ond",
830 .id_table = dbs_ids,
831};
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
834 unsigned int event)
835{
836 unsigned int cpu = policy->cpu;
837 struct cpu_dbs_info_s *this_dbs_info;
838 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700839 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Tejun Heo245b2e72009-06-24 15:13:48 +0900841 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 switch (event) {
844 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700845 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return -EINVAL;
847
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800848 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700849
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700850 dbs_enable++;
Rusty Russell835481d2009-01-04 05:18:06 -0800851 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct cpu_dbs_info_s *j_dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900853 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500855
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700856 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
857 &j_dbs_info->prev_cpu_wall);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500858 if (dbs_tuners_ins.ignore_nice) {
859 j_dbs_info->prev_cpu_nice =
860 kstat_cpu(j).cpustat.nice;
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800863 this_dbs_info->cpu = cpu;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400864 this_dbs_info->rate_mult = 1;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700865 ondemand_powersave_bias_init_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /*
867 * Start the timerschedule work, when this governor
868 * is used for first time
869 */
870 if (dbs_enable == 1) {
871 unsigned int latency;
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200872
873 rc = sysfs_create_group(cpufreq_global_kobject,
874 &dbs_attr_group);
875 if (rc) {
876 mutex_unlock(&dbs_mutex);
877 return rc;
878 }
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700881 latency = policy->cpuinfo.transition_latency / 1000;
882 if (latency == 0)
883 latency = 1;
Thomas Renningercef96152009-04-22 13:48:29 +0200884 /* Bring kernel and HW constraints together */
885 min_sampling_rate = max(min_sampling_rate,
886 MIN_LATENCY_MULTIPLIER * latency);
887 dbs_tuners_ins.sampling_rate =
888 max(min_sampling_rate,
889 latency * LATENCY_MULTIPLIER);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700890 dbs_tuners_ins.io_is_busy = should_io_be_busy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 if (!cpu)
893 rc = input_register_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800894 mutex_unlock(&dbs_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700895
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200896 mutex_init(&this_dbs_info->timer_mutex);
David Ng8192a2f2012-01-19 14:16:19 -0800897
898 if (!ondemand_powersave_bias_setspeed(
899 this_dbs_info->cur_policy,
900 NULL,
901 dbs_tuners_ins.powersave_bias))
902 dbs_timer_init(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 break;
904
905 case CPUFREQ_GOV_STOP:
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700906 dbs_timer_exit(this_dbs_info);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700907
908 mutex_lock(&dbs_mutex);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700909 mutex_destroy(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 dbs_enable--;
Anitha Anand3dd65092012-01-18 17:17:40 -0800911 /* If device is being removed, policy is no longer
912 * valid. */
913 this_dbs_info->cur_policy = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 if (!cpu)
915 input_unregister_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800916 mutex_unlock(&dbs_mutex);
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200917 if (!dbs_enable)
918 sysfs_remove_group(cpufreq_global_kobject,
919 &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 break;
922
923 case CPUFREQ_GOV_LIMITS:
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700924 mutex_lock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700926 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500927 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700929 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500930 policy->min, CPUFREQ_RELATION_L);
David Ng8192a2f2012-01-19 14:16:19 -0800931 else if (dbs_tuners_ins.powersave_bias != 0)
932 ondemand_powersave_bias_setspeed(
933 this_dbs_info->cur_policy,
934 policy,
935 dbs_tuners_ins.powersave_bias);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700936 mutex_unlock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 break;
938 }
939 return 0;
940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942static int __init cpufreq_gov_dbs_init(void)
943{
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700944 cputime64_t wall;
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000945 u64 idle_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 unsigned int i;
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000947 int cpu = get_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700948
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000949 idle_time = get_cpu_idle_time_us(cpu, &wall);
950 put_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700951 if (idle_time != -1ULL) {
952 /* Idle micro accounting is supported. Use finer thresholds */
953 dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
954 dbs_tuners_ins.down_differential =
955 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
Thomas Renningercef96152009-04-22 13:48:29 +0200956 /*
957 * In no_hz/micro accounting case we set the minimum frequency
958 * not depending on HZ, but fixed (very low). The deferred
959 * timer might skip some samples if idle/sleeping as needed.
960 */
961 min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
962 } else {
963 /* For correct statistics, we need 10 ticks for each measure */
964 min_sampling_rate =
965 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700966 }
Akinobu Mita888a7942008-07-14 12:00:45 +0900967
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968 input_wq = create_workqueue("iewq");
969 if (!input_wq) {
970 printk(KERN_ERR "Failed to create iewq workqueue\n");
971 return -EFAULT;
972 }
973 for_each_possible_cpu(i) {
974 INIT_WORK(&per_cpu(dbs_refresh_work, i), dbs_refresh_callback);
975 }
976
Tejun Heo57df5572011-01-26 12:12:50 +0100977 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
980static void __exit cpufreq_gov_dbs_exit(void)
981{
Thomas Renninger1c256242007-10-02 13:28:12 -0700982 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 destroy_workqueue(input_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700987MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
988MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
989MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -0500990 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700991MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Johannes Weiner69157192008-01-17 15:21:08 -0800993#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
994fs_initcall(cpufreq_gov_dbs_init);
995#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800997#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998module_exit(cpufreq_gov_dbs_exit);