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;
51 /* The fields below are only needed when sharing a policy. */
58 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
60 /************************ Governor internals ***********************/
62 static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
66 if (sg_policy->work_in_progress)
69 if (unlikely(sg_policy->need_freq_update)) {
70 sg_policy->need_freq_update = false;
72 * This happens when limits change, so forget the previous
73 * next_freq value and force an update.
75 sg_policy->next_freq = UINT_MAX;
79 delta_ns = time - sg_policy->last_freq_update_time;
80 return delta_ns >= sg_policy->freq_update_delay_ns;
83 static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
84 unsigned int next_freq)
86 struct cpufreq_policy *policy = sg_policy->policy;
88 sg_policy->last_freq_update_time = time;
90 if (policy->fast_switch_enabled) {
91 if (sg_policy->next_freq == next_freq) {
92 trace_cpu_frequency(policy->cur, smp_processor_id());
95 sg_policy->next_freq = next_freq;
96 next_freq = cpufreq_driver_fast_switch(policy, next_freq);
97 if (next_freq == CPUFREQ_ENTRY_INVALID)
100 policy->cur = next_freq;
101 trace_cpu_frequency(next_freq, smp_processor_id());
102 } else if (sg_policy->next_freq != next_freq) {
103 sg_policy->next_freq = next_freq;
104 sg_policy->work_in_progress = true;
105 irq_work_queue(&sg_policy->irq_work);
110 * get_next_freq - Compute a new frequency for a given cpufreq policy.
111 * @sg_cpu: schedutil cpu object to compute the new frequency for.
112 * @util: Current CPU utilization.
113 * @max: CPU capacity.
115 * If the utilization is frequency-invariant, choose the new frequency to be
116 * proportional to it, that is
118 * next_freq = C * max_freq * util / max
120 * Otherwise, approximate the would-be frequency-invariant utilization by
121 * util_raw * (curr_freq / max_freq) which leads to
123 * next_freq = C * curr_freq * util_raw / max
125 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
127 * The lowest driver-supported frequency which is equal or greater than the raw
128 * next_freq (as calculated above) is returned, subject to policy min/max and
129 * cpufreq driver limitations.
131 static unsigned int get_next_freq(struct sugov_cpu *sg_cpu, unsigned long util,
134 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
135 struct cpufreq_policy *policy = sg_policy->policy;
136 unsigned int freq = arch_scale_freq_invariant() ?
137 policy->cpuinfo.max_freq : policy->cur;
139 freq = (freq + (freq >> 2)) * util / max;
141 if (freq == sg_cpu->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
142 return sg_policy->next_freq;
143 sg_cpu->cached_raw_freq = freq;
144 return cpufreq_driver_resolve_freq(policy, freq);
147 static void sugov_get_util(unsigned long *util, unsigned long *max)
149 struct rq *rq = this_rq();
150 unsigned long cfs_max;
152 cfs_max = arch_scale_cpu_capacity(NULL, smp_processor_id());
154 *util = min(rq->cfs.avg.util_avg, cfs_max);
158 static void sugov_update_single(struct update_util_data *hook, u64 time,
161 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
162 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
163 struct cpufreq_policy *policy = sg_policy->policy;
164 unsigned long util, max;
167 if (!sugov_should_update_freq(sg_policy, time))
170 if (flags & SCHED_CPUFREQ_RT_DL) {
171 next_f = policy->cpuinfo.max_freq;
173 sugov_get_util(&util, &max);
174 next_f = get_next_freq(sg_cpu, util, max);
176 sugov_update_commit(sg_policy, time, next_f);
179 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu,
180 unsigned long util, unsigned long max,
183 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
184 struct cpufreq_policy *policy = sg_policy->policy;
185 unsigned int max_f = policy->cpuinfo.max_freq;
186 u64 last_freq_update_time = sg_policy->last_freq_update_time;
189 if (flags & SCHED_CPUFREQ_RT_DL)
192 for_each_cpu(j, policy->cpus) {
193 struct sugov_cpu *j_sg_cpu;
194 unsigned long j_util, j_max;
197 if (j == smp_processor_id())
200 j_sg_cpu = &per_cpu(sugov_cpu, j);
202 * If the CPU utilization was last updated before the previous
203 * frequency update and the time elapsed between the last update
204 * of the CPU utilization and the last frequency update is long
205 * enough, don't take the CPU into account as it probably is
208 delta_ns = last_freq_update_time - j_sg_cpu->last_update;
209 if (delta_ns > TICK_NSEC)
212 if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
215 j_util = j_sg_cpu->util;
216 j_max = j_sg_cpu->max;
217 if (j_util * max > j_max * util) {
223 return get_next_freq(sg_cpu, util, max);
226 static void sugov_update_shared(struct update_util_data *hook, u64 time,
229 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
230 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
231 unsigned long util, max;
234 sugov_get_util(&util, &max);
236 raw_spin_lock(&sg_policy->update_lock);
240 sg_cpu->flags = flags;
241 sg_cpu->last_update = time;
243 if (sugov_should_update_freq(sg_policy, time)) {
244 next_f = sugov_next_freq_shared(sg_cpu, util, max, flags);
245 sugov_update_commit(sg_policy, time, next_f);
248 raw_spin_unlock(&sg_policy->update_lock);
251 static void sugov_work(struct work_struct *work)
253 struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
255 mutex_lock(&sg_policy->work_lock);
256 __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
258 mutex_unlock(&sg_policy->work_lock);
260 sg_policy->work_in_progress = false;
263 static void sugov_irq_work(struct irq_work *irq_work)
265 struct sugov_policy *sg_policy;
267 sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
268 schedule_work_on(smp_processor_id(), &sg_policy->work);
271 /************************** sysfs interface ************************/
273 static struct sugov_tunables *global_tunables;
274 static DEFINE_MUTEX(global_tunables_lock);
276 static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
278 return container_of(attr_set, struct sugov_tunables, attr_set);
281 static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
283 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
285 return sprintf(buf, "%u\n", tunables->rate_limit_us);
288 static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
291 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
292 struct sugov_policy *sg_policy;
293 unsigned int rate_limit_us;
295 if (kstrtouint(buf, 10, &rate_limit_us))
298 tunables->rate_limit_us = rate_limit_us;
300 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
301 sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
306 static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
308 static struct attribute *sugov_attributes[] = {
313 static struct kobj_type sugov_tunables_ktype = {
314 .default_attrs = sugov_attributes,
315 .sysfs_ops = &governor_sysfs_ops,
318 /********************** cpufreq governor interface *********************/
320 static struct cpufreq_governor schedutil_gov;
322 static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
324 struct sugov_policy *sg_policy;
326 sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
330 sg_policy->policy = policy;
331 init_irq_work(&sg_policy->irq_work, sugov_irq_work);
332 INIT_WORK(&sg_policy->work, sugov_work);
333 mutex_init(&sg_policy->work_lock);
334 raw_spin_lock_init(&sg_policy->update_lock);
338 static void sugov_policy_free(struct sugov_policy *sg_policy)
340 mutex_destroy(&sg_policy->work_lock);
344 static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
346 struct sugov_tunables *tunables;
348 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
350 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
351 if (!have_governor_per_policy())
352 global_tunables = tunables;
357 static void sugov_tunables_free(struct sugov_tunables *tunables)
359 if (!have_governor_per_policy())
360 global_tunables = NULL;
365 static int sugov_init(struct cpufreq_policy *policy)
367 struct sugov_policy *sg_policy;
368 struct sugov_tunables *tunables;
372 /* State should be equivalent to EXIT */
373 if (policy->governor_data)
376 sg_policy = sugov_policy_alloc(policy);
380 mutex_lock(&global_tunables_lock);
382 if (global_tunables) {
383 if (WARN_ON(have_governor_per_policy())) {
387 policy->governor_data = sg_policy;
388 sg_policy->tunables = global_tunables;
390 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
394 tunables = sugov_tunables_alloc(sg_policy);
400 tunables->rate_limit_us = LATENCY_MULTIPLIER;
401 lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
403 tunables->rate_limit_us *= lat;
405 policy->governor_data = sg_policy;
406 sg_policy->tunables = tunables;
408 ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
409 get_governor_parent_kobj(policy), "%s",
415 mutex_unlock(&global_tunables_lock);
417 cpufreq_enable_fast_switch(policy);
421 policy->governor_data = NULL;
422 sugov_tunables_free(tunables);
425 mutex_unlock(&global_tunables_lock);
427 sugov_policy_free(sg_policy);
428 pr_err("initialization failed (error %d)\n", ret);
432 static void sugov_exit(struct cpufreq_policy *policy)
434 struct sugov_policy *sg_policy = policy->governor_data;
435 struct sugov_tunables *tunables = sg_policy->tunables;
438 cpufreq_disable_fast_switch(policy);
440 mutex_lock(&global_tunables_lock);
442 count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
443 policy->governor_data = NULL;
445 sugov_tunables_free(tunables);
447 mutex_unlock(&global_tunables_lock);
449 sugov_policy_free(sg_policy);
452 static int sugov_start(struct cpufreq_policy *policy)
454 struct sugov_policy *sg_policy = policy->governor_data;
457 sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
458 sg_policy->last_freq_update_time = 0;
459 sg_policy->next_freq = UINT_MAX;
460 sg_policy->work_in_progress = false;
461 sg_policy->need_freq_update = false;
463 for_each_cpu(cpu, policy->cpus) {
464 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
466 sg_cpu->sg_policy = sg_policy;
467 if (policy_is_shared(policy)) {
470 sg_cpu->flags = SCHED_CPUFREQ_RT;
471 sg_cpu->last_update = 0;
472 sg_cpu->cached_raw_freq = 0;
473 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
474 sugov_update_shared);
476 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
477 sugov_update_single);
483 static void sugov_stop(struct cpufreq_policy *policy)
485 struct sugov_policy *sg_policy = policy->governor_data;
488 for_each_cpu(cpu, policy->cpus)
489 cpufreq_remove_update_util_hook(cpu);
493 irq_work_sync(&sg_policy->irq_work);
494 cancel_work_sync(&sg_policy->work);
497 static void sugov_limits(struct cpufreq_policy *policy)
499 struct sugov_policy *sg_policy = policy->governor_data;
501 if (!policy->fast_switch_enabled) {
502 mutex_lock(&sg_policy->work_lock);
503 cpufreq_policy_apply_limits(policy);
504 mutex_unlock(&sg_policy->work_lock);
507 sg_policy->need_freq_update = true;
510 static struct cpufreq_governor schedutil_gov = {
512 .owner = THIS_MODULE,
515 .start = sugov_start,
517 .limits = sugov_limits,
520 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
521 struct cpufreq_governor *cpufreq_default_governor(void)
523 return &schedutil_gov;
527 static int __init sugov_register(void)
529 return cpufreq_register_governor(&schedutil_gov);
531 fs_initcall(sugov_register);