blob: d4cb1eb8a936f3492990e11ec1aaed70be6797dc [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>
Mike Chan9d49b702010-06-22 11:26:45 -070033
Todd Poynora1e19512012-02-16 16:27:59 -080034#define CREATE_TRACE_POINTS
35#include <trace/events/cpufreq_interactive.h>
36
Mike Chan9d49b702010-06-22 11:26:45 -070037#include <asm/cputime.h>
38
39static atomic_t active_count = ATOMIC_INIT(0);
40
41struct cpufreq_interactive_cpuinfo {
42 struct timer_list cpu_timer;
43 int timer_idlecancel;
44 u64 time_in_idle;
45 u64 idle_exit_time;
46 u64 timer_run_time;
47 int idling;
Todd Poynor0a92d482012-04-06 19:59:36 -070048 u64 target_set_time;
49 u64 target_set_time_in_idle;
Todd Poynorbc699d82012-04-20 13:18:32 -070050 u64 target_validate_time;
51 u64 target_validate_time_in_idle;
Mike Chan9d49b702010-06-22 11:26:45 -070052 struct cpufreq_policy *policy;
53 struct cpufreq_frequency_table *freq_table;
54 unsigned int target_freq;
55 int governor_enabled;
56};
57
58static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
59
60/* Workqueues handle frequency scaling */
61static struct task_struct *up_task;
62static struct workqueue_struct *down_wq;
63static struct work_struct freq_scale_down_work;
64static cpumask_t up_cpumask;
65static spinlock_t up_cpumask_lock;
66static cpumask_t down_cpumask;
67static spinlock_t down_cpumask_lock;
68static struct mutex set_speed_lock;
69
70/* Hi speed to bump to from lo speed when load burst (default max) */
71static u64 hispeed_freq;
72
73/* Go to hi speed when CPU load at or above this value. */
Todd Poynora0ec4362012-04-17 17:39:34 -070074#define DEFAULT_GO_HISPEED_LOAD 85
Mike Chan9d49b702010-06-22 11:26:45 -070075static unsigned long go_hispeed_load;
76
77/*
78 * The minimum amount of time to spend at a frequency before we can ramp down.
79 */
Todd Poynora0ec4362012-04-17 17:39:34 -070080#define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
Mike Chan9d49b702010-06-22 11:26:45 -070081static unsigned long min_sample_time;
82
83/*
84 * The sample rate of the timer used to increase frequency
85 */
Todd Poynora0ec4362012-04-17 17:39:34 -070086#define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
Mike Chan9d49b702010-06-22 11:26:45 -070087static unsigned long timer_rate;
88
Todd Poynor596cf1f2012-04-13 20:18:02 -070089/*
90 * Wait this long before raising speed above hispeed, by default a single
91 * timer interval.
92 */
93#define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
94static unsigned long above_hispeed_delay_val;
95
Todd Poynor7820a652012-04-02 17:17:14 -070096/*
97 * Boost to hispeed on touchscreen input.
98 */
99
100static int input_boost_val;
101
102struct cpufreq_interactive_inputopen {
103 struct input_handle *handle;
104 struct work_struct inputopen_work;
105};
106
107static struct cpufreq_interactive_inputopen inputopen;
108
Mike Chan9d49b702010-06-22 11:26:45 -0700109static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
110 unsigned int event);
111
112#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
113static
114#endif
115struct cpufreq_governor cpufreq_gov_interactive = {
116 .name = "interactive",
117 .governor = cpufreq_governor_interactive,
118 .max_transition_latency = 10000000,
119 .owner = THIS_MODULE,
120};
121
122static void cpufreq_interactive_timer(unsigned long data)
123{
124 unsigned int delta_idle;
125 unsigned int delta_time;
126 int cpu_load;
127 int load_since_change;
128 u64 time_in_idle;
129 u64 idle_exit_time;
130 struct cpufreq_interactive_cpuinfo *pcpu =
131 &per_cpu(cpuinfo, data);
132 u64 now_idle;
133 unsigned int new_freq;
134 unsigned int index;
135 unsigned long flags;
136
137 smp_rmb();
138
139 if (!pcpu->governor_enabled)
140 goto exit;
141
142 /*
143 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
144 * this lets idle exit know the current idle time sample has
145 * been processed, and idle exit can generate a new sample and
146 * re-arm the timer. This prevents a concurrent idle
147 * exit on that CPU from writing a new set of info at the same time
148 * the timer function runs (the timer function can't use that info
149 * until more time passes).
150 */
151 time_in_idle = pcpu->time_in_idle;
152 idle_exit_time = pcpu->idle_exit_time;
153 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
154 smp_wmb();
155
156 /* If we raced with cancelling a timer, skip. */
157 if (!idle_exit_time)
158 goto exit;
159
160 delta_idle = (unsigned int)(now_idle - time_in_idle);
161 delta_time = (unsigned int)(pcpu->timer_run_time - idle_exit_time);
162
163 /*
164 * If timer ran less than 1ms after short-term sample started, retry.
165 */
166 if (delta_time < 1000)
167 goto rearm;
168
169 if (delta_idle > delta_time)
170 cpu_load = 0;
171 else
172 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
173
Todd Poynor0a92d482012-04-06 19:59:36 -0700174 delta_idle = (unsigned int)(now_idle - pcpu->target_set_time_in_idle);
175 delta_time = (unsigned int)(pcpu->timer_run_time -
176 pcpu->target_set_time);
Mike Chan9d49b702010-06-22 11:26:45 -0700177
178 if ((delta_time == 0) || (delta_idle > delta_time))
179 load_since_change = 0;
180 else
181 load_since_change =
182 100 * (delta_time - delta_idle) / delta_time;
183
184 /*
185 * Choose greater of short-term load (since last idle timer
186 * started or timer function re-armed itself) or long-term load
187 * (since last frequency change).
188 */
189 if (load_since_change > cpu_load)
190 cpu_load = load_since_change;
191
192 if (cpu_load >= go_hispeed_load) {
Todd Poynor730910b2012-04-19 12:52:48 -0700193 if (pcpu->target_freq <= pcpu->policy->min) {
Mike Chan9d49b702010-06-22 11:26:45 -0700194 new_freq = hispeed_freq;
Todd Poynor8dc352c2012-04-06 19:50:12 -0700195 } else {
Mike Chan9d49b702010-06-22 11:26:45 -0700196 new_freq = pcpu->policy->max * cpu_load / 100;
Todd Poynor8dc352c2012-04-06 19:50:12 -0700197
198 if (new_freq < hispeed_freq)
199 new_freq = hispeed_freq;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700200
201 if (pcpu->target_freq == hispeed_freq &&
202 new_freq > hispeed_freq &&
203 pcpu->timer_run_time - pcpu->target_set_time
204 < above_hispeed_delay_val) {
205 trace_cpufreq_interactive_notyet(data, cpu_load,
206 pcpu->target_freq,
207 new_freq);
208 goto rearm;
209 }
Todd Poynor8dc352c2012-04-06 19:50:12 -0700210 }
Mike Chan9d49b702010-06-22 11:26:45 -0700211 } else {
Todd Poynor1f53ef22012-04-06 01:13:09 -0700212 new_freq = pcpu->policy->max * cpu_load / 100;
Mike Chan9d49b702010-06-22 11:26:45 -0700213 }
214
215 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
216 new_freq, CPUFREQ_RELATION_H,
217 &index)) {
218 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
219 (int) data);
220 goto rearm;
221 }
222
223 new_freq = pcpu->freq_table[index].frequency;
224
Mike Chan9d49b702010-06-22 11:26:45 -0700225 /*
226 * Do not scale down unless we have been at this frequency for the
Todd Poynorbc699d82012-04-20 13:18:32 -0700227 * minimum sample time since last validated.
Mike Chan9d49b702010-06-22 11:26:45 -0700228 */
229 if (new_freq < pcpu->target_freq) {
Todd Poynorbc699d82012-04-20 13:18:32 -0700230 if (pcpu->timer_run_time - pcpu->target_validate_time
Todd Poynora1e19512012-02-16 16:27:59 -0800231 < min_sample_time) {
232 trace_cpufreq_interactive_notyet(data, cpu_load,
233 pcpu->target_freq, new_freq);
Mike Chan9d49b702010-06-22 11:26:45 -0700234 goto rearm;
Todd Poynora1e19512012-02-16 16:27:59 -0800235 }
Mike Chan9d49b702010-06-22 11:26:45 -0700236 }
237
Todd Poynorbc699d82012-04-20 13:18:32 -0700238 pcpu->target_validate_time_in_idle = now_idle;
239 pcpu->target_validate_time = pcpu->timer_run_time;
Todd Poynor0a92d482012-04-06 19:59:36 -0700240
241 if (pcpu->target_freq == new_freq) {
242 trace_cpufreq_interactive_already(data, cpu_load,
243 pcpu->target_freq, new_freq);
244 goto rearm_if_notmax;
245 }
246
Todd Poynora1e19512012-02-16 16:27:59 -0800247 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
248 new_freq);
Todd Poynorbc699d82012-04-20 13:18:32 -0700249 pcpu->target_set_time_in_idle = now_idle;
250 pcpu->target_set_time = pcpu->timer_run_time;
Todd Poynora1e19512012-02-16 16:27:59 -0800251
Mike Chan9d49b702010-06-22 11:26:45 -0700252 if (new_freq < pcpu->target_freq) {
253 pcpu->target_freq = new_freq;
254 spin_lock_irqsave(&down_cpumask_lock, flags);
255 cpumask_set_cpu(data, &down_cpumask);
256 spin_unlock_irqrestore(&down_cpumask_lock, flags);
257 queue_work(down_wq, &freq_scale_down_work);
258 } else {
259 pcpu->target_freq = new_freq;
260 spin_lock_irqsave(&up_cpumask_lock, flags);
261 cpumask_set_cpu(data, &up_cpumask);
262 spin_unlock_irqrestore(&up_cpumask_lock, flags);
263 wake_up_process(up_task);
264 }
265
266rearm_if_notmax:
267 /*
268 * Already set max speed and don't see a need to change that,
269 * wait until next idle to re-evaluate, don't need timer.
270 */
271 if (pcpu->target_freq == pcpu->policy->max)
272 goto exit;
273
274rearm:
275 if (!timer_pending(&pcpu->cpu_timer)) {
276 /*
277 * If already at min: if that CPU is idle, don't set timer.
278 * Else cancel the timer if that CPU goes idle. We don't
279 * need to re-evaluate speed until the next idle exit.
280 */
281 if (pcpu->target_freq == pcpu->policy->min) {
282 smp_rmb();
283
284 if (pcpu->idling)
285 goto exit;
286
287 pcpu->timer_idlecancel = 1;
288 }
289
290 pcpu->time_in_idle = get_cpu_idle_time_us(
291 data, &pcpu->idle_exit_time);
292 mod_timer(&pcpu->cpu_timer,
293 jiffies + usecs_to_jiffies(timer_rate));
294 }
295
296exit:
297 return;
298}
299
300static void cpufreq_interactive_idle_start(void)
301{
302 struct cpufreq_interactive_cpuinfo *pcpu =
303 &per_cpu(cpuinfo, smp_processor_id());
304 int pending;
305
306 if (!pcpu->governor_enabled)
307 return;
308
309 pcpu->idling = 1;
310 smp_wmb();
311 pending = timer_pending(&pcpu->cpu_timer);
312
313 if (pcpu->target_freq != pcpu->policy->min) {
314#ifdef CONFIG_SMP
315 /*
316 * Entering idle while not at lowest speed. On some
317 * platforms this can hold the other CPU(s) at that speed
318 * even though the CPU is idle. Set a timer to re-evaluate
319 * speed so this idle CPU doesn't hold the other CPUs above
320 * min indefinitely. This should probably be a quirk of
321 * the CPUFreq driver.
322 */
323 if (!pending) {
324 pcpu->time_in_idle = get_cpu_idle_time_us(
325 smp_processor_id(), &pcpu->idle_exit_time);
326 pcpu->timer_idlecancel = 0;
327 mod_timer(&pcpu->cpu_timer,
328 jiffies + usecs_to_jiffies(timer_rate));
329 }
330#endif
331 } else {
332 /*
333 * If at min speed and entering idle after load has
334 * already been evaluated, and a timer has been set just in
335 * case the CPU suddenly goes busy, cancel that timer. The
336 * CPU didn't go busy; we'll recheck things upon idle exit.
337 */
338 if (pending && pcpu->timer_idlecancel) {
339 del_timer(&pcpu->cpu_timer);
340 /*
341 * Ensure last timer run time is after current idle
342 * sample start time, so next idle exit will always
343 * start a new idle sampling period.
344 */
345 pcpu->idle_exit_time = 0;
346 pcpu->timer_idlecancel = 0;
347 }
348 }
349
350}
351
352static void cpufreq_interactive_idle_end(void)
353{
354 struct cpufreq_interactive_cpuinfo *pcpu =
355 &per_cpu(cpuinfo, smp_processor_id());
356
357 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
384static int cpufreq_interactive_up_task(void *data)
385{
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);
393 spin_lock_irqsave(&up_cpumask_lock, flags);
394
395 if (cpumask_empty(&up_cpumask)) {
396 spin_unlock_irqrestore(&up_cpumask_lock, flags);
397 schedule();
398
399 if (kthread_should_stop())
400 break;
401
402 spin_lock_irqsave(&up_cpumask_lock, flags);
403 }
404
405 set_current_state(TASK_RUNNING);
406 tmp_mask = up_cpumask;
407 cpumask_clear(&up_cpumask);
408 spin_unlock_irqrestore(&up_cpumask_lock, flags);
409
410 for_each_cpu(cpu, &tmp_mask) {
411 unsigned int j;
412 unsigned int max_freq = 0;
413
414 pcpu = &per_cpu(cpuinfo, cpu);
415 smp_rmb();
416
417 if (!pcpu->governor_enabled)
418 continue;
419
420 mutex_lock(&set_speed_lock);
421
422 for_each_cpu(j, pcpu->policy->cpus) {
423 struct cpufreq_interactive_cpuinfo *pjcpu =
424 &per_cpu(cpuinfo, j);
425
426 if (pjcpu->target_freq > max_freq)
427 max_freq = pjcpu->target_freq;
428 }
429
430 if (max_freq != pcpu->policy->cur)
431 __cpufreq_driver_target(pcpu->policy,
432 max_freq,
433 CPUFREQ_RELATION_H);
434 mutex_unlock(&set_speed_lock);
Todd Poynora1e19512012-02-16 16:27:59 -0800435 trace_cpufreq_interactive_up(cpu, pcpu->target_freq,
436 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700437 }
438 }
439
440 return 0;
441}
442
443static void cpufreq_interactive_freq_down(struct work_struct *work)
444{
445 unsigned int cpu;
446 cpumask_t tmp_mask;
447 unsigned long flags;
448 struct cpufreq_interactive_cpuinfo *pcpu;
449
450 spin_lock_irqsave(&down_cpumask_lock, flags);
451 tmp_mask = down_cpumask;
452 cpumask_clear(&down_cpumask);
453 spin_unlock_irqrestore(&down_cpumask_lock, flags);
454
455 for_each_cpu(cpu, &tmp_mask) {
456 unsigned int j;
457 unsigned int max_freq = 0;
458
459 pcpu = &per_cpu(cpuinfo, cpu);
460 smp_rmb();
461
462 if (!pcpu->governor_enabled)
463 continue;
464
465 mutex_lock(&set_speed_lock);
466
467 for_each_cpu(j, pcpu->policy->cpus) {
468 struct cpufreq_interactive_cpuinfo *pjcpu =
469 &per_cpu(cpuinfo, j);
470
471 if (pjcpu->target_freq > max_freq)
472 max_freq = pjcpu->target_freq;
473 }
474
475 if (max_freq != pcpu->policy->cur)
476 __cpufreq_driver_target(pcpu->policy, max_freq,
477 CPUFREQ_RELATION_H);
478
479 mutex_unlock(&set_speed_lock);
Todd Poynora1e19512012-02-16 16:27:59 -0800480 trace_cpufreq_interactive_down(cpu, pcpu->target_freq,
481 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700482 }
483}
484
Todd Poynor7820a652012-04-02 17:17:14 -0700485static void cpufreq_interactive_boost(void)
486{
487 int i;
488 int anyboost = 0;
489 unsigned long flags;
490 struct cpufreq_interactive_cpuinfo *pcpu;
491
492 trace_cpufreq_interactive_boost(hispeed_freq);
493 spin_lock_irqsave(&up_cpumask_lock, flags);
494
495 for_each_online_cpu(i) {
496 pcpu = &per_cpu(cpuinfo, i);
497
498 if (pcpu->target_freq < hispeed_freq) {
499 pcpu->target_freq = hispeed_freq;
500 cpumask_set_cpu(i, &up_cpumask);
501 pcpu->target_set_time_in_idle =
502 get_cpu_idle_time_us(i, &pcpu->target_set_time);
503 anyboost = 1;
504 }
505
506 /*
507 * Refresh time at which current (possibly being
508 * boosted) speed last validated (reset timer for
509 * allowing speed to drop).
510 */
511
512 pcpu->target_validate_time_in_idle =
513 get_cpu_idle_time_us(i, &pcpu->target_validate_time);
514 }
515
516 spin_unlock_irqrestore(&up_cpumask_lock, flags);
517
518 if (anyboost)
519 wake_up_process(up_task);
520}
521
522static void cpufreq_interactive_input_event(struct input_handle *handle,
523 unsigned int type,
524 unsigned int code, int value)
525{
526 if (input_boost_val && type == EV_SYN && code == SYN_REPORT)
527 cpufreq_interactive_boost();
528}
529
530static void cpufreq_interactive_input_open(struct work_struct *w)
531{
532 struct cpufreq_interactive_inputopen *io =
533 container_of(w, struct cpufreq_interactive_inputopen,
534 inputopen_work);
535 int error;
536
537 error = input_open_device(io->handle);
538 if (error)
539 input_unregister_handle(io->handle);
540}
541
542static int cpufreq_interactive_input_connect(struct input_handler *handler,
543 struct input_dev *dev,
544 const struct input_device_id *id)
545{
546 struct input_handle *handle;
547 int error;
548
549 pr_info("%s: connect to %s\n", __func__, dev->name);
550 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
551 if (!handle)
552 return -ENOMEM;
553
554 handle->dev = dev;
555 handle->handler = handler;
556 handle->name = "cpufreq_interactive";
557
558 error = input_register_handle(handle);
559 if (error)
560 goto err;
561
562 inputopen.handle = handle;
563 queue_work(down_wq, &inputopen.inputopen_work);
564 return 0;
565err:
566 kfree(handle);
567 return error;
568}
569
570static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
571{
572 input_close_device(handle);
573 input_unregister_handle(handle);
574 kfree(handle);
575}
576
577static const struct input_device_id cpufreq_interactive_ids[] = {
578 {
579 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
580 INPUT_DEVICE_ID_MATCH_ABSBIT,
581 .evbit = { BIT_MASK(EV_ABS) },
582 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
583 BIT_MASK(ABS_MT_POSITION_X) |
584 BIT_MASK(ABS_MT_POSITION_Y) },
585 }, /* multi-touch touchscreen */
586 {
587 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
588 INPUT_DEVICE_ID_MATCH_ABSBIT,
589 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
590 .absbit = { [BIT_WORD(ABS_X)] =
591 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
592 }, /* touchpad */
593 { },
594};
595
596static struct input_handler cpufreq_interactive_input_handler = {
597 .event = cpufreq_interactive_input_event,
598 .connect = cpufreq_interactive_input_connect,
599 .disconnect = cpufreq_interactive_input_disconnect,
600 .name = "cpufreq_interactive",
601 .id_table = cpufreq_interactive_ids,
602};
603
Mike Chan9d49b702010-06-22 11:26:45 -0700604static ssize_t show_hispeed_freq(struct kobject *kobj,
605 struct attribute *attr, char *buf)
606{
607 return sprintf(buf, "%llu\n", hispeed_freq);
608}
609
610static ssize_t store_hispeed_freq(struct kobject *kobj,
611 struct attribute *attr, const char *buf,
612 size_t count)
613{
614 int ret;
615 u64 val;
616
617 ret = strict_strtoull(buf, 0, &val);
618 if (ret < 0)
619 return ret;
620 hispeed_freq = val;
621 return count;
622}
623
624static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
625 show_hispeed_freq, store_hispeed_freq);
626
627
628static ssize_t show_go_hispeed_load(struct kobject *kobj,
629 struct attribute *attr, char *buf)
630{
631 return sprintf(buf, "%lu\n", go_hispeed_load);
632}
633
634static ssize_t store_go_hispeed_load(struct kobject *kobj,
635 struct attribute *attr, const char *buf, size_t count)
636{
637 int ret;
638 unsigned long val;
639
640 ret = strict_strtoul(buf, 0, &val);
641 if (ret < 0)
642 return ret;
643 go_hispeed_load = val;
644 return count;
645}
646
647static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
648 show_go_hispeed_load, store_go_hispeed_load);
649
650static ssize_t show_min_sample_time(struct kobject *kobj,
651 struct attribute *attr, char *buf)
652{
653 return sprintf(buf, "%lu\n", min_sample_time);
654}
655
656static ssize_t store_min_sample_time(struct kobject *kobj,
657 struct attribute *attr, const char *buf, size_t count)
658{
659 int ret;
660 unsigned long val;
661
662 ret = strict_strtoul(buf, 0, &val);
663 if (ret < 0)
664 return ret;
665 min_sample_time = val;
666 return count;
667}
668
669static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
670 show_min_sample_time, store_min_sample_time);
671
Todd Poynor596cf1f2012-04-13 20:18:02 -0700672static ssize_t show_above_hispeed_delay(struct kobject *kobj,
673 struct attribute *attr, char *buf)
674{
675 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
676}
677
678static ssize_t store_above_hispeed_delay(struct kobject *kobj,
679 struct attribute *attr,
680 const char *buf, size_t count)
681{
682 int ret;
683 unsigned long val;
684
685 ret = strict_strtoul(buf, 0, &val);
686 if (ret < 0)
687 return ret;
688 above_hispeed_delay_val = val;
689 return count;
690}
691
692define_one_global_rw(above_hispeed_delay);
693
Mike Chan9d49b702010-06-22 11:26:45 -0700694static ssize_t show_timer_rate(struct kobject *kobj,
695 struct attribute *attr, char *buf)
696{
697 return sprintf(buf, "%lu\n", timer_rate);
698}
699
700static ssize_t store_timer_rate(struct kobject *kobj,
701 struct attribute *attr, const char *buf, size_t count)
702{
703 int ret;
704 unsigned long val;
705
706 ret = strict_strtoul(buf, 0, &val);
707 if (ret < 0)
708 return ret;
709 timer_rate = val;
710 return count;
711}
712
713static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
714 show_timer_rate, store_timer_rate);
715
Todd Poynor7820a652012-04-02 17:17:14 -0700716static ssize_t show_input_boost(struct kobject *kobj, struct attribute *attr,
717 char *buf)
718{
719 return sprintf(buf, "%u\n", input_boost_val);
720}
721
722static ssize_t store_input_boost(struct kobject *kobj, struct attribute *attr,
723 const char *buf, size_t count)
724{
725 int ret;
726 unsigned long val;
727
728 ret = strict_strtoul(buf, 0, &val);
729 if (ret < 0)
730 return ret;
731 input_boost_val = val;
732 return count;
733}
734
735define_one_global_rw(input_boost);
736
Mike Chan9d49b702010-06-22 11:26:45 -0700737static struct attribute *interactive_attributes[] = {
738 &hispeed_freq_attr.attr,
739 &go_hispeed_load_attr.attr,
Todd Poynor596cf1f2012-04-13 20:18:02 -0700740 &above_hispeed_delay.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700741 &min_sample_time_attr.attr,
742 &timer_rate_attr.attr,
Todd Poynor7820a652012-04-02 17:17:14 -0700743 &input_boost.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700744 NULL,
745};
746
747static struct attribute_group interactive_attr_group = {
748 .attrs = interactive_attributes,
749 .name = "interactive",
750};
751
752static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
753 unsigned int event)
754{
755 int rc;
756 unsigned int j;
757 struct cpufreq_interactive_cpuinfo *pcpu;
758 struct cpufreq_frequency_table *freq_table;
759
760 switch (event) {
761 case CPUFREQ_GOV_START:
762 if (!cpu_online(policy->cpu))
763 return -EINVAL;
764
765 freq_table =
766 cpufreq_frequency_get_table(policy->cpu);
767
768 for_each_cpu(j, policy->cpus) {
769 pcpu = &per_cpu(cpuinfo, j);
770 pcpu->policy = policy;
771 pcpu->target_freq = policy->cur;
772 pcpu->freq_table = freq_table;
Todd Poynor0a92d482012-04-06 19:59:36 -0700773 pcpu->target_set_time_in_idle =
Mike Chan9d49b702010-06-22 11:26:45 -0700774 get_cpu_idle_time_us(j,
Todd Poynor0a92d482012-04-06 19:59:36 -0700775 &pcpu->target_set_time);
Todd Poynorbc699d82012-04-20 13:18:32 -0700776 pcpu->target_validate_time =
777 pcpu->target_set_time;
778 pcpu->target_validate_time_in_idle =
779 pcpu->target_set_time_in_idle;
Mike Chan9d49b702010-06-22 11:26:45 -0700780 pcpu->governor_enabled = 1;
781 smp_wmb();
782 }
783
784 if (!hispeed_freq)
785 hispeed_freq = policy->max;
786
787 /*
788 * Do not register the idle hook and create sysfs
789 * entries if we have already done so.
790 */
791 if (atomic_inc_return(&active_count) > 1)
792 return 0;
793
794 rc = sysfs_create_group(cpufreq_global_kobject,
795 &interactive_attr_group);
796 if (rc)
797 return rc;
798
Todd Poynor7820a652012-04-02 17:17:14 -0700799 rc = input_register_handler(&cpufreq_interactive_input_handler);
800 if (rc)
801 pr_warn("%s: failed to register input handler\n",
802 __func__);
803
Mike Chan9d49b702010-06-22 11:26:45 -0700804 break;
805
806 case CPUFREQ_GOV_STOP:
807 for_each_cpu(j, policy->cpus) {
808 pcpu = &per_cpu(cpuinfo, j);
809 pcpu->governor_enabled = 0;
810 smp_wmb();
811 del_timer_sync(&pcpu->cpu_timer);
812
813 /*
814 * Reset idle exit time since we may cancel the timer
815 * before it can run after the last idle exit time,
816 * to avoid tripping the check in idle exit for a timer
817 * that is trying to run.
818 */
819 pcpu->idle_exit_time = 0;
820 }
821
822 flush_work(&freq_scale_down_work);
823 if (atomic_dec_return(&active_count) > 0)
824 return 0;
825
Todd Poynor7820a652012-04-02 17:17:14 -0700826 input_unregister_handler(&cpufreq_interactive_input_handler);
Mike Chan9d49b702010-06-22 11:26:45 -0700827 sysfs_remove_group(cpufreq_global_kobject,
828 &interactive_attr_group);
829
830 break;
831
832 case CPUFREQ_GOV_LIMITS:
833 if (policy->max < policy->cur)
834 __cpufreq_driver_target(policy,
835 policy->max, CPUFREQ_RELATION_H);
836 else if (policy->min > policy->cur)
837 __cpufreq_driver_target(policy,
838 policy->min, CPUFREQ_RELATION_L);
839 break;
840 }
841 return 0;
842}
843
844static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
845 unsigned long val,
846 void *data)
847{
848 switch (val) {
849 case IDLE_START:
850 cpufreq_interactive_idle_start();
851 break;
852 case IDLE_END:
853 cpufreq_interactive_idle_end();
854 break;
855 }
856
857 return 0;
858}
859
860static struct notifier_block cpufreq_interactive_idle_nb = {
861 .notifier_call = cpufreq_interactive_idle_notifier,
862};
863
864static int __init cpufreq_interactive_init(void)
865{
866 unsigned int i;
867 struct cpufreq_interactive_cpuinfo *pcpu;
868 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
869
870 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
871 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700872 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
Mike Chan9d49b702010-06-22 11:26:45 -0700873 timer_rate = DEFAULT_TIMER_RATE;
874
875 /* Initalize per-cpu timers */
876 for_each_possible_cpu(i) {
877 pcpu = &per_cpu(cpuinfo, i);
878 init_timer(&pcpu->cpu_timer);
879 pcpu->cpu_timer.function = cpufreq_interactive_timer;
880 pcpu->cpu_timer.data = i;
881 }
882
883 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
884 "kinteractiveup");
885 if (IS_ERR(up_task))
886 return PTR_ERR(up_task);
887
888 sched_setscheduler_nocheck(up_task, SCHED_FIFO, &param);
889 get_task_struct(up_task);
890
891 /* No rescuer thread, bind to CPU queuing the work for possibly
892 warm cache (probably doesn't matter much). */
893 down_wq = alloc_workqueue("knteractive_down", 0, 1);
894
895 if (!down_wq)
896 goto err_freeuptask;
897
898 INIT_WORK(&freq_scale_down_work,
899 cpufreq_interactive_freq_down);
900
901 spin_lock_init(&up_cpumask_lock);
902 spin_lock_init(&down_cpumask_lock);
903 mutex_init(&set_speed_lock);
904
905 idle_notifier_register(&cpufreq_interactive_idle_nb);
Todd Poynor7820a652012-04-02 17:17:14 -0700906 INIT_WORK(&inputopen.inputopen_work, cpufreq_interactive_input_open);
Mike Chan9d49b702010-06-22 11:26:45 -0700907 return cpufreq_register_governor(&cpufreq_gov_interactive);
908
909err_freeuptask:
910 put_task_struct(up_task);
911 return -ENOMEM;
912}
913
914#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
915fs_initcall(cpufreq_interactive_init);
916#else
917module_init(cpufreq_interactive_init);
918#endif
919
920static void __exit cpufreq_interactive_exit(void)
921{
922 cpufreq_unregister_governor(&cpufreq_gov_interactive);
923 kthread_stop(up_task);
924 put_task_struct(up_task);
925 destroy_workqueue(down_wq);
926}
927
928module_exit(cpufreq_interactive_exit);
929
930MODULE_AUTHOR("Mike Chan <mike@android.com>");
931MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
932 "Latency sensitive workloads");
933MODULE_LICENSE("GPL");