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