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 pol_floor_val_time; /* policy floor_validate_time */
52 u64 loc_floor_val_time; /* per-cpu floor_validate_time */
53 u64 pol_hispeed_val_time; /* policy hispeed_validate_time */
54 u64 loc_hispeed_val_time; /* per-cpu 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 /* Target load. Lower values result in higher CPU speeds. */
68 #define DEFAULT_TARGET_LOAD 90
69 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
71 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
72 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
73 static unsigned int default_above_hispeed_delay[] = {
74 DEFAULT_ABOVE_HISPEED_DELAY };
76 struct cpufreq_interactive_tunables {
78 /* Hi speed to bump to from lo speed when load burst (default max) */
79 unsigned int hispeed_freq;
80 /* Go to hi speed when CPU load at or above this value. */
81 #define DEFAULT_GO_HISPEED_LOAD 99
82 unsigned long go_hispeed_load;
83 /* Target load. Lower values result in higher CPU speeds. */
84 spinlock_t target_loads_lock;
85 unsigned int *target_loads;
88 * The minimum amount of time to spend at a frequency before we can ramp
91 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
92 unsigned long min_sample_time;
94 * The sample rate of the timer used to increase frequency
96 unsigned long timer_rate;
98 * Wait this long before raising speed above hispeed, by default a
99 * single timer interval.
101 spinlock_t above_hispeed_delay_lock;
102 unsigned int *above_hispeed_delay;
103 int nabove_hispeed_delay;
104 /* Non-zero means indefinite speed boost active */
106 /* Duration of a boot pulse in usecs */
107 int boostpulse_duration_val;
108 /* End time of boost pulse in ktime converted to usecs */
109 u64 boostpulse_endtime;
112 * Max additional time to wait in idle, beyond timer_rate, at speeds
113 * above minimum before wakeup to reduce speed, or -1 if unnecessary.
115 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
120 /* For cases where we have single governor instance for system */
121 static struct cpufreq_interactive_tunables *common_tunables;
123 static struct attribute_group *get_sysfs_attr(void);
125 static void cpufreq_interactive_timer_resched(
126 struct cpufreq_interactive_cpuinfo *pcpu)
128 struct cpufreq_interactive_tunables *tunables =
129 pcpu->policy->governor_data;
130 unsigned long expires;
133 spin_lock_irqsave(&pcpu->load_lock, flags);
135 get_cpu_idle_time(smp_processor_id(),
136 &pcpu->time_in_idle_timestamp,
137 tunables->io_is_busy);
138 pcpu->cputime_speedadj = 0;
139 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
140 expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
141 mod_timer_pinned(&pcpu->cpu_timer, expires);
143 if (tunables->timer_slack_val >= 0 &&
144 pcpu->target_freq > pcpu->policy->min) {
145 expires += usecs_to_jiffies(tunables->timer_slack_val);
146 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
149 spin_unlock_irqrestore(&pcpu->load_lock, flags);
152 /* The caller shall take enable_sem write semaphore to avoid any timer race.
153 * The cpu_timer and cpu_slack_timer must be deactivated when calling this
156 static void cpufreq_interactive_timer_start(
157 struct cpufreq_interactive_tunables *tunables, int cpu)
159 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
160 unsigned long expires = jiffies +
161 usecs_to_jiffies(tunables->timer_rate);
164 pcpu->cpu_timer.expires = expires;
165 add_timer_on(&pcpu->cpu_timer, cpu);
166 if (tunables->timer_slack_val >= 0 &&
167 pcpu->target_freq > pcpu->policy->min) {
168 expires += usecs_to_jiffies(tunables->timer_slack_val);
169 pcpu->cpu_slack_timer.expires = expires;
170 add_timer_on(&pcpu->cpu_slack_timer, cpu);
173 spin_lock_irqsave(&pcpu->load_lock, flags);
175 get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
176 tunables->io_is_busy);
177 pcpu->cputime_speedadj = 0;
178 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
179 spin_unlock_irqrestore(&pcpu->load_lock, flags);
182 static unsigned int freq_to_above_hispeed_delay(
183 struct cpufreq_interactive_tunables *tunables,
190 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
192 for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
193 freq >= tunables->above_hispeed_delay[i+1]; i += 2)
196 ret = tunables->above_hispeed_delay[i];
197 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
201 static unsigned int freq_to_targetload(
202 struct cpufreq_interactive_tunables *tunables, unsigned int freq)
208 spin_lock_irqsave(&tunables->target_loads_lock, flags);
210 for (i = 0; i < tunables->ntarget_loads - 1 &&
211 freq >= tunables->target_loads[i+1]; i += 2)
214 ret = tunables->target_loads[i];
215 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
220 * If increasing frequencies never map to a lower target load then
221 * choose_freq() will find the minimum frequency that does not exceed its
222 * target load given the current load.
224 static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
225 unsigned int loadadjfreq)
227 unsigned int freq = pcpu->policy->cur;
228 unsigned int prevfreq, freqmin, freqmax;
237 tl = freq_to_targetload(pcpu->policy->governor_data, freq);
240 * Find the lowest frequency where the computed load is less
241 * than or equal to the target load.
244 if (cpufreq_frequency_table_target(
245 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
246 CPUFREQ_RELATION_L, &index))
248 freq = pcpu->freq_table[index].frequency;
250 if (freq > prevfreq) {
251 /* The previous frequency is too low. */
254 if (freq >= freqmax) {
256 * Find the highest frequency that is less
259 if (cpufreq_frequency_table_target(
260 pcpu->policy, pcpu->freq_table,
261 freqmax - 1, CPUFREQ_RELATION_H,
264 freq = pcpu->freq_table[index].frequency;
266 if (freq == freqmin) {
268 * The first frequency below freqmax
269 * has already been found to be too
270 * low. freqmax is the lowest speed
271 * we found that is fast enough.
277 } else if (freq < prevfreq) {
278 /* The previous frequency is high enough. */
281 if (freq <= freqmin) {
283 * Find the lowest frequency that is higher
286 if (cpufreq_frequency_table_target(
287 pcpu->policy, pcpu->freq_table,
288 freqmin + 1, CPUFREQ_RELATION_L,
291 freq = pcpu->freq_table[index].frequency;
294 * If freqmax is the first frequency above
295 * freqmin then we have already found that
296 * this speed is fast enough.
303 /* If same frequency chosen as previous then done. */
304 } while (freq != prevfreq);
309 static u64 update_load(int cpu)
311 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
312 struct cpufreq_interactive_tunables *tunables =
313 pcpu->policy->governor_data;
316 unsigned int delta_idle;
317 unsigned int delta_time;
320 now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
321 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
322 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
324 if (delta_time <= delta_idle)
327 active_time = delta_time - delta_idle;
329 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
331 pcpu->time_in_idle = now_idle;
332 pcpu->time_in_idle_timestamp = now;
336 static void cpufreq_interactive_timer(unsigned long data)
339 unsigned int delta_time;
340 u64 cputime_speedadj;
342 struct cpufreq_interactive_cpuinfo *pcpu =
343 &per_cpu(cpuinfo, data);
344 struct cpufreq_interactive_tunables *tunables =
345 pcpu->policy->governor_data;
346 unsigned int new_freq;
347 unsigned int loadadjfreq;
352 if (!down_read_trylock(&pcpu->enable_sem))
354 if (!pcpu->governor_enabled)
357 spin_lock_irqsave(&pcpu->load_lock, flags);
358 now = update_load(data);
359 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
360 cputime_speedadj = pcpu->cputime_speedadj;
361 spin_unlock_irqrestore(&pcpu->load_lock, flags);
363 if (WARN_ON_ONCE(!delta_time))
366 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
367 do_div(cputime_speedadj, delta_time);
368 loadadjfreq = (unsigned int)cputime_speedadj * 100;
369 cpu_load = loadadjfreq / pcpu->target_freq;
370 tunables->boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
372 if (cpu_load >= tunables->go_hispeed_load || tunables->boosted) {
373 if (pcpu->policy->cur < tunables->hispeed_freq) {
374 new_freq = tunables->hispeed_freq;
376 new_freq = choose_freq(pcpu, loadadjfreq);
378 if (new_freq < tunables->hispeed_freq)
379 new_freq = tunables->hispeed_freq;
382 new_freq = choose_freq(pcpu, loadadjfreq);
383 if (new_freq > tunables->hispeed_freq &&
384 pcpu->policy->cur < tunables->hispeed_freq)
385 new_freq = tunables->hispeed_freq;
388 if (pcpu->policy->cur >= tunables->hispeed_freq &&
389 new_freq > pcpu->policy->cur &&
390 now - pcpu->pol_hispeed_val_time <
391 freq_to_above_hispeed_delay(tunables, pcpu->policy->cur)) {
392 trace_cpufreq_interactive_notyet(
393 data, cpu_load, pcpu->target_freq,
394 pcpu->policy->cur, new_freq);
395 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
399 pcpu->loc_hispeed_val_time = now;
401 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
402 new_freq, CPUFREQ_RELATION_L,
404 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
408 new_freq = pcpu->freq_table[index].frequency;
411 * Do not scale below floor_freq unless we have been at or above the
412 * floor frequency for the minimum sample time since last validated.
414 max_fvtime = max(pcpu->pol_floor_val_time, pcpu->loc_floor_val_time);
415 if (new_freq < pcpu->floor_freq &&
416 pcpu->target_freq >= pcpu->policy->cur) {
417 if (now - max_fvtime < tunables->min_sample_time) {
418 trace_cpufreq_interactive_notyet(
419 data, cpu_load, pcpu->target_freq,
420 pcpu->policy->cur, new_freq);
421 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
427 * Update the timestamp for checking whether speed has been held at
428 * or above the selected frequency for a minimum of min_sample_time,
429 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
430 * allow the speed to drop as soon as the boostpulse duration expires
431 * (or the indefinite boost is turned off).
434 if (!tunables->boosted || new_freq > tunables->hispeed_freq) {
435 pcpu->floor_freq = new_freq;
436 if (pcpu->target_freq >= pcpu->policy->cur ||
437 new_freq >= pcpu->policy->cur)
438 pcpu->loc_floor_val_time = now;
441 if (pcpu->target_freq == new_freq &&
442 pcpu->target_freq <= pcpu->policy->cur) {
443 trace_cpufreq_interactive_already(
444 data, cpu_load, pcpu->target_freq,
445 pcpu->policy->cur, new_freq);
446 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
447 goto rearm_if_notmax;
450 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
451 pcpu->policy->cur, new_freq);
453 pcpu->target_freq = new_freq;
454 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
455 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
456 cpumask_set_cpu(data, &speedchange_cpumask);
457 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
458 wake_up_process(speedchange_task);
462 * Already set max speed and don't see a need to change that,
463 * wait until next idle to re-evaluate, don't need timer.
465 if (pcpu->target_freq == pcpu->policy->max)
469 if (!timer_pending(&pcpu->cpu_timer))
470 cpufreq_interactive_timer_resched(pcpu);
473 up_read(&pcpu->enable_sem);
477 static void cpufreq_interactive_idle_start(void)
479 struct cpufreq_interactive_cpuinfo *pcpu =
480 &per_cpu(cpuinfo, smp_processor_id());
483 if (!down_read_trylock(&pcpu->enable_sem))
485 if (!pcpu->governor_enabled) {
486 up_read(&pcpu->enable_sem);
490 pending = timer_pending(&pcpu->cpu_timer);
492 if (pcpu->target_freq != pcpu->policy->min) {
494 * Entering idle while not at lowest speed. On some
495 * platforms this can hold the other CPU(s) at that speed
496 * even though the CPU is idle. Set a timer to re-evaluate
497 * speed so this idle CPU doesn't hold the other CPUs above
498 * min indefinitely. This should probably be a quirk of
499 * the CPUFreq driver.
502 cpufreq_interactive_timer_resched(pcpu);
505 up_read(&pcpu->enable_sem);
508 static void cpufreq_interactive_idle_end(void)
510 struct cpufreq_interactive_cpuinfo *pcpu =
511 &per_cpu(cpuinfo, smp_processor_id());
513 if (!down_read_trylock(&pcpu->enable_sem))
515 if (!pcpu->governor_enabled) {
516 up_read(&pcpu->enable_sem);
520 /* Arm the timer for 1-2 ticks later if not already. */
521 if (!timer_pending(&pcpu->cpu_timer)) {
522 cpufreq_interactive_timer_resched(pcpu);
523 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
524 del_timer(&pcpu->cpu_timer);
525 del_timer(&pcpu->cpu_slack_timer);
526 cpufreq_interactive_timer(smp_processor_id());
529 up_read(&pcpu->enable_sem);
532 static int cpufreq_interactive_speedchange_task(void *data)
537 struct cpufreq_interactive_cpuinfo *pcpu;
540 set_current_state(TASK_INTERRUPTIBLE);
541 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
543 if (cpumask_empty(&speedchange_cpumask)) {
544 spin_unlock_irqrestore(&speedchange_cpumask_lock,
548 if (kthread_should_stop())
551 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
554 set_current_state(TASK_RUNNING);
555 tmp_mask = speedchange_cpumask;
556 cpumask_clear(&speedchange_cpumask);
557 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
559 for_each_cpu(cpu, &tmp_mask) {
561 unsigned int max_freq = 0;
562 struct cpufreq_interactive_cpuinfo *pjcpu;
563 u64 hvt = ~0ULL, fvt = 0;
565 pcpu = &per_cpu(cpuinfo, cpu);
566 if (!down_read_trylock(&pcpu->enable_sem))
568 if (!pcpu->governor_enabled) {
569 up_read(&pcpu->enable_sem);
573 for_each_cpu(j, pcpu->policy->cpus) {
574 pjcpu = &per_cpu(cpuinfo, j);
576 fvt = max(fvt, pjcpu->loc_floor_val_time);
577 if (pjcpu->target_freq > max_freq) {
578 max_freq = pjcpu->target_freq;
579 hvt = pjcpu->loc_hispeed_val_time;
580 } else if (pjcpu->target_freq == max_freq) {
581 hvt = min(hvt, pjcpu->loc_hispeed_val_time);
584 for_each_cpu(j, pcpu->policy->cpus) {
585 pjcpu = &per_cpu(cpuinfo, j);
586 pjcpu->pol_floor_val_time = fvt;
589 if (max_freq != pcpu->policy->cur) {
590 __cpufreq_driver_target(pcpu->policy,
593 for_each_cpu(j, pcpu->policy->cpus) {
594 pjcpu = &per_cpu(cpuinfo, j);
595 pjcpu->pol_hispeed_val_time = hvt;
598 trace_cpufreq_interactive_setspeed(cpu,
602 up_read(&pcpu->enable_sem);
609 static void cpufreq_interactive_boost(struct cpufreq_interactive_tunables *tunables)
613 unsigned long flags[2];
614 struct cpufreq_interactive_cpuinfo *pcpu;
616 tunables->boosted = true;
618 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
620 for_each_online_cpu(i) {
621 pcpu = &per_cpu(cpuinfo, i);
622 if (tunables != pcpu->policy->governor_data)
625 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
626 if (pcpu->target_freq < tunables->hispeed_freq) {
627 pcpu->target_freq = tunables->hispeed_freq;
628 cpumask_set_cpu(i, &speedchange_cpumask);
629 pcpu->pol_hispeed_val_time =
630 ktime_to_us(ktime_get());
633 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
636 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
639 wake_up_process(speedchange_task);
642 static int cpufreq_interactive_notifier(
643 struct notifier_block *nb, unsigned long val, void *data)
645 struct cpufreq_freqs *freq = data;
646 struct cpufreq_interactive_cpuinfo *pcpu;
650 if (val == CPUFREQ_POSTCHANGE) {
651 pcpu = &per_cpu(cpuinfo, freq->cpu);
652 if (!down_read_trylock(&pcpu->enable_sem))
654 if (!pcpu->governor_enabled) {
655 up_read(&pcpu->enable_sem);
659 for_each_cpu(cpu, pcpu->policy->cpus) {
660 struct cpufreq_interactive_cpuinfo *pjcpu =
661 &per_cpu(cpuinfo, cpu);
662 if (cpu != freq->cpu) {
663 if (!down_read_trylock(&pjcpu->enable_sem))
665 if (!pjcpu->governor_enabled) {
666 up_read(&pjcpu->enable_sem);
670 spin_lock_irqsave(&pjcpu->load_lock, flags);
672 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
673 if (cpu != freq->cpu)
674 up_read(&pjcpu->enable_sem);
677 up_read(&pcpu->enable_sem);
682 static struct notifier_block cpufreq_notifier_block = {
683 .notifier_call = cpufreq_interactive_notifier,
686 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
691 unsigned int *tokenized_data;
695 while ((cp = strpbrk(cp + 1, " :")))
698 if (!(ntokens & 0x1))
701 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
702 if (!tokenized_data) {
709 while (i < ntokens) {
710 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
713 cp = strpbrk(cp, " :");
722 *num_tokens = ntokens;
723 return tokenized_data;
726 kfree(tokenized_data);
731 static ssize_t show_target_loads(
732 struct cpufreq_interactive_tunables *tunables,
739 spin_lock_irqsave(&tunables->target_loads_lock, flags);
741 for (i = 0; i < tunables->ntarget_loads; i++)
742 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
743 i & 0x1 ? ":" : " ");
745 sprintf(buf + ret - 1, "\n");
746 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
750 static ssize_t store_target_loads(
751 struct cpufreq_interactive_tunables *tunables,
752 const char *buf, size_t count)
755 unsigned int *new_target_loads = NULL;
758 new_target_loads = get_tokenized_data(buf, &ntokens);
759 if (IS_ERR(new_target_loads))
760 return PTR_RET(new_target_loads);
762 spin_lock_irqsave(&tunables->target_loads_lock, flags);
763 if (tunables->target_loads != default_target_loads)
764 kfree(tunables->target_loads);
765 tunables->target_loads = new_target_loads;
766 tunables->ntarget_loads = ntokens;
767 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
771 static ssize_t show_above_hispeed_delay(
772 struct cpufreq_interactive_tunables *tunables, char *buf)
778 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
780 for (i = 0; i < tunables->nabove_hispeed_delay; i++)
781 ret += sprintf(buf + ret, "%u%s",
782 tunables->above_hispeed_delay[i],
783 i & 0x1 ? ":" : " ");
785 sprintf(buf + ret - 1, "\n");
786 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
790 static ssize_t store_above_hispeed_delay(
791 struct cpufreq_interactive_tunables *tunables,
792 const char *buf, size_t count)
795 unsigned int *new_above_hispeed_delay = NULL;
798 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
799 if (IS_ERR(new_above_hispeed_delay))
800 return PTR_RET(new_above_hispeed_delay);
802 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
803 if (tunables->above_hispeed_delay != default_above_hispeed_delay)
804 kfree(tunables->above_hispeed_delay);
805 tunables->above_hispeed_delay = new_above_hispeed_delay;
806 tunables->nabove_hispeed_delay = ntokens;
807 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
812 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
815 return sprintf(buf, "%u\n", tunables->hispeed_freq);
818 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
819 const char *buf, size_t count)
822 long unsigned int val;
824 ret = strict_strtoul(buf, 0, &val);
827 tunables->hispeed_freq = val;
831 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
832 *tunables, char *buf)
834 return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
837 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
838 *tunables, const char *buf, size_t count)
843 ret = strict_strtoul(buf, 0, &val);
846 tunables->go_hispeed_load = val;
850 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
851 *tunables, char *buf)
853 return sprintf(buf, "%lu\n", tunables->min_sample_time);
856 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
857 *tunables, const char *buf, size_t count)
862 ret = strict_strtoul(buf, 0, &val);
865 tunables->min_sample_time = val;
869 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
872 return sprintf(buf, "%lu\n", tunables->timer_rate);
875 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
876 const char *buf, size_t count)
879 unsigned long val, val_round;
881 ret = strict_strtoul(buf, 0, &val);
885 val_round = jiffies_to_usecs(usecs_to_jiffies(val));
886 if (val != val_round)
887 pr_warn("timer_rate not aligned to jiffy. Rounded up to %lu\n",
890 tunables->timer_rate = val_round;
894 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
897 return sprintf(buf, "%d\n", tunables->timer_slack_val);
900 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
901 const char *buf, size_t count)
906 ret = kstrtol(buf, 10, &val);
910 tunables->timer_slack_val = val;
914 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
917 return sprintf(buf, "%d\n", tunables->boost_val);
920 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
921 const char *buf, size_t count)
926 ret = kstrtoul(buf, 0, &val);
930 tunables->boost_val = val;
932 if (tunables->boost_val) {
933 trace_cpufreq_interactive_boost("on");
934 if (!tunables->boosted)
935 cpufreq_interactive_boost(tunables);
937 tunables->boostpulse_endtime = ktime_to_us(ktime_get());
938 trace_cpufreq_interactive_unboost("off");
944 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
945 const char *buf, size_t count)
950 ret = kstrtoul(buf, 0, &val);
954 tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
955 tunables->boostpulse_duration_val;
956 trace_cpufreq_interactive_boost("pulse");
957 if (!tunables->boosted)
958 cpufreq_interactive_boost(tunables);
962 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
963 *tunables, char *buf)
965 return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
968 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
969 *tunables, const char *buf, size_t count)
974 ret = kstrtoul(buf, 0, &val);
978 tunables->boostpulse_duration_val = val;
982 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
985 return sprintf(buf, "%u\n", tunables->io_is_busy);
988 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
989 const char *buf, size_t count)
994 ret = kstrtoul(buf, 0, &val);
997 tunables->io_is_busy = val;
1002 * Create show/store routines
1003 * - sys: One governor instance for complete SYSTEM
1004 * - pol: One governor instance per struct cpufreq_policy
1006 #define show_gov_pol_sys(file_name) \
1007 static ssize_t show_##file_name##_gov_sys \
1008 (struct kobject *kobj, struct attribute *attr, char *buf) \
1010 return show_##file_name(common_tunables, buf); \
1013 static ssize_t show_##file_name##_gov_pol \
1014 (struct cpufreq_policy *policy, char *buf) \
1016 return show_##file_name(policy->governor_data, buf); \
1019 #define store_gov_pol_sys(file_name) \
1020 static ssize_t store_##file_name##_gov_sys \
1021 (struct kobject *kobj, struct attribute *attr, const char *buf, \
1024 return store_##file_name(common_tunables, buf, count); \
1027 static ssize_t store_##file_name##_gov_pol \
1028 (struct cpufreq_policy *policy, const char *buf, size_t count) \
1030 return store_##file_name(policy->governor_data, buf, count); \
1033 #define show_store_gov_pol_sys(file_name) \
1034 show_gov_pol_sys(file_name); \
1035 store_gov_pol_sys(file_name)
1037 show_store_gov_pol_sys(target_loads);
1038 show_store_gov_pol_sys(above_hispeed_delay);
1039 show_store_gov_pol_sys(hispeed_freq);
1040 show_store_gov_pol_sys(go_hispeed_load);
1041 show_store_gov_pol_sys(min_sample_time);
1042 show_store_gov_pol_sys(timer_rate);
1043 show_store_gov_pol_sys(timer_slack);
1044 show_store_gov_pol_sys(boost);
1045 store_gov_pol_sys(boostpulse);
1046 show_store_gov_pol_sys(boostpulse_duration);
1047 show_store_gov_pol_sys(io_is_busy);
1049 #define gov_sys_attr_rw(_name) \
1050 static struct global_attr _name##_gov_sys = \
1051 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1053 #define gov_pol_attr_rw(_name) \
1054 static struct freq_attr _name##_gov_pol = \
1055 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1057 #define gov_sys_pol_attr_rw(_name) \
1058 gov_sys_attr_rw(_name); \
1059 gov_pol_attr_rw(_name)
1061 gov_sys_pol_attr_rw(target_loads);
1062 gov_sys_pol_attr_rw(above_hispeed_delay);
1063 gov_sys_pol_attr_rw(hispeed_freq);
1064 gov_sys_pol_attr_rw(go_hispeed_load);
1065 gov_sys_pol_attr_rw(min_sample_time);
1066 gov_sys_pol_attr_rw(timer_rate);
1067 gov_sys_pol_attr_rw(timer_slack);
1068 gov_sys_pol_attr_rw(boost);
1069 gov_sys_pol_attr_rw(boostpulse_duration);
1070 gov_sys_pol_attr_rw(io_is_busy);
1072 static struct global_attr boostpulse_gov_sys =
1073 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1075 static struct freq_attr boostpulse_gov_pol =
1076 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1078 /* One Governor instance for entire system */
1079 static struct attribute *interactive_attributes_gov_sys[] = {
1080 &target_loads_gov_sys.attr,
1081 &above_hispeed_delay_gov_sys.attr,
1082 &hispeed_freq_gov_sys.attr,
1083 &go_hispeed_load_gov_sys.attr,
1084 &min_sample_time_gov_sys.attr,
1085 &timer_rate_gov_sys.attr,
1086 &timer_slack_gov_sys.attr,
1087 &boost_gov_sys.attr,
1088 &boostpulse_gov_sys.attr,
1089 &boostpulse_duration_gov_sys.attr,
1090 &io_is_busy_gov_sys.attr,
1094 static struct attribute_group interactive_attr_group_gov_sys = {
1095 .attrs = interactive_attributes_gov_sys,
1096 .name = "interactive",
1099 /* Per policy governor instance */
1100 static struct attribute *interactive_attributes_gov_pol[] = {
1101 &target_loads_gov_pol.attr,
1102 &above_hispeed_delay_gov_pol.attr,
1103 &hispeed_freq_gov_pol.attr,
1104 &go_hispeed_load_gov_pol.attr,
1105 &min_sample_time_gov_pol.attr,
1106 &timer_rate_gov_pol.attr,
1107 &timer_slack_gov_pol.attr,
1108 &boost_gov_pol.attr,
1109 &boostpulse_gov_pol.attr,
1110 &boostpulse_duration_gov_pol.attr,
1111 &io_is_busy_gov_pol.attr,
1115 static struct attribute_group interactive_attr_group_gov_pol = {
1116 .attrs = interactive_attributes_gov_pol,
1117 .name = "interactive",
1120 static struct attribute_group *get_sysfs_attr(void)
1122 if (have_governor_per_policy())
1123 return &interactive_attr_group_gov_pol;
1125 return &interactive_attr_group_gov_sys;
1128 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1134 cpufreq_interactive_idle_start();
1137 cpufreq_interactive_idle_end();
1144 static struct notifier_block cpufreq_interactive_idle_nb = {
1145 .notifier_call = cpufreq_interactive_idle_notifier,
1148 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1153 struct cpufreq_interactive_cpuinfo *pcpu;
1154 struct cpufreq_frequency_table *freq_table;
1155 struct cpufreq_interactive_tunables *tunables;
1156 unsigned long flags;
1158 if (have_governor_per_policy())
1159 tunables = policy->governor_data;
1161 tunables = common_tunables;
1163 WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1166 case CPUFREQ_GOV_POLICY_INIT:
1167 if (have_governor_per_policy()) {
1169 } else if (tunables) {
1170 tunables->usage_count++;
1171 policy->governor_data = tunables;
1175 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1177 pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1181 tunables->usage_count = 1;
1182 tunables->above_hispeed_delay = default_above_hispeed_delay;
1183 tunables->nabove_hispeed_delay =
1184 ARRAY_SIZE(default_above_hispeed_delay);
1185 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1186 tunables->target_loads = default_target_loads;
1187 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1188 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1189 tunables->timer_rate = DEFAULT_TIMER_RATE;
1190 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1191 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1193 spin_lock_init(&tunables->target_loads_lock);
1194 spin_lock_init(&tunables->above_hispeed_delay_lock);
1196 policy->governor_data = tunables;
1197 if (!have_governor_per_policy()) {
1198 common_tunables = tunables;
1199 WARN_ON(cpufreq_get_global_kobject());
1202 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1206 policy->governor_data = NULL;
1207 if (!have_governor_per_policy()) {
1208 common_tunables = NULL;
1209 cpufreq_put_global_kobject();
1214 if (!policy->governor->initialized) {
1215 idle_notifier_register(&cpufreq_interactive_idle_nb);
1216 cpufreq_register_notifier(&cpufreq_notifier_block,
1217 CPUFREQ_TRANSITION_NOTIFIER);
1222 case CPUFREQ_GOV_POLICY_EXIT:
1223 if (!--tunables->usage_count) {
1224 if (policy->governor->initialized == 1) {
1225 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1226 CPUFREQ_TRANSITION_NOTIFIER);
1227 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1230 sysfs_remove_group(get_governor_parent_kobj(policy),
1233 if (!have_governor_per_policy())
1234 cpufreq_put_global_kobject();
1237 common_tunables = NULL;
1240 policy->governor_data = NULL;
1243 case CPUFREQ_GOV_START:
1244 mutex_lock(&gov_lock);
1246 freq_table = cpufreq_frequency_get_table(policy->cpu);
1247 if (!tunables->hispeed_freq)
1248 tunables->hispeed_freq = policy->max;
1250 for_each_cpu(j, policy->cpus) {
1251 pcpu = &per_cpu(cpuinfo, j);
1252 pcpu->policy = policy;
1253 pcpu->target_freq = policy->cur;
1254 pcpu->freq_table = freq_table;
1255 pcpu->floor_freq = pcpu->target_freq;
1256 pcpu->pol_floor_val_time =
1257 ktime_to_us(ktime_get());
1258 pcpu->loc_floor_val_time = pcpu->pol_floor_val_time;
1259 pcpu->pol_hispeed_val_time = pcpu->pol_floor_val_time;
1260 pcpu->loc_hispeed_val_time = pcpu->pol_floor_val_time;
1261 pcpu->max_freq = policy->max;
1262 down_write(&pcpu->enable_sem);
1263 del_timer_sync(&pcpu->cpu_timer);
1264 del_timer_sync(&pcpu->cpu_slack_timer);
1265 cpufreq_interactive_timer_start(tunables, j);
1266 pcpu->governor_enabled = 1;
1267 up_write(&pcpu->enable_sem);
1270 mutex_unlock(&gov_lock);
1273 case CPUFREQ_GOV_STOP:
1274 mutex_lock(&gov_lock);
1275 for_each_cpu(j, policy->cpus) {
1276 pcpu = &per_cpu(cpuinfo, j);
1277 down_write(&pcpu->enable_sem);
1278 pcpu->governor_enabled = 0;
1279 del_timer_sync(&pcpu->cpu_timer);
1280 del_timer_sync(&pcpu->cpu_slack_timer);
1281 up_write(&pcpu->enable_sem);
1284 mutex_unlock(&gov_lock);
1287 case CPUFREQ_GOV_LIMITS:
1288 if (policy->max < policy->cur)
1289 __cpufreq_driver_target(policy,
1290 policy->max, CPUFREQ_RELATION_H);
1291 else if (policy->min > policy->cur)
1292 __cpufreq_driver_target(policy,
1293 policy->min, CPUFREQ_RELATION_L);
1294 for_each_cpu(j, policy->cpus) {
1295 pcpu = &per_cpu(cpuinfo, j);
1297 down_read(&pcpu->enable_sem);
1298 if (pcpu->governor_enabled == 0) {
1299 up_read(&pcpu->enable_sem);
1303 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
1304 if (policy->max < pcpu->target_freq)
1305 pcpu->target_freq = policy->max;
1306 else if (policy->min > pcpu->target_freq)
1307 pcpu->target_freq = policy->min;
1309 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
1310 up_read(&pcpu->enable_sem);
1312 /* Reschedule timer only if policy->max is raised.
1313 * Delete the timers, else the timer callback may
1314 * return without re-arm the timer when failed
1315 * acquire the semaphore. This race may cause timer
1316 * stopped unexpectedly.
1319 if (policy->max > pcpu->max_freq) {
1320 down_write(&pcpu->enable_sem);
1321 del_timer_sync(&pcpu->cpu_timer);
1322 del_timer_sync(&pcpu->cpu_slack_timer);
1323 cpufreq_interactive_timer_start(tunables, j);
1324 up_write(&pcpu->enable_sem);
1327 pcpu->max_freq = policy->max;
1334 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1337 struct cpufreq_governor cpufreq_gov_interactive = {
1338 .name = "interactive",
1339 .governor = cpufreq_governor_interactive,
1340 .max_transition_latency = 10000000,
1341 .owner = THIS_MODULE,
1344 static void cpufreq_interactive_nop_timer(unsigned long data)
1348 static int __init cpufreq_interactive_init(void)
1351 struct cpufreq_interactive_cpuinfo *pcpu;
1352 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1354 /* Initalize per-cpu timers */
1355 for_each_possible_cpu(i) {
1356 pcpu = &per_cpu(cpuinfo, i);
1357 init_timer_deferrable(&pcpu->cpu_timer);
1358 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1359 pcpu->cpu_timer.data = i;
1360 init_timer(&pcpu->cpu_slack_timer);
1361 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1362 spin_lock_init(&pcpu->load_lock);
1363 spin_lock_init(&pcpu->target_freq_lock);
1364 init_rwsem(&pcpu->enable_sem);
1367 spin_lock_init(&speedchange_cpumask_lock);
1368 mutex_init(&gov_lock);
1370 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1372 if (IS_ERR(speedchange_task))
1373 return PTR_ERR(speedchange_task);
1375 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1376 get_task_struct(speedchange_task);
1378 /* NB: wake up so the thread does not look hung to the freezer */
1379 wake_up_process(speedchange_task);
1381 return cpufreq_register_governor(&cpufreq_gov_interactive);
1384 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1385 fs_initcall(cpufreq_interactive_init);
1387 module_init(cpufreq_interactive_init);
1390 static void __exit cpufreq_interactive_exit(void)
1392 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1393 kthread_stop(speedchange_task);
1394 put_task_struct(speedchange_task);
1397 module_exit(cpufreq_interactive_exit);
1399 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1400 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1401 "Latency sensitive workloads");
1402 MODULE_LICENSE("GPL");