blob: ddface4d02c7943f9d0eb9415ecd841ac849afac [file] [log] [blame]
Mike Chan9d49b702010-06-22 11:26:45 -07001/*
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>
Lianwei Wangd4edd5d2012-11-01 09:59:52 +080023#include <linux/moduleparam.h>
Mike Chan9d49b702010-06-22 11:26:45 -070024#include <linux/mutex.h>
25#include <linux/sched.h>
26#include <linux/tick.h>
27#include <linux/time.h>
28#include <linux/timer.h>
29#include <linux/workqueue.h>
30#include <linux/kthread.h>
31#include <linux/mutex.h>
Todd Poynor7820a652012-04-02 17:17:14 -070032#include <linux/slab.h>
Todd Poynor9fb15312012-04-23 20:42:41 -070033#include <asm/cputime.h>
Mike Chan9d49b702010-06-22 11:26:45 -070034
Todd Poynora1e19512012-02-16 16:27:59 -080035#define CREATE_TRACE_POINTS
36#include <trace/events/cpufreq_interactive.h>
37
Mike Chan9d49b702010-06-22 11:26:45 -070038static atomic_t active_count = ATOMIC_INIT(0);
39
40struct cpufreq_interactive_cpuinfo {
41 struct timer_list cpu_timer;
Todd Poynor264e2912012-12-18 17:50:10 -080042 struct timer_list cpu_slack_timer;
Todd Poynor07a9e292012-12-11 16:05:03 -080043 spinlock_t load_lock; /* protects the next 4 fields */
Mike Chan9d49b702010-06-22 11:26:45 -070044 u64 time_in_idle;
Todd Poynorae7f28c2012-10-08 20:14:34 -070045 u64 time_in_idle_timestamp;
Todd Poynor07a9e292012-12-11 16:05:03 -080046 u64 cputime_speedadj;
47 u64 cputime_speedadj_timestamp;
Mike Chan9d49b702010-06-22 11:26:45 -070048 struct cpufreq_policy *policy;
49 struct cpufreq_frequency_table *freq_table;
50 unsigned int target_freq;
Todd Poynoraad27322012-04-26 21:41:40 -070051 unsigned int floor_freq;
52 u64 floor_validate_time;
Todd Poynor5a5aa702012-05-10 23:28:06 -070053 u64 hispeed_validate_time;
Mike Chan9d49b702010-06-22 11:26:45 -070054 int governor_enabled;
55};
56
57static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
58
Todd Poynor02442cf2012-07-16 17:07:15 -070059/* realtime thread handles frequency scaling */
60static struct task_struct *speedchange_task;
61static cpumask_t speedchange_cpumask;
62static spinlock_t speedchange_cpumask_lock;
Mike Chan9d49b702010-06-22 11:26:45 -070063
64/* Hi speed to bump to from lo speed when load burst (default max) */
Todd Poynorf090ef02012-10-03 00:39:56 -070065static unsigned int hispeed_freq;
Mike Chan9d49b702010-06-22 11:26:45 -070066
67/* Go to hi speed when CPU load at or above this value. */
Todd Poynora0ec4362012-04-17 17:39:34 -070068#define DEFAULT_GO_HISPEED_LOAD 85
Mike Chan9d49b702010-06-22 11:26:45 -070069static unsigned long go_hispeed_load;
70
Todd Poynor6ecca112012-11-28 17:58:17 -080071/* Target load. Lower values result in higher CPU speeds. */
72#define DEFAULT_TARGET_LOAD 90
Todd Poynor21df1ca2012-11-14 11:41:21 -080073static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD};
74static spinlock_t target_loads_lock;
75static unsigned int *target_loads = default_target_loads;
76static int ntarget_loads = ARRAY_SIZE(default_target_loads);
Todd Poynor6ecca112012-11-28 17:58:17 -080077
Mike Chan9d49b702010-06-22 11:26:45 -070078/*
79 * The minimum amount of time to spend at a frequency before we can ramp down.
80 */
Todd Poynora0ec4362012-04-17 17:39:34 -070081#define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
Mike Chan9d49b702010-06-22 11:26:45 -070082static unsigned long min_sample_time;
83
84/*
85 * The sample rate of the timer used to increase frequency
86 */
Todd Poynora0ec4362012-04-17 17:39:34 -070087#define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
Mike Chan9d49b702010-06-22 11:26:45 -070088static unsigned long timer_rate;
89
Todd Poynor596cf1f2012-04-13 20:18:02 -070090/*
91 * Wait this long before raising speed above hispeed, by default a single
92 * timer interval.
93 */
94#define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
95static unsigned long above_hispeed_delay_val;
96
Todd Poynor29835472012-12-14 17:31:19 -080097/* Non-zero means indefinite speed boost active */
Todd Poynor9fb15312012-04-23 20:42:41 -070098static int boost_val;
Todd Poynor29835472012-12-14 17:31:19 -080099/* Duration of a boot pulse in usecs */
100static int boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME;
101/* End time of boost pulse in ktime converted to usecs */
102static u64 boostpulse_endtime;
Todd Poynor9fb15312012-04-23 20:42:41 -0700103
Todd Poynor264e2912012-12-18 17:50:10 -0800104/*
105 * Max additional time to wait in idle, beyond timer_rate, at speeds above
106 * minimum before wakeup to reduce speed, or -1 if unnecessary.
107 */
108#define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE)
109static int timer_slack_val = DEFAULT_TIMER_SLACK;
Lianwei Wangd4edd5d2012-11-01 09:59:52 +0800110
Mike Chan9d49b702010-06-22 11:26:45 -0700111static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
112 unsigned int event);
113
114#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
115static
116#endif
117struct cpufreq_governor cpufreq_gov_interactive = {
118 .name = "interactive",
119 .governor = cpufreq_governor_interactive,
120 .max_transition_latency = 10000000,
121 .owner = THIS_MODULE,
122};
123
Todd Poynorae7f28c2012-10-08 20:14:34 -0700124static void cpufreq_interactive_timer_resched(
125 struct cpufreq_interactive_cpuinfo *pcpu)
126{
Todd Poynor264e2912012-12-18 17:50:10 -0800127 unsigned long expires = jiffies + usecs_to_jiffies(timer_rate);
128
129 mod_timer_pinned(&pcpu->cpu_timer, expires);
130 if (timer_slack_val >= 0 && pcpu->target_freq > pcpu->policy->min) {
131 expires += usecs_to_jiffies(timer_slack_val);
132 mod_timer_pinned(&pcpu->cpu_slack_timer, expires);
133 }
134
Todd Poynor07a9e292012-12-11 16:05:03 -0800135 spin_lock(&pcpu->load_lock);
Todd Poynorae7f28c2012-10-08 20:14:34 -0700136 pcpu->time_in_idle =
137 get_cpu_idle_time_us(smp_processor_id(),
138 &pcpu->time_in_idle_timestamp);
Todd Poynor07a9e292012-12-11 16:05:03 -0800139 pcpu->cputime_speedadj = 0;
140 pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp;
141 spin_unlock(&pcpu->load_lock);
Todd Poynorae7f28c2012-10-08 20:14:34 -0700142}
143
Todd Poynor21df1ca2012-11-14 11:41:21 -0800144static unsigned int freq_to_targetload(unsigned int freq)
145{
146 int i;
147 unsigned int ret;
148
149 spin_lock(&target_loads_lock);
150
151 for (i = 0; i < ntarget_loads - 1 && freq >= target_loads[i+1]; i += 2)
152 ;
153
154 ret = target_loads[i];
155 spin_unlock(&target_loads_lock);
156 return ret;
157}
158
159/*
160 * If increasing frequencies never map to a lower target load then
161 * choose_freq() will find the minimum frequency that does not exceed its
162 * target load given the current load.
163 */
164
165static unsigned int choose_freq(
Todd Poynor07a9e292012-12-11 16:05:03 -0800166 struct cpufreq_interactive_cpuinfo *pcpu, unsigned int loadadjfreq)
Todd Poynor21df1ca2012-11-14 11:41:21 -0800167{
168 unsigned int freq = pcpu->policy->cur;
Todd Poynor21df1ca2012-11-14 11:41:21 -0800169 unsigned int prevfreq, freqmin, freqmax;
170 unsigned int tl;
171 int index;
172
173 freqmin = 0;
174 freqmax = UINT_MAX;
175
176 do {
177 prevfreq = freq;
178 tl = freq_to_targetload(freq);
179
180 /*
181 * Find the lowest frequency where the computed load is less
182 * than or equal to the target load.
183 */
184
185 cpufreq_frequency_table_target(
186 pcpu->policy, pcpu->freq_table, loadadjfreq / tl,
187 CPUFREQ_RELATION_L, &index);
188 freq = pcpu->freq_table[index].frequency;
189
190 if (freq > prevfreq) {
191 /* The previous frequency is too low. */
192 freqmin = prevfreq;
193
194 if (freq >= freqmax) {
195 /*
196 * Find the highest frequency that is less
197 * than freqmax.
198 */
199 cpufreq_frequency_table_target(
200 pcpu->policy, pcpu->freq_table,
201 freqmax - 1, CPUFREQ_RELATION_H,
202 &index);
203 freq = pcpu->freq_table[index].frequency;
204
205 if (freq == freqmin) {
206 /*
207 * The first frequency below freqmax
208 * has already been found to be too
209 * low. freqmax is the lowest speed
210 * we found that is fast enough.
211 */
212 freq = freqmax;
213 break;
214 }
215 }
216 } else if (freq < prevfreq) {
217 /* The previous frequency is high enough. */
218 freqmax = prevfreq;
219
220 if (freq <= freqmin) {
221 /*
222 * Find the lowest frequency that is higher
223 * than freqmin.
224 */
225 cpufreq_frequency_table_target(
226 pcpu->policy, pcpu->freq_table,
227 freqmin + 1, CPUFREQ_RELATION_L,
228 &index);
229 freq = pcpu->freq_table[index].frequency;
230
231 /*
232 * If freqmax is the first frequency above
233 * freqmin then we have already found that
234 * this speed is fast enough.
235 */
236 if (freq == freqmax)
237 break;
238 }
239 }
240
241 /* If same frequency chosen as previous then done. */
242 } while (freq != prevfreq);
243
244 return freq;
245}
246
Todd Poynor07a9e292012-12-11 16:05:03 -0800247static u64 update_load(int cpu)
248{
249 struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu);
250 u64 now;
251 u64 now_idle;
252 unsigned int delta_idle;
253 unsigned int delta_time;
254 u64 active_time;
255
256 now_idle = get_cpu_idle_time_us(cpu, &now);
257 delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle);
258 delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp);
259 active_time = delta_time - delta_idle;
260 pcpu->cputime_speedadj += active_time * pcpu->policy->cur;
261
262 pcpu->time_in_idle = now_idle;
263 pcpu->time_in_idle_timestamp = now;
264 return now;
265}
266
Mike Chan9d49b702010-06-22 11:26:45 -0700267static void cpufreq_interactive_timer(unsigned long data)
268{
Todd Poynor1913e0f2012-11-05 13:09:03 -0800269 u64 now;
Mike Chan9d49b702010-06-22 11:26:45 -0700270 unsigned int delta_time;
Todd Poynor07a9e292012-12-11 16:05:03 -0800271 u64 cputime_speedadj;
Mike Chan9d49b702010-06-22 11:26:45 -0700272 int cpu_load;
Mike Chan9d49b702010-06-22 11:26:45 -0700273 struct cpufreq_interactive_cpuinfo *pcpu =
274 &per_cpu(cpuinfo, data);
Mike Chan9d49b702010-06-22 11:26:45 -0700275 unsigned int new_freq;
Todd Poynor07a9e292012-12-11 16:05:03 -0800276 unsigned int loadadjfreq;
Mike Chan9d49b702010-06-22 11:26:45 -0700277 unsigned int index;
278 unsigned long flags;
Todd Poynor29835472012-12-14 17:31:19 -0800279 bool boosted;
Mike Chan9d49b702010-06-22 11:26:45 -0700280
281 smp_rmb();
282
283 if (!pcpu->governor_enabled)
284 goto exit;
285
Todd Poynor07a9e292012-12-11 16:05:03 -0800286 spin_lock(&pcpu->load_lock);
287 now = update_load(data);
288 delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp);
289 cputime_speedadj = pcpu->cputime_speedadj;
290 spin_unlock(&pcpu->load_lock);
Mike Chan9d49b702010-06-22 11:26:45 -0700291
Todd Poynor07a9e292012-12-11 16:05:03 -0800292 if (WARN_ON_ONCE(!delta_time))
Mike Chan9d49b702010-06-22 11:26:45 -0700293 goto rearm;
294
Todd Poynor07a9e292012-12-11 16:05:03 -0800295 do_div(cputime_speedadj, delta_time);
296 loadadjfreq = (unsigned int)cputime_speedadj * 100;
297 cpu_load = loadadjfreq / pcpu->target_freq;
Todd Poynor29835472012-12-14 17:31:19 -0800298 boosted = boost_val || now < boostpulse_endtime;
Mike Chan9d49b702010-06-22 11:26:45 -0700299
Todd Poynor29835472012-12-14 17:31:19 -0800300 if ((cpu_load >= go_hispeed_load || boosted) &&
Todd Poynor67f07522012-11-08 15:06:55 -0800301 pcpu->target_freq < hispeed_freq)
302 new_freq = hispeed_freq;
303 else
Todd Poynor07a9e292012-12-11 16:05:03 -0800304 new_freq = choose_freq(pcpu, loadadjfreq);
Todd Poynor67f07522012-11-08 15:06:55 -0800305
306 if (pcpu->target_freq >= hispeed_freq &&
307 new_freq > pcpu->target_freq &&
308 now - pcpu->hispeed_validate_time < above_hispeed_delay_val) {
309 trace_cpufreq_interactive_notyet(
310 data, cpu_load, pcpu->target_freq,
311 pcpu->policy->cur, new_freq);
312 goto rearm;
Mike Chan9d49b702010-06-22 11:26:45 -0700313 }
314
Todd Poynor67f07522012-11-08 15:06:55 -0800315 pcpu->hispeed_validate_time = now;
Todd Poynor5a5aa702012-05-10 23:28:06 -0700316
Mike Chan9d49b702010-06-22 11:26:45 -0700317 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
Todd Poynor6ecca112012-11-28 17:58:17 -0800318 new_freq, CPUFREQ_RELATION_L,
Mike Chan9d49b702010-06-22 11:26:45 -0700319 &index)) {
320 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
321 (int) data);
322 goto rearm;
323 }
324
325 new_freq = pcpu->freq_table[index].frequency;
326
Mike Chan9d49b702010-06-22 11:26:45 -0700327 /*
Todd Poynoraad27322012-04-26 21:41:40 -0700328 * Do not scale below floor_freq unless we have been at or above the
329 * floor frequency for the minimum sample time since last validated.
Mike Chan9d49b702010-06-22 11:26:45 -0700330 */
Todd Poynoraad27322012-04-26 21:41:40 -0700331 if (new_freq < pcpu->floor_freq) {
Todd Poynor1913e0f2012-11-05 13:09:03 -0800332 if (now - pcpu->floor_validate_time < min_sample_time) {
Todd Poynor27f7b8e2012-11-28 17:56:09 -0800333 trace_cpufreq_interactive_notyet(
334 data, cpu_load, pcpu->target_freq,
335 pcpu->policy->cur, new_freq);
Mike Chan9d49b702010-06-22 11:26:45 -0700336 goto rearm;
Todd Poynora1e19512012-02-16 16:27:59 -0800337 }
Mike Chan9d49b702010-06-22 11:26:45 -0700338 }
339
Todd Poynor29835472012-12-14 17:31:19 -0800340 /*
341 * Update the timestamp for checking whether speed has been held at
342 * or above the selected frequency for a minimum of min_sample_time,
343 * if not boosted to hispeed_freq. If boosted to hispeed_freq then we
344 * allow the speed to drop as soon as the boostpulse duration expires
345 * (or the indefinite boost is turned off).
346 */
347
348 if (!boosted || new_freq > hispeed_freq) {
349 pcpu->floor_freq = new_freq;
350 pcpu->floor_validate_time = now;
351 }
Todd Poynor0a92d482012-04-06 19:59:36 -0700352
353 if (pcpu->target_freq == new_freq) {
Todd Poynor27f7b8e2012-11-28 17:56:09 -0800354 trace_cpufreq_interactive_already(
355 data, cpu_load, pcpu->target_freq,
356 pcpu->policy->cur, new_freq);
Todd Poynor0a92d482012-04-06 19:59:36 -0700357 goto rearm_if_notmax;
358 }
359
Todd Poynora1e19512012-02-16 16:27:59 -0800360 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
Todd Poynor27f7b8e2012-11-28 17:56:09 -0800361 pcpu->policy->cur, new_freq);
Todd Poynora1e19512012-02-16 16:27:59 -0800362
Todd Poynor02442cf2012-07-16 17:07:15 -0700363 pcpu->target_freq = new_freq;
364 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
365 cpumask_set_cpu(data, &speedchange_cpumask);
366 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
367 wake_up_process(speedchange_task);
Mike Chan9d49b702010-06-22 11:26:45 -0700368
369rearm_if_notmax:
370 /*
371 * Already set max speed and don't see a need to change that,
372 * wait until next idle to re-evaluate, don't need timer.
373 */
374 if (pcpu->target_freq == pcpu->policy->max)
375 goto exit;
376
377rearm:
Todd Poynor264e2912012-12-18 17:50:10 -0800378 if (!timer_pending(&pcpu->cpu_timer))
Todd Poynorae7f28c2012-10-08 20:14:34 -0700379 cpufreq_interactive_timer_resched(pcpu);
Mike Chan9d49b702010-06-22 11:26:45 -0700380
381exit:
382 return;
383}
384
385static void cpufreq_interactive_idle_start(void)
386{
387 struct cpufreq_interactive_cpuinfo *pcpu =
388 &per_cpu(cpuinfo, smp_processor_id());
389 int pending;
390
391 if (!pcpu->governor_enabled)
392 return;
393
Mike Chan9d49b702010-06-22 11:26:45 -0700394 pending = timer_pending(&pcpu->cpu_timer);
395
396 if (pcpu->target_freq != pcpu->policy->min) {
Mike Chan9d49b702010-06-22 11:26:45 -0700397 /*
398 * Entering idle while not at lowest speed. On some
399 * platforms this can hold the other CPU(s) at that speed
400 * even though the CPU is idle. Set a timer to re-evaluate
401 * speed so this idle CPU doesn't hold the other CPUs above
402 * min indefinitely. This should probably be a quirk of
403 * the CPUFreq driver.
404 */
Todd Poynor264e2912012-12-18 17:50:10 -0800405 if (!pending)
Todd Poynorae7f28c2012-10-08 20:14:34 -0700406 cpufreq_interactive_timer_resched(pcpu);
Mike Chan9d49b702010-06-22 11:26:45 -0700407 }
408
409}
410
411static void cpufreq_interactive_idle_end(void)
412{
413 struct cpufreq_interactive_cpuinfo *pcpu =
414 &per_cpu(cpuinfo, smp_processor_id());
415
Sam Lefflera04e4412012-06-27 10:12:04 -0700416 if (!pcpu->governor_enabled)
417 return;
418
Todd Poynor1913e0f2012-11-05 13:09:03 -0800419 /* Arm the timer for 1-2 ticks later if not already. */
420 if (!timer_pending(&pcpu->cpu_timer)) {
Todd Poynorae7f28c2012-10-08 20:14:34 -0700421 cpufreq_interactive_timer_resched(pcpu);
Todd Poynor264e2912012-12-18 17:50:10 -0800422 } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) {
Todd Poynorae7f28c2012-10-08 20:14:34 -0700423 del_timer(&pcpu->cpu_timer);
Todd Poynor264e2912012-12-18 17:50:10 -0800424 del_timer(&pcpu->cpu_slack_timer);
Todd Poynorae7f28c2012-10-08 20:14:34 -0700425 cpufreq_interactive_timer(smp_processor_id());
Mike Chan9d49b702010-06-22 11:26:45 -0700426 }
Mike Chan9d49b702010-06-22 11:26:45 -0700427}
428
Todd Poynor02442cf2012-07-16 17:07:15 -0700429static int cpufreq_interactive_speedchange_task(void *data)
Mike Chan9d49b702010-06-22 11:26:45 -0700430{
431 unsigned int cpu;
432 cpumask_t tmp_mask;
433 unsigned long flags;
434 struct cpufreq_interactive_cpuinfo *pcpu;
435
436 while (1) {
437 set_current_state(TASK_INTERRUPTIBLE);
Todd Poynor02442cf2012-07-16 17:07:15 -0700438 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
Mike Chan9d49b702010-06-22 11:26:45 -0700439
Todd Poynor02442cf2012-07-16 17:07:15 -0700440 if (cpumask_empty(&speedchange_cpumask)) {
441 spin_unlock_irqrestore(&speedchange_cpumask_lock,
442 flags);
Mike Chan9d49b702010-06-22 11:26:45 -0700443 schedule();
444
445 if (kthread_should_stop())
446 break;
447
Todd Poynor02442cf2012-07-16 17:07:15 -0700448 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
Mike Chan9d49b702010-06-22 11:26:45 -0700449 }
450
451 set_current_state(TASK_RUNNING);
Todd Poynor02442cf2012-07-16 17:07:15 -0700452 tmp_mask = speedchange_cpumask;
453 cpumask_clear(&speedchange_cpumask);
454 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
Mike Chan9d49b702010-06-22 11:26:45 -0700455
456 for_each_cpu(cpu, &tmp_mask) {
457 unsigned int j;
458 unsigned int max_freq = 0;
459
460 pcpu = &per_cpu(cpuinfo, cpu);
461 smp_rmb();
462
463 if (!pcpu->governor_enabled)
464 continue;
465
Mike Chan9d49b702010-06-22 11:26:45 -0700466 for_each_cpu(j, pcpu->policy->cpus) {
467 struct cpufreq_interactive_cpuinfo *pjcpu =
468 &per_cpu(cpuinfo, j);
469
470 if (pjcpu->target_freq > max_freq)
471 max_freq = pjcpu->target_freq;
472 }
473
474 if (max_freq != pcpu->policy->cur)
475 __cpufreq_driver_target(pcpu->policy,
476 max_freq,
477 CPUFREQ_RELATION_H);
Todd Poynor02442cf2012-07-16 17:07:15 -0700478 trace_cpufreq_interactive_setspeed(cpu,
479 pcpu->target_freq,
Todd Poynora1e19512012-02-16 16:27:59 -0800480 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700481 }
482 }
483
484 return 0;
485}
486
Todd Poynor7820a652012-04-02 17:17:14 -0700487static void cpufreq_interactive_boost(void)
488{
489 int i;
490 int anyboost = 0;
491 unsigned long flags;
492 struct cpufreq_interactive_cpuinfo *pcpu;
493
Todd Poynor02442cf2012-07-16 17:07:15 -0700494 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
Todd Poynor7820a652012-04-02 17:17:14 -0700495
496 for_each_online_cpu(i) {
497 pcpu = &per_cpu(cpuinfo, i);
498
499 if (pcpu->target_freq < hispeed_freq) {
500 pcpu->target_freq = hispeed_freq;
Todd Poynor02442cf2012-07-16 17:07:15 -0700501 cpumask_set_cpu(i, &speedchange_cpumask);
Todd Poynorc9d53b32012-12-07 20:08:45 -0800502 pcpu->hispeed_validate_time =
503 ktime_to_us(ktime_get());
Todd Poynor7820a652012-04-02 17:17:14 -0700504 anyboost = 1;
505 }
506
507 /*
Todd Poynoraad27322012-04-26 21:41:40 -0700508 * Set floor freq and (re)start timer for when last
509 * validated.
Todd Poynor7820a652012-04-02 17:17:14 -0700510 */
511
Todd Poynoraad27322012-04-26 21:41:40 -0700512 pcpu->floor_freq = hispeed_freq;
513 pcpu->floor_validate_time = ktime_to_us(ktime_get());
Todd Poynor7820a652012-04-02 17:17:14 -0700514 }
515
Todd Poynor02442cf2012-07-16 17:07:15 -0700516 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
Todd Poynor7820a652012-04-02 17:17:14 -0700517
518 if (anyboost)
Todd Poynor02442cf2012-07-16 17:07:15 -0700519 wake_up_process(speedchange_task);
Todd Poynor7820a652012-04-02 17:17:14 -0700520}
521
Todd Poynor07a9e292012-12-11 16:05:03 -0800522static int cpufreq_interactive_notifier(
523 struct notifier_block *nb, unsigned long val, void *data)
524{
525 struct cpufreq_freqs *freq = data;
526 struct cpufreq_interactive_cpuinfo *pcpu;
527 int cpu;
528
529 if (val == CPUFREQ_POSTCHANGE) {
530 pcpu = &per_cpu(cpuinfo, freq->cpu);
531
532 for_each_cpu(cpu, pcpu->policy->cpus) {
533 struct cpufreq_interactive_cpuinfo *pjcpu =
534 &per_cpu(cpuinfo, cpu);
535 spin_lock(&pjcpu->load_lock);
536 update_load(cpu);
537 spin_unlock(&pjcpu->load_lock);
538 }
539 }
540
541 return 0;
542}
543
544static struct notifier_block cpufreq_notifier_block = {
545 .notifier_call = cpufreq_interactive_notifier,
546};
547
Todd Poynor21df1ca2012-11-14 11:41:21 -0800548static ssize_t show_target_loads(
Todd Poynor6ecca112012-11-28 17:58:17 -0800549 struct kobject *kobj, struct attribute *attr, char *buf)
550{
Todd Poynor21df1ca2012-11-14 11:41:21 -0800551 int i;
552 ssize_t ret = 0;
553
554 spin_lock(&target_loads_lock);
555
556 for (i = 0; i < ntarget_loads; i++)
557 ret += sprintf(buf + ret, "%u%s", target_loads[i],
558 i & 0x1 ? ":" : " ");
559
560 ret += sprintf(buf + ret, "\n");
561 spin_unlock(&target_loads_lock);
562 return ret;
Todd Poynor6ecca112012-11-28 17:58:17 -0800563}
564
Todd Poynor21df1ca2012-11-14 11:41:21 -0800565static ssize_t store_target_loads(
Todd Poynor6ecca112012-11-28 17:58:17 -0800566 struct kobject *kobj, struct attribute *attr, const char *buf,
567 size_t count)
568{
569 int ret;
Todd Poynor21df1ca2012-11-14 11:41:21 -0800570 const char *cp;
571 unsigned int *new_target_loads = NULL;
572 int ntokens = 1;
573 int i;
Todd Poynor6ecca112012-11-28 17:58:17 -0800574
Todd Poynor21df1ca2012-11-14 11:41:21 -0800575 cp = buf;
576 while ((cp = strpbrk(cp + 1, " :")))
577 ntokens++;
578
579 if (!(ntokens & 0x1))
580 goto err_inval;
581
582 new_target_loads = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL);
583 if (!new_target_loads) {
584 ret = -ENOMEM;
585 goto err;
586 }
587
588 cp = buf;
589 i = 0;
590 while (i < ntokens) {
591 if (sscanf(cp, "%u", &new_target_loads[i++]) != 1)
592 goto err_inval;
593
594 cp = strpbrk(cp, " :");
595 if (!cp)
596 break;
597 cp++;
598 }
599
600 if (i != ntokens)
601 goto err_inval;
602
603 spin_lock(&target_loads_lock);
604 if (target_loads != default_target_loads)
605 kfree(target_loads);
606 target_loads = new_target_loads;
607 ntarget_loads = ntokens;
608 spin_unlock(&target_loads_lock);
Todd Poynor6ecca112012-11-28 17:58:17 -0800609 return count;
Todd Poynor21df1ca2012-11-14 11:41:21 -0800610
611err_inval:
612 ret = -EINVAL;
613err:
614 kfree(new_target_loads);
615 return ret;
Todd Poynor6ecca112012-11-28 17:58:17 -0800616}
617
Todd Poynor21df1ca2012-11-14 11:41:21 -0800618static struct global_attr target_loads_attr =
619 __ATTR(target_loads, S_IRUGO | S_IWUSR,
620 show_target_loads, store_target_loads);
Todd Poynor6ecca112012-11-28 17:58:17 -0800621
Mike Chan9d49b702010-06-22 11:26:45 -0700622static ssize_t show_hispeed_freq(struct kobject *kobj,
623 struct attribute *attr, char *buf)
624{
Todd Poynorf090ef02012-10-03 00:39:56 -0700625 return sprintf(buf, "%u\n", hispeed_freq);
Mike Chan9d49b702010-06-22 11:26:45 -0700626}
627
628static ssize_t store_hispeed_freq(struct kobject *kobj,
629 struct attribute *attr, const char *buf,
630 size_t count)
631{
632 int ret;
Todd Poynorf090ef02012-10-03 00:39:56 -0700633 long unsigned int val;
Mike Chan9d49b702010-06-22 11:26:45 -0700634
Todd Poynorf090ef02012-10-03 00:39:56 -0700635 ret = strict_strtoul(buf, 0, &val);
Mike Chan9d49b702010-06-22 11:26:45 -0700636 if (ret < 0)
637 return ret;
638 hispeed_freq = val;
639 return count;
640}
641
642static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
643 show_hispeed_freq, store_hispeed_freq);
644
645
646static ssize_t show_go_hispeed_load(struct kobject *kobj,
647 struct attribute *attr, char *buf)
648{
649 return sprintf(buf, "%lu\n", go_hispeed_load);
650}
651
652static ssize_t store_go_hispeed_load(struct kobject *kobj,
653 struct attribute *attr, const char *buf, size_t count)
654{
655 int ret;
656 unsigned long val;
657
658 ret = strict_strtoul(buf, 0, &val);
659 if (ret < 0)
660 return ret;
661 go_hispeed_load = val;
662 return count;
663}
664
665static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
666 show_go_hispeed_load, store_go_hispeed_load);
667
668static ssize_t show_min_sample_time(struct kobject *kobj,
669 struct attribute *attr, char *buf)
670{
671 return sprintf(buf, "%lu\n", min_sample_time);
672}
673
674static ssize_t store_min_sample_time(struct kobject *kobj,
675 struct attribute *attr, const char *buf, size_t count)
676{
677 int ret;
678 unsigned long val;
679
680 ret = strict_strtoul(buf, 0, &val);
681 if (ret < 0)
682 return ret;
683 min_sample_time = val;
684 return count;
685}
686
687static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
688 show_min_sample_time, store_min_sample_time);
689
Todd Poynor596cf1f2012-04-13 20:18:02 -0700690static ssize_t show_above_hispeed_delay(struct kobject *kobj,
691 struct attribute *attr, char *buf)
692{
693 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
694}
695
696static ssize_t store_above_hispeed_delay(struct kobject *kobj,
697 struct attribute *attr,
698 const char *buf, size_t count)
699{
700 int ret;
701 unsigned long val;
702
703 ret = strict_strtoul(buf, 0, &val);
704 if (ret < 0)
705 return ret;
706 above_hispeed_delay_val = val;
707 return count;
708}
709
710define_one_global_rw(above_hispeed_delay);
711
Mike Chan9d49b702010-06-22 11:26:45 -0700712static ssize_t show_timer_rate(struct kobject *kobj,
713 struct attribute *attr, char *buf)
714{
715 return sprintf(buf, "%lu\n", timer_rate);
716}
717
718static ssize_t store_timer_rate(struct kobject *kobj,
719 struct attribute *attr, const char *buf, size_t count)
720{
721 int ret;
722 unsigned long val;
723
724 ret = strict_strtoul(buf, 0, &val);
725 if (ret < 0)
726 return ret;
727 timer_rate = val;
728 return count;
729}
730
731static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
732 show_timer_rate, store_timer_rate);
733
Todd Poynor264e2912012-12-18 17:50:10 -0800734static ssize_t show_timer_slack(
735 struct kobject *kobj, struct attribute *attr, char *buf)
736{
737 return sprintf(buf, "%d\n", timer_slack_val);
738}
739
740static ssize_t store_timer_slack(
741 struct kobject *kobj, struct attribute *attr, const char *buf,
742 size_t count)
743{
744 int ret;
745 unsigned long val;
746
747 ret = kstrtol(buf, 10, &val);
748 if (ret < 0)
749 return ret;
750
751 timer_slack_val = val;
752 return count;
753}
754
755define_one_global_rw(timer_slack);
756
Todd Poynor9fb15312012-04-23 20:42:41 -0700757static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
758 char *buf)
759{
760 return sprintf(buf, "%d\n", boost_val);
761}
762
763static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
764 const char *buf, size_t count)
765{
766 int ret;
767 unsigned long val;
768
769 ret = kstrtoul(buf, 0, &val);
770 if (ret < 0)
771 return ret;
772
773 boost_val = val;
774
Todd Poynor2e739a02012-05-03 00:16:55 -0700775 if (boost_val) {
776 trace_cpufreq_interactive_boost("on");
Todd Poynor9fb15312012-04-23 20:42:41 -0700777 cpufreq_interactive_boost();
Todd Poynor2e739a02012-05-03 00:16:55 -0700778 } else {
779 trace_cpufreq_interactive_unboost("off");
780 }
Todd Poynor9fb15312012-04-23 20:42:41 -0700781
782 return count;
783}
784
785define_one_global_rw(boost);
786
Todd Poynor2e739a02012-05-03 00:16:55 -0700787static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
788 const char *buf, size_t count)
789{
790 int ret;
791 unsigned long val;
792
793 ret = kstrtoul(buf, 0, &val);
794 if (ret < 0)
795 return ret;
796
Todd Poynor29835472012-12-14 17:31:19 -0800797 boostpulse_endtime = ktime_to_us(ktime_get()) + boostpulse_duration_val;
Todd Poynor2e739a02012-05-03 00:16:55 -0700798 trace_cpufreq_interactive_boost("pulse");
799 cpufreq_interactive_boost();
800 return count;
801}
802
803static struct global_attr boostpulse =
804 __ATTR(boostpulse, 0200, NULL, store_boostpulse);
805
Todd Poynor29835472012-12-14 17:31:19 -0800806static ssize_t show_boostpulse_duration(
807 struct kobject *kobj, struct attribute *attr, char *buf)
808{
809 return sprintf(buf, "%d\n", boostpulse_duration_val);
810}
811
812static ssize_t store_boostpulse_duration(
813 struct kobject *kobj, struct attribute *attr, const char *buf,
814 size_t count)
815{
816 int ret;
817 unsigned long val;
818
819 ret = kstrtoul(buf, 0, &val);
820 if (ret < 0)
821 return ret;
822
823 boostpulse_duration_val = val;
824 return count;
825}
826
827define_one_global_rw(boostpulse_duration);
828
Mike Chan9d49b702010-06-22 11:26:45 -0700829static struct attribute *interactive_attributes[] = {
Todd Poynor21df1ca2012-11-14 11:41:21 -0800830 &target_loads_attr.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700831 &hispeed_freq_attr.attr,
832 &go_hispeed_load_attr.attr,
Todd Poynor596cf1f2012-04-13 20:18:02 -0700833 &above_hispeed_delay.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700834 &min_sample_time_attr.attr,
835 &timer_rate_attr.attr,
Todd Poynor264e2912012-12-18 17:50:10 -0800836 &timer_slack.attr,
Todd Poynor9fb15312012-04-23 20:42:41 -0700837 &boost.attr,
Todd Poynor2e739a02012-05-03 00:16:55 -0700838 &boostpulse.attr,
Todd Poynor29835472012-12-14 17:31:19 -0800839 &boostpulse_duration.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700840 NULL,
841};
842
843static struct attribute_group interactive_attr_group = {
844 .attrs = interactive_attributes,
845 .name = "interactive",
846};
847
Sam Lefflera04e4412012-06-27 10:12:04 -0700848static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
849 unsigned long val,
850 void *data)
851{
852 switch (val) {
853 case IDLE_START:
854 cpufreq_interactive_idle_start();
855 break;
856 case IDLE_END:
857 cpufreq_interactive_idle_end();
858 break;
859 }
860
861 return 0;
862}
863
864static struct notifier_block cpufreq_interactive_idle_nb = {
865 .notifier_call = cpufreq_interactive_idle_notifier,
866};
867
Mike Chan9d49b702010-06-22 11:26:45 -0700868static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
869 unsigned int event)
870{
871 int rc;
872 unsigned int j;
873 struct cpufreq_interactive_cpuinfo *pcpu;
874 struct cpufreq_frequency_table *freq_table;
875
876 switch (event) {
877 case CPUFREQ_GOV_START:
878 if (!cpu_online(policy->cpu))
879 return -EINVAL;
880
881 freq_table =
882 cpufreq_frequency_get_table(policy->cpu);
Todd Poynor1913e0f2012-11-05 13:09:03 -0800883 if (!hispeed_freq)
884 hispeed_freq = policy->max;
Mike Chan9d49b702010-06-22 11:26:45 -0700885
886 for_each_cpu(j, policy->cpus) {
Todd Poynor264e2912012-12-18 17:50:10 -0800887 unsigned long expires;
888
Mike Chan9d49b702010-06-22 11:26:45 -0700889 pcpu = &per_cpu(cpuinfo, j);
890 pcpu->policy = policy;
891 pcpu->target_freq = policy->cur;
892 pcpu->freq_table = freq_table;
Todd Poynoraad27322012-04-26 21:41:40 -0700893 pcpu->floor_freq = pcpu->target_freq;
894 pcpu->floor_validate_time =
Todd Poynorc9d53b32012-12-07 20:08:45 -0800895 ktime_to_us(ktime_get());
Todd Poynor5a5aa702012-05-10 23:28:06 -0700896 pcpu->hispeed_validate_time =
Todd Poynorc9d53b32012-12-07 20:08:45 -0800897 pcpu->floor_validate_time;
Mike Chan9d49b702010-06-22 11:26:45 -0700898 pcpu->governor_enabled = 1;
899 smp_wmb();
Todd Poynor264e2912012-12-18 17:50:10 -0800900 expires = jiffies + usecs_to_jiffies(timer_rate);
901 pcpu->cpu_timer.expires = expires;
Todd Poynor1913e0f2012-11-05 13:09:03 -0800902 add_timer_on(&pcpu->cpu_timer, j);
Todd Poynor264e2912012-12-18 17:50:10 -0800903
904 if (timer_slack_val >= 0) {
905 expires += usecs_to_jiffies(timer_slack_val);
906 pcpu->cpu_slack_timer.expires = expires;
907 add_timer_on(&pcpu->cpu_slack_timer, j);
908 }
Mike Chan9d49b702010-06-22 11:26:45 -0700909 }
910
Mike Chan9d49b702010-06-22 11:26:45 -0700911 /*
912 * Do not register the idle hook and create sysfs
913 * entries if we have already done so.
914 */
915 if (atomic_inc_return(&active_count) > 1)
916 return 0;
917
918 rc = sysfs_create_group(cpufreq_global_kobject,
919 &interactive_attr_group);
920 if (rc)
921 return rc;
922
Sam Lefflera04e4412012-06-27 10:12:04 -0700923 idle_notifier_register(&cpufreq_interactive_idle_nb);
Todd Poynor07a9e292012-12-11 16:05:03 -0800924 cpufreq_register_notifier(
925 &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
Mike Chan9d49b702010-06-22 11:26:45 -0700926 break;
927
928 case CPUFREQ_GOV_STOP:
929 for_each_cpu(j, policy->cpus) {
930 pcpu = &per_cpu(cpuinfo, j);
931 pcpu->governor_enabled = 0;
932 smp_wmb();
933 del_timer_sync(&pcpu->cpu_timer);
Todd Poynor264e2912012-12-18 17:50:10 -0800934 del_timer_sync(&pcpu->cpu_slack_timer);
Mike Chan9d49b702010-06-22 11:26:45 -0700935 }
936
Mike Chan9d49b702010-06-22 11:26:45 -0700937 if (atomic_dec_return(&active_count) > 0)
938 return 0;
939
Todd Poynor07a9e292012-12-11 16:05:03 -0800940 cpufreq_unregister_notifier(
941 &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
Sam Lefflera04e4412012-06-27 10:12:04 -0700942 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
Mike Chan9d49b702010-06-22 11:26:45 -0700943 sysfs_remove_group(cpufreq_global_kobject,
944 &interactive_attr_group);
945
946 break;
947
948 case CPUFREQ_GOV_LIMITS:
949 if (policy->max < policy->cur)
950 __cpufreq_driver_target(policy,
951 policy->max, CPUFREQ_RELATION_H);
952 else if (policy->min > policy->cur)
953 __cpufreq_driver_target(policy,
954 policy->min, CPUFREQ_RELATION_L);
955 break;
956 }
957 return 0;
958}
959
Todd Poynor264e2912012-12-18 17:50:10 -0800960static void cpufreq_interactive_nop_timer(unsigned long data)
961{
962}
963
Mike Chan9d49b702010-06-22 11:26:45 -0700964static int __init cpufreq_interactive_init(void)
965{
966 unsigned int i;
967 struct cpufreq_interactive_cpuinfo *pcpu;
968 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
969
970 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
971 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700972 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
Mike Chan9d49b702010-06-22 11:26:45 -0700973 timer_rate = DEFAULT_TIMER_RATE;
974
975 /* Initalize per-cpu timers */
976 for_each_possible_cpu(i) {
977 pcpu = &per_cpu(cpuinfo, i);
Todd Poynor264e2912012-12-18 17:50:10 -0800978 init_timer_deferrable(&pcpu->cpu_timer);
Mike Chan9d49b702010-06-22 11:26:45 -0700979 pcpu->cpu_timer.function = cpufreq_interactive_timer;
980 pcpu->cpu_timer.data = i;
Todd Poynor264e2912012-12-18 17:50:10 -0800981 init_timer(&pcpu->cpu_slack_timer);
982 pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer;
Todd Poynor07a9e292012-12-11 16:05:03 -0800983 spin_lock_init(&pcpu->load_lock);
Mike Chan9d49b702010-06-22 11:26:45 -0700984 }
985
Todd Poynor21df1ca2012-11-14 11:41:21 -0800986 spin_lock_init(&target_loads_lock);
Todd Poynor02442cf2012-07-16 17:07:15 -0700987 spin_lock_init(&speedchange_cpumask_lock);
988 speedchange_task =
989 kthread_create(cpufreq_interactive_speedchange_task, NULL,
990 "cfinteractive");
991 if (IS_ERR(speedchange_task))
992 return PTR_ERR(speedchange_task);
Sam Lefflera13f4152012-06-27 12:55:56 -0700993
Todd Poynor02442cf2012-07-16 17:07:15 -0700994 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, &param);
995 get_task_struct(speedchange_task);
Mike Chan9d49b702010-06-22 11:26:45 -0700996
Sam Lefflera13f4152012-06-27 12:55:56 -0700997 /* NB: wake up so the thread does not look hung to the freezer */
Todd Poynor02442cf2012-07-16 17:07:15 -0700998 wake_up_process(speedchange_task);
Sam Lefflera13f4152012-06-27 12:55:56 -0700999
Mike Chan9d49b702010-06-22 11:26:45 -07001000 return cpufreq_register_governor(&cpufreq_gov_interactive);
Mike Chan9d49b702010-06-22 11:26:45 -07001001}
1002
1003#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
1004fs_initcall(cpufreq_interactive_init);
1005#else
1006module_init(cpufreq_interactive_init);
1007#endif
1008
1009static void __exit cpufreq_interactive_exit(void)
1010{
1011 cpufreq_unregister_governor(&cpufreq_gov_interactive);
Todd Poynor02442cf2012-07-16 17:07:15 -07001012 kthread_stop(speedchange_task);
1013 put_task_struct(speedchange_task);
Mike Chan9d49b702010-06-22 11:26:45 -07001014}
1015
1016module_exit(cpufreq_interactive_exit);
1017
1018MODULE_AUTHOR("Mike Chan <mike@android.com>");
1019MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1020 "Latency sensitive workloads");
1021MODULE_LICENSE("GPL");