drm/rockchip: add rk3399 vop big csc support
[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 #ifdef CONFIG_ARCH_ROCKCHIP
23 #include <linux/input.h>
24 #endif
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/rwsem.h>
28 #include <linux/sched.h>
29 #include <linux/sched/rt.h>
30 #include <linux/tick.h>
31 #include <linux/time.h>
32 #include <linux/timer.h>
33 #include <linux/workqueue.h>
34 #include <linux/kthread.h>
35 #include <linux/slab.h>
36
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/cpufreq_interactive.h>
39
40 struct cpufreq_interactive_cpuinfo {
41         struct timer_list cpu_timer;
42         struct timer_list cpu_slack_timer;
43         spinlock_t load_lock; /* protects the next 4 fields */
44         u64 time_in_idle;
45         u64 time_in_idle_timestamp;
46         u64 cputime_speedadj;
47         u64 cputime_speedadj_timestamp;
48         struct cpufreq_policy *policy;
49         struct cpufreq_frequency_table *freq_table;
50         spinlock_t target_freq_lock; /*protects target freq */
51         unsigned int target_freq;
52         unsigned int floor_freq;
53         u64 pol_floor_val_time; /* policy floor_validate_time */
54         u64 loc_floor_val_time; /* per-cpu floor_validate_time */
55         u64 pol_hispeed_val_time; /* policy hispeed_validate_time */
56         u64 loc_hispeed_val_time; /* per-cpu hispeed_validate_time */
57         struct rw_semaphore enable_sem;
58         int governor_enabled;
59 };
60
61 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
62
63 /* realtime thread handles frequency scaling */
64 static struct task_struct *speedchange_task;
65 static cpumask_t speedchange_cpumask;
66 static spinlock_t speedchange_cpumask_lock;
67 static struct mutex gov_lock;
68
69 /* Target load.  Lower values result in higher CPU speeds. */
70 #define DEFAULT_TARGET_LOAD 90
71 static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
72
73 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
74 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
75 static unsigned int default_above_hispeed_delay[] = {
76         DEFAULT_ABOVE_HISPEED_DELAY };
77
78 struct cpufreq_interactive_tunables {
79         int usage_count;
80         /* Hi speed to bump to from lo speed when load burst (default max) */
81         unsigned int hispeed_freq;
82         /* Go to hi speed when CPU load at or above this value. */
83 #define DEFAULT_GO_HISPEED_LOAD 99
84         unsigned long go_hispeed_load;
85         /* Target load. Lower values result in higher CPU speeds. */
86         spinlock_t target_loads_lock;
87         unsigned int *target_loads;
88         int ntarget_loads;
89         /*
90          * The minimum amount of time to spend at a frequency before we can ramp
91          * down.
92          */
93 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
94         unsigned long min_sample_time;
95         /*
96          * The sample rate of the timer used to increase frequency
97          */
98         unsigned long timer_rate;
99         /*
100          * Wait this long before raising speed above hispeed, by default a
101          * single timer interval.
102          */
103         spinlock_t above_hispeed_delay_lock;
104         unsigned int *above_hispeed_delay;
105         int nabove_hispeed_delay;
106         /* Non-zero means indefinite speed boost active */
107         int boost_val;
108         /* Duration of a boot pulse in usecs */
109         int boostpulse_duration_val;
110         /* End time of boost pulse in ktime converted to usecs */
111         u64 boostpulse_endtime;
112 #ifdef CONFIG_ARCH_ROCKCHIP
113         /* Frequency to which a touch boost takes the cpus to */
114         unsigned long touchboost_freq;
115         /* Duration of a touchboost pulse in usecs */
116         int touchboostpulse_duration_val;
117         /* End time of touchboost pulse in ktime converted to usecs */
118         u64 touchboostpulse_endtime;
119 #endif
120         bool boosted;
121         /*
122          * Max additional time to wait in idle, beyond timer_rate, at speeds
123          * above minimum before wakeup to reduce speed, or -1 if unnecessary.
124          */
125 #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
126         int timer_slack_val;
127         bool io_is_busy;
128 };
129
130 /* For cases where we have single governor instance for system */
131 static struct cpufreq_interactive_tunables *common_tunables;
132
133 static struct attribute_group *get_sysfs_attr(void);
134
135 static void cpufreq_interactive_timer_resched(
136         struct cpufreq_interactive_cpuinfo *pcpu)
137 {
138         struct cpufreq_interactive_tunables *tunables =
139                 pcpu->policy->governor_data;
140         unsigned long expires;
141         unsigned long flags;
142
143         spin_lock_irqsave(&pcpu->load_lock, flags);
144         pcpu->time_in_idle =
145                 get_cpu_idle_time(smp_processor_id(),
146                                   &pcpu->time_in_idle_timestamp,
147                                   tunables->io_is_busy);
148         pcpu->cputime_speedadj = 0;
149         pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
150         expires = jiffies + usecs_to_jiffies(tunables->timer_rate);
151         mod_timer_pinned(&pcpu->cpu_timer, expires);
152
153         if (tunables->timer_slack_val >= 0 &&
154             pcpu->target_freq > pcpu->policy->min) {
155                 expires += usecs_to_jiffies(tunables->timer_slack_val);
156                 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
157         }
158
159         spin_unlock_irqrestore(&pcpu->load_lock, flags);
160 }
161
162 /* The caller shall take enable_sem write semaphore to avoid any timer race.
163  * The cpu_timer and cpu_slack_timer must be deactivated when calling this
164  * function.
165  */
166 static void cpufreq_interactive_timer_start(
167         struct cpufreq_interactive_tunables *tunables, int cpu)
168 {
169         struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
170         unsigned long expires = jiffies +
171                 usecs_to_jiffies(tunables->timer_rate);
172         unsigned long flags;
173
174         pcpu->cpu_timer.expires = expires;
175         add_timer_on(&pcpu->cpu_timer, cpu);
176         if (tunables->timer_slack_val >= 0 &&
177             pcpu->target_freq > pcpu->policy->min) {
178                 expires += usecs_to_jiffies(tunables->timer_slack_val);
179                 pcpu->cpu_slack_timer.expires = expires;
180                 add_timer_on(&pcpu->cpu_slack_timer, cpu);
181         }
182
183         spin_lock_irqsave(&pcpu->load_lock, flags);
184         pcpu->time_in_idle =
185                 get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp,
186                                   tunables->io_is_busy);
187         pcpu->cputime_speedadj = 0;
188         pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
189         spin_unlock_irqrestore(&pcpu->load_lock, flags);
190 }
191
192 static unsigned int freq_to_above_hispeed_delay(
193         struct cpufreq_interactive_tunables *tunables,
194         unsigned int freq)
195 {
196         int i;
197         unsigned int ret;
198         unsigned long flags;
199
200         spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
201
202         for (i = 0; i < tunables->nabove_hispeed_delay - 1 &&
203                         freq >= tunables->above_hispeed_delay[i+1]; i += 2)
204                 ;
205
206         ret = tunables->above_hispeed_delay[i];
207         spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
208         return ret;
209 }
210
211 static unsigned int freq_to_targetload(
212         struct cpufreq_interactive_tunables *tunables, unsigned int freq)
213 {
214         int i;
215         unsigned int ret;
216         unsigned long flags;
217
218         spin_lock_irqsave(&tunables->target_loads_lock, flags);
219
220         for (i = 0; i < tunables->ntarget_loads - 1 &&
221                     freq >= tunables->target_loads[i+1]; i += 2)
222                 ;
223
224         ret = tunables->target_loads[i];
225         spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
226         return ret;
227 }
228
229 /*
230  * If increasing frequencies never map to a lower target load then
231  * choose_freq() will find the minimum frequency that does not exceed its
232  * target load given the current load.
233  */
234 static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu,
235                 unsigned int loadadjfreq)
236 {
237         unsigned int freq = pcpu->policy->cur;
238         unsigned int prevfreq, freqmin, freqmax;
239         unsigned int tl;
240         int index;
241
242         freqmin = 0;
243         freqmax = UINT_MAX;
244
245         do {
246                 prevfreq = freq;
247                 tl = freq_to_targetload(pcpu->policy->governor_data, freq);
248
249                 /*
250                  * Find the lowest frequency where the computed load is less
251                  * than or equal to the target load.
252                  */
253
254                 if (cpufreq_frequency_table_target(
255                             pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
256                             CPUFREQ_RELATION_L, &index))
257                         break;
258                 freq = pcpu->freq_table[index].frequency;
259
260                 if (freq > prevfreq) {
261                         /* The previous frequency is too low. */
262                         freqmin = prevfreq;
263
264                         if (freq >= freqmax) {
265                                 /*
266                                  * Find the highest frequency that is less
267                                  * than freqmax.
268                                  */
269                                 if (cpufreq_frequency_table_target(
270                                             pcpu->policy, pcpu->freq_table,
271                                             freqmax - 1, CPUFREQ_RELATION_H,
272                                             &index))
273                                         break;
274                                 freq = pcpu->freq_table[index].frequency;
275
276                                 if (freq == freqmin) {
277                                         /*
278                                          * The first frequency below freqmax
279                                          * has already been found to be too
280                                          * low.  freqmax is the lowest speed
281                                          * we found that is fast enough.
282                                          */
283                                         freq = freqmax;
284                                         break;
285                                 }
286                         }
287                 } else if (freq < prevfreq) {
288                         /* The previous frequency is high enough. */
289                         freqmax = prevfreq;
290
291                         if (freq <= freqmin) {
292                                 /*
293                                  * Find the lowest frequency that is higher
294                                  * than freqmin.
295                                  */
296                                 if (cpufreq_frequency_table_target(
297                                             pcpu->policy, pcpu->freq_table,
298                                             freqmin + 1, CPUFREQ_RELATION_L,
299                                             &index))
300                                         break;
301                                 freq = pcpu->freq_table[index].frequency;
302
303                                 /*
304                                  * If freqmax is the first frequency above
305                                  * freqmin then we have already found that
306                                  * this speed is fast enough.
307                                  */
308                                 if (freq == freqmax)
309                                         break;
310                         }
311                 }
312
313                 /* If same frequency chosen as previous then done. */
314         } while (freq != prevfreq);
315
316         return freq;
317 }
318
319 static u64 update_load(int cpu)
320 {
321         struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
322         struct cpufreq_interactive_tunables *tunables =
323                 pcpu->policy->governor_data;
324         u64 now;
325         u64 now_idle;
326         unsigned int delta_idle;
327         unsigned int delta_time;
328         u64 active_time;
329
330         now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy);
331         delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
332         delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
333
334         if (delta_time <= delta_idle)
335                 active_time = 0;
336         else
337                 active_time = delta_time - delta_idle;
338
339         pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
340
341         pcpu->time_in_idle = now_idle;
342         pcpu->time_in_idle_timestamp = now;
343         return now;
344 }
345
346 static void cpufreq_interactive_timer(unsigned long data)
347 {
348         u64 now;
349         unsigned int delta_time;
350         u64 cputime_speedadj;
351         int cpu_load;
352         struct cpufreq_interactive_cpuinfo *pcpu =
353                 &per_cpu(cpuinfo, data);
354         struct cpufreq_interactive_tunables *tunables =
355                 pcpu->policy->governor_data;
356         unsigned int new_freq;
357         unsigned int loadadjfreq;
358         unsigned int index;
359         unsigned long flags;
360         u64 max_fvtime;
361
362         if (!down_read_trylock(&pcpu->enable_sem))
363                 return;
364         if (!pcpu->governor_enabled)
365                 goto exit;
366
367         spin_lock_irqsave(&pcpu->load_lock, flags);
368         now = update_load(data);
369         delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
370         cputime_speedadj = pcpu->cputime_speedadj;
371         spin_unlock_irqrestore(&pcpu->load_lock, flags);
372
373         if (WARN_ON_ONCE(!delta_time))
374                 goto rearm;
375
376         spin_lock_irqsave(&pcpu->target_freq_lock, flags);
377         do_div(cputime_speedadj, delta_time);
378         loadadjfreq = (unsigned int)cputime_speedadj * 100;
379         cpu_load = loadadjfreq / pcpu->policy->cur;
380         tunables->boosted = tunables->boost_val || now < tunables->boostpulse_endtime;
381
382         if (cpu_load >= tunables->go_hispeed_load || tunables->boosted) {
383                 if (pcpu->policy->cur < tunables->hispeed_freq) {
384                         new_freq = tunables->hispeed_freq;
385                 } else {
386                         new_freq = choose_freq(pcpu, loadadjfreq);
387
388                         if (new_freq < tunables->hispeed_freq)
389                                 new_freq = tunables->hispeed_freq;
390                 }
391         } else {
392                 new_freq = choose_freq(pcpu, loadadjfreq);
393                 if (new_freq > tunables->hispeed_freq &&
394                                 pcpu->policy->cur < tunables->hispeed_freq)
395                         new_freq = tunables->hispeed_freq;
396         }
397 #ifdef CONFIG_ARCH_ROCKCHIP
398         if ((now < tunables->touchboostpulse_endtime) &&
399             (new_freq < tunables->touchboost_freq)) {
400                 new_freq = tunables->touchboost_freq;
401         }
402 #endif
403         if (pcpu->policy->cur >= tunables->hispeed_freq &&
404             new_freq > pcpu->policy->cur &&
405             now - pcpu->pol_hispeed_val_time <
406             freq_to_above_hispeed_delay(tunables, pcpu->policy->cur)) {
407                 trace_cpufreq_interactive_notyet(
408                         data, cpu_load, pcpu->target_freq,
409                         pcpu->policy->cur, new_freq);
410                 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
411                 goto rearm;
412         }
413
414         pcpu->loc_hispeed_val_time = now;
415
416         if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
417                                            new_freq, CPUFREQ_RELATION_L,
418                                            &index)) {
419                 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
420                 goto rearm;
421         }
422
423         new_freq = pcpu->freq_table[index].frequency;
424
425         /*
426          * Do not scale below floor_freq unless we have been at or above the
427          * floor frequency for the minimum sample time since last validated.
428          */
429         max_fvtime = max(pcpu->pol_floor_val_time, pcpu->loc_floor_val_time);
430         if (new_freq < pcpu->floor_freq &&
431             pcpu->target_freq >= pcpu->policy->cur) {
432                 if (now - max_fvtime < tunables->min_sample_time) {
433                         trace_cpufreq_interactive_notyet(
434                                 data, cpu_load, pcpu->target_freq,
435                                 pcpu->policy->cur, new_freq);
436                         spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
437                         goto rearm;
438                 }
439         }
440
441         /*
442          * Update the timestamp for checking whether speed has been held at
443          * or above the selected frequency for a minimum of min_sample_time,
444          * if not boosted to hispeed_freq.  If boosted to hispeed_freq then we
445          * allow the speed to drop as soon as the boostpulse duration expires
446          * (or the indefinite boost is turned off).
447          */
448
449         if (!tunables->boosted || new_freq > tunables->hispeed_freq) {
450                 pcpu->floor_freq = new_freq;
451                 if (pcpu->target_freq >= pcpu->policy->cur ||
452                     new_freq >= pcpu->policy->cur)
453                         pcpu->loc_floor_val_time = now;
454         }
455
456         if (pcpu->target_freq == new_freq &&
457                         pcpu->target_freq <= pcpu->policy->cur) {
458                 trace_cpufreq_interactive_already(
459                         data, cpu_load, pcpu->target_freq,
460                         pcpu->policy->cur, new_freq);
461                 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
462                 goto rearm;
463         }
464
465         trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
466                                          pcpu->policy->cur, new_freq);
467
468         pcpu->target_freq = new_freq;
469         spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
470         spin_lock_irqsave(&speedchange_cpumask_lock, flags);
471         cpumask_set_cpu(data, &speedchange_cpumask);
472         spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
473         wake_up_process(speedchange_task);
474
475 rearm:
476         if (!timer_pending(&pcpu->cpu_timer))
477                 cpufreq_interactive_timer_resched(pcpu);
478
479 exit:
480         up_read(&pcpu->enable_sem);
481         return;
482 }
483
484 static void cpufreq_interactive_idle_end(void)
485 {
486         struct cpufreq_interactive_cpuinfo *pcpu =
487                 &per_cpu(cpuinfo, smp_processor_id());
488
489         if (!down_read_trylock(&pcpu->enable_sem))
490                 return;
491         if (!pcpu->governor_enabled) {
492                 up_read(&pcpu->enable_sem);
493                 return;
494         }
495
496         /* Arm the timer for 1-2 ticks later if not already. */
497         if (!timer_pending(&pcpu->cpu_timer)) {
498                 cpufreq_interactive_timer_resched(pcpu);
499         } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
500                 del_timer(&pcpu->cpu_timer);
501                 del_timer(&pcpu->cpu_slack_timer);
502                 cpufreq_interactive_timer(smp_processor_id());
503         }
504
505         up_read(&pcpu->enable_sem);
506 }
507
508 static void cpufreq_interactive_get_policy_info(struct cpufreq_policy *policy,
509                                                 unsigned int *pmax_freq,
510                                                 u64 *phvt, u64 *pfvt)
511 {
512         struct cpufreq_interactive_cpuinfo *pcpu;
513         unsigned int max_freq = 0;
514         u64 hvt = ~0ULL, fvt = 0;
515         unsigned int i;
516
517         for_each_cpu(i, policy->cpus) {
518                 pcpu = &per_cpu(cpuinfo, i);
519
520                 fvt = max(fvt, pcpu->loc_floor_val_time);
521                 if (pcpu->target_freq > max_freq) {
522                         max_freq = pcpu->target_freq;
523                         hvt = pcpu->loc_hispeed_val_time;
524                 } else if (pcpu->target_freq == max_freq) {
525                         hvt = min(hvt, pcpu->loc_hispeed_val_time);
526                 }
527         }
528
529         *pmax_freq = max_freq;
530         *phvt = hvt;
531         *pfvt = fvt;
532 }
533
534 static void cpufreq_interactive_adjust_cpu(unsigned int cpu,
535                                            struct cpufreq_policy *policy)
536 {
537         struct cpufreq_interactive_cpuinfo *pcpu;
538         u64 hvt, fvt;
539         unsigned int max_freq;
540         int i;
541
542         cpufreq_interactive_get_policy_info(policy, &max_freq, &hvt, &fvt);
543
544         for_each_cpu(i, policy->cpus) {
545                 pcpu = &per_cpu(cpuinfo, i);
546                 pcpu->pol_floor_val_time = fvt;
547         }
548
549         if (max_freq != policy->cur) {
550                 __cpufreq_driver_target(policy, max_freq, CPUFREQ_RELATION_H);
551                 for_each_cpu(i, policy->cpus) {
552                         pcpu = &per_cpu(cpuinfo, i);
553                         pcpu->pol_hispeed_val_time = hvt;
554                 }
555         }
556
557         trace_cpufreq_interactive_setspeed(cpu, max_freq, policy->cur);
558 }
559
560 static int cpufreq_interactive_speedchange_task(void *data)
561 {
562         unsigned int cpu;
563         cpumask_t tmp_mask;
564         unsigned long flags;
565         struct cpufreq_interactive_cpuinfo *pcpu;
566
567         while (1) {
568                 set_current_state(TASK_INTERRUPTIBLE);
569                 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
570
571                 if (cpumask_empty(&speedchange_cpumask)) {
572                         spin_unlock_irqrestore(&speedchange_cpumask_lock,
573                                                flags);
574                         schedule();
575
576                         if (kthread_should_stop())
577                                 break;
578
579                         spin_lock_irqsave(&speedchange_cpumask_lock, flags);
580                 }
581
582                 set_current_state(TASK_RUNNING);
583                 tmp_mask = speedchange_cpumask;
584                 cpumask_clear(&speedchange_cpumask);
585                 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
586
587                 for_each_cpu(cpu, &tmp_mask) {
588                         pcpu = &per_cpu(cpuinfo, cpu);
589
590                         down_write(&pcpu->policy->rwsem);
591
592                         if (likely(down_read_trylock(&pcpu->enable_sem))) {
593                                 if (likely(pcpu->governor_enabled))
594                                         cpufreq_interactive_adjust_cpu(cpu,
595                                                         pcpu->policy);
596                                 up_read(&pcpu->enable_sem);
597                         }
598
599                         up_write(&pcpu->policy->rwsem);
600                 }
601         }
602
603         return 0;
604 }
605
606 static void cpufreq_interactive_boost(struct cpufreq_interactive_tunables *tunables)
607 {
608         int i;
609         int anyboost = 0;
610         unsigned long flags[2];
611         struct cpufreq_interactive_cpuinfo *pcpu;
612
613         tunables->boosted = true;
614
615         spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
616
617         for_each_online_cpu(i) {
618                 pcpu = &per_cpu(cpuinfo, i);
619
620                 if (!down_read_trylock(&pcpu->enable_sem))
621                         continue;
622
623                 if (!pcpu->governor_enabled) {
624                         up_read(&pcpu->enable_sem);
625                         continue;
626                 }
627
628                 if (tunables != pcpu->policy->governor_data) {
629                         up_read(&pcpu->enable_sem);
630                         continue;
631                 }
632
633                 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
634                 if (pcpu->target_freq < tunables->hispeed_freq) {
635                         pcpu->target_freq = tunables->hispeed_freq;
636                         cpumask_set_cpu(i, &speedchange_cpumask);
637                         pcpu->pol_hispeed_val_time =
638                                 ktime_to_us(ktime_get());
639                         anyboost = 1;
640                 }
641                 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
642
643                 up_read(&pcpu->enable_sem);
644         }
645
646         spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
647
648         if (anyboost)
649                 wake_up_process(speedchange_task);
650 }
651
652 static int cpufreq_interactive_notifier(
653         struct notifier_block *nb, unsigned long val, void *data)
654 {
655         struct cpufreq_freqs *freq = data;
656         struct cpufreq_interactive_cpuinfo *pcpu;
657         int cpu;
658         unsigned long flags;
659
660         if (val == CPUFREQ_POSTCHANGE) {
661                 pcpu = &per_cpu(cpuinfo, freq->cpu);
662                 if (!down_read_trylock(&pcpu->enable_sem))
663                         return 0;
664                 if (!pcpu->governor_enabled) {
665                         up_read(&pcpu->enable_sem);
666                         return 0;
667                 }
668
669                 for_each_cpu(cpu, pcpu->policy->cpus) {
670                         struct cpufreq_interactive_cpuinfo *pjcpu =
671                                 &per_cpu(cpuinfo, cpu);
672                         if (cpu != freq->cpu) {
673                                 if (!down_read_trylock(&pjcpu->enable_sem))
674                                         continue;
675                                 if (!pjcpu->governor_enabled) {
676                                         up_read(&pjcpu->enable_sem);
677                                         continue;
678                                 }
679                         }
680                         spin_lock_irqsave(&pjcpu->load_lock, flags);
681                         update_load(cpu);
682                         spin_unlock_irqrestore(&pjcpu->load_lock, flags);
683                         if (cpu != freq->cpu)
684                                 up_read(&pjcpu->enable_sem);
685                 }
686
687                 up_read(&pcpu->enable_sem);
688         }
689         return 0;
690 }
691
692 static struct notifier_block cpufreq_notifier_block = {
693         .notifier_call = cpufreq_interactive_notifier,
694 };
695
696 static unsigned int *get_tokenized_data(const char *buf, int *num_tokens)
697 {
698         const char *cp;
699         int i;
700         int ntokens = 1;
701         unsigned int *tokenized_data;
702         int err = -EINVAL;
703
704         cp = buf;
705         while ((cp = strpbrk(cp + 1, " :")))
706                 ntokens++;
707
708         if (!(ntokens & 0x1))
709                 goto err;
710
711         tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
712         if (!tokenized_data) {
713                 err = -ENOMEM;
714                 goto err;
715         }
716
717         cp = buf;
718         i = 0;
719         while (i < ntokens) {
720                 if (sscanf(cp, "%u", &tokenized_data[i++]) != 1)
721                         goto err_kfree;
722
723                 cp = strpbrk(cp, " :");
724                 if (!cp)
725                         break;
726                 cp++;
727         }
728
729         if (i != ntokens)
730                 goto err_kfree;
731
732         *num_tokens = ntokens;
733         return tokenized_data;
734
735 err_kfree:
736         kfree(tokenized_data);
737 err:
738         return ERR_PTR(err);
739 }
740
741 static ssize_t show_target_loads(
742         struct cpufreq_interactive_tunables *tunables,
743         char *buf)
744 {
745         int i;
746         ssize_t ret = 0;
747         unsigned long flags;
748
749         spin_lock_irqsave(&tunables->target_loads_lock, flags);
750
751         for (i = 0; i < tunables->ntarget_loads; i++)
752                 ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i],
753                                i & 0x1 ? ":" : " ");
754
755         sprintf(buf + ret - 1, "\n");
756         spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
757         return ret;
758 }
759
760 static ssize_t store_target_loads(
761         struct cpufreq_interactive_tunables *tunables,
762         const char *buf, size_t count)
763 {
764         int ntokens;
765         unsigned int *new_target_loads = NULL;
766         unsigned long flags;
767
768         new_target_loads = get_tokenized_data(buf, &ntokens);
769         if (IS_ERR(new_target_loads))
770                 return PTR_RET(new_target_loads);
771
772         spin_lock_irqsave(&tunables->target_loads_lock, flags);
773         if (tunables->target_loads != default_target_loads)
774                 kfree(tunables->target_loads);
775         tunables->target_loads = new_target_loads;
776         tunables->ntarget_loads = ntokens;
777         spin_unlock_irqrestore(&tunables->target_loads_lock, flags);
778         return count;
779 }
780
781 static ssize_t show_above_hispeed_delay(
782         struct cpufreq_interactive_tunables *tunables, char *buf)
783 {
784         int i;
785         ssize_t ret = 0;
786         unsigned long flags;
787
788         spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
789
790         for (i = 0; i < tunables->nabove_hispeed_delay; i++)
791                 ret += sprintf(buf + ret, "%u%s",
792                                tunables->above_hispeed_delay[i],
793                                i & 0x1 ? ":" : " ");
794
795         sprintf(buf + ret - 1, "\n");
796         spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
797         return ret;
798 }
799
800 static ssize_t store_above_hispeed_delay(
801         struct cpufreq_interactive_tunables *tunables,
802         const char *buf, size_t count)
803 {
804         int ntokens;
805         unsigned int *new_above_hispeed_delay = NULL;
806         unsigned long flags;
807
808         new_above_hispeed_delay = get_tokenized_data(buf, &ntokens);
809         if (IS_ERR(new_above_hispeed_delay))
810                 return PTR_RET(new_above_hispeed_delay);
811
812         spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags);
813         if (tunables->above_hispeed_delay != default_above_hispeed_delay)
814                 kfree(tunables->above_hispeed_delay);
815         tunables->above_hispeed_delay = new_above_hispeed_delay;
816         tunables->nabove_hispeed_delay = ntokens;
817         spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags);
818         return count;
819
820 }
821
822 static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
823                 char *buf)
824 {
825         return sprintf(buf, "%u\n", tunables->hispeed_freq);
826 }
827
828 static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables,
829                 const char *buf, size_t count)
830 {
831         int ret;
832         long unsigned int val;
833
834         ret = kstrtoul(buf, 0, &val);
835         if (ret < 0)
836                 return ret;
837         tunables->hispeed_freq = val;
838         return count;
839 }
840
841 static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables
842                 *tunables, char *buf)
843 {
844         return sprintf(buf, "%lu\n", tunables->go_hispeed_load);
845 }
846
847 static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables
848                 *tunables, const char *buf, size_t count)
849 {
850         int ret;
851         unsigned long val;
852
853         ret = kstrtoul(buf, 0, &val);
854         if (ret < 0)
855                 return ret;
856         tunables->go_hispeed_load = val;
857         return count;
858 }
859
860 static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables
861                 *tunables, char *buf)
862 {
863         return sprintf(buf, "%lu\n", tunables->min_sample_time);
864 }
865
866 static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables
867                 *tunables, const char *buf, size_t count)
868 {
869         int ret;
870         unsigned long val;
871
872         ret = kstrtoul(buf, 0, &val);
873         if (ret < 0)
874                 return ret;
875         tunables->min_sample_time = val;
876         return count;
877 }
878
879 static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables,
880                 char *buf)
881 {
882         return sprintf(buf, "%lu\n", tunables->timer_rate);
883 }
884
885 static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables,
886                 const char *buf, size_t count)
887 {
888         int ret;
889         unsigned long val, val_round;
890
891         ret = kstrtoul(buf, 0, &val);
892         if (ret < 0)
893                 return ret;
894
895         val_round = jiffies_to_usecs(usecs_to_jiffies(val));
896         if (val != val_round)
897                 pr_warn("timer_rate not aligned to jiffy. Rounded up to %lu\n",
898                         val_round);
899
900         tunables->timer_rate = val_round;
901         return count;
902 }
903
904 static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables,
905                 char *buf)
906 {
907         return sprintf(buf, "%d\n", tunables->timer_slack_val);
908 }
909
910 static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables,
911                 const char *buf, size_t count)
912 {
913         int ret;
914         unsigned long val;
915
916         ret = kstrtol(buf, 10, &val);
917         if (ret < 0)
918                 return ret;
919
920         tunables->timer_slack_val = val;
921         return count;
922 }
923
924 static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables,
925                           char *buf)
926 {
927         return sprintf(buf, "%d\n", tunables->boost_val);
928 }
929
930 static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables,
931                            const char *buf, size_t count)
932 {
933         int ret;
934         unsigned long val;
935
936         ret = kstrtoul(buf, 0, &val);
937         if (ret < 0)
938                 return ret;
939
940         tunables->boost_val = val;
941
942         if (tunables->boost_val) {
943                 trace_cpufreq_interactive_boost("on");
944                 if (!tunables->boosted)
945                         cpufreq_interactive_boost(tunables);
946         } else {
947                 tunables->boostpulse_endtime = ktime_to_us(ktime_get());
948                 trace_cpufreq_interactive_unboost("off");
949         }
950
951         return count;
952 }
953
954 static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables,
955                                 const char *buf, size_t count)
956 {
957         int ret;
958         unsigned long val;
959
960         ret = kstrtoul(buf, 0, &val);
961         if (ret < 0)
962                 return ret;
963
964         tunables->boostpulse_endtime = ktime_to_us(ktime_get()) +
965                 tunables->boostpulse_duration_val;
966         trace_cpufreq_interactive_boost("pulse");
967         if (!tunables->boosted)
968                 cpufreq_interactive_boost(tunables);
969         return count;
970 }
971
972 static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables
973                 *tunables, char *buf)
974 {
975         return sprintf(buf, "%d\n", tunables->boostpulse_duration_val);
976 }
977
978 static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables
979                 *tunables, const char *buf, size_t count)
980 {
981         int ret;
982         unsigned long val;
983
984         ret = kstrtoul(buf, 0, &val);
985         if (ret < 0)
986                 return ret;
987
988         tunables->boostpulse_duration_val = val;
989         return count;
990 }
991
992 static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables,
993                 char *buf)
994 {
995         return sprintf(buf, "%u\n", tunables->io_is_busy);
996 }
997
998 static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables,
999                 const char *buf, size_t count)
1000 {
1001         int ret;
1002         unsigned long val;
1003
1004         ret = kstrtoul(buf, 0, &val);
1005         if (ret < 0)
1006                 return ret;
1007         tunables->io_is_busy = val;
1008         return count;
1009 }
1010
1011 /*
1012  * Create show/store routines
1013  * - sys: One governor instance for complete SYSTEM
1014  * - pol: One governor instance per struct cpufreq_policy
1015  */
1016 #define show_gov_pol_sys(file_name)                                     \
1017 static ssize_t show_##file_name##_gov_sys                               \
1018 (struct kobject *kobj, struct attribute *attr, char *buf)               \
1019 {                                                                       \
1020         return show_##file_name(common_tunables, buf);                  \
1021 }                                                                       \
1022                                                                         \
1023 static ssize_t show_##file_name##_gov_pol                               \
1024 (struct cpufreq_policy *policy, char *buf)                              \
1025 {                                                                       \
1026         return show_##file_name(policy->governor_data, buf);            \
1027 }
1028
1029 #define store_gov_pol_sys(file_name)                                    \
1030 static ssize_t store_##file_name##_gov_sys                              \
1031 (struct kobject *kobj, struct attribute *attr, const char *buf,         \
1032         size_t count)                                                   \
1033 {                                                                       \
1034         return store_##file_name(common_tunables, buf, count);          \
1035 }                                                                       \
1036                                                                         \
1037 static ssize_t store_##file_name##_gov_pol                              \
1038 (struct cpufreq_policy *policy, const char *buf, size_t count)          \
1039 {                                                                       \
1040         return store_##file_name(policy->governor_data, buf, count);    \
1041 }
1042
1043 #define show_store_gov_pol_sys(file_name)                               \
1044 show_gov_pol_sys(file_name);                                            \
1045 store_gov_pol_sys(file_name)
1046
1047 show_store_gov_pol_sys(target_loads);
1048 show_store_gov_pol_sys(above_hispeed_delay);
1049 show_store_gov_pol_sys(hispeed_freq);
1050 show_store_gov_pol_sys(go_hispeed_load);
1051 show_store_gov_pol_sys(min_sample_time);
1052 show_store_gov_pol_sys(timer_rate);
1053 show_store_gov_pol_sys(timer_slack);
1054 show_store_gov_pol_sys(boost);
1055 store_gov_pol_sys(boostpulse);
1056 show_store_gov_pol_sys(boostpulse_duration);
1057 show_store_gov_pol_sys(io_is_busy);
1058
1059 #define gov_sys_attr_rw(_name)                                          \
1060 static struct global_attr _name##_gov_sys =                             \
1061 __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys)
1062
1063 #define gov_pol_attr_rw(_name)                                          \
1064 static struct freq_attr _name##_gov_pol =                               \
1065 __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol)
1066
1067 #define gov_sys_pol_attr_rw(_name)                                      \
1068         gov_sys_attr_rw(_name);                                         \
1069         gov_pol_attr_rw(_name)
1070
1071 gov_sys_pol_attr_rw(target_loads);
1072 gov_sys_pol_attr_rw(above_hispeed_delay);
1073 gov_sys_pol_attr_rw(hispeed_freq);
1074 gov_sys_pol_attr_rw(go_hispeed_load);
1075 gov_sys_pol_attr_rw(min_sample_time);
1076 gov_sys_pol_attr_rw(timer_rate);
1077 gov_sys_pol_attr_rw(timer_slack);
1078 gov_sys_pol_attr_rw(boost);
1079 gov_sys_pol_attr_rw(boostpulse_duration);
1080 gov_sys_pol_attr_rw(io_is_busy);
1081
1082 static struct global_attr boostpulse_gov_sys =
1083         __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys);
1084
1085 static struct freq_attr boostpulse_gov_pol =
1086         __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol);
1087
1088 /* One Governor instance for entire system */
1089 static struct attribute *interactive_attributes_gov_sys[] = {
1090         &target_loads_gov_sys.attr,
1091         &above_hispeed_delay_gov_sys.attr,
1092         &hispeed_freq_gov_sys.attr,
1093         &go_hispeed_load_gov_sys.attr,
1094         &min_sample_time_gov_sys.attr,
1095         &timer_rate_gov_sys.attr,
1096         &timer_slack_gov_sys.attr,
1097         &boost_gov_sys.attr,
1098         &boostpulse_gov_sys.attr,
1099         &boostpulse_duration_gov_sys.attr,
1100         &io_is_busy_gov_sys.attr,
1101         NULL,
1102 };
1103
1104 static struct attribute_group interactive_attr_group_gov_sys = {
1105         .attrs = interactive_attributes_gov_sys,
1106         .name = "interactive",
1107 };
1108
1109 /* Per policy governor instance */
1110 static struct attribute *interactive_attributes_gov_pol[] = {
1111         &target_loads_gov_pol.attr,
1112         &above_hispeed_delay_gov_pol.attr,
1113         &hispeed_freq_gov_pol.attr,
1114         &go_hispeed_load_gov_pol.attr,
1115         &min_sample_time_gov_pol.attr,
1116         &timer_rate_gov_pol.attr,
1117         &timer_slack_gov_pol.attr,
1118         &boost_gov_pol.attr,
1119         &boostpulse_gov_pol.attr,
1120         &boostpulse_duration_gov_pol.attr,
1121         &io_is_busy_gov_pol.attr,
1122         NULL,
1123 };
1124
1125 static struct attribute_group interactive_attr_group_gov_pol = {
1126         .attrs = interactive_attributes_gov_pol,
1127         .name = "interactive",
1128 };
1129
1130 static struct attribute_group *get_sysfs_attr(void)
1131 {
1132         if (have_governor_per_policy())
1133                 return &interactive_attr_group_gov_pol;
1134         else
1135                 return &interactive_attr_group_gov_sys;
1136 }
1137
1138 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
1139                                              unsigned long val,
1140                                              void *data)
1141 {
1142         if (val == IDLE_END)
1143                 cpufreq_interactive_idle_end();
1144
1145         return 0;
1146 }
1147
1148 static struct notifier_block cpufreq_interactive_idle_nb = {
1149         .notifier_call = cpufreq_interactive_idle_notifier,
1150 };
1151
1152 #ifdef CONFIG_ARCH_ROCKCHIP
1153 static void cpufreq_interactive_input_event(struct input_handle *handle,
1154                                             unsigned int type,
1155                                             unsigned int code,
1156                                             int value)
1157 {
1158         u64 now, endtime;
1159         int i;
1160         int anyboost = 0;
1161         unsigned long flags[2];
1162         struct cpufreq_interactive_cpuinfo *pcpu;
1163         struct cpufreq_interactive_tunables *tunables;
1164
1165         if ((type != EV_ABS) && (type != EV_KEY))
1166                 return;
1167
1168         trace_cpufreq_interactive_boost("touch");
1169         spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]);
1170
1171         now = ktime_to_us(ktime_get());
1172         for_each_online_cpu(i) {
1173                 pcpu = &per_cpu(cpuinfo, i);
1174                 if (have_governor_per_policy())
1175                         tunables = pcpu->policy->governor_data;
1176                 else
1177                         tunables = common_tunables;
1178                 if (!tunables)
1179                         continue;
1180
1181                 endtime = now + tunables->touchboostpulse_duration_val;
1182                 if (endtime < (tunables->touchboostpulse_endtime +
1183                                10 * USEC_PER_MSEC))
1184                         continue;
1185                 tunables->touchboostpulse_endtime = endtime;
1186
1187                 spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]);
1188                 if (pcpu->target_freq < tunables->touchboost_freq) {
1189                         pcpu->target_freq = tunables->touchboost_freq;
1190                         cpumask_set_cpu(i, &speedchange_cpumask);
1191                         pcpu->loc_hispeed_val_time =
1192                                         ktime_to_us(ktime_get());
1193                         anyboost = 1;
1194                 }
1195
1196                 pcpu->floor_freq = tunables->touchboost_freq;
1197                 pcpu->loc_floor_val_time = ktime_to_us(ktime_get());
1198
1199                 spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]);
1200         }
1201
1202         spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]);
1203
1204         if (anyboost)
1205                 wake_up_process(speedchange_task);
1206 }
1207
1208 static int cpufreq_interactive_input_connect(struct input_handler *handler,
1209                                              struct input_dev *dev,
1210                                              const struct input_device_id *id)
1211 {
1212         struct input_handle *handle;
1213         int error;
1214
1215         handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1216         if (!handle)
1217                 return -ENOMEM;
1218
1219         handle->dev = dev;
1220         handle->handler = handler;
1221         handle->name = "cpufreq";
1222
1223         error = input_register_handle(handle);
1224         if (error)
1225                 goto err2;
1226
1227         error = input_open_device(handle);
1228         if (error)
1229                 goto err1;
1230
1231         return 0;
1232 err1:
1233         input_unregister_handle(handle);
1234 err2:
1235         kfree(handle);
1236         return error;
1237 }
1238
1239 static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
1240 {
1241         input_close_device(handle);
1242         input_unregister_handle(handle);
1243         kfree(handle);
1244 }
1245
1246 static const struct input_device_id cpufreq_interactive_ids[] = {
1247         {
1248                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1249                         INPUT_DEVICE_ID_MATCH_ABSBIT,
1250                 .evbit = { BIT_MASK(EV_ABS) },
1251                 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
1252                         BIT_MASK(ABS_MT_POSITION_X) |
1253                         BIT_MASK(ABS_MT_POSITION_Y) },
1254         },
1255         {
1256                 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
1257                         INPUT_DEVICE_ID_MATCH_ABSBIT,
1258                 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
1259                 .absbit = { [BIT_WORD(ABS_X)] =
1260                         BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
1261         },
1262         {
1263                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1264                 .evbit = { BIT_MASK(EV_KEY) },
1265         },
1266         { },
1267 };
1268
1269 static struct input_handler cpufreq_interactive_input_handler = {
1270         .event          = cpufreq_interactive_input_event,
1271         .connect        = cpufreq_interactive_input_connect,
1272         .disconnect     = cpufreq_interactive_input_disconnect,
1273         .name           = "cpufreq_interactive",
1274         .id_table       = cpufreq_interactive_ids,
1275 };
1276
1277 static void rockchip_cpufreq_policy_init(struct cpufreq_policy *policy)
1278 {
1279         struct cpufreq_interactive_tunables *tunables = policy->governor_data;
1280
1281         tunables->min_sample_time = 40 * USEC_PER_MSEC;
1282         tunables->boostpulse_duration_val = 40 * USEC_PER_MSEC;
1283         if (policy->cpu == 0) {
1284                 tunables->hispeed_freq = 1008000;
1285                 tunables->touchboostpulse_duration_val = 500 * USEC_PER_MSEC;
1286                 tunables->touchboost_freq = 1200000;
1287         } else {
1288                 tunables->hispeed_freq = 816000;
1289         }
1290 }
1291 #endif
1292
1293 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
1294                 unsigned int event)
1295 {
1296         int rc;
1297         unsigned int j;
1298         struct cpufreq_interactive_cpuinfo *pcpu;
1299         struct cpufreq_frequency_table *freq_table;
1300         struct cpufreq_interactive_tunables *tunables;
1301         unsigned long flags;
1302
1303         if (have_governor_per_policy())
1304                 tunables = policy->governor_data;
1305         else
1306                 tunables = common_tunables;
1307
1308         WARN_ON(!tunables && (event != CPUFREQ_GOV_POLICY_INIT));
1309
1310         switch (event) {
1311         case CPUFREQ_GOV_POLICY_INIT:
1312                 if (have_governor_per_policy()) {
1313                         WARN_ON(tunables);
1314                 } else if (tunables) {
1315                         tunables->usage_count++;
1316                         policy->governor_data = tunables;
1317                         return 0;
1318                 }
1319
1320                 tunables = kzalloc(sizeof(*tunables), GFP_KERNEL);
1321                 if (!tunables) {
1322                         pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
1323                         return -ENOMEM;
1324                 }
1325
1326                 tunables->usage_count = 1;
1327                 tunables->above_hispeed_delay = default_above_hispeed_delay;
1328                 tunables->nabove_hispeed_delay =
1329                         ARRAY_SIZE(default_above_hispeed_delay);
1330                 tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
1331                 tunables->target_loads = default_target_loads;
1332                 tunables->ntarget_loads = ARRAY_SIZE(default_target_loads);
1333                 tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
1334                 tunables->timer_rate = DEFAULT_TIMER_RATE;
1335                 tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
1336                 tunables->timer_slack_val = DEFAULT_TIMER_SLACK;
1337
1338                 spin_lock_init(&tunables->target_loads_lock);
1339                 spin_lock_init(&tunables->above_hispeed_delay_lock);
1340
1341                 policy->governor_data = tunables;
1342                 if (!have_governor_per_policy()) {
1343                         common_tunables = tunables;
1344                 }
1345
1346 #ifdef CONFIG_ARCH_ROCKCHIP
1347                 rockchip_cpufreq_policy_init(policy);
1348 #endif
1349
1350                 rc = sysfs_create_group(get_governor_parent_kobj(policy),
1351                                 get_sysfs_attr());
1352                 if (rc) {
1353                         kfree(tunables);
1354                         policy->governor_data = NULL;
1355                         if (!have_governor_per_policy()) {
1356                                 common_tunables = NULL;
1357                         }
1358                         return rc;
1359                 }
1360
1361                 if (!policy->governor->initialized) {
1362                         idle_notifier_register(&cpufreq_interactive_idle_nb);
1363                         cpufreq_register_notifier(&cpufreq_notifier_block,
1364                                         CPUFREQ_TRANSITION_NOTIFIER);
1365 #ifdef CONFIG_ARCH_ROCKCHIP
1366                         rc = input_register_handler(&cpufreq_interactive_input_handler);
1367 #endif
1368
1369                 }
1370
1371                 break;
1372
1373         case CPUFREQ_GOV_POLICY_EXIT:
1374                 if (!--tunables->usage_count) {
1375                         if (policy->governor->initialized == 1) {
1376 #ifdef CONFIG_ARCH_ROCKCHIP
1377                                 input_unregister_handler(&cpufreq_interactive_input_handler);
1378 #endif
1379                                 cpufreq_unregister_notifier(&cpufreq_notifier_block,
1380                                                 CPUFREQ_TRANSITION_NOTIFIER);
1381                                 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
1382                         }
1383
1384                         sysfs_remove_group(get_governor_parent_kobj(policy),
1385                                         get_sysfs_attr());
1386
1387                         kfree(tunables);
1388                         common_tunables = NULL;
1389                 }
1390
1391                 policy->governor_data = NULL;
1392                 break;
1393
1394         case CPUFREQ_GOV_START:
1395                 mutex_lock(&gov_lock);
1396
1397                 freq_table = cpufreq_frequency_get_table(policy->cpu);
1398                 if (!tunables->hispeed_freq)
1399                         tunables->hispeed_freq = policy->max;
1400
1401                 for_each_cpu(j, policy->cpus) {
1402                         pcpu = &per_cpu(cpuinfo, j);
1403                         pcpu->policy = policy;
1404                         pcpu->target_freq = policy->cur;
1405                         pcpu->freq_table = freq_table;
1406                         pcpu->floor_freq = pcpu->target_freq;
1407                         pcpu->pol_floor_val_time =
1408                                 ktime_to_us(ktime_get());
1409                         pcpu->loc_floor_val_time = pcpu->pol_floor_val_time;
1410                         pcpu->pol_hispeed_val_time = pcpu->pol_floor_val_time;
1411                         pcpu->loc_hispeed_val_time = pcpu->pol_floor_val_time;
1412                         down_write(&pcpu->enable_sem);
1413                         del_timer_sync(&pcpu->cpu_timer);
1414                         del_timer_sync(&pcpu->cpu_slack_timer);
1415                         cpufreq_interactive_timer_start(tunables, j);
1416                         pcpu->governor_enabled = 1;
1417                         up_write(&pcpu->enable_sem);
1418                 }
1419
1420                 mutex_unlock(&gov_lock);
1421                 break;
1422
1423         case CPUFREQ_GOV_STOP:
1424                 mutex_lock(&gov_lock);
1425                 for_each_cpu(j, policy->cpus) {
1426                         pcpu = &per_cpu(cpuinfo, j);
1427                         down_write(&pcpu->enable_sem);
1428                         pcpu->governor_enabled = 0;
1429                         del_timer_sync(&pcpu->cpu_timer);
1430                         del_timer_sync(&pcpu->cpu_slack_timer);
1431                         up_write(&pcpu->enable_sem);
1432                 }
1433
1434                 mutex_unlock(&gov_lock);
1435                 break;
1436
1437         case CPUFREQ_GOV_LIMITS:
1438                 if (policy->max < policy->cur)
1439                         __cpufreq_driver_target(policy,
1440                                         policy->max, CPUFREQ_RELATION_H);
1441                 else if (policy->min > policy->cur)
1442                         __cpufreq_driver_target(policy,
1443                                         policy->min, CPUFREQ_RELATION_L);
1444                 for_each_cpu(j, policy->cpus) {
1445                         pcpu = &per_cpu(cpuinfo, j);
1446
1447                         down_read(&pcpu->enable_sem);
1448                         if (pcpu->governor_enabled == 0) {
1449                                 up_read(&pcpu->enable_sem);
1450                                 continue;
1451                         }
1452
1453                         spin_lock_irqsave(&pcpu->target_freq_lock, flags);
1454                         if (policy->max < pcpu->target_freq)
1455                                 pcpu->target_freq = policy->max;
1456                         else if (policy->min > pcpu->target_freq)
1457                                 pcpu->target_freq = policy->min;
1458
1459                         spin_unlock_irqrestore(&pcpu->target_freq_lock, flags);
1460                         up_read(&pcpu->enable_sem);
1461                 }
1462                 break;
1463         }
1464         return 0;
1465 }
1466
1467 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1468 static
1469 #endif
1470 struct cpufreq_governor cpufreq_gov_interactive = {
1471         .name = "interactive",
1472         .governor = cpufreq_governor_interactive,
1473         .max_transition_latency = 10000000,
1474         .owner = THIS_MODULE,
1475 };
1476
1477 static void cpufreq_interactive_nop_timer(unsigned long data)
1478 {
1479 }
1480
1481 static int __init cpufreq_interactive_init(void)
1482 {
1483         unsigned int i;
1484         struct cpufreq_interactive_cpuinfo *pcpu;
1485         struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
1486
1487         /* Initalize per-cpu timers */
1488         for_each_possible_cpu(i) {
1489                 pcpu = &per_cpu(cpuinfo, i);
1490                 init_timer_deferrable(&pcpu->cpu_timer);
1491                 pcpu->cpu_timer.function = cpufreq_interactive_timer;
1492                 pcpu->cpu_timer.data = i;
1493                 init_timer(&pcpu->cpu_slack_timer);
1494                 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
1495                 spin_lock_init(&pcpu->load_lock);
1496                 spin_lock_init(&pcpu->target_freq_lock);
1497                 init_rwsem(&pcpu->enable_sem);
1498         }
1499
1500         spin_lock_init(&speedchange_cpumask_lock);
1501         mutex_init(&gov_lock);
1502         speedchange_task =
1503                 kthread_create(cpufreq_interactive_speedchange_task, NULL,
1504                                "cfinteractive");
1505         if (IS_ERR(speedchange_task))
1506                 return PTR_ERR(speedchange_task);
1507
1508         sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, &param);
1509         get_task_struct(speedchange_task);
1510
1511         /* NB: wake up so the thread does not look hung to the freezer */
1512         wake_up_process(speedchange_task);
1513
1514         return cpufreq_register_governor(&cpufreq_gov_interactive);
1515 }
1516
1517 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1518 fs_initcall(cpufreq_interactive_init);
1519 #else
1520 module_init(cpufreq_interactive_init);
1521 #endif
1522
1523 static void __exit cpufreq_interactive_exit(void)
1524 {
1525         cpufreq_unregister_governor(&cpufreq_gov_interactive);
1526         kthread_stop(speedchange_task);
1527         put_task_struct(speedchange_task);
1528 }
1529
1530 module_exit(cpufreq_interactive_exit);
1531
1532 MODULE_AUTHOR("Mike Chan <mike@android.com>");
1533 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1534         "Latency sensitive workloads");
1535 MODULE_LICENSE("GPL");