blob: 93eb5cbcc1f639bf1ec52479a169a49deecd72f5 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/cpufreq.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020016#include <linux/init.h>
17#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel_stat.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020019#include <linux/kobject.h>
20#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080021#include <linux/mutex.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020022#include <linux/percpu-defs.h>
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000023#include <linux/slab.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020024#include <linux/sysfs.h>
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070025#include <linux/tick.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020026#include <linux/types.h>
Jacob Shinfb308092013-04-02 09:56:56 -050027#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Viresh Kumar4471a342012-10-26 00:47:42 +020029#include "cpufreq_governor.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000031/* On-demand governor macros */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -070032#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define DEF_FREQUENCY_UP_THRESHOLD (80)
David C Niemi3f78a9f2010-10-06 16:54:24 -040034#define DEF_SAMPLING_DOWN_FACTOR (1)
35#define MAX_SAMPLING_DOWN_FACTOR (100000)
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070036#define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
37#define MICRO_FREQUENCY_UP_THRESHOLD (95)
Thomas Renningercef96152009-04-22 13:48:29 +020038#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
Dave Jonesc29f1402005-05-31 19:03:50 -070039#define MIN_FREQUENCY_UP_THRESHOLD (11)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define MAX_FREQUENCY_UP_THRESHOLD (100)
41
Viresh Kumar4471a342012-10-26 00:47:42 +020042static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info);
Thomas Renninger112124a2009-02-04 11:55:12 +010043
Jacob Shinfb308092013-04-02 09:56:56 -050044static struct od_ops od_ops;
45
Fabio Baltieri3e33ee92012-11-26 18:10:12 +000046#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
47static struct cpufreq_governor cpufreq_gov_ondemand;
48#endif
49
Jacob Shinc2837552013-06-25 22:42:37 +020050static unsigned int default_powersave_bias;
51
Viresh Kumar4471a342012-10-26 00:47:42 +020052static void ondemand_powersave_bias_init_cpu(int cpu)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070053{
Viresh Kumar4471a342012-10-26 00:47:42 +020054 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070055
Viresh Kumar4471a342012-10-26 00:47:42 +020056 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
57 dbs_info->freq_lo = 0;
58}
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070059
Viresh Kumar4471a342012-10-26 00:47:42 +020060/*
61 * Not all CPUs want IO time to be accounted as busy; this depends on how
62 * efficient idling at a higher frequency/voltage is.
63 * Pavel Machek says this is not so for various generations of AMD and old
64 * Intel systems.
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000065 * Mike Chan (android.com) claims this is also not true for ARM.
Viresh Kumar4471a342012-10-26 00:47:42 +020066 * Because of this, whitelist specific known (series) of CPUs by default, and
67 * leave all others up to the user.
68 */
69static int should_io_be_busy(void)
70{
71#if defined(CONFIG_X86)
72 /*
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000073 * For Intel, Core 2 (model 15) and later have an efficient idle.
Viresh Kumar4471a342012-10-26 00:47:42 +020074 */
75 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
76 boot_cpu_data.x86 == 6 &&
77 boot_cpu_data.x86_model >= 15)
78 return 1;
79#endif
80 return 0;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070081}
82
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040083/*
84 * Find right freq to be set now with powersave_bias on.
85 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
86 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
87 */
Jacob Shinfb308092013-04-02 09:56:56 -050088static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
Viresh Kumar4471a342012-10-26 00:47:42 +020089 unsigned int freq_next, unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040090{
91 unsigned int freq_req, freq_reduc, freq_avg;
92 unsigned int freq_hi, freq_lo;
93 unsigned int index = 0;
94 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
Viresh Kumar4471a342012-10-26 00:47:42 +020095 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
Tejun Heo245b2e72009-06-24 15:13:48 +090096 policy->cpu);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000097 struct dbs_data *dbs_data = policy->governor_data;
98 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040099
100 if (!dbs_info->freq_table) {
101 dbs_info->freq_lo = 0;
102 dbs_info->freq_lo_jiffies = 0;
103 return freq_next;
104 }
105
106 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
107 relation, &index);
108 freq_req = dbs_info->freq_table[index].frequency;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000109 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400110 freq_avg = freq_req - freq_reduc;
111
112 /* Find freq bounds for freq_avg in freq_table */
113 index = 0;
114 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
115 CPUFREQ_RELATION_H, &index);
116 freq_lo = dbs_info->freq_table[index].frequency;
117 index = 0;
118 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
119 CPUFREQ_RELATION_L, &index);
120 freq_hi = dbs_info->freq_table[index].frequency;
121
122 /* Find out how long we have to be in hi and lo freqs */
123 if (freq_hi == freq_lo) {
124 dbs_info->freq_lo = 0;
125 dbs_info->freq_lo_jiffies = 0;
126 return freq_lo;
127 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000128 jiffies_total = usecs_to_jiffies(od_tuners->sampling_rate);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400129 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
130 jiffies_hi += ((freq_hi - freq_lo) / 2);
131 jiffies_hi /= (freq_hi - freq_lo);
132 jiffies_lo = jiffies_total - jiffies_hi;
133 dbs_info->freq_lo = freq_lo;
134 dbs_info->freq_lo_jiffies = jiffies_lo;
135 dbs_info->freq_hi_jiffies = jiffies_hi;
136 return freq_hi;
137}
138
139static void ondemand_powersave_bias_init(void)
140{
141 int i;
142 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700143 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400144 }
145}
146
Viresh Kumar4471a342012-10-26 00:47:42 +0200147static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
148{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000149 struct dbs_data *dbs_data = p->governor_data;
150 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
151
152 if (od_tuners->powersave_bias)
Jacob Shinfb308092013-04-02 09:56:56 -0500153 freq = od_ops.powersave_bias_target(p, freq,
154 CPUFREQ_RELATION_H);
Viresh Kumar4471a342012-10-26 00:47:42 +0200155 else if (p->cur == p->max)
156 return;
157
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000158 __cpufreq_driver_target(p, freq, od_tuners->powersave_bias ?
Viresh Kumar4471a342012-10-26 00:47:42 +0200159 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
160}
161
162/*
163 * Every sampling_rate, we check, if current idle time is less than 20%
Stratos Karafotis06eb09d2013-02-08 17:24:18 +0000164 * (default), then we try to increase frequency. Every sampling_rate, we look
165 * for the lowest frequency which can sustain the load while keeping idle time
Viresh Kumar4471a342012-10-26 00:47:42 +0200166 * over 30%. If such a frequency exist, we try to decrease to this frequency.
167 *
168 * Any frequency increase takes it to the maximum frequency. Frequency reduction
169 * happens at minimum steps of 5% (default) of current frequency
170 */
171static void od_check_cpu(int cpu, unsigned int load_freq)
172{
173 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
174 struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000175 struct dbs_data *dbs_data = policy->governor_data;
176 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar4471a342012-10-26 00:47:42 +0200177
178 dbs_info->freq_lo = 0;
179
180 /* Check for frequency increase */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000181 if (load_freq > od_tuners->up_threshold * policy->cur) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200182 /* If switching to max speed, apply sampling_down_factor */
183 if (policy->cur < policy->max)
184 dbs_info->rate_mult =
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000185 od_tuners->sampling_down_factor;
Viresh Kumar4471a342012-10-26 00:47:42 +0200186 dbs_freq_increase(policy, policy->max);
187 return;
188 }
189
190 /* Check for frequency decrease */
191 /* if we cannot reduce the frequency anymore, break out early */
192 if (policy->cur == policy->min)
193 return;
194
195 /*
196 * The optimal frequency is the frequency that is the lowest that can
197 * support the current CPU usage without triggering the up policy. To be
198 * safe, we focus 10 points under the threshold.
199 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000200 if (load_freq < od_tuners->adj_up_threshold
201 * policy->cur) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200202 unsigned int freq_next;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000203 freq_next = load_freq / od_tuners->adj_up_threshold;
Viresh Kumar4471a342012-10-26 00:47:42 +0200204
205 /* No longer fully busy, reset rate_mult */
206 dbs_info->rate_mult = 1;
207
208 if (freq_next < policy->min)
209 freq_next = policy->min;
210
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000211 if (!od_tuners->powersave_bias) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200212 __cpufreq_driver_target(policy, freq_next,
213 CPUFREQ_RELATION_L);
Jacob Shinfb308092013-04-02 09:56:56 -0500214 return;
Viresh Kumar4471a342012-10-26 00:47:42 +0200215 }
Jacob Shinfb308092013-04-02 09:56:56 -0500216
217 freq_next = od_ops.powersave_bias_target(policy, freq_next,
218 CPUFREQ_RELATION_L);
219 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
Viresh Kumar4471a342012-10-26 00:47:42 +0200220 }
221}
222
Fabio Baltierida53d612012-12-27 14:55:40 +0000223static void od_dbs_timer(struct work_struct *work)
224{
Fabio Baltierida53d612012-12-27 14:55:40 +0000225 struct od_cpu_dbs_info_s *dbs_info =
226 container_of(work, struct od_cpu_dbs_info_s, cdbs.work.work);
Viresh Kumar44472662013-01-31 17:28:02 +0000227 unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
228 struct od_cpu_dbs_info_s *core_dbs_info = &per_cpu(od_cpu_dbs_info,
229 cpu);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000230 struct dbs_data *dbs_data = dbs_info->cdbs.cur_policy->governor_data;
231 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar9d445922013-02-27 11:06:36 +0530232 int delay = 0, sample_type = core_dbs_info->sample_type;
Viresh Kumar031299b2013-02-27 12:24:03 +0530233 bool modify_all = true;
Fabio Baltierida53d612012-12-27 14:55:40 +0000234
Viresh Kumar44472662013-01-31 17:28:02 +0000235 mutex_lock(&core_dbs_info->cdbs.timer_mutex);
Viresh Kumar031299b2013-02-27 12:24:03 +0530236 if (!need_load_eval(&core_dbs_info->cdbs, od_tuners->sampling_rate)) {
237 modify_all = false;
Viresh Kumar9d445922013-02-27 11:06:36 +0530238 goto max_delay;
Viresh Kumar031299b2013-02-27 12:24:03 +0530239 }
Viresh Kumar44472662013-01-31 17:28:02 +0000240
241 /* Common NORMAL_SAMPLE setup */
242 core_dbs_info->sample_type = OD_NORMAL_SAMPLE;
243 if (sample_type == OD_SUB_SAMPLE) {
244 delay = core_dbs_info->freq_lo_jiffies;
Viresh Kumar9d445922013-02-27 11:06:36 +0530245 __cpufreq_driver_target(core_dbs_info->cdbs.cur_policy,
246 core_dbs_info->freq_lo, CPUFREQ_RELATION_H);
Fabio Baltierida53d612012-12-27 14:55:40 +0000247 } else {
Viresh Kumar9d445922013-02-27 11:06:36 +0530248 dbs_check_cpu(dbs_data, cpu);
Viresh Kumar44472662013-01-31 17:28:02 +0000249 if (core_dbs_info->freq_lo) {
250 /* Setup timer for SUB_SAMPLE */
251 core_dbs_info->sample_type = OD_SUB_SAMPLE;
252 delay = core_dbs_info->freq_hi_jiffies;
Viresh Kumar44472662013-01-31 17:28:02 +0000253 }
Fabio Baltierida53d612012-12-27 14:55:40 +0000254 }
Viresh Kumar44472662013-01-31 17:28:02 +0000255
Viresh Kumar9d445922013-02-27 11:06:36 +0530256max_delay:
257 if (!delay)
258 delay = delay_for_sampling_rate(od_tuners->sampling_rate
259 * core_dbs_info->rate_mult);
260
Viresh Kumar031299b2013-02-27 12:24:03 +0530261 gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy, delay, modify_all);
Viresh Kumar44472662013-01-31 17:28:02 +0000262 mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
Fabio Baltierida53d612012-12-27 14:55:40 +0000263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/************************** sysfs interface ************************/
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000266static struct common_dbs_data od_dbs_cdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900268/**
269 * update_sampling_rate - update sampling rate effective immediately if needed.
270 * @new_rate: new sampling rate
271 *
Stratos Karafotis06eb09d2013-02-08 17:24:18 +0000272 * If new rate is smaller than the old, simply updating
Viresh Kumar4471a342012-10-26 00:47:42 +0200273 * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
274 * original sampling_rate was 1 second and the requested new sampling rate is 10
275 * ms because the user needs immediate reaction from ondemand governor, but not
276 * sure if higher frequency will be required or not, then, the governor may
277 * change the sampling rate too late; up to 1 second later. Thus, if we are
278 * reducing the sampling rate, we need to make the new value effective
279 * immediately.
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900280 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000281static void update_sampling_rate(struct dbs_data *dbs_data,
282 unsigned int new_rate)
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900283{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000284 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900285 int cpu;
286
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000287 od_tuners->sampling_rate = new_rate = max(new_rate,
288 dbs_data->min_sampling_rate);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900289
290 for_each_online_cpu(cpu) {
291 struct cpufreq_policy *policy;
Viresh Kumar4471a342012-10-26 00:47:42 +0200292 struct od_cpu_dbs_info_s *dbs_info;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900293 unsigned long next_sampling, appointed_at;
294
295 policy = cpufreq_cpu_get(cpu);
296 if (!policy)
297 continue;
Fabio Baltieri3e33ee92012-11-26 18:10:12 +0000298 if (policy->governor != &cpufreq_gov_ondemand) {
299 cpufreq_cpu_put(policy);
300 continue;
301 }
Fabio Baltieri8ee2ec52012-12-27 14:55:42 +0000302 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900303 cpufreq_cpu_put(policy);
304
Viresh Kumar4471a342012-10-26 00:47:42 +0200305 mutex_lock(&dbs_info->cdbs.timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900306
Viresh Kumar4471a342012-10-26 00:47:42 +0200307 if (!delayed_work_pending(&dbs_info->cdbs.work)) {
308 mutex_unlock(&dbs_info->cdbs.timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900309 continue;
310 }
311
Viresh Kumar4471a342012-10-26 00:47:42 +0200312 next_sampling = jiffies + usecs_to_jiffies(new_rate);
313 appointed_at = dbs_info->cdbs.work.timer.expires;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900314
315 if (time_before(next_sampling, appointed_at)) {
316
Viresh Kumar4471a342012-10-26 00:47:42 +0200317 mutex_unlock(&dbs_info->cdbs.timer_mutex);
318 cancel_delayed_work_sync(&dbs_info->cdbs.work);
319 mutex_lock(&dbs_info->cdbs.timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900320
Viresh Kumar031299b2013-02-27 12:24:03 +0530321 gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy,
322 usecs_to_jiffies(new_rate), true);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900323
324 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200325 mutex_unlock(&dbs_info->cdbs.timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900326 }
327}
328
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000329static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
330 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 unsigned int input;
333 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700334 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700335 if (ret != 1)
336 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000337
338 update_sampling_rate(dbs_data, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return count;
340}
341
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000342static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf,
343 size_t count)
Arjan van de Ven19379b12010-05-09 08:26:51 -0700344{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000345 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700346 unsigned int input;
347 int ret;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000348 unsigned int j;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700349
350 ret = sscanf(buf, "%u", &input);
351 if (ret != 1)
352 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000353 od_tuners->io_is_busy = !!input;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000354
355 /* we need to re-evaluate prev_cpu_idle */
356 for_each_online_cpu(j) {
357 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
358 j);
359 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
360 &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
361 }
Arjan van de Ven19379b12010-05-09 08:26:51 -0700362 return count;
363}
364
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000365static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
366 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000368 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned int input;
370 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700371 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Dave Jones32ee8c32006-02-28 00:43:23 -0500373 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700374 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EINVAL;
376 }
Stratos Karafotis4bd4e422013-02-06 13:34:00 +0100377 /* Calculate the new adj_up_threshold */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000378 od_tuners->adj_up_threshold += input;
379 od_tuners->adj_up_threshold -= od_tuners->up_threshold;
Stratos Karafotis4bd4e422013-02-06 13:34:00 +0100380
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000381 od_tuners->up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return count;
383}
384
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000385static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
386 const char *buf, size_t count)
David C Niemi3f78a9f2010-10-06 16:54:24 -0400387{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000388 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400389 unsigned int input, j;
390 int ret;
391 ret = sscanf(buf, "%u", &input);
392
393 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
394 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000395 od_tuners->sampling_down_factor = input;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400396
397 /* Reset down sampling multiplier in case it was active */
398 for_each_online_cpu(j) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200399 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
400 j);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400401 dbs_info->rate_mult = 1;
402 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400403 return count;
404}
405
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000406static ssize_t store_ignore_nice(struct dbs_data *dbs_data, const char *buf,
407 size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700408{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000409 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700410 unsigned int input;
411 int ret;
412
413 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500414
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700415 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500416 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700417 return -EINVAL;
418
Dave Jones2b03f892009-01-18 01:43:44 -0500419 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700420 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500421
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000422 if (input == od_tuners->ignore_nice) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700423 return count;
424 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000425 od_tuners->ignore_nice = input;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700426
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700427 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700428 for_each_online_cpu(j) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200429 struct od_cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900430 dbs_info = &per_cpu(od_cpu_dbs_info, j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200431 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
Stratos Karafotis9366d842013-02-28 16:57:32 +0000432 &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000433 if (od_tuners->ignore_nice)
Viresh Kumar4471a342012-10-26 00:47:42 +0200434 dbs_info->cdbs.prev_cpu_nice =
435 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500436
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700437 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700438 return count;
439}
440
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000441static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf,
442 size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400443{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000444 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400445 unsigned int input;
446 int ret;
447 ret = sscanf(buf, "%u", &input);
448
449 if (ret != 1)
450 return -EINVAL;
451
452 if (input > 1000)
453 input = 1000;
454
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000455 od_tuners->powersave_bias = input;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400456 ondemand_powersave_bias_init();
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400457 return count;
458}
459
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000460show_store_one(od, sampling_rate);
461show_store_one(od, io_is_busy);
462show_store_one(od, up_threshold);
463show_store_one(od, sampling_down_factor);
464show_store_one(od, ignore_nice);
465show_store_one(od, powersave_bias);
466declare_show_sampling_rate_min(od);
Viresh Kumar4471a342012-10-26 00:47:42 +0200467
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000468gov_sys_pol_attr_rw(sampling_rate);
469gov_sys_pol_attr_rw(io_is_busy);
470gov_sys_pol_attr_rw(up_threshold);
471gov_sys_pol_attr_rw(sampling_down_factor);
472gov_sys_pol_attr_rw(ignore_nice);
473gov_sys_pol_attr_rw(powersave_bias);
474gov_sys_pol_attr_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000476static struct attribute *dbs_attributes_gov_sys[] = {
477 &sampling_rate_min_gov_sys.attr,
478 &sampling_rate_gov_sys.attr,
479 &up_threshold_gov_sys.attr,
480 &sampling_down_factor_gov_sys.attr,
481 &ignore_nice_gov_sys.attr,
482 &powersave_bias_gov_sys.attr,
483 &io_is_busy_gov_sys.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 NULL
485};
486
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000487static struct attribute_group od_attr_group_gov_sys = {
488 .attrs = dbs_attributes_gov_sys,
489 .name = "ondemand",
490};
491
492static struct attribute *dbs_attributes_gov_pol[] = {
493 &sampling_rate_min_gov_pol.attr,
494 &sampling_rate_gov_pol.attr,
495 &up_threshold_gov_pol.attr,
496 &sampling_down_factor_gov_pol.attr,
497 &ignore_nice_gov_pol.attr,
498 &powersave_bias_gov_pol.attr,
499 &io_is_busy_gov_pol.attr,
500 NULL
501};
502
503static struct attribute_group od_attr_group_gov_pol = {
504 .attrs = dbs_attributes_gov_pol,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .name = "ondemand",
506};
507
508/************************** sysfs end ************************/
509
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000510static int od_init(struct dbs_data *dbs_data)
511{
512 struct od_dbs_tuners *tuners;
513 u64 idle_time;
514 int cpu;
515
516 tuners = kzalloc(sizeof(struct od_dbs_tuners), GFP_KERNEL);
517 if (!tuners) {
518 pr_err("%s: kzalloc failed\n", __func__);
519 return -ENOMEM;
520 }
521
522 cpu = get_cpu();
523 idle_time = get_cpu_idle_time_us(cpu, NULL);
524 put_cpu();
525 if (idle_time != -1ULL) {
526 /* Idle micro accounting is supported. Use finer thresholds */
527 tuners->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
528 tuners->adj_up_threshold = MICRO_FREQUENCY_UP_THRESHOLD -
529 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
530 /*
531 * In nohz/micro accounting case we set the minimum frequency
532 * not depending on HZ, but fixed (very low). The deferred
533 * timer might skip some samples if idle/sleeping as needed.
534 */
535 dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
536 } else {
537 tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
538 tuners->adj_up_threshold = DEF_FREQUENCY_UP_THRESHOLD -
539 DEF_FREQUENCY_DOWN_DIFFERENTIAL;
540
541 /* For correct statistics, we need 10 ticks for each measure */
542 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
543 jiffies_to_usecs(10);
544 }
545
546 tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
547 tuners->ignore_nice = 0;
Jacob Shinc2837552013-06-25 22:42:37 +0200548 tuners->powersave_bias = default_powersave_bias;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000549 tuners->io_is_busy = should_io_be_busy();
550
551 dbs_data->tuners = tuners;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000552 mutex_init(&dbs_data->mutex);
553 return 0;
554}
555
556static void od_exit(struct dbs_data *dbs_data)
557{
558 kfree(dbs_data->tuners);
559}
560
Viresh Kumar4471a342012-10-26 00:47:42 +0200561define_get_cpu_dbs_routines(od_cpu_dbs_info);
Mike Chan00e299f2010-01-26 17:06:47 -0800562
Viresh Kumar4471a342012-10-26 00:47:42 +0200563static struct od_ops od_ops = {
Viresh Kumar4471a342012-10-26 00:47:42 +0200564 .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu,
Jacob Shinfb308092013-04-02 09:56:56 -0500565 .powersave_bias_target = generic_powersave_bias_target,
Viresh Kumar4471a342012-10-26 00:47:42 +0200566 .freq_increase = dbs_freq_increase,
567};
568
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000569static struct common_dbs_data od_dbs_cdata = {
Viresh Kumar4471a342012-10-26 00:47:42 +0200570 .governor = GOV_ONDEMAND,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000571 .attr_group_gov_sys = &od_attr_group_gov_sys,
572 .attr_group_gov_pol = &od_attr_group_gov_pol,
Viresh Kumar4471a342012-10-26 00:47:42 +0200573 .get_cpu_cdbs = get_cpu_cdbs,
574 .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
575 .gov_dbs_timer = od_dbs_timer,
576 .gov_check_cpu = od_check_cpu,
577 .gov_ops = &od_ops,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000578 .init = od_init,
579 .exit = od_exit,
Viresh Kumar4471a342012-10-26 00:47:42 +0200580};
581
Jacob Shinfb308092013-04-02 09:56:56 -0500582static void od_set_powersave_bias(unsigned int powersave_bias)
583{
584 struct cpufreq_policy *policy;
585 struct dbs_data *dbs_data;
586 struct od_dbs_tuners *od_tuners;
587 unsigned int cpu;
588 cpumask_t done;
589
Jacob Shinc2837552013-06-25 22:42:37 +0200590 default_powersave_bias = powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500591 cpumask_clear(&done);
592
593 get_online_cpus();
594 for_each_online_cpu(cpu) {
595 if (cpumask_test_cpu(cpu, &done))
596 continue;
597
598 policy = per_cpu(od_cpu_dbs_info, cpu).cdbs.cur_policy;
Jacob Shinc2837552013-06-25 22:42:37 +0200599 if (!policy)
600 continue;
Jacob Shinfb308092013-04-02 09:56:56 -0500601
602 cpumask_or(&done, &done, policy->cpus);
Jacob Shinc2837552013-06-25 22:42:37 +0200603
604 if (policy->governor != &cpufreq_gov_ondemand)
605 continue;
606
607 dbs_data = policy->governor_data;
608 od_tuners = dbs_data->tuners;
609 od_tuners->powersave_bias = default_powersave_bias;
Jacob Shinfb308092013-04-02 09:56:56 -0500610 }
611 put_online_cpus();
612}
613
614void od_register_powersave_bias_handler(unsigned int (*f)
615 (struct cpufreq_policy *, unsigned int, unsigned int),
616 unsigned int powersave_bias)
617{
618 od_ops.powersave_bias_target = f;
619 od_set_powersave_bias(powersave_bias);
620}
621EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
622
623void od_unregister_powersave_bias_handler(void)
624{
625 od_ops.powersave_bias_target = generic_powersave_bias_target;
626 od_set_powersave_bias(0);
627}
628EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
629
Viresh Kumar4471a342012-10-26 00:47:42 +0200630static int od_cpufreq_governor_dbs(struct cpufreq_policy *policy,
631 unsigned int event)
632{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000633 return cpufreq_governor_dbs(policy, &od_dbs_cdata, event);
Mike Chan00e299f2010-01-26 17:06:47 -0800634}
635
Viresh Kumar4471a342012-10-26 00:47:42 +0200636#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
637static
Arjan van de Ven19379b12010-05-09 08:26:51 -0700638#endif
Viresh Kumar4471a342012-10-26 00:47:42 +0200639struct cpufreq_governor cpufreq_gov_ondemand = {
640 .name = "ondemand",
641 .governor = od_cpufreq_governor_dbs,
642 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
643 .owner = THIS_MODULE,
644};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static int __init cpufreq_gov_dbs_init(void)
647{
Tejun Heo57df5572011-01-26 12:12:50 +0100648 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651static void __exit cpufreq_gov_dbs_exit(void)
652{
Thomas Renninger1c256242007-10-02 13:28:12 -0700653 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700656MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
657MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
658MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -0500659 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700660MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Johannes Weiner69157192008-01-17 15:21:08 -0800662#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
663fs_initcall(cpufreq_gov_dbs_init);
664#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800666#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667module_exit(cpufreq_gov_dbs_exit);