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/moduleparam.h>
24 #include <linux/mutex.h>
25 #include <linux/sched.h>
26 #include <linux/sched/rt.h>
27 #include <linux/tick.h>
28 #include <linux/time.h>
29 #include <linux/timer.h>
30 #include <linux/workqueue.h>
31 #include <linux/kthread.h>
32 #include <linux/mutex.h>
33 #include <linux/slab.h>
34 #include <asm/cputime.h>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/cpufreq_interactive.h>
39 static atomic_t active_count = ATOMIC_INIT(0);
41 struct cpufreq_interactive_cpuinfo {
42 struct timer_list cpu_timer;
45 u64 time_in_idle_timestamp;
47 u64 target_set_time_in_idle;
48 struct cpufreq_policy *policy;
49 struct cpufreq_frequency_table *freq_table;
50 unsigned int target_freq;
51 unsigned int floor_freq;
52 u64 floor_validate_time;
53 u64 hispeed_validate_time;
57 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
59 /* realtime thread handles frequency scaling */
60 static struct task_struct *speedchange_task;
61 static cpumask_t speedchange_cpumask;
62 static spinlock_t speedchange_cpumask_lock;
64 /* Hi speed to bump to from lo speed when load burst (default max) */
65 static unsigned int hispeed_freq;
67 /* Go to hi speed when CPU load at or above this value. */
68 #define DEFAULT_GO_HISPEED_LOAD 85
69 static unsigned long go_hispeed_load;
71 /* Target load. Lower values result in higher CPU speeds. */
72 #define DEFAULT_TARGET_LOAD 90
73 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
74 static spinlock_t target_loads_lock;
75 static unsigned int *target_loads = default_target_loads;
76 static int ntarget_loads = ARRAY_SIZE(default_target_loads);
79 * The minimum amount of time to spend at a frequency before we can ramp down.
81 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
82 static unsigned long min_sample_time;
85 * The sample rate of the timer used to increase frequency
87 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
88 static unsigned long timer_rate;
91 * Wait this long before raising speed above hispeed, by default a single
94 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
95 static unsigned long above_hispeed_delay_val;
98 * Non-zero means longer-term speed boost active.
101 static int boost_val;
103 static bool governidle;
104 module_param(governidle, bool, S_IWUSR | S_IRUGO);
105 MODULE_PARM_DESC(governidle,
106 "Set to 1 to wake up CPUs from idle to reduce speed (default 0)");
108 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
111 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
114 struct cpufreq_governor cpufreq_gov_interactive = {
115 .name = "interactive",
116 .governor = cpufreq_governor_interactive,
117 .max_transition_latency = 10000000,
118 .owner = THIS_MODULE,
121 static void cpufreq_interactive_timer_resched(
122 struct cpufreq_interactive_cpuinfo *pcpu)
124 mod_timer_pinned(&pcpu->cpu_timer,
125 jiffies + usecs_to_jiffies(timer_rate));
127 get_cpu_idle_time_us(smp_processor_id(),
128 &pcpu->time_in_idle_timestamp);
131 static unsigned int freq_to_targetload(unsigned int freq)
136 spin_lock(&target_loads_lock);
138 for (i = 0; i < ntarget_loads - 1 && freq >= target_loads[i+1]; i += 2)
141 ret = target_loads[i];
142 spin_unlock(&target_loads_lock);
147 * If increasing frequencies never map to a lower target load then
148 * choose_freq() will find the minimum frequency that does not exceed its
149 * target load given the current load.
152 static unsigned int choose_freq(
153 struct cpufreq_interactive_cpuinfo *pcpu, unsigned int curload)
155 unsigned int freq = pcpu->policy->cur;
156 unsigned int loadadjfreq = freq * curload;
157 unsigned int prevfreq, freqmin, freqmax;
166 tl = freq_to_targetload(freq);
169 * Find the lowest frequency where the computed load is less
170 * than or equal to the target load.
173 cpufreq_frequency_table_target(
174 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
175 CPUFREQ_RELATION_L, &index);
176 freq = pcpu->freq_table[index].frequency;
178 if (freq > prevfreq) {
179 /* The previous frequency is too low. */
182 if (freq >= freqmax) {
184 * Find the highest frequency that is less
187 cpufreq_frequency_table_target(
188 pcpu->policy, pcpu->freq_table,
189 freqmax - 1, CPUFREQ_RELATION_H,
191 freq = pcpu->freq_table[index].frequency;
193 if (freq == freqmin) {
195 * The first frequency below freqmax
196 * has already been found to be too
197 * low. freqmax is the lowest speed
198 * we found that is fast enough.
204 } else if (freq < prevfreq) {
205 /* The previous frequency is high enough. */
208 if (freq <= freqmin) {
210 * Find the lowest frequency that is higher
213 cpufreq_frequency_table_target(
214 pcpu->policy, pcpu->freq_table,
215 freqmin + 1, CPUFREQ_RELATION_L,
217 freq = pcpu->freq_table[index].frequency;
220 * If freqmax is the first frequency above
221 * freqmin then we have already found that
222 * this speed is fast enough.
229 /* If same frequency chosen as previous then done. */
230 } while (freq != prevfreq);
235 static void cpufreq_interactive_timer(unsigned long data)
238 unsigned int delta_idle;
239 unsigned int delta_time;
241 int load_since_change;
242 struct cpufreq_interactive_cpuinfo *pcpu =
243 &per_cpu(cpuinfo, data);
245 unsigned int new_freq;
251 if (!pcpu->governor_enabled)
254 now_idle = get_cpu_idle_time_us(data, &now);
255 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
256 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
259 * If timer ran less than 1ms after short-term sample started, retry.
261 if (delta_time < 1000)
264 if (delta_idle > delta_time)
267 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
269 delta_idle = (unsigned int)(now_idle - pcpu->target_set_time_in_idle);
270 delta_time = (unsigned int)(now - pcpu->target_set_time);
272 if ((delta_time == 0) || (delta_idle > delta_time))
273 load_since_change = 0;
276 100 * (delta_time - delta_idle) / delta_time;
279 * Choose greater of short-term load (since last idle timer
280 * started or timer function re-armed itself) or long-term load
281 * (since last frequency change).
283 if (load_since_change > cpu_load)
284 cpu_load = load_since_change;
286 if ((cpu_load >= go_hispeed_load || boost_val) &&
287 pcpu->target_freq < hispeed_freq)
288 new_freq = hispeed_freq;
290 new_freq = choose_freq(pcpu, cpu_load);
292 if (pcpu->target_freq >= hispeed_freq &&
293 new_freq > pcpu->target_freq &&
294 now - pcpu->hispeed_validate_time < above_hispeed_delay_val) {
295 trace_cpufreq_interactive_notyet(
296 data, cpu_load, pcpu->target_freq,
297 pcpu->policy->cur, new_freq);
301 pcpu->hispeed_validate_time = now;
303 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
304 new_freq, CPUFREQ_RELATION_L,
306 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
311 new_freq = pcpu->freq_table[index].frequency;
314 * Do not scale below floor_freq unless we have been at or above the
315 * floor frequency for the minimum sample time since last validated.
317 if (new_freq < pcpu->floor_freq) {
318 if (now - pcpu->floor_validate_time < min_sample_time) {
319 trace_cpufreq_interactive_notyet(
320 data, cpu_load, pcpu->target_freq,
321 pcpu->policy->cur, new_freq);
326 pcpu->floor_freq = new_freq;
327 pcpu->floor_validate_time = now;
329 if (pcpu->target_freq == new_freq) {
330 trace_cpufreq_interactive_already(
331 data, cpu_load, pcpu->target_freq,
332 pcpu->policy->cur, new_freq);
333 goto rearm_if_notmax;
336 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
337 pcpu->policy->cur, new_freq);
338 pcpu->target_set_time_in_idle = now_idle;
339 pcpu->target_set_time = now;
341 pcpu->target_freq = new_freq;
342 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
343 cpumask_set_cpu(data, &speedchange_cpumask);
344 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
345 wake_up_process(speedchange_task);
349 * Already set max speed and don't see a need to change that,
350 * wait until next idle to re-evaluate, don't need timer.
352 if (pcpu->target_freq == pcpu->policy->max)
356 if (!timer_pending(&pcpu->cpu_timer)) {
358 * If governing speed in idle and already at min, cancel the
359 * timer if that CPU goes idle. We don't need to re-evaluate
360 * speed until the next idle exit.
362 if (governidle && pcpu->target_freq == pcpu->policy->min)
363 pcpu->timer_idlecancel = 1;
365 cpufreq_interactive_timer_resched(pcpu);
372 static void cpufreq_interactive_idle_start(void)
374 struct cpufreq_interactive_cpuinfo *pcpu =
375 &per_cpu(cpuinfo, smp_processor_id());
378 if (!pcpu->governor_enabled)
381 pending = timer_pending(&pcpu->cpu_timer);
383 if (pcpu->target_freq != pcpu->policy->min) {
385 * Entering idle while not at lowest speed. On some
386 * platforms this can hold the other CPU(s) at that speed
387 * even though the CPU is idle. Set a timer to re-evaluate
388 * speed so this idle CPU doesn't hold the other CPUs above
389 * min indefinitely. This should probably be a quirk of
390 * the CPUFreq driver.
393 pcpu->timer_idlecancel = 0;
394 cpufreq_interactive_timer_resched(pcpu);
396 } else if (governidle) {
398 * If at min speed and entering idle after load has
399 * already been evaluated, and a timer has been set just in
400 * case the CPU suddenly goes busy, cancel that timer. The
401 * CPU didn't go busy; we'll recheck things upon idle exit.
403 if (pending && pcpu->timer_idlecancel) {
404 del_timer(&pcpu->cpu_timer);
405 pcpu->timer_idlecancel = 0;
411 static void cpufreq_interactive_idle_end(void)
413 struct cpufreq_interactive_cpuinfo *pcpu =
414 &per_cpu(cpuinfo, smp_processor_id());
416 if (!pcpu->governor_enabled)
419 /* Arm the timer for 1-2 ticks later if not already. */
420 if (!timer_pending(&pcpu->cpu_timer)) {
421 pcpu->timer_idlecancel = 0;
422 cpufreq_interactive_timer_resched(pcpu);
423 } else if (!governidle &&
424 time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
425 del_timer(&pcpu->cpu_timer);
426 cpufreq_interactive_timer(smp_processor_id());
430 static int cpufreq_interactive_speedchange_task(void *data)
435 struct cpufreq_interactive_cpuinfo *pcpu;
438 set_current_state(TASK_INTERRUPTIBLE);
439 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
441 if (cpumask_empty(&speedchange_cpumask)) {
442 spin_unlock_irqrestore(&speedchange_cpumask_lock,
446 if (kthread_should_stop())
449 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
452 set_current_state(TASK_RUNNING);
453 tmp_mask = speedchange_cpumask;
454 cpumask_clear(&speedchange_cpumask);
455 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
457 for_each_cpu(cpu, &tmp_mask) {
459 unsigned int max_freq = 0;
461 pcpu = &per_cpu(cpuinfo, cpu);
464 if (!pcpu->governor_enabled)
467 for_each_cpu(j, pcpu->policy->cpus) {
468 struct cpufreq_interactive_cpuinfo *pjcpu =
469 &per_cpu(cpuinfo, j);
471 if (pjcpu->target_freq > max_freq)
472 max_freq = pjcpu->target_freq;
475 if (max_freq != pcpu->policy->cur)
476 __cpufreq_driver_target(pcpu->policy,
479 trace_cpufreq_interactive_setspeed(cpu,
488 static void cpufreq_interactive_boost(void)
493 struct cpufreq_interactive_cpuinfo *pcpu;
495 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
497 for_each_online_cpu(i) {
498 pcpu = &per_cpu(cpuinfo, i);
500 if (pcpu->target_freq < hispeed_freq) {
501 pcpu->target_freq = hispeed_freq;
502 cpumask_set_cpu(i, &speedchange_cpumask);
503 pcpu->target_set_time_in_idle =
504 get_cpu_idle_time_us(i, &pcpu->target_set_time);
505 pcpu->hispeed_validate_time = pcpu->target_set_time;
510 * Set floor freq and (re)start timer for when last
514 pcpu->floor_freq = hispeed_freq;
515 pcpu->floor_validate_time = ktime_to_us(ktime_get());
518 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
521 wake_up_process(speedchange_task);
524 static ssize_t show_target_loads(
525 struct kobject *kobj, struct attribute *attr, char *buf)
530 spin_lock(&target_loads_lock);
532 for (i = 0; i < ntarget_loads; i++)
533 ret += sprintf(buf + ret, "%u%s", target_loads[i],
534 i & 0x1 ? ":" : " ");
536 ret += sprintf(buf + ret, "\n");
537 spin_unlock(&target_loads_lock);
541 static ssize_t store_target_loads(
542 struct kobject *kobj, struct attribute *attr, const char *buf,
547 unsigned int *new_target_loads = NULL;
552 while ((cp = strpbrk(cp + 1, " :")))
555 if (!(ntokens & 0x1))
558 new_target_loads = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
559 if (!new_target_loads) {
566 while (i < ntokens) {
567 if (sscanf(cp, "%u", &new_target_loads[i++]) != 1)
570 cp = strpbrk(cp, " :");
579 spin_lock(&target_loads_lock);
580 if (target_loads != default_target_loads)
582 target_loads = new_target_loads;
583 ntarget_loads = ntokens;
584 spin_unlock(&target_loads_lock);
590 kfree(new_target_loads);
594 static struct global_attr target_loads_attr =
595 __ATTR(target_loads, S_IRUGO | S_IWUSR,
596 show_target_loads, store_target_loads);
598 static ssize_t show_hispeed_freq(struct kobject *kobj,
599 struct attribute *attr, char *buf)
601 return sprintf(buf, "%u\n", hispeed_freq);
604 static ssize_t store_hispeed_freq(struct kobject *kobj,
605 struct attribute *attr, const char *buf,
609 long unsigned int val;
611 ret = strict_strtoul(buf, 0, &val);
618 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
619 show_hispeed_freq, store_hispeed_freq);
622 static ssize_t show_go_hispeed_load(struct kobject *kobj,
623 struct attribute *attr, char *buf)
625 return sprintf(buf, "%lu\n", go_hispeed_load);
628 static ssize_t store_go_hispeed_load(struct kobject *kobj,
629 struct attribute *attr, const char *buf, size_t count)
634 ret = strict_strtoul(buf, 0, &val);
637 go_hispeed_load = val;
641 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
642 show_go_hispeed_load, store_go_hispeed_load);
644 static ssize_t show_min_sample_time(struct kobject *kobj,
645 struct attribute *attr, char *buf)
647 return sprintf(buf, "%lu\n", min_sample_time);
650 static ssize_t store_min_sample_time(struct kobject *kobj,
651 struct attribute *attr, const char *buf, size_t count)
656 ret = strict_strtoul(buf, 0, &val);
659 min_sample_time = val;
663 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
664 show_min_sample_time, store_min_sample_time);
666 static ssize_t show_above_hispeed_delay(struct kobject *kobj,
667 struct attribute *attr, char *buf)
669 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
672 static ssize_t store_above_hispeed_delay(struct kobject *kobj,
673 struct attribute *attr,
674 const char *buf, size_t count)
679 ret = strict_strtoul(buf, 0, &val);
682 above_hispeed_delay_val = val;
686 define_one_global_rw(above_hispeed_delay);
688 static ssize_t show_timer_rate(struct kobject *kobj,
689 struct attribute *attr, char *buf)
691 return sprintf(buf, "%lu\n", timer_rate);
694 static ssize_t store_timer_rate(struct kobject *kobj,
695 struct attribute *attr, const char *buf, size_t count)
700 ret = strict_strtoul(buf, 0, &val);
707 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
708 show_timer_rate, store_timer_rate);
710 static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
713 return sprintf(buf, "%d\n", boost_val);
716 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
717 const char *buf, size_t count)
722 ret = kstrtoul(buf, 0, &val);
729 trace_cpufreq_interactive_boost("on");
730 cpufreq_interactive_boost();
732 trace_cpufreq_interactive_unboost("off");
738 define_one_global_rw(boost);
740 static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
741 const char *buf, size_t count)
746 ret = kstrtoul(buf, 0, &val);
750 trace_cpufreq_interactive_boost("pulse");
751 cpufreq_interactive_boost();
755 static struct global_attr boostpulse =
756 __ATTR(boostpulse, 0200, NULL, store_boostpulse);
758 static struct attribute *interactive_attributes[] = {
759 &target_loads_attr.attr,
760 &hispeed_freq_attr.attr,
761 &go_hispeed_load_attr.attr,
762 &above_hispeed_delay.attr,
763 &min_sample_time_attr.attr,
764 &timer_rate_attr.attr,
770 static struct attribute_group interactive_attr_group = {
771 .attrs = interactive_attributes,
772 .name = "interactive",
775 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
781 cpufreq_interactive_idle_start();
784 cpufreq_interactive_idle_end();
791 static struct notifier_block cpufreq_interactive_idle_nb = {
792 .notifier_call = cpufreq_interactive_idle_notifier,
795 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
800 struct cpufreq_interactive_cpuinfo *pcpu;
801 struct cpufreq_frequency_table *freq_table;
804 case CPUFREQ_GOV_START:
805 if (!cpu_online(policy->cpu))
809 cpufreq_frequency_get_table(policy->cpu);
811 hispeed_freq = policy->max;
813 for_each_cpu(j, policy->cpus) {
814 pcpu = &per_cpu(cpuinfo, j);
815 pcpu->policy = policy;
816 pcpu->target_freq = policy->cur;
817 pcpu->freq_table = freq_table;
818 pcpu->target_set_time_in_idle =
819 get_cpu_idle_time_us(j,
820 &pcpu->target_set_time);
821 pcpu->floor_freq = pcpu->target_freq;
822 pcpu->floor_validate_time =
823 pcpu->target_set_time;
824 pcpu->hispeed_validate_time =
825 pcpu->target_set_time;
826 pcpu->governor_enabled = 1;
828 pcpu->cpu_timer.expires =
829 jiffies + usecs_to_jiffies(timer_rate);
830 add_timer_on(&pcpu->cpu_timer, j);
834 * Do not register the idle hook and create sysfs
835 * entries if we have already done so.
837 if (atomic_inc_return(&active_count) > 1)
840 rc = sysfs_create_group(cpufreq_global_kobject,
841 &interactive_attr_group);
845 idle_notifier_register(&cpufreq_interactive_idle_nb);
848 case CPUFREQ_GOV_STOP:
849 for_each_cpu(j, policy->cpus) {
850 pcpu = &per_cpu(cpuinfo, j);
851 pcpu->governor_enabled = 0;
853 del_timer_sync(&pcpu->cpu_timer);
856 if (atomic_dec_return(&active_count) > 0)
859 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
860 sysfs_remove_group(cpufreq_global_kobject,
861 &interactive_attr_group);
865 case CPUFREQ_GOV_LIMITS:
866 if (policy->max < policy->cur)
867 __cpufreq_driver_target(policy,
868 policy->max, CPUFREQ_RELATION_H);
869 else if (policy->min > policy->cur)
870 __cpufreq_driver_target(policy,
871 policy->min, CPUFREQ_RELATION_L);
877 static int __init cpufreq_interactive_init(void)
880 struct cpufreq_interactive_cpuinfo *pcpu;
881 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
883 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
884 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
885 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
886 timer_rate = DEFAULT_TIMER_RATE;
888 /* Initalize per-cpu timers */
889 for_each_possible_cpu(i) {
890 pcpu = &per_cpu(cpuinfo, i);
892 init_timer(&pcpu->cpu_timer);
894 init_timer_deferrable(&pcpu->cpu_timer);
895 pcpu->cpu_timer.function = cpufreq_interactive_timer;
896 pcpu->cpu_timer.data = i;
899 spin_lock_init(&target_loads_lock);
900 spin_lock_init(&speedchange_cpumask_lock);
902 kthread_create(cpufreq_interactive_speedchange_task, NULL,
904 if (IS_ERR(speedchange_task))
905 return PTR_ERR(speedchange_task);
907 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
908 get_task_struct(speedchange_task);
910 /* NB: wake up so the thread does not look hung to the freezer */
911 wake_up_process(speedchange_task);
913 return cpufreq_register_governor(&cpufreq_gov_interactive);
916 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
917 fs_initcall(cpufreq_interactive_init);
919 module_init(cpufreq_interactive_init);
922 static void __exit cpufreq_interactive_exit(void)
924 cpufreq_unregister_governor(&cpufreq_gov_interactive);
925 kthread_stop(speedchange_task);
926 put_task_struct(speedchange_task);
929 module_exit(cpufreq_interactive_exit);
931 MODULE_AUTHOR("Mike Chan <mike@android.com>");
932 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
933 "Latency sensitive workloads");
934 MODULE_LICENSE("GPL");