Commit | Line | Data |
---|---|---|
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 <linux@brodo.de> | |
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 | ||
db701151 VK |
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
15 | ||
1da177e4 LT |
16 | #include <linux/kernel.h> |
17 | #include <linux/module.h> | |
18 | #include <linux/smp.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/spinlock.h> | |
21 | #include <linux/interrupt.h> | |
22 | #include <linux/cpufreq.h> | |
153d7f3f | 23 | #include <linux/cpu.h> |
1da177e4 LT |
24 | #include <linux/types.h> |
25 | #include <linux/fs.h> | |
26 | #include <linux/sysfs.h> | |
3fc54d37 | 27 | #include <linux/mutex.h> |
1da177e4 | 28 | |
1da177e4 LT |
29 | /** |
30 | * A few values needed by the userspace governor | |
31 | */ | |
b38868aa MT |
32 | static DEFINE_PER_CPU(unsigned int, cpu_max_freq); |
33 | static DEFINE_PER_CPU(unsigned int, cpu_min_freq); | |
34 | static DEFINE_PER_CPU(unsigned int, cpu_cur_freq); /* current CPU freq */ | |
35 | static DEFINE_PER_CPU(unsigned int, cpu_set_freq); /* CPU freq desired by | |
36 | userspace */ | |
37 | static DEFINE_PER_CPU(unsigned int, cpu_is_managed); | |
1da177e4 | 38 | |
1bceb8d1 | 39 | static DEFINE_MUTEX(userspace_mutex); |
c7f652e0 | 40 | static int cpus_using_userspace_governor; |
1da177e4 | 41 | |
1da177e4 | 42 | /* keep track of frequency transitions */ |
32ee8c3e | 43 | static int |
1da177e4 | 44 | userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val, |
1bceb8d1 | 45 | void *data) |
1da177e4 | 46 | { |
1bceb8d1 | 47 | struct cpufreq_freqs *freq = data; |
1da177e4 | 48 | |
b38868aa | 49 | if (!per_cpu(cpu_is_managed, freq->cpu)) |
c7f652e0 VP |
50 | return 0; |
51 | ||
226dd019 AM |
52 | if (val == CPUFREQ_POSTCHANGE) { |
53 | pr_debug("saving cpu_cur_freq of cpu %u to be %u kHz\n", | |
54 | freq->cpu, freq->new); | |
55 | per_cpu(cpu_cur_freq, freq->cpu) = freq->new; | |
56 | } | |
1da177e4 | 57 | |
1bceb8d1 | 58 | return 0; |
1da177e4 LT |
59 | } |
60 | ||
61 | static struct notifier_block userspace_cpufreq_notifier_block = { | |
1bceb8d1 | 62 | .notifier_call = userspace_cpufreq_notifier |
1da177e4 LT |
63 | }; |
64 | ||
65 | ||
32ee8c3e | 66 | /** |
1da177e4 | 67 | * cpufreq_set - set the CPU frequency |
9e76988e | 68 | * @policy: pointer to policy struct where freq is being set |
1da177e4 | 69 | * @freq: target frequency in kHz |
1da177e4 LT |
70 | * |
71 | * Sets the CPU frequency to freq. | |
72 | */ | |
9e76988e | 73 | static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq) |
1da177e4 LT |
74 | { |
75 | int ret = -EINVAL; | |
76 | ||
2d06d8c4 | 77 | pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq); |
1da177e4 | 78 | |
3fc54d37 | 79 | mutex_lock(&userspace_mutex); |
b38868aa | 80 | if (!per_cpu(cpu_is_managed, policy->cpu)) |
1da177e4 LT |
81 | goto err; |
82 | ||
b38868aa | 83 | per_cpu(cpu_set_freq, policy->cpu) = freq; |
1da177e4 | 84 | |
b38868aa MT |
85 | if (freq < per_cpu(cpu_min_freq, policy->cpu)) |
86 | freq = per_cpu(cpu_min_freq, policy->cpu); | |
87 | if (freq > per_cpu(cpu_max_freq, policy->cpu)) | |
88 | freq = per_cpu(cpu_max_freq, policy->cpu); | |
1da177e4 LT |
89 | |
90 | /* | |
91 | * We're safe from concurrent calls to ->target() here | |
3fc54d37 | 92 | * as we hold the userspace_mutex lock. If we were calling |
1da177e4 | 93 | * cpufreq_driver_target, a deadlock situation might occur: |
1bceb8d1 DJ |
94 | * A: cpufreq_set (lock userspace_mutex) -> |
95 | * cpufreq_driver_target(lock policy->lock) | |
96 | * B: cpufreq_set_policy(lock policy->lock) -> | |
97 | * __cpufreq_governor -> | |
98 | * cpufreq_governor_userspace (lock userspace_mutex) | |
1da177e4 | 99 | */ |
c0672860 | 100 | ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L); |
1da177e4 LT |
101 | |
102 | err: | |
3fc54d37 | 103 | mutex_unlock(&userspace_mutex); |
1da177e4 LT |
104 | return ret; |
105 | } | |
106 | ||
107 | ||
9e76988e | 108 | static ssize_t show_speed(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 109 | { |
b38868aa | 110 | return sprintf(buf, "%u\n", per_cpu(cpu_cur_freq, policy->cpu)); |
1da177e4 LT |
111 | } |
112 | ||
1da177e4 LT |
113 | static int cpufreq_governor_userspace(struct cpufreq_policy *policy, |
114 | unsigned int event) | |
115 | { | |
116 | unsigned int cpu = policy->cpu; | |
914f7c31 JG |
117 | int rc = 0; |
118 | ||
1da177e4 LT |
119 | switch (event) { |
120 | case CPUFREQ_GOV_START: | |
1da177e4 | 121 | BUG_ON(!policy->cur); |
3fc54d37 | 122 | mutex_lock(&userspace_mutex); |
914f7c31 | 123 | |
c7f652e0 VP |
124 | if (cpus_using_userspace_governor == 0) { |
125 | cpufreq_register_notifier( | |
126 | &userspace_cpufreq_notifier_block, | |
127 | CPUFREQ_TRANSITION_NOTIFIER); | |
128 | } | |
129 | cpus_using_userspace_governor++; | |
130 | ||
b38868aa MT |
131 | per_cpu(cpu_is_managed, cpu) = 1; |
132 | per_cpu(cpu_min_freq, cpu) = policy->min; | |
133 | per_cpu(cpu_max_freq, cpu) = policy->max; | |
134 | per_cpu(cpu_cur_freq, cpu) = policy->cur; | |
135 | per_cpu(cpu_set_freq, cpu) = policy->cur; | |
2d06d8c4 | 136 | pr_debug("managing cpu %u started " |
b38868aa MT |
137 | "(%u - %u kHz, currently %u kHz)\n", |
138 | cpu, | |
139 | per_cpu(cpu_min_freq, cpu), | |
140 | per_cpu(cpu_max_freq, cpu), | |
141 | per_cpu(cpu_cur_freq, cpu)); | |
9e76988e | 142 | |
3fc54d37 | 143 | mutex_unlock(&userspace_mutex); |
1da177e4 LT |
144 | break; |
145 | case CPUFREQ_GOV_STOP: | |
3fc54d37 | 146 | mutex_lock(&userspace_mutex); |
c7f652e0 VP |
147 | cpus_using_userspace_governor--; |
148 | if (cpus_using_userspace_governor == 0) { | |
149 | cpufreq_unregister_notifier( | |
150 | &userspace_cpufreq_notifier_block, | |
151 | CPUFREQ_TRANSITION_NOTIFIER); | |
152 | } | |
153 | ||
b38868aa MT |
154 | per_cpu(cpu_is_managed, cpu) = 0; |
155 | per_cpu(cpu_min_freq, cpu) = 0; | |
156 | per_cpu(cpu_max_freq, cpu) = 0; | |
157 | per_cpu(cpu_set_freq, cpu) = 0; | |
2d06d8c4 | 158 | pr_debug("managing cpu %u stopped\n", cpu); |
3fc54d37 | 159 | mutex_unlock(&userspace_mutex); |
1da177e4 LT |
160 | break; |
161 | case CPUFREQ_GOV_LIMITS: | |
3fc54d37 | 162 | mutex_lock(&userspace_mutex); |
2d06d8c4 | 163 | pr_debug("limit event for cpu %u: %u - %u kHz, " |
c0672860 TR |
164 | "currently %u kHz, last set to %u kHz\n", |
165 | cpu, policy->min, policy->max, | |
b38868aa MT |
166 | per_cpu(cpu_cur_freq, cpu), |
167 | per_cpu(cpu_set_freq, cpu)); | |
168 | if (policy->max < per_cpu(cpu_set_freq, cpu)) { | |
c0672860 TR |
169 | __cpufreq_driver_target(policy, policy->max, |
170 | CPUFREQ_RELATION_H); | |
b38868aa | 171 | } else if (policy->min > per_cpu(cpu_set_freq, cpu)) { |
c0672860 TR |
172 | __cpufreq_driver_target(policy, policy->min, |
173 | CPUFREQ_RELATION_L); | |
b38868aa MT |
174 | } else { |
175 | __cpufreq_driver_target(policy, | |
176 | per_cpu(cpu_set_freq, cpu), | |
c0672860 TR |
177 | CPUFREQ_RELATION_L); |
178 | } | |
b38868aa MT |
179 | per_cpu(cpu_min_freq, cpu) = policy->min; |
180 | per_cpu(cpu_max_freq, cpu) = policy->max; | |
181 | per_cpu(cpu_cur_freq, cpu) = policy->cur; | |
3fc54d37 | 182 | mutex_unlock(&userspace_mutex); |
1da177e4 LT |
183 | break; |
184 | } | |
914f7c31 | 185 | return rc; |
1da177e4 LT |
186 | } |
187 | ||
188 | ||
c4d14bc0 SW |
189 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
190 | static | |
191 | #endif | |
1da177e4 LT |
192 | struct cpufreq_governor cpufreq_gov_userspace = { |
193 | .name = "userspace", | |
194 | .governor = cpufreq_governor_userspace, | |
9e76988e VP |
195 | .store_setspeed = cpufreq_set, |
196 | .show_setspeed = show_speed, | |
1da177e4 LT |
197 | .owner = THIS_MODULE, |
198 | }; | |
1da177e4 LT |
199 | |
200 | static int __init cpufreq_gov_userspace_init(void) | |
201 | { | |
1da177e4 LT |
202 | return cpufreq_register_governor(&cpufreq_gov_userspace); |
203 | } | |
204 | ||
205 | ||
206 | static void __exit cpufreq_gov_userspace_exit(void) | |
207 | { | |
208 | cpufreq_unregister_governor(&cpufreq_gov_userspace); | |
1da177e4 LT |
209 | } |
210 | ||
211 | ||
1bceb8d1 DJ |
212 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, " |
213 | "Russell King <rmk@arm.linux.org.uk>"); | |
214 | MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'"); | |
215 | MODULE_LICENSE("GPL"); | |
1da177e4 | 216 | |
6915719b | 217 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE |
1da177e4 | 218 | fs_initcall(cpufreq_gov_userspace_init); |
6915719b JW |
219 | #else |
220 | module_init(cpufreq_gov_userspace_init); | |
221 | #endif | |
1da177e4 | 222 | module_exit(cpufreq_gov_userspace_exit); |