blob: 3a15935cf62e51cd21fafa201d630b6277d4a683 [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;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +020075};
76
77static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
78
79/************************ Governor internals ***********************/
80
81static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
82{
83 s64 delta_ns;
84
85 if (sg_policy->work_in_progress)
86 return false;
87
88 if (unlikely(sg_policy->need_freq_update)) {
89 sg_policy->need_freq_update = false;
90 /*
91 * This happens when limits change, so forget the previous
92 * next_freq value and force an update.
93 */
94 sg_policy->next_freq = UINT_MAX;
95 return true;
96 }
97
98 delta_ns = time - sg_policy->last_freq_update_time;
Steve Muckle4152c222016-11-17 10:48:45 +053099
100 /* No need to recalculate next freq for min_rate_limit_us at least */
101 return delta_ns >= sg_policy->min_rate_limit_ns;
102}
103
104static bool sugov_up_down_rate_limit(struct sugov_policy *sg_policy, u64 time,
105 unsigned int next_freq)
106{
107 s64 delta_ns;
108
109 delta_ns = time - sg_policy->last_freq_update_time;
110
111 if (next_freq > sg_policy->next_freq &&
112 delta_ns < sg_policy->up_rate_delay_ns)
113 return true;
114
115 if (next_freq < sg_policy->next_freq &&
116 delta_ns < sg_policy->down_rate_delay_ns)
117 return true;
118
119 return false;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200120}
121
122static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
123 unsigned int next_freq)
124{
125 struct cpufreq_policy *policy = sg_policy->policy;
126
Steve Muckle4152c222016-11-17 10:48:45 +0530127 if (sugov_up_down_rate_limit(sg_policy, time, next_freq))
128 return;
129
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200130 if (policy->fast_switch_enabled) {
131 if (sg_policy->next_freq == next_freq) {
132 trace_cpu_frequency(policy->cur, smp_processor_id());
133 return;
134 }
135 sg_policy->next_freq = next_freq;
Viresh Kumar8c29c1a2017-02-21 10:15:18 +0530136 sg_policy->last_freq_update_time = time;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200137 next_freq = cpufreq_driver_fast_switch(policy, next_freq);
138 if (next_freq == CPUFREQ_ENTRY_INVALID)
139 return;
140
141 policy->cur = next_freq;
142 trace_cpu_frequency(next_freq, smp_processor_id());
143 } else if (sg_policy->next_freq != next_freq) {
144 sg_policy->next_freq = next_freq;
Viresh Kumar8c29c1a2017-02-21 10:15:18 +0530145 sg_policy->last_freq_update_time = time;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200146 sg_policy->work_in_progress = true;
147 irq_work_queue(&sg_policy->irq_work);
148 }
149}
150
151/**
152 * get_next_freq - Compute a new frequency for a given cpufreq policy.
Steve Muckle5cbea462016-07-13 13:25:26 -0700153 * @sg_cpu: schedutil cpu object to compute the new frequency for.
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200154 * @util: Current CPU utilization.
155 * @max: CPU capacity.
156 *
157 * If the utilization is frequency-invariant, choose the new frequency to be
158 * proportional to it, that is
159 *
160 * next_freq = C * max_freq * util / max
161 *
162 * Otherwise, approximate the would-be frequency-invariant utilization by
163 * util_raw * (curr_freq / max_freq) which leads to
164 *
165 * next_freq = C * curr_freq * util_raw / max
166 *
167 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
Steve Muckle5cbea462016-07-13 13:25:26 -0700168 *
169 * The lowest driver-supported frequency which is equal or greater than the raw
170 * next_freq (as calculated above) is returned, subject to policy min/max and
171 * cpufreq driver limitations.
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200172 */
Steve Muckle5cbea462016-07-13 13:25:26 -0700173static unsigned int get_next_freq(struct sugov_cpu *sg_cpu, unsigned long util,
174 unsigned long max)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200175{
Steve Muckle5cbea462016-07-13 13:25:26 -0700176 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
177 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200178 unsigned int freq = arch_scale_freq_invariant() ?
179 policy->cpuinfo.max_freq : policy->cur;
180
Steve Muckle5cbea462016-07-13 13:25:26 -0700181 freq = (freq + (freq >> 2)) * util / max;
182
Viresh Kumarafe8d4a2017-03-02 14:03:20 +0530183 if (freq == sg_policy->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
Steve Muckle5cbea462016-07-13 13:25:26 -0700184 return sg_policy->next_freq;
Viresh Kumarafe8d4a2017-03-02 14:03:20 +0530185 sg_policy->cached_raw_freq = freq;
Steve Muckle5cbea462016-07-13 13:25:26 -0700186 return cpufreq_driver_resolve_freq(policy, freq);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200187}
188
Chris Redpatha6d67352017-03-24 17:37:28 +0000189static inline bool use_pelt(void)
190{
191#ifdef CONFIG_SCHED_WALT
192 return (!sysctl_sched_use_walt_cpu_util || walt_disabled);
193#else
194 return true;
195#endif
196}
197
Steve Muckle8d408122016-08-25 15:59:17 -0700198static void sugov_get_util(unsigned long *util, unsigned long *max, u64 time)
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200199{
Steve Muckle8d408122016-08-25 15:59:17 -0700200 int cpu = smp_processor_id();
201 struct rq *rq = cpu_rq(cpu);
202 unsigned long max_cap, rt;
203 s64 delta;
Steve Muckle8314bc82016-08-26 11:40:47 -0700204
Steve Muckle8d408122016-08-25 15:59:17 -0700205 max_cap = arch_scale_cpu_capacity(NULL, cpu);
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200206
Steve Muckle8d408122016-08-25 15:59:17 -0700207 sched_avg_update(rq);
208 delta = time - rq->age_stamp;
209 if (unlikely(delta < 0))
210 delta = 0;
211 rt = div64_u64(rq->rt_avg, sched_avg_period() + delta);
212 rt = (rt * max_cap) >> SCHED_CAPACITY_SHIFT;
213
Chris Redpatha6d67352017-03-24 17:37:28 +0000214 *util = boosted_cpu_util(cpu);
215 if (likely(use_pelt()))
216 *util = min((*util + rt), max_cap);
217
Steve Muckle8d408122016-08-25 15:59:17 -0700218 *max = max_cap;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200219}
220
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200221static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
222 unsigned int flags)
223{
224 if (flags & SCHED_CPUFREQ_IOWAIT) {
225 sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
226 } else if (sg_cpu->iowait_boost) {
227 s64 delta_ns = time - sg_cpu->last_update;
228
229 /* Clear iowait_boost if the CPU apprears to have been idle. */
230 if (delta_ns > TICK_NSEC)
231 sg_cpu->iowait_boost = 0;
232 }
233}
234
235static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util,
236 unsigned long *max)
237{
238 unsigned long boost_util = sg_cpu->iowait_boost;
239 unsigned long boost_max = sg_cpu->iowait_boost_max;
240
241 if (!boost_util)
242 return;
243
244 if (*util * boost_max < *max * boost_util) {
245 *util = boost_util;
246 *max = boost_max;
247 }
248 sg_cpu->iowait_boost >>= 1;
249}
250
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200251static void sugov_update_single(struct update_util_data *hook, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200252 unsigned int flags)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200253{
254 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
255 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
256 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200257 unsigned long util, max;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200258 unsigned int next_f;
259
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200260 sugov_set_iowait_boost(sg_cpu, time, flags);
261 sg_cpu->last_update = time;
262
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200263 if (!sugov_should_update_freq(sg_policy, time))
264 return;
265
Steve Muckle8d408122016-08-25 15:59:17 -0700266 if (flags & SCHED_CPUFREQ_DL) {
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200267 next_f = policy->cpuinfo.max_freq;
268 } else {
Steve Muckle8d408122016-08-25 15:59:17 -0700269 sugov_get_util(&util, &max, time);
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200270 sugov_iowait_boost(sg_cpu, &util, &max);
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200271 next_f = get_next_freq(sg_cpu, util, max);
272 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200273 sugov_update_commit(sg_policy, time, next_f);
274}
275
Steve Muckle5cbea462016-07-13 13:25:26 -0700276static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200277 unsigned long util, unsigned long max,
278 unsigned int flags)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200279{
Steve Muckle5cbea462016-07-13 13:25:26 -0700280 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200281 struct cpufreq_policy *policy = sg_policy->policy;
282 unsigned int max_f = policy->cpuinfo.max_freq;
283 u64 last_freq_update_time = sg_policy->last_freq_update_time;
284 unsigned int j;
285
Steve Muckle8d408122016-08-25 15:59:17 -0700286 if (flags & SCHED_CPUFREQ_DL)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200287 return max_f;
288
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200289 sugov_iowait_boost(sg_cpu, &util, &max);
290
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200291 for_each_cpu(j, policy->cpus) {
292 struct sugov_cpu *j_sg_cpu;
293 unsigned long j_util, j_max;
294 s64 delta_ns;
295
296 if (j == smp_processor_id())
297 continue;
298
299 j_sg_cpu = &per_cpu(sugov_cpu, j);
300 /*
301 * If the CPU utilization was last updated before the previous
302 * frequency update and the time elapsed between the last update
303 * of the CPU utilization and the last frequency update is long
304 * enough, don't take the CPU into account as it probably is
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200305 * idle now (and clear iowait_boost for it).
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200306 */
307 delta_ns = last_freq_update_time - j_sg_cpu->last_update;
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200308 if (delta_ns > TICK_NSEC) {
309 j_sg_cpu->iowait_boost = 0;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200310 continue;
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200311 }
Steve Muckle8d408122016-08-25 15:59:17 -0700312 if (j_sg_cpu->flags & SCHED_CPUFREQ_DL)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200313 return max_f;
314
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200315 j_util = j_sg_cpu->util;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200316 j_max = j_sg_cpu->max;
317 if (j_util * max > j_max * util) {
318 util = j_util;
319 max = j_max;
320 }
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200321
322 sugov_iowait_boost(j_sg_cpu, &util, &max);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200323 }
324
Steve Muckle5cbea462016-07-13 13:25:26 -0700325 return get_next_freq(sg_cpu, util, max);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200326}
327
328static void sugov_update_shared(struct update_util_data *hook, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200329 unsigned int flags)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200330{
331 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
332 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200333 unsigned long util, max;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200334 unsigned int next_f;
335
Steve Muckle8d408122016-08-25 15:59:17 -0700336 sugov_get_util(&util, &max, time);
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200337
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200338 raw_spin_lock(&sg_policy->update_lock);
339
340 sg_cpu->util = util;
341 sg_cpu->max = max;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200342 sg_cpu->flags = flags;
Rafael J. Wysocki21ca6d22016-09-10 00:00:31 +0200343
344 sugov_set_iowait_boost(sg_cpu, time, flags);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200345 sg_cpu->last_update = time;
346
347 if (sugov_should_update_freq(sg_policy, time)) {
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200348 next_f = sugov_next_freq_shared(sg_cpu, util, max, flags);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200349 sugov_update_commit(sg_policy, time, next_f);
350 }
351
352 raw_spin_unlock(&sg_policy->update_lock);
353}
354
Viresh Kumar29d892d72016-11-15 13:53:22 +0530355static void sugov_work(struct kthread_work *work)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200356{
357 struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
358
359 mutex_lock(&sg_policy->work_lock);
360 __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
361 CPUFREQ_RELATION_L);
362 mutex_unlock(&sg_policy->work_lock);
363
364 sg_policy->work_in_progress = false;
365}
366
367static void sugov_irq_work(struct irq_work *irq_work)
368{
369 struct sugov_policy *sg_policy;
370
371 sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
Viresh Kumar29d892d72016-11-15 13:53:22 +0530372
373 /*
374 * For Real Time and Deadline tasks, schedutil governor shoots the
375 * frequency to maximum. And special care must be taken to ensure that
376 * this kthread doesn't result in that.
377 *
378 * This is (mostly) guaranteed by the work_in_progress flag. The flag is
379 * updated only at the end of the sugov_work() and before that schedutil
380 * rejects all other frequency scaling requests.
381 *
382 * Though there is a very rare case where the RT thread yields right
383 * after the work_in_progress flag is cleared. The effects of that are
384 * neglected for now.
385 */
386 kthread_queue_work(&sg_policy->worker, &sg_policy->work);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200387}
388
389/************************** sysfs interface ************************/
390
391static struct sugov_tunables *global_tunables;
392static DEFINE_MUTEX(global_tunables_lock);
393
394static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
395{
396 return container_of(attr_set, struct sugov_tunables, attr_set);
397}
398
Steve Muckle4152c222016-11-17 10:48:45 +0530399static DEFINE_MUTEX(min_rate_lock);
400
401static void update_min_rate_limit_us(struct sugov_policy *sg_policy)
402{
403 mutex_lock(&min_rate_lock);
404 sg_policy->min_rate_limit_ns = min(sg_policy->up_rate_delay_ns,
405 sg_policy->down_rate_delay_ns);
406 mutex_unlock(&min_rate_lock);
407}
408
409static ssize_t up_rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200410{
411 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
412
Steve Muckle4152c222016-11-17 10:48:45 +0530413 return sprintf(buf, "%u\n", tunables->up_rate_limit_us);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200414}
415
Steve Muckle4152c222016-11-17 10:48:45 +0530416static ssize_t down_rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
417{
418 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
419
420 return sprintf(buf, "%u\n", tunables->down_rate_limit_us);
421}
422
423static ssize_t up_rate_limit_us_store(struct gov_attr_set *attr_set,
424 const char *buf, size_t count)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200425{
426 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
427 struct sugov_policy *sg_policy;
428 unsigned int rate_limit_us;
429
430 if (kstrtouint(buf, 10, &rate_limit_us))
431 return -EINVAL;
432
Steve Muckle4152c222016-11-17 10:48:45 +0530433 tunables->up_rate_limit_us = rate_limit_us;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200434
Steve Muckle4152c222016-11-17 10:48:45 +0530435 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) {
436 sg_policy->up_rate_delay_ns = rate_limit_us * NSEC_PER_USEC;
437 update_min_rate_limit_us(sg_policy);
438 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200439
440 return count;
441}
442
Steve Muckle4152c222016-11-17 10:48:45 +0530443static ssize_t down_rate_limit_us_store(struct gov_attr_set *attr_set,
444 const char *buf, size_t count)
445{
446 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
447 struct sugov_policy *sg_policy;
448 unsigned int rate_limit_us;
449
450 if (kstrtouint(buf, 10, &rate_limit_us))
451 return -EINVAL;
452
453 tunables->down_rate_limit_us = rate_limit_us;
454
455 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) {
456 sg_policy->down_rate_delay_ns = rate_limit_us * NSEC_PER_USEC;
457 update_min_rate_limit_us(sg_policy);
458 }
459
460 return count;
461}
462
463static struct governor_attr up_rate_limit_us = __ATTR_RW(up_rate_limit_us);
464static struct governor_attr down_rate_limit_us = __ATTR_RW(down_rate_limit_us);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200465
466static struct attribute *sugov_attributes[] = {
Steve Muckle4152c222016-11-17 10:48:45 +0530467 &up_rate_limit_us.attr,
468 &down_rate_limit_us.attr,
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200469 NULL
470};
471
472static struct kobj_type sugov_tunables_ktype = {
473 .default_attrs = sugov_attributes,
474 .sysfs_ops = &governor_sysfs_ops,
475};
476
477/********************** cpufreq governor interface *********************/
478
479static struct cpufreq_governor schedutil_gov;
480
481static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
482{
483 struct sugov_policy *sg_policy;
484
485 sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
486 if (!sg_policy)
487 return NULL;
488
489 sg_policy->policy = policy;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200490 raw_spin_lock_init(&sg_policy->update_lock);
491 return sg_policy;
492}
493
494static void sugov_policy_free(struct sugov_policy *sg_policy)
495{
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200496 kfree(sg_policy);
497}
498
Viresh Kumar29d892d72016-11-15 13:53:22 +0530499static int sugov_kthread_create(struct sugov_policy *sg_policy)
500{
501 struct task_struct *thread;
502 struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO / 2 };
503 struct cpufreq_policy *policy = sg_policy->policy;
504 int ret;
505
506 /* kthread only required for slow path */
507 if (policy->fast_switch_enabled)
508 return 0;
509
510 kthread_init_work(&sg_policy->work, sugov_work);
511 kthread_init_worker(&sg_policy->worker);
512 thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
513 "sugov:%d",
514 cpumask_first(policy->related_cpus));
515 if (IS_ERR(thread)) {
516 pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
517 return PTR_ERR(thread);
518 }
519
520 ret = sched_setscheduler_nocheck(thread, SCHED_FIFO, &param);
521 if (ret) {
522 kthread_stop(thread);
523 pr_warn("%s: failed to set SCHED_FIFO\n", __func__);
524 return ret;
525 }
526
527 sg_policy->thread = thread;
528 kthread_bind_mask(thread, policy->related_cpus);
Chris Redpath338ad2c2017-07-20 16:34:10 +0100529 init_irq_work(&sg_policy->irq_work, sugov_irq_work);
530 mutex_init(&sg_policy->work_lock);
531
Viresh Kumar29d892d72016-11-15 13:53:22 +0530532 wake_up_process(thread);
533
534 return 0;
535}
536
537static void sugov_kthread_stop(struct sugov_policy *sg_policy)
538{
539 /* kthread only required for slow path */
540 if (sg_policy->policy->fast_switch_enabled)
541 return;
542
543 kthread_flush_worker(&sg_policy->worker);
544 kthread_stop(sg_policy->thread);
Chris Redpath338ad2c2017-07-20 16:34:10 +0100545 mutex_destroy(&sg_policy->work_lock);
Viresh Kumar29d892d72016-11-15 13:53:22 +0530546}
547
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200548static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
549{
550 struct sugov_tunables *tunables;
551
552 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
553 if (tunables) {
554 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
555 if (!have_governor_per_policy())
556 global_tunables = tunables;
557 }
558 return tunables;
559}
560
561static void sugov_tunables_free(struct sugov_tunables *tunables)
562{
563 if (!have_governor_per_policy())
564 global_tunables = NULL;
565
566 kfree(tunables);
567}
568
569static int sugov_init(struct cpufreq_policy *policy)
570{
571 struct sugov_policy *sg_policy;
572 struct sugov_tunables *tunables;
573 unsigned int lat;
574 int ret = 0;
575
576 /* State should be equivalent to EXIT */
577 if (policy->governor_data)
578 return -EBUSY;
579
Chris Redpath99ab82d2017-07-20 16:32:35 +0100580 cpufreq_enable_fast_switch(policy);
581
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200582 sg_policy = sugov_policy_alloc(policy);
Chris Redpath99ab82d2017-07-20 16:32:35 +0100583 if (!sg_policy) {
584 ret = -ENOMEM;
585 goto disable_fast_switch;
586 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200587
Viresh Kumar29d892d72016-11-15 13:53:22 +0530588 ret = sugov_kthread_create(sg_policy);
589 if (ret)
590 goto free_sg_policy;
591
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200592 mutex_lock(&global_tunables_lock);
593
594 if (global_tunables) {
595 if (WARN_ON(have_governor_per_policy())) {
596 ret = -EINVAL;
Viresh Kumar29d892d72016-11-15 13:53:22 +0530597 goto stop_kthread;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200598 }
599 policy->governor_data = sg_policy;
600 sg_policy->tunables = global_tunables;
601
602 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
603 goto out;
604 }
605
606 tunables = sugov_tunables_alloc(sg_policy);
607 if (!tunables) {
608 ret = -ENOMEM;
Viresh Kumar29d892d72016-11-15 13:53:22 +0530609 goto stop_kthread;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200610 }
611
Steve Muckle4152c222016-11-17 10:48:45 +0530612 tunables->up_rate_limit_us = LATENCY_MULTIPLIER;
613 tunables->down_rate_limit_us = LATENCY_MULTIPLIER;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200614 lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
Steve Muckle4152c222016-11-17 10:48:45 +0530615 if (lat) {
616 tunables->up_rate_limit_us *= lat;
617 tunables->down_rate_limit_us *= lat;
618 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200619
620 policy->governor_data = sg_policy;
621 sg_policy->tunables = tunables;
622
623 ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
624 get_governor_parent_kobj(policy), "%s",
625 schedutil_gov.name);
626 if (ret)
627 goto fail;
628
Chris Redpath91a6b312017-05-25 15:04:04 +0100629out:
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200630 mutex_unlock(&global_tunables_lock);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200631 return 0;
632
Chris Redpath91a6b312017-05-25 15:04:04 +0100633fail:
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200634 policy->governor_data = NULL;
635 sugov_tunables_free(tunables);
636
Viresh Kumar29d892d72016-11-15 13:53:22 +0530637 stop_kthread:
638 sugov_kthread_stop(sg_policy);
639
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200640 free_sg_policy:
641 mutex_unlock(&global_tunables_lock);
642
643 sugov_policy_free(sg_policy);
Chris Redpath99ab82d2017-07-20 16:32:35 +0100644
645disable_fast_switch:
646 cpufreq_disable_fast_switch(policy);
647
Viresh Kumar60f05e82016-05-18 17:55:28 +0530648 pr_err("initialization failed (error %d)\n", ret);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200649 return ret;
650}
651
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200652static void sugov_exit(struct cpufreq_policy *policy)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200653{
654 struct sugov_policy *sg_policy = policy->governor_data;
655 struct sugov_tunables *tunables = sg_policy->tunables;
656 unsigned int count;
657
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200658 mutex_lock(&global_tunables_lock);
659
660 count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
661 policy->governor_data = NULL;
662 if (!count)
663 sugov_tunables_free(tunables);
664
665 mutex_unlock(&global_tunables_lock);
666
Viresh Kumar29d892d72016-11-15 13:53:22 +0530667 sugov_kthread_stop(sg_policy);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200668 sugov_policy_free(sg_policy);
Chris Redpath99ab82d2017-07-20 16:32:35 +0100669
670 cpufreq_disable_fast_switch(policy);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200671}
672
673static int sugov_start(struct cpufreq_policy *policy)
674{
675 struct sugov_policy *sg_policy = policy->governor_data;
676 unsigned int cpu;
677
Steve Muckle4152c222016-11-17 10:48:45 +0530678 sg_policy->up_rate_delay_ns =
679 sg_policy->tunables->up_rate_limit_us * NSEC_PER_USEC;
680 sg_policy->down_rate_delay_ns =
681 sg_policy->tunables->down_rate_limit_us * NSEC_PER_USEC;
682 update_min_rate_limit_us(sg_policy);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200683 sg_policy->last_freq_update_time = 0;
684 sg_policy->next_freq = UINT_MAX;
685 sg_policy->work_in_progress = false;
686 sg_policy->need_freq_update = false;
Viresh Kumarafe8d4a2017-03-02 14:03:20 +0530687 sg_policy->cached_raw_freq = 0;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200688
689 for_each_cpu(cpu, policy->cpus) {
690 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
691
Rafael J. Wysockia8fc3152017-03-19 14:30:02 +0100692 memset(sg_cpu, 0, sizeof(*sg_cpu));
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200693 sg_cpu->sg_policy = sg_policy;
Steve Muckle8d408122016-08-25 15:59:17 -0700694 sg_cpu->flags = SCHED_CPUFREQ_DL;
Rafael J. Wysockia8fc3152017-03-19 14:30:02 +0100695 sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
696 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
697 policy_is_shared(policy) ?
698 sugov_update_shared :
699 sugov_update_single);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200700 }
701 return 0;
702}
703
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200704static void sugov_stop(struct cpufreq_policy *policy)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200705{
706 struct sugov_policy *sg_policy = policy->governor_data;
707 unsigned int cpu;
708
709 for_each_cpu(cpu, policy->cpus)
710 cpufreq_remove_update_util_hook(cpu);
711
712 synchronize_sched();
713
Chris Redpath338ad2c2017-07-20 16:34:10 +0100714 if (!policy->fast_switch_enabled) {
715 irq_work_sync(&sg_policy->irq_work);
716 kthread_cancel_work_sync(&sg_policy->work);
717 }
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200718}
719
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200720static void sugov_limits(struct cpufreq_policy *policy)
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200721{
722 struct sugov_policy *sg_policy = policy->governor_data;
723
724 if (!policy->fast_switch_enabled) {
725 mutex_lock(&sg_policy->work_lock);
Viresh Kumarbf2be2d2016-05-18 17:55:31 +0530726 cpufreq_policy_apply_limits(policy);
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200727 mutex_unlock(&sg_policy->work_lock);
728 }
729
730 sg_policy->need_freq_update = true;
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200731}
732
733static struct cpufreq_governor schedutil_gov = {
734 .name = "schedutil",
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200735 .owner = THIS_MODULE,
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200736 .init = sugov_init,
737 .exit = sugov_exit,
738 .start = sugov_start,
739 .stop = sugov_stop,
740 .limits = sugov_limits,
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200741};
742
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200743#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
744struct cpufreq_governor *cpufreq_default_governor(void)
745{
746 return &schedutil_gov;
747}
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +0200748#endif
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200749
750static int __init sugov_register(void)
751{
752 return cpufreq_register_governor(&schedutil_gov);
753}
754fs_initcall(sugov_register);