Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / drivers / cpufreq / cpufreq_userspace.c
1 /*
2  *  linux/drivers/cpufreq/cpufreq_userspace.c
3  *
4  *  Copyright (C)  2001 Russell King
5  *            (C)  2002 - 2004 Dominik Brodowski <linux@brodo.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  */
12
13 #include <linux/config.h>
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/types.h>
22 #include <linux/fs.h>
23 #include <linux/sysfs.h>
24
25 #include <asm/uaccess.h>
26
27
28 /**
29  * A few values needed by the userspace governor
30  */
31 static unsigned int     cpu_max_freq[NR_CPUS];
32 static unsigned int     cpu_min_freq[NR_CPUS];
33 static unsigned int     cpu_cur_freq[NR_CPUS]; /* current CPU freq */
34 static unsigned int     cpu_set_freq[NR_CPUS]; /* CPU freq desired by userspace */
35 static unsigned int     cpu_is_managed[NR_CPUS];
36 static struct cpufreq_policy current_policy[NR_CPUS];
37
38 static DECLARE_MUTEX    (userspace_sem); 
39
40 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
41
42 /* keep track of frequency transitions */
43 static int 
44 userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
45                        void *data)
46 {
47         struct cpufreq_freqs *freq = data;
48
49         dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n", freq->cpu, freq->new);
50         cpu_cur_freq[freq->cpu] = freq->new;
51
52         return 0;
53 }
54
55 static struct notifier_block userspace_cpufreq_notifier_block = {
56         .notifier_call  = userspace_cpufreq_notifier
57 };
58
59
60 /** 
61  * cpufreq_set - set the CPU frequency
62  * @freq: target frequency in kHz
63  * @cpu: CPU for which the frequency is to be set
64  *
65  * Sets the CPU frequency to freq.
66  */
67 static int cpufreq_set(unsigned int freq, unsigned int cpu)
68 {
69         int ret = -EINVAL;
70
71         dprintk("cpufreq_set for cpu %u, freq %u kHz\n", cpu, freq);
72
73         down(&userspace_sem);
74         if (!cpu_is_managed[cpu])
75                 goto err;
76
77         cpu_set_freq[cpu] = freq;
78
79         if (freq < cpu_min_freq[cpu])
80                 freq = cpu_min_freq[cpu];
81         if (freq > cpu_max_freq[cpu])
82                 freq = cpu_max_freq[cpu];
83
84         /*
85          * We're safe from concurrent calls to ->target() here
86          * as we hold the userspace_sem lock. If we were calling
87          * cpufreq_driver_target, a deadlock situation might occur:
88          * A: cpufreq_set (lock userspace_sem) -> cpufreq_driver_target(lock policy->lock)
89          * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_sem)
90          */
91         ret = __cpufreq_driver_target(&current_policy[cpu], freq, 
92               CPUFREQ_RELATION_L);
93
94  err:
95         up(&userspace_sem);
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->cpu);
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                 down(&userspace_sem);
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                 memcpy (&current_policy[cpu], policy, sizeof(struct cpufreq_policy));
145                 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]);
146                 up(&userspace_sem);
147                 break;
148         case CPUFREQ_GOV_STOP:
149                 down(&userspace_sem);
150                 cpu_is_managed[cpu] = 0;
151                 cpu_min_freq[cpu] = 0;
152                 cpu_max_freq[cpu] = 0;
153                 cpu_set_freq[cpu] = 0;
154                 sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
155                 dprintk("managing cpu %u stopped\n", cpu);
156                 up(&userspace_sem);
157                 break;
158         case CPUFREQ_GOV_LIMITS:
159                 down(&userspace_sem);
160                 cpu_min_freq[cpu] = policy->min;
161                 cpu_max_freq[cpu] = policy->max;
162                 dprintk("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n", cpu, cpu_min_freq[cpu], cpu_max_freq[cpu], cpu_cur_freq[cpu], cpu_set_freq[cpu]);
163                 if (policy->max < cpu_set_freq[cpu]) {
164                         __cpufreq_driver_target(&current_policy[cpu], policy->max, 
165                               CPUFREQ_RELATION_H);
166                 } else if (policy->min > cpu_set_freq[cpu]) {
167                         __cpufreq_driver_target(&current_policy[cpu], policy->min, 
168                               CPUFREQ_RELATION_L);
169                 } else {
170                         __cpufreq_driver_target(&current_policy[cpu], cpu_set_freq[cpu],
171                               CPUFREQ_RELATION_L);
172                 }
173                 memcpy (&current_policy[cpu], policy, sizeof(struct cpufreq_policy));
174                 up(&userspace_sem);
175                 break;
176         }
177         return 0;
178 }
179
180
181 struct cpufreq_governor cpufreq_gov_userspace = {
182         .name           = "userspace",
183         .governor       = cpufreq_governor_userspace,
184         .owner          = THIS_MODULE,
185 };
186 EXPORT_SYMBOL(cpufreq_gov_userspace);
187
188 static int __init cpufreq_gov_userspace_init(void)
189 {
190         cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
191         return cpufreq_register_governor(&cpufreq_gov_userspace);
192 }
193
194
195 static void __exit cpufreq_gov_userspace_exit(void)
196 {
197         cpufreq_unregister_governor(&cpufreq_gov_userspace);
198         cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
199 }
200
201
202 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
203 MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
204 MODULE_LICENSE ("GPL");
205
206 fs_initcall(cpufreq_gov_userspace_init);
207 module_exit(cpufreq_gov_userspace_exit);