blob: 84c658822a105393801b01d243dbb6b719593c52 [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;
81};
82
83static struct dbs_tuners dbs_tuners_ins = {
84 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
85 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
86 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
87};
88
89/************************** sysfs interface ************************/
90static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
91{
92 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
93}
94
95static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
96{
97 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
98}
99
100#define define_one_ro(_name) \
101static struct freq_attr _name = \
102__ATTR(_name, 0444, show_##_name, NULL)
103
104define_one_ro(sampling_rate_max);
105define_one_ro(sampling_rate_min);
106
107/* cpufreq_ondemand Governor Tunables */
108#define show_one(file_name, object) \
109static ssize_t show_##file_name \
110(struct cpufreq_policy *unused, char *buf) \
111{ \
112 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
113}
114show_one(sampling_rate, sampling_rate);
115show_one(sampling_down_factor, sampling_down_factor);
116show_one(up_threshold, up_threshold);
117show_one(down_threshold, down_threshold);
118
119static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
120 const char *buf, size_t count)
121{
122 unsigned int input;
123 int ret;
124 ret = sscanf (buf, "%u", &input);
125 if (ret != 1 )
126 return -EINVAL;
127
128 down(&dbs_sem);
129 dbs_tuners_ins.sampling_down_factor = input;
130 up(&dbs_sem);
131
132 return count;
133}
134
135static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
136 const char *buf, size_t count)
137{
138 unsigned int input;
139 int ret;
140 ret = sscanf (buf, "%u", &input);
141
142 down(&dbs_sem);
143 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
144 up(&dbs_sem);
145 return -EINVAL;
146 }
147
148 dbs_tuners_ins.sampling_rate = input;
149 up(&dbs_sem);
150
151 return count;
152}
153
154static ssize_t store_up_threshold(struct cpufreq_policy *unused,
155 const char *buf, size_t count)
156{
157 unsigned int input;
158 int ret;
159 ret = sscanf (buf, "%u", &input);
160
161 down(&dbs_sem);
162 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
163 input < MIN_FREQUENCY_UP_THRESHOLD ||
164 input <= dbs_tuners_ins.down_threshold) {
165 up(&dbs_sem);
166 return -EINVAL;
167 }
168
169 dbs_tuners_ins.up_threshold = input;
170 up(&dbs_sem);
171
172 return count;
173}
174
175static ssize_t store_down_threshold(struct cpufreq_policy *unused,
176 const char *buf, size_t count)
177{
178 unsigned int input;
179 int ret;
180 ret = sscanf (buf, "%u", &input);
181
182 down(&dbs_sem);
183 if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD ||
184 input < MIN_FREQUENCY_DOWN_THRESHOLD ||
185 input >= dbs_tuners_ins.up_threshold) {
186 up(&dbs_sem);
187 return -EINVAL;
188 }
189
190 dbs_tuners_ins.down_threshold = input;
191 up(&dbs_sem);
192
193 return count;
194}
195
196#define define_one_rw(_name) \
197static struct freq_attr _name = \
198__ATTR(_name, 0644, show_##_name, store_##_name)
199
200define_one_rw(sampling_rate);
201define_one_rw(sampling_down_factor);
202define_one_rw(up_threshold);
203define_one_rw(down_threshold);
204
205static struct attribute * dbs_attributes[] = {
206 &sampling_rate_max.attr,
207 &sampling_rate_min.attr,
208 &sampling_rate.attr,
209 &sampling_down_factor.attr,
210 &up_threshold.attr,
211 &down_threshold.attr,
212 NULL
213};
214
215static struct attribute_group dbs_attr_group = {
216 .attrs = dbs_attributes,
217 .name = "ondemand",
218};
219
220/************************** sysfs end ************************/
221
222static void dbs_check_cpu(int cpu)
223{
224 unsigned int idle_ticks, up_idle_ticks, down_idle_ticks;
225 unsigned int total_idle_ticks;
226 unsigned int freq_down_step;
227 unsigned int freq_down_sampling_rate;
228 static int down_skip[NR_CPUS];
229 struct cpu_dbs_info_s *this_dbs_info;
230
231 struct cpufreq_policy *policy;
232 unsigned int j;
233
234 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
235 if (!this_dbs_info->enable)
236 return;
237
238 policy = this_dbs_info->cur_policy;
239 /*
240 * The default safe range is 20% to 80%
241 * Every sampling_rate, we check
242 * - If current idle time is less than 20%, then we try to
243 * increase frequency
244 * Every sampling_rate*sampling_down_factor, we check
245 * - If current idle time is more than 80%, then we try to
246 * decrease frequency
247 *
248 * Any frequency increase takes it to the maximum frequency.
249 * Frequency reduction happens at minimum steps of
250 * 5% of max_frequency
251 */
252
253 /* Check for frequency increase */
254 total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
255 kstat_cpu(cpu).cpustat.iowait;
256 idle_ticks = total_idle_ticks -
257 this_dbs_info->prev_cpu_idle_up;
258 this_dbs_info->prev_cpu_idle_up = total_idle_ticks;
259
260
261 for_each_cpu_mask(j, policy->cpus) {
262 unsigned int tmp_idle_ticks;
263 struct cpu_dbs_info_s *j_dbs_info;
264
265 if (j == cpu)
266 continue;
267
268 j_dbs_info = &per_cpu(cpu_dbs_info, j);
269 /* Check for frequency increase */
270 total_idle_ticks = kstat_cpu(j).cpustat.idle +
271 kstat_cpu(j).cpustat.iowait;
272 tmp_idle_ticks = total_idle_ticks -
273 j_dbs_info->prev_cpu_idle_up;
274 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
275
276 if (tmp_idle_ticks < idle_ticks)
277 idle_ticks = tmp_idle_ticks;
278 }
279
280 /* Scale idle ticks by 100 and compare with up and down ticks */
281 idle_ticks *= 100;
282 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
Dave Jones6fe71162005-05-31 19:03:44 -0700283 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (idle_ticks < up_idle_ticks) {
286 __cpufreq_driver_target(policy, policy->max,
287 CPUFREQ_RELATION_H);
288 down_skip[cpu] = 0;
289 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
290 return;
291 }
292
293 /* Check for frequency decrease */
294 down_skip[cpu]++;
295 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
296 return;
297
298 total_idle_ticks = kstat_cpu(cpu).cpustat.idle +
299 kstat_cpu(cpu).cpustat.iowait;
300 idle_ticks = total_idle_ticks -
301 this_dbs_info->prev_cpu_idle_down;
302 this_dbs_info->prev_cpu_idle_down = total_idle_ticks;
303
304 for_each_cpu_mask(j, policy->cpus) {
305 unsigned int tmp_idle_ticks;
306 struct cpu_dbs_info_s *j_dbs_info;
307
308 if (j == cpu)
309 continue;
310
311 j_dbs_info = &per_cpu(cpu_dbs_info, j);
312 /* Check for frequency increase */
313 total_idle_ticks = kstat_cpu(j).cpustat.idle +
314 kstat_cpu(j).cpustat.iowait;
315 tmp_idle_ticks = total_idle_ticks -
316 j_dbs_info->prev_cpu_idle_down;
317 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
318
319 if (tmp_idle_ticks < idle_ticks)
320 idle_ticks = tmp_idle_ticks;
321 }
322
323 /* Scale idle ticks by 100 and compare with up and down ticks */
324 idle_ticks *= 100;
325 down_skip[cpu] = 0;
326
327 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
328 dbs_tuners_ins.sampling_down_factor;
329 down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) *
Dave Jones6fe71162005-05-31 19:03:44 -0700330 usecs_to_jiffies(freq_down_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (idle_ticks > down_idle_ticks ) {
333 freq_down_step = (5 * policy->max) / 100;
334
335 /* max freq cannot be less than 100. But who knows.... */
336 if (unlikely(freq_down_step == 0))
337 freq_down_step = 5;
338
339 __cpufreq_driver_target(policy,
340 policy->cur - freq_down_step,
341 CPUFREQ_RELATION_H);
342 return;
343 }
344}
345
346static void do_dbs_timer(void *data)
347{
348 int i;
349 down(&dbs_sem);
Dave Jones6fe71162005-05-31 19:03:44 -0700350 for_each_online_cpu(i)
351 dbs_check_cpu(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 schedule_delayed_work(&dbs_work,
Dave Jones6fe71162005-05-31 19:03:44 -0700353 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 up(&dbs_sem);
355}
356
357static inline void dbs_timer_init(void)
358{
359 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
360 schedule_delayed_work(&dbs_work,
Dave Jones6fe71162005-05-31 19:03:44 -0700361 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return;
363}
364
365static inline void dbs_timer_exit(void)
366{
367 cancel_delayed_work(&dbs_work);
368 return;
369}
370
371static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
372 unsigned int event)
373{
374 unsigned int cpu = policy->cpu;
375 struct cpu_dbs_info_s *this_dbs_info;
376 unsigned int j;
377
378 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
379
380 switch (event) {
381 case CPUFREQ_GOV_START:
382 if ((!cpu_online(cpu)) ||
383 (!policy->cur))
384 return -EINVAL;
385
386 if (policy->cpuinfo.transition_latency >
387 (TRANSITION_LATENCY_LIMIT * 1000))
388 return -EINVAL;
389 if (this_dbs_info->enable) /* Already enabled */
390 break;
391
392 down(&dbs_sem);
393 for_each_cpu_mask(j, policy->cpus) {
394 struct cpu_dbs_info_s *j_dbs_info;
395 j_dbs_info = &per_cpu(cpu_dbs_info, j);
396 j_dbs_info->cur_policy = policy;
397
398 j_dbs_info->prev_cpu_idle_up =
399 kstat_cpu(j).cpustat.idle +
400 kstat_cpu(j).cpustat.iowait;
401 j_dbs_info->prev_cpu_idle_down =
402 kstat_cpu(j).cpustat.idle +
403 kstat_cpu(j).cpustat.iowait;
404 }
405 this_dbs_info->enable = 1;
406 sysfs_create_group(&policy->kobj, &dbs_attr_group);
407 dbs_enable++;
408 /*
409 * Start the timerschedule work, when this governor
410 * is used for first time
411 */
412 if (dbs_enable == 1) {
413 unsigned int latency;
414 /* policy latency is in nS. Convert it to uS first */
415
416 latency = policy->cpuinfo.transition_latency;
417 if (latency < 1000)
418 latency = 1000;
419
420 def_sampling_rate = (latency / 1000) *
421 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
422 dbs_tuners_ins.sampling_rate = def_sampling_rate;
423
424 dbs_timer_init();
425 }
426
427 up(&dbs_sem);
428 break;
429
430 case CPUFREQ_GOV_STOP:
431 down(&dbs_sem);
432 this_dbs_info->enable = 0;
433 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
434 dbs_enable--;
435 /*
436 * Stop the timerschedule work, when this governor
437 * is used for first time
438 */
439 if (dbs_enable == 0)
440 dbs_timer_exit();
441
442 up(&dbs_sem);
443
444 break;
445
446 case CPUFREQ_GOV_LIMITS:
447 down(&dbs_sem);
448 if (policy->max < this_dbs_info->cur_policy->cur)
449 __cpufreq_driver_target(
450 this_dbs_info->cur_policy,
451 policy->max, CPUFREQ_RELATION_H);
452 else if (policy->min > this_dbs_info->cur_policy->cur)
453 __cpufreq_driver_target(
454 this_dbs_info->cur_policy,
455 policy->min, CPUFREQ_RELATION_L);
456 up(&dbs_sem);
457 break;
458 }
459 return 0;
460}
461
Dave Jones7f335d42005-05-31 19:03:46 -0700462static struct cpufreq_governor cpufreq_gov_dbs = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 .name = "ondemand",
464 .governor = cpufreq_governor_dbs,
465 .owner = THIS_MODULE,
466};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468static int __init cpufreq_gov_dbs_init(void)
469{
470 return cpufreq_register_governor(&cpufreq_gov_dbs);
471}
472
473static void __exit cpufreq_gov_dbs_exit(void)
474{
475 /* Make sure that the scheduled work is indeed not running */
476 flush_scheduled_work();
477
478 cpufreq_unregister_governor(&cpufreq_gov_dbs);
479}
480
481
482MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
483MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
484 "Low Latency Frequency Transition capable processors");
485MODULE_LICENSE ("GPL");
486
487module_init(cpufreq_gov_dbs_init);
488module_exit(cpufreq_gov_dbs_exit);