Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/cpufreq/cpufreq_ondemand.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 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/smp.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/ctype.h> |
| 19 | #include <linux/cpufreq.h> |
| 20 | #include <linux/sysctl.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/sysfs.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/kmod.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/kernel_stat.h> |
| 29 | #include <linux/percpu.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 30 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 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 | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 38 | #define MIN_FREQUENCY_UP_THRESHOLD (11) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #define MAX_FREQUENCY_UP_THRESHOLD (100) |
| 40 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 41 | /* |
| 42 | * The polling frequency of this governor depends on the capability of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * the processor. Default polling frequency is 1000 times the transition |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 44 | * latency of the processor. The governor will work on any processor with |
| 45 | * transition latency <= 10mS, using appropriate sampling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 51 | static unsigned int def_sampling_rate; |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #define MAX_SAMPLING_RATE (500 * def_sampling_rate) |
| 57 | #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000) |
Dave Jones | e131832 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 58 | #define DEF_SAMPLING_DOWN_FACTOR (1) |
| 59 | #define MAX_SAMPLING_DOWN_FACTOR (10) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #define TRANSITION_LATENCY_LIMIT (10 * 1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | static void do_dbs_timer(void *data); |
| 63 | |
| 64 | struct cpu_dbs_info_s { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 65 | struct cpufreq_policy *cur_policy; |
| 66 | unsigned int prev_cpu_idle_up; |
| 67 | unsigned int prev_cpu_idle_down; |
| 68 | unsigned int enable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | }; |
| 70 | static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info); |
| 71 | |
| 72 | static unsigned int dbs_enable; /* number of CPUs using this policy */ |
| 73 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 74 | static DEFINE_MUTEX (dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | static DECLARE_WORK (dbs_work, do_dbs_timer, NULL); |
| 76 | |
Andi Kleen | 6810b54 | 2006-05-08 15:17:31 +0200 | [diff] [blame] | 77 | static struct workqueue_struct *dbs_workq; |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct dbs_tuners { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 80 | unsigned int sampling_rate; |
| 81 | unsigned int sampling_down_factor; |
| 82 | unsigned int up_threshold; |
| 83 | unsigned int ignore_nice; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static struct dbs_tuners dbs_tuners_ins = { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 87 | .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, |
| 88 | .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR, |
Eric Piel | 9cbad61 | 2006-03-10 11:35:27 +0200 | [diff] [blame] | 89 | .ignore_nice = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 92 | static inline unsigned int get_cpu_idle_time(unsigned int cpu) |
| 93 | { |
| 94 | return kstat_cpu(cpu).cpustat.idle + |
| 95 | kstat_cpu(cpu).cpustat.iowait + |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 96 | ( dbs_tuners_ins.ignore_nice ? |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 97 | kstat_cpu(cpu).cpustat.nice : |
| 98 | 0); |
| 99 | } |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /************************** sysfs interface ************************/ |
| 102 | static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf) |
| 103 | { |
| 104 | return sprintf (buf, "%u\n", MAX_SAMPLING_RATE); |
| 105 | } |
| 106 | |
| 107 | static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf) |
| 108 | { |
| 109 | return sprintf (buf, "%u\n", MIN_SAMPLING_RATE); |
| 110 | } |
| 111 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 112 | #define define_one_ro(_name) \ |
| 113 | static struct freq_attr _name = \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | __ATTR(_name, 0444, show_##_name, NULL) |
| 115 | |
| 116 | define_one_ro(sampling_rate_max); |
| 117 | define_one_ro(sampling_rate_min); |
| 118 | |
| 119 | /* cpufreq_ondemand Governor Tunables */ |
| 120 | #define show_one(file_name, object) \ |
| 121 | static ssize_t show_##file_name \ |
| 122 | (struct cpufreq_policy *unused, char *buf) \ |
| 123 | { \ |
| 124 | return sprintf(buf, "%u\n", dbs_tuners_ins.object); \ |
| 125 | } |
| 126 | show_one(sampling_rate, sampling_rate); |
| 127 | show_one(sampling_down_factor, sampling_down_factor); |
| 128 | show_one(up_threshold, up_threshold); |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 129 | show_one(ignore_nice_load, ignore_nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 131 | static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | const char *buf, size_t count) |
| 133 | { |
| 134 | unsigned int input; |
| 135 | int ret; |
| 136 | ret = sscanf (buf, "%u", &input); |
| 137 | if (ret != 1 ) |
| 138 | return -EINVAL; |
| 139 | |
Dave Jones | e131832 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 140 | if (input > MAX_SAMPLING_DOWN_FACTOR || input < 1) |
| 141 | return -EINVAL; |
| 142 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 143 | mutex_lock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | dbs_tuners_ins.sampling_down_factor = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 145 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | return count; |
| 148 | } |
| 149 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 150 | static ssize_t store_sampling_rate(struct cpufreq_policy *unused, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | const char *buf, size_t count) |
| 152 | { |
| 153 | unsigned int input; |
| 154 | int ret; |
| 155 | ret = sscanf (buf, "%u", &input); |
| 156 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 157 | mutex_lock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) { |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 159 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | dbs_tuners_ins.sampling_rate = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 164 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | return count; |
| 167 | } |
| 168 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 169 | static ssize_t store_up_threshold(struct cpufreq_policy *unused, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | const char *buf, size_t count) |
| 171 | { |
| 172 | unsigned int input; |
| 173 | int ret; |
| 174 | ret = sscanf (buf, "%u", &input); |
| 175 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 176 | mutex_lock(&dbs_mutex); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 177 | if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 178 | input < MIN_FREQUENCY_UP_THRESHOLD) { |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 179 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | dbs_tuners_ins.up_threshold = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 184 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | return count; |
| 187 | } |
| 188 | |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 189 | static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy, |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 190 | const char *buf, size_t count) |
| 191 | { |
| 192 | unsigned int input; |
| 193 | int ret; |
| 194 | |
| 195 | unsigned int j; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 196 | |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 197 | ret = sscanf (buf, "%u", &input); |
| 198 | if ( ret != 1 ) |
| 199 | return -EINVAL; |
| 200 | |
| 201 | if ( input > 1 ) |
| 202 | input = 1; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 203 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 204 | mutex_lock(&dbs_mutex); |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 205 | if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */ |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 206 | mutex_unlock(&dbs_mutex); |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 207 | return count; |
| 208 | } |
| 209 | dbs_tuners_ins.ignore_nice = input; |
| 210 | |
| 211 | /* 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] | 212 | for_each_online_cpu(j) { |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 213 | struct cpu_dbs_info_s *j_dbs_info; |
| 214 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 215 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j); |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 216 | j_dbs_info->prev_cpu_idle_down = j_dbs_info->prev_cpu_idle_up; |
| 217 | } |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 218 | mutex_unlock(&dbs_mutex); |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 219 | |
| 220 | return count; |
| 221 | } |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | #define define_one_rw(_name) \ |
| 224 | static struct freq_attr _name = \ |
| 225 | __ATTR(_name, 0644, show_##_name, store_##_name) |
| 226 | |
| 227 | define_one_rw(sampling_rate); |
| 228 | define_one_rw(sampling_down_factor); |
| 229 | define_one_rw(up_threshold); |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 230 | define_one_rw(ignore_nice_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | static struct attribute * dbs_attributes[] = { |
| 233 | &sampling_rate_max.attr, |
| 234 | &sampling_rate_min.attr, |
| 235 | &sampling_rate.attr, |
| 236 | &sampling_down_factor.attr, |
| 237 | &up_threshold.attr, |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 238 | &ignore_nice_load.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | NULL |
| 240 | }; |
| 241 | |
| 242 | static struct attribute_group dbs_attr_group = { |
| 243 | .attrs = dbs_attributes, |
| 244 | .name = "ondemand", |
| 245 | }; |
| 246 | |
| 247 | /************************** sysfs end ************************/ |
| 248 | |
| 249 | static void dbs_check_cpu(int cpu) |
| 250 | { |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 251 | unsigned int idle_ticks, up_idle_ticks, total_ticks; |
| 252 | unsigned int freq_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | unsigned int freq_down_sampling_rate; |
| 254 | static int down_skip[NR_CPUS]; |
| 255 | struct cpu_dbs_info_s *this_dbs_info; |
| 256 | |
| 257 | struct cpufreq_policy *policy; |
| 258 | unsigned int j; |
| 259 | |
| 260 | this_dbs_info = &per_cpu(cpu_dbs_info, cpu); |
| 261 | if (!this_dbs_info->enable) |
| 262 | return; |
| 263 | |
| 264 | policy = this_dbs_info->cur_policy; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 265 | /* |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 266 | * Every sampling_rate, we check, if current idle time is less |
| 267 | * than 20% (default), then we try to increase frequency |
| 268 | * Every sampling_rate*sampling_down_factor, we look for a the lowest |
| 269 | * frequency which can sustain the load while keeping idle time over |
| 270 | * 30%. If such a frequency exist, we try to decrease to this frequency. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | * |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 272 | * Any frequency increase takes it to the maximum frequency. |
| 273 | * Frequency reduction happens at minimum steps of |
| 274 | * 5% (default) of current frequency |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | */ |
| 276 | |
| 277 | /* Check for frequency increase */ |
Dave Jones | 9c7d269 | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 278 | idle_ticks = UINT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | for_each_cpu_mask(j, policy->cpus) { |
Dave Jones | 9c7d269 | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 280 | unsigned int tmp_idle_ticks, total_idle_ticks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | struct cpu_dbs_info_s *j_dbs_info; |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 284 | total_idle_ticks = get_cpu_idle_time(j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | tmp_idle_ticks = total_idle_ticks - |
| 286 | j_dbs_info->prev_cpu_idle_up; |
| 287 | j_dbs_info->prev_cpu_idle_up = total_idle_ticks; |
| 288 | |
| 289 | if (tmp_idle_ticks < idle_ticks) |
| 290 | idle_ticks = tmp_idle_ticks; |
| 291 | } |
| 292 | |
| 293 | /* Scale idle ticks by 100 and compare with up and down ticks */ |
| 294 | idle_ticks *= 100; |
| 295 | up_idle_ticks = (100 - dbs_tuners_ins.up_threshold) * |
Dave Jones | 6fe7116 | 2005-05-31 19:03:44 -0700 | [diff] [blame] | 296 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | if (idle_ticks < up_idle_ticks) { |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 299 | down_skip[cpu] = 0; |
Dave Jones | 790d76f | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 300 | for_each_cpu_mask(j, policy->cpus) { |
| 301 | struct cpu_dbs_info_s *j_dbs_info; |
| 302 | |
| 303 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 304 | j_dbs_info->prev_cpu_idle_down = |
Dave Jones | 790d76f | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 305 | j_dbs_info->prev_cpu_idle_up; |
| 306 | } |
Dave Jones | c11420a | 2005-05-31 19:03:48 -0700 | [diff] [blame] | 307 | /* if we are already at full speed then break out early */ |
| 308 | if (policy->cur == policy->max) |
| 309 | return; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 310 | |
| 311 | __cpufreq_driver_target(policy, policy->max, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | CPUFREQ_RELATION_H); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
| 316 | /* Check for frequency decrease */ |
| 317 | down_skip[cpu]++; |
| 318 | if (down_skip[cpu] < dbs_tuners_ins.sampling_down_factor) |
| 319 | return; |
| 320 | |
Dave Jones | 9c7d269 | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 321 | idle_ticks = UINT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | for_each_cpu_mask(j, policy->cpus) { |
Dave Jones | 9c7d269 | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 323 | unsigned int tmp_idle_ticks, total_idle_ticks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | struct cpu_dbs_info_s *j_dbs_info; |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 327 | /* Check for frequency decrease */ |
| 328 | total_idle_ticks = j_dbs_info->prev_cpu_idle_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | tmp_idle_ticks = total_idle_ticks - |
| 330 | j_dbs_info->prev_cpu_idle_down; |
| 331 | j_dbs_info->prev_cpu_idle_down = total_idle_ticks; |
| 332 | |
| 333 | if (tmp_idle_ticks < idle_ticks) |
| 334 | idle_ticks = tmp_idle_ticks; |
| 335 | } |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | down_skip[cpu] = 0; |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 338 | /* if we cannot reduce the frequency anymore, break out early */ |
| 339 | if (policy->cur == policy->min) |
| 340 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 342 | /* Compute how many ticks there are between two measurements */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | freq_down_sampling_rate = dbs_tuners_ins.sampling_rate * |
| 344 | dbs_tuners_ins.sampling_down_factor; |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 345 | total_ticks = usecs_to_jiffies(freq_down_sampling_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 347 | /* |
| 348 | * The optimal frequency is the frequency that is the lowest that |
| 349 | * can support the current CPU usage without triggering the up |
| 350 | * policy. To be safe, we focus 10 points under the threshold. |
| 351 | */ |
| 352 | freq_next = ((total_ticks - idle_ticks) * 100) / total_ticks; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 353 | freq_next = (freq_next * policy->cur) / |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 354 | (dbs_tuners_ins.up_threshold - 10); |
Dave Jones | 1206aaa | 2005-05-31 19:03:48 -0700 | [diff] [blame] | 355 | |
Dominik Brodowski | 7c9d8c0 | 2006-03-26 11:11:03 +0200 | [diff] [blame] | 356 | if (freq_next < policy->min) |
| 357 | freq_next = policy->min; |
| 358 | |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 359 | if (freq_next <= ((policy->cur * 95) / 100)) |
| 360 | __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static void do_dbs_timer(void *data) |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 364 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | int i; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 366 | mutex_lock(&dbs_mutex); |
Dave Jones | 6fe7116 | 2005-05-31 19:03:44 -0700 | [diff] [blame] | 367 | for_each_online_cpu(i) |
| 368 | dbs_check_cpu(i); |
Andi Kleen | 6810b54 | 2006-05-08 15:17:31 +0200 | [diff] [blame] | 369 | queue_delayed_work(dbs_workq, &dbs_work, |
| 370 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 371 | mutex_unlock(&dbs_mutex); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | static inline void dbs_timer_init(void) |
| 375 | { |
| 376 | INIT_WORK(&dbs_work, do_dbs_timer, NULL); |
Andi Kleen | 6810b54 | 2006-05-08 15:17:31 +0200 | [diff] [blame] | 377 | if (!dbs_workq) |
| 378 | dbs_workq = create_singlethread_workqueue("ondemand"); |
| 379 | if (!dbs_workq) { |
| 380 | printk(KERN_ERR "ondemand: Cannot initialize kernel thread\n"); |
| 381 | return; |
| 382 | } |
| 383 | queue_delayed_work(dbs_workq, &dbs_work, |
| 384 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
| 388 | static inline void dbs_timer_exit(void) |
| 389 | { |
Andi Kleen | 6810b54 | 2006-05-08 15:17:31 +0200 | [diff] [blame] | 390 | if (dbs_workq) |
| 391 | cancel_rearming_delayed_workqueue(dbs_workq, &dbs_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 395 | unsigned int event) |
| 396 | { |
| 397 | unsigned int cpu = policy->cpu; |
| 398 | struct cpu_dbs_info_s *this_dbs_info; |
| 399 | unsigned int j; |
| 400 | |
| 401 | this_dbs_info = &per_cpu(cpu_dbs_info, cpu); |
| 402 | |
| 403 | switch (event) { |
| 404 | case CPUFREQ_GOV_START: |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 405 | if ((!cpu_online(cpu)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | (!policy->cur)) |
| 407 | return -EINVAL; |
| 408 | |
| 409 | if (policy->cpuinfo.transition_latency > |
Eric Piel | ff8c288 | 2006-03-10 11:34:16 +0200 | [diff] [blame] | 410 | (TRANSITION_LATENCY_LIMIT * 1000)) { |
| 411 | printk(KERN_WARNING "ondemand governor failed to load " |
| 412 | "due to too long transition latency\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | return -EINVAL; |
Eric Piel | ff8c288 | 2006-03-10 11:34:16 +0200 | [diff] [blame] | 414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if (this_dbs_info->enable) /* Already enabled */ |
| 416 | break; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 417 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 418 | mutex_lock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | for_each_cpu_mask(j, policy->cpus) { |
| 420 | struct cpu_dbs_info_s *j_dbs_info; |
| 421 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
| 422 | j_dbs_info->cur_policy = policy; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 423 | |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 424 | j_dbs_info->prev_cpu_idle_up = get_cpu_idle_time(j); |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 425 | j_dbs_info->prev_cpu_idle_down |
| 426 | = j_dbs_info->prev_cpu_idle_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | this_dbs_info->enable = 1; |
| 429 | sysfs_create_group(&policy->kobj, &dbs_attr_group); |
| 430 | dbs_enable++; |
| 431 | /* |
| 432 | * Start the timerschedule work, when this governor |
| 433 | * is used for first time |
| 434 | */ |
| 435 | if (dbs_enable == 1) { |
| 436 | unsigned int latency; |
| 437 | /* policy latency is in nS. Convert it to uS first */ |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 438 | latency = policy->cpuinfo.transition_latency / 1000; |
| 439 | if (latency == 0) |
| 440 | latency = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 442 | def_sampling_rate = latency * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | DEF_SAMPLING_RATE_LATENCY_MULTIPLIER; |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 444 | |
| 445 | if (def_sampling_rate < MIN_STAT_SAMPLING_RATE) |
| 446 | def_sampling_rate = MIN_STAT_SAMPLING_RATE; |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | dbs_tuners_ins.sampling_rate = def_sampling_rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | dbs_timer_init(); |
| 450 | } |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 451 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 452 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | break; |
| 454 | |
| 455 | case CPUFREQ_GOV_STOP: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 456 | mutex_lock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | this_dbs_info->enable = 0; |
| 458 | sysfs_remove_group(&policy->kobj, &dbs_attr_group); |
| 459 | dbs_enable--; |
| 460 | /* |
| 461 | * Stop the timerschedule work, when this governor |
| 462 | * is used for first time |
| 463 | */ |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 464 | if (dbs_enable == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | dbs_timer_exit(); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 466 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 467 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
| 469 | break; |
| 470 | |
| 471 | case CPUFREQ_GOV_LIMITS: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 472 | mutex_lock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | if (policy->max < this_dbs_info->cur_policy->cur) |
| 474 | __cpufreq_driver_target( |
| 475 | this_dbs_info->cur_policy, |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 476 | policy->max, CPUFREQ_RELATION_H); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | else if (policy->min > this_dbs_info->cur_policy->cur) |
| 478 | __cpufreq_driver_target( |
| 479 | this_dbs_info->cur_policy, |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 480 | policy->min, CPUFREQ_RELATION_L); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 481 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | return 0; |
| 485 | } |
| 486 | |
Dave Jones | 7f335d4 | 2005-05-31 19:03:46 -0700 | [diff] [blame] | 487 | static struct cpufreq_governor cpufreq_gov_dbs = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | .name = "ondemand", |
| 489 | .governor = cpufreq_governor_dbs, |
| 490 | .owner = THIS_MODULE, |
| 491 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
| 493 | static int __init cpufreq_gov_dbs_init(void) |
| 494 | { |
| 495 | return cpufreq_register_governor(&cpufreq_gov_dbs); |
| 496 | } |
| 497 | |
| 498 | static void __exit cpufreq_gov_dbs_exit(void) |
| 499 | { |
Andi Kleen | 6810b54 | 2006-05-08 15:17:31 +0200 | [diff] [blame] | 500 | /* Make sure that the scheduled work is indeed not running. |
| 501 | Assumes the timer has been cancelled first. */ |
| 502 | if (dbs_workq) { |
| 503 | flush_workqueue(dbs_workq); |
| 504 | destroy_workqueue(dbs_workq); |
| 505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | cpufreq_unregister_governor(&cpufreq_gov_dbs); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>"); |
| 512 | MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for " |
| 513 | "Low Latency Frequency Transition capable processors"); |
| 514 | MODULE_LICENSE ("GPL"); |
| 515 | |
| 516 | module_init(cpufreq_gov_dbs_init); |
| 517 | module_exit(cpufreq_gov_dbs_exit); |