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);
149 * cpufreq_get - get the current CPU frequency (in kHz)
152 * Get the CPU current (static) CPU frequency
154 unsigned int cpufreq_get(unsigned int cpu)
156 return cpu_cur_freq[cpu];
158 EXPORT_SYMBOL(cpufreq_get);
161 #ifdef CONFIG_CPU_FREQ_24_API
164 /*********************** cpufreq_sysctl interface ********************/
166 cpufreq_procctl(ctl_table *ctl, int write, struct file *filp,
167 void __user *buffer, size_t *lenp)
170 int cpu = (long) ctl->extra1;
171 unsigned int len, left = *lenp;
173 if (!left || (filp->f_pos && !write) || !cpu_online(cpu)) {
182 if (left > sizeof(buf))
184 if (copy_from_user(buf, buffer, left))
186 buf[sizeof(buf) - 1] = '\0';
188 freq = simple_strtoul(buf, &p, 0);
189 cpufreq_set(freq, cpu);
191 len = sprintf(buf, "%d\n", cpufreq_get(cpu));
194 if (copy_to_user(buffer, buf, len))
204 cpufreq_sysctl(ctl_table *table, int __user *name, int nlen,
205 void __user *oldval, size_t __user *oldlenp,
206 void __user *newval, size_t newlen, void **context)
208 int cpu = (long) table->extra1;
210 if (!cpu_online(cpu))
213 if (oldval && oldlenp) {
216 if (get_user(oldlen, oldlenp))
219 if (oldlen != sizeof(unsigned int))
222 if (put_user(cpufreq_get(cpu), (unsigned int *)oldval) ||
223 put_user(sizeof(unsigned int), oldlenp))
226 if (newval && newlen) {
229 if (newlen != sizeof(unsigned int))
232 if (get_user(freq, (unsigned int *)newval))
235 cpufreq_set(freq, cpu);
240 /* ctl_table ctl_cpu_vars_{0,1,...,(NR_CPUS-1)} */
241 /* due to NR_CPUS tweaking, a lot of if/endifs are required, sorry */
242 CTL_TABLE_CPU_VARS(0);
244 CTL_TABLE_CPU_VARS(1);
247 CTL_TABLE_CPU_VARS(2);
250 CTL_TABLE_CPU_VARS(3);
253 CTL_TABLE_CPU_VARS(4);
256 CTL_TABLE_CPU_VARS(5);
259 CTL_TABLE_CPU_VARS(6);
262 CTL_TABLE_CPU_VARS(7);
265 CTL_TABLE_CPU_VARS(8);
268 CTL_TABLE_CPU_VARS(9);
271 CTL_TABLE_CPU_VARS(10);
274 CTL_TABLE_CPU_VARS(11);
277 CTL_TABLE_CPU_VARS(12);
280 CTL_TABLE_CPU_VARS(13);
283 CTL_TABLE_CPU_VARS(14);
286 CTL_TABLE_CPU_VARS(15);
289 CTL_TABLE_CPU_VARS(16);
292 CTL_TABLE_CPU_VARS(17);
295 CTL_TABLE_CPU_VARS(18);
298 CTL_TABLE_CPU_VARS(19);
301 CTL_TABLE_CPU_VARS(20);
304 CTL_TABLE_CPU_VARS(21);
307 CTL_TABLE_CPU_VARS(22);
310 CTL_TABLE_CPU_VARS(23);
313 CTL_TABLE_CPU_VARS(24);
316 CTL_TABLE_CPU_VARS(25);
319 CTL_TABLE_CPU_VARS(26);
322 CTL_TABLE_CPU_VARS(27);
325 CTL_TABLE_CPU_VARS(28);
328 CTL_TABLE_CPU_VARS(29);
331 CTL_TABLE_CPU_VARS(30);
334 CTL_TABLE_CPU_VARS(31);
337 #error please extend CPU enumeration
340 /* due to NR_CPUS tweaking, a lot of if/endifs are required, sorry */
341 static ctl_table ctl_cpu_table[NR_CPUS + 1] = {
437 #error please extend CPU enumeration
444 static ctl_table ctl_cpu[2] = {
449 .child = ctl_cpu_table,
456 struct ctl_table_header *cpufreq_sysctl_table;
458 static inline void cpufreq_sysctl_init(void)
460 cpufreq_sysctl_table = register_sysctl_table(ctl_cpu, 0);
463 static inline void cpufreq_sysctl_exit(void)
465 unregister_sysctl_table(cpufreq_sysctl_table);
469 #define cpufreq_sysctl_init() do {} while(0)
470 #define cpufreq_sysctl_exit() do {} while(0)
471 #endif /* CONFIG_CPU_FREQ_24API */
474 /************************** sysfs interface ************************/
475 static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
477 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
481 store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
483 unsigned int freq = 0;
486 ret = sscanf (buf, "%u", &freq);
490 cpufreq_set(freq, policy->cpu);
495 static struct freq_attr freq_attr_scaling_setspeed = {
496 .attr = { .name = "scaling_setspeed", .mode = 0644 },
498 .store = store_speed,
501 static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
504 unsigned int cpu = policy->cpu;
506 case CPUFREQ_GOV_START:
507 if ((!cpu_online(cpu)) || (!try_module_get(THIS_MODULE)))
509 BUG_ON(!policy->cur);
510 down(&userspace_sem);
511 cpu_is_managed[cpu] = 1;
512 cpu_min_freq[cpu] = policy->min;
513 cpu_max_freq[cpu] = policy->max;
514 cpu_cur_freq[cpu] = policy->cur;
515 sysfs_create_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
516 memcpy (¤t_policy[cpu], policy, sizeof(struct cpufreq_policy));
519 case CPUFREQ_GOV_STOP:
520 down(&userspace_sem);
521 cpu_is_managed[cpu] = 0;
522 cpu_min_freq[cpu] = 0;
523 cpu_max_freq[cpu] = 0;
524 sysfs_remove_file (&policy->kobj, &freq_attr_scaling_setspeed.attr);
526 module_put(THIS_MODULE);
528 case CPUFREQ_GOV_LIMITS:
529 down(&userspace_sem);
530 cpu_min_freq[cpu] = policy->min;
531 cpu_max_freq[cpu] = policy->max;
532 if (policy->max < cpu_cur_freq[cpu])
533 __cpufreq_driver_target(¤t_policy[cpu], policy->max,
535 else if (policy->min > cpu_cur_freq[cpu])
536 __cpufreq_driver_target(¤t_policy[cpu], policy->min,
538 memcpy (¤t_policy[cpu], policy, sizeof(struct cpufreq_policy));
545 /* on ARM SA1100 we need to rely on the values of cpufreq_get() - because
546 * of this, cpu_cur_freq[] needs to be set early.
548 #if defined(CONFIG_ARM) && defined(CONFIG_ARCH_SA1100)
549 extern unsigned int sa11x0_getspeed(void);
551 static void cpufreq_sa11x0_compat(void)
553 cpu_cur_freq[0] = sa11x0_getspeed();
556 #define cpufreq_sa11x0_compat() do {} while(0)
560 struct cpufreq_governor cpufreq_gov_userspace = {
562 .governor = cpufreq_governor_userspace,
563 .owner = THIS_MODULE,
565 EXPORT_SYMBOL(cpufreq_gov_userspace);
567 static int already_init = 0;
569 int cpufreq_gov_userspace_init(void)
572 down(&userspace_sem);
573 cpufreq_sa11x0_compat();
574 cpufreq_sysctl_init();
575 cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
579 return cpufreq_register_governor(&cpufreq_gov_userspace);
581 EXPORT_SYMBOL(cpufreq_gov_userspace_init);
584 static void __exit cpufreq_gov_userspace_exit(void)
586 cpufreq_unregister_governor(&cpufreq_gov_userspace);
587 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
588 cpufreq_sysctl_exit();
592 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
593 MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
594 MODULE_LICENSE ("GPL");
596 fs_initcall(cpufreq_gov_userspace_init);
597 module_exit(cpufreq_gov_userspace_exit);