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