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