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 #include <linux/cpufreq.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <trace/events/power.h>
19 struct sugov_tunables
{
20 struct gov_attr_set attr_set
;
21 unsigned int rate_limit_us
;
25 struct cpufreq_policy
*policy
;
27 struct sugov_tunables
*tunables
;
28 struct list_head tunables_hook
;
30 raw_spinlock_t update_lock
; /* For shared policies */
31 u64 last_freq_update_time
;
32 s64 freq_update_delay_ns
;
33 unsigned int next_freq
;
35 /* The next fields are only needed if fast switch cannot be used. */
36 struct irq_work irq_work
;
37 struct work_struct work
;
38 struct mutex work_lock
;
39 bool work_in_progress
;
41 bool need_freq_update
;
45 struct update_util_data update_util
;
46 struct sugov_policy
*sg_policy
;
48 /* The fields below are only needed when sharing a policy. */
54 static DEFINE_PER_CPU(struct sugov_cpu
, sugov_cpu
);
56 /************************ Governor internals ***********************/
58 static bool sugov_should_update_freq(struct sugov_policy
*sg_policy
, u64 time
)
62 if (sg_policy
->work_in_progress
)
65 if (unlikely(sg_policy
->need_freq_update
)) {
66 sg_policy
->need_freq_update
= false;
68 * This happens when limits change, so forget the previous
69 * next_freq value and force an update.
71 sg_policy
->next_freq
= UINT_MAX
;
75 delta_ns
= time
- sg_policy
->last_freq_update_time
;
76 return delta_ns
>= sg_policy
->freq_update_delay_ns
;
79 static void sugov_update_commit(struct sugov_policy
*sg_policy
, u64 time
,
80 unsigned int next_freq
)
82 struct cpufreq_policy
*policy
= sg_policy
->policy
;
84 sg_policy
->last_freq_update_time
= time
;
86 if (policy
->fast_switch_enabled
) {
87 if (sg_policy
->next_freq
== next_freq
) {
88 trace_cpu_frequency(policy
->cur
, smp_processor_id());
91 sg_policy
->next_freq
= next_freq
;
92 next_freq
= cpufreq_driver_fast_switch(policy
, next_freq
);
93 if (next_freq
== CPUFREQ_ENTRY_INVALID
)
96 policy
->cur
= next_freq
;
97 trace_cpu_frequency(next_freq
, smp_processor_id());
98 } else if (sg_policy
->next_freq
!= next_freq
) {
99 sg_policy
->next_freq
= next_freq
;
100 sg_policy
->work_in_progress
= true;
101 irq_work_queue(&sg_policy
->irq_work
);
106 * get_next_freq - Compute a new frequency for a given cpufreq policy.
107 * @policy: cpufreq policy object to compute the new frequency for.
108 * @util: Current CPU utilization.
109 * @max: CPU capacity.
111 * If the utilization is frequency-invariant, choose the new frequency to be
112 * proportional to it, that is
114 * next_freq = C * max_freq * util / max
116 * Otherwise, approximate the would-be frequency-invariant utilization by
117 * util_raw * (curr_freq / max_freq) which leads to
119 * next_freq = C * curr_freq * util_raw / max
121 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
123 static unsigned int get_next_freq(struct cpufreq_policy
*policy
,
124 unsigned long util
, unsigned long max
)
126 unsigned int freq
= arch_scale_freq_invariant() ?
127 policy
->cpuinfo
.max_freq
: policy
->cur
;
129 return (freq
+ (freq
>> 2)) * util
/ max
;
132 static void sugov_update_single(struct update_util_data
*hook
, u64 time
,
133 unsigned long util
, unsigned long max
)
135 struct sugov_cpu
*sg_cpu
= container_of(hook
, struct sugov_cpu
, update_util
);
136 struct sugov_policy
*sg_policy
= sg_cpu
->sg_policy
;
137 struct cpufreq_policy
*policy
= sg_policy
->policy
;
140 if (!sugov_should_update_freq(sg_policy
, time
))
143 next_f
= util
== ULONG_MAX
? policy
->cpuinfo
.max_freq
:
144 get_next_freq(policy
, util
, max
);
145 sugov_update_commit(sg_policy
, time
, next_f
);
148 static unsigned int sugov_next_freq_shared(struct sugov_policy
*sg_policy
,
149 unsigned long util
, unsigned long max
)
151 struct cpufreq_policy
*policy
= sg_policy
->policy
;
152 unsigned int max_f
= policy
->cpuinfo
.max_freq
;
153 u64 last_freq_update_time
= sg_policy
->last_freq_update_time
;
156 if (util
== ULONG_MAX
)
159 for_each_cpu(j
, policy
->cpus
) {
160 struct sugov_cpu
*j_sg_cpu
;
161 unsigned long j_util
, j_max
;
164 if (j
== smp_processor_id())
167 j_sg_cpu
= &per_cpu(sugov_cpu
, j
);
169 * If the CPU utilization was last updated before the previous
170 * frequency update and the time elapsed between the last update
171 * of the CPU utilization and the last frequency update is long
172 * enough, don't take the CPU into account as it probably is
175 delta_ns
= last_freq_update_time
- j_sg_cpu
->last_update
;
176 if (delta_ns
> TICK_NSEC
)
179 j_util
= j_sg_cpu
->util
;
180 if (j_util
== ULONG_MAX
)
183 j_max
= j_sg_cpu
->max
;
184 if (j_util
* max
> j_max
* util
) {
190 return get_next_freq(policy
, util
, max
);
193 static void sugov_update_shared(struct update_util_data
*hook
, u64 time
,
194 unsigned long util
, unsigned long max
)
196 struct sugov_cpu
*sg_cpu
= container_of(hook
, struct sugov_cpu
, update_util
);
197 struct sugov_policy
*sg_policy
= sg_cpu
->sg_policy
;
200 raw_spin_lock(&sg_policy
->update_lock
);
204 sg_cpu
->last_update
= time
;
206 if (sugov_should_update_freq(sg_policy
, time
)) {
207 next_f
= sugov_next_freq_shared(sg_policy
, util
, max
);
208 sugov_update_commit(sg_policy
, time
, next_f
);
211 raw_spin_unlock(&sg_policy
->update_lock
);
214 static void sugov_work(struct work_struct
*work
)
216 struct sugov_policy
*sg_policy
= container_of(work
, struct sugov_policy
, work
);
218 mutex_lock(&sg_policy
->work_lock
);
219 __cpufreq_driver_target(sg_policy
->policy
, sg_policy
->next_freq
,
221 mutex_unlock(&sg_policy
->work_lock
);
223 sg_policy
->work_in_progress
= false;
226 static void sugov_irq_work(struct irq_work
*irq_work
)
228 struct sugov_policy
*sg_policy
;
230 sg_policy
= container_of(irq_work
, struct sugov_policy
, irq_work
);
231 schedule_work_on(smp_processor_id(), &sg_policy
->work
);
234 /************************** sysfs interface ************************/
236 static struct sugov_tunables
*global_tunables
;
237 static DEFINE_MUTEX(global_tunables_lock
);
239 static inline struct sugov_tunables
*to_sugov_tunables(struct gov_attr_set
*attr_set
)
241 return container_of(attr_set
, struct sugov_tunables
, attr_set
);
244 static ssize_t
rate_limit_us_show(struct gov_attr_set
*attr_set
, char *buf
)
246 struct sugov_tunables
*tunables
= to_sugov_tunables(attr_set
);
248 return sprintf(buf
, "%u\n", tunables
->rate_limit_us
);
251 static ssize_t
rate_limit_us_store(struct gov_attr_set
*attr_set
, const char *buf
,
254 struct sugov_tunables
*tunables
= to_sugov_tunables(attr_set
);
255 struct sugov_policy
*sg_policy
;
256 unsigned int rate_limit_us
;
258 if (kstrtouint(buf
, 10, &rate_limit_us
))
261 tunables
->rate_limit_us
= rate_limit_us
;
263 list_for_each_entry(sg_policy
, &attr_set
->policy_list
, tunables_hook
)
264 sg_policy
->freq_update_delay_ns
= rate_limit_us
* NSEC_PER_USEC
;
269 static struct governor_attr rate_limit_us
= __ATTR_RW(rate_limit_us
);
271 static struct attribute
*sugov_attributes
[] = {
276 static struct kobj_type sugov_tunables_ktype
= {
277 .default_attrs
= sugov_attributes
,
278 .sysfs_ops
= &governor_sysfs_ops
,
281 /********************** cpufreq governor interface *********************/
283 static struct cpufreq_governor schedutil_gov
;
285 static struct sugov_policy
*sugov_policy_alloc(struct cpufreq_policy
*policy
)
287 struct sugov_policy
*sg_policy
;
289 sg_policy
= kzalloc(sizeof(*sg_policy
), GFP_KERNEL
);
293 sg_policy
->policy
= policy
;
294 init_irq_work(&sg_policy
->irq_work
, sugov_irq_work
);
295 INIT_WORK(&sg_policy
->work
, sugov_work
);
296 mutex_init(&sg_policy
->work_lock
);
297 raw_spin_lock_init(&sg_policy
->update_lock
);
301 static void sugov_policy_free(struct sugov_policy
*sg_policy
)
303 mutex_destroy(&sg_policy
->work_lock
);
307 static struct sugov_tunables
*sugov_tunables_alloc(struct sugov_policy
*sg_policy
)
309 struct sugov_tunables
*tunables
;
311 tunables
= kzalloc(sizeof(*tunables
), GFP_KERNEL
);
313 gov_attr_set_init(&tunables
->attr_set
, &sg_policy
->tunables_hook
);
314 if (!have_governor_per_policy())
315 global_tunables
= tunables
;
320 static void sugov_tunables_free(struct sugov_tunables
*tunables
)
322 if (!have_governor_per_policy())
323 global_tunables
= NULL
;
328 static int sugov_init(struct cpufreq_policy
*policy
)
330 struct sugov_policy
*sg_policy
;
331 struct sugov_tunables
*tunables
;
335 /* State should be equivalent to EXIT */
336 if (policy
->governor_data
)
339 sg_policy
= sugov_policy_alloc(policy
);
343 mutex_lock(&global_tunables_lock
);
345 if (global_tunables
) {
346 if (WARN_ON(have_governor_per_policy())) {
350 policy
->governor_data
= sg_policy
;
351 sg_policy
->tunables
= global_tunables
;
353 gov_attr_set_get(&global_tunables
->attr_set
, &sg_policy
->tunables_hook
);
357 tunables
= sugov_tunables_alloc(sg_policy
);
363 tunables
->rate_limit_us
= LATENCY_MULTIPLIER
;
364 lat
= policy
->cpuinfo
.transition_latency
/ NSEC_PER_USEC
;
366 tunables
->rate_limit_us
*= lat
;
368 policy
->governor_data
= sg_policy
;
369 sg_policy
->tunables
= tunables
;
371 ret
= kobject_init_and_add(&tunables
->attr_set
.kobj
, &sugov_tunables_ktype
,
372 get_governor_parent_kobj(policy
), "%s",
378 mutex_unlock(&global_tunables_lock
);
380 cpufreq_enable_fast_switch(policy
);
384 policy
->governor_data
= NULL
;
385 sugov_tunables_free(tunables
);
388 mutex_unlock(&global_tunables_lock
);
390 sugov_policy_free(sg_policy
);
391 pr_err("cpufreq: schedutil governor initialization failed (error %d)\n", ret
);
395 static int sugov_exit(struct cpufreq_policy
*policy
)
397 struct sugov_policy
*sg_policy
= policy
->governor_data
;
398 struct sugov_tunables
*tunables
= sg_policy
->tunables
;
401 cpufreq_disable_fast_switch(policy
);
403 mutex_lock(&global_tunables_lock
);
405 count
= gov_attr_set_put(&tunables
->attr_set
, &sg_policy
->tunables_hook
);
406 policy
->governor_data
= NULL
;
408 sugov_tunables_free(tunables
);
410 mutex_unlock(&global_tunables_lock
);
412 sugov_policy_free(sg_policy
);
416 static int sugov_start(struct cpufreq_policy
*policy
)
418 struct sugov_policy
*sg_policy
= policy
->governor_data
;
421 sg_policy
->freq_update_delay_ns
= sg_policy
->tunables
->rate_limit_us
* NSEC_PER_USEC
;
422 sg_policy
->last_freq_update_time
= 0;
423 sg_policy
->next_freq
= UINT_MAX
;
424 sg_policy
->work_in_progress
= false;
425 sg_policy
->need_freq_update
= false;
427 for_each_cpu(cpu
, policy
->cpus
) {
428 struct sugov_cpu
*sg_cpu
= &per_cpu(sugov_cpu
, cpu
);
430 sg_cpu
->sg_policy
= sg_policy
;
431 if (policy_is_shared(policy
)) {
432 sg_cpu
->util
= ULONG_MAX
;
434 sg_cpu
->last_update
= 0;
435 cpufreq_add_update_util_hook(cpu
, &sg_cpu
->update_util
,
436 sugov_update_shared
);
438 cpufreq_add_update_util_hook(cpu
, &sg_cpu
->update_util
,
439 sugov_update_single
);
445 static int sugov_stop(struct cpufreq_policy
*policy
)
447 struct sugov_policy
*sg_policy
= policy
->governor_data
;
450 for_each_cpu(cpu
, policy
->cpus
)
451 cpufreq_remove_update_util_hook(cpu
);
455 irq_work_sync(&sg_policy
->irq_work
);
456 cancel_work_sync(&sg_policy
->work
);
460 static int sugov_limits(struct cpufreq_policy
*policy
)
462 struct sugov_policy
*sg_policy
= policy
->governor_data
;
464 if (!policy
->fast_switch_enabled
) {
465 mutex_lock(&sg_policy
->work_lock
);
467 if (policy
->max
< policy
->cur
)
468 __cpufreq_driver_target(policy
, policy
->max
,
470 else if (policy
->min
> policy
->cur
)
471 __cpufreq_driver_target(policy
, policy
->min
,
474 mutex_unlock(&sg_policy
->work_lock
);
477 sg_policy
->need_freq_update
= true;
481 int sugov_governor(struct cpufreq_policy
*policy
, unsigned int event
)
483 if (event
== CPUFREQ_GOV_POLICY_INIT
) {
484 return sugov_init(policy
);
485 } else if (policy
->governor_data
) {
487 case CPUFREQ_GOV_POLICY_EXIT
:
488 return sugov_exit(policy
);
489 case CPUFREQ_GOV_START
:
490 return sugov_start(policy
);
491 case CPUFREQ_GOV_STOP
:
492 return sugov_stop(policy
);
493 case CPUFREQ_GOV_LIMITS
:
494 return sugov_limits(policy
);
500 static struct cpufreq_governor schedutil_gov
= {
502 .governor
= sugov_governor
,
503 .owner
= THIS_MODULE
,
506 static int __init
sugov_module_init(void)
508 return cpufreq_register_governor(&schedutil_gov
);
511 static void __exit
sugov_module_exit(void)
513 cpufreq_unregister_governor(&schedutil_gov
);
516 MODULE_AUTHOR("Rafael J. Wysocki <rafael.j.wysocki@intel.com>");
517 MODULE_DESCRIPTION("Utilization-based CPU frequency selection");
518 MODULE_LICENSE("GPL");
520 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
521 struct cpufreq_governor
*cpufreq_default_governor(void)
523 return &schedutil_gov
;
526 fs_initcall(sugov_module_init
);
528 module_init(sugov_module_init
);
530 module_exit(sugov_module_exit
);