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 u64 pol_floor_val_time; /* policy floor_validate_time */
51 u64 loc_floor_val_time; /* per-cpu floor_validate_time */
52 u64 pol_hispeed_val_time; /* policy hispeed_validate_time */
53 u64 loc_hispeed_val_time; /* per-cpu hispeed_validate_time */
54 struct rw_semaphore enable_sem;
58 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
60 /* realtime thread handles frequency scaling */
61 static struct task_struct *speedchange_task;
62 static cpumask_t speedchange_cpumask;
63 static spinlock_t speedchange_cpumask_lock;
64 static struct mutex gov_lock;
66 /* Target load. Lower values result in higher CPU speeds. */
67 #define DEFAULT_TARGET_LOAD 90
68 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
70 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
71 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
72 static unsigned int default_above_hispeed_delay[] = {
73 DEFAULT_ABOVE_HISPEED_DELAY };
75 struct cpufreq_interactive_tunables {
77 /* Hi speed to bump to from lo speed when load burst (default max) */
78 unsigned int hispeed_freq;
79 /* Go to hi speed when CPU load at or above this value. */
80 #define DEFAULT_GO_HISPEED_LOAD 99
81 unsigned long go_hispeed_load;
82 /* Target load. Lower values result in higher CPU speeds. */
83 spinlock_t target_loads_lock;
84 unsigned int *target_loads;
87 * The minimum amount of time to spend at a frequency before we can ramp
90 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
91 unsigned long min_sample_time;
93 * The sample rate of the timer used to increase frequency
95 unsigned long timer_rate;
97 * Wait this long before raising speed above hispeed, by default a
98 * single timer interval.
100 spinlock_t above_hispeed_delay_lock;
101 unsigned int *above_hispeed_delay;
102 int nabove_hispeed_delay;
103 /* Non-zero means indefinite speed boost active */
105 /* Duration of a boot pulse in usecs */
106 int boostpulse_duration_val;
107 /* End time of boost pulse in ktime converted to usecs */
108 u64 boostpulse_endtime;
111 * Max additional time to wait in idle, beyond timer_rate, at speeds
112 * above minimum before wakeup to reduce speed, or -1 if unnecessary.
114 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
119 /* For cases where we have single governor instance for system */
120 static struct cpufreq_interactive_tunables *common_tunables;
122 static struct attribute_group *get_sysfs_attr(void);
124 static void cpufreq_interactive_timer_resched(
125 struct cpufreq_interactive_cpuinfo *pcpu)
127 struct cpufreq_interactive_tunables *tunables =
128 pcpu->policy->governor_data;
129 unsigned long expires;
132 spin_lock_irqsave(&pcpu->load_lock, flags);
134 get_cpu_idle_time(smp_processor_id(),
135 &pcpu->time_in_idle_timestamp,
136 tunables->io_is_busy);
137 pcpu->cputime_speedadj = 0;
138 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
139 expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
140 mod_timer_pinned(&pcpu->cpu_timer, expires);
142 if (tunables->timer_slack_val >= 0 &&
143 pcpu->target_freq > pcpu->policy->min) {
144 expires += usecs_to_jiffies(tunables->timer_slack_val);
145 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
148 spin_unlock_irqrestore(&pcpu->load_lock, flags);
151 /* The caller shall take enable_sem write semaphore to avoid any timer race.
152 * The cpu_timer and cpu_slack_timer must be deactivated when calling this
155 static void cpufreq_interactive_timer_start(
156 struct cpufreq_interactive_tunables *tunables, int cpu)
158 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
159 unsigned long expires = jiffies +
160 usecs_to_jiffies(tunables->timer_rate);
163 pcpu->cpu_timer.expires = expires;
164 add_timer_on(&pcpu->cpu_timer, cpu);
165 if (tunables->timer_slack_val >= 0 &&
166 pcpu->target_freq > pcpu->policy->min) {
167 expires += usecs_to_jiffies(tunables->timer_slack_val);
168 pcpu->cpu_slack_timer.expires = expires;
169 add_timer_on(&pcpu->cpu_slack_timer, cpu);
172 spin_lock_irqsave(&pcpu->load_lock, flags);
174 get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
175 tunables->io_is_busy);
176 pcpu->cputime_speedadj = 0;
177 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
178 spin_unlock_irqrestore(&pcpu->load_lock, flags);
181 static unsigned int freq_to_above_hispeed_delay(
182 struct cpufreq_interactive_tunables *tunables,
189 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
191 for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
192 freq >= tunables->above_hispeed_delay[i+1]; i += 2)
195 ret = tunables->above_hispeed_delay[i];
196 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
200 static unsigned int freq_to_targetload(
201 struct cpufreq_interactive_tunables *tunables, unsigned int freq)
207 spin_lock_irqsave(&tunables->target_loads_lock, flags);
209 for (i = 0; i < tunables->ntarget_loads - 1 &&
210 freq >= tunables->target_loads[i+1]; i += 2)
213 ret = tunables->target_loads[i];
214 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
219 * If increasing frequencies never map to a lower target load then
220 * choose_freq() will find the minimum frequency that does not exceed its
221 * target load given the current load.
223 static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
224 unsigned int loadadjfreq)
226 unsigned int freq = pcpu->policy->cur;
227 unsigned int prevfreq, freqmin, freqmax;
236 tl = freq_to_targetload(pcpu->policy->governor_data, freq);
239 * Find the lowest frequency where the computed load is less
240 * than or equal to the target load.
243 if (cpufreq_frequency_table_target(
244 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
245 CPUFREQ_RELATION_L, &index))
247 freq = pcpu->freq_table[index].frequency;
249 if (freq > prevfreq) {
250 /* The previous frequency is too low. */
253 if (freq >= freqmax) {
255 * Find the highest frequency that is less
258 if (cpufreq_frequency_table_target(
259 pcpu->policy, pcpu->freq_table,
260 freqmax - 1, CPUFREQ_RELATION_H,
263 freq = pcpu->freq_table[index].frequency;
265 if (freq == freqmin) {
267 * The first frequency below freqmax
268 * has already been found to be too
269 * low. freqmax is the lowest speed
270 * we found that is fast enough.
276 } else if (freq < prevfreq) {
277 /* The previous frequency is high enough. */
280 if (freq <= freqmin) {
282 * Find the lowest frequency that is higher
285 if (cpufreq_frequency_table_target(
286 pcpu->policy, pcpu->freq_table,
287 freqmin + 1, CPUFREQ_RELATION_L,
290 freq = pcpu->freq_table[index].frequency;
293 * If freqmax is the first frequency above
294 * freqmin then we have already found that
295 * this speed is fast enough.
302 /* If same frequency chosen as previous then done. */
303 } while (freq != prevfreq);
308 static u64 update_load(int cpu)
310 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
311 struct cpufreq_interactive_tunables *tunables =
312 pcpu->policy->governor_data;
315 unsigned int delta_idle;
316 unsigned int delta_time;
319 now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
320 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
321 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
323 if (delta_time <= delta_idle)
326 active_time = delta_time - delta_idle;
328 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
330 pcpu->time_in_idle = now_idle;
331 pcpu->time_in_idle_timestamp = now;
335 static void cpufreq_interactive_timer(unsigned long data)
338 unsigned int delta_time;
339 u64 cputime_speedadj;
341 struct cpufreq_interactive_cpuinfo *pcpu =
342 &per_cpu(cpuinfo, data);
343 struct cpufreq_interactive_tunables *tunables =
344 pcpu->policy->governor_data;
345 unsigned int new_freq;
346 unsigned int loadadjfreq;
351 if (!down_read_trylock(&pcpu->enable_sem))
353 if (!pcpu->governor_enabled)
356 spin_lock_irqsave(&pcpu->load_lock, flags);
357 now = update_load(data);
358 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
359 cputime_speedadj = pcpu->cputime_speedadj;
360 spin_unlock_irqrestore(&pcpu->load_lock, flags);
362 if (WARN_ON_ONCE(!delta_time))
365 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
366 do_div(cputime_speedadj, delta_time);
367 loadadjfreq = (unsigned int)cputime_speedadj * 100;
368 cpu_load = loadadjfreq / pcpu->policy->cur;
369 tunables->boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
371 if (cpu_load >= tunables->go_hispeed_load || tunables->boosted) {
372 if (pcpu->policy->cur < tunables->hispeed_freq) {
373 new_freq = tunables->hispeed_freq;
375 new_freq = choose_freq(pcpu, loadadjfreq);
377 if (new_freq < tunables->hispeed_freq)
378 new_freq = tunables->hispeed_freq;
381 new_freq = choose_freq(pcpu, loadadjfreq);
382 if (new_freq > tunables->hispeed_freq &&
383 pcpu->policy->cur < tunables->hispeed_freq)
384 new_freq = tunables->hispeed_freq;
387 if (pcpu->policy->cur >= tunables->hispeed_freq &&
388 new_freq > pcpu->policy->cur &&
389 now - pcpu->pol_hispeed_val_time <
390 freq_to_above_hispeed_delay(tunables, pcpu->policy->cur)) {
391 trace_cpufreq_interactive_notyet(
392 data, cpu_load, pcpu->target_freq,
393 pcpu->policy->cur, new_freq);
394 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
398 pcpu->loc_hispeed_val_time = now;
400 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
401 new_freq, CPUFREQ_RELATION_L,
403 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
407 new_freq = pcpu->freq_table[index].frequency;
410 * Do not scale below floor_freq unless we have been at or above the
411 * floor frequency for the minimum sample time since last validated.
413 max_fvtime = max(pcpu->pol_floor_val_time, pcpu->loc_floor_val_time);
414 if (new_freq < pcpu->floor_freq &&
415 pcpu->target_freq >= pcpu->policy->cur) {
416 if (now - max_fvtime < tunables->min_sample_time) {
417 trace_cpufreq_interactive_notyet(
418 data, cpu_load, pcpu->target_freq,
419 pcpu->policy->cur, new_freq);
420 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
426 * Update the timestamp for checking whether speed has been held at
427 * or above the selected frequency for a minimum of min_sample_time,
428 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
429 * allow the speed to drop as soon as the boostpulse duration expires
430 * (or the indefinite boost is turned off).
433 if (!tunables->boosted || new_freq > tunables->hispeed_freq) {
434 pcpu->floor_freq = new_freq;
435 if (pcpu->target_freq >= pcpu->policy->cur ||
436 new_freq >= pcpu->policy->cur)
437 pcpu->loc_floor_val_time = now;
440 if (pcpu->target_freq == new_freq &&
441 pcpu->target_freq <= pcpu->policy->cur) {
442 trace_cpufreq_interactive_already(
443 data, cpu_load, pcpu->target_freq,
444 pcpu->policy->cur, new_freq);
445 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
449 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
450 pcpu->policy->cur, new_freq);
452 pcpu->target_freq = new_freq;
453 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
454 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
455 cpumask_set_cpu(data, &speedchange_cpumask);
456 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
457 wake_up_process(speedchange_task);
460 if (!timer_pending(&pcpu->cpu_timer))
461 cpufreq_interactive_timer_resched(pcpu);
464 up_read(&pcpu->enable_sem);
468 static void cpufreq_interactive_idle_end(void)
470 struct cpufreq_interactive_cpuinfo *pcpu =
471 &per_cpu(cpuinfo, smp_processor_id());
473 if (!down_read_trylock(&pcpu->enable_sem))
475 if (!pcpu->governor_enabled) {
476 up_read(&pcpu->enable_sem);
480 /* Arm the timer for 1-2 ticks later if not already. */
481 if (!timer_pending(&pcpu->cpu_timer)) {
482 cpufreq_interactive_timer_resched(pcpu);
483 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
484 del_timer(&pcpu->cpu_timer);
485 del_timer(&pcpu->cpu_slack_timer);
486 cpufreq_interactive_timer(smp_processor_id());
489 up_read(&pcpu->enable_sem);
492 static int cpufreq_interactive_speedchange_task(void *data)
497 struct cpufreq_interactive_cpuinfo *pcpu;
500 set_current_state(TASK_INTERRUPTIBLE);
501 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
503 if (cpumask_empty(&speedchange_cpumask)) {
504 spin_unlock_irqrestore(&speedchange_cpumask_lock,
508 if (kthread_should_stop())
511 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
514 set_current_state(TASK_RUNNING);
515 tmp_mask = speedchange_cpumask;
516 cpumask_clear(&speedchange_cpumask);
517 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
519 for_each_cpu(cpu, &tmp_mask) {
521 unsigned int max_freq = 0;
522 struct cpufreq_interactive_cpuinfo *pjcpu;
523 u64 hvt = ~0ULL, fvt = 0;
525 pcpu = &per_cpu(cpuinfo, cpu);
526 if (!down_read_trylock(&pcpu->enable_sem))
528 if (!pcpu->governor_enabled) {
529 up_read(&pcpu->enable_sem);
533 for_each_cpu(j, pcpu->policy->cpus) {
534 pjcpu = &per_cpu(cpuinfo, j);
536 fvt = max(fvt, pjcpu->loc_floor_val_time);
537 if (pjcpu->target_freq > max_freq) {
538 max_freq = pjcpu->target_freq;
539 hvt = pjcpu->loc_hispeed_val_time;
540 } else if (pjcpu->target_freq == max_freq) {
541 hvt = min(hvt, pjcpu->loc_hispeed_val_time);
544 for_each_cpu(j, pcpu->policy->cpus) {
545 pjcpu = &per_cpu(cpuinfo, j);
546 pjcpu->pol_floor_val_time = fvt;
549 if (max_freq != pcpu->policy->cur) {
550 __cpufreq_driver_target(pcpu->policy,
553 for_each_cpu(j, pcpu->policy->cpus) {
554 pjcpu = &per_cpu(cpuinfo, j);
555 pjcpu->pol_hispeed_val_time = hvt;
558 trace_cpufreq_interactive_setspeed(cpu,
562 up_read(&pcpu->enable_sem);
569 static void cpufreq_interactive_boost(struct cpufreq_interactive_tunables *tunables)
573 unsigned long flags[2];
574 struct cpufreq_interactive_cpuinfo *pcpu;
576 tunables->boosted = true;
578 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
580 for_each_online_cpu(i) {
581 pcpu = &per_cpu(cpuinfo, i);
582 if (tunables != pcpu->policy->governor_data)
585 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
586 if (pcpu->target_freq < tunables->hispeed_freq) {
587 pcpu->target_freq = tunables->hispeed_freq;
588 cpumask_set_cpu(i, &speedchange_cpumask);
589 pcpu->pol_hispeed_val_time =
590 ktime_to_us(ktime_get());
593 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
596 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
599 wake_up_process(speedchange_task);
602 static int cpufreq_interactive_notifier(
603 struct notifier_block *nb, unsigned long val, void *data)
605 struct cpufreq_freqs *freq = data;
606 struct cpufreq_interactive_cpuinfo *pcpu;
610 if (val == CPUFREQ_POSTCHANGE) {
611 pcpu = &per_cpu(cpuinfo, freq->cpu);
612 if (!down_read_trylock(&pcpu->enable_sem))
614 if (!pcpu->governor_enabled) {
615 up_read(&pcpu->enable_sem);
619 for_each_cpu(cpu, pcpu->policy->cpus) {
620 struct cpufreq_interactive_cpuinfo *pjcpu =
621 &per_cpu(cpuinfo, cpu);
622 if (cpu != freq->cpu) {
623 if (!down_read_trylock(&pjcpu->enable_sem))
625 if (!pjcpu->governor_enabled) {
626 up_read(&pjcpu->enable_sem);
630 spin_lock_irqsave(&pjcpu->load_lock, flags);
632 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
633 if (cpu != freq->cpu)
634 up_read(&pjcpu->enable_sem);
637 up_read(&pcpu->enable_sem);
642 static struct notifier_block cpufreq_notifier_block = {
643 .notifier_call = cpufreq_interactive_notifier,
646 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
651 unsigned int *tokenized_data;
655 while ((cp = strpbrk(cp + 1, " :")))
658 if (!(ntokens & 0x1))
661 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
662 if (!tokenized_data) {
669 while (i < ntokens) {
670 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
673 cp = strpbrk(cp, " :");
682 *num_tokens = ntokens;
683 return tokenized_data;
686 kfree(tokenized_data);
691 static ssize_t show_target_loads(
692 struct cpufreq_interactive_tunables *tunables,
699 spin_lock_irqsave(&tunables->target_loads_lock, flags);
701 for (i = 0; i < tunables->ntarget_loads; i++)
702 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
703 i & 0x1 ? ":" : " ");
705 sprintf(buf + ret - 1, "\n");
706 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
710 static ssize_t store_target_loads(
711 struct cpufreq_interactive_tunables *tunables,
712 const char *buf, size_t count)
715 unsigned int *new_target_loads = NULL;
718 new_target_loads = get_tokenized_data(buf, &ntokens);
719 if (IS_ERR(new_target_loads))
720 return PTR_RET(new_target_loads);
722 spin_lock_irqsave(&tunables->target_loads_lock, flags);
723 if (tunables->target_loads != default_target_loads)
724 kfree(tunables->target_loads);
725 tunables->target_loads = new_target_loads;
726 tunables->ntarget_loads = ntokens;
727 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
731 static ssize_t show_above_hispeed_delay(
732 struct cpufreq_interactive_tunables *tunables, char *buf)
738 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
740 for (i = 0; i < tunables->nabove_hispeed_delay; i++)
741 ret += sprintf(buf + ret, "%u%s",
742 tunables->above_hispeed_delay[i],
743 i & 0x1 ? ":" : " ");
745 sprintf(buf + ret - 1, "\n");
746 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
750 static ssize_t store_above_hispeed_delay(
751 struct cpufreq_interactive_tunables *tunables,
752 const char *buf, size_t count)
755 unsigned int *new_above_hispeed_delay = NULL;
758 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
759 if (IS_ERR(new_above_hispeed_delay))
760 return PTR_RET(new_above_hispeed_delay);
762 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
763 if (tunables->above_hispeed_delay != default_above_hispeed_delay)
764 kfree(tunables->above_hispeed_delay);
765 tunables->above_hispeed_delay = new_above_hispeed_delay;
766 tunables->nabove_hispeed_delay = ntokens;
767 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
772 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
775 return sprintf(buf, "%u\n", tunables->hispeed_freq);
778 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
779 const char *buf, size_t count)
782 long unsigned int val;
784 ret = kstrtoul(buf, 0, &val);
787 tunables->hispeed_freq = val;
791 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
792 *tunables, char *buf)
794 return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
797 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
798 *tunables, const char *buf, size_t count)
803 ret = kstrtoul(buf, 0, &val);
806 tunables->go_hispeed_load = val;
810 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
811 *tunables, char *buf)
813 return sprintf(buf, "%lu\n", tunables->min_sample_time);
816 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
817 *tunables, const char *buf, size_t count)
822 ret = kstrtoul(buf, 0, &val);
825 tunables->min_sample_time = val;
829 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
832 return sprintf(buf, "%lu\n", tunables->timer_rate);
835 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
836 const char *buf, size_t count)
839 unsigned long val, val_round;
841 ret = kstrtoul(buf, 0, &val);
845 val_round = jiffies_to_usecs(usecs_to_jiffies(val));
846 if (val != val_round)
847 pr_warn("timer_rate not aligned to jiffy. Rounded up to %lu\n",
850 tunables->timer_rate = val_round;
854 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
857 return sprintf(buf, "%d\n", tunables->timer_slack_val);
860 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
861 const char *buf, size_t count)
866 ret = kstrtol(buf, 10, &val);
870 tunables->timer_slack_val = val;
874 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
877 return sprintf(buf, "%d\n", tunables->boost_val);
880 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
881 const char *buf, size_t count)
886 ret = kstrtoul(buf, 0, &val);
890 tunables->boost_val = val;
892 if (tunables->boost_val) {
893 trace_cpufreq_interactive_boost("on");
894 if (!tunables->boosted)
895 cpufreq_interactive_boost(tunables);
897 tunables->boostpulse_endtime = ktime_to_us(ktime_get());
898 trace_cpufreq_interactive_unboost("off");
904 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
905 const char *buf, size_t count)
910 ret = kstrtoul(buf, 0, &val);
914 tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
915 tunables->boostpulse_duration_val;
916 trace_cpufreq_interactive_boost("pulse");
917 if (!tunables->boosted)
918 cpufreq_interactive_boost(tunables);
922 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
923 *tunables, char *buf)
925 return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
928 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
929 *tunables, const char *buf, size_t count)
934 ret = kstrtoul(buf, 0, &val);
938 tunables->boostpulse_duration_val = val;
942 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
945 return sprintf(buf, "%u\n", tunables->io_is_busy);
948 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
949 const char *buf, size_t count)
954 ret = kstrtoul(buf, 0, &val);
957 tunables->io_is_busy = val;
962 * Create show/store routines
963 * - sys: One governor instance for complete SYSTEM
964 * - pol: One governor instance per struct cpufreq_policy
966 #define show_gov_pol_sys(file_name) \
967 static ssize_t show_##file_name##_gov_sys \
968 (struct kobject *kobj, struct attribute *attr, char *buf) \
970 return show_##file_name(common_tunables, buf); \
973 static ssize_t show_##file_name##_gov_pol \
974 (struct cpufreq_policy *policy, char *buf) \
976 return show_##file_name(policy->governor_data, buf); \
979 #define store_gov_pol_sys(file_name) \
980 static ssize_t store_##file_name##_gov_sys \
981 (struct kobject *kobj, struct attribute *attr, const char *buf, \
984 return store_##file_name(common_tunables, buf, count); \
987 static ssize_t store_##file_name##_gov_pol \
988 (struct cpufreq_policy *policy, const char *buf, size_t count) \
990 return store_##file_name(policy->governor_data, buf, count); \
993 #define show_store_gov_pol_sys(file_name) \
994 show_gov_pol_sys(file_name); \
995 store_gov_pol_sys(file_name)
997 show_store_gov_pol_sys(target_loads);
998 show_store_gov_pol_sys(above_hispeed_delay);
999 show_store_gov_pol_sys(hispeed_freq);
1000 show_store_gov_pol_sys(go_hispeed_load);
1001 show_store_gov_pol_sys(min_sample_time);
1002 show_store_gov_pol_sys(timer_rate);
1003 show_store_gov_pol_sys(timer_slack);
1004 show_store_gov_pol_sys(boost);
1005 store_gov_pol_sys(boostpulse);
1006 show_store_gov_pol_sys(boostpulse_duration);
1007 show_store_gov_pol_sys(io_is_busy);
1009 #define gov_sys_attr_rw(_name) \
1010 static struct global_attr _name##_gov_sys = \
1011 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1013 #define gov_pol_attr_rw(_name) \
1014 static struct freq_attr _name##_gov_pol = \
1015 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1017 #define gov_sys_pol_attr_rw(_name) \
1018 gov_sys_attr_rw(_name); \
1019 gov_pol_attr_rw(_name)
1021 gov_sys_pol_attr_rw(target_loads);
1022 gov_sys_pol_attr_rw(above_hispeed_delay);
1023 gov_sys_pol_attr_rw(hispeed_freq);
1024 gov_sys_pol_attr_rw(go_hispeed_load);
1025 gov_sys_pol_attr_rw(min_sample_time);
1026 gov_sys_pol_attr_rw(timer_rate);
1027 gov_sys_pol_attr_rw(timer_slack);
1028 gov_sys_pol_attr_rw(boost);
1029 gov_sys_pol_attr_rw(boostpulse_duration);
1030 gov_sys_pol_attr_rw(io_is_busy);
1032 static struct global_attr boostpulse_gov_sys =
1033 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1035 static struct freq_attr boostpulse_gov_pol =
1036 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1038 /* One Governor instance for entire system */
1039 static struct attribute *interactive_attributes_gov_sys[] = {
1040 &target_loads_gov_sys.attr,
1041 &above_hispeed_delay_gov_sys.attr,
1042 &hispeed_freq_gov_sys.attr,
1043 &go_hispeed_load_gov_sys.attr,
1044 &min_sample_time_gov_sys.attr,
1045 &timer_rate_gov_sys.attr,
1046 &timer_slack_gov_sys.attr,
1047 &boost_gov_sys.attr,
1048 &boostpulse_gov_sys.attr,
1049 &boostpulse_duration_gov_sys.attr,
1050 &io_is_busy_gov_sys.attr,
1054 static struct attribute_group interactive_attr_group_gov_sys = {
1055 .attrs = interactive_attributes_gov_sys,
1056 .name = "interactive",
1059 /* Per policy governor instance */
1060 static struct attribute *interactive_attributes_gov_pol[] = {
1061 &target_loads_gov_pol.attr,
1062 &above_hispeed_delay_gov_pol.attr,
1063 &hispeed_freq_gov_pol.attr,
1064 &go_hispeed_load_gov_pol.attr,
1065 &min_sample_time_gov_pol.attr,
1066 &timer_rate_gov_pol.attr,
1067 &timer_slack_gov_pol.attr,
1068 &boost_gov_pol.attr,
1069 &boostpulse_gov_pol.attr,
1070 &boostpulse_duration_gov_pol.attr,
1071 &io_is_busy_gov_pol.attr,
1075 static struct attribute_group interactive_attr_group_gov_pol = {
1076 .attrs = interactive_attributes_gov_pol,
1077 .name = "interactive",
1080 static struct attribute_group *get_sysfs_attr(void)
1082 if (have_governor_per_policy())
1083 return &interactive_attr_group_gov_pol;
1085 return &interactive_attr_group_gov_sys;
1088 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1092 if (val == IDLE_END)
1093 cpufreq_interactive_idle_end();
1098 static struct notifier_block cpufreq_interactive_idle_nb = {
1099 .notifier_call = cpufreq_interactive_idle_notifier,
1102 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1107 struct cpufreq_interactive_cpuinfo *pcpu;
1108 struct cpufreq_frequency_table *freq_table;
1109 struct cpufreq_interactive_tunables *tunables;
1110 unsigned long flags;
1112 if (have_governor_per_policy())
1113 tunables = policy->governor_data;
1115 tunables = common_tunables;
1117 WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1120 case CPUFREQ_GOV_POLICY_INIT:
1121 if (have_governor_per_policy()) {
1123 } else if (tunables) {
1124 tunables->usage_count++;
1125 policy->governor_data = tunables;
1129 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1131 pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1135 tunables->usage_count = 1;
1136 tunables->above_hispeed_delay = default_above_hispeed_delay;
1137 tunables->nabove_hispeed_delay =
1138 ARRAY_SIZE(default_above_hispeed_delay);
1139 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1140 tunables->target_loads = default_target_loads;
1141 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1142 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1143 tunables->timer_rate = DEFAULT_TIMER_RATE;
1144 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1145 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1147 spin_lock_init(&tunables->target_loads_lock);
1148 spin_lock_init(&tunables->above_hispeed_delay_lock);
1150 policy->governor_data = tunables;
1151 if (!have_governor_per_policy()) {
1152 common_tunables = tunables;
1155 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1159 policy->governor_data = NULL;
1160 if (!have_governor_per_policy()) {
1161 common_tunables = NULL;
1166 if (!policy->governor->initialized) {
1167 idle_notifier_register(&cpufreq_interactive_idle_nb);
1168 cpufreq_register_notifier(&cpufreq_notifier_block,
1169 CPUFREQ_TRANSITION_NOTIFIER);
1174 case CPUFREQ_GOV_POLICY_EXIT:
1175 if (!--tunables->usage_count) {
1176 if (policy->governor->initialized == 1) {
1177 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1178 CPUFREQ_TRANSITION_NOTIFIER);
1179 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1182 sysfs_remove_group(get_governor_parent_kobj(policy),
1187 common_tunables = NULL;
1190 policy->governor_data = NULL;
1193 case CPUFREQ_GOV_START:
1194 mutex_lock(&gov_lock);
1196 freq_table = cpufreq_frequency_get_table(policy->cpu);
1197 if (!tunables->hispeed_freq)
1198 tunables->hispeed_freq = policy->max;
1200 for_each_cpu(j, policy->cpus) {
1201 pcpu = &per_cpu(cpuinfo, j);
1202 pcpu->policy = policy;
1203 pcpu->target_freq = policy->cur;
1204 pcpu->freq_table = freq_table;
1205 pcpu->floor_freq = pcpu->target_freq;
1206 pcpu->pol_floor_val_time =
1207 ktime_to_us(ktime_get());
1208 pcpu->loc_floor_val_time = pcpu->pol_floor_val_time;
1209 pcpu->pol_hispeed_val_time = pcpu->pol_floor_val_time;
1210 pcpu->loc_hispeed_val_time = pcpu->pol_floor_val_time;
1211 down_write(&pcpu->enable_sem);
1212 del_timer_sync(&pcpu->cpu_timer);
1213 del_timer_sync(&pcpu->cpu_slack_timer);
1214 cpufreq_interactive_timer_start(tunables, j);
1215 pcpu->governor_enabled = 1;
1216 up_write(&pcpu->enable_sem);
1219 mutex_unlock(&gov_lock);
1222 case CPUFREQ_GOV_STOP:
1223 mutex_lock(&gov_lock);
1224 for_each_cpu(j, policy->cpus) {
1225 pcpu = &per_cpu(cpuinfo, j);
1226 down_write(&pcpu->enable_sem);
1227 pcpu->governor_enabled = 0;
1228 del_timer_sync(&pcpu->cpu_timer);
1229 del_timer_sync(&pcpu->cpu_slack_timer);
1230 up_write(&pcpu->enable_sem);
1233 mutex_unlock(&gov_lock);
1236 case CPUFREQ_GOV_LIMITS:
1237 if (policy->max < policy->cur)
1238 __cpufreq_driver_target(policy,
1239 policy->max, CPUFREQ_RELATION_H);
1240 else if (policy->min > policy->cur)
1241 __cpufreq_driver_target(policy,
1242 policy->min, CPUFREQ_RELATION_L);
1243 for_each_cpu(j, policy->cpus) {
1244 pcpu = &per_cpu(cpuinfo, j);
1246 down_read(&pcpu->enable_sem);
1247 if (pcpu->governor_enabled == 0) {
1248 up_read(&pcpu->enable_sem);
1252 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
1253 if (policy->max < pcpu->target_freq)
1254 pcpu->target_freq = policy->max;
1255 else if (policy->min > pcpu->target_freq)
1256 pcpu->target_freq = policy->min;
1258 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
1259 up_read(&pcpu->enable_sem);
1266 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1269 struct cpufreq_governor cpufreq_gov_interactive = {
1270 .name = "interactive",
1271 .governor = cpufreq_governor_interactive,
1272 .max_transition_latency = 10000000,
1273 .owner = THIS_MODULE,
1276 static void cpufreq_interactive_nop_timer(unsigned long data)
1280 static int __init cpufreq_interactive_init(void)
1283 struct cpufreq_interactive_cpuinfo *pcpu;
1284 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1286 /* Initalize per-cpu timers */
1287 for_each_possible_cpu(i) {
1288 pcpu = &per_cpu(cpuinfo, i);
1289 init_timer_deferrable(&pcpu->cpu_timer);
1290 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1291 pcpu->cpu_timer.data = i;
1292 init_timer(&pcpu->cpu_slack_timer);
1293 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1294 spin_lock_init(&pcpu->load_lock);
1295 spin_lock_init(&pcpu->target_freq_lock);
1296 init_rwsem(&pcpu->enable_sem);
1299 spin_lock_init(&speedchange_cpumask_lock);
1300 mutex_init(&gov_lock);
1302 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1304 if (IS_ERR(speedchange_task))
1305 return PTR_ERR(speedchange_task);
1307 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1308 get_task_struct(speedchange_task);
1310 /* NB: wake up so the thread does not look hung to the freezer */
1311 wake_up_process(speedchange_task);
1313 return cpufreq_register_governor(&cpufreq_gov_interactive);
1316 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1317 fs_initcall(cpufreq_interactive_init);
1319 module_init(cpufreq_interactive_init);
1322 static void __exit cpufreq_interactive_exit(void)
1324 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1325 kthread_stop(speedchange_task);
1326 put_task_struct(speedchange_task);
1329 module_exit(cpufreq_interactive_exit);
1331 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1332 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1333 "Latency sensitive workloads");
1334 MODULE_LICENSE("GPL");