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
16 #include <trace/events/power.h>
18 struct sugov_tunables {
19 struct gov_attr_set attr_set;
20 unsigned int rate_limit_us;
24 struct cpufreq_policy *policy;
26 struct sugov_tunables *tunables;
27 struct list_head tunables_hook;
29 raw_spinlock_t update_lock; /* For shared policies */
30 u64 last_freq_update_time;
31 s64 freq_update_delay_ns;
32 unsigned int next_freq;
33 unsigned int cached_raw_freq;
35 /* The next fields are only needed if fast switch cannot be used: */
36 struct irq_work irq_work;
37 struct kthread_work work;
38 struct mutex work_lock;
39 struct kthread_worker worker;
40 struct task_struct *thread;
41 bool work_in_progress;
43 bool need_freq_update;
47 struct update_util_data update_util;
48 struct sugov_policy *sg_policy;
51 bool iowait_boost_pending;
52 unsigned int iowait_boost;
53 unsigned int iowait_boost_max;
59 /* The field below is for single-CPU policies only: */
60 #ifdef CONFIG_NO_HZ_COMMON
61 unsigned long saved_idle_calls;
65 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
67 /************************ Governor internals ***********************/
69 static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
74 * Since cpufreq_update_util() is called with rq->lock held for
75 * the @target_cpu, our per-CPU data is fully serialized.
77 * However, drivers cannot in general deal with cross-CPU
78 * requests, so while get_next_freq() will work, our
79 * sugov_update_commit() call may not for the fast switching platforms.
81 * Hence stop here for remote requests if they aren't supported
82 * by the hardware, as calculating the frequency is pointless if
83 * we cannot in fact act on it.
85 * For the slow switching platforms, the kthread is always scheduled on
86 * the right set of CPUs and any CPU can find the next frequency and
87 * schedule the kthread.
89 if (sg_policy->policy->fast_switch_enabled &&
90 !cpufreq_this_cpu_can_update(sg_policy->policy))
93 if (unlikely(sg_policy->need_freq_update))
96 delta_ns = time - sg_policy->last_freq_update_time;
98 return delta_ns >= sg_policy->freq_update_delay_ns;
101 static bool sugov_update_next_freq(struct sugov_policy *sg_policy, u64 time,
102 unsigned int next_freq)
104 if (sg_policy->next_freq == next_freq)
107 sg_policy->next_freq = next_freq;
108 sg_policy->last_freq_update_time = time;
113 static void sugov_fast_switch(struct sugov_policy *sg_policy, u64 time,
114 unsigned int next_freq)
116 struct cpufreq_policy *policy = sg_policy->policy;
118 if (!sugov_update_next_freq(sg_policy, time, next_freq))
121 next_freq = cpufreq_driver_fast_switch(policy, next_freq);
125 policy->cur = next_freq;
126 trace_cpu_frequency(next_freq, smp_processor_id());
129 static void sugov_deferred_update(struct sugov_policy *sg_policy, u64 time,
130 unsigned int next_freq)
132 if (!sugov_update_next_freq(sg_policy, time, next_freq))
135 if (!sg_policy->work_in_progress) {
136 sg_policy->work_in_progress = true;
137 irq_work_queue(&sg_policy->irq_work);
142 * get_next_freq - Compute a new frequency for a given cpufreq policy.
143 * @sg_policy: schedutil policy object to compute the new frequency for.
144 * @util: Current CPU utilization.
145 * @max: CPU capacity.
147 * If the utilization is frequency-invariant, choose the new frequency to be
148 * proportional to it, that is
150 * next_freq = C * max_freq * util / max
152 * Otherwise, approximate the would-be frequency-invariant utilization by
153 * util_raw * (curr_freq / max_freq) which leads to
155 * next_freq = C * curr_freq * util_raw / max
157 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
159 * The lowest driver-supported frequency which is equal or greater than the raw
160 * next_freq (as calculated above) is returned, subject to policy min/max and
161 * cpufreq driver limitations.
163 static unsigned int get_next_freq(struct sugov_policy *sg_policy,
164 unsigned long util, unsigned long max)
166 struct cpufreq_policy *policy = sg_policy->policy;
167 unsigned int freq = arch_scale_freq_invariant() ?
168 policy->cpuinfo.max_freq : policy->cur;
170 freq = (freq + (freq >> 2)) * util / max;
172 if (freq == sg_policy->cached_raw_freq && !sg_policy->need_freq_update)
173 return sg_policy->next_freq;
175 sg_policy->need_freq_update = false;
176 sg_policy->cached_raw_freq = freq;
177 return cpufreq_driver_resolve_freq(policy, freq);
181 * This function computes an effective utilization for the given CPU, to be
182 * used for frequency selection given the linear relation: f = u * f_max.
184 * The scheduler tracks the following metrics:
186 * cpu_util_{cfs,rt,dl,irq}()
189 * Where the cfs,rt and dl util numbers are tracked with the same metric and
190 * synchronized windows and are thus directly comparable.
192 * The cfs,rt,dl utilization are the running times measured with rq->clock_task
193 * which excludes things like IRQ and steal-time. These latter are then accrued
194 * in the irq utilization.
196 * The DL bandwidth number otoh is not a measured metric but a value computed
197 * based on the task model parameters and gives the minimal utilization
198 * required to meet deadlines.
200 static unsigned long sugov_get_util(struct sugov_cpu *sg_cpu)
202 struct rq *rq = cpu_rq(sg_cpu->cpu);
203 unsigned long util, irq, max;
205 sg_cpu->max = max = arch_scale_cpu_capacity(NULL, sg_cpu->cpu);
206 sg_cpu->bw_dl = cpu_bw_dl(rq);
208 if (rt_rq_is_runnable(&rq->rt))
212 * Early check to see if IRQ/steal time saturates the CPU, can be
213 * because of inaccuracies in how we track these -- see
214 * update_irq_load_avg().
216 irq = cpu_util_irq(rq);
217 if (unlikely(irq >= max))
221 * Because the time spend on RT/DL tasks is visible as 'lost' time to
222 * CFS tasks and we use the same metric to track the effective
223 * utilization (PELT windows are synchronized) we can directly add them
224 * to obtain the CPU's actual utilization.
226 util = cpu_util_cfs(rq);
227 util += cpu_util_rt(rq);
230 * We do not make cpu_util_dl() a permanent part of this sum because we
231 * want to use cpu_bw_dl() later on, but we need to check if the
232 * CFS+RT+DL sum is saturated (ie. no idle time) such that we select
233 * f_max when there is no idle time.
235 * NOTE: numerical errors or stop class might cause us to not quite hit
236 * saturation when we should -- something for later.
238 if ((util + cpu_util_dl(rq)) >= max)
242 * There is still idle time; further improve the number by using the
243 * irq metric. Because IRQ/steal time is hidden from the task clock we
244 * need to scale the task numbers:
247 * U' = irq + ------- * U
250 util = scale_irq_capacity(util, irq, max);
254 * Bandwidth required by DEADLINE must always be granted while, for
255 * FAIR and RT, we use blocked utilization of IDLE CPUs as a mechanism
256 * to gracefully reduce the frequency when no tasks show up for longer
259 * Ideally we would like to set bw_dl as min/guaranteed freq and util +
260 * bw_dl as requested freq. However, cpufreq is not yet ready for such
261 * an interface. So, we only do the latter for now.
263 return min(max, util + sg_cpu->bw_dl);
267 * sugov_iowait_reset() - Reset the IO boost status of a CPU.
268 * @sg_cpu: the sugov data for the CPU to boost
269 * @time: the update time from the caller
270 * @set_iowait_boost: true if an IO boost has been requested
272 * The IO wait boost of a task is disabled after a tick since the last update
273 * of a CPU. If a new IO wait boost is requested after more then a tick, then
274 * we enable the boost starting from the minimum frequency, which improves
275 * energy efficiency by ignoring sporadic wakeups from IO.
277 static bool sugov_iowait_reset(struct sugov_cpu *sg_cpu, u64 time,
278 bool set_iowait_boost)
280 s64 delta_ns = time - sg_cpu->last_update;
282 /* Reset boost only if a tick has elapsed since last request */
283 if (delta_ns <= TICK_NSEC)
286 sg_cpu->iowait_boost = set_iowait_boost
287 ? sg_cpu->sg_policy->policy->min : 0;
288 sg_cpu->iowait_boost_pending = set_iowait_boost;
294 * sugov_iowait_boost() - Updates the IO boost status of a CPU.
295 * @sg_cpu: the sugov data for the CPU to boost
296 * @time: the update time from the caller
297 * @flags: SCHED_CPUFREQ_IOWAIT if the task is waking up after an IO wait
299 * Each time a task wakes up after an IO operation, the CPU utilization can be
300 * boosted to a certain utilization which doubles at each "frequent and
301 * successive" wakeup from IO, ranging from the utilization of the minimum
302 * OPP to the utilization of the maximum OPP.
303 * To keep doubling, an IO boost has to be requested at least once per tick,
304 * otherwise we restart from the utilization of the minimum OPP.
306 static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
309 bool set_iowait_boost = flags & SCHED_CPUFREQ_IOWAIT;
311 /* Reset boost if the CPU appears to have been idle enough */
312 if (sg_cpu->iowait_boost &&
313 sugov_iowait_reset(sg_cpu, time, set_iowait_boost))
316 /* Boost only tasks waking up after IO */
317 if (!set_iowait_boost)
320 /* Ensure boost doubles only one time at each request */
321 if (sg_cpu->iowait_boost_pending)
323 sg_cpu->iowait_boost_pending = true;
325 /* Double the boost at each request */
326 if (sg_cpu->iowait_boost) {
327 sg_cpu->iowait_boost <<= 1;
328 if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max)
329 sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
333 /* First wakeup after IO: start with minimum boost */
334 sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min;
338 * sugov_iowait_apply() - Apply the IO boost to a CPU.
339 * @sg_cpu: the sugov data for the cpu to boost
340 * @time: the update time from the caller
341 * @util: the utilization to (eventually) boost
342 * @max: the maximum value the utilization can be boosted to
344 * A CPU running a task which woken up after an IO operation can have its
345 * utilization boosted to speed up the completion of those IO operations.
346 * The IO boost value is increased each time a task wakes up from IO, in
347 * sugov_iowait_apply(), and it's instead decreased by this function,
348 * each time an increase has not been requested (!iowait_boost_pending).
350 * A CPU which also appears to have been idle for at least one tick has also
351 * its IO boost utilization reset.
353 * This mechanism is designed to boost high frequently IO waiting tasks, while
354 * being more conservative on tasks which does sporadic IO operations.
356 static void sugov_iowait_apply(struct sugov_cpu *sg_cpu, u64 time,
357 unsigned long *util, unsigned long *max)
359 unsigned int boost_util, boost_max;
361 /* No boost currently required */
362 if (!sg_cpu->iowait_boost)
365 /* Reset boost if the CPU appears to have been idle enough */
366 if (sugov_iowait_reset(sg_cpu, time, false))
370 * An IO waiting task has just woken up:
371 * allow to further double the boost value
373 if (sg_cpu->iowait_boost_pending) {
374 sg_cpu->iowait_boost_pending = false;
377 * Otherwise: reduce the boost value and disable it when we
380 sg_cpu->iowait_boost >>= 1;
381 if (sg_cpu->iowait_boost < sg_cpu->sg_policy->policy->min) {
382 sg_cpu->iowait_boost = 0;
388 * Apply the current boost value: a CPU is boosted only if its current
389 * utilization is smaller then the current IO boost level.
391 boost_util = sg_cpu->iowait_boost;
392 boost_max = sg_cpu->iowait_boost_max;
393 if (*util * boost_max < *max * boost_util) {
399 #ifdef CONFIG_NO_HZ_COMMON
400 static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
402 unsigned long idle_calls = tick_nohz_get_idle_calls_cpu(sg_cpu->cpu);
403 bool ret = idle_calls == sg_cpu->saved_idle_calls;
405 sg_cpu->saved_idle_calls = idle_calls;
409 static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
410 #endif /* CONFIG_NO_HZ_COMMON */
413 * Make sugov_should_update_freq() ignore the rate limit when DL
414 * has increased the utilization.
416 static inline void ignore_dl_rate_limit(struct sugov_cpu *sg_cpu, struct sugov_policy *sg_policy)
418 if (cpu_bw_dl(cpu_rq(sg_cpu->cpu)) > sg_cpu->bw_dl)
419 sg_policy->need_freq_update = true;
422 static void sugov_update_single(struct update_util_data *hook, u64 time,
425 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
426 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
427 unsigned long util, max;
431 sugov_iowait_boost(sg_cpu, time, flags);
432 sg_cpu->last_update = time;
434 ignore_dl_rate_limit(sg_cpu, sg_policy);
436 if (!sugov_should_update_freq(sg_policy, time))
439 busy = sugov_cpu_is_busy(sg_cpu);
441 util = sugov_get_util(sg_cpu);
443 sugov_iowait_apply(sg_cpu, time, &util, &max);
444 next_f = get_next_freq(sg_policy, util, max);
446 * Do not reduce the frequency if the CPU has not been idle
447 * recently, as the reduction is likely to be premature then.
449 if (busy && next_f < sg_policy->next_freq) {
450 next_f = sg_policy->next_freq;
452 /* Reset cached freq as next_freq has changed */
453 sg_policy->cached_raw_freq = 0;
457 * This code runs under rq->lock for the target CPU, so it won't run
458 * concurrently on two different CPUs for the same target and it is not
459 * necessary to acquire the lock in the fast switch case.
461 if (sg_policy->policy->fast_switch_enabled) {
462 sugov_fast_switch(sg_policy, time, next_f);
464 raw_spin_lock(&sg_policy->update_lock);
465 sugov_deferred_update(sg_policy, time, next_f);
466 raw_spin_unlock(&sg_policy->update_lock);
470 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
472 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
473 struct cpufreq_policy *policy = sg_policy->policy;
474 unsigned long util = 0, max = 1;
477 for_each_cpu(j, policy->cpus) {
478 struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
479 unsigned long j_util, j_max;
481 j_util = sugov_get_util(j_sg_cpu);
482 j_max = j_sg_cpu->max;
483 sugov_iowait_apply(j_sg_cpu, time, &j_util, &j_max);
485 if (j_util * max > j_max * util) {
491 return get_next_freq(sg_policy, util, max);
495 sugov_update_shared(struct update_util_data *hook, u64 time, unsigned int flags)
497 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
498 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
501 raw_spin_lock(&sg_policy->update_lock);
503 sugov_iowait_boost(sg_cpu, time, flags);
504 sg_cpu->last_update = time;
506 ignore_dl_rate_limit(sg_cpu, sg_policy);
508 if (sugov_should_update_freq(sg_policy, time)) {
509 next_f = sugov_next_freq_shared(sg_cpu, time);
511 if (sg_policy->policy->fast_switch_enabled)
512 sugov_fast_switch(sg_policy, time, next_f);
514 sugov_deferred_update(sg_policy, time, next_f);
517 raw_spin_unlock(&sg_policy->update_lock);
520 static void sugov_work(struct kthread_work *work)
522 struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
527 * Hold sg_policy->update_lock shortly to handle the case where:
528 * incase sg_policy->next_freq is read here, and then updated by
529 * sugov_deferred_update() just before work_in_progress is set to false
530 * here, we may miss queueing the new update.
532 * Note: If a work was queued after the update_lock is released,
533 * sugov_work() will just be called again by kthread_work code; and the
534 * request will be proceed before the sugov thread sleeps.
536 raw_spin_lock_irqsave(&sg_policy->update_lock, flags);
537 freq = sg_policy->next_freq;
538 sg_policy->work_in_progress = false;
539 raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags);
541 mutex_lock(&sg_policy->work_lock);
542 __cpufreq_driver_target(sg_policy->policy, freq, CPUFREQ_RELATION_L);
543 mutex_unlock(&sg_policy->work_lock);
546 static void sugov_irq_work(struct irq_work *irq_work)
548 struct sugov_policy *sg_policy;
550 sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
552 kthread_queue_work(&sg_policy->worker, &sg_policy->work);
555 /************************** sysfs interface ************************/
557 static struct sugov_tunables *global_tunables;
558 static DEFINE_MUTEX(global_tunables_lock);
560 static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
562 return container_of(attr_set, struct sugov_tunables, attr_set);
565 static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
567 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
569 return sprintf(buf, "%u\n", tunables->rate_limit_us);
573 rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf, size_t count)
575 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
576 struct sugov_policy *sg_policy;
577 unsigned int rate_limit_us;
579 if (kstrtouint(buf, 10, &rate_limit_us))
582 tunables->rate_limit_us = rate_limit_us;
584 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
585 sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
590 static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
592 static struct attribute *sugov_attributes[] = {
597 static struct kobj_type sugov_tunables_ktype = {
598 .default_attrs = sugov_attributes,
599 .sysfs_ops = &governor_sysfs_ops,
602 /********************** cpufreq governor interface *********************/
604 static struct cpufreq_governor schedutil_gov;
606 static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
608 struct sugov_policy *sg_policy;
610 sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
614 sg_policy->policy = policy;
615 raw_spin_lock_init(&sg_policy->update_lock);
619 static void sugov_policy_free(struct sugov_policy *sg_policy)
624 static int sugov_kthread_create(struct sugov_policy *sg_policy)
626 struct task_struct *thread;
627 struct sched_attr attr = {
628 .size = sizeof(struct sched_attr),
629 .sched_policy = SCHED_DEADLINE,
630 .sched_flags = SCHED_FLAG_SUGOV,
634 * Fake (unused) bandwidth; workaround to "fix"
635 * priority inheritance.
637 .sched_runtime = 1000000,
638 .sched_deadline = 10000000,
639 .sched_period = 10000000,
641 struct cpufreq_policy *policy = sg_policy->policy;
644 /* kthread only required for slow path */
645 if (policy->fast_switch_enabled)
648 kthread_init_work(&sg_policy->work, sugov_work);
649 kthread_init_worker(&sg_policy->worker);
650 thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
652 cpumask_first(policy->related_cpus));
653 if (IS_ERR(thread)) {
654 pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
655 return PTR_ERR(thread);
658 ret = sched_setattr_nocheck(thread, &attr);
660 kthread_stop(thread);
661 pr_warn("%s: failed to set SCHED_DEADLINE\n", __func__);
665 sg_policy->thread = thread;
666 kthread_bind_mask(thread, policy->related_cpus);
667 init_irq_work(&sg_policy->irq_work, sugov_irq_work);
668 mutex_init(&sg_policy->work_lock);
670 wake_up_process(thread);
675 static void sugov_kthread_stop(struct sugov_policy *sg_policy)
677 /* kthread only required for slow path */
678 if (sg_policy->policy->fast_switch_enabled)
681 kthread_flush_worker(&sg_policy->worker);
682 kthread_stop(sg_policy->thread);
683 mutex_destroy(&sg_policy->work_lock);
686 static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
688 struct sugov_tunables *tunables;
690 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
692 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
693 if (!have_governor_per_policy())
694 global_tunables = tunables;
699 static void sugov_tunables_free(struct sugov_tunables *tunables)
701 if (!have_governor_per_policy())
702 global_tunables = NULL;
707 static int sugov_init(struct cpufreq_policy *policy)
709 struct sugov_policy *sg_policy;
710 struct sugov_tunables *tunables;
713 /* State should be equivalent to EXIT */
714 if (policy->governor_data)
717 cpufreq_enable_fast_switch(policy);
719 sg_policy = sugov_policy_alloc(policy);
722 goto disable_fast_switch;
725 ret = sugov_kthread_create(sg_policy);
729 mutex_lock(&global_tunables_lock);
731 if (global_tunables) {
732 if (WARN_ON(have_governor_per_policy())) {
736 policy->governor_data = sg_policy;
737 sg_policy->tunables = global_tunables;
739 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
743 tunables = sugov_tunables_alloc(sg_policy);
749 tunables->rate_limit_us = cpufreq_policy_transition_delay_us(policy);
751 policy->governor_data = sg_policy;
752 sg_policy->tunables = tunables;
754 ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
755 get_governor_parent_kobj(policy), "%s",
761 mutex_unlock(&global_tunables_lock);
765 policy->governor_data = NULL;
766 sugov_tunables_free(tunables);
769 sugov_kthread_stop(sg_policy);
770 mutex_unlock(&global_tunables_lock);
773 sugov_policy_free(sg_policy);
776 cpufreq_disable_fast_switch(policy);
778 pr_err("initialization failed (error %d)\n", ret);
782 static void sugov_exit(struct cpufreq_policy *policy)
784 struct sugov_policy *sg_policy = policy->governor_data;
785 struct sugov_tunables *tunables = sg_policy->tunables;
788 mutex_lock(&global_tunables_lock);
790 count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
791 policy->governor_data = NULL;
793 sugov_tunables_free(tunables);
795 mutex_unlock(&global_tunables_lock);
797 sugov_kthread_stop(sg_policy);
798 sugov_policy_free(sg_policy);
799 cpufreq_disable_fast_switch(policy);
802 static int sugov_start(struct cpufreq_policy *policy)
804 struct sugov_policy *sg_policy = policy->governor_data;
807 sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
808 sg_policy->last_freq_update_time = 0;
809 sg_policy->next_freq = 0;
810 sg_policy->work_in_progress = false;
811 sg_policy->need_freq_update = false;
812 sg_policy->cached_raw_freq = 0;
814 for_each_cpu(cpu, policy->cpus) {
815 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
817 memset(sg_cpu, 0, sizeof(*sg_cpu));
819 sg_cpu->sg_policy = sg_policy;
820 sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
823 for_each_cpu(cpu, policy->cpus) {
824 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
826 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
827 policy_is_shared(policy) ?
828 sugov_update_shared :
829 sugov_update_single);
834 static void sugov_stop(struct cpufreq_policy *policy)
836 struct sugov_policy *sg_policy = policy->governor_data;
839 for_each_cpu(cpu, policy->cpus)
840 cpufreq_remove_update_util_hook(cpu);
844 if (!policy->fast_switch_enabled) {
845 irq_work_sync(&sg_policy->irq_work);
846 kthread_cancel_work_sync(&sg_policy->work);
850 static void sugov_limits(struct cpufreq_policy *policy)
852 struct sugov_policy *sg_policy = policy->governor_data;
854 if (!policy->fast_switch_enabled) {
855 mutex_lock(&sg_policy->work_lock);
856 cpufreq_policy_apply_limits(policy);
857 mutex_unlock(&sg_policy->work_lock);
860 sg_policy->need_freq_update = true;
863 static struct cpufreq_governor schedutil_gov = {
865 .owner = THIS_MODULE,
866 .dynamic_switching = true,
869 .start = sugov_start,
871 .limits = sugov_limits,
874 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
875 struct cpufreq_governor *cpufreq_default_governor(void)
877 return &schedutil_gov;
881 static int __init sugov_register(void)
883 return cpufreq_register_governor(&schedutil_gov);
885 fs_initcall(sugov_register);