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