blob: a34de9d10cbc8001f7bf3f50fdd857f66f423a9e [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 Kumarc4435632016-02-09 09:01:33 +053028static inline struct dbs_data *to_dbs_data(struct kobject *kobj)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000029{
Viresh Kumarc4435632016-02-09 09:01:33 +053030 return container_of(kobj, struct dbs_data, kobj);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000031}
32
Viresh Kumarc4435632016-02-09 09:01:33 +053033static inline struct governor_attr *to_gov_attr(struct attribute *attr)
34{
35 return container_of(attr, struct governor_attr, attr);
36}
37
38static ssize_t governor_show(struct kobject *kobj, struct attribute *attr,
39 char *buf)
40{
41 struct dbs_data *dbs_data = to_dbs_data(kobj);
42 struct governor_attr *gattr = to_gov_attr(attr);
43 int ret = -EIO;
44
45 if (gattr->show)
46 ret = gattr->show(dbs_data, buf);
47
48 return ret;
49}
50
51static ssize_t governor_store(struct kobject *kobj, struct attribute *attr,
52 const char *buf, size_t count)
53{
54 struct dbs_data *dbs_data = to_dbs_data(kobj);
55 struct governor_attr *gattr = to_gov_attr(attr);
56 int ret = -EIO;
57
58 mutex_lock(&dbs_data->mutex);
59
60 if (gattr->store)
61 ret = gattr->store(dbs_data, buf, count);
62
63 mutex_unlock(&dbs_data->mutex);
64
65 return ret;
66}
67
68/*
69 * Sysfs Ops for accessing governor attributes.
70 *
71 * All show/store invocations for governor specific sysfs attributes, will first
72 * call the below show/store callbacks and the attribute specific callback will
73 * be called from within it.
74 */
75static const struct sysfs_ops governor_sysfs_ops = {
76 .show = governor_show,
77 .store = governor_store,
78};
79
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +010080void dbs_check_cpu(struct cpufreq_policy *policy)
Viresh Kumar4471a342012-10-26 00:47:42 +020081{
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +010082 int cpu = policy->cpu;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010083 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +010084 struct policy_dbs_info *policy_dbs = policy->governor_data;
85 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4471a342012-10-26 00:47:42 +020086 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
Viresh Kumarff4b1782016-02-09 09:01:32 +053087 unsigned int sampling_rate = dbs_data->sampling_rate;
88 unsigned int ignore_nice = dbs_data->ignore_nice_load;
Viresh Kumar4471a342012-10-26 00:47:42 +020089 unsigned int max_load = 0;
Viresh Kumar4471a342012-10-26 00:47:42 +020090 unsigned int j;
91
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010092 if (gov->governor == GOV_ONDEMAND) {
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053093 struct od_cpu_dbs_info_s *od_dbs_info =
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010094 gov->get_cpu_dbs_info_s(cpu);
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053095
96 /*
97 * Sometimes, the ondemand governor uses an additional
98 * multiplier to give long delays. So apply this multiplier to
99 * the 'sampling_rate', so as to keep the wake-up-from-idle
100 * detection logic a bit conservative.
101 */
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530102 sampling_rate *= od_dbs_info->rate_mult;
103
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530104 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200105
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +0300106 /* Get Absolute Load */
Viresh Kumar4471a342012-10-26 00:47:42 +0200107 for_each_cpu(j, policy->cpus) {
Viresh Kumar875b8502015-06-19 17:18:03 +0530108 struct cpu_dbs_info *j_cdbs;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000109 u64 cur_wall_time, cur_idle_time;
110 unsigned int idle_time, wall_time;
Viresh Kumar4471a342012-10-26 00:47:42 +0200111 unsigned int load;
Stratos Karafotis9366d842013-02-28 16:57:32 +0000112 int io_busy = 0;
Viresh Kumar4471a342012-10-26 00:47:42 +0200113
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100114 j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200115
Stratos Karafotis9366d842013-02-28 16:57:32 +0000116 /*
117 * For the purpose of ondemand, waiting for disk IO is
118 * an indication that you're performance critical, and
119 * not that the system is actually idle. So do not add
120 * the iowait time to the cpu idle time.
121 */
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100122 if (gov->governor == GOV_ONDEMAND)
Stratos Karafotis9366d842013-02-28 16:57:32 +0000123 io_busy = od_tuners->io_is_busy;
124 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
Viresh Kumar4471a342012-10-26 00:47:42 +0200125
126 wall_time = (unsigned int)
127 (cur_wall_time - j_cdbs->prev_cpu_wall);
128 j_cdbs->prev_cpu_wall = cur_wall_time;
129
Chen Yu0df35022015-12-16 12:20:29 +0800130 if (cur_idle_time < j_cdbs->prev_cpu_idle)
131 cur_idle_time = j_cdbs->prev_cpu_idle;
132
Viresh Kumar4471a342012-10-26 00:47:42 +0200133 idle_time = (unsigned int)
134 (cur_idle_time - j_cdbs->prev_cpu_idle);
135 j_cdbs->prev_cpu_idle = cur_idle_time;
136
137 if (ignore_nice) {
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100138 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
Viresh Kumar4471a342012-10-26 00:47:42 +0200139 u64 cur_nice;
140 unsigned long cur_nice_jiffies;
141
142 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
143 cdbs->prev_cpu_nice;
144 /*
145 * Assumption: nice time between sampling periods will
146 * be less than 2^32 jiffies for 32 bit sys
147 */
148 cur_nice_jiffies = (unsigned long)
149 cputime64_to_jiffies64(cur_nice);
150
151 cdbs->prev_cpu_nice =
152 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
153 idle_time += jiffies_to_usecs(cur_nice_jiffies);
154 }
155
Viresh Kumar4471a342012-10-26 00:47:42 +0200156 if (unlikely(!wall_time || wall_time < idle_time))
157 continue;
158
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530159 /*
160 * If the CPU had gone completely idle, and a task just woke up
161 * on this CPU now, it would be unfair to calculate 'load' the
162 * usual way for this elapsed time-window, because it will show
163 * near-zero load, irrespective of how CPU intensive that task
164 * actually is. This is undesirable for latency-sensitive bursty
165 * workloads.
166 *
167 * To avoid this, we reuse the 'load' from the previous
168 * time-window and give this task a chance to start with a
169 * reasonably high CPU frequency. (However, we shouldn't over-do
170 * this copy, lest we get stuck at a high load (high frequency)
171 * for too long, even when the current system load has actually
172 * dropped down. So we perform the copy only once, upon the
173 * first wake-up from idle.)
174 *
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100175 * Detecting this situation is easy: the governor's utilization
176 * update handler would not have run during CPU-idle periods.
177 * Hence, an unusually large 'wall_time' (as compared to the
178 * sampling rate) indicates this scenario.
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530179 *
180 * prev_load can be zero in two cases and we must recalculate it
181 * for both cases:
182 * - during long idle intervals
183 * - explicitly set to zero
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530184 */
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530185 if (unlikely(wall_time > (2 * sampling_rate) &&
186 j_cdbs->prev_load)) {
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530187 load = j_cdbs->prev_load;
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530188
189 /*
190 * Perform a destructive copy, to ensure that we copy
191 * the previous load only once, upon the first wake-up
192 * from idle.
193 */
194 j_cdbs->prev_load = 0;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530195 } else {
196 load = 100 * (wall_time - idle_time) / wall_time;
197 j_cdbs->prev_load = load;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530198 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200199
Viresh Kumar4471a342012-10-26 00:47:42 +0200200 if (load > max_load)
201 max_load = load;
202 }
203
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100204 gov->gov_check_cpu(cpu, max_load);
Viresh Kumar4471a342012-10-26 00:47:42 +0200205}
206EXPORT_SYMBOL_GPL(dbs_check_cpu);
207
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100208void gov_set_update_util(struct policy_dbs_info *policy_dbs,
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100209 unsigned int delay_us)
Viresh Kumar4471a342012-10-26 00:47:42 +0200210{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100211 struct cpufreq_policy *policy = policy_dbs->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100212 struct dbs_governor *gov = dbs_governor_of(policy);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530213 int cpu;
Viresh Kumar4471a342012-10-26 00:47:42 +0200214
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100215 gov_update_sample_delay(policy_dbs, delay_us);
216 policy_dbs->last_sample_time = 0;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100217
Viresh Kumar70f43e52015-12-09 07:34:42 +0530218 for_each_cpu(cpu, policy->cpus) {
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100219 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100220
221 cpufreq_set_update_util_data(cpu, &cdbs->update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530222 }
223}
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100224EXPORT_SYMBOL_GPL(gov_set_update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530225
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100226static inline void gov_clear_update_util(struct cpufreq_policy *policy)
Viresh Kumar031299b2013-02-27 12:24:03 +0530227{
Viresh Kumar031299b2013-02-27 12:24:03 +0530228 int i;
229
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100230 for_each_cpu(i, policy->cpus)
231 cpufreq_set_update_util_data(i, NULL);
232
233 synchronize_rcu();
Viresh Kumar4471a342012-10-26 00:47:42 +0200234}
235
Viresh Kumar581c2142016-02-11 17:31:14 +0530236static void gov_cancel_work(struct cpufreq_policy *policy)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530237{
Viresh Kumar581c2142016-02-11 17:31:14 +0530238 struct policy_dbs_info *policy_dbs = policy->governor_data;
239
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100240 /* Tell dbs_update_util_handler() to skip queuing up work items. */
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100241 atomic_inc(&policy_dbs->work_count);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530242 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100243 * If dbs_update_util_handler() is already running, it may not notice
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100244 * the incremented work_count, so wait for it to complete to prevent its
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100245 * work item from being queued up after the cancel_work_sync() below.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530246 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100247 gov_clear_update_util(policy_dbs->policy);
248 irq_work_sync(&policy_dbs->irq_work);
249 cancel_work_sync(&policy_dbs->work);
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100250 atomic_set(&policy_dbs->work_count, 0);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530251}
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530252
Viresh Kumar70f43e52015-12-09 07:34:42 +0530253static void dbs_work_handler(struct work_struct *work)
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530254{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100255 struct policy_dbs_info *policy_dbs;
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530256 struct cpufreq_policy *policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100257 struct dbs_governor *gov;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100258 unsigned int delay;
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530259
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100260 policy_dbs = container_of(work, struct policy_dbs_info, work);
261 policy = policy_dbs->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100262 gov = dbs_governor_of(policy);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530263
Viresh Kumar70f43e52015-12-09 07:34:42 +0530264 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100265 * Make sure cpufreq_governor_limits() isn't evaluating load or the
266 * ondemand governor isn't updating the sampling rate in parallel.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530267 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100268 mutex_lock(&policy_dbs->timer_mutex);
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100269 delay = gov->gov_dbs_timer(policy);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100270 policy_dbs->sample_delay_ns = jiffies_to_nsecs(delay);
271 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530272
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100273 /*
274 * If the atomic operation below is reordered with respect to the
275 * sample delay modification, the utilization update handler may end
276 * up using a stale sample delay value.
277 */
278 smp_mb__before_atomic();
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100279 atomic_dec(&policy_dbs->work_count);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530280}
281
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100282static void dbs_irq_work(struct irq_work *irq_work)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530283{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100284 struct policy_dbs_info *policy_dbs;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100285
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100286 policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work);
287 schedule_work(&policy_dbs->work);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100288}
289
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100290static inline void gov_queue_irq_work(struct policy_dbs_info *policy_dbs)
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100291{
292#ifdef CONFIG_SMP
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100293 irq_work_queue_on(&policy_dbs->irq_work, smp_processor_id());
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100294#else
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100295 irq_work_queue(&policy_dbs->irq_work);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100296#endif
297}
298
299static void dbs_update_util_handler(struct update_util_data *data, u64 time,
300 unsigned long util, unsigned long max)
301{
302 struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100303 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530304
305 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100306 * The work may not be allowed to be queued up right now.
307 * Possible reasons:
308 * - Work has already been queued up or is in progress.
309 * - The governor is being stopped.
310 * - It is too early (too little time from the previous sample).
Viresh Kumar70f43e52015-12-09 07:34:42 +0530311 */
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100312 if (atomic_inc_return(&policy_dbs->work_count) == 1) {
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100313 u64 delta_ns;
314
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100315 delta_ns = time - policy_dbs->last_sample_time;
316 if ((s64)delta_ns >= policy_dbs->sample_delay_ns) {
317 policy_dbs->last_sample_time = time;
318 gov_queue_irq_work(policy_dbs);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100319 return;
320 }
321 }
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100322 atomic_dec(&policy_dbs->work_count);
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530323}
Viresh Kumar44472662013-01-31 17:28:02 +0000324
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100325static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy,
326 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530327{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100328 struct policy_dbs_info *policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530329 int j;
330
331 /* Allocate memory for the common information for policy->cpus */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100332 policy_dbs = kzalloc(sizeof(*policy_dbs), GFP_KERNEL);
333 if (!policy_dbs)
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100334 return NULL;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530335
Viresh Kumar581c2142016-02-11 17:31:14 +0530336 policy_dbs->policy = policy;
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100337 mutex_init(&policy_dbs->timer_mutex);
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100338 atomic_set(&policy_dbs->work_count, 0);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100339 init_irq_work(&policy_dbs->irq_work, dbs_irq_work);
340 INIT_WORK(&policy_dbs->work, dbs_work_handler);
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100341
342 /* Set policy_dbs for all CPUs, online+offline */
343 for_each_cpu(j, policy->related_cpus) {
344 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
345
346 j_cdbs->policy_dbs = policy_dbs;
347 j_cdbs->update_util.func = dbs_update_util_handler;
348 }
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100349 return policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530350}
351
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100352static void free_policy_dbs_info(struct cpufreq_policy *policy,
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100353 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530354{
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100355 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100356 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530357 int j;
358
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100359 mutex_destroy(&policy_dbs->timer_mutex);
Viresh Kumar5e4500d2015-12-03 09:37:52 +0530360
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100361 for_each_cpu(j, policy->related_cpus) {
362 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530363
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100364 j_cdbs->policy_dbs = NULL;
365 j_cdbs->update_util.func = NULL;
366 }
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100367 kfree(policy_dbs);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530368}
369
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100370static int cpufreq_governor_init(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530371{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100372 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100373 struct dbs_data *dbs_data = gov->gdbs_data;
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100374 struct policy_dbs_info *policy_dbs;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530375 unsigned int latency;
376 int ret;
377
Viresh Kumara72c4952015-07-18 11:31:01 +0530378 /* State should be equivalent to EXIT */
379 if (policy->governor_data)
380 return -EBUSY;
381
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100382 policy_dbs = alloc_policy_dbs_info(policy, gov);
383 if (!policy_dbs)
384 return -ENOMEM;
385
Viresh Kumar714a2d92015-06-04 16:43:27 +0530386 if (dbs_data) {
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100387 if (WARN_ON(have_governor_per_policy())) {
388 ret = -EINVAL;
389 goto free_policy_dbs_info;
390 }
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100391 policy_dbs->dbs_data = dbs_data;
392 policy->governor_data = policy_dbs;
Viresh Kumarc54df072016-02-10 11:00:25 +0530393
394 mutex_lock(&dbs_data->mutex);
395 dbs_data->usage_count++;
396 list_add(&policy_dbs->list, &dbs_data->policy_dbs_list);
397 mutex_unlock(&dbs_data->mutex);
398
Viresh Kumar714a2d92015-06-04 16:43:27 +0530399 return 0;
400 }
401
402 dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100403 if (!dbs_data) {
404 ret = -ENOMEM;
405 goto free_policy_dbs_info;
406 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530407
Viresh Kumarc54df072016-02-10 11:00:25 +0530408 INIT_LIST_HEAD(&dbs_data->policy_dbs_list);
Viresh Kumarc4435632016-02-09 09:01:33 +0530409 mutex_init(&dbs_data->mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530410
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100411 ret = gov->init(dbs_data, !policy->governor->initialized);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530412 if (ret)
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100413 goto free_policy_dbs_info;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530414
415 /* policy latency is in ns. Convert it to us first */
416 latency = policy->cpuinfo.transition_latency / 1000;
417 if (latency == 0)
418 latency = 1;
419
420 /* Bring kernel and HW constraints together */
421 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
422 MIN_LATENCY_MULTIPLIER * latency);
Viresh Kumarff4b1782016-02-09 09:01:32 +0530423 dbs_data->sampling_rate = max(dbs_data->min_sampling_rate,
424 LATENCY_MULTIPLIER * latency);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530425
Viresh Kumar8eec1022015-10-15 21:35:22 +0530426 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100427 gov->gdbs_data = dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530428
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100429 policy->governor_data = policy_dbs;
Viresh Kumare4b133c2016-01-25 22:33:46 +0530430
Viresh Kumarc54df072016-02-10 11:00:25 +0530431 policy_dbs->dbs_data = dbs_data;
432 dbs_data->usage_count = 1;
433 list_add(&policy_dbs->list, &dbs_data->policy_dbs_list);
434
Viresh Kumarc4435632016-02-09 09:01:33 +0530435 gov->kobj_type.sysfs_ops = &governor_sysfs_ops;
436 ret = kobject_init_and_add(&dbs_data->kobj, &gov->kobj_type,
437 get_governor_parent_kobj(policy),
438 "%s", gov->gov.name);
Rafael J. Wysockifafd5e82016-02-08 23:57:22 +0100439 if (!ret)
440 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530441
Rafael J. Wysockifafd5e82016-02-08 23:57:22 +0100442 /* Failure, so roll back. */
Viresh Kumarc4435632016-02-09 09:01:33 +0530443 pr_err("cpufreq: Governor initialization failed (dbs_data kobject init error %d)\n", ret);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530444
Viresh Kumare4b133c2016-01-25 22:33:46 +0530445 policy->governor_data = NULL;
446
Viresh Kumar8eec1022015-10-15 21:35:22 +0530447 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100448 gov->gdbs_data = NULL;
449 gov->exit(dbs_data, !policy->governor->initialized);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100450 kfree(dbs_data);
451
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100452free_policy_dbs_info:
453 free_policy_dbs_info(policy, gov);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530454 return ret;
455}
456
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100457static int cpufreq_governor_exit(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530458{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100459 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100460 struct policy_dbs_info *policy_dbs = policy->governor_data;
461 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumarc54df072016-02-10 11:00:25 +0530462 int count;
Viresh Kumara72c4952015-07-18 11:31:01 +0530463
Viresh Kumarc54df072016-02-10 11:00:25 +0530464 mutex_lock(&dbs_data->mutex);
465 list_del(&policy_dbs->list);
466 count = --dbs_data->usage_count;
467 mutex_unlock(&dbs_data->mutex);
468
469 if (!count) {
Viresh Kumarc4435632016-02-09 09:01:33 +0530470 kobject_put(&dbs_data->kobj);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530471
Viresh Kumare4b133c2016-01-25 22:33:46 +0530472 policy->governor_data = NULL;
473
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 = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530476
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100477 gov->exit(dbs_data, policy->governor->initialized == 1);
Viresh Kumarc4435632016-02-09 09:01:33 +0530478 mutex_destroy(&dbs_data->mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530479 kfree(dbs_data);
Viresh Kumare4b133c2016-01-25 22:33:46 +0530480 } else {
481 policy->governor_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530482 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530483
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100484 free_policy_dbs_info(policy, gov);
Viresh Kumara72c4952015-07-18 11:31:01 +0530485 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530486}
487
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100488static int cpufreq_governor_start(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530489{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100490 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100491 struct policy_dbs_info *policy_dbs = policy->governor_data;
492 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530493 unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530494 int io_busy = 0;
495
496 if (!policy->cur)
497 return -EINVAL;
498
Viresh Kumarff4b1782016-02-09 09:01:32 +0530499 sampling_rate = dbs_data->sampling_rate;
500 ignore_nice = dbs_data->ignore_nice_load;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530501
Viresh Kumarff4b1782016-02-09 09:01:32 +0530502 if (gov->governor == GOV_ONDEMAND) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530503 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
504
Viresh Kumar714a2d92015-06-04 16:43:27 +0530505 io_busy = od_tuners->io_is_busy;
506 }
507
Viresh Kumar714a2d92015-06-04 16:43:27 +0530508 for_each_cpu(j, policy->cpus) {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100509 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530510 unsigned int prev_load;
511
Viresh Kumar714a2d92015-06-04 16:43:27 +0530512 j_cdbs->prev_cpu_idle =
513 get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy);
514
515 prev_load = (unsigned int)(j_cdbs->prev_cpu_wall -
516 j_cdbs->prev_cpu_idle);
517 j_cdbs->prev_load = 100 * prev_load /
518 (unsigned int)j_cdbs->prev_cpu_wall;
519
520 if (ignore_nice)
521 j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Viresh Kumar714a2d92015-06-04 16:43:27 +0530522 }
523
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100524 if (gov->governor == GOV_CONSERVATIVE) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530525 struct cs_cpu_dbs_info_s *cs_dbs_info =
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100526 gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530527
528 cs_dbs_info->down_skip = 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530529 cs_dbs_info->requested_freq = policy->cur;
530 } else {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100531 struct od_ops *od_ops = gov->gov_ops;
532 struct od_cpu_dbs_info_s *od_dbs_info = gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530533
534 od_dbs_info->rate_mult = 1;
535 od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
536 od_ops->powersave_bias_init_cpu(cpu);
537 }
538
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100539 gov_set_update_util(policy_dbs, sampling_rate);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530540 return 0;
541}
542
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100543static int cpufreq_governor_stop(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530544{
Viresh Kumar581c2142016-02-11 17:31:14 +0530545 gov_cancel_work(policy);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530546
Viresh Kumara72c4952015-07-18 11:31:01 +0530547 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530548}
549
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100550static int cpufreq_governor_limits(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530551{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100552 struct policy_dbs_info *policy_dbs = policy->governor_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530553
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100554 mutex_lock(&policy_dbs->timer_mutex);
555 if (policy->max < policy->cur)
556 __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
557 else if (policy->min > policy->cur)
558 __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +0100559 dbs_check_cpu(policy);
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100560 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumara72c4952015-07-18 11:31:01 +0530561
562 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530563}
564
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100565int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000566{
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100567 int ret = -EINVAL;
Viresh Kumar4471a342012-10-26 00:47:42 +0200568
Viresh Kumar732b6d62015-06-03 15:57:13 +0530569 /* Lock governor to block concurrent initialization of governor */
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100570 mutex_lock(&dbs_data_mutex);
Viresh Kumar732b6d62015-06-03 15:57:13 +0530571
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100572 if (event == CPUFREQ_GOV_POLICY_INIT) {
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100573 ret = cpufreq_governor_init(policy);
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100574 } else if (policy->governor_data) {
575 switch (event) {
576 case CPUFREQ_GOV_POLICY_EXIT:
577 ret = cpufreq_governor_exit(policy);
578 break;
579 case CPUFREQ_GOV_START:
580 ret = cpufreq_governor_start(policy);
581 break;
582 case CPUFREQ_GOV_STOP:
583 ret = cpufreq_governor_stop(policy);
584 break;
585 case CPUFREQ_GOV_LIMITS:
586 ret = cpufreq_governor_limits(policy);
587 break;
588 }
Viresh Kumar732b6d62015-06-03 15:57:13 +0530589 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000590
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100591 mutex_unlock(&dbs_data_mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530592 return ret;
Viresh Kumar4471a342012-10-26 00:47:42 +0200593}
594EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);