Commit | Line | Data |
---|---|---|
b9170836 DJ |
1 | /* |
2 | * drivers/cpufreq/cpufreq_conservative.c | |
3 | * | |
4 | * Copyright (C) 2001 Russell King | |
5 | * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. | |
6 | * Jun Nakajima <jun.nakajima@intel.com> | |
11a80a9c | 7 | * (C) 2009 Alexander Clouter <alex@digriz.org.uk> |
b9170836 DJ |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
13 | ||
4d5dcc42 | 14 | #include <linux/slab.h> |
4471a34f | 15 | #include "cpufreq_governor.h" |
b9170836 | 16 | |
c88883cd | 17 | /* Conservative governor macros */ |
b9170836 | 18 | #define DEF_FREQUENCY_UP_THRESHOLD (80) |
b9170836 | 19 | #define DEF_FREQUENCY_DOWN_THRESHOLD (20) |
98765847 | 20 | #define DEF_FREQUENCY_STEP (5) |
2c906b31 AC |
21 | #define DEF_SAMPLING_DOWN_FACTOR (1) |
22 | #define MAX_SAMPLING_DOWN_FACTOR (10) | |
b9170836 | 23 | |
4471a34f | 24 | static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info); |
b9170836 | 25 | |
98765847 SK |
26 | static inline unsigned int get_freq_target(struct cs_dbs_tuners *cs_tuners, |
27 | struct cpufreq_policy *policy) | |
28 | { | |
29 | unsigned int freq_target = (cs_tuners->freq_step * policy->max) / 100; | |
30 | ||
31 | /* max freq cannot be less than 100. But who knows... */ | |
32 | if (unlikely(freq_target == 0)) | |
33 | freq_target = DEF_FREQUENCY_STEP; | |
34 | ||
35 | return freq_target; | |
36 | } | |
37 | ||
4471a34f VK |
38 | /* |
39 | * Every sampling_rate, we check, if current idle time is less than 20% | |
7af1c056 SK |
40 | * (default), then we try to increase frequency. Every sampling_rate * |
41 | * sampling_down_factor, we check, if current idle time is more than 80% | |
42 | * (default), then we try to decrease frequency | |
4471a34f VK |
43 | * |
44 | * Any frequency increase takes it to the maximum frequency. Frequency reduction | |
45 | * happens at minimum steps of 5% (default) of maximum frequency | |
46 | */ | |
47 | static void cs_check_cpu(int cpu, unsigned int load) | |
a8d7c3bc | 48 | { |
4471a34f | 49 | struct cs_cpu_dbs_info_s *dbs_info = &per_cpu(cs_cpu_dbs_info, cpu); |
e40e7b25 | 50 | struct cpufreq_policy *policy = dbs_info->cdbs.policy_dbs->policy; |
bc505475 RW |
51 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
52 | struct dbs_data *dbs_data = policy_dbs->dbs_data; | |
4d5dcc42 | 53 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; |
4471a34f VK |
54 | |
55 | /* | |
56 | * break out if we 'cannot' reduce the speed as the user might | |
57 | * want freq_step to be zero | |
58 | */ | |
4d5dcc42 | 59 | if (cs_tuners->freq_step == 0) |
4471a34f VK |
60 | return; |
61 | ||
62 | /* Check for frequency increase */ | |
ff4b1789 | 63 | if (load > dbs_data->up_threshold) { |
4471a34f VK |
64 | dbs_info->down_skip = 0; |
65 | ||
66 | /* if we are already at full speed then break out early */ | |
67 | if (dbs_info->requested_freq == policy->max) | |
68 | return; | |
69 | ||
98765847 | 70 | dbs_info->requested_freq += get_freq_target(cs_tuners, policy); |
a8d7c3bc | 71 | |
6d7bcb14 XC |
72 | if (dbs_info->requested_freq > policy->max) |
73 | dbs_info->requested_freq = policy->max; | |
74 | ||
4471a34f VK |
75 | __cpufreq_driver_target(policy, dbs_info->requested_freq, |
76 | CPUFREQ_RELATION_H); | |
77 | return; | |
78 | } | |
79 | ||
7af1c056 | 80 | /* if sampling_down_factor is active break out early */ |
ff4b1789 | 81 | if (++dbs_info->down_skip < dbs_data->sampling_down_factor) |
7af1c056 SK |
82 | return; |
83 | dbs_info->down_skip = 0; | |
84 | ||
27ed3cd2 SK |
85 | /* Check for frequency decrease */ |
86 | if (load < cs_tuners->down_threshold) { | |
3baa976a | 87 | unsigned int freq_target; |
4471a34f VK |
88 | /* |
89 | * if we cannot reduce the frequency anymore, break out early | |
90 | */ | |
91 | if (policy->cur == policy->min) | |
92 | return; | |
93 | ||
3baa976a XC |
94 | freq_target = get_freq_target(cs_tuners, policy); |
95 | if (dbs_info->requested_freq > freq_target) | |
96 | dbs_info->requested_freq -= freq_target; | |
97 | else | |
98 | dbs_info->requested_freq = policy->min; | |
ad529a9c | 99 | |
4471a34f | 100 | __cpufreq_driver_target(policy, dbs_info->requested_freq, |
fed573d5 | 101 | CPUFREQ_RELATION_L); |
4471a34f VK |
102 | return; |
103 | } | |
104 | } | |
105 | ||
9be4fd2c | 106 | static unsigned int cs_dbs_timer(struct cpufreq_policy *policy) |
4471a34f | 107 | { |
bc505475 RW |
108 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
109 | struct dbs_data *dbs_data = policy_dbs->dbs_data; | |
43e0ee36 | 110 | |
d10b5eb5 | 111 | dbs_check_cpu(policy); |
ff4b1789 | 112 | return delay_for_sampling_rate(dbs_data->sampling_rate); |
66df2a01 FB |
113 | } |
114 | ||
4471a34f | 115 | static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
af926185 | 116 | void *data); |
a8d7c3bc | 117 | |
8e0484d2 VK |
118 | static struct notifier_block cs_cpufreq_notifier_block = { |
119 | .notifier_call = dbs_cpufreq_notifier, | |
120 | }; | |
121 | ||
b9170836 | 122 | /************************** sysfs interface ************************/ |
7bdad34d | 123 | static struct dbs_governor cs_dbs_gov; |
b9170836 | 124 | |
4d5dcc42 VK |
125 | static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data, |
126 | const char *buf, size_t count) | |
b9170836 DJ |
127 | { |
128 | unsigned int input; | |
129 | int ret; | |
9acef487 | 130 | ret = sscanf(buf, "%u", &input); |
8e677ce8 | 131 | |
2c906b31 | 132 | if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) |
b9170836 DJ |
133 | return -EINVAL; |
134 | ||
ff4b1789 | 135 | dbs_data->sampling_down_factor = input; |
b9170836 DJ |
136 | return count; |
137 | } | |
138 | ||
4d5dcc42 VK |
139 | static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf, |
140 | size_t count) | |
b9170836 DJ |
141 | { |
142 | unsigned int input; | |
143 | int ret; | |
9acef487 | 144 | ret = sscanf(buf, "%u", &input); |
b9170836 | 145 | |
8e677ce8 | 146 | if (ret != 1) |
b9170836 | 147 | return -EINVAL; |
8e677ce8 | 148 | |
ff4b1789 | 149 | dbs_data->sampling_rate = max(input, dbs_data->min_sampling_rate); |
b9170836 DJ |
150 | return count; |
151 | } | |
152 | ||
4d5dcc42 VK |
153 | static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf, |
154 | size_t count) | |
b9170836 | 155 | { |
4d5dcc42 | 156 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; |
b9170836 DJ |
157 | unsigned int input; |
158 | int ret; | |
9acef487 | 159 | ret = sscanf(buf, "%u", &input); |
b9170836 | 160 | |
4d5dcc42 | 161 | if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold) |
b9170836 | 162 | return -EINVAL; |
b9170836 | 163 | |
ff4b1789 | 164 | dbs_data->up_threshold = input; |
b9170836 DJ |
165 | return count; |
166 | } | |
167 | ||
4d5dcc42 VK |
168 | static ssize_t store_down_threshold(struct dbs_data *dbs_data, const char *buf, |
169 | size_t count) | |
b9170836 | 170 | { |
4d5dcc42 | 171 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; |
b9170836 DJ |
172 | unsigned int input; |
173 | int ret; | |
9acef487 | 174 | ret = sscanf(buf, "%u", &input); |
b9170836 | 175 | |
8e677ce8 AC |
176 | /* cannot be lower than 11 otherwise freq will not fall */ |
177 | if (ret != 1 || input < 11 || input > 100 || | |
ff4b1789 | 178 | input >= dbs_data->up_threshold) |
b9170836 | 179 | return -EINVAL; |
b9170836 | 180 | |
4d5dcc42 | 181 | cs_tuners->down_threshold = input; |
b9170836 DJ |
182 | return count; |
183 | } | |
184 | ||
6c4640c3 VK |
185 | static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data, |
186 | const char *buf, size_t count) | |
b9170836 | 187 | { |
4471a34f | 188 | unsigned int input, j; |
b9170836 DJ |
189 | int ret; |
190 | ||
18a7247d DJ |
191 | ret = sscanf(buf, "%u", &input); |
192 | if (ret != 1) | |
b9170836 DJ |
193 | return -EINVAL; |
194 | ||
18a7247d | 195 | if (input > 1) |
b9170836 | 196 | input = 1; |
18a7247d | 197 | |
ff4b1789 | 198 | if (input == dbs_data->ignore_nice_load) /* nothing to do */ |
b9170836 | 199 | return count; |
326c86de | 200 | |
ff4b1789 | 201 | dbs_data->ignore_nice_load = input; |
b9170836 | 202 | |
8e677ce8 | 203 | /* we need to re-evaluate prev_cpu_idle */ |
dac1c1a5 | 204 | for_each_online_cpu(j) { |
4471a34f | 205 | struct cs_cpu_dbs_info_s *dbs_info; |
245b2e70 | 206 | dbs_info = &per_cpu(cs_cpu_dbs_info, j); |
4471a34f | 207 | dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j, |
9366d840 | 208 | &dbs_info->cdbs.prev_cpu_wall, 0); |
ff4b1789 | 209 | if (dbs_data->ignore_nice_load) |
4471a34f VK |
210 | dbs_info->cdbs.prev_cpu_nice = |
211 | kcpustat_cpu(j).cpustat[CPUTIME_NICE]; | |
b9170836 | 212 | } |
b9170836 DJ |
213 | return count; |
214 | } | |
215 | ||
4d5dcc42 VK |
216 | static ssize_t store_freq_step(struct dbs_data *dbs_data, const char *buf, |
217 | size_t count) | |
b9170836 | 218 | { |
4d5dcc42 | 219 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; |
b9170836 DJ |
220 | unsigned int input; |
221 | int ret; | |
18a7247d | 222 | ret = sscanf(buf, "%u", &input); |
b9170836 | 223 | |
18a7247d | 224 | if (ret != 1) |
b9170836 DJ |
225 | return -EINVAL; |
226 | ||
18a7247d | 227 | if (input > 100) |
b9170836 | 228 | input = 100; |
18a7247d | 229 | |
4471a34f VK |
230 | /* |
231 | * no need to test here if freq_step is zero as the user might actually | |
232 | * want this, they would be crazy though :) | |
233 | */ | |
4d5dcc42 | 234 | cs_tuners->freq_step = input; |
b9170836 DJ |
235 | return count; |
236 | } | |
237 | ||
4d5dcc42 | 238 | show_store_one(cs, down_threshold); |
4d5dcc42 | 239 | show_store_one(cs, freq_step); |
ff4b1789 VK |
240 | show_store_one_common(cs, sampling_rate); |
241 | show_store_one_common(cs, sampling_down_factor); | |
242 | show_store_one_common(cs, up_threshold); | |
243 | show_store_one_common(cs, ignore_nice_load); | |
d0684d3b | 244 | show_one_common(cs, min_sampling_rate); |
4d5dcc42 VK |
245 | |
246 | gov_sys_pol_attr_rw(sampling_rate); | |
247 | gov_sys_pol_attr_rw(sampling_down_factor); | |
248 | gov_sys_pol_attr_rw(up_threshold); | |
249 | gov_sys_pol_attr_rw(down_threshold); | |
6c4640c3 | 250 | gov_sys_pol_attr_rw(ignore_nice_load); |
4d5dcc42 | 251 | gov_sys_pol_attr_rw(freq_step); |
d0684d3b | 252 | gov_sys_pol_attr_ro(min_sampling_rate); |
4d5dcc42 VK |
253 | |
254 | static struct attribute *dbs_attributes_gov_sys[] = { | |
d0684d3b | 255 | &min_sampling_rate_gov_sys.attr, |
4d5dcc42 VK |
256 | &sampling_rate_gov_sys.attr, |
257 | &sampling_down_factor_gov_sys.attr, | |
258 | &up_threshold_gov_sys.attr, | |
259 | &down_threshold_gov_sys.attr, | |
6c4640c3 | 260 | &ignore_nice_load_gov_sys.attr, |
4d5dcc42 | 261 | &freq_step_gov_sys.attr, |
b9170836 DJ |
262 | NULL |
263 | }; | |
264 | ||
4d5dcc42 VK |
265 | static struct attribute_group cs_attr_group_gov_sys = { |
266 | .attrs = dbs_attributes_gov_sys, | |
267 | .name = "conservative", | |
268 | }; | |
269 | ||
270 | static struct attribute *dbs_attributes_gov_pol[] = { | |
d0684d3b | 271 | &min_sampling_rate_gov_pol.attr, |
4d5dcc42 VK |
272 | &sampling_rate_gov_pol.attr, |
273 | &sampling_down_factor_gov_pol.attr, | |
274 | &up_threshold_gov_pol.attr, | |
275 | &down_threshold_gov_pol.attr, | |
6c4640c3 | 276 | &ignore_nice_load_gov_pol.attr, |
4d5dcc42 VK |
277 | &freq_step_gov_pol.attr, |
278 | NULL | |
279 | }; | |
280 | ||
281 | static struct attribute_group cs_attr_group_gov_pol = { | |
282 | .attrs = dbs_attributes_gov_pol, | |
b9170836 DJ |
283 | .name = "conservative", |
284 | }; | |
285 | ||
286 | /************************** sysfs end ************************/ | |
287 | ||
8e0484d2 | 288 | static int cs_init(struct dbs_data *dbs_data, bool notify) |
4d5dcc42 VK |
289 | { |
290 | struct cs_dbs_tuners *tuners; | |
291 | ||
d5b73cd8 | 292 | tuners = kzalloc(sizeof(*tuners), GFP_KERNEL); |
4d5dcc42 VK |
293 | if (!tuners) { |
294 | pr_err("%s: kzalloc failed\n", __func__); | |
295 | return -ENOMEM; | |
296 | } | |
297 | ||
4d5dcc42 | 298 | tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD; |
98765847 | 299 | tuners->freq_step = DEF_FREQUENCY_STEP; |
ff4b1789 VK |
300 | dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD; |
301 | dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR; | |
302 | dbs_data->ignore_nice_load = 0; | |
4d5dcc42 VK |
303 | |
304 | dbs_data->tuners = tuners; | |
305 | dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO * | |
306 | jiffies_to_usecs(10); | |
8e0484d2 VK |
307 | |
308 | if (notify) | |
309 | cpufreq_register_notifier(&cs_cpufreq_notifier_block, | |
310 | CPUFREQ_TRANSITION_NOTIFIER); | |
311 | ||
4d5dcc42 VK |
312 | return 0; |
313 | } | |
314 | ||
8e0484d2 | 315 | static void cs_exit(struct dbs_data *dbs_data, bool notify) |
4d5dcc42 | 316 | { |
8e0484d2 VK |
317 | if (notify) |
318 | cpufreq_unregister_notifier(&cs_cpufreq_notifier_block, | |
319 | CPUFREQ_TRANSITION_NOTIFIER); | |
320 | ||
4d5dcc42 VK |
321 | kfree(dbs_data->tuners); |
322 | } | |
323 | ||
4471a34f | 324 | define_get_cpu_dbs_routines(cs_cpu_dbs_info); |
8e677ce8 | 325 | |
7bdad34d | 326 | static struct dbs_governor cs_dbs_gov = { |
af926185 RW |
327 | .gov = { |
328 | .name = "conservative", | |
906a6e5a | 329 | .governor = cpufreq_governor_dbs, |
af926185 RW |
330 | .max_transition_latency = TRANSITION_LATENCY_LIMIT, |
331 | .owner = THIS_MODULE, | |
332 | }, | |
4471a34f | 333 | .governor = GOV_CONSERVATIVE, |
4d5dcc42 VK |
334 | .attr_group_gov_sys = &cs_attr_group_gov_sys, |
335 | .attr_group_gov_pol = &cs_attr_group_gov_pol, | |
4471a34f VK |
336 | .get_cpu_cdbs = get_cpu_cdbs, |
337 | .get_cpu_dbs_info_s = get_cpu_dbs_info_s, | |
338 | .gov_dbs_timer = cs_dbs_timer, | |
339 | .gov_check_cpu = cs_check_cpu, | |
4d5dcc42 VK |
340 | .init = cs_init, |
341 | .exit = cs_exit, | |
4471a34f | 342 | }; |
b9170836 | 343 | |
7bdad34d | 344 | #define CPU_FREQ_GOV_CONSERVATIVE (&cs_dbs_gov.gov) |
af926185 | 345 | |
af926185 RW |
346 | static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
347 | void *data) | |
348 | { | |
349 | struct cpufreq_freqs *freq = data; | |
350 | struct cs_cpu_dbs_info_s *dbs_info = | |
351 | &per_cpu(cs_cpu_dbs_info, freq->cpu); | |
352 | struct cpufreq_policy *policy = cpufreq_cpu_get_raw(freq->cpu); | |
353 | ||
354 | if (!policy) | |
355 | return 0; | |
356 | ||
357 | /* policy isn't governed by conservative governor */ | |
358 | if (policy->governor != CPU_FREQ_GOV_CONSERVATIVE) | |
359 | return 0; | |
360 | ||
361 | /* | |
362 | * we only care if our internally tracked freq moves outside the 'valid' | |
363 | * ranges of frequency available to us otherwise we do not change it | |
364 | */ | |
365 | if (dbs_info->requested_freq > policy->max | |
366 | || dbs_info->requested_freq < policy->min) | |
367 | dbs_info->requested_freq = freq->new; | |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
b9170836 DJ |
372 | static int __init cpufreq_gov_dbs_init(void) |
373 | { | |
af926185 | 374 | return cpufreq_register_governor(CPU_FREQ_GOV_CONSERVATIVE); |
b9170836 DJ |
375 | } |
376 | ||
377 | static void __exit cpufreq_gov_dbs_exit(void) | |
378 | { | |
af926185 | 379 | cpufreq_unregister_governor(CPU_FREQ_GOV_CONSERVATIVE); |
b9170836 DJ |
380 | } |
381 | ||
11a80a9c | 382 | MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>"); |
9acef487 | 383 | MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for " |
b9170836 DJ |
384 | "Low Latency Frequency Transition capable processors " |
385 | "optimised for use in a battery environment"); | |
9acef487 | 386 | MODULE_LICENSE("GPL"); |
b9170836 | 387 | |
6915719b | 388 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE |
de1df26b RW |
389 | struct cpufreq_governor *cpufreq_default_governor(void) |
390 | { | |
af926185 | 391 | return CPU_FREQ_GOV_CONSERVATIVE; |
de1df26b RW |
392 | } |
393 | ||
6915719b JW |
394 | fs_initcall(cpufreq_gov_dbs_init); |
395 | #else | |
b9170836 | 396 | module_init(cpufreq_gov_dbs_init); |
6915719b | 397 | #endif |
b9170836 | 398 | module_exit(cpufreq_gov_dbs_exit); |