2 * drivers/cpufreq/cpufreq_interactive.c
4 * Copyright (C) 2010 Google, Inc.
5 * Copyright (C) 2012-2013 Freescale Semiconductor, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * Author: Mike Chan (mike@android.com)
20 #include <linux/cpu.h>
21 #include <linux/cpumask.h>
22 #include <linux/cpufreq.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 #include <linux/kernel_stat.h>
32 #include <linux/module.h>
33 #include <asm/cputime.h>
35 static atomic_t active_count = ATOMIC_INIT(0);
37 struct cpufreq_interactive_cpuinfo {
38 struct timer_list cpu_timer;
45 u64 freq_change_time_in_idle;
46 struct cpufreq_policy *policy;
47 struct cpufreq_frequency_table *freq_table;
48 unsigned int target_freq;
52 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
54 /* Workqueues handle frequency scaling */
55 static struct task_struct *up_task;
56 static struct workqueue_struct *down_wq;
57 static struct work_struct freq_scale_down_work;
58 static cpumask_t up_cpumask;
59 static spinlock_t up_cpumask_lock;
60 static cpumask_t down_cpumask;
61 static spinlock_t down_cpumask_lock;
62 static struct mutex set_speed_lock;
64 /* Hi speed to bump to from lo speed when load burst (default max) */
65 static u64 hispeed_freq;
67 /* Go to hi speed when CPU load at or above this value. */
68 #define DEFAULT_GO_HISPEED_LOAD 95
69 static unsigned long go_hispeed_load;
72 * The minimum amount of time to spend at a frequency before we can ramp down.
74 #define DEFAULT_MIN_SAMPLE_TIME (20 * USEC_PER_MSEC)
75 static unsigned long min_sample_time;
78 * The sample rate of the timer used to increase frequency
80 #define DEFAULT_TIMER_RATE (50 * USEC_PER_MSEC)
81 #define CPUFREQ_IRQ_LEN 60
82 #define CPUFREQ_NOTE_LEN 120
83 static unsigned long timer_rate;
85 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
88 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
91 struct cpufreq_governor cpufreq_gov_interactive = {
92 .name = "interactive",
93 .governor = cpufreq_governor_interactive,
94 .max_transition_latency = 10000000,
98 static void cpufreq_interactive_timer(unsigned long data)
100 unsigned int delta_idle;
101 unsigned int delta_time;
103 int load_since_change;
106 struct cpufreq_interactive_cpuinfo *pcpu =
107 &per_cpu(cpuinfo, data);
109 unsigned int new_freq;
115 if (!pcpu->governor_enabled)
119 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
120 * this lets idle exit know the current idle time sample has
121 * been processed, and idle exit can generate a new sample and
122 * re-arm the timer. This prevents a concurrent idle
123 * exit on that CPU from writing a new set of info at the same time
124 * the timer function runs (the timer function can't use that info
125 * until more time passes).
127 time_in_idle = pcpu->time_in_idle;
128 idle_exit_time = pcpu->idle_exit_time;
129 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
132 /* If we raced with cancelling a timer, skip. */
136 delta_idle = (unsigned int)(now_idle - time_in_idle);
137 delta_time = (unsigned int)(pcpu->timer_run_time - idle_exit_time);
140 * If timer ran less than 1ms after short-term sample started, retry.
142 if (delta_time < 1000)
145 if (delta_idle > delta_time)
148 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
150 delta_idle = (unsigned int)(now_idle - pcpu->freq_change_time_in_idle);
151 delta_time = (unsigned int)(pcpu->timer_run_time -
152 pcpu->freq_change_time);
154 if ((delta_time == 0) || (delta_idle > delta_time))
155 load_since_change = 0;
158 100 * (delta_time - delta_idle) / delta_time;
161 * Choose greater of short-term load (since last idle timer
162 * started or timer function re-armed itself) or long-term load
163 * (since last frequency change).
165 if (load_since_change > cpu_load)
166 cpu_load = load_since_change;
168 if (cpu_load >= go_hispeed_load) {
169 if (pcpu->policy->cur == pcpu->policy->min)
170 new_freq = hispeed_freq;
172 new_freq = pcpu->policy->max * cpu_load / 100;
174 new_freq = pcpu->policy->cur * cpu_load / 100;
177 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
178 new_freq, CPUFREQ_RELATION_H,
180 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
185 new_freq = pcpu->freq_table[index].frequency;
186 if (pcpu->target_freq == new_freq)
187 goto rearm_if_notmax;
190 * Do not scale down unless we have been at this frequency for the
191 * minimum sample time.
193 if (new_freq < pcpu->target_freq) {
194 if ((pcpu->timer_run_time - pcpu->freq_change_time)
199 if (new_freq < pcpu->target_freq) {
200 pcpu->target_freq = new_freq;
201 spin_lock_irqsave(&down_cpumask_lock, flags);
202 cpumask_set_cpu(data, &down_cpumask);
203 spin_unlock_irqrestore(&down_cpumask_lock, flags);
204 queue_work(down_wq, &freq_scale_down_work);
206 pcpu->target_freq = new_freq;
207 spin_lock_irqsave(&up_cpumask_lock, flags);
208 cpumask_set_cpu(data, &up_cpumask);
209 spin_unlock_irqrestore(&up_cpumask_lock, flags);
210 wake_up_process(up_task);
215 * Already set max speed and don't see a need to change that,
216 * wait until next idle to re-evaluate, don't need timer.
218 if (pcpu->target_freq == pcpu->policy->max)
222 if (!timer_pending(&pcpu->cpu_timer)) {
224 * If already at min: if that CPU is idle, don't set timer.
225 * Else cancel the timer if that CPU goes idle. We don't
226 * need to re-evaluate speed until the next idle exit.
228 if (pcpu->target_freq == pcpu->policy->min) {
234 pcpu->timer_idlecancel = 1;
237 pcpu->time_in_idle = get_cpu_idle_time_us(
238 data, &pcpu->idle_exit_time);
239 mod_timer(&pcpu->cpu_timer,
240 jiffies + usecs_to_jiffies(timer_rate));
247 static void cpufreq_interactive_idle_start(void)
249 struct cpufreq_interactive_cpuinfo *pcpu =
250 &per_cpu(cpuinfo, smp_processor_id());
255 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)
334 struct cpufreq_interactive_cpuinfo *pcpu;
337 set_current_state(TASK_INTERRUPTIBLE);
338 spin_lock_irqsave(&up_cpumask_lock, flags);
340 if (cpumask_empty(&up_cpumask)) {
341 spin_unlock_irqrestore(&up_cpumask_lock, flags);
344 if (kthread_should_stop())
347 spin_lock_irqsave(&up_cpumask_lock, flags);
350 set_current_state(TASK_RUNNING);
351 cpumask_clear(&up_cpumask);
352 spin_unlock_irqrestore(&up_cpumask_lock, flags);
354 for_each_online_cpu(cpu) {
356 unsigned int max_freq = 0;
358 pcpu = &per_cpu(cpuinfo, cpu);
361 if (!pcpu->governor_enabled)
364 mutex_lock(&set_speed_lock);
366 for_each_online_cpu(j) {
367 struct cpufreq_interactive_cpuinfo *pjcpu =
368 &per_cpu(cpuinfo, j);
369 if (pjcpu->target_freq > max_freq)
370 max_freq = pjcpu->target_freq;
372 if (max_freq != pcpu->policy->cur)
373 __cpufreq_driver_target(pcpu->policy,
376 mutex_unlock(&set_speed_lock);
378 pcpu->freq_change_time_in_idle =
379 get_cpu_idle_time_us(cpu,
380 &pcpu->freq_change_time);
387 static void cpufreq_interactive_freq_down(struct work_struct *work)
391 struct cpufreq_interactive_cpuinfo *pcpu;
393 spin_lock_irqsave(&down_cpumask_lock, flags);
394 cpumask_clear(&down_cpumask);
395 spin_unlock_irqrestore(&down_cpumask_lock, flags);
397 for_each_online_cpu(cpu) {
399 unsigned int max_freq = 0;
401 pcpu = &per_cpu(cpuinfo, cpu);
404 if (!pcpu->governor_enabled)
407 mutex_lock(&set_speed_lock);
409 for_each_online_cpu(j) {
410 struct cpufreq_interactive_cpuinfo *pjcpu =
411 &per_cpu(cpuinfo, j);
413 if (pjcpu->target_freq > max_freq)
414 max_freq = pjcpu->target_freq;
417 if (max_freq != pcpu->policy->cur)
418 __cpufreq_driver_target(pcpu->policy, max_freq,
421 mutex_unlock(&set_speed_lock);
422 pcpu->freq_change_time_in_idle =
423 get_cpu_idle_time_us(cpu,
424 &pcpu->freq_change_time);
428 static ssize_t show_hispeed_freq(struct kobject *kobj,
429 struct attribute *attr, char *buf)
431 return sprintf(buf, "%llu\n", hispeed_freq);
434 static ssize_t store_hispeed_freq(struct kobject *kobj,
435 struct attribute *attr, const char *buf,
441 ret = strict_strtoull(buf, 0, &val);
448 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
449 show_hispeed_freq, store_hispeed_freq);
452 static ssize_t show_go_hispeed_load(struct kobject *kobj,
453 struct attribute *attr, char *buf)
455 return sprintf(buf, "%lu\n", go_hispeed_load);
458 static ssize_t store_go_hispeed_load(struct kobject *kobj,
459 struct attribute *attr, const char *buf, size_t count)
464 ret = strict_strtoul(buf, 0, &val);
467 go_hispeed_load = val;
471 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
472 show_go_hispeed_load, store_go_hispeed_load);
474 static ssize_t show_min_sample_time(struct kobject *kobj,
475 struct attribute *attr, char *buf)
477 return sprintf(buf, "%lu\n", min_sample_time);
480 static ssize_t store_min_sample_time(struct kobject *kobj,
481 struct attribute *attr, const char *buf, size_t count)
486 ret = strict_strtoul(buf, 0, &val);
489 min_sample_time = val;
493 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
494 show_min_sample_time, store_min_sample_time);
496 static ssize_t show_timer_rate(struct kobject *kobj,
497 struct attribute *attr, char *buf)
499 return sprintf(buf, "%lu\n", timer_rate);
502 static ssize_t store_timer_rate(struct kobject *kobj,
503 struct attribute *attr, const char *buf, size_t count)
508 ret = strict_strtoul(buf, 0, &val);
515 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
516 show_timer_rate, store_timer_rate);
518 static struct attribute *interactive_attributes[] = {
519 &hispeed_freq_attr.attr,
520 &go_hispeed_load_attr.attr,
521 &min_sample_time_attr.attr,
522 &timer_rate_attr.attr,
526 static struct attribute_group interactive_attr_group = {
527 .attrs = interactive_attributes,
528 .name = "interactive",
531 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
536 struct cpufreq_interactive_cpuinfo *pcpu;
537 struct cpufreq_frequency_table *freq_table;
540 case CPUFREQ_GOV_START:
541 if (!cpu_online(policy->cpu))
545 cpufreq_frequency_get_table(policy->cpu);
547 for_each_cpu(j, policy->cpus) {
548 pcpu = &per_cpu(cpuinfo, j);
549 pcpu->policy = policy;
551 pcpu->target_freq = policy->min;
553 pcpu->target_freq = policy->cur;
555 pcpu->freq_table = freq_table;
556 pcpu->freq_change_time_in_idle =
557 get_cpu_idle_time_us(j,
558 &pcpu->freq_change_time);
559 pcpu->governor_enabled = 1;
564 hispeed_freq = policy->max;
567 * Do not register the idle hook and create sysfs
568 * entries if we have already done so.
570 if (atomic_inc_return(&active_count) > 1)
573 rc = sysfs_create_group(cpufreq_global_kobject,
574 &interactive_attr_group);
580 case CPUFREQ_GOV_STOP:
581 for_each_cpu(j, policy->cpus) {
582 pcpu = &per_cpu(cpuinfo, j);
583 pcpu->governor_enabled = 0;
585 del_timer_sync(&pcpu->cpu_timer);
588 * Reset idle exit time since we may cancel the timer
589 * before it can run after the last idle exit time,
590 * to avoid tripping the check in idle exit for a timer
591 * that is trying to run.
593 pcpu->idle_exit_time = 0;
596 flush_work(&freq_scale_down_work);
597 if (atomic_dec_return(&active_count) > 0)
600 sysfs_remove_group(cpufreq_global_kobject,
601 &interactive_attr_group);
605 case CPUFREQ_GOV_LIMITS:
606 if (policy->max < policy->cur)
607 __cpufreq_driver_target(policy,
608 policy->max, CPUFREQ_RELATION_H);
609 else if (policy->min > policy->cur)
610 __cpufreq_driver_target(policy,
611 policy->min, CPUFREQ_RELATION_L);
617 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
623 cpufreq_interactive_idle_start();
626 cpufreq_interactive_idle_end();
633 static struct notifier_block cpufreq_interactive_idle_nb = {
634 .notifier_call = cpufreq_interactive_idle_notifier,
637 static int __init cpufreq_interactive_init(void)
640 struct cpufreq_interactive_cpuinfo *pcpu;
641 struct sched_param param = { .sched_priority = 99 };
643 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
644 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
645 timer_rate = DEFAULT_TIMER_RATE;
647 /* Initalize per-cpu timers */
648 for_each_possible_cpu(i) {
649 pcpu = &per_cpu(cpuinfo, i);
650 init_timer(&pcpu->cpu_timer);
651 pcpu->cpu_timer.function = cpufreq_interactive_timer;
652 pcpu->cpu_timer.data = i;
655 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
658 return PTR_ERR(up_task);
660 sched_setscheduler_nocheck(up_task, SCHED_FIFO, ¶m);
661 get_task_struct(up_task);
663 /* No rescuer thread, bind to CPU queuing the work for possibly
664 warm cache (probably doesn't matter much). */
665 down_wq = alloc_workqueue("kinteractive_down", 0, 1);
670 INIT_WORK(&freq_scale_down_work,
671 cpufreq_interactive_freq_down);
673 spin_lock_init(&up_cpumask_lock);
674 spin_lock_init(&down_cpumask_lock);
675 mutex_init(&set_speed_lock);
677 idle_notifier_register(&cpufreq_interactive_idle_nb);
679 return cpufreq_register_governor(&cpufreq_gov_interactive);
682 put_task_struct(up_task);
686 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
687 late_initcall(cpufreq_interactive_init);
689 module_init(cpufreq_interactive_init);
692 static void __exit cpufreq_interactive_exit(void)
694 cpufreq_unregister_governor(&cpufreq_gov_interactive);
695 kthread_stop(up_task);
696 put_task_struct(up_task);
697 destroy_workqueue(down_wq);
700 module_exit(cpufreq_interactive_exit);
702 MODULE_AUTHOR("Mike Chan <mike@android.com>");
703 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
704 "Latency sensitive workloads");
705 MODULE_LICENSE("GPL");