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