Mike Chan | 9d49b70 | 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 | d4edd5d | 2012-11-01 09:59:52 +0800 | [diff] [blame] | 23 | #include <linux/moduleparam.h> |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 24 | #include <linux/rwsem.h> |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 25 | #include <linux/sched.h> |
| 26 | #include <linux/tick.h> |
| 27 | #include <linux/time.h> |
| 28 | #include <linux/timer.h> |
| 29 | #include <linux/workqueue.h> |
| 30 | #include <linux/kthread.h> |
Todd Poynor | 7820a65 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 31 | #include <linux/slab.h> |
Todd Poynor | 9fb1531 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 32 | #include <asm/cputime.h> |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 33 | |
Todd Poynor | a1e1951 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 34 | #define CREATE_TRACE_POINTS |
| 35 | #include <trace/events/cpufreq_interactive.h> |
| 36 | |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 37 | static int active_count; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 38 | |
| 39 | struct cpufreq_interactive_cpuinfo { |
| 40 | struct timer_list cpu_timer; |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 41 | struct timer_list cpu_slack_timer; |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 42 | spinlock_t load_lock; /* protects the next 4 fields */ |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 43 | u64 time_in_idle; |
Todd Poynor | ae7f28c | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 44 | u64 time_in_idle_timestamp; |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 45 | u64 cputime_speedadj; |
| 46 | u64 cputime_speedadj_timestamp; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 47 | struct cpufreq_policy *policy; |
| 48 | struct cpufreq_frequency_table *freq_table; |
| 49 | unsigned int target_freq; |
Todd Poynor | aad2732 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 50 | unsigned int floor_freq; |
| 51 | u64 floor_validate_time; |
Todd Poynor | 5a5aa70 | 2012-05-10 23:28:06 -0700 | [diff] [blame] | 52 | u64 hispeed_validate_time; |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 53 | struct rw_semaphore enable_sem; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 54 | int governor_enabled; |
Steve Kondik | a437194 | 2012-10-10 12:20:25 -0700 | [diff] [blame] | 55 | int cpu_load; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static DEFINE_PER_CPU(struct cpufreq_interactive_cpuinfo, cpuinfo); |
| 59 | |
Todd Poynor | 02442cf | 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 | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 64 | static struct mutex gov_lock; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 65 | |
| 66 | /* Hi speed to bump to from lo speed when load burst (default max) */ |
Todd Poynor | f090ef0 | 2012-10-03 00:39:56 -0700 | [diff] [blame] | 67 | static unsigned int hispeed_freq; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 68 | |
| 69 | /* Go to hi speed when CPU load at or above this value. */ |
Todd Poynor | ed02868 | 2012-12-21 15:13:01 -0800 | [diff] [blame] | 70 | #define DEFAULT_GO_HISPEED_LOAD 99 |
Todd Poynor | 9f58f84 | 2012-12-21 15:32:21 -0800 | [diff] [blame] | 71 | static unsigned long go_hispeed_load = DEFAULT_GO_HISPEED_LOAD; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 72 | |
Todd Poynor | 6ecca11 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 73 | /* Target load. Lower values result in higher CPU speeds. */ |
| 74 | #define DEFAULT_TARGET_LOAD 90 |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 75 | static unsigned int default_target_loads[] = {DEFAULT_TARGET_LOAD}; |
| 76 | static spinlock_t target_loads_lock; |
| 77 | static unsigned int *target_loads = default_target_loads; |
| 78 | static int ntarget_loads = ARRAY_SIZE(default_target_loads); |
Todd Poynor | 6ecca11 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 79 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 80 | /* |
| 81 | * The minimum amount of time to spend at a frequency before we can ramp down. |
| 82 | */ |
Todd Poynor | a0ec436 | 2012-04-17 17:39:34 -0700 | [diff] [blame] | 83 | #define DEFAULT_MIN_SAMPLE_TIME (80 * USEC_PER_MSEC) |
Todd Poynor | 9f58f84 | 2012-12-21 15:32:21 -0800 | [diff] [blame] | 84 | static unsigned long min_sample_time = DEFAULT_MIN_SAMPLE_TIME; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * The sample rate of the timer used to increase frequency |
| 88 | */ |
Todd Poynor | a0ec436 | 2012-04-17 17:39:34 -0700 | [diff] [blame] | 89 | #define DEFAULT_TIMER_RATE (20 * USEC_PER_MSEC) |
Todd Poynor | 9f58f84 | 2012-12-21 15:32:21 -0800 | [diff] [blame] | 90 | static unsigned long timer_rate = DEFAULT_TIMER_RATE; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 91 | |
Todd Poynor | 596cf1f | 2012-04-13 20:18:02 -0700 | [diff] [blame] | 92 | /* |
| 93 | * Wait this long before raising speed above hispeed, by default a single |
| 94 | * timer interval. |
| 95 | */ |
| 96 | #define DEFAULT_ABOVE_HISPEED_DELAY DEFAULT_TIMER_RATE |
Minsung Kim | cce741d | 2013-02-25 23:48:04 +0900 | [diff] [blame^] | 97 | static unsigned int default_above_hispeed_delay[] = { |
| 98 | DEFAULT_ABOVE_HISPEED_DELAY }; |
| 99 | static spinlock_t above_hispeed_delay_lock; |
| 100 | static unsigned int *above_hispeed_delay = default_above_hispeed_delay; |
| 101 | static int nabove_hispeed_delay = ARRAY_SIZE(default_above_hispeed_delay); |
Todd Poynor | 596cf1f | 2012-04-13 20:18:02 -0700 | [diff] [blame] | 102 | |
Todd Poynor | 2983547 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 103 | /* Non-zero means indefinite speed boost active */ |
Todd Poynor | 9fb1531 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 104 | static int boost_val; |
Todd Poynor | 2983547 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 105 | /* Duration of a boot pulse in usecs */ |
| 106 | static int boostpulse_duration_val = DEFAULT_MIN_SAMPLE_TIME; |
| 107 | /* End time of boost pulse in ktime converted to usecs */ |
| 108 | static u64 boostpulse_endtime; |
Todd Poynor | 9fb1531 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 109 | |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 110 | /* |
| 111 | * Max additional time to wait in idle, beyond timer_rate, at speeds above |
| 112 | * minimum before wakeup to reduce speed, or -1 if unnecessary. |
| 113 | */ |
| 114 | #define DEFAULT_TIMER_SLACK (4 * DEFAULT_TIMER_RATE) |
| 115 | static int timer_slack_val = DEFAULT_TIMER_SLACK; |
Lianwei Wang | d4edd5d | 2012-11-01 09:59:52 +0800 | [diff] [blame] | 116 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 117 | static int cpufreq_governor_interactive(struct cpufreq_policy *policy, |
| 118 | unsigned int event); |
| 119 | |
| 120 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE |
| 121 | static |
| 122 | #endif |
| 123 | struct cpufreq_governor cpufreq_gov_interactive = { |
| 124 | .name = "interactive", |
| 125 | .governor = cpufreq_governor_interactive, |
| 126 | .max_transition_latency = 10000000, |
| 127 | .owner = THIS_MODULE, |
| 128 | }; |
| 129 | |
Todd Poynor | ae7f28c | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 130 | static void cpufreq_interactive_timer_resched( |
| 131 | struct cpufreq_interactive_cpuinfo *pcpu) |
| 132 | { |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 133 | unsigned long expires = jiffies + usecs_to_jiffies(timer_rate); |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 134 | unsigned long flags; |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 135 | |
| 136 | mod_timer_pinned(&pcpu->cpu_timer, expires); |
| 137 | if (timer_slack_val >= 0 && pcpu->target_freq > pcpu->policy->min) { |
| 138 | expires += usecs_to_jiffies(timer_slack_val); |
| 139 | mod_timer_pinned(&pcpu->cpu_slack_timer, expires); |
| 140 | } |
| 141 | |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 142 | spin_lock_irqsave(&pcpu->load_lock, flags); |
Todd Poynor | ae7f28c | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 143 | pcpu->time_in_idle = |
| 144 | get_cpu_idle_time_us(smp_processor_id(), |
| 145 | &pcpu->time_in_idle_timestamp); |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 146 | pcpu->cputime_speedadj = 0; |
| 147 | pcpu->cputime_speedadj_timestamp = pcpu->time_in_idle_timestamp; |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 148 | spin_unlock_irqrestore(&pcpu->load_lock, flags); |
Todd Poynor | ae7f28c | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Minsung Kim | cce741d | 2013-02-25 23:48:04 +0900 | [diff] [blame^] | 151 | static unsigned int freq_to_above_hispeed_delay(unsigned int freq) |
| 152 | { |
| 153 | int i; |
| 154 | unsigned int ret; |
| 155 | unsigned long flags; |
| 156 | |
| 157 | spin_lock_irqsave(&above_hispeed_delay_lock, flags); |
| 158 | |
| 159 | for (i = 0; i < nabove_hispeed_delay - 1 && |
| 160 | freq >= above_hispeed_delay[i+1]; i += 2) |
| 161 | ; |
| 162 | |
| 163 | ret = above_hispeed_delay[i]; |
| 164 | spin_unlock_irqrestore(&above_hispeed_delay_lock, flags); |
| 165 | return ret; |
| 166 | } |
| 167 | |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 168 | static unsigned int freq_to_targetload(unsigned int freq) |
| 169 | { |
| 170 | int i; |
| 171 | unsigned int ret; |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 172 | unsigned long flags; |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 173 | |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 174 | spin_lock_irqsave(&target_loads_lock, flags); |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 175 | |
| 176 | for (i = 0; i < ntarget_loads - 1 && freq >= target_loads[i+1]; i += 2) |
| 177 | ; |
| 178 | |
| 179 | ret = target_loads[i]; |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 180 | spin_unlock_irqrestore(&target_loads_lock, flags); |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * If increasing frequencies never map to a lower target load then |
| 186 | * choose_freq() will find the minimum frequency that does not exceed its |
| 187 | * target load given the current load. |
| 188 | */ |
| 189 | |
| 190 | static unsigned int choose_freq( |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 191 | struct cpufreq_interactive_cpuinfo *pcpu, unsigned int loadadjfreq) |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 192 | { |
| 193 | unsigned int freq = pcpu->policy->cur; |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 194 | unsigned int prevfreq, freqmin, freqmax; |
| 195 | unsigned int tl; |
| 196 | int index; |
| 197 | |
| 198 | freqmin = 0; |
| 199 | freqmax = UINT_MAX; |
| 200 | |
| 201 | do { |
| 202 | prevfreq = freq; |
| 203 | tl = freq_to_targetload(freq); |
| 204 | |
| 205 | /* |
| 206 | * Find the lowest frequency where the computed load is less |
| 207 | * than or equal to the target load. |
| 208 | */ |
| 209 | |
| 210 | cpufreq_frequency_table_target( |
| 211 | pcpu->policy, pcpu->freq_table, loadadjfreq / tl, |
| 212 | CPUFREQ_RELATION_L, &index); |
| 213 | freq = pcpu->freq_table[index].frequency; |
| 214 | |
| 215 | if (freq > prevfreq) { |
| 216 | /* The previous frequency is too low. */ |
| 217 | freqmin = prevfreq; |
| 218 | |
| 219 | if (freq >= freqmax) { |
| 220 | /* |
| 221 | * Find the highest frequency that is less |
| 222 | * than freqmax. |
| 223 | */ |
| 224 | cpufreq_frequency_table_target( |
| 225 | pcpu->policy, pcpu->freq_table, |
| 226 | freqmax - 1, CPUFREQ_RELATION_H, |
| 227 | &index); |
| 228 | freq = pcpu->freq_table[index].frequency; |
| 229 | |
| 230 | if (freq == freqmin) { |
| 231 | /* |
| 232 | * The first frequency below freqmax |
| 233 | * has already been found to be too |
| 234 | * low. freqmax is the lowest speed |
| 235 | * we found that is fast enough. |
| 236 | */ |
| 237 | freq = freqmax; |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | } else if (freq < prevfreq) { |
| 242 | /* The previous frequency is high enough. */ |
| 243 | freqmax = prevfreq; |
| 244 | |
| 245 | if (freq <= freqmin) { |
| 246 | /* |
| 247 | * Find the lowest frequency that is higher |
| 248 | * than freqmin. |
| 249 | */ |
| 250 | cpufreq_frequency_table_target( |
| 251 | pcpu->policy, pcpu->freq_table, |
| 252 | freqmin + 1, CPUFREQ_RELATION_L, |
| 253 | &index); |
| 254 | freq = pcpu->freq_table[index].frequency; |
| 255 | |
| 256 | /* |
| 257 | * If freqmax is the first frequency above |
| 258 | * freqmin then we have already found that |
| 259 | * this speed is fast enough. |
| 260 | */ |
| 261 | if (freq == freqmax) |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* If same frequency chosen as previous then done. */ |
| 267 | } while (freq != prevfreq); |
| 268 | |
| 269 | return freq; |
| 270 | } |
| 271 | |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 272 | static u64 update_load(int cpu) |
| 273 | { |
| 274 | struct cpufreq_interactive_cpuinfo *pcpu = &per_cpu(cpuinfo, cpu); |
| 275 | u64 now; |
| 276 | u64 now_idle; |
| 277 | unsigned int delta_idle; |
| 278 | unsigned int delta_time; |
| 279 | u64 active_time; |
| 280 | |
| 281 | now_idle = get_cpu_idle_time_us(cpu, &now); |
| 282 | delta_idle = (unsigned int)(now_idle - pcpu->time_in_idle); |
| 283 | delta_time = (unsigned int)(now - pcpu->time_in_idle_timestamp); |
| 284 | active_time = delta_time - delta_idle; |
| 285 | pcpu->cputime_speedadj += active_time * pcpu->policy->cur; |
| 286 | |
| 287 | pcpu->time_in_idle = now_idle; |
| 288 | pcpu->time_in_idle_timestamp = now; |
| 289 | return now; |
| 290 | } |
| 291 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 292 | static void cpufreq_interactive_timer(unsigned long data) |
| 293 | { |
Todd Poynor | 1913e0f | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 294 | u64 now; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 295 | unsigned int delta_time; |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 296 | u64 cputime_speedadj; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 297 | int cpu_load; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 298 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 299 | &per_cpu(cpuinfo, data); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 300 | unsigned int new_freq; |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 301 | unsigned int loadadjfreq; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 302 | unsigned int index; |
| 303 | unsigned long flags; |
Todd Poynor | 2983547 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 304 | bool boosted; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 305 | |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 306 | if (!down_read_trylock(&pcpu->enable_sem)) |
| 307 | return; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 308 | if (!pcpu->governor_enabled) |
| 309 | goto exit; |
| 310 | |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 311 | spin_lock_irqsave(&pcpu->load_lock, flags); |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 312 | now = update_load(data); |
| 313 | delta_time = (unsigned int)(now - pcpu->cputime_speedadj_timestamp); |
| 314 | cputime_speedadj = pcpu->cputime_speedadj; |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 315 | spin_unlock_irqrestore(&pcpu->load_lock, flags); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 316 | |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 317 | if (WARN_ON_ONCE(!delta_time)) |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 318 | goto rearm; |
| 319 | |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 320 | do_div(cputime_speedadj, delta_time); |
| 321 | loadadjfreq = (unsigned int)cputime_speedadj * 100; |
| 322 | cpu_load = loadadjfreq / pcpu->target_freq; |
Todd Poynor | 2983547 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 323 | boosted = boost_val || now < boostpulse_endtime; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 324 | |
Steve Kondik | a437194 | 2012-10-10 12:20:25 -0700 | [diff] [blame] | 325 | pcpu->cpu_load = cpu_load; |
| 326 | |
Todd Poynor | 7fca0cc | 2012-12-19 16:06:48 -0800 | [diff] [blame] | 327 | if (cpu_load >= go_hispeed_load || boosted) { |
| 328 | if (pcpu->target_freq < hispeed_freq) { |
| 329 | new_freq = hispeed_freq; |
| 330 | } else { |
| 331 | new_freq = choose_freq(pcpu, loadadjfreq); |
| 332 | |
| 333 | if (new_freq < hispeed_freq) |
| 334 | new_freq = hispeed_freq; |
| 335 | } |
| 336 | } else { |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 337 | new_freq = choose_freq(pcpu, loadadjfreq); |
Todd Poynor | 7fca0cc | 2012-12-19 16:06:48 -0800 | [diff] [blame] | 338 | } |
Todd Poynor | 67f0752 | 2012-11-08 15:06:55 -0800 | [diff] [blame] | 339 | |
| 340 | if (pcpu->target_freq >= hispeed_freq && |
| 341 | new_freq > pcpu->target_freq && |
Minsung Kim | cce741d | 2013-02-25 23:48:04 +0900 | [diff] [blame^] | 342 | now - pcpu->hispeed_validate_time < |
| 343 | freq_to_above_hispeed_delay(pcpu->policy->cur)) { |
Todd Poynor | 67f0752 | 2012-11-08 15:06:55 -0800 | [diff] [blame] | 344 | trace_cpufreq_interactive_notyet( |
| 345 | data, cpu_load, pcpu->target_freq, |
| 346 | pcpu->policy->cur, new_freq); |
| 347 | goto rearm; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Todd Poynor | 67f0752 | 2012-11-08 15:06:55 -0800 | [diff] [blame] | 350 | pcpu->hispeed_validate_time = now; |
Todd Poynor | 5a5aa70 | 2012-05-10 23:28:06 -0700 | [diff] [blame] | 351 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 352 | if (cpufreq_frequency_table_target(pcpu->policy, pcpu->freq_table, |
Todd Poynor | 6ecca11 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 353 | new_freq, CPUFREQ_RELATION_L, |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 354 | &index)) { |
| 355 | pr_warn_once("timer %d: cpufreq_frequency_table_target error\n", |
| 356 | (int) data); |
| 357 | goto rearm; |
| 358 | } |
| 359 | |
| 360 | new_freq = pcpu->freq_table[index].frequency; |
| 361 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 362 | /* |
Todd Poynor | aad2732 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 363 | * Do not scale below floor_freq unless we have been at or above the |
| 364 | * floor frequency for the minimum sample time since last validated. |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 365 | */ |
Todd Poynor | aad2732 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 366 | if (new_freq < pcpu->floor_freq) { |
Todd Poynor | 1913e0f | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 367 | if (now - pcpu->floor_validate_time < min_sample_time) { |
Todd Poynor | 27f7b8e | 2012-11-28 17:56:09 -0800 | [diff] [blame] | 368 | trace_cpufreq_interactive_notyet( |
| 369 | data, cpu_load, pcpu->target_freq, |
| 370 | pcpu->policy->cur, new_freq); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 371 | goto rearm; |
Todd Poynor | a1e1951 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 372 | } |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Todd Poynor | 2983547 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 375 | /* |
| 376 | * Update the timestamp for checking whether speed has been held at |
| 377 | * or above the selected frequency for a minimum of min_sample_time, |
| 378 | * if not boosted to hispeed_freq. If boosted to hispeed_freq then we |
| 379 | * allow the speed to drop as soon as the boostpulse duration expires |
| 380 | * (or the indefinite boost is turned off). |
| 381 | */ |
| 382 | |
| 383 | if (!boosted || new_freq > hispeed_freq) { |
| 384 | pcpu->floor_freq = new_freq; |
| 385 | pcpu->floor_validate_time = now; |
| 386 | } |
Todd Poynor | 0a92d48 | 2012-04-06 19:59:36 -0700 | [diff] [blame] | 387 | |
| 388 | if (pcpu->target_freq == new_freq) { |
Todd Poynor | 27f7b8e | 2012-11-28 17:56:09 -0800 | [diff] [blame] | 389 | trace_cpufreq_interactive_already( |
| 390 | data, cpu_load, pcpu->target_freq, |
| 391 | pcpu->policy->cur, new_freq); |
Todd Poynor | 0a92d48 | 2012-04-06 19:59:36 -0700 | [diff] [blame] | 392 | goto rearm_if_notmax; |
| 393 | } |
| 394 | |
Todd Poynor | a1e1951 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 395 | trace_cpufreq_interactive_target(data, cpu_load, pcpu->target_freq, |
Todd Poynor | 27f7b8e | 2012-11-28 17:56:09 -0800 | [diff] [blame] | 396 | pcpu->policy->cur, new_freq); |
Todd Poynor | a1e1951 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 397 | |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 398 | pcpu->target_freq = new_freq; |
| 399 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
| 400 | cpumask_set_cpu(data, &speedchange_cpumask); |
| 401 | spin_unlock_irqrestore(&speedchange_cpumask_lock, flags); |
| 402 | wake_up_process(speedchange_task); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 403 | |
| 404 | rearm_if_notmax: |
| 405 | /* |
| 406 | * Already set max speed and don't see a need to change that, |
| 407 | * wait until next idle to re-evaluate, don't need timer. |
| 408 | */ |
| 409 | if (pcpu->target_freq == pcpu->policy->max) |
| 410 | goto exit; |
| 411 | |
| 412 | rearm: |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 413 | if (!timer_pending(&pcpu->cpu_timer)) |
Todd Poynor | ae7f28c | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 414 | cpufreq_interactive_timer_resched(pcpu); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 415 | |
| 416 | exit: |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 417 | up_read(&pcpu->enable_sem); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 418 | return; |
| 419 | } |
| 420 | |
| 421 | static void cpufreq_interactive_idle_start(void) |
| 422 | { |
| 423 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 424 | &per_cpu(cpuinfo, smp_processor_id()); |
| 425 | int pending; |
| 426 | |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 427 | if (!down_read_trylock(&pcpu->enable_sem)) |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 428 | return; |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 429 | if (!pcpu->governor_enabled) { |
| 430 | up_read(&pcpu->enable_sem); |
| 431 | return; |
| 432 | } |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 433 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 434 | pending = timer_pending(&pcpu->cpu_timer); |
| 435 | |
| 436 | if (pcpu->target_freq != pcpu->policy->min) { |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 437 | /* |
| 438 | * Entering idle while not at lowest speed. On some |
| 439 | * platforms this can hold the other CPU(s) at that speed |
| 440 | * even though the CPU is idle. Set a timer to re-evaluate |
| 441 | * speed so this idle CPU doesn't hold the other CPUs above |
| 442 | * min indefinitely. This should probably be a quirk of |
| 443 | * the CPUFreq driver. |
| 444 | */ |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 445 | if (!pending) |
Todd Poynor | ae7f28c | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 446 | cpufreq_interactive_timer_resched(pcpu); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 449 | up_read(&pcpu->enable_sem); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static void cpufreq_interactive_idle_end(void) |
| 453 | { |
| 454 | struct cpufreq_interactive_cpuinfo *pcpu = |
| 455 | &per_cpu(cpuinfo, smp_processor_id()); |
| 456 | |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 457 | if (!down_read_trylock(&pcpu->enable_sem)) |
Sam Leffler | a04e441 | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 458 | return; |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 459 | if (!pcpu->governor_enabled) { |
| 460 | up_read(&pcpu->enable_sem); |
| 461 | return; |
| 462 | } |
Sam Leffler | a04e441 | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 463 | |
Todd Poynor | 1913e0f | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 464 | /* Arm the timer for 1-2 ticks later if not already. */ |
| 465 | if (!timer_pending(&pcpu->cpu_timer)) { |
Todd Poynor | ae7f28c | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 466 | cpufreq_interactive_timer_resched(pcpu); |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 467 | } else if (time_after_eq(jiffies, pcpu->cpu_timer.expires)) { |
Todd Poynor | ae7f28c | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 468 | del_timer(&pcpu->cpu_timer); |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 469 | del_timer(&pcpu->cpu_slack_timer); |
Todd Poynor | ae7f28c | 2012-10-08 20:14:34 -0700 | [diff] [blame] | 470 | cpufreq_interactive_timer(smp_processor_id()); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 471 | } |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 472 | |
| 473 | up_read(&pcpu->enable_sem); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 476 | static int cpufreq_interactive_speedchange_task(void *data) |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 477 | { |
| 478 | unsigned int cpu; |
| 479 | cpumask_t tmp_mask; |
| 480 | unsigned long flags; |
| 481 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 482 | |
| 483 | while (1) { |
| 484 | set_current_state(TASK_INTERRUPTIBLE); |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 485 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 486 | |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 487 | if (cpumask_empty(&speedchange_cpumask)) { |
| 488 | spin_unlock_irqrestore(&speedchange_cpumask_lock, |
| 489 | flags); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 490 | schedule(); |
| 491 | |
| 492 | if (kthread_should_stop()) |
| 493 | break; |
| 494 | |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 495 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | set_current_state(TASK_RUNNING); |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 499 | tmp_mask = speedchange_cpumask; |
| 500 | cpumask_clear(&speedchange_cpumask); |
| 501 | spin_unlock_irqrestore(&speedchange_cpumask_lock, flags); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 502 | |
| 503 | for_each_cpu(cpu, &tmp_mask) { |
| 504 | unsigned int j; |
| 505 | unsigned int max_freq = 0; |
| 506 | |
| 507 | pcpu = &per_cpu(cpuinfo, cpu); |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 508 | if (!down_read_trylock(&pcpu->enable_sem)) |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 509 | continue; |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 510 | if (!pcpu->governor_enabled) { |
| 511 | up_read(&pcpu->enable_sem); |
| 512 | continue; |
| 513 | } |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 514 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 515 | for_each_cpu(j, pcpu->policy->cpus) { |
| 516 | struct cpufreq_interactive_cpuinfo *pjcpu = |
| 517 | &per_cpu(cpuinfo, j); |
| 518 | |
| 519 | if (pjcpu->target_freq > max_freq) |
| 520 | max_freq = pjcpu->target_freq; |
Steve Kondik | a437194 | 2012-10-10 12:20:25 -0700 | [diff] [blame] | 521 | |
| 522 | cpufreq_notify_utilization(pcpu->policy, (pcpu->cpu_load * pcpu->policy->cur) / pcpu->policy->cpuinfo.max_freq); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if (max_freq != pcpu->policy->cur) |
| 526 | __cpufreq_driver_target(pcpu->policy, |
| 527 | max_freq, |
| 528 | CPUFREQ_RELATION_H); |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 529 | trace_cpufreq_interactive_setspeed(cpu, |
| 530 | pcpu->target_freq, |
Todd Poynor | a1e1951 | 2012-02-16 16:27:59 -0800 | [diff] [blame] | 531 | pcpu->policy->cur); |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 532 | |
| 533 | up_read(&pcpu->enable_sem); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
Todd Poynor | 7820a65 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 540 | static void cpufreq_interactive_boost(void) |
| 541 | { |
| 542 | int i; |
| 543 | int anyboost = 0; |
| 544 | unsigned long flags; |
| 545 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 546 | |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 547 | spin_lock_irqsave(&speedchange_cpumask_lock, flags); |
Todd Poynor | 7820a65 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 548 | |
| 549 | for_each_online_cpu(i) { |
| 550 | pcpu = &per_cpu(cpuinfo, i); |
| 551 | |
| 552 | if (pcpu->target_freq < hispeed_freq) { |
| 553 | pcpu->target_freq = hispeed_freq; |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 554 | cpumask_set_cpu(i, &speedchange_cpumask); |
Todd Poynor | c9d53b3 | 2012-12-07 20:08:45 -0800 | [diff] [blame] | 555 | pcpu->hispeed_validate_time = |
| 556 | ktime_to_us(ktime_get()); |
Todd Poynor | 7820a65 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 557 | anyboost = 1; |
| 558 | } |
| 559 | |
| 560 | /* |
Todd Poynor | aad2732 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 561 | * Set floor freq and (re)start timer for when last |
| 562 | * validated. |
Todd Poynor | 7820a65 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 563 | */ |
| 564 | |
Todd Poynor | aad2732 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 565 | pcpu->floor_freq = hispeed_freq; |
| 566 | pcpu->floor_validate_time = ktime_to_us(ktime_get()); |
Todd Poynor | 7820a65 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 569 | spin_unlock_irqrestore(&speedchange_cpumask_lock, flags); |
Todd Poynor | 7820a65 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 570 | |
| 571 | if (anyboost) |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 572 | wake_up_process(speedchange_task); |
Todd Poynor | 7820a65 | 2012-04-02 17:17:14 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 575 | static int cpufreq_interactive_notifier( |
| 576 | struct notifier_block *nb, unsigned long val, void *data) |
| 577 | { |
| 578 | struct cpufreq_freqs *freq = data; |
| 579 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 580 | int cpu; |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 581 | unsigned long flags; |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 582 | |
| 583 | if (val == CPUFREQ_POSTCHANGE) { |
| 584 | pcpu = &per_cpu(cpuinfo, freq->cpu); |
Todd Poynor | 84a96d2 | 2012-12-23 12:28:49 -0800 | [diff] [blame] | 585 | if (!down_read_trylock(&pcpu->enable_sem)) |
| 586 | return 0; |
| 587 | if (!pcpu->governor_enabled) { |
| 588 | up_read(&pcpu->enable_sem); |
| 589 | return 0; |
| 590 | } |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 591 | |
| 592 | for_each_cpu(cpu, pcpu->policy->cpus) { |
| 593 | struct cpufreq_interactive_cpuinfo *pjcpu = |
| 594 | &per_cpu(cpuinfo, cpu); |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 595 | spin_lock_irqsave(&pjcpu->load_lock, flags); |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 596 | update_load(cpu); |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 597 | spin_unlock_irqrestore(&pjcpu->load_lock, flags); |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 598 | } |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 599 | |
Todd Poynor | 84a96d2 | 2012-12-23 12:28:49 -0800 | [diff] [blame] | 600 | up_read(&pcpu->enable_sem); |
| 601 | } |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static struct notifier_block cpufreq_notifier_block = { |
| 606 | .notifier_call = cpufreq_interactive_notifier, |
| 607 | }; |
| 608 | |
Minsung Kim | cce741d | 2013-02-25 23:48:04 +0900 | [diff] [blame^] | 609 | static unsigned int *get_tokenized_data(const char *buf, int *num_tokens) |
| 610 | { |
| 611 | const char *cp; |
| 612 | int i; |
| 613 | int ntokens = 1; |
| 614 | unsigned int *tokenized_data; |
| 615 | |
| 616 | cp = buf; |
| 617 | while ((cp = strpbrk(cp + 1, " :"))) |
| 618 | ntokens++; |
| 619 | |
| 620 | if (!(ntokens & 0x1)) { |
| 621 | tokenized_data = ERR_PTR(-EINVAL); |
| 622 | goto err; |
| 623 | } |
| 624 | |
| 625 | tokenized_data = kmalloc(ntokens * sizeof(unsigned int), GFP_KERNEL); |
| 626 | if (!tokenized_data) { |
| 627 | tokenized_data = ERR_PTR(-ENOMEM); |
| 628 | goto err; |
| 629 | } |
| 630 | |
| 631 | cp = buf; |
| 632 | i = 0; |
| 633 | while (i < ntokens) { |
| 634 | if (sscanf(cp, "%u", &tokenized_data[i++]) != 1) { |
| 635 | tokenized_data = ERR_PTR(-EINVAL); |
| 636 | goto err_kfree; |
| 637 | } |
| 638 | |
| 639 | cp = strpbrk(cp, " :"); |
| 640 | if (!cp) |
| 641 | break; |
| 642 | cp++; |
| 643 | } |
| 644 | |
| 645 | if (i != ntokens) { |
| 646 | tokenized_data = ERR_PTR(-EINVAL); |
| 647 | goto err_kfree; |
| 648 | } |
| 649 | |
| 650 | *num_tokens = ntokens; |
| 651 | return tokenized_data; |
| 652 | |
| 653 | err_kfree: |
| 654 | kfree(tokenized_data); |
| 655 | err: |
| 656 | return tokenized_data; |
| 657 | } |
| 658 | |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 659 | static ssize_t show_target_loads( |
Todd Poynor | 6ecca11 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 660 | struct kobject *kobj, struct attribute *attr, char *buf) |
| 661 | { |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 662 | int i; |
| 663 | ssize_t ret = 0; |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 664 | unsigned long flags; |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 665 | |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 666 | spin_lock_irqsave(&target_loads_lock, flags); |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 667 | |
| 668 | for (i = 0; i < ntarget_loads; i++) |
| 669 | ret += sprintf(buf + ret, "%u%s", target_loads[i], |
| 670 | i & 0x1 ? ":" : " "); |
| 671 | |
| 672 | ret += sprintf(buf + ret, "\n"); |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 673 | spin_unlock_irqrestore(&target_loads_lock, flags); |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 674 | return ret; |
Todd Poynor | 6ecca11 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 677 | static ssize_t store_target_loads( |
Todd Poynor | 6ecca11 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 678 | struct kobject *kobj, struct attribute *attr, const char *buf, |
| 679 | size_t count) |
| 680 | { |
Minsung Kim | cce741d | 2013-02-25 23:48:04 +0900 | [diff] [blame^] | 681 | int ntokens; |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 682 | unsigned int *new_target_loads = NULL; |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 683 | unsigned long flags; |
Todd Poynor | 6ecca11 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 684 | |
Minsung Kim | cce741d | 2013-02-25 23:48:04 +0900 | [diff] [blame^] | 685 | new_target_loads = get_tokenized_data(buf, &ntokens); |
| 686 | if (IS_ERR(new_target_loads)) |
| 687 | return PTR_RET(new_target_loads); |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 688 | |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 689 | spin_lock_irqsave(&target_loads_lock, flags); |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 690 | if (target_loads != default_target_loads) |
| 691 | kfree(target_loads); |
| 692 | target_loads = new_target_loads; |
| 693 | ntarget_loads = ntokens; |
Todd Poynor | 8a42a03 | 2013-01-02 13:14:00 -0800 | [diff] [blame] | 694 | spin_unlock_irqrestore(&target_loads_lock, flags); |
Todd Poynor | 6ecca11 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 695 | return count; |
| 696 | } |
| 697 | |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 698 | static struct global_attr target_loads_attr = |
| 699 | __ATTR(target_loads, S_IRUGO | S_IWUSR, |
| 700 | show_target_loads, store_target_loads); |
Todd Poynor | 6ecca11 | 2012-11-28 17:58:17 -0800 | [diff] [blame] | 701 | |
Minsung Kim | cce741d | 2013-02-25 23:48:04 +0900 | [diff] [blame^] | 702 | static ssize_t show_above_hispeed_delay( |
| 703 | struct kobject *kobj, struct attribute *attr, char *buf) |
| 704 | { |
| 705 | int i; |
| 706 | ssize_t ret = 0; |
| 707 | unsigned long flags; |
| 708 | |
| 709 | spin_lock_irqsave(&above_hispeed_delay_lock, flags); |
| 710 | |
| 711 | for (i = 0; i < nabove_hispeed_delay; i++) |
| 712 | ret += sprintf(buf + ret, "%u%s", above_hispeed_delay[i], |
| 713 | i & 0x1 ? ":" : " "); |
| 714 | |
| 715 | ret += sprintf(buf + ret, "\n"); |
| 716 | spin_unlock_irqrestore(&above_hispeed_delay_lock, flags); |
| 717 | return ret; |
| 718 | } |
| 719 | |
| 720 | static ssize_t store_above_hispeed_delay( |
| 721 | struct kobject *kobj, struct attribute *attr, const char *buf, |
| 722 | size_t count) |
| 723 | { |
| 724 | int ntokens; |
| 725 | unsigned int *new_above_hispeed_delay = NULL; |
| 726 | unsigned long flags; |
| 727 | |
| 728 | new_above_hispeed_delay = get_tokenized_data(buf, &ntokens); |
| 729 | if (IS_ERR(new_above_hispeed_delay)) |
| 730 | return PTR_RET(new_above_hispeed_delay); |
| 731 | |
| 732 | spin_lock_irqsave(&above_hispeed_delay_lock, flags); |
| 733 | if (above_hispeed_delay != default_above_hispeed_delay) |
| 734 | kfree(above_hispeed_delay); |
| 735 | above_hispeed_delay = new_above_hispeed_delay; |
| 736 | nabove_hispeed_delay = ntokens; |
| 737 | spin_unlock_irqrestore(&above_hispeed_delay_lock, flags); |
| 738 | return count; |
| 739 | |
| 740 | } |
| 741 | |
| 742 | static struct global_attr above_hispeed_delay_attr = |
| 743 | __ATTR(above_hispeed_delay, S_IRUGO | S_IWUSR, |
| 744 | show_above_hispeed_delay, store_above_hispeed_delay); |
| 745 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 746 | static ssize_t show_hispeed_freq(struct kobject *kobj, |
| 747 | struct attribute *attr, char *buf) |
| 748 | { |
Todd Poynor | f090ef0 | 2012-10-03 00:39:56 -0700 | [diff] [blame] | 749 | return sprintf(buf, "%u\n", hispeed_freq); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static ssize_t store_hispeed_freq(struct kobject *kobj, |
| 753 | struct attribute *attr, const char *buf, |
| 754 | size_t count) |
| 755 | { |
| 756 | int ret; |
Todd Poynor | f090ef0 | 2012-10-03 00:39:56 -0700 | [diff] [blame] | 757 | long unsigned int val; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 758 | |
Todd Poynor | f090ef0 | 2012-10-03 00:39:56 -0700 | [diff] [blame] | 759 | ret = strict_strtoul(buf, 0, &val); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 760 | if (ret < 0) |
| 761 | return ret; |
| 762 | hispeed_freq = val; |
| 763 | return count; |
| 764 | } |
| 765 | |
| 766 | static struct global_attr hispeed_freq_attr = __ATTR(hispeed_freq, 0644, |
| 767 | show_hispeed_freq, store_hispeed_freq); |
| 768 | |
| 769 | |
| 770 | static ssize_t show_go_hispeed_load(struct kobject *kobj, |
| 771 | struct attribute *attr, char *buf) |
| 772 | { |
| 773 | return sprintf(buf, "%lu\n", go_hispeed_load); |
| 774 | } |
| 775 | |
| 776 | static ssize_t store_go_hispeed_load(struct kobject *kobj, |
| 777 | struct attribute *attr, const char *buf, size_t count) |
| 778 | { |
| 779 | int ret; |
| 780 | unsigned long val; |
| 781 | |
| 782 | ret = strict_strtoul(buf, 0, &val); |
| 783 | if (ret < 0) |
| 784 | return ret; |
| 785 | go_hispeed_load = val; |
| 786 | return count; |
| 787 | } |
| 788 | |
| 789 | static struct global_attr go_hispeed_load_attr = __ATTR(go_hispeed_load, 0644, |
| 790 | show_go_hispeed_load, store_go_hispeed_load); |
| 791 | |
| 792 | static ssize_t show_min_sample_time(struct kobject *kobj, |
| 793 | struct attribute *attr, char *buf) |
| 794 | { |
| 795 | return sprintf(buf, "%lu\n", min_sample_time); |
| 796 | } |
| 797 | |
| 798 | static ssize_t store_min_sample_time(struct kobject *kobj, |
| 799 | struct attribute *attr, const char *buf, size_t count) |
| 800 | { |
| 801 | int ret; |
| 802 | unsigned long val; |
| 803 | |
| 804 | ret = strict_strtoul(buf, 0, &val); |
| 805 | if (ret < 0) |
| 806 | return ret; |
| 807 | min_sample_time = val; |
| 808 | return count; |
| 809 | } |
| 810 | |
| 811 | static struct global_attr min_sample_time_attr = __ATTR(min_sample_time, 0644, |
| 812 | show_min_sample_time, store_min_sample_time); |
| 813 | |
| 814 | static ssize_t show_timer_rate(struct kobject *kobj, |
| 815 | struct attribute *attr, char *buf) |
| 816 | { |
| 817 | return sprintf(buf, "%lu\n", timer_rate); |
| 818 | } |
| 819 | |
| 820 | static ssize_t store_timer_rate(struct kobject *kobj, |
| 821 | struct attribute *attr, const char *buf, size_t count) |
| 822 | { |
| 823 | int ret; |
| 824 | unsigned long val; |
| 825 | |
| 826 | ret = strict_strtoul(buf, 0, &val); |
| 827 | if (ret < 0) |
| 828 | return ret; |
| 829 | timer_rate = val; |
| 830 | return count; |
| 831 | } |
| 832 | |
| 833 | static struct global_attr timer_rate_attr = __ATTR(timer_rate, 0644, |
| 834 | show_timer_rate, store_timer_rate); |
| 835 | |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 836 | static ssize_t show_timer_slack( |
| 837 | struct kobject *kobj, struct attribute *attr, char *buf) |
| 838 | { |
| 839 | return sprintf(buf, "%d\n", timer_slack_val); |
| 840 | } |
| 841 | |
| 842 | static ssize_t store_timer_slack( |
| 843 | struct kobject *kobj, struct attribute *attr, const char *buf, |
| 844 | size_t count) |
| 845 | { |
| 846 | int ret; |
| 847 | unsigned long val; |
| 848 | |
| 849 | ret = kstrtol(buf, 10, &val); |
| 850 | if (ret < 0) |
| 851 | return ret; |
| 852 | |
| 853 | timer_slack_val = val; |
| 854 | return count; |
| 855 | } |
| 856 | |
| 857 | define_one_global_rw(timer_slack); |
| 858 | |
Todd Poynor | 9fb1531 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 859 | static ssize_t show_boost(struct kobject *kobj, struct attribute *attr, |
| 860 | char *buf) |
| 861 | { |
| 862 | return sprintf(buf, "%d\n", boost_val); |
| 863 | } |
| 864 | |
| 865 | static ssize_t store_boost(struct kobject *kobj, struct attribute *attr, |
| 866 | const char *buf, size_t count) |
| 867 | { |
| 868 | int ret; |
| 869 | unsigned long val; |
| 870 | |
| 871 | ret = kstrtoul(buf, 0, &val); |
| 872 | if (ret < 0) |
| 873 | return ret; |
| 874 | |
| 875 | boost_val = val; |
| 876 | |
Todd Poynor | 2e739a0 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 877 | if (boost_val) { |
| 878 | trace_cpufreq_interactive_boost("on"); |
Todd Poynor | 9fb1531 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 879 | cpufreq_interactive_boost(); |
Todd Poynor | 2e739a0 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 880 | } else { |
| 881 | trace_cpufreq_interactive_unboost("off"); |
| 882 | } |
Todd Poynor | 9fb1531 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 883 | |
| 884 | return count; |
| 885 | } |
| 886 | |
| 887 | define_one_global_rw(boost); |
| 888 | |
Todd Poynor | 2e739a0 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 889 | static ssize_t store_boostpulse(struct kobject *kobj, struct attribute *attr, |
| 890 | const char *buf, size_t count) |
| 891 | { |
| 892 | int ret; |
| 893 | unsigned long val; |
| 894 | |
| 895 | ret = kstrtoul(buf, 0, &val); |
| 896 | if (ret < 0) |
| 897 | return ret; |
| 898 | |
Todd Poynor | 2983547 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 899 | boostpulse_endtime = ktime_to_us(ktime_get()) + boostpulse_duration_val; |
Todd Poynor | 2e739a0 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 900 | trace_cpufreq_interactive_boost("pulse"); |
| 901 | cpufreq_interactive_boost(); |
| 902 | return count; |
| 903 | } |
| 904 | |
| 905 | static struct global_attr boostpulse = |
| 906 | __ATTR(boostpulse, 0200, NULL, store_boostpulse); |
| 907 | |
Todd Poynor | 2983547 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 908 | static ssize_t show_boostpulse_duration( |
| 909 | struct kobject *kobj, struct attribute *attr, char *buf) |
| 910 | { |
| 911 | return sprintf(buf, "%d\n", boostpulse_duration_val); |
| 912 | } |
| 913 | |
| 914 | static ssize_t store_boostpulse_duration( |
| 915 | struct kobject *kobj, struct attribute *attr, const char *buf, |
| 916 | size_t count) |
| 917 | { |
| 918 | int ret; |
| 919 | unsigned long val; |
| 920 | |
| 921 | ret = kstrtoul(buf, 0, &val); |
| 922 | if (ret < 0) |
| 923 | return ret; |
| 924 | |
| 925 | boostpulse_duration_val = val; |
| 926 | return count; |
| 927 | } |
| 928 | |
| 929 | define_one_global_rw(boostpulse_duration); |
| 930 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 931 | static struct attribute *interactive_attributes[] = { |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 932 | &target_loads_attr.attr, |
Minsung Kim | cce741d | 2013-02-25 23:48:04 +0900 | [diff] [blame^] | 933 | &above_hispeed_delay_attr.attr, |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 934 | &hispeed_freq_attr.attr, |
| 935 | &go_hispeed_load_attr.attr, |
| 936 | &min_sample_time_attr.attr, |
| 937 | &timer_rate_attr.attr, |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 938 | &timer_slack.attr, |
Todd Poynor | 9fb1531 | 2012-04-23 20:42:41 -0700 | [diff] [blame] | 939 | &boost.attr, |
Todd Poynor | 2e739a0 | 2012-05-03 00:16:55 -0700 | [diff] [blame] | 940 | &boostpulse.attr, |
Todd Poynor | 2983547 | 2012-12-14 17:31:19 -0800 | [diff] [blame] | 941 | &boostpulse_duration.attr, |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 942 | NULL, |
| 943 | }; |
| 944 | |
| 945 | static struct attribute_group interactive_attr_group = { |
| 946 | .attrs = interactive_attributes, |
| 947 | .name = "interactive", |
| 948 | }; |
| 949 | |
Sam Leffler | a04e441 | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 950 | static int cpufreq_interactive_idle_notifier(struct notifier_block *nb, |
| 951 | unsigned long val, |
| 952 | void *data) |
| 953 | { |
| 954 | switch (val) { |
| 955 | case IDLE_START: |
| 956 | cpufreq_interactive_idle_start(); |
| 957 | break; |
| 958 | case IDLE_END: |
| 959 | cpufreq_interactive_idle_end(); |
| 960 | break; |
| 961 | } |
| 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | static struct notifier_block cpufreq_interactive_idle_nb = { |
| 967 | .notifier_call = cpufreq_interactive_idle_notifier, |
| 968 | }; |
| 969 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 970 | static int cpufreq_governor_interactive(struct cpufreq_policy *policy, |
| 971 | unsigned int event) |
| 972 | { |
| 973 | int rc; |
| 974 | unsigned int j; |
| 975 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 976 | struct cpufreq_frequency_table *freq_table; |
| 977 | |
| 978 | switch (event) { |
| 979 | case CPUFREQ_GOV_START: |
| 980 | if (!cpu_online(policy->cpu)) |
| 981 | return -EINVAL; |
| 982 | |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 983 | mutex_lock(&gov_lock); |
| 984 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 985 | freq_table = |
| 986 | cpufreq_frequency_get_table(policy->cpu); |
Todd Poynor | 1913e0f | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 987 | if (!hispeed_freq) |
| 988 | hispeed_freq = policy->max; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 989 | |
| 990 | for_each_cpu(j, policy->cpus) { |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 991 | unsigned long expires; |
| 992 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 993 | pcpu = &per_cpu(cpuinfo, j); |
| 994 | pcpu->policy = policy; |
| 995 | pcpu->target_freq = policy->cur; |
| 996 | pcpu->freq_table = freq_table; |
Todd Poynor | aad2732 | 2012-04-26 21:41:40 -0700 | [diff] [blame] | 997 | pcpu->floor_freq = pcpu->target_freq; |
| 998 | pcpu->floor_validate_time = |
Todd Poynor | c9d53b3 | 2012-12-07 20:08:45 -0800 | [diff] [blame] | 999 | ktime_to_us(ktime_get()); |
Todd Poynor | 5a5aa70 | 2012-05-10 23:28:06 -0700 | [diff] [blame] | 1000 | pcpu->hispeed_validate_time = |
Todd Poynor | c9d53b3 | 2012-12-07 20:08:45 -0800 | [diff] [blame] | 1001 | pcpu->floor_validate_time; |
Todd Poynor | 2ca5647 | 2012-12-20 15:51:00 -0800 | [diff] [blame] | 1002 | down_write(&pcpu->enable_sem); |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 1003 | expires = jiffies + usecs_to_jiffies(timer_rate); |
| 1004 | pcpu->cpu_timer.expires = expires; |
Todd Poynor | 1913e0f | 2012-11-05 13:09:03 -0800 | [diff] [blame] | 1005 | add_timer_on(&pcpu->cpu_timer, j); |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 1006 | if (timer_slack_val >= 0) { |
| 1007 | expires += usecs_to_jiffies(timer_slack_val); |
| 1008 | pcpu->cpu_slack_timer.expires = expires; |
| 1009 | add_timer_on(&pcpu->cpu_slack_timer, j); |
| 1010 | } |
Todd Poynor | 2ca5647 | 2012-12-20 15:51:00 -0800 | [diff] [blame] | 1011 | pcpu->governor_enabled = 1; |
| 1012 | up_write(&pcpu->enable_sem); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1015 | /* |
| 1016 | * Do not register the idle hook and create sysfs |
| 1017 | * entries if we have already done so. |
| 1018 | */ |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1019 | if (++active_count > 1) { |
| 1020 | mutex_unlock(&gov_lock); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1021 | return 0; |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1022 | } |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1023 | |
| 1024 | rc = sysfs_create_group(cpufreq_global_kobject, |
| 1025 | &interactive_attr_group); |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1026 | if (rc) { |
| 1027 | mutex_unlock(&gov_lock); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1028 | return rc; |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1029 | } |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1030 | |
Sam Leffler | a04e441 | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 1031 | idle_notifier_register(&cpufreq_interactive_idle_nb); |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 1032 | cpufreq_register_notifier( |
| 1033 | &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1034 | mutex_unlock(&gov_lock); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1035 | break; |
| 1036 | |
| 1037 | case CPUFREQ_GOV_STOP: |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1038 | mutex_lock(&gov_lock); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1039 | for_each_cpu(j, policy->cpus) { |
| 1040 | pcpu = &per_cpu(cpuinfo, j); |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 1041 | down_write(&pcpu->enable_sem); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1042 | pcpu->governor_enabled = 0; |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1043 | del_timer_sync(&pcpu->cpu_timer); |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 1044 | del_timer_sync(&pcpu->cpu_slack_timer); |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 1045 | up_write(&pcpu->enable_sem); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1048 | if (--active_count > 0) { |
| 1049 | mutex_unlock(&gov_lock); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1050 | return 0; |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1051 | } |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1052 | |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 1053 | cpufreq_unregister_notifier( |
| 1054 | &cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); |
Sam Leffler | a04e441 | 2012-06-27 10:12:04 -0700 | [diff] [blame] | 1055 | idle_notifier_unregister(&cpufreq_interactive_idle_nb); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1056 | sysfs_remove_group(cpufreq_global_kobject, |
| 1057 | &interactive_attr_group); |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1058 | mutex_unlock(&gov_lock); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1059 | |
| 1060 | break; |
| 1061 | |
| 1062 | case CPUFREQ_GOV_LIMITS: |
| 1063 | if (policy->max < policy->cur) |
| 1064 | __cpufreq_driver_target(policy, |
| 1065 | policy->max, CPUFREQ_RELATION_H); |
| 1066 | else if (policy->min > policy->cur) |
| 1067 | __cpufreq_driver_target(policy, |
| 1068 | policy->min, CPUFREQ_RELATION_L); |
| 1069 | break; |
| 1070 | } |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 1074 | static void cpufreq_interactive_nop_timer(unsigned long data) |
| 1075 | { |
| 1076 | } |
| 1077 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1078 | static int __init cpufreq_interactive_init(void) |
| 1079 | { |
| 1080 | unsigned int i; |
| 1081 | struct cpufreq_interactive_cpuinfo *pcpu; |
| 1082 | struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
| 1083 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1084 | /* Initalize per-cpu timers */ |
| 1085 | for_each_possible_cpu(i) { |
| 1086 | pcpu = &per_cpu(cpuinfo, i); |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 1087 | init_timer_deferrable(&pcpu->cpu_timer); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1088 | pcpu->cpu_timer.function = cpufreq_interactive_timer; |
| 1089 | pcpu->cpu_timer.data = i; |
Todd Poynor | 264e291 | 2012-12-18 17:50:10 -0800 | [diff] [blame] | 1090 | init_timer(&pcpu->cpu_slack_timer); |
| 1091 | pcpu->cpu_slack_timer.function = cpufreq_interactive_nop_timer; |
Todd Poynor | 07a9e29 | 2012-12-11 16:05:03 -0800 | [diff] [blame] | 1092 | spin_lock_init(&pcpu->load_lock); |
Todd Poynor | bc819a2 | 2012-12-18 17:50:44 -0800 | [diff] [blame] | 1093 | init_rwsem(&pcpu->enable_sem); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Todd Poynor | 21df1ca | 2012-11-14 11:41:21 -0800 | [diff] [blame] | 1096 | spin_lock_init(&target_loads_lock); |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1097 | spin_lock_init(&speedchange_cpumask_lock); |
Lianwei Wang | 9f8d537 | 2013-01-07 14:15:51 +0800 | [diff] [blame] | 1098 | mutex_init(&gov_lock); |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1099 | speedchange_task = |
| 1100 | kthread_create(cpufreq_interactive_speedchange_task, NULL, |
| 1101 | "cfinteractive"); |
| 1102 | if (IS_ERR(speedchange_task)) |
| 1103 | return PTR_ERR(speedchange_task); |
Sam Leffler | a13f415 | 2012-06-27 12:55:56 -0700 | [diff] [blame] | 1104 | |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1105 | sched_setscheduler_nocheck(speedchange_task, SCHED_FIFO, ¶m); |
| 1106 | get_task_struct(speedchange_task); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1107 | |
Sam Leffler | a13f415 | 2012-06-27 12:55:56 -0700 | [diff] [blame] | 1108 | /* NB: wake up so the thread does not look hung to the freezer */ |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1109 | wake_up_process(speedchange_task); |
Sam Leffler | a13f415 | 2012-06-27 12:55:56 -0700 | [diff] [blame] | 1110 | |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1111 | return cpufreq_register_governor(&cpufreq_gov_interactive); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_INTERACTIVE |
| 1115 | fs_initcall(cpufreq_interactive_init); |
| 1116 | #else |
| 1117 | module_init(cpufreq_interactive_init); |
| 1118 | #endif |
| 1119 | |
| 1120 | static void __exit cpufreq_interactive_exit(void) |
| 1121 | { |
| 1122 | cpufreq_unregister_governor(&cpufreq_gov_interactive); |
Todd Poynor | 02442cf | 2012-07-16 17:07:15 -0700 | [diff] [blame] | 1123 | kthread_stop(speedchange_task); |
| 1124 | put_task_struct(speedchange_task); |
Mike Chan | 9d49b70 | 2010-06-22 11:26:45 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | module_exit(cpufreq_interactive_exit); |
| 1128 | |
| 1129 | MODULE_AUTHOR("Mike Chan <mike@android.com>"); |
| 1130 | MODULE_DESCRIPTION("'cpufreq_interactive' - A cpufreq governor for " |
| 1131 | "Latency sensitive workloads"); |
| 1132 | MODULE_LICENSE("GPL"); |