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