blob: 63cdc6812defe031edfcb6d200a200c4648f80ab [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>
23#include <linux/mutex.h>
24#include <linux/sched.h>
25#include <linux/tick.h>
26#include <linux/time.h>
27#include <linux/timer.h>
28#include <linux/workqueue.h>
29#include <linux/kthread.h>
30#include <linux/mutex.h>
Todd Poynor7820a652012-04-02 17:17:14 -070031#include <linux/slab.h>
32#include <linux/input.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;
42 int timer_idlecancel;
43 u64 time_in_idle;
44 u64 idle_exit_time;
45 u64 timer_run_time;
46 int idling;
Todd Poynor0a92d482012-04-06 19:59:36 -070047 u64 target_set_time;
48 u64 target_set_time_in_idle;
Mike Chan9d49b702010-06-22 11:26:45 -070049 struct cpufreq_policy *policy;
50 struct cpufreq_frequency_table *freq_table;
51 unsigned int target_freq;
Todd Poynoraad27322012-04-26 21:41:40 -070052 unsigned int floor_freq;
53 u64 floor_validate_time;
Todd Poynor5a5aa702012-05-10 23:28:06 -070054 u64 hispeed_validate_time;
Mike Chan9d49b702010-06-22 11:26:45 -070055 int governor_enabled;
56};
57
58static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
59
60/* Workqueues handle frequency scaling */
61static struct task_struct *up_task;
62static struct workqueue_struct *down_wq;
63static struct work_struct freq_scale_down_work;
64static cpumask_t up_cpumask;
65static spinlock_t up_cpumask_lock;
66static cpumask_t down_cpumask;
67static spinlock_t down_cpumask_lock;
68static struct mutex set_speed_lock;
69
70/* Hi speed to bump to from lo speed when load burst (default max) */
71static u64 hispeed_freq;
72
73/* Go to hi speed when CPU load at or above this value. */
Todd Poynora0ec4362012-04-17 17:39:34 -070074#define DEFAULT_GO_HISPEED_LOAD 85
Mike Chan9d49b702010-06-22 11:26:45 -070075static unsigned long go_hispeed_load;
76
77/*
78 * The minimum amount of time to spend at a frequency before we can ramp down.
79 */
Todd Poynora0ec4362012-04-17 17:39:34 -070080#define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
Mike Chan9d49b702010-06-22 11:26:45 -070081static unsigned long min_sample_time;
82
83/*
84 * The sample rate of the timer used to increase frequency
85 */
Todd Poynora0ec4362012-04-17 17:39:34 -070086#define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
Mike Chan9d49b702010-06-22 11:26:45 -070087static unsigned long timer_rate;
88
Todd Poynor596cf1f2012-04-13 20:18:02 -070089/*
90 * Wait this long before raising speed above hispeed, by default a single
91 * timer interval.
92 */
93#define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
94static unsigned long above_hispeed_delay_val;
95
Todd Poynor7820a652012-04-02 17:17:14 -070096/*
Todd Poynor9fb15312012-04-23 20:42:41 -070097 * Boost pulse to hispeed on touchscreen input.
Todd Poynor7820a652012-04-02 17:17:14 -070098 */
99
100static int input_boost_val;
101
102struct cpufreq_interactive_inputopen {
103 struct input_handle *handle;
104 struct work_struct inputopen_work;
105};
106
107static struct cpufreq_interactive_inputopen inputopen;
108
Todd Poynor9fb15312012-04-23 20:42:41 -0700109/*
110 * Non-zero means longer-term speed boost active.
111 */
112
113static int boost_val;
114
Mike Chan9d49b702010-06-22 11:26:45 -0700115static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
116 unsigned int event);
117
118#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
119static
120#endif
121struct cpufreq_governor cpufreq_gov_interactive = {
122 .name = "interactive",
123 .governor = cpufreq_governor_interactive,
124 .max_transition_latency = 10000000,
125 .owner = THIS_MODULE,
126};
127
128static void cpufreq_interactive_timer(unsigned long data)
129{
130 unsigned int delta_idle;
131 unsigned int delta_time;
132 int cpu_load;
133 int load_since_change;
134 u64 time_in_idle;
135 u64 idle_exit_time;
136 struct cpufreq_interactive_cpuinfo *pcpu =
137 &per_cpu(cpuinfo, data);
138 u64 now_idle;
139 unsigned int new_freq;
140 unsigned int index;
141 unsigned long flags;
142
143 smp_rmb();
144
145 if (!pcpu->governor_enabled)
146 goto exit;
147
148 /*
149 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
150 * this lets idle exit know the current idle time sample has
151 * been processed, and idle exit can generate a new sample and
152 * re-arm the timer. This prevents a concurrent idle
153 * exit on that CPU from writing a new set of info at the same time
154 * the timer function runs (the timer function can't use that info
155 * until more time passes).
156 */
157 time_in_idle = pcpu->time_in_idle;
158 idle_exit_time = pcpu->idle_exit_time;
159 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
160 smp_wmb();
161
162 /* If we raced with cancelling a timer, skip. */
163 if (!idle_exit_time)
164 goto exit;
165
166 delta_idle = (unsigned int)(now_idle - time_in_idle);
167 delta_time = (unsigned int)(pcpu->timer_run_time - idle_exit_time);
168
169 /*
170 * If timer ran less than 1ms after short-term sample started, retry.
171 */
172 if (delta_time < 1000)
173 goto rearm;
174
175 if (delta_idle > delta_time)
176 cpu_load = 0;
177 else
178 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
179
Todd Poynor0a92d482012-04-06 19:59:36 -0700180 delta_idle = (unsigned int)(now_idle - pcpu->target_set_time_in_idle);
181 delta_time = (unsigned int)(pcpu->timer_run_time -
182 pcpu->target_set_time);
Mike Chan9d49b702010-06-22 11:26:45 -0700183
184 if ((delta_time == 0) || (delta_idle > delta_time))
185 load_since_change = 0;
186 else
187 load_since_change =
188 100 * (delta_time - delta_idle) / delta_time;
189
190 /*
191 * Choose greater of short-term load (since last idle timer
192 * started or timer function re-armed itself) or long-term load
193 * (since last frequency change).
194 */
195 if (load_since_change > cpu_load)
196 cpu_load = load_since_change;
197
Todd Poynor9fb15312012-04-23 20:42:41 -0700198 if (cpu_load >= go_hispeed_load || boost_val) {
Todd Poynor730910b2012-04-19 12:52:48 -0700199 if (pcpu->target_freq <= pcpu->policy->min) {
Mike Chan9d49b702010-06-22 11:26:45 -0700200 new_freq = hispeed_freq;
Todd Poynor8dc352c2012-04-06 19:50:12 -0700201 } else {
Mike Chan9d49b702010-06-22 11:26:45 -0700202 new_freq = pcpu->policy->max * cpu_load / 100;
Todd Poynor8dc352c2012-04-06 19:50:12 -0700203
204 if (new_freq < hispeed_freq)
205 new_freq = hispeed_freq;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700206
207 if (pcpu->target_freq == hispeed_freq &&
208 new_freq > hispeed_freq &&
Todd Poynor5a5aa702012-05-10 23:28:06 -0700209 pcpu->timer_run_time - pcpu->hispeed_validate_time
Todd Poynor596cf1f2012-04-13 20:18:02 -0700210 < above_hispeed_delay_val) {
211 trace_cpufreq_interactive_notyet(data, cpu_load,
212 pcpu->target_freq,
213 new_freq);
214 goto rearm;
215 }
Todd Poynor8dc352c2012-04-06 19:50:12 -0700216 }
Mike Chan9d49b702010-06-22 11:26:45 -0700217 } else {
Todd Poynor1f53ef22012-04-06 01:13:09 -0700218 new_freq = pcpu->policy->max * cpu_load / 100;
Mike Chan9d49b702010-06-22 11:26:45 -0700219 }
220
Todd Poynor5a5aa702012-05-10 23:28:06 -0700221 if (new_freq <= hispeed_freq)
222 pcpu->hispeed_validate_time = pcpu->timer_run_time;
223
Mike Chan9d49b702010-06-22 11:26:45 -0700224 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
225 new_freq, CPUFREQ_RELATION_H,
226 &index)) {
227 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
228 (int) data);
229 goto rearm;
230 }
231
232 new_freq = pcpu->freq_table[index].frequency;
233
Mike Chan9d49b702010-06-22 11:26:45 -0700234 /*
Todd Poynoraad27322012-04-26 21:41:40 -0700235 * Do not scale below floor_freq unless we have been at or above the
236 * floor frequency for the minimum sample time since last validated.
Mike Chan9d49b702010-06-22 11:26:45 -0700237 */
Todd Poynoraad27322012-04-26 21:41:40 -0700238 if (new_freq < pcpu->floor_freq) {
John Stultz92967112012-05-01 14:10:31 -0700239 if (pcpu->timer_run_time - pcpu->floor_validate_time
Todd Poynora1e19512012-02-16 16:27:59 -0800240 < min_sample_time) {
241 trace_cpufreq_interactive_notyet(data, cpu_load,
242 pcpu->target_freq, new_freq);
Mike Chan9d49b702010-06-22 11:26:45 -0700243 goto rearm;
Todd Poynora1e19512012-02-16 16:27:59 -0800244 }
Mike Chan9d49b702010-06-22 11:26:45 -0700245 }
246
Todd Poynoraad27322012-04-26 21:41:40 -0700247 pcpu->floor_freq = new_freq;
248 pcpu->floor_validate_time = pcpu->timer_run_time;
Todd Poynor0a92d482012-04-06 19:59:36 -0700249
250 if (pcpu->target_freq == new_freq) {
251 trace_cpufreq_interactive_already(data, cpu_load,
252 pcpu->target_freq, new_freq);
253 goto rearm_if_notmax;
254 }
255
Todd Poynora1e19512012-02-16 16:27:59 -0800256 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
257 new_freq);
Todd Poynorbc699d82012-04-20 13:18:32 -0700258 pcpu->target_set_time_in_idle = now_idle;
259 pcpu->target_set_time = pcpu->timer_run_time;
Todd Poynora1e19512012-02-16 16:27:59 -0800260
Mike Chan9d49b702010-06-22 11:26:45 -0700261 if (new_freq < pcpu->target_freq) {
262 pcpu->target_freq = new_freq;
263 spin_lock_irqsave(&down_cpumask_lock, flags);
264 cpumask_set_cpu(data, &down_cpumask);
265 spin_unlock_irqrestore(&down_cpumask_lock, flags);
266 queue_work(down_wq, &freq_scale_down_work);
267 } else {
268 pcpu->target_freq = new_freq;
269 spin_lock_irqsave(&up_cpumask_lock, flags);
270 cpumask_set_cpu(data, &up_cpumask);
271 spin_unlock_irqrestore(&up_cpumask_lock, flags);
272 wake_up_process(up_task);
273 }
274
275rearm_if_notmax:
276 /*
277 * Already set max speed and don't see a need to change that,
278 * wait until next idle to re-evaluate, don't need timer.
279 */
280 if (pcpu->target_freq == pcpu->policy->max)
281 goto exit;
282
283rearm:
284 if (!timer_pending(&pcpu->cpu_timer)) {
285 /*
286 * If already at min: if that CPU is idle, don't set timer.
287 * Else cancel the timer if that CPU goes idle. We don't
288 * need to re-evaluate speed until the next idle exit.
289 */
290 if (pcpu->target_freq == pcpu->policy->min) {
291 smp_rmb();
292
293 if (pcpu->idling)
294 goto exit;
295
296 pcpu->timer_idlecancel = 1;
297 }
298
299 pcpu->time_in_idle = get_cpu_idle_time_us(
300 data, &pcpu->idle_exit_time);
301 mod_timer(&pcpu->cpu_timer,
302 jiffies + usecs_to_jiffies(timer_rate));
303 }
304
305exit:
306 return;
307}
308
309static void cpufreq_interactive_idle_start(void)
310{
311 struct cpufreq_interactive_cpuinfo *pcpu =
312 &per_cpu(cpuinfo, smp_processor_id());
313 int pending;
314
315 if (!pcpu->governor_enabled)
316 return;
317
318 pcpu->idling = 1;
319 smp_wmb();
320 pending = timer_pending(&pcpu->cpu_timer);
321
322 if (pcpu->target_freq != pcpu->policy->min) {
323#ifdef CONFIG_SMP
324 /*
325 * Entering idle while not at lowest speed. On some
326 * platforms this can hold the other CPU(s) at that speed
327 * even though the CPU is idle. Set a timer to re-evaluate
328 * speed so this idle CPU doesn't hold the other CPUs above
329 * min indefinitely. This should probably be a quirk of
330 * the CPUFreq driver.
331 */
332 if (!pending) {
333 pcpu->time_in_idle = get_cpu_idle_time_us(
334 smp_processor_id(), &pcpu->idle_exit_time);
335 pcpu->timer_idlecancel = 0;
336 mod_timer(&pcpu->cpu_timer,
337 jiffies + usecs_to_jiffies(timer_rate));
338 }
339#endif
340 } else {
341 /*
342 * If at min speed and entering idle after load has
343 * already been evaluated, and a timer has been set just in
344 * case the CPU suddenly goes busy, cancel that timer. The
345 * CPU didn't go busy; we'll recheck things upon idle exit.
346 */
347 if (pending && pcpu->timer_idlecancel) {
348 del_timer(&pcpu->cpu_timer);
349 /*
350 * Ensure last timer run time is after current idle
351 * sample start time, so next idle exit will always
352 * start a new idle sampling period.
353 */
354 pcpu->idle_exit_time = 0;
355 pcpu->timer_idlecancel = 0;
356 }
357 }
358
359}
360
361static void cpufreq_interactive_idle_end(void)
362{
363 struct cpufreq_interactive_cpuinfo *pcpu =
364 &per_cpu(cpuinfo, smp_processor_id());
365
366 pcpu->idling = 0;
367 smp_wmb();
368
369 /*
370 * Arm the timer for 1-2 ticks later if not already, and if the timer
371 * function has already processed the previous load sampling
372 * interval. (If the timer is not pending but has not processed
373 * the previous interval, it is probably racing with us on another
374 * CPU. Let it compute load based on the previous sample and then
375 * re-arm the timer for another interval when it's done, rather
376 * than updating the interval start time to be "now", which doesn't
377 * give the timer function enough time to make a decision on this
378 * run.)
379 */
380 if (timer_pending(&pcpu->cpu_timer) == 0 &&
381 pcpu->timer_run_time >= pcpu->idle_exit_time &&
382 pcpu->governor_enabled) {
383 pcpu->time_in_idle =
384 get_cpu_idle_time_us(smp_processor_id(),
385 &pcpu->idle_exit_time);
386 pcpu->timer_idlecancel = 0;
387 mod_timer(&pcpu->cpu_timer,
388 jiffies + usecs_to_jiffies(timer_rate));
389 }
390
391}
392
393static int cpufreq_interactive_up_task(void *data)
394{
395 unsigned int cpu;
396 cpumask_t tmp_mask;
397 unsigned long flags;
398 struct cpufreq_interactive_cpuinfo *pcpu;
399
400 while (1) {
401 set_current_state(TASK_INTERRUPTIBLE);
402 spin_lock_irqsave(&up_cpumask_lock, flags);
403
404 if (cpumask_empty(&up_cpumask)) {
405 spin_unlock_irqrestore(&up_cpumask_lock, flags);
406 schedule();
407
408 if (kthread_should_stop())
409 break;
410
411 spin_lock_irqsave(&up_cpumask_lock, flags);
412 }
413
414 set_current_state(TASK_RUNNING);
415 tmp_mask = up_cpumask;
416 cpumask_clear(&up_cpumask);
417 spin_unlock_irqrestore(&up_cpumask_lock, flags);
418
419 for_each_cpu(cpu, &tmp_mask) {
420 unsigned int j;
421 unsigned int max_freq = 0;
422
423 pcpu = &per_cpu(cpuinfo, cpu);
424 smp_rmb();
425
426 if (!pcpu->governor_enabled)
427 continue;
428
429 mutex_lock(&set_speed_lock);
430
431 for_each_cpu(j, pcpu->policy->cpus) {
432 struct cpufreq_interactive_cpuinfo *pjcpu =
433 &per_cpu(cpuinfo, j);
434
435 if (pjcpu->target_freq > max_freq)
436 max_freq = pjcpu->target_freq;
437 }
438
439 if (max_freq != pcpu->policy->cur)
440 __cpufreq_driver_target(pcpu->policy,
441 max_freq,
442 CPUFREQ_RELATION_H);
443 mutex_unlock(&set_speed_lock);
Todd Poynora1e19512012-02-16 16:27:59 -0800444 trace_cpufreq_interactive_up(cpu, pcpu->target_freq,
445 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700446 }
447 }
448
449 return 0;
450}
451
452static void cpufreq_interactive_freq_down(struct work_struct *work)
453{
454 unsigned int cpu;
455 cpumask_t tmp_mask;
456 unsigned long flags;
457 struct cpufreq_interactive_cpuinfo *pcpu;
458
459 spin_lock_irqsave(&down_cpumask_lock, flags);
460 tmp_mask = down_cpumask;
461 cpumask_clear(&down_cpumask);
462 spin_unlock_irqrestore(&down_cpumask_lock, flags);
463
464 for_each_cpu(cpu, &tmp_mask) {
465 unsigned int j;
466 unsigned int max_freq = 0;
467
468 pcpu = &per_cpu(cpuinfo, cpu);
469 smp_rmb();
470
471 if (!pcpu->governor_enabled)
472 continue;
473
474 mutex_lock(&set_speed_lock);
475
476 for_each_cpu(j, pcpu->policy->cpus) {
477 struct cpufreq_interactive_cpuinfo *pjcpu =
478 &per_cpu(cpuinfo, j);
479
480 if (pjcpu->target_freq > max_freq)
481 max_freq = pjcpu->target_freq;
482 }
483
484 if (max_freq != pcpu->policy->cur)
485 __cpufreq_driver_target(pcpu->policy, max_freq,
486 CPUFREQ_RELATION_H);
487
488 mutex_unlock(&set_speed_lock);
Todd Poynora1e19512012-02-16 16:27:59 -0800489 trace_cpufreq_interactive_down(cpu, pcpu->target_freq,
490 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700491 }
492}
493
Todd Poynor7820a652012-04-02 17:17:14 -0700494static void cpufreq_interactive_boost(void)
495{
496 int i;
497 int anyboost = 0;
498 unsigned long flags;
499 struct cpufreq_interactive_cpuinfo *pcpu;
500
Todd Poynor7820a652012-04-02 17:17:14 -0700501 spin_lock_irqsave(&up_cpumask_lock, flags);
502
503 for_each_online_cpu(i) {
504 pcpu = &per_cpu(cpuinfo, i);
505
506 if (pcpu->target_freq < hispeed_freq) {
507 pcpu->target_freq = hispeed_freq;
508 cpumask_set_cpu(i, &up_cpumask);
509 pcpu->target_set_time_in_idle =
510 get_cpu_idle_time_us(i, &pcpu->target_set_time);
Todd Poynor5a5aa702012-05-10 23:28:06 -0700511 pcpu->hispeed_validate_time = pcpu->target_set_time;
Todd Poynor7820a652012-04-02 17:17:14 -0700512 anyboost = 1;
513 }
514
515 /*
Todd Poynoraad27322012-04-26 21:41:40 -0700516 * Set floor freq and (re)start timer for when last
517 * validated.
Todd Poynor7820a652012-04-02 17:17:14 -0700518 */
519
Todd Poynoraad27322012-04-26 21:41:40 -0700520 pcpu->floor_freq = hispeed_freq;
521 pcpu->floor_validate_time = ktime_to_us(ktime_get());
Todd Poynor7820a652012-04-02 17:17:14 -0700522 }
523
524 spin_unlock_irqrestore(&up_cpumask_lock, flags);
525
526 if (anyboost)
527 wake_up_process(up_task);
528}
529
Todd Poynor9fb15312012-04-23 20:42:41 -0700530/*
531 * Pulsed boost on input event raises CPUs to hispeed_freq and lets
532 * usual algorithm of min_sample_time decide when to allow speed
533 * to drop.
534 */
535
Todd Poynor7820a652012-04-02 17:17:14 -0700536static void cpufreq_interactive_input_event(struct input_handle *handle,
537 unsigned int type,
538 unsigned int code, int value)
539{
Todd Poynor2e739a02012-05-03 00:16:55 -0700540 if (input_boost_val && type == EV_SYN && code == SYN_REPORT) {
541 trace_cpufreq_interactive_boost("input");
Todd Poynor7820a652012-04-02 17:17:14 -0700542 cpufreq_interactive_boost();
Todd Poynor2e739a02012-05-03 00:16:55 -0700543 }
Todd Poynor7820a652012-04-02 17:17:14 -0700544}
545
546static void cpufreq_interactive_input_open(struct work_struct *w)
547{
548 struct cpufreq_interactive_inputopen *io =
549 container_of(w, struct cpufreq_interactive_inputopen,
550 inputopen_work);
551 int error;
552
553 error = input_open_device(io->handle);
554 if (error)
555 input_unregister_handle(io->handle);
556}
557
558static int cpufreq_interactive_input_connect(struct input_handler *handler,
559 struct input_dev *dev,
560 const struct input_device_id *id)
561{
562 struct input_handle *handle;
563 int error;
564
565 pr_info("%s: connect to %s\n", __func__, dev->name);
566 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
567 if (!handle)
568 return -ENOMEM;
569
570 handle->dev = dev;
571 handle->handler = handler;
572 handle->name = "cpufreq_interactive";
573
574 error = input_register_handle(handle);
575 if (error)
576 goto err;
577
578 inputopen.handle = handle;
579 queue_work(down_wq, &inputopen.inputopen_work);
580 return 0;
581err:
582 kfree(handle);
583 return error;
584}
585
586static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
587{
588 input_close_device(handle);
589 input_unregister_handle(handle);
590 kfree(handle);
591}
592
593static const struct input_device_id cpufreq_interactive_ids[] = {
594 {
595 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
596 INPUT_DEVICE_ID_MATCH_ABSBIT,
597 .evbit = { BIT_MASK(EV_ABS) },
598 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
599 BIT_MASK(ABS_MT_POSITION_X) |
600 BIT_MASK(ABS_MT_POSITION_Y) },
601 }, /* multi-touch touchscreen */
602 {
603 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
604 INPUT_DEVICE_ID_MATCH_ABSBIT,
605 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
606 .absbit = { [BIT_WORD(ABS_X)] =
607 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
608 }, /* touchpad */
609 { },
610};
611
612static struct input_handler cpufreq_interactive_input_handler = {
613 .event = cpufreq_interactive_input_event,
614 .connect = cpufreq_interactive_input_connect,
615 .disconnect = cpufreq_interactive_input_disconnect,
616 .name = "cpufreq_interactive",
617 .id_table = cpufreq_interactive_ids,
618};
619
Mike Chan9d49b702010-06-22 11:26:45 -0700620static ssize_t show_hispeed_freq(struct kobject *kobj,
621 struct attribute *attr, char *buf)
622{
623 return sprintf(buf, "%llu\n", hispeed_freq);
624}
625
626static ssize_t store_hispeed_freq(struct kobject *kobj,
627 struct attribute *attr, const char *buf,
628 size_t count)
629{
630 int ret;
631 u64 val;
632
633 ret = strict_strtoull(buf, 0, &val);
634 if (ret < 0)
635 return ret;
636 hispeed_freq = val;
637 return count;
638}
639
640static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
641 show_hispeed_freq, store_hispeed_freq);
642
643
644static ssize_t show_go_hispeed_load(struct kobject *kobj,
645 struct attribute *attr, char *buf)
646{
647 return sprintf(buf, "%lu\n", go_hispeed_load);
648}
649
650static ssize_t store_go_hispeed_load(struct kobject *kobj,
651 struct attribute *attr, const char *buf, size_t count)
652{
653 int ret;
654 unsigned long val;
655
656 ret = strict_strtoul(buf, 0, &val);
657 if (ret < 0)
658 return ret;
659 go_hispeed_load = val;
660 return count;
661}
662
663static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
664 show_go_hispeed_load, store_go_hispeed_load);
665
666static ssize_t show_min_sample_time(struct kobject *kobj,
667 struct attribute *attr, char *buf)
668{
669 return sprintf(buf, "%lu\n", min_sample_time);
670}
671
672static ssize_t store_min_sample_time(struct kobject *kobj,
673 struct attribute *attr, const char *buf, size_t count)
674{
675 int ret;
676 unsigned long val;
677
678 ret = strict_strtoul(buf, 0, &val);
679 if (ret < 0)
680 return ret;
681 min_sample_time = val;
682 return count;
683}
684
685static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
686 show_min_sample_time, store_min_sample_time);
687
Todd Poynor596cf1f2012-04-13 20:18:02 -0700688static ssize_t show_above_hispeed_delay(struct kobject *kobj,
689 struct attribute *attr, char *buf)
690{
691 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
692}
693
694static ssize_t store_above_hispeed_delay(struct kobject *kobj,
695 struct attribute *attr,
696 const char *buf, size_t count)
697{
698 int ret;
699 unsigned long val;
700
701 ret = strict_strtoul(buf, 0, &val);
702 if (ret < 0)
703 return ret;
704 above_hispeed_delay_val = val;
705 return count;
706}
707
708define_one_global_rw(above_hispeed_delay);
709
Mike Chan9d49b702010-06-22 11:26:45 -0700710static ssize_t show_timer_rate(struct kobject *kobj,
711 struct attribute *attr, char *buf)
712{
713 return sprintf(buf, "%lu\n", timer_rate);
714}
715
716static ssize_t store_timer_rate(struct kobject *kobj,
717 struct attribute *attr, const char *buf, size_t count)
718{
719 int ret;
720 unsigned long val;
721
722 ret = strict_strtoul(buf, 0, &val);
723 if (ret < 0)
724 return ret;
725 timer_rate = val;
726 return count;
727}
728
729static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
730 show_timer_rate, store_timer_rate);
731
Todd Poynor7820a652012-04-02 17:17:14 -0700732static ssize_t show_input_boost(struct kobject *kobj, struct attribute *attr,
733 char *buf)
734{
735 return sprintf(buf, "%u\n", input_boost_val);
736}
737
738static ssize_t store_input_boost(struct kobject *kobj, struct attribute *attr,
739 const char *buf, size_t count)
740{
741 int ret;
742 unsigned long val;
743
744 ret = strict_strtoul(buf, 0, &val);
745 if (ret < 0)
746 return ret;
747 input_boost_val = val;
748 return count;
749}
750
751define_one_global_rw(input_boost);
752
Todd Poynor9fb15312012-04-23 20:42:41 -0700753static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
754 char *buf)
755{
756 return sprintf(buf, "%d\n", boost_val);
757}
758
759static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
760 const char *buf, size_t count)
761{
762 int ret;
763 unsigned long val;
764
765 ret = kstrtoul(buf, 0, &val);
766 if (ret < 0)
767 return ret;
768
769 boost_val = val;
770
Todd Poynor2e739a02012-05-03 00:16:55 -0700771 if (boost_val) {
772 trace_cpufreq_interactive_boost("on");
Todd Poynor9fb15312012-04-23 20:42:41 -0700773 cpufreq_interactive_boost();
Todd Poynor2e739a02012-05-03 00:16:55 -0700774 } else {
775 trace_cpufreq_interactive_unboost("off");
776 }
Todd Poynor9fb15312012-04-23 20:42:41 -0700777
778 return count;
779}
780
781define_one_global_rw(boost);
782
Todd Poynor2e739a02012-05-03 00:16:55 -0700783static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
784 const char *buf, size_t count)
785{
786 int ret;
787 unsigned long val;
788
789 ret = kstrtoul(buf, 0, &val);
790 if (ret < 0)
791 return ret;
792
793 trace_cpufreq_interactive_boost("pulse");
794 cpufreq_interactive_boost();
795 return count;
796}
797
798static struct global_attr boostpulse =
799 __ATTR(boostpulse, 0200, NULL, store_boostpulse);
800
Mike Chan9d49b702010-06-22 11:26:45 -0700801static struct attribute *interactive_attributes[] = {
802 &hispeed_freq_attr.attr,
803 &go_hispeed_load_attr.attr,
Todd Poynor596cf1f2012-04-13 20:18:02 -0700804 &above_hispeed_delay.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700805 &min_sample_time_attr.attr,
806 &timer_rate_attr.attr,
Todd Poynor7820a652012-04-02 17:17:14 -0700807 &input_boost.attr,
Todd Poynor9fb15312012-04-23 20:42:41 -0700808 &boost.attr,
Todd Poynor2e739a02012-05-03 00:16:55 -0700809 &boostpulse.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700810 NULL,
811};
812
813static struct attribute_group interactive_attr_group = {
814 .attrs = interactive_attributes,
815 .name = "interactive",
816};
817
818static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
819 unsigned int event)
820{
821 int rc;
822 unsigned int j;
823 struct cpufreq_interactive_cpuinfo *pcpu;
824 struct cpufreq_frequency_table *freq_table;
825
826 switch (event) {
827 case CPUFREQ_GOV_START:
828 if (!cpu_online(policy->cpu))
829 return -EINVAL;
830
831 freq_table =
832 cpufreq_frequency_get_table(policy->cpu);
833
834 for_each_cpu(j, policy->cpus) {
835 pcpu = &per_cpu(cpuinfo, j);
836 pcpu->policy = policy;
837 pcpu->target_freq = policy->cur;
838 pcpu->freq_table = freq_table;
Todd Poynor0a92d482012-04-06 19:59:36 -0700839 pcpu->target_set_time_in_idle =
Mike Chan9d49b702010-06-22 11:26:45 -0700840 get_cpu_idle_time_us(j,
Todd Poynor0a92d482012-04-06 19:59:36 -0700841 &pcpu->target_set_time);
Todd Poynoraad27322012-04-26 21:41:40 -0700842 pcpu->floor_freq = pcpu->target_freq;
843 pcpu->floor_validate_time =
Todd Poynorbc699d82012-04-20 13:18:32 -0700844 pcpu->target_set_time;
Todd Poynor5a5aa702012-05-10 23:28:06 -0700845 pcpu->hispeed_validate_time =
846 pcpu->target_set_time;
Mike Chan9d49b702010-06-22 11:26:45 -0700847 pcpu->governor_enabled = 1;
Narayanan Gopalakrishnan940d4b92012-08-20 10:52:13 -0700848 pcpu->idle_exit_time = pcpu->target_set_time;
849 mod_timer(&pcpu->cpu_timer,
850 jiffies + usecs_to_jiffies(timer_rate));
Mike Chan9d49b702010-06-22 11:26:45 -0700851 smp_wmb();
852 }
853
854 if (!hispeed_freq)
855 hispeed_freq = policy->max;
856
857 /*
858 * Do not register the idle hook and create sysfs
859 * entries if we have already done so.
860 */
861 if (atomic_inc_return(&active_count) > 1)
862 return 0;
863
864 rc = sysfs_create_group(cpufreq_global_kobject,
865 &interactive_attr_group);
866 if (rc)
867 return rc;
868
Todd Poynor7820a652012-04-02 17:17:14 -0700869 rc = input_register_handler(&cpufreq_interactive_input_handler);
870 if (rc)
871 pr_warn("%s: failed to register input handler\n",
872 __func__);
873
Mike Chan9d49b702010-06-22 11:26:45 -0700874 break;
875
876 case CPUFREQ_GOV_STOP:
877 for_each_cpu(j, policy->cpus) {
878 pcpu = &per_cpu(cpuinfo, j);
879 pcpu->governor_enabled = 0;
880 smp_wmb();
881 del_timer_sync(&pcpu->cpu_timer);
882
883 /*
884 * Reset idle exit time since we may cancel the timer
885 * before it can run after the last idle exit time,
886 * to avoid tripping the check in idle exit for a timer
887 * that is trying to run.
888 */
889 pcpu->idle_exit_time = 0;
890 }
891
892 flush_work(&freq_scale_down_work);
893 if (atomic_dec_return(&active_count) > 0)
894 return 0;
895
Todd Poynor7820a652012-04-02 17:17:14 -0700896 input_unregister_handler(&cpufreq_interactive_input_handler);
Mike Chan9d49b702010-06-22 11:26:45 -0700897 sysfs_remove_group(cpufreq_global_kobject,
898 &interactive_attr_group);
899
900 break;
901
902 case CPUFREQ_GOV_LIMITS:
903 if (policy->max < policy->cur)
904 __cpufreq_driver_target(policy,
905 policy->max, CPUFREQ_RELATION_H);
906 else if (policy->min > policy->cur)
907 __cpufreq_driver_target(policy,
908 policy->min, CPUFREQ_RELATION_L);
909 break;
910 }
911 return 0;
912}
913
914static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
915 unsigned long val,
916 void *data)
917{
918 switch (val) {
919 case IDLE_START:
920 cpufreq_interactive_idle_start();
921 break;
922 case IDLE_END:
923 cpufreq_interactive_idle_end();
924 break;
925 }
926
927 return 0;
928}
929
930static struct notifier_block cpufreq_interactive_idle_nb = {
931 .notifier_call = cpufreq_interactive_idle_notifier,
932};
933
934static int __init cpufreq_interactive_init(void)
935{
936 unsigned int i;
937 struct cpufreq_interactive_cpuinfo *pcpu;
938 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
939
940 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
941 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700942 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
Mike Chan9d49b702010-06-22 11:26:45 -0700943 timer_rate = DEFAULT_TIMER_RATE;
944
945 /* Initalize per-cpu timers */
946 for_each_possible_cpu(i) {
947 pcpu = &per_cpu(cpuinfo, i);
948 init_timer(&pcpu->cpu_timer);
949 pcpu->cpu_timer.function = cpufreq_interactive_timer;
950 pcpu->cpu_timer.data = i;
951 }
952
953 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
954 "kinteractiveup");
955 if (IS_ERR(up_task))
956 return PTR_ERR(up_task);
957
958 sched_setscheduler_nocheck(up_task, SCHED_FIFO, &param);
959 get_task_struct(up_task);
960
961 /* No rescuer thread, bind to CPU queuing the work for possibly
962 warm cache (probably doesn't matter much). */
963 down_wq = alloc_workqueue("knteractive_down", 0, 1);
964
965 if (!down_wq)
966 goto err_freeuptask;
967
968 INIT_WORK(&freq_scale_down_work,
969 cpufreq_interactive_freq_down);
970
971 spin_lock_init(&up_cpumask_lock);
972 spin_lock_init(&down_cpumask_lock);
973 mutex_init(&set_speed_lock);
974
Stephen Boyd7b912cc2012-10-29 12:45:01 -0700975 /* Kick the kthread to idle */
976 wake_up_process(up_task);
977
Mike Chan9d49b702010-06-22 11:26:45 -0700978 idle_notifier_register(&cpufreq_interactive_idle_nb);
Todd Poynor7820a652012-04-02 17:17:14 -0700979 INIT_WORK(&inputopen.inputopen_work, cpufreq_interactive_input_open);
Mike Chan9d49b702010-06-22 11:26:45 -0700980 return cpufreq_register_governor(&cpufreq_gov_interactive);
981
982err_freeuptask:
983 put_task_struct(up_task);
984 return -ENOMEM;
985}
986
987#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
988fs_initcall(cpufreq_interactive_init);
989#else
990module_init(cpufreq_interactive_init);
991#endif
992
993static void __exit cpufreq_interactive_exit(void)
994{
995 cpufreq_unregister_governor(&cpufreq_gov_interactive);
996 kthread_stop(up_task);
997 put_task_struct(up_task);
998 destroy_workqueue(down_wq);
999}
1000
1001module_exit(cpufreq_interactive_exit);
1002
1003MODULE_AUTHOR("Mike Chan <mike@android.com>");
1004MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1005 "Latency sensitive workloads");
1006MODULE_LICENSE("GPL");