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 | ||
b9170836 | 14 | #include <linux/cpufreq.h> |
4471a34f VK |
15 | #include <linux/init.h> |
16 | #include <linux/kernel.h> | |
b9170836 | 17 | #include <linux/kernel_stat.h> |
4471a34f VK |
18 | #include <linux/kobject.h> |
19 | #include <linux/module.h> | |
3fc54d37 | 20 | #include <linux/mutex.h> |
4471a34f VK |
21 | #include <linux/notifier.h> |
22 | #include <linux/percpu-defs.h> | |
23 | #include <linux/sysfs.h> | |
24 | #include <linux/types.h> | |
8e677ce8 | 25 | |
4471a34f | 26 | #include "cpufreq_governor.h" |
b9170836 | 27 | |
4471a34f | 28 | /* Conservative governor macors */ |
b9170836 | 29 | #define DEF_FREQUENCY_UP_THRESHOLD (80) |
b9170836 | 30 | #define DEF_FREQUENCY_DOWN_THRESHOLD (20) |
2c906b31 AC |
31 | #define DEF_SAMPLING_DOWN_FACTOR (1) |
32 | #define MAX_SAMPLING_DOWN_FACTOR (10) | |
b9170836 | 33 | |
4471a34f VK |
34 | static struct dbs_data cs_dbs_data; |
35 | static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info); | |
b9170836 | 36 | |
4471a34f | 37 | static struct cs_dbs_tuners cs_tuners = { |
18a7247d DJ |
38 | .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, |
39 | .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD, | |
40 | .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR, | |
41 | .ignore_nice = 0, | |
42 | .freq_step = 5, | |
b9170836 DJ |
43 | }; |
44 | ||
4471a34f VK |
45 | /* |
46 | * Every sampling_rate, we check, if current idle time is less than 20% | |
47 | * (default), then we try to increase frequency Every sampling_rate * | |
48 | * sampling_down_factor, we check, if current idle time is more than 80%, then | |
49 | * we try to decrease frequency | |
50 | * | |
51 | * Any frequency increase takes it to the maximum frequency. Frequency reduction | |
52 | * happens at minimum steps of 5% (default) of maximum frequency | |
53 | */ | |
54 | static void cs_check_cpu(int cpu, unsigned int load) | |
a8d7c3bc | 55 | { |
4471a34f VK |
56 | struct cs_cpu_dbs_info_s *dbs_info = &per_cpu(cs_cpu_dbs_info, cpu); |
57 | struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy; | |
58 | unsigned int freq_target; | |
59 | ||
60 | /* | |
61 | * break out if we 'cannot' reduce the speed as the user might | |
62 | * want freq_step to be zero | |
63 | */ | |
64 | if (cs_tuners.freq_step == 0) | |
65 | return; | |
66 | ||
67 | /* Check for frequency increase */ | |
68 | if (load > cs_tuners.up_threshold) { | |
69 | dbs_info->down_skip = 0; | |
70 | ||
71 | /* if we are already at full speed then break out early */ | |
72 | if (dbs_info->requested_freq == policy->max) | |
73 | return; | |
74 | ||
75 | freq_target = (cs_tuners.freq_step * policy->max) / 100; | |
76 | ||
77 | /* max freq cannot be less than 100. But who knows.... */ | |
78 | if (unlikely(freq_target == 0)) | |
79 | freq_target = 5; | |
80 | ||
81 | dbs_info->requested_freq += freq_target; | |
82 | if (dbs_info->requested_freq > policy->max) | |
83 | dbs_info->requested_freq = policy->max; | |
a8d7c3bc | 84 | |
4471a34f VK |
85 | __cpufreq_driver_target(policy, dbs_info->requested_freq, |
86 | CPUFREQ_RELATION_H); | |
87 | return; | |
88 | } | |
89 | ||
90 | /* | |
91 | * The optimal frequency is the frequency that is the lowest that can | |
92 | * support the current CPU usage without triggering the up policy. To be | |
93 | * safe, we focus 10 points under the threshold. | |
94 | */ | |
95 | if (load < (cs_tuners.down_threshold - 10)) { | |
96 | freq_target = (cs_tuners.freq_step * policy->max) / 100; | |
97 | ||
98 | dbs_info->requested_freq -= freq_target; | |
99 | if (dbs_info->requested_freq < policy->min) | |
100 | dbs_info->requested_freq = policy->min; | |
101 | ||
102 | /* | |
103 | * if we cannot reduce the frequency anymore, break out early | |
104 | */ | |
105 | if (policy->cur == policy->min) | |
106 | return; | |
107 | ||
108 | __cpufreq_driver_target(policy, dbs_info->requested_freq, | |
109 | CPUFREQ_RELATION_H); | |
110 | return; | |
111 | } | |
112 | } | |
113 | ||
66df2a01 FB |
114 | static void cs_timer_update(struct cs_cpu_dbs_info_s *dbs_info, bool sample, |
115 | struct delayed_work *dw) | |
4471a34f | 116 | { |
09dca5ae | 117 | unsigned int cpu = dbs_info->cdbs.cur_policy->cpu; |
4471a34f VK |
118 | int delay = delay_for_sampling_rate(cs_tuners.sampling_rate); |
119 | ||
66df2a01 FB |
120 | if (sample) |
121 | dbs_check_cpu(&cs_dbs_data, cpu); | |
122 | ||
123 | schedule_delayed_work_on(smp_processor_id(), dw, delay); | |
124 | } | |
125 | ||
126 | static void cs_timer_coordinated(struct cs_cpu_dbs_info_s *dbs_info_local, | |
127 | struct delayed_work *dw) | |
128 | { | |
129 | struct cs_cpu_dbs_info_s *dbs_info; | |
130 | ktime_t time_now; | |
131 | s64 delta_us; | |
132 | bool sample = true; | |
133 | ||
134 | /* use leader CPU's dbs_info */ | |
09dca5ae FB |
135 | dbs_info = &per_cpu(cs_cpu_dbs_info, |
136 | dbs_info_local->cdbs.cur_policy->cpu); | |
4471a34f VK |
137 | mutex_lock(&dbs_info->cdbs.timer_mutex); |
138 | ||
66df2a01 FB |
139 | time_now = ktime_get(); |
140 | delta_us = ktime_us_delta(time_now, dbs_info->cdbs.time_stamp); | |
4471a34f | 141 | |
66df2a01 FB |
142 | /* Do nothing if we recently have sampled */ |
143 | if (delta_us < (s64)(cs_tuners.sampling_rate / 2)) | |
144 | sample = false; | |
145 | else | |
146 | dbs_info->cdbs.time_stamp = time_now; | |
147 | ||
148 | cs_timer_update(dbs_info, sample, dw); | |
4471a34f VK |
149 | mutex_unlock(&dbs_info->cdbs.timer_mutex); |
150 | } | |
151 | ||
66df2a01 FB |
152 | static void cs_dbs_timer(struct work_struct *work) |
153 | { | |
154 | struct delayed_work *dw = to_delayed_work(work); | |
155 | struct cs_cpu_dbs_info_s *dbs_info = container_of(work, | |
156 | struct cs_cpu_dbs_info_s, cdbs.work.work); | |
157 | ||
2624f90c | 158 | if (policy_is_shared(dbs_info->cdbs.cur_policy)) { |
66df2a01 FB |
159 | cs_timer_coordinated(dbs_info, dw); |
160 | } else { | |
161 | mutex_lock(&dbs_info->cdbs.timer_mutex); | |
162 | cs_timer_update(dbs_info, true, dw); | |
163 | mutex_unlock(&dbs_info->cdbs.timer_mutex); | |
164 | } | |
165 | } | |
4471a34f VK |
166 | static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
167 | void *data) | |
168 | { | |
169 | struct cpufreq_freqs *freq = data; | |
170 | struct cs_cpu_dbs_info_s *dbs_info = | |
171 | &per_cpu(cs_cpu_dbs_info, freq->cpu); | |
f407a08b AC |
172 | struct cpufreq_policy *policy; |
173 | ||
4471a34f | 174 | if (!dbs_info->enable) |
a8d7c3bc EO |
175 | return 0; |
176 | ||
4471a34f | 177 | policy = dbs_info->cdbs.cur_policy; |
f407a08b AC |
178 | |
179 | /* | |
4471a34f VK |
180 | * we only care if our internally tracked freq moves outside the 'valid' |
181 | * ranges of freqency available to us otherwise we do not change it | |
f407a08b | 182 | */ |
4471a34f VK |
183 | if (dbs_info->requested_freq > policy->max |
184 | || dbs_info->requested_freq < policy->min) | |
185 | dbs_info->requested_freq = freq->new; | |
a8d7c3bc EO |
186 | |
187 | return 0; | |
188 | } | |
189 | ||
b9170836 | 190 | /************************** sysfs interface ************************/ |
49b015ce TR |
191 | static ssize_t show_sampling_rate_min(struct kobject *kobj, |
192 | struct attribute *attr, char *buf) | |
b9170836 | 193 | { |
4471a34f | 194 | return sprintf(buf, "%u\n", cs_dbs_data.min_sampling_rate); |
b9170836 DJ |
195 | } |
196 | ||
49b015ce TR |
197 | static ssize_t store_sampling_down_factor(struct kobject *a, |
198 | struct attribute *b, | |
199 | const char *buf, size_t count) | |
b9170836 DJ |
200 | { |
201 | unsigned int input; | |
202 | int ret; | |
9acef487 | 203 | ret = sscanf(buf, "%u", &input); |
8e677ce8 | 204 | |
2c906b31 | 205 | if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) |
b9170836 DJ |
206 | return -EINVAL; |
207 | ||
4471a34f | 208 | cs_tuners.sampling_down_factor = input; |
b9170836 DJ |
209 | return count; |
210 | } | |
211 | ||
49b015ce TR |
212 | static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b, |
213 | const char *buf, size_t count) | |
b9170836 DJ |
214 | { |
215 | unsigned int input; | |
216 | int ret; | |
9acef487 | 217 | ret = sscanf(buf, "%u", &input); |
b9170836 | 218 | |
8e677ce8 | 219 | if (ret != 1) |
b9170836 | 220 | return -EINVAL; |
8e677ce8 | 221 | |
4471a34f | 222 | cs_tuners.sampling_rate = max(input, cs_dbs_data.min_sampling_rate); |
b9170836 DJ |
223 | return count; |
224 | } | |
225 | ||
49b015ce TR |
226 | static ssize_t store_up_threshold(struct kobject *a, struct attribute *b, |
227 | const char *buf, size_t count) | |
b9170836 DJ |
228 | { |
229 | unsigned int input; | |
230 | int ret; | |
9acef487 | 231 | ret = sscanf(buf, "%u", &input); |
b9170836 | 232 | |
4471a34f | 233 | if (ret != 1 || input > 100 || input <= cs_tuners.down_threshold) |
b9170836 | 234 | return -EINVAL; |
b9170836 | 235 | |
4471a34f | 236 | cs_tuners.up_threshold = input; |
b9170836 DJ |
237 | return count; |
238 | } | |
239 | ||
49b015ce TR |
240 | static ssize_t store_down_threshold(struct kobject *a, struct attribute *b, |
241 | const char *buf, size_t count) | |
b9170836 DJ |
242 | { |
243 | unsigned int input; | |
244 | int ret; | |
9acef487 | 245 | ret = sscanf(buf, "%u", &input); |
b9170836 | 246 | |
8e677ce8 AC |
247 | /* cannot be lower than 11 otherwise freq will not fall */ |
248 | if (ret != 1 || input < 11 || input > 100 || | |
4471a34f | 249 | input >= cs_tuners.up_threshold) |
b9170836 | 250 | return -EINVAL; |
b9170836 | 251 | |
4471a34f | 252 | cs_tuners.down_threshold = input; |
b9170836 DJ |
253 | return count; |
254 | } | |
255 | ||
49b015ce TR |
256 | static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b, |
257 | const char *buf, size_t count) | |
b9170836 | 258 | { |
4471a34f | 259 | unsigned int input, j; |
b9170836 DJ |
260 | int ret; |
261 | ||
18a7247d DJ |
262 | ret = sscanf(buf, "%u", &input); |
263 | if (ret != 1) | |
b9170836 DJ |
264 | return -EINVAL; |
265 | ||
18a7247d | 266 | if (input > 1) |
b9170836 | 267 | input = 1; |
18a7247d | 268 | |
4471a34f | 269 | if (input == cs_tuners.ignore_nice) /* nothing to do */ |
b9170836 | 270 | return count; |
326c86de | 271 | |
4471a34f | 272 | cs_tuners.ignore_nice = input; |
b9170836 | 273 | |
8e677ce8 | 274 | /* we need to re-evaluate prev_cpu_idle */ |
dac1c1a5 | 275 | for_each_online_cpu(j) { |
4471a34f | 276 | struct cs_cpu_dbs_info_s *dbs_info; |
245b2e70 | 277 | dbs_info = &per_cpu(cs_cpu_dbs_info, j); |
4471a34f VK |
278 | dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j, |
279 | &dbs_info->cdbs.prev_cpu_wall); | |
280 | if (cs_tuners.ignore_nice) | |
281 | dbs_info->cdbs.prev_cpu_nice = | |
282 | kcpustat_cpu(j).cpustat[CPUTIME_NICE]; | |
b9170836 | 283 | } |
b9170836 DJ |
284 | return count; |
285 | } | |
286 | ||
49b015ce TR |
287 | static ssize_t store_freq_step(struct kobject *a, struct attribute *b, |
288 | const char *buf, size_t count) | |
b9170836 DJ |
289 | { |
290 | unsigned int input; | |
291 | int ret; | |
18a7247d | 292 | ret = sscanf(buf, "%u", &input); |
b9170836 | 293 | |
18a7247d | 294 | if (ret != 1) |
b9170836 DJ |
295 | return -EINVAL; |
296 | ||
18a7247d | 297 | if (input > 100) |
b9170836 | 298 | input = 100; |
18a7247d | 299 | |
4471a34f VK |
300 | /* |
301 | * no need to test here if freq_step is zero as the user might actually | |
302 | * want this, they would be crazy though :) | |
303 | */ | |
304 | cs_tuners.freq_step = input; | |
b9170836 DJ |
305 | return count; |
306 | } | |
307 | ||
4471a34f VK |
308 | show_one(cs, sampling_rate, sampling_rate); |
309 | show_one(cs, sampling_down_factor, sampling_down_factor); | |
310 | show_one(cs, up_threshold, up_threshold); | |
311 | show_one(cs, down_threshold, down_threshold); | |
312 | show_one(cs, ignore_nice_load, ignore_nice); | |
313 | show_one(cs, freq_step, freq_step); | |
314 | ||
6dad2a29 BP |
315 | define_one_global_rw(sampling_rate); |
316 | define_one_global_rw(sampling_down_factor); | |
317 | define_one_global_rw(up_threshold); | |
318 | define_one_global_rw(down_threshold); | |
319 | define_one_global_rw(ignore_nice_load); | |
320 | define_one_global_rw(freq_step); | |
4471a34f | 321 | define_one_global_ro(sampling_rate_min); |
b9170836 | 322 | |
9acef487 | 323 | static struct attribute *dbs_attributes[] = { |
b9170836 DJ |
324 | &sampling_rate_min.attr, |
325 | &sampling_rate.attr, | |
326 | &sampling_down_factor.attr, | |
327 | &up_threshold.attr, | |
328 | &down_threshold.attr, | |
001893cd | 329 | &ignore_nice_load.attr, |
b9170836 DJ |
330 | &freq_step.attr, |
331 | NULL | |
332 | }; | |
333 | ||
4471a34f | 334 | static struct attribute_group cs_attr_group = { |
b9170836 DJ |
335 | .attrs = dbs_attributes, |
336 | .name = "conservative", | |
337 | }; | |
338 | ||
339 | /************************** sysfs end ************************/ | |
340 | ||
4471a34f | 341 | define_get_cpu_dbs_routines(cs_cpu_dbs_info); |
8e677ce8 | 342 | |
4471a34f VK |
343 | static struct notifier_block cs_cpufreq_notifier_block = { |
344 | .notifier_call = dbs_cpufreq_notifier, | |
345 | }; | |
8e677ce8 | 346 | |
4471a34f VK |
347 | static struct cs_ops cs_ops = { |
348 | .notifier_block = &cs_cpufreq_notifier_block, | |
349 | }; | |
b9170836 | 350 | |
4471a34f VK |
351 | static struct dbs_data cs_dbs_data = { |
352 | .governor = GOV_CONSERVATIVE, | |
353 | .attr_group = &cs_attr_group, | |
354 | .tuners = &cs_tuners, | |
355 | .get_cpu_cdbs = get_cpu_cdbs, | |
356 | .get_cpu_dbs_info_s = get_cpu_dbs_info_s, | |
357 | .gov_dbs_timer = cs_dbs_timer, | |
358 | .gov_check_cpu = cs_check_cpu, | |
359 | .gov_ops = &cs_ops, | |
360 | }; | |
b9170836 | 361 | |
4471a34f | 362 | static int cs_cpufreq_governor_dbs(struct cpufreq_policy *policy, |
b9170836 DJ |
363 | unsigned int event) |
364 | { | |
4471a34f | 365 | return cpufreq_governor_dbs(&cs_dbs_data, policy, event); |
b9170836 DJ |
366 | } |
367 | ||
c4d14bc0 SW |
368 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE |
369 | static | |
370 | #endif | |
1c256245 TR |
371 | struct cpufreq_governor cpufreq_gov_conservative = { |
372 | .name = "conservative", | |
4471a34f | 373 | .governor = cs_cpufreq_governor_dbs, |
1c256245 TR |
374 | .max_transition_latency = TRANSITION_LATENCY_LIMIT, |
375 | .owner = THIS_MODULE, | |
b9170836 DJ |
376 | }; |
377 | ||
378 | static int __init cpufreq_gov_dbs_init(void) | |
379 | { | |
4471a34f | 380 | mutex_init(&cs_dbs_data.mutex); |
57df5573 | 381 | return cpufreq_register_governor(&cpufreq_gov_conservative); |
b9170836 DJ |
382 | } |
383 | ||
384 | static void __exit cpufreq_gov_dbs_exit(void) | |
385 | { | |
1c256245 | 386 | cpufreq_unregister_governor(&cpufreq_gov_conservative); |
b9170836 DJ |
387 | } |
388 | ||
11a80a9c | 389 | MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>"); |
9acef487 | 390 | MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for " |
b9170836 DJ |
391 | "Low Latency Frequency Transition capable processors " |
392 | "optimised for use in a battery environment"); | |
9acef487 | 393 | MODULE_LICENSE("GPL"); |
b9170836 | 394 | |
6915719b JW |
395 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE |
396 | fs_initcall(cpufreq_gov_dbs_init); | |
397 | #else | |
b9170836 | 398 | module_init(cpufreq_gov_dbs_init); |
6915719b | 399 | #endif |
b9170836 | 400 | module_exit(cpufreq_gov_dbs_exit); |