2 * drivers/cpufreq/cpufreq_interactive.c
4 * Copyright (C) 2010 Google, Inc.
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.
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.
15 * Author: Mike Chan (mike@android.com)
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/time.h>
26 #include <linux/timer.h>
27 #include <linux/workqueue.h>
28 #include <linux/kthread.h>
29 #include <linux/mutex.h>
31 #include <asm/cputime.h>
33 static atomic_t active_count = ATOMIC_INIT(0);
35 struct cpufreq_interactive_cpuinfo {
36 struct timer_list cpu_timer;
43 u64 freq_change_time_in_idle;
44 struct cpufreq_policy *policy;
45 struct cpufreq_frequency_table *freq_table;
46 unsigned int target_freq;
50 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
52 /* Workqueues handle frequency scaling */
53 static struct task_struct *up_task;
54 static struct workqueue_struct *down_wq;
55 static struct work_struct freq_scale_down_work;
56 static cpumask_t up_cpumask;
57 static spinlock_t up_cpumask_lock;
58 static cpumask_t down_cpumask;
59 static spinlock_t down_cpumask_lock;
60 static struct mutex set_speed_lock;
62 /* Hi speed to bump to from lo speed when load burst (default max) */
63 static u64 hispeed_freq;
65 /* Go to hi speed when CPU load at or above this value. */
66 #define DEFAULT_GO_HISPEED_LOAD 95
67 static unsigned long go_hispeed_load;
70 * The minimum amount of time to spend at a frequency before we can ramp down.
72 #define DEFAULT_MIN_SAMPLE_TIME 20 * USEC_PER_MSEC
73 static unsigned long min_sample_time;
76 * The sample rate of the timer used to increase frequency
78 #define DEFAULT_TIMER_RATE 20 * USEC_PER_MSEC
79 static unsigned long timer_rate;
81 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
84 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
87 struct cpufreq_governor cpufreq_gov_interactive = {
88 .name = "interactive",
89 .governor = cpufreq_governor_interactive,
90 .max_transition_latency = 10000000,
94 static void cpufreq_interactive_timer(unsigned long data)
96 unsigned int delta_idle;
97 unsigned int delta_time;
99 int load_since_change;
102 struct cpufreq_interactive_cpuinfo *pcpu =
103 &per_cpu(cpuinfo, data);
105 unsigned int new_freq;
111 if (!pcpu->governor_enabled)
115 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
116 * this lets idle exit know the current idle time sample has
117 * been processed, and idle exit can generate a new sample and
118 * re-arm the timer. This prevents a concurrent idle
119 * exit on that CPU from writing a new set of info at the same time
120 * the timer function runs (the timer function can't use that info
121 * until more time passes).
123 time_in_idle = pcpu->time_in_idle;
124 idle_exit_time = pcpu->idle_exit_time;
125 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
128 /* If we raced with cancelling a timer, skip. */
132 delta_idle = (unsigned int) cputime64_sub(now_idle, time_in_idle);
133 delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time,
137 * If timer ran less than 1ms after short-term sample started, retry.
139 if (delta_time < 1000)
142 if (delta_idle > delta_time)
145 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
147 delta_idle = (unsigned int) cputime64_sub(now_idle,
148 pcpu->freq_change_time_in_idle);
149 delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time,
150 pcpu->freq_change_time);
152 if ((delta_time == 0) || (delta_idle > delta_time))
153 load_since_change = 0;
156 100 * (delta_time - delta_idle) / delta_time;
159 * Choose greater of short-term load (since last idle timer
160 * started or timer function re-armed itself) or long-term load
161 * (since last frequency change).
163 if (load_since_change > cpu_load)
164 cpu_load = load_since_change;
166 if (cpu_load >= go_hispeed_load) {
167 if (pcpu->policy->cur == pcpu->policy->min)
168 new_freq = hispeed_freq;
170 new_freq = pcpu->policy->max * cpu_load / 100;
172 new_freq = pcpu->policy->cur * cpu_load / 100;
175 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
176 new_freq, CPUFREQ_RELATION_H,
178 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
183 new_freq = pcpu->freq_table[index].frequency;
185 if (pcpu->target_freq == new_freq)
186 goto rearm_if_notmax;
189 * Do not scale down unless we have been at this frequency for the
190 * minimum sample time.
192 if (new_freq < pcpu->target_freq) {
193 if (cputime64_sub(pcpu->timer_run_time, pcpu->freq_change_time)
198 if (new_freq < pcpu->target_freq) {
199 pcpu->target_freq = new_freq;
200 spin_lock_irqsave(&down_cpumask_lock, flags);
201 cpumask_set_cpu(data, &down_cpumask);
202 spin_unlock_irqrestore(&down_cpumask_lock, flags);
203 queue_work(down_wq, &freq_scale_down_work);
205 pcpu->target_freq = new_freq;
206 spin_lock_irqsave(&up_cpumask_lock, flags);
207 cpumask_set_cpu(data, &up_cpumask);
208 spin_unlock_irqrestore(&up_cpumask_lock, flags);
209 wake_up_process(up_task);
214 * Already set max speed and don't see a need to change that,
215 * wait until next idle to re-evaluate, don't need timer.
217 if (pcpu->target_freq == pcpu->policy->max)
221 if (!timer_pending(&pcpu->cpu_timer)) {
223 * If already at min: if that CPU is idle, don't set timer.
224 * Else cancel the timer if that CPU goes idle. We don't
225 * need to re-evaluate speed until the next idle exit.
227 if (pcpu->target_freq == pcpu->policy->min) {
233 pcpu->timer_idlecancel = 1;
236 pcpu->time_in_idle = get_cpu_idle_time_us(
237 data, &pcpu->idle_exit_time);
238 mod_timer(&pcpu->cpu_timer,
239 jiffies + usecs_to_jiffies(timer_rate));
246 static void cpufreq_interactive_idle_start(void)
248 struct cpufreq_interactive_cpuinfo *pcpu =
249 &per_cpu(cpuinfo, smp_processor_id());
252 if (!pcpu->governor_enabled)
257 pending = timer_pending(&pcpu->cpu_timer);
259 if (pcpu->target_freq != pcpu->policy->min) {
262 * Entering idle while not at lowest speed. On some
263 * platforms this can hold the other CPU(s) at that speed
264 * even though the CPU is idle. Set a timer to re-evaluate
265 * speed so this idle CPU doesn't hold the other CPUs above
266 * min indefinitely. This should probably be a quirk of
267 * the CPUFreq driver.
270 pcpu->time_in_idle = get_cpu_idle_time_us(
271 smp_processor_id(), &pcpu->idle_exit_time);
272 pcpu->timer_idlecancel = 0;
273 mod_timer(&pcpu->cpu_timer,
274 jiffies + usecs_to_jiffies(timer_rate));
279 * If at min speed and entering idle after load has
280 * already been evaluated, and a timer has been set just in
281 * case the CPU suddenly goes busy, cancel that timer. The
282 * CPU didn't go busy; we'll recheck things upon idle exit.
284 if (pending && pcpu->timer_idlecancel) {
285 del_timer(&pcpu->cpu_timer);
287 * Ensure last timer run time is after current idle
288 * sample start time, so next idle exit will always
289 * start a new idle sampling period.
291 pcpu->idle_exit_time = 0;
292 pcpu->timer_idlecancel = 0;
298 static void cpufreq_interactive_idle_end(void)
300 struct cpufreq_interactive_cpuinfo *pcpu =
301 &per_cpu(cpuinfo, smp_processor_id());
307 * Arm the timer for 1-2 ticks later if not already, and if the timer
308 * function has already processed the previous load sampling
309 * interval. (If the timer is not pending but has not processed
310 * the previous interval, it is probably racing with us on another
311 * CPU. Let it compute load based on the previous sample and then
312 * re-arm the timer for another interval when it's done, rather
313 * than updating the interval start time to be "now", which doesn't
314 * give the timer function enough time to make a decision on this
317 if (timer_pending(&pcpu->cpu_timer) == 0 &&
318 pcpu->timer_run_time >= pcpu->idle_exit_time &&
319 pcpu->governor_enabled) {
321 get_cpu_idle_time_us(smp_processor_id(),
322 &pcpu->idle_exit_time);
323 pcpu->timer_idlecancel = 0;
324 mod_timer(&pcpu->cpu_timer,
325 jiffies + usecs_to_jiffies(timer_rate));
330 static int cpufreq_interactive_up_task(void *data)
335 struct cpufreq_interactive_cpuinfo *pcpu;
338 set_current_state(TASK_INTERRUPTIBLE);
339 spin_lock_irqsave(&up_cpumask_lock, flags);
341 if (cpumask_empty(&up_cpumask)) {
342 spin_unlock_irqrestore(&up_cpumask_lock, flags);
345 if (kthread_should_stop())
348 spin_lock_irqsave(&up_cpumask_lock, flags);
351 set_current_state(TASK_RUNNING);
352 tmp_mask = up_cpumask;
353 cpumask_clear(&up_cpumask);
354 spin_unlock_irqrestore(&up_cpumask_lock, flags);
356 for_each_cpu(cpu, &tmp_mask) {
358 unsigned int max_freq = 0;
360 pcpu = &per_cpu(cpuinfo, cpu);
363 if (!pcpu->governor_enabled)
366 mutex_lock(&set_speed_lock);
368 for_each_cpu(j, pcpu->policy->cpus) {
369 struct cpufreq_interactive_cpuinfo *pjcpu =
370 &per_cpu(cpuinfo, j);
372 if (pjcpu->target_freq > max_freq)
373 max_freq = pjcpu->target_freq;
376 if (max_freq != pcpu->policy->cur)
377 __cpufreq_driver_target(pcpu->policy,
380 mutex_unlock(&set_speed_lock);
382 pcpu->freq_change_time_in_idle =
383 get_cpu_idle_time_us(cpu,
384 &pcpu->freq_change_time);
391 static void cpufreq_interactive_freq_down(struct work_struct *work)
396 struct cpufreq_interactive_cpuinfo *pcpu;
398 spin_lock_irqsave(&down_cpumask_lock, flags);
399 tmp_mask = down_cpumask;
400 cpumask_clear(&down_cpumask);
401 spin_unlock_irqrestore(&down_cpumask_lock, flags);
403 for_each_cpu(cpu, &tmp_mask) {
405 unsigned int max_freq = 0;
407 pcpu = &per_cpu(cpuinfo, cpu);
410 if (!pcpu->governor_enabled)
413 mutex_lock(&set_speed_lock);
415 for_each_cpu(j, pcpu->policy->cpus) {
416 struct cpufreq_interactive_cpuinfo *pjcpu =
417 &per_cpu(cpuinfo, j);
419 if (pjcpu->target_freq > max_freq)
420 max_freq = pjcpu->target_freq;
423 if (max_freq != pcpu->policy->cur)
424 __cpufreq_driver_target(pcpu->policy, max_freq,
427 mutex_unlock(&set_speed_lock);
428 pcpu->freq_change_time_in_idle =
429 get_cpu_idle_time_us(cpu,
430 &pcpu->freq_change_time);
434 static ssize_t show_hispeed_freq(struct kobject *kobj,
435 struct attribute *attr, char *buf)
437 return sprintf(buf, "%llu\n", hispeed_freq);
440 static ssize_t store_hispeed_freq(struct kobject *kobj,
441 struct attribute *attr, const char *buf,
447 ret = strict_strtoull(buf, 0, &val);
454 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
455 show_hispeed_freq, store_hispeed_freq);
458 static ssize_t show_go_hispeed_load(struct kobject *kobj,
459 struct attribute *attr, char *buf)
461 return sprintf(buf, "%lu\n", go_hispeed_load);
464 static ssize_t store_go_hispeed_load(struct kobject *kobj,
465 struct attribute *attr, const char *buf, size_t count)
470 ret = strict_strtoul(buf, 0, &val);
473 go_hispeed_load = val;
477 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
478 show_go_hispeed_load, store_go_hispeed_load);
480 static ssize_t show_min_sample_time(struct kobject *kobj,
481 struct attribute *attr, char *buf)
483 return sprintf(buf, "%lu\n", min_sample_time);
486 static ssize_t store_min_sample_time(struct kobject *kobj,
487 struct attribute *attr, const char *buf, size_t count)
492 ret = strict_strtoul(buf, 0, &val);
495 min_sample_time = val;
499 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
500 show_min_sample_time, store_min_sample_time);
502 static ssize_t show_timer_rate(struct kobject *kobj,
503 struct attribute *attr, char *buf)
505 return sprintf(buf, "%lu\n", timer_rate);
508 static ssize_t store_timer_rate(struct kobject *kobj,
509 struct attribute *attr, const char *buf, size_t count)
514 ret = strict_strtoul(buf, 0, &val);
521 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
522 show_timer_rate, store_timer_rate);
524 static struct attribute *interactive_attributes[] = {
525 &hispeed_freq_attr.attr,
526 &go_hispeed_load_attr.attr,
527 &min_sample_time_attr.attr,
528 &timer_rate_attr.attr,
532 static struct attribute_group interactive_attr_group = {
533 .attrs = interactive_attributes,
534 .name = "interactive",
537 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
542 struct cpufreq_interactive_cpuinfo *pcpu;
543 struct cpufreq_frequency_table *freq_table;
546 case CPUFREQ_GOV_START:
547 if (!cpu_online(policy->cpu))
551 cpufreq_frequency_get_table(policy->cpu);
553 for_each_cpu(j, policy->cpus) {
554 pcpu = &per_cpu(cpuinfo, j);
555 pcpu->policy = policy;
556 pcpu->target_freq = policy->cur;
557 pcpu->freq_table = freq_table;
558 pcpu->freq_change_time_in_idle =
559 get_cpu_idle_time_us(j,
560 &pcpu->freq_change_time);
561 pcpu->governor_enabled = 1;
566 hispeed_freq = policy->max;
569 * Do not register the idle hook and create sysfs
570 * entries if we have already done so.
572 if (atomic_inc_return(&active_count) > 1)
575 rc = sysfs_create_group(cpufreq_global_kobject,
576 &interactive_attr_group);
582 case CPUFREQ_GOV_STOP:
583 for_each_cpu(j, policy->cpus) {
584 pcpu = &per_cpu(cpuinfo, j);
585 pcpu->governor_enabled = 0;
587 del_timer_sync(&pcpu->cpu_timer);
590 * Reset idle exit time since we may cancel the timer
591 * before it can run after the last idle exit time,
592 * to avoid tripping the check in idle exit for a timer
593 * that is trying to run.
595 pcpu->idle_exit_time = 0;
598 flush_work(&freq_scale_down_work);
599 if (atomic_dec_return(&active_count) > 0)
602 sysfs_remove_group(cpufreq_global_kobject,
603 &interactive_attr_group);
607 case CPUFREQ_GOV_LIMITS:
608 if (policy->max < policy->cur)
609 __cpufreq_driver_target(policy,
610 policy->max, CPUFREQ_RELATION_H);
611 else if (policy->min > policy->cur)
612 __cpufreq_driver_target(policy,
613 policy->min, CPUFREQ_RELATION_L);
619 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
625 cpufreq_interactive_idle_start();
628 cpufreq_interactive_idle_end();
635 static struct notifier_block cpufreq_interactive_idle_nb = {
636 .notifier_call = cpufreq_interactive_idle_notifier,
639 static int __init cpufreq_interactive_init(void)
642 struct cpufreq_interactive_cpuinfo *pcpu;
643 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
645 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
646 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
647 timer_rate = DEFAULT_TIMER_RATE;
649 /* Initalize per-cpu timers */
650 for_each_possible_cpu(i) {
651 pcpu = &per_cpu(cpuinfo, i);
652 init_timer(&pcpu->cpu_timer);
653 pcpu->cpu_timer.function = cpufreq_interactive_timer;
654 pcpu->cpu_timer.data = i;
657 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
660 return PTR_ERR(up_task);
662 sched_setscheduler_nocheck(up_task, SCHED_FIFO, ¶m);
663 get_task_struct(up_task);
665 /* No rescuer thread, bind to CPU queuing the work for possibly
666 warm cache (probably doesn't matter much). */
667 down_wq = alloc_workqueue("knteractive_down", 0, 1);
672 INIT_WORK(&freq_scale_down_work,
673 cpufreq_interactive_freq_down);
675 spin_lock_init(&up_cpumask_lock);
676 spin_lock_init(&down_cpumask_lock);
677 mutex_init(&set_speed_lock);
679 idle_notifier_register(&cpufreq_interactive_idle_nb);
681 return cpufreq_register_governor(&cpufreq_gov_interactive);
684 put_task_struct(up_task);
688 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
689 fs_initcall(cpufreq_interactive_init);
691 module_init(cpufreq_interactive_init);
694 static void __exit cpufreq_interactive_exit(void)
696 cpufreq_unregister_governor(&cpufreq_gov_interactive);
697 kthread_stop(up_task);
698 put_task_struct(up_task);
699 destroy_workqueue(down_wq);
702 module_exit(cpufreq_interactive_exit);
704 MODULE_AUTHOR("Mike Chan <mike@android.com>");
705 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
706 "Latency sensitive workloads");
707 MODULE_LICENSE("GPL");