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