blob: fddd73d6a363c8d30bdd406a74b83f51de9446a2 [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>
18
19#include "sched.h"
20
Viresh Kumara231c652016-11-15 13:53:22 +053021#define SUGOV_KTHREAD_PRIORITY 50
22
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020023struct sugov_tunables {
24 struct gov_attr_set attr_set;
25 unsigned int rate_limit_us;
26};
27
28struct sugov_policy {
29 struct cpufreq_policy *policy;
30
31 struct sugov_tunables *tunables;
32 struct list_head tunables_hook;
33
34 raw_spinlock_t update_lock; /* For shared policies */
35 u64 last_freq_update_time;
36 s64 freq_update_delay_ns;
37 unsigned int next_freq;
Viresh Kumar55a6d462017-03-02 14:03:20 +053038 unsigned int cached_raw_freq;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020039
40 /* The next fields are only needed if fast switch cannot be used. */
41 struct irq_work irq_work;
Viresh Kumara231c652016-11-15 13:53:22 +053042 struct kthread_work work;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020043 struct mutex work_lock;
Viresh Kumara231c652016-11-15 13:53:22 +053044 struct kthread_worker worker;
45 struct task_struct *thread;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020046 bool work_in_progress;
47
48 bool need_freq_update;
49};
50
51struct sugov_cpu {
52 struct update_util_data update_util;
53 struct sugov_policy *sg_policy;
54
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +020055 unsigned long iowait_boost;
56 unsigned long iowait_boost_max;
57 u64 last_update;
Steve Muckled7439bc2016-07-13 13:25:26 -070058
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020059 /* The fields below are only needed when sharing a policy. */
60 unsigned long util;
61 unsigned long max;
Rafael J. Wysockic4568722016-08-16 22:14:55 +020062 unsigned int flags;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +020063};
64
65static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu);
66
67/************************ Governor internals ***********************/
68
69static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time)
70{
71 s64 delta_ns;
72
73 if (sg_policy->work_in_progress)
74 return false;
75
76 if (unlikely(sg_policy->need_freq_update)) {
77 sg_policy->need_freq_update = false;
78 /*
79 * This happens when limits change, so forget the previous
80 * next_freq value and force an update.
81 */
82 sg_policy->next_freq = UINT_MAX;
83 return true;
84 }
85
86 delta_ns = time - sg_policy->last_freq_update_time;
87 return delta_ns >= sg_policy->freq_update_delay_ns;
88}
89
90static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time,
91 unsigned int next_freq)
92{
93 struct cpufreq_policy *policy = sg_policy->policy;
94
95 sg_policy->last_freq_update_time = time;
96
97 if (policy->fast_switch_enabled) {
98 if (sg_policy->next_freq == next_freq) {
99 trace_cpu_frequency(policy->cur, smp_processor_id());
100 return;
101 }
102 sg_policy->next_freq = next_freq;
103 next_freq = cpufreq_driver_fast_switch(policy, next_freq);
104 if (next_freq == CPUFREQ_ENTRY_INVALID)
105 return;
106
107 policy->cur = next_freq;
108 trace_cpu_frequency(next_freq, smp_processor_id());
109 } else if (sg_policy->next_freq != next_freq) {
110 sg_policy->next_freq = next_freq;
111 sg_policy->work_in_progress = true;
112 irq_work_queue(&sg_policy->irq_work);
113 }
114}
115
116/**
117 * get_next_freq - Compute a new frequency for a given cpufreq policy.
Viresh Kumare74f7f12017-03-02 14:03:21 +0530118 * @sg_policy: schedutil policy object to compute the new frequency for.
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200119 * @util: Current CPU utilization.
120 * @max: CPU capacity.
121 *
122 * If the utilization is frequency-invariant, choose the new frequency to be
123 * proportional to it, that is
124 *
125 * next_freq = C * max_freq * util / max
126 *
127 * Otherwise, approximate the would-be frequency-invariant utilization by
128 * util_raw * (curr_freq / max_freq) which leads to
129 *
130 * next_freq = C * curr_freq * util_raw / max
131 *
132 * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8.
Steve Muckled7439bc2016-07-13 13:25:26 -0700133 *
134 * The lowest driver-supported frequency which is equal or greater than the raw
135 * next_freq (as calculated above) is returned, subject to policy min/max and
136 * cpufreq driver limitations.
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200137 */
Viresh Kumare74f7f12017-03-02 14:03:21 +0530138static unsigned int get_next_freq(struct sugov_policy *sg_policy,
139 unsigned long util, unsigned long max)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200140{
Steve Muckled7439bc2016-07-13 13:25:26 -0700141 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200142 unsigned int freq = arch_scale_freq_invariant() ?
143 policy->cpuinfo.max_freq : policy->cur;
144
Steve Muckled7439bc2016-07-13 13:25:26 -0700145 freq = (freq + (freq >> 2)) * util / max;
146
Viresh Kumar55a6d462017-03-02 14:03:20 +0530147 if (freq == sg_policy->cached_raw_freq && sg_policy->next_freq != UINT_MAX)
Steve Muckled7439bc2016-07-13 13:25:26 -0700148 return sg_policy->next_freq;
Viresh Kumar55a6d462017-03-02 14:03:20 +0530149 sg_policy->cached_raw_freq = freq;
Steve Muckled7439bc2016-07-13 13:25:26 -0700150 return cpufreq_driver_resolve_freq(policy, freq);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200151}
152
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200153static void sugov_get_util(unsigned long *util, unsigned long *max)
154{
155 struct rq *rq = this_rq();
Steve Muckle097cf682016-08-26 11:40:47 -0700156 unsigned long cfs_max;
157
158 cfs_max = arch_scale_cpu_capacity(NULL, smp_processor_id());
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200159
160 *util = min(rq->cfs.avg.util_avg, cfs_max);
161 *max = cfs_max;
162}
163
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200164static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time,
165 unsigned int flags)
166{
167 if (flags & SCHED_CPUFREQ_IOWAIT) {
168 sg_cpu->iowait_boost = sg_cpu->iowait_boost_max;
169 } else if (sg_cpu->iowait_boost) {
170 s64 delta_ns = time - sg_cpu->last_update;
171
172 /* Clear iowait_boost if the CPU apprears to have been idle. */
173 if (delta_ns > TICK_NSEC)
174 sg_cpu->iowait_boost = 0;
175 }
176}
177
178static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util,
179 unsigned long *max)
180{
181 unsigned long boost_util = sg_cpu->iowait_boost;
182 unsigned long boost_max = sg_cpu->iowait_boost_max;
183
184 if (!boost_util)
185 return;
186
187 if (*util * boost_max < *max * boost_util) {
188 *util = boost_util;
189 *max = boost_max;
190 }
191 sg_cpu->iowait_boost >>= 1;
192}
193
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200194static void sugov_update_single(struct update_util_data *hook, u64 time,
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200195 unsigned int flags)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200196{
197 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
198 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
199 struct cpufreq_policy *policy = sg_policy->policy;
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200200 unsigned long util, max;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200201 unsigned int next_f;
202
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200203 sugov_set_iowait_boost(sg_cpu, time, flags);
204 sg_cpu->last_update = time;
205
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200206 if (!sugov_should_update_freq(sg_policy, time))
207 return;
208
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200209 if (flags & SCHED_CPUFREQ_RT_DL) {
210 next_f = policy->cpuinfo.max_freq;
211 } else {
212 sugov_get_util(&util, &max);
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200213 sugov_iowait_boost(sg_cpu, &util, &max);
Viresh Kumare74f7f12017-03-02 14:03:21 +0530214 next_f = get_next_freq(sg_policy, util, max);
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200215 }
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200216 sugov_update_commit(sg_policy, time, next_f);
217}
218
Steve Muckled7439bc2016-07-13 13:25:26 -0700219static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu,
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200220 unsigned long util, unsigned long max,
221 unsigned int flags)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200222{
Steve Muckled7439bc2016-07-13 13:25:26 -0700223 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200224 struct cpufreq_policy *policy = sg_policy->policy;
225 unsigned int max_f = policy->cpuinfo.max_freq;
226 u64 last_freq_update_time = sg_policy->last_freq_update_time;
227 unsigned int j;
228
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200229 if (flags & SCHED_CPUFREQ_RT_DL)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200230 return max_f;
231
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200232 sugov_iowait_boost(sg_cpu, &util, &max);
233
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200234 for_each_cpu(j, policy->cpus) {
235 struct sugov_cpu *j_sg_cpu;
236 unsigned long j_util, j_max;
237 s64 delta_ns;
238
239 if (j == smp_processor_id())
240 continue;
241
242 j_sg_cpu = &per_cpu(sugov_cpu, j);
243 /*
244 * If the CPU utilization was last updated before the previous
245 * frequency update and the time elapsed between the last update
246 * of the CPU utilization and the last frequency update is long
247 * enough, don't take the CPU into account as it probably is
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200248 * idle now (and clear iowait_boost for it).
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200249 */
250 delta_ns = last_freq_update_time - j_sg_cpu->last_update;
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200251 if (delta_ns > TICK_NSEC) {
252 j_sg_cpu->iowait_boost = 0;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200253 continue;
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200254 }
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200255 if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200256 return max_f;
257
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200258 j_util = j_sg_cpu->util;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200259 j_max = j_sg_cpu->max;
260 if (j_util * max > j_max * util) {
261 util = j_util;
262 max = j_max;
263 }
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200264
265 sugov_iowait_boost(j_sg_cpu, &util, &max);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200266 }
267
Viresh Kumare74f7f12017-03-02 14:03:21 +0530268 return get_next_freq(sg_policy, util, max);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200269}
270
271static void sugov_update_shared(struct update_util_data *hook, u64 time,
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200272 unsigned int flags)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200273{
274 struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util);
275 struct sugov_policy *sg_policy = sg_cpu->sg_policy;
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200276 unsigned long util, max;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200277 unsigned int next_f;
278
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200279 sugov_get_util(&util, &max);
280
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200281 raw_spin_lock(&sg_policy->update_lock);
282
283 sg_cpu->util = util;
284 sg_cpu->max = max;
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200285 sg_cpu->flags = flags;
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200286
287 sugov_set_iowait_boost(sg_cpu, time, flags);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200288 sg_cpu->last_update = time;
289
290 if (sugov_should_update_freq(sg_policy, time)) {
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200291 next_f = sugov_next_freq_shared(sg_cpu, util, max, flags);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200292 sugov_update_commit(sg_policy, time, next_f);
293 }
294
295 raw_spin_unlock(&sg_policy->update_lock);
296}
297
Viresh Kumara231c652016-11-15 13:53:22 +0530298static void sugov_work(struct kthread_work *work)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200299{
300 struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work);
301
302 mutex_lock(&sg_policy->work_lock);
303 __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq,
304 CPUFREQ_RELATION_L);
305 mutex_unlock(&sg_policy->work_lock);
306
307 sg_policy->work_in_progress = false;
308}
309
310static void sugov_irq_work(struct irq_work *irq_work)
311{
312 struct sugov_policy *sg_policy;
313
314 sg_policy = container_of(irq_work, struct sugov_policy, irq_work);
Viresh Kumara231c652016-11-15 13:53:22 +0530315
316 /*
Viresh Kumar75526a22016-11-24 13:51:11 +0530317 * For RT and deadline tasks, the schedutil governor shoots the
318 * frequency to maximum. Special care must be taken to ensure that this
319 * kthread doesn't result in the same behavior.
Viresh Kumara231c652016-11-15 13:53:22 +0530320 *
321 * This is (mostly) guaranteed by the work_in_progress flag. The flag is
Viresh Kumar75526a22016-11-24 13:51:11 +0530322 * updated only at the end of the sugov_work() function and before that
323 * the schedutil governor rejects all other frequency scaling requests.
Viresh Kumara231c652016-11-15 13:53:22 +0530324 *
Viresh Kumar75526a22016-11-24 13:51:11 +0530325 * There is a very rare case though, where the RT thread yields right
Viresh Kumara231c652016-11-15 13:53:22 +0530326 * after the work_in_progress flag is cleared. The effects of that are
327 * neglected for now.
328 */
329 kthread_queue_work(&sg_policy->worker, &sg_policy->work);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200330}
331
332/************************** sysfs interface ************************/
333
334static struct sugov_tunables *global_tunables;
335static DEFINE_MUTEX(global_tunables_lock);
336
337static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set)
338{
339 return container_of(attr_set, struct sugov_tunables, attr_set);
340}
341
342static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf)
343{
344 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
345
346 return sprintf(buf, "%u\n", tunables->rate_limit_us);
347}
348
349static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf,
350 size_t count)
351{
352 struct sugov_tunables *tunables = to_sugov_tunables(attr_set);
353 struct sugov_policy *sg_policy;
354 unsigned int rate_limit_us;
355
356 if (kstrtouint(buf, 10, &rate_limit_us))
357 return -EINVAL;
358
359 tunables->rate_limit_us = rate_limit_us;
360
361 list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook)
362 sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC;
363
364 return count;
365}
366
367static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us);
368
369static struct attribute *sugov_attributes[] = {
370 &rate_limit_us.attr,
371 NULL
372};
373
374static struct kobj_type sugov_tunables_ktype = {
375 .default_attrs = sugov_attributes,
376 .sysfs_ops = &governor_sysfs_ops,
377};
378
379/********************** cpufreq governor interface *********************/
380
381static struct cpufreq_governor schedutil_gov;
382
383static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy)
384{
385 struct sugov_policy *sg_policy;
386
387 sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL);
388 if (!sg_policy)
389 return NULL;
390
391 sg_policy->policy = policy;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200392 raw_spin_lock_init(&sg_policy->update_lock);
393 return sg_policy;
394}
395
396static void sugov_policy_free(struct sugov_policy *sg_policy)
397{
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200398 kfree(sg_policy);
399}
400
Viresh Kumara231c652016-11-15 13:53:22 +0530401static int sugov_kthread_create(struct sugov_policy *sg_policy)
402{
403 struct task_struct *thread;
404 struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO / 2 };
405 struct cpufreq_policy *policy = sg_policy->policy;
406 int ret;
407
408 /* kthread only required for slow path */
409 if (policy->fast_switch_enabled)
410 return 0;
411
412 kthread_init_work(&sg_policy->work, sugov_work);
413 kthread_init_worker(&sg_policy->worker);
414 thread = kthread_create(kthread_worker_fn, &sg_policy->worker,
415 "sugov:%d",
416 cpumask_first(policy->related_cpus));
417 if (IS_ERR(thread)) {
418 pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread));
419 return PTR_ERR(thread);
420 }
421
422 ret = sched_setscheduler_nocheck(thread, SCHED_FIFO, &param);
423 if (ret) {
424 kthread_stop(thread);
425 pr_warn("%s: failed to set SCHED_FIFO\n", __func__);
426 return ret;
427 }
428
429 sg_policy->thread = thread;
430 kthread_bind_mask(thread, policy->related_cpus);
Viresh Kumar0ced0be2016-11-15 13:53:23 +0530431 init_irq_work(&sg_policy->irq_work, sugov_irq_work);
432 mutex_init(&sg_policy->work_lock);
433
Viresh Kumara231c652016-11-15 13:53:22 +0530434 wake_up_process(thread);
435
436 return 0;
437}
438
439static void sugov_kthread_stop(struct sugov_policy *sg_policy)
440{
441 /* kthread only required for slow path */
442 if (sg_policy->policy->fast_switch_enabled)
443 return;
444
445 kthread_flush_worker(&sg_policy->worker);
446 kthread_stop(sg_policy->thread);
Viresh Kumar0ced0be2016-11-15 13:53:23 +0530447 mutex_destroy(&sg_policy->work_lock);
Viresh Kumara231c652016-11-15 13:53:22 +0530448}
449
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200450static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy)
451{
452 struct sugov_tunables *tunables;
453
454 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
455 if (tunables) {
456 gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook);
457 if (!have_governor_per_policy())
458 global_tunables = tunables;
459 }
460 return tunables;
461}
462
463static void sugov_tunables_free(struct sugov_tunables *tunables)
464{
465 if (!have_governor_per_policy())
466 global_tunables = NULL;
467
468 kfree(tunables);
469}
470
471static int sugov_init(struct cpufreq_policy *policy)
472{
473 struct sugov_policy *sg_policy;
474 struct sugov_tunables *tunables;
475 unsigned int lat;
476 int ret = 0;
477
478 /* State should be equivalent to EXIT */
479 if (policy->governor_data)
480 return -EBUSY;
481
Viresh Kumar6d5551b2016-11-15 13:53:21 +0530482 cpufreq_enable_fast_switch(policy);
483
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200484 sg_policy = sugov_policy_alloc(policy);
Viresh Kumar6d5551b2016-11-15 13:53:21 +0530485 if (!sg_policy) {
486 ret = -ENOMEM;
487 goto disable_fast_switch;
488 }
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200489
Viresh Kumara231c652016-11-15 13:53:22 +0530490 ret = sugov_kthread_create(sg_policy);
491 if (ret)
492 goto free_sg_policy;
493
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200494 mutex_lock(&global_tunables_lock);
495
496 if (global_tunables) {
497 if (WARN_ON(have_governor_per_policy())) {
498 ret = -EINVAL;
Viresh Kumara231c652016-11-15 13:53:22 +0530499 goto stop_kthread;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200500 }
501 policy->governor_data = sg_policy;
502 sg_policy->tunables = global_tunables;
503
504 gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook);
505 goto out;
506 }
507
508 tunables = sugov_tunables_alloc(sg_policy);
509 if (!tunables) {
510 ret = -ENOMEM;
Viresh Kumara231c652016-11-15 13:53:22 +0530511 goto stop_kthread;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200512 }
513
514 tunables->rate_limit_us = LATENCY_MULTIPLIER;
515 lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
516 if (lat)
517 tunables->rate_limit_us *= lat;
518
519 policy->governor_data = sg_policy;
520 sg_policy->tunables = tunables;
521
522 ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype,
523 get_governor_parent_kobj(policy), "%s",
524 schedutil_gov.name);
525 if (ret)
526 goto fail;
527
Viresh Kumarb5b11602016-11-15 13:53:20 +0530528out:
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200529 mutex_unlock(&global_tunables_lock);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200530 return 0;
531
Viresh Kumarb5b11602016-11-15 13:53:20 +0530532fail:
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200533 policy->governor_data = NULL;
534 sugov_tunables_free(tunables);
535
Viresh Kumara231c652016-11-15 13:53:22 +0530536stop_kthread:
537 sugov_kthread_stop(sg_policy);
538
Viresh Kumarb5b11602016-11-15 13:53:20 +0530539free_sg_policy:
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200540 mutex_unlock(&global_tunables_lock);
541
542 sugov_policy_free(sg_policy);
Viresh Kumar6d5551b2016-11-15 13:53:21 +0530543
544disable_fast_switch:
545 cpufreq_disable_fast_switch(policy);
546
Viresh Kumar87ecf322016-05-18 17:55:28 +0530547 pr_err("initialization failed (error %d)\n", ret);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200548 return ret;
549}
550
Rafael J. Wysocki48f5adc2016-06-02 23:24:15 +0200551static void sugov_exit(struct cpufreq_policy *policy)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200552{
553 struct sugov_policy *sg_policy = policy->governor_data;
554 struct sugov_tunables *tunables = sg_policy->tunables;
555 unsigned int count;
556
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200557 mutex_lock(&global_tunables_lock);
558
559 count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook);
560 policy->governor_data = NULL;
561 if (!count)
562 sugov_tunables_free(tunables);
563
564 mutex_unlock(&global_tunables_lock);
565
Viresh Kumara231c652016-11-15 13:53:22 +0530566 sugov_kthread_stop(sg_policy);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200567 sugov_policy_free(sg_policy);
Viresh Kumar6d5551b2016-11-15 13:53:21 +0530568 cpufreq_disable_fast_switch(policy);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200569}
570
571static int sugov_start(struct cpufreq_policy *policy)
572{
573 struct sugov_policy *sg_policy = policy->governor_data;
574 unsigned int cpu;
575
576 sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC;
577 sg_policy->last_freq_update_time = 0;
578 sg_policy->next_freq = UINT_MAX;
579 sg_policy->work_in_progress = false;
580 sg_policy->need_freq_update = false;
Viresh Kumar55a6d462017-03-02 14:03:20 +0530581 sg_policy->cached_raw_freq = 0;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200582
583 for_each_cpu(cpu, policy->cpus) {
584 struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu);
585
586 sg_cpu->sg_policy = sg_policy;
587 if (policy_is_shared(policy)) {
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200588 sg_cpu->util = 0;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200589 sg_cpu->max = 0;
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200590 sg_cpu->flags = SCHED_CPUFREQ_RT;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200591 sg_cpu->last_update = 0;
Rafael J. Wysocki193d25c2016-09-10 00:00:31 +0200592 sg_cpu->iowait_boost = 0;
593 sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200594 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
595 sugov_update_shared);
596 } else {
597 cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util,
598 sugov_update_single);
599 }
600 }
601 return 0;
602}
603
Rafael J. Wysocki48f5adc2016-06-02 23:24:15 +0200604static void sugov_stop(struct cpufreq_policy *policy)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200605{
606 struct sugov_policy *sg_policy = policy->governor_data;
607 unsigned int cpu;
608
609 for_each_cpu(cpu, policy->cpus)
610 cpufreq_remove_update_util_hook(cpu);
611
612 synchronize_sched();
613
Viresh Kumar0ced0be2016-11-15 13:53:23 +0530614 if (!policy->fast_switch_enabled) {
615 irq_work_sync(&sg_policy->irq_work);
616 kthread_cancel_work_sync(&sg_policy->work);
617 }
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200618}
619
Rafael J. Wysocki48f5adc2016-06-02 23:24:15 +0200620static void sugov_limits(struct cpufreq_policy *policy)
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200621{
622 struct sugov_policy *sg_policy = policy->governor_data;
623
624 if (!policy->fast_switch_enabled) {
625 mutex_lock(&sg_policy->work_lock);
Viresh Kumar73d427c2016-05-18 17:55:31 +0530626 cpufreq_policy_apply_limits(policy);
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200627 mutex_unlock(&sg_policy->work_lock);
628 }
629
630 sg_policy->need_freq_update = true;
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200631}
632
633static struct cpufreq_governor schedutil_gov = {
634 .name = "schedutil",
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200635 .owner = THIS_MODULE,
Rafael J. Wysocki48f5adc2016-06-02 23:24:15 +0200636 .init = sugov_init,
637 .exit = sugov_exit,
638 .start = sugov_start,
639 .stop = sugov_stop,
640 .limits = sugov_limits,
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200641};
642
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200643#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL
644struct cpufreq_governor *cpufreq_default_governor(void)
645{
646 return &schedutil_gov;
647}
Rafael J. Wysockib1d09762016-04-02 01:09:12 +0200648#endif
Rafael J. Wysockic4568722016-08-16 22:14:55 +0200649
650static int __init sugov_register(void)
651{
652 return cpufreq_register_governor(&schedutil_gov);
653}
654fs_initcall(sugov_register);