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