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