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