blob: 99d25af6485b5e31a78040d8399c0c252bf29a07 [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 *
41 * On the other hand, if new rate is larger than the old, then we may evaluate
42 * the load too soon, and it might we worth updating sample_delay_ns then as
43 * well.
44 *
45 * This must be called with dbs_data->mutex held, otherwise traversing
46 * policy_dbs_list isn't safe.
47 */
48ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
49 size_t count)
50{
51 struct policy_dbs_info *policy_dbs;
52 unsigned int rate;
53 int ret;
54 ret = sscanf(buf, "%u", &rate);
55 if (ret != 1)
56 return -EINVAL;
57
58 dbs_data->sampling_rate = max(rate, dbs_data->min_sampling_rate);
59
60 /*
61 * We are operating under dbs_data->mutex and so the list and its
62 * entries can't be freed concurrently.
63 */
64 list_for_each_entry(policy_dbs, &dbs_data->policy_dbs_list, list) {
65 mutex_lock(&policy_dbs->timer_mutex);
66 /*
67 * On 32-bit architectures this may race with the
68 * sample_delay_ns read in dbs_update_util_handler(), but that
69 * really doesn't matter. If the read returns a value that's
70 * too big, the sample will be skipped, but the next invocation
71 * of dbs_update_util_handler() (when the update has been
72 * completed) will take a sample. If the returned value is too
73 * small, the sample will be taken immediately, but that isn't a
74 * problem, as we want the new rate to take effect immediately
75 * anyway.
76 *
77 * If this runs in parallel with dbs_work_handler(), we may end
78 * up overwriting the sample_delay_ns value that it has just
79 * written, but the difference should not be too big and it will
80 * be corrected next time a sample is taken, so it shouldn't be
81 * significant.
82 */
83 gov_update_sample_delay(policy_dbs, dbs_data->sampling_rate);
84 mutex_unlock(&policy_dbs->timer_mutex);
85 }
86
87 return count;
88}
89EXPORT_SYMBOL_GPL(store_sampling_rate);
90
Viresh Kumarc4435632016-02-09 09:01:33 +053091static inline struct dbs_data *to_dbs_data(struct kobject *kobj)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000092{
Viresh Kumarc4435632016-02-09 09:01:33 +053093 return container_of(kobj, struct dbs_data, kobj);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000094}
95
Viresh Kumarc4435632016-02-09 09:01:33 +053096static inline struct governor_attr *to_gov_attr(struct attribute *attr)
97{
98 return container_of(attr, struct governor_attr, attr);
99}
100
101static ssize_t governor_show(struct kobject *kobj, struct attribute *attr,
102 char *buf)
103{
104 struct dbs_data *dbs_data = to_dbs_data(kobj);
105 struct governor_attr *gattr = to_gov_attr(attr);
106 int ret = -EIO;
107
108 if (gattr->show)
109 ret = gattr->show(dbs_data, buf);
110
111 return ret;
112}
113
114static ssize_t governor_store(struct kobject *kobj, struct attribute *attr,
115 const char *buf, size_t count)
116{
117 struct dbs_data *dbs_data = to_dbs_data(kobj);
118 struct governor_attr *gattr = to_gov_attr(attr);
119 int ret = -EIO;
120
121 mutex_lock(&dbs_data->mutex);
122
123 if (gattr->store)
124 ret = gattr->store(dbs_data, buf, count);
125
126 mutex_unlock(&dbs_data->mutex);
127
128 return ret;
129}
130
131/*
132 * Sysfs Ops for accessing governor attributes.
133 *
134 * All show/store invocations for governor specific sysfs attributes, will first
135 * call the below show/store callbacks and the attribute specific callback will
136 * be called from within it.
137 */
138static const struct sysfs_ops governor_sysfs_ops = {
139 .show = governor_show,
140 .store = governor_store,
141};
142
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100143unsigned int dbs_update(struct cpufreq_policy *policy)
Viresh Kumar4471a342012-10-26 00:47:42 +0200144{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100145 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100146 struct policy_dbs_info *policy_dbs = policy->governor_data;
147 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4471a342012-10-26 00:47:42 +0200148 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumarff4b1782016-02-09 09:01:32 +0530149 unsigned int sampling_rate = dbs_data->sampling_rate;
150 unsigned int ignore_nice = dbs_data->ignore_nice_load;
Viresh Kumar4471a342012-10-26 00:47:42 +0200151 unsigned int max_load = 0;
Viresh Kumar4471a342012-10-26 00:47:42 +0200152 unsigned int j;
153
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100154 if (gov->governor == GOV_ONDEMAND) {
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530155 struct od_cpu_dbs_info_s *od_dbs_info =
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100156 gov->get_cpu_dbs_info_s(policy->cpu);
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530157
158 /*
159 * Sometimes, the ondemand governor uses an additional
160 * multiplier to give long delays. So apply this multiplier to
161 * the 'sampling_rate', so as to keep the wake-up-from-idle
162 * detection logic a bit conservative.
163 */
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530164 sampling_rate *= od_dbs_info->rate_mult;
165
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530166 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200167
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300168 /* Get Absolute Load */
Viresh Kumar4471a342012-10-26 00:47:42 +0200169 for_each_cpu(j, policy->cpus) {
Viresh Kumar875b8502015-06-19 17:18:03 +0530170 struct cpu_dbs_info *j_cdbs;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000171 u64 cur_wall_time, cur_idle_time;
172 unsigned int idle_time, wall_time;
Viresh Kumar4471a342012-10-26 00:47:42 +0200173 unsigned int load;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000174 int io_busy = 0;
Viresh Kumar4471a342012-10-26 00:47:42 +0200175
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100176 j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200177
Stratos Karafotis9366d842013-02-28 16:57:32 +0000178 /*
179 * For the purpose of ondemand, waiting for disk IO is
180 * an indication that you're performance critical, and
181 * not that the system is actually idle. So do not add
182 * the iowait time to the cpu idle time.
183 */
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100184 if (gov->governor == GOV_ONDEMAND)
Stratos Karafotis9366d842013-02-28 16:57:32 +0000185 io_busy = od_tuners->io_is_busy;
186 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
Viresh Kumar4471a342012-10-26 00:47:42 +0200187
Rafael J. Wysocki57eb8322016-02-16 00:58:47 +0100188 wall_time = cur_wall_time - j_cdbs->prev_cpu_wall;
Viresh Kumar4471a342012-10-26 00:47:42 +0200189 j_cdbs->prev_cpu_wall = cur_wall_time;
190
Rafael J. Wysocki57eb8322016-02-16 00:58:47 +0100191 if (cur_idle_time <= j_cdbs->prev_cpu_idle) {
192 idle_time = 0;
193 } else {
194 idle_time = cur_idle_time - j_cdbs->prev_cpu_idle;
195 j_cdbs->prev_cpu_idle = cur_idle_time;
196 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200197
198 if (ignore_nice) {
Rafael J. Wysocki679b8fe2016-02-15 02:15:50 +0100199 u64 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Viresh Kumar4471a342012-10-26 00:47:42 +0200200
Rafael J. Wysocki679b8fe2016-02-15 02:15:50 +0100201 idle_time += cputime_to_usecs(cur_nice - j_cdbs->prev_cpu_nice);
202 j_cdbs->prev_cpu_nice = cur_nice;
Viresh Kumar4471a342012-10-26 00:47:42 +0200203 }
204
Viresh Kumar4471a342012-10-26 00:47:42 +0200205 if (unlikely(!wall_time || wall_time < idle_time))
206 continue;
207
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530208 /*
209 * If the CPU had gone completely idle, and a task just woke up
210 * on this CPU now, it would be unfair to calculate 'load' the
211 * usual way for this elapsed time-window, because it will show
212 * near-zero load, irrespective of how CPU intensive that task
213 * actually is. This is undesirable for latency-sensitive bursty
214 * workloads.
215 *
216 * To avoid this, we reuse the 'load' from the previous
217 * time-window and give this task a chance to start with a
218 * reasonably high CPU frequency. (However, we shouldn't over-do
219 * this copy, lest we get stuck at a high load (high frequency)
220 * for too long, even when the current system load has actually
221 * dropped down. So we perform the copy only once, upon the
222 * first wake-up from idle.)
223 *
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100224 * Detecting this situation is easy: the governor's utilization
225 * update handler would not have run during CPU-idle periods.
226 * Hence, an unusually large 'wall_time' (as compared to the
227 * sampling rate) indicates this scenario.
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530228 *
229 * prev_load can be zero in two cases and we must recalculate it
230 * for both cases:
231 * - during long idle intervals
232 * - explicitly set to zero
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530233 */
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530234 if (unlikely(wall_time > (2 * sampling_rate) &&
235 j_cdbs->prev_load)) {
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530236 load = j_cdbs->prev_load;
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530237
238 /*
239 * Perform a destructive copy, to ensure that we copy
240 * the previous load only once, upon the first wake-up
241 * from idle.
242 */
243 j_cdbs->prev_load = 0;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530244 } else {
245 load = 100 * (wall_time - idle_time) / wall_time;
246 j_cdbs->prev_load = load;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530247 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200248
Viresh Kumar4471a342012-10-26 00:47:42 +0200249 if (load > max_load)
250 max_load = load;
251 }
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100252 return max_load;
Viresh Kumar4471a342012-10-26 00:47:42 +0200253}
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100254EXPORT_SYMBOL_GPL(dbs_update);
Viresh Kumar4471a342012-10-26 00:47:42 +0200255
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100256void gov_set_update_util(struct policy_dbs_info *policy_dbs,
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100257 unsigned int delay_us)
Viresh Kumar4471a342012-10-26 00:47:42 +0200258{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100259 struct cpufreq_policy *policy = policy_dbs->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100260 struct dbs_governor *gov = dbs_governor_of(policy);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530261 int cpu;
Viresh Kumar4471a342012-10-26 00:47:42 +0200262
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100263 gov_update_sample_delay(policy_dbs, delay_us);
264 policy_dbs->last_sample_time = 0;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100265
Viresh Kumar70f43e52015-12-09 07:34:42 +0530266 for_each_cpu(cpu, policy->cpus) {
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100267 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100268
269 cpufreq_set_update_util_data(cpu, &cdbs->update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530270 }
271}
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100272EXPORT_SYMBOL_GPL(gov_set_update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530273
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100274static inline void gov_clear_update_util(struct cpufreq_policy *policy)
Viresh Kumar031299b2013-02-27 12:24:03 +0530275{
Viresh Kumar031299b2013-02-27 12:24:03 +0530276 int i;
277
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100278 for_each_cpu(i, policy->cpus)
279 cpufreq_set_update_util_data(i, NULL);
280
281 synchronize_rcu();
Viresh Kumar4471a342012-10-26 00:47:42 +0200282}
283
Viresh Kumar581c2142016-02-11 17:31:14 +0530284static void gov_cancel_work(struct cpufreq_policy *policy)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530285{
Viresh Kumar581c2142016-02-11 17:31:14 +0530286 struct policy_dbs_info *policy_dbs = policy->governor_data;
287
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100288 gov_clear_update_util(policy_dbs->policy);
289 irq_work_sync(&policy_dbs->irq_work);
290 cancel_work_sync(&policy_dbs->work);
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100291 atomic_set(&policy_dbs->work_count, 0);
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100292 policy_dbs->work_in_progress = false;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530293}
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530294
Viresh Kumar70f43e52015-12-09 07:34:42 +0530295static void dbs_work_handler(struct work_struct *work)
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530296{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100297 struct policy_dbs_info *policy_dbs;
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530298 struct cpufreq_policy *policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100299 struct dbs_governor *gov;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100300 unsigned int delay;
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530301
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100302 policy_dbs = container_of(work, struct policy_dbs_info, work);
303 policy = policy_dbs->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100304 gov = dbs_governor_of(policy);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530305
Viresh Kumar70f43e52015-12-09 07:34:42 +0530306 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100307 * Make sure cpufreq_governor_limits() isn't evaluating load or the
308 * ondemand governor isn't updating the sampling rate in parallel.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530309 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100310 mutex_lock(&policy_dbs->timer_mutex);
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100311 delay = gov->gov_dbs_timer(policy);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100312 policy_dbs->sample_delay_ns = jiffies_to_nsecs(delay);
313 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530314
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100315 /* Allow the utilization update handler to queue up more work. */
316 atomic_set(&policy_dbs->work_count, 0);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100317 /*
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100318 * If the update below is reordered with respect to the sample delay
319 * modification, the utilization update handler may end up using a stale
320 * sample delay value.
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100321 */
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100322 smp_wmb();
323 policy_dbs->work_in_progress = false;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530324}
325
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100326static void dbs_irq_work(struct irq_work *irq_work)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530327{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100328 struct policy_dbs_info *policy_dbs;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100329
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100330 policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work);
331 schedule_work(&policy_dbs->work);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100332}
333
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100334static void dbs_update_util_handler(struct update_util_data *data, u64 time,
335 unsigned long util, unsigned long max)
336{
337 struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100338 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100339 u64 delta_ns;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530340
341 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100342 * The work may not be allowed to be queued up right now.
343 * Possible reasons:
344 * - Work has already been queued up or is in progress.
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100345 * - It is too early (too little time from the previous sample).
Viresh Kumar70f43e52015-12-09 07:34:42 +0530346 */
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100347 if (policy_dbs->work_in_progress)
348 return;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100349
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100350 /*
351 * If the reads below are reordered before the check above, the value
352 * of sample_delay_ns used in the computation may be stale.
353 */
354 smp_rmb();
355 delta_ns = time - policy_dbs->last_sample_time;
356 if ((s64)delta_ns < policy_dbs->sample_delay_ns)
357 return;
358
359 /*
360 * If the policy is not shared, the irq_work may be queued up right away
361 * at this point. Otherwise, we need to ensure that only one of the
362 * CPUs sharing the policy will do that.
363 */
364 if (policy_dbs->is_shared &&
365 !atomic_add_unless(&policy_dbs->work_count, 1, 1))
366 return;
367
368 policy_dbs->last_sample_time = time;
369 policy_dbs->work_in_progress = true;
370 irq_work_queue(&policy_dbs->irq_work);
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530371}
Viresh Kumar44472662013-01-31 17:28:02 +0000372
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100373static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy,
374 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530375{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100376 struct policy_dbs_info *policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530377 int j;
378
379 /* Allocate memory for the common information for policy->cpus */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100380 policy_dbs = kzalloc(sizeof(*policy_dbs), GFP_KERNEL);
381 if (!policy_dbs)
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100382 return NULL;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530383
Viresh Kumar581c2142016-02-11 17:31:14 +0530384 policy_dbs->policy = policy;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100385 mutex_init(&policy_dbs->timer_mutex);
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100386 atomic_set(&policy_dbs->work_count, 0);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100387 init_irq_work(&policy_dbs->irq_work, dbs_irq_work);
388 INIT_WORK(&policy_dbs->work, dbs_work_handler);
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100389
390 /* Set policy_dbs for all CPUs, online+offline */
391 for_each_cpu(j, policy->related_cpus) {
392 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
393
394 j_cdbs->policy_dbs = policy_dbs;
395 j_cdbs->update_util.func = dbs_update_util_handler;
396 }
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100397 return policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530398}
399
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100400static void free_policy_dbs_info(struct cpufreq_policy *policy,
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100401 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530402{
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100403 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100404 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530405 int j;
406
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100407 mutex_destroy(&policy_dbs->timer_mutex);
Viresh Kumar5e4500d2015-12-03 09:37:52 +0530408
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100409 for_each_cpu(j, policy->related_cpus) {
410 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530411
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100412 j_cdbs->policy_dbs = NULL;
413 j_cdbs->update_util.func = NULL;
414 }
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100415 kfree(policy_dbs);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530416}
417
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100418static int cpufreq_governor_init(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530419{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100420 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100421 struct dbs_data *dbs_data = gov->gdbs_data;
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100422 struct policy_dbs_info *policy_dbs;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530423 unsigned int latency;
424 int ret;
425
Viresh Kumara72c4952015-07-18 11:31:01 +0530426 /* State should be equivalent to EXIT */
427 if (policy->governor_data)
428 return -EBUSY;
429
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100430 policy_dbs = alloc_policy_dbs_info(policy, gov);
431 if (!policy_dbs)
432 return -ENOMEM;
433
Viresh Kumar714a2d92015-06-04 16:43:27 +0530434 if (dbs_data) {
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100435 if (WARN_ON(have_governor_per_policy())) {
436 ret = -EINVAL;
437 goto free_policy_dbs_info;
438 }
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100439 policy_dbs->dbs_data = dbs_data;
440 policy->governor_data = policy_dbs;
Viresh Kumarc54df072016-02-10 11:00:25 +0530441
442 mutex_lock(&dbs_data->mutex);
443 dbs_data->usage_count++;
444 list_add(&policy_dbs->list, &dbs_data->policy_dbs_list);
445 mutex_unlock(&dbs_data->mutex);
446
Viresh Kumar714a2d92015-06-04 16:43:27 +0530447 return 0;
448 }
449
450 dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100451 if (!dbs_data) {
452 ret = -ENOMEM;
453 goto free_policy_dbs_info;
454 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530455
Viresh Kumarc54df072016-02-10 11:00:25 +0530456 INIT_LIST_HEAD(&dbs_data->policy_dbs_list);
Viresh Kumarc4435632016-02-09 09:01:33 +0530457 mutex_init(&dbs_data->mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530458
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100459 ret = gov->init(dbs_data, !policy->governor->initialized);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530460 if (ret)
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100461 goto free_policy_dbs_info;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530462
463 /* policy latency is in ns. Convert it to us first */
464 latency = policy->cpuinfo.transition_latency / 1000;
465 if (latency == 0)
466 latency = 1;
467
468 /* Bring kernel and HW constraints together */
469 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
470 MIN_LATENCY_MULTIPLIER * latency);
Viresh Kumarff4b1782016-02-09 09:01:32 +0530471 dbs_data->sampling_rate = max(dbs_data->min_sampling_rate,
472 LATENCY_MULTIPLIER * latency);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530473
Viresh Kumar8eec1022015-10-15 21:35:22 +0530474 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100475 gov->gdbs_data = dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530476
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100477 policy->governor_data = policy_dbs;
Viresh Kumare4b133c2016-01-25 22:33:46 +0530478
Viresh Kumarc54df072016-02-10 11:00:25 +0530479 policy_dbs->dbs_data = dbs_data;
480 dbs_data->usage_count = 1;
481 list_add(&policy_dbs->list, &dbs_data->policy_dbs_list);
482
Viresh Kumarc4435632016-02-09 09:01:33 +0530483 gov->kobj_type.sysfs_ops = &governor_sysfs_ops;
484 ret = kobject_init_and_add(&dbs_data->kobj, &gov->kobj_type,
485 get_governor_parent_kobj(policy),
486 "%s", gov->gov.name);
Rafael J. Wysockifafd5e82016-02-08 23:57:22 +0100487 if (!ret)
488 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530489
Rafael J. Wysockifafd5e82016-02-08 23:57:22 +0100490 /* Failure, so roll back. */
Viresh Kumarc4435632016-02-09 09:01:33 +0530491 pr_err("cpufreq: Governor initialization failed (dbs_data kobject init error %d)\n", ret);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530492
Viresh Kumare4b133c2016-01-25 22:33:46 +0530493 policy->governor_data = NULL;
494
Viresh Kumar8eec1022015-10-15 21:35:22 +0530495 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100496 gov->gdbs_data = NULL;
497 gov->exit(dbs_data, !policy->governor->initialized);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100498 kfree(dbs_data);
499
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100500free_policy_dbs_info:
501 free_policy_dbs_info(policy, gov);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530502 return ret;
503}
504
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100505static int cpufreq_governor_exit(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530506{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100507 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100508 struct policy_dbs_info *policy_dbs = policy->governor_data;
509 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumarc54df072016-02-10 11:00:25 +0530510 int count;
Viresh Kumara72c4952015-07-18 11:31:01 +0530511
Viresh Kumarc54df072016-02-10 11:00:25 +0530512 mutex_lock(&dbs_data->mutex);
513 list_del(&policy_dbs->list);
514 count = --dbs_data->usage_count;
515 mutex_unlock(&dbs_data->mutex);
516
517 if (!count) {
Viresh Kumarc4435632016-02-09 09:01:33 +0530518 kobject_put(&dbs_data->kobj);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530519
Viresh Kumare4b133c2016-01-25 22:33:46 +0530520 policy->governor_data = NULL;
521
Viresh Kumar8eec1022015-10-15 21:35:22 +0530522 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100523 gov->gdbs_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530524
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100525 gov->exit(dbs_data, policy->governor->initialized == 1);
Viresh Kumarc4435632016-02-09 09:01:33 +0530526 mutex_destroy(&dbs_data->mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530527 kfree(dbs_data);
Viresh Kumare4b133c2016-01-25 22:33:46 +0530528 } else {
529 policy->governor_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530530 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530531
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100532 free_policy_dbs_info(policy, gov);
Viresh Kumara72c4952015-07-18 11:31:01 +0530533 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530534}
535
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100536static int cpufreq_governor_start(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530537{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100538 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100539 struct policy_dbs_info *policy_dbs = policy->governor_data;
540 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530541 unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530542 int io_busy = 0;
543
544 if (!policy->cur)
545 return -EINVAL;
546
Rafael J. Wysockie4db2812016-02-15 02:13:42 +0100547 policy_dbs->is_shared = policy_is_shared(policy);
548
Viresh Kumarff4b1782016-02-09 09:01:32 +0530549 sampling_rate = dbs_data->sampling_rate;
550 ignore_nice = dbs_data->ignore_nice_load;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530551
Viresh Kumarff4b1782016-02-09 09:01:32 +0530552 if (gov->governor == GOV_ONDEMAND) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530553 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
554
Viresh Kumar714a2d92015-06-04 16:43:27 +0530555 io_busy = od_tuners->io_is_busy;
556 }
557
Viresh Kumar714a2d92015-06-04 16:43:27 +0530558 for_each_cpu(j, policy->cpus) {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100559 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530560 unsigned int prev_load;
561
Rafael J. Wysocki57eb8322016-02-16 00:58:47 +0100562 j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530563
Rafael J. Wysocki57eb8322016-02-16 00:58:47 +0100564 prev_load = j_cdbs->prev_cpu_wall - j_cdbs->prev_cpu_idle;
565 j_cdbs->prev_load = 100 * prev_load / (unsigned int)j_cdbs->prev_cpu_wall;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530566
567 if (ignore_nice)
568 j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Viresh Kumar714a2d92015-06-04 16:43:27 +0530569 }
570
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100571 if (gov->governor == GOV_CONSERVATIVE) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530572 struct cs_cpu_dbs_info_s *cs_dbs_info =
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100573 gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530574
575 cs_dbs_info->down_skip = 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530576 cs_dbs_info->requested_freq = policy->cur;
577 } else {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100578 struct od_ops *od_ops = gov->gov_ops;
579 struct od_cpu_dbs_info_s *od_dbs_info = gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530580
581 od_dbs_info->rate_mult = 1;
582 od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
583 od_ops->powersave_bias_init_cpu(cpu);
584 }
585
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100586 gov_set_update_util(policy_dbs, sampling_rate);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530587 return 0;
588}
589
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100590static int cpufreq_governor_stop(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530591{
Viresh Kumar581c2142016-02-11 17:31:14 +0530592 gov_cancel_work(policy);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530593
Viresh Kumara72c4952015-07-18 11:31:01 +0530594 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530595}
596
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100597static int cpufreq_governor_limits(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530598{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100599 struct policy_dbs_info *policy_dbs = policy->governor_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530600
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100601 mutex_lock(&policy_dbs->timer_mutex);
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100602
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100603 if (policy->max < policy->cur)
604 __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
605 else if (policy->min > policy->cur)
606 __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
Rafael J. Wysocki4cccf752016-02-15 02:19:31 +0100607
608 gov_update_sample_delay(policy_dbs, 0);
609
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100610 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumara72c4952015-07-18 11:31:01 +0530611
612 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530613}
614
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100615int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000616{
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100617 int ret = -EINVAL;
Viresh Kumar4471a342012-10-26 00:47:42 +0200618
Viresh Kumar732b6d62015-06-03 15:57:13 +0530619 /* Lock governor to block concurrent initialization of governor */
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100620 mutex_lock(&dbs_data_mutex);
Viresh Kumar732b6d62015-06-03 15:57:13 +0530621
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100622 if (event == CPUFREQ_GOV_POLICY_INIT) {
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100623 ret = cpufreq_governor_init(policy);
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100624 } else if (policy->governor_data) {
625 switch (event) {
626 case CPUFREQ_GOV_POLICY_EXIT:
627 ret = cpufreq_governor_exit(policy);
628 break;
629 case CPUFREQ_GOV_START:
630 ret = cpufreq_governor_start(policy);
631 break;
632 case CPUFREQ_GOV_STOP:
633 ret = cpufreq_governor_stop(policy);
634 break;
635 case CPUFREQ_GOV_LIMITS:
636 ret = cpufreq_governor_limits(policy);
637 break;
638 }
Viresh Kumar732b6d62015-06-03 15:57:13 +0530639 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000640
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100641 mutex_unlock(&dbs_data_mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530642 return ret;
Viresh Kumar4471a342012-10-26 00:47:42 +0200643}
644EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);