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/module.h>
23 #include <linux/mutex.h>
24 #include <linux/sched.h>
25 #include <linux/sched/rt.h>
26 #include <linux/tick.h>
27 #include <linux/time.h>
28 #include <linux/timer.h>
29 #include <linux/workqueue.h>
30 #include <linux/kthread.h>
31 #include <linux/mutex.h>
32 #include <linux/slab.h>
33 #include <asm/cputime.h>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/cpufreq_interactive.h>
38 static atomic_t active_count = ATOMIC_INIT(0);
40 struct cpufreq_interactive_cpuinfo {
41 struct timer_list cpu_timer;
48 u64 target_set_time_in_idle;
49 struct cpufreq_policy *policy;
50 struct cpufreq_frequency_table *freq_table;
51 unsigned int target_freq;
52 unsigned int floor_freq;
53 u64 floor_validate_time;
54 u64 hispeed_validate_time;
58 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
60 /* realtime thread handles frequency scaling */
61 static struct task_struct *speedchange_task;
62 static cpumask_t speedchange_cpumask;
63 static spinlock_t speedchange_cpumask_lock;
65 /* Hi speed to bump to from lo speed when load burst (default max) */
66 static u64 hispeed_freq;
68 /* Go to hi speed when CPU load at or above this value. */
69 #define DEFAULT_GO_HISPEED_LOAD 85
70 static unsigned long go_hispeed_load;
73 * The minimum amount of time to spend at a frequency before we can ramp down.
75 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
76 static unsigned long min_sample_time;
79 * The sample rate of the timer used to increase frequency
81 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
82 static unsigned long timer_rate;
85 * Wait this long before raising speed above hispeed, by default a single
88 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
89 static unsigned long above_hispeed_delay_val;
92 * Non-zero means longer-term speed boost active.
97 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
100 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
103 struct cpufreq_governor cpufreq_gov_interactive = {
104 .name = "interactive",
105 .governor = cpufreq_governor_interactive,
106 .max_transition_latency = 10000000,
107 .owner = THIS_MODULE,
110 static void cpufreq_interactive_timer(unsigned long data)
112 unsigned int delta_idle;
113 unsigned int delta_time;
115 int load_since_change;
118 struct cpufreq_interactive_cpuinfo *pcpu =
119 &per_cpu(cpuinfo, data);
121 unsigned int new_freq;
127 if (!pcpu->governor_enabled)
131 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
132 * this lets idle exit know the current idle time sample has
133 * been processed, and idle exit can generate a new sample and
134 * re-arm the timer. This prevents a concurrent idle
135 * exit on that CPU from writing a new set of info at the same time
136 * the timer function runs (the timer function can't use that info
137 * until more time passes).
139 time_in_idle = pcpu->time_in_idle;
140 idle_exit_time = pcpu->idle_exit_time;
141 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
144 /* If we raced with cancelling a timer, skip. */
148 delta_idle = (unsigned int)(now_idle - time_in_idle);
149 delta_time = (unsigned int)(pcpu->timer_run_time - idle_exit_time);
152 * If timer ran less than 1ms after short-term sample started, retry.
154 if (delta_time < 1000)
157 if (delta_idle > delta_time)
160 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
162 delta_idle = (unsigned int)(now_idle - pcpu->target_set_time_in_idle);
163 delta_time = (unsigned int)(pcpu->timer_run_time -
164 pcpu->target_set_time);
166 if ((delta_time == 0) || (delta_idle > delta_time))
167 load_since_change = 0;
170 100 * (delta_time - delta_idle) / delta_time;
173 * Choose greater of short-term load (since last idle timer
174 * started or timer function re-armed itself) or long-term load
175 * (since last frequency change).
177 if (load_since_change > cpu_load)
178 cpu_load = load_since_change;
180 if (cpu_load >= go_hispeed_load || boost_val) {
181 if (pcpu->target_freq <= pcpu->policy->min) {
182 new_freq = hispeed_freq;
184 new_freq = pcpu->policy->max * cpu_load / 100;
186 if (new_freq < hispeed_freq)
187 new_freq = hispeed_freq;
189 if (pcpu->target_freq == hispeed_freq &&
190 new_freq > hispeed_freq &&
191 pcpu->timer_run_time - pcpu->hispeed_validate_time
192 < above_hispeed_delay_val) {
193 trace_cpufreq_interactive_notyet(data, cpu_load,
200 new_freq = pcpu->policy->max * cpu_load / 100;
203 if (new_freq <= hispeed_freq)
204 pcpu->hispeed_validate_time = pcpu->timer_run_time;
206 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
207 new_freq, CPUFREQ_RELATION_H,
209 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
214 new_freq = pcpu->freq_table[index].frequency;
217 * Do not scale below floor_freq unless we have been at or above the
218 * floor frequency for the minimum sample time since last validated.
220 if (new_freq < pcpu->floor_freq) {
221 if (pcpu->timer_run_time - pcpu->floor_validate_time
223 trace_cpufreq_interactive_notyet(data, cpu_load,
224 pcpu->target_freq, new_freq);
229 pcpu->floor_freq = new_freq;
230 pcpu->floor_validate_time = pcpu->timer_run_time;
232 if (pcpu->target_freq == new_freq) {
233 trace_cpufreq_interactive_already(data, cpu_load,
234 pcpu->target_freq, new_freq);
235 goto rearm_if_notmax;
238 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
240 pcpu->target_set_time_in_idle = now_idle;
241 pcpu->target_set_time = pcpu->timer_run_time;
243 pcpu->target_freq = new_freq;
244 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
245 cpumask_set_cpu(data, &speedchange_cpumask);
246 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
247 wake_up_process(speedchange_task);
251 * Already set max speed and don't see a need to change that,
252 * wait until next idle to re-evaluate, don't need timer.
254 if (pcpu->target_freq == pcpu->policy->max)
258 if (!timer_pending(&pcpu->cpu_timer)) {
260 * If already at min: if that CPU is idle, don't set timer.
261 * Else cancel the timer if that CPU goes idle. We don't
262 * need to re-evaluate speed until the next idle exit.
264 if (pcpu->target_freq == pcpu->policy->min) {
270 pcpu->timer_idlecancel = 1;
273 pcpu->time_in_idle = get_cpu_idle_time_us(
274 data, &pcpu->idle_exit_time);
275 mod_timer(&pcpu->cpu_timer,
276 jiffies + usecs_to_jiffies(timer_rate));
283 static void cpufreq_interactive_idle_start(void)
285 struct cpufreq_interactive_cpuinfo *pcpu =
286 &per_cpu(cpuinfo, smp_processor_id());
289 if (!pcpu->governor_enabled)
294 pending = timer_pending(&pcpu->cpu_timer);
296 if (pcpu->target_freq != pcpu->policy->min) {
299 * Entering idle while not at lowest speed. On some
300 * platforms this can hold the other CPU(s) at that speed
301 * even though the CPU is idle. Set a timer to re-evaluate
302 * speed so this idle CPU doesn't hold the other CPUs above
303 * min indefinitely. This should probably be a quirk of
304 * the CPUFreq driver.
307 pcpu->time_in_idle = get_cpu_idle_time_us(
308 smp_processor_id(), &pcpu->idle_exit_time);
309 pcpu->timer_idlecancel = 0;
310 mod_timer(&pcpu->cpu_timer,
311 jiffies + usecs_to_jiffies(timer_rate));
316 * If at min speed and entering idle after load has
317 * already been evaluated, and a timer has been set just in
318 * case the CPU suddenly goes busy, cancel that timer. The
319 * CPU didn't go busy; we'll recheck things upon idle exit.
321 if (pending && pcpu->timer_idlecancel) {
322 del_timer(&pcpu->cpu_timer);
324 * Ensure last timer run time is after current idle
325 * sample start time, so next idle exit will always
326 * start a new idle sampling period.
328 pcpu->idle_exit_time = 0;
329 pcpu->timer_idlecancel = 0;
335 static void cpufreq_interactive_idle_end(void)
337 struct cpufreq_interactive_cpuinfo *pcpu =
338 &per_cpu(cpuinfo, smp_processor_id());
340 if (!pcpu->governor_enabled)
347 * Arm the timer for 1-2 ticks later if not already, and if the timer
348 * function has already processed the previous load sampling
349 * interval. (If the timer is not pending but has not processed
350 * the previous interval, it is probably racing with us on another
351 * CPU. Let it compute load based on the previous sample and then
352 * re-arm the timer for another interval when it's done, rather
353 * than updating the interval start time to be "now", which doesn't
354 * give the timer function enough time to make a decision on this
357 if (timer_pending(&pcpu->cpu_timer) == 0 &&
358 pcpu->timer_run_time >= pcpu->idle_exit_time &&
359 pcpu->governor_enabled) {
361 get_cpu_idle_time_us(smp_processor_id(),
362 &pcpu->idle_exit_time);
363 pcpu->timer_idlecancel = 0;
364 mod_timer(&pcpu->cpu_timer,
365 jiffies + usecs_to_jiffies(timer_rate));
370 static int cpufreq_interactive_speedchange_task(void *data)
375 struct cpufreq_interactive_cpuinfo *pcpu;
378 set_current_state(TASK_INTERRUPTIBLE);
379 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
381 if (cpumask_empty(&speedchange_cpumask)) {
382 spin_unlock_irqrestore(&speedchange_cpumask_lock,
386 if (kthread_should_stop())
389 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
392 set_current_state(TASK_RUNNING);
393 tmp_mask = speedchange_cpumask;
394 cpumask_clear(&speedchange_cpumask);
395 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
397 for_each_cpu(cpu, &tmp_mask) {
399 unsigned int max_freq = 0;
401 pcpu = &per_cpu(cpuinfo, cpu);
404 if (!pcpu->governor_enabled)
407 for_each_cpu(j, pcpu->policy->cpus) {
408 struct cpufreq_interactive_cpuinfo *pjcpu =
409 &per_cpu(cpuinfo, j);
411 if (pjcpu->target_freq > max_freq)
412 max_freq = pjcpu->target_freq;
415 if (max_freq != pcpu->policy->cur)
416 __cpufreq_driver_target(pcpu->policy,
419 trace_cpufreq_interactive_setspeed(cpu,
428 static void cpufreq_interactive_boost(void)
433 struct cpufreq_interactive_cpuinfo *pcpu;
435 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
437 for_each_online_cpu(i) {
438 pcpu = &per_cpu(cpuinfo, i);
440 if (pcpu->target_freq < hispeed_freq) {
441 pcpu->target_freq = hispeed_freq;
442 cpumask_set_cpu(i, &speedchange_cpumask);
443 pcpu->target_set_time_in_idle =
444 get_cpu_idle_time_us(i, &pcpu->target_set_time);
445 pcpu->hispeed_validate_time = pcpu->target_set_time;
450 * Set floor freq and (re)start timer for when last
454 pcpu->floor_freq = hispeed_freq;
455 pcpu->floor_validate_time = ktime_to_us(ktime_get());
458 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
461 wake_up_process(speedchange_task);
464 static ssize_t show_hispeed_freq(struct kobject *kobj,
465 struct attribute *attr, char *buf)
467 return sprintf(buf, "%llu\n", hispeed_freq);
470 static ssize_t store_hispeed_freq(struct kobject *kobj,
471 struct attribute *attr, const char *buf,
477 ret = strict_strtoull(buf, 0, &val);
484 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
485 show_hispeed_freq, store_hispeed_freq);
488 static ssize_t show_go_hispeed_load(struct kobject *kobj,
489 struct attribute *attr, char *buf)
491 return sprintf(buf, "%lu\n", go_hispeed_load);
494 static ssize_t store_go_hispeed_load(struct kobject *kobj,
495 struct attribute *attr, const char *buf, size_t count)
500 ret = strict_strtoul(buf, 0, &val);
503 go_hispeed_load = val;
507 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
508 show_go_hispeed_load, store_go_hispeed_load);
510 static ssize_t show_min_sample_time(struct kobject *kobj,
511 struct attribute *attr, char *buf)
513 return sprintf(buf, "%lu\n", min_sample_time);
516 static ssize_t store_min_sample_time(struct kobject *kobj,
517 struct attribute *attr, const char *buf, size_t count)
522 ret = strict_strtoul(buf, 0, &val);
525 min_sample_time = val;
529 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
530 show_min_sample_time, store_min_sample_time);
532 static ssize_t show_above_hispeed_delay(struct kobject *kobj,
533 struct attribute *attr, char *buf)
535 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
538 static ssize_t store_above_hispeed_delay(struct kobject *kobj,
539 struct attribute *attr,
540 const char *buf, size_t count)
545 ret = strict_strtoul(buf, 0, &val);
548 above_hispeed_delay_val = val;
552 define_one_global_rw(above_hispeed_delay);
554 static ssize_t show_timer_rate(struct kobject *kobj,
555 struct attribute *attr, char *buf)
557 return sprintf(buf, "%lu\n", timer_rate);
560 static ssize_t store_timer_rate(struct kobject *kobj,
561 struct attribute *attr, const char *buf, size_t count)
566 ret = strict_strtoul(buf, 0, &val);
573 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
574 show_timer_rate, store_timer_rate);
576 static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
579 return sprintf(buf, "%d\n", boost_val);
582 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
583 const char *buf, size_t count)
588 ret = kstrtoul(buf, 0, &val);
595 trace_cpufreq_interactive_boost("on");
596 cpufreq_interactive_boost();
598 trace_cpufreq_interactive_unboost("off");
604 define_one_global_rw(boost);
606 static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
607 const char *buf, size_t count)
612 ret = kstrtoul(buf, 0, &val);
616 trace_cpufreq_interactive_boost("pulse");
617 cpufreq_interactive_boost();
621 static struct global_attr boostpulse =
622 __ATTR(boostpulse, 0200, NULL, store_boostpulse);
624 static struct attribute *interactive_attributes[] = {
625 &hispeed_freq_attr.attr,
626 &go_hispeed_load_attr.attr,
627 &above_hispeed_delay.attr,
628 &min_sample_time_attr.attr,
629 &timer_rate_attr.attr,
635 static struct attribute_group interactive_attr_group = {
636 .attrs = interactive_attributes,
637 .name = "interactive",
640 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
646 cpufreq_interactive_idle_start();
649 cpufreq_interactive_idle_end();
656 static struct notifier_block cpufreq_interactive_idle_nb = {
657 .notifier_call = cpufreq_interactive_idle_notifier,
660 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
665 struct cpufreq_interactive_cpuinfo *pcpu;
666 struct cpufreq_frequency_table *freq_table;
669 case CPUFREQ_GOV_START:
670 if (!cpu_online(policy->cpu))
674 cpufreq_frequency_get_table(policy->cpu);
676 for_each_cpu(j, policy->cpus) {
677 pcpu = &per_cpu(cpuinfo, j);
678 pcpu->policy = policy;
679 pcpu->target_freq = policy->cur;
680 pcpu->freq_table = freq_table;
681 pcpu->target_set_time_in_idle =
682 get_cpu_idle_time_us(j,
683 &pcpu->target_set_time);
684 pcpu->floor_freq = pcpu->target_freq;
685 pcpu->floor_validate_time =
686 pcpu->target_set_time;
687 pcpu->hispeed_validate_time =
688 pcpu->target_set_time;
689 pcpu->governor_enabled = 1;
694 hispeed_freq = policy->max;
697 * Do not register the idle hook and create sysfs
698 * entries if we have already done so.
700 if (atomic_inc_return(&active_count) > 1)
703 rc = sysfs_create_group(cpufreq_global_kobject,
704 &interactive_attr_group);
708 idle_notifier_register(&cpufreq_interactive_idle_nb);
711 case CPUFREQ_GOV_STOP:
712 for_each_cpu(j, policy->cpus) {
713 pcpu = &per_cpu(cpuinfo, j);
714 pcpu->governor_enabled = 0;
716 del_timer_sync(&pcpu->cpu_timer);
719 * Reset idle exit time since we may cancel the timer
720 * before it can run after the last idle exit time,
721 * to avoid tripping the check in idle exit for a timer
722 * that is trying to run.
724 pcpu->idle_exit_time = 0;
727 if (atomic_dec_return(&active_count) > 0)
730 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
731 sysfs_remove_group(cpufreq_global_kobject,
732 &interactive_attr_group);
736 case CPUFREQ_GOV_LIMITS:
737 if (policy->max < policy->cur)
738 __cpufreq_driver_target(policy,
739 policy->max, CPUFREQ_RELATION_H);
740 else if (policy->min > policy->cur)
741 __cpufreq_driver_target(policy,
742 policy->min, CPUFREQ_RELATION_L);
748 static int __init cpufreq_interactive_init(void)
751 struct cpufreq_interactive_cpuinfo *pcpu;
752 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
754 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
755 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
756 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
757 timer_rate = DEFAULT_TIMER_RATE;
759 /* Initalize per-cpu timers */
760 for_each_possible_cpu(i) {
761 pcpu = &per_cpu(cpuinfo, i);
762 init_timer(&pcpu->cpu_timer);
763 pcpu->cpu_timer.function = cpufreq_interactive_timer;
764 pcpu->cpu_timer.data = i;
767 spin_lock_init(&speedchange_cpumask_lock);
769 kthread_create(cpufreq_interactive_speedchange_task, NULL,
771 if (IS_ERR(speedchange_task))
772 return PTR_ERR(speedchange_task);
774 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
775 get_task_struct(speedchange_task);
777 /* NB: wake up so the thread does not look hung to the freezer */
778 wake_up_process(speedchange_task);
780 return cpufreq_register_governor(&cpufreq_gov_interactive);
783 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
784 fs_initcall(cpufreq_interactive_init);
786 module_init(cpufreq_interactive_init);
789 static void __exit cpufreq_interactive_exit(void)
791 cpufreq_unregister_governor(&cpufreq_gov_interactive);
792 kthread_stop(speedchange_task);
793 put_task_struct(speedchange_task);
796 module_exit(cpufreq_interactive_exit);
798 MODULE_AUTHOR("Mike Chan <mike@android.com>");
799 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
800 "Latency sensitive workloads");
801 MODULE_LICENSE("GPL");