Merge master.kernel.org:/pub/scm/linux/kernel/git/aia21/ntfs-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / cpufreq / cpufreq_ondemand.c
CommitLineData
1da177e4
LT
1/*
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)
c29f1403 37#define MIN_FREQUENCY_UP_THRESHOLD (11)
1da177e4
LT
38#define MAX_FREQUENCY_UP_THRESHOLD (100)
39
1da177e4
LT
40/*
41 * The polling frequency of this governor depends on the capability of
42 * the processor. Default polling frequency is 1000 times the transition
43 * latency of the processor. The governor will work on any processor with
44 * transition latency <= 10mS, using appropriate sampling
45 * rate.
46 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
47 * this governor will not work.
48 * All times here are in uS.
49 */
50static unsigned int def_sampling_rate;
51#define MIN_SAMPLING_RATE (def_sampling_rate / 2)
52#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
53#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
e131832c
DJ
54#define DEF_SAMPLING_DOWN_FACTOR (1)
55#define MAX_SAMPLING_DOWN_FACTOR (10)
1da177e4 56#define TRANSITION_LATENCY_LIMIT (10 * 1000)
1da177e4
LT
57
58static void do_dbs_timer(void *data);
59
60struct cpu_dbs_info_s {
61 struct cpufreq_policy *cur_policy;
62 unsigned int prev_cpu_idle_up;
63 unsigned int prev_cpu_idle_down;
64 unsigned int enable;
65};
66static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
67
68static unsigned int dbs_enable; /* number of CPUs using this policy */
69
70static DECLARE_MUTEX (dbs_sem);
71static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
72
73struct dbs_tuners {
74 unsigned int sampling_rate;
75 unsigned int sampling_down_factor;
76 unsigned int up_threshold;
3d5ee9e5 77 unsigned int ignore_nice;
1da177e4
LT
78};
79
80static struct dbs_tuners dbs_tuners_ins = {
81 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
1da177e4
LT
82 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
83};
84
dac1c1a5
DJ
85static inline unsigned int get_cpu_idle_time(unsigned int cpu)
86{
87 return kstat_cpu(cpu).cpustat.idle +
88 kstat_cpu(cpu).cpustat.iowait +
89 ( !dbs_tuners_ins.ignore_nice ?
90 kstat_cpu(cpu).cpustat.nice :
91 0);
92}
93
1da177e4
LT
94/************************** sysfs interface ************************/
95static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
96{
97 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
98}
99
100static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
101{
102 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
103}
104
105#define define_one_ro(_name) \
106static struct freq_attr _name = \
107__ATTR(_name, 0444, show_##_name, NULL)
108
109define_one_ro(sampling_rate_max);
110define_one_ro(sampling_rate_min);
111
112/* cpufreq_ondemand Governor Tunables */
113#define show_one(file_name, object) \
114static ssize_t show_##file_name \
115(struct cpufreq_policy *unused, char *buf) \
116{ \
117 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
118}
119show_one(sampling_rate, sampling_rate);
120show_one(sampling_down_factor, sampling_down_factor);
121show_one(up_threshold, up_threshold);
3d5ee9e5 122show_one(ignore_nice, ignore_nice);
1da177e4
LT
123
124static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused,
125 const char *buf, size_t count)
126{
127 unsigned int input;
128 int ret;
129 ret = sscanf (buf, "%u", &input);
130 if (ret != 1 )
131 return -EINVAL;
132
e131832c
DJ
133 if (input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
134 return -EINVAL;
135
1da177e4
LT
136 down(&dbs_sem);
137 dbs_tuners_ins.sampling_down_factor = input;
138 up(&dbs_sem);
139
140 return count;
141}
142
143static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
144 const char *buf, size_t count)
145{
146 unsigned int input;
147 int ret;
148 ret = sscanf (buf, "%u", &input);
149
150 down(&dbs_sem);
151 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
152 up(&dbs_sem);
153 return -EINVAL;
154 }
155
156 dbs_tuners_ins.sampling_rate = input;
157 up(&dbs_sem);
158
159 return count;
160}
161
162static ssize_t store_up_threshold(struct cpufreq_policy *unused,
163 const char *buf, size_t count)
164{
165 unsigned int input;
166 int ret;
167 ret = sscanf (buf, "%u", &input);
168
169 down(&dbs_sem);
170 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
c29f1403 171 input < MIN_FREQUENCY_UP_THRESHOLD) {
1da177e4
LT
172 up(&dbs_sem);
173 return -EINVAL;
174 }
175
176 dbs_tuners_ins.up_threshold = input;
177 up(&dbs_sem);
178
179 return count;
180}
181
3d5ee9e5
DJ
182static ssize_t store_ignore_nice(struct cpufreq_policy *policy,
183 const char *buf, size_t count)
184{
185 unsigned int input;
186 int ret;
187
188 unsigned int j;
189
190 ret = sscanf (buf, "%u", &input);
191 if ( ret != 1 )
192 return -EINVAL;
193
194 if ( input > 1 )
195 input = 1;
196
197 down(&dbs_sem);
198 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
199 up(&dbs_sem);
200 return count;
201 }
202 dbs_tuners_ins.ignore_nice = input;
203
204 /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */
dac1c1a5 205 for_each_online_cpu(j) {
3d5ee9e5
DJ
206 struct cpu_dbs_info_s *j_dbs_info;
207 j_dbs_info = &per_cpu(cpu_dbs_info, j);
dac1c1a5 208 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
3d5ee9e5
DJ
209 j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up;
210 }
211 up(&dbs_sem);
212
213 return count;
214}
215
1da177e4
LT
216#define define_one_rw(_name) \
217static struct freq_attr _name = \
218__ATTR(_name, 0644, show_##_name, store_##_name)
219
220define_one_rw(sampling_rate);
221define_one_rw(sampling_down_factor);
222define_one_rw(up_threshold);
3d5ee9e5 223define_one_rw(ignore_nice);
1da177e4
LT
224
225static struct attribute * dbs_attributes[] = {
226 &sampling_rate_max.attr,
227 &sampling_rate_min.attr,
228 &sampling_rate.attr,
229 &sampling_down_factor.attr,
230 &up_threshold.attr,
3d5ee9e5 231 &ignore_nice.attr,
1da177e4
LT
232 NULL
233};
234
235static struct attribute_group dbs_attr_group = {
236 .attrs = dbs_attributes,
237 .name = "ondemand",
238};
239
240/************************** sysfs end ************************/
241
242static void dbs_check_cpu(int cpu)
243{
c29f1403
DJ
244 unsigned int idle_ticks, up_idle_ticks, total_ticks;
245 unsigned int freq_next;
1da177e4
LT
246 unsigned int freq_down_sampling_rate;
247 static int down_skip[NR_CPUS];
248 struct cpu_dbs_info_s *this_dbs_info;
249
250 struct cpufreq_policy *policy;
251 unsigned int j;
252
253 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
254 if (!this_dbs_info->enable)
255 return;
256
257 policy = this_dbs_info->cur_policy;
258 /*
c29f1403
DJ
259 * Every sampling_rate, we check, if current idle time is less
260 * than 20% (default), then we try to increase frequency
261 * Every sampling_rate*sampling_down_factor, we look for a the lowest
262 * frequency which can sustain the load while keeping idle time over
263 * 30%. If such a frequency exist, we try to decrease to this frequency.
1da177e4
LT
264 *
265 * Any frequency increase takes it to the maximum frequency.
266 * Frequency reduction happens at minimum steps of
c29f1403 267 * 5% (default) of current frequency
1da177e4
LT
268 */
269
270 /* Check for frequency increase */
9c7d269b 271 idle_ticks = UINT_MAX;
1da177e4 272 for_each_cpu_mask(j, policy->cpus) {
9c7d269b 273 unsigned int tmp_idle_ticks, total_idle_ticks;
1da177e4
LT
274 struct cpu_dbs_info_s *j_dbs_info;
275
1da177e4 276 j_dbs_info = &per_cpu(cpu_dbs_info, j);
dac1c1a5 277 total_idle_ticks = get_cpu_idle_time(j);
1da177e4
LT
278 tmp_idle_ticks = total_idle_ticks -
279 j_dbs_info->prev_cpu_idle_up;
280 j_dbs_info->prev_cpu_idle_up = total_idle_ticks;
281
282 if (tmp_idle_ticks < idle_ticks)
283 idle_ticks = tmp_idle_ticks;
284 }
285
286 /* Scale idle ticks by 100 and compare with up and down ticks */
287 idle_ticks *= 100;
288 up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) *
6fe71165 289 usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
1da177e4
LT
290
291 if (idle_ticks < up_idle_ticks) {
dac1c1a5 292 down_skip[cpu] = 0;
790d76fa
DJ
293 for_each_cpu_mask(j, policy->cpus) {
294 struct cpu_dbs_info_s *j_dbs_info;
295
296 j_dbs_info = &per_cpu(cpu_dbs_info, j);
297 j_dbs_info->prev_cpu_idle_down =
298 j_dbs_info->prev_cpu_idle_up;
299 }
c11420a6
DJ
300 /* if we are already at full speed then break out early */
301 if (policy->cur == policy->max)
302 return;
303
1da177e4
LT
304 __cpufreq_driver_target(policy, policy->max,
305 CPUFREQ_RELATION_H);
1da177e4
LT
306 return;
307 }
308
309 /* Check for frequency decrease */
310 down_skip[cpu]++;
311 if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor)
312 return;
313
9c7d269b 314 idle_ticks = UINT_MAX;
1da177e4 315 for_each_cpu_mask(j, policy->cpus) {
9c7d269b 316 unsigned int tmp_idle_ticks, total_idle_ticks;
1da177e4
LT
317 struct cpu_dbs_info_s *j_dbs_info;
318
1da177e4 319 j_dbs_info = &per_cpu(cpu_dbs_info, j);
dac1c1a5
DJ
320 /* Check for frequency decrease */
321 total_idle_ticks = j_dbs_info->prev_cpu_idle_up;
1da177e4
LT
322 tmp_idle_ticks = total_idle_ticks -
323 j_dbs_info->prev_cpu_idle_down;
324 j_dbs_info->prev_cpu_idle_down = total_idle_ticks;
325
326 if (tmp_idle_ticks < idle_ticks)
327 idle_ticks = tmp_idle_ticks;
328 }
329
1da177e4 330 down_skip[cpu] = 0;
c29f1403
DJ
331 /* if we cannot reduce the frequency anymore, break out early */
332 if (policy->cur == policy->min)
333 return;
1da177e4 334
c29f1403 335 /* Compute how many ticks there are between two measurements */
1da177e4
LT
336 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate *
337 dbs_tuners_ins.sampling_down_factor;
c29f1403 338 total_ticks = usecs_to_jiffies(freq_down_sampling_rate);
1206aaac 339
c29f1403
DJ
340 /*
341 * The optimal frequency is the frequency that is the lowest that
342 * can support the current CPU usage without triggering the up
343 * policy. To be safe, we focus 10 points under the threshold.
344 */
345 freq_next = ((total_ticks - idle_ticks) * 100) / total_ticks;
346 freq_next = (freq_next * policy->cur) /
347 (dbs_tuners_ins.up_threshold - 10);
1da177e4 348
c29f1403
DJ
349 if (freq_next <= ((policy->cur * 95) / 100))
350 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
1da177e4
LT
351}
352
353static void do_dbs_timer(void *data)
354{
355 int i;
356 down(&dbs_sem);
6fe71165
DJ
357 for_each_online_cpu(i)
358 dbs_check_cpu(i);
1da177e4 359 schedule_delayed_work(&dbs_work,
6fe71165 360 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
1da177e4
LT
361 up(&dbs_sem);
362}
363
364static inline void dbs_timer_init(void)
365{
366 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
367 schedule_delayed_work(&dbs_work,
6fe71165 368 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
1da177e4
LT
369 return;
370}
371
372static inline void dbs_timer_exit(void)
373{
374 cancel_delayed_work(&dbs_work);
375 return;
376}
377
378static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
379 unsigned int event)
380{
381 unsigned int cpu = policy->cpu;
382 struct cpu_dbs_info_s *this_dbs_info;
383 unsigned int j;
384
385 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
386
387 switch (event) {
388 case CPUFREQ_GOV_START:
389 if ((!cpu_online(cpu)) ||
390 (!policy->cur))
391 return -EINVAL;
392
393 if (policy->cpuinfo.transition_latency >
394 (TRANSITION_LATENCY_LIMIT * 1000))
395 return -EINVAL;
396 if (this_dbs_info->enable) /* Already enabled */
397 break;
398
399 down(&dbs_sem);
400 for_each_cpu_mask(j, policy->cpus) {
401 struct cpu_dbs_info_s *j_dbs_info;
402 j_dbs_info = &per_cpu(cpu_dbs_info, j);
403 j_dbs_info->cur_policy = policy;
404
dac1c1a5 405 j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j);
3d5ee9e5
DJ
406 j_dbs_info->prev_cpu_idle_down
407 = j_dbs_info->prev_cpu_idle_up;
1da177e4
LT
408 }
409 this_dbs_info->enable = 1;
410 sysfs_create_group(&policy->kobj, &dbs_attr_group);
411 dbs_enable++;
412 /*
413 * Start the timerschedule work, when this governor
414 * is used for first time
415 */
416 if (dbs_enable == 1) {
417 unsigned int latency;
418 /* policy latency is in nS. Convert it to uS first */
419
420 latency = policy->cpuinfo.transition_latency;
421 if (latency < 1000)
422 latency = 1000;
423
424 def_sampling_rate = (latency / 1000) *
425 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
426 dbs_tuners_ins.sampling_rate = def_sampling_rate;
3d5ee9e5 427 dbs_tuners_ins.ignore_nice = 0;
1da177e4
LT
428
429 dbs_timer_init();
430 }
431
432 up(&dbs_sem);
433 break;
434
435 case CPUFREQ_GOV_STOP:
436 down(&dbs_sem);
437 this_dbs_info->enable = 0;
438 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
439 dbs_enable--;
440 /*
441 * Stop the timerschedule work, when this governor
442 * is used for first time
443 */
444 if (dbs_enable == 0)
445 dbs_timer_exit();
446
447 up(&dbs_sem);
448
449 break;
450
451 case CPUFREQ_GOV_LIMITS:
452 down(&dbs_sem);
453 if (policy->max < this_dbs_info->cur_policy->cur)
454 __cpufreq_driver_target(
455 this_dbs_info->cur_policy,
456 policy->max, CPUFREQ_RELATION_H);
457 else if (policy->min > this_dbs_info->cur_policy->cur)
458 __cpufreq_driver_target(
459 this_dbs_info->cur_policy,
460 policy->min, CPUFREQ_RELATION_L);
461 up(&dbs_sem);
462 break;
463 }
464 return 0;
465}
466
7f335d4e 467static struct cpufreq_governor cpufreq_gov_dbs = {
1da177e4
LT
468 .name = "ondemand",
469 .governor = cpufreq_governor_dbs,
470 .owner = THIS_MODULE,
471};
1da177e4
LT
472
473static int __init cpufreq_gov_dbs_init(void)
474{
475 return cpufreq_register_governor(&cpufreq_gov_dbs);
476}
477
478static void __exit cpufreq_gov_dbs_exit(void)
479{
480 /* Make sure that the scheduled work is indeed not running */
481 flush_scheduled_work();
482
483 cpufreq_unregister_governor(&cpufreq_gov_dbs);
484}
485
486
487MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
488MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
489 "Low Latency Frequency Transition capable processors");
490MODULE_LICENSE ("GPL");
491
492module_init(cpufreq_gov_dbs_init);
493module_exit(cpufreq_gov_dbs_exit);