blob: b7ef2e7f4d4a4e497ff8039738b32266fa10cb31 [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);
Rafael J. Wysockibc505472016-02-07 16:24:26 +010081 struct policy_dbs_info *policy_dbs = policy->governor_data;
82 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000083 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040084
85 if (!dbs_info->freq_table) {
86 dbs_info->freq_lo = 0;
87 dbs_info->freq_lo_jiffies = 0;
88 return freq_next;
89 }
90
91 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
92 relation, &index);
93 freq_req = dbs_info->freq_table[index].frequency;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000094 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040095 freq_avg = freq_req - freq_reduc;
96
97 /* Find freq bounds for freq_avg in freq_table */
98 index = 0;
99 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
100 CPUFREQ_RELATION_H, &index);
101 freq_lo = dbs_info->freq_table[index].frequency;
102 index = 0;
103 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
104 CPUFREQ_RELATION_L, &index);
105 freq_hi = dbs_info->freq_table[index].frequency;
106
107 /* Find out how long we have to be in hi and lo freqs */
108 if (freq_hi == freq_lo) {
109 dbs_info->freq_lo = 0;
110 dbs_info->freq_lo_jiffies = 0;
111 return freq_lo;
112 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000113 jiffies_total = usecs_to_jiffies(od_tuners->sampling_rate);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400114 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
115 jiffies_hi += ((freq_hi - freq_lo) / 2);
116 jiffies_hi /= (freq_hi - freq_lo);
117 jiffies_lo = jiffies_total - jiffies_hi;
118 dbs_info->freq_lo = freq_lo;
119 dbs_info->freq_lo_jiffies = jiffies_lo;
120 dbs_info->freq_hi_jiffies = jiffies_hi;
121 return freq_hi;
122}
123
124static void ondemand_powersave_bias_init(void)
125{
126 int i;
127 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700128 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400129 }
130}
131
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530132static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
Viresh Kumar4471a342012-10-26 00:47:42 +0200133{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100134 struct policy_dbs_info *policy_dbs = policy->governor_data;
135 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000136 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
137
138 if (od_tuners->powersave_bias)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530139 freq = od_ops.powersave_bias_target(policy, freq,
Jacob Shinfb308092013-04-02 09:56:56 -0500140 CPUFREQ_RELATION_H);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530141 else if (policy->cur == policy->max)
Viresh Kumar4471a342012-10-26 00:47:42 +0200142 return;
143
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530144 __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
Viresh Kumar4471a342012-10-26 00:47:42 +0200145 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
146}
147
148/*
149 * Every sampling_rate, we check, if current idle time is less than 20%
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300150 * (default), then we try to increase frequency. Else, we adjust the frequency
151 * proportional to load.
Viresh Kumar4471a342012-10-26 00:47:42 +0200152 */
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300153static void od_check_cpu(int cpu, unsigned int load)
Viresh Kumar4471a342012-10-26 00:47:42 +0200154{
155 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100156 struct policy_dbs_info *policy_dbs = dbs_info->cdbs.policy_dbs;
157 struct cpufreq_policy *policy = policy_dbs->policy;
158 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000159 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar4471a342012-10-26 00:47:42 +0200160
161 dbs_info->freq_lo = 0;
162
163 /* Check for frequency increase */
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300164 if (load > od_tuners->up_threshold) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200165 /* If switching to max speed, apply sampling_down_factor */
166 if (policy->cur < policy->max)
167 dbs_info->rate_mult =
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000168 od_tuners->sampling_down_factor;
Viresh Kumar4471a342012-10-26 00:47:42 +0200169 dbs_freq_increase(policy, policy->max);
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300170 } else {
171 /* Calculate the next frequency proportional to load */
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300172 unsigned int freq_next, min_f, max_f;
173
174 min_f = policy->cpuinfo.min_freq;
175 max_f = policy->cpuinfo.max_freq;
176 freq_next = min_f + load * (max_f - min_f) / 100;
Viresh Kumar4471a342012-10-26 00:47:42 +0200177
178 /* No longer fully busy, reset rate_mult */
179 dbs_info->rate_mult = 1;
180
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000181 if (!od_tuners->powersave_bias) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200182 __cpufreq_driver_target(policy, freq_next,
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300183 CPUFREQ_RELATION_C);
Jacob Shinfb308092013-04-02 09:56:56 -0500184 return;
Viresh Kumar4471a342012-10-26 00:47:42 +0200185 }
Jacob Shinfb308092013-04-02 09:56:56 -0500186
187 freq_next = od_ops.powersave_bias_target(policy, freq_next,
188 CPUFREQ_RELATION_L);
Stratos Karafotis6393d6a2014-06-30 19:59:34 +0300189 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C);
Viresh Kumar4471a342012-10-26 00:47:42 +0200190 }
191}
192
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100193static unsigned int od_dbs_timer(struct cpufreq_policy *policy)
Fabio Baltierida53d612012-12-27 14:55:40 +0000194{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100195 struct policy_dbs_info *policy_dbs = policy->governor_data;
196 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +0100197 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000198 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530199 int delay = 0, sample_type = dbs_info->sample_type;
Fabio Baltierida53d612012-12-27 14:55:40 +0000200
Viresh Kumar44472662013-01-31 17:28:02 +0000201 /* Common NORMAL_SAMPLE setup */
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530202 dbs_info->sample_type = OD_NORMAL_SAMPLE;
Viresh Kumar44472662013-01-31 17:28:02 +0000203 if (sample_type == OD_SUB_SAMPLE) {
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530204 delay = dbs_info->freq_lo_jiffies;
205 __cpufreq_driver_target(policy, dbs_info->freq_lo,
Viresh Kumar42994af2015-06-19 17:18:05 +0530206 CPUFREQ_RELATION_H);
Fabio Baltierida53d612012-12-27 14:55:40 +0000207 } else {
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +0100208 dbs_check_cpu(policy);
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530209 if (dbs_info->freq_lo) {
Viresh Kumar44472662013-01-31 17:28:02 +0000210 /* Setup timer for SUB_SAMPLE */
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530211 dbs_info->sample_type = OD_SUB_SAMPLE;
212 delay = dbs_info->freq_hi_jiffies;
Viresh Kumar44472662013-01-31 17:28:02 +0000213 }
Fabio Baltierida53d612012-12-27 14:55:40 +0000214 }
Viresh Kumar44472662013-01-31 17:28:02 +0000215
Viresh Kumar9d445922013-02-27 11:06:36 +0530216 if (!delay)
217 delay = delay_for_sampling_rate(od_tuners->sampling_rate
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530218 * dbs_info->rate_mult);
Viresh Kumar9d445922013-02-27 11:06:36 +0530219
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530220 return delay;
Fabio Baltierida53d612012-12-27 14:55:40 +0000221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/************************** sysfs interface ************************/
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100224static struct dbs_governor od_dbs_gov;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900226/**
227 * update_sampling_rate - update sampling rate effective immediately if needed.
228 * @new_rate: new sampling rate
229 *
Stratos Karafotis06eb09d2013-02-08 17:24:18 +0000230 * If new rate is smaller than the old, simply updating
Viresh Kumar4471a342012-10-26 00:47:42 +0200231 * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
232 * original sampling_rate was 1 second and the requested new sampling rate is 10
233 * ms because the user needs immediate reaction from ondemand governor, but not
234 * sure if higher frequency will be required or not, then, the governor may
235 * change the sampling rate too late; up to 1 second later. Thus, if we are
236 * reducing the sampling rate, we need to make the new value effective
237 * immediately.
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900238 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000239static void update_sampling_rate(struct dbs_data *dbs_data,
240 unsigned int new_rate)
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900241{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000242 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumarf08f6382015-12-03 09:37:54 +0530243 struct cpumask cpumask;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900244 int cpu;
245
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000246 od_tuners->sampling_rate = new_rate = max(new_rate,
247 dbs_data->min_sampling_rate);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900248
Viresh Kumare128c862015-12-03 09:37:49 +0530249 /*
250 * Lock governor so that governor start/stop can't execute in parallel.
251 */
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100252 mutex_lock(&dbs_data_mutex);
Viresh Kumare128c862015-12-03 09:37:49 +0530253
Viresh Kumarf08f6382015-12-03 09:37:54 +0530254 cpumask_copy(&cpumask, cpu_online_mask);
255
256 for_each_cpu(cpu, &cpumask) {
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900257 struct cpufreq_policy *policy;
Viresh Kumar4471a342012-10-26 00:47:42 +0200258 struct od_cpu_dbs_info_s *dbs_info;
Viresh Kumare128c862015-12-03 09:37:49 +0530259 struct cpu_dbs_info *cdbs;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100260 struct policy_dbs_info *policy_dbs;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900261
Fabio Baltieri8ee2ec52012-12-27 14:55:42 +0000262 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Viresh Kumare128c862015-12-03 09:37:49 +0530263 cdbs = &dbs_info->cdbs;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100264 policy_dbs = cdbs->policy_dbs;
Viresh Kumare128c862015-12-03 09:37:49 +0530265
266 /*
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100267 * A valid policy_dbs and policy_dbs->policy means governor
268 * hasn't stopped or exited yet.
Viresh Kumare128c862015-12-03 09:37:49 +0530269 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100270 if (!policy_dbs || !policy_dbs->policy)
Viresh Kumare128c862015-12-03 09:37:49 +0530271 continue;
272
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100273 policy = policy_dbs->policy;
Viresh Kumare128c862015-12-03 09:37:49 +0530274
Viresh Kumarf08f6382015-12-03 09:37:54 +0530275 /* clear all CPUs of this policy */
276 cpumask_andnot(&cpumask, &cpumask, policy->cpus);
277
Viresh Kumare128c862015-12-03 09:37:49 +0530278 /*
279 * Update sampling rate for CPUs whose policy is governed by
280 * dbs_data. In case of governor_per_policy, only a single
281 * policy will be governed by dbs_data, otherwise there can be
282 * multiple policies that are governed by the same dbs_data.
283 */
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100284 if (dbs_data == policy_dbs->dbs_data) {
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100285 mutex_lock(&policy_dbs->timer_mutex);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100286 /*
287 * On 32-bit architectures this may race with the
288 * sample_delay_ns read in dbs_update_util_handler(),
289 * but that really doesn't matter. If the read returns
290 * a value that's too big, the sample will be skipped,
291 * but the next invocation of dbs_update_util_handler()
292 * (when the update has been completed) will take a
293 * sample. If the returned value is too small, the
294 * sample will be taken immediately, but that isn't a
295 * problem, as we want the new rate to take effect
296 * immediately anyway.
297 *
298 * If this runs in parallel with dbs_work_handler(), we
299 * may end up overwriting the sample_delay_ns value that
300 * it has just written, but the difference should not be
301 * too big and it will be corrected next time a sample
302 * is taken, so it shouldn't be significant.
303 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100304 gov_update_sample_delay(policy_dbs, new_rate);
305 mutex_unlock(&policy_dbs->timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900306 }
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900307 }
Viresh Kumare128c862015-12-03 09:37:49 +0530308
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100309 mutex_unlock(&dbs_data_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900310}
311
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000312static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
313 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 unsigned int input;
316 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700317 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700318 if (ret != 1)
319 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000320
321 update_sampling_rate(dbs_data, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return count;
323}
324
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000325static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf,
326 size_t count)
Arjan van de Ven19379b12010-05-09 08:26:51 -0700327{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000328 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700329 unsigned int input;
330 int ret;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000331 unsigned int j;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700332
333 ret = sscanf(buf, "%u", &input);
334 if (ret != 1)
335 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000336 od_tuners->io_is_busy = !!input;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000337
338 /* we need to re-evaluate prev_cpu_idle */
339 for_each_online_cpu(j) {
340 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
341 j);
342 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
343 &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
344 }
Arjan van de Ven19379b12010-05-09 08:26:51 -0700345 return count;
346}
347
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000348static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
349 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000351 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unsigned int input;
353 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700354 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Dave Jones32ee8c32006-02-28 00:43:23 -0500356 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700357 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return -EINVAL;
359 }
Stratos Karafotis4bd4e422013-02-06 13:34:00 +0100360
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000361 od_tuners->up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return count;
363}
364
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000365static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
366 const char *buf, size_t count)
David C Niemi3f78a9f2010-10-06 16:54:24 -0400367{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000368 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400369 unsigned int input, j;
370 int ret;
371 ret = sscanf(buf, "%u", &input);
372
373 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
374 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000375 od_tuners->sampling_down_factor = input;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400376
377 /* Reset down sampling multiplier in case it was active */
378 for_each_online_cpu(j) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200379 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
380 j);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400381 dbs_info->rate_mult = 1;
382 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400383 return count;
384}
385
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530386static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data,
387 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700388{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000389 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700390 unsigned int input;
391 int ret;
392
393 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500394
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700395 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500396 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700397 return -EINVAL;
398
Dave Jones2b03f892009-01-18 01:43:44 -0500399 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700400 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500401
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530402 if (input == od_tuners->ignore_nice_load) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700403 return count;
404 }
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530405 od_tuners->ignore_nice_load = input;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700406
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700407 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700408 for_each_online_cpu(j) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200409 struct od_cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900410 dbs_info = &per_cpu(od_cpu_dbs_info, j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200411 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
Stratos Karafotis9366d842013-02-28 16:57:32 +0000412 &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530413 if (od_tuners->ignore_nice_load)
Viresh Kumar4471a342012-10-26 00:47:42 +0200414 dbs_info->cdbs.prev_cpu_nice =
415 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500416
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700417 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700418 return count;
419}
420
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000421static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf,
422 size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400423{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000424 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400425 unsigned int input;
426 int ret;
427 ret = sscanf(buf, "%u", &input);
428
429 if (ret != 1)
430 return -EINVAL;
431
432 if (input > 1000)
433 input = 1000;
434
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000435 od_tuners->powersave_bias = input;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400436 ondemand_powersave_bias_init();
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400437 return count;
438}
439
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000440show_store_one(od, sampling_rate);
441show_store_one(od, io_is_busy);
442show_store_one(od, up_threshold);
443show_store_one(od, sampling_down_factor);
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530444show_store_one(od, ignore_nice_load);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000445show_store_one(od, powersave_bias);
446declare_show_sampling_rate_min(od);
Viresh Kumar4471a342012-10-26 00:47:42 +0200447
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000448gov_sys_pol_attr_rw(sampling_rate);
449gov_sys_pol_attr_rw(io_is_busy);
450gov_sys_pol_attr_rw(up_threshold);
451gov_sys_pol_attr_rw(sampling_down_factor);
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530452gov_sys_pol_attr_rw(ignore_nice_load);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000453gov_sys_pol_attr_rw(powersave_bias);
454gov_sys_pol_attr_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000456static struct attribute *dbs_attributes_gov_sys[] = {
457 &sampling_rate_min_gov_sys.attr,
458 &sampling_rate_gov_sys.attr,
459 &up_threshold_gov_sys.attr,
460 &sampling_down_factor_gov_sys.attr,
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530461 &ignore_nice_load_gov_sys.attr,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000462 &powersave_bias_gov_sys.attr,
463 &io_is_busy_gov_sys.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 NULL
465};
466
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000467static struct attribute_group od_attr_group_gov_sys = {
468 .attrs = dbs_attributes_gov_sys,
469 .name = "ondemand",
470};
471
472static struct attribute *dbs_attributes_gov_pol[] = {
473 &sampling_rate_min_gov_pol.attr,
474 &sampling_rate_gov_pol.attr,
475 &up_threshold_gov_pol.attr,
476 &sampling_down_factor_gov_pol.attr,
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530477 &ignore_nice_load_gov_pol.attr,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000478 &powersave_bias_gov_pol.attr,
479 &io_is_busy_gov_pol.attr,
480 NULL
481};
482
483static struct attribute_group od_attr_group_gov_pol = {
484 .attrs = dbs_attributes_gov_pol,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .name = "ondemand",
486};
487
488/************************** sysfs end ************************/
489
Viresh Kumar8e0484d2015-06-03 15:57:11 +0530490static int od_init(struct dbs_data *dbs_data, bool notify)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000491{
492 struct od_dbs_tuners *tuners;
493 u64 idle_time;
494 int cpu;
495
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530496 tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000497 if (!tuners) {
498 pr_err("%s: kzalloc failed\n", __func__);
499 return -ENOMEM;
500 }
501
502 cpu = get_cpu();
503 idle_time = get_cpu_idle_time_us(cpu, NULL);
504 put_cpu();
505 if (idle_time != -1ULL) {
506 /* Idle micro accounting is supported. Use finer thresholds */
507 tuners->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000508 /*
509 * In nohz/micro accounting case we set the minimum frequency
510 * not depending on HZ, but fixed (very low). The deferred
511 * timer might skip some samples if idle/sleeping as needed.
512 */
513 dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
514 } else {
515 tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000516
517 /* For correct statistics, we need 10 ticks for each measure */
518 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
519 jiffies_to_usecs(10);
520 }
521
522 tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
Viresh Kumar6c4640c2013-08-05 12:28:02 +0530523 tuners->ignore_nice_load = 0;
Jacob Shinc2837552013-06-25 22:42:37 +0200524 tuners->powersave_bias = default_powersave_bias;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000525 tuners->io_is_busy = should_io_be_busy();
526
527 dbs_data->tuners = tuners;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000528 return 0;
529}
530
Viresh Kumar8e0484d2015-06-03 15:57:11 +0530531static void od_exit(struct dbs_data *dbs_data, bool notify)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000532{
533 kfree(dbs_data->tuners);
534}
535
Viresh Kumar4471a342012-10-26 00:47:42 +0200536define_get_cpu_dbs_routines(od_cpu_dbs_info);
Mike Chan00e299f2010-01-26 17:06:47 -0800537
Viresh Kumar4471a342012-10-26 00:47:42 +0200538static struct od_ops od_ops = {
Viresh Kumar4471a342012-10-26 00:47:42 +0200539 .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu,
Jacob Shinfb308092013-04-02 09:56:56 -0500540 .powersave_bias_target = generic_powersave_bias_target,
Viresh Kumar4471a342012-10-26 00:47:42 +0200541 .freq_increase = dbs_freq_increase,
542};
543
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100544static struct dbs_governor od_dbs_gov = {
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100545 .gov = {
546 .name = "ondemand",
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100547 .governor = cpufreq_governor_dbs,
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100548 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
549 .owner = THIS_MODULE,
550 },
Viresh Kumar4471a342012-10-26 00:47:42 +0200551 .governor = GOV_ONDEMAND,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000552 .attr_group_gov_sys = &od_attr_group_gov_sys,
553 .attr_group_gov_pol = &od_attr_group_gov_pol,
Viresh Kumar4471a342012-10-26 00:47:42 +0200554 .get_cpu_cdbs = get_cpu_cdbs,
555 .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
556 .gov_dbs_timer = od_dbs_timer,
557 .gov_check_cpu = od_check_cpu,
558 .gov_ops = &od_ops,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000559 .init = od_init,
560 .exit = od_exit,
Viresh Kumar4471a342012-10-26 00:47:42 +0200561};
562
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100563#define CPU_FREQ_GOV_ONDEMAND (&od_dbs_gov.gov)
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100564
Jacob Shinfb308092013-04-02 09:56:56 -0500565static void od_set_powersave_bias(unsigned int powersave_bias)
566{
567 struct cpufreq_policy *policy;
568 struct dbs_data *dbs_data;
569 struct od_dbs_tuners *od_tuners;
570 unsigned int cpu;
571 cpumask_t done;
572
Jacob Shinc2837552013-06-25 22:42:37 +0200573 default_powersave_bias = powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500574 cpumask_clear(&done);
575
576 get_online_cpus();
577 for_each_online_cpu(cpu) {
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100578 struct policy_dbs_info *policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530579
Jacob Shinfb308092013-04-02 09:56:56 -0500580 if (cpumask_test_cpu(cpu, &done))
581 continue;
582
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100583 policy_dbs = per_cpu(od_cpu_dbs_info, cpu).cdbs.policy_dbs;
584 if (!policy_dbs)
Jacob Shinc2837552013-06-25 22:42:37 +0200585 continue;
Jacob Shinfb308092013-04-02 09:56:56 -0500586
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100587 policy = policy_dbs->policy;
Jacob Shinfb308092013-04-02 09:56:56 -0500588 cpumask_or(&done, &done, policy->cpus);
Jacob Shinc2837552013-06-25 22:42:37 +0200589
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100590 if (policy->governor != CPU_FREQ_GOV_ONDEMAND)
Jacob Shinc2837552013-06-25 22:42:37 +0200591 continue;
592
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100593 dbs_data = policy_dbs->dbs_data;
Jacob Shinc2837552013-06-25 22:42:37 +0200594 od_tuners = dbs_data->tuners;
595 od_tuners->powersave_bias = default_powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500596 }
597 put_online_cpus();
598}
599
600void od_register_powersave_bias_handler(unsigned int (*f)
601 (struct cpufreq_policy *, unsigned int, unsigned int),
602 unsigned int powersave_bias)
603{
604 od_ops.powersave_bias_target = f;
605 od_set_powersave_bias(powersave_bias);
606}
607EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
608
609void od_unregister_powersave_bias_handler(void)
610{
611 od_ops.powersave_bias_target = generic_powersave_bias_target;
612 od_set_powersave_bias(0);
613}
614EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616static int __init cpufreq_gov_dbs_init(void)
617{
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100618 return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621static void __exit cpufreq_gov_dbs_exit(void)
622{
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100623 cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700626MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
627MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
628MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -0500629 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700630MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Johannes Weiner69157192008-01-17 15:21:08 -0800632#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100633struct cpufreq_governor *cpufreq_default_governor(void)
634{
Rafael J. Wysockiaf926182016-02-05 03:16:08 +0100635 return CPU_FREQ_GOV_ONDEMAND;
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100636}
637
Johannes Weiner69157192008-01-17 15:21:08 -0800638fs_initcall(cpufreq_gov_dbs_init);
639#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642module_exit(cpufreq_gov_dbs_exit);