Commit | Line | Data |
---|---|---|
b9170836 DJ |
1 | /* |
2 | * drivers/cpufreq/cpufreq_conservative.c | |
3 | * | |
4 | * Copyright (C) 2001 Russell King | |
5 | * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. | |
6 | * Jun Nakajima <jun.nakajima@intel.com> | |
11a80a9c | 7 | * (C) 2009 Alexander Clouter <alex@digriz.org.uk> |
b9170836 DJ |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/smp.h> | |
17 | #include <linux/init.h> | |
18 | #include <linux/interrupt.h> | |
19 | #include <linux/ctype.h> | |
20 | #include <linux/cpufreq.h> | |
21 | #include <linux/sysctl.h> | |
22 | #include <linux/types.h> | |
23 | #include <linux/fs.h> | |
24 | #include <linux/sysfs.h> | |
138a0128 | 25 | #include <linux/cpu.h> |
b9170836 DJ |
26 | #include <linux/kmod.h> |
27 | #include <linux/workqueue.h> | |
28 | #include <linux/jiffies.h> | |
29 | #include <linux/kernel_stat.h> | |
30 | #include <linux/percpu.h> | |
3fc54d37 | 31 | #include <linux/mutex.h> |
b9170836 DJ |
32 | /* |
33 | * dbs is used in this file as a shortform for demandbased switching | |
34 | * It helps to keep variable names smaller, simpler | |
35 | */ | |
36 | ||
37 | #define DEF_FREQUENCY_UP_THRESHOLD (80) | |
b9170836 | 38 | #define DEF_FREQUENCY_DOWN_THRESHOLD (20) |
b9170836 | 39 | |
18a7247d DJ |
40 | /* |
41 | * The polling frequency of this governor depends on the capability of | |
b9170836 | 42 | * the processor. Default polling frequency is 1000 times the transition |
18a7247d DJ |
43 | * latency of the processor. The governor will work on any processor with |
44 | * transition latency <= 10mS, using appropriate sampling | |
b9170836 | 45 | * rate. |
e08f5f5b GS |
46 | * For CPUs with transition latency > 10mS (mostly drivers |
47 | * with CPUFREQ_ETERNAL), this governor will not work. | |
b9170836 DJ |
48 | * All times here are in uS. |
49 | */ | |
18a7247d | 50 | static unsigned int def_sampling_rate; |
2c906b31 AC |
51 | #define MIN_SAMPLING_RATE_RATIO (2) |
52 | /* for correct statistics, we need at least 10 ticks between each measure */ | |
e08f5f5b GS |
53 | #define MIN_STAT_SAMPLING_RATE \ |
54 | (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10)) | |
55 | #define MIN_SAMPLING_RATE \ | |
56 | (def_sampling_rate / MIN_SAMPLING_RATE_RATIO) | |
112124ab TR |
57 | /* Above MIN_SAMPLING_RATE will vanish with its sysfs file soon |
58 | * Define the minimal settable sampling rate to the greater of: | |
59 | * - "HW transition latency" * 100 (same as default sampling / 10) | |
60 | * - MIN_STAT_SAMPLING_RATE | |
61 | * To avoid that userspace shoots itself. | |
62 | */ | |
63 | static unsigned int minimum_sampling_rate(void) | |
64 | { | |
65 | return max(def_sampling_rate / 10, MIN_STAT_SAMPLING_RATE); | |
66 | } | |
67 | ||
68 | /* This will also vanish soon with removing sampling_rate_max */ | |
b9170836 | 69 | #define MAX_SAMPLING_RATE (500 * def_sampling_rate) |
112124ab | 70 | #define LATENCY_MULTIPLIER (1000) |
2c906b31 AC |
71 | #define DEF_SAMPLING_DOWN_FACTOR (1) |
72 | #define MAX_SAMPLING_DOWN_FACTOR (10) | |
1c256245 | 73 | #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000) |
b9170836 | 74 | |
c4028958 | 75 | static void do_dbs_timer(struct work_struct *work); |
b9170836 DJ |
76 | |
77 | struct cpu_dbs_info_s { | |
18a7247d DJ |
78 | struct cpufreq_policy *cur_policy; |
79 | unsigned int prev_cpu_idle_up; | |
80 | unsigned int prev_cpu_idle_down; | |
81 | unsigned int enable; | |
82 | unsigned int down_skip; | |
83 | unsigned int requested_freq; | |
b9170836 DJ |
84 | }; |
85 | static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info); | |
86 | ||
87 | static unsigned int dbs_enable; /* number of CPUs using this policy */ | |
88 | ||
4ec223d0 VP |
89 | /* |
90 | * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug | |
91 | * lock and dbs_mutex. cpu_hotplug lock should always be held before | |
92 | * dbs_mutex. If any function that can potentially take cpu_hotplug lock | |
93 | * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then | |
94 | * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock | |
95 | * is recursive for the same process. -Venki | |
96 | */ | |
9acef487 | 97 | static DEFINE_MUTEX(dbs_mutex); |
c4028958 | 98 | static DECLARE_DELAYED_WORK(dbs_work, do_dbs_timer); |
b9170836 DJ |
99 | |
100 | struct dbs_tuners { | |
18a7247d DJ |
101 | unsigned int sampling_rate; |
102 | unsigned int sampling_down_factor; | |
103 | unsigned int up_threshold; | |
104 | unsigned int down_threshold; | |
105 | unsigned int ignore_nice; | |
106 | unsigned int freq_step; | |
b9170836 DJ |
107 | }; |
108 | ||
109 | static struct dbs_tuners dbs_tuners_ins = { | |
18a7247d DJ |
110 | .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, |
111 | .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD, | |
112 | .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR, | |
113 | .ignore_nice = 0, | |
114 | .freq_step = 5, | |
b9170836 DJ |
115 | }; |
116 | ||
dac1c1a5 DJ |
117 | static inline unsigned int get_cpu_idle_time(unsigned int cpu) |
118 | { | |
e08f5f5b GS |
119 | unsigned int add_nice = 0, ret; |
120 | ||
121 | if (dbs_tuners_ins.ignore_nice) | |
122 | add_nice = kstat_cpu(cpu).cpustat.nice; | |
123 | ||
18a7247d | 124 | ret = kstat_cpu(cpu).cpustat.idle + |
dac1c1a5 | 125 | kstat_cpu(cpu).cpustat.iowait + |
e08f5f5b GS |
126 | add_nice; |
127 | ||
128 | return ret; | |
dac1c1a5 DJ |
129 | } |
130 | ||
a8d7c3bc EO |
131 | /* keep track of frequency transitions */ |
132 | static int | |
133 | dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, | |
134 | void *data) | |
135 | { | |
136 | struct cpufreq_freqs *freq = data; | |
137 | struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, | |
138 | freq->cpu); | |
139 | ||
140 | if (!this_dbs_info->enable) | |
141 | return 0; | |
142 | ||
143 | this_dbs_info->requested_freq = freq->new; | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | static struct notifier_block dbs_cpufreq_notifier_block = { | |
149 | .notifier_call = dbs_cpufreq_notifier | |
150 | }; | |
151 | ||
b9170836 DJ |
152 | /************************** sysfs interface ************************/ |
153 | static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf) | |
154 | { | |
9411b4ef TR |
155 | static int print_once; |
156 | ||
157 | if (!print_once) { | |
158 | printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max " | |
159 | "sysfs file is deprecated - used by: %s\n", | |
160 | current->comm); | |
161 | print_once = 1; | |
162 | } | |
9acef487 | 163 | return sprintf(buf, "%u\n", MAX_SAMPLING_RATE); |
b9170836 DJ |
164 | } |
165 | ||
166 | static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf) | |
167 | { | |
9411b4ef TR |
168 | static int print_once; |
169 | ||
170 | if (!print_once) { | |
171 | printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max " | |
172 | "sysfs file is deprecated - used by: %s\n", current->comm); | |
173 | print_once = 1; | |
174 | } | |
9acef487 | 175 | return sprintf(buf, "%u\n", MIN_SAMPLING_RATE); |
b9170836 DJ |
176 | } |
177 | ||
18a7247d DJ |
178 | #define define_one_ro(_name) \ |
179 | static struct freq_attr _name = \ | |
b9170836 DJ |
180 | __ATTR(_name, 0444, show_##_name, NULL) |
181 | ||
182 | define_one_ro(sampling_rate_max); | |
183 | define_one_ro(sampling_rate_min); | |
184 | ||
185 | /* cpufreq_conservative Governor Tunables */ | |
186 | #define show_one(file_name, object) \ | |
187 | static ssize_t show_##file_name \ | |
188 | (struct cpufreq_policy *unused, char *buf) \ | |
189 | { \ | |
190 | return sprintf(buf, "%u\n", dbs_tuners_ins.object); \ | |
191 | } | |
192 | show_one(sampling_rate, sampling_rate); | |
193 | show_one(sampling_down_factor, sampling_down_factor); | |
194 | show_one(up_threshold, up_threshold); | |
195 | show_one(down_threshold, down_threshold); | |
001893cd | 196 | show_one(ignore_nice_load, ignore_nice); |
b9170836 DJ |
197 | show_one(freq_step, freq_step); |
198 | ||
18a7247d | 199 | static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused, |
b9170836 DJ |
200 | const char *buf, size_t count) |
201 | { | |
202 | unsigned int input; | |
203 | int ret; | |
9acef487 | 204 | ret = sscanf(buf, "%u", &input); |
2c906b31 | 205 | if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) |
b9170836 DJ |
206 | return -EINVAL; |
207 | ||
3fc54d37 | 208 | mutex_lock(&dbs_mutex); |
b9170836 | 209 | dbs_tuners_ins.sampling_down_factor = input; |
3fc54d37 | 210 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
211 | |
212 | return count; | |
213 | } | |
214 | ||
18a7247d | 215 | static ssize_t store_sampling_rate(struct cpufreq_policy *unused, |
b9170836 DJ |
216 | const char *buf, size_t count) |
217 | { | |
218 | unsigned int input; | |
219 | int ret; | |
9acef487 | 220 | ret = sscanf(buf, "%u", &input); |
b9170836 | 221 | |
3fc54d37 | 222 | mutex_lock(&dbs_mutex); |
112124ab | 223 | if (ret != 1) { |
3fc54d37 | 224 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
225 | return -EINVAL; |
226 | } | |
112124ab | 227 | dbs_tuners_ins.sampling_rate = max(input, minimum_sampling_rate()); |
3fc54d37 | 228 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
229 | |
230 | return count; | |
231 | } | |
232 | ||
18a7247d | 233 | static ssize_t store_up_threshold(struct cpufreq_policy *unused, |
b9170836 DJ |
234 | const char *buf, size_t count) |
235 | { | |
236 | unsigned int input; | |
237 | int ret; | |
9acef487 | 238 | ret = sscanf(buf, "%u", &input); |
b9170836 | 239 | |
3fc54d37 | 240 | mutex_lock(&dbs_mutex); |
9acef487 DJ |
241 | if (ret != 1 || input > 100 || |
242 | input <= dbs_tuners_ins.down_threshold) { | |
3fc54d37 | 243 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
244 | return -EINVAL; |
245 | } | |
246 | ||
247 | dbs_tuners_ins.up_threshold = input; | |
3fc54d37 | 248 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
249 | |
250 | return count; | |
251 | } | |
252 | ||
18a7247d | 253 | static ssize_t store_down_threshold(struct cpufreq_policy *unused, |
b9170836 DJ |
254 | const char *buf, size_t count) |
255 | { | |
256 | unsigned int input; | |
257 | int ret; | |
9acef487 | 258 | ret = sscanf(buf, "%u", &input); |
b9170836 | 259 | |
3fc54d37 | 260 | mutex_lock(&dbs_mutex); |
b82fbe6c | 261 | if (ret != 1 || input > 100 || input >= dbs_tuners_ins.up_threshold) { |
3fc54d37 | 262 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
263 | return -EINVAL; |
264 | } | |
265 | ||
266 | dbs_tuners_ins.down_threshold = input; | |
3fc54d37 | 267 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
268 | |
269 | return count; | |
270 | } | |
271 | ||
001893cd | 272 | static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy, |
b9170836 DJ |
273 | const char *buf, size_t count) |
274 | { | |
275 | unsigned int input; | |
276 | int ret; | |
277 | ||
278 | unsigned int j; | |
18a7247d DJ |
279 | |
280 | ret = sscanf(buf, "%u", &input); | |
281 | if (ret != 1) | |
b9170836 DJ |
282 | return -EINVAL; |
283 | ||
18a7247d | 284 | if (input > 1) |
b9170836 | 285 | input = 1; |
18a7247d | 286 | |
3fc54d37 | 287 | mutex_lock(&dbs_mutex); |
18a7247d | 288 | if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */ |
3fc54d37 | 289 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
290 | return count; |
291 | } | |
292 | dbs_tuners_ins.ignore_nice = input; | |
293 | ||
294 | /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */ | |
dac1c1a5 | 295 | for_each_online_cpu(j) { |
b9170836 DJ |
296 | struct cpu_dbs_info_s *j_dbs_info; |
297 | j_dbs_info = &per_cpu(cpu_dbs_info, j); | |
dac1c1a5 | 298 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j); |
b9170836 DJ |
299 | j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up; |
300 | } | |
3fc54d37 | 301 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
302 | |
303 | return count; | |
304 | } | |
305 | ||
306 | static ssize_t store_freq_step(struct cpufreq_policy *policy, | |
307 | const char *buf, size_t count) | |
308 | { | |
309 | unsigned int input; | |
310 | int ret; | |
311 | ||
18a7247d | 312 | ret = sscanf(buf, "%u", &input); |
b9170836 | 313 | |
18a7247d | 314 | if (ret != 1) |
b9170836 DJ |
315 | return -EINVAL; |
316 | ||
18a7247d | 317 | if (input > 100) |
b9170836 | 318 | input = 100; |
18a7247d | 319 | |
b9170836 DJ |
320 | /* no need to test here if freq_step is zero as the user might actually |
321 | * want this, they would be crazy though :) */ | |
3fc54d37 | 322 | mutex_lock(&dbs_mutex); |
b9170836 | 323 | dbs_tuners_ins.freq_step = input; |
3fc54d37 | 324 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
325 | |
326 | return count; | |
327 | } | |
328 | ||
329 | #define define_one_rw(_name) \ | |
330 | static struct freq_attr _name = \ | |
331 | __ATTR(_name, 0644, show_##_name, store_##_name) | |
332 | ||
333 | define_one_rw(sampling_rate); | |
334 | define_one_rw(sampling_down_factor); | |
335 | define_one_rw(up_threshold); | |
336 | define_one_rw(down_threshold); | |
001893cd | 337 | define_one_rw(ignore_nice_load); |
b9170836 DJ |
338 | define_one_rw(freq_step); |
339 | ||
9acef487 | 340 | static struct attribute *dbs_attributes[] = { |
b9170836 DJ |
341 | &sampling_rate_max.attr, |
342 | &sampling_rate_min.attr, | |
343 | &sampling_rate.attr, | |
344 | &sampling_down_factor.attr, | |
345 | &up_threshold.attr, | |
346 | &down_threshold.attr, | |
001893cd | 347 | &ignore_nice_load.attr, |
b9170836 DJ |
348 | &freq_step.attr, |
349 | NULL | |
350 | }; | |
351 | ||
352 | static struct attribute_group dbs_attr_group = { | |
353 | .attrs = dbs_attributes, | |
354 | .name = "conservative", | |
355 | }; | |
356 | ||
357 | /************************** sysfs end ************************/ | |
358 | ||
359 | static void dbs_check_cpu(int cpu) | |
360 | { | |
361 | unsigned int idle_ticks, up_idle_ticks, down_idle_ticks; | |
08a28e2e | 362 | unsigned int tmp_idle_ticks, total_idle_ticks; |
f068c04b | 363 | unsigned int freq_target; |
b9170836 | 364 | unsigned int freq_down_sampling_rate; |
08a28e2e | 365 | struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu); |
b9170836 | 366 | struct cpufreq_policy *policy; |
b9170836 | 367 | |
b9170836 DJ |
368 | if (!this_dbs_info->enable) |
369 | return; | |
370 | ||
08a28e2e AC |
371 | policy = this_dbs_info->cur_policy; |
372 | ||
18a7247d DJ |
373 | /* |
374 | * The default safe range is 20% to 80% | |
b9170836 | 375 | * Every sampling_rate, we check |
18a7247d DJ |
376 | * - If current idle time is less than 20%, then we try to |
377 | * increase frequency | |
b9170836 | 378 | * Every sampling_rate*sampling_down_factor, we check |
18a7247d DJ |
379 | * - If current idle time is more than 80%, then we try to |
380 | * decrease frequency | |
b9170836 | 381 | * |
18a7247d DJ |
382 | * Any frequency increase takes it to the maximum frequency. |
383 | * Frequency reduction happens at minimum steps of | |
384 | * 5% (default) of max_frequency | |
b9170836 DJ |
385 | */ |
386 | ||
387 | /* Check for frequency increase */ | |
9c7d269b | 388 | idle_ticks = UINT_MAX; |
b9170836 | 389 | |
08a28e2e AC |
390 | /* Check for frequency increase */ |
391 | total_idle_ticks = get_cpu_idle_time(cpu); | |
392 | tmp_idle_ticks = total_idle_ticks - | |
393 | this_dbs_info->prev_cpu_idle_up; | |
394 | this_dbs_info->prev_cpu_idle_up = total_idle_ticks; | |
395 | ||
396 | if (tmp_idle_ticks < idle_ticks) | |
397 | idle_ticks = tmp_idle_ticks; | |
b9170836 DJ |
398 | |
399 | /* Scale idle ticks by 100 and compare with up and down ticks */ | |
400 | idle_ticks *= 100; | |
401 | up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) * | |
2c906b31 | 402 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate); |
b9170836 DJ |
403 | |
404 | if (idle_ticks < up_idle_ticks) { | |
a159b827 | 405 | this_dbs_info->down_skip = 0; |
08a28e2e AC |
406 | this_dbs_info->prev_cpu_idle_down = |
407 | this_dbs_info->prev_cpu_idle_up; | |
790d76fa | 408 | |
b9170836 | 409 | /* if we are already at full speed then break out early */ |
a159b827 | 410 | if (this_dbs_info->requested_freq == policy->max) |
b9170836 | 411 | return; |
18a7247d | 412 | |
f068c04b | 413 | freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100; |
b9170836 DJ |
414 | |
415 | /* max freq cannot be less than 100. But who knows.... */ | |
f068c04b DJ |
416 | if (unlikely(freq_target == 0)) |
417 | freq_target = 5; | |
18a7247d | 418 | |
f068c04b | 419 | this_dbs_info->requested_freq += freq_target; |
a159b827 AC |
420 | if (this_dbs_info->requested_freq > policy->max) |
421 | this_dbs_info->requested_freq = policy->max; | |
b9170836 | 422 | |
a159b827 | 423 | __cpufreq_driver_target(policy, this_dbs_info->requested_freq, |
b9170836 | 424 | CPUFREQ_RELATION_H); |
b9170836 DJ |
425 | return; |
426 | } | |
427 | ||
428 | /* Check for frequency decrease */ | |
a159b827 AC |
429 | this_dbs_info->down_skip++; |
430 | if (this_dbs_info->down_skip < dbs_tuners_ins.sampling_down_factor) | |
b9170836 DJ |
431 | return; |
432 | ||
08a28e2e AC |
433 | /* Check for frequency decrease */ |
434 | total_idle_ticks = this_dbs_info->prev_cpu_idle_up; | |
435 | tmp_idle_ticks = total_idle_ticks - | |
436 | this_dbs_info->prev_cpu_idle_down; | |
437 | this_dbs_info->prev_cpu_idle_down = total_idle_ticks; | |
b9170836 | 438 | |
08a28e2e AC |
439 | if (tmp_idle_ticks < idle_ticks) |
440 | idle_ticks = tmp_idle_ticks; | |
b9170836 DJ |
441 | |
442 | /* Scale idle ticks by 100 and compare with up and down ticks */ | |
443 | idle_ticks *= 100; | |
a159b827 | 444 | this_dbs_info->down_skip = 0; |
b9170836 DJ |
445 | |
446 | freq_down_sampling_rate = dbs_tuners_ins.sampling_rate * | |
447 | dbs_tuners_ins.sampling_down_factor; | |
448 | down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) * | |
2c906b31 | 449 | usecs_to_jiffies(freq_down_sampling_rate); |
b9170836 | 450 | |
9c7d269b | 451 | if (idle_ticks > down_idle_ticks) { |
2c906b31 AC |
452 | /* |
453 | * if we are already at the lowest speed then break out early | |
b9170836 | 454 | * or if we 'cannot' reduce the speed as the user might want |
f068c04b | 455 | * freq_target to be zero |
2c906b31 | 456 | */ |
a159b827 | 457 | if (this_dbs_info->requested_freq == policy->min |
b9170836 DJ |
458 | || dbs_tuners_ins.freq_step == 0) |
459 | return; | |
460 | ||
f068c04b | 461 | freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100; |
b9170836 DJ |
462 | |
463 | /* max freq cannot be less than 100. But who knows.... */ | |
f068c04b DJ |
464 | if (unlikely(freq_target == 0)) |
465 | freq_target = 5; | |
b9170836 | 466 | |
f068c04b | 467 | this_dbs_info->requested_freq -= freq_target; |
a159b827 AC |
468 | if (this_dbs_info->requested_freq < policy->min) |
469 | this_dbs_info->requested_freq = policy->min; | |
b9170836 | 470 | |
a159b827 | 471 | __cpufreq_driver_target(policy, this_dbs_info->requested_freq, |
2c906b31 | 472 | CPUFREQ_RELATION_H); |
b9170836 DJ |
473 | return; |
474 | } | |
475 | } | |
476 | ||
c4028958 | 477 | static void do_dbs_timer(struct work_struct *work) |
18a7247d | 478 | { |
b9170836 | 479 | int i; |
3fc54d37 | 480 | mutex_lock(&dbs_mutex); |
b9170836 DJ |
481 | for_each_online_cpu(i) |
482 | dbs_check_cpu(i); | |
18a7247d | 483 | schedule_delayed_work(&dbs_work, |
b9170836 | 484 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
3fc54d37 | 485 | mutex_unlock(&dbs_mutex); |
18a7247d | 486 | } |
b9170836 DJ |
487 | |
488 | static inline void dbs_timer_init(void) | |
489 | { | |
8217e4f4 | 490 | init_timer_deferrable(&dbs_work.timer); |
b9170836 DJ |
491 | schedule_delayed_work(&dbs_work, |
492 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); | |
493 | return; | |
494 | } | |
495 | ||
496 | static inline void dbs_timer_exit(void) | |
497 | { | |
498 | cancel_delayed_work(&dbs_work); | |
499 | return; | |
500 | } | |
501 | ||
502 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, | |
503 | unsigned int event) | |
504 | { | |
505 | unsigned int cpu = policy->cpu; | |
506 | struct cpu_dbs_info_s *this_dbs_info; | |
507 | unsigned int j; | |
914f7c31 | 508 | int rc; |
b9170836 DJ |
509 | |
510 | this_dbs_info = &per_cpu(cpu_dbs_info, cpu); | |
511 | ||
512 | switch (event) { | |
513 | case CPUFREQ_GOV_START: | |
18a7247d | 514 | if ((!cpu_online(cpu)) || (!policy->cur)) |
b9170836 DJ |
515 | return -EINVAL; |
516 | ||
b9170836 DJ |
517 | if (this_dbs_info->enable) /* Already enabled */ |
518 | break; | |
18a7247d | 519 | |
3fc54d37 | 520 | mutex_lock(&dbs_mutex); |
914f7c31 JG |
521 | |
522 | rc = sysfs_create_group(&policy->kobj, &dbs_attr_group); | |
523 | if (rc) { | |
524 | mutex_unlock(&dbs_mutex); | |
525 | return rc; | |
526 | } | |
527 | ||
835481d9 | 528 | for_each_cpu(j, policy->cpus) { |
b9170836 DJ |
529 | struct cpu_dbs_info_s *j_dbs_info; |
530 | j_dbs_info = &per_cpu(cpu_dbs_info, j); | |
531 | j_dbs_info->cur_policy = policy; | |
18a7247d | 532 | |
08a28e2e | 533 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu); |
b9170836 DJ |
534 | j_dbs_info->prev_cpu_idle_down |
535 | = j_dbs_info->prev_cpu_idle_up; | |
536 | } | |
537 | this_dbs_info->enable = 1; | |
a159b827 AC |
538 | this_dbs_info->down_skip = 0; |
539 | this_dbs_info->requested_freq = policy->cur; | |
914f7c31 | 540 | |
b9170836 DJ |
541 | dbs_enable++; |
542 | /* | |
543 | * Start the timerschedule work, when this governor | |
544 | * is used for first time | |
545 | */ | |
546 | if (dbs_enable == 1) { | |
547 | unsigned int latency; | |
548 | /* policy latency is in nS. Convert it to uS first */ | |
2c906b31 AC |
549 | latency = policy->cpuinfo.transition_latency / 1000; |
550 | if (latency == 0) | |
551 | latency = 1; | |
b9170836 | 552 | |
112124ab TR |
553 | def_sampling_rate = |
554 | max(10 * latency * LATENCY_MULTIPLIER, | |
555 | MIN_STAT_SAMPLING_RATE); | |
2c906b31 | 556 | |
b9170836 | 557 | dbs_tuners_ins.sampling_rate = def_sampling_rate; |
b9170836 DJ |
558 | |
559 | dbs_timer_init(); | |
a8d7c3bc EO |
560 | cpufreq_register_notifier( |
561 | &dbs_cpufreq_notifier_block, | |
562 | CPUFREQ_TRANSITION_NOTIFIER); | |
b9170836 | 563 | } |
18a7247d | 564 | |
3fc54d37 | 565 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
566 | break; |
567 | ||
568 | case CPUFREQ_GOV_STOP: | |
3fc54d37 | 569 | mutex_lock(&dbs_mutex); |
b9170836 DJ |
570 | this_dbs_info->enable = 0; |
571 | sysfs_remove_group(&policy->kobj, &dbs_attr_group); | |
572 | dbs_enable--; | |
573 | /* | |
574 | * Stop the timerschedule work, when this governor | |
575 | * is used for first time | |
576 | */ | |
a8d7c3bc | 577 | if (dbs_enable == 0) { |
b9170836 | 578 | dbs_timer_exit(); |
a8d7c3bc EO |
579 | cpufreq_unregister_notifier( |
580 | &dbs_cpufreq_notifier_block, | |
581 | CPUFREQ_TRANSITION_NOTIFIER); | |
582 | } | |
583 | ||
3fc54d37 | 584 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
585 | |
586 | break; | |
587 | ||
588 | case CPUFREQ_GOV_LIMITS: | |
3fc54d37 | 589 | mutex_lock(&dbs_mutex); |
b9170836 DJ |
590 | if (policy->max < this_dbs_info->cur_policy->cur) |
591 | __cpufreq_driver_target( | |
592 | this_dbs_info->cur_policy, | |
18a7247d | 593 | policy->max, CPUFREQ_RELATION_H); |
b9170836 DJ |
594 | else if (policy->min > this_dbs_info->cur_policy->cur) |
595 | __cpufreq_driver_target( | |
596 | this_dbs_info->cur_policy, | |
18a7247d | 597 | policy->min, CPUFREQ_RELATION_L); |
3fc54d37 | 598 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
599 | break; |
600 | } | |
601 | return 0; | |
602 | } | |
603 | ||
c4d14bc0 SW |
604 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE |
605 | static | |
606 | #endif | |
1c256245 TR |
607 | struct cpufreq_governor cpufreq_gov_conservative = { |
608 | .name = "conservative", | |
609 | .governor = cpufreq_governor_dbs, | |
610 | .max_transition_latency = TRANSITION_LATENCY_LIMIT, | |
611 | .owner = THIS_MODULE, | |
b9170836 DJ |
612 | }; |
613 | ||
614 | static int __init cpufreq_gov_dbs_init(void) | |
615 | { | |
1c256245 | 616 | return cpufreq_register_governor(&cpufreq_gov_conservative); |
b9170836 DJ |
617 | } |
618 | ||
619 | static void __exit cpufreq_gov_dbs_exit(void) | |
620 | { | |
621 | /* Make sure that the scheduled work is indeed not running */ | |
622 | flush_scheduled_work(); | |
623 | ||
1c256245 | 624 | cpufreq_unregister_governor(&cpufreq_gov_conservative); |
b9170836 DJ |
625 | } |
626 | ||
627 | ||
11a80a9c | 628 | MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>"); |
9acef487 | 629 | MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for " |
b9170836 DJ |
630 | "Low Latency Frequency Transition capable processors " |
631 | "optimised for use in a battery environment"); | |
9acef487 | 632 | MODULE_LICENSE("GPL"); |
b9170836 | 633 | |
6915719b JW |
634 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE |
635 | fs_initcall(cpufreq_gov_dbs_init); | |
636 | #else | |
b9170836 | 637 | module_init(cpufreq_gov_dbs_init); |
6915719b | 638 | #endif |
b9170836 | 639 | module_exit(cpufreq_gov_dbs_exit); |