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