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