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>
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/cpufreq_interactive.h>
37 struct cpufreq_interactive_cpuinfo {
38 struct timer_list cpu_timer;
39 struct timer_list cpu_slack_timer;
40 spinlock_t load_lock; /* protects the next 4 fields */
42 u64 time_in_idle_timestamp;
44 u64 cputime_speedadj_timestamp;
45 struct cpufreq_policy *policy;
46 struct cpufreq_frequency_table *freq_table;
47 spinlock_t target_freq_lock; /*protects target freq */
48 unsigned int target_freq;
49 unsigned int floor_freq;
50 unsigned int max_freq;
51 u64 floor_validate_time;
52 u64 hispeed_validate_time;
53 struct rw_semaphore enable_sem;
57 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
59 /* realtime thread handles frequency scaling */
60 static struct task_struct *speedchange_task;
61 static cpumask_t speedchange_cpumask;
62 static spinlock_t speedchange_cpumask_lock;
63 static struct mutex gov_lock;
65 /* Target load. Lower values result in higher CPU speeds. */
66 #define DEFAULT_TARGET_LOAD 90
67 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
69 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
70 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
71 static unsigned int default_above_hispeed_delay[] = {
72 DEFAULT_ABOVE_HISPEED_DELAY };
74 struct cpufreq_interactive_tunables {
76 /* Hi speed to bump to from lo speed when load burst (default max) */
77 unsigned int hispeed_freq;
78 /* Go to hi speed when CPU load at or above this value. */
79 #define DEFAULT_GO_HISPEED_LOAD 99
80 unsigned long go_hispeed_load;
81 /* Target load. Lower values result in higher CPU speeds. */
82 spinlock_t target_loads_lock;
83 unsigned int *target_loads;
86 * The minimum amount of time to spend at a frequency before we can ramp
89 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
90 unsigned long min_sample_time;
92 * The sample rate of the timer used to increase frequency
94 unsigned long timer_rate;
96 * Wait this long before raising speed above hispeed, by default a
97 * single timer interval.
99 spinlock_t above_hispeed_delay_lock;
100 unsigned int *above_hispeed_delay;
101 int nabove_hispeed_delay;
102 /* Non-zero means indefinite speed boost active */
104 /* Duration of a boot pulse in usecs */
105 int boostpulse_duration_val;
106 /* End time of boost pulse in ktime converted to usecs */
107 u64 boostpulse_endtime;
110 * Max additional time to wait in idle, beyond timer_rate, at speeds
111 * above minimum before wakeup to reduce speed, or -1 if unnecessary.
113 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
118 /* For cases where we have single governor instance for system */
119 static struct cpufreq_interactive_tunables *common_tunables;
121 static struct attribute_group *get_sysfs_attr(void);
123 static void cpufreq_interactive_timer_resched(
124 struct cpufreq_interactive_cpuinfo *pcpu)
126 struct cpufreq_interactive_tunables *tunables =
127 pcpu->policy->governor_data;
128 unsigned long expires;
131 spin_lock_irqsave(&pcpu->load_lock, flags);
133 get_cpu_idle_time(smp_processor_id(),
134 &pcpu->time_in_idle_timestamp,
135 tunables->io_is_busy);
136 pcpu->cputime_speedadj = 0;
137 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
138 expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
139 mod_timer_pinned(&pcpu->cpu_timer, expires);
141 if (tunables->timer_slack_val >= 0 &&
142 pcpu->target_freq > pcpu->policy->min) {
143 expires += usecs_to_jiffies(tunables->timer_slack_val);
144 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
147 spin_unlock_irqrestore(&pcpu->load_lock, flags);
150 /* The caller shall take enable_sem write semaphore to avoid any timer race.
151 * The cpu_timer and cpu_slack_timer must be deactivated when calling this
154 static void cpufreq_interactive_timer_start(
155 struct cpufreq_interactive_tunables *tunables, int cpu)
157 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
158 unsigned long expires = jiffies +
159 usecs_to_jiffies(tunables->timer_rate);
162 pcpu->cpu_timer.expires = expires;
163 add_timer_on(&pcpu->cpu_timer, cpu);
164 if (tunables->timer_slack_val >= 0 &&
165 pcpu->target_freq > pcpu->policy->min) {
166 expires += usecs_to_jiffies(tunables->timer_slack_val);
167 pcpu->cpu_slack_timer.expires = expires;
168 add_timer_on(&pcpu->cpu_slack_timer, cpu);
171 spin_lock_irqsave(&pcpu->load_lock, flags);
173 get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
174 tunables->io_is_busy);
175 pcpu->cputime_speedadj = 0;
176 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
177 spin_unlock_irqrestore(&pcpu->load_lock, flags);
180 static unsigned int freq_to_above_hispeed_delay(
181 struct cpufreq_interactive_tunables *tunables,
188 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
190 for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
191 freq >= tunables->above_hispeed_delay[i+1]; i += 2)
194 ret = tunables->above_hispeed_delay[i];
195 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
199 static unsigned int freq_to_targetload(
200 struct cpufreq_interactive_tunables *tunables, unsigned int freq)
206 spin_lock_irqsave(&tunables->target_loads_lock, flags);
208 for (i = 0; i < tunables->ntarget_loads - 1 &&
209 freq >= tunables->target_loads[i+1]; i += 2)
212 ret = tunables->target_loads[i];
213 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
218 * If increasing frequencies never map to a lower target load then
219 * choose_freq() will find the minimum frequency that does not exceed its
220 * target load given the current load.
222 static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
223 unsigned int loadadjfreq)
225 unsigned int freq = pcpu->policy->cur;
226 unsigned int prevfreq, freqmin, freqmax;
235 tl = freq_to_targetload(pcpu->policy->governor_data, freq);
238 * Find the lowest frequency where the computed load is less
239 * than or equal to the target load.
242 if (cpufreq_frequency_table_target(
243 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
244 CPUFREQ_RELATION_L, &index))
246 freq = pcpu->freq_table[index].frequency;
248 if (freq > prevfreq) {
249 /* The previous frequency is too low. */
252 if (freq >= freqmax) {
254 * Find the highest frequency that is less
257 if (cpufreq_frequency_table_target(
258 pcpu->policy, pcpu->freq_table,
259 freqmax - 1, CPUFREQ_RELATION_H,
262 freq = pcpu->freq_table[index].frequency;
264 if (freq == freqmin) {
266 * The first frequency below freqmax
267 * has already been found to be too
268 * low. freqmax is the lowest speed
269 * we found that is fast enough.
275 } else if (freq < prevfreq) {
276 /* The previous frequency is high enough. */
279 if (freq <= freqmin) {
281 * Find the lowest frequency that is higher
284 if (cpufreq_frequency_table_target(
285 pcpu->policy, pcpu->freq_table,
286 freqmin + 1, CPUFREQ_RELATION_L,
289 freq = pcpu->freq_table[index].frequency;
292 * If freqmax is the first frequency above
293 * freqmin then we have already found that
294 * this speed is fast enough.
301 /* If same frequency chosen as previous then done. */
302 } while (freq != prevfreq);
307 static u64 update_load(int cpu)
309 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
310 struct cpufreq_interactive_tunables *tunables =
311 pcpu->policy->governor_data;
314 unsigned int delta_idle;
315 unsigned int delta_time;
318 now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
319 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
320 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
322 if (delta_time <= delta_idle)
325 active_time = delta_time - delta_idle;
327 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
329 pcpu->time_in_idle = now_idle;
330 pcpu->time_in_idle_timestamp = now;
334 static void cpufreq_interactive_timer(unsigned long data)
337 unsigned int delta_time;
338 u64 cputime_speedadj;
340 struct cpufreq_interactive_cpuinfo *pcpu =
341 &per_cpu(cpuinfo, data);
342 struct cpufreq_interactive_tunables *tunables =
343 pcpu->policy->governor_data;
344 unsigned int new_freq;
345 unsigned int loadadjfreq;
349 if (!down_read_trylock(&pcpu->enable_sem))
351 if (!pcpu->governor_enabled)
354 spin_lock_irqsave(&pcpu->load_lock, flags);
355 now = update_load(data);
356 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
357 cputime_speedadj = pcpu->cputime_speedadj;
358 spin_unlock_irqrestore(&pcpu->load_lock, flags);
360 if (WARN_ON_ONCE(!delta_time))
363 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
364 do_div(cputime_speedadj, delta_time);
365 loadadjfreq = (unsigned int)cputime_speedadj * 100;
366 cpu_load = loadadjfreq / pcpu->target_freq;
367 tunables->boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
369 if (cpu_load >= tunables->go_hispeed_load || tunables->boosted) {
370 if (pcpu->target_freq < tunables->hispeed_freq) {
371 new_freq = tunables->hispeed_freq;
373 new_freq = choose_freq(pcpu, loadadjfreq);
375 if (new_freq < tunables->hispeed_freq)
376 new_freq = tunables->hispeed_freq;
379 new_freq = choose_freq(pcpu, loadadjfreq);
380 if (new_freq > tunables->hispeed_freq &&
381 pcpu->target_freq < tunables->hispeed_freq)
382 new_freq = tunables->hispeed_freq;
385 if (pcpu->target_freq >= tunables->hispeed_freq &&
386 new_freq > pcpu->target_freq &&
387 now - pcpu->hispeed_validate_time <
388 freq_to_above_hispeed_delay(tunables, pcpu->target_freq)) {
389 trace_cpufreq_interactive_notyet(
390 data, cpu_load, pcpu->target_freq,
391 pcpu->policy->cur, new_freq);
392 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
396 pcpu->hispeed_validate_time = now;
398 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
399 new_freq, CPUFREQ_RELATION_L,
401 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
405 new_freq = pcpu->freq_table[index].frequency;
408 * Do not scale below floor_freq unless we have been at or above the
409 * floor frequency for the minimum sample time since last validated.
411 if (new_freq < pcpu->floor_freq) {
412 if (now - pcpu->floor_validate_time <
413 tunables->min_sample_time) {
414 trace_cpufreq_interactive_notyet(
415 data, cpu_load, pcpu->target_freq,
416 pcpu->policy->cur, new_freq);
417 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
423 * Update the timestamp for checking whether speed has been held at
424 * or above the selected frequency for a minimum of min_sample_time,
425 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
426 * allow the speed to drop as soon as the boostpulse duration expires
427 * (or the indefinite boost is turned off).
430 if (!tunables->boosted || new_freq > tunables->hispeed_freq) {
431 pcpu->floor_freq = new_freq;
432 pcpu->floor_validate_time = now;
435 if (pcpu->target_freq == new_freq &&
436 pcpu->target_freq <= pcpu->policy->cur) {
437 trace_cpufreq_interactive_already(
438 data, cpu_load, pcpu->target_freq,
439 pcpu->policy->cur, new_freq);
440 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
441 goto rearm_if_notmax;
444 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
445 pcpu->policy->cur, new_freq);
447 pcpu->target_freq = new_freq;
448 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
449 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
450 cpumask_set_cpu(data, &speedchange_cpumask);
451 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
452 wake_up_process(speedchange_task);
456 * Already set max speed and don't see a need to change that,
457 * wait until next idle to re-evaluate, don't need timer.
459 if (pcpu->target_freq == pcpu->policy->max)
463 if (!timer_pending(&pcpu->cpu_timer))
464 cpufreq_interactive_timer_resched(pcpu);
467 up_read(&pcpu->enable_sem);
471 static void cpufreq_interactive_idle_start(void)
473 struct cpufreq_interactive_cpuinfo *pcpu =
474 &per_cpu(cpuinfo, smp_processor_id());
477 if (!down_read_trylock(&pcpu->enable_sem))
479 if (!pcpu->governor_enabled) {
480 up_read(&pcpu->enable_sem);
484 pending = timer_pending(&pcpu->cpu_timer);
486 if (pcpu->target_freq != pcpu->policy->min) {
488 * Entering idle while not at lowest speed. On some
489 * platforms this can hold the other CPU(s) at that speed
490 * even though the CPU is idle. Set a timer to re-evaluate
491 * speed so this idle CPU doesn't hold the other CPUs above
492 * min indefinitely. This should probably be a quirk of
493 * the CPUFreq driver.
496 cpufreq_interactive_timer_resched(pcpu);
499 up_read(&pcpu->enable_sem);
502 static void cpufreq_interactive_idle_end(void)
504 struct cpufreq_interactive_cpuinfo *pcpu =
505 &per_cpu(cpuinfo, smp_processor_id());
507 if (!down_read_trylock(&pcpu->enable_sem))
509 if (!pcpu->governor_enabled) {
510 up_read(&pcpu->enable_sem);
514 /* Arm the timer for 1-2 ticks later if not already. */
515 if (!timer_pending(&pcpu->cpu_timer)) {
516 cpufreq_interactive_timer_resched(pcpu);
517 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
518 del_timer(&pcpu->cpu_timer);
519 del_timer(&pcpu->cpu_slack_timer);
520 cpufreq_interactive_timer(smp_processor_id());
523 up_read(&pcpu->enable_sem);
526 static int cpufreq_interactive_speedchange_task(void *data)
531 struct cpufreq_interactive_cpuinfo *pcpu;
534 set_current_state(TASK_INTERRUPTIBLE);
535 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
537 if (cpumask_empty(&speedchange_cpumask)) {
538 spin_unlock_irqrestore(&speedchange_cpumask_lock,
542 if (kthread_should_stop())
545 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
548 set_current_state(TASK_RUNNING);
549 tmp_mask = speedchange_cpumask;
550 cpumask_clear(&speedchange_cpumask);
551 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
553 for_each_cpu(cpu, &tmp_mask) {
555 unsigned int max_freq = 0;
557 pcpu = &per_cpu(cpuinfo, cpu);
558 if (!down_read_trylock(&pcpu->enable_sem))
560 if (!pcpu->governor_enabled) {
561 up_read(&pcpu->enable_sem);
565 for_each_cpu(j, pcpu->policy->cpus) {
566 struct cpufreq_interactive_cpuinfo *pjcpu =
567 &per_cpu(cpuinfo, j);
569 if (pjcpu->target_freq > max_freq)
570 max_freq = pjcpu->target_freq;
573 if (max_freq != pcpu->policy->cur)
574 __cpufreq_driver_target(pcpu->policy,
577 trace_cpufreq_interactive_setspeed(cpu,
581 up_read(&pcpu->enable_sem);
588 static void cpufreq_interactive_boost(struct cpufreq_interactive_tunables *tunables)
592 unsigned long flags[2];
593 struct cpufreq_interactive_cpuinfo *pcpu;
595 tunables->boosted = true;
597 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
599 for_each_online_cpu(i) {
600 pcpu = &per_cpu(cpuinfo, i);
601 if (tunables != pcpu->policy->governor_data)
604 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
605 if (pcpu->target_freq < tunables->hispeed_freq) {
606 pcpu->target_freq = tunables->hispeed_freq;
607 cpumask_set_cpu(i, &speedchange_cpumask);
608 pcpu->hispeed_validate_time =
609 ktime_to_us(ktime_get());
612 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
615 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
618 wake_up_process(speedchange_task);
621 static int cpufreq_interactive_notifier(
622 struct notifier_block *nb, unsigned long val, void *data)
624 struct cpufreq_freqs *freq = data;
625 struct cpufreq_interactive_cpuinfo *pcpu;
629 if (val == CPUFREQ_POSTCHANGE) {
630 pcpu = &per_cpu(cpuinfo, freq->cpu);
631 if (!down_read_trylock(&pcpu->enable_sem))
633 if (!pcpu->governor_enabled) {
634 up_read(&pcpu->enable_sem);
638 for_each_cpu(cpu, pcpu->policy->cpus) {
639 struct cpufreq_interactive_cpuinfo *pjcpu =
640 &per_cpu(cpuinfo, cpu);
641 if (cpu != freq->cpu) {
642 if (!down_read_trylock(&pjcpu->enable_sem))
644 if (!pjcpu->governor_enabled) {
645 up_read(&pjcpu->enable_sem);
649 spin_lock_irqsave(&pjcpu->load_lock, flags);
651 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
652 if (cpu != freq->cpu)
653 up_read(&pjcpu->enable_sem);
656 up_read(&pcpu->enable_sem);
661 static struct notifier_block cpufreq_notifier_block = {
662 .notifier_call = cpufreq_interactive_notifier,
665 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
670 unsigned int *tokenized_data;
674 while ((cp = strpbrk(cp + 1, " :")))
677 if (!(ntokens & 0x1))
680 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
681 if (!tokenized_data) {
688 while (i < ntokens) {
689 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
692 cp = strpbrk(cp, " :");
701 *num_tokens = ntokens;
702 return tokenized_data;
705 kfree(tokenized_data);
710 static ssize_t show_target_loads(
711 struct cpufreq_interactive_tunables *tunables,
718 spin_lock_irqsave(&tunables->target_loads_lock, flags);
720 for (i = 0; i < tunables->ntarget_loads; i++)
721 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
722 i & 0x1 ? ":" : " ");
724 sprintf(buf + ret - 1, "\n");
725 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
729 static ssize_t store_target_loads(
730 struct cpufreq_interactive_tunables *tunables,
731 const char *buf, size_t count)
734 unsigned int *new_target_loads = NULL;
737 new_target_loads = get_tokenized_data(buf, &ntokens);
738 if (IS_ERR(new_target_loads))
739 return PTR_RET(new_target_loads);
741 spin_lock_irqsave(&tunables->target_loads_lock, flags);
742 if (tunables->target_loads != default_target_loads)
743 kfree(tunables->target_loads);
744 tunables->target_loads = new_target_loads;
745 tunables->ntarget_loads = ntokens;
746 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
750 static ssize_t show_above_hispeed_delay(
751 struct cpufreq_interactive_tunables *tunables, char *buf)
757 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
759 for (i = 0; i < tunables->nabove_hispeed_delay; i++)
760 ret += sprintf(buf + ret, "%u%s",
761 tunables->above_hispeed_delay[i],
762 i & 0x1 ? ":" : " ");
764 sprintf(buf + ret - 1, "\n");
765 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
769 static ssize_t store_above_hispeed_delay(
770 struct cpufreq_interactive_tunables *tunables,
771 const char *buf, size_t count)
774 unsigned int *new_above_hispeed_delay = NULL;
777 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
778 if (IS_ERR(new_above_hispeed_delay))
779 return PTR_RET(new_above_hispeed_delay);
781 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
782 if (tunables->above_hispeed_delay != default_above_hispeed_delay)
783 kfree(tunables->above_hispeed_delay);
784 tunables->above_hispeed_delay = new_above_hispeed_delay;
785 tunables->nabove_hispeed_delay = ntokens;
786 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
791 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
794 return sprintf(buf, "%u\n", tunables->hispeed_freq);
797 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
798 const char *buf, size_t count)
801 long unsigned int val;
803 ret = strict_strtoul(buf, 0, &val);
806 tunables->hispeed_freq = val;
810 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
811 *tunables, char *buf)
813 return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
816 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
817 *tunables, const char *buf, size_t count)
822 ret = strict_strtoul(buf, 0, &val);
825 tunables->go_hispeed_load = val;
829 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
830 *tunables, char *buf)
832 return sprintf(buf, "%lu\n", tunables->min_sample_time);
835 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
836 *tunables, const char *buf, size_t count)
841 ret = strict_strtoul(buf, 0, &val);
844 tunables->min_sample_time = val;
848 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
851 return sprintf(buf, "%lu\n", tunables->timer_rate);
854 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
855 const char *buf, size_t count)
860 ret = strict_strtoul(buf, 0, &val);
863 tunables->timer_rate = val;
867 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
870 return sprintf(buf, "%d\n", tunables->timer_slack_val);
873 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
874 const char *buf, size_t count)
879 ret = kstrtol(buf, 10, &val);
883 tunables->timer_slack_val = val;
887 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
890 return sprintf(buf, "%d\n", tunables->boost_val);
893 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
894 const char *buf, size_t count)
899 ret = kstrtoul(buf, 0, &val);
903 tunables->boost_val = val;
905 if (tunables->boost_val) {
906 trace_cpufreq_interactive_boost("on");
907 if (!tunables->boosted)
908 cpufreq_interactive_boost(tunables);
910 tunables->boostpulse_endtime = ktime_to_us(ktime_get());
911 trace_cpufreq_interactive_unboost("off");
917 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
918 const char *buf, size_t count)
923 ret = kstrtoul(buf, 0, &val);
927 tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
928 tunables->boostpulse_duration_val;
929 trace_cpufreq_interactive_boost("pulse");
930 if (!tunables->boosted)
931 cpufreq_interactive_boost(tunables);
935 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
936 *tunables, char *buf)
938 return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
941 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
942 *tunables, const char *buf, size_t count)
947 ret = kstrtoul(buf, 0, &val);
951 tunables->boostpulse_duration_val = val;
955 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
958 return sprintf(buf, "%u\n", tunables->io_is_busy);
961 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
962 const char *buf, size_t count)
967 ret = kstrtoul(buf, 0, &val);
970 tunables->io_is_busy = val;
975 * Create show/store routines
976 * - sys: One governor instance for complete SYSTEM
977 * - pol: One governor instance per struct cpufreq_policy
979 #define show_gov_pol_sys(file_name) \
980 static ssize_t show_##file_name##_gov_sys \
981 (struct kobject *kobj, struct attribute *attr, char *buf) \
983 return show_##file_name(common_tunables, buf); \
986 static ssize_t show_##file_name##_gov_pol \
987 (struct cpufreq_policy *policy, char *buf) \
989 return show_##file_name(policy->governor_data, buf); \
992 #define store_gov_pol_sys(file_name) \
993 static ssize_t store_##file_name##_gov_sys \
994 (struct kobject *kobj, struct attribute *attr, const char *buf, \
997 return store_##file_name(common_tunables, buf, count); \
1000 static ssize_t store_##file_name##_gov_pol \
1001 (struct cpufreq_policy *policy, const char *buf, size_t count) \
1003 return store_##file_name(policy->governor_data, buf, count); \
1006 #define show_store_gov_pol_sys(file_name) \
1007 show_gov_pol_sys(file_name); \
1008 store_gov_pol_sys(file_name)
1010 show_store_gov_pol_sys(target_loads);
1011 show_store_gov_pol_sys(above_hispeed_delay);
1012 show_store_gov_pol_sys(hispeed_freq);
1013 show_store_gov_pol_sys(go_hispeed_load);
1014 show_store_gov_pol_sys(min_sample_time);
1015 show_store_gov_pol_sys(timer_rate);
1016 show_store_gov_pol_sys(timer_slack);
1017 show_store_gov_pol_sys(boost);
1018 store_gov_pol_sys(boostpulse);
1019 show_store_gov_pol_sys(boostpulse_duration);
1020 show_store_gov_pol_sys(io_is_busy);
1022 #define gov_sys_attr_rw(_name) \
1023 static struct global_attr _name##_gov_sys = \
1024 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1026 #define gov_pol_attr_rw(_name) \
1027 static struct freq_attr _name##_gov_pol = \
1028 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1030 #define gov_sys_pol_attr_rw(_name) \
1031 gov_sys_attr_rw(_name); \
1032 gov_pol_attr_rw(_name)
1034 gov_sys_pol_attr_rw(target_loads);
1035 gov_sys_pol_attr_rw(above_hispeed_delay);
1036 gov_sys_pol_attr_rw(hispeed_freq);
1037 gov_sys_pol_attr_rw(go_hispeed_load);
1038 gov_sys_pol_attr_rw(min_sample_time);
1039 gov_sys_pol_attr_rw(timer_rate);
1040 gov_sys_pol_attr_rw(timer_slack);
1041 gov_sys_pol_attr_rw(boost);
1042 gov_sys_pol_attr_rw(boostpulse_duration);
1043 gov_sys_pol_attr_rw(io_is_busy);
1045 static struct global_attr boostpulse_gov_sys =
1046 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1048 static struct freq_attr boostpulse_gov_pol =
1049 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1051 /* One Governor instance for entire system */
1052 static struct attribute *interactive_attributes_gov_sys[] = {
1053 &target_loads_gov_sys.attr,
1054 &above_hispeed_delay_gov_sys.attr,
1055 &hispeed_freq_gov_sys.attr,
1056 &go_hispeed_load_gov_sys.attr,
1057 &min_sample_time_gov_sys.attr,
1058 &timer_rate_gov_sys.attr,
1059 &timer_slack_gov_sys.attr,
1060 &boost_gov_sys.attr,
1061 &boostpulse_gov_sys.attr,
1062 &boostpulse_duration_gov_sys.attr,
1063 &io_is_busy_gov_sys.attr,
1067 static struct attribute_group interactive_attr_group_gov_sys = {
1068 .attrs = interactive_attributes_gov_sys,
1069 .name = "interactive",
1072 /* Per policy governor instance */
1073 static struct attribute *interactive_attributes_gov_pol[] = {
1074 &target_loads_gov_pol.attr,
1075 &above_hispeed_delay_gov_pol.attr,
1076 &hispeed_freq_gov_pol.attr,
1077 &go_hispeed_load_gov_pol.attr,
1078 &min_sample_time_gov_pol.attr,
1079 &timer_rate_gov_pol.attr,
1080 &timer_slack_gov_pol.attr,
1081 &boost_gov_pol.attr,
1082 &boostpulse_gov_pol.attr,
1083 &boostpulse_duration_gov_pol.attr,
1084 &io_is_busy_gov_pol.attr,
1088 static struct attribute_group interactive_attr_group_gov_pol = {
1089 .attrs = interactive_attributes_gov_pol,
1090 .name = "interactive",
1093 static struct attribute_group *get_sysfs_attr(void)
1095 if (have_governor_per_policy())
1096 return &interactive_attr_group_gov_pol;
1098 return &interactive_attr_group_gov_sys;
1101 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1107 cpufreq_interactive_idle_start();
1110 cpufreq_interactive_idle_end();
1117 static struct notifier_block cpufreq_interactive_idle_nb = {
1118 .notifier_call = cpufreq_interactive_idle_notifier,
1121 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1126 struct cpufreq_interactive_cpuinfo *pcpu;
1127 struct cpufreq_frequency_table *freq_table;
1128 struct cpufreq_interactive_tunables *tunables;
1129 unsigned long flags;
1131 if (have_governor_per_policy())
1132 tunables = policy->governor_data;
1134 tunables = common_tunables;
1136 WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1139 case CPUFREQ_GOV_POLICY_INIT:
1140 if (have_governor_per_policy()) {
1142 } else if (tunables) {
1143 tunables->usage_count++;
1144 policy->governor_data = tunables;
1148 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1150 pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1154 tunables->usage_count = 1;
1155 tunables->above_hispeed_delay = default_above_hispeed_delay;
1156 tunables->nabove_hispeed_delay =
1157 ARRAY_SIZE(default_above_hispeed_delay);
1158 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1159 tunables->target_loads = default_target_loads;
1160 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1161 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1162 tunables->timer_rate = DEFAULT_TIMER_RATE;
1163 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1164 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1166 spin_lock_init(&tunables->target_loads_lock);
1167 spin_lock_init(&tunables->above_hispeed_delay_lock);
1169 policy->governor_data = tunables;
1170 if (!have_governor_per_policy()) {
1171 common_tunables = tunables;
1172 WARN_ON(cpufreq_get_global_kobject());
1175 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1179 policy->governor_data = NULL;
1180 if (!have_governor_per_policy()) {
1181 common_tunables = NULL;
1182 cpufreq_put_global_kobject();
1187 if (!policy->governor->initialized) {
1188 idle_notifier_register(&cpufreq_interactive_idle_nb);
1189 cpufreq_register_notifier(&cpufreq_notifier_block,
1190 CPUFREQ_TRANSITION_NOTIFIER);
1195 case CPUFREQ_GOV_POLICY_EXIT:
1196 if (!--tunables->usage_count) {
1197 if (policy->governor->initialized == 1) {
1198 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1199 CPUFREQ_TRANSITION_NOTIFIER);
1200 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1203 sysfs_remove_group(get_governor_parent_kobj(policy),
1206 if (!have_governor_per_policy())
1207 cpufreq_put_global_kobject();
1210 common_tunables = NULL;
1213 policy->governor_data = NULL;
1216 case CPUFREQ_GOV_START:
1217 mutex_lock(&gov_lock);
1219 freq_table = cpufreq_frequency_get_table(policy->cpu);
1220 if (!tunables->hispeed_freq)
1221 tunables->hispeed_freq = policy->max;
1223 for_each_cpu(j, policy->cpus) {
1224 pcpu = &per_cpu(cpuinfo, j);
1225 pcpu->policy = policy;
1226 pcpu->target_freq = policy->cur;
1227 pcpu->freq_table = freq_table;
1228 pcpu->floor_freq = pcpu->target_freq;
1229 pcpu->floor_validate_time =
1230 ktime_to_us(ktime_get());
1231 pcpu->hispeed_validate_time =
1232 pcpu->floor_validate_time;
1233 pcpu->max_freq = policy->max;
1234 down_write(&pcpu->enable_sem);
1235 del_timer_sync(&pcpu->cpu_timer);
1236 del_timer_sync(&pcpu->cpu_slack_timer);
1237 cpufreq_interactive_timer_start(tunables, j);
1238 pcpu->governor_enabled = 1;
1239 up_write(&pcpu->enable_sem);
1242 mutex_unlock(&gov_lock);
1245 case CPUFREQ_GOV_STOP:
1246 mutex_lock(&gov_lock);
1247 for_each_cpu(j, policy->cpus) {
1248 pcpu = &per_cpu(cpuinfo, j);
1249 down_write(&pcpu->enable_sem);
1250 pcpu->governor_enabled = 0;
1251 del_timer_sync(&pcpu->cpu_timer);
1252 del_timer_sync(&pcpu->cpu_slack_timer);
1253 up_write(&pcpu->enable_sem);
1256 mutex_unlock(&gov_lock);
1259 case CPUFREQ_GOV_LIMITS:
1260 if (policy->max < policy->cur)
1261 __cpufreq_driver_target(policy,
1262 policy->max, CPUFREQ_RELATION_H);
1263 else if (policy->min > policy->cur)
1264 __cpufreq_driver_target(policy,
1265 policy->min, CPUFREQ_RELATION_L);
1266 for_each_cpu(j, policy->cpus) {
1267 pcpu = &per_cpu(cpuinfo, j);
1269 down_read(&pcpu->enable_sem);
1270 if (pcpu->governor_enabled == 0) {
1271 up_read(&pcpu->enable_sem);
1275 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
1276 if (policy->max < pcpu->target_freq)
1277 pcpu->target_freq = policy->max;
1278 else if (policy->min > pcpu->target_freq)
1279 pcpu->target_freq = policy->min;
1281 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
1282 up_read(&pcpu->enable_sem);
1284 /* Reschedule timer only if policy->max is raised.
1285 * Delete the timers, else the timer callback may
1286 * return without re-arm the timer when failed
1287 * acquire the semaphore. This race may cause timer
1288 * stopped unexpectedly.
1291 if (policy->max > pcpu->max_freq) {
1292 down_write(&pcpu->enable_sem);
1293 del_timer_sync(&pcpu->cpu_timer);
1294 del_timer_sync(&pcpu->cpu_slack_timer);
1295 cpufreq_interactive_timer_start(tunables, j);
1296 up_write(&pcpu->enable_sem);
1299 pcpu->max_freq = policy->max;
1306 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1309 struct cpufreq_governor cpufreq_gov_interactive = {
1310 .name = "interactive",
1311 .governor = cpufreq_governor_interactive,
1312 .max_transition_latency = 10000000,
1313 .owner = THIS_MODULE,
1316 static void cpufreq_interactive_nop_timer(unsigned long data)
1320 static int __init cpufreq_interactive_init(void)
1323 struct cpufreq_interactive_cpuinfo *pcpu;
1324 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1326 /* Initalize per-cpu timers */
1327 for_each_possible_cpu(i) {
1328 pcpu = &per_cpu(cpuinfo, i);
1329 init_timer_deferrable(&pcpu->cpu_timer);
1330 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1331 pcpu->cpu_timer.data = i;
1332 init_timer(&pcpu->cpu_slack_timer);
1333 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1334 spin_lock_init(&pcpu->load_lock);
1335 spin_lock_init(&pcpu->target_freq_lock);
1336 init_rwsem(&pcpu->enable_sem);
1339 spin_lock_init(&speedchange_cpumask_lock);
1340 mutex_init(&gov_lock);
1342 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1344 if (IS_ERR(speedchange_task))
1345 return PTR_ERR(speedchange_task);
1347 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1348 get_task_struct(speedchange_task);
1350 /* NB: wake up so the thread does not look hung to the freezer */
1351 wake_up_process(speedchange_task);
1353 return cpufreq_register_governor(&cpufreq_gov_interactive);
1356 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1357 fs_initcall(cpufreq_interactive_init);
1359 module_init(cpufreq_interactive_init);
1362 static void __exit cpufreq_interactive_exit(void)
1364 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1365 kthread_stop(speedchange_task);
1366 put_task_struct(speedchange_task);
1369 module_exit(cpufreq_interactive_exit);
1371 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1372 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1373 "Latency sensitive workloads");
1374 MODULE_LICENSE("GPL");