blob: 00cb468d3b6a55d49fabe629a303bdace0122e1b [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
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100236static void gov_cancel_work(struct policy_dbs_info *policy_dbs)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530237{
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100238 /* Tell dbs_update_util_handler() to skip queuing up work items. */
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100239 atomic_inc(&policy_dbs->work_count);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530240 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100241 * If dbs_update_util_handler() is already running, it may not notice
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100242 * the incremented work_count, so wait for it to complete to prevent its
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100243 * work item from being queued up after the cancel_work_sync() below.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530244 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100245 gov_clear_update_util(policy_dbs->policy);
246 irq_work_sync(&policy_dbs->irq_work);
247 cancel_work_sync(&policy_dbs->work);
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100248 atomic_set(&policy_dbs->work_count, 0);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530249}
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530250
Viresh Kumar70f43e52015-12-09 07:34:42 +0530251static void dbs_work_handler(struct work_struct *work)
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530252{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100253 struct policy_dbs_info *policy_dbs;
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530254 struct cpufreq_policy *policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100255 struct dbs_governor *gov;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100256 unsigned int delay;
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530257
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100258 policy_dbs = container_of(work, struct policy_dbs_info, work);
259 policy = policy_dbs->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100260 gov = dbs_governor_of(policy);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530261
Viresh Kumar70f43e52015-12-09 07:34:42 +0530262 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100263 * Make sure cpufreq_governor_limits() isn't evaluating load or the
264 * ondemand governor isn't updating the sampling rate in parallel.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530265 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100266 mutex_lock(&policy_dbs->timer_mutex);
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100267 delay = gov->gov_dbs_timer(policy);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100268 policy_dbs->sample_delay_ns = jiffies_to_nsecs(delay);
269 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530270
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100271 /*
272 * If the atomic operation below is reordered with respect to the
273 * sample delay modification, the utilization update handler may end
274 * up using a stale sample delay value.
275 */
276 smp_mb__before_atomic();
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100277 atomic_dec(&policy_dbs->work_count);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530278}
279
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100280static void dbs_irq_work(struct irq_work *irq_work)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530281{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100282 struct policy_dbs_info *policy_dbs;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100283
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100284 policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work);
285 schedule_work(&policy_dbs->work);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100286}
287
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100288static inline void gov_queue_irq_work(struct policy_dbs_info *policy_dbs)
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100289{
290#ifdef CONFIG_SMP
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100291 irq_work_queue_on(&policy_dbs->irq_work, smp_processor_id());
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100292#else
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100293 irq_work_queue(&policy_dbs->irq_work);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100294#endif
295}
296
297static void dbs_update_util_handler(struct update_util_data *data, u64 time,
298 unsigned long util, unsigned long max)
299{
300 struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100301 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530302
303 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100304 * The work may not be allowed to be queued up right now.
305 * Possible reasons:
306 * - Work has already been queued up or is in progress.
307 * - The governor is being stopped.
308 * - It is too early (too little time from the previous sample).
Viresh Kumar70f43e52015-12-09 07:34:42 +0530309 */
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100310 if (atomic_inc_return(&policy_dbs->work_count) == 1) {
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100311 u64 delta_ns;
312
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100313 delta_ns = time - policy_dbs->last_sample_time;
314 if ((s64)delta_ns >= policy_dbs->sample_delay_ns) {
315 policy_dbs->last_sample_time = time;
316 gov_queue_irq_work(policy_dbs);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100317 return;
318 }
319 }
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100320 atomic_dec(&policy_dbs->work_count);
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530321}
Viresh Kumar44472662013-01-31 17:28:02 +0000322
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100323static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy,
324 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530325{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100326 struct policy_dbs_info *policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530327 int j;
328
329 /* Allocate memory for the common information for policy->cpus */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100330 policy_dbs = kzalloc(sizeof(*policy_dbs), GFP_KERNEL);
331 if (!policy_dbs)
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100332 return NULL;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530333
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100334 mutex_init(&policy_dbs->timer_mutex);
Rafael J. Wysocki686cc632016-02-08 23:41:10 +0100335 atomic_set(&policy_dbs->work_count, 0);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100336 init_irq_work(&policy_dbs->irq_work, dbs_irq_work);
337 INIT_WORK(&policy_dbs->work, dbs_work_handler);
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100338
339 /* Set policy_dbs for all CPUs, online+offline */
340 for_each_cpu(j, policy->related_cpus) {
341 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
342
343 j_cdbs->policy_dbs = policy_dbs;
344 j_cdbs->update_util.func = dbs_update_util_handler;
345 }
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100346 return policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530347}
348
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100349static void free_policy_dbs_info(struct cpufreq_policy *policy,
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100350 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530351{
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100352 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100353 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530354 int j;
355
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100356 mutex_destroy(&policy_dbs->timer_mutex);
Viresh Kumar5e4500d2015-12-03 09:37:52 +0530357
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100358 for_each_cpu(j, policy->related_cpus) {
359 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530360
Rafael J. Wysockicea6a9e2016-02-07 16:25:02 +0100361 j_cdbs->policy_dbs = NULL;
362 j_cdbs->update_util.func = NULL;
363 }
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100364 kfree(policy_dbs);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530365}
366
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100367static int cpufreq_governor_init(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530368{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100369 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100370 struct dbs_data *dbs_data = gov->gdbs_data;
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100371 struct policy_dbs_info *policy_dbs;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530372 unsigned int latency;
373 int ret;
374
Viresh Kumara72c4952015-07-18 11:31:01 +0530375 /* State should be equivalent to EXIT */
376 if (policy->governor_data)
377 return -EBUSY;
378
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100379 policy_dbs = alloc_policy_dbs_info(policy, gov);
380 if (!policy_dbs)
381 return -ENOMEM;
382
Viresh Kumar714a2d92015-06-04 16:43:27 +0530383 if (dbs_data) {
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100384 if (WARN_ON(have_governor_per_policy())) {
385 ret = -EINVAL;
386 goto free_policy_dbs_info;
387 }
Viresh Kumar714a2d92015-06-04 16:43:27 +0530388 dbs_data->usage_count++;
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100389 policy_dbs->dbs_data = dbs_data;
390 policy->governor_data = policy_dbs;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530391 return 0;
392 }
393
394 dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100395 if (!dbs_data) {
396 ret = -ENOMEM;
397 goto free_policy_dbs_info;
398 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530399
Viresh Kumar714a2d92015-06-04 16:43:27 +0530400 dbs_data->usage_count = 1;
Viresh Kumarc4435632016-02-09 09:01:33 +0530401 mutex_init(&dbs_data->mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530402
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100403 ret = gov->init(dbs_data, !policy->governor->initialized);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530404 if (ret)
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100405 goto free_policy_dbs_info;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530406
407 /* policy latency is in ns. Convert it to us first */
408 latency = policy->cpuinfo.transition_latency / 1000;
409 if (latency == 0)
410 latency = 1;
411
412 /* Bring kernel and HW constraints together */
413 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
414 MIN_LATENCY_MULTIPLIER * latency);
Viresh Kumarff4b1782016-02-09 09:01:32 +0530415 dbs_data->sampling_rate = max(dbs_data->min_sampling_rate,
416 LATENCY_MULTIPLIER * latency);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530417
Viresh Kumar8eec1022015-10-15 21:35:22 +0530418 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100419 gov->gdbs_data = dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530420
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100421 policy_dbs->dbs_data = dbs_data;
422 policy->governor_data = policy_dbs;
Viresh Kumare4b133c2016-01-25 22:33:46 +0530423
Viresh Kumarc4435632016-02-09 09:01:33 +0530424 gov->kobj_type.sysfs_ops = &governor_sysfs_ops;
425 ret = kobject_init_and_add(&dbs_data->kobj, &gov->kobj_type,
426 get_governor_parent_kobj(policy),
427 "%s", gov->gov.name);
Rafael J. Wysockifafd5e82016-02-08 23:57:22 +0100428 if (!ret)
429 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530430
Rafael J. Wysockifafd5e82016-02-08 23:57:22 +0100431 /* Failure, so roll back. */
Viresh Kumarc4435632016-02-09 09:01:33 +0530432 pr_err("cpufreq: Governor initialization failed (dbs_data kobject init error %d)\n", ret);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530433
Viresh Kumare4b133c2016-01-25 22:33:46 +0530434 policy->governor_data = NULL;
435
Viresh Kumar8eec1022015-10-15 21:35:22 +0530436 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100437 gov->gdbs_data = NULL;
438 gov->exit(dbs_data, !policy->governor->initialized);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100439 kfree(dbs_data);
440
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100441free_policy_dbs_info:
442 free_policy_dbs_info(policy, gov);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530443 return ret;
444}
445
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100446static int cpufreq_governor_exit(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530447{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100448 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100449 struct policy_dbs_info *policy_dbs = policy->governor_data;
450 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumara72c4952015-07-18 11:31:01 +0530451
452 /* State should be equivalent to INIT */
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100453 if (policy_dbs->policy)
Viresh Kumara72c4952015-07-18 11:31:01 +0530454 return -EBUSY;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530455
Viresh Kumar714a2d92015-06-04 16:43:27 +0530456 if (!--dbs_data->usage_count) {
Viresh Kumarc4435632016-02-09 09:01:33 +0530457 kobject_put(&dbs_data->kobj);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530458
Viresh Kumare4b133c2016-01-25 22:33:46 +0530459 policy->governor_data = NULL;
460
Viresh Kumar8eec1022015-10-15 21:35:22 +0530461 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100462 gov->gdbs_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530463
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100464 gov->exit(dbs_data, policy->governor->initialized == 1);
Viresh Kumarc4435632016-02-09 09:01:33 +0530465 mutex_destroy(&dbs_data->mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530466 kfree(dbs_data);
Viresh Kumare4b133c2016-01-25 22:33:46 +0530467 } else {
468 policy->governor_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530469 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530470
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100471 free_policy_dbs_info(policy, gov);
Viresh Kumara72c4952015-07-18 11:31:01 +0530472 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530473}
474
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100475static int cpufreq_governor_start(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530476{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100477 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100478 struct policy_dbs_info *policy_dbs = policy->governor_data;
479 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530480 unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530481 int io_busy = 0;
482
483 if (!policy->cur)
484 return -EINVAL;
485
Viresh Kumara72c4952015-07-18 11:31:01 +0530486 /* State should be equivalent to INIT */
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100487 if (policy_dbs->policy)
Viresh Kumara72c4952015-07-18 11:31:01 +0530488 return -EBUSY;
489
Viresh Kumarff4b1782016-02-09 09:01:32 +0530490 sampling_rate = dbs_data->sampling_rate;
491 ignore_nice = dbs_data->ignore_nice_load;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530492
Viresh Kumarff4b1782016-02-09 09:01:32 +0530493 if (gov->governor == GOV_ONDEMAND) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530494 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
495
Viresh Kumar714a2d92015-06-04 16:43:27 +0530496 io_busy = od_tuners->io_is_busy;
497 }
498
Viresh Kumar714a2d92015-06-04 16:43:27 +0530499 for_each_cpu(j, policy->cpus) {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100500 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530501 unsigned int prev_load;
502
Viresh Kumar714a2d92015-06-04 16:43:27 +0530503 j_cdbs->prev_cpu_idle =
504 get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy);
505
506 prev_load = (unsigned int)(j_cdbs->prev_cpu_wall -
507 j_cdbs->prev_cpu_idle);
508 j_cdbs->prev_load = 100 * prev_load /
509 (unsigned int)j_cdbs->prev_cpu_wall;
510
511 if (ignore_nice)
512 j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Viresh Kumar714a2d92015-06-04 16:43:27 +0530513 }
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100514 policy_dbs->policy = policy;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530515
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100516 if (gov->governor == GOV_CONSERVATIVE) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530517 struct cs_cpu_dbs_info_s *cs_dbs_info =
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100518 gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530519
520 cs_dbs_info->down_skip = 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530521 cs_dbs_info->requested_freq = policy->cur;
522 } else {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100523 struct od_ops *od_ops = gov->gov_ops;
524 struct od_cpu_dbs_info_s *od_dbs_info = gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530525
526 od_dbs_info->rate_mult = 1;
527 od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
528 od_ops->powersave_bias_init_cpu(cpu);
529 }
530
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100531 gov_set_update_util(policy_dbs, sampling_rate);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530532 return 0;
533}
534
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100535static int cpufreq_governor_stop(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530536{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100537 struct policy_dbs_info *policy_dbs = policy->governor_data;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530538
Viresh Kumara72c4952015-07-18 11:31:01 +0530539 /* State should be equivalent to START */
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100540 if (!policy_dbs->policy)
Viresh Kumara72c4952015-07-18 11:31:01 +0530541 return -EBUSY;
542
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100543 gov_cancel_work(policy_dbs);
544 policy_dbs->policy = NULL;
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530545
Viresh Kumara72c4952015-07-18 11:31:01 +0530546 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530547}
548
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100549static int cpufreq_governor_limits(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530550{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100551 struct policy_dbs_info *policy_dbs = policy->governor_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530552
Viresh Kumara72c4952015-07-18 11:31:01 +0530553 /* State should be equivalent to START */
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100554 if (!policy_dbs->policy)
Viresh Kumara72c4952015-07-18 11:31:01 +0530555 return -EBUSY;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530556
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100557 mutex_lock(&policy_dbs->timer_mutex);
558 if (policy->max < policy->cur)
559 __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
560 else if (policy->min > policy->cur)
561 __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +0100562 dbs_check_cpu(policy);
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100563 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumara72c4952015-07-18 11:31:01 +0530564
565 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530566}
567
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100568int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000569{
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100570 int ret = -EINVAL;
Viresh Kumar4471a342012-10-26 00:47:42 +0200571
Viresh Kumar732b6d62015-06-03 15:57:13 +0530572 /* Lock governor to block concurrent initialization of governor */
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100573 mutex_lock(&dbs_data_mutex);
Viresh Kumar732b6d62015-06-03 15:57:13 +0530574
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100575 if (event == CPUFREQ_GOV_POLICY_INIT) {
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100576 ret = cpufreq_governor_init(policy);
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100577 } else if (policy->governor_data) {
578 switch (event) {
579 case CPUFREQ_GOV_POLICY_EXIT:
580 ret = cpufreq_governor_exit(policy);
581 break;
582 case CPUFREQ_GOV_START:
583 ret = cpufreq_governor_start(policy);
584 break;
585 case CPUFREQ_GOV_STOP:
586 ret = cpufreq_governor_stop(policy);
587 break;
588 case CPUFREQ_GOV_LIMITS:
589 ret = cpufreq_governor_limits(policy);
590 break;
591 }
Viresh Kumar732b6d62015-06-03 15:57:13 +0530592 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000593
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100594 mutex_unlock(&dbs_data_mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530595 return ret;
Viresh Kumar4471a342012-10-26 00:47:42 +0200596}
597EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);