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>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/cpufreq_interactive.h>
36 #include <asm/cputime.h>
38 static atomic_t active_count = ATOMIC_INIT(0);
40 struct cpufreq_interactive_cpuinfo {
41 struct timer_list cpu_timer;
48 u64 freq_change_time_in_idle;
49 struct cpufreq_policy *policy;
50 struct cpufreq_frequency_table *freq_table;
51 unsigned int target_freq;
55 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
57 /* Workqueues handle frequency scaling */
58 static struct task_struct *up_task;
59 static struct workqueue_struct *down_wq;
60 static struct work_struct freq_scale_down_work;
61 static cpumask_t up_cpumask;
62 static spinlock_t up_cpumask_lock;
63 static cpumask_t down_cpumask;
64 static spinlock_t down_cpumask_lock;
65 static struct mutex set_speed_lock;
67 /* Hi speed to bump to from lo speed when load burst (default max) */
68 static u64 hispeed_freq;
70 /* Go to hi speed when CPU load at or above this value. */
71 #define DEFAULT_GO_HISPEED_LOAD 95
72 static unsigned long go_hispeed_load;
75 * The minimum amount of time to spend at a frequency before we can ramp down.
77 #define DEFAULT_MIN_SAMPLE_TIME 20 * USEC_PER_MSEC
78 static unsigned long min_sample_time;
81 * The sample rate of the timer used to increase frequency
83 #define DEFAULT_TIMER_RATE 20 * USEC_PER_MSEC
84 static unsigned long timer_rate;
86 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
89 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
92 struct cpufreq_governor cpufreq_gov_interactive = {
93 .name = "interactive",
94 .governor = cpufreq_governor_interactive,
95 .max_transition_latency = 10000000,
99 static void cpufreq_interactive_timer(unsigned long data)
101 unsigned int delta_idle;
102 unsigned int delta_time;
104 int load_since_change;
107 struct cpufreq_interactive_cpuinfo *pcpu =
108 &per_cpu(cpuinfo, data);
110 unsigned int new_freq;
116 if (!pcpu->governor_enabled)
120 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
121 * this lets idle exit know the current idle time sample has
122 * been processed, and idle exit can generate a new sample and
123 * re-arm the timer. This prevents a concurrent idle
124 * exit on that CPU from writing a new set of info at the same time
125 * the timer function runs (the timer function can't use that info
126 * until more time passes).
128 time_in_idle = pcpu->time_in_idle;
129 idle_exit_time = pcpu->idle_exit_time;
130 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
133 /* If we raced with cancelling a timer, skip. */
137 delta_idle = (unsigned int)(now_idle - time_in_idle);
138 delta_time = (unsigned int)(pcpu->timer_run_time - idle_exit_time);
141 * If timer ran less than 1ms after short-term sample started, retry.
143 if (delta_time < 1000)
146 if (delta_idle > delta_time)
149 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
151 delta_idle = (unsigned int)(now_idle - pcpu->freq_change_time_in_idle);
152 delta_time = (unsigned int)(pcpu->timer_run_time - 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;
187 if (pcpu->target_freq == new_freq)
189 trace_cpufreq_interactive_already(data, cpu_load,
190 pcpu->target_freq, new_freq);
191 goto rearm_if_notmax;
195 * Do not scale down unless we have been at this frequency for the
196 * minimum sample time.
198 if (new_freq < pcpu->target_freq) {
199 if (pcpu->timer_run_time - pcpu->freq_change_time
201 trace_cpufreq_interactive_notyet(data, cpu_load,
202 pcpu->target_freq, new_freq);
207 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
210 if (new_freq < pcpu->target_freq) {
211 pcpu->target_freq = new_freq;
212 spin_lock_irqsave(&down_cpumask_lock, flags);
213 cpumask_set_cpu(data, &down_cpumask);
214 spin_unlock_irqrestore(&down_cpumask_lock, flags);
215 queue_work(down_wq, &freq_scale_down_work);
217 pcpu->target_freq = new_freq;
218 spin_lock_irqsave(&up_cpumask_lock, flags);
219 cpumask_set_cpu(data, &up_cpumask);
220 spin_unlock_irqrestore(&up_cpumask_lock, flags);
221 wake_up_process(up_task);
226 * Already set max speed and don't see a need to change that,
227 * wait until next idle to re-evaluate, don't need timer.
229 if (pcpu->target_freq == pcpu->policy->max)
233 if (!timer_pending(&pcpu->cpu_timer)) {
235 * If already at min: if that CPU is idle, don't set timer.
236 * Else cancel the timer if that CPU goes idle. We don't
237 * need to re-evaluate speed until the next idle exit.
239 if (pcpu->target_freq == pcpu->policy->min) {
245 pcpu->timer_idlecancel = 1;
248 pcpu->time_in_idle = get_cpu_idle_time_us(
249 data, &pcpu->idle_exit_time);
250 mod_timer(&pcpu->cpu_timer,
251 jiffies + usecs_to_jiffies(timer_rate));
258 static void cpufreq_interactive_idle_start(void)
260 struct cpufreq_interactive_cpuinfo *pcpu =
261 &per_cpu(cpuinfo, smp_processor_id());
264 if (!pcpu->governor_enabled)
269 pending = timer_pending(&pcpu->cpu_timer);
271 if (pcpu->target_freq != pcpu->policy->min) {
274 * Entering idle while not at lowest speed. On some
275 * platforms this can hold the other CPU(s) at that speed
276 * even though the CPU is idle. Set a timer to re-evaluate
277 * speed so this idle CPU doesn't hold the other CPUs above
278 * min indefinitely. This should probably be a quirk of
279 * the CPUFreq driver.
282 pcpu->time_in_idle = get_cpu_idle_time_us(
283 smp_processor_id(), &pcpu->idle_exit_time);
284 pcpu->timer_idlecancel = 0;
285 mod_timer(&pcpu->cpu_timer,
286 jiffies + usecs_to_jiffies(timer_rate));
291 * If at min speed and entering idle after load has
292 * already been evaluated, and a timer has been set just in
293 * case the CPU suddenly goes busy, cancel that timer. The
294 * CPU didn't go busy; we'll recheck things upon idle exit.
296 if (pending && pcpu->timer_idlecancel) {
297 del_timer(&pcpu->cpu_timer);
299 * Ensure last timer run time is after current idle
300 * sample start time, so next idle exit will always
301 * start a new idle sampling period.
303 pcpu->idle_exit_time = 0;
304 pcpu->timer_idlecancel = 0;
310 static void cpufreq_interactive_idle_end(void)
312 struct cpufreq_interactive_cpuinfo *pcpu =
313 &per_cpu(cpuinfo, smp_processor_id());
319 * Arm the timer for 1-2 ticks later if not already, and if the timer
320 * function has already processed the previous load sampling
321 * interval. (If the timer is not pending but has not processed
322 * the previous interval, it is probably racing with us on another
323 * CPU. Let it compute load based on the previous sample and then
324 * re-arm the timer for another interval when it's done, rather
325 * than updating the interval start time to be "now", which doesn't
326 * give the timer function enough time to make a decision on this
329 if (timer_pending(&pcpu->cpu_timer) == 0 &&
330 pcpu->timer_run_time >= pcpu->idle_exit_time &&
331 pcpu->governor_enabled) {
333 get_cpu_idle_time_us(smp_processor_id(),
334 &pcpu->idle_exit_time);
335 pcpu->timer_idlecancel = 0;
336 mod_timer(&pcpu->cpu_timer,
337 jiffies + usecs_to_jiffies(timer_rate));
342 static int cpufreq_interactive_up_task(void *data)
347 struct cpufreq_interactive_cpuinfo *pcpu;
350 set_current_state(TASK_INTERRUPTIBLE);
351 spin_lock_irqsave(&up_cpumask_lock, flags);
353 if (cpumask_empty(&up_cpumask)) {
354 spin_unlock_irqrestore(&up_cpumask_lock, flags);
357 if (kthread_should_stop())
360 spin_lock_irqsave(&up_cpumask_lock, flags);
363 set_current_state(TASK_RUNNING);
364 tmp_mask = up_cpumask;
365 cpumask_clear(&up_cpumask);
366 spin_unlock_irqrestore(&up_cpumask_lock, flags);
368 for_each_cpu(cpu, &tmp_mask) {
370 unsigned int max_freq = 0;
372 pcpu = &per_cpu(cpuinfo, cpu);
375 if (!pcpu->governor_enabled)
378 mutex_lock(&set_speed_lock);
380 for_each_cpu(j, pcpu->policy->cpus) {
381 struct cpufreq_interactive_cpuinfo *pjcpu =
382 &per_cpu(cpuinfo, j);
384 if (pjcpu->target_freq > max_freq)
385 max_freq = pjcpu->target_freq;
388 if (max_freq != pcpu->policy->cur)
389 __cpufreq_driver_target(pcpu->policy,
392 mutex_unlock(&set_speed_lock);
394 pcpu->freq_change_time_in_idle =
395 get_cpu_idle_time_us(cpu,
396 &pcpu->freq_change_time);
397 trace_cpufreq_interactive_up(cpu, pcpu->target_freq,
405 static void cpufreq_interactive_freq_down(struct work_struct *work)
410 struct cpufreq_interactive_cpuinfo *pcpu;
412 spin_lock_irqsave(&down_cpumask_lock, flags);
413 tmp_mask = down_cpumask;
414 cpumask_clear(&down_cpumask);
415 spin_unlock_irqrestore(&down_cpumask_lock, flags);
417 for_each_cpu(cpu, &tmp_mask) {
419 unsigned int max_freq = 0;
421 pcpu = &per_cpu(cpuinfo, cpu);
424 if (!pcpu->governor_enabled)
427 mutex_lock(&set_speed_lock);
429 for_each_cpu(j, pcpu->policy->cpus) {
430 struct cpufreq_interactive_cpuinfo *pjcpu =
431 &per_cpu(cpuinfo, j);
433 if (pjcpu->target_freq > max_freq)
434 max_freq = pjcpu->target_freq;
437 if (max_freq != pcpu->policy->cur)
438 __cpufreq_driver_target(pcpu->policy, max_freq,
441 mutex_unlock(&set_speed_lock);
442 pcpu->freq_change_time_in_idle =
443 get_cpu_idle_time_us(cpu,
444 &pcpu->freq_change_time);
445 trace_cpufreq_interactive_down(cpu, pcpu->target_freq,
450 static ssize_t show_hispeed_freq(struct kobject *kobj,
451 struct attribute *attr, char *buf)
453 return sprintf(buf, "%llu\n", hispeed_freq);
456 static ssize_t store_hispeed_freq(struct kobject *kobj,
457 struct attribute *attr, const char *buf,
463 ret = strict_strtoull(buf, 0, &val);
470 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
471 show_hispeed_freq, store_hispeed_freq);
474 static ssize_t show_go_hispeed_load(struct kobject *kobj,
475 struct attribute *attr, char *buf)
477 return sprintf(buf, "%lu\n", go_hispeed_load);
480 static ssize_t store_go_hispeed_load(struct kobject *kobj,
481 struct attribute *attr, const char *buf, size_t count)
486 ret = strict_strtoul(buf, 0, &val);
489 go_hispeed_load = val;
493 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
494 show_go_hispeed_load, store_go_hispeed_load);
496 static ssize_t show_min_sample_time(struct kobject *kobj,
497 struct attribute *attr, char *buf)
499 return sprintf(buf, "%lu\n", min_sample_time);
502 static ssize_t store_min_sample_time(struct kobject *kobj,
503 struct attribute *attr, const char *buf, size_t count)
508 ret = strict_strtoul(buf, 0, &val);
511 min_sample_time = val;
515 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
516 show_min_sample_time, store_min_sample_time);
518 static ssize_t show_timer_rate(struct kobject *kobj,
519 struct attribute *attr, char *buf)
521 return sprintf(buf, "%lu\n", timer_rate);
524 static ssize_t store_timer_rate(struct kobject *kobj,
525 struct attribute *attr, const char *buf, size_t count)
530 ret = strict_strtoul(buf, 0, &val);
537 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
538 show_timer_rate, store_timer_rate);
540 static struct attribute *interactive_attributes[] = {
541 &hispeed_freq_attr.attr,
542 &go_hispeed_load_attr.attr,
543 &min_sample_time_attr.attr,
544 &timer_rate_attr.attr,
548 static struct attribute_group interactive_attr_group = {
549 .attrs = interactive_attributes,
550 .name = "interactive",
553 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
558 struct cpufreq_interactive_cpuinfo *pcpu;
559 struct cpufreq_frequency_table *freq_table;
562 case CPUFREQ_GOV_START:
563 if (!cpu_online(policy->cpu))
567 cpufreq_frequency_get_table(policy->cpu);
569 for_each_cpu(j, policy->cpus) {
570 pcpu = &per_cpu(cpuinfo, j);
571 pcpu->policy = policy;
572 pcpu->target_freq = policy->cur;
573 pcpu->freq_table = freq_table;
574 pcpu->freq_change_time_in_idle =
575 get_cpu_idle_time_us(j,
576 &pcpu->freq_change_time);
577 pcpu->governor_enabled = 1;
582 hispeed_freq = policy->max;
585 * Do not register the idle hook and create sysfs
586 * entries if we have already done so.
588 if (atomic_inc_return(&active_count) > 1)
591 rc = sysfs_create_group(cpufreq_global_kobject,
592 &interactive_attr_group);
598 case CPUFREQ_GOV_STOP:
599 for_each_cpu(j, policy->cpus) {
600 pcpu = &per_cpu(cpuinfo, j);
601 pcpu->governor_enabled = 0;
603 del_timer_sync(&pcpu->cpu_timer);
606 * Reset idle exit time since we may cancel the timer
607 * before it can run after the last idle exit time,
608 * to avoid tripping the check in idle exit for a timer
609 * that is trying to run.
611 pcpu->idle_exit_time = 0;
614 flush_work(&freq_scale_down_work);
615 if (atomic_dec_return(&active_count) > 0)
618 sysfs_remove_group(cpufreq_global_kobject,
619 &interactive_attr_group);
623 case CPUFREQ_GOV_LIMITS:
624 if (policy->max < policy->cur)
625 __cpufreq_driver_target(policy,
626 policy->max, CPUFREQ_RELATION_H);
627 else if (policy->min > policy->cur)
628 __cpufreq_driver_target(policy,
629 policy->min, CPUFREQ_RELATION_L);
635 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
641 cpufreq_interactive_idle_start();
644 cpufreq_interactive_idle_end();
651 static struct notifier_block cpufreq_interactive_idle_nb = {
652 .notifier_call = cpufreq_interactive_idle_notifier,
655 static int __init cpufreq_interactive_init(void)
658 struct cpufreq_interactive_cpuinfo *pcpu;
659 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
661 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
662 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
663 timer_rate = DEFAULT_TIMER_RATE;
665 /* Initalize per-cpu timers */
666 for_each_possible_cpu(i) {
667 pcpu = &per_cpu(cpuinfo, i);
668 init_timer(&pcpu->cpu_timer);
669 pcpu->cpu_timer.function = cpufreq_interactive_timer;
670 pcpu->cpu_timer.data = i;
673 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
676 return PTR_ERR(up_task);
678 sched_setscheduler_nocheck(up_task, SCHED_FIFO, ¶m);
679 get_task_struct(up_task);
681 /* No rescuer thread, bind to CPU queuing the work for possibly
682 warm cache (probably doesn't matter much). */
683 down_wq = alloc_workqueue("knteractive_down", 0, 1);
688 INIT_WORK(&freq_scale_down_work,
689 cpufreq_interactive_freq_down);
691 spin_lock_init(&up_cpumask_lock);
692 spin_lock_init(&down_cpumask_lock);
693 mutex_init(&set_speed_lock);
695 idle_notifier_register(&cpufreq_interactive_idle_nb);
697 return cpufreq_register_governor(&cpufreq_gov_interactive);
700 put_task_struct(up_task);
704 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
705 fs_initcall(cpufreq_interactive_init);
707 module_init(cpufreq_interactive_init);
710 static void __exit cpufreq_interactive_exit(void)
712 cpufreq_unregister_governor(&cpufreq_gov_interactive);
713 kthread_stop(up_task);
714 put_task_struct(up_task);
715 destroy_workqueue(down_wq);
718 module_exit(cpufreq_interactive_exit);
720 MODULE_AUTHOR("Mike Chan <mike@android.com>");
721 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
722 "Latency sensitive workloads");
723 MODULE_LICENSE("GPL");