patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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/ctype.h>
21 #include <linux/cpufreq.h>
22 #include <linux/sysctl.h>
23 #include <linux/types.h>
24 #include <linux/fs.h>
25 #include <linux/sysfs.h>
26
27 #include <asm/uaccess.h>
28
29 #define CTL_CPU_VARS_SPEED_MAX(cpunr) { \
30                 .ctl_name       = CPU_NR_FREQ_MAX, \
31                 .data           = &cpu_max_freq[cpunr], \
32                 .procname       = "speed-max", \
33                 .maxlen         = sizeof(cpu_max_freq[cpunr]),\
34                 .mode           = 0444, \
35                 .proc_handler   = proc_dointvec, }
36
37 #define CTL_CPU_VARS_SPEED_MIN(cpunr) { \
38                 .ctl_name       = CPU_NR_FREQ_MIN, \
39                 .data           = &cpu_min_freq[cpunr], \
40                 .procname       = "speed-min", \
41                 .maxlen         = sizeof(cpu_min_freq[cpunr]),\
42                 .mode           = 0444, \
43                 .proc_handler   = proc_dointvec, }
44
45 #define CTL_CPU_VARS_SPEED(cpunr) { \
46                 .ctl_name       = CPU_NR_FREQ, \
47                 .procname       = "speed", \
48                 .mode           = 0644, \
49                 .proc_handler   = cpufreq_procctl, \
50                 .strategy       = cpufreq_sysctl, \
51                 .extra1         = (void*) (cpunr), }
52
53 #define CTL_TABLE_CPU_VARS(cpunr) static ctl_table ctl_cpu_vars_##cpunr[] = {\
54                 CTL_CPU_VARS_SPEED_MAX(cpunr), \
55                 CTL_CPU_VARS_SPEED_MIN(cpunr), \
56                 CTL_CPU_VARS_SPEED(cpunr),  \
57                 { .ctl_name = 0, }, }
58
59 /* the ctl_table entry for each CPU */
60 #define CPU_ENUM(s) { \
61                 .ctl_name       = (CPU_NR + s), \
62                 .procname       = #s, \
63                 .mode           = 0555, \
64                 .child          = ctl_cpu_vars_##s }
65
66 /**
67  * A few values needed by the userspace governor
68  */
69 static unsigned int     cpu_max_freq[NR_CPUS];
70 static unsigned int     cpu_min_freq[NR_CPUS];
71 static unsigned int     cpu_cur_freq[NR_CPUS];
72 static unsigned int     cpu_is_managed[NR_CPUS];
73 static struct cpufreq_policy current_policy[NR_CPUS];
74
75 static DECLARE_MUTEX    (userspace_sem); 
76
77
78 /* keep track of frequency transitions */
79 static int 
80 userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
81                        void *data)
82 {
83         struct cpufreq_freqs *freq = data;
84
85         cpu_cur_freq[freq->cpu] = freq->new;
86
87         return 0;
88 }
89
90 static struct notifier_block userspace_cpufreq_notifier_block = {
91         .notifier_call  = userspace_cpufreq_notifier
92 };
93
94
95 /** 
96  * cpufreq_set - set the CPU frequency
97  * @freq: target frequency in kHz
98  * @cpu: CPU for which the frequency is to be set
99  *
100  * Sets the CPU frequency to freq.
101  */
102 int cpufreq_set(unsigned int freq, unsigned int cpu)
103 {
104         int ret = -EINVAL;
105
106         down(&userspace_sem);
107         if (!cpu_is_managed[cpu])
108                 goto err;
109
110         if (freq < cpu_min_freq[cpu])
111                 freq = cpu_min_freq[cpu];
112         if (freq > cpu_max_freq[cpu])
113                 freq = cpu_max_freq[cpu];
114
115         /*
116          * We're safe from concurrent calls to ->target() here
117          * as we hold the userspace_sem lock. If we were calling
118          * cpufreq_driver_target, a deadlock situation might occur:
119          * A: cpufreq_set (lock userspace_sem) -> cpufreq_driver_target(lock policy->lock)
120          * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_sem)
121          */
122         ret = __cpufreq_driver_target(&current_policy[cpu], freq, 
123               CPUFREQ_RELATION_L);
124
125  err:
126         up(&userspace_sem);
127         return ret;
128 }
129 EXPORT_SYMBOL_GPL(cpufreq_set);
130
131
132 /** 
133  * cpufreq_setmax - set the CPU to the maximum frequency
134  * @cpu - affected cpu;
135  *
136  * Sets the CPU frequency to the maximum frequency supported by
137  * this CPU.
138  */
139 int cpufreq_setmax(unsigned int cpu)
140 {
141         if (!cpu_is_managed[cpu] || !cpu_online(cpu))
142                 return -EINVAL;
143         return cpufreq_set(cpu_max_freq[cpu], cpu);
144 }
145 EXPORT_SYMBOL_GPL(cpufreq_setmax);
146
147
148 #ifdef CONFIG_CPU_FREQ_24_API
149
150
151 /*********************** cpufreq_sysctl interface ********************/
152 static int
153 cpufreq_procctl(ctl_table *ctl, int write, struct file *filp,
154                 void __user *buffer, size_t *lenp)
155 {
156         char buf[16], *p;
157         int cpu = (long) ctl->extra1;
158         unsigned int len, left = *lenp;
159
160         if (!left || (filp->f_pos && !write) || !cpu_online(cpu)) {
161                 *lenp = 0;
162                 return 0;
163         }
164
165         if (write) {
166                 unsigned int freq;
167
168                 len = left;
169                 if (left > sizeof(buf))
170                         left = sizeof(buf);
171                 if (copy_from_user(buf, buffer, left))
172                         return -EFAULT;
173                 buf[sizeof(buf) - 1] = '\0';
174
175                 freq = simple_strtoul(buf, &p, 0);
176                 cpufreq_set(freq, cpu);
177         } else {
178                 len = sprintf(buf, "%d\n", cpufreq_get(cpu));
179                 if (len > left)
180                         len = left;
181                 if (copy_to_user(buffer, buf, len))
182                         return -EFAULT;
183         }
184
185         *lenp = len;
186         filp->f_pos += len;
187         return 0;
188 }
189
190 static int
191 cpufreq_sysctl(ctl_table *table, int __user *name, int nlen,
192                void __user *oldval, size_t __user *oldlenp,
193                void __user *newval, size_t newlen, void **context)
194 {
195         int cpu = (long) table->extra1;
196
197         if (!cpu_online(cpu))
198                 return -EINVAL;
199
200         if (oldval && oldlenp) {
201                 size_t oldlen;
202
203                 if (get_user(oldlen, oldlenp))
204                         return -EFAULT;
205
206                 if (oldlen != sizeof(unsigned int))
207                         return -EINVAL;
208
209                 if (put_user(cpufreq_get(cpu), (unsigned int __user *)oldval) ||
210                     put_user(sizeof(unsigned int), oldlenp))
211                         return -EFAULT;
212         }
213         if (newval && newlen) {
214                 unsigned int freq;
215
216                 if (newlen != sizeof(unsigned int))
217                         return -EINVAL;
218
219                 if (get_user(freq, (unsigned int __user *)newval))
220                         return -EFAULT;
221
222                 cpufreq_set(freq, cpu);
223         }
224         return 1;
225 }
226
227 /* ctl_table ctl_cpu_vars_{0,1,...,(NR_CPUS-1)} */
228 /* due to NR_CPUS tweaking, a lot of if/endifs are required, sorry */
229         CTL_TABLE_CPU_VARS(0);
230 #if NR_CPUS > 1
231         CTL_TABLE_CPU_VARS(1);
232 #endif
233 #if NR_CPUS > 2
234         CTL_TABLE_CPU_VARS(2);
235 #endif
236 #if NR_CPUS > 3
237         CTL_TABLE_CPU_VARS(3);
238 #endif
239 #if NR_CPUS > 4
240         CTL_TABLE_CPU_VARS(4);
241 #endif
242 #if NR_CPUS > 5
243         CTL_TABLE_CPU_VARS(5);
244 #endif
245 #if NR_CPUS > 6
246         CTL_TABLE_CPU_VARS(6);
247 #endif
248 #if NR_CPUS > 7
249         CTL_TABLE_CPU_VARS(7);
250 #endif
251 #if NR_CPUS > 8
252         CTL_TABLE_CPU_VARS(8);
253 #endif
254 #if NR_CPUS > 9
255         CTL_TABLE_CPU_VARS(9);
256 #endif
257 #if NR_CPUS > 10
258         CTL_TABLE_CPU_VARS(10);
259 #endif
260 #if NR_CPUS > 11
261         CTL_TABLE_CPU_VARS(11);
262 #endif
263 #if NR_CPUS > 12
264         CTL_TABLE_CPU_VARS(12);
265 #endif
266 #if NR_CPUS > 13
267         CTL_TABLE_CPU_VARS(13);
268 #endif
269 #if NR_CPUS > 14
270         CTL_TABLE_CPU_VARS(14);
271 #endif
272 #if NR_CPUS > 15
273         CTL_TABLE_CPU_VARS(15);
274 #endif
275 #if NR_CPUS > 16
276         CTL_TABLE_CPU_VARS(16);
277 #endif
278 #if NR_CPUS > 17
279         CTL_TABLE_CPU_VARS(17);
280 #endif
281 #if NR_CPUS > 18
282         CTL_TABLE_CPU_VARS(18);
283 #endif
284 #if NR_CPUS > 19
285         CTL_TABLE_CPU_VARS(19);
286 #endif
287 #if NR_CPUS > 20
288         CTL_TABLE_CPU_VARS(20);
289 #endif
290 #if NR_CPUS > 21
291         CTL_TABLE_CPU_VARS(21);
292 #endif
293 #if NR_CPUS > 22
294         CTL_TABLE_CPU_VARS(22);
295 #endif
296 #if NR_CPUS > 23
297         CTL_TABLE_CPU_VARS(23);
298 #endif
299 #if NR_CPUS > 24
300         CTL_TABLE_CPU_VARS(24);
301 #endif
302 #if NR_CPUS > 25
303         CTL_TABLE_CPU_VARS(25);
304 #endif
305 #if NR_CPUS > 26
306         CTL_TABLE_CPU_VARS(26);
307 #endif
308 #if NR_CPUS > 27
309         CTL_TABLE_CPU_VARS(27);
310 #endif
311 #if NR_CPUS > 28
312         CTL_TABLE_CPU_VARS(28);
313 #endif
314 #if NR_CPUS > 29
315         CTL_TABLE_CPU_VARS(29);
316 #endif
317 #if NR_CPUS > 30
318         CTL_TABLE_CPU_VARS(30);
319 #endif
320 #if NR_CPUS > 31
321         CTL_TABLE_CPU_VARS(31);
322 #endif
323 #if NR_CPUS > 32
324 #error please extend CPU enumeration
325 #endif
326
327 /* due to NR_CPUS tweaking, a lot of if/endifs are required, sorry */
328 static ctl_table ctl_cpu_table[NR_CPUS + 1] = {
329         CPU_ENUM(0),
330 #if NR_CPUS > 1
331         CPU_ENUM(1),
332 #endif
333 #if NR_CPUS > 2
334         CPU_ENUM(2),
335 #endif
336 #if NR_CPUS > 3
337         CPU_ENUM(3),
338 #endif
339 #if NR_CPUS > 4
340         CPU_ENUM(4),
341 #endif
342 #if NR_CPUS > 5
343         CPU_ENUM(5),
344 #endif
345 #if NR_CPUS > 6
346         CPU_ENUM(6),
347 #endif
348 #if NR_CPUS > 7
349         CPU_ENUM(7),
350 #endif
351 #if NR_CPUS > 8
352         CPU_ENUM(8),
353 #endif
354 #if NR_CPUS > 9
355         CPU_ENUM(9),
356 #endif
357 #if NR_CPUS > 10
358         CPU_ENUM(10),
359 #endif
360 #if NR_CPUS > 11
361         CPU_ENUM(11),
362 #endif
363 #if NR_CPUS > 12
364         CPU_ENUM(12),
365 #endif
366 #if NR_CPUS > 13
367         CPU_ENUM(13),
368 #endif
369 #if NR_CPUS > 14
370         CPU_ENUM(14),
371 #endif
372 #if NR_CPUS > 15
373         CPU_ENUM(15),
374 #endif
375 #if NR_CPUS > 16
376         CPU_ENUM(16),
377 #endif
378 #if NR_CPUS > 17
379         CPU_ENUM(17),
380 #endif
381 #if NR_CPUS > 18
382         CPU_ENUM(18),
383 #endif
384 #if NR_CPUS > 19
385         CPU_ENUM(19),
386 #endif
387 #if NR_CPUS > 20
388         CPU_ENUM(20),
389 #endif
390 #if NR_CPUS > 21
391         CPU_ENUM(21),
392 #endif
393 #if NR_CPUS > 22
394         CPU_ENUM(22),
395 #endif
396 #if NR_CPUS > 23
397         CPU_ENUM(23),
398 #endif
399 #if NR_CPUS > 24
400         CPU_ENUM(24),
401 #endif
402 #if NR_CPUS > 25
403         CPU_ENUM(25),
404 #endif
405 #if NR_CPUS > 26
406         CPU_ENUM(26),
407 #endif
408 #if NR_CPUS > 27
409         CPU_ENUM(27),
410 #endif
411 #if NR_CPUS > 28
412         CPU_ENUM(28),
413 #endif
414 #if NR_CPUS > 29
415         CPU_ENUM(29),
416 #endif
417 #if NR_CPUS > 30
418         CPU_ENUM(30),
419 #endif
420 #if NR_CPUS > 31
421         CPU_ENUM(31),
422 #endif
423 #if NR_CPUS > 32
424 #error please extend CPU enumeration
425 #endif
426         {
427                 .ctl_name       = 0,
428         }
429 };
430
431 static ctl_table ctl_cpu[2] = {
432         {
433                 .ctl_name       = CTL_CPU,
434                 .procname       = "cpu",
435                 .mode           = 0555,
436                 .child          = ctl_cpu_table,
437         },
438         {
439                 .ctl_name       = 0,
440         }
441 };
442
443 struct ctl_table_header *cpufreq_sysctl_table;
444
445 static inline void cpufreq_sysctl_init(void)
446 {
447         cpufreq_sysctl_table = register_sysctl_table(ctl_cpu, 0);
448 }
449
450 static inline void cpufreq_sysctl_exit(void)
451 {
452         unregister_sysctl_table(cpufreq_sysctl_table);
453 }
454
455 #else
456 #define cpufreq_sysctl_init() do {} while(0)
457 #define cpufreq_sysctl_exit() do {} while(0)
458 #endif /* CONFIG_CPU_FREQ_24API */
459
460
461 /************************** sysfs interface ************************/
462 static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
463 {
464         return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
465 }
466
467 static ssize_t 
468 store_speed (struct cpufreq_policy *policy, const char *buf, size_t count) 
469 {
470         unsigned int freq = 0;
471         unsigned int ret;
472
473         ret = sscanf (buf, "%u", &freq);
474         if (ret != 1)
475                 return -EINVAL;
476
477         cpufreq_set(freq, policy->cpu);
478
479         return count;
480 }
481
482 static struct freq_attr freq_attr_scaling_setspeed = {
483         .attr = { .name = "scaling_setspeed", .mode = 0644 },
484         .show = show_speed,
485         .store = store_speed,
486 };
487
488 static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
489                                    unsigned int event)
490 {
491         unsigned int cpu = policy->cpu;
492         switch (event) {
493         case CPUFREQ_GOV_START:
494                 if ((!cpu_online(cpu)) || (!try_module_get(THIS_MODULE)))
495                         return -EINVAL;
496                 BUG_ON(!policy->cur);
497                 down(&userspace_sem);
498                 cpu_is_managed[cpu] = 1;                
499                 cpu_min_freq[cpu] = policy->min;
500                 cpu_max_freq[cpu] = policy->max;
501                 cpu_cur_freq[cpu] = policy->cur;
502                 sysfs_create_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
503                 memcpy (&current_policy[cpu], policy, sizeof(struct cpufreq_policy));
504                 up(&userspace_sem);
505                 break;
506         case CPUFREQ_GOV_STOP:
507                 down(&userspace_sem);
508                 cpu_is_managed[cpu] = 0;
509                 cpu_min_freq[cpu] = 0;
510                 cpu_max_freq[cpu] = 0;
511                 sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
512                 up(&userspace_sem);
513                 module_put(THIS_MODULE);
514                 break;
515         case CPUFREQ_GOV_LIMITS:
516                 down(&userspace_sem);
517                 cpu_min_freq[cpu] = policy->min;
518                 cpu_max_freq[cpu] = policy->max;
519                 if (policy->max < cpu_cur_freq[cpu])
520                         __cpufreq_driver_target(&current_policy[cpu], policy->max, 
521                               CPUFREQ_RELATION_H);
522                 else if (policy->min > cpu_cur_freq[cpu])
523                         __cpufreq_driver_target(&current_policy[cpu], policy->min, 
524                               CPUFREQ_RELATION_L);
525                 memcpy (&current_policy[cpu], policy, sizeof(struct cpufreq_policy));
526                 up(&userspace_sem);
527                 break;
528         }
529         return 0;
530 }
531
532
533 struct cpufreq_governor cpufreq_gov_userspace = {
534         .name           = "userspace",
535         .governor       = cpufreq_governor_userspace,
536         .owner          = THIS_MODULE,
537 };
538 EXPORT_SYMBOL(cpufreq_gov_userspace);
539
540 static int __init cpufreq_gov_userspace_init(void)
541 {
542         cpufreq_sysctl_init();
543         cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
544         return cpufreq_register_governor(&cpufreq_gov_userspace);
545 }
546
547
548 static void __exit cpufreq_gov_userspace_exit(void)
549 {
550         cpufreq_unregister_governor(&cpufreq_gov_userspace);
551         cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
552         cpufreq_sysctl_exit();
553 }
554
555
556 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
557 MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
558 MODULE_LICENSE ("GPL");
559
560 fs_initcall(cpufreq_gov_userspace_init);
561 module_exit(cpufreq_gov_userspace_exit);