Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 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) 2004 Alexander Clouter <alex-kernel@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/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/smp.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/ctype.h> |
| 20 | #include <linux/cpufreq.h> |
| 21 | #include <linux/sysctl.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/sysfs.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/kmod.h> |
| 27 | #include <linux/workqueue.h> |
| 28 | #include <linux/jiffies.h> |
| 29 | #include <linux/kernel_stat.h> |
| 30 | #include <linux/percpu.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 32 | /* |
| 33 | * dbs is used in this file as a shortform for demandbased switching |
| 34 | * It helps to keep variable names smaller, simpler |
| 35 | */ |
| 36 | |
| 37 | #define DEF_FREQUENCY_UP_THRESHOLD (80) |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 38 | #define DEF_FREQUENCY_DOWN_THRESHOLD (20) |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * The polling frequency of this governor depends on the capability of |
| 42 | * the processor. Default polling frequency is 1000 times the transition |
| 43 | * latency of the processor. The governor will work on any processor with |
| 44 | * transition latency <= 10mS, using appropriate sampling |
| 45 | * rate. |
| 46 | * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL) |
| 47 | * this governor will not work. |
| 48 | * All times here are in uS. |
| 49 | */ |
| 50 | static unsigned int def_sampling_rate; |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 51 | #define MIN_SAMPLING_RATE_RATIO (2) |
| 52 | /* for correct statistics, we need at least 10 ticks between each measure */ |
| 53 | #define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10)) |
| 54 | #define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO) |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 55 | #define MAX_SAMPLING_RATE (500 * def_sampling_rate) |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 56 | #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000) |
| 57 | #define DEF_SAMPLING_DOWN_FACTOR (1) |
| 58 | #define MAX_SAMPLING_DOWN_FACTOR (10) |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 59 | #define TRANSITION_LATENCY_LIMIT (10 * 1000) |
| 60 | |
| 61 | static void do_dbs_timer(void *data); |
| 62 | |
| 63 | struct cpu_dbs_info_s { |
| 64 | struct cpufreq_policy *cur_policy; |
| 65 | unsigned int prev_cpu_idle_up; |
| 66 | unsigned int prev_cpu_idle_down; |
| 67 | unsigned int enable; |
| 68 | }; |
| 69 | static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info); |
| 70 | |
| 71 | static unsigned int dbs_enable; /* number of CPUs using this policy */ |
| 72 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 73 | static DEFINE_MUTEX (dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 74 | static DECLARE_WORK (dbs_work, do_dbs_timer, NULL); |
| 75 | |
| 76 | struct dbs_tuners { |
| 77 | unsigned int sampling_rate; |
| 78 | unsigned int sampling_down_factor; |
| 79 | unsigned int up_threshold; |
| 80 | unsigned int down_threshold; |
| 81 | unsigned int ignore_nice; |
| 82 | unsigned int freq_step; |
| 83 | }; |
| 84 | |
| 85 | static struct dbs_tuners dbs_tuners_ins = { |
| 86 | .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, |
| 87 | .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD, |
| 88 | .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR, |
| 89 | }; |
| 90 | |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 91 | static inline unsigned int get_cpu_idle_time(unsigned int cpu) |
| 92 | { |
| 93 | return kstat_cpu(cpu).cpustat.idle + |
| 94 | kstat_cpu(cpu).cpustat.iowait + |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 95 | ( dbs_tuners_ins.ignore_nice ? |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 96 | kstat_cpu(cpu).cpustat.nice : |
| 97 | 0); |
| 98 | } |
| 99 | |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 100 | /************************** sysfs interface ************************/ |
| 101 | static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf) |
| 102 | { |
| 103 | return sprintf (buf, "%u\n", MAX_SAMPLING_RATE); |
| 104 | } |
| 105 | |
| 106 | static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf) |
| 107 | { |
| 108 | return sprintf (buf, "%u\n", MIN_SAMPLING_RATE); |
| 109 | } |
| 110 | |
| 111 | #define define_one_ro(_name) \ |
| 112 | static struct freq_attr _name = \ |
| 113 | __ATTR(_name, 0444, show_##_name, NULL) |
| 114 | |
| 115 | define_one_ro(sampling_rate_max); |
| 116 | define_one_ro(sampling_rate_min); |
| 117 | |
| 118 | /* cpufreq_conservative Governor Tunables */ |
| 119 | #define show_one(file_name, object) \ |
| 120 | static ssize_t show_##file_name \ |
| 121 | (struct cpufreq_policy *unused, char *buf) \ |
| 122 | { \ |
| 123 | return sprintf(buf, "%u\n", dbs_tuners_ins.object); \ |
| 124 | } |
| 125 | show_one(sampling_rate, sampling_rate); |
| 126 | show_one(sampling_down_factor, sampling_down_factor); |
| 127 | show_one(up_threshold, up_threshold); |
| 128 | show_one(down_threshold, down_threshold); |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 129 | show_one(ignore_nice_load, ignore_nice); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 130 | show_one(freq_step, freq_step); |
| 131 | |
| 132 | static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused, |
| 133 | const char *buf, size_t count) |
| 134 | { |
| 135 | unsigned int input; |
| 136 | int ret; |
| 137 | ret = sscanf (buf, "%u", &input); |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 138 | if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 139 | return -EINVAL; |
| 140 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 141 | mutex_lock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 142 | dbs_tuners_ins.sampling_down_factor = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 143 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 144 | |
| 145 | return count; |
| 146 | } |
| 147 | |
| 148 | static ssize_t store_sampling_rate(struct cpufreq_policy *unused, |
| 149 | const char *buf, size_t count) |
| 150 | { |
| 151 | unsigned int input; |
| 152 | int ret; |
| 153 | ret = sscanf (buf, "%u", &input); |
| 154 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 155 | mutex_lock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 156 | if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) { |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 157 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | dbs_tuners_ins.sampling_rate = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 162 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 163 | |
| 164 | return count; |
| 165 | } |
| 166 | |
| 167 | static ssize_t store_up_threshold(struct cpufreq_policy *unused, |
| 168 | const char *buf, size_t count) |
| 169 | { |
| 170 | unsigned int input; |
| 171 | int ret; |
| 172 | ret = sscanf (buf, "%u", &input); |
| 173 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 174 | mutex_lock(&dbs_mutex); |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 175 | if (ret != 1 || input > 100 || input < 0 || |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 176 | input <= dbs_tuners_ins.down_threshold) { |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 177 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 178 | return -EINVAL; |
| 179 | } |
| 180 | |
| 181 | dbs_tuners_ins.up_threshold = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 182 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 183 | |
| 184 | return count; |
| 185 | } |
| 186 | |
| 187 | static ssize_t store_down_threshold(struct cpufreq_policy *unused, |
| 188 | const char *buf, size_t count) |
| 189 | { |
| 190 | unsigned int input; |
| 191 | int ret; |
| 192 | ret = sscanf (buf, "%u", &input); |
| 193 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 194 | mutex_lock(&dbs_mutex); |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 195 | if (ret != 1 || input > 100 || input < 0 || |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 196 | input >= dbs_tuners_ins.up_threshold) { |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 197 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | dbs_tuners_ins.down_threshold = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 202 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 203 | |
| 204 | return count; |
| 205 | } |
| 206 | |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 207 | static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy, |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 208 | const char *buf, size_t count) |
| 209 | { |
| 210 | unsigned int input; |
| 211 | int ret; |
| 212 | |
| 213 | unsigned int j; |
| 214 | |
| 215 | ret = sscanf (buf, "%u", &input); |
| 216 | if ( ret != 1 ) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | if ( input > 1 ) |
| 220 | input = 1; |
| 221 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 222 | mutex_lock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 223 | if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */ |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 224 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 225 | return count; |
| 226 | } |
| 227 | dbs_tuners_ins.ignore_nice = input; |
| 228 | |
| 229 | /* we need to re-evaluate prev_cpu_idle_up and prev_cpu_idle_down */ |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 230 | for_each_online_cpu(j) { |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 231 | struct cpu_dbs_info_s *j_dbs_info; |
| 232 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 233 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 234 | j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up; |
| 235 | } |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 236 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 237 | |
| 238 | return count; |
| 239 | } |
| 240 | |
| 241 | static ssize_t store_freq_step(struct cpufreq_policy *policy, |
| 242 | const char *buf, size_t count) |
| 243 | { |
| 244 | unsigned int input; |
| 245 | int ret; |
| 246 | |
| 247 | ret = sscanf (buf, "%u", &input); |
| 248 | |
| 249 | if ( ret != 1 ) |
| 250 | return -EINVAL; |
| 251 | |
| 252 | if ( input > 100 ) |
| 253 | input = 100; |
| 254 | |
| 255 | /* no need to test here if freq_step is zero as the user might actually |
| 256 | * want this, they would be crazy though :) */ |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 257 | mutex_lock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 258 | dbs_tuners_ins.freq_step = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 259 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 260 | |
| 261 | return count; |
| 262 | } |
| 263 | |
| 264 | #define define_one_rw(_name) \ |
| 265 | static struct freq_attr _name = \ |
| 266 | __ATTR(_name, 0644, show_##_name, store_##_name) |
| 267 | |
| 268 | define_one_rw(sampling_rate); |
| 269 | define_one_rw(sampling_down_factor); |
| 270 | define_one_rw(up_threshold); |
| 271 | define_one_rw(down_threshold); |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 272 | define_one_rw(ignore_nice_load); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 273 | define_one_rw(freq_step); |
| 274 | |
| 275 | static struct attribute * dbs_attributes[] = { |
| 276 | &sampling_rate_max.attr, |
| 277 | &sampling_rate_min.attr, |
| 278 | &sampling_rate.attr, |
| 279 | &sampling_down_factor.attr, |
| 280 | &up_threshold.attr, |
| 281 | &down_threshold.attr, |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 282 | &ignore_nice_load.attr, |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 283 | &freq_step.attr, |
| 284 | NULL |
| 285 | }; |
| 286 | |
| 287 | static struct attribute_group dbs_attr_group = { |
| 288 | .attrs = dbs_attributes, |
| 289 | .name = "conservative", |
| 290 | }; |
| 291 | |
| 292 | /************************** sysfs end ************************/ |
| 293 | |
| 294 | static void dbs_check_cpu(int cpu) |
| 295 | { |
| 296 | unsigned int idle_ticks, up_idle_ticks, down_idle_ticks; |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 297 | unsigned int tmp_idle_ticks, total_idle_ticks; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 298 | unsigned int freq_step; |
| 299 | unsigned int freq_down_sampling_rate; |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 300 | static unsigned short down_skip[NR_CPUS]; |
| 301 | static unsigned int requested_freq[NR_CPUS]; |
| 302 | static unsigned int init_flag = NR_CPUS; |
| 303 | struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cpu_dbs_info, cpu); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 304 | struct cpufreq_policy *policy; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 305 | |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 306 | if (!this_dbs_info->enable) |
| 307 | return; |
| 308 | |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 309 | if ( init_flag != 0 ) { |
| 310 | for_each_cpu(init_flag) { |
| 311 | down_skip[init_flag] = 0; |
| 312 | /* I doubt a CPU exists with a freq of 0hz :) */ |
| 313 | requested_freq[init_flag] = 0; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 314 | } |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 315 | init_flag = 0; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 318 | /* |
| 319 | * If its a freshly initialised cpu we setup requested_freq. This |
| 320 | * check could be avoided if we did not care about a first time |
| 321 | * stunted increase in CPU speed when there is a load. I feel we |
| 322 | * should be initialising this to something. The removal of a CPU |
| 323 | * is not a problem, after a short time the CPU should settle down |
| 324 | * to a 'natural' frequency. |
| 325 | */ |
| 326 | if (requested_freq[cpu] == 0) |
| 327 | requested_freq[cpu] = this_dbs_info->cur_policy->cur; |
| 328 | |
| 329 | policy = this_dbs_info->cur_policy; |
| 330 | |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 331 | /* |
| 332 | * The default safe range is 20% to 80% |
| 333 | * Every sampling_rate, we check |
| 334 | * - If current idle time is less than 20%, then we try to |
| 335 | * increase frequency |
| 336 | * Every sampling_rate*sampling_down_factor, we check |
| 337 | * - If current idle time is more than 80%, then we try to |
| 338 | * decrease frequency |
| 339 | * |
| 340 | * Any frequency increase takes it to the maximum frequency. |
| 341 | * Frequency reduction happens at minimum steps of |
| 342 | * 5% (default) of max_frequency |
| 343 | */ |
| 344 | |
| 345 | /* Check for frequency increase */ |
Dave Jones | 9c7d269 | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 346 | idle_ticks = UINT_MAX; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 347 | |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 348 | /* Check for frequency increase */ |
| 349 | total_idle_ticks = get_cpu_idle_time(cpu); |
| 350 | tmp_idle_ticks = total_idle_ticks - |
| 351 | this_dbs_info->prev_cpu_idle_up; |
| 352 | this_dbs_info->prev_cpu_idle_up = total_idle_ticks; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 353 | |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 354 | if (tmp_idle_ticks < idle_ticks) |
| 355 | idle_ticks = tmp_idle_ticks; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 356 | |
| 357 | /* Scale idle ticks by 100 and compare with up and down ticks */ |
| 358 | idle_ticks *= 100; |
| 359 | up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) * |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 360 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 361 | |
| 362 | if (idle_ticks < up_idle_ticks) { |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 363 | down_skip[cpu] = 0; |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 364 | this_dbs_info->prev_cpu_idle_down = |
| 365 | this_dbs_info->prev_cpu_idle_up; |
Dave Jones | 790d76f | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 366 | |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 367 | /* if we are already at full speed then break out early */ |
| 368 | if (requested_freq[cpu] == policy->max) |
| 369 | return; |
| 370 | |
| 371 | freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100; |
| 372 | |
| 373 | /* max freq cannot be less than 100. But who knows.... */ |
| 374 | if (unlikely(freq_step == 0)) |
| 375 | freq_step = 5; |
| 376 | |
| 377 | requested_freq[cpu] += freq_step; |
| 378 | if (requested_freq[cpu] > policy->max) |
| 379 | requested_freq[cpu] = policy->max; |
| 380 | |
| 381 | __cpufreq_driver_target(policy, requested_freq[cpu], |
| 382 | CPUFREQ_RELATION_H); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
| 386 | /* Check for frequency decrease */ |
| 387 | down_skip[cpu]++; |
| 388 | if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor) |
| 389 | return; |
| 390 | |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 391 | /* Check for frequency decrease */ |
| 392 | total_idle_ticks = this_dbs_info->prev_cpu_idle_up; |
| 393 | tmp_idle_ticks = total_idle_ticks - |
| 394 | this_dbs_info->prev_cpu_idle_down; |
| 395 | this_dbs_info->prev_cpu_idle_down = total_idle_ticks; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 396 | |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 397 | if (tmp_idle_ticks < idle_ticks) |
| 398 | idle_ticks = tmp_idle_ticks; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 399 | |
| 400 | /* Scale idle ticks by 100 and compare with up and down ticks */ |
| 401 | idle_ticks *= 100; |
| 402 | down_skip[cpu] = 0; |
| 403 | |
| 404 | freq_down_sampling_rate = dbs_tuners_ins.sampling_rate * |
| 405 | dbs_tuners_ins.sampling_down_factor; |
| 406 | down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) * |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 407 | usecs_to_jiffies(freq_down_sampling_rate); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 408 | |
Dave Jones | 9c7d269 | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 409 | if (idle_ticks > down_idle_ticks) { |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 410 | /* |
| 411 | * if we are already at the lowest speed then break out early |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 412 | * or if we 'cannot' reduce the speed as the user might want |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 413 | * freq_step to be zero |
| 414 | */ |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 415 | if (requested_freq[cpu] == policy->min |
| 416 | || dbs_tuners_ins.freq_step == 0) |
| 417 | return; |
| 418 | |
| 419 | freq_step = (dbs_tuners_ins.freq_step * policy->max) / 100; |
| 420 | |
| 421 | /* max freq cannot be less than 100. But who knows.... */ |
| 422 | if (unlikely(freq_step == 0)) |
| 423 | freq_step = 5; |
| 424 | |
| 425 | requested_freq[cpu] -= freq_step; |
| 426 | if (requested_freq[cpu] < policy->min) |
| 427 | requested_freq[cpu] = policy->min; |
| 428 | |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 429 | __cpufreq_driver_target(policy, requested_freq[cpu], |
| 430 | CPUFREQ_RELATION_H); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | static void do_dbs_timer(void *data) |
| 436 | { |
| 437 | int i; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 438 | mutex_lock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 439 | for_each_online_cpu(i) |
| 440 | dbs_check_cpu(i); |
| 441 | schedule_delayed_work(&dbs_work, |
| 442 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 443 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static inline void dbs_timer_init(void) |
| 447 | { |
| 448 | INIT_WORK(&dbs_work, do_dbs_timer, NULL); |
| 449 | schedule_delayed_work(&dbs_work, |
| 450 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | static inline void dbs_timer_exit(void) |
| 455 | { |
| 456 | cancel_delayed_work(&dbs_work); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 461 | unsigned int event) |
| 462 | { |
| 463 | unsigned int cpu = policy->cpu; |
| 464 | struct cpu_dbs_info_s *this_dbs_info; |
| 465 | unsigned int j; |
| 466 | |
| 467 | this_dbs_info = &per_cpu(cpu_dbs_info, cpu); |
| 468 | |
| 469 | switch (event) { |
| 470 | case CPUFREQ_GOV_START: |
| 471 | if ((!cpu_online(cpu)) || |
| 472 | (!policy->cur)) |
| 473 | return -EINVAL; |
| 474 | |
| 475 | if (policy->cpuinfo.transition_latency > |
| 476 | (TRANSITION_LATENCY_LIMIT * 1000)) |
| 477 | return -EINVAL; |
| 478 | if (this_dbs_info->enable) /* Already enabled */ |
| 479 | break; |
| 480 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 481 | mutex_lock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 482 | for_each_cpu_mask(j, policy->cpus) { |
| 483 | struct cpu_dbs_info_s *j_dbs_info; |
| 484 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
| 485 | j_dbs_info->cur_policy = policy; |
| 486 | |
Alexander Clouter | 08a28e2 | 2006-03-22 09:59:16 +0000 | [diff] [blame^] | 487 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(cpu); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 488 | j_dbs_info->prev_cpu_idle_down |
| 489 | = j_dbs_info->prev_cpu_idle_up; |
| 490 | } |
| 491 | this_dbs_info->enable = 1; |
| 492 | sysfs_create_group(&policy->kobj, &dbs_attr_group); |
| 493 | dbs_enable++; |
| 494 | /* |
| 495 | * Start the timerschedule work, when this governor |
| 496 | * is used for first time |
| 497 | */ |
| 498 | if (dbs_enable == 1) { |
| 499 | unsigned int latency; |
| 500 | /* policy latency is in nS. Convert it to uS first */ |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 501 | latency = policy->cpuinfo.transition_latency / 1000; |
| 502 | if (latency == 0) |
| 503 | latency = 1; |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 504 | |
Alexander Clouter | e8a0257 | 2006-03-22 09:56:23 +0000 | [diff] [blame] | 505 | def_sampling_rate = 10 * latency * |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 506 | DEF_SAMPLING_RATE_LATENCY_MULTIPLIER; |
Alexander Clouter | 2c906b3 | 2006-03-22 09:54:10 +0000 | [diff] [blame] | 507 | |
| 508 | if (def_sampling_rate < MIN_STAT_SAMPLING_RATE) |
| 509 | def_sampling_rate = MIN_STAT_SAMPLING_RATE; |
| 510 | |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 511 | dbs_tuners_ins.sampling_rate = def_sampling_rate; |
| 512 | dbs_tuners_ins.ignore_nice = 0; |
| 513 | dbs_tuners_ins.freq_step = 5; |
| 514 | |
| 515 | dbs_timer_init(); |
| 516 | } |
| 517 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 518 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 519 | break; |
| 520 | |
| 521 | case CPUFREQ_GOV_STOP: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 522 | mutex_lock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 523 | this_dbs_info->enable = 0; |
| 524 | sysfs_remove_group(&policy->kobj, &dbs_attr_group); |
| 525 | dbs_enable--; |
| 526 | /* |
| 527 | * Stop the timerschedule work, when this governor |
| 528 | * is used for first time |
| 529 | */ |
| 530 | if (dbs_enable == 0) |
| 531 | dbs_timer_exit(); |
| 532 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 533 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 534 | |
| 535 | break; |
| 536 | |
| 537 | case CPUFREQ_GOV_LIMITS: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 538 | mutex_lock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 539 | if (policy->max < this_dbs_info->cur_policy->cur) |
| 540 | __cpufreq_driver_target( |
| 541 | this_dbs_info->cur_policy, |
| 542 | policy->max, CPUFREQ_RELATION_H); |
| 543 | else if (policy->min > this_dbs_info->cur_policy->cur) |
| 544 | __cpufreq_driver_target( |
| 545 | this_dbs_info->cur_policy, |
| 546 | policy->min, CPUFREQ_RELATION_L); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 547 | mutex_unlock(&dbs_mutex); |
Dave Jones | b917083 | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 548 | break; |
| 549 | } |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static struct cpufreq_governor cpufreq_gov_dbs = { |
| 554 | .name = "conservative", |
| 555 | .governor = cpufreq_governor_dbs, |
| 556 | .owner = THIS_MODULE, |
| 557 | }; |
| 558 | |
| 559 | static int __init cpufreq_gov_dbs_init(void) |
| 560 | { |
| 561 | return cpufreq_register_governor(&cpufreq_gov_dbs); |
| 562 | } |
| 563 | |
| 564 | static void __exit cpufreq_gov_dbs_exit(void) |
| 565 | { |
| 566 | /* Make sure that the scheduled work is indeed not running */ |
| 567 | flush_scheduled_work(); |
| 568 | |
| 569 | cpufreq_unregister_governor(&cpufreq_gov_dbs); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | MODULE_AUTHOR ("Alexander Clouter <alex-kernel@digriz.org.uk>"); |
| 574 | MODULE_DESCRIPTION ("'cpufreq_conservative' - A dynamic cpufreq governor for " |
| 575 | "Low Latency Frequency Transition capable processors " |
| 576 | "optimised for use in a battery environment"); |
| 577 | MODULE_LICENSE ("GPL"); |
| 578 | |
| 579 | module_init(cpufreq_gov_dbs_init); |
| 580 | module_exit(cpufreq_gov_dbs_exit); |