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