]>
Commit | Line | Data |
---|---|---|
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> | |
1da177e4 | 15 | #include <linux/init.h> |
1da177e4 | 16 | #include <linux/cpufreq.h> |
138a0128 | 17 | #include <linux/cpu.h> |
1da177e4 LT |
18 | #include <linux/jiffies.h> |
19 | #include <linux/kernel_stat.h> | |
3fc54d37 | 20 | #include <linux/mutex.h> |
80800913 | 21 | #include <linux/hrtimer.h> |
22 | #include <linux/tick.h> | |
23 | #include <linux/ktime.h> | |
1da177e4 LT |
24 | |
25 | /* | |
26 | * dbs is used in this file as a shortform for demandbased switching | |
27 | * It helps to keep variable names smaller, simpler | |
28 | */ | |
29 | ||
e9d95bf7 | 30 | #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10) |
1da177e4 | 31 | #define DEF_FREQUENCY_UP_THRESHOLD (80) |
80800913 | 32 | #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3) |
33 | #define MICRO_FREQUENCY_UP_THRESHOLD (95) | |
c29f1403 | 34 | #define MIN_FREQUENCY_UP_THRESHOLD (11) |
1da177e4 LT |
35 | #define MAX_FREQUENCY_UP_THRESHOLD (100) |
36 | ||
32ee8c3e DJ |
37 | /* |
38 | * The polling frequency of this governor depends on the capability of | |
1da177e4 | 39 | * the processor. Default polling frequency is 1000 times the transition |
32ee8c3e DJ |
40 | * latency of the processor. The governor will work on any processor with |
41 | * transition latency <= 10mS, using appropriate sampling | |
1da177e4 LT |
42 | * rate. |
43 | * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL) | |
44 | * this governor will not work. | |
45 | * All times here are in uS. | |
46 | */ | |
32ee8c3e | 47 | static unsigned int def_sampling_rate; |
df8b59be DJ |
48 | #define MIN_SAMPLING_RATE_RATIO (2) |
49 | /* for correct statistics, we need at least 10 ticks between each measure */ | |
e08f5f5b GS |
50 | #define MIN_STAT_SAMPLING_RATE \ |
51 | (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10)) | |
52 | #define MIN_SAMPLING_RATE \ | |
53 | (def_sampling_rate / MIN_SAMPLING_RATE_RATIO) | |
1da177e4 LT |
54 | #define MAX_SAMPLING_RATE (500 * def_sampling_rate) |
55 | #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000) | |
1c256245 | 56 | #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000) |
1da177e4 | 57 | |
c4028958 DH |
58 | static void do_dbs_timer(struct work_struct *work); |
59 | ||
60 | /* Sampling types */ | |
529af7a1 | 61 | enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE}; |
1da177e4 LT |
62 | |
63 | struct cpu_dbs_info_s { | |
ccb2fe20 VP |
64 | cputime64_t prev_cpu_idle; |
65 | cputime64_t prev_cpu_wall; | |
80800913 | 66 | cputime64_t prev_cpu_nice; |
32ee8c3e | 67 | struct cpufreq_policy *cur_policy; |
2b03f891 | 68 | struct delayed_work work; |
05ca0350 AS |
69 | struct cpufreq_frequency_table *freq_table; |
70 | unsigned int freq_lo; | |
71 | unsigned int freq_lo_jiffies; | |
72 | unsigned int freq_hi_jiffies; | |
529af7a1 VP |
73 | int cpu; |
74 | unsigned int enable:1, | |
2b03f891 | 75 | sample_type:1; |
1da177e4 LT |
76 | }; |
77 | static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info); | |
78 | ||
79 | static unsigned int dbs_enable; /* number of CPUs using this policy */ | |
80 | ||
4ec223d0 VP |
81 | /* |
82 | * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug | |
83 | * lock and dbs_mutex. cpu_hotplug lock should always be held before | |
84 | * dbs_mutex. If any function that can potentially take cpu_hotplug lock | |
85 | * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then | |
86 | * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock | |
87 | * is recursive for the same process. -Venki | |
88 | */ | |
ffac80e9 | 89 | static DEFINE_MUTEX(dbs_mutex); |
1da177e4 | 90 | |
2f8a835c | 91 | static struct workqueue_struct *kondemand_wq; |
6810b548 | 92 | |
05ca0350 | 93 | static struct dbs_tuners { |
32ee8c3e | 94 | unsigned int sampling_rate; |
32ee8c3e | 95 | unsigned int up_threshold; |
e9d95bf7 | 96 | unsigned int down_differential; |
32ee8c3e | 97 | unsigned int ignore_nice; |
05ca0350 AS |
98 | unsigned int powersave_bias; |
99 | } dbs_tuners_ins = { | |
32ee8c3e | 100 | .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, |
e9d95bf7 | 101 | .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL, |
9cbad61b | 102 | .ignore_nice = 0, |
05ca0350 | 103 | .powersave_bias = 0, |
1da177e4 LT |
104 | }; |
105 | ||
80800913 | 106 | static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu, |
107 | cputime64_t *wall) | |
dac1c1a5 | 108 | { |
ea487615 | 109 | cputime64_t idle_time; |
3430502d | 110 | cputime64_t cur_wall_time; |
ea487615 | 111 | cputime64_t busy_time; |
ccb2fe20 | 112 | |
3430502d | 113 | cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); |
ea487615 VP |
114 | busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user, |
115 | kstat_cpu(cpu).cpustat.system); | |
ccb2fe20 | 116 | |
ea487615 VP |
117 | busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq); |
118 | busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq); | |
119 | busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal); | |
1ca3abdb | 120 | busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice); |
ea487615 | 121 | |
3430502d | 122 | idle_time = cputime64_sub(cur_wall_time, busy_time); |
123 | if (wall) | |
124 | *wall = cur_wall_time; | |
125 | ||
ea487615 | 126 | return idle_time; |
dac1c1a5 DJ |
127 | } |
128 | ||
80800913 | 129 | static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall) |
130 | { | |
131 | u64 idle_time = get_cpu_idle_time_us(cpu, wall); | |
132 | ||
133 | if (idle_time == -1ULL) | |
134 | return get_cpu_idle_time_jiffy(cpu, wall); | |
135 | ||
80800913 | 136 | return idle_time; |
137 | } | |
138 | ||
05ca0350 AS |
139 | /* |
140 | * Find right freq to be set now with powersave_bias on. | |
141 | * Returns the freq_hi to be used right now and will set freq_hi_jiffies, | |
142 | * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs. | |
143 | */ | |
b5ecf60f AB |
144 | static unsigned int powersave_bias_target(struct cpufreq_policy *policy, |
145 | unsigned int freq_next, | |
146 | unsigned int relation) | |
05ca0350 AS |
147 | { |
148 | unsigned int freq_req, freq_reduc, freq_avg; | |
149 | unsigned int freq_hi, freq_lo; | |
150 | unsigned int index = 0; | |
151 | unsigned int jiffies_total, jiffies_hi, jiffies_lo; | |
152 | struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu); | |
153 | ||
154 | if (!dbs_info->freq_table) { | |
155 | dbs_info->freq_lo = 0; | |
156 | dbs_info->freq_lo_jiffies = 0; | |
157 | return freq_next; | |
158 | } | |
159 | ||
160 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next, | |
161 | relation, &index); | |
162 | freq_req = dbs_info->freq_table[index].frequency; | |
163 | freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000; | |
164 | freq_avg = freq_req - freq_reduc; | |
165 | ||
166 | /* Find freq bounds for freq_avg in freq_table */ | |
167 | index = 0; | |
168 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, | |
169 | CPUFREQ_RELATION_H, &index); | |
170 | freq_lo = dbs_info->freq_table[index].frequency; | |
171 | index = 0; | |
172 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, | |
173 | CPUFREQ_RELATION_L, &index); | |
174 | freq_hi = dbs_info->freq_table[index].frequency; | |
175 | ||
176 | /* Find out how long we have to be in hi and lo freqs */ | |
177 | if (freq_hi == freq_lo) { | |
178 | dbs_info->freq_lo = 0; | |
179 | dbs_info->freq_lo_jiffies = 0; | |
180 | return freq_lo; | |
181 | } | |
182 | jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate); | |
183 | jiffies_hi = (freq_avg - freq_lo) * jiffies_total; | |
184 | jiffies_hi += ((freq_hi - freq_lo) / 2); | |
185 | jiffies_hi /= (freq_hi - freq_lo); | |
186 | jiffies_lo = jiffies_total - jiffies_hi; | |
187 | dbs_info->freq_lo = freq_lo; | |
188 | dbs_info->freq_lo_jiffies = jiffies_lo; | |
189 | dbs_info->freq_hi_jiffies = jiffies_hi; | |
190 | return freq_hi; | |
191 | } | |
192 | ||
193 | static void ondemand_powersave_bias_init(void) | |
194 | { | |
195 | int i; | |
196 | for_each_online_cpu(i) { | |
197 | struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i); | |
198 | dbs_info->freq_table = cpufreq_frequency_get_table(i); | |
199 | dbs_info->freq_lo = 0; | |
200 | } | |
201 | } | |
202 | ||
1da177e4 LT |
203 | /************************** sysfs interface ************************/ |
204 | static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf) | |
205 | { | |
2b03f891 | 206 | return sprintf(buf, "%u\n", MAX_SAMPLING_RATE); |
1da177e4 LT |
207 | } |
208 | ||
209 | static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf) | |
210 | { | |
2b03f891 | 211 | return sprintf(buf, "%u\n", MIN_SAMPLING_RATE); |
1da177e4 LT |
212 | } |
213 | ||
32ee8c3e DJ |
214 | #define define_one_ro(_name) \ |
215 | static struct freq_attr _name = \ | |
1da177e4 LT |
216 | __ATTR(_name, 0444, show_##_name, NULL) |
217 | ||
218 | define_one_ro(sampling_rate_max); | |
219 | define_one_ro(sampling_rate_min); | |
220 | ||
221 | /* cpufreq_ondemand Governor Tunables */ | |
222 | #define show_one(file_name, object) \ | |
223 | static ssize_t show_##file_name \ | |
224 | (struct cpufreq_policy *unused, char *buf) \ | |
225 | { \ | |
226 | return sprintf(buf, "%u\n", dbs_tuners_ins.object); \ | |
227 | } | |
228 | show_one(sampling_rate, sampling_rate); | |
1da177e4 | 229 | show_one(up_threshold, up_threshold); |
001893cd | 230 | show_one(ignore_nice_load, ignore_nice); |
05ca0350 | 231 | show_one(powersave_bias, powersave_bias); |
1da177e4 | 232 | |
32ee8c3e | 233 | static ssize_t store_sampling_rate(struct cpufreq_policy *unused, |
1da177e4 LT |
234 | const char *buf, size_t count) |
235 | { | |
236 | unsigned int input; | |
237 | int ret; | |
ffac80e9 | 238 | ret = sscanf(buf, "%u", &input); |
1da177e4 | 239 | |
3fc54d37 | 240 | mutex_lock(&dbs_mutex); |
e08f5f5b GS |
241 | if (ret != 1 || input > MAX_SAMPLING_RATE |
242 | || input < MIN_SAMPLING_RATE) { | |
3fc54d37 | 243 | mutex_unlock(&dbs_mutex); |
1da177e4 LT |
244 | return -EINVAL; |
245 | } | |
246 | ||
247 | dbs_tuners_ins.sampling_rate = input; | |
3fc54d37 | 248 | mutex_unlock(&dbs_mutex); |
1da177e4 LT |
249 | |
250 | return count; | |
251 | } | |
252 | ||
32ee8c3e | 253 | static ssize_t store_up_threshold(struct cpufreq_policy *unused, |
1da177e4 LT |
254 | const char *buf, size_t count) |
255 | { | |
256 | unsigned int input; | |
257 | int ret; | |
ffac80e9 | 258 | ret = sscanf(buf, "%u", &input); |
1da177e4 | 259 | |
3fc54d37 | 260 | mutex_lock(&dbs_mutex); |
32ee8c3e | 261 | if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || |
c29f1403 | 262 | input < MIN_FREQUENCY_UP_THRESHOLD) { |
3fc54d37 | 263 | mutex_unlock(&dbs_mutex); |
1da177e4 LT |
264 | return -EINVAL; |
265 | } | |
266 | ||
267 | dbs_tuners_ins.up_threshold = input; | |
3fc54d37 | 268 | mutex_unlock(&dbs_mutex); |
1da177e4 LT |
269 | |
270 | return count; | |
271 | } | |
272 | ||
001893cd | 273 | static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy, |
3d5ee9e5 DJ |
274 | const char *buf, size_t count) |
275 | { | |
276 | unsigned int input; | |
277 | int ret; | |
278 | ||
279 | unsigned int j; | |
32ee8c3e | 280 | |
ffac80e9 | 281 | ret = sscanf(buf, "%u", &input); |
2b03f891 | 282 | if (ret != 1) |
3d5ee9e5 DJ |
283 | return -EINVAL; |
284 | ||
2b03f891 | 285 | if (input > 1) |
3d5ee9e5 | 286 | input = 1; |
32ee8c3e | 287 | |
3fc54d37 | 288 | mutex_lock(&dbs_mutex); |
2b03f891 | 289 | if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */ |
3fc54d37 | 290 | mutex_unlock(&dbs_mutex); |
3d5ee9e5 DJ |
291 | return count; |
292 | } | |
293 | dbs_tuners_ins.ignore_nice = input; | |
294 | ||
ccb2fe20 | 295 | /* we need to re-evaluate prev_cpu_idle */ |
dac1c1a5 | 296 | for_each_online_cpu(j) { |
ccb2fe20 VP |
297 | struct cpu_dbs_info_s *dbs_info; |
298 | dbs_info = &per_cpu(cpu_dbs_info, j); | |
3430502d | 299 | dbs_info->prev_cpu_idle = get_cpu_idle_time(j, |
300 | &dbs_info->prev_cpu_wall); | |
1ca3abdb VP |
301 | if (dbs_tuners_ins.ignore_nice) |
302 | dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice; | |
303 | ||
3d5ee9e5 | 304 | } |
3fc54d37 | 305 | mutex_unlock(&dbs_mutex); |
3d5ee9e5 DJ |
306 | |
307 | return count; | |
308 | } | |
309 | ||
05ca0350 AS |
310 | static ssize_t store_powersave_bias(struct cpufreq_policy *unused, |
311 | const char *buf, size_t count) | |
312 | { | |
313 | unsigned int input; | |
314 | int ret; | |
315 | ret = sscanf(buf, "%u", &input); | |
316 | ||
317 | if (ret != 1) | |
318 | return -EINVAL; | |
319 | ||
320 | if (input > 1000) | |
321 | input = 1000; | |
322 | ||
323 | mutex_lock(&dbs_mutex); | |
324 | dbs_tuners_ins.powersave_bias = input; | |
325 | ondemand_powersave_bias_init(); | |
326 | mutex_unlock(&dbs_mutex); | |
327 | ||
328 | return count; | |
329 | } | |
330 | ||
1da177e4 LT |
331 | #define define_one_rw(_name) \ |
332 | static struct freq_attr _name = \ | |
333 | __ATTR(_name, 0644, show_##_name, store_##_name) | |
334 | ||
335 | define_one_rw(sampling_rate); | |
1da177e4 | 336 | define_one_rw(up_threshold); |
001893cd | 337 | define_one_rw(ignore_nice_load); |
05ca0350 | 338 | define_one_rw(powersave_bias); |
1da177e4 | 339 | |
2b03f891 | 340 | static struct attribute *dbs_attributes[] = { |
1da177e4 LT |
341 | &sampling_rate_max.attr, |
342 | &sampling_rate_min.attr, | |
343 | &sampling_rate.attr, | |
1da177e4 | 344 | &up_threshold.attr, |
001893cd | 345 | &ignore_nice_load.attr, |
05ca0350 | 346 | &powersave_bias.attr, |
1da177e4 LT |
347 | NULL |
348 | }; | |
349 | ||
350 | static struct attribute_group dbs_attr_group = { | |
351 | .attrs = dbs_attributes, | |
352 | .name = "ondemand", | |
353 | }; | |
354 | ||
355 | /************************** sysfs end ************************/ | |
356 | ||
2f8a835c | 357 | static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info) |
1da177e4 | 358 | { |
c43aa3bd | 359 | unsigned int max_load_freq; |
1da177e4 LT |
360 | |
361 | struct cpufreq_policy *policy; | |
362 | unsigned int j; | |
363 | ||
1da177e4 LT |
364 | if (!this_dbs_info->enable) |
365 | return; | |
366 | ||
05ca0350 | 367 | this_dbs_info->freq_lo = 0; |
1da177e4 | 368 | policy = this_dbs_info->cur_policy; |
ea487615 | 369 | |
32ee8c3e | 370 | /* |
c29f1403 DJ |
371 | * Every sampling_rate, we check, if current idle time is less |
372 | * than 20% (default), then we try to increase frequency | |
ccb2fe20 | 373 | * Every sampling_rate, we look for a the lowest |
c29f1403 DJ |
374 | * frequency which can sustain the load while keeping idle time over |
375 | * 30%. If such a frequency exist, we try to decrease to this frequency. | |
1da177e4 | 376 | * |
32ee8c3e DJ |
377 | * Any frequency increase takes it to the maximum frequency. |
378 | * Frequency reduction happens at minimum steps of | |
379 | * 5% (default) of current frequency | |
1da177e4 LT |
380 | */ |
381 | ||
c43aa3bd | 382 | /* Get Absolute Load - in terms of freq */ |
383 | max_load_freq = 0; | |
384 | ||
835481d9 | 385 | for_each_cpu(j, policy->cpus) { |
1da177e4 | 386 | struct cpu_dbs_info_s *j_dbs_info; |
c43aa3bd | 387 | cputime64_t cur_wall_time, cur_idle_time; |
388 | unsigned int idle_time, wall_time; | |
389 | unsigned int load, load_freq; | |
390 | int freq_avg; | |
1da177e4 | 391 | |
1da177e4 | 392 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
3430502d | 393 | |
394 | cur_idle_time = get_cpu_idle_time(j, &cur_wall_time); | |
395 | ||
c43aa3bd | 396 | wall_time = (unsigned int) cputime64_sub(cur_wall_time, |
397 | j_dbs_info->prev_cpu_wall); | |
398 | j_dbs_info->prev_cpu_wall = cur_wall_time; | |
399 | ||
c43aa3bd | 400 | idle_time = (unsigned int) cputime64_sub(cur_idle_time, |
ccb2fe20 | 401 | j_dbs_info->prev_cpu_idle); |
c43aa3bd | 402 | j_dbs_info->prev_cpu_idle = cur_idle_time; |
1da177e4 | 403 | |
1ca3abdb VP |
404 | if (dbs_tuners_ins.ignore_nice) { |
405 | cputime64_t cur_nice; | |
406 | unsigned long cur_nice_jiffies; | |
407 | ||
408 | cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice, | |
409 | j_dbs_info->prev_cpu_nice); | |
410 | /* | |
411 | * Assumption: nice time between sampling periods will | |
412 | * be less than 2^32 jiffies for 32 bit sys | |
413 | */ | |
414 | cur_nice_jiffies = (unsigned long) | |
415 | cputime64_to_jiffies64(cur_nice); | |
416 | ||
417 | j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice; | |
418 | idle_time += jiffies_to_usecs(cur_nice_jiffies); | |
419 | } | |
420 | ||
3430502d | 421 | if (unlikely(!wall_time || wall_time < idle_time)) |
c43aa3bd | 422 | continue; |
c43aa3bd | 423 | |
424 | load = 100 * (wall_time - idle_time) / wall_time; | |
425 | ||
426 | freq_avg = __cpufreq_driver_getavg(policy, j); | |
427 | if (freq_avg <= 0) | |
428 | freq_avg = policy->cur; | |
429 | ||
430 | load_freq = load * freq_avg; | |
431 | if (load_freq > max_load_freq) | |
432 | max_load_freq = load_freq; | |
1da177e4 LT |
433 | } |
434 | ||
ccb2fe20 | 435 | /* Check for frequency increase */ |
c43aa3bd | 436 | if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) { |
c11420a6 | 437 | /* if we are already at full speed then break out early */ |
05ca0350 AS |
438 | if (!dbs_tuners_ins.powersave_bias) { |
439 | if (policy->cur == policy->max) | |
440 | return; | |
441 | ||
442 | __cpufreq_driver_target(policy, policy->max, | |
443 | CPUFREQ_RELATION_H); | |
444 | } else { | |
445 | int freq = powersave_bias_target(policy, policy->max, | |
446 | CPUFREQ_RELATION_H); | |
447 | __cpufreq_driver_target(policy, freq, | |
448 | CPUFREQ_RELATION_L); | |
449 | } | |
1da177e4 LT |
450 | return; |
451 | } | |
452 | ||
453 | /* Check for frequency decrease */ | |
c29f1403 DJ |
454 | /* if we cannot reduce the frequency anymore, break out early */ |
455 | if (policy->cur == policy->min) | |
456 | return; | |
1da177e4 | 457 | |
c29f1403 DJ |
458 | /* |
459 | * The optimal frequency is the frequency that is the lowest that | |
460 | * can support the current CPU usage without triggering the up | |
461 | * policy. To be safe, we focus 10 points under the threshold. | |
462 | */ | |
e9d95bf7 | 463 | if (max_load_freq < |
464 | (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) * | |
465 | policy->cur) { | |
c43aa3bd | 466 | unsigned int freq_next; |
e9d95bf7 | 467 | freq_next = max_load_freq / |
468 | (dbs_tuners_ins.up_threshold - | |
469 | dbs_tuners_ins.down_differential); | |
dfde5d62 | 470 | |
05ca0350 AS |
471 | if (!dbs_tuners_ins.powersave_bias) { |
472 | __cpufreq_driver_target(policy, freq_next, | |
473 | CPUFREQ_RELATION_L); | |
474 | } else { | |
475 | int freq = powersave_bias_target(policy, freq_next, | |
476 | CPUFREQ_RELATION_L); | |
477 | __cpufreq_driver_target(policy, freq, | |
478 | CPUFREQ_RELATION_L); | |
479 | } | |
ccb2fe20 | 480 | } |
1da177e4 LT |
481 | } |
482 | ||
c4028958 | 483 | static void do_dbs_timer(struct work_struct *work) |
32ee8c3e | 484 | { |
529af7a1 VP |
485 | struct cpu_dbs_info_s *dbs_info = |
486 | container_of(work, struct cpu_dbs_info_s, work.work); | |
487 | unsigned int cpu = dbs_info->cpu; | |
488 | int sample_type = dbs_info->sample_type; | |
489 | ||
1ce28d6b AS |
490 | /* We want all CPUs to do sampling nearly on same jiffy */ |
491 | int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate); | |
c4028958 | 492 | |
1ce28d6b | 493 | delay -= jiffies % delay; |
2f8a835c | 494 | |
56463b78 | 495 | if (lock_policy_rwsem_write(cpu) < 0) |
2cd7cbdf | 496 | return; |
56463b78 VP |
497 | |
498 | if (!dbs_info->enable) { | |
499 | unlock_policy_rwsem_write(cpu); | |
500 | return; | |
501 | } | |
502 | ||
05ca0350 | 503 | /* Common NORMAL_SAMPLE setup */ |
c4028958 | 504 | dbs_info->sample_type = DBS_NORMAL_SAMPLE; |
05ca0350 | 505 | if (!dbs_tuners_ins.powersave_bias || |
c4028958 | 506 | sample_type == DBS_NORMAL_SAMPLE) { |
05ca0350 | 507 | dbs_check_cpu(dbs_info); |
05ca0350 AS |
508 | if (dbs_info->freq_lo) { |
509 | /* Setup timer for SUB_SAMPLE */ | |
c4028958 | 510 | dbs_info->sample_type = DBS_SUB_SAMPLE; |
05ca0350 AS |
511 | delay = dbs_info->freq_hi_jiffies; |
512 | } | |
513 | } else { | |
514 | __cpufreq_driver_target(dbs_info->cur_policy, | |
2b03f891 | 515 | dbs_info->freq_lo, CPUFREQ_RELATION_H); |
05ca0350 | 516 | } |
1ce28d6b | 517 | queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay); |
56463b78 | 518 | unlock_policy_rwsem_write(cpu); |
32ee8c3e | 519 | } |
1da177e4 | 520 | |
529af7a1 | 521 | static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info) |
1da177e4 | 522 | { |
1ce28d6b AS |
523 | /* We want all CPUs to do sampling nearly on same jiffy */ |
524 | int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate); | |
525 | delay -= jiffies % delay; | |
2f8a835c | 526 | |
c18a1483 | 527 | dbs_info->enable = 1; |
05ca0350 | 528 | ondemand_powersave_bias_init(); |
c4028958 | 529 | dbs_info->sample_type = DBS_NORMAL_SAMPLE; |
28287033 | 530 | INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer); |
529af7a1 | 531 | queue_delayed_work_on(dbs_info->cpu, kondemand_wq, &dbs_info->work, |
2b03f891 | 532 | delay); |
1da177e4 LT |
533 | } |
534 | ||
2cd7cbdf | 535 | static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info) |
1da177e4 | 536 | { |
2cd7cbdf LT |
537 | dbs_info->enable = 0; |
538 | cancel_delayed_work(&dbs_info->work); | |
1da177e4 LT |
539 | } |
540 | ||
541 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, | |
542 | unsigned int event) | |
543 | { | |
544 | unsigned int cpu = policy->cpu; | |
545 | struct cpu_dbs_info_s *this_dbs_info; | |
546 | unsigned int j; | |
914f7c31 | 547 | int rc; |
1da177e4 LT |
548 | |
549 | this_dbs_info = &per_cpu(cpu_dbs_info, cpu); | |
550 | ||
551 | switch (event) { | |
552 | case CPUFREQ_GOV_START: | |
ffac80e9 | 553 | if ((!cpu_online(cpu)) || (!policy->cur)) |
1da177e4 LT |
554 | return -EINVAL; |
555 | ||
1da177e4 LT |
556 | if (this_dbs_info->enable) /* Already enabled */ |
557 | break; | |
32ee8c3e | 558 | |
3fc54d37 | 559 | mutex_lock(&dbs_mutex); |
2f8a835c | 560 | dbs_enable++; |
914f7c31 JG |
561 | |
562 | rc = sysfs_create_group(&policy->kobj, &dbs_attr_group); | |
563 | if (rc) { | |
914f7c31 JG |
564 | dbs_enable--; |
565 | mutex_unlock(&dbs_mutex); | |
566 | return rc; | |
567 | } | |
568 | ||
835481d9 | 569 | for_each_cpu(j, policy->cpus) { |
1da177e4 LT |
570 | struct cpu_dbs_info_s *j_dbs_info; |
571 | j_dbs_info = &per_cpu(cpu_dbs_info, j); | |
572 | j_dbs_info->cur_policy = policy; | |
32ee8c3e | 573 | |
3430502d | 574 | j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j, |
575 | &j_dbs_info->prev_cpu_wall); | |
1ca3abdb VP |
576 | if (dbs_tuners_ins.ignore_nice) { |
577 | j_dbs_info->prev_cpu_nice = | |
578 | kstat_cpu(j).cpustat.nice; | |
579 | } | |
1da177e4 | 580 | } |
529af7a1 | 581 | this_dbs_info->cpu = cpu; |
1da177e4 LT |
582 | /* |
583 | * Start the timerschedule work, when this governor | |
584 | * is used for first time | |
585 | */ | |
586 | if (dbs_enable == 1) { | |
587 | unsigned int latency; | |
588 | /* policy latency is in nS. Convert it to uS first */ | |
df8b59be DJ |
589 | latency = policy->cpuinfo.transition_latency / 1000; |
590 | if (latency == 0) | |
591 | latency = 1; | |
1da177e4 | 592 | |
df8b59be | 593 | def_sampling_rate = latency * |
1da177e4 | 594 | DEF_SAMPLING_RATE_LATENCY_MULTIPLIER; |
df8b59be DJ |
595 | |
596 | if (def_sampling_rate < MIN_STAT_SAMPLING_RATE) | |
597 | def_sampling_rate = MIN_STAT_SAMPLING_RATE; | |
598 | ||
1da177e4 | 599 | dbs_tuners_ins.sampling_rate = def_sampling_rate; |
1da177e4 | 600 | } |
529af7a1 | 601 | dbs_timer_init(this_dbs_info); |
32ee8c3e | 602 | |
3fc54d37 | 603 | mutex_unlock(&dbs_mutex); |
1da177e4 LT |
604 | break; |
605 | ||
606 | case CPUFREQ_GOV_STOP: | |
3fc54d37 | 607 | mutex_lock(&dbs_mutex); |
2cd7cbdf | 608 | dbs_timer_exit(this_dbs_info); |
1da177e4 LT |
609 | sysfs_remove_group(&policy->kobj, &dbs_attr_group); |
610 | dbs_enable--; | |
3fc54d37 | 611 | mutex_unlock(&dbs_mutex); |
1da177e4 LT |
612 | |
613 | break; | |
614 | ||
615 | case CPUFREQ_GOV_LIMITS: | |
3fc54d37 | 616 | mutex_lock(&dbs_mutex); |
1da177e4 | 617 | if (policy->max < this_dbs_info->cur_policy->cur) |
ffac80e9 | 618 | __cpufreq_driver_target(this_dbs_info->cur_policy, |
2b03f891 | 619 | policy->max, CPUFREQ_RELATION_H); |
1da177e4 | 620 | else if (policy->min > this_dbs_info->cur_policy->cur) |
ffac80e9 | 621 | __cpufreq_driver_target(this_dbs_info->cur_policy, |
2b03f891 | 622 | policy->min, CPUFREQ_RELATION_L); |
3fc54d37 | 623 | mutex_unlock(&dbs_mutex); |
1da177e4 LT |
624 | break; |
625 | } | |
626 | return 0; | |
627 | } | |
628 | ||
c4d14bc0 SW |
629 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND |
630 | static | |
631 | #endif | |
1c256245 TR |
632 | struct cpufreq_governor cpufreq_gov_ondemand = { |
633 | .name = "ondemand", | |
634 | .governor = cpufreq_governor_dbs, | |
635 | .max_transition_latency = TRANSITION_LATENCY_LIMIT, | |
636 | .owner = THIS_MODULE, | |
1da177e4 | 637 | }; |
1da177e4 LT |
638 | |
639 | static int __init cpufreq_gov_dbs_init(void) | |
640 | { | |
888a794c | 641 | int err; |
80800913 | 642 | cputime64_t wall; |
4f6e6b9f AR |
643 | u64 idle_time; |
644 | int cpu = get_cpu(); | |
80800913 | 645 | |
4f6e6b9f AR |
646 | idle_time = get_cpu_idle_time_us(cpu, &wall); |
647 | put_cpu(); | |
80800913 | 648 | if (idle_time != -1ULL) { |
649 | /* Idle micro accounting is supported. Use finer thresholds */ | |
650 | dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD; | |
651 | dbs_tuners_ins.down_differential = | |
652 | MICRO_FREQUENCY_DOWN_DIFFERENTIAL; | |
653 | } | |
888a794c | 654 | |
56463b78 VP |
655 | kondemand_wq = create_workqueue("kondemand"); |
656 | if (!kondemand_wq) { | |
657 | printk(KERN_ERR "Creation of kondemand failed\n"); | |
658 | return -EFAULT; | |
659 | } | |
888a794c AM |
660 | err = cpufreq_register_governor(&cpufreq_gov_ondemand); |
661 | if (err) | |
662 | destroy_workqueue(kondemand_wq); | |
663 | ||
664 | return err; | |
1da177e4 LT |
665 | } |
666 | ||
667 | static void __exit cpufreq_gov_dbs_exit(void) | |
668 | { | |
1c256245 | 669 | cpufreq_unregister_governor(&cpufreq_gov_ondemand); |
56463b78 | 670 | destroy_workqueue(kondemand_wq); |
1da177e4 LT |
671 | } |
672 | ||
673 | ||
ffac80e9 VP |
674 | MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>"); |
675 | MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>"); | |
676 | MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for " | |
2b03f891 | 677 | "Low Latency Frequency Transition capable processors"); |
ffac80e9 | 678 | MODULE_LICENSE("GPL"); |
1da177e4 | 679 | |
6915719b JW |
680 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND |
681 | fs_initcall(cpufreq_gov_dbs_init); | |
682 | #else | |
1da177e4 | 683 | module_init(cpufreq_gov_dbs_init); |
6915719b | 684 | #endif |
1da177e4 | 685 | module_exit(cpufreq_gov_dbs_exit); |