Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/cpufreq/cpufreq_interactive.c |
| 3 | * |
| 4 | * Copyright (C) 2010 Google, Inc. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * Author: Mike Chan (mike@android.com) |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/cpu.h> |
| 20 | #include <linux/cpumask.h> |
| 21 | #include <linux/cpufreq.h> |
| 22 | #include <linux/module.h> |
Lianwei Wang | ff5a709 | 2012-11-01 09:59:52 +0800 | [diff] [blame] | 23 | #include <linux/moduleparam.h> |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 24 | #include <linux/rwsem.h> |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 25 | #include <linux/sched.h> |
| 26 | #include <linux/sched/rt.h> |
| 27 | #include <linux/tick.h> |
| 28 | #include <linux/time.h> |
| 29 | #include <linux/timer.h> |
| 30 | #include <linux/workqueue.h> |
| 31 | #include <linux/kthread.h> |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 32 | #include <linux/slab.h> |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 33 | |
Todd Poynor | 1c2fa8a | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 34 | #define CREATE_TRACE_POINTS |
| 35 | #include <trace/events/cpufreq_interactive.h> |
| 36 | |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 37 | struct cpufreq_interactive_cpuinfo { |
| 38 | struct timer_list cpu_timer; |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 39 | struct timer_list cpu_slack_timer; |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 40 | spinlock_t load_lock; /* protects the next 4 fields */ |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 41 | u64 time_in_idle; |
Todd Poynor | 8a2739e | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 42 | u64 time_in_idle_timestamp; |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 43 | u64 cputime_speedadj; |
| 44 | u64 cputime_speedadj_timestamp; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 45 | struct cpufreq_policy *policy; |
| 46 | struct cpufreq_frequency_table *freq_table; |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 47 | spinlock_t target_freq_lock; /*protects target freq */ |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 48 | unsigned int target_freq; |
Todd Poynor | 90fe90d | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 49 | unsigned int floor_freq; |
Junjie Wu | 302f05f | 2015-03-24 15:51:10 -0700 | [diff] [blame] | 50 | u64 pol_floor_val_time; /* policy floor_validate_time */ |
| 51 | u64 loc_floor_val_time; /* per-cpu floor_validate_time */ |
Saravana Kannan | 76c17af | 2014-10-15 12:44:18 -0700 | [diff] [blame] | 52 | u64 pol_hispeed_val_time; /* policy hispeed_validate_time */ |
| 53 | u64 loc_hispeed_val_time; /* per-cpu hispeed_validate_time */ |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 54 | struct rw_semaphore enable_sem; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 55 | int governor_enabled; |
| 56 | }; |
| 57 | |
| 58 | static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo); |
| 59 | |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 60 | /* realtime thread handles frequency scaling */ |
| 61 | static struct task_struct *speedchange_task; |
| 62 | static cpumask_t speedchange_cpumask; |
| 63 | static spinlock_t speedchange_cpumask_lock; |
Lianwei Wang | 6becfaa | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 64 | static struct mutex gov_lock; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 65 | |
Todd Poynor | ff6cb66 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 66 | /* Target load. Lower values result in higher CPU speeds. */ |
| 67 | #define DEFAULT_TARGET_LOAD 90 |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 68 | static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD}; |
Todd Poynor | ff6cb66 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 69 | |
Todd Poynor | 82845bf | 2012-04-17 17:39:34 -0700 | [diff] [blame] | 70 | #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC) |
Todd Poynor | fb421c4 | 2012-04-13 20:18:02 -0700 | [diff] [blame] | 71 | #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 72 | static unsigned int default_above_hispeed_delay[] = { |
| 73 | DEFAULT_ABOVE_HISPEED_DELAY }; |
Todd Poynor | fb421c4 | 2012-04-13 20:18:02 -0700 | [diff] [blame] | 74 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 75 | struct cpufreq_interactive_tunables { |
| 76 | int usage_count; |
| 77 | /* Hi speed to bump to from lo speed when load burst (default max) */ |
| 78 | unsigned int hispeed_freq; |
| 79 | /* Go to hi speed when CPU load at or above this value. */ |
| 80 | #define DEFAULT_GO_HISPEED_LOAD 99 |
| 81 | unsigned long go_hispeed_load; |
| 82 | /* Target load. Lower values result in higher CPU speeds. */ |
| 83 | spinlock_t target_loads_lock; |
| 84 | unsigned int *target_loads; |
| 85 | int ntarget_loads; |
| 86 | /* |
| 87 | * The minimum amount of time to spend at a frequency before we can ramp |
| 88 | * down. |
| 89 | */ |
| 90 | #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC) |
| 91 | unsigned long min_sample_time; |
| 92 | /* |
| 93 | * The sample rate of the timer used to increase frequency |
| 94 | */ |
| 95 | unsigned long timer_rate; |
| 96 | /* |
| 97 | * Wait this long before raising speed above hispeed, by default a |
| 98 | * single timer interval. |
| 99 | */ |
| 100 | spinlock_t above_hispeed_delay_lock; |
| 101 | unsigned int *above_hispeed_delay; |
| 102 | int nabove_hispeed_delay; |
| 103 | /* Non-zero means indefinite speed boost active */ |
| 104 | int boost_val; |
| 105 | /* Duration of a boot pulse in usecs */ |
| 106 | int boostpulse_duration_val; |
| 107 | /* End time of boost pulse in ktime converted to usecs */ |
| 108 | u64 boostpulse_endtime; |
Lianwei Wang | 4f81d19 | 2014-12-02 17:20:50 -0800 | [diff] [blame] | 109 | bool boosted; |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 110 | /* |
| 111 | * Max additional time to wait in idle, beyond timer_rate, at speeds |
| 112 | * above minimum before wakeup to reduce speed, or -1 if unnecessary. |
| 113 | */ |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 114 | #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE) |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 115 | int timer_slack_val; |
| 116 | bool io_is_busy; |
| 117 | }; |
Lianwei Wang | ff5a709 | 2012-11-01 09:59:52 +0800 | [diff] [blame] | 118 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 119 | /* For cases where we have single governor instance for system */ |
Cylen Yao | 2f32796 | 2014-09-05 18:27:38 -0700 | [diff] [blame] | 120 | static struct cpufreq_interactive_tunables *common_tunables; |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 121 | |
| 122 | static struct attribute_group *get_sysfs_attr(void); |
Lianwei Wang | 2747480 | 2013-02-22 11:39:18 +0800 | [diff] [blame] | 123 | |
Todd Poynor | 8a2739e | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 124 | static void cpufreq_interactive_timer_resched( |
| 125 | struct cpufreq_interactive_cpuinfo *pcpu) |
| 126 | { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 127 | struct cpufreq_interactive_tunables *tunables = |
| 128 | pcpu->policy->governor_data; |
Todd Poynor | 4eebb54 | 2013-04-05 13:25:21 -0700 | [diff] [blame] | 129 | unsigned long expires; |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 130 | unsigned long flags; |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 131 | |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 132 | spin_lock_irqsave(&pcpu->load_lock, flags); |
Todd Poynor | 8a2739e | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 133 | pcpu->time_in_idle = |
Lianwei Wang | 2747480 | 2013-02-22 11:39:18 +0800 | [diff] [blame] | 134 | get_cpu_idle_time(smp_processor_id(), |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 135 | &pcpu->time_in_idle_timestamp, |
| 136 | tunables->io_is_busy); |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 137 | pcpu->cputime_speedadj = 0; |
| 138 | pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp; |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 139 | expires = jiffies + usecs_to_jiffies(tunables->timer_rate); |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 140 | mod_timer(&pcpu->cpu_timer, expires); |
Todd Poynor | 4eebb54 | 2013-04-05 13:25:21 -0700 | [diff] [blame] | 141 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 142 | if (tunables->timer_slack_val >= 0 && |
| 143 | pcpu->target_freq > pcpu->policy->min) { |
| 144 | expires += usecs_to_jiffies(tunables->timer_slack_val); |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 145 | mod_timer(&pcpu->cpu_slack_timer, expires); |
Todd Poynor | 4eebb54 | 2013-04-05 13:25:21 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 148 | spin_unlock_irqrestore(&pcpu->load_lock, flags); |
Todd Poynor | 8a2739e | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Lianwei Wang | bbe849d | 2013-04-26 13:30:51 +0800 | [diff] [blame] | 151 | /* The caller shall take enable_sem write semaphore to avoid any timer race. |
| 152 | * The cpu_timer and cpu_slack_timer must be deactivated when calling this |
| 153 | * function. |
| 154 | */ |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 155 | static void cpufreq_interactive_timer_start( |
| 156 | struct cpufreq_interactive_tunables *tunables, int cpu) |
Lianwei Wang | bbe849d | 2013-04-26 13:30:51 +0800 | [diff] [blame] | 157 | { |
| 158 | struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu); |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 159 | unsigned long expires = jiffies + |
| 160 | usecs_to_jiffies(tunables->timer_rate); |
Lianwei Wang | bbe849d | 2013-04-26 13:30:51 +0800 | [diff] [blame] | 161 | unsigned long flags; |
| 162 | |
| 163 | pcpu->cpu_timer.expires = expires; |
| 164 | add_timer_on(&pcpu->cpu_timer, cpu); |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 165 | if (tunables->timer_slack_val >= 0 && |
| 166 | pcpu->target_freq > pcpu->policy->min) { |
| 167 | expires += usecs_to_jiffies(tunables->timer_slack_val); |
Lianwei Wang | bbe849d | 2013-04-26 13:30:51 +0800 | [diff] [blame] | 168 | pcpu->cpu_slack_timer.expires = expires; |
| 169 | add_timer_on(&pcpu->cpu_slack_timer, cpu); |
| 170 | } |
| 171 | |
| 172 | spin_lock_irqsave(&pcpu->load_lock, flags); |
| 173 | pcpu->time_in_idle = |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 174 | get_cpu_idle_time(cpu, &pcpu->time_in_idle_timestamp, |
| 175 | tunables->io_is_busy); |
Lianwei Wang | bbe849d | 2013-04-26 13:30:51 +0800 | [diff] [blame] | 176 | pcpu->cputime_speedadj = 0; |
| 177 | pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp; |
| 178 | spin_unlock_irqrestore(&pcpu->load_lock, flags); |
| 179 | } |
| 180 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 181 | static unsigned int freq_to_above_hispeed_delay( |
| 182 | struct cpufreq_interactive_tunables *tunables, |
| 183 | unsigned int freq) |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 184 | { |
| 185 | int i; |
| 186 | unsigned int ret; |
| 187 | unsigned long flags; |
| 188 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 189 | spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags); |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 190 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 191 | for (i = 0; i < tunables->nabove_hispeed_delay - 1 && |
| 192 | freq >= tunables->above_hispeed_delay[i+1]; i += 2) |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 193 | ; |
| 194 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 195 | ret = tunables->above_hispeed_delay[i]; |
| 196 | spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags); |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 197 | return ret; |
| 198 | } |
| 199 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 200 | static unsigned int freq_to_targetload( |
| 201 | struct cpufreq_interactive_tunables *tunables, unsigned int freq) |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 202 | { |
| 203 | int i; |
| 204 | unsigned int ret; |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 205 | unsigned long flags; |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 206 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 207 | spin_lock_irqsave(&tunables->target_loads_lock, flags); |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 208 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 209 | for (i = 0; i < tunables->ntarget_loads - 1 && |
| 210 | freq >= tunables->target_loads[i+1]; i += 2) |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 211 | ; |
| 212 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 213 | ret = tunables->target_loads[i]; |
| 214 | spin_unlock_irqrestore(&tunables->target_loads_lock, flags); |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * If increasing frequencies never map to a lower target load then |
| 220 | * choose_freq() will find the minimum frequency that does not exceed its |
| 221 | * target load given the current load. |
| 222 | */ |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 223 | static unsigned int choose_freq(struct cpufreq_interactive_cpuinfo *pcpu, |
| 224 | unsigned int loadadjfreq) |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 225 | { |
| 226 | unsigned int freq = pcpu->policy->cur; |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 227 | unsigned int prevfreq, freqmin, freqmax; |
| 228 | unsigned int tl; |
| 229 | int index; |
| 230 | |
| 231 | freqmin = 0; |
| 232 | freqmax = UINT_MAX; |
| 233 | |
| 234 | do { |
| 235 | prevfreq = freq; |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 236 | tl = freq_to_targetload(pcpu->policy->governor_data, freq); |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * Find the lowest frequency where the computed load is less |
| 240 | * than or equal to the target load. |
| 241 | */ |
| 242 | |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 243 | index = cpufreq_frequency_table_target( |
| 244 | pcpu->policy, loadadjfreq / tl, |
| 245 | CPUFREQ_RELATION_L); |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 246 | freq = pcpu->freq_table[index].frequency; |
| 247 | |
| 248 | if (freq > prevfreq) { |
| 249 | /* The previous frequency is too low. */ |
| 250 | freqmin = prevfreq; |
| 251 | |
| 252 | if (freq >= freqmax) { |
| 253 | /* |
| 254 | * Find the highest frequency that is less |
| 255 | * than freqmax. |
| 256 | */ |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 257 | index = cpufreq_frequency_table_target( |
| 258 | pcpu->policy, |
| 259 | freqmax - 1, CPUFREQ_RELATION_H); |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 260 | freq = pcpu->freq_table[index].frequency; |
| 261 | |
| 262 | if (freq == freqmin) { |
| 263 | /* |
| 264 | * The first frequency below freqmax |
| 265 | * has already been found to be too |
| 266 | * low. freqmax is the lowest speed |
| 267 | * we found that is fast enough. |
| 268 | */ |
| 269 | freq = freqmax; |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | } else if (freq < prevfreq) { |
| 274 | /* The previous frequency is high enough. */ |
| 275 | freqmax = prevfreq; |
| 276 | |
| 277 | if (freq <= freqmin) { |
| 278 | /* |
| 279 | * Find the lowest frequency that is higher |
| 280 | * than freqmin. |
| 281 | */ |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 282 | index = cpufreq_frequency_table_target( |
| 283 | pcpu->policy, |
| 284 | freqmin + 1, CPUFREQ_RELATION_L); |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 285 | freq = pcpu->freq_table[index].frequency; |
| 286 | |
| 287 | /* |
| 288 | * If freqmax is the first frequency above |
| 289 | * freqmin then we have already found that |
| 290 | * this speed is fast enough. |
| 291 | */ |
| 292 | if (freq == freqmax) |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* If same frequency chosen as previous then done. */ |
| 298 | } while (freq != prevfreq); |
| 299 | |
| 300 | return freq; |
| 301 | } |
| 302 | |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 303 | static u64 update_load(int cpu) |
| 304 | { |
| 305 | struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu); |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 306 | struct cpufreq_interactive_tunables *tunables = |
| 307 | pcpu->policy->governor_data; |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 308 | u64 now; |
| 309 | u64 now_idle; |
| 310 | unsigned int delta_idle; |
| 311 | unsigned int delta_time; |
| 312 | u64 active_time; |
| 313 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 314 | now_idle = get_cpu_idle_time(cpu, &now, tunables->io_is_busy); |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 315 | delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle); |
| 316 | delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp); |
Minsung Kim | dd7886b | 2013-04-23 22:32:01 +0900 | [diff] [blame] | 317 | |
| 318 | if (delta_time <= delta_idle) |
| 319 | active_time = 0; |
| 320 | else |
| 321 | active_time = delta_time - delta_idle; |
| 322 | |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 323 | pcpu->cputime_speedadj += active_time * pcpu->policy->cur; |
| 324 | |
| 325 | pcpu->time_in_idle = now_idle; |
| 326 | pcpu->time_in_idle_timestamp = now; |
| 327 | return now; |
| 328 | } |
| 329 | |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 330 | static void cpufreq_interactive_timer(unsigned long data) |
| 331 | { |
Todd Poynor | b510d29 | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 332 | u64 now; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 333 | unsigned int delta_time; |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 334 | u64 cputime_speedadj; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 335 | int cpu_load; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 336 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 337 | &per_cpu(cpuinfo, data); |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 338 | struct cpufreq_interactive_tunables *tunables = |
| 339 | pcpu->policy->governor_data; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 340 | unsigned int new_freq; |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 341 | unsigned int loadadjfreq; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 342 | unsigned int index; |
| 343 | unsigned long flags; |
Junjie Wu | 302f05f | 2015-03-24 15:51:10 -0700 | [diff] [blame] | 344 | u64 max_fvtime; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 345 | |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 346 | if (!down_read_trylock(&pcpu->enable_sem)) |
| 347 | return; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 348 | if (!pcpu->governor_enabled) |
| 349 | goto exit; |
| 350 | |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 351 | spin_lock_irqsave(&pcpu->load_lock, flags); |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 352 | now = update_load(data); |
| 353 | delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp); |
| 354 | cputime_speedadj = pcpu->cputime_speedadj; |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 355 | spin_unlock_irqrestore(&pcpu->load_lock, flags); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 356 | |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 357 | if (WARN_ON_ONCE(!delta_time)) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 358 | goto rearm; |
| 359 | |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 360 | spin_lock_irqsave(&pcpu->target_freq_lock, flags); |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 361 | do_div(cputime_speedadj, delta_time); |
| 362 | loadadjfreq = (unsigned int)cputime_speedadj * 100; |
rahul.khandelwal | 9c0deef | 2015-04-17 11:45:23 +0530 | [diff] [blame] | 363 | cpu_load = loadadjfreq / pcpu->policy->cur; |
Lianwei Wang | 4f81d19 | 2014-12-02 17:20:50 -0800 | [diff] [blame] | 364 | tunables->boosted = tunables->boost_val || now < tunables->boostpulse_endtime; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 365 | |
Lianwei Wang | 4f81d19 | 2014-12-02 17:20:50 -0800 | [diff] [blame] | 366 | if (cpu_load >= tunables->go_hispeed_load || tunables->boosted) { |
Saravana Kannan | 76c17af | 2014-10-15 12:44:18 -0700 | [diff] [blame] | 367 | if (pcpu->policy->cur < tunables->hispeed_freq) { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 368 | new_freq = tunables->hispeed_freq; |
Todd Poynor | eb84e53 | 2012-12-19 16:06:48 -0800 | [diff] [blame] | 369 | } else { |
| 370 | new_freq = choose_freq(pcpu, loadadjfreq); |
| 371 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 372 | if (new_freq < tunables->hispeed_freq) |
| 373 | new_freq = tunables->hispeed_freq; |
Todd Poynor | eb84e53 | 2012-12-19 16:06:48 -0800 | [diff] [blame] | 374 | } |
| 375 | } else { |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 376 | new_freq = choose_freq(pcpu, loadadjfreq); |
Ruchi Kandoi | 4b9012a | 2014-06-13 16:24:15 -0700 | [diff] [blame] | 377 | if (new_freq > tunables->hispeed_freq && |
Saravana Kannan | 76c17af | 2014-10-15 12:44:18 -0700 | [diff] [blame] | 378 | pcpu->policy->cur < tunables->hispeed_freq) |
Ruchi Kandoi | 4b9012a | 2014-06-13 16:24:15 -0700 | [diff] [blame] | 379 | new_freq = tunables->hispeed_freq; |
Todd Poynor | eb84e53 | 2012-12-19 16:06:48 -0800 | [diff] [blame] | 380 | } |
Todd Poynor | 240997a | 2012-11-08 15:06:55 -0800 | [diff] [blame] | 381 | |
Saravana Kannan | 76c17af | 2014-10-15 12:44:18 -0700 | [diff] [blame] | 382 | if (pcpu->policy->cur >= tunables->hispeed_freq && |
| 383 | new_freq > pcpu->policy->cur && |
| 384 | now - pcpu->pol_hispeed_val_time < |
| 385 | freq_to_above_hispeed_delay(tunables, pcpu->policy->cur)) { |
Todd Poynor | 240997a | 2012-11-08 15:06:55 -0800 | [diff] [blame] | 386 | trace_cpufreq_interactive_notyet( |
| 387 | data, cpu_load, pcpu->target_freq, |
| 388 | pcpu->policy->cur, new_freq); |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 389 | spin_unlock_irqrestore(&pcpu->target_freq_lock, flags); |
Todd Poynor | 240997a | 2012-11-08 15:06:55 -0800 | [diff] [blame] | 390 | goto rearm; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Saravana Kannan | 76c17af | 2014-10-15 12:44:18 -0700 | [diff] [blame] | 393 | pcpu->loc_hispeed_val_time = now; |
Todd Poynor | d0ca13f | 2012-05-10 23:28:06 -0700 | [diff] [blame] | 394 | |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 395 | index = cpufreq_frequency_table_target(pcpu->policy, |
| 396 | new_freq, CPUFREQ_RELATION_L); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 397 | new_freq = pcpu->freq_table[index].frequency; |
| 398 | |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 399 | /* |
Todd Poynor | 90fe90d | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 400 | * Do not scale below floor_freq unless we have been at or above the |
| 401 | * floor frequency for the minimum sample time since last validated. |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 402 | */ |
Junjie Wu | 302f05f | 2015-03-24 15:51:10 -0700 | [diff] [blame] | 403 | max_fvtime = max(pcpu->pol_floor_val_time, pcpu->loc_floor_val_time); |
| 404 | if (new_freq < pcpu->floor_freq && |
| 405 | pcpu->target_freq >= pcpu->policy->cur) { |
| 406 | if (now - max_fvtime < tunables->min_sample_time) { |
Todd Poynor | 5645931 | 2012-11-28 17:56:09 -0800 | [diff] [blame] | 407 | trace_cpufreq_interactive_notyet( |
| 408 | data, cpu_load, pcpu->target_freq, |
| 409 | pcpu->policy->cur, new_freq); |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 410 | spin_unlock_irqrestore(&pcpu->target_freq_lock, flags); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 411 | goto rearm; |
Todd Poynor | 1c2fa8a | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 412 | } |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Todd Poynor | eda4029 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 415 | /* |
| 416 | * Update the timestamp for checking whether speed has been held at |
| 417 | * or above the selected frequency for a minimum of min_sample_time, |
| 418 | * if not boosted to hispeed_freq. If boosted to hispeed_freq then we |
| 419 | * allow the speed to drop as soon as the boostpulse duration expires |
| 420 | * (or the indefinite boost is turned off). |
| 421 | */ |
| 422 | |
Lianwei Wang | 4f81d19 | 2014-12-02 17:20:50 -0800 | [diff] [blame] | 423 | if (!tunables->boosted || new_freq > tunables->hispeed_freq) { |
Todd Poynor | eda4029 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 424 | pcpu->floor_freq = new_freq; |
Junjie Wu | 302f05f | 2015-03-24 15:51:10 -0700 | [diff] [blame] | 425 | if (pcpu->target_freq >= pcpu->policy->cur || |
| 426 | new_freq >= pcpu->policy->cur) |
| 427 | pcpu->loc_floor_val_time = now; |
Todd Poynor | eda4029 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 428 | } |
Todd Poynor | a994e81 | 2012-04-06 19:59:36 -0700 | [diff] [blame] | 429 | |
Minsung Kim | 2680020 | 2014-11-29 21:43:53 +0900 | [diff] [blame] | 430 | if (pcpu->target_freq == new_freq && |
| 431 | pcpu->target_freq <= pcpu->policy->cur) { |
Todd Poynor | 5645931 | 2012-11-28 17:56:09 -0800 | [diff] [blame] | 432 | trace_cpufreq_interactive_already( |
| 433 | data, cpu_load, pcpu->target_freq, |
| 434 | pcpu->policy->cur, new_freq); |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 435 | spin_unlock_irqrestore(&pcpu->target_freq_lock, flags); |
Rohit Gupta | dc42410 | 2015-03-06 18:46:04 -0800 | [diff] [blame] | 436 | goto rearm; |
Todd Poynor | a994e81 | 2012-04-06 19:59:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Todd Poynor | 1c2fa8a | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 439 | trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq, |
Todd Poynor | 5645931 | 2012-11-28 17:56:09 -0800 | [diff] [blame] | 440 | pcpu->policy->cur, new_freq); |
Todd Poynor | 1c2fa8a | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 441 | |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 442 | pcpu->target_freq = new_freq; |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 443 | spin_unlock_irqrestore(&pcpu->target_freq_lock, flags); |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 444 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
| 445 | cpumask_set_cpu(data, &speedchange_cpumask); |
| 446 | spin_unlock_irqrestore(&speedchange_cpumask_lock, flags); |
| 447 | wake_up_process(speedchange_task); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 448 | |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 449 | rearm: |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 450 | if (!timer_pending(&pcpu->cpu_timer)) |
Todd Poynor | 8a2739e | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 451 | cpufreq_interactive_timer_resched(pcpu); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 452 | |
| 453 | exit: |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 454 | up_read(&pcpu->enable_sem); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 458 | static void cpufreq_interactive_idle_end(void) |
| 459 | { |
| 460 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 461 | &per_cpu(cpuinfo, smp_processor_id()); |
| 462 | |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 463 | if (!down_read_trylock(&pcpu->enable_sem)) |
Sam Leffler | f61219a | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 464 | return; |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 465 | if (!pcpu->governor_enabled) { |
| 466 | up_read(&pcpu->enable_sem); |
| 467 | return; |
| 468 | } |
Sam Leffler | f61219a | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 469 | |
Todd Poynor | b510d29 | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 470 | /* Arm the timer for 1-2 ticks later if not already. */ |
| 471 | if (!timer_pending(&pcpu->cpu_timer)) { |
Todd Poynor | 8a2739e | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 472 | cpufreq_interactive_timer_resched(pcpu); |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 473 | } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) { |
Todd Poynor | 8a2739e | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 474 | del_timer(&pcpu->cpu_timer); |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 475 | del_timer(&pcpu->cpu_slack_timer); |
Todd Poynor | 8a2739e | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 476 | cpufreq_interactive_timer(smp_processor_id()); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 477 | } |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 478 | |
| 479 | up_read(&pcpu->enable_sem); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Dmitry Torokhov | 78e6913 | 2015-02-04 13:54:48 -0800 | [diff] [blame] | 482 | static void cpufreq_interactive_get_policy_info(struct cpufreq_policy *policy, |
| 483 | unsigned int *pmax_freq, |
| 484 | u64 *phvt, u64 *pfvt) |
| 485 | { |
| 486 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 487 | unsigned int max_freq = 0; |
| 488 | u64 hvt = ~0ULL, fvt = 0; |
| 489 | unsigned int i; |
| 490 | |
| 491 | for_each_cpu(i, policy->cpus) { |
| 492 | pcpu = &per_cpu(cpuinfo, i); |
| 493 | |
| 494 | fvt = max(fvt, pcpu->loc_floor_val_time); |
| 495 | if (pcpu->target_freq > max_freq) { |
| 496 | max_freq = pcpu->target_freq; |
| 497 | hvt = pcpu->loc_hispeed_val_time; |
| 498 | } else if (pcpu->target_freq == max_freq) { |
| 499 | hvt = min(hvt, pcpu->loc_hispeed_val_time); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | *pmax_freq = max_freq; |
| 504 | *phvt = hvt; |
| 505 | *pfvt = fvt; |
| 506 | } |
| 507 | |
| 508 | static void cpufreq_interactive_adjust_cpu(unsigned int cpu, |
| 509 | struct cpufreq_policy *policy) |
| 510 | { |
| 511 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 512 | u64 hvt, fvt; |
| 513 | unsigned int max_freq; |
| 514 | int i; |
| 515 | |
| 516 | cpufreq_interactive_get_policy_info(policy, &max_freq, &hvt, &fvt); |
| 517 | |
| 518 | for_each_cpu(i, policy->cpus) { |
| 519 | pcpu = &per_cpu(cpuinfo, i); |
| 520 | pcpu->pol_floor_val_time = fvt; |
| 521 | } |
| 522 | |
| 523 | if (max_freq != policy->cur) { |
| 524 | __cpufreq_driver_target(policy, max_freq, CPUFREQ_RELATION_H); |
| 525 | for_each_cpu(i, policy->cpus) { |
| 526 | pcpu = &per_cpu(cpuinfo, i); |
| 527 | pcpu->pol_hispeed_val_time = hvt; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | trace_cpufreq_interactive_setspeed(cpu, max_freq, policy->cur); |
| 532 | } |
| 533 | |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 534 | static int cpufreq_interactive_speedchange_task(void *data) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 535 | { |
| 536 | unsigned int cpu; |
| 537 | cpumask_t tmp_mask; |
| 538 | unsigned long flags; |
| 539 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 540 | |
| 541 | while (1) { |
| 542 | set_current_state(TASK_INTERRUPTIBLE); |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 543 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 544 | |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 545 | if (cpumask_empty(&speedchange_cpumask)) { |
| 546 | spin_unlock_irqrestore(&speedchange_cpumask_lock, |
| 547 | flags); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 548 | schedule(); |
| 549 | |
| 550 | if (kthread_should_stop()) |
| 551 | break; |
| 552 | |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 553 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | set_current_state(TASK_RUNNING); |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 557 | tmp_mask = speedchange_cpumask; |
| 558 | cpumask_clear(&speedchange_cpumask); |
| 559 | spin_unlock_irqrestore(&speedchange_cpumask_lock, flags); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 560 | |
| 561 | for_each_cpu(cpu, &tmp_mask) { |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 562 | pcpu = &per_cpu(cpuinfo, cpu); |
Dmitry Torokhov | 78e6913 | 2015-02-04 13:54:48 -0800 | [diff] [blame] | 563 | |
| 564 | down_write(&pcpu->policy->rwsem); |
| 565 | |
| 566 | if (likely(down_read_trylock(&pcpu->enable_sem))) { |
| 567 | if (likely(pcpu->governor_enabled)) |
| 568 | cpufreq_interactive_adjust_cpu(cpu, |
| 569 | pcpu->policy); |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 570 | up_read(&pcpu->enable_sem); |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 571 | } |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 572 | |
Dmitry Torokhov | 78e6913 | 2015-02-04 13:54:48 -0800 | [diff] [blame] | 573 | up_write(&pcpu->policy->rwsem); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
Lianwei Wang | 4f81d19 | 2014-12-02 17:20:50 -0800 | [diff] [blame] | 580 | static void cpufreq_interactive_boost(struct cpufreq_interactive_tunables *tunables) |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 581 | { |
| 582 | int i; |
| 583 | int anyboost = 0; |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 584 | unsigned long flags[2]; |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 585 | struct cpufreq_interactive_cpuinfo *pcpu; |
Lianwei Wang | 4f81d19 | 2014-12-02 17:20:50 -0800 | [diff] [blame] | 586 | |
| 587 | tunables->boosted = true; |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 588 | |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 589 | spin_lock_irqsave(&speedchange_cpumask_lock, flags[0]); |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 590 | |
| 591 | for_each_online_cpu(i) { |
| 592 | pcpu = &per_cpu(cpuinfo, i); |
Daniel Kurtz | 1160fcb | 2015-05-28 12:08:11 +0800 | [diff] [blame] | 593 | |
| 594 | if (!down_read_trylock(&pcpu->enable_sem)) |
Lianwei Wang | 4f81d19 | 2014-12-02 17:20:50 -0800 | [diff] [blame] | 595 | continue; |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 596 | |
Daniel Kurtz | 1160fcb | 2015-05-28 12:08:11 +0800 | [diff] [blame] | 597 | if (!pcpu->governor_enabled) { |
| 598 | up_read(&pcpu->enable_sem); |
| 599 | continue; |
| 600 | } |
| 601 | |
| 602 | if (tunables != pcpu->policy->governor_data) { |
| 603 | up_read(&pcpu->enable_sem); |
| 604 | continue; |
| 605 | } |
| 606 | |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 607 | spin_lock_irqsave(&pcpu->target_freq_lock, flags[1]); |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 608 | if (pcpu->target_freq < tunables->hispeed_freq) { |
| 609 | pcpu->target_freq = tunables->hispeed_freq; |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 610 | cpumask_set_cpu(i, &speedchange_cpumask); |
Saravana Kannan | 76c17af | 2014-10-15 12:44:18 -0700 | [diff] [blame] | 611 | pcpu->pol_hispeed_val_time = |
Todd Poynor | 91c1f6e | 2012-12-07 20:08:45 -0800 | [diff] [blame] | 612 | ktime_to_us(ktime_get()); |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 613 | anyboost = 1; |
| 614 | } |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 615 | spin_unlock_irqrestore(&pcpu->target_freq_lock, flags[1]); |
Daniel Kurtz | 1160fcb | 2015-05-28 12:08:11 +0800 | [diff] [blame] | 616 | |
| 617 | up_read(&pcpu->enable_sem); |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 620 | spin_unlock_irqrestore(&speedchange_cpumask_lock, flags[0]); |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 621 | |
| 622 | if (anyboost) |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 623 | wake_up_process(speedchange_task); |
Todd Poynor | cc50604 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 626 | static int cpufreq_interactive_notifier( |
| 627 | struct notifier_block *nb, unsigned long val, void *data) |
| 628 | { |
| 629 | struct cpufreq_freqs *freq = data; |
| 630 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 631 | int cpu; |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 632 | unsigned long flags; |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 633 | |
| 634 | if (val == CPUFREQ_POSTCHANGE) { |
| 635 | pcpu = &per_cpu(cpuinfo, freq->cpu); |
Todd Poynor | 74368d8 | 2012-12-23 12:28:49 -0800 | [diff] [blame] | 636 | if (!down_read_trylock(&pcpu->enable_sem)) |
| 637 | return 0; |
| 638 | if (!pcpu->governor_enabled) { |
| 639 | up_read(&pcpu->enable_sem); |
| 640 | return 0; |
| 641 | } |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 642 | |
| 643 | for_each_cpu(cpu, pcpu->policy->cpus) { |
| 644 | struct cpufreq_interactive_cpuinfo *pjcpu = |
| 645 | &per_cpu(cpuinfo, cpu); |
Lianwei Wang | fc8229c | 2013-05-16 12:07:23 +0800 | [diff] [blame] | 646 | if (cpu != freq->cpu) { |
| 647 | if (!down_read_trylock(&pjcpu->enable_sem)) |
| 648 | continue; |
| 649 | if (!pjcpu->governor_enabled) { |
| 650 | up_read(&pjcpu->enable_sem); |
| 651 | continue; |
| 652 | } |
| 653 | } |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 654 | spin_lock_irqsave(&pjcpu->load_lock, flags); |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 655 | update_load(cpu); |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 656 | spin_unlock_irqrestore(&pjcpu->load_lock, flags); |
Lianwei Wang | fc8229c | 2013-05-16 12:07:23 +0800 | [diff] [blame] | 657 | if (cpu != freq->cpu) |
| 658 | up_read(&pjcpu->enable_sem); |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 659 | } |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 660 | |
Todd Poynor | 74368d8 | 2012-12-23 12:28:49 -0800 | [diff] [blame] | 661 | up_read(&pcpu->enable_sem); |
| 662 | } |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static struct notifier_block cpufreq_notifier_block = { |
| 667 | .notifier_call = cpufreq_interactive_notifier, |
| 668 | }; |
| 669 | |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 670 | static unsigned int *get_tokenized_data(const char *buf, int *num_tokens) |
| 671 | { |
| 672 | const char *cp; |
| 673 | int i; |
| 674 | int ntokens = 1; |
| 675 | unsigned int *tokenized_data; |
Todd Poynor | 5fc17ad | 2013-03-20 15:40:46 -0700 | [diff] [blame] | 676 | int err = -EINVAL; |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 677 | |
| 678 | cp = buf; |
| 679 | while ((cp = strpbrk(cp + 1, " :"))) |
| 680 | ntokens++; |
| 681 | |
Todd Poynor | 5fc17ad | 2013-03-20 15:40:46 -0700 | [diff] [blame] | 682 | if (!(ntokens & 0x1)) |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 683 | goto err; |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 684 | |
| 685 | tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL); |
| 686 | if (!tokenized_data) { |
Todd Poynor | 5fc17ad | 2013-03-20 15:40:46 -0700 | [diff] [blame] | 687 | err = -ENOMEM; |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 688 | goto err; |
| 689 | } |
| 690 | |
| 691 | cp = buf; |
| 692 | i = 0; |
| 693 | while (i < ntokens) { |
Todd Poynor | 5fc17ad | 2013-03-20 15:40:46 -0700 | [diff] [blame] | 694 | if (sscanf(cp, "%u", &tokenized_data[i++]) != 1) |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 695 | goto err_kfree; |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 696 | |
| 697 | cp = strpbrk(cp, " :"); |
| 698 | if (!cp) |
| 699 | break; |
| 700 | cp++; |
| 701 | } |
| 702 | |
Todd Poynor | 5fc17ad | 2013-03-20 15:40:46 -0700 | [diff] [blame] | 703 | if (i != ntokens) |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 704 | goto err_kfree; |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 705 | |
| 706 | *num_tokens = ntokens; |
| 707 | return tokenized_data; |
| 708 | |
| 709 | err_kfree: |
| 710 | kfree(tokenized_data); |
| 711 | err: |
Todd Poynor | 5fc17ad | 2013-03-20 15:40:46 -0700 | [diff] [blame] | 712 | return ERR_PTR(err); |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 713 | } |
| 714 | |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 715 | static ssize_t show_target_loads( |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 716 | struct cpufreq_interactive_tunables *tunables, |
| 717 | char *buf) |
Todd Poynor | ff6cb66 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 718 | { |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 719 | int i; |
| 720 | ssize_t ret = 0; |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 721 | unsigned long flags; |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 722 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 723 | spin_lock_irqsave(&tunables->target_loads_lock, flags); |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 724 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 725 | for (i = 0; i < tunables->ntarget_loads; i++) |
| 726 | ret += sprintf(buf + ret, "%u%s", tunables->target_loads[i], |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 727 | i & 0x1 ? ":" : " "); |
| 728 | |
Chih-Wei Huang | 32582ff | 2013-12-24 17:51:55 +0800 | [diff] [blame] | 729 | sprintf(buf + ret - 1, "\n"); |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 730 | spin_unlock_irqrestore(&tunables->target_loads_lock, flags); |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 731 | return ret; |
Todd Poynor | ff6cb66 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 732 | } |
| 733 | |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 734 | static ssize_t store_target_loads( |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 735 | struct cpufreq_interactive_tunables *tunables, |
| 736 | const char *buf, size_t count) |
Todd Poynor | ff6cb66 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 737 | { |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 738 | int ntokens; |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 739 | unsigned int *new_target_loads = NULL; |
Todd Poynor | bd2b55b | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 740 | unsigned long flags; |
Todd Poynor | ff6cb66 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 741 | |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 742 | new_target_loads = get_tokenized_data(buf, &ntokens); |
| 743 | if (IS_ERR(new_target_loads)) |
| 744 | return PTR_RET(new_target_loads); |
Todd Poynor | 4fbea96 | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 745 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 746 | spin_lock_irqsave(&tunables->target_loads_lock, flags); |
| 747 | if (tunables->target_loads != default_target_loads) |
| 748 | kfree(tunables->target_loads); |
| 749 | tunables->target_loads = new_target_loads; |
| 750 | tunables->ntarget_loads = ntokens; |
| 751 | spin_unlock_irqrestore(&tunables->target_loads_lock, flags); |
Todd Poynor | ff6cb66 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 752 | return count; |
| 753 | } |
| 754 | |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 755 | static ssize_t show_above_hispeed_delay( |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 756 | struct cpufreq_interactive_tunables *tunables, char *buf) |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 757 | { |
| 758 | int i; |
| 759 | ssize_t ret = 0; |
| 760 | unsigned long flags; |
| 761 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 762 | spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags); |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 763 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 764 | for (i = 0; i < tunables->nabove_hispeed_delay; i++) |
| 765 | ret += sprintf(buf + ret, "%u%s", |
| 766 | tunables->above_hispeed_delay[i], |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 767 | i & 0x1 ? ":" : " "); |
| 768 | |
Chih-Wei Huang | 32582ff | 2013-12-24 17:51:55 +0800 | [diff] [blame] | 769 | sprintf(buf + ret - 1, "\n"); |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 770 | spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags); |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | static ssize_t store_above_hispeed_delay( |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 775 | struct cpufreq_interactive_tunables *tunables, |
| 776 | const char *buf, size_t count) |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 777 | { |
| 778 | int ntokens; |
| 779 | unsigned int *new_above_hispeed_delay = NULL; |
| 780 | unsigned long flags; |
| 781 | |
| 782 | new_above_hispeed_delay = get_tokenized_data(buf, &ntokens); |
| 783 | if (IS_ERR(new_above_hispeed_delay)) |
| 784 | return PTR_RET(new_above_hispeed_delay); |
| 785 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 786 | spin_lock_irqsave(&tunables->above_hispeed_delay_lock, flags); |
| 787 | if (tunables->above_hispeed_delay != default_above_hispeed_delay) |
| 788 | kfree(tunables->above_hispeed_delay); |
| 789 | tunables->above_hispeed_delay = new_above_hispeed_delay; |
| 790 | tunables->nabove_hispeed_delay = ntokens; |
| 791 | spin_unlock_irqrestore(&tunables->above_hispeed_delay_lock, flags); |
Minsung Kim | 700119b | 2013-02-25 23:48:04 +0900 | [diff] [blame] | 792 | return count; |
| 793 | |
| 794 | } |
| 795 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 796 | static ssize_t show_hispeed_freq(struct cpufreq_interactive_tunables *tunables, |
| 797 | char *buf) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 798 | { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 799 | return sprintf(buf, "%u\n", tunables->hispeed_freq); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 802 | static ssize_t store_hispeed_freq(struct cpufreq_interactive_tunables *tunables, |
| 803 | const char *buf, size_t count) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 804 | { |
| 805 | int ret; |
Todd Poynor | df284d9 | 2012-10-03 00:39:56 -0700 | [diff] [blame] | 806 | long unsigned int val; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 807 | |
Amit Pundir | dcc2f2b | 2015-11-03 20:53:29 +0530 | [diff] [blame] | 808 | ret = kstrtoul(buf, 0, &val); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 809 | if (ret < 0) |
| 810 | return ret; |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 811 | tunables->hispeed_freq = val; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 812 | return count; |
| 813 | } |
| 814 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 815 | static ssize_t show_go_hispeed_load(struct cpufreq_interactive_tunables |
| 816 | *tunables, char *buf) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 817 | { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 818 | return sprintf(buf, "%lu\n", tunables->go_hispeed_load); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 821 | static ssize_t store_go_hispeed_load(struct cpufreq_interactive_tunables |
| 822 | *tunables, const char *buf, size_t count) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 823 | { |
| 824 | int ret; |
| 825 | unsigned long val; |
| 826 | |
Amit Pundir | dcc2f2b | 2015-11-03 20:53:29 +0530 | [diff] [blame] | 827 | ret = kstrtoul(buf, 0, &val); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 828 | if (ret < 0) |
| 829 | return ret; |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 830 | tunables->go_hispeed_load = val; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 831 | return count; |
| 832 | } |
| 833 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 834 | static ssize_t show_min_sample_time(struct cpufreq_interactive_tunables |
| 835 | *tunables, char *buf) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 836 | { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 837 | return sprintf(buf, "%lu\n", tunables->min_sample_time); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 840 | static ssize_t store_min_sample_time(struct cpufreq_interactive_tunables |
| 841 | *tunables, const char *buf, size_t count) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 842 | { |
| 843 | int ret; |
| 844 | unsigned long val; |
| 845 | |
Amit Pundir | dcc2f2b | 2015-11-03 20:53:29 +0530 | [diff] [blame] | 846 | ret = kstrtoul(buf, 0, &val); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 847 | if (ret < 0) |
| 848 | return ret; |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 849 | tunables->min_sample_time = val; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 850 | return count; |
| 851 | } |
| 852 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 853 | static ssize_t show_timer_rate(struct cpufreq_interactive_tunables *tunables, |
| 854 | char *buf) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 855 | { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 856 | return sprintf(buf, "%lu\n", tunables->timer_rate); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 859 | static ssize_t store_timer_rate(struct cpufreq_interactive_tunables *tunables, |
| 860 | const char *buf, size_t count) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 861 | { |
| 862 | int ret; |
Junjie Wu | 6f4610d | 2014-08-15 16:34:37 -0700 | [diff] [blame] | 863 | unsigned long val, val_round; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 864 | |
Amit Pundir | dcc2f2b | 2015-11-03 20:53:29 +0530 | [diff] [blame] | 865 | ret = kstrtoul(buf, 0, &val); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 866 | if (ret < 0) |
| 867 | return ret; |
Junjie Wu | 6f4610d | 2014-08-15 16:34:37 -0700 | [diff] [blame] | 868 | |
| 869 | val_round = jiffies_to_usecs(usecs_to_jiffies(val)); |
| 870 | if (val != val_round) |
| 871 | pr_warn("timer_rate not aligned to jiffy. Rounded up to %lu\n", |
| 872 | val_round); |
| 873 | |
| 874 | tunables->timer_rate = val_round; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 875 | return count; |
| 876 | } |
| 877 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 878 | static ssize_t show_timer_slack(struct cpufreq_interactive_tunables *tunables, |
| 879 | char *buf) |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 880 | { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 881 | return sprintf(buf, "%d\n", tunables->timer_slack_val); |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 884 | static ssize_t store_timer_slack(struct cpufreq_interactive_tunables *tunables, |
| 885 | const char *buf, size_t count) |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 886 | { |
| 887 | int ret; |
| 888 | unsigned long val; |
| 889 | |
| 890 | ret = kstrtol(buf, 10, &val); |
| 891 | if (ret < 0) |
| 892 | return ret; |
| 893 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 894 | tunables->timer_slack_val = val; |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 895 | return count; |
| 896 | } |
| 897 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 898 | static ssize_t show_boost(struct cpufreq_interactive_tunables *tunables, |
Todd Poynor | 199620b | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 899 | char *buf) |
| 900 | { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 901 | return sprintf(buf, "%d\n", tunables->boost_val); |
Todd Poynor | 199620b | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 904 | static ssize_t store_boost(struct cpufreq_interactive_tunables *tunables, |
Todd Poynor | 199620b | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 905 | const char *buf, size_t count) |
| 906 | { |
| 907 | int ret; |
| 908 | unsigned long val; |
| 909 | |
| 910 | ret = kstrtoul(buf, 0, &val); |
| 911 | if (ret < 0) |
| 912 | return ret; |
| 913 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 914 | tunables->boost_val = val; |
Todd Poynor | 199620b | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 915 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 916 | if (tunables->boost_val) { |
Todd Poynor | b905452 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 917 | trace_cpufreq_interactive_boost("on"); |
Lianwei Wang | 4f81d19 | 2014-12-02 17:20:50 -0800 | [diff] [blame] | 918 | if (!tunables->boosted) |
| 919 | cpufreq_interactive_boost(tunables); |
Todd Poynor | b905452 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 920 | } else { |
Ruchi Kandoi | e68985a | 2014-04-09 16:47:59 -0700 | [diff] [blame] | 921 | tunables->boostpulse_endtime = ktime_to_us(ktime_get()); |
Todd Poynor | b905452 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 922 | trace_cpufreq_interactive_unboost("off"); |
| 923 | } |
Todd Poynor | 199620b | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 924 | |
| 925 | return count; |
| 926 | } |
| 927 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 928 | static ssize_t store_boostpulse(struct cpufreq_interactive_tunables *tunables, |
Todd Poynor | b905452 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 929 | const char *buf, size_t count) |
| 930 | { |
| 931 | int ret; |
| 932 | unsigned long val; |
| 933 | |
| 934 | ret = kstrtoul(buf, 0, &val); |
| 935 | if (ret < 0) |
| 936 | return ret; |
| 937 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 938 | tunables->boostpulse_endtime = ktime_to_us(ktime_get()) + |
| 939 | tunables->boostpulse_duration_val; |
Todd Poynor | b905452 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 940 | trace_cpufreq_interactive_boost("pulse"); |
Lianwei Wang | 4f81d19 | 2014-12-02 17:20:50 -0800 | [diff] [blame] | 941 | if (!tunables->boosted) |
| 942 | cpufreq_interactive_boost(tunables); |
Todd Poynor | b905452 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 943 | return count; |
| 944 | } |
| 945 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 946 | static ssize_t show_boostpulse_duration(struct cpufreq_interactive_tunables |
| 947 | *tunables, char *buf) |
Todd Poynor | eda4029 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 948 | { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 949 | return sprintf(buf, "%d\n", tunables->boostpulse_duration_val); |
Todd Poynor | eda4029 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 950 | } |
| 951 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 952 | static ssize_t store_boostpulse_duration(struct cpufreq_interactive_tunables |
| 953 | *tunables, const char *buf, size_t count) |
Todd Poynor | eda4029 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 954 | { |
| 955 | int ret; |
| 956 | unsigned long val; |
| 957 | |
| 958 | ret = kstrtoul(buf, 0, &val); |
| 959 | if (ret < 0) |
| 960 | return ret; |
| 961 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 962 | tunables->boostpulse_duration_val = val; |
Todd Poynor | eda4029 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 963 | return count; |
| 964 | } |
| 965 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 966 | static ssize_t show_io_is_busy(struct cpufreq_interactive_tunables *tunables, |
| 967 | char *buf) |
Lianwei Wang | 2747480 | 2013-02-22 11:39:18 +0800 | [diff] [blame] | 968 | { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 969 | return sprintf(buf, "%u\n", tunables->io_is_busy); |
Lianwei Wang | 2747480 | 2013-02-22 11:39:18 +0800 | [diff] [blame] | 970 | } |
| 971 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 972 | static ssize_t store_io_is_busy(struct cpufreq_interactive_tunables *tunables, |
| 973 | const char *buf, size_t count) |
Lianwei Wang | 2747480 | 2013-02-22 11:39:18 +0800 | [diff] [blame] | 974 | { |
| 975 | int ret; |
| 976 | unsigned long val; |
| 977 | |
| 978 | ret = kstrtoul(buf, 0, &val); |
| 979 | if (ret < 0) |
| 980 | return ret; |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 981 | tunables->io_is_busy = val; |
Lianwei Wang | 2747480 | 2013-02-22 11:39:18 +0800 | [diff] [blame] | 982 | return count; |
| 983 | } |
| 984 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 985 | /* |
| 986 | * Create show/store routines |
| 987 | * - sys: One governor instance for complete SYSTEM |
| 988 | * - pol: One governor instance per struct cpufreq_policy |
| 989 | */ |
| 990 | #define show_gov_pol_sys(file_name) \ |
| 991 | static ssize_t show_##file_name##_gov_sys \ |
| 992 | (struct kobject *kobj, struct attribute *attr, char *buf) \ |
| 993 | { \ |
| 994 | return show_##file_name(common_tunables, buf); \ |
| 995 | } \ |
| 996 | \ |
| 997 | static ssize_t show_##file_name##_gov_pol \ |
| 998 | (struct cpufreq_policy *policy, char *buf) \ |
| 999 | { \ |
| 1000 | return show_##file_name(policy->governor_data, buf); \ |
| 1001 | } |
Lianwei Wang | 2747480 | 2013-02-22 11:39:18 +0800 | [diff] [blame] | 1002 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 1003 | #define store_gov_pol_sys(file_name) \ |
| 1004 | static ssize_t store_##file_name##_gov_sys \ |
| 1005 | (struct kobject *kobj, struct attribute *attr, const char *buf, \ |
| 1006 | size_t count) \ |
| 1007 | { \ |
| 1008 | return store_##file_name(common_tunables, buf, count); \ |
| 1009 | } \ |
| 1010 | \ |
| 1011 | static ssize_t store_##file_name##_gov_pol \ |
| 1012 | (struct cpufreq_policy *policy, const char *buf, size_t count) \ |
| 1013 | { \ |
| 1014 | return store_##file_name(policy->governor_data, buf, count); \ |
| 1015 | } |
| 1016 | |
| 1017 | #define show_store_gov_pol_sys(file_name) \ |
| 1018 | show_gov_pol_sys(file_name); \ |
| 1019 | store_gov_pol_sys(file_name) |
| 1020 | |
| 1021 | show_store_gov_pol_sys(target_loads); |
| 1022 | show_store_gov_pol_sys(above_hispeed_delay); |
| 1023 | show_store_gov_pol_sys(hispeed_freq); |
| 1024 | show_store_gov_pol_sys(go_hispeed_load); |
| 1025 | show_store_gov_pol_sys(min_sample_time); |
| 1026 | show_store_gov_pol_sys(timer_rate); |
| 1027 | show_store_gov_pol_sys(timer_slack); |
| 1028 | show_store_gov_pol_sys(boost); |
| 1029 | store_gov_pol_sys(boostpulse); |
| 1030 | show_store_gov_pol_sys(boostpulse_duration); |
| 1031 | show_store_gov_pol_sys(io_is_busy); |
| 1032 | |
| 1033 | #define gov_sys_attr_rw(_name) \ |
| 1034 | static struct global_attr _name##_gov_sys = \ |
| 1035 | __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys) |
| 1036 | |
| 1037 | #define gov_pol_attr_rw(_name) \ |
| 1038 | static struct freq_attr _name##_gov_pol = \ |
| 1039 | __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol) |
| 1040 | |
| 1041 | #define gov_sys_pol_attr_rw(_name) \ |
| 1042 | gov_sys_attr_rw(_name); \ |
| 1043 | gov_pol_attr_rw(_name) |
| 1044 | |
| 1045 | gov_sys_pol_attr_rw(target_loads); |
| 1046 | gov_sys_pol_attr_rw(above_hispeed_delay); |
| 1047 | gov_sys_pol_attr_rw(hispeed_freq); |
| 1048 | gov_sys_pol_attr_rw(go_hispeed_load); |
| 1049 | gov_sys_pol_attr_rw(min_sample_time); |
| 1050 | gov_sys_pol_attr_rw(timer_rate); |
| 1051 | gov_sys_pol_attr_rw(timer_slack); |
| 1052 | gov_sys_pol_attr_rw(boost); |
| 1053 | gov_sys_pol_attr_rw(boostpulse_duration); |
| 1054 | gov_sys_pol_attr_rw(io_is_busy); |
| 1055 | |
| 1056 | static struct global_attr boostpulse_gov_sys = |
| 1057 | __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_sys); |
| 1058 | |
| 1059 | static struct freq_attr boostpulse_gov_pol = |
| 1060 | __ATTR(boostpulse, 0200, NULL, store_boostpulse_gov_pol); |
| 1061 | |
| 1062 | /* One Governor instance for entire system */ |
| 1063 | static struct attribute *interactive_attributes_gov_sys[] = { |
| 1064 | &target_loads_gov_sys.attr, |
| 1065 | &above_hispeed_delay_gov_sys.attr, |
| 1066 | &hispeed_freq_gov_sys.attr, |
| 1067 | &go_hispeed_load_gov_sys.attr, |
| 1068 | &min_sample_time_gov_sys.attr, |
| 1069 | &timer_rate_gov_sys.attr, |
| 1070 | &timer_slack_gov_sys.attr, |
| 1071 | &boost_gov_sys.attr, |
| 1072 | &boostpulse_gov_sys.attr, |
| 1073 | &boostpulse_duration_gov_sys.attr, |
| 1074 | &io_is_busy_gov_sys.attr, |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1075 | NULL, |
| 1076 | }; |
| 1077 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 1078 | static struct attribute_group interactive_attr_group_gov_sys = { |
| 1079 | .attrs = interactive_attributes_gov_sys, |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1080 | .name = "interactive", |
| 1081 | }; |
| 1082 | |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 1083 | /* Per policy governor instance */ |
| 1084 | static struct attribute *interactive_attributes_gov_pol[] = { |
| 1085 | &target_loads_gov_pol.attr, |
| 1086 | &above_hispeed_delay_gov_pol.attr, |
| 1087 | &hispeed_freq_gov_pol.attr, |
| 1088 | &go_hispeed_load_gov_pol.attr, |
| 1089 | &min_sample_time_gov_pol.attr, |
| 1090 | &timer_rate_gov_pol.attr, |
| 1091 | &timer_slack_gov_pol.attr, |
| 1092 | &boost_gov_pol.attr, |
| 1093 | &boostpulse_gov_pol.attr, |
| 1094 | &boostpulse_duration_gov_pol.attr, |
| 1095 | &io_is_busy_gov_pol.attr, |
| 1096 | NULL, |
| 1097 | }; |
| 1098 | |
| 1099 | static struct attribute_group interactive_attr_group_gov_pol = { |
| 1100 | .attrs = interactive_attributes_gov_pol, |
| 1101 | .name = "interactive", |
| 1102 | }; |
| 1103 | |
| 1104 | static struct attribute_group *get_sysfs_attr(void) |
| 1105 | { |
| 1106 | if (have_governor_per_policy()) |
| 1107 | return &interactive_attr_group_gov_pol; |
| 1108 | else |
| 1109 | return &interactive_attr_group_gov_sys; |
| 1110 | } |
| 1111 | |
Sam Leffler | f61219a | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 1112 | static int cpufreq_interactive_idle_notifier(struct notifier_block *nb, |
| 1113 | unsigned long val, |
| 1114 | void *data) |
| 1115 | { |
Rohit Gupta | dc42410 | 2015-03-06 18:46:04 -0800 | [diff] [blame] | 1116 | if (val == IDLE_END) |
Sam Leffler | f61219a | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 1117 | cpufreq_interactive_idle_end(); |
Sam Leffler | f61219a | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 1118 | |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | static struct notifier_block cpufreq_interactive_idle_nb = { |
| 1123 | .notifier_call = cpufreq_interactive_idle_notifier, |
| 1124 | }; |
| 1125 | |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 1126 | static int cpufreq_governor_interactive_init(struct cpufreq_policy *policy) |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1127 | { |
| 1128 | int rc; |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 1129 | struct cpufreq_interactive_tunables *tunables; |
| 1130 | |
| 1131 | if (have_governor_per_policy()) |
| 1132 | tunables = policy->governor_data; |
| 1133 | else |
| 1134 | tunables = common_tunables; |
| 1135 | |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 1136 | if (have_governor_per_policy()) { |
| 1137 | WARN_ON(tunables); |
| 1138 | } else if (tunables) { |
| 1139 | tunables->usage_count++; |
Minsung Kim | 4f6c7ec | 2014-01-19 14:32:42 +0900 | [diff] [blame] | 1140 | policy->governor_data = tunables; |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 1141 | return 0; |
| 1142 | } |
| 1143 | |
| 1144 | tunables = kzalloc(sizeof(*tunables), GFP_KERNEL); |
| 1145 | if (!tunables) { |
| 1146 | pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__); |
| 1147 | return -ENOMEM; |
| 1148 | } |
| 1149 | |
| 1150 | tunables->usage_count = 1; |
| 1151 | tunables->above_hispeed_delay = default_above_hispeed_delay; |
| 1152 | tunables->nabove_hispeed_delay = |
| 1153 | ARRAY_SIZE(default_above_hispeed_delay); |
| 1154 | tunables->go_hispeed_load = DEFAULT_GO_HISPEED_LOAD; |
| 1155 | tunables->target_loads = default_target_loads; |
| 1156 | tunables->ntarget_loads = ARRAY_SIZE(default_target_loads); |
| 1157 | tunables->min_sample_time = DEFAULT_MIN_SAMPLE_TIME; |
| 1158 | tunables->timer_rate = DEFAULT_TIMER_RATE; |
| 1159 | tunables->boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME; |
| 1160 | tunables->timer_slack_val = DEFAULT_TIMER_SLACK; |
| 1161 | |
| 1162 | spin_lock_init(&tunables->target_loads_lock); |
| 1163 | spin_lock_init(&tunables->above_hispeed_delay_lock); |
| 1164 | |
| 1165 | policy->governor_data = tunables; |
| 1166 | if (!have_governor_per_policy()) { |
| 1167 | common_tunables = tunables; |
| 1168 | } |
| 1169 | |
| 1170 | rc = sysfs_create_group(get_governor_parent_kobj(policy), |
| 1171 | get_sysfs_attr()); |
| 1172 | if (rc) { |
| 1173 | kfree(tunables); |
| 1174 | policy->governor_data = NULL; |
Greg Hackmann | e2c6c88 | 2014-12-08 10:08:35 -0800 | [diff] [blame] | 1175 | if (!have_governor_per_policy()) { |
Viresh Kumar | b9c5075 | 2013-05-16 14:58:54 +0530 | [diff] [blame] | 1176 | common_tunables = NULL; |
| 1177 | } |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 1178 | return rc; |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1179 | } |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 1180 | |
| 1181 | idle_notifier_register(&cpufreq_interactive_idle_nb); |
| 1182 | cpufreq_register_notifier(&cpufreq_notifier_block, |
| 1183 | CPUFREQ_TRANSITION_NOTIFIER); |
| 1184 | |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1185 | return 0; |
| 1186 | } |
| 1187 | |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 1188 | static void cpufreq_governor_interactive_exit(struct cpufreq_policy *policy) |
| 1189 | { |
| 1190 | struct cpufreq_interactive_tunables *tunables; |
| 1191 | |
| 1192 | if (have_governor_per_policy()) |
| 1193 | tunables = policy->governor_data; |
| 1194 | else |
| 1195 | tunables = common_tunables; |
| 1196 | |
| 1197 | WARN_ON(!tunables); |
| 1198 | |
| 1199 | if (!--tunables->usage_count) { |
| 1200 | cpufreq_unregister_notifier(&cpufreq_notifier_block, |
| 1201 | CPUFREQ_TRANSITION_NOTIFIER); |
| 1202 | idle_notifier_unregister(&cpufreq_interactive_idle_nb); |
| 1203 | |
| 1204 | sysfs_remove_group(get_governor_parent_kobj(policy), |
| 1205 | get_sysfs_attr()); |
| 1206 | |
| 1207 | kfree(tunables); |
| 1208 | common_tunables = NULL; |
| 1209 | } |
| 1210 | |
| 1211 | policy->governor_data = NULL; |
| 1212 | } |
| 1213 | |
| 1214 | static int cpufreq_governor_interactive_start(struct cpufreq_policy *policy) |
| 1215 | { |
| 1216 | unsigned int j; |
| 1217 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 1218 | struct cpufreq_frequency_table *freq_table; |
| 1219 | struct cpufreq_interactive_tunables *tunables; |
| 1220 | |
| 1221 | if (have_governor_per_policy()) |
| 1222 | tunables = policy->governor_data; |
| 1223 | else |
| 1224 | tunables = common_tunables; |
| 1225 | |
| 1226 | WARN_ON(!tunables); |
| 1227 | |
| 1228 | mutex_lock(&gov_lock); |
| 1229 | |
| 1230 | freq_table = policy->freq_table; |
| 1231 | if (!tunables->hispeed_freq) |
| 1232 | tunables->hispeed_freq = policy->max; |
| 1233 | |
| 1234 | for_each_cpu(j, policy->cpus) { |
| 1235 | pcpu = &per_cpu(cpuinfo, j); |
| 1236 | pcpu->policy = policy; |
| 1237 | pcpu->target_freq = policy->cur; |
| 1238 | pcpu->freq_table = freq_table; |
| 1239 | pcpu->floor_freq = pcpu->target_freq; |
| 1240 | pcpu->pol_floor_val_time = |
| 1241 | ktime_to_us(ktime_get()); |
| 1242 | pcpu->loc_floor_val_time = pcpu->pol_floor_val_time; |
| 1243 | pcpu->pol_hispeed_val_time = pcpu->pol_floor_val_time; |
| 1244 | pcpu->loc_hispeed_val_time = pcpu->pol_floor_val_time; |
| 1245 | down_write(&pcpu->enable_sem); |
| 1246 | del_timer_sync(&pcpu->cpu_timer); |
| 1247 | del_timer_sync(&pcpu->cpu_slack_timer); |
| 1248 | cpufreq_interactive_timer_start(tunables, j); |
| 1249 | pcpu->governor_enabled = 1; |
| 1250 | up_write(&pcpu->enable_sem); |
| 1251 | } |
| 1252 | |
| 1253 | mutex_unlock(&gov_lock); |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static void cpufreq_governor_interactive_stop(struct cpufreq_policy *policy) |
| 1258 | { |
| 1259 | unsigned int j; |
| 1260 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 1261 | |
| 1262 | mutex_lock(&gov_lock); |
| 1263 | for_each_cpu(j, policy->cpus) { |
| 1264 | pcpu = &per_cpu(cpuinfo, j); |
| 1265 | down_write(&pcpu->enable_sem); |
| 1266 | pcpu->governor_enabled = 0; |
| 1267 | del_timer_sync(&pcpu->cpu_timer); |
| 1268 | del_timer_sync(&pcpu->cpu_slack_timer); |
| 1269 | up_write(&pcpu->enable_sem); |
| 1270 | } |
| 1271 | |
| 1272 | mutex_unlock(&gov_lock); |
| 1273 | } |
| 1274 | |
| 1275 | static void cpufreq_governor_interactive_limits(struct cpufreq_policy *policy) |
| 1276 | { |
| 1277 | unsigned int j; |
| 1278 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 1279 | unsigned long flags; |
| 1280 | |
| 1281 | if (policy->max < policy->cur) |
| 1282 | __cpufreq_driver_target(policy, |
| 1283 | policy->max, CPUFREQ_RELATION_H); |
| 1284 | else if (policy->min > policy->cur) |
| 1285 | __cpufreq_driver_target(policy, |
| 1286 | policy->min, CPUFREQ_RELATION_L); |
| 1287 | for_each_cpu(j, policy->cpus) { |
| 1288 | pcpu = &per_cpu(cpuinfo, j); |
| 1289 | |
| 1290 | down_read(&pcpu->enable_sem); |
| 1291 | if (pcpu->governor_enabled == 0) { |
| 1292 | up_read(&pcpu->enable_sem); |
| 1293 | continue; |
| 1294 | } |
| 1295 | |
| 1296 | spin_lock_irqsave(&pcpu->target_freq_lock, flags); |
| 1297 | if (policy->max < pcpu->target_freq) |
| 1298 | pcpu->target_freq = policy->max; |
| 1299 | else if (policy->min > pcpu->target_freq) |
| 1300 | pcpu->target_freq = policy->min; |
| 1301 | |
| 1302 | spin_unlock_irqrestore(&pcpu->target_freq_lock, flags); |
| 1303 | up_read(&pcpu->enable_sem); |
| 1304 | } |
| 1305 | } |
| 1306 | |
Amit Pundir | 67aa284 | 2016-08-12 13:17:25 +0530 | [diff] [blame] | 1307 | static struct cpufreq_governor cpufreq_gov_interactive = { |
Viresh Kumar | cba75f5 | 2013-05-16 14:58:53 +0530 | [diff] [blame] | 1308 | .name = "interactive", |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 1309 | .init = cpufreq_governor_interactive_init, |
| 1310 | .exit = cpufreq_governor_interactive_exit, |
| 1311 | .start = cpufreq_governor_interactive_start, |
| 1312 | .stop = cpufreq_governor_interactive_stop, |
| 1313 | .limits = cpufreq_governor_interactive_limits, |
Viresh Kumar | cba75f5 | 2013-05-16 14:58:53 +0530 | [diff] [blame] | 1314 | .max_transition_latency = 10000000, |
| 1315 | .owner = THIS_MODULE, |
| 1316 | }; |
| 1317 | |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 1318 | static void cpufreq_interactive_nop_timer(unsigned long data) |
| 1319 | { |
| 1320 | } |
| 1321 | |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1322 | static int __init cpufreq_interactive_init(void) |
| 1323 | { |
| 1324 | unsigned int i; |
| 1325 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 1326 | struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
| 1327 | |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1328 | /* Initalize per-cpu timers */ |
| 1329 | for_each_possible_cpu(i) { |
| 1330 | pcpu = &per_cpu(cpuinfo, i); |
Amit Pundir | 5e3ce74 | 2016-08-12 17:12:36 +0530 | [diff] [blame] | 1331 | init_timer_pinned_deferrable(&pcpu->cpu_timer); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1332 | pcpu->cpu_timer.function = cpufreq_interactive_timer; |
| 1333 | pcpu->cpu_timer.data = i; |
Todd Poynor | 4707840 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 1334 | init_timer(&pcpu->cpu_slack_timer); |
| 1335 | pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer; |
Todd Poynor | 4ceca14 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 1336 | spin_lock_init(&pcpu->load_lock); |
Badhri Jagan Sridharan | d58bb95 | 2014-04-07 18:26:30 -0700 | [diff] [blame] | 1337 | spin_lock_init(&pcpu->target_freq_lock); |
Todd Poynor | 4235053 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 1338 | init_rwsem(&pcpu->enable_sem); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1341 | spin_lock_init(&speedchange_cpumask_lock); |
Lianwei Wang | 6becfaa | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1342 | mutex_init(&gov_lock); |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1343 | speedchange_task = |
| 1344 | kthread_create(cpufreq_interactive_speedchange_task, NULL, |
| 1345 | "cfinteractive"); |
| 1346 | if (IS_ERR(speedchange_task)) |
| 1347 | return PTR_ERR(speedchange_task); |
Sam Leffler | 1b89756 | 2012-06-27 12:55:56 -0700 | [diff] [blame] | 1348 | |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1349 | sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m); |
| 1350 | get_task_struct(speedchange_task); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1351 | |
Sam Leffler | 1b89756 | 2012-06-27 12:55:56 -0700 | [diff] [blame] | 1352 | /* NB: wake up so the thread does not look hung to the freezer */ |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1353 | wake_up_process(speedchange_task); |
Sam Leffler | 1b89756 | 2012-06-27 12:55:56 -0700 | [diff] [blame] | 1354 | |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1355 | return cpufreq_register_governor(&cpufreq_gov_interactive); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE |
Amit Pundir | 67aa284 | 2016-08-12 13:17:25 +0530 | [diff] [blame] | 1359 | struct cpufreq_governor *cpufreq_default_governor(void) |
| 1360 | { |
| 1361 | return &cpufreq_gov_interactive; |
| 1362 | } |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1363 | fs_initcall(cpufreq_interactive_init); |
| 1364 | #else |
| 1365 | module_init(cpufreq_interactive_init); |
| 1366 | #endif |
| 1367 | |
| 1368 | static void __exit cpufreq_interactive_exit(void) |
| 1369 | { |
| 1370 | cpufreq_unregister_governor(&cpufreq_gov_interactive); |
Todd Poynor | a21083d | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1371 | kthread_stop(speedchange_task); |
| 1372 | put_task_struct(speedchange_task); |
Mike Chan | 34a9e02 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | module_exit(cpufreq_interactive_exit); |
| 1376 | |
| 1377 | MODULE_AUTHOR("Mike Chan <mike@android.com>"); |
| 1378 | MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for " |
| 1379 | "Latency sensitive workloads"); |
| 1380 | MODULE_LICENSE("GPL"); |