Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-mmc
[linux-drm-fsl-dcu.git] / drivers / cpufreq / cpufreq_conservative.c
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>
31 #include <linux/mutex.h>
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 DEF_FREQUENCY_DOWN_THRESHOLD            (20)
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  */
50 static unsigned int                             def_sampling_rate;
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)
55 #define MAX_SAMPLING_RATE                       (500 * def_sampling_rate)
56 #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER    (1000)
57 #define DEF_SAMPLING_DOWN_FACTOR                (1)
58 #define MAX_SAMPLING_DOWN_FACTOR                (10)
59 #define TRANSITION_LATENCY_LIMIT                (10 * 1000)
60
61 static void do_dbs_timer(void *data);
62
63 struct 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;
68         unsigned int            down_skip;
69         unsigned int            requested_freq;
70 };
71 static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
72
73 static unsigned int dbs_enable; /* number of CPUs using this policy */
74
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  */
83 static DEFINE_MUTEX     (dbs_mutex);
84 static DECLARE_WORK     (dbs_work, do_dbs_timer, NULL);
85
86 struct 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
95 static 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,
99         .ignore_nice            = 0,
100         .freq_step              = 5,
101 };
102
103 static inline unsigned int get_cpu_idle_time(unsigned int cpu)
104 {
105         return  kstat_cpu(cpu).cpustat.idle +
106                 kstat_cpu(cpu).cpustat.iowait +
107                 ( dbs_tuners_ins.ignore_nice ?
108                   kstat_cpu(cpu).cpustat.nice :
109                   0);
110 }
111
112 /************************** sysfs interface ************************/
113 static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
114 {
115         return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
116 }
117
118 static 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)                                    \
124 static struct freq_attr _name =                                 \
125 __ATTR(_name, 0444, show_##_name, NULL)
126
127 define_one_ro(sampling_rate_max);
128 define_one_ro(sampling_rate_min);
129
130 /* cpufreq_conservative Governor Tunables */
131 #define show_one(file_name, object)                                     \
132 static ssize_t show_##file_name                                         \
133 (struct cpufreq_policy *unused, char *buf)                              \
134 {                                                                       \
135         return sprintf(buf, "%u\n", dbs_tuners_ins.object);             \
136 }
137 show_one(sampling_rate, sampling_rate);
138 show_one(sampling_down_factor, sampling_down_factor);
139 show_one(up_threshold, up_threshold);
140 show_one(down_threshold, down_threshold);
141 show_one(ignore_nice_load, ignore_nice);
142 show_one(freq_step, freq_step);
143
144 static 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);
150         if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
151                 return -EINVAL;
152
153         mutex_lock(&dbs_mutex);
154         dbs_tuners_ins.sampling_down_factor = input;
155         mutex_unlock(&dbs_mutex);
156
157         return count;
158 }
159
160 static 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
167         mutex_lock(&dbs_mutex);
168         if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
169                 mutex_unlock(&dbs_mutex);
170                 return -EINVAL;
171         }
172
173         dbs_tuners_ins.sampling_rate = input;
174         mutex_unlock(&dbs_mutex);
175
176         return count;
177 }
178
179 static 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
186         mutex_lock(&dbs_mutex);
187         if (ret != 1 || input > 100 || input <= dbs_tuners_ins.down_threshold) {
188                 mutex_unlock(&dbs_mutex);
189                 return -EINVAL;
190         }
191
192         dbs_tuners_ins.up_threshold = input;
193         mutex_unlock(&dbs_mutex);
194
195         return count;
196 }
197
198 static 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
205         mutex_lock(&dbs_mutex);
206         if (ret != 1 || input > 100 || input >= dbs_tuners_ins.up_threshold) {
207                 mutex_unlock(&dbs_mutex);
208                 return -EINVAL;
209         }
210
211         dbs_tuners_ins.down_threshold = input;
212         mutex_unlock(&dbs_mutex);
213
214         return count;
215 }
216
217 static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
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         
232         mutex_lock(&dbs_mutex);
233         if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
234                 mutex_unlock(&dbs_mutex);
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 */
240         for_each_online_cpu(j) {
241                 struct cpu_dbs_info_s *j_dbs_info;
242                 j_dbs_info = &per_cpu(cpu_dbs_info, j);
243                 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
244                 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
245         }
246         mutex_unlock(&dbs_mutex);
247
248         return count;
249 }
250
251 static 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 :) */
267         mutex_lock(&dbs_mutex);
268         dbs_tuners_ins.freq_step = input;
269         mutex_unlock(&dbs_mutex);
270
271         return count;
272 }
273
274 #define define_one_rw(_name) \
275 static struct freq_attr _name = \
276 __ATTR(_name, 0644, show_##_name, store_##_name)
277
278 define_one_rw(sampling_rate);
279 define_one_rw(sampling_down_factor);
280 define_one_rw(up_threshold);
281 define_one_rw(down_threshold);
282 define_one_rw(ignore_nice_load);
283 define_one_rw(freq_step);
284
285 static 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,
292         &ignore_nice_load.attr,
293         &freq_step.attr,
294         NULL
295 };
296
297 static struct attribute_group dbs_attr_group = {
298         .attrs = dbs_attributes,
299         .name = "conservative",
300 };
301
302 /************************** sysfs end ************************/
303
304 static void dbs_check_cpu(int cpu)
305 {
306         unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
307         unsigned int tmp_idle_ticks, total_idle_ticks;
308         unsigned int freq_step;
309         unsigned int freq_down_sampling_rate;
310         struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
311         struct cpufreq_policy *policy;
312
313         if (!this_dbs_info->enable)
314                 return;
315
316         policy = this_dbs_info->cur_policy;
317
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 */
333         idle_ticks = UINT_MAX;
334
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;
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) *
347                         usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
348
349         if (idle_ticks < up_idle_ticks) {
350                 this_dbs_info->down_skip = 0;
351                 this_dbs_info->prev_cpu_idle_down =
352                         this_dbs_info->prev_cpu_idle_up;
353
354                 /* if we are already at full speed then break out early */
355                 if (this_dbs_info->requested_freq == policy->max)
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                 
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;
367
368                 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
369                         CPUFREQ_RELATION_H);
370                 return;
371         }
372
373         /* Check for frequency decrease */
374         this_dbs_info->down_skip++;
375         if (this_dbs_info->down_skip < dbs_tuners_ins.sampling_down_factor)
376                 return;
377
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;
383
384         if (tmp_idle_ticks < idle_ticks)
385                 idle_ticks = tmp_idle_ticks;
386
387         /* Scale idle ticks by 100 and compare with up and down ticks */
388         idle_ticks *= 100;
389         this_dbs_info->down_skip = 0;
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) *
394                 usecs_to_jiffies(freq_down_sampling_rate);
395
396         if (idle_ticks > down_idle_ticks) {
397                 /*
398                  * if we are already at the lowest speed then break out early
399                  * or if we 'cannot' reduce the speed as the user might want
400                  * freq_step to be zero
401                  */
402                 if (this_dbs_info->requested_freq == policy->min
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
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;
415
416                 __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
417                                 CPUFREQ_RELATION_H);
418                 return;
419         }
420 }
421
422 static void do_dbs_timer(void *data)
423
424         int i;
425         lock_cpu_hotplug();
426         mutex_lock(&dbs_mutex);
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));
431         mutex_unlock(&dbs_mutex);
432         unlock_cpu_hotplug();
433
434
435 static 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
443 static inline void dbs_timer_exit(void)
444 {
445         cancel_delayed_work(&dbs_work);
446         return;
447 }
448
449 static 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                  
470                 mutex_lock(&dbs_mutex);
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                 
476                         j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu);
477                         j_dbs_info->prev_cpu_idle_down
478                                 = j_dbs_info->prev_cpu_idle_up;
479                 }
480                 this_dbs_info->enable = 1;
481                 this_dbs_info->down_skip = 0;
482                 this_dbs_info->requested_freq = policy->cur;
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 */
492                         latency = policy->cpuinfo.transition_latency / 1000;
493                         if (latency == 0)
494                                 latency = 1;
495
496                         def_sampling_rate = 10 * latency *
497                                         DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
498
499                         if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
500                                 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
501
502                         dbs_tuners_ins.sampling_rate = def_sampling_rate;
503
504                         dbs_timer_init();
505                 }
506                 
507                 mutex_unlock(&dbs_mutex);
508                 break;
509
510         case CPUFREQ_GOV_STOP:
511                 mutex_lock(&dbs_mutex);
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                 
522                 mutex_unlock(&dbs_mutex);
523
524                 break;
525
526         case CPUFREQ_GOV_LIMITS:
527                 lock_cpu_hotplug();
528                 mutex_lock(&dbs_mutex);
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);
537                 mutex_unlock(&dbs_mutex);
538                 unlock_cpu_hotplug();
539                 break;
540         }
541         return 0;
542 }
543
544 static struct cpufreq_governor cpufreq_gov_dbs = {
545         .name           = "conservative",
546         .governor       = cpufreq_governor_dbs,
547         .owner          = THIS_MODULE,
548 };
549
550 static int __init cpufreq_gov_dbs_init(void)
551 {
552         return cpufreq_register_governor(&cpufreq_gov_dbs);
553 }
554
555 static 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
564 MODULE_AUTHOR ("Alexander Clouter <alex-kernel@digriz.org.uk>");
565 MODULE_DESCRIPTION ("'cpufreq_conservative' - A dynamic cpufreq governor for "
566                 "Low Latency Frequency Transition capable processors "
567                 "optimised for use in a battery environment");
568 MODULE_LICENSE ("GPL");
569
570 module_init(cpufreq_gov_dbs_init);
571 module_exit(cpufreq_gov_dbs_exit);