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> |
Andrew Morton | 138a0128 | 2006-06-23 03:31:19 -0700 | [diff] [blame] | 24 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 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 | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 39 | #define MIN_FREQUENCY_UP_THRESHOLD (11) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define MAX_FREQUENCY_UP_THRESHOLD (100) |
| 41 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 42 | /* |
| 43 | * The polling frequency of this governor depends on the capability of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * the processor. Default polling frequency is 1000 times the transition |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 45 | * latency of the processor. The governor will work on any processor with |
| 46 | * transition latency <= 10mS, using appropriate sampling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * rate. |
| 48 | * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL) |
| 49 | * this governor will not work. |
| 50 | * All times here are in uS. |
| 51 | */ |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 52 | static unsigned int def_sampling_rate; |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 53 | #define MIN_SAMPLING_RATE_RATIO (2) |
| 54 | /* for correct statistics, we need at least 10 ticks between each measure */ |
| 55 | #define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10)) |
| 56 | #define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #define MAX_SAMPLING_RATE (500 * def_sampling_rate) |
| 58 | #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #define TRANSITION_LATENCY_LIMIT (10 * 1000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | static void do_dbs_timer(void *data); |
| 62 | |
| 63 | struct cpu_dbs_info_s { |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 64 | cputime64_t prev_cpu_idle; |
| 65 | cputime64_t prev_cpu_wall; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 66 | struct cpufreq_policy *cur_policy; |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 67 | struct work_struct work; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 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 | |
Venkatesh Pallipadi | 4ec223d | 2006-06-21 15:18:34 -0700 | [diff] [blame] | 74 | /* |
| 75 | * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug |
| 76 | * lock and dbs_mutex. cpu_hotplug lock should always be held before |
| 77 | * dbs_mutex. If any function that can potentially take cpu_hotplug lock |
| 78 | * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then |
| 79 | * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock |
| 80 | * is recursive for the same process. -Venki |
| 81 | */ |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 82 | static DEFINE_MUTEX (dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | static DECLARE_WORK (dbs_work, do_dbs_timer, NULL); |
| 84 | |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 85 | static struct workqueue_struct *kondemand_wq; |
Andi Kleen | 6810b54 | 2006-05-08 15:17:31 +0200 | [diff] [blame] | 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | struct dbs_tuners { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 88 | unsigned int sampling_rate; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 89 | unsigned int up_threshold; |
| 90 | unsigned int ignore_nice; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static struct dbs_tuners dbs_tuners_ins = { |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 94 | .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, |
Eric Piel | 9cbad61 | 2006-03-10 11:35:27 +0200 | [diff] [blame] | 95 | .ignore_nice = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 98 | static inline cputime64_t get_cpu_idle_time(unsigned int cpu) |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 99 | { |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 100 | cputime64_t retval; |
| 101 | |
| 102 | retval = cputime64_add(kstat_cpu(cpu).cpustat.idle, |
| 103 | kstat_cpu(cpu).cpustat.iowait); |
| 104 | |
| 105 | if (dbs_tuners_ins.ignore_nice) |
| 106 | retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice); |
| 107 | |
| 108 | return retval; |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /************************** sysfs interface ************************/ |
| 112 | static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf) |
| 113 | { |
| 114 | return sprintf (buf, "%u\n", MAX_SAMPLING_RATE); |
| 115 | } |
| 116 | |
| 117 | static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf) |
| 118 | { |
| 119 | return sprintf (buf, "%u\n", MIN_SAMPLING_RATE); |
| 120 | } |
| 121 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 122 | #define define_one_ro(_name) \ |
| 123 | static struct freq_attr _name = \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | __ATTR(_name, 0444, show_##_name, NULL) |
| 125 | |
| 126 | define_one_ro(sampling_rate_max); |
| 127 | define_one_ro(sampling_rate_min); |
| 128 | |
| 129 | /* cpufreq_ondemand Governor Tunables */ |
| 130 | #define show_one(file_name, object) \ |
| 131 | static ssize_t show_##file_name \ |
| 132 | (struct cpufreq_policy *unused, char *buf) \ |
| 133 | { \ |
| 134 | return sprintf(buf, "%u\n", dbs_tuners_ins.object); \ |
| 135 | } |
| 136 | show_one(sampling_rate, sampling_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | show_one(up_threshold, up_threshold); |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 138 | show_one(ignore_nice_load, ignore_nice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 140 | static ssize_t store_sampling_rate(struct cpufreq_policy *unused, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | const char *buf, size_t count) |
| 142 | { |
| 143 | unsigned int input; |
| 144 | int ret; |
| 145 | ret = sscanf (buf, "%u", &input); |
| 146 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 147 | mutex_lock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) { |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 149 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return -EINVAL; |
| 151 | } |
| 152 | |
| 153 | dbs_tuners_ins.sampling_rate = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 154 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | return count; |
| 157 | } |
| 158 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 159 | static ssize_t store_up_threshold(struct cpufreq_policy *unused, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | const char *buf, size_t count) |
| 161 | { |
| 162 | unsigned int input; |
| 163 | int ret; |
| 164 | ret = sscanf (buf, "%u", &input); |
| 165 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 166 | mutex_lock(&dbs_mutex); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 167 | if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 168 | input < MIN_FREQUENCY_UP_THRESHOLD) { |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 169 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return -EINVAL; |
| 171 | } |
| 172 | |
| 173 | dbs_tuners_ins.up_threshold = input; |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 174 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | return count; |
| 177 | } |
| 178 | |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 179 | static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy, |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 180 | const char *buf, size_t count) |
| 181 | { |
| 182 | unsigned int input; |
| 183 | int ret; |
| 184 | |
| 185 | unsigned int j; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 186 | |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 187 | ret = sscanf (buf, "%u", &input); |
| 188 | if ( ret != 1 ) |
| 189 | return -EINVAL; |
| 190 | |
| 191 | if ( input > 1 ) |
| 192 | input = 1; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 193 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 194 | mutex_lock(&dbs_mutex); |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 195 | if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */ |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 196 | mutex_unlock(&dbs_mutex); |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 197 | return count; |
| 198 | } |
| 199 | dbs_tuners_ins.ignore_nice = input; |
| 200 | |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 201 | /* we need to re-evaluate prev_cpu_idle */ |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 202 | for_each_online_cpu(j) { |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 203 | struct cpu_dbs_info_s *dbs_info; |
| 204 | dbs_info = &per_cpu(cpu_dbs_info, j); |
| 205 | dbs_info->prev_cpu_idle = get_cpu_idle_time(j); |
| 206 | dbs_info->prev_cpu_wall = get_jiffies_64(); |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 207 | } |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 208 | mutex_unlock(&dbs_mutex); |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 209 | |
| 210 | return count; |
| 211 | } |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | #define define_one_rw(_name) \ |
| 214 | static struct freq_attr _name = \ |
| 215 | __ATTR(_name, 0644, show_##_name, store_##_name) |
| 216 | |
| 217 | define_one_rw(sampling_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | define_one_rw(up_threshold); |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 219 | define_one_rw(ignore_nice_load); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | static struct attribute * dbs_attributes[] = { |
| 222 | &sampling_rate_max.attr, |
| 223 | &sampling_rate_min.attr, |
| 224 | &sampling_rate.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | &up_threshold.attr, |
Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 226 | &ignore_nice_load.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | NULL |
| 228 | }; |
| 229 | |
| 230 | static struct attribute_group dbs_attr_group = { |
| 231 | .attrs = dbs_attributes, |
| 232 | .name = "ondemand", |
| 233 | }; |
| 234 | |
| 235 | /************************** sysfs end ************************/ |
| 236 | |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 237 | static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 239 | unsigned int idle_ticks, total_ticks; |
| 240 | unsigned int load; |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 241 | cputime64_t cur_jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | struct cpufreq_policy *policy; |
| 244 | unsigned int j; |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (!this_dbs_info->enable) |
| 247 | return; |
| 248 | |
| 249 | policy = this_dbs_info->cur_policy; |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 250 | cur_jiffies = jiffies64_to_cputime64(get_jiffies_64()); |
| 251 | total_ticks = (unsigned int) cputime64_sub(cur_jiffies, |
| 252 | this_dbs_info->prev_cpu_wall); |
| 253 | this_dbs_info->prev_cpu_wall = cur_jiffies; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 254 | /* |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 255 | * Every sampling_rate, we check, if current idle time is less |
| 256 | * than 20% (default), then we try to increase frequency |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 257 | * Every sampling_rate, we look for a the lowest |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 258 | * frequency which can sustain the load while keeping idle time over |
| 259 | * 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] | 260 | * |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 261 | * Any frequency increase takes it to the maximum frequency. |
| 262 | * Frequency reduction happens at minimum steps of |
| 263 | * 5% (default) of current frequency |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | */ |
| 265 | |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 266 | /* Get Idle Time */ |
Dave Jones | 9c7d269 | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 267 | idle_ticks = UINT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | for_each_cpu_mask(j, policy->cpus) { |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 269 | cputime64_t total_idle_ticks; |
| 270 | unsigned int tmp_idle_ticks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | struct cpu_dbs_info_s *j_dbs_info; |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 274 | total_idle_ticks = get_cpu_idle_time(j); |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 275 | tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks, |
| 276 | j_dbs_info->prev_cpu_idle); |
| 277 | j_dbs_info->prev_cpu_idle = total_idle_ticks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | if (tmp_idle_ticks < idle_ticks) |
| 280 | idle_ticks = tmp_idle_ticks; |
| 281 | } |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 282 | load = (100 * (total_ticks - idle_ticks)) / total_ticks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 284 | /* Check for frequency increase */ |
| 285 | if (load > dbs_tuners_ins.up_threshold) { |
Dave Jones | c11420a | 2005-05-31 19:03:48 -0700 | [diff] [blame] | 286 | /* if we are already at full speed then break out early */ |
| 287 | if (policy->cur == policy->max) |
| 288 | return; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 289 | |
| 290 | __cpufreq_driver_target(policy, policy->max, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | CPUFREQ_RELATION_H); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
| 295 | /* Check for frequency decrease */ |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 296 | /* if we cannot reduce the frequency anymore, break out early */ |
| 297 | if (policy->cur == policy->min) |
| 298 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 300 | /* |
| 301 | * The optimal frequency is the frequency that is the lowest that |
| 302 | * can support the current CPU usage without triggering the up |
| 303 | * policy. To be safe, we focus 10 points under the threshold. |
| 304 | */ |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 305 | if (load < (dbs_tuners_ins.up_threshold - 10)) { |
| 306 | unsigned int freq_next; |
| 307 | freq_next = (policy->cur * load) / |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 308 | (dbs_tuners_ins.up_threshold - 10); |
Dave Jones | 1206aaa | 2005-05-31 19:03:48 -0700 | [diff] [blame] | 309 | |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 310 | __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L); |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static void do_dbs_timer(void *data) |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 315 | { |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 316 | unsigned int cpu = smp_processor_id(); |
| 317 | struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu); |
| 318 | |
| 319 | dbs_check_cpu(dbs_info); |
| 320 | queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, |
| 321 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 324 | static inline void dbs_timer_init(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 326 | struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu); |
| 327 | |
| 328 | INIT_WORK(&dbs_info->work, do_dbs_timer, 0); |
| 329 | queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, |
| 330 | usecs_to_jiffies(dbs_tuners_ins.sampling_rate)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 334 | static inline void dbs_timer_exit(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 336 | struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu); |
| 337 | |
| 338 | cancel_rearming_delayed_workqueue(kondemand_wq, &dbs_info->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 342 | unsigned int event) |
| 343 | { |
| 344 | unsigned int cpu = policy->cpu; |
| 345 | struct cpu_dbs_info_s *this_dbs_info; |
| 346 | unsigned int j; |
| 347 | |
| 348 | this_dbs_info = &per_cpu(cpu_dbs_info, cpu); |
| 349 | |
| 350 | switch (event) { |
| 351 | case CPUFREQ_GOV_START: |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 352 | if ((!cpu_online(cpu)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | (!policy->cur)) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | if (policy->cpuinfo.transition_latency > |
Eric Piel | ff8c288 | 2006-03-10 11:34:16 +0200 | [diff] [blame] | 357 | (TRANSITION_LATENCY_LIMIT * 1000)) { |
| 358 | printk(KERN_WARNING "ondemand governor failed to load " |
| 359 | "due to too long transition latency\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return -EINVAL; |
Eric Piel | ff8c288 | 2006-03-10 11:34:16 +0200 | [diff] [blame] | 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | if (this_dbs_info->enable) /* Already enabled */ |
| 363 | break; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 364 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 365 | mutex_lock(&dbs_mutex); |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 366 | dbs_enable++; |
| 367 | if (dbs_enable == 1) { |
| 368 | kondemand_wq = create_workqueue("kondemand"); |
| 369 | if (!kondemand_wq) { |
| 370 | printk(KERN_ERR "Creation of kondemand failed\n"); |
| 371 | dbs_enable--; |
| 372 | mutex_unlock(&dbs_mutex); |
| 373 | return -ENOSPC; |
| 374 | } |
| 375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | for_each_cpu_mask(j, policy->cpus) { |
| 377 | struct cpu_dbs_info_s *j_dbs_info; |
| 378 | j_dbs_info = &per_cpu(cpu_dbs_info, j); |
| 379 | j_dbs_info->cur_policy = policy; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 380 | |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 381 | j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j); |
| 382 | j_dbs_info->prev_cpu_wall = get_jiffies_64(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | this_dbs_info->enable = 1; |
| 385 | sysfs_create_group(&policy->kobj, &dbs_attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | /* |
| 387 | * Start the timerschedule work, when this governor |
| 388 | * is used for first time |
| 389 | */ |
| 390 | if (dbs_enable == 1) { |
| 391 | unsigned int latency; |
| 392 | /* policy latency is in nS. Convert it to uS first */ |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 393 | latency = policy->cpuinfo.transition_latency / 1000; |
| 394 | if (latency == 0) |
| 395 | latency = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 397 | def_sampling_rate = latency * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | DEF_SAMPLING_RATE_LATENCY_MULTIPLIER; |
Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 399 | |
| 400 | if (def_sampling_rate < MIN_STAT_SAMPLING_RATE) |
| 401 | def_sampling_rate = MIN_STAT_SAMPLING_RATE; |
| 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | dbs_tuners_ins.sampling_rate = def_sampling_rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 405 | dbs_timer_init(policy->cpu); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 406 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 407 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | break; |
| 409 | |
| 410 | case CPUFREQ_GOV_STOP: |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 411 | mutex_lock(&dbs_mutex); |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 412 | dbs_timer_exit(policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | this_dbs_info->enable = 0; |
| 414 | sysfs_remove_group(&policy->kobj, &dbs_attr_group); |
| 415 | dbs_enable--; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 416 | if (dbs_enable == 0) |
Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame^] | 417 | destroy_workqueue(kondemand_wq); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 418 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 419 | mutex_unlock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | break; |
| 422 | |
| 423 | case CPUFREQ_GOV_LIMITS: |
Venkatesh Pallipadi | 4ec223d | 2006-06-21 15:18:34 -0700 | [diff] [blame] | 424 | lock_cpu_hotplug(); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 425 | mutex_lock(&dbs_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | if (policy->max < this_dbs_info->cur_policy->cur) |
| 427 | __cpufreq_driver_target( |
| 428 | this_dbs_info->cur_policy, |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 429 | policy->max, CPUFREQ_RELATION_H); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | else if (policy->min > this_dbs_info->cur_policy->cur) |
| 431 | __cpufreq_driver_target( |
| 432 | this_dbs_info->cur_policy, |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 433 | policy->min, CPUFREQ_RELATION_L); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 434 | mutex_unlock(&dbs_mutex); |
Venkatesh Pallipadi | 4ec223d | 2006-06-21 15:18:34 -0700 | [diff] [blame] | 435 | unlock_cpu_hotplug(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | return 0; |
| 439 | } |
| 440 | |
Dave Jones | 7f335d4 | 2005-05-31 19:03:46 -0700 | [diff] [blame] | 441 | static struct cpufreq_governor cpufreq_gov_dbs = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | .name = "ondemand", |
| 443 | .governor = cpufreq_governor_dbs, |
| 444 | .owner = THIS_MODULE, |
| 445 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | static int __init cpufreq_gov_dbs_init(void) |
| 448 | { |
| 449 | return cpufreq_register_governor(&cpufreq_gov_dbs); |
| 450 | } |
| 451 | |
| 452 | static void __exit cpufreq_gov_dbs_exit(void) |
| 453 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | cpufreq_unregister_governor(&cpufreq_gov_dbs); |
| 455 | } |
| 456 | |
| 457 | |
| 458 | MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>"); |
| 459 | MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for " |
| 460 | "Low Latency Frequency Transition capable processors"); |
| 461 | MODULE_LICENSE ("GPL"); |
| 462 | |
| 463 | module_init(cpufreq_gov_dbs_init); |
| 464 | module_exit(cpufreq_gov_dbs_exit); |