]> Git Repo - linux.git/blame - drivers/cpufreq/cpufreq_userspace.c
[CPUFREQ] Fix typo.
[linux.git] / drivers / cpufreq / cpufreq_userspace.c
CommitLineData
c0672860 1
1da177e4
LT
2/*
3 * linux/drivers/cpufreq/cpufreq_userspace.c
4 *
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2004 Dominik Brodowski <[email protected]>
7 *
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.
11 *
12 */
13
1da177e4
LT
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>
153d7f3f 21#include <linux/cpu.h>
1da177e4
LT
22#include <linux/types.h>
23#include <linux/fs.h>
24#include <linux/sysfs.h>
3fc54d37 25#include <linux/mutex.h>
1da177e4
LT
26
27#include <asm/uaccess.h>
28
29
30/**
31 * A few values needed by the userspace governor
32 */
33static unsigned int cpu_max_freq[NR_CPUS];
34static unsigned int cpu_min_freq[NR_CPUS];
35static unsigned int cpu_cur_freq[NR_CPUS]; /* current CPU freq */
36static unsigned int cpu_set_freq[NR_CPUS]; /* CPU freq desired by userspace */
37static unsigned int cpu_is_managed[NR_CPUS];
1da177e4 38
3fc54d37 39static DEFINE_MUTEX (userspace_mutex);
1da177e4
LT
40
41#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_GOVERNOR, "userspace", msg)
42
43/* keep track of frequency transitions */
32ee8c3e 44static int
1da177e4
LT
45userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
46 void *data)
47{
48 struct cpufreq_freqs *freq = data;
49
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;
52
53 return 0;
54}
55
56static struct notifier_block userspace_cpufreq_notifier_block = {
57 .notifier_call = userspace_cpufreq_notifier
58};
59
60
32ee8c3e 61/**
1da177e4
LT
62 * cpufreq_set - set the CPU frequency
63 * @freq: target frequency in kHz
64 * @cpu: CPU for which the frequency is to be set
65 *
66 * Sets the CPU frequency to freq.
67 */
c0672860 68static int cpufreq_set(unsigned int freq, struct cpufreq_policy *policy)
1da177e4
LT
69{
70 int ret = -EINVAL;
71
c0672860 72 dprintk("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
1da177e4 73
153d7f3f 74 lock_cpu_hotplug();
3fc54d37 75 mutex_lock(&userspace_mutex);
c0672860 76 if (!cpu_is_managed[policy->cpu])
1da177e4
LT
77 goto err;
78
c0672860 79 cpu_set_freq[policy->cpu] = freq;
1da177e4 80
c0672860
TR
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];
1da177e4
LT
85
86 /*
87 * We're safe from concurrent calls to ->target() here
3fc54d37 88 * as we hold the userspace_mutex lock. If we were calling
1da177e4 89 * cpufreq_driver_target, a deadlock situation might occur:
3fc54d37 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)
1da177e4 92 */
c0672860 93 ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
1da177e4
LT
94
95 err:
3fc54d37 96 mutex_unlock(&userspace_mutex);
153d7f3f 97 unlock_cpu_hotplug();
1da177e4
LT
98 return ret;
99}
100
101
102/************************** sysfs interface ************************/
103static ssize_t show_speed (struct cpufreq_policy *policy, char *buf)
104{
105 return sprintf (buf, "%u\n", cpu_cur_freq[policy->cpu]);
106}
107
32ee8c3e
DJ
108static ssize_t
109store_speed (struct cpufreq_policy *policy, const char *buf, size_t count)
1da177e4
LT
110{
111 unsigned int freq = 0;
112 unsigned int ret;
113
114 ret = sscanf (buf, "%u", &freq);
115 if (ret != 1)
116 return -EINVAL;
117
c0672860 118 cpufreq_set(freq, policy);
1da177e4
LT
119
120 return count;
121}
122
32ee8c3e 123static struct freq_attr freq_attr_scaling_setspeed =
1da177e4
LT
124{
125 .attr = { .name = "scaling_setspeed", .mode = 0644, .owner = THIS_MODULE },
126 .show = show_speed,
127 .store = store_speed,
128};
129
130static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
131 unsigned int event)
132{
133 unsigned int cpu = policy->cpu;
134 switch (event) {
135 case CPUFREQ_GOV_START:
136 if (!cpu_online(cpu))
137 return -EINVAL;
138 BUG_ON(!policy->cur);
3fc54d37 139 mutex_lock(&userspace_mutex);
32ee8c3e 140 cpu_is_managed[cpu] = 1;
1da177e4
LT
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);
1da177e4 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]);
3fc54d37 147 mutex_unlock(&userspace_mutex);
1da177e4
LT
148 break;
149 case CPUFREQ_GOV_STOP:
3fc54d37 150 mutex_lock(&userspace_mutex);
1da177e4
LT
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);
3fc54d37 157 mutex_unlock(&userspace_mutex);
1da177e4
LT
158 break;
159 case CPUFREQ_GOV_LIMITS:
3fc54d37 160 mutex_lock(&userspace_mutex);
c0672860
TR
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]);
1da177e4 165 if (policy->max < cpu_set_freq[cpu]) {
c0672860
TR
166 __cpufreq_driver_target(policy, policy->max,
167 CPUFREQ_RELATION_H);
168 }
169 else if (policy->min > cpu_set_freq[cpu]) {
170 __cpufreq_driver_target(policy, policy->min,
171 CPUFREQ_RELATION_L);
1da177e4 172 }
c0672860
TR
173 else {
174 __cpufreq_driver_target(policy, cpu_set_freq[cpu],
175 CPUFREQ_RELATION_L);
176 }
177 cpu_min_freq[cpu] = policy->min;
178 cpu_max_freq[cpu] = policy->max;
179 cpu_cur_freq[cpu] = policy->cur;
3fc54d37 180 mutex_unlock(&userspace_mutex);
1da177e4
LT
181 break;
182 }
183 return 0;
184}
185
186
187struct cpufreq_governor cpufreq_gov_userspace = {
188 .name = "userspace",
189 .governor = cpufreq_governor_userspace,
190 .owner = THIS_MODULE,
191};
192EXPORT_SYMBOL(cpufreq_gov_userspace);
193
194static int __init cpufreq_gov_userspace_init(void)
195{
196 cpufreq_register_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
197 return cpufreq_register_governor(&cpufreq_gov_userspace);
198}
199
200
201static void __exit cpufreq_gov_userspace_exit(void)
202{
203 cpufreq_unregister_governor(&cpufreq_gov_userspace);
204 cpufreq_unregister_notifier(&userspace_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
205}
206
207
208MODULE_AUTHOR ("Dominik Brodowski <[email protected]>, Russell King <[email protected]>");
209MODULE_DESCRIPTION ("CPUfreq policy governor 'userspace'");
210MODULE_LICENSE ("GPL");
211
212fs_initcall(cpufreq_gov_userspace_init);
213module_exit(cpufreq_gov_userspace_exit);
This page took 0.220639 seconds and 4 git commands to generate.