xref: /linux/drivers/cpufreq/cpufreq_conservative.c (revision 486b257357c23743d9729ea715136d105fff461e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  drivers/cpufreq/cpufreq_conservative.c
4  *
5  *  Copyright (C)  2001 Russell King
6  *            (C)  2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
7  *                      Jun Nakajima <jun.nakajima@intel.com>
8  *            (C)  2009 Alexander Clouter <alex@digriz.org.uk>
9  */
10 
11 #include <linux/slab.h>
12 #include "cpufreq_governor.h"
13 
14 struct cs_policy_dbs_info {
15 	struct policy_dbs_info policy_dbs;
16 	unsigned int down_skip;
17 	unsigned int requested_freq;
18 };
19 
20 static inline struct cs_policy_dbs_info *to_dbs_info(struct policy_dbs_info *policy_dbs)
21 {
22 	return container_of(policy_dbs, struct cs_policy_dbs_info, policy_dbs);
23 }
24 
25 struct cs_dbs_tuners {
26 	unsigned int down_threshold;
27 	unsigned int freq_step;
28 };
29 
30 /* Conservative governor macros */
31 #define DEF_FREQUENCY_UP_THRESHOLD		(80)
32 #define DEF_FREQUENCY_DOWN_THRESHOLD		(20)
33 #define DEF_FREQUENCY_STEP			(5)
34 #define DEF_SAMPLING_DOWN_FACTOR		(1)
35 #define MAX_SAMPLING_DOWN_FACTOR		(10)
36 
37 static inline unsigned int get_freq_step(struct cs_dbs_tuners *cs_tuners,
38 					 struct cpufreq_policy *policy)
39 {
40 	unsigned int freq_step = (cs_tuners->freq_step * policy->max) / 100;
41 
42 	/* max freq cannot be less than 100. But who knows... */
43 	if (unlikely(freq_step == 0))
44 		freq_step = DEF_FREQUENCY_STEP;
45 
46 	return freq_step;
47 }
48 
49 /*
50  * Every sampling_rate, we check, if current idle time is less than 20%
51  * (default), then we try to increase frequency. Every sampling_rate *
52  * sampling_down_factor, we check, if current idle time is more than 80%
53  * (default), then we try to decrease frequency
54  *
55  * Frequency updates happen at minimum steps of 5% (default) of maximum
56  * frequency
57  */
58 static unsigned int cs_dbs_update(struct cpufreq_policy *policy)
59 {
60 	struct policy_dbs_info *policy_dbs = policy->governor_data;
61 	struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
62 	unsigned int requested_freq = dbs_info->requested_freq;
63 	struct dbs_data *dbs_data = policy_dbs->dbs_data;
64 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
65 	unsigned int load = dbs_update(policy);
66 	unsigned int freq_step;
67 
68 	/*
69 	 * break out if we 'cannot' reduce the speed as the user might
70 	 * want freq_step to be zero
71 	 */
72 	if (cs_tuners->freq_step == 0)
73 		goto out;
74 
75 	/*
76 	 * If requested_freq is out of range, it is likely that the limits
77 	 * changed in the meantime, so fall back to current frequency in that
78 	 * case.
79 	 */
80 	if (requested_freq > policy->max || requested_freq < policy->min) {
81 		requested_freq = policy->cur;
82 		dbs_info->requested_freq = requested_freq;
83 	}
84 
85 	freq_step = get_freq_step(cs_tuners, policy);
86 
87 	/*
88 	 * Apply deferred down steps only when the policy sample load is
89 	 * below down_threshold. Otherwise, multiple deferred down steps may
90 	 * cause a net frequency decrease outside the downscaling region.
91 	 */
92 	if (policy_dbs->max_sample_load < cs_tuners->down_threshold &&
93 	    policy_dbs->idle_periods < UINT_MAX) {
94 		unsigned int freq_steps = policy_dbs->idle_periods * freq_step;
95 
96 		if (requested_freq > policy->min + freq_steps)
97 			requested_freq -= freq_steps;
98 		else
99 			requested_freq = policy->min;
100 
101 		policy_dbs->idle_periods = UINT_MAX;
102 	}
103 
104 	/* Check for frequency increase */
105 	if (load > dbs_data->up_threshold) {
106 		dbs_info->down_skip = 0;
107 
108 		requested_freq += freq_step;
109 		if (requested_freq > policy->max)
110 			requested_freq = policy->max;
111 
112 		__cpufreq_driver_target(policy, requested_freq,
113 					CPUFREQ_RELATION_HE);
114 		dbs_info->requested_freq = requested_freq;
115 		goto out;
116 	}
117 
118 	/* if sampling_down_factor is active break out early */
119 	if (++dbs_info->down_skip < dbs_data->sampling_down_factor)
120 		goto out;
121 	dbs_info->down_skip = 0;
122 
123 	/* Check for frequency decrease */
124 	if (load < cs_tuners->down_threshold) {
125 		if (requested_freq > policy->min + freq_step)
126 			requested_freq -= freq_step;
127 		else
128 			requested_freq = policy->min;
129 
130 		__cpufreq_driver_target(policy, requested_freq,
131 					CPUFREQ_RELATION_LE);
132 		dbs_info->requested_freq = requested_freq;
133 	}
134 
135  out:
136 	return dbs_data->sampling_rate;
137 }
138 
139 /************************** sysfs interface ************************/
140 
141 static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
142 					  const char *buf, size_t count)
143 {
144 	struct dbs_data *dbs_data = to_dbs_data(attr_set);
145 	unsigned int input;
146 	int ret;
147 	ret = kstrtouint(buf, 0, &input);
148 
149 	if (ret || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
150 		return -EINVAL;
151 
152 	dbs_data->sampling_down_factor = input;
153 	return count;
154 }
155 
156 static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
157 				  const char *buf, size_t count)
158 {
159 	struct dbs_data *dbs_data = to_dbs_data(attr_set);
160 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
161 	unsigned int input;
162 	int ret;
163 	ret = kstrtouint(buf, 0, &input);
164 
165 	if (ret || input > 100 || input <= cs_tuners->down_threshold)
166 		return -EINVAL;
167 
168 	dbs_data->up_threshold = input;
169 	return count;
170 }
171 
172 static ssize_t down_threshold_store(struct gov_attr_set *attr_set,
173 				    const char *buf, size_t count)
174 {
175 	struct dbs_data *dbs_data = to_dbs_data(attr_set);
176 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
177 	unsigned int input;
178 	int ret;
179 	ret = kstrtouint(buf, 0, &input);
180 
181 	/* cannot be lower than 1 otherwise freq will not fall */
182 	if (ret || input < 1 || input >= dbs_data->up_threshold)
183 		return -EINVAL;
184 
185 	cs_tuners->down_threshold = input;
186 	return count;
187 }
188 
189 static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
190 				      const char *buf, size_t count)
191 {
192 	struct dbs_data *dbs_data = to_dbs_data(attr_set);
193 	unsigned int input;
194 	int ret;
195 
196 	ret = kstrtouint(buf, 0, &input);
197 	if (ret)
198 		return ret;
199 
200 	if (input > 1)
201 		input = 1;
202 
203 	if (input == dbs_data->ignore_nice_load) /* nothing to do */
204 		return count;
205 
206 	dbs_data->ignore_nice_load = input;
207 
208 	/* we need to re-evaluate prev_cpu_idle */
209 	gov_update_cpu_data(dbs_data);
210 
211 	return count;
212 }
213 
214 static ssize_t freq_step_store(struct gov_attr_set *attr_set, const char *buf,
215 			       size_t count)
216 {
217 	struct dbs_data *dbs_data = to_dbs_data(attr_set);
218 	struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
219 	unsigned int input;
220 	int ret;
221 	ret = kstrtouint(buf, 0, &input);
222 
223 	if (ret)
224 		return ret;
225 
226 	if (input > 100)
227 		input = 100;
228 
229 	/*
230 	 * no need to test here if freq_step is zero as the user might actually
231 	 * want this, they would be crazy though :)
232 	 */
233 	cs_tuners->freq_step = input;
234 	return count;
235 }
236 
237 gov_show_one_common(sampling_rate);
238 gov_show_one_common(sampling_down_factor);
239 gov_show_one_common(up_threshold);
240 gov_show_one_common(ignore_nice_load);
241 gov_show_one(cs, down_threshold);
242 gov_show_one(cs, freq_step);
243 
244 gov_attr_rw(sampling_rate);
245 gov_attr_rw(sampling_down_factor);
246 gov_attr_rw(up_threshold);
247 gov_attr_rw(ignore_nice_load);
248 gov_attr_rw(down_threshold);
249 gov_attr_rw(freq_step);
250 
251 static struct attribute *cs_attrs[] = {
252 	&sampling_rate.attr,
253 	&sampling_down_factor.attr,
254 	&up_threshold.attr,
255 	&down_threshold.attr,
256 	&ignore_nice_load.attr,
257 	&freq_step.attr,
258 	NULL
259 };
260 ATTRIBUTE_GROUPS(cs);
261 
262 /************************** sysfs end ************************/
263 
264 static struct policy_dbs_info *cs_alloc(void)
265 {
266 	struct cs_policy_dbs_info *dbs_info;
267 
268 	dbs_info = kzalloc_obj(*dbs_info);
269 	return dbs_info ? &dbs_info->policy_dbs : NULL;
270 }
271 
272 static void cs_free(struct policy_dbs_info *policy_dbs)
273 {
274 	kfree(to_dbs_info(policy_dbs));
275 }
276 
277 static int cs_init(struct dbs_data *dbs_data)
278 {
279 	struct cs_dbs_tuners *tuners;
280 
281 	tuners = kzalloc_obj(*tuners);
282 	if (!tuners)
283 		return -ENOMEM;
284 
285 	tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD;
286 	tuners->freq_step = DEF_FREQUENCY_STEP;
287 	dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
288 	dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
289 	dbs_data->ignore_nice_load = 0;
290 	dbs_data->tuners = tuners;
291 
292 	return 0;
293 }
294 
295 static void cs_exit(struct dbs_data *dbs_data)
296 {
297 	kfree(dbs_data->tuners);
298 }
299 
300 static void cs_start(struct cpufreq_policy *policy)
301 {
302 	struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
303 
304 	dbs_info->down_skip = 0;
305 	dbs_info->requested_freq = policy->cur;
306 }
307 
308 static void cs_limits(struct cpufreq_policy *policy)
309 {
310 	struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
311 
312 	/*
313 	 * The limits have changed, so may have the current frequency. Reset
314 	 * requested_freq to avoid any unintended outcomes due to the mismatch.
315 	 */
316 	dbs_info->requested_freq = policy->cur;
317 }
318 
319 static struct dbs_governor cs_governor = {
320 	.gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("conservative"),
321 	.kobj_type = { .default_groups = cs_groups },
322 	.gov_dbs_update = cs_dbs_update,
323 	.alloc = cs_alloc,
324 	.free = cs_free,
325 	.init = cs_init,
326 	.exit = cs_exit,
327 	.start = cs_start,
328 	.limits = cs_limits,
329 };
330 
331 #define CPU_FREQ_GOV_CONSERVATIVE	(cs_governor.gov)
332 
333 MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
334 MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
335 		"Low Latency Frequency Transition capable processors "
336 		"optimised for use in a battery environment");
337 MODULE_LICENSE("GPL");
338 
339 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
340 struct cpufreq_governor *cpufreq_default_governor(void)
341 {
342 	return &CPU_FREQ_GOV_CONSERVATIVE;
343 }
344 #endif
345 
346 cpufreq_governor_init(CPU_FREQ_GOV_CONSERVATIVE);
347 cpufreq_governor_exit(CPU_FREQ_GOV_CONSERVATIVE);
348