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) \ |
| 112 | static struct cpu_dbs_common_info *get_cpu_cdbs(int cpu) \ |
| 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 | |
| 131 | /* Per cpu structures */ |
| 132 | struct cpu_dbs_common_info { |
| 133 | int cpu; |
Viresh Kumar | 1e7586a | 2012-10-26 00:51:21 +0200 | [diff] [blame] | 134 | u64 prev_cpu_idle; |
| 135 | u64 prev_cpu_wall; |
| 136 | u64 prev_cpu_nice; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 137 | struct cpufreq_policy *cur_policy; |
| 138 | struct delayed_work work; |
| 139 | /* |
| 140 | * percpu mutex that serializes governor limit change with gov_dbs_timer |
| 141 | * invocation. We do not want gov_dbs_timer to run when user is changing |
| 142 | * the governor or limits. |
| 143 | */ |
| 144 | struct mutex timer_mutex; |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 145 | ktime_t time_stamp; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | struct od_cpu_dbs_info_s { |
| 149 | struct cpu_dbs_common_info cdbs; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 150 | struct cpufreq_frequency_table *freq_table; |
| 151 | unsigned int freq_lo; |
| 152 | unsigned int freq_lo_jiffies; |
| 153 | unsigned int freq_hi_jiffies; |
| 154 | unsigned int rate_mult; |
| 155 | unsigned int sample_type:1; |
| 156 | }; |
| 157 | |
| 158 | struct cs_cpu_dbs_info_s { |
| 159 | struct cpu_dbs_common_info cdbs; |
| 160 | unsigned int down_skip; |
| 161 | unsigned int requested_freq; |
| 162 | unsigned int enable:1; |
| 163 | }; |
| 164 | |
Stratos Karafotis | c4afc41 | 2013-08-26 21:42:21 +0300 | [diff] [blame] | 165 | /* Per policy Governors sysfs tunables */ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 166 | struct od_dbs_tuners { |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 167 | unsigned int ignore_nice_load; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 168 | unsigned int sampling_rate; |
| 169 | unsigned int sampling_down_factor; |
| 170 | unsigned int up_threshold; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 171 | unsigned int powersave_bias; |
| 172 | unsigned int io_is_busy; |
| 173 | }; |
| 174 | |
| 175 | struct cs_dbs_tuners { |
Viresh Kumar | 6c4640c | 2013-08-05 12:28:02 +0530 | [diff] [blame] | 176 | unsigned int ignore_nice_load; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 177 | unsigned int sampling_rate; |
| 178 | unsigned int sampling_down_factor; |
| 179 | unsigned int up_threshold; |
| 180 | unsigned int down_threshold; |
| 181 | unsigned int freq_step; |
| 182 | }; |
| 183 | |
Stratos Karafotis | c4afc41 | 2013-08-26 21:42:21 +0300 | [diff] [blame] | 184 | /* Common Governor data across policies */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 185 | struct dbs_data; |
| 186 | struct common_dbs_data { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 187 | /* Common across governors */ |
| 188 | #define GOV_ONDEMAND 0 |
| 189 | #define GOV_CONSERVATIVE 1 |
| 190 | int governor; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 191 | struct attribute_group *attr_group_gov_sys; /* one governor - system */ |
| 192 | struct attribute_group *attr_group_gov_pol; /* one governor - policy */ |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 193 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 194 | /* Common data for platforms that don't set have_governor_per_policy */ |
| 195 | struct dbs_data *gdbs_data; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 196 | |
| 197 | struct cpu_dbs_common_info *(*get_cpu_cdbs)(int cpu); |
| 198 | void *(*get_cpu_dbs_info_s)(int cpu); |
| 199 | void (*gov_dbs_timer)(struct work_struct *work); |
| 200 | void (*gov_check_cpu)(int cpu, unsigned int load); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 201 | int (*init)(struct dbs_data *dbs_data); |
| 202 | void (*exit)(struct dbs_data *dbs_data); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 203 | |
| 204 | /* Governor specific ops, see below */ |
| 205 | void *gov_ops; |
| 206 | }; |
| 207 | |
Stratos Karafotis | c4afc41 | 2013-08-26 21:42:21 +0300 | [diff] [blame] | 208 | /* Governor Per policy data */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 209 | struct dbs_data { |
| 210 | struct common_dbs_data *cdata; |
| 211 | unsigned int min_sampling_rate; |
Viresh Kumar | a97c98a | 2013-04-30 14:32:17 +0000 | [diff] [blame] | 212 | int usage_count; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 213 | void *tuners; |
| 214 | |
| 215 | /* dbs_mutex protects dbs_enable in governor start/stop */ |
| 216 | struct mutex mutex; |
| 217 | }; |
| 218 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 219 | /* Governor specific ops, will be passed to dbs_data->gov_ops */ |
| 220 | struct od_ops { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 221 | void (*powersave_bias_init_cpu)(int cpu); |
| 222 | unsigned int (*powersave_bias_target)(struct cpufreq_policy *policy, |
| 223 | unsigned int freq_next, unsigned int relation); |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 224 | void (*freq_increase)(struct cpufreq_policy *policy, unsigned int freq); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | struct cs_ops { |
| 228 | struct notifier_block *notifier_block; |
| 229 | }; |
| 230 | |
| 231 | static inline int delay_for_sampling_rate(unsigned int sampling_rate) |
| 232 | { |
| 233 | int delay = usecs_to_jiffies(sampling_rate); |
| 234 | |
| 235 | /* We want all CPUs to do sampling nearly on same jiffy */ |
| 236 | if (num_online_cpus() > 1) |
| 237 | delay -= jiffies % delay; |
| 238 | |
| 239 | return delay; |
| 240 | } |
| 241 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 242 | #define declare_show_sampling_rate_min(_gov) \ |
| 243 | static ssize_t show_sampling_rate_min_gov_sys \ |
| 244 | (struct kobject *kobj, struct attribute *attr, char *buf) \ |
| 245 | { \ |
| 246 | struct dbs_data *dbs_data = _gov##_dbs_cdata.gdbs_data; \ |
| 247 | return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \ |
| 248 | } \ |
| 249 | \ |
| 250 | static ssize_t show_sampling_rate_min_gov_pol \ |
| 251 | (struct cpufreq_policy *policy, char *buf) \ |
| 252 | { \ |
| 253 | struct dbs_data *dbs_data = policy->governor_data; \ |
| 254 | return sprintf(buf, "%u\n", dbs_data->min_sampling_rate); \ |
| 255 | } |
| 256 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 257 | void dbs_check_cpu(struct dbs_data *dbs_data, int cpu); |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 258 | bool need_load_eval(struct cpu_dbs_common_info *cdbs, |
| 259 | unsigned int sampling_rate); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 260 | int cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 261 | struct common_dbs_data *cdata, unsigned int event); |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 262 | void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy, |
| 263 | unsigned int delay, bool all_cpus); |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 264 | void od_register_powersave_bias_handler(unsigned int (*f) |
| 265 | (struct cpufreq_policy *, unsigned int, unsigned int), |
| 266 | unsigned int powersave_bias); |
| 267 | void od_unregister_powersave_bias_handler(void); |
Borislav Petkov | beb0ff3 | 2013-04-02 12:26:15 +0000 | [diff] [blame] | 268 | #endif /* _CPUFREQ_GOVERNOR_H */ |