3 * linux/drivers/cpufreq/cpufreq_userspace.c
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
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/cpufreq.h>
21 #include <linux/cpu.h>
22 #include <linux/types.h>
24 #include <linux/sysfs.h>
25 #include <linux/mutex.h>
27 #include <asm/uaccess.h>
31 * A few values needed by the userspace governor
33 static unsigned int cpu_max_freq
[NR_CPUS
];
34 static unsigned int cpu_min_freq
[NR_CPUS
];
35 static unsigned int cpu_cur_freq
[NR_CPUS
]; /* current CPU freq */
36 static unsigned int cpu_set_freq
[NR_CPUS
]; /* CPU freq desired by userspace */
37 static unsigned int cpu_is_managed
[NR_CPUS
];
39 static DEFINE_MUTEX (userspace_mutex
);
41 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
43 /* keep track of frequency transitions */
45 userspace_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
48 struct cpufreq_freqs
*freq
= data
;
50 dprintk("saving cpu_cur_freq of cpu %u to be %u kHz\n", freq
->cpu
, freq
->new);
51 cpu_cur_freq
[freq
->cpu
] = freq
->new;
56 static struct notifier_block userspace_cpufreq_notifier_block
= {
57 .notifier_call
= userspace_cpufreq_notifier
62 * cpufreq_set - set the CPU frequency
63 * @freq: target frequency in kHz
64 * @cpu: CPU for which the frequency is to be set
66 * Sets the CPU frequency to freq.
68 static int cpufreq_set(unsigned int freq
, struct cpufreq_policy
*policy
)
72 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy
->cpu
, freq
);
75 mutex_lock(&userspace_mutex
);
76 if (!cpu_is_managed
[policy
->cpu
])
79 cpu_set_freq
[policy
->cpu
] = freq
;
81 if (freq
< cpu_min_freq
[policy
->cpu
])
82 freq
= cpu_min_freq
[policy
->cpu
];
83 if (freq
> cpu_max_freq
[policy
->cpu
])
84 freq
= cpu_max_freq
[policy
->cpu
];
87 * We're safe from concurrent calls to ->target() here
88 * as we hold the userspace_mutex lock. If we were calling
89 * cpufreq_driver_target, a deadlock situation might occur:
90 * A: cpufreq_set (lock userspace_mutex) -> cpufreq_driver_target(lock policy->lock)
91 * B: cpufreq_set_policy(lock policy->lock) -> __cpufreq_governor -> cpufreq_governor_userspace (lock userspace_mutex)
93 ret
= __cpufreq_driver_target(policy
, freq
, CPUFREQ_RELATION_L
);
96 mutex_unlock(&userspace_mutex
);
102 /************************** sysfs interface ************************/
103 static ssize_t
show_speed (struct cpufreq_policy
*policy
, char *buf
)
105 return sprintf (buf
, "%u\n", cpu_cur_freq
[policy
->cpu
]);
109 store_speed (struct cpufreq_policy
*policy
, const char *buf
, size_t count
)
111 unsigned int freq
= 0;
114 ret
= sscanf (buf
, "%u", &freq
);
118 cpufreq_set(freq
, policy
);
123 static struct freq_attr freq_attr_scaling_setspeed
=
125 .attr
= { .name
= "scaling_setspeed", .mode
= 0644, .owner
= THIS_MODULE
},
127 .store
= store_speed
,
130 static int cpufreq_governor_userspace(struct cpufreq_policy
*policy
,
133 unsigned int cpu
= policy
->cpu
;
135 case CPUFREQ_GOV_START
:
136 if (!cpu_online(cpu
))
138 BUG_ON(!policy
->cur
);
139 mutex_lock(&userspace_mutex
);
140 cpu_is_managed
[cpu
] = 1;
141 cpu_min_freq
[cpu
] = policy
->min
;
142 cpu_max_freq
[cpu
] = policy
->max
;
143 cpu_cur_freq
[cpu
] = policy
->cur
;
144 cpu_set_freq
[cpu
] = policy
->cur
;
145 sysfs_create_file (&policy
->kobj
, &freq_attr_scaling_setspeed
.attr
);
146 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
]);
147 mutex_unlock(&userspace_mutex
);
149 case CPUFREQ_GOV_STOP
:
150 mutex_lock(&userspace_mutex
);
151 cpu_is_managed
[cpu
] = 0;
152 cpu_min_freq
[cpu
] = 0;
153 cpu_max_freq
[cpu
] = 0;
154 cpu_set_freq
[cpu
] = 0;
155 sysfs_remove_file (&policy
->kobj
, &freq_attr_scaling_setspeed
.attr
);
156 dprintk("managing cpu %u stopped\n", cpu
);
157 mutex_unlock(&userspace_mutex
);
159 case CPUFREQ_GOV_LIMITS
:
160 mutex_lock(&userspace_mutex
);
161 dprintk("limit event for cpu %u: %u - %u kHz,"
162 "currently %u kHz, last set to %u kHz\n",
163 cpu
, policy
->min
, policy
->max
,
164 cpu_cur_freq
[cpu
], cpu_set_freq
[cpu
]);
165 if (policy
->max
< cpu_set_freq
[cpu
]) {
166 __cpufreq_driver_target(policy
, policy
->max
,
169 else if (policy
->min
> cpu_set_freq
[cpu
]) {
170 __cpufreq_driver_target(policy
, policy
->min
,
174 __cpufreq_driver_target(policy
, cpu_set_freq
[cpu
],
177 cpu_min_freq
[cpu
] = policy
->min
;
178 cpu_max_freq
[cpu
] = policy
->max
;
179 cpu_cur_freq
[cpu
] = policy
->cur
;
180 mutex_unlock(&userspace_mutex
);
187 struct cpufreq_governor cpufreq_gov_userspace
= {
189 .governor
= cpufreq_governor_userspace
,
190 .owner
= THIS_MODULE
,
192 EXPORT_SYMBOL(cpufreq_gov_userspace
);
194 static int __init
cpufreq_gov_userspace_init(void)
196 cpufreq_register_notifier(&userspace_cpufreq_notifier_block
, CPUFREQ_TRANSITION_NOTIFIER
);
197 return cpufreq_register_governor(&cpufreq_gov_userspace
);
201 static void __exit
cpufreq_gov_userspace_exit(void)
203 cpufreq_unregister_governor(&cpufreq_gov_userspace
);
204 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block
, CPUFREQ_TRANSITION_NOTIFIER
);
208 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>, Russell King <rmk@arm.linux.org.uk>");
209 MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
210 MODULE_LICENSE ("GPL");
212 fs_initcall(cpufreq_gov_userspace_init
);
213 module_exit(cpufreq_gov_userspace_exit
);