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