[CPUFREQ] make cpufreq_gov_dbs static
[linux-2.6/linux-mips.git] / drivers / cpufreq / cpufreq_ondemand.c
blob84c658822a105393801b01d243dbb6b719593c52
1 /*
2 * drivers/cpufreq/cpufreq_ondemand.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com>
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.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/smp.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/ctype.h>
19 #include <linux/cpufreq.h>
20 #include <linux/sysctl.h>
21 #include <linux/types.h>
22 #include <linux/fs.h>
23 #include <linux/sysfs.h>
24 #include <linux/sched.h>
25 #include <linux/kmod.h>
26 #include <linux/workqueue.h>
27 #include <linux/jiffies.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/percpu.h>
32 * dbs is used in this file as a shortform for demandbased switching
33 * It helps to keep variable names smaller, simpler
36 #define DEF_FREQUENCY_UP_THRESHOLD (80)
37 #define MIN_FREQUENCY_UP_THRESHOLD (0)
38 #define MAX_FREQUENCY_UP_THRESHOLD (100)
40 #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
41 #define MIN_FREQUENCY_DOWN_THRESHOLD (0)
42 #define MAX_FREQUENCY_DOWN_THRESHOLD (100)
44 /*
45 * The polling frequency of this governor depends on the capability of
46 * the processor. Default polling frequency is 1000 times the transition
47 * latency of the processor. The governor will work on any processor with
48 * transition latency <= 10mS, using appropriate sampling
49 * rate.
50 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
51 * this governor will not work.
52 * All times here are in uS.
54 static unsigned int def_sampling_rate;
55 #define MIN_SAMPLING_RATE (def_sampling_rate / 2)
56 #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
57 #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
58 #define DEF_SAMPLING_DOWN_FACTOR (10)
59 #define TRANSITION_LATENCY_LIMIT (10 * 1000)
61 static void do_dbs_timer(void *data);
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;
69 static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
71 static unsigned int dbs_enable; /* number of CPUs using this policy */
73 static DECLARE_MUTEX (dbs_sem);
74 static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
76 struct dbs_tuners {
77 unsigned int sampling_rate;
78 unsigned int sampling_down_factor;
79 unsigned int up_threshold;
80 unsigned int down_threshold;
83 static struct dbs_tuners dbs_tuners_ins = {
84 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
85 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
86 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
89 /************************** sysfs interface ************************/
90 static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
92 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
95 static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
97 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
100 #define define_one_ro(_name) \
101 static struct freq_attr _name = \
102 __ATTR(_name, 0444, show_##_name, NULL)
104 define_one_ro(sampling_rate_max);
105 define_one_ro(sampling_rate_min);
107 /* cpufreq_ondemand Governor Tunables */
108 #define show_one(file_name, object) \
109 static ssize_t show_##file_name \
110 (struct cpufreq_policy *unused, char *buf) \
112 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
114 show_one(sampling_rate, sampling_rate);
115 show_one(sampling_down_factor, sampling_down_factor);
116 show_one(up_threshold, up_threshold);
117 show_one(down_threshold, down_threshold);
119 static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
120 const char *buf, size_t count)
122 unsigned int input;
123 int ret;
124 ret = sscanf (buf, "%u", &input);
125 if (ret != 1 )
126 return -EINVAL;
128 down(&dbs_sem);
129 dbs_tuners_ins.sampling_down_factor = input;
130 up(&dbs_sem);
132 return count;
135 static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
136 const char *buf, size_t count)
138 unsigned int input;
139 int ret;
140 ret = sscanf (buf, "%u", &input);
142 down(&dbs_sem);
143 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
144 up(&dbs_sem);
145 return -EINVAL;
148 dbs_tuners_ins.sampling_rate = input;
149 up(&dbs_sem);
151 return count;
154 static ssize_t store_up_threshold(struct cpufreq_policy *unused,
155 const char *buf, size_t count)
157 unsigned int input;
158 int ret;
159 ret = sscanf (buf, "%u", &input);
161 down(&dbs_sem);
162 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
163 input < MIN_FREQUENCY_UP_THRESHOLD ||
164 input <= dbs_tuners_ins.down_threshold) {
165 up(&dbs_sem);
166 return -EINVAL;
169 dbs_tuners_ins.up_threshold = input;
170 up(&dbs_sem);
172 return count;
175 static ssize_t store_down_threshold(struct cpufreq_policy *unused,
176 const char *buf, size_t count)
178 unsigned int input;
179 int ret;
180 ret = sscanf (buf, "%u", &input);
182 down(&dbs_sem);
183 if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
184 input < MIN_FREQUENCY_DOWN_THRESHOLD ||
185 input >= dbs_tuners_ins.up_threshold) {
186 up(&dbs_sem);
187 return -EINVAL;
190 dbs_tuners_ins.down_threshold = input;
191 up(&dbs_sem);
193 return count;
196 #define define_one_rw(_name) \
197 static struct freq_attr _name = \
198 __ATTR(_name, 0644, show_##_name, store_##_name)
200 define_one_rw(sampling_rate);
201 define_one_rw(sampling_down_factor);
202 define_one_rw(up_threshold);
203 define_one_rw(down_threshold);
205 static struct attribute * dbs_attributes[] = {
206 &sampling_rate_max.attr,
207 &sampling_rate_min.attr,
208 &sampling_rate.attr,
209 &sampling_down_factor.attr,
210 &up_threshold.attr,
211 &down_threshold.attr,
212 NULL
215 static struct attribute_group dbs_attr_group = {
216 .attrs = dbs_attributes,
217 .name = "ondemand",
220 /************************** sysfs end ************************/
222 static void dbs_check_cpu(int cpu)
224 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
225 unsigned int total_idle_ticks;
226 unsigned int freq_down_step;
227 unsigned int freq_down_sampling_rate;
228 static int down_skip[NR_CPUS];
229 struct cpu_dbs_info_s *this_dbs_info;
231 struct cpufreq_policy *policy;
232 unsigned int j;
234 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
235 if (!this_dbs_info->enable)
236 return;
238 policy = this_dbs_info->cur_policy;
240 * The default safe range is 20% to 80%
241 * Every sampling_rate, we check
242 * - If current idle time is less than 20%, then we try to
243 * increase frequency
244 * Every sampling_rate*sampling_down_factor, we check
245 * - If current idle time is more than 80%, then we try to
246 * decrease frequency
248 * Any frequency increase takes it to the maximum frequency.
249 * Frequency reduction happens at minimum steps of
250 * 5% of max_frequency
253 /* Check for frequency increase */
254 total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
255 kstat_cpu(cpu).cpustat.iowait;
256 idle_ticks = total_idle_ticks -
257 this_dbs_info->prev_cpu_idle_up;
258 this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
261 for_each_cpu_mask(j, policy->cpus) {
262 unsigned int tmp_idle_ticks;
263 struct cpu_dbs_info_s *j_dbs_info;
265 if (j == cpu)
266 continue;
268 j_dbs_info = &per_cpu(cpu_dbs_info, j);
269 /* Check for frequency increase */
270 total_idle_ticks = kstat_cpu(j).cpustat.idle +
271 kstat_cpu(j).cpustat.iowait;
272 tmp_idle_ticks = total_idle_ticks -
273 j_dbs_info->prev_cpu_idle_up;
274 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
276 if (tmp_idle_ticks < idle_ticks)
277 idle_ticks = tmp_idle_ticks;
280 /* Scale idle ticks by 100 and compare with up and down ticks */
281 idle_ticks *= 100;
282 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
283 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
285 if (idle_ticks < up_idle_ticks) {
286 __cpufreq_driver_target(policy, policy->max,
287 CPUFREQ_RELATION_H);
288 down_skip[cpu] = 0;
289 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
290 return;
293 /* Check for frequency decrease */
294 down_skip[cpu]++;
295 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
296 return;
298 total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
299 kstat_cpu(cpu).cpustat.iowait;
300 idle_ticks = total_idle_ticks -
301 this_dbs_info->prev_cpu_idle_down;
302 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
304 for_each_cpu_mask(j, policy->cpus) {
305 unsigned int tmp_idle_ticks;
306 struct cpu_dbs_info_s *j_dbs_info;
308 if (j == cpu)
309 continue;
311 j_dbs_info = &per_cpu(cpu_dbs_info, j);
312 /* Check for frequency increase */
313 total_idle_ticks = kstat_cpu(j).cpustat.idle +
314 kstat_cpu(j).cpustat.iowait;
315 tmp_idle_ticks = total_idle_ticks -
316 j_dbs_info->prev_cpu_idle_down;
317 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
319 if (tmp_idle_ticks < idle_ticks)
320 idle_ticks = tmp_idle_ticks;
323 /* Scale idle ticks by 100 and compare with up and down ticks */
324 idle_ticks *= 100;
325 down_skip[cpu] = 0;
327 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
328 dbs_tuners_ins.sampling_down_factor;
329 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
330 usecs_to_jiffies(freq_down_sampling_rate);
332 if (idle_ticks > down_idle_ticks ) {
333 freq_down_step = (5 * policy->max) / 100;
335 /* max freq cannot be less than 100. But who knows.... */
336 if (unlikely(freq_down_step == 0))
337 freq_down_step = 5;
339 __cpufreq_driver_target(policy,
340 policy->cur - freq_down_step,
341 CPUFREQ_RELATION_H);
342 return;
346 static void do_dbs_timer(void *data)
348 int i;
349 down(&dbs_sem);
350 for_each_online_cpu(i)
351 dbs_check_cpu(i);
352 schedule_delayed_work(&dbs_work,
353 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
354 up(&dbs_sem);
357 static inline void dbs_timer_init(void)
359 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
360 schedule_delayed_work(&dbs_work,
361 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
362 return;
365 static inline void dbs_timer_exit(void)
367 cancel_delayed_work(&dbs_work);
368 return;
371 static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
372 unsigned int event)
374 unsigned int cpu = policy->cpu;
375 struct cpu_dbs_info_s *this_dbs_info;
376 unsigned int j;
378 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
380 switch (event) {
381 case CPUFREQ_GOV_START:
382 if ((!cpu_online(cpu)) ||
383 (!policy->cur))
384 return -EINVAL;
386 if (policy->cpuinfo.transition_latency >
387 (TRANSITION_LATENCY_LIMIT * 1000))
388 return -EINVAL;
389 if (this_dbs_info->enable) /* Already enabled */
390 break;
392 down(&dbs_sem);
393 for_each_cpu_mask(j, policy->cpus) {
394 struct cpu_dbs_info_s *j_dbs_info;
395 j_dbs_info = &per_cpu(cpu_dbs_info, j);
396 j_dbs_info->cur_policy = policy;
398 j_dbs_info->prev_cpu_idle_up =
399 kstat_cpu(j).cpustat.idle +
400 kstat_cpu(j).cpustat.iowait;
401 j_dbs_info->prev_cpu_idle_down =
402 kstat_cpu(j).cpustat.idle +
403 kstat_cpu(j).cpustat.iowait;
405 this_dbs_info->enable = 1;
406 sysfs_create_group(&policy->kobj, &dbs_attr_group);
407 dbs_enable++;
409 * Start the timerschedule work, when this governor
410 * is used for first time
412 if (dbs_enable == 1) {
413 unsigned int latency;
414 /* policy latency is in nS. Convert it to uS first */
416 latency = policy->cpuinfo.transition_latency;
417 if (latency < 1000)
418 latency = 1000;
420 def_sampling_rate = (latency / 1000) *
421 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
422 dbs_tuners_ins.sampling_rate = def_sampling_rate;
424 dbs_timer_init();
427 up(&dbs_sem);
428 break;
430 case CPUFREQ_GOV_STOP:
431 down(&dbs_sem);
432 this_dbs_info->enable = 0;
433 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
434 dbs_enable--;
436 * Stop the timerschedule work, when this governor
437 * is used for first time
439 if (dbs_enable == 0)
440 dbs_timer_exit();
442 up(&dbs_sem);
444 break;
446 case CPUFREQ_GOV_LIMITS:
447 down(&dbs_sem);
448 if (policy->max < this_dbs_info->cur_policy->cur)
449 __cpufreq_driver_target(
450 this_dbs_info->cur_policy,
451 policy->max, CPUFREQ_RELATION_H);
452 else if (policy->min > this_dbs_info->cur_policy->cur)
453 __cpufreq_driver_target(
454 this_dbs_info->cur_policy,
455 policy->min, CPUFREQ_RELATION_L);
456 up(&dbs_sem);
457 break;
459 return 0;
462 static struct cpufreq_governor cpufreq_gov_dbs = {
463 .name = "ondemand",
464 .governor = cpufreq_governor_dbs,
465 .owner = THIS_MODULE,
468 static int __init cpufreq_gov_dbs_init(void)
470 return cpufreq_register_governor(&cpufreq_gov_dbs);
473 static void __exit cpufreq_gov_dbs_exit(void)
475 /* Make sure that the scheduled work is indeed not running */
476 flush_scheduled_work();
478 cpufreq_unregister_governor(&cpufreq_gov_dbs);
482 MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
483 MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
484 "Low Latency Frequency Transition capable processors");
485 MODULE_LICENSE ("GPL");
487 module_init(cpufreq_gov_dbs_init);
488 module_exit(cpufreq_gov_dbs_exit);