blob: cbd9bbce0cb2a9e403507f5d76be98407bd04b6e [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
Todd Poynor02442cf2012-07-16 17:07:15 -070060/* realtime thread handles frequency scaling */
61static struct task_struct *speedchange_task;
62static cpumask_t speedchange_cpumask;
63static spinlock_t speedchange_cpumask_lock;
Mike Chan9d49b702010-06-22 11:26:45 -070064
65/* Hi speed to bump to from lo speed when load burst (default max) */
66static u64 hispeed_freq;
67
68/* Go to hi speed when CPU load at or above this value. */
Todd Poynora0ec4362012-04-17 17:39:34 -070069#define DEFAULT_GO_HISPEED_LOAD 85
Mike Chan9d49b702010-06-22 11:26:45 -070070static unsigned long go_hispeed_load;
71
72/*
73 * The minimum amount of time to spend at a frequency before we can ramp down.
74 */
Todd Poynora0ec4362012-04-17 17:39:34 -070075#define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
Mike Chan9d49b702010-06-22 11:26:45 -070076static unsigned long min_sample_time;
77
78/*
79 * The sample rate of the timer used to increase frequency
80 */
Todd Poynora0ec4362012-04-17 17:39:34 -070081#define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
Mike Chan9d49b702010-06-22 11:26:45 -070082static unsigned long timer_rate;
83
Todd Poynor596cf1f2012-04-13 20:18:02 -070084/*
85 * Wait this long before raising speed above hispeed, by default a single
86 * timer interval.
87 */
88#define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
89static unsigned long above_hispeed_delay_val;
90
Todd Poynor7820a652012-04-02 17:17:14 -070091/*
Todd Poynor9fb15312012-04-23 20:42:41 -070092 * Boost pulse to hispeed on touchscreen input.
Todd Poynor7820a652012-04-02 17:17:14 -070093 */
94
95static int input_boost_val;
96
97struct cpufreq_interactive_inputopen {
98 struct input_handle *handle;
99 struct work_struct inputopen_work;
100};
101
102static struct cpufreq_interactive_inputopen inputopen;
Todd Poynor02442cf2012-07-16 17:07:15 -0700103static struct workqueue_struct *inputopen_wq;
Todd Poynor7820a652012-04-02 17:17:14 -0700104
Todd Poynor9fb15312012-04-23 20:42:41 -0700105/*
106 * Non-zero means longer-term speed boost active.
107 */
108
109static int boost_val;
110
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
124static void cpufreq_interactive_timer(unsigned long data)
125{
126 unsigned int delta_idle;
127 unsigned int delta_time;
128 int cpu_load;
129 int load_since_change;
130 u64 time_in_idle;
131 u64 idle_exit_time;
132 struct cpufreq_interactive_cpuinfo *pcpu =
133 &per_cpu(cpuinfo, data);
134 u64 now_idle;
135 unsigned int new_freq;
136 unsigned int index;
137 unsigned long flags;
138
139 smp_rmb();
140
141 if (!pcpu->governor_enabled)
142 goto exit;
143
144 /*
145 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
146 * this lets idle exit know the current idle time sample has
147 * been processed, and idle exit can generate a new sample and
148 * re-arm the timer. This prevents a concurrent idle
149 * exit on that CPU from writing a new set of info at the same time
150 * the timer function runs (the timer function can't use that info
151 * until more time passes).
152 */
153 time_in_idle = pcpu->time_in_idle;
154 idle_exit_time = pcpu->idle_exit_time;
155 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
156 smp_wmb();
157
158 /* If we raced with cancelling a timer, skip. */
159 if (!idle_exit_time)
160 goto exit;
161
162 delta_idle = (unsigned int)(now_idle - time_in_idle);
163 delta_time = (unsigned int)(pcpu->timer_run_time - idle_exit_time);
164
165 /*
166 * If timer ran less than 1ms after short-term sample started, retry.
167 */
168 if (delta_time < 1000)
169 goto rearm;
170
171 if (delta_idle > delta_time)
172 cpu_load = 0;
173 else
174 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
175
Todd Poynor0a92d482012-04-06 19:59:36 -0700176 delta_idle = (unsigned int)(now_idle - pcpu->target_set_time_in_idle);
177 delta_time = (unsigned int)(pcpu->timer_run_time -
178 pcpu->target_set_time);
Mike Chan9d49b702010-06-22 11:26:45 -0700179
180 if ((delta_time == 0) || (delta_idle > delta_time))
181 load_since_change = 0;
182 else
183 load_since_change =
184 100 * (delta_time - delta_idle) / delta_time;
185
186 /*
187 * Choose greater of short-term load (since last idle timer
188 * started or timer function re-armed itself) or long-term load
189 * (since last frequency change).
190 */
191 if (load_since_change > cpu_load)
192 cpu_load = load_since_change;
193
Todd Poynor9fb15312012-04-23 20:42:41 -0700194 if (cpu_load >= go_hispeed_load || boost_val) {
Todd Poynor730910b2012-04-19 12:52:48 -0700195 if (pcpu->target_freq <= pcpu->policy->min) {
Mike Chan9d49b702010-06-22 11:26:45 -0700196 new_freq = hispeed_freq;
Todd Poynor8dc352c2012-04-06 19:50:12 -0700197 } else {
Mike Chan9d49b702010-06-22 11:26:45 -0700198 new_freq = pcpu->policy->max * cpu_load / 100;
Todd Poynor8dc352c2012-04-06 19:50:12 -0700199
200 if (new_freq < hispeed_freq)
201 new_freq = hispeed_freq;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700202
203 if (pcpu->target_freq == hispeed_freq &&
204 new_freq > hispeed_freq &&
Todd Poynor5a5aa702012-05-10 23:28:06 -0700205 pcpu->timer_run_time - pcpu->hispeed_validate_time
Todd Poynor596cf1f2012-04-13 20:18:02 -0700206 < above_hispeed_delay_val) {
207 trace_cpufreq_interactive_notyet(data, cpu_load,
208 pcpu->target_freq,
209 new_freq);
210 goto rearm;
211 }
Todd Poynor8dc352c2012-04-06 19:50:12 -0700212 }
Mike Chan9d49b702010-06-22 11:26:45 -0700213 } else {
Todd Poynor1f53ef22012-04-06 01:13:09 -0700214 new_freq = pcpu->policy->max * cpu_load / 100;
Mike Chan9d49b702010-06-22 11:26:45 -0700215 }
216
Todd Poynor5a5aa702012-05-10 23:28:06 -0700217 if (new_freq <= hispeed_freq)
218 pcpu->hispeed_validate_time = pcpu->timer_run_time;
219
Mike Chan9d49b702010-06-22 11:26:45 -0700220 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
221 new_freq, CPUFREQ_RELATION_H,
222 &index)) {
223 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
224 (int) data);
225 goto rearm;
226 }
227
228 new_freq = pcpu->freq_table[index].frequency;
229
Mike Chan9d49b702010-06-22 11:26:45 -0700230 /*
Todd Poynoraad27322012-04-26 21:41:40 -0700231 * Do not scale below floor_freq unless we have been at or above the
232 * floor frequency for the minimum sample time since last validated.
Mike Chan9d49b702010-06-22 11:26:45 -0700233 */
Todd Poynoraad27322012-04-26 21:41:40 -0700234 if (new_freq < pcpu->floor_freq) {
John Stultz92967112012-05-01 14:10:31 -0700235 if (pcpu->timer_run_time - pcpu->floor_validate_time
Todd Poynora1e19512012-02-16 16:27:59 -0800236 < min_sample_time) {
237 trace_cpufreq_interactive_notyet(data, cpu_load,
238 pcpu->target_freq, new_freq);
Mike Chan9d49b702010-06-22 11:26:45 -0700239 goto rearm;
Todd Poynora1e19512012-02-16 16:27:59 -0800240 }
Mike Chan9d49b702010-06-22 11:26:45 -0700241 }
242
Todd Poynoraad27322012-04-26 21:41:40 -0700243 pcpu->floor_freq = new_freq;
244 pcpu->floor_validate_time = pcpu->timer_run_time;
Todd Poynor0a92d482012-04-06 19:59:36 -0700245
246 if (pcpu->target_freq == new_freq) {
247 trace_cpufreq_interactive_already(data, cpu_load,
248 pcpu->target_freq, new_freq);
249 goto rearm_if_notmax;
250 }
251
Todd Poynora1e19512012-02-16 16:27:59 -0800252 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
253 new_freq);
Todd Poynorbc699d82012-04-20 13:18:32 -0700254 pcpu->target_set_time_in_idle = now_idle;
255 pcpu->target_set_time = pcpu->timer_run_time;
Todd Poynora1e19512012-02-16 16:27:59 -0800256
Todd Poynor02442cf2012-07-16 17:07:15 -0700257 pcpu->target_freq = new_freq;
258 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
259 cpumask_set_cpu(data, &speedchange_cpumask);
260 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
261 wake_up_process(speedchange_task);
Mike Chan9d49b702010-06-22 11:26:45 -0700262
263rearm_if_notmax:
264 /*
265 * Already set max speed and don't see a need to change that,
266 * wait until next idle to re-evaluate, don't need timer.
267 */
268 if (pcpu->target_freq == pcpu->policy->max)
269 goto exit;
270
271rearm:
272 if (!timer_pending(&pcpu->cpu_timer)) {
273 /*
274 * If already at min: if that CPU is idle, don't set timer.
275 * Else cancel the timer if that CPU goes idle. We don't
276 * need to re-evaluate speed until the next idle exit.
277 */
278 if (pcpu->target_freq == pcpu->policy->min) {
279 smp_rmb();
280
281 if (pcpu->idling)
282 goto exit;
283
284 pcpu->timer_idlecancel = 1;
285 }
286
287 pcpu->time_in_idle = get_cpu_idle_time_us(
288 data, &pcpu->idle_exit_time);
289 mod_timer(&pcpu->cpu_timer,
290 jiffies + usecs_to_jiffies(timer_rate));
291 }
292
293exit:
294 return;
295}
296
297static void cpufreq_interactive_idle_start(void)
298{
299 struct cpufreq_interactive_cpuinfo *pcpu =
300 &per_cpu(cpuinfo, smp_processor_id());
301 int pending;
302
303 if (!pcpu->governor_enabled)
304 return;
305
306 pcpu->idling = 1;
307 smp_wmb();
308 pending = timer_pending(&pcpu->cpu_timer);
309
310 if (pcpu->target_freq != pcpu->policy->min) {
311#ifdef CONFIG_SMP
312 /*
313 * Entering idle while not at lowest speed. On some
314 * platforms this can hold the other CPU(s) at that speed
315 * even though the CPU is idle. Set a timer to re-evaluate
316 * speed so this idle CPU doesn't hold the other CPUs above
317 * min indefinitely. This should probably be a quirk of
318 * the CPUFreq driver.
319 */
320 if (!pending) {
321 pcpu->time_in_idle = get_cpu_idle_time_us(
322 smp_processor_id(), &pcpu->idle_exit_time);
323 pcpu->timer_idlecancel = 0;
324 mod_timer(&pcpu->cpu_timer,
325 jiffies + usecs_to_jiffies(timer_rate));
326 }
327#endif
328 } else {
329 /*
330 * If at min speed and entering idle after load has
331 * already been evaluated, and a timer has been set just in
332 * case the CPU suddenly goes busy, cancel that timer. The
333 * CPU didn't go busy; we'll recheck things upon idle exit.
334 */
335 if (pending && pcpu->timer_idlecancel) {
336 del_timer(&pcpu->cpu_timer);
337 /*
338 * Ensure last timer run time is after current idle
339 * sample start time, so next idle exit will always
340 * start a new idle sampling period.
341 */
342 pcpu->idle_exit_time = 0;
343 pcpu->timer_idlecancel = 0;
344 }
345 }
346
347}
348
349static void cpufreq_interactive_idle_end(void)
350{
351 struct cpufreq_interactive_cpuinfo *pcpu =
352 &per_cpu(cpuinfo, smp_processor_id());
353
Sam Lefflera04e4412012-06-27 10:12:04 -0700354 if (!pcpu->governor_enabled)
355 return;
356
Mike Chan9d49b702010-06-22 11:26:45 -0700357 pcpu->idling = 0;
358 smp_wmb();
359
360 /*
361 * Arm the timer for 1-2 ticks later if not already, and if the timer
362 * function has already processed the previous load sampling
363 * interval. (If the timer is not pending but has not processed
364 * the previous interval, it is probably racing with us on another
365 * CPU. Let it compute load based on the previous sample and then
366 * re-arm the timer for another interval when it's done, rather
367 * than updating the interval start time to be "now", which doesn't
368 * give the timer function enough time to make a decision on this
369 * run.)
370 */
371 if (timer_pending(&pcpu->cpu_timer) == 0 &&
372 pcpu->timer_run_time >= pcpu->idle_exit_time &&
373 pcpu->governor_enabled) {
374 pcpu->time_in_idle =
375 get_cpu_idle_time_us(smp_processor_id(),
376 &pcpu->idle_exit_time);
377 pcpu->timer_idlecancel = 0;
378 mod_timer(&pcpu->cpu_timer,
379 jiffies + usecs_to_jiffies(timer_rate));
380 }
381
382}
383
Todd Poynor02442cf2012-07-16 17:07:15 -0700384static int cpufreq_interactive_speedchange_task(void *data)
Mike Chan9d49b702010-06-22 11:26:45 -0700385{
386 unsigned int cpu;
387 cpumask_t tmp_mask;
388 unsigned long flags;
389 struct cpufreq_interactive_cpuinfo *pcpu;
390
391 while (1) {
392 set_current_state(TASK_INTERRUPTIBLE);
Todd Poynor02442cf2012-07-16 17:07:15 -0700393 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
Mike Chan9d49b702010-06-22 11:26:45 -0700394
Todd Poynor02442cf2012-07-16 17:07:15 -0700395 if (cpumask_empty(&speedchange_cpumask)) {
396 spin_unlock_irqrestore(&speedchange_cpumask_lock,
397 flags);
Mike Chan9d49b702010-06-22 11:26:45 -0700398 schedule();
399
400 if (kthread_should_stop())
401 break;
402
Todd Poynor02442cf2012-07-16 17:07:15 -0700403 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
Mike Chan9d49b702010-06-22 11:26:45 -0700404 }
405
406 set_current_state(TASK_RUNNING);
Todd Poynor02442cf2012-07-16 17:07:15 -0700407 tmp_mask = speedchange_cpumask;
408 cpumask_clear(&speedchange_cpumask);
409 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
Mike Chan9d49b702010-06-22 11:26:45 -0700410
411 for_each_cpu(cpu, &tmp_mask) {
412 unsigned int j;
413 unsigned int max_freq = 0;
414
415 pcpu = &per_cpu(cpuinfo, cpu);
416 smp_rmb();
417
418 if (!pcpu->governor_enabled)
419 continue;
420
Mike Chan9d49b702010-06-22 11:26:45 -0700421 for_each_cpu(j, pcpu->policy->cpus) {
422 struct cpufreq_interactive_cpuinfo *pjcpu =
423 &per_cpu(cpuinfo, j);
424
425 if (pjcpu->target_freq > max_freq)
426 max_freq = pjcpu->target_freq;
427 }
428
429 if (max_freq != pcpu->policy->cur)
430 __cpufreq_driver_target(pcpu->policy,
431 max_freq,
432 CPUFREQ_RELATION_H);
Todd Poynor02442cf2012-07-16 17:07:15 -0700433 trace_cpufreq_interactive_setspeed(cpu,
434 pcpu->target_freq,
Todd Poynora1e19512012-02-16 16:27:59 -0800435 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700436 }
437 }
438
439 return 0;
440}
441
Todd Poynor7820a652012-04-02 17:17:14 -0700442static void cpufreq_interactive_boost(void)
443{
444 int i;
445 int anyboost = 0;
446 unsigned long flags;
447 struct cpufreq_interactive_cpuinfo *pcpu;
448
Todd Poynor02442cf2012-07-16 17:07:15 -0700449 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
Todd Poynor7820a652012-04-02 17:17:14 -0700450
451 for_each_online_cpu(i) {
452 pcpu = &per_cpu(cpuinfo, i);
453
454 if (pcpu->target_freq < hispeed_freq) {
455 pcpu->target_freq = hispeed_freq;
Todd Poynor02442cf2012-07-16 17:07:15 -0700456 cpumask_set_cpu(i, &speedchange_cpumask);
Todd Poynor7820a652012-04-02 17:17:14 -0700457 pcpu->target_set_time_in_idle =
458 get_cpu_idle_time_us(i, &pcpu->target_set_time);
Todd Poynor5a5aa702012-05-10 23:28:06 -0700459 pcpu->hispeed_validate_time = pcpu->target_set_time;
Todd Poynor7820a652012-04-02 17:17:14 -0700460 anyboost = 1;
461 }
462
463 /*
Todd Poynoraad27322012-04-26 21:41:40 -0700464 * Set floor freq and (re)start timer for when last
465 * validated.
Todd Poynor7820a652012-04-02 17:17:14 -0700466 */
467
Todd Poynoraad27322012-04-26 21:41:40 -0700468 pcpu->floor_freq = hispeed_freq;
469 pcpu->floor_validate_time = ktime_to_us(ktime_get());
Todd Poynor7820a652012-04-02 17:17:14 -0700470 }
471
Todd Poynor02442cf2012-07-16 17:07:15 -0700472 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
Todd Poynor7820a652012-04-02 17:17:14 -0700473
474 if (anyboost)
Todd Poynor02442cf2012-07-16 17:07:15 -0700475 wake_up_process(speedchange_task);
Todd Poynor7820a652012-04-02 17:17:14 -0700476}
477
Todd Poynor9fb15312012-04-23 20:42:41 -0700478/*
479 * Pulsed boost on input event raises CPUs to hispeed_freq and lets
480 * usual algorithm of min_sample_time decide when to allow speed
481 * to drop.
482 */
483
Todd Poynor7820a652012-04-02 17:17:14 -0700484static void cpufreq_interactive_input_event(struct input_handle *handle,
485 unsigned int type,
486 unsigned int code, int value)
487{
Todd Poynor2e739a02012-05-03 00:16:55 -0700488 if (input_boost_val && type == EV_SYN && code == SYN_REPORT) {
489 trace_cpufreq_interactive_boost("input");
Todd Poynor7820a652012-04-02 17:17:14 -0700490 cpufreq_interactive_boost();
Todd Poynor2e739a02012-05-03 00:16:55 -0700491 }
Todd Poynor7820a652012-04-02 17:17:14 -0700492}
493
494static void cpufreq_interactive_input_open(struct work_struct *w)
495{
496 struct cpufreq_interactive_inputopen *io =
497 container_of(w, struct cpufreq_interactive_inputopen,
498 inputopen_work);
499 int error;
500
501 error = input_open_device(io->handle);
502 if (error)
503 input_unregister_handle(io->handle);
504}
505
506static int cpufreq_interactive_input_connect(struct input_handler *handler,
507 struct input_dev *dev,
508 const struct input_device_id *id)
509{
510 struct input_handle *handle;
511 int error;
512
513 pr_info("%s: connect to %s\n", __func__, dev->name);
514 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
515 if (!handle)
516 return -ENOMEM;
517
518 handle->dev = dev;
519 handle->handler = handler;
520 handle->name = "cpufreq_interactive";
521
522 error = input_register_handle(handle);
523 if (error)
524 goto err;
525
526 inputopen.handle = handle;
Todd Poynor02442cf2012-07-16 17:07:15 -0700527 queue_work(inputopen_wq, &inputopen.inputopen_work);
Todd Poynor7820a652012-04-02 17:17:14 -0700528 return 0;
529err:
530 kfree(handle);
531 return error;
532}
533
534static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
535{
536 input_close_device(handle);
537 input_unregister_handle(handle);
538 kfree(handle);
539}
540
541static const struct input_device_id cpufreq_interactive_ids[] = {
542 {
543 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
544 INPUT_DEVICE_ID_MATCH_ABSBIT,
545 .evbit = { BIT_MASK(EV_ABS) },
546 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
547 BIT_MASK(ABS_MT_POSITION_X) |
548 BIT_MASK(ABS_MT_POSITION_Y) },
549 }, /* multi-touch touchscreen */
550 {
551 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
552 INPUT_DEVICE_ID_MATCH_ABSBIT,
553 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
554 .absbit = { [BIT_WORD(ABS_X)] =
555 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
556 }, /* touchpad */
557 { },
558};
559
560static struct input_handler cpufreq_interactive_input_handler = {
561 .event = cpufreq_interactive_input_event,
562 .connect = cpufreq_interactive_input_connect,
563 .disconnect = cpufreq_interactive_input_disconnect,
564 .name = "cpufreq_interactive",
565 .id_table = cpufreq_interactive_ids,
566};
567
Mike Chan9d49b702010-06-22 11:26:45 -0700568static ssize_t show_hispeed_freq(struct kobject *kobj,
569 struct attribute *attr, char *buf)
570{
571 return sprintf(buf, "%llu\n", hispeed_freq);
572}
573
574static ssize_t store_hispeed_freq(struct kobject *kobj,
575 struct attribute *attr, const char *buf,
576 size_t count)
577{
578 int ret;
579 u64 val;
580
581 ret = strict_strtoull(buf, 0, &val);
582 if (ret < 0)
583 return ret;
584 hispeed_freq = val;
585 return count;
586}
587
588static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
589 show_hispeed_freq, store_hispeed_freq);
590
591
592static ssize_t show_go_hispeed_load(struct kobject *kobj,
593 struct attribute *attr, char *buf)
594{
595 return sprintf(buf, "%lu\n", go_hispeed_load);
596}
597
598static ssize_t store_go_hispeed_load(struct kobject *kobj,
599 struct attribute *attr, const char *buf, size_t count)
600{
601 int ret;
602 unsigned long val;
603
604 ret = strict_strtoul(buf, 0, &val);
605 if (ret < 0)
606 return ret;
607 go_hispeed_load = val;
608 return count;
609}
610
611static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
612 show_go_hispeed_load, store_go_hispeed_load);
613
614static ssize_t show_min_sample_time(struct kobject *kobj,
615 struct attribute *attr, char *buf)
616{
617 return sprintf(buf, "%lu\n", min_sample_time);
618}
619
620static ssize_t store_min_sample_time(struct kobject *kobj,
621 struct attribute *attr, const char *buf, size_t count)
622{
623 int ret;
624 unsigned long val;
625
626 ret = strict_strtoul(buf, 0, &val);
627 if (ret < 0)
628 return ret;
629 min_sample_time = val;
630 return count;
631}
632
633static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
634 show_min_sample_time, store_min_sample_time);
635
Todd Poynor596cf1f2012-04-13 20:18:02 -0700636static ssize_t show_above_hispeed_delay(struct kobject *kobj,
637 struct attribute *attr, char *buf)
638{
639 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
640}
641
642static ssize_t store_above_hispeed_delay(struct kobject *kobj,
643 struct attribute *attr,
644 const char *buf, size_t count)
645{
646 int ret;
647 unsigned long val;
648
649 ret = strict_strtoul(buf, 0, &val);
650 if (ret < 0)
651 return ret;
652 above_hispeed_delay_val = val;
653 return count;
654}
655
656define_one_global_rw(above_hispeed_delay);
657
Mike Chan9d49b702010-06-22 11:26:45 -0700658static ssize_t show_timer_rate(struct kobject *kobj,
659 struct attribute *attr, char *buf)
660{
661 return sprintf(buf, "%lu\n", timer_rate);
662}
663
664static ssize_t store_timer_rate(struct kobject *kobj,
665 struct attribute *attr, const char *buf, size_t count)
666{
667 int ret;
668 unsigned long val;
669
670 ret = strict_strtoul(buf, 0, &val);
671 if (ret < 0)
672 return ret;
673 timer_rate = val;
674 return count;
675}
676
677static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
678 show_timer_rate, store_timer_rate);
679
Todd Poynor7820a652012-04-02 17:17:14 -0700680static ssize_t show_input_boost(struct kobject *kobj, struct attribute *attr,
681 char *buf)
682{
683 return sprintf(buf, "%u\n", input_boost_val);
684}
685
686static ssize_t store_input_boost(struct kobject *kobj, struct attribute *attr,
687 const char *buf, size_t count)
688{
689 int ret;
690 unsigned long val;
691
692 ret = strict_strtoul(buf, 0, &val);
693 if (ret < 0)
694 return ret;
695 input_boost_val = val;
696 return count;
697}
698
699define_one_global_rw(input_boost);
700
Todd Poynor9fb15312012-04-23 20:42:41 -0700701static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
702 char *buf)
703{
704 return sprintf(buf, "%d\n", boost_val);
705}
706
707static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
708 const char *buf, size_t count)
709{
710 int ret;
711 unsigned long val;
712
713 ret = kstrtoul(buf, 0, &val);
714 if (ret < 0)
715 return ret;
716
717 boost_val = val;
718
Todd Poynor2e739a02012-05-03 00:16:55 -0700719 if (boost_val) {
720 trace_cpufreq_interactive_boost("on");
Todd Poynor9fb15312012-04-23 20:42:41 -0700721 cpufreq_interactive_boost();
Todd Poynor2e739a02012-05-03 00:16:55 -0700722 } else {
723 trace_cpufreq_interactive_unboost("off");
724 }
Todd Poynor9fb15312012-04-23 20:42:41 -0700725
726 return count;
727}
728
729define_one_global_rw(boost);
730
Todd Poynor2e739a02012-05-03 00:16:55 -0700731static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
732 const char *buf, size_t count)
733{
734 int ret;
735 unsigned long val;
736
737 ret = kstrtoul(buf, 0, &val);
738 if (ret < 0)
739 return ret;
740
741 trace_cpufreq_interactive_boost("pulse");
742 cpufreq_interactive_boost();
743 return count;
744}
745
746static struct global_attr boostpulse =
747 __ATTR(boostpulse, 0200, NULL, store_boostpulse);
748
Mike Chan9d49b702010-06-22 11:26:45 -0700749static struct attribute *interactive_attributes[] = {
750 &hispeed_freq_attr.attr,
751 &go_hispeed_load_attr.attr,
Todd Poynor596cf1f2012-04-13 20:18:02 -0700752 &above_hispeed_delay.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700753 &min_sample_time_attr.attr,
754 &timer_rate_attr.attr,
Todd Poynor7820a652012-04-02 17:17:14 -0700755 &input_boost.attr,
Todd Poynor9fb15312012-04-23 20:42:41 -0700756 &boost.attr,
Todd Poynor2e739a02012-05-03 00:16:55 -0700757 &boostpulse.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700758 NULL,
759};
760
761static struct attribute_group interactive_attr_group = {
762 .attrs = interactive_attributes,
763 .name = "interactive",
764};
765
Sam Lefflera04e4412012-06-27 10:12:04 -0700766static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
767 unsigned long val,
768 void *data)
769{
770 switch (val) {
771 case IDLE_START:
772 cpufreq_interactive_idle_start();
773 break;
774 case IDLE_END:
775 cpufreq_interactive_idle_end();
776 break;
777 }
778
779 return 0;
780}
781
782static struct notifier_block cpufreq_interactive_idle_nb = {
783 .notifier_call = cpufreq_interactive_idle_notifier,
784};
785
Mike Chan9d49b702010-06-22 11:26:45 -0700786static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
787 unsigned int event)
788{
789 int rc;
790 unsigned int j;
791 struct cpufreq_interactive_cpuinfo *pcpu;
792 struct cpufreq_frequency_table *freq_table;
793
794 switch (event) {
795 case CPUFREQ_GOV_START:
796 if (!cpu_online(policy->cpu))
797 return -EINVAL;
798
799 freq_table =
800 cpufreq_frequency_get_table(policy->cpu);
801
802 for_each_cpu(j, policy->cpus) {
803 pcpu = &per_cpu(cpuinfo, j);
804 pcpu->policy = policy;
805 pcpu->target_freq = policy->cur;
806 pcpu->freq_table = freq_table;
Todd Poynor0a92d482012-04-06 19:59:36 -0700807 pcpu->target_set_time_in_idle =
Mike Chan9d49b702010-06-22 11:26:45 -0700808 get_cpu_idle_time_us(j,
Todd Poynor0a92d482012-04-06 19:59:36 -0700809 &pcpu->target_set_time);
Todd Poynoraad27322012-04-26 21:41:40 -0700810 pcpu->floor_freq = pcpu->target_freq;
811 pcpu->floor_validate_time =
Todd Poynorbc699d82012-04-20 13:18:32 -0700812 pcpu->target_set_time;
Todd Poynor5a5aa702012-05-10 23:28:06 -0700813 pcpu->hispeed_validate_time =
814 pcpu->target_set_time;
Mike Chan9d49b702010-06-22 11:26:45 -0700815 pcpu->governor_enabled = 1;
816 smp_wmb();
817 }
818
819 if (!hispeed_freq)
820 hispeed_freq = policy->max;
821
822 /*
823 * Do not register the idle hook and create sysfs
824 * entries if we have already done so.
825 */
826 if (atomic_inc_return(&active_count) > 1)
827 return 0;
828
829 rc = sysfs_create_group(cpufreq_global_kobject,
830 &interactive_attr_group);
831 if (rc)
832 return rc;
833
Todd Poynor7820a652012-04-02 17:17:14 -0700834 rc = input_register_handler(&cpufreq_interactive_input_handler);
835 if (rc)
836 pr_warn("%s: failed to register input handler\n",
837 __func__);
838
Sam Lefflera04e4412012-06-27 10:12:04 -0700839 idle_notifier_register(&cpufreq_interactive_idle_nb);
Mike Chan9d49b702010-06-22 11:26:45 -0700840 break;
841
842 case CPUFREQ_GOV_STOP:
843 for_each_cpu(j, policy->cpus) {
844 pcpu = &per_cpu(cpuinfo, j);
845 pcpu->governor_enabled = 0;
846 smp_wmb();
847 del_timer_sync(&pcpu->cpu_timer);
848
849 /*
850 * Reset idle exit time since we may cancel the timer
851 * before it can run after the last idle exit time,
852 * to avoid tripping the check in idle exit for a timer
853 * that is trying to run.
854 */
855 pcpu->idle_exit_time = 0;
856 }
857
Todd Poynor02442cf2012-07-16 17:07:15 -0700858 flush_work(&inputopen.inputopen_work);
Mike Chan9d49b702010-06-22 11:26:45 -0700859 if (atomic_dec_return(&active_count) > 0)
860 return 0;
861
Sam Lefflera04e4412012-06-27 10:12:04 -0700862 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
Todd Poynor7820a652012-04-02 17:17:14 -0700863 input_unregister_handler(&cpufreq_interactive_input_handler);
Mike Chan9d49b702010-06-22 11:26:45 -0700864 sysfs_remove_group(cpufreq_global_kobject,
865 &interactive_attr_group);
866
867 break;
868
869 case CPUFREQ_GOV_LIMITS:
870 if (policy->max < policy->cur)
871 __cpufreq_driver_target(policy,
872 policy->max, CPUFREQ_RELATION_H);
873 else if (policy->min > policy->cur)
874 __cpufreq_driver_target(policy,
875 policy->min, CPUFREQ_RELATION_L);
876 break;
877 }
878 return 0;
879}
880
Mike Chan9d49b702010-06-22 11:26:45 -0700881static int __init cpufreq_interactive_init(void)
882{
883 unsigned int i;
884 struct cpufreq_interactive_cpuinfo *pcpu;
885 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
886
887 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
888 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700889 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
Mike Chan9d49b702010-06-22 11:26:45 -0700890 timer_rate = DEFAULT_TIMER_RATE;
891
892 /* Initalize per-cpu timers */
893 for_each_possible_cpu(i) {
894 pcpu = &per_cpu(cpuinfo, i);
895 init_timer(&pcpu->cpu_timer);
896 pcpu->cpu_timer.function = cpufreq_interactive_timer;
897 pcpu->cpu_timer.data = i;
898 }
899
Todd Poynor02442cf2012-07-16 17:07:15 -0700900 spin_lock_init(&speedchange_cpumask_lock);
901 speedchange_task =
902 kthread_create(cpufreq_interactive_speedchange_task, NULL,
903 "cfinteractive");
904 if (IS_ERR(speedchange_task))
905 return PTR_ERR(speedchange_task);
Sam Lefflera13f4152012-06-27 12:55:56 -0700906
Todd Poynor02442cf2012-07-16 17:07:15 -0700907 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, &param);
908 get_task_struct(speedchange_task);
Mike Chan9d49b702010-06-22 11:26:45 -0700909
Todd Poynor02442cf2012-07-16 17:07:15 -0700910 inputopen_wq = create_workqueue("cfinteractive");
Mike Chan9d49b702010-06-22 11:26:45 -0700911
Todd Poynor02442cf2012-07-16 17:07:15 -0700912 if (!inputopen_wq)
913 goto err_freetask;
Mike Chan9d49b702010-06-22 11:26:45 -0700914
Todd Poynor7820a652012-04-02 17:17:14 -0700915 INIT_WORK(&inputopen.inputopen_work, cpufreq_interactive_input_open);
Sam Lefflera13f4152012-06-27 12:55:56 -0700916
917 /* NB: wake up so the thread does not look hung to the freezer */
Todd Poynor02442cf2012-07-16 17:07:15 -0700918 wake_up_process(speedchange_task);
Sam Lefflera13f4152012-06-27 12:55:56 -0700919
Mike Chan9d49b702010-06-22 11:26:45 -0700920 return cpufreq_register_governor(&cpufreq_gov_interactive);
921
Todd Poynor02442cf2012-07-16 17:07:15 -0700922err_freetask:
923 put_task_struct(speedchange_task);
Mike Chan9d49b702010-06-22 11:26:45 -0700924 return -ENOMEM;
925}
926
927#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
928fs_initcall(cpufreq_interactive_init);
929#else
930module_init(cpufreq_interactive_init);
931#endif
932
933static void __exit cpufreq_interactive_exit(void)
934{
935 cpufreq_unregister_governor(&cpufreq_gov_interactive);
Todd Poynor02442cf2012-07-16 17:07:15 -0700936 kthread_stop(speedchange_task);
937 put_task_struct(speedchange_task);
938 destroy_workqueue(inputopen_wq);
Mike Chan9d49b702010-06-22 11:26:45 -0700939}
940
941module_exit(cpufreq_interactive_exit);
942
943MODULE_AUTHOR("Mike Chan <mike@android.com>");
944MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
945 "Latency sensitive workloads");
946MODULE_LICENSE("GPL");