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 #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 20 * USEC_PER_MSEC
81 static unsigned long timer_rate;
83 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
86 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
89 struct cpufreq_governor cpufreq_gov_interactive = {
90 .name = "interactive",
91 .governor = cpufreq_governor_interactive,
92 .max_transition_latency = 10000000,
96 static void cpufreq_interactive_timer(unsigned long data)
98 unsigned int delta_idle;
99 unsigned int delta_time;
101 int load_since_change;
104 struct cpufreq_interactive_cpuinfo *pcpu =
105 &per_cpu(cpuinfo, data);
107 unsigned int new_freq;
113 if (!pcpu->governor_enabled)
117 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
118 * this lets idle exit know the current idle time sample has
119 * been processed, and idle exit can generate a new sample and
120 * re-arm the timer. This prevents a concurrent idle
121 * exit on that CPU from writing a new set of info at the same time
122 * the timer function runs (the timer function can't use that info
123 * until more time passes).
125 time_in_idle = pcpu->time_in_idle;
126 idle_exit_time = pcpu->idle_exit_time;
127 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
130 /* If we raced with cancelling a timer, skip. */
134 delta_idle = (unsigned int)(now_idle - time_in_idle);
135 delta_time = (unsigned int)(pcpu->timer_run_time - idle_exit_time);
138 * If timer ran less than 1ms after short-term sample started, retry.
140 if (delta_time < 1000)
143 if (delta_idle > delta_time)
146 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
148 delta_idle = (unsigned int)(now_idle - pcpu->freq_change_time_in_idle);
149 delta_time = (unsigned int)(pcpu->timer_run_time - pcpu->freq_change_time);
151 if ((delta_time == 0) || (delta_idle > delta_time))
152 load_since_change = 0;
155 100 * (delta_time - delta_idle) / delta_time;
158 * Choose greater of short-term load (since last idle timer
159 * started or timer function re-armed itself) or long-term load
160 * (since last frequency change).
162 if (load_since_change > cpu_load)
163 cpu_load = load_since_change;
165 if (cpu_load >= go_hispeed_load) {
166 if (pcpu->policy->cur == pcpu->policy->min)
167 new_freq = hispeed_freq;
169 new_freq = pcpu->policy->max * cpu_load / 100;
171 new_freq = pcpu->policy->cur * cpu_load / 100;
174 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
175 new_freq, CPUFREQ_RELATION_H,
177 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
182 new_freq = pcpu->freq_table[index].frequency;
184 if (pcpu->target_freq == new_freq)
185 goto rearm_if_notmax;
188 * Do not scale down unless we have been at this frequency for the
189 * minimum sample time.
191 if (new_freq < pcpu->target_freq) {
192 if (pcpu->timer_run_time - pcpu->freq_change_time
197 if (new_freq < pcpu->target_freq) {
198 pcpu->target_freq = new_freq;
199 spin_lock_irqsave(&down_cpumask_lock, flags);
200 cpumask_set_cpu(data, &down_cpumask);
201 spin_unlock_irqrestore(&down_cpumask_lock, flags);
202 queue_work(down_wq, &freq_scale_down_work);
204 pcpu->target_freq = new_freq;
205 spin_lock_irqsave(&up_cpumask_lock, flags);
206 cpumask_set_cpu(data, &up_cpumask);
207 spin_unlock_irqrestore(&up_cpumask_lock, flags);
208 wake_up_process(up_task);
213 * Already set max speed and don't see a need to change that,
214 * wait until next idle to re-evaluate, don't need timer.
216 if (pcpu->target_freq == pcpu->policy->max)
220 if (!timer_pending(&pcpu->cpu_timer)) {
222 * If already at min: if that CPU is idle, don't set timer.
223 * Else cancel the timer if that CPU goes idle. We don't
224 * need to re-evaluate speed until the next idle exit.
226 if (pcpu->target_freq == pcpu->policy->min) {
232 pcpu->timer_idlecancel = 1;
235 pcpu->time_in_idle = get_cpu_idle_time_us(
236 data, &pcpu->idle_exit_time);
237 mod_timer(&pcpu->cpu_timer,
238 jiffies + usecs_to_jiffies(timer_rate));
245 static void cpufreq_interactive_idle_start(void)
247 struct cpufreq_interactive_cpuinfo *pcpu =
248 &per_cpu(cpuinfo, smp_processor_id());
251 if (!pcpu->governor_enabled)
256 pending = timer_pending(&pcpu->cpu_timer);
258 if (pcpu->target_freq != pcpu->policy->min) {
261 * Entering idle while not at lowest speed. On some
262 * platforms this can hold the other CPU(s) at that speed
263 * even though the CPU is idle. Set a timer to re-evaluate
264 * speed so this idle CPU doesn't hold the other CPUs above
265 * min indefinitely. This should probably be a quirk of
266 * the CPUFreq driver.
269 pcpu->time_in_idle = get_cpu_idle_time_us(
270 smp_processor_id(), &pcpu->idle_exit_time);
271 pcpu->timer_idlecancel = 0;
272 mod_timer(&pcpu->cpu_timer,
273 jiffies + usecs_to_jiffies(timer_rate));
278 * If at min speed and entering idle after load has
279 * already been evaluated, and a timer has been set just in
280 * case the CPU suddenly goes busy, cancel that timer. The
281 * CPU didn't go busy; we'll recheck things upon idle exit.
283 if (pending && pcpu->timer_idlecancel) {
284 del_timer(&pcpu->cpu_timer);
286 * Ensure last timer run time is after current idle
287 * sample start time, so next idle exit will always
288 * start a new idle sampling period.
290 pcpu->idle_exit_time = 0;
291 pcpu->timer_idlecancel = 0;
297 static void cpufreq_interactive_idle_end(void)
299 struct cpufreq_interactive_cpuinfo *pcpu =
300 &per_cpu(cpuinfo, smp_processor_id());
306 * Arm the timer for 1-2 ticks later if not already, and if the timer
307 * function has already processed the previous load sampling
308 * interval. (If the timer is not pending but has not processed
309 * the previous interval, it is probably racing with us on another
310 * CPU. Let it compute load based on the previous sample and then
311 * re-arm the timer for another interval when it's done, rather
312 * than updating the interval start time to be "now", which doesn't
313 * give the timer function enough time to make a decision on this
316 if (timer_pending(&pcpu->cpu_timer) == 0 &&
317 pcpu->timer_run_time >= pcpu->idle_exit_time &&
318 pcpu->governor_enabled) {
320 get_cpu_idle_time_us(smp_processor_id(),
321 &pcpu->idle_exit_time);
322 pcpu->timer_idlecancel = 0;
323 mod_timer(&pcpu->cpu_timer,
324 jiffies + usecs_to_jiffies(timer_rate));
329 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 tmp_mask = up_cpumask;
352 cpumask_clear(&up_cpumask);
353 spin_unlock_irqrestore(&up_cpumask_lock, flags);
355 for_each_cpu(cpu, &tmp_mask) {
357 unsigned int max_freq = 0;
359 pcpu = &per_cpu(cpuinfo, cpu);
362 if (!pcpu->governor_enabled)
365 mutex_lock(&set_speed_lock);
367 for_each_cpu(j, pcpu->policy->cpus) {
368 struct cpufreq_interactive_cpuinfo *pjcpu =
369 &per_cpu(cpuinfo, j);
371 if (pjcpu->target_freq > max_freq)
372 max_freq = pjcpu->target_freq;
375 if (max_freq != pcpu->policy->cur)
376 __cpufreq_driver_target(pcpu->policy,
379 mutex_unlock(&set_speed_lock);
381 pcpu->freq_change_time_in_idle =
382 get_cpu_idle_time_us(cpu,
383 &pcpu->freq_change_time);
390 static void cpufreq_interactive_freq_down(struct work_struct *work)
395 struct cpufreq_interactive_cpuinfo *pcpu;
397 spin_lock_irqsave(&down_cpumask_lock, flags);
398 tmp_mask = down_cpumask;
399 cpumask_clear(&down_cpumask);
400 spin_unlock_irqrestore(&down_cpumask_lock, flags);
402 for_each_cpu(cpu, &tmp_mask) {
404 unsigned int max_freq = 0;
406 pcpu = &per_cpu(cpuinfo, cpu);
409 if (!pcpu->governor_enabled)
412 mutex_lock(&set_speed_lock);
414 for_each_cpu(j, pcpu->policy->cpus) {
415 struct cpufreq_interactive_cpuinfo *pjcpu =
416 &per_cpu(cpuinfo, j);
418 if (pjcpu->target_freq > max_freq)
419 max_freq = pjcpu->target_freq;
422 if (max_freq != pcpu->policy->cur)
423 __cpufreq_driver_target(pcpu->policy, max_freq,
426 mutex_unlock(&set_speed_lock);
427 pcpu->freq_change_time_in_idle =
428 get_cpu_idle_time_us(cpu,
429 &pcpu->freq_change_time);
433 static ssize_t show_hispeed_freq(struct kobject *kobj,
434 struct attribute *attr, char *buf)
436 return sprintf(buf, "%llu\n", hispeed_freq);
439 static ssize_t store_hispeed_freq(struct kobject *kobj,
440 struct attribute *attr, const char *buf,
446 ret = strict_strtoull(buf, 0, &val);
453 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
454 show_hispeed_freq, store_hispeed_freq);
457 static ssize_t show_go_hispeed_load(struct kobject *kobj,
458 struct attribute *attr, char *buf)
460 return sprintf(buf, "%lu\n", go_hispeed_load);
463 static ssize_t store_go_hispeed_load(struct kobject *kobj,
464 struct attribute *attr, const char *buf, size_t count)
469 ret = strict_strtoul(buf, 0, &val);
472 go_hispeed_load = val;
476 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
477 show_go_hispeed_load, store_go_hispeed_load);
479 static ssize_t show_min_sample_time(struct kobject *kobj,
480 struct attribute *attr, char *buf)
482 return sprintf(buf, "%lu\n", min_sample_time);
485 static ssize_t store_min_sample_time(struct kobject *kobj,
486 struct attribute *attr, const char *buf, size_t count)
491 ret = strict_strtoul(buf, 0, &val);
494 min_sample_time = val;
498 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
499 show_min_sample_time, store_min_sample_time);
501 static ssize_t show_timer_rate(struct kobject *kobj,
502 struct attribute *attr, char *buf)
504 return sprintf(buf, "%lu\n", timer_rate);
507 static ssize_t store_timer_rate(struct kobject *kobj,
508 struct attribute *attr, const char *buf, size_t count)
513 ret = strict_strtoul(buf, 0, &val);
520 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
521 show_timer_rate, store_timer_rate);
523 static struct attribute *interactive_attributes[] = {
524 &hispeed_freq_attr.attr,
525 &go_hispeed_load_attr.attr,
526 &min_sample_time_attr.attr,
527 &timer_rate_attr.attr,
531 static struct attribute_group interactive_attr_group = {
532 .attrs = interactive_attributes,
533 .name = "interactive",
536 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
541 struct cpufreq_interactive_cpuinfo *pcpu;
542 struct cpufreq_frequency_table *freq_table;
545 case CPUFREQ_GOV_START:
546 if (!cpu_online(policy->cpu))
550 cpufreq_frequency_get_table(policy->cpu);
552 for_each_cpu(j, policy->cpus) {
553 pcpu = &per_cpu(cpuinfo, j);
554 pcpu->policy = policy;
555 pcpu->target_freq = policy->cur;
556 pcpu->freq_table = freq_table;
557 pcpu->freq_change_time_in_idle =
558 get_cpu_idle_time_us(j,
559 &pcpu->freq_change_time);
560 pcpu->governor_enabled = 1;
565 hispeed_freq = policy->max;
568 * Do not register the idle hook and create sysfs
569 * entries if we have already done so.
571 if (atomic_inc_return(&active_count) > 1)
574 rc = sysfs_create_group(cpufreq_global_kobject,
575 &interactive_attr_group);
581 case CPUFREQ_GOV_STOP:
582 for_each_cpu(j, policy->cpus) {
583 pcpu = &per_cpu(cpuinfo, j);
584 pcpu->governor_enabled = 0;
586 del_timer_sync(&pcpu->cpu_timer);
589 * Reset idle exit time since we may cancel the timer
590 * before it can run after the last idle exit time,
591 * to avoid tripping the check in idle exit for a timer
592 * that is trying to run.
594 pcpu->idle_exit_time = 0;
597 flush_work(&freq_scale_down_work);
598 if (atomic_dec_return(&active_count) > 0)
601 sysfs_remove_group(cpufreq_global_kobject,
602 &interactive_attr_group);
606 case CPUFREQ_GOV_LIMITS:
607 if (policy->max < policy->cur)
608 __cpufreq_driver_target(policy,
609 policy->max, CPUFREQ_RELATION_H);
610 else if (policy->min > policy->cur)
611 __cpufreq_driver_target(policy,
612 policy->min, CPUFREQ_RELATION_L);
618 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
624 cpufreq_interactive_idle_start();
627 cpufreq_interactive_idle_end();
634 static struct notifier_block cpufreq_interactive_idle_nb = {
635 .notifier_call = cpufreq_interactive_idle_notifier,
638 static int __init cpufreq_interactive_init(void)
641 struct cpufreq_interactive_cpuinfo *pcpu;
642 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
644 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
645 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
646 timer_rate = DEFAULT_TIMER_RATE;
648 /* Initalize per-cpu timers */
649 for_each_possible_cpu(i) {
650 pcpu = &per_cpu(cpuinfo, i);
651 init_timer(&pcpu->cpu_timer);
652 pcpu->cpu_timer.function = cpufreq_interactive_timer;
653 pcpu->cpu_timer.data = i;
656 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
659 return PTR_ERR(up_task);
661 sched_setscheduler_nocheck(up_task, SCHED_FIFO, ¶m);
662 get_task_struct(up_task);
664 /* No rescuer thread, bind to CPU queuing the work for possibly
665 warm cache (probably doesn't matter much). */
666 down_wq = alloc_workqueue("knteractive_down", 0, 1);
671 INIT_WORK(&freq_scale_down_work,
672 cpufreq_interactive_freq_down);
674 spin_lock_init(&up_cpumask_lock);
675 spin_lock_init(&down_cpumask_lock);
676 mutex_init(&set_speed_lock);
678 idle_notifier_register(&cpufreq_interactive_idle_nb);
680 return cpufreq_register_governor(&cpufreq_gov_interactive);
683 put_task_struct(up_task);
687 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
688 fs_initcall(cpufreq_interactive_init);
690 module_init(cpufreq_interactive_init);
693 static void __exit cpufreq_interactive_exit(void)
695 cpufreq_unregister_governor(&cpufreq_gov_interactive);
696 kthread_stop(up_task);
697 put_task_struct(up_task);
698 destroy_workqueue(down_wq);
701 module_exit(cpufreq_interactive_exit);
703 MODULE_AUTHOR("Mike Chan <mike@android.com>");
704 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
705 "Latency sensitive workloads");
706 MODULE_LICENSE("GPL");