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