2 * linux/drivers/cpufreq/cpufreq_userspace.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
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.
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>
25 #include <linux/sysfs.h>
27 #include <asm/uaccess.h>
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]),\
35 .proc_handler = proc_dointvec, }
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]),\
43 .proc_handler = proc_dointvec, }
45 #define CTL_CPU_VARS_SPEED(cpunr) { \
46 .ctl_name = CPU_NR_FREQ, \
47 .procname = "speed", \
49 .proc_handler = cpufreq_procctl, \
50 .strategy = cpufreq_sysctl, \
51 .extra1 = (void*) (cpunr), }
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), \
59 /* the ctl_table entry for each CPU */
60 #define CPU_ENUM(s) { \
61 .ctl_name = (CPU_NR + s), \
64 .child = ctl_cpu_vars_##s }
67 * A few values needed by the userspace governor
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];
76 static DECLARE_MUTEX (userspace_sem);
78 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
80 /* keep track of frequency transitions */
82 userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
85 struct cpufreq_freqs *freq = data;
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;
93 static struct notifier_block userspace_cpufreq_notifier_block = {
94 .notifier_call = userspace_cpufreq_notifier
99 * _cpufreq_set - set the CPU frequency
100 * @freq: target frequency in kHz
101 * @cpu: CPU for which the frequency is to be set
103 * Sets the CPU frequency to freq.
105 static int _cpufreq_set(unsigned int freq, unsigned int cpu)
109 dprintk("_cpufreq_set for cpu %u, freq %u kHz\n", cpu, freq);
111 down(&userspace_sem);
112 if (!cpu_is_managed[cpu])
115 cpu_set_freq[cpu] = freq;
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];
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)
129 ret = __cpufreq_driver_target(¤t_policy[cpu], freq,
138 #ifdef CONFIG_CPU_FREQ_24_API
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
142 static unsigned int warning_print = 0;
144 int __deprecated cpufreq_set(unsigned int freq, unsigned int cpu)
146 return _cpufreq_set(freq, cpu);
148 EXPORT_SYMBOL_GPL(cpufreq_set);
152 * cpufreq_setmax - set the CPU to the maximum frequency
153 * @cpu - affected cpu;
155 * Sets the CPU frequency to the maximum frequency supported by
158 int __deprecated cpufreq_setmax(unsigned int cpu)
160 if (!cpu_is_managed[cpu] || !cpu_online(cpu))
162 return _cpufreq_set(cpu_max_freq[cpu], cpu);
164 EXPORT_SYMBOL_GPL(cpufreq_setmax);
166 /*********************** cpufreq_sysctl interface ********************/
168 cpufreq_procctl(ctl_table *ctl, int write, struct file *filp,
169 void __user *buffer, size_t *lenp, loff_t *ppos)
172 int cpu = (long) ctl->extra1;
173 unsigned int len, left = *lenp;
175 if (!left || (*ppos && !write) || !cpu_online(cpu)) {
180 if (!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");
191 if (left > sizeof(buf))
193 if (copy_from_user(buf, buffer, left))
195 buf[sizeof(buf) - 1] = '\0';
197 freq = simple_strtoul(buf, &p, 0);
198 _cpufreq_set(freq, cpu);
200 len = sprintf(buf, "%d\n", cpufreq_get(cpu));
203 if (copy_to_user(buffer, buf, len))
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)
217 int cpu = (long) table->extra1;
219 if (!cpu_online(cpu))
222 if (!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");
229 if (oldval && oldlenp) {
232 if (get_user(oldlen, oldlenp))
235 if (oldlen != sizeof(unsigned int))
238 if (put_user(cpufreq_get(cpu), (unsigned int __user *)oldval) ||
239 put_user(sizeof(unsigned int), oldlenp))
242 if (newval && newlen) {
245 if (newlen != sizeof(unsigned int))
248 if (get_user(freq, (unsigned int __user *)newval))
251 _cpufreq_set(freq, cpu);
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);
260 CTL_TABLE_CPU_VARS(1);
263 CTL_TABLE_CPU_VARS(2);
266 CTL_TABLE_CPU_VARS(3);
269 CTL_TABLE_CPU_VARS(4);
272 CTL_TABLE_CPU_VARS(5);
275 CTL_TABLE_CPU_VARS(6);
278 CTL_TABLE_CPU_VARS(7);
281 CTL_TABLE_CPU_VARS(8);
284 CTL_TABLE_CPU_VARS(9);
287 CTL_TABLE_CPU_VARS(10);
290 CTL_TABLE_CPU_VARS(11);
293 CTL_TABLE_CPU_VARS(12);
296 CTL_TABLE_CPU_VARS(13);
299 CTL_TABLE_CPU_VARS(14);
302 CTL_TABLE_CPU_VARS(15);
305 CTL_TABLE_CPU_VARS(16);
308 CTL_TABLE_CPU_VARS(17);
311 CTL_TABLE_CPU_VARS(18);
314 CTL_TABLE_CPU_VARS(19);
317 CTL_TABLE_CPU_VARS(20);
320 CTL_TABLE_CPU_VARS(21);
323 CTL_TABLE_CPU_VARS(22);
326 CTL_TABLE_CPU_VARS(23);
329 CTL_TABLE_CPU_VARS(24);
332 CTL_TABLE_CPU_VARS(25);
335 CTL_TABLE_CPU_VARS(26);
338 CTL_TABLE_CPU_VARS(27);
341 CTL_TABLE_CPU_VARS(28);
344 CTL_TABLE_CPU_VARS(29);
347 CTL_TABLE_CPU_VARS(30);
350 CTL_TABLE_CPU_VARS(31);
353 #error please extend CPU enumeration
356 /* due to NR_CPUS tweaking, a lot of if/endifs are required, sorry */
357 static ctl_table ctl_cpu_table[NR_CPUS + 1] = {
453 #error please extend CPU enumeration
460 static ctl_table ctl_cpu[2] = {
465 .child = ctl_cpu_table,
472 static struct ctl_table_header *cpufreq_sysctl_table;
474 static inline void cpufreq_sysctl_init(void)
476 cpufreq_sysctl_table = register_sysctl_table(ctl_cpu, 0);
479 static inline void cpufreq_sysctl_exit(void)
481 unregister_sysctl_table(cpufreq_sysctl_table);
485 #define cpufreq_sysctl_init() do {} while(0)
486 #define cpufreq_sysctl_exit() do {} while(0)
487 #endif /* CONFIG_CPU_FREQ_24API */
490 /************************** sysfs interface ************************/
491 static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
493 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
497 store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
499 unsigned int freq = 0;
502 ret = sscanf (buf, "%u", &freq);
506 _cpufreq_set(freq, policy->cpu);
511 static struct freq_attr freq_attr_scaling_setspeed =
513 .attr = { .name = "scaling_setspeed", .mode = 0644, .owner = THIS_MODULE },
515 .store = store_speed,
518 static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
521 unsigned int cpu = policy->cpu;
523 case CPUFREQ_GOV_START:
524 if (!cpu_online(cpu))
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 (¤t_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]);
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);
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(¤t_policy[cpu], policy->max,
556 } else if (policy->min > cpu_set_freq[cpu]) {
557 __cpufreq_driver_target(¤t_policy[cpu], policy->min,
560 __cpufreq_driver_target(¤t_policy[cpu], cpu_set_freq[cpu],
563 memcpy (¤t_policy[cpu], policy, sizeof(struct cpufreq_policy));
571 struct cpufreq_governor cpufreq_gov_userspace = {
573 .governor = cpufreq_governor_userspace,
574 .owner = THIS_MODULE,
576 EXPORT_SYMBOL(cpufreq_gov_userspace);
578 static int __init cpufreq_gov_userspace_init(void)
580 cpufreq_sysctl_init();
581 cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
582 return cpufreq_register_governor(&cpufreq_gov_userspace);
586 static void __exit cpufreq_gov_userspace_exit(void)
588 cpufreq_unregister_governor(&cpufreq_gov_userspace);
589 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
590 cpufreq_sysctl_exit();
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");
598 fs_initcall(cpufreq_gov_userspace_init);
599 module_exit(cpufreq_gov_userspace_exit);