blob: bc55f3f905146d13253889d24c0e2e468d438170 [file] [log] [blame]
Rafael J. Wysockib1d09762016-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 Kumar87ecf322016-05-18 17:55:28 +053012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020014#include <linux/cpufreq.h>
Viresh Kumara231c652016-11-15 13:53:22 +053015#include <linux/kthread.h>
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020016#include <linux/slab.h>
17#include <trace/events/power.h>
Jonathan Avila703e2432017-08-10 13:49:49 -070018#include <linux/sched/sysctl.h>
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020019#include "sched.h"
Juri Lellic6e94382016-12-14 16:10:10 +000020#include "tune.h"
21
Kyle Yane2486b72017-08-25 14:36:53 -070022#ifdef CONFIG_SCHED_WALT
Juri Lellic6e94382016-12-14 16:10:10 +000023unsigned long boosted_cpu_util(int cpu);
Kyle Yane2486b72017-08-25 14:36:53 -070024#endif
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020025
Viresh Kumara231c652016-11-15 13:53:22 +053026#define SUGOV_KTHREAD_PRIORITY 50
27
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020028struct sugov_tunables {
29 struct gov_attr_set attr_set;
30 unsigned int rate_limit_us;
Saravana Kannan3ca7d7a2017-09-11 12:33:54 -070031 unsigned int hispeed_load;
Rohit Gupta30249632017-02-02 18:39:07 -080032 unsigned int hispeed_freq;
Saravana Kannand0a7c6b2017-05-23 17:26:32 -070033 bool pl;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020034};
35
36struct sugov_policy {
37 struct cpufreq_policy *policy;
38
39 struct sugov_tunables *tunables;
40 struct list_head tunables_hook;
41
42 raw_spinlock_t update_lock; /* For shared policies */
43 u64 last_freq_update_time;
44 s64 freq_update_delay_ns;
Saravana Kannan0f34ee92017-06-28 21:44:14 -070045 u64 last_ws;
46 u64 curr_cycles;
47 u64 last_cyc_update_time;
48 unsigned long avg_cap;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020049 unsigned int next_freq;
Viresh Kumar55a6d462017-03-02 14:03:20 +053050 unsigned int cached_raw_freq;
Rohit Gupta30249632017-02-02 18:39:07 -080051 unsigned long hispeed_util;
52 unsigned long max;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020053
54 /* The next fields are only needed if fast switch cannot be used. */
55 struct irq_work irq_work;
Viresh Kumara231c652016-11-15 13:53:22 +053056 struct kthread_work work;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020057 struct mutex work_lock;
Viresh Kumara231c652016-11-15 13:53:22 +053058 struct kthread_worker worker;
59 struct task_struct *thread;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020060 bool work_in_progress;
61
62 bool need_freq_update;
63};
64
65struct sugov_cpu {
66 struct update_util_data update_util;
67 struct sugov_policy *sg_policy;
68
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +020069 unsigned long iowait_boost;
70 unsigned long iowait_boost_max;
71 u64 last_update;
Steve Muckled7439bc2016-07-13 13:25:26 -070072
Saravana Kannan05649862017-05-04 19:03:53 -070073 struct sched_walt_cpu_load walt_load;
74
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020075 /* The fields below are only needed when sharing a policy. */
76 unsigned long util;
77 unsigned long max;
Rafael J. Wysockic4568722016-08-16 22:14:55 +020078 unsigned int flags;
Vikram Mulukutla857cffa2017-05-04 19:47:06 -070079 unsigned int cpu;
Chris Redpath595ae4a2017-05-25 15:24:58 +010080
81 /* The field below is for single-CPU policies only. */
82#ifdef CONFIG_NO_HZ_COMMON
83 unsigned long saved_idle_calls;
84#endif
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020085};
86
87static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
Saravana Kannan7070a9e2017-08-24 17:02:49 -070088static unsigned int stale_ns;
Rohit Guptaae5c8d22017-09-25 14:23:48 -070089static DEFINE_PER_CPU(struct sugov_tunables *, cached_tunables);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020090
91/************************ Governor internals ***********************/
92
93static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
94{
95 s64 delta_ns;
96
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020097 if (unlikely(sg_policy->need_freq_update)) {
98 sg_policy->need_freq_update = false;
99 /*
100 * This happens when limits change, so forget the previous
101 * next_freq value and force an update.
102 */
103 sg_policy->next_freq = UINT_MAX;
104 return true;
105 }
106
107 delta_ns = time - sg_policy->last_freq_update_time;
108 return delta_ns >= sg_policy->freq_update_delay_ns;
109}
110
111static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
112 unsigned int next_freq)
113{
114 struct cpufreq_policy *policy = sg_policy->policy;
115
Chris Redpath6702ce12017-05-25 15:27:07 +0100116 if (sg_policy->next_freq == next_freq)
117 return;
118
119 sg_policy->next_freq = next_freq;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200120 sg_policy->last_freq_update_time = time;
121
122 if (policy->fast_switch_enabled) {
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200123 next_freq = cpufreq_driver_fast_switch(policy, next_freq);
124 if (next_freq == CPUFREQ_ENTRY_INVALID)
125 return;
126
127 policy->cur = next_freq;
128 trace_cpu_frequency(next_freq, smp_processor_id());
Chris Redpath6702ce12017-05-25 15:27:07 +0100129 } else {
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200130 sg_policy->work_in_progress = true;
131 irq_work_queue(&sg_policy->irq_work);
132 }
133}
134
Rohit Gupta30249632017-02-02 18:39:07 -0800135#define TARGET_LOAD 80
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200136/**
137 * get_next_freq - Compute a new frequency for a given cpufreq policy.
Viresh Kumare74f7f12017-03-02 14:03:21 +0530138 * @sg_policy: schedutil policy object to compute the new frequency for.
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200139 * @util: Current CPU utilization.
140 * @max: CPU capacity.
141 *
142 * If the utilization is frequency-invariant, choose the new frequency to be
143 * proportional to it, that is
144 *
145 * next_freq = C * max_freq * util / max
146 *
147 * Otherwise, approximate the would-be frequency-invariant utilization by
148 * util_raw * (curr_freq / max_freq) which leads to
149 *
150 * next_freq = C * curr_freq * util_raw / max
151 *
152 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
Steve Muckled7439bc2016-07-13 13:25:26 -0700153 *
154 * The lowest driver-supported frequency which is equal or greater than the raw
155 * next_freq (as calculated above) is returned, subject to policy min/max and
156 * cpufreq driver limitations.
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200157 */
Viresh Kumare74f7f12017-03-02 14:03:21 +0530158static unsigned int get_next_freq(struct sugov_policy *sg_policy,
159 unsigned long util, unsigned long max)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200160{
Steve Muckled7439bc2016-07-13 13:25:26 -0700161 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200162 unsigned int freq = arch_scale_freq_invariant() ?
163 policy->cpuinfo.max_freq : policy->cur;
164
Steve Muckled7439bc2016-07-13 13:25:26 -0700165 freq = (freq + (freq >> 2)) * util / max;
166
Viresh Kumar55a6d462017-03-02 14:03:20 +0530167 if (freq == sg_policy->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
Steve Muckled7439bc2016-07-13 13:25:26 -0700168 return sg_policy->next_freq;
Viresh Kumar55a6d462017-03-02 14:03:20 +0530169 sg_policy->cached_raw_freq = freq;
Steve Muckled7439bc2016-07-13 13:25:26 -0700170 return cpufreq_driver_resolve_freq(policy, freq);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200171}
172
Vikram Mulukutla857cffa2017-05-04 19:47:06 -0700173static void sugov_get_util(unsigned long *util, unsigned long *max, int cpu)
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200174{
Vikram Mulukutla857cffa2017-05-04 19:47:06 -0700175 struct rq *rq = cpu_rq(cpu);
Steve Muckle097cf682016-08-26 11:40:47 -0700176 unsigned long cfs_max;
Saravana Kannan05649862017-05-04 19:03:53 -0700177 struct sugov_cpu *loadcpu = &per_cpu(sugov_cpu, cpu);
Steve Muckle097cf682016-08-26 11:40:47 -0700178
Vikram Mulukutla857cffa2017-05-04 19:47:06 -0700179 cfs_max = arch_scale_cpu_capacity(NULL, cpu);
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200180
181 *util = min(rq->cfs.avg.util_avg, cfs_max);
182 *max = cfs_max;
Saravana Kannan05649862017-05-04 19:03:53 -0700183
184 *util = cpu_util_freq(cpu, &loadcpu->walt_load);
Chris Redpatha6d67352017-03-24 17:37:28 +0000185 *util = boosted_cpu_util(cpu);
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200186}
187
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200188static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
189 unsigned int flags)
190{
191 if (flags & SCHED_CPUFREQ_IOWAIT) {
192 sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
193 } else if (sg_cpu->iowait_boost) {
194 s64 delta_ns = time - sg_cpu->last_update;
195
196 /* Clear iowait_boost if the CPU apprears to have been idle. */
197 if (delta_ns > TICK_NSEC)
198 sg_cpu->iowait_boost = 0;
199 }
200}
201
202static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util,
203 unsigned long *max)
204{
205 unsigned long boost_util = sg_cpu->iowait_boost;
206 unsigned long boost_max = sg_cpu->iowait_boost_max;
207
208 if (!boost_util)
209 return;
210
211 if (*util * boost_max < *max * boost_util) {
212 *util = boost_util;
213 *max = boost_max;
214 }
215 sg_cpu->iowait_boost >>= 1;
216}
217
Saravana Kannan0f34ee92017-06-28 21:44:14 -0700218static unsigned long freq_to_util(struct sugov_policy *sg_policy,
219 unsigned int freq)
220{
221 return mult_frac(sg_policy->max, freq,
222 sg_policy->policy->cpuinfo.max_freq);
223}
224
225#define KHZ 1000
226static void sugov_track_cycles(struct sugov_policy *sg_policy,
227 unsigned int prev_freq,
228 u64 upto)
229{
230 u64 delta_ns, cycles;
Jonathan Avila703e2432017-08-10 13:49:49 -0700231
232 if (unlikely(!sysctl_sched_use_walt_cpu_util))
233 return;
234
Saravana Kannan0f34ee92017-06-28 21:44:14 -0700235 /* Track cycles in current window */
236 delta_ns = upto - sg_policy->last_cyc_update_time;
237 cycles = (prev_freq * delta_ns) / (NSEC_PER_SEC / KHZ);
238 sg_policy->curr_cycles += cycles;
239 sg_policy->last_cyc_update_time = upto;
240}
241
242static void sugov_calc_avg_cap(struct sugov_policy *sg_policy, u64 curr_ws,
243 unsigned int prev_freq)
244{
245 u64 last_ws = sg_policy->last_ws;
246 unsigned int avg_freq;
247
Jonathan Avila703e2432017-08-10 13:49:49 -0700248 if (unlikely(!sysctl_sched_use_walt_cpu_util))
249 return;
250
Saravana Kannan0f34ee92017-06-28 21:44:14 -0700251 WARN_ON(curr_ws < last_ws);
252 if (curr_ws <= last_ws)
253 return;
254
255 /* If we skipped some windows */
256 if (curr_ws > (last_ws + sched_ravg_window)) {
257 avg_freq = prev_freq;
258 /* Reset tracking history */
259 sg_policy->last_cyc_update_time = curr_ws;
260 } else {
261 sugov_track_cycles(sg_policy, prev_freq, curr_ws);
262 avg_freq = sg_policy->curr_cycles;
263 avg_freq /= sched_ravg_window / (NSEC_PER_SEC / KHZ);
264 }
265 sg_policy->avg_cap = freq_to_util(sg_policy, avg_freq);
266 sg_policy->curr_cycles = 0;
267 sg_policy->last_ws = curr_ws;
268}
269
Rohit Gupta30249632017-02-02 18:39:07 -0800270#define NL_RATIO 75
Saravana Kannan3ca7d7a2017-09-11 12:33:54 -0700271#define DEFAULT_HISPEED_LOAD 90
Rohit Gupta30249632017-02-02 18:39:07 -0800272static void sugov_walt_adjust(struct sugov_cpu *sg_cpu, unsigned long *util,
273 unsigned long *max)
274{
275 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
Rohit Gupta30249632017-02-02 18:39:07 -0800276 bool is_migration = sg_cpu->flags & SCHED_CPUFREQ_INTERCLUSTER_MIG;
277 unsigned long nl = sg_cpu->walt_load.nl;
278 unsigned long cpu_util = sg_cpu->util;
279 bool is_hiload;
280
Jonathan Avila703e2432017-08-10 13:49:49 -0700281 if (unlikely(!sysctl_sched_use_walt_cpu_util))
282 return;
283
Saravana Kannan36faa282017-06-28 21:44:56 -0700284 is_hiload = (cpu_util >= mult_frac(sg_policy->avg_cap,
Saravana Kannan3ca7d7a2017-09-11 12:33:54 -0700285 sg_policy->tunables->hispeed_load,
Rohit Gupta30249632017-02-02 18:39:07 -0800286 100));
287
Rohit Gupta5573e732017-06-21 10:08:07 -0700288 if (is_hiload && !is_migration)
Rohit Gupta30249632017-02-02 18:39:07 -0800289 *util = max(*util, sg_policy->hispeed_util);
Rohit Gupta30249632017-02-02 18:39:07 -0800290
291 if (is_hiload && nl >= mult_frac(cpu_util, NL_RATIO, 100))
292 *util = *max;
293
Saravana Kannand0a7c6b2017-05-23 17:26:32 -0700294 if (sg_policy->tunables->pl)
295 *util = max(*util, sg_cpu->walt_load.pl);
Rohit Gupta30249632017-02-02 18:39:07 -0800296}
297
Chris Redpath595ae4a2017-05-25 15:24:58 +0100298#ifdef CONFIG_NO_HZ_COMMON
299static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu)
300{
301 unsigned long idle_calls = tick_nohz_get_idle_calls();
302 bool ret = idle_calls == sg_cpu->saved_idle_calls;
303
304 sg_cpu->saved_idle_calls = idle_calls;
305 return ret;
306}
307#else
308static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; }
309#endif /* CONFIG_NO_HZ_COMMON */
310
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200311static void sugov_update_single(struct update_util_data *hook, u64 time,
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200312 unsigned int flags)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200313{
314 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
315 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
316 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200317 unsigned long util, max;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200318 unsigned int next_f;
Chris Redpath595ae4a2017-05-25 15:24:58 +0100319 bool busy;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200320
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200321 sugov_set_iowait_boost(sg_cpu, time, flags);
322 sg_cpu->last_update = time;
323
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200324 if (!sugov_should_update_freq(sg_policy, time))
325 return;
326
Chris Redpath595ae4a2017-05-25 15:24:58 +0100327 busy = sugov_cpu_is_busy(sg_cpu);
Saravana Kannan76ef1712017-05-04 19:44:36 -0700328 flags &= ~SCHED_CPUFREQ_RT_DL;
329
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200330 if (flags & SCHED_CPUFREQ_RT_DL) {
331 next_f = policy->cpuinfo.max_freq;
332 } else {
Vikram Mulukutla857cffa2017-05-04 19:47:06 -0700333 sugov_get_util(&util, &max, sg_cpu->cpu);
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200334 sugov_iowait_boost(sg_cpu, &util, &max);
Saravana Kannan0f34ee92017-06-28 21:44:14 -0700335 sugov_calc_avg_cap(sg_policy, sg_cpu->walt_load.ws,
336 sg_policy->policy->cur);
Rohit Gupta30249632017-02-02 18:39:07 -0800337 sugov_walt_adjust(sg_cpu, &util, &max);
Viresh Kumare74f7f12017-03-02 14:03:21 +0530338 next_f = get_next_freq(sg_policy, util, max);
Chris Redpath595ae4a2017-05-25 15:24:58 +0100339 /*
340 * Do not reduce the frequency if the CPU has not been idle
341 * recently, as the reduction is likely to be premature then.
342 */
343 if (busy && next_f < sg_policy->next_freq)
344 next_f = sg_policy->next_freq;
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200345 }
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200346 sugov_update_commit(sg_policy, time, next_f);
347}
348
Chris Redpath39151862017-05-25 15:22:59 +0100349static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200350{
Steve Muckled7439bc2016-07-13 13:25:26 -0700351 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200352 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200353 u64 last_freq_update_time = sg_policy->last_freq_update_time;
Chris Redpath39151862017-05-25 15:22:59 +0100354 unsigned long util = 0, max = 1;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200355 unsigned int j;
356
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200357 for_each_cpu(j, policy->cpus) {
Chris Redpath39151862017-05-25 15:22:59 +0100358 struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200359 unsigned long j_util, j_max;
360 s64 delta_ns;
361
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200362 /*
363 * If the CPU utilization was last updated before the previous
364 * frequency update and the time elapsed between the last update
365 * of the CPU utilization and the last frequency update is long
366 * enough, don't take the CPU into account as it probably is
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200367 * idle now (and clear iowait_boost for it).
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200368 */
369 delta_ns = last_freq_update_time - j_sg_cpu->last_update;
Saravana Kannan7070a9e2017-08-24 17:02:49 -0700370 if (delta_ns > stale_ns) {
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200371 j_sg_cpu->iowait_boost = 0;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200372 continue;
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200373 }
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200374 if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
Chris Redpath39151862017-05-25 15:22:59 +0100375 return policy->cpuinfo.max_freq;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200376
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200377 j_util = j_sg_cpu->util;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200378 j_max = j_sg_cpu->max;
Saravana Kannan750f9702017-10-12 15:56:12 -0700379 if (j_util * max >= j_max * util) {
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200380 util = j_util;
381 max = j_max;
382 }
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200383
384 sugov_iowait_boost(j_sg_cpu, &util, &max);
Rohit Gupta30249632017-02-02 18:39:07 -0800385 sugov_walt_adjust(j_sg_cpu, &util, &max);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200386 }
387
Viresh Kumare74f7f12017-03-02 14:03:21 +0530388 return get_next_freq(sg_policy, util, max);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200389}
390
391static void sugov_update_shared(struct update_util_data *hook, u64 time,
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200392 unsigned int flags)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200393{
394 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
395 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
Rohit Gupta30249632017-02-02 18:39:07 -0800396 unsigned long util, max, hs_util;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200397 unsigned int next_f;
398
Vikram Mulukutla857cffa2017-05-04 19:47:06 -0700399 sugov_get_util(&util, &max, sg_cpu->cpu);
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200400
Saravana Kannan76ef1712017-05-04 19:44:36 -0700401 flags &= ~SCHED_CPUFREQ_RT_DL;
402
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200403 raw_spin_lock(&sg_policy->update_lock);
404
Rohit Gupta30249632017-02-02 18:39:07 -0800405 if (sg_policy->max != max) {
Saravana Kannanaf541e12017-06-28 20:16:15 -0700406 sg_policy->max = max;
407 hs_util = freq_to_util(sg_policy,
408 sg_policy->tunables->hispeed_freq);
Rohit Gupta30249632017-02-02 18:39:07 -0800409 hs_util = mult_frac(hs_util, TARGET_LOAD, 100);
410 sg_policy->hispeed_util = hs_util;
Rohit Gupta30249632017-02-02 18:39:07 -0800411 }
412
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200413 sg_cpu->util = util;
414 sg_cpu->max = max;
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200415 sg_cpu->flags = flags;
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200416
417 sugov_set_iowait_boost(sg_cpu, time, flags);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200418 sg_cpu->last_update = time;
419
Saravana Kannan0f34ee92017-06-28 21:44:14 -0700420 sugov_calc_avg_cap(sg_policy, sg_cpu->walt_load.ws,
421 sg_policy->policy->cur);
422
Rohit Gupta2cd0e9e2017-08-07 10:50:35 -0700423 trace_sugov_util_update(sg_cpu->cpu, sg_cpu->util, sg_policy->avg_cap,
424 max, sg_cpu->walt_load.nl,
Saravana Kannan12a35ed2017-03-27 15:46:28 -0700425 sg_cpu->walt_load.pl, flags);
426
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200427 if (sugov_should_update_freq(sg_policy, time)) {
Kyle Yane2486b72017-08-25 14:36:53 -0700428 if (flags & SCHED_CPUFREQ_RT_DL)
Chris Redpath39151862017-05-25 15:22:59 +0100429 next_f = sg_policy->policy->cpuinfo.max_freq;
430 else
431 next_f = sugov_next_freq_shared(sg_cpu);
432
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200433 sugov_update_commit(sg_policy, time, next_f);
434 }
435
436 raw_spin_unlock(&sg_policy->update_lock);
437}
438
Viresh Kumara231c652016-11-15 13:53:22 +0530439static void sugov_work(struct kthread_work *work)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200440{
441 struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
Saravana Kannan01adff122017-07-11 17:39:53 -0700442 unsigned long flags;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200443
444 mutex_lock(&sg_policy->work_lock);
Saravana Kannan01adff122017-07-11 17:39:53 -0700445 raw_spin_lock_irqsave(&sg_policy->update_lock, flags);
Saravana Kannan0f34ee92017-06-28 21:44:14 -0700446 sugov_track_cycles(sg_policy, sg_policy->policy->cur,
Stephen Boyd24c18122017-08-15 10:39:25 -0700447 ktime_get_ns());
Saravana Kannan01adff122017-07-11 17:39:53 -0700448 raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200449 __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
450 CPUFREQ_RELATION_L);
451 mutex_unlock(&sg_policy->work_lock);
452
453 sg_policy->work_in_progress = false;
454}
455
456static void sugov_irq_work(struct irq_work *irq_work)
457{
458 struct sugov_policy *sg_policy;
459
460 sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
Viresh Kumara231c652016-11-15 13:53:22 +0530461
462 /*
Viresh Kumar75526a22016-11-24 13:51:11 +0530463 * For RT and deadline tasks, the schedutil governor shoots the
464 * frequency to maximum. Special care must be taken to ensure that this
465 * kthread doesn't result in the same behavior.
Viresh Kumara231c652016-11-15 13:53:22 +0530466 *
467 * This is (mostly) guaranteed by the work_in_progress flag. The flag is
Viresh Kumar75526a22016-11-24 13:51:11 +0530468 * updated only at the end of the sugov_work() function and before that
469 * the schedutil governor rejects all other frequency scaling requests.
Viresh Kumara231c652016-11-15 13:53:22 +0530470 *
Viresh Kumar75526a22016-11-24 13:51:11 +0530471 * There is a very rare case though, where the RT thread yields right
Viresh Kumara231c652016-11-15 13:53:22 +0530472 * after the work_in_progress flag is cleared. The effects of that are
473 * neglected for now.
474 */
475 kthread_queue_work(&sg_policy->worker, &sg_policy->work);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200476}
477
478/************************** sysfs interface ************************/
479
480static struct sugov_tunables *global_tunables;
481static DEFINE_MUTEX(global_tunables_lock);
482
483static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
484{
485 return container_of(attr_set, struct sugov_tunables, attr_set);
486}
487
488static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
489{
490 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
491
492 return sprintf(buf, "%u\n", tunables->rate_limit_us);
493}
494
495static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
496 size_t count)
497{
498 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
499 struct sugov_policy *sg_policy;
500 unsigned int rate_limit_us;
501
502 if (kstrtouint(buf, 10, &rate_limit_us))
503 return -EINVAL;
504
505 tunables->rate_limit_us = rate_limit_us;
506
507 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
508 sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
509
510 return count;
511}
512
Saravana Kannan3ca7d7a2017-09-11 12:33:54 -0700513static ssize_t hispeed_load_show(struct gov_attr_set *attr_set, char *buf)
514{
515 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
516
Saravana Kannan95e1d762017-10-11 17:46:11 -0700517 return scnprintf(buf, PAGE_SIZE, "%u\n", tunables->hispeed_load);
Saravana Kannan3ca7d7a2017-09-11 12:33:54 -0700518}
519
520static ssize_t hispeed_load_store(struct gov_attr_set *attr_set,
521 const char *buf, size_t count)
522{
523 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
524
525 if (kstrtouint(buf, 10, &tunables->hispeed_load))
526 return -EINVAL;
527
528 tunables->hispeed_load = min(100U, tunables->hispeed_load);
529
530 return count;
531}
532
Rohit Gupta30249632017-02-02 18:39:07 -0800533static ssize_t hispeed_freq_show(struct gov_attr_set *attr_set, char *buf)
534{
535 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
536
Saravana Kannan95e1d762017-10-11 17:46:11 -0700537 return scnprintf(buf, PAGE_SIZE, "%u\n", tunables->hispeed_freq);
Rohit Gupta30249632017-02-02 18:39:07 -0800538}
539
540static ssize_t hispeed_freq_store(struct gov_attr_set *attr_set,
541 const char *buf, size_t count)
542{
543 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
544 unsigned int val;
545 struct sugov_policy *sg_policy;
546 unsigned long hs_util;
Saravana Kannan01adff122017-07-11 17:39:53 -0700547 unsigned long flags;
Rohit Gupta30249632017-02-02 18:39:07 -0800548
549 if (kstrtouint(buf, 10, &val))
550 return -EINVAL;
551
552 tunables->hispeed_freq = val;
553 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) {
Saravana Kannan01adff122017-07-11 17:39:53 -0700554 raw_spin_lock_irqsave(&sg_policy->update_lock, flags);
Saravana Kannanaf541e12017-06-28 20:16:15 -0700555 hs_util = freq_to_util(sg_policy,
556 sg_policy->tunables->hispeed_freq);
Rohit Gupta30249632017-02-02 18:39:07 -0800557 hs_util = mult_frac(hs_util, TARGET_LOAD, 100);
558 sg_policy->hispeed_util = hs_util;
Saravana Kannan01adff122017-07-11 17:39:53 -0700559 raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags);
Rohit Gupta30249632017-02-02 18:39:07 -0800560 }
561
562 return count;
563}
564
Saravana Kannand0a7c6b2017-05-23 17:26:32 -0700565static ssize_t pl_show(struct gov_attr_set *attr_set, char *buf)
566{
567 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
568
Saravana Kannan95e1d762017-10-11 17:46:11 -0700569 return scnprintf(buf, PAGE_SIZE, "%u\n", tunables->pl);
Saravana Kannand0a7c6b2017-05-23 17:26:32 -0700570}
571
572static ssize_t pl_store(struct gov_attr_set *attr_set, const char *buf,
573 size_t count)
574{
575 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
576
577 if (kstrtobool(buf, &tunables->pl))
578 return -EINVAL;
579
580 return count;
581}
582
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200583static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
Saravana Kannan3ca7d7a2017-09-11 12:33:54 -0700584static struct governor_attr hispeed_load = __ATTR_RW(hispeed_load);
Rohit Gupta30249632017-02-02 18:39:07 -0800585static struct governor_attr hispeed_freq = __ATTR_RW(hispeed_freq);
Saravana Kannand0a7c6b2017-05-23 17:26:32 -0700586static struct governor_attr pl = __ATTR_RW(pl);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200587
588static struct attribute *sugov_attributes[] = {
589 &rate_limit_us.attr,
Saravana Kannan3ca7d7a2017-09-11 12:33:54 -0700590 &hispeed_load.attr,
Rohit Gupta30249632017-02-02 18:39:07 -0800591 &hispeed_freq.attr,
Saravana Kannand0a7c6b2017-05-23 17:26:32 -0700592 &pl.attr,
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200593 NULL
594};
595
596static struct kobj_type sugov_tunables_ktype = {
597 .default_attrs = sugov_attributes,
598 .sysfs_ops = &governor_sysfs_ops,
599};
600
601/********************** cpufreq governor interface *********************/
602
603static struct cpufreq_governor schedutil_gov;
604
605static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
606{
607 struct sugov_policy *sg_policy;
608
609 sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
610 if (!sg_policy)
611 return NULL;
612
613 sg_policy->policy = policy;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200614 raw_spin_lock_init(&sg_policy->update_lock);
615 return sg_policy;
616}
617
618static void sugov_policy_free(struct sugov_policy *sg_policy)
619{
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200620 kfree(sg_policy);
621}
622
Viresh Kumara231c652016-11-15 13:53:22 +0530623static int sugov_kthread_create(struct sugov_policy *sg_policy)
624{
625 struct task_struct *thread;
626 struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO / 2 };
627 struct cpufreq_policy *policy = sg_policy->policy;
628 int ret;
629
630 /* kthread only required for slow path */
631 if (policy->fast_switch_enabled)
632 return 0;
633
634 kthread_init_work(&sg_policy->work, sugov_work);
635 kthread_init_worker(&sg_policy->worker);
636 thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
637 "sugov:%d",
638 cpumask_first(policy->related_cpus));
639 if (IS_ERR(thread)) {
640 pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
641 return PTR_ERR(thread);
642 }
643
644 ret = sched_setscheduler_nocheck(thread, SCHED_FIFO, &param);
645 if (ret) {
646 kthread_stop(thread);
647 pr_warn("%s: failed to set SCHED_FIFO\n", __func__);
648 return ret;
649 }
650
651 sg_policy->thread = thread;
652 kthread_bind_mask(thread, policy->related_cpus);
Viresh Kumar0ced0be2016-11-15 13:53:23 +0530653 init_irq_work(&sg_policy->irq_work, sugov_irq_work);
654 mutex_init(&sg_policy->work_lock);
655
Viresh Kumara231c652016-11-15 13:53:22 +0530656 wake_up_process(thread);
657
658 return 0;
659}
660
661static void sugov_kthread_stop(struct sugov_policy *sg_policy)
662{
663 /* kthread only required for slow path */
664 if (sg_policy->policy->fast_switch_enabled)
665 return;
666
667 kthread_flush_worker(&sg_policy->worker);
668 kthread_stop(sg_policy->thread);
Viresh Kumar0ced0be2016-11-15 13:53:23 +0530669 mutex_destroy(&sg_policy->work_lock);
Viresh Kumara231c652016-11-15 13:53:22 +0530670}
671
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200672static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
673{
674 struct sugov_tunables *tunables;
675
676 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
677 if (tunables) {
678 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
679 if (!have_governor_per_policy())
680 global_tunables = tunables;
681 }
682 return tunables;
683}
684
Rohit Guptaae5c8d22017-09-25 14:23:48 -0700685static void sugov_tunables_save(struct cpufreq_policy *policy,
686 struct sugov_tunables *tunables)
687{
688 int cpu;
689 struct sugov_tunables *cached = per_cpu(cached_tunables, policy->cpu);
690
691 if (!have_governor_per_policy())
692 return;
693
694 if (!cached) {
695 cached = kzalloc(sizeof(*tunables), GFP_KERNEL);
696 if (!cached) {
697 pr_warn("Couldn't allocate tunables for caching\n");
698 return;
699 }
700 for_each_cpu(cpu, policy->related_cpus)
701 per_cpu(cached_tunables, cpu) = cached;
702 }
703
704 cached->pl = tunables->pl;
705 cached->hispeed_load = tunables->hispeed_load;
706 cached->hispeed_freq = tunables->hispeed_freq;
707 cached->rate_limit_us = tunables->rate_limit_us;
708}
709
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200710static void sugov_tunables_free(struct sugov_tunables *tunables)
711{
712 if (!have_governor_per_policy())
713 global_tunables = NULL;
714
715 kfree(tunables);
716}
717
Rohit Guptaae5c8d22017-09-25 14:23:48 -0700718static void sugov_tunables_restore(struct cpufreq_policy *policy)
719{
720 struct sugov_policy *sg_policy = policy->governor_data;
721 struct sugov_tunables *tunables = sg_policy->tunables;
722 struct sugov_tunables *cached = per_cpu(cached_tunables, policy->cpu);
723
724 if (!cached)
725 return;
726
727 tunables->pl = cached->pl;
728 tunables->hispeed_load = cached->hispeed_load;
729 tunables->hispeed_freq = cached->hispeed_freq;
730 tunables->rate_limit_us = cached->rate_limit_us;
731 sg_policy->freq_update_delay_ns =
732 tunables->rate_limit_us * NSEC_PER_USEC;
733}
734
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200735static int sugov_init(struct cpufreq_policy *policy)
736{
737 struct sugov_policy *sg_policy;
738 struct sugov_tunables *tunables;
739 unsigned int lat;
740 int ret = 0;
741
742 /* State should be equivalent to EXIT */
743 if (policy->governor_data)
744 return -EBUSY;
745
Viresh Kumar6d5551b2016-11-15 13:53:21 +0530746 cpufreq_enable_fast_switch(policy);
747
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200748 sg_policy = sugov_policy_alloc(policy);
Viresh Kumar6d5551b2016-11-15 13:53:21 +0530749 if (!sg_policy) {
750 ret = -ENOMEM;
751 goto disable_fast_switch;
752 }
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200753
Viresh Kumara231c652016-11-15 13:53:22 +0530754 ret = sugov_kthread_create(sg_policy);
755 if (ret)
756 goto free_sg_policy;
757
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200758 mutex_lock(&global_tunables_lock);
759
760 if (global_tunables) {
761 if (WARN_ON(have_governor_per_policy())) {
762 ret = -EINVAL;
Viresh Kumara231c652016-11-15 13:53:22 +0530763 goto stop_kthread;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200764 }
765 policy->governor_data = sg_policy;
766 sg_policy->tunables = global_tunables;
767
768 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
769 goto out;
770 }
771
772 tunables = sugov_tunables_alloc(sg_policy);
773 if (!tunables) {
774 ret = -ENOMEM;
Viresh Kumara231c652016-11-15 13:53:22 +0530775 goto stop_kthread;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200776 }
777
778 tunables->rate_limit_us = LATENCY_MULTIPLIER;
Saravana Kannan3ca7d7a2017-09-11 12:33:54 -0700779 tunables->hispeed_load = DEFAULT_HISPEED_LOAD;
Rohit Gupta30249632017-02-02 18:39:07 -0800780 tunables->hispeed_freq = 0;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200781 lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
782 if (lat)
783 tunables->rate_limit_us *= lat;
784
785 policy->governor_data = sg_policy;
786 sg_policy->tunables = tunables;
Saravana Kannan7070a9e2017-08-24 17:02:49 -0700787 stale_ns = sched_ravg_window + (sched_ravg_window >> 3);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200788
Rohit Guptaae5c8d22017-09-25 14:23:48 -0700789 sugov_tunables_restore(policy);
790
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200791 ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
792 get_governor_parent_kobj(policy), "%s",
793 schedutil_gov.name);
794 if (ret)
795 goto fail;
796
Viresh Kumarb5b11602016-11-15 13:53:20 +0530797out:
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200798 mutex_unlock(&global_tunables_lock);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200799 return 0;
800
Viresh Kumarb5b11602016-11-15 13:53:20 +0530801fail:
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200802 policy->governor_data = NULL;
803 sugov_tunables_free(tunables);
804
Viresh Kumara231c652016-11-15 13:53:22 +0530805stop_kthread:
806 sugov_kthread_stop(sg_policy);
807
Viresh Kumarb5b11602016-11-15 13:53:20 +0530808free_sg_policy:
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200809 mutex_unlock(&global_tunables_lock);
810
811 sugov_policy_free(sg_policy);
Viresh Kumar6d5551b2016-11-15 13:53:21 +0530812
813disable_fast_switch:
814 cpufreq_disable_fast_switch(policy);
815
Viresh Kumar87ecf322016-05-18 17:55:28 +0530816 pr_err("initialization failed (error %d)\n", ret);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200817 return ret;
818}
819
Rafael J. Wysocki48f5adc2016-06-02 23:24:15 +0200820static void sugov_exit(struct cpufreq_policy *policy)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200821{
822 struct sugov_policy *sg_policy = policy->governor_data;
823 struct sugov_tunables *tunables = sg_policy->tunables;
824 unsigned int count;
825
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200826 mutex_lock(&global_tunables_lock);
827
828 count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
829 policy->governor_data = NULL;
Rohit Guptaae5c8d22017-09-25 14:23:48 -0700830 if (!count) {
831 sugov_tunables_save(policy, tunables);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200832 sugov_tunables_free(tunables);
Rohit Guptaae5c8d22017-09-25 14:23:48 -0700833 }
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200834
835 mutex_unlock(&global_tunables_lock);
836
Viresh Kumara231c652016-11-15 13:53:22 +0530837 sugov_kthread_stop(sg_policy);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200838 sugov_policy_free(sg_policy);
Viresh Kumar6d5551b2016-11-15 13:53:21 +0530839 cpufreq_disable_fast_switch(policy);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200840}
841
842static int sugov_start(struct cpufreq_policy *policy)
843{
844 struct sugov_policy *sg_policy = policy->governor_data;
845 unsigned int cpu;
846
847 sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
848 sg_policy->last_freq_update_time = 0;
849 sg_policy->next_freq = UINT_MAX;
850 sg_policy->work_in_progress = false;
851 sg_policy->need_freq_update = false;
Viresh Kumar55a6d462017-03-02 14:03:20 +0530852 sg_policy->cached_raw_freq = 0;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200853
854 for_each_cpu(cpu, policy->cpus) {
855 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
856
Rafael J. Wysocki7922ae42017-03-19 14:30:02 +0100857 memset(sg_cpu, 0, sizeof(*sg_cpu));
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200858 sg_cpu->sg_policy = sg_policy;
Vikram Mulukutla857cffa2017-05-04 19:47:06 -0700859 sg_cpu->cpu = cpu;
Rafael J. Wysocki7922ae42017-03-19 14:30:02 +0100860 sg_cpu->flags = SCHED_CPUFREQ_RT;
861 sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
Vikram Mulukutlaeb843722017-07-06 10:05:52 -0700862 }
863
864 for_each_cpu(cpu, policy->cpus) {
865 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
866
Rafael J. Wysocki7922ae42017-03-19 14:30:02 +0100867 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
868 policy_is_shared(policy) ?
869 sugov_update_shared :
870 sugov_update_single);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200871 }
872 return 0;
873}
874
Rafael J. Wysocki48f5adc2016-06-02 23:24:15 +0200875static void sugov_stop(struct cpufreq_policy *policy)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200876{
877 struct sugov_policy *sg_policy = policy->governor_data;
878 unsigned int cpu;
879
880 for_each_cpu(cpu, policy->cpus)
881 cpufreq_remove_update_util_hook(cpu);
882
883 synchronize_sched();
884
Viresh Kumar0ced0be2016-11-15 13:53:23 +0530885 if (!policy->fast_switch_enabled) {
886 irq_work_sync(&sg_policy->irq_work);
887 kthread_cancel_work_sync(&sg_policy->work);
888 }
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200889}
890
Rafael J. Wysocki48f5adc2016-06-02 23:24:15 +0200891static void sugov_limits(struct cpufreq_policy *policy)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200892{
893 struct sugov_policy *sg_policy = policy->governor_data;
Saravana Kannan01adff122017-07-11 17:39:53 -0700894 unsigned long flags;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200895
896 if (!policy->fast_switch_enabled) {
897 mutex_lock(&sg_policy->work_lock);
Saravana Kannan01adff122017-07-11 17:39:53 -0700898 raw_spin_lock_irqsave(&sg_policy->update_lock, flags);
Saravana Kannan0f34ee92017-06-28 21:44:14 -0700899 sugov_track_cycles(sg_policy, sg_policy->policy->cur,
Stephen Boyd24c18122017-08-15 10:39:25 -0700900 ktime_get_ns());
Saravana Kannan01adff122017-07-11 17:39:53 -0700901 raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags);
Viresh Kumar73d427c2016-05-18 17:55:31 +0530902 cpufreq_policy_apply_limits(policy);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200903 mutex_unlock(&sg_policy->work_lock);
904 }
905
906 sg_policy->need_freq_update = true;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200907}
908
909static struct cpufreq_governor schedutil_gov = {
910 .name = "schedutil",
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200911 .owner = THIS_MODULE,
Rafael J. Wysocki48f5adc2016-06-02 23:24:15 +0200912 .init = sugov_init,
913 .exit = sugov_exit,
914 .start = sugov_start,
915 .stop = sugov_stop,
916 .limits = sugov_limits,
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200917};
918
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200919#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
920struct cpufreq_governor *cpufreq_default_governor(void)
921{
922 return &schedutil_gov;
923}
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200924#endif
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200925
926static int __init sugov_register(void)
927{
928 return cpufreq_register_governor(&schedutil_gov);
929}
930fs_initcall(sugov_register);