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