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 static int active_count;
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 unsigned int target_freq;
52 unsigned int floor_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 /* Hi speed to bump to from lo speed when load burst (default max) */
68 static unsigned int hispeed_freq;
70 /* Go to hi speed when CPU load at or above this value. */
71 #define DEFAULT_GO_HISPEED_LOAD 99
72 static unsigned long go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
74 /* Target load. Lower values result in higher CPU speeds. */
75 #define DEFAULT_TARGET_LOAD 90
76 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
77 static spinlock_t target_loads_lock;
78 static unsigned int *target_loads = default_target_loads;
79 static int ntarget_loads = ARRAY_SIZE(default_target_loads);
82 * The minimum amount of time to spend at a frequency before we can ramp down.
84 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
85 static unsigned long min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
88 * The sample rate of the timer used to increase frequency
90 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
91 static unsigned long timer_rate = DEFAULT_TIMER_RATE;
94 * Wait this long before raising speed above hispeed, by default a single
97 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
98 static unsigned int default_above_hispeed_delay[] = {
99 DEFAULT_ABOVE_HISPEED_DELAY };
100 static spinlock_t above_hispeed_delay_lock;
101 static unsigned int *above_hispeed_delay = default_above_hispeed_delay;
102 static int nabove_hispeed_delay = ARRAY_SIZE(default_above_hispeed_delay);
104 /* Non-zero means indefinite speed boost active */
105 static int boost_val;
106 /* Duration of a boot pulse in usecs */
107 static int boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
108 /* End time of boost pulse in ktime converted to usecs */
109 static u64 boostpulse_endtime;
112 * Max additional time to wait in idle, beyond timer_rate, at speeds above
113 * minimum before wakeup to reduce speed, or -1 if unnecessary.
115 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
116 static int timer_slack_val = DEFAULT_TIMER_SLACK;
118 static bool io_is_busy;
120 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
123 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
126 struct cpufreq_governor cpufreq_gov_interactive = {
127 .name = "interactive",
128 .governor = cpufreq_governor_interactive,
129 .max_transition_latency = 10000000,
130 .owner = THIS_MODULE,
133 static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
140 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
142 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
143 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
144 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
145 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
146 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
147 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
149 idle_time = cur_wall_time - busy_time;
151 *wall = jiffies_to_usecs(cur_wall_time);
153 return jiffies_to_usecs(idle_time);
156 static inline cputime64_t get_cpu_idle_time(unsigned int cpu,
159 u64 idle_time = get_cpu_idle_time_us(cpu, wall);
161 if (idle_time == -1ULL)
162 idle_time = get_cpu_idle_time_jiffy(cpu, wall);
163 else if (!io_is_busy)
164 idle_time += get_cpu_iowait_time_us(cpu, wall);
169 static void cpufreq_interactive_timer_resched(
170 struct cpufreq_interactive_cpuinfo *pcpu)
172 unsigned long expires = jiffies + usecs_to_jiffies(timer_rate);
175 mod_timer_pinned(&pcpu->cpu_timer, expires);
176 if (timer_slack_val >= 0 && pcpu->target_freq > pcpu->policy->min) {
177 expires += usecs_to_jiffies(timer_slack_val);
178 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
181 spin_lock_irqsave(&pcpu->load_lock, flags);
183 get_cpu_idle_time(smp_processor_id(),
184 &pcpu->time_in_idle_timestamp);
185 pcpu->cputime_speedadj = 0;
186 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
187 spin_unlock_irqrestore(&pcpu->load_lock, flags);
190 static unsigned int freq_to_above_hispeed_delay(unsigned int freq)
196 spin_lock_irqsave(&above_hispeed_delay_lock, flags);
198 for (i = 0; i < nabove_hispeed_delay - 1 &&
199 freq >= above_hispeed_delay[i+1]; i += 2)
202 ret = above_hispeed_delay[i];
203 spin_unlock_irqrestore(&above_hispeed_delay_lock, flags);
207 static unsigned int freq_to_targetload(unsigned int freq)
213 spin_lock_irqsave(&target_loads_lock, flags);
215 for (i = 0; i < ntarget_loads - 1 && freq >= target_loads[i+1]; i += 2)
218 ret = target_loads[i];
219 spin_unlock_irqrestore(&target_loads_lock, flags);
224 * If increasing frequencies never map to a lower target load then
225 * choose_freq() will find the minimum frequency that does not exceed its
226 * target load given the current load.
229 static unsigned int choose_freq(
230 struct cpufreq_interactive_cpuinfo *pcpu, unsigned int loadadjfreq)
232 unsigned int freq = pcpu->policy->cur;
233 unsigned int prevfreq, freqmin, freqmax;
242 tl = freq_to_targetload(freq);
245 * Find the lowest frequency where the computed load is less
246 * than or equal to the target load.
249 cpufreq_frequency_table_target(
250 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
251 CPUFREQ_RELATION_L, &index);
252 freq = pcpu->freq_table[index].frequency;
254 if (freq > prevfreq) {
255 /* The previous frequency is too low. */
258 if (freq >= freqmax) {
260 * Find the highest frequency that is less
263 cpufreq_frequency_table_target(
264 pcpu->policy, pcpu->freq_table,
265 freqmax - 1, CPUFREQ_RELATION_H,
267 freq = pcpu->freq_table[index].frequency;
269 if (freq == freqmin) {
271 * The first frequency below freqmax
272 * has already been found to be too
273 * low. freqmax is the lowest speed
274 * we found that is fast enough.
280 } else if (freq < prevfreq) {
281 /* The previous frequency is high enough. */
284 if (freq <= freqmin) {
286 * Find the lowest frequency that is higher
289 cpufreq_frequency_table_target(
290 pcpu->policy, pcpu->freq_table,
291 freqmin + 1, CPUFREQ_RELATION_L,
293 freq = pcpu->freq_table[index].frequency;
296 * If freqmax is the first frequency above
297 * freqmin then we have already found that
298 * this speed is fast enough.
305 /* If same frequency chosen as previous then done. */
306 } while (freq != prevfreq);
311 static u64 update_load(int cpu)
313 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
316 unsigned int delta_idle;
317 unsigned int delta_time;
320 now_idle = get_cpu_idle_time(cpu, &now);
321 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
322 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
323 active_time = delta_time - delta_idle;
324 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
326 pcpu->time_in_idle = now_idle;
327 pcpu->time_in_idle_timestamp = now;
331 static void cpufreq_interactive_timer(unsigned long data)
334 unsigned int delta_time;
335 u64 cputime_speedadj;
337 struct cpufreq_interactive_cpuinfo *pcpu =
338 &per_cpu(cpuinfo, data);
339 unsigned int new_freq;
340 unsigned int loadadjfreq;
345 if (!down_read_trylock(&pcpu->enable_sem))
347 if (!pcpu->governor_enabled)
350 spin_lock_irqsave(&pcpu->load_lock, flags);
351 now = update_load(data);
352 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
353 cputime_speedadj = pcpu->cputime_speedadj;
354 spin_unlock_irqrestore(&pcpu->load_lock, flags);
356 if (WARN_ON_ONCE(!delta_time))
359 do_div(cputime_speedadj, delta_time);
360 loadadjfreq = (unsigned int)cputime_speedadj * 100;
361 cpu_load = loadadjfreq / pcpu->target_freq;
362 boosted = boost_val || now < boostpulse_endtime;
364 if (cpu_load >= go_hispeed_load || boosted) {
365 if (pcpu->target_freq < hispeed_freq) {
366 new_freq = hispeed_freq;
368 new_freq = choose_freq(pcpu, loadadjfreq);
370 if (new_freq < hispeed_freq)
371 new_freq = hispeed_freq;
374 new_freq = choose_freq(pcpu, loadadjfreq);
377 if (pcpu->target_freq >= hispeed_freq &&
378 new_freq > pcpu->target_freq &&
379 now - pcpu->hispeed_validate_time <
380 freq_to_above_hispeed_delay(pcpu->target_freq)) {
381 trace_cpufreq_interactive_notyet(
382 data, cpu_load, pcpu->target_freq,
383 pcpu->policy->cur, new_freq);
387 pcpu->hispeed_validate_time = now;
389 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
390 new_freq, CPUFREQ_RELATION_L,
392 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
397 new_freq = pcpu->freq_table[index].frequency;
400 * Do not scale below floor_freq unless we have been at or above the
401 * floor frequency for the minimum sample time since last validated.
403 if (new_freq < pcpu->floor_freq) {
404 if (now - pcpu->floor_validate_time < min_sample_time) {
405 trace_cpufreq_interactive_notyet(
406 data, cpu_load, pcpu->target_freq,
407 pcpu->policy->cur, new_freq);
413 * Update the timestamp for checking whether speed has been held at
414 * or above the selected frequency for a minimum of min_sample_time,
415 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
416 * allow the speed to drop as soon as the boostpulse duration expires
417 * (or the indefinite boost is turned off).
420 if (!boosted || new_freq > hispeed_freq) {
421 pcpu->floor_freq = new_freq;
422 pcpu->floor_validate_time = now;
425 if (pcpu->target_freq == new_freq) {
426 trace_cpufreq_interactive_already(
427 data, cpu_load, pcpu->target_freq,
428 pcpu->policy->cur, new_freq);
429 goto rearm_if_notmax;
432 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
433 pcpu->policy->cur, new_freq);
435 pcpu->target_freq = new_freq;
436 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
437 cpumask_set_cpu(data, &speedchange_cpumask);
438 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
439 wake_up_process(speedchange_task);
443 * Already set max speed and don't see a need to change that,
444 * wait until next idle to re-evaluate, don't need timer.
446 if (pcpu->target_freq == pcpu->policy->max)
450 if (!timer_pending(&pcpu->cpu_timer))
451 cpufreq_interactive_timer_resched(pcpu);
454 up_read(&pcpu->enable_sem);
458 static void cpufreq_interactive_idle_start(void)
460 struct cpufreq_interactive_cpuinfo *pcpu =
461 &per_cpu(cpuinfo, smp_processor_id());
464 if (!down_read_trylock(&pcpu->enable_sem))
466 if (!pcpu->governor_enabled) {
467 up_read(&pcpu->enable_sem);
471 pending = timer_pending(&pcpu->cpu_timer);
473 if (pcpu->target_freq != pcpu->policy->min) {
475 * Entering idle while not at lowest speed. On some
476 * platforms this can hold the other CPU(s) at that speed
477 * even though the CPU is idle. Set a timer to re-evaluate
478 * speed so this idle CPU doesn't hold the other CPUs above
479 * min indefinitely. This should probably be a quirk of
480 * the CPUFreq driver.
483 cpufreq_interactive_timer_resched(pcpu);
486 up_read(&pcpu->enable_sem);
489 static void cpufreq_interactive_idle_end(void)
491 struct cpufreq_interactive_cpuinfo *pcpu =
492 &per_cpu(cpuinfo, smp_processor_id());
494 if (!down_read_trylock(&pcpu->enable_sem))
496 if (!pcpu->governor_enabled) {
497 up_read(&pcpu->enable_sem);
501 /* Arm the timer for 1-2 ticks later if not already. */
502 if (!timer_pending(&pcpu->cpu_timer)) {
503 cpufreq_interactive_timer_resched(pcpu);
504 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
505 del_timer(&pcpu->cpu_timer);
506 del_timer(&pcpu->cpu_slack_timer);
507 cpufreq_interactive_timer(smp_processor_id());
510 up_read(&pcpu->enable_sem);
513 static int cpufreq_interactive_speedchange_task(void *data)
518 struct cpufreq_interactive_cpuinfo *pcpu;
521 set_current_state(TASK_INTERRUPTIBLE);
522 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
524 if (cpumask_empty(&speedchange_cpumask)) {
525 spin_unlock_irqrestore(&speedchange_cpumask_lock,
529 if (kthread_should_stop())
532 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
535 set_current_state(TASK_RUNNING);
536 tmp_mask = speedchange_cpumask;
537 cpumask_clear(&speedchange_cpumask);
538 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
540 for_each_cpu(cpu, &tmp_mask) {
542 unsigned int max_freq = 0;
544 pcpu = &per_cpu(cpuinfo, cpu);
545 if (!down_read_trylock(&pcpu->enable_sem))
547 if (!pcpu->governor_enabled) {
548 up_read(&pcpu->enable_sem);
552 for_each_cpu(j, pcpu->policy->cpus) {
553 struct cpufreq_interactive_cpuinfo *pjcpu =
554 &per_cpu(cpuinfo, j);
556 if (pjcpu->target_freq > max_freq)
557 max_freq = pjcpu->target_freq;
560 if (max_freq != pcpu->policy->cur)
561 __cpufreq_driver_target(pcpu->policy,
564 trace_cpufreq_interactive_setspeed(cpu,
568 up_read(&pcpu->enable_sem);
575 static void cpufreq_interactive_boost(void)
580 struct cpufreq_interactive_cpuinfo *pcpu;
582 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
584 for_each_online_cpu(i) {
585 pcpu = &per_cpu(cpuinfo, i);
587 if (pcpu->target_freq < hispeed_freq) {
588 pcpu->target_freq = hispeed_freq;
589 cpumask_set_cpu(i, &speedchange_cpumask);
590 pcpu->hispeed_validate_time =
591 ktime_to_us(ktime_get());
596 * Set floor freq and (re)start timer for when last
600 pcpu->floor_freq = hispeed_freq;
601 pcpu->floor_validate_time = ktime_to_us(ktime_get());
604 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
607 wake_up_process(speedchange_task);
610 static int cpufreq_interactive_notifier(
611 struct notifier_block *nb, unsigned long val, void *data)
613 struct cpufreq_freqs *freq = data;
614 struct cpufreq_interactive_cpuinfo *pcpu;
618 if (val == CPUFREQ_POSTCHANGE) {
619 pcpu = &per_cpu(cpuinfo, freq->cpu);
620 if (!down_read_trylock(&pcpu->enable_sem))
622 if (!pcpu->governor_enabled) {
623 up_read(&pcpu->enable_sem);
627 for_each_cpu(cpu, pcpu->policy->cpus) {
628 struct cpufreq_interactive_cpuinfo *pjcpu =
629 &per_cpu(cpuinfo, cpu);
630 spin_lock_irqsave(&pjcpu->load_lock, flags);
632 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
635 up_read(&pcpu->enable_sem);
640 static struct notifier_block cpufreq_notifier_block = {
641 .notifier_call = cpufreq_interactive_notifier,
644 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
649 unsigned int *tokenized_data;
653 while ((cp = strpbrk(cp + 1, " :")))
656 if (!(ntokens & 0x1))
659 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
660 if (!tokenized_data) {
667 while (i < ntokens) {
668 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
671 cp = strpbrk(cp, " :");
680 *num_tokens = ntokens;
681 return tokenized_data;
684 kfree(tokenized_data);
689 static ssize_t show_target_loads(
690 struct kobject *kobj, struct attribute *attr, char *buf)
696 spin_lock_irqsave(&target_loads_lock, flags);
698 for (i = 0; i < ntarget_loads; i++)
699 ret += sprintf(buf + ret, "%u%s", target_loads[i],
700 i & 0x1 ? ":" : " ");
702 ret += sprintf(buf + ret, "\n");
703 spin_unlock_irqrestore(&target_loads_lock, flags);
707 static ssize_t store_target_loads(
708 struct kobject *kobj, struct attribute *attr, const char *buf,
712 unsigned int *new_target_loads = NULL;
715 new_target_loads = get_tokenized_data(buf, &ntokens);
716 if (IS_ERR(new_target_loads))
717 return PTR_RET(new_target_loads);
719 spin_lock_irqsave(&target_loads_lock, flags);
720 if (target_loads != default_target_loads)
722 target_loads = new_target_loads;
723 ntarget_loads = ntokens;
724 spin_unlock_irqrestore(&target_loads_lock, flags);
728 static struct global_attr target_loads_attr =
729 __ATTR(target_loads, S_IRUGO | S_IWUSR,
730 show_target_loads, store_target_loads);
732 static ssize_t show_above_hispeed_delay(
733 struct kobject *kobj, struct attribute *attr, char *buf)
739 spin_lock_irqsave(&above_hispeed_delay_lock, flags);
741 for (i = 0; i < nabove_hispeed_delay; i++)
742 ret += sprintf(buf + ret, "%u%s", above_hispeed_delay[i],
743 i & 0x1 ? ":" : " ");
745 ret += sprintf(buf + ret, "\n");
746 spin_unlock_irqrestore(&above_hispeed_delay_lock, flags);
750 static ssize_t store_above_hispeed_delay(
751 struct kobject *kobj, struct attribute *attr, const char *buf,
755 unsigned int *new_above_hispeed_delay = NULL;
758 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
759 if (IS_ERR(new_above_hispeed_delay))
760 return PTR_RET(new_above_hispeed_delay);
762 spin_lock_irqsave(&above_hispeed_delay_lock, flags);
763 if (above_hispeed_delay != default_above_hispeed_delay)
764 kfree(above_hispeed_delay);
765 above_hispeed_delay = new_above_hispeed_delay;
766 nabove_hispeed_delay = ntokens;
767 spin_unlock_irqrestore(&above_hispeed_delay_lock, flags);
772 static struct global_attr above_hispeed_delay_attr =
773 __ATTR(above_hispeed_delay, S_IRUGO | S_IWUSR,
774 show_above_hispeed_delay, store_above_hispeed_delay);
776 static ssize_t show_hispeed_freq(struct kobject *kobj,
777 struct attribute *attr, char *buf)
779 return sprintf(buf, "%u\n", hispeed_freq);
782 static ssize_t store_hispeed_freq(struct kobject *kobj,
783 struct attribute *attr, const char *buf,
787 long unsigned int val;
789 ret = strict_strtoul(buf, 0, &val);
796 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
797 show_hispeed_freq, store_hispeed_freq);
800 static ssize_t show_go_hispeed_load(struct kobject *kobj,
801 struct attribute *attr, char *buf)
803 return sprintf(buf, "%lu\n", go_hispeed_load);
806 static ssize_t store_go_hispeed_load(struct kobject *kobj,
807 struct attribute *attr, const char *buf, size_t count)
812 ret = strict_strtoul(buf, 0, &val);
815 go_hispeed_load = val;
819 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
820 show_go_hispeed_load, store_go_hispeed_load);
822 static ssize_t show_min_sample_time(struct kobject *kobj,
823 struct attribute *attr, char *buf)
825 return sprintf(buf, "%lu\n", min_sample_time);
828 static ssize_t store_min_sample_time(struct kobject *kobj,
829 struct attribute *attr, const char *buf, size_t count)
834 ret = strict_strtoul(buf, 0, &val);
837 min_sample_time = val;
841 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
842 show_min_sample_time, store_min_sample_time);
844 static ssize_t show_timer_rate(struct kobject *kobj,
845 struct attribute *attr, char *buf)
847 return sprintf(buf, "%lu\n", timer_rate);
850 static ssize_t store_timer_rate(struct kobject *kobj,
851 struct attribute *attr, const char *buf, size_t count)
856 ret = strict_strtoul(buf, 0, &val);
863 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
864 show_timer_rate, store_timer_rate);
866 static ssize_t show_timer_slack(
867 struct kobject *kobj, struct attribute *attr, char *buf)
869 return sprintf(buf, "%d\n", timer_slack_val);
872 static ssize_t store_timer_slack(
873 struct kobject *kobj, struct attribute *attr, const char *buf,
879 ret = kstrtol(buf, 10, &val);
883 timer_slack_val = val;
887 define_one_global_rw(timer_slack);
889 static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
892 return sprintf(buf, "%d\n", boost_val);
895 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
896 const char *buf, size_t count)
901 ret = kstrtoul(buf, 0, &val);
908 trace_cpufreq_interactive_boost("on");
909 cpufreq_interactive_boost();
911 trace_cpufreq_interactive_unboost("off");
917 define_one_global_rw(boost);
919 static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
920 const char *buf, size_t count)
925 ret = kstrtoul(buf, 0, &val);
929 boostpulse_endtime = ktime_to_us(ktime_get()) + boostpulse_duration_val;
930 trace_cpufreq_interactive_boost("pulse");
931 cpufreq_interactive_boost();
935 static struct global_attr boostpulse =
936 __ATTR(boostpulse, 0200, NULL, store_boostpulse);
938 static ssize_t show_boostpulse_duration(
939 struct kobject *kobj, struct attribute *attr, char *buf)
941 return sprintf(buf, "%d\n", boostpulse_duration_val);
944 static ssize_t store_boostpulse_duration(
945 struct kobject *kobj, struct attribute *attr, const char *buf,
951 ret = kstrtoul(buf, 0, &val);
955 boostpulse_duration_val = val;
959 define_one_global_rw(boostpulse_duration);
961 static ssize_t show_io_is_busy(struct kobject *kobj,
962 struct attribute *attr, char *buf)
964 return sprintf(buf, "%u\n", io_is_busy);
967 static ssize_t store_io_is_busy(struct kobject *kobj,
968 struct attribute *attr, const char *buf, size_t count)
973 ret = kstrtoul(buf, 0, &val);
980 static struct global_attr io_is_busy_attr = __ATTR(io_is_busy, 0644,
981 show_io_is_busy, store_io_is_busy);
983 static struct attribute *interactive_attributes[] = {
984 &target_loads_attr.attr,
985 &above_hispeed_delay_attr.attr,
986 &hispeed_freq_attr.attr,
987 &go_hispeed_load_attr.attr,
988 &min_sample_time_attr.attr,
989 &timer_rate_attr.attr,
993 &boostpulse_duration.attr,
994 &io_is_busy_attr.attr,
998 static struct attribute_group interactive_attr_group = {
999 .attrs = interactive_attributes,
1000 .name = "interactive",
1003 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1009 cpufreq_interactive_idle_start();
1012 cpufreq_interactive_idle_end();
1019 static struct notifier_block cpufreq_interactive_idle_nb = {
1020 .notifier_call = cpufreq_interactive_idle_notifier,
1023 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1028 struct cpufreq_interactive_cpuinfo *pcpu;
1029 struct cpufreq_frequency_table *freq_table;
1032 case CPUFREQ_GOV_START:
1033 if (!cpu_online(policy->cpu))
1036 mutex_lock(&gov_lock);
1039 cpufreq_frequency_get_table(policy->cpu);
1041 hispeed_freq = policy->max;
1043 for_each_cpu(j, policy->cpus) {
1044 unsigned long expires;
1046 pcpu = &per_cpu(cpuinfo, j);
1047 pcpu->policy = policy;
1048 pcpu->target_freq = policy->cur;
1049 pcpu->freq_table = freq_table;
1050 pcpu->floor_freq = pcpu->target_freq;
1051 pcpu->floor_validate_time =
1052 ktime_to_us(ktime_get());
1053 pcpu->hispeed_validate_time =
1054 pcpu->floor_validate_time;
1055 down_write(&pcpu->enable_sem);
1056 expires = jiffies + usecs_to_jiffies(timer_rate);
1057 pcpu->cpu_timer.expires = expires;
1058 add_timer_on(&pcpu->cpu_timer, j);
1059 if (timer_slack_val >= 0) {
1060 expires += usecs_to_jiffies(timer_slack_val);
1061 pcpu->cpu_slack_timer.expires = expires;
1062 add_timer_on(&pcpu->cpu_slack_timer, j);
1064 pcpu->governor_enabled = 1;
1065 up_write(&pcpu->enable_sem);
1069 * Do not register the idle hook and create sysfs
1070 * entries if we have already done so.
1072 if (++active_count > 1) {
1073 mutex_unlock(&gov_lock);
1077 rc = sysfs_create_group(cpufreq_global_kobject,
1078 &interactive_attr_group);
1080 mutex_unlock(&gov_lock);
1084 idle_notifier_register(&cpufreq_interactive_idle_nb);
1085 cpufreq_register_notifier(
1086 &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
1087 mutex_unlock(&gov_lock);
1090 case CPUFREQ_GOV_STOP:
1091 mutex_lock(&gov_lock);
1092 for_each_cpu(j, policy->cpus) {
1093 pcpu = &per_cpu(cpuinfo, j);
1094 down_write(&pcpu->enable_sem);
1095 pcpu->governor_enabled = 0;
1096 del_timer_sync(&pcpu->cpu_timer);
1097 del_timer_sync(&pcpu->cpu_slack_timer);
1098 up_write(&pcpu->enable_sem);
1101 if (--active_count > 0) {
1102 mutex_unlock(&gov_lock);
1106 cpufreq_unregister_notifier(
1107 &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
1108 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1109 sysfs_remove_group(cpufreq_global_kobject,
1110 &interactive_attr_group);
1111 mutex_unlock(&gov_lock);
1115 case CPUFREQ_GOV_LIMITS:
1116 if (policy->max < policy->cur)
1117 __cpufreq_driver_target(policy,
1118 policy->max, CPUFREQ_RELATION_H);
1119 else if (policy->min > policy->cur)
1120 __cpufreq_driver_target(policy,
1121 policy->min, CPUFREQ_RELATION_L);
1127 static void cpufreq_interactive_nop_timer(unsigned long data)
1131 static int __init cpufreq_interactive_init(void)
1134 struct cpufreq_interactive_cpuinfo *pcpu;
1135 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1137 /* Initalize per-cpu timers */
1138 for_each_possible_cpu(i) {
1139 pcpu = &per_cpu(cpuinfo, i);
1140 init_timer_deferrable(&pcpu->cpu_timer);
1141 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1142 pcpu->cpu_timer.data = i;
1143 init_timer(&pcpu->cpu_slack_timer);
1144 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1145 spin_lock_init(&pcpu->load_lock);
1146 init_rwsem(&pcpu->enable_sem);
1149 spin_lock_init(&target_loads_lock);
1150 spin_lock_init(&speedchange_cpumask_lock);
1151 mutex_init(&gov_lock);
1153 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1155 if (IS_ERR(speedchange_task))
1156 return PTR_ERR(speedchange_task);
1158 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1159 get_task_struct(speedchange_task);
1161 /* NB: wake up so the thread does not look hung to the freezer */
1162 wake_up_process(speedchange_task);
1164 return cpufreq_register_governor(&cpufreq_gov_interactive);
1167 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1168 fs_initcall(cpufreq_interactive_init);
1170 module_init(cpufreq_interactive_init);
1173 static void __exit cpufreq_interactive_exit(void)
1175 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1176 kthread_stop(speedchange_task);
1177 put_task_struct(speedchange_task);
1180 module_exit(cpufreq_interactive_exit);
1182 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1183 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1184 "Latency sensitive workloads");
1185 MODULE_LICENSE("GPL");