ARM: dts: stm32: add gadget fifo sizes to usbotg_hs on stm32f746
[linux/fpc-iii.git] / kernel / sched / cpufreq_schedutil.c
blobdd062a1c8cf043a26882e3b694d7c15588a2718c
1 /*
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/kthread.h>
16 #include <uapi/linux/sched/types.h>
17 #include <linux/slab.h>
18 #include <trace/events/power.h>
20 #include "sched.h"
22 #define SUGOV_KTHREAD_PRIORITY 50
24 struct sugov_tunables {
25 struct gov_attr_set attr_set;
26 unsigned int rate_limit_us;
29 struct sugov_policy {
30 struct cpufreq_policy *policy;
32 struct sugov_tunables *tunables;
33 struct list_head tunables_hook;
35 raw_spinlock_t update_lock; /* For shared policies */
36 u64 last_freq_update_time;
37 s64 freq_update_delay_ns;
38 unsigned int next_freq;
39 unsigned int cached_raw_freq;
41 /* The next fields are only needed if fast switch cannot be used. */
42 struct irq_work irq_work;
43 struct kthread_work work;
44 struct mutex work_lock;
45 struct kthread_worker worker;
46 struct task_struct *thread;
47 bool work_in_progress;
49 bool need_freq_update;
52 struct sugov_cpu {
53 struct update_util_data update_util;
54 struct sugov_policy *sg_policy;
55 unsigned int cpu;
57 bool iowait_boost_pending;
58 unsigned int iowait_boost;
59 unsigned int iowait_boost_max;
60 u64 last_update;
62 /* The fields below are only needed when sharing a policy. */
63 unsigned long util_cfs;
64 unsigned long util_dl;
65 unsigned long max;
66 unsigned int flags;
68 /* The field below is for single-CPU policies only. */
69 #ifdef CONFIG_NO_HZ_COMMON
70 unsigned long saved_idle_calls;
71 #endif
74 static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
76 /************************ Governor internals ***********************/
78 static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
80 s64 delta_ns;
83 * Since cpufreq_update_util() is called with rq->lock held for
84 * the @target_cpu, our per-cpu data is fully serialized.
86 * However, drivers cannot in general deal with cross-cpu
87 * requests, so while get_next_freq() will work, our
88 * sugov_update_commit() call may not for the fast switching platforms.
90 * Hence stop here for remote requests if they aren't supported
91 * by the hardware, as calculating the frequency is pointless if
92 * we cannot in fact act on it.
94 * For the slow switching platforms, the kthread is always scheduled on
95 * the right set of CPUs and any CPU can find the next frequency and
96 * schedule the kthread.
98 if (sg_policy->policy->fast_switch_enabled &&
99 !cpufreq_can_do_remote_dvfs(sg_policy->policy))
100 return false;
102 if (sg_policy->work_in_progress)
103 return false;
105 if (unlikely(sg_policy->need_freq_update)) {
106 sg_policy->need_freq_update = false;
108 * This happens when limits change, so forget the previous
109 * next_freq value and force an update.
111 sg_policy->next_freq = UINT_MAX;
112 return true;
115 delta_ns = time - sg_policy->last_freq_update_time;
116 return delta_ns >= sg_policy->freq_update_delay_ns;
119 static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
120 unsigned int next_freq)
122 struct cpufreq_policy *policy = sg_policy->policy;
124 if (sg_policy->next_freq == next_freq)
125 return;
127 sg_policy->next_freq = next_freq;
128 sg_policy->last_freq_update_time = time;
130 if (policy->fast_switch_enabled) {
131 next_freq = cpufreq_driver_fast_switch(policy, next_freq);
132 if (!next_freq)
133 return;
135 policy->cur = next_freq;
136 trace_cpu_frequency(next_freq, smp_processor_id());
137 } else {
138 sg_policy->work_in_progress = true;
139 irq_work_queue(&sg_policy->irq_work);
144 * get_next_freq - Compute a new frequency for a given cpufreq policy.
145 * @sg_policy: schedutil policy object to compute the new frequency for.
146 * @util: Current CPU utilization.
147 * @max: CPU capacity.
149 * If the utilization is frequency-invariant, choose the new frequency to be
150 * proportional to it, that is
152 * next_freq = C * max_freq * util / max
154 * Otherwise, approximate the would-be frequency-invariant utilization by
155 * util_raw * (curr_freq / max_freq) which leads to
157 * next_freq = C * curr_freq * util_raw / max
159 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
161 * The lowest driver-supported frequency which is equal or greater than the raw
162 * next_freq (as calculated above) is returned, subject to policy min/max and
163 * cpufreq driver limitations.
165 static unsigned int get_next_freq(struct sugov_policy *sg_policy,
166 unsigned long util, unsigned long max)
168 struct cpufreq_policy *policy = sg_policy->policy;
169 unsigned int freq = arch_scale_freq_invariant() ?
170 policy->cpuinfo.max_freq : policy->cur;
172 freq = (freq + (freq >> 2)) * util / max;
174 if (freq == sg_policy->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
175 return sg_policy->next_freq;
176 sg_policy->cached_raw_freq = freq;
177 return cpufreq_driver_resolve_freq(policy, freq);
180 static void sugov_get_util(struct sugov_cpu *sg_cpu)
182 struct rq *rq = cpu_rq(sg_cpu->cpu);
184 sg_cpu->max = arch_scale_cpu_capacity(NULL, sg_cpu->cpu);
185 sg_cpu->util_cfs = cpu_util_cfs(rq);
186 sg_cpu->util_dl = cpu_util_dl(rq);
189 static unsigned long sugov_aggregate_util(struct sugov_cpu *sg_cpu)
192 * Ideally we would like to set util_dl as min/guaranteed freq and
193 * util_cfs + util_dl as requested freq. However, cpufreq is not yet
194 * ready for such an interface. So, we only do the latter for now.
196 return min(sg_cpu->util_cfs + sg_cpu->util_dl, sg_cpu->max);
199 static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time)
201 if (sg_cpu->flags & SCHED_CPUFREQ_IOWAIT) {
202 if (sg_cpu->iowait_boost_pending)
203 return;
205 sg_cpu->iowait_boost_pending = true;
207 if (sg_cpu->iowait_boost) {
208 sg_cpu->iowait_boost <<= 1;
209 if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max)
210 sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
211 } else {
212 sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min;
214 } else if (sg_cpu->iowait_boost) {
215 s64 delta_ns = time - sg_cpu->last_update;
217 /* Clear iowait_boost if the CPU apprears to have been idle. */
218 if (delta_ns > TICK_NSEC) {
219 sg_cpu->iowait_boost = 0;
220 sg_cpu->iowait_boost_pending = false;
225 static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util,
226 unsigned long *max)
228 unsigned int boost_util, boost_max;
230 if (!sg_cpu->iowait_boost)
231 return;
233 if (sg_cpu->iowait_boost_pending) {
234 sg_cpu->iowait_boost_pending = false;
235 } else {
236 sg_cpu->iowait_boost >>= 1;
237 if (sg_cpu->iowait_boost < sg_cpu->sg_policy->policy->min) {
238 sg_cpu->iowait_boost = 0;
239 return;
243 boost_util = sg_cpu->iowait_boost;
244 boost_max = sg_cpu->iowait_boost_max;
246 if (*util * boost_max < *max * boost_util) {
247 *util = boost_util;
248 *max = boost_max;
252 #ifdef CONFIG_NO_HZ_COMMON
253 static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
255 unsigned long idle_calls = tick_nohz_get_idle_calls_cpu(sg_cpu->cpu);
256 bool ret = idle_calls == sg_cpu->saved_idle_calls;
258 sg_cpu->saved_idle_calls = idle_calls;
259 return ret;
261 #else
262 static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
263 #endif /* CONFIG_NO_HZ_COMMON */
265 static void sugov_update_single(struct update_util_data *hook, u64 time,
266 unsigned int flags)
268 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
269 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
270 struct cpufreq_policy *policy = sg_policy->policy;
271 unsigned long util, max;
272 unsigned int next_f;
273 bool busy;
275 sugov_set_iowait_boost(sg_cpu, time);
276 sg_cpu->last_update = time;
278 if (!sugov_should_update_freq(sg_policy, time))
279 return;
281 busy = sugov_cpu_is_busy(sg_cpu);
283 if (flags & SCHED_CPUFREQ_RT) {
284 next_f = policy->cpuinfo.max_freq;
285 } else {
286 sugov_get_util(sg_cpu);
287 max = sg_cpu->max;
288 util = sugov_aggregate_util(sg_cpu);
289 sugov_iowait_boost(sg_cpu, &util, &max);
290 next_f = get_next_freq(sg_policy, util, max);
292 * Do not reduce the frequency if the CPU has not been idle
293 * recently, as the reduction is likely to be premature then.
295 if (busy && next_f < sg_policy->next_freq) {
296 next_f = sg_policy->next_freq;
298 /* Reset cached freq as next_freq has changed */
299 sg_policy->cached_raw_freq = 0;
302 sugov_update_commit(sg_policy, time, next_f);
305 static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time)
307 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
308 struct cpufreq_policy *policy = sg_policy->policy;
309 unsigned long util = 0, max = 1;
310 unsigned int j;
312 for_each_cpu(j, policy->cpus) {
313 struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
314 unsigned long j_util, j_max;
315 s64 delta_ns;
318 * If the CFS CPU utilization was last updated before the
319 * previous frequency update and the time elapsed between the
320 * last update of the CPU utilization and the last frequency
321 * update is long enough, reset iowait_boost and util_cfs, as
322 * they are now probably stale. However, still consider the
323 * CPU contribution if it has some DEADLINE utilization
324 * (util_dl).
326 delta_ns = time - j_sg_cpu->last_update;
327 if (delta_ns > TICK_NSEC) {
328 j_sg_cpu->iowait_boost = 0;
329 j_sg_cpu->iowait_boost_pending = false;
330 j_sg_cpu->util_cfs = 0;
331 if (j_sg_cpu->util_dl == 0)
332 continue;
334 if (j_sg_cpu->flags & SCHED_CPUFREQ_RT)
335 return policy->cpuinfo.max_freq;
337 j_max = j_sg_cpu->max;
338 j_util = sugov_aggregate_util(j_sg_cpu);
339 if (j_util * max > j_max * util) {
340 util = j_util;
341 max = j_max;
344 sugov_iowait_boost(j_sg_cpu, &util, &max);
347 return get_next_freq(sg_policy, util, max);
350 static void sugov_update_shared(struct update_util_data *hook, u64 time,
351 unsigned int flags)
353 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
354 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
355 unsigned int next_f;
357 raw_spin_lock(&sg_policy->update_lock);
359 sugov_get_util(sg_cpu);
360 sg_cpu->flags = flags;
362 sugov_set_iowait_boost(sg_cpu, time);
363 sg_cpu->last_update = time;
365 if (sugov_should_update_freq(sg_policy, time)) {
366 if (flags & SCHED_CPUFREQ_RT)
367 next_f = sg_policy->policy->cpuinfo.max_freq;
368 else
369 next_f = sugov_next_freq_shared(sg_cpu, time);
371 sugov_update_commit(sg_policy, time, next_f);
374 raw_spin_unlock(&sg_policy->update_lock);
377 static void sugov_work(struct kthread_work *work)
379 struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
381 mutex_lock(&sg_policy->work_lock);
382 __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
383 CPUFREQ_RELATION_L);
384 mutex_unlock(&sg_policy->work_lock);
386 sg_policy->work_in_progress = false;
389 static void sugov_irq_work(struct irq_work *irq_work)
391 struct sugov_policy *sg_policy;
393 sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
396 * For RT tasks, the schedutil governor shoots the frequency to maximum.
397 * Special care must be taken to ensure that this kthread doesn't result
398 * in the same behavior.
400 * This is (mostly) guaranteed by the work_in_progress flag. The flag is
401 * updated only at the end of the sugov_work() function and before that
402 * the schedutil governor rejects all other frequency scaling requests.
404 * There is a very rare case though, where the RT thread yields right
405 * after the work_in_progress flag is cleared. The effects of that are
406 * neglected for now.
408 kthread_queue_work(&sg_policy->worker, &sg_policy->work);
411 /************************** sysfs interface ************************/
413 static struct sugov_tunables *global_tunables;
414 static DEFINE_MUTEX(global_tunables_lock);
416 static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
418 return container_of(attr_set, struct sugov_tunables, attr_set);
421 static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
423 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
425 return sprintf(buf, "%u\n", tunables->rate_limit_us);
428 static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
429 size_t count)
431 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
432 struct sugov_policy *sg_policy;
433 unsigned int rate_limit_us;
435 if (kstrtouint(buf, 10, &rate_limit_us))
436 return -EINVAL;
438 tunables->rate_limit_us = rate_limit_us;
440 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
441 sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
443 return count;
446 static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
448 static struct attribute *sugov_attributes[] = {
449 &rate_limit_us.attr,
450 NULL
453 static struct kobj_type sugov_tunables_ktype = {
454 .default_attrs = sugov_attributes,
455 .sysfs_ops = &governor_sysfs_ops,
458 /********************** cpufreq governor interface *********************/
460 static struct cpufreq_governor schedutil_gov;
462 static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
464 struct sugov_policy *sg_policy;
466 sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
467 if (!sg_policy)
468 return NULL;
470 sg_policy->policy = policy;
471 raw_spin_lock_init(&sg_policy->update_lock);
472 return sg_policy;
475 static void sugov_policy_free(struct sugov_policy *sg_policy)
477 kfree(sg_policy);
480 static int sugov_kthread_create(struct sugov_policy *sg_policy)
482 struct task_struct *thread;
483 struct sched_attr attr = {
484 .size = sizeof(struct sched_attr),
485 .sched_policy = SCHED_DEADLINE,
486 .sched_flags = SCHED_FLAG_SUGOV,
487 .sched_nice = 0,
488 .sched_priority = 0,
490 * Fake (unused) bandwidth; workaround to "fix"
491 * priority inheritance.
493 .sched_runtime = 1000000,
494 .sched_deadline = 10000000,
495 .sched_period = 10000000,
497 struct cpufreq_policy *policy = sg_policy->policy;
498 int ret;
500 /* kthread only required for slow path */
501 if (policy->fast_switch_enabled)
502 return 0;
504 kthread_init_work(&sg_policy->work, sugov_work);
505 kthread_init_worker(&sg_policy->worker);
506 thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
507 "sugov:%d",
508 cpumask_first(policy->related_cpus));
509 if (IS_ERR(thread)) {
510 pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
511 return PTR_ERR(thread);
514 ret = sched_setattr_nocheck(thread, &attr);
515 if (ret) {
516 kthread_stop(thread);
517 pr_warn("%s: failed to set SCHED_DEADLINE\n", __func__);
518 return ret;
521 sg_policy->thread = thread;
523 /* Kthread is bound to all CPUs by default */
524 if (!policy->dvfs_possible_from_any_cpu)
525 kthread_bind_mask(thread, policy->related_cpus);
527 init_irq_work(&sg_policy->irq_work, sugov_irq_work);
528 mutex_init(&sg_policy->work_lock);
530 wake_up_process(thread);
532 return 0;
535 static void sugov_kthread_stop(struct sugov_policy *sg_policy)
537 /* kthread only required for slow path */
538 if (sg_policy->policy->fast_switch_enabled)
539 return;
541 kthread_flush_worker(&sg_policy->worker);
542 kthread_stop(sg_policy->thread);
543 mutex_destroy(&sg_policy->work_lock);
546 static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
548 struct sugov_tunables *tunables;
550 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
551 if (tunables) {
552 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
553 if (!have_governor_per_policy())
554 global_tunables = tunables;
556 return tunables;
559 static void sugov_tunables_free(struct sugov_tunables *tunables)
561 if (!have_governor_per_policy())
562 global_tunables = NULL;
564 kfree(tunables);
567 static int sugov_init(struct cpufreq_policy *policy)
569 struct sugov_policy *sg_policy;
570 struct sugov_tunables *tunables;
571 int ret = 0;
573 /* State should be equivalent to EXIT */
574 if (policy->governor_data)
575 return -EBUSY;
577 cpufreq_enable_fast_switch(policy);
579 sg_policy = sugov_policy_alloc(policy);
580 if (!sg_policy) {
581 ret = -ENOMEM;
582 goto disable_fast_switch;
585 ret = sugov_kthread_create(sg_policy);
586 if (ret)
587 goto free_sg_policy;
589 mutex_lock(&global_tunables_lock);
591 if (global_tunables) {
592 if (WARN_ON(have_governor_per_policy())) {
593 ret = -EINVAL;
594 goto stop_kthread;
596 policy->governor_data = sg_policy;
597 sg_policy->tunables = global_tunables;
599 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
600 goto out;
603 tunables = sugov_tunables_alloc(sg_policy);
604 if (!tunables) {
605 ret = -ENOMEM;
606 goto stop_kthread;
609 tunables->rate_limit_us = cpufreq_policy_transition_delay_us(policy);
611 policy->governor_data = sg_policy;
612 sg_policy->tunables = tunables;
614 ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
615 get_governor_parent_kobj(policy), "%s",
616 schedutil_gov.name);
617 if (ret)
618 goto fail;
620 out:
621 mutex_unlock(&global_tunables_lock);
622 return 0;
624 fail:
625 policy->governor_data = NULL;
626 sugov_tunables_free(tunables);
628 stop_kthread:
629 sugov_kthread_stop(sg_policy);
631 free_sg_policy:
632 mutex_unlock(&global_tunables_lock);
634 sugov_policy_free(sg_policy);
636 disable_fast_switch:
637 cpufreq_disable_fast_switch(policy);
639 pr_err("initialization failed (error %d)\n", ret);
640 return ret;
643 static void sugov_exit(struct cpufreq_policy *policy)
645 struct sugov_policy *sg_policy = policy->governor_data;
646 struct sugov_tunables *tunables = sg_policy->tunables;
647 unsigned int count;
649 mutex_lock(&global_tunables_lock);
651 count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
652 policy->governor_data = NULL;
653 if (!count)
654 sugov_tunables_free(tunables);
656 mutex_unlock(&global_tunables_lock);
658 sugov_kthread_stop(sg_policy);
659 sugov_policy_free(sg_policy);
660 cpufreq_disable_fast_switch(policy);
663 static int sugov_start(struct cpufreq_policy *policy)
665 struct sugov_policy *sg_policy = policy->governor_data;
666 unsigned int cpu;
668 sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
669 sg_policy->last_freq_update_time = 0;
670 sg_policy->next_freq = UINT_MAX;
671 sg_policy->work_in_progress = false;
672 sg_policy->need_freq_update = false;
673 sg_policy->cached_raw_freq = 0;
675 for_each_cpu(cpu, policy->cpus) {
676 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
678 memset(sg_cpu, 0, sizeof(*sg_cpu));
679 sg_cpu->cpu = cpu;
680 sg_cpu->sg_policy = sg_policy;
681 sg_cpu->flags = 0;
682 sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
685 for_each_cpu(cpu, policy->cpus) {
686 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
688 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
689 policy_is_shared(policy) ?
690 sugov_update_shared :
691 sugov_update_single);
693 return 0;
696 static void sugov_stop(struct cpufreq_policy *policy)
698 struct sugov_policy *sg_policy = policy->governor_data;
699 unsigned int cpu;
701 for_each_cpu(cpu, policy->cpus)
702 cpufreq_remove_update_util_hook(cpu);
704 synchronize_sched();
706 if (!policy->fast_switch_enabled) {
707 irq_work_sync(&sg_policy->irq_work);
708 kthread_cancel_work_sync(&sg_policy->work);
712 static void sugov_limits(struct cpufreq_policy *policy)
714 struct sugov_policy *sg_policy = policy->governor_data;
716 if (!policy->fast_switch_enabled) {
717 mutex_lock(&sg_policy->work_lock);
718 cpufreq_policy_apply_limits(policy);
719 mutex_unlock(&sg_policy->work_lock);
722 sg_policy->need_freq_update = true;
725 static struct cpufreq_governor schedutil_gov = {
726 .name = "schedutil",
727 .owner = THIS_MODULE,
728 .dynamic_switching = true,
729 .init = sugov_init,
730 .exit = sugov_exit,
731 .start = sugov_start,
732 .stop = sugov_stop,
733 .limits = sugov_limits,
736 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
737 struct cpufreq_governor *cpufreq_default_governor(void)
739 return &schedutil_gov;
741 #endif
743 static int __init sugov_register(void)
745 return cpufreq_register_governor(&schedutil_gov);
747 fs_initcall(sugov_register);