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