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; |
| 183 | |
| 184 | /* |
| 185 | * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time, |
| 186 | * this lets idle exit know the current idle time sample has |
| 187 | * been processed, and idle exit can generate a new sample and |
| 188 | * re-arm the timer. This prevents a concurrent idle |
| 189 | * exit on that CPU from writing a new set of info at the same time |
| 190 | * the timer function runs (the timer function can't use that info |
| 191 | * until more time passes). |
| 192 | */ |
| 193 | time_in_idle = pcpu->time_in_idle; |
| 194 | idle_exit_time = pcpu->idle_exit_time; |
| 195 | now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time); |
| 196 | smp_wmb(); |
| 197 | |
| 198 | /* If we raced with cancelling a timer, skip. */ |
| 199 | if (!idle_exit_time) { |
| 200 | dbgpr("timer %d: no valid idle exit sample\n", (int) data); |
| 201 | goto exit; |
| 202 | } |
| 203 | |
| 204 | #if DEBUG |
| 205 | if ((int) jiffies - (int) pcpu->cpu_timer.expires >= 10) |
| 206 | dbgpr("timer %d: late by %d ticks\n", |
| 207 | (int) data, jiffies - pcpu->cpu_timer.expires); |
| 208 | #endif |
| 209 | |
| 210 | delta_idle = (unsigned int) cputime64_sub(now_idle, time_in_idle); |
| 211 | delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time, |
| 212 | idle_exit_time); |
| 213 | |
| 214 | /* |
| 215 | * If timer ran less than 1ms after short-term sample started, retry. |
| 216 | */ |
| 217 | if (delta_time < 1000) { |
| 218 | dbgpr("timer %d: time delta %u too short exit=%llu now=%llu\n", (int) data, |
| 219 | delta_time, idle_exit_time, pcpu->timer_run_time); |
| 220 | goto rearm; |
| 221 | } |
| 222 | |
| 223 | if (delta_idle > delta_time) |
| 224 | cpu_load = 0; |
| 225 | else |
| 226 | cpu_load = 100 * (delta_time - delta_idle) / delta_time; |
| 227 | |
| 228 | delta_idle = (unsigned int) cputime64_sub(now_idle, |
| 229 | pcpu->freq_change_time_in_idle); |
| 230 | delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time, |
| 231 | pcpu->freq_change_time); |
| 232 | |
| 233 | if (delta_idle > delta_time) |
| 234 | load_since_change = 0; |
| 235 | else |
| 236 | load_since_change = |
| 237 | 100 * (delta_time - delta_idle) / delta_time; |
| 238 | |
| 239 | /* |
| 240 | * Choose greater of short-term load (since last idle timer |
| 241 | * started or timer function re-armed itself) or long-term load |
| 242 | * (since last frequency change). |
| 243 | */ |
| 244 | if (load_since_change > cpu_load) |
| 245 | cpu_load = load_since_change; |
| 246 | |
| 247 | if (cpu_load >= go_maxspeed_load) |
| 248 | new_freq = pcpu->policy->max; |
| 249 | else |
| 250 | new_freq = pcpu->policy->max * cpu_load / 100; |
| 251 | |
| 252 | if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table, |
| 253 | new_freq, CPUFREQ_RELATION_H, |
| 254 | &index)) { |
| 255 | dbgpr("timer %d: cpufreq_frequency_table_target error\n", (int) data); |
| 256 | goto rearm; |
| 257 | } |
| 258 | |
| 259 | new_freq = pcpu->freq_table[index].frequency; |
| 260 | |
| 261 | if (pcpu->target_freq == new_freq) |
| 262 | { |
| 263 | dbgpr("timer %d: load=%d, already at %d\n", (int) data, cpu_load, new_freq); |
| 264 | goto rearm_if_notmax; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Do not scale down unless we have been at this frequency for the |
| 269 | * minimum sample time. |
| 270 | */ |
| 271 | if (new_freq < pcpu->target_freq) { |
| 272 | if (cputime64_sub(pcpu->timer_run_time, pcpu->freq_change_time) < |
| 273 | min_sample_time) { |
| 274 | dbgpr("timer %d: load=%d cur=%d tgt=%d not yet\n", (int) data, cpu_load, pcpu->target_freq, new_freq); |
| 275 | goto rearm; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | dbgpr("timer %d: load=%d cur=%d tgt=%d queue\n", (int) data, cpu_load, pcpu->target_freq, new_freq); |
| 280 | |
| 281 | if (new_freq < pcpu->target_freq) { |
| 282 | pcpu->target_freq = new_freq; |
| 283 | spin_lock(&down_cpumask_lock); |
| 284 | cpumask_set_cpu(data, &down_cpumask); |
| 285 | spin_unlock(&down_cpumask_lock); |
| 286 | queue_work(down_wq, &freq_scale_down_work); |
| 287 | } else { |
| 288 | pcpu->target_freq = new_freq; |
| 289 | #if DEBUG |
| 290 | up_request_time = ktime_to_us(ktime_get()); |
| 291 | #endif |
| 292 | spin_lock(&up_cpumask_lock); |
| 293 | cpumask_set_cpu(data, &up_cpumask); |
| 294 | spin_unlock(&up_cpumask_lock); |
| 295 | wake_up_process(up_task); |
| 296 | } |
| 297 | |
| 298 | rearm_if_notmax: |
| 299 | /* |
| 300 | * Already set max speed and don't see a need to change that, |
| 301 | * wait until next idle to re-evaluate, don't need timer. |
| 302 | */ |
| 303 | if (pcpu->target_freq == pcpu->policy->max) |
| 304 | goto exit; |
| 305 | |
| 306 | rearm: |
| 307 | if (!timer_pending(&pcpu->cpu_timer)) { |
| 308 | /* |
| 309 | * If already at min: if that CPU is idle, don't set timer. |
| 310 | * Else cancel the timer if that CPU goes idle. We don't |
| 311 | * need to re-evaluate speed until the next idle exit. |
| 312 | */ |
| 313 | if (pcpu->target_freq == pcpu->policy->min) { |
| 314 | smp_rmb(); |
| 315 | |
| 316 | if (pcpu->idling) { |
| 317 | dbgpr("timer %d: cpu idle, don't re-arm\n", (int) data); |
| 318 | goto exit; |
| 319 | } |
| 320 | |
| 321 | pcpu->timer_idlecancel = 1; |
| 322 | } |
| 323 | |
| 324 | pcpu->time_in_idle = get_cpu_idle_time_us( |
| 325 | data, &pcpu->idle_exit_time); |
| 326 | mod_timer(&pcpu->cpu_timer, jiffies + 2); |
| 327 | dbgpr("timer %d: set timer for %lu exit=%llu\n", (int) data, pcpu->cpu_timer.expires, pcpu->idle_exit_time); |
| 328 | } |
| 329 | |
| 330 | exit: |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | static void cpufreq_interactive_idle(void) |
| 335 | { |
| 336 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 337 | &per_cpu(cpuinfo, smp_processor_id()); |
| 338 | int pending; |
| 339 | |
| 340 | if (!pcpu->governor_enabled) { |
| 341 | pm_idle_old(); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | pcpu->idling = 1; |
| 346 | smp_wmb(); |
| 347 | pending = timer_pending(&pcpu->cpu_timer); |
| 348 | |
| 349 | if (pcpu->target_freq != pcpu->policy->min) { |
| 350 | #ifdef CONFIG_SMP |
| 351 | /* |
| 352 | * Entering idle while not at lowest speed. On some |
| 353 | * platforms this can hold the other CPU(s) at that speed |
| 354 | * even though the CPU is idle. Set a timer to re-evaluate |
| 355 | * speed so this idle CPU doesn't hold the other CPUs above |
| 356 | * min indefinitely. This should probably be a quirk of |
| 357 | * the CPUFreq driver. |
| 358 | */ |
| 359 | if (!pending) { |
| 360 | pcpu->time_in_idle = get_cpu_idle_time_us( |
| 361 | smp_processor_id(), &pcpu->idle_exit_time); |
| 362 | pcpu->timer_idlecancel = 0; |
| 363 | mod_timer(&pcpu->cpu_timer, jiffies + 2); |
| 364 | dbgpr("idle: enter at %d, set timer for %lu exit=%llu\n", |
| 365 | pcpu->target_freq, pcpu->cpu_timer.expires, |
| 366 | pcpu->idle_exit_time); |
| 367 | } |
| 368 | #endif |
| 369 | } else { |
| 370 | /* |
| 371 | * If at min speed and entering idle after load has |
| 372 | * already been evaluated, and a timer has been set just in |
| 373 | * case the CPU suddenly goes busy, cancel that timer. The |
| 374 | * CPU didn't go busy; we'll recheck things upon idle exit. |
| 375 | */ |
| 376 | if (pending && pcpu->timer_idlecancel) { |
| 377 | dbgpr("idle: cancel timer for %lu\n", pcpu->cpu_timer.expires); |
| 378 | del_timer(&pcpu->cpu_timer); |
| 379 | /* |
| 380 | * Ensure last timer run time is after current idle |
| 381 | * sample start time, so next idle exit will always |
| 382 | * start a new idle sampling period. |
| 383 | */ |
| 384 | pcpu->idle_exit_time = 0; |
| 385 | pcpu->timer_idlecancel = 0; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | pm_idle_old(); |
| 390 | pcpu->idling = 0; |
| 391 | smp_wmb(); |
| 392 | |
| 393 | /* |
| 394 | * Arm the timer for 1-2 ticks later if not already, and if the timer |
| 395 | * function has already processed the previous load sampling |
| 396 | * interval. (If the timer is not pending but has not processed |
| 397 | * the previous interval, it is probably racing with us on another |
| 398 | * CPU. Let it compute load based on the previous sample and then |
| 399 | * re-arm the timer for another interval when it's done, rather |
| 400 | * than updating the interval start time to be "now", which doesn't |
| 401 | * give the timer function enough time to make a decision on this |
| 402 | * run.) |
| 403 | */ |
| 404 | if (timer_pending(&pcpu->cpu_timer) == 0 && |
| 405 | pcpu->timer_run_time >= pcpu->idle_exit_time) { |
| 406 | pcpu->time_in_idle = |
| 407 | get_cpu_idle_time_us(smp_processor_id(), |
| 408 | &pcpu->idle_exit_time); |
| 409 | pcpu->timer_idlecancel = 0; |
| 410 | mod_timer(&pcpu->cpu_timer, jiffies + 2); |
| 411 | dbgpr("idle: exit, set timer for %lu exit=%llu\n", pcpu->cpu_timer.expires, pcpu->idle_exit_time); |
| 412 | #if DEBUG |
| 413 | } else if (timer_pending(&pcpu->cpu_timer) == 0 && |
| 414 | pcpu->timer_run_time < pcpu->idle_exit_time) { |
| 415 | dbgpr("idle: timer not run yet: exit=%llu tmrrun=%llu\n", |
| 416 | pcpu->idle_exit_time, pcpu->timer_run_time); |
| 417 | #endif |
| 418 | } |
| 419 | |
| 420 | } |
| 421 | |
| 422 | static int cpufreq_interactive_up_task(void *data) |
| 423 | { |
| 424 | unsigned int cpu; |
| 425 | cpumask_t tmp_mask; |
| 426 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 427 | |
| 428 | #if DEBUG |
| 429 | u64 now; |
| 430 | u64 then; |
| 431 | unsigned int lat; |
| 432 | #endif |
| 433 | |
| 434 | while (1) { |
| 435 | set_current_state(TASK_INTERRUPTIBLE); |
| 436 | spin_lock(&up_cpumask_lock); |
| 437 | |
| 438 | if (cpumask_empty(&up_cpumask)) { |
| 439 | spin_unlock(&up_cpumask_lock); |
| 440 | schedule(); |
| 441 | |
| 442 | if (kthread_should_stop()) |
| 443 | break; |
| 444 | |
| 445 | spin_lock(&up_cpumask_lock); |
| 446 | } |
| 447 | |
| 448 | set_current_state(TASK_RUNNING); |
| 449 | |
| 450 | #if DEBUG |
| 451 | then = up_request_time; |
| 452 | now = ktime_to_us(ktime_get()); |
| 453 | |
| 454 | if (now > then) { |
| 455 | lat = ktime_to_us(ktime_get()) - then; |
| 456 | |
| 457 | if (lat > up_max_latency) |
| 458 | up_max_latency = lat; |
| 459 | } |
| 460 | #endif |
| 461 | |
| 462 | tmp_mask = up_cpumask; |
| 463 | cpumask_clear(&up_cpumask); |
| 464 | spin_unlock(&up_cpumask_lock); |
| 465 | |
| 466 | for_each_cpu(cpu, &tmp_mask) { |
| 467 | pcpu = &per_cpu(cpuinfo, cpu); |
| 468 | |
| 469 | if (nr_running() == 1) { |
| 470 | dbgpr("up %d: tgt=%d nothing else running\n", cpu, |
| 471 | pcpu->target_freq); |
| 472 | } |
| 473 | |
| 474 | __cpufreq_driver_target(pcpu->policy, |
| 475 | pcpu->target_freq, |
| 476 | CPUFREQ_RELATION_H); |
| 477 | pcpu->freq_change_time_in_idle = |
| 478 | get_cpu_idle_time_us(cpu, |
| 479 | &pcpu->freq_change_time); |
| 480 | dbgpr("up %d: set tgt=%d (actual=%d)\n", cpu, pcpu->target_freq, pcpu->policy->cur); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | static void cpufreq_interactive_freq_down(struct work_struct *work) |
| 488 | { |
| 489 | unsigned int cpu; |
| 490 | cpumask_t tmp_mask; |
| 491 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 492 | |
| 493 | spin_lock(&down_cpumask_lock); |
| 494 | tmp_mask = down_cpumask; |
| 495 | cpumask_clear(&down_cpumask); |
| 496 | spin_unlock(&down_cpumask_lock); |
| 497 | |
| 498 | for_each_cpu(cpu, &tmp_mask) { |
| 499 | pcpu = &per_cpu(cpuinfo, cpu); |
| 500 | __cpufreq_driver_target(pcpu->policy, |
| 501 | pcpu->target_freq, |
| 502 | CPUFREQ_RELATION_H); |
| 503 | pcpu->freq_change_time_in_idle = |
| 504 | get_cpu_idle_time_us(cpu, |
| 505 | &pcpu->freq_change_time); |
| 506 | dbgpr("down %d: set tgt=%d (actual=%d)\n", cpu, pcpu->target_freq, pcpu->policy->cur); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | static ssize_t show_go_maxspeed_load(struct kobject *kobj, |
| 511 | struct attribute *attr, char *buf) |
| 512 | { |
| 513 | return sprintf(buf, "%lu\n", go_maxspeed_load); |
| 514 | } |
| 515 | |
| 516 | static ssize_t store_go_maxspeed_load(struct kobject *kobj, |
| 517 | struct attribute *attr, const char *buf, size_t count) |
| 518 | { |
| 519 | return strict_strtoul(buf, 0, &go_maxspeed_load); |
| 520 | } |
| 521 | |
| 522 | static struct global_attr go_maxspeed_load_attr = __ATTR(go_maxspeed_load, 0644, |
| 523 | show_go_maxspeed_load, store_go_maxspeed_load); |
| 524 | |
| 525 | static ssize_t show_min_sample_time(struct kobject *kobj, |
| 526 | struct attribute *attr, char *buf) |
| 527 | { |
| 528 | return sprintf(buf, "%lu\n", min_sample_time); |
| 529 | } |
| 530 | |
| 531 | static ssize_t store_min_sample_time(struct kobject *kobj, |
| 532 | struct attribute *attr, const char *buf, size_t count) |
| 533 | { |
| 534 | return strict_strtoul(buf, 0, &min_sample_time); |
| 535 | } |
| 536 | |
| 537 | static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644, |
| 538 | show_min_sample_time, store_min_sample_time); |
| 539 | |
| 540 | static struct attribute *interactive_attributes[] = { |
| 541 | &go_maxspeed_load_attr.attr, |
| 542 | &min_sample_time_attr.attr, |
| 543 | NULL, |
| 544 | }; |
| 545 | |
| 546 | static struct attribute_group interactive_attr_group = { |
| 547 | .attrs = interactive_attributes, |
| 548 | .name = "interactive", |
| 549 | }; |
| 550 | |
| 551 | static int cpufreq_governor_interactive(struct cpufreq_policy *new_policy, |
| 552 | unsigned int event) |
| 553 | { |
| 554 | int rc; |
| 555 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 556 | &per_cpu(cpuinfo, new_policy->cpu); |
| 557 | |
| 558 | switch (event) { |
| 559 | case CPUFREQ_GOV_START: |
| 560 | if (!cpu_online(new_policy->cpu)) |
| 561 | return -EINVAL; |
| 562 | |
| 563 | pcpu->policy = new_policy; |
| 564 | pcpu->freq_table = cpufreq_frequency_get_table(new_policy->cpu); |
| 565 | pcpu->target_freq = new_policy->cur; |
| 566 | pcpu->freq_change_time_in_idle = |
| 567 | get_cpu_idle_time_us(new_policy->cpu, |
| 568 | &pcpu->freq_change_time); |
| 569 | pcpu->governor_enabled = 1; |
| 570 | /* |
| 571 | * Do not register the idle hook and create sysfs |
| 572 | * entries if we have already done so. |
| 573 | */ |
| 574 | if (atomic_inc_return(&active_count) > 1) |
| 575 | return 0; |
| 576 | |
| 577 | rc = sysfs_create_group(cpufreq_global_kobject, |
| 578 | &interactive_attr_group); |
| 579 | if (rc) |
| 580 | return rc; |
| 581 | |
| 582 | pm_idle_old = pm_idle; |
| 583 | pm_idle = cpufreq_interactive_idle; |
| 584 | break; |
| 585 | |
| 586 | case CPUFREQ_GOV_STOP: |
| 587 | pcpu->governor_enabled = 0; |
| 588 | |
| 589 | if (atomic_dec_return(&active_count) > 0) |
| 590 | return 0; |
| 591 | |
| 592 | sysfs_remove_group(cpufreq_global_kobject, |
| 593 | &interactive_attr_group); |
| 594 | |
| 595 | pm_idle = pm_idle_old; |
| 596 | del_timer(&pcpu->cpu_timer); |
| 597 | break; |
| 598 | |
| 599 | case CPUFREQ_GOV_LIMITS: |
| 600 | if (new_policy->max < new_policy->cur) |
| 601 | __cpufreq_driver_target(new_policy, |
| 602 | new_policy->max, CPUFREQ_RELATION_H); |
| 603 | else if (new_policy->min > new_policy->cur) |
| 604 | __cpufreq_driver_target(new_policy, |
| 605 | new_policy->min, CPUFREQ_RELATION_L); |
| 606 | break; |
| 607 | } |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | static int __init cpufreq_interactive_init(void) |
| 612 | { |
| 613 | unsigned int i; |
| 614 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 615 | struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
| 616 | |
| 617 | go_maxspeed_load = DEFAULT_GO_MAXSPEED_LOAD; |
| 618 | min_sample_time = DEFAULT_MIN_SAMPLE_TIME; |
| 619 | |
| 620 | /* Initalize per-cpu timers */ |
| 621 | for_each_possible_cpu(i) { |
| 622 | pcpu = &per_cpu(cpuinfo, i); |
| 623 | init_timer(&pcpu->cpu_timer); |
| 624 | pcpu->cpu_timer.function = cpufreq_interactive_timer; |
| 625 | pcpu->cpu_timer.data = i; |
| 626 | } |
| 627 | |
| 628 | up_task = kthread_create(cpufreq_interactive_up_task, NULL, |
| 629 | "kinteractiveup"); |
| 630 | if (IS_ERR(up_task)) |
| 631 | return PTR_ERR(up_task); |
| 632 | |
| 633 | sched_setscheduler_nocheck(up_task, SCHED_FIFO, ¶m); |
| 634 | get_task_struct(up_task); |
| 635 | |
| 636 | /* No rescuer thread, bind to CPU queuing the work for possibly |
| 637 | warm cache (probably doesn't matter much). */ |
| 638 | down_wq = alloc_workqueue("knteractive_down", 0, 1); |
| 639 | |
| 640 | if (! down_wq) |
| 641 | goto err_freeuptask; |
| 642 | |
| 643 | INIT_WORK(&freq_scale_down_work, |
| 644 | cpufreq_interactive_freq_down); |
| 645 | |
| 646 | spin_lock_init(&up_cpumask_lock); |
| 647 | spin_lock_init(&down_cpumask_lock); |
| 648 | |
| 649 | #if DEBUG |
| 650 | spin_lock_init(&dbgpr_lock); |
| 651 | dbg_proc = create_proc_entry("igov", S_IWUSR | S_IRUGO, NULL); |
| 652 | dbg_proc->read_proc = dbg_proc_read; |
| 653 | #endif |
| 654 | |
| 655 | return cpufreq_register_governor(&cpufreq_gov_interactive); |
| 656 | |
| 657 | err_freeuptask: |
| 658 | put_task_struct(up_task); |
| 659 | return -ENOMEM; |
| 660 | } |
| 661 | |
| 662 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE |
| 663 | fs_initcall(cpufreq_interactive_init); |
| 664 | #else |
| 665 | module_init(cpufreq_interactive_init); |
| 666 | #endif |
| 667 | |
| 668 | static void __exit cpufreq_interactive_exit(void) |
| 669 | { |
| 670 | cpufreq_unregister_governor(&cpufreq_gov_interactive); |
| 671 | kthread_stop(up_task); |
| 672 | put_task_struct(up_task); |
| 673 | destroy_workqueue(down_wq); |
| 674 | } |
| 675 | |
| 676 | module_exit(cpufreq_interactive_exit); |
| 677 | |
| 678 | MODULE_AUTHOR("Mike Chan <mike@android.com>"); |
| 679 | MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for " |
| 680 | "Latency sensitive workloads"); |
| 681 | MODULE_LICENSE("GPL"); |