This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / cpufreq / cpufreq_ondemand.c
1 /*
2  *  drivers/cpufreq/cpufreq_ondemand.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  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/config.h>
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/config.h>
30 #include <linux/kernel_stat.h>
31 #include <linux/percpu.h>
32
33 /*
34  * dbs is used in this file as a shortform for demandbased switching
35  * It helps to keep variable names smaller, simpler
36  */
37
38 #define DEF_FREQUENCY_UP_THRESHOLD              (80)
39 #define MIN_FREQUENCY_UP_THRESHOLD              (0)
40 #define MAX_FREQUENCY_UP_THRESHOLD              (100)
41
42 #define DEF_FREQUENCY_DOWN_THRESHOLD            (20)
43 #define MIN_FREQUENCY_DOWN_THRESHOLD            (0)
44 #define MAX_FREQUENCY_DOWN_THRESHOLD            (100)
45
46 /* 
47  * The polling frequency of this governor depends on the capability of 
48  * the processor. Default polling frequency is 1000 times the transition
49  * latency of the processor. The governor will work on any processor with 
50  * transition latency <= 10mS, using appropriate sampling 
51  * rate.
52  * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
53  * this governor will not work.
54  * All times here are in uS.
55  */
56 static unsigned int                             def_sampling_rate;
57 #define MIN_SAMPLING_RATE                       (def_sampling_rate / 2)
58 #define MAX_SAMPLING_RATE                       (500 * def_sampling_rate)
59 #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER    (1000)
60 #define DEF_SAMPLING_DOWN_FACTOR                (10)
61 #define TRANSITION_LATENCY_LIMIT                (10 * 1000)
62 #define sampling_rate_in_HZ(x)                  (((x * HZ) < (1000 * 1000))?1:((x * HZ) / (1000 * 1000)))
63
64 static void do_dbs_timer(void *data);
65
66 struct cpu_dbs_info_s {
67         struct cpufreq_policy   *cur_policy;
68         unsigned int            prev_cpu_idle_up;
69         unsigned int            prev_cpu_idle_down;
70         unsigned int            enable;
71 };
72 static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
73
74 static unsigned int dbs_enable; /* number of CPUs using this policy */
75
76 static DECLARE_MUTEX    (dbs_sem);
77 static DECLARE_WORK     (dbs_work, do_dbs_timer, NULL);
78
79 struct dbs_tuners {
80         unsigned int            sampling_rate;
81         unsigned int            sampling_down_factor;
82         unsigned int            up_threshold;
83         unsigned int            down_threshold;
84 };
85
86 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
92 /************************** sysfs interface ************************/
93 static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
94 {
95         return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
96 }
97
98 static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
99 {
100         return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
101 }
102
103 #define define_one_ro(_name)                                    \
104 static struct freq_attr _name = {                               \
105         .attr = { .name = __stringify(_name), .mode = 0444 },   \
106         .show = show_##_name,                                   \
107 }
108
109 define_one_ro(sampling_rate_max);
110 define_one_ro(sampling_rate_min);
111
112 /* cpufreq_ondemand Governor Tunables */
113 #define show_one(file_name, object)                                     \
114 static ssize_t show_##file_name                                         \
115 (struct cpufreq_policy *unused, char *buf)                              \
116 {                                                                       \
117         return sprintf(buf, "%u\n", dbs_tuners_ins.object);             \
118 }
119 show_one(sampling_rate, sampling_rate);
120 show_one(sampling_down_factor, sampling_down_factor);
121 show_one(up_threshold, up_threshold);
122 show_one(down_threshold, down_threshold);
123
124 static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused, 
125                 const char *buf, size_t count)
126 {
127         unsigned int input;
128         int ret;
129         ret = sscanf (buf, "%u", &input);
130         down(&dbs_sem);
131         if (ret != 1 )
132                 goto out;
133
134         dbs_tuners_ins.sampling_down_factor = input;
135 out:
136         up(&dbs_sem);
137         return count;
138 }
139
140 static ssize_t store_sampling_rate(struct cpufreq_policy *unused, 
141                 const char *buf, size_t count)
142 {
143         unsigned int input;
144         int ret;
145         ret = sscanf (buf, "%u", &input);
146         down(&dbs_sem);
147         if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE)
148                 goto out;
149
150         dbs_tuners_ins.sampling_rate = input;
151 out:
152         up(&dbs_sem);
153         return count;
154 }
155
156 static ssize_t store_up_threshold(struct cpufreq_policy *unused, 
157                 const char *buf, size_t count)
158 {
159         unsigned int input;
160         int ret;
161         ret = sscanf (buf, "%u", &input);
162         down(&dbs_sem);
163         if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || 
164                         input < MIN_FREQUENCY_UP_THRESHOLD ||
165                         input <= dbs_tuners_ins.down_threshold)
166                 goto out;
167
168         dbs_tuners_ins.up_threshold = input;
169 out:
170         up(&dbs_sem);
171         return count;
172 }
173
174 static ssize_t store_down_threshold(struct cpufreq_policy *unused, 
175                 const char *buf, size_t count)
176 {
177         unsigned int input;
178         int ret;
179         ret = sscanf (buf, "%u", &input);
180         down(&dbs_sem);
181         if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD || 
182                         input < MIN_FREQUENCY_DOWN_THRESHOLD ||
183                         input >= dbs_tuners_ins.up_threshold)
184                 goto out;
185
186         dbs_tuners_ins.down_threshold = input;
187 out:
188         up(&dbs_sem);
189         return count;
190 }
191
192 #define define_one_rw(_name)                                    \
193 static struct freq_attr _name = {                               \
194         .attr = { .name = __stringify(_name), .mode = 0644 },   \
195         .show = show_##_name,                                   \
196         .store = store_##_name,                                 \
197 }
198
199 define_one_rw(sampling_rate);
200 define_one_rw(sampling_down_factor);
201 define_one_rw(up_threshold);
202 define_one_rw(down_threshold);
203
204 static struct attribute * dbs_attributes[] = {
205         &sampling_rate_max.attr,
206         &sampling_rate_min.attr,
207         &sampling_rate.attr,
208         &sampling_down_factor.attr,
209         &up_threshold.attr,
210         &down_threshold.attr,
211         NULL
212 };
213
214 static struct attribute_group dbs_attr_group = {
215         .attrs = dbs_attributes,
216         .name = "ondemand",
217 };
218
219 /************************** sysfs end ************************/
220
221 static void dbs_check_cpu(int cpu)
222 {
223         unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
224         unsigned int total_idle_ticks;
225         unsigned int freq_down_step;
226         unsigned int freq_down_sampling_rate;
227         static int down_skip[NR_CPUS];
228         struct cpu_dbs_info_s *this_dbs_info;
229
230         this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
231         if (!this_dbs_info->enable)
232                 return;
233
234         /* 
235          * The default safe range is 20% to 80% 
236          * Every sampling_rate, we check
237          *      - If current idle time is less than 20%, then we try to 
238          *        increase frequency
239          * Every sampling_rate*sampling_down_factor, we check
240          *      - If current idle time is more than 80%, then we try to
241          *        decrease frequency
242          *
243          * Any frequency increase takes it to the maximum frequency. 
244          * Frequency reduction happens at minimum steps of 
245          * 5% of max_frequency 
246          */
247         /* Check for frequency increase */
248         total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
249                 kstat_cpu(cpu).cpustat.iowait;
250         idle_ticks = total_idle_ticks -
251                 this_dbs_info->prev_cpu_idle_up;
252         this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
253
254         /* Scale idle ticks by 100 and compare with up and down ticks */
255         idle_ticks *= 100;
256         up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
257                         sampling_rate_in_HZ(dbs_tuners_ins.sampling_rate);
258
259         if (idle_ticks < up_idle_ticks) {
260                 __cpufreq_driver_target(this_dbs_info->cur_policy,
261                         this_dbs_info->cur_policy->max, 
262                         CPUFREQ_RELATION_H);
263                 down_skip[cpu] = 0;
264                 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
265                 return;
266         }
267
268         /* Check for frequency decrease */
269         down_skip[cpu]++;
270         if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
271                 return;
272
273         idle_ticks = total_idle_ticks -
274                 this_dbs_info->prev_cpu_idle_down;
275         /* Scale idle ticks by 100 and compare with up and down ticks */
276         idle_ticks *= 100;
277         down_skip[cpu] = 0;
278         this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
279
280         freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
281                 dbs_tuners_ins.sampling_down_factor;
282         down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
283                         sampling_rate_in_HZ(freq_down_sampling_rate);
284
285         if (idle_ticks > down_idle_ticks ) {
286                 freq_down_step = (5 * this_dbs_info->cur_policy->max) / 100;
287
288                 /* max freq cannot be less than 100. But who knows.... */
289                 if (unlikely(freq_down_step == 0))
290                         freq_down_step = 5;
291
292                 __cpufreq_driver_target(this_dbs_info->cur_policy,
293                         this_dbs_info->cur_policy->cur - freq_down_step, 
294                         CPUFREQ_RELATION_H);
295                 return;
296         }
297 }
298
299 static void do_dbs_timer(void *data)
300
301         int i;
302         down(&dbs_sem);
303         for (i = 0; i < NR_CPUS; i++)
304                 if (cpu_online(i))
305                         dbs_check_cpu(i);
306         schedule_delayed_work(&dbs_work, 
307                         sampling_rate_in_HZ(dbs_tuners_ins.sampling_rate));
308         up(&dbs_sem);
309
310
311 static inline void dbs_timer_init(void)
312 {
313         INIT_WORK(&dbs_work, do_dbs_timer, NULL);
314         schedule_work(&dbs_work);
315         return;
316 }
317
318 static inline void dbs_timer_exit(void)
319 {
320         cancel_delayed_work(&dbs_work);
321         return;
322 }
323
324 static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
325                                    unsigned int event)
326 {
327         unsigned int cpu = policy->cpu;
328         struct cpu_dbs_info_s *this_dbs_info;
329
330         this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
331
332         switch (event) {
333         case CPUFREQ_GOV_START:
334                 if ((!cpu_online(cpu)) || 
335                     (!policy->cur))
336                         return -EINVAL;
337
338                 if (policy->cpuinfo.transition_latency >
339                                 (TRANSITION_LATENCY_LIMIT * 1000))
340                         return -EINVAL;
341                 if (this_dbs_info->enable) /* Already enabled */
342                         break;
343                  
344                 down(&dbs_sem);
345                 this_dbs_info->cur_policy = policy;
346                 
347                 this_dbs_info->prev_cpu_idle_up = 
348                                 kstat_cpu(cpu).cpustat.idle +
349                                 kstat_cpu(cpu).cpustat.iowait;
350                 this_dbs_info->prev_cpu_idle_down = 
351                                 kstat_cpu(cpu).cpustat.idle +
352                                 kstat_cpu(cpu).cpustat.iowait;
353                 this_dbs_info->enable = 1;
354                 sysfs_create_group(&policy->kobj, &dbs_attr_group);
355                 dbs_enable++;
356                 /*
357                  * Start the timerschedule work, when this governor
358                  * is used for first time
359                  */
360                 if (dbs_enable == 1) {
361                         unsigned int latency;
362                         /* policy latency is in nS. Convert it to uS first */
363
364                         latency = policy->cpuinfo.transition_latency;
365                         if (latency < 1000)
366                                 latency = 1000;
367
368                         def_sampling_rate = (latency / 1000) *
369                                         DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
370                         dbs_tuners_ins.sampling_rate = def_sampling_rate;
371
372                         dbs_timer_init();
373                 }
374                 
375                 up(&dbs_sem);
376                 break;
377
378         case CPUFREQ_GOV_STOP:
379                 down(&dbs_sem);
380                 this_dbs_info->enable = 0;
381                 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
382                 dbs_enable--;
383                 /*
384                  * Stop the timerschedule work, when this governor
385                  * is used for first time
386                  */
387                 if (dbs_enable == 0) 
388                         dbs_timer_exit();
389                 
390                 up(&dbs_sem);
391
392                 break;
393
394         case CPUFREQ_GOV_LIMITS:
395                 down(&dbs_sem);
396                 if (policy->max < this_dbs_info->cur_policy->cur)
397                         __cpufreq_driver_target(
398                                         this_dbs_info->cur_policy,
399                                         policy->max, CPUFREQ_RELATION_H);
400                 else if (policy->min > this_dbs_info->cur_policy->cur)
401                         __cpufreq_driver_target(
402                                         this_dbs_info->cur_policy,
403                                         policy->min, CPUFREQ_RELATION_L);
404                 up(&dbs_sem);
405                 break;
406         }
407         return 0;
408 }
409
410 struct cpufreq_governor cpufreq_gov_dbs = {
411         .name           = "ondemand",
412         .governor       = cpufreq_governor_dbs,
413         .owner          = THIS_MODULE,
414 };
415 EXPORT_SYMBOL(cpufreq_gov_dbs);
416
417 static int __init cpufreq_gov_dbs_init(void)
418 {
419         return cpufreq_register_governor(&cpufreq_gov_dbs);
420 }
421
422 static void __exit cpufreq_gov_dbs_exit(void)
423 {
424         /* Make sure that the scheduled work is indeed not running */
425         flush_scheduled_work();
426
427         cpufreq_unregister_governor(&cpufreq_gov_dbs);
428 }
429
430
431 MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
432 MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
433                 "Low Latency Frequency Transition capable processors");
434 MODULE_LICENSE ("GPL");
435
436 module_init(cpufreq_gov_dbs_init);
437 module_exit(cpufreq_gov_dbs_exit);