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