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