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