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> | |
15 | #include <linux/smp.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/interrupt.h> | |
18 | #include <linux/ctype.h> | |
19 | #include <linux/cpufreq.h> | |
20 | #include <linux/sysctl.h> | |
21 | #include <linux/types.h> | |
22 | #include <linux/fs.h> | |
23 | #include <linux/sysfs.h> | |
24 | #include <linux/sched.h> | |
25 | #include <linux/kmod.h> | |
26 | #include <linux/workqueue.h> | |
27 | #include <linux/jiffies.h> | |
28 | #include <linux/kernel_stat.h> | |
29 | #include <linux/percpu.h> | |
30 | ||
31 | /* | |
32 | * dbs is used in this file as a shortform for demandbased switching | |
33 | * It helps to keep variable names smaller, simpler | |
34 | */ | |
35 | ||
36 | #define DEF_FREQUENCY_UP_THRESHOLD (80) | |
c29f1403 | 37 | #define MIN_FREQUENCY_UP_THRESHOLD (11) |
1da177e4 LT |
38 | #define MAX_FREQUENCY_UP_THRESHOLD (100) |
39 | ||
1da177e4 LT |
40 | /* |
41 | * The polling frequency of this governor depends on the capability of | |
42 | * the processor. Default polling frequency is 1000 times the transition | |
43 | * latency of the processor. The governor will work on any processor with | |
44 | * transition latency <= 10mS, using appropriate sampling | |
45 | * rate. | |
46 | * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL) | |
47 | * this governor will not work. | |
48 | * All times here are in uS. | |
49 | */ | |
50 | static unsigned int def_sampling_rate; | |
51 | #define MIN_SAMPLING_RATE (def_sampling_rate / 2) | |
52 | #define MAX_SAMPLING_RATE (500 * def_sampling_rate) | |
53 | #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000) | |
54 | #define DEF_SAMPLING_DOWN_FACTOR (10) | |
55 | #define TRANSITION_LATENCY_LIMIT (10 * 1000) | |
1da177e4 LT |
56 | |
57 | static void do_dbs_timer(void *data); | |
58 | ||
59 | struct cpu_dbs_info_s { | |
60 | struct cpufreq_policy *cur_policy; | |
61 | unsigned int prev_cpu_idle_up; | |
62 | unsigned int prev_cpu_idle_down; | |
63 | unsigned int enable; | |
64 | }; | |
65 | static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info); | |
66 | ||
67 | static unsigned int dbs_enable; /* number of CPUs using this policy */ | |
68 | ||
69 | static DECLARE_MUTEX (dbs_sem); | |
70 | static DECLARE_WORK (dbs_work, do_dbs_timer, NULL); | |
71 | ||
72 | struct dbs_tuners { | |
73 | unsigned int sampling_rate; | |
74 | unsigned int sampling_down_factor; | |
75 | unsigned int up_threshold; | |
3d5ee9e5 | 76 | unsigned int ignore_nice; |
1da177e4 LT |
77 | }; |
78 | ||
79 | static struct dbs_tuners dbs_tuners_ins = { | |
80 | .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, | |
1da177e4 LT |
81 | .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR, |
82 | }; | |
83 | ||
dac1c1a5 DJ |
84 | static inline unsigned int get_cpu_idle_time(unsigned int cpu) |
85 | { | |
86 | return kstat_cpu(cpu).cpustat.idle + | |
87 | kstat_cpu(cpu).cpustat.iowait + | |
88 | ( !dbs_tuners_ins.ignore_nice ? | |
89 | kstat_cpu(cpu).cpustat.nice : | |
90 | 0); | |
91 | } | |
92 | ||
1da177e4 LT |
93 | /************************** sysfs interface ************************/ |
94 | static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf) | |
95 | { | |
96 | return sprintf (buf, "%u\n", MAX_SAMPLING_RATE); | |
97 | } | |
98 | ||
99 | static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf) | |
100 | { | |
101 | return sprintf (buf, "%u\n", MIN_SAMPLING_RATE); | |
102 | } | |
103 | ||
104 | #define define_one_ro(_name) \ | |
105 | static struct freq_attr _name = \ | |
106 | __ATTR(_name, 0444, show_##_name, NULL) | |
107 | ||
108 | define_one_ro(sampling_rate_max); | |
109 | define_one_ro(sampling_rate_min); | |
110 | ||
111 | /* cpufreq_ondemand Governor Tunables */ | |
112 | #define show_one(file_name, object) \ | |
113 | static ssize_t show_##file_name \ | |
114 | (struct cpufreq_policy *unused, char *buf) \ | |
115 | { \ | |
116 | return sprintf(buf, "%u\n", dbs_tuners_ins.object); \ | |
117 | } | |
118 | show_one(sampling_rate, sampling_rate); | |
119 | show_one(sampling_down_factor, sampling_down_factor); | |
120 | show_one(up_threshold, up_threshold); | |
3d5ee9e5 | 121 | show_one(ignore_nice, ignore_nice); |
1da177e4 LT |
122 | |
123 | static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused, | |
124 | const char *buf, size_t count) | |
125 | { | |
126 | unsigned int input; | |
127 | int ret; | |
128 | ret = sscanf (buf, "%u", &input); | |
129 | if (ret != 1 ) | |
130 | return -EINVAL; | |
131 | ||
132 | down(&dbs_sem); | |
133 | dbs_tuners_ins.sampling_down_factor = input; | |
134 | up(&dbs_sem); | |
135 | ||
136 | return count; | |
137 | } | |
138 | ||
139 | static ssize_t store_sampling_rate(struct cpufreq_policy *unused, | |
140 | const char *buf, size_t count) | |
141 | { | |
142 | unsigned int input; | |
143 | int ret; | |
144 | ret = sscanf (buf, "%u", &input); | |
145 | ||
146 | down(&dbs_sem); | |
147 | if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) { | |
148 | up(&dbs_sem); | |
149 | return -EINVAL; | |
150 | } | |
151 | ||
152 | dbs_tuners_ins.sampling_rate = input; | |
153 | up(&dbs_sem); | |
154 | ||
155 | return count; | |
156 | } | |
157 | ||
158 | static ssize_t store_up_threshold(struct cpufreq_policy *unused, | |
159 | const char *buf, size_t count) | |
160 | { | |
161 | unsigned int input; | |
162 | int ret; | |
163 | ret = sscanf (buf, "%u", &input); | |
164 | ||
165 | down(&dbs_sem); | |
166 | if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || | |
c29f1403 | 167 | input < MIN_FREQUENCY_UP_THRESHOLD) { |
1da177e4 LT |
168 | up(&dbs_sem); |
169 | return -EINVAL; | |
170 | } | |
171 | ||
172 | dbs_tuners_ins.up_threshold = input; | |
173 | up(&dbs_sem); | |
174 | ||
175 | return count; | |
176 | } | |
177 | ||
3d5ee9e5 DJ |
178 | static ssize_t store_ignore_nice(struct cpufreq_policy *policy, |
179 | const char *buf, size_t count) | |
180 | { | |
181 | unsigned int input; | |
182 | int ret; | |
183 | ||
184 | unsigned int j; | |
185 | ||
186 | ret = sscanf (buf, "%u", &input); | |
187 | if ( ret != 1 ) | |
188 | return -EINVAL; | |
189 | ||
190 | if ( input > 1 ) | |
191 | input = 1; | |
192 | ||
193 | down(&dbs_sem); | |
194 | if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */ | |
195 | up(&dbs_sem); | |
196 | return count; | |
197 | } | |
198 | dbs_tuners_ins.ignore_nice = input; | |
199 | ||
200 | /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */ | |
dac1c1a5 | 201 | for_each_online_cpu(j) { |
3d5ee9e5 DJ |
202 | struct cpu_dbs_info_s *j_dbs_info; |
203 | j_dbs_info = &per_cpu(cpu_dbs_info, j); | |
dac1c1a5 | 204 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j); |
3d5ee9e5 DJ |
205 | j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up; |
206 | } | |
207 | up(&dbs_sem); | |
208 | ||
209 | return count; | |
210 | } | |
211 | ||
1da177e4 LT |
212 | #define define_one_rw(_name) \ |
213 | static struct freq_attr _name = \ | |
214 | __ATTR(_name, 0644, show_##_name, store_##_name) | |
215 | ||
216 | define_one_rw(sampling_rate); | |
217 | define_one_rw(sampling_down_factor); | |
218 | define_one_rw(up_threshold); | |
3d5ee9e5 | 219 | define_one_rw(ignore_nice); |
1da177e4 LT |
220 | |
221 | static struct attribute * dbs_attributes[] = { | |
222 | &sampling_rate_max.attr, | |
223 | &sampling_rate_min.attr, | |
224 | &sampling_rate.attr, | |
225 | &sampling_down_factor.attr, | |
226 | &up_threshold.attr, | |
3d5ee9e5 | 227 | &ignore_nice.attr, |
1da177e4 LT |
228 | NULL |
229 | }; | |
230 | ||
231 | static struct attribute_group dbs_attr_group = { | |
232 | .attrs = dbs_attributes, | |
233 | .name = "ondemand", | |
234 | }; | |
235 | ||
236 | /************************** sysfs end ************************/ | |
237 | ||
238 | static void dbs_check_cpu(int cpu) | |
239 | { | |
c29f1403 DJ |
240 | unsigned int idle_ticks, up_idle_ticks, total_ticks; |
241 | unsigned int freq_next; | |
1da177e4 LT |
242 | unsigned int freq_down_sampling_rate; |
243 | static int down_skip[NR_CPUS]; | |
244 | struct cpu_dbs_info_s *this_dbs_info; | |
245 | ||
246 | struct cpufreq_policy *policy; | |
247 | unsigned int j; | |
248 | ||
249 | this_dbs_info = &per_cpu(cpu_dbs_info, cpu); | |
250 | if (!this_dbs_info->enable) | |
251 | return; | |
252 | ||
253 | policy = this_dbs_info->cur_policy; | |
254 | /* | |
c29f1403 DJ |
255 | * Every sampling_rate, we check, if current idle time is less |
256 | * than 20% (default), then we try to increase frequency | |
257 | * Every sampling_rate*sampling_down_factor, we look for a the lowest | |
258 | * frequency which can sustain the load while keeping idle time over | |
259 | * 30%. If such a frequency exist, we try to decrease to this frequency. | |
1da177e4 LT |
260 | * |
261 | * Any frequency increase takes it to the maximum frequency. | |
262 | * Frequency reduction happens at minimum steps of | |
c29f1403 | 263 | * 5% (default) of current frequency |
1da177e4 LT |
264 | */ |
265 | ||
266 | /* Check for frequency increase */ | |
9c7d269b | 267 | idle_ticks = UINT_MAX; |
1da177e4 | 268 | for_each_cpu_mask(j, policy->cpus) { |
9c7d269b | 269 | unsigned int tmp_idle_ticks, total_idle_ticks; |
1da177e4 LT |
270 | struct cpu_dbs_info_s *j_dbs_info; |
271 | ||
1da177e4 | 272 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
dac1c1a5 | 273 | total_idle_ticks = get_cpu_idle_time(j); |
1da177e4 LT |
274 | tmp_idle_ticks = total_idle_ticks - |
275 | j_dbs_info->prev_cpu_idle_up; | |
276 | j_dbs_info->prev_cpu_idle_up = total_idle_ticks; | |
277 | ||
278 | if (tmp_idle_ticks < idle_ticks) | |
279 | idle_ticks = tmp_idle_ticks; | |
280 | } | |
281 | ||
282 | /* Scale idle ticks by 100 and compare with up and down ticks */ | |
283 | idle_ticks *= 100; | |
284 | up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) * | |
6fe71165 | 285 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate); |
1da177e4 LT |
286 | |
287 | if (idle_ticks < up_idle_ticks) { | |
dac1c1a5 | 288 | down_skip[cpu] = 0; |
790d76fa DJ |
289 | for_each_cpu_mask(j, policy->cpus) { |
290 | struct cpu_dbs_info_s *j_dbs_info; | |
291 | ||
292 | j_dbs_info = &per_cpu(cpu_dbs_info, j); | |
293 | j_dbs_info->prev_cpu_idle_down = | |
294 | j_dbs_info->prev_cpu_idle_up; | |
295 | } | |
c11420a6 DJ |
296 | /* if we are already at full speed then break out early */ |
297 | if (policy->cur == policy->max) | |
298 | return; | |
299 | ||
1da177e4 LT |
300 | __cpufreq_driver_target(policy, policy->max, |
301 | CPUFREQ_RELATION_H); | |
1da177e4 LT |
302 | return; |
303 | } | |
304 | ||
305 | /* Check for frequency decrease */ | |
306 | down_skip[cpu]++; | |
307 | if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor) | |
308 | return; | |
309 | ||
9c7d269b | 310 | idle_ticks = UINT_MAX; |
1da177e4 | 311 | for_each_cpu_mask(j, policy->cpus) { |
9c7d269b | 312 | unsigned int tmp_idle_ticks, total_idle_ticks; |
1da177e4 LT |
313 | struct cpu_dbs_info_s *j_dbs_info; |
314 | ||
1da177e4 | 315 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
dac1c1a5 DJ |
316 | /* Check for frequency decrease */ |
317 | total_idle_ticks = j_dbs_info->prev_cpu_idle_up; | |
1da177e4 LT |
318 | tmp_idle_ticks = total_idle_ticks - |
319 | j_dbs_info->prev_cpu_idle_down; | |
320 | j_dbs_info->prev_cpu_idle_down = total_idle_ticks; | |
321 | ||
322 | if (tmp_idle_ticks < idle_ticks) | |
323 | idle_ticks = tmp_idle_ticks; | |
324 | } | |
325 | ||
1da177e4 | 326 | down_skip[cpu] = 0; |
c29f1403 DJ |
327 | /* if we cannot reduce the frequency anymore, break out early */ |
328 | if (policy->cur == policy->min) | |
329 | return; | |
1da177e4 | 330 | |
c29f1403 | 331 | /* Compute how many ticks there are between two measurements */ |
1da177e4 LT |
332 | freq_down_sampling_rate = dbs_tuners_ins.sampling_rate * |
333 | dbs_tuners_ins.sampling_down_factor; | |
c29f1403 | 334 | total_ticks = usecs_to_jiffies(freq_down_sampling_rate); |
1206aaac | 335 | |
c29f1403 DJ |
336 | /* |
337 | * The optimal frequency is the frequency that is the lowest that | |
338 | * can support the current CPU usage without triggering the up | |
339 | * policy. To be safe, we focus 10 points under the threshold. | |
340 | */ | |
341 | freq_next = ((total_ticks - idle_ticks) * 100) / total_ticks; | |
342 | freq_next = (freq_next * policy->cur) / | |
343 | (dbs_tuners_ins.up_threshold - 10); | |
1da177e4 | 344 | |
c29f1403 DJ |
345 | if (freq_next <= ((policy->cur * 95) / 100)) |
346 | __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L); | |
1da177e4 LT |
347 | } |
348 | ||
349 | static void do_dbs_timer(void *data) | |
350 | { | |
351 | int i; | |
352 | down(&dbs_sem); | |
6fe71165 DJ |
353 | for_each_online_cpu(i) |
354 | dbs_check_cpu(i); | |
1da177e4 | 355 | schedule_delayed_work(&dbs_work, |
6fe71165 | 356 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
1da177e4 LT |
357 | up(&dbs_sem); |
358 | } | |
359 | ||
360 | static inline void dbs_timer_init(void) | |
361 | { | |
362 | INIT_WORK(&dbs_work, do_dbs_timer, NULL); | |
363 | schedule_delayed_work(&dbs_work, | |
6fe71165 | 364 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
1da177e4 LT |
365 | return; |
366 | } | |
367 | ||
368 | static inline void dbs_timer_exit(void) | |
369 | { | |
370 | cancel_delayed_work(&dbs_work); | |
371 | return; | |
372 | } | |
373 | ||
374 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, | |
375 | unsigned int event) | |
376 | { | |
377 | unsigned int cpu = policy->cpu; | |
378 | struct cpu_dbs_info_s *this_dbs_info; | |
379 | unsigned int j; | |
380 | ||
381 | this_dbs_info = &per_cpu(cpu_dbs_info, cpu); | |
382 | ||
383 | switch (event) { | |
384 | case CPUFREQ_GOV_START: | |
385 | if ((!cpu_online(cpu)) || | |
386 | (!policy->cur)) | |
387 | return -EINVAL; | |
388 | ||
389 | if (policy->cpuinfo.transition_latency > | |
390 | (TRANSITION_LATENCY_LIMIT * 1000)) | |
391 | return -EINVAL; | |
392 | if (this_dbs_info->enable) /* Already enabled */ | |
393 | break; | |
394 | ||
395 | down(&dbs_sem); | |
396 | for_each_cpu_mask(j, policy->cpus) { | |
397 | struct cpu_dbs_info_s *j_dbs_info; | |
398 | j_dbs_info = &per_cpu(cpu_dbs_info, j); | |
399 | j_dbs_info->cur_policy = policy; | |
400 | ||
dac1c1a5 | 401 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j); |
3d5ee9e5 DJ |
402 | j_dbs_info->prev_cpu_idle_down |
403 | = j_dbs_info->prev_cpu_idle_up; | |
1da177e4 LT |
404 | } |
405 | this_dbs_info->enable = 1; | |
406 | sysfs_create_group(&policy->kobj, &dbs_attr_group); | |
407 | dbs_enable++; | |
408 | /* | |
409 | * Start the timerschedule work, when this governor | |
410 | * is used for first time | |
411 | */ | |
412 | if (dbs_enable == 1) { | |
413 | unsigned int latency; | |
414 | /* policy latency is in nS. Convert it to uS first */ | |
415 | ||
416 | latency = policy->cpuinfo.transition_latency; | |
417 | if (latency < 1000) | |
418 | latency = 1000; | |
419 | ||
420 | def_sampling_rate = (latency / 1000) * | |
421 | DEF_SAMPLING_RATE_LATENCY_MULTIPLIER; | |
422 | dbs_tuners_ins.sampling_rate = def_sampling_rate; | |
3d5ee9e5 | 423 | dbs_tuners_ins.ignore_nice = 0; |
1da177e4 LT |
424 | |
425 | dbs_timer_init(); | |
426 | } | |
427 | ||
428 | up(&dbs_sem); | |
429 | break; | |
430 | ||
431 | case CPUFREQ_GOV_STOP: | |
432 | down(&dbs_sem); | |
433 | this_dbs_info->enable = 0; | |
434 | sysfs_remove_group(&policy->kobj, &dbs_attr_group); | |
435 | dbs_enable--; | |
436 | /* | |
437 | * Stop the timerschedule work, when this governor | |
438 | * is used for first time | |
439 | */ | |
440 | if (dbs_enable == 0) | |
441 | dbs_timer_exit(); | |
442 | ||
443 | up(&dbs_sem); | |
444 | ||
445 | break; | |
446 | ||
447 | case CPUFREQ_GOV_LIMITS: | |
448 | down(&dbs_sem); | |
449 | if (policy->max < this_dbs_info->cur_policy->cur) | |
450 | __cpufreq_driver_target( | |
451 | this_dbs_info->cur_policy, | |
452 | policy->max, CPUFREQ_RELATION_H); | |
453 | else if (policy->min > this_dbs_info->cur_policy->cur) | |
454 | __cpufreq_driver_target( | |
455 | this_dbs_info->cur_policy, | |
456 | policy->min, CPUFREQ_RELATION_L); | |
457 | up(&dbs_sem); | |
458 | break; | |
459 | } | |
460 | return 0; | |
461 | } | |
462 | ||
7f335d4e | 463 | static struct cpufreq_governor cpufreq_gov_dbs = { |
1da177e4 LT |
464 | .name = "ondemand", |
465 | .governor = cpufreq_governor_dbs, | |
466 | .owner = THIS_MODULE, | |
467 | }; | |
1da177e4 LT |
468 | |
469 | static int __init cpufreq_gov_dbs_init(void) | |
470 | { | |
471 | return cpufreq_register_governor(&cpufreq_gov_dbs); | |
472 | } | |
473 | ||
474 | static void __exit cpufreq_gov_dbs_exit(void) | |
475 | { | |
476 | /* Make sure that the scheduled work is indeed not running */ | |
477 | flush_scheduled_work(); | |
478 | ||
479 | cpufreq_unregister_governor(&cpufreq_gov_dbs); | |
480 | } | |
481 | ||
482 | ||
483 | MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>"); | |
484 | MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for " | |
485 | "Low Latency Frequency Transition capable processors"); | |
486 | MODULE_LICENSE ("GPL"); | |
487 | ||
488 | module_init(cpufreq_gov_dbs_init); | |
489 | module_exit(cpufreq_gov_dbs_exit); |