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/rwsem.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/slab.h>
33 #include "cpufreq_governor.h"
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/cpufreq_interactive.h>
38 struct cpufreq_interactive_cpuinfo {
39 struct timer_list cpu_timer;
40 struct timer_list cpu_slack_timer;
41 spinlock_t load_lock; /* protects the next 4 fields */
43 u64 time_in_idle_timestamp;
45 u64 cputime_speedadj_timestamp;
46 struct cpufreq_policy *policy;
47 struct cpufreq_frequency_table *freq_table;
48 unsigned int target_freq;
49 unsigned int floor_freq;
50 u64 floor_validate_time;
51 u64 hispeed_validate_time;
52 struct rw_semaphore enable_sem;
56 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
58 /* realtime thread handles frequency scaling */
59 static struct task_struct *speedchange_task;
60 static cpumask_t speedchange_cpumask;
61 static spinlock_t speedchange_cpumask_lock;
62 static struct mutex gov_lock;
64 /* Target load. Lower values result in higher CPU speeds. */
65 #define DEFAULT_TARGET_LOAD 90
66 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
68 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
69 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
70 static unsigned int default_above_hispeed_delay[] = {
71 DEFAULT_ABOVE_HISPEED_DELAY };
73 struct cpufreq_interactive_tunables {
75 /* Hi speed to bump to from lo speed when load burst (default max) */
76 unsigned int hispeed_freq;
77 /* Go to hi speed when CPU load at or above this value. */
78 #define DEFAULT_GO_HISPEED_LOAD 99
79 unsigned long go_hispeed_load;
80 /* Target load. Lower values result in higher CPU speeds. */
81 spinlock_t target_loads_lock;
82 unsigned int *target_loads;
85 * The minimum amount of time to spend at a frequency before we can ramp
88 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
89 unsigned long min_sample_time;
91 * The sample rate of the timer used to increase frequency
93 unsigned long timer_rate;
95 * Wait this long before raising speed above hispeed, by default a
96 * single timer interval.
98 spinlock_t above_hispeed_delay_lock;
99 unsigned int *above_hispeed_delay;
100 int nabove_hispeed_delay;
101 /* Non-zero means indefinite speed boost active */
103 /* Duration of a boot pulse in usecs */
104 int boostpulse_duration_val;
105 /* End time of boost pulse in ktime converted to usecs */
106 u64 boostpulse_endtime;
108 * Max additional time to wait in idle, beyond timer_rate, at speeds
109 * above minimum before wakeup to reduce speed, or -1 if unnecessary.
111 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
116 /* For cases where we have single governor instance for system */
117 struct cpufreq_interactive_tunables *common_tunables;
119 static struct attribute_group *get_sysfs_attr(void);
121 static void cpufreq_interactive_timer_resched(
122 struct cpufreq_interactive_cpuinfo *pcpu)
124 struct cpufreq_interactive_tunables *tunables =
125 pcpu->policy->governor_data;
126 unsigned long expires;
129 spin_lock_irqsave(&pcpu->load_lock, flags);
131 get_cpu_idle_time(smp_processor_id(),
132 &pcpu->time_in_idle_timestamp,
133 tunables->io_is_busy);
134 pcpu->cputime_speedadj = 0;
135 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
136 expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
137 mod_timer_pinned(&pcpu->cpu_timer, expires);
139 if (tunables->timer_slack_val >= 0 &&
140 pcpu->target_freq > pcpu->policy->min) {
141 expires += usecs_to_jiffies(tunables->timer_slack_val);
142 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
145 spin_unlock_irqrestore(&pcpu->load_lock, flags);
148 /* The caller shall take enable_sem write semaphore to avoid any timer race.
149 * The cpu_timer and cpu_slack_timer must be deactivated when calling this
152 static void cpufreq_interactive_timer_start(
153 struct cpufreq_interactive_tunables *tunables, int cpu)
155 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
156 unsigned long expires = jiffies +
157 usecs_to_jiffies(tunables->timer_rate);
160 pcpu->cpu_timer.expires = expires;
161 add_timer_on(&pcpu->cpu_timer, cpu);
162 if (tunables->timer_slack_val >= 0 &&
163 pcpu->target_freq > pcpu->policy->min) {
164 expires += usecs_to_jiffies(tunables->timer_slack_val);
165 pcpu->cpu_slack_timer.expires = expires;
166 add_timer_on(&pcpu->cpu_slack_timer, cpu);
169 spin_lock_irqsave(&pcpu->load_lock, flags);
171 get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
172 tunables->io_is_busy);
173 pcpu->cputime_speedadj = 0;
174 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
175 spin_unlock_irqrestore(&pcpu->load_lock, flags);
178 static unsigned int freq_to_above_hispeed_delay(
179 struct cpufreq_interactive_tunables *tunables,
186 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
188 for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
189 freq >= tunables->above_hispeed_delay[i+1]; i += 2)
192 ret = tunables->above_hispeed_delay[i];
193 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
197 static unsigned int freq_to_targetload(
198 struct cpufreq_interactive_tunables *tunables, unsigned int freq)
204 spin_lock_irqsave(&tunables->target_loads_lock, flags);
206 for (i = 0; i < tunables->ntarget_loads - 1 &&
207 freq >= tunables->target_loads[i+1]; i += 2)
210 ret = tunables->target_loads[i];
211 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
216 * If increasing frequencies never map to a lower target load then
217 * choose_freq() will find the minimum frequency that does not exceed its
218 * target load given the current load.
220 static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
221 unsigned int loadadjfreq)
223 unsigned int freq = pcpu->policy->cur;
224 unsigned int prevfreq, freqmin, freqmax;
233 tl = freq_to_targetload(pcpu->policy->governor_data, freq);
236 * Find the lowest frequency where the computed load is less
237 * than or equal to the target load.
240 if (cpufreq_frequency_table_target(
241 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
242 CPUFREQ_RELATION_L, &index))
244 freq = pcpu->freq_table[index].frequency;
246 if (freq > prevfreq) {
247 /* The previous frequency is too low. */
250 if (freq >= freqmax) {
252 * Find the highest frequency that is less
255 if (cpufreq_frequency_table_target(
256 pcpu->policy, pcpu->freq_table,
257 freqmax - 1, CPUFREQ_RELATION_H,
260 freq = pcpu->freq_table[index].frequency;
262 if (freq == freqmin) {
264 * The first frequency below freqmax
265 * has already been found to be too
266 * low. freqmax is the lowest speed
267 * we found that is fast enough.
273 } else if (freq < prevfreq) {
274 /* The previous frequency is high enough. */
277 if (freq <= freqmin) {
279 * Find the lowest frequency that is higher
282 if (cpufreq_frequency_table_target(
283 pcpu->policy, pcpu->freq_table,
284 freqmin + 1, CPUFREQ_RELATION_L,
287 freq = pcpu->freq_table[index].frequency;
290 * If freqmax is the first frequency above
291 * freqmin then we have already found that
292 * this speed is fast enough.
299 /* If same frequency chosen as previous then done. */
300 } while (freq != prevfreq);
305 static u64 update_load(int cpu)
307 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
308 struct cpufreq_interactive_tunables *tunables =
309 pcpu->policy->governor_data;
316 now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
317 delta_idle = (now_idle - pcpu->time_in_idle);
318 delta_time = (now - pcpu->time_in_idle_timestamp);
320 if (delta_time <= delta_idle)
323 active_time = delta_time - delta_idle;
325 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
327 pcpu->time_in_idle = now_idle;
328 pcpu->time_in_idle_timestamp = now;
332 static void cpufreq_interactive_timer(unsigned long data)
335 unsigned int delta_time;
336 u64 cputime_speedadj;
338 struct cpufreq_interactive_cpuinfo *pcpu =
339 &per_cpu(cpuinfo, data);
340 struct cpufreq_interactive_tunables *tunables =
341 pcpu->policy->governor_data;
342 unsigned int new_freq;
343 unsigned int loadadjfreq;
348 if (!down_read_trylock(&pcpu->enable_sem))
350 if (!pcpu->governor_enabled)
353 spin_lock_irqsave(&pcpu->load_lock, flags);
354 now = update_load(data);
355 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
356 cputime_speedadj = pcpu->cputime_speedadj;
357 spin_unlock_irqrestore(&pcpu->load_lock, flags);
359 if (WARN_ON_ONCE(!delta_time))
362 do_div(cputime_speedadj, delta_time);
363 loadadjfreq = (unsigned int)cputime_speedadj * 100;
364 cpu_load = loadadjfreq / pcpu->target_freq;
365 boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
367 if (cpu_load >= tunables->go_hispeed_load || boosted) {
368 if (pcpu->target_freq < tunables->hispeed_freq) {
369 new_freq = tunables->hispeed_freq;
371 new_freq = choose_freq(pcpu, loadadjfreq);
373 if (new_freq < tunables->hispeed_freq)
374 new_freq = tunables->hispeed_freq;
377 new_freq = choose_freq(pcpu, loadadjfreq);
380 if (pcpu->target_freq >= tunables->hispeed_freq &&
381 new_freq > pcpu->target_freq &&
382 now - pcpu->hispeed_validate_time <
383 freq_to_above_hispeed_delay(tunables, pcpu->target_freq)) {
384 trace_cpufreq_interactive_notyet(
385 data, cpu_load, pcpu->target_freq,
386 pcpu->policy->cur, new_freq);
390 pcpu->hispeed_validate_time = now;
392 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
393 new_freq, CPUFREQ_RELATION_L,
397 new_freq = pcpu->freq_table[index].frequency;
400 * Do not scale below floor_freq unless we have been at or above the
401 * floor frequency for the minimum sample time since last validated.
403 if (new_freq < pcpu->floor_freq) {
404 if (now - pcpu->floor_validate_time <
405 tunables->min_sample_time) {
406 trace_cpufreq_interactive_notyet(
407 data, cpu_load, pcpu->target_freq,
408 pcpu->policy->cur, new_freq);
414 * Update the timestamp for checking whether speed has been held at
415 * or above the selected frequency for a minimum of min_sample_time,
416 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
417 * allow the speed to drop as soon as the boostpulse duration expires
418 * (or the indefinite boost is turned off).
421 if (!boosted || new_freq > tunables->hispeed_freq) {
422 pcpu->floor_freq = new_freq;
423 pcpu->floor_validate_time = now;
426 if (pcpu->target_freq == new_freq) {
427 trace_cpufreq_interactive_already(
428 data, cpu_load, pcpu->target_freq,
429 pcpu->policy->cur, new_freq);
430 goto rearm_if_notmax;
433 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
434 pcpu->policy->cur, new_freq);
436 pcpu->target_freq = new_freq;
437 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
438 cpumask_set_cpu(data, &speedchange_cpumask);
439 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
440 wake_up_process(speedchange_task);
444 * Already set max speed and don't see a need to change that,
445 * wait until next idle to re-evaluate, don't need timer.
447 if (pcpu->target_freq == pcpu->policy->max)
451 if (!timer_pending(&pcpu->cpu_timer))
452 cpufreq_interactive_timer_resched(pcpu);
455 up_read(&pcpu->enable_sem);
459 static void cpufreq_interactive_idle_start(void)
461 struct cpufreq_interactive_cpuinfo *pcpu =
462 &per_cpu(cpuinfo, smp_processor_id());
465 if (!down_read_trylock(&pcpu->enable_sem))
467 if (!pcpu->governor_enabled) {
468 up_read(&pcpu->enable_sem);
472 pending = timer_pending(&pcpu->cpu_timer);
474 if (pcpu->target_freq != pcpu->policy->min) {
476 * Entering idle while not at lowest speed. On some
477 * platforms this can hold the other CPU(s) at that speed
478 * even though the CPU is idle. Set a timer to re-evaluate
479 * speed so this idle CPU doesn't hold the other CPUs above
480 * min indefinitely. This should probably be a quirk of
481 * the CPUFreq driver.
484 cpufreq_interactive_timer_resched(pcpu);
487 up_read(&pcpu->enable_sem);
490 static void cpufreq_interactive_idle_end(void)
492 struct cpufreq_interactive_cpuinfo *pcpu =
493 &per_cpu(cpuinfo, smp_processor_id());
495 if (!down_read_trylock(&pcpu->enable_sem))
497 if (!pcpu->governor_enabled) {
498 up_read(&pcpu->enable_sem);
502 /* Arm the timer for 1-2 ticks later if not already. */
503 if (!timer_pending(&pcpu->cpu_timer)) {
504 cpufreq_interactive_timer_resched(pcpu);
505 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
506 del_timer(&pcpu->cpu_timer);
507 del_timer(&pcpu->cpu_slack_timer);
508 cpufreq_interactive_timer(smp_processor_id());
511 up_read(&pcpu->enable_sem);
514 static int cpufreq_interactive_speedchange_task(void *data)
519 struct cpufreq_interactive_cpuinfo *pcpu;
522 set_current_state(TASK_INTERRUPTIBLE);
523 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
525 if (cpumask_empty(&speedchange_cpumask)) {
526 spin_unlock_irqrestore(&speedchange_cpumask_lock,
530 if (kthread_should_stop())
533 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
536 set_current_state(TASK_RUNNING);
537 tmp_mask = speedchange_cpumask;
538 cpumask_clear(&speedchange_cpumask);
539 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
541 for_each_cpu(cpu, &tmp_mask) {
543 unsigned int max_freq = 0;
545 pcpu = &per_cpu(cpuinfo, cpu);
546 if (!down_read_trylock(&pcpu->enable_sem))
548 if (!pcpu->governor_enabled) {
549 up_read(&pcpu->enable_sem);
553 for_each_cpu(j, pcpu->policy->cpus) {
554 struct cpufreq_interactive_cpuinfo *pjcpu =
555 &per_cpu(cpuinfo, j);
557 if (pjcpu->target_freq > max_freq)
558 max_freq = pjcpu->target_freq;
561 if (max_freq != pcpu->policy->cur)
562 __cpufreq_driver_target(pcpu->policy,
565 trace_cpufreq_interactive_setspeed(cpu,
569 up_read(&pcpu->enable_sem);
576 static void cpufreq_interactive_boost(void)
581 struct cpufreq_interactive_cpuinfo *pcpu;
582 struct cpufreq_interactive_tunables *tunables;
584 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
586 for_each_online_cpu(i) {
587 pcpu = &per_cpu(cpuinfo, i);
588 tunables = pcpu->policy->governor_data;
590 if (pcpu->target_freq < tunables->hispeed_freq) {
591 pcpu->target_freq = tunables->hispeed_freq;
592 cpumask_set_cpu(i, &speedchange_cpumask);
593 pcpu->hispeed_validate_time =
594 ktime_to_us(ktime_get());
599 * Set floor freq and (re)start timer for when last
603 pcpu->floor_freq = tunables->hispeed_freq;
604 pcpu->floor_validate_time = ktime_to_us(ktime_get());
607 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
610 wake_up_process(speedchange_task);
613 static int cpufreq_interactive_notifier(
614 struct notifier_block *nb, unsigned long val, void *data)
616 struct cpufreq_freqs *freq = data;
617 struct cpufreq_interactive_cpuinfo *pcpu;
621 if (val == CPUFREQ_POSTCHANGE) {
622 pcpu = &per_cpu(cpuinfo, freq->cpu);
623 if (!down_read_trylock(&pcpu->enable_sem))
625 if (!pcpu->governor_enabled) {
626 up_read(&pcpu->enable_sem);
630 for_each_cpu(cpu, pcpu->policy->cpus) {
631 struct cpufreq_interactive_cpuinfo *pjcpu =
632 &per_cpu(cpuinfo, cpu);
633 if (cpu != freq->cpu) {
634 if (!down_read_trylock(&pjcpu->enable_sem))
636 if (!pjcpu->governor_enabled) {
637 up_read(&pjcpu->enable_sem);
641 spin_lock_irqsave(&pjcpu->load_lock, flags);
643 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
644 if (cpu != freq->cpu)
645 up_read(&pjcpu->enable_sem);
648 up_read(&pcpu->enable_sem);
653 static struct notifier_block cpufreq_notifier_block = {
654 .notifier_call = cpufreq_interactive_notifier,
657 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
662 unsigned int *tokenized_data;
666 while ((cp = strpbrk(cp + 1, " :")))
669 if (!(ntokens & 0x1))
672 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
673 if (!tokenized_data) {
680 while (i < ntokens) {
681 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
684 cp = strpbrk(cp, " :");
693 *num_tokens = ntokens;
694 return tokenized_data;
697 kfree(tokenized_data);
702 static ssize_t show_target_loads(
703 struct cpufreq_interactive_tunables *tunables,
710 spin_lock_irqsave(&tunables->target_loads_lock, flags);
712 for (i = 0; i < tunables->ntarget_loads; i++)
713 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
714 i & 0x1 ? ":" : " ");
716 ret += sprintf(buf + --ret, "\n");
717 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
721 static ssize_t store_target_loads(
722 struct cpufreq_interactive_tunables *tunables,
723 const char *buf, size_t count)
726 unsigned int *new_target_loads = NULL;
729 new_target_loads = get_tokenized_data(buf, &ntokens);
730 if (IS_ERR(new_target_loads))
731 return PTR_RET(new_target_loads);
733 spin_lock_irqsave(&tunables->target_loads_lock, flags);
734 if (tunables->target_loads != default_target_loads)
735 kfree(tunables->target_loads);
736 tunables->target_loads = new_target_loads;
737 tunables->ntarget_loads = ntokens;
738 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
742 static ssize_t show_above_hispeed_delay(
743 struct cpufreq_interactive_tunables *tunables, char *buf)
749 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
751 for (i = 0; i < tunables->nabove_hispeed_delay; i++)
752 ret += sprintf(buf + ret, "%u%s",
753 tunables->above_hispeed_delay[i],
754 i & 0x1 ? ":" : " ");
756 ret += sprintf(buf + --ret, "\n");
757 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
761 static ssize_t store_above_hispeed_delay(
762 struct cpufreq_interactive_tunables *tunables,
763 const char *buf, size_t count)
766 unsigned int *new_above_hispeed_delay = NULL;
769 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
770 if (IS_ERR(new_above_hispeed_delay))
771 return PTR_RET(new_above_hispeed_delay);
773 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
774 if (tunables->above_hispeed_delay != default_above_hispeed_delay)
775 kfree(tunables->above_hispeed_delay);
776 tunables->above_hispeed_delay = new_above_hispeed_delay;
777 tunables->nabove_hispeed_delay = ntokens;
778 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
783 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
786 return sprintf(buf, "%u\n", tunables->hispeed_freq);
789 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
790 const char *buf, size_t count)
793 long unsigned int val;
795 ret = strict_strtoul(buf, 0, &val);
798 tunables->hispeed_freq = val;
802 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
803 *tunables, char *buf)
805 return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
808 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
809 *tunables, const char *buf, size_t count)
814 ret = strict_strtoul(buf, 0, &val);
817 tunables->go_hispeed_load = val;
821 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
822 *tunables, char *buf)
824 return sprintf(buf, "%lu\n", tunables->min_sample_time);
827 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
828 *tunables, const char *buf, size_t count)
833 ret = strict_strtoul(buf, 0, &val);
836 tunables->min_sample_time = val;
840 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
843 return sprintf(buf, "%lu\n", tunables->timer_rate);
846 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
847 const char *buf, size_t count)
852 ret = strict_strtoul(buf, 0, &val);
855 tunables->timer_rate = val;
859 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
862 return sprintf(buf, "%d\n", tunables->timer_slack_val);
865 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
866 const char *buf, size_t count)
871 ret = kstrtol(buf, 10, &val);
875 tunables->timer_slack_val = val;
879 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
882 return sprintf(buf, "%d\n", tunables->boost_val);
885 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
886 const char *buf, size_t count)
891 ret = kstrtoul(buf, 0, &val);
895 tunables->boost_val = val;
897 if (tunables->boost_val) {
898 trace_cpufreq_interactive_boost("on");
899 cpufreq_interactive_boost();
901 trace_cpufreq_interactive_unboost("off");
907 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
908 const char *buf, size_t count)
913 ret = kstrtoul(buf, 0, &val);
917 tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
918 tunables->boostpulse_duration_val;
919 trace_cpufreq_interactive_boost("pulse");
920 cpufreq_interactive_boost();
924 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
925 *tunables, char *buf)
927 return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
930 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
931 *tunables, const char *buf, size_t count)
936 ret = kstrtoul(buf, 0, &val);
940 tunables->boostpulse_duration_val = val;
944 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
947 return sprintf(buf, "%u\n", tunables->io_is_busy);
950 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
951 const char *buf, size_t count)
956 ret = kstrtoul(buf, 0, &val);
959 tunables->io_is_busy = val;
964 * Create show/store routines
965 * - sys: One governor instance for complete SYSTEM
966 * - pol: One governor instance per struct cpufreq_policy
968 #define show_gov_pol_sys(file_name) \
969 static ssize_t show_##file_name##_gov_sys \
970 (struct kobject *kobj, struct attribute *attr, char *buf) \
972 return show_##file_name(common_tunables, buf); \
975 static ssize_t show_##file_name##_gov_pol \
976 (struct cpufreq_policy *policy, char *buf) \
978 return show_##file_name(policy->governor_data, buf); \
981 #define store_gov_pol_sys(file_name) \
982 static ssize_t store_##file_name##_gov_sys \
983 (struct kobject *kobj, struct attribute *attr, const char *buf, \
986 return store_##file_name(common_tunables, buf, count); \
989 static ssize_t store_##file_name##_gov_pol \
990 (struct cpufreq_policy *policy, const char *buf, size_t count) \
992 return store_##file_name(policy->governor_data, buf, count); \
995 #define show_store_gov_pol_sys(file_name) \
996 show_gov_pol_sys(file_name); \
997 store_gov_pol_sys(file_name)
999 show_store_gov_pol_sys(target_loads);
1000 show_store_gov_pol_sys(above_hispeed_delay);
1001 show_store_gov_pol_sys(hispeed_freq);
1002 show_store_gov_pol_sys(go_hispeed_load);
1003 show_store_gov_pol_sys(min_sample_time);
1004 show_store_gov_pol_sys(timer_rate);
1005 show_store_gov_pol_sys(timer_slack);
1006 show_store_gov_pol_sys(boost);
1007 store_gov_pol_sys(boostpulse);
1008 show_store_gov_pol_sys(boostpulse_duration);
1009 show_store_gov_pol_sys(io_is_busy);
1011 #define gov_sys_attr_rw(_name) \
1012 static struct global_attr _name##_gov_sys = \
1013 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1015 #define gov_pol_attr_rw(_name) \
1016 static struct freq_attr _name##_gov_pol = \
1017 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1019 #define gov_sys_pol_attr_rw(_name) \
1020 gov_sys_attr_rw(_name); \
1021 gov_pol_attr_rw(_name)
1023 gov_sys_pol_attr_rw(target_loads);
1024 gov_sys_pol_attr_rw(above_hispeed_delay);
1025 gov_sys_pol_attr_rw(hispeed_freq);
1026 gov_sys_pol_attr_rw(go_hispeed_load);
1027 gov_sys_pol_attr_rw(min_sample_time);
1028 gov_sys_pol_attr_rw(timer_rate);
1029 gov_sys_pol_attr_rw(timer_slack);
1030 gov_sys_pol_attr_rw(boost);
1031 gov_sys_pol_attr_rw(boostpulse_duration);
1032 gov_sys_pol_attr_rw(io_is_busy);
1034 static struct global_attr boostpulse_gov_sys =
1035 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1037 static struct freq_attr boostpulse_gov_pol =
1038 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1040 /* One Governor instance for entire system */
1041 static struct attribute *interactive_attributes_gov_sys[] = {
1042 &target_loads_gov_sys.attr,
1043 &above_hispeed_delay_gov_sys.attr,
1044 &hispeed_freq_gov_sys.attr,
1045 &go_hispeed_load_gov_sys.attr,
1046 &min_sample_time_gov_sys.attr,
1047 &timer_rate_gov_sys.attr,
1048 &timer_slack_gov_sys.attr,
1049 &boost_gov_sys.attr,
1050 &boostpulse_gov_sys.attr,
1051 &boostpulse_duration_gov_sys.attr,
1052 &io_is_busy_gov_sys.attr,
1056 static struct attribute_group interactive_attr_group_gov_sys = {
1057 .attrs = interactive_attributes_gov_sys,
1058 .name = "interactive",
1061 /* Per policy governor instance */
1062 static struct attribute *interactive_attributes_gov_pol[] = {
1063 &target_loads_gov_pol.attr,
1064 &above_hispeed_delay_gov_pol.attr,
1065 &hispeed_freq_gov_pol.attr,
1066 &go_hispeed_load_gov_pol.attr,
1067 &min_sample_time_gov_pol.attr,
1068 &timer_rate_gov_pol.attr,
1069 &timer_slack_gov_pol.attr,
1070 &boost_gov_pol.attr,
1071 &boostpulse_gov_pol.attr,
1072 &boostpulse_duration_gov_pol.attr,
1073 &io_is_busy_gov_pol.attr,
1077 static struct attribute_group interactive_attr_group_gov_pol = {
1078 .attrs = interactive_attributes_gov_pol,
1079 .name = "interactive",
1082 static struct attribute_group *get_sysfs_attr(void)
1084 if (have_governor_per_policy())
1085 return &interactive_attr_group_gov_pol;
1087 return &interactive_attr_group_gov_sys;
1090 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1096 cpufreq_interactive_idle_start();
1099 cpufreq_interactive_idle_end();
1106 static struct notifier_block cpufreq_interactive_idle_nb = {
1107 .notifier_call = cpufreq_interactive_idle_notifier,
1110 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1115 struct cpufreq_interactive_cpuinfo *pcpu;
1116 struct cpufreq_frequency_table *freq_table;
1117 struct cpufreq_interactive_tunables *tunables;
1119 if (have_governor_per_policy())
1120 tunables = policy->governor_data;
1122 tunables = common_tunables;
1124 WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1127 case CPUFREQ_GOV_POLICY_INIT:
1128 if (have_governor_per_policy()) {
1130 } else if (tunables) {
1131 tunables->usage_count++;
1132 policy->governor_data = tunables;
1136 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1138 pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1142 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1149 tunables->usage_count = 1;
1150 tunables->above_hispeed_delay = default_above_hispeed_delay;
1151 tunables->nabove_hispeed_delay =
1152 ARRAY_SIZE(default_above_hispeed_delay);
1153 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1154 tunables->target_loads = default_target_loads;
1155 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1156 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1157 tunables->timer_rate = DEFAULT_TIMER_RATE;
1158 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1159 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1161 spin_lock_init(&tunables->target_loads_lock);
1162 spin_lock_init(&tunables->above_hispeed_delay_lock);
1164 if (!policy->governor->initialized) {
1165 idle_notifier_register(&cpufreq_interactive_idle_nb);
1166 cpufreq_register_notifier(&cpufreq_notifier_block,
1167 CPUFREQ_TRANSITION_NOTIFIER);
1170 policy->governor_data = tunables;
1171 if (!have_governor_per_policy())
1172 common_tunables = tunables;
1176 case CPUFREQ_GOV_POLICY_EXIT:
1177 if (!--tunables->usage_count) {
1178 if (policy->governor->initialized == 1) {
1179 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1180 CPUFREQ_TRANSITION_NOTIFIER);
1181 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1184 sysfs_remove_group(get_governor_parent_kobj(policy),
1187 common_tunables = NULL;
1190 policy->governor_data = NULL;
1193 case CPUFREQ_GOV_START:
1194 mutex_lock(&gov_lock);
1196 freq_table = cpufreq_frequency_get_table(policy->cpu);
1197 if (!tunables->hispeed_freq)
1198 tunables->hispeed_freq = policy->max;
1200 for_each_cpu(j, policy->cpus) {
1201 pcpu = &per_cpu(cpuinfo, j);
1202 pcpu->policy = policy;
1203 pcpu->target_freq = policy->cur;
1204 pcpu->freq_table = freq_table;
1205 pcpu->floor_freq = pcpu->target_freq;
1206 pcpu->floor_validate_time =
1207 ktime_to_us(ktime_get());
1208 pcpu->hispeed_validate_time =
1209 pcpu->floor_validate_time;
1210 down_write(&pcpu->enable_sem);
1211 del_timer_sync(&pcpu->cpu_timer);
1212 del_timer_sync(&pcpu->cpu_slack_timer);
1213 cpufreq_interactive_timer_start(tunables, j);
1214 pcpu->governor_enabled = 1;
1215 up_write(&pcpu->enable_sem);
1218 mutex_unlock(&gov_lock);
1221 case CPUFREQ_GOV_STOP:
1222 mutex_lock(&gov_lock);
1223 for_each_cpu(j, policy->cpus) {
1224 pcpu = &per_cpu(cpuinfo, j);
1225 down_write(&pcpu->enable_sem);
1226 pcpu->governor_enabled = 0;
1227 del_timer_sync(&pcpu->cpu_timer);
1228 del_timer_sync(&pcpu->cpu_slack_timer);
1229 up_write(&pcpu->enable_sem);
1232 mutex_unlock(&gov_lock);
1235 case CPUFREQ_GOV_LIMITS:
1236 if (policy->max < policy->cur)
1237 __cpufreq_driver_target(policy,
1238 policy->max, CPUFREQ_RELATION_H);
1239 else if (policy->min > policy->cur)
1240 __cpufreq_driver_target(policy,
1241 policy->min, CPUFREQ_RELATION_L);
1242 for_each_cpu(j, policy->cpus) {
1243 pcpu = &per_cpu(cpuinfo, j);
1245 /* hold write semaphore to avoid race */
1246 down_write(&pcpu->enable_sem);
1247 if (pcpu->governor_enabled == 0) {
1248 up_write(&pcpu->enable_sem);
1252 /* update target_freq firstly */
1253 if (policy->max < pcpu->target_freq)
1254 pcpu->target_freq = policy->max;
1255 else if (policy->min > pcpu->target_freq)
1256 pcpu->target_freq = policy->min;
1258 /* Reschedule timer.
1259 * Delete the timers, else the timer callback may
1260 * return without re-arm the timer when failed
1261 * acquire the semaphore. This race may cause timer
1262 * stopped unexpectedly.
1264 del_timer_sync(&pcpu->cpu_timer);
1265 del_timer_sync(&pcpu->cpu_slack_timer);
1266 cpufreq_interactive_timer_start(tunables, j);
1267 up_write(&pcpu->enable_sem);
1274 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1277 struct cpufreq_governor cpufreq_gov_interactive = {
1278 .name = "interactive",
1279 .governor = cpufreq_governor_interactive,
1280 .max_transition_latency = 10000000,
1281 .owner = THIS_MODULE,
1284 static void cpufreq_interactive_nop_timer(unsigned long data)
1288 static int __init cpufreq_interactive_init(void)
1291 struct cpufreq_interactive_cpuinfo *pcpu;
1292 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1294 /* Initalize per-cpu timers */
1295 for_each_possible_cpu(i) {
1296 pcpu = &per_cpu(cpuinfo, i);
1297 init_timer_deferrable(&pcpu->cpu_timer);
1298 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1299 pcpu->cpu_timer.data = i;
1300 init_timer(&pcpu->cpu_slack_timer);
1301 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1302 spin_lock_init(&pcpu->load_lock);
1303 init_rwsem(&pcpu->enable_sem);
1306 spin_lock_init(&speedchange_cpumask_lock);
1307 mutex_init(&gov_lock);
1309 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1311 if (IS_ERR(speedchange_task))
1312 return PTR_ERR(speedchange_task);
1314 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1315 get_task_struct(speedchange_task);
1317 /* NB: wake up so the thread does not look hung to the freezer */
1318 wake_up_process(speedchange_task);
1320 return cpufreq_register_governor(&cpufreq_gov_interactive);
1323 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1324 fs_initcall(cpufreq_interactive_init);
1326 module_init(cpufreq_interactive_init);
1329 static void __exit cpufreq_interactive_exit(void)
1331 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1332 kthread_stop(speedchange_task);
1333 put_task_struct(speedchange_task);
1336 module_exit(cpufreq_interactive_exit);
1338 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1339 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1340 "Latency sensitive workloads");
1341 MODULE_LICENSE("GPL");