cpufreq: interactive: add timer slack to limit idle at speed > min
[firefly-linux-kernel-4.4.55.git] / drivers / cpufreq / cpufreq_interactive.c
1 /*
2  * drivers/cpufreq/cpufreq_interactive.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  *
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.
9  *
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.
14  *
15  * Author: Mike Chan (mike@android.com)
16  *
17  */
18
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/mutex.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/mutex.h>
33 #include <linux/slab.h>
34 #include <asm/cputime.h>
35
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/cpufreq_interactive.h>
38
39 static atomic_t active_count = ATOMIC_INIT(0);
40
41 struct cpufreq_interactive_cpuinfo {
42         struct timer_list cpu_timer;
43         struct timer_list cpu_slack_timer;
44         spinlock_t load_lock; /* protects the next 4 fields */
45         u64 time_in_idle;
46         u64 time_in_idle_timestamp;
47         u64 cputime_speedadj;
48         u64 cputime_speedadj_timestamp;
49         struct cpufreq_policy *policy;
50         struct cpufreq_frequency_table *freq_table;
51         unsigned int target_freq;
52         unsigned int floor_freq;
53         u64 floor_validate_time;
54         u64 hispeed_validate_time;
55         int governor_enabled;
56 };
57
58 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
59
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
65 /* Hi speed to bump to from lo speed when load burst (default max) */
66 static unsigned int hispeed_freq;
67
68 /* Go to hi speed when CPU load at or above this value. */
69 #define DEFAULT_GO_HISPEED_LOAD 85
70 static unsigned long go_hispeed_load;
71
72 /* Target load.  Lower values result in higher CPU speeds. */
73 #define DEFAULT_TARGET_LOAD 90
74 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
75 static spinlock_t target_loads_lock;
76 static unsigned int *target_loads = default_target_loads;
77 static int ntarget_loads = ARRAY_SIZE(default_target_loads);
78
79 /*
80  * The minimum amount of time to spend at a frequency before we can ramp down.
81  */
82 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
83 static unsigned long min_sample_time;
84
85 /*
86  * The sample rate of the timer used to increase frequency
87  */
88 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
89 static unsigned long timer_rate;
90
91 /*
92  * Wait this long before raising speed above hispeed, by default a single
93  * timer interval.
94  */
95 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
96 static unsigned long above_hispeed_delay_val;
97
98 /* Non-zero means indefinite speed boost active */
99 static int boost_val;
100 /* Duration of a boot pulse in usecs */
101 static int boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
102 /* End time of boost pulse in ktime converted to usecs */
103 static u64 boostpulse_endtime;
104
105 /*
106  * Max additional time to wait in idle, beyond timer_rate, at speeds above
107  * minimum before wakeup to reduce speed, or -1 if unnecessary.
108  */
109 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
110 static int timer_slack_val = DEFAULT_TIMER_SLACK;
111
112 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
113                 unsigned int event);
114
115 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
116 static
117 #endif
118 struct cpufreq_governor cpufreq_gov_interactive = {
119         .name = "interactive",
120         .governor = cpufreq_governor_interactive,
121         .max_transition_latency = 10000000,
122         .owner = THIS_MODULE,
123 };
124
125 static void cpufreq_interactive_timer_resched(
126         struct cpufreq_interactive_cpuinfo *pcpu)
127 {
128         unsigned long expires = jiffies + usecs_to_jiffies(timer_rate);
129
130         mod_timer_pinned(&pcpu->cpu_timer, expires);
131         if (timer_slack_val >= 0 && pcpu->target_freq > pcpu->policy->min) {
132                 expires += usecs_to_jiffies(timer_slack_val);
133                 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
134         }
135
136         spin_lock(&pcpu->load_lock);
137         pcpu->time_in_idle =
138                 get_cpu_idle_time_us(smp_processor_id(),
139                                      &pcpu->time_in_idle_timestamp);
140         pcpu->cputime_speedadj = 0;
141         pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
142         spin_unlock(&pcpu->load_lock);
143 }
144
145 static unsigned int freq_to_targetload(unsigned int freq)
146 {
147         int i;
148         unsigned int ret;
149
150         spin_lock(&target_loads_lock);
151
152         for (i = 0; i < ntarget_loads - 1 && freq >= target_loads[i+1]; i += 2)
153                 ;
154
155         ret = target_loads[i];
156         spin_unlock(&target_loads_lock);
157         return ret;
158 }
159
160 /*
161  * If increasing frequencies never map to a lower target load then
162  * choose_freq() will find the minimum frequency that does not exceed its
163  * target load given the current load.
164  */
165
166 static unsigned int choose_freq(
167         struct cpufreq_interactive_cpuinfo *pcpu, unsigned int loadadjfreq)
168 {
169         unsigned int freq = pcpu->policy->cur;
170         unsigned int prevfreq, freqmin, freqmax;
171         unsigned int tl;
172         int index;
173
174         freqmin = 0;
175         freqmax = UINT_MAX;
176
177         do {
178                 prevfreq = freq;
179                 tl = freq_to_targetload(freq);
180
181                 /*
182                  * Find the lowest frequency where the computed load is less
183                  * than or equal to the target load.
184                  */
185
186                 cpufreq_frequency_table_target(
187                         pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
188                         CPUFREQ_RELATION_L, &index);
189                 freq = pcpu->freq_table[index].frequency;
190
191                 if (freq > prevfreq) {
192                         /* The previous frequency is too low. */
193                         freqmin = prevfreq;
194
195                         if (freq >= freqmax) {
196                                 /*
197                                  * Find the highest frequency that is less
198                                  * than freqmax.
199                                  */
200                                 cpufreq_frequency_table_target(
201                                         pcpu->policy, pcpu->freq_table,
202                                         freqmax - 1, CPUFREQ_RELATION_H,
203                                         &index);
204                                 freq = pcpu->freq_table[index].frequency;
205
206                                 if (freq == freqmin) {
207                                         /*
208                                          * The first frequency below freqmax
209                                          * has already been found to be too
210                                          * low.  freqmax is the lowest speed
211                                          * we found that is fast enough.
212                                          */
213                                         freq = freqmax;
214                                         break;
215                                 }
216                         }
217                 } else if (freq < prevfreq) {
218                         /* The previous frequency is high enough. */
219                         freqmax = prevfreq;
220
221                         if (freq <= freqmin) {
222                                 /*
223                                  * Find the lowest frequency that is higher
224                                  * than freqmin.
225                                  */
226                                 cpufreq_frequency_table_target(
227                                         pcpu->policy, pcpu->freq_table,
228                                         freqmin + 1, CPUFREQ_RELATION_L,
229                                         &index);
230                                 freq = pcpu->freq_table[index].frequency;
231
232                                 /*
233                                  * If freqmax is the first frequency above
234                                  * freqmin then we have already found that
235                                  * this speed is fast enough.
236                                  */
237                                 if (freq == freqmax)
238                                         break;
239                         }
240                 }
241
242                 /* If same frequency chosen as previous then done. */
243         } while (freq != prevfreq);
244
245         return freq;
246 }
247
248 static u64 update_load(int cpu)
249 {
250         struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
251         u64 now;
252         u64 now_idle;
253         unsigned int delta_idle;
254         unsigned int delta_time;
255         u64 active_time;
256
257         now_idle = get_cpu_idle_time_us(cpu, &now);
258         delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
259         delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
260         active_time = delta_time - delta_idle;
261         pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
262
263         pcpu->time_in_idle = now_idle;
264         pcpu->time_in_idle_timestamp = now;
265         return now;
266 }
267
268 static void cpufreq_interactive_timer(unsigned long data)
269 {
270         u64 now;
271         unsigned int delta_time;
272         u64 cputime_speedadj;
273         int cpu_load;
274         struct cpufreq_interactive_cpuinfo *pcpu =
275                 &per_cpu(cpuinfo, data);
276         unsigned int new_freq;
277         unsigned int loadadjfreq;
278         unsigned int index;
279         unsigned long flags;
280         bool boosted;
281
282         smp_rmb();
283
284         if (!pcpu->governor_enabled)
285                 goto exit;
286
287         spin_lock(&pcpu->load_lock);
288         now = update_load(data);
289         delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
290         cputime_speedadj = pcpu->cputime_speedadj;
291         spin_unlock(&pcpu->load_lock);
292
293         if (WARN_ON_ONCE(!delta_time))
294                 goto rearm;
295
296         do_div(cputime_speedadj, delta_time);
297         loadadjfreq = (unsigned int)cputime_speedadj * 100;
298         cpu_load = loadadjfreq / pcpu->target_freq;
299         boosted = boost_val || now < boostpulse_endtime;
300
301         if ((cpu_load >= go_hispeed_load || boosted) &&
302             pcpu->target_freq < hispeed_freq)
303                 new_freq = hispeed_freq;
304         else
305                 new_freq = choose_freq(pcpu, loadadjfreq);
306
307         if (pcpu->target_freq >= hispeed_freq &&
308             new_freq > pcpu->target_freq &&
309             now - pcpu->hispeed_validate_time < above_hispeed_delay_val) {
310                 trace_cpufreq_interactive_notyet(
311                         data, cpu_load, pcpu->target_freq,
312                         pcpu->policy->cur, new_freq);
313                 goto rearm;
314         }
315
316         pcpu->hispeed_validate_time = now;
317
318         if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
319                                            new_freq, CPUFREQ_RELATION_L,
320                                            &index)) {
321                 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
322                              (int) data);
323                 goto rearm;
324         }
325
326         new_freq = pcpu->freq_table[index].frequency;
327
328         /*
329          * Do not scale below floor_freq unless we have been at or above the
330          * floor frequency for the minimum sample time since last validated.
331          */
332         if (new_freq < pcpu->floor_freq) {
333                 if (now - pcpu->floor_validate_time < min_sample_time) {
334                         trace_cpufreq_interactive_notyet(
335                                 data, cpu_load, pcpu->target_freq,
336                                 pcpu->policy->cur, new_freq);
337                         goto rearm;
338                 }
339         }
340
341         /*
342          * Update the timestamp for checking whether speed has been held at
343          * or above the selected frequency for a minimum of min_sample_time,
344          * if not boosted to hispeed_freq.  If boosted to hispeed_freq then we
345          * allow the speed to drop as soon as the boostpulse duration expires
346          * (or the indefinite boost is turned off).
347          */
348
349         if (!boosted || new_freq > hispeed_freq) {
350                 pcpu->floor_freq = new_freq;
351                 pcpu->floor_validate_time = now;
352         }
353
354         if (pcpu->target_freq == new_freq) {
355                 trace_cpufreq_interactive_already(
356                         data, cpu_load, pcpu->target_freq,
357                         pcpu->policy->cur, new_freq);
358                 goto rearm_if_notmax;
359         }
360
361         trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
362                                          pcpu->policy->cur, new_freq);
363
364         pcpu->target_freq = new_freq;
365         spin_lock_irqsave(&speedchange_cpumask_lock, flags);
366         cpumask_set_cpu(data, &speedchange_cpumask);
367         spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
368         wake_up_process(speedchange_task);
369
370 rearm_if_notmax:
371         /*
372          * Already set max speed and don't see a need to change that,
373          * wait until next idle to re-evaluate, don't need timer.
374          */
375         if (pcpu->target_freq == pcpu->policy->max)
376                 goto exit;
377
378 rearm:
379         if (!timer_pending(&pcpu->cpu_timer))
380                 cpufreq_interactive_timer_resched(pcpu);
381
382 exit:
383         return;
384 }
385
386 static void cpufreq_interactive_idle_start(void)
387 {
388         struct cpufreq_interactive_cpuinfo *pcpu =
389                 &per_cpu(cpuinfo, smp_processor_id());
390         int pending;
391
392         if (!pcpu->governor_enabled)
393                 return;
394
395         pending = timer_pending(&pcpu->cpu_timer);
396
397         if (pcpu->target_freq != pcpu->policy->min) {
398                 /*
399                  * Entering idle while not at lowest speed.  On some
400                  * platforms this can hold the other CPU(s) at that speed
401                  * even though the CPU is idle. Set a timer to re-evaluate
402                  * speed so this idle CPU doesn't hold the other CPUs above
403                  * min indefinitely.  This should probably be a quirk of
404                  * the CPUFreq driver.
405                  */
406                 if (!pending)
407                         cpufreq_interactive_timer_resched(pcpu);
408         }
409
410 }
411
412 static void cpufreq_interactive_idle_end(void)
413 {
414         struct cpufreq_interactive_cpuinfo *pcpu =
415                 &per_cpu(cpuinfo, smp_processor_id());
416
417         if (!pcpu->governor_enabled)
418                 return;
419
420         /* Arm the timer for 1-2 ticks later if not already. */
421         if (!timer_pending(&pcpu->cpu_timer)) {
422                 cpufreq_interactive_timer_resched(pcpu);
423         } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
424                 del_timer(&pcpu->cpu_timer);
425                 del_timer(&pcpu->cpu_slack_timer);
426                 cpufreq_interactive_timer(smp_processor_id());
427         }
428 }
429
430 static int cpufreq_interactive_speedchange_task(void *data)
431 {
432         unsigned int cpu;
433         cpumask_t tmp_mask;
434         unsigned long flags;
435         struct cpufreq_interactive_cpuinfo *pcpu;
436
437         while (1) {
438                 set_current_state(TASK_INTERRUPTIBLE);
439                 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
440
441                 if (cpumask_empty(&speedchange_cpumask)) {
442                         spin_unlock_irqrestore(&speedchange_cpumask_lock,
443                                                flags);
444                         schedule();
445
446                         if (kthread_should_stop())
447                                 break;
448
449                         spin_lock_irqsave(&speedchange_cpumask_lock, flags);
450                 }
451
452                 set_current_state(TASK_RUNNING);
453                 tmp_mask = speedchange_cpumask;
454                 cpumask_clear(&speedchange_cpumask);
455                 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
456
457                 for_each_cpu(cpu, &tmp_mask) {
458                         unsigned int j;
459                         unsigned int max_freq = 0;
460
461                         pcpu = &per_cpu(cpuinfo, cpu);
462                         smp_rmb();
463
464                         if (!pcpu->governor_enabled)
465                                 continue;
466
467                         for_each_cpu(j, pcpu->policy->cpus) {
468                                 struct cpufreq_interactive_cpuinfo *pjcpu =
469                                         &per_cpu(cpuinfo, j);
470
471                                 if (pjcpu->target_freq > max_freq)
472                                         max_freq = pjcpu->target_freq;
473                         }
474
475                         if (max_freq != pcpu->policy->cur)
476                                 __cpufreq_driver_target(pcpu->policy,
477                                                         max_freq,
478                                                         CPUFREQ_RELATION_H);
479                         trace_cpufreq_interactive_setspeed(cpu,
480                                                      pcpu->target_freq,
481                                                      pcpu->policy->cur);
482                 }
483         }
484
485         return 0;
486 }
487
488 static void cpufreq_interactive_boost(void)
489 {
490         int i;
491         int anyboost = 0;
492         unsigned long flags;
493         struct cpufreq_interactive_cpuinfo *pcpu;
494
495         spin_lock_irqsave(&speedchange_cpumask_lock, flags);
496
497         for_each_online_cpu(i) {
498                 pcpu = &per_cpu(cpuinfo, i);
499
500                 if (pcpu->target_freq < hispeed_freq) {
501                         pcpu->target_freq = hispeed_freq;
502                         cpumask_set_cpu(i, &speedchange_cpumask);
503                         pcpu->hispeed_validate_time =
504                                 ktime_to_us(ktime_get());
505                         anyboost = 1;
506                 }
507
508                 /*
509                  * Set floor freq and (re)start timer for when last
510                  * validated.
511                  */
512
513                 pcpu->floor_freq = hispeed_freq;
514                 pcpu->floor_validate_time = ktime_to_us(ktime_get());
515         }
516
517         spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
518
519         if (anyboost)
520                 wake_up_process(speedchange_task);
521 }
522
523 static int cpufreq_interactive_notifier(
524         struct notifier_block *nb, unsigned long val, void *data)
525 {
526         struct cpufreq_freqs *freq = data;
527         struct cpufreq_interactive_cpuinfo *pcpu;
528         int cpu;
529
530         if (val == CPUFREQ_POSTCHANGE) {
531                 pcpu = &per_cpu(cpuinfo, freq->cpu);
532
533                 for_each_cpu(cpu, pcpu->policy->cpus) {
534                         struct cpufreq_interactive_cpuinfo *pjcpu =
535                                 &per_cpu(cpuinfo, cpu);
536                         spin_lock(&pjcpu->load_lock);
537                         update_load(cpu);
538                         spin_unlock(&pjcpu->load_lock);
539                 }
540         }
541
542         return 0;
543 }
544
545 static struct notifier_block cpufreq_notifier_block = {
546         .notifier_call = cpufreq_interactive_notifier,
547 };
548
549 static ssize_t show_target_loads(
550         struct kobject *kobj, struct attribute *attr, char *buf)
551 {
552         int i;
553         ssize_t ret = 0;
554
555         spin_lock(&target_loads_lock);
556
557         for (i = 0; i < ntarget_loads; i++)
558                 ret += sprintf(buf + ret, "%u%s", target_loads[i],
559                                i & 0x1 ? ":" : " ");
560
561         ret += sprintf(buf + ret, "\n");
562         spin_unlock(&target_loads_lock);
563         return ret;
564 }
565
566 static ssize_t store_target_loads(
567         struct kobject *kobj, struct attribute *attr, const char *buf,
568         size_t count)
569 {
570         int ret;
571         const char *cp;
572         unsigned int *new_target_loads = NULL;
573         int ntokens = 1;
574         int i;
575
576         cp = buf;
577         while ((cp = strpbrk(cp + 1, " :")))
578                 ntokens++;
579
580         if (!(ntokens & 0x1))
581                 goto err_inval;
582
583         new_target_loads = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
584         if (!new_target_loads) {
585                 ret = -ENOMEM;
586                 goto err;
587         }
588
589         cp = buf;
590         i = 0;
591         while (i < ntokens) {
592                 if (sscanf(cp, "%u", &new_target_loads[i++]) != 1)
593                         goto err_inval;
594
595                 cp = strpbrk(cp, " :");
596                 if (!cp)
597                         break;
598                 cp++;
599         }
600
601         if (i != ntokens)
602                 goto err_inval;
603
604         spin_lock(&target_loads_lock);
605         if (target_loads != default_target_loads)
606                 kfree(target_loads);
607         target_loads = new_target_loads;
608         ntarget_loads = ntokens;
609         spin_unlock(&target_loads_lock);
610         return count;
611
612 err_inval:
613         ret = -EINVAL;
614 err:
615         kfree(new_target_loads);
616         return ret;
617 }
618
619 static struct global_attr target_loads_attr =
620         __ATTR(target_loads, S_IRUGO | S_IWUSR,
621                 show_target_loads, store_target_loads);
622
623 static ssize_t show_hispeed_freq(struct kobject *kobj,
624                                  struct attribute *attr, char *buf)
625 {
626         return sprintf(buf, "%u\n", hispeed_freq);
627 }
628
629 static ssize_t store_hispeed_freq(struct kobject *kobj,
630                                   struct attribute *attr, const char *buf,
631                                   size_t count)
632 {
633         int ret;
634         long unsigned int val;
635
636         ret = strict_strtoul(buf, 0, &val);
637         if (ret < 0)
638                 return ret;
639         hispeed_freq = val;
640         return count;
641 }
642
643 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
644                 show_hispeed_freq, store_hispeed_freq);
645
646
647 static ssize_t show_go_hispeed_load(struct kobject *kobj,
648                                      struct attribute *attr, char *buf)
649 {
650         return sprintf(buf, "%lu\n", go_hispeed_load);
651 }
652
653 static ssize_t store_go_hispeed_load(struct kobject *kobj,
654                         struct attribute *attr, const char *buf, size_t count)
655 {
656         int ret;
657         unsigned long val;
658
659         ret = strict_strtoul(buf, 0, &val);
660         if (ret < 0)
661                 return ret;
662         go_hispeed_load = val;
663         return count;
664 }
665
666 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
667                 show_go_hispeed_load, store_go_hispeed_load);
668
669 static ssize_t show_min_sample_time(struct kobject *kobj,
670                                 struct attribute *attr, char *buf)
671 {
672         return sprintf(buf, "%lu\n", min_sample_time);
673 }
674
675 static ssize_t store_min_sample_time(struct kobject *kobj,
676                         struct attribute *attr, const char *buf, size_t count)
677 {
678         int ret;
679         unsigned long val;
680
681         ret = strict_strtoul(buf, 0, &val);
682         if (ret < 0)
683                 return ret;
684         min_sample_time = val;
685         return count;
686 }
687
688 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
689                 show_min_sample_time, store_min_sample_time);
690
691 static ssize_t show_above_hispeed_delay(struct kobject *kobj,
692                                         struct attribute *attr, char *buf)
693 {
694         return sprintf(buf, "%lu\n", above_hispeed_delay_val);
695 }
696
697 static ssize_t store_above_hispeed_delay(struct kobject *kobj,
698                                          struct attribute *attr,
699                                          const char *buf, size_t count)
700 {
701         int ret;
702         unsigned long val;
703
704         ret = strict_strtoul(buf, 0, &val);
705         if (ret < 0)
706                 return ret;
707         above_hispeed_delay_val = val;
708         return count;
709 }
710
711 define_one_global_rw(above_hispeed_delay);
712
713 static ssize_t show_timer_rate(struct kobject *kobj,
714                         struct attribute *attr, char *buf)
715 {
716         return sprintf(buf, "%lu\n", timer_rate);
717 }
718
719 static ssize_t store_timer_rate(struct kobject *kobj,
720                         struct attribute *attr, const char *buf, size_t count)
721 {
722         int ret;
723         unsigned long val;
724
725         ret = strict_strtoul(buf, 0, &val);
726         if (ret < 0)
727                 return ret;
728         timer_rate = val;
729         return count;
730 }
731
732 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
733                 show_timer_rate, store_timer_rate);
734
735 static ssize_t show_timer_slack(
736         struct kobject *kobj, struct attribute *attr, char *buf)
737 {
738         return sprintf(buf, "%d\n", timer_slack_val);
739 }
740
741 static ssize_t store_timer_slack(
742         struct kobject *kobj, struct attribute *attr, const char *buf,
743         size_t count)
744 {
745         int ret;
746         unsigned long val;
747
748         ret = kstrtol(buf, 10, &val);
749         if (ret < 0)
750                 return ret;
751
752         timer_slack_val = val;
753         return count;
754 }
755
756 define_one_global_rw(timer_slack);
757
758 static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
759                           char *buf)
760 {
761         return sprintf(buf, "%d\n", boost_val);
762 }
763
764 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
765                            const char *buf, size_t count)
766 {
767         int ret;
768         unsigned long val;
769
770         ret = kstrtoul(buf, 0, &val);
771         if (ret < 0)
772                 return ret;
773
774         boost_val = val;
775
776         if (boost_val) {
777                 trace_cpufreq_interactive_boost("on");
778                 cpufreq_interactive_boost();
779         } else {
780                 trace_cpufreq_interactive_unboost("off");
781         }
782
783         return count;
784 }
785
786 define_one_global_rw(boost);
787
788 static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
789                                 const char *buf, size_t count)
790 {
791         int ret;
792         unsigned long val;
793
794         ret = kstrtoul(buf, 0, &val);
795         if (ret < 0)
796                 return ret;
797
798         boostpulse_endtime = ktime_to_us(ktime_get()) + boostpulse_duration_val;
799         trace_cpufreq_interactive_boost("pulse");
800         cpufreq_interactive_boost();
801         return count;
802 }
803
804 static struct global_attr boostpulse =
805         __ATTR(boostpulse, 0200, NULL, store_boostpulse);
806
807 static ssize_t show_boostpulse_duration(
808         struct kobject *kobj, struct attribute *attr, char *buf)
809 {
810         return sprintf(buf, "%d\n", boostpulse_duration_val);
811 }
812
813 static ssize_t store_boostpulse_duration(
814         struct kobject *kobj, struct attribute *attr, const char *buf,
815         size_t count)
816 {
817         int ret;
818         unsigned long val;
819
820         ret = kstrtoul(buf, 0, &val);
821         if (ret < 0)
822                 return ret;
823
824         boostpulse_duration_val = val;
825         return count;
826 }
827
828 define_one_global_rw(boostpulse_duration);
829
830 static struct attribute *interactive_attributes[] = {
831         &target_loads_attr.attr,
832         &hispeed_freq_attr.attr,
833         &go_hispeed_load_attr.attr,
834         &above_hispeed_delay.attr,
835         &min_sample_time_attr.attr,
836         &timer_rate_attr.attr,
837         &timer_slack.attr,
838         &boost.attr,
839         &boostpulse.attr,
840         &boostpulse_duration.attr,
841         NULL,
842 };
843
844 static struct attribute_group interactive_attr_group = {
845         .attrs = interactive_attributes,
846         .name = "interactive",
847 };
848
849 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
850                                              unsigned long val,
851                                              void *data)
852 {
853         switch (val) {
854         case IDLE_START:
855                 cpufreq_interactive_idle_start();
856                 break;
857         case IDLE_END:
858                 cpufreq_interactive_idle_end();
859                 break;
860         }
861
862         return 0;
863 }
864
865 static struct notifier_block cpufreq_interactive_idle_nb = {
866         .notifier_call = cpufreq_interactive_idle_notifier,
867 };
868
869 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
870                 unsigned int event)
871 {
872         int rc;
873         unsigned int j;
874         struct cpufreq_interactive_cpuinfo *pcpu;
875         struct cpufreq_frequency_table *freq_table;
876
877         switch (event) {
878         case CPUFREQ_GOV_START:
879                 if (!cpu_online(policy->cpu))
880                         return -EINVAL;
881
882                 freq_table =
883                         cpufreq_frequency_get_table(policy->cpu);
884                 if (!hispeed_freq)
885                         hispeed_freq = policy->max;
886
887                 for_each_cpu(j, policy->cpus) {
888                         unsigned long expires;
889
890                         pcpu = &per_cpu(cpuinfo, j);
891                         pcpu->policy = policy;
892                         pcpu->target_freq = policy->cur;
893                         pcpu->freq_table = freq_table;
894                         pcpu->floor_freq = pcpu->target_freq;
895                         pcpu->floor_validate_time =
896                                 ktime_to_us(ktime_get());
897                         pcpu->hispeed_validate_time =
898                                 pcpu->floor_validate_time;
899                         pcpu->governor_enabled = 1;
900                         smp_wmb();
901                         expires = jiffies + usecs_to_jiffies(timer_rate);
902                         pcpu->cpu_timer.expires = expires;
903                         add_timer_on(&pcpu->cpu_timer, j);
904
905                         if (timer_slack_val >= 0) {
906                                 expires += usecs_to_jiffies(timer_slack_val);
907                                 pcpu->cpu_slack_timer.expires = expires;
908                                 add_timer_on(&pcpu->cpu_slack_timer, j);
909                         }
910                 }
911
912                 /*
913                  * Do not register the idle hook and create sysfs
914                  * entries if we have already done so.
915                  */
916                 if (atomic_inc_return(&active_count) > 1)
917                         return 0;
918
919                 rc = sysfs_create_group(cpufreq_global_kobject,
920                                 &interactive_attr_group);
921                 if (rc)
922                         return rc;
923
924                 idle_notifier_register(&cpufreq_interactive_idle_nb);
925                 cpufreq_register_notifier(
926                         &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
927                 break;
928
929         case CPUFREQ_GOV_STOP:
930                 for_each_cpu(j, policy->cpus) {
931                         pcpu = &per_cpu(cpuinfo, j);
932                         pcpu->governor_enabled = 0;
933                         smp_wmb();
934                         del_timer_sync(&pcpu->cpu_timer);
935                         del_timer_sync(&pcpu->cpu_slack_timer);
936                 }
937
938                 if (atomic_dec_return(&active_count) > 0)
939                         return 0;
940
941                 cpufreq_unregister_notifier(
942                         &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
943                 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
944                 sysfs_remove_group(cpufreq_global_kobject,
945                                 &interactive_attr_group);
946
947                 break;
948
949         case CPUFREQ_GOV_LIMITS:
950                 if (policy->max < policy->cur)
951                         __cpufreq_driver_target(policy,
952                                         policy->max, CPUFREQ_RELATION_H);
953                 else if (policy->min > policy->cur)
954                         __cpufreq_driver_target(policy,
955                                         policy->min, CPUFREQ_RELATION_L);
956                 break;
957         }
958         return 0;
959 }
960
961 static void cpufreq_interactive_nop_timer(unsigned long data)
962 {
963 }
964
965 static int __init cpufreq_interactive_init(void)
966 {
967         unsigned int i;
968         struct cpufreq_interactive_cpuinfo *pcpu;
969         struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
970
971         go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
972         min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
973         above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
974         timer_rate = DEFAULT_TIMER_RATE;
975
976         /* Initalize per-cpu timers */
977         for_each_possible_cpu(i) {
978                 pcpu = &per_cpu(cpuinfo, i);
979                 init_timer_deferrable(&pcpu->cpu_timer);
980                 pcpu->cpu_timer.function = cpufreq_interactive_timer;
981                 pcpu->cpu_timer.data = i;
982                 init_timer(&pcpu->cpu_slack_timer);
983                 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
984                 spin_lock_init(&pcpu->load_lock);
985         }
986
987         spin_lock_init(&target_loads_lock);
988         spin_lock_init(&speedchange_cpumask_lock);
989         speedchange_task =
990                 kthread_create(cpufreq_interactive_speedchange_task, NULL,
991                                "cfinteractive");
992         if (IS_ERR(speedchange_task))
993                 return PTR_ERR(speedchange_task);
994
995         sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, &param);
996         get_task_struct(speedchange_task);
997
998         /* NB: wake up so the thread does not look hung to the freezer */
999         wake_up_process(speedchange_task);
1000
1001         return cpufreq_register_governor(&cpufreq_gov_interactive);
1002 }
1003
1004 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1005 fs_initcall(cpufreq_interactive_init);
1006 #else
1007 module_init(cpufreq_interactive_init);
1008 #endif
1009
1010 static void __exit cpufreq_interactive_exit(void)
1011 {
1012         cpufreq_unregister_governor(&cpufreq_gov_interactive);
1013         kthread_stop(speedchange_task);
1014         put_task_struct(speedchange_task);
1015 }
1016
1017 module_exit(cpufreq_interactive_exit);
1018
1019 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1020 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1021         "Latency sensitive workloads");
1022 MODULE_LICENSE("GPL");