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 /* 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.
89 if (cpu_is_managed[freq->cpu] && (val == CPUFREQ_RESUMECHANGE))
92 cpu_cur_freq[freq->cpu] = freq->new;
97 static struct notifier_block userspace_cpufreq_notifier_block = {
98 .notifier_call = userspace_cpufreq_notifier
103 * cpufreq_set - set the CPU frequency
104 * @freq: target frequency in kHz
105 * @cpu: CPU for which the frequency is to be set
107 * Sets the CPU frequency to freq.
109 int cpufreq_set(unsigned int freq, unsigned int cpu)
113 down(&userspace_sem);
114 if (!cpu_is_managed[cpu])
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,
136 EXPORT_SYMBOL_GPL(cpufreq_set);
140 * cpufreq_setmax - set the CPU to the maximum frequency
141 * @cpu - affected cpu;
143 * Sets the CPU frequency to the maximum frequency supported by
146 int cpufreq_setmax(unsigned int cpu)
148 if (!cpu_is_managed[cpu] || !cpu_online(cpu))
150 return cpufreq_set(cpu_max_freq[cpu], cpu);
152 EXPORT_SYMBOL_GPL(cpufreq_setmax);
155 #ifdef CONFIG_CPU_FREQ_24_API
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
159 static unsigned int warning_print = 0;
161 /*********************** cpufreq_sysctl interface ********************/
163 cpufreq_procctl(ctl_table *ctl, int write, struct file *filp,
164 void __user *buffer, size_t *lenp, loff_t *ppos)
167 int cpu = (long) ctl->extra1;
168 unsigned int len, left = *lenp;
170 if (!left || (*ppos && !write) || !cpu_online(cpu)) {
175 if (!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");
186 if (left > sizeof(buf))
188 if (copy_from_user(buf, buffer, left))
190 buf[sizeof(buf) - 1] = '\0';
192 freq = simple_strtoul(buf, &p, 0);
193 cpufreq_set(freq, cpu);
195 len = sprintf(buf, "%d\n", cpufreq_get(cpu));
198 if (copy_to_user(buffer, buf, len))
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)
212 int cpu = (long) table->extra1;
214 if (!cpu_online(cpu))
217 if (!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");
224 if (oldval && oldlenp) {
227 if (get_user(oldlen, oldlenp))
230 if (oldlen != sizeof(unsigned int))
233 if (put_user(cpufreq_get(cpu), (unsigned int __user *)oldval) ||
234 put_user(sizeof(unsigned int), oldlenp))
237 if (newval && newlen) {
240 if (newlen != sizeof(unsigned int))
243 if (get_user(freq, (unsigned int __user *)newval))
246 cpufreq_set(freq, cpu);
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);
255 CTL_TABLE_CPU_VARS(1);
258 CTL_TABLE_CPU_VARS(2);
261 CTL_TABLE_CPU_VARS(3);
264 CTL_TABLE_CPU_VARS(4);
267 CTL_TABLE_CPU_VARS(5);
270 CTL_TABLE_CPU_VARS(6);
273 CTL_TABLE_CPU_VARS(7);
276 CTL_TABLE_CPU_VARS(8);
279 CTL_TABLE_CPU_VARS(9);
282 CTL_TABLE_CPU_VARS(10);
285 CTL_TABLE_CPU_VARS(11);
288 CTL_TABLE_CPU_VARS(12);
291 CTL_TABLE_CPU_VARS(13);
294 CTL_TABLE_CPU_VARS(14);
297 CTL_TABLE_CPU_VARS(15);
300 CTL_TABLE_CPU_VARS(16);
303 CTL_TABLE_CPU_VARS(17);
306 CTL_TABLE_CPU_VARS(18);
309 CTL_TABLE_CPU_VARS(19);
312 CTL_TABLE_CPU_VARS(20);
315 CTL_TABLE_CPU_VARS(21);
318 CTL_TABLE_CPU_VARS(22);
321 CTL_TABLE_CPU_VARS(23);
324 CTL_TABLE_CPU_VARS(24);
327 CTL_TABLE_CPU_VARS(25);
330 CTL_TABLE_CPU_VARS(26);
333 CTL_TABLE_CPU_VARS(27);
336 CTL_TABLE_CPU_VARS(28);
339 CTL_TABLE_CPU_VARS(29);
342 CTL_TABLE_CPU_VARS(30);
345 CTL_TABLE_CPU_VARS(31);
348 #error please extend CPU enumeration
351 /* due to NR_CPUS tweaking, a lot of if/endifs are required, sorry */
352 static ctl_table ctl_cpu_table[NR_CPUS + 1] = {
448 #error please extend CPU enumeration
455 static ctl_table ctl_cpu[2] = {
460 .child = ctl_cpu_table,
467 struct ctl_table_header *cpufreq_sysctl_table;
469 static inline void cpufreq_sysctl_init(void)
471 cpufreq_sysctl_table = register_sysctl_table(ctl_cpu, 0);
474 static inline void cpufreq_sysctl_exit(void)
476 unregister_sysctl_table(cpufreq_sysctl_table);
480 #define cpufreq_sysctl_init() do {} while(0)
481 #define cpufreq_sysctl_exit() do {} while(0)
482 #endif /* CONFIG_CPU_FREQ_24API */
485 /************************** sysfs interface ************************/
486 static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
488 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
492 store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
494 unsigned int freq = 0;
497 ret = sscanf (buf, "%u", &freq);
501 cpufreq_set(freq, policy->cpu);
506 static struct freq_attr freq_attr_scaling_setspeed = {
507 .attr = { .name = "scaling_setspeed", .mode = 0644 },
509 .store = store_speed,
512 static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
515 unsigned int cpu = policy->cpu;
517 case CPUFREQ_GOV_START:
518 if ((!cpu_online(cpu)) || (!try_module_get(THIS_MODULE)))
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 (¤t_policy[cpu], policy, sizeof(struct cpufreq_policy));
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);
537 module_put(THIS_MODULE);
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(¤t_policy[cpu], policy->max,
546 else if (policy->min > cpu_cur_freq[cpu])
547 __cpufreq_driver_target(¤t_policy[cpu], policy->min,
550 __cpufreq_driver_target(¤t_policy[cpu], cpu_cur_freq[cpu],
552 memcpy (¤t_policy[cpu], policy, sizeof(struct cpufreq_policy));
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 __init cpufreq_gov_userspace_init(void)
569 cpufreq_sysctl_init();
570 cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
571 return cpufreq_register_governor(&cpufreq_gov_userspace);
575 static void __exit cpufreq_gov_userspace_exit(void)
577 cpufreq_unregister_governor(&cpufreq_gov_userspace);
578 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
579 cpufreq_sysctl_exit();
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");
587 fs_initcall(cpufreq_gov_userspace_init);
588 module_exit(cpufreq_gov_userspace_exit);