cpufreq: interactive: set floor for boosted speed
[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/mutex.h>
24 #include <linux/sched.h>
25 #include <linux/sched/rt.h>
26 #include <linux/tick.h>
27 #include <linux/time.h>
28 #include <linux/timer.h>
29 #include <linux/workqueue.h>
30 #include <linux/kthread.h>
31 #include <linux/mutex.h>
32 #include <linux/slab.h>
33 #include <linux/input.h>
34 #include <asm/cputime.h>
35
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/cpufreq_interactive.h>
38
39 static atomic_t active_count = ATOMIC_INIT(0);
40
41 struct cpufreq_interactive_cpuinfo {
42         struct timer_list cpu_timer;
43         int timer_idlecancel;
44         u64 time_in_idle;
45         u64 idle_exit_time;
46         u64 timer_run_time;
47         int idling;
48         u64 target_set_time;
49         u64 target_set_time_in_idle;
50         struct cpufreq_policy *policy;
51         struct cpufreq_frequency_table *freq_table;
52         unsigned int target_freq;
53         unsigned int floor_freq;
54         u64 floor_validate_time;
55         int governor_enabled;
56 };
57
58 static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
59
60 /* Workqueues handle frequency scaling */
61 static struct task_struct *up_task;
62 static struct workqueue_struct *down_wq;
63 static struct work_struct freq_scale_down_work;
64 static cpumask_t up_cpumask;
65 static spinlock_t up_cpumask_lock;
66 static cpumask_t down_cpumask;
67 static spinlock_t down_cpumask_lock;
68 static struct mutex set_speed_lock;
69
70 /* Hi speed to bump to from lo speed when load burst (default max) */
71 static u64 hispeed_freq;
72
73 /* Go to hi speed when CPU load at or above this value. */
74 #define DEFAULT_GO_HISPEED_LOAD 85
75 static unsigned long go_hispeed_load;
76
77 /*
78  * The minimum amount of time to spend at a frequency before we can ramp down.
79  */
80 #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
81 static unsigned long min_sample_time;
82
83 /*
84  * The sample rate of the timer used to increase frequency
85  */
86 #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
87 static unsigned long timer_rate;
88
89 /*
90  * Wait this long before raising speed above hispeed, by default a single
91  * timer interval.
92  */
93 #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
94 static unsigned long above_hispeed_delay_val;
95
96 /*
97  * Boost pulse to hispeed on touchscreen input.
98  */
99
100 static int input_boost_val;
101
102 struct cpufreq_interactive_inputopen {
103         struct input_handle *handle;
104         struct work_struct inputopen_work;
105 };
106
107 static struct cpufreq_interactive_inputopen inputopen;
108
109 /*
110  * Non-zero means longer-term speed boost active.
111  */
112
113 static int boost_val;
114
115 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
116                 unsigned int event);
117
118 #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
119 static
120 #endif
121 struct cpufreq_governor cpufreq_gov_interactive = {
122         .name = "interactive",
123         .governor = cpufreq_governor_interactive,
124         .max_transition_latency = 10000000,
125         .owner = THIS_MODULE,
126 };
127
128 static void cpufreq_interactive_timer(unsigned long data)
129 {
130         unsigned int delta_idle;
131         unsigned int delta_time;
132         int cpu_load;
133         int load_since_change;
134         u64 time_in_idle;
135         u64 idle_exit_time;
136         struct cpufreq_interactive_cpuinfo *pcpu =
137                 &per_cpu(cpuinfo, data);
138         u64 now_idle;
139         unsigned int new_freq;
140         unsigned int index;
141         unsigned long flags;
142
143         smp_rmb();
144
145         if (!pcpu->governor_enabled)
146                 goto exit;
147
148         /*
149          * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
150          * this lets idle exit know the current idle time sample has
151          * been processed, and idle exit can generate a new sample and
152          * re-arm the timer.  This prevents a concurrent idle
153          * exit on that CPU from writing a new set of info at the same time
154          * the timer function runs (the timer function can't use that info
155          * until more time passes).
156          */
157         time_in_idle = pcpu->time_in_idle;
158         idle_exit_time = pcpu->idle_exit_time;
159         now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
160         smp_wmb();
161
162         /* If we raced with cancelling a timer, skip. */
163         if (!idle_exit_time)
164                 goto exit;
165
166         delta_idle = (unsigned int)(now_idle - time_in_idle);
167         delta_time = (unsigned int)(pcpu->timer_run_time - idle_exit_time);
168
169         /*
170          * If timer ran less than 1ms after short-term sample started, retry.
171          */
172         if (delta_time < 1000)
173                 goto rearm;
174
175         if (delta_idle > delta_time)
176                 cpu_load = 0;
177         else
178                 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
179
180         delta_idle = (unsigned int)(now_idle - pcpu->target_set_time_in_idle);
181         delta_time = (unsigned int)(pcpu->timer_run_time -
182                                     pcpu->target_set_time);
183
184         if ((delta_time == 0) || (delta_idle > delta_time))
185                 load_since_change = 0;
186         else
187                 load_since_change =
188                         100 * (delta_time - delta_idle) / delta_time;
189
190         /*
191          * Choose greater of short-term load (since last idle timer
192          * started or timer function re-armed itself) or long-term load
193          * (since last frequency change).
194          */
195         if (load_since_change > cpu_load)
196                 cpu_load = load_since_change;
197
198         if (cpu_load >= go_hispeed_load || boost_val) {
199                 if (pcpu->target_freq <= pcpu->policy->min) {
200                         new_freq = hispeed_freq;
201                 } else {
202                         new_freq = pcpu->policy->max * cpu_load / 100;
203
204                         if (new_freq < hispeed_freq)
205                                 new_freq = hispeed_freq;
206
207                         if (pcpu->target_freq == hispeed_freq &&
208                             new_freq > hispeed_freq &&
209                             pcpu->timer_run_time - pcpu->target_set_time
210                             < above_hispeed_delay_val) {
211                                 trace_cpufreq_interactive_notyet(data, cpu_load,
212                                                                  pcpu->target_freq,
213                                                                  new_freq);
214                                 goto rearm;
215                         }
216                 }
217         } else {
218                 new_freq = pcpu->policy->max * cpu_load / 100;
219         }
220
221         if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
222                                            new_freq, CPUFREQ_RELATION_H,
223                                            &index)) {
224                 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
225                              (int) data);
226                 goto rearm;
227         }
228
229         new_freq = pcpu->freq_table[index].frequency;
230
231         /*
232          * Do not scale below floor_freq unless we have been at or above the
233          * floor frequency for the minimum sample time since last validated.
234          */
235         if (new_freq < pcpu->floor_freq) {
236                 if (pcpu->timer_run_time - pcpu->target_validate_time
237                     < min_sample_time) {
238                         trace_cpufreq_interactive_notyet(data, cpu_load,
239                                          pcpu->target_freq, new_freq);
240                         goto rearm;
241                 }
242         }
243
244         pcpu->floor_freq = new_freq;
245         pcpu->floor_validate_time = pcpu->timer_run_time;
246
247         if (pcpu->target_freq == new_freq) {
248                 trace_cpufreq_interactive_already(data, cpu_load,
249                                                   pcpu->target_freq, new_freq);
250                 goto rearm_if_notmax;
251         }
252
253         trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
254                                          new_freq);
255         pcpu->target_set_time_in_idle = now_idle;
256         pcpu->target_set_time = pcpu->timer_run_time;
257
258         if (new_freq < pcpu->target_freq) {
259                 pcpu->target_freq = new_freq;
260                 spin_lock_irqsave(&down_cpumask_lock, flags);
261                 cpumask_set_cpu(data, &down_cpumask);
262                 spin_unlock_irqrestore(&down_cpumask_lock, flags);
263                 queue_work(down_wq, &freq_scale_down_work);
264         } else {
265                 pcpu->target_freq = new_freq;
266                 spin_lock_irqsave(&up_cpumask_lock, flags);
267                 cpumask_set_cpu(data, &up_cpumask);
268                 spin_unlock_irqrestore(&up_cpumask_lock, flags);
269                 wake_up_process(up_task);
270         }
271
272 rearm_if_notmax:
273         /*
274          * Already set max speed and don't see a need to change that,
275          * wait until next idle to re-evaluate, don't need timer.
276          */
277         if (pcpu->target_freq == pcpu->policy->max)
278                 goto exit;
279
280 rearm:
281         if (!timer_pending(&pcpu->cpu_timer)) {
282                 /*
283                  * If already at min: if that CPU is idle, don't set timer.
284                  * Else cancel the timer if that CPU goes idle.  We don't
285                  * need to re-evaluate speed until the next idle exit.
286                  */
287                 if (pcpu->target_freq == pcpu->policy->min) {
288                         smp_rmb();
289
290                         if (pcpu->idling)
291                                 goto exit;
292
293                         pcpu->timer_idlecancel = 1;
294                 }
295
296                 pcpu->time_in_idle = get_cpu_idle_time_us(
297                         data, &pcpu->idle_exit_time);
298                 mod_timer(&pcpu->cpu_timer,
299                           jiffies + usecs_to_jiffies(timer_rate));
300         }
301
302 exit:
303         return;
304 }
305
306 static void cpufreq_interactive_idle_start(void)
307 {
308         struct cpufreq_interactive_cpuinfo *pcpu =
309                 &per_cpu(cpuinfo, smp_processor_id());
310         int pending;
311
312         if (!pcpu->governor_enabled)
313                 return;
314
315         pcpu->idling = 1;
316         smp_wmb();
317         pending = timer_pending(&pcpu->cpu_timer);
318
319         if (pcpu->target_freq != pcpu->policy->min) {
320 #ifdef CONFIG_SMP
321                 /*
322                  * Entering idle while not at lowest speed.  On some
323                  * platforms this can hold the other CPU(s) at that speed
324                  * even though the CPU is idle. Set a timer to re-evaluate
325                  * speed so this idle CPU doesn't hold the other CPUs above
326                  * min indefinitely.  This should probably be a quirk of
327                  * the CPUFreq driver.
328                  */
329                 if (!pending) {
330                         pcpu->time_in_idle = get_cpu_idle_time_us(
331                                 smp_processor_id(), &pcpu->idle_exit_time);
332                         pcpu->timer_idlecancel = 0;
333                         mod_timer(&pcpu->cpu_timer,
334                                   jiffies + usecs_to_jiffies(timer_rate));
335                 }
336 #endif
337         } else {
338                 /*
339                  * If at min speed and entering idle after load has
340                  * already been evaluated, and a timer has been set just in
341                  * case the CPU suddenly goes busy, cancel that timer.  The
342                  * CPU didn't go busy; we'll recheck things upon idle exit.
343                  */
344                 if (pending && pcpu->timer_idlecancel) {
345                         del_timer(&pcpu->cpu_timer);
346                         /*
347                          * Ensure last timer run time is after current idle
348                          * sample start time, so next idle exit will always
349                          * start a new idle sampling period.
350                          */
351                         pcpu->idle_exit_time = 0;
352                         pcpu->timer_idlecancel = 0;
353                 }
354         }
355
356 }
357
358 static void cpufreq_interactive_idle_end(void)
359 {
360         struct cpufreq_interactive_cpuinfo *pcpu =
361                 &per_cpu(cpuinfo, smp_processor_id());
362
363         pcpu->idling = 0;
364         smp_wmb();
365
366         /*
367          * Arm the timer for 1-2 ticks later if not already, and if the timer
368          * function has already processed the previous load sampling
369          * interval.  (If the timer is not pending but has not processed
370          * the previous interval, it is probably racing with us on another
371          * CPU.  Let it compute load based on the previous sample and then
372          * re-arm the timer for another interval when it's done, rather
373          * than updating the interval start time to be "now", which doesn't
374          * give the timer function enough time to make a decision on this
375          * run.)
376          */
377         if (timer_pending(&pcpu->cpu_timer) == 0 &&
378             pcpu->timer_run_time >= pcpu->idle_exit_time &&
379             pcpu->governor_enabled) {
380                 pcpu->time_in_idle =
381                         get_cpu_idle_time_us(smp_processor_id(),
382                                              &pcpu->idle_exit_time);
383                 pcpu->timer_idlecancel = 0;
384                 mod_timer(&pcpu->cpu_timer,
385                           jiffies + usecs_to_jiffies(timer_rate));
386         }
387
388 }
389
390 static int cpufreq_interactive_up_task(void *data)
391 {
392         unsigned int cpu;
393         cpumask_t tmp_mask;
394         unsigned long flags;
395         struct cpufreq_interactive_cpuinfo *pcpu;
396
397         while (1) {
398                 set_current_state(TASK_INTERRUPTIBLE);
399                 spin_lock_irqsave(&up_cpumask_lock, flags);
400
401                 if (cpumask_empty(&up_cpumask)) {
402                         spin_unlock_irqrestore(&up_cpumask_lock, flags);
403                         schedule();
404
405                         if (kthread_should_stop())
406                                 break;
407
408                         spin_lock_irqsave(&up_cpumask_lock, flags);
409                 }
410
411                 set_current_state(TASK_RUNNING);
412                 tmp_mask = up_cpumask;
413                 cpumask_clear(&up_cpumask);
414                 spin_unlock_irqrestore(&up_cpumask_lock, flags);
415
416                 for_each_cpu(cpu, &tmp_mask) {
417                         unsigned int j;
418                         unsigned int max_freq = 0;
419
420                         pcpu = &per_cpu(cpuinfo, cpu);
421                         smp_rmb();
422
423                         if (!pcpu->governor_enabled)
424                                 continue;
425
426                         mutex_lock(&set_speed_lock);
427
428                         for_each_cpu(j, pcpu->policy->cpus) {
429                                 struct cpufreq_interactive_cpuinfo *pjcpu =
430                                         &per_cpu(cpuinfo, j);
431
432                                 if (pjcpu->target_freq > max_freq)
433                                         max_freq = pjcpu->target_freq;
434                         }
435
436                         if (max_freq != pcpu->policy->cur)
437                                 __cpufreq_driver_target(pcpu->policy,
438                                                         max_freq,
439                                                         CPUFREQ_RELATION_H);
440                         mutex_unlock(&set_speed_lock);
441                         trace_cpufreq_interactive_up(cpu, pcpu->target_freq,
442                                                      pcpu->policy->cur);
443                 }
444         }
445
446         return 0;
447 }
448
449 static void cpufreq_interactive_freq_down(struct work_struct *work)
450 {
451         unsigned int cpu;
452         cpumask_t tmp_mask;
453         unsigned long flags;
454         struct cpufreq_interactive_cpuinfo *pcpu;
455
456         spin_lock_irqsave(&down_cpumask_lock, flags);
457         tmp_mask = down_cpumask;
458         cpumask_clear(&down_cpumask);
459         spin_unlock_irqrestore(&down_cpumask_lock, flags);
460
461         for_each_cpu(cpu, &tmp_mask) {
462                 unsigned int j;
463                 unsigned int max_freq = 0;
464
465                 pcpu = &per_cpu(cpuinfo, cpu);
466                 smp_rmb();
467
468                 if (!pcpu->governor_enabled)
469                         continue;
470
471                 mutex_lock(&set_speed_lock);
472
473                 for_each_cpu(j, pcpu->policy->cpus) {
474                         struct cpufreq_interactive_cpuinfo *pjcpu =
475                                 &per_cpu(cpuinfo, j);
476
477                         if (pjcpu->target_freq > max_freq)
478                                 max_freq = pjcpu->target_freq;
479                 }
480
481                 if (max_freq != pcpu->policy->cur)
482                         __cpufreq_driver_target(pcpu->policy, max_freq,
483                                                 CPUFREQ_RELATION_H);
484
485                 mutex_unlock(&set_speed_lock);
486                 trace_cpufreq_interactive_down(cpu, pcpu->target_freq,
487                                                pcpu->policy->cur);
488         }
489 }
490
491 static void cpufreq_interactive_boost(void)
492 {
493         int i;
494         int anyboost = 0;
495         unsigned long flags;
496         struct cpufreq_interactive_cpuinfo *pcpu;
497
498         trace_cpufreq_interactive_boost(hispeed_freq);
499         spin_lock_irqsave(&up_cpumask_lock, flags);
500
501         for_each_online_cpu(i) {
502                 pcpu = &per_cpu(cpuinfo, i);
503
504                 if (pcpu->target_freq < hispeed_freq) {
505                         pcpu->target_freq = hispeed_freq;
506                         cpumask_set_cpu(i, &up_cpumask);
507                         pcpu->target_set_time_in_idle =
508                                 get_cpu_idle_time_us(i, &pcpu->target_set_time);
509                         anyboost = 1;
510                 }
511
512                 /*
513                  * Set floor freq and (re)start timer for when last
514                  * validated.
515                  */
516
517                 pcpu->floor_freq = hispeed_freq;
518                 pcpu->floor_validate_time = ktime_to_us(ktime_get());
519         }
520
521         spin_unlock_irqrestore(&up_cpumask_lock, flags);
522
523         if (anyboost)
524                 wake_up_process(up_task);
525 }
526
527 /*
528  * Pulsed boost on input event raises CPUs to hispeed_freq and lets
529  * usual algorithm of min_sample_time  decide when to allow speed
530  * to drop.
531  */
532
533 static void cpufreq_interactive_input_event(struct input_handle *handle,
534                                             unsigned int type,
535                                             unsigned int code, int value)
536 {
537         if (input_boost_val && type == EV_SYN && code == SYN_REPORT)
538                 cpufreq_interactive_boost();
539 }
540
541 static void cpufreq_interactive_input_open(struct work_struct *w)
542 {
543         struct cpufreq_interactive_inputopen *io =
544                 container_of(w, struct cpufreq_interactive_inputopen,
545                              inputopen_work);
546         int error;
547
548         error = input_open_device(io->handle);
549         if (error)
550                 input_unregister_handle(io->handle);
551 }
552
553 static int cpufreq_interactive_input_connect(struct input_handler *handler,
554                                              struct input_dev *dev,
555                                              const struct input_device_id *id)
556 {
557         struct input_handle *handle;
558         int error;
559
560         pr_info("%s: connect to %s\n", __func__, dev->name);
561         handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
562         if (!handle)
563                 return -ENOMEM;
564
565         handle->dev = dev;
566         handle->handler = handler;
567         handle->name = "cpufreq_interactive";
568
569         error = input_register_handle(handle);
570         if (error)
571                 goto err;
572
573         inputopen.handle = handle;
574         queue_work(down_wq, &inputopen.inputopen_work);
575         return 0;
576 err:
577         kfree(handle);
578         return error;
579 }
580
581 static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
582 {
583         input_close_device(handle);
584         input_unregister_handle(handle);
585         kfree(handle);
586 }
587
588 static const struct input_device_id cpufreq_interactive_ids[] = {
589         {
590                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
591                          INPUT_DEVICE_ID_MATCH_ABSBIT,
592                 .evbit = { BIT_MASK(EV_ABS) },
593                 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
594                             BIT_MASK(ABS_MT_POSITION_X) |
595                             BIT_MASK(ABS_MT_POSITION_Y) },
596         }, /* multi-touch touchscreen */
597         {
598                 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
599                          INPUT_DEVICE_ID_MATCH_ABSBIT,
600                 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
601                 .absbit = { [BIT_WORD(ABS_X)] =
602                             BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
603         }, /* touchpad */
604         { },
605 };
606
607 static struct input_handler cpufreq_interactive_input_handler = {
608         .event          = cpufreq_interactive_input_event,
609         .connect        = cpufreq_interactive_input_connect,
610         .disconnect     = cpufreq_interactive_input_disconnect,
611         .name           = "cpufreq_interactive",
612         .id_table       = cpufreq_interactive_ids,
613 };
614
615 static ssize_t show_hispeed_freq(struct kobject *kobj,
616                                  struct attribute *attr, char *buf)
617 {
618         return sprintf(buf, "%llu\n", hispeed_freq);
619 }
620
621 static ssize_t store_hispeed_freq(struct kobject *kobj,
622                                   struct attribute *attr, const char *buf,
623                                   size_t count)
624 {
625         int ret;
626         u64 val;
627
628         ret = strict_strtoull(buf, 0, &val);
629         if (ret < 0)
630                 return ret;
631         hispeed_freq = val;
632         return count;
633 }
634
635 static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
636                 show_hispeed_freq, store_hispeed_freq);
637
638
639 static ssize_t show_go_hispeed_load(struct kobject *kobj,
640                                      struct attribute *attr, char *buf)
641 {
642         return sprintf(buf, "%lu\n", go_hispeed_load);
643 }
644
645 static ssize_t store_go_hispeed_load(struct kobject *kobj,
646                         struct attribute *attr, const char *buf, size_t count)
647 {
648         int ret;
649         unsigned long val;
650
651         ret = strict_strtoul(buf, 0, &val);
652         if (ret < 0)
653                 return ret;
654         go_hispeed_load = val;
655         return count;
656 }
657
658 static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
659                 show_go_hispeed_load, store_go_hispeed_load);
660
661 static ssize_t show_min_sample_time(struct kobject *kobj,
662                                 struct attribute *attr, char *buf)
663 {
664         return sprintf(buf, "%lu\n", min_sample_time);
665 }
666
667 static ssize_t store_min_sample_time(struct kobject *kobj,
668                         struct attribute *attr, const char *buf, size_t count)
669 {
670         int ret;
671         unsigned long val;
672
673         ret = strict_strtoul(buf, 0, &val);
674         if (ret < 0)
675                 return ret;
676         min_sample_time = val;
677         return count;
678 }
679
680 static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
681                 show_min_sample_time, store_min_sample_time);
682
683 static ssize_t show_above_hispeed_delay(struct kobject *kobj,
684                                         struct attribute *attr, char *buf)
685 {
686         return sprintf(buf, "%lu\n", above_hispeed_delay_val);
687 }
688
689 static ssize_t store_above_hispeed_delay(struct kobject *kobj,
690                                          struct attribute *attr,
691                                          const char *buf, size_t count)
692 {
693         int ret;
694         unsigned long val;
695
696         ret = strict_strtoul(buf, 0, &val);
697         if (ret < 0)
698                 return ret;
699         above_hispeed_delay_val = val;
700         return count;
701 }
702
703 define_one_global_rw(above_hispeed_delay);
704
705 static ssize_t show_timer_rate(struct kobject *kobj,
706                         struct attribute *attr, char *buf)
707 {
708         return sprintf(buf, "%lu\n", timer_rate);
709 }
710
711 static ssize_t store_timer_rate(struct kobject *kobj,
712                         struct attribute *attr, const char *buf, size_t count)
713 {
714         int ret;
715         unsigned long val;
716
717         ret = strict_strtoul(buf, 0, &val);
718         if (ret < 0)
719                 return ret;
720         timer_rate = val;
721         return count;
722 }
723
724 static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
725                 show_timer_rate, store_timer_rate);
726
727 static ssize_t show_input_boost(struct kobject *kobj, struct attribute *attr,
728                                 char *buf)
729 {
730         return sprintf(buf, "%u\n", input_boost_val);
731 }
732
733 static ssize_t store_input_boost(struct kobject *kobj, struct attribute *attr,
734                                  const char *buf, size_t count)
735 {
736         int ret;
737         unsigned long val;
738
739         ret = strict_strtoul(buf, 0, &val);
740         if (ret < 0)
741                 return ret;
742         input_boost_val = val;
743         return count;
744 }
745
746 define_one_global_rw(input_boost);
747
748 static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
749                           char *buf)
750 {
751         return sprintf(buf, "%d\n", boost_val);
752 }
753
754 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
755                            const char *buf, size_t count)
756 {
757         int ret;
758         unsigned long val;
759
760         ret = kstrtoul(buf, 0, &val);
761         if (ret < 0)
762                 return ret;
763
764         boost_val = val;
765
766         if (boost_val)
767                 cpufreq_interactive_boost();
768         else
769                 trace_cpufreq_interactive_unboost(hispeed_freq);
770
771         return count;
772 }
773
774 define_one_global_rw(boost);
775
776 static struct attribute *interactive_attributes[] = {
777         &hispeed_freq_attr.attr,
778         &go_hispeed_load_attr.attr,
779         &above_hispeed_delay.attr,
780         &min_sample_time_attr.attr,
781         &timer_rate_attr.attr,
782         &input_boost.attr,
783         &boost.attr,
784         NULL,
785 };
786
787 static struct attribute_group interactive_attr_group = {
788         .attrs = interactive_attributes,
789         .name = "interactive",
790 };
791
792 static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
793                 unsigned int event)
794 {
795         int rc;
796         unsigned int j;
797         struct cpufreq_interactive_cpuinfo *pcpu;
798         struct cpufreq_frequency_table *freq_table;
799
800         switch (event) {
801         case CPUFREQ_GOV_START:
802                 if (!cpu_online(policy->cpu))
803                         return -EINVAL;
804
805                 freq_table =
806                         cpufreq_frequency_get_table(policy->cpu);
807
808                 for_each_cpu(j, policy->cpus) {
809                         pcpu = &per_cpu(cpuinfo, j);
810                         pcpu->policy = policy;
811                         pcpu->target_freq = policy->cur;
812                         pcpu->freq_table = freq_table;
813                         pcpu->target_set_time_in_idle =
814                                 get_cpu_idle_time_us(j,
815                                              &pcpu->target_set_time);
816                         pcpu->floor_freq = pcpu->target_freq;
817                         pcpu->floor_validate_time =
818                                 pcpu->target_set_time;
819                         pcpu->governor_enabled = 1;
820                         smp_wmb();
821                 }
822
823                 if (!hispeed_freq)
824                         hispeed_freq = policy->max;
825
826                 /*
827                  * Do not register the idle hook and create sysfs
828                  * entries if we have already done so.
829                  */
830                 if (atomic_inc_return(&active_count) > 1)
831                         return 0;
832
833                 rc = sysfs_create_group(cpufreq_global_kobject,
834                                 &interactive_attr_group);
835                 if (rc)
836                         return rc;
837
838                 rc = input_register_handler(&cpufreq_interactive_input_handler);
839                 if (rc)
840                         pr_warn("%s: failed to register input handler\n",
841                                 __func__);
842
843                 break;
844
845         case CPUFREQ_GOV_STOP:
846                 for_each_cpu(j, policy->cpus) {
847                         pcpu = &per_cpu(cpuinfo, j);
848                         pcpu->governor_enabled = 0;
849                         smp_wmb();
850                         del_timer_sync(&pcpu->cpu_timer);
851
852                         /*
853                          * Reset idle exit time since we may cancel the timer
854                          * before it can run after the last idle exit time,
855                          * to avoid tripping the check in idle exit for a timer
856                          * that is trying to run.
857                          */
858                         pcpu->idle_exit_time = 0;
859                 }
860
861                 flush_work(&freq_scale_down_work);
862                 if (atomic_dec_return(&active_count) > 0)
863                         return 0;
864
865                 input_unregister_handler(&cpufreq_interactive_input_handler);
866                 sysfs_remove_group(cpufreq_global_kobject,
867                                 &interactive_attr_group);
868
869                 break;
870
871         case CPUFREQ_GOV_LIMITS:
872                 if (policy->max < policy->cur)
873                         __cpufreq_driver_target(policy,
874                                         policy->max, CPUFREQ_RELATION_H);
875                 else if (policy->min > policy->cur)
876                         __cpufreq_driver_target(policy,
877                                         policy->min, CPUFREQ_RELATION_L);
878                 break;
879         }
880         return 0;
881 }
882
883 static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
884                                              unsigned long val,
885                                              void *data)
886 {
887         switch (val) {
888         case IDLE_START:
889                 cpufreq_interactive_idle_start();
890                 break;
891         case IDLE_END:
892                 cpufreq_interactive_idle_end();
893                 break;
894         }
895
896         return 0;
897 }
898
899 static struct notifier_block cpufreq_interactive_idle_nb = {
900         .notifier_call = cpufreq_interactive_idle_notifier,
901 };
902
903 static int __init cpufreq_interactive_init(void)
904 {
905         unsigned int i;
906         struct cpufreq_interactive_cpuinfo *pcpu;
907         struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
908
909         go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
910         min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
911         above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
912         timer_rate = DEFAULT_TIMER_RATE;
913
914         /* Initalize per-cpu timers */
915         for_each_possible_cpu(i) {
916                 pcpu = &per_cpu(cpuinfo, i);
917                 init_timer(&pcpu->cpu_timer);
918                 pcpu->cpu_timer.function = cpufreq_interactive_timer;
919                 pcpu->cpu_timer.data = i;
920         }
921
922         up_task = kthread_create(cpufreq_interactive_up_task, NULL,
923                                  "kinteractiveup");
924         if (IS_ERR(up_task))
925                 return PTR_ERR(up_task);
926
927         sched_setscheduler_nocheck(up_task, SCHED_FIFO, &param);
928         get_task_struct(up_task);
929
930         /* No rescuer thread, bind to CPU queuing the work for possibly
931            warm cache (probably doesn't matter much). */
932         down_wq = alloc_workqueue("knteractive_down", 0, 1);
933
934         if (!down_wq)
935                 goto err_freeuptask;
936
937         INIT_WORK(&freq_scale_down_work,
938                   cpufreq_interactive_freq_down);
939
940         spin_lock_init(&up_cpumask_lock);
941         spin_lock_init(&down_cpumask_lock);
942         mutex_init(&set_speed_lock);
943
944         idle_notifier_register(&cpufreq_interactive_idle_nb);
945         INIT_WORK(&inputopen.inputopen_work, cpufreq_interactive_input_open);
946         return cpufreq_register_governor(&cpufreq_gov_interactive);
947
948 err_freeuptask:
949         put_task_struct(up_task);
950         return -ENOMEM;
951 }
952
953 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
954 fs_initcall(cpufreq_interactive_init);
955 #else
956 module_init(cpufreq_interactive_init);
957 #endif
958
959 static void __exit cpufreq_interactive_exit(void)
960 {
961         cpufreq_unregister_governor(&cpufreq_gov_interactive);
962         kthread_stop(up_task);
963         put_task_struct(up_task);
964         destroy_workqueue(down_wq);
965 }
966
967 module_exit(cpufreq_interactive_exit);
968
969 MODULE_AUTHOR("Mike Chan <mike@android.com>");
970 MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
971         "Latency sensitive workloads");
972 MODULE_LICENSE("GPL");