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 <linux/kernel_stat.h>
34 #include <asm/cputime.h>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/cpufreq_interactive.h>
39 struct cpufreq_interactive_cpuinfo {
40 struct timer_list cpu_timer;
41 struct timer_list cpu_slack_timer;
42 spinlock_t load_lock; /* protects the next 4 fields */
44 u64 time_in_idle_timestamp;
46 u64 cputime_speedadj_timestamp;
47 struct cpufreq_policy *policy;
48 struct cpufreq_frequency_table *freq_table;
49 spinlock_t target_freq_lock; /*protects target freq */
50 unsigned int target_freq;
51 unsigned int floor_freq;
52 unsigned int max_freq;
53 u64 floor_validate_time;
54 u64 hispeed_validate_time;
55 struct rw_semaphore enable_sem;
59 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
61 /* realtime thread handles frequency scaling */
62 static struct task_struct *speedchange_task;
63 static cpumask_t speedchange_cpumask;
64 static spinlock_t speedchange_cpumask_lock;
65 static struct mutex gov_lock;
67 /* Target load. Lower values result in higher CPU speeds. */
68 #define DEFAULT_TARGET_LOAD 90
69 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
71 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
72 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
73 static unsigned int default_above_hispeed_delay[] = {
74 DEFAULT_ABOVE_HISPEED_DELAY };
76 struct cpufreq_interactive_tunables {
78 /* Hi speed to bump to from lo speed when load burst (default max) */
79 unsigned int hispeed_freq;
80 /* Go to hi speed when CPU load at or above this value. */
81 #define DEFAULT_GO_HISPEED_LOAD 99
82 unsigned long go_hispeed_load;
83 /* Target load. Lower values result in higher CPU speeds. */
84 spinlock_t target_loads_lock;
85 unsigned int *target_loads;
88 * The minimum amount of time to spend at a frequency before we can ramp
91 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
92 unsigned long min_sample_time;
94 * The sample rate of the timer used to increase frequency
96 unsigned long timer_rate;
98 * Wait this long before raising speed above hispeed, by default a
99 * single timer interval.
101 spinlock_t above_hispeed_delay_lock;
102 unsigned int *above_hispeed_delay;
103 int nabove_hispeed_delay;
104 /* Non-zero means indefinite speed boost active */
106 /* Duration of a boot pulse in usecs */
107 int boostpulse_duration_val;
108 /* End time of boost pulse in ktime converted to usecs */
109 u64 boostpulse_endtime;
111 * Max additional time to wait in idle, beyond timer_rate, at speeds
112 * above minimum before wakeup to reduce speed, or -1 if unnecessary.
114 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
119 /* For cases where we have single governor instance for system */
120 struct cpufreq_interactive_tunables *common_tunables;
122 static struct attribute_group *get_sysfs_attr(void);
124 static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
131 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
133 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
134 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
135 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
136 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
137 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
138 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
140 idle_time = cur_wall_time - busy_time;
142 *wall = jiffies_to_usecs(cur_wall_time);
144 return jiffies_to_usecs(idle_time);
147 static inline cputime64_t get_cpu_idle_time(
152 u64 idle_time = get_cpu_idle_time_us(cpu, wall);
154 if (idle_time == -1ULL)
155 idle_time = get_cpu_idle_time_jiffy(cpu, wall);
156 else if (!io_is_busy)
157 idle_time += get_cpu_iowait_time_us(cpu, wall);
162 static void cpufreq_interactive_timer_resched(
163 struct cpufreq_interactive_cpuinfo *pcpu)
165 struct cpufreq_interactive_tunables *tunables =
166 pcpu->policy->governor_data;
167 unsigned long expires;
170 spin_lock_irqsave(&pcpu->load_lock, flags);
172 get_cpu_idle_time(smp_processor_id(),
173 &pcpu->time_in_idle_timestamp,
174 tunables->io_is_busy);
175 pcpu->cputime_speedadj = 0;
176 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
177 expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
178 mod_timer_pinned(&pcpu->cpu_timer, expires);
180 if (tunables->timer_slack_val >= 0 &&
181 pcpu->target_freq > pcpu->policy->min) {
182 expires += usecs_to_jiffies(tunables->timer_slack_val);
183 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
186 spin_unlock_irqrestore(&pcpu->load_lock, flags);
189 /* The caller shall take enable_sem write semaphore to avoid any timer race.
190 * The cpu_timer and cpu_slack_timer must be deactivated when calling this
193 static void cpufreq_interactive_timer_start(
194 struct cpufreq_interactive_tunables *tunables, int cpu)
196 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
197 unsigned long expires = jiffies +
198 usecs_to_jiffies(tunables->timer_rate);
201 pcpu->cpu_timer.expires = expires;
202 add_timer_on(&pcpu->cpu_timer, cpu);
203 if (tunables->timer_slack_val >= 0 &&
204 pcpu->target_freq > pcpu->policy->min) {
205 expires += usecs_to_jiffies(tunables->timer_slack_val);
206 pcpu->cpu_slack_timer.expires = expires;
207 add_timer_on(&pcpu->cpu_slack_timer, cpu);
210 spin_lock_irqsave(&pcpu->load_lock, flags);
212 get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
213 tunables->io_is_busy);
214 pcpu->cputime_speedadj = 0;
215 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
216 spin_unlock_irqrestore(&pcpu->load_lock, flags);
219 static unsigned int freq_to_above_hispeed_delay(
220 struct cpufreq_interactive_tunables *tunables,
227 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
229 for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
230 freq >= tunables->above_hispeed_delay[i+1]; i += 2)
233 ret = tunables->above_hispeed_delay[i];
234 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
238 static unsigned int freq_to_targetload(
239 struct cpufreq_interactive_tunables *tunables, unsigned int freq)
245 spin_lock_irqsave(&tunables->target_loads_lock, flags);
247 for (i = 0; i < tunables->ntarget_loads - 1 &&
248 freq >= tunables->target_loads[i+1]; i += 2)
251 ret = tunables->target_loads[i];
252 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
257 * If increasing frequencies never map to a lower target load then
258 * choose_freq() will find the minimum frequency that does not exceed its
259 * target load given the current load.
261 static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
262 unsigned int loadadjfreq)
264 unsigned int freq = pcpu->policy->cur;
265 unsigned int prevfreq, freqmin, freqmax;
274 tl = freq_to_targetload(pcpu->policy->governor_data, freq);
277 * Find the lowest frequency where the computed load is less
278 * than or equal to the target load.
281 if (cpufreq_frequency_table_target(
282 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
283 CPUFREQ_RELATION_L, &index))
285 freq = pcpu->freq_table[index].frequency;
287 if (freq > prevfreq) {
288 /* The previous frequency is too low. */
291 if (freq >= freqmax) {
293 * Find the highest frequency that is less
296 if (cpufreq_frequency_table_target(
297 pcpu->policy, pcpu->freq_table,
298 freqmax - 1, CPUFREQ_RELATION_H,
301 freq = pcpu->freq_table[index].frequency;
303 if (freq == freqmin) {
305 * The first frequency below freqmax
306 * has already been found to be too
307 * low. freqmax is the lowest speed
308 * we found that is fast enough.
314 } else if (freq < prevfreq) {
315 /* The previous frequency is high enough. */
318 if (freq <= freqmin) {
320 * Find the lowest frequency that is higher
323 if (cpufreq_frequency_table_target(
324 pcpu->policy, pcpu->freq_table,
325 freqmin + 1, CPUFREQ_RELATION_L,
328 freq = pcpu->freq_table[index].frequency;
331 * If freqmax is the first frequency above
332 * freqmin then we have already found that
333 * this speed is fast enough.
340 /* If same frequency chosen as previous then done. */
341 } while (freq != prevfreq);
346 static u64 update_load(int cpu)
348 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
349 struct cpufreq_interactive_tunables *tunables =
350 pcpu->policy->governor_data;
353 unsigned int delta_idle;
354 unsigned int delta_time;
357 now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
358 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
359 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
361 if (delta_time <= delta_idle)
364 active_time = delta_time - delta_idle;
366 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
368 pcpu->time_in_idle = now_idle;
369 pcpu->time_in_idle_timestamp = now;
373 static void cpufreq_interactive_timer(unsigned long data)
376 unsigned int delta_time;
377 u64 cputime_speedadj;
379 struct cpufreq_interactive_cpuinfo *pcpu =
380 &per_cpu(cpuinfo, data);
381 struct cpufreq_interactive_tunables *tunables =
382 pcpu->policy->governor_data;
383 unsigned int new_freq;
384 unsigned int loadadjfreq;
389 if (!down_read_trylock(&pcpu->enable_sem))
391 if (!pcpu->governor_enabled)
394 spin_lock_irqsave(&pcpu->load_lock, flags);
395 now = update_load(data);
396 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
397 cputime_speedadj = pcpu->cputime_speedadj;
398 spin_unlock_irqrestore(&pcpu->load_lock, flags);
400 if (WARN_ON_ONCE(!delta_time))
403 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
404 do_div(cputime_speedadj, delta_time);
405 loadadjfreq = (unsigned int)cputime_speedadj * 100;
406 cpu_load = loadadjfreq / pcpu->target_freq;
407 boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
409 if (cpu_load >= tunables->go_hispeed_load || boosted) {
410 if (pcpu->target_freq < tunables->hispeed_freq) {
411 new_freq = tunables->hispeed_freq;
413 new_freq = choose_freq(pcpu, loadadjfreq);
415 if (new_freq < tunables->hispeed_freq)
416 new_freq = tunables->hispeed_freq;
419 new_freq = choose_freq(pcpu, loadadjfreq);
422 if (pcpu->target_freq >= tunables->hispeed_freq &&
423 new_freq > pcpu->target_freq &&
424 now - pcpu->hispeed_validate_time <
425 freq_to_above_hispeed_delay(tunables, pcpu->target_freq)) {
426 trace_cpufreq_interactive_notyet(
427 data, cpu_load, pcpu->target_freq,
428 pcpu->policy->cur, new_freq);
429 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
433 pcpu->hispeed_validate_time = now;
435 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
436 new_freq, CPUFREQ_RELATION_L,
438 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
442 new_freq = pcpu->freq_table[index].frequency;
445 * Do not scale below floor_freq unless we have been at or above the
446 * floor frequency for the minimum sample time since last validated.
448 if (new_freq < pcpu->floor_freq) {
449 if (now - pcpu->floor_validate_time <
450 tunables->min_sample_time) {
451 trace_cpufreq_interactive_notyet(
452 data, cpu_load, pcpu->target_freq,
453 pcpu->policy->cur, new_freq);
454 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
460 * Update the timestamp for checking whether speed has been held at
461 * or above the selected frequency for a minimum of min_sample_time,
462 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
463 * allow the speed to drop as soon as the boostpulse duration expires
464 * (or the indefinite boost is turned off).
467 if (!boosted || new_freq > tunables->hispeed_freq) {
468 pcpu->floor_freq = new_freq;
469 pcpu->floor_validate_time = now;
472 if (pcpu->target_freq == new_freq) {
473 trace_cpufreq_interactive_already(
474 data, cpu_load, pcpu->target_freq,
475 pcpu->policy->cur, new_freq);
476 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
477 goto rearm_if_notmax;
480 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
481 pcpu->policy->cur, new_freq);
483 pcpu->target_freq = new_freq;
484 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
485 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
486 cpumask_set_cpu(data, &speedchange_cpumask);
487 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
488 wake_up_process(speedchange_task);
492 * Already set max speed and don't see a need to change that,
493 * wait until next idle to re-evaluate, don't need timer.
495 if (pcpu->target_freq == pcpu->policy->max)
499 if (!timer_pending(&pcpu->cpu_timer))
500 cpufreq_interactive_timer_resched(pcpu);
503 up_read(&pcpu->enable_sem);
507 static void cpufreq_interactive_idle_start(void)
509 struct cpufreq_interactive_cpuinfo *pcpu =
510 &per_cpu(cpuinfo, smp_processor_id());
513 if (!down_read_trylock(&pcpu->enable_sem))
515 if (!pcpu->governor_enabled) {
516 up_read(&pcpu->enable_sem);
520 pending = timer_pending(&pcpu->cpu_timer);
522 if (pcpu->target_freq != pcpu->policy->min) {
524 * Entering idle while not at lowest speed. On some
525 * platforms this can hold the other CPU(s) at that speed
526 * even though the CPU is idle. Set a timer to re-evaluate
527 * speed so this idle CPU doesn't hold the other CPUs above
528 * min indefinitely. This should probably be a quirk of
529 * the CPUFreq driver.
532 cpufreq_interactive_timer_resched(pcpu);
535 up_read(&pcpu->enable_sem);
538 static void cpufreq_interactive_idle_end(void)
540 struct cpufreq_interactive_cpuinfo *pcpu =
541 &per_cpu(cpuinfo, smp_processor_id());
543 if (!down_read_trylock(&pcpu->enable_sem))
545 if (!pcpu->governor_enabled) {
546 up_read(&pcpu->enable_sem);
550 /* Arm the timer for 1-2 ticks later if not already. */
551 if (!timer_pending(&pcpu->cpu_timer)) {
552 cpufreq_interactive_timer_resched(pcpu);
553 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
554 del_timer(&pcpu->cpu_timer);
555 del_timer(&pcpu->cpu_slack_timer);
556 cpufreq_interactive_timer(smp_processor_id());
559 up_read(&pcpu->enable_sem);
562 static int cpufreq_interactive_speedchange_task(void *data)
567 struct cpufreq_interactive_cpuinfo *pcpu;
570 set_current_state(TASK_INTERRUPTIBLE);
571 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
573 if (cpumask_empty(&speedchange_cpumask)) {
574 spin_unlock_irqrestore(&speedchange_cpumask_lock,
578 if (kthread_should_stop())
581 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
584 set_current_state(TASK_RUNNING);
585 tmp_mask = speedchange_cpumask;
586 cpumask_clear(&speedchange_cpumask);
587 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
589 for_each_cpu(cpu, &tmp_mask) {
591 unsigned int max_freq = 0;
593 pcpu = &per_cpu(cpuinfo, cpu);
594 if (!down_read_trylock(&pcpu->enable_sem))
596 if (!pcpu->governor_enabled) {
597 up_read(&pcpu->enable_sem);
601 for_each_cpu(j, pcpu->policy->cpus) {
602 struct cpufreq_interactive_cpuinfo *pjcpu =
603 &per_cpu(cpuinfo, j);
605 if (pjcpu->target_freq > max_freq)
606 max_freq = pjcpu->target_freq;
609 if (max_freq != pcpu->policy->cur)
610 __cpufreq_driver_target(pcpu->policy,
613 trace_cpufreq_interactive_setspeed(cpu,
617 up_read(&pcpu->enable_sem);
624 static void cpufreq_interactive_boost(void)
628 unsigned long flags[2];
629 struct cpufreq_interactive_cpuinfo *pcpu;
630 struct cpufreq_interactive_tunables *tunables;
632 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
634 for_each_online_cpu(i) {
635 pcpu = &per_cpu(cpuinfo, i);
636 tunables = pcpu->policy->governor_data;
638 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
639 if (pcpu->target_freq < tunables->hispeed_freq) {
640 pcpu->target_freq = tunables->hispeed_freq;
641 cpumask_set_cpu(i, &speedchange_cpumask);
642 pcpu->hispeed_validate_time =
643 ktime_to_us(ktime_get());
648 * Set floor freq and (re)start timer for when last
652 pcpu->floor_freq = tunables->hispeed_freq;
653 pcpu->floor_validate_time = ktime_to_us(ktime_get());
654 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
657 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
660 wake_up_process(speedchange_task);
663 static int cpufreq_interactive_notifier(
664 struct notifier_block *nb, unsigned long val, void *data)
666 struct cpufreq_freqs *freq = data;
667 struct cpufreq_interactive_cpuinfo *pcpu;
671 if (val == CPUFREQ_POSTCHANGE) {
672 pcpu = &per_cpu(cpuinfo, freq->cpu);
673 if (!down_read_trylock(&pcpu->enable_sem))
675 if (!pcpu->governor_enabled) {
676 up_read(&pcpu->enable_sem);
680 for_each_cpu(cpu, pcpu->policy->cpus) {
681 struct cpufreq_interactive_cpuinfo *pjcpu =
682 &per_cpu(cpuinfo, cpu);
683 if (cpu != freq->cpu) {
684 if (!down_read_trylock(&pjcpu->enable_sem))
686 if (!pjcpu->governor_enabled) {
687 up_read(&pjcpu->enable_sem);
691 spin_lock_irqsave(&pjcpu->load_lock, flags);
693 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
694 if (cpu != freq->cpu)
695 up_read(&pjcpu->enable_sem);
698 up_read(&pcpu->enable_sem);
703 static struct notifier_block cpufreq_notifier_block = {
704 .notifier_call = cpufreq_interactive_notifier,
707 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
712 unsigned int *tokenized_data;
716 while ((cp = strpbrk(cp + 1, " :")))
719 if (!(ntokens & 0x1))
722 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
723 if (!tokenized_data) {
730 while (i < ntokens) {
731 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
734 cp = strpbrk(cp, " :");
743 *num_tokens = ntokens;
744 return tokenized_data;
747 kfree(tokenized_data);
752 static ssize_t show_target_loads(
753 struct cpufreq_interactive_tunables *tunables,
760 spin_lock_irqsave(&tunables->target_loads_lock, flags);
762 for (i = 0; i < tunables->ntarget_loads; i++)
763 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
764 i & 0x1 ? ":" : " ");
766 sprintf(buf + ret - 1, "\n");
767 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
771 static ssize_t store_target_loads(
772 struct cpufreq_interactive_tunables *tunables,
773 const char *buf, size_t count)
776 unsigned int *new_target_loads = NULL;
779 new_target_loads = get_tokenized_data(buf, &ntokens);
780 if (IS_ERR(new_target_loads))
781 return PTR_RET(new_target_loads);
783 spin_lock_irqsave(&tunables->target_loads_lock, flags);
784 if (tunables->target_loads != default_target_loads)
785 kfree(tunables->target_loads);
786 tunables->target_loads = new_target_loads;
787 tunables->ntarget_loads = ntokens;
788 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
792 static ssize_t show_above_hispeed_delay(
793 struct cpufreq_interactive_tunables *tunables, char *buf)
799 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
801 for (i = 0; i < tunables->nabove_hispeed_delay; i++)
802 ret += sprintf(buf + ret, "%u%s",
803 tunables->above_hispeed_delay[i],
804 i & 0x1 ? ":" : " ");
806 sprintf(buf + ret - 1, "\n");
807 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
811 static ssize_t store_above_hispeed_delay(
812 struct cpufreq_interactive_tunables *tunables,
813 const char *buf, size_t count)
816 unsigned int *new_above_hispeed_delay = NULL;
819 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
820 if (IS_ERR(new_above_hispeed_delay))
821 return PTR_RET(new_above_hispeed_delay);
823 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
824 if (tunables->above_hispeed_delay != default_above_hispeed_delay)
825 kfree(tunables->above_hispeed_delay);
826 tunables->above_hispeed_delay = new_above_hispeed_delay;
827 tunables->nabove_hispeed_delay = ntokens;
828 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
833 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
836 return sprintf(buf, "%u\n", tunables->hispeed_freq);
839 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
840 const char *buf, size_t count)
843 long unsigned int val;
845 ret = strict_strtoul(buf, 0, &val);
848 tunables->hispeed_freq = val;
852 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
853 *tunables, char *buf)
855 return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
858 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
859 *tunables, const char *buf, size_t count)
864 ret = strict_strtoul(buf, 0, &val);
867 tunables->go_hispeed_load = val;
871 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
872 *tunables, char *buf)
874 return sprintf(buf, "%lu\n", tunables->min_sample_time);
877 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
878 *tunables, const char *buf, size_t count)
883 ret = strict_strtoul(buf, 0, &val);
886 tunables->min_sample_time = val;
890 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
893 return sprintf(buf, "%lu\n", tunables->timer_rate);
896 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
897 const char *buf, size_t count)
902 ret = strict_strtoul(buf, 0, &val);
905 tunables->timer_rate = val;
909 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
912 return sprintf(buf, "%d\n", tunables->timer_slack_val);
915 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
916 const char *buf, size_t count)
921 ret = kstrtol(buf, 10, &val);
925 tunables->timer_slack_val = val;
929 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
932 return sprintf(buf, "%d\n", tunables->boost_val);
935 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
936 const char *buf, size_t count)
941 ret = kstrtoul(buf, 0, &val);
945 tunables->boost_val = val;
947 if (tunables->boost_val) {
948 trace_cpufreq_interactive_boost("on");
949 cpufreq_interactive_boost();
951 tunables->boostpulse_endtime = ktime_to_us(ktime_get());
952 trace_cpufreq_interactive_unboost("off");
958 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
959 const char *buf, size_t count)
964 ret = kstrtoul(buf, 0, &val);
968 tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
969 tunables->boostpulse_duration_val;
970 trace_cpufreq_interactive_boost("pulse");
971 cpufreq_interactive_boost();
975 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
976 *tunables, char *buf)
978 return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
981 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
982 *tunables, const char *buf, size_t count)
987 ret = kstrtoul(buf, 0, &val);
991 tunables->boostpulse_duration_val = val;
995 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
998 return sprintf(buf, "%u\n", tunables->io_is_busy);
1001 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
1002 const char *buf, size_t count)
1007 ret = kstrtoul(buf, 0, &val);
1010 tunables->io_is_busy = val;
1015 * Create show/store routines
1016 * - sys: One governor instance for complete SYSTEM
1017 * - pol: One governor instance per struct cpufreq_policy
1019 #define show_gov_pol_sys(file_name) \
1020 static ssize_t show_##file_name##_gov_sys \
1021 (struct kobject *kobj, struct attribute *attr, char *buf) \
1023 return show_##file_name(common_tunables, buf); \
1026 static ssize_t show_##file_name##_gov_pol \
1027 (struct cpufreq_policy *policy, char *buf) \
1029 return show_##file_name(policy->governor_data, buf); \
1032 #define store_gov_pol_sys(file_name) \
1033 static ssize_t store_##file_name##_gov_sys \
1034 (struct kobject *kobj, struct attribute *attr, const char *buf, \
1037 return store_##file_name(common_tunables, buf, count); \
1040 static ssize_t store_##file_name##_gov_pol \
1041 (struct cpufreq_policy *policy, const char *buf, size_t count) \
1043 return store_##file_name(policy->governor_data, buf, count); \
1046 #define show_store_gov_pol_sys(file_name) \
1047 show_gov_pol_sys(file_name); \
1048 store_gov_pol_sys(file_name)
1050 show_store_gov_pol_sys(target_loads);
1051 show_store_gov_pol_sys(above_hispeed_delay);
1052 show_store_gov_pol_sys(hispeed_freq);
1053 show_store_gov_pol_sys(go_hispeed_load);
1054 show_store_gov_pol_sys(min_sample_time);
1055 show_store_gov_pol_sys(timer_rate);
1056 show_store_gov_pol_sys(timer_slack);
1057 show_store_gov_pol_sys(boost);
1058 store_gov_pol_sys(boostpulse);
1059 show_store_gov_pol_sys(boostpulse_duration);
1060 show_store_gov_pol_sys(io_is_busy);
1062 #define gov_sys_attr_rw(_name) \
1063 static struct global_attr _name##_gov_sys = \
1064 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1066 #define gov_pol_attr_rw(_name) \
1067 static struct freq_attr _name##_gov_pol = \
1068 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1070 #define gov_sys_pol_attr_rw(_name) \
1071 gov_sys_attr_rw(_name); \
1072 gov_pol_attr_rw(_name)
1074 gov_sys_pol_attr_rw(target_loads);
1075 gov_sys_pol_attr_rw(above_hispeed_delay);
1076 gov_sys_pol_attr_rw(hispeed_freq);
1077 gov_sys_pol_attr_rw(go_hispeed_load);
1078 gov_sys_pol_attr_rw(min_sample_time);
1079 gov_sys_pol_attr_rw(timer_rate);
1080 gov_sys_pol_attr_rw(timer_slack);
1081 gov_sys_pol_attr_rw(boost);
1082 gov_sys_pol_attr_rw(boostpulse_duration);
1083 gov_sys_pol_attr_rw(io_is_busy);
1085 static struct global_attr boostpulse_gov_sys =
1086 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1088 static struct freq_attr boostpulse_gov_pol =
1089 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1091 /* One Governor instance for entire system */
1092 static struct attribute *interactive_attributes_gov_sys[] = {
1093 &target_loads_gov_sys.attr,
1094 &above_hispeed_delay_gov_sys.attr,
1095 &hispeed_freq_gov_sys.attr,
1096 &go_hispeed_load_gov_sys.attr,
1097 &min_sample_time_gov_sys.attr,
1098 &timer_rate_gov_sys.attr,
1099 &timer_slack_gov_sys.attr,
1100 &boost_gov_sys.attr,
1101 &boostpulse_gov_sys.attr,
1102 &boostpulse_duration_gov_sys.attr,
1103 &io_is_busy_gov_sys.attr,
1107 static struct attribute_group interactive_attr_group_gov_sys = {
1108 .attrs = interactive_attributes_gov_sys,
1109 .name = "interactive",
1112 /* Per policy governor instance */
1113 static struct attribute *interactive_attributes_gov_pol[] = {
1114 &target_loads_gov_pol.attr,
1115 &above_hispeed_delay_gov_pol.attr,
1116 &hispeed_freq_gov_pol.attr,
1117 &go_hispeed_load_gov_pol.attr,
1118 &min_sample_time_gov_pol.attr,
1119 &timer_rate_gov_pol.attr,
1120 &timer_slack_gov_pol.attr,
1121 &boost_gov_pol.attr,
1122 &boostpulse_gov_pol.attr,
1123 &boostpulse_duration_gov_pol.attr,
1124 &io_is_busy_gov_pol.attr,
1128 static struct attribute_group interactive_attr_group_gov_pol = {
1129 .attrs = interactive_attributes_gov_pol,
1130 .name = "interactive",
1133 static struct attribute_group *get_sysfs_attr(void)
1135 if (have_governor_per_policy())
1136 return &interactive_attr_group_gov_pol;
1138 return &interactive_attr_group_gov_sys;
1141 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1147 cpufreq_interactive_idle_start();
1150 cpufreq_interactive_idle_end();
1157 static struct notifier_block cpufreq_interactive_idle_nb = {
1158 .notifier_call = cpufreq_interactive_idle_notifier,
1161 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1166 struct cpufreq_interactive_cpuinfo *pcpu;
1167 struct cpufreq_frequency_table *freq_table;
1168 struct cpufreq_interactive_tunables *tunables;
1169 unsigned long flags;
1171 if (have_governor_per_policy())
1172 tunables = policy->governor_data;
1174 tunables = common_tunables;
1176 WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1179 case CPUFREQ_GOV_POLICY_INIT:
1180 if (have_governor_per_policy()) {
1182 } else if (tunables) {
1183 tunables->usage_count++;
1184 policy->governor_data = tunables;
1188 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1190 pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1194 tunables->usage_count = 1;
1195 tunables->above_hispeed_delay = default_above_hispeed_delay;
1196 tunables->nabove_hispeed_delay =
1197 ARRAY_SIZE(default_above_hispeed_delay);
1198 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1199 tunables->target_loads = default_target_loads;
1200 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1201 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1202 tunables->timer_rate = DEFAULT_TIMER_RATE;
1203 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1204 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1206 spin_lock_init(&tunables->target_loads_lock);
1207 spin_lock_init(&tunables->above_hispeed_delay_lock);
1209 policy->governor_data = tunables;
1210 if (!have_governor_per_policy())
1211 common_tunables = tunables;
1213 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1217 policy->governor_data = NULL;
1218 if (!have_governor_per_policy())
1219 common_tunables = NULL;
1223 if (!policy->governor->initialized) {
1224 idle_notifier_register(&cpufreq_interactive_idle_nb);
1225 cpufreq_register_notifier(&cpufreq_notifier_block,
1226 CPUFREQ_TRANSITION_NOTIFIER);
1231 case CPUFREQ_GOV_POLICY_EXIT:
1232 if (!--tunables->usage_count) {
1233 if (policy->governor->initialized == 1) {
1234 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1235 CPUFREQ_TRANSITION_NOTIFIER);
1236 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1239 sysfs_remove_group(get_governor_parent_kobj(policy),
1242 common_tunables = NULL;
1245 policy->governor_data = NULL;
1248 case CPUFREQ_GOV_START:
1249 mutex_lock(&gov_lock);
1251 freq_table = cpufreq_frequency_get_table(policy->cpu);
1252 if (!tunables->hispeed_freq)
1253 tunables->hispeed_freq = policy->max;
1255 for_each_cpu(j, policy->cpus) {
1256 pcpu = &per_cpu(cpuinfo, j);
1257 pcpu->policy = policy;
1258 pcpu->target_freq = policy->cur;
1259 pcpu->freq_table = freq_table;
1260 pcpu->floor_freq = pcpu->target_freq;
1261 pcpu->floor_validate_time =
1262 ktime_to_us(ktime_get());
1263 pcpu->hispeed_validate_time =
1264 pcpu->floor_validate_time;
1265 pcpu->max_freq = policy->max;
1266 down_write(&pcpu->enable_sem);
1267 del_timer_sync(&pcpu->cpu_timer);
1268 del_timer_sync(&pcpu->cpu_slack_timer);
1269 cpufreq_interactive_timer_start(tunables, j);
1270 pcpu->governor_enabled = 1;
1271 up_write(&pcpu->enable_sem);
1274 mutex_unlock(&gov_lock);
1277 case CPUFREQ_GOV_STOP:
1278 mutex_lock(&gov_lock);
1279 for_each_cpu(j, policy->cpus) {
1280 pcpu = &per_cpu(cpuinfo, j);
1281 down_write(&pcpu->enable_sem);
1282 pcpu->governor_enabled = 0;
1283 del_timer_sync(&pcpu->cpu_timer);
1284 del_timer_sync(&pcpu->cpu_slack_timer);
1285 up_write(&pcpu->enable_sem);
1288 mutex_unlock(&gov_lock);
1291 case CPUFREQ_GOV_LIMITS:
1292 if (policy->max < policy->cur)
1293 __cpufreq_driver_target(policy,
1294 policy->max, CPUFREQ_RELATION_H);
1295 else if (policy->min > policy->cur)
1296 __cpufreq_driver_target(policy,
1297 policy->min, CPUFREQ_RELATION_L);
1298 for_each_cpu(j, policy->cpus) {
1299 pcpu = &per_cpu(cpuinfo, j);
1301 down_read(&pcpu->enable_sem);
1302 if (pcpu->governor_enabled == 0) {
1303 up_read(&pcpu->enable_sem);
1307 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
1308 if (policy->max < pcpu->target_freq)
1309 pcpu->target_freq = policy->max;
1310 else if (policy->min > pcpu->target_freq)
1311 pcpu->target_freq = policy->min;
1313 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
1314 up_read(&pcpu->enable_sem);
1316 /* Reschedule timer only if policy->max is raised.
1317 * Delete the timers, else the timer callback may
1318 * return without re-arm the timer when failed
1319 * acquire the semaphore. This race may cause timer
1320 * stopped unexpectedly.
1323 if (policy->max > pcpu->max_freq) {
1324 down_write(&pcpu->enable_sem);
1325 del_timer_sync(&pcpu->cpu_timer);
1326 del_timer_sync(&pcpu->cpu_slack_timer);
1327 cpufreq_interactive_timer_start(tunables, j);
1328 up_write(&pcpu->enable_sem);
1331 pcpu->max_freq = policy->max;
1338 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1341 struct cpufreq_governor cpufreq_gov_interactive = {
1342 .name = "interactive",
1343 .governor = cpufreq_governor_interactive,
1344 .max_transition_latency = 10000000,
1345 .owner = THIS_MODULE,
1348 static void cpufreq_interactive_nop_timer(unsigned long data)
1352 static int __init cpufreq_interactive_init(void)
1355 struct cpufreq_interactive_cpuinfo *pcpu;
1356 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1358 /* Initalize per-cpu timers */
1359 for_each_possible_cpu(i) {
1360 pcpu = &per_cpu(cpuinfo, i);
1361 init_timer_deferrable(&pcpu->cpu_timer);
1362 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1363 pcpu->cpu_timer.data = i;
1364 init_timer(&pcpu->cpu_slack_timer);
1365 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1366 spin_lock_init(&pcpu->load_lock);
1367 spin_lock_init(&pcpu->target_freq_lock);
1368 init_rwsem(&pcpu->enable_sem);
1371 spin_lock_init(&speedchange_cpumask_lock);
1372 mutex_init(&gov_lock);
1374 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1376 if (IS_ERR(speedchange_task))
1377 return PTR_ERR(speedchange_task);
1379 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1380 get_task_struct(speedchange_task);
1382 /* NB: wake up so the thread does not look hung to the freezer */
1383 wake_up_process(speedchange_task);
1385 return cpufreq_register_governor(&cpufreq_gov_interactive);
1388 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1389 fs_initcall(cpufreq_interactive_init);
1391 module_init(cpufreq_interactive_init);
1394 static void __exit cpufreq_interactive_exit(void)
1396 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1397 kthread_stop(speedchange_task);
1398 put_task_struct(speedchange_task);
1401 module_exit(cpufreq_interactive_exit);
1403 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1404 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1405 "Latency sensitive workloads");
1406 MODULE_LICENSE("GPL");