blob: 5b0dcf31d14f74ec313046acfa1f9243deaa2bb7 [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;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -070095 unsigned int prev_load;
96 unsigned int max_load;
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080097 int cpu;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -070098 unsigned int sample_type:1;
99 /*
100 * percpu mutex that serializes governor limit change with
101 * do_dbs_timer invocation. We do not want do_dbs_timer to run
102 * when user is changing the governor or limits.
103 */
104 struct mutex timer_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
Tejun Heo245b2e72009-06-24 15:13:48 +0900106static DEFINE_PER_CPU(struct cpu_dbs_info_s, od_cpu_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
David Ng8192a2f2012-01-19 14:16:19 -0800108static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info);
109static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static unsigned int dbs_enable; /* number of CPUs using this policy */
112
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700113/*
Thomas Renninger326c86d2011-03-03 21:31:27 +0100114 * dbs_mutex protects dbs_enable in governor start/stop.
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700115 */
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700116static DEFINE_MUTEX(dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118static struct workqueue_struct *input_wq;
119
120static DEFINE_PER_CPU(struct work_struct, dbs_refresh_work);
121
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400122static struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -0500123 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -0500124 unsigned int up_threshold;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700125 unsigned int up_threshold_multi_core;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700126 unsigned int down_differential;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700127 unsigned int down_differential_multi_core;
128 unsigned int optimal_freq;
129 unsigned int up_threshold_any_cpu_load;
130 unsigned int sync_freq;
Dave Jones32ee8c32006-02-28 00:43:23 -0500131 unsigned int ignore_nice;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400132 unsigned int sampling_down_factor;
David Ng8192a2f2012-01-19 14:16:19 -0800133 int powersave_bias;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700134 unsigned int io_is_busy;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400135} dbs_tuners_ins = {
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700136 .up_threshold_multi_core = DEF_FREQUENCY_UP_THRESHOLD,
Dave Jones32ee8c32006-02-28 00:43:23 -0500137 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400138 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700139 .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700140 .down_differential_multi_core = MICRO_FREQUENCY_DOWN_DIFFERENTIAL,
141 .up_threshold_any_cpu_load = DEF_FREQUENCY_UP_THRESHOLD,
Eric Piel9cbad612006-03-10 11:35:27 +0200142 .ignore_nice = 0,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400143 .powersave_bias = 0,
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700144 .sync_freq = 0,
145 .optimal_freq = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Glauber Costa3292beb2011-11-28 14:45:17 -0200148static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700149{
Glauber Costa3292beb2011-11-28 14:45:17 -0200150 u64 idle_time;
Martin Schwidefsky612ef282011-12-19 19:23:15 +0100151 u64 cur_wall_time;
Glauber Costa3292beb2011-11-28 14:45:17 -0200152 u64 busy_time;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700153
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700154 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700155
Martin Schwidefsky612ef282011-12-19 19:23:15 +0100156 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
157 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
Glauber Costa3292beb2011-11-28 14:45:17 -0200158 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
159 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700162
Martin Schwidefsky64861632011-12-15 14:56:09 +0100163 idle_time = cur_wall_time - busy_time;
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700164 if (wall)
Glauber Costa3292beb2011-11-28 14:45:17 -0200165 *wall = jiffies_to_usecs(cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700166
Glauber Costa3292beb2011-11-28 14:45:17 -0200167 return jiffies_to_usecs(idle_time);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700168}
169
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700170static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
171{
Michal Hocko6beea0c2011-08-24 09:37:48 +0200172 u64 idle_time = get_cpu_idle_time_us(cpu, NULL);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700173
174 if (idle_time == -1ULL)
175 return get_cpu_idle_time_jiffy(cpu, wall);
Michal Hocko6beea0c2011-08-24 09:37:48 +0200176 else
177 idle_time += get_cpu_iowait_time_us(cpu, wall);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700178
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -0700179 return idle_time;
180}
181
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700182static inline cputime64_t get_cpu_iowait_time(unsigned int cpu, cputime64_t *wall)
183{
184 u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
185
186 if (iowait_time == -1ULL)
187 return 0;
188
189 return iowait_time;
190}
191
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400192/*
193 * Find right freq to be set now with powersave_bias on.
194 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
195 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
196 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +0200197static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
198 unsigned int freq_next,
199 unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400200{
David Ng8192a2f2012-01-19 14:16:19 -0800201 unsigned int freq_req, freq_avg;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400202 unsigned int freq_hi, freq_lo;
203 unsigned int index = 0;
204 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
David Ng8192a2f2012-01-19 14:16:19 -0800205 int freq_reduc;
Tejun Heo245b2e72009-06-24 15:13:48 +0900206 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
207 policy->cpu);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400208
209 if (!dbs_info->freq_table) {
210 dbs_info->freq_lo = 0;
211 dbs_info->freq_lo_jiffies = 0;
212 return freq_next;
213 }
214
215 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
216 relation, &index);
217 freq_req = dbs_info->freq_table[index].frequency;
218 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
219 freq_avg = freq_req - freq_reduc;
220
221 /* Find freq bounds for freq_avg in freq_table */
222 index = 0;
223 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
224 CPUFREQ_RELATION_H, &index);
225 freq_lo = dbs_info->freq_table[index].frequency;
226 index = 0;
227 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
228 CPUFREQ_RELATION_L, &index);
229 freq_hi = dbs_info->freq_table[index].frequency;
230
231 /* Find out how long we have to be in hi and lo freqs */
232 if (freq_hi == freq_lo) {
233 dbs_info->freq_lo = 0;
234 dbs_info->freq_lo_jiffies = 0;
235 return freq_lo;
236 }
237 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
238 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
239 jiffies_hi += ((freq_hi - freq_lo) / 2);
240 jiffies_hi /= (freq_hi - freq_lo);
241 jiffies_lo = jiffies_total - jiffies_hi;
242 dbs_info->freq_lo = freq_lo;
243 dbs_info->freq_lo_jiffies = jiffies_lo;
244 dbs_info->freq_hi_jiffies = jiffies_hi;
245 return freq_hi;
246}
247
David Ng8192a2f2012-01-19 14:16:19 -0800248static int ondemand_powersave_bias_setspeed(struct cpufreq_policy *policy,
249 struct cpufreq_policy *altpolicy,
250 int level)
251{
252 if (level == POWERSAVE_BIAS_MAXLEVEL) {
253 /* maximum powersave; set to lowest frequency */
254 __cpufreq_driver_target(policy,
255 (altpolicy) ? altpolicy->min : policy->min,
256 CPUFREQ_RELATION_L);
257 return 1;
258 } else if (level == POWERSAVE_BIAS_MINLEVEL) {
259 /* minimum powersave; set to highest frequency */
260 __cpufreq_driver_target(policy,
261 (altpolicy) ? altpolicy->max : policy->max,
262 CPUFREQ_RELATION_H);
263 return 1;
264 }
265 return 0;
266}
267
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700268static void ondemand_powersave_bias_init_cpu(int cpu)
269{
Tejun Heo384be2b2009-08-14 14:41:02 +0900270 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700271 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
272 dbs_info->freq_lo = 0;
273}
274
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400275static void ondemand_powersave_bias_init(void)
276{
277 int i;
278 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700279 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400280 }
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/************************** sysfs interface ************************/
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200284
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200285static ssize_t show_sampling_rate_min(struct kobject *kobj,
286 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Thomas Renningercef96152009-04-22 13:48:29 +0200288 return sprintf(buf, "%u\n", min_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200291define_one_global_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293/* cpufreq_ondemand Governor Tunables */
294#define show_one(file_name, object) \
295static ssize_t show_##file_name \
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200296(struct kobject *kobj, struct attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{ \
298 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
299}
300show_one(sampling_rate, sampling_rate);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700301show_one(io_is_busy, io_is_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302show_one(up_threshold, up_threshold);
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700303show_one(up_threshold_multi_core, up_threshold_multi_core);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304show_one(down_differential, down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400305show_one(sampling_down_factor, sampling_down_factor);
Alexander Clouter001893c2005-12-01 01:09:25 -0800306show_one(ignore_nice_load, ignore_nice);
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700307show_one(optimal_freq, optimal_freq);
308show_one(up_threshold_any_cpu_load, up_threshold_any_cpu_load);
309show_one(sync_freq, sync_freq);
David Ng8192a2f2012-01-19 14:16:19 -0800310
311static ssize_t show_powersave_bias
312(struct kobject *kobj, struct attribute *attr, char *buf)
313{
314 return snprintf(buf, PAGE_SIZE, "%d\n", dbs_tuners_ins.powersave_bias);
315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900317/**
318 * update_sampling_rate - update sampling rate effective immediately if needed.
319 * @new_rate: new sampling rate
320 *
321 * If new rate is smaller than the old, simply updaing
322 * dbs_tuners_int.sampling_rate might not be appropriate. For example,
323 * if the original sampling_rate was 1 second and the requested new sampling
324 * rate is 10 ms because the user needs immediate reaction from ondemand
325 * governor, but not sure if higher frequency will be required or not,
326 * then, the governor may change the sampling rate too late; up to 1 second
327 * later. Thus, if we are reducing the sampling rate, we need to make the
328 * new value effective immediately.
329 */
330static void update_sampling_rate(unsigned int new_rate)
331{
332 int cpu;
333
334 dbs_tuners_ins.sampling_rate = new_rate
335 = max(new_rate, min_sampling_rate);
336
337 for_each_online_cpu(cpu) {
338 struct cpufreq_policy *policy;
339 struct cpu_dbs_info_s *dbs_info;
340 unsigned long next_sampling, appointed_at;
341
342 policy = cpufreq_cpu_get(cpu);
343 if (!policy)
344 continue;
345 dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
346 cpufreq_cpu_put(policy);
347
348 mutex_lock(&dbs_info->timer_mutex);
349
350 if (!delayed_work_pending(&dbs_info->work)) {
351 mutex_unlock(&dbs_info->timer_mutex);
352 continue;
353 }
354
355 next_sampling = jiffies + usecs_to_jiffies(new_rate);
356 appointed_at = dbs_info->work.timer.expires;
357
358
359 if (time_before(next_sampling, appointed_at)) {
360
361 mutex_unlock(&dbs_info->timer_mutex);
362 cancel_delayed_work_sync(&dbs_info->work);
363 mutex_lock(&dbs_info->timer_mutex);
364
365 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work,
366 usecs_to_jiffies(new_rate));
367
368 }
369 mutex_unlock(&dbs_info->timer_mutex);
370 }
371}
372
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200373static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
374 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 unsigned int input;
377 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700378 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700379 if (ret != 1)
380 return -EINVAL;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900381 update_sampling_rate(input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return count;
383}
384
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700385static ssize_t store_sync_freq(struct kobject *a, struct attribute *b,
386 const char *buf, size_t count)
387{
388 unsigned int input;
389 int ret;
390
391 ret = sscanf(buf, "%u", &input);
392 if (ret != 1)
393 return -EINVAL;
394 dbs_tuners_ins.sync_freq = input;
395 return count;
396}
397
Arjan van de Ven19379b12010-05-09 08:26:51 -0700398static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
399 const char *buf, size_t count)
400{
401 unsigned int input;
402 int ret;
403
404 ret = sscanf(buf, "%u", &input);
405 if (ret != 1)
406 return -EINVAL;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700407 dbs_tuners_ins.io_is_busy = !!input;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700408 return count;
409}
410
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700411static ssize_t store_optimal_freq(struct kobject *a, struct attribute *b,
412 const char *buf, size_t count)
413{
414 unsigned int input;
415 int ret;
416
417 ret = sscanf(buf, "%u", &input);
418 if (ret != 1)
419 return -EINVAL;
420 dbs_tuners_ins.optimal_freq = input;
421 return count;
422}
423
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200424static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
425 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 unsigned int input;
428 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700429 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Dave Jones32ee8c32006-02-28 00:43:23 -0500431 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700432 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -EINVAL;
434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 dbs_tuners_ins.up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return count;
437}
438
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700439static ssize_t store_up_threshold_multi_core(struct kobject *a,
440 struct attribute *b, const char *buf, size_t count)
441{
442 unsigned int input;
443 int ret;
444 ret = sscanf(buf, "%u", &input);
445
446 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
447 input < MIN_FREQUENCY_UP_THRESHOLD) {
448 return -EINVAL;
449 }
450 dbs_tuners_ins.up_threshold_multi_core = input;
451 return count;
452}
453
454static ssize_t store_up_threshold_any_cpu_load(struct kobject *a,
455 struct attribute *b, const char *buf, size_t count)
456{
457 unsigned int input;
458 int ret;
459 ret = sscanf(buf, "%u", &input);
460
461 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
462 input < MIN_FREQUENCY_UP_THRESHOLD) {
463 return -EINVAL;
464 }
465 dbs_tuners_ins.up_threshold_any_cpu_load = input;
466 return count;
467}
468
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469static ssize_t store_down_differential(struct kobject *a, struct attribute *b,
470 const char *buf, size_t count)
471{
472 unsigned int input;
473 int ret;
474 ret = sscanf(buf, "%u", &input);
475
476 if (ret != 1 || input >= dbs_tuners_ins.up_threshold ||
477 input < MIN_FREQUENCY_DOWN_DIFFERENTIAL) {
478 return -EINVAL;
479 }
480
481 dbs_tuners_ins.down_differential = input;
482
483 return count;
484}
485
David C Niemi3f78a9f2010-10-06 16:54:24 -0400486static ssize_t store_sampling_down_factor(struct kobject *a,
487 struct attribute *b, const char *buf, size_t count)
488{
489 unsigned int input, j;
490 int ret;
491 ret = sscanf(buf, "%u", &input);
492
493 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
494 return -EINVAL;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400495 dbs_tuners_ins.sampling_down_factor = input;
496
497 /* Reset down sampling multiplier in case it was active */
498 for_each_online_cpu(j) {
499 struct cpu_dbs_info_s *dbs_info;
500 dbs_info = &per_cpu(od_cpu_dbs_info, j);
501 dbs_info->rate_mult = 1;
502 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400503 return count;
504}
505
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200506static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
507 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700508{
509 unsigned int input;
510 int ret;
511
512 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500513
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700514 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500515 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700516 return -EINVAL;
517
Dave Jones2b03f892009-01-18 01:43:44 -0500518 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700519 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500520
Dave Jones2b03f892009-01-18 01:43:44 -0500521 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700522 return count;
523 }
524 dbs_tuners_ins.ignore_nice = input;
525
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700526 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700527 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700528 struct cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900529 dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700530 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
531 &dbs_info->prev_cpu_wall);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500532 if (dbs_tuners_ins.ignore_nice)
Glauber Costa3292beb2011-11-28 14:45:17 -0200533 dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500534
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700535 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700536 return count;
537}
538
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200539static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
540 const char *buf, size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400541{
David Ng8192a2f2012-01-19 14:16:19 -0800542 int input = 0;
543 int bypass = 0;
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530544 int ret, cpu, reenable_timer, j;
David Ng8192a2f2012-01-19 14:16:19 -0800545 struct cpu_dbs_info_s *dbs_info;
546
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530547 struct cpumask cpus_timer_done;
548 cpumask_clear(&cpus_timer_done);
549
David Ng8192a2f2012-01-19 14:16:19 -0800550 ret = sscanf(buf, "%d", &input);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400551
552 if (ret != 1)
553 return -EINVAL;
554
David Ng8192a2f2012-01-19 14:16:19 -0800555 if (input >= POWERSAVE_BIAS_MAXLEVEL) {
556 input = POWERSAVE_BIAS_MAXLEVEL;
557 bypass = 1;
558 } else if (input <= POWERSAVE_BIAS_MINLEVEL) {
559 input = POWERSAVE_BIAS_MINLEVEL;
560 bypass = 1;
561 }
562
563 if (input == dbs_tuners_ins.powersave_bias) {
564 /* no change */
565 return count;
566 }
567
568 reenable_timer = ((dbs_tuners_ins.powersave_bias ==
569 POWERSAVE_BIAS_MAXLEVEL) ||
570 (dbs_tuners_ins.powersave_bias ==
571 POWERSAVE_BIAS_MINLEVEL));
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400572
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400573 dbs_tuners_ins.powersave_bias = input;
David Ng8192a2f2012-01-19 14:16:19 -0800574 if (!bypass) {
575 if (reenable_timer) {
576 /* reinstate dbs timer */
577 for_each_online_cpu(cpu) {
578 if (lock_policy_rwsem_write(cpu) < 0)
579 continue;
580
581 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530582
583 for_each_cpu(j, &cpus_timer_done) {
584 if (!dbs_info->cur_policy) {
585 pr_err("Dbs policy is NULL\n");
586 goto skip_this_cpu;
587 }
588 if (cpumask_test_cpu(j, dbs_info->
589 cur_policy->cpus))
590 goto skip_this_cpu;
591 }
592
593 cpumask_set_cpu(cpu, &cpus_timer_done);
David Ng8192a2f2012-01-19 14:16:19 -0800594 if (dbs_info->cur_policy) {
595 /* restart dbs timer */
596 dbs_timer_init(dbs_info);
597 }
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530598skip_this_cpu:
David Ng8192a2f2012-01-19 14:16:19 -0800599 unlock_policy_rwsem_write(cpu);
600 }
601 }
602 ondemand_powersave_bias_init();
603 } else {
604 /* running at maximum or minimum frequencies; cancel
605 dbs timer as periodic load sampling is not necessary */
606 for_each_online_cpu(cpu) {
607 if (lock_policy_rwsem_write(cpu) < 0)
608 continue;
609
610 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530611
612 for_each_cpu(j, &cpus_timer_done) {
613 if (!dbs_info->cur_policy) {
614 pr_err("Dbs policy is NULL\n");
615 goto skip_this_cpu_bypass;
616 }
617 if (cpumask_test_cpu(j, dbs_info->
618 cur_policy->cpus))
619 goto skip_this_cpu_bypass;
620 }
621
622 cpumask_set_cpu(cpu, &cpus_timer_done);
623
David Ng8192a2f2012-01-19 14:16:19 -0800624 if (dbs_info->cur_policy) {
625 /* cpu using ondemand, cancel dbs timer */
626 mutex_lock(&dbs_info->timer_mutex);
627 dbs_timer_exit(dbs_info);
628
629 ondemand_powersave_bias_setspeed(
630 dbs_info->cur_policy,
631 NULL,
632 input);
633
634 mutex_unlock(&dbs_info->timer_mutex);
635 }
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530636skip_this_cpu_bypass:
David Ng8192a2f2012-01-19 14:16:19 -0800637 unlock_policy_rwsem_write(cpu);
638 }
639 }
640
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400641 return count;
642}
643
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200644define_one_global_rw(sampling_rate);
Linus Torvalds07d77752010-05-18 08:49:13 -0700645define_one_global_rw(io_is_busy);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200646define_one_global_rw(up_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647define_one_global_rw(down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400648define_one_global_rw(sampling_down_factor);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200649define_one_global_rw(ignore_nice_load);
650define_one_global_rw(powersave_bias);
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700651define_one_global_rw(up_threshold_multi_core);
652define_one_global_rw(optimal_freq);
653define_one_global_rw(up_threshold_any_cpu_load);
654define_one_global_rw(sync_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Dave Jones2b03f892009-01-18 01:43:44 -0500656static struct attribute *dbs_attributes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 &sampling_rate_min.attr,
658 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 &up_threshold.attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 &down_differential.attr,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400661 &sampling_down_factor.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800662 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400663 &powersave_bias.attr,
Arjan van de Ven19379b12010-05-09 08:26:51 -0700664 &io_is_busy.attr,
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700665 &up_threshold_multi_core.attr,
666 &optimal_freq.attr,
667 &up_threshold_any_cpu_load.attr,
668 &sync_freq.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 NULL
670};
671
672static struct attribute_group dbs_attr_group = {
673 .attrs = dbs_attributes,
674 .name = "ondemand",
675};
676
677/************************** sysfs end ************************/
678
Mike Chan00e299f2010-01-26 17:06:47 -0800679static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
680{
681 if (dbs_tuners_ins.powersave_bias)
682 freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
683 else if (p->cur == p->max)
684 return;
685
686 __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
687 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
688}
689
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700690static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800692 /* Extrapolated load of this CPU */
693 unsigned int load_at_max_freq = 0;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700694 unsigned int max_load_freq;
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800695 /* Current load across this CPU */
696 unsigned int cur_load = 0;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700697 unsigned int max_load_other_cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct cpufreq_policy *policy;
699 unsigned int j;
700
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400701 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 policy = this_dbs_info->cur_policy;
Venki Pallipadiea487612007-06-20 14:26:24 -0700703
Dave Jones32ee8c32006-02-28 00:43:23 -0500704 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700705 * Every sampling_rate, we check, if current idle time is less
706 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700707 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700708 * frequency which can sustain the load while keeping idle time over
709 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500711 * Any frequency increase takes it to the maximum frequency.
712 * Frequency reduction happens at minimum steps of
713 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
715
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700716 /* Get Absolute Load - in terms of freq */
717 max_load_freq = 0;
718
Rusty Russell835481d2009-01-04 05:18:06 -0800719 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct cpu_dbs_info_s *j_dbs_info;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700721 cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
722 unsigned int idle_time, wall_time, iowait_time;
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800723 unsigned int load_freq;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700724 int freq_avg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Tejun Heo245b2e72009-06-24 15:13:48 +0900726 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700727
728 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700729 cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700730
Martin Schwidefsky64861632011-12-15 14:56:09 +0100731 wall_time = (unsigned int)
732 (cur_wall_time - j_dbs_info->prev_cpu_wall);
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700733 j_dbs_info->prev_cpu_wall = cur_wall_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Martin Schwidefsky64861632011-12-15 14:56:09 +0100735 idle_time = (unsigned int)
736 (cur_idle_time - j_dbs_info->prev_cpu_idle);
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700737 j_dbs_info->prev_cpu_idle = cur_idle_time;
738
Martin Schwidefsky64861632011-12-15 14:56:09 +0100739 iowait_time = (unsigned int)
740 (cur_iowait_time - j_dbs_info->prev_cpu_iowait);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700741 j_dbs_info->prev_cpu_iowait = cur_iowait_time;
742
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500743 if (dbs_tuners_ins.ignore_nice) {
Glauber Costa3292beb2011-11-28 14:45:17 -0200744 u64 cur_nice;
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500745 unsigned long cur_nice_jiffies;
746
Glauber Costa3292beb2011-11-28 14:45:17 -0200747 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
748 j_dbs_info->prev_cpu_nice;
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500749 /*
750 * Assumption: nice time between sampling periods will
751 * be less than 2^32 jiffies for 32 bit sys
752 */
753 cur_nice_jiffies = (unsigned long)
754 cputime64_to_jiffies64(cur_nice);
755
Glauber Costa3292beb2011-11-28 14:45:17 -0200756 j_dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500757 idle_time += jiffies_to_usecs(cur_nice_jiffies);
758 }
759
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700760 /*
761 * For the purpose of ondemand, waiting for disk IO is an
762 * indication that you're performance critical, and not that
763 * the system is actually idle. So subtract the iowait time
764 * from the cpu idle time.
765 */
766
Arjan van de Ven19379b12010-05-09 08:26:51 -0700767 if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700768 idle_time -= iowait_time;
769
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700770 if (unlikely(!wall_time || wall_time < idle_time))
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700771 continue;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700772
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800773 cur_load = 100 * (wall_time - idle_time) / wall_time;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700774 j_dbs_info->max_load = max(cur_load, j_dbs_info->prev_load);
775 j_dbs_info->prev_load = cur_load;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700776 freq_avg = __cpufreq_driver_getavg(policy, j);
777 if (freq_avg <= 0)
778 freq_avg = policy->cur;
779
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800780 load_freq = cur_load * freq_avg;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700781 if (load_freq > max_load_freq)
782 max_load_freq = load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700784
785 for_each_online_cpu(j) {
786 struct cpu_dbs_info_s *j_dbs_info;
787 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
788
789 if (j == policy->cpu)
790 continue;
791
792 if (max_load_other_cpu < j_dbs_info->max_load)
793 max_load_other_cpu = j_dbs_info->max_load;
794 /*
795 * The other cpu could be running at higher frequency
796 * but may not have completed it's sampling_down_factor.
797 * For that case consider other cpu is loaded so that
798 * frequency imbalance does not occur.
799 */
800
801 if ((j_dbs_info->cur_policy != NULL)
802 && (j_dbs_info->cur_policy->cur ==
803 j_dbs_info->cur_policy->max)) {
804
805 if (policy->cur >= dbs_tuners_ins.optimal_freq)
806 max_load_other_cpu =
807 dbs_tuners_ins.up_threshold_any_cpu_load;
808 }
809 }
810
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800811 /* calculate the scaled load across CPU */
812 load_at_max_freq = (cur_load * policy->cur)/policy->cpuinfo.max_freq;
813
814 cpufreq_notify_utilization(policy, load_at_max_freq);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700815 /* Check for frequency increase */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700816 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
David C Niemi3f78a9f2010-10-06 16:54:24 -0400817 /* If switching to max speed, apply sampling_down_factor */
818 if (policy->cur < policy->max)
819 this_dbs_info->rate_mult =
820 dbs_tuners_ins.sampling_down_factor;
Mike Chan00e299f2010-01-26 17:06:47 -0800821 dbs_freq_increase(policy, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return;
823 }
824
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700825 if (num_online_cpus() > 1) {
826
827 if (max_load_other_cpu >
828 dbs_tuners_ins.up_threshold_any_cpu_load) {
829 if (policy->cur < dbs_tuners_ins.sync_freq)
830 dbs_freq_increase(policy,
831 dbs_tuners_ins.sync_freq);
832 return;
833 }
834
835 if (max_load_freq > dbs_tuners_ins.up_threshold_multi_core *
836 policy->cur) {
837 if (policy->cur < dbs_tuners_ins.optimal_freq)
838 dbs_freq_increase(policy,
839 dbs_tuners_ins.optimal_freq);
840 return;
841 }
842 }
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700845 /* if we cannot reduce the frequency anymore, break out early */
846 if (policy->cur == policy->min)
847 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Dave Jonesc29f1402005-05-31 19:03:50 -0700849 /*
850 * The optimal frequency is the frequency that is the lowest that
851 * can support the current CPU usage without triggering the up
852 * policy. To be safe, we focus 10 points under the threshold.
853 */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700854 if (max_load_freq <
855 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
856 policy->cur) {
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700857 unsigned int freq_next;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700858 freq_next = max_load_freq /
859 (dbs_tuners_ins.up_threshold -
860 dbs_tuners_ins.down_differential);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700861
David C Niemi3f78a9f2010-10-06 16:54:24 -0400862 /* No longer fully busy, reset rate_mult */
863 this_dbs_info->rate_mult = 1;
864
Nagananda.Chumbalkar@hp.com1dbf5882009-12-21 23:40:52 +0100865 if (freq_next < policy->min)
866 freq_next = policy->min;
867
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700868 if (num_online_cpus() > 1) {
869 if (max_load_other_cpu >
870 (dbs_tuners_ins.up_threshold_multi_core -
871 dbs_tuners_ins.down_differential) &&
872 freq_next < dbs_tuners_ins.sync_freq)
873 freq_next = dbs_tuners_ins.sync_freq;
874
875 if (max_load_freq >
876 (dbs_tuners_ins.up_threshold_multi_core -
877 dbs_tuners_ins.down_differential_multi_core) *
878 policy->cur)
879 freq_next = dbs_tuners_ins.optimal_freq;
880
881 }
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400882 if (!dbs_tuners_ins.powersave_bias) {
883 __cpufreq_driver_target(policy, freq_next,
884 CPUFREQ_RELATION_L);
885 } else {
886 int freq = powersave_bias_target(policy, freq_next,
887 CPUFREQ_RELATION_L);
888 __cpufreq_driver_target(policy, freq,
889 CPUFREQ_RELATION_L);
890 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
David Howellsc4028952006-11-22 14:57:56 +0000894static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500895{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800896 struct cpu_dbs_info_s *dbs_info =
897 container_of(work, struct cpu_dbs_info_s, work.work);
898 unsigned int cpu = dbs_info->cpu;
899 int sample_type = dbs_info->sample_type;
900
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100901 int delay;
Jocelyn Falempea665df92010-03-11 14:01:11 -0800902
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700903 mutex_lock(&dbs_info->timer_mutex);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800904
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400905 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000906 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400907 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000908 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400909 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400910 if (dbs_info->freq_lo) {
911 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000912 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400913 delay = dbs_info->freq_hi_jiffies;
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100914 } else {
915 /* We want all CPUs to do sampling nearly on
916 * same jiffy
917 */
918 delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
919 * dbs_info->rate_mult);
920
921 if (num_online_cpus() > 1)
922 delay -= jiffies % delay;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400923 }
924 } else {
925 __cpufreq_driver_target(dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500926 dbs_info->freq_lo, CPUFREQ_RELATION_H);
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100927 delay = dbs_info->freq_lo_jiffies;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400928 }
Tejun Heo57df5572011-01-26 12:12:50 +0100929 schedule_delayed_work_on(cpu, &dbs_info->work, delay);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700930 mutex_unlock(&dbs_info->timer_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500931}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800933static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400935 /* We want all CPUs to do sampling nearly on same jiffy */
936 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Jocelyn Falempea665df92010-03-11 14:01:11 -0800937
938 if (num_online_cpus() > 1)
939 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700940
David Howellsc4028952006-11-22 14:57:56 +0000941 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Venki Pallipadi28287032007-05-08 00:27:47 -0700942 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
Tejun Heo57df5572011-01-26 12:12:50 +0100943 schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700946static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Mathieu Desnoyersb14893a2009-05-17 10:30:45 -0400948 cancel_delayed_work_sync(&dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Arjan van de Ven19379b12010-05-09 08:26:51 -0700951/*
952 * Not all CPUs want IO time to be accounted as busy; this dependson how
953 * efficient idling at a higher frequency/voltage is.
954 * Pavel Machek says this is not so for various generations of AMD and old
955 * Intel systems.
956 * Mike Chan (androidlcom) calis this is also not true for ARM.
957 * Because of this, whitelist specific known (series) of CPUs by default, and
958 * leave all others up to the user.
959 */
960static int should_io_be_busy(void)
961{
962#if defined(CONFIG_X86)
963 /*
964 * For Intel, Core 2 (model 15) andl later have an efficient idle.
965 */
966 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
967 boot_cpu_data.x86 == 6 &&
968 boot_cpu_data.x86_model >= 15)
969 return 1;
970#endif
971 return 0;
972}
973
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974static void dbs_refresh_callback(struct work_struct *unused)
975{
976 struct cpufreq_policy *policy;
977 struct cpu_dbs_info_s *this_dbs_info;
978 unsigned int cpu = smp_processor_id();
979
Krishna Vankaa3e04d82012-06-08 11:35:43 +0530980 get_online_cpus();
981
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982 if (lock_policy_rwsem_write(cpu) < 0)
Krishna Vankaa3e04d82012-06-08 11:35:43 +0530983 goto bail_acq_sema_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984
985 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
986 policy = this_dbs_info->cur_policy;
David Ng4a0a0232011-08-03 14:04:43 -0700987 if (!policy) {
988 /* CPU not using ondemand governor */
Krishna Vankaa3e04d82012-06-08 11:35:43 +0530989 goto bail_incorrect_governor;
David Ng4a0a0232011-08-03 14:04:43 -0700990 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991
992 if (policy->cur < policy->max) {
Anji Jonnalab2408f42012-12-13 14:03:54 +0530993 /*
994 * Arch specific cpufreq driver may fail.
995 * Don't update governor frequency upon failure.
996 */
997 if (__cpufreq_driver_target(policy, policy->max,
998 CPUFREQ_RELATION_L) >= 0)
999 policy->cur = policy->max;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001 this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu,
1002 &this_dbs_info->prev_cpu_wall);
1003 }
Krishna Vankaa3e04d82012-06-08 11:35:43 +05301004
1005bail_incorrect_governor:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006 unlock_policy_rwsem_write(cpu);
Krishna Vankaa3e04d82012-06-08 11:35:43 +05301007
1008bail_acq_sema_failed:
1009 put_online_cpus();
1010 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011}
1012
1013static void dbs_input_event(struct input_handle *handle, unsigned int type,
1014 unsigned int code, int value)
1015{
1016 int i;
1017
David Ng8192a2f2012-01-19 14:16:19 -08001018 if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) ||
1019 (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) {
1020 /* nothing to do */
1021 return;
1022 }
1023
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 for_each_online_cpu(i) {
1025 queue_work_on(i, input_wq, &per_cpu(dbs_refresh_work, i));
1026 }
1027}
1028
1029static int dbs_input_connect(struct input_handler *handler,
1030 struct input_dev *dev, const struct input_device_id *id)
1031{
1032 struct input_handle *handle;
1033 int error;
1034
1035 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
1036 if (!handle)
1037 return -ENOMEM;
1038
1039 handle->dev = dev;
1040 handle->handler = handler;
1041 handle->name = "cpufreq";
1042
1043 error = input_register_handle(handle);
1044 if (error)
1045 goto err2;
1046
1047 error = input_open_device(handle);
1048 if (error)
1049 goto err1;
1050
1051 return 0;
1052err1:
1053 input_unregister_handle(handle);
1054err2:
1055 kfree(handle);
1056 return error;
1057}
1058
1059static void dbs_input_disconnect(struct input_handle *handle)
1060{
1061 input_close_device(handle);
1062 input_unregister_handle(handle);
1063 kfree(handle);
1064}
1065
1066static const struct input_device_id dbs_ids[] = {
1067 { .driver_info = 1 },
1068 { },
1069};
1070
1071static struct input_handler dbs_input_handler = {
1072 .event = dbs_input_event,
1073 .connect = dbs_input_connect,
1074 .disconnect = dbs_input_disconnect,
1075 .name = "cpufreq_ond",
1076 .id_table = dbs_ids,
1077};
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
1080 unsigned int event)
1081{
1082 unsigned int cpu = policy->cpu;
1083 struct cpu_dbs_info_s *this_dbs_info;
1084 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -07001085 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Tejun Heo245b2e72009-06-24 15:13:48 +09001087 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 switch (event) {
1090 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001091 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -EINVAL;
1093
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001094 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -07001095
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001096 dbs_enable++;
Rusty Russell835481d2009-01-04 05:18:06 -08001097 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct cpu_dbs_info_s *j_dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +09001099 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -05001101
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -07001102 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
1103 &j_dbs_info->prev_cpu_wall);
Glauber Costa3292beb2011-11-28 14:45:17 -02001104 if (dbs_tuners_ins.ignore_nice)
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -05001105 j_dbs_info->prev_cpu_nice =
Glauber Costa3292beb2011-11-28 14:45:17 -02001106 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -08001108 this_dbs_info->cpu = cpu;
David C Niemi3f78a9f2010-10-06 16:54:24 -04001109 this_dbs_info->rate_mult = 1;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001110 ondemand_powersave_bias_init_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /*
1112 * Start the timerschedule work, when this governor
1113 * is used for first time
1114 */
1115 if (dbs_enable == 1) {
1116 unsigned int latency;
Thomas Renninger0e625ac2009-07-24 15:25:06 +02001117
1118 rc = sysfs_create_group(cpufreq_global_kobject,
1119 &dbs_attr_group);
1120 if (rc) {
1121 mutex_unlock(&dbs_mutex);
1122 return rc;
1123 }
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -07001126 latency = policy->cpuinfo.transition_latency / 1000;
1127 if (latency == 0)
1128 latency = 1;
Thomas Renningercef96152009-04-22 13:48:29 +02001129 /* Bring kernel and HW constraints together */
1130 min_sampling_rate = max(min_sampling_rate,
1131 MIN_LATENCY_MULTIPLIER * latency);
1132 dbs_tuners_ins.sampling_rate =
1133 max(min_sampling_rate,
1134 latency * LATENCY_MULTIPLIER);
Arjan van de Ven19379b12010-05-09 08:26:51 -07001135 dbs_tuners_ins.io_is_busy = should_io_be_busy();
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -07001136
1137 if (dbs_tuners_ins.optimal_freq == 0)
1138 dbs_tuners_ins.optimal_freq = policy->min;
1139
1140 if (dbs_tuners_ins.sync_freq == 0)
1141 dbs_tuners_ins.sync_freq = policy->min;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143 if (!cpu)
1144 rc = input_register_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001145 mutex_unlock(&dbs_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001146
David Ng8192a2f2012-01-19 14:16:19 -08001147
1148 if (!ondemand_powersave_bias_setspeed(
1149 this_dbs_info->cur_policy,
1150 NULL,
1151 dbs_tuners_ins.powersave_bias))
1152 dbs_timer_init(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
1154
1155 case CPUFREQ_GOV_STOP:
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -07001156 dbs_timer_exit(this_dbs_info);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001157
1158 mutex_lock(&dbs_mutex);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001159 mutex_destroy(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 dbs_enable--;
Anitha Anand3dd65092012-01-18 17:17:40 -08001161 /* If device is being removed, policy is no longer
1162 * valid. */
1163 this_dbs_info->cur_policy = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 if (!cpu)
1165 input_unregister_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001166 mutex_unlock(&dbs_mutex);
Thomas Renninger0e625ac2009-07-24 15:25:06 +02001167 if (!dbs_enable)
1168 sysfs_remove_group(cpufreq_global_kobject,
1169 &dbs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 break;
1172
1173 case CPUFREQ_GOV_LIMITS:
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001174 mutex_lock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001176 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -05001177 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001179 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -05001180 policy->min, CPUFREQ_RELATION_L);
David Ng8192a2f2012-01-19 14:16:19 -08001181 else if (dbs_tuners_ins.powersave_bias != 0)
1182 ondemand_powersave_bias_setspeed(
1183 this_dbs_info->cur_policy,
1184 policy,
1185 dbs_tuners_ins.powersave_bias);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001186 mutex_unlock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 break;
1188 }
1189 return 0;
1190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static int __init cpufreq_gov_dbs_init(void)
1193{
Andrea Righi4f6e6b92008-09-18 10:43:40 +00001194 u64 idle_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195 unsigned int i;
Andrea Righi4f6e6b92008-09-18 10:43:40 +00001196 int cpu = get_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001197
Kamalesh Babulal21f2e3c2011-12-09 16:18:42 +05301198 idle_time = get_cpu_idle_time_us(cpu, NULL);
Andrea Righi4f6e6b92008-09-18 10:43:40 +00001199 put_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001200 if (idle_time != -1ULL) {
1201 /* Idle micro accounting is supported. Use finer thresholds */
1202 dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
1203 dbs_tuners_ins.down_differential =
1204 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
Thomas Renningercef96152009-04-22 13:48:29 +02001205 /*
Paul Bollebd74b322011-08-06 14:33:43 +02001206 * In nohz/micro accounting case we set the minimum frequency
Thomas Renningercef96152009-04-22 13:48:29 +02001207 * not depending on HZ, but fixed (very low). The deferred
1208 * timer might skip some samples if idle/sleeping as needed.
1209 */
1210 min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
1211 } else {
1212 /* For correct statistics, we need 10 ticks for each measure */
1213 min_sampling_rate =
1214 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001215 }
Akinobu Mita888a7942008-07-14 12:00:45 +09001216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217 input_wq = create_workqueue("iewq");
1218 if (!input_wq) {
1219 printk(KERN_ERR "Failed to create iewq workqueue\n");
1220 return -EFAULT;
1221 }
1222 for_each_possible_cpu(i) {
Praveen Chidambaram457a4452012-07-19 10:45:07 -06001223 struct cpu_dbs_info_s *this_dbs_info =
1224 &per_cpu(od_cpu_dbs_info, i);
1225 mutex_init(&this_dbs_info->timer_mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226 INIT_WORK(&per_cpu(dbs_refresh_work, i), dbs_refresh_callback);
1227 }
1228
Tejun Heo57df5572011-01-26 12:12:50 +01001229 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232static void __exit cpufreq_gov_dbs_exit(void)
1233{
Thomas Renninger1c256242007-10-02 13:28:12 -07001234 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235 destroy_workqueue(input_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001239MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
1240MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
1241MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -05001242 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001243MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Johannes Weiner69157192008-01-17 15:21:08 -08001245#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
1246fs_initcall(cpufreq_gov_dbs_init);
1247#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -08001249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250module_exit(cpufreq_gov_dbs_exit);