2 * drivers/cpufreq/cpufreq_conservative.c
4 * Copyright (C) 2001 Russell King
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.
14 #include <linux/cpufreq.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/kobject.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/notifier.h>
22 #include <linux/percpu-defs.h>
23 #include <linux/sysfs.h>
24 #include <linux/types.h>
26 #include "cpufreq_governor.h"
28 /* Conservative governor macros */
29 #define DEF_FREQUENCY_UP_THRESHOLD (80)
30 #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
31 #define DEF_SAMPLING_DOWN_FACTOR (1)
32 #define MAX_SAMPLING_DOWN_FACTOR (10)
34 static struct dbs_data cs_dbs_data;
35 static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info);
37 static struct cs_dbs_tuners cs_tuners = {
38 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
39 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
40 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
46 * Every sampling_rate, we check, if current idle time is less than 20%
47 * (default), then we try to increase frequency Every sampling_rate *
48 * sampling_down_factor, we check, if current idle time is more than 80%, then
49 * we try to decrease frequency
51 * Any frequency increase takes it to the maximum frequency. Frequency reduction
52 * happens at minimum steps of 5% (default) of maximum frequency
54 static void cs_check_cpu(int cpu, unsigned int load)
56 struct cs_cpu_dbs_info_s *dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
57 struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
58 unsigned int freq_target;
61 * break out if we 'cannot' reduce the speed as the user might
62 * want freq_step to be zero
64 if (cs_tuners.freq_step == 0)
67 /* Check for frequency increase */
68 if (load > cs_tuners.up_threshold) {
69 dbs_info->down_skip = 0;
71 /* if we are already at full speed then break out early */
72 if (dbs_info->requested_freq == policy->max)
75 freq_target = (cs_tuners.freq_step * policy->max) / 100;
77 /* max freq cannot be less than 100. But who knows.... */
78 if (unlikely(freq_target == 0))
81 dbs_info->requested_freq += freq_target;
82 if (dbs_info->requested_freq > policy->max)
83 dbs_info->requested_freq = policy->max;
85 __cpufreq_driver_target(policy, dbs_info->requested_freq,
91 * The optimal frequency is the frequency that is the lowest that can
92 * support the current CPU usage without triggering the up policy. To be
93 * safe, we focus 10 points under the threshold.
95 if (load < (cs_tuners.down_threshold - 10)) {
96 freq_target = (cs_tuners.freq_step * policy->max) / 100;
98 dbs_info->requested_freq -= freq_target;
99 if (dbs_info->requested_freq < policy->min)
100 dbs_info->requested_freq = policy->min;
103 * if we cannot reduce the frequency anymore, break out early
105 if (policy->cur == policy->min)
108 __cpufreq_driver_target(policy, dbs_info->requested_freq,
114 static void cs_dbs_timer(struct work_struct *work)
116 struct delayed_work *dw = to_delayed_work(work);
117 struct cs_cpu_dbs_info_s *dbs_info = container_of(work,
118 struct cs_cpu_dbs_info_s, cdbs.work.work);
119 unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
120 struct cs_cpu_dbs_info_s *core_dbs_info = &per_cpu(cs_cpu_dbs_info,
122 int delay = delay_for_sampling_rate(cs_tuners.sampling_rate);
124 mutex_lock(&core_dbs_info->cdbs.timer_mutex);
125 if (need_load_eval(&core_dbs_info->cdbs, cs_tuners.sampling_rate))
126 dbs_check_cpu(&cs_dbs_data, cpu);
128 schedule_delayed_work_on(smp_processor_id(), dw, delay);
129 mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
132 static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
135 struct cpufreq_freqs *freq = data;
136 struct cs_cpu_dbs_info_s *dbs_info =
137 &per_cpu(cs_cpu_dbs_info, freq->cpu);
138 struct cpufreq_policy *policy;
140 if (!dbs_info->enable)
143 policy = dbs_info->cdbs.cur_policy;
146 * we only care if our internally tracked freq moves outside the 'valid'
147 * ranges of frequency available to us otherwise we do not change it
149 if (dbs_info->requested_freq > policy->max
150 || dbs_info->requested_freq < policy->min)
151 dbs_info->requested_freq = freq->new;
156 /************************** sysfs interface ************************/
157 static ssize_t show_sampling_rate_min(struct kobject *kobj,
158 struct attribute *attr, char *buf)
160 return sprintf(buf, "%u\n", cs_dbs_data.min_sampling_rate);
163 static ssize_t store_sampling_down_factor(struct kobject *a,
165 const char *buf, size_t count)
169 ret = sscanf(buf, "%u", &input);
171 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
174 cs_tuners.sampling_down_factor = input;
178 static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
179 const char *buf, size_t count)
183 ret = sscanf(buf, "%u", &input);
188 cs_tuners.sampling_rate = max(input, cs_dbs_data.min_sampling_rate);
192 static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
193 const char *buf, size_t count)
197 ret = sscanf(buf, "%u", &input);
199 if (ret != 1 || input > 100 || input <= cs_tuners.down_threshold)
202 cs_tuners.up_threshold = input;
206 static ssize_t store_down_threshold(struct kobject *a, struct attribute *b,
207 const char *buf, size_t count)
211 ret = sscanf(buf, "%u", &input);
213 /* cannot be lower than 11 otherwise freq will not fall */
214 if (ret != 1 || input < 11 || input > 100 ||
215 input >= cs_tuners.up_threshold)
218 cs_tuners.down_threshold = input;
222 static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
223 const char *buf, size_t count)
225 unsigned int input, j;
228 ret = sscanf(buf, "%u", &input);
235 if (input == cs_tuners.ignore_nice) /* nothing to do */
238 cs_tuners.ignore_nice = input;
240 /* we need to re-evaluate prev_cpu_idle */
241 for_each_online_cpu(j) {
242 struct cs_cpu_dbs_info_s *dbs_info;
243 dbs_info = &per_cpu(cs_cpu_dbs_info, j);
244 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
245 &dbs_info->cdbs.prev_cpu_wall);
246 if (cs_tuners.ignore_nice)
247 dbs_info->cdbs.prev_cpu_nice =
248 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
253 static ssize_t store_freq_step(struct kobject *a, struct attribute *b,
254 const char *buf, size_t count)
258 ret = sscanf(buf, "%u", &input);
267 * no need to test here if freq_step is zero as the user might actually
268 * want this, they would be crazy though :)
270 cs_tuners.freq_step = input;
274 show_one(cs, sampling_rate, sampling_rate);
275 show_one(cs, sampling_down_factor, sampling_down_factor);
276 show_one(cs, up_threshold, up_threshold);
277 show_one(cs, down_threshold, down_threshold);
278 show_one(cs, ignore_nice_load, ignore_nice);
279 show_one(cs, freq_step, freq_step);
281 define_one_global_rw(sampling_rate);
282 define_one_global_rw(sampling_down_factor);
283 define_one_global_rw(up_threshold);
284 define_one_global_rw(down_threshold);
285 define_one_global_rw(ignore_nice_load);
286 define_one_global_rw(freq_step);
287 define_one_global_ro(sampling_rate_min);
289 static struct attribute *dbs_attributes[] = {
290 &sampling_rate_min.attr,
292 &sampling_down_factor.attr,
294 &down_threshold.attr,
295 &ignore_nice_load.attr,
300 static struct attribute_group cs_attr_group = {
301 .attrs = dbs_attributes,
302 .name = "conservative",
305 /************************** sysfs end ************************/
307 define_get_cpu_dbs_routines(cs_cpu_dbs_info);
309 static struct notifier_block cs_cpufreq_notifier_block = {
310 .notifier_call = dbs_cpufreq_notifier,
313 static struct cs_ops cs_ops = {
314 .notifier_block = &cs_cpufreq_notifier_block,
317 static struct dbs_data cs_dbs_data = {
318 .governor = GOV_CONSERVATIVE,
319 .attr_group = &cs_attr_group,
320 .tuners = &cs_tuners,
321 .get_cpu_cdbs = get_cpu_cdbs,
322 .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
323 .gov_dbs_timer = cs_dbs_timer,
324 .gov_check_cpu = cs_check_cpu,
328 static int cs_cpufreq_governor_dbs(struct cpufreq_policy *policy,
331 return cpufreq_governor_dbs(&cs_dbs_data, policy, event);
334 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
337 struct cpufreq_governor cpufreq_gov_conservative = {
338 .name = "conservative",
339 .governor = cs_cpufreq_governor_dbs,
340 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
341 .owner = THIS_MODULE,
344 static int __init cpufreq_gov_dbs_init(void)
346 mutex_init(&cs_dbs_data.mutex);
347 return cpufreq_register_governor(&cpufreq_gov_conservative);
350 static void __exit cpufreq_gov_dbs_exit(void)
352 cpufreq_unregister_governor(&cpufreq_gov_conservative);
356 MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
357 "Low Latency Frequency Transition capable processors "
358 "optimised for use in a battery environment");
359 MODULE_LICENSE("GPL");
361 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
362 fs_initcall(cpufreq_gov_dbs_init);
364 module_init(cpufreq_gov_dbs_init);
366 module_exit(cpufreq_gov_dbs_exit);