Merge branch 'upstream'
[linux-2.6] / drivers / cpufreq / cpufreq_userspace.c
1
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
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/smp.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/interrupt.h>
21 #include <linux/cpufreq.h>
22 #include <linux/types.h>
23 #include <linux/fs.h>
24 #include <linux/sysfs.h>
25 #include <linux/mutex.h>
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];
38
39 static DEFINE_MUTEX     (userspace_mutex);
40
41 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
42
43 /* keep track of frequency transitions */
44 static int
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
61 /**
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  */
68 static int cpufreq_set(unsigned int freq, struct cpufreq_policy *policy)
69 {
70         int ret = -EINVAL;
71
72         dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
73
74         mutex_lock(&userspace_mutex);
75         if (!cpu_is_managed[policy->cpu])
76                 goto err;
77
78         cpu_set_freq[policy->cpu] = freq;
79
80         if (freq < cpu_min_freq[policy->cpu])
81                 freq = cpu_min_freq[policy->cpu];
82         if (freq > cpu_max_freq[policy->cpu])
83                 freq = cpu_max_freq[policy->cpu];
84
85         /*
86          * We're safe from concurrent calls to ->target() here
87          * as we hold the userspace_mutex lock. If we were calling
88          * cpufreq_driver_target, a deadlock situation might occur:
89          * A: cpufreq_set (lock userspace_mutex) -> cpufreq_driver_target(lock policy->lock)
90          * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_mutex)
91          */
92         ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
93
94  err:
95         mutex_unlock(&userspace_mutex);
96         return ret;
97 }
98
99
100 /************************** sysfs interface ************************/
101 static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
102 {
103         return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
104 }
105
106 static ssize_t
107 store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
108 {
109         unsigned int freq = 0;
110         unsigned int ret;
111
112         ret = sscanf (buf, "%u", &freq);
113         if (ret != 1)
114                 return -EINVAL;
115
116         cpufreq_set(freq, policy);
117
118         return count;
119 }
120
121 static struct freq_attr freq_attr_scaling_setspeed =
122 {
123         .attr = { .name = "scaling_setspeed", .mode = 0644, .owner = THIS_MODULE },
124         .show = show_speed,
125         .store = store_speed,
126 };
127
128 static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
129                                    unsigned int event)
130 {
131         unsigned int cpu = policy->cpu;
132         switch (event) {
133         case CPUFREQ_GOV_START:
134                 if (!cpu_online(cpu))
135                         return -EINVAL;
136                 BUG_ON(!policy->cur);
137                 mutex_lock(&userspace_mutex);
138                 cpu_is_managed[cpu] = 1;
139                 cpu_min_freq[cpu] = policy->min;
140                 cpu_max_freq[cpu] = policy->max;
141                 cpu_cur_freq[cpu] = policy->cur;
142                 cpu_set_freq[cpu] = policy->cur;
143                 sysfs_create_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
144                 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]);
145                 mutex_unlock(&userspace_mutex);
146                 break;
147         case CPUFREQ_GOV_STOP:
148                 mutex_lock(&userspace_mutex);
149                 cpu_is_managed[cpu] = 0;
150                 cpu_min_freq[cpu] = 0;
151                 cpu_max_freq[cpu] = 0;
152                 cpu_set_freq[cpu] = 0;
153                 sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
154                 dprintk("managing cpu %u stopped\n", cpu);
155                 mutex_unlock(&userspace_mutex);
156                 break;
157         case CPUFREQ_GOV_LIMITS:
158                 mutex_lock(&userspace_mutex);
159                 dprintk("limit event for cpu %u: %u - %u kHz,"
160                         "currently %u kHz, last set to %u kHz\n",
161                         cpu, policy->min, policy->max,
162                         cpu_cur_freq[cpu], cpu_set_freq[cpu]);
163                 if (policy->max < cpu_set_freq[cpu]) {
164                         __cpufreq_driver_target(policy, policy->max,
165                                                 CPUFREQ_RELATION_H);
166                 }
167                 else if (policy->min > cpu_set_freq[cpu]) {
168                         __cpufreq_driver_target(policy, policy->min,
169                                                 CPUFREQ_RELATION_L);
170                 }
171                 else {
172                         __cpufreq_driver_target(policy, cpu_set_freq[cpu],
173                                                 CPUFREQ_RELATION_L);
174                 }
175                 cpu_min_freq[cpu] = policy->min;
176                 cpu_max_freq[cpu] = policy->max;
177                 cpu_cur_freq[cpu] = policy->cur;
178                 mutex_unlock(&userspace_mutex);
179                 break;
180         }
181         return 0;
182 }
183
184
185 struct cpufreq_governor cpufreq_gov_userspace = {
186         .name           = "userspace",
187         .governor       = cpufreq_governor_userspace,
188         .owner          = THIS_MODULE,
189 };
190 EXPORT_SYMBOL(cpufreq_gov_userspace);
191
192 static int __init cpufreq_gov_userspace_init(void)
193 {
194         cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
195         return cpufreq_register_governor(&cpufreq_gov_userspace);
196 }
197
198
199 static void __exit cpufreq_gov_userspace_exit(void)
200 {
201         cpufreq_unregister_governor(&cpufreq_gov_userspace);
202         cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
203 }
204
205
206 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
207 MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
208 MODULE_LICENSE ("GPL");
209
210 fs_initcall(cpufreq_gov_userspace_init);
211 module_exit(cpufreq_gov_userspace_exit);