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 | |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame^] | 28 | static inline struct dbs_data *to_dbs_data(struct kobject *kobj) |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 29 | { |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame^] | 30 | return container_of(kobj, struct dbs_data, kobj); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame^] | 33 | static inline struct governor_attr *to_gov_attr(struct attribute *attr) |
| 34 | { |
| 35 | return container_of(attr, struct governor_attr, attr); |
| 36 | } |
| 37 | |
| 38 | static 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 | |
| 51 | static 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 | */ |
| 75 | static const struct sysfs_ops governor_sysfs_ops = { |
| 76 | .show = governor_show, |
| 77 | .store = governor_store, |
| 78 | }; |
| 79 | |
Rafael J. Wysocki | d10b5eb | 2016-02-06 13:50:24 +0100 | [diff] [blame] | 80 | void dbs_check_cpu(struct cpufreq_policy *policy) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 81 | { |
Rafael J. Wysocki | d10b5eb | 2016-02-06 13:50:24 +0100 | [diff] [blame] | 82 | int cpu = policy->cpu; |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 83 | struct dbs_governor *gov = dbs_governor_of(policy); |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 84 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
| 85 | struct dbs_data *dbs_data = policy_dbs->dbs_data; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 86 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Viresh Kumar | ff4b178 | 2016-02-09 09:01:32 +0530 | [diff] [blame] | 87 | unsigned int sampling_rate = dbs_data->sampling_rate; |
| 88 | unsigned int ignore_nice = dbs_data->ignore_nice_load; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 89 | unsigned int max_load = 0; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 90 | unsigned int j; |
| 91 | |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 92 | if (gov->governor == GOV_ONDEMAND) { |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 93 | struct od_cpu_dbs_info_s *od_dbs_info = |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 94 | gov->get_cpu_dbs_info_s(cpu); |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 95 | |
| 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. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 102 | sampling_rate *= od_dbs_info->rate_mult; |
| 103 | |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 104 | } |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 105 | |
Stratos Karafotis | dfa5bb6 | 2013-06-05 19:01:25 +0300 | [diff] [blame] | 106 | /* Get Absolute Load */ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 107 | for_each_cpu(j, policy->cpus) { |
Viresh Kumar | 875b850 | 2015-06-19 17:18:03 +0530 | [diff] [blame] | 108 | struct cpu_dbs_info *j_cdbs; |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 109 | u64 cur_wall_time, cur_idle_time; |
| 110 | unsigned int idle_time, wall_time; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 111 | unsigned int load; |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 112 | int io_busy = 0; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 113 | |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 114 | j_cdbs = gov->get_cpu_cdbs(j); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 115 | |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 116 | /* |
| 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. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 122 | if (gov->governor == GOV_ONDEMAND) |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 123 | io_busy = od_tuners->io_is_busy; |
| 124 | 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] | 125 | |
| 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 Yu | 0df3502 | 2015-12-16 12:20:29 +0800 | [diff] [blame] | 130 | if (cur_idle_time < j_cdbs->prev_cpu_idle) |
| 131 | cur_idle_time = j_cdbs->prev_cpu_idle; |
| 132 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 133 | 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. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 138 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 139 | 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 Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 156 | if (unlikely(!wall_time || wall_time < idle_time)) |
| 157 | continue; |
| 158 | |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 159 | /* |
| 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. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 175 | * 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 Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 179 | * |
| 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. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 184 | */ |
Viresh Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 185 | if (unlikely(wall_time > (2 * sampling_rate) && |
| 186 | j_cdbs->prev_load)) { |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 187 | load = j_cdbs->prev_load; |
Viresh Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 188 | |
| 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. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 195 | } else { |
| 196 | load = 100 * (wall_time - idle_time) / wall_time; |
| 197 | j_cdbs->prev_load = load; |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 198 | } |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 199 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 200 | if (load > max_load) |
| 201 | max_load = load; |
| 202 | } |
| 203 | |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 204 | gov->gov_check_cpu(cpu, max_load); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 205 | } |
| 206 | EXPORT_SYMBOL_GPL(dbs_check_cpu); |
| 207 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 208 | void gov_set_update_util(struct policy_dbs_info *policy_dbs, |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 209 | unsigned int delay_us) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 210 | { |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 211 | struct cpufreq_policy *policy = policy_dbs->policy; |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 212 | struct dbs_governor *gov = dbs_governor_of(policy); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 213 | int cpu; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 214 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 215 | gov_update_sample_delay(policy_dbs, delay_us); |
| 216 | policy_dbs->last_sample_time = 0; |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 217 | |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 218 | for_each_cpu(cpu, policy->cpus) { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 219 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu); |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 220 | |
| 221 | cpufreq_set_update_util_data(cpu, &cdbs->update_util); |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 222 | } |
| 223 | } |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 224 | EXPORT_SYMBOL_GPL(gov_set_update_util); |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 225 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 226 | static inline void gov_clear_update_util(struct cpufreq_policy *policy) |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 227 | { |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 228 | int i; |
| 229 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 230 | for_each_cpu(i, policy->cpus) |
| 231 | cpufreq_set_update_util_data(i, NULL); |
| 232 | |
| 233 | synchronize_rcu(); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 236 | static void gov_cancel_work(struct policy_dbs_info *policy_dbs) |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 237 | { |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 238 | /* Tell dbs_update_util_handler() to skip queuing up work items. */ |
Rafael J. Wysocki | 686cc63 | 2016-02-08 23:41:10 +0100 | [diff] [blame] | 239 | atomic_inc(&policy_dbs->work_count); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 240 | /* |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 241 | * If dbs_update_util_handler() is already running, it may not notice |
Rafael J. Wysocki | 686cc63 | 2016-02-08 23:41:10 +0100 | [diff] [blame] | 242 | * the incremented work_count, so wait for it to complete to prevent its |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 243 | * work item from being queued up after the cancel_work_sync() below. |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 244 | */ |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 245 | gov_clear_update_util(policy_dbs->policy); |
| 246 | irq_work_sync(&policy_dbs->irq_work); |
| 247 | cancel_work_sync(&policy_dbs->work); |
Rafael J. Wysocki | 686cc63 | 2016-02-08 23:41:10 +0100 | [diff] [blame] | 248 | atomic_set(&policy_dbs->work_count, 0); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 249 | } |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 250 | |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 251 | static void dbs_work_handler(struct work_struct *work) |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 252 | { |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 253 | struct policy_dbs_info *policy_dbs; |
Viresh Kumar | 3a91b069 | 2015-10-29 08:08:38 +0530 | [diff] [blame] | 254 | struct cpufreq_policy *policy; |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 255 | struct dbs_governor *gov; |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 256 | unsigned int delay; |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 257 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 258 | policy_dbs = container_of(work, struct policy_dbs_info, work); |
| 259 | policy = policy_dbs->policy; |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 260 | gov = dbs_governor_of(policy); |
Viresh Kumar | 3a91b069 | 2015-10-29 08:08:38 +0530 | [diff] [blame] | 261 | |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 262 | /* |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 263 | * Make sure cpufreq_governor_limits() isn't evaluating load or the |
| 264 | * ondemand governor isn't updating the sampling rate in parallel. |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 265 | */ |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 266 | mutex_lock(&policy_dbs->timer_mutex); |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 267 | delay = gov->gov_dbs_timer(policy); |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 268 | policy_dbs->sample_delay_ns = jiffies_to_nsecs(delay); |
| 269 | mutex_unlock(&policy_dbs->timer_mutex); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 270 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 271 | /* |
| 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. Wysocki | 686cc63 | 2016-02-08 23:41:10 +0100 | [diff] [blame] | 277 | atomic_dec(&policy_dbs->work_count); |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 278 | } |
| 279 | |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 280 | static void dbs_irq_work(struct irq_work *irq_work) |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 281 | { |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 282 | struct policy_dbs_info *policy_dbs; |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 283 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 284 | policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work); |
| 285 | schedule_work(&policy_dbs->work); |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 288 | static inline void gov_queue_irq_work(struct policy_dbs_info *policy_dbs) |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 289 | { |
| 290 | #ifdef CONFIG_SMP |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 291 | irq_work_queue_on(&policy_dbs->irq_work, smp_processor_id()); |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 292 | #else |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 293 | irq_work_queue(&policy_dbs->irq_work); |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 294 | #endif |
| 295 | } |
| 296 | |
| 297 | static 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. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 301 | struct policy_dbs_info *policy_dbs = cdbs->policy_dbs; |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 302 | |
| 303 | /* |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 304 | * 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 Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame] | 309 | */ |
Rafael J. Wysocki | 686cc63 | 2016-02-08 23:41:10 +0100 | [diff] [blame] | 310 | if (atomic_inc_return(&policy_dbs->work_count) == 1) { |
Rafael J. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 311 | u64 delta_ns; |
| 312 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 313 | 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. Wysocki | 9be4fd2 | 2016-02-10 16:53:50 +0100 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | } |
Rafael J. Wysocki | 686cc63 | 2016-02-08 23:41:10 +0100 | [diff] [blame] | 320 | atomic_dec(&policy_dbs->work_count); |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 321 | } |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 322 | |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 323 | static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy, |
| 324 | struct dbs_governor *gov) |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 325 | { |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 326 | struct policy_dbs_info *policy_dbs; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 327 | int j; |
| 328 | |
| 329 | /* Allocate memory for the common information for policy->cpus */ |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 330 | policy_dbs = kzalloc(sizeof(*policy_dbs), GFP_KERNEL); |
| 331 | if (!policy_dbs) |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 332 | return NULL; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 333 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 334 | mutex_init(&policy_dbs->timer_mutex); |
Rafael J. Wysocki | 686cc63 | 2016-02-08 23:41:10 +0100 | [diff] [blame] | 335 | atomic_set(&policy_dbs->work_count, 0); |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 336 | init_irq_work(&policy_dbs->irq_work, dbs_irq_work); |
| 337 | INIT_WORK(&policy_dbs->work, dbs_work_handler); |
Rafael J. Wysocki | cea6a9e | 2016-02-07 16:25:02 +0100 | [diff] [blame] | 338 | |
| 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. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 346 | return policy_dbs; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 347 | } |
| 348 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 349 | static void free_policy_dbs_info(struct cpufreq_policy *policy, |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 350 | struct dbs_governor *gov) |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 351 | { |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 352 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu); |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 353 | struct policy_dbs_info *policy_dbs = cdbs->policy_dbs; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 354 | int j; |
| 355 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 356 | mutex_destroy(&policy_dbs->timer_mutex); |
Viresh Kumar | 5e4500d | 2015-12-03 09:37:52 +0530 | [diff] [blame] | 357 | |
Rafael J. Wysocki | cea6a9e | 2016-02-07 16:25:02 +0100 | [diff] [blame] | 358 | for_each_cpu(j, policy->related_cpus) { |
| 359 | struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 360 | |
Rafael J. Wysocki | cea6a9e | 2016-02-07 16:25:02 +0100 | [diff] [blame] | 361 | j_cdbs->policy_dbs = NULL; |
| 362 | j_cdbs->update_util.func = NULL; |
| 363 | } |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 364 | kfree(policy_dbs); |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 365 | } |
| 366 | |
Rafael J. Wysocki | 906a6e5 | 2016-02-07 16:07:51 +0100 | [diff] [blame] | 367 | static int cpufreq_governor_init(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 368 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 369 | struct dbs_governor *gov = dbs_governor_of(policy); |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 370 | struct dbs_data *dbs_data = gov->gdbs_data; |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 371 | struct policy_dbs_info *policy_dbs; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 372 | unsigned int latency; |
| 373 | int ret; |
| 374 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 375 | /* State should be equivalent to EXIT */ |
| 376 | if (policy->governor_data) |
| 377 | return -EBUSY; |
| 378 | |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 379 | policy_dbs = alloc_policy_dbs_info(policy, gov); |
| 380 | if (!policy_dbs) |
| 381 | return -ENOMEM; |
| 382 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 383 | if (dbs_data) { |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 384 | if (WARN_ON(have_governor_per_policy())) { |
| 385 | ret = -EINVAL; |
| 386 | goto free_policy_dbs_info; |
| 387 | } |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 388 | dbs_data->usage_count++; |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 389 | policy_dbs->dbs_data = dbs_data; |
| 390 | policy->governor_data = policy_dbs; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL); |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 395 | if (!dbs_data) { |
| 396 | ret = -ENOMEM; |
| 397 | goto free_policy_dbs_info; |
| 398 | } |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 399 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 400 | dbs_data->usage_count = 1; |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame^] | 401 | mutex_init(&dbs_data->mutex); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 402 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 403 | ret = gov->init(dbs_data, !policy->governor->initialized); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 404 | if (ret) |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 405 | goto free_policy_dbs_info; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 406 | |
| 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 Kumar | ff4b178 | 2016-02-09 09:01:32 +0530 | [diff] [blame] | 415 | dbs_data->sampling_rate = max(dbs_data->min_sampling_rate, |
| 416 | LATENCY_MULTIPLIER * latency); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 417 | |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 418 | if (!have_governor_per_policy()) |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 419 | gov->gdbs_data = dbs_data; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 420 | |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 421 | policy_dbs->dbs_data = dbs_data; |
| 422 | policy->governor_data = policy_dbs; |
Viresh Kumar | e4b133c | 2016-01-25 22:33:46 +0530 | [diff] [blame] | 423 | |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame^] | 424 | 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. Wysocki | fafd5e8 | 2016-02-08 23:57:22 +0100 | [diff] [blame] | 428 | if (!ret) |
| 429 | return 0; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 430 | |
Rafael J. Wysocki | fafd5e8 | 2016-02-08 23:57:22 +0100 | [diff] [blame] | 431 | /* Failure, so roll back. */ |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame^] | 432 | pr_err("cpufreq: Governor initialization failed (dbs_data kobject init error %d)\n", ret); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 433 | |
Viresh Kumar | e4b133c | 2016-01-25 22:33:46 +0530 | [diff] [blame] | 434 | policy->governor_data = NULL; |
| 435 | |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 436 | if (!have_governor_per_policy()) |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 437 | gov->gdbs_data = NULL; |
| 438 | gov->exit(dbs_data, !policy->governor->initialized); |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 439 | kfree(dbs_data); |
| 440 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 441 | free_policy_dbs_info: |
| 442 | free_policy_dbs_info(policy, gov); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 443 | return ret; |
| 444 | } |
| 445 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 446 | static int cpufreq_governor_exit(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 447 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 448 | struct dbs_governor *gov = dbs_governor_of(policy); |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 449 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
| 450 | struct dbs_data *dbs_data = policy_dbs->dbs_data; |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 451 | |
| 452 | /* State should be equivalent to INIT */ |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 453 | if (policy_dbs->policy) |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 454 | return -EBUSY; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 455 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 456 | if (!--dbs_data->usage_count) { |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame^] | 457 | kobject_put(&dbs_data->kobj); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 458 | |
Viresh Kumar | e4b133c | 2016-01-25 22:33:46 +0530 | [diff] [blame] | 459 | policy->governor_data = NULL; |
| 460 | |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 461 | if (!have_governor_per_policy()) |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 462 | gov->gdbs_data = NULL; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 463 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 464 | gov->exit(dbs_data, policy->governor->initialized == 1); |
Viresh Kumar | c443563 | 2016-02-09 09:01:33 +0530 | [diff] [blame^] | 465 | mutex_destroy(&dbs_data->mutex); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 466 | kfree(dbs_data); |
Viresh Kumar | e4b133c | 2016-01-25 22:33:46 +0530 | [diff] [blame] | 467 | } else { |
| 468 | policy->governor_data = NULL; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 469 | } |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 470 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 471 | free_policy_dbs_info(policy, gov); |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 472 | return 0; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 473 | } |
| 474 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 475 | static int cpufreq_governor_start(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 476 | { |
Rafael J. Wysocki | ea59ee0d | 2016-02-07 16:09:51 +0100 | [diff] [blame] | 477 | struct dbs_governor *gov = dbs_governor_of(policy); |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 478 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
| 479 | struct dbs_data *dbs_data = policy_dbs->dbs_data; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 480 | unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 481 | int io_busy = 0; |
| 482 | |
| 483 | if (!policy->cur) |
| 484 | return -EINVAL; |
| 485 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 486 | /* State should be equivalent to INIT */ |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 487 | if (policy_dbs->policy) |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 488 | return -EBUSY; |
| 489 | |
Viresh Kumar | ff4b178 | 2016-02-09 09:01:32 +0530 | [diff] [blame] | 490 | sampling_rate = dbs_data->sampling_rate; |
| 491 | ignore_nice = dbs_data->ignore_nice_load; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 492 | |
Viresh Kumar | ff4b178 | 2016-02-09 09:01:32 +0530 | [diff] [blame] | 493 | if (gov->governor == GOV_ONDEMAND) { |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 494 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
| 495 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 496 | io_busy = od_tuners->io_is_busy; |
| 497 | } |
| 498 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 499 | for_each_cpu(j, policy->cpus) { |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 500 | struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 501 | unsigned int prev_load; |
| 502 | |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 503 | 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 Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 513 | } |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 514 | policy_dbs->policy = policy; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 515 | |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 516 | if (gov->governor == GOV_CONSERVATIVE) { |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 517 | struct cs_cpu_dbs_info_s *cs_dbs_info = |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 518 | gov->get_cpu_dbs_info_s(cpu); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 519 | |
| 520 | cs_dbs_info->down_skip = 0; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 521 | cs_dbs_info->requested_freq = policy->cur; |
| 522 | } else { |
Rafael J. Wysocki | 7bdad34 | 2016-02-07 16:05:07 +0100 | [diff] [blame] | 523 | 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 Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 525 | |
| 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. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 531 | gov_set_update_util(policy_dbs, sampling_rate); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 535 | static int cpufreq_governor_stop(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 536 | { |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 537 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 538 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 539 | /* State should be equivalent to START */ |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 540 | if (!policy_dbs->policy) |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 541 | return -EBUSY; |
| 542 | |
Rafael J. Wysocki | e40e7b2 | 2016-02-10 17:07:44 +0100 | [diff] [blame] | 543 | gov_cancel_work(policy_dbs); |
| 544 | policy_dbs->policy = NULL; |
Viresh Kumar | 3a91b069 | 2015-10-29 08:08:38 +0530 | [diff] [blame] | 545 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 546 | return 0; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 547 | } |
| 548 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 549 | static int cpufreq_governor_limits(struct cpufreq_policy *policy) |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 550 | { |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 551 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 552 | |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 553 | /* State should be equivalent to START */ |
Rafael J. Wysocki | bc50547 | 2016-02-07 16:24:26 +0100 | [diff] [blame] | 554 | if (!policy_dbs->policy) |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 555 | return -EBUSY; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 556 | |
Rafael J. Wysocki | e975189 | 2016-02-07 16:23:49 +0100 | [diff] [blame] | 557 | 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. Wysocki | d10b5eb | 2016-02-06 13:50:24 +0100 | [diff] [blame] | 562 | dbs_check_cpu(policy); |
Rafael J. Wysocki | e975189 | 2016-02-07 16:23:49 +0100 | [diff] [blame] | 563 | mutex_unlock(&policy_dbs->timer_mutex); |
Viresh Kumar | a72c495 | 2015-07-18 11:31:01 +0530 | [diff] [blame] | 564 | |
| 565 | return 0; |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 566 | } |
| 567 | |
Rafael J. Wysocki | 906a6e5 | 2016-02-07 16:07:51 +0100 | [diff] [blame] | 568 | int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event) |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 569 | { |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 570 | int ret = -EINVAL; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 571 | |
Viresh Kumar | 732b6d6 | 2015-06-03 15:57:13 +0530 | [diff] [blame] | 572 | /* Lock governor to block concurrent initialization of governor */ |
Rafael J. Wysocki | 2bb8d94 | 2016-02-07 16:01:31 +0100 | [diff] [blame] | 573 | mutex_lock(&dbs_data_mutex); |
Viresh Kumar | 732b6d6 | 2015-06-03 15:57:13 +0530 | [diff] [blame] | 574 | |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 575 | if (event == CPUFREQ_GOV_POLICY_INIT) { |
Rafael J. Wysocki | 906a6e5 | 2016-02-07 16:07:51 +0100 | [diff] [blame] | 576 | ret = cpufreq_governor_init(policy); |
Rafael J. Wysocki | 5da3dd1 | 2016-02-05 03:15:24 +0100 | [diff] [blame] | 577 | } 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 Kumar | 732b6d6 | 2015-06-03 15:57:13 +0530 | [diff] [blame] | 592 | } |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 593 | |
Rafael J. Wysocki | 2bb8d94 | 2016-02-07 16:01:31 +0100 | [diff] [blame] | 594 | mutex_unlock(&dbs_data_mutex); |
Viresh Kumar | 714a2d9 | 2015-06-04 16:43:27 +0530 | [diff] [blame] | 595 | return ret; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 596 | } |
| 597 | EXPORT_SYMBOL_GPL(cpufreq_governor_dbs); |