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