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