blob: cfc2534a528ca71aed662d1e0f1c58f7b4c9c1e0 [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;
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530415 int ret, cpu, reenable_timer, j;
David Ng8192a2f2012-01-19 14:16:19 -0800416 struct cpu_dbs_info_s *dbs_info;
417
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530418 struct cpumask cpus_timer_done;
419 cpumask_clear(&cpus_timer_done);
420
David Ng8192a2f2012-01-19 14:16:19 -0800421 ret = sscanf(buf, "%d", &input);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400422
423 if (ret != 1)
424 return -EINVAL;
425
David Ng8192a2f2012-01-19 14:16:19 -0800426 if (input >= POWERSAVE_BIAS_MAXLEVEL) {
427 input = POWERSAVE_BIAS_MAXLEVEL;
428 bypass = 1;
429 } else if (input <= POWERSAVE_BIAS_MINLEVEL) {
430 input = POWERSAVE_BIAS_MINLEVEL;
431 bypass = 1;
432 }
433
434 if (input == dbs_tuners_ins.powersave_bias) {
435 /* no change */
436 return count;
437 }
438
439 reenable_timer = ((dbs_tuners_ins.powersave_bias ==
440 POWERSAVE_BIAS_MAXLEVEL) ||
441 (dbs_tuners_ins.powersave_bias ==
442 POWERSAVE_BIAS_MINLEVEL));
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400443
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400444 dbs_tuners_ins.powersave_bias = input;
David Ng8192a2f2012-01-19 14:16:19 -0800445 if (!bypass) {
446 if (reenable_timer) {
447 /* reinstate dbs timer */
448 for_each_online_cpu(cpu) {
449 if (lock_policy_rwsem_write(cpu) < 0)
450 continue;
451
452 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530453
454 for_each_cpu(j, &cpus_timer_done) {
455 if (!dbs_info->cur_policy) {
456 pr_err("Dbs policy is NULL\n");
457 goto skip_this_cpu;
458 }
459 if (cpumask_test_cpu(j, dbs_info->
460 cur_policy->cpus))
461 goto skip_this_cpu;
462 }
463
464 cpumask_set_cpu(cpu, &cpus_timer_done);
David Ng8192a2f2012-01-19 14:16:19 -0800465 if (dbs_info->cur_policy) {
466 /* restart dbs timer */
467 dbs_timer_init(dbs_info);
468 }
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530469skip_this_cpu:
David Ng8192a2f2012-01-19 14:16:19 -0800470 unlock_policy_rwsem_write(cpu);
471 }
472 }
473 ondemand_powersave_bias_init();
474 } else {
475 /* running at maximum or minimum frequencies; cancel
476 dbs timer as periodic load sampling is not necessary */
477 for_each_online_cpu(cpu) {
478 if (lock_policy_rwsem_write(cpu) < 0)
479 continue;
480
481 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530482
483 for_each_cpu(j, &cpus_timer_done) {
484 if (!dbs_info->cur_policy) {
485 pr_err("Dbs policy is NULL\n");
486 goto skip_this_cpu_bypass;
487 }
488 if (cpumask_test_cpu(j, dbs_info->
489 cur_policy->cpus))
490 goto skip_this_cpu_bypass;
491 }
492
493 cpumask_set_cpu(cpu, &cpus_timer_done);
494
David Ng8192a2f2012-01-19 14:16:19 -0800495 if (dbs_info->cur_policy) {
496 /* cpu using ondemand, cancel dbs timer */
497 mutex_lock(&dbs_info->timer_mutex);
498 dbs_timer_exit(dbs_info);
499
500 ondemand_powersave_bias_setspeed(
501 dbs_info->cur_policy,
502 NULL,
503 input);
504
505 mutex_unlock(&dbs_info->timer_mutex);
506 }
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530507skip_this_cpu_bypass:
David Ng8192a2f2012-01-19 14:16:19 -0800508 unlock_policy_rwsem_write(cpu);
509 }
510 }
511
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400512 return count;
513}
514
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200515define_one_global_rw(sampling_rate);
Linus Torvalds07d77752010-05-18 08:49:13 -0700516define_one_global_rw(io_is_busy);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200517define_one_global_rw(up_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518define_one_global_rw(down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400519define_one_global_rw(sampling_down_factor);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200520define_one_global_rw(ignore_nice_load);
521define_one_global_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Dave Jones2b03f892009-01-18 01:43:44 -0500523static struct attribute *dbs_attributes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 &sampling_rate_min.attr,
525 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 &up_threshold.attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 &down_differential.attr,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400528 &sampling_down_factor.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800529 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400530 &powersave_bias.attr,
Arjan van de Ven19379b12010-05-09 08:26:51 -0700531 &io_is_busy.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 NULL
533};
534
535static struct attribute_group dbs_attr_group = {
536 .attrs = dbs_attributes,
537 .name = "ondemand",
538};
539
540/************************** sysfs end ************************/
541
Mike Chan00e299f2010-01-26 17:06:47 -0800542static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
543{
544 if (dbs_tuners_ins.powersave_bias)
545 freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
546 else if (p->cur == p->max)
547 return;
548
549 __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
550 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
551}
552
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700553static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800555 /* Extrapolated load of this CPU */
556 unsigned int load_at_max_freq = 0;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700557 unsigned int max_load_freq;
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800558 /* Current load across this CPU */
559 unsigned int cur_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 struct cpufreq_policy *policy;
562 unsigned int j;
563
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400564 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 policy = this_dbs_info->cur_policy;
Venki Pallipadiea487612007-06-20 14:26:24 -0700566
Dave Jones32ee8c32006-02-28 00:43:23 -0500567 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700568 * Every sampling_rate, we check, if current idle time is less
569 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700570 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700571 * frequency which can sustain the load while keeping idle time over
572 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500574 * Any frequency increase takes it to the maximum frequency.
575 * Frequency reduction happens at minimum steps of
576 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
578
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700579 /* Get Absolute Load - in terms of freq */
580 max_load_freq = 0;
581
Rusty Russell835481d2009-01-04 05:18:06 -0800582 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct cpu_dbs_info_s *j_dbs_info;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700584 cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
585 unsigned int idle_time, wall_time, iowait_time;
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800586 unsigned int load_freq;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700587 int freq_avg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Tejun Heo245b2e72009-06-24 15:13:48 +0900589 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700590
591 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700592 cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700593
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700594 wall_time = (unsigned int) cputime64_sub(cur_wall_time,
595 j_dbs_info->prev_cpu_wall);
596 j_dbs_info->prev_cpu_wall = cur_wall_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700598 idle_time = (unsigned int) cputime64_sub(cur_idle_time,
599 j_dbs_info->prev_cpu_idle);
600 j_dbs_info->prev_cpu_idle = cur_idle_time;
601
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700602 iowait_time = (unsigned int) cputime64_sub(cur_iowait_time,
603 j_dbs_info->prev_cpu_iowait);
604 j_dbs_info->prev_cpu_iowait = cur_iowait_time;
605
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500606 if (dbs_tuners_ins.ignore_nice) {
607 cputime64_t cur_nice;
608 unsigned long cur_nice_jiffies;
609
610 cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
611 j_dbs_info->prev_cpu_nice);
612 /*
613 * Assumption: nice time between sampling periods will
614 * be less than 2^32 jiffies for 32 bit sys
615 */
616 cur_nice_jiffies = (unsigned long)
617 cputime64_to_jiffies64(cur_nice);
618
619 j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
620 idle_time += jiffies_to_usecs(cur_nice_jiffies);
621 }
622
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700623 /*
624 * For the purpose of ondemand, waiting for disk IO is an
625 * indication that you're performance critical, and not that
626 * the system is actually idle. So subtract the iowait time
627 * from the cpu idle time.
628 */
629
Arjan van de Ven19379b12010-05-09 08:26:51 -0700630 if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700631 idle_time -= iowait_time;
632
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700633 if (unlikely(!wall_time || wall_time < idle_time))
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700634 continue;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700635
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800636 cur_load = 100 * (wall_time - idle_time) / wall_time;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700637
638 freq_avg = __cpufreq_driver_getavg(policy, j);
639 if (freq_avg <= 0)
640 freq_avg = policy->cur;
641
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800642 load_freq = cur_load * freq_avg;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700643 if (load_freq > max_load_freq)
644 max_load_freq = load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800646 /* calculate the scaled load across CPU */
647 load_at_max_freq = (cur_load * policy->cur)/policy->cpuinfo.max_freq;
648
649 cpufreq_notify_utilization(policy, load_at_max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700651 /* Check for frequency increase */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700652 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
David C Niemi3f78a9f2010-10-06 16:54:24 -0400653 /* If switching to max speed, apply sampling_down_factor */
654 if (policy->cur < policy->max)
655 this_dbs_info->rate_mult =
656 dbs_tuners_ins.sampling_down_factor;
Mike Chan00e299f2010-01-26 17:06:47 -0800657 dbs_freq_increase(policy, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return;
659 }
660
661 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700662 /* if we cannot reduce the frequency anymore, break out early */
663 if (policy->cur == policy->min)
664 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Dave Jonesc29f1402005-05-31 19:03:50 -0700666 /*
667 * The optimal frequency is the frequency that is the lowest that
668 * can support the current CPU usage without triggering the up
669 * policy. To be safe, we focus 10 points under the threshold.
670 */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700671 if (max_load_freq <
672 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
673 policy->cur) {
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700674 unsigned int freq_next;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700675 freq_next = max_load_freq /
676 (dbs_tuners_ins.up_threshold -
677 dbs_tuners_ins.down_differential);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700678
David C Niemi3f78a9f2010-10-06 16:54:24 -0400679 /* No longer fully busy, reset rate_mult */
680 this_dbs_info->rate_mult = 1;
681
Nagananda.Chumbalkar@hp.com1dbf5882009-12-21 23:40:52 +0100682 if (freq_next < policy->min)
683 freq_next = policy->min;
684
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400685 if (!dbs_tuners_ins.powersave_bias) {
686 __cpufreq_driver_target(policy, freq_next,
687 CPUFREQ_RELATION_L);
688 } else {
689 int freq = powersave_bias_target(policy, freq_next,
690 CPUFREQ_RELATION_L);
691 __cpufreq_driver_target(policy, freq,
692 CPUFREQ_RELATION_L);
693 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
David Howellsc4028952006-11-22 14:57:56 +0000697static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500698{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800699 struct cpu_dbs_info_s *dbs_info =
700 container_of(work, struct cpu_dbs_info_s, work.work);
701 unsigned int cpu = dbs_info->cpu;
702 int sample_type = dbs_info->sample_type;
703
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100704 int delay;
Jocelyn Falempea665df92010-03-11 14:01:11 -0800705
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700706 mutex_lock(&dbs_info->timer_mutex);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800707
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400708 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000709 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400710 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000711 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400712 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400713 if (dbs_info->freq_lo) {
714 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000715 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400716 delay = dbs_info->freq_hi_jiffies;
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100717 } else {
718 /* We want all CPUs to do sampling nearly on
719 * same jiffy
720 */
721 delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
722 * dbs_info->rate_mult);
723
724 if (num_online_cpus() > 1)
725 delay -= jiffies % delay;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400726 }
727 } else {
728 __cpufreq_driver_target(dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500729 dbs_info->freq_lo, CPUFREQ_RELATION_H);
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100730 delay = dbs_info->freq_lo_jiffies;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400731 }
Tejun Heo57df5572011-01-26 12:12:50 +0100732 schedule_delayed_work_on(cpu, &dbs_info->work, delay);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700733 mutex_unlock(&dbs_info->timer_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500734}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800736static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400738 /* We want all CPUs to do sampling nearly on same jiffy */
739 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Jocelyn Falempea665df92010-03-11 14:01:11 -0800740
741 if (num_online_cpus() > 1)
742 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700743
David Howellsc4028952006-11-22 14:57:56 +0000744 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Venki Pallipadi28287032007-05-08 00:27:47 -0700745 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
Tejun Heo57df5572011-01-26 12:12:50 +0100746 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700749static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Mathieu Desnoyersb14893a2009-05-17 10:30:45 -0400751 cancel_delayed_work_sync(&dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Arjan van de Ven19379b12010-05-09 08:26:51 -0700754/*
755 * Not all CPUs want IO time to be accounted as busy; this dependson how
756 * efficient idling at a higher frequency/voltage is.
757 * Pavel Machek says this is not so for various generations of AMD and old
758 * Intel systems.
759 * Mike Chan (androidlcom) calis this is also not true for ARM.
760 * Because of this, whitelist specific known (series) of CPUs by default, and
761 * leave all others up to the user.
762 */
763static int should_io_be_busy(void)
764{
765#if defined(CONFIG_X86)
766 /*
767 * For Intel, Core 2 (model 15) andl later have an efficient idle.
768 */
769 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
770 boot_cpu_data.x86 == 6 &&
771 boot_cpu_data.x86_model >= 15)
772 return 1;
773#endif
774 return 0;
775}
776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777static void dbs_refresh_callback(struct work_struct *unused)
778{
779 struct cpufreq_policy *policy;
780 struct cpu_dbs_info_s *this_dbs_info;
781 unsigned int cpu = smp_processor_id();
782
783 if (lock_policy_rwsem_write(cpu) < 0)
784 return;
785
786 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
787 policy = this_dbs_info->cur_policy;
David Ng4a0a0232011-08-03 14:04:43 -0700788 if (!policy) {
789 /* CPU not using ondemand governor */
790 unlock_policy_rwsem_write(cpu);
791 return;
792 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793
794 if (policy->cur < policy->max) {
795 policy->cur = policy->max;
796
797 __cpufreq_driver_target(policy, policy->max,
798 CPUFREQ_RELATION_L);
799 this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu,
800 &this_dbs_info->prev_cpu_wall);
801 }
802 unlock_policy_rwsem_write(cpu);
803}
804
805static void dbs_input_event(struct input_handle *handle, unsigned int type,
806 unsigned int code, int value)
807{
808 int i;
809
David Ng8192a2f2012-01-19 14:16:19 -0800810 if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) ||
811 (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) {
812 /* nothing to do */
813 return;
814 }
815
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 for_each_online_cpu(i) {
817 queue_work_on(i, input_wq, &per_cpu(dbs_refresh_work, i));
818 }
819}
820
821static int dbs_input_connect(struct input_handler *handler,
822 struct input_dev *dev, const struct input_device_id *id)
823{
824 struct input_handle *handle;
825 int error;
826
827 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
828 if (!handle)
829 return -ENOMEM;
830
831 handle->dev = dev;
832 handle->handler = handler;
833 handle->name = "cpufreq";
834
835 error = input_register_handle(handle);
836 if (error)
837 goto err2;
838
839 error = input_open_device(handle);
840 if (error)
841 goto err1;
842
843 return 0;
844err1:
845 input_unregister_handle(handle);
846err2:
847 kfree(handle);
848 return error;
849}
850
851static void dbs_input_disconnect(struct input_handle *handle)
852{
853 input_close_device(handle);
854 input_unregister_handle(handle);
855 kfree(handle);
856}
857
858static const struct input_device_id dbs_ids[] = {
859 { .driver_info = 1 },
860 { },
861};
862
863static struct input_handler dbs_input_handler = {
864 .event = dbs_input_event,
865 .connect = dbs_input_connect,
866 .disconnect = dbs_input_disconnect,
867 .name = "cpufreq_ond",
868 .id_table = dbs_ids,
869};
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
872 unsigned int event)
873{
874 unsigned int cpu = policy->cpu;
875 struct cpu_dbs_info_s *this_dbs_info;
876 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700877 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Tejun Heo245b2e72009-06-24 15:13:48 +0900879 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 switch (event) {
882 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700883 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return -EINVAL;
885
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800886 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700887
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700888 dbs_enable++;
Rusty Russell835481d2009-01-04 05:18:06 -0800889 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct cpu_dbs_info_s *j_dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900891 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500893
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700894 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
895 &j_dbs_info->prev_cpu_wall);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500896 if (dbs_tuners_ins.ignore_nice) {
897 j_dbs_info->prev_cpu_nice =
898 kstat_cpu(j).cpustat.nice;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800901 this_dbs_info->cpu = cpu;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400902 this_dbs_info->rate_mult = 1;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700903 ondemand_powersave_bias_init_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
905 * Start the timerschedule work, when this governor
906 * is used for first time
907 */
908 if (dbs_enable == 1) {
909 unsigned int latency;
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200910
911 rc = sysfs_create_group(cpufreq_global_kobject,
912 &dbs_attr_group);
913 if (rc) {
914 mutex_unlock(&dbs_mutex);
915 return rc;
916 }
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700919 latency = policy->cpuinfo.transition_latency / 1000;
920 if (latency == 0)
921 latency = 1;
Thomas Renningercef96152009-04-22 13:48:29 +0200922 /* Bring kernel and HW constraints together */
923 min_sampling_rate = max(min_sampling_rate,
924 MIN_LATENCY_MULTIPLIER * latency);
925 dbs_tuners_ins.sampling_rate =
926 max(min_sampling_rate,
927 latency * LATENCY_MULTIPLIER);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700928 dbs_tuners_ins.io_is_busy = should_io_be_busy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930 if (!cpu)
931 rc = input_register_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800932 mutex_unlock(&dbs_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700933
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200934 mutex_init(&this_dbs_info->timer_mutex);
David Ng8192a2f2012-01-19 14:16:19 -0800935
936 if (!ondemand_powersave_bias_setspeed(
937 this_dbs_info->cur_policy,
938 NULL,
939 dbs_tuners_ins.powersave_bias))
940 dbs_timer_init(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942
943 case CPUFREQ_GOV_STOP:
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700944 dbs_timer_exit(this_dbs_info);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700945
946 mutex_lock(&dbs_mutex);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700947 mutex_destroy(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dbs_enable--;
Anitha Anand3dd65092012-01-18 17:17:40 -0800949 /* If device is being removed, policy is no longer
950 * valid. */
951 this_dbs_info->cur_policy = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 if (!cpu)
953 input_unregister_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800954 mutex_unlock(&dbs_mutex);
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200955 if (!dbs_enable)
956 sysfs_remove_group(cpufreq_global_kobject,
957 &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 break;
960
961 case CPUFREQ_GOV_LIMITS:
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700962 mutex_lock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700964 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500965 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700967 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500968 policy->min, CPUFREQ_RELATION_L);
David Ng8192a2f2012-01-19 14:16:19 -0800969 else if (dbs_tuners_ins.powersave_bias != 0)
970 ondemand_powersave_bias_setspeed(
971 this_dbs_info->cur_policy,
972 policy,
973 dbs_tuners_ins.powersave_bias);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700974 mutex_unlock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
976 }
977 return 0;
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static int __init cpufreq_gov_dbs_init(void)
981{
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700982 cputime64_t wall;
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000983 u64 idle_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 unsigned int i;
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000985 int cpu = get_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700986
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000987 idle_time = get_cpu_idle_time_us(cpu, &wall);
988 put_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700989 if (idle_time != -1ULL) {
990 /* Idle micro accounting is supported. Use finer thresholds */
991 dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
992 dbs_tuners_ins.down_differential =
993 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
Thomas Renningercef96152009-04-22 13:48:29 +0200994 /*
995 * In no_hz/micro accounting case we set the minimum frequency
996 * not depending on HZ, but fixed (very low). The deferred
997 * timer might skip some samples if idle/sleeping as needed.
998 */
999 min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
1000 } else {
1001 /* For correct statistics, we need 10 ticks for each measure */
1002 min_sampling_rate =
1003 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001004 }
Akinobu Mita888a7942008-07-14 12:00:45 +09001005
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006 input_wq = create_workqueue("iewq");
1007 if (!input_wq) {
1008 printk(KERN_ERR "Failed to create iewq workqueue\n");
1009 return -EFAULT;
1010 }
1011 for_each_possible_cpu(i) {
1012 INIT_WORK(&per_cpu(dbs_refresh_work, i), dbs_refresh_callback);
1013 }
1014
Tejun Heo57df5572011-01-26 12:12:50 +01001015 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018static void __exit cpufreq_gov_dbs_exit(void)
1019{
Thomas Renninger1c256242007-10-02 13:28:12 -07001020 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 destroy_workqueue(input_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
1024
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001025MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
1026MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
1027MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -05001028 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001029MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Johannes Weiner69157192008-01-17 15:21:08 -08001031#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
1032fs_initcall(cpufreq_gov_dbs_init);
1033#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -08001035#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036module_exit(cpufreq_gov_dbs_exit);