2 * CPUFreq governor based on scheduler-provided CPU utilization data.
4 * Copyright (C) 2016, Intel Corporation
5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/cpufreq.h>
15 #include <linux/slab.h>
16 #include <trace/events/power.h>
20 struct sugov_tunables
{
21 struct gov_attr_set attr_set
;
22 unsigned int rate_limit_us
;
26 struct cpufreq_policy
*policy
;
28 struct sugov_tunables
*tunables
;
29 struct list_head tunables_hook
;
31 raw_spinlock_t update_lock
; /* For shared policies */
32 u64 last_freq_update_time
;
33 s64 freq_update_delay_ns
;
34 unsigned int next_freq
;
36 /* The next fields are only needed if fast switch cannot be used. */
37 struct irq_work irq_work
;
38 struct work_struct work
;
39 struct mutex work_lock
;
40 bool work_in_progress
;
42 bool need_freq_update
;
46 struct update_util_data update_util
;
47 struct sugov_policy
*sg_policy
;
49 unsigned int cached_raw_freq
;
50 unsigned long iowait_boost
;
51 unsigned long iowait_boost_max
;
54 /* The fields below are only needed when sharing a policy. */
60 static DEFINE_PER_CPU(struct sugov_cpu
, sugov_cpu
);
62 /************************ Governor internals ***********************/
64 static bool sugov_should_update_freq(struct sugov_policy
*sg_policy
, u64 time
)
68 if (sg_policy
->work_in_progress
)
71 if (unlikely(sg_policy
->need_freq_update
)) {
72 sg_policy
->need_freq_update
= false;
74 * This happens when limits change, so forget the previous
75 * next_freq value and force an update.
77 sg_policy
->next_freq
= UINT_MAX
;
81 delta_ns
= time
- sg_policy
->last_freq_update_time
;
82 return delta_ns
>= sg_policy
->freq_update_delay_ns
;
85 static void sugov_update_commit(struct sugov_policy
*sg_policy
, u64 time
,
86 unsigned int next_freq
)
88 struct cpufreq_policy
*policy
= sg_policy
->policy
;
90 sg_policy
->last_freq_update_time
= time
;
92 if (policy
->fast_switch_enabled
) {
93 if (sg_policy
->next_freq
== next_freq
) {
94 trace_cpu_frequency(policy
->cur
, smp_processor_id());
97 sg_policy
->next_freq
= next_freq
;
98 next_freq
= cpufreq_driver_fast_switch(policy
, next_freq
);
99 if (next_freq
== CPUFREQ_ENTRY_INVALID
)
102 policy
->cur
= next_freq
;
103 trace_cpu_frequency(next_freq
, smp_processor_id());
104 } else if (sg_policy
->next_freq
!= next_freq
) {
105 sg_policy
->next_freq
= next_freq
;
106 sg_policy
->work_in_progress
= true;
107 irq_work_queue(&sg_policy
->irq_work
);
112 * get_next_freq - Compute a new frequency for a given cpufreq policy.
113 * @sg_cpu: schedutil cpu object to compute the new frequency for.
114 * @util: Current CPU utilization.
115 * @max: CPU capacity.
117 * If the utilization is frequency-invariant, choose the new frequency to be
118 * proportional to it, that is
120 * next_freq = C * max_freq * util / max
122 * Otherwise, approximate the would-be frequency-invariant utilization by
123 * util_raw * (curr_freq / max_freq) which leads to
125 * next_freq = C * curr_freq * util_raw / max
127 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
129 * The lowest driver-supported frequency which is equal or greater than the raw
130 * next_freq (as calculated above) is returned, subject to policy min/max and
131 * cpufreq driver limitations.
133 static unsigned int get_next_freq(struct sugov_cpu
*sg_cpu
, unsigned long util
,
136 struct sugov_policy
*sg_policy
= sg_cpu
->sg_policy
;
137 struct cpufreq_policy
*policy
= sg_policy
->policy
;
138 unsigned int freq
= arch_scale_freq_invariant() ?
139 policy
->cpuinfo
.max_freq
: policy
->cur
;
141 freq
= (freq
+ (freq
>> 2)) * util
/ max
;
143 if (freq
== sg_cpu
->cached_raw_freq
&& sg_policy
->next_freq
!= UINT_MAX
)
144 return sg_policy
->next_freq
;
145 sg_cpu
->cached_raw_freq
= freq
;
146 return cpufreq_driver_resolve_freq(policy
, freq
);
149 static void sugov_get_util(unsigned long *util
, unsigned long *max
)
151 struct rq
*rq
= this_rq();
152 unsigned long cfs_max
;
154 cfs_max
= arch_scale_cpu_capacity(NULL
, smp_processor_id());
156 *util
= min(rq
->cfs
.avg
.util_avg
, cfs_max
);
160 static void sugov_set_iowait_boost(struct sugov_cpu
*sg_cpu
, u64 time
,
163 if (flags
& SCHED_CPUFREQ_IOWAIT
) {
164 sg_cpu
->iowait_boost
= sg_cpu
->iowait_boost_max
;
165 } else if (sg_cpu
->iowait_boost
) {
166 s64 delta_ns
= time
- sg_cpu
->last_update
;
168 /* Clear iowait_boost if the CPU apprears to have been idle. */
169 if (delta_ns
> TICK_NSEC
)
170 sg_cpu
->iowait_boost
= 0;
174 static void sugov_iowait_boost(struct sugov_cpu
*sg_cpu
, unsigned long *util
,
177 unsigned long boost_util
= sg_cpu
->iowait_boost
;
178 unsigned long boost_max
= sg_cpu
->iowait_boost_max
;
183 if (*util
* boost_max
< *max
* boost_util
) {
187 sg_cpu
->iowait_boost
>>= 1;
190 static void sugov_update_single(struct update_util_data
*hook
, u64 time
,
193 struct sugov_cpu
*sg_cpu
= container_of(hook
, struct sugov_cpu
, update_util
);
194 struct sugov_policy
*sg_policy
= sg_cpu
->sg_policy
;
195 struct cpufreq_policy
*policy
= sg_policy
->policy
;
196 unsigned long util
, max
;
199 sugov_set_iowait_boost(sg_cpu
, time
, flags
);
200 sg_cpu
->last_update
= time
;
202 if (!sugov_should_update_freq(sg_policy
, time
))
205 if (flags
& SCHED_CPUFREQ_RT_DL
) {
206 next_f
= policy
->cpuinfo
.max_freq
;
208 sugov_get_util(&util
, &max
);
209 sugov_iowait_boost(sg_cpu
, &util
, &max
);
210 next_f
= get_next_freq(sg_cpu
, util
, max
);
212 sugov_update_commit(sg_policy
, time
, next_f
);
215 static unsigned int sugov_next_freq_shared(struct sugov_cpu
*sg_cpu
,
216 unsigned long util
, unsigned long max
,
219 struct sugov_policy
*sg_policy
= sg_cpu
->sg_policy
;
220 struct cpufreq_policy
*policy
= sg_policy
->policy
;
221 unsigned int max_f
= policy
->cpuinfo
.max_freq
;
222 u64 last_freq_update_time
= sg_policy
->last_freq_update_time
;
225 if (flags
& SCHED_CPUFREQ_RT_DL
)
228 sugov_iowait_boost(sg_cpu
, &util
, &max
);
230 for_each_cpu(j
, policy
->cpus
) {
231 struct sugov_cpu
*j_sg_cpu
;
232 unsigned long j_util
, j_max
;
235 if (j
== smp_processor_id())
238 j_sg_cpu
= &per_cpu(sugov_cpu
, j
);
240 * If the CPU utilization was last updated before the previous
241 * frequency update and the time elapsed between the last update
242 * of the CPU utilization and the last frequency update is long
243 * enough, don't take the CPU into account as it probably is
244 * idle now (and clear iowait_boost for it).
246 delta_ns
= last_freq_update_time
- j_sg_cpu
->last_update
;
247 if (delta_ns
> TICK_NSEC
) {
248 j_sg_cpu
->iowait_boost
= 0;
251 if (j_sg_cpu
->flags
& SCHED_CPUFREQ_RT_DL
)
254 j_util
= j_sg_cpu
->util
;
255 j_max
= j_sg_cpu
->max
;
256 if (j_util
* max
> j_max
* util
) {
261 sugov_iowait_boost(j_sg_cpu
, &util
, &max
);
264 return get_next_freq(sg_cpu
, util
, max
);
267 static void sugov_update_shared(struct update_util_data
*hook
, u64 time
,
270 struct sugov_cpu
*sg_cpu
= container_of(hook
, struct sugov_cpu
, update_util
);
271 struct sugov_policy
*sg_policy
= sg_cpu
->sg_policy
;
272 unsigned long util
, max
;
275 sugov_get_util(&util
, &max
);
277 raw_spin_lock(&sg_policy
->update_lock
);
281 sg_cpu
->flags
= flags
;
283 sugov_set_iowait_boost(sg_cpu
, time
, flags
);
284 sg_cpu
->last_update
= time
;
286 if (sugov_should_update_freq(sg_policy
, time
)) {
287 next_f
= sugov_next_freq_shared(sg_cpu
, util
, max
, flags
);
288 sugov_update_commit(sg_policy
, time
, next_f
);
291 raw_spin_unlock(&sg_policy
->update_lock
);
294 static void sugov_work(struct work_struct
*work
)
296 struct sugov_policy
*sg_policy
= container_of(work
, struct sugov_policy
, work
);
298 mutex_lock(&sg_policy
->work_lock
);
299 __cpufreq_driver_target(sg_policy
->policy
, sg_policy
->next_freq
,
301 mutex_unlock(&sg_policy
->work_lock
);
303 sg_policy
->work_in_progress
= false;
306 static void sugov_irq_work(struct irq_work
*irq_work
)
308 struct sugov_policy
*sg_policy
;
310 sg_policy
= container_of(irq_work
, struct sugov_policy
, irq_work
);
311 schedule_work_on(smp_processor_id(), &sg_policy
->work
);
314 /************************** sysfs interface ************************/
316 static struct sugov_tunables
*global_tunables
;
317 static DEFINE_MUTEX(global_tunables_lock
);
319 static inline struct sugov_tunables
*to_sugov_tunables(struct gov_attr_set
*attr_set
)
321 return container_of(attr_set
, struct sugov_tunables
, attr_set
);
324 static ssize_t
rate_limit_us_show(struct gov_attr_set
*attr_set
, char *buf
)
326 struct sugov_tunables
*tunables
= to_sugov_tunables(attr_set
);
328 return sprintf(buf
, "%u\n", tunables
->rate_limit_us
);
331 static ssize_t
rate_limit_us_store(struct gov_attr_set
*attr_set
, const char *buf
,
334 struct sugov_tunables
*tunables
= to_sugov_tunables(attr_set
);
335 struct sugov_policy
*sg_policy
;
336 unsigned int rate_limit_us
;
338 if (kstrtouint(buf
, 10, &rate_limit_us
))
341 tunables
->rate_limit_us
= rate_limit_us
;
343 list_for_each_entry(sg_policy
, &attr_set
->policy_list
, tunables_hook
)
344 sg_policy
->freq_update_delay_ns
= rate_limit_us
* NSEC_PER_USEC
;
349 static struct governor_attr rate_limit_us
= __ATTR_RW(rate_limit_us
);
351 static struct attribute
*sugov_attributes
[] = {
356 static struct kobj_type sugov_tunables_ktype
= {
357 .default_attrs
= sugov_attributes
,
358 .sysfs_ops
= &governor_sysfs_ops
,
361 /********************** cpufreq governor interface *********************/
363 static struct cpufreq_governor schedutil_gov
;
365 static struct sugov_policy
*sugov_policy_alloc(struct cpufreq_policy
*policy
)
367 struct sugov_policy
*sg_policy
;
369 sg_policy
= kzalloc(sizeof(*sg_policy
), GFP_KERNEL
);
373 sg_policy
->policy
= policy
;
374 init_irq_work(&sg_policy
->irq_work
, sugov_irq_work
);
375 INIT_WORK(&sg_policy
->work
, sugov_work
);
376 mutex_init(&sg_policy
->work_lock
);
377 raw_spin_lock_init(&sg_policy
->update_lock
);
381 static void sugov_policy_free(struct sugov_policy
*sg_policy
)
383 mutex_destroy(&sg_policy
->work_lock
);
387 static struct sugov_tunables
*sugov_tunables_alloc(struct sugov_policy
*sg_policy
)
389 struct sugov_tunables
*tunables
;
391 tunables
= kzalloc(sizeof(*tunables
), GFP_KERNEL
);
393 gov_attr_set_init(&tunables
->attr_set
, &sg_policy
->tunables_hook
);
394 if (!have_governor_per_policy())
395 global_tunables
= tunables
;
400 static void sugov_tunables_free(struct sugov_tunables
*tunables
)
402 if (!have_governor_per_policy())
403 global_tunables
= NULL
;
408 static int sugov_init(struct cpufreq_policy
*policy
)
410 struct sugov_policy
*sg_policy
;
411 struct sugov_tunables
*tunables
;
415 /* State should be equivalent to EXIT */
416 if (policy
->governor_data
)
419 sg_policy
= sugov_policy_alloc(policy
);
423 mutex_lock(&global_tunables_lock
);
425 if (global_tunables
) {
426 if (WARN_ON(have_governor_per_policy())) {
430 policy
->governor_data
= sg_policy
;
431 sg_policy
->tunables
= global_tunables
;
433 gov_attr_set_get(&global_tunables
->attr_set
, &sg_policy
->tunables_hook
);
437 tunables
= sugov_tunables_alloc(sg_policy
);
443 tunables
->rate_limit_us
= LATENCY_MULTIPLIER
;
444 lat
= policy
->cpuinfo
.transition_latency
/ NSEC_PER_USEC
;
446 tunables
->rate_limit_us
*= lat
;
448 policy
->governor_data
= sg_policy
;
449 sg_policy
->tunables
= tunables
;
451 ret
= kobject_init_and_add(&tunables
->attr_set
.kobj
, &sugov_tunables_ktype
,
452 get_governor_parent_kobj(policy
), "%s",
458 mutex_unlock(&global_tunables_lock
);
460 cpufreq_enable_fast_switch(policy
);
464 policy
->governor_data
= NULL
;
465 sugov_tunables_free(tunables
);
468 mutex_unlock(&global_tunables_lock
);
470 sugov_policy_free(sg_policy
);
471 pr_err("initialization failed (error %d)\n", ret
);
475 static void sugov_exit(struct cpufreq_policy
*policy
)
477 struct sugov_policy
*sg_policy
= policy
->governor_data
;
478 struct sugov_tunables
*tunables
= sg_policy
->tunables
;
481 cpufreq_disable_fast_switch(policy
);
483 mutex_lock(&global_tunables_lock
);
485 count
= gov_attr_set_put(&tunables
->attr_set
, &sg_policy
->tunables_hook
);
486 policy
->governor_data
= NULL
;
488 sugov_tunables_free(tunables
);
490 mutex_unlock(&global_tunables_lock
);
492 sugov_policy_free(sg_policy
);
495 static int sugov_start(struct cpufreq_policy
*policy
)
497 struct sugov_policy
*sg_policy
= policy
->governor_data
;
500 sg_policy
->freq_update_delay_ns
= sg_policy
->tunables
->rate_limit_us
* NSEC_PER_USEC
;
501 sg_policy
->last_freq_update_time
= 0;
502 sg_policy
->next_freq
= UINT_MAX
;
503 sg_policy
->work_in_progress
= false;
504 sg_policy
->need_freq_update
= false;
506 for_each_cpu(cpu
, policy
->cpus
) {
507 struct sugov_cpu
*sg_cpu
= &per_cpu(sugov_cpu
, cpu
);
509 sg_cpu
->sg_policy
= sg_policy
;
510 if (policy_is_shared(policy
)) {
513 sg_cpu
->flags
= SCHED_CPUFREQ_RT
;
514 sg_cpu
->last_update
= 0;
515 sg_cpu
->cached_raw_freq
= 0;
516 sg_cpu
->iowait_boost
= 0;
517 sg_cpu
->iowait_boost_max
= policy
->cpuinfo
.max_freq
;
518 cpufreq_add_update_util_hook(cpu
, &sg_cpu
->update_util
,
519 sugov_update_shared
);
521 cpufreq_add_update_util_hook(cpu
, &sg_cpu
->update_util
,
522 sugov_update_single
);
528 static void sugov_stop(struct cpufreq_policy
*policy
)
530 struct sugov_policy
*sg_policy
= policy
->governor_data
;
533 for_each_cpu(cpu
, policy
->cpus
)
534 cpufreq_remove_update_util_hook(cpu
);
538 irq_work_sync(&sg_policy
->irq_work
);
539 cancel_work_sync(&sg_policy
->work
);
542 static void sugov_limits(struct cpufreq_policy
*policy
)
544 struct sugov_policy
*sg_policy
= policy
->governor_data
;
546 if (!policy
->fast_switch_enabled
) {
547 mutex_lock(&sg_policy
->work_lock
);
548 cpufreq_policy_apply_limits(policy
);
549 mutex_unlock(&sg_policy
->work_lock
);
552 sg_policy
->need_freq_update
= true;
555 static struct cpufreq_governor schedutil_gov
= {
557 .owner
= THIS_MODULE
,
560 .start
= sugov_start
,
562 .limits
= sugov_limits
,
565 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
566 struct cpufreq_governor
*cpufreq_default_governor(void)
568 return &schedutil_gov
;
572 static int __init
sugov_register(void)
574 return cpufreq_register_governor(&schedutil_gov
);
576 fs_initcall(sugov_register
);