Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/cpufreq/cpufreq_interactive.c |
| 3 | * |
| 4 | * Copyright (C) 2010 Google, Inc. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * Author: Mike Chan (mike@android.com) |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/cpu.h> |
| 20 | #include <linux/cpumask.h> |
| 21 | #include <linux/cpufreq.h> |
| 22 | #include <linux/module.h> |
Lianwei Wang | d72db42 | 2012-11-01 09:59:52 +0800 | [diff] [blame] | 23 | #include <linux/moduleparam.h> |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 24 | #include <linux/mutex.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/sched/rt.h> |
| 27 | #include <linux/tick.h> |
| 28 | #include <linux/time.h> |
| 29 | #include <linux/timer.h> |
| 30 | #include <linux/workqueue.h> |
| 31 | #include <linux/kthread.h> |
| 32 | #include <linux/mutex.h> |
Todd Poynor | ab8dc40 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 33 | #include <linux/slab.h> |
Todd Poynor | 15a9ea0 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 34 | #include <asm/cputime.h> |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 35 | |
Todd Poynor | ae01047 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 36 | #define CREATE_TRACE_POINTS |
| 37 | #include <trace/events/cpufreq_interactive.h> |
| 38 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 39 | static atomic_t active_count = ATOMIC_INIT(0); |
| 40 | |
| 41 | struct cpufreq_interactive_cpuinfo { |
| 42 | struct timer_list cpu_timer; |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 43 | struct timer_list cpu_slack_timer; |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 44 | spinlock_t load_lock; /* protects the next 4 fields */ |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 45 | u64 time_in_idle; |
Todd Poynor | 8eccd41 | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 46 | u64 time_in_idle_timestamp; |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 47 | u64 cputime_speedadj; |
| 48 | u64 cputime_speedadj_timestamp; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 49 | struct cpufreq_policy *policy; |
| 50 | struct cpufreq_frequency_table *freq_table; |
| 51 | unsigned int target_freq; |
Todd Poynor | 6d15fa3 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 52 | unsigned int floor_freq; |
| 53 | u64 floor_validate_time; |
Todd Poynor | 1a0389a | 2012-05-10 23:28:06 -0700 | [diff] [blame] | 54 | u64 hispeed_validate_time; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 55 | int governor_enabled; |
| 56 | }; |
| 57 | |
| 58 | static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo); |
| 59 | |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 60 | /* realtime thread handles frequency scaling */ |
| 61 | static struct task_struct *speedchange_task; |
| 62 | static cpumask_t speedchange_cpumask; |
| 63 | static spinlock_t speedchange_cpumask_lock; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 64 | |
| 65 | /* Hi speed to bump to from lo speed when load burst (default max) */ |
Todd Poynor | 3b7b5f8 | 2012-10-03 00:39:56 -0700 | [diff] [blame] | 66 | static unsigned int hispeed_freq; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 67 | |
| 68 | /* Go to hi speed when CPU load at or above this value. */ |
Todd Poynor | a380aa8 | 2012-04-17 17:39:34 -0700 | [diff] [blame] | 69 | #define DEFAULT_GO_HISPEED_LOAD 85 |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 70 | static unsigned long go_hispeed_load; |
| 71 | |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 72 | /* Target load. Lower values result in higher CPU speeds. */ |
| 73 | #define DEFAULT_TARGET_LOAD 90 |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 74 | static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD}; |
| 75 | static spinlock_t target_loads_lock; |
| 76 | static unsigned int *target_loads = default_target_loads; |
| 77 | static int ntarget_loads = ARRAY_SIZE(default_target_loads); |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 78 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 79 | /* |
| 80 | * The minimum amount of time to spend at a frequency before we can ramp down. |
| 81 | */ |
Todd Poynor | a380aa8 | 2012-04-17 17:39:34 -0700 | [diff] [blame] | 82 | #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC) |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 83 | static unsigned long min_sample_time; |
| 84 | |
| 85 | /* |
| 86 | * The sample rate of the timer used to increase frequency |
| 87 | */ |
Todd Poynor | a380aa8 | 2012-04-17 17:39:34 -0700 | [diff] [blame] | 88 | #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC) |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 89 | static unsigned long timer_rate; |
| 90 | |
Todd Poynor | cbbe17d | 2012-04-13 20:18:02 -0700 | [diff] [blame] | 91 | /* |
| 92 | * Wait this long before raising speed above hispeed, by default a single |
| 93 | * timer interval. |
| 94 | */ |
| 95 | #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE |
| 96 | static unsigned long above_hispeed_delay_val; |
| 97 | |
Todd Poynor | e16d592 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 98 | /* Non-zero means indefinite speed boost active */ |
Todd Poynor | 15a9ea0 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 99 | static int boost_val; |
Todd Poynor | e16d592 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 100 | /* Duration of a boot pulse in usecs */ |
| 101 | static int boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME; |
| 102 | /* End time of boost pulse in ktime converted to usecs */ |
| 103 | static u64 boostpulse_endtime; |
Todd Poynor | 15a9ea0 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 104 | |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 105 | /* |
| 106 | * Max additional time to wait in idle, beyond timer_rate, at speeds above |
| 107 | * minimum before wakeup to reduce speed, or -1 if unnecessary. |
| 108 | */ |
| 109 | #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE) |
| 110 | static int timer_slack_val = DEFAULT_TIMER_SLACK; |
Lianwei Wang | d72db42 | 2012-11-01 09:59:52 +0800 | [diff] [blame] | 111 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 112 | static int cpufreq_governor_interactive(struct cpufreq_policy *policy, |
| 113 | unsigned int event); |
| 114 | |
| 115 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE |
| 116 | static |
| 117 | #endif |
| 118 | struct cpufreq_governor cpufreq_gov_interactive = { |
| 119 | .name = "interactive", |
| 120 | .governor = cpufreq_governor_interactive, |
| 121 | .max_transition_latency = 10000000, |
| 122 | .owner = THIS_MODULE, |
| 123 | }; |
| 124 | |
Todd Poynor | 8eccd41 | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 125 | static void cpufreq_interactive_timer_resched( |
| 126 | struct cpufreq_interactive_cpuinfo *pcpu) |
| 127 | { |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 128 | unsigned long expires = jiffies + usecs_to_jiffies(timer_rate); |
| 129 | |
| 130 | mod_timer_pinned(&pcpu->cpu_timer, expires); |
| 131 | if (timer_slack_val >= 0 && pcpu->target_freq > pcpu->policy->min) { |
| 132 | expires += usecs_to_jiffies(timer_slack_val); |
| 133 | mod_timer_pinned(&pcpu->cpu_slack_timer, expires); |
| 134 | } |
| 135 | |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 136 | spin_lock(&pcpu->load_lock); |
Todd Poynor | 8eccd41 | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 137 | pcpu->time_in_idle = |
| 138 | get_cpu_idle_time_us(smp_processor_id(), |
| 139 | &pcpu->time_in_idle_timestamp); |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 140 | pcpu->cputime_speedadj = 0; |
| 141 | pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp; |
| 142 | spin_unlock(&pcpu->load_lock); |
Todd Poynor | 8eccd41 | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 145 | static unsigned int freq_to_targetload(unsigned int freq) |
| 146 | { |
| 147 | int i; |
| 148 | unsigned int ret; |
| 149 | |
| 150 | spin_lock(&target_loads_lock); |
| 151 | |
| 152 | for (i = 0; i < ntarget_loads - 1 && freq >= target_loads[i+1]; i += 2) |
| 153 | ; |
| 154 | |
| 155 | ret = target_loads[i]; |
| 156 | spin_unlock(&target_loads_lock); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * If increasing frequencies never map to a lower target load then |
| 162 | * choose_freq() will find the minimum frequency that does not exceed its |
| 163 | * target load given the current load. |
| 164 | */ |
| 165 | |
| 166 | static unsigned int choose_freq( |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 167 | struct cpufreq_interactive_cpuinfo *pcpu, unsigned int loadadjfreq) |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 168 | { |
| 169 | unsigned int freq = pcpu->policy->cur; |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 170 | unsigned int prevfreq, freqmin, freqmax; |
| 171 | unsigned int tl; |
| 172 | int index; |
| 173 | |
| 174 | freqmin = 0; |
| 175 | freqmax = UINT_MAX; |
| 176 | |
| 177 | do { |
| 178 | prevfreq = freq; |
| 179 | tl = freq_to_targetload(freq); |
| 180 | |
| 181 | /* |
| 182 | * Find the lowest frequency where the computed load is less |
| 183 | * than or equal to the target load. |
| 184 | */ |
| 185 | |
| 186 | cpufreq_frequency_table_target( |
| 187 | pcpu->policy, pcpu->freq_table, loadadjfreq / tl, |
| 188 | CPUFREQ_RELATION_L, &index); |
| 189 | freq = pcpu->freq_table[index].frequency; |
| 190 | |
| 191 | if (freq > prevfreq) { |
| 192 | /* The previous frequency is too low. */ |
| 193 | freqmin = prevfreq; |
| 194 | |
| 195 | if (freq >= freqmax) { |
| 196 | /* |
| 197 | * Find the highest frequency that is less |
| 198 | * than freqmax. |
| 199 | */ |
| 200 | cpufreq_frequency_table_target( |
| 201 | pcpu->policy, pcpu->freq_table, |
| 202 | freqmax - 1, CPUFREQ_RELATION_H, |
| 203 | &index); |
| 204 | freq = pcpu->freq_table[index].frequency; |
| 205 | |
| 206 | if (freq == freqmin) { |
| 207 | /* |
| 208 | * The first frequency below freqmax |
| 209 | * has already been found to be too |
| 210 | * low. freqmax is the lowest speed |
| 211 | * we found that is fast enough. |
| 212 | */ |
| 213 | freq = freqmax; |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | } else if (freq < prevfreq) { |
| 218 | /* The previous frequency is high enough. */ |
| 219 | freqmax = prevfreq; |
| 220 | |
| 221 | if (freq <= freqmin) { |
| 222 | /* |
| 223 | * Find the lowest frequency that is higher |
| 224 | * than freqmin. |
| 225 | */ |
| 226 | cpufreq_frequency_table_target( |
| 227 | pcpu->policy, pcpu->freq_table, |
| 228 | freqmin + 1, CPUFREQ_RELATION_L, |
| 229 | &index); |
| 230 | freq = pcpu->freq_table[index].frequency; |
| 231 | |
| 232 | /* |
| 233 | * If freqmax is the first frequency above |
| 234 | * freqmin then we have already found that |
| 235 | * this speed is fast enough. |
| 236 | */ |
| 237 | if (freq == freqmax) |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* If same frequency chosen as previous then done. */ |
| 243 | } while (freq != prevfreq); |
| 244 | |
| 245 | return freq; |
| 246 | } |
| 247 | |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 248 | static u64 update_load(int cpu) |
| 249 | { |
| 250 | struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu); |
| 251 | u64 now; |
| 252 | u64 now_idle; |
| 253 | unsigned int delta_idle; |
| 254 | unsigned int delta_time; |
| 255 | u64 active_time; |
| 256 | |
| 257 | now_idle = get_cpu_idle_time_us(cpu, &now); |
| 258 | delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle); |
| 259 | delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp); |
| 260 | active_time = delta_time - delta_idle; |
| 261 | pcpu->cputime_speedadj += active_time * pcpu->policy->cur; |
| 262 | |
| 263 | pcpu->time_in_idle = now_idle; |
| 264 | pcpu->time_in_idle_timestamp = now; |
| 265 | return now; |
| 266 | } |
| 267 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 268 | static void cpufreq_interactive_timer(unsigned long data) |
| 269 | { |
Todd Poynor | e7afb7e | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 270 | u64 now; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 271 | unsigned int delta_time; |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 272 | u64 cputime_speedadj; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 273 | int cpu_load; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 274 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 275 | &per_cpu(cpuinfo, data); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 276 | unsigned int new_freq; |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 277 | unsigned int loadadjfreq; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 278 | unsigned int index; |
| 279 | unsigned long flags; |
Todd Poynor | e16d592 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 280 | bool boosted; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 281 | |
| 282 | smp_rmb(); |
| 283 | |
| 284 | if (!pcpu->governor_enabled) |
| 285 | goto exit; |
| 286 | |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 287 | spin_lock(&pcpu->load_lock); |
| 288 | now = update_load(data); |
| 289 | delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp); |
| 290 | cputime_speedadj = pcpu->cputime_speedadj; |
| 291 | spin_unlock(&pcpu->load_lock); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 292 | |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 293 | if (WARN_ON_ONCE(!delta_time)) |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 294 | goto rearm; |
| 295 | |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 296 | do_div(cputime_speedadj, delta_time); |
| 297 | loadadjfreq = (unsigned int)cputime_speedadj * 100; |
| 298 | cpu_load = loadadjfreq / pcpu->target_freq; |
Todd Poynor | e16d592 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 299 | boosted = boost_val || now < boostpulse_endtime; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 300 | |
Todd Poynor | 2b66049 | 2012-12-19 16:06:48 -0800 | [diff] [blame^] | 301 | if (cpu_load >= go_hispeed_load || boosted) { |
| 302 | if (pcpu->target_freq < hispeed_freq) { |
| 303 | new_freq = hispeed_freq; |
| 304 | } else { |
| 305 | new_freq = choose_freq(pcpu, loadadjfreq); |
| 306 | |
| 307 | if (new_freq < hispeed_freq) |
| 308 | new_freq = hispeed_freq; |
| 309 | } |
| 310 | } else { |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 311 | new_freq = choose_freq(pcpu, loadadjfreq); |
Todd Poynor | 2b66049 | 2012-12-19 16:06:48 -0800 | [diff] [blame^] | 312 | } |
Todd Poynor | 131ff02 | 2012-11-08 15:06:55 -0800 | [diff] [blame] | 313 | |
| 314 | if (pcpu->target_freq >= hispeed_freq && |
| 315 | new_freq > pcpu->target_freq && |
| 316 | now - pcpu->hispeed_validate_time < above_hispeed_delay_val) { |
| 317 | trace_cpufreq_interactive_notyet( |
| 318 | data, cpu_load, pcpu->target_freq, |
| 319 | pcpu->policy->cur, new_freq); |
| 320 | goto rearm; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Todd Poynor | 131ff02 | 2012-11-08 15:06:55 -0800 | [diff] [blame] | 323 | pcpu->hispeed_validate_time = now; |
Todd Poynor | 1a0389a | 2012-05-10 23:28:06 -0700 | [diff] [blame] | 324 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 325 | if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table, |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 326 | new_freq, CPUFREQ_RELATION_L, |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 327 | &index)) { |
| 328 | pr_warn_once("timer %d: cpufreq_frequency_table_target error\n", |
| 329 | (int) data); |
| 330 | goto rearm; |
| 331 | } |
| 332 | |
| 333 | new_freq = pcpu->freq_table[index].frequency; |
| 334 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 335 | /* |
Todd Poynor | 6d15fa3 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 336 | * Do not scale below floor_freq unless we have been at or above the |
| 337 | * floor frequency for the minimum sample time since last validated. |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 338 | */ |
Todd Poynor | 6d15fa3 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 339 | if (new_freq < pcpu->floor_freq) { |
Todd Poynor | e7afb7e | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 340 | if (now - pcpu->floor_validate_time < min_sample_time) { |
Todd Poynor | e60cc1b | 2012-11-28 17:56:09 -0800 | [diff] [blame] | 341 | trace_cpufreq_interactive_notyet( |
| 342 | data, cpu_load, pcpu->target_freq, |
| 343 | pcpu->policy->cur, new_freq); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 344 | goto rearm; |
Todd Poynor | ae01047 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 345 | } |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Todd Poynor | e16d592 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 348 | /* |
| 349 | * Update the timestamp for checking whether speed has been held at |
| 350 | * or above the selected frequency for a minimum of min_sample_time, |
| 351 | * if not boosted to hispeed_freq. If boosted to hispeed_freq then we |
| 352 | * allow the speed to drop as soon as the boostpulse duration expires |
| 353 | * (or the indefinite boost is turned off). |
| 354 | */ |
| 355 | |
| 356 | if (!boosted || new_freq > hispeed_freq) { |
| 357 | pcpu->floor_freq = new_freq; |
| 358 | pcpu->floor_validate_time = now; |
| 359 | } |
Todd Poynor | 1f408dc | 2012-04-06 19:59:36 -0700 | [diff] [blame] | 360 | |
| 361 | if (pcpu->target_freq == new_freq) { |
Todd Poynor | e60cc1b | 2012-11-28 17:56:09 -0800 | [diff] [blame] | 362 | trace_cpufreq_interactive_already( |
| 363 | data, cpu_load, pcpu->target_freq, |
| 364 | pcpu->policy->cur, new_freq); |
Todd Poynor | 1f408dc | 2012-04-06 19:59:36 -0700 | [diff] [blame] | 365 | goto rearm_if_notmax; |
| 366 | } |
| 367 | |
Todd Poynor | ae01047 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 368 | trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq, |
Todd Poynor | e60cc1b | 2012-11-28 17:56:09 -0800 | [diff] [blame] | 369 | pcpu->policy->cur, new_freq); |
Todd Poynor | ae01047 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 370 | |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 371 | pcpu->target_freq = new_freq; |
| 372 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
| 373 | cpumask_set_cpu(data, &speedchange_cpumask); |
| 374 | spin_unlock_irqrestore(&speedchange_cpumask_lock, flags); |
| 375 | wake_up_process(speedchange_task); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 376 | |
| 377 | rearm_if_notmax: |
| 378 | /* |
| 379 | * Already set max speed and don't see a need to change that, |
| 380 | * wait until next idle to re-evaluate, don't need timer. |
| 381 | */ |
| 382 | if (pcpu->target_freq == pcpu->policy->max) |
| 383 | goto exit; |
| 384 | |
| 385 | rearm: |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 386 | if (!timer_pending(&pcpu->cpu_timer)) |
Todd Poynor | 8eccd41 | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 387 | cpufreq_interactive_timer_resched(pcpu); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 388 | |
| 389 | exit: |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | static void cpufreq_interactive_idle_start(void) |
| 394 | { |
| 395 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 396 | &per_cpu(cpuinfo, smp_processor_id()); |
| 397 | int pending; |
| 398 | |
| 399 | if (!pcpu->governor_enabled) |
| 400 | return; |
| 401 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 402 | pending = timer_pending(&pcpu->cpu_timer); |
| 403 | |
| 404 | if (pcpu->target_freq != pcpu->policy->min) { |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 405 | /* |
| 406 | * Entering idle while not at lowest speed. On some |
| 407 | * platforms this can hold the other CPU(s) at that speed |
| 408 | * even though the CPU is idle. Set a timer to re-evaluate |
| 409 | * speed so this idle CPU doesn't hold the other CPUs above |
| 410 | * min indefinitely. This should probably be a quirk of |
| 411 | * the CPUFreq driver. |
| 412 | */ |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 413 | if (!pending) |
Todd Poynor | 8eccd41 | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 414 | cpufreq_interactive_timer_resched(pcpu); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | } |
| 418 | |
| 419 | static void cpufreq_interactive_idle_end(void) |
| 420 | { |
| 421 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 422 | &per_cpu(cpuinfo, smp_processor_id()); |
| 423 | |
Sam Leffler | 3ab7c2b | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 424 | if (!pcpu->governor_enabled) |
| 425 | return; |
| 426 | |
Todd Poynor | e7afb7e | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 427 | /* Arm the timer for 1-2 ticks later if not already. */ |
| 428 | if (!timer_pending(&pcpu->cpu_timer)) { |
Todd Poynor | 8eccd41 | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 429 | cpufreq_interactive_timer_resched(pcpu); |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 430 | } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) { |
Todd Poynor | 8eccd41 | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 431 | del_timer(&pcpu->cpu_timer); |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 432 | del_timer(&pcpu->cpu_slack_timer); |
Todd Poynor | 8eccd41 | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 433 | cpufreq_interactive_timer(smp_processor_id()); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 434 | } |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 437 | static int cpufreq_interactive_speedchange_task(void *data) |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 438 | { |
| 439 | unsigned int cpu; |
| 440 | cpumask_t tmp_mask; |
| 441 | unsigned long flags; |
| 442 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 443 | |
| 444 | while (1) { |
| 445 | set_current_state(TASK_INTERRUPTIBLE); |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 446 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 447 | |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 448 | if (cpumask_empty(&speedchange_cpumask)) { |
| 449 | spin_unlock_irqrestore(&speedchange_cpumask_lock, |
| 450 | flags); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 451 | schedule(); |
| 452 | |
| 453 | if (kthread_should_stop()) |
| 454 | break; |
| 455 | |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 456 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | set_current_state(TASK_RUNNING); |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 460 | tmp_mask = speedchange_cpumask; |
| 461 | cpumask_clear(&speedchange_cpumask); |
| 462 | spin_unlock_irqrestore(&speedchange_cpumask_lock, flags); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 463 | |
| 464 | for_each_cpu(cpu, &tmp_mask) { |
| 465 | unsigned int j; |
| 466 | unsigned int max_freq = 0; |
| 467 | |
| 468 | pcpu = &per_cpu(cpuinfo, cpu); |
| 469 | smp_rmb(); |
| 470 | |
| 471 | if (!pcpu->governor_enabled) |
| 472 | continue; |
| 473 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 474 | for_each_cpu(j, pcpu->policy->cpus) { |
| 475 | struct cpufreq_interactive_cpuinfo *pjcpu = |
| 476 | &per_cpu(cpuinfo, j); |
| 477 | |
| 478 | if (pjcpu->target_freq > max_freq) |
| 479 | max_freq = pjcpu->target_freq; |
| 480 | } |
| 481 | |
| 482 | if (max_freq != pcpu->policy->cur) |
| 483 | __cpufreq_driver_target(pcpu->policy, |
| 484 | max_freq, |
| 485 | CPUFREQ_RELATION_H); |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 486 | trace_cpufreq_interactive_setspeed(cpu, |
| 487 | pcpu->target_freq, |
Todd Poynor | ae01047 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 488 | pcpu->policy->cur); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
Todd Poynor | ab8dc40 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 495 | static void cpufreq_interactive_boost(void) |
| 496 | { |
| 497 | int i; |
| 498 | int anyboost = 0; |
| 499 | unsigned long flags; |
| 500 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 501 | |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 502 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
Todd Poynor | ab8dc40 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 503 | |
| 504 | for_each_online_cpu(i) { |
| 505 | pcpu = &per_cpu(cpuinfo, i); |
| 506 | |
| 507 | if (pcpu->target_freq < hispeed_freq) { |
| 508 | pcpu->target_freq = hispeed_freq; |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 509 | cpumask_set_cpu(i, &speedchange_cpumask); |
Todd Poynor | 31817c9 | 2012-12-07 20:08:45 -0800 | [diff] [blame] | 510 | pcpu->hispeed_validate_time = |
| 511 | ktime_to_us(ktime_get()); |
Todd Poynor | ab8dc40 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 512 | anyboost = 1; |
| 513 | } |
| 514 | |
| 515 | /* |
Todd Poynor | 6d15fa3 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 516 | * Set floor freq and (re)start timer for when last |
| 517 | * validated. |
Todd Poynor | ab8dc40 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 518 | */ |
| 519 | |
Todd Poynor | 6d15fa3 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 520 | pcpu->floor_freq = hispeed_freq; |
| 521 | pcpu->floor_validate_time = ktime_to_us(ktime_get()); |
Todd Poynor | ab8dc40 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 524 | spin_unlock_irqrestore(&speedchange_cpumask_lock, flags); |
Todd Poynor | ab8dc40 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 525 | |
| 526 | if (anyboost) |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 527 | wake_up_process(speedchange_task); |
Todd Poynor | ab8dc40 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 530 | static int cpufreq_interactive_notifier( |
| 531 | struct notifier_block *nb, unsigned long val, void *data) |
| 532 | { |
| 533 | struct cpufreq_freqs *freq = data; |
| 534 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 535 | int cpu; |
| 536 | |
| 537 | if (val == CPUFREQ_POSTCHANGE) { |
| 538 | pcpu = &per_cpu(cpuinfo, freq->cpu); |
| 539 | |
| 540 | for_each_cpu(cpu, pcpu->policy->cpus) { |
| 541 | struct cpufreq_interactive_cpuinfo *pjcpu = |
| 542 | &per_cpu(cpuinfo, cpu); |
| 543 | spin_lock(&pjcpu->load_lock); |
| 544 | update_load(cpu); |
| 545 | spin_unlock(&pjcpu->load_lock); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static struct notifier_block cpufreq_notifier_block = { |
| 553 | .notifier_call = cpufreq_interactive_notifier, |
| 554 | }; |
| 555 | |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 556 | static ssize_t show_target_loads( |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 557 | struct kobject *kobj, struct attribute *attr, char *buf) |
| 558 | { |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 559 | int i; |
| 560 | ssize_t ret = 0; |
| 561 | |
| 562 | spin_lock(&target_loads_lock); |
| 563 | |
| 564 | for (i = 0; i < ntarget_loads; i++) |
| 565 | ret += sprintf(buf + ret, "%u%s", target_loads[i], |
| 566 | i & 0x1 ? ":" : " "); |
| 567 | |
| 568 | ret += sprintf(buf + ret, "\n"); |
| 569 | spin_unlock(&target_loads_lock); |
| 570 | return ret; |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 573 | static ssize_t store_target_loads( |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 574 | struct kobject *kobj, struct attribute *attr, const char *buf, |
| 575 | size_t count) |
| 576 | { |
| 577 | int ret; |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 578 | const char *cp; |
| 579 | unsigned int *new_target_loads = NULL; |
| 580 | int ntokens = 1; |
| 581 | int i; |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 582 | |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 583 | cp = buf; |
| 584 | while ((cp = strpbrk(cp + 1, " :"))) |
| 585 | ntokens++; |
| 586 | |
| 587 | if (!(ntokens & 0x1)) |
| 588 | goto err_inval; |
| 589 | |
| 590 | new_target_loads = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL); |
| 591 | if (!new_target_loads) { |
| 592 | ret = -ENOMEM; |
| 593 | goto err; |
| 594 | } |
| 595 | |
| 596 | cp = buf; |
| 597 | i = 0; |
| 598 | while (i < ntokens) { |
| 599 | if (sscanf(cp, "%u", &new_target_loads[i++]) != 1) |
| 600 | goto err_inval; |
| 601 | |
| 602 | cp = strpbrk(cp, " :"); |
| 603 | if (!cp) |
| 604 | break; |
| 605 | cp++; |
| 606 | } |
| 607 | |
| 608 | if (i != ntokens) |
| 609 | goto err_inval; |
| 610 | |
| 611 | spin_lock(&target_loads_lock); |
| 612 | if (target_loads != default_target_loads) |
| 613 | kfree(target_loads); |
| 614 | target_loads = new_target_loads; |
| 615 | ntarget_loads = ntokens; |
| 616 | spin_unlock(&target_loads_lock); |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 617 | return count; |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 618 | |
| 619 | err_inval: |
| 620 | ret = -EINVAL; |
| 621 | err: |
| 622 | kfree(new_target_loads); |
| 623 | return ret; |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 626 | static struct global_attr target_loads_attr = |
| 627 | __ATTR(target_loads, S_IRUGO | S_IWUSR, |
| 628 | show_target_loads, store_target_loads); |
Todd Poynor | 8d2d93f | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 629 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 630 | static ssize_t show_hispeed_freq(struct kobject *kobj, |
| 631 | struct attribute *attr, char *buf) |
| 632 | { |
Todd Poynor | 3b7b5f8 | 2012-10-03 00:39:56 -0700 | [diff] [blame] | 633 | return sprintf(buf, "%u\n", hispeed_freq); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static ssize_t store_hispeed_freq(struct kobject *kobj, |
| 637 | struct attribute *attr, const char *buf, |
| 638 | size_t count) |
| 639 | { |
| 640 | int ret; |
Todd Poynor | 3b7b5f8 | 2012-10-03 00:39:56 -0700 | [diff] [blame] | 641 | long unsigned int val; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 642 | |
Todd Poynor | 3b7b5f8 | 2012-10-03 00:39:56 -0700 | [diff] [blame] | 643 | ret = strict_strtoul(buf, 0, &val); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 644 | if (ret < 0) |
| 645 | return ret; |
| 646 | hispeed_freq = val; |
| 647 | return count; |
| 648 | } |
| 649 | |
| 650 | static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644, |
| 651 | show_hispeed_freq, store_hispeed_freq); |
| 652 | |
| 653 | |
| 654 | static ssize_t show_go_hispeed_load(struct kobject *kobj, |
| 655 | struct attribute *attr, char *buf) |
| 656 | { |
| 657 | return sprintf(buf, "%lu\n", go_hispeed_load); |
| 658 | } |
| 659 | |
| 660 | static ssize_t store_go_hispeed_load(struct kobject *kobj, |
| 661 | struct attribute *attr, const char *buf, size_t count) |
| 662 | { |
| 663 | int ret; |
| 664 | unsigned long val; |
| 665 | |
| 666 | ret = strict_strtoul(buf, 0, &val); |
| 667 | if (ret < 0) |
| 668 | return ret; |
| 669 | go_hispeed_load = val; |
| 670 | return count; |
| 671 | } |
| 672 | |
| 673 | static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644, |
| 674 | show_go_hispeed_load, store_go_hispeed_load); |
| 675 | |
| 676 | static ssize_t show_min_sample_time(struct kobject *kobj, |
| 677 | struct attribute *attr, char *buf) |
| 678 | { |
| 679 | return sprintf(buf, "%lu\n", min_sample_time); |
| 680 | } |
| 681 | |
| 682 | static ssize_t store_min_sample_time(struct kobject *kobj, |
| 683 | struct attribute *attr, const char *buf, size_t count) |
| 684 | { |
| 685 | int ret; |
| 686 | unsigned long val; |
| 687 | |
| 688 | ret = strict_strtoul(buf, 0, &val); |
| 689 | if (ret < 0) |
| 690 | return ret; |
| 691 | min_sample_time = val; |
| 692 | return count; |
| 693 | } |
| 694 | |
| 695 | static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644, |
| 696 | show_min_sample_time, store_min_sample_time); |
| 697 | |
Todd Poynor | cbbe17d | 2012-04-13 20:18:02 -0700 | [diff] [blame] | 698 | static ssize_t show_above_hispeed_delay(struct kobject *kobj, |
| 699 | struct attribute *attr, char *buf) |
| 700 | { |
| 701 | return sprintf(buf, "%lu\n", above_hispeed_delay_val); |
| 702 | } |
| 703 | |
| 704 | static ssize_t store_above_hispeed_delay(struct kobject *kobj, |
| 705 | struct attribute *attr, |
| 706 | const char *buf, size_t count) |
| 707 | { |
| 708 | int ret; |
| 709 | unsigned long val; |
| 710 | |
| 711 | ret = strict_strtoul(buf, 0, &val); |
| 712 | if (ret < 0) |
| 713 | return ret; |
| 714 | above_hispeed_delay_val = val; |
| 715 | return count; |
| 716 | } |
| 717 | |
| 718 | define_one_global_rw(above_hispeed_delay); |
| 719 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 720 | static ssize_t show_timer_rate(struct kobject *kobj, |
| 721 | struct attribute *attr, char *buf) |
| 722 | { |
| 723 | return sprintf(buf, "%lu\n", timer_rate); |
| 724 | } |
| 725 | |
| 726 | static ssize_t store_timer_rate(struct kobject *kobj, |
| 727 | struct attribute *attr, const char *buf, size_t count) |
| 728 | { |
| 729 | int ret; |
| 730 | unsigned long val; |
| 731 | |
| 732 | ret = strict_strtoul(buf, 0, &val); |
| 733 | if (ret < 0) |
| 734 | return ret; |
| 735 | timer_rate = val; |
| 736 | return count; |
| 737 | } |
| 738 | |
| 739 | static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644, |
| 740 | show_timer_rate, store_timer_rate); |
| 741 | |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 742 | static ssize_t show_timer_slack( |
| 743 | struct kobject *kobj, struct attribute *attr, char *buf) |
| 744 | { |
| 745 | return sprintf(buf, "%d\n", timer_slack_val); |
| 746 | } |
| 747 | |
| 748 | static ssize_t store_timer_slack( |
| 749 | struct kobject *kobj, struct attribute *attr, const char *buf, |
| 750 | size_t count) |
| 751 | { |
| 752 | int ret; |
| 753 | unsigned long val; |
| 754 | |
| 755 | ret = kstrtol(buf, 10, &val); |
| 756 | if (ret < 0) |
| 757 | return ret; |
| 758 | |
| 759 | timer_slack_val = val; |
| 760 | return count; |
| 761 | } |
| 762 | |
| 763 | define_one_global_rw(timer_slack); |
| 764 | |
Todd Poynor | 15a9ea0 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 765 | static ssize_t show_boost(struct kobject *kobj, struct attribute *attr, |
| 766 | char *buf) |
| 767 | { |
| 768 | return sprintf(buf, "%d\n", boost_val); |
| 769 | } |
| 770 | |
| 771 | static ssize_t store_boost(struct kobject *kobj, struct attribute *attr, |
| 772 | const char *buf, size_t count) |
| 773 | { |
| 774 | int ret; |
| 775 | unsigned long val; |
| 776 | |
| 777 | ret = kstrtoul(buf, 0, &val); |
| 778 | if (ret < 0) |
| 779 | return ret; |
| 780 | |
| 781 | boost_val = val; |
| 782 | |
Todd Poynor | 442a312 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 783 | if (boost_val) { |
| 784 | trace_cpufreq_interactive_boost("on"); |
Todd Poynor | 15a9ea0 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 785 | cpufreq_interactive_boost(); |
Todd Poynor | 442a312 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 786 | } else { |
| 787 | trace_cpufreq_interactive_unboost("off"); |
| 788 | } |
Todd Poynor | 15a9ea0 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 789 | |
| 790 | return count; |
| 791 | } |
| 792 | |
| 793 | define_one_global_rw(boost); |
| 794 | |
Todd Poynor | 442a312 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 795 | static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr, |
| 796 | const char *buf, size_t count) |
| 797 | { |
| 798 | int ret; |
| 799 | unsigned long val; |
| 800 | |
| 801 | ret = kstrtoul(buf, 0, &val); |
| 802 | if (ret < 0) |
| 803 | return ret; |
| 804 | |
Todd Poynor | e16d592 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 805 | boostpulse_endtime = ktime_to_us(ktime_get()) + boostpulse_duration_val; |
Todd Poynor | 442a312 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 806 | trace_cpufreq_interactive_boost("pulse"); |
| 807 | cpufreq_interactive_boost(); |
| 808 | return count; |
| 809 | } |
| 810 | |
| 811 | static struct global_attr boostpulse = |
| 812 | __ATTR(boostpulse, 0200, NULL, store_boostpulse); |
| 813 | |
Todd Poynor | e16d592 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 814 | static ssize_t show_boostpulse_duration( |
| 815 | struct kobject *kobj, struct attribute *attr, char *buf) |
| 816 | { |
| 817 | return sprintf(buf, "%d\n", boostpulse_duration_val); |
| 818 | } |
| 819 | |
| 820 | static ssize_t store_boostpulse_duration( |
| 821 | struct kobject *kobj, struct attribute *attr, const char *buf, |
| 822 | size_t count) |
| 823 | { |
| 824 | int ret; |
| 825 | unsigned long val; |
| 826 | |
| 827 | ret = kstrtoul(buf, 0, &val); |
| 828 | if (ret < 0) |
| 829 | return ret; |
| 830 | |
| 831 | boostpulse_duration_val = val; |
| 832 | return count; |
| 833 | } |
| 834 | |
| 835 | define_one_global_rw(boostpulse_duration); |
| 836 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 837 | static struct attribute *interactive_attributes[] = { |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 838 | &target_loads_attr.attr, |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 839 | &hispeed_freq_attr.attr, |
| 840 | &go_hispeed_load_attr.attr, |
Todd Poynor | cbbe17d | 2012-04-13 20:18:02 -0700 | [diff] [blame] | 841 | &above_hispeed_delay.attr, |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 842 | &min_sample_time_attr.attr, |
| 843 | &timer_rate_attr.attr, |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 844 | &timer_slack.attr, |
Todd Poynor | 15a9ea0 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 845 | &boost.attr, |
Todd Poynor | 442a312 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 846 | &boostpulse.attr, |
Todd Poynor | e16d592 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 847 | &boostpulse_duration.attr, |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 848 | NULL, |
| 849 | }; |
| 850 | |
| 851 | static struct attribute_group interactive_attr_group = { |
| 852 | .attrs = interactive_attributes, |
| 853 | .name = "interactive", |
| 854 | }; |
| 855 | |
Sam Leffler | 3ab7c2b | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 856 | static int cpufreq_interactive_idle_notifier(struct notifier_block *nb, |
| 857 | unsigned long val, |
| 858 | void *data) |
| 859 | { |
| 860 | switch (val) { |
| 861 | case IDLE_START: |
| 862 | cpufreq_interactive_idle_start(); |
| 863 | break; |
| 864 | case IDLE_END: |
| 865 | cpufreq_interactive_idle_end(); |
| 866 | break; |
| 867 | } |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | static struct notifier_block cpufreq_interactive_idle_nb = { |
| 873 | .notifier_call = cpufreq_interactive_idle_notifier, |
| 874 | }; |
| 875 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 876 | static int cpufreq_governor_interactive(struct cpufreq_policy *policy, |
| 877 | unsigned int event) |
| 878 | { |
| 879 | int rc; |
| 880 | unsigned int j; |
| 881 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 882 | struct cpufreq_frequency_table *freq_table; |
| 883 | |
| 884 | switch (event) { |
| 885 | case CPUFREQ_GOV_START: |
| 886 | if (!cpu_online(policy->cpu)) |
| 887 | return -EINVAL; |
| 888 | |
| 889 | freq_table = |
| 890 | cpufreq_frequency_get_table(policy->cpu); |
Todd Poynor | e7afb7e | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 891 | if (!hispeed_freq) |
| 892 | hispeed_freq = policy->max; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 893 | |
| 894 | for_each_cpu(j, policy->cpus) { |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 895 | unsigned long expires; |
| 896 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 897 | pcpu = &per_cpu(cpuinfo, j); |
| 898 | pcpu->policy = policy; |
| 899 | pcpu->target_freq = policy->cur; |
| 900 | pcpu->freq_table = freq_table; |
Todd Poynor | 6d15fa3 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 901 | pcpu->floor_freq = pcpu->target_freq; |
| 902 | pcpu->floor_validate_time = |
Todd Poynor | 31817c9 | 2012-12-07 20:08:45 -0800 | [diff] [blame] | 903 | ktime_to_us(ktime_get()); |
Todd Poynor | 1a0389a | 2012-05-10 23:28:06 -0700 | [diff] [blame] | 904 | pcpu->hispeed_validate_time = |
Todd Poynor | 31817c9 | 2012-12-07 20:08:45 -0800 | [diff] [blame] | 905 | pcpu->floor_validate_time; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 906 | pcpu->governor_enabled = 1; |
| 907 | smp_wmb(); |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 908 | expires = jiffies + usecs_to_jiffies(timer_rate); |
| 909 | pcpu->cpu_timer.expires = expires; |
Todd Poynor | e7afb7e | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 910 | add_timer_on(&pcpu->cpu_timer, j); |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 911 | |
| 912 | if (timer_slack_val >= 0) { |
| 913 | expires += usecs_to_jiffies(timer_slack_val); |
| 914 | pcpu->cpu_slack_timer.expires = expires; |
| 915 | add_timer_on(&pcpu->cpu_slack_timer, j); |
| 916 | } |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 919 | /* |
| 920 | * Do not register the idle hook and create sysfs |
| 921 | * entries if we have already done so. |
| 922 | */ |
| 923 | if (atomic_inc_return(&active_count) > 1) |
| 924 | return 0; |
| 925 | |
| 926 | rc = sysfs_create_group(cpufreq_global_kobject, |
| 927 | &interactive_attr_group); |
| 928 | if (rc) |
| 929 | return rc; |
| 930 | |
Sam Leffler | 3ab7c2b | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 931 | idle_notifier_register(&cpufreq_interactive_idle_nb); |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 932 | cpufreq_register_notifier( |
| 933 | &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 934 | break; |
| 935 | |
| 936 | case CPUFREQ_GOV_STOP: |
| 937 | for_each_cpu(j, policy->cpus) { |
| 938 | pcpu = &per_cpu(cpuinfo, j); |
| 939 | pcpu->governor_enabled = 0; |
| 940 | smp_wmb(); |
| 941 | del_timer_sync(&pcpu->cpu_timer); |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 942 | del_timer_sync(&pcpu->cpu_slack_timer); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 945 | if (atomic_dec_return(&active_count) > 0) |
| 946 | return 0; |
| 947 | |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 948 | cpufreq_unregister_notifier( |
| 949 | &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); |
Sam Leffler | 3ab7c2b | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 950 | idle_notifier_unregister(&cpufreq_interactive_idle_nb); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 951 | sysfs_remove_group(cpufreq_global_kobject, |
| 952 | &interactive_attr_group); |
| 953 | |
| 954 | break; |
| 955 | |
| 956 | case CPUFREQ_GOV_LIMITS: |
| 957 | if (policy->max < policy->cur) |
| 958 | __cpufreq_driver_target(policy, |
| 959 | policy->max, CPUFREQ_RELATION_H); |
| 960 | else if (policy->min > policy->cur) |
| 961 | __cpufreq_driver_target(policy, |
| 962 | policy->min, CPUFREQ_RELATION_L); |
| 963 | break; |
| 964 | } |
| 965 | return 0; |
| 966 | } |
| 967 | |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 968 | static void cpufreq_interactive_nop_timer(unsigned long data) |
| 969 | { |
| 970 | } |
| 971 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 972 | static int __init cpufreq_interactive_init(void) |
| 973 | { |
| 974 | unsigned int i; |
| 975 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 976 | struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
| 977 | |
| 978 | go_hispeed_load = DEFAULT_GO_HISPEED_LOAD; |
| 979 | min_sample_time = DEFAULT_MIN_SAMPLE_TIME; |
Todd Poynor | cbbe17d | 2012-04-13 20:18:02 -0700 | [diff] [blame] | 980 | above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY; |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 981 | timer_rate = DEFAULT_TIMER_RATE; |
| 982 | |
| 983 | /* Initalize per-cpu timers */ |
| 984 | for_each_possible_cpu(i) { |
| 985 | pcpu = &per_cpu(cpuinfo, i); |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 986 | init_timer_deferrable(&pcpu->cpu_timer); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 987 | pcpu->cpu_timer.function = cpufreq_interactive_timer; |
| 988 | pcpu->cpu_timer.data = i; |
Todd Poynor | 4add259 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 989 | init_timer(&pcpu->cpu_slack_timer); |
| 990 | pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer; |
Todd Poynor | 0e58da2 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 991 | spin_lock_init(&pcpu->load_lock); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Todd Poynor | e9c6074 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 994 | spin_lock_init(&target_loads_lock); |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 995 | spin_lock_init(&speedchange_cpumask_lock); |
| 996 | speedchange_task = |
| 997 | kthread_create(cpufreq_interactive_speedchange_task, NULL, |
| 998 | "cfinteractive"); |
| 999 | if (IS_ERR(speedchange_task)) |
| 1000 | return PTR_ERR(speedchange_task); |
Sam Leffler | 5c9b827 | 2012-06-27 12:55:56 -0700 | [diff] [blame] | 1001 | |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1002 | sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m); |
| 1003 | get_task_struct(speedchange_task); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1004 | |
Sam Leffler | 5c9b827 | 2012-06-27 12:55:56 -0700 | [diff] [blame] | 1005 | /* NB: wake up so the thread does not look hung to the freezer */ |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1006 | wake_up_process(speedchange_task); |
Sam Leffler | 5c9b827 | 2012-06-27 12:55:56 -0700 | [diff] [blame] | 1007 | |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1008 | return cpufreq_register_governor(&cpufreq_gov_interactive); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE |
| 1012 | fs_initcall(cpufreq_interactive_init); |
| 1013 | #else |
| 1014 | module_init(cpufreq_interactive_init); |
| 1015 | #endif |
| 1016 | |
| 1017 | static void __exit cpufreq_interactive_exit(void) |
| 1018 | { |
| 1019 | cpufreq_unregister_governor(&cpufreq_gov_interactive); |
Todd Poynor | 0f1920b | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1020 | kthread_stop(speedchange_task); |
| 1021 | put_task_struct(speedchange_task); |
Mike Chan | ef96969 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | module_exit(cpufreq_interactive_exit); |
| 1025 | |
| 1026 | MODULE_AUTHOR("Mike Chan <mike@android.com>"); |
| 1027 | MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for " |
| 1028 | "Latency sensitive workloads"); |
| 1029 | MODULE_LICENSE("GPL"); |