blob: e53eae2214990186bef0026b8bec9fb09fca0f6d [file] [log] [blame]
Mike Chanef969692010-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/sched/rt.h>
26#include <linux/tick.h>
27#include <linux/time.h>
28#include <linux/timer.h>
29#include <linux/workqueue.h>
30#include <linux/kthread.h>
31#include <linux/mutex.h>
Todd Poynorab8dc402012-04-02 17:17:14 -070032#include <linux/slab.h>
Todd Poynor15a9ea02012-04-23 20:42:41 -070033#include <asm/cputime.h>
Mike Chanef969692010-06-22 11:26:45 -070034
Todd Poynorae010472012-02-16 16:27:59 -080035#define CREATE_TRACE_POINTS
36#include <trace/events/cpufreq_interactive.h>
37
Mike Chanef969692010-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;
Todd Poynor1f408dc2012-04-06 19:59:36 -070045 u64 target_set_time;
46 u64 target_set_time_in_idle;
Mike Chanef969692010-06-22 11:26:45 -070047 struct cpufreq_policy *policy;
48 struct cpufreq_frequency_table *freq_table;
49 unsigned int target_freq;
Todd Poynor6d15fa32012-04-26 21:41:40 -070050 unsigned int floor_freq;
51 u64 floor_validate_time;
Todd Poynor1a0389a2012-05-10 23:28:06 -070052 u64 hispeed_validate_time;
Mike Chanef969692010-06-22 11:26:45 -070053 int governor_enabled;
54};
55
56static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
57
Todd Poynor0f1920b2012-07-16 17:07:15 -070058/* realtime thread handles frequency scaling */
59static struct task_struct *speedchange_task;
60static cpumask_t speedchange_cpumask;
61static spinlock_t speedchange_cpumask_lock;
Mike Chanef969692010-06-22 11:26:45 -070062
63/* Hi speed to bump to from lo speed when load burst (default max) */
Todd Poynor3b7b5f82012-10-03 00:39:56 -070064static unsigned int hispeed_freq;
Mike Chanef969692010-06-22 11:26:45 -070065
66/* Go to hi speed when CPU load at or above this value. */
Todd Poynora380aa82012-04-17 17:39:34 -070067#define DEFAULT_GO_HISPEED_LOAD 85
Mike Chanef969692010-06-22 11:26:45 -070068static unsigned long go_hispeed_load;
69
70/*
71 * The minimum amount of time to spend at a frequency before we can ramp down.
72 */
Todd Poynora380aa82012-04-17 17:39:34 -070073#define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC)
Mike Chanef969692010-06-22 11:26:45 -070074static unsigned long min_sample_time;
75
76/*
77 * The sample rate of the timer used to increase frequency
78 */
Todd Poynora380aa82012-04-17 17:39:34 -070079#define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC)
Mike Chanef969692010-06-22 11:26:45 -070080static unsigned long timer_rate;
81
Todd Poynorcbbe17d2012-04-13 20:18:02 -070082/*
83 * Wait this long before raising speed above hispeed, by default a single
84 * timer interval.
85 */
86#define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE
87static unsigned long above_hispeed_delay_val;
88
Todd Poynorab8dc402012-04-02 17:17:14 -070089/*
Todd Poynor15a9ea02012-04-23 20:42:41 -070090 * Non-zero means longer-term speed boost active.
91 */
92
93static int boost_val;
94
Mike Chanef969692010-06-22 11:26:45 -070095static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
96 unsigned int event);
97
98#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
99static
100#endif
101struct cpufreq_governor cpufreq_gov_interactive = {
102 .name = "interactive",
103 .governor = cpufreq_governor_interactive,
104 .max_transition_latency = 10000000,
105 .owner = THIS_MODULE,
106};
107
108static void cpufreq_interactive_timer(unsigned long data)
109{
Todd Poynore7afb7e2012-11-05 13:09:03 -0800110 u64 now;
Mike Chanef969692010-06-22 11:26:45 -0700111 unsigned int delta_idle;
112 unsigned int delta_time;
113 int cpu_load;
114 int load_since_change;
115 u64 time_in_idle;
116 u64 idle_exit_time;
117 struct cpufreq_interactive_cpuinfo *pcpu =
118 &per_cpu(cpuinfo, data);
119 u64 now_idle;
120 unsigned int new_freq;
121 unsigned int index;
122 unsigned long flags;
123
124 smp_rmb();
125
126 if (!pcpu->governor_enabled)
127 goto exit;
128
Mike Chanef969692010-06-22 11:26:45 -0700129 time_in_idle = pcpu->time_in_idle;
130 idle_exit_time = pcpu->idle_exit_time;
Todd Poynore7afb7e2012-11-05 13:09:03 -0800131 now_idle = get_cpu_idle_time_us(data, &now);
Mike Chanef969692010-06-22 11:26:45 -0700132 delta_idle = (unsigned int)(now_idle - time_in_idle);
Todd Poynore7afb7e2012-11-05 13:09:03 -0800133 delta_time = (unsigned int)(now - idle_exit_time);
Mike Chanef969692010-06-22 11:26:45 -0700134
135 /*
136 * If timer ran less than 1ms after short-term sample started, retry.
137 */
138 if (delta_time < 1000)
139 goto rearm;
140
141 if (delta_idle > delta_time)
142 cpu_load = 0;
143 else
144 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
145
Todd Poynor1f408dc2012-04-06 19:59:36 -0700146 delta_idle = (unsigned int)(now_idle - pcpu->target_set_time_in_idle);
Todd Poynore7afb7e2012-11-05 13:09:03 -0800147 delta_time = (unsigned int)(now - pcpu->target_set_time);
Mike Chanef969692010-06-22 11:26:45 -0700148
149 if ((delta_time == 0) || (delta_idle > delta_time))
150 load_since_change = 0;
151 else
152 load_since_change =
153 100 * (delta_time - delta_idle) / delta_time;
154
155 /*
156 * Choose greater of short-term load (since last idle timer
157 * started or timer function re-armed itself) or long-term load
158 * (since last frequency change).
159 */
160 if (load_since_change > cpu_load)
161 cpu_load = load_since_change;
162
Todd Poynor15a9ea02012-04-23 20:42:41 -0700163 if (cpu_load >= go_hispeed_load || boost_val) {
Todd Poynor762d62a2012-09-24 18:03:58 -0700164 if (pcpu->target_freq < hispeed_freq &&
165 hispeed_freq < pcpu->policy->max) {
Mike Chanef969692010-06-22 11:26:45 -0700166 new_freq = hispeed_freq;
Todd Poynorf87b9d52012-04-06 19:50:12 -0700167 } else {
Mike Chanef969692010-06-22 11:26:45 -0700168 new_freq = pcpu->policy->max * cpu_load / 100;
Todd Poynorf87b9d52012-04-06 19:50:12 -0700169
170 if (new_freq < hispeed_freq)
171 new_freq = hispeed_freq;
Todd Poynorcbbe17d2012-04-13 20:18:02 -0700172
173 if (pcpu->target_freq == hispeed_freq &&
174 new_freq > hispeed_freq &&
Todd Poynore7afb7e2012-11-05 13:09:03 -0800175 now - pcpu->hispeed_validate_time
Todd Poynorcbbe17d2012-04-13 20:18:02 -0700176 < above_hispeed_delay_val) {
177 trace_cpufreq_interactive_notyet(data, cpu_load,
178 pcpu->target_freq,
179 new_freq);
180 goto rearm;
181 }
Todd Poynorf87b9d52012-04-06 19:50:12 -0700182 }
Mike Chanef969692010-06-22 11:26:45 -0700183 } else {
Todd Poynor3b7b5f82012-10-03 00:39:56 -0700184 new_freq = hispeed_freq * cpu_load / 100;
Mike Chanef969692010-06-22 11:26:45 -0700185 }
186
Todd Poynor1a0389a2012-05-10 23:28:06 -0700187 if (new_freq <= hispeed_freq)
Todd Poynore7afb7e2012-11-05 13:09:03 -0800188 pcpu->hispeed_validate_time = now;
Todd Poynor1a0389a2012-05-10 23:28:06 -0700189
Mike Chanef969692010-06-22 11:26:45 -0700190 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
191 new_freq, CPUFREQ_RELATION_H,
192 &index)) {
193 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
194 (int) data);
195 goto rearm;
196 }
197
198 new_freq = pcpu->freq_table[index].frequency;
199
Mike Chanef969692010-06-22 11:26:45 -0700200 /*
Todd Poynor6d15fa32012-04-26 21:41:40 -0700201 * Do not scale below floor_freq unless we have been at or above the
202 * floor frequency for the minimum sample time since last validated.
Mike Chanef969692010-06-22 11:26:45 -0700203 */
Todd Poynor6d15fa32012-04-26 21:41:40 -0700204 if (new_freq < pcpu->floor_freq) {
Todd Poynore7afb7e2012-11-05 13:09:03 -0800205 if (now - pcpu->floor_validate_time < min_sample_time) {
Todd Poynorae010472012-02-16 16:27:59 -0800206 trace_cpufreq_interactive_notyet(data, cpu_load,
207 pcpu->target_freq, new_freq);
Mike Chanef969692010-06-22 11:26:45 -0700208 goto rearm;
Todd Poynorae010472012-02-16 16:27:59 -0800209 }
Mike Chanef969692010-06-22 11:26:45 -0700210 }
211
Todd Poynor6d15fa32012-04-26 21:41:40 -0700212 pcpu->floor_freq = new_freq;
Todd Poynore7afb7e2012-11-05 13:09:03 -0800213 pcpu->floor_validate_time = now;
Todd Poynor1f408dc2012-04-06 19:59:36 -0700214
215 if (pcpu->target_freq == new_freq) {
216 trace_cpufreq_interactive_already(data, cpu_load,
217 pcpu->target_freq, new_freq);
218 goto rearm_if_notmax;
219 }
220
Todd Poynorae010472012-02-16 16:27:59 -0800221 trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq,
222 new_freq);
Todd Poynor8a833f12012-04-20 13:18:32 -0700223 pcpu->target_set_time_in_idle = now_idle;
Todd Poynore7afb7e2012-11-05 13:09:03 -0800224 pcpu->target_set_time = now;
Todd Poynorae010472012-02-16 16:27:59 -0800225
Todd Poynor0f1920b2012-07-16 17:07:15 -0700226 pcpu->target_freq = new_freq;
227 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
228 cpumask_set_cpu(data, &speedchange_cpumask);
229 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
230 wake_up_process(speedchange_task);
Mike Chanef969692010-06-22 11:26:45 -0700231
232rearm_if_notmax:
233 /*
234 * Already set max speed and don't see a need to change that,
235 * wait until next idle to re-evaluate, don't need timer.
236 */
237 if (pcpu->target_freq == pcpu->policy->max)
238 goto exit;
239
240rearm:
241 if (!timer_pending(&pcpu->cpu_timer)) {
242 /*
Todd Poynore7afb7e2012-11-05 13:09:03 -0800243 * If already at min, cancel the timer if that CPU goes idle.
244 * We don't need to re-evaluate speed until the next idle exit.
Mike Chanef969692010-06-22 11:26:45 -0700245 */
Todd Poynore7afb7e2012-11-05 13:09:03 -0800246 if (pcpu->target_freq == pcpu->policy->min)
Mike Chanef969692010-06-22 11:26:45 -0700247 pcpu->timer_idlecancel = 1;
Mike Chanef969692010-06-22 11:26:45 -0700248
249 pcpu->time_in_idle = get_cpu_idle_time_us(
250 data, &pcpu->idle_exit_time);
Todd Poynore7afb7e2012-11-05 13:09:03 -0800251 mod_timer_pinned(&pcpu->cpu_timer,
252 jiffies + usecs_to_jiffies(timer_rate));
Mike Chanef969692010-06-22 11:26:45 -0700253 }
254
255exit:
256 return;
257}
258
259static void cpufreq_interactive_idle_start(void)
260{
261 struct cpufreq_interactive_cpuinfo *pcpu =
262 &per_cpu(cpuinfo, smp_processor_id());
263 int pending;
264
265 if (!pcpu->governor_enabled)
266 return;
267
Mike Chanef969692010-06-22 11:26:45 -0700268 pending = timer_pending(&pcpu->cpu_timer);
269
270 if (pcpu->target_freq != pcpu->policy->min) {
271#ifdef CONFIG_SMP
272 /*
273 * Entering idle while not at lowest speed. On some
274 * platforms this can hold the other CPU(s) at that speed
275 * even though the CPU is idle. Set a timer to re-evaluate
276 * speed so this idle CPU doesn't hold the other CPUs above
277 * min indefinitely. This should probably be a quirk of
278 * the CPUFreq driver.
279 */
280 if (!pending) {
281 pcpu->time_in_idle = get_cpu_idle_time_us(
282 smp_processor_id(), &pcpu->idle_exit_time);
283 pcpu->timer_idlecancel = 0;
Todd Poynore7afb7e2012-11-05 13:09:03 -0800284 mod_timer_pinned(
285 &pcpu->cpu_timer,
286 jiffies + usecs_to_jiffies(timer_rate));
Mike Chanef969692010-06-22 11:26:45 -0700287 }
288#endif
289 } else {
290 /*
291 * If at min speed and entering idle after load has
292 * already been evaluated, and a timer has been set just in
293 * case the CPU suddenly goes busy, cancel that timer. The
294 * CPU didn't go busy; we'll recheck things upon idle exit.
295 */
296 if (pending && pcpu->timer_idlecancel) {
297 del_timer(&pcpu->cpu_timer);
Mike Chanef969692010-06-22 11:26:45 -0700298 pcpu->timer_idlecancel = 0;
299 }
300 }
301
302}
303
304static void cpufreq_interactive_idle_end(void)
305{
306 struct cpufreq_interactive_cpuinfo *pcpu =
307 &per_cpu(cpuinfo, smp_processor_id());
308
Sam Leffler3ab7c2b2012-06-27 10:12:04 -0700309 if (!pcpu->governor_enabled)
310 return;
311
Todd Poynore7afb7e2012-11-05 13:09:03 -0800312 /* Arm the timer for 1-2 ticks later if not already. */
313 if (!timer_pending(&pcpu->cpu_timer)) {
Mike Chanef969692010-06-22 11:26:45 -0700314 pcpu->time_in_idle =
315 get_cpu_idle_time_us(smp_processor_id(),
316 &pcpu->idle_exit_time);
317 pcpu->timer_idlecancel = 0;
Todd Poynore7afb7e2012-11-05 13:09:03 -0800318 mod_timer_pinned(
319 &pcpu->cpu_timer,
320 jiffies + usecs_to_jiffies(timer_rate));
Mike Chanef969692010-06-22 11:26:45 -0700321 }
322
323}
324
Todd Poynor0f1920b2012-07-16 17:07:15 -0700325static int cpufreq_interactive_speedchange_task(void *data)
Mike Chanef969692010-06-22 11:26:45 -0700326{
327 unsigned int cpu;
328 cpumask_t tmp_mask;
329 unsigned long flags;
330 struct cpufreq_interactive_cpuinfo *pcpu;
331
332 while (1) {
333 set_current_state(TASK_INTERRUPTIBLE);
Todd Poynor0f1920b2012-07-16 17:07:15 -0700334 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
Mike Chanef969692010-06-22 11:26:45 -0700335
Todd Poynor0f1920b2012-07-16 17:07:15 -0700336 if (cpumask_empty(&speedchange_cpumask)) {
337 spin_unlock_irqrestore(&speedchange_cpumask_lock,
338 flags);
Mike Chanef969692010-06-22 11:26:45 -0700339 schedule();
340
341 if (kthread_should_stop())
342 break;
343
Todd Poynor0f1920b2012-07-16 17:07:15 -0700344 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
Mike Chanef969692010-06-22 11:26:45 -0700345 }
346
347 set_current_state(TASK_RUNNING);
Todd Poynor0f1920b2012-07-16 17:07:15 -0700348 tmp_mask = speedchange_cpumask;
349 cpumask_clear(&speedchange_cpumask);
350 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
Mike Chanef969692010-06-22 11:26:45 -0700351
352 for_each_cpu(cpu, &tmp_mask) {
353 unsigned int j;
354 unsigned int max_freq = 0;
355
356 pcpu = &per_cpu(cpuinfo, cpu);
357 smp_rmb();
358
359 if (!pcpu->governor_enabled)
360 continue;
361
Mike Chanef969692010-06-22 11:26:45 -0700362 for_each_cpu(j, pcpu->policy->cpus) {
363 struct cpufreq_interactive_cpuinfo *pjcpu =
364 &per_cpu(cpuinfo, j);
365
366 if (pjcpu->target_freq > max_freq)
367 max_freq = pjcpu->target_freq;
368 }
369
370 if (max_freq != pcpu->policy->cur)
371 __cpufreq_driver_target(pcpu->policy,
372 max_freq,
373 CPUFREQ_RELATION_H);
Todd Poynor0f1920b2012-07-16 17:07:15 -0700374 trace_cpufreq_interactive_setspeed(cpu,
375 pcpu->target_freq,
Todd Poynorae010472012-02-16 16:27:59 -0800376 pcpu->policy->cur);
Mike Chanef969692010-06-22 11:26:45 -0700377 }
378 }
379
380 return 0;
381}
382
Todd Poynorab8dc402012-04-02 17:17:14 -0700383static void cpufreq_interactive_boost(void)
384{
385 int i;
386 int anyboost = 0;
387 unsigned long flags;
388 struct cpufreq_interactive_cpuinfo *pcpu;
389
Todd Poynor0f1920b2012-07-16 17:07:15 -0700390 spin_lock_irqsave(&speedchange_cpumask_lock, flags);
Todd Poynorab8dc402012-04-02 17:17:14 -0700391
392 for_each_online_cpu(i) {
393 pcpu = &per_cpu(cpuinfo, i);
394
395 if (pcpu->target_freq < hispeed_freq) {
396 pcpu->target_freq = hispeed_freq;
Todd Poynor0f1920b2012-07-16 17:07:15 -0700397 cpumask_set_cpu(i, &speedchange_cpumask);
Todd Poynorab8dc402012-04-02 17:17:14 -0700398 pcpu->target_set_time_in_idle =
399 get_cpu_idle_time_us(i, &pcpu->target_set_time);
Todd Poynor1a0389a2012-05-10 23:28:06 -0700400 pcpu->hispeed_validate_time = pcpu->target_set_time;
Todd Poynorab8dc402012-04-02 17:17:14 -0700401 anyboost = 1;
402 }
403
404 /*
Todd Poynor6d15fa32012-04-26 21:41:40 -0700405 * Set floor freq and (re)start timer for when last
406 * validated.
Todd Poynorab8dc402012-04-02 17:17:14 -0700407 */
408
Todd Poynor6d15fa32012-04-26 21:41:40 -0700409 pcpu->floor_freq = hispeed_freq;
410 pcpu->floor_validate_time = ktime_to_us(ktime_get());
Todd Poynorab8dc402012-04-02 17:17:14 -0700411 }
412
Todd Poynor0f1920b2012-07-16 17:07:15 -0700413 spin_unlock_irqrestore(&speedchange_cpumask_lock, flags);
Todd Poynorab8dc402012-04-02 17:17:14 -0700414
415 if (anyboost)
Todd Poynor0f1920b2012-07-16 17:07:15 -0700416 wake_up_process(speedchange_task);
Todd Poynorab8dc402012-04-02 17:17:14 -0700417}
418
Mike Chanef969692010-06-22 11:26:45 -0700419static ssize_t show_hispeed_freq(struct kobject *kobj,
420 struct attribute *attr, char *buf)
421{
Todd Poynor3b7b5f82012-10-03 00:39:56 -0700422 return sprintf(buf, "%u\n", hispeed_freq);
Mike Chanef969692010-06-22 11:26:45 -0700423}
424
425static ssize_t store_hispeed_freq(struct kobject *kobj,
426 struct attribute *attr, const char *buf,
427 size_t count)
428{
429 int ret;
Todd Poynor3b7b5f82012-10-03 00:39:56 -0700430 long unsigned int val;
Mike Chanef969692010-06-22 11:26:45 -0700431
Todd Poynor3b7b5f82012-10-03 00:39:56 -0700432 ret = strict_strtoul(buf, 0, &val);
Mike Chanef969692010-06-22 11:26:45 -0700433 if (ret < 0)
434 return ret;
435 hispeed_freq = val;
436 return count;
437}
438
439static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644,
440 show_hispeed_freq, store_hispeed_freq);
441
442
443static ssize_t show_go_hispeed_load(struct kobject *kobj,
444 struct attribute *attr, char *buf)
445{
446 return sprintf(buf, "%lu\n", go_hispeed_load);
447}
448
449static ssize_t store_go_hispeed_load(struct kobject *kobj,
450 struct attribute *attr, const char *buf, size_t count)
451{
452 int ret;
453 unsigned long val;
454
455 ret = strict_strtoul(buf, 0, &val);
456 if (ret < 0)
457 return ret;
458 go_hispeed_load = val;
459 return count;
460}
461
462static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644,
463 show_go_hispeed_load, store_go_hispeed_load);
464
465static ssize_t show_min_sample_time(struct kobject *kobj,
466 struct attribute *attr, char *buf)
467{
468 return sprintf(buf, "%lu\n", min_sample_time);
469}
470
471static ssize_t store_min_sample_time(struct kobject *kobj,
472 struct attribute *attr, const char *buf, size_t count)
473{
474 int ret;
475 unsigned long val;
476
477 ret = strict_strtoul(buf, 0, &val);
478 if (ret < 0)
479 return ret;
480 min_sample_time = val;
481 return count;
482}
483
484static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
485 show_min_sample_time, store_min_sample_time);
486
Todd Poynorcbbe17d2012-04-13 20:18:02 -0700487static ssize_t show_above_hispeed_delay(struct kobject *kobj,
488 struct attribute *attr, char *buf)
489{
490 return sprintf(buf, "%lu\n", above_hispeed_delay_val);
491}
492
493static ssize_t store_above_hispeed_delay(struct kobject *kobj,
494 struct attribute *attr,
495 const char *buf, size_t count)
496{
497 int ret;
498 unsigned long val;
499
500 ret = strict_strtoul(buf, 0, &val);
501 if (ret < 0)
502 return ret;
503 above_hispeed_delay_val = val;
504 return count;
505}
506
507define_one_global_rw(above_hispeed_delay);
508
Mike Chanef969692010-06-22 11:26:45 -0700509static ssize_t show_timer_rate(struct kobject *kobj,
510 struct attribute *attr, char *buf)
511{
512 return sprintf(buf, "%lu\n", timer_rate);
513}
514
515static ssize_t store_timer_rate(struct kobject *kobj,
516 struct attribute *attr, const char *buf, size_t count)
517{
518 int ret;
519 unsigned long val;
520
521 ret = strict_strtoul(buf, 0, &val);
522 if (ret < 0)
523 return ret;
524 timer_rate = val;
525 return count;
526}
527
528static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644,
529 show_timer_rate, store_timer_rate);
530
Todd Poynor15a9ea02012-04-23 20:42:41 -0700531static ssize_t show_boost(struct kobject *kobj, struct attribute *attr,
532 char *buf)
533{
534 return sprintf(buf, "%d\n", boost_val);
535}
536
537static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
538 const char *buf, size_t count)
539{
540 int ret;
541 unsigned long val;
542
543 ret = kstrtoul(buf, 0, &val);
544 if (ret < 0)
545 return ret;
546
547 boost_val = val;
548
Todd Poynor442a3122012-05-03 00:16:55 -0700549 if (boost_val) {
550 trace_cpufreq_interactive_boost("on");
Todd Poynor15a9ea02012-04-23 20:42:41 -0700551 cpufreq_interactive_boost();
Todd Poynor442a3122012-05-03 00:16:55 -0700552 } else {
553 trace_cpufreq_interactive_unboost("off");
554 }
Todd Poynor15a9ea02012-04-23 20:42:41 -0700555
556 return count;
557}
558
559define_one_global_rw(boost);
560
Todd Poynor442a3122012-05-03 00:16:55 -0700561static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr,
562 const char *buf, size_t count)
563{
564 int ret;
565 unsigned long val;
566
567 ret = kstrtoul(buf, 0, &val);
568 if (ret < 0)
569 return ret;
570
571 trace_cpufreq_interactive_boost("pulse");
572 cpufreq_interactive_boost();
573 return count;
574}
575
576static struct global_attr boostpulse =
577 __ATTR(boostpulse, 0200, NULL, store_boostpulse);
578
Mike Chanef969692010-06-22 11:26:45 -0700579static struct attribute *interactive_attributes[] = {
580 &hispeed_freq_attr.attr,
581 &go_hispeed_load_attr.attr,
Todd Poynorcbbe17d2012-04-13 20:18:02 -0700582 &above_hispeed_delay.attr,
Mike Chanef969692010-06-22 11:26:45 -0700583 &min_sample_time_attr.attr,
584 &timer_rate_attr.attr,
Todd Poynor15a9ea02012-04-23 20:42:41 -0700585 &boost.attr,
Todd Poynor442a3122012-05-03 00:16:55 -0700586 &boostpulse.attr,
Mike Chanef969692010-06-22 11:26:45 -0700587 NULL,
588};
589
590static struct attribute_group interactive_attr_group = {
591 .attrs = interactive_attributes,
592 .name = "interactive",
593};
594
Sam Leffler3ab7c2b2012-06-27 10:12:04 -0700595static int cpufreq_interactive_idle_notifier(struct notifier_block *nb,
596 unsigned long val,
597 void *data)
598{
599 switch (val) {
600 case IDLE_START:
601 cpufreq_interactive_idle_start();
602 break;
603 case IDLE_END:
604 cpufreq_interactive_idle_end();
605 break;
606 }
607
608 return 0;
609}
610
611static struct notifier_block cpufreq_interactive_idle_nb = {
612 .notifier_call = cpufreq_interactive_idle_notifier,
613};
614
Mike Chanef969692010-06-22 11:26:45 -0700615static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
616 unsigned int event)
617{
618 int rc;
619 unsigned int j;
620 struct cpufreq_interactive_cpuinfo *pcpu;
621 struct cpufreq_frequency_table *freq_table;
622
623 switch (event) {
624 case CPUFREQ_GOV_START:
625 if (!cpu_online(policy->cpu))
626 return -EINVAL;
627
628 freq_table =
629 cpufreq_frequency_get_table(policy->cpu);
Todd Poynore7afb7e2012-11-05 13:09:03 -0800630 if (!hispeed_freq)
631 hispeed_freq = policy->max;
Mike Chanef969692010-06-22 11:26:45 -0700632
633 for_each_cpu(j, policy->cpus) {
634 pcpu = &per_cpu(cpuinfo, j);
635 pcpu->policy = policy;
636 pcpu->target_freq = policy->cur;
637 pcpu->freq_table = freq_table;
Todd Poynor1f408dc2012-04-06 19:59:36 -0700638 pcpu->target_set_time_in_idle =
Mike Chanef969692010-06-22 11:26:45 -0700639 get_cpu_idle_time_us(j,
Todd Poynor1f408dc2012-04-06 19:59:36 -0700640 &pcpu->target_set_time);
Todd Poynor6d15fa32012-04-26 21:41:40 -0700641 pcpu->floor_freq = pcpu->target_freq;
642 pcpu->floor_validate_time =
Todd Poynor8a833f12012-04-20 13:18:32 -0700643 pcpu->target_set_time;
Todd Poynor1a0389a2012-05-10 23:28:06 -0700644 pcpu->hispeed_validate_time =
645 pcpu->target_set_time;
Mike Chanef969692010-06-22 11:26:45 -0700646 pcpu->governor_enabled = 1;
647 smp_wmb();
Todd Poynore7afb7e2012-11-05 13:09:03 -0800648 pcpu->cpu_timer.expires =
649 jiffies + usecs_to_jiffies(timer_rate);
650 add_timer_on(&pcpu->cpu_timer, j);
Mike Chanef969692010-06-22 11:26:45 -0700651 }
652
Mike Chanef969692010-06-22 11:26:45 -0700653 /*
654 * Do not register the idle hook and create sysfs
655 * entries if we have already done so.
656 */
657 if (atomic_inc_return(&active_count) > 1)
658 return 0;
659
660 rc = sysfs_create_group(cpufreq_global_kobject,
661 &interactive_attr_group);
662 if (rc)
663 return rc;
664
Sam Leffler3ab7c2b2012-06-27 10:12:04 -0700665 idle_notifier_register(&cpufreq_interactive_idle_nb);
Mike Chanef969692010-06-22 11:26:45 -0700666 break;
667
668 case CPUFREQ_GOV_STOP:
669 for_each_cpu(j, policy->cpus) {
670 pcpu = &per_cpu(cpuinfo, j);
671 pcpu->governor_enabled = 0;
672 smp_wmb();
673 del_timer_sync(&pcpu->cpu_timer);
Mike Chanef969692010-06-22 11:26:45 -0700674 }
675
Mike Chanef969692010-06-22 11:26:45 -0700676 if (atomic_dec_return(&active_count) > 0)
677 return 0;
678
Sam Leffler3ab7c2b2012-06-27 10:12:04 -0700679 idle_notifier_unregister(&cpufreq_interactive_idle_nb);
Mike Chanef969692010-06-22 11:26:45 -0700680 sysfs_remove_group(cpufreq_global_kobject,
681 &interactive_attr_group);
682
683 break;
684
685 case CPUFREQ_GOV_LIMITS:
686 if (policy->max < policy->cur)
687 __cpufreq_driver_target(policy,
688 policy->max, CPUFREQ_RELATION_H);
689 else if (policy->min > policy->cur)
690 __cpufreq_driver_target(policy,
691 policy->min, CPUFREQ_RELATION_L);
692 break;
693 }
694 return 0;
695}
696
Mike Chanef969692010-06-22 11:26:45 -0700697static int __init cpufreq_interactive_init(void)
698{
699 unsigned int i;
700 struct cpufreq_interactive_cpuinfo *pcpu;
701 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
702
703 go_hispeed_load = DEFAULT_GO_HISPEED_LOAD;
704 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
Todd Poynorcbbe17d2012-04-13 20:18:02 -0700705 above_hispeed_delay_val = DEFAULT_ABOVE_HISPEED_DELAY;
Mike Chanef969692010-06-22 11:26:45 -0700706 timer_rate = DEFAULT_TIMER_RATE;
707
708 /* Initalize per-cpu timers */
709 for_each_possible_cpu(i) {
710 pcpu = &per_cpu(cpuinfo, i);
711 init_timer(&pcpu->cpu_timer);
712 pcpu->cpu_timer.function = cpufreq_interactive_timer;
713 pcpu->cpu_timer.data = i;
714 }
715
Todd Poynor0f1920b2012-07-16 17:07:15 -0700716 spin_lock_init(&speedchange_cpumask_lock);
717 speedchange_task =
718 kthread_create(cpufreq_interactive_speedchange_task, NULL,
719 "cfinteractive");
720 if (IS_ERR(speedchange_task))
721 return PTR_ERR(speedchange_task);
Sam Leffler5c9b8272012-06-27 12:55:56 -0700722
Todd Poynor0f1920b2012-07-16 17:07:15 -0700723 sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, &param);
724 get_task_struct(speedchange_task);
Mike Chanef969692010-06-22 11:26:45 -0700725
Sam Leffler5c9b8272012-06-27 12:55:56 -0700726 /* NB: wake up so the thread does not look hung to the freezer */
Todd Poynor0f1920b2012-07-16 17:07:15 -0700727 wake_up_process(speedchange_task);
Sam Leffler5c9b8272012-06-27 12:55:56 -0700728
Mike Chanef969692010-06-22 11:26:45 -0700729 return cpufreq_register_governor(&cpufreq_gov_interactive);
Mike Chanef969692010-06-22 11:26:45 -0700730}
731
732#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
733fs_initcall(cpufreq_interactive_init);
734#else
735module_init(cpufreq_interactive_init);
736#endif
737
738static void __exit cpufreq_interactive_exit(void)
739{
740 cpufreq_unregister_governor(&cpufreq_gov_interactive);
Todd Poynor0f1920b2012-07-16 17:07:15 -0700741 kthread_stop(speedchange_task);
742 put_task_struct(speedchange_task);
Mike Chanef969692010-06-22 11:26:45 -0700743}
744
745module_exit(cpufreq_interactive_exit);
746
747MODULE_AUTHOR("Mike Chan <mike@android.com>");
748MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
749 "Latency sensitive workloads");
750MODULE_LICENSE("GPL");