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