]>
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 | ||
f407a08b AC |
140 | struct cpufreq_policy *policy; |
141 | ||
a8d7c3bc EO |
142 | if (!this_dbs_info->enable) |
143 | return 0; | |
144 | ||
f407a08b AC |
145 | policy = this_dbs_info->cur_policy; |
146 | ||
147 | /* | |
148 | * we only care if our internally tracked freq moves outside | |
149 | * the 'valid' ranges of freqency available to us otherwise | |
150 | * we do not change it | |
151 | */ | |
152 | if (this_dbs_info->requested_freq > policy->max | |
153 | || this_dbs_info->requested_freq < policy->min) | |
154 | this_dbs_info->requested_freq = freq->new; | |
a8d7c3bc EO |
155 | |
156 | return 0; | |
157 | } | |
158 | ||
159 | static struct notifier_block dbs_cpufreq_notifier_block = { | |
160 | .notifier_call = dbs_cpufreq_notifier | |
161 | }; | |
162 | ||
b9170836 DJ |
163 | /************************** sysfs interface ************************/ |
164 | static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf) | |
165 | { | |
9411b4ef TR |
166 | static int print_once; |
167 | ||
168 | if (!print_once) { | |
169 | printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max " | |
170 | "sysfs file is deprecated - used by: %s\n", | |
171 | current->comm); | |
172 | print_once = 1; | |
173 | } | |
9acef487 | 174 | return sprintf(buf, "%u\n", MAX_SAMPLING_RATE); |
b9170836 DJ |
175 | } |
176 | ||
177 | static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf) | |
178 | { | |
9411b4ef TR |
179 | static int print_once; |
180 | ||
181 | if (!print_once) { | |
182 | printk(KERN_INFO "CPUFREQ: conservative sampling_rate_max " | |
183 | "sysfs file is deprecated - used by: %s\n", current->comm); | |
184 | print_once = 1; | |
185 | } | |
9acef487 | 186 | return sprintf(buf, "%u\n", MIN_SAMPLING_RATE); |
b9170836 DJ |
187 | } |
188 | ||
18a7247d DJ |
189 | #define define_one_ro(_name) \ |
190 | static struct freq_attr _name = \ | |
b9170836 DJ |
191 | __ATTR(_name, 0444, show_##_name, NULL) |
192 | ||
193 | define_one_ro(sampling_rate_max); | |
194 | define_one_ro(sampling_rate_min); | |
195 | ||
196 | /* cpufreq_conservative Governor Tunables */ | |
197 | #define show_one(file_name, object) \ | |
198 | static ssize_t show_##file_name \ | |
199 | (struct cpufreq_policy *unused, char *buf) \ | |
200 | { \ | |
201 | return sprintf(buf, "%u\n", dbs_tuners_ins.object); \ | |
202 | } | |
203 | show_one(sampling_rate, sampling_rate); | |
204 | show_one(sampling_down_factor, sampling_down_factor); | |
205 | show_one(up_threshold, up_threshold); | |
206 | show_one(down_threshold, down_threshold); | |
001893cd | 207 | show_one(ignore_nice_load, ignore_nice); |
b9170836 DJ |
208 | show_one(freq_step, freq_step); |
209 | ||
18a7247d | 210 | static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused, |
b9170836 DJ |
211 | const char *buf, size_t count) |
212 | { | |
213 | unsigned int input; | |
214 | int ret; | |
9acef487 | 215 | ret = sscanf(buf, "%u", &input); |
2c906b31 | 216 | if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) |
b9170836 DJ |
217 | return -EINVAL; |
218 | ||
3fc54d37 | 219 | mutex_lock(&dbs_mutex); |
b9170836 | 220 | dbs_tuners_ins.sampling_down_factor = input; |
3fc54d37 | 221 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
222 | |
223 | return count; | |
224 | } | |
225 | ||
18a7247d | 226 | static ssize_t store_sampling_rate(struct cpufreq_policy *unused, |
b9170836 DJ |
227 | const char *buf, size_t count) |
228 | { | |
229 | unsigned int input; | |
230 | int ret; | |
9acef487 | 231 | ret = sscanf(buf, "%u", &input); |
b9170836 | 232 | |
3fc54d37 | 233 | mutex_lock(&dbs_mutex); |
112124ab | 234 | if (ret != 1) { |
3fc54d37 | 235 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
236 | return -EINVAL; |
237 | } | |
112124ab | 238 | dbs_tuners_ins.sampling_rate = max(input, minimum_sampling_rate()); |
3fc54d37 | 239 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
240 | |
241 | return count; | |
242 | } | |
243 | ||
18a7247d | 244 | static ssize_t store_up_threshold(struct cpufreq_policy *unused, |
b9170836 DJ |
245 | const char *buf, size_t count) |
246 | { | |
247 | unsigned int input; | |
248 | int ret; | |
9acef487 | 249 | ret = sscanf(buf, "%u", &input); |
b9170836 | 250 | |
3fc54d37 | 251 | mutex_lock(&dbs_mutex); |
9acef487 DJ |
252 | if (ret != 1 || input > 100 || |
253 | input <= dbs_tuners_ins.down_threshold) { | |
3fc54d37 | 254 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
255 | return -EINVAL; |
256 | } | |
257 | ||
258 | dbs_tuners_ins.up_threshold = input; | |
3fc54d37 | 259 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
260 | |
261 | return count; | |
262 | } | |
263 | ||
18a7247d | 264 | static ssize_t store_down_threshold(struct cpufreq_policy *unused, |
b9170836 DJ |
265 | const char *buf, size_t count) |
266 | { | |
267 | unsigned int input; | |
268 | int ret; | |
9acef487 | 269 | ret = sscanf(buf, "%u", &input); |
b9170836 | 270 | |
3fc54d37 | 271 | mutex_lock(&dbs_mutex); |
b82fbe6c | 272 | if (ret != 1 || input > 100 || input >= dbs_tuners_ins.up_threshold) { |
3fc54d37 | 273 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
274 | return -EINVAL; |
275 | } | |
276 | ||
277 | dbs_tuners_ins.down_threshold = input; | |
3fc54d37 | 278 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
279 | |
280 | return count; | |
281 | } | |
282 | ||
001893cd | 283 | static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy, |
b9170836 DJ |
284 | const char *buf, size_t count) |
285 | { | |
286 | unsigned int input; | |
287 | int ret; | |
288 | ||
289 | unsigned int j; | |
18a7247d DJ |
290 | |
291 | ret = sscanf(buf, "%u", &input); | |
292 | if (ret != 1) | |
b9170836 DJ |
293 | return -EINVAL; |
294 | ||
18a7247d | 295 | if (input > 1) |
b9170836 | 296 | input = 1; |
18a7247d | 297 | |
3fc54d37 | 298 | mutex_lock(&dbs_mutex); |
18a7247d | 299 | if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */ |
3fc54d37 | 300 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
301 | return count; |
302 | } | |
303 | dbs_tuners_ins.ignore_nice = input; | |
304 | ||
305 | /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */ | |
dac1c1a5 | 306 | for_each_online_cpu(j) { |
b9170836 DJ |
307 | struct cpu_dbs_info_s *j_dbs_info; |
308 | j_dbs_info = &per_cpu(cpu_dbs_info, j); | |
dac1c1a5 | 309 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j); |
b9170836 DJ |
310 | j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up; |
311 | } | |
3fc54d37 | 312 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
313 | |
314 | return count; | |
315 | } | |
316 | ||
317 | static ssize_t store_freq_step(struct cpufreq_policy *policy, | |
318 | const char *buf, size_t count) | |
319 | { | |
320 | unsigned int input; | |
321 | int ret; | |
322 | ||
18a7247d | 323 | ret = sscanf(buf, "%u", &input); |
b9170836 | 324 | |
18a7247d | 325 | if (ret != 1) |
b9170836 DJ |
326 | return -EINVAL; |
327 | ||
18a7247d | 328 | if (input > 100) |
b9170836 | 329 | input = 100; |
18a7247d | 330 | |
b9170836 DJ |
331 | /* no need to test here if freq_step is zero as the user might actually |
332 | * want this, they would be crazy though :) */ | |
3fc54d37 | 333 | mutex_lock(&dbs_mutex); |
b9170836 | 334 | dbs_tuners_ins.freq_step = input; |
3fc54d37 | 335 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
336 | |
337 | return count; | |
338 | } | |
339 | ||
340 | #define define_one_rw(_name) \ | |
341 | static struct freq_attr _name = \ | |
342 | __ATTR(_name, 0644, show_##_name, store_##_name) | |
343 | ||
344 | define_one_rw(sampling_rate); | |
345 | define_one_rw(sampling_down_factor); | |
346 | define_one_rw(up_threshold); | |
347 | define_one_rw(down_threshold); | |
001893cd | 348 | define_one_rw(ignore_nice_load); |
b9170836 DJ |
349 | define_one_rw(freq_step); |
350 | ||
9acef487 | 351 | static struct attribute *dbs_attributes[] = { |
b9170836 DJ |
352 | &sampling_rate_max.attr, |
353 | &sampling_rate_min.attr, | |
354 | &sampling_rate.attr, | |
355 | &sampling_down_factor.attr, | |
356 | &up_threshold.attr, | |
357 | &down_threshold.attr, | |
001893cd | 358 | &ignore_nice_load.attr, |
b9170836 DJ |
359 | &freq_step.attr, |
360 | NULL | |
361 | }; | |
362 | ||
363 | static struct attribute_group dbs_attr_group = { | |
364 | .attrs = dbs_attributes, | |
365 | .name = "conservative", | |
366 | }; | |
367 | ||
368 | /************************** sysfs end ************************/ | |
369 | ||
370 | static void dbs_check_cpu(int cpu) | |
371 | { | |
372 | unsigned int idle_ticks, up_idle_ticks, down_idle_ticks; | |
08a28e2e | 373 | unsigned int tmp_idle_ticks, total_idle_ticks; |
f068c04b | 374 | unsigned int freq_target; |
b9170836 | 375 | unsigned int freq_down_sampling_rate; |
08a28e2e | 376 | struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu); |
b9170836 | 377 | struct cpufreq_policy *policy; |
b9170836 | 378 | |
b9170836 DJ |
379 | if (!this_dbs_info->enable) |
380 | return; | |
381 | ||
08a28e2e AC |
382 | policy = this_dbs_info->cur_policy; |
383 | ||
18a7247d DJ |
384 | /* |
385 | * The default safe range is 20% to 80% | |
b9170836 | 386 | * Every sampling_rate, we check |
18a7247d DJ |
387 | * - If current idle time is less than 20%, then we try to |
388 | * increase frequency | |
b9170836 | 389 | * Every sampling_rate*sampling_down_factor, we check |
18a7247d DJ |
390 | * - If current idle time is more than 80%, then we try to |
391 | * decrease frequency | |
b9170836 | 392 | * |
18a7247d DJ |
393 | * Any frequency increase takes it to the maximum frequency. |
394 | * Frequency reduction happens at minimum steps of | |
395 | * 5% (default) of max_frequency | |
b9170836 DJ |
396 | */ |
397 | ||
398 | /* Check for frequency increase */ | |
9c7d269b | 399 | idle_ticks = UINT_MAX; |
b9170836 | 400 | |
08a28e2e AC |
401 | /* Check for frequency increase */ |
402 | total_idle_ticks = get_cpu_idle_time(cpu); | |
403 | tmp_idle_ticks = total_idle_ticks - | |
404 | this_dbs_info->prev_cpu_idle_up; | |
405 | this_dbs_info->prev_cpu_idle_up = total_idle_ticks; | |
406 | ||
407 | if (tmp_idle_ticks < idle_ticks) | |
408 | idle_ticks = tmp_idle_ticks; | |
b9170836 DJ |
409 | |
410 | /* Scale idle ticks by 100 and compare with up and down ticks */ | |
411 | idle_ticks *= 100; | |
412 | up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) * | |
2c906b31 | 413 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate); |
b9170836 DJ |
414 | |
415 | if (idle_ticks < up_idle_ticks) { | |
a159b827 | 416 | this_dbs_info->down_skip = 0; |
08a28e2e AC |
417 | this_dbs_info->prev_cpu_idle_down = |
418 | this_dbs_info->prev_cpu_idle_up; | |
790d76fa | 419 | |
b9170836 | 420 | /* if we are already at full speed then break out early */ |
a159b827 | 421 | if (this_dbs_info->requested_freq == policy->max) |
b9170836 | 422 | return; |
18a7247d | 423 | |
f068c04b | 424 | freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100; |
b9170836 DJ |
425 | |
426 | /* max freq cannot be less than 100. But who knows.... */ | |
f068c04b DJ |
427 | if (unlikely(freq_target == 0)) |
428 | freq_target = 5; | |
18a7247d | 429 | |
f068c04b | 430 | this_dbs_info->requested_freq += freq_target; |
a159b827 AC |
431 | if (this_dbs_info->requested_freq > policy->max) |
432 | this_dbs_info->requested_freq = policy->max; | |
b9170836 | 433 | |
a159b827 | 434 | __cpufreq_driver_target(policy, this_dbs_info->requested_freq, |
b9170836 | 435 | CPUFREQ_RELATION_H); |
b9170836 DJ |
436 | return; |
437 | } | |
438 | ||
439 | /* Check for frequency decrease */ | |
a159b827 AC |
440 | this_dbs_info->down_skip++; |
441 | if (this_dbs_info->down_skip < dbs_tuners_ins.sampling_down_factor) | |
b9170836 DJ |
442 | return; |
443 | ||
08a28e2e AC |
444 | /* Check for frequency decrease */ |
445 | total_idle_ticks = this_dbs_info->prev_cpu_idle_up; | |
446 | tmp_idle_ticks = total_idle_ticks - | |
447 | this_dbs_info->prev_cpu_idle_down; | |
448 | this_dbs_info->prev_cpu_idle_down = total_idle_ticks; | |
b9170836 | 449 | |
08a28e2e AC |
450 | if (tmp_idle_ticks < idle_ticks) |
451 | idle_ticks = tmp_idle_ticks; | |
b9170836 DJ |
452 | |
453 | /* Scale idle ticks by 100 and compare with up and down ticks */ | |
454 | idle_ticks *= 100; | |
a159b827 | 455 | this_dbs_info->down_skip = 0; |
b9170836 DJ |
456 | |
457 | freq_down_sampling_rate = dbs_tuners_ins.sampling_rate * | |
458 | dbs_tuners_ins.sampling_down_factor; | |
459 | down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) * | |
2c906b31 | 460 | usecs_to_jiffies(freq_down_sampling_rate); |
b9170836 | 461 | |
9c7d269b | 462 | if (idle_ticks > down_idle_ticks) { |
2c906b31 AC |
463 | /* |
464 | * if we are already at the lowest speed then break out early | |
b9170836 | 465 | * or if we 'cannot' reduce the speed as the user might want |
f068c04b | 466 | * freq_target to be zero |
2c906b31 | 467 | */ |
a159b827 | 468 | if (this_dbs_info->requested_freq == policy->min |
b9170836 DJ |
469 | || dbs_tuners_ins.freq_step == 0) |
470 | return; | |
471 | ||
f068c04b | 472 | freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100; |
b9170836 DJ |
473 | |
474 | /* max freq cannot be less than 100. But who knows.... */ | |
f068c04b DJ |
475 | if (unlikely(freq_target == 0)) |
476 | freq_target = 5; | |
b9170836 | 477 | |
f068c04b | 478 | this_dbs_info->requested_freq -= freq_target; |
a159b827 AC |
479 | if (this_dbs_info->requested_freq < policy->min) |
480 | this_dbs_info->requested_freq = policy->min; | |
b9170836 | 481 | |
a159b827 | 482 | __cpufreq_driver_target(policy, this_dbs_info->requested_freq, |
2c906b31 | 483 | CPUFREQ_RELATION_H); |
b9170836 DJ |
484 | return; |
485 | } | |
486 | } | |
487 | ||
c4028958 | 488 | static void do_dbs_timer(struct work_struct *work) |
18a7247d | 489 | { |
b9170836 | 490 | int i; |
3fc54d37 | 491 | mutex_lock(&dbs_mutex); |
b9170836 DJ |
492 | for_each_online_cpu(i) |
493 | dbs_check_cpu(i); | |
18a7247d | 494 | schedule_delayed_work(&dbs_work, |
b9170836 | 495 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
3fc54d37 | 496 | mutex_unlock(&dbs_mutex); |
18a7247d | 497 | } |
b9170836 DJ |
498 | |
499 | static inline void dbs_timer_init(void) | |
500 | { | |
8217e4f4 | 501 | init_timer_deferrable(&dbs_work.timer); |
b9170836 DJ |
502 | schedule_delayed_work(&dbs_work, |
503 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); | |
504 | return; | |
505 | } | |
506 | ||
507 | static inline void dbs_timer_exit(void) | |
508 | { | |
509 | cancel_delayed_work(&dbs_work); | |
510 | return; | |
511 | } | |
512 | ||
513 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, | |
514 | unsigned int event) | |
515 | { | |
516 | unsigned int cpu = policy->cpu; | |
517 | struct cpu_dbs_info_s *this_dbs_info; | |
518 | unsigned int j; | |
914f7c31 | 519 | int rc; |
b9170836 DJ |
520 | |
521 | this_dbs_info = &per_cpu(cpu_dbs_info, cpu); | |
522 | ||
523 | switch (event) { | |
524 | case CPUFREQ_GOV_START: | |
18a7247d | 525 | if ((!cpu_online(cpu)) || (!policy->cur)) |
b9170836 DJ |
526 | return -EINVAL; |
527 | ||
b9170836 DJ |
528 | if (this_dbs_info->enable) /* Already enabled */ |
529 | break; | |
18a7247d | 530 | |
3fc54d37 | 531 | mutex_lock(&dbs_mutex); |
914f7c31 JG |
532 | |
533 | rc = sysfs_create_group(&policy->kobj, &dbs_attr_group); | |
534 | if (rc) { | |
535 | mutex_unlock(&dbs_mutex); | |
536 | return rc; | |
537 | } | |
538 | ||
835481d9 | 539 | for_each_cpu(j, policy->cpus) { |
b9170836 DJ |
540 | struct cpu_dbs_info_s *j_dbs_info; |
541 | j_dbs_info = &per_cpu(cpu_dbs_info, j); | |
542 | j_dbs_info->cur_policy = policy; | |
18a7247d | 543 | |
08a28e2e | 544 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu); |
b9170836 DJ |
545 | j_dbs_info->prev_cpu_idle_down |
546 | = j_dbs_info->prev_cpu_idle_up; | |
547 | } | |
548 | this_dbs_info->enable = 1; | |
a159b827 AC |
549 | this_dbs_info->down_skip = 0; |
550 | this_dbs_info->requested_freq = policy->cur; | |
914f7c31 | 551 | |
b9170836 DJ |
552 | dbs_enable++; |
553 | /* | |
554 | * Start the timerschedule work, when this governor | |
555 | * is used for first time | |
556 | */ | |
557 | if (dbs_enable == 1) { | |
558 | unsigned int latency; | |
559 | /* policy latency is in nS. Convert it to uS first */ | |
2c906b31 AC |
560 | latency = policy->cpuinfo.transition_latency / 1000; |
561 | if (latency == 0) | |
562 | latency = 1; | |
b9170836 | 563 | |
112124ab TR |
564 | def_sampling_rate = |
565 | max(10 * latency * LATENCY_MULTIPLIER, | |
566 | MIN_STAT_SAMPLING_RATE); | |
2c906b31 | 567 | |
b9170836 | 568 | dbs_tuners_ins.sampling_rate = def_sampling_rate; |
b9170836 DJ |
569 | |
570 | dbs_timer_init(); | |
a8d7c3bc EO |
571 | cpufreq_register_notifier( |
572 | &dbs_cpufreq_notifier_block, | |
573 | CPUFREQ_TRANSITION_NOTIFIER); | |
b9170836 | 574 | } |
18a7247d | 575 | |
3fc54d37 | 576 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
577 | break; |
578 | ||
579 | case CPUFREQ_GOV_STOP: | |
3fc54d37 | 580 | mutex_lock(&dbs_mutex); |
b9170836 DJ |
581 | this_dbs_info->enable = 0; |
582 | sysfs_remove_group(&policy->kobj, &dbs_attr_group); | |
583 | dbs_enable--; | |
584 | /* | |
585 | * Stop the timerschedule work, when this governor | |
586 | * is used for first time | |
587 | */ | |
a8d7c3bc | 588 | if (dbs_enable == 0) { |
b9170836 | 589 | dbs_timer_exit(); |
a8d7c3bc EO |
590 | cpufreq_unregister_notifier( |
591 | &dbs_cpufreq_notifier_block, | |
592 | CPUFREQ_TRANSITION_NOTIFIER); | |
593 | } | |
594 | ||
3fc54d37 | 595 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
596 | |
597 | break; | |
598 | ||
599 | case CPUFREQ_GOV_LIMITS: | |
3fc54d37 | 600 | mutex_lock(&dbs_mutex); |
b9170836 DJ |
601 | if (policy->max < this_dbs_info->cur_policy->cur) |
602 | __cpufreq_driver_target( | |
603 | this_dbs_info->cur_policy, | |
18a7247d | 604 | policy->max, CPUFREQ_RELATION_H); |
b9170836 DJ |
605 | else if (policy->min > this_dbs_info->cur_policy->cur) |
606 | __cpufreq_driver_target( | |
607 | this_dbs_info->cur_policy, | |
18a7247d | 608 | policy->min, CPUFREQ_RELATION_L); |
3fc54d37 | 609 | mutex_unlock(&dbs_mutex); |
b9170836 DJ |
610 | break; |
611 | } | |
612 | return 0; | |
613 | } | |
614 | ||
c4d14bc0 SW |
615 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE |
616 | static | |
617 | #endif | |
1c256245 TR |
618 | struct cpufreq_governor cpufreq_gov_conservative = { |
619 | .name = "conservative", | |
620 | .governor = cpufreq_governor_dbs, | |
621 | .max_transition_latency = TRANSITION_LATENCY_LIMIT, | |
622 | .owner = THIS_MODULE, | |
b9170836 DJ |
623 | }; |
624 | ||
625 | static int __init cpufreq_gov_dbs_init(void) | |
626 | { | |
1c256245 | 627 | return cpufreq_register_governor(&cpufreq_gov_conservative); |
b9170836 DJ |
628 | } |
629 | ||
630 | static void __exit cpufreq_gov_dbs_exit(void) | |
631 | { | |
632 | /* Make sure that the scheduled work is indeed not running */ | |
633 | flush_scheduled_work(); | |
634 | ||
1c256245 | 635 | cpufreq_unregister_governor(&cpufreq_gov_conservative); |
b9170836 DJ |
636 | } |
637 | ||
638 | ||
11a80a9c | 639 | MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>"); |
9acef487 | 640 | MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for " |
b9170836 DJ |
641 | "Low Latency Frequency Transition capable processors " |
642 | "optimised for use in a battery environment"); | |
9acef487 | 643 | MODULE_LICENSE("GPL"); |
b9170836 | 644 | |
6915719b JW |
645 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE |
646 | fs_initcall(cpufreq_gov_dbs_init); | |
647 | #else | |
b9170836 | 648 | module_init(cpufreq_gov_dbs_init); |
6915719b | 649 | #endif |
b9170836 | 650 | module_exit(cpufreq_gov_dbs_exit); |