blob: 785ba6cea52538f5b0ad08d2c1f20bd60ea80b47 [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
Glauber Costa3292beb2011-11-28 14:45:17 -0200136static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700137{
Glauber Costa3292beb2011-11-28 14:45:17 -0200138 u64 idle_time;
Martin Schwidefsky612ef282011-12-19 19:23:15 +0100139 u64 cur_wall_time;
Glauber Costa3292beb2011-11-28 14:45:17 -0200140 u64 busy_time;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700141
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700142 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700143
Martin Schwidefsky612ef282011-12-19 19:23:15 +0100144 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
145 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
Glauber Costa3292beb2011-11-28 14:45:17 -0200146 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
147 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
148 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
149 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700150
Martin Schwidefsky64861632011-12-15 14:56:09 +0100151 idle_time = cur_wall_time - busy_time;
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700152 if (wall)
Glauber Costa3292beb2011-11-28 14:45:17 -0200153 *wall = jiffies_to_usecs(cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700154
Glauber Costa3292beb2011-11-28 14:45:17 -0200155 return jiffies_to_usecs(idle_time);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700156}
157
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700158static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
159{
Michal Hocko6beea0c2011-08-24 09:37:48 +0200160 u64 idle_time = get_cpu_idle_time_us(cpu, NULL);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700161
162 if (idle_time == -1ULL)
163 return get_cpu_idle_time_jiffy(cpu, wall);
Michal Hocko6beea0c2011-08-24 09:37:48 +0200164 else
165 idle_time += get_cpu_iowait_time_us(cpu, wall);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700166
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700167 return idle_time;
168}
169
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700170static inline cputime64_t get_cpu_iowait_time(unsigned int cpu, cputime64_t *wall)
171{
172 u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
173
174 if (iowait_time == -1ULL)
175 return 0;
176
177 return iowait_time;
178}
179
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400180/*
181 * Find right freq to be set now with powersave_bias on.
182 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
183 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
184 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +0200185static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
186 unsigned int freq_next,
187 unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400188{
David Ng8192a2f2012-01-19 14:16:19 -0800189 unsigned int freq_req, freq_avg;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400190 unsigned int freq_hi, freq_lo;
191 unsigned int index = 0;
192 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
David Ng8192a2f2012-01-19 14:16:19 -0800193 int freq_reduc;
Tejun Heo245b2e72009-06-24 15:13:48 +0900194 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
195 policy->cpu);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400196
197 if (!dbs_info->freq_table) {
198 dbs_info->freq_lo = 0;
199 dbs_info->freq_lo_jiffies = 0;
200 return freq_next;
201 }
202
203 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
204 relation, &index);
205 freq_req = dbs_info->freq_table[index].frequency;
206 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
207 freq_avg = freq_req - freq_reduc;
208
209 /* Find freq bounds for freq_avg in freq_table */
210 index = 0;
211 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
212 CPUFREQ_RELATION_H, &index);
213 freq_lo = dbs_info->freq_table[index].frequency;
214 index = 0;
215 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
216 CPUFREQ_RELATION_L, &index);
217 freq_hi = dbs_info->freq_table[index].frequency;
218
219 /* Find out how long we have to be in hi and lo freqs */
220 if (freq_hi == freq_lo) {
221 dbs_info->freq_lo = 0;
222 dbs_info->freq_lo_jiffies = 0;
223 return freq_lo;
224 }
225 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
226 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
227 jiffies_hi += ((freq_hi - freq_lo) / 2);
228 jiffies_hi /= (freq_hi - freq_lo);
229 jiffies_lo = jiffies_total - jiffies_hi;
230 dbs_info->freq_lo = freq_lo;
231 dbs_info->freq_lo_jiffies = jiffies_lo;
232 dbs_info->freq_hi_jiffies = jiffies_hi;
233 return freq_hi;
234}
235
David Ng8192a2f2012-01-19 14:16:19 -0800236static int ondemand_powersave_bias_setspeed(struct cpufreq_policy *policy,
237 struct cpufreq_policy *altpolicy,
238 int level)
239{
240 if (level == POWERSAVE_BIAS_MAXLEVEL) {
241 /* maximum powersave; set to lowest frequency */
242 __cpufreq_driver_target(policy,
243 (altpolicy) ? altpolicy->min : policy->min,
244 CPUFREQ_RELATION_L);
245 return 1;
246 } else if (level == POWERSAVE_BIAS_MINLEVEL) {
247 /* minimum powersave; set to highest frequency */
248 __cpufreq_driver_target(policy,
249 (altpolicy) ? altpolicy->max : policy->max,
250 CPUFREQ_RELATION_H);
251 return 1;
252 }
253 return 0;
254}
255
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700256static void ondemand_powersave_bias_init_cpu(int cpu)
257{
Tejun Heo384be2b2009-08-14 14:41:02 +0900258 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700259 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
260 dbs_info->freq_lo = 0;
261}
262
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400263static void ondemand_powersave_bias_init(void)
264{
265 int i;
266 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700267 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400268 }
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/************************** sysfs interface ************************/
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200272
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200273static ssize_t show_sampling_rate_min(struct kobject *kobj,
274 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Thomas Renningercef96152009-04-22 13:48:29 +0200276 return sprintf(buf, "%u\n", min_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200279define_one_global_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281/* cpufreq_ondemand Governor Tunables */
282#define show_one(file_name, object) \
283static ssize_t show_##file_name \
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200284(struct kobject *kobj, struct attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{ \
286 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
287}
288show_one(sampling_rate, sampling_rate);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700289show_one(io_is_busy, io_is_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290show_one(up_threshold, up_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291show_one(down_differential, down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400292show_one(sampling_down_factor, sampling_down_factor);
Alexander Clouter001893c2005-12-01 01:09:25 -0800293show_one(ignore_nice_load, ignore_nice);
David Ng8192a2f2012-01-19 14:16:19 -0800294
295static ssize_t show_powersave_bias
296(struct kobject *kobj, struct attribute *attr, char *buf)
297{
298 return snprintf(buf, PAGE_SIZE, "%d\n", dbs_tuners_ins.powersave_bias);
299}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900301/**
302 * update_sampling_rate - update sampling rate effective immediately if needed.
303 * @new_rate: new sampling rate
304 *
305 * If new rate is smaller than the old, simply updaing
306 * dbs_tuners_int.sampling_rate might not be appropriate. For example,
307 * if the original sampling_rate was 1 second and the requested new sampling
308 * rate is 10 ms because the user needs immediate reaction from ondemand
309 * governor, but not sure if higher frequency will be required or not,
310 * then, the governor may change the sampling rate too late; up to 1 second
311 * later. Thus, if we are reducing the sampling rate, we need to make the
312 * new value effective immediately.
313 */
314static void update_sampling_rate(unsigned int new_rate)
315{
316 int cpu;
317
318 dbs_tuners_ins.sampling_rate = new_rate
319 = max(new_rate, min_sampling_rate);
320
321 for_each_online_cpu(cpu) {
322 struct cpufreq_policy *policy;
323 struct cpu_dbs_info_s *dbs_info;
324 unsigned long next_sampling, appointed_at;
325
326 policy = cpufreq_cpu_get(cpu);
327 if (!policy)
328 continue;
329 dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
330 cpufreq_cpu_put(policy);
331
332 mutex_lock(&dbs_info->timer_mutex);
333
334 if (!delayed_work_pending(&dbs_info->work)) {
335 mutex_unlock(&dbs_info->timer_mutex);
336 continue;
337 }
338
339 next_sampling = jiffies + usecs_to_jiffies(new_rate);
340 appointed_at = dbs_info->work.timer.expires;
341
342
343 if (time_before(next_sampling, appointed_at)) {
344
345 mutex_unlock(&dbs_info->timer_mutex);
346 cancel_delayed_work_sync(&dbs_info->work);
347 mutex_lock(&dbs_info->timer_mutex);
348
349 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work,
350 usecs_to_jiffies(new_rate));
351
352 }
353 mutex_unlock(&dbs_info->timer_mutex);
354 }
355}
356
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200357static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
358 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 unsigned int input;
361 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700362 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700363 if (ret != 1)
364 return -EINVAL;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900365 update_sampling_rate(input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return count;
367}
368
Arjan van de Ven19379b12010-05-09 08:26:51 -0700369static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
370 const char *buf, size_t count)
371{
372 unsigned int input;
373 int ret;
374
375 ret = sscanf(buf, "%u", &input);
376 if (ret != 1)
377 return -EINVAL;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700378 dbs_tuners_ins.io_is_busy = !!input;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700379 return count;
380}
381
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200382static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
383 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 unsigned int input;
386 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700387 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Dave Jones32ee8c32006-02-28 00:43:23 -0500389 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700390 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -EINVAL;
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 dbs_tuners_ins.up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return count;
395}
396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397static ssize_t store_down_differential(struct kobject *a, struct attribute *b,
398 const char *buf, size_t count)
399{
400 unsigned int input;
401 int ret;
402 ret = sscanf(buf, "%u", &input);
403
404 if (ret != 1 || input >= dbs_tuners_ins.up_threshold ||
405 input < MIN_FREQUENCY_DOWN_DIFFERENTIAL) {
406 return -EINVAL;
407 }
408
409 dbs_tuners_ins.down_differential = input;
410
411 return count;
412}
413
David C Niemi3f78a9f2010-10-06 16:54:24 -0400414static ssize_t store_sampling_down_factor(struct kobject *a,
415 struct attribute *b, const char *buf, size_t count)
416{
417 unsigned int input, j;
418 int ret;
419 ret = sscanf(buf, "%u", &input);
420
421 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
422 return -EINVAL;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400423 dbs_tuners_ins.sampling_down_factor = input;
424
425 /* Reset down sampling multiplier in case it was active */
426 for_each_online_cpu(j) {
427 struct cpu_dbs_info_s *dbs_info;
428 dbs_info = &per_cpu(od_cpu_dbs_info, j);
429 dbs_info->rate_mult = 1;
430 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400431 return count;
432}
433
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200434static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
435 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700436{
437 unsigned int input;
438 int ret;
439
440 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500441
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700442 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500443 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700444 return -EINVAL;
445
Dave Jones2b03f892009-01-18 01:43:44 -0500446 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700447 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500448
Dave Jones2b03f892009-01-18 01:43:44 -0500449 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700450 return count;
451 }
452 dbs_tuners_ins.ignore_nice = input;
453
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700454 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700455 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700456 struct cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900457 dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700458 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
459 &dbs_info->prev_cpu_wall);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500460 if (dbs_tuners_ins.ignore_nice)
Glauber Costa3292beb2011-11-28 14:45:17 -0200461 dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500462
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700463 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700464 return count;
465}
466
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200467static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
468 const char *buf, size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400469{
David Ng8192a2f2012-01-19 14:16:19 -0800470 int input = 0;
471 int bypass = 0;
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530472 int ret, cpu, reenable_timer, j;
David Ng8192a2f2012-01-19 14:16:19 -0800473 struct cpu_dbs_info_s *dbs_info;
474
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530475 struct cpumask cpus_timer_done;
476 cpumask_clear(&cpus_timer_done);
477
David Ng8192a2f2012-01-19 14:16:19 -0800478 ret = sscanf(buf, "%d", &input);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400479
480 if (ret != 1)
481 return -EINVAL;
482
David Ng8192a2f2012-01-19 14:16:19 -0800483 if (input >= POWERSAVE_BIAS_MAXLEVEL) {
484 input = POWERSAVE_BIAS_MAXLEVEL;
485 bypass = 1;
486 } else if (input <= POWERSAVE_BIAS_MINLEVEL) {
487 input = POWERSAVE_BIAS_MINLEVEL;
488 bypass = 1;
489 }
490
491 if (input == dbs_tuners_ins.powersave_bias) {
492 /* no change */
493 return count;
494 }
495
496 reenable_timer = ((dbs_tuners_ins.powersave_bias ==
497 POWERSAVE_BIAS_MAXLEVEL) ||
498 (dbs_tuners_ins.powersave_bias ==
499 POWERSAVE_BIAS_MINLEVEL));
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400500
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400501 dbs_tuners_ins.powersave_bias = input;
David Ng8192a2f2012-01-19 14:16:19 -0800502 if (!bypass) {
503 if (reenable_timer) {
504 /* reinstate dbs timer */
505 for_each_online_cpu(cpu) {
506 if (lock_policy_rwsem_write(cpu) < 0)
507 continue;
508
509 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530510
511 for_each_cpu(j, &cpus_timer_done) {
512 if (!dbs_info->cur_policy) {
513 pr_err("Dbs policy is NULL\n");
514 goto skip_this_cpu;
515 }
516 if (cpumask_test_cpu(j, dbs_info->
517 cur_policy->cpus))
518 goto skip_this_cpu;
519 }
520
521 cpumask_set_cpu(cpu, &cpus_timer_done);
David Ng8192a2f2012-01-19 14:16:19 -0800522 if (dbs_info->cur_policy) {
523 /* restart dbs timer */
524 dbs_timer_init(dbs_info);
525 }
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530526skip_this_cpu:
David Ng8192a2f2012-01-19 14:16:19 -0800527 unlock_policy_rwsem_write(cpu);
528 }
529 }
530 ondemand_powersave_bias_init();
531 } else {
532 /* running at maximum or minimum frequencies; cancel
533 dbs timer as periodic load sampling is not necessary */
534 for_each_online_cpu(cpu) {
535 if (lock_policy_rwsem_write(cpu) < 0)
536 continue;
537
538 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530539
540 for_each_cpu(j, &cpus_timer_done) {
541 if (!dbs_info->cur_policy) {
542 pr_err("Dbs policy is NULL\n");
543 goto skip_this_cpu_bypass;
544 }
545 if (cpumask_test_cpu(j, dbs_info->
546 cur_policy->cpus))
547 goto skip_this_cpu_bypass;
548 }
549
550 cpumask_set_cpu(cpu, &cpus_timer_done);
551
David Ng8192a2f2012-01-19 14:16:19 -0800552 if (dbs_info->cur_policy) {
553 /* cpu using ondemand, cancel dbs timer */
554 mutex_lock(&dbs_info->timer_mutex);
555 dbs_timer_exit(dbs_info);
556
557 ondemand_powersave_bias_setspeed(
558 dbs_info->cur_policy,
559 NULL,
560 input);
561
562 mutex_unlock(&dbs_info->timer_mutex);
563 }
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530564skip_this_cpu_bypass:
David Ng8192a2f2012-01-19 14:16:19 -0800565 unlock_policy_rwsem_write(cpu);
566 }
567 }
568
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400569 return count;
570}
571
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200572define_one_global_rw(sampling_rate);
Linus Torvalds07d77752010-05-18 08:49:13 -0700573define_one_global_rw(io_is_busy);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200574define_one_global_rw(up_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575define_one_global_rw(down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400576define_one_global_rw(sampling_down_factor);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200577define_one_global_rw(ignore_nice_load);
578define_one_global_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Dave Jones2b03f892009-01-18 01:43:44 -0500580static struct attribute *dbs_attributes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 &sampling_rate_min.attr,
582 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 &up_threshold.attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584 &down_differential.attr,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400585 &sampling_down_factor.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800586 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400587 &powersave_bias.attr,
Arjan van de Ven19379b12010-05-09 08:26:51 -0700588 &io_is_busy.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 NULL
590};
591
592static struct attribute_group dbs_attr_group = {
593 .attrs = dbs_attributes,
594 .name = "ondemand",
595};
596
597/************************** sysfs end ************************/
598
Mike Chan00e299f2010-01-26 17:06:47 -0800599static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
600{
601 if (dbs_tuners_ins.powersave_bias)
602 freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
603 else if (p->cur == p->max)
604 return;
605
606 __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
607 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
608}
609
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700610static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800612 /* Extrapolated load of this CPU */
613 unsigned int load_at_max_freq = 0;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700614 unsigned int max_load_freq;
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800615 /* Current load across this CPU */
616 unsigned int cur_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 struct cpufreq_policy *policy;
619 unsigned int j;
620
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400621 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 policy = this_dbs_info->cur_policy;
Venki Pallipadiea487612007-06-20 14:26:24 -0700623
Dave Jones32ee8c32006-02-28 00:43:23 -0500624 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700625 * Every sampling_rate, we check, if current idle time is less
626 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700627 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700628 * frequency which can sustain the load while keeping idle time over
629 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500631 * Any frequency increase takes it to the maximum frequency.
632 * Frequency reduction happens at minimum steps of
633 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
635
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700636 /* Get Absolute Load - in terms of freq */
637 max_load_freq = 0;
638
Rusty Russell835481d2009-01-04 05:18:06 -0800639 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct cpu_dbs_info_s *j_dbs_info;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700641 cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
642 unsigned int idle_time, wall_time, iowait_time;
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800643 unsigned int load_freq;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700644 int freq_avg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Tejun Heo245b2e72009-06-24 15:13:48 +0900646 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700647
648 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700649 cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700650
Martin Schwidefsky64861632011-12-15 14:56:09 +0100651 wall_time = (unsigned int)
652 (cur_wall_time - j_dbs_info->prev_cpu_wall);
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700653 j_dbs_info->prev_cpu_wall = cur_wall_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Martin Schwidefsky64861632011-12-15 14:56:09 +0100655 idle_time = (unsigned int)
656 (cur_idle_time - j_dbs_info->prev_cpu_idle);
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700657 j_dbs_info->prev_cpu_idle = cur_idle_time;
658
Martin Schwidefsky64861632011-12-15 14:56:09 +0100659 iowait_time = (unsigned int)
660 (cur_iowait_time - j_dbs_info->prev_cpu_iowait);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700661 j_dbs_info->prev_cpu_iowait = cur_iowait_time;
662
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500663 if (dbs_tuners_ins.ignore_nice) {
Glauber Costa3292beb2011-11-28 14:45:17 -0200664 u64 cur_nice;
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500665 unsigned long cur_nice_jiffies;
666
Glauber Costa3292beb2011-11-28 14:45:17 -0200667 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
668 j_dbs_info->prev_cpu_nice;
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500669 /*
670 * Assumption: nice time between sampling periods will
671 * be less than 2^32 jiffies for 32 bit sys
672 */
673 cur_nice_jiffies = (unsigned long)
674 cputime64_to_jiffies64(cur_nice);
675
Glauber Costa3292beb2011-11-28 14:45:17 -0200676 j_dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500677 idle_time += jiffies_to_usecs(cur_nice_jiffies);
678 }
679
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700680 /*
681 * For the purpose of ondemand, waiting for disk IO is an
682 * indication that you're performance critical, and not that
683 * the system is actually idle. So subtract the iowait time
684 * from the cpu idle time.
685 */
686
Arjan van de Ven19379b12010-05-09 08:26:51 -0700687 if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700688 idle_time -= iowait_time;
689
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700690 if (unlikely(!wall_time || wall_time < idle_time))
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700691 continue;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700692
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800693 cur_load = 100 * (wall_time - idle_time) / wall_time;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700694
695 freq_avg = __cpufreq_driver_getavg(policy, j);
696 if (freq_avg <= 0)
697 freq_avg = policy->cur;
698
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800699 load_freq = cur_load * freq_avg;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700700 if (load_freq > max_load_freq)
701 max_load_freq = load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800703 /* calculate the scaled load across CPU */
704 load_at_max_freq = (cur_load * policy->cur)/policy->cpuinfo.max_freq;
705
706 cpufreq_notify_utilization(policy, load_at_max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700708 /* Check for frequency increase */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700709 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
David C Niemi3f78a9f2010-10-06 16:54:24 -0400710 /* If switching to max speed, apply sampling_down_factor */
711 if (policy->cur < policy->max)
712 this_dbs_info->rate_mult =
713 dbs_tuners_ins.sampling_down_factor;
Mike Chan00e299f2010-01-26 17:06:47 -0800714 dbs_freq_increase(policy, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return;
716 }
717
718 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700719 /* if we cannot reduce the frequency anymore, break out early */
720 if (policy->cur == policy->min)
721 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Dave Jonesc29f1402005-05-31 19:03:50 -0700723 /*
724 * The optimal frequency is the frequency that is the lowest that
725 * can support the current CPU usage without triggering the up
726 * policy. To be safe, we focus 10 points under the threshold.
727 */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700728 if (max_load_freq <
729 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
730 policy->cur) {
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700731 unsigned int freq_next;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700732 freq_next = max_load_freq /
733 (dbs_tuners_ins.up_threshold -
734 dbs_tuners_ins.down_differential);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700735
David C Niemi3f78a9f2010-10-06 16:54:24 -0400736 /* No longer fully busy, reset rate_mult */
737 this_dbs_info->rate_mult = 1;
738
Nagananda.Chumbalkar@hp.com1dbf5882009-12-21 23:40:52 +0100739 if (freq_next < policy->min)
740 freq_next = policy->min;
741
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400742 if (!dbs_tuners_ins.powersave_bias) {
743 __cpufreq_driver_target(policy, freq_next,
744 CPUFREQ_RELATION_L);
745 } else {
746 int freq = powersave_bias_target(policy, freq_next,
747 CPUFREQ_RELATION_L);
748 __cpufreq_driver_target(policy, freq,
749 CPUFREQ_RELATION_L);
750 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
David Howellsc4028952006-11-22 14:57:56 +0000754static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500755{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800756 struct cpu_dbs_info_s *dbs_info =
757 container_of(work, struct cpu_dbs_info_s, work.work);
758 unsigned int cpu = dbs_info->cpu;
759 int sample_type = dbs_info->sample_type;
760
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100761 int delay;
Jocelyn Falempea665df92010-03-11 14:01:11 -0800762
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700763 mutex_lock(&dbs_info->timer_mutex);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800764
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400765 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000766 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400767 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000768 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400769 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400770 if (dbs_info->freq_lo) {
771 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000772 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400773 delay = dbs_info->freq_hi_jiffies;
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100774 } else {
775 /* We want all CPUs to do sampling nearly on
776 * same jiffy
777 */
778 delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
779 * dbs_info->rate_mult);
780
781 if (num_online_cpus() > 1)
782 delay -= jiffies % delay;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400783 }
784 } else {
785 __cpufreq_driver_target(dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500786 dbs_info->freq_lo, CPUFREQ_RELATION_H);
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100787 delay = dbs_info->freq_lo_jiffies;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400788 }
Tejun Heo57df5572011-01-26 12:12:50 +0100789 schedule_delayed_work_on(cpu, &dbs_info->work, delay);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700790 mutex_unlock(&dbs_info->timer_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500791}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800793static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400795 /* We want all CPUs to do sampling nearly on same jiffy */
796 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Jocelyn Falempea665df92010-03-11 14:01:11 -0800797
798 if (num_online_cpus() > 1)
799 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700800
David Howellsc4028952006-11-22 14:57:56 +0000801 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Venki Pallipadi28287032007-05-08 00:27:47 -0700802 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
Tejun Heo57df5572011-01-26 12:12:50 +0100803 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700806static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Mathieu Desnoyersb14893a2009-05-17 10:30:45 -0400808 cancel_delayed_work_sync(&dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Arjan van de Ven19379b12010-05-09 08:26:51 -0700811/*
812 * Not all CPUs want IO time to be accounted as busy; this dependson how
813 * efficient idling at a higher frequency/voltage is.
814 * Pavel Machek says this is not so for various generations of AMD and old
815 * Intel systems.
816 * Mike Chan (androidlcom) calis this is also not true for ARM.
817 * Because of this, whitelist specific known (series) of CPUs by default, and
818 * leave all others up to the user.
819 */
820static int should_io_be_busy(void)
821{
822#if defined(CONFIG_X86)
823 /*
824 * For Intel, Core 2 (model 15) andl later have an efficient idle.
825 */
826 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
827 boot_cpu_data.x86 == 6 &&
828 boot_cpu_data.x86_model >= 15)
829 return 1;
830#endif
831 return 0;
832}
833
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834static void dbs_refresh_callback(struct work_struct *unused)
835{
836 struct cpufreq_policy *policy;
837 struct cpu_dbs_info_s *this_dbs_info;
838 unsigned int cpu = smp_processor_id();
839
Krishna Vankaa3e04d82012-06-08 11:35:43 +0530840 get_online_cpus();
841
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842 if (lock_policy_rwsem_write(cpu) < 0)
Krishna Vankaa3e04d82012-06-08 11:35:43 +0530843 goto bail_acq_sema_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844
845 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
846 policy = this_dbs_info->cur_policy;
David Ng4a0a0232011-08-03 14:04:43 -0700847 if (!policy) {
848 /* CPU not using ondemand governor */
Krishna Vankaa3e04d82012-06-08 11:35:43 +0530849 goto bail_incorrect_governor;
David Ng4a0a0232011-08-03 14:04:43 -0700850 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851
852 if (policy->cur < policy->max) {
853 policy->cur = policy->max;
854
855 __cpufreq_driver_target(policy, policy->max,
856 CPUFREQ_RELATION_L);
857 this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu,
858 &this_dbs_info->prev_cpu_wall);
859 }
Krishna Vankaa3e04d82012-06-08 11:35:43 +0530860
861bail_incorrect_governor:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862 unlock_policy_rwsem_write(cpu);
Krishna Vankaa3e04d82012-06-08 11:35:43 +0530863
864bail_acq_sema_failed:
865 put_online_cpus();
866 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867}
868
869static void dbs_input_event(struct input_handle *handle, unsigned int type,
870 unsigned int code, int value)
871{
872 int i;
873
David Ng8192a2f2012-01-19 14:16:19 -0800874 if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) ||
875 (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) {
876 /* nothing to do */
877 return;
878 }
879
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 for_each_online_cpu(i) {
881 queue_work_on(i, input_wq, &per_cpu(dbs_refresh_work, i));
882 }
883}
884
885static int dbs_input_connect(struct input_handler *handler,
886 struct input_dev *dev, const struct input_device_id *id)
887{
888 struct input_handle *handle;
889 int error;
890
891 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
892 if (!handle)
893 return -ENOMEM;
894
895 handle->dev = dev;
896 handle->handler = handler;
897 handle->name = "cpufreq";
898
899 error = input_register_handle(handle);
900 if (error)
901 goto err2;
902
903 error = input_open_device(handle);
904 if (error)
905 goto err1;
906
907 return 0;
908err1:
909 input_unregister_handle(handle);
910err2:
911 kfree(handle);
912 return error;
913}
914
915static void dbs_input_disconnect(struct input_handle *handle)
916{
917 input_close_device(handle);
918 input_unregister_handle(handle);
919 kfree(handle);
920}
921
922static const struct input_device_id dbs_ids[] = {
923 { .driver_info = 1 },
924 { },
925};
926
927static struct input_handler dbs_input_handler = {
928 .event = dbs_input_event,
929 .connect = dbs_input_connect,
930 .disconnect = dbs_input_disconnect,
931 .name = "cpufreq_ond",
932 .id_table = dbs_ids,
933};
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
936 unsigned int event)
937{
938 unsigned int cpu = policy->cpu;
939 struct cpu_dbs_info_s *this_dbs_info;
940 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700941 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Tejun Heo245b2e72009-06-24 15:13:48 +0900943 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 switch (event) {
946 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700947 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -EINVAL;
949
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800950 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700951
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700952 dbs_enable++;
Rusty Russell835481d2009-01-04 05:18:06 -0800953 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct cpu_dbs_info_s *j_dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900955 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -0500957
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700958 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
959 &j_dbs_info->prev_cpu_wall);
Glauber Costa3292beb2011-11-28 14:45:17 -0200960 if (dbs_tuners_ins.ignore_nice)
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500961 j_dbs_info->prev_cpu_nice =
Glauber Costa3292beb2011-11-28 14:45:17 -0200962 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800964 this_dbs_info->cpu = cpu;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400965 this_dbs_info->rate_mult = 1;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700966 ondemand_powersave_bias_init_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /*
968 * Start the timerschedule work, when this governor
969 * is used for first time
970 */
971 if (dbs_enable == 1) {
972 unsigned int latency;
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200973
974 rc = sysfs_create_group(cpufreq_global_kobject,
975 &dbs_attr_group);
976 if (rc) {
977 mutex_unlock(&dbs_mutex);
978 return rc;
979 }
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -0700982 latency = policy->cpuinfo.transition_latency / 1000;
983 if (latency == 0)
984 latency = 1;
Thomas Renningercef96152009-04-22 13:48:29 +0200985 /* Bring kernel and HW constraints together */
986 min_sampling_rate = max(min_sampling_rate,
987 MIN_LATENCY_MULTIPLIER * latency);
988 dbs_tuners_ins.sampling_rate =
989 max(min_sampling_rate,
990 latency * LATENCY_MULTIPLIER);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700991 dbs_tuners_ins.io_is_busy = should_io_be_busy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 if (!cpu)
994 rc = input_register_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800995 mutex_unlock(&dbs_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700996
David Ng8192a2f2012-01-19 14:16:19 -0800997
998 if (!ondemand_powersave_bias_setspeed(
999 this_dbs_info->cur_policy,
1000 NULL,
1001 dbs_tuners_ins.powersave_bias))
1002 dbs_timer_init(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004
1005 case CPUFREQ_GOV_STOP:
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -07001006 dbs_timer_exit(this_dbs_info);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001007
1008 mutex_lock(&dbs_mutex);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001009 mutex_destroy(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 dbs_enable--;
Anitha Anand3dd65092012-01-18 17:17:40 -08001011 /* If device is being removed, policy is no longer
1012 * valid. */
1013 this_dbs_info->cur_policy = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 if (!cpu)
1015 input_unregister_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001016 mutex_unlock(&dbs_mutex);
Thomas Renninger0e625ac2009-07-24 15:25:06 +02001017 if (!dbs_enable)
1018 sysfs_remove_group(cpufreq_global_kobject,
1019 &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 break;
1022
1023 case CPUFREQ_GOV_LIMITS:
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001024 mutex_lock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001026 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -05001027 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001029 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -05001030 policy->min, CPUFREQ_RELATION_L);
David Ng8192a2f2012-01-19 14:16:19 -08001031 else if (dbs_tuners_ins.powersave_bias != 0)
1032 ondemand_powersave_bias_setspeed(
1033 this_dbs_info->cur_policy,
1034 policy,
1035 dbs_tuners_ins.powersave_bias);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001036 mutex_unlock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 break;
1038 }
1039 return 0;
1040}
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042static int __init cpufreq_gov_dbs_init(void)
1043{
Andrea Righi4f6e6b92008-09-18 10:43:40 +00001044 u64 idle_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 unsigned int i;
Andrea Righi4f6e6b92008-09-18 10:43:40 +00001046 int cpu = get_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001047
Kamalesh Babulal21f2e3c2011-12-09 16:18:42 +05301048 idle_time = get_cpu_idle_time_us(cpu, NULL);
Andrea Righi4f6e6b92008-09-18 10:43:40 +00001049 put_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001050 if (idle_time != -1ULL) {
1051 /* Idle micro accounting is supported. Use finer thresholds */
1052 dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
1053 dbs_tuners_ins.down_differential =
1054 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
Thomas Renningercef96152009-04-22 13:48:29 +02001055 /*
Paul Bollebd74b322011-08-06 14:33:43 +02001056 * In nohz/micro accounting case we set the minimum frequency
Thomas Renningercef96152009-04-22 13:48:29 +02001057 * not depending on HZ, but fixed (very low). The deferred
1058 * timer might skip some samples if idle/sleeping as needed.
1059 */
1060 min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
1061 } else {
1062 /* For correct statistics, we need 10 ticks for each measure */
1063 min_sampling_rate =
1064 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001065 }
Akinobu Mita888a7942008-07-14 12:00:45 +09001066
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001067 input_wq = create_workqueue("iewq");
1068 if (!input_wq) {
1069 printk(KERN_ERR "Failed to create iewq workqueue\n");
1070 return -EFAULT;
1071 }
1072 for_each_possible_cpu(i) {
Praveen Chidambaram457a4452012-07-19 10:45:07 -06001073 struct cpu_dbs_info_s *this_dbs_info =
1074 &per_cpu(od_cpu_dbs_info, i);
1075 mutex_init(&this_dbs_info->timer_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076 INIT_WORK(&per_cpu(dbs_refresh_work, i), dbs_refresh_callback);
1077 }
1078
Tejun Heo57df5572011-01-26 12:12:50 +01001079 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082static void __exit cpufreq_gov_dbs_exit(void)
1083{
Thomas Renninger1c256242007-10-02 13:28:12 -07001084 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085 destroy_workqueue(input_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
1088
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001089MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
1090MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
1091MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -05001092 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001093MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Johannes Weiner69157192008-01-17 15:21:08 -08001095#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
1096fs_initcall(cpufreq_gov_dbs_init);
1097#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -08001099#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100module_exit(cpufreq_gov_dbs_exit);