14 #include <linux/kernel.h>
15 #include <linux/module.h>
22 #include <linux/hrtimer.h>
25 #include <linux/sched.h>
32 #define DEF_FREQUENCY_UP_THRESHOLD (80)
33 #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
45 #define MIN_SAMPLING_RATE_RATIO (2)
47 static unsigned int min_sampling_rate;
49 #define LATENCY_MULTIPLIER (1000)
50 #define MIN_LATENCY_MULTIPLIER (100)
51 #define DEF_SAMPLING_DOWN_FACTOR (1)
52 #define MAX_SAMPLING_DOWN_FACTOR (10)
53 #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
76 static unsigned int dbs_enable;
83 static struct dbs_tuners {
84 unsigned int sampling_rate;
85 unsigned int sampling_down_factor;
86 unsigned int up_threshold;
87 unsigned int down_threshold;
88 unsigned int ignore_nice;
89 unsigned int freq_step;
98 static inline u64 get_cpu_idle_time_jiffy(
unsigned int cpu,
u64 *wall)
113 idle_time = cur_wall_time - busy_time;
122 u64 idle_time = get_cpu_idle_time_us(cpu,
NULL);
124 if (idle_time == -1ULL)
125 return get_cpu_idle_time_jiffy(cpu, wall);
127 idle_time += get_cpu_iowait_time_us(cpu, wall);
143 if (!this_dbs_info->
enable)
161 .notifier_call = dbs_cpufreq_notifier
168 return sprintf(buf,
"%u\n", min_sampling_rate);
174 #define show_one(file_name, object) \
175 static ssize_t show_##file_name \
176 (struct kobject *kobj, struct attribute *attr, char *buf) \
178 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
180 show_one(sampling_rate, sampling_rate);
181 show_one(sampling_down_factor, sampling_down_factor);
182 show_one(up_threshold, up_threshold);
183 show_one(down_threshold, down_threshold);
184 show_one(ignore_nice_load, ignore_nice);
193 ret =
sscanf(buf,
"%u", &input);
198 dbs_tuners_ins.sampling_down_factor =
input;
203 const char *buf,
size_t count)
207 ret =
sscanf(buf,
"%u", &input);
212 dbs_tuners_ins.sampling_rate =
max(input, min_sampling_rate);
217 const char *buf,
size_t count)
221 ret =
sscanf(buf,
"%u", &input);
223 if (ret != 1 || input > 100 ||
224 input <= dbs_tuners_ins.down_threshold)
227 dbs_tuners_ins.up_threshold =
input;
232 const char *buf,
size_t count)
236 ret =
sscanf(buf,
"%u", &input);
239 if (ret != 1 || input < 11 || input > 100 ||
240 input >= dbs_tuners_ins.up_threshold)
243 dbs_tuners_ins.down_threshold =
input;
248 const char *buf,
size_t count)
255 ret =
sscanf(buf,
"%u", &input);
262 if (input == dbs_tuners_ins.ignore_nice)
265 dbs_tuners_ins.ignore_nice =
input;
270 dbs_info = &
per_cpu(cs_cpu_dbs_info, j);
273 if (dbs_tuners_ins.ignore_nice)
280 const char *buf,
size_t count)
284 ret =
sscanf(buf,
"%u", &input);
294 dbs_tuners_ins.freq_step =
input;
305 static struct attribute *dbs_attributes[] = {
306 &sampling_rate_min.attr,
308 &sampling_down_factor.attr,
310 &down_threshold.attr,
311 &ignore_nice_load.attr,
317 .attrs = dbs_attributes,
318 .
name =
"conservative",
325 unsigned int load = 0;
326 unsigned int max_load = 0;
327 unsigned int freq_target;
349 unsigned int idle_time, wall_time;
351 j_dbs_info = &
per_cpu(cs_cpu_dbs_info, j);
353 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
355 wall_time = (
unsigned int)
359 idle_time = (
unsigned int)
363 if (dbs_tuners_ins.ignore_nice) {
365 unsigned long cur_nice_jiffies;
373 cur_nice_jiffies = (
unsigned long)
380 if (
unlikely(!wall_time || wall_time < idle_time))
383 load = 100 * (wall_time - idle_time) / wall_time;
393 if (dbs_tuners_ins.freq_step == 0)
397 if (max_load > dbs_tuners_ins.up_threshold) {
404 freq_target = (dbs_tuners_ins.freq_step * policy->
max) / 100;
424 if (max_load < (dbs_tuners_ins.down_threshold - 10)) {
425 freq_target = (dbs_tuners_ins.freq_step * policy->
max) / 100;
434 if (policy->
cur == policy->
min)
447 unsigned int cpu = dbs_info->
cpu;
456 dbs_check_cpu(dbs_info);
462 static inline void dbs_timer_init(
struct cpu_dbs_info_s *dbs_info)
473 static inline void dbs_timer_exit(
struct cpu_dbs_info_s *dbs_info)
482 unsigned int cpu = policy->
cpu;
487 this_dbs_info = &
per_cpu(cs_cpu_dbs_info, cpu);
498 j_dbs_info = &
per_cpu(cs_cpu_dbs_info, j);
503 if (dbs_tuners_ins.ignore_nice)
517 if (dbs_enable == 1) {
520 latency = policy->
cpuinfo.transition_latency / 1000;
538 min_sampling_rate =
max(min_sampling_rate,
540 dbs_tuners_ins.sampling_rate =
541 max(min_sampling_rate,
545 &dbs_cpufreq_notifier_block,
550 dbs_timer_init(this_dbs_info);
555 dbs_timer_exit(this_dbs_info);
567 &dbs_cpufreq_notifier_block,
587 dbs_check_cpu(this_dbs_info);
595 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
599 .name =
"conservative",
600 .governor = cpufreq_governor_dbs,
605 static int __init cpufreq_gov_dbs_init(
void)
610 static void __exit cpufreq_gov_dbs_exit(
void)
618 "Low Latency Frequency Transition capable processors "
619 "optimised for use in a battery environment");
622 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE