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