blob: 82e50dcf9febade3b2f8c0a14321be51b90cd607 [file] [log] [blame]
viresh kumar2aacdff2012-10-23 01:28:05 +02001/*
2 * drivers/cpufreq/cpufreq_governor.c
3 *
4 * CPUFREQ governors common code
5 *
Viresh Kumar4471a342012-10-26 00:47:42 +02006 * Copyright (C) 2001 Russell King
7 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
8 * (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
9 * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
10 * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
11 *
viresh kumar2aacdff2012-10-23 01:28:05 +020012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Viresh Kumar4471a342012-10-26 00:47:42 +020017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
viresh kumar2aacdff2012-10-23 01:28:05 +020019#include <linux/export.h>
20#include <linux/kernel_stat.h>
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000021#include <linux/slab.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020022
23#include "cpufreq_governor.h"
24
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +010025DEFINE_MUTEX(dbs_data_mutex);
26EXPORT_SYMBOL_GPL(dbs_data_mutex);
27
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010028static struct attribute_group *get_sysfs_attr(struct dbs_governor *gov)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000029{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010030 return have_governor_per_policy() ?
31 gov->attr_group_gov_pol : gov->attr_group_gov_sys;
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000032}
33
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +010034void dbs_check_cpu(struct cpufreq_policy *policy)
Viresh Kumar4471a342012-10-26 00:47:42 +020035{
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +010036 int cpu = policy->cpu;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010037 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +010038 struct policy_dbs_info *policy_dbs = policy->governor_data;
39 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar4471a342012-10-26 00:47:42 +020040 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
41 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053042 unsigned int sampling_rate;
Viresh Kumar4471a342012-10-26 00:47:42 +020043 unsigned int max_load = 0;
44 unsigned int ignore_nice;
45 unsigned int j;
46
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010047 if (gov->governor == GOV_ONDEMAND) {
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053048 struct od_cpu_dbs_info_s *od_dbs_info =
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010049 gov->get_cpu_dbs_info_s(cpu);
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053050
51 /*
52 * Sometimes, the ondemand governor uses an additional
53 * multiplier to give long delays. So apply this multiplier to
54 * the 'sampling_rate', so as to keep the wake-up-from-idle
55 * detection logic a bit conservative.
56 */
57 sampling_rate = od_tuners->sampling_rate;
58 sampling_rate *= od_dbs_info->rate_mult;
59
Viresh Kumar6c4640c2013-08-05 12:28:02 +053060 ignore_nice = od_tuners->ignore_nice_load;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053061 } else {
62 sampling_rate = cs_tuners->sampling_rate;
Viresh Kumar6c4640c2013-08-05 12:28:02 +053063 ignore_nice = cs_tuners->ignore_nice_load;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +053064 }
Viresh Kumar4471a342012-10-26 00:47:42 +020065
Stratos Karafotisdfa5bb62013-06-05 19:01:25 +030066 /* Get Absolute Load */
Viresh Kumar4471a342012-10-26 00:47:42 +020067 for_each_cpu(j, policy->cpus) {
Viresh Kumar875b8502015-06-19 17:18:03 +053068 struct cpu_dbs_info *j_cdbs;
Stratos Karafotis9366d842013-02-28 16:57:32 +000069 u64 cur_wall_time, cur_idle_time;
70 unsigned int idle_time, wall_time;
Viresh Kumar4471a342012-10-26 00:47:42 +020071 unsigned int load;
Stratos Karafotis9366d842013-02-28 16:57:32 +000072 int io_busy = 0;
Viresh Kumar4471a342012-10-26 00:47:42 +020073
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010074 j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar4471a342012-10-26 00:47:42 +020075
Stratos Karafotis9366d842013-02-28 16:57:32 +000076 /*
77 * For the purpose of ondemand, waiting for disk IO is
78 * an indication that you're performance critical, and
79 * not that the system is actually idle. So do not add
80 * the iowait time to the cpu idle time.
81 */
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +010082 if (gov->governor == GOV_ONDEMAND)
Stratos Karafotis9366d842013-02-28 16:57:32 +000083 io_busy = od_tuners->io_is_busy;
84 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
Viresh Kumar4471a342012-10-26 00:47:42 +020085
86 wall_time = (unsigned int)
87 (cur_wall_time - j_cdbs->prev_cpu_wall);
88 j_cdbs->prev_cpu_wall = cur_wall_time;
89
Chen Yu0df35022015-12-16 12:20:29 +080090 if (cur_idle_time < j_cdbs->prev_cpu_idle)
91 cur_idle_time = j_cdbs->prev_cpu_idle;
92
Viresh Kumar4471a342012-10-26 00:47:42 +020093 idle_time = (unsigned int)
94 (cur_idle_time - j_cdbs->prev_cpu_idle);
95 j_cdbs->prev_cpu_idle = cur_idle_time;
96
97 if (ignore_nice) {
Rafael J. Wysockibc505472016-02-07 16:24:26 +010098 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
Viresh Kumar4471a342012-10-26 00:47:42 +020099 u64 cur_nice;
100 unsigned long cur_nice_jiffies;
101
102 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
103 cdbs->prev_cpu_nice;
104 /*
105 * Assumption: nice time between sampling periods will
106 * be less than 2^32 jiffies for 32 bit sys
107 */
108 cur_nice_jiffies = (unsigned long)
109 cputime64_to_jiffies64(cur_nice);
110
111 cdbs->prev_cpu_nice =
112 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
113 idle_time += jiffies_to_usecs(cur_nice_jiffies);
114 }
115
Viresh Kumar4471a342012-10-26 00:47:42 +0200116 if (unlikely(!wall_time || wall_time < idle_time))
117 continue;
118
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530119 /*
120 * If the CPU had gone completely idle, and a task just woke up
121 * on this CPU now, it would be unfair to calculate 'load' the
122 * usual way for this elapsed time-window, because it will show
123 * near-zero load, irrespective of how CPU intensive that task
124 * actually is. This is undesirable for latency-sensitive bursty
125 * workloads.
126 *
127 * To avoid this, we reuse the 'load' from the previous
128 * time-window and give this task a chance to start with a
129 * reasonably high CPU frequency. (However, we shouldn't over-do
130 * this copy, lest we get stuck at a high load (high frequency)
131 * for too long, even when the current system load has actually
132 * dropped down. So we perform the copy only once, upon the
133 * first wake-up from idle.)
134 *
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100135 * Detecting this situation is easy: the governor's utilization
136 * update handler would not have run during CPU-idle periods.
137 * Hence, an unusually large 'wall_time' (as compared to the
138 * sampling rate) indicates this scenario.
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530139 *
140 * prev_load can be zero in two cases and we must recalculate it
141 * for both cases:
142 * - during long idle intervals
143 * - explicitly set to zero
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530144 */
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530145 if (unlikely(wall_time > (2 * sampling_rate) &&
146 j_cdbs->prev_load)) {
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530147 load = j_cdbs->prev_load;
Viresh Kumarc8ae4812014-06-09 14:21:24 +0530148
149 /*
150 * Perform a destructive copy, to ensure that we copy
151 * the previous load only once, upon the first wake-up
152 * from idle.
153 */
154 j_cdbs->prev_load = 0;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530155 } else {
156 load = 100 * (wall_time - idle_time) / wall_time;
157 j_cdbs->prev_load = load;
Srivatsa S. Bhat18b46ab2014-06-08 02:11:43 +0530158 }
Viresh Kumar4471a342012-10-26 00:47:42 +0200159
Viresh Kumar4471a342012-10-26 00:47:42 +0200160 if (load > max_load)
161 max_load = load;
162 }
163
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100164 gov->gov_check_cpu(cpu, max_load);
Viresh Kumar4471a342012-10-26 00:47:42 +0200165}
166EXPORT_SYMBOL_GPL(dbs_check_cpu);
167
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100168void gov_set_update_util(struct policy_dbs_info *policy_dbs,
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100169 unsigned int delay_us)
Viresh Kumar4471a342012-10-26 00:47:42 +0200170{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100171 struct cpufreq_policy *policy = policy_dbs->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100172 struct dbs_governor *gov = dbs_governor_of(policy);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530173 int cpu;
Viresh Kumar4471a342012-10-26 00:47:42 +0200174
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100175 gov_update_sample_delay(policy_dbs, delay_us);
176 policy_dbs->last_sample_time = 0;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100177
Viresh Kumar70f43e52015-12-09 07:34:42 +0530178 for_each_cpu(cpu, policy->cpus) {
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100179 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100180
181 cpufreq_set_update_util_data(cpu, &cdbs->update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530182 }
183}
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100184EXPORT_SYMBOL_GPL(gov_set_update_util);
Viresh Kumar031299b2013-02-27 12:24:03 +0530185
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100186static inline void gov_clear_update_util(struct cpufreq_policy *policy)
Viresh Kumar031299b2013-02-27 12:24:03 +0530187{
Viresh Kumar031299b2013-02-27 12:24:03 +0530188 int i;
189
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100190 for_each_cpu(i, policy->cpus)
191 cpufreq_set_update_util_data(i, NULL);
192
193 synchronize_rcu();
Viresh Kumar4471a342012-10-26 00:47:42 +0200194}
195
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100196static void gov_cancel_work(struct policy_dbs_info *policy_dbs)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530197{
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100198 /* Tell dbs_update_util_handler() to skip queuing up work items. */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100199 atomic_inc(&policy_dbs->skip_work);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530200 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100201 * If dbs_update_util_handler() is already running, it may not notice
202 * the incremented skip_work, so wait for it to complete to prevent its
203 * work item from being queued up after the cancel_work_sync() below.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530204 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100205 gov_clear_update_util(policy_dbs->policy);
206 irq_work_sync(&policy_dbs->irq_work);
207 cancel_work_sync(&policy_dbs->work);
208 atomic_set(&policy_dbs->skip_work, 0);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530209}
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530210
Viresh Kumar70f43e52015-12-09 07:34:42 +0530211static void dbs_work_handler(struct work_struct *work)
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530212{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100213 struct policy_dbs_info *policy_dbs;
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530214 struct cpufreq_policy *policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100215 struct dbs_governor *gov;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100216 unsigned int delay;
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530217
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100218 policy_dbs = container_of(work, struct policy_dbs_info, work);
219 policy = policy_dbs->policy;
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100220 gov = dbs_governor_of(policy);
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530221
Viresh Kumar70f43e52015-12-09 07:34:42 +0530222 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100223 * Make sure cpufreq_governor_limits() isn't evaluating load or the
224 * ondemand governor isn't updating the sampling rate in parallel.
Viresh Kumar70f43e52015-12-09 07:34:42 +0530225 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100226 mutex_lock(&policy_dbs->timer_mutex);
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100227 delay = gov->gov_dbs_timer(policy);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100228 policy_dbs->sample_delay_ns = jiffies_to_nsecs(delay);
229 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530230
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100231 /*
232 * If the atomic operation below is reordered with respect to the
233 * sample delay modification, the utilization update handler may end
234 * up using a stale sample delay value.
235 */
236 smp_mb__before_atomic();
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100237 atomic_dec(&policy_dbs->skip_work);
Viresh Kumar70f43e52015-12-09 07:34:42 +0530238}
239
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100240static void dbs_irq_work(struct irq_work *irq_work)
Viresh Kumar70f43e52015-12-09 07:34:42 +0530241{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100242 struct policy_dbs_info *policy_dbs;
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100243
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100244 policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work);
245 schedule_work(&policy_dbs->work);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100246}
247
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100248static inline void gov_queue_irq_work(struct policy_dbs_info *policy_dbs)
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100249{
250#ifdef CONFIG_SMP
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100251 irq_work_queue_on(&policy_dbs->irq_work, smp_processor_id());
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100252#else
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100253 irq_work_queue(&policy_dbs->irq_work);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100254#endif
255}
256
257static void dbs_update_util_handler(struct update_util_data *data, u64 time,
258 unsigned long util, unsigned long max)
259{
260 struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100261 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Viresh Kumar70f43e52015-12-09 07:34:42 +0530262
263 /*
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100264 * The work may not be allowed to be queued up right now.
265 * Possible reasons:
266 * - Work has already been queued up or is in progress.
267 * - The governor is being stopped.
268 * - It is too early (too little time from the previous sample).
Viresh Kumar70f43e52015-12-09 07:34:42 +0530269 */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100270 if (atomic_inc_return(&policy_dbs->skip_work) == 1) {
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100271 u64 delta_ns;
272
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100273 delta_ns = time - policy_dbs->last_sample_time;
274 if ((s64)delta_ns >= policy_dbs->sample_delay_ns) {
275 policy_dbs->last_sample_time = time;
276 gov_queue_irq_work(policy_dbs);
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100277 return;
278 }
279 }
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100280 atomic_dec(&policy_dbs->skip_work);
Viresh Kumar43e0ee32015-07-18 11:31:00 +0530281}
Viresh Kumar44472662013-01-31 17:28:02 +0000282
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000283static void set_sampling_rate(struct dbs_data *dbs_data,
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100284 struct dbs_governor *gov,
285 unsigned int sampling_rate)
Viresh Kumar4471a342012-10-26 00:47:42 +0200286{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100287 if (gov->governor == GOV_CONSERVATIVE) {
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000288 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
289 cs_tuners->sampling_rate = sampling_rate;
290 } else {
291 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
292 od_tuners->sampling_rate = sampling_rate;
293 }
294}
295
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100296static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy,
297 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530298{
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100299 struct policy_dbs_info *policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530300 int j;
301
302 /* Allocate memory for the common information for policy->cpus */
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100303 policy_dbs = kzalloc(sizeof(*policy_dbs), GFP_KERNEL);
304 if (!policy_dbs)
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100305 return NULL;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530306
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100307 /* Set policy_dbs for all CPUs, online+offline */
Viresh Kumar44152cb2015-07-18 11:30:59 +0530308 for_each_cpu(j, policy->related_cpus)
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100309 gov->get_cpu_cdbs(j)->policy_dbs = policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530310
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100311 mutex_init(&policy_dbs->timer_mutex);
312 atomic_set(&policy_dbs->skip_work, 0);
313 init_irq_work(&policy_dbs->irq_work, dbs_irq_work);
314 INIT_WORK(&policy_dbs->work, dbs_work_handler);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100315 return policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530316}
317
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100318static void free_policy_dbs_info(struct cpufreq_policy *policy,
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100319 struct dbs_governor *gov)
Viresh Kumar44152cb2015-07-18 11:30:59 +0530320{
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100321 struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu);
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100322 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530323 int j;
324
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100325 mutex_destroy(&policy_dbs->timer_mutex);
Viresh Kumar5e4500d2015-12-03 09:37:52 +0530326
Viresh Kumar44152cb2015-07-18 11:30:59 +0530327 for_each_cpu(j, policy->cpus)
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100328 gov->get_cpu_cdbs(j)->policy_dbs = NULL;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530329
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100330 kfree(policy_dbs);
Viresh Kumar44152cb2015-07-18 11:30:59 +0530331}
332
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100333static int cpufreq_governor_init(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530334{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100335 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100336 struct dbs_data *dbs_data = gov->gdbs_data;
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100337 struct policy_dbs_info *policy_dbs;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530338 unsigned int latency;
339 int ret;
340
Viresh Kumara72c4952015-07-18 11:31:01 +0530341 /* State should be equivalent to EXIT */
342 if (policy->governor_data)
343 return -EBUSY;
344
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100345 policy_dbs = alloc_policy_dbs_info(policy, gov);
346 if (!policy_dbs)
347 return -ENOMEM;
348
Viresh Kumar714a2d92015-06-04 16:43:27 +0530349 if (dbs_data) {
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100350 if (WARN_ON(have_governor_per_policy())) {
351 ret = -EINVAL;
352 goto free_policy_dbs_info;
353 }
Viresh Kumar714a2d92015-06-04 16:43:27 +0530354 dbs_data->usage_count++;
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100355 policy_dbs->dbs_data = dbs_data;
356 policy->governor_data = policy_dbs;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530357 return 0;
358 }
359
360 dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100361 if (!dbs_data) {
362 ret = -ENOMEM;
363 goto free_policy_dbs_info;
364 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530365
Viresh Kumar714a2d92015-06-04 16:43:27 +0530366 dbs_data->usage_count = 1;
367
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100368 ret = gov->init(dbs_data, !policy->governor->initialized);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530369 if (ret)
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100370 goto free_policy_dbs_info;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530371
372 /* policy latency is in ns. Convert it to us first */
373 latency = policy->cpuinfo.transition_latency / 1000;
374 if (latency == 0)
375 latency = 1;
376
377 /* Bring kernel and HW constraints together */
378 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
379 MIN_LATENCY_MULTIPLIER * latency);
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100380 set_sampling_rate(dbs_data, gov, max(dbs_data->min_sampling_rate,
Viresh Kumar714a2d92015-06-04 16:43:27 +0530381 latency * LATENCY_MULTIPLIER));
382
Viresh Kumar8eec1022015-10-15 21:35:22 +0530383 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100384 gov->gdbs_data = dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530385
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100386 policy_dbs->dbs_data = dbs_data;
387 policy->governor_data = policy_dbs;
Viresh Kumare4b133c2016-01-25 22:33:46 +0530388
Viresh Kumar714a2d92015-06-04 16:43:27 +0530389 ret = sysfs_create_group(get_governor_parent_kobj(policy),
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100390 get_sysfs_attr(gov));
Viresh Kumar714a2d92015-06-04 16:43:27 +0530391 if (ret)
Viresh Kumar8eec1022015-10-15 21:35:22 +0530392 goto reset_gdbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530393
Viresh Kumar714a2d92015-06-04 16:43:27 +0530394 return 0;
395
Viresh Kumar8eec1022015-10-15 21:35:22 +0530396reset_gdbs_data:
Viresh Kumare4b133c2016-01-25 22:33:46 +0530397 policy->governor_data = NULL;
398
Viresh Kumar8eec1022015-10-15 21:35:22 +0530399 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100400 gov->gdbs_data = NULL;
401 gov->exit(dbs_data, !policy->governor->initialized);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100402 kfree(dbs_data);
403
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100404free_policy_dbs_info:
405 free_policy_dbs_info(policy, gov);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530406 return ret;
407}
408
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100409static int cpufreq_governor_exit(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530410{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100411 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100412 struct policy_dbs_info *policy_dbs = policy->governor_data;
413 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumara72c4952015-07-18 11:31:01 +0530414
415 /* State should be equivalent to INIT */
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100416 if (policy_dbs->policy)
Viresh Kumara72c4952015-07-18 11:31:01 +0530417 return -EBUSY;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530418
Viresh Kumar714a2d92015-06-04 16:43:27 +0530419 if (!--dbs_data->usage_count) {
420 sysfs_remove_group(get_governor_parent_kobj(policy),
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100421 get_sysfs_attr(gov));
Viresh Kumar714a2d92015-06-04 16:43:27 +0530422
Viresh Kumare4b133c2016-01-25 22:33:46 +0530423 policy->governor_data = NULL;
424
Viresh Kumar8eec1022015-10-15 21:35:22 +0530425 if (!have_governor_per_policy())
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100426 gov->gdbs_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530427
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100428 gov->exit(dbs_data, policy->governor->initialized == 1);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530429 kfree(dbs_data);
Viresh Kumare4b133c2016-01-25 22:33:46 +0530430 } else {
431 policy->governor_data = NULL;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530432 }
Viresh Kumar44152cb2015-07-18 11:30:59 +0530433
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100434 free_policy_dbs_info(policy, gov);
Viresh Kumara72c4952015-07-18 11:31:01 +0530435 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530436}
437
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100438static int cpufreq_governor_start(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530439{
Rafael J. Wysockiea59ee0d2016-02-07 16:09:51 +0100440 struct dbs_governor *gov = dbs_governor_of(policy);
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100441 struct policy_dbs_info *policy_dbs = policy->governor_data;
442 struct dbs_data *dbs_data = policy_dbs->dbs_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530443 unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530444 int io_busy = 0;
445
446 if (!policy->cur)
447 return -EINVAL;
448
Viresh Kumara72c4952015-07-18 11:31:01 +0530449 /* State should be equivalent to INIT */
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100450 if (policy_dbs->policy)
Viresh Kumara72c4952015-07-18 11:31:01 +0530451 return -EBUSY;
452
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100453 if (gov->governor == GOV_CONSERVATIVE) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530454 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
455
456 sampling_rate = cs_tuners->sampling_rate;
457 ignore_nice = cs_tuners->ignore_nice_load;
458 } else {
459 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
460
461 sampling_rate = od_tuners->sampling_rate;
462 ignore_nice = od_tuners->ignore_nice_load;
463 io_busy = od_tuners->io_is_busy;
464 }
465
Viresh Kumar714a2d92015-06-04 16:43:27 +0530466 for_each_cpu(j, policy->cpus) {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100467 struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530468 unsigned int prev_load;
469
Viresh Kumar714a2d92015-06-04 16:43:27 +0530470 j_cdbs->prev_cpu_idle =
471 get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy);
472
473 prev_load = (unsigned int)(j_cdbs->prev_cpu_wall -
474 j_cdbs->prev_cpu_idle);
475 j_cdbs->prev_load = 100 * prev_load /
476 (unsigned int)j_cdbs->prev_cpu_wall;
477
478 if (ignore_nice)
479 j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
480
Rafael J. Wysocki9be4fd22016-02-10 16:53:50 +0100481 j_cdbs->update_util.func = dbs_update_util_handler;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530482 }
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100483 policy_dbs->policy = policy;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530484
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100485 if (gov->governor == GOV_CONSERVATIVE) {
Viresh Kumar714a2d92015-06-04 16:43:27 +0530486 struct cs_cpu_dbs_info_s *cs_dbs_info =
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100487 gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530488
489 cs_dbs_info->down_skip = 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530490 cs_dbs_info->requested_freq = policy->cur;
491 } else {
Rafael J. Wysocki7bdad342016-02-07 16:05:07 +0100492 struct od_ops *od_ops = gov->gov_ops;
493 struct od_cpu_dbs_info_s *od_dbs_info = gov->get_cpu_dbs_info_s(cpu);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530494
495 od_dbs_info->rate_mult = 1;
496 od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
497 od_ops->powersave_bias_init_cpu(cpu);
498 }
499
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100500 gov_set_update_util(policy_dbs, sampling_rate);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530501 return 0;
502}
503
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100504static int cpufreq_governor_stop(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530505{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100506 struct policy_dbs_info *policy_dbs = policy->governor_data;
Viresh Kumar44152cb2015-07-18 11:30:59 +0530507
Viresh Kumara72c4952015-07-18 11:31:01 +0530508 /* State should be equivalent to START */
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100509 if (!policy_dbs->policy)
Viresh Kumara72c4952015-07-18 11:31:01 +0530510 return -EBUSY;
511
Rafael J. Wysockie40e7b22016-02-10 17:07:44 +0100512 gov_cancel_work(policy_dbs);
513 policy_dbs->policy = NULL;
Viresh Kumar3a91b0692015-10-29 08:08:38 +0530514
Viresh Kumara72c4952015-07-18 11:31:01 +0530515 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530516}
517
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100518static int cpufreq_governor_limits(struct cpufreq_policy *policy)
Viresh Kumar714a2d92015-06-04 16:43:27 +0530519{
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100520 struct policy_dbs_info *policy_dbs = policy->governor_data;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530521
Viresh Kumara72c4952015-07-18 11:31:01 +0530522 /* State should be equivalent to START */
Rafael J. Wysockibc505472016-02-07 16:24:26 +0100523 if (!policy_dbs->policy)
Viresh Kumara72c4952015-07-18 11:31:01 +0530524 return -EBUSY;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530525
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100526 mutex_lock(&policy_dbs->timer_mutex);
527 if (policy->max < policy->cur)
528 __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
529 else if (policy->min > policy->cur)
530 __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
Rafael J. Wysockid10b5eb2016-02-06 13:50:24 +0100531 dbs_check_cpu(policy);
Rafael J. Wysockie9751892016-02-07 16:23:49 +0100532 mutex_unlock(&policy_dbs->timer_mutex);
Viresh Kumara72c4952015-07-18 11:31:01 +0530533
534 return 0;
Viresh Kumar714a2d92015-06-04 16:43:27 +0530535}
536
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100537int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event)
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000538{
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100539 int ret = -EINVAL;
Viresh Kumar4471a342012-10-26 00:47:42 +0200540
Viresh Kumar732b6d62015-06-03 15:57:13 +0530541 /* Lock governor to block concurrent initialization of governor */
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100542 mutex_lock(&dbs_data_mutex);
Viresh Kumar732b6d62015-06-03 15:57:13 +0530543
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100544 if (event == CPUFREQ_GOV_POLICY_INIT) {
Rafael J. Wysocki906a6e52016-02-07 16:07:51 +0100545 ret = cpufreq_governor_init(policy);
Rafael J. Wysocki5da3dd12016-02-05 03:15:24 +0100546 } else if (policy->governor_data) {
547 switch (event) {
548 case CPUFREQ_GOV_POLICY_EXIT:
549 ret = cpufreq_governor_exit(policy);
550 break;
551 case CPUFREQ_GOV_START:
552 ret = cpufreq_governor_start(policy);
553 break;
554 case CPUFREQ_GOV_STOP:
555 ret = cpufreq_governor_stop(policy);
556 break;
557 case CPUFREQ_GOV_LIMITS:
558 ret = cpufreq_governor_limits(policy);
559 break;
560 }
Viresh Kumar732b6d62015-06-03 15:57:13 +0530561 }
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000562
Rafael J. Wysocki2bb8d942016-02-07 16:01:31 +0100563 mutex_unlock(&dbs_data_mutex);
Viresh Kumar714a2d92015-06-04 16:43:27 +0530564 return ret;
Viresh Kumar4471a342012-10-26 00:47:42 +0200565}
566EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);