blob: 6a98631d294732b9746229851ffe16d356a6a022 [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,
142 pcpu->freq_change_time_in_idle);
143 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) {
183 if (cputime64_sub(pcpu->timer_run_time, pcpu->freq_change_time) <
Allen Martin13b2b142011-06-28 10:40:30 -0700184 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 Martin5d14f982011-06-30 23:54:07 -0700228 mod_timer(&pcpu->cpu_timer, jiffies + usecs_to_jiffies(timer_rate));
Mike Chan1dab2592010-06-22 11:26:45 -0700229 }
230
231exit:
232 return;
233}
234
Allen Martin80e65d92011-06-28 09:58:39 -0700235static void cpufreq_interactive_idle_start(void)
Mike Chan1dab2592010-06-22 11:26:45 -0700236{
237 struct cpufreq_interactive_cpuinfo *pcpu =
238 &per_cpu(cpuinfo, smp_processor_id());
239 int pending;
240
241 if (!pcpu->governor_enabled) {
Mike Chan1dab2592010-06-22 11:26:45 -0700242 return;
243 }
244
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 Martin5d14f982011-06-30 23:54:07 -0700263 mod_timer(&pcpu->cpu_timer, jiffies + usecs_to_jiffies(timer_rate));
Mike Chan1dab2592010-06-22 11:26:45 -0700264 }
265#endif
266 } else {
267 /*
268 * If at min speed and entering idle after load has
269 * already been evaluated, and a timer has been set just in
270 * case the CPU suddenly goes busy, cancel that timer. The
271 * CPU didn't go busy; we'll recheck things upon idle exit.
272 */
273 if (pending && pcpu->timer_idlecancel) {
Mike Chan1dab2592010-06-22 11:26:45 -0700274 del_timer(&pcpu->cpu_timer);
275 /*
276 * Ensure last timer run time is after current idle
277 * sample start time, so next idle exit will always
278 * start a new idle sampling period.
279 */
280 pcpu->idle_exit_time = 0;
281 pcpu->timer_idlecancel = 0;
282 }
283 }
284
Allen Martin80e65d92011-06-28 09:58:39 -0700285}
286
287static void cpufreq_interactive_idle_end(void)
288{
289 struct cpufreq_interactive_cpuinfo *pcpu =
290 &per_cpu(cpuinfo, smp_processor_id());
291
Mike Chan1dab2592010-06-22 11:26:45 -0700292 pcpu->idling = 0;
293 smp_wmb();
294
295 /*
296 * Arm the timer for 1-2 ticks later if not already, and if the timer
297 * function has already processed the previous load sampling
298 * interval. (If the timer is not pending but has not processed
299 * the previous interval, it is probably racing with us on another
300 * CPU. Let it compute load based on the previous sample and then
301 * re-arm the timer for another interval when it's done, rather
302 * than updating the interval start time to be "now", which doesn't
303 * give the timer function enough time to make a decision on this
304 * run.)
305 */
306 if (timer_pending(&pcpu->cpu_timer) == 0 &&
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800307 pcpu->timer_run_time >= pcpu->idle_exit_time &&
308 pcpu->governor_enabled) {
Mike Chan1dab2592010-06-22 11:26:45 -0700309 pcpu->time_in_idle =
310 get_cpu_idle_time_us(smp_processor_id(),
311 &pcpu->idle_exit_time);
312 pcpu->timer_idlecancel = 0;
Allen Martin5d14f982011-06-30 23:54:07 -0700313 mod_timer(&pcpu->cpu_timer, jiffies + usecs_to_jiffies(timer_rate));
Mike Chan1dab2592010-06-22 11:26:45 -0700314 }
315
316}
317
318static int cpufreq_interactive_up_task(void *data)
319{
320 unsigned int cpu;
321 cpumask_t tmp_mask;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800322 unsigned long flags;
Mike Chan1dab2592010-06-22 11:26:45 -0700323 struct cpufreq_interactive_cpuinfo *pcpu;
324
Mike Chan1dab2592010-06-22 11:26:45 -0700325 while (1) {
326 set_current_state(TASK_INTERRUPTIBLE);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800327 spin_lock_irqsave(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700328
329 if (cpumask_empty(&up_cpumask)) {
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800330 spin_unlock_irqrestore(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700331 schedule();
332
333 if (kthread_should_stop())
334 break;
335
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800336 spin_lock_irqsave(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700337 }
338
339 set_current_state(TASK_RUNNING);
340
Mike Chan1dab2592010-06-22 11:26:45 -0700341 tmp_mask = up_cpumask;
342 cpumask_clear(&up_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800343 spin_unlock_irqrestore(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700344
345 for_each_cpu(cpu, &tmp_mask) {
346 pcpu = &per_cpu(cpuinfo, cpu);
347
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800348 smp_rmb();
349
350 if (!pcpu->governor_enabled)
351 continue;
352
Mike Chan1dab2592010-06-22 11:26:45 -0700353 __cpufreq_driver_target(pcpu->policy,
354 pcpu->target_freq,
355 CPUFREQ_RELATION_H);
356 pcpu->freq_change_time_in_idle =
357 get_cpu_idle_time_us(cpu,
358 &pcpu->freq_change_time);
Mike Chan1dab2592010-06-22 11:26:45 -0700359 }
360 }
361
362 return 0;
363}
364
365static void cpufreq_interactive_freq_down(struct work_struct *work)
366{
367 unsigned int cpu;
368 cpumask_t tmp_mask;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800369 unsigned long flags;
Mike Chan1dab2592010-06-22 11:26:45 -0700370 struct cpufreq_interactive_cpuinfo *pcpu;
371
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800372 spin_lock_irqsave(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700373 tmp_mask = down_cpumask;
374 cpumask_clear(&down_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800375 spin_unlock_irqrestore(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700376
377 for_each_cpu(cpu, &tmp_mask) {
378 pcpu = &per_cpu(cpuinfo, cpu);
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800379
380 smp_rmb();
381
382 if (!pcpu->governor_enabled)
383 continue;
384
Mike Chan1dab2592010-06-22 11:26:45 -0700385 __cpufreq_driver_target(pcpu->policy,
386 pcpu->target_freq,
387 CPUFREQ_RELATION_H);
388 pcpu->freq_change_time_in_idle =
389 get_cpu_idle_time_us(cpu,
390 &pcpu->freq_change_time);
Mike Chan1dab2592010-06-22 11:26:45 -0700391 }
392}
393
394static ssize_t show_go_maxspeed_load(struct kobject *kobj,
395 struct attribute *attr, char *buf)
396{
397 return sprintf(buf, "%lu\n", go_maxspeed_load);
398}
399
400static ssize_t store_go_maxspeed_load(struct kobject *kobj,
401 struct attribute *attr, const char *buf, size_t count)
402{
Allen Martin8b125522011-06-30 16:59:19 -0700403 int ret;
404 unsigned long val;
405
406 ret = strict_strtoul(buf, 0, &val);
407 if (ret < 0)
408 return ret;
409 go_maxspeed_load = val;
410 return count;
Mike Chan1dab2592010-06-22 11:26:45 -0700411}
412
413static struct global_attr go_maxspeed_load_attr = __ATTR(go_maxspeed_load, 0644,
414 show_go_maxspeed_load, store_go_maxspeed_load);
415
416static ssize_t show_min_sample_time(struct kobject *kobj,
417 struct attribute *attr, char *buf)
418{
419 return sprintf(buf, "%lu\n", min_sample_time);
420}
421
422static ssize_t store_min_sample_time(struct kobject *kobj,
423 struct attribute *attr, const char *buf, size_t count)
424{
Allen Martin8b125522011-06-30 16:59:19 -0700425 int ret;
426 unsigned long val;
427
428 ret = strict_strtoul(buf, 0, &val);
429 if (ret < 0)
430 return ret;
431 min_sample_time = val;
432 return count;
Mike Chan1dab2592010-06-22 11:26:45 -0700433}
434
435static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
436 show_min_sample_time, store_min_sample_time);
437
Allen Martin5d14f982011-06-30 23:54:07 -0700438static ssize_t show_timer_rate(struct kobject *kobj,
439 struct attribute *attr, char *buf)
440{
441 return sprintf(buf, "%lu\n", timer_rate);
442}
443
444static ssize_t store_timer_rate(struct kobject *kobj,
445 struct attribute *attr, const char *buf, size_t count)
446{
447 int ret;
448 unsigned long val;
449
450 ret = strict_strtoul(buf, 0, &val);
451 if (ret < 0)
452 return ret;
453 timer_rate = val;
454 return count;
455}
456
457static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
458 show_timer_rate, store_timer_rate);
459
Mike Chan1dab2592010-06-22 11:26:45 -0700460static struct attribute *interactive_attributes[] = {
461 &go_maxspeed_load_attr.attr,
462 &min_sample_time_attr.attr,
Allen Martin5d14f982011-06-30 23:54:07 -0700463 &timer_rate_attr.attr,
Mike Chan1dab2592010-06-22 11:26:45 -0700464 NULL,
465};
466
467static struct attribute_group interactive_attr_group = {
468 .attrs = interactive_attributes,
469 .name = "interactive",
470};
471
Todd Poynor097d3962011-06-06 18:30:23 -0700472static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
Mike Chan1dab2592010-06-22 11:26:45 -0700473 unsigned int event)
474{
475 int rc;
Todd Poynor097d3962011-06-06 18:30:23 -0700476 unsigned int j;
477 struct cpufreq_interactive_cpuinfo *pcpu;
478 struct cpufreq_frequency_table *freq_table;
Mike Chan1dab2592010-06-22 11:26:45 -0700479
480 switch (event) {
481 case CPUFREQ_GOV_START:
Todd Poynor097d3962011-06-06 18:30:23 -0700482 if (!cpu_online(policy->cpu))
Mike Chan1dab2592010-06-22 11:26:45 -0700483 return -EINVAL;
484
Todd Poynor097d3962011-06-06 18:30:23 -0700485 freq_table =
486 cpufreq_frequency_get_table(policy->cpu);
487
488 for_each_cpu(j, policy->cpus) {
489 pcpu = &per_cpu(cpuinfo, j);
490 pcpu->policy = policy;
491 pcpu->target_freq = policy->cur;
492 pcpu->freq_table = freq_table;
493 pcpu->freq_change_time_in_idle =
494 get_cpu_idle_time_us(j,
Mike Chan1dab2592010-06-22 11:26:45 -0700495 &pcpu->freq_change_time);
Todd Poynor097d3962011-06-06 18:30:23 -0700496 pcpu->governor_enabled = 1;
497 smp_wmb();
498 }
499
Mike Chan1dab2592010-06-22 11:26:45 -0700500 /*
501 * Do not register the idle hook and create sysfs
502 * entries if we have already done so.
503 */
504 if (atomic_inc_return(&active_count) > 1)
505 return 0;
506
507 rc = sysfs_create_group(cpufreq_global_kobject,
508 &interactive_attr_group);
509 if (rc)
510 return rc;
511
Mike Chan1dab2592010-06-22 11:26:45 -0700512 break;
513
514 case CPUFREQ_GOV_STOP:
Todd Poynor097d3962011-06-06 18:30:23 -0700515 for_each_cpu(j, policy->cpus) {
516 pcpu = &per_cpu(cpuinfo, j);
517 pcpu->governor_enabled = 0;
518 smp_wmb();
519 del_timer_sync(&pcpu->cpu_timer);
Mike Chan1dab2592010-06-22 11:26:45 -0700520
Todd Poynor097d3962011-06-06 18:30:23 -0700521 /*
522 * Reset idle exit time since we may cancel the timer
523 * before it can run after the last idle exit time,
524 * to avoid tripping the check in idle exit for a timer
525 * that is trying to run.
526 */
527 pcpu->idle_exit_time = 0;
528 }
529
530 flush_work(&freq_scale_down_work);
Mike Chan1dab2592010-06-22 11:26:45 -0700531 if (atomic_dec_return(&active_count) > 0)
532 return 0;
533
534 sysfs_remove_group(cpufreq_global_kobject,
535 &interactive_attr_group);
536
Mike Chan1dab2592010-06-22 11:26:45 -0700537 break;
538
539 case CPUFREQ_GOV_LIMITS:
Todd Poynor097d3962011-06-06 18:30:23 -0700540 if (policy->max < policy->cur)
541 __cpufreq_driver_target(policy,
542 policy->max, CPUFREQ_RELATION_H);
543 else if (policy->min > policy->cur)
544 __cpufreq_driver_target(policy,
545 policy->min, CPUFREQ_RELATION_L);
Mike Chan1dab2592010-06-22 11:26:45 -0700546 break;
547 }
548 return 0;
549}
550
Allen Martin80e65d92011-06-28 09:58:39 -0700551static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
552 unsigned long val,
553 void *data)
554{
555 switch (val) {
556 case IDLE_START:
557 cpufreq_interactive_idle_start();
558 break;
559 case IDLE_END:
560 cpufreq_interactive_idle_end();
561 break;
562 }
563
564 return 0;
565}
566
567static struct notifier_block cpufreq_interactive_idle_nb = {
568 .notifier_call = cpufreq_interactive_idle_notifier,
569};
570
Mike Chan1dab2592010-06-22 11:26:45 -0700571static int __init cpufreq_interactive_init(void)
572{
573 unsigned int i;
574 struct cpufreq_interactive_cpuinfo *pcpu;
575 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
576
577 go_maxspeed_load = DEFAULT_GO_MAXSPEED_LOAD;
578 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Allen Martin5d14f982011-06-30 23:54:07 -0700579 timer_rate = DEFAULT_TIMER_RATE;
Mike Chan1dab2592010-06-22 11:26:45 -0700580
581 /* Initalize per-cpu timers */
582 for_each_possible_cpu(i) {
583 pcpu = &per_cpu(cpuinfo, i);
584 init_timer(&pcpu->cpu_timer);
585 pcpu->cpu_timer.function = cpufreq_interactive_timer;
586 pcpu->cpu_timer.data = i;
587 }
588
589 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
590 "kinteractiveup");
591 if (IS_ERR(up_task))
592 return PTR_ERR(up_task);
593
594 sched_setscheduler_nocheck(up_task, SCHED_FIFO, &param);
595 get_task_struct(up_task);
596
597 /* No rescuer thread, bind to CPU queuing the work for possibly
598 warm cache (probably doesn't matter much). */
599 down_wq = alloc_workqueue("knteractive_down", 0, 1);
600
601 if (! down_wq)
602 goto err_freeuptask;
603
604 INIT_WORK(&freq_scale_down_work,
605 cpufreq_interactive_freq_down);
606
607 spin_lock_init(&up_cpumask_lock);
608 spin_lock_init(&down_cpumask_lock);
609
Allen Martin80e65d92011-06-28 09:58:39 -0700610 idle_notifier_register(&cpufreq_interactive_idle_nb);
611
Mike Chan1dab2592010-06-22 11:26:45 -0700612 return cpufreq_register_governor(&cpufreq_gov_interactive);
613
614err_freeuptask:
615 put_task_struct(up_task);
616 return -ENOMEM;
617}
618
619#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
620fs_initcall(cpufreq_interactive_init);
621#else
622module_init(cpufreq_interactive_init);
623#endif
624
625static void __exit cpufreq_interactive_exit(void)
626{
627 cpufreq_unregister_governor(&cpufreq_gov_interactive);
628 kthread_stop(up_task);
629 put_task_struct(up_task);
630 destroy_workqueue(down_wq);
631}
632
633module_exit(cpufreq_interactive_exit);
634
635MODULE_AUTHOR("Mike Chan <mike@android.com>");
636MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
637 "Latency sensitive workloads");
638MODULE_LICENSE("GPL");