blob: 4b14f04daa41f852fed2bb7c676bf16162f82ff0 [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
Viresh Kumaraded3872016-02-11 17:31:15 +053028/* Common sysfs tunables */
29/**
30 * store_sampling_rate - update sampling rate effective immediately if needed.
31 *
32 * If new rate is smaller than the old, simply updating
33 * dbs.sampling_rate might not be appropriate. For example, if the
34 * original sampling_rate was 1 second and the requested new sampling rate is 10
35 * ms because the user needs immediate reaction from ondemand governor, but not
36 * sure if higher frequency will be required or not, then, the governor may
37 * change the sampling rate too late; up to 1 second later. Thus, if we are
38 * reducing the sampling rate, we need to make the new value effective
39 * immediately.
40 *
Viresh Kumaraded3872016-02-11 17:31:15 +053041 * This must be called with dbs_data->mutex held, otherwise traversing
42 * policy_dbs_list isn't safe.
43 */
44ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
45 size_t count)
46{
47 struct policy_dbs_info *policy_dbs;
48 unsigned int rate;
49 int ret;
50 ret = sscanf(buf, "%u", &rate);
51 if (ret != 1)
52 return -EINVAL;
53
54 dbs_data->sampling_rate = max(rate, dbs_data->min_sampling_rate);
55
56 /*
57 * We are operating under dbs_data->mutex and so the list and its
58 * entries can't be freed concurrently.
59 */
60 list_for_each_entry(policy_dbs, &dbs_data->policy_dbs_list, list) {
61 mutex_lock(&policy_dbs->timer_mutex);
62 /*
63 * On 32-bit architectures this may race with the
64 * sample_delay_ns read in dbs_update_util_handler(), but that
65 * really doesn't matter. If the read returns a value that's
66 * too big, the sample will be skipped, but the next invocation
67 * of dbs_update_util_handler() (when the update has been
Rafael J. Wysocki78347cd2016-02-15 02:20:11 +010068 * completed) will take a sample.
Viresh Kumaraded3872016-02-11 17:31:15 +053069 *
70 * If this runs in parallel with dbs_work_handler(), we may end
71 * up overwriting the sample_delay_ns value that it has just
Rafael J. Wysocki78347cd2016-02-15 02:20:11 +010072 * written, but it will be corrected next time a sample is
73 * taken, so it shouldn't be significant.
Viresh Kumaraded3872016-02-11 17:31:15 +053074 */
Rafael J. Wysocki78347cd2016-02-15 02:20:11 +010075 gov_update_sample_delay(policy_dbs, 0);
Viresh Kumaraded3872016-02-11 17:31:15 +053076 mutex_unlock(&policy_dbs->timer_mutex);
77 }
78
79 return count;
80}
81EXPORT_SYMBOL_GPL(store_sampling_rate);
82
Rafael J. Wysockia33cce12016-02-18 02:26:55 +010083/**
84 * gov_update_cpu_data - Update CPU load data.
85 * @gov: Governor whose data is to be updated.
86 * @dbs_data: Top-level governor data pointer.
87 *
88 * Update CPU load data for all CPUs in the domain governed by @dbs_data
89 * (that may be a single policy or a bunch of them if governor tunables are
90 * system-wide).
91 *
92 * Call under the @dbs_data mutex.
93 */
94void gov_update_cpu_data(struct dbs_governor *gov, struct dbs_data *dbs_data)
95{
96 struct policy_dbs_info *policy_dbs;
97
98 list_for_each_entry(policy_dbs, &dbs_data->policy_dbs_list, list) {
99 unsigned int j;
100
101 for_each_cpu(j, policy_dbs->policy->cpus) {
102 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
103
104 j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall,
105 dbs_data->io_is_busy);
106 if (dbs_data->ignore_nice_load)
107 j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
108 }
109 }
110}
111EXPORT_SYMBOL_GPL(gov_update_cpu_data);
112
Viresh Kumarc4435632016-02-09 09:01:33 +0530113static inline struct dbs_data *to_dbs_data(struct kobject *kobj)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000114{
Viresh Kumarc4435632016-02-09 09:01:33 +0530115 return container_of(kobj, struct dbs_data, kobj);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000116}
117
Viresh Kumarc4435632016-02-09 09:01:33 +0530118static inline struct governor_attr *to_gov_attr(struct attribute *attr)
119{
120 return container_of(attr, struct governor_attr, attr);
121}
122
123static ssize_t governor_show(struct kobject *kobj, struct attribute *attr,
124 char *buf)
125{
126 struct dbs_data *dbs_data = to_dbs_data(kobj);
127 struct governor_attr *gattr = to_gov_attr(attr);
128 int ret = -EIO;
129
130 if (gattr->show)
131 ret = gattr->show(dbs_data, buf);
132
133 return ret;
134}
135
136static ssize_t governor_store(struct kobject *kobj, struct attribute *attr,
137 const char *buf, size_t count)
138{
139 struct dbs_data *dbs_data = to_dbs_data(kobj);
140 struct governor_attr *gattr = to_gov_attr(attr);
141 int ret = -EIO;
142
143 mutex_lock(&dbs_data->mutex);
144
Rafael J. Wysocki574ef142016-02-18 02:19:00 +0100145 if (dbs_data->usage_count && gattr->store)
Viresh Kumarc4435632016-02-09 09:01:33 +0530146 ret = gattr->store(dbs_data, buf, count);
147
148 mutex_unlock(&dbs_data->mutex);
149
150 return ret;
151}
152
153/*
154 * Sysfs Ops for accessing governor attributes.
155 *
156 * All show/store invocations for governor specific sysfs attributes, will first
157 * call the below show/store callbacks and the attribute specific callback will
158 * be called from within it.
159 */
160static const struct sysfs_ops governor_sysfs_ops = {
161 .show = governor_show,
162 .store = governor_store,
163};
164
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100165unsigned int dbs_update(struct cpufreq_policy *policy)
Viresh Kumar4471a342012-10-26 00:47:42 +0200166{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100167 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100168 struct policy_dbs_info *policy_dbs = policy->governor_data;
169 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumarff4b1782016-02-09 09:01:32 +0530170 unsigned int ignore_nice = dbs_data->ignore_nice_load;
Viresh Kumar4471a342012-10-26 00:47:42 +0200171 unsigned int max_load = 0;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100172 unsigned int sampling_rate, io_busy, j;
Viresh Kumar4471a342012-10-26 00:47:42 +0200173
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100174 /*
175 * Sometimes governors may use an additional multiplier to increase
176 * sample delays temporarily. Apply that multiplier to sampling_rate
177 * so as to keep the wake-up-from-idle detection logic a bit
178 * conservative.
179 */
180 sampling_rate = dbs_data->sampling_rate * policy_dbs->rate_mult;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100181 /*
182 * For the purpose of ondemand, waiting for disk IO is an indication
183 * that you're performance critical, and not that the system is actually
184 * idle, so do not add the iowait time to the CPU idle time then.
185 */
186 io_busy = dbs_data->io_is_busy;
Viresh Kumar4471a342012-10-26 00:47:42 +0200187
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300188 /* Get Absolute Load */
Viresh Kumar4471a342012-10-26 00:47:42 +0200189 for_each_cpu(j, policy->cpus) {
Viresh Kumar875b8502015-06-19 17:18:03 +0530190 struct cpu_dbs_info *j_cdbs;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000191 u64 cur_wall_time, cur_idle_time;
192 unsigned int idle_time, wall_time;
Viresh Kumar4471a342012-10-26 00:47:42 +0200193 unsigned int load;
194
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100195 j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200196
Stratos Karafotis9366d842013-02-28 16:57:32 +0000197 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
Viresh Kumar4471a342012-10-26 00:47:42 +0200198
Rafael J. Wysocki57eb8322016-02-16 00:58:47 +0100199 wall_time = cur_wall_time - j_cdbs->prev_cpu_wall;
Viresh Kumar4471a342012-10-26 00:47:42 +0200200 j_cdbs->prev_cpu_wall = cur_wall_time;
201
Rafael J. Wysocki57eb8322016-02-16 00:58:47 +0100202 if (cur_idle_time <= j_cdbs->prev_cpu_idle) {
203 idle_time = 0;
204 } else {
205 idle_time = cur_idle_time - j_cdbs->prev_cpu_idle;
206 j_cdbs->prev_cpu_idle = cur_idle_time;
207 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200208
209 if (ignore_nice) {
Rafael J. Wysocki679b8fe2016-02-15 02:15:50 +0100210 u64 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Viresh Kumar4471a342012-10-26 00:47:42 +0200211
Rafael J. Wysocki679b8fe2016-02-15 02:15:50 +0100212 idle_time += cputime_to_usecs(cur_nice - j_cdbs->prev_cpu_nice);
213 j_cdbs->prev_cpu_nice = cur_nice;
Viresh Kumar4471a342012-10-26 00:47:42 +0200214 }
215
Viresh Kumar4471a342012-10-26 00:47:42 +0200216 if (unlikely(!wall_time || wall_time < idle_time))
217 continue;
218
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530219 /*
220 * If the CPU had gone completely idle, and a task just woke up
221 * on this CPU now, it would be unfair to calculate 'load' the
222 * usual way for this elapsed time-window, because it will show
223 * near-zero load, irrespective of how CPU intensive that task
224 * actually is. This is undesirable for latency-sensitive bursty
225 * workloads.
226 *
227 * To avoid this, we reuse the 'load' from the previous
228 * time-window and give this task a chance to start with a
229 * reasonably high CPU frequency. (However, we shouldn't over-do
230 * this copy, lest we get stuck at a high load (high frequency)
231 * for too long, even when the current system load has actually
232 * dropped down. So we perform the copy only once, upon the
233 * first wake-up from idle.)
234 *
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100235 * Detecting this situation is easy: the governor's utilization
236 * update handler would not have run during CPU-idle periods.
237 * Hence, an unusually large 'wall_time' (as compared to the
238 * sampling rate) indicates this scenario.
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530239 *
240 * prev_load can be zero in two cases and we must recalculate it
241 * for both cases:
242 * - during long idle intervals
243 * - explicitly set to zero
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530244 */
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530245 if (unlikely(wall_time > (2 * sampling_rate) &&
246 j_cdbs->prev_load)) {
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530247 load = j_cdbs->prev_load;
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530248
249 /*
250 * Perform a destructive copy, to ensure that we copy
251 * the previous load only once, upon the first wake-up
252 * from idle.
253 */
254 j_cdbs->prev_load = 0;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530255 } else {
256 load = 100 * (wall_time - idle_time) / wall_time;
257 j_cdbs->prev_load = load;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530258 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200259
Viresh Kumar4471a342012-10-26 00:47:42 +0200260 if (load > max_load)
261 max_load = load;
262 }
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100263 return max_load;
Viresh Kumar4471a342012-10-26 00:47:42 +0200264}
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100265EXPORT_SYMBOL_GPL(dbs_update);
Viresh Kumar4471a342012-10-26 00:47:42 +0200266
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100267void gov_set_update_util(struct policy_dbs_info *policy_dbs,
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100268 unsigned int delay_us)
Viresh Kumar4471a342012-10-26 00:47:42 +0200269{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100270 struct cpufreq_policy *policy = policy_dbs->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100271 struct dbs_governor *gov = dbs_governor_of(policy);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530272 int cpu;
Viresh Kumar4471a342012-10-26 00:47:42 +0200273
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100274 gov_update_sample_delay(policy_dbs, delay_us);
275 policy_dbs->last_sample_time = 0;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100276
Viresh Kumar70f43e52015-12-09 07:34:42 +0530277 for_each_cpu(cpu, policy->cpus) {
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100278 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100279
280 cpufreq_set_update_util_data(cpu, &cdbs->update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530281 }
282}
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100283EXPORT_SYMBOL_GPL(gov_set_update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530284
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100285static inline void gov_clear_update_util(struct cpufreq_policy *policy)
Viresh Kumar031299b2013-02-27 12:24:03 +0530286{
Viresh Kumar031299b2013-02-27 12:24:03 +0530287 int i;
288
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100289 for_each_cpu(i, policy->cpus)
290 cpufreq_set_update_util_data(i, NULL);
291
292 synchronize_rcu();
Viresh Kumar4471a342012-10-26 00:47:42 +0200293}
294
Viresh Kumar581c2142016-02-11 17:31:14 +0530295static void gov_cancel_work(struct cpufreq_policy *policy)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530296{
Viresh Kumar581c2142016-02-11 17:31:14 +0530297 struct policy_dbs_info *policy_dbs = policy->governor_data;
298
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100299 gov_clear_update_util(policy_dbs->policy);
300 irq_work_sync(&policy_dbs->irq_work);
301 cancel_work_sync(&policy_dbs->work);
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100302 atomic_set(&policy_dbs->work_count, 0);
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100303 policy_dbs->work_in_progress = false;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530304}
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530305
Viresh Kumar70f43e52015-12-09 07:34:42 +0530306static void dbs_work_handler(struct work_struct *work)
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530307{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100308 struct policy_dbs_info *policy_dbs;
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530309 struct cpufreq_policy *policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100310 struct dbs_governor *gov;
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530311
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100312 policy_dbs = container_of(work, struct policy_dbs_info, work);
313 policy = policy_dbs->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100314 gov = dbs_governor_of(policy);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530315
Viresh Kumar70f43e52015-12-09 07:34:42 +0530316 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100317 * Make sure cpufreq_governor_limits() isn't evaluating load or the
318 * ondemand governor isn't updating the sampling rate in parallel.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530319 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100320 mutex_lock(&policy_dbs->timer_mutex);
Rafael J. Wysocki07aa4402016-02-15 02:22:13 +0100321 gov_update_sample_delay(policy_dbs, gov->gov_dbs_timer(policy));
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100322 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530323
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100324 /* Allow the utilization update handler to queue up more work. */
325 atomic_set(&policy_dbs->work_count, 0);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100326 /*
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100327 * If the update below is reordered with respect to the sample delay
328 * modification, the utilization update handler may end up using a stale
329 * sample delay value.
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100330 */
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100331 smp_wmb();
332 policy_dbs->work_in_progress = false;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530333}
334
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100335static void dbs_irq_work(struct irq_work *irq_work)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530336{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100337 struct policy_dbs_info *policy_dbs;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100338
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100339 policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work);
340 schedule_work(&policy_dbs->work);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100341}
342
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100343static void dbs_update_util_handler(struct update_util_data *data, u64 time,
344 unsigned long util, unsigned long max)
345{
346 struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100347 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100348 u64 delta_ns;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530349
350 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100351 * The work may not be allowed to be queued up right now.
352 * Possible reasons:
353 * - Work has already been queued up or is in progress.
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100354 * - It is too early (too little time from the previous sample).
Viresh Kumar70f43e52015-12-09 07:34:42 +0530355 */
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100356 if (policy_dbs->work_in_progress)
357 return;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100358
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100359 /*
360 * If the reads below are reordered before the check above, the value
361 * of sample_delay_ns used in the computation may be stale.
362 */
363 smp_rmb();
364 delta_ns = time - policy_dbs->last_sample_time;
365 if ((s64)delta_ns < policy_dbs->sample_delay_ns)
366 return;
367
368 /*
369 * If the policy is not shared, the irq_work may be queued up right away
370 * at this point. Otherwise, we need to ensure that only one of the
371 * CPUs sharing the policy will do that.
372 */
373 if (policy_dbs->is_shared &&
374 !atomic_add_unless(&policy_dbs->work_count, 1, 1))
375 return;
376
377 policy_dbs->last_sample_time = time;
378 policy_dbs->work_in_progress = true;
379 irq_work_queue(&policy_dbs->irq_work);
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530380}
Viresh Kumar44472662013-01-31 17:28:02 +0000381
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100382static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy,
383 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530384{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100385 struct policy_dbs_info *policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530386 int j;
387
388 /* Allocate memory for the common information for policy->cpus */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100389 policy_dbs = kzalloc(sizeof(*policy_dbs), GFP_KERNEL);
390 if (!policy_dbs)
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100391 return NULL;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530392
Viresh Kumar581c2142016-02-11 17:31:14 +0530393 policy_dbs->policy = policy;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100394 mutex_init(&policy_dbs->timer_mutex);
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100395 atomic_set(&policy_dbs->work_count, 0);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100396 init_irq_work(&policy_dbs->irq_work, dbs_irq_work);
397 INIT_WORK(&policy_dbs->work, dbs_work_handler);
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100398
399 /* Set policy_dbs for all CPUs, online+offline */
400 for_each_cpu(j, policy->related_cpus) {
401 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
402
403 j_cdbs->policy_dbs = policy_dbs;
404 j_cdbs->update_util.func = dbs_update_util_handler;
405 }
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100406 return policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530407}
408
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100409static void free_policy_dbs_info(struct cpufreq_policy *policy,
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100410 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530411{
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100412 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100413 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530414 int j;
415
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100416 mutex_destroy(&policy_dbs->timer_mutex);
Viresh Kumar5e4500d2015-12-03 09:37:52 +0530417
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100418 for_each_cpu(j, policy->related_cpus) {
419 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530420
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100421 j_cdbs->policy_dbs = NULL;
422 j_cdbs->update_util.func = NULL;
423 }
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100424 kfree(policy_dbs);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530425}
426
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100427static int cpufreq_governor_init(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530428{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100429 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100430 struct dbs_data *dbs_data = gov->gdbs_data;
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100431 struct policy_dbs_info *policy_dbs;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530432 unsigned int latency;
433 int ret;
434
Viresh Kumara72c4952015-07-18 11:31:01 +0530435 /* State should be equivalent to EXIT */
436 if (policy->governor_data)
437 return -EBUSY;
438
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100439 policy_dbs = alloc_policy_dbs_info(policy, gov);
440 if (!policy_dbs)
441 return -ENOMEM;
442
Viresh Kumar714a2d92015-06-04 16:43:27 +0530443 if (dbs_data) {
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100444 if (WARN_ON(have_governor_per_policy())) {
445 ret = -EINVAL;
446 goto free_policy_dbs_info;
447 }
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100448 policy_dbs->dbs_data = dbs_data;
449 policy->governor_data = policy_dbs;
Viresh Kumarc54df072016-02-10 11:00:25 +0530450
451 mutex_lock(&dbs_data->mutex);
452 dbs_data->usage_count++;
453 list_add(&policy_dbs->list, &dbs_data->policy_dbs_list);
454 mutex_unlock(&dbs_data->mutex);
455
Viresh Kumar714a2d92015-06-04 16:43:27 +0530456 return 0;
457 }
458
459 dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100460 if (!dbs_data) {
461 ret = -ENOMEM;
462 goto free_policy_dbs_info;
463 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530464
Viresh Kumarc54df072016-02-10 11:00:25 +0530465 INIT_LIST_HEAD(&dbs_data->policy_dbs_list);
Viresh Kumarc4435632016-02-09 09:01:33 +0530466 mutex_init(&dbs_data->mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530467
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100468 ret = gov->init(dbs_data, !policy->governor->initialized);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530469 if (ret)
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100470 goto free_policy_dbs_info;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530471
472 /* policy latency is in ns. Convert it to us first */
473 latency = policy->cpuinfo.transition_latency / 1000;
474 if (latency == 0)
475 latency = 1;
476
477 /* Bring kernel and HW constraints together */
478 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
479 MIN_LATENCY_MULTIPLIER * latency);
Viresh Kumarff4b1782016-02-09 09:01:32 +0530480 dbs_data->sampling_rate = max(dbs_data->min_sampling_rate,
481 LATENCY_MULTIPLIER * latency);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530482
Viresh Kumar8eec1022015-10-15 21:35:22 +0530483 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100484 gov->gdbs_data = dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530485
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100486 policy->governor_data = policy_dbs;
Viresh Kumare4b133c2016-01-25 22:33:46 +0530487
Viresh Kumarc54df072016-02-10 11:00:25 +0530488 policy_dbs->dbs_data = dbs_data;
489 dbs_data->usage_count = 1;
490 list_add(&policy_dbs->list, &dbs_data->policy_dbs_list);
491
Viresh Kumarc4435632016-02-09 09:01:33 +0530492 gov->kobj_type.sysfs_ops = &governor_sysfs_ops;
493 ret = kobject_init_and_add(&dbs_data->kobj, &gov->kobj_type,
494 get_governor_parent_kobj(policy),
495 "%s", gov->gov.name);
Rafael J. Wysockifafd5e82016-02-08 23:57:22 +0100496 if (!ret)
497 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530498
Rafael J. Wysockifafd5e82016-02-08 23:57:22 +0100499 /* Failure, so roll back. */
Viresh Kumarc4435632016-02-09 09:01:33 +0530500 pr_err("cpufreq: Governor initialization failed (dbs_data kobject init error %d)\n", ret);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530501
Viresh Kumare4b133c2016-01-25 22:33:46 +0530502 policy->governor_data = NULL;
503
Viresh Kumar8eec1022015-10-15 21:35:22 +0530504 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100505 gov->gdbs_data = NULL;
506 gov->exit(dbs_data, !policy->governor->initialized);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100507 kfree(dbs_data);
508
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100509free_policy_dbs_info:
510 free_policy_dbs_info(policy, gov);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530511 return ret;
512}
513
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100514static int cpufreq_governor_exit(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530515{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100516 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100517 struct policy_dbs_info *policy_dbs = policy->governor_data;
518 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumarc54df072016-02-10 11:00:25 +0530519 int count;
Viresh Kumara72c4952015-07-18 11:31:01 +0530520
Viresh Kumarc54df072016-02-10 11:00:25 +0530521 mutex_lock(&dbs_data->mutex);
522 list_del(&policy_dbs->list);
523 count = --dbs_data->usage_count;
524 mutex_unlock(&dbs_data->mutex);
525
526 if (!count) {
Viresh Kumarc4435632016-02-09 09:01:33 +0530527 kobject_put(&dbs_data->kobj);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530528
Viresh Kumare4b133c2016-01-25 22:33:46 +0530529 policy->governor_data = NULL;
530
Viresh Kumar8eec1022015-10-15 21:35:22 +0530531 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100532 gov->gdbs_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530533
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100534 gov->exit(dbs_data, policy->governor->initialized == 1);
Viresh Kumarc4435632016-02-09 09:01:33 +0530535 mutex_destroy(&dbs_data->mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530536 kfree(dbs_data);
Viresh Kumare4b133c2016-01-25 22:33:46 +0530537 } else {
538 policy->governor_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530539 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530540
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100541 free_policy_dbs_info(policy, gov);
Viresh Kumara72c4952015-07-18 11:31:01 +0530542 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530543}
544
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100545static int cpufreq_governor_start(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530546{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100547 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100548 struct policy_dbs_info *policy_dbs = policy->governor_data;
549 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100550 unsigned int sampling_rate, ignore_nice, j;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100551 unsigned int io_busy;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530552
553 if (!policy->cur)
554 return -EINVAL;
555
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100556 policy_dbs->is_shared = policy_is_shared(policy);
Rafael J. Wysocki57dc3bc2016-02-15 02:20:51 +0100557 policy_dbs->rate_mult = 1;
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100558
Viresh Kumarff4b1782016-02-09 09:01:32 +0530559 sampling_rate = dbs_data->sampling_rate;
560 ignore_nice = dbs_data->ignore_nice_load;
Rafael J. Wysocki8847e032016-02-18 02:20:13 +0100561 io_busy = dbs_data->io_is_busy;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530562
Viresh Kumar714a2d92015-06-04 16:43:27 +0530563 for_each_cpu(j, policy->cpus) {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100564 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530565 unsigned int prev_load;
566
Rafael J. Wysocki57eb8322016-02-16 00:58:47 +0100567 j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530568
Rafael J. Wysocki57eb8322016-02-16 00:58:47 +0100569 prev_load = j_cdbs->prev_cpu_wall - j_cdbs->prev_cpu_idle;
570 j_cdbs->prev_load = 100 * prev_load / (unsigned int)j_cdbs->prev_cpu_wall;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530571
572 if (ignore_nice)
573 j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Viresh Kumar714a2d92015-06-04 16:43:27 +0530574 }
575
Rafael J. Wysocki702c9e52016-02-18 02:21:21 +0100576 gov->start(policy);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530577
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100578 gov_set_update_util(policy_dbs, sampling_rate);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530579 return 0;
580}
581
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100582static int cpufreq_governor_stop(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530583{
Viresh Kumar581c2142016-02-11 17:31:14 +0530584 gov_cancel_work(policy);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530585
Viresh Kumara72c4952015-07-18 11:31:01 +0530586 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530587}
588
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100589static int cpufreq_governor_limits(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530590{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100591 struct policy_dbs_info *policy_dbs = policy->governor_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530592
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100593 mutex_lock(&policy_dbs->timer_mutex);
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100594
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100595 if (policy->max < policy->cur)
596 __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
597 else if (policy->min > policy->cur)
598 __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100599
600 gov_update_sample_delay(policy_dbs, 0);
601
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100602 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumara72c4952015-07-18 11:31:01 +0530603
604 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530605}
606
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100607int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000608{
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100609 int ret = -EINVAL;
Viresh Kumar4471a342012-10-26 00:47:42 +0200610
Viresh Kumar732b6d62015-06-03 15:57:13 +0530611 /* Lock governor to block concurrent initialization of governor */
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100612 mutex_lock(&dbs_data_mutex);
Viresh Kumar732b6d62015-06-03 15:57:13 +0530613
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100614 if (event == CPUFREQ_GOV_POLICY_INIT) {
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100615 ret = cpufreq_governor_init(policy);
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100616 } else if (policy->governor_data) {
617 switch (event) {
618 case CPUFREQ_GOV_POLICY_EXIT:
619 ret = cpufreq_governor_exit(policy);
620 break;
621 case CPUFREQ_GOV_START:
622 ret = cpufreq_governor_start(policy);
623 break;
624 case CPUFREQ_GOV_STOP:
625 ret = cpufreq_governor_stop(policy);
626 break;
627 case CPUFREQ_GOV_LIMITS:
628 ret = cpufreq_governor_limits(policy);
629 break;
630 }
Viresh Kumar732b6d62015-06-03 15:57:13 +0530631 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000632
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100633 mutex_unlock(&dbs_data_mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530634 return ret;
Viresh Kumar4471a342012-10-26 00:47:42 +0200635}
636EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);