[BLOCK] Fix bounce limit address check
[linux-2.6-block.git] / drivers / cpufreq / cpufreq_conservative.c
CommitLineData
b9170836
DJ
1/*
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>
7 * (C) 2004 Alexander Clouter <alex-kernel@digriz.org.uk>
8 *
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>
16#include <linux/smp.h>
17#include <linux/init.h>
18#include <linux/interrupt.h>
19#include <linux/ctype.h>
20#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/sched.h>
26#include <linux/kmod.h>
27#include <linux/workqueue.h>
28#include <linux/jiffies.h>
29#include <linux/kernel_stat.h>
30#include <linux/percpu.h>
3fc54d37 31#include <linux/mutex.h>
b9170836
DJ
32/*
33 * dbs is used in this file as a shortform for demandbased switching
34 * It helps to keep variable names smaller, simpler
35 */
36
37#define DEF_FREQUENCY_UP_THRESHOLD (80)
b9170836 38#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
b9170836
DJ
39
40/*
41 * The polling frequency of this governor depends on the capability of
42 * the processor. Default polling frequency is 1000 times the transition
43 * latency of the processor. The governor will work on any processor with
44 * transition latency <= 10mS, using appropriate sampling
45 * rate.
46 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
47 * this governor will not work.
48 * All times here are in uS.
49 */
50static unsigned int def_sampling_rate;
2c906b31
AC
51#define MIN_SAMPLING_RATE_RATIO (2)
52/* for correct statistics, we need at least 10 ticks between each measure */
53#define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
54#define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
b9170836 55#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
2c906b31
AC
56#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
57#define DEF_SAMPLING_DOWN_FACTOR (1)
58#define MAX_SAMPLING_DOWN_FACTOR (10)
b9170836
DJ
59#define TRANSITION_LATENCY_LIMIT (10 * 1000)
60
61static void do_dbs_timer(void *data);
62
63struct cpu_dbs_info_s {
64 struct cpufreq_policy *cur_policy;
65 unsigned int prev_cpu_idle_up;
66 unsigned int prev_cpu_idle_down;
67 unsigned int enable;
a159b827
AC
68 unsigned int down_skip;
69 unsigned int requested_freq;
b9170836
DJ
70};
71static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
72
73static unsigned int dbs_enable; /* number of CPUs using this policy */
74
4ec223d0
VP
75/*
76 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
77 * lock and dbs_mutex. cpu_hotplug lock should always be held before
78 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
79 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
80 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
81 * is recursive for the same process. -Venki
82 */
3fc54d37 83static DEFINE_MUTEX (dbs_mutex);
b9170836
DJ
84static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
85
86struct dbs_tuners {
87 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;
93};
94
95static struct dbs_tuners dbs_tuners_ins = {
96 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
97 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
98 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
c326e27e
MD
99 .ignore_nice = 0,
100 .freq_step = 5,
b9170836
DJ
101};
102
dac1c1a5
DJ
103static inline unsigned int get_cpu_idle_time(unsigned int cpu)
104{
105 return kstat_cpu(cpu).cpustat.idle +
106 kstat_cpu(cpu).cpustat.iowait +
001893cd 107 ( dbs_tuners_ins.ignore_nice ?
dac1c1a5
DJ
108 kstat_cpu(cpu).cpustat.nice :
109 0);
110}
111
b9170836
DJ
112/************************** sysfs interface ************************/
113static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
114{
115 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
116}
117
118static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
119{
120 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
121}
122
123#define define_one_ro(_name) \
124static struct freq_attr _name = \
125__ATTR(_name, 0444, show_##_name, NULL)
126
127define_one_ro(sampling_rate_max);
128define_one_ro(sampling_rate_min);
129
130/* cpufreq_conservative Governor Tunables */
131#define show_one(file_name, object) \
132static ssize_t show_##file_name \
133(struct cpufreq_policy *unused, char *buf) \
134{ \
135 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
136}
137show_one(sampling_rate, sampling_rate);
138show_one(sampling_down_factor, sampling_down_factor);
139show_one(up_threshold, up_threshold);
140show_one(down_threshold, down_threshold);
001893cd 141show_one(ignore_nice_load, ignore_nice);
b9170836
DJ
142show_one(freq_step, freq_step);
143
144static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
145 const char *buf, size_t count)
146{
147 unsigned int input;
148 int ret;
149 ret = sscanf (buf, "%u", &input);
2c906b31 150 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
b9170836
DJ
151 return -EINVAL;
152
3fc54d37 153 mutex_lock(&dbs_mutex);
b9170836 154 dbs_tuners_ins.sampling_down_factor = input;
3fc54d37 155 mutex_unlock(&dbs_mutex);
b9170836
DJ
156
157 return count;
158}
159
160static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
161 const char *buf, size_t count)
162{
163 unsigned int input;
164 int ret;
165 ret = sscanf (buf, "%u", &input);
166
3fc54d37 167 mutex_lock(&dbs_mutex);
b9170836 168 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
3fc54d37 169 mutex_unlock(&dbs_mutex);
b9170836
DJ
170 return -EINVAL;
171 }
172
173 dbs_tuners_ins.sampling_rate = input;
3fc54d37 174 mutex_unlock(&dbs_mutex);
b9170836
DJ
175
176 return count;
177}
178
179static ssize_t store_up_threshold(struct cpufreq_policy *unused,
180 const char *buf, size_t count)
181{
182 unsigned int input;
183 int ret;
184 ret = sscanf (buf, "%u", &input);
185
3fc54d37 186 mutex_lock(&dbs_mutex);
b82fbe6c 187 if (ret != 1 || input > 100 || input <= dbs_tuners_ins.down_threshold) {
3fc54d37 188 mutex_unlock(&dbs_mutex);
b9170836
DJ
189 return -EINVAL;
190 }
191
192 dbs_tuners_ins.up_threshold = input;
3fc54d37 193 mutex_unlock(&dbs_mutex);
b9170836
DJ
194
195 return count;
196}
197
198static ssize_t store_down_threshold(struct cpufreq_policy *unused,
199 const char *buf, size_t count)
200{
201 unsigned int input;
202 int ret;
203 ret = sscanf (buf, "%u", &input);
204
3fc54d37 205 mutex_lock(&dbs_mutex);
b82fbe6c 206 if (ret != 1 || input > 100 || input >= dbs_tuners_ins.up_threshold) {
3fc54d37 207 mutex_unlock(&dbs_mutex);
b9170836
DJ
208 return -EINVAL;
209 }
210
211 dbs_tuners_ins.down_threshold = input;
3fc54d37 212 mutex_unlock(&dbs_mutex);
b9170836
DJ
213
214 return count;
215}
216
001893cd 217static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
b9170836
DJ
218 const char *buf, size_t count)
219{
220 unsigned int input;
221 int ret;
222
223 unsigned int j;
224
225 ret = sscanf (buf, "%u", &input);
226 if ( ret != 1 )
227 return -EINVAL;
228
229 if ( input > 1 )
230 input = 1;
231
3fc54d37 232 mutex_lock(&dbs_mutex);
b9170836 233 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
3fc54d37 234 mutex_unlock(&dbs_mutex);
b9170836
DJ
235 return count;
236 }
237 dbs_tuners_ins.ignore_nice = input;
238
239 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
dac1c1a5 240 for_each_online_cpu(j) {
b9170836
DJ
241 struct cpu_dbs_info_s *j_dbs_info;
242 j_dbs_info = &per_cpu(cpu_dbs_info, j);
dac1c1a5 243 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
b9170836
DJ
244 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
245 }
3fc54d37 246 mutex_unlock(&dbs_mutex);
b9170836
DJ
247
248 return count;
249}
250
251static ssize_t store_freq_step(struct cpufreq_policy *policy,
252 const char *buf, size_t count)
253{
254 unsigned int input;
255 int ret;
256
257 ret = sscanf (buf, "%u", &input);
258
259 if ( ret != 1 )
260 return -EINVAL;
261
262 if ( input > 100 )
263 input = 100;
264
265 /* no need to test here if freq_step is zero as the user might actually
266 * want this, they would be crazy though :) */
3fc54d37 267 mutex_lock(&dbs_mutex);
b9170836 268 dbs_tuners_ins.freq_step = input;
3fc54d37 269 mutex_unlock(&dbs_mutex);
b9170836
DJ
270
271 return count;
272}
273
274#define define_one_rw(_name) \
275static struct freq_attr _name = \
276__ATTR(_name, 0644, show_##_name, store_##_name)
277
278define_one_rw(sampling_rate);
279define_one_rw(sampling_down_factor);
280define_one_rw(up_threshold);
281define_one_rw(down_threshold);
001893cd 282define_one_rw(ignore_nice_load);
b9170836
DJ
283define_one_rw(freq_step);
284
285static struct attribute * dbs_attributes[] = {
286 &sampling_rate_max.attr,
287 &sampling_rate_min.attr,
288 &sampling_rate.attr,
289 &sampling_down_factor.attr,
290 &up_threshold.attr,
291 &down_threshold.attr,
001893cd 292 &ignore_nice_load.attr,
b9170836
DJ
293 &freq_step.attr,
294 NULL
295};
296
297static struct attribute_group dbs_attr_group = {
298 .attrs = dbs_attributes,
299 .name = "conservative",
300};
301
302/************************** sysfs end ************************/
303
304static void dbs_check_cpu(int cpu)
305{
306 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
08a28e2e 307 unsigned int tmp_idle_ticks, total_idle_ticks;
b9170836
DJ
308 unsigned int freq_step;
309 unsigned int freq_down_sampling_rate;
08a28e2e 310 struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
b9170836 311 struct cpufreq_policy *policy;
b9170836 312
b9170836
DJ
313 if (!this_dbs_info->enable)
314 return;
315
08a28e2e
AC
316 policy = this_dbs_info->cur_policy;
317
b9170836
DJ
318 /*
319 * The default safe range is 20% to 80%
320 * Every sampling_rate, we check
321 * - If current idle time is less than 20%, then we try to
322 * increase frequency
323 * Every sampling_rate*sampling_down_factor, we check
324 * - If current idle time is more than 80%, then we try to
325 * decrease frequency
326 *
327 * Any frequency increase takes it to the maximum frequency.
328 * Frequency reduction happens at minimum steps of
329 * 5% (default) of max_frequency
330 */
331
332 /* Check for frequency increase */
9c7d269b 333 idle_ticks = UINT_MAX;
b9170836 334
08a28e2e
AC
335 /* Check for frequency increase */
336 total_idle_ticks = get_cpu_idle_time(cpu);
337 tmp_idle_ticks = total_idle_ticks -
338 this_dbs_info->prev_cpu_idle_up;
339 this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
340
341 if (tmp_idle_ticks < idle_ticks)
342 idle_ticks = tmp_idle_ticks;
b9170836
DJ
343
344 /* Scale idle ticks by 100 and compare with up and down ticks */
345 idle_ticks *= 100;
346 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
2c906b31 347 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
b9170836
DJ
348
349 if (idle_ticks < up_idle_ticks) {
a159b827 350 this_dbs_info->down_skip = 0;
08a28e2e
AC
351 this_dbs_info->prev_cpu_idle_down =
352 this_dbs_info->prev_cpu_idle_up;
790d76fa 353
b9170836 354 /* if we are already at full speed then break out early */
a159b827 355 if (this_dbs_info->requested_freq == policy->max)
b9170836
DJ
356 return;
357
358 freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
359
360 /* max freq cannot be less than 100. But who knows.... */
361 if (unlikely(freq_step == 0))
362 freq_step = 5;
363
a159b827
AC
364 this_dbs_info->requested_freq += freq_step;
365 if (this_dbs_info->requested_freq > policy->max)
366 this_dbs_info->requested_freq = policy->max;
b9170836 367
a159b827 368 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
b9170836 369 CPUFREQ_RELATION_H);
b9170836
DJ
370 return;
371 }
372
373 /* Check for frequency decrease */
a159b827
AC
374 this_dbs_info->down_skip++;
375 if (this_dbs_info->down_skip < dbs_tuners_ins.sampling_down_factor)
b9170836
DJ
376 return;
377
08a28e2e
AC
378 /* Check for frequency decrease */
379 total_idle_ticks = this_dbs_info->prev_cpu_idle_up;
380 tmp_idle_ticks = total_idle_ticks -
381 this_dbs_info->prev_cpu_idle_down;
382 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
b9170836 383
08a28e2e
AC
384 if (tmp_idle_ticks < idle_ticks)
385 idle_ticks = tmp_idle_ticks;
b9170836
DJ
386
387 /* Scale idle ticks by 100 and compare with up and down ticks */
388 idle_ticks *= 100;
a159b827 389 this_dbs_info->down_skip = 0;
b9170836
DJ
390
391 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
392 dbs_tuners_ins.sampling_down_factor;
393 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
2c906b31 394 usecs_to_jiffies(freq_down_sampling_rate);
b9170836 395
9c7d269b 396 if (idle_ticks > down_idle_ticks) {
2c906b31
AC
397 /*
398 * if we are already at the lowest speed then break out early
b9170836 399 * or if we 'cannot' reduce the speed as the user might want
2c906b31
AC
400 * freq_step to be zero
401 */
a159b827 402 if (this_dbs_info->requested_freq == policy->min
b9170836
DJ
403 || dbs_tuners_ins.freq_step == 0)
404 return;
405
406 freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
407
408 /* max freq cannot be less than 100. But who knows.... */
409 if (unlikely(freq_step == 0))
410 freq_step = 5;
411
a159b827
AC
412 this_dbs_info->requested_freq -= freq_step;
413 if (this_dbs_info->requested_freq < policy->min)
414 this_dbs_info->requested_freq = policy->min;
b9170836 415
a159b827 416 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
2c906b31 417 CPUFREQ_RELATION_H);
b9170836
DJ
418 return;
419 }
420}
421
422static void do_dbs_timer(void *data)
423{
424 int i;
4ec223d0 425 lock_cpu_hotplug();
3fc54d37 426 mutex_lock(&dbs_mutex);
b9170836
DJ
427 for_each_online_cpu(i)
428 dbs_check_cpu(i);
429 schedule_delayed_work(&dbs_work,
430 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
3fc54d37 431 mutex_unlock(&dbs_mutex);
4ec223d0 432 unlock_cpu_hotplug();
b9170836
DJ
433}
434
435static inline void dbs_timer_init(void)
436{
437 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
438 schedule_delayed_work(&dbs_work,
439 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
440 return;
441}
442
443static inline void dbs_timer_exit(void)
444{
445 cancel_delayed_work(&dbs_work);
446 return;
447}
448
449static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
450 unsigned int event)
451{
452 unsigned int cpu = policy->cpu;
453 struct cpu_dbs_info_s *this_dbs_info;
454 unsigned int j;
455
456 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
457
458 switch (event) {
459 case CPUFREQ_GOV_START:
460 if ((!cpu_online(cpu)) ||
461 (!policy->cur))
462 return -EINVAL;
463
464 if (policy->cpuinfo.transition_latency >
465 (TRANSITION_LATENCY_LIMIT * 1000))
466 return -EINVAL;
467 if (this_dbs_info->enable) /* Already enabled */
468 break;
469
3fc54d37 470 mutex_lock(&dbs_mutex);
b9170836
DJ
471 for_each_cpu_mask(j, policy->cpus) {
472 struct cpu_dbs_info_s *j_dbs_info;
473 j_dbs_info = &per_cpu(cpu_dbs_info, j);
474 j_dbs_info->cur_policy = policy;
475
08a28e2e 476 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu);
b9170836
DJ
477 j_dbs_info->prev_cpu_idle_down
478 = j_dbs_info->prev_cpu_idle_up;
479 }
480 this_dbs_info->enable = 1;
a159b827
AC
481 this_dbs_info->down_skip = 0;
482 this_dbs_info->requested_freq = policy->cur;
b9170836
DJ
483 sysfs_create_group(&policy->kobj, &dbs_attr_group);
484 dbs_enable++;
485 /*
486 * Start the timerschedule work, when this governor
487 * is used for first time
488 */
489 if (dbs_enable == 1) {
490 unsigned int latency;
491 /* policy latency is in nS. Convert it to uS first */
2c906b31
AC
492 latency = policy->cpuinfo.transition_latency / 1000;
493 if (latency == 0)
494 latency = 1;
b9170836 495
e8a02572 496 def_sampling_rate = 10 * latency *
b9170836 497 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
2c906b31
AC
498
499 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
500 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
501
b9170836 502 dbs_tuners_ins.sampling_rate = def_sampling_rate;
b9170836
DJ
503
504 dbs_timer_init();
505 }
506
3fc54d37 507 mutex_unlock(&dbs_mutex);
b9170836
DJ
508 break;
509
510 case CPUFREQ_GOV_STOP:
3fc54d37 511 mutex_lock(&dbs_mutex);
b9170836
DJ
512 this_dbs_info->enable = 0;
513 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
514 dbs_enable--;
515 /*
516 * Stop the timerschedule work, when this governor
517 * is used for first time
518 */
519 if (dbs_enable == 0)
520 dbs_timer_exit();
521
3fc54d37 522 mutex_unlock(&dbs_mutex);
b9170836
DJ
523
524 break;
525
526 case CPUFREQ_GOV_LIMITS:
4ec223d0 527 lock_cpu_hotplug();
3fc54d37 528 mutex_lock(&dbs_mutex);
b9170836
DJ
529 if (policy->max < this_dbs_info->cur_policy->cur)
530 __cpufreq_driver_target(
531 this_dbs_info->cur_policy,
532 policy->max, CPUFREQ_RELATION_H);
533 else if (policy->min > this_dbs_info->cur_policy->cur)
534 __cpufreq_driver_target(
535 this_dbs_info->cur_policy,
536 policy->min, CPUFREQ_RELATION_L);
3fc54d37 537 mutex_unlock(&dbs_mutex);
4ec223d0 538 unlock_cpu_hotplug();
b9170836
DJ
539 break;
540 }
541 return 0;
542}
543
544static struct cpufreq_governor cpufreq_gov_dbs = {
545 .name = "conservative",
546 .governor = cpufreq_governor_dbs,
547 .owner = THIS_MODULE,
548};
549
550static int __init cpufreq_gov_dbs_init(void)
551{
552 return cpufreq_register_governor(&cpufreq_gov_dbs);
553}
554
555static void __exit cpufreq_gov_dbs_exit(void)
556{
557 /* Make sure that the scheduled work is indeed not running */
558 flush_scheduled_work();
559
560 cpufreq_unregister_governor(&cpufreq_gov_dbs);
561}
562
563
564MODULE_AUTHOR ("Alexander Clouter <alex-kernel@digriz.org.uk>");
565MODULE_DESCRIPTION ("'cpufreq_conservative' - A dynamic cpufreq governor for "
566 "Low Latency Frequency Transition capable processors "
567 "optimised for use in a battery environment");
568MODULE_LICENSE ("GPL");
569
570module_init(cpufreq_gov_dbs_init);
571module_exit(cpufreq_gov_dbs_exit);