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;
109 * Max additional time to wait in idle, beyond timer_rate, at speeds
110 * above minimum before wakeup to reduce speed, or -1 if unnecessary.
112 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
117 /* For cases where we have single governor instance for system */
118 static struct cpufreq_interactive_tunables *common_tunables;
120 static struct attribute_group *get_sysfs_attr(void);
122 static void cpufreq_interactive_timer_resched(
123 struct cpufreq_interactive_cpuinfo *pcpu)
125 struct cpufreq_interactive_tunables *tunables =
126 pcpu->policy->governor_data;
127 unsigned long expires;
130 spin_lock_irqsave(&pcpu->load_lock, flags);
132 get_cpu_idle_time(smp_processor_id(),
133 &pcpu->time_in_idle_timestamp,
134 tunables->io_is_busy);
135 pcpu->cputime_speedadj = 0;
136 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
137 expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
138 mod_timer_pinned(&pcpu->cpu_timer, expires);
140 if (tunables->timer_slack_val >= 0 &&
141 pcpu->target_freq > pcpu->policy->min) {
142 expires += usecs_to_jiffies(tunables->timer_slack_val);
143 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
146 spin_unlock_irqrestore(&pcpu->load_lock, flags);
149 /* The caller shall take enable_sem write semaphore to avoid any timer race.
150 * The cpu_timer and cpu_slack_timer must be deactivated when calling this
153 static void cpufreq_interactive_timer_start(
154 struct cpufreq_interactive_tunables *tunables, int cpu)
156 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
157 unsigned long expires = jiffies +
158 usecs_to_jiffies(tunables->timer_rate);
161 pcpu->cpu_timer.expires = expires;
162 add_timer_on(&pcpu->cpu_timer, cpu);
163 if (tunables->timer_slack_val >= 0 &&
164 pcpu->target_freq > pcpu->policy->min) {
165 expires += usecs_to_jiffies(tunables->timer_slack_val);
166 pcpu->cpu_slack_timer.expires = expires;
167 add_timer_on(&pcpu->cpu_slack_timer, cpu);
170 spin_lock_irqsave(&pcpu->load_lock, flags);
172 get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
173 tunables->io_is_busy);
174 pcpu->cputime_speedadj = 0;
175 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
176 spin_unlock_irqrestore(&pcpu->load_lock, flags);
179 static unsigned int freq_to_above_hispeed_delay(
180 struct cpufreq_interactive_tunables *tunables,
187 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
189 for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
190 freq >= tunables->above_hispeed_delay[i+1]; i += 2)
193 ret = tunables->above_hispeed_delay[i];
194 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
198 static unsigned int freq_to_targetload(
199 struct cpufreq_interactive_tunables *tunables, unsigned int freq)
205 spin_lock_irqsave(&tunables->target_loads_lock, flags);
207 for (i = 0; i < tunables->ntarget_loads - 1 &&
208 freq >= tunables->target_loads[i+1]; i += 2)
211 ret = tunables->target_loads[i];
212 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
217 * If increasing frequencies never map to a lower target load then
218 * choose_freq() will find the minimum frequency that does not exceed its
219 * target load given the current load.
221 static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
222 unsigned int loadadjfreq)
224 unsigned int freq = pcpu->policy->cur;
225 unsigned int prevfreq, freqmin, freqmax;
234 tl = freq_to_targetload(pcpu->policy->governor_data, freq);
237 * Find the lowest frequency where the computed load is less
238 * than or equal to the target load.
241 if (cpufreq_frequency_table_target(
242 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
243 CPUFREQ_RELATION_L, &index))
245 freq = pcpu->freq_table[index].frequency;
247 if (freq > prevfreq) {
248 /* The previous frequency is too low. */
251 if (freq >= freqmax) {
253 * Find the highest frequency that is less
256 if (cpufreq_frequency_table_target(
257 pcpu->policy, pcpu->freq_table,
258 freqmax - 1, CPUFREQ_RELATION_H,
261 freq = pcpu->freq_table[index].frequency;
263 if (freq == freqmin) {
265 * The first frequency below freqmax
266 * has already been found to be too
267 * low. freqmax is the lowest speed
268 * we found that is fast enough.
274 } else if (freq < prevfreq) {
275 /* The previous frequency is high enough. */
278 if (freq <= freqmin) {
280 * Find the lowest frequency that is higher
283 if (cpufreq_frequency_table_target(
284 pcpu->policy, pcpu->freq_table,
285 freqmin + 1, CPUFREQ_RELATION_L,
288 freq = pcpu->freq_table[index].frequency;
291 * If freqmax is the first frequency above
292 * freqmin then we have already found that
293 * this speed is fast enough.
300 /* If same frequency chosen as previous then done. */
301 } while (freq != prevfreq);
306 static u64 update_load(int cpu)
308 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
309 struct cpufreq_interactive_tunables *tunables =
310 pcpu->policy->governor_data;
313 unsigned int delta_idle;
314 unsigned int delta_time;
317 now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
318 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
319 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
321 if (delta_time <= delta_idle)
324 active_time = delta_time - delta_idle;
326 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
328 pcpu->time_in_idle = now_idle;
329 pcpu->time_in_idle_timestamp = now;
333 static void cpufreq_interactive_timer(unsigned long data)
336 unsigned int delta_time;
337 u64 cputime_speedadj;
339 struct cpufreq_interactive_cpuinfo *pcpu =
340 &per_cpu(cpuinfo, data);
341 struct cpufreq_interactive_tunables *tunables =
342 pcpu->policy->governor_data;
343 unsigned int new_freq;
344 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 boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
369 if (cpu_load >= tunables->go_hispeed_load || 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 (!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 trace_cpufreq_interactive_already(
437 data, cpu_load, pcpu->target_freq,
438 pcpu->policy->cur, new_freq);
439 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
440 goto rearm_if_notmax;
443 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
444 pcpu->policy->cur, new_freq);
446 pcpu->target_freq = new_freq;
447 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
448 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
449 cpumask_set_cpu(data, &speedchange_cpumask);
450 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
451 wake_up_process(speedchange_task);
455 * Already set max speed and don't see a need to change that,
456 * wait until next idle to re-evaluate, don't need timer.
458 if (pcpu->target_freq == pcpu->policy->max)
462 if (!timer_pending(&pcpu->cpu_timer))
463 cpufreq_interactive_timer_resched(pcpu);
466 up_read(&pcpu->enable_sem);
470 static void cpufreq_interactive_idle_start(void)
472 struct cpufreq_interactive_cpuinfo *pcpu =
473 &per_cpu(cpuinfo, smp_processor_id());
476 if (!down_read_trylock(&pcpu->enable_sem))
478 if (!pcpu->governor_enabled) {
479 up_read(&pcpu->enable_sem);
483 pending = timer_pending(&pcpu->cpu_timer);
485 if (pcpu->target_freq != pcpu->policy->min) {
487 * Entering idle while not at lowest speed. On some
488 * platforms this can hold the other CPU(s) at that speed
489 * even though the CPU is idle. Set a timer to re-evaluate
490 * speed so this idle CPU doesn't hold the other CPUs above
491 * min indefinitely. This should probably be a quirk of
492 * the CPUFreq driver.
495 cpufreq_interactive_timer_resched(pcpu);
498 up_read(&pcpu->enable_sem);
501 static void cpufreq_interactive_idle_end(void)
503 struct cpufreq_interactive_cpuinfo *pcpu =
504 &per_cpu(cpuinfo, smp_processor_id());
506 if (!down_read_trylock(&pcpu->enable_sem))
508 if (!pcpu->governor_enabled) {
509 up_read(&pcpu->enable_sem);
513 /* Arm the timer for 1-2 ticks later if not already. */
514 if (!timer_pending(&pcpu->cpu_timer)) {
515 cpufreq_interactive_timer_resched(pcpu);
516 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
517 del_timer(&pcpu->cpu_timer);
518 del_timer(&pcpu->cpu_slack_timer);
519 cpufreq_interactive_timer(smp_processor_id());
522 up_read(&pcpu->enable_sem);
525 static int cpufreq_interactive_speedchange_task(void *data)
530 struct cpufreq_interactive_cpuinfo *pcpu;
533 set_current_state(TASK_INTERRUPTIBLE);
534 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
536 if (cpumask_empty(&speedchange_cpumask)) {
537 spin_unlock_irqrestore(&speedchange_cpumask_lock,
541 if (kthread_should_stop())
544 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
547 set_current_state(TASK_RUNNING);
548 tmp_mask = speedchange_cpumask;
549 cpumask_clear(&speedchange_cpumask);
550 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
552 for_each_cpu(cpu, &tmp_mask) {
554 unsigned int max_freq = 0;
556 pcpu = &per_cpu(cpuinfo, cpu);
557 if (!down_read_trylock(&pcpu->enable_sem))
559 if (!pcpu->governor_enabled) {
560 up_read(&pcpu->enable_sem);
564 for_each_cpu(j, pcpu->policy->cpus) {
565 struct cpufreq_interactive_cpuinfo *pjcpu =
566 &per_cpu(cpuinfo, j);
568 if (pjcpu->target_freq > max_freq)
569 max_freq = pjcpu->target_freq;
572 if (max_freq != pcpu->policy->cur)
573 __cpufreq_driver_target(pcpu->policy,
576 trace_cpufreq_interactive_setspeed(cpu,
580 up_read(&pcpu->enable_sem);
587 static void cpufreq_interactive_boost(void)
591 unsigned long flags[2];
592 struct cpufreq_interactive_cpuinfo *pcpu;
593 struct cpufreq_interactive_tunables *tunables;
595 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
597 for_each_online_cpu(i) {
598 pcpu = &per_cpu(cpuinfo, i);
599 tunables = pcpu->policy->governor_data;
601 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
602 if (pcpu->target_freq < tunables->hispeed_freq) {
603 pcpu->target_freq = tunables->hispeed_freq;
604 cpumask_set_cpu(i, &speedchange_cpumask);
605 pcpu->hispeed_validate_time =
606 ktime_to_us(ktime_get());
611 * Set floor freq and (re)start timer for when last
615 pcpu->floor_freq = tunables->hispeed_freq;
616 pcpu->floor_validate_time = ktime_to_us(ktime_get());
617 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
620 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
623 wake_up_process(speedchange_task);
626 static int cpufreq_interactive_notifier(
627 struct notifier_block *nb, unsigned long val, void *data)
629 struct cpufreq_freqs *freq = data;
630 struct cpufreq_interactive_cpuinfo *pcpu;
634 if (val == CPUFREQ_POSTCHANGE) {
635 pcpu = &per_cpu(cpuinfo, freq->cpu);
636 if (!down_read_trylock(&pcpu->enable_sem))
638 if (!pcpu->governor_enabled) {
639 up_read(&pcpu->enable_sem);
643 for_each_cpu(cpu, pcpu->policy->cpus) {
644 struct cpufreq_interactive_cpuinfo *pjcpu =
645 &per_cpu(cpuinfo, cpu);
646 if (cpu != freq->cpu) {
647 if (!down_read_trylock(&pjcpu->enable_sem))
649 if (!pjcpu->governor_enabled) {
650 up_read(&pjcpu->enable_sem);
654 spin_lock_irqsave(&pjcpu->load_lock, flags);
656 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
657 if (cpu != freq->cpu)
658 up_read(&pjcpu->enable_sem);
661 up_read(&pcpu->enable_sem);
666 static struct notifier_block cpufreq_notifier_block = {
667 .notifier_call = cpufreq_interactive_notifier,
670 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
675 unsigned int *tokenized_data;
679 while ((cp = strpbrk(cp + 1, " :")))
682 if (!(ntokens & 0x1))
685 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
686 if (!tokenized_data) {
693 while (i < ntokens) {
694 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
697 cp = strpbrk(cp, " :");
706 *num_tokens = ntokens;
707 return tokenized_data;
710 kfree(tokenized_data);
715 static ssize_t show_target_loads(
716 struct cpufreq_interactive_tunables *tunables,
723 spin_lock_irqsave(&tunables->target_loads_lock, flags);
725 for (i = 0; i < tunables->ntarget_loads; i++)
726 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
727 i & 0x1 ? ":" : " ");
729 sprintf(buf + ret - 1, "\n");
730 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
734 static ssize_t store_target_loads(
735 struct cpufreq_interactive_tunables *tunables,
736 const char *buf, size_t count)
739 unsigned int *new_target_loads = NULL;
742 new_target_loads = get_tokenized_data(buf, &ntokens);
743 if (IS_ERR(new_target_loads))
744 return PTR_RET(new_target_loads);
746 spin_lock_irqsave(&tunables->target_loads_lock, flags);
747 if (tunables->target_loads != default_target_loads)
748 kfree(tunables->target_loads);
749 tunables->target_loads = new_target_loads;
750 tunables->ntarget_loads = ntokens;
751 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
755 static ssize_t show_above_hispeed_delay(
756 struct cpufreq_interactive_tunables *tunables, char *buf)
762 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
764 for (i = 0; i < tunables->nabove_hispeed_delay; i++)
765 ret += sprintf(buf + ret, "%u%s",
766 tunables->above_hispeed_delay[i],
767 i & 0x1 ? ":" : " ");
769 sprintf(buf + ret - 1, "\n");
770 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
774 static ssize_t store_above_hispeed_delay(
775 struct cpufreq_interactive_tunables *tunables,
776 const char *buf, size_t count)
779 unsigned int *new_above_hispeed_delay = NULL;
782 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
783 if (IS_ERR(new_above_hispeed_delay))
784 return PTR_RET(new_above_hispeed_delay);
786 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
787 if (tunables->above_hispeed_delay != default_above_hispeed_delay)
788 kfree(tunables->above_hispeed_delay);
789 tunables->above_hispeed_delay = new_above_hispeed_delay;
790 tunables->nabove_hispeed_delay = ntokens;
791 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
796 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
799 return sprintf(buf, "%u\n", tunables->hispeed_freq);
802 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
803 const char *buf, size_t count)
806 long unsigned int val;
808 ret = strict_strtoul(buf, 0, &val);
811 tunables->hispeed_freq = val;
815 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
816 *tunables, char *buf)
818 return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
821 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
822 *tunables, const char *buf, size_t count)
827 ret = strict_strtoul(buf, 0, &val);
830 tunables->go_hispeed_load = val;
834 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
835 *tunables, char *buf)
837 return sprintf(buf, "%lu\n", tunables->min_sample_time);
840 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
841 *tunables, const char *buf, size_t count)
846 ret = strict_strtoul(buf, 0, &val);
849 tunables->min_sample_time = val;
853 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
856 return sprintf(buf, "%lu\n", tunables->timer_rate);
859 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
860 const char *buf, size_t count)
865 ret = strict_strtoul(buf, 0, &val);
868 tunables->timer_rate = val;
872 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
875 return sprintf(buf, "%d\n", tunables->timer_slack_val);
878 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
879 const char *buf, size_t count)
884 ret = kstrtol(buf, 10, &val);
888 tunables->timer_slack_val = val;
892 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
895 return sprintf(buf, "%d\n", tunables->boost_val);
898 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
899 const char *buf, size_t count)
904 ret = kstrtoul(buf, 0, &val);
908 tunables->boost_val = val;
910 if (tunables->boost_val) {
911 trace_cpufreq_interactive_boost("on");
912 cpufreq_interactive_boost();
914 tunables->boostpulse_endtime = ktime_to_us(ktime_get());
915 trace_cpufreq_interactive_unboost("off");
921 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
922 const char *buf, size_t count)
927 ret = kstrtoul(buf, 0, &val);
931 tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
932 tunables->boostpulse_duration_val;
933 trace_cpufreq_interactive_boost("pulse");
934 cpufreq_interactive_boost();
938 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
939 *tunables, char *buf)
941 return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
944 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
945 *tunables, const char *buf, size_t count)
950 ret = kstrtoul(buf, 0, &val);
954 tunables->boostpulse_duration_val = val;
958 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
961 return sprintf(buf, "%u\n", tunables->io_is_busy);
964 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
965 const char *buf, size_t count)
970 ret = kstrtoul(buf, 0, &val);
973 tunables->io_is_busy = val;
978 * Create show/store routines
979 * - sys: One governor instance for complete SYSTEM
980 * - pol: One governor instance per struct cpufreq_policy
982 #define show_gov_pol_sys(file_name) \
983 static ssize_t show_##file_name##_gov_sys \
984 (struct kobject *kobj, struct attribute *attr, char *buf) \
986 return show_##file_name(common_tunables, buf); \
989 static ssize_t show_##file_name##_gov_pol \
990 (struct cpufreq_policy *policy, char *buf) \
992 return show_##file_name(policy->governor_data, buf); \
995 #define store_gov_pol_sys(file_name) \
996 static ssize_t store_##file_name##_gov_sys \
997 (struct kobject *kobj, struct attribute *attr, const char *buf, \
1000 return store_##file_name(common_tunables, buf, count); \
1003 static ssize_t store_##file_name##_gov_pol \
1004 (struct cpufreq_policy *policy, const char *buf, size_t count) \
1006 return store_##file_name(policy->governor_data, buf, count); \
1009 #define show_store_gov_pol_sys(file_name) \
1010 show_gov_pol_sys(file_name); \
1011 store_gov_pol_sys(file_name)
1013 show_store_gov_pol_sys(target_loads);
1014 show_store_gov_pol_sys(above_hispeed_delay);
1015 show_store_gov_pol_sys(hispeed_freq);
1016 show_store_gov_pol_sys(go_hispeed_load);
1017 show_store_gov_pol_sys(min_sample_time);
1018 show_store_gov_pol_sys(timer_rate);
1019 show_store_gov_pol_sys(timer_slack);
1020 show_store_gov_pol_sys(boost);
1021 store_gov_pol_sys(boostpulse);
1022 show_store_gov_pol_sys(boostpulse_duration);
1023 show_store_gov_pol_sys(io_is_busy);
1025 #define gov_sys_attr_rw(_name) \
1026 static struct global_attr _name##_gov_sys = \
1027 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1029 #define gov_pol_attr_rw(_name) \
1030 static struct freq_attr _name##_gov_pol = \
1031 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1033 #define gov_sys_pol_attr_rw(_name) \
1034 gov_sys_attr_rw(_name); \
1035 gov_pol_attr_rw(_name)
1037 gov_sys_pol_attr_rw(target_loads);
1038 gov_sys_pol_attr_rw(above_hispeed_delay);
1039 gov_sys_pol_attr_rw(hispeed_freq);
1040 gov_sys_pol_attr_rw(go_hispeed_load);
1041 gov_sys_pol_attr_rw(min_sample_time);
1042 gov_sys_pol_attr_rw(timer_rate);
1043 gov_sys_pol_attr_rw(timer_slack);
1044 gov_sys_pol_attr_rw(boost);
1045 gov_sys_pol_attr_rw(boostpulse_duration);
1046 gov_sys_pol_attr_rw(io_is_busy);
1048 static struct global_attr boostpulse_gov_sys =
1049 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1051 static struct freq_attr boostpulse_gov_pol =
1052 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1054 /* One Governor instance for entire system */
1055 static struct attribute *interactive_attributes_gov_sys[] = {
1056 &target_loads_gov_sys.attr,
1057 &above_hispeed_delay_gov_sys.attr,
1058 &hispeed_freq_gov_sys.attr,
1059 &go_hispeed_load_gov_sys.attr,
1060 &min_sample_time_gov_sys.attr,
1061 &timer_rate_gov_sys.attr,
1062 &timer_slack_gov_sys.attr,
1063 &boost_gov_sys.attr,
1064 &boostpulse_gov_sys.attr,
1065 &boostpulse_duration_gov_sys.attr,
1066 &io_is_busy_gov_sys.attr,
1070 static struct attribute_group interactive_attr_group_gov_sys = {
1071 .attrs = interactive_attributes_gov_sys,
1072 .name = "interactive",
1075 /* Per policy governor instance */
1076 static struct attribute *interactive_attributes_gov_pol[] = {
1077 &target_loads_gov_pol.attr,
1078 &above_hispeed_delay_gov_pol.attr,
1079 &hispeed_freq_gov_pol.attr,
1080 &go_hispeed_load_gov_pol.attr,
1081 &min_sample_time_gov_pol.attr,
1082 &timer_rate_gov_pol.attr,
1083 &timer_slack_gov_pol.attr,
1084 &boost_gov_pol.attr,
1085 &boostpulse_gov_pol.attr,
1086 &boostpulse_duration_gov_pol.attr,
1087 &io_is_busy_gov_pol.attr,
1091 static struct attribute_group interactive_attr_group_gov_pol = {
1092 .attrs = interactive_attributes_gov_pol,
1093 .name = "interactive",
1096 static struct attribute_group *get_sysfs_attr(void)
1098 if (have_governor_per_policy())
1099 return &interactive_attr_group_gov_pol;
1101 return &interactive_attr_group_gov_sys;
1104 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1110 cpufreq_interactive_idle_start();
1113 cpufreq_interactive_idle_end();
1120 static struct notifier_block cpufreq_interactive_idle_nb = {
1121 .notifier_call = cpufreq_interactive_idle_notifier,
1124 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1129 struct cpufreq_interactive_cpuinfo *pcpu;
1130 struct cpufreq_frequency_table *freq_table;
1131 struct cpufreq_interactive_tunables *tunables;
1132 unsigned long flags;
1134 if (have_governor_per_policy())
1135 tunables = policy->governor_data;
1137 tunables = common_tunables;
1139 WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1142 case CPUFREQ_GOV_POLICY_INIT:
1143 if (have_governor_per_policy()) {
1145 } else if (tunables) {
1146 tunables->usage_count++;
1147 policy->governor_data = tunables;
1151 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1153 pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1157 tunables->usage_count = 1;
1158 tunables->above_hispeed_delay = default_above_hispeed_delay;
1159 tunables->nabove_hispeed_delay =
1160 ARRAY_SIZE(default_above_hispeed_delay);
1161 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1162 tunables->target_loads = default_target_loads;
1163 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1164 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1165 tunables->timer_rate = DEFAULT_TIMER_RATE;
1166 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1167 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1169 spin_lock_init(&tunables->target_loads_lock);
1170 spin_lock_init(&tunables->above_hispeed_delay_lock);
1172 policy->governor_data = tunables;
1173 if (!have_governor_per_policy()) {
1174 common_tunables = tunables;
1175 WARN_ON(cpufreq_get_global_kobject());
1178 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1182 policy->governor_data = NULL;
1183 if (!have_governor_per_policy())
1184 common_tunables = NULL;
1188 if (!policy->governor->initialized) {
1189 idle_notifier_register(&cpufreq_interactive_idle_nb);
1190 cpufreq_register_notifier(&cpufreq_notifier_block,
1191 CPUFREQ_TRANSITION_NOTIFIER);
1196 case CPUFREQ_GOV_POLICY_EXIT:
1197 if (!--tunables->usage_count) {
1198 if (policy->governor->initialized == 1) {
1199 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1200 CPUFREQ_TRANSITION_NOTIFIER);
1201 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1204 sysfs_remove_group(get_governor_parent_kobj(policy),
1207 if (!have_governor_per_policy())
1208 cpufreq_put_global_kobject();
1211 common_tunables = NULL;
1214 policy->governor_data = NULL;
1217 case CPUFREQ_GOV_START:
1218 mutex_lock(&gov_lock);
1220 freq_table = cpufreq_frequency_get_table(policy->cpu);
1221 if (!tunables->hispeed_freq)
1222 tunables->hispeed_freq = policy->max;
1224 for_each_cpu(j, policy->cpus) {
1225 pcpu = &per_cpu(cpuinfo, j);
1226 pcpu->policy = policy;
1227 pcpu->target_freq = policy->cur;
1228 pcpu->freq_table = freq_table;
1229 pcpu->floor_freq = pcpu->target_freq;
1230 pcpu->floor_validate_time =
1231 ktime_to_us(ktime_get());
1232 pcpu->hispeed_validate_time =
1233 pcpu->floor_validate_time;
1234 pcpu->max_freq = policy->max;
1235 down_write(&pcpu->enable_sem);
1236 del_timer_sync(&pcpu->cpu_timer);
1237 del_timer_sync(&pcpu->cpu_slack_timer);
1238 cpufreq_interactive_timer_start(tunables, j);
1239 pcpu->governor_enabled = 1;
1240 up_write(&pcpu->enable_sem);
1243 mutex_unlock(&gov_lock);
1246 case CPUFREQ_GOV_STOP:
1247 mutex_lock(&gov_lock);
1248 for_each_cpu(j, policy->cpus) {
1249 pcpu = &per_cpu(cpuinfo, j);
1250 down_write(&pcpu->enable_sem);
1251 pcpu->governor_enabled = 0;
1252 del_timer_sync(&pcpu->cpu_timer);
1253 del_timer_sync(&pcpu->cpu_slack_timer);
1254 up_write(&pcpu->enable_sem);
1257 mutex_unlock(&gov_lock);
1260 case CPUFREQ_GOV_LIMITS:
1261 if (policy->max < policy->cur)
1262 __cpufreq_driver_target(policy,
1263 policy->max, CPUFREQ_RELATION_H);
1264 else if (policy->min > policy->cur)
1265 __cpufreq_driver_target(policy,
1266 policy->min, CPUFREQ_RELATION_L);
1267 for_each_cpu(j, policy->cpus) {
1268 pcpu = &per_cpu(cpuinfo, j);
1270 down_read(&pcpu->enable_sem);
1271 if (pcpu->governor_enabled == 0) {
1272 up_read(&pcpu->enable_sem);
1276 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
1277 if (policy->max < pcpu->target_freq)
1278 pcpu->target_freq = policy->max;
1279 else if (policy->min > pcpu->target_freq)
1280 pcpu->target_freq = policy->min;
1282 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
1283 up_read(&pcpu->enable_sem);
1285 /* Reschedule timer only if policy->max is raised.
1286 * Delete the timers, else the timer callback may
1287 * return without re-arm the timer when failed
1288 * acquire the semaphore. This race may cause timer
1289 * stopped unexpectedly.
1292 if (policy->max > pcpu->max_freq) {
1293 down_write(&pcpu->enable_sem);
1294 del_timer_sync(&pcpu->cpu_timer);
1295 del_timer_sync(&pcpu->cpu_slack_timer);
1296 cpufreq_interactive_timer_start(tunables, j);
1297 up_write(&pcpu->enable_sem);
1300 pcpu->max_freq = policy->max;
1307 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1310 struct cpufreq_governor cpufreq_gov_interactive = {
1311 .name = "interactive",
1312 .governor = cpufreq_governor_interactive,
1313 .max_transition_latency = 10000000,
1314 .owner = THIS_MODULE,
1317 static void cpufreq_interactive_nop_timer(unsigned long data)
1321 static int __init cpufreq_interactive_init(void)
1324 struct cpufreq_interactive_cpuinfo *pcpu;
1325 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1327 /* Initalize per-cpu timers */
1328 for_each_possible_cpu(i) {
1329 pcpu = &per_cpu(cpuinfo, i);
1330 init_timer_deferrable(&pcpu->cpu_timer);
1331 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1332 pcpu->cpu_timer.data = i;
1333 init_timer(&pcpu->cpu_slack_timer);
1334 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1335 spin_lock_init(&pcpu->load_lock);
1336 spin_lock_init(&pcpu->target_freq_lock);
1337 init_rwsem(&pcpu->enable_sem);
1340 spin_lock_init(&speedchange_cpumask_lock);
1341 mutex_init(&gov_lock);
1343 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1345 if (IS_ERR(speedchange_task))
1346 return PTR_ERR(speedchange_task);
1348 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1349 get_task_struct(speedchange_task);
1351 /* NB: wake up so the thread does not look hung to the freezer */
1352 wake_up_process(speedchange_task);
1354 return cpufreq_register_governor(&cpufreq_gov_interactive);
1357 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1358 fs_initcall(cpufreq_interactive_init);
1360 module_init(cpufreq_interactive_init);
1363 static void __exit cpufreq_interactive_exit(void)
1365 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1366 kthread_stop(speedchange_task);
1367 put_task_struct(speedchange_task);
1370 module_exit(cpufreq_interactive_exit);
1372 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1373 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1374 "Latency sensitive workloads");
1375 MODULE_LICENSE("GPL");