Mike Chan | 1dab259 | 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/mutex.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/tick.h> |
Todd Poynor | 5113cb1 | 2011-11-09 16:56:18 -0800 | [diff] [blame] | 25 | #include <linux/time.h> |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 26 | #include <linux/timer.h> |
| 27 | #include <linux/workqueue.h> |
| 28 | #include <linux/kthread.h> |
Todd Poynor | a50b053 | 2011-06-22 14:34:23 -0700 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 30 | |
| 31 | #include <asm/cputime.h> |
| 32 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 33 | static atomic_t active_count = ATOMIC_INIT(0); |
| 34 | |
| 35 | struct cpufreq_interactive_cpuinfo { |
| 36 | struct timer_list cpu_timer; |
| 37 | int timer_idlecancel; |
| 38 | u64 time_in_idle; |
| 39 | u64 idle_exit_time; |
| 40 | u64 timer_run_time; |
| 41 | int idling; |
| 42 | u64 freq_change_time; |
| 43 | u64 freq_change_time_in_idle; |
| 44 | struct cpufreq_policy *policy; |
| 45 | struct cpufreq_frequency_table *freq_table; |
| 46 | unsigned int target_freq; |
| 47 | int governor_enabled; |
| 48 | }; |
| 49 | |
| 50 | static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo); |
| 51 | |
| 52 | /* Workqueues handle frequency scaling */ |
| 53 | static struct task_struct *up_task; |
| 54 | static struct workqueue_struct *down_wq; |
| 55 | static struct work_struct freq_scale_down_work; |
| 56 | static cpumask_t up_cpumask; |
| 57 | static spinlock_t up_cpumask_lock; |
| 58 | static cpumask_t down_cpumask; |
| 59 | static spinlock_t down_cpumask_lock; |
Todd Poynor | a50b053 | 2011-06-22 14:34:23 -0700 | [diff] [blame] | 60 | static struct mutex set_speed_lock; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 61 | |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 62 | /* Hi speed to bump to from lo speed when load burst (default max) */ |
| 63 | static u64 hispeed_freq; |
| 64 | |
| 65 | /* Go to hi speed when CPU load at or above this value. */ |
| 66 | #define DEFAULT_GO_HISPEED_LOAD 95 |
| 67 | static unsigned long go_hispeed_load; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * The minimum amount of time to spend at a frequency before we can ramp down. |
| 71 | */ |
Todd Poynor | 5113cb1 | 2011-11-09 16:56:18 -0800 | [diff] [blame] | 72 | #define DEFAULT_MIN_SAMPLE_TIME 20 * USEC_PER_MSEC |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 73 | static unsigned long min_sample_time; |
| 74 | |
Allen Martin | 5d14f98 | 2011-06-30 23:54:07 -0700 | [diff] [blame] | 75 | /* |
| 76 | * The sample rate of the timer used to increase frequency |
| 77 | */ |
Todd Poynor | 5113cb1 | 2011-11-09 16:56:18 -0800 | [diff] [blame] | 78 | #define DEFAULT_TIMER_RATE 20 * USEC_PER_MSEC |
Allen Martin | 5d14f98 | 2011-06-30 23:54:07 -0700 | [diff] [blame] | 79 | static unsigned long timer_rate; |
| 80 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 81 | static int cpufreq_governor_interactive(struct cpufreq_policy *policy, |
| 82 | unsigned int event); |
| 83 | |
| 84 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE |
| 85 | static |
| 86 | #endif |
| 87 | struct cpufreq_governor cpufreq_gov_interactive = { |
| 88 | .name = "interactive", |
| 89 | .governor = cpufreq_governor_interactive, |
| 90 | .max_transition_latency = 10000000, |
| 91 | .owner = THIS_MODULE, |
| 92 | }; |
| 93 | |
| 94 | static void cpufreq_interactive_timer(unsigned long data) |
| 95 | { |
| 96 | unsigned int delta_idle; |
| 97 | unsigned int delta_time; |
| 98 | int cpu_load; |
| 99 | int load_since_change; |
| 100 | u64 time_in_idle; |
| 101 | u64 idle_exit_time; |
| 102 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 103 | &per_cpu(cpuinfo, data); |
| 104 | u64 now_idle; |
| 105 | unsigned int new_freq; |
| 106 | unsigned int index; |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 107 | unsigned long flags; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 108 | |
Todd Poynor | 9b6f4a3 | 2010-12-23 17:33:07 -0800 | [diff] [blame] | 109 | smp_rmb(); |
| 110 | |
| 111 | if (!pcpu->governor_enabled) |
| 112 | goto exit; |
| 113 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time, |
| 116 | * this lets idle exit know the current idle time sample has |
| 117 | * been processed, and idle exit can generate a new sample and |
| 118 | * re-arm the timer. This prevents a concurrent idle |
| 119 | * exit on that CPU from writing a new set of info at the same time |
| 120 | * the timer function runs (the timer function can't use that info |
| 121 | * until more time passes). |
| 122 | */ |
| 123 | time_in_idle = pcpu->time_in_idle; |
| 124 | idle_exit_time = pcpu->idle_exit_time; |
| 125 | now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time); |
| 126 | smp_wmb(); |
| 127 | |
| 128 | /* If we raced with cancelling a timer, skip. */ |
Allen Martin | 13b2b14 | 2011-06-28 10:40:30 -0700 | [diff] [blame] | 129 | if (!idle_exit_time) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 130 | goto exit; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 131 | |
| 132 | delta_idle = (unsigned int) cputime64_sub(now_idle, time_in_idle); |
| 133 | delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time, |
| 134 | idle_exit_time); |
| 135 | |
| 136 | /* |
| 137 | * If timer ran less than 1ms after short-term sample started, retry. |
| 138 | */ |
Allen Martin | 13b2b14 | 2011-06-28 10:40:30 -0700 | [diff] [blame] | 139 | if (delta_time < 1000) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 140 | goto rearm; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 141 | |
| 142 | if (delta_idle > delta_time) |
| 143 | cpu_load = 0; |
| 144 | else |
| 145 | cpu_load = 100 * (delta_time - delta_idle) / delta_time; |
| 146 | |
| 147 | delta_idle = (unsigned int) cputime64_sub(now_idle, |
Allen Martin | adce689 | 2011-07-01 11:19:14 -0700 | [diff] [blame] | 148 | pcpu->freq_change_time_in_idle); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 149 | delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time, |
| 150 | pcpu->freq_change_time); |
| 151 | |
Axel Haslam | 3c053e1 | 2011-10-05 14:04:57 -0700 | [diff] [blame] | 152 | if ((delta_time == 0) || (delta_idle > delta_time)) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 153 | load_since_change = 0; |
| 154 | else |
| 155 | load_since_change = |
| 156 | 100 * (delta_time - delta_idle) / delta_time; |
| 157 | |
| 158 | /* |
| 159 | * Choose greater of short-term load (since last idle timer |
| 160 | * started or timer function re-armed itself) or long-term load |
| 161 | * (since last frequency change). |
| 162 | */ |
| 163 | if (load_since_change > cpu_load) |
| 164 | cpu_load = load_since_change; |
| 165 | |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 166 | if (cpu_load >= go_hispeed_load) { |
Todd Poynor | eb5a342 | 2011-11-03 21:22:54 -0700 | [diff] [blame] | 167 | if (pcpu->policy->cur == pcpu->policy->min) |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 168 | new_freq = hispeed_freq; |
Todd Poynor | eb5a342 | 2011-11-03 21:22:54 -0700 | [diff] [blame] | 169 | else |
| 170 | new_freq = pcpu->policy->max * cpu_load / 100; |
| 171 | } else { |
Todd Poynor | a610c81 | 2011-10-27 18:25:59 -0700 | [diff] [blame] | 172 | new_freq = pcpu->policy->cur * cpu_load / 100; |
Todd Poynor | eb5a342 | 2011-11-03 21:22:54 -0700 | [diff] [blame] | 173 | } |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 174 | |
| 175 | if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table, |
| 176 | new_freq, CPUFREQ_RELATION_H, |
| 177 | &index)) { |
Allen Martin | 13b2b14 | 2011-06-28 10:40:30 -0700 | [diff] [blame] | 178 | pr_warn_once("timer %d: cpufreq_frequency_table_target error\n", |
| 179 | (int) data); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 180 | goto rearm; |
| 181 | } |
| 182 | |
| 183 | new_freq = pcpu->freq_table[index].frequency; |
| 184 | |
| 185 | if (pcpu->target_freq == new_freq) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 186 | goto rearm_if_notmax; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * Do not scale down unless we have been at this frequency for the |
| 190 | * minimum sample time. |
| 191 | */ |
| 192 | if (new_freq < pcpu->target_freq) { |
Allen Martin | adce689 | 2011-07-01 11:19:14 -0700 | [diff] [blame] | 193 | if (cputime64_sub(pcpu->timer_run_time, pcpu->freq_change_time) |
| 194 | < min_sample_time) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 195 | goto rearm; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 198 | if (new_freq < pcpu->target_freq) { |
| 199 | pcpu->target_freq = new_freq; |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 200 | spin_lock_irqsave(&down_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 201 | cpumask_set_cpu(data, &down_cpumask); |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 202 | spin_unlock_irqrestore(&down_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 203 | queue_work(down_wq, &freq_scale_down_work); |
| 204 | } else { |
| 205 | pcpu->target_freq = new_freq; |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 206 | spin_lock_irqsave(&up_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 207 | cpumask_set_cpu(data, &up_cpumask); |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 208 | spin_unlock_irqrestore(&up_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 209 | wake_up_process(up_task); |
| 210 | } |
| 211 | |
| 212 | rearm_if_notmax: |
| 213 | /* |
| 214 | * Already set max speed and don't see a need to change that, |
| 215 | * wait until next idle to re-evaluate, don't need timer. |
| 216 | */ |
| 217 | if (pcpu->target_freq == pcpu->policy->max) |
| 218 | goto exit; |
| 219 | |
| 220 | rearm: |
| 221 | if (!timer_pending(&pcpu->cpu_timer)) { |
| 222 | /* |
| 223 | * If already at min: if that CPU is idle, don't set timer. |
| 224 | * Else cancel the timer if that CPU goes idle. We don't |
| 225 | * need to re-evaluate speed until the next idle exit. |
| 226 | */ |
| 227 | if (pcpu->target_freq == pcpu->policy->min) { |
| 228 | smp_rmb(); |
| 229 | |
Allen Martin | 13b2b14 | 2011-06-28 10:40:30 -0700 | [diff] [blame] | 230 | if (pcpu->idling) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 231 | goto exit; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 232 | |
| 233 | pcpu->timer_idlecancel = 1; |
| 234 | } |
| 235 | |
| 236 | pcpu->time_in_idle = get_cpu_idle_time_us( |
| 237 | data, &pcpu->idle_exit_time); |
Allen Martin | 9549cff | 2011-07-19 14:56:21 -0700 | [diff] [blame] | 238 | mod_timer(&pcpu->cpu_timer, |
| 239 | jiffies + usecs_to_jiffies(timer_rate)); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | exit: |
| 243 | return; |
| 244 | } |
| 245 | |
Allen Martin | 80e65d9 | 2011-06-28 09:58:39 -0700 | [diff] [blame] | 246 | static void cpufreq_interactive_idle_start(void) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 247 | { |
| 248 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 249 | &per_cpu(cpuinfo, smp_processor_id()); |
| 250 | int pending; |
| 251 | |
Allen Martin | adce689 | 2011-07-01 11:19:14 -0700 | [diff] [blame] | 252 | if (!pcpu->governor_enabled) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 253 | return; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 254 | |
| 255 | pcpu->idling = 1; |
| 256 | smp_wmb(); |
| 257 | pending = timer_pending(&pcpu->cpu_timer); |
| 258 | |
| 259 | if (pcpu->target_freq != pcpu->policy->min) { |
| 260 | #ifdef CONFIG_SMP |
| 261 | /* |
| 262 | * Entering idle while not at lowest speed. On some |
| 263 | * platforms this can hold the other CPU(s) at that speed |
| 264 | * even though the CPU is idle. Set a timer to re-evaluate |
| 265 | * speed so this idle CPU doesn't hold the other CPUs above |
| 266 | * min indefinitely. This should probably be a quirk of |
| 267 | * the CPUFreq driver. |
| 268 | */ |
| 269 | if (!pending) { |
| 270 | pcpu->time_in_idle = get_cpu_idle_time_us( |
| 271 | smp_processor_id(), &pcpu->idle_exit_time); |
| 272 | pcpu->timer_idlecancel = 0; |
Allen Martin | 9549cff | 2011-07-19 14:56:21 -0700 | [diff] [blame] | 273 | mod_timer(&pcpu->cpu_timer, |
| 274 | jiffies + usecs_to_jiffies(timer_rate)); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 275 | } |
| 276 | #endif |
| 277 | } else { |
| 278 | /* |
| 279 | * If at min speed and entering idle after load has |
| 280 | * already been evaluated, and a timer has been set just in |
| 281 | * case the CPU suddenly goes busy, cancel that timer. The |
| 282 | * CPU didn't go busy; we'll recheck things upon idle exit. |
| 283 | */ |
| 284 | if (pending && pcpu->timer_idlecancel) { |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 285 | del_timer(&pcpu->cpu_timer); |
| 286 | /* |
| 287 | * Ensure last timer run time is after current idle |
| 288 | * sample start time, so next idle exit will always |
| 289 | * start a new idle sampling period. |
| 290 | */ |
| 291 | pcpu->idle_exit_time = 0; |
| 292 | pcpu->timer_idlecancel = 0; |
| 293 | } |
| 294 | } |
| 295 | |
Allen Martin | 80e65d9 | 2011-06-28 09:58:39 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void cpufreq_interactive_idle_end(void) |
| 299 | { |
| 300 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 301 | &per_cpu(cpuinfo, smp_processor_id()); |
| 302 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 303 | pcpu->idling = 0; |
| 304 | smp_wmb(); |
| 305 | |
| 306 | /* |
| 307 | * Arm the timer for 1-2 ticks later if not already, and if the timer |
| 308 | * function has already processed the previous load sampling |
| 309 | * interval. (If the timer is not pending but has not processed |
| 310 | * the previous interval, it is probably racing with us on another |
| 311 | * CPU. Let it compute load based on the previous sample and then |
| 312 | * re-arm the timer for another interval when it's done, rather |
| 313 | * than updating the interval start time to be "now", which doesn't |
| 314 | * give the timer function enough time to make a decision on this |
| 315 | * run.) |
| 316 | */ |
| 317 | if (timer_pending(&pcpu->cpu_timer) == 0 && |
Todd Poynor | 9b6f4a3 | 2010-12-23 17:33:07 -0800 | [diff] [blame] | 318 | pcpu->timer_run_time >= pcpu->idle_exit_time && |
| 319 | pcpu->governor_enabled) { |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 320 | pcpu->time_in_idle = |
| 321 | get_cpu_idle_time_us(smp_processor_id(), |
| 322 | &pcpu->idle_exit_time); |
| 323 | pcpu->timer_idlecancel = 0; |
Allen Martin | 9549cff | 2011-07-19 14:56:21 -0700 | [diff] [blame] | 324 | mod_timer(&pcpu->cpu_timer, |
| 325 | jiffies + usecs_to_jiffies(timer_rate)); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | } |
| 329 | |
| 330 | static int cpufreq_interactive_up_task(void *data) |
| 331 | { |
| 332 | unsigned int cpu; |
| 333 | cpumask_t tmp_mask; |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 334 | unsigned long flags; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 335 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 336 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 337 | while (1) { |
| 338 | set_current_state(TASK_INTERRUPTIBLE); |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 339 | spin_lock_irqsave(&up_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 340 | |
| 341 | if (cpumask_empty(&up_cpumask)) { |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 342 | spin_unlock_irqrestore(&up_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 343 | schedule(); |
| 344 | |
| 345 | if (kthread_should_stop()) |
| 346 | break; |
| 347 | |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 348 | spin_lock_irqsave(&up_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | set_current_state(TASK_RUNNING); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 352 | tmp_mask = up_cpumask; |
| 353 | cpumask_clear(&up_cpumask); |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 354 | spin_unlock_irqrestore(&up_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 355 | |
| 356 | for_each_cpu(cpu, &tmp_mask) { |
Todd Poynor | a50b053 | 2011-06-22 14:34:23 -0700 | [diff] [blame] | 357 | unsigned int j; |
| 358 | unsigned int max_freq = 0; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 359 | |
Todd Poynor | a50b053 | 2011-06-22 14:34:23 -0700 | [diff] [blame] | 360 | pcpu = &per_cpu(cpuinfo, cpu); |
Todd Poynor | 9b6f4a3 | 2010-12-23 17:33:07 -0800 | [diff] [blame] | 361 | smp_rmb(); |
| 362 | |
| 363 | if (!pcpu->governor_enabled) |
| 364 | continue; |
| 365 | |
Todd Poynor | a50b053 | 2011-06-22 14:34:23 -0700 | [diff] [blame] | 366 | mutex_lock(&set_speed_lock); |
| 367 | |
| 368 | for_each_cpu(j, pcpu->policy->cpus) { |
| 369 | struct cpufreq_interactive_cpuinfo *pjcpu = |
| 370 | &per_cpu(cpuinfo, j); |
| 371 | |
| 372 | if (pjcpu->target_freq > max_freq) |
| 373 | max_freq = pjcpu->target_freq; |
| 374 | } |
| 375 | |
| 376 | if (max_freq != pcpu->policy->cur) |
| 377 | __cpufreq_driver_target(pcpu->policy, |
| 378 | max_freq, |
| 379 | CPUFREQ_RELATION_H); |
| 380 | mutex_unlock(&set_speed_lock); |
| 381 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 382 | pcpu->freq_change_time_in_idle = |
| 383 | get_cpu_idle_time_us(cpu, |
| 384 | &pcpu->freq_change_time); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static void cpufreq_interactive_freq_down(struct work_struct *work) |
| 392 | { |
| 393 | unsigned int cpu; |
| 394 | cpumask_t tmp_mask; |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 395 | unsigned long flags; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 396 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 397 | |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 398 | spin_lock_irqsave(&down_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 399 | tmp_mask = down_cpumask; |
| 400 | cpumask_clear(&down_cpumask); |
Todd Poynor | 1c4f95f | 2010-12-03 11:20:09 -0800 | [diff] [blame] | 401 | spin_unlock_irqrestore(&down_cpumask_lock, flags); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 402 | |
| 403 | for_each_cpu(cpu, &tmp_mask) { |
Todd Poynor | a50b053 | 2011-06-22 14:34:23 -0700 | [diff] [blame] | 404 | unsigned int j; |
| 405 | unsigned int max_freq = 0; |
Todd Poynor | 9b6f4a3 | 2010-12-23 17:33:07 -0800 | [diff] [blame] | 406 | |
Todd Poynor | a50b053 | 2011-06-22 14:34:23 -0700 | [diff] [blame] | 407 | pcpu = &per_cpu(cpuinfo, cpu); |
Todd Poynor | 9b6f4a3 | 2010-12-23 17:33:07 -0800 | [diff] [blame] | 408 | smp_rmb(); |
| 409 | |
| 410 | if (!pcpu->governor_enabled) |
| 411 | continue; |
| 412 | |
Todd Poynor | a50b053 | 2011-06-22 14:34:23 -0700 | [diff] [blame] | 413 | mutex_lock(&set_speed_lock); |
| 414 | |
| 415 | for_each_cpu(j, pcpu->policy->cpus) { |
| 416 | struct cpufreq_interactive_cpuinfo *pjcpu = |
| 417 | &per_cpu(cpuinfo, j); |
| 418 | |
| 419 | if (pjcpu->target_freq > max_freq) |
| 420 | max_freq = pjcpu->target_freq; |
| 421 | } |
| 422 | |
| 423 | if (max_freq != pcpu->policy->cur) |
| 424 | __cpufreq_driver_target(pcpu->policy, max_freq, |
| 425 | CPUFREQ_RELATION_H); |
| 426 | |
| 427 | mutex_unlock(&set_speed_lock); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 428 | pcpu->freq_change_time_in_idle = |
| 429 | get_cpu_idle_time_us(cpu, |
| 430 | &pcpu->freq_change_time); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 434 | static ssize_t show_hispeed_freq(struct kobject *kobj, |
| 435 | struct attribute *attr, char *buf) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 436 | { |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 437 | return sprintf(buf, "%llu\n", hispeed_freq); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 440 | static ssize_t store_hispeed_freq(struct kobject *kobj, |
| 441 | struct attribute *attr, const char *buf, |
| 442 | size_t count) |
| 443 | { |
| 444 | int ret; |
| 445 | u64 val; |
| 446 | |
| 447 | ret = strict_strtoull(buf, 0, &val); |
| 448 | if (ret < 0) |
| 449 | return ret; |
| 450 | hispeed_freq = val; |
| 451 | return count; |
| 452 | } |
| 453 | |
| 454 | static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644, |
| 455 | show_hispeed_freq, store_hispeed_freq); |
| 456 | |
| 457 | |
| 458 | static ssize_t show_go_hispeed_load(struct kobject *kobj, |
| 459 | struct attribute *attr, char *buf) |
| 460 | { |
| 461 | return sprintf(buf, "%lu\n", go_hispeed_load); |
| 462 | } |
| 463 | |
| 464 | static ssize_t store_go_hispeed_load(struct kobject *kobj, |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 465 | struct attribute *attr, const char *buf, size_t count) |
| 466 | { |
Allen Martin | 8b12552 | 2011-06-30 16:59:19 -0700 | [diff] [blame] | 467 | int ret; |
| 468 | unsigned long val; |
| 469 | |
| 470 | ret = strict_strtoul(buf, 0, &val); |
| 471 | if (ret < 0) |
| 472 | return ret; |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 473 | go_hispeed_load = val; |
Allen Martin | 8b12552 | 2011-06-30 16:59:19 -0700 | [diff] [blame] | 474 | return count; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 477 | static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644, |
| 478 | show_go_hispeed_load, store_go_hispeed_load); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 479 | |
| 480 | static ssize_t show_min_sample_time(struct kobject *kobj, |
| 481 | struct attribute *attr, char *buf) |
| 482 | { |
| 483 | return sprintf(buf, "%lu\n", min_sample_time); |
| 484 | } |
| 485 | |
| 486 | static ssize_t store_min_sample_time(struct kobject *kobj, |
| 487 | struct attribute *attr, const char *buf, size_t count) |
| 488 | { |
Allen Martin | 8b12552 | 2011-06-30 16:59:19 -0700 | [diff] [blame] | 489 | int ret; |
| 490 | unsigned long val; |
| 491 | |
| 492 | ret = strict_strtoul(buf, 0, &val); |
| 493 | if (ret < 0) |
| 494 | return ret; |
| 495 | min_sample_time = val; |
| 496 | return count; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644, |
| 500 | show_min_sample_time, store_min_sample_time); |
| 501 | |
Allen Martin | 5d14f98 | 2011-06-30 23:54:07 -0700 | [diff] [blame] | 502 | static ssize_t show_timer_rate(struct kobject *kobj, |
| 503 | struct attribute *attr, char *buf) |
| 504 | { |
| 505 | return sprintf(buf, "%lu\n", timer_rate); |
| 506 | } |
| 507 | |
| 508 | static ssize_t store_timer_rate(struct kobject *kobj, |
| 509 | struct attribute *attr, const char *buf, size_t count) |
| 510 | { |
| 511 | int ret; |
| 512 | unsigned long val; |
| 513 | |
| 514 | ret = strict_strtoul(buf, 0, &val); |
| 515 | if (ret < 0) |
| 516 | return ret; |
| 517 | timer_rate = val; |
| 518 | return count; |
| 519 | } |
| 520 | |
| 521 | static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644, |
| 522 | show_timer_rate, store_timer_rate); |
| 523 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 524 | static struct attribute *interactive_attributes[] = { |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 525 | &hispeed_freq_attr.attr, |
| 526 | &go_hispeed_load_attr.attr, |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 527 | &min_sample_time_attr.attr, |
Allen Martin | 5d14f98 | 2011-06-30 23:54:07 -0700 | [diff] [blame] | 528 | &timer_rate_attr.attr, |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 529 | NULL, |
| 530 | }; |
| 531 | |
| 532 | static struct attribute_group interactive_attr_group = { |
| 533 | .attrs = interactive_attributes, |
| 534 | .name = "interactive", |
| 535 | }; |
| 536 | |
Todd Poynor | 097d396 | 2011-06-06 18:30:23 -0700 | [diff] [blame] | 537 | static int cpufreq_governor_interactive(struct cpufreq_policy *policy, |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 538 | unsigned int event) |
| 539 | { |
| 540 | int rc; |
Todd Poynor | 097d396 | 2011-06-06 18:30:23 -0700 | [diff] [blame] | 541 | unsigned int j; |
| 542 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 543 | struct cpufreq_frequency_table *freq_table; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 544 | |
| 545 | switch (event) { |
| 546 | case CPUFREQ_GOV_START: |
Todd Poynor | 097d396 | 2011-06-06 18:30:23 -0700 | [diff] [blame] | 547 | if (!cpu_online(policy->cpu)) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 548 | return -EINVAL; |
| 549 | |
Todd Poynor | 097d396 | 2011-06-06 18:30:23 -0700 | [diff] [blame] | 550 | freq_table = |
| 551 | cpufreq_frequency_get_table(policy->cpu); |
| 552 | |
| 553 | for_each_cpu(j, policy->cpus) { |
| 554 | pcpu = &per_cpu(cpuinfo, j); |
| 555 | pcpu->policy = policy; |
| 556 | pcpu->target_freq = policy->cur; |
| 557 | pcpu->freq_table = freq_table; |
| 558 | pcpu->freq_change_time_in_idle = |
| 559 | get_cpu_idle_time_us(j, |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 560 | &pcpu->freq_change_time); |
Todd Poynor | 097d396 | 2011-06-06 18:30:23 -0700 | [diff] [blame] | 561 | pcpu->governor_enabled = 1; |
| 562 | smp_wmb(); |
| 563 | } |
| 564 | |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 565 | if (!hispeed_freq) |
| 566 | hispeed_freq = policy->max; |
| 567 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 568 | /* |
| 569 | * Do not register the idle hook and create sysfs |
| 570 | * entries if we have already done so. |
| 571 | */ |
| 572 | if (atomic_inc_return(&active_count) > 1) |
| 573 | return 0; |
| 574 | |
| 575 | rc = sysfs_create_group(cpufreq_global_kobject, |
| 576 | &interactive_attr_group); |
| 577 | if (rc) |
| 578 | return rc; |
| 579 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 580 | break; |
| 581 | |
| 582 | case CPUFREQ_GOV_STOP: |
Todd Poynor | 097d396 | 2011-06-06 18:30:23 -0700 | [diff] [blame] | 583 | for_each_cpu(j, policy->cpus) { |
| 584 | pcpu = &per_cpu(cpuinfo, j); |
| 585 | pcpu->governor_enabled = 0; |
| 586 | smp_wmb(); |
| 587 | del_timer_sync(&pcpu->cpu_timer); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 588 | |
Todd Poynor | 097d396 | 2011-06-06 18:30:23 -0700 | [diff] [blame] | 589 | /* |
| 590 | * Reset idle exit time since we may cancel the timer |
| 591 | * before it can run after the last idle exit time, |
| 592 | * to avoid tripping the check in idle exit for a timer |
| 593 | * that is trying to run. |
| 594 | */ |
| 595 | pcpu->idle_exit_time = 0; |
| 596 | } |
| 597 | |
| 598 | flush_work(&freq_scale_down_work); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 599 | if (atomic_dec_return(&active_count) > 0) |
| 600 | return 0; |
| 601 | |
| 602 | sysfs_remove_group(cpufreq_global_kobject, |
| 603 | &interactive_attr_group); |
| 604 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 605 | break; |
| 606 | |
| 607 | case CPUFREQ_GOV_LIMITS: |
Todd Poynor | 097d396 | 2011-06-06 18:30:23 -0700 | [diff] [blame] | 608 | if (policy->max < policy->cur) |
| 609 | __cpufreq_driver_target(policy, |
| 610 | policy->max, CPUFREQ_RELATION_H); |
| 611 | else if (policy->min > policy->cur) |
| 612 | __cpufreq_driver_target(policy, |
| 613 | policy->min, CPUFREQ_RELATION_L); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 614 | break; |
| 615 | } |
| 616 | return 0; |
| 617 | } |
| 618 | |
Allen Martin | 80e65d9 | 2011-06-28 09:58:39 -0700 | [diff] [blame] | 619 | static int cpufreq_interactive_idle_notifier(struct notifier_block *nb, |
| 620 | unsigned long val, |
| 621 | void *data) |
| 622 | { |
| 623 | switch (val) { |
| 624 | case IDLE_START: |
| 625 | cpufreq_interactive_idle_start(); |
| 626 | break; |
| 627 | case IDLE_END: |
| 628 | cpufreq_interactive_idle_end(); |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static struct notifier_block cpufreq_interactive_idle_nb = { |
| 636 | .notifier_call = cpufreq_interactive_idle_notifier, |
| 637 | }; |
| 638 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 639 | static int __init cpufreq_interactive_init(void) |
| 640 | { |
| 641 | unsigned int i; |
| 642 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 643 | struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
| 644 | |
Todd Poynor | 96cd250 | 2011-11-08 19:54:07 -0800 | [diff] [blame] | 645 | go_hispeed_load = DEFAULT_GO_HISPEED_LOAD; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 646 | min_sample_time = DEFAULT_MIN_SAMPLE_TIME; |
Allen Martin | 5d14f98 | 2011-06-30 23:54:07 -0700 | [diff] [blame] | 647 | timer_rate = DEFAULT_TIMER_RATE; |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 648 | |
| 649 | /* Initalize per-cpu timers */ |
| 650 | for_each_possible_cpu(i) { |
| 651 | pcpu = &per_cpu(cpuinfo, i); |
| 652 | init_timer(&pcpu->cpu_timer); |
| 653 | pcpu->cpu_timer.function = cpufreq_interactive_timer; |
| 654 | pcpu->cpu_timer.data = i; |
| 655 | } |
| 656 | |
| 657 | up_task = kthread_create(cpufreq_interactive_up_task, NULL, |
| 658 | "kinteractiveup"); |
| 659 | if (IS_ERR(up_task)) |
| 660 | return PTR_ERR(up_task); |
| 661 | |
| 662 | sched_setscheduler_nocheck(up_task, SCHED_FIFO, ¶m); |
| 663 | get_task_struct(up_task); |
| 664 | |
| 665 | /* No rescuer thread, bind to CPU queuing the work for possibly |
| 666 | warm cache (probably doesn't matter much). */ |
| 667 | down_wq = alloc_workqueue("knteractive_down", 0, 1); |
| 668 | |
Allen Martin | adce689 | 2011-07-01 11:19:14 -0700 | [diff] [blame] | 669 | if (!down_wq) |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 670 | goto err_freeuptask; |
| 671 | |
| 672 | INIT_WORK(&freq_scale_down_work, |
| 673 | cpufreq_interactive_freq_down); |
| 674 | |
| 675 | spin_lock_init(&up_cpumask_lock); |
| 676 | spin_lock_init(&down_cpumask_lock); |
Todd Poynor | a50b053 | 2011-06-22 14:34:23 -0700 | [diff] [blame] | 677 | mutex_init(&set_speed_lock); |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 678 | |
Allen Martin | 80e65d9 | 2011-06-28 09:58:39 -0700 | [diff] [blame] | 679 | idle_notifier_register(&cpufreq_interactive_idle_nb); |
| 680 | |
Mike Chan | 1dab259 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 681 | return cpufreq_register_governor(&cpufreq_gov_interactive); |
| 682 | |
| 683 | err_freeuptask: |
| 684 | put_task_struct(up_task); |
| 685 | return -ENOMEM; |
| 686 | } |
| 687 | |
| 688 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE |
| 689 | fs_initcall(cpufreq_interactive_init); |
| 690 | #else |
| 691 | module_init(cpufreq_interactive_init); |
| 692 | #endif |
| 693 | |
| 694 | static void __exit cpufreq_interactive_exit(void) |
| 695 | { |
| 696 | cpufreq_unregister_governor(&cpufreq_gov_interactive); |
| 697 | kthread_stop(up_task); |
| 698 | put_task_struct(up_task); |
| 699 | destroy_workqueue(down_wq); |
| 700 | } |
| 701 | |
| 702 | module_exit(cpufreq_interactive_exit); |
| 703 | |
| 704 | MODULE_AUTHOR("Mike Chan <mike@android.com>"); |
| 705 | MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for " |
| 706 | "Latency sensitive workloads"); |
| 707 | MODULE_LICENSE("GPL"); |