blob: 9ef4402644c7c8f69b170190e937fe9ce80f57fc [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>
Viresh Kumar4471a342012-10-26 00:47:42 +020019#include "cpufreq_governor.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000021/* On-demand governor macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define DEF_FREQUENCY_UP_THRESHOLD (80)
David C Niemi3f78a9f2010-10-06 16:54:24 -040023#define DEF_SAMPLING_DOWN_FACTOR (1)
24#define MAX_SAMPLING_DOWN_FACTOR (100000)
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070025#define MICRO_FREQUENCY_UP_THRESHOLD (95)
Thomas Renningercef96152009-04-22 13:48:29 +020026#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
Dave Jonesc29f1402005-05-31 19:03:50 -070027#define MIN_FREQUENCY_UP_THRESHOLD (11)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define MAX_FREQUENCY_UP_THRESHOLD (100)
29
Viresh Kumar4471a342012-10-26 00:47:42 +020030static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info);
Thomas Renninger112124a2009-02-04 11:55:12 +010031
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 +020036static void ondemand_powersave_bias_init_cpu(int cpu)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070037{
Viresh Kumar4471a342012-10-26 00:47:42 +020038 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070039
Viresh Kumar4471a342012-10-26 00:47:42 +020040 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
41 dbs_info->freq_lo = 0;
42}
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070043
Viresh Kumar4471a342012-10-26 00:47:42 +020044/*
45 * Not all CPUs want IO time to be accounted as busy; this depends on how
46 * efficient idling at a higher frequency/voltage is.
47 * Pavel Machek says this is not so for various generations of AMD and old
48 * Intel systems.
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000049 * Mike Chan (android.com) claims this is also not true for ARM.
Viresh Kumar4471a342012-10-26 00:47:42 +020050 * Because of this, whitelist specific known (series) of CPUs by default, and
51 * leave all others up to the user.
52 */
53static int should_io_be_busy(void)
54{
55#if defined(CONFIG_X86)
56 /*
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000057 * For Intel, Core 2 (model 15) and later have an efficient idle.
Viresh Kumar4471a342012-10-26 00:47:42 +020058 */
59 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
60 boot_cpu_data.x86 == 6 &&
61 boot_cpu_data.x86_model >= 15)
62 return 1;
63#endif
64 return 0;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070065}
66
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040067/*
68 * Find right freq to be set now with powersave_bias on.
69 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
70 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
71 */
Jacob Shinfb308092013-04-02 09:56:56 -050072static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
Viresh Kumar4471a342012-10-26 00:47:42 +020073 unsigned int freq_next, unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040074{
75 unsigned int freq_req, freq_reduc, freq_avg;
76 unsigned int freq_hi, freq_lo;
77 unsigned int index = 0;
78 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
Viresh Kumar4471a342012-10-26 00:47:42 +020079 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
Tejun Heo245b2e72009-06-24 15:13:48 +090080 policy->cpu);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000081 struct dbs_data *dbs_data = policy->governor_data;
82 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040083
84 if (!dbs_info->freq_table) {
85 dbs_info->freq_lo = 0;
86 dbs_info->freq_lo_jiffies = 0;
87 return freq_next;
88 }
89
90 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
91 relation, &index);
92 freq_req = dbs_info->freq_table[index].frequency;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000093 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040094 freq_avg = freq_req - freq_reduc;
95
96 /* Find freq bounds for freq_avg in freq_table */
97 index = 0;
98 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
99 CPUFREQ_RELATION_H, &index);
100 freq_lo = dbs_info->freq_table[index].frequency;
101 index = 0;
102 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
103 CPUFREQ_RELATION_L, &index);
104 freq_hi = dbs_info->freq_table[index].frequency;
105
106 /* Find out how long we have to be in hi and lo freqs */
107 if (freq_hi == freq_lo) {
108 dbs_info->freq_lo = 0;
109 dbs_info->freq_lo_jiffies = 0;
110 return freq_lo;
111 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000112 jiffies_total = usecs_to_jiffies(od_tuners->sampling_rate);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400113 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
114 jiffies_hi += ((freq_hi - freq_lo) / 2);
115 jiffies_hi /= (freq_hi - freq_lo);
116 jiffies_lo = jiffies_total - jiffies_hi;
117 dbs_info->freq_lo = freq_lo;
118 dbs_info->freq_lo_jiffies = jiffies_lo;
119 dbs_info->freq_hi_jiffies = jiffies_hi;
120 return freq_hi;
121}
122
123static void ondemand_powersave_bias_init(void)
124{
125 int i;
126 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700127 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400128 }
129}
130
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530131static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
Viresh Kumar4471a342012-10-26 00:47:42 +0200132{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530133 struct dbs_data *dbs_data = policy->governor_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000134 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
135
136 if (od_tuners->powersave_bias)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530137 freq = od_ops.powersave_bias_target(policy, freq,
Jacob Shinfb308092013-04-02 09:56:56 -0500138 CPUFREQ_RELATION_H);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530139 else if (policy->cur == policy->max)
Viresh Kumar4471a342012-10-26 00:47:42 +0200140 return;
141
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530142 __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
Viresh Kumar4471a342012-10-26 00:47:42 +0200143 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
144}
145
146/*
147 * Every sampling_rate, we check, if current idle time is less than 20%
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300148 * (default), then we try to increase frequency. Else, we adjust the frequency
149 * proportional to load.
Viresh Kumar4471a342012-10-26 00:47:42 +0200150 */
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300151static void od_check_cpu(int cpu, unsigned int load)
Viresh Kumar4471a342012-10-26 00:47:42 +0200152{
153 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100154 struct cpufreq_policy *policy = dbs_info->cdbs.policy_dbs->policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000155 struct dbs_data *dbs_data = policy->governor_data;
156 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar4471a342012-10-26 00:47:42 +0200157
158 dbs_info->freq_lo = 0;
159
160 /* Check for frequency increase */
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300161 if (load > od_tuners->up_threshold) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200162 /* If switching to max speed, apply sampling_down_factor */
163 if (policy->cur < policy->max)
164 dbs_info->rate_mult =
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000165 od_tuners->sampling_down_factor;
Viresh Kumar4471a342012-10-26 00:47:42 +0200166 dbs_freq_increase(policy, policy->max);
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300167 } else {
168 /* Calculate the next frequency proportional to load */
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300169 unsigned int freq_next, min_f, max_f;
170
171 min_f = policy->cpuinfo.min_freq;
172 max_f = policy->cpuinfo.max_freq;
173 freq_next = min_f + load * (max_f - min_f) / 100;
Viresh Kumar4471a342012-10-26 00:47:42 +0200174
175 /* No longer fully busy, reset rate_mult */
176 dbs_info->rate_mult = 1;
177
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000178 if (!od_tuners->powersave_bias) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200179 __cpufreq_driver_target(policy, freq_next,
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300180 CPUFREQ_RELATION_C);
Jacob Shinfb308092013-04-02 09:56:56 -0500181 return;
Viresh Kumar4471a342012-10-26 00:47:42 +0200182 }
Jacob Shinfb308092013-04-02 09:56:56 -0500183
184 freq_next = od_ops.powersave_bias_target(policy, freq_next,
185 CPUFREQ_RELATION_L);
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300186 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C);
Viresh Kumar4471a342012-10-26 00:47:42 +0200187 }
188}
189
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100190static unsigned int od_dbs_timer(struct cpufreq_policy *policy)
Fabio Baltierida53d612012-12-27 14:55:40 +0000191{
Viresh Kumaraffde5d2015-12-03 09:37:51 +0530192 struct dbs_data *dbs_data = policy->governor_data;
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +0100193 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000194 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530195 int delay = 0, sample_type = dbs_info->sample_type;
Fabio Baltierida53d612012-12-27 14:55:40 +0000196
Viresh Kumar44472662013-01-31 17:28:02 +0000197 /* Common NORMAL_SAMPLE setup */
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530198 dbs_info->sample_type = OD_NORMAL_SAMPLE;
Viresh Kumar44472662013-01-31 17:28:02 +0000199 if (sample_type == OD_SUB_SAMPLE) {
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530200 delay = dbs_info->freq_lo_jiffies;
201 __cpufreq_driver_target(policy, dbs_info->freq_lo,
Viresh Kumar42994af2015-06-19 17:18:05 +0530202 CPUFREQ_RELATION_H);
Fabio Baltierida53d612012-12-27 14:55:40 +0000203 } else {
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +0100204 dbs_check_cpu(policy);
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530205 if (dbs_info->freq_lo) {
Viresh Kumar44472662013-01-31 17:28:02 +0000206 /* Setup timer for SUB_SAMPLE */
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530207 dbs_info->sample_type = OD_SUB_SAMPLE;
208 delay = dbs_info->freq_hi_jiffies;
Viresh Kumar44472662013-01-31 17:28:02 +0000209 }
Fabio Baltierida53d612012-12-27 14:55:40 +0000210 }
Viresh Kumar44472662013-01-31 17:28:02 +0000211
Viresh Kumar9d445922013-02-27 11:06:36 +0530212 if (!delay)
213 delay = delay_for_sampling_rate(od_tuners->sampling_rate
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530214 * dbs_info->rate_mult);
Viresh Kumar9d445922013-02-27 11:06:36 +0530215
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530216 return delay;
Fabio Baltierida53d612012-12-27 14:55:40 +0000217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/************************** sysfs interface ************************/
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100220static struct dbs_governor od_dbs_gov;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900222/**
223 * update_sampling_rate - update sampling rate effective immediately if needed.
224 * @new_rate: new sampling rate
225 *
Stratos Karafotis06eb09d2013-02-08 17:24:18 +0000226 * If new rate is smaller than the old, simply updating
Viresh Kumar4471a342012-10-26 00:47:42 +0200227 * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
228 * original sampling_rate was 1 second and the requested new sampling rate is 10
229 * ms because the user needs immediate reaction from ondemand governor, but not
230 * sure if higher frequency will be required or not, then, the governor may
231 * change the sampling rate too late; up to 1 second later. Thus, if we are
232 * reducing the sampling rate, we need to make the new value effective
233 * immediately.
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900234 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000235static void update_sampling_rate(struct dbs_data *dbs_data,
236 unsigned int new_rate)
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900237{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000238 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumarf08f6382015-12-03 09:37:54 +0530239 struct cpumask cpumask;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900240 int cpu;
241
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000242 od_tuners->sampling_rate = new_rate = max(new_rate,
243 dbs_data->min_sampling_rate);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900244
Viresh Kumare128c862015-12-03 09:37:49 +0530245 /*
246 * Lock governor so that governor start/stop can't execute in parallel.
247 */
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100248 mutex_lock(&dbs_data_mutex);
Viresh Kumare128c862015-12-03 09:37:49 +0530249
Viresh Kumarf08f6382015-12-03 09:37:54 +0530250 cpumask_copy(&cpumask, cpu_online_mask);
251
252 for_each_cpu(cpu, &cpumask) {
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900253 struct cpufreq_policy *policy;
Viresh Kumar4471a342012-10-26 00:47:42 +0200254 struct od_cpu_dbs_info_s *dbs_info;
Viresh Kumare128c862015-12-03 09:37:49 +0530255 struct cpu_dbs_info *cdbs;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100256 struct policy_dbs_info *policy_dbs;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900257
Fabio Baltieri8ee2ec52012-12-27 14:55:42 +0000258 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Viresh Kumare128c862015-12-03 09:37:49 +0530259 cdbs = &dbs_info->cdbs;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100260 policy_dbs = cdbs->policy_dbs;
Viresh Kumare128c862015-12-03 09:37:49 +0530261
262 /*
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100263 * A valid policy_dbs and policy_dbs->policy means governor
264 * hasn't stopped or exited yet.
Viresh Kumare128c862015-12-03 09:37:49 +0530265 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100266 if (!policy_dbs || !policy_dbs->policy)
Viresh Kumare128c862015-12-03 09:37:49 +0530267 continue;
268
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100269 policy = policy_dbs->policy;
Viresh Kumare128c862015-12-03 09:37:49 +0530270
Viresh Kumarf08f6382015-12-03 09:37:54 +0530271 /* clear all CPUs of this policy */
272 cpumask_andnot(&cpumask, &cpumask, policy->cpus);
273
Viresh Kumare128c862015-12-03 09:37:49 +0530274 /*
275 * Update sampling rate for CPUs whose policy is governed by
276 * dbs_data. In case of governor_per_policy, only a single
277 * policy will be governed by dbs_data, otherwise there can be
278 * multiple policies that are governed by the same dbs_data.
279 */
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100280 if (dbs_data == policy->governor_data) {
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100281 mutex_lock(&policy_dbs->timer_mutex);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100282 /*
283 * On 32-bit architectures this may race with the
284 * sample_delay_ns read in dbs_update_util_handler(),
285 * but that really doesn't matter. If the read returns
286 * a value that's too big, the sample will be skipped,
287 * but the next invocation of dbs_update_util_handler()
288 * (when the update has been completed) will take a
289 * sample. If the returned value is too small, the
290 * sample will be taken immediately, but that isn't a
291 * problem, as we want the new rate to take effect
292 * immediately anyway.
293 *
294 * If this runs in parallel with dbs_work_handler(), we
295 * may end up overwriting the sample_delay_ns value that
296 * it has just written, but the difference should not be
297 * too big and it will be corrected next time a sample
298 * is taken, so it shouldn't be significant.
299 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100300 gov_update_sample_delay(policy_dbs, new_rate);
301 mutex_unlock(&policy_dbs->timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900302 }
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900303 }
Viresh Kumare128c862015-12-03 09:37:49 +0530304
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100305 mutex_unlock(&dbs_data_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900306}
307
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000308static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
309 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 unsigned int input;
312 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700313 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700314 if (ret != 1)
315 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000316
317 update_sampling_rate(dbs_data, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return count;
319}
320
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000321static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf,
322 size_t count)
Arjan van de Ven19379b12010-05-09 08:26:51 -0700323{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000324 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700325 unsigned int input;
326 int ret;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000327 unsigned int j;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700328
329 ret = sscanf(buf, "%u", &input);
330 if (ret != 1)
331 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000332 od_tuners->io_is_busy = !!input;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000333
334 /* we need to re-evaluate prev_cpu_idle */
335 for_each_online_cpu(j) {
336 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
337 j);
338 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
339 &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
340 }
Arjan van de Ven19379b12010-05-09 08:26:51 -0700341 return count;
342}
343
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000344static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
345 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000347 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned int input;
349 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700350 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dave Jones32ee8c32006-02-28 00:43:23 -0500352 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700353 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EINVAL;
355 }
Stratos Karafotis4bd4e422013-02-06 13:34:00 +0100356
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000357 od_tuners->up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return count;
359}
360
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000361static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
362 const char *buf, size_t count)
David C Niemi3f78a9f2010-10-06 16:54:24 -0400363{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000364 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400365 unsigned int input, j;
366 int ret;
367 ret = sscanf(buf, "%u", &input);
368
369 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
370 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000371 od_tuners->sampling_down_factor = input;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400372
373 /* Reset down sampling multiplier in case it was active */
374 for_each_online_cpu(j) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200375 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
376 j);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400377 dbs_info->rate_mult = 1;
378 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400379 return count;
380}
381
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530382static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data,
383 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700384{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000385 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700386 unsigned int input;
387 int ret;
388
389 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500390
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700391 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500392 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700393 return -EINVAL;
394
Dave Jones2b03f892009-01-18 01:43:44 -0500395 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700396 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500397
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530398 if (input == od_tuners->ignore_nice_load) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700399 return count;
400 }
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530401 od_tuners->ignore_nice_load = input;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700402
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700403 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700404 for_each_online_cpu(j) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200405 struct od_cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900406 dbs_info = &per_cpu(od_cpu_dbs_info, j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200407 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
Stratos Karafotis9366d842013-02-28 16:57:32 +0000408 &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530409 if (od_tuners->ignore_nice_load)
Viresh Kumar4471a342012-10-26 00:47:42 +0200410 dbs_info->cdbs.prev_cpu_nice =
411 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500412
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700413 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700414 return count;
415}
416
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000417static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf,
418 size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400419{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000420 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400421 unsigned int input;
422 int ret;
423 ret = sscanf(buf, "%u", &input);
424
425 if (ret != 1)
426 return -EINVAL;
427
428 if (input > 1000)
429 input = 1000;
430
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000431 od_tuners->powersave_bias = input;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400432 ondemand_powersave_bias_init();
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400433 return count;
434}
435
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000436show_store_one(od, sampling_rate);
437show_store_one(od, io_is_busy);
438show_store_one(od, up_threshold);
439show_store_one(od, sampling_down_factor);
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530440show_store_one(od, ignore_nice_load);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000441show_store_one(od, powersave_bias);
442declare_show_sampling_rate_min(od);
Viresh Kumar4471a342012-10-26 00:47:42 +0200443
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000444gov_sys_pol_attr_rw(sampling_rate);
445gov_sys_pol_attr_rw(io_is_busy);
446gov_sys_pol_attr_rw(up_threshold);
447gov_sys_pol_attr_rw(sampling_down_factor);
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530448gov_sys_pol_attr_rw(ignore_nice_load);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000449gov_sys_pol_attr_rw(powersave_bias);
450gov_sys_pol_attr_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000452static struct attribute *dbs_attributes_gov_sys[] = {
453 &sampling_rate_min_gov_sys.attr,
454 &sampling_rate_gov_sys.attr,
455 &up_threshold_gov_sys.attr,
456 &sampling_down_factor_gov_sys.attr,
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530457 &ignore_nice_load_gov_sys.attr,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000458 &powersave_bias_gov_sys.attr,
459 &io_is_busy_gov_sys.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 NULL
461};
462
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000463static struct attribute_group od_attr_group_gov_sys = {
464 .attrs = dbs_attributes_gov_sys,
465 .name = "ondemand",
466};
467
468static struct attribute *dbs_attributes_gov_pol[] = {
469 &sampling_rate_min_gov_pol.attr,
470 &sampling_rate_gov_pol.attr,
471 &up_threshold_gov_pol.attr,
472 &sampling_down_factor_gov_pol.attr,
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530473 &ignore_nice_load_gov_pol.attr,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000474 &powersave_bias_gov_pol.attr,
475 &io_is_busy_gov_pol.attr,
476 NULL
477};
478
479static struct attribute_group od_attr_group_gov_pol = {
480 .attrs = dbs_attributes_gov_pol,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 .name = "ondemand",
482};
483
484/************************** sysfs end ************************/
485
Viresh Kumar8e0484d2015-06-03 15:57:11 +0530486static int od_init(struct dbs_data *dbs_data, bool notify)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000487{
488 struct od_dbs_tuners *tuners;
489 u64 idle_time;
490 int cpu;
491
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530492 tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000493 if (!tuners) {
494 pr_err("%s: kzalloc failed\n", __func__);
495 return -ENOMEM;
496 }
497
498 cpu = get_cpu();
499 idle_time = get_cpu_idle_time_us(cpu, NULL);
500 put_cpu();
501 if (idle_time != -1ULL) {
502 /* Idle micro accounting is supported. Use finer thresholds */
503 tuners->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000504 /*
505 * In nohz/micro accounting case we set the minimum frequency
506 * not depending on HZ, but fixed (very low). The deferred
507 * timer might skip some samples if idle/sleeping as needed.
508 */
509 dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
510 } else {
511 tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000512
513 /* For correct statistics, we need 10 ticks for each measure */
514 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
515 jiffies_to_usecs(10);
516 }
517
518 tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530519 tuners->ignore_nice_load = 0;
Jacob Shinc2837552013-06-25 22:42:37 +0200520 tuners->powersave_bias = default_powersave_bias;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000521 tuners->io_is_busy = should_io_be_busy();
522
523 dbs_data->tuners = tuners;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000524 return 0;
525}
526
Viresh Kumar8e0484d2015-06-03 15:57:11 +0530527static void od_exit(struct dbs_data *dbs_data, bool notify)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000528{
529 kfree(dbs_data->tuners);
530}
531
Viresh Kumar4471a342012-10-26 00:47:42 +0200532define_get_cpu_dbs_routines(od_cpu_dbs_info);
Mike Chan00e299f2010-01-26 17:06:47 -0800533
Viresh Kumar4471a342012-10-26 00:47:42 +0200534static struct od_ops od_ops = {
Viresh Kumar4471a342012-10-26 00:47:42 +0200535 .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu,
Jacob Shinfb308092013-04-02 09:56:56 -0500536 .powersave_bias_target = generic_powersave_bias_target,
Viresh Kumar4471a342012-10-26 00:47:42 +0200537 .freq_increase = dbs_freq_increase,
538};
539
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100540static struct dbs_governor od_dbs_gov = {
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100541 .gov = {
542 .name = "ondemand",
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100543 .governor = cpufreq_governor_dbs,
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100544 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
545 .owner = THIS_MODULE,
546 },
Viresh Kumar4471a342012-10-26 00:47:42 +0200547 .governor = GOV_ONDEMAND,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000548 .attr_group_gov_sys = &od_attr_group_gov_sys,
549 .attr_group_gov_pol = &od_attr_group_gov_pol,
Viresh Kumar4471a342012-10-26 00:47:42 +0200550 .get_cpu_cdbs = get_cpu_cdbs,
551 .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
552 .gov_dbs_timer = od_dbs_timer,
553 .gov_check_cpu = od_check_cpu,
554 .gov_ops = &od_ops,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000555 .init = od_init,
556 .exit = od_exit,
Viresh Kumar4471a342012-10-26 00:47:42 +0200557};
558
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100559#define CPU_FREQ_GOV_ONDEMAND (&od_dbs_gov.gov)
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100560
Jacob Shinfb308092013-04-02 09:56:56 -0500561static void od_set_powersave_bias(unsigned int powersave_bias)
562{
563 struct cpufreq_policy *policy;
564 struct dbs_data *dbs_data;
565 struct od_dbs_tuners *od_tuners;
566 unsigned int cpu;
567 cpumask_t done;
568
Jacob Shinc2837552013-06-25 22:42:37 +0200569 default_powersave_bias = powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500570 cpumask_clear(&done);
571
572 get_online_cpus();
573 for_each_online_cpu(cpu) {
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100574 struct policy_dbs_info *policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530575
Jacob Shinfb308092013-04-02 09:56:56 -0500576 if (cpumask_test_cpu(cpu, &done))
577 continue;
578
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100579 policy_dbs = per_cpu(od_cpu_dbs_info, cpu).cdbs.policy_dbs;
580 if (!policy_dbs)
Jacob Shinc2837552013-06-25 22:42:37 +0200581 continue;
Jacob Shinfb308092013-04-02 09:56:56 -0500582
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100583 policy = policy_dbs->policy;
Jacob Shinfb308092013-04-02 09:56:56 -0500584 cpumask_or(&done, &done, policy->cpus);
Jacob Shinc2837552013-06-25 22:42:37 +0200585
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100586 if (policy->governor != CPU_FREQ_GOV_ONDEMAND)
Jacob Shinc2837552013-06-25 22:42:37 +0200587 continue;
588
589 dbs_data = policy->governor_data;
590 od_tuners = dbs_data->tuners;
591 od_tuners->powersave_bias = default_powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500592 }
593 put_online_cpus();
594}
595
596void od_register_powersave_bias_handler(unsigned int (*f)
597 (struct cpufreq_policy *, unsigned int, unsigned int),
598 unsigned int powersave_bias)
599{
600 od_ops.powersave_bias_target = f;
601 od_set_powersave_bias(powersave_bias);
602}
603EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
604
605void od_unregister_powersave_bias_handler(void)
606{
607 od_ops.powersave_bias_target = generic_powersave_bias_target;
608 od_set_powersave_bias(0);
609}
610EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612static int __init cpufreq_gov_dbs_init(void)
613{
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100614 return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617static void __exit cpufreq_gov_dbs_exit(void)
618{
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100619 cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700622MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
623MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
624MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -0500625 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700626MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Johannes Weiner69157192008-01-17 15:21:08 -0800628#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100629struct cpufreq_governor *cpufreq_default_governor(void)
630{
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100631 return CPU_FREQ_GOV_ONDEMAND;
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100632}
633
Johannes Weiner69157192008-01-17 15:21:08 -0800634fs_initcall(cpufreq_gov_dbs_init);
635#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800637#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638module_exit(cpufreq_gov_dbs_exit);