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