blob: c90d345c636aecf0a8257cfbb82514dbe5d05936 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Viresh Kumar4471a342012-10-26 00:47:42 +020028#include "cpufreq_governor.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000030/* On-demand governor macros */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -070031#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define DEF_FREQUENCY_UP_THRESHOLD (80)
David C Niemi3f78a9f2010-10-06 16:54:24 -040033#define DEF_SAMPLING_DOWN_FACTOR (1)
34#define MAX_SAMPLING_DOWN_FACTOR (100000)
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070035#define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
36#define MICRO_FREQUENCY_UP_THRESHOLD (95)
Thomas Renningercef96152009-04-22 13:48:29 +020037#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
Dave Jonesc29f1402005-05-31 19:03:50 -070038#define MIN_FREQUENCY_UP_THRESHOLD (11)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define MAX_FREQUENCY_UP_THRESHOLD (100)
40
Viresh Kumar4471a342012-10-26 00:47:42 +020041static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info);
Thomas Renninger112124a2009-02-04 11:55:12 +010042
Fabio Baltieri3e33ee92012-11-26 18:10:12 +000043#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
44static struct cpufreq_governor cpufreq_gov_ondemand;
45#endif
46
Viresh Kumar4471a342012-10-26 00:47:42 +020047static void ondemand_powersave_bias_init_cpu(int cpu)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070048{
Viresh Kumar4471a342012-10-26 00:47:42 +020049 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070050
Viresh Kumar4471a342012-10-26 00:47:42 +020051 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
52 dbs_info->freq_lo = 0;
53}
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070054
Viresh Kumar4471a342012-10-26 00:47:42 +020055/*
56 * Not all CPUs want IO time to be accounted as busy; this depends on how
57 * efficient idling at a higher frequency/voltage is.
58 * Pavel Machek says this is not so for various generations of AMD and old
59 * Intel systems.
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000060 * Mike Chan (android.com) claims this is also not true for ARM.
Viresh Kumar4471a342012-10-26 00:47:42 +020061 * Because of this, whitelist specific known (series) of CPUs by default, and
62 * leave all others up to the user.
63 */
64static int should_io_be_busy(void)
65{
66#if defined(CONFIG_X86)
67 /*
Stratos Karafotis06eb09d2013-02-08 17:24:18 +000068 * For Intel, Core 2 (model 15) and later have an efficient idle.
Viresh Kumar4471a342012-10-26 00:47:42 +020069 */
70 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
71 boot_cpu_data.x86 == 6 &&
72 boot_cpu_data.x86_model >= 15)
73 return 1;
74#endif
75 return 0;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070076}
77
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040078/*
79 * Find right freq to be set now with powersave_bias on.
80 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
81 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
82 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +020083static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
Viresh Kumar4471a342012-10-26 00:47:42 +020084 unsigned int freq_next, unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040085{
86 unsigned int freq_req, freq_reduc, freq_avg;
87 unsigned int freq_hi, freq_lo;
88 unsigned int index = 0;
89 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
Viresh Kumar4471a342012-10-26 00:47:42 +020090 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
Tejun Heo245b2e72009-06-24 15:13:48 +090091 policy->cpu);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000092 struct dbs_data *dbs_data = policy->governor_data;
93 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040094
95 if (!dbs_info->freq_table) {
96 dbs_info->freq_lo = 0;
97 dbs_info->freq_lo_jiffies = 0;
98 return freq_next;
99 }
100
101 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
102 relation, &index);
103 freq_req = dbs_info->freq_table[index].frequency;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000104 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400105 freq_avg = freq_req - freq_reduc;
106
107 /* Find freq bounds for freq_avg in freq_table */
108 index = 0;
109 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
110 CPUFREQ_RELATION_H, &index);
111 freq_lo = dbs_info->freq_table[index].frequency;
112 index = 0;
113 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
114 CPUFREQ_RELATION_L, &index);
115 freq_hi = dbs_info->freq_table[index].frequency;
116
117 /* Find out how long we have to be in hi and lo freqs */
118 if (freq_hi == freq_lo) {
119 dbs_info->freq_lo = 0;
120 dbs_info->freq_lo_jiffies = 0;
121 return freq_lo;
122 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000123 jiffies_total = usecs_to_jiffies(od_tuners->sampling_rate);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400124 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
125 jiffies_hi += ((freq_hi - freq_lo) / 2);
126 jiffies_hi /= (freq_hi - freq_lo);
127 jiffies_lo = jiffies_total - jiffies_hi;
128 dbs_info->freq_lo = freq_lo;
129 dbs_info->freq_lo_jiffies = jiffies_lo;
130 dbs_info->freq_hi_jiffies = jiffies_hi;
131 return freq_hi;
132}
133
134static void ondemand_powersave_bias_init(void)
135{
136 int i;
137 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700138 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400139 }
140}
141
Viresh Kumar4471a342012-10-26 00:47:42 +0200142static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
143{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000144 struct dbs_data *dbs_data = p->governor_data;
145 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
146
147 if (od_tuners->powersave_bias)
Viresh Kumar4471a342012-10-26 00:47:42 +0200148 freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
149 else if (p->cur == p->max)
150 return;
151
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000152 __cpufreq_driver_target(p, freq, od_tuners->powersave_bias ?
Viresh Kumar4471a342012-10-26 00:47:42 +0200153 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
154}
155
156/*
157 * Every sampling_rate, we check, if current idle time is less than 20%
Stratos Karafotis06eb09d2013-02-08 17:24:18 +0000158 * (default), then we try to increase frequency. Every sampling_rate, we look
159 * for the lowest frequency which can sustain the load while keeping idle time
Viresh Kumar4471a342012-10-26 00:47:42 +0200160 * over 30%. If such a frequency exist, we try to decrease to this frequency.
161 *
162 * Any frequency increase takes it to the maximum frequency. Frequency reduction
163 * happens at minimum steps of 5% (default) of current frequency
164 */
165static void od_check_cpu(int cpu, unsigned int load_freq)
166{
167 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
168 struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000169 struct dbs_data *dbs_data = policy->governor_data;
170 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar4471a342012-10-26 00:47:42 +0200171
172 dbs_info->freq_lo = 0;
173
174 /* Check for frequency increase */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000175 if (load_freq > od_tuners->up_threshold * policy->cur) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200176 /* If switching to max speed, apply sampling_down_factor */
177 if (policy->cur < policy->max)
178 dbs_info->rate_mult =
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000179 od_tuners->sampling_down_factor;
Viresh Kumar4471a342012-10-26 00:47:42 +0200180 dbs_freq_increase(policy, policy->max);
181 return;
182 }
183
184 /* Check for frequency decrease */
185 /* if we cannot reduce the frequency anymore, break out early */
186 if (policy->cur == policy->min)
187 return;
188
189 /*
190 * The optimal frequency is the frequency that is the lowest that can
191 * support the current CPU usage without triggering the up policy. To be
192 * safe, we focus 10 points under the threshold.
193 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000194 if (load_freq < od_tuners->adj_up_threshold
195 * policy->cur) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200196 unsigned int freq_next;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000197 freq_next = load_freq / od_tuners->adj_up_threshold;
Viresh Kumar4471a342012-10-26 00:47:42 +0200198
199 /* No longer fully busy, reset rate_mult */
200 dbs_info->rate_mult = 1;
201
202 if (freq_next < policy->min)
203 freq_next = policy->min;
204
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000205 if (!od_tuners->powersave_bias) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200206 __cpufreq_driver_target(policy, freq_next,
207 CPUFREQ_RELATION_L);
208 } else {
209 int freq = powersave_bias_target(policy, freq_next,
210 CPUFREQ_RELATION_L);
211 __cpufreq_driver_target(policy, freq,
212 CPUFREQ_RELATION_L);
213 }
214 }
215}
216
Fabio Baltierida53d612012-12-27 14:55:40 +0000217static void od_dbs_timer(struct work_struct *work)
218{
219 struct delayed_work *dw = to_delayed_work(work);
220 struct od_cpu_dbs_info_s *dbs_info =
221 container_of(work, struct od_cpu_dbs_info_s, cdbs.work.work);
Viresh Kumar44472662013-01-31 17:28:02 +0000222 unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
223 struct od_cpu_dbs_info_s *core_dbs_info = &per_cpu(od_cpu_dbs_info,
224 cpu);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000225 struct dbs_data *dbs_data = dbs_info->cdbs.cur_policy->governor_data;
226 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumar9d445922013-02-27 11:06:36 +0530227 int delay = 0, sample_type = core_dbs_info->sample_type;
Fabio Baltierida53d612012-12-27 14:55:40 +0000228
Viresh Kumar44472662013-01-31 17:28:02 +0000229 mutex_lock(&core_dbs_info->cdbs.timer_mutex);
Viresh Kumar9d445922013-02-27 11:06:36 +0530230 if (!need_load_eval(&core_dbs_info->cdbs, od_tuners->sampling_rate))
231 goto max_delay;
Viresh Kumar44472662013-01-31 17:28:02 +0000232
233 /* Common NORMAL_SAMPLE setup */
234 core_dbs_info->sample_type = OD_NORMAL_SAMPLE;
235 if (sample_type == OD_SUB_SAMPLE) {
236 delay = core_dbs_info->freq_lo_jiffies;
Viresh Kumar9d445922013-02-27 11:06:36 +0530237 __cpufreq_driver_target(core_dbs_info->cdbs.cur_policy,
238 core_dbs_info->freq_lo, CPUFREQ_RELATION_H);
Fabio Baltierida53d612012-12-27 14:55:40 +0000239 } else {
Viresh Kumar9d445922013-02-27 11:06:36 +0530240 dbs_check_cpu(dbs_data, cpu);
Viresh Kumar44472662013-01-31 17:28:02 +0000241 if (core_dbs_info->freq_lo) {
242 /* Setup timer for SUB_SAMPLE */
243 core_dbs_info->sample_type = OD_SUB_SAMPLE;
244 delay = core_dbs_info->freq_hi_jiffies;
Viresh Kumar44472662013-01-31 17:28:02 +0000245 }
Fabio Baltierida53d612012-12-27 14:55:40 +0000246 }
Viresh Kumar44472662013-01-31 17:28:02 +0000247
Viresh Kumar9d445922013-02-27 11:06:36 +0530248max_delay:
249 if (!delay)
250 delay = delay_for_sampling_rate(od_tuners->sampling_rate
251 * core_dbs_info->rate_mult);
252
Viresh Kumar44472662013-01-31 17:28:02 +0000253 schedule_delayed_work_on(smp_processor_id(), dw, delay);
254 mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
Fabio Baltierida53d612012-12-27 14:55:40 +0000255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/************************** sysfs interface ************************/
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000258static struct common_dbs_data od_dbs_cdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900260/**
261 * update_sampling_rate - update sampling rate effective immediately if needed.
262 * @new_rate: new sampling rate
263 *
Stratos Karafotis06eb09d2013-02-08 17:24:18 +0000264 * If new rate is smaller than the old, simply updating
Viresh Kumar4471a342012-10-26 00:47:42 +0200265 * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
266 * original sampling_rate was 1 second and the requested new sampling rate is 10
267 * ms because the user needs immediate reaction from ondemand governor, but not
268 * sure if higher frequency will be required or not, then, the governor may
269 * change the sampling rate too late; up to 1 second later. Thus, if we are
270 * reducing the sampling rate, we need to make the new value effective
271 * immediately.
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900272 */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000273static void update_sampling_rate(struct dbs_data *dbs_data,
274 unsigned int new_rate)
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900275{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000276 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900277 int cpu;
278
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000279 od_tuners->sampling_rate = new_rate = max(new_rate,
280 dbs_data->min_sampling_rate);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900281
282 for_each_online_cpu(cpu) {
283 struct cpufreq_policy *policy;
Viresh Kumar4471a342012-10-26 00:47:42 +0200284 struct od_cpu_dbs_info_s *dbs_info;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900285 unsigned long next_sampling, appointed_at;
286
287 policy = cpufreq_cpu_get(cpu);
288 if (!policy)
289 continue;
Fabio Baltieri3e33ee92012-11-26 18:10:12 +0000290 if (policy->governor != &cpufreq_gov_ondemand) {
291 cpufreq_cpu_put(policy);
292 continue;
293 }
Fabio Baltieri8ee2ec52012-12-27 14:55:42 +0000294 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900295 cpufreq_cpu_put(policy);
296
Viresh Kumar4471a342012-10-26 00:47:42 +0200297 mutex_lock(&dbs_info->cdbs.timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900298
Viresh Kumar4471a342012-10-26 00:47:42 +0200299 if (!delayed_work_pending(&dbs_info->cdbs.work)) {
300 mutex_unlock(&dbs_info->cdbs.timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900301 continue;
302 }
303
Viresh Kumar4471a342012-10-26 00:47:42 +0200304 next_sampling = jiffies + usecs_to_jiffies(new_rate);
305 appointed_at = dbs_info->cdbs.work.timer.expires;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900306
307 if (time_before(next_sampling, appointed_at)) {
308
Viresh Kumar4471a342012-10-26 00:47:42 +0200309 mutex_unlock(&dbs_info->cdbs.timer_mutex);
310 cancel_delayed_work_sync(&dbs_info->cdbs.work);
311 mutex_lock(&dbs_info->cdbs.timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900312
Fabio Baltieri8ee2ec52012-12-27 14:55:42 +0000313 schedule_delayed_work_on(cpu, &dbs_info->cdbs.work,
Viresh Kumar4471a342012-10-26 00:47:42 +0200314 usecs_to_jiffies(new_rate));
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900315
316 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200317 mutex_unlock(&dbs_info->cdbs.timer_mutex);
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900318 }
319}
320
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000321static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
322 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 unsigned int input;
325 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700326 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700327 if (ret != 1)
328 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000329
330 update_sampling_rate(dbs_data, input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return count;
332}
333
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000334static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf,
335 size_t count)
Arjan van de Ven19379b12010-05-09 08:26:51 -0700336{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000337 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700338 unsigned int input;
339 int ret;
340
341 ret = sscanf(buf, "%u", &input);
342 if (ret != 1)
343 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000344 od_tuners->io_is_busy = !!input;
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 /* Calculate the new adj_up_threshold */
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000361 od_tuners->adj_up_threshold += input;
362 od_tuners->adj_up_threshold -= od_tuners->up_threshold;
Stratos Karafotis4bd4e422013-02-06 13:34:00 +0100363
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000364 od_tuners->up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return count;
366}
367
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000368static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
369 const char *buf, size_t count)
David C Niemi3f78a9f2010-10-06 16:54:24 -0400370{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000371 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400372 unsigned int input, j;
373 int ret;
374 ret = sscanf(buf, "%u", &input);
375
376 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
377 return -EINVAL;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000378 od_tuners->sampling_down_factor = input;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400379
380 /* Reset down sampling multiplier in case it was active */
381 for_each_online_cpu(j) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200382 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
383 j);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400384 dbs_info->rate_mult = 1;
385 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400386 return count;
387}
388
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000389static ssize_t store_ignore_nice(struct dbs_data *dbs_data, const char *buf,
390 size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700391{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000392 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700393 unsigned int input;
394 int ret;
395
396 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500397
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700398 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500399 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700400 return -EINVAL;
401
Dave Jones2b03f892009-01-18 01:43:44 -0500402 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700403 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500404
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000405 if (input == od_tuners->ignore_nice) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700406 return count;
407 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000408 od_tuners->ignore_nice = input;
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700409
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700410 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700411 for_each_online_cpu(j) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200412 struct od_cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900413 dbs_info = &per_cpu(od_cpu_dbs_info, j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200414 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
415 &dbs_info->cdbs.prev_cpu_wall);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000416 if (od_tuners->ignore_nice)
Viresh Kumar4471a342012-10-26 00:47:42 +0200417 dbs_info->cdbs.prev_cpu_nice =
418 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500419
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700420 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700421 return count;
422}
423
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000424static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf,
425 size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400426{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000427 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400428 unsigned int input;
429 int ret;
430 ret = sscanf(buf, "%u", &input);
431
432 if (ret != 1)
433 return -EINVAL;
434
435 if (input > 1000)
436 input = 1000;
437
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000438 od_tuners->powersave_bias = input;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400439 ondemand_powersave_bias_init();
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400440 return count;
441}
442
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000443show_store_one(od, sampling_rate);
444show_store_one(od, io_is_busy);
445show_store_one(od, up_threshold);
446show_store_one(od, sampling_down_factor);
447show_store_one(od, ignore_nice);
448show_store_one(od, powersave_bias);
449declare_show_sampling_rate_min(od);
Viresh Kumar4471a342012-10-26 00:47:42 +0200450
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000451gov_sys_pol_attr_rw(sampling_rate);
452gov_sys_pol_attr_rw(io_is_busy);
453gov_sys_pol_attr_rw(up_threshold);
454gov_sys_pol_attr_rw(sampling_down_factor);
455gov_sys_pol_attr_rw(ignore_nice);
456gov_sys_pol_attr_rw(powersave_bias);
457gov_sys_pol_attr_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000459static struct attribute *dbs_attributes_gov_sys[] = {
460 &sampling_rate_min_gov_sys.attr,
461 &sampling_rate_gov_sys.attr,
462 &up_threshold_gov_sys.attr,
463 &sampling_down_factor_gov_sys.attr,
464 &ignore_nice_gov_sys.attr,
465 &powersave_bias_gov_sys.attr,
466 &io_is_busy_gov_sys.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 NULL
468};
469
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000470static struct attribute_group od_attr_group_gov_sys = {
471 .attrs = dbs_attributes_gov_sys,
472 .name = "ondemand",
473};
474
475static struct attribute *dbs_attributes_gov_pol[] = {
476 &sampling_rate_min_gov_pol.attr,
477 &sampling_rate_gov_pol.attr,
478 &up_threshold_gov_pol.attr,
479 &sampling_down_factor_gov_pol.attr,
480 &ignore_nice_gov_pol.attr,
481 &powersave_bias_gov_pol.attr,
482 &io_is_busy_gov_pol.attr,
483 NULL
484};
485
486static struct attribute_group od_attr_group_gov_pol = {
487 .attrs = dbs_attributes_gov_pol,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 .name = "ondemand",
489};
490
491/************************** sysfs end ************************/
492
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000493static int od_init(struct dbs_data *dbs_data)
494{
495 struct od_dbs_tuners *tuners;
496 u64 idle_time;
497 int cpu;
498
499 tuners = kzalloc(sizeof(struct od_dbs_tuners), GFP_KERNEL);
500 if (!tuners) {
501 pr_err("%s: kzalloc failed\n", __func__);
502 return -ENOMEM;
503 }
504
505 cpu = get_cpu();
506 idle_time = get_cpu_idle_time_us(cpu, NULL);
507 put_cpu();
508 if (idle_time != -1ULL) {
509 /* Idle micro accounting is supported. Use finer thresholds */
510 tuners->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
511 tuners->adj_up_threshold = MICRO_FREQUENCY_UP_THRESHOLD -
512 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
513 /*
514 * In nohz/micro accounting case we set the minimum frequency
515 * not depending on HZ, but fixed (very low). The deferred
516 * timer might skip some samples if idle/sleeping as needed.
517 */
518 dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
519 } else {
520 tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
521 tuners->adj_up_threshold = DEF_FREQUENCY_UP_THRESHOLD -
522 DEF_FREQUENCY_DOWN_DIFFERENTIAL;
523
524 /* For correct statistics, we need 10 ticks for each measure */
525 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
526 jiffies_to_usecs(10);
527 }
528
529 tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
530 tuners->ignore_nice = 0;
531 tuners->powersave_bias = 0;
532 tuners->io_is_busy = should_io_be_busy();
533
534 dbs_data->tuners = tuners;
535 pr_info("%s: tuners %p\n", __func__, tuners);
536 mutex_init(&dbs_data->mutex);
537 return 0;
538}
539
540static void od_exit(struct dbs_data *dbs_data)
541{
542 kfree(dbs_data->tuners);
543}
544
Viresh Kumar4471a342012-10-26 00:47:42 +0200545define_get_cpu_dbs_routines(od_cpu_dbs_info);
Mike Chan00e299f2010-01-26 17:06:47 -0800546
Viresh Kumar4471a342012-10-26 00:47:42 +0200547static struct od_ops od_ops = {
Viresh Kumar4471a342012-10-26 00:47:42 +0200548 .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu,
549 .powersave_bias_target = powersave_bias_target,
550 .freq_increase = dbs_freq_increase,
551};
552
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000553static struct common_dbs_data od_dbs_cdata = {
Viresh Kumar4471a342012-10-26 00:47:42 +0200554 .governor = GOV_ONDEMAND,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000555 .attr_group_gov_sys = &od_attr_group_gov_sys,
556 .attr_group_gov_pol = &od_attr_group_gov_pol,
Viresh Kumar4471a342012-10-26 00:47:42 +0200557 .get_cpu_cdbs = get_cpu_cdbs,
558 .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
559 .gov_dbs_timer = od_dbs_timer,
560 .gov_check_cpu = od_check_cpu,
561 .gov_ops = &od_ops,
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000562 .init = od_init,
563 .exit = od_exit,
Viresh Kumar4471a342012-10-26 00:47:42 +0200564};
565
566static int od_cpufreq_governor_dbs(struct cpufreq_policy *policy,
567 unsigned int event)
568{
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000569 return cpufreq_governor_dbs(policy, &od_dbs_cdata, event);
Mike Chan00e299f2010-01-26 17:06:47 -0800570}
571
Viresh Kumar4471a342012-10-26 00:47:42 +0200572#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
573static
Arjan van de Ven19379b12010-05-09 08:26:51 -0700574#endif
Viresh Kumar4471a342012-10-26 00:47:42 +0200575struct cpufreq_governor cpufreq_gov_ondemand = {
576 .name = "ondemand",
577 .governor = od_cpufreq_governor_dbs,
578 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
579 .owner = THIS_MODULE,
580};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static int __init cpufreq_gov_dbs_init(void)
583{
Tejun Heo57df5572011-01-26 12:12:50 +0100584 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587static void __exit cpufreq_gov_dbs_exit(void)
588{
Thomas Renninger1c256242007-10-02 13:28:12 -0700589 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700592MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
593MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
594MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -0500595 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700596MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Johannes Weiner69157192008-01-17 15:21:08 -0800598#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
599fs_initcall(cpufreq_gov_dbs_init);
600#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800602#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603module_exit(cpufreq_gov_dbs_exit);