| 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); | 
| Venki Pallipadi | c7f652e | 2007-06-20 14:24:00 -0700 | [diff] [blame] | 40 | static int cpus_using_userspace_governor; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 |  | 
 | 42 | #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg) | 
 | 43 |  | 
 | 44 | /* keep track of frequency transitions */ | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 45 | static int | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val, | 
 | 47 |                        void *data) | 
 | 48 | { | 
 | 49 |         struct cpufreq_freqs *freq = data; | 
 | 50 |  | 
| Venki Pallipadi | c7f652e | 2007-06-20 14:24:00 -0700 | [diff] [blame] | 51 | 	if (!cpu_is_managed[freq->cpu]) | 
 | 52 | 		return 0; | 
 | 53 |  | 
 | 54 | 	dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n", | 
 | 55 | 			freq->cpu, freq->new); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | 	cpu_cur_freq[freq->cpu] = freq->new; | 
 | 57 |  | 
 | 58 |         return 0; | 
 | 59 | } | 
 | 60 |  | 
 | 61 | static struct notifier_block userspace_cpufreq_notifier_block = { | 
 | 62 |         .notifier_call  = userspace_cpufreq_notifier | 
 | 63 | }; | 
 | 64 |  | 
 | 65 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 66 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 |  * cpufreq_set - set the CPU frequency | 
| Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 68 |  * @policy: pointer to policy struct where freq is being set | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 |  * @freq: target frequency in kHz | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 |  * | 
 | 71 |  * Sets the CPU frequency to freq. | 
 | 72 |  */ | 
| Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 73 | static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { | 
 | 75 | 	int ret = -EINVAL; | 
 | 76 |  | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 77 | 	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] | 78 |  | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 79 | 	mutex_lock(&userspace_mutex); | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 80 | 	if (!cpu_is_managed[policy->cpu]) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | 		goto err; | 
 | 82 |  | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 83 | 	cpu_set_freq[policy->cpu] = freq; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 |  | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 85 | 	if (freq < cpu_min_freq[policy->cpu]) | 
 | 86 | 		freq = cpu_min_freq[policy->cpu]; | 
 | 87 | 	if (freq > cpu_max_freq[policy->cpu]) | 
 | 88 | 		freq = cpu_max_freq[policy->cpu]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 |  | 
 | 90 | 	/* | 
 | 91 | 	 * We're safe from concurrent calls to ->target() here | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 92 | 	 * as we hold the userspace_mutex lock. If we were calling | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | 	 * cpufreq_driver_target, a deadlock situation might occur: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 94 | 	 * A: cpufreq_set (lock userspace_mutex) -> cpufreq_driver_target(lock policy->lock) | 
 | 95 | 	 * 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] | 96 | 	 */ | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 97 | 	ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 |  | 
 | 99 |  err: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 100 | 	mutex_unlock(&userspace_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | 	return ret; | 
 | 102 | } | 
 | 103 |  | 
 | 104 |  | 
| Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 105 | static ssize_t show_speed(struct cpufreq_policy *policy, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { | 
| Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 107 | 	return sprintf(buf, "%u\n", cpu_cur_freq[policy->cpu]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } | 
 | 109 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | static int cpufreq_governor_userspace(struct cpufreq_policy *policy, | 
 | 111 | 				   unsigned int event) | 
 | 112 | { | 
 | 113 | 	unsigned int cpu = policy->cpu; | 
| Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 114 | 	int rc = 0; | 
 | 115 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | 	switch (event) { | 
 | 117 | 	case CPUFREQ_GOV_START: | 
 | 118 | 		if (!cpu_online(cpu)) | 
 | 119 | 			return -EINVAL; | 
 | 120 | 		BUG_ON(!policy->cur); | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 121 | 		mutex_lock(&userspace_mutex); | 
| Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 122 |  | 
| Venki Pallipadi | c7f652e | 2007-06-20 14:24:00 -0700 | [diff] [blame] | 123 | 		if (cpus_using_userspace_governor == 0) { | 
 | 124 | 			cpufreq_register_notifier( | 
 | 125 | 					&userspace_cpufreq_notifier_block, | 
 | 126 | 					CPUFREQ_TRANSITION_NOTIFIER); | 
 | 127 | 		} | 
 | 128 | 		cpus_using_userspace_governor++; | 
 | 129 |  | 
| Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 130 | 		cpu_is_managed[cpu] = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | 		cpu_min_freq[cpu] = policy->min; | 
 | 132 | 		cpu_max_freq[cpu] = policy->max; | 
 | 133 | 		cpu_cur_freq[cpu] = policy->cur; | 
 | 134 | 		cpu_set_freq[cpu] = policy->cur; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | 		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]); | 
| Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 136 |  | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 137 | 		mutex_unlock(&userspace_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | 		break; | 
 | 139 | 	case CPUFREQ_GOV_STOP: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 140 | 		mutex_lock(&userspace_mutex); | 
| Venki Pallipadi | c7f652e | 2007-06-20 14:24:00 -0700 | [diff] [blame] | 141 | 		cpus_using_userspace_governor--; | 
 | 142 | 		if (cpus_using_userspace_governor == 0) { | 
 | 143 | 			cpufreq_unregister_notifier( | 
 | 144 | 					&userspace_cpufreq_notifier_block, | 
 | 145 | 					CPUFREQ_TRANSITION_NOTIFIER); | 
 | 146 | 		} | 
 | 147 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 		cpu_is_managed[cpu] = 0; | 
 | 149 | 		cpu_min_freq[cpu] = 0; | 
 | 150 | 		cpu_max_freq[cpu] = 0; | 
 | 151 | 		cpu_set_freq[cpu] = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | 		dprintk("managing cpu %u stopped\n", cpu); | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 153 | 		mutex_unlock(&userspace_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | 		break; | 
 | 155 | 	case CPUFREQ_GOV_LIMITS: | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 156 | 		mutex_lock(&userspace_mutex); | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 157 | 		dprintk("limit event for cpu %u: %u - %u kHz," | 
 | 158 | 			"currently %u kHz, last set to %u kHz\n", | 
 | 159 | 			cpu, policy->min, policy->max, | 
 | 160 | 			cpu_cur_freq[cpu], cpu_set_freq[cpu]); | 
 | 161 | 		if (policy->max < cpu_set_freq[cpu]) { | 
 | 162 | 			__cpufreq_driver_target(policy, policy->max, | 
 | 163 | 						CPUFREQ_RELATION_H); | 
 | 164 | 		} | 
 | 165 | 		else if (policy->min > cpu_set_freq[cpu]) { | 
 | 166 | 			__cpufreq_driver_target(policy, policy->min, | 
 | 167 | 						CPUFREQ_RELATION_L); | 
 | 168 | 		} | 
 | 169 | 		else { | 
 | 170 | 			__cpufreq_driver_target(policy, cpu_set_freq[cpu], | 
 | 171 | 						CPUFREQ_RELATION_L); | 
 | 172 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | 		cpu_min_freq[cpu] = policy->min; | 
 | 174 | 		cpu_max_freq[cpu] = policy->max; | 
| Thomas Renninger | c067286 | 2006-01-27 16:15:26 +0100 | [diff] [blame] | 175 | 		cpu_cur_freq[cpu] = policy->cur; | 
| akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 176 | 		mutex_unlock(&userspace_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | 		break; | 
 | 178 | 	} | 
| Jeff Garzik | 914f7c3 | 2006-10-20 14:31:00 -0700 | [diff] [blame] | 179 | 	return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } | 
 | 181 |  | 
 | 182 |  | 
 | 183 | struct cpufreq_governor cpufreq_gov_userspace = { | 
 | 184 | 	.name		= "userspace", | 
 | 185 | 	.governor	= cpufreq_governor_userspace, | 
| Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 186 | 	.store_setspeed	= cpufreq_set, | 
 | 187 | 	.show_setspeed	= show_speed, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | 	.owner		= THIS_MODULE, | 
 | 189 | }; | 
 | 190 | EXPORT_SYMBOL(cpufreq_gov_userspace); | 
 | 191 |  | 
 | 192 | static int __init cpufreq_gov_userspace_init(void) | 
 | 193 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | 	return cpufreq_register_governor(&cpufreq_gov_userspace); | 
 | 195 | } | 
 | 196 |  | 
 | 197 |  | 
 | 198 | static void __exit cpufreq_gov_userspace_exit(void) | 
 | 199 | { | 
 | 200 | 	cpufreq_unregister_governor(&cpufreq_gov_userspace); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } | 
 | 202 |  | 
 | 203 |  | 
 | 204 | MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>"); | 
 | 205 | MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'"); | 
 | 206 | MODULE_LICENSE ("GPL"); | 
 | 207 |  | 
| Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 208 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | fs_initcall(cpufreq_gov_userspace_init); | 
| Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 210 | #else | 
 | 211 | module_init(cpufreq_gov_userspace_init); | 
 | 212 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | module_exit(cpufreq_gov_userspace_exit); |