blob: ac361a8b1d3bb4cafe3e68e74445e91032c8498f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Viresh Kumar4471a342012-10-26 00:47:42 +020010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Viresh Kumar5ff0a262013-08-06 22:53:03 +053012#include <linux/cpu.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020013#include <linux/percpu-defs.h>
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000014#include <linux/slab.h>
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070015#include <linux/tick.h>
Ingo Molnar55687da2017-02-08 18:51:31 +010016#include <linux/sched/cpufreq.h>
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +010017
18#include "cpufreq_ondemand.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000020/* On-demand governor macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define DEF_FREQUENCY_UP_THRESHOLD (80)
David C Niemi3f78a9f2010-10-06 16:54:24 -040022#define DEF_SAMPLING_DOWN_FACTOR (1)
23#define MAX_SAMPLING_DOWN_FACTOR (100000)
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070024#define MICRO_FREQUENCY_UP_THRESHOLD (95)
Thomas Renningercef96152009-04-22 13:48:29 +020025#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
Chen Yu4dd63b42016-01-04 12:14:45 +080026#define MIN_FREQUENCY_UP_THRESHOLD (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define MAX_FREQUENCY_UP_THRESHOLD (100)
28
Jacob Shinfb308092013-04-02 09:56:56 -050029static struct od_ops od_ops;
30
Jacob Shinc2837552013-06-25 22:42:37 +020031static unsigned int default_powersave_bias;
32
Viresh Kumar4471a342012-10-26 00:47:42 +020033/*
34 * Not all CPUs want IO time to be accounted as busy; this depends on how
35 * efficient idling at a higher frequency/voltage is.
36 * Pavel Machek says this is not so for various generations of AMD and old
37 * Intel systems.
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000038 * Mike Chan (android.com) claims this is also not true for ARM.
Viresh Kumar4471a342012-10-26 00:47:42 +020039 * Because of this, whitelist specific known (series) of CPUs by default, and
40 * leave all others up to the user.
41 */
42static int should_io_be_busy(void)
43{
44#if defined(CONFIG_X86)
45 /*
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000046 * For Intel, Core 2 (model 15) and later have an efficient idle.
Viresh Kumar4471a342012-10-26 00:47:42 +020047 */
48 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
49 boot_cpu_data.x86 == 6 &&
50 boot_cpu_data.x86_model >= 15)
51 return 1;
52#endif
53 return 0;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070054}
55
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040056/*
57 * Find right freq to be set now with powersave_bias on.
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +010058 * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
59 * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040060 */
Jacob Shinfb308092013-04-02 09:56:56 -050061static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
Viresh Kumar4471a342012-10-26 00:47:42 +020062 unsigned int freq_next, unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040063{
64 unsigned int freq_req, freq_reduc, freq_avg;
65 unsigned int freq_hi, freq_lo;
Viresh Kumard218ed72016-06-03 10:58:51 +053066 unsigned int index;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +010067 unsigned int delay_hi_us;
Rafael J. Wysockibc505472016-02-07 16:24:26 +010068 struct policy_dbs_info *policy_dbs = policy->governor_data;
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +010069 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
Rafael J. Wysockibc505472016-02-07 16:24:26 +010070 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000071 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar34ac5d72016-06-03 10:58:48 +053072 struct cpufreq_frequency_table *freq_table = policy->freq_table;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040073
Viresh Kumar34ac5d72016-06-03 10:58:48 +053074 if (!freq_table) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040075 dbs_info->freq_lo = 0;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +010076 dbs_info->freq_lo_delay_us = 0;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040077 return freq_next;
78 }
79
Viresh Kumard218ed72016-06-03 10:58:51 +053080 index = cpufreq_frequency_table_target(policy, freq_next, relation);
Viresh Kumar34ac5d72016-06-03 10:58:48 +053081 freq_req = freq_table[index].frequency;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000082 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040083 freq_avg = freq_req - freq_reduc;
84
85 /* Find freq bounds for freq_avg in freq_table */
Viresh Kumar82577362016-06-27 09:59:34 +053086 index = cpufreq_table_find_index_h(policy, freq_avg);
Viresh Kumar34ac5d72016-06-03 10:58:48 +053087 freq_lo = freq_table[index].frequency;
Viresh Kumar82577362016-06-27 09:59:34 +053088 index = cpufreq_table_find_index_l(policy, freq_avg);
Viresh Kumar34ac5d72016-06-03 10:58:48 +053089 freq_hi = freq_table[index].frequency;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040090
91 /* Find out how long we have to be in hi and lo freqs */
92 if (freq_hi == freq_lo) {
93 dbs_info->freq_lo = 0;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +010094 dbs_info->freq_lo_delay_us = 0;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040095 return freq_lo;
96 }
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +010097 delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
98 delay_hi_us += (freq_hi - freq_lo) / 2;
99 delay_hi_us /= freq_hi - freq_lo;
100 dbs_info->freq_hi_delay_us = delay_hi_us;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400101 dbs_info->freq_lo = freq_lo;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100102 dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400103 return freq_hi;
104}
105
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100106static void ondemand_powersave_bias_init(struct cpufreq_policy *policy)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400107{
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100108 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100109
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100110 dbs_info->freq_lo = 0;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400111}
112
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530113static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
Viresh Kumar4471a342012-10-26 00:47:42 +0200114{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100115 struct policy_dbs_info *policy_dbs = policy->governor_data;
116 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000117 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
118
119 if (od_tuners->powersave_bias)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530120 freq = od_ops.powersave_bias_target(policy, freq,
Jacob Shinfb308092013-04-02 09:56:56 -0500121 CPUFREQ_RELATION_H);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530122 else if (policy->cur == policy->max)
Viresh Kumar4471a342012-10-26 00:47:42 +0200123 return;
124
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530125 __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
Viresh Kumar4471a342012-10-26 00:47:42 +0200126 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
127}
128
129/*
130 * Every sampling_rate, we check, if current idle time is less than 20%
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300131 * (default), then we try to increase frequency. Else, we adjust the frequency
132 * proportional to load.
Viresh Kumar4471a342012-10-26 00:47:42 +0200133 */
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100134static void od_update(struct cpufreq_policy *policy)
Viresh Kumar4471a342012-10-26 00:47:42 +0200135{
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100136 struct policy_dbs_info *policy_dbs = policy->governor_data;
137 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100138 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000139 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100140 unsigned int load = dbs_update(policy);
Viresh Kumar4471a342012-10-26 00:47:42 +0200141
142 dbs_info->freq_lo = 0;
143
144 /* Check for frequency increase */
Viresh Kumarff4b1782016-02-09 09:01:32 +0530145 if (load > dbs_data->up_threshold) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200146 /* If switching to max speed, apply sampling_down_factor */
147 if (policy->cur < policy->max)
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100148 policy_dbs->rate_mult = dbs_data->sampling_down_factor;
Viresh Kumar4471a342012-10-26 00:47:42 +0200149 dbs_freq_increase(policy, policy->max);
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300150 } else {
151 /* Calculate the next frequency proportional to load */
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300152 unsigned int freq_next, min_f, max_f;
153
154 min_f = policy->cpuinfo.min_freq;
155 max_f = policy->cpuinfo.max_freq;
156 freq_next = min_f + load * (max_f - min_f) / 100;
Viresh Kumar4471a342012-10-26 00:47:42 +0200157
158 /* No longer fully busy, reset rate_mult */
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100159 policy_dbs->rate_mult = 1;
Viresh Kumar4471a342012-10-26 00:47:42 +0200160
Rafael J. Wysockia7f35cf2016-02-16 21:02:24 +0100161 if (od_tuners->powersave_bias)
162 freq_next = od_ops.powersave_bias_target(policy,
163 freq_next,
164 CPUFREQ_RELATION_L);
Jacob Shinfb308092013-04-02 09:56:56 -0500165
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300166 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C);
Viresh Kumar4471a342012-10-26 00:47:42 +0200167 }
168}
169
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530170static unsigned int od_dbs_update(struct cpufreq_policy *policy)
Fabio Baltierida53d612012-12-27 14:55:40 +0000171{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100172 struct policy_dbs_info *policy_dbs = policy->governor_data;
173 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100174 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
Rafael J. Wysocki6e96c5b2016-02-15 02:21:35 +0100175 int sample_type = dbs_info->sample_type;
Fabio Baltierida53d612012-12-27 14:55:40 +0000176
Viresh Kumar44472662013-01-31 17:28:02 +0000177 /* Common NORMAL_SAMPLE setup */
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530178 dbs_info->sample_type = OD_NORMAL_SAMPLE;
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100179 /*
180 * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
181 * it then.
182 */
183 if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530184 __cpufreq_driver_target(policy, dbs_info->freq_lo,
Viresh Kumar42994af2015-06-19 17:18:05 +0530185 CPUFREQ_RELATION_H);
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100186 return dbs_info->freq_lo_delay_us;
Fabio Baltierida53d612012-12-27 14:55:40 +0000187 }
Viresh Kumar44472662013-01-31 17:28:02 +0000188
Rafael J. Wysocki6e96c5b2016-02-15 02:21:35 +0100189 od_update(policy);
190
191 if (dbs_info->freq_lo) {
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530192 /* Setup SUB_SAMPLE */
Rafael J. Wysocki6e96c5b2016-02-15 02:21:35 +0100193 dbs_info->sample_type = OD_SUB_SAMPLE;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100194 return dbs_info->freq_hi_delay_us;
Rafael J. Wysocki6e96c5b2016-02-15 02:21:35 +0100195 }
196
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100197 return dbs_data->sampling_rate * policy_dbs->rate_mult;
Fabio Baltierida53d612012-12-27 14:55:40 +0000198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/************************** sysfs interface ************************/
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100201static struct dbs_governor od_dbs_gov;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100203static ssize_t store_io_is_busy(struct gov_attr_set *attr_set, const char *buf,
204 size_t count)
Arjan van de Ven19379b12010-05-09 08:26:51 -0700205{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100206 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700207 unsigned int input;
208 int ret;
209
210 ret = sscanf(buf, "%u", &input);
211 if (ret != 1)
212 return -EINVAL;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100213 dbs_data->io_is_busy = !!input;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000214
215 /* we need to re-evaluate prev_cpu_idle */
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100216 gov_update_cpu_data(dbs_data);
Rafael J. Wysockia33cce12016-02-18 02:26:55 +0100217
Arjan van de Ven19379b12010-05-09 08:26:51 -0700218 return count;
219}
220
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100221static ssize_t store_up_threshold(struct gov_attr_set *attr_set,
222 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100224 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 unsigned int input;
226 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700227 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Dave Jones32ee8c32006-02-28 00:43:23 -0500229 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700230 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -EINVAL;
232 }
Stratos Karafotis4bd4e422013-02-06 13:34:00 +0100233
Viresh Kumarff4b1782016-02-09 09:01:32 +0530234 dbs_data->up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return count;
236}
237
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100238static ssize_t store_sampling_down_factor(struct gov_attr_set *attr_set,
239 const char *buf, size_t count)
David C Niemi3f78a9f2010-10-06 16:54:24 -0400240{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100241 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100242 struct policy_dbs_info *policy_dbs;
243 unsigned int input;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400244 int ret;
245 ret = sscanf(buf, "%u", &input);
246
247 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
248 return -EINVAL;
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100249
Viresh Kumarff4b1782016-02-09 09:01:32 +0530250 dbs_data->sampling_down_factor = input;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400251
252 /* Reset down sampling multiplier in case it was active */
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100253 list_for_each_entry(policy_dbs, &attr_set->policy_list, list) {
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100254 /*
255 * Doing this without locking might lead to using different
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530256 * rate_mult values in od_update() and od_dbs_update().
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100257 */
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530258 mutex_lock(&policy_dbs->update_mutex);
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100259 policy_dbs->rate_mult = 1;
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530260 mutex_unlock(&policy_dbs->update_mutex);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400261 }
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100262
David C Niemi3f78a9f2010-10-06 16:54:24 -0400263 return count;
264}
265
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100266static ssize_t store_ignore_nice_load(struct gov_attr_set *attr_set,
267 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700268{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100269 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700270 unsigned int input;
271 int ret;
272
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700273 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500274 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700275 return -EINVAL;
276
Dave Jones2b03f892009-01-18 01:43:44 -0500277 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700278 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500279
Viresh Kumarff4b1782016-02-09 09:01:32 +0530280 if (input == dbs_data->ignore_nice_load) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700281 return count;
282 }
Viresh Kumarff4b1782016-02-09 09:01:32 +0530283 dbs_data->ignore_nice_load = input;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700284
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700285 /* we need to re-evaluate prev_cpu_idle */
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100286 gov_update_cpu_data(dbs_data);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500287
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700288 return count;
289}
290
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100291static ssize_t store_powersave_bias(struct gov_attr_set *attr_set,
292 const char *buf, size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400293{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100294 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000295 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100296 struct policy_dbs_info *policy_dbs;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400297 unsigned int input;
298 int ret;
299 ret = sscanf(buf, "%u", &input);
300
301 if (ret != 1)
302 return -EINVAL;
303
304 if (input > 1000)
305 input = 1000;
306
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000307 od_tuners->powersave_bias = input;
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100308
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100309 list_for_each_entry(policy_dbs, &attr_set->policy_list, list)
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100310 ondemand_powersave_bias_init(policy_dbs->policy);
311
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400312 return count;
313}
314
Viresh Kumarc4435632016-02-09 09:01:33 +0530315gov_show_one_common(sampling_rate);
316gov_show_one_common(up_threshold);
317gov_show_one_common(sampling_down_factor);
318gov_show_one_common(ignore_nice_load);
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100319gov_show_one_common(io_is_busy);
Viresh Kumarc4435632016-02-09 09:01:33 +0530320gov_show_one(od, powersave_bias);
Viresh Kumar4471a342012-10-26 00:47:42 +0200321
Viresh Kumarc4435632016-02-09 09:01:33 +0530322gov_attr_rw(sampling_rate);
323gov_attr_rw(io_is_busy);
324gov_attr_rw(up_threshold);
325gov_attr_rw(sampling_down_factor);
326gov_attr_rw(ignore_nice_load);
327gov_attr_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Viresh Kumarc4435632016-02-09 09:01:33 +0530329static struct attribute *od_attributes[] = {
Viresh Kumarc4435632016-02-09 09:01:33 +0530330 &sampling_rate.attr,
331 &up_threshold.attr,
332 &sampling_down_factor.attr,
333 &ignore_nice_load.attr,
334 &powersave_bias.attr,
335 &io_is_busy.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 NULL
337};
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/************************** sysfs end ************************/
340
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100341static struct policy_dbs_info *od_alloc(void)
342{
343 struct od_policy_dbs_info *dbs_info;
344
345 dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
346 return dbs_info ? &dbs_info->policy_dbs : NULL;
347}
348
349static void od_free(struct policy_dbs_info *policy_dbs)
350{
351 kfree(to_dbs_info(policy_dbs));
352}
353
Rafael J. Wysocki9a15fb22016-05-18 22:59:49 +0200354static int od_init(struct dbs_data *dbs_data)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000355{
356 struct od_dbs_tuners *tuners;
357 u64 idle_time;
358 int cpu;
359
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530360 tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
Viresh Kumara69d6b22016-05-18 17:55:26 +0530361 if (!tuners)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000362 return -ENOMEM;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000363
364 cpu = get_cpu();
365 idle_time = get_cpu_idle_time_us(cpu, NULL);
366 put_cpu();
367 if (idle_time != -1ULL) {
368 /* Idle micro accounting is supported. Use finer thresholds */
Viresh Kumarff4b1782016-02-09 09:01:32 +0530369 dbs_data->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000370 } else {
Viresh Kumarff4b1782016-02-09 09:01:32 +0530371 dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000372 }
373
Viresh Kumarff4b1782016-02-09 09:01:32 +0530374 dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
375 dbs_data->ignore_nice_load = 0;
Jacob Shinc2837552013-06-25 22:42:37 +0200376 tuners->powersave_bias = default_powersave_bias;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100377 dbs_data->io_is_busy = should_io_be_busy();
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000378
379 dbs_data->tuners = tuners;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000380 return 0;
381}
382
Rafael J. Wysocki9a15fb22016-05-18 22:59:49 +0200383static void od_exit(struct dbs_data *dbs_data)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000384{
385 kfree(dbs_data->tuners);
386}
387
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100388static void od_start(struct cpufreq_policy *policy)
389{
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100390 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100391
392 dbs_info->sample_type = OD_NORMAL_SAMPLE;
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100393 ondemand_powersave_bias_init(policy);
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100394}
395
Viresh Kumar4471a342012-10-26 00:47:42 +0200396static struct od_ops od_ops = {
Jacob Shinfb308092013-04-02 09:56:56 -0500397 .powersave_bias_target = generic_powersave_bias_target,
Viresh Kumar4471a342012-10-26 00:47:42 +0200398};
399
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100400static struct dbs_governor od_dbs_gov = {
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200401 .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("ondemand"),
Viresh Kumarc4435632016-02-09 09:01:33 +0530402 .kobj_type = { .default_attrs = od_attributes },
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530403 .gov_dbs_update = od_dbs_update,
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100404 .alloc = od_alloc,
405 .free = od_free,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000406 .init = od_init,
407 .exit = od_exit,
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100408 .start = od_start,
Viresh Kumar4471a342012-10-26 00:47:42 +0200409};
410
Quentin Perret10dd8572020-06-29 13:54:59 +0530411#define CPU_FREQ_GOV_ONDEMAND (od_dbs_gov.gov)
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100412
Jacob Shinfb308092013-04-02 09:56:56 -0500413static void od_set_powersave_bias(unsigned int powersave_bias)
414{
Jacob Shinfb308092013-04-02 09:56:56 -0500415 unsigned int cpu;
416 cpumask_t done;
417
Jacob Shinc2837552013-06-25 22:42:37 +0200418 default_powersave_bias = powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500419 cpumask_clear(&done);
420
421 get_online_cpus();
422 for_each_online_cpu(cpu) {
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100423 struct cpufreq_policy *policy;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100424 struct policy_dbs_info *policy_dbs;
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100425 struct dbs_data *dbs_data;
426 struct od_dbs_tuners *od_tuners;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530427
Jacob Shinfb308092013-04-02 09:56:56 -0500428 if (cpumask_test_cpu(cpu, &done))
429 continue;
430
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100431 policy = cpufreq_cpu_get_raw(cpu);
Quentin Perret10dd8572020-06-29 13:54:59 +0530432 if (!policy || policy->governor != &CPU_FREQ_GOV_ONDEMAND)
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100433 continue;
434
435 policy_dbs = policy->governor_data;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100436 if (!policy_dbs)
Jacob Shinc2837552013-06-25 22:42:37 +0200437 continue;
Jacob Shinfb308092013-04-02 09:56:56 -0500438
439 cpumask_or(&done, &done, policy->cpus);
Jacob Shinc2837552013-06-25 22:42:37 +0200440
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100441 dbs_data = policy_dbs->dbs_data;
Jacob Shinc2837552013-06-25 22:42:37 +0200442 od_tuners = dbs_data->tuners;
443 od_tuners->powersave_bias = default_powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500444 }
445 put_online_cpus();
446}
447
448void od_register_powersave_bias_handler(unsigned int (*f)
449 (struct cpufreq_policy *, unsigned int, unsigned int),
450 unsigned int powersave_bias)
451{
452 od_ops.powersave_bias_target = f;
453 od_set_powersave_bias(powersave_bias);
454}
455EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
456
457void od_unregister_powersave_bias_handler(void)
458{
459 od_ops.powersave_bias_target = generic_powersave_bias_target;
460 od_set_powersave_bias(0);
461}
462EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
463
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700464MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
465MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
466MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -0500467 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700468MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Johannes Weiner69157192008-01-17 15:21:08 -0800470#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100471struct cpufreq_governor *cpufreq_default_governor(void)
472{
Quentin Perret10dd8572020-06-29 13:54:59 +0530473 return &CPU_FREQ_GOV_ONDEMAND;
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100474}
Johannes Weiner69157192008-01-17 15:21:08 -0800475#endif
Quentin Perret10dd8572020-06-29 13:54:59 +0530476
477cpufreq_governor_init(CPU_FREQ_GOV_ONDEMAND);
478cpufreq_governor_exit(CPU_FREQ_GOV_ONDEMAND);