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