| 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/init.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/cpufreq.h> | 
| Andrew Morton | 138a0128 | 2006-06-23 03:31:19 -0700 | [diff] [blame] | 17 | #include <linux/cpu.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/jiffies.h> | 
 | 19 | #include <linux/kernel_stat.h> | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 20 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 |  | 
 | 22 | /* | 
 | 23 |  * dbs is used in this file as a shortform for demandbased switching | 
 | 24 |  * It helps to keep variable names smaller, simpler | 
 | 25 |  */ | 
 | 26 |  | 
 | 27 | #define DEF_FREQUENCY_UP_THRESHOLD		(80) | 
| Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 28 | #define MIN_FREQUENCY_UP_THRESHOLD		(11) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #define MAX_FREQUENCY_UP_THRESHOLD		(100) | 
 | 30 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 31 | /* | 
 | 32 |  * The polling frequency of this governor depends on the capability of | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 |  * the processor. Default polling frequency is 1000 times the transition | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 34 |  * latency of the processor. The governor will work on any processor with | 
 | 35 |  * transition latency <= 10mS, using appropriate sampling | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  * rate. | 
 | 37 |  * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL) | 
 | 38 |  * this governor will not work. | 
 | 39 |  * All times here are in uS. | 
 | 40 |  */ | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 41 | static unsigned int def_sampling_rate; | 
| Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 42 | #define MIN_SAMPLING_RATE_RATIO			(2) | 
 | 43 | /* for correct statistics, we need at least 10 ticks between each measure */ | 
 | 44 | #define MIN_STAT_SAMPLING_RATE			(MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10)) | 
 | 45 | #define MIN_SAMPLING_RATE			(def_sampling_rate / MIN_SAMPLING_RATE_RATIO) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #define MAX_SAMPLING_RATE			(500 * def_sampling_rate) | 
 | 47 | #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER	(1000) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define TRANSITION_LATENCY_LIMIT		(10 * 1000) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 |  | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 50 | static void do_dbs_timer(struct work_struct *work); | 
 | 51 |  | 
 | 52 | /* Sampling types */ | 
 | 53 | enum dbs_sample {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 |  | 
 | 55 | struct cpu_dbs_info_s { | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 56 | 	cputime64_t prev_cpu_idle; | 
 | 57 | 	cputime64_t prev_cpu_wall; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 58 | 	struct cpufreq_policy *cur_policy; | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 59 |  	struct delayed_work work; | 
 | 60 | 	enum dbs_sample sample_type; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 61 | 	unsigned int enable; | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 62 | 	struct cpufreq_frequency_table *freq_table; | 
 | 63 | 	unsigned int freq_lo; | 
 | 64 | 	unsigned int freq_lo_jiffies; | 
 | 65 | 	unsigned int freq_hi_jiffies; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | }; | 
 | 67 | static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info); | 
 | 68 |  | 
 | 69 | static unsigned int dbs_enable;	/* number of CPUs using this policy */ | 
 | 70 |  | 
| Venkatesh Pallipadi | 4ec223d | 2006-06-21 15:18:34 -0700 | [diff] [blame] | 71 | /* | 
 | 72 |  * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug | 
 | 73 |  * lock and dbs_mutex. cpu_hotplug lock should always be held before | 
 | 74 |  * dbs_mutex. If any function that can potentially take cpu_hotplug lock | 
 | 75 |  * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then | 
 | 76 |  * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock | 
 | 77 |  * is recursive for the same process. -Venki | 
 | 78 |  */ | 
| Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 79 | static DEFINE_MUTEX(dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 |  | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 81 | static struct workqueue_struct	*kondemand_wq; | 
| Andi Kleen | 6810b54 | 2006-05-08 15:17:31 +0200 | [diff] [blame] | 82 |  | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 83 | static struct dbs_tuners { | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 84 | 	unsigned int sampling_rate; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 85 | 	unsigned int up_threshold; | 
 | 86 | 	unsigned int ignore_nice; | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 87 | 	unsigned int powersave_bias; | 
 | 88 | } dbs_tuners_ins = { | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 89 | 	.up_threshold = DEF_FREQUENCY_UP_THRESHOLD, | 
| Eric Piel | 9cbad61 | 2006-03-10 11:35:27 +0200 | [diff] [blame] | 90 | 	.ignore_nice = 0, | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 91 | 	.powersave_bias = 0, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | }; | 
 | 93 |  | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 94 | static inline cputime64_t get_cpu_idle_time(unsigned int cpu) | 
| Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 95 | { | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 96 | 	cputime64_t retval; | 
 | 97 |  | 
 | 98 | 	retval = cputime64_add(kstat_cpu(cpu).cpustat.idle, | 
 | 99 | 			kstat_cpu(cpu).cpustat.iowait); | 
 | 100 |  | 
 | 101 | 	if (dbs_tuners_ins.ignore_nice) | 
 | 102 | 		retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice); | 
 | 103 |  | 
 | 104 | 	return retval; | 
| Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 105 | } | 
 | 106 |  | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 107 | /* | 
 | 108 |  * Find right freq to be set now with powersave_bias on. | 
 | 109 |  * Returns the freq_hi to be used right now and will set freq_hi_jiffies, | 
 | 110 |  * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs. | 
 | 111 |  */ | 
| Adrian Bunk | b5ecf60 | 2006-08-13 23:00:08 +0200 | [diff] [blame] | 112 | static unsigned int powersave_bias_target(struct cpufreq_policy *policy, | 
 | 113 | 					  unsigned int freq_next, | 
 | 114 | 					  unsigned int relation) | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 115 | { | 
 | 116 | 	unsigned int freq_req, freq_reduc, freq_avg; | 
 | 117 | 	unsigned int freq_hi, freq_lo; | 
 | 118 | 	unsigned int index = 0; | 
 | 119 | 	unsigned int jiffies_total, jiffies_hi, jiffies_lo; | 
 | 120 | 	struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu); | 
 | 121 |  | 
 | 122 | 	if (!dbs_info->freq_table) { | 
 | 123 | 		dbs_info->freq_lo = 0; | 
 | 124 | 		dbs_info->freq_lo_jiffies = 0; | 
 | 125 | 		return freq_next; | 
 | 126 | 	} | 
 | 127 |  | 
 | 128 | 	cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next, | 
 | 129 | 			relation, &index); | 
 | 130 | 	freq_req = dbs_info->freq_table[index].frequency; | 
 | 131 | 	freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000; | 
 | 132 | 	freq_avg = freq_req - freq_reduc; | 
 | 133 |  | 
 | 134 | 	/* Find freq bounds for freq_avg in freq_table */ | 
 | 135 | 	index = 0; | 
 | 136 | 	cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, | 
 | 137 | 			CPUFREQ_RELATION_H, &index); | 
 | 138 | 	freq_lo = dbs_info->freq_table[index].frequency; | 
 | 139 | 	index = 0; | 
 | 140 | 	cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, | 
 | 141 | 			CPUFREQ_RELATION_L, &index); | 
 | 142 | 	freq_hi = dbs_info->freq_table[index].frequency; | 
 | 143 |  | 
 | 144 | 	/* Find out how long we have to be in hi and lo freqs */ | 
 | 145 | 	if (freq_hi == freq_lo) { | 
 | 146 | 		dbs_info->freq_lo = 0; | 
 | 147 | 		dbs_info->freq_lo_jiffies = 0; | 
 | 148 | 		return freq_lo; | 
 | 149 | 	} | 
 | 150 | 	jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate); | 
 | 151 | 	jiffies_hi = (freq_avg - freq_lo) * jiffies_total; | 
 | 152 | 	jiffies_hi += ((freq_hi - freq_lo) / 2); | 
 | 153 | 	jiffies_hi /= (freq_hi - freq_lo); | 
 | 154 | 	jiffies_lo = jiffies_total - jiffies_hi; | 
 | 155 | 	dbs_info->freq_lo = freq_lo; | 
 | 156 | 	dbs_info->freq_lo_jiffies = jiffies_lo; | 
 | 157 | 	dbs_info->freq_hi_jiffies = jiffies_hi; | 
 | 158 | 	return freq_hi; | 
 | 159 | } | 
 | 160 |  | 
 | 161 | static void ondemand_powersave_bias_init(void) | 
 | 162 | { | 
 | 163 | 	int i; | 
 | 164 | 	for_each_online_cpu(i) { | 
 | 165 | 		struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i); | 
 | 166 | 		dbs_info->freq_table = cpufreq_frequency_get_table(i); | 
 | 167 | 		dbs_info->freq_lo = 0; | 
 | 168 | 	} | 
 | 169 | } | 
 | 170 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /************************** sysfs interface ************************/ | 
 | 172 | static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf) | 
 | 173 | { | 
 | 174 | 	return sprintf (buf, "%u\n", MAX_SAMPLING_RATE); | 
 | 175 | } | 
 | 176 |  | 
 | 177 | static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf) | 
 | 178 | { | 
 | 179 | 	return sprintf (buf, "%u\n", MIN_SAMPLING_RATE); | 
 | 180 | } | 
 | 181 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 182 | #define define_one_ro(_name)		\ | 
 | 183 | static struct freq_attr _name =		\ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | __ATTR(_name, 0444, show_##_name, NULL) | 
 | 185 |  | 
 | 186 | define_one_ro(sampling_rate_max); | 
 | 187 | define_one_ro(sampling_rate_min); | 
 | 188 |  | 
 | 189 | /* cpufreq_ondemand Governor Tunables */ | 
 | 190 | #define show_one(file_name, object)					\ | 
 | 191 | static ssize_t show_##file_name						\ | 
 | 192 | (struct cpufreq_policy *unused, char *buf)				\ | 
 | 193 | {									\ | 
 | 194 | 	return sprintf(buf, "%u\n", dbs_tuners_ins.object);		\ | 
 | 195 | } | 
 | 196 | show_one(sampling_rate, sampling_rate); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | show_one(up_threshold, up_threshold); | 
| Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 198 | show_one(ignore_nice_load, ignore_nice); | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 199 | show_one(powersave_bias, powersave_bias); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 201 | static ssize_t store_sampling_rate(struct cpufreq_policy *unused, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | 		const char *buf, size_t count) | 
 | 203 | { | 
 | 204 | 	unsigned int input; | 
 | 205 | 	int ret; | 
| Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 206 | 	ret = sscanf(buf, "%u", &input); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 |  | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 208 | 	mutex_lock(&dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | 	if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) { | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 210 | 		mutex_unlock(&dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | 		return -EINVAL; | 
 | 212 | 	} | 
 | 213 |  | 
 | 214 | 	dbs_tuners_ins.sampling_rate = input; | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 215 | 	mutex_unlock(&dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 |  | 
 | 217 | 	return count; | 
 | 218 | } | 
 | 219 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 220 | static ssize_t store_up_threshold(struct cpufreq_policy *unused, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | 		const char *buf, size_t count) | 
 | 222 | { | 
 | 223 | 	unsigned int input; | 
 | 224 | 	int ret; | 
| Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 225 | 	ret = sscanf(buf, "%u", &input); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 |  | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 227 | 	mutex_lock(&dbs_mutex); | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 228 | 	if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || | 
| Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 229 | 			input < MIN_FREQUENCY_UP_THRESHOLD) { | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 230 | 		mutex_unlock(&dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | 		return -EINVAL; | 
 | 232 | 	} | 
 | 233 |  | 
 | 234 | 	dbs_tuners_ins.up_threshold = input; | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 235 | 	mutex_unlock(&dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 |  | 
 | 237 | 	return count; | 
 | 238 | } | 
 | 239 |  | 
| Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 240 | static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy, | 
| Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 241 | 		const char *buf, size_t count) | 
 | 242 | { | 
 | 243 | 	unsigned int input; | 
 | 244 | 	int ret; | 
 | 245 |  | 
 | 246 | 	unsigned int j; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 247 |  | 
| Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 248 | 	ret = sscanf(buf, "%u", &input); | 
| Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 249 | 	if ( ret != 1 ) | 
 | 250 | 		return -EINVAL; | 
 | 251 |  | 
 | 252 | 	if ( input > 1 ) | 
 | 253 | 		input = 1; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 254 |  | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 255 | 	mutex_lock(&dbs_mutex); | 
| Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 256 | 	if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */ | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 257 | 		mutex_unlock(&dbs_mutex); | 
| Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 258 | 		return count; | 
 | 259 | 	} | 
 | 260 | 	dbs_tuners_ins.ignore_nice = input; | 
 | 261 |  | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 262 | 	/* we need to re-evaluate prev_cpu_idle */ | 
| Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 263 | 	for_each_online_cpu(j) { | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 264 | 		struct cpu_dbs_info_s *dbs_info; | 
 | 265 | 		dbs_info = &per_cpu(cpu_dbs_info, j); | 
 | 266 | 		dbs_info->prev_cpu_idle = get_cpu_idle_time(j); | 
 | 267 | 		dbs_info->prev_cpu_wall = get_jiffies_64(); | 
| Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 268 | 	} | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 269 | 	mutex_unlock(&dbs_mutex); | 
| Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 270 |  | 
 | 271 | 	return count; | 
 | 272 | } | 
 | 273 |  | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 274 | static ssize_t store_powersave_bias(struct cpufreq_policy *unused, | 
 | 275 | 		const char *buf, size_t count) | 
 | 276 | { | 
 | 277 | 	unsigned int input; | 
 | 278 | 	int ret; | 
 | 279 | 	ret = sscanf(buf, "%u", &input); | 
 | 280 |  | 
 | 281 | 	if (ret != 1) | 
 | 282 | 		return -EINVAL; | 
 | 283 |  | 
 | 284 | 	if (input > 1000) | 
 | 285 | 		input = 1000; | 
 | 286 |  | 
 | 287 | 	mutex_lock(&dbs_mutex); | 
 | 288 | 	dbs_tuners_ins.powersave_bias = input; | 
 | 289 | 	ondemand_powersave_bias_init(); | 
 | 290 | 	mutex_unlock(&dbs_mutex); | 
 | 291 |  | 
 | 292 | 	return count; | 
 | 293 | } | 
 | 294 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | #define define_one_rw(_name) \ | 
 | 296 | static struct freq_attr _name = \ | 
 | 297 | __ATTR(_name, 0644, show_##_name, store_##_name) | 
 | 298 |  | 
 | 299 | define_one_rw(sampling_rate); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | define_one_rw(up_threshold); | 
| Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 301 | define_one_rw(ignore_nice_load); | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 302 | define_one_rw(powersave_bias); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 |  | 
 | 304 | static struct attribute * dbs_attributes[] = { | 
 | 305 | 	&sampling_rate_max.attr, | 
 | 306 | 	&sampling_rate_min.attr, | 
 | 307 | 	&sampling_rate.attr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | 	&up_threshold.attr, | 
| Alexander Clouter | 001893c | 2005-12-01 01:09:25 -0800 | [diff] [blame] | 309 | 	&ignore_nice_load.attr, | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 310 | 	&powersave_bias.attr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | 	NULL | 
 | 312 | }; | 
 | 313 |  | 
 | 314 | static struct attribute_group dbs_attr_group = { | 
 | 315 | 	.attrs = dbs_attributes, | 
 | 316 | 	.name = "ondemand", | 
 | 317 | }; | 
 | 318 |  | 
 | 319 | /************************** sysfs end ************************/ | 
 | 320 |  | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 321 | 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] | 322 | { | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 323 | 	unsigned int idle_ticks, total_ticks; | 
 | 324 | 	unsigned int load; | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 325 | 	cputime64_t cur_jiffies; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 |  | 
 | 327 | 	struct cpufreq_policy *policy; | 
 | 328 | 	unsigned int j; | 
 | 329 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 	if (!this_dbs_info->enable) | 
 | 331 | 		return; | 
 | 332 |  | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 333 | 	this_dbs_info->freq_lo = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | 	policy = this_dbs_info->cur_policy; | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 335 | 	cur_jiffies = jiffies64_to_cputime64(get_jiffies_64()); | 
 | 336 | 	total_ticks = (unsigned int) cputime64_sub(cur_jiffies, | 
 | 337 | 			this_dbs_info->prev_cpu_wall); | 
 | 338 | 	this_dbs_info->prev_cpu_wall = cur_jiffies; | 
| Linus Torvalds | 2cd7cbd | 2006-07-23 12:05:00 -0700 | [diff] [blame] | 339 | 	if (!total_ticks) | 
 | 340 | 		return; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 341 | 	/* | 
| Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 342 | 	 * Every sampling_rate, we check, if current idle time is less | 
 | 343 | 	 * than 20% (default), then we try to increase frequency | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 344 | 	 * Every sampling_rate, we look for a the lowest | 
| Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 345 | 	 * frequency which can sustain the load while keeping idle time over | 
 | 346 | 	 * 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] | 347 | 	 * | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 348 | 	 * Any frequency increase takes it to the maximum frequency. | 
 | 349 | 	 * Frequency reduction happens at minimum steps of | 
 | 350 | 	 * 5% (default) of current frequency | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | 	 */ | 
 | 352 |  | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 353 | 	/* Get Idle Time */ | 
| Dave Jones | 9c7d269 | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 354 | 	idle_ticks = UINT_MAX; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | 	for_each_cpu_mask(j, policy->cpus) { | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 356 | 		cputime64_t total_idle_ticks; | 
 | 357 | 		unsigned int tmp_idle_ticks; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | 		struct cpu_dbs_info_s *j_dbs_info; | 
 | 359 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | 		j_dbs_info = &per_cpu(cpu_dbs_info, j); | 
| Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 361 | 		total_idle_ticks = get_cpu_idle_time(j); | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 362 | 		tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks, | 
 | 363 | 				j_dbs_info->prev_cpu_idle); | 
 | 364 | 		j_dbs_info->prev_cpu_idle = total_idle_ticks; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 |  | 
 | 366 | 		if (tmp_idle_ticks < idle_ticks) | 
 | 367 | 			idle_ticks = tmp_idle_ticks; | 
 | 368 | 	} | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 369 | 	load = (100 * (total_ticks - idle_ticks)) / total_ticks; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 |  | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 371 | 	/* Check for frequency increase */ | 
 | 372 | 	if (load > dbs_tuners_ins.up_threshold) { | 
| Dave Jones | c11420a | 2005-05-31 19:03:48 -0700 | [diff] [blame] | 373 | 		/* if we are already at full speed then break out early */ | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 374 | 		if (!dbs_tuners_ins.powersave_bias) { | 
 | 375 | 			if (policy->cur == policy->max) | 
 | 376 | 				return; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 377 |  | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 378 | 			__cpufreq_driver_target(policy, policy->max, | 
 | 379 | 				CPUFREQ_RELATION_H); | 
 | 380 | 		} else { | 
 | 381 | 			int freq = powersave_bias_target(policy, policy->max, | 
 | 382 | 					CPUFREQ_RELATION_H); | 
 | 383 | 			__cpufreq_driver_target(policy, freq, | 
 | 384 | 				CPUFREQ_RELATION_L); | 
 | 385 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | 		return; | 
 | 387 | 	} | 
 | 388 |  | 
 | 389 | 	/* Check for frequency decrease */ | 
| Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 390 | 	/* if we cannot reduce the frequency anymore, break out early */ | 
 | 391 | 	if (policy->cur == policy->min) | 
 | 392 | 		return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 |  | 
| Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 394 | 	/* | 
 | 395 | 	 * The optimal frequency is the frequency that is the lowest that | 
 | 396 | 	 * can support the current CPU usage without triggering the up | 
 | 397 | 	 * policy. To be safe, we focus 10 points under the threshold. | 
 | 398 | 	 */ | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 399 | 	if (load < (dbs_tuners_ins.up_threshold - 10)) { | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 400 | 		unsigned int freq_next = (policy->cur * load) / | 
| Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 401 | 			(dbs_tuners_ins.up_threshold - 10); | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 402 | 		if (!dbs_tuners_ins.powersave_bias) { | 
 | 403 | 			__cpufreq_driver_target(policy, freq_next, | 
 | 404 | 					CPUFREQ_RELATION_L); | 
 | 405 | 		} else { | 
 | 406 | 			int freq = powersave_bias_target(policy, freq_next, | 
 | 407 | 					CPUFREQ_RELATION_L); | 
 | 408 | 			__cpufreq_driver_target(policy, freq, | 
 | 409 | 				CPUFREQ_RELATION_L); | 
 | 410 | 		} | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 411 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } | 
 | 413 |  | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 414 | static void do_dbs_timer(struct work_struct *work) | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 415 | { | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 416 | 	unsigned int cpu = smp_processor_id(); | 
 | 417 | 	struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu); | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 418 | 	enum dbs_sample sample_type = dbs_info->sample_type; | 
| Alexey Starikovskiy | 1ce28d6 | 2006-07-31 22:25:20 +0400 | [diff] [blame] | 419 | 	/* We want all CPUs to do sampling nearly on same jiffy */ | 
 | 420 | 	int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate); | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 421 |  | 
 | 422 | 	/* Permit rescheduling of this work item */ | 
 | 423 | 	work_release(work); | 
 | 424 |  | 
| Alexey Starikovskiy | 1ce28d6 | 2006-07-31 22:25:20 +0400 | [diff] [blame] | 425 | 	delay -= jiffies % delay; | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 426 |  | 
| Linus Torvalds | 2cd7cbd | 2006-07-23 12:05:00 -0700 | [diff] [blame] | 427 | 	if (!dbs_info->enable) | 
 | 428 | 		return; | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 429 | 	/* Common NORMAL_SAMPLE setup */ | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 430 | 	dbs_info->sample_type = DBS_NORMAL_SAMPLE; | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 431 | 	if (!dbs_tuners_ins.powersave_bias || | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 432 | 	    sample_type == DBS_NORMAL_SAMPLE) { | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 433 | 		lock_cpu_hotplug(); | 
 | 434 | 		dbs_check_cpu(dbs_info); | 
 | 435 | 		unlock_cpu_hotplug(); | 
 | 436 | 		if (dbs_info->freq_lo) { | 
 | 437 | 			/* Setup timer for SUB_SAMPLE */ | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 438 | 			dbs_info->sample_type = DBS_SUB_SAMPLE; | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 439 | 			delay = dbs_info->freq_hi_jiffies; | 
 | 440 | 		} | 
 | 441 | 	} else { | 
 | 442 | 		__cpufreq_driver_target(dbs_info->cur_policy, | 
 | 443 | 	                        	dbs_info->freq_lo, | 
 | 444 | 	                        	CPUFREQ_RELATION_H); | 
 | 445 | 	} | 
| Alexey Starikovskiy | 1ce28d6 | 2006-07-31 22:25:20 +0400 | [diff] [blame] | 446 | 	queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay); | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 447 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 |  | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 449 | static inline void dbs_timer_init(unsigned int cpu) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 451 | 	struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, cpu); | 
| Alexey Starikovskiy | 1ce28d6 | 2006-07-31 22:25:20 +0400 | [diff] [blame] | 452 | 	/* We want all CPUs to do sampling nearly on same jiffy */ | 
 | 453 | 	int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate); | 
 | 454 | 	delay -= jiffies % delay; | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 455 |  | 
| Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 456 | 	ondemand_powersave_bias_init(); | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 457 | 	INIT_DELAYED_WORK_NAR(&dbs_info->work, do_dbs_timer); | 
 | 458 | 	dbs_info->sample_type = DBS_NORMAL_SAMPLE; | 
| Alexey Starikovskiy | 1ce28d6 | 2006-07-31 22:25:20 +0400 | [diff] [blame] | 459 | 	queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } | 
 | 461 |  | 
| Linus Torvalds | 2cd7cbd | 2006-07-23 12:05:00 -0700 | [diff] [blame] | 462 | static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { | 
| Linus Torvalds | 2cd7cbd | 2006-07-23 12:05:00 -0700 | [diff] [blame] | 464 | 	dbs_info->enable = 0; | 
 | 465 | 	cancel_delayed_work(&dbs_info->work); | 
 | 466 | 	flush_workqueue(kondemand_wq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } | 
 | 468 |  | 
 | 469 | static int cpufreq_governor_dbs(struct cpufreq_policy *policy, | 
 | 470 | 				   unsigned int event) | 
 | 471 | { | 
 | 472 | 	unsigned int cpu = policy->cpu; | 
 | 473 | 	struct cpu_dbs_info_s *this_dbs_info; | 
 | 474 | 	unsigned int j; | 
 | 475 |  | 
 | 476 | 	this_dbs_info = &per_cpu(cpu_dbs_info, cpu); | 
 | 477 |  | 
 | 478 | 	switch (event) { | 
 | 479 | 	case CPUFREQ_GOV_START: | 
| Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 480 | 		if ((!cpu_online(cpu)) || (!policy->cur)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | 			return -EINVAL; | 
 | 482 |  | 
 | 483 | 		if (policy->cpuinfo.transition_latency > | 
| Eric Piel | ff8c288 | 2006-03-10 11:34:16 +0200 | [diff] [blame] | 484 | 				(TRANSITION_LATENCY_LIMIT * 1000)) { | 
 | 485 | 			printk(KERN_WARNING "ondemand governor failed to load " | 
 | 486 | 			       "due to too long transition latency\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | 			return -EINVAL; | 
| Eric Piel | ff8c288 | 2006-03-10 11:34:16 +0200 | [diff] [blame] | 488 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | 		if (this_dbs_info->enable) /* Already enabled */ | 
 | 490 | 			break; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 491 |  | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 492 | 		mutex_lock(&dbs_mutex); | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 493 | 		dbs_enable++; | 
 | 494 | 		if (dbs_enable == 1) { | 
 | 495 | 			kondemand_wq = create_workqueue("kondemand"); | 
 | 496 | 			if (!kondemand_wq) { | 
 | 497 | 				printk(KERN_ERR "Creation of kondemand failed\n"); | 
 | 498 | 				dbs_enable--; | 
 | 499 | 				mutex_unlock(&dbs_mutex); | 
 | 500 | 				return -ENOSPC; | 
 | 501 | 			} | 
 | 502 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | 		for_each_cpu_mask(j, policy->cpus) { | 
 | 504 | 			struct cpu_dbs_info_s *j_dbs_info; | 
 | 505 | 			j_dbs_info = &per_cpu(cpu_dbs_info, j); | 
 | 506 | 			j_dbs_info->cur_policy = policy; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 507 |  | 
| Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 508 | 			j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j); | 
 | 509 | 			j_dbs_info->prev_cpu_wall = get_jiffies_64(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | 		} | 
 | 511 | 		this_dbs_info->enable = 1; | 
 | 512 | 		sysfs_create_group(&policy->kobj, &dbs_attr_group); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | 		/* | 
 | 514 | 		 * Start the timerschedule work, when this governor | 
 | 515 | 		 * is used for first time | 
 | 516 | 		 */ | 
 | 517 | 		if (dbs_enable == 1) { | 
 | 518 | 			unsigned int latency; | 
 | 519 | 			/* policy latency is in nS. Convert it to uS first */ | 
| Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 520 | 			latency = policy->cpuinfo.transition_latency / 1000; | 
 | 521 | 			if (latency == 0) | 
 | 522 | 				latency = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 |  | 
| Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 524 | 			def_sampling_rate = latency * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | 					DEF_SAMPLING_RATE_LATENCY_MULTIPLIER; | 
| Dave Jones | df8b59b | 2005-09-20 12:39:35 -0700 | [diff] [blame] | 526 |  | 
 | 527 | 			if (def_sampling_rate < MIN_STAT_SAMPLING_RATE) | 
 | 528 | 				def_sampling_rate = MIN_STAT_SAMPLING_RATE; | 
 | 529 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | 			dbs_tuners_ins.sampling_rate = def_sampling_rate; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | 		} | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 532 | 		dbs_timer_init(policy->cpu); | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 533 |  | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 534 | 		mutex_unlock(&dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | 		break; | 
 | 536 |  | 
 | 537 | 	case CPUFREQ_GOV_STOP: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 538 | 		mutex_lock(&dbs_mutex); | 
| Linus Torvalds | 2cd7cbd | 2006-07-23 12:05:00 -0700 | [diff] [blame] | 539 | 		dbs_timer_exit(this_dbs_info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | 		sysfs_remove_group(&policy->kobj, &dbs_attr_group); | 
 | 541 | 		dbs_enable--; | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 542 | 		if (dbs_enable == 0) | 
| Venkatesh Pallipadi | 2f8a835 | 2006-06-28 13:51:19 -0700 | [diff] [blame] | 543 | 			destroy_workqueue(kondemand_wq); | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 544 |  | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 545 | 		mutex_unlock(&dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 |  | 
 | 547 | 		break; | 
 | 548 |  | 
 | 549 | 	case CPUFREQ_GOV_LIMITS: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 550 | 		mutex_lock(&dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | 		if (policy->max < this_dbs_info->cur_policy->cur) | 
| Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 552 | 			__cpufreq_driver_target(this_dbs_info->cur_policy, | 
 | 553 | 			                        policy->max, | 
 | 554 | 			                        CPUFREQ_RELATION_H); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | 		else if (policy->min > this_dbs_info->cur_policy->cur) | 
| Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 556 | 			__cpufreq_driver_target(this_dbs_info->cur_policy, | 
 | 557 | 			                        policy->min, | 
 | 558 | 			                        CPUFREQ_RELATION_L); | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 559 | 		mutex_unlock(&dbs_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | 		break; | 
 | 561 | 	} | 
 | 562 | 	return 0; | 
 | 563 | } | 
 | 564 |  | 
| Dave Jones | 7f335d4 | 2005-05-31 19:03:46 -0700 | [diff] [blame] | 565 | static struct cpufreq_governor cpufreq_gov_dbs = { | 
| Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 566 | 	.name = "ondemand", | 
 | 567 | 	.governor = cpufreq_governor_dbs, | 
 | 568 | 	.owner = THIS_MODULE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 |  | 
 | 571 | static int __init cpufreq_gov_dbs_init(void) | 
 | 572 | { | 
 | 573 | 	return cpufreq_register_governor(&cpufreq_gov_dbs); | 
 | 574 | } | 
 | 575 |  | 
 | 576 | static void __exit cpufreq_gov_dbs_exit(void) | 
 | 577 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | 	cpufreq_unregister_governor(&cpufreq_gov_dbs); | 
 | 579 | } | 
 | 580 |  | 
 | 581 |  | 
| Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 582 | MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>"); | 
 | 583 | MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>"); | 
 | 584 | MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for " | 
 | 585 |                    "Low Latency Frequency Transition capable processors"); | 
 | 586 | MODULE_LICENSE("GPL"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 |  | 
 | 588 | module_init(cpufreq_gov_dbs_init); | 
 | 589 | module_exit(cpufreq_gov_dbs_exit); |