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 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/cpufreq.h>
17 #include <linux/cpu.h>
18 #include <linux/jiffies.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/mutex.h>
23 * dbs is used in this file as a shortform for demandbased switching
24 * It helps to keep variable names smaller, simpler
27 #define DEF_FREQUENCY_UP_THRESHOLD (80)
28 #define MIN_FREQUENCY_UP_THRESHOLD (11)
29 #define MAX_FREQUENCY_UP_THRESHOLD (100)
32 * The polling frequency of this governor depends on the capability of
33 * the processor. Default polling frequency is 1000 times the transition
34 * latency of the processor. The governor will work on any processor with
35 * transition latency <= 10mS, using appropriate sampling
37 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
38 * this governor will not work.
39 * All times here are in uS.
41 static unsigned int def_sampling_rate
;
42 #define MIN_SAMPLING_RATE_RATIO (2)
43 /* for correct statistics, we need at least 10 ticks between each measure */
44 #define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
45 #define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
46 #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
47 #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
48 #define TRANSITION_LATENCY_LIMIT (10 * 1000)
50 static void do_dbs_timer(void *data
);
52 struct cpu_dbs_info_s
{
53 cputime64_t prev_cpu_idle
;
54 cputime64_t prev_cpu_wall
;
55 struct cpufreq_policy
*cur_policy
;
56 struct work_struct work
;
58 struct cpufreq_frequency_table
*freq_table
;
60 unsigned int freq_lo_jiffies
;
61 unsigned int freq_hi_jiffies
;
63 static DEFINE_PER_CPU(struct cpu_dbs_info_s
, cpu_dbs_info
);
65 static unsigned int dbs_enable
; /* number of CPUs using this policy */
68 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
69 * lock and dbs_mutex. cpu_hotplug lock should always be held before
70 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
71 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
72 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
73 * is recursive for the same process. -Venki
75 static DEFINE_MUTEX(dbs_mutex
);
77 static struct workqueue_struct
*kondemand_wq
;
79 static struct dbs_tuners
{
80 unsigned int sampling_rate
;
81 unsigned int up_threshold
;
82 unsigned int ignore_nice
;
83 unsigned int powersave_bias
;
85 .up_threshold
= DEF_FREQUENCY_UP_THRESHOLD
,
90 static inline cputime64_t
get_cpu_idle_time(unsigned int cpu
)
94 retval
= cputime64_add(kstat_cpu(cpu
).cpustat
.idle
,
95 kstat_cpu(cpu
).cpustat
.iowait
);
97 if (dbs_tuners_ins
.ignore_nice
)
98 retval
= cputime64_add(retval
, kstat_cpu(cpu
).cpustat
.nice
);
104 * Find right freq to be set now with powersave_bias on.
105 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
106 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
108 unsigned int powersave_bias_target(struct cpufreq_policy
*policy
,
109 unsigned int freq_next
, unsigned int relation
)
111 unsigned int freq_req
, freq_reduc
, freq_avg
;
112 unsigned int freq_hi
, freq_lo
;
113 unsigned int index
= 0;
114 unsigned int jiffies_total
, jiffies_hi
, jiffies_lo
;
115 struct cpu_dbs_info_s
*dbs_info
= &per_cpu(cpu_dbs_info
, policy
->cpu
);
117 if (!dbs_info
->freq_table
) {
118 dbs_info
->freq_lo
= 0;
119 dbs_info
->freq_lo_jiffies
= 0;
123 cpufreq_frequency_table_target(policy
, dbs_info
->freq_table
, freq_next
,
125 freq_req
= dbs_info
->freq_table
[index
].frequency
;
126 freq_reduc
= freq_req
* dbs_tuners_ins
.powersave_bias
/ 1000;
127 freq_avg
= freq_req
- freq_reduc
;
129 /* Find freq bounds for freq_avg in freq_table */
131 cpufreq_frequency_table_target(policy
, dbs_info
->freq_table
, freq_avg
,
132 CPUFREQ_RELATION_H
, &index
);
133 freq_lo
= dbs_info
->freq_table
[index
].frequency
;
135 cpufreq_frequency_table_target(policy
, dbs_info
->freq_table
, freq_avg
,
136 CPUFREQ_RELATION_L
, &index
);
137 freq_hi
= dbs_info
->freq_table
[index
].frequency
;
139 /* Find out how long we have to be in hi and lo freqs */
140 if (freq_hi
== freq_lo
) {
141 dbs_info
->freq_lo
= 0;
142 dbs_info
->freq_lo_jiffies
= 0;
145 jiffies_total
= usecs_to_jiffies(dbs_tuners_ins
.sampling_rate
);
146 jiffies_hi
= (freq_avg
- freq_lo
) * jiffies_total
;
147 jiffies_hi
+= ((freq_hi
- freq_lo
) / 2);
148 jiffies_hi
/= (freq_hi
- freq_lo
);
149 jiffies_lo
= jiffies_total
- jiffies_hi
;
150 dbs_info
->freq_lo
= freq_lo
;
151 dbs_info
->freq_lo_jiffies
= jiffies_lo
;
152 dbs_info
->freq_hi_jiffies
= jiffies_hi
;
156 static void ondemand_powersave_bias_init(void)
159 for_each_online_cpu(i
) {
160 struct cpu_dbs_info_s
*dbs_info
= &per_cpu(cpu_dbs_info
, i
);
161 dbs_info
->freq_table
= cpufreq_frequency_get_table(i
);
162 dbs_info
->freq_lo
= 0;
166 /************************** sysfs interface ************************/
167 static ssize_t
show_sampling_rate_max(struct cpufreq_policy
*policy
, char *buf
)
169 return sprintf (buf
, "%u\n", MAX_SAMPLING_RATE
);
172 static ssize_t
show_sampling_rate_min(struct cpufreq_policy
*policy
, char *buf
)
174 return sprintf (buf
, "%u\n", MIN_SAMPLING_RATE
);
177 #define define_one_ro(_name) \
178 static struct freq_attr _name = \
179 __ATTR(_name, 0444, show_##_name, NULL)
181 define_one_ro(sampling_rate_max
);
182 define_one_ro(sampling_rate_min
);
184 /* cpufreq_ondemand Governor Tunables */
185 #define show_one(file_name, object) \
186 static ssize_t show_##file_name \
187 (struct cpufreq_policy *unused, char *buf) \
189 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
191 show_one(sampling_rate
, sampling_rate
);
192 show_one(up_threshold
, up_threshold
);
193 show_one(ignore_nice_load
, ignore_nice
);
194 show_one(powersave_bias
, powersave_bias
);
196 static ssize_t
store_sampling_rate(struct cpufreq_policy
*unused
,
197 const char *buf
, size_t count
)
201 ret
= sscanf(buf
, "%u", &input
);
203 mutex_lock(&dbs_mutex
);
204 if (ret
!= 1 || input
> MAX_SAMPLING_RATE
|| input
< MIN_SAMPLING_RATE
) {
205 mutex_unlock(&dbs_mutex
);
209 dbs_tuners_ins
.sampling_rate
= input
;
210 mutex_unlock(&dbs_mutex
);
215 static ssize_t
store_up_threshold(struct cpufreq_policy
*unused
,
216 const char *buf
, size_t count
)
220 ret
= sscanf(buf
, "%u", &input
);
222 mutex_lock(&dbs_mutex
);
223 if (ret
!= 1 || input
> MAX_FREQUENCY_UP_THRESHOLD
||
224 input
< MIN_FREQUENCY_UP_THRESHOLD
) {
225 mutex_unlock(&dbs_mutex
);
229 dbs_tuners_ins
.up_threshold
= input
;
230 mutex_unlock(&dbs_mutex
);
235 static ssize_t
store_ignore_nice_load(struct cpufreq_policy
*policy
,
236 const char *buf
, size_t count
)
243 ret
= sscanf(buf
, "%u", &input
);
250 mutex_lock(&dbs_mutex
);
251 if ( input
== dbs_tuners_ins
.ignore_nice
) { /* nothing to do */
252 mutex_unlock(&dbs_mutex
);
255 dbs_tuners_ins
.ignore_nice
= input
;
257 /* we need to re-evaluate prev_cpu_idle */
258 for_each_online_cpu(j
) {
259 struct cpu_dbs_info_s
*dbs_info
;
260 dbs_info
= &per_cpu(cpu_dbs_info
, j
);
261 dbs_info
->prev_cpu_idle
= get_cpu_idle_time(j
);
262 dbs_info
->prev_cpu_wall
= get_jiffies_64();
264 mutex_unlock(&dbs_mutex
);
269 static ssize_t
store_powersave_bias(struct cpufreq_policy
*unused
,
270 const char *buf
, size_t count
)
274 ret
= sscanf(buf
, "%u", &input
);
282 mutex_lock(&dbs_mutex
);
283 dbs_tuners_ins
.powersave_bias
= input
;
284 ondemand_powersave_bias_init();
285 mutex_unlock(&dbs_mutex
);
290 #define define_one_rw(_name) \
291 static struct freq_attr _name = \
292 __ATTR(_name, 0644, show_##_name, store_##_name)
294 define_one_rw(sampling_rate
);
295 define_one_rw(up_threshold
);
296 define_one_rw(ignore_nice_load
);
297 define_one_rw(powersave_bias
);
299 static struct attribute
* dbs_attributes
[] = {
300 &sampling_rate_max
.attr
,
301 &sampling_rate_min
.attr
,
304 &ignore_nice_load
.attr
,
305 &powersave_bias
.attr
,
309 static struct attribute_group dbs_attr_group
= {
310 .attrs
= dbs_attributes
,
314 /************************** sysfs end ************************/
316 static void dbs_check_cpu(struct cpu_dbs_info_s
*this_dbs_info
)
318 unsigned int idle_ticks
, total_ticks
;
320 cputime64_t cur_jiffies
;
322 struct cpufreq_policy
*policy
;
325 if (!this_dbs_info
->enable
)
328 this_dbs_info
->freq_lo
= 0;
329 policy
= this_dbs_info
->cur_policy
;
330 cur_jiffies
= jiffies64_to_cputime64(get_jiffies_64());
331 total_ticks
= (unsigned int) cputime64_sub(cur_jiffies
,
332 this_dbs_info
->prev_cpu_wall
);
333 this_dbs_info
->prev_cpu_wall
= cur_jiffies
;
337 * Every sampling_rate, we check, if current idle time is less
338 * than 20% (default), then we try to increase frequency
339 * Every sampling_rate, we look for a the lowest
340 * frequency which can sustain the load while keeping idle time over
341 * 30%. If such a frequency exist, we try to decrease to this frequency.
343 * Any frequency increase takes it to the maximum frequency.
344 * Frequency reduction happens at minimum steps of
345 * 5% (default) of current frequency
349 idle_ticks
= UINT_MAX
;
350 for_each_cpu_mask(j
, policy
->cpus
) {
351 cputime64_t total_idle_ticks
;
352 unsigned int tmp_idle_ticks
;
353 struct cpu_dbs_info_s
*j_dbs_info
;
355 j_dbs_info
= &per_cpu(cpu_dbs_info
, j
);
356 total_idle_ticks
= get_cpu_idle_time(j
);
357 tmp_idle_ticks
= (unsigned int) cputime64_sub(total_idle_ticks
,
358 j_dbs_info
->prev_cpu_idle
);
359 j_dbs_info
->prev_cpu_idle
= total_idle_ticks
;
361 if (tmp_idle_ticks
< idle_ticks
)
362 idle_ticks
= tmp_idle_ticks
;
364 load
= (100 * (total_ticks
- idle_ticks
)) / total_ticks
;
366 /* Check for frequency increase */
367 if (load
> dbs_tuners_ins
.up_threshold
) {
368 /* if we are already at full speed then break out early */
369 if (!dbs_tuners_ins
.powersave_bias
) {
370 if (policy
->cur
== policy
->max
)
373 __cpufreq_driver_target(policy
, policy
->max
,
376 int freq
= powersave_bias_target(policy
, policy
->max
,
378 __cpufreq_driver_target(policy
, freq
,
384 /* Check for frequency decrease */
385 /* if we cannot reduce the frequency anymore, break out early */
386 if (policy
->cur
== policy
->min
)
390 * The optimal frequency is the frequency that is the lowest that
391 * can support the current CPU usage without triggering the up
392 * policy. To be safe, we focus 10 points under the threshold.
394 if (load
< (dbs_tuners_ins
.up_threshold
- 10)) {
395 unsigned int freq_next
= (policy
->cur
* load
) /
396 (dbs_tuners_ins
.up_threshold
- 10);
397 if (!dbs_tuners_ins
.powersave_bias
) {
398 __cpufreq_driver_target(policy
, freq_next
,
401 int freq
= powersave_bias_target(policy
, freq_next
,
403 __cpufreq_driver_target(policy
, freq
,
410 enum {DBS_NORMAL_SAMPLE
, DBS_SUB_SAMPLE
};
412 static void do_dbs_timer(void *data
)
414 unsigned int cpu
= smp_processor_id();
415 struct cpu_dbs_info_s
*dbs_info
= &per_cpu(cpu_dbs_info
, cpu
);
416 /* We want all CPUs to do sampling nearly on same jiffy */
417 int delay
= usecs_to_jiffies(dbs_tuners_ins
.sampling_rate
);
418 delay
-= jiffies
% delay
;
420 if (!dbs_info
->enable
)
422 /* Common NORMAL_SAMPLE setup */
423 INIT_WORK(&dbs_info
->work
, do_dbs_timer
, (void *)DBS_NORMAL_SAMPLE
);
424 if (!dbs_tuners_ins
.powersave_bias
||
425 (unsigned long) data
== DBS_NORMAL_SAMPLE
) {
427 dbs_check_cpu(dbs_info
);
428 unlock_cpu_hotplug();
429 if (dbs_info
->freq_lo
) {
430 /* Setup timer for SUB_SAMPLE */
431 INIT_WORK(&dbs_info
->work
, do_dbs_timer
,
432 (void *)DBS_SUB_SAMPLE
);
433 delay
= dbs_info
->freq_hi_jiffies
;
436 __cpufreq_driver_target(dbs_info
->cur_policy
,
440 queue_delayed_work_on(cpu
, kondemand_wq
, &dbs_info
->work
, delay
);
443 static inline void dbs_timer_init(unsigned int cpu
)
445 struct cpu_dbs_info_s
*dbs_info
= &per_cpu(cpu_dbs_info
, cpu
);
446 /* We want all CPUs to do sampling nearly on same jiffy */
447 int delay
= usecs_to_jiffies(dbs_tuners_ins
.sampling_rate
);
448 delay
-= jiffies
% delay
;
450 ondemand_powersave_bias_init();
451 INIT_WORK(&dbs_info
->work
, do_dbs_timer
, 0);
452 queue_delayed_work_on(cpu
, kondemand_wq
, &dbs_info
->work
, delay
);
455 static inline void dbs_timer_exit(struct cpu_dbs_info_s
*dbs_info
)
457 dbs_info
->enable
= 0;
458 cancel_delayed_work(&dbs_info
->work
);
459 flush_workqueue(kondemand_wq
);
462 static int cpufreq_governor_dbs(struct cpufreq_policy
*policy
,
465 unsigned int cpu
= policy
->cpu
;
466 struct cpu_dbs_info_s
*this_dbs_info
;
469 this_dbs_info
= &per_cpu(cpu_dbs_info
, cpu
);
472 case CPUFREQ_GOV_START
:
473 if ((!cpu_online(cpu
)) || (!policy
->cur
))
476 if (policy
->cpuinfo
.transition_latency
>
477 (TRANSITION_LATENCY_LIMIT
* 1000)) {
478 printk(KERN_WARNING
"ondemand governor failed to load "
479 "due to too long transition latency\n");
482 if (this_dbs_info
->enable
) /* Already enabled */
485 mutex_lock(&dbs_mutex
);
487 if (dbs_enable
== 1) {
488 kondemand_wq
= create_workqueue("kondemand");
490 printk(KERN_ERR
"Creation of kondemand failed\n");
492 mutex_unlock(&dbs_mutex
);
496 for_each_cpu_mask(j
, policy
->cpus
) {
497 struct cpu_dbs_info_s
*j_dbs_info
;
498 j_dbs_info
= &per_cpu(cpu_dbs_info
, j
);
499 j_dbs_info
->cur_policy
= policy
;
501 j_dbs_info
->prev_cpu_idle
= get_cpu_idle_time(j
);
502 j_dbs_info
->prev_cpu_wall
= get_jiffies_64();
504 this_dbs_info
->enable
= 1;
505 sysfs_create_group(&policy
->kobj
, &dbs_attr_group
);
507 * Start the timerschedule work, when this governor
508 * is used for first time
510 if (dbs_enable
== 1) {
511 unsigned int latency
;
512 /* policy latency is in nS. Convert it to uS first */
513 latency
= policy
->cpuinfo
.transition_latency
/ 1000;
517 def_sampling_rate
= latency
*
518 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER
;
520 if (def_sampling_rate
< MIN_STAT_SAMPLING_RATE
)
521 def_sampling_rate
= MIN_STAT_SAMPLING_RATE
;
523 dbs_tuners_ins
.sampling_rate
= def_sampling_rate
;
525 dbs_timer_init(policy
->cpu
);
527 mutex_unlock(&dbs_mutex
);
530 case CPUFREQ_GOV_STOP
:
531 mutex_lock(&dbs_mutex
);
532 dbs_timer_exit(this_dbs_info
);
533 sysfs_remove_group(&policy
->kobj
, &dbs_attr_group
);
536 destroy_workqueue(kondemand_wq
);
538 mutex_unlock(&dbs_mutex
);
542 case CPUFREQ_GOV_LIMITS
:
543 mutex_lock(&dbs_mutex
);
544 if (policy
->max
< this_dbs_info
->cur_policy
->cur
)
545 __cpufreq_driver_target(this_dbs_info
->cur_policy
,
548 else if (policy
->min
> this_dbs_info
->cur_policy
->cur
)
549 __cpufreq_driver_target(this_dbs_info
->cur_policy
,
552 mutex_unlock(&dbs_mutex
);
558 static struct cpufreq_governor cpufreq_gov_dbs
= {
560 .governor
= cpufreq_governor_dbs
,
561 .owner
= THIS_MODULE
,
564 static int __init
cpufreq_gov_dbs_init(void)
566 return cpufreq_register_governor(&cpufreq_gov_dbs
);
569 static void __exit
cpufreq_gov_dbs_exit(void)
571 cpufreq_unregister_governor(&cpufreq_gov_dbs
);
575 MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
576 MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
577 MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
578 "Low Latency Frequency Transition capable processors");
579 MODULE_LICENSE("GPL");
581 module_init(cpufreq_gov_dbs_init
);
582 module_exit(cpufreq_gov_dbs_exit
);