1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/cpufreq/cpufreq_ondemand.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 */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/cpu.h>
13 #include <linux/percpu-defs.h>
14 #include <linux/slab.h>
15 #include <linux/tick.h>
16 #include <linux/sched/cpufreq.h>
17
18 #include "cpufreq_ondemand.h"
19
20 /* On-demand governor macros */
21 #define DEF_FREQUENCY_UP_THRESHOLD (80)
22 #define DEF_SAMPLING_DOWN_FACTOR (1)
23 #define MAX_SAMPLING_DOWN_FACTOR (100000)
24 #define MICRO_FREQUENCY_UP_THRESHOLD (95)
25 #define MIN_FREQUENCY_UP_THRESHOLD (1)
26 #define MAX_FREQUENCY_UP_THRESHOLD (100)
27
28 static struct od_ops od_ops;
29
30 static unsigned int default_powersave_bias;
31
32 /*
33 * Find right freq to be set now with powersave_bias on.
34 * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
35 * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
36 */
generic_powersave_bias_target(struct cpufreq_policy * policy,unsigned int freq_next,unsigned int relation)37 static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
38 unsigned int freq_next, unsigned int relation)
39 {
40 unsigned int freq_req, freq_reduc, freq_avg;
41 unsigned int freq_hi, freq_lo;
42 unsigned int index;
43 unsigned int delay_hi_us;
44 struct policy_dbs_info *policy_dbs = policy->governor_data;
45 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
46 struct dbs_data *dbs_data = policy_dbs->dbs_data;
47 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
48 struct cpufreq_frequency_table *freq_table = policy->freq_table;
49
50 if (!freq_table) {
51 dbs_info->freq_lo = 0;
52 dbs_info->freq_lo_delay_us = 0;
53 return freq_next;
54 }
55
56 index = cpufreq_frequency_table_target(policy, freq_next, policy->min,
57 policy->max, relation);
58 freq_req = freq_table[index].frequency;
59 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
60 freq_avg = freq_req - freq_reduc;
61
62 /* Find freq bounds for freq_avg in freq_table */
63 index = cpufreq_table_find_index_h(policy, freq_avg,
64 relation & CPUFREQ_RELATION_E);
65 freq_lo = freq_table[index].frequency;
66 index = cpufreq_table_find_index_l(policy, freq_avg,
67 relation & CPUFREQ_RELATION_E);
68 freq_hi = freq_table[index].frequency;
69
70 /* Find out how long we have to be in hi and lo freqs */
71 if (freq_hi == freq_lo) {
72 dbs_info->freq_lo = 0;
73 dbs_info->freq_lo_delay_us = 0;
74 return freq_lo;
75 }
76 delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
77 delay_hi_us += (freq_hi - freq_lo) / 2;
78 delay_hi_us /= freq_hi - freq_lo;
79 dbs_info->freq_hi_delay_us = delay_hi_us;
80 dbs_info->freq_lo = freq_lo;
81 dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
82 return freq_hi;
83 }
84
ondemand_powersave_bias_init(struct cpufreq_policy * policy)85 static void ondemand_powersave_bias_init(struct cpufreq_policy *policy)
86 {
87 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
88
89 dbs_info->freq_lo = 0;
90 }
91
dbs_freq_increase(struct cpufreq_policy * policy,unsigned int freq)92 static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
93 {
94 struct policy_dbs_info *policy_dbs = policy->governor_data;
95 struct dbs_data *dbs_data = policy_dbs->dbs_data;
96 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
97
98 if (od_tuners->powersave_bias)
99 freq = od_ops.powersave_bias_target(policy, freq,
100 CPUFREQ_RELATION_HE);
101 else if (policy->cur == policy->max)
102 return;
103
104 __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
105 CPUFREQ_RELATION_LE : CPUFREQ_RELATION_HE);
106 }
107
108 /*
109 * Every sampling_rate, we check, if current idle time is less than 20%
110 * (default), then we try to increase frequency. Else, we adjust the frequency
111 * proportional to load.
112 */
od_update(struct cpufreq_policy * policy)113 static void od_update(struct cpufreq_policy *policy)
114 {
115 struct policy_dbs_info *policy_dbs = policy->governor_data;
116 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
117 struct dbs_data *dbs_data = policy_dbs->dbs_data;
118 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
119 unsigned int load = dbs_update(policy);
120
121 dbs_info->freq_lo = 0;
122
123 /* Check for frequency increase */
124 if (load > dbs_data->up_threshold) {
125 /* If switching to max speed, apply sampling_down_factor */
126 if (policy->cur < policy->max)
127 policy_dbs->rate_mult = dbs_data->sampling_down_factor;
128 dbs_freq_increase(policy, policy->max);
129 } else {
130 /* Calculate the next frequency proportional to load */
131 unsigned int freq_next, min_f, max_f;
132
133 min_f = policy->cpuinfo.min_freq;
134 max_f = policy->cpuinfo.max_freq;
135 freq_next = min_f + load * (max_f - min_f) / 100;
136
137 /* No longer fully busy, reset rate_mult */
138 policy_dbs->rate_mult = 1;
139
140 if (od_tuners->powersave_bias)
141 freq_next = od_ops.powersave_bias_target(policy,
142 freq_next,
143 CPUFREQ_RELATION_LE);
144
145 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_CE);
146 }
147 }
148
od_dbs_update(struct cpufreq_policy * policy)149 static unsigned int od_dbs_update(struct cpufreq_policy *policy)
150 {
151 struct policy_dbs_info *policy_dbs = policy->governor_data;
152 struct dbs_data *dbs_data = policy_dbs->dbs_data;
153 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
154 int sample_type = dbs_info->sample_type;
155
156 /* Common NORMAL_SAMPLE setup */
157 dbs_info->sample_type = OD_NORMAL_SAMPLE;
158 /*
159 * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
160 * it then.
161 */
162 if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
163 __cpufreq_driver_target(policy, dbs_info->freq_lo,
164 CPUFREQ_RELATION_HE);
165 return dbs_info->freq_lo_delay_us;
166 }
167
168 od_update(policy);
169
170 if (dbs_info->freq_lo) {
171 /* Setup SUB_SAMPLE */
172 dbs_info->sample_type = OD_SUB_SAMPLE;
173 return dbs_info->freq_hi_delay_us;
174 }
175
176 return dbs_data->sampling_rate * policy_dbs->rate_mult;
177 }
178
179 /************************** sysfs interface ************************/
180 static struct dbs_governor od_dbs_gov;
181
io_is_busy_store(struct gov_attr_set * attr_set,const char * buf,size_t count)182 static ssize_t io_is_busy_store(struct gov_attr_set *attr_set, const char *buf,
183 size_t count)
184 {
185 struct dbs_data *dbs_data = to_dbs_data(attr_set);
186 unsigned int input;
187 int ret;
188
189 ret = sscanf(buf, "%u", &input);
190 if (ret != 1)
191 return -EINVAL;
192 dbs_data->io_is_busy = !!input;
193
194 /* we need to re-evaluate prev_cpu_idle */
195 gov_update_cpu_data(dbs_data);
196
197 return count;
198 }
199
up_threshold_store(struct gov_attr_set * attr_set,const char * buf,size_t count)200 static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
201 const char *buf, size_t count)
202 {
203 struct dbs_data *dbs_data = to_dbs_data(attr_set);
204 unsigned int input;
205 int ret;
206 ret = sscanf(buf, "%u", &input);
207
208 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
209 input < MIN_FREQUENCY_UP_THRESHOLD) {
210 return -EINVAL;
211 }
212
213 dbs_data->up_threshold = input;
214 return count;
215 }
216
sampling_down_factor_store(struct gov_attr_set * attr_set,const char * buf,size_t count)217 static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
218 const char *buf, size_t count)
219 {
220 struct dbs_data *dbs_data = to_dbs_data(attr_set);
221 struct policy_dbs_info *policy_dbs;
222 unsigned int input;
223 int ret;
224 ret = sscanf(buf, "%u", &input);
225
226 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
227 return -EINVAL;
228
229 dbs_data->sampling_down_factor = input;
230
231 /* Reset down sampling multiplier in case it was active */
232 list_for_each_entry(policy_dbs, &attr_set->policy_list, list) {
233 /*
234 * Doing this without locking might lead to using different
235 * rate_mult values in od_update() and od_dbs_update().
236 */
237 mutex_lock(&policy_dbs->update_mutex);
238 policy_dbs->rate_mult = 1;
239 mutex_unlock(&policy_dbs->update_mutex);
240 }
241
242 return count;
243 }
244
ignore_nice_load_store(struct gov_attr_set * attr_set,const char * buf,size_t count)245 static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
246 const char *buf, size_t count)
247 {
248 struct dbs_data *dbs_data = to_dbs_data(attr_set);
249 unsigned int input;
250 int ret;
251
252 ret = sscanf(buf, "%u", &input);
253 if (ret != 1)
254 return -EINVAL;
255
256 if (input > 1)
257 input = 1;
258
259 if (input == dbs_data->ignore_nice_load) { /* nothing to do */
260 return count;
261 }
262 dbs_data->ignore_nice_load = input;
263
264 /* we need to re-evaluate prev_cpu_idle */
265 gov_update_cpu_data(dbs_data);
266
267 return count;
268 }
269
powersave_bias_store(struct gov_attr_set * attr_set,const char * buf,size_t count)270 static ssize_t powersave_bias_store(struct gov_attr_set *attr_set,
271 const char *buf, size_t count)
272 {
273 struct dbs_data *dbs_data = to_dbs_data(attr_set);
274 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
275 struct policy_dbs_info *policy_dbs;
276 unsigned int input;
277 int ret;
278 ret = sscanf(buf, "%u", &input);
279
280 if (ret != 1)
281 return -EINVAL;
282
283 if (input > 1000)
284 input = 1000;
285
286 od_tuners->powersave_bias = input;
287
288 list_for_each_entry(policy_dbs, &attr_set->policy_list, list)
289 ondemand_powersave_bias_init(policy_dbs->policy);
290
291 return count;
292 }
293
294 gov_show_one_common(sampling_rate);
295 gov_show_one_common(up_threshold);
296 gov_show_one_common(sampling_down_factor);
297 gov_show_one_common(ignore_nice_load);
298 gov_show_one_common(io_is_busy);
299 gov_show_one(od, powersave_bias);
300
301 gov_attr_rw(sampling_rate);
302 gov_attr_rw(io_is_busy);
303 gov_attr_rw(up_threshold);
304 gov_attr_rw(sampling_down_factor);
305 gov_attr_rw(ignore_nice_load);
306 gov_attr_rw(powersave_bias);
307
308 static struct attribute *od_attrs[] = {
309 &sampling_rate.attr,
310 &up_threshold.attr,
311 &sampling_down_factor.attr,
312 &ignore_nice_load.attr,
313 &powersave_bias.attr,
314 &io_is_busy.attr,
315 NULL
316 };
317 ATTRIBUTE_GROUPS(od);
318
319 /************************** sysfs end ************************/
320
od_alloc(void)321 static struct policy_dbs_info *od_alloc(void)
322 {
323 struct od_policy_dbs_info *dbs_info;
324
325 dbs_info = kzalloc_obj(*dbs_info);
326 return dbs_info ? &dbs_info->policy_dbs : NULL;
327 }
328
od_free(struct policy_dbs_info * policy_dbs)329 static void od_free(struct policy_dbs_info *policy_dbs)
330 {
331 kfree(to_dbs_info(policy_dbs));
332 }
333
od_init(struct dbs_data * dbs_data)334 static int od_init(struct dbs_data *dbs_data)
335 {
336 struct od_dbs_tuners *tuners;
337
338 tuners = kzalloc_obj(*tuners);
339 if (!tuners)
340 return -ENOMEM;
341
342 if (tick_nohz_is_active()) {
343 /* Idle micro accounting is supported. Use finer thresholds */
344 dbs_data->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
345 } else {
346 dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
347 }
348
349 dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
350 dbs_data->ignore_nice_load = 0;
351 tuners->powersave_bias = default_powersave_bias;
352 dbs_data->io_is_busy = od_should_io_be_busy();
353
354 dbs_data->tuners = tuners;
355 return 0;
356 }
357
od_exit(struct dbs_data * dbs_data)358 static void od_exit(struct dbs_data *dbs_data)
359 {
360 kfree(dbs_data->tuners);
361 }
362
od_start(struct cpufreq_policy * policy)363 static void od_start(struct cpufreq_policy *policy)
364 {
365 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
366
367 dbs_info->sample_type = OD_NORMAL_SAMPLE;
368 ondemand_powersave_bias_init(policy);
369 }
370
371 static struct od_ops od_ops = {
372 .powersave_bias_target = generic_powersave_bias_target,
373 };
374
375 static struct dbs_governor od_dbs_gov = {
376 .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("ondemand"),
377 .kobj_type = { .default_groups = od_groups },
378 .gov_dbs_update = od_dbs_update,
379 .alloc = od_alloc,
380 .free = od_free,
381 .init = od_init,
382 .exit = od_exit,
383 .start = od_start,
384 };
385
386 #define CPU_FREQ_GOV_ONDEMAND (od_dbs_gov.gov)
387
od_set_powersave_bias(unsigned int powersave_bias)388 static void od_set_powersave_bias(unsigned int powersave_bias)
389 {
390 unsigned int cpu;
391 cpumask_var_t done;
392
393 if (!alloc_cpumask_var(&done, GFP_KERNEL))
394 return;
395
396 default_powersave_bias = powersave_bias;
397 cpumask_clear(done);
398
399 cpus_read_lock();
400 for_each_online_cpu(cpu) {
401 struct cpufreq_policy *policy;
402 struct policy_dbs_info *policy_dbs;
403 struct dbs_data *dbs_data;
404 struct od_dbs_tuners *od_tuners;
405
406 if (cpumask_test_cpu(cpu, done))
407 continue;
408
409 policy = cpufreq_cpu_get_raw(cpu);
410 if (!policy || policy->governor != &CPU_FREQ_GOV_ONDEMAND)
411 continue;
412
413 policy_dbs = policy->governor_data;
414 if (!policy_dbs)
415 continue;
416
417 cpumask_or(done, done, policy->cpus);
418
419 dbs_data = policy_dbs->dbs_data;
420 od_tuners = dbs_data->tuners;
421 od_tuners->powersave_bias = default_powersave_bias;
422 }
423 cpus_read_unlock();
424
425 free_cpumask_var(done);
426 }
427
od_register_powersave_bias_handler(unsigned int (* f)(struct cpufreq_policy *,unsigned int,unsigned int),unsigned int powersave_bias)428 void od_register_powersave_bias_handler(unsigned int (*f)
429 (struct cpufreq_policy *, unsigned int, unsigned int),
430 unsigned int powersave_bias)
431 {
432 od_ops.powersave_bias_target = f;
433 od_set_powersave_bias(powersave_bias);
434 }
435 EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
436
od_unregister_powersave_bias_handler(void)437 void od_unregister_powersave_bias_handler(void)
438 {
439 od_ops.powersave_bias_target = generic_powersave_bias_target;
440 od_set_powersave_bias(0);
441 }
442 EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
443
444 MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
445 MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
446 MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
447 "Low Latency Frequency Transition capable processors");
448 MODULE_LICENSE("GPL");
449
450 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
cpufreq_default_governor(void)451 struct cpufreq_governor *cpufreq_default_governor(void)
452 {
453 return &CPU_FREQ_GOV_ONDEMAND;
454 }
455 #endif
456
457 cpufreq_governor_init(CPU_FREQ_GOV_ONDEMAND);
458 cpufreq_governor_exit(CPU_FREQ_GOV_ONDEMAND);
459