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;
175 spin_lock_irqsave(&pcpu->load_lock, flags);
177 get_cpu_idle_time(smp_processor_id(),
178 &pcpu->time_in_idle_timestamp);
179 pcpu->cputime_speedadj = 0;
180 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
181 expires = jiffies + usecs_to_jiffies(timer_rate);
182 mod_timer_pinned(&pcpu->cpu_timer, expires);
184 if (timer_slack_val >= 0 && pcpu->target_freq > pcpu->policy->min) {
185 expires += usecs_to_jiffies(timer_slack_val);
186 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
189 spin_unlock_irqrestore(&pcpu->load_lock, flags);
192 static unsigned int freq_to_above_hispeed_delay(unsigned int freq)
198 spin_lock_irqsave(&above_hispeed_delay_lock, flags);
200 for (i = 0; i < nabove_hispeed_delay - 1 &&
201 freq >= above_hispeed_delay[i+1]; i += 2)
204 ret = above_hispeed_delay[i];
205 spin_unlock_irqrestore(&above_hispeed_delay_lock, flags);
209 static unsigned int freq_to_targetload(unsigned int freq)
215 spin_lock_irqsave(&target_loads_lock, flags);
217 for (i = 0; i < ntarget_loads - 1 && freq >= target_loads[i+1]; i += 2)
220 ret = target_loads[i];
221 spin_unlock_irqrestore(&target_loads_lock, flags);
226 * If increasing frequencies never map to a lower target load then
227 * choose_freq() will find the minimum frequency that does not exceed its
228 * target load given the current load.
231 static unsigned int choose_freq(
232 struct cpufreq_interactive_cpuinfo *pcpu, unsigned int loadadjfreq)
234 unsigned int freq = pcpu->policy->cur;
235 unsigned int prevfreq, freqmin, freqmax;
244 tl = freq_to_targetload(freq);
247 * Find the lowest frequency where the computed load is less
248 * than or equal to the target load.
251 if (cpufreq_frequency_table_target(
252 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
253 CPUFREQ_RELATION_L, &index))
255 freq = pcpu->freq_table[index].frequency;
257 if (freq > prevfreq) {
258 /* The previous frequency is too low. */
261 if (freq >= freqmax) {
263 * Find the highest frequency that is less
266 if (cpufreq_frequency_table_target(
267 pcpu->policy, pcpu->freq_table,
268 freqmax - 1, CPUFREQ_RELATION_H,
271 freq = pcpu->freq_table[index].frequency;
273 if (freq == freqmin) {
275 * The first frequency below freqmax
276 * has already been found to be too
277 * low. freqmax is the lowest speed
278 * we found that is fast enough.
284 } else if (freq < prevfreq) {
285 /* The previous frequency is high enough. */
288 if (freq <= freqmin) {
290 * Find the lowest frequency that is higher
293 if (cpufreq_frequency_table_target(
294 pcpu->policy, pcpu->freq_table,
295 freqmin + 1, CPUFREQ_RELATION_L,
298 freq = pcpu->freq_table[index].frequency;
301 * If freqmax is the first frequency above
302 * freqmin then we have already found that
303 * this speed is fast enough.
310 /* If same frequency chosen as previous then done. */
311 } while (freq != prevfreq);
316 static u64 update_load(int cpu)
318 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
321 unsigned int delta_idle;
322 unsigned int delta_time;
325 now_idle = get_cpu_idle_time(cpu, &now);
326 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
327 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
329 if (delta_time <= delta_idle)
332 active_time = delta_time - delta_idle;
334 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
336 pcpu->time_in_idle = now_idle;
337 pcpu->time_in_idle_timestamp = now;
341 static void cpufreq_interactive_timer(unsigned long data)
344 unsigned int delta_time;
345 u64 cputime_speedadj;
347 struct cpufreq_interactive_cpuinfo *pcpu =
348 &per_cpu(cpuinfo, data);
349 unsigned int new_freq;
350 unsigned int loadadjfreq;
355 if (!down_read_trylock(&pcpu->enable_sem))
357 if (!pcpu->governor_enabled)
360 spin_lock_irqsave(&pcpu->load_lock, flags);
361 now = update_load(data);
362 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
363 cputime_speedadj = pcpu->cputime_speedadj;
364 spin_unlock_irqrestore(&pcpu->load_lock, flags);
366 if (WARN_ON_ONCE(!delta_time))
369 do_div(cputime_speedadj, delta_time);
370 loadadjfreq = (unsigned int)cputime_speedadj * 100;
371 cpu_load = loadadjfreq / pcpu->target_freq;
372 boosted = boost_val || now < boostpulse_endtime;
374 if (cpu_load >= go_hispeed_load || boosted) {
375 if (pcpu->target_freq < hispeed_freq) {
376 new_freq = hispeed_freq;
378 new_freq = choose_freq(pcpu, loadadjfreq);
380 if (new_freq < hispeed_freq)
381 new_freq = hispeed_freq;
384 new_freq = choose_freq(pcpu, loadadjfreq);
387 if (pcpu->target_freq >= hispeed_freq &&
388 new_freq > pcpu->target_freq &&
389 now - pcpu->hispeed_validate_time <
390 freq_to_above_hispeed_delay(pcpu->target_freq)) {
391 trace_cpufreq_interactive_notyet(
392 data, cpu_load, pcpu->target_freq,
393 pcpu->policy->cur, new_freq);
397 pcpu->hispeed_validate_time = now;
399 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
400 new_freq, CPUFREQ_RELATION_L,
404 new_freq = pcpu->freq_table[index].frequency;
407 * Do not scale below floor_freq unless we have been at or above the
408 * floor frequency for the minimum sample time since last validated.
410 if (new_freq < pcpu->floor_freq) {
411 if (now - pcpu->floor_validate_time < min_sample_time) {
412 trace_cpufreq_interactive_notyet(
413 data, cpu_load, pcpu->target_freq,
414 pcpu->policy->cur, new_freq);
420 * Update the timestamp for checking whether speed has been held at
421 * or above the selected frequency for a minimum of min_sample_time,
422 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
423 * allow the speed to drop as soon as the boostpulse duration expires
424 * (or the indefinite boost is turned off).
427 if (!boosted || new_freq > hispeed_freq) {
428 pcpu->floor_freq = new_freq;
429 pcpu->floor_validate_time = now;
432 if (pcpu->target_freq == new_freq) {
433 trace_cpufreq_interactive_already(
434 data, cpu_load, pcpu->target_freq,
435 pcpu->policy->cur, new_freq);
436 goto rearm_if_notmax;
439 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
440 pcpu->policy->cur, new_freq);
442 pcpu->target_freq = new_freq;
443 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
444 cpumask_set_cpu(data, &speedchange_cpumask);
445 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
446 wake_up_process(speedchange_task);
450 * Already set max speed and don't see a need to change that,
451 * wait until next idle to re-evaluate, don't need timer.
453 if (pcpu->target_freq == pcpu->policy->max)
457 if (!timer_pending(&pcpu->cpu_timer))
458 cpufreq_interactive_timer_resched(pcpu);
461 up_read(&pcpu->enable_sem);
465 static void cpufreq_interactive_idle_start(void)
467 struct cpufreq_interactive_cpuinfo *pcpu =
468 &per_cpu(cpuinfo, smp_processor_id());
471 if (!down_read_trylock(&pcpu->enable_sem))
473 if (!pcpu->governor_enabled) {
474 up_read(&pcpu->enable_sem);
478 pending = timer_pending(&pcpu->cpu_timer);
480 if (pcpu->target_freq != pcpu->policy->min) {
482 * Entering idle while not at lowest speed. On some
483 * platforms this can hold the other CPU(s) at that speed
484 * even though the CPU is idle. Set a timer to re-evaluate
485 * speed so this idle CPU doesn't hold the other CPUs above
486 * min indefinitely. This should probably be a quirk of
487 * the CPUFreq driver.
490 cpufreq_interactive_timer_resched(pcpu);
493 up_read(&pcpu->enable_sem);
496 static void cpufreq_interactive_idle_end(void)
498 struct cpufreq_interactive_cpuinfo *pcpu =
499 &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 /* Arm the timer for 1-2 ticks later if not already. */
509 if (!timer_pending(&pcpu->cpu_timer)) {
510 cpufreq_interactive_timer_resched(pcpu);
511 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
512 del_timer(&pcpu->cpu_timer);
513 del_timer(&pcpu->cpu_slack_timer);
514 cpufreq_interactive_timer(smp_processor_id());
517 up_read(&pcpu->enable_sem);
520 static int cpufreq_interactive_speedchange_task(void *data)
525 struct cpufreq_interactive_cpuinfo *pcpu;
528 set_current_state(TASK_INTERRUPTIBLE);
529 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
531 if (cpumask_empty(&speedchange_cpumask)) {
532 spin_unlock_irqrestore(&speedchange_cpumask_lock,
536 if (kthread_should_stop())
539 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
542 set_current_state(TASK_RUNNING);
543 tmp_mask = speedchange_cpumask;
544 cpumask_clear(&speedchange_cpumask);
545 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
547 for_each_cpu(cpu, &tmp_mask) {
549 unsigned int max_freq = 0;
551 pcpu = &per_cpu(cpuinfo, cpu);
552 if (!down_read_trylock(&pcpu->enable_sem))
554 if (!pcpu->governor_enabled) {
555 up_read(&pcpu->enable_sem);
559 for_each_cpu(j, pcpu->policy->cpus) {
560 struct cpufreq_interactive_cpuinfo *pjcpu =
561 &per_cpu(cpuinfo, j);
563 if (pjcpu->target_freq > max_freq)
564 max_freq = pjcpu->target_freq;
567 if (max_freq != pcpu->policy->cur)
568 __cpufreq_driver_target(pcpu->policy,
571 trace_cpufreq_interactive_setspeed(cpu,
575 up_read(&pcpu->enable_sem);
582 static void cpufreq_interactive_boost(void)
587 struct cpufreq_interactive_cpuinfo *pcpu;
589 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
591 for_each_online_cpu(i) {
592 pcpu = &per_cpu(cpuinfo, i);
594 if (pcpu->target_freq < hispeed_freq) {
595 pcpu->target_freq = hispeed_freq;
596 cpumask_set_cpu(i, &speedchange_cpumask);
597 pcpu->hispeed_validate_time =
598 ktime_to_us(ktime_get());
603 * Set floor freq and (re)start timer for when last
607 pcpu->floor_freq = hispeed_freq;
608 pcpu->floor_validate_time = ktime_to_us(ktime_get());
611 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
614 wake_up_process(speedchange_task);
617 static int cpufreq_interactive_notifier(
618 struct notifier_block *nb, unsigned long val, void *data)
620 struct cpufreq_freqs *freq = data;
621 struct cpufreq_interactive_cpuinfo *pcpu;
625 if (val == CPUFREQ_POSTCHANGE) {
626 pcpu = &per_cpu(cpuinfo, freq->cpu);
627 if (!down_read_trylock(&pcpu->enable_sem))
629 if (!pcpu->governor_enabled) {
630 up_read(&pcpu->enable_sem);
634 for_each_cpu(cpu, pcpu->policy->cpus) {
635 struct cpufreq_interactive_cpuinfo *pjcpu =
636 &per_cpu(cpuinfo, cpu);
637 spin_lock_irqsave(&pjcpu->load_lock, flags);
639 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
642 up_read(&pcpu->enable_sem);
647 static struct notifier_block cpufreq_notifier_block = {
648 .notifier_call = cpufreq_interactive_notifier,
651 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
656 unsigned int *tokenized_data;
660 while ((cp = strpbrk(cp + 1, " :")))
663 if (!(ntokens & 0x1))
666 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
667 if (!tokenized_data) {
674 while (i < ntokens) {
675 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
678 cp = strpbrk(cp, " :");
687 *num_tokens = ntokens;
688 return tokenized_data;
691 kfree(tokenized_data);
696 static ssize_t show_target_loads(
697 struct kobject *kobj, struct attribute *attr, char *buf)
703 spin_lock_irqsave(&target_loads_lock, flags);
705 for (i = 0; i < ntarget_loads; i++)
706 ret += sprintf(buf + ret, "%u%s", target_loads[i],
707 i & 0x1 ? ":" : " ");
709 ret += sprintf(buf + ret, "\n");
710 spin_unlock_irqrestore(&target_loads_lock, flags);
714 static ssize_t store_target_loads(
715 struct kobject *kobj, struct attribute *attr, const char *buf,
719 unsigned int *new_target_loads = NULL;
722 new_target_loads = get_tokenized_data(buf, &ntokens);
723 if (IS_ERR(new_target_loads))
724 return PTR_RET(new_target_loads);
726 spin_lock_irqsave(&target_loads_lock, flags);
727 if (target_loads != default_target_loads)
729 target_loads = new_target_loads;
730 ntarget_loads = ntokens;
731 spin_unlock_irqrestore(&target_loads_lock, flags);
735 static struct global_attr target_loads_attr =
736 __ATTR(target_loads, S_IRUGO | S_IWUSR,
737 show_target_loads, store_target_loads);
739 static ssize_t show_above_hispeed_delay(
740 struct kobject *kobj, struct attribute *attr, char *buf)
746 spin_lock_irqsave(&above_hispeed_delay_lock, flags);
748 for (i = 0; i < nabove_hispeed_delay; i++)
749 ret += sprintf(buf + ret, "%u%s", above_hispeed_delay[i],
750 i & 0x1 ? ":" : " ");
752 ret += sprintf(buf + ret, "\n");
753 spin_unlock_irqrestore(&above_hispeed_delay_lock, flags);
757 static ssize_t store_above_hispeed_delay(
758 struct kobject *kobj, struct attribute *attr, const char *buf,
762 unsigned int *new_above_hispeed_delay = NULL;
765 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
766 if (IS_ERR(new_above_hispeed_delay))
767 return PTR_RET(new_above_hispeed_delay);
769 spin_lock_irqsave(&above_hispeed_delay_lock, flags);
770 if (above_hispeed_delay != default_above_hispeed_delay)
771 kfree(above_hispeed_delay);
772 above_hispeed_delay = new_above_hispeed_delay;
773 nabove_hispeed_delay = ntokens;
774 spin_unlock_irqrestore(&above_hispeed_delay_lock, flags);
779 static struct global_attr above_hispeed_delay_attr =
780 __ATTR(above_hispeed_delay, S_IRUGO | S_IWUSR,
781 show_above_hispeed_delay, store_above_hispeed_delay);
783 static ssize_t show_hispeed_freq(struct kobject *kobj,
784 struct attribute *attr, char *buf)
786 return sprintf(buf, "%u\n", hispeed_freq);
789 static ssize_t store_hispeed_freq(struct kobject *kobj,
790 struct attribute *attr, const char *buf,
794 long unsigned int val;
796 ret = strict_strtoul(buf, 0, &val);
803 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
804 show_hispeed_freq, store_hispeed_freq);
807 static ssize_t show_go_hispeed_load(struct kobject *kobj,
808 struct attribute *attr, char *buf)
810 return sprintf(buf, "%lu\n", go_hispeed_load);
813 static ssize_t store_go_hispeed_load(struct kobject *kobj,
814 struct attribute *attr, const char *buf, size_t count)
819 ret = strict_strtoul(buf, 0, &val);
822 go_hispeed_load = val;
826 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
827 show_go_hispeed_load, store_go_hispeed_load);
829 static ssize_t show_min_sample_time(struct kobject *kobj,
830 struct attribute *attr, char *buf)
832 return sprintf(buf, "%lu\n", min_sample_time);
835 static ssize_t store_min_sample_time(struct kobject *kobj,
836 struct attribute *attr, const char *buf, size_t count)
841 ret = strict_strtoul(buf, 0, &val);
844 min_sample_time = val;
848 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
849 show_min_sample_time, store_min_sample_time);
851 static ssize_t show_timer_rate(struct kobject *kobj,
852 struct attribute *attr, char *buf)
854 return sprintf(buf, "%lu\n", timer_rate);
857 static ssize_t store_timer_rate(struct kobject *kobj,
858 struct attribute *attr, const char *buf, size_t count)
863 ret = strict_strtoul(buf, 0, &val);
870 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
871 show_timer_rate, store_timer_rate);
873 static ssize_t show_timer_slack(
874 struct kobject *kobj, struct attribute *attr, char *buf)
876 return sprintf(buf, "%d\n", timer_slack_val);
879 static ssize_t store_timer_slack(
880 struct kobject *kobj, struct attribute *attr, const char *buf,
886 ret = kstrtol(buf, 10, &val);
890 timer_slack_val = val;
894 define_one_global_rw(timer_slack);
896 static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
899 return sprintf(buf, "%d\n", boost_val);
902 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
903 const char *buf, size_t count)
908 ret = kstrtoul(buf, 0, &val);
915 trace_cpufreq_interactive_boost("on");
916 cpufreq_interactive_boost();
918 trace_cpufreq_interactive_unboost("off");
924 define_one_global_rw(boost);
926 static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
927 const char *buf, size_t count)
932 ret = kstrtoul(buf, 0, &val);
936 boostpulse_endtime = ktime_to_us(ktime_get()) + boostpulse_duration_val;
937 trace_cpufreq_interactive_boost("pulse");
938 cpufreq_interactive_boost();
942 static struct global_attr boostpulse =
943 __ATTR(boostpulse, 0200, NULL, store_boostpulse);
945 static ssize_t show_boostpulse_duration(
946 struct kobject *kobj, struct attribute *attr, char *buf)
948 return sprintf(buf, "%d\n", boostpulse_duration_val);
951 static ssize_t store_boostpulse_duration(
952 struct kobject *kobj, struct attribute *attr, const char *buf,
958 ret = kstrtoul(buf, 0, &val);
962 boostpulse_duration_val = val;
966 define_one_global_rw(boostpulse_duration);
968 static ssize_t show_io_is_busy(struct kobject *kobj,
969 struct attribute *attr, char *buf)
971 return sprintf(buf, "%u\n", io_is_busy);
974 static ssize_t store_io_is_busy(struct kobject *kobj,
975 struct attribute *attr, const char *buf, size_t count)
980 ret = kstrtoul(buf, 0, &val);
987 static struct global_attr io_is_busy_attr = __ATTR(io_is_busy, 0644,
988 show_io_is_busy, store_io_is_busy);
990 static struct attribute *interactive_attributes[] = {
991 &target_loads_attr.attr,
992 &above_hispeed_delay_attr.attr,
993 &hispeed_freq_attr.attr,
994 &go_hispeed_load_attr.attr,
995 &min_sample_time_attr.attr,
996 &timer_rate_attr.attr,
1000 &boostpulse_duration.attr,
1001 &io_is_busy_attr.attr,
1005 static struct attribute_group interactive_attr_group = {
1006 .attrs = interactive_attributes,
1007 .name = "interactive",
1010 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1016 cpufreq_interactive_idle_start();
1019 cpufreq_interactive_idle_end();
1026 static struct notifier_block cpufreq_interactive_idle_nb = {
1027 .notifier_call = cpufreq_interactive_idle_notifier,
1030 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1035 struct cpufreq_interactive_cpuinfo *pcpu;
1036 struct cpufreq_frequency_table *freq_table;
1039 case CPUFREQ_GOV_START:
1040 if (!cpu_online(policy->cpu))
1043 mutex_lock(&gov_lock);
1046 cpufreq_frequency_get_table(policy->cpu);
1048 hispeed_freq = policy->max;
1050 for_each_cpu(j, policy->cpus) {
1051 unsigned long expires;
1053 pcpu = &per_cpu(cpuinfo, j);
1054 pcpu->policy = policy;
1055 pcpu->target_freq = policy->cur;
1056 pcpu->freq_table = freq_table;
1057 pcpu->floor_freq = pcpu->target_freq;
1058 pcpu->floor_validate_time =
1059 ktime_to_us(ktime_get());
1060 pcpu->hispeed_validate_time =
1061 pcpu->floor_validate_time;
1062 down_write(&pcpu->enable_sem);
1063 expires = jiffies + usecs_to_jiffies(timer_rate);
1064 pcpu->cpu_timer.expires = expires;
1065 add_timer_on(&pcpu->cpu_timer, j);
1066 if (timer_slack_val >= 0) {
1067 expires += usecs_to_jiffies(timer_slack_val);
1068 pcpu->cpu_slack_timer.expires = expires;
1069 add_timer_on(&pcpu->cpu_slack_timer, j);
1071 pcpu->governor_enabled = 1;
1072 up_write(&pcpu->enable_sem);
1076 * Do not register the idle hook and create sysfs
1077 * entries if we have already done so.
1079 if (++active_count > 1) {
1080 mutex_unlock(&gov_lock);
1084 rc = sysfs_create_group(cpufreq_global_kobject,
1085 &interactive_attr_group);
1087 mutex_unlock(&gov_lock);
1091 idle_notifier_register(&cpufreq_interactive_idle_nb);
1092 cpufreq_register_notifier(
1093 &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
1094 mutex_unlock(&gov_lock);
1097 case CPUFREQ_GOV_STOP:
1098 mutex_lock(&gov_lock);
1099 for_each_cpu(j, policy->cpus) {
1100 pcpu = &per_cpu(cpuinfo, j);
1101 down_write(&pcpu->enable_sem);
1102 pcpu->governor_enabled = 0;
1103 del_timer_sync(&pcpu->cpu_timer);
1104 del_timer_sync(&pcpu->cpu_slack_timer);
1105 up_write(&pcpu->enable_sem);
1108 if (--active_count > 0) {
1109 mutex_unlock(&gov_lock);
1113 cpufreq_unregister_notifier(
1114 &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
1115 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1116 sysfs_remove_group(cpufreq_global_kobject,
1117 &interactive_attr_group);
1118 mutex_unlock(&gov_lock);
1122 case CPUFREQ_GOV_LIMITS:
1123 if (policy->max < policy->cur)
1124 __cpufreq_driver_target(policy,
1125 policy->max, CPUFREQ_RELATION_H);
1126 else if (policy->min > policy->cur)
1127 __cpufreq_driver_target(policy,
1128 policy->min, CPUFREQ_RELATION_L);
1134 static void cpufreq_interactive_nop_timer(unsigned long data)
1138 static int __init cpufreq_interactive_init(void)
1141 struct cpufreq_interactive_cpuinfo *pcpu;
1142 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1144 /* Initalize per-cpu timers */
1145 for_each_possible_cpu(i) {
1146 pcpu = &per_cpu(cpuinfo, i);
1147 init_timer_deferrable(&pcpu->cpu_timer);
1148 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1149 pcpu->cpu_timer.data = i;
1150 init_timer(&pcpu->cpu_slack_timer);
1151 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1152 spin_lock_init(&pcpu->load_lock);
1153 init_rwsem(&pcpu->enable_sem);
1156 spin_lock_init(&target_loads_lock);
1157 spin_lock_init(&speedchange_cpumask_lock);
1158 spin_lock_init(&above_hispeed_delay_lock);
1159 mutex_init(&gov_lock);
1161 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1163 if (IS_ERR(speedchange_task))
1164 return PTR_ERR(speedchange_task);
1166 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1167 get_task_struct(speedchange_task);
1169 /* NB: wake up so the thread does not look hung to the freezer */
1170 wake_up_process(speedchange_task);
1172 return cpufreq_register_governor(&cpufreq_gov_interactive);
1175 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1176 fs_initcall(cpufreq_interactive_init);
1178 module_init(cpufreq_interactive_init);
1181 static void __exit cpufreq_interactive_exit(void)
1183 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1184 kthread_stop(speedchange_task);
1185 put_task_struct(speedchange_task);
1188 module_exit(cpufreq_interactive_exit);
1190 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1191 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1192 "Latency sensitive workloads");
1193 MODULE_LICENSE("GPL");