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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/cpufreq.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/kobject.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/percpu-defs.h>
23 #include <linux/slab.h>
24 #include <linux/sysfs.h>
25 #include <linux/tick.h>
26 #include <linux/types.h>
27 #include <linux/cpu.h>
29 #include "cpufreq_governor.h"
31 /* On-demand governor macros */
32 #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
33 #define DEF_FREQUENCY_UP_THRESHOLD (80)
34 #define DEF_SAMPLING_DOWN_FACTOR (1)
35 #define MAX_SAMPLING_DOWN_FACTOR (100000)
36 #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
37 #define MICRO_FREQUENCY_UP_THRESHOLD (95)
38 #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
39 #define MIN_FREQUENCY_UP_THRESHOLD (11)
40 #define MAX_FREQUENCY_UP_THRESHOLD (100)
42 static DEFINE_PER_CPU(struct od_cpu_dbs_info_s
, od_cpu_dbs_info
);
44 static struct od_ops od_ops
;
46 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
47 static struct cpufreq_governor cpufreq_gov_ondemand
;
50 static unsigned int default_powersave_bias
;
52 static void ondemand_powersave_bias_init_cpu(int cpu
)
54 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
, cpu
);
56 dbs_info
->freq_table
= cpufreq_frequency_get_table(cpu
);
57 dbs_info
->freq_lo
= 0;
61 * Not all CPUs want IO time to be accounted as busy; this depends on how
62 * efficient idling at a higher frequency/voltage is.
63 * Pavel Machek says this is not so for various generations of AMD and old
65 * Mike Chan (android.com) claims this is also not true for ARM.
66 * Because of this, whitelist specific known (series) of CPUs by default, and
67 * leave all others up to the user.
69 static int should_io_be_busy(void)
71 #if defined(CONFIG_X86)
73 * For Intel, Core 2 (model 15) and later have an efficient idle.
75 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
&&
76 boot_cpu_data
.x86
== 6 &&
77 boot_cpu_data
.x86_model
>= 15)
84 * Find right freq to be set now with powersave_bias on.
85 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
86 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
88 static unsigned int generic_powersave_bias_target(struct cpufreq_policy
*policy
,
89 unsigned int freq_next
, unsigned int relation
)
91 unsigned int freq_req
, freq_reduc
, freq_avg
;
92 unsigned int freq_hi
, freq_lo
;
93 unsigned int index
= 0;
94 unsigned int jiffies_total
, jiffies_hi
, jiffies_lo
;
95 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
,
97 struct dbs_data
*dbs_data
= policy
->governor_data
;
98 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
100 if (!dbs_info
->freq_table
) {
101 dbs_info
->freq_lo
= 0;
102 dbs_info
->freq_lo_jiffies
= 0;
106 cpufreq_frequency_table_target(policy
, dbs_info
->freq_table
, freq_next
,
108 freq_req
= dbs_info
->freq_table
[index
].frequency
;
109 freq_reduc
= freq_req
* od_tuners
->powersave_bias
/ 1000;
110 freq_avg
= freq_req
- freq_reduc
;
112 /* Find freq bounds for freq_avg in freq_table */
114 cpufreq_frequency_table_target(policy
, dbs_info
->freq_table
, freq_avg
,
115 CPUFREQ_RELATION_H
, &index
);
116 freq_lo
= dbs_info
->freq_table
[index
].frequency
;
118 cpufreq_frequency_table_target(policy
, dbs_info
->freq_table
, freq_avg
,
119 CPUFREQ_RELATION_L
, &index
);
120 freq_hi
= dbs_info
->freq_table
[index
].frequency
;
122 /* Find out how long we have to be in hi and lo freqs */
123 if (freq_hi
== freq_lo
) {
124 dbs_info
->freq_lo
= 0;
125 dbs_info
->freq_lo_jiffies
= 0;
128 jiffies_total
= usecs_to_jiffies(od_tuners
->sampling_rate
);
129 jiffies_hi
= (freq_avg
- freq_lo
) * jiffies_total
;
130 jiffies_hi
+= ((freq_hi
- freq_lo
) / 2);
131 jiffies_hi
/= (freq_hi
- freq_lo
);
132 jiffies_lo
= jiffies_total
- jiffies_hi
;
133 dbs_info
->freq_lo
= freq_lo
;
134 dbs_info
->freq_lo_jiffies
= jiffies_lo
;
135 dbs_info
->freq_hi_jiffies
= jiffies_hi
;
139 static void ondemand_powersave_bias_init(void)
142 for_each_online_cpu(i
) {
143 ondemand_powersave_bias_init_cpu(i
);
147 static void dbs_freq_increase(struct cpufreq_policy
*p
, unsigned int freq
)
149 struct dbs_data
*dbs_data
= p
->governor_data
;
150 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
152 if (od_tuners
->powersave_bias
)
153 freq
= od_ops
.powersave_bias_target(p
, freq
,
155 else if (p
->cur
== p
->max
)
158 __cpufreq_driver_target(p
, freq
, od_tuners
->powersave_bias
?
159 CPUFREQ_RELATION_L
: CPUFREQ_RELATION_H
);
163 * Every sampling_rate, we check, if current idle time is less than 20%
164 * (default), then we try to increase frequency. Every sampling_rate, we look
165 * for the lowest frequency which can sustain the load while keeping idle time
166 * over 30%. If such a frequency exist, we try to decrease to this frequency.
168 * Any frequency increase takes it to the maximum frequency. Frequency reduction
169 * happens at minimum steps of 5% (default) of current frequency
171 static void od_check_cpu(int cpu
, unsigned int load_freq
)
173 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
, cpu
);
174 struct cpufreq_policy
*policy
= dbs_info
->cdbs
.cur_policy
;
175 struct dbs_data
*dbs_data
= policy
->governor_data
;
176 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
178 dbs_info
->freq_lo
= 0;
180 /* Check for frequency increase */
181 if (load_freq
> od_tuners
->up_threshold
* policy
->cur
) {
182 /* If switching to max speed, apply sampling_down_factor */
183 if (policy
->cur
< policy
->max
)
184 dbs_info
->rate_mult
=
185 od_tuners
->sampling_down_factor
;
186 dbs_freq_increase(policy
, policy
->max
);
190 /* Check for frequency decrease */
191 /* if we cannot reduce the frequency anymore, break out early */
192 if (policy
->cur
== policy
->min
)
196 * The optimal frequency is the frequency that is the lowest that can
197 * support the current CPU usage without triggering the up policy. To be
198 * safe, we focus 10 points under the threshold.
200 if (load_freq
< od_tuners
->adj_up_threshold
202 unsigned int freq_next
;
203 freq_next
= load_freq
/ od_tuners
->adj_up_threshold
;
205 /* No longer fully busy, reset rate_mult */
206 dbs_info
->rate_mult
= 1;
208 if (freq_next
< policy
->min
)
209 freq_next
= policy
->min
;
211 if (!od_tuners
->powersave_bias
) {
212 __cpufreq_driver_target(policy
, freq_next
,
217 freq_next
= od_ops
.powersave_bias_target(policy
, freq_next
,
219 __cpufreq_driver_target(policy
, freq_next
, CPUFREQ_RELATION_L
);
223 static void od_dbs_timer(struct work_struct
*work
)
225 struct od_cpu_dbs_info_s
*dbs_info
=
226 container_of(work
, struct od_cpu_dbs_info_s
, cdbs
.work
.work
);
227 unsigned int cpu
= dbs_info
->cdbs
.cur_policy
->cpu
;
228 struct od_cpu_dbs_info_s
*core_dbs_info
= &per_cpu(od_cpu_dbs_info
,
230 struct dbs_data
*dbs_data
= dbs_info
->cdbs
.cur_policy
->governor_data
;
231 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
232 int delay
= 0, sample_type
= core_dbs_info
->sample_type
;
233 bool modify_all
= true;
235 mutex_lock(&core_dbs_info
->cdbs
.timer_mutex
);
236 if (!need_load_eval(&core_dbs_info
->cdbs
, od_tuners
->sampling_rate
)) {
241 /* Common NORMAL_SAMPLE setup */
242 core_dbs_info
->sample_type
= OD_NORMAL_SAMPLE
;
243 if (sample_type
== OD_SUB_SAMPLE
) {
244 delay
= core_dbs_info
->freq_lo_jiffies
;
245 __cpufreq_driver_target(core_dbs_info
->cdbs
.cur_policy
,
246 core_dbs_info
->freq_lo
, CPUFREQ_RELATION_H
);
248 dbs_check_cpu(dbs_data
, cpu
);
249 if (core_dbs_info
->freq_lo
) {
250 /* Setup timer for SUB_SAMPLE */
251 core_dbs_info
->sample_type
= OD_SUB_SAMPLE
;
252 delay
= core_dbs_info
->freq_hi_jiffies
;
258 delay
= delay_for_sampling_rate(od_tuners
->sampling_rate
259 * core_dbs_info
->rate_mult
);
261 gov_queue_work(dbs_data
, dbs_info
->cdbs
.cur_policy
, delay
, modify_all
);
262 mutex_unlock(&core_dbs_info
->cdbs
.timer_mutex
);
265 /************************** sysfs interface ************************/
266 static struct common_dbs_data od_dbs_cdata
;
269 * update_sampling_rate - update sampling rate effective immediately if needed.
270 * @new_rate: new sampling rate
272 * If new rate is smaller than the old, simply updating
273 * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
274 * original sampling_rate was 1 second and the requested new sampling rate is 10
275 * ms because the user needs immediate reaction from ondemand governor, but not
276 * sure if higher frequency will be required or not, then, the governor may
277 * change the sampling rate too late; up to 1 second later. Thus, if we are
278 * reducing the sampling rate, we need to make the new value effective
281 static void update_sampling_rate(struct dbs_data
*dbs_data
,
282 unsigned int new_rate
)
284 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
287 od_tuners
->sampling_rate
= new_rate
= max(new_rate
,
288 dbs_data
->min_sampling_rate
);
290 for_each_online_cpu(cpu
) {
291 struct cpufreq_policy
*policy
;
292 struct od_cpu_dbs_info_s
*dbs_info
;
293 unsigned long next_sampling
, appointed_at
;
295 policy
= cpufreq_cpu_get(cpu
);
298 if (policy
->governor
!= &cpufreq_gov_ondemand
) {
299 cpufreq_cpu_put(policy
);
302 dbs_info
= &per_cpu(od_cpu_dbs_info
, cpu
);
303 cpufreq_cpu_put(policy
);
305 mutex_lock(&dbs_info
->cdbs
.timer_mutex
);
307 if (!delayed_work_pending(&dbs_info
->cdbs
.work
)) {
308 mutex_unlock(&dbs_info
->cdbs
.timer_mutex
);
312 next_sampling
= jiffies
+ usecs_to_jiffies(new_rate
);
313 appointed_at
= dbs_info
->cdbs
.work
.timer
.expires
;
315 if (time_before(next_sampling
, appointed_at
)) {
317 mutex_unlock(&dbs_info
->cdbs
.timer_mutex
);
318 cancel_delayed_work_sync(&dbs_info
->cdbs
.work
);
319 mutex_lock(&dbs_info
->cdbs
.timer_mutex
);
321 gov_queue_work(dbs_data
, dbs_info
->cdbs
.cur_policy
,
322 usecs_to_jiffies(new_rate
), true);
325 mutex_unlock(&dbs_info
->cdbs
.timer_mutex
);
329 static ssize_t
store_sampling_rate(struct dbs_data
*dbs_data
, const char *buf
,
334 ret
= sscanf(buf
, "%u", &input
);
338 update_sampling_rate(dbs_data
, input
);
342 static ssize_t
store_io_is_busy(struct dbs_data
*dbs_data
, const char *buf
,
345 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
350 ret
= sscanf(buf
, "%u", &input
);
353 od_tuners
->io_is_busy
= !!input
;
355 /* we need to re-evaluate prev_cpu_idle */
356 for_each_online_cpu(j
) {
357 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
,
359 dbs_info
->cdbs
.prev_cpu_idle
= get_cpu_idle_time(j
,
360 &dbs_info
->cdbs
.prev_cpu_wall
, od_tuners
->io_is_busy
);
365 static ssize_t
store_up_threshold(struct dbs_data
*dbs_data
, const char *buf
,
368 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
371 ret
= sscanf(buf
, "%u", &input
);
373 if (ret
!= 1 || input
> MAX_FREQUENCY_UP_THRESHOLD
||
374 input
< MIN_FREQUENCY_UP_THRESHOLD
) {
377 /* Calculate the new adj_up_threshold */
378 od_tuners
->adj_up_threshold
+= input
;
379 od_tuners
->adj_up_threshold
-= od_tuners
->up_threshold
;
381 od_tuners
->up_threshold
= input
;
385 static ssize_t
store_sampling_down_factor(struct dbs_data
*dbs_data
,
386 const char *buf
, size_t count
)
388 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
389 unsigned int input
, j
;
391 ret
= sscanf(buf
, "%u", &input
);
393 if (ret
!= 1 || input
> MAX_SAMPLING_DOWN_FACTOR
|| input
< 1)
395 od_tuners
->sampling_down_factor
= input
;
397 /* Reset down sampling multiplier in case it was active */
398 for_each_online_cpu(j
) {
399 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
,
401 dbs_info
->rate_mult
= 1;
406 static ssize_t
store_ignore_nice_load(struct dbs_data
*dbs_data
,
407 const char *buf
, size_t count
)
409 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
415 ret
= sscanf(buf
, "%u", &input
);
422 if (input
== od_tuners
->ignore_nice_load
) { /* nothing to do */
425 od_tuners
->ignore_nice_load
= input
;
427 /* we need to re-evaluate prev_cpu_idle */
428 for_each_online_cpu(j
) {
429 struct od_cpu_dbs_info_s
*dbs_info
;
430 dbs_info
= &per_cpu(od_cpu_dbs_info
, j
);
431 dbs_info
->cdbs
.prev_cpu_idle
= get_cpu_idle_time(j
,
432 &dbs_info
->cdbs
.prev_cpu_wall
, od_tuners
->io_is_busy
);
433 if (od_tuners
->ignore_nice_load
)
434 dbs_info
->cdbs
.prev_cpu_nice
=
435 kcpustat_cpu(j
).cpustat
[CPUTIME_NICE
];
441 static ssize_t
store_powersave_bias(struct dbs_data
*dbs_data
, const char *buf
,
444 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
447 ret
= sscanf(buf
, "%u", &input
);
455 od_tuners
->powersave_bias
= input
;
456 ondemand_powersave_bias_init();
460 show_store_one(od
, sampling_rate
);
461 show_store_one(od
, io_is_busy
);
462 show_store_one(od
, up_threshold
);
463 show_store_one(od
, sampling_down_factor
);
464 show_store_one(od
, ignore_nice_load
);
465 show_store_one(od
, powersave_bias
);
466 declare_show_sampling_rate_min(od
);
468 gov_sys_pol_attr_rw(sampling_rate
);
469 gov_sys_pol_attr_rw(io_is_busy
);
470 gov_sys_pol_attr_rw(up_threshold
);
471 gov_sys_pol_attr_rw(sampling_down_factor
);
472 gov_sys_pol_attr_rw(ignore_nice_load
);
473 gov_sys_pol_attr_rw(powersave_bias
);
474 gov_sys_pol_attr_ro(sampling_rate_min
);
476 static struct attribute
*dbs_attributes_gov_sys
[] = {
477 &sampling_rate_min_gov_sys
.attr
,
478 &sampling_rate_gov_sys
.attr
,
479 &up_threshold_gov_sys
.attr
,
480 &sampling_down_factor_gov_sys
.attr
,
481 &ignore_nice_load_gov_sys
.attr
,
482 &powersave_bias_gov_sys
.attr
,
483 &io_is_busy_gov_sys
.attr
,
487 static struct attribute_group od_attr_group_gov_sys
= {
488 .attrs
= dbs_attributes_gov_sys
,
492 static struct attribute
*dbs_attributes_gov_pol
[] = {
493 &sampling_rate_min_gov_pol
.attr
,
494 &sampling_rate_gov_pol
.attr
,
495 &up_threshold_gov_pol
.attr
,
496 &sampling_down_factor_gov_pol
.attr
,
497 &ignore_nice_load_gov_pol
.attr
,
498 &powersave_bias_gov_pol
.attr
,
499 &io_is_busy_gov_pol
.attr
,
503 static struct attribute_group od_attr_group_gov_pol
= {
504 .attrs
= dbs_attributes_gov_pol
,
508 /************************** sysfs end ************************/
510 static int od_init(struct dbs_data
*dbs_data
)
512 struct od_dbs_tuners
*tuners
;
516 tuners
= kzalloc(sizeof(struct od_dbs_tuners
), GFP_KERNEL
);
518 pr_err("%s: kzalloc failed\n", __func__
);
523 idle_time
= get_cpu_idle_time_us(cpu
, NULL
);
525 if (idle_time
!= -1ULL) {
526 /* Idle micro accounting is supported. Use finer thresholds */
527 tuners
->up_threshold
= MICRO_FREQUENCY_UP_THRESHOLD
;
528 tuners
->adj_up_threshold
= MICRO_FREQUENCY_UP_THRESHOLD
-
529 MICRO_FREQUENCY_DOWN_DIFFERENTIAL
;
531 * In nohz/micro accounting case we set the minimum frequency
532 * not depending on HZ, but fixed (very low). The deferred
533 * timer might skip some samples if idle/sleeping as needed.
535 dbs_data
->min_sampling_rate
= MICRO_FREQUENCY_MIN_SAMPLE_RATE
;
537 tuners
->up_threshold
= DEF_FREQUENCY_UP_THRESHOLD
;
538 tuners
->adj_up_threshold
= DEF_FREQUENCY_UP_THRESHOLD
-
539 DEF_FREQUENCY_DOWN_DIFFERENTIAL
;
541 /* For correct statistics, we need 10 ticks for each measure */
542 dbs_data
->min_sampling_rate
= MIN_SAMPLING_RATE_RATIO
*
543 jiffies_to_usecs(10);
546 tuners
->sampling_down_factor
= DEF_SAMPLING_DOWN_FACTOR
;
547 tuners
->ignore_nice_load
= 0;
548 tuners
->powersave_bias
= default_powersave_bias
;
549 tuners
->io_is_busy
= should_io_be_busy();
551 dbs_data
->tuners
= tuners
;
552 mutex_init(&dbs_data
->mutex
);
556 static void od_exit(struct dbs_data
*dbs_data
)
558 kfree(dbs_data
->tuners
);
561 define_get_cpu_dbs_routines(od_cpu_dbs_info
);
563 static struct od_ops od_ops
= {
564 .powersave_bias_init_cpu
= ondemand_powersave_bias_init_cpu
,
565 .powersave_bias_target
= generic_powersave_bias_target
,
566 .freq_increase
= dbs_freq_increase
,
569 static struct common_dbs_data od_dbs_cdata
= {
570 .governor
= GOV_ONDEMAND
,
571 .attr_group_gov_sys
= &od_attr_group_gov_sys
,
572 .attr_group_gov_pol
= &od_attr_group_gov_pol
,
573 .get_cpu_cdbs
= get_cpu_cdbs
,
574 .get_cpu_dbs_info_s
= get_cpu_dbs_info_s
,
575 .gov_dbs_timer
= od_dbs_timer
,
576 .gov_check_cpu
= od_check_cpu
,
582 static void od_set_powersave_bias(unsigned int powersave_bias
)
584 struct cpufreq_policy
*policy
;
585 struct dbs_data
*dbs_data
;
586 struct od_dbs_tuners
*od_tuners
;
590 default_powersave_bias
= powersave_bias
;
591 cpumask_clear(&done
);
594 for_each_online_cpu(cpu
) {
595 if (cpumask_test_cpu(cpu
, &done
))
598 policy
= per_cpu(od_cpu_dbs_info
, cpu
).cdbs
.cur_policy
;
602 cpumask_or(&done
, &done
, policy
->cpus
);
604 if (policy
->governor
!= &cpufreq_gov_ondemand
)
607 dbs_data
= policy
->governor_data
;
608 od_tuners
= dbs_data
->tuners
;
609 od_tuners
->powersave_bias
= default_powersave_bias
;
614 void od_register_powersave_bias_handler(unsigned int (*f
)
615 (struct cpufreq_policy
*, unsigned int, unsigned int),
616 unsigned int powersave_bias
)
618 od_ops
.powersave_bias_target
= f
;
619 od_set_powersave_bias(powersave_bias
);
621 EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler
);
623 void od_unregister_powersave_bias_handler(void)
625 od_ops
.powersave_bias_target
= generic_powersave_bias_target
;
626 od_set_powersave_bias(0);
628 EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler
);
630 static int od_cpufreq_governor_dbs(struct cpufreq_policy
*policy
,
633 return cpufreq_governor_dbs(policy
, &od_dbs_cdata
, event
);
636 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
639 struct cpufreq_governor cpufreq_gov_ondemand
= {
641 .governor
= od_cpufreq_governor_dbs
,
642 .max_transition_latency
= TRANSITION_LATENCY_LIMIT
,
643 .owner
= THIS_MODULE
,
646 static int __init
cpufreq_gov_dbs_init(void)
648 return cpufreq_register_governor(&cpufreq_gov_ondemand
);
651 static void __exit
cpufreq_gov_dbs_exit(void)
653 cpufreq_unregister_governor(&cpufreq_gov_ondemand
);
656 MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
657 MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
658 MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
659 "Low Latency Frequency Transition capable processors");
660 MODULE_LICENSE("GPL");
662 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
663 fs_initcall(cpufreq_gov_dbs_init
);
665 module_init(cpufreq_gov_dbs_init
);
667 module_exit(cpufreq_gov_dbs_exit
);