Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 1 | /* |
| 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 Kumar | 60f05e8 | 2016-05-18 17:55:28 +0530 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 14 | #include <linux/cpufreq.h> |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 15 | #include <linux/kthread.h> |
Ingo Molnar | ae7e81c | 2017-02-01 18:07:51 +0100 | [diff] [blame] | 16 | #include <uapi/linux/sched/types.h> |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <trace/events/power.h> |
| 19 | |
| 20 | #include "sched.h" |
| 21 | |
Patrick Bellasi | 07d1bac | 2019-02-15 11:49:36 +0000 | [diff] [blame] | 22 | unsigned long boosted_cpu_util(int cpu); |
Patrick Bellasi | 159c14f | 2017-10-21 18:07:35 +0100 | [diff] [blame] | 23 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 24 | #define SUGOV_KTHREAD_PRIORITY 50 |
| 25 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 26 | struct sugov_tunables { |
| 27 | struct gov_attr_set attr_set; |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 28 | unsigned int up_rate_limit_us; |
| 29 | unsigned int down_rate_limit_us; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct sugov_policy { |
| 33 | struct cpufreq_policy *policy; |
| 34 | |
| 35 | struct sugov_tunables *tunables; |
| 36 | struct list_head tunables_hook; |
| 37 | |
| 38 | raw_spinlock_t update_lock; /* For shared policies */ |
| 39 | u64 last_freq_update_time; |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 40 | s64 min_rate_limit_ns; |
| 41 | s64 up_rate_delay_ns; |
| 42 | s64 down_rate_delay_ns; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 43 | unsigned int next_freq; |
Viresh Kumar | 6c4f0fa | 2017-03-02 14:03:20 +0530 | [diff] [blame] | 44 | unsigned int cached_raw_freq; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 45 | |
| 46 | /* The next fields are only needed if fast switch cannot be used. */ |
| 47 | struct irq_work irq_work; |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 48 | struct kthread_work work; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 49 | struct mutex work_lock; |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 50 | struct kthread_worker worker; |
| 51 | struct task_struct *thread; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 52 | bool work_in_progress; |
| 53 | |
| 54 | bool need_freq_update; |
| 55 | }; |
| 56 | |
| 57 | struct sugov_cpu { |
| 58 | struct update_util_data update_util; |
| 59 | struct sugov_policy *sg_policy; |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 60 | unsigned int cpu; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 61 | |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 62 | bool iowait_boost_pending; |
Joel Fernandes | 251accf | 2017-07-23 08:54:26 -0700 | [diff] [blame] | 63 | unsigned int iowait_boost; |
| 64 | unsigned int iowait_boost_max; |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 65 | u64 last_update; |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 66 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 67 | /* The fields below are only needed when sharing a policy. */ |
| 68 | unsigned long util; |
| 69 | unsigned long max; |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 70 | unsigned int flags; |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 71 | |
| 72 | /* The field below is for single-CPU policies only. */ |
| 73 | #ifdef CONFIG_NO_HZ_COMMON |
| 74 | unsigned long saved_idle_calls; |
| 75 | #endif |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu); |
| 79 | |
| 80 | /************************ Governor internals ***********************/ |
| 81 | |
| 82 | static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time) |
| 83 | { |
| 84 | s64 delta_ns; |
| 85 | |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 86 | /* |
| 87 | * Since cpufreq_update_util() is called with rq->lock held for |
| 88 | * the @target_cpu, our per-cpu data is fully serialized. |
| 89 | * |
| 90 | * However, drivers cannot in general deal with cross-cpu |
| 91 | * requests, so while get_next_freq() will work, our |
Viresh Kumar | c49cbc1 | 2017-08-14 14:50:16 +0530 | [diff] [blame] | 92 | * sugov_update_commit() call may not for the fast switching platforms. |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 93 | * |
| 94 | * Hence stop here for remote requests if they aren't supported |
| 95 | * by the hardware, as calculating the frequency is pointless if |
| 96 | * we cannot in fact act on it. |
Viresh Kumar | c49cbc1 | 2017-08-14 14:50:16 +0530 | [diff] [blame] | 97 | * |
| 98 | * For the slow switching platforms, the kthread is always scheduled on |
| 99 | * the right set of CPUs and any CPU can find the next frequency and |
| 100 | * schedule the kthread. |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 101 | */ |
Viresh Kumar | c49cbc1 | 2017-08-14 14:50:16 +0530 | [diff] [blame] | 102 | if (sg_policy->policy->fast_switch_enabled && |
| 103 | !cpufreq_can_do_remote_dvfs(sg_policy->policy)) |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 104 | return false; |
| 105 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 106 | if (sg_policy->work_in_progress) |
| 107 | return false; |
| 108 | |
| 109 | if (unlikely(sg_policy->need_freq_update)) { |
| 110 | sg_policy->need_freq_update = false; |
| 111 | /* |
| 112 | * This happens when limits change, so forget the previous |
| 113 | * next_freq value and force an update. |
| 114 | */ |
| 115 | sg_policy->next_freq = UINT_MAX; |
| 116 | return true; |
| 117 | } |
| 118 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 119 | /* No need to recalculate next freq for min_rate_limit_us |
| 120 | * at least. However we might still decide to further rate |
| 121 | * limit once frequency change direction is decided, according |
| 122 | * to the separate rate limits. |
| 123 | */ |
| 124 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 125 | delta_ns = time - sg_policy->last_freq_update_time; |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 126 | return delta_ns >= sg_policy->min_rate_limit_ns; |
| 127 | } |
| 128 | |
| 129 | static bool sugov_up_down_rate_limit(struct sugov_policy *sg_policy, u64 time, |
| 130 | unsigned int next_freq) |
| 131 | { |
| 132 | s64 delta_ns; |
| 133 | |
| 134 | delta_ns = time - sg_policy->last_freq_update_time; |
| 135 | |
| 136 | if (next_freq > sg_policy->next_freq && |
| 137 | delta_ns < sg_policy->up_rate_delay_ns) |
| 138 | return true; |
| 139 | |
| 140 | if (next_freq < sg_policy->next_freq && |
| 141 | delta_ns < sg_policy->down_rate_delay_ns) |
| 142 | return true; |
| 143 | |
| 144 | return false; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time, |
| 148 | unsigned int next_freq) |
| 149 | { |
| 150 | struct cpufreq_policy *policy = sg_policy->policy; |
| 151 | |
Rafael J. Wysocki | 38d4ea2 | 2017-03-22 18:32:47 +0100 | [diff] [blame] | 152 | if (sg_policy->next_freq == next_freq) |
| 153 | return; |
| 154 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 155 | if (sugov_up_down_rate_limit(sg_policy, time, next_freq)) |
| 156 | return; |
| 157 | |
Rafael J. Wysocki | 38d4ea2 | 2017-03-22 18:32:47 +0100 | [diff] [blame] | 158 | sg_policy->next_freq = next_freq; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 159 | sg_policy->last_freq_update_time = time; |
| 160 | |
| 161 | if (policy->fast_switch_enabled) { |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 162 | next_freq = cpufreq_driver_fast_switch(policy, next_freq); |
Viresh Kumar | 209887e | 2017-08-09 10:21:46 +0530 | [diff] [blame] | 163 | if (!next_freq) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 164 | return; |
| 165 | |
| 166 | policy->cur = next_freq; |
| 167 | trace_cpu_frequency(next_freq, smp_processor_id()); |
Rafael J. Wysocki | 38d4ea2 | 2017-03-22 18:32:47 +0100 | [diff] [blame] | 168 | } else { |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 169 | sg_policy->work_in_progress = true; |
| 170 | irq_work_queue(&sg_policy->irq_work); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * get_next_freq - Compute a new frequency for a given cpufreq policy. |
Viresh Kumar | 655cb1e | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 176 | * @sg_policy: schedutil policy object to compute the new frequency for. |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 177 | * @util: Current CPU utilization. |
| 178 | * @max: CPU capacity. |
| 179 | * |
| 180 | * If the utilization is frequency-invariant, choose the new frequency to be |
| 181 | * proportional to it, that is |
| 182 | * |
| 183 | * next_freq = C * max_freq * util / max |
| 184 | * |
| 185 | * Otherwise, approximate the would-be frequency-invariant utilization by |
| 186 | * util_raw * (curr_freq / max_freq) which leads to |
| 187 | * |
| 188 | * next_freq = C * curr_freq * util_raw / max |
| 189 | * |
| 190 | * Take C = 1.25 for the frequency tipping point at (util / max) = 0.8. |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 191 | * |
| 192 | * The lowest driver-supported frequency which is equal or greater than the raw |
| 193 | * next_freq (as calculated above) is returned, subject to policy min/max and |
| 194 | * cpufreq driver limitations. |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 195 | */ |
Viresh Kumar | 655cb1e | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 196 | static unsigned int get_next_freq(struct sugov_policy *sg_policy, |
| 197 | unsigned long util, unsigned long max) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 198 | { |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 199 | struct cpufreq_policy *policy = sg_policy->policy; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 200 | unsigned int freq = arch_scale_freq_invariant() ? |
| 201 | policy->cpuinfo.max_freq : policy->cur; |
| 202 | |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 203 | freq = (freq + (freq >> 2)) * util / max; |
| 204 | |
Viresh Kumar | 6c4f0fa | 2017-03-02 14:03:20 +0530 | [diff] [blame] | 205 | if (freq == sg_policy->cached_raw_freq && sg_policy->next_freq != UINT_MAX) |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 206 | return sg_policy->next_freq; |
Viresh Kumar | 6c4f0fa | 2017-03-02 14:03:20 +0530 | [diff] [blame] | 207 | sg_policy->cached_raw_freq = freq; |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 208 | return cpufreq_driver_resolve_freq(policy, freq); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 211 | static void sugov_get_util(unsigned long *util, unsigned long *max, int cpu) |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 212 | { |
Patrick Bellasi | 07d1bac | 2019-02-15 11:49:36 +0000 | [diff] [blame] | 213 | unsigned long max_cap; |
Steve Muckle | 8314bc8 | 2016-08-26 11:40:47 -0700 | [diff] [blame] | 214 | |
Steve Muckle | f1f7125 | 2016-08-25 15:59:17 -0700 | [diff] [blame] | 215 | max_cap = arch_scale_cpu_capacity(NULL, cpu); |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 216 | |
Patrick Bellasi | 07d1bac | 2019-02-15 11:49:36 +0000 | [diff] [blame] | 217 | *util = boosted_cpu_util(cpu); |
Patrick Bellasi | 159c14f | 2017-10-21 18:07:35 +0100 | [diff] [blame] | 218 | *util = min(*util, max_cap); |
Steve Muckle | f1f7125 | 2016-08-25 15:59:17 -0700 | [diff] [blame] | 219 | *max = max_cap; |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 222 | static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time, |
| 223 | unsigned int flags) |
| 224 | { |
| 225 | if (flags & SCHED_CPUFREQ_IOWAIT) { |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 226 | if (sg_cpu->iowait_boost_pending) |
| 227 | return; |
| 228 | |
| 229 | sg_cpu->iowait_boost_pending = true; |
| 230 | |
| 231 | if (sg_cpu->iowait_boost) { |
| 232 | sg_cpu->iowait_boost <<= 1; |
| 233 | if (sg_cpu->iowait_boost > sg_cpu->iowait_boost_max) |
| 234 | sg_cpu->iowait_boost = sg_cpu->iowait_boost_max; |
| 235 | } else { |
| 236 | sg_cpu->iowait_boost = sg_cpu->sg_policy->policy->min; |
| 237 | } |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 238 | } else if (sg_cpu->iowait_boost) { |
| 239 | s64 delta_ns = time - sg_cpu->last_update; |
| 240 | |
| 241 | /* Clear iowait_boost if the CPU apprears to have been idle. */ |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 242 | if (delta_ns > TICK_NSEC) { |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 243 | sg_cpu->iowait_boost = 0; |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 244 | sg_cpu->iowait_boost_pending = false; |
| 245 | } |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util, |
| 250 | unsigned long *max) |
| 251 | { |
Joel Fernandes | 251accf | 2017-07-23 08:54:26 -0700 | [diff] [blame] | 252 | unsigned int boost_util, boost_max; |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 253 | |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 254 | if (!sg_cpu->iowait_boost) |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 255 | return; |
| 256 | |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 257 | if (sg_cpu->iowait_boost_pending) { |
| 258 | sg_cpu->iowait_boost_pending = false; |
| 259 | } else { |
| 260 | sg_cpu->iowait_boost >>= 1; |
| 261 | if (sg_cpu->iowait_boost < sg_cpu->sg_policy->policy->min) { |
| 262 | sg_cpu->iowait_boost = 0; |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | boost_util = sg_cpu->iowait_boost; |
| 268 | boost_max = sg_cpu->iowait_boost_max; |
| 269 | |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 270 | if (*util * boost_max < *max * boost_util) { |
| 271 | *util = boost_util; |
| 272 | *max = boost_max; |
| 273 | } |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 276 | #ifdef CONFIG_NO_HZ_COMMON |
| 277 | static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) |
| 278 | { |
Joel Fernandes | 0c688c2 | 2017-12-21 02:22:45 +0100 | [diff] [blame] | 279 | unsigned long idle_calls = tick_nohz_get_idle_calls_cpu(sg_cpu->cpu); |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 280 | bool ret = idle_calls == sg_cpu->saved_idle_calls; |
| 281 | |
| 282 | sg_cpu->saved_idle_calls = idle_calls; |
| 283 | return ret; |
| 284 | } |
| 285 | #else |
| 286 | static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; } |
| 287 | #endif /* CONFIG_NO_HZ_COMMON */ |
| 288 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 289 | static void sugov_update_single(struct update_util_data *hook, u64 time, |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 290 | unsigned int flags) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 291 | { |
| 292 | struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util); |
| 293 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
| 294 | struct cpufreq_policy *policy = sg_policy->policy; |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 295 | unsigned long util, max; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 296 | unsigned int next_f; |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 297 | bool busy; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 298 | |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 299 | sugov_set_iowait_boost(sg_cpu, time, flags); |
| 300 | sg_cpu->last_update = time; |
| 301 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 302 | if (!sugov_should_update_freq(sg_policy, time)) |
| 303 | return; |
| 304 | |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 305 | busy = sugov_cpu_is_busy(sg_cpu); |
| 306 | |
Steve Muckle | f1f7125 | 2016-08-25 15:59:17 -0700 | [diff] [blame] | 307 | if (flags & SCHED_CPUFREQ_DL) { |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 308 | next_f = policy->cpuinfo.max_freq; |
| 309 | } else { |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 310 | sugov_get_util(&util, &max, sg_cpu->cpu); |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 311 | sugov_iowait_boost(sg_cpu, &util, &max); |
Viresh Kumar | 655cb1e | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 312 | next_f = get_next_freq(sg_policy, util, max); |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 313 | /* |
| 314 | * Do not reduce the frequency if the CPU has not been idle |
| 315 | * recently, as the reduction is likely to be premature then. |
| 316 | */ |
Rafael J. Wysocki | 99e9acc | 2018-05-09 11:44:56 +0200 | [diff] [blame] | 317 | if (busy && next_f < sg_policy->next_freq && |
| 318 | sg_policy->next_freq != UINT_MAX) { |
Rafael J. Wysocki | b7eaf1a | 2017-03-22 00:08:50 +0100 | [diff] [blame] | 319 | next_f = sg_policy->next_freq; |
Viresh Kumar | b799749 | 2017-11-08 20:23:55 +0530 | [diff] [blame] | 320 | |
| 321 | /* Reset cached freq as next_freq has changed */ |
| 322 | sg_policy->cached_raw_freq = 0; |
| 323 | } |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 324 | } |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 325 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 326 | sugov_update_commit(sg_policy, time, next_f); |
| 327 | } |
| 328 | |
Juri Lelli | d86ab9c | 2017-05-03 14:30:48 +0100 | [diff] [blame] | 329 | static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu, u64 time) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 330 | { |
Steve Muckle | 5cbea46 | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 331 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 332 | struct cpufreq_policy *policy = sg_policy->policy; |
Viresh Kumar | cba1dfb | 2017-03-09 09:34:54 +0530 | [diff] [blame] | 333 | unsigned long util = 0, max = 1; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 334 | unsigned int j; |
| 335 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 336 | for_each_cpu(j, policy->cpus) { |
Viresh Kumar | cba1dfb | 2017-03-09 09:34:54 +0530 | [diff] [blame] | 337 | struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 338 | unsigned long j_util, j_max; |
| 339 | s64 delta_ns; |
| 340 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 341 | /* |
| 342 | * If the CPU utilization was last updated before the previous |
| 343 | * frequency update and the time elapsed between the last update |
| 344 | * of the CPU utilization and the last frequency update is long |
| 345 | * enough, don't take the CPU into account as it probably is |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 346 | * idle now (and clear iowait_boost for it). |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 347 | */ |
Juri Lelli | d86ab9c | 2017-05-03 14:30:48 +0100 | [diff] [blame] | 348 | delta_ns = time - j_sg_cpu->last_update; |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 349 | if (delta_ns > TICK_NSEC) { |
| 350 | j_sg_cpu->iowait_boost = 0; |
Joel Fernandes | a5a0809 | 2017-07-23 08:54:25 -0700 | [diff] [blame] | 351 | j_sg_cpu->iowait_boost_pending = false; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 352 | continue; |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 353 | } |
Steve Muckle | f1f7125 | 2016-08-25 15:59:17 -0700 | [diff] [blame] | 354 | if (j_sg_cpu->flags & SCHED_CPUFREQ_DL) |
Viresh Kumar | cba1dfb | 2017-03-09 09:34:54 +0530 | [diff] [blame] | 355 | return policy->cpuinfo.max_freq; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 356 | |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 357 | j_util = j_sg_cpu->util; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 358 | j_max = j_sg_cpu->max; |
| 359 | if (j_util * max > j_max * util) { |
| 360 | util = j_util; |
| 361 | max = j_max; |
| 362 | } |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 363 | |
| 364 | sugov_iowait_boost(j_sg_cpu, &util, &max); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Viresh Kumar | 655cb1e | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 367 | return get_next_freq(sg_policy, util, max); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void sugov_update_shared(struct update_util_data *hook, u64 time, |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 371 | unsigned int flags) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 372 | { |
| 373 | struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util); |
| 374 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 375 | unsigned long util, max; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 376 | unsigned int next_f; |
| 377 | |
Viresh Kumar | 674e754 | 2017-07-28 12:16:38 +0530 | [diff] [blame] | 378 | sugov_get_util(&util, &max, sg_cpu->cpu); |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 379 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 380 | raw_spin_lock(&sg_policy->update_lock); |
| 381 | |
| 382 | sg_cpu->util = util; |
| 383 | sg_cpu->max = max; |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 384 | sg_cpu->flags = flags; |
Rafael J. Wysocki | 21ca6d2 | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 385 | |
| 386 | sugov_set_iowait_boost(sg_cpu, time, flags); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 387 | sg_cpu->last_update = time; |
| 388 | |
| 389 | if (sugov_should_update_freq(sg_policy, time)) { |
Steve Muckle | f1f7125 | 2016-08-25 15:59:17 -0700 | [diff] [blame] | 390 | if (flags & SCHED_CPUFREQ_DL) |
Viresh Kumar | cba1dfb | 2017-03-09 09:34:54 +0530 | [diff] [blame] | 391 | next_f = sg_policy->policy->cpuinfo.max_freq; |
| 392 | else |
Juri Lelli | d86ab9c | 2017-05-03 14:30:48 +0100 | [diff] [blame] | 393 | next_f = sugov_next_freq_shared(sg_cpu, time); |
Viresh Kumar | cba1dfb | 2017-03-09 09:34:54 +0530 | [diff] [blame] | 394 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 395 | sugov_update_commit(sg_policy, time, next_f); |
| 396 | } |
| 397 | |
| 398 | raw_spin_unlock(&sg_policy->update_lock); |
| 399 | } |
| 400 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 401 | static void sugov_work(struct kthread_work *work) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 402 | { |
| 403 | struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work); |
| 404 | |
| 405 | mutex_lock(&sg_policy->work_lock); |
| 406 | __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq, |
| 407 | CPUFREQ_RELATION_L); |
| 408 | mutex_unlock(&sg_policy->work_lock); |
| 409 | |
| 410 | sg_policy->work_in_progress = false; |
| 411 | } |
| 412 | |
| 413 | static void sugov_irq_work(struct irq_work *irq_work) |
| 414 | { |
| 415 | struct sugov_policy *sg_policy; |
| 416 | |
| 417 | sg_policy = container_of(irq_work, struct sugov_policy, irq_work); |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 418 | |
| 419 | /* |
Viresh Kumar | d06e622 | 2016-11-24 13:51:11 +0530 | [diff] [blame] | 420 | * For RT and deadline tasks, the schedutil governor shoots the |
| 421 | * frequency to maximum. Special care must be taken to ensure that this |
| 422 | * kthread doesn't result in the same behavior. |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 423 | * |
| 424 | * This is (mostly) guaranteed by the work_in_progress flag. The flag is |
Viresh Kumar | d06e622 | 2016-11-24 13:51:11 +0530 | [diff] [blame] | 425 | * updated only at the end of the sugov_work() function and before that |
| 426 | * the schedutil governor rejects all other frequency scaling requests. |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 427 | * |
Viresh Kumar | d06e622 | 2016-11-24 13:51:11 +0530 | [diff] [blame] | 428 | * There is a very rare case though, where the RT thread yields right |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 429 | * after the work_in_progress flag is cleared. The effects of that are |
| 430 | * neglected for now. |
| 431 | */ |
| 432 | kthread_queue_work(&sg_policy->worker, &sg_policy->work); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /************************** sysfs interface ************************/ |
| 436 | |
| 437 | static struct sugov_tunables *global_tunables; |
| 438 | static DEFINE_MUTEX(global_tunables_lock); |
| 439 | |
| 440 | static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set) |
| 441 | { |
| 442 | return container_of(attr_set, struct sugov_tunables, attr_set); |
| 443 | } |
| 444 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 445 | static DEFINE_MUTEX(min_rate_lock); |
| 446 | |
| 447 | static void update_min_rate_limit_ns(struct sugov_policy *sg_policy) |
| 448 | { |
| 449 | mutex_lock(&min_rate_lock); |
| 450 | sg_policy->min_rate_limit_ns = min(sg_policy->up_rate_delay_ns, |
| 451 | sg_policy->down_rate_delay_ns); |
| 452 | mutex_unlock(&min_rate_lock); |
| 453 | } |
| 454 | |
| 455 | static ssize_t up_rate_limit_us_show(struct gov_attr_set *attr_set, char *buf) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 456 | { |
| 457 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 458 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 459 | return sprintf(buf, "%u\n", tunables->up_rate_limit_us); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 462 | static ssize_t down_rate_limit_us_show(struct gov_attr_set *attr_set, char *buf) |
| 463 | { |
| 464 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 465 | |
| 466 | return sprintf(buf, "%u\n", tunables->down_rate_limit_us); |
| 467 | } |
| 468 | |
| 469 | static ssize_t up_rate_limit_us_store(struct gov_attr_set *attr_set, |
| 470 | const char *buf, size_t count) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 471 | { |
| 472 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 473 | struct sugov_policy *sg_policy; |
| 474 | unsigned int rate_limit_us; |
| 475 | |
| 476 | if (kstrtouint(buf, 10, &rate_limit_us)) |
| 477 | return -EINVAL; |
| 478 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 479 | tunables->up_rate_limit_us = rate_limit_us; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 480 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 481 | list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) { |
| 482 | sg_policy->up_rate_delay_ns = rate_limit_us * NSEC_PER_USEC; |
| 483 | update_min_rate_limit_ns(sg_policy); |
| 484 | } |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 485 | |
| 486 | return count; |
| 487 | } |
| 488 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 489 | static ssize_t down_rate_limit_us_store(struct gov_attr_set *attr_set, |
| 490 | const char *buf, size_t count) |
| 491 | { |
| 492 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 493 | struct sugov_policy *sg_policy; |
| 494 | unsigned int rate_limit_us; |
| 495 | |
| 496 | if (kstrtouint(buf, 10, &rate_limit_us)) |
| 497 | return -EINVAL; |
| 498 | |
| 499 | tunables->down_rate_limit_us = rate_limit_us; |
| 500 | |
| 501 | list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) { |
| 502 | sg_policy->down_rate_delay_ns = rate_limit_us * NSEC_PER_USEC; |
| 503 | update_min_rate_limit_ns(sg_policy); |
| 504 | } |
| 505 | |
| 506 | return count; |
| 507 | } |
| 508 | |
| 509 | static struct governor_attr up_rate_limit_us = __ATTR_RW(up_rate_limit_us); |
| 510 | static struct governor_attr down_rate_limit_us = __ATTR_RW(down_rate_limit_us); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 511 | |
| 512 | static struct attribute *sugov_attributes[] = { |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 513 | &up_rate_limit_us.attr, |
| 514 | &down_rate_limit_us.attr, |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 515 | NULL |
| 516 | }; |
| 517 | |
| 518 | static struct kobj_type sugov_tunables_ktype = { |
| 519 | .default_attrs = sugov_attributes, |
| 520 | .sysfs_ops = &governor_sysfs_ops, |
| 521 | }; |
| 522 | |
| 523 | /********************** cpufreq governor interface *********************/ |
| 524 | |
| 525 | static struct cpufreq_governor schedutil_gov; |
| 526 | |
| 527 | static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy) |
| 528 | { |
| 529 | struct sugov_policy *sg_policy; |
| 530 | |
| 531 | sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL); |
| 532 | if (!sg_policy) |
| 533 | return NULL; |
| 534 | |
| 535 | sg_policy->policy = policy; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 536 | raw_spin_lock_init(&sg_policy->update_lock); |
| 537 | return sg_policy; |
| 538 | } |
| 539 | |
| 540 | static void sugov_policy_free(struct sugov_policy *sg_policy) |
| 541 | { |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 542 | kfree(sg_policy); |
| 543 | } |
| 544 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 545 | static int sugov_kthread_create(struct sugov_policy *sg_policy) |
| 546 | { |
| 547 | struct task_struct *thread; |
| 548 | struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO / 2 }; |
| 549 | struct cpufreq_policy *policy = sg_policy->policy; |
| 550 | int ret; |
| 551 | |
| 552 | /* kthread only required for slow path */ |
| 553 | if (policy->fast_switch_enabled) |
| 554 | return 0; |
| 555 | |
| 556 | kthread_init_work(&sg_policy->work, sugov_work); |
| 557 | kthread_init_worker(&sg_policy->worker); |
| 558 | thread = kthread_create(kthread_worker_fn, &sg_policy->worker, |
| 559 | "sugov:%d", |
| 560 | cpumask_first(policy->related_cpus)); |
| 561 | if (IS_ERR(thread)) { |
| 562 | pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread)); |
| 563 | return PTR_ERR(thread); |
| 564 | } |
| 565 | |
| 566 | ret = sched_setscheduler_nocheck(thread, SCHED_FIFO, ¶m); |
| 567 | if (ret) { |
| 568 | kthread_stop(thread); |
| 569 | pr_warn("%s: failed to set SCHED_FIFO\n", __func__); |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | sg_policy->thread = thread; |
Viresh Kumar | e2cabe4 | 2017-08-10 09:50:55 +0530 | [diff] [blame] | 574 | |
| 575 | /* Kthread is bound to all CPUs by default */ |
| 576 | if (!policy->dvfs_possible_from_any_cpu) |
| 577 | kthread_bind_mask(thread, policy->related_cpus); |
| 578 | |
Viresh Kumar | 21ef572 | 2016-11-15 13:53:23 +0530 | [diff] [blame] | 579 | init_irq_work(&sg_policy->irq_work, sugov_irq_work); |
| 580 | mutex_init(&sg_policy->work_lock); |
| 581 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 582 | wake_up_process(thread); |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static void sugov_kthread_stop(struct sugov_policy *sg_policy) |
| 588 | { |
| 589 | /* kthread only required for slow path */ |
| 590 | if (sg_policy->policy->fast_switch_enabled) |
| 591 | return; |
| 592 | |
| 593 | kthread_flush_worker(&sg_policy->worker); |
| 594 | kthread_stop(sg_policy->thread); |
Viresh Kumar | 21ef572 | 2016-11-15 13:53:23 +0530 | [diff] [blame] | 595 | mutex_destroy(&sg_policy->work_lock); |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 596 | } |
| 597 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 598 | static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy) |
| 599 | { |
| 600 | struct sugov_tunables *tunables; |
| 601 | |
| 602 | tunables = kzalloc(sizeof(*tunables), GFP_KERNEL); |
| 603 | if (tunables) { |
| 604 | gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook); |
| 605 | if (!have_governor_per_policy()) |
| 606 | global_tunables = tunables; |
| 607 | } |
| 608 | return tunables; |
| 609 | } |
| 610 | |
| 611 | static void sugov_tunables_free(struct sugov_tunables *tunables) |
| 612 | { |
| 613 | if (!have_governor_per_policy()) |
| 614 | global_tunables = NULL; |
| 615 | |
| 616 | kfree(tunables); |
| 617 | } |
| 618 | |
| 619 | static int sugov_init(struct cpufreq_policy *policy) |
| 620 | { |
| 621 | struct sugov_policy *sg_policy; |
| 622 | struct sugov_tunables *tunables; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 623 | int ret = 0; |
| 624 | |
| 625 | /* State should be equivalent to EXIT */ |
| 626 | if (policy->governor_data) |
| 627 | return -EBUSY; |
| 628 | |
Viresh Kumar | 4a71ce4 | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 629 | cpufreq_enable_fast_switch(policy); |
| 630 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 631 | sg_policy = sugov_policy_alloc(policy); |
Viresh Kumar | 4a71ce4 | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 632 | if (!sg_policy) { |
| 633 | ret = -ENOMEM; |
| 634 | goto disable_fast_switch; |
| 635 | } |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 636 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 637 | ret = sugov_kthread_create(sg_policy); |
| 638 | if (ret) |
| 639 | goto free_sg_policy; |
| 640 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 641 | mutex_lock(&global_tunables_lock); |
| 642 | |
| 643 | if (global_tunables) { |
| 644 | if (WARN_ON(have_governor_per_policy())) { |
| 645 | ret = -EINVAL; |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 646 | goto stop_kthread; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 647 | } |
| 648 | policy->governor_data = sg_policy; |
| 649 | sg_policy->tunables = global_tunables; |
| 650 | |
| 651 | gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook); |
| 652 | goto out; |
| 653 | } |
| 654 | |
| 655 | tunables = sugov_tunables_alloc(sg_policy); |
| 656 | if (!tunables) { |
| 657 | ret = -ENOMEM; |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 658 | goto stop_kthread; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 661 | tunables->up_rate_limit_us = cpufreq_policy_transition_delay_us(policy); |
| 662 | tunables->down_rate_limit_us = cpufreq_policy_transition_delay_us(policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 663 | |
| 664 | policy->governor_data = sg_policy; |
| 665 | sg_policy->tunables = tunables; |
| 666 | |
| 667 | ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype, |
| 668 | get_governor_parent_kobj(policy), "%s", |
| 669 | schedutil_gov.name); |
| 670 | if (ret) |
| 671 | goto fail; |
| 672 | |
Viresh Kumar | 8e2ddb0 | 2016-11-15 13:53:20 +0530 | [diff] [blame] | 673 | out: |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 674 | mutex_unlock(&global_tunables_lock); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 675 | return 0; |
| 676 | |
Viresh Kumar | 8e2ddb0 | 2016-11-15 13:53:20 +0530 | [diff] [blame] | 677 | fail: |
Tobin C. Harding | abda293 | 2019-04-30 10:11:44 +1000 | [diff] [blame] | 678 | kobject_put(&tunables->attr_set.kobj); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 679 | policy->governor_data = NULL; |
| 680 | sugov_tunables_free(tunables); |
| 681 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 682 | stop_kthread: |
| 683 | sugov_kthread_stop(sg_policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 684 | mutex_unlock(&global_tunables_lock); |
| 685 | |
Jules Maselbas | 423f2c9 | 2018-03-29 15:43:01 +0100 | [diff] [blame] | 686 | free_sg_policy: |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 687 | sugov_policy_free(sg_policy); |
Viresh Kumar | 4a71ce4 | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 688 | |
| 689 | disable_fast_switch: |
| 690 | cpufreq_disable_fast_switch(policy); |
| 691 | |
Viresh Kumar | 60f05e8 | 2016-05-18 17:55:28 +0530 | [diff] [blame] | 692 | pr_err("initialization failed (error %d)\n", ret); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 693 | return ret; |
| 694 | } |
| 695 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 696 | static void sugov_exit(struct cpufreq_policy *policy) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 697 | { |
| 698 | struct sugov_policy *sg_policy = policy->governor_data; |
| 699 | struct sugov_tunables *tunables = sg_policy->tunables; |
| 700 | unsigned int count; |
| 701 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 702 | mutex_lock(&global_tunables_lock); |
| 703 | |
| 704 | count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook); |
| 705 | policy->governor_data = NULL; |
| 706 | if (!count) |
| 707 | sugov_tunables_free(tunables); |
| 708 | |
| 709 | mutex_unlock(&global_tunables_lock); |
| 710 | |
Viresh Kumar | 02a7b1e | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 711 | sugov_kthread_stop(sg_policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 712 | sugov_policy_free(sg_policy); |
Viresh Kumar | 4a71ce4 | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 713 | cpufreq_disable_fast_switch(policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | static int sugov_start(struct cpufreq_policy *policy) |
| 717 | { |
| 718 | struct sugov_policy *sg_policy = policy->governor_data; |
| 719 | unsigned int cpu; |
| 720 | |
Steve Muckle | 50c26fd | 2016-11-17 10:48:45 +0530 | [diff] [blame] | 721 | sg_policy->up_rate_delay_ns = |
| 722 | sg_policy->tunables->up_rate_limit_us * NSEC_PER_USEC; |
| 723 | sg_policy->down_rate_delay_ns = |
| 724 | sg_policy->tunables->down_rate_limit_us * NSEC_PER_USEC; |
| 725 | update_min_rate_limit_ns(sg_policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 726 | sg_policy->last_freq_update_time = 0; |
| 727 | sg_policy->next_freq = UINT_MAX; |
| 728 | sg_policy->work_in_progress = false; |
| 729 | sg_policy->need_freq_update = false; |
Viresh Kumar | 6c4f0fa | 2017-03-02 14:03:20 +0530 | [diff] [blame] | 730 | sg_policy->cached_raw_freq = 0; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 731 | |
| 732 | for_each_cpu(cpu, policy->cpus) { |
| 733 | struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu); |
| 734 | |
Rafael J. Wysocki | 4296f23 | 2017-03-19 14:30:02 +0100 | [diff] [blame] | 735 | memset(sg_cpu, 0, sizeof(*sg_cpu)); |
Chris Redpath | d62d813 | 2017-11-03 13:36:42 +0000 | [diff] [blame] | 736 | sg_cpu->cpu = cpu; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 737 | sg_cpu->sg_policy = sg_policy; |
Steve Muckle | f1f7125 | 2016-08-25 15:59:17 -0700 | [diff] [blame] | 738 | sg_cpu->flags = SCHED_CPUFREQ_DL; |
Rafael J. Wysocki | 4296f23 | 2017-03-19 14:30:02 +0100 | [diff] [blame] | 739 | sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq; |
Vikram Mulukutla | ab2f7cf | 2017-07-06 10:53:20 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | for_each_cpu(cpu, policy->cpus) { |
| 743 | struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu); |
| 744 | |
Rafael J. Wysocki | 4296f23 | 2017-03-19 14:30:02 +0100 | [diff] [blame] | 745 | cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, |
| 746 | policy_is_shared(policy) ? |
| 747 | sugov_update_shared : |
| 748 | sugov_update_single); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 749 | } |
| 750 | return 0; |
| 751 | } |
| 752 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 753 | static void sugov_stop(struct cpufreq_policy *policy) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 754 | { |
| 755 | struct sugov_policy *sg_policy = policy->governor_data; |
| 756 | unsigned int cpu; |
| 757 | |
| 758 | for_each_cpu(cpu, policy->cpus) |
| 759 | cpufreq_remove_update_util_hook(cpu); |
| 760 | |
| 761 | synchronize_sched(); |
| 762 | |
Viresh Kumar | 21ef572 | 2016-11-15 13:53:23 +0530 | [diff] [blame] | 763 | if (!policy->fast_switch_enabled) { |
| 764 | irq_work_sync(&sg_policy->irq_work); |
| 765 | kthread_cancel_work_sync(&sg_policy->work); |
| 766 | } |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 767 | } |
| 768 | |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 769 | static void sugov_limits(struct cpufreq_policy *policy) |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 770 | { |
| 771 | struct sugov_policy *sg_policy = policy->governor_data; |
| 772 | |
| 773 | if (!policy->fast_switch_enabled) { |
| 774 | mutex_lock(&sg_policy->work_lock); |
Viresh Kumar | bf2be2d | 2016-05-18 17:55:31 +0530 | [diff] [blame] | 775 | cpufreq_policy_apply_limits(policy); |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 776 | mutex_unlock(&sg_policy->work_lock); |
| 777 | } |
| 778 | |
| 779 | sg_policy->need_freq_update = true; |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static struct cpufreq_governor schedutil_gov = { |
| 783 | .name = "schedutil", |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 784 | .owner = THIS_MODULE, |
Viresh Kumar | 560c6e4 | 2017-07-19 15:42:47 +0530 | [diff] [blame] | 785 | .dynamic_switching = true, |
Rafael J. Wysocki | e788892 | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 786 | .init = sugov_init, |
| 787 | .exit = sugov_exit, |
| 788 | .start = sugov_start, |
| 789 | .stop = sugov_stop, |
| 790 | .limits = sugov_limits, |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 791 | }; |
| 792 | |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 793 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL |
| 794 | struct cpufreq_governor *cpufreq_default_governor(void) |
| 795 | { |
| 796 | return &schedutil_gov; |
| 797 | } |
Rafael J. Wysocki | 9bdcb44 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 798 | #endif |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 799 | |
| 800 | static int __init sugov_register(void) |
| 801 | { |
| 802 | return cpufreq_register_governor(&schedutil_gov); |
| 803 | } |
| 804 | fs_initcall(sugov_register); |