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