blob: 7d7244314ac9a4d55e78d9427102dbe7ab59366a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/cpufreq/cpufreq_ondemand.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/smp.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/ctype.h>
19#include <linux/cpufreq.h>
20#include <linux/sysctl.h>
21#include <linux/types.h>
22#include <linux/fs.h>
23#include <linux/sysfs.h>
24#include <linux/sched.h>
25#include <linux/kmod.h>
26#include <linux/workqueue.h>
27#include <linux/jiffies.h>
28#include <linux/kernel_stat.h>
29#include <linux/percpu.h>
30
31/*
32 * dbs is used in this file as a shortform for demandbased switching
33 * It helps to keep variable names smaller, simpler
34 */
35
36#define DEF_FREQUENCY_UP_THRESHOLD (80)
37#define MIN_FREQUENCY_UP_THRESHOLD (0)
38#define MAX_FREQUENCY_UP_THRESHOLD (100)
39
40#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
41#define MIN_FREQUENCY_DOWN_THRESHOLD (0)
42#define MAX_FREQUENCY_DOWN_THRESHOLD (100)
43
44/*
45 * The polling frequency of this governor depends on the capability of
46 * the processor. Default polling frequency is 1000 times the transition
47 * latency of the processor. The governor will work on any processor with
48 * transition latency <= 10mS, using appropriate sampling
49 * rate.
50 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
51 * this governor will not work.
52 * All times here are in uS.
53 */
54static unsigned int def_sampling_rate;
55#define MIN_SAMPLING_RATE (def_sampling_rate / 2)
56#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
57#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
58#define DEF_SAMPLING_DOWN_FACTOR (10)
59#define TRANSITION_LATENCY_LIMIT (10 * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61static void do_dbs_timer(void *data);
62
63struct cpu_dbs_info_s {
64 struct cpufreq_policy *cur_policy;
65 unsigned int prev_cpu_idle_up;
66 unsigned int prev_cpu_idle_down;
67 unsigned int enable;
68};
69static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
70
71static unsigned int dbs_enable; /* number of CPUs using this policy */
72
73static DECLARE_MUTEX (dbs_sem);
74static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
75
76struct dbs_tuners {
77 unsigned int sampling_rate;
78 unsigned int sampling_down_factor;
79 unsigned int up_threshold;
80 unsigned int down_threshold;
Dave Jones3d5ee9e2005-05-31 19:03:47 -070081 unsigned int ignore_nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
84static struct dbs_tuners dbs_tuners_ins = {
85 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
86 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
87 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
88};
89
90/************************** sysfs interface ************************/
91static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
92{
93 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
94}
95
96static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
97{
98 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
99}
100
101#define define_one_ro(_name) \
102static struct freq_attr _name = \
103__ATTR(_name, 0444, show_##_name, NULL)
104
105define_one_ro(sampling_rate_max);
106define_one_ro(sampling_rate_min);
107
108/* cpufreq_ondemand Governor Tunables */
109#define show_one(file_name, object) \
110static ssize_t show_##file_name \
111(struct cpufreq_policy *unused, char *buf) \
112{ \
113 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
114}
115show_one(sampling_rate, sampling_rate);
116show_one(sampling_down_factor, sampling_down_factor);
117show_one(up_threshold, up_threshold);
118show_one(down_threshold, down_threshold);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700119show_one(ignore_nice, ignore_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
122 const char *buf, size_t count)
123{
124 unsigned int input;
125 int ret;
126 ret = sscanf (buf, "%u", &input);
127 if (ret != 1 )
128 return -EINVAL;
129
130 down(&dbs_sem);
131 dbs_tuners_ins.sampling_down_factor = input;
132 up(&dbs_sem);
133
134 return count;
135}
136
137static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
138 const char *buf, size_t count)
139{
140 unsigned int input;
141 int ret;
142 ret = sscanf (buf, "%u", &input);
143
144 down(&dbs_sem);
145 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
146 up(&dbs_sem);
147 return -EINVAL;
148 }
149
150 dbs_tuners_ins.sampling_rate = input;
151 up(&dbs_sem);
152
153 return count;
154}
155
156static ssize_t store_up_threshold(struct cpufreq_policy *unused,
157 const char *buf, size_t count)
158{
159 unsigned int input;
160 int ret;
161 ret = sscanf (buf, "%u", &input);
162
163 down(&dbs_sem);
164 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
165 input < MIN_FREQUENCY_UP_THRESHOLD ||
166 input <= dbs_tuners_ins.down_threshold) {
167 up(&dbs_sem);
168 return -EINVAL;
169 }
170
171 dbs_tuners_ins.up_threshold = input;
172 up(&dbs_sem);
173
174 return count;
175}
176
177static ssize_t store_down_threshold(struct cpufreq_policy *unused,
178 const char *buf, size_t count)
179{
180 unsigned int input;
181 int ret;
182 ret = sscanf (buf, "%u", &input);
183
184 down(&dbs_sem);
185 if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
186 input < MIN_FREQUENCY_DOWN_THRESHOLD ||
187 input >= dbs_tuners_ins.up_threshold) {
188 up(&dbs_sem);
189 return -EINVAL;
190 }
191
192 dbs_tuners_ins.down_threshold = input;
193 up(&dbs_sem);
194
195 return count;
196}
197
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700198static ssize_t store_ignore_nice(struct cpufreq_policy *policy,
199 const char *buf, size_t count)
200{
201 unsigned int input;
202 int ret;
203
204 unsigned int j;
205
206 ret = sscanf (buf, "%u", &input);
207 if ( ret != 1 )
208 return -EINVAL;
209
210 if ( input > 1 )
211 input = 1;
212
213 down(&dbs_sem);
214 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
215 up(&dbs_sem);
216 return count;
217 }
218 dbs_tuners_ins.ignore_nice = input;
219
220 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
221 for_each_cpu_mask(j, policy->cpus) {
222 struct cpu_dbs_info_s *j_dbs_info;
223 j_dbs_info = &per_cpu(cpu_dbs_info, j);
224 j_dbs_info->cur_policy = policy;
225
226 j_dbs_info->prev_cpu_idle_up =
227 kstat_cpu(j).cpustat.idle +
228 kstat_cpu(j).cpustat.iowait +
229 ( !dbs_tuners_ins.ignore_nice
230 ? kstat_cpu(j).cpustat.nice : 0 );
231 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
232 }
233 up(&dbs_sem);
234
235 return count;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#define define_one_rw(_name) \
239static struct freq_attr _name = \
240__ATTR(_name, 0644, show_##_name, store_##_name)
241
242define_one_rw(sampling_rate);
243define_one_rw(sampling_down_factor);
244define_one_rw(up_threshold);
245define_one_rw(down_threshold);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700246define_one_rw(ignore_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248static struct attribute * dbs_attributes[] = {
249 &sampling_rate_max.attr,
250 &sampling_rate_min.attr,
251 &sampling_rate.attr,
252 &sampling_down_factor.attr,
253 &up_threshold.attr,
254 &down_threshold.attr,
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700255 &ignore_nice.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 NULL
257};
258
259static struct attribute_group dbs_attr_group = {
260 .attrs = dbs_attributes,
261 .name = "ondemand",
262};
263
264/************************** sysfs end ************************/
265
266static void dbs_check_cpu(int cpu)
267{
268 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
269 unsigned int total_idle_ticks;
270 unsigned int freq_down_step;
271 unsigned int freq_down_sampling_rate;
272 static int down_skip[NR_CPUS];
273 struct cpu_dbs_info_s *this_dbs_info;
274
275 struct cpufreq_policy *policy;
276 unsigned int j;
277
278 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
279 if (!this_dbs_info->enable)
280 return;
281
282 policy = this_dbs_info->cur_policy;
283 /*
284 * The default safe range is 20% to 80%
285 * Every sampling_rate, we check
286 * - If current idle time is less than 20%, then we try to
287 * increase frequency
288 * Every sampling_rate*sampling_down_factor, we check
289 * - If current idle time is more than 80%, then we try to
290 * decrease frequency
291 *
292 * Any frequency increase takes it to the maximum frequency.
293 * Frequency reduction happens at minimum steps of
294 * 5% of max_frequency
295 */
296
297 /* Check for frequency increase */
298 total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
299 kstat_cpu(cpu).cpustat.iowait;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700300 /* consider 'nice' tasks as 'idle' time too if required */
301 if (dbs_tuners_ins.ignore_nice == 0)
302 total_idle_ticks += kstat_cpu(cpu).cpustat.nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 idle_ticks = total_idle_ticks -
304 this_dbs_info->prev_cpu_idle_up;
305 this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
306
307
308 for_each_cpu_mask(j, policy->cpus) {
309 unsigned int tmp_idle_ticks;
310 struct cpu_dbs_info_s *j_dbs_info;
311
312 if (j == cpu)
313 continue;
314
315 j_dbs_info = &per_cpu(cpu_dbs_info, j);
316 /* Check for frequency increase */
317 total_idle_ticks = kstat_cpu(j).cpustat.idle +
318 kstat_cpu(j).cpustat.iowait;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700319 /* consider 'nice' too? */
320 if (dbs_tuners_ins.ignore_nice == 0)
321 total_idle_ticks += kstat_cpu(j).cpustat.nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 tmp_idle_ticks = total_idle_ticks -
323 j_dbs_info->prev_cpu_idle_up;
324 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
325
326 if (tmp_idle_ticks < idle_ticks)
327 idle_ticks = tmp_idle_ticks;
328 }
329
330 /* Scale idle ticks by 100 and compare with up and down ticks */
331 idle_ticks *= 100;
332 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
Dave Jones6fe71162005-05-31 19:03:44 -0700333 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (idle_ticks < up_idle_ticks) {
336 __cpufreq_driver_target(policy, policy->max,
337 CPUFREQ_RELATION_H);
338 down_skip[cpu] = 0;
339 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
340 return;
341 }
342
343 /* Check for frequency decrease */
344 down_skip[cpu]++;
345 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
346 return;
347
348 total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
349 kstat_cpu(cpu).cpustat.iowait;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700350 /* consider 'nice' too? */
351 if (dbs_tuners_ins.ignore_nice == 0)
352 total_idle_ticks += kstat_cpu(cpu).cpustat.nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 idle_ticks = total_idle_ticks -
354 this_dbs_info->prev_cpu_idle_down;
355 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
356
357 for_each_cpu_mask(j, policy->cpus) {
358 unsigned int tmp_idle_ticks;
359 struct cpu_dbs_info_s *j_dbs_info;
360
361 if (j == cpu)
362 continue;
363
364 j_dbs_info = &per_cpu(cpu_dbs_info, j);
365 /* Check for frequency increase */
366 total_idle_ticks = kstat_cpu(j).cpustat.idle +
367 kstat_cpu(j).cpustat.iowait;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700368 /* consider 'nice' too? */
369 if (dbs_tuners_ins.ignore_nice == 0)
370 total_idle_ticks += kstat_cpu(j).cpustat.nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 tmp_idle_ticks = total_idle_ticks -
372 j_dbs_info->prev_cpu_idle_down;
373 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
374
375 if (tmp_idle_ticks < idle_ticks)
376 idle_ticks = tmp_idle_ticks;
377 }
378
379 /* Scale idle ticks by 100 and compare with up and down ticks */
380 idle_ticks *= 100;
381 down_skip[cpu] = 0;
382
383 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
384 dbs_tuners_ins.sampling_down_factor;
385 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
Dave Jones6fe71162005-05-31 19:03:44 -0700386 usecs_to_jiffies(freq_down_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (idle_ticks > down_idle_ticks ) {
389 freq_down_step = (5 * policy->max) / 100;
390
391 /* max freq cannot be less than 100. But who knows.... */
392 if (unlikely(freq_down_step == 0))
393 freq_down_step = 5;
394
395 __cpufreq_driver_target(policy,
396 policy->cur - freq_down_step,
397 CPUFREQ_RELATION_H);
398 return;
399 }
400}
401
402static void do_dbs_timer(void *data)
403{
404 int i;
405 down(&dbs_sem);
Dave Jones6fe71162005-05-31 19:03:44 -0700406 for_each_online_cpu(i)
407 dbs_check_cpu(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 schedule_delayed_work(&dbs_work,
Dave Jones6fe71162005-05-31 19:03:44 -0700409 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 up(&dbs_sem);
411}
412
413static inline void dbs_timer_init(void)
414{
415 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
416 schedule_delayed_work(&dbs_work,
Dave Jones6fe71162005-05-31 19:03:44 -0700417 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return;
419}
420
421static inline void dbs_timer_exit(void)
422{
423 cancel_delayed_work(&dbs_work);
424 return;
425}
426
427static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
428 unsigned int event)
429{
430 unsigned int cpu = policy->cpu;
431 struct cpu_dbs_info_s *this_dbs_info;
432 unsigned int j;
433
434 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
435
436 switch (event) {
437 case CPUFREQ_GOV_START:
438 if ((!cpu_online(cpu)) ||
439 (!policy->cur))
440 return -EINVAL;
441
442 if (policy->cpuinfo.transition_latency >
443 (TRANSITION_LATENCY_LIMIT * 1000))
444 return -EINVAL;
445 if (this_dbs_info->enable) /* Already enabled */
446 break;
447
448 down(&dbs_sem);
449 for_each_cpu_mask(j, policy->cpus) {
450 struct cpu_dbs_info_s *j_dbs_info;
451 j_dbs_info = &per_cpu(cpu_dbs_info, j);
452 j_dbs_info->cur_policy = policy;
453
454 j_dbs_info->prev_cpu_idle_up =
455 kstat_cpu(j).cpustat.idle +
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700456 kstat_cpu(j).cpustat.iowait +
457 ( !dbs_tuners_ins.ignore_nice
458 ? kstat_cpu(j).cpustat.nice : 0 );
459 j_dbs_info->prev_cpu_idle_down
460 = j_dbs_info->prev_cpu_idle_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 this_dbs_info->enable = 1;
463 sysfs_create_group(&policy->kobj, &dbs_attr_group);
464 dbs_enable++;
465 /*
466 * Start the timerschedule work, when this governor
467 * is used for first time
468 */
469 if (dbs_enable == 1) {
470 unsigned int latency;
471 /* policy latency is in nS. Convert it to uS first */
472
473 latency = policy->cpuinfo.transition_latency;
474 if (latency < 1000)
475 latency = 1000;
476
477 def_sampling_rate = (latency / 1000) *
478 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
479 dbs_tuners_ins.sampling_rate = def_sampling_rate;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700480 dbs_tuners_ins.ignore_nice = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 dbs_timer_init();
483 }
484
485 up(&dbs_sem);
486 break;
487
488 case CPUFREQ_GOV_STOP:
489 down(&dbs_sem);
490 this_dbs_info->enable = 0;
491 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
492 dbs_enable--;
493 /*
494 * Stop the timerschedule work, when this governor
495 * is used for first time
496 */
497 if (dbs_enable == 0)
498 dbs_timer_exit();
499
500 up(&dbs_sem);
501
502 break;
503
504 case CPUFREQ_GOV_LIMITS:
505 down(&dbs_sem);
506 if (policy->max < this_dbs_info->cur_policy->cur)
507 __cpufreq_driver_target(
508 this_dbs_info->cur_policy,
509 policy->max, CPUFREQ_RELATION_H);
510 else if (policy->min > this_dbs_info->cur_policy->cur)
511 __cpufreq_driver_target(
512 this_dbs_info->cur_policy,
513 policy->min, CPUFREQ_RELATION_L);
514 up(&dbs_sem);
515 break;
516 }
517 return 0;
518}
519
Dave Jones7f335d42005-05-31 19:03:46 -0700520static struct cpufreq_governor cpufreq_gov_dbs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 .name = "ondemand",
522 .governor = cpufreq_governor_dbs,
523 .owner = THIS_MODULE,
524};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526static int __init cpufreq_gov_dbs_init(void)
527{
528 return cpufreq_register_governor(&cpufreq_gov_dbs);
529}
530
531static void __exit cpufreq_gov_dbs_exit(void)
532{
533 /* Make sure that the scheduled work is indeed not running */
534 flush_scheduled_work();
535
536 cpufreq_unregister_governor(&cpufreq_gov_dbs);
537}
538
539
540MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
541MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
542 "Low Latency Frequency Transition capable processors");
543MODULE_LICENSE ("GPL");
544
545module_init(cpufreq_gov_dbs_init);
546module_exit(cpufreq_gov_dbs_exit);