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