Merge tag 'for-v4.8-rc' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux...
[deliverable/linux.git] / drivers / cpufreq / cpufreq_conservative.c
CommitLineData
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
7d5a9956
RW
17struct cs_policy_dbs_info {
18 struct policy_dbs_info policy_dbs;
19 unsigned int down_skip;
7d5a9956
RW
20};
21
22static inline struct cs_policy_dbs_info *to_dbs_info(struct policy_dbs_info *policy_dbs)
23{
24 return container_of(policy_dbs, struct cs_policy_dbs_info, policy_dbs);
25}
26
47ebaac1
RW
27struct cs_dbs_tuners {
28 unsigned int down_threshold;
29 unsigned int freq_step;
30};
31
c88883cd 32/* Conservative governor macros */
b9170836 33#define DEF_FREQUENCY_UP_THRESHOLD (80)
b9170836 34#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
98765847 35#define DEF_FREQUENCY_STEP (5)
2c906b31
AC
36#define DEF_SAMPLING_DOWN_FACTOR (1)
37#define MAX_SAMPLING_DOWN_FACTOR (10)
b9170836 38
98765847
SK
39static inline unsigned int get_freq_target(struct cs_dbs_tuners *cs_tuners,
40 struct cpufreq_policy *policy)
41{
42 unsigned int freq_target = (cs_tuners->freq_step * policy->max) / 100;
43
44 /* max freq cannot be less than 100. But who knows... */
45 if (unlikely(freq_target == 0))
46 freq_target = DEF_FREQUENCY_STEP;
47
48 return freq_target;
49}
50
4471a34f
VK
51/*
52 * Every sampling_rate, we check, if current idle time is less than 20%
7af1c056
SK
53 * (default), then we try to increase frequency. Every sampling_rate *
54 * sampling_down_factor, we check, if current idle time is more than 80%
55 * (default), then we try to decrease frequency
4471a34f
VK
56 *
57 * Any frequency increase takes it to the maximum frequency. Frequency reduction
58 * happens at minimum steps of 5% (default) of maximum frequency
59 */
4cccf755 60static unsigned int cs_dbs_timer(struct cpufreq_policy *policy)
a8d7c3bc 61{
bc505475 62 struct policy_dbs_info *policy_dbs = policy->governor_data;
7d5a9956 63 struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
bc505475 64 struct dbs_data *dbs_data = policy_dbs->dbs_data;
4d5dcc42 65 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
4cccf755 66 unsigned int load = dbs_update(policy);
4471a34f
VK
67
68 /*
69 * break out if we 'cannot' reduce the speed as the user might
70 * want freq_step to be zero
71 */
4d5dcc42 72 if (cs_tuners->freq_step == 0)
4cccf755 73 goto out;
4471a34f
VK
74
75 /* Check for frequency increase */
ff4b1789 76 if (load > dbs_data->up_threshold) {
d352cf47
RW
77 unsigned int requested_freq = policy->cur;
78
4471a34f
VK
79 dbs_info->down_skip = 0;
80
81 /* if we are already at full speed then break out early */
d352cf47 82 if (requested_freq == policy->max)
4cccf755 83 goto out;
4471a34f 84
d352cf47 85 requested_freq += get_freq_target(cs_tuners, policy);
6d7bcb14 86
d352cf47 87 __cpufreq_driver_target(policy, requested_freq, CPUFREQ_RELATION_H);
4cccf755 88 goto out;
4471a34f
VK
89 }
90
7af1c056 91 /* if sampling_down_factor is active break out early */
ff4b1789 92 if (++dbs_info->down_skip < dbs_data->sampling_down_factor)
4cccf755 93 goto out;
7af1c056
SK
94 dbs_info->down_skip = 0;
95
27ed3cd2
SK
96 /* Check for frequency decrease */
97 if (load < cs_tuners->down_threshold) {
d352cf47 98 unsigned int freq_target, requested_freq = policy->cur;
4471a34f
VK
99 /*
100 * if we cannot reduce the frequency anymore, break out early
101 */
d352cf47 102 if (requested_freq == policy->min)
4cccf755 103 goto out;
4471a34f 104
3baa976a 105 freq_target = get_freq_target(cs_tuners, policy);
d352cf47
RW
106 if (requested_freq > freq_target)
107 requested_freq -= freq_target;
3baa976a 108 else
d352cf47 109 requested_freq = policy->min;
ad529a9c 110
d352cf47 111 __cpufreq_driver_target(policy, requested_freq, CPUFREQ_RELATION_L);
4471a34f 112 }
43e0ee36 113
4cccf755 114 out:
07aa4402 115 return dbs_data->sampling_rate;
66df2a01
FB
116}
117
b9170836 118/************************** sysfs interface ************************/
b9170836 119
0dd3c1d6
RW
120static ssize_t store_sampling_down_factor(struct gov_attr_set *attr_set,
121 const char *buf, size_t count)
b9170836 122{
0dd3c1d6 123 struct dbs_data *dbs_data = to_dbs_data(attr_set);
b9170836
DJ
124 unsigned int input;
125 int ret;
9acef487 126 ret = sscanf(buf, "%u", &input);
8e677ce8 127
2c906b31 128 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
b9170836
DJ
129 return -EINVAL;
130
ff4b1789 131 dbs_data->sampling_down_factor = input;
b9170836
DJ
132 return count;
133}
134
0dd3c1d6
RW
135static ssize_t store_up_threshold(struct gov_attr_set *attr_set,
136 const char *buf, size_t count)
b9170836 137{
0dd3c1d6 138 struct dbs_data *dbs_data = to_dbs_data(attr_set);
4d5dcc42 139 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
b9170836
DJ
140 unsigned int input;
141 int ret;
9acef487 142 ret = sscanf(buf, "%u", &input);
b9170836 143
4d5dcc42 144 if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold)
b9170836 145 return -EINVAL;
b9170836 146
ff4b1789 147 dbs_data->up_threshold = input;
b9170836
DJ
148 return count;
149}
150
0dd3c1d6
RW
151static ssize_t store_down_threshold(struct gov_attr_set *attr_set,
152 const char *buf, size_t count)
b9170836 153{
0dd3c1d6 154 struct dbs_data *dbs_data = to_dbs_data(attr_set);
4d5dcc42 155 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
b9170836
DJ
156 unsigned int input;
157 int ret;
9acef487 158 ret = sscanf(buf, "%u", &input);
b9170836 159
8e677ce8
AC
160 /* cannot be lower than 11 otherwise freq will not fall */
161 if (ret != 1 || input < 11 || input > 100 ||
ff4b1789 162 input >= dbs_data->up_threshold)
b9170836 163 return -EINVAL;
b9170836 164
4d5dcc42 165 cs_tuners->down_threshold = input;
b9170836
DJ
166 return count;
167}
168
0dd3c1d6
RW
169static ssize_t store_ignore_nice_load(struct gov_attr_set *attr_set,
170 const char *buf, size_t count)
b9170836 171{
0dd3c1d6 172 struct dbs_data *dbs_data = to_dbs_data(attr_set);
a33cce1c 173 unsigned int input;
b9170836
DJ
174 int ret;
175
18a7247d
DJ
176 ret = sscanf(buf, "%u", &input);
177 if (ret != 1)
b9170836
DJ
178 return -EINVAL;
179
18a7247d 180 if (input > 1)
b9170836 181 input = 1;
18a7247d 182
ff4b1789 183 if (input == dbs_data->ignore_nice_load) /* nothing to do */
b9170836 184 return count;
326c86de 185
ff4b1789 186 dbs_data->ignore_nice_load = input;
b9170836 187
8e677ce8 188 /* we need to re-evaluate prev_cpu_idle */
8c8f77fd 189 gov_update_cpu_data(dbs_data);
a33cce1c 190
b9170836
DJ
191 return count;
192}
193
0dd3c1d6
RW
194static ssize_t store_freq_step(struct gov_attr_set *attr_set, const char *buf,
195 size_t count)
b9170836 196{
0dd3c1d6 197 struct dbs_data *dbs_data = to_dbs_data(attr_set);
4d5dcc42 198 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
b9170836
DJ
199 unsigned int input;
200 int ret;
18a7247d 201 ret = sscanf(buf, "%u", &input);
b9170836 202
18a7247d 203 if (ret != 1)
b9170836
DJ
204 return -EINVAL;
205
18a7247d 206 if (input > 100)
b9170836 207 input = 100;
18a7247d 208
4471a34f
VK
209 /*
210 * no need to test here if freq_step is zero as the user might actually
211 * want this, they would be crazy though :)
212 */
4d5dcc42 213 cs_tuners->freq_step = input;
b9170836
DJ
214 return count;
215}
216
c4435630
VK
217gov_show_one_common(sampling_rate);
218gov_show_one_common(sampling_down_factor);
219gov_show_one_common(up_threshold);
220gov_show_one_common(ignore_nice_load);
221gov_show_one_common(min_sampling_rate);
222gov_show_one(cs, down_threshold);
223gov_show_one(cs, freq_step);
224
225gov_attr_rw(sampling_rate);
226gov_attr_rw(sampling_down_factor);
227gov_attr_rw(up_threshold);
228gov_attr_rw(ignore_nice_load);
229gov_attr_ro(min_sampling_rate);
230gov_attr_rw(down_threshold);
231gov_attr_rw(freq_step);
232
233static struct attribute *cs_attributes[] = {
234 &min_sampling_rate.attr,
235 &sampling_rate.attr,
236 &sampling_down_factor.attr,
237 &up_threshold.attr,
238 &down_threshold.attr,
239 &ignore_nice_load.attr,
240 &freq_step.attr,
b9170836
DJ
241 NULL
242};
243
b9170836
DJ
244/************************** sysfs end ************************/
245
7d5a9956
RW
246static struct policy_dbs_info *cs_alloc(void)
247{
248 struct cs_policy_dbs_info *dbs_info;
249
250 dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
251 return dbs_info ? &dbs_info->policy_dbs : NULL;
252}
253
254static void cs_free(struct policy_dbs_info *policy_dbs)
255{
256 kfree(to_dbs_info(policy_dbs));
257}
258
9a15fb2c 259static int cs_init(struct dbs_data *dbs_data)
4d5dcc42
VK
260{
261 struct cs_dbs_tuners *tuners;
262
d5b73cd8 263 tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
a69d6b29 264 if (!tuners)
4d5dcc42 265 return -ENOMEM;
4d5dcc42 266
4d5dcc42 267 tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD;
98765847 268 tuners->freq_step = DEF_FREQUENCY_STEP;
ff4b1789
VK
269 dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
270 dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
271 dbs_data->ignore_nice_load = 0;
4d5dcc42
VK
272
273 dbs_data->tuners = tuners;
274 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
275 jiffies_to_usecs(10);
8e0484d2 276
4d5dcc42
VK
277 return 0;
278}
279
9a15fb2c 280static void cs_exit(struct dbs_data *dbs_data)
4d5dcc42
VK
281{
282 kfree(dbs_data->tuners);
283}
284
702c9e54
RW
285static void cs_start(struct cpufreq_policy *policy)
286{
7d5a9956 287 struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
702c9e54
RW
288
289 dbs_info->down_skip = 0;
702c9e54
RW
290}
291
d352cf47
RW
292static struct dbs_governor cs_governor = {
293 .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("conservative"),
294 .kobj_type = { .default_attrs = cs_attributes },
295 .gov_dbs_timer = cs_dbs_timer,
296 .alloc = cs_alloc,
297 .free = cs_free,
298 .init = cs_init,
299 .exit = cs_exit,
300 .start = cs_start,
4471a34f 301};
b9170836 302
d352cf47 303#define CPU_FREQ_GOV_CONSERVATIVE (&cs_governor.gov)
af926185 304
b9170836
DJ
305static int __init cpufreq_gov_dbs_init(void)
306{
af926185 307 return cpufreq_register_governor(CPU_FREQ_GOV_CONSERVATIVE);
b9170836
DJ
308}
309
310static void __exit cpufreq_gov_dbs_exit(void)
311{
af926185 312 cpufreq_unregister_governor(CPU_FREQ_GOV_CONSERVATIVE);
b9170836
DJ
313}
314
11a80a9c 315MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
9acef487 316MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
b9170836
DJ
317 "Low Latency Frequency Transition capable processors "
318 "optimised for use in a battery environment");
9acef487 319MODULE_LICENSE("GPL");
b9170836 320
6915719b 321#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
de1df26b
RW
322struct cpufreq_governor *cpufreq_default_governor(void)
323{
af926185 324 return CPU_FREQ_GOV_CONSERVATIVE;
de1df26b
RW
325}
326
6915719b
JW
327fs_initcall(cpufreq_gov_dbs_init);
328#else
b9170836 329module_init(cpufreq_gov_dbs_init);
6915719b 330#endif
b9170836 331module_exit(cpufreq_gov_dbs_exit);
This page took 0.78156 seconds and 5 git commands to generate.