Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/cpufreq/cpufreq_governor.h |
| 3 | * |
| 4 | * Header file for CPUFreq governors common code |
| 5 | * |
| 6 | * Copyright (C) 2001 Russell King |
| 7 | * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. |
| 8 | * (C) 2003 Jun Nakajima <jun.nakajima@intel.com> |
| 9 | * (C) 2009 Alexander Clouter <alex@digriz.org.uk> |
| 10 | * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
| 16 | |
Borislav Petkov | beb0ff3 | 2013-04-02 12:26:15 +0000 | [diff] [blame] | 17 | #ifndef _CPUFREQ_GOVERNOR_H |
| 18 | #define _CPUFREQ_GOVERNOR_H |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 19 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 20 | #include <linux/cpufreq.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 21 | #include <linux/kernel_stat.h> |
| 22 | #include <linux/module.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * The polling frequency depends on the capability of the processor. Default |
| 27 | * polling frequency is 1000 times the transition latency of the processor. The |
Stratos Karafotis | c4afc41 | 2013-08-26 21:42:21 +0300 | [diff] [blame] | 28 | * governor will work on any processor with transition latency <= 10ms, using |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 29 | * appropriate sampling rate. |
| 30 | * |
Stratos Karafotis | c4afc41 | 2013-08-26 21:42:21 +0300 | [diff] [blame] | 31 | * For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL) |
| 32 | * this governor will not work. All times here are in us (micro seconds). |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 33 | */ |
| 34 | #define MIN_SAMPLING_RATE_RATIO (2) |
| 35 | #define LATENCY_MULTIPLIER (1000) |
Viresh Kumar | 98104ee | 2013-02-26 15:07:24 +0530 | [diff] [blame] | 36 | #define MIN_LATENCY_MULTIPLIER (20) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 37 | #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000) |
| 38 | |
| 39 | /* Ondemand Sampling types */ |
| 40 | enum {OD_NORMAL_SAMPLE, OD_SUB_SAMPLE}; |
| 41 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 42 | /* |
| 43 | * Macro for creating governors sysfs routines |
| 44 | * |
| 45 | * - gov_sys: One governor instance per whole system |
| 46 | * - gov_pol: One governor instance per policy |
| 47 | */ |
| 48 | |
| 49 | /* Create attributes */ |
| 50 | #define gov_sys_attr_ro(_name) \ |
| 51 | static struct global_attr _name##_gov_sys = \ |
| 52 | __ATTR(_name, 0444, show_##_name##_gov_sys, NULL) |
| 53 | |
| 54 | #define gov_sys_attr_rw(_name) \ |
| 55 | static struct global_attr _name##_gov_sys = \ |
| 56 | __ATTR(_name, 0644, show_##_name##_gov_sys, store_##_name##_gov_sys) |
| 57 | |
| 58 | #define gov_pol_attr_ro(_name) \ |
| 59 | static struct freq_attr _name##_gov_pol = \ |
| 60 | __ATTR(_name, 0444, show_##_name##_gov_pol, NULL) |
| 61 | |
| 62 | #define gov_pol_attr_rw(_name) \ |
| 63 | static struct freq_attr _name##_gov_pol = \ |
| 64 | __ATTR(_name, 0644, show_##_name##_gov_pol, store_##_name##_gov_pol) |
| 65 | |
| 66 | #define gov_sys_pol_attr_rw(_name) \ |
| 67 | gov_sys_attr_rw(_name); \ |
| 68 | gov_pol_attr_rw(_name) |
| 69 | |
| 70 | #define gov_sys_pol_attr_ro(_name) \ |
| 71 | gov_sys_attr_ro(_name); \ |
| 72 | gov_pol_attr_ro(_name) |
| 73 | |
| 74 | /* Create show/store routines */ |
| 75 | #define show_one(_gov, file_name) \ |
| 76 | static ssize_t show_##file_name##_gov_sys \ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 77 | (struct kobject *kobj, struct attribute *attr, char *buf) \ |
| 78 | { \ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 79 | struct _gov##_dbs_tuners *tuners = _gov##_dbs_cdata.gdbs_data->tuners; \ |
| 80 | return sprintf(buf, "%u\n", tuners->file_name); \ |
| 81 | } \ |
| 82 | \ |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 83 | static ssize_t show_##file_name##_gov_pol \ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 84 | (struct cpufreq_policy *policy, char *buf) \ |
| 85 | { \ |
| 86 | struct dbs_data *dbs_data = policy->governor_data; \ |
| 87 | struct _gov##_dbs_tuners *tuners = dbs_data->tuners; \ |
| 88 | return sprintf(buf, "%u\n", tuners->file_name); \ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 91 | #define store_one(_gov, file_name) \ |
| 92 | static ssize_t store_##file_name##_gov_sys \ |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 93 | (struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) \ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 94 | { \ |
| 95 | struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \ |
| 96 | return store_##file_name(dbs_data, buf, count); \ |
| 97 | } \ |
| 98 | \ |
| 99 | static ssize_t store_##file_name##_gov_pol \ |
| 100 | (struct cpufreq_policy *policy, const char *buf, size_t count) \ |
| 101 | { \ |
| 102 | struct dbs_data *dbs_data = policy->governor_data; \ |
| 103 | return store_##file_name(dbs_data, buf, count); \ |
| 104 | } |
| 105 | |
| 106 | #define show_store_one(_gov, file_name) \ |
| 107 | show_one(_gov, file_name); \ |
| 108 | store_one(_gov, file_name) |
| 109 | |
| 110 | /* create helper routines */ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 111 | #define define_get_cpu_dbs_routines(_dbs_info) \ |
Viresh Kumar | 875b850 | 2015-06-19 17:18:03 +0530 | [diff] [blame] | 112 | static struct cpu_dbs_info *get_cpu_cdbs(int cpu) \ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 113 | { \ |
| 114 | return &per_cpu(_dbs_info, cpu).cdbs; \ |
| 115 | } \ |
| 116 | \ |
| 117 | static void *get_cpu_dbs_info_s(int cpu) \ |
| 118 | { \ |
| 119 | return &per_cpu(_dbs_info, cpu); \ |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Abbreviations: |
| 124 | * dbs: used as a shortform for demand based switching It helps to keep variable |
| 125 | * names smaller, simpler |
| 126 | * cdbs: common dbs |
Namhyung Kim | e5dde92 | 2013-02-28 05:38:00 +0000 | [diff] [blame] | 127 | * od_*: On-demand governor |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 128 | * cs_*: Conservative governor |
| 129 | */ |
| 130 | |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 131 | /* Common to all CPUs of a policy */ |
| 132 | struct cpu_common_dbs_info { |
| 133 | struct cpufreq_policy *policy; |
| 134 | /* |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame^] | 135 | * Per policy mutex that serializes load evaluation from limit-change |
| 136 | * and work-handler. |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 137 | */ |
| 138 | struct mutex timer_mutex; |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame^] | 139 | |
| 140 | /* |
| 141 | * Per policy lock that serializes access to queuing work from timer |
| 142 | * handlers. |
| 143 | */ |
| 144 | spinlock_t timer_lock; |
| 145 | |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 146 | ktime_t time_stamp; |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame^] | 147 | unsigned int skip_work; |
| 148 | struct work_struct work; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 149 | }; |
| 150 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 151 | /* Per cpu structures */ |
Viresh Kumar | 875b850 | 2015-06-19 17:18:03 +0530 | [diff] [blame] | 152 | struct cpu_dbs_info { |
Viresh Kumar | 1e7586a | 2012-10-26 00:51:21 +0200 | [diff] [blame] | 153 | u64 prev_cpu_idle; |
| 154 | u64 prev_cpu_wall; |
| 155 | u64 prev_cpu_nice; |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 156 | /* |
Viresh Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 157 | * Used to keep track of load in the previous interval. However, when |
| 158 | * explicitly set to zero, it is used as a flag to ensure that we copy |
| 159 | * the previous load to the current interval only once, upon the first |
| 160 | * wake-up from idle. |
Srivatsa S. Bhat | 18b46ab | 2014-06-08 02:11:43 +0530 | [diff] [blame] | 161 | */ |
Viresh Kumar | c8ae481 | 2014-06-09 14:21:24 +0530 | [diff] [blame] | 162 | unsigned int prev_load; |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame^] | 163 | struct timer_list timer; |
Viresh Kumar | 44152cb | 2015-07-18 11:30:59 +0530 | [diff] [blame] | 164 | struct cpu_common_dbs_info *shared; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | struct od_cpu_dbs_info_s { |
Viresh Kumar | 875b850 | 2015-06-19 17:18:03 +0530 | [diff] [blame] | 168 | struct cpu_dbs_info cdbs; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 169 | struct cpufreq_frequency_table *freq_table; |
| 170 | unsigned int freq_lo; |
| 171 | unsigned int freq_lo_jiffies; |
| 172 | unsigned int freq_hi_jiffies; |
| 173 | unsigned int rate_mult; |
| 174 | unsigned int sample_type:1; |
| 175 | }; |
| 176 | |
| 177 | struct cs_cpu_dbs_info_s { |
Viresh Kumar | 875b850 | 2015-06-19 17:18:03 +0530 | [diff] [blame] | 178 | struct cpu_dbs_info cdbs; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 179 | unsigned int down_skip; |
| 180 | unsigned int requested_freq; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 181 | }; |
| 182 | |
Stratos Karafotis | c4afc41 | 2013-08-26 21:42:21 +0300 | [diff] [blame] | 183 | /* Per policy Governors sysfs tunables */ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 184 | struct od_dbs_tuners { |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 185 | unsigned int ignore_nice_load; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 186 | unsigned int sampling_rate; |
| 187 | unsigned int sampling_down_factor; |
| 188 | unsigned int up_threshold; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 189 | unsigned int powersave_bias; |
| 190 | unsigned int io_is_busy; |
| 191 | }; |
| 192 | |
| 193 | struct cs_dbs_tuners { |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 194 | unsigned int ignore_nice_load; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 195 | unsigned int sampling_rate; |
| 196 | unsigned int sampling_down_factor; |
| 197 | unsigned int up_threshold; |
| 198 | unsigned int down_threshold; |
| 199 | unsigned int freq_step; |
| 200 | }; |
| 201 | |
Stratos Karafotis | c4afc41 | 2013-08-26 21:42:21 +0300 | [diff] [blame] | 202 | /* Common Governor data across policies */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 203 | struct dbs_data; |
| 204 | struct common_dbs_data { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 205 | /* Common across governors */ |
| 206 | #define GOV_ONDEMAND 0 |
| 207 | #define GOV_CONSERVATIVE 1 |
| 208 | int governor; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 209 | struct attribute_group *attr_group_gov_sys; /* one governor - system */ |
| 210 | struct attribute_group *attr_group_gov_pol; /* one governor - policy */ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 211 | |
Viresh Kumar | 0b981e7 | 2013-10-02 14:13:18 +0530 | [diff] [blame] | 212 | /* |
| 213 | * Common data for platforms that don't set |
| 214 | * CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
| 215 | */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 216 | struct dbs_data *gdbs_data; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 217 | |
Viresh Kumar | 875b850 | 2015-06-19 17:18:03 +0530 | [diff] [blame] | 218 | struct cpu_dbs_info *(*get_cpu_cdbs)(int cpu); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 219 | void *(*get_cpu_dbs_info_s)(int cpu); |
Viresh Kumar | affde5d | 2015-12-03 09:37:51 +0530 | [diff] [blame] | 220 | unsigned int (*gov_dbs_timer)(struct cpufreq_policy *policy, |
Viresh Kumar | 43e0ee3 | 2015-07-18 11:31:00 +0530 | [diff] [blame] | 221 | bool modify_all); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 222 | void (*gov_check_cpu)(int cpu, unsigned int load); |
Viresh Kumar | 8e0484d | 2015-06-03 15:57:11 +0530 | [diff] [blame] | 223 | int (*init)(struct dbs_data *dbs_data, bool notify); |
| 224 | void (*exit)(struct dbs_data *dbs_data, bool notify); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 225 | |
| 226 | /* Governor specific ops, see below */ |
| 227 | void *gov_ops; |
Viresh Kumar | 732b6d6 | 2015-06-03 15:57:13 +0530 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * Protects governor's data (struct dbs_data and struct common_dbs_data) |
| 231 | */ |
| 232 | struct mutex mutex; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 233 | }; |
| 234 | |
Stratos Karafotis | c4afc41 | 2013-08-26 21:42:21 +0300 | [diff] [blame] | 235 | /* Governor Per policy data */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 236 | struct dbs_data { |
| 237 | struct common_dbs_data *cdata; |
| 238 | unsigned int min_sampling_rate; |
Viresh Kumar | a97c98a | 2013-04-30 14:32:17 +0000 | [diff] [blame] | 239 | int usage_count; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 240 | void *tuners; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 243 | /* Governor specific ops, will be passed to dbs_data->gov_ops */ |
| 244 | struct od_ops { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 245 | void (*powersave_bias_init_cpu)(int cpu); |
| 246 | unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy, |
| 247 | unsigned int freq_next, unsigned int relation); |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 248 | void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 249 | }; |
| 250 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 251 | static inline int delay_for_sampling_rate(unsigned int sampling_rate) |
| 252 | { |
| 253 | int delay = usecs_to_jiffies(sampling_rate); |
| 254 | |
| 255 | /* We want all CPUs to do sampling nearly on same jiffy */ |
| 256 | if (num_online_cpus() > 1) |
| 257 | delay -= jiffies % delay; |
| 258 | |
| 259 | return delay; |
| 260 | } |
| 261 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 262 | #define declare_show_sampling_rate_min(_gov) \ |
| 263 | static ssize_t show_sampling_rate_min_gov_sys \ |
| 264 | (struct kobject *kobj, struct attribute *attr, char *buf) \ |
| 265 | { \ |
| 266 | struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \ |
| 267 | return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \ |
| 268 | } \ |
| 269 | \ |
| 270 | static ssize_t show_sampling_rate_min_gov_pol \ |
| 271 | (struct cpufreq_policy *policy, char *buf) \ |
| 272 | { \ |
| 273 | struct dbs_data *dbs_data = policy->governor_data; \ |
| 274 | return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \ |
| 275 | } |
| 276 | |
Jane Li | 6f1e4ef | 2014-01-03 17:17:41 +0800 | [diff] [blame] | 277 | extern struct mutex cpufreq_governor_lock; |
| 278 | |
Viresh Kumar | 70f43e5 | 2015-12-09 07:34:42 +0530 | [diff] [blame^] | 279 | void gov_add_timers(struct cpufreq_policy *policy, unsigned int delay); |
| 280 | void gov_cancel_work(struct cpu_common_dbs_info *shared); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 281 | void dbs_check_cpu(struct dbs_data *dbs_data, int cpu); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 282 | int cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 283 | struct common_dbs_data *cdata, unsigned int event); |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 284 | void od_register_powersave_bias_handler(unsigned int (*f) |
| 285 | (struct cpufreq_policy *, unsigned int, unsigned int), |
| 286 | unsigned int powersave_bias); |
| 287 | void od_unregister_powersave_bias_handler(void); |
Borislav Petkov | beb0ff3 | 2013-04-02 12:26:15 +0000 | [diff] [blame] | 288 | #endif /* _CPUFREQ_GOVERNOR_H */ |