aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/cpufreq/cpufreq_conservative.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/cpufreq/cpufreq_conservative.c')
-rw-r--r--drivers/cpufreq/cpufreq_conservative.c404
1 files changed, 244 insertions, 160 deletions
diff --git a/drivers/cpufreq/cpufreq_conservative.c b/drivers/cpufreq/cpufreq_conservative.c
index 0320962c4ec5..2ecd95e4ab1a 100644
--- a/drivers/cpufreq/cpufreq_conservative.c
+++ b/drivers/cpufreq/cpufreq_conservative.c
@@ -4,7 +4,7 @@
4 * Copyright (C) 2001 Russell King 4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. 5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com> 6 * Jun Nakajima <jun.nakajima@intel.com>
7 * (C) 2004 Alexander Clouter <alex-kernel@digriz.org.uk> 7 * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 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 10 * it under the terms of the GNU General Public License version 2 as
@@ -13,22 +13,17 @@
13 13
14#include <linux/kernel.h> 14#include <linux/kernel.h>
15#include <linux/module.h> 15#include <linux/module.h>
16#include <linux/smp.h>
17#include <linux/init.h> 16#include <linux/init.h>
18#include <linux/interrupt.h>
19#include <linux/ctype.h>
20#include <linux/cpufreq.h> 17#include <linux/cpufreq.h>
21#include <linux/sysctl.h>
22#include <linux/types.h>
23#include <linux/fs.h>
24#include <linux/sysfs.h>
25#include <linux/cpu.h> 18#include <linux/cpu.h>
26#include <linux/kmod.h>
27#include <linux/workqueue.h>
28#include <linux/jiffies.h> 19#include <linux/jiffies.h>
29#include <linux/kernel_stat.h> 20#include <linux/kernel_stat.h>
30#include <linux/percpu.h>
31#include <linux/mutex.h> 21#include <linux/mutex.h>
22#include <linux/hrtimer.h>
23#include <linux/tick.h>
24#include <linux/ktime.h>
25#include <linux/sched.h>
26
32/* 27/*
33 * dbs is used in this file as a shortform for demandbased switching 28 * dbs is used in this file as a shortform for demandbased switching
34 * It helps to keep variable names smaller, simpler 29 * It helps to keep variable names smaller, simpler
@@ -43,19 +38,31 @@
43 * latency of the processor. The governor will work on any processor with 38 * latency of the processor. The governor will work on any processor with
44 * transition latency <= 10mS, using appropriate sampling 39 * transition latency <= 10mS, using appropriate sampling
45 * rate. 40 * rate.
46 * For CPUs with transition latency > 10mS (mostly drivers 41 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
47 * with CPUFREQ_ETERNAL), this governor will not work. 42 * this governor will not work.
48 * All times here are in uS. 43 * All times here are in uS.
49 */ 44 */
50static unsigned int def_sampling_rate; 45static unsigned int def_sampling_rate;
51#define MIN_SAMPLING_RATE_RATIO (2) 46#define MIN_SAMPLING_RATE_RATIO (2)
52/* for correct statistics, we need at least 10 ticks between each measure */ 47/* for correct statistics, we need at least 10 ticks between each measure */
53#define MIN_STAT_SAMPLING_RATE \ 48#define MIN_STAT_SAMPLING_RATE \
54 (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10)) 49 (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
55#define MIN_SAMPLING_RATE \ 50#define MIN_SAMPLING_RATE \
56 (def_sampling_rate / MIN_SAMPLING_RATE_RATIO) 51 (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
52/* Above MIN_SAMPLING_RATE will vanish with its sysfs file soon
53 * Define the minimal settable sampling rate to the greater of:
54 * - "HW transition latency" * 100 (same as default sampling / 10)
55 * - MIN_STAT_SAMPLING_RATE
56 * To avoid that userspace shoots itself.
57*/
58static unsigned int minimum_sampling_rate(void)
59{
60 return max(def_sampling_rate / 10, MIN_STAT_SAMPLING_RATE);
61}
62
63/* This will also vanish soon with removing sampling_rate_max */
57#define MAX_SAMPLING_RATE (500 * def_sampling_rate) 64#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
58#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000) 65#define LATENCY_MULTIPLIER (1000)
59#define DEF_SAMPLING_DOWN_FACTOR (1) 66#define DEF_SAMPLING_DOWN_FACTOR (1)
60#define MAX_SAMPLING_DOWN_FACTOR (10) 67#define MAX_SAMPLING_DOWN_FACTOR (10)
61#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000) 68#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
@@ -63,12 +70,15 @@ static unsigned int def_sampling_rate;
63static void do_dbs_timer(struct work_struct *work); 70static void do_dbs_timer(struct work_struct *work);
64 71
65struct cpu_dbs_info_s { 72struct cpu_dbs_info_s {
73 cputime64_t prev_cpu_idle;
74 cputime64_t prev_cpu_wall;
75 cputime64_t prev_cpu_nice;
66 struct cpufreq_policy *cur_policy; 76 struct cpufreq_policy *cur_policy;
67 unsigned int prev_cpu_idle_up; 77 struct delayed_work work;
68 unsigned int prev_cpu_idle_down;
69 unsigned int enable;
70 unsigned int down_skip; 78 unsigned int down_skip;
71 unsigned int requested_freq; 79 unsigned int requested_freq;
80 int cpu;
81 unsigned int enable:1;
72}; 82};
73static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info); 83static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
74 84
@@ -82,19 +92,18 @@ static unsigned int dbs_enable; /* number of CPUs using this policy */
82 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock 92 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
83 * is recursive for the same process. -Venki 93 * is recursive for the same process. -Venki
84 */ 94 */
85static DEFINE_MUTEX (dbs_mutex); 95static DEFINE_MUTEX(dbs_mutex);
86static DECLARE_DELAYED_WORK(dbs_work, do_dbs_timer);
87 96
88struct dbs_tuners { 97static struct workqueue_struct *kconservative_wq;
98
99static struct dbs_tuners {
89 unsigned int sampling_rate; 100 unsigned int sampling_rate;
90 unsigned int sampling_down_factor; 101 unsigned int sampling_down_factor;
91 unsigned int up_threshold; 102 unsigned int up_threshold;
92 unsigned int down_threshold; 103 unsigned int down_threshold;
93 unsigned int ignore_nice; 104 unsigned int ignore_nice;
94 unsigned int freq_step; 105 unsigned int freq_step;
95}; 106} dbs_tuners_ins = {
96
97static struct dbs_tuners dbs_tuners_ins = {
98 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, 107 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
99 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD, 108 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
100 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR, 109 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
@@ -102,18 +111,37 @@ static struct dbs_tuners dbs_tuners_ins = {
102 .freq_step = 5, 111 .freq_step = 5,
103}; 112};
104 113
105static inline unsigned int get_cpu_idle_time(unsigned int cpu) 114static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
115 cputime64_t *wall)
106{ 116{
107 unsigned int add_nice = 0, ret; 117 cputime64_t idle_time;
118 cputime64_t cur_wall_time;
119 cputime64_t busy_time;
108 120
109 if (dbs_tuners_ins.ignore_nice) 121 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
110 add_nice = kstat_cpu(cpu).cpustat.nice; 122 busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
123 kstat_cpu(cpu).cpustat.system);
124
125 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
126 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
127 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
128 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice);
129
130 idle_time = cputime64_sub(cur_wall_time, busy_time);
131 if (wall)
132 *wall = cur_wall_time;
133
134 return idle_time;
135}
136
137static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
138{
139 u64 idle_time = get_cpu_idle_time_us(cpu, wall);
111 140
112 ret = kstat_cpu(cpu).cpustat.idle + 141 if (idle_time == -1ULL)
113 kstat_cpu(cpu).cpustat.iowait + 142 return get_cpu_idle_time_jiffy(cpu, wall);
114 add_nice;
115 143
116 return ret; 144 return idle_time;
117} 145}
118 146
119/* keep track of frequency transitions */ 147/* keep track of frequency transitions */
@@ -125,10 +153,21 @@ dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
125 struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, 153 struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info,
126 freq->cpu); 154 freq->cpu);
127 155
156 struct cpufreq_policy *policy;
157
128 if (!this_dbs_info->enable) 158 if (!this_dbs_info->enable)
129 return 0; 159 return 0;
130 160
131 this_dbs_info->requested_freq = freq->new; 161 policy = this_dbs_info->cur_policy;
162
163 /*
164 * we only care if our internally tracked freq moves outside
165 * the 'valid' ranges of freqency available to us otherwise
166 * we do not change it
167 */
168 if (this_dbs_info->requested_freq > policy->max
169 || this_dbs_info->requested_freq < policy->min)
170 this_dbs_info->requested_freq = freq->new;
132 171
133 return 0; 172 return 0;
134} 173}
@@ -140,16 +179,31 @@ static struct notifier_block dbs_cpufreq_notifier_block = {
140/************************** sysfs interface ************************/ 179/************************** sysfs interface ************************/
141static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf) 180static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
142{ 181{
143 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE); 182 static int print_once;
183
184 if (!print_once) {
185 printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
186 "sysfs file is deprecated - used by: %s\n",
187 current->comm);
188 print_once = 1;
189 }
190 return sprintf(buf, "%u\n", MAX_SAMPLING_RATE);
144} 191}
145 192
146static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf) 193static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
147{ 194{
148 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE); 195 static int print_once;
196
197 if (!print_once) {
198 printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
199 "sysfs file is deprecated - used by: %s\n", current->comm);
200 print_once = 1;
201 }
202 return sprintf(buf, "%u\n", MIN_SAMPLING_RATE);
149} 203}
150 204
151#define define_one_ro(_name) \ 205#define define_one_ro(_name) \
152static struct freq_attr _name = \ 206static struct freq_attr _name = \
153__ATTR(_name, 0444, show_##_name, NULL) 207__ATTR(_name, 0444, show_##_name, NULL)
154 208
155define_one_ro(sampling_rate_max); 209define_one_ro(sampling_rate_max);
@@ -174,7 +228,8 @@ static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
174{ 228{
175 unsigned int input; 229 unsigned int input;
176 int ret; 230 int ret;
177 ret = sscanf (buf, "%u", &input); 231 ret = sscanf(buf, "%u", &input);
232
178 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) 233 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
179 return -EINVAL; 234 return -EINVAL;
180 235
@@ -190,15 +245,13 @@ static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
190{ 245{
191 unsigned int input; 246 unsigned int input;
192 int ret; 247 int ret;
193 ret = sscanf (buf, "%u", &input); 248 ret = sscanf(buf, "%u", &input);
194 249
195 mutex_lock(&dbs_mutex); 250 if (ret != 1)
196 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
197 mutex_unlock(&dbs_mutex);
198 return -EINVAL; 251 return -EINVAL;
199 }
200 252
201 dbs_tuners_ins.sampling_rate = input; 253 mutex_lock(&dbs_mutex);
254 dbs_tuners_ins.sampling_rate = max(input, minimum_sampling_rate());
202 mutex_unlock(&dbs_mutex); 255 mutex_unlock(&dbs_mutex);
203 256
204 return count; 257 return count;
@@ -209,10 +262,11 @@ static ssize_t store_up_threshold(struct cpufreq_policy *unused,
209{ 262{
210 unsigned int input; 263 unsigned int input;
211 int ret; 264 int ret;
212 ret = sscanf (buf, "%u", &input); 265 ret = sscanf(buf, "%u", &input);
213 266
214 mutex_lock(&dbs_mutex); 267 mutex_lock(&dbs_mutex);
215 if (ret != 1 || input > 100 || input <= dbs_tuners_ins.down_threshold) { 268 if (ret != 1 || input > 100 ||
269 input <= dbs_tuners_ins.down_threshold) {
216 mutex_unlock(&dbs_mutex); 270 mutex_unlock(&dbs_mutex);
217 return -EINVAL; 271 return -EINVAL;
218 } 272 }
@@ -228,10 +282,12 @@ static ssize_t store_down_threshold(struct cpufreq_policy *unused,
228{ 282{
229 unsigned int input; 283 unsigned int input;
230 int ret; 284 int ret;
231 ret = sscanf (buf, "%u", &input); 285 ret = sscanf(buf, "%u", &input);
232 286
233 mutex_lock(&dbs_mutex); 287 mutex_lock(&dbs_mutex);
234 if (ret != 1 || input > 100 || input >= dbs_tuners_ins.up_threshold) { 288 /* cannot be lower than 11 otherwise freq will not fall */
289 if (ret != 1 || input < 11 || input > 100 ||
290 input >= dbs_tuners_ins.up_threshold) {
235 mutex_unlock(&dbs_mutex); 291 mutex_unlock(&dbs_mutex);
236 return -EINVAL; 292 return -EINVAL;
237 } 293 }
@@ -264,12 +320,14 @@ static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
264 } 320 }
265 dbs_tuners_ins.ignore_nice = input; 321 dbs_tuners_ins.ignore_nice = input;
266 322
267 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */ 323 /* we need to re-evaluate prev_cpu_idle */
268 for_each_online_cpu(j) { 324 for_each_online_cpu(j) {
269 struct cpu_dbs_info_s *j_dbs_info; 325 struct cpu_dbs_info_s *dbs_info;
270 j_dbs_info = &per_cpu(cpu_dbs_info, j); 326 dbs_info = &per_cpu(cpu_dbs_info, j);
271 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j); 327 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
272 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up; 328 &dbs_info->prev_cpu_wall);
329 if (dbs_tuners_ins.ignore_nice)
330 dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
273 } 331 }
274 mutex_unlock(&dbs_mutex); 332 mutex_unlock(&dbs_mutex);
275 333
@@ -281,7 +339,6 @@ static ssize_t store_freq_step(struct cpufreq_policy *policy,
281{ 339{
282 unsigned int input; 340 unsigned int input;
283 int ret; 341 int ret;
284
285 ret = sscanf(buf, "%u", &input); 342 ret = sscanf(buf, "%u", &input);
286 343
287 if (ret != 1) 344 if (ret != 1)
@@ -310,7 +367,7 @@ define_one_rw(down_threshold);
310define_one_rw(ignore_nice_load); 367define_one_rw(ignore_nice_load);
311define_one_rw(freq_step); 368define_one_rw(freq_step);
312 369
313static struct attribute * dbs_attributes[] = { 370static struct attribute *dbs_attributes[] = {
314 &sampling_rate_max.attr, 371 &sampling_rate_max.attr,
315 &sampling_rate_min.attr, 372 &sampling_rate_min.attr,
316 &sampling_rate.attr, 373 &sampling_rate.attr,
@@ -329,55 +386,78 @@ static struct attribute_group dbs_attr_group = {
329 386
330/************************** sysfs end ************************/ 387/************************** sysfs end ************************/
331 388
332static void dbs_check_cpu(int cpu) 389static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
333{ 390{
334 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks; 391 unsigned int load = 0;
335 unsigned int tmp_idle_ticks, total_idle_ticks;
336 unsigned int freq_target; 392 unsigned int freq_target;
337 unsigned int freq_down_sampling_rate;
338 struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
339 struct cpufreq_policy *policy;
340 393
341 if (!this_dbs_info->enable) 394 struct cpufreq_policy *policy;
342 return; 395 unsigned int j;
343 396
344 policy = this_dbs_info->cur_policy; 397 policy = this_dbs_info->cur_policy;
345 398
346 /* 399 /*
347 * The default safe range is 20% to 80% 400 * Every sampling_rate, we check, if current idle time is less
348 * Every sampling_rate, we check 401 * than 20% (default), then we try to increase frequency
349 * - If current idle time is less than 20%, then we try to 402 * Every sampling_rate*sampling_down_factor, we check, if current
350 * increase frequency 403 * idle time is more than 80%, then we try to decrease frequency
351 * Every sampling_rate*sampling_down_factor, we check
352 * - If current idle time is more than 80%, then we try to
353 * decrease frequency
354 * 404 *
355 * Any frequency increase takes it to the maximum frequency. 405 * Any frequency increase takes it to the maximum frequency.
356 * Frequency reduction happens at minimum steps of 406 * Frequency reduction happens at minimum steps of
357 * 5% (default) of max_frequency 407 * 5% (default) of maximum frequency
358 */ 408 */
359 409
360 /* Check for frequency increase */ 410 /* Get Absolute Load */
361 idle_ticks = UINT_MAX; 411 for_each_cpu(j, policy->cpus) {
412 struct cpu_dbs_info_s *j_dbs_info;
413 cputime64_t cur_wall_time, cur_idle_time;
414 unsigned int idle_time, wall_time;
362 415
363 /* Check for frequency increase */ 416 j_dbs_info = &per_cpu(cpu_dbs_info, j);
364 total_idle_ticks = get_cpu_idle_time(cpu); 417
365 tmp_idle_ticks = total_idle_ticks - 418 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
366 this_dbs_info->prev_cpu_idle_up; 419
367 this_dbs_info->prev_cpu_idle_up = total_idle_ticks; 420 wall_time = (unsigned int) cputime64_sub(cur_wall_time,
421 j_dbs_info->prev_cpu_wall);
422 j_dbs_info->prev_cpu_wall = cur_wall_time;
423
424 idle_time = (unsigned int) cputime64_sub(cur_idle_time,
425 j_dbs_info->prev_cpu_idle);
426 j_dbs_info->prev_cpu_idle = cur_idle_time;
427
428 if (dbs_tuners_ins.ignore_nice) {
429 cputime64_t cur_nice;
430 unsigned long cur_nice_jiffies;
431
432 cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
433 j_dbs_info->prev_cpu_nice);
434 /*
435 * Assumption: nice time between sampling periods will
436 * be less than 2^32 jiffies for 32 bit sys
437 */
438 cur_nice_jiffies = (unsigned long)
439 cputime64_to_jiffies64(cur_nice);
368 440
369 if (tmp_idle_ticks < idle_ticks) 441 j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
370 idle_ticks = tmp_idle_ticks; 442 idle_time += jiffies_to_usecs(cur_nice_jiffies);
443 }
444
445 if (unlikely(!wall_time || wall_time < idle_time))
446 continue;
447
448 load = 100 * (wall_time - idle_time) / wall_time;
449 }
371 450
372 /* Scale idle ticks by 100 and compare with up and down ticks */ 451 /*
373 idle_ticks *= 100; 452 * break out if we 'cannot' reduce the speed as the user might
374 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) * 453 * want freq_step to be zero
375 usecs_to_jiffies(dbs_tuners_ins.sampling_rate); 454 */
455 if (dbs_tuners_ins.freq_step == 0)
456 return;
376 457
377 if (idle_ticks < up_idle_ticks) { 458 /* Check for frequency increase */
459 if (load > dbs_tuners_ins.up_threshold) {
378 this_dbs_info->down_skip = 0; 460 this_dbs_info->down_skip = 0;
379 this_dbs_info->prev_cpu_idle_down =
380 this_dbs_info->prev_cpu_idle_up;
381 461
382 /* if we are already at full speed then break out early */ 462 /* if we are already at full speed then break out early */
383 if (this_dbs_info->requested_freq == policy->max) 463 if (this_dbs_info->requested_freq == policy->max)
@@ -398,49 +478,24 @@ static void dbs_check_cpu(int cpu)
398 return; 478 return;
399 } 479 }
400 480
401 /* Check for frequency decrease */ 481 /*
402 this_dbs_info->down_skip++; 482 * The optimal frequency is the frequency that is the lowest that
403 if (this_dbs_info->down_skip < dbs_tuners_ins.sampling_down_factor) 483 * can support the current CPU usage without triggering the up
404 return; 484 * policy. To be safe, we focus 10 points under the threshold.
405 485 */
406 /* Check for frequency decrease */ 486 if (load < (dbs_tuners_ins.down_threshold - 10)) {
407 total_idle_ticks = this_dbs_info->prev_cpu_idle_up;
408 tmp_idle_ticks = total_idle_ticks -
409 this_dbs_info->prev_cpu_idle_down;
410 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
411
412 if (tmp_idle_ticks < idle_ticks)
413 idle_ticks = tmp_idle_ticks;
414
415 /* Scale idle ticks by 100 and compare with up and down ticks */
416 idle_ticks *= 100;
417 this_dbs_info->down_skip = 0;
418
419 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
420 dbs_tuners_ins.sampling_down_factor;
421 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
422 usecs_to_jiffies(freq_down_sampling_rate);
423
424 if (idle_ticks > down_idle_ticks) {
425 /*
426 * if we are already at the lowest speed then break out early
427 * or if we 'cannot' reduce the speed as the user might want
428 * freq_target to be zero
429 */
430 if (this_dbs_info->requested_freq == policy->min
431 || dbs_tuners_ins.freq_step == 0)
432 return;
433
434 freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100; 487 freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
435 488
436 /* max freq cannot be less than 100. But who knows.... */
437 if (unlikely(freq_target == 0))
438 freq_target = 5;
439
440 this_dbs_info->requested_freq -= freq_target; 489 this_dbs_info->requested_freq -= freq_target;
441 if (this_dbs_info->requested_freq < policy->min) 490 if (this_dbs_info->requested_freq < policy->min)
442 this_dbs_info->requested_freq = policy->min; 491 this_dbs_info->requested_freq = policy->min;
443 492
493 /*
494 * if we cannot reduce the frequency anymore, break out early
495 */
496 if (policy->cur == policy->min)
497 return;
498
444 __cpufreq_driver_target(policy, this_dbs_info->requested_freq, 499 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
445 CPUFREQ_RELATION_H); 500 CPUFREQ_RELATION_H);
446 return; 501 return;
@@ -449,27 +504,45 @@ static void dbs_check_cpu(int cpu)
449 504
450static void do_dbs_timer(struct work_struct *work) 505static void do_dbs_timer(struct work_struct *work)
451{ 506{
452 int i; 507 struct cpu_dbs_info_s *dbs_info =
453 mutex_lock(&dbs_mutex); 508 container_of(work, struct cpu_dbs_info_s, work.work);
454 for_each_online_cpu(i) 509 unsigned int cpu = dbs_info->cpu;
455 dbs_check_cpu(i); 510
456 schedule_delayed_work(&dbs_work, 511 /* We want all CPUs to do sampling nearly on same jiffy */
457 usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); 512 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
458 mutex_unlock(&dbs_mutex); 513
514 delay -= jiffies % delay;
515
516 if (lock_policy_rwsem_write(cpu) < 0)
517 return;
518
519 if (!dbs_info->enable) {
520 unlock_policy_rwsem_write(cpu);
521 return;
522 }
523
524 dbs_check_cpu(dbs_info);
525
526 queue_delayed_work_on(cpu, kconservative_wq, &dbs_info->work, delay);
527 unlock_policy_rwsem_write(cpu);
459} 528}
460 529
461static inline void dbs_timer_init(void) 530static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
462{ 531{
463 init_timer_deferrable(&dbs_work.timer); 532 /* We want all CPUs to do sampling nearly on same jiffy */
464 schedule_delayed_work(&dbs_work, 533 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
465 usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); 534 delay -= jiffies % delay;
466 return; 535
536 dbs_info->enable = 1;
537 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
538 queue_delayed_work_on(dbs_info->cpu, kconservative_wq, &dbs_info->work,
539 delay);
467} 540}
468 541
469static inline void dbs_timer_exit(void) 542static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
470{ 543{
471 cancel_delayed_work(&dbs_work); 544 dbs_info->enable = 0;
472 return; 545 cancel_delayed_work(&dbs_info->work);
473} 546}
474 547
475static int cpufreq_governor_dbs(struct cpufreq_policy *policy, 548static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
@@ -503,11 +576,13 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
503 j_dbs_info = &per_cpu(cpu_dbs_info, j); 576 j_dbs_info = &per_cpu(cpu_dbs_info, j);
504 j_dbs_info->cur_policy = policy; 577 j_dbs_info->cur_policy = policy;
505 578
506 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu); 579 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
507 j_dbs_info->prev_cpu_idle_down 580 &j_dbs_info->prev_cpu_wall);
508 = j_dbs_info->prev_cpu_idle_up; 581 if (dbs_tuners_ins.ignore_nice) {
582 j_dbs_info->prev_cpu_nice =
583 kstat_cpu(j).cpustat.nice;
584 }
509 } 585 }
510 this_dbs_info->enable = 1;
511 this_dbs_info->down_skip = 0; 586 this_dbs_info->down_skip = 0;
512 this_dbs_info->requested_freq = policy->cur; 587 this_dbs_info->requested_freq = policy->cur;
513 588
@@ -523,38 +598,36 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
523 if (latency == 0) 598 if (latency == 0)
524 latency = 1; 599 latency = 1;
525 600
526 def_sampling_rate = 10 * latency * 601 def_sampling_rate =
527 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER; 602 max(latency * LATENCY_MULTIPLIER,
528 603 MIN_STAT_SAMPLING_RATE);
529 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
530 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
531 604
532 dbs_tuners_ins.sampling_rate = def_sampling_rate; 605 dbs_tuners_ins.sampling_rate = def_sampling_rate;
533 606
534 dbs_timer_init();
535 cpufreq_register_notifier( 607 cpufreq_register_notifier(
536 &dbs_cpufreq_notifier_block, 608 &dbs_cpufreq_notifier_block,
537 CPUFREQ_TRANSITION_NOTIFIER); 609 CPUFREQ_TRANSITION_NOTIFIER);
538 } 610 }
611 dbs_timer_init(this_dbs_info);
539 612
540 mutex_unlock(&dbs_mutex); 613 mutex_unlock(&dbs_mutex);
614
541 break; 615 break;
542 616
543 case CPUFREQ_GOV_STOP: 617 case CPUFREQ_GOV_STOP:
544 mutex_lock(&dbs_mutex); 618 mutex_lock(&dbs_mutex);
545 this_dbs_info->enable = 0; 619 dbs_timer_exit(this_dbs_info);
546 sysfs_remove_group(&policy->kobj, &dbs_attr_group); 620 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
547 dbs_enable--; 621 dbs_enable--;
622
548 /* 623 /*
549 * Stop the timerschedule work, when this governor 624 * Stop the timerschedule work, when this governor
550 * is used for first time 625 * is used for first time
551 */ 626 */
552 if (dbs_enable == 0) { 627 if (dbs_enable == 0)
553 dbs_timer_exit();
554 cpufreq_unregister_notifier( 628 cpufreq_unregister_notifier(
555 &dbs_cpufreq_notifier_block, 629 &dbs_cpufreq_notifier_block,
556 CPUFREQ_TRANSITION_NOTIFIER); 630 CPUFREQ_TRANSITION_NOTIFIER);
557 }
558 631
559 mutex_unlock(&dbs_mutex); 632 mutex_unlock(&dbs_mutex);
560 633
@@ -571,6 +644,7 @@ static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
571 this_dbs_info->cur_policy, 644 this_dbs_info->cur_policy,
572 policy->min, CPUFREQ_RELATION_L); 645 policy->min, CPUFREQ_RELATION_L);
573 mutex_unlock(&dbs_mutex); 646 mutex_unlock(&dbs_mutex);
647
574 break; 648 break;
575 } 649 }
576 return 0; 650 return 0;
@@ -588,23 +662,33 @@ struct cpufreq_governor cpufreq_gov_conservative = {
588 662
589static int __init cpufreq_gov_dbs_init(void) 663static int __init cpufreq_gov_dbs_init(void)
590{ 664{
591 return cpufreq_register_governor(&cpufreq_gov_conservative); 665 int err;
666
667 kconservative_wq = create_workqueue("kconservative");
668 if (!kconservative_wq) {
669 printk(KERN_ERR "Creation of kconservative failed\n");
670 return -EFAULT;
671 }
672
673 err = cpufreq_register_governor(&cpufreq_gov_conservative);
674 if (err)
675 destroy_workqueue(kconservative_wq);
676
677 return err;
592} 678}
593 679
594static void __exit cpufreq_gov_dbs_exit(void) 680static void __exit cpufreq_gov_dbs_exit(void)
595{ 681{
596 /* Make sure that the scheduled work is indeed not running */
597 flush_scheduled_work();
598
599 cpufreq_unregister_governor(&cpufreq_gov_conservative); 682 cpufreq_unregister_governor(&cpufreq_gov_conservative);
683 destroy_workqueue(kconservative_wq);
600} 684}
601 685
602 686
603MODULE_AUTHOR ("Alexander Clouter <alex-kernel@digriz.org.uk>"); 687MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
604MODULE_DESCRIPTION ("'cpufreq_conservative' - A dynamic cpufreq governor for " 688MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
605 "Low Latency Frequency Transition capable processors " 689 "Low Latency Frequency Transition capable processors "
606 "optimised for use in a battery environment"); 690 "optimised for use in a battery environment");
607MODULE_LICENSE ("GPL"); 691MODULE_LICENSE("GPL");
608 692
609#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE 693#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
610fs_initcall(cpufreq_gov_dbs_init); 694fs_initcall(cpufreq_gov_dbs_init);