viresh kumar | 2aacdff | 2012-10-23 01:28:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/cpufreq/cpufreq_governor.c |
| 3 | * |
| 4 | * CPUFREQ governors common code |
| 5 | * |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 6 | * 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 kumar | 2aacdff | 2012-10-23 01:28:05 +0200 | [diff] [blame] | 12 | * 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 Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 18 | |
viresh kumar | 2aacdff | 2012-10-23 01:28:05 +0200 | [diff] [blame] | 19 | #include <linux/export.h> |
| 20 | #include <linux/kernel_stat.h> |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 21 | #include <linux/slab.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 22 | |
| 23 | #include "cpufreq_governor.h" |
| 24 | |
Rafael J. Wysocki | 2bb8d94 | 2016-02-07 16:01:31 +0100 | [diff] [blame] | 25 | DEFINE_MUTEX(dbs_data_mutex); |
| 26 | EXPORT_SYMBOL_GPL(dbs_data_mutex); |
| 27 | |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 28 | static struct attribute_group *get_sysfs_attr(struct dbs_governor *gov) |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 29 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 30 | return have_governor_per_policy() ? |
| 31 | gov->attr_group_gov_pol : gov->attr_group_gov_sys; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 34 | void dbs_check_cpu(struct cpufreq_policy *policy, int cpu) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 35 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 36 | struct dbs_governor *gov = dbs_governor_of(policy); |
| 37 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu); |
| 38 | struct dbs_data *dbs_data = policy->governor_data; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 39 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
| 40 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 41 | unsigned int sampling_rate; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 42 | unsigned int max_load = 0; |
| 43 | unsigned int ignore_nice; |
| 44 | unsigned int j; |
| 45 | |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 46 | if (gov->governor == GOV_ONDEMAND) { |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 47 | struct od_cpu_dbs_info_s *od_dbs_info = |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 48 | gov->get_cpu_dbs_info_s(cpu); |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Sometimes, the ondemand governor uses an additional |
| 52 | * multiplier to give long delays. So apply this multiplier to |
| 53 | * the 'sampling_rate', so as to keep the wake-up-from-idle |
| 54 | * detection logic a bit conservative. |
| 55 | */ |
| 56 | sampling_rate = od_tuners->sampling_rate; |
| 57 | sampling_rate *= od_dbs_info->rate_mult; |
| 58 | |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 59 | ignore_nice = od_tuners->ignore_nice_load; |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 60 | } else { |
| 61 | sampling_rate = cs_tuners->sampling_rate; |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 62 | ignore_nice = cs_tuners->ignore_nice_load; |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 63 | } |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 64 | |
Stratos Karafotis | dfa5bb6 | 2013-06-05 19:01:25 +0300 | [diff] [blame] | 65 | /* Get Absolute Load */ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 66 | for_each_cpu(j, policy->cpus) { |
Viresh Kumar | 875b850 | 2015-06-19 17:18:03 +0530 | [diff] [blame] | 67 | struct cpu_dbs_info *j_cdbs; |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 68 | u64 cur_wall_time, cur_idle_time; |
| 69 | unsigned int idle_time, wall_time; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 70 | unsigned int load; |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 71 | int io_busy = 0; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 72 | |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 73 | j_cdbs = gov->get_cpu_cdbs(j); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 74 | |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 75 | /* |
| 76 | * For the purpose of ondemand, waiting for disk IO is |
| 77 | * an indication that you're performance critical, and |
| 78 | * not that the system is actually idle. So do not add |
| 79 | * the iowait time to the cpu idle time. |
| 80 | */ |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 81 | if (gov->governor == GOV_ONDEMAND) |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 82 | io_busy = od_tuners->io_is_busy; |
| 83 | cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 84 | |
| 85 | wall_time = (unsigned int) |
| 86 | (cur_wall_time - j_cdbs->prev_cpu_wall); |
| 87 | j_cdbs->prev_cpu_wall = cur_wall_time; |
| 88 | |
Chen Yu | 0df3502 | 2015-12-16 12:20:29 +0800 | [diff] [blame] | 89 | if (cur_idle_time < j_cdbs->prev_cpu_idle) |
| 90 | cur_idle_time = j_cdbs->prev_cpu_idle; |
| 91 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 92 | idle_time = (unsigned int) |
| 93 | (cur_idle_time - j_cdbs->prev_cpu_idle); |
| 94 | j_cdbs->prev_cpu_idle = cur_idle_time; |
| 95 | |
| 96 | if (ignore_nice) { |
| 97 | u64 cur_nice; |
| 98 | unsigned long cur_nice_jiffies; |
| 99 | |
| 100 | cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] - |
| 101 | cdbs->prev_cpu_nice; |
| 102 | /* |
| 103 | * Assumption: nice time between sampling periods will |
| 104 | * be less than 2^32 jiffies for 32 bit sys |
| 105 | */ |
| 106 | cur_nice_jiffies = (unsigned long) |
| 107 | cputime64_to_jiffies64(cur_nice); |
| 108 | |
| 109 | cdbs->prev_cpu_nice = |
| 110 | kcpustat_cpu(j).cpustat[CPUTIME_NICE]; |
| 111 | idle_time += jiffies_to_usecs(cur_nice_jiffies); |
| 112 | } |
| 113 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 114 | if (unlikely(!wall_time || wall_time < idle_time)) |
| 115 | continue; |
| 116 | |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 117 | /* |
| 118 | * If the CPU had gone completely idle, and a task just woke up |
| 119 | * on this CPU now, it would be unfair to calculate 'load' the |
| 120 | * usual way for this elapsed time-window, because it will show |
| 121 | * near-zero load, irrespective of how CPU intensive that task |
| 122 | * actually is. This is undesirable for latency-sensitive bursty |
| 123 | * workloads. |
| 124 | * |
| 125 | * To avoid this, we reuse the 'load' from the previous |
| 126 | * time-window and give this task a chance to start with a |
| 127 | * reasonably high CPU frequency. (However, we shouldn't over-do |
| 128 | * this copy, lest we get stuck at a high load (high frequency) |
| 129 | * for too long, even when the current system load has actually |
| 130 | * dropped down. So we perform the copy only once, upon the |
| 131 | * first wake-up from idle.) |
| 132 | * |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 133 | * Detecting this situation is easy: the governor's utilization |
| 134 | * update handler would not have run during CPU-idle periods. |
| 135 | * Hence, an unusually large 'wall_time' (as compared to the |
| 136 | * sampling rate) indicates this scenario. |
Viresh Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 137 | * |
| 138 | * prev_load can be zero in two cases and we must recalculate it |
| 139 | * for both cases: |
| 140 | * - during long idle intervals |
| 141 | * - explicitly set to zero |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 142 | */ |
Viresh Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 143 | if (unlikely(wall_time > (2 * sampling_rate) && |
| 144 | j_cdbs->prev_load)) { |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 145 | load = j_cdbs->prev_load; |
Viresh Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * Perform a destructive copy, to ensure that we copy |
| 149 | * the previous load only once, upon the first wake-up |
| 150 | * from idle. |
| 151 | */ |
| 152 | j_cdbs->prev_load = 0; |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 153 | } else { |
| 154 | load = 100 * (wall_time - idle_time) / wall_time; |
| 155 | j_cdbs->prev_load = load; |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 156 | } |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 157 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 158 | if (load > max_load) |
| 159 | max_load = load; |
| 160 | } |
| 161 | |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 162 | gov->gov_check_cpu(cpu, max_load); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 163 | } |
| 164 | EXPORT_SYMBOL_GPL(dbs_check_cpu); |
| 165 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 166 | void gov_set_update_util(struct cpu_common_dbs_info *shared, |
| 167 | unsigned int delay_us) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 168 | { |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 169 | struct cpufreq_policy *policy = shared->policy; |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 170 | struct dbs_governor *gov = dbs_governor_of(policy); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 171 | int cpu; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 172 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 173 | gov_update_sample_delay(shared, delay_us); |
| 174 | shared->last_sample_time = 0; |
| 175 | |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 176 | for_each_cpu(cpu, policy->cpus) { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 177 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu); |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 178 | |
| 179 | cpufreq_set_update_util_data(cpu, &cdbs->update_util); |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 180 | } |
| 181 | } |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 182 | EXPORT_SYMBOL_GPL(gov_set_update_util); |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 183 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 184 | static inline void gov_clear_update_util(struct cpufreq_policy *policy) |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 185 | { |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 186 | int i; |
| 187 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 188 | for_each_cpu(i, policy->cpus) |
| 189 | cpufreq_set_update_util_data(i, NULL); |
| 190 | |
| 191 | synchronize_rcu(); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 194 | static void gov_cancel_work(struct cpu_common_dbs_info *shared) |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 195 | { |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 196 | /* Tell dbs_update_util_handler() to skip queuing up work items. */ |
Rafael J. Wysocki | 2dd3e72 | 2015-12-08 21:44:05 +0100 | [diff] [blame] | 197 | atomic_inc(&shared->skip_work); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 198 | /* |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 199 | * If dbs_update_util_handler() is already running, it may not notice |
| 200 | * the incremented skip_work, so wait for it to complete to prevent its |
| 201 | * work item from being queued up after the cancel_work_sync() below. |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 202 | */ |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 203 | gov_clear_update_util(shared->policy); |
| 204 | irq_work_sync(&shared->irq_work); |
Rafael J. Wysocki | 2dd3e72 | 2015-12-08 21:44:05 +0100 | [diff] [blame] | 205 | cancel_work_sync(&shared->work); |
Rafael J. Wysocki | 2dd3e72 | 2015-12-08 21:44:05 +0100 | [diff] [blame] | 206 | atomic_set(&shared->skip_work, 0); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 207 | } |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 208 | |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 209 | static void dbs_work_handler(struct work_struct *work) |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 210 | { |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 211 | struct cpu_common_dbs_info *shared = container_of(work, struct |
| 212 | cpu_common_dbs_info, work); |
Viresh Kumar | 3a91b069 | 2015-10-29 08:08:38 +0530 | [diff] [blame] | 213 | struct cpufreq_policy *policy; |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 214 | struct dbs_governor *gov; |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 215 | unsigned int delay; |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 216 | |
Viresh Kumar | 3a91b069 | 2015-10-29 08:08:38 +0530 | [diff] [blame] | 217 | policy = shared->policy; |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 218 | gov = dbs_governor_of(policy); |
Viresh Kumar | 3a91b069 | 2015-10-29 08:08:38 +0530 | [diff] [blame] | 219 | |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 220 | /* |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 221 | * Make sure cpufreq_governor_limits() isn't evaluating load or the |
| 222 | * ondemand governor isn't updating the sampling rate in parallel. |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 223 | */ |
| 224 | mutex_lock(&shared->timer_mutex); |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 225 | delay = gov->gov_dbs_timer(policy); |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 226 | shared->sample_delay_ns = jiffies_to_nsecs(delay); |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 227 | mutex_unlock(&shared->timer_mutex); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 228 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 229 | /* |
| 230 | * If the atomic operation below is reordered with respect to the |
| 231 | * sample delay modification, the utilization update handler may end |
| 232 | * up using a stale sample delay value. |
| 233 | */ |
| 234 | smp_mb__before_atomic(); |
Rafael J. Wysocki | 2dd3e72 | 2015-12-08 21:44:05 +0100 | [diff] [blame] | 235 | atomic_dec(&shared->skip_work); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 236 | } |
| 237 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 238 | static void dbs_irq_work(struct irq_work *irq_work) |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 239 | { |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 240 | struct cpu_common_dbs_info *shared; |
| 241 | |
| 242 | shared = container_of(irq_work, struct cpu_common_dbs_info, irq_work); |
| 243 | schedule_work(&shared->work); |
| 244 | } |
| 245 | |
| 246 | static inline void gov_queue_irq_work(struct cpu_common_dbs_info *shared) |
| 247 | { |
| 248 | #ifdef CONFIG_SMP |
| 249 | irq_work_queue_on(&shared->irq_work, smp_processor_id()); |
| 250 | #else |
| 251 | irq_work_queue(&shared->irq_work); |
| 252 | #endif |
| 253 | } |
| 254 | |
| 255 | static void dbs_update_util_handler(struct update_util_data *data, u64 time, |
| 256 | unsigned long util, unsigned long max) |
| 257 | { |
| 258 | struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 259 | struct cpu_common_dbs_info *shared = cdbs->shared; |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 260 | |
| 261 | /* |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 262 | * The work may not be allowed to be queued up right now. |
| 263 | * Possible reasons: |
| 264 | * - Work has already been queued up or is in progress. |
| 265 | * - The governor is being stopped. |
| 266 | * - It is too early (too little time from the previous sample). |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 267 | */ |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 268 | if (atomic_inc_return(&shared->skip_work) == 1) { |
| 269 | u64 delta_ns; |
| 270 | |
| 271 | delta_ns = time - shared->last_sample_time; |
| 272 | if ((s64)delta_ns >= shared->sample_delay_ns) { |
| 273 | shared->last_sample_time = time; |
| 274 | gov_queue_irq_work(shared); |
| 275 | return; |
| 276 | } |
| 277 | } |
| 278 | atomic_dec(&shared->skip_work); |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 279 | } |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 280 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 281 | static void set_sampling_rate(struct dbs_data *dbs_data, |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 282 | struct dbs_governor *gov, |
| 283 | unsigned int sampling_rate) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 284 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 285 | if (gov->governor == GOV_CONSERVATIVE) { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 286 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; |
| 287 | cs_tuners->sampling_rate = sampling_rate; |
| 288 | } else { |
| 289 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
| 290 | od_tuners->sampling_rate = sampling_rate; |
| 291 | } |
| 292 | } |
| 293 | |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 294 | static int alloc_common_dbs_info(struct cpufreq_policy *policy, |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 295 | struct dbs_governor *gov) |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 296 | { |
| 297 | struct cpu_common_dbs_info *shared; |
| 298 | int j; |
| 299 | |
| 300 | /* Allocate memory for the common information for policy->cpus */ |
| 301 | shared = kzalloc(sizeof(*shared), GFP_KERNEL); |
| 302 | if (!shared) |
| 303 | return -ENOMEM; |
| 304 | |
| 305 | /* Set shared for all CPUs, online+offline */ |
| 306 | for_each_cpu(j, policy->related_cpus) |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 307 | gov->get_cpu_cdbs(j)->shared = shared; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 308 | |
Viresh Kumar | 5e4500d | 2015-12-03 09:37:52 +0530 | [diff] [blame] | 309 | mutex_init(&shared->timer_mutex); |
Rafael J. Wysocki | 2dd3e72 | 2015-12-08 21:44:05 +0100 | [diff] [blame] | 310 | atomic_set(&shared->skip_work, 0); |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 311 | init_irq_work(&shared->irq_work, dbs_irq_work); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 312 | INIT_WORK(&shared->work, dbs_work_handler); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static void free_common_dbs_info(struct cpufreq_policy *policy, |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 317 | struct dbs_governor *gov) |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 318 | { |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 319 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 320 | struct cpu_common_dbs_info *shared = cdbs->shared; |
| 321 | int j; |
| 322 | |
Viresh Kumar | 5e4500d | 2015-12-03 09:37:52 +0530 | [diff] [blame] | 323 | mutex_destroy(&shared->timer_mutex); |
| 324 | |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 325 | for_each_cpu(j, policy->cpus) |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 326 | gov->get_cpu_cdbs(j)->shared = NULL; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 327 | |
| 328 | kfree(shared); |
| 329 | } |
| 330 | |
Rafael J. Wysocki | 906a6e5 | 2016-02-07 16:07:51 +0100 | [diff] [blame] | 331 | static int cpufreq_governor_init(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 332 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 333 | struct dbs_governor *gov = dbs_governor_of(policy); |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 334 | struct dbs_data *dbs_data = gov->gdbs_data; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 335 | unsigned int latency; |
| 336 | int ret; |
| 337 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 338 | /* State should be equivalent to EXIT */ |
| 339 | if (policy->governor_data) |
| 340 | return -EBUSY; |
| 341 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 342 | if (dbs_data) { |
| 343 | if (WARN_ON(have_governor_per_policy())) |
| 344 | return -EINVAL; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 345 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 346 | ret = alloc_common_dbs_info(policy, gov); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 347 | if (ret) |
| 348 | return ret; |
| 349 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 350 | dbs_data->usage_count++; |
| 351 | policy->governor_data = dbs_data; |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL); |
| 356 | if (!dbs_data) |
| 357 | return -ENOMEM; |
| 358 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 359 | ret = alloc_common_dbs_info(policy, gov); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 360 | if (ret) |
| 361 | goto free_dbs_data; |
| 362 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 363 | dbs_data->usage_count = 1; |
| 364 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 365 | ret = gov->init(dbs_data, !policy->governor->initialized); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 366 | if (ret) |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 367 | goto free_common_dbs_info; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 368 | |
| 369 | /* policy latency is in ns. Convert it to us first */ |
| 370 | latency = policy->cpuinfo.transition_latency / 1000; |
| 371 | if (latency == 0) |
| 372 | latency = 1; |
| 373 | |
| 374 | /* Bring kernel and HW constraints together */ |
| 375 | dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate, |
| 376 | MIN_LATENCY_MULTIPLIER * latency); |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 377 | set_sampling_rate(dbs_data, gov, max(dbs_data->min_sampling_rate, |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 378 | latency * LATENCY_MULTIPLIER)); |
| 379 | |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 380 | if (!have_governor_per_policy()) |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 381 | gov->gdbs_data = dbs_data; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 382 | |
Viresh Kumar | e4b133c | 2016-01-25 22:33:46 +0530 | [diff] [blame] | 383 | policy->governor_data = dbs_data; |
| 384 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 385 | ret = sysfs_create_group(get_governor_parent_kobj(policy), |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 386 | get_sysfs_attr(gov)); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 387 | if (ret) |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 388 | goto reset_gdbs_data; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 389 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 390 | return 0; |
| 391 | |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 392 | reset_gdbs_data: |
Viresh Kumar | e4b133c | 2016-01-25 22:33:46 +0530 | [diff] [blame] | 393 | policy->governor_data = NULL; |
| 394 | |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 395 | if (!have_governor_per_policy()) |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 396 | gov->gdbs_data = NULL; |
| 397 | gov->exit(dbs_data, !policy->governor->initialized); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 398 | free_common_dbs_info: |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 399 | free_common_dbs_info(policy, gov); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 400 | free_dbs_data: |
| 401 | kfree(dbs_data); |
| 402 | return ret; |
| 403 | } |
| 404 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 405 | static int cpufreq_governor_exit(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 406 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 407 | struct dbs_governor *gov = dbs_governor_of(policy); |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 408 | struct dbs_data *dbs_data = policy->governor_data; |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 409 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu); |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 410 | |
| 411 | /* State should be equivalent to INIT */ |
| 412 | if (!cdbs->shared || cdbs->shared->policy) |
| 413 | return -EBUSY; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 414 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 415 | if (!--dbs_data->usage_count) { |
| 416 | sysfs_remove_group(get_governor_parent_kobj(policy), |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 417 | get_sysfs_attr(gov)); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 418 | |
Viresh Kumar | e4b133c | 2016-01-25 22:33:46 +0530 | [diff] [blame] | 419 | policy->governor_data = NULL; |
| 420 | |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 421 | if (!have_governor_per_policy()) |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 422 | gov->gdbs_data = NULL; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 423 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 424 | gov->exit(dbs_data, policy->governor->initialized == 1); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 425 | kfree(dbs_data); |
Viresh Kumar | e4b133c | 2016-01-25 22:33:46 +0530 | [diff] [blame] | 426 | } else { |
| 427 | policy->governor_data = NULL; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 428 | } |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 429 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 430 | free_common_dbs_info(policy, gov); |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 431 | return 0; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 432 | } |
| 433 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 434 | static int cpufreq_governor_start(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 435 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 436 | struct dbs_governor *gov = dbs_governor_of(policy); |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 437 | struct dbs_data *dbs_data = policy->governor_data; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 438 | unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu; |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 439 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 440 | struct cpu_common_dbs_info *shared = cdbs->shared; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 441 | int io_busy = 0; |
| 442 | |
| 443 | if (!policy->cur) |
| 444 | return -EINVAL; |
| 445 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 446 | /* State should be equivalent to INIT */ |
| 447 | if (!shared || shared->policy) |
| 448 | return -EBUSY; |
| 449 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 450 | if (gov->governor == GOV_CONSERVATIVE) { |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 451 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; |
| 452 | |
| 453 | sampling_rate = cs_tuners->sampling_rate; |
| 454 | ignore_nice = cs_tuners->ignore_nice_load; |
| 455 | } else { |
| 456 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
| 457 | |
| 458 | sampling_rate = od_tuners->sampling_rate; |
| 459 | ignore_nice = od_tuners->ignore_nice_load; |
| 460 | io_busy = od_tuners->io_is_busy; |
| 461 | } |
| 462 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 463 | for_each_cpu(j, policy->cpus) { |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 464 | struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 465 | unsigned int prev_load; |
| 466 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 467 | j_cdbs->prev_cpu_idle = |
| 468 | get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy); |
| 469 | |
| 470 | prev_load = (unsigned int)(j_cdbs->prev_cpu_wall - |
| 471 | j_cdbs->prev_cpu_idle); |
| 472 | j_cdbs->prev_load = 100 * prev_load / |
| 473 | (unsigned int)j_cdbs->prev_cpu_wall; |
| 474 | |
| 475 | if (ignore_nice) |
| 476 | j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE]; |
| 477 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 478 | j_cdbs->update_util.func = dbs_update_util_handler; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 479 | } |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 480 | shared->policy = policy; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 481 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 482 | if (gov->governor == GOV_CONSERVATIVE) { |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 483 | struct cs_cpu_dbs_info_s *cs_dbs_info = |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 484 | gov->get_cpu_dbs_info_s(cpu); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 485 | |
| 486 | cs_dbs_info->down_skip = 0; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 487 | cs_dbs_info->requested_freq = policy->cur; |
| 488 | } else { |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 489 | struct od_ops *od_ops = gov->gov_ops; |
| 490 | struct od_cpu_dbs_info_s *od_dbs_info = gov->get_cpu_dbs_info_s(cpu); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 491 | |
| 492 | od_dbs_info->rate_mult = 1; |
| 493 | od_dbs_info->sample_type = OD_NORMAL_SAMPLE; |
| 494 | od_ops->powersave_bias_init_cpu(cpu); |
| 495 | } |
| 496 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 497 | gov_set_update_util(shared, sampling_rate); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 501 | static int cpufreq_governor_stop(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 502 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 503 | struct dbs_governor *gov = dbs_governor_of(policy); |
| 504 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 505 | struct cpu_common_dbs_info *shared = cdbs->shared; |
| 506 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 507 | /* State should be equivalent to START */ |
| 508 | if (!shared || !shared->policy) |
| 509 | return -EBUSY; |
| 510 | |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 511 | gov_cancel_work(shared); |
Viresh Kumar | 3a91b069 | 2015-10-29 08:08:38 +0530 | [diff] [blame] | 512 | shared->policy = NULL; |
Viresh Kumar | 3a91b069 | 2015-10-29 08:08:38 +0530 | [diff] [blame] | 513 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 514 | return 0; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 515 | } |
| 516 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 517 | static int cpufreq_governor_limits(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 518 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 519 | struct dbs_governor *gov = dbs_governor_of(policy); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 520 | unsigned int cpu = policy->cpu; |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 521 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 522 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 523 | /* State should be equivalent to START */ |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 524 | if (!cdbs->shared || !cdbs->shared->policy) |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 525 | return -EBUSY; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 526 | |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 527 | mutex_lock(&cdbs->shared->timer_mutex); |
| 528 | if (policy->max < cdbs->shared->policy->cur) |
| 529 | __cpufreq_driver_target(cdbs->shared->policy, policy->max, |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 530 | CPUFREQ_RELATION_H); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 531 | else if (policy->min > cdbs->shared->policy->cur) |
| 532 | __cpufreq_driver_target(cdbs->shared->policy, policy->min, |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 533 | CPUFREQ_RELATION_L); |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame^] | 534 | dbs_check_cpu(policy, cpu); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 535 | mutex_unlock(&cdbs->shared->timer_mutex); |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 536 | |
| 537 | return 0; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 538 | } |
| 539 | |
Rafael J. Wysocki | 906a6e5 | 2016-02-07 16:07:51 +0100 | [diff] [blame] | 540 | int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event) |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 541 | { |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 542 | int ret = -EINVAL; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 543 | |
Viresh Kumar | 732b6d6 | 2015-06-03 15:57:13 +0530 | [diff] [blame] | 544 | /* Lock governor to block concurrent initialization of governor */ |
Rafael J. Wysocki | 2bb8d94 | 2016-02-07 16:01:31 +0100 | [diff] [blame] | 545 | mutex_lock(&dbs_data_mutex); |
Viresh Kumar | 732b6d6 | 2015-06-03 15:57:13 +0530 | [diff] [blame] | 546 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 547 | if (event == CPUFREQ_GOV_POLICY_INIT) { |
Rafael J. Wysocki | 906a6e5 | 2016-02-07 16:07:51 +0100 | [diff] [blame] | 548 | ret = cpufreq_governor_init(policy); |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 549 | } else if (policy->governor_data) { |
| 550 | switch (event) { |
| 551 | case CPUFREQ_GOV_POLICY_EXIT: |
| 552 | ret = cpufreq_governor_exit(policy); |
| 553 | break; |
| 554 | case CPUFREQ_GOV_START: |
| 555 | ret = cpufreq_governor_start(policy); |
| 556 | break; |
| 557 | case CPUFREQ_GOV_STOP: |
| 558 | ret = cpufreq_governor_stop(policy); |
| 559 | break; |
| 560 | case CPUFREQ_GOV_LIMITS: |
| 561 | ret = cpufreq_governor_limits(policy); |
| 562 | break; |
| 563 | } |
Viresh Kumar | 732b6d6 | 2015-06-03 15:57:13 +0530 | [diff] [blame] | 564 | } |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 565 | |
Rafael J. Wysocki | 2bb8d94 | 2016-02-07 16:01:31 +0100 | [diff] [blame] | 566 | mutex_unlock(&dbs_data_mutex); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 567 | return ret; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 568 | } |
| 569 | EXPORT_SYMBOL_GPL(cpufreq_governor_dbs); |