blob: e1310c449dcbfc5bc99b9dea4e9dff209aa14661 [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
497 trace_cpufreq_interactive_boost(hispeed_freq);
498 spin_lock_irqsave(&up_cpumask_lock, flags);
499
500 for_each_online_cpu(i) {
501 pcpu = &per_cpu(cpuinfo, i);
502
503 if (pcpu->target_freq < hispeed_freq) {
504 pcpu->target_freq = hispeed_freq;
505 cpumask_set_cpu(i, &up_cpumask);
506 pcpu->target_set_time_in_idle =
507 get_cpu_idle_time_us(i, &pcpu->target_set_time);
508 anyboost = 1;
509 }
510
511 /*
Todd Poynoraad27322012-04-26 21:41:40 -0700512 * Set floor freq and (re)start timer for when last
513 * validated.
Todd Poynor7820a652012-04-02 17:17:14 -0700514 */
515
Todd Poynoraad27322012-04-26 21:41:40 -0700516 pcpu->floor_freq = hispeed_freq;
517 pcpu->floor_validate_time = ktime_to_us(ktime_get());
Todd Poynor7820a652012-04-02 17:17:14 -0700518 }
519
520 spin_unlock_irqrestore(&up_cpumask_lock, flags);
521
522 if (anyboost)
523 wake_up_process(up_task);
524}
525
Todd Poynor9fb15312012-04-23 20:42:41 -0700526/*
527 * Pulsed boost on input event raises CPUs to hispeed_freq and lets
528 * usual algorithm of min_sample_time decide when to allow speed
529 * to drop.
530 */
531
Todd Poynor7820a652012-04-02 17:17:14 -0700532static void cpufreq_interactive_input_event(struct input_handle *handle,
533 unsigned int type,
534 unsigned int code, int value)
535{
536 if (input_boost_val && type == EV_SYN && code == SYN_REPORT)
537 cpufreq_interactive_boost();
538}
539
540static void cpufreq_interactive_input_open(struct work_struct *w)
541{
542 struct cpufreq_interactive_inputopen *io =
543 container_of(w, struct cpufreq_interactive_inputopen,
544 inputopen_work);
545 int error;
546
547 error = input_open_device(io->handle);
548 if (error)
549 input_unregister_handle(io->handle);
550}
551
552static int cpufreq_interactive_input_connect(struct input_handler *handler,
553 struct input_dev *dev,
554 const struct input_device_id *id)
555{
556 struct input_handle *handle;
557 int error;
558
559 pr_info("%s: connect to %s\n", __func__, dev->name);
560 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
561 if (!handle)
562 return -ENOMEM;
563
564 handle->dev = dev;
565 handle->handler = handler;
566 handle->name = "cpufreq_interactive";
567
568 error = input_register_handle(handle);
569 if (error)
570 goto err;
571
572 inputopen.handle = handle;
573 queue_work(down_wq, &inputopen.inputopen_work);
574 return 0;
575err:
576 kfree(handle);
577 return error;
578}
579
580static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
581{
582 input_close_device(handle);
583 input_unregister_handle(handle);
584 kfree(handle);
585}
586
587static const struct input_device_id cpufreq_interactive_ids[] = {
588 {
589 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
590 INPUT_DEVICE_ID_MATCH_ABSBIT,
591 .evbit = { BIT_MASK(EV_ABS) },
592 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
593 BIT_MASK(ABS_MT_POSITION_X) |
594 BIT_MASK(ABS_MT_POSITION_Y) },
595 }, /* multi-touch touchscreen */
596 {
597 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
598 INPUT_DEVICE_ID_MATCH_ABSBIT,
599 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
600 .absbit = { [BIT_WORD(ABS_X)] =
601 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
602 }, /* touchpad */
603 { },
604};
605
606static struct input_handler cpufreq_interactive_input_handler = {
607 .event = cpufreq_interactive_input_event,
608 .connect = cpufreq_interactive_input_connect,
609 .disconnect = cpufreq_interactive_input_disconnect,
610 .name = "cpufreq_interactive",
611 .id_table = cpufreq_interactive_ids,
612};
613
Mike Chan9d49b702010-06-22 11:26:45 -0700614static ssize_t show_hispeed_freq(struct kobject *kobj,
615 struct attribute *attr, char *buf)
616{
617 return sprintf(buf, "%llu\n", hispeed_freq);
618}
619
620static ssize_t store_hispeed_freq(struct kobject *kobj,
621 struct attribute *attr, const char *buf,
622 size_t count)
623{
624 int ret;
625 u64 val;
626
627 ret = strict_strtoull(buf, 0, &val);
628 if (ret < 0)
629 return ret;
630 hispeed_freq = val;
631 return count;
632}
633
634static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
635 show_hispeed_freq, store_hispeed_freq);
636
637
638static ssize_t show_go_hispeed_load(struct kobject *kobj,
639 struct attribute *attr, char *buf)
640{
641 return sprintf(buf, "%lu\n", go_hispeed_load);
642}
643
644static ssize_t store_go_hispeed_load(struct kobject *kobj,
645 struct attribute *attr, const char *buf, size_t count)
646{
647 int ret;
648 unsigned long val;
649
650 ret = strict_strtoul(buf, 0, &val);
651 if (ret < 0)
652 return ret;
653 go_hispeed_load = val;
654 return count;
655}
656
657static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
658 show_go_hispeed_load, store_go_hispeed_load);
659
660static ssize_t show_min_sample_time(struct kobject *kobj,
661 struct attribute *attr, char *buf)
662{
663 return sprintf(buf, "%lu\n", min_sample_time);
664}
665
666static ssize_t store_min_sample_time(struct kobject *kobj,
667 struct attribute *attr, const char *buf, size_t count)
668{
669 int ret;
670 unsigned long val;
671
672 ret = strict_strtoul(buf, 0, &val);
673 if (ret < 0)
674 return ret;
675 min_sample_time = val;
676 return count;
677}
678
679static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
680 show_min_sample_time, store_min_sample_time);
681
Todd Poynor596cf1f2012-04-13 20:18:02 -0700682static ssize_t show_above_hispeed_delay(struct kobject *kobj,
683 struct attribute *attr, char *buf)
684{
685 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
686}
687
688static ssize_t store_above_hispeed_delay(struct kobject *kobj,
689 struct attribute *attr,
690 const char *buf, size_t count)
691{
692 int ret;
693 unsigned long val;
694
695 ret = strict_strtoul(buf, 0, &val);
696 if (ret < 0)
697 return ret;
698 above_hispeed_delay_val = val;
699 return count;
700}
701
702define_one_global_rw(above_hispeed_delay);
703
Mike Chan9d49b702010-06-22 11:26:45 -0700704static ssize_t show_timer_rate(struct kobject *kobj,
705 struct attribute *attr, char *buf)
706{
707 return sprintf(buf, "%lu\n", timer_rate);
708}
709
710static ssize_t store_timer_rate(struct kobject *kobj,
711 struct attribute *attr, const char *buf, size_t count)
712{
713 int ret;
714 unsigned long val;
715
716 ret = strict_strtoul(buf, 0, &val);
717 if (ret < 0)
718 return ret;
719 timer_rate = val;
720 return count;
721}
722
723static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
724 show_timer_rate, store_timer_rate);
725
Todd Poynor7820a652012-04-02 17:17:14 -0700726static ssize_t show_input_boost(struct kobject *kobj, struct attribute *attr,
727 char *buf)
728{
729 return sprintf(buf, "%u\n", input_boost_val);
730}
731
732static ssize_t store_input_boost(struct kobject *kobj, struct attribute *attr,
733 const char *buf, size_t count)
734{
735 int ret;
736 unsigned long val;
737
738 ret = strict_strtoul(buf, 0, &val);
739 if (ret < 0)
740 return ret;
741 input_boost_val = val;
742 return count;
743}
744
745define_one_global_rw(input_boost);
746
Todd Poynor9fb15312012-04-23 20:42:41 -0700747static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
748 char *buf)
749{
750 return sprintf(buf, "%d\n", boost_val);
751}
752
753static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
754 const char *buf, size_t count)
755{
756 int ret;
757 unsigned long val;
758
759 ret = kstrtoul(buf, 0, &val);
760 if (ret < 0)
761 return ret;
762
763 boost_val = val;
764
765 if (boost_val)
766 cpufreq_interactive_boost();
767 else
768 trace_cpufreq_interactive_unboost(hispeed_freq);
769
770 return count;
771}
772
773define_one_global_rw(boost);
774
Mike Chan9d49b702010-06-22 11:26:45 -0700775static struct attribute *interactive_attributes[] = {
776 &hispeed_freq_attr.attr,
777 &go_hispeed_load_attr.attr,
Todd Poynor596cf1f2012-04-13 20:18:02 -0700778 &above_hispeed_delay.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700779 &min_sample_time_attr.attr,
780 &timer_rate_attr.attr,
Todd Poynor7820a652012-04-02 17:17:14 -0700781 &input_boost.attr,
Todd Poynor9fb15312012-04-23 20:42:41 -0700782 &boost.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700783 NULL,
784};
785
786static struct attribute_group interactive_attr_group = {
787 .attrs = interactive_attributes,
788 .name = "interactive",
789};
790
791static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
792 unsigned int event)
793{
794 int rc;
795 unsigned int j;
796 struct cpufreq_interactive_cpuinfo *pcpu;
797 struct cpufreq_frequency_table *freq_table;
798
799 switch (event) {
800 case CPUFREQ_GOV_START:
801 if (!cpu_online(policy->cpu))
802 return -EINVAL;
803
804 freq_table =
805 cpufreq_frequency_get_table(policy->cpu);
806
807 for_each_cpu(j, policy->cpus) {
808 pcpu = &per_cpu(cpuinfo, j);
809 pcpu->policy = policy;
810 pcpu->target_freq = policy->cur;
811 pcpu->freq_table = freq_table;
Todd Poynor0a92d482012-04-06 19:59:36 -0700812 pcpu->target_set_time_in_idle =
Mike Chan9d49b702010-06-22 11:26:45 -0700813 get_cpu_idle_time_us(j,
Todd Poynor0a92d482012-04-06 19:59:36 -0700814 &pcpu->target_set_time);
Todd Poynoraad27322012-04-26 21:41:40 -0700815 pcpu->floor_freq = pcpu->target_freq;
816 pcpu->floor_validate_time =
Todd Poynorbc699d82012-04-20 13:18:32 -0700817 pcpu->target_set_time;
Mike Chan9d49b702010-06-22 11:26:45 -0700818 pcpu->governor_enabled = 1;
819 smp_wmb();
820 }
821
822 if (!hispeed_freq)
823 hispeed_freq = policy->max;
824
825 /*
826 * Do not register the idle hook and create sysfs
827 * entries if we have already done so.
828 */
829 if (atomic_inc_return(&active_count) > 1)
830 return 0;
831
832 rc = sysfs_create_group(cpufreq_global_kobject,
833 &interactive_attr_group);
834 if (rc)
835 return rc;
836
Todd Poynor7820a652012-04-02 17:17:14 -0700837 rc = input_register_handler(&cpufreq_interactive_input_handler);
838 if (rc)
839 pr_warn("%s: failed to register input handler\n",
840 __func__);
841
Mike Chan9d49b702010-06-22 11:26:45 -0700842 break;
843
844 case CPUFREQ_GOV_STOP:
845 for_each_cpu(j, policy->cpus) {
846 pcpu = &per_cpu(cpuinfo, j);
847 pcpu->governor_enabled = 0;
848 smp_wmb();
849 del_timer_sync(&pcpu->cpu_timer);
850
851 /*
852 * Reset idle exit time since we may cancel the timer
853 * before it can run after the last idle exit time,
854 * to avoid tripping the check in idle exit for a timer
855 * that is trying to run.
856 */
857 pcpu->idle_exit_time = 0;
858 }
859
860 flush_work(&freq_scale_down_work);
861 if (atomic_dec_return(&active_count) > 0)
862 return 0;
863
Todd Poynor7820a652012-04-02 17:17:14 -0700864 input_unregister_handler(&cpufreq_interactive_input_handler);
Mike Chan9d49b702010-06-22 11:26:45 -0700865 sysfs_remove_group(cpufreq_global_kobject,
866 &interactive_attr_group);
867
868 break;
869
870 case CPUFREQ_GOV_LIMITS:
871 if (policy->max < policy->cur)
872 __cpufreq_driver_target(policy,
873 policy->max, CPUFREQ_RELATION_H);
874 else if (policy->min > policy->cur)
875 __cpufreq_driver_target(policy,
876 policy->min, CPUFREQ_RELATION_L);
877 break;
878 }
879 return 0;
880}
881
882static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
883 unsigned long val,
884 void *data)
885{
886 switch (val) {
887 case IDLE_START:
888 cpufreq_interactive_idle_start();
889 break;
890 case IDLE_END:
891 cpufreq_interactive_idle_end();
892 break;
893 }
894
895 return 0;
896}
897
898static struct notifier_block cpufreq_interactive_idle_nb = {
899 .notifier_call = cpufreq_interactive_idle_notifier,
900};
901
902static int __init cpufreq_interactive_init(void)
903{
904 unsigned int i;
905 struct cpufreq_interactive_cpuinfo *pcpu;
906 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
907
908 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
909 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700910 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
Mike Chan9d49b702010-06-22 11:26:45 -0700911 timer_rate = DEFAULT_TIMER_RATE;
912
913 /* Initalize per-cpu timers */
914 for_each_possible_cpu(i) {
915 pcpu = &per_cpu(cpuinfo, i);
916 init_timer(&pcpu->cpu_timer);
917 pcpu->cpu_timer.function = cpufreq_interactive_timer;
918 pcpu->cpu_timer.data = i;
919 }
920
921 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
922 "kinteractiveup");
923 if (IS_ERR(up_task))
924 return PTR_ERR(up_task);
925
926 sched_setscheduler_nocheck(up_task, SCHED_FIFO, &param);
927 get_task_struct(up_task);
928
929 /* No rescuer thread, bind to CPU queuing the work for possibly
930 warm cache (probably doesn't matter much). */
931 down_wq = alloc_workqueue("knteractive_down", 0, 1);
932
933 if (!down_wq)
934 goto err_freeuptask;
935
936 INIT_WORK(&freq_scale_down_work,
937 cpufreq_interactive_freq_down);
938
939 spin_lock_init(&up_cpumask_lock);
940 spin_lock_init(&down_cpumask_lock);
941 mutex_init(&set_speed_lock);
942
943 idle_notifier_register(&cpufreq_interactive_idle_nb);
Todd Poynor7820a652012-04-02 17:17:14 -0700944 INIT_WORK(&inputopen.inputopen_work, cpufreq_interactive_input_open);
Mike Chan9d49b702010-06-22 11:26:45 -0700945 return cpufreq_register_governor(&cpufreq_gov_interactive);
946
947err_freeuptask:
948 put_task_struct(up_task);
949 return -ENOMEM;
950}
951
952#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
953fs_initcall(cpufreq_interactive_init);
954#else
955module_init(cpufreq_interactive_init);
956#endif
957
958static void __exit cpufreq_interactive_exit(void)
959{
960 cpufreq_unregister_governor(&cpufreq_gov_interactive);
961 kthread_stop(up_task);
962 put_task_struct(up_task);
963 destroy_workqueue(down_wq);
964}
965
966module_exit(cpufreq_interactive_exit);
967
968MODULE_AUTHOR("Mike Chan <mike@android.com>");
969MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
970 "Latency sensitive workloads");
971MODULE_LICENSE("GPL");