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