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 #ifdef CONFIG_ARCH_ROCKCHIP
23 #include <linux/input.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/rwsem.h>
28 #include <linux/sched.h>
29 #include <linux/sched/rt.h>
30 #include <linux/tick.h>
31 #include <linux/time.h>
32 #include <linux/timer.h>
33 #include <linux/workqueue.h>
34 #include <linux/kthread.h>
35 #include <linux/slab.h>
36 #include "cpufreq_governor.h"
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/cpufreq_interactive.h>
41 struct cpufreq_interactive_cpuinfo {
42 struct timer_list cpu_timer;
43 struct timer_list cpu_slack_timer;
44 spinlock_t load_lock; /* protects the next 4 fields */
46 u64 time_in_idle_timestamp;
48 u64 cputime_speedadj_timestamp;
49 struct cpufreq_policy *policy;
50 struct cpufreq_frequency_table *freq_table;
51 spinlock_t target_freq_lock; /*protects target freq */
52 unsigned int target_freq;
53 unsigned int floor_freq;
54 unsigned int max_freq;
55 u64 floor_validate_time;
56 u64 hispeed_validate_time;
57 struct rw_semaphore enable_sem;
61 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
63 /* realtime thread handles frequency scaling */
64 static struct task_struct *speedchange_task;
65 static cpumask_t speedchange_cpumask;
66 static spinlock_t speedchange_cpumask_lock;
67 static struct mutex gov_lock;
69 /* Target load. Lower values result in higher CPU speeds. */
70 #define DEFAULT_TARGET_LOAD 90
71 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
73 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
74 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
75 static unsigned int default_above_hispeed_delay[] = {
76 DEFAULT_ABOVE_HISPEED_DELAY };
78 struct cpufreq_interactive_tunables {
80 /* Hi speed to bump to from lo speed when load burst (default max) */
81 unsigned int hispeed_freq;
82 /* Go to hi speed when CPU load at or above this value. */
83 #define DEFAULT_GO_HISPEED_LOAD 99
84 unsigned long go_hispeed_load;
85 /* Target load. Lower values result in higher CPU speeds. */
86 spinlock_t target_loads_lock;
87 unsigned int *target_loads;
90 * The minimum amount of time to spend at a frequency before we can ramp
93 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
94 unsigned long min_sample_time;
96 * The sample rate of the timer used to increase frequency
98 unsigned long timer_rate;
100 * Wait this long before raising speed above hispeed, by default a
101 * single timer interval.
103 spinlock_t above_hispeed_delay_lock;
104 unsigned int *above_hispeed_delay;
105 int nabove_hispeed_delay;
106 /* Non-zero means indefinite speed boost active */
108 /* Duration of a boot pulse in usecs */
109 int boostpulse_duration_val;
110 /* End time of boost pulse in ktime converted to usecs */
111 u64 boostpulse_endtime;
112 #ifdef CONFIG_ARCH_ROCKCHIP
113 /* Frequency to which a touch boost takes the cpus to */
114 unsigned long touchboost_freq;
115 /* Duration of a touchboost pulse in usecs */
116 int touchboostpulse_duration_val;
117 /* End time of touchboost pulse in ktime converted to usecs */
118 u64 touchboostpulse_endtime;
122 * Max additional time to wait in idle, beyond timer_rate, at speeds
123 * above minimum before wakeup to reduce speed, or -1 if unnecessary.
125 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
130 /* For cases where we have single governor instance for system */
131 static struct cpufreq_interactive_tunables *common_tunables;
133 static struct attribute_group *get_sysfs_attr(void);
135 static void cpufreq_interactive_timer_resched(
136 struct cpufreq_interactive_cpuinfo *pcpu)
138 struct cpufreq_interactive_tunables *tunables =
139 pcpu->policy->governor_data;
140 unsigned long expires;
143 spin_lock_irqsave(&pcpu->load_lock, flags);
145 get_cpu_idle_time(smp_processor_id(),
146 &pcpu->time_in_idle_timestamp,
147 tunables->io_is_busy);
148 pcpu->cputime_speedadj = 0;
149 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
150 expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
151 mod_timer_pinned(&pcpu->cpu_timer, expires);
153 if (tunables->timer_slack_val >= 0 &&
154 pcpu->target_freq > pcpu->policy->min) {
155 expires += usecs_to_jiffies(tunables->timer_slack_val);
156 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
159 spin_unlock_irqrestore(&pcpu->load_lock, flags);
162 /* The caller shall take enable_sem write semaphore to avoid any timer race.
163 * The cpu_timer and cpu_slack_timer must be deactivated when calling this
166 static void cpufreq_interactive_timer_start(
167 struct cpufreq_interactive_tunables *tunables, int cpu)
169 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
170 unsigned long expires = jiffies +
171 usecs_to_jiffies(tunables->timer_rate);
174 pcpu->cpu_timer.expires = expires;
175 add_timer_on(&pcpu->cpu_timer, cpu);
176 if (tunables->timer_slack_val >= 0 &&
177 pcpu->target_freq > pcpu->policy->min) {
178 expires += usecs_to_jiffies(tunables->timer_slack_val);
179 pcpu->cpu_slack_timer.expires = expires;
180 add_timer_on(&pcpu->cpu_slack_timer, cpu);
183 spin_lock_irqsave(&pcpu->load_lock, flags);
185 get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
186 tunables->io_is_busy);
187 pcpu->cputime_speedadj = 0;
188 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
189 spin_unlock_irqrestore(&pcpu->load_lock, flags);
192 static unsigned int freq_to_above_hispeed_delay(
193 struct cpufreq_interactive_tunables *tunables,
200 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
202 for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
203 freq >= tunables->above_hispeed_delay[i+1]; i += 2)
206 ret = tunables->above_hispeed_delay[i];
207 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
211 static unsigned int freq_to_targetload(
212 struct cpufreq_interactive_tunables *tunables, unsigned int freq)
218 spin_lock_irqsave(&tunables->target_loads_lock, flags);
220 for (i = 0; i < tunables->ntarget_loads - 1 &&
221 freq >= tunables->target_loads[i+1]; i += 2)
224 ret = tunables->target_loads[i];
225 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
230 * If increasing frequencies never map to a lower target load then
231 * choose_freq() will find the minimum frequency that does not exceed its
232 * target load given the current load.
234 static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
235 unsigned int loadadjfreq)
237 unsigned int freq = pcpu->policy->cur;
238 unsigned int prevfreq, freqmin, freqmax;
247 tl = freq_to_targetload(pcpu->policy->governor_data, freq);
250 * Find the lowest frequency where the computed load is less
251 * than or equal to the target load.
254 if (cpufreq_frequency_table_target(
255 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
256 CPUFREQ_RELATION_L, &index))
258 freq = pcpu->freq_table[index].frequency;
260 if (freq > prevfreq) {
261 /* The previous frequency is too low. */
264 if (freq >= freqmax) {
266 * Find the highest frequency that is less
269 if (cpufreq_frequency_table_target(
270 pcpu->policy, pcpu->freq_table,
271 freqmax - 1, CPUFREQ_RELATION_H,
274 freq = pcpu->freq_table[index].frequency;
276 if (freq == freqmin) {
278 * The first frequency below freqmax
279 * has already been found to be too
280 * low. freqmax is the lowest speed
281 * we found that is fast enough.
287 } else if (freq < prevfreq) {
288 /* The previous frequency is high enough. */
291 if (freq <= freqmin) {
293 * Find the lowest frequency that is higher
296 if (cpufreq_frequency_table_target(
297 pcpu->policy, pcpu->freq_table,
298 freqmin + 1, CPUFREQ_RELATION_L,
301 freq = pcpu->freq_table[index].frequency;
304 * If freqmax is the first frequency above
305 * freqmin then we have already found that
306 * this speed is fast enough.
313 /* If same frequency chosen as previous then done. */
314 } while (freq != prevfreq);
319 static u64 update_load(int cpu)
321 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
322 struct cpufreq_interactive_tunables *tunables =
323 pcpu->policy->governor_data;
330 now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
331 delta_idle = (now_idle - pcpu->time_in_idle);
332 delta_time = (now - pcpu->time_in_idle_timestamp);
334 if (delta_time <= delta_idle)
337 active_time = delta_time - delta_idle;
339 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
341 pcpu->time_in_idle = now_idle;
342 pcpu->time_in_idle_timestamp = now;
346 static void cpufreq_interactive_timer(unsigned long data)
349 unsigned int delta_time;
350 u64 cputime_speedadj;
352 struct cpufreq_interactive_cpuinfo *pcpu =
353 &per_cpu(cpuinfo, data);
354 struct cpufreq_interactive_tunables *tunables =
355 pcpu->policy->governor_data;
356 unsigned int new_freq;
357 unsigned int loadadjfreq;
361 if (!down_read_trylock(&pcpu->enable_sem))
363 if (!pcpu->governor_enabled)
366 spin_lock_irqsave(&pcpu->load_lock, flags);
367 now = update_load(data);
368 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
369 cputime_speedadj = pcpu->cputime_speedadj;
370 spin_unlock_irqrestore(&pcpu->load_lock, flags);
372 if (WARN_ON_ONCE(!delta_time))
375 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
376 do_div(cputime_speedadj, delta_time);
377 loadadjfreq = (unsigned int)cputime_speedadj * 100;
378 cpu_load = loadadjfreq / pcpu->target_freq;
379 tunables->boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
381 #ifdef CONFIG_ARCH_ROCKCHIP
382 pcpu->target_freq = pcpu->policy->cur;
383 tunables->boosted |= now < tunables->touchboostpulse_endtime;
386 if (cpu_load >= tunables->go_hispeed_load || tunables->boosted) {
387 #ifdef CONFIG_ARCH_ROCKCHIP
388 if (now < tunables->touchboostpulse_endtime) {
389 new_freq = choose_freq(pcpu, loadadjfreq);
390 if (new_freq < tunables->touchboost_freq)
391 new_freq = tunables->touchboost_freq;
394 if (pcpu->target_freq < tunables->hispeed_freq) {
395 new_freq = tunables->hispeed_freq;
397 new_freq = choose_freq(pcpu, loadadjfreq);
399 if (new_freq < tunables->hispeed_freq)
400 new_freq = tunables->hispeed_freq;
403 new_freq = choose_freq(pcpu, loadadjfreq);
404 if (new_freq > tunables->hispeed_freq &&
405 pcpu->target_freq < tunables->hispeed_freq)
406 new_freq = tunables->hispeed_freq;
409 if (pcpu->target_freq >= tunables->hispeed_freq &&
410 new_freq > pcpu->target_freq &&
411 now - pcpu->hispeed_validate_time <
412 freq_to_above_hispeed_delay(tunables, pcpu->target_freq)) {
413 trace_cpufreq_interactive_notyet(
414 data, cpu_load, pcpu->target_freq,
415 pcpu->policy->cur, new_freq);
416 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
420 pcpu->hispeed_validate_time = now;
422 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
423 new_freq, CPUFREQ_RELATION_L,
425 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
429 new_freq = pcpu->freq_table[index].frequency;
432 * Do not scale below floor_freq unless we have been at or above the
433 * floor frequency for the minimum sample time since last validated.
435 if (new_freq < pcpu->floor_freq) {
436 if (now - pcpu->floor_validate_time <
437 tunables->min_sample_time) {
438 trace_cpufreq_interactive_notyet(
439 data, cpu_load, pcpu->target_freq,
440 pcpu->policy->cur, new_freq);
441 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
447 * Update the timestamp for checking whether speed has been held at
448 * or above the selected frequency for a minimum of min_sample_time,
449 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
450 * allow the speed to drop as soon as the boostpulse duration expires
451 * (or the indefinite boost is turned off).
454 if (!tunables->boosted || new_freq > tunables->hispeed_freq) {
455 pcpu->floor_freq = new_freq;
456 pcpu->floor_validate_time = now;
459 if (pcpu->target_freq == new_freq &&
460 pcpu->target_freq <= pcpu->policy->cur) {
461 trace_cpufreq_interactive_already(
462 data, cpu_load, pcpu->target_freq,
463 pcpu->policy->cur, new_freq);
464 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
465 goto rearm_if_notmax;
468 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
469 pcpu->policy->cur, new_freq);
471 pcpu->target_freq = new_freq;
472 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
473 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
474 cpumask_set_cpu(data, &speedchange_cpumask);
475 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
476 wake_up_process(speedchange_task);
480 * Already set max speed and don't see a need to change that,
481 * wait until next idle to re-evaluate, don't need timer.
483 if (pcpu->target_freq == pcpu->policy->max)
487 if (!timer_pending(&pcpu->cpu_timer))
488 cpufreq_interactive_timer_resched(pcpu);
491 up_read(&pcpu->enable_sem);
495 static void cpufreq_interactive_idle_start(void)
497 struct cpufreq_interactive_cpuinfo *pcpu =
498 &per_cpu(cpuinfo, smp_processor_id());
501 if (!down_read_trylock(&pcpu->enable_sem))
503 if (!pcpu->governor_enabled) {
504 up_read(&pcpu->enable_sem);
508 pending = timer_pending(&pcpu->cpu_timer);
510 if (pcpu->target_freq != pcpu->policy->min) {
512 * Entering idle while not at lowest speed. On some
513 * platforms this can hold the other CPU(s) at that speed
514 * even though the CPU is idle. Set a timer to re-evaluate
515 * speed so this idle CPU doesn't hold the other CPUs above
516 * min indefinitely. This should probably be a quirk of
517 * the CPUFreq driver.
520 cpufreq_interactive_timer_resched(pcpu);
523 up_read(&pcpu->enable_sem);
526 static void cpufreq_interactive_idle_end(void)
528 struct cpufreq_interactive_cpuinfo *pcpu =
529 &per_cpu(cpuinfo, smp_processor_id());
531 if (!down_read_trylock(&pcpu->enable_sem))
533 if (!pcpu->governor_enabled) {
534 up_read(&pcpu->enable_sem);
538 /* Arm the timer for 1-2 ticks later if not already. */
539 if (!timer_pending(&pcpu->cpu_timer)) {
540 cpufreq_interactive_timer_resched(pcpu);
541 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
542 del_timer(&pcpu->cpu_timer);
543 del_timer(&pcpu->cpu_slack_timer);
544 cpufreq_interactive_timer(smp_processor_id());
547 up_read(&pcpu->enable_sem);
550 static int cpufreq_interactive_speedchange_task(void *data)
555 struct cpufreq_interactive_cpuinfo *pcpu;
558 set_current_state(TASK_INTERRUPTIBLE);
559 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
561 if (cpumask_empty(&speedchange_cpumask)) {
562 spin_unlock_irqrestore(&speedchange_cpumask_lock,
566 if (kthread_should_stop())
569 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
572 set_current_state(TASK_RUNNING);
573 tmp_mask = speedchange_cpumask;
574 cpumask_clear(&speedchange_cpumask);
575 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
577 for_each_cpu(cpu, &tmp_mask) {
579 unsigned int max_freq = 0;
581 pcpu = &per_cpu(cpuinfo, cpu);
582 if (!down_read_trylock(&pcpu->enable_sem))
584 if (!pcpu->governor_enabled) {
585 up_read(&pcpu->enable_sem);
589 for_each_cpu(j, pcpu->policy->cpus) {
590 struct cpufreq_interactive_cpuinfo *pjcpu =
591 &per_cpu(cpuinfo, j);
593 if (pjcpu->target_freq > max_freq)
594 max_freq = pjcpu->target_freq;
597 if (max_freq != pcpu->policy->cur)
598 __cpufreq_driver_target(pcpu->policy,
601 trace_cpufreq_interactive_setspeed(cpu,
605 up_read(&pcpu->enable_sem);
612 static void cpufreq_interactive_boost(struct cpufreq_interactive_tunables *tunables)
616 unsigned long flags[2];
617 struct cpufreq_interactive_cpuinfo *pcpu;
619 tunables->boosted = true;
621 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
623 for_each_online_cpu(i) {
624 pcpu = &per_cpu(cpuinfo, i);
625 if (tunables != pcpu->policy->governor_data)
628 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
629 if (pcpu->target_freq < tunables->hispeed_freq) {
630 pcpu->target_freq = tunables->hispeed_freq;
631 cpumask_set_cpu(i, &speedchange_cpumask);
632 pcpu->hispeed_validate_time =
633 ktime_to_us(ktime_get());
638 * Set floor freq and (re)start timer for when last
642 pcpu->floor_freq = tunables->hispeed_freq;
643 pcpu->floor_validate_time = ktime_to_us(ktime_get());
644 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
647 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
650 wake_up_process(speedchange_task);
653 static int cpufreq_interactive_notifier(
654 struct notifier_block *nb, unsigned long val, void *data)
656 struct cpufreq_freqs *freq = data;
657 struct cpufreq_interactive_cpuinfo *pcpu;
661 if (val == CPUFREQ_POSTCHANGE) {
662 pcpu = &per_cpu(cpuinfo, freq->cpu);
663 if (!down_read_trylock(&pcpu->enable_sem))
665 if (!pcpu->governor_enabled) {
666 up_read(&pcpu->enable_sem);
670 for_each_cpu(cpu, pcpu->policy->cpus) {
671 struct cpufreq_interactive_cpuinfo *pjcpu =
672 &per_cpu(cpuinfo, cpu);
673 if (cpu != freq->cpu) {
674 if (!down_read_trylock(&pjcpu->enable_sem))
676 if (!pjcpu->governor_enabled) {
677 up_read(&pjcpu->enable_sem);
681 spin_lock_irqsave(&pjcpu->load_lock, flags);
683 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
684 if (cpu != freq->cpu)
685 up_read(&pjcpu->enable_sem);
688 up_read(&pcpu->enable_sem);
693 static struct notifier_block cpufreq_notifier_block = {
694 .notifier_call = cpufreq_interactive_notifier,
697 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
702 unsigned int *tokenized_data;
706 while ((cp = strpbrk(cp + 1, " :")))
709 if (!(ntokens & 0x1))
712 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
713 if (!tokenized_data) {
720 while (i < ntokens) {
721 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
724 cp = strpbrk(cp, " :");
733 *num_tokens = ntokens;
734 return tokenized_data;
737 kfree(tokenized_data);
742 static ssize_t show_target_loads(
743 struct cpufreq_interactive_tunables *tunables,
750 spin_lock_irqsave(&tunables->target_loads_lock, flags);
752 for (i = 0; i < tunables->ntarget_loads; i++)
753 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
754 i & 0x1 ? ":" : " ");
756 sprintf(buf + ret - 1, "\n");
757 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
761 static ssize_t store_target_loads(
762 struct cpufreq_interactive_tunables *tunables,
763 const char *buf, size_t count)
766 unsigned int *new_target_loads = NULL;
769 new_target_loads = get_tokenized_data(buf, &ntokens);
770 if (IS_ERR(new_target_loads))
771 return PTR_RET(new_target_loads);
773 spin_lock_irqsave(&tunables->target_loads_lock, flags);
774 if (tunables->target_loads != default_target_loads)
775 kfree(tunables->target_loads);
776 tunables->target_loads = new_target_loads;
777 tunables->ntarget_loads = ntokens;
778 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
782 static ssize_t show_above_hispeed_delay(
783 struct cpufreq_interactive_tunables *tunables, char *buf)
789 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
791 for (i = 0; i < tunables->nabove_hispeed_delay; i++)
792 ret += sprintf(buf + ret, "%u%s",
793 tunables->above_hispeed_delay[i],
794 i & 0x1 ? ":" : " ");
796 sprintf(buf + ret - 1, "\n");
797 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
801 static ssize_t store_above_hispeed_delay(
802 struct cpufreq_interactive_tunables *tunables,
803 const char *buf, size_t count)
806 unsigned int *new_above_hispeed_delay = NULL;
809 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
810 if (IS_ERR(new_above_hispeed_delay))
811 return PTR_RET(new_above_hispeed_delay);
813 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
814 if (tunables->above_hispeed_delay != default_above_hispeed_delay)
815 kfree(tunables->above_hispeed_delay);
816 tunables->above_hispeed_delay = new_above_hispeed_delay;
817 tunables->nabove_hispeed_delay = ntokens;
818 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
823 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
826 return sprintf(buf, "%u\n", tunables->hispeed_freq);
829 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
830 const char *buf, size_t count)
833 long unsigned int val;
835 ret = strict_strtoul(buf, 0, &val);
838 tunables->hispeed_freq = val;
842 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
843 *tunables, char *buf)
845 return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
848 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
849 *tunables, const char *buf, size_t count)
854 ret = strict_strtoul(buf, 0, &val);
857 tunables->go_hispeed_load = val;
861 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
862 *tunables, char *buf)
864 return sprintf(buf, "%lu\n", tunables->min_sample_time);
867 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
868 *tunables, const char *buf, size_t count)
873 ret = strict_strtoul(buf, 0, &val);
876 tunables->min_sample_time = val;
880 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
883 return sprintf(buf, "%lu\n", tunables->timer_rate);
886 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
887 const char *buf, size_t count)
892 ret = strict_strtoul(buf, 0, &val);
895 tunables->timer_rate = val;
899 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
902 return sprintf(buf, "%d\n", tunables->timer_slack_val);
905 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
906 const char *buf, size_t count)
911 ret = kstrtol(buf, 10, &val);
915 tunables->timer_slack_val = val;
919 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
922 return sprintf(buf, "%d\n", tunables->boost_val);
925 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
926 const char *buf, size_t count)
931 ret = kstrtoul(buf, 0, &val);
935 tunables->boost_val = val;
937 if (tunables->boost_val) {
938 trace_cpufreq_interactive_boost("on");
939 if (!tunables->boosted)
940 cpufreq_interactive_boost(tunables);
942 tunables->boostpulse_endtime = ktime_to_us(ktime_get());
943 trace_cpufreq_interactive_unboost("off");
949 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
950 const char *buf, size_t count)
955 ret = kstrtoul(buf, 0, &val);
959 tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
960 tunables->boostpulse_duration_val;
961 trace_cpufreq_interactive_boost("pulse");
962 if (!tunables->boosted)
963 cpufreq_interactive_boost(tunables);
967 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
968 *tunables, char *buf)
970 return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
973 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
974 *tunables, const char *buf, size_t count)
979 ret = kstrtoul(buf, 0, &val);
983 tunables->boostpulse_duration_val = val;
987 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
990 return sprintf(buf, "%u\n", tunables->io_is_busy);
993 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
994 const char *buf, size_t count)
999 ret = kstrtoul(buf, 0, &val);
1002 tunables->io_is_busy = val;
1007 * Create show/store routines
1008 * - sys: One governor instance for complete SYSTEM
1009 * - pol: One governor instance per struct cpufreq_policy
1011 #define show_gov_pol_sys(file_name) \
1012 static ssize_t show_##file_name##_gov_sys \
1013 (struct kobject *kobj, struct attribute *attr, char *buf) \
1015 return show_##file_name(common_tunables, buf); \
1018 static ssize_t show_##file_name##_gov_pol \
1019 (struct cpufreq_policy *policy, char *buf) \
1021 return show_##file_name(policy->governor_data, buf); \
1024 #define store_gov_pol_sys(file_name) \
1025 static ssize_t store_##file_name##_gov_sys \
1026 (struct kobject *kobj, struct attribute *attr, const char *buf, \
1029 return store_##file_name(common_tunables, buf, count); \
1032 static ssize_t store_##file_name##_gov_pol \
1033 (struct cpufreq_policy *policy, const char *buf, size_t count) \
1035 return store_##file_name(policy->governor_data, buf, count); \
1038 #define show_store_gov_pol_sys(file_name) \
1039 show_gov_pol_sys(file_name); \
1040 store_gov_pol_sys(file_name)
1042 show_store_gov_pol_sys(target_loads);
1043 show_store_gov_pol_sys(above_hispeed_delay);
1044 show_store_gov_pol_sys(hispeed_freq);
1045 show_store_gov_pol_sys(go_hispeed_load);
1046 show_store_gov_pol_sys(min_sample_time);
1047 show_store_gov_pol_sys(timer_rate);
1048 show_store_gov_pol_sys(timer_slack);
1049 show_store_gov_pol_sys(boost);
1050 store_gov_pol_sys(boostpulse);
1051 show_store_gov_pol_sys(boostpulse_duration);
1052 show_store_gov_pol_sys(io_is_busy);
1054 #define gov_sys_attr_rw(_name) \
1055 static struct global_attr _name##_gov_sys = \
1056 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1058 #define gov_pol_attr_rw(_name) \
1059 static struct freq_attr _name##_gov_pol = \
1060 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1062 #define gov_sys_pol_attr_rw(_name) \
1063 gov_sys_attr_rw(_name); \
1064 gov_pol_attr_rw(_name)
1066 gov_sys_pol_attr_rw(target_loads);
1067 gov_sys_pol_attr_rw(above_hispeed_delay);
1068 gov_sys_pol_attr_rw(hispeed_freq);
1069 gov_sys_pol_attr_rw(go_hispeed_load);
1070 gov_sys_pol_attr_rw(min_sample_time);
1071 gov_sys_pol_attr_rw(timer_rate);
1072 gov_sys_pol_attr_rw(timer_slack);
1073 gov_sys_pol_attr_rw(boost);
1074 gov_sys_pol_attr_rw(boostpulse_duration);
1075 gov_sys_pol_attr_rw(io_is_busy);
1077 static struct global_attr boostpulse_gov_sys =
1078 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1080 static struct freq_attr boostpulse_gov_pol =
1081 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1083 /* One Governor instance for entire system */
1084 static struct attribute *interactive_attributes_gov_sys[] = {
1085 &target_loads_gov_sys.attr,
1086 &above_hispeed_delay_gov_sys.attr,
1087 &hispeed_freq_gov_sys.attr,
1088 &go_hispeed_load_gov_sys.attr,
1089 &min_sample_time_gov_sys.attr,
1090 &timer_rate_gov_sys.attr,
1091 &timer_slack_gov_sys.attr,
1092 &boost_gov_sys.attr,
1093 &boostpulse_gov_sys.attr,
1094 &boostpulse_duration_gov_sys.attr,
1095 &io_is_busy_gov_sys.attr,
1099 static struct attribute_group interactive_attr_group_gov_sys = {
1100 .attrs = interactive_attributes_gov_sys,
1101 .name = "interactive",
1104 /* Per policy governor instance */
1105 static struct attribute *interactive_attributes_gov_pol[] = {
1106 &target_loads_gov_pol.attr,
1107 &above_hispeed_delay_gov_pol.attr,
1108 &hispeed_freq_gov_pol.attr,
1109 &go_hispeed_load_gov_pol.attr,
1110 &min_sample_time_gov_pol.attr,
1111 &timer_rate_gov_pol.attr,
1112 &timer_slack_gov_pol.attr,
1113 &boost_gov_pol.attr,
1114 &boostpulse_gov_pol.attr,
1115 &boostpulse_duration_gov_pol.attr,
1116 &io_is_busy_gov_pol.attr,
1120 static struct attribute_group interactive_attr_group_gov_pol = {
1121 .attrs = interactive_attributes_gov_pol,
1122 .name = "interactive",
1125 static struct attribute_group *get_sysfs_attr(void)
1127 if (have_governor_per_policy())
1128 return &interactive_attr_group_gov_pol;
1130 return &interactive_attr_group_gov_sys;
1133 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1139 cpufreq_interactive_idle_start();
1142 cpufreq_interactive_idle_end();
1149 static struct notifier_block cpufreq_interactive_idle_nb = {
1150 .notifier_call = cpufreq_interactive_idle_notifier,
1153 #ifdef CONFIG_ARCH_ROCKCHIP
1154 static void cpufreq_interactive_input_event(struct input_handle *handle, unsigned int type,
1155 unsigned int code, int value)
1160 unsigned long flags[2];
1161 struct cpufreq_interactive_cpuinfo *pcpu;
1162 struct cpufreq_interactive_tunables *tunables;
1167 trace_cpufreq_interactive_boost("touch");
1168 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
1170 now = ktime_to_us(ktime_get());
1171 for_each_online_cpu(i) {
1172 pcpu = &per_cpu(cpuinfo, i);
1173 if (have_governor_per_policy())
1174 tunables = pcpu->policy->governor_data;
1176 tunables = common_tunables;
1180 endtime = now + tunables->touchboostpulse_duration_val;
1181 if (endtime < (tunables->touchboostpulse_endtime + 10 * USEC_PER_MSEC))
1183 tunables->touchboostpulse_endtime = endtime;
1185 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
1186 if (pcpu->target_freq < tunables->touchboost_freq) {
1187 pcpu->target_freq = tunables->touchboost_freq;
1188 cpumask_set_cpu(i, &speedchange_cpumask);
1189 pcpu->hispeed_validate_time =
1190 ktime_to_us(ktime_get());
1194 pcpu->floor_freq = tunables->touchboost_freq;
1195 pcpu->floor_validate_time = ktime_to_us(ktime_get());
1197 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
1200 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
1202 wake_up_process(speedchange_task);
1205 static int cpufreq_interactive_input_connect(struct input_handler *handler,
1206 struct input_dev *dev, const struct input_device_id *id)
1208 struct input_handle *handle;
1211 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
1216 handle->handler = handler;
1217 handle->name = "cpufreq";
1219 error = input_register_handle(handle);
1223 error = input_open_device(handle);
1229 input_unregister_handle(handle);
1235 static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
1237 input_close_device(handle);
1238 input_unregister_handle(handle);
1242 static const struct input_device_id cpufreq_interactive_ids[] = {
1244 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1245 INPUT_DEVICE_ID_MATCH_ABSBIT,
1246 .evbit = { BIT_MASK(EV_ABS) },
1247 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
1248 BIT_MASK(ABS_MT_POSITION_X) |
1249 BIT_MASK(ABS_MT_POSITION_Y) },
1252 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
1253 INPUT_DEVICE_ID_MATCH_ABSBIT,
1254 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
1255 .absbit = { [BIT_WORD(ABS_X)] =
1256 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
1261 static struct input_handler cpufreq_interactive_input_handler = {
1262 .event = cpufreq_interactive_input_event,
1263 .connect = cpufreq_interactive_input_connect,
1264 .disconnect = cpufreq_interactive_input_disconnect,
1265 .name = "cpufreq_interactive",
1266 .id_table = cpufreq_interactive_ids,
1270 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1275 struct cpufreq_interactive_cpuinfo *pcpu;
1276 struct cpufreq_frequency_table *freq_table;
1277 struct cpufreq_interactive_tunables *tunables;
1278 unsigned long flags;
1280 if (have_governor_per_policy())
1281 tunables = policy->governor_data;
1283 tunables = common_tunables;
1285 WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1288 case CPUFREQ_GOV_POLICY_INIT:
1289 if (have_governor_per_policy()) {
1291 } else if (tunables) {
1292 tunables->usage_count++;
1293 policy->governor_data = tunables;
1297 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1299 pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1303 tunables->usage_count = 1;
1304 tunables->above_hispeed_delay = default_above_hispeed_delay;
1305 tunables->nabove_hispeed_delay =
1306 ARRAY_SIZE(default_above_hispeed_delay);
1307 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1308 tunables->target_loads = default_target_loads;
1309 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1310 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1311 tunables->timer_rate = DEFAULT_TIMER_RATE;
1312 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1313 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1315 spin_lock_init(&tunables->target_loads_lock);
1316 spin_lock_init(&tunables->above_hispeed_delay_lock);
1318 #ifdef CONFIG_ARCH_ROCKCHIP
1321 freq_table = cpufreq_frequency_get_table(policy->cpu);
1322 tunables->hispeed_freq = policy->max;
1323 if (policy->min < 600000)
1324 tunables->hispeed_freq = 600000;
1325 else if (cpufreq_frequency_table_target(policy, freq_table, policy->min + 1, CPUFREQ_RELATION_L, &index) == 0)
1326 tunables->hispeed_freq = freq_table[index].frequency;
1327 tunables->timer_slack_val = 20 * USEC_PER_MSEC;
1328 tunables->min_sample_time = 40 * USEC_PER_MSEC;
1329 store_above_hispeed_delay(tunables, "20000 1000000:80000 1200000:100000 1700000:20000", 0);
1330 store_target_loads(tunables, "70 600000:70 800000:75 1500000:80 1700000:90", 0);
1331 tunables->boostpulse_duration_val = 40 * USEC_PER_MSEC;
1332 tunables->touchboostpulse_duration_val = 500 * USEC_PER_MSEC;
1333 tunables->touchboost_freq = 1200000;
1337 policy->governor_data = tunables;
1338 if (!have_governor_per_policy())
1339 common_tunables = tunables;
1341 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1345 policy->governor_data = NULL;
1346 if (!have_governor_per_policy())
1347 common_tunables = NULL;
1351 if (!policy->governor->initialized) {
1352 idle_notifier_register(&cpufreq_interactive_idle_nb);
1353 cpufreq_register_notifier(&cpufreq_notifier_block,
1354 CPUFREQ_TRANSITION_NOTIFIER);
1355 #ifdef CONFIG_ARCH_ROCKCHIP
1356 rc = input_register_handler(&cpufreq_interactive_input_handler);
1362 case CPUFREQ_GOV_POLICY_EXIT:
1363 if (!--tunables->usage_count) {
1364 if (policy->governor->initialized == 1) {
1365 #ifdef CONFIG_ARCH_ROCKCHIP
1366 input_unregister_handler(&cpufreq_interactive_input_handler);
1368 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1369 CPUFREQ_TRANSITION_NOTIFIER);
1370 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1373 sysfs_remove_group(get_governor_parent_kobj(policy),
1376 common_tunables = NULL;
1379 policy->governor_data = NULL;
1382 case CPUFREQ_GOV_START:
1383 mutex_lock(&gov_lock);
1385 freq_table = cpufreq_frequency_get_table(policy->cpu);
1386 if (!tunables->hispeed_freq)
1387 tunables->hispeed_freq = policy->max;
1389 for_each_cpu(j, policy->cpus) {
1390 pcpu = &per_cpu(cpuinfo, j);
1391 pcpu->policy = policy;
1392 pcpu->target_freq = policy->cur;
1393 pcpu->freq_table = freq_table;
1394 pcpu->floor_freq = pcpu->target_freq;
1395 pcpu->floor_validate_time =
1396 ktime_to_us(ktime_get());
1397 pcpu->hispeed_validate_time =
1398 pcpu->floor_validate_time;
1399 pcpu->max_freq = policy->max;
1400 down_write(&pcpu->enable_sem);
1401 del_timer_sync(&pcpu->cpu_timer);
1402 del_timer_sync(&pcpu->cpu_slack_timer);
1403 cpufreq_interactive_timer_start(tunables, j);
1404 pcpu->governor_enabled = 1;
1405 up_write(&pcpu->enable_sem);
1408 mutex_unlock(&gov_lock);
1411 case CPUFREQ_GOV_STOP:
1412 mutex_lock(&gov_lock);
1413 for_each_cpu(j, policy->cpus) {
1414 pcpu = &per_cpu(cpuinfo, j);
1415 down_write(&pcpu->enable_sem);
1416 pcpu->governor_enabled = 0;
1417 del_timer_sync(&pcpu->cpu_timer);
1418 del_timer_sync(&pcpu->cpu_slack_timer);
1419 up_write(&pcpu->enable_sem);
1422 mutex_unlock(&gov_lock);
1425 case CPUFREQ_GOV_LIMITS:
1426 if (policy->max < policy->cur)
1427 __cpufreq_driver_target(policy,
1428 policy->max, CPUFREQ_RELATION_H);
1429 else if (policy->min > policy->cur)
1430 __cpufreq_driver_target(policy,
1431 policy->min, CPUFREQ_RELATION_L);
1432 for_each_cpu(j, policy->cpus) {
1433 pcpu = &per_cpu(cpuinfo, j);
1435 down_read(&pcpu->enable_sem);
1436 if (pcpu->governor_enabled == 0) {
1437 up_read(&pcpu->enable_sem);
1441 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
1442 if (policy->max < pcpu->target_freq)
1443 pcpu->target_freq = policy->max;
1444 else if (policy->min > pcpu->target_freq)
1445 pcpu->target_freq = policy->min;
1447 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
1448 up_read(&pcpu->enable_sem);
1450 /* Reschedule timer only if policy->max is raised.
1451 * Delete the timers, else the timer callback may
1452 * return without re-arm the timer when failed
1453 * acquire the semaphore. This race may cause timer
1454 * stopped unexpectedly.
1457 if (policy->max > pcpu->max_freq) {
1458 down_write(&pcpu->enable_sem);
1459 del_timer_sync(&pcpu->cpu_timer);
1460 del_timer_sync(&pcpu->cpu_slack_timer);
1461 cpufreq_interactive_timer_start(tunables, j);
1462 up_write(&pcpu->enable_sem);
1465 pcpu->max_freq = policy->max;
1472 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1475 struct cpufreq_governor cpufreq_gov_interactive = {
1476 .name = "interactive",
1477 .governor = cpufreq_governor_interactive,
1478 .max_transition_latency = 10000000,
1479 .owner = THIS_MODULE,
1482 static void cpufreq_interactive_nop_timer(unsigned long data)
1486 static int __init cpufreq_interactive_init(void)
1489 struct cpufreq_interactive_cpuinfo *pcpu;
1490 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1492 /* Initalize per-cpu timers */
1493 for_each_possible_cpu(i) {
1494 pcpu = &per_cpu(cpuinfo, i);
1495 init_timer_deferrable(&pcpu->cpu_timer);
1496 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1497 pcpu->cpu_timer.data = i;
1498 init_timer(&pcpu->cpu_slack_timer);
1499 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1500 spin_lock_init(&pcpu->load_lock);
1501 spin_lock_init(&pcpu->target_freq_lock);
1502 init_rwsem(&pcpu->enable_sem);
1505 spin_lock_init(&speedchange_cpumask_lock);
1506 mutex_init(&gov_lock);
1508 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1510 if (IS_ERR(speedchange_task))
1511 return PTR_ERR(speedchange_task);
1513 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1514 get_task_struct(speedchange_task);
1516 /* NB: wake up so the thread does not look hung to the freezer */
1517 wake_up_process(speedchange_task);
1519 return cpufreq_register_governor(&cpufreq_gov_interactive);
1522 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1523 fs_initcall(cpufreq_interactive_init);
1525 module_init(cpufreq_interactive_init);
1528 static void __exit cpufreq_interactive_exit(void)
1530 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1531 kthread_stop(speedchange_task);
1532 put_task_struct(speedchange_task);
1535 module_exit(cpufreq_interactive_exit);
1537 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1538 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1539 "Latency sensitive workloads");
1540 MODULE_LICENSE("GPL");