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/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>
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>
34 * dbs is used in this file as a shortform for demandbased switching
35 * It helps to keep variable names smaller, simpler
38 #define DEF_FREQUENCY_UP_THRESHOLD (80)
39 #define MIN_FREQUENCY_UP_THRESHOLD (0)
40 #define MAX_FREQUENCY_UP_THRESHOLD (100)
42 #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
43 #define MIN_FREQUENCY_DOWN_THRESHOLD (0)
44 #define MAX_FREQUENCY_DOWN_THRESHOLD (100)
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
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.
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)))
64 static void do_dbs_timer(void *data);
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;
72 static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
74 static unsigned int dbs_enable; /* number of CPUs using this policy */
76 static DECLARE_MUTEX (dbs_sem);
77 static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
80 unsigned int sampling_rate;
81 unsigned int sampling_down_factor;
82 unsigned int up_threshold;
83 unsigned int down_threshold;
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,
92 /************************** sysfs interface ************************/
93 static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
95 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
98 static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
100 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
103 #define define_one_ro(_name) \
104 static struct freq_attr _name = { \
105 .attr = { .name = __stringify(_name), .mode = 0444 }, \
106 .show = show_##_name, \
109 define_one_ro(sampling_rate_max);
110 define_one_ro(sampling_rate_min);
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) \
117 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
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);
124 static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
125 const char *buf, size_t count)
129 ret = sscanf (buf, "%u", &input);
134 dbs_tuners_ins.sampling_down_factor = input;
140 static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
141 const char *buf, size_t count)
145 ret = sscanf (buf, "%u", &input);
147 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE)
150 dbs_tuners_ins.sampling_rate = input;
156 static ssize_t store_up_threshold(struct cpufreq_policy *unused,
157 const char *buf, size_t count)
161 ret = sscanf (buf, "%u", &input);
163 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
164 input < MIN_FREQUENCY_UP_THRESHOLD ||
165 input <= dbs_tuners_ins.down_threshold)
168 dbs_tuners_ins.up_threshold = input;
174 static ssize_t store_down_threshold(struct cpufreq_policy *unused,
175 const char *buf, size_t count)
179 ret = sscanf (buf, "%u", &input);
181 if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
182 input < MIN_FREQUENCY_DOWN_THRESHOLD ||
183 input >= dbs_tuners_ins.up_threshold)
186 dbs_tuners_ins.down_threshold = input;
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, \
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);
204 static struct attribute * dbs_attributes[] = {
205 &sampling_rate_max.attr,
206 &sampling_rate_min.attr,
208 &sampling_down_factor.attr,
210 &down_threshold.attr,
214 static struct attribute_group dbs_attr_group = {
215 .attrs = dbs_attributes,
219 /************************** sysfs end ************************/
221 static void dbs_check_cpu(int cpu)
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;
230 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
231 if (!this_dbs_info->enable)
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
239 * Every sampling_rate*sampling_down_factor, we check
240 * - If current idle time is more than 80%, then we try to
243 * Any frequency increase takes it to the maximum frequency.
244 * Frequency reduction happens at minimum steps of
245 * 5% of max_frequency
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;
254 /* Scale idle ticks by 100 and compare with up and down ticks */
256 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
257 sampling_rate_in_HZ(dbs_tuners_ins.sampling_rate);
259 if (idle_ticks < up_idle_ticks) {
260 __cpufreq_driver_target(this_dbs_info->cur_policy,
261 this_dbs_info->cur_policy->max,
264 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
268 /* Check for frequency decrease */
270 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
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 */
278 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
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);
285 if (idle_ticks > down_idle_ticks ) {
286 freq_down_step = (5 * this_dbs_info->cur_policy->max) / 100;
288 /* max freq cannot be less than 100. But who knows.... */
289 if (unlikely(freq_down_step == 0))
292 __cpufreq_driver_target(this_dbs_info->cur_policy,
293 this_dbs_info->cur_policy->cur - freq_down_step,
299 static void do_dbs_timer(void *data)
303 for (i = 0; i < NR_CPUS; i++)
306 schedule_delayed_work(&dbs_work,
307 sampling_rate_in_HZ(dbs_tuners_ins.sampling_rate));
311 static inline void dbs_timer_init(void)
313 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
314 schedule_work(&dbs_work);
318 static inline void dbs_timer_exit(void)
320 cancel_delayed_work(&dbs_work);
324 static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
327 unsigned int cpu = policy->cpu;
328 struct cpu_dbs_info_s *this_dbs_info;
330 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
333 case CPUFREQ_GOV_START:
334 if ((!cpu_online(cpu)) ||
338 if (policy->cpuinfo.transition_latency >
339 (TRANSITION_LATENCY_LIMIT * 1000))
341 if (this_dbs_info->enable) /* Already enabled */
345 this_dbs_info->cur_policy = policy;
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);
357 * Start the timerschedule work, when this governor
358 * is used for first time
360 if (dbs_enable == 1) {
361 unsigned int latency;
362 /* policy latency is in nS. Convert it to uS first */
364 latency = policy->cpuinfo.transition_latency;
368 def_sampling_rate = (latency / 1000) *
369 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
370 dbs_tuners_ins.sampling_rate = def_sampling_rate;
378 case CPUFREQ_GOV_STOP:
380 this_dbs_info->enable = 0;
381 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
384 * Stop the timerschedule work, when this governor
385 * is used for first time
394 case CPUFREQ_GOV_LIMITS:
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);
410 struct cpufreq_governor cpufreq_gov_dbs = {
412 .governor = cpufreq_governor_dbs,
413 .owner = THIS_MODULE,
415 EXPORT_SYMBOL(cpufreq_gov_dbs);
417 static int __init cpufreq_gov_dbs_init(void)
419 return cpufreq_register_governor(&cpufreq_gov_dbs);
422 static void __exit cpufreq_gov_dbs_exit(void)
424 /* Make sure that the scheduled work is indeed not running */
425 flush_scheduled_work();
427 cpufreq_unregister_governor(&cpufreq_gov_dbs);
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");
436 module_init(cpufreq_gov_dbs_init);
437 module_exit(cpufreq_gov_dbs_exit);