blob: 3a147874a4656757f7946d082b4b369d7a21953e [file] [log] [blame]
Dave Jonesb9170832005-05-31 19:03:47 -07001/*
2 * drivers/cpufreq/cpufreq_conservative.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com>
Alexander Clouter11a80a9c762009-02-13 19:01:01 +00007 * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
Dave Jonesb9170832005-05-31 19:03:47 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
Dave Jonesb9170832005-05-31 19:03:47 -070016#include <linux/init.h>
Dave Jonesb9170832005-05-31 19:03:47 -070017#include <linux/cpufreq.h>
Andrew Morton138a01282006-06-23 03:31:19 -070018#include <linux/cpu.h>
Dave Jonesb9170832005-05-31 19:03:47 -070019#include <linux/jiffies.h>
20#include <linux/kernel_stat.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080021#include <linux/mutex.h>
Alexander Clouter8e677ce2009-02-13 19:02:34 +000022#include <linux/hrtimer.h>
23#include <linux/tick.h>
24#include <linux/ktime.h>
25#include <linux/sched.h>
26
Dave Jonesb9170832005-05-31 19:03:47 -070027/*
28 * dbs is used in this file as a shortform for demandbased switching
29 * It helps to keep variable names smaller, simpler
30 */
31
32#define DEF_FREQUENCY_UP_THRESHOLD (80)
Dave Jonesb9170832005-05-31 19:03:47 -070033#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
Dave Jonesb9170832005-05-31 19:03:47 -070034
Dave Jones18a72472007-10-22 16:49:09 -040035/*
36 * The polling frequency of this governor depends on the capability of
Dave Jonesb9170832005-05-31 19:03:47 -070037 * the processor. Default polling frequency is 1000 times the transition
Dave Jones18a72472007-10-22 16:49:09 -040038 * latency of the processor. The governor will work on any processor with
39 * transition latency <= 10mS, using appropriate sampling
Dave Jonesb9170832005-05-31 19:03:47 -070040 * rate.
Alexander Clouter8e677ce2009-02-13 19:02:34 +000041 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
42 * this governor will not work.
Dave Jonesb9170832005-05-31 19:03:47 -070043 * All times here are in uS.
44 */
Alexander Clouter2c906b32006-03-22 09:54:10 +000045#define MIN_SAMPLING_RATE_RATIO (2)
Thomas Renninger112124a2009-02-04 11:55:12 +010046
Thomas Renningercef96152009-04-22 13:48:29 +020047static unsigned int min_sampling_rate;
48
Thomas Renninger112124a2009-02-04 11:55:12 +010049#define LATENCY_MULTIPLIER (1000)
Thomas Renningercef96152009-04-22 13:48:29 +020050#define MIN_LATENCY_MULTIPLIER (100)
Alexander Clouter2c906b32006-03-22 09:54:10 +000051#define DEF_SAMPLING_DOWN_FACTOR (1)
52#define MAX_SAMPLING_DOWN_FACTOR (10)
Thomas Renninger1c256242007-10-02 13:28:12 -070053#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
Dave Jonesb9170832005-05-31 19:03:47 -070054
David Howellsc4028952006-11-22 14:57:56 +000055static void do_dbs_timer(struct work_struct *work);
Dave Jonesb9170832005-05-31 19:03:47 -070056
57struct cpu_dbs_info_s {
Alexander Clouter8e677ce2009-02-13 19:02:34 +000058 cputime64_t prev_cpu_idle;
59 cputime64_t prev_cpu_wall;
60 cputime64_t prev_cpu_nice;
Dave Jones18a72472007-10-22 16:49:09 -040061 struct cpufreq_policy *cur_policy;
Alexander Clouter8e677ce2009-02-13 19:02:34 +000062 struct delayed_work work;
Dave Jones18a72472007-10-22 16:49:09 -040063 unsigned int down_skip;
64 unsigned int requested_freq;
Alexander Clouter8e677ce2009-02-13 19:02:34 +000065 int cpu;
66 unsigned int enable:1;
venkatesh.pallipadi@intel.comee884152009-07-02 17:08:33 -070067 /*
68 * percpu mutex that serializes governor limit change with
69 * do_dbs_timer invocation. We do not want do_dbs_timer to run
70 * when user is changing the governor or limits.
71 */
72 struct mutex timer_mutex;
Dave Jonesb9170832005-05-31 19:03:47 -070073};
Tejun Heo245b2e72009-06-24 15:13:48 +090074static DEFINE_PER_CPU(struct cpu_dbs_info_s, cs_cpu_dbs_info);
Dave Jonesb9170832005-05-31 19:03:47 -070075
76static unsigned int dbs_enable; /* number of CPUs using this policy */
77
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -070078/*
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -070079 * dbs_mutex protects data in dbs_tuners_ins from concurrent changes on
venkatesh.pallipadi@intel.comee884152009-07-02 17:08:33 -070080 * different CPUs. It protects dbs_enable in governor start/stop.
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -070081 */
Dave Jones9acef482009-01-18 01:39:51 -050082static DEFINE_MUTEX(dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -070083
Alexander Clouter8e677ce2009-02-13 19:02:34 +000084static struct workqueue_struct *kconservative_wq;
85
86static struct dbs_tuners {
Dave Jones18a72472007-10-22 16:49:09 -040087 unsigned int sampling_rate;
88 unsigned int sampling_down_factor;
89 unsigned int up_threshold;
90 unsigned int down_threshold;
91 unsigned int ignore_nice;
92 unsigned int freq_step;
Alexander Clouter8e677ce2009-02-13 19:02:34 +000093} dbs_tuners_ins = {
Dave Jones18a72472007-10-22 16:49:09 -040094 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
95 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
96 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
97 .ignore_nice = 0,
98 .freq_step = 5,
Dave Jonesb9170832005-05-31 19:03:47 -070099};
100
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000101static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
102 cputime64_t *wall)
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700103{
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000104 cputime64_t idle_time;
105 cputime64_t cur_wall_time;
106 cputime64_t busy_time;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530107
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000108 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
109 busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
110 kstat_cpu(cpu).cpustat.system);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530111
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000112 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
113 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
114 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
115 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530116
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000117 idle_time = cputime64_sub(cur_wall_time, busy_time);
118 if (wall)
Pallipadi, Venkatesh54c9a352009-11-11 16:50:29 -0800119 *wall = (cputime64_t)jiffies_to_usecs(cur_wall_time);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000120
Pallipadi, Venkatesh54c9a352009-11-11 16:50:29 -0800121 return (cputime64_t)jiffies_to_usecs(idle_time);;
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000122}
123
124static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
125{
126 u64 idle_time = get_cpu_idle_time_us(cpu, wall);
127
128 if (idle_time == -1ULL)
129 return get_cpu_idle_time_jiffy(cpu, wall);
130
131 return idle_time;
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700132}
133
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200134/* keep track of frequency transitions */
135static int
136dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
137 void *data)
138{
139 struct cpufreq_freqs *freq = data;
Tejun Heo245b2e72009-06-24 15:13:48 +0900140 struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cs_cpu_dbs_info,
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200141 freq->cpu);
142
Alexander Clouterf407a082009-02-13 19:01:51 +0000143 struct cpufreq_policy *policy;
144
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200145 if (!this_dbs_info->enable)
146 return 0;
147
Alexander Clouterf407a082009-02-13 19:01:51 +0000148 policy = this_dbs_info->cur_policy;
149
150 /*
151 * we only care if our internally tracked freq moves outside
152 * the 'valid' ranges of freqency available to us otherwise
153 * we do not change it
154 */
155 if (this_dbs_info->requested_freq > policy->max
156 || this_dbs_info->requested_freq < policy->min)
157 this_dbs_info->requested_freq = freq->new;
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200158
159 return 0;
160}
161
162static struct notifier_block dbs_cpufreq_notifier_block = {
163 .notifier_call = dbs_cpufreq_notifier
164};
165
Dave Jonesb9170832005-05-31 19:03:47 -0700166/************************** sysfs interface ************************/
Thomas Renninger49b015c2009-10-01 19:49:28 +0200167static ssize_t show_sampling_rate_max(struct kobject *kobj,
168 struct attribute *attr, char *buf)
Dave Jonesb9170832005-05-31 19:03:47 -0700169{
Thomas Renninger4f4d1ad2009-04-22 13:48:31 +0200170 printk_once(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
171 "sysfs file is deprecated - used by: %s\n", current->comm);
Thomas Renningercef96152009-04-22 13:48:29 +0200172 return sprintf(buf, "%u\n", -1U);
Dave Jonesb9170832005-05-31 19:03:47 -0700173}
174
Thomas Renninger49b015c2009-10-01 19:49:28 +0200175static ssize_t show_sampling_rate_min(struct kobject *kobj,
176 struct attribute *attr, char *buf)
Dave Jonesb9170832005-05-31 19:03:47 -0700177{
Thomas Renningercef96152009-04-22 13:48:29 +0200178 return sprintf(buf, "%u\n", min_sampling_rate);
Dave Jonesb9170832005-05-31 19:03:47 -0700179}
180
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000181#define define_one_ro(_name) \
Thomas Renninger49b015c2009-10-01 19:49:28 +0200182static struct global_attr _name = \
Dave Jonesb9170832005-05-31 19:03:47 -0700183__ATTR(_name, 0444, show_##_name, NULL)
184
185define_one_ro(sampling_rate_max);
186define_one_ro(sampling_rate_min);
187
188/* cpufreq_conservative Governor Tunables */
189#define show_one(file_name, object) \
190static ssize_t show_##file_name \
Thomas Renninger49b015c2009-10-01 19:49:28 +0200191(struct kobject *kobj, struct attribute *attr, char *buf) \
Dave Jonesb9170832005-05-31 19:03:47 -0700192{ \
193 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
194}
195show_one(sampling_rate, sampling_rate);
196show_one(sampling_down_factor, sampling_down_factor);
197show_one(up_threshold, up_threshold);
198show_one(down_threshold, down_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800199show_one(ignore_nice_load, ignore_nice);
Dave Jonesb9170832005-05-31 19:03:47 -0700200show_one(freq_step, freq_step);
201
Thomas Renninger49b015c2009-10-01 19:49:28 +0200202/*** delete after deprecation time ***/
203#define DEPRECATION_MSG(file_name) \
204 printk_once(KERN_INFO "CPUFREQ: Per core conservative sysfs " \
205 "interface is deprecated - " #file_name "\n");
206
207#define show_one_old(file_name) \
208static ssize_t show_##file_name##_old \
209(struct cpufreq_policy *unused, char *buf) \
210{ \
211 printk_once(KERN_INFO "CPUFREQ: Per core conservative sysfs " \
212 "interface is deprecated - " #file_name "\n"); \
213 return show_##file_name(NULL, NULL, buf); \
214}
215show_one_old(sampling_rate);
216show_one_old(sampling_down_factor);
217show_one_old(up_threshold);
218show_one_old(down_threshold);
219show_one_old(ignore_nice_load);
220show_one_old(freq_step);
221show_one_old(sampling_rate_min);
222show_one_old(sampling_rate_max);
223
224#define define_one_ro_old(object, _name) \
225static struct freq_attr object = \
226__ATTR(_name, 0444, show_##_name##_old, NULL)
227
228define_one_ro_old(sampling_rate_min_old, sampling_rate_min);
229define_one_ro_old(sampling_rate_max_old, sampling_rate_max);
230
231/*** delete after deprecation time ***/
232
233static ssize_t store_sampling_down_factor(struct kobject *a,
234 struct attribute *b,
235 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700236{
237 unsigned int input;
238 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500239 ret = sscanf(buf, "%u", &input);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000240
Alexander Clouter2c906b32006-03-22 09:54:10 +0000241 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700242 return -EINVAL;
243
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800244 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700245 dbs_tuners_ins.sampling_down_factor = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800246 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700247
248 return count;
249}
250
Thomas Renninger49b015c2009-10-01 19:49:28 +0200251static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
252 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700253{
254 unsigned int input;
255 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500256 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700257
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000258 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700259 return -EINVAL;
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000260
261 mutex_lock(&dbs_mutex);
Thomas Renningercef96152009-04-22 13:48:29 +0200262 dbs_tuners_ins.sampling_rate = max(input, min_sampling_rate);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800263 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700264
265 return count;
266}
267
Thomas Renninger49b015c2009-10-01 19:49:28 +0200268static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
269 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700270{
271 unsigned int input;
272 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500273 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700274
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800275 mutex_lock(&dbs_mutex);
Dave Jones9acef482009-01-18 01:39:51 -0500276 if (ret != 1 || input > 100 ||
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000277 input <= dbs_tuners_ins.down_threshold) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800278 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700279 return -EINVAL;
280 }
281
282 dbs_tuners_ins.up_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800283 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700284
285 return count;
286}
287
Thomas Renninger49b015c2009-10-01 19:49:28 +0200288static ssize_t store_down_threshold(struct kobject *a, struct attribute *b,
289 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700290{
291 unsigned int input;
292 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500293 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700294
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800295 mutex_lock(&dbs_mutex);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000296 /* cannot be lower than 11 otherwise freq will not fall */
297 if (ret != 1 || input < 11 || input > 100 ||
298 input >= dbs_tuners_ins.up_threshold) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800299 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700300 return -EINVAL;
301 }
302
303 dbs_tuners_ins.down_threshold = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800304 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700305
306 return count;
307}
308
Thomas Renninger49b015c2009-10-01 19:49:28 +0200309static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
310 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700311{
312 unsigned int input;
313 int ret;
314
315 unsigned int j;
Dave Jones18a72472007-10-22 16:49:09 -0400316
317 ret = sscanf(buf, "%u", &input);
318 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700319 return -EINVAL;
320
Dave Jones18a72472007-10-22 16:49:09 -0400321 if (input > 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700322 input = 1;
Dave Jones18a72472007-10-22 16:49:09 -0400323
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800324 mutex_lock(&dbs_mutex);
Dave Jones18a72472007-10-22 16:49:09 -0400325 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800326 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700327 return count;
328 }
329 dbs_tuners_ins.ignore_nice = input;
330
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000331 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700332 for_each_online_cpu(j) {
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000333 struct cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900334 dbs_info = &per_cpu(cs_cpu_dbs_info, j);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000335 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
336 &dbs_info->prev_cpu_wall);
337 if (dbs_tuners_ins.ignore_nice)
338 dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
Dave Jonesb9170832005-05-31 19:03:47 -0700339 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800340 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700341
342 return count;
343}
344
Thomas Renninger49b015c2009-10-01 19:49:28 +0200345static ssize_t store_freq_step(struct kobject *a, struct attribute *b,
346 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700347{
348 unsigned int input;
349 int ret;
Dave Jones18a72472007-10-22 16:49:09 -0400350 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700351
Dave Jones18a72472007-10-22 16:49:09 -0400352 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700353 return -EINVAL;
354
Dave Jones18a72472007-10-22 16:49:09 -0400355 if (input > 100)
Dave Jonesb9170832005-05-31 19:03:47 -0700356 input = 100;
Dave Jones18a72472007-10-22 16:49:09 -0400357
Dave Jonesb9170832005-05-31 19:03:47 -0700358 /* no need to test here if freq_step is zero as the user might actually
359 * want this, they would be crazy though :) */
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800360 mutex_lock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700361 dbs_tuners_ins.freq_step = input;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800362 mutex_unlock(&dbs_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700363
364 return count;
365}
366
367#define define_one_rw(_name) \
Thomas Renninger49b015c2009-10-01 19:49:28 +0200368static struct global_attr _name = \
Dave Jonesb9170832005-05-31 19:03:47 -0700369__ATTR(_name, 0644, show_##_name, store_##_name)
370
371define_one_rw(sampling_rate);
372define_one_rw(sampling_down_factor);
373define_one_rw(up_threshold);
374define_one_rw(down_threshold);
Alexander Clouter001893c2005-12-01 01:09:25 -0800375define_one_rw(ignore_nice_load);
Dave Jonesb9170832005-05-31 19:03:47 -0700376define_one_rw(freq_step);
377
Dave Jones9acef482009-01-18 01:39:51 -0500378static struct attribute *dbs_attributes[] = {
Dave Jonesb9170832005-05-31 19:03:47 -0700379 &sampling_rate_max.attr,
380 &sampling_rate_min.attr,
381 &sampling_rate.attr,
382 &sampling_down_factor.attr,
383 &up_threshold.attr,
384 &down_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800385 &ignore_nice_load.attr,
Dave Jonesb9170832005-05-31 19:03:47 -0700386 &freq_step.attr,
387 NULL
388};
389
390static struct attribute_group dbs_attr_group = {
391 .attrs = dbs_attributes,
392 .name = "conservative",
393};
394
Thomas Renninger49b015c2009-10-01 19:49:28 +0200395/*** delete after deprecation time ***/
396
397#define write_one_old(file_name) \
398static ssize_t store_##file_name##_old \
399(struct cpufreq_policy *unused, const char *buf, size_t count) \
400{ \
401 printk_once(KERN_INFO "CPUFREQ: Per core conservative sysfs " \
402 "interface is deprecated - " #file_name "\n"); \
403 return store_##file_name(NULL, NULL, buf, count); \
404}
405write_one_old(sampling_rate);
406write_one_old(sampling_down_factor);
407write_one_old(up_threshold);
408write_one_old(down_threshold);
409write_one_old(ignore_nice_load);
410write_one_old(freq_step);
411
412#define define_one_rw_old(object, _name) \
413static struct freq_attr object = \
414__ATTR(_name, 0644, show_##_name##_old, store_##_name##_old)
415
416define_one_rw_old(sampling_rate_old, sampling_rate);
417define_one_rw_old(sampling_down_factor_old, sampling_down_factor);
418define_one_rw_old(up_threshold_old, up_threshold);
419define_one_rw_old(down_threshold_old, down_threshold);
420define_one_rw_old(ignore_nice_load_old, ignore_nice_load);
421define_one_rw_old(freq_step_old, freq_step);
422
423static struct attribute *dbs_attributes_old[] = {
424 &sampling_rate_max_old.attr,
425 &sampling_rate_min_old.attr,
426 &sampling_rate_old.attr,
427 &sampling_down_factor_old.attr,
428 &up_threshold_old.attr,
429 &down_threshold_old.attr,
430 &ignore_nice_load_old.attr,
431 &freq_step_old.attr,
432 NULL
433};
434
435static struct attribute_group dbs_attr_group_old = {
436 .attrs = dbs_attributes_old,
437 .name = "conservative",
438};
439
440/*** delete after deprecation time ***/
441
Dave Jonesb9170832005-05-31 19:03:47 -0700442/************************** sysfs end ************************/
443
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000444static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Dave Jonesb9170832005-05-31 19:03:47 -0700445{
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000446 unsigned int load = 0;
Dominik Brodowskifd187aa2010-03-26 10:01:34 +0100447 unsigned int max_load = 0;
Dave Jonesf068c042008-07-30 12:59:56 -0400448 unsigned int freq_target;
Dave Jonesb9170832005-05-31 19:03:47 -0700449
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000450 struct cpufreq_policy *policy;
451 unsigned int j;
Dave Jonesb9170832005-05-31 19:03:47 -0700452
Alexander Clouter08a28e22006-03-22 09:59:16 +0000453 policy = this_dbs_info->cur_policy;
454
Dave Jones18a72472007-10-22 16:49:09 -0400455 /*
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000456 * Every sampling_rate, we check, if current idle time is less
457 * than 20% (default), then we try to increase frequency
458 * Every sampling_rate*sampling_down_factor, we check, if current
459 * idle time is more than 80%, then we try to decrease frequency
Dave Jonesb9170832005-05-31 19:03:47 -0700460 *
Dave Jones18a72472007-10-22 16:49:09 -0400461 * Any frequency increase takes it to the maximum frequency.
462 * Frequency reduction happens at minimum steps of
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000463 * 5% (default) of maximum frequency
Dave Jonesb9170832005-05-31 19:03:47 -0700464 */
465
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000466 /* Get Absolute Load */
467 for_each_cpu(j, policy->cpus) {
468 struct cpu_dbs_info_s *j_dbs_info;
469 cputime64_t cur_wall_time, cur_idle_time;
470 unsigned int idle_time, wall_time;
471
Tejun Heo245b2e72009-06-24 15:13:48 +0900472 j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000473
474 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
475
476 wall_time = (unsigned int) cputime64_sub(cur_wall_time,
477 j_dbs_info->prev_cpu_wall);
478 j_dbs_info->prev_cpu_wall = cur_wall_time;
479
480 idle_time = (unsigned int) cputime64_sub(cur_idle_time,
481 j_dbs_info->prev_cpu_idle);
482 j_dbs_info->prev_cpu_idle = cur_idle_time;
483
484 if (dbs_tuners_ins.ignore_nice) {
485 cputime64_t cur_nice;
486 unsigned long cur_nice_jiffies;
487
488 cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
489 j_dbs_info->prev_cpu_nice);
490 /*
491 * Assumption: nice time between sampling periods will
492 * be less than 2^32 jiffies for 32 bit sys
493 */
494 cur_nice_jiffies = (unsigned long)
495 cputime64_to_jiffies64(cur_nice);
496
497 j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
498 idle_time += jiffies_to_usecs(cur_nice_jiffies);
499 }
500
501 if (unlikely(!wall_time || wall_time < idle_time))
502 continue;
503
504 load = 100 * (wall_time - idle_time) / wall_time;
Dominik Brodowskifd187aa2010-03-26 10:01:34 +0100505
506 if (load > max_load)
507 max_load = load;
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000508 }
509
510 /*
511 * break out if we 'cannot' reduce the speed as the user might
512 * want freq_step to be zero
513 */
514 if (dbs_tuners_ins.freq_step == 0)
515 return;
Dave Jonesb9170832005-05-31 19:03:47 -0700516
Alexander Clouter08a28e22006-03-22 09:59:16 +0000517 /* Check for frequency increase */
Dominik Brodowskifd187aa2010-03-26 10:01:34 +0100518 if (max_load > dbs_tuners_ins.up_threshold) {
Alexander Cloutera159b822006-03-22 10:00:18 +0000519 this_dbs_info->down_skip = 0;
Dave Jones790d76f2005-05-31 19:03:49 -0700520
Dave Jonesb9170832005-05-31 19:03:47 -0700521 /* if we are already at full speed then break out early */
Alexander Cloutera159b822006-03-22 10:00:18 +0000522 if (this_dbs_info->requested_freq == policy->max)
Dave Jonesb9170832005-05-31 19:03:47 -0700523 return;
Dave Jones18a72472007-10-22 16:49:09 -0400524
Dave Jonesf068c042008-07-30 12:59:56 -0400525 freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
Dave Jonesb9170832005-05-31 19:03:47 -0700526
527 /* max freq cannot be less than 100. But who knows.... */
Dave Jonesf068c042008-07-30 12:59:56 -0400528 if (unlikely(freq_target == 0))
529 freq_target = 5;
Dave Jones18a72472007-10-22 16:49:09 -0400530
Dave Jonesf068c042008-07-30 12:59:56 -0400531 this_dbs_info->requested_freq += freq_target;
Alexander Cloutera159b822006-03-22 10:00:18 +0000532 if (this_dbs_info->requested_freq > policy->max)
533 this_dbs_info->requested_freq = policy->max;
Dave Jonesb9170832005-05-31 19:03:47 -0700534
Alexander Cloutera159b822006-03-22 10:00:18 +0000535 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
Dave Jonesb9170832005-05-31 19:03:47 -0700536 CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700537 return;
538 }
539
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000540 /*
541 * The optimal frequency is the frequency that is the lowest that
542 * can support the current CPU usage without triggering the up
543 * policy. To be safe, we focus 10 points under the threshold.
544 */
Dominik Brodowskifd187aa2010-03-26 10:01:34 +0100545 if (max_load < (dbs_tuners_ins.down_threshold - 10)) {
Dave Jonesf068c042008-07-30 12:59:56 -0400546 freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
Dave Jonesb9170832005-05-31 19:03:47 -0700547
Dave Jonesf068c042008-07-30 12:59:56 -0400548 this_dbs_info->requested_freq -= freq_target;
Alexander Cloutera159b822006-03-22 10:00:18 +0000549 if (this_dbs_info->requested_freq < policy->min)
550 this_dbs_info->requested_freq = policy->min;
Dave Jonesb9170832005-05-31 19:03:47 -0700551
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000552 /*
553 * if we cannot reduce the frequency anymore, break out early
554 */
555 if (policy->cur == policy->min)
556 return;
557
Alexander Cloutera159b822006-03-22 10:00:18 +0000558 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
Alexander Clouter2c906b32006-03-22 09:54:10 +0000559 CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700560 return;
561 }
562}
563
David Howellsc4028952006-11-22 14:57:56 +0000564static void do_dbs_timer(struct work_struct *work)
Dave Jones18a72472007-10-22 16:49:09 -0400565{
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000566 struct cpu_dbs_info_s *dbs_info =
567 container_of(work, struct cpu_dbs_info_s, work.work);
568 unsigned int cpu = dbs_info->cpu;
569
570 /* We want all CPUs to do sampling nearly on same jiffy */
571 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
572
573 delay -= jiffies % delay;
574
venkatesh.pallipadi@intel.comee884152009-07-02 17:08:33 -0700575 mutex_lock(&dbs_info->timer_mutex);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000576
577 dbs_check_cpu(dbs_info);
578
579 queue_delayed_work_on(cpu, kconservative_wq, &dbs_info->work, delay);
venkatesh.pallipadi@intel.comee884152009-07-02 17:08:33 -0700580 mutex_unlock(&dbs_info->timer_mutex);
Dave Jones18a72472007-10-22 16:49:09 -0400581}
Dave Jonesb9170832005-05-31 19:03:47 -0700582
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000583static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Dave Jonesb9170832005-05-31 19:03:47 -0700584{
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000585 /* We want all CPUs to do sampling nearly on same jiffy */
586 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
587 delay -= jiffies % delay;
588
589 dbs_info->enable = 1;
590 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
591 queue_delayed_work_on(dbs_info->cpu, kconservative_wq, &dbs_info->work,
592 delay);
Dave Jonesb9170832005-05-31 19:03:47 -0700593}
594
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000595static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Dave Jonesb9170832005-05-31 19:03:47 -0700596{
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000597 dbs_info->enable = 0;
Mathieu Desnoyersb253d2b2009-05-17 10:29:33 -0400598 cancel_delayed_work_sync(&dbs_info->work);
Dave Jonesb9170832005-05-31 19:03:47 -0700599}
600
601static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
602 unsigned int event)
603{
604 unsigned int cpu = policy->cpu;
605 struct cpu_dbs_info_s *this_dbs_info;
606 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700607 int rc;
Dave Jonesb9170832005-05-31 19:03:47 -0700608
Tejun Heo245b2e72009-06-24 15:13:48 +0900609 this_dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
Dave Jonesb9170832005-05-31 19:03:47 -0700610
611 switch (event) {
612 case CPUFREQ_GOV_START:
Dave Jones18a72472007-10-22 16:49:09 -0400613 if ((!cpu_online(cpu)) || (!policy->cur))
Dave Jonesb9170832005-05-31 19:03:47 -0700614 return -EINVAL;
615
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800616 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700617
Thomas Renninger49b015c2009-10-01 19:49:28 +0200618 rc = sysfs_create_group(&policy->kobj, &dbs_attr_group_old);
Jeff Garzik914f7c32006-10-20 14:31:00 -0700619 if (rc) {
620 mutex_unlock(&dbs_mutex);
621 return rc;
622 }
623
Rusty Russell835481d2009-01-04 05:18:06 -0800624 for_each_cpu(j, policy->cpus) {
Dave Jonesb9170832005-05-31 19:03:47 -0700625 struct cpu_dbs_info_s *j_dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900626 j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
Dave Jonesb9170832005-05-31 19:03:47 -0700627 j_dbs_info->cur_policy = policy;
Dave Jones18a72472007-10-22 16:49:09 -0400628
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000629 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
630 &j_dbs_info->prev_cpu_wall);
631 if (dbs_tuners_ins.ignore_nice) {
632 j_dbs_info->prev_cpu_nice =
633 kstat_cpu(j).cpustat.nice;
634 }
Dave Jonesb9170832005-05-31 19:03:47 -0700635 }
Alexander Cloutera159b822006-03-22 10:00:18 +0000636 this_dbs_info->down_skip = 0;
637 this_dbs_info->requested_freq = policy->cur;
Jeff Garzik914f7c32006-10-20 14:31:00 -0700638
venkatesh.pallipadi@intel.comee884152009-07-02 17:08:33 -0700639 mutex_init(&this_dbs_info->timer_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700640 dbs_enable++;
641 /*
642 * Start the timerschedule work, when this governor
643 * is used for first time
644 */
645 if (dbs_enable == 1) {
646 unsigned int latency;
647 /* policy latency is in nS. Convert it to uS first */
Alexander Clouter2c906b32006-03-22 09:54:10 +0000648 latency = policy->cpuinfo.transition_latency / 1000;
649 if (latency == 0)
650 latency = 1;
Dave Jonesb9170832005-05-31 19:03:47 -0700651
Thomas Renninger49b015c2009-10-01 19:49:28 +0200652 rc = sysfs_create_group(cpufreq_global_kobject,
653 &dbs_attr_group);
654 if (rc) {
655 mutex_unlock(&dbs_mutex);
656 return rc;
657 }
658
Thomas Renningercef96152009-04-22 13:48:29 +0200659 /*
660 * conservative does not implement micro like ondemand
661 * governor, thus we are bound to jiffes/HZ
662 */
663 min_sampling_rate =
664 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
665 /* Bring kernel and HW constraints together */
666 min_sampling_rate = max(min_sampling_rate,
667 MIN_LATENCY_MULTIPLIER * latency);
668 dbs_tuners_ins.sampling_rate =
669 max(min_sampling_rate,
670 latency * LATENCY_MULTIPLIER);
Dave Jonesb9170832005-05-31 19:03:47 -0700671
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200672 cpufreq_register_notifier(
673 &dbs_cpufreq_notifier_block,
674 CPUFREQ_TRANSITION_NOTIFIER);
Dave Jonesb9170832005-05-31 19:03:47 -0700675 }
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800676 mutex_unlock(&dbs_mutex);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000677
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700678 dbs_timer_init(this_dbs_info);
679
Dave Jonesb9170832005-05-31 19:03:47 -0700680 break;
681
682 case CPUFREQ_GOV_STOP:
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000683 dbs_timer_exit(this_dbs_info);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -0700684
685 mutex_lock(&dbs_mutex);
Thomas Renninger49b015c2009-10-01 19:49:28 +0200686 sysfs_remove_group(&policy->kobj, &dbs_attr_group_old);
Dave Jonesb9170832005-05-31 19:03:47 -0700687 dbs_enable--;
venkatesh.pallipadi@intel.comee884152009-07-02 17:08:33 -0700688 mutex_destroy(&this_dbs_info->timer_mutex);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000689
Dave Jonesb9170832005-05-31 19:03:47 -0700690 /*
691 * Stop the timerschedule work, when this governor
692 * is used for first time
693 */
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000694 if (dbs_enable == 0)
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200695 cpufreq_unregister_notifier(
696 &dbs_cpufreq_notifier_block,
697 CPUFREQ_TRANSITION_NOTIFIER);
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200698
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800699 mutex_unlock(&dbs_mutex);
Thomas Renninger49b015c2009-10-01 19:49:28 +0200700 if (!dbs_enable)
701 sysfs_remove_group(cpufreq_global_kobject,
702 &dbs_attr_group);
Dave Jonesb9170832005-05-31 19:03:47 -0700703
704 break;
705
706 case CPUFREQ_GOV_LIMITS:
venkatesh.pallipadi@intel.comee884152009-07-02 17:08:33 -0700707 mutex_lock(&this_dbs_info->timer_mutex);
Dave Jonesb9170832005-05-31 19:03:47 -0700708 if (policy->max < this_dbs_info->cur_policy->cur)
709 __cpufreq_driver_target(
710 this_dbs_info->cur_policy,
Dave Jones18a72472007-10-22 16:49:09 -0400711 policy->max, CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700712 else if (policy->min > this_dbs_info->cur_policy->cur)
713 __cpufreq_driver_target(
714 this_dbs_info->cur_policy,
Dave Jones18a72472007-10-22 16:49:09 -0400715 policy->min, CPUFREQ_RELATION_L);
venkatesh.pallipadi@intel.comee884152009-07-02 17:08:33 -0700716 mutex_unlock(&this_dbs_info->timer_mutex);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000717
Dave Jonesb9170832005-05-31 19:03:47 -0700718 break;
719 }
720 return 0;
721}
722
Sven Wegenerc4d14bc2008-09-20 16:50:08 +0200723#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
724static
725#endif
Thomas Renninger1c256242007-10-02 13:28:12 -0700726struct cpufreq_governor cpufreq_gov_conservative = {
727 .name = "conservative",
728 .governor = cpufreq_governor_dbs,
729 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
730 .owner = THIS_MODULE,
Dave Jonesb9170832005-05-31 19:03:47 -0700731};
732
733static int __init cpufreq_gov_dbs_init(void)
734{
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000735 int err;
736
737 kconservative_wq = create_workqueue("kconservative");
738 if (!kconservative_wq) {
739 printk(KERN_ERR "Creation of kconservative failed\n");
740 return -EFAULT;
741 }
742
743 err = cpufreq_register_governor(&cpufreq_gov_conservative);
744 if (err)
745 destroy_workqueue(kconservative_wq);
746
747 return err;
Dave Jonesb9170832005-05-31 19:03:47 -0700748}
749
750static void __exit cpufreq_gov_dbs_exit(void)
751{
Thomas Renninger1c256242007-10-02 13:28:12 -0700752 cpufreq_unregister_governor(&cpufreq_gov_conservative);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000753 destroy_workqueue(kconservative_wq);
Dave Jonesb9170832005-05-31 19:03:47 -0700754}
755
756
Alexander Clouter11a80a9c762009-02-13 19:01:01 +0000757MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
Dave Jones9acef482009-01-18 01:39:51 -0500758MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
Dave Jonesb9170832005-05-31 19:03:47 -0700759 "Low Latency Frequency Transition capable processors "
760 "optimised for use in a battery environment");
Dave Jones9acef482009-01-18 01:39:51 -0500761MODULE_LICENSE("GPL");
Dave Jonesb9170832005-05-31 19:03:47 -0700762
Johannes Weiner69157192008-01-17 15:21:08 -0800763#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
764fs_initcall(cpufreq_gov_dbs_init);
765#else
Dave Jonesb9170832005-05-31 19:03:47 -0700766module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800767#endif
Dave Jonesb9170832005-05-31 19:03:47 -0700768module_exit(cpufreq_gov_dbs_exit);