1 /* 2 * drivers/cpufreq/cpufreq_conservative.c 3 * 4 * Copyright (C) 2001 Russell King 5 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. 6 * Jun Nakajima <jun.nakajima@intel.com> 7 * (C) 2009 Alexander Clouter <alex@digriz.org.uk> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 */ 13 14 #include <linux/slab.h> 15 #include "cpufreq_governor.h" 16 17 struct cs_policy_dbs_info { 18 struct policy_dbs_info policy_dbs; 19 unsigned int down_skip; 20 unsigned int requested_freq; 21 }; 22 23 static inline struct cs_policy_dbs_info *to_dbs_info(struct policy_dbs_info *policy_dbs) 24 { 25 return container_of(policy_dbs, struct cs_policy_dbs_info, policy_dbs); 26 } 27 28 /* Conservative governor macros */ 29 #define DEF_FREQUENCY_UP_THRESHOLD (80) 30 #define DEF_FREQUENCY_DOWN_THRESHOLD (20) 31 #define DEF_FREQUENCY_STEP (5) 32 #define DEF_SAMPLING_DOWN_FACTOR (1) 33 #define MAX_SAMPLING_DOWN_FACTOR (10) 34 35 static inline unsigned int get_freq_target(struct cs_dbs_tuners *cs_tuners, 36 struct cpufreq_policy *policy) 37 { 38 unsigned int freq_target = (cs_tuners->freq_step * policy->max) / 100; 39 40 /* max freq cannot be less than 100. But who knows... */ 41 if (unlikely(freq_target == 0)) 42 freq_target = DEF_FREQUENCY_STEP; 43 44 return freq_target; 45 } 46 47 /* 48 * Every sampling_rate, we check, if current idle time is less than 20% 49 * (default), then we try to increase frequency. Every sampling_rate * 50 * sampling_down_factor, we check, if current idle time is more than 80% 51 * (default), then we try to decrease frequency 52 * 53 * Any frequency increase takes it to the maximum frequency. Frequency reduction 54 * happens at minimum steps of 5% (default) of maximum frequency 55 */ 56 static unsigned int cs_dbs_timer(struct cpufreq_policy *policy) 57 { 58 struct policy_dbs_info *policy_dbs = policy->governor_data; 59 struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs); 60 struct dbs_data *dbs_data = policy_dbs->dbs_data; 61 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; 62 unsigned int load = dbs_update(policy); 63 64 /* 65 * break out if we 'cannot' reduce the speed as the user might 66 * want freq_step to be zero 67 */ 68 if (cs_tuners->freq_step == 0) 69 goto out; 70 71 /* Check for frequency increase */ 72 if (load > dbs_data->up_threshold) { 73 dbs_info->down_skip = 0; 74 75 /* if we are already at full speed then break out early */ 76 if (dbs_info->requested_freq == policy->max) 77 goto out; 78 79 dbs_info->requested_freq += get_freq_target(cs_tuners, policy); 80 81 if (dbs_info->requested_freq > policy->max) 82 dbs_info->requested_freq = policy->max; 83 84 __cpufreq_driver_target(policy, dbs_info->requested_freq, 85 CPUFREQ_RELATION_H); 86 goto out; 87 } 88 89 /* if sampling_down_factor is active break out early */ 90 if (++dbs_info->down_skip < dbs_data->sampling_down_factor) 91 goto out; 92 dbs_info->down_skip = 0; 93 94 /* Check for frequency decrease */ 95 if (load < cs_tuners->down_threshold) { 96 unsigned int freq_target; 97 /* 98 * if we cannot reduce the frequency anymore, break out early 99 */ 100 if (policy->cur == policy->min) 101 goto out; 102 103 freq_target = get_freq_target(cs_tuners, policy); 104 if (dbs_info->requested_freq > freq_target) 105 dbs_info->requested_freq -= freq_target; 106 else 107 dbs_info->requested_freq = policy->min; 108 109 __cpufreq_driver_target(policy, dbs_info->requested_freq, 110 CPUFREQ_RELATION_L); 111 } 112 113 out: 114 return dbs_data->sampling_rate; 115 } 116 117 static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, 118 void *data); 119 120 static struct notifier_block cs_cpufreq_notifier_block = { 121 .notifier_call = dbs_cpufreq_notifier, 122 }; 123 124 /************************** sysfs interface ************************/ 125 static struct dbs_governor cs_dbs_gov; 126 127 static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data, 128 const char *buf, size_t count) 129 { 130 unsigned int input; 131 int ret; 132 ret = sscanf(buf, "%u", &input); 133 134 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) 135 return -EINVAL; 136 137 dbs_data->sampling_down_factor = input; 138 return count; 139 } 140 141 static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf, 142 size_t count) 143 { 144 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; 145 unsigned int input; 146 int ret; 147 ret = sscanf(buf, "%u", &input); 148 149 if (ret != 1 || input > 100 || input <= cs_tuners->down_threshold) 150 return -EINVAL; 151 152 dbs_data->up_threshold = input; 153 return count; 154 } 155 156 static ssize_t store_down_threshold(struct dbs_data *dbs_data, const char *buf, 157 size_t count) 158 { 159 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; 160 unsigned int input; 161 int ret; 162 ret = sscanf(buf, "%u", &input); 163 164 /* cannot be lower than 11 otherwise freq will not fall */ 165 if (ret != 1 || input < 11 || input > 100 || 166 input >= dbs_data->up_threshold) 167 return -EINVAL; 168 169 cs_tuners->down_threshold = input; 170 return count; 171 } 172 173 static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data, 174 const char *buf, size_t count) 175 { 176 unsigned int input; 177 int ret; 178 179 ret = sscanf(buf, "%u", &input); 180 if (ret != 1) 181 return -EINVAL; 182 183 if (input > 1) 184 input = 1; 185 186 if (input == dbs_data->ignore_nice_load) /* nothing to do */ 187 return count; 188 189 dbs_data->ignore_nice_load = input; 190 191 /* we need to re-evaluate prev_cpu_idle */ 192 gov_update_cpu_data(dbs_data); 193 194 return count; 195 } 196 197 static ssize_t store_freq_step(struct dbs_data *dbs_data, const char *buf, 198 size_t count) 199 { 200 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; 201 unsigned int input; 202 int ret; 203 ret = sscanf(buf, "%u", &input); 204 205 if (ret != 1) 206 return -EINVAL; 207 208 if (input > 100) 209 input = 100; 210 211 /* 212 * no need to test here if freq_step is zero as the user might actually 213 * want this, they would be crazy though :) 214 */ 215 cs_tuners->freq_step = input; 216 return count; 217 } 218 219 gov_show_one_common(sampling_rate); 220 gov_show_one_common(sampling_down_factor); 221 gov_show_one_common(up_threshold); 222 gov_show_one_common(ignore_nice_load); 223 gov_show_one_common(min_sampling_rate); 224 gov_show_one(cs, down_threshold); 225 gov_show_one(cs, freq_step); 226 227 gov_attr_rw(sampling_rate); 228 gov_attr_rw(sampling_down_factor); 229 gov_attr_rw(up_threshold); 230 gov_attr_rw(ignore_nice_load); 231 gov_attr_ro(min_sampling_rate); 232 gov_attr_rw(down_threshold); 233 gov_attr_rw(freq_step); 234 235 static struct attribute *cs_attributes[] = { 236 &min_sampling_rate.attr, 237 &sampling_rate.attr, 238 &sampling_down_factor.attr, 239 &up_threshold.attr, 240 &down_threshold.attr, 241 &ignore_nice_load.attr, 242 &freq_step.attr, 243 NULL 244 }; 245 246 /************************** sysfs end ************************/ 247 248 static struct policy_dbs_info *cs_alloc(void) 249 { 250 struct cs_policy_dbs_info *dbs_info; 251 252 dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL); 253 return dbs_info ? &dbs_info->policy_dbs : NULL; 254 } 255 256 static void cs_free(struct policy_dbs_info *policy_dbs) 257 { 258 kfree(to_dbs_info(policy_dbs)); 259 } 260 261 static int cs_init(struct dbs_data *dbs_data, bool notify) 262 { 263 struct cs_dbs_tuners *tuners; 264 265 tuners = kzalloc(sizeof(*tuners), GFP_KERNEL); 266 if (!tuners) { 267 pr_err("%s: kzalloc failed\n", __func__); 268 return -ENOMEM; 269 } 270 271 tuners->down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD; 272 tuners->freq_step = DEF_FREQUENCY_STEP; 273 dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD; 274 dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR; 275 dbs_data->ignore_nice_load = 0; 276 277 dbs_data->tuners = tuners; 278 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO * 279 jiffies_to_usecs(10); 280 281 if (notify) 282 cpufreq_register_notifier(&cs_cpufreq_notifier_block, 283 CPUFREQ_TRANSITION_NOTIFIER); 284 285 return 0; 286 } 287 288 static void cs_exit(struct dbs_data *dbs_data, bool notify) 289 { 290 if (notify) 291 cpufreq_unregister_notifier(&cs_cpufreq_notifier_block, 292 CPUFREQ_TRANSITION_NOTIFIER); 293 294 kfree(dbs_data->tuners); 295 } 296 297 static void cs_start(struct cpufreq_policy *policy) 298 { 299 struct cs_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data); 300 301 dbs_info->down_skip = 0; 302 dbs_info->requested_freq = policy->cur; 303 } 304 305 static struct dbs_governor cs_dbs_gov = { 306 .gov = { 307 .name = "conservative", 308 .governor = cpufreq_governor_dbs, 309 .max_transition_latency = TRANSITION_LATENCY_LIMIT, 310 .owner = THIS_MODULE, 311 }, 312 .kobj_type = { .default_attrs = cs_attributes }, 313 .gov_dbs_timer = cs_dbs_timer, 314 .alloc = cs_alloc, 315 .free = cs_free, 316 .init = cs_init, 317 .exit = cs_exit, 318 .start = cs_start, 319 }; 320 321 #define CPU_FREQ_GOV_CONSERVATIVE (&cs_dbs_gov.gov) 322 323 static int dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val, 324 void *data) 325 { 326 struct cpufreq_freqs *freq = data; 327 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(freq->cpu); 328 struct cs_policy_dbs_info *dbs_info; 329 330 if (!policy) 331 return 0; 332 333 /* policy isn't governed by conservative governor */ 334 if (policy->governor != CPU_FREQ_GOV_CONSERVATIVE) 335 return 0; 336 337 dbs_info = to_dbs_info(policy->governor_data); 338 /* 339 * we only care if our internally tracked freq moves outside the 'valid' 340 * ranges of frequency available to us otherwise we do not change it 341 */ 342 if (dbs_info->requested_freq > policy->max 343 || dbs_info->requested_freq < policy->min) 344 dbs_info->requested_freq = freq->new; 345 346 return 0; 347 } 348 349 static int __init cpufreq_gov_dbs_init(void) 350 { 351 return cpufreq_register_governor(CPU_FREQ_GOV_CONSERVATIVE); 352 } 353 354 static void __exit cpufreq_gov_dbs_exit(void) 355 { 356 cpufreq_unregister_governor(CPU_FREQ_GOV_CONSERVATIVE); 357 } 358 359 MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>"); 360 MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for " 361 "Low Latency Frequency Transition capable processors " 362 "optimised for use in a battery environment"); 363 MODULE_LICENSE("GPL"); 364 365 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE 366 struct cpufreq_governor *cpufreq_default_governor(void) 367 { 368 return CPU_FREQ_GOV_CONSERVATIVE; 369 } 370 371 fs_initcall(cpufreq_gov_dbs_init); 372 #else 373 module_init(cpufreq_gov_dbs_init); 374 #endif 375 module_exit(cpufreq_gov_dbs_exit); 376