blob: 12605610b06e0cc5f510a16cc32d495db601f70a [file] [log] [blame]
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +02001/*
2 * CPUFreq governor based on scheduler-provided CPU utilization data.
3 *
4 * Copyright (C) 2016, Intel Corporation
5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 *
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.
10 */
11
Viresh Kumar60f05e82016-05-18 17:55:28 +053012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020014#include <linux/cpufreq.h>
Viresh Kumar29d892d72016-11-15 13:53:22 +053015#include <linux/kthread.h>
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020016#include <linux/slab.h>
17#include <trace/events/power.h>
18
19#include "sched.h"
Juri Lellic6e94382016-12-14 16:10:10 +000020#include "tune.h"
21
Juri Lellic6e94382016-12-14 16:10:10 +000022unsigned long boosted_cpu_util(int cpu);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020023
Steve Muckle4152c222016-11-17 10:48:45 +053024/* Stub out fast switch routines present on mainline to reduce the backport
25 * overhead. */
26#define cpufreq_driver_fast_switch(x, y) 0
27#define cpufreq_enable_fast_switch(x)
28#define cpufreq_disable_fast_switch(x)
29#define LATENCY_MULTIPLIER (1000)
Viresh Kumar29d892d72016-11-15 13:53:22 +053030#define SUGOV_KTHREAD_PRIORITY 50
31
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020032struct sugov_tunables {
33 struct gov_attr_set attr_set;
Steve Muckle4152c222016-11-17 10:48:45 +053034 unsigned int up_rate_limit_us;
35 unsigned int down_rate_limit_us;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020036};
37
38struct sugov_policy {
39 struct cpufreq_policy *policy;
40
41 struct sugov_tunables *tunables;
42 struct list_head tunables_hook;
43
44 raw_spinlock_t update_lock; /* For shared policies */
45 u64 last_freq_update_time;
Steve Muckle4152c222016-11-17 10:48:45 +053046 s64 min_rate_limit_ns;
47 s64 up_rate_delay_ns;
48 s64 down_rate_delay_ns;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020049 unsigned int next_freq;
Viresh Kumarafe8d4a2017-03-02 14:03:20 +053050 unsigned int cached_raw_freq;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020051
52 /* The next fields are only needed if fast switch cannot be used. */
53 struct irq_work irq_work;
Viresh Kumar29d892d72016-11-15 13:53:22 +053054 struct kthread_work work;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020055 struct mutex work_lock;
Viresh Kumar29d892d72016-11-15 13:53:22 +053056 struct kthread_worker worker;
57 struct task_struct *thread;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020058 bool work_in_progress;
59
60 bool need_freq_update;
61};
62
63struct sugov_cpu {
64 struct update_util_data update_util;
65 struct sugov_policy *sg_policy;
66
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +020067 unsigned long iowait_boost;
68 unsigned long iowait_boost_max;
69 u64 last_update;
Steve Muckle5cbea462016-07-13 13:25:26 -070070
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020071 /* The fields below are only needed when sharing a policy. */
72 unsigned long util;
73 unsigned long max;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +020074 unsigned int flags;
Chris Redpath595ae4a2017-05-25 15:24:58 +010075
76 /* The field below is for single-CPU policies only. */
77#ifdef CONFIG_NO_HZ_COMMON
78 unsigned long saved_idle_calls;
79#endif
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020080};
81
82static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
83
84/************************ Governor internals ***********************/
85
86static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
87{
88 s64 delta_ns;
89
90 if (sg_policy->work_in_progress)
91 return false;
92
93 if (unlikely(sg_policy->need_freq_update)) {
94 sg_policy->need_freq_update = false;
95 /*
96 * This happens when limits change, so forget the previous
97 * next_freq value and force an update.
98 */
99 sg_policy->next_freq = UINT_MAX;
100 return true;
101 }
102
103 delta_ns = time - sg_policy->last_freq_update_time;
Steve Muckle4152c222016-11-17 10:48:45 +0530104
105 /* No need to recalculate next freq for min_rate_limit_us at least */
106 return delta_ns >= sg_policy->min_rate_limit_ns;
107}
108
109static bool sugov_up_down_rate_limit(struct sugov_policy *sg_policy, u64 time,
110 unsigned int next_freq)
111{
112 s64 delta_ns;
113
114 delta_ns = time - sg_policy->last_freq_update_time;
115
116 if (next_freq > sg_policy->next_freq &&
117 delta_ns < sg_policy->up_rate_delay_ns)
118 return true;
119
120 if (next_freq < sg_policy->next_freq &&
121 delta_ns < sg_policy->down_rate_delay_ns)
122 return true;
123
124 return false;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200125}
126
127static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
128 unsigned int next_freq)
129{
130 struct cpufreq_policy *policy = sg_policy->policy;
131
Steve Muckle4152c222016-11-17 10:48:45 +0530132 if (sugov_up_down_rate_limit(sg_policy, time, next_freq))
133 return;
134
Chris Redpath6702ce12017-05-25 15:27:07 +0100135 if (sg_policy->next_freq == next_freq)
136 return;
137
138 sg_policy->next_freq = next_freq;
139 sg_policy->last_freq_update_time = time;
140
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200141 if (policy->fast_switch_enabled) {
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200142 next_freq = cpufreq_driver_fast_switch(policy, next_freq);
143 if (next_freq == CPUFREQ_ENTRY_INVALID)
144 return;
145
146 policy->cur = next_freq;
147 trace_cpu_frequency(next_freq, smp_processor_id());
Chris Redpath6702ce12017-05-25 15:27:07 +0100148 } else {
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200149 sg_policy->work_in_progress = true;
150 irq_work_queue(&sg_policy->irq_work);
151 }
152}
153
154/**
155 * get_next_freq - Compute a new frequency for a given cpufreq policy.
Viresh Kumar0942cea2017-03-02 14:03:21 +0530156 * @sg_policy: schedutil policy object to compute the new frequency for.
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200157 * @util: Current CPU utilization.
158 * @max: CPU capacity.
159 *
160 * If the utilization is frequency-invariant, choose the new frequency to be
161 * proportional to it, that is
162 *
163 * next_freq = C * max_freq * util / max
164 *
165 * Otherwise, approximate the would-be frequency-invariant utilization by
166 * util_raw * (curr_freq / max_freq) which leads to
167 *
168 * next_freq = C * curr_freq * util_raw / max
169 *
170 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
Steve Muckle5cbea462016-07-13 13:25:26 -0700171 *
172 * The lowest driver-supported frequency which is equal or greater than the raw
173 * next_freq (as calculated above) is returned, subject to policy min/max and
174 * cpufreq driver limitations.
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200175 */
Viresh Kumar0942cea2017-03-02 14:03:21 +0530176static unsigned int get_next_freq(struct sugov_policy *sg_policy,
177 unsigned long util, unsigned long max)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200178{
Steve Muckle5cbea462016-07-13 13:25:26 -0700179 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200180 unsigned int freq = arch_scale_freq_invariant() ?
181 policy->cpuinfo.max_freq : policy->cur;
182
Steve Muckle5cbea462016-07-13 13:25:26 -0700183 freq = (freq + (freq >> 2)) * util / max;
184
Viresh Kumarafe8d4a2017-03-02 14:03:20 +0530185 if (freq == sg_policy->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
Steve Muckle5cbea462016-07-13 13:25:26 -0700186 return sg_policy->next_freq;
Viresh Kumarafe8d4a2017-03-02 14:03:20 +0530187 sg_policy->cached_raw_freq = freq;
Steve Muckle5cbea462016-07-13 13:25:26 -0700188 return cpufreq_driver_resolve_freq(policy, freq);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200189}
190
Chris Redpatha6d67352017-03-24 17:37:28 +0000191static inline bool use_pelt(void)
192{
193#ifdef CONFIG_SCHED_WALT
194 return (!sysctl_sched_use_walt_cpu_util || walt_disabled);
195#else
196 return true;
197#endif
198}
199
Steve Muckle8d408122016-08-25 15:59:17 -0700200static void sugov_get_util(unsigned long *util, unsigned long *max, u64 time)
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200201{
Steve Muckle8d408122016-08-25 15:59:17 -0700202 int cpu = smp_processor_id();
203 struct rq *rq = cpu_rq(cpu);
204 unsigned long max_cap, rt;
205 s64 delta;
Steve Muckle8314bc82016-08-26 11:40:47 -0700206
Steve Muckle8d408122016-08-25 15:59:17 -0700207 max_cap = arch_scale_cpu_capacity(NULL, cpu);
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200208
Steve Muckle8d408122016-08-25 15:59:17 -0700209 sched_avg_update(rq);
210 delta = time - rq->age_stamp;
211 if (unlikely(delta < 0))
212 delta = 0;
213 rt = div64_u64(rq->rt_avg, sched_avg_period() + delta);
214 rt = (rt * max_cap) >> SCHED_CAPACITY_SHIFT;
215
Chris Redpatha6d67352017-03-24 17:37:28 +0000216 *util = boosted_cpu_util(cpu);
217 if (likely(use_pelt()))
218 *util = min((*util + rt), max_cap);
219
Steve Muckle8d408122016-08-25 15:59:17 -0700220 *max = max_cap;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200221}
222
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200223static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
224 unsigned int flags)
225{
226 if (flags & SCHED_CPUFREQ_IOWAIT) {
227 sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
228 } else if (sg_cpu->iowait_boost) {
229 s64 delta_ns = time - sg_cpu->last_update;
230
231 /* Clear iowait_boost if the CPU apprears to have been idle. */
232 if (delta_ns > TICK_NSEC)
233 sg_cpu->iowait_boost = 0;
234 }
235}
236
237static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util,
238 unsigned long *max)
239{
240 unsigned long boost_util = sg_cpu->iowait_boost;
241 unsigned long boost_max = sg_cpu->iowait_boost_max;
242
243 if (!boost_util)
244 return;
245
246 if (*util * boost_max < *max * boost_util) {
247 *util = boost_util;
248 *max = boost_max;
249 }
250 sg_cpu->iowait_boost >>= 1;
251}
252
Chris Redpath595ae4a2017-05-25 15:24:58 +0100253#ifdef CONFIG_NO_HZ_COMMON
254static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
255{
256 unsigned long idle_calls = tick_nohz_get_idle_calls();
257 bool ret = idle_calls == sg_cpu->saved_idle_calls;
258
259 sg_cpu->saved_idle_calls = idle_calls;
260 return ret;
261}
262#else
263static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
264#endif /* CONFIG_NO_HZ_COMMON */
265
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200266static void sugov_update_single(struct update_util_data *hook, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200267 unsigned int flags)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200268{
269 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
270 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
271 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200272 unsigned long util, max;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200273 unsigned int next_f;
Chris Redpath595ae4a2017-05-25 15:24:58 +0100274 bool busy;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200275
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200276 sugov_set_iowait_boost(sg_cpu, time, flags);
277 sg_cpu->last_update = time;
278
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200279 if (!sugov_should_update_freq(sg_policy, time))
280 return;
281
Chris Redpath595ae4a2017-05-25 15:24:58 +0100282 busy = sugov_cpu_is_busy(sg_cpu);
283
Steve Muckle8d408122016-08-25 15:59:17 -0700284 if (flags & SCHED_CPUFREQ_DL) {
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200285 next_f = policy->cpuinfo.max_freq;
286 } else {
Steve Muckle8d408122016-08-25 15:59:17 -0700287 sugov_get_util(&util, &max, time);
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200288 sugov_iowait_boost(sg_cpu, &util, &max);
Viresh Kumar0942cea2017-03-02 14:03:21 +0530289 next_f = get_next_freq(sg_policy, util, max);
Chris Redpath595ae4a2017-05-25 15:24:58 +0100290 /*
291 * Do not reduce the frequency if the CPU has not been idle
292 * recently, as the reduction is likely to be premature then.
293 */
294 if (busy && next_f < sg_policy->next_freq)
295 next_f = sg_policy->next_freq;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200296 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200297 sugov_update_commit(sg_policy, time, next_f);
298}
299
Chris Redpath39151862017-05-25 15:22:59 +0100300static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200301{
Steve Muckle5cbea462016-07-13 13:25:26 -0700302 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200303 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200304 u64 last_freq_update_time = sg_policy->last_freq_update_time;
Chris Redpath39151862017-05-25 15:22:59 +0100305 unsigned long util = 0, max = 1;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200306 unsigned int j;
307
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200308 for_each_cpu(j, policy->cpus) {
Chris Redpath39151862017-05-25 15:22:59 +0100309 struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200310 unsigned long j_util, j_max;
311 s64 delta_ns;
312
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200313 /*
314 * If the CPU utilization was last updated before the previous
315 * frequency update and the time elapsed between the last update
316 * of the CPU utilization and the last frequency update is long
317 * enough, don't take the CPU into account as it probably is
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200318 * idle now (and clear iowait_boost for it).
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200319 */
320 delta_ns = last_freq_update_time - j_sg_cpu->last_update;
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200321 if (delta_ns > TICK_NSEC) {
322 j_sg_cpu->iowait_boost = 0;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200323 continue;
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200324 }
Steve Muckle8d408122016-08-25 15:59:17 -0700325 if (j_sg_cpu->flags & SCHED_CPUFREQ_DL)
Chris Redpath39151862017-05-25 15:22:59 +0100326 return policy->cpuinfo.max_freq;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200327
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200328 j_util = j_sg_cpu->util;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200329 j_max = j_sg_cpu->max;
330 if (j_util * max > j_max * util) {
331 util = j_util;
332 max = j_max;
333 }
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200334
335 sugov_iowait_boost(j_sg_cpu, &util, &max);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200336 }
337
Viresh Kumar0942cea2017-03-02 14:03:21 +0530338 return get_next_freq(sg_policy, util, max);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200339}
340
341static void sugov_update_shared(struct update_util_data *hook, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200342 unsigned int flags)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200343{
344 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
345 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200346 unsigned long util, max;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200347 unsigned int next_f;
348
Steve Muckle8d408122016-08-25 15:59:17 -0700349 sugov_get_util(&util, &max, time);
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200350
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200351 raw_spin_lock(&sg_policy->update_lock);
352
353 sg_cpu->util = util;
354 sg_cpu->max = max;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200355 sg_cpu->flags = flags;
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200356
357 sugov_set_iowait_boost(sg_cpu, time, flags);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200358 sg_cpu->last_update = time;
359
360 if (sugov_should_update_freq(sg_policy, time)) {
Chris Redpath39151862017-05-25 15:22:59 +0100361 if (flags & SCHED_CPUFREQ_DL)
362 next_f = sg_policy->policy->cpuinfo.max_freq;
363 else
364 next_f = sugov_next_freq_shared(sg_cpu);
365
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200366 sugov_update_commit(sg_policy, time, next_f);
367 }
368
369 raw_spin_unlock(&sg_policy->update_lock);
370}
371
Viresh Kumar29d892d72016-11-15 13:53:22 +0530372static void sugov_work(struct kthread_work *work)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200373{
374 struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
375
376 mutex_lock(&sg_policy->work_lock);
377 __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
378 CPUFREQ_RELATION_L);
379 mutex_unlock(&sg_policy->work_lock);
380
381 sg_policy->work_in_progress = false;
382}
383
384static void sugov_irq_work(struct irq_work *irq_work)
385{
386 struct sugov_policy *sg_policy;
387
388 sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
Viresh Kumar29d892d72016-11-15 13:53:22 +0530389
390 /*
Viresh Kumar81162a92016-11-24 13:51:11 +0530391 * For RT and deadline tasks, the schedutil governor shoots the
392 * frequency to maximum. Special care must be taken to ensure that this
393 * kthread doesn't result in the same behavior.
Viresh Kumar29d892d72016-11-15 13:53:22 +0530394 *
395 * This is (mostly) guaranteed by the work_in_progress flag. The flag is
Viresh Kumar81162a92016-11-24 13:51:11 +0530396 * updated only at the end of the sugov_work() function and before that
397 * the schedutil governor rejects all other frequency scaling requests.
Viresh Kumar29d892d72016-11-15 13:53:22 +0530398 *
Viresh Kumar81162a92016-11-24 13:51:11 +0530399 * There is a very rare case though, where the RT thread yields right
Viresh Kumar29d892d72016-11-15 13:53:22 +0530400 * after the work_in_progress flag is cleared. The effects of that are
401 * neglected for now.
402 */
403 kthread_queue_work(&sg_policy->worker, &sg_policy->work);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200404}
405
406/************************** sysfs interface ************************/
407
408static struct sugov_tunables *global_tunables;
409static DEFINE_MUTEX(global_tunables_lock);
410
411static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
412{
413 return container_of(attr_set, struct sugov_tunables, attr_set);
414}
415
Steve Muckle4152c222016-11-17 10:48:45 +0530416static DEFINE_MUTEX(min_rate_lock);
417
418static void update_min_rate_limit_us(struct sugov_policy *sg_policy)
419{
420 mutex_lock(&min_rate_lock);
421 sg_policy->min_rate_limit_ns = min(sg_policy->up_rate_delay_ns,
422 sg_policy->down_rate_delay_ns);
423 mutex_unlock(&min_rate_lock);
424}
425
426static ssize_t up_rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200427{
428 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
429
Steve Muckle4152c222016-11-17 10:48:45 +0530430 return sprintf(buf, "%u\n", tunables->up_rate_limit_us);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200431}
432
Steve Muckle4152c222016-11-17 10:48:45 +0530433static ssize_t down_rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
434{
435 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
436
437 return sprintf(buf, "%u\n", tunables->down_rate_limit_us);
438}
439
440static ssize_t up_rate_limit_us_store(struct gov_attr_set *attr_set,
441 const char *buf, size_t count)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200442{
443 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
444 struct sugov_policy *sg_policy;
445 unsigned int rate_limit_us;
446
447 if (kstrtouint(buf, 10, &rate_limit_us))
448 return -EINVAL;
449
Steve Muckle4152c222016-11-17 10:48:45 +0530450 tunables->up_rate_limit_us = rate_limit_us;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200451
Steve Muckle4152c222016-11-17 10:48:45 +0530452 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) {
453 sg_policy->up_rate_delay_ns = rate_limit_us * NSEC_PER_USEC;
454 update_min_rate_limit_us(sg_policy);
455 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200456
457 return count;
458}
459
Steve Muckle4152c222016-11-17 10:48:45 +0530460static ssize_t down_rate_limit_us_store(struct gov_attr_set *attr_set,
461 const char *buf, size_t count)
462{
463 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
464 struct sugov_policy *sg_policy;
465 unsigned int rate_limit_us;
466
467 if (kstrtouint(buf, 10, &rate_limit_us))
468 return -EINVAL;
469
470 tunables->down_rate_limit_us = rate_limit_us;
471
472 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) {
473 sg_policy->down_rate_delay_ns = rate_limit_us * NSEC_PER_USEC;
474 update_min_rate_limit_us(sg_policy);
475 }
476
477 return count;
478}
479
480static struct governor_attr up_rate_limit_us = __ATTR_RW(up_rate_limit_us);
481static struct governor_attr down_rate_limit_us = __ATTR_RW(down_rate_limit_us);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200482
483static struct attribute *sugov_attributes[] = {
Steve Muckle4152c222016-11-17 10:48:45 +0530484 &up_rate_limit_us.attr,
485 &down_rate_limit_us.attr,
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200486 NULL
487};
488
489static struct kobj_type sugov_tunables_ktype = {
490 .default_attrs = sugov_attributes,
491 .sysfs_ops = &governor_sysfs_ops,
492};
493
494/********************** cpufreq governor interface *********************/
495
496static struct cpufreq_governor schedutil_gov;
497
498static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
499{
500 struct sugov_policy *sg_policy;
501
502 sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
503 if (!sg_policy)
504 return NULL;
505
506 sg_policy->policy = policy;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200507 raw_spin_lock_init(&sg_policy->update_lock);
508 return sg_policy;
509}
510
511static void sugov_policy_free(struct sugov_policy *sg_policy)
512{
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200513 kfree(sg_policy);
514}
515
Viresh Kumar29d892d72016-11-15 13:53:22 +0530516static int sugov_kthread_create(struct sugov_policy *sg_policy)
517{
518 struct task_struct *thread;
519 struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO / 2 };
520 struct cpufreq_policy *policy = sg_policy->policy;
521 int ret;
522
523 /* kthread only required for slow path */
524 if (policy->fast_switch_enabled)
525 return 0;
526
527 kthread_init_work(&sg_policy->work, sugov_work);
528 kthread_init_worker(&sg_policy->worker);
529 thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
530 "sugov:%d",
531 cpumask_first(policy->related_cpus));
532 if (IS_ERR(thread)) {
533 pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
534 return PTR_ERR(thread);
535 }
536
537 ret = sched_setscheduler_nocheck(thread, SCHED_FIFO, &param);
538 if (ret) {
539 kthread_stop(thread);
540 pr_warn("%s: failed to set SCHED_FIFO\n", __func__);
541 return ret;
542 }
543
544 sg_policy->thread = thread;
545 kthread_bind_mask(thread, policy->related_cpus);
Chris Redpath338ad2c2017-07-20 16:34:10 +0100546 init_irq_work(&sg_policy->irq_work, sugov_irq_work);
547 mutex_init(&sg_policy->work_lock);
548
Viresh Kumar29d892d72016-11-15 13:53:22 +0530549 wake_up_process(thread);
550
551 return 0;
552}
553
554static void sugov_kthread_stop(struct sugov_policy *sg_policy)
555{
556 /* kthread only required for slow path */
557 if (sg_policy->policy->fast_switch_enabled)
558 return;
559
560 kthread_flush_worker(&sg_policy->worker);
561 kthread_stop(sg_policy->thread);
Chris Redpath338ad2c2017-07-20 16:34:10 +0100562 mutex_destroy(&sg_policy->work_lock);
Viresh Kumar29d892d72016-11-15 13:53:22 +0530563}
564
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200565static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
566{
567 struct sugov_tunables *tunables;
568
569 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
570 if (tunables) {
571 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
572 if (!have_governor_per_policy())
573 global_tunables = tunables;
574 }
575 return tunables;
576}
577
578static void sugov_tunables_free(struct sugov_tunables *tunables)
579{
580 if (!have_governor_per_policy())
581 global_tunables = NULL;
582
583 kfree(tunables);
584}
585
586static int sugov_init(struct cpufreq_policy *policy)
587{
588 struct sugov_policy *sg_policy;
589 struct sugov_tunables *tunables;
590 unsigned int lat;
591 int ret = 0;
592
593 /* State should be equivalent to EXIT */
594 if (policy->governor_data)
595 return -EBUSY;
596
Chris Redpath99ab82d2017-07-20 16:32:35 +0100597 cpufreq_enable_fast_switch(policy);
598
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200599 sg_policy = sugov_policy_alloc(policy);
Chris Redpath99ab82d2017-07-20 16:32:35 +0100600 if (!sg_policy) {
601 ret = -ENOMEM;
602 goto disable_fast_switch;
603 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200604
Viresh Kumar29d892d72016-11-15 13:53:22 +0530605 ret = sugov_kthread_create(sg_policy);
606 if (ret)
607 goto free_sg_policy;
608
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200609 mutex_lock(&global_tunables_lock);
610
611 if (global_tunables) {
612 if (WARN_ON(have_governor_per_policy())) {
613 ret = -EINVAL;
Viresh Kumar29d892d72016-11-15 13:53:22 +0530614 goto stop_kthread;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200615 }
616 policy->governor_data = sg_policy;
617 sg_policy->tunables = global_tunables;
618
619 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
620 goto out;
621 }
622
623 tunables = sugov_tunables_alloc(sg_policy);
624 if (!tunables) {
625 ret = -ENOMEM;
Viresh Kumar29d892d72016-11-15 13:53:22 +0530626 goto stop_kthread;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200627 }
628
Steve Muckle4152c222016-11-17 10:48:45 +0530629 tunables->up_rate_limit_us = LATENCY_MULTIPLIER;
630 tunables->down_rate_limit_us = LATENCY_MULTIPLIER;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200631 lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
Steve Muckle4152c222016-11-17 10:48:45 +0530632 if (lat) {
633 tunables->up_rate_limit_us *= lat;
634 tunables->down_rate_limit_us *= lat;
635 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200636
637 policy->governor_data = sg_policy;
638 sg_policy->tunables = tunables;
639
640 ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
641 get_governor_parent_kobj(policy), "%s",
642 schedutil_gov.name);
643 if (ret)
644 goto fail;
645
Chris Redpath91a6b312017-05-25 15:04:04 +0100646out:
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200647 mutex_unlock(&global_tunables_lock);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200648 return 0;
649
Chris Redpath91a6b312017-05-25 15:04:04 +0100650fail:
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200651 policy->governor_data = NULL;
652 sugov_tunables_free(tunables);
653
Viresh Kumar29d892d72016-11-15 13:53:22 +0530654 stop_kthread:
655 sugov_kthread_stop(sg_policy);
656
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200657 free_sg_policy:
658 mutex_unlock(&global_tunables_lock);
659
660 sugov_policy_free(sg_policy);
Chris Redpath99ab82d2017-07-20 16:32:35 +0100661
662disable_fast_switch:
663 cpufreq_disable_fast_switch(policy);
664
Viresh Kumar60f05e82016-05-18 17:55:28 +0530665 pr_err("initialization failed (error %d)\n", ret);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200666 return ret;
667}
668
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200669static void sugov_exit(struct cpufreq_policy *policy)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200670{
671 struct sugov_policy *sg_policy = policy->governor_data;
672 struct sugov_tunables *tunables = sg_policy->tunables;
673 unsigned int count;
674
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200675 mutex_lock(&global_tunables_lock);
676
677 count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
678 policy->governor_data = NULL;
679 if (!count)
680 sugov_tunables_free(tunables);
681
682 mutex_unlock(&global_tunables_lock);
683
Viresh Kumar29d892d72016-11-15 13:53:22 +0530684 sugov_kthread_stop(sg_policy);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200685 sugov_policy_free(sg_policy);
Chris Redpath99ab82d2017-07-20 16:32:35 +0100686
687 cpufreq_disable_fast_switch(policy);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200688}
689
690static int sugov_start(struct cpufreq_policy *policy)
691{
692 struct sugov_policy *sg_policy = policy->governor_data;
693 unsigned int cpu;
694
Steve Muckle4152c222016-11-17 10:48:45 +0530695 sg_policy->up_rate_delay_ns =
696 sg_policy->tunables->up_rate_limit_us * NSEC_PER_USEC;
697 sg_policy->down_rate_delay_ns =
698 sg_policy->tunables->down_rate_limit_us * NSEC_PER_USEC;
699 update_min_rate_limit_us(sg_policy);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200700 sg_policy->last_freq_update_time = 0;
701 sg_policy->next_freq = UINT_MAX;
702 sg_policy->work_in_progress = false;
703 sg_policy->need_freq_update = false;
Viresh Kumarafe8d4a2017-03-02 14:03:20 +0530704 sg_policy->cached_raw_freq = 0;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200705
706 for_each_cpu(cpu, policy->cpus) {
707 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
708
Rafael J. Wysockia8fc3152017-03-19 14:30:02 +0100709 memset(sg_cpu, 0, sizeof(*sg_cpu));
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200710 sg_cpu->sg_policy = sg_policy;
Steve Muckle8d408122016-08-25 15:59:17 -0700711 sg_cpu->flags = SCHED_CPUFREQ_DL;
Rafael J. Wysockia8fc3152017-03-19 14:30:02 +0100712 sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
713 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
714 policy_is_shared(policy) ?
715 sugov_update_shared :
716 sugov_update_single);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200717 }
718 return 0;
719}
720
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200721static void sugov_stop(struct cpufreq_policy *policy)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200722{
723 struct sugov_policy *sg_policy = policy->governor_data;
724 unsigned int cpu;
725
726 for_each_cpu(cpu, policy->cpus)
727 cpufreq_remove_update_util_hook(cpu);
728
729 synchronize_sched();
730
Chris Redpath338ad2c2017-07-20 16:34:10 +0100731 if (!policy->fast_switch_enabled) {
732 irq_work_sync(&sg_policy->irq_work);
733 kthread_cancel_work_sync(&sg_policy->work);
734 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200735}
736
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200737static void sugov_limits(struct cpufreq_policy *policy)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200738{
739 struct sugov_policy *sg_policy = policy->governor_data;
740
741 if (!policy->fast_switch_enabled) {
742 mutex_lock(&sg_policy->work_lock);
Viresh Kumarbf2be2d2016-05-18 17:55:31 +0530743 cpufreq_policy_apply_limits(policy);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200744 mutex_unlock(&sg_policy->work_lock);
745 }
746
747 sg_policy->need_freq_update = true;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200748}
749
750static struct cpufreq_governor schedutil_gov = {
751 .name = "schedutil",
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200752 .owner = THIS_MODULE,
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200753 .init = sugov_init,
754 .exit = sugov_exit,
755 .start = sugov_start,
756 .stop = sugov_stop,
757 .limits = sugov_limits,
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200758};
759
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200760#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
761struct cpufreq_governor *cpufreq_default_governor(void)
762{
763 return &schedutil_gov;
764}
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200765#endif
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200766
767static int __init sugov_register(void)
768{
769 return cpufreq_register_governor(&schedutil_gov);
770}
771fs_initcall(sugov_register);