blob: 6b423eebfd5dbefccc2f20e52825271d0b2a7114 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/cpufreq/cpufreq_ondemand.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Viresh Kumar4471a342012-10-26 00:47:42 +020013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Viresh Kumar5ff0a262013-08-06 22:53:03 +053015#include <linux/cpu.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020016#include <linux/percpu-defs.h>
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000017#include <linux/slab.h>
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070018#include <linux/tick.h>
Ingo Molnar55687da2017-02-08 18:51:31 +010019#include <linux/sched/cpufreq.h>
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +010020
21#include "cpufreq_ondemand.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000023/* On-demand governor macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define DEF_FREQUENCY_UP_THRESHOLD (80)
David C Niemi3f78a9f2010-10-06 16:54:24 -040025#define DEF_SAMPLING_DOWN_FACTOR (1)
26#define MAX_SAMPLING_DOWN_FACTOR (100000)
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070027#define MICRO_FREQUENCY_UP_THRESHOLD (95)
Thomas Renningercef96152009-04-22 13:48:29 +020028#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
Chen Yu4dd63b42016-01-04 12:14:45 +080029#define MIN_FREQUENCY_UP_THRESHOLD (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define MAX_FREQUENCY_UP_THRESHOLD (100)
31
Jacob Shinfb308092013-04-02 09:56:56 -050032static struct od_ops od_ops;
33
Jacob Shinc2837552013-06-25 22:42:37 +020034static unsigned int default_powersave_bias;
35
Viresh Kumar4471a342012-10-26 00:47:42 +020036/*
37 * Not all CPUs want IO time to be accounted as busy; this depends on how
38 * efficient idling at a higher frequency/voltage is.
39 * Pavel Machek says this is not so for various generations of AMD and old
40 * Intel systems.
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000041 * Mike Chan (android.com) claims this is also not true for ARM.
Viresh Kumar4471a342012-10-26 00:47:42 +020042 * Because of this, whitelist specific known (series) of CPUs by default, and
43 * leave all others up to the user.
44 */
45static int should_io_be_busy(void)
46{
47#if defined(CONFIG_X86)
48 /*
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000049 * For Intel, Core 2 (model 15) and later have an efficient idle.
Viresh Kumar4471a342012-10-26 00:47:42 +020050 */
51 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
52 boot_cpu_data.x86 == 6 &&
53 boot_cpu_data.x86_model >= 15)
54 return 1;
55#endif
56 return 0;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070057}
58
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040059/*
60 * Find right freq to be set now with powersave_bias on.
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +010061 * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
62 * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040063 */
Jacob Shinfb308092013-04-02 09:56:56 -050064static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
Viresh Kumar4471a342012-10-26 00:47:42 +020065 unsigned int freq_next, unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040066{
67 unsigned int freq_req, freq_reduc, freq_avg;
68 unsigned int freq_hi, freq_lo;
Viresh Kumard218ed72016-06-03 10:58:51 +053069 unsigned int index;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +010070 unsigned int delay_hi_us;
Rafael J. Wysockibc505472016-02-07 16:24:26 +010071 struct policy_dbs_info *policy_dbs = policy->governor_data;
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +010072 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
Rafael J. Wysockibc505472016-02-07 16:24:26 +010073 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000074 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar34ac5d72016-06-03 10:58:48 +053075 struct cpufreq_frequency_table *freq_table = policy->freq_table;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040076
Viresh Kumar34ac5d72016-06-03 10:58:48 +053077 if (!freq_table) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040078 dbs_info->freq_lo = 0;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +010079 dbs_info->freq_lo_delay_us = 0;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040080 return freq_next;
81 }
82
Viresh Kumard218ed72016-06-03 10:58:51 +053083 index = cpufreq_frequency_table_target(policy, freq_next, relation);
Viresh Kumar34ac5d72016-06-03 10:58:48 +053084 freq_req = freq_table[index].frequency;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000085 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040086 freq_avg = freq_req - freq_reduc;
87
88 /* Find freq bounds for freq_avg in freq_table */
Viresh Kumar82577362016-06-27 09:59:34 +053089 index = cpufreq_table_find_index_h(policy, freq_avg);
Viresh Kumar34ac5d72016-06-03 10:58:48 +053090 freq_lo = freq_table[index].frequency;
Viresh Kumar82577362016-06-27 09:59:34 +053091 index = cpufreq_table_find_index_l(policy, freq_avg);
Viresh Kumar34ac5d72016-06-03 10:58:48 +053092 freq_hi = freq_table[index].frequency;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040093
94 /* Find out how long we have to be in hi and lo freqs */
95 if (freq_hi == freq_lo) {
96 dbs_info->freq_lo = 0;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +010097 dbs_info->freq_lo_delay_us = 0;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040098 return freq_lo;
99 }
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100100 delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
101 delay_hi_us += (freq_hi - freq_lo) / 2;
102 delay_hi_us /= freq_hi - freq_lo;
103 dbs_info->freq_hi_delay_us = delay_hi_us;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400104 dbs_info->freq_lo = freq_lo;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100105 dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400106 return freq_hi;
107}
108
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100109static void ondemand_powersave_bias_init(struct cpufreq_policy *policy)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400110{
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100111 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100112
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100113 dbs_info->freq_lo = 0;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400114}
115
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530116static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
Viresh Kumar4471a342012-10-26 00:47:42 +0200117{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100118 struct policy_dbs_info *policy_dbs = policy->governor_data;
119 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000120 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
121
122 if (od_tuners->powersave_bias)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530123 freq = od_ops.powersave_bias_target(policy, freq,
Jacob Shinfb308092013-04-02 09:56:56 -0500124 CPUFREQ_RELATION_H);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530125 else if (policy->cur == policy->max)
Viresh Kumar4471a342012-10-26 00:47:42 +0200126 return;
127
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530128 __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
Viresh Kumar4471a342012-10-26 00:47:42 +0200129 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
130}
131
132/*
133 * Every sampling_rate, we check, if current idle time is less than 20%
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300134 * (default), then we try to increase frequency. Else, we adjust the frequency
135 * proportional to load.
Viresh Kumar4471a342012-10-26 00:47:42 +0200136 */
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100137static void od_update(struct cpufreq_policy *policy)
Viresh Kumar4471a342012-10-26 00:47:42 +0200138{
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100139 struct policy_dbs_info *policy_dbs = policy->governor_data;
140 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100141 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000142 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100143 unsigned int load = dbs_update(policy);
Viresh Kumar4471a342012-10-26 00:47:42 +0200144
145 dbs_info->freq_lo = 0;
146
147 /* Check for frequency increase */
Viresh Kumarff4b1782016-02-09 09:01:32 +0530148 if (load > dbs_data->up_threshold) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200149 /* If switching to max speed, apply sampling_down_factor */
150 if (policy->cur < policy->max)
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100151 policy_dbs->rate_mult = dbs_data->sampling_down_factor;
Viresh Kumar4471a342012-10-26 00:47:42 +0200152 dbs_freq_increase(policy, policy->max);
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300153 } else {
154 /* Calculate the next frequency proportional to load */
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300155 unsigned int freq_next, min_f, max_f;
156
157 min_f = policy->cpuinfo.min_freq;
158 max_f = policy->cpuinfo.max_freq;
159 freq_next = min_f + load * (max_f - min_f) / 100;
Viresh Kumar4471a342012-10-26 00:47:42 +0200160
161 /* No longer fully busy, reset rate_mult */
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100162 policy_dbs->rate_mult = 1;
Viresh Kumar4471a342012-10-26 00:47:42 +0200163
Rafael J. Wysockia7f35cf2016-02-16 21:02:24 +0100164 if (od_tuners->powersave_bias)
165 freq_next = od_ops.powersave_bias_target(policy,
166 freq_next,
167 CPUFREQ_RELATION_L);
Jacob Shinfb308092013-04-02 09:56:56 -0500168
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300169 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C);
Viresh Kumar4471a342012-10-26 00:47:42 +0200170 }
171}
172
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530173static unsigned int od_dbs_update(struct cpufreq_policy *policy)
Fabio Baltierida53d612012-12-27 14:55:40 +0000174{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100175 struct policy_dbs_info *policy_dbs = policy->governor_data;
176 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100177 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
Rafael J. Wysocki6e96c5b2016-02-15 02:21:35 +0100178 int sample_type = dbs_info->sample_type;
Fabio Baltierida53d612012-12-27 14:55:40 +0000179
Viresh Kumar44472662013-01-31 17:28:02 +0000180 /* Common NORMAL_SAMPLE setup */
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530181 dbs_info->sample_type = OD_NORMAL_SAMPLE;
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100182 /*
183 * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
184 * it then.
185 */
186 if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530187 __cpufreq_driver_target(policy, dbs_info->freq_lo,
Viresh Kumar42994af2015-06-19 17:18:05 +0530188 CPUFREQ_RELATION_H);
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100189 return dbs_info->freq_lo_delay_us;
Fabio Baltierida53d612012-12-27 14:55:40 +0000190 }
Viresh Kumar44472662013-01-31 17:28:02 +0000191
Rafael J. Wysocki6e96c5b2016-02-15 02:21:35 +0100192 od_update(policy);
193
194 if (dbs_info->freq_lo) {
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530195 /* Setup SUB_SAMPLE */
Rafael J. Wysocki6e96c5b2016-02-15 02:21:35 +0100196 dbs_info->sample_type = OD_SUB_SAMPLE;
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100197 return dbs_info->freq_hi_delay_us;
Rafael J. Wysocki6e96c5b2016-02-15 02:21:35 +0100198 }
199
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100200 return dbs_data->sampling_rate * policy_dbs->rate_mult;
Fabio Baltierida53d612012-12-27 14:55:40 +0000201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/************************** sysfs interface ************************/
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100204static struct dbs_governor od_dbs_gov;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100206static ssize_t store_io_is_busy(struct gov_attr_set *attr_set, const char *buf,
207 size_t count)
Arjan van de Ven19379b12010-05-09 08:26:51 -0700208{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100209 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700210 unsigned int input;
211 int ret;
212
213 ret = sscanf(buf, "%u", &input);
214 if (ret != 1)
215 return -EINVAL;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100216 dbs_data->io_is_busy = !!input;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000217
218 /* we need to re-evaluate prev_cpu_idle */
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100219 gov_update_cpu_data(dbs_data);
Rafael J. Wysockia33cce12016-02-18 02:26:55 +0100220
Arjan van de Ven19379b12010-05-09 08:26:51 -0700221 return count;
222}
223
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100224static ssize_t store_up_threshold(struct gov_attr_set *attr_set,
225 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100227 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 unsigned int input;
229 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700230 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Dave Jones32ee8c32006-02-28 00:43:23 -0500232 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700233 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EINVAL;
235 }
Stratos Karafotis4bd4e422013-02-06 13:34:00 +0100236
Viresh Kumarff4b1782016-02-09 09:01:32 +0530237 dbs_data->up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return count;
239}
240
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100241static ssize_t store_sampling_down_factor(struct gov_attr_set *attr_set,
242 const char *buf, size_t count)
David C Niemi3f78a9f2010-10-06 16:54:24 -0400243{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100244 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100245 struct policy_dbs_info *policy_dbs;
246 unsigned int input;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400247 int ret;
248 ret = sscanf(buf, "%u", &input);
249
250 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
251 return -EINVAL;
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100252
Viresh Kumarff4b1782016-02-09 09:01:32 +0530253 dbs_data->sampling_down_factor = input;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400254
255 /* Reset down sampling multiplier in case it was active */
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100256 list_for_each_entry(policy_dbs, &attr_set->policy_list, list) {
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100257 /*
258 * Doing this without locking might lead to using different
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530259 * rate_mult values in od_update() and od_dbs_update().
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100260 */
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530261 mutex_lock(&policy_dbs->update_mutex);
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100262 policy_dbs->rate_mult = 1;
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530263 mutex_unlock(&policy_dbs->update_mutex);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400264 }
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100265
David C Niemi3f78a9f2010-10-06 16:54:24 -0400266 return count;
267}
268
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100269static ssize_t store_ignore_nice_load(struct gov_attr_set *attr_set,
270 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700271{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100272 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700273 unsigned int input;
274 int ret;
275
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700276 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500277 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700278 return -EINVAL;
279
Dave Jones2b03f892009-01-18 01:43:44 -0500280 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700281 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500282
Viresh Kumarff4b1782016-02-09 09:01:32 +0530283 if (input == dbs_data->ignore_nice_load) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700284 return count;
285 }
Viresh Kumarff4b1782016-02-09 09:01:32 +0530286 dbs_data->ignore_nice_load = input;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700287
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700288 /* we need to re-evaluate prev_cpu_idle */
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100289 gov_update_cpu_data(dbs_data);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500290
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700291 return count;
292}
293
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100294static ssize_t store_powersave_bias(struct gov_attr_set *attr_set,
295 const char *buf, size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400296{
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100297 struct dbs_data *dbs_data = to_dbs_data(attr_set);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000298 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100299 struct policy_dbs_info *policy_dbs;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400300 unsigned int input;
301 int ret;
302 ret = sscanf(buf, "%u", &input);
303
304 if (ret != 1)
305 return -EINVAL;
306
307 if (input > 1000)
308 input = 1000;
309
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000310 od_tuners->powersave_bias = input;
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100311
Rafael J. Wysocki0dd3c1d2016-03-22 02:47:51 +0100312 list_for_each_entry(policy_dbs, &attr_set->policy_list, list)
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100313 ondemand_powersave_bias_init(policy_dbs->policy);
314
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400315 return count;
316}
317
Viresh Kumarc4435632016-02-09 09:01:33 +0530318gov_show_one_common(sampling_rate);
319gov_show_one_common(up_threshold);
320gov_show_one_common(sampling_down_factor);
321gov_show_one_common(ignore_nice_load);
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100322gov_show_one_common(io_is_busy);
Viresh Kumarc4435632016-02-09 09:01:33 +0530323gov_show_one(od, powersave_bias);
Viresh Kumar4471a342012-10-26 00:47:42 +0200324
Viresh Kumarc4435632016-02-09 09:01:33 +0530325gov_attr_rw(sampling_rate);
326gov_attr_rw(io_is_busy);
327gov_attr_rw(up_threshold);
328gov_attr_rw(sampling_down_factor);
329gov_attr_rw(ignore_nice_load);
330gov_attr_rw(powersave_bias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Viresh Kumarc4435632016-02-09 09:01:33 +0530332static struct attribute *od_attributes[] = {
Viresh Kumarc4435632016-02-09 09:01:33 +0530333 &sampling_rate.attr,
334 &up_threshold.attr,
335 &sampling_down_factor.attr,
336 &ignore_nice_load.attr,
337 &powersave_bias.attr,
338 &io_is_busy.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 NULL
340};
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/************************** sysfs end ************************/
343
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100344static struct policy_dbs_info *od_alloc(void)
345{
346 struct od_policy_dbs_info *dbs_info;
347
348 dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
349 return dbs_info ? &dbs_info->policy_dbs : NULL;
350}
351
352static void od_free(struct policy_dbs_info *policy_dbs)
353{
354 kfree(to_dbs_info(policy_dbs));
355}
356
Rafael J. Wysocki9a15fb22016-05-18 22:59:49 +0200357static int od_init(struct dbs_data *dbs_data)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000358{
359 struct od_dbs_tuners *tuners;
360 u64 idle_time;
361 int cpu;
362
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530363 tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
Viresh Kumara69d6b22016-05-18 17:55:26 +0530364 if (!tuners)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000365 return -ENOMEM;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000366
367 cpu = get_cpu();
368 idle_time = get_cpu_idle_time_us(cpu, NULL);
369 put_cpu();
370 if (idle_time != -1ULL) {
371 /* Idle micro accounting is supported. Use finer thresholds */
Viresh Kumarff4b1782016-02-09 09:01:32 +0530372 dbs_data->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000373 } else {
Viresh Kumarff4b1782016-02-09 09:01:32 +0530374 dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000375 }
376
Viresh Kumarff4b1782016-02-09 09:01:32 +0530377 dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
378 dbs_data->ignore_nice_load = 0;
Jacob Shinc2837552013-06-25 22:42:37 +0200379 tuners->powersave_bias = default_powersave_bias;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100380 dbs_data->io_is_busy = should_io_be_busy();
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000381
382 dbs_data->tuners = tuners;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000383 return 0;
384}
385
Rafael J. Wysocki9a15fb22016-05-18 22:59:49 +0200386static void od_exit(struct dbs_data *dbs_data)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000387{
388 kfree(dbs_data->tuners);
389}
390
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100391static void od_start(struct cpufreq_policy *policy)
392{
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100393 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100394
395 dbs_info->sample_type = OD_NORMAL_SAMPLE;
Rafael J. Wysockid1db75f2016-02-18 02:28:24 +0100396 ondemand_powersave_bias_init(policy);
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100397}
398
Viresh Kumar4471a342012-10-26 00:47:42 +0200399static struct od_ops od_ops = {
Jacob Shinfb308092013-04-02 09:56:56 -0500400 .powersave_bias_target = generic_powersave_bias_target,
Viresh Kumar4471a342012-10-26 00:47:42 +0200401};
402
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100403static struct dbs_governor od_dbs_gov = {
Rafael J. Wysockie7888922016-06-02 23:24:15 +0200404 .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("ondemand"),
Viresh Kumarc4435632016-02-09 09:01:33 +0530405 .kobj_type = { .default_attrs = od_attributes },
Viresh Kumar26f0dbc2016-11-08 11:06:33 +0530406 .gov_dbs_update = od_dbs_update,
Rafael J. Wysocki7d5a9952016-02-18 18:40:14 +0100407 .alloc = od_alloc,
408 .free = od_free,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000409 .init = od_init,
410 .exit = od_exit,
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100411 .start = od_start,
Viresh Kumar4471a342012-10-26 00:47:42 +0200412};
413
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100414#define CPU_FREQ_GOV_ONDEMAND (&od_dbs_gov.gov)
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100415
Jacob Shinfb308092013-04-02 09:56:56 -0500416static void od_set_powersave_bias(unsigned int powersave_bias)
417{
Jacob Shinfb308092013-04-02 09:56:56 -0500418 unsigned int cpu;
419 cpumask_t done;
420
Jacob Shinc2837552013-06-25 22:42:37 +0200421 default_powersave_bias = powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500422 cpumask_clear(&done);
423
424 get_online_cpus();
425 for_each_online_cpu(cpu) {
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100426 struct cpufreq_policy *policy;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100427 struct policy_dbs_info *policy_dbs;
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100428 struct dbs_data *dbs_data;
429 struct od_dbs_tuners *od_tuners;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530430
Jacob Shinfb308092013-04-02 09:56:56 -0500431 if (cpumask_test_cpu(cpu, &done))
432 continue;
433
Rafael J. Wysocki8c8f77f2016-02-21 00:51:27 +0100434 policy = cpufreq_cpu_get_raw(cpu);
435 if (!policy || policy->governor != CPU_FREQ_GOV_ONDEMAND)
436 continue;
437
438 policy_dbs = policy->governor_data;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100439 if (!policy_dbs)
Jacob Shinc2837552013-06-25 22:42:37 +0200440 continue;
Jacob Shinfb308092013-04-02 09:56:56 -0500441
442 cpumask_or(&done, &done, policy->cpus);
Jacob Shinc2837552013-06-25 22:42:37 +0200443
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100444 dbs_data = policy_dbs->dbs_data;
Jacob Shinc2837552013-06-25 22:42:37 +0200445 od_tuners = dbs_data->tuners;
446 od_tuners->powersave_bias = default_powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500447 }
448 put_online_cpus();
449}
450
451void od_register_powersave_bias_handler(unsigned int (*f)
452 (struct cpufreq_policy *, unsigned int, unsigned int),
453 unsigned int powersave_bias)
454{
455 od_ops.powersave_bias_target = f;
456 od_set_powersave_bias(powersave_bias);
457}
458EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
459
460void od_unregister_powersave_bias_handler(void)
461{
462 od_ops.powersave_bias_target = generic_powersave_bias_target;
463 od_set_powersave_bias(0);
464}
465EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static int __init cpufreq_gov_dbs_init(void)
468{
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100469 return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472static void __exit cpufreq_gov_dbs_exit(void)
473{
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100474 cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700477MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
478MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
479MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -0500480 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700481MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Johannes Weiner69157192008-01-17 15:21:08 -0800483#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100484struct cpufreq_governor *cpufreq_default_governor(void)
485{
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100486 return CPU_FREQ_GOV_ONDEMAND;
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100487}
488
Johannes Weiner69157192008-01-17 15:21:08 -0800489fs_initcall(cpufreq_gov_dbs_init);
490#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800492#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493module_exit(cpufreq_gov_dbs_exit);