blob: 86bff9f0d5820e6968330fbf1d0111e95f1b5707 [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
Sam Lefflera04e4412012-06-27 10:12:04 -0700366 if (!pcpu->governor_enabled)
367 return;
368
Mike Chan9d49b702010-06-22 11:26:45 -0700369 pcpu->idling = 0;
370 smp_wmb();
371
372 /*
373 * Arm the timer for 1-2 ticks later if not already, and if the timer
374 * function has already processed the previous load sampling
375 * interval. (If the timer is not pending but has not processed
376 * the previous interval, it is probably racing with us on another
377 * CPU. Let it compute load based on the previous sample and then
378 * re-arm the timer for another interval when it's done, rather
379 * than updating the interval start time to be "now", which doesn't
380 * give the timer function enough time to make a decision on this
381 * run.)
382 */
383 if (timer_pending(&pcpu->cpu_timer) == 0 &&
384 pcpu->timer_run_time >= pcpu->idle_exit_time &&
385 pcpu->governor_enabled) {
386 pcpu->time_in_idle =
387 get_cpu_idle_time_us(smp_processor_id(),
388 &pcpu->idle_exit_time);
389 pcpu->timer_idlecancel = 0;
390 mod_timer(&pcpu->cpu_timer,
391 jiffies + usecs_to_jiffies(timer_rate));
392 }
393
394}
395
396static int cpufreq_interactive_up_task(void *data)
397{
398 unsigned int cpu;
399 cpumask_t tmp_mask;
400 unsigned long flags;
401 struct cpufreq_interactive_cpuinfo *pcpu;
402
403 while (1) {
404 set_current_state(TASK_INTERRUPTIBLE);
405 spin_lock_irqsave(&up_cpumask_lock, flags);
406
407 if (cpumask_empty(&up_cpumask)) {
408 spin_unlock_irqrestore(&up_cpumask_lock, flags);
409 schedule();
410
411 if (kthread_should_stop())
412 break;
413
414 spin_lock_irqsave(&up_cpumask_lock, flags);
415 }
416
417 set_current_state(TASK_RUNNING);
418 tmp_mask = up_cpumask;
419 cpumask_clear(&up_cpumask);
420 spin_unlock_irqrestore(&up_cpumask_lock, flags);
421
422 for_each_cpu(cpu, &tmp_mask) {
423 unsigned int j;
424 unsigned int max_freq = 0;
425
426 pcpu = &per_cpu(cpuinfo, cpu);
427 smp_rmb();
428
429 if (!pcpu->governor_enabled)
430 continue;
431
432 mutex_lock(&set_speed_lock);
433
434 for_each_cpu(j, pcpu->policy->cpus) {
435 struct cpufreq_interactive_cpuinfo *pjcpu =
436 &per_cpu(cpuinfo, j);
437
438 if (pjcpu->target_freq > max_freq)
439 max_freq = pjcpu->target_freq;
440 }
441
442 if (max_freq != pcpu->policy->cur)
443 __cpufreq_driver_target(pcpu->policy,
444 max_freq,
445 CPUFREQ_RELATION_H);
446 mutex_unlock(&set_speed_lock);
Todd Poynora1e19512012-02-16 16:27:59 -0800447 trace_cpufreq_interactive_up(cpu, pcpu->target_freq,
448 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700449 }
450 }
451
452 return 0;
453}
454
455static void cpufreq_interactive_freq_down(struct work_struct *work)
456{
457 unsigned int cpu;
458 cpumask_t tmp_mask;
459 unsigned long flags;
460 struct cpufreq_interactive_cpuinfo *pcpu;
461
462 spin_lock_irqsave(&down_cpumask_lock, flags);
463 tmp_mask = down_cpumask;
464 cpumask_clear(&down_cpumask);
465 spin_unlock_irqrestore(&down_cpumask_lock, flags);
466
467 for_each_cpu(cpu, &tmp_mask) {
468 unsigned int j;
469 unsigned int max_freq = 0;
470
471 pcpu = &per_cpu(cpuinfo, cpu);
472 smp_rmb();
473
474 if (!pcpu->governor_enabled)
475 continue;
476
477 mutex_lock(&set_speed_lock);
478
479 for_each_cpu(j, pcpu->policy->cpus) {
480 struct cpufreq_interactive_cpuinfo *pjcpu =
481 &per_cpu(cpuinfo, j);
482
483 if (pjcpu->target_freq > max_freq)
484 max_freq = pjcpu->target_freq;
485 }
486
487 if (max_freq != pcpu->policy->cur)
488 __cpufreq_driver_target(pcpu->policy, max_freq,
489 CPUFREQ_RELATION_H);
490
491 mutex_unlock(&set_speed_lock);
Todd Poynora1e19512012-02-16 16:27:59 -0800492 trace_cpufreq_interactive_down(cpu, pcpu->target_freq,
493 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700494 }
495}
496
Todd Poynor7820a652012-04-02 17:17:14 -0700497static void cpufreq_interactive_boost(void)
498{
499 int i;
500 int anyboost = 0;
501 unsigned long flags;
502 struct cpufreq_interactive_cpuinfo *pcpu;
503
Todd Poynor7820a652012-04-02 17:17:14 -0700504 spin_lock_irqsave(&up_cpumask_lock, flags);
505
506 for_each_online_cpu(i) {
507 pcpu = &per_cpu(cpuinfo, i);
508
509 if (pcpu->target_freq < hispeed_freq) {
510 pcpu->target_freq = hispeed_freq;
511 cpumask_set_cpu(i, &up_cpumask);
512 pcpu->target_set_time_in_idle =
513 get_cpu_idle_time_us(i, &pcpu->target_set_time);
Todd Poynor5a5aa702012-05-10 23:28:06 -0700514 pcpu->hispeed_validate_time = pcpu->target_set_time;
Todd Poynor7820a652012-04-02 17:17:14 -0700515 anyboost = 1;
516 }
517
518 /*
Todd Poynoraad27322012-04-26 21:41:40 -0700519 * Set floor freq and (re)start timer for when last
520 * validated.
Todd Poynor7820a652012-04-02 17:17:14 -0700521 */
522
Todd Poynoraad27322012-04-26 21:41:40 -0700523 pcpu->floor_freq = hispeed_freq;
524 pcpu->floor_validate_time = ktime_to_us(ktime_get());
Todd Poynor7820a652012-04-02 17:17:14 -0700525 }
526
527 spin_unlock_irqrestore(&up_cpumask_lock, flags);
528
529 if (anyboost)
530 wake_up_process(up_task);
531}
532
Todd Poynor9fb15312012-04-23 20:42:41 -0700533/*
534 * Pulsed boost on input event raises CPUs to hispeed_freq and lets
535 * usual algorithm of min_sample_time decide when to allow speed
536 * to drop.
537 */
538
Todd Poynor7820a652012-04-02 17:17:14 -0700539static void cpufreq_interactive_input_event(struct input_handle *handle,
540 unsigned int type,
541 unsigned int code, int value)
542{
Todd Poynor2e739a02012-05-03 00:16:55 -0700543 if (input_boost_val && type == EV_SYN && code == SYN_REPORT) {
544 trace_cpufreq_interactive_boost("input");
Todd Poynor7820a652012-04-02 17:17:14 -0700545 cpufreq_interactive_boost();
Todd Poynor2e739a02012-05-03 00:16:55 -0700546 }
Todd Poynor7820a652012-04-02 17:17:14 -0700547}
548
549static void cpufreq_interactive_input_open(struct work_struct *w)
550{
551 struct cpufreq_interactive_inputopen *io =
552 container_of(w, struct cpufreq_interactive_inputopen,
553 inputopen_work);
554 int error;
555
556 error = input_open_device(io->handle);
557 if (error)
558 input_unregister_handle(io->handle);
559}
560
561static int cpufreq_interactive_input_connect(struct input_handler *handler,
562 struct input_dev *dev,
563 const struct input_device_id *id)
564{
565 struct input_handle *handle;
566 int error;
567
568 pr_info("%s: connect to %s\n", __func__, dev->name);
569 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
570 if (!handle)
571 return -ENOMEM;
572
573 handle->dev = dev;
574 handle->handler = handler;
575 handle->name = "cpufreq_interactive";
576
577 error = input_register_handle(handle);
578 if (error)
579 goto err;
580
581 inputopen.handle = handle;
582 queue_work(down_wq, &inputopen.inputopen_work);
583 return 0;
584err:
585 kfree(handle);
586 return error;
587}
588
589static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
590{
591 input_close_device(handle);
592 input_unregister_handle(handle);
593 kfree(handle);
594}
595
596static const struct input_device_id cpufreq_interactive_ids[] = {
597 {
598 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
599 INPUT_DEVICE_ID_MATCH_ABSBIT,
600 .evbit = { BIT_MASK(EV_ABS) },
601 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
602 BIT_MASK(ABS_MT_POSITION_X) |
603 BIT_MASK(ABS_MT_POSITION_Y) },
604 }, /* multi-touch touchscreen */
605 {
606 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
607 INPUT_DEVICE_ID_MATCH_ABSBIT,
608 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
609 .absbit = { [BIT_WORD(ABS_X)] =
610 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
611 }, /* touchpad */
612 { },
613};
614
615static struct input_handler cpufreq_interactive_input_handler = {
616 .event = cpufreq_interactive_input_event,
617 .connect = cpufreq_interactive_input_connect,
618 .disconnect = cpufreq_interactive_input_disconnect,
619 .name = "cpufreq_interactive",
620 .id_table = cpufreq_interactive_ids,
621};
622
Mike Chan9d49b702010-06-22 11:26:45 -0700623static ssize_t show_hispeed_freq(struct kobject *kobj,
624 struct attribute *attr, char *buf)
625{
626 return sprintf(buf, "%llu\n", hispeed_freq);
627}
628
629static ssize_t store_hispeed_freq(struct kobject *kobj,
630 struct attribute *attr, const char *buf,
631 size_t count)
632{
633 int ret;
634 u64 val;
635
636 ret = strict_strtoull(buf, 0, &val);
637 if (ret < 0)
638 return ret;
639 hispeed_freq = val;
640 return count;
641}
642
643static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
644 show_hispeed_freq, store_hispeed_freq);
645
646
647static ssize_t show_go_hispeed_load(struct kobject *kobj,
648 struct attribute *attr, char *buf)
649{
650 return sprintf(buf, "%lu\n", go_hispeed_load);
651}
652
653static ssize_t store_go_hispeed_load(struct kobject *kobj,
654 struct attribute *attr, const char *buf, size_t count)
655{
656 int ret;
657 unsigned long val;
658
659 ret = strict_strtoul(buf, 0, &val);
660 if (ret < 0)
661 return ret;
662 go_hispeed_load = val;
663 return count;
664}
665
666static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
667 show_go_hispeed_load, store_go_hispeed_load);
668
669static ssize_t show_min_sample_time(struct kobject *kobj,
670 struct attribute *attr, char *buf)
671{
672 return sprintf(buf, "%lu\n", min_sample_time);
673}
674
675static ssize_t store_min_sample_time(struct kobject *kobj,
676 struct attribute *attr, const char *buf, size_t count)
677{
678 int ret;
679 unsigned long val;
680
681 ret = strict_strtoul(buf, 0, &val);
682 if (ret < 0)
683 return ret;
684 min_sample_time = val;
685 return count;
686}
687
688static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
689 show_min_sample_time, store_min_sample_time);
690
Todd Poynor596cf1f2012-04-13 20:18:02 -0700691static ssize_t show_above_hispeed_delay(struct kobject *kobj,
692 struct attribute *attr, char *buf)
693{
694 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
695}
696
697static ssize_t store_above_hispeed_delay(struct kobject *kobj,
698 struct attribute *attr,
699 const char *buf, size_t count)
700{
701 int ret;
702 unsigned long val;
703
704 ret = strict_strtoul(buf, 0, &val);
705 if (ret < 0)
706 return ret;
707 above_hispeed_delay_val = val;
708 return count;
709}
710
711define_one_global_rw(above_hispeed_delay);
712
Mike Chan9d49b702010-06-22 11:26:45 -0700713static ssize_t show_timer_rate(struct kobject *kobj,
714 struct attribute *attr, char *buf)
715{
716 return sprintf(buf, "%lu\n", timer_rate);
717}
718
719static ssize_t store_timer_rate(struct kobject *kobj,
720 struct attribute *attr, const char *buf, size_t count)
721{
722 int ret;
723 unsigned long val;
724
725 ret = strict_strtoul(buf, 0, &val);
726 if (ret < 0)
727 return ret;
728 timer_rate = val;
729 return count;
730}
731
732static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
733 show_timer_rate, store_timer_rate);
734
Todd Poynor7820a652012-04-02 17:17:14 -0700735static ssize_t show_input_boost(struct kobject *kobj, struct attribute *attr,
736 char *buf)
737{
738 return sprintf(buf, "%u\n", input_boost_val);
739}
740
741static ssize_t store_input_boost(struct kobject *kobj, struct attribute *attr,
742 const char *buf, size_t count)
743{
744 int ret;
745 unsigned long val;
746
747 ret = strict_strtoul(buf, 0, &val);
748 if (ret < 0)
749 return ret;
750 input_boost_val = val;
751 return count;
752}
753
754define_one_global_rw(input_boost);
755
Todd Poynor9fb15312012-04-23 20:42:41 -0700756static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
757 char *buf)
758{
759 return sprintf(buf, "%d\n", boost_val);
760}
761
762static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
763 const char *buf, size_t count)
764{
765 int ret;
766 unsigned long val;
767
768 ret = kstrtoul(buf, 0, &val);
769 if (ret < 0)
770 return ret;
771
772 boost_val = val;
773
Todd Poynor2e739a02012-05-03 00:16:55 -0700774 if (boost_val) {
775 trace_cpufreq_interactive_boost("on");
Todd Poynor9fb15312012-04-23 20:42:41 -0700776 cpufreq_interactive_boost();
Todd Poynor2e739a02012-05-03 00:16:55 -0700777 } else {
778 trace_cpufreq_interactive_unboost("off");
779 }
Todd Poynor9fb15312012-04-23 20:42:41 -0700780
781 return count;
782}
783
784define_one_global_rw(boost);
785
Todd Poynor2e739a02012-05-03 00:16:55 -0700786static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
787 const char *buf, size_t count)
788{
789 int ret;
790 unsigned long val;
791
792 ret = kstrtoul(buf, 0, &val);
793 if (ret < 0)
794 return ret;
795
796 trace_cpufreq_interactive_boost("pulse");
797 cpufreq_interactive_boost();
798 return count;
799}
800
801static struct global_attr boostpulse =
802 __ATTR(boostpulse, 0200, NULL, store_boostpulse);
803
Mike Chan9d49b702010-06-22 11:26:45 -0700804static struct attribute *interactive_attributes[] = {
805 &hispeed_freq_attr.attr,
806 &go_hispeed_load_attr.attr,
Todd Poynor596cf1f2012-04-13 20:18:02 -0700807 &above_hispeed_delay.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700808 &min_sample_time_attr.attr,
809 &timer_rate_attr.attr,
Todd Poynor7820a652012-04-02 17:17:14 -0700810 &input_boost.attr,
Todd Poynor9fb15312012-04-23 20:42:41 -0700811 &boost.attr,
Todd Poynor2e739a02012-05-03 00:16:55 -0700812 &boostpulse.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700813 NULL,
814};
815
816static struct attribute_group interactive_attr_group = {
817 .attrs = interactive_attributes,
818 .name = "interactive",
819};
820
Sam Lefflera04e4412012-06-27 10:12:04 -0700821static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
822 unsigned long val,
823 void *data)
824{
825 switch (val) {
826 case IDLE_START:
827 cpufreq_interactive_idle_start();
828 break;
829 case IDLE_END:
830 cpufreq_interactive_idle_end();
831 break;
832 }
833
834 return 0;
835}
836
837static struct notifier_block cpufreq_interactive_idle_nb = {
838 .notifier_call = cpufreq_interactive_idle_notifier,
839};
840
Mike Chan9d49b702010-06-22 11:26:45 -0700841static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
842 unsigned int event)
843{
844 int rc;
845 unsigned int j;
846 struct cpufreq_interactive_cpuinfo *pcpu;
847 struct cpufreq_frequency_table *freq_table;
848
849 switch (event) {
850 case CPUFREQ_GOV_START:
851 if (!cpu_online(policy->cpu))
852 return -EINVAL;
853
854 freq_table =
855 cpufreq_frequency_get_table(policy->cpu);
856
857 for_each_cpu(j, policy->cpus) {
858 pcpu = &per_cpu(cpuinfo, j);
859 pcpu->policy = policy;
860 pcpu->target_freq = policy->cur;
861 pcpu->freq_table = freq_table;
Todd Poynor0a92d482012-04-06 19:59:36 -0700862 pcpu->target_set_time_in_idle =
Mike Chan9d49b702010-06-22 11:26:45 -0700863 get_cpu_idle_time_us(j,
Todd Poynor0a92d482012-04-06 19:59:36 -0700864 &pcpu->target_set_time);
Todd Poynoraad27322012-04-26 21:41:40 -0700865 pcpu->floor_freq = pcpu->target_freq;
866 pcpu->floor_validate_time =
Todd Poynorbc699d82012-04-20 13:18:32 -0700867 pcpu->target_set_time;
Todd Poynor5a5aa702012-05-10 23:28:06 -0700868 pcpu->hispeed_validate_time =
869 pcpu->target_set_time;
Mike Chan9d49b702010-06-22 11:26:45 -0700870 pcpu->governor_enabled = 1;
871 smp_wmb();
872 }
873
874 if (!hispeed_freq)
875 hispeed_freq = policy->max;
876
877 /*
878 * Do not register the idle hook and create sysfs
879 * entries if we have already done so.
880 */
881 if (atomic_inc_return(&active_count) > 1)
882 return 0;
883
884 rc = sysfs_create_group(cpufreq_global_kobject,
885 &interactive_attr_group);
886 if (rc)
887 return rc;
888
Todd Poynor7820a652012-04-02 17:17:14 -0700889 rc = input_register_handler(&cpufreq_interactive_input_handler);
890 if (rc)
891 pr_warn("%s: failed to register input handler\n",
892 __func__);
893
Sam Lefflera04e4412012-06-27 10:12:04 -0700894 idle_notifier_register(&cpufreq_interactive_idle_nb);
Mike Chan9d49b702010-06-22 11:26:45 -0700895 break;
896
897 case CPUFREQ_GOV_STOP:
898 for_each_cpu(j, policy->cpus) {
899 pcpu = &per_cpu(cpuinfo, j);
900 pcpu->governor_enabled = 0;
901 smp_wmb();
902 del_timer_sync(&pcpu->cpu_timer);
903
904 /*
905 * Reset idle exit time since we may cancel the timer
906 * before it can run after the last idle exit time,
907 * to avoid tripping the check in idle exit for a timer
908 * that is trying to run.
909 */
910 pcpu->idle_exit_time = 0;
911 }
912
913 flush_work(&freq_scale_down_work);
914 if (atomic_dec_return(&active_count) > 0)
915 return 0;
916
Sam Lefflera04e4412012-06-27 10:12:04 -0700917 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
Todd Poynor7820a652012-04-02 17:17:14 -0700918 input_unregister_handler(&cpufreq_interactive_input_handler);
Mike Chan9d49b702010-06-22 11:26:45 -0700919 sysfs_remove_group(cpufreq_global_kobject,
920 &interactive_attr_group);
921
922 break;
923
924 case CPUFREQ_GOV_LIMITS:
925 if (policy->max < policy->cur)
926 __cpufreq_driver_target(policy,
927 policy->max, CPUFREQ_RELATION_H);
928 else if (policy->min > policy->cur)
929 __cpufreq_driver_target(policy,
930 policy->min, CPUFREQ_RELATION_L);
931 break;
932 }
933 return 0;
934}
935
Mike Chan9d49b702010-06-22 11:26:45 -0700936static int __init cpufreq_interactive_init(void)
937{
938 unsigned int i;
939 struct cpufreq_interactive_cpuinfo *pcpu;
940 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
941
942 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
943 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700944 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
Mike Chan9d49b702010-06-22 11:26:45 -0700945 timer_rate = DEFAULT_TIMER_RATE;
946
947 /* Initalize per-cpu timers */
948 for_each_possible_cpu(i) {
949 pcpu = &per_cpu(cpuinfo, i);
950 init_timer(&pcpu->cpu_timer);
951 pcpu->cpu_timer.function = cpufreq_interactive_timer;
952 pcpu->cpu_timer.data = i;
953 }
954
955 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
956 "kinteractiveup");
957 if (IS_ERR(up_task))
958 return PTR_ERR(up_task);
959
960 sched_setscheduler_nocheck(up_task, SCHED_FIFO, &param);
961 get_task_struct(up_task);
962
963 /* No rescuer thread, bind to CPU queuing the work for possibly
964 warm cache (probably doesn't matter much). */
965 down_wq = alloc_workqueue("knteractive_down", 0, 1);
966
967 if (!down_wq)
968 goto err_freeuptask;
969
970 INIT_WORK(&freq_scale_down_work,
971 cpufreq_interactive_freq_down);
972
973 spin_lock_init(&up_cpumask_lock);
974 spin_lock_init(&down_cpumask_lock);
975 mutex_init(&set_speed_lock);
976
Todd Poynor7820a652012-04-02 17:17:14 -0700977 INIT_WORK(&inputopen.inputopen_work, cpufreq_interactive_input_open);
Mike Chan9d49b702010-06-22 11:26:45 -0700978 return cpufreq_register_governor(&cpufreq_gov_interactive);
979
980err_freeuptask:
981 put_task_struct(up_task);
982 return -ENOMEM;
983}
984
985#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
986fs_initcall(cpufreq_interactive_init);
987#else
988module_init(cpufreq_interactive_init);
989#endif
990
991static void __exit cpufreq_interactive_exit(void)
992{
993 cpufreq_unregister_governor(&cpufreq_gov_interactive);
994 kthread_stop(up_task);
995 put_task_struct(up_task);
996 destroy_workqueue(down_wq);
997}
998
999module_exit(cpufreq_interactive_exit);
1000
1001MODULE_AUTHOR("Mike Chan <mike@android.com>");
1002MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
1003 "Latency sensitive workloads");
1004MODULE_LICENSE("GPL");