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