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/cpu.h>
16 #include <linux/percpu-defs.h>
17 #include <linux/slab.h>
18 #include <linux/tick.h>
19 #include "cpufreq_governor.h"
21 /* On-demand governor macros */
22 #define DEF_FREQUENCY_UP_THRESHOLD (80)
23 #define DEF_SAMPLING_DOWN_FACTOR (1)
24 #define MAX_SAMPLING_DOWN_FACTOR (100000)
25 #define MICRO_FREQUENCY_UP_THRESHOLD (95)
26 #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
27 #define MIN_FREQUENCY_UP_THRESHOLD (11)
28 #define MAX_FREQUENCY_UP_THRESHOLD (100)
30 static DEFINE_PER_CPU(struct od_cpu_dbs_info_s
, od_cpu_dbs_info
);
32 static struct od_ops od_ops
;
34 static unsigned int default_powersave_bias
;
36 static void ondemand_powersave_bias_init_cpu(int cpu
)
38 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
, cpu
);
40 dbs_info
->freq_table
= cpufreq_frequency_get_table(cpu
);
41 dbs_info
->freq_lo
= 0;
45 * Not all CPUs want IO time to be accounted as busy; this depends on how
46 * efficient idling at a higher frequency/voltage is.
47 * Pavel Machek says this is not so for various generations of AMD and old
49 * Mike Chan (android.com) claims this is also not true for ARM.
50 * Because of this, whitelist specific known (series) of CPUs by default, and
51 * leave all others up to the user.
53 static int should_io_be_busy(void)
55 #if defined(CONFIG_X86)
57 * For Intel, Core 2 (model 15) and later have an efficient idle.
59 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
&&
60 boot_cpu_data
.x86
== 6 &&
61 boot_cpu_data
.x86_model
>= 15)
68 * Find right freq to be set now with powersave_bias on.
69 * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
70 * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
72 static unsigned int generic_powersave_bias_target(struct cpufreq_policy
*policy
,
73 unsigned int freq_next
, unsigned int relation
)
75 unsigned int freq_req
, freq_reduc
, freq_avg
;
76 unsigned int freq_hi
, freq_lo
;
77 unsigned int index
= 0;
78 unsigned int delay_hi_us
;
79 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
,
81 struct policy_dbs_info
*policy_dbs
= policy
->governor_data
;
82 struct dbs_data
*dbs_data
= policy_dbs
->dbs_data
;
83 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
85 if (!dbs_info
->freq_table
) {
86 dbs_info
->freq_lo
= 0;
87 dbs_info
->freq_lo_delay_us
= 0;
91 cpufreq_frequency_table_target(policy
, dbs_info
->freq_table
, freq_next
,
93 freq_req
= dbs_info
->freq_table
[index
].frequency
;
94 freq_reduc
= freq_req
* od_tuners
->powersave_bias
/ 1000;
95 freq_avg
= freq_req
- freq_reduc
;
97 /* Find freq bounds for freq_avg in freq_table */
99 cpufreq_frequency_table_target(policy
, dbs_info
->freq_table
, freq_avg
,
100 CPUFREQ_RELATION_H
, &index
);
101 freq_lo
= dbs_info
->freq_table
[index
].frequency
;
103 cpufreq_frequency_table_target(policy
, dbs_info
->freq_table
, freq_avg
,
104 CPUFREQ_RELATION_L
, &index
);
105 freq_hi
= dbs_info
->freq_table
[index
].frequency
;
107 /* Find out how long we have to be in hi and lo freqs */
108 if (freq_hi
== freq_lo
) {
109 dbs_info
->freq_lo
= 0;
110 dbs_info
->freq_lo_delay_us
= 0;
113 delay_hi_us
= (freq_avg
- freq_lo
) * dbs_data
->sampling_rate
;
114 delay_hi_us
+= (freq_hi
- freq_lo
) / 2;
115 delay_hi_us
/= freq_hi
- freq_lo
;
116 dbs_info
->freq_hi_delay_us
= delay_hi_us
;
117 dbs_info
->freq_lo
= freq_lo
;
118 dbs_info
->freq_lo_delay_us
= dbs_data
->sampling_rate
- delay_hi_us
;
122 static void ondemand_powersave_bias_init(void)
125 for_each_online_cpu(i
) {
126 ondemand_powersave_bias_init_cpu(i
);
130 static void dbs_freq_increase(struct cpufreq_policy
*policy
, unsigned int freq
)
132 struct policy_dbs_info
*policy_dbs
= policy
->governor_data
;
133 struct dbs_data
*dbs_data
= policy_dbs
->dbs_data
;
134 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
136 if (od_tuners
->powersave_bias
)
137 freq
= od_ops
.powersave_bias_target(policy
, freq
,
139 else if (policy
->cur
== policy
->max
)
142 __cpufreq_driver_target(policy
, freq
, od_tuners
->powersave_bias
?
143 CPUFREQ_RELATION_L
: CPUFREQ_RELATION_H
);
147 * Every sampling_rate, we check, if current idle time is less than 20%
148 * (default), then we try to increase frequency. Else, we adjust the frequency
149 * proportional to load.
151 static void od_update(struct cpufreq_policy
*policy
)
153 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
, policy
->cpu
);
154 struct policy_dbs_info
*policy_dbs
= dbs_info
->cdbs
.policy_dbs
;
155 struct dbs_data
*dbs_data
= policy_dbs
->dbs_data
;
156 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
157 unsigned int load
= dbs_update(policy
);
159 dbs_info
->freq_lo
= 0;
161 /* Check for frequency increase */
162 if (load
> dbs_data
->up_threshold
) {
163 /* If switching to max speed, apply sampling_down_factor */
164 if (policy
->cur
< policy
->max
)
165 policy_dbs
->rate_mult
= dbs_data
->sampling_down_factor
;
166 dbs_freq_increase(policy
, policy
->max
);
168 /* Calculate the next frequency proportional to load */
169 unsigned int freq_next
, min_f
, max_f
;
171 min_f
= policy
->cpuinfo
.min_freq
;
172 max_f
= policy
->cpuinfo
.max_freq
;
173 freq_next
= min_f
+ load
* (max_f
- min_f
) / 100;
175 /* No longer fully busy, reset rate_mult */
176 policy_dbs
->rate_mult
= 1;
178 if (!od_tuners
->powersave_bias
) {
179 __cpufreq_driver_target(policy
, freq_next
,
184 freq_next
= od_ops
.powersave_bias_target(policy
, freq_next
,
186 __cpufreq_driver_target(policy
, freq_next
, CPUFREQ_RELATION_C
);
190 static unsigned int od_dbs_timer(struct cpufreq_policy
*policy
)
192 struct policy_dbs_info
*policy_dbs
= policy
->governor_data
;
193 struct dbs_data
*dbs_data
= policy_dbs
->dbs_data
;
194 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
, policy
->cpu
);
195 int sample_type
= dbs_info
->sample_type
;
197 /* Common NORMAL_SAMPLE setup */
198 dbs_info
->sample_type
= OD_NORMAL_SAMPLE
;
200 * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
203 if (sample_type
== OD_SUB_SAMPLE
&& policy_dbs
->sample_delay_ns
> 0) {
204 __cpufreq_driver_target(policy
, dbs_info
->freq_lo
,
206 return dbs_info
->freq_lo_delay_us
;
211 if (dbs_info
->freq_lo
) {
212 /* Setup timer for SUB_SAMPLE */
213 dbs_info
->sample_type
= OD_SUB_SAMPLE
;
214 return dbs_info
->freq_hi_delay_us
;
217 return dbs_data
->sampling_rate
* policy_dbs
->rate_mult
;
220 /************************** sysfs interface ************************/
221 static struct dbs_governor od_dbs_gov
;
223 static ssize_t
store_io_is_busy(struct dbs_data
*dbs_data
, const char *buf
,
226 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
231 ret
= sscanf(buf
, "%u", &input
);
234 od_tuners
->io_is_busy
= !!input
;
236 /* we need to re-evaluate prev_cpu_idle */
237 for_each_online_cpu(j
) {
238 struct od_cpu_dbs_info_s
*dbs_info
= &per_cpu(od_cpu_dbs_info
,
240 dbs_info
->cdbs
.prev_cpu_idle
= get_cpu_idle_time(j
,
241 &dbs_info
->cdbs
.prev_cpu_wall
, od_tuners
->io_is_busy
);
246 static ssize_t
store_up_threshold(struct dbs_data
*dbs_data
, const char *buf
,
251 ret
= sscanf(buf
, "%u", &input
);
253 if (ret
!= 1 || input
> MAX_FREQUENCY_UP_THRESHOLD
||
254 input
< MIN_FREQUENCY_UP_THRESHOLD
) {
258 dbs_data
->up_threshold
= input
;
262 static ssize_t
store_sampling_down_factor(struct dbs_data
*dbs_data
,
263 const char *buf
, size_t count
)
265 struct policy_dbs_info
*policy_dbs
;
268 ret
= sscanf(buf
, "%u", &input
);
270 if (ret
!= 1 || input
> MAX_SAMPLING_DOWN_FACTOR
|| input
< 1)
273 dbs_data
->sampling_down_factor
= input
;
275 /* Reset down sampling multiplier in case it was active */
276 list_for_each_entry(policy_dbs
, &dbs_data
->policy_dbs_list
, list
) {
278 * Doing this without locking might lead to using different
279 * rate_mult values in od_update() and od_dbs_timer().
281 mutex_lock(&policy_dbs
->timer_mutex
);
282 policy_dbs
->rate_mult
= 1;
283 mutex_unlock(&policy_dbs
->timer_mutex
);
289 static ssize_t
store_ignore_nice_load(struct dbs_data
*dbs_data
,
290 const char *buf
, size_t count
)
292 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
298 ret
= sscanf(buf
, "%u", &input
);
305 if (input
== dbs_data
->ignore_nice_load
) { /* nothing to do */
308 dbs_data
->ignore_nice_load
= input
;
310 /* we need to re-evaluate prev_cpu_idle */
311 for_each_online_cpu(j
) {
312 struct od_cpu_dbs_info_s
*dbs_info
;
313 dbs_info
= &per_cpu(od_cpu_dbs_info
, j
);
314 dbs_info
->cdbs
.prev_cpu_idle
= get_cpu_idle_time(j
,
315 &dbs_info
->cdbs
.prev_cpu_wall
, od_tuners
->io_is_busy
);
316 if (dbs_data
->ignore_nice_load
)
317 dbs_info
->cdbs
.prev_cpu_nice
=
318 kcpustat_cpu(j
).cpustat
[CPUTIME_NICE
];
324 static ssize_t
store_powersave_bias(struct dbs_data
*dbs_data
, const char *buf
,
327 struct od_dbs_tuners
*od_tuners
= dbs_data
->tuners
;
330 ret
= sscanf(buf
, "%u", &input
);
338 od_tuners
->powersave_bias
= input
;
339 ondemand_powersave_bias_init();
343 gov_show_one_common(sampling_rate
);
344 gov_show_one_common(up_threshold
);
345 gov_show_one_common(sampling_down_factor
);
346 gov_show_one_common(ignore_nice_load
);
347 gov_show_one_common(min_sampling_rate
);
348 gov_show_one(od
, io_is_busy
);
349 gov_show_one(od
, powersave_bias
);
351 gov_attr_rw(sampling_rate
);
352 gov_attr_rw(io_is_busy
);
353 gov_attr_rw(up_threshold
);
354 gov_attr_rw(sampling_down_factor
);
355 gov_attr_rw(ignore_nice_load
);
356 gov_attr_rw(powersave_bias
);
357 gov_attr_ro(min_sampling_rate
);
359 static struct attribute
*od_attributes
[] = {
360 &min_sampling_rate
.attr
,
363 &sampling_down_factor
.attr
,
364 &ignore_nice_load
.attr
,
365 &powersave_bias
.attr
,
370 /************************** sysfs end ************************/
372 static int od_init(struct dbs_data
*dbs_data
, bool notify
)
374 struct od_dbs_tuners
*tuners
;
378 tuners
= kzalloc(sizeof(*tuners
), GFP_KERNEL
);
380 pr_err("%s: kzalloc failed\n", __func__
);
385 idle_time
= get_cpu_idle_time_us(cpu
, NULL
);
387 if (idle_time
!= -1ULL) {
388 /* Idle micro accounting is supported. Use finer thresholds */
389 dbs_data
->up_threshold
= MICRO_FREQUENCY_UP_THRESHOLD
;
391 * In nohz/micro accounting case we set the minimum frequency
392 * not depending on HZ, but fixed (very low). The deferred
393 * timer might skip some samples if idle/sleeping as needed.
395 dbs_data
->min_sampling_rate
= MICRO_FREQUENCY_MIN_SAMPLE_RATE
;
397 dbs_data
->up_threshold
= DEF_FREQUENCY_UP_THRESHOLD
;
399 /* For correct statistics, we need 10 ticks for each measure */
400 dbs_data
->min_sampling_rate
= MIN_SAMPLING_RATE_RATIO
*
401 jiffies_to_usecs(10);
404 dbs_data
->sampling_down_factor
= DEF_SAMPLING_DOWN_FACTOR
;
405 dbs_data
->ignore_nice_load
= 0;
406 tuners
->powersave_bias
= default_powersave_bias
;
407 tuners
->io_is_busy
= should_io_be_busy();
409 dbs_data
->tuners
= tuners
;
413 static void od_exit(struct dbs_data
*dbs_data
, bool notify
)
415 kfree(dbs_data
->tuners
);
418 define_get_cpu_dbs_routines(od_cpu_dbs_info
);
420 static struct od_ops od_ops
= {
421 .powersave_bias_init_cpu
= ondemand_powersave_bias_init_cpu
,
422 .powersave_bias_target
= generic_powersave_bias_target
,
423 .freq_increase
= dbs_freq_increase
,
426 static struct dbs_governor od_dbs_gov
= {
429 .governor
= cpufreq_governor_dbs
,
430 .max_transition_latency
= TRANSITION_LATENCY_LIMIT
,
431 .owner
= THIS_MODULE
,
433 .governor
= GOV_ONDEMAND
,
434 .kobj_type
= { .default_attrs
= od_attributes
},
435 .get_cpu_cdbs
= get_cpu_cdbs
,
436 .get_cpu_dbs_info_s
= get_cpu_dbs_info_s
,
437 .gov_dbs_timer
= od_dbs_timer
,
443 #define CPU_FREQ_GOV_ONDEMAND (&od_dbs_gov.gov)
445 static void od_set_powersave_bias(unsigned int powersave_bias
)
447 struct cpufreq_policy
*policy
;
448 struct dbs_data
*dbs_data
;
449 struct od_dbs_tuners
*od_tuners
;
453 default_powersave_bias
= powersave_bias
;
454 cpumask_clear(&done
);
457 for_each_online_cpu(cpu
) {
458 struct policy_dbs_info
*policy_dbs
;
460 if (cpumask_test_cpu(cpu
, &done
))
463 policy_dbs
= per_cpu(od_cpu_dbs_info
, cpu
).cdbs
.policy_dbs
;
467 policy
= policy_dbs
->policy
;
468 cpumask_or(&done
, &done
, policy
->cpus
);
470 if (policy
->governor
!= CPU_FREQ_GOV_ONDEMAND
)
473 dbs_data
= policy_dbs
->dbs_data
;
474 od_tuners
= dbs_data
->tuners
;
475 od_tuners
->powersave_bias
= default_powersave_bias
;
480 void od_register_powersave_bias_handler(unsigned int (*f
)
481 (struct cpufreq_policy
*, unsigned int, unsigned int),
482 unsigned int powersave_bias
)
484 od_ops
.powersave_bias_target
= f
;
485 od_set_powersave_bias(powersave_bias
);
487 EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler
);
489 void od_unregister_powersave_bias_handler(void)
491 od_ops
.powersave_bias_target
= generic_powersave_bias_target
;
492 od_set_powersave_bias(0);
494 EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler
);
496 static int __init
cpufreq_gov_dbs_init(void)
498 return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND
);
501 static void __exit
cpufreq_gov_dbs_exit(void)
503 cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND
);
506 MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
507 MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
508 MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
509 "Low Latency Frequency Transition capable processors");
510 MODULE_LICENSE("GPL");
512 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
513 struct cpufreq_governor
*cpufreq_default_governor(void)
515 return CPU_FREQ_GOV_ONDEMAND
;
518 fs_initcall(cpufreq_gov_dbs_init
);
520 module_init(cpufreq_gov_dbs_init
);
522 module_exit(cpufreq_gov_dbs_exit
);