blob: 69eeabb967937fdc04941fd2f4bb4380fd84e8c0 [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;
Mike Chan9d49b702010-06-22 11:26:45 -070051 struct cpufreq_policy *policy;
52 struct cpufreq_frequency_table *freq_table;
53 unsigned int target_freq;
54 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/*
96 * Boost to hispeed on touchscreen input.
97 */
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
Mike Chan9d49b702010-06-22 11:26:45 -0700108static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
109 unsigned int event);
110
111#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
112static
113#endif
114struct cpufreq_governor cpufreq_gov_interactive = {
115 .name = "interactive",
116 .governor = cpufreq_governor_interactive,
117 .max_transition_latency = 10000000,
118 .owner = THIS_MODULE,
119};
120
121static void cpufreq_interactive_timer(unsigned long data)
122{
123 unsigned int delta_idle;
124 unsigned int delta_time;
125 int cpu_load;
126 int load_since_change;
127 u64 time_in_idle;
128 u64 idle_exit_time;
129 struct cpufreq_interactive_cpuinfo *pcpu =
130 &per_cpu(cpuinfo, data);
131 u64 now_idle;
132 unsigned int new_freq;
133 unsigned int index;
134 unsigned long flags;
135
136 smp_rmb();
137
138 if (!pcpu->governor_enabled)
139 goto exit;
140
141 /*
142 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
143 * this lets idle exit know the current idle time sample has
144 * been processed, and idle exit can generate a new sample and
145 * re-arm the timer. This prevents a concurrent idle
146 * exit on that CPU from writing a new set of info at the same time
147 * the timer function runs (the timer function can't use that info
148 * until more time passes).
149 */
150 time_in_idle = pcpu->time_in_idle;
151 idle_exit_time = pcpu->idle_exit_time;
152 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
153 smp_wmb();
154
155 /* If we raced with cancelling a timer, skip. */
156 if (!idle_exit_time)
157 goto exit;
158
159 delta_idle = (unsigned int)(now_idle - time_in_idle);
160 delta_time = (unsigned int)(pcpu->timer_run_time - idle_exit_time);
161
162 /*
163 * If timer ran less than 1ms after short-term sample started, retry.
164 */
165 if (delta_time < 1000)
166 goto rearm;
167
168 if (delta_idle > delta_time)
169 cpu_load = 0;
170 else
171 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
172
Todd Poynor0a92d482012-04-06 19:59:36 -0700173 delta_idle = (unsigned int)(now_idle - pcpu->target_set_time_in_idle);
174 delta_time = (unsigned int)(pcpu->timer_run_time -
175 pcpu->target_set_time);
Mike Chan9d49b702010-06-22 11:26:45 -0700176
177 if ((delta_time == 0) || (delta_idle > delta_time))
178 load_since_change = 0;
179 else
180 load_since_change =
181 100 * (delta_time - delta_idle) / delta_time;
182
183 /*
184 * Choose greater of short-term load (since last idle timer
185 * started or timer function re-armed itself) or long-term load
186 * (since last frequency change).
187 */
188 if (load_since_change > cpu_load)
189 cpu_load = load_since_change;
190
191 if (cpu_load >= go_hispeed_load) {
Todd Poynor730910b2012-04-19 12:52:48 -0700192 if (pcpu->target_freq <= pcpu->policy->min) {
Mike Chan9d49b702010-06-22 11:26:45 -0700193 new_freq = hispeed_freq;
Todd Poynor8dc352c2012-04-06 19:50:12 -0700194 } else {
Mike Chan9d49b702010-06-22 11:26:45 -0700195 new_freq = pcpu->policy->max * cpu_load / 100;
Todd Poynor8dc352c2012-04-06 19:50:12 -0700196
197 if (new_freq < hispeed_freq)
198 new_freq = hispeed_freq;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700199
200 if (pcpu->target_freq == hispeed_freq &&
201 new_freq > hispeed_freq &&
202 pcpu->timer_run_time - pcpu->target_set_time
203 < above_hispeed_delay_val) {
204 trace_cpufreq_interactive_notyet(data, cpu_load,
205 pcpu->target_freq,
206 new_freq);
207 goto rearm;
208 }
Todd Poynor8dc352c2012-04-06 19:50:12 -0700209 }
Mike Chan9d49b702010-06-22 11:26:45 -0700210 } else {
Todd Poynor1f53ef22012-04-06 01:13:09 -0700211 new_freq = pcpu->policy->max * cpu_load / 100;
Mike Chan9d49b702010-06-22 11:26:45 -0700212 }
213
214 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
215 new_freq, CPUFREQ_RELATION_H,
216 &index)) {
217 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
218 (int) data);
219 goto rearm;
220 }
221
222 new_freq = pcpu->freq_table[index].frequency;
223
Mike Chan9d49b702010-06-22 11:26:45 -0700224 /*
225 * Do not scale down unless we have been at this frequency for the
Todd Poynorbc699d82012-04-20 13:18:32 -0700226 * minimum sample time since last validated.
Mike Chan9d49b702010-06-22 11:26:45 -0700227 */
228 if (new_freq < pcpu->target_freq) {
Todd Poynorbc699d82012-04-20 13:18:32 -0700229 if (pcpu->timer_run_time - pcpu->target_validate_time
Todd Poynora1e19512012-02-16 16:27:59 -0800230 < min_sample_time) {
231 trace_cpufreq_interactive_notyet(data, cpu_load,
232 pcpu->target_freq, new_freq);
Mike Chan9d49b702010-06-22 11:26:45 -0700233 goto rearm;
Todd Poynora1e19512012-02-16 16:27:59 -0800234 }
Mike Chan9d49b702010-06-22 11:26:45 -0700235 }
236
Todd Poynorbc699d82012-04-20 13:18:32 -0700237 pcpu->target_validate_time = pcpu->timer_run_time;
Todd Poynor0a92d482012-04-06 19:59:36 -0700238
239 if (pcpu->target_freq == new_freq) {
240 trace_cpufreq_interactive_already(data, cpu_load,
241 pcpu->target_freq, new_freq);
242 goto rearm_if_notmax;
243 }
244
Todd Poynora1e19512012-02-16 16:27:59 -0800245 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
246 new_freq);
Todd Poynorbc699d82012-04-20 13:18:32 -0700247 pcpu->target_set_time_in_idle = now_idle;
248 pcpu->target_set_time = pcpu->timer_run_time;
Todd Poynora1e19512012-02-16 16:27:59 -0800249
Mike Chan9d49b702010-06-22 11:26:45 -0700250 if (new_freq < pcpu->target_freq) {
251 pcpu->target_freq = new_freq;
252 spin_lock_irqsave(&down_cpumask_lock, flags);
253 cpumask_set_cpu(data, &down_cpumask);
254 spin_unlock_irqrestore(&down_cpumask_lock, flags);
255 queue_work(down_wq, &freq_scale_down_work);
256 } else {
257 pcpu->target_freq = new_freq;
258 spin_lock_irqsave(&up_cpumask_lock, flags);
259 cpumask_set_cpu(data, &up_cpumask);
260 spin_unlock_irqrestore(&up_cpumask_lock, flags);
261 wake_up_process(up_task);
262 }
263
264rearm_if_notmax:
265 /*
266 * Already set max speed and don't see a need to change that,
267 * wait until next idle to re-evaluate, don't need timer.
268 */
269 if (pcpu->target_freq == pcpu->policy->max)
270 goto exit;
271
272rearm:
273 if (!timer_pending(&pcpu->cpu_timer)) {
274 /*
275 * If already at min: if that CPU is idle, don't set timer.
276 * Else cancel the timer if that CPU goes idle. We don't
277 * need to re-evaluate speed until the next idle exit.
278 */
279 if (pcpu->target_freq == pcpu->policy->min) {
280 smp_rmb();
281
282 if (pcpu->idling)
283 goto exit;
284
285 pcpu->timer_idlecancel = 1;
286 }
287
288 pcpu->time_in_idle = get_cpu_idle_time_us(
289 data, &pcpu->idle_exit_time);
290 mod_timer(&pcpu->cpu_timer,
291 jiffies + usecs_to_jiffies(timer_rate));
292 }
293
294exit:
295 return;
296}
297
298static void cpufreq_interactive_idle_start(void)
299{
300 struct cpufreq_interactive_cpuinfo *pcpu =
301 &per_cpu(cpuinfo, smp_processor_id());
302 int pending;
303
304 if (!pcpu->governor_enabled)
305 return;
306
307 pcpu->idling = 1;
308 smp_wmb();
309 pending = timer_pending(&pcpu->cpu_timer);
310
311 if (pcpu->target_freq != pcpu->policy->min) {
312#ifdef CONFIG_SMP
313 /*
314 * Entering idle while not at lowest speed. On some
315 * platforms this can hold the other CPU(s) at that speed
316 * even though the CPU is idle. Set a timer to re-evaluate
317 * speed so this idle CPU doesn't hold the other CPUs above
318 * min indefinitely. This should probably be a quirk of
319 * the CPUFreq driver.
320 */
321 if (!pending) {
322 pcpu->time_in_idle = get_cpu_idle_time_us(
323 smp_processor_id(), &pcpu->idle_exit_time);
324 pcpu->timer_idlecancel = 0;
325 mod_timer(&pcpu->cpu_timer,
326 jiffies + usecs_to_jiffies(timer_rate));
327 }
328#endif
329 } else {
330 /*
331 * If at min speed and entering idle after load has
332 * already been evaluated, and a timer has been set just in
333 * case the CPU suddenly goes busy, cancel that timer. The
334 * CPU didn't go busy; we'll recheck things upon idle exit.
335 */
336 if (pending && pcpu->timer_idlecancel) {
337 del_timer(&pcpu->cpu_timer);
338 /*
339 * Ensure last timer run time is after current idle
340 * sample start time, so next idle exit will always
341 * start a new idle sampling period.
342 */
343 pcpu->idle_exit_time = 0;
344 pcpu->timer_idlecancel = 0;
345 }
346 }
347
348}
349
350static void cpufreq_interactive_idle_end(void)
351{
352 struct cpufreq_interactive_cpuinfo *pcpu =
353 &per_cpu(cpuinfo, smp_processor_id());
354
355 pcpu->idling = 0;
356 smp_wmb();
357
358 /*
359 * Arm the timer for 1-2 ticks later if not already, and if the timer
360 * function has already processed the previous load sampling
361 * interval. (If the timer is not pending but has not processed
362 * the previous interval, it is probably racing with us on another
363 * CPU. Let it compute load based on the previous sample and then
364 * re-arm the timer for another interval when it's done, rather
365 * than updating the interval start time to be "now", which doesn't
366 * give the timer function enough time to make a decision on this
367 * run.)
368 */
369 if (timer_pending(&pcpu->cpu_timer) == 0 &&
370 pcpu->timer_run_time >= pcpu->idle_exit_time &&
371 pcpu->governor_enabled) {
372 pcpu->time_in_idle =
373 get_cpu_idle_time_us(smp_processor_id(),
374 &pcpu->idle_exit_time);
375 pcpu->timer_idlecancel = 0;
376 mod_timer(&pcpu->cpu_timer,
377 jiffies + usecs_to_jiffies(timer_rate));
378 }
379
380}
381
382static int cpufreq_interactive_up_task(void *data)
383{
384 unsigned int cpu;
385 cpumask_t tmp_mask;
386 unsigned long flags;
387 struct cpufreq_interactive_cpuinfo *pcpu;
388
389 while (1) {
390 set_current_state(TASK_INTERRUPTIBLE);
391 spin_lock_irqsave(&up_cpumask_lock, flags);
392
393 if (cpumask_empty(&up_cpumask)) {
394 spin_unlock_irqrestore(&up_cpumask_lock, flags);
395 schedule();
396
397 if (kthread_should_stop())
398 break;
399
400 spin_lock_irqsave(&up_cpumask_lock, flags);
401 }
402
403 set_current_state(TASK_RUNNING);
404 tmp_mask = up_cpumask;
405 cpumask_clear(&up_cpumask);
406 spin_unlock_irqrestore(&up_cpumask_lock, flags);
407
408 for_each_cpu(cpu, &tmp_mask) {
409 unsigned int j;
410 unsigned int max_freq = 0;
411
412 pcpu = &per_cpu(cpuinfo, cpu);
413 smp_rmb();
414
415 if (!pcpu->governor_enabled)
416 continue;
417
418 mutex_lock(&set_speed_lock);
419
420 for_each_cpu(j, pcpu->policy->cpus) {
421 struct cpufreq_interactive_cpuinfo *pjcpu =
422 &per_cpu(cpuinfo, j);
423
424 if (pjcpu->target_freq > max_freq)
425 max_freq = pjcpu->target_freq;
426 }
427
428 if (max_freq != pcpu->policy->cur)
429 __cpufreq_driver_target(pcpu->policy,
430 max_freq,
431 CPUFREQ_RELATION_H);
432 mutex_unlock(&set_speed_lock);
Todd Poynora1e19512012-02-16 16:27:59 -0800433 trace_cpufreq_interactive_up(cpu, pcpu->target_freq,
434 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700435 }
436 }
437
438 return 0;
439}
440
441static void cpufreq_interactive_freq_down(struct work_struct *work)
442{
443 unsigned int cpu;
444 cpumask_t tmp_mask;
445 unsigned long flags;
446 struct cpufreq_interactive_cpuinfo *pcpu;
447
448 spin_lock_irqsave(&down_cpumask_lock, flags);
449 tmp_mask = down_cpumask;
450 cpumask_clear(&down_cpumask);
451 spin_unlock_irqrestore(&down_cpumask_lock, flags);
452
453 for_each_cpu(cpu, &tmp_mask) {
454 unsigned int j;
455 unsigned int max_freq = 0;
456
457 pcpu = &per_cpu(cpuinfo, cpu);
458 smp_rmb();
459
460 if (!pcpu->governor_enabled)
461 continue;
462
463 mutex_lock(&set_speed_lock);
464
465 for_each_cpu(j, pcpu->policy->cpus) {
466 struct cpufreq_interactive_cpuinfo *pjcpu =
467 &per_cpu(cpuinfo, j);
468
469 if (pjcpu->target_freq > max_freq)
470 max_freq = pjcpu->target_freq;
471 }
472
473 if (max_freq != pcpu->policy->cur)
474 __cpufreq_driver_target(pcpu->policy, max_freq,
475 CPUFREQ_RELATION_H);
476
477 mutex_unlock(&set_speed_lock);
Todd Poynora1e19512012-02-16 16:27:59 -0800478 trace_cpufreq_interactive_down(cpu, pcpu->target_freq,
479 pcpu->policy->cur);
Mike Chan9d49b702010-06-22 11:26:45 -0700480 }
481}
482
Todd Poynor7820a652012-04-02 17:17:14 -0700483static void cpufreq_interactive_boost(void)
484{
485 int i;
486 int anyboost = 0;
487 unsigned long flags;
488 struct cpufreq_interactive_cpuinfo *pcpu;
489
490 trace_cpufreq_interactive_boost(hispeed_freq);
491 spin_lock_irqsave(&up_cpumask_lock, flags);
492
493 for_each_online_cpu(i) {
494 pcpu = &per_cpu(cpuinfo, i);
495
496 if (pcpu->target_freq < hispeed_freq) {
497 pcpu->target_freq = hispeed_freq;
498 cpumask_set_cpu(i, &up_cpumask);
499 pcpu->target_set_time_in_idle =
500 get_cpu_idle_time_us(i, &pcpu->target_set_time);
501 anyboost = 1;
502 }
503
504 /*
505 * Refresh time at which current (possibly being
506 * boosted) speed last validated (reset timer for
507 * allowing speed to drop).
508 */
509
Todd Poynor96aa0fc2012-04-23 21:22:45 -0700510 pcpu->target_validate_time = ktime_to_us(ktime_get());
Todd Poynor7820a652012-04-02 17:17:14 -0700511 }
512
513 spin_unlock_irqrestore(&up_cpumask_lock, flags);
514
515 if (anyboost)
516 wake_up_process(up_task);
517}
518
519static void cpufreq_interactive_input_event(struct input_handle *handle,
520 unsigned int type,
521 unsigned int code, int value)
522{
523 if (input_boost_val && type == EV_SYN && code == SYN_REPORT)
524 cpufreq_interactive_boost();
525}
526
527static void cpufreq_interactive_input_open(struct work_struct *w)
528{
529 struct cpufreq_interactive_inputopen *io =
530 container_of(w, struct cpufreq_interactive_inputopen,
531 inputopen_work);
532 int error;
533
534 error = input_open_device(io->handle);
535 if (error)
536 input_unregister_handle(io->handle);
537}
538
539static int cpufreq_interactive_input_connect(struct input_handler *handler,
540 struct input_dev *dev,
541 const struct input_device_id *id)
542{
543 struct input_handle *handle;
544 int error;
545
546 pr_info("%s: connect to %s\n", __func__, dev->name);
547 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
548 if (!handle)
549 return -ENOMEM;
550
551 handle->dev = dev;
552 handle->handler = handler;
553 handle->name = "cpufreq_interactive";
554
555 error = input_register_handle(handle);
556 if (error)
557 goto err;
558
559 inputopen.handle = handle;
560 queue_work(down_wq, &inputopen.inputopen_work);
561 return 0;
562err:
563 kfree(handle);
564 return error;
565}
566
567static void cpufreq_interactive_input_disconnect(struct input_handle *handle)
568{
569 input_close_device(handle);
570 input_unregister_handle(handle);
571 kfree(handle);
572}
573
574static const struct input_device_id cpufreq_interactive_ids[] = {
575 {
576 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
577 INPUT_DEVICE_ID_MATCH_ABSBIT,
578 .evbit = { BIT_MASK(EV_ABS) },
579 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
580 BIT_MASK(ABS_MT_POSITION_X) |
581 BIT_MASK(ABS_MT_POSITION_Y) },
582 }, /* multi-touch touchscreen */
583 {
584 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
585 INPUT_DEVICE_ID_MATCH_ABSBIT,
586 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
587 .absbit = { [BIT_WORD(ABS_X)] =
588 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
589 }, /* touchpad */
590 { },
591};
592
593static struct input_handler cpufreq_interactive_input_handler = {
594 .event = cpufreq_interactive_input_event,
595 .connect = cpufreq_interactive_input_connect,
596 .disconnect = cpufreq_interactive_input_disconnect,
597 .name = "cpufreq_interactive",
598 .id_table = cpufreq_interactive_ids,
599};
600
Mike Chan9d49b702010-06-22 11:26:45 -0700601static ssize_t show_hispeed_freq(struct kobject *kobj,
602 struct attribute *attr, char *buf)
603{
604 return sprintf(buf, "%llu\n", hispeed_freq);
605}
606
607static ssize_t store_hispeed_freq(struct kobject *kobj,
608 struct attribute *attr, const char *buf,
609 size_t count)
610{
611 int ret;
612 u64 val;
613
614 ret = strict_strtoull(buf, 0, &val);
615 if (ret < 0)
616 return ret;
617 hispeed_freq = val;
618 return count;
619}
620
621static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
622 show_hispeed_freq, store_hispeed_freq);
623
624
625static ssize_t show_go_hispeed_load(struct kobject *kobj,
626 struct attribute *attr, char *buf)
627{
628 return sprintf(buf, "%lu\n", go_hispeed_load);
629}
630
631static ssize_t store_go_hispeed_load(struct kobject *kobj,
632 struct attribute *attr, const char *buf, size_t count)
633{
634 int ret;
635 unsigned long val;
636
637 ret = strict_strtoul(buf, 0, &val);
638 if (ret < 0)
639 return ret;
640 go_hispeed_load = val;
641 return count;
642}
643
644static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
645 show_go_hispeed_load, store_go_hispeed_load);
646
647static ssize_t show_min_sample_time(struct kobject *kobj,
648 struct attribute *attr, char *buf)
649{
650 return sprintf(buf, "%lu\n", min_sample_time);
651}
652
653static ssize_t store_min_sample_time(struct kobject *kobj,
654 struct attribute *attr, const char *buf, size_t count)
655{
656 int ret;
657 unsigned long val;
658
659 ret = strict_strtoul(buf, 0, &val);
660 if (ret < 0)
661 return ret;
662 min_sample_time = val;
663 return count;
664}
665
666static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
667 show_min_sample_time, store_min_sample_time);
668
Todd Poynor596cf1f2012-04-13 20:18:02 -0700669static ssize_t show_above_hispeed_delay(struct kobject *kobj,
670 struct attribute *attr, char *buf)
671{
672 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
673}
674
675static ssize_t store_above_hispeed_delay(struct kobject *kobj,
676 struct attribute *attr,
677 const char *buf, size_t count)
678{
679 int ret;
680 unsigned long val;
681
682 ret = strict_strtoul(buf, 0, &val);
683 if (ret < 0)
684 return ret;
685 above_hispeed_delay_val = val;
686 return count;
687}
688
689define_one_global_rw(above_hispeed_delay);
690
Mike Chan9d49b702010-06-22 11:26:45 -0700691static ssize_t show_timer_rate(struct kobject *kobj,
692 struct attribute *attr, char *buf)
693{
694 return sprintf(buf, "%lu\n", timer_rate);
695}
696
697static ssize_t store_timer_rate(struct kobject *kobj,
698 struct attribute *attr, const char *buf, size_t count)
699{
700 int ret;
701 unsigned long val;
702
703 ret = strict_strtoul(buf, 0, &val);
704 if (ret < 0)
705 return ret;
706 timer_rate = val;
707 return count;
708}
709
710static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
711 show_timer_rate, store_timer_rate);
712
Todd Poynor7820a652012-04-02 17:17:14 -0700713static ssize_t show_input_boost(struct kobject *kobj, struct attribute *attr,
714 char *buf)
715{
716 return sprintf(buf, "%u\n", input_boost_val);
717}
718
719static ssize_t store_input_boost(struct kobject *kobj, struct attribute *attr,
720 const char *buf, size_t count)
721{
722 int ret;
723 unsigned long val;
724
725 ret = strict_strtoul(buf, 0, &val);
726 if (ret < 0)
727 return ret;
728 input_boost_val = val;
729 return count;
730}
731
732define_one_global_rw(input_boost);
733
Mike Chan9d49b702010-06-22 11:26:45 -0700734static struct attribute *interactive_attributes[] = {
735 &hispeed_freq_attr.attr,
736 &go_hispeed_load_attr.attr,
Todd Poynor596cf1f2012-04-13 20:18:02 -0700737 &above_hispeed_delay.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700738 &min_sample_time_attr.attr,
739 &timer_rate_attr.attr,
Todd Poynor7820a652012-04-02 17:17:14 -0700740 &input_boost.attr,
Mike Chan9d49b702010-06-22 11:26:45 -0700741 NULL,
742};
743
744static struct attribute_group interactive_attr_group = {
745 .attrs = interactive_attributes,
746 .name = "interactive",
747};
748
749static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
750 unsigned int event)
751{
752 int rc;
753 unsigned int j;
754 struct cpufreq_interactive_cpuinfo *pcpu;
755 struct cpufreq_frequency_table *freq_table;
756
757 switch (event) {
758 case CPUFREQ_GOV_START:
759 if (!cpu_online(policy->cpu))
760 return -EINVAL;
761
762 freq_table =
763 cpufreq_frequency_get_table(policy->cpu);
764
765 for_each_cpu(j, policy->cpus) {
766 pcpu = &per_cpu(cpuinfo, j);
767 pcpu->policy = policy;
768 pcpu->target_freq = policy->cur;
769 pcpu->freq_table = freq_table;
Todd Poynor0a92d482012-04-06 19:59:36 -0700770 pcpu->target_set_time_in_idle =
Mike Chan9d49b702010-06-22 11:26:45 -0700771 get_cpu_idle_time_us(j,
Todd Poynor0a92d482012-04-06 19:59:36 -0700772 &pcpu->target_set_time);
Todd Poynorbc699d82012-04-20 13:18:32 -0700773 pcpu->target_validate_time =
774 pcpu->target_set_time;
Mike Chan9d49b702010-06-22 11:26:45 -0700775 pcpu->governor_enabled = 1;
776 smp_wmb();
777 }
778
779 if (!hispeed_freq)
780 hispeed_freq = policy->max;
781
782 /*
783 * Do not register the idle hook and create sysfs
784 * entries if we have already done so.
785 */
786 if (atomic_inc_return(&active_count) > 1)
787 return 0;
788
789 rc = sysfs_create_group(cpufreq_global_kobject,
790 &interactive_attr_group);
791 if (rc)
792 return rc;
793
Todd Poynor7820a652012-04-02 17:17:14 -0700794 rc = input_register_handler(&cpufreq_interactive_input_handler);
795 if (rc)
796 pr_warn("%s: failed to register input handler\n",
797 __func__);
798
Mike Chan9d49b702010-06-22 11:26:45 -0700799 break;
800
801 case CPUFREQ_GOV_STOP:
802 for_each_cpu(j, policy->cpus) {
803 pcpu = &per_cpu(cpuinfo, j);
804 pcpu->governor_enabled = 0;
805 smp_wmb();
806 del_timer_sync(&pcpu->cpu_timer);
807
808 /*
809 * Reset idle exit time since we may cancel the timer
810 * before it can run after the last idle exit time,
811 * to avoid tripping the check in idle exit for a timer
812 * that is trying to run.
813 */
814 pcpu->idle_exit_time = 0;
815 }
816
817 flush_work(&freq_scale_down_work);
818 if (atomic_dec_return(&active_count) > 0)
819 return 0;
820
Todd Poynor7820a652012-04-02 17:17:14 -0700821 input_unregister_handler(&cpufreq_interactive_input_handler);
Mike Chan9d49b702010-06-22 11:26:45 -0700822 sysfs_remove_group(cpufreq_global_kobject,
823 &interactive_attr_group);
824
825 break;
826
827 case CPUFREQ_GOV_LIMITS:
828 if (policy->max < policy->cur)
829 __cpufreq_driver_target(policy,
830 policy->max, CPUFREQ_RELATION_H);
831 else if (policy->min > policy->cur)
832 __cpufreq_driver_target(policy,
833 policy->min, CPUFREQ_RELATION_L);
834 break;
835 }
836 return 0;
837}
838
839static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
840 unsigned long val,
841 void *data)
842{
843 switch (val) {
844 case IDLE_START:
845 cpufreq_interactive_idle_start();
846 break;
847 case IDLE_END:
848 cpufreq_interactive_idle_end();
849 break;
850 }
851
852 return 0;
853}
854
855static struct notifier_block cpufreq_interactive_idle_nb = {
856 .notifier_call = cpufreq_interactive_idle_notifier,
857};
858
859static int __init cpufreq_interactive_init(void)
860{
861 unsigned int i;
862 struct cpufreq_interactive_cpuinfo *pcpu;
863 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
864
865 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
866 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Todd Poynor596cf1f2012-04-13 20:18:02 -0700867 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
Mike Chan9d49b702010-06-22 11:26:45 -0700868 timer_rate = DEFAULT_TIMER_RATE;
869
870 /* Initalize per-cpu timers */
871 for_each_possible_cpu(i) {
872 pcpu = &per_cpu(cpuinfo, i);
873 init_timer(&pcpu->cpu_timer);
874 pcpu->cpu_timer.function = cpufreq_interactive_timer;
875 pcpu->cpu_timer.data = i;
876 }
877
878 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
879 "kinteractiveup");
880 if (IS_ERR(up_task))
881 return PTR_ERR(up_task);
882
883 sched_setscheduler_nocheck(up_task, SCHED_FIFO, &param);
884 get_task_struct(up_task);
885
886 /* No rescuer thread, bind to CPU queuing the work for possibly
887 warm cache (probably doesn't matter much). */
888 down_wq = alloc_workqueue("knteractive_down", 0, 1);
889
890 if (!down_wq)
891 goto err_freeuptask;
892
893 INIT_WORK(&freq_scale_down_work,
894 cpufreq_interactive_freq_down);
895
896 spin_lock_init(&up_cpumask_lock);
897 spin_lock_init(&down_cpumask_lock);
898 mutex_init(&set_speed_lock);
899
900 idle_notifier_register(&cpufreq_interactive_idle_nb);
Todd Poynor7820a652012-04-02 17:17:14 -0700901 INIT_WORK(&inputopen.inputopen_work, cpufreq_interactive_input_open);
Mike Chan9d49b702010-06-22 11:26:45 -0700902 return cpufreq_register_governor(&cpufreq_gov_interactive);
903
904err_freeuptask:
905 put_task_struct(up_task);
906 return -ENOMEM;
907}
908
909#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
910fs_initcall(cpufreq_interactive_init);
911#else
912module_init(cpufreq_interactive_init);
913#endif
914
915static void __exit cpufreq_interactive_exit(void)
916{
917 cpufreq_unregister_governor(&cpufreq_gov_interactive);
918 kthread_stop(up_task);
919 put_task_struct(up_task);
920 destroy_workqueue(down_wq);
921}
922
923module_exit(cpufreq_interactive_exit);
924
925MODULE_AUTHOR("Mike Chan <mike@android.com>");
926MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
927 "Latency sensitive workloads");
928MODULE_LICENSE("GPL");