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