blob: c18a304b3a38f4daf95dbd354daf129ce9db5ea6 [file] [log] [blame]
Dave Jonesb9170832005-05-31 19:03:47 -07001/*
2 * drivers/cpufreq/cpufreq_conservative.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
6 * Jun Nakajima <jun.nakajima@intel.com>
Alexander Clouter11a80a9c762009-02-13 19:01:01 +00007 * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
Dave Jonesb9170832005-05-31 19:03:47 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Dave Jonesb9170832005-05-31 19:03:47 -070014#include <linux/cpufreq.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020015#include <linux/init.h>
16#include <linux/kernel.h>
Dave Jonesb9170832005-05-31 19:03:47 -070017#include <linux/kernel_stat.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020018#include <linux/kobject.h>
19#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080020#include <linux/mutex.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020021#include <linux/notifier.h>
22#include <linux/percpu-defs.h>
23#include <linux/sysfs.h>
24#include <linux/types.h>
Alexander Clouter8e677ce2009-02-13 19:02:34 +000025
Viresh Kumar4471a342012-10-26 00:47:42 +020026#include "cpufreq_governor.h"
Dave Jonesb9170832005-05-31 19:03:47 -070027
Viresh Kumar4471a342012-10-26 00:47:42 +020028/* Conservative governor macors */
Dave Jonesb9170832005-05-31 19:03:47 -070029#define DEF_FREQUENCY_UP_THRESHOLD (80)
Dave Jonesb9170832005-05-31 19:03:47 -070030#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
Alexander Clouter2c906b32006-03-22 09:54:10 +000031#define DEF_SAMPLING_DOWN_FACTOR (1)
32#define MAX_SAMPLING_DOWN_FACTOR (10)
Dave Jonesb9170832005-05-31 19:03:47 -070033
Viresh Kumar4471a342012-10-26 00:47:42 +020034static struct dbs_data cs_dbs_data;
35static DEFINE_PER_CPU(struct cs_cpu_dbs_info_s, cs_cpu_dbs_info);
Dave Jonesb9170832005-05-31 19:03:47 -070036
Viresh Kumar4471a342012-10-26 00:47:42 +020037static struct cs_dbs_tuners cs_tuners = {
Dave Jones18a72472007-10-22 16:49:09 -040038 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
39 .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
40 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
41 .ignore_nice = 0,
42 .freq_step = 5,
Dave Jonesb9170832005-05-31 19:03:47 -070043};
44
Viresh Kumar4471a342012-10-26 00:47:42 +020045/*
46 * Every sampling_rate, we check, if current idle time is less than 20%
47 * (default), then we try to increase frequency Every sampling_rate *
48 * sampling_down_factor, we check, if current idle time is more than 80%, then
49 * we try to decrease frequency
50 *
51 * Any frequency increase takes it to the maximum frequency. Frequency reduction
52 * happens at minimum steps of 5% (default) of maximum frequency
53 */
54static void cs_check_cpu(int cpu, unsigned int load)
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +020055{
Viresh Kumar4471a342012-10-26 00:47:42 +020056 struct cs_cpu_dbs_info_s *dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
57 struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
58 unsigned int freq_target;
Alexander Clouterf407a082009-02-13 19:01:51 +000059
60 /*
Viresh Kumar4471a342012-10-26 00:47:42 +020061 * break out if we 'cannot' reduce the speed as the user might
62 * want freq_step to be zero
63 */
64 if (cs_tuners.freq_step == 0)
65 return;
66
67 /* Check for frequency increase */
68 if (load > cs_tuners.up_threshold) {
69 dbs_info->down_skip = 0;
70
71 /* if we are already at full speed then break out early */
72 if (dbs_info->requested_freq == policy->max)
73 return;
74
75 freq_target = (cs_tuners.freq_step * policy->max) / 100;
76
77 /* max freq cannot be less than 100. But who knows.... */
78 if (unlikely(freq_target == 0))
79 freq_target = 5;
80
81 dbs_info->requested_freq += freq_target;
82 if (dbs_info->requested_freq > policy->max)
83 dbs_info->requested_freq = policy->max;
84
85 __cpufreq_driver_target(policy, dbs_info->requested_freq,
86 CPUFREQ_RELATION_H);
87 return;
88 }
89
90 /*
91 * The optimal frequency is the frequency that is the lowest that can
92 * support the current CPU usage without triggering the up policy. To be
93 * safe, we focus 10 points under the threshold.
94 */
95 if (load < (cs_tuners.down_threshold - 10)) {
96 freq_target = (cs_tuners.freq_step * policy->max) / 100;
97
98 dbs_info->requested_freq -= freq_target;
99 if (dbs_info->requested_freq < policy->min)
100 dbs_info->requested_freq = policy->min;
101
102 /*
103 * if we cannot reduce the frequency anymore, break out early
104 */
105 if (policy->cur == policy->min)
106 return;
107
108 __cpufreq_driver_target(policy, dbs_info->requested_freq,
109 CPUFREQ_RELATION_H);
110 return;
111 }
112}
113
Fabio Baltieri66df2a012012-12-27 14:55:41 +0000114static void cs_timer_update(struct cs_cpu_dbs_info_s *dbs_info, bool sample,
115 struct delayed_work *dw)
Viresh Kumar4471a342012-10-26 00:47:42 +0200116{
Fabio Baltieri09dca5a2013-01-31 10:39:19 +0000117 unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
Viresh Kumar4471a342012-10-26 00:47:42 +0200118 int delay = delay_for_sampling_rate(cs_tuners.sampling_rate);
119
Fabio Baltieri66df2a012012-12-27 14:55:41 +0000120 if (sample)
121 dbs_check_cpu(&cs_dbs_data, cpu);
122
123 schedule_delayed_work_on(smp_processor_id(), dw, delay);
124}
125
126static void cs_timer_coordinated(struct cs_cpu_dbs_info_s *dbs_info_local,
127 struct delayed_work *dw)
128{
129 struct cs_cpu_dbs_info_s *dbs_info;
130 ktime_t time_now;
131 s64 delta_us;
132 bool sample = true;
133
134 /* use leader CPU's dbs_info */
Fabio Baltieri09dca5a2013-01-31 10:39:19 +0000135 dbs_info = &per_cpu(cs_cpu_dbs_info,
136 dbs_info_local->cdbs.cur_policy->cpu);
Viresh Kumar4471a342012-10-26 00:47:42 +0200137 mutex_lock(&dbs_info->cdbs.timer_mutex);
138
Fabio Baltieri66df2a012012-12-27 14:55:41 +0000139 time_now = ktime_get();
140 delta_us = ktime_us_delta(time_now, dbs_info->cdbs.time_stamp);
Viresh Kumar4471a342012-10-26 00:47:42 +0200141
Fabio Baltieri66df2a012012-12-27 14:55:41 +0000142 /* Do nothing if we recently have sampled */
143 if (delta_us < (s64)(cs_tuners.sampling_rate / 2))
144 sample = false;
145 else
146 dbs_info->cdbs.time_stamp = time_now;
147
148 cs_timer_update(dbs_info, sample, dw);
Viresh Kumar4471a342012-10-26 00:47:42 +0200149 mutex_unlock(&dbs_info->cdbs.timer_mutex);
150}
151
Fabio Baltieri66df2a012012-12-27 14:55:41 +0000152static void cs_dbs_timer(struct work_struct *work)
153{
154 struct delayed_work *dw = to_delayed_work(work);
155 struct cs_cpu_dbs_info_s *dbs_info = container_of(work,
156 struct cs_cpu_dbs_info_s, cdbs.work.work);
157
Fabio Baltieri2624f902013-01-31 09:44:40 +0000158 if (policy_is_shared(dbs_info->cdbs.cur_policy)) {
Fabio Baltieri66df2a012012-12-27 14:55:41 +0000159 cs_timer_coordinated(dbs_info, dw);
160 } else {
161 mutex_lock(&dbs_info->cdbs.timer_mutex);
162 cs_timer_update(dbs_info, true, dw);
163 mutex_unlock(&dbs_info->cdbs.timer_mutex);
164 }
165}
Viresh Kumar4471a342012-10-26 00:47:42 +0200166static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
167 void *data)
168{
169 struct cpufreq_freqs *freq = data;
170 struct cs_cpu_dbs_info_s *dbs_info =
171 &per_cpu(cs_cpu_dbs_info, freq->cpu);
172 struct cpufreq_policy *policy;
173
174 if (!dbs_info->enable)
175 return 0;
176
177 policy = dbs_info->cdbs.cur_policy;
178
179 /*
180 * we only care if our internally tracked freq moves outside the 'valid'
181 * ranges of freqency available to us otherwise we do not change it
Alexander Clouterf407a082009-02-13 19:01:51 +0000182 */
Viresh Kumar4471a342012-10-26 00:47:42 +0200183 if (dbs_info->requested_freq > policy->max
184 || dbs_info->requested_freq < policy->min)
185 dbs_info->requested_freq = freq->new;
Elias Oltmannsa8d7c3b2007-10-22 09:50:13 +0200186
187 return 0;
188}
189
Dave Jonesb9170832005-05-31 19:03:47 -0700190/************************** sysfs interface ************************/
Thomas Renninger49b015c2009-10-01 19:49:28 +0200191static ssize_t show_sampling_rate_min(struct kobject *kobj,
192 struct attribute *attr, char *buf)
Dave Jonesb9170832005-05-31 19:03:47 -0700193{
Viresh Kumar4471a342012-10-26 00:47:42 +0200194 return sprintf(buf, "%u\n", cs_dbs_data.min_sampling_rate);
Dave Jonesb9170832005-05-31 19:03:47 -0700195}
196
Thomas Renninger49b015c2009-10-01 19:49:28 +0200197static ssize_t store_sampling_down_factor(struct kobject *a,
198 struct attribute *b,
199 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700200{
201 unsigned int input;
202 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500203 ret = sscanf(buf, "%u", &input);
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000204
Alexander Clouter2c906b32006-03-22 09:54:10 +0000205 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700206 return -EINVAL;
207
Viresh Kumar4471a342012-10-26 00:47:42 +0200208 cs_tuners.sampling_down_factor = input;
Dave Jonesb9170832005-05-31 19:03:47 -0700209 return count;
210}
211
Thomas Renninger49b015c2009-10-01 19:49:28 +0200212static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
213 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700214{
215 unsigned int input;
216 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500217 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700218
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000219 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700220 return -EINVAL;
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000221
Viresh Kumar4471a342012-10-26 00:47:42 +0200222 cs_tuners.sampling_rate = max(input, cs_dbs_data.min_sampling_rate);
Dave Jonesb9170832005-05-31 19:03:47 -0700223 return count;
224}
225
Thomas Renninger49b015c2009-10-01 19:49:28 +0200226static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
227 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700228{
229 unsigned int input;
230 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500231 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700232
Viresh Kumar4471a342012-10-26 00:47:42 +0200233 if (ret != 1 || input > 100 || input <= cs_tuners.down_threshold)
Dave Jonesb9170832005-05-31 19:03:47 -0700234 return -EINVAL;
Dave Jonesb9170832005-05-31 19:03:47 -0700235
Viresh Kumar4471a342012-10-26 00:47:42 +0200236 cs_tuners.up_threshold = input;
Dave Jonesb9170832005-05-31 19:03:47 -0700237 return count;
238}
239
Thomas Renninger49b015c2009-10-01 19:49:28 +0200240static ssize_t store_down_threshold(struct kobject *a, struct attribute *b,
241 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700242{
243 unsigned int input;
244 int ret;
Dave Jones9acef482009-01-18 01:39:51 -0500245 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700246
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000247 /* cannot be lower than 11 otherwise freq will not fall */
248 if (ret != 1 || input < 11 || input > 100 ||
Viresh Kumar4471a342012-10-26 00:47:42 +0200249 input >= cs_tuners.up_threshold)
Dave Jonesb9170832005-05-31 19:03:47 -0700250 return -EINVAL;
Dave Jonesb9170832005-05-31 19:03:47 -0700251
Viresh Kumar4471a342012-10-26 00:47:42 +0200252 cs_tuners.down_threshold = input;
Dave Jonesb9170832005-05-31 19:03:47 -0700253 return count;
254}
255
Thomas Renninger49b015c2009-10-01 19:49:28 +0200256static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
257 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700258{
Viresh Kumar4471a342012-10-26 00:47:42 +0200259 unsigned int input, j;
Dave Jonesb9170832005-05-31 19:03:47 -0700260 int ret;
261
Dave Jones18a72472007-10-22 16:49:09 -0400262 ret = sscanf(buf, "%u", &input);
263 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700264 return -EINVAL;
265
Dave Jones18a72472007-10-22 16:49:09 -0400266 if (input > 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700267 input = 1;
Dave Jones18a72472007-10-22 16:49:09 -0400268
Viresh Kumar4471a342012-10-26 00:47:42 +0200269 if (input == cs_tuners.ignore_nice) /* nothing to do */
Dave Jonesb9170832005-05-31 19:03:47 -0700270 return count;
Thomas Renninger326c86d2011-03-03 21:31:27 +0100271
Viresh Kumar4471a342012-10-26 00:47:42 +0200272 cs_tuners.ignore_nice = input;
Dave Jonesb9170832005-05-31 19:03:47 -0700273
Alexander Clouter8e677ce2009-02-13 19:02:34 +0000274 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700275 for_each_online_cpu(j) {
Viresh Kumar4471a342012-10-26 00:47:42 +0200276 struct cs_cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900277 dbs_info = &per_cpu(cs_cpu_dbs_info, j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200278 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
279 &dbs_info->cdbs.prev_cpu_wall);
280 if (cs_tuners.ignore_nice)
281 dbs_info->cdbs.prev_cpu_nice =
282 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Dave Jonesb9170832005-05-31 19:03:47 -0700283 }
Dave Jonesb9170832005-05-31 19:03:47 -0700284 return count;
285}
286
Thomas Renninger49b015c2009-10-01 19:49:28 +0200287static ssize_t store_freq_step(struct kobject *a, struct attribute *b,
288 const char *buf, size_t count)
Dave Jonesb9170832005-05-31 19:03:47 -0700289{
290 unsigned int input;
291 int ret;
Dave Jones18a72472007-10-22 16:49:09 -0400292 ret = sscanf(buf, "%u", &input);
Dave Jonesb9170832005-05-31 19:03:47 -0700293
Dave Jones18a72472007-10-22 16:49:09 -0400294 if (ret != 1)
Dave Jonesb9170832005-05-31 19:03:47 -0700295 return -EINVAL;
296
Dave Jones18a72472007-10-22 16:49:09 -0400297 if (input > 100)
Dave Jonesb9170832005-05-31 19:03:47 -0700298 input = 100;
Dave Jones18a72472007-10-22 16:49:09 -0400299
Viresh Kumar4471a342012-10-26 00:47:42 +0200300 /*
301 * no need to test here if freq_step is zero as the user might actually
302 * want this, they would be crazy though :)
303 */
304 cs_tuners.freq_step = input;
Dave Jonesb9170832005-05-31 19:03:47 -0700305 return count;
306}
307
Viresh Kumar4471a342012-10-26 00:47:42 +0200308show_one(cs, sampling_rate, sampling_rate);
309show_one(cs, sampling_down_factor, sampling_down_factor);
310show_one(cs, up_threshold, up_threshold);
311show_one(cs, down_threshold, down_threshold);
312show_one(cs, ignore_nice_load, ignore_nice);
313show_one(cs, freq_step, freq_step);
314
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200315define_one_global_rw(sampling_rate);
316define_one_global_rw(sampling_down_factor);
317define_one_global_rw(up_threshold);
318define_one_global_rw(down_threshold);
319define_one_global_rw(ignore_nice_load);
320define_one_global_rw(freq_step);
Viresh Kumar4471a342012-10-26 00:47:42 +0200321define_one_global_ro(sampling_rate_min);
Dave Jonesb9170832005-05-31 19:03:47 -0700322
Dave Jones9acef482009-01-18 01:39:51 -0500323static struct attribute *dbs_attributes[] = {
Dave Jonesb9170832005-05-31 19:03:47 -0700324 &sampling_rate_min.attr,
325 &sampling_rate.attr,
326 &sampling_down_factor.attr,
327 &up_threshold.attr,
328 &down_threshold.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800329 &ignore_nice_load.attr,
Dave Jonesb9170832005-05-31 19:03:47 -0700330 &freq_step.attr,
331 NULL
332};
333
Viresh Kumar4471a342012-10-26 00:47:42 +0200334static struct attribute_group cs_attr_group = {
Dave Jonesb9170832005-05-31 19:03:47 -0700335 .attrs = dbs_attributes,
336 .name = "conservative",
337};
338
339/************************** sysfs end ************************/
340
Viresh Kumar4471a342012-10-26 00:47:42 +0200341define_get_cpu_dbs_routines(cs_cpu_dbs_info);
Dave Jonesb9170832005-05-31 19:03:47 -0700342
Viresh Kumar4471a342012-10-26 00:47:42 +0200343static struct notifier_block cs_cpufreq_notifier_block = {
344 .notifier_call = dbs_cpufreq_notifier,
345};
Dave Jonesb9170832005-05-31 19:03:47 -0700346
Viresh Kumar4471a342012-10-26 00:47:42 +0200347static struct cs_ops cs_ops = {
348 .notifier_block = &cs_cpufreq_notifier_block,
349};
Alexander Clouter08a28e22006-03-22 09:59:16 +0000350
Viresh Kumar4471a342012-10-26 00:47:42 +0200351static struct dbs_data cs_dbs_data = {
352 .governor = GOV_CONSERVATIVE,
353 .attr_group = &cs_attr_group,
354 .tuners = &cs_tuners,
355 .get_cpu_cdbs = get_cpu_cdbs,
356 .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
357 .gov_dbs_timer = cs_dbs_timer,
358 .gov_check_cpu = cs_check_cpu,
359 .gov_ops = &cs_ops,
360};
Dave Jonesb9170832005-05-31 19:03:47 -0700361
Viresh Kumar4471a342012-10-26 00:47:42 +0200362static int cs_cpufreq_governor_dbs(struct cpufreq_policy *policy,
Dave Jonesb9170832005-05-31 19:03:47 -0700363 unsigned int event)
364{
Viresh Kumar4471a342012-10-26 00:47:42 +0200365 return cpufreq_governor_dbs(&cs_dbs_data, policy, event);
Dave Jonesb9170832005-05-31 19:03:47 -0700366}
367
Sven Wegenerc4d14bc2008-09-20 16:50:08 +0200368#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
369static
370#endif
Thomas Renninger1c256242007-10-02 13:28:12 -0700371struct cpufreq_governor cpufreq_gov_conservative = {
372 .name = "conservative",
Viresh Kumar4471a342012-10-26 00:47:42 +0200373 .governor = cs_cpufreq_governor_dbs,
Thomas Renninger1c256242007-10-02 13:28:12 -0700374 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
375 .owner = THIS_MODULE,
Dave Jonesb9170832005-05-31 19:03:47 -0700376};
377
378static int __init cpufreq_gov_dbs_init(void)
379{
Viresh Kumar4471a342012-10-26 00:47:42 +0200380 mutex_init(&cs_dbs_data.mutex);
Tejun Heo57df5572011-01-26 12:12:50 +0100381 return cpufreq_register_governor(&cpufreq_gov_conservative);
Dave Jonesb9170832005-05-31 19:03:47 -0700382}
383
384static void __exit cpufreq_gov_dbs_exit(void)
385{
Thomas Renninger1c256242007-10-02 13:28:12 -0700386 cpufreq_unregister_governor(&cpufreq_gov_conservative);
Dave Jonesb9170832005-05-31 19:03:47 -0700387}
388
Alexander Clouter11a80a9c762009-02-13 19:01:01 +0000389MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
Dave Jones9acef482009-01-18 01:39:51 -0500390MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
Dave Jonesb9170832005-05-31 19:03:47 -0700391 "Low Latency Frequency Transition capable processors "
392 "optimised for use in a battery environment");
Dave Jones9acef482009-01-18 01:39:51 -0500393MODULE_LICENSE("GPL");
Dave Jonesb9170832005-05-31 19:03:47 -0700394
Johannes Weiner69157192008-01-17 15:21:08 -0800395#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
396fs_initcall(cpufreq_gov_dbs_init);
397#else
Dave Jonesb9170832005-05-31 19:03:47 -0700398module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -0800399#endif
Dave Jonesb9170832005-05-31 19:03:47 -0700400module_exit(cpufreq_gov_dbs_exit);