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 = rq->cpu_capacity_orig;
152 *util = min(rq->cfs.avg.util_avg, cfs_max);
156 static void sugov_update_single(struct update_util_data *hook, u64 time,
159 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
160 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
161 struct cpufreq_policy *policy = sg_policy->policy;
162 unsigned long util, max;
165 if (!sugov_should_update_freq(sg_policy, time))
168 if (flags & SCHED_CPUFREQ_RT_DL) {
169 next_f = policy->cpuinfo.max_freq;
171 sugov_get_util(&util, &max);
172 next_f = get_next_freq(sg_cpu, util, max);
174 sugov_update_commit(sg_policy, time, next_f);
177 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu,
178 unsigned long util, unsigned long max,
181 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
182 struct cpufreq_policy *policy = sg_policy->policy;
183 unsigned int max_f = policy->cpuinfo.max_freq;
184 u64 last_freq_update_time = sg_policy->last_freq_update_time;
187 if (flags & SCHED_CPUFREQ_RT_DL)
190 for_each_cpu(j, policy->cpus) {
191 struct sugov_cpu *j_sg_cpu;
192 unsigned long j_util, j_max;
195 if (j == smp_processor_id())
198 j_sg_cpu = &per_cpu(sugov_cpu, j);
200 * If the CPU utilization was last updated before the previous
201 * frequency update and the time elapsed between the last update
202 * of the CPU utilization and the last frequency update is long
203 * enough, don't take the CPU into account as it probably is
206 delta_ns = last_freq_update_time - j_sg_cpu->last_update;
207 if (delta_ns > TICK_NSEC)
210 if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
213 j_util = j_sg_cpu->util;
214 j_max = j_sg_cpu->max;
215 if (j_util * max > j_max * util) {
221 return get_next_freq(sg_cpu, util, max);
224 static void sugov_update_shared(struct update_util_data *hook, u64 time,
227 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
228 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
229 unsigned long util, max;
232 sugov_get_util(&util, &max);
234 raw_spin_lock(&sg_policy->update_lock);
238 sg_cpu->flags = flags;
239 sg_cpu->last_update = time;
241 if (sugov_should_update_freq(sg_policy, time)) {
242 next_f = sugov_next_freq_shared(sg_cpu, util, max, flags);
243 sugov_update_commit(sg_policy, time, next_f);
246 raw_spin_unlock(&sg_policy->update_lock);
249 static void sugov_work(struct work_struct *work)
251 struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
253 mutex_lock(&sg_policy->work_lock);
254 __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
256 mutex_unlock(&sg_policy->work_lock);
258 sg_policy->work_in_progress = false;
261 static void sugov_irq_work(struct irq_work *irq_work)
263 struct sugov_policy *sg_policy;
265 sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
266 schedule_work_on(smp_processor_id(), &sg_policy->work);
269 /************************** sysfs interface ************************/
271 static struct sugov_tunables *global_tunables;
272 static DEFINE_MUTEX(global_tunables_lock);
274 static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
276 return container_of(attr_set, struct sugov_tunables, attr_set);
279 static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
281 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
283 return sprintf(buf, "%u\n", tunables->rate_limit_us);
286 static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
289 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
290 struct sugov_policy *sg_policy;
291 unsigned int rate_limit_us;
293 if (kstrtouint(buf, 10, &rate_limit_us))
296 tunables->rate_limit_us = rate_limit_us;
298 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
299 sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
304 static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
306 static struct attribute *sugov_attributes[] = {
311 static struct kobj_type sugov_tunables_ktype = {
312 .default_attrs = sugov_attributes,
313 .sysfs_ops = &governor_sysfs_ops,
316 /********************** cpufreq governor interface *********************/
318 static struct cpufreq_governor schedutil_gov;
320 static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
322 struct sugov_policy *sg_policy;
324 sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
328 sg_policy->policy = policy;
329 init_irq_work(&sg_policy->irq_work, sugov_irq_work);
330 INIT_WORK(&sg_policy->work, sugov_work);
331 mutex_init(&sg_policy->work_lock);
332 raw_spin_lock_init(&sg_policy->update_lock);
336 static void sugov_policy_free(struct sugov_policy *sg_policy)
338 mutex_destroy(&sg_policy->work_lock);
342 static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
344 struct sugov_tunables *tunables;
346 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
348 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
349 if (!have_governor_per_policy())
350 global_tunables = tunables;
355 static void sugov_tunables_free(struct sugov_tunables *tunables)
357 if (!have_governor_per_policy())
358 global_tunables = NULL;
363 static int sugov_init(struct cpufreq_policy *policy)
365 struct sugov_policy *sg_policy;
366 struct sugov_tunables *tunables;
370 /* State should be equivalent to EXIT */
371 if (policy->governor_data)
374 sg_policy = sugov_policy_alloc(policy);
378 mutex_lock(&global_tunables_lock);
380 if (global_tunables) {
381 if (WARN_ON(have_governor_per_policy())) {
385 policy->governor_data = sg_policy;
386 sg_policy->tunables = global_tunables;
388 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
392 tunables = sugov_tunables_alloc(sg_policy);
398 tunables->rate_limit_us = LATENCY_MULTIPLIER;
399 lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
401 tunables->rate_limit_us *= lat;
403 policy->governor_data = sg_policy;
404 sg_policy->tunables = tunables;
406 ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
407 get_governor_parent_kobj(policy), "%s",
413 mutex_unlock(&global_tunables_lock);
415 cpufreq_enable_fast_switch(policy);
419 policy->governor_data = NULL;
420 sugov_tunables_free(tunables);
423 mutex_unlock(&global_tunables_lock);
425 sugov_policy_free(sg_policy);
426 pr_err("initialization failed (error %d)\n", ret);
430 static void sugov_exit(struct cpufreq_policy *policy)
432 struct sugov_policy *sg_policy = policy->governor_data;
433 struct sugov_tunables *tunables = sg_policy->tunables;
436 cpufreq_disable_fast_switch(policy);
438 mutex_lock(&global_tunables_lock);
440 count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
441 policy->governor_data = NULL;
443 sugov_tunables_free(tunables);
445 mutex_unlock(&global_tunables_lock);
447 sugov_policy_free(sg_policy);
450 static int sugov_start(struct cpufreq_policy *policy)
452 struct sugov_policy *sg_policy = policy->governor_data;
455 sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
456 sg_policy->last_freq_update_time = 0;
457 sg_policy->next_freq = UINT_MAX;
458 sg_policy->work_in_progress = false;
459 sg_policy->need_freq_update = false;
461 for_each_cpu(cpu, policy->cpus) {
462 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
464 sg_cpu->sg_policy = sg_policy;
465 if (policy_is_shared(policy)) {
468 sg_cpu->flags = SCHED_CPUFREQ_RT;
469 sg_cpu->last_update = 0;
470 sg_cpu->cached_raw_freq = 0;
471 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
472 sugov_update_shared);
474 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
475 sugov_update_single);
481 static void sugov_stop(struct cpufreq_policy *policy)
483 struct sugov_policy *sg_policy = policy->governor_data;
486 for_each_cpu(cpu, policy->cpus)
487 cpufreq_remove_update_util_hook(cpu);
491 irq_work_sync(&sg_policy->irq_work);
492 cancel_work_sync(&sg_policy->work);
495 static void sugov_limits(struct cpufreq_policy *policy)
497 struct sugov_policy *sg_policy = policy->governor_data;
499 if (!policy->fast_switch_enabled) {
500 mutex_lock(&sg_policy->work_lock);
501 cpufreq_policy_apply_limits(policy);
502 mutex_unlock(&sg_policy->work_lock);
505 sg_policy->need_freq_update = true;
508 static struct cpufreq_governor schedutil_gov = {
510 .owner = THIS_MODULE,
513 .start = sugov_start,
515 .limits = sugov_limits,
518 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
519 struct cpufreq_governor *cpufreq_default_governor(void)
521 return &schedutil_gov;
525 static int __init sugov_register(void)
527 return cpufreq_register_governor(&schedutil_gov);
529 fs_initcall(sugov_register);