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