blob: 26a423bb3da0ab13dab0538a78413dc4491bde72 [file] [log] [blame]
Mike Chan1dab2592010-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/mutex.h>
23#include <linux/sched.h>
24#include <linux/tick.h>
25#include <linux/timer.h>
26#include <linux/workqueue.h>
27#include <linux/kthread.h>
28
29#include <asm/cputime.h>
30
31static void (*pm_idle_old)(void);
32static atomic_t active_count = ATOMIC_INIT(0);
33
34struct cpufreq_interactive_cpuinfo {
35 struct timer_list cpu_timer;
36 int timer_idlecancel;
37 u64 time_in_idle;
38 u64 idle_exit_time;
39 u64 timer_run_time;
40 int idling;
41 u64 freq_change_time;
42 u64 freq_change_time_in_idle;
43 struct cpufreq_policy *policy;
44 struct cpufreq_frequency_table *freq_table;
45 unsigned int target_freq;
46 int governor_enabled;
47};
48
49static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo);
50
51/* Workqueues handle frequency scaling */
52static struct task_struct *up_task;
53static struct workqueue_struct *down_wq;
54static struct work_struct freq_scale_down_work;
55static cpumask_t up_cpumask;
56static spinlock_t up_cpumask_lock;
57static cpumask_t down_cpumask;
58static spinlock_t down_cpumask_lock;
59
60/* Go to max speed when CPU load at or above this value. */
61#define DEFAULT_GO_MAXSPEED_LOAD 85
62static unsigned long go_maxspeed_load;
63
64/*
65 * The minimum amount of time to spend at a frequency before we can ramp down.
66 */
67#define DEFAULT_MIN_SAMPLE_TIME 80000;
68static unsigned long min_sample_time;
69
Mike Chan1dab2592010-06-22 11:26:45 -070070static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
71 unsigned int event);
72
73#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
74static
75#endif
76struct cpufreq_governor cpufreq_gov_interactive = {
77 .name = "interactive",
78 .governor = cpufreq_governor_interactive,
79 .max_transition_latency = 10000000,
80 .owner = THIS_MODULE,
81};
82
83static void cpufreq_interactive_timer(unsigned long data)
84{
85 unsigned int delta_idle;
86 unsigned int delta_time;
87 int cpu_load;
88 int load_since_change;
89 u64 time_in_idle;
90 u64 idle_exit_time;
91 struct cpufreq_interactive_cpuinfo *pcpu =
92 &per_cpu(cpuinfo, data);
93 u64 now_idle;
94 unsigned int new_freq;
95 unsigned int index;
Todd Poynor1c4f95f2010-12-03 11:20:09 -080096 unsigned long flags;
Mike Chan1dab2592010-06-22 11:26:45 -070097
Todd Poynor9b6f4a32010-12-23 17:33:07 -080098 smp_rmb();
99
100 if (!pcpu->governor_enabled)
101 goto exit;
102
Mike Chan1dab2592010-06-22 11:26:45 -0700103 /*
104 * Once pcpu->timer_run_time is updated to >= pcpu->idle_exit_time,
105 * this lets idle exit know the current idle time sample has
106 * been processed, and idle exit can generate a new sample and
107 * re-arm the timer. This prevents a concurrent idle
108 * exit on that CPU from writing a new set of info at the same time
109 * the timer function runs (the timer function can't use that info
110 * until more time passes).
111 */
112 time_in_idle = pcpu->time_in_idle;
113 idle_exit_time = pcpu->idle_exit_time;
114 now_idle = get_cpu_idle_time_us(data, &pcpu->timer_run_time);
115 smp_wmb();
116
117 /* If we raced with cancelling a timer, skip. */
Allen Martin13b2b142011-06-28 10:40:30 -0700118 if (!idle_exit_time)
Mike Chan1dab2592010-06-22 11:26:45 -0700119 goto exit;
Mike Chan1dab2592010-06-22 11:26:45 -0700120
121 delta_idle = (unsigned int) cputime64_sub(now_idle, time_in_idle);
122 delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time,
123 idle_exit_time);
124
125 /*
126 * If timer ran less than 1ms after short-term sample started, retry.
127 */
Allen Martin13b2b142011-06-28 10:40:30 -0700128 if (delta_time < 1000)
Mike Chan1dab2592010-06-22 11:26:45 -0700129 goto rearm;
Mike Chan1dab2592010-06-22 11:26:45 -0700130
131 if (delta_idle > delta_time)
132 cpu_load = 0;
133 else
134 cpu_load = 100 * (delta_time - delta_idle) / delta_time;
135
136 delta_idle = (unsigned int) cputime64_sub(now_idle,
137 pcpu->freq_change_time_in_idle);
138 delta_time = (unsigned int) cputime64_sub(pcpu->timer_run_time,
139 pcpu->freq_change_time);
140
141 if (delta_idle > delta_time)
142 load_since_change = 0;
143 else
144 load_since_change =
145 100 * (delta_time - delta_idle) / delta_time;
146
147 /*
148 * Choose greater of short-term load (since last idle timer
149 * started or timer function re-armed itself) or long-term load
150 * (since last frequency change).
151 */
152 if (load_since_change > cpu_load)
153 cpu_load = load_since_change;
154
155 if (cpu_load >= go_maxspeed_load)
156 new_freq = pcpu->policy->max;
157 else
158 new_freq = pcpu->policy->max * cpu_load / 100;
159
160 if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table,
161 new_freq, CPUFREQ_RELATION_H,
162 &index)) {
Allen Martin13b2b142011-06-28 10:40:30 -0700163 pr_warn_once("timer %d: cpufreq_frequency_table_target error\n",
164 (int) data);
Mike Chan1dab2592010-06-22 11:26:45 -0700165 goto rearm;
166 }
167
168 new_freq = pcpu->freq_table[index].frequency;
169
170 if (pcpu->target_freq == new_freq)
Mike Chan1dab2592010-06-22 11:26:45 -0700171 goto rearm_if_notmax;
Mike Chan1dab2592010-06-22 11:26:45 -0700172
173 /*
174 * Do not scale down unless we have been at this frequency for the
175 * minimum sample time.
176 */
177 if (new_freq < pcpu->target_freq) {
178 if (cputime64_sub(pcpu->timer_run_time, pcpu->freq_change_time) <
Allen Martin13b2b142011-06-28 10:40:30 -0700179 min_sample_time)
Mike Chan1dab2592010-06-22 11:26:45 -0700180 goto rearm;
Mike Chan1dab2592010-06-22 11:26:45 -0700181 }
182
Mike Chan1dab2592010-06-22 11:26:45 -0700183 if (new_freq < pcpu->target_freq) {
184 pcpu->target_freq = new_freq;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800185 spin_lock_irqsave(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700186 cpumask_set_cpu(data, &down_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800187 spin_unlock_irqrestore(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700188 queue_work(down_wq, &freq_scale_down_work);
189 } else {
190 pcpu->target_freq = new_freq;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800191 spin_lock_irqsave(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700192 cpumask_set_cpu(data, &up_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800193 spin_unlock_irqrestore(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700194 wake_up_process(up_task);
195 }
196
197rearm_if_notmax:
198 /*
199 * Already set max speed and don't see a need to change that,
200 * wait until next idle to re-evaluate, don't need timer.
201 */
202 if (pcpu->target_freq == pcpu->policy->max)
203 goto exit;
204
205rearm:
206 if (!timer_pending(&pcpu->cpu_timer)) {
207 /*
208 * If already at min: if that CPU is idle, don't set timer.
209 * Else cancel the timer if that CPU goes idle. We don't
210 * need to re-evaluate speed until the next idle exit.
211 */
212 if (pcpu->target_freq == pcpu->policy->min) {
213 smp_rmb();
214
Allen Martin13b2b142011-06-28 10:40:30 -0700215 if (pcpu->idling)
Mike Chan1dab2592010-06-22 11:26:45 -0700216 goto exit;
Mike Chan1dab2592010-06-22 11:26:45 -0700217
218 pcpu->timer_idlecancel = 1;
219 }
220
221 pcpu->time_in_idle = get_cpu_idle_time_us(
222 data, &pcpu->idle_exit_time);
223 mod_timer(&pcpu->cpu_timer, jiffies + 2);
Mike Chan1dab2592010-06-22 11:26:45 -0700224 }
225
226exit:
227 return;
228}
229
230static void cpufreq_interactive_idle(void)
231{
232 struct cpufreq_interactive_cpuinfo *pcpu =
233 &per_cpu(cpuinfo, smp_processor_id());
234 int pending;
235
236 if (!pcpu->governor_enabled) {
237 pm_idle_old();
238 return;
239 }
240
241 pcpu->idling = 1;
242 smp_wmb();
243 pending = timer_pending(&pcpu->cpu_timer);
244
245 if (pcpu->target_freq != pcpu->policy->min) {
246#ifdef CONFIG_SMP
247 /*
248 * Entering idle while not at lowest speed. On some
249 * platforms this can hold the other CPU(s) at that speed
250 * even though the CPU is idle. Set a timer to re-evaluate
251 * speed so this idle CPU doesn't hold the other CPUs above
252 * min indefinitely. This should probably be a quirk of
253 * the CPUFreq driver.
254 */
255 if (!pending) {
256 pcpu->time_in_idle = get_cpu_idle_time_us(
257 smp_processor_id(), &pcpu->idle_exit_time);
258 pcpu->timer_idlecancel = 0;
259 mod_timer(&pcpu->cpu_timer, jiffies + 2);
Mike Chan1dab2592010-06-22 11:26:45 -0700260 }
261#endif
262 } else {
263 /*
264 * If at min speed and entering idle after load has
265 * already been evaluated, and a timer has been set just in
266 * case the CPU suddenly goes busy, cancel that timer. The
267 * CPU didn't go busy; we'll recheck things upon idle exit.
268 */
269 if (pending && pcpu->timer_idlecancel) {
Mike Chan1dab2592010-06-22 11:26:45 -0700270 del_timer(&pcpu->cpu_timer);
271 /*
272 * Ensure last timer run time is after current idle
273 * sample start time, so next idle exit will always
274 * start a new idle sampling period.
275 */
276 pcpu->idle_exit_time = 0;
277 pcpu->timer_idlecancel = 0;
278 }
279 }
280
281 pm_idle_old();
282 pcpu->idling = 0;
283 smp_wmb();
284
285 /*
286 * Arm the timer for 1-2 ticks later if not already, and if the timer
287 * function has already processed the previous load sampling
288 * interval. (If the timer is not pending but has not processed
289 * the previous interval, it is probably racing with us on another
290 * CPU. Let it compute load based on the previous sample and then
291 * re-arm the timer for another interval when it's done, rather
292 * than updating the interval start time to be "now", which doesn't
293 * give the timer function enough time to make a decision on this
294 * run.)
295 */
296 if (timer_pending(&pcpu->cpu_timer) == 0 &&
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800297 pcpu->timer_run_time >= pcpu->idle_exit_time &&
298 pcpu->governor_enabled) {
Mike Chan1dab2592010-06-22 11:26:45 -0700299 pcpu->time_in_idle =
300 get_cpu_idle_time_us(smp_processor_id(),
301 &pcpu->idle_exit_time);
302 pcpu->timer_idlecancel = 0;
303 mod_timer(&pcpu->cpu_timer, jiffies + 2);
Mike Chan1dab2592010-06-22 11:26:45 -0700304 }
305
306}
307
308static int cpufreq_interactive_up_task(void *data)
309{
310 unsigned int cpu;
311 cpumask_t tmp_mask;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800312 unsigned long flags;
Mike Chan1dab2592010-06-22 11:26:45 -0700313 struct cpufreq_interactive_cpuinfo *pcpu;
314
Mike Chan1dab2592010-06-22 11:26:45 -0700315 while (1) {
316 set_current_state(TASK_INTERRUPTIBLE);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800317 spin_lock_irqsave(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700318
319 if (cpumask_empty(&up_cpumask)) {
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800320 spin_unlock_irqrestore(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700321 schedule();
322
323 if (kthread_should_stop())
324 break;
325
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800326 spin_lock_irqsave(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700327 }
328
329 set_current_state(TASK_RUNNING);
330
Mike Chan1dab2592010-06-22 11:26:45 -0700331 tmp_mask = up_cpumask;
332 cpumask_clear(&up_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800333 spin_unlock_irqrestore(&up_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700334
335 for_each_cpu(cpu, &tmp_mask) {
336 pcpu = &per_cpu(cpuinfo, cpu);
337
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800338 smp_rmb();
339
340 if (!pcpu->governor_enabled)
341 continue;
342
Mike Chan1dab2592010-06-22 11:26:45 -0700343 __cpufreq_driver_target(pcpu->policy,
344 pcpu->target_freq,
345 CPUFREQ_RELATION_H);
346 pcpu->freq_change_time_in_idle =
347 get_cpu_idle_time_us(cpu,
348 &pcpu->freq_change_time);
Mike Chan1dab2592010-06-22 11:26:45 -0700349 }
350 }
351
352 return 0;
353}
354
355static void cpufreq_interactive_freq_down(struct work_struct *work)
356{
357 unsigned int cpu;
358 cpumask_t tmp_mask;
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800359 unsigned long flags;
Mike Chan1dab2592010-06-22 11:26:45 -0700360 struct cpufreq_interactive_cpuinfo *pcpu;
361
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800362 spin_lock_irqsave(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700363 tmp_mask = down_cpumask;
364 cpumask_clear(&down_cpumask);
Todd Poynor1c4f95f2010-12-03 11:20:09 -0800365 spin_unlock_irqrestore(&down_cpumask_lock, flags);
Mike Chan1dab2592010-06-22 11:26:45 -0700366
367 for_each_cpu(cpu, &tmp_mask) {
368 pcpu = &per_cpu(cpuinfo, cpu);
Todd Poynor9b6f4a32010-12-23 17:33:07 -0800369
370 smp_rmb();
371
372 if (!pcpu->governor_enabled)
373 continue;
374
Mike Chan1dab2592010-06-22 11:26:45 -0700375 __cpufreq_driver_target(pcpu->policy,
376 pcpu->target_freq,
377 CPUFREQ_RELATION_H);
378 pcpu->freq_change_time_in_idle =
379 get_cpu_idle_time_us(cpu,
380 &pcpu->freq_change_time);
Mike Chan1dab2592010-06-22 11:26:45 -0700381 }
382}
383
384static ssize_t show_go_maxspeed_load(struct kobject *kobj,
385 struct attribute *attr, char *buf)
386{
387 return sprintf(buf, "%lu\n", go_maxspeed_load);
388}
389
390static ssize_t store_go_maxspeed_load(struct kobject *kobj,
391 struct attribute *attr, const char *buf, size_t count)
392{
393 return strict_strtoul(buf, 0, &go_maxspeed_load);
394}
395
396static struct global_attr go_maxspeed_load_attr = __ATTR(go_maxspeed_load, 0644,
397 show_go_maxspeed_load, store_go_maxspeed_load);
398
399static ssize_t show_min_sample_time(struct kobject *kobj,
400 struct attribute *attr, char *buf)
401{
402 return sprintf(buf, "%lu\n", min_sample_time);
403}
404
405static ssize_t store_min_sample_time(struct kobject *kobj,
406 struct attribute *attr, const char *buf, size_t count)
407{
408 return strict_strtoul(buf, 0, &min_sample_time);
409}
410
411static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644,
412 show_min_sample_time, store_min_sample_time);
413
414static struct attribute *interactive_attributes[] = {
415 &go_maxspeed_load_attr.attr,
416 &min_sample_time_attr.attr,
417 NULL,
418};
419
420static struct attribute_group interactive_attr_group = {
421 .attrs = interactive_attributes,
422 .name = "interactive",
423};
424
Todd Poynor097d3962011-06-06 18:30:23 -0700425static int cpufreq_governor_interactive(struct cpufreq_policy *policy,
Mike Chan1dab2592010-06-22 11:26:45 -0700426 unsigned int event)
427{
428 int rc;
Todd Poynor097d3962011-06-06 18:30:23 -0700429 unsigned int j;
430 struct cpufreq_interactive_cpuinfo *pcpu;
431 struct cpufreq_frequency_table *freq_table;
Mike Chan1dab2592010-06-22 11:26:45 -0700432
433 switch (event) {
434 case CPUFREQ_GOV_START:
Todd Poynor097d3962011-06-06 18:30:23 -0700435 if (!cpu_online(policy->cpu))
Mike Chan1dab2592010-06-22 11:26:45 -0700436 return -EINVAL;
437
Todd Poynor097d3962011-06-06 18:30:23 -0700438 freq_table =
439 cpufreq_frequency_get_table(policy->cpu);
440
441 for_each_cpu(j, policy->cpus) {
442 pcpu = &per_cpu(cpuinfo, j);
443 pcpu->policy = policy;
444 pcpu->target_freq = policy->cur;
445 pcpu->freq_table = freq_table;
446 pcpu->freq_change_time_in_idle =
447 get_cpu_idle_time_us(j,
Mike Chan1dab2592010-06-22 11:26:45 -0700448 &pcpu->freq_change_time);
Todd Poynor097d3962011-06-06 18:30:23 -0700449 pcpu->governor_enabled = 1;
450 smp_wmb();
451 }
452
Mike Chan1dab2592010-06-22 11:26:45 -0700453 /*
454 * Do not register the idle hook and create sysfs
455 * entries if we have already done so.
456 */
457 if (atomic_inc_return(&active_count) > 1)
458 return 0;
459
460 rc = sysfs_create_group(cpufreq_global_kobject,
461 &interactive_attr_group);
462 if (rc)
463 return rc;
464
465 pm_idle_old = pm_idle;
466 pm_idle = cpufreq_interactive_idle;
467 break;
468
469 case CPUFREQ_GOV_STOP:
Todd Poynor097d3962011-06-06 18:30:23 -0700470 for_each_cpu(j, policy->cpus) {
471 pcpu = &per_cpu(cpuinfo, j);
472 pcpu->governor_enabled = 0;
473 smp_wmb();
474 del_timer_sync(&pcpu->cpu_timer);
Mike Chan1dab2592010-06-22 11:26:45 -0700475
Todd Poynor097d3962011-06-06 18:30:23 -0700476 /*
477 * Reset idle exit time since we may cancel the timer
478 * before it can run after the last idle exit time,
479 * to avoid tripping the check in idle exit for a timer
480 * that is trying to run.
481 */
482 pcpu->idle_exit_time = 0;
483 }
484
485 flush_work(&freq_scale_down_work);
Mike Chan1dab2592010-06-22 11:26:45 -0700486 if (atomic_dec_return(&active_count) > 0)
487 return 0;
488
489 sysfs_remove_group(cpufreq_global_kobject,
490 &interactive_attr_group);
491
492 pm_idle = pm_idle_old;
Mike Chan1dab2592010-06-22 11:26:45 -0700493 break;
494
495 case CPUFREQ_GOV_LIMITS:
Todd Poynor097d3962011-06-06 18:30:23 -0700496 if (policy->max < policy->cur)
497 __cpufreq_driver_target(policy,
498 policy->max, CPUFREQ_RELATION_H);
499 else if (policy->min > policy->cur)
500 __cpufreq_driver_target(policy,
501 policy->min, CPUFREQ_RELATION_L);
Mike Chan1dab2592010-06-22 11:26:45 -0700502 break;
503 }
504 return 0;
505}
506
507static int __init cpufreq_interactive_init(void)
508{
509 unsigned int i;
510 struct cpufreq_interactive_cpuinfo *pcpu;
511 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
512
513 go_maxspeed_load = DEFAULT_GO_MAXSPEED_LOAD;
514 min_sample_time = DEFAULT_MIN_SAMPLE_TIME;
515
516 /* Initalize per-cpu timers */
517 for_each_possible_cpu(i) {
518 pcpu = &per_cpu(cpuinfo, i);
519 init_timer(&pcpu->cpu_timer);
520 pcpu->cpu_timer.function = cpufreq_interactive_timer;
521 pcpu->cpu_timer.data = i;
522 }
523
524 up_task = kthread_create(cpufreq_interactive_up_task, NULL,
525 "kinteractiveup");
526 if (IS_ERR(up_task))
527 return PTR_ERR(up_task);
528
529 sched_setscheduler_nocheck(up_task, SCHED_FIFO, &param);
530 get_task_struct(up_task);
531
532 /* No rescuer thread, bind to CPU queuing the work for possibly
533 warm cache (probably doesn't matter much). */
534 down_wq = alloc_workqueue("knteractive_down", 0, 1);
535
536 if (! down_wq)
537 goto err_freeuptask;
538
539 INIT_WORK(&freq_scale_down_work,
540 cpufreq_interactive_freq_down);
541
542 spin_lock_init(&up_cpumask_lock);
543 spin_lock_init(&down_cpumask_lock);
544
Mike Chan1dab2592010-06-22 11:26:45 -0700545 return cpufreq_register_governor(&cpufreq_gov_interactive);
546
547err_freeuptask:
548 put_task_struct(up_task);
549 return -ENOMEM;
550}
551
552#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE
553fs_initcall(cpufreq_interactive_init);
554#else
555module_init(cpufreq_interactive_init);
556#endif
557
558static void __exit cpufreq_interactive_exit(void)
559{
560 cpufreq_unregister_governor(&cpufreq_gov_interactive);
561 kthread_stop(up_task);
562 put_task_struct(up_task);
563 destroy_workqueue(down_wq);
564}
565
566module_exit(cpufreq_interactive_exit);
567
568MODULE_AUTHOR("Mike Chan <mike@android.com>");
569MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for "
570 "Latency sensitive workloads");
571MODULE_LICENSE("GPL");