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