[POWERPC] 82xx and embedded6xx: Use machine_*_initcall() hooks in platform code
[linux-2.6/mini2440.git] / drivers / cpufreq / cpufreq_userspace.c
blobf8cdde4bf6cd86030a8f829d00fdac862d5847f2
2 /*
3 * linux/drivers/cpufreq/cpufreq_userspace.c
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
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.
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>
21 #include <linux/cpu.h>
22 #include <linux/types.h>
23 #include <linux/fs.h>
24 #include <linux/sysfs.h>
25 #include <linux/mutex.h>
27 #include <asm/uaccess.h>
30 /**
31 * A few values needed by the userspace governor
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];
39 static DEFINE_MUTEX (userspace_mutex);
40 static int cpus_using_userspace_governor;
42 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
44 /* keep track of frequency transitions */
45 static int
46 userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
47 void *data)
49 struct cpufreq_freqs *freq = data;
51 if (!cpu_is_managed[freq->cpu])
52 return 0;
54 dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n",
55 freq->cpu, freq->new);
56 cpu_cur_freq[freq->cpu] = freq->new;
58 return 0;
61 static struct notifier_block userspace_cpufreq_notifier_block = {
62 .notifier_call = userspace_cpufreq_notifier
66 /**
67 * cpufreq_set - set the CPU frequency
68 * @freq: target frequency in kHz
69 * @cpu: CPU for which the frequency is to be set
71 * Sets the CPU frequency to freq.
73 static int cpufreq_set(unsigned int freq, struct cpufreq_policy *policy)
75 int ret = -EINVAL;
77 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
79 mutex_lock(&userspace_mutex);
80 if (!cpu_is_managed[policy->cpu])
81 goto err;
83 cpu_set_freq[policy->cpu] = freq;
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];
91 * We're safe from concurrent calls to ->target() here
92 * as we hold the userspace_mutex lock. If we were calling
93 * cpufreq_driver_target, a deadlock situation might occur:
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)
97 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
99 err:
100 mutex_unlock(&userspace_mutex);
101 return ret;
105 /************************** sysfs interface ************************/
106 static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
108 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
111 static ssize_t
112 store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
114 unsigned int freq = 0;
115 unsigned int ret;
117 ret = sscanf (buf, "%u", &freq);
118 if (ret != 1)
119 return -EINVAL;
121 cpufreq_set(freq, policy);
123 return count;
126 static struct freq_attr freq_attr_scaling_setspeed =
128 .attr = { .name = "scaling_setspeed", .mode = 0644 },
129 .show = show_speed,
130 .store = store_speed,
133 static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
134 unsigned int event)
136 unsigned int cpu = policy->cpu;
137 int rc = 0;
139 switch (event) {
140 case CPUFREQ_GOV_START:
141 if (!cpu_online(cpu))
142 return -EINVAL;
143 BUG_ON(!policy->cur);
144 mutex_lock(&userspace_mutex);
145 rc = sysfs_create_file (&policy->kobj,
146 &freq_attr_scaling_setspeed.attr);
147 if (rc)
148 goto start_out;
150 if (cpus_using_userspace_governor == 0) {
151 cpufreq_register_notifier(
152 &userspace_cpufreq_notifier_block,
153 CPUFREQ_TRANSITION_NOTIFIER);
155 cpus_using_userspace_governor++;
157 cpu_is_managed[cpu] = 1;
158 cpu_min_freq[cpu] = policy->min;
159 cpu_max_freq[cpu] = policy->max;
160 cpu_cur_freq[cpu] = policy->cur;
161 cpu_set_freq[cpu] = policy->cur;
162 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]);
163 start_out:
164 mutex_unlock(&userspace_mutex);
165 break;
166 case CPUFREQ_GOV_STOP:
167 mutex_lock(&userspace_mutex);
168 cpus_using_userspace_governor--;
169 if (cpus_using_userspace_governor == 0) {
170 cpufreq_unregister_notifier(
171 &userspace_cpufreq_notifier_block,
172 CPUFREQ_TRANSITION_NOTIFIER);
175 cpu_is_managed[cpu] = 0;
176 cpu_min_freq[cpu] = 0;
177 cpu_max_freq[cpu] = 0;
178 cpu_set_freq[cpu] = 0;
179 sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
180 dprintk("managing cpu %u stopped\n", cpu);
181 mutex_unlock(&userspace_mutex);
182 break;
183 case CPUFREQ_GOV_LIMITS:
184 mutex_lock(&userspace_mutex);
185 dprintk("limit event for cpu %u: %u - %u kHz,"
186 "currently %u kHz, last set to %u kHz\n",
187 cpu, policy->min, policy->max,
188 cpu_cur_freq[cpu], cpu_set_freq[cpu]);
189 if (policy->max < cpu_set_freq[cpu]) {
190 __cpufreq_driver_target(policy, policy->max,
191 CPUFREQ_RELATION_H);
193 else if (policy->min > cpu_set_freq[cpu]) {
194 __cpufreq_driver_target(policy, policy->min,
195 CPUFREQ_RELATION_L);
197 else {
198 __cpufreq_driver_target(policy, cpu_set_freq[cpu],
199 CPUFREQ_RELATION_L);
201 cpu_min_freq[cpu] = policy->min;
202 cpu_max_freq[cpu] = policy->max;
203 cpu_cur_freq[cpu] = policy->cur;
204 mutex_unlock(&userspace_mutex);
205 break;
207 return rc;
211 struct cpufreq_governor cpufreq_gov_userspace = {
212 .name = "userspace",
213 .governor = cpufreq_governor_userspace,
214 .owner = THIS_MODULE,
216 EXPORT_SYMBOL(cpufreq_gov_userspace);
218 static int __init cpufreq_gov_userspace_init(void)
220 return cpufreq_register_governor(&cpufreq_gov_userspace);
224 static void __exit cpufreq_gov_userspace_exit(void)
226 cpufreq_unregister_governor(&cpufreq_gov_userspace);
230 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
231 MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
232 MODULE_LICENSE ("GPL");
234 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
235 fs_initcall(cpufreq_gov_userspace_init);
236 #else
237 module_init(cpufreq_gov_userspace_init);
238 #endif
239 module_exit(cpufreq_gov_userspace_exit);