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];
72 static unsigned int cpu_is_managed[NR_CPUS];
73 static struct cpufreq_policy current_policy[NR_CPUS];
75 static DECLARE_MUTEX (userspace_sem);
78 /* keep track of frequency transitions */
80 userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
83 struct cpufreq_freqs *freq = data;
85 cpu_cur_freq[freq->cpu] = freq->new;
90 static struct notifier_block userspace_cpufreq_notifier_block = {
91 .notifier_call = userspace_cpufreq_notifier
96 * cpufreq_set - set the CPU frequency
97 * @freq: target frequency in kHz
98 * @cpu: CPU for which the frequency is to be set
100 * Sets the CPU frequency to freq.
102 int cpufreq_set(unsigned int freq, unsigned int cpu)
106 down(&userspace_sem);
107 if (!cpu_is_managed[cpu])
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];
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)
122 ret = __cpufreq_driver_target(¤t_policy[cpu], freq,
129 EXPORT_SYMBOL_GPL(cpufreq_set);
133 * cpufreq_setmax - set the CPU to the maximum frequency
134 * @cpu - affected cpu;
136 * Sets the CPU frequency to the maximum frequency supported by
139 int cpufreq_setmax(unsigned int cpu)
141 if (!cpu_is_managed[cpu] || !cpu_online(cpu))
143 return cpufreq_set(cpu_max_freq[cpu], cpu);
145 EXPORT_SYMBOL_GPL(cpufreq_setmax);
148 #ifdef CONFIG_CPU_FREQ_24_API
151 /*********************** cpufreq_sysctl interface ********************/
153 cpufreq_procctl(ctl_table *ctl, int write, struct file *filp,
154 void __user *buffer, size_t *lenp)
157 int cpu = (long) ctl->extra1;
158 unsigned int len, left = *lenp;
160 if (!left || (filp->f_pos && !write) || !cpu_online(cpu)) {
169 if (left > sizeof(buf))
171 if (copy_from_user(buf, buffer, left))
173 buf[sizeof(buf) - 1] = '\0';
175 freq = simple_strtoul(buf, &p, 0);
176 cpufreq_set(freq, cpu);
178 len = sprintf(buf, "%d\n", cpufreq_get(cpu));
181 if (copy_to_user(buffer, buf, len))
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)
195 int cpu = (long) table->extra1;
197 if (!cpu_online(cpu))
200 if (oldval && oldlenp) {
203 if (get_user(oldlen, oldlenp))
206 if (oldlen != sizeof(unsigned int))
209 if (put_user(cpufreq_get(cpu), (unsigned int __user *)oldval) ||
210 put_user(sizeof(unsigned int), oldlenp))
213 if (newval && newlen) {
216 if (newlen != sizeof(unsigned int))
219 if (get_user(freq, (unsigned int __user *)newval))
222 cpufreq_set(freq, cpu);
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);
231 CTL_TABLE_CPU_VARS(1);
234 CTL_TABLE_CPU_VARS(2);
237 CTL_TABLE_CPU_VARS(3);
240 CTL_TABLE_CPU_VARS(4);
243 CTL_TABLE_CPU_VARS(5);
246 CTL_TABLE_CPU_VARS(6);
249 CTL_TABLE_CPU_VARS(7);
252 CTL_TABLE_CPU_VARS(8);
255 CTL_TABLE_CPU_VARS(9);
258 CTL_TABLE_CPU_VARS(10);
261 CTL_TABLE_CPU_VARS(11);
264 CTL_TABLE_CPU_VARS(12);
267 CTL_TABLE_CPU_VARS(13);
270 CTL_TABLE_CPU_VARS(14);
273 CTL_TABLE_CPU_VARS(15);
276 CTL_TABLE_CPU_VARS(16);
279 CTL_TABLE_CPU_VARS(17);
282 CTL_TABLE_CPU_VARS(18);
285 CTL_TABLE_CPU_VARS(19);
288 CTL_TABLE_CPU_VARS(20);
291 CTL_TABLE_CPU_VARS(21);
294 CTL_TABLE_CPU_VARS(22);
297 CTL_TABLE_CPU_VARS(23);
300 CTL_TABLE_CPU_VARS(24);
303 CTL_TABLE_CPU_VARS(25);
306 CTL_TABLE_CPU_VARS(26);
309 CTL_TABLE_CPU_VARS(27);
312 CTL_TABLE_CPU_VARS(28);
315 CTL_TABLE_CPU_VARS(29);
318 CTL_TABLE_CPU_VARS(30);
321 CTL_TABLE_CPU_VARS(31);
324 #error please extend CPU enumeration
327 /* due to NR_CPUS tweaking, a lot of if/endifs are required, sorry */
328 static ctl_table ctl_cpu_table[NR_CPUS + 1] = {
424 #error please extend CPU enumeration
431 static ctl_table ctl_cpu[2] = {
436 .child = ctl_cpu_table,
443 struct ctl_table_header *cpufreq_sysctl_table;
445 static inline void cpufreq_sysctl_init(void)
447 cpufreq_sysctl_table = register_sysctl_table(ctl_cpu, 0);
450 static inline void cpufreq_sysctl_exit(void)
452 unregister_sysctl_table(cpufreq_sysctl_table);
456 #define cpufreq_sysctl_init() do {} while(0)
457 #define cpufreq_sysctl_exit() do {} while(0)
458 #endif /* CONFIG_CPU_FREQ_24API */
461 /************************** sysfs interface ************************/
462 static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
464 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
468 store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
470 unsigned int freq = 0;
473 ret = sscanf (buf, "%u", &freq);
477 cpufreq_set(freq, policy->cpu);
482 static struct freq_attr freq_attr_scaling_setspeed = {
483 .attr = { .name = "scaling_setspeed", .mode = 0644 },
485 .store = store_speed,
488 static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
491 unsigned int cpu = policy->cpu;
493 case CPUFREQ_GOV_START:
494 if ((!cpu_online(cpu)) || (!try_module_get(THIS_MODULE)))
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 (¤t_policy[cpu], policy, sizeof(struct cpufreq_policy));
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);
513 module_put(THIS_MODULE);
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(¤t_policy[cpu], policy->max,
522 else if (policy->min > cpu_cur_freq[cpu])
523 __cpufreq_driver_target(¤t_policy[cpu], policy->min,
525 memcpy (¤t_policy[cpu], policy, sizeof(struct cpufreq_policy));
533 struct cpufreq_governor cpufreq_gov_userspace = {
535 .governor = cpufreq_governor_userspace,
536 .owner = THIS_MODULE,
538 EXPORT_SYMBOL(cpufreq_gov_userspace);
540 static int __init cpufreq_gov_userspace_init(void)
542 cpufreq_sysctl_init();
543 cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
544 return cpufreq_register_governor(&cpufreq_gov_userspace);
548 static void __exit cpufreq_gov_userspace_exit(void)
550 cpufreq_unregister_governor(&cpufreq_gov_userspace);
551 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
552 cpufreq_sysctl_exit();
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");
560 fs_initcall(cpufreq_gov_userspace_init);
561 module_exit(cpufreq_gov_userspace_exit);