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)
120 * HACK: FIXME: Bring back cpufreq_{get,put}_global_kobject()
121 * definition removed by upstream commit 8eec1020f0c0 "cpufreq:
122 * create cpu/cpufreq at boot time" to fix build failures.
124 static int cpufreq_global_kobject_usage;
126 int cpufreq_get_global_kobject(void)
128 if (!cpufreq_global_kobject_usage++)
129 return kobject_add(cpufreq_global_kobject,
130 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
135 void cpufreq_put_global_kobject(void)
137 if (!--cpufreq_global_kobject_usage)
138 kobject_del(cpufreq_global_kobject);
141 /* For cases where we have single governor instance for system */
142 static struct cpufreq_interactive_tunables *common_tunables;
144 static struct attribute_group *get_sysfs_attr(void);
146 static void cpufreq_interactive_timer_resched(
147 struct cpufreq_interactive_cpuinfo *pcpu)
149 struct cpufreq_interactive_tunables *tunables =
150 pcpu->policy->governor_data;
151 unsigned long expires;
154 spin_lock_irqsave(&pcpu->load_lock, flags);
156 get_cpu_idle_time(smp_processor_id(),
157 &pcpu->time_in_idle_timestamp,
158 tunables->io_is_busy);
159 pcpu->cputime_speedadj = 0;
160 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
161 expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
162 mod_timer_pinned(&pcpu->cpu_timer, expires);
164 if (tunables->timer_slack_val >= 0 &&
165 pcpu->target_freq > pcpu->policy->min) {
166 expires += usecs_to_jiffies(tunables->timer_slack_val);
167 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
170 spin_unlock_irqrestore(&pcpu->load_lock, flags);
173 /* The caller shall take enable_sem write semaphore to avoid any timer race.
174 * The cpu_timer and cpu_slack_timer must be deactivated when calling this
177 static void cpufreq_interactive_timer_start(
178 struct cpufreq_interactive_tunables *tunables, int cpu)
180 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
181 unsigned long expires = jiffies +
182 usecs_to_jiffies(tunables->timer_rate);
185 pcpu->cpu_timer.expires = expires;
186 add_timer_on(&pcpu->cpu_timer, cpu);
187 if (tunables->timer_slack_val >= 0 &&
188 pcpu->target_freq > pcpu->policy->min) {
189 expires += usecs_to_jiffies(tunables->timer_slack_val);
190 pcpu->cpu_slack_timer.expires = expires;
191 add_timer_on(&pcpu->cpu_slack_timer, cpu);
194 spin_lock_irqsave(&pcpu->load_lock, flags);
196 get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
197 tunables->io_is_busy);
198 pcpu->cputime_speedadj = 0;
199 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
200 spin_unlock_irqrestore(&pcpu->load_lock, flags);
203 static unsigned int freq_to_above_hispeed_delay(
204 struct cpufreq_interactive_tunables *tunables,
211 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
213 for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
214 freq >= tunables->above_hispeed_delay[i+1]; i += 2)
217 ret = tunables->above_hispeed_delay[i];
218 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
222 static unsigned int freq_to_targetload(
223 struct cpufreq_interactive_tunables *tunables, unsigned int freq)
229 spin_lock_irqsave(&tunables->target_loads_lock, flags);
231 for (i = 0; i < tunables->ntarget_loads - 1 &&
232 freq >= tunables->target_loads[i+1]; i += 2)
235 ret = tunables->target_loads[i];
236 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
241 * If increasing frequencies never map to a lower target load then
242 * choose_freq() will find the minimum frequency that does not exceed its
243 * target load given the current load.
245 static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
246 unsigned int loadadjfreq)
248 unsigned int freq = pcpu->policy->cur;
249 unsigned int prevfreq, freqmin, freqmax;
258 tl = freq_to_targetload(pcpu->policy->governor_data, freq);
261 * Find the lowest frequency where the computed load is less
262 * than or equal to the target load.
265 if (cpufreq_frequency_table_target(
266 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
267 CPUFREQ_RELATION_L, &index))
269 freq = pcpu->freq_table[index].frequency;
271 if (freq > prevfreq) {
272 /* The previous frequency is too low. */
275 if (freq >= freqmax) {
277 * Find the highest frequency that is less
280 if (cpufreq_frequency_table_target(
281 pcpu->policy, pcpu->freq_table,
282 freqmax - 1, CPUFREQ_RELATION_H,
285 freq = pcpu->freq_table[index].frequency;
287 if (freq == freqmin) {
289 * The first frequency below freqmax
290 * has already been found to be too
291 * low. freqmax is the lowest speed
292 * we found that is fast enough.
298 } else if (freq < prevfreq) {
299 /* The previous frequency is high enough. */
302 if (freq <= freqmin) {
304 * Find the lowest frequency that is higher
307 if (cpufreq_frequency_table_target(
308 pcpu->policy, pcpu->freq_table,
309 freqmin + 1, CPUFREQ_RELATION_L,
312 freq = pcpu->freq_table[index].frequency;
315 * If freqmax is the first frequency above
316 * freqmin then we have already found that
317 * this speed is fast enough.
324 /* If same frequency chosen as previous then done. */
325 } while (freq != prevfreq);
330 static u64 update_load(int cpu)
332 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
333 struct cpufreq_interactive_tunables *tunables =
334 pcpu->policy->governor_data;
337 unsigned int delta_idle;
338 unsigned int delta_time;
341 now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
342 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
343 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
345 if (delta_time <= delta_idle)
348 active_time = delta_time - delta_idle;
350 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
352 pcpu->time_in_idle = now_idle;
353 pcpu->time_in_idle_timestamp = now;
357 static void cpufreq_interactive_timer(unsigned long data)
360 unsigned int delta_time;
361 u64 cputime_speedadj;
363 struct cpufreq_interactive_cpuinfo *pcpu =
364 &per_cpu(cpuinfo, data);
365 struct cpufreq_interactive_tunables *tunables =
366 pcpu->policy->governor_data;
367 unsigned int new_freq;
368 unsigned int loadadjfreq;
373 if (!down_read_trylock(&pcpu->enable_sem))
375 if (!pcpu->governor_enabled)
378 spin_lock_irqsave(&pcpu->load_lock, flags);
379 now = update_load(data);
380 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
381 cputime_speedadj = pcpu->cputime_speedadj;
382 spin_unlock_irqrestore(&pcpu->load_lock, flags);
384 if (WARN_ON_ONCE(!delta_time))
387 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
388 do_div(cputime_speedadj, delta_time);
389 loadadjfreq = (unsigned int)cputime_speedadj * 100;
390 cpu_load = loadadjfreq / pcpu->policy->cur;
391 tunables->boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
393 if (cpu_load >= tunables->go_hispeed_load || tunables->boosted) {
394 if (pcpu->policy->cur < tunables->hispeed_freq) {
395 new_freq = tunables->hispeed_freq;
397 new_freq = choose_freq(pcpu, loadadjfreq);
399 if (new_freq < tunables->hispeed_freq)
400 new_freq = tunables->hispeed_freq;
403 new_freq = choose_freq(pcpu, loadadjfreq);
404 if (new_freq > tunables->hispeed_freq &&
405 pcpu->policy->cur < tunables->hispeed_freq)
406 new_freq = tunables->hispeed_freq;
409 if (pcpu->policy->cur >= tunables->hispeed_freq &&
410 new_freq > pcpu->policy->cur &&
411 now - pcpu->pol_hispeed_val_time <
412 freq_to_above_hispeed_delay(tunables, pcpu->policy->cur)) {
413 trace_cpufreq_interactive_notyet(
414 data, cpu_load, pcpu->target_freq,
415 pcpu->policy->cur, new_freq);
416 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
420 pcpu->loc_hispeed_val_time = now;
422 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
423 new_freq, CPUFREQ_RELATION_L,
425 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
429 new_freq = pcpu->freq_table[index].frequency;
432 * Do not scale below floor_freq unless we have been at or above the
433 * floor frequency for the minimum sample time since last validated.
435 max_fvtime = max(pcpu->pol_floor_val_time, pcpu->loc_floor_val_time);
436 if (new_freq < pcpu->floor_freq &&
437 pcpu->target_freq >= pcpu->policy->cur) {
438 if (now - max_fvtime < tunables->min_sample_time) {
439 trace_cpufreq_interactive_notyet(
440 data, cpu_load, pcpu->target_freq,
441 pcpu->policy->cur, new_freq);
442 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
448 * Update the timestamp for checking whether speed has been held at
449 * or above the selected frequency for a minimum of min_sample_time,
450 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
451 * allow the speed to drop as soon as the boostpulse duration expires
452 * (or the indefinite boost is turned off).
455 if (!tunables->boosted || new_freq > tunables->hispeed_freq) {
456 pcpu->floor_freq = new_freq;
457 if (pcpu->target_freq >= pcpu->policy->cur ||
458 new_freq >= pcpu->policy->cur)
459 pcpu->loc_floor_val_time = now;
462 if (pcpu->target_freq == new_freq &&
463 pcpu->target_freq <= pcpu->policy->cur) {
464 trace_cpufreq_interactive_already(
465 data, cpu_load, pcpu->target_freq,
466 pcpu->policy->cur, new_freq);
467 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
471 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
472 pcpu->policy->cur, new_freq);
474 pcpu->target_freq = new_freq;
475 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
476 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
477 cpumask_set_cpu(data, &speedchange_cpumask);
478 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
479 wake_up_process(speedchange_task);
482 if (!timer_pending(&pcpu->cpu_timer))
483 cpufreq_interactive_timer_resched(pcpu);
486 up_read(&pcpu->enable_sem);
490 static void cpufreq_interactive_idle_end(void)
492 struct cpufreq_interactive_cpuinfo *pcpu =
493 &per_cpu(cpuinfo, smp_processor_id());
495 if (!down_read_trylock(&pcpu->enable_sem))
497 if (!pcpu->governor_enabled) {
498 up_read(&pcpu->enable_sem);
502 /* Arm the timer for 1-2 ticks later if not already. */
503 if (!timer_pending(&pcpu->cpu_timer)) {
504 cpufreq_interactive_timer_resched(pcpu);
505 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
506 del_timer(&pcpu->cpu_timer);
507 del_timer(&pcpu->cpu_slack_timer);
508 cpufreq_interactive_timer(smp_processor_id());
511 up_read(&pcpu->enable_sem);
514 static void cpufreq_interactive_get_policy_info(struct cpufreq_policy *policy,
515 unsigned int *pmax_freq,
516 u64 *phvt, u64 *pfvt)
518 struct cpufreq_interactive_cpuinfo *pcpu;
519 unsigned int max_freq = 0;
520 u64 hvt = ~0ULL, fvt = 0;
523 for_each_cpu(i, policy->cpus) {
524 pcpu = &per_cpu(cpuinfo, i);
526 fvt = max(fvt, pcpu->loc_floor_val_time);
527 if (pcpu->target_freq > max_freq) {
528 max_freq = pcpu->target_freq;
529 hvt = pcpu->loc_hispeed_val_time;
530 } else if (pcpu->target_freq == max_freq) {
531 hvt = min(hvt, pcpu->loc_hispeed_val_time);
535 *pmax_freq = max_freq;
540 static void cpufreq_interactive_adjust_cpu(unsigned int cpu,
541 struct cpufreq_policy *policy)
543 struct cpufreq_interactive_cpuinfo *pcpu;
545 unsigned int max_freq;
548 cpufreq_interactive_get_policy_info(policy, &max_freq, &hvt, &fvt);
550 for_each_cpu(i, policy->cpus) {
551 pcpu = &per_cpu(cpuinfo, i);
552 pcpu->pol_floor_val_time = fvt;
555 if (max_freq != policy->cur) {
556 __cpufreq_driver_target(policy, max_freq, CPUFREQ_RELATION_H);
557 for_each_cpu(i, policy->cpus) {
558 pcpu = &per_cpu(cpuinfo, i);
559 pcpu->pol_hispeed_val_time = hvt;
563 trace_cpufreq_interactive_setspeed(cpu, max_freq, policy->cur);
566 static int cpufreq_interactive_speedchange_task(void *data)
571 struct cpufreq_interactive_cpuinfo *pcpu;
574 set_current_state(TASK_INTERRUPTIBLE);
575 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
577 if (cpumask_empty(&speedchange_cpumask)) {
578 spin_unlock_irqrestore(&speedchange_cpumask_lock,
582 if (kthread_should_stop())
585 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
588 set_current_state(TASK_RUNNING);
589 tmp_mask = speedchange_cpumask;
590 cpumask_clear(&speedchange_cpumask);
591 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
593 for_each_cpu(cpu, &tmp_mask) {
594 pcpu = &per_cpu(cpuinfo, cpu);
596 down_write(&pcpu->policy->rwsem);
598 if (likely(down_read_trylock(&pcpu->enable_sem))) {
599 if (likely(pcpu->governor_enabled))
600 cpufreq_interactive_adjust_cpu(cpu,
602 up_read(&pcpu->enable_sem);
605 up_write(&pcpu->policy->rwsem);
612 static void cpufreq_interactive_boost(struct cpufreq_interactive_tunables *tunables)
616 unsigned long flags[2];
617 struct cpufreq_interactive_cpuinfo *pcpu;
619 tunables->boosted = true;
621 spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
623 for_each_online_cpu(i) {
624 pcpu = &per_cpu(cpuinfo, i);
625 if (tunables != pcpu->policy->governor_data)
628 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
629 if (pcpu->target_freq < tunables->hispeed_freq) {
630 pcpu->target_freq = tunables->hispeed_freq;
631 cpumask_set_cpu(i, &speedchange_cpumask);
632 pcpu->pol_hispeed_val_time =
633 ktime_to_us(ktime_get());
636 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
639 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
642 wake_up_process(speedchange_task);
645 static int cpufreq_interactive_notifier(
646 struct notifier_block *nb, unsigned long val, void *data)
648 struct cpufreq_freqs *freq = data;
649 struct cpufreq_interactive_cpuinfo *pcpu;
653 if (val == CPUFREQ_POSTCHANGE) {
654 pcpu = &per_cpu(cpuinfo, freq->cpu);
655 if (!down_read_trylock(&pcpu->enable_sem))
657 if (!pcpu->governor_enabled) {
658 up_read(&pcpu->enable_sem);
662 for_each_cpu(cpu, pcpu->policy->cpus) {
663 struct cpufreq_interactive_cpuinfo *pjcpu =
664 &per_cpu(cpuinfo, cpu);
665 if (cpu != freq->cpu) {
666 if (!down_read_trylock(&pjcpu->enable_sem))
668 if (!pjcpu->governor_enabled) {
669 up_read(&pjcpu->enable_sem);
673 spin_lock_irqsave(&pjcpu->load_lock, flags);
675 spin_unlock_irqrestore(&pjcpu->load_lock, flags);
676 if (cpu != freq->cpu)
677 up_read(&pjcpu->enable_sem);
680 up_read(&pcpu->enable_sem);
685 static struct notifier_block cpufreq_notifier_block = {
686 .notifier_call = cpufreq_interactive_notifier,
689 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
694 unsigned int *tokenized_data;
698 while ((cp = strpbrk(cp + 1, " :")))
701 if (!(ntokens & 0x1))
704 tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
705 if (!tokenized_data) {
712 while (i < ntokens) {
713 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
716 cp = strpbrk(cp, " :");
725 *num_tokens = ntokens;
726 return tokenized_data;
729 kfree(tokenized_data);
734 static ssize_t show_target_loads(
735 struct cpufreq_interactive_tunables *tunables,
742 spin_lock_irqsave(&tunables->target_loads_lock, flags);
744 for (i = 0; i < tunables->ntarget_loads; i++)
745 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
746 i & 0x1 ? ":" : " ");
748 sprintf(buf + ret - 1, "\n");
749 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
753 static ssize_t store_target_loads(
754 struct cpufreq_interactive_tunables *tunables,
755 const char *buf, size_t count)
758 unsigned int *new_target_loads = NULL;
761 new_target_loads = get_tokenized_data(buf, &ntokens);
762 if (IS_ERR(new_target_loads))
763 return PTR_RET(new_target_loads);
765 spin_lock_irqsave(&tunables->target_loads_lock, flags);
766 if (tunables->target_loads != default_target_loads)
767 kfree(tunables->target_loads);
768 tunables->target_loads = new_target_loads;
769 tunables->ntarget_loads = ntokens;
770 spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
774 static ssize_t show_above_hispeed_delay(
775 struct cpufreq_interactive_tunables *tunables, char *buf)
781 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
783 for (i = 0; i < tunables->nabove_hispeed_delay; i++)
784 ret += sprintf(buf + ret, "%u%s",
785 tunables->above_hispeed_delay[i],
786 i & 0x1 ? ":" : " ");
788 sprintf(buf + ret - 1, "\n");
789 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
793 static ssize_t store_above_hispeed_delay(
794 struct cpufreq_interactive_tunables *tunables,
795 const char *buf, size_t count)
798 unsigned int *new_above_hispeed_delay = NULL;
801 new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
802 if (IS_ERR(new_above_hispeed_delay))
803 return PTR_RET(new_above_hispeed_delay);
805 spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
806 if (tunables->above_hispeed_delay != default_above_hispeed_delay)
807 kfree(tunables->above_hispeed_delay);
808 tunables->above_hispeed_delay = new_above_hispeed_delay;
809 tunables->nabove_hispeed_delay = ntokens;
810 spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
815 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
818 return sprintf(buf, "%u\n", tunables->hispeed_freq);
821 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
822 const char *buf, size_t count)
825 long unsigned int val;
827 ret = kstrtoul(buf, 0, &val);
830 tunables->hispeed_freq = val;
834 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
835 *tunables, char *buf)
837 return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
840 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
841 *tunables, const char *buf, size_t count)
846 ret = kstrtoul(buf, 0, &val);
849 tunables->go_hispeed_load = val;
853 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
854 *tunables, char *buf)
856 return sprintf(buf, "%lu\n", tunables->min_sample_time);
859 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
860 *tunables, const char *buf, size_t count)
865 ret = kstrtoul(buf, 0, &val);
868 tunables->min_sample_time = val;
872 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
875 return sprintf(buf, "%lu\n", tunables->timer_rate);
878 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
879 const char *buf, size_t count)
882 unsigned long val, val_round;
884 ret = kstrtoul(buf, 0, &val);
888 val_round = jiffies_to_usecs(usecs_to_jiffies(val));
889 if (val != val_round)
890 pr_warn("timer_rate not aligned to jiffy. Rounded up to %lu\n",
893 tunables->timer_rate = val_round;
897 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
900 return sprintf(buf, "%d\n", tunables->timer_slack_val);
903 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
904 const char *buf, size_t count)
909 ret = kstrtol(buf, 10, &val);
913 tunables->timer_slack_val = val;
917 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
920 return sprintf(buf, "%d\n", tunables->boost_val);
923 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
924 const char *buf, size_t count)
929 ret = kstrtoul(buf, 0, &val);
933 tunables->boost_val = val;
935 if (tunables->boost_val) {
936 trace_cpufreq_interactive_boost("on");
937 if (!tunables->boosted)
938 cpufreq_interactive_boost(tunables);
940 tunables->boostpulse_endtime = ktime_to_us(ktime_get());
941 trace_cpufreq_interactive_unboost("off");
947 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
948 const char *buf, size_t count)
953 ret = kstrtoul(buf, 0, &val);
957 tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
958 tunables->boostpulse_duration_val;
959 trace_cpufreq_interactive_boost("pulse");
960 if (!tunables->boosted)
961 cpufreq_interactive_boost(tunables);
965 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
966 *tunables, char *buf)
968 return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
971 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
972 *tunables, const char *buf, size_t count)
977 ret = kstrtoul(buf, 0, &val);
981 tunables->boostpulse_duration_val = val;
985 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
988 return sprintf(buf, "%u\n", tunables->io_is_busy);
991 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
992 const char *buf, size_t count)
997 ret = kstrtoul(buf, 0, &val);
1000 tunables->io_is_busy = val;
1005 * Create show/store routines
1006 * - sys: One governor instance for complete SYSTEM
1007 * - pol: One governor instance per struct cpufreq_policy
1009 #define show_gov_pol_sys(file_name) \
1010 static ssize_t show_##file_name##_gov_sys \
1011 (struct kobject *kobj, struct attribute *attr, char *buf) \
1013 return show_##file_name(common_tunables, buf); \
1016 static ssize_t show_##file_name##_gov_pol \
1017 (struct cpufreq_policy *policy, char *buf) \
1019 return show_##file_name(policy->governor_data, buf); \
1022 #define store_gov_pol_sys(file_name) \
1023 static ssize_t store_##file_name##_gov_sys \
1024 (struct kobject *kobj, struct attribute *attr, const char *buf, \
1027 return store_##file_name(common_tunables, buf, count); \
1030 static ssize_t store_##file_name##_gov_pol \
1031 (struct cpufreq_policy *policy, const char *buf, size_t count) \
1033 return store_##file_name(policy->governor_data, buf, count); \
1036 #define show_store_gov_pol_sys(file_name) \
1037 show_gov_pol_sys(file_name); \
1038 store_gov_pol_sys(file_name)
1040 show_store_gov_pol_sys(target_loads);
1041 show_store_gov_pol_sys(above_hispeed_delay);
1042 show_store_gov_pol_sys(hispeed_freq);
1043 show_store_gov_pol_sys(go_hispeed_load);
1044 show_store_gov_pol_sys(min_sample_time);
1045 show_store_gov_pol_sys(timer_rate);
1046 show_store_gov_pol_sys(timer_slack);
1047 show_store_gov_pol_sys(boost);
1048 store_gov_pol_sys(boostpulse);
1049 show_store_gov_pol_sys(boostpulse_duration);
1050 show_store_gov_pol_sys(io_is_busy);
1052 #define gov_sys_attr_rw(_name) \
1053 static struct global_attr _name##_gov_sys = \
1054 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1056 #define gov_pol_attr_rw(_name) \
1057 static struct freq_attr _name##_gov_pol = \
1058 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1060 #define gov_sys_pol_attr_rw(_name) \
1061 gov_sys_attr_rw(_name); \
1062 gov_pol_attr_rw(_name)
1064 gov_sys_pol_attr_rw(target_loads);
1065 gov_sys_pol_attr_rw(above_hispeed_delay);
1066 gov_sys_pol_attr_rw(hispeed_freq);
1067 gov_sys_pol_attr_rw(go_hispeed_load);
1068 gov_sys_pol_attr_rw(min_sample_time);
1069 gov_sys_pol_attr_rw(timer_rate);
1070 gov_sys_pol_attr_rw(timer_slack);
1071 gov_sys_pol_attr_rw(boost);
1072 gov_sys_pol_attr_rw(boostpulse_duration);
1073 gov_sys_pol_attr_rw(io_is_busy);
1075 static struct global_attr boostpulse_gov_sys =
1076 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1078 static struct freq_attr boostpulse_gov_pol =
1079 __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1081 /* One Governor instance for entire system */
1082 static struct attribute *interactive_attributes_gov_sys[] = {
1083 &target_loads_gov_sys.attr,
1084 &above_hispeed_delay_gov_sys.attr,
1085 &hispeed_freq_gov_sys.attr,
1086 &go_hispeed_load_gov_sys.attr,
1087 &min_sample_time_gov_sys.attr,
1088 &timer_rate_gov_sys.attr,
1089 &timer_slack_gov_sys.attr,
1090 &boost_gov_sys.attr,
1091 &boostpulse_gov_sys.attr,
1092 &boostpulse_duration_gov_sys.attr,
1093 &io_is_busy_gov_sys.attr,
1097 static struct attribute_group interactive_attr_group_gov_sys = {
1098 .attrs = interactive_attributes_gov_sys,
1099 .name = "interactive",
1102 /* Per policy governor instance */
1103 static struct attribute *interactive_attributes_gov_pol[] = {
1104 &target_loads_gov_pol.attr,
1105 &above_hispeed_delay_gov_pol.attr,
1106 &hispeed_freq_gov_pol.attr,
1107 &go_hispeed_load_gov_pol.attr,
1108 &min_sample_time_gov_pol.attr,
1109 &timer_rate_gov_pol.attr,
1110 &timer_slack_gov_pol.attr,
1111 &boost_gov_pol.attr,
1112 &boostpulse_gov_pol.attr,
1113 &boostpulse_duration_gov_pol.attr,
1114 &io_is_busy_gov_pol.attr,
1118 static struct attribute_group interactive_attr_group_gov_pol = {
1119 .attrs = interactive_attributes_gov_pol,
1120 .name = "interactive",
1123 static struct attribute_group *get_sysfs_attr(void)
1125 if (have_governor_per_policy())
1126 return &interactive_attr_group_gov_pol;
1128 return &interactive_attr_group_gov_sys;
1131 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1135 if (val == IDLE_END)
1136 cpufreq_interactive_idle_end();
1141 static struct notifier_block cpufreq_interactive_idle_nb = {
1142 .notifier_call = cpufreq_interactive_idle_notifier,
1145 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1150 struct cpufreq_interactive_cpuinfo *pcpu;
1151 struct cpufreq_frequency_table *freq_table;
1152 struct cpufreq_interactive_tunables *tunables;
1153 unsigned long flags;
1155 if (have_governor_per_policy())
1156 tunables = policy->governor_data;
1158 tunables = common_tunables;
1160 WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1163 case CPUFREQ_GOV_POLICY_INIT:
1164 if (have_governor_per_policy()) {
1166 } else if (tunables) {
1167 tunables->usage_count++;
1168 policy->governor_data = tunables;
1172 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1174 pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1178 tunables->usage_count = 1;
1179 tunables->above_hispeed_delay = default_above_hispeed_delay;
1180 tunables->nabove_hispeed_delay =
1181 ARRAY_SIZE(default_above_hispeed_delay);
1182 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1183 tunables->target_loads = default_target_loads;
1184 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1185 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1186 tunables->timer_rate = DEFAULT_TIMER_RATE;
1187 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1188 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1190 spin_lock_init(&tunables->target_loads_lock);
1191 spin_lock_init(&tunables->above_hispeed_delay_lock);
1193 policy->governor_data = tunables;
1194 if (!have_governor_per_policy()) {
1195 common_tunables = tunables;
1196 WARN_ON(cpufreq_get_global_kobject());
1199 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1203 policy->governor_data = NULL;
1204 if (!have_governor_per_policy()) {
1205 common_tunables = NULL;
1206 cpufreq_put_global_kobject();
1211 if (!policy->governor->initialized) {
1212 idle_notifier_register(&cpufreq_interactive_idle_nb);
1213 cpufreq_register_notifier(&cpufreq_notifier_block,
1214 CPUFREQ_TRANSITION_NOTIFIER);
1219 case CPUFREQ_GOV_POLICY_EXIT:
1220 if (!--tunables->usage_count) {
1221 if (policy->governor->initialized == 1) {
1222 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1223 CPUFREQ_TRANSITION_NOTIFIER);
1224 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1227 sysfs_remove_group(get_governor_parent_kobj(policy),
1230 if (!have_governor_per_policy())
1231 cpufreq_put_global_kobject();
1234 common_tunables = NULL;
1237 policy->governor_data = NULL;
1240 case CPUFREQ_GOV_START:
1241 mutex_lock(&gov_lock);
1243 freq_table = cpufreq_frequency_get_table(policy->cpu);
1244 if (!tunables->hispeed_freq)
1245 tunables->hispeed_freq = policy->max;
1247 for_each_cpu(j, policy->cpus) {
1248 pcpu = &per_cpu(cpuinfo, j);
1249 pcpu->policy = policy;
1250 pcpu->target_freq = policy->cur;
1251 pcpu->freq_table = freq_table;
1252 pcpu->floor_freq = pcpu->target_freq;
1253 pcpu->pol_floor_val_time =
1254 ktime_to_us(ktime_get());
1255 pcpu->loc_floor_val_time = pcpu->pol_floor_val_time;
1256 pcpu->pol_hispeed_val_time = pcpu->pol_floor_val_time;
1257 pcpu->loc_hispeed_val_time = pcpu->pol_floor_val_time;
1258 down_write(&pcpu->enable_sem);
1259 del_timer_sync(&pcpu->cpu_timer);
1260 del_timer_sync(&pcpu->cpu_slack_timer);
1261 cpufreq_interactive_timer_start(tunables, j);
1262 pcpu->governor_enabled = 1;
1263 up_write(&pcpu->enable_sem);
1266 mutex_unlock(&gov_lock);
1269 case CPUFREQ_GOV_STOP:
1270 mutex_lock(&gov_lock);
1271 for_each_cpu(j, policy->cpus) {
1272 pcpu = &per_cpu(cpuinfo, j);
1273 down_write(&pcpu->enable_sem);
1274 pcpu->governor_enabled = 0;
1275 del_timer_sync(&pcpu->cpu_timer);
1276 del_timer_sync(&pcpu->cpu_slack_timer);
1277 up_write(&pcpu->enable_sem);
1280 mutex_unlock(&gov_lock);
1283 case CPUFREQ_GOV_LIMITS:
1284 if (policy->max < policy->cur)
1285 __cpufreq_driver_target(policy,
1286 policy->max, CPUFREQ_RELATION_H);
1287 else if (policy->min > policy->cur)
1288 __cpufreq_driver_target(policy,
1289 policy->min, CPUFREQ_RELATION_L);
1290 for_each_cpu(j, policy->cpus) {
1291 pcpu = &per_cpu(cpuinfo, j);
1293 down_read(&pcpu->enable_sem);
1294 if (pcpu->governor_enabled == 0) {
1295 up_read(&pcpu->enable_sem);
1299 spin_lock_irqsave(&pcpu->target_freq_lock, flags);
1300 if (policy->max < pcpu->target_freq)
1301 pcpu->target_freq = policy->max;
1302 else if (policy->min > pcpu->target_freq)
1303 pcpu->target_freq = policy->min;
1305 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
1306 up_read(&pcpu->enable_sem);
1313 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1316 struct cpufreq_governor cpufreq_gov_interactive = {
1317 .name = "interactive",
1318 .governor = cpufreq_governor_interactive,
1319 .max_transition_latency = 10000000,
1320 .owner = THIS_MODULE,
1323 static void cpufreq_interactive_nop_timer(unsigned long data)
1327 static int __init cpufreq_interactive_init(void)
1330 struct cpufreq_interactive_cpuinfo *pcpu;
1331 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1333 /* Initalize per-cpu timers */
1334 for_each_possible_cpu(i) {
1335 pcpu = &per_cpu(cpuinfo, i);
1336 init_timer_deferrable(&pcpu->cpu_timer);
1337 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1338 pcpu->cpu_timer.data = i;
1339 init_timer(&pcpu->cpu_slack_timer);
1340 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1341 spin_lock_init(&pcpu->load_lock);
1342 spin_lock_init(&pcpu->target_freq_lock);
1343 init_rwsem(&pcpu->enable_sem);
1346 spin_lock_init(&speedchange_cpumask_lock);
1347 mutex_init(&gov_lock);
1349 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1351 if (IS_ERR(speedchange_task))
1352 return PTR_ERR(speedchange_task);
1354 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m);
1355 get_task_struct(speedchange_task);
1357 /* NB: wake up so the thread does not look hung to the freezer */
1358 wake_up_process(speedchange_task);
1360 return cpufreq_register_governor(&cpufreq_gov_interactive);
1363 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1364 fs_initcall(cpufreq_interactive_init);
1366 module_init(cpufreq_interactive_init);
1369 static void __exit cpufreq_interactive_exit(void)
1371 cpufreq_unregister_governor(&cpufreq_gov_interactive);
1372 kthread_stop(speedchange_task);
1373 put_task_struct(speedchange_task);
1376 module_exit(cpufreq_interactive_exit);
1378 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1379 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1380 "Latency sensitive workloads");
1381 MODULE_LICENSE("GPL");