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