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