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>
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/cpufreq_interactive.h>
40 struct cpufreq_interactive_cpuinfo {
41 struct timer_list cpu_timer;
42 struct timer_list cpu_slack_timer;
43 spinlock_t load_lock; /* protects the next 4 fields */
45 u64 time_in_idle_timestamp;
47 u64 cputime_speedadj_timestamp;
48 struct cpufreq_policy *policy;
49 struct cpufreq_frequency_table *freq_table;
50 spinlock_t target_freq_lock; /*protects target freq */
51 unsigned int target_freq;
52 unsigned int floor_freq;
53 u64 pol_floor_val_time; /* policy floor_validate_time */
54 u64 loc_floor_val_time; /* per-cpu floor_validate_time */
55 u64 pol_hispeed_val_time; /* policy hispeed_validate_time */
56 u64 loc_hispeed_val_time; /* per-cpu 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;
326 unsigned int delta_idle;
327 unsigned int delta_time;
330 now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
331 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
332 delta_time = (unsigned int)(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;
362 if (!down_read_trylock(&pcpu->enable_sem))
364 if (!pcpu->governor_enabled)
367 spin_lock_irqsave(&pcpu->load_lock, flags);
368 now = update_load(data);
369 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
370 cputime_speedadj = pcpu->cputime_speedadj;
371 spin_unlock_irqrestore(&pcpu->load_lock, flags);
373 if (WARN_ON_ONCE(!delta_time))
376 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
377 do_div(cputime_speedadj, delta_time);
378 loadadjfreq = (unsigned int)cputime_speedadj * 100;
379 cpu_load = loadadjfreq / pcpu->policy->cur;
380 tunables->boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
382 if (cpu_load >= tunables->go_hispeed_load || tunables->boosted) {
383 if (pcpu->policy->cur < tunables->hispeed_freq) {
384 new_freq = tunables->hispeed_freq;
386 new_freq = choose_freq(pcpu, loadadjfreq);
388 if (new_freq < tunables->hispeed_freq)
389 new_freq = tunables->hispeed_freq;
392 new_freq = choose_freq(pcpu, loadadjfreq);
393 if (new_freq > tunables->hispeed_freq &&
394 pcpu->policy->cur < tunables->hispeed_freq)
395 new_freq = tunables->hispeed_freq;
397 #ifdef CONFIG_ARCH_ROCKCHIP
398 if ((now < tunables->touchboostpulse_endtime) &&
399 (new_freq < tunables->touchboost_freq)) {
400 new_freq = tunables->touchboost_freq;
403 if (pcpu->policy->cur >= tunables->hispeed_freq &&
404 new_freq > pcpu->policy->cur &&
405 now - pcpu->pol_hispeed_val_time <
406 freq_to_above_hispeed_delay(tunables, pcpu->policy->cur)) {
407 trace_cpufreq_interactive_notyet(
408 data, cpu_load, pcpu->target_freq,
409 pcpu->policy->cur, new_freq);
410 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
414 pcpu->loc_hispeed_val_time = now;
416 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
417 new_freq, CPUFREQ_RELATION_L,
419 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
423 new_freq = pcpu->freq_table[index].frequency;
426 * Do not scale below floor_freq unless we have been at or above the
427 * floor frequency for the minimum sample time since last validated.
429 max_fvtime = max(pcpu->pol_floor_val_time, pcpu->loc_floor_val_time);
430 if (new_freq < pcpu->floor_freq &&
431 pcpu->target_freq >= pcpu->policy->cur) {
432 if (now - max_fvtime < tunables->min_sample_time) {
433 trace_cpufreq_interactive_notyet(
434 data, cpu_load, pcpu->target_freq,
435 pcpu->policy->cur, new_freq);
436 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
442 * Update the timestamp for checking whether speed has been held at
443 * or above the selected frequency for a minimum of min_sample_time,
444 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
445 * allow the speed to drop as soon as the boostpulse duration expires
446 * (or the indefinite boost is turned off).
449 if (!tunables->boosted || new_freq > tunables->hispeed_freq) {
450 pcpu->floor_freq = new_freq;
451 if (pcpu->target_freq >= pcpu->policy->cur ||
452 new_freq >= pcpu->policy->cur)
453 pcpu->loc_floor_val_time = now;
456 if (pcpu->target_freq == new_freq &&
457 pcpu->target_freq <= pcpu->policy->cur) {
458 trace_cpufreq_interactive_already(
459 data, cpu_load, pcpu->target_freq,
460 pcpu->policy->cur, new_freq);
461 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
465 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
466 pcpu->policy->cur, new_freq);
468 pcpu->target_freq = new_freq;
469 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
470 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
471 cpumask_set_cpu(data, &speedchange_cpumask);
472 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
473 wake_up_process(speedchange_task);
476 if (!timer_pending(&pcpu->cpu_timer))
477 cpufreq_interactive_timer_resched(pcpu);
480 up_read(&pcpu->enable_sem);
484 static void cpufreq_interactive_idle_end(void)
486 struct cpufreq_interactive_cpuinfo *pcpu =
487 &per_cpu(cpuinfo, smp_processor_id());
489 if (!down_read_trylock(&pcpu->enable_sem))
491 if (!pcpu->governor_enabled) {
492 up_read(&pcpu->enable_sem);
496 /* Arm the timer for 1-2 ticks later if not already. */
497 if (!timer_pending(&pcpu->cpu_timer)) {
498 cpufreq_interactive_timer_resched(pcpu);
499 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
500 del_timer(&pcpu->cpu_timer);
501 del_timer(&pcpu->cpu_slack_timer);
502 cpufreq_interactive_timer(smp_processor_id());
505 up_read(&pcpu->enable_sem);
508 static void cpufreq_interactive_get_policy_info(struct cpufreq_policy *policy,
509 unsigned int *pmax_freq,
510 u64 *phvt, u64 *pfvt)
512 struct cpufreq_interactive_cpuinfo *pcpu;
513 unsigned int max_freq = 0;
514 u64 hvt = ~0ULL, fvt = 0;
517 for_each_cpu(i, policy->cpus) {
518 pcpu = &per_cpu(cpuinfo, i);
520 fvt = max(fvt, pcpu->loc_floor_val_time);
521 if (pcpu->target_freq > max_freq) {
522 max_freq = pcpu->target_freq;
523 hvt = pcpu->loc_hispeed_val_time;
524 } else if (pcpu->target_freq == max_freq) {
525 hvt = min(hvt, pcpu->loc_hispeed_val_time);
529 *pmax_freq = max_freq;
534 static void cpufreq_interactive_adjust_cpu(unsigned int cpu,
535 struct cpufreq_policy *policy)
537 struct cpufreq_interactive_cpuinfo *pcpu;
539 unsigned int max_freq;
542 cpufreq_interactive_get_policy_info(policy, &max_freq, &hvt, &fvt);
544 for_each_cpu(i, policy->cpus) {
545 pcpu = &per_cpu(cpuinfo, i);
546 pcpu->pol_floor_val_time = fvt;
549 if (max_freq != policy->cur) {
550 __cpufreq_driver_target(policy, max_freq, CPUFREQ_RELATION_H);
551 for_each_cpu(i, policy->cpus) {
552 pcpu = &per_cpu(cpuinfo, i);
553 pcpu->pol_hispeed_val_time = hvt;
557 trace_cpufreq_interactive_setspeed(cpu, max_freq, policy->cur);
560 static int cpufreq_interactive_speedchange_task(void *data)
565 struct cpufreq_interactive_cpuinfo *pcpu;
568 set_current_state(TASK_INTERRUPTIBLE);
569 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
571 if (cpumask_empty(&speedchange_cpumask)) {
572 spin_unlock_irqrestore(&speedchange_cpumask_lock,
576 if (kthread_should_stop())
579 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
582 set_current_state(TASK_RUNNING);
583 tmp_mask = speedchange_cpumask;
584 cpumask_clear(&speedchange_cpumask);
585 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
587 for_each_cpu(cpu, &tmp_mask) {
588 pcpu = &per_cpu(cpuinfo, cpu);
590 down_write(&pcpu->policy->rwsem);
592 if (likely(down_read_trylock(&pcpu->enable_sem))) {
593 if (likely(pcpu->governor_enabled))
594 cpufreq_interactive_adjust_cpu(cpu,
596 up_read(&pcpu->enable_sem);
599 up_write(&pcpu->policy->rwsem);
606 static void cpufreq_interactive_boost(struct cpufreq_interactive_tunables *tunables)
610 unsigned long flags[2];
611 struct cpufreq_interactive_cpuinfo *pcpu;
613 tunables->boosted = true;
615 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
617 for_each_online_cpu(i) {
618 pcpu = &per_cpu(cpuinfo, i);
620 if (!down_read_trylock(&pcpu->enable_sem))
623 if (!pcpu->governor_enabled) {
624 up_read(&pcpu->enable_sem);
628 if (tunables != pcpu->policy->governor_data) {
629 up_read(&pcpu->enable_sem);
633 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
634 if (pcpu->target_freq < tunables->hispeed_freq) {
635 pcpu->target_freq = tunables->hispeed_freq;
636 cpumask_set_cpu(i, &speedchange_cpumask);
637 pcpu->pol_hispeed_val_time =
638 ktime_to_us(ktime_get());
641 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
643 up_read(&pcpu->enable_sem);
646 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
649 wake_up_process(speedchange_task);
652 static int cpufreq_interactive_notifier(
653 struct notifier_block *nb, unsigned long val, void *data)
655 struct cpufreq_freqs *freq = data;
656 struct cpufreq_interactive_cpuinfo *pcpu;
660 if (val == CPUFREQ_POSTCHANGE) {
661 pcpu = &per_cpu(cpuinfo, freq->cpu);
662 if (!down_read_trylock(&pcpu->enable_sem))
664 if (!pcpu->governor_enabled) {
665 up_read(&pcpu->enable_sem);
669 for_each_cpu(cpu, pcpu->policy->cpus) {
670 struct cpufreq_interactive_cpuinfo *pjcpu =
671 &per_cpu(cpuinfo, cpu);
672 if (cpu != freq->cpu) {
673 if (!down_read_trylock(&pjcpu->enable_sem))
675 if (!pjcpu->governor_enabled) {
676 up_read(&pjcpu->enable_sem);
680 spin_lock_irqsave(&pjcpu->load_lock, flags);
682 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
683 if (cpu != freq->cpu)
684 up_read(&pjcpu->enable_sem);
687 up_read(&pcpu->enable_sem);
692 static struct notifier_block cpufreq_notifier_block = {
693 .notifier_call = cpufreq_interactive_notifier,
696 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
701 unsigned int *tokenized_data;
705 while ((cp = strpbrk(cp + 1, " :")))
708 if (!(ntokens & 0x1))
711 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
712 if (!tokenized_data) {
719 while (i < ntokens) {
720 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
723 cp = strpbrk(cp, " :");
732 *num_tokens = ntokens;
733 return tokenized_data;
736 kfree(tokenized_data);
741 static ssize_t show_target_loads(
742 struct cpufreq_interactive_tunables *tunables,
749 spin_lock_irqsave(&tunables->target_loads_lock, flags);
751 for (i = 0; i < tunables->ntarget_loads; i++)
752 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
753 i & 0x1 ? ":" : " ");
755 sprintf(buf + ret - 1, "\n");
756 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
760 static ssize_t store_target_loads(
761 struct cpufreq_interactive_tunables *tunables,
762 const char *buf, size_t count)
765 unsigned int *new_target_loads = NULL;
768 new_target_loads = get_tokenized_data(buf, &ntokens);
769 if (IS_ERR(new_target_loads))
770 return PTR_RET(new_target_loads);
772 spin_lock_irqsave(&tunables->target_loads_lock, flags);
773 if (tunables->target_loads != default_target_loads)
774 kfree(tunables->target_loads);
775 tunables->target_loads = new_target_loads;
776 tunables->ntarget_loads = ntokens;
777 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
781 static ssize_t show_above_hispeed_delay(
782 struct cpufreq_interactive_tunables *tunables, char *buf)
788 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
790 for (i = 0; i < tunables->nabove_hispeed_delay; i++)
791 ret += sprintf(buf + ret, "%u%s",
792 tunables->above_hispeed_delay[i],
793 i & 0x1 ? ":" : " ");
795 sprintf(buf + ret - 1, "\n");
796 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
800 static ssize_t store_above_hispeed_delay(
801 struct cpufreq_interactive_tunables *tunables,
802 const char *buf, size_t count)
805 unsigned int *new_above_hispeed_delay = NULL;
808 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
809 if (IS_ERR(new_above_hispeed_delay))
810 return PTR_RET(new_above_hispeed_delay);
812 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
813 if (tunables->above_hispeed_delay != default_above_hispeed_delay)
814 kfree(tunables->above_hispeed_delay);
815 tunables->above_hispeed_delay = new_above_hispeed_delay;
816 tunables->nabove_hispeed_delay = ntokens;
817 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
822 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
825 return sprintf(buf, "%u\n", tunables->hispeed_freq);
828 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
829 const char *buf, size_t count)
832 long unsigned int val;
834 ret = kstrtoul(buf, 0, &val);
837 tunables->hispeed_freq = val;
841 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
842 *tunables, char *buf)
844 return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
847 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
848 *tunables, const char *buf, size_t count)
853 ret = kstrtoul(buf, 0, &val);
856 tunables->go_hispeed_load = val;
860 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
861 *tunables, char *buf)
863 return sprintf(buf, "%lu\n", tunables->min_sample_time);
866 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
867 *tunables, const char *buf, size_t count)
872 ret = kstrtoul(buf, 0, &val);
875 tunables->min_sample_time = val;
879 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
882 return sprintf(buf, "%lu\n", tunables->timer_rate);
885 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
886 const char *buf, size_t count)
889 unsigned long val, val_round;
891 ret = kstrtoul(buf, 0, &val);
895 val_round = jiffies_to_usecs(usecs_to_jiffies(val));
896 if (val != val_round)
897 pr_warn("timer_rate not aligned to jiffy. Rounded up to %lu\n",
900 tunables->timer_rate = val_round;
904 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
907 return sprintf(buf, "%d\n", tunables->timer_slack_val);
910 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
911 const char *buf, size_t count)
916 ret = kstrtol(buf, 10, &val);
920 tunables->timer_slack_val = val;
924 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
927 return sprintf(buf, "%d\n", tunables->boost_val);
930 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
931 const char *buf, size_t count)
936 ret = kstrtoul(buf, 0, &val);
940 tunables->boost_val = val;
942 if (tunables->boost_val) {
943 trace_cpufreq_interactive_boost("on");
944 if (!tunables->boosted)
945 cpufreq_interactive_boost(tunables);
947 tunables->boostpulse_endtime = ktime_to_us(ktime_get());
948 trace_cpufreq_interactive_unboost("off");
954 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
955 const char *buf, size_t count)
960 ret = kstrtoul(buf, 0, &val);
964 tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
965 tunables->boostpulse_duration_val;
966 trace_cpufreq_interactive_boost("pulse");
967 if (!tunables->boosted)
968 cpufreq_interactive_boost(tunables);
972 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
973 *tunables, char *buf)
975 return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
978 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
979 *tunables, const char *buf, size_t count)
984 ret = kstrtoul(buf, 0, &val);
988 tunables->boostpulse_duration_val = val;
992 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
995 return sprintf(buf, "%u\n", tunables->io_is_busy);
998 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
999 const char *buf, size_t count)
1004 ret = kstrtoul(buf, 0, &val);
1007 tunables->io_is_busy = val;
1012 * Create show/store routines
1013 * - sys: One governor instance for complete SYSTEM
1014 * - pol: One governor instance per struct cpufreq_policy
1016 #define show_gov_pol_sys(file_name) \
1017 static ssize_t show_##file_name##_gov_sys \
1018 (struct kobject *kobj, struct attribute *attr, char *buf) \
1020 return show_##file_name(common_tunables, buf); \
1023 static ssize_t show_##file_name##_gov_pol \
1024 (struct cpufreq_policy *policy, char *buf) \
1026 return show_##file_name(policy->governor_data, buf); \
1029 #define store_gov_pol_sys(file_name) \
1030 static ssize_t store_##file_name##_gov_sys \
1031 (struct kobject *kobj, struct attribute *attr, const char *buf, \
1034 return store_##file_name(common_tunables, buf, count); \
1037 static ssize_t store_##file_name##_gov_pol \
1038 (struct cpufreq_policy *policy, const char *buf, size_t count) \
1040 return store_##file_name(policy->governor_data, buf, count); \
1043 #define show_store_gov_pol_sys(file_name) \
1044 show_gov_pol_sys(file_name); \
1045 store_gov_pol_sys(file_name)
1047 show_store_gov_pol_sys(target_loads);
1048 show_store_gov_pol_sys(above_hispeed_delay);
1049 show_store_gov_pol_sys(hispeed_freq);
1050 show_store_gov_pol_sys(go_hispeed_load);
1051 show_store_gov_pol_sys(min_sample_time);
1052 show_store_gov_pol_sys(timer_rate);
1053 show_store_gov_pol_sys(timer_slack);
1054 show_store_gov_pol_sys(boost);
1055 store_gov_pol_sys(boostpulse);
1056 show_store_gov_pol_sys(boostpulse_duration);
1057 show_store_gov_pol_sys(io_is_busy);
1059 #define gov_sys_attr_rw(_name) \
1060 static struct global_attr _name##_gov_sys = \
1061 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1063 #define gov_pol_attr_rw(_name) \
1064 static struct freq_attr _name##_gov_pol = \
1065 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1067 #define gov_sys_pol_attr_rw(_name) \
1068 gov_sys_attr_rw(_name); \
1069 gov_pol_attr_rw(_name)
1071 gov_sys_pol_attr_rw(target_loads);
1072 gov_sys_pol_attr_rw(above_hispeed_delay);
1073 gov_sys_pol_attr_rw(hispeed_freq);
1074 gov_sys_pol_attr_rw(go_hispeed_load);
1075 gov_sys_pol_attr_rw(min_sample_time);
1076 gov_sys_pol_attr_rw(timer_rate);
1077 gov_sys_pol_attr_rw(timer_slack);
1078 gov_sys_pol_attr_rw(boost);
1079 gov_sys_pol_attr_rw(boostpulse_duration);
1080 gov_sys_pol_attr_rw(io_is_busy);
1082 static struct global_attr boostpulse_gov_sys =
1083 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1085 static struct freq_attr boostpulse_gov_pol =
1086 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1088 /* One Governor instance for entire system */
1089 static struct attribute *interactive_attributes_gov_sys[] = {
1090 &target_loads_gov_sys.attr,
1091 &above_hispeed_delay_gov_sys.attr,
1092 &hispeed_freq_gov_sys.attr,
1093 &go_hispeed_load_gov_sys.attr,
1094 &min_sample_time_gov_sys.attr,
1095 &timer_rate_gov_sys.attr,
1096 &timer_slack_gov_sys.attr,
1097 &boost_gov_sys.attr,
1098 &boostpulse_gov_sys.attr,
1099 &boostpulse_duration_gov_sys.attr,
1100 &io_is_busy_gov_sys.attr,
1104 static struct attribute_group interactive_attr_group_gov_sys = {
1105 .attrs = interactive_attributes_gov_sys,
1106 .name = "interactive",
1109 /* Per policy governor instance */
1110 static struct attribute *interactive_attributes_gov_pol[] = {
1111 &target_loads_gov_pol.attr,
1112 &above_hispeed_delay_gov_pol.attr,
1113 &hispeed_freq_gov_pol.attr,
1114 &go_hispeed_load_gov_pol.attr,
1115 &min_sample_time_gov_pol.attr,
1116 &timer_rate_gov_pol.attr,
1117 &timer_slack_gov_pol.attr,
1118 &boost_gov_pol.attr,
1119 &boostpulse_gov_pol.attr,
1120 &boostpulse_duration_gov_pol.attr,
1121 &io_is_busy_gov_pol.attr,
1125 static struct attribute_group interactive_attr_group_gov_pol = {
1126 .attrs = interactive_attributes_gov_pol,
1127 .name = "interactive",
1130 static struct attribute_group *get_sysfs_attr(void)
1132 if (have_governor_per_policy())
1133 return &interactive_attr_group_gov_pol;
1135 return &interactive_attr_group_gov_sys;
1138 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1142 if (val == IDLE_END)
1143 cpufreq_interactive_idle_end();
1148 static struct notifier_block cpufreq_interactive_idle_nb = {
1149 .notifier_call = cpufreq_interactive_idle_notifier,
1152 #ifdef CONFIG_ARCH_ROCKCHIP
1153 static void cpufreq_interactive_input_event(struct input_handle *handle,
1161 unsigned long flags[2];
1162 struct cpufreq_interactive_cpuinfo *pcpu;
1163 struct cpufreq_interactive_tunables *tunables;
1165 if ((type != EV_ABS) && (type != EV_KEY))
1168 trace_cpufreq_interactive_boost("touch");
1169 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
1171 now = ktime_to_us(ktime_get());
1172 for_each_online_cpu(i) {
1173 pcpu = &per_cpu(cpuinfo, i);
1177 if (have_governor_per_policy())
1178 tunables = pcpu->policy->governor_data;
1180 tunables = common_tunables;
1184 endtime = now + tunables->touchboostpulse_duration_val;
1185 if (endtime < (tunables->touchboostpulse_endtime +
1186 10 * USEC_PER_MSEC))
1188 tunables->touchboostpulse_endtime = endtime;
1190 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
1191 if (pcpu->target_freq < tunables->touchboost_freq) {
1192 pcpu->target_freq = tunables->touchboost_freq;
1193 cpumask_set_cpu(i, &speedchange_cpumask);
1194 pcpu->loc_hispeed_val_time =
1195 ktime_to_us(ktime_get());
1199 pcpu->floor_freq = tunables->touchboost_freq;
1200 pcpu->loc_floor_val_time = ktime_to_us(ktime_get());
1202 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
1205 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
1208 wake_up_process(speedchange_task);
1211 static int cpufreq_interactive_input_connect(struct input_handler *handler,
1212 struct input_dev *dev,
1213 const struct input_device_id *id)
1215 struct input_handle *handle;
1218 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1223 handle->handler = handler;
1224 handle->name = "cpufreq";
1226 error = input_register_handle(handle);
1230 error = input_open_device(handle);
1236 input_unregister_handle(handle);
1242 static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
1244 input_close_device(handle);
1245 input_unregister_handle(handle);
1249 static const struct input_device_id cpufreq_interactive_ids[] = {
1251 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1252 INPUT_DEVICE_ID_MATCH_ABSBIT,
1253 .evbit = { BIT_MASK(EV_ABS) },
1254 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
1255 BIT_MASK(ABS_MT_POSITION_X) |
1256 BIT_MASK(ABS_MT_POSITION_Y) },
1259 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
1260 INPUT_DEVICE_ID_MATCH_ABSBIT,
1261 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
1262 .absbit = { [BIT_WORD(ABS_X)] =
1263 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
1266 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1267 .evbit = { BIT_MASK(EV_KEY) },
1272 static struct input_handler cpufreq_interactive_input_handler = {
1273 .event = cpufreq_interactive_input_event,
1274 .connect = cpufreq_interactive_input_connect,
1275 .disconnect = cpufreq_interactive_input_disconnect,
1276 .name = "cpufreq_interactive",
1277 .id_table = cpufreq_interactive_ids,
1280 static void rockchip_cpufreq_policy_init(struct cpufreq_policy *policy)
1282 struct cpufreq_interactive_tunables *tunables = policy->governor_data;
1284 tunables->min_sample_time = 40 * USEC_PER_MSEC;
1285 tunables->boostpulse_duration_val = 40 * USEC_PER_MSEC;
1286 if (policy->cpu == 0) {
1287 tunables->hispeed_freq = 1008000;
1288 tunables->touchboostpulse_duration_val = 500 * USEC_PER_MSEC;
1289 tunables->touchboost_freq = 1200000;
1291 tunables->hispeed_freq = 816000;
1296 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1301 struct cpufreq_interactive_cpuinfo *pcpu;
1302 struct cpufreq_frequency_table *freq_table;
1303 struct cpufreq_interactive_tunables *tunables;
1304 unsigned long flags;
1306 if (have_governor_per_policy())
1307 tunables = policy->governor_data;
1309 tunables = common_tunables;
1311 WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1314 case CPUFREQ_GOV_POLICY_INIT:
1315 if (have_governor_per_policy()) {
1317 } else if (tunables) {
1318 tunables->usage_count++;
1319 policy->governor_data = tunables;
1323 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1325 pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1329 tunables->usage_count = 1;
1330 tunables->above_hispeed_delay = default_above_hispeed_delay;
1331 tunables->nabove_hispeed_delay =
1332 ARRAY_SIZE(default_above_hispeed_delay);
1333 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1334 tunables->target_loads = default_target_loads;
1335 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1336 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1337 tunables->timer_rate = DEFAULT_TIMER_RATE;
1338 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1339 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1341 spin_lock_init(&tunables->target_loads_lock);
1342 spin_lock_init(&tunables->above_hispeed_delay_lock);
1344 policy->governor_data = tunables;
1345 if (!have_governor_per_policy()) {
1346 common_tunables = tunables;
1349 #ifdef CONFIG_ARCH_ROCKCHIP
1350 rockchip_cpufreq_policy_init(policy);
1353 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1357 policy->governor_data = NULL;
1358 if (!have_governor_per_policy()) {
1359 common_tunables = NULL;
1364 if (!policy->governor->initialized) {
1365 idle_notifier_register(&cpufreq_interactive_idle_nb);
1366 cpufreq_register_notifier(&cpufreq_notifier_block,
1367 CPUFREQ_TRANSITION_NOTIFIER);
1368 #ifdef CONFIG_ARCH_ROCKCHIP
1369 rc = input_register_handler(&cpufreq_interactive_input_handler);
1376 case CPUFREQ_GOV_POLICY_EXIT:
1377 if (!--tunables->usage_count) {
1378 if (policy->governor->initialized == 1) {
1379 #ifdef CONFIG_ARCH_ROCKCHIP
1380 input_unregister_handler(&cpufreq_interactive_input_handler);
1382 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1383 CPUFREQ_TRANSITION_NOTIFIER);
1384 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1387 sysfs_remove_group(get_governor_parent_kobj(policy),
1391 common_tunables = NULL;
1394 policy->governor_data = NULL;
1397 case CPUFREQ_GOV_START:
1398 mutex_lock(&gov_lock);
1400 freq_table = cpufreq_frequency_get_table(policy->cpu);
1401 if (!tunables->hispeed_freq)
1402 tunables->hispeed_freq = policy->max;
1404 for_each_cpu(j, policy->cpus) {
1405 pcpu = &per_cpu(cpuinfo, j);
1406 pcpu->policy = policy;
1407 pcpu->target_freq = policy->cur;
1408 pcpu->freq_table = freq_table;
1409 pcpu->floor_freq = pcpu->target_freq;
1410 pcpu->pol_floor_val_time =
1411 ktime_to_us(ktime_get());
1412 pcpu->loc_floor_val_time = pcpu->pol_floor_val_time;
1413 pcpu->pol_hispeed_val_time = pcpu->pol_floor_val_time;
1414 pcpu->loc_hispeed_val_time = pcpu->pol_floor_val_time;
1415 down_write(&pcpu->enable_sem);
1416 del_timer_sync(&pcpu->cpu_timer);
1417 del_timer_sync(&pcpu->cpu_slack_timer);
1418 cpufreq_interactive_timer_start(tunables, j);
1419 pcpu->governor_enabled = 1;
1420 up_write(&pcpu->enable_sem);
1423 mutex_unlock(&gov_lock);
1426 case CPUFREQ_GOV_STOP:
1427 mutex_lock(&gov_lock);
1428 for_each_cpu(j, policy->cpus) {
1429 pcpu = &per_cpu(cpuinfo, j);
1430 down_write(&pcpu->enable_sem);
1431 pcpu->governor_enabled = 0;
1432 del_timer_sync(&pcpu->cpu_timer);
1433 del_timer_sync(&pcpu->cpu_slack_timer);
1434 up_write(&pcpu->enable_sem);
1437 mutex_unlock(&gov_lock);
1440 case CPUFREQ_GOV_LIMITS:
1441 if (policy->max < policy->cur)
1442 __cpufreq_driver_target(policy,
1443 policy->max, CPUFREQ_RELATION_H);
1444 else if (policy->min > policy->cur)
1445 __cpufreq_driver_target(policy,
1446 policy->min, CPUFREQ_RELATION_L);
1447 for_each_cpu(j, policy->cpus) {
1448 pcpu = &per_cpu(cpuinfo, j);
1450 down_read(&pcpu->enable_sem);
1451 if (pcpu->governor_enabled == 0) {
1452 up_read(&pcpu->enable_sem);
1456 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
1457 if (policy->max < pcpu->target_freq)
1458 pcpu->target_freq = policy->max;
1459 else if (policy->min > pcpu->target_freq)
1460 pcpu->target_freq = policy->min;
1462 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
1463 up_read(&pcpu->enable_sem);
1470 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1473 struct cpufreq_governor cpufreq_gov_interactive = {
1474 .name = "interactive",
1475 .governor = cpufreq_governor_interactive,
1476 .max_transition_latency = 10000000,
1477 .owner = THIS_MODULE,
1480 static void cpufreq_interactive_nop_timer(unsigned long data)
1484 static int __init cpufreq_interactive_init(void)
1487 struct cpufreq_interactive_cpuinfo *pcpu;
1488 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1490 /* Initalize per-cpu timers */
1491 for_each_possible_cpu(i) {
1492 pcpu = &per_cpu(cpuinfo, i);
1493 init_timer_deferrable(&pcpu->cpu_timer);
1494 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1495 pcpu->cpu_timer.data = i;
1496 init_timer(&pcpu->cpu_slack_timer);
1497 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1498 spin_lock_init(&pcpu->load_lock);
1499 spin_lock_init(&pcpu->target_freq_lock);
1500 init_rwsem(&pcpu->enable_sem);
1503 spin_lock_init(&speedchange_cpumask_lock);
1504 mutex_init(&gov_lock);
1506 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1508 if (IS_ERR(speedchange_task))
1509 return PTR_ERR(speedchange_task);
1511 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1512 get_task_struct(speedchange_task);
1514 /* NB: wake up so the thread does not look hung to the freezer */
1515 wake_up_process(speedchange_task);
1517 return cpufreq_register_governor(&cpufreq_gov_interactive);
1520 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1521 fs_initcall(cpufreq_interactive_init);
1523 module_init(cpufreq_interactive_init);
1526 static void __exit cpufreq_interactive_exit(void)
1528 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1529 kthread_stop(speedchange_task);
1530 put_task_struct(speedchange_task);
1533 module_exit(cpufreq_interactive_exit);
1535 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1536 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1537 "Latency sensitive workloads");
1538 MODULE_LICENSE("GPL");