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> |
Jonathan Avila | 703e243 | 2017-08-10 13:49:49 -0700 | [diff] [blame] | 18 | #include <linux/sched/sysctl.h> |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 19 | #include "sched.h" |
Juri Lelli | c6e9438 | 2016-12-14 16:10:10 +0000 | [diff] [blame] | 20 | #include "tune.h" |
| 21 | |
Kyle Yan | e2486b7 | 2017-08-25 14:36:53 -0700 | [diff] [blame] | 22 | #ifdef CONFIG_SCHED_WALT |
Juri Lelli | c6e9438 | 2016-12-14 16:10:10 +0000 | [diff] [blame] | 23 | unsigned long boosted_cpu_util(int cpu); |
Kyle Yan | e2486b7 | 2017-08-25 14:36:53 -0700 | [diff] [blame] | 24 | #endif |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 25 | |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 26 | #define SUGOV_KTHREAD_PRIORITY 50 |
| 27 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 28 | struct sugov_tunables { |
| 29 | struct gov_attr_set attr_set; |
| 30 | unsigned int rate_limit_us; |
Saravana Kannan | 3ca7d7a | 2017-09-11 12:33:54 -0700 | [diff] [blame] | 31 | unsigned int hispeed_load; |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 32 | unsigned int hispeed_freq; |
Saravana Kannan | d0a7c6b | 2017-05-23 17:26:32 -0700 | [diff] [blame] | 33 | bool pl; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | struct sugov_policy { |
| 37 | struct cpufreq_policy *policy; |
| 38 | |
| 39 | struct sugov_tunables *tunables; |
| 40 | struct list_head tunables_hook; |
| 41 | |
| 42 | raw_spinlock_t update_lock; /* For shared policies */ |
| 43 | u64 last_freq_update_time; |
| 44 | s64 freq_update_delay_ns; |
Saravana Kannan | 0f34ee9 | 2017-06-28 21:44:14 -0700 | [diff] [blame] | 45 | u64 last_ws; |
| 46 | u64 curr_cycles; |
| 47 | u64 last_cyc_update_time; |
| 48 | unsigned long avg_cap; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 49 | unsigned int next_freq; |
Viresh Kumar | 55a6d46 | 2017-03-02 14:03:20 +0530 | [diff] [blame] | 50 | unsigned int cached_raw_freq; |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 51 | unsigned long hispeed_util; |
| 52 | unsigned long max; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 53 | |
| 54 | /* The next fields are only needed if fast switch cannot be used. */ |
| 55 | struct irq_work irq_work; |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 56 | struct kthread_work work; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 57 | struct mutex work_lock; |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 58 | struct kthread_worker worker; |
| 59 | struct task_struct *thread; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 60 | bool work_in_progress; |
| 61 | |
| 62 | bool need_freq_update; |
| 63 | }; |
| 64 | |
| 65 | struct sugov_cpu { |
| 66 | struct update_util_data update_util; |
| 67 | struct sugov_policy *sg_policy; |
| 68 | |
Rafael J. Wysocki | 193d25c | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 69 | unsigned long iowait_boost; |
| 70 | unsigned long iowait_boost_max; |
| 71 | u64 last_update; |
Steve Muckle | d7439bc | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 72 | |
Saravana Kannan | 0564986 | 2017-05-04 19:03:53 -0700 | [diff] [blame] | 73 | struct sched_walt_cpu_load walt_load; |
| 74 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 75 | /* The fields below are only needed when sharing a policy. */ |
| 76 | unsigned long util; |
| 77 | unsigned long max; |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 78 | unsigned int flags; |
Vikram Mulukutla | 857cffa | 2017-05-04 19:47:06 -0700 | [diff] [blame] | 79 | unsigned int cpu; |
Chris Redpath | 595ae4a | 2017-05-25 15:24:58 +0100 | [diff] [blame] | 80 | |
| 81 | /* The field below is for single-CPU policies only. */ |
| 82 | #ifdef CONFIG_NO_HZ_COMMON |
| 83 | unsigned long saved_idle_calls; |
| 84 | #endif |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static DEFINE_PER_CPU(struct sugov_cpu, sugov_cpu); |
Saravana Kannan | 7070a9e | 2017-08-24 17:02:49 -0700 | [diff] [blame] | 88 | static unsigned int stale_ns; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 89 | |
| 90 | /************************ Governor internals ***********************/ |
| 91 | |
| 92 | static bool sugov_should_update_freq(struct sugov_policy *sg_policy, u64 time) |
| 93 | { |
| 94 | s64 delta_ns; |
| 95 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 96 | if (unlikely(sg_policy->need_freq_update)) { |
| 97 | sg_policy->need_freq_update = false; |
| 98 | /* |
| 99 | * This happens when limits change, so forget the previous |
| 100 | * next_freq value and force an update. |
| 101 | */ |
| 102 | sg_policy->next_freq = UINT_MAX; |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | delta_ns = time - sg_policy->last_freq_update_time; |
| 107 | return delta_ns >= sg_policy->freq_update_delay_ns; |
| 108 | } |
| 109 | |
| 110 | static void sugov_update_commit(struct sugov_policy *sg_policy, u64 time, |
| 111 | unsigned int next_freq) |
| 112 | { |
| 113 | struct cpufreq_policy *policy = sg_policy->policy; |
| 114 | |
Chris Redpath | 6702ce1 | 2017-05-25 15:27:07 +0100 | [diff] [blame] | 115 | if (sg_policy->next_freq == next_freq) |
| 116 | return; |
| 117 | |
| 118 | sg_policy->next_freq = next_freq; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 119 | sg_policy->last_freq_update_time = time; |
| 120 | |
| 121 | if (policy->fast_switch_enabled) { |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 122 | next_freq = cpufreq_driver_fast_switch(policy, next_freq); |
| 123 | if (next_freq == CPUFREQ_ENTRY_INVALID) |
| 124 | return; |
| 125 | |
| 126 | policy->cur = next_freq; |
| 127 | trace_cpu_frequency(next_freq, smp_processor_id()); |
Chris Redpath | 6702ce1 | 2017-05-25 15:27:07 +0100 | [diff] [blame] | 128 | } else { |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 129 | sg_policy->work_in_progress = true; |
| 130 | irq_work_queue(&sg_policy->irq_work); |
| 131 | } |
| 132 | } |
| 133 | |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 134 | #define TARGET_LOAD 80 |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 135 | /** |
| 136 | * get_next_freq - Compute a new frequency for a given cpufreq policy. |
Viresh Kumar | e74f7f1 | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 137 | * @sg_policy: schedutil policy object to compute the new frequency for. |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 138 | * @util: Current CPU utilization. |
| 139 | * @max: CPU capacity. |
| 140 | * |
| 141 | * If the utilization is frequency-invariant, choose the new frequency to be |
| 142 | * proportional to it, that is |
| 143 | * |
| 144 | * next_freq = C * max_freq * util / max |
| 145 | * |
| 146 | * Otherwise, approximate the would-be frequency-invariant utilization by |
| 147 | * util_raw * (curr_freq / max_freq) which leads to |
| 148 | * |
| 149 | * next_freq = C * curr_freq * util_raw / max |
| 150 | * |
| 151 | * 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] | 152 | * |
| 153 | * The lowest driver-supported frequency which is equal or greater than the raw |
| 154 | * next_freq (as calculated above) is returned, subject to policy min/max and |
| 155 | * cpufreq driver limitations. |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 156 | */ |
Viresh Kumar | e74f7f1 | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 157 | static unsigned int get_next_freq(struct sugov_policy *sg_policy, |
| 158 | unsigned long util, unsigned long max) |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 159 | { |
Steve Muckle | d7439bc | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 160 | struct cpufreq_policy *policy = sg_policy->policy; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 161 | unsigned int freq = arch_scale_freq_invariant() ? |
| 162 | policy->cpuinfo.max_freq : policy->cur; |
| 163 | |
Steve Muckle | d7439bc | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 164 | freq = (freq + (freq >> 2)) * util / max; |
| 165 | |
Viresh Kumar | 55a6d46 | 2017-03-02 14:03:20 +0530 | [diff] [blame] | 166 | 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] | 167 | return sg_policy->next_freq; |
Viresh Kumar | 55a6d46 | 2017-03-02 14:03:20 +0530 | [diff] [blame] | 168 | sg_policy->cached_raw_freq = freq; |
Steve Muckle | d7439bc | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 169 | return cpufreq_driver_resolve_freq(policy, freq); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Vikram Mulukutla | 857cffa | 2017-05-04 19:47:06 -0700 | [diff] [blame] | 172 | static void sugov_get_util(unsigned long *util, unsigned long *max, int cpu) |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 173 | { |
Vikram Mulukutla | 857cffa | 2017-05-04 19:47:06 -0700 | [diff] [blame] | 174 | struct rq *rq = cpu_rq(cpu); |
Steve Muckle | 097cf68 | 2016-08-26 11:40:47 -0700 | [diff] [blame] | 175 | unsigned long cfs_max; |
Saravana Kannan | 0564986 | 2017-05-04 19:03:53 -0700 | [diff] [blame] | 176 | struct sugov_cpu *loadcpu = &per_cpu(sugov_cpu, cpu); |
Steve Muckle | 097cf68 | 2016-08-26 11:40:47 -0700 | [diff] [blame] | 177 | |
Vikram Mulukutla | 857cffa | 2017-05-04 19:47:06 -0700 | [diff] [blame] | 178 | cfs_max = arch_scale_cpu_capacity(NULL, cpu); |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 179 | |
| 180 | *util = min(rq->cfs.avg.util_avg, cfs_max); |
| 181 | *max = cfs_max; |
Saravana Kannan | 0564986 | 2017-05-04 19:03:53 -0700 | [diff] [blame] | 182 | |
| 183 | *util = cpu_util_freq(cpu, &loadcpu->walt_load); |
Chris Redpath | a6d6735 | 2017-03-24 17:37:28 +0000 | [diff] [blame] | 184 | *util = boosted_cpu_util(cpu); |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Rafael J. Wysocki | 193d25c | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 187 | static void sugov_set_iowait_boost(struct sugov_cpu *sg_cpu, u64 time, |
| 188 | unsigned int flags) |
| 189 | { |
| 190 | if (flags & SCHED_CPUFREQ_IOWAIT) { |
| 191 | sg_cpu->iowait_boost = sg_cpu->iowait_boost_max; |
| 192 | } else if (sg_cpu->iowait_boost) { |
| 193 | s64 delta_ns = time - sg_cpu->last_update; |
| 194 | |
| 195 | /* Clear iowait_boost if the CPU apprears to have been idle. */ |
| 196 | if (delta_ns > TICK_NSEC) |
| 197 | sg_cpu->iowait_boost = 0; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static void sugov_iowait_boost(struct sugov_cpu *sg_cpu, unsigned long *util, |
| 202 | unsigned long *max) |
| 203 | { |
| 204 | unsigned long boost_util = sg_cpu->iowait_boost; |
| 205 | unsigned long boost_max = sg_cpu->iowait_boost_max; |
| 206 | |
| 207 | if (!boost_util) |
| 208 | return; |
| 209 | |
| 210 | if (*util * boost_max < *max * boost_util) { |
| 211 | *util = boost_util; |
| 212 | *max = boost_max; |
| 213 | } |
| 214 | sg_cpu->iowait_boost >>= 1; |
| 215 | } |
| 216 | |
Saravana Kannan | 0f34ee9 | 2017-06-28 21:44:14 -0700 | [diff] [blame] | 217 | static unsigned long freq_to_util(struct sugov_policy *sg_policy, |
| 218 | unsigned int freq) |
| 219 | { |
| 220 | return mult_frac(sg_policy->max, freq, |
| 221 | sg_policy->policy->cpuinfo.max_freq); |
| 222 | } |
| 223 | |
| 224 | #define KHZ 1000 |
| 225 | static void sugov_track_cycles(struct sugov_policy *sg_policy, |
| 226 | unsigned int prev_freq, |
| 227 | u64 upto) |
| 228 | { |
| 229 | u64 delta_ns, cycles; |
Jonathan Avila | 703e243 | 2017-08-10 13:49:49 -0700 | [diff] [blame] | 230 | |
| 231 | if (unlikely(!sysctl_sched_use_walt_cpu_util)) |
| 232 | return; |
| 233 | |
Saravana Kannan | 0f34ee9 | 2017-06-28 21:44:14 -0700 | [diff] [blame] | 234 | /* Track cycles in current window */ |
| 235 | delta_ns = upto - sg_policy->last_cyc_update_time; |
| 236 | cycles = (prev_freq * delta_ns) / (NSEC_PER_SEC / KHZ); |
| 237 | sg_policy->curr_cycles += cycles; |
| 238 | sg_policy->last_cyc_update_time = upto; |
| 239 | } |
| 240 | |
| 241 | static void sugov_calc_avg_cap(struct sugov_policy *sg_policy, u64 curr_ws, |
| 242 | unsigned int prev_freq) |
| 243 | { |
| 244 | u64 last_ws = sg_policy->last_ws; |
| 245 | unsigned int avg_freq; |
| 246 | |
Jonathan Avila | 703e243 | 2017-08-10 13:49:49 -0700 | [diff] [blame] | 247 | if (unlikely(!sysctl_sched_use_walt_cpu_util)) |
| 248 | return; |
| 249 | |
Saravana Kannan | 0f34ee9 | 2017-06-28 21:44:14 -0700 | [diff] [blame] | 250 | WARN_ON(curr_ws < last_ws); |
| 251 | if (curr_ws <= last_ws) |
| 252 | return; |
| 253 | |
| 254 | /* If we skipped some windows */ |
| 255 | if (curr_ws > (last_ws + sched_ravg_window)) { |
| 256 | avg_freq = prev_freq; |
| 257 | /* Reset tracking history */ |
| 258 | sg_policy->last_cyc_update_time = curr_ws; |
| 259 | } else { |
| 260 | sugov_track_cycles(sg_policy, prev_freq, curr_ws); |
| 261 | avg_freq = sg_policy->curr_cycles; |
| 262 | avg_freq /= sched_ravg_window / (NSEC_PER_SEC / KHZ); |
| 263 | } |
| 264 | sg_policy->avg_cap = freq_to_util(sg_policy, avg_freq); |
| 265 | sg_policy->curr_cycles = 0; |
| 266 | sg_policy->last_ws = curr_ws; |
| 267 | } |
| 268 | |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 269 | #define NL_RATIO 75 |
Saravana Kannan | 3ca7d7a | 2017-09-11 12:33:54 -0700 | [diff] [blame] | 270 | #define DEFAULT_HISPEED_LOAD 90 |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 271 | static void sugov_walt_adjust(struct sugov_cpu *sg_cpu, unsigned long *util, |
| 272 | unsigned long *max) |
| 273 | { |
| 274 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 275 | bool is_migration = sg_cpu->flags & SCHED_CPUFREQ_INTERCLUSTER_MIG; |
| 276 | unsigned long nl = sg_cpu->walt_load.nl; |
| 277 | unsigned long cpu_util = sg_cpu->util; |
| 278 | bool is_hiload; |
| 279 | |
Jonathan Avila | 703e243 | 2017-08-10 13:49:49 -0700 | [diff] [blame] | 280 | if (unlikely(!sysctl_sched_use_walt_cpu_util)) |
| 281 | return; |
| 282 | |
Saravana Kannan | 36faa28 | 2017-06-28 21:44:56 -0700 | [diff] [blame] | 283 | is_hiload = (cpu_util >= mult_frac(sg_policy->avg_cap, |
Saravana Kannan | 3ca7d7a | 2017-09-11 12:33:54 -0700 | [diff] [blame] | 284 | sg_policy->tunables->hispeed_load, |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 285 | 100)); |
| 286 | |
Rohit Gupta | 5573e73 | 2017-06-21 10:08:07 -0700 | [diff] [blame] | 287 | if (is_hiload && !is_migration) |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 288 | *util = max(*util, sg_policy->hispeed_util); |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 289 | |
| 290 | if (is_hiload && nl >= mult_frac(cpu_util, NL_RATIO, 100)) |
| 291 | *util = *max; |
| 292 | |
Saravana Kannan | d0a7c6b | 2017-05-23 17:26:32 -0700 | [diff] [blame] | 293 | if (sg_policy->tunables->pl) |
| 294 | *util = max(*util, sg_cpu->walt_load.pl); |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Chris Redpath | 595ae4a | 2017-05-25 15:24:58 +0100 | [diff] [blame] | 297 | #ifdef CONFIG_NO_HZ_COMMON |
| 298 | static bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) |
| 299 | { |
| 300 | unsigned long idle_calls = tick_nohz_get_idle_calls(); |
| 301 | bool ret = idle_calls == sg_cpu->saved_idle_calls; |
| 302 | |
| 303 | sg_cpu->saved_idle_calls = idle_calls; |
| 304 | return ret; |
| 305 | } |
| 306 | #else |
| 307 | static inline bool sugov_cpu_is_busy(struct sugov_cpu *sg_cpu) { return false; } |
| 308 | #endif /* CONFIG_NO_HZ_COMMON */ |
| 309 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 310 | 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] | 311 | unsigned int flags) |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 312 | { |
| 313 | struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util); |
| 314 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
| 315 | struct cpufreq_policy *policy = sg_policy->policy; |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 316 | unsigned long util, max; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 317 | unsigned int next_f; |
Chris Redpath | 595ae4a | 2017-05-25 15:24:58 +0100 | [diff] [blame] | 318 | bool busy; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 319 | |
Rafael J. Wysocki | 193d25c | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 320 | sugov_set_iowait_boost(sg_cpu, time, flags); |
| 321 | sg_cpu->last_update = time; |
| 322 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 323 | if (!sugov_should_update_freq(sg_policy, time)) |
| 324 | return; |
| 325 | |
Chris Redpath | 595ae4a | 2017-05-25 15:24:58 +0100 | [diff] [blame] | 326 | busy = sugov_cpu_is_busy(sg_cpu); |
Saravana Kannan | 76ef171 | 2017-05-04 19:44:36 -0700 | [diff] [blame] | 327 | flags &= ~SCHED_CPUFREQ_RT_DL; |
| 328 | |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 329 | if (flags & SCHED_CPUFREQ_RT_DL) { |
| 330 | next_f = policy->cpuinfo.max_freq; |
| 331 | } else { |
Vikram Mulukutla | 857cffa | 2017-05-04 19:47:06 -0700 | [diff] [blame] | 332 | sugov_get_util(&util, &max, sg_cpu->cpu); |
Rafael J. Wysocki | 193d25c | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 333 | sugov_iowait_boost(sg_cpu, &util, &max); |
Saravana Kannan | 0f34ee9 | 2017-06-28 21:44:14 -0700 | [diff] [blame] | 334 | sugov_calc_avg_cap(sg_policy, sg_cpu->walt_load.ws, |
| 335 | sg_policy->policy->cur); |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 336 | sugov_walt_adjust(sg_cpu, &util, &max); |
Viresh Kumar | e74f7f1 | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 337 | next_f = get_next_freq(sg_policy, util, max); |
Chris Redpath | 595ae4a | 2017-05-25 15:24:58 +0100 | [diff] [blame] | 338 | /* |
| 339 | * Do not reduce the frequency if the CPU has not been idle |
| 340 | * recently, as the reduction is likely to be premature then. |
| 341 | */ |
| 342 | if (busy && next_f < sg_policy->next_freq) |
| 343 | next_f = sg_policy->next_freq; |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 344 | } |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 345 | sugov_update_commit(sg_policy, time, next_f); |
| 346 | } |
| 347 | |
Chris Redpath | 3915186 | 2017-05-25 15:22:59 +0100 | [diff] [blame] | 348 | static unsigned int sugov_next_freq_shared(struct sugov_cpu *sg_cpu) |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 349 | { |
Steve Muckle | d7439bc | 2016-07-13 13:25:26 -0700 | [diff] [blame] | 350 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 351 | struct cpufreq_policy *policy = sg_policy->policy; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 352 | u64 last_freq_update_time = sg_policy->last_freq_update_time; |
Chris Redpath | 3915186 | 2017-05-25 15:22:59 +0100 | [diff] [blame] | 353 | unsigned long util = 0, max = 1; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 354 | unsigned int j; |
| 355 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 356 | for_each_cpu(j, policy->cpus) { |
Chris Redpath | 3915186 | 2017-05-25 15:22:59 +0100 | [diff] [blame] | 357 | struct sugov_cpu *j_sg_cpu = &per_cpu(sugov_cpu, j); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 358 | unsigned long j_util, j_max; |
| 359 | s64 delta_ns; |
| 360 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 361 | /* |
| 362 | * If the CPU utilization was last updated before the previous |
| 363 | * frequency update and the time elapsed between the last update |
| 364 | * of the CPU utilization and the last frequency update is long |
| 365 | * 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] | 366 | * idle now (and clear iowait_boost for it). |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 367 | */ |
| 368 | delta_ns = last_freq_update_time - j_sg_cpu->last_update; |
Saravana Kannan | 7070a9e | 2017-08-24 17:02:49 -0700 | [diff] [blame] | 369 | if (delta_ns > stale_ns) { |
Rafael J. Wysocki | 193d25c | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 370 | j_sg_cpu->iowait_boost = 0; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 371 | continue; |
Rafael J. Wysocki | 193d25c | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 372 | } |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 373 | if (j_sg_cpu->flags & SCHED_CPUFREQ_RT_DL) |
Chris Redpath | 3915186 | 2017-05-25 15:22:59 +0100 | [diff] [blame] | 374 | return policy->cpuinfo.max_freq; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 375 | |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 376 | j_util = j_sg_cpu->util; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 377 | j_max = j_sg_cpu->max; |
| 378 | if (j_util * max > j_max * util) { |
| 379 | util = j_util; |
| 380 | max = j_max; |
| 381 | } |
Rafael J. Wysocki | 193d25c | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 382 | |
| 383 | sugov_iowait_boost(j_sg_cpu, &util, &max); |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 384 | sugov_walt_adjust(j_sg_cpu, &util, &max); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Viresh Kumar | e74f7f1 | 2017-03-02 14:03:21 +0530 | [diff] [blame] | 387 | return get_next_freq(sg_policy, util, max); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | 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] | 391 | unsigned int flags) |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 392 | { |
| 393 | struct sugov_cpu *sg_cpu = container_of(hook, struct sugov_cpu, update_util); |
| 394 | struct sugov_policy *sg_policy = sg_cpu->sg_policy; |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 395 | unsigned long util, max, hs_util; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 396 | unsigned int next_f; |
| 397 | |
Vikram Mulukutla | 857cffa | 2017-05-04 19:47:06 -0700 | [diff] [blame] | 398 | sugov_get_util(&util, &max, sg_cpu->cpu); |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 399 | |
Saravana Kannan | 76ef171 | 2017-05-04 19:44:36 -0700 | [diff] [blame] | 400 | flags &= ~SCHED_CPUFREQ_RT_DL; |
| 401 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 402 | raw_spin_lock(&sg_policy->update_lock); |
| 403 | |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 404 | if (sg_policy->max != max) { |
Saravana Kannan | af541e1 | 2017-06-28 20:16:15 -0700 | [diff] [blame] | 405 | sg_policy->max = max; |
| 406 | hs_util = freq_to_util(sg_policy, |
| 407 | sg_policy->tunables->hispeed_freq); |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 408 | hs_util = mult_frac(hs_util, TARGET_LOAD, 100); |
| 409 | sg_policy->hispeed_util = hs_util; |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 412 | sg_cpu->util = util; |
| 413 | sg_cpu->max = max; |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 414 | sg_cpu->flags = flags; |
Rafael J. Wysocki | 193d25c | 2016-09-10 00:00:31 +0200 | [diff] [blame] | 415 | |
| 416 | sugov_set_iowait_boost(sg_cpu, time, flags); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 417 | sg_cpu->last_update = time; |
| 418 | |
Saravana Kannan | 0f34ee9 | 2017-06-28 21:44:14 -0700 | [diff] [blame] | 419 | sugov_calc_avg_cap(sg_policy, sg_cpu->walt_load.ws, |
| 420 | sg_policy->policy->cur); |
| 421 | |
Rohit Gupta | 2cd0e9e | 2017-08-07 10:50:35 -0700 | [diff] [blame] | 422 | trace_sugov_util_update(sg_cpu->cpu, sg_cpu->util, sg_policy->avg_cap, |
| 423 | max, sg_cpu->walt_load.nl, |
Saravana Kannan | 12a35ed | 2017-03-27 15:46:28 -0700 | [diff] [blame] | 424 | sg_cpu->walt_load.pl, flags); |
| 425 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 426 | if (sugov_should_update_freq(sg_policy, time)) { |
Kyle Yan | e2486b7 | 2017-08-25 14:36:53 -0700 | [diff] [blame] | 427 | if (flags & SCHED_CPUFREQ_RT_DL) |
Chris Redpath | 3915186 | 2017-05-25 15:22:59 +0100 | [diff] [blame] | 428 | next_f = sg_policy->policy->cpuinfo.max_freq; |
| 429 | else |
| 430 | next_f = sugov_next_freq_shared(sg_cpu); |
| 431 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 432 | sugov_update_commit(sg_policy, time, next_f); |
| 433 | } |
| 434 | |
| 435 | raw_spin_unlock(&sg_policy->update_lock); |
| 436 | } |
| 437 | |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 438 | static void sugov_work(struct kthread_work *work) |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 439 | { |
| 440 | struct sugov_policy *sg_policy = container_of(work, struct sugov_policy, work); |
Saravana Kannan | 01adff12 | 2017-07-11 17:39:53 -0700 | [diff] [blame] | 441 | unsigned long flags; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 442 | |
| 443 | mutex_lock(&sg_policy->work_lock); |
Saravana Kannan | 01adff12 | 2017-07-11 17:39:53 -0700 | [diff] [blame] | 444 | raw_spin_lock_irqsave(&sg_policy->update_lock, flags); |
Saravana Kannan | 0f34ee9 | 2017-06-28 21:44:14 -0700 | [diff] [blame] | 445 | sugov_track_cycles(sg_policy, sg_policy->policy->cur, |
Stephen Boyd | 24c1812 | 2017-08-15 10:39:25 -0700 | [diff] [blame] | 446 | ktime_get_ns()); |
Saravana Kannan | 01adff12 | 2017-07-11 17:39:53 -0700 | [diff] [blame] | 447 | raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 448 | __cpufreq_driver_target(sg_policy->policy, sg_policy->next_freq, |
| 449 | CPUFREQ_RELATION_L); |
| 450 | mutex_unlock(&sg_policy->work_lock); |
| 451 | |
| 452 | sg_policy->work_in_progress = false; |
| 453 | } |
| 454 | |
| 455 | static void sugov_irq_work(struct irq_work *irq_work) |
| 456 | { |
| 457 | struct sugov_policy *sg_policy; |
| 458 | |
| 459 | sg_policy = container_of(irq_work, struct sugov_policy, irq_work); |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 460 | |
| 461 | /* |
Viresh Kumar | 75526a2 | 2016-11-24 13:51:11 +0530 | [diff] [blame] | 462 | * For RT and deadline tasks, the schedutil governor shoots the |
| 463 | * frequency to maximum. Special care must be taken to ensure that this |
| 464 | * kthread doesn't result in the same behavior. |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 465 | * |
| 466 | * 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] | 467 | * updated only at the end of the sugov_work() function and before that |
| 468 | * the schedutil governor rejects all other frequency scaling requests. |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 469 | * |
Viresh Kumar | 75526a2 | 2016-11-24 13:51:11 +0530 | [diff] [blame] | 470 | * 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] | 471 | * after the work_in_progress flag is cleared. The effects of that are |
| 472 | * neglected for now. |
| 473 | */ |
| 474 | kthread_queue_work(&sg_policy->worker, &sg_policy->work); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /************************** sysfs interface ************************/ |
| 478 | |
| 479 | static struct sugov_tunables *global_tunables; |
| 480 | static DEFINE_MUTEX(global_tunables_lock); |
| 481 | |
| 482 | static inline struct sugov_tunables *to_sugov_tunables(struct gov_attr_set *attr_set) |
| 483 | { |
| 484 | return container_of(attr_set, struct sugov_tunables, attr_set); |
| 485 | } |
| 486 | |
| 487 | static ssize_t rate_limit_us_show(struct gov_attr_set *attr_set, char *buf) |
| 488 | { |
| 489 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 490 | |
| 491 | return sprintf(buf, "%u\n", tunables->rate_limit_us); |
| 492 | } |
| 493 | |
| 494 | static ssize_t rate_limit_us_store(struct gov_attr_set *attr_set, const char *buf, |
| 495 | size_t count) |
| 496 | { |
| 497 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 498 | struct sugov_policy *sg_policy; |
| 499 | unsigned int rate_limit_us; |
| 500 | |
| 501 | if (kstrtouint(buf, 10, &rate_limit_us)) |
| 502 | return -EINVAL; |
| 503 | |
| 504 | tunables->rate_limit_us = rate_limit_us; |
| 505 | |
| 506 | list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) |
| 507 | sg_policy->freq_update_delay_ns = rate_limit_us * NSEC_PER_USEC; |
| 508 | |
| 509 | return count; |
| 510 | } |
| 511 | |
Saravana Kannan | 3ca7d7a | 2017-09-11 12:33:54 -0700 | [diff] [blame] | 512 | static ssize_t hispeed_load_show(struct gov_attr_set *attr_set, char *buf) |
| 513 | { |
| 514 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 515 | |
Saravana Kannan | 95e1d76 | 2017-10-11 17:46:11 -0700 | [diff] [blame^] | 516 | return scnprintf(buf, PAGE_SIZE, "%u\n", tunables->hispeed_load); |
Saravana Kannan | 3ca7d7a | 2017-09-11 12:33:54 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static ssize_t hispeed_load_store(struct gov_attr_set *attr_set, |
| 520 | const char *buf, size_t count) |
| 521 | { |
| 522 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 523 | |
| 524 | if (kstrtouint(buf, 10, &tunables->hispeed_load)) |
| 525 | return -EINVAL; |
| 526 | |
| 527 | tunables->hispeed_load = min(100U, tunables->hispeed_load); |
| 528 | |
| 529 | return count; |
| 530 | } |
| 531 | |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 532 | static ssize_t hispeed_freq_show(struct gov_attr_set *attr_set, char *buf) |
| 533 | { |
| 534 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 535 | |
Saravana Kannan | 95e1d76 | 2017-10-11 17:46:11 -0700 | [diff] [blame^] | 536 | return scnprintf(buf, PAGE_SIZE, "%u\n", tunables->hispeed_freq); |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static ssize_t hispeed_freq_store(struct gov_attr_set *attr_set, |
| 540 | const char *buf, size_t count) |
| 541 | { |
| 542 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 543 | unsigned int val; |
| 544 | struct sugov_policy *sg_policy; |
| 545 | unsigned long hs_util; |
Saravana Kannan | 01adff12 | 2017-07-11 17:39:53 -0700 | [diff] [blame] | 546 | unsigned long flags; |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 547 | |
| 548 | if (kstrtouint(buf, 10, &val)) |
| 549 | return -EINVAL; |
| 550 | |
| 551 | tunables->hispeed_freq = val; |
| 552 | list_for_each_entry(sg_policy, &attr_set->policy_list, tunables_hook) { |
Saravana Kannan | 01adff12 | 2017-07-11 17:39:53 -0700 | [diff] [blame] | 553 | raw_spin_lock_irqsave(&sg_policy->update_lock, flags); |
Saravana Kannan | af541e1 | 2017-06-28 20:16:15 -0700 | [diff] [blame] | 554 | hs_util = freq_to_util(sg_policy, |
| 555 | sg_policy->tunables->hispeed_freq); |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 556 | hs_util = mult_frac(hs_util, TARGET_LOAD, 100); |
| 557 | sg_policy->hispeed_util = hs_util; |
Saravana Kannan | 01adff12 | 2017-07-11 17:39:53 -0700 | [diff] [blame] | 558 | raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags); |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | return count; |
| 562 | } |
| 563 | |
Saravana Kannan | d0a7c6b | 2017-05-23 17:26:32 -0700 | [diff] [blame] | 564 | static ssize_t pl_show(struct gov_attr_set *attr_set, char *buf) |
| 565 | { |
| 566 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 567 | |
Saravana Kannan | 95e1d76 | 2017-10-11 17:46:11 -0700 | [diff] [blame^] | 568 | return scnprintf(buf, PAGE_SIZE, "%u\n", tunables->pl); |
Saravana Kannan | d0a7c6b | 2017-05-23 17:26:32 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | static ssize_t pl_store(struct gov_attr_set *attr_set, const char *buf, |
| 572 | size_t count) |
| 573 | { |
| 574 | struct sugov_tunables *tunables = to_sugov_tunables(attr_set); |
| 575 | |
| 576 | if (kstrtobool(buf, &tunables->pl)) |
| 577 | return -EINVAL; |
| 578 | |
| 579 | return count; |
| 580 | } |
| 581 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 582 | static struct governor_attr rate_limit_us = __ATTR_RW(rate_limit_us); |
Saravana Kannan | 3ca7d7a | 2017-09-11 12:33:54 -0700 | [diff] [blame] | 583 | static struct governor_attr hispeed_load = __ATTR_RW(hispeed_load); |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 584 | static struct governor_attr hispeed_freq = __ATTR_RW(hispeed_freq); |
Saravana Kannan | d0a7c6b | 2017-05-23 17:26:32 -0700 | [diff] [blame] | 585 | static struct governor_attr pl = __ATTR_RW(pl); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 586 | |
| 587 | static struct attribute *sugov_attributes[] = { |
| 588 | &rate_limit_us.attr, |
Saravana Kannan | 3ca7d7a | 2017-09-11 12:33:54 -0700 | [diff] [blame] | 589 | &hispeed_load.attr, |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 590 | &hispeed_freq.attr, |
Saravana Kannan | d0a7c6b | 2017-05-23 17:26:32 -0700 | [diff] [blame] | 591 | &pl.attr, |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 592 | NULL |
| 593 | }; |
| 594 | |
| 595 | static struct kobj_type sugov_tunables_ktype = { |
| 596 | .default_attrs = sugov_attributes, |
| 597 | .sysfs_ops = &governor_sysfs_ops, |
| 598 | }; |
| 599 | |
| 600 | /********************** cpufreq governor interface *********************/ |
| 601 | |
| 602 | static struct cpufreq_governor schedutil_gov; |
| 603 | |
| 604 | static struct sugov_policy *sugov_policy_alloc(struct cpufreq_policy *policy) |
| 605 | { |
| 606 | struct sugov_policy *sg_policy; |
| 607 | |
| 608 | sg_policy = kzalloc(sizeof(*sg_policy), GFP_KERNEL); |
| 609 | if (!sg_policy) |
| 610 | return NULL; |
| 611 | |
| 612 | sg_policy->policy = policy; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 613 | raw_spin_lock_init(&sg_policy->update_lock); |
| 614 | return sg_policy; |
| 615 | } |
| 616 | |
| 617 | static void sugov_policy_free(struct sugov_policy *sg_policy) |
| 618 | { |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 619 | kfree(sg_policy); |
| 620 | } |
| 621 | |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 622 | static int sugov_kthread_create(struct sugov_policy *sg_policy) |
| 623 | { |
| 624 | struct task_struct *thread; |
| 625 | struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO / 2 }; |
| 626 | struct cpufreq_policy *policy = sg_policy->policy; |
| 627 | int ret; |
| 628 | |
| 629 | /* kthread only required for slow path */ |
| 630 | if (policy->fast_switch_enabled) |
| 631 | return 0; |
| 632 | |
| 633 | kthread_init_work(&sg_policy->work, sugov_work); |
| 634 | kthread_init_worker(&sg_policy->worker); |
| 635 | thread = kthread_create(kthread_worker_fn, &sg_policy->worker, |
| 636 | "sugov:%d", |
| 637 | cpumask_first(policy->related_cpus)); |
| 638 | if (IS_ERR(thread)) { |
| 639 | pr_err("failed to create sugov thread: %ld\n", PTR_ERR(thread)); |
| 640 | return PTR_ERR(thread); |
| 641 | } |
| 642 | |
| 643 | ret = sched_setscheduler_nocheck(thread, SCHED_FIFO, ¶m); |
| 644 | if (ret) { |
| 645 | kthread_stop(thread); |
| 646 | pr_warn("%s: failed to set SCHED_FIFO\n", __func__); |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | sg_policy->thread = thread; |
| 651 | kthread_bind_mask(thread, policy->related_cpus); |
Viresh Kumar | 0ced0be | 2016-11-15 13:53:23 +0530 | [diff] [blame] | 652 | init_irq_work(&sg_policy->irq_work, sugov_irq_work); |
| 653 | mutex_init(&sg_policy->work_lock); |
| 654 | |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 655 | wake_up_process(thread); |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static void sugov_kthread_stop(struct sugov_policy *sg_policy) |
| 661 | { |
| 662 | /* kthread only required for slow path */ |
| 663 | if (sg_policy->policy->fast_switch_enabled) |
| 664 | return; |
| 665 | |
| 666 | kthread_flush_worker(&sg_policy->worker); |
| 667 | kthread_stop(sg_policy->thread); |
Viresh Kumar | 0ced0be | 2016-11-15 13:53:23 +0530 | [diff] [blame] | 668 | mutex_destroy(&sg_policy->work_lock); |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 669 | } |
| 670 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 671 | static struct sugov_tunables *sugov_tunables_alloc(struct sugov_policy *sg_policy) |
| 672 | { |
| 673 | struct sugov_tunables *tunables; |
| 674 | |
| 675 | tunables = kzalloc(sizeof(*tunables), GFP_KERNEL); |
| 676 | if (tunables) { |
| 677 | gov_attr_set_init(&tunables->attr_set, &sg_policy->tunables_hook); |
| 678 | if (!have_governor_per_policy()) |
| 679 | global_tunables = tunables; |
| 680 | } |
| 681 | return tunables; |
| 682 | } |
| 683 | |
| 684 | static void sugov_tunables_free(struct sugov_tunables *tunables) |
| 685 | { |
| 686 | if (!have_governor_per_policy()) |
| 687 | global_tunables = NULL; |
| 688 | |
| 689 | kfree(tunables); |
| 690 | } |
| 691 | |
| 692 | static int sugov_init(struct cpufreq_policy *policy) |
| 693 | { |
| 694 | struct sugov_policy *sg_policy; |
| 695 | struct sugov_tunables *tunables; |
| 696 | unsigned int lat; |
| 697 | int ret = 0; |
| 698 | |
| 699 | /* State should be equivalent to EXIT */ |
| 700 | if (policy->governor_data) |
| 701 | return -EBUSY; |
| 702 | |
Viresh Kumar | 6d5551b | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 703 | cpufreq_enable_fast_switch(policy); |
| 704 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 705 | sg_policy = sugov_policy_alloc(policy); |
Viresh Kumar | 6d5551b | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 706 | if (!sg_policy) { |
| 707 | ret = -ENOMEM; |
| 708 | goto disable_fast_switch; |
| 709 | } |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 710 | |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 711 | ret = sugov_kthread_create(sg_policy); |
| 712 | if (ret) |
| 713 | goto free_sg_policy; |
| 714 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 715 | mutex_lock(&global_tunables_lock); |
| 716 | |
| 717 | if (global_tunables) { |
| 718 | if (WARN_ON(have_governor_per_policy())) { |
| 719 | ret = -EINVAL; |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 720 | goto stop_kthread; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 721 | } |
| 722 | policy->governor_data = sg_policy; |
| 723 | sg_policy->tunables = global_tunables; |
| 724 | |
| 725 | gov_attr_set_get(&global_tunables->attr_set, &sg_policy->tunables_hook); |
| 726 | goto out; |
| 727 | } |
| 728 | |
| 729 | tunables = sugov_tunables_alloc(sg_policy); |
| 730 | if (!tunables) { |
| 731 | ret = -ENOMEM; |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 732 | goto stop_kthread; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | tunables->rate_limit_us = LATENCY_MULTIPLIER; |
Saravana Kannan | 3ca7d7a | 2017-09-11 12:33:54 -0700 | [diff] [blame] | 736 | tunables->hispeed_load = DEFAULT_HISPEED_LOAD; |
Rohit Gupta | 3024963 | 2017-02-02 18:39:07 -0800 | [diff] [blame] | 737 | tunables->hispeed_freq = 0; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 738 | lat = policy->cpuinfo.transition_latency / NSEC_PER_USEC; |
| 739 | if (lat) |
| 740 | tunables->rate_limit_us *= lat; |
| 741 | |
| 742 | policy->governor_data = sg_policy; |
| 743 | sg_policy->tunables = tunables; |
Saravana Kannan | 7070a9e | 2017-08-24 17:02:49 -0700 | [diff] [blame] | 744 | stale_ns = sched_ravg_window + (sched_ravg_window >> 3); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 745 | |
| 746 | ret = kobject_init_and_add(&tunables->attr_set.kobj, &sugov_tunables_ktype, |
| 747 | get_governor_parent_kobj(policy), "%s", |
| 748 | schedutil_gov.name); |
| 749 | if (ret) |
| 750 | goto fail; |
| 751 | |
Viresh Kumar | b5b1160 | 2016-11-15 13:53:20 +0530 | [diff] [blame] | 752 | out: |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 753 | mutex_unlock(&global_tunables_lock); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 754 | return 0; |
| 755 | |
Viresh Kumar | b5b1160 | 2016-11-15 13:53:20 +0530 | [diff] [blame] | 756 | fail: |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 757 | policy->governor_data = NULL; |
| 758 | sugov_tunables_free(tunables); |
| 759 | |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 760 | stop_kthread: |
| 761 | sugov_kthread_stop(sg_policy); |
| 762 | |
Viresh Kumar | b5b1160 | 2016-11-15 13:53:20 +0530 | [diff] [blame] | 763 | free_sg_policy: |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 764 | mutex_unlock(&global_tunables_lock); |
| 765 | |
| 766 | sugov_policy_free(sg_policy); |
Viresh Kumar | 6d5551b | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 767 | |
| 768 | disable_fast_switch: |
| 769 | cpufreq_disable_fast_switch(policy); |
| 770 | |
Viresh Kumar | 87ecf32 | 2016-05-18 17:55:28 +0530 | [diff] [blame] | 771 | pr_err("initialization failed (error %d)\n", ret); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 772 | return ret; |
| 773 | } |
| 774 | |
Rafael J. Wysocki | 48f5adc | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 775 | static void sugov_exit(struct cpufreq_policy *policy) |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 776 | { |
| 777 | struct sugov_policy *sg_policy = policy->governor_data; |
| 778 | struct sugov_tunables *tunables = sg_policy->tunables; |
| 779 | unsigned int count; |
| 780 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 781 | mutex_lock(&global_tunables_lock); |
| 782 | |
| 783 | count = gov_attr_set_put(&tunables->attr_set, &sg_policy->tunables_hook); |
| 784 | policy->governor_data = NULL; |
| 785 | if (!count) |
| 786 | sugov_tunables_free(tunables); |
| 787 | |
| 788 | mutex_unlock(&global_tunables_lock); |
| 789 | |
Viresh Kumar | a231c65 | 2016-11-15 13:53:22 +0530 | [diff] [blame] | 790 | sugov_kthread_stop(sg_policy); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 791 | sugov_policy_free(sg_policy); |
Viresh Kumar | 6d5551b | 2016-11-15 13:53:21 +0530 | [diff] [blame] | 792 | cpufreq_disable_fast_switch(policy); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static int sugov_start(struct cpufreq_policy *policy) |
| 796 | { |
| 797 | struct sugov_policy *sg_policy = policy->governor_data; |
| 798 | unsigned int cpu; |
| 799 | |
| 800 | sg_policy->freq_update_delay_ns = sg_policy->tunables->rate_limit_us * NSEC_PER_USEC; |
| 801 | sg_policy->last_freq_update_time = 0; |
| 802 | sg_policy->next_freq = UINT_MAX; |
| 803 | sg_policy->work_in_progress = false; |
| 804 | sg_policy->need_freq_update = false; |
Viresh Kumar | 55a6d46 | 2017-03-02 14:03:20 +0530 | [diff] [blame] | 805 | sg_policy->cached_raw_freq = 0; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 806 | |
| 807 | for_each_cpu(cpu, policy->cpus) { |
| 808 | struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu); |
| 809 | |
Rafael J. Wysocki | 7922ae4 | 2017-03-19 14:30:02 +0100 | [diff] [blame] | 810 | memset(sg_cpu, 0, sizeof(*sg_cpu)); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 811 | sg_cpu->sg_policy = sg_policy; |
Vikram Mulukutla | 857cffa | 2017-05-04 19:47:06 -0700 | [diff] [blame] | 812 | sg_cpu->cpu = cpu; |
Rafael J. Wysocki | 7922ae4 | 2017-03-19 14:30:02 +0100 | [diff] [blame] | 813 | sg_cpu->flags = SCHED_CPUFREQ_RT; |
| 814 | sg_cpu->iowait_boost_max = policy->cpuinfo.max_freq; |
Vikram Mulukutla | eb84372 | 2017-07-06 10:05:52 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | for_each_cpu(cpu, policy->cpus) { |
| 818 | struct sugov_cpu *sg_cpu = &per_cpu(sugov_cpu, cpu); |
| 819 | |
Rafael J. Wysocki | 7922ae4 | 2017-03-19 14:30:02 +0100 | [diff] [blame] | 820 | cpufreq_add_update_util_hook(cpu, &sg_cpu->update_util, |
| 821 | policy_is_shared(policy) ? |
| 822 | sugov_update_shared : |
| 823 | sugov_update_single); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 824 | } |
| 825 | return 0; |
| 826 | } |
| 827 | |
Rafael J. Wysocki | 48f5adc | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 828 | static void sugov_stop(struct cpufreq_policy *policy) |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 829 | { |
| 830 | struct sugov_policy *sg_policy = policy->governor_data; |
| 831 | unsigned int cpu; |
| 832 | |
| 833 | for_each_cpu(cpu, policy->cpus) |
| 834 | cpufreq_remove_update_util_hook(cpu); |
| 835 | |
| 836 | synchronize_sched(); |
| 837 | |
Viresh Kumar | 0ced0be | 2016-11-15 13:53:23 +0530 | [diff] [blame] | 838 | if (!policy->fast_switch_enabled) { |
| 839 | irq_work_sync(&sg_policy->irq_work); |
| 840 | kthread_cancel_work_sync(&sg_policy->work); |
| 841 | } |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 842 | } |
| 843 | |
Rafael J. Wysocki | 48f5adc | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 844 | static void sugov_limits(struct cpufreq_policy *policy) |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 845 | { |
| 846 | struct sugov_policy *sg_policy = policy->governor_data; |
Saravana Kannan | 01adff12 | 2017-07-11 17:39:53 -0700 | [diff] [blame] | 847 | unsigned long flags; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 848 | |
| 849 | if (!policy->fast_switch_enabled) { |
| 850 | mutex_lock(&sg_policy->work_lock); |
Saravana Kannan | 01adff12 | 2017-07-11 17:39:53 -0700 | [diff] [blame] | 851 | raw_spin_lock_irqsave(&sg_policy->update_lock, flags); |
Saravana Kannan | 0f34ee9 | 2017-06-28 21:44:14 -0700 | [diff] [blame] | 852 | sugov_track_cycles(sg_policy, sg_policy->policy->cur, |
Stephen Boyd | 24c1812 | 2017-08-15 10:39:25 -0700 | [diff] [blame] | 853 | ktime_get_ns()); |
Saravana Kannan | 01adff12 | 2017-07-11 17:39:53 -0700 | [diff] [blame] | 854 | raw_spin_unlock_irqrestore(&sg_policy->update_lock, flags); |
Viresh Kumar | 73d427c | 2016-05-18 17:55:31 +0530 | [diff] [blame] | 855 | cpufreq_policy_apply_limits(policy); |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 856 | mutex_unlock(&sg_policy->work_lock); |
| 857 | } |
| 858 | |
| 859 | sg_policy->need_freq_update = true; |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | static struct cpufreq_governor schedutil_gov = { |
| 863 | .name = "schedutil", |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 864 | .owner = THIS_MODULE, |
Rafael J. Wysocki | 48f5adc | 2016-06-02 23:24:15 +0200 | [diff] [blame] | 865 | .init = sugov_init, |
| 866 | .exit = sugov_exit, |
| 867 | .start = sugov_start, |
| 868 | .stop = sugov_stop, |
| 869 | .limits = sugov_limits, |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 870 | }; |
| 871 | |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 872 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL |
| 873 | struct cpufreq_governor *cpufreq_default_governor(void) |
| 874 | { |
| 875 | return &schedutil_gov; |
| 876 | } |
Rafael J. Wysocki | b1d0976 | 2016-04-02 01:09:12 +0200 | [diff] [blame] | 877 | #endif |
Rafael J. Wysocki | c456872 | 2016-08-16 22:14:55 +0200 | [diff] [blame] | 878 | |
| 879 | static int __init sugov_register(void) |
| 880 | { |
| 881 | return cpufreq_register_governor(&schedutil_gov); |
| 882 | } |
| 883 | fs_initcall(sugov_register); |