Merge tag 's390-6.10-7' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[linux-2.6-block.git] / drivers / cpufreq / cpufreq_ondemand.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
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>
1da177e4
LT
8 */
9
4471a34f
VK
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
5ff0a268 12#include <linux/cpu.h>
4471a34f 13#include <linux/percpu-defs.h>
4d5dcc42 14#include <linux/slab.h>
80800913 15#include <linux/tick.h>
55687da1 16#include <linux/sched/cpufreq.h>
7d5a9956
RW
17
18#include "cpufreq_ondemand.h"
1da177e4 19
06eb09d1 20/* On-demand governor macros */
1da177e4 21#define DEF_FREQUENCY_UP_THRESHOLD (80)
3f78a9f7
DN
22#define DEF_SAMPLING_DOWN_FACTOR (1)
23#define MAX_SAMPLING_DOWN_FACTOR (100000)
80800913 24#define MICRO_FREQUENCY_UP_THRESHOLD (95)
4dd63b49 25#define MIN_FREQUENCY_UP_THRESHOLD (1)
1da177e4
LT
26#define MAX_FREQUENCY_UP_THRESHOLD (100)
27
fb30809e
JS
28static struct od_ops od_ops;
29
c2837558
JS
30static unsigned int default_powersave_bias;
31
4471a34f
VK
32/*
33 * Not all CPUs want IO time to be accounted as busy; this depends on how
34 * efficient idling at a higher frequency/voltage is.
35 * Pavel Machek says this is not so for various generations of AMD and old
36 * Intel systems.
06eb09d1 37 * Mike Chan (android.com) claims this is also not true for ARM.
4471a34f
VK
38 * Because of this, whitelist specific known (series) of CPUs by default, and
39 * leave all others up to the user.
40 */
41static int should_io_be_busy(void)
42{
43#if defined(CONFIG_X86)
44 /*
06eb09d1 45 * For Intel, Core 2 (model 15) and later have an efficient idle.
4471a34f
VK
46 */
47 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
48 boot_cpu_data.x86 == 6 &&
49 boot_cpu_data.x86_model >= 15)
50 return 1;
51#endif
52 return 0;
6b8fcd90
AV
53}
54
05ca0350
AS
55/*
56 * Find right freq to be set now with powersave_bias on.
07aa4402
RW
57 * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
58 * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
05ca0350 59 */
fb30809e 60static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
4471a34f 61 unsigned int freq_next, unsigned int relation)
05ca0350
AS
62{
63 unsigned int freq_req, freq_reduc, freq_avg;
64 unsigned int freq_hi, freq_lo;
d218ed77 65 unsigned int index;
07aa4402 66 unsigned int delay_hi_us;
bc505475 67 struct policy_dbs_info *policy_dbs = policy->governor_data;
7d5a9956 68 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
bc505475 69 struct dbs_data *dbs_data = policy_dbs->dbs_data;
4d5dcc42 70 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
34ac5d7a 71 struct cpufreq_frequency_table *freq_table = policy->freq_table;
05ca0350 72
34ac5d7a 73 if (!freq_table) {
05ca0350 74 dbs_info->freq_lo = 0;
07aa4402 75 dbs_info->freq_lo_delay_us = 0;
05ca0350
AS
76 return freq_next;
77 }
78
d218ed77 79 index = cpufreq_frequency_table_target(policy, freq_next, relation);
34ac5d7a 80 freq_req = freq_table[index].frequency;
4d5dcc42 81 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
05ca0350
AS
82 freq_avg = freq_req - freq_reduc;
83
84 /* Find freq bounds for freq_avg in freq_table */
1f39fa0d
VD
85 index = cpufreq_table_find_index_h(policy, freq_avg,
86 relation & CPUFREQ_RELATION_E);
34ac5d7a 87 freq_lo = freq_table[index].frequency;
1f39fa0d
VD
88 index = cpufreq_table_find_index_l(policy, freq_avg,
89 relation & CPUFREQ_RELATION_E);
34ac5d7a 90 freq_hi = freq_table[index].frequency;
05ca0350
AS
91
92 /* Find out how long we have to be in hi and lo freqs */
93 if (freq_hi == freq_lo) {
94 dbs_info->freq_lo = 0;
07aa4402 95 dbs_info->freq_lo_delay_us = 0;
05ca0350
AS
96 return freq_lo;
97 }
07aa4402
RW
98 delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
99 delay_hi_us += (freq_hi - freq_lo) / 2;
100 delay_hi_us /= freq_hi - freq_lo;
101 dbs_info->freq_hi_delay_us = delay_hi_us;
05ca0350 102 dbs_info->freq_lo = freq_lo;
07aa4402 103 dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
05ca0350
AS
104 return freq_hi;
105}
106
d1db75ff 107static void ondemand_powersave_bias_init(struct cpufreq_policy *policy)
05ca0350 108{
7d5a9956 109 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
d1db75ff 110
d1db75ff 111 dbs_info->freq_lo = 0;
05ca0350
AS
112}
113
3a3e9e06 114static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
4471a34f 115{
bc505475
RW
116 struct policy_dbs_info *policy_dbs = policy->governor_data;
117 struct dbs_data *dbs_data = policy_dbs->dbs_data;
4d5dcc42
VK
118 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
119
120 if (od_tuners->powersave_bias)
3a3e9e06 121 freq = od_ops.powersave_bias_target(policy, freq,
b894d20e 122 CPUFREQ_RELATION_HE);
3a3e9e06 123 else if (policy->cur == policy->max)
4471a34f 124 return;
0e625ac1 125
3a3e9e06 126 __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
b894d20e 127 CPUFREQ_RELATION_LE : CPUFREQ_RELATION_HE);
4471a34f
VK
128}
129
130/*
131 * Every sampling_rate, we check, if current idle time is less than 20%
dfa5bb62
SK
132 * (default), then we try to increase frequency. Else, we adjust the frequency
133 * proportional to load.
4471a34f 134 */
4cccf755 135static void od_update(struct cpufreq_policy *policy)
1da177e4 136{
7d5a9956
RW
137 struct policy_dbs_info *policy_dbs = policy->governor_data;
138 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
bc505475 139 struct dbs_data *dbs_data = policy_dbs->dbs_data;
4d5dcc42 140 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
4cccf755 141 unsigned int load = dbs_update(policy);
4471a34f
VK
142
143 dbs_info->freq_lo = 0;
144
145 /* Check for frequency increase */
ff4b1789 146 if (load > dbs_data->up_threshold) {
4471a34f
VK
147 /* If switching to max speed, apply sampling_down_factor */
148 if (policy->cur < policy->max)
57dc3bcd 149 policy_dbs->rate_mult = dbs_data->sampling_down_factor;
4471a34f 150 dbs_freq_increase(policy, policy->max);
dfa5bb62
SK
151 } else {
152 /* Calculate the next frequency proportional to load */
6393d6a1
SK
153 unsigned int freq_next, min_f, max_f;
154
155 min_f = policy->cpuinfo.min_freq;
156 max_f = policy->cpuinfo.max_freq;
157 freq_next = min_f + load * (max_f - min_f) / 100;
4471a34f
VK
158
159 /* No longer fully busy, reset rate_mult */
57dc3bcd 160 policy_dbs->rate_mult = 1;
4471a34f 161
a7f35cff
RW
162 if (od_tuners->powersave_bias)
163 freq_next = od_ops.powersave_bias_target(policy,
164 freq_next,
b894d20e 165 CPUFREQ_RELATION_LE);
a7f35cff 166
b894d20e 167 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_CE);
4471a34f 168 }
1da177e4
LT
169}
170
26f0dbc9 171static unsigned int od_dbs_update(struct cpufreq_policy *policy)
4471a34f 172{
bc505475
RW
173 struct policy_dbs_info *policy_dbs = policy->governor_data;
174 struct dbs_data *dbs_data = policy_dbs->dbs_data;
7d5a9956 175 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
6e96c5b3 176 int sample_type = dbs_info->sample_type;
4447266b 177
4471a34f 178 /* Common NORMAL_SAMPLE setup */
43e0ee36 179 dbs_info->sample_type = OD_NORMAL_SAMPLE;
4cccf755
RW
180 /*
181 * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
182 * it then.
183 */
184 if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
43e0ee36 185 __cpufreq_driver_target(policy, dbs_info->freq_lo,
b894d20e 186 CPUFREQ_RELATION_HE);
07aa4402 187 return dbs_info->freq_lo_delay_us;
6e96c5b3
RW
188 }
189
190 od_update(policy);
191
192 if (dbs_info->freq_lo) {
26f0dbc9 193 /* Setup SUB_SAMPLE */
6e96c5b3 194 dbs_info->sample_type = OD_SUB_SAMPLE;
07aa4402 195 return dbs_info->freq_hi_delay_us;
4471a34f
VK
196 }
197
07aa4402 198 return dbs_data->sampling_rate * policy_dbs->rate_mult;
da53d61e
FB
199}
200
4471a34f 201/************************** sysfs interface ************************/
7bdad34d 202static struct dbs_governor od_dbs_gov;
1da177e4 203
85750bcd 204static ssize_t io_is_busy_store(struct gov_attr_set *attr_set, const char *buf,
0dd3c1d6 205 size_t count)
19379b11 206{
0dd3c1d6 207 struct dbs_data *dbs_data = to_dbs_data(attr_set);
19379b11
AV
208 unsigned int input;
209 int ret;
210
211 ret = sscanf(buf, "%u", &input);
212 if (ret != 1)
213 return -EINVAL;
8847e038 214 dbs_data->io_is_busy = !!input;
9366d840
SK
215
216 /* we need to re-evaluate prev_cpu_idle */
8c8f77fd 217 gov_update_cpu_data(dbs_data);
a33cce1c 218
19379b11
AV
219 return count;
220}
221
85750bcd 222static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
0dd3c1d6 223 const char *buf, size_t count)
1da177e4 224{
0dd3c1d6 225 struct dbs_data *dbs_data = to_dbs_data(attr_set);
1da177e4
LT
226 unsigned int input;
227 int ret;
ffac80e9 228 ret = sscanf(buf, "%u", &input);
1da177e4 229
32ee8c3e 230 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
c29f1403 231 input < MIN_FREQUENCY_UP_THRESHOLD) {
1da177e4
LT
232 return -EINVAL;
233 }
4bd4e428 234
ff4b1789 235 dbs_data->up_threshold = input;
1da177e4
LT
236 return count;
237}
238
85750bcd 239static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
0dd3c1d6 240 const char *buf, size_t count)
3f78a9f7 241{
0dd3c1d6 242 struct dbs_data *dbs_data = to_dbs_data(attr_set);
57dc3bcd
RW
243 struct policy_dbs_info *policy_dbs;
244 unsigned int input;
3f78a9f7
DN
245 int ret;
246 ret = sscanf(buf, "%u", &input);
247
248 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
249 return -EINVAL;
57dc3bcd 250
ff4b1789 251 dbs_data->sampling_down_factor = input;
3f78a9f7
DN
252
253 /* Reset down sampling multiplier in case it was active */
0dd3c1d6 254 list_for_each_entry(policy_dbs, &attr_set->policy_list, list) {
57dc3bcd
RW
255 /*
256 * Doing this without locking might lead to using different
26f0dbc9 257 * rate_mult values in od_update() and od_dbs_update().
57dc3bcd 258 */
26f0dbc9 259 mutex_lock(&policy_dbs->update_mutex);
57dc3bcd 260 policy_dbs->rate_mult = 1;
26f0dbc9 261 mutex_unlock(&policy_dbs->update_mutex);
3f78a9f7 262 }
57dc3bcd 263
3f78a9f7
DN
264 return count;
265}
266
85750bcd 267static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
0dd3c1d6 268 const char *buf, size_t count)
3d5ee9e5 269{
0dd3c1d6 270 struct dbs_data *dbs_data = to_dbs_data(attr_set);
3d5ee9e5
DJ
271 unsigned int input;
272 int ret;
273
ffac80e9 274 ret = sscanf(buf, "%u", &input);
2b03f891 275 if (ret != 1)
3d5ee9e5
DJ
276 return -EINVAL;
277
2b03f891 278 if (input > 1)
3d5ee9e5 279 input = 1;
32ee8c3e 280
ff4b1789 281 if (input == dbs_data->ignore_nice_load) { /* nothing to do */
3d5ee9e5
DJ
282 return count;
283 }
ff4b1789 284 dbs_data->ignore_nice_load = input;
3d5ee9e5 285
ccb2fe20 286 /* we need to re-evaluate prev_cpu_idle */
8c8f77fd 287 gov_update_cpu_data(dbs_data);
1ca3abdb 288
3d5ee9e5
DJ
289 return count;
290}
291
85750bcd 292static ssize_t powersave_bias_store(struct gov_attr_set *attr_set,
0dd3c1d6 293 const char *buf, size_t count)
05ca0350 294{
0dd3c1d6 295 struct dbs_data *dbs_data = to_dbs_data(attr_set);
4d5dcc42 296 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
d1db75ff 297 struct policy_dbs_info *policy_dbs;
05ca0350
AS
298 unsigned int input;
299 int ret;
300 ret = sscanf(buf, "%u", &input);
301
302 if (ret != 1)
303 return -EINVAL;
304
305 if (input > 1000)
306 input = 1000;
307
4d5dcc42 308 od_tuners->powersave_bias = input;
d1db75ff 309
0dd3c1d6 310 list_for_each_entry(policy_dbs, &attr_set->policy_list, list)
d1db75ff
RW
311 ondemand_powersave_bias_init(policy_dbs->policy);
312
05ca0350
AS
313 return count;
314}
315
c4435630
VK
316gov_show_one_common(sampling_rate);
317gov_show_one_common(up_threshold);
318gov_show_one_common(sampling_down_factor);
319gov_show_one_common(ignore_nice_load);
8847e038 320gov_show_one_common(io_is_busy);
c4435630
VK
321gov_show_one(od, powersave_bias);
322
323gov_attr_rw(sampling_rate);
324gov_attr_rw(io_is_busy);
325gov_attr_rw(up_threshold);
326gov_attr_rw(sampling_down_factor);
327gov_attr_rw(ignore_nice_load);
328gov_attr_rw(powersave_bias);
c4435630 329
fe262d5c 330static struct attribute *od_attrs[] = {
c4435630
VK
331 &sampling_rate.attr,
332 &up_threshold.attr,
333 &sampling_down_factor.attr,
334 &ignore_nice_load.attr,
335 &powersave_bias.attr,
336 &io_is_busy.attr,
1da177e4
LT
337 NULL
338};
fe262d5c 339ATTRIBUTE_GROUPS(od);
1da177e4 340
1da177e4
LT
341/************************** sysfs end ************************/
342
7d5a9956
RW
343static struct policy_dbs_info *od_alloc(void)
344{
345 struct od_policy_dbs_info *dbs_info;
346
347 dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
348 return dbs_info ? &dbs_info->policy_dbs : NULL;
349}
350
351static void od_free(struct policy_dbs_info *policy_dbs)
352{
353 kfree(to_dbs_info(policy_dbs));
354}
355
9a15fb2c 356static int od_init(struct dbs_data *dbs_data)
4d5dcc42
VK
357{
358 struct od_dbs_tuners *tuners;
359 u64 idle_time;
360 int cpu;
361
d5b73cd8 362 tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
a69d6b29 363 if (!tuners)
4d5dcc42 364 return -ENOMEM;
4d5dcc42
VK
365
366 cpu = get_cpu();
367 idle_time = get_cpu_idle_time_us(cpu, NULL);
368 put_cpu();
369 if (idle_time != -1ULL) {
370 /* Idle micro accounting is supported. Use finer thresholds */
ff4b1789 371 dbs_data->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
4d5dcc42 372 } else {
ff4b1789 373 dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
4d5dcc42
VK
374 }
375
ff4b1789
VK
376 dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
377 dbs_data->ignore_nice_load = 0;
c2837558 378 tuners->powersave_bias = default_powersave_bias;
8847e038 379 dbs_data->io_is_busy = should_io_be_busy();
4d5dcc42
VK
380
381 dbs_data->tuners = tuners;
4d5dcc42
VK
382 return 0;
383}
384
9a15fb2c 385static void od_exit(struct dbs_data *dbs_data)
4d5dcc42
VK
386{
387 kfree(dbs_data->tuners);
388}
389
702c9e54
RW
390static void od_start(struct cpufreq_policy *policy)
391{
7d5a9956 392 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
702c9e54
RW
393
394 dbs_info->sample_type = OD_NORMAL_SAMPLE;
d1db75ff 395 ondemand_powersave_bias_init(policy);
702c9e54
RW
396}
397
4471a34f 398static struct od_ops od_ops = {
fb30809e 399 .powersave_bias_target = generic_powersave_bias_target,
4471a34f 400};
2f8a835c 401
7bdad34d 402static struct dbs_governor od_dbs_gov = {
e788892b 403 .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("ondemand"),
fe262d5c 404 .kobj_type = { .default_groups = od_groups },
26f0dbc9 405 .gov_dbs_update = od_dbs_update,
7d5a9956
RW
406 .alloc = od_alloc,
407 .free = od_free,
4d5dcc42
VK
408 .init = od_init,
409 .exit = od_exit,
702c9e54 410 .start = od_start,
4471a34f 411};
1da177e4 412
10dd8573 413#define CPU_FREQ_GOV_ONDEMAND (od_dbs_gov.gov)
af926185 414
fb30809e
JS
415static void od_set_powersave_bias(unsigned int powersave_bias)
416{
fb30809e 417 unsigned int cpu;
3e5c04f9
ZL
418 cpumask_var_t done;
419
420 if (!alloc_cpumask_var(&done, GFP_KERNEL))
421 return;
fb30809e 422
c2837558 423 default_powersave_bias = powersave_bias;
3e5c04f9 424 cpumask_clear(done);
fb30809e 425
09681a07 426 cpus_read_lock();
fb30809e 427 for_each_online_cpu(cpu) {
8c8f77fd 428 struct cpufreq_policy *policy;
e40e7b25 429 struct policy_dbs_info *policy_dbs;
8c8f77fd
RW
430 struct dbs_data *dbs_data;
431 struct od_dbs_tuners *od_tuners;
44152cb8 432
3e5c04f9 433 if (cpumask_test_cpu(cpu, done))
fb30809e
JS
434 continue;
435
8c8f77fd 436 policy = cpufreq_cpu_get_raw(cpu);
10dd8573 437 if (!policy || policy->governor != &CPU_FREQ_GOV_ONDEMAND)
8c8f77fd
RW
438 continue;
439
440 policy_dbs = policy->governor_data;
e40e7b25 441 if (!policy_dbs)
c2837558 442 continue;
fb30809e 443
3e5c04f9 444 cpumask_or(done, done, policy->cpus);
c2837558 445
bc505475 446 dbs_data = policy_dbs->dbs_data;
c2837558
JS
447 od_tuners = dbs_data->tuners;
448 od_tuners->powersave_bias = default_powersave_bias;
fb30809e 449 }
09681a07 450 cpus_read_unlock();
3e5c04f9
ZL
451
452 free_cpumask_var(done);
fb30809e
JS
453}
454
455void od_register_powersave_bias_handler(unsigned int (*f)
456 (struct cpufreq_policy *, unsigned int, unsigned int),
457 unsigned int powersave_bias)
458{
459 od_ops.powersave_bias_target = f;
460 od_set_powersave_bias(powersave_bias);
461}
462EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
463
464void od_unregister_powersave_bias_handler(void)
465{
466 od_ops.powersave_bias_target = generic_powersave_bias_target;
467 od_set_powersave_bias(0);
468}
469EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
470
ffac80e9
VP
471MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
472MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
473MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
2b03f891 474 "Low Latency Frequency Transition capable processors");
ffac80e9 475MODULE_LICENSE("GPL");
1da177e4 476
6915719b 477#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
de1df26b
RW
478struct cpufreq_governor *cpufreq_default_governor(void)
479{
10dd8573 480 return &CPU_FREQ_GOV_ONDEMAND;
de1df26b 481}
6915719b 482#endif
10dd8573
QP
483
484cpufreq_governor_init(CPU_FREQ_GOV_ONDEMAND);
485cpufreq_governor_exit(CPU_FREQ_GOV_ONDEMAND);