13 #include <linux/kernel.h>
14 #include <linux/module.h>
21 #include <linux/hrtimer.h>
24 #include <linux/sched.h>
31 #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
32 #define DEF_FREQUENCY_UP_THRESHOLD (80)
33 #define DEF_SAMPLING_DOWN_FACTOR (1)
34 #define MAX_SAMPLING_DOWN_FACTOR (100000)
35 #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
36 #define MICRO_FREQUENCY_UP_THRESHOLD (95)
37 #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
38 #define MIN_FREQUENCY_UP_THRESHOLD (11)
39 #define MAX_FREQUENCY_UP_THRESHOLD (100)
51 #define MIN_SAMPLING_RATE_RATIO (2)
53 static unsigned int min_sampling_rate;
55 #define LATENCY_MULTIPLIER (1000)
56 #define MIN_LATENCY_MULTIPLIER (100)
57 #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
63 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
68 .governor = cpufreq_governor_dbs,
99 static unsigned int dbs_enable;
106 static struct dbs_tuners {
107 unsigned int sampling_rate;
108 unsigned int up_threshold;
109 unsigned int down_differential;
110 unsigned int ignore_nice;
111 unsigned int sampling_down_factor;
112 unsigned int powersave_bias;
113 unsigned int io_is_busy;
122 static inline u64 get_cpu_idle_time_jiffy(
unsigned int cpu,
u64 *wall)
137 idle_time = cur_wall_time - busy_time;
146 u64 idle_time = get_cpu_idle_time_us(cpu,
NULL);
148 if (idle_time == -1ULL)
149 return get_cpu_idle_time_jiffy(cpu, wall);
151 idle_time += get_cpu_iowait_time_us(cpu, wall);
158 u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
160 if (iowait_time == -1ULL)
171 static unsigned int powersave_bias_target(
struct cpufreq_policy *policy,
172 unsigned int freq_next,
173 unsigned int relation)
175 unsigned int freq_req, freq_reduc, freq_avg;
176 unsigned int freq_hi, freq_lo;
177 unsigned int index = 0;
178 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
191 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
192 freq_avg = freq_req - freq_reduc;
205 if (freq_hi == freq_lo) {
211 jiffies_hi = (freq_avg -
freq_lo) * jiffies_total;
212 jiffies_hi += ((freq_hi -
freq_lo) / 2);
213 jiffies_hi /= (freq_hi -
freq_lo);
214 jiffies_lo = jiffies_total - jiffies_hi;
221 static void ondemand_powersave_bias_init_cpu(
int cpu)
228 static void ondemand_powersave_bias_init(
void)
232 ondemand_powersave_bias_init_cpu(i);
241 return sprintf(buf,
"%u\n", min_sampling_rate);
247 #define show_one(file_name, object) \
248 static ssize_t show_##file_name \
249 (struct kobject *kobj, struct attribute *attr, char *buf) \
251 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
253 show_one(sampling_rate, sampling_rate);
255 show_one(up_threshold, up_threshold);
256 show_one(sampling_down_factor, sampling_down_factor);
257 show_one(ignore_nice_load, ignore_nice);
258 show_one(powersave_bias, powersave_bias);
273 static void update_sampling_rate(
unsigned int new_rate)
277 dbs_tuners_ins.sampling_rate = new_rate
278 =
max(new_rate, min_sampling_rate);
283 unsigned long next_sampling, appointed_at;
288 dbs_info = &
per_cpu(od_cpu_dbs_info, policy->
cpu);
299 appointed_at = dbs_info->
work.timer.expires;
321 ret =
sscanf(buf,
"%u", &input);
324 update_sampling_rate(input);
329 const char *buf,
size_t count)
334 ret =
sscanf(buf,
"%u", &input);
337 dbs_tuners_ins.io_is_busy = !!
input;
342 const char *buf,
size_t count)
346 ret =
sscanf(buf,
"%u", &input);
352 dbs_tuners_ins.up_threshold =
input;
357 struct attribute *b,
const char *buf,
size_t count)
361 ret =
sscanf(buf,
"%u", &input);
365 dbs_tuners_ins.sampling_down_factor =
input;
370 dbs_info = &
per_cpu(od_cpu_dbs_info, j);
377 const char *buf,
size_t count)
384 ret =
sscanf(buf,
"%u", &input);
391 if (input == dbs_tuners_ins.ignore_nice) {
394 dbs_tuners_ins.ignore_nice =
input;
399 dbs_info = &
per_cpu(od_cpu_dbs_info, j);
402 if (dbs_tuners_ins.ignore_nice)
410 const char *buf,
size_t count)
414 ret =
sscanf(buf,
"%u", &input);
422 dbs_tuners_ins.powersave_bias =
input;
423 ondemand_powersave_bias_init();
434 static struct attribute *dbs_attributes[] = {
435 &sampling_rate_min.attr,
438 &sampling_down_factor.attr,
439 &ignore_nice_load.attr,
440 &powersave_bias.attr,
446 .attrs = dbs_attributes,
454 if (dbs_tuners_ins.powersave_bias)
456 else if (p->
cur == p->
max)
465 unsigned int max_load_freq;
490 cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
491 unsigned int idle_time, wall_time, iowait_time;
492 unsigned int load, load_freq;
495 j_dbs_info = &
per_cpu(od_cpu_dbs_info, j);
497 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
498 cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
500 wall_time = (
unsigned int)
504 idle_time = (
unsigned int)
508 iowait_time = (
unsigned int)
512 if (dbs_tuners_ins.ignore_nice) {
514 unsigned long cur_nice_jiffies;
522 cur_nice_jiffies = (
unsigned long)
536 if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
537 idle_time -= iowait_time;
539 if (
unlikely(!wall_time || wall_time < idle_time))
542 load = 100 * (wall_time - idle_time) / wall_time;
546 freq_avg = policy->
cur;
548 load_freq = load * freq_avg;
549 if (load_freq > max_load_freq)
550 max_load_freq = load_freq;
554 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->
cur) {
556 if (policy->
cur < policy->
max)
558 dbs_tuners_ins.sampling_down_factor;
559 dbs_freq_increase(policy, policy->
max);
565 if (policy->
cur == policy->
min)
574 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
576 unsigned int freq_next;
577 freq_next = max_load_freq /
578 (dbs_tuners_ins.up_threshold -
579 dbs_tuners_ins.down_differential);
584 if (freq_next < policy->
min)
585 freq_next = policy->
min;
587 if (!dbs_tuners_ins.powersave_bias) {
591 int freq = powersave_bias_target(policy, freq_next,
603 unsigned int cpu = dbs_info->
cpu;
612 if (!dbs_tuners_ins.powersave_bias ||
614 dbs_check_cpu(dbs_info);
638 static inline void dbs_timer_init(
struct cpu_dbs_info_s *dbs_info)
651 static inline void dbs_timer_exit(
struct cpu_dbs_info_s *dbs_info)
665 static int should_io_be_busy(
void)
667 #if defined(CONFIG_X86)
682 unsigned int cpu = policy->
cpu;
687 this_dbs_info = &
per_cpu(od_cpu_dbs_info, cpu);
699 j_dbs_info = &
per_cpu(od_cpu_dbs_info, j);
704 if (dbs_tuners_ins.ignore_nice)
710 ondemand_powersave_bias_init_cpu(cpu);
715 if (dbs_enable == 1) {
726 latency = policy->
cpuinfo.transition_latency / 1000;
730 min_sampling_rate =
max(min_sampling_rate,
732 dbs_tuners_ins.sampling_rate =
733 max(min_sampling_rate,
735 dbs_tuners_ins.io_is_busy = should_io_be_busy();
740 dbs_timer_init(this_dbs_info);
744 dbs_timer_exit(this_dbs_info);
764 dbs_check_cpu(this_dbs_info);
771 static int __init cpufreq_gov_dbs_init(
void)
776 idle_time = get_cpu_idle_time_us(cpu,
NULL);
778 if (idle_time != -1ULL) {
781 dbs_tuners_ins.down_differential =
798 static void __exit cpufreq_gov_dbs_exit(
void)
807 "Low Latency Frequency Transition capable processors");
810 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND