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