blob: f90d3a5d52eea7a52e75b742b78f2376a28c2ebf [file] [log] [blame]
Mike Chan1dab2592010-06-22 11:26:45 -07001/*
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
Mike Chan1dab2592010-06-22 11:26:45 -070031static atomic_t active_count = ATOMIC_INIT(0);
32
33struct cpufreq_interactive_cpuinfo {
34 struct timer_list cpu_timer;
35 int timer_idlecancel;
36 u64 time_in_idle;
37 u64 idle_exit_time;
38 u64 timer_run_time;
39 int idling;
40 u64 freq_change_time;
41 u64 freq_change_time_in_idle;
42 struct cpufreq_policy *policy;
43 struct cpufreq_frequency_table *freq_table;
44 unsigned int target_freq;
45 int governor_enabled;
46};
47
48static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
49
50/* Workqueues handle frequency scaling */
51static struct task_struct *up_task;
52static struct workqueue_struct *down_wq;
53static struct work_struct freq_scale_down_work;
54static cpumask_t up_cpumask;
55static spinlock_t up_cpumask_lock;
56static cpumask_t down_cpumask;
57static spinlock_t down_cpumask_lock;
58
59/* Go to max speed when CPU load at or above this value. */
60#define DEFAULT_GO_MAXSPEED_LOAD 85
61static unsigned long go_maxspeed_load;
62
63/*
64 * The minimum amount of time to spend at a frequency before we can ramp down.
65 */
66#define DEFAULT_MIN_SAMPLE_TIME 80000;
67static unsigned long min_sample_time;
68
Allen Martin5d14f982011-06-30 23:54:07 -070069/*
70 * The sample rate of the timer used to increase frequency
71 */
72#define DEFAULT_TIMER_RATE 30000;
73static unsigned long timer_rate;
74
Mike Chan1dab2592010-06-22 11:26:45 -070075static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
76 unsigned int event);
77
78#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
79static
80#endif
81struct cpufreq_governor cpufreq_gov_interactive = {
82 .name = "interactive",
83 .governor = cpufreq_governor_interactive,
84 .max_transition_latency = 10000000,
85 .owner = THIS_MODULE,
86};
87
88static void cpufreq_interactive_timer(unsigned long data)
89{
90 unsigned int delta_idle;
91 unsigned int delta_time;
92 int cpu_load;
93 int load_since_change;
94 u64 time_in_idle;
95 u64 idle_exit_time;
96 struct cpufreq_interactive_cpuinfo *pcpu =
97 &per_cpu(cpuinfo, data);
98 u64 now_idle;
99 unsigned int new_freq;
100 unsigned int index;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800101 unsigned long flags;
Mike Chan1dab2592010-06-22 11:26:45 -0700102
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800103 smp_rmb();
104
105 if (!pcpu->governor_enabled)
106 goto exit;
107
Mike Chan1dab2592010-06-22 11:26:45 -0700108 /*
109 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
110 * this lets idle exit know the current idle time sample has
111 * been processed, and idle exit can generate a new sample and
112 * re-arm the timer. This prevents a concurrent idle
113 * exit on that CPU from writing a new set of info at the same time
114 * the timer function runs (the timer function can't use that info
115 * until more time passes).
116 */
117 time_in_idle = pcpu->time_in_idle;
118 idle_exit_time = pcpu->idle_exit_time;
119 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
120 smp_wmb();
121
122 /* If we raced with cancelling a timer, skip. */
Allen Martin13b2b142011-06-28 10:40:30 -0700123 if (!idle_exit_time)
Mike Chan1dab2592010-06-22 11:26:45 -0700124 goto exit;
Mike Chan1dab2592010-06-22 11:26:45 -0700125
126 delta_idle = (unsigned int) cputime64_sub(now_idle, time_in_idle);
127 delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time,
128 idle_exit_time);
129
130 /*
131 * If timer ran less than 1ms after short-term sample started, retry.
132 */
Allen Martin13b2b142011-06-28 10:40:30 -0700133 if (delta_time < 1000)
Mike Chan1dab2592010-06-22 11:26:45 -0700134 goto rearm;
Mike Chan1dab2592010-06-22 11:26:45 -0700135
136 if (delta_idle > delta_time)
137 cpu_load = 0;
138 else
139 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
140
141 delta_idle = (unsigned int) cputime64_sub(now_idle,
Allen Martinadce6892011-07-01 11:19:14 -0700142 pcpu->freq_change_time_in_idle);
Mike Chan1dab2592010-06-22 11:26:45 -0700143 delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time,
144 pcpu->freq_change_time);
145
146 if (delta_idle > delta_time)
147 load_since_change = 0;
148 else
149 load_since_change =
150 100 * (delta_time - delta_idle) / delta_time;
151
152 /*
153 * Choose greater of short-term load (since last idle timer
154 * started or timer function re-armed itself) or long-term load
155 * (since last frequency change).
156 */
157 if (load_since_change > cpu_load)
158 cpu_load = load_since_change;
159
160 if (cpu_load >= go_maxspeed_load)
161 new_freq = pcpu->policy->max;
162 else
163 new_freq = pcpu->policy->max * cpu_load / 100;
164
165 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
166 new_freq, CPUFREQ_RELATION_H,
167 &index)) {
Allen Martin13b2b142011-06-28 10:40:30 -0700168 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
169 (int) data);
Mike Chan1dab2592010-06-22 11:26:45 -0700170 goto rearm;
171 }
172
173 new_freq = pcpu->freq_table[index].frequency;
174
175 if (pcpu->target_freq == new_freq)
Mike Chan1dab2592010-06-22 11:26:45 -0700176 goto rearm_if_notmax;
Mike Chan1dab2592010-06-22 11:26:45 -0700177
178 /*
179 * Do not scale down unless we have been at this frequency for the
180 * minimum sample time.
181 */
182 if (new_freq < pcpu->target_freq) {
Allen Martinadce6892011-07-01 11:19:14 -0700183 if (cputime64_sub(pcpu->timer_run_time, pcpu->freq_change_time)
184 < min_sample_time)
Mike Chan1dab2592010-06-22 11:26:45 -0700185 goto rearm;
Mike Chan1dab2592010-06-22 11:26:45 -0700186 }
187
Mike Chan1dab2592010-06-22 11:26:45 -0700188 if (new_freq < pcpu->target_freq) {
189 pcpu->target_freq = new_freq;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800190 spin_lock_irqsave(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700191 cpumask_set_cpu(data, &down_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800192 spin_unlock_irqrestore(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700193 queue_work(down_wq, &freq_scale_down_work);
194 } else {
195 pcpu->target_freq = new_freq;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800196 spin_lock_irqsave(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700197 cpumask_set_cpu(data, &up_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800198 spin_unlock_irqrestore(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700199 wake_up_process(up_task);
200 }
201
202rearm_if_notmax:
203 /*
204 * Already set max speed and don't see a need to change that,
205 * wait until next idle to re-evaluate, don't need timer.
206 */
207 if (pcpu->target_freq == pcpu->policy->max)
208 goto exit;
209
210rearm:
211 if (!timer_pending(&pcpu->cpu_timer)) {
212 /*
213 * If already at min: if that CPU is idle, don't set timer.
214 * Else cancel the timer if that CPU goes idle. We don't
215 * need to re-evaluate speed until the next idle exit.
216 */
217 if (pcpu->target_freq == pcpu->policy->min) {
218 smp_rmb();
219
Allen Martin13b2b142011-06-28 10:40:30 -0700220 if (pcpu->idling)
Mike Chan1dab2592010-06-22 11:26:45 -0700221 goto exit;
Mike Chan1dab2592010-06-22 11:26:45 -0700222
223 pcpu->timer_idlecancel = 1;
224 }
225
226 pcpu->time_in_idle = get_cpu_idle_time_us(
227 data, &pcpu->idle_exit_time);
Allen Martin9549cff2011-07-19 14:56:21 -0700228 mod_timer(&pcpu->cpu_timer,
229 jiffies + usecs_to_jiffies(timer_rate));
Mike Chan1dab2592010-06-22 11:26:45 -0700230 }
231
232exit:
233 return;
234}
235
Allen Martin80e65d92011-06-28 09:58:39 -0700236static void cpufreq_interactive_idle_start(void)
Mike Chan1dab2592010-06-22 11:26:45 -0700237{
238 struct cpufreq_interactive_cpuinfo *pcpu =
239 &per_cpu(cpuinfo, smp_processor_id());
240 int pending;
241
Allen Martinadce6892011-07-01 11:19:14 -0700242 if (!pcpu->governor_enabled)
Mike Chan1dab2592010-06-22 11:26:45 -0700243 return;
Mike Chan1dab2592010-06-22 11:26:45 -0700244
245 pcpu->idling = 1;
246 smp_wmb();
247 pending = timer_pending(&pcpu->cpu_timer);
248
249 if (pcpu->target_freq != pcpu->policy->min) {
250#ifdef CONFIG_SMP
251 /*
252 * Entering idle while not at lowest speed. On some
253 * platforms this can hold the other CPU(s) at that speed
254 * even though the CPU is idle. Set a timer to re-evaluate
255 * speed so this idle CPU doesn't hold the other CPUs above
256 * min indefinitely. This should probably be a quirk of
257 * the CPUFreq driver.
258 */
259 if (!pending) {
260 pcpu->time_in_idle = get_cpu_idle_time_us(
261 smp_processor_id(), &pcpu->idle_exit_time);
262 pcpu->timer_idlecancel = 0;
Allen Martin9549cff2011-07-19 14:56:21 -0700263 mod_timer(&pcpu->cpu_timer,
264 jiffies + usecs_to_jiffies(timer_rate));
Mike Chan1dab2592010-06-22 11:26:45 -0700265 }
266#endif
267 } else {
268 /*
269 * If at min speed and entering idle after load has
270 * already been evaluated, and a timer has been set just in
271 * case the CPU suddenly goes busy, cancel that timer. The
272 * CPU didn't go busy; we'll recheck things upon idle exit.
273 */
274 if (pending && pcpu->timer_idlecancel) {
Mike Chan1dab2592010-06-22 11:26:45 -0700275 del_timer(&pcpu->cpu_timer);
276 /*
277 * Ensure last timer run time is after current idle
278 * sample start time, so next idle exit will always
279 * start a new idle sampling period.
280 */
281 pcpu->idle_exit_time = 0;
282 pcpu->timer_idlecancel = 0;
283 }
284 }
285
Allen Martin80e65d92011-06-28 09:58:39 -0700286}
287
288static void cpufreq_interactive_idle_end(void)
289{
290 struct cpufreq_interactive_cpuinfo *pcpu =
291 &per_cpu(cpuinfo, smp_processor_id());
292
Mike Chan1dab2592010-06-22 11:26:45 -0700293 pcpu->idling = 0;
294 smp_wmb();
295
296 /*
297 * Arm the timer for 1-2 ticks later if not already, and if the timer
298 * function has already processed the previous load sampling
299 * interval. (If the timer is not pending but has not processed
300 * the previous interval, it is probably racing with us on another
301 * CPU. Let it compute load based on the previous sample and then
302 * re-arm the timer for another interval when it's done, rather
303 * than updating the interval start time to be "now", which doesn't
304 * give the timer function enough time to make a decision on this
305 * run.)
306 */
307 if (timer_pending(&pcpu->cpu_timer) == 0 &&
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800308 pcpu->timer_run_time >= pcpu->idle_exit_time &&
309 pcpu->governor_enabled) {
Mike Chan1dab2592010-06-22 11:26:45 -0700310 pcpu->time_in_idle =
311 get_cpu_idle_time_us(smp_processor_id(),
312 &pcpu->idle_exit_time);
313 pcpu->timer_idlecancel = 0;
Allen Martin9549cff2011-07-19 14:56:21 -0700314 mod_timer(&pcpu->cpu_timer,
315 jiffies + usecs_to_jiffies(timer_rate));
Mike Chan1dab2592010-06-22 11:26:45 -0700316 }
317
318}
319
320static int cpufreq_interactive_up_task(void *data)
321{
322 unsigned int cpu;
323 cpumask_t tmp_mask;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800324 unsigned long flags;
Mike Chan1dab2592010-06-22 11:26:45 -0700325 struct cpufreq_interactive_cpuinfo *pcpu;
326
Mike Chan1dab2592010-06-22 11:26:45 -0700327 while (1) {
328 set_current_state(TASK_INTERRUPTIBLE);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800329 spin_lock_irqsave(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700330
331 if (cpumask_empty(&up_cpumask)) {
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800332 spin_unlock_irqrestore(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700333 schedule();
334
335 if (kthread_should_stop())
336 break;
337
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800338 spin_lock_irqsave(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700339 }
340
341 set_current_state(TASK_RUNNING);
342
Mike Chan1dab2592010-06-22 11:26:45 -0700343 tmp_mask = up_cpumask;
344 cpumask_clear(&up_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800345 spin_unlock_irqrestore(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700346
347 for_each_cpu(cpu, &tmp_mask) {
348 pcpu = &per_cpu(cpuinfo, cpu);
349
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800350 smp_rmb();
351
352 if (!pcpu->governor_enabled)
353 continue;
354
Mike Chan1dab2592010-06-22 11:26:45 -0700355 __cpufreq_driver_target(pcpu->policy,
356 pcpu->target_freq,
357 CPUFREQ_RELATION_H);
358 pcpu->freq_change_time_in_idle =
359 get_cpu_idle_time_us(cpu,
360 &pcpu->freq_change_time);
Mike Chan1dab2592010-06-22 11:26:45 -0700361 }
362 }
363
364 return 0;
365}
366
367static void cpufreq_interactive_freq_down(struct work_struct *work)
368{
369 unsigned int cpu;
370 cpumask_t tmp_mask;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800371 unsigned long flags;
Mike Chan1dab2592010-06-22 11:26:45 -0700372 struct cpufreq_interactive_cpuinfo *pcpu;
373
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800374 spin_lock_irqsave(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700375 tmp_mask = down_cpumask;
376 cpumask_clear(&down_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800377 spin_unlock_irqrestore(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700378
379 for_each_cpu(cpu, &tmp_mask) {
380 pcpu = &per_cpu(cpuinfo, cpu);
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800381
382 smp_rmb();
383
384 if (!pcpu->governor_enabled)
385 continue;
386
Mike Chan1dab2592010-06-22 11:26:45 -0700387 __cpufreq_driver_target(pcpu->policy,
388 pcpu->target_freq,
389 CPUFREQ_RELATION_H);
390 pcpu->freq_change_time_in_idle =
391 get_cpu_idle_time_us(cpu,
392 &pcpu->freq_change_time);
Mike Chan1dab2592010-06-22 11:26:45 -0700393 }
394}
395
396static ssize_t show_go_maxspeed_load(struct kobject *kobj,
397 struct attribute *attr, char *buf)
398{
399 return sprintf(buf, "%lu\n", go_maxspeed_load);
400}
401
402static ssize_t store_go_maxspeed_load(struct kobject *kobj,
403 struct attribute *attr, const char *buf, size_t count)
404{
Allen Martin8b125522011-06-30 16:59:19 -0700405 int ret;
406 unsigned long val;
407
408 ret = strict_strtoul(buf, 0, &val);
409 if (ret < 0)
410 return ret;
411 go_maxspeed_load = val;
412 return count;
Mike Chan1dab2592010-06-22 11:26:45 -0700413}
414
415static struct global_attr go_maxspeed_load_attr = __ATTR(go_maxspeed_load, 0644,
416 show_go_maxspeed_load, store_go_maxspeed_load);
417
418static ssize_t show_min_sample_time(struct kobject *kobj,
419 struct attribute *attr, char *buf)
420{
421 return sprintf(buf, "%lu\n", min_sample_time);
422}
423
424static ssize_t store_min_sample_time(struct kobject *kobj,
425 struct attribute *attr, const char *buf, size_t count)
426{
Allen Martin8b125522011-06-30 16:59:19 -0700427 int ret;
428 unsigned long val;
429
430 ret = strict_strtoul(buf, 0, &val);
431 if (ret < 0)
432 return ret;
433 min_sample_time = val;
434 return count;
Mike Chan1dab2592010-06-22 11:26:45 -0700435}
436
437static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
438 show_min_sample_time, store_min_sample_time);
439
Allen Martin5d14f982011-06-30 23:54:07 -0700440static ssize_t show_timer_rate(struct kobject *kobj,
441 struct attribute *attr, char *buf)
442{
443 return sprintf(buf, "%lu\n", timer_rate);
444}
445
446static ssize_t store_timer_rate(struct kobject *kobj,
447 struct attribute *attr, const char *buf, size_t count)
448{
449 int ret;
450 unsigned long val;
451
452 ret = strict_strtoul(buf, 0, &val);
453 if (ret < 0)
454 return ret;
455 timer_rate = val;
456 return count;
457}
458
459static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
460 show_timer_rate, store_timer_rate);
461
Mike Chan1dab2592010-06-22 11:26:45 -0700462static struct attribute *interactive_attributes[] = {
463 &go_maxspeed_load_attr.attr,
464 &min_sample_time_attr.attr,
Allen Martin5d14f982011-06-30 23:54:07 -0700465 &timer_rate_attr.attr,
Mike Chan1dab2592010-06-22 11:26:45 -0700466 NULL,
467};
468
469static struct attribute_group interactive_attr_group = {
470 .attrs = interactive_attributes,
471 .name = "interactive",
472};
473
Todd Poynor097d3962011-06-06 18:30:23 -0700474static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
Mike Chan1dab2592010-06-22 11:26:45 -0700475 unsigned int event)
476{
477 int rc;
Todd Poynor097d3962011-06-06 18:30:23 -0700478 unsigned int j;
479 struct cpufreq_interactive_cpuinfo *pcpu;
480 struct cpufreq_frequency_table *freq_table;
Mike Chan1dab2592010-06-22 11:26:45 -0700481
482 switch (event) {
483 case CPUFREQ_GOV_START:
Todd Poynor097d3962011-06-06 18:30:23 -0700484 if (!cpu_online(policy->cpu))
Mike Chan1dab2592010-06-22 11:26:45 -0700485 return -EINVAL;
486
Todd Poynor097d3962011-06-06 18:30:23 -0700487 freq_table =
488 cpufreq_frequency_get_table(policy->cpu);
489
490 for_each_cpu(j, policy->cpus) {
491 pcpu = &per_cpu(cpuinfo, j);
492 pcpu->policy = policy;
493 pcpu->target_freq = policy->cur;
494 pcpu->freq_table = freq_table;
495 pcpu->freq_change_time_in_idle =
496 get_cpu_idle_time_us(j,
Mike Chan1dab2592010-06-22 11:26:45 -0700497 &pcpu->freq_change_time);
Todd Poynor097d3962011-06-06 18:30:23 -0700498 pcpu->governor_enabled = 1;
499 smp_wmb();
500 }
501
Mike Chan1dab2592010-06-22 11:26:45 -0700502 /*
503 * Do not register the idle hook and create sysfs
504 * entries if we have already done so.
505 */
506 if (atomic_inc_return(&active_count) > 1)
507 return 0;
508
509 rc = sysfs_create_group(cpufreq_global_kobject,
510 &interactive_attr_group);
511 if (rc)
512 return rc;
513
Mike Chan1dab2592010-06-22 11:26:45 -0700514 break;
515
516 case CPUFREQ_GOV_STOP:
Todd Poynor097d3962011-06-06 18:30:23 -0700517 for_each_cpu(j, policy->cpus) {
518 pcpu = &per_cpu(cpuinfo, j);
519 pcpu->governor_enabled = 0;
520 smp_wmb();
521 del_timer_sync(&pcpu->cpu_timer);
Mike Chan1dab2592010-06-22 11:26:45 -0700522
Todd Poynor097d3962011-06-06 18:30:23 -0700523 /*
524 * Reset idle exit time since we may cancel the timer
525 * before it can run after the last idle exit time,
526 * to avoid tripping the check in idle exit for a timer
527 * that is trying to run.
528 */
529 pcpu->idle_exit_time = 0;
530 }
531
532 flush_work(&freq_scale_down_work);
Mike Chan1dab2592010-06-22 11:26:45 -0700533 if (atomic_dec_return(&active_count) > 0)
534 return 0;
535
536 sysfs_remove_group(cpufreq_global_kobject,
537 &interactive_attr_group);
538
Mike Chan1dab2592010-06-22 11:26:45 -0700539 break;
540
541 case CPUFREQ_GOV_LIMITS:
Todd Poynor097d3962011-06-06 18:30:23 -0700542 if (policy->max < policy->cur)
543 __cpufreq_driver_target(policy,
544 policy->max, CPUFREQ_RELATION_H);
545 else if (policy->min > policy->cur)
546 __cpufreq_driver_target(policy,
547 policy->min, CPUFREQ_RELATION_L);
Mike Chan1dab2592010-06-22 11:26:45 -0700548 break;
549 }
550 return 0;
551}
552
Allen Martin80e65d92011-06-28 09:58:39 -0700553static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
554 unsigned long val,
555 void *data)
556{
557 switch (val) {
558 case IDLE_START:
559 cpufreq_interactive_idle_start();
560 break;
561 case IDLE_END:
562 cpufreq_interactive_idle_end();
563 break;
564 }
565
566 return 0;
567}
568
569static struct notifier_block cpufreq_interactive_idle_nb = {
570 .notifier_call = cpufreq_interactive_idle_notifier,
571};
572
Mike Chan1dab2592010-06-22 11:26:45 -0700573static int __init cpufreq_interactive_init(void)
574{
575 unsigned int i;
576 struct cpufreq_interactive_cpuinfo *pcpu;
577 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
578
579 go_maxspeed_load = DEFAULT_GO_MAXSPEED_LOAD;
580 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Allen Martin5d14f982011-06-30 23:54:07 -0700581 timer_rate = DEFAULT_TIMER_RATE;
Mike Chan1dab2592010-06-22 11:26:45 -0700582
583 /* Initalize per-cpu timers */
584 for_each_possible_cpu(i) {
585 pcpu = &per_cpu(cpuinfo, i);
586 init_timer(&pcpu->cpu_timer);
587 pcpu->cpu_timer.function = cpufreq_interactive_timer;
588 pcpu->cpu_timer.data = i;
589 }
590
591 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
592 "kinteractiveup");
593 if (IS_ERR(up_task))
594 return PTR_ERR(up_task);
595
596 sched_setscheduler_nocheck(up_task, SCHED_FIFO, &param);
597 get_task_struct(up_task);
598
599 /* No rescuer thread, bind to CPU queuing the work for possibly
600 warm cache (probably doesn't matter much). */
601 down_wq = alloc_workqueue("knteractive_down", 0, 1);
602
Allen Martinadce6892011-07-01 11:19:14 -0700603 if (!down_wq)
Mike Chan1dab2592010-06-22 11:26:45 -0700604 goto err_freeuptask;
605
606 INIT_WORK(&freq_scale_down_work,
607 cpufreq_interactive_freq_down);
608
609 spin_lock_init(&up_cpumask_lock);
610 spin_lock_init(&down_cpumask_lock);
611
Allen Martin80e65d92011-06-28 09:58:39 -0700612 idle_notifier_register(&cpufreq_interactive_idle_nb);
613
Mike Chan1dab2592010-06-22 11:26:45 -0700614 return cpufreq_register_governor(&cpufreq_gov_interactive);
615
616err_freeuptask:
617 put_task_struct(up_task);
618 return -ENOMEM;
619}
620
621#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
622fs_initcall(cpufreq_interactive_init);
623#else
624module_init(cpufreq_interactive_init);
625#endif
626
627static void __exit cpufreq_interactive_exit(void)
628{
629 cpufreq_unregister_governor(&cpufreq_gov_interactive);
630 kthread_stop(up_task);
631 put_task_struct(up_task);
632 destroy_workqueue(down_wq);
633}
634
635module_exit(cpufreq_interactive_exit);
636
637MODULE_AUTHOR("Mike Chan <mike@android.com>");
638MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
639 "Latency sensitive workloads");
640MODULE_LICENSE("GPL");