| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 1 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* | 
 | 3 |  *  linux/drivers/cpufreq/cpufreq_userspace.c | 
 | 4 |  * | 
 | 5 |  *  Copyright (C)  2001 Russell King | 
 | 6 |  *            (C)  2002 - 2004 Dominik Brodowski <linux@brodo.de> | 
 | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> | 
 | 15 | #include <linux/module.h> | 
 | 16 | #include <linux/smp.h> | 
 | 17 | #include <linux/init.h> | 
 | 18 | #include <linux/spinlock.h> | 
 | 19 | #include <linux/interrupt.h> | 
 | 20 | #include <linux/cpufreq.h> | 
| Arjan van de Ven | 153d7f3 | 2006-07-26 15:40:07 +0200 | [diff] [blame] | 21 | #include <linux/cpu.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/types.h> | 
 | 23 | #include <linux/fs.h> | 
 | 24 | #include <linux/sysfs.h> | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 25 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 |  | 
 | 27 | #include <asm/uaccess.h> | 
 | 28 |  | 
 | 29 |  | 
 | 30 | /** | 
 | 31 |  * A few values needed by the userspace governor | 
 | 32 |  */ | 
 | 33 | static unsigned int	cpu_max_freq[NR_CPUS]; | 
 | 34 | static unsigned int	cpu_min_freq[NR_CPUS]; | 
 | 35 | static unsigned int	cpu_cur_freq[NR_CPUS]; /* current CPU freq */ | 
 | 36 | static unsigned int	cpu_set_freq[NR_CPUS]; /* CPU freq desired by userspace */ | 
 | 37 | static unsigned int	cpu_is_managed[NR_CPUS]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 |  | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 39 | static DEFINE_MUTEX	(userspace_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
 | 41 | #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg) | 
 | 42 |  | 
 | 43 | /* keep track of frequency transitions */ | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 44 | static int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val, | 
 | 46 |                        void *data) | 
 | 47 | { | 
 | 48 |         struct cpufreq_freqs *freq = data; | 
 | 49 |  | 
 | 50 | 	dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n", freq->cpu, freq->new); | 
 | 51 | 	cpu_cur_freq[freq->cpu] = freq->new; | 
 | 52 |  | 
 | 53 |         return 0; | 
 | 54 | } | 
 | 55 |  | 
 | 56 | static struct notifier_block userspace_cpufreq_notifier_block = { | 
 | 57 |         .notifier_call  = userspace_cpufreq_notifier | 
 | 58 | }; | 
 | 59 |  | 
 | 60 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 61 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 |  * cpufreq_set - set the CPU frequency | 
 | 63 |  * @freq: target frequency in kHz | 
 | 64 |  * @cpu: CPU for which the frequency is to be set | 
 | 65 |  * | 
 | 66 |  * Sets the CPU frequency to freq. | 
 | 67 |  */ | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 68 | static int cpufreq_set(unsigned int freq, struct cpufreq_policy *policy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { | 
 | 70 | 	int ret = -EINVAL; | 
 | 71 |  | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 72 | 	dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 |  | 
| Arjan van de Ven | 153d7f3 | 2006-07-26 15:40:07 +0200 | [diff] [blame] | 74 | 	lock_cpu_hotplug(); | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 75 | 	mutex_lock(&userspace_mutex); | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 76 | 	if (!cpu_is_managed[policy->cpu]) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | 		goto err; | 
 | 78 |  | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 79 | 	cpu_set_freq[policy->cpu] = freq; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 |  | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 81 | 	if (freq < cpu_min_freq[policy->cpu]) | 
 | 82 | 		freq = cpu_min_freq[policy->cpu]; | 
 | 83 | 	if (freq > cpu_max_freq[policy->cpu]) | 
 | 84 | 		freq = cpu_max_freq[policy->cpu]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 |  | 
 | 86 | 	/* | 
 | 87 | 	 * We're safe from concurrent calls to ->target() here | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 88 | 	 * as we hold the userspace_mutex lock. If we were calling | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | 	 * cpufreq_driver_target, a deadlock situation might occur: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 90 | 	 * A: cpufreq_set (lock userspace_mutex) -> cpufreq_driver_target(lock policy->lock) | 
 | 91 | 	 * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_mutex) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | 	 */ | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 93 | 	ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 |  | 
 | 95 |  err: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 96 | 	mutex_unlock(&userspace_mutex); | 
| Arjan van de Ven | 153d7f3 | 2006-07-26 15:40:07 +0200 | [diff] [blame] | 97 | 	unlock_cpu_hotplug(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | 	return ret; | 
 | 99 | } | 
 | 100 |  | 
 | 101 |  | 
 | 102 | /************************** sysfs interface ************************/ | 
 | 103 | static ssize_t show_speed (struct cpufreq_policy *policy, char *buf) | 
 | 104 | { | 
 | 105 | 	return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]); | 
 | 106 | } | 
 | 107 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 108 | static ssize_t | 
 | 109 | store_speed (struct cpufreq_policy *policy, const char *buf, size_t count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { | 
 | 111 | 	unsigned int freq = 0; | 
 | 112 | 	unsigned int ret; | 
 | 113 |  | 
 | 114 | 	ret = sscanf (buf, "%u", &freq); | 
 | 115 | 	if (ret != 1) | 
 | 116 | 		return -EINVAL; | 
 | 117 |  | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 118 | 	cpufreq_set(freq, policy); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 |  | 
 | 120 | 	return count; | 
 | 121 | } | 
 | 122 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 123 | static struct freq_attr freq_attr_scaling_setspeed = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { | 
 | 125 | 	.attr = { .name = "scaling_setspeed", .mode = 0644, .owner = THIS_MODULE }, | 
 | 126 | 	.show = show_speed, | 
 | 127 | 	.store = store_speed, | 
 | 128 | }; | 
 | 129 |  | 
 | 130 | static int cpufreq_governor_userspace(struct cpufreq_policy *policy, | 
 | 131 | 				   unsigned int event) | 
 | 132 | { | 
 | 133 | 	unsigned int cpu = policy->cpu; | 
| Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 134 | 	int rc = 0; | 
 | 135 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | 	switch (event) { | 
 | 137 | 	case CPUFREQ_GOV_START: | 
 | 138 | 		if (!cpu_online(cpu)) | 
 | 139 | 			return -EINVAL; | 
 | 140 | 		BUG_ON(!policy->cur); | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 141 | 		mutex_lock(&userspace_mutex); | 
| Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 142 | 		rc = sysfs_create_file (&policy->kobj, | 
 | 143 | 					&freq_attr_scaling_setspeed.attr); | 
 | 144 | 		if (rc) | 
 | 145 | 			goto start_out; | 
 | 146 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 147 | 		cpu_is_managed[cpu] = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 		cpu_min_freq[cpu] = policy->min; | 
 | 149 | 		cpu_max_freq[cpu] = policy->max; | 
 | 150 | 		cpu_cur_freq[cpu] = policy->cur; | 
 | 151 | 		cpu_set_freq[cpu] = policy->cur; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | 		dprintk("managing cpu %u started (%u - %u kHz, currently %u kHz)\n", cpu, cpu_min_freq[cpu], cpu_max_freq[cpu], cpu_cur_freq[cpu]); | 
| Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 153 | start_out: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 154 | 		mutex_unlock(&userspace_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | 		break; | 
 | 156 | 	case CPUFREQ_GOV_STOP: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 157 | 		mutex_lock(&userspace_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | 		cpu_is_managed[cpu] = 0; | 
 | 159 | 		cpu_min_freq[cpu] = 0; | 
 | 160 | 		cpu_max_freq[cpu] = 0; | 
 | 161 | 		cpu_set_freq[cpu] = 0; | 
 | 162 | 		sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr); | 
 | 163 | 		dprintk("managing cpu %u stopped\n", cpu); | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 164 | 		mutex_unlock(&userspace_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | 		break; | 
 | 166 | 	case CPUFREQ_GOV_LIMITS: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 167 | 		mutex_lock(&userspace_mutex); | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 168 | 		dprintk("limit event for cpu %u: %u - %u kHz," | 
 | 169 | 			"currently %u kHz, last set to %u kHz\n", | 
 | 170 | 			cpu, policy->min, policy->max, | 
 | 171 | 			cpu_cur_freq[cpu], cpu_set_freq[cpu]); | 
 | 172 | 		if (policy->max < cpu_set_freq[cpu]) { | 
 | 173 | 			__cpufreq_driver_target(policy, policy->max, | 
 | 174 | 						CPUFREQ_RELATION_H); | 
 | 175 | 		} | 
 | 176 | 		else if (policy->min > cpu_set_freq[cpu]) { | 
 | 177 | 			__cpufreq_driver_target(policy, policy->min, | 
 | 178 | 						CPUFREQ_RELATION_L); | 
 | 179 | 		} | 
 | 180 | 		else { | 
 | 181 | 			__cpufreq_driver_target(policy, cpu_set_freq[cpu], | 
 | 182 | 						CPUFREQ_RELATION_L); | 
 | 183 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | 		cpu_min_freq[cpu] = policy->min; | 
 | 185 | 		cpu_max_freq[cpu] = policy->max; | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 186 | 		cpu_cur_freq[cpu] = policy->cur; | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 187 | 		mutex_unlock(&userspace_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | 		break; | 
 | 189 | 	} | 
| Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 190 | 	return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } | 
 | 192 |  | 
 | 193 |  | 
 | 194 | struct cpufreq_governor cpufreq_gov_userspace = { | 
 | 195 | 	.name		= "userspace", | 
 | 196 | 	.governor	= cpufreq_governor_userspace, | 
 | 197 | 	.owner		= THIS_MODULE, | 
 | 198 | }; | 
 | 199 | EXPORT_SYMBOL(cpufreq_gov_userspace); | 
 | 200 |  | 
 | 201 | static int __init cpufreq_gov_userspace_init(void) | 
 | 202 | { | 
 | 203 | 	cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); | 
 | 204 | 	return cpufreq_register_governor(&cpufreq_gov_userspace); | 
 | 205 | } | 
 | 206 |  | 
 | 207 |  | 
 | 208 | static void __exit cpufreq_gov_userspace_exit(void) | 
 | 209 | { | 
 | 210 | 	cpufreq_unregister_governor(&cpufreq_gov_userspace); | 
 | 211 |         cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); | 
 | 212 | } | 
 | 213 |  | 
 | 214 |  | 
 | 215 | MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>"); | 
 | 216 | MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'"); | 
 | 217 | MODULE_LICENSE ("GPL"); | 
 | 218 |  | 
 | 219 | fs_initcall(cpufreq_gov_userspace_init); | 
 | 220 | module_exit(cpufreq_gov_userspace_exit); |