blob: 2d33096a1c252886651b700c4d5a5ad88723865d [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{
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700555 unsigned int max_load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 struct cpufreq_policy *policy;
558 unsigned int j;
559
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400560 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 policy = this_dbs_info->cur_policy;
Venki Pallipadiea487612007-06-20 14:26:24 -0700562
Dave Jones32ee8c32006-02-28 00:43:23 -0500563 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700564 * Every sampling_rate, we check, if current idle time is less
565 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700566 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700567 * frequency which can sustain the load while keeping idle time over
568 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500570 * Any frequency increase takes it to the maximum frequency.
571 * Frequency reduction happens at minimum steps of
572 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
574
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700575 /* Get Absolute Load - in terms of freq */
576 max_load_freq = 0;
577
Rusty Russell835481d2009-01-04 05:18:06 -0800578 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct cpu_dbs_info_s *j_dbs_info;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700580 cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
581 unsigned int idle_time, wall_time, iowait_time;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700582 unsigned int load, load_freq;
583 int freq_avg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Tejun Heo245b2e72009-06-24 15:13:48 +0900585 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700586
587 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700588 cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700589
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700590 wall_time = (unsigned int) cputime64_sub(cur_wall_time,
591 j_dbs_info->prev_cpu_wall);
592 j_dbs_info->prev_cpu_wall = cur_wall_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700594 idle_time = (unsigned int) cputime64_sub(cur_idle_time,
595 j_dbs_info->prev_cpu_idle);
596 j_dbs_info->prev_cpu_idle = cur_idle_time;
597
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700598 iowait_time = (unsigned int) cputime64_sub(cur_iowait_time,
599 j_dbs_info->prev_cpu_iowait);
600 j_dbs_info->prev_cpu_iowait = cur_iowait_time;
601
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500602 if (dbs_tuners_ins.ignore_nice) {
603 cputime64_t cur_nice;
604 unsigned long cur_nice_jiffies;
605
606 cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
607 j_dbs_info->prev_cpu_nice);
608 /*
609 * Assumption: nice time between sampling periods will
610 * be less than 2^32 jiffies for 32 bit sys
611 */
612 cur_nice_jiffies = (unsigned long)
613 cputime64_to_jiffies64(cur_nice);
614
615 j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
616 idle_time += jiffies_to_usecs(cur_nice_jiffies);
617 }
618
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700619 /*
620 * For the purpose of ondemand, waiting for disk IO is an
621 * indication that you're performance critical, and not that
622 * the system is actually idle. So subtract the iowait time
623 * from the cpu idle time.
624 */
625
Arjan van de Ven19379b12010-05-09 08:26:51 -0700626 if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700627 idle_time -= iowait_time;
628
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700629 if (unlikely(!wall_time || wall_time < idle_time))
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700630 continue;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700631
632 load = 100 * (wall_time - idle_time) / wall_time;
633
634 freq_avg = __cpufreq_driver_getavg(policy, j);
635 if (freq_avg <= 0)
636 freq_avg = policy->cur;
637
638 load_freq = load * freq_avg;
639 if (load_freq > max_load_freq)
640 max_load_freq = load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700643 /* Check for frequency increase */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700644 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
David C Niemi3f78a9f2010-10-06 16:54:24 -0400645 /* If switching to max speed, apply sampling_down_factor */
646 if (policy->cur < policy->max)
647 this_dbs_info->rate_mult =
648 dbs_tuners_ins.sampling_down_factor;
Mike Chan00e299f2010-01-26 17:06:47 -0800649 dbs_freq_increase(policy, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return;
651 }
652
653 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700654 /* if we cannot reduce the frequency anymore, break out early */
655 if (policy->cur == policy->min)
656 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dave Jonesc29f1402005-05-31 19:03:50 -0700658 /*
659 * The optimal frequency is the frequency that is the lowest that
660 * can support the current CPU usage without triggering the up
661 * policy. To be safe, we focus 10 points under the threshold.
662 */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700663 if (max_load_freq <
664 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
665 policy->cur) {
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700666 unsigned int freq_next;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700667 freq_next = max_load_freq /
668 (dbs_tuners_ins.up_threshold -
669 dbs_tuners_ins.down_differential);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700670
David C Niemi3f78a9f2010-10-06 16:54:24 -0400671 /* No longer fully busy, reset rate_mult */
672 this_dbs_info->rate_mult = 1;
673
Nagananda.Chumbalkar@hp.com1dbf5882009-12-21 23:40:52 +0100674 if (freq_next < policy->min)
675 freq_next = policy->min;
676
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400677 if (!dbs_tuners_ins.powersave_bias) {
678 __cpufreq_driver_target(policy, freq_next,
679 CPUFREQ_RELATION_L);
680 } else {
681 int freq = powersave_bias_target(policy, freq_next,
682 CPUFREQ_RELATION_L);
683 __cpufreq_driver_target(policy, freq,
684 CPUFREQ_RELATION_L);
685 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
David Howellsc4028952006-11-22 14:57:56 +0000689static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500690{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800691 struct cpu_dbs_info_s *dbs_info =
692 container_of(work, struct cpu_dbs_info_s, work.work);
693 unsigned int cpu = dbs_info->cpu;
694 int sample_type = dbs_info->sample_type;
695
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100696 int delay;
Jocelyn Falempea665df92010-03-11 14:01:11 -0800697
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700698 mutex_lock(&dbs_info->timer_mutex);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800699
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400700 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000701 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400702 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000703 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400704 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400705 if (dbs_info->freq_lo) {
706 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000707 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400708 delay = dbs_info->freq_hi_jiffies;
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100709 } else {
710 /* We want all CPUs to do sampling nearly on
711 * same jiffy
712 */
713 delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
714 * dbs_info->rate_mult);
715
716 if (num_online_cpus() > 1)
717 delay -= jiffies % delay;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400718 }
719 } else {
720 __cpufreq_driver_target(dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500721 dbs_info->freq_lo, CPUFREQ_RELATION_H);
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100722 delay = dbs_info->freq_lo_jiffies;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400723 }
Tejun Heo57df5572011-01-26 12:12:50 +0100724 schedule_delayed_work_on(cpu, &dbs_info->work, delay);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700725 mutex_unlock(&dbs_info->timer_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500726}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800728static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400730 /* We want all CPUs to do sampling nearly on same jiffy */
731 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Jocelyn Falempea665df92010-03-11 14:01:11 -0800732
733 if (num_online_cpus() > 1)
734 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700735
David Howellsc4028952006-11-22 14:57:56 +0000736 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Venki Pallipadi28287032007-05-08 00:27:47 -0700737 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
Tejun Heo57df5572011-01-26 12:12:50 +0100738 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700741static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Mathieu Desnoyersb14893a2009-05-17 10:30:45 -0400743 cancel_delayed_work_sync(&dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Arjan van de Ven19379b12010-05-09 08:26:51 -0700746/*
747 * Not all CPUs want IO time to be accounted as busy; this dependson how
748 * efficient idling at a higher frequency/voltage is.
749 * Pavel Machek says this is not so for various generations of AMD and old
750 * Intel systems.
751 * Mike Chan (androidlcom) calis this is also not true for ARM.
752 * Because of this, whitelist specific known (series) of CPUs by default, and
753 * leave all others up to the user.
754 */
755static int should_io_be_busy(void)
756{
757#if defined(CONFIG_X86)
758 /*
759 * For Intel, Core 2 (model 15) andl later have an efficient idle.
760 */
761 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
762 boot_cpu_data.x86 == 6 &&
763 boot_cpu_data.x86_model >= 15)
764 return 1;
765#endif
766 return 0;
767}
768
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769static void dbs_refresh_callback(struct work_struct *unused)
770{
771 struct cpufreq_policy *policy;
772 struct cpu_dbs_info_s *this_dbs_info;
773 unsigned int cpu = smp_processor_id();
774
775 if (lock_policy_rwsem_write(cpu) < 0)
776 return;
777
778 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
779 policy = this_dbs_info->cur_policy;
David Ng4a0a0232011-08-03 14:04:43 -0700780 if (!policy) {
781 /* CPU not using ondemand governor */
782 unlock_policy_rwsem_write(cpu);
783 return;
784 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785
786 if (policy->cur < policy->max) {
787 policy->cur = policy->max;
788
789 __cpufreq_driver_target(policy, policy->max,
790 CPUFREQ_RELATION_L);
791 this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu,
792 &this_dbs_info->prev_cpu_wall);
793 }
794 unlock_policy_rwsem_write(cpu);
795}
796
797static void dbs_input_event(struct input_handle *handle, unsigned int type,
798 unsigned int code, int value)
799{
800 int i;
801
David Ng8192a2f2012-01-19 14:16:19 -0800802 if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) ||
803 (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) {
804 /* nothing to do */
805 return;
806 }
807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 for_each_online_cpu(i) {
809 queue_work_on(i, input_wq, &per_cpu(dbs_refresh_work, i));
810 }
811}
812
813static int dbs_input_connect(struct input_handler *handler,
814 struct input_dev *dev, const struct input_device_id *id)
815{
816 struct input_handle *handle;
817 int error;
818
819 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
820 if (!handle)
821 return -ENOMEM;
822
823 handle->dev = dev;
824 handle->handler = handler;
825 handle->name = "cpufreq";
826
827 error = input_register_handle(handle);
828 if (error)
829 goto err2;
830
831 error = input_open_device(handle);
832 if (error)
833 goto err1;
834
835 return 0;
836err1:
837 input_unregister_handle(handle);
838err2:
839 kfree(handle);
840 return error;
841}
842
843static void dbs_input_disconnect(struct input_handle *handle)
844{
845 input_close_device(handle);
846 input_unregister_handle(handle);
847 kfree(handle);
848}
849
850static const struct input_device_id dbs_ids[] = {
851 { .driver_info = 1 },
852 { },
853};
854
855static struct input_handler dbs_input_handler = {
856 .event = dbs_input_event,
857 .connect = dbs_input_connect,
858 .disconnect = dbs_input_disconnect,
859 .name = "cpufreq_ond",
860 .id_table = dbs_ids,
861};
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
864 unsigned int event)
865{
866 unsigned int cpu = policy->cpu;
867 struct cpu_dbs_info_s *this_dbs_info;
868 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700869 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Tejun Heo245b2e72009-06-24 15:13:48 +0900871 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 switch (event) {
874 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700875 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -EINVAL;
877
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800878 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700879
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700880 dbs_enable++;
Rusty Russell835481d2009-01-04 05:18:06 -0800881 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct cpu_dbs_info_s *j_dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900883 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500885
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700886 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
887 &j_dbs_info->prev_cpu_wall);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500888 if (dbs_tuners_ins.ignore_nice) {
889 j_dbs_info->prev_cpu_nice =
890 kstat_cpu(j).cpustat.nice;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800893 this_dbs_info->cpu = cpu;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400894 this_dbs_info->rate_mult = 1;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700895 ondemand_powersave_bias_init_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /*
897 * Start the timerschedule work, when this governor
898 * is used for first time
899 */
900 if (dbs_enable == 1) {
901 unsigned int latency;
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200902
903 rc = sysfs_create_group(cpufreq_global_kobject,
904 &dbs_attr_group);
905 if (rc) {
906 mutex_unlock(&dbs_mutex);
907 return rc;
908 }
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700911 latency = policy->cpuinfo.transition_latency / 1000;
912 if (latency == 0)
913 latency = 1;
Thomas Renningercef96152009-04-22 13:48:29 +0200914 /* Bring kernel and HW constraints together */
915 min_sampling_rate = max(min_sampling_rate,
916 MIN_LATENCY_MULTIPLIER * latency);
917 dbs_tuners_ins.sampling_rate =
918 max(min_sampling_rate,
919 latency * LATENCY_MULTIPLIER);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700920 dbs_tuners_ins.io_is_busy = should_io_be_busy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 if (!cpu)
923 rc = input_register_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800924 mutex_unlock(&dbs_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700925
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200926 mutex_init(&this_dbs_info->timer_mutex);
David Ng8192a2f2012-01-19 14:16:19 -0800927
928 if (!ondemand_powersave_bias_setspeed(
929 this_dbs_info->cur_policy,
930 NULL,
931 dbs_tuners_ins.powersave_bias))
932 dbs_timer_init(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break;
934
935 case CPUFREQ_GOV_STOP:
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700936 dbs_timer_exit(this_dbs_info);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700937
938 mutex_lock(&dbs_mutex);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700939 mutex_destroy(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 dbs_enable--;
Anitha Anand3dd65092012-01-18 17:17:40 -0800941 /* If device is being removed, policy is no longer
942 * valid. */
943 this_dbs_info->cur_policy = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 if (!cpu)
945 input_unregister_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800946 mutex_unlock(&dbs_mutex);
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200947 if (!dbs_enable)
948 sysfs_remove_group(cpufreq_global_kobject,
949 &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 break;
952
953 case CPUFREQ_GOV_LIMITS:
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700954 mutex_lock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700956 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500957 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700959 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500960 policy->min, CPUFREQ_RELATION_L);
David Ng8192a2f2012-01-19 14:16:19 -0800961 else if (dbs_tuners_ins.powersave_bias != 0)
962 ondemand_powersave_bias_setspeed(
963 this_dbs_info->cur_policy,
964 policy,
965 dbs_tuners_ins.powersave_bias);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700966 mutex_unlock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 }
969 return 0;
970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972static int __init cpufreq_gov_dbs_init(void)
973{
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700974 cputime64_t wall;
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000975 u64 idle_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 unsigned int i;
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000977 int cpu = get_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700978
Andrea Righi4f6e6b92008-09-18 10:43:40 +0000979 idle_time = get_cpu_idle_time_us(cpu, &wall);
980 put_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700981 if (idle_time != -1ULL) {
982 /* Idle micro accounting is supported. Use finer thresholds */
983 dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
984 dbs_tuners_ins.down_differential =
985 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
Thomas Renningercef96152009-04-22 13:48:29 +0200986 /*
987 * In no_hz/micro accounting case we set the minimum frequency
988 * not depending on HZ, but fixed (very low). The deferred
989 * timer might skip some samples if idle/sleeping as needed.
990 */
991 min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
992 } else {
993 /* For correct statistics, we need 10 ticks for each measure */
994 min_sampling_rate =
995 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700996 }
Akinobu Mita888a7942008-07-14 12:00:45 +0900997
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 input_wq = create_workqueue("iewq");
999 if (!input_wq) {
1000 printk(KERN_ERR "Failed to create iewq workqueue\n");
1001 return -EFAULT;
1002 }
1003 for_each_possible_cpu(i) {
1004 INIT_WORK(&per_cpu(dbs_refresh_work, i), dbs_refresh_callback);
1005 }
1006
Tejun Heo57df5572011-01-26 12:12:50 +01001007 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010static void __exit cpufreq_gov_dbs_exit(void)
1011{
Thomas Renninger1c256242007-10-02 13:28:12 -07001012 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 destroy_workqueue(input_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001017MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
1018MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
1019MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -05001020 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001021MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Johannes Weiner69157192008-01-17 15:21:08 -08001023#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
1024fs_initcall(cpufreq_gov_dbs_init);
1025#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -08001027#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028module_exit(cpufreq_gov_dbs_exit);