blob: c503ec14765f77dd3bc5d618393a1bfde1b1c3ba [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>
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>
31
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)
38#define MIN_FREQUENCY_UP_THRESHOLD (0)
39#define MAX_FREQUENCY_UP_THRESHOLD (100)
40
41#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
42#define MIN_FREQUENCY_DOWN_THRESHOLD (0)
43#define MAX_FREQUENCY_DOWN_THRESHOLD (100)
44
45/*
46 * The polling frequency of this governor depends on the capability of
47 * the processor. Default polling frequency is 1000 times the transition
48 * latency of the processor. The governor will work on any processor with
49 * transition latency <= 10mS, using appropriate sampling
50 * 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 */
55static unsigned int def_sampling_rate;
56#define MIN_SAMPLING_RATE (def_sampling_rate / 2)
57#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
58#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (100000)
59#define DEF_SAMPLING_DOWN_FACTOR (5)
60#define TRANSITION_LATENCY_LIMIT (10 * 1000)
61
62static void do_dbs_timer(void *data);
63
64struct cpu_dbs_info_s {
65 struct cpufreq_policy *cur_policy;
66 unsigned int prev_cpu_idle_up;
67 unsigned int prev_cpu_idle_down;
68 unsigned int enable;
69};
70static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
71
72static unsigned int dbs_enable; /* number of CPUs using this policy */
73
74static DECLARE_MUTEX (dbs_sem);
75static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
76
77struct dbs_tuners {
78 unsigned int sampling_rate;
79 unsigned int sampling_down_factor;
80 unsigned int up_threshold;
81 unsigned int down_threshold;
82 unsigned int ignore_nice;
83 unsigned int freq_step;
84};
85
86static struct dbs_tuners dbs_tuners_ins = {
87 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
88 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
89 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
90};
91
Dave Jonesdac1c1a2005-05-31 19:03:49 -070092static inline unsigned int get_cpu_idle_time(unsigned int cpu)
93{
94 return kstat_cpu(cpu).cpustat.idle +
95 kstat_cpu(cpu).cpustat.iowait +
96 ( !dbs_tuners_ins.ignore_nice ?
97 kstat_cpu(cpu).cpustat.nice :
98 0);
99}
100
Dave Jonesb9170832005-05-31 19:03:47 -0700101/************************** sysfs interface ************************/
102static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
103{
104 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
105}
106
107static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
108{
109 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
110}
111
112#define define_one_ro(_name) \
113static struct freq_attr _name = \
114__ATTR(_name, 0444, show_##_name, NULL)
115
116define_one_ro(sampling_rate_max);
117define_one_ro(sampling_rate_min);
118
119/* cpufreq_conservative Governor Tunables */
120#define show_one(file_name, object) \
121static ssize_t show_##file_name \
122(struct cpufreq_policy *unused, char *buf) \
123{ \
124 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
125}
126show_one(sampling_rate, sampling_rate);
127show_one(sampling_down_factor, sampling_down_factor);
128show_one(up_threshold, up_threshold);
129show_one(down_threshold, down_threshold);
130show_one(ignore_nice, ignore_nice);
131show_one(freq_step, freq_step);
132
133static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
134 const char *buf, size_t count)
135{
136 unsigned int input;
137 int ret;
138 ret = sscanf (buf, "%u", &input);
139 if (ret != 1 )
140 return -EINVAL;
141
142 down(&dbs_sem);
143 dbs_tuners_ins.sampling_down_factor = input;
144 up(&dbs_sem);
145
146 return count;
147}
148
149static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
150 const char *buf, size_t count)
151{
152 unsigned int input;
153 int ret;
154 ret = sscanf (buf, "%u", &input);
155
156 down(&dbs_sem);
157 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
158 up(&dbs_sem);
159 return -EINVAL;
160 }
161
162 dbs_tuners_ins.sampling_rate = input;
163 up(&dbs_sem);
164
165 return count;
166}
167
168static ssize_t store_up_threshold(struct cpufreq_policy *unused,
169 const char *buf, size_t count)
170{
171 unsigned int input;
172 int ret;
173 ret = sscanf (buf, "%u", &input);
174
175 down(&dbs_sem);
176 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
177 input < MIN_FREQUENCY_UP_THRESHOLD ||
178 input <= dbs_tuners_ins.down_threshold) {
179 up(&dbs_sem);
180 return -EINVAL;
181 }
182
183 dbs_tuners_ins.up_threshold = input;
184 up(&dbs_sem);
185
186 return count;
187}
188
189static ssize_t store_down_threshold(struct cpufreq_policy *unused,
190 const char *buf, size_t count)
191{
192 unsigned int input;
193 int ret;
194 ret = sscanf (buf, "%u", &input);
195
196 down(&dbs_sem);
197 if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
198 input < MIN_FREQUENCY_DOWN_THRESHOLD ||
199 input >= dbs_tuners_ins.up_threshold) {
200 up(&dbs_sem);
201 return -EINVAL;
202 }
203
204 dbs_tuners_ins.down_threshold = input;
205 up(&dbs_sem);
206
207 return count;
208}
209
210static ssize_t store_ignore_nice(struct cpufreq_policy *policy,
211 const char *buf, size_t count)
212{
213 unsigned int input;
214 int ret;
215
216 unsigned int j;
217
218 ret = sscanf (buf, "%u", &input);
219 if ( ret != 1 )
220 return -EINVAL;
221
222 if ( input > 1 )
223 input = 1;
224
225 down(&dbs_sem);
226 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
227 up(&dbs_sem);
228 return count;
229 }
230 dbs_tuners_ins.ignore_nice = input;
231
232 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700233 for_each_online_cpu(j) {
Dave Jonesb9170832005-05-31 19:03:47 -0700234 struct cpu_dbs_info_s *j_dbs_info;
235 j_dbs_info = &per_cpu(cpu_dbs_info, j);
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700236 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jonesb9170832005-05-31 19:03:47 -0700237 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
238 }
239 up(&dbs_sem);
240
241 return count;
242}
243
244static ssize_t store_freq_step(struct cpufreq_policy *policy,
245 const char *buf, size_t count)
246{
247 unsigned int input;
248 int ret;
249
250 ret = sscanf (buf, "%u", &input);
251
252 if ( ret != 1 )
253 return -EINVAL;
254
255 if ( input > 100 )
256 input = 100;
257
258 /* no need to test here if freq_step is zero as the user might actually
259 * want this, they would be crazy though :) */
260 down(&dbs_sem);
261 dbs_tuners_ins.freq_step = input;
262 up(&dbs_sem);
263
264 return count;
265}
266
267#define define_one_rw(_name) \
268static struct freq_attr _name = \
269__ATTR(_name, 0644, show_##_name, store_##_name)
270
271define_one_rw(sampling_rate);
272define_one_rw(sampling_down_factor);
273define_one_rw(up_threshold);
274define_one_rw(down_threshold);
275define_one_rw(ignore_nice);
276define_one_rw(freq_step);
277
278static struct attribute * dbs_attributes[] = {
279 &sampling_rate_max.attr,
280 &sampling_rate_min.attr,
281 &sampling_rate.attr,
282 &sampling_down_factor.attr,
283 &up_threshold.attr,
284 &down_threshold.attr,
285 &ignore_nice.attr,
286 &freq_step.attr,
287 NULL
288};
289
290static struct attribute_group dbs_attr_group = {
291 .attrs = dbs_attributes,
292 .name = "conservative",
293};
294
295/************************** sysfs end ************************/
296
297static void dbs_check_cpu(int cpu)
298{
299 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
300 unsigned int total_idle_ticks;
301 unsigned int freq_step;
302 unsigned int freq_down_sampling_rate;
303 static int down_skip[NR_CPUS];
304 static int requested_freq[NR_CPUS];
305 static unsigned short init_flag = 0;
306 struct cpu_dbs_info_s *this_dbs_info;
307 struct cpu_dbs_info_s *dbs_info;
308
309 struct cpufreq_policy *policy;
310 unsigned int j;
311
312 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
313 if (!this_dbs_info->enable)
314 return;
315
316 policy = this_dbs_info->cur_policy;
317
318 if ( init_flag == 0 ) {
319 for ( /* NULL */; init_flag < NR_CPUS; init_flag++ ) {
320 dbs_info = &per_cpu(cpu_dbs_info, init_flag);
321 requested_freq[cpu] = dbs_info->cur_policy->cur;
322 }
323 init_flag = 1;
324 }
325
326 /*
327 * The default safe range is 20% to 80%
328 * Every sampling_rate, we check
329 * - If current idle time is less than 20%, then we try to
330 * increase frequency
331 * Every sampling_rate*sampling_down_factor, we check
332 * - If current idle time is more than 80%, then we try to
333 * decrease frequency
334 *
335 * Any frequency increase takes it to the maximum frequency.
336 * Frequency reduction happens at minimum steps of
337 * 5% (default) of max_frequency
338 */
339
340 /* Check for frequency increase */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700341 total_idle_ticks = get_cpu_idle_time(cpu);
Dave Jonesb9170832005-05-31 19:03:47 -0700342 idle_ticks = total_idle_ticks -
343 this_dbs_info->prev_cpu_idle_up;
344 this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
345
346
347 for_each_cpu_mask(j, policy->cpus) {
348 unsigned int tmp_idle_ticks;
349 struct cpu_dbs_info_s *j_dbs_info;
350
351 if (j == cpu)
352 continue;
353
354 j_dbs_info = &per_cpu(cpu_dbs_info, j);
355 /* Check for frequency increase */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700356 total_idle_ticks = get_cpu_idle_time(j);
Dave Jonesb9170832005-05-31 19:03:47 -0700357 tmp_idle_ticks = total_idle_ticks -
358 j_dbs_info->prev_cpu_idle_up;
359 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
360
361 if (tmp_idle_ticks < idle_ticks)
362 idle_ticks = tmp_idle_ticks;
363 }
364
365 /* Scale idle ticks by 100 and compare with up and down ticks */
366 idle_ticks *= 100;
367 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
368 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
369
370 if (idle_ticks < up_idle_ticks) {
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700371 down_skip[cpu] = 0;
Dave Jones790d76f2005-05-31 19:03:49 -0700372 for_each_cpu_mask(j, policy->cpus) {
373 struct cpu_dbs_info_s *j_dbs_info;
374
375 j_dbs_info = &per_cpu(cpu_dbs_info, j);
376 j_dbs_info->prev_cpu_idle_down =
377 j_dbs_info->prev_cpu_idle_up;
378 }
Dave Jonesb9170832005-05-31 19:03:47 -0700379 /* if we are already at full speed then break out early */
380 if (requested_freq[cpu] == policy->max)
381 return;
382
383 freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
384
385 /* max freq cannot be less than 100. But who knows.... */
386 if (unlikely(freq_step == 0))
387 freq_step = 5;
388
389 requested_freq[cpu] += freq_step;
390 if (requested_freq[cpu] > policy->max)
391 requested_freq[cpu] = policy->max;
392
393 __cpufreq_driver_target(policy, requested_freq[cpu],
394 CPUFREQ_RELATION_H);
Dave Jonesb9170832005-05-31 19:03:47 -0700395 return;
396 }
397
398 /* Check for frequency decrease */
399 down_skip[cpu]++;
400 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
401 return;
402
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700403 total_idle_ticks = this_dbs_info->prev_cpu_idle_up;
Dave Jonesb9170832005-05-31 19:03:47 -0700404 idle_ticks = total_idle_ticks -
405 this_dbs_info->prev_cpu_idle_down;
406 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
407
408 for_each_cpu_mask(j, policy->cpus) {
409 unsigned int tmp_idle_ticks;
410 struct cpu_dbs_info_s *j_dbs_info;
411
412 if (j == cpu)
413 continue;
414
415 j_dbs_info = &per_cpu(cpu_dbs_info, j);
416 /* Check for frequency increase */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700417 total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
Dave Jonesb9170832005-05-31 19:03:47 -0700418 tmp_idle_ticks = total_idle_ticks -
419 j_dbs_info->prev_cpu_idle_down;
420 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
421
422 if (tmp_idle_ticks < idle_ticks)
423 idle_ticks = tmp_idle_ticks;
424 }
425
426 /* Scale idle ticks by 100 and compare with up and down ticks */
427 idle_ticks *= 100;
428 down_skip[cpu] = 0;
429
430 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
431 dbs_tuners_ins.sampling_down_factor;
432 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
433 usecs_to_jiffies(freq_down_sampling_rate);
434
435 if (idle_ticks > down_idle_ticks ) {
436 /* if we are already at the lowest speed then break out early
437 * or if we 'cannot' reduce the speed as the user might want
438 * freq_step to be zero */
439 if (requested_freq[cpu] == policy->min
440 || dbs_tuners_ins.freq_step == 0)
441 return;
442
443 freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100;
444
445 /* max freq cannot be less than 100. But who knows.... */
446 if (unlikely(freq_step == 0))
447 freq_step = 5;
448
449 requested_freq[cpu] -= freq_step;
450 if (requested_freq[cpu] < policy->min)
451 requested_freq[cpu] = policy->min;
452
453 __cpufreq_driver_target(policy,
454 requested_freq[cpu],
455 CPUFREQ_RELATION_H);
456 return;
457 }
458}
459
460static void do_dbs_timer(void *data)
461{
462 int i;
463 down(&dbs_sem);
464 for_each_online_cpu(i)
465 dbs_check_cpu(i);
466 schedule_delayed_work(&dbs_work,
467 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
468 up(&dbs_sem);
469}
470
471static inline void dbs_timer_init(void)
472{
473 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
474 schedule_delayed_work(&dbs_work,
475 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
476 return;
477}
478
479static inline void dbs_timer_exit(void)
480{
481 cancel_delayed_work(&dbs_work);
482 return;
483}
484
485static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
486 unsigned int event)
487{
488 unsigned int cpu = policy->cpu;
489 struct cpu_dbs_info_s *this_dbs_info;
490 unsigned int j;
491
492 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
493
494 switch (event) {
495 case CPUFREQ_GOV_START:
496 if ((!cpu_online(cpu)) ||
497 (!policy->cur))
498 return -EINVAL;
499
500 if (policy->cpuinfo.transition_latency >
501 (TRANSITION_LATENCY_LIMIT * 1000))
502 return -EINVAL;
503 if (this_dbs_info->enable) /* Already enabled */
504 break;
505
506 down(&dbs_sem);
507 for_each_cpu_mask(j, policy->cpus) {
508 struct cpu_dbs_info_s *j_dbs_info;
509 j_dbs_info = &per_cpu(cpu_dbs_info, j);
510 j_dbs_info->cur_policy = policy;
511
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700512 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
Dave Jonesb9170832005-05-31 19:03:47 -0700513 j_dbs_info->prev_cpu_idle_down
514 = j_dbs_info->prev_cpu_idle_up;
515 }
516 this_dbs_info->enable = 1;
517 sysfs_create_group(&policy->kobj, &dbs_attr_group);
518 dbs_enable++;
519 /*
520 * Start the timerschedule work, when this governor
521 * is used for first time
522 */
523 if (dbs_enable == 1) {
524 unsigned int latency;
525 /* policy latency is in nS. Convert it to uS first */
526
527 latency = policy->cpuinfo.transition_latency;
528 if (latency < 1000)
529 latency = 1000;
530
531 def_sampling_rate = (latency / 1000) *
532 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
533 dbs_tuners_ins.sampling_rate = def_sampling_rate;
534 dbs_tuners_ins.ignore_nice = 0;
535 dbs_tuners_ins.freq_step = 5;
536
537 dbs_timer_init();
538 }
539
540 up(&dbs_sem);
541 break;
542
543 case CPUFREQ_GOV_STOP:
544 down(&dbs_sem);
545 this_dbs_info->enable = 0;
546 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
547 dbs_enable--;
548 /*
549 * Stop the timerschedule work, when this governor
550 * is used for first time
551 */
552 if (dbs_enable == 0)
553 dbs_timer_exit();
554
555 up(&dbs_sem);
556
557 break;
558
559 case CPUFREQ_GOV_LIMITS:
560 down(&dbs_sem);
561 if (policy->max < this_dbs_info->cur_policy->cur)
562 __cpufreq_driver_target(
563 this_dbs_info->cur_policy,
564 policy->max, CPUFREQ_RELATION_H);
565 else if (policy->min > this_dbs_info->cur_policy->cur)
566 __cpufreq_driver_target(
567 this_dbs_info->cur_policy,
568 policy->min, CPUFREQ_RELATION_L);
569 up(&dbs_sem);
570 break;
571 }
572 return 0;
573}
574
575static struct cpufreq_governor cpufreq_gov_dbs = {
576 .name = "conservative",
577 .governor = cpufreq_governor_dbs,
578 .owner = THIS_MODULE,
579};
580
581static int __init cpufreq_gov_dbs_init(void)
582{
583 return cpufreq_register_governor(&cpufreq_gov_dbs);
584}
585
586static void __exit cpufreq_gov_dbs_exit(void)
587{
588 /* Make sure that the scheduled work is indeed not running */
589 flush_scheduled_work();
590
591 cpufreq_unregister_governor(&cpufreq_gov_dbs);
592}
593
594
595MODULE_AUTHOR ("Alexander Clouter <alex-kernel@digriz.org.uk>");
596MODULE_DESCRIPTION ("'cpufreq_conservative' - A dynamic cpufreq governor for "
597 "Low Latency Frequency Transition capable processors "
598 "optimised for use in a battery environment");
599MODULE_LICENSE ("GPL");
600
601module_init(cpufreq_gov_dbs_init);
602module_exit(cpufreq_gov_dbs_exit);