blob: d3fa8b31015ca78668b744b3c568c2b7e8739698 [file] [log] [blame]
viresh kumar2aacdff2012-10-23 01:28:05 +02001/*
2 * drivers/cpufreq/cpufreq_governor.c
3 *
4 * CPUFREQ governors common code
5 *
Viresh Kumar4471a342012-10-26 00:47:42 +02006 * Copyright (C) 2001 Russell King
7 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
8 * (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
9 * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
10 * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
11 *
viresh kumar2aacdff2012-10-23 01:28:05 +020012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Viresh Kumar4471a342012-10-26 00:47:42 +020017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
viresh kumar2aacdff2012-10-23 01:28:05 +020019#include <linux/export.h>
20#include <linux/kernel_stat.h>
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000021#include <linux/slab.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020022
23#include "cpufreq_governor.h"
24
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +010025DEFINE_MUTEX(dbs_data_mutex);
26EXPORT_SYMBOL_GPL(dbs_data_mutex);
27
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010028static struct attribute_group *get_sysfs_attr(struct dbs_governor *gov)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000029{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010030 return have_governor_per_policy() ?
31 gov->attr_group_gov_pol : gov->attr_group_gov_sys;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000032}
33
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010034void dbs_check_cpu(struct cpufreq_policy *policy, int cpu)
Viresh Kumar4471a342012-10-26 00:47:42 +020035{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010036 struct dbs_governor *gov = dbs_governor_of(policy);
37 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
38 struct dbs_data *dbs_data = policy->governor_data;
Viresh Kumar4471a342012-10-26 00:47:42 +020039 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
40 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053041 unsigned int sampling_rate;
Viresh Kumar4471a342012-10-26 00:47:42 +020042 unsigned int max_load = 0;
43 unsigned int ignore_nice;
44 unsigned int j;
45
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010046 if (gov->governor == GOV_ONDEMAND) {
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053047 struct od_cpu_dbs_info_s *od_dbs_info =
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010048 gov->get_cpu_dbs_info_s(cpu);
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053049
50 /*
51 * Sometimes, the ondemand governor uses an additional
52 * multiplier to give long delays. So apply this multiplier to
53 * the 'sampling_rate', so as to keep the wake-up-from-idle
54 * detection logic a bit conservative.
55 */
56 sampling_rate = od_tuners->sampling_rate;
57 sampling_rate *= od_dbs_info->rate_mult;
58
Viresh Kumar6c4640c2013-08-05 12:28:02 +053059 ignore_nice = od_tuners->ignore_nice_load;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053060 } else {
61 sampling_rate = cs_tuners->sampling_rate;
Viresh Kumar6c4640c2013-08-05 12:28:02 +053062 ignore_nice = cs_tuners->ignore_nice_load;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053063 }
Viresh Kumar4471a342012-10-26 00:47:42 +020064
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +030065 /* Get Absolute Load */
Viresh Kumar4471a342012-10-26 00:47:42 +020066 for_each_cpu(j, policy->cpus) {
Viresh Kumar875b8502015-06-19 17:18:03 +053067 struct cpu_dbs_info *j_cdbs;
Stratos Karafotis9366d842013-02-28 16:57:32 +000068 u64 cur_wall_time, cur_idle_time;
69 unsigned int idle_time, wall_time;
Viresh Kumar4471a342012-10-26 00:47:42 +020070 unsigned int load;
Stratos Karafotis9366d842013-02-28 16:57:32 +000071 int io_busy = 0;
Viresh Kumar4471a342012-10-26 00:47:42 +020072
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010073 j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar4471a342012-10-26 00:47:42 +020074
Stratos Karafotis9366d842013-02-28 16:57:32 +000075 /*
76 * For the purpose of ondemand, waiting for disk IO is
77 * an indication that you're performance critical, and
78 * not that the system is actually idle. So do not add
79 * the iowait time to the cpu idle time.
80 */
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010081 if (gov->governor == GOV_ONDEMAND)
Stratos Karafotis9366d842013-02-28 16:57:32 +000082 io_busy = od_tuners->io_is_busy;
83 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
Viresh Kumar4471a342012-10-26 00:47:42 +020084
85 wall_time = (unsigned int)
86 (cur_wall_time - j_cdbs->prev_cpu_wall);
87 j_cdbs->prev_cpu_wall = cur_wall_time;
88
Chen Yu0df35022015-12-16 12:20:29 +080089 if (cur_idle_time < j_cdbs->prev_cpu_idle)
90 cur_idle_time = j_cdbs->prev_cpu_idle;
91
Viresh Kumar4471a342012-10-26 00:47:42 +020092 idle_time = (unsigned int)
93 (cur_idle_time - j_cdbs->prev_cpu_idle);
94 j_cdbs->prev_cpu_idle = cur_idle_time;
95
96 if (ignore_nice) {
97 u64 cur_nice;
98 unsigned long cur_nice_jiffies;
99
100 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
101 cdbs->prev_cpu_nice;
102 /*
103 * Assumption: nice time between sampling periods will
104 * be less than 2^32 jiffies for 32 bit sys
105 */
106 cur_nice_jiffies = (unsigned long)
107 cputime64_to_jiffies64(cur_nice);
108
109 cdbs->prev_cpu_nice =
110 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
111 idle_time += jiffies_to_usecs(cur_nice_jiffies);
112 }
113
Viresh Kumar4471a342012-10-26 00:47:42 +0200114 if (unlikely(!wall_time || wall_time < idle_time))
115 continue;
116
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530117 /*
118 * If the CPU had gone completely idle, and a task just woke up
119 * on this CPU now, it would be unfair to calculate 'load' the
120 * usual way for this elapsed time-window, because it will show
121 * near-zero load, irrespective of how CPU intensive that task
122 * actually is. This is undesirable for latency-sensitive bursty
123 * workloads.
124 *
125 * To avoid this, we reuse the 'load' from the previous
126 * time-window and give this task a chance to start with a
127 * reasonably high CPU frequency. (However, we shouldn't over-do
128 * this copy, lest we get stuck at a high load (high frequency)
129 * for too long, even when the current system load has actually
130 * dropped down. So we perform the copy only once, upon the
131 * first wake-up from idle.)
132 *
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100133 * Detecting this situation is easy: the governor's utilization
134 * update handler would not have run during CPU-idle periods.
135 * Hence, an unusually large 'wall_time' (as compared to the
136 * sampling rate) indicates this scenario.
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530137 *
138 * prev_load can be zero in two cases and we must recalculate it
139 * for both cases:
140 * - during long idle intervals
141 * - explicitly set to zero
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530142 */
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530143 if (unlikely(wall_time > (2 * sampling_rate) &&
144 j_cdbs->prev_load)) {
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530145 load = j_cdbs->prev_load;
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530146
147 /*
148 * Perform a destructive copy, to ensure that we copy
149 * the previous load only once, upon the first wake-up
150 * from idle.
151 */
152 j_cdbs->prev_load = 0;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530153 } else {
154 load = 100 * (wall_time - idle_time) / wall_time;
155 j_cdbs->prev_load = load;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530156 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200157
Viresh Kumar4471a342012-10-26 00:47:42 +0200158 if (load > max_load)
159 max_load = load;
160 }
161
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100162 gov->gov_check_cpu(cpu, max_load);
Viresh Kumar4471a342012-10-26 00:47:42 +0200163}
164EXPORT_SYMBOL_GPL(dbs_check_cpu);
165
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100166void gov_set_update_util(struct cpu_common_dbs_info *shared,
167 unsigned int delay_us)
Viresh Kumar4471a342012-10-26 00:47:42 +0200168{
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100169 struct cpufreq_policy *policy = shared->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100170 struct dbs_governor *gov = dbs_governor_of(policy);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530171 int cpu;
Viresh Kumar4471a342012-10-26 00:47:42 +0200172
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100173 gov_update_sample_delay(shared, delay_us);
174 shared->last_sample_time = 0;
175
Viresh Kumar70f43e52015-12-09 07:34:42 +0530176 for_each_cpu(cpu, policy->cpus) {
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100177 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100178
179 cpufreq_set_update_util_data(cpu, &cdbs->update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530180 }
181}
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100182EXPORT_SYMBOL_GPL(gov_set_update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530183
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100184static inline void gov_clear_update_util(struct cpufreq_policy *policy)
Viresh Kumar031299b2013-02-27 12:24:03 +0530185{
Viresh Kumar031299b2013-02-27 12:24:03 +0530186 int i;
187
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100188 for_each_cpu(i, policy->cpus)
189 cpufreq_set_update_util_data(i, NULL);
190
191 synchronize_rcu();
Viresh Kumar4471a342012-10-26 00:47:42 +0200192}
193
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100194static void gov_cancel_work(struct cpu_common_dbs_info *shared)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530195{
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100196 /* Tell dbs_update_util_handler() to skip queuing up work items. */
Rafael J. Wysocki2dd3e722015-12-08 21:44:05 +0100197 atomic_inc(&shared->skip_work);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530198 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100199 * If dbs_update_util_handler() is already running, it may not notice
200 * the incremented skip_work, so wait for it to complete to prevent its
201 * work item from being queued up after the cancel_work_sync() below.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530202 */
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100203 gov_clear_update_util(shared->policy);
204 irq_work_sync(&shared->irq_work);
Rafael J. Wysocki2dd3e722015-12-08 21:44:05 +0100205 cancel_work_sync(&shared->work);
Rafael J. Wysocki2dd3e722015-12-08 21:44:05 +0100206 atomic_set(&shared->skip_work, 0);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530207}
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530208
Viresh Kumar70f43e52015-12-09 07:34:42 +0530209static void dbs_work_handler(struct work_struct *work)
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530210{
Viresh Kumar70f43e52015-12-09 07:34:42 +0530211 struct cpu_common_dbs_info *shared = container_of(work, struct
212 cpu_common_dbs_info, work);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530213 struct cpufreq_policy *policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100214 struct dbs_governor *gov;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100215 unsigned int delay;
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530216
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530217 policy = shared->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100218 gov = dbs_governor_of(policy);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530219
Viresh Kumar70f43e52015-12-09 07:34:42 +0530220 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100221 * Make sure cpufreq_governor_limits() isn't evaluating load or the
222 * ondemand governor isn't updating the sampling rate in parallel.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530223 */
224 mutex_lock(&shared->timer_mutex);
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100225 delay = gov->gov_dbs_timer(policy);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100226 shared->sample_delay_ns = jiffies_to_nsecs(delay);
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530227 mutex_unlock(&shared->timer_mutex);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530228
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100229 /*
230 * If the atomic operation below is reordered with respect to the
231 * sample delay modification, the utilization update handler may end
232 * up using a stale sample delay value.
233 */
234 smp_mb__before_atomic();
Rafael J. Wysocki2dd3e722015-12-08 21:44:05 +0100235 atomic_dec(&shared->skip_work);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530236}
237
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100238static void dbs_irq_work(struct irq_work *irq_work)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530239{
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100240 struct cpu_common_dbs_info *shared;
241
242 shared = container_of(irq_work, struct cpu_common_dbs_info, irq_work);
243 schedule_work(&shared->work);
244}
245
246static inline void gov_queue_irq_work(struct cpu_common_dbs_info *shared)
247{
248#ifdef CONFIG_SMP
249 irq_work_queue_on(&shared->irq_work, smp_processor_id());
250#else
251 irq_work_queue(&shared->irq_work);
252#endif
253}
254
255static void dbs_update_util_handler(struct update_util_data *data, u64 time,
256 unsigned long util, unsigned long max)
257{
258 struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530259 struct cpu_common_dbs_info *shared = cdbs->shared;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530260
261 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100262 * The work may not be allowed to be queued up right now.
263 * Possible reasons:
264 * - Work has already been queued up or is in progress.
265 * - The governor is being stopped.
266 * - It is too early (too little time from the previous sample).
Viresh Kumar70f43e52015-12-09 07:34:42 +0530267 */
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100268 if (atomic_inc_return(&shared->skip_work) == 1) {
269 u64 delta_ns;
270
271 delta_ns = time - shared->last_sample_time;
272 if ((s64)delta_ns >= shared->sample_delay_ns) {
273 shared->last_sample_time = time;
274 gov_queue_irq_work(shared);
275 return;
276 }
277 }
278 atomic_dec(&shared->skip_work);
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530279}
Viresh Kumar44472662013-01-31 17:28:02 +0000280
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000281static void set_sampling_rate(struct dbs_data *dbs_data,
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100282 struct dbs_governor *gov,
283 unsigned int sampling_rate)
Viresh Kumar4471a342012-10-26 00:47:42 +0200284{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100285 if (gov->governor == GOV_CONSERVATIVE) {
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000286 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
287 cs_tuners->sampling_rate = sampling_rate;
288 } else {
289 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
290 od_tuners->sampling_rate = sampling_rate;
291 }
292}
293
Viresh Kumar44152cb2015-07-18 11:30:59 +0530294static int alloc_common_dbs_info(struct cpufreq_policy *policy,
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100295 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530296{
297 struct cpu_common_dbs_info *shared;
298 int j;
299
300 /* Allocate memory for the common information for policy->cpus */
301 shared = kzalloc(sizeof(*shared), GFP_KERNEL);
302 if (!shared)
303 return -ENOMEM;
304
305 /* Set shared for all CPUs, online+offline */
306 for_each_cpu(j, policy->related_cpus)
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100307 gov->get_cpu_cdbs(j)->shared = shared;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530308
Viresh Kumar5e4500d2015-12-03 09:37:52 +0530309 mutex_init(&shared->timer_mutex);
Rafael J. Wysocki2dd3e722015-12-08 21:44:05 +0100310 atomic_set(&shared->skip_work, 0);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100311 init_irq_work(&shared->irq_work, dbs_irq_work);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530312 INIT_WORK(&shared->work, dbs_work_handler);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530313 return 0;
314}
315
316static void free_common_dbs_info(struct cpufreq_policy *policy,
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100317 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530318{
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100319 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530320 struct cpu_common_dbs_info *shared = cdbs->shared;
321 int j;
322
Viresh Kumar5e4500d2015-12-03 09:37:52 +0530323 mutex_destroy(&shared->timer_mutex);
324
Viresh Kumar44152cb2015-07-18 11:30:59 +0530325 for_each_cpu(j, policy->cpus)
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100326 gov->get_cpu_cdbs(j)->shared = NULL;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530327
328 kfree(shared);
329}
330
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100331static int cpufreq_governor_init(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530332{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100333 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100334 struct dbs_data *dbs_data = gov->gdbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530335 unsigned int latency;
336 int ret;
337
Viresh Kumara72c4952015-07-18 11:31:01 +0530338 /* State should be equivalent to EXIT */
339 if (policy->governor_data)
340 return -EBUSY;
341
Viresh Kumar714a2d92015-06-04 16:43:27 +0530342 if (dbs_data) {
343 if (WARN_ON(have_governor_per_policy()))
344 return -EINVAL;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530345
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100346 ret = alloc_common_dbs_info(policy, gov);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530347 if (ret)
348 return ret;
349
Viresh Kumar714a2d92015-06-04 16:43:27 +0530350 dbs_data->usage_count++;
351 policy->governor_data = dbs_data;
352 return 0;
353 }
354
355 dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
356 if (!dbs_data)
357 return -ENOMEM;
358
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100359 ret = alloc_common_dbs_info(policy, gov);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530360 if (ret)
361 goto free_dbs_data;
362
Viresh Kumar714a2d92015-06-04 16:43:27 +0530363 dbs_data->usage_count = 1;
364
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100365 ret = gov->init(dbs_data, !policy->governor->initialized);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530366 if (ret)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530367 goto free_common_dbs_info;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530368
369 /* policy latency is in ns. Convert it to us first */
370 latency = policy->cpuinfo.transition_latency / 1000;
371 if (latency == 0)
372 latency = 1;
373
374 /* Bring kernel and HW constraints together */
375 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
376 MIN_LATENCY_MULTIPLIER * latency);
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100377 set_sampling_rate(dbs_data, gov, max(dbs_data->min_sampling_rate,
Viresh Kumar714a2d92015-06-04 16:43:27 +0530378 latency * LATENCY_MULTIPLIER));
379
Viresh Kumar8eec1022015-10-15 21:35:22 +0530380 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100381 gov->gdbs_data = dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530382
Viresh Kumare4b133c2016-01-25 22:33:46 +0530383 policy->governor_data = dbs_data;
384
Viresh Kumar714a2d92015-06-04 16:43:27 +0530385 ret = sysfs_create_group(get_governor_parent_kobj(policy),
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100386 get_sysfs_attr(gov));
Viresh Kumar714a2d92015-06-04 16:43:27 +0530387 if (ret)
Viresh Kumar8eec1022015-10-15 21:35:22 +0530388 goto reset_gdbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530389
Viresh Kumar714a2d92015-06-04 16:43:27 +0530390 return 0;
391
Viresh Kumar8eec1022015-10-15 21:35:22 +0530392reset_gdbs_data:
Viresh Kumare4b133c2016-01-25 22:33:46 +0530393 policy->governor_data = NULL;
394
Viresh Kumar8eec1022015-10-15 21:35:22 +0530395 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100396 gov->gdbs_data = NULL;
397 gov->exit(dbs_data, !policy->governor->initialized);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530398free_common_dbs_info:
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100399 free_common_dbs_info(policy, gov);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530400free_dbs_data:
401 kfree(dbs_data);
402 return ret;
403}
404
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100405static int cpufreq_governor_exit(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530406{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100407 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100408 struct dbs_data *dbs_data = policy->governor_data;
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100409 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu);
Viresh Kumara72c4952015-07-18 11:31:01 +0530410
411 /* State should be equivalent to INIT */
412 if (!cdbs->shared || cdbs->shared->policy)
413 return -EBUSY;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530414
Viresh Kumar714a2d92015-06-04 16:43:27 +0530415 if (!--dbs_data->usage_count) {
416 sysfs_remove_group(get_governor_parent_kobj(policy),
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100417 get_sysfs_attr(gov));
Viresh Kumar714a2d92015-06-04 16:43:27 +0530418
Viresh Kumare4b133c2016-01-25 22:33:46 +0530419 policy->governor_data = NULL;
420
Viresh Kumar8eec1022015-10-15 21:35:22 +0530421 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100422 gov->gdbs_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530423
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100424 gov->exit(dbs_data, policy->governor->initialized == 1);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530425 kfree(dbs_data);
Viresh Kumare4b133c2016-01-25 22:33:46 +0530426 } else {
427 policy->governor_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530428 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530429
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100430 free_common_dbs_info(policy, gov);
Viresh Kumara72c4952015-07-18 11:31:01 +0530431 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530432}
433
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100434static int cpufreq_governor_start(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530435{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100436 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100437 struct dbs_data *dbs_data = policy->governor_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530438 unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu;
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100439 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530440 struct cpu_common_dbs_info *shared = cdbs->shared;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530441 int io_busy = 0;
442
443 if (!policy->cur)
444 return -EINVAL;
445
Viresh Kumara72c4952015-07-18 11:31:01 +0530446 /* State should be equivalent to INIT */
447 if (!shared || shared->policy)
448 return -EBUSY;
449
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100450 if (gov->governor == GOV_CONSERVATIVE) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530451 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
452
453 sampling_rate = cs_tuners->sampling_rate;
454 ignore_nice = cs_tuners->ignore_nice_load;
455 } else {
456 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
457
458 sampling_rate = od_tuners->sampling_rate;
459 ignore_nice = od_tuners->ignore_nice_load;
460 io_busy = od_tuners->io_is_busy;
461 }
462
Viresh Kumar714a2d92015-06-04 16:43:27 +0530463 for_each_cpu(j, policy->cpus) {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100464 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530465 unsigned int prev_load;
466
Viresh Kumar714a2d92015-06-04 16:43:27 +0530467 j_cdbs->prev_cpu_idle =
468 get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy);
469
470 prev_load = (unsigned int)(j_cdbs->prev_cpu_wall -
471 j_cdbs->prev_cpu_idle);
472 j_cdbs->prev_load = 100 * prev_load /
473 (unsigned int)j_cdbs->prev_cpu_wall;
474
475 if (ignore_nice)
476 j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
477
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100478 j_cdbs->update_util.func = dbs_update_util_handler;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530479 }
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100480 shared->policy = policy;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530481
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100482 if (gov->governor == GOV_CONSERVATIVE) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530483 struct cs_cpu_dbs_info_s *cs_dbs_info =
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100484 gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530485
486 cs_dbs_info->down_skip = 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530487 cs_dbs_info->requested_freq = policy->cur;
488 } else {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100489 struct od_ops *od_ops = gov->gov_ops;
490 struct od_cpu_dbs_info_s *od_dbs_info = gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530491
492 od_dbs_info->rate_mult = 1;
493 od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
494 od_ops->powersave_bias_init_cpu(cpu);
495 }
496
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100497 gov_set_update_util(shared, sampling_rate);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530498 return 0;
499}
500
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100501static int cpufreq_governor_stop(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530502{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100503 struct dbs_governor *gov = dbs_governor_of(policy);
504 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530505 struct cpu_common_dbs_info *shared = cdbs->shared;
506
Viresh Kumara72c4952015-07-18 11:31:01 +0530507 /* State should be equivalent to START */
508 if (!shared || !shared->policy)
509 return -EBUSY;
510
Viresh Kumar70f43e52015-12-09 07:34:42 +0530511 gov_cancel_work(shared);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530512 shared->policy = NULL;
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530513
Viresh Kumara72c4952015-07-18 11:31:01 +0530514 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530515}
516
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100517static int cpufreq_governor_limits(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530518{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100519 struct dbs_governor *gov = dbs_governor_of(policy);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530520 unsigned int cpu = policy->cpu;
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100521 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530522
Viresh Kumara72c4952015-07-18 11:31:01 +0530523 /* State should be equivalent to START */
Viresh Kumar44152cb2015-07-18 11:30:59 +0530524 if (!cdbs->shared || !cdbs->shared->policy)
Viresh Kumara72c4952015-07-18 11:31:01 +0530525 return -EBUSY;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530526
Viresh Kumar44152cb2015-07-18 11:30:59 +0530527 mutex_lock(&cdbs->shared->timer_mutex);
528 if (policy->max < cdbs->shared->policy->cur)
529 __cpufreq_driver_target(cdbs->shared->policy, policy->max,
Viresh Kumar714a2d92015-06-04 16:43:27 +0530530 CPUFREQ_RELATION_H);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530531 else if (policy->min > cdbs->shared->policy->cur)
532 __cpufreq_driver_target(cdbs->shared->policy, policy->min,
Viresh Kumar714a2d92015-06-04 16:43:27 +0530533 CPUFREQ_RELATION_L);
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100534 dbs_check_cpu(policy, cpu);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530535 mutex_unlock(&cdbs->shared->timer_mutex);
Viresh Kumara72c4952015-07-18 11:31:01 +0530536
537 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530538}
539
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100540int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000541{
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100542 int ret = -EINVAL;
Viresh Kumar4471a342012-10-26 00:47:42 +0200543
Viresh Kumar732b6d62015-06-03 15:57:13 +0530544 /* Lock governor to block concurrent initialization of governor */
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100545 mutex_lock(&dbs_data_mutex);
Viresh Kumar732b6d62015-06-03 15:57:13 +0530546
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100547 if (event == CPUFREQ_GOV_POLICY_INIT) {
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100548 ret = cpufreq_governor_init(policy);
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100549 } else if (policy->governor_data) {
550 switch (event) {
551 case CPUFREQ_GOV_POLICY_EXIT:
552 ret = cpufreq_governor_exit(policy);
553 break;
554 case CPUFREQ_GOV_START:
555 ret = cpufreq_governor_start(policy);
556 break;
557 case CPUFREQ_GOV_STOP:
558 ret = cpufreq_governor_stop(policy);
559 break;
560 case CPUFREQ_GOV_LIMITS:
561 ret = cpufreq_governor_limits(policy);
562 break;
563 }
Viresh Kumar732b6d62015-06-03 15:57:13 +0530564 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000565
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100566 mutex_unlock(&dbs_data_mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530567 return ret;
Viresh Kumar4471a342012-10-26 00:47:42 +0200568}
569EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);