]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/cpufreq/freq_table.c | |
3 | * | |
4 | * Copyright (C) 2002 - 2003 Dominik Brodowski | |
4f743694 DB |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
1da177e4 LT |
10 | */ |
11 | ||
db701151 VK |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
1da177e4 | 14 | #include <linux/cpufreq.h> |
5ff0a268 | 15 | #include <linux/module.h> |
1da177e4 | 16 | |
1da177e4 LT |
17 | /********************************************************************* |
18 | * FREQUENCY TABLE HELPERS * | |
19 | *********************************************************************/ | |
20 | ||
44139ed4 VK |
21 | bool policy_has_boost_freq(struct cpufreq_policy *policy) |
22 | { | |
23 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; | |
24 | ||
25 | if (!table) | |
26 | return false; | |
27 | ||
28 | cpufreq_for_each_valid_entry(pos, table) | |
29 | if (pos->flags & CPUFREQ_BOOST_FREQ) | |
30 | return true; | |
31 | ||
32 | return false; | |
33 | } | |
34 | EXPORT_SYMBOL_GPL(policy_has_boost_freq); | |
35 | ||
1da177e4 LT |
36 | int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy, |
37 | struct cpufreq_frequency_table *table) | |
38 | { | |
041526f9 | 39 | struct cpufreq_frequency_table *pos; |
1da177e4 LT |
40 | unsigned int min_freq = ~0; |
41 | unsigned int max_freq = 0; | |
041526f9 | 42 | unsigned int freq; |
1da177e4 | 43 | |
041526f9 SK |
44 | cpufreq_for_each_valid_entry(pos, table) { |
45 | freq = pos->frequency; | |
1da177e4 | 46 | |
6f19efc0 | 47 | if (!cpufreq_boost_enabled() |
041526f9 | 48 | && (pos->flags & CPUFREQ_BOOST_FREQ)) |
6f19efc0 LM |
49 | continue; |
50 | ||
041526f9 | 51 | pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq); |
1da177e4 LT |
52 | if (freq < min_freq) |
53 | min_freq = freq; | |
54 | if (freq > max_freq) | |
55 | max_freq = freq; | |
56 | } | |
57 | ||
58 | policy->min = policy->cpuinfo.min_freq = min_freq; | |
59 | policy->max = policy->cpuinfo.max_freq = max_freq; | |
60 | ||
61 | if (policy->min == ~0) | |
62 | return -EINVAL; | |
63 | else | |
64 | return 0; | |
65 | } | |
66 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo); | |
67 | ||
68 | ||
69 | int cpufreq_frequency_table_verify(struct cpufreq_policy *policy, | |
70 | struct cpufreq_frequency_table *table) | |
71 | { | |
041526f9 SK |
72 | struct cpufreq_frequency_table *pos; |
73 | unsigned int freq, next_larger = ~0; | |
77db50c4 | 74 | bool found = false; |
1da177e4 | 75 | |
2d06d8c4 | 76 | pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n", |
e08f5f5b | 77 | policy->min, policy->max, policy->cpu); |
1da177e4 | 78 | |
be49e346 | 79 | cpufreq_verify_within_cpu_limits(policy); |
1da177e4 | 80 | |
041526f9 SK |
81 | cpufreq_for_each_valid_entry(pos, table) { |
82 | freq = pos->frequency; | |
83 | ||
77db50c4 VK |
84 | if ((freq >= policy->min) && (freq <= policy->max)) { |
85 | found = true; | |
86 | break; | |
87 | } | |
88 | ||
89 | if ((next_larger > freq) && (freq > policy->max)) | |
1da177e4 LT |
90 | next_larger = freq; |
91 | } | |
92 | ||
77db50c4 | 93 | if (!found) { |
1da177e4 | 94 | policy->max = next_larger; |
be49e346 | 95 | cpufreq_verify_within_cpu_limits(policy); |
77db50c4 | 96 | } |
1da177e4 | 97 | |
2d06d8c4 | 98 | pr_debug("verification lead to (%u - %u kHz) for cpu %u\n", |
e08f5f5b | 99 | policy->min, policy->max, policy->cpu); |
1da177e4 LT |
100 | |
101 | return 0; | |
102 | } | |
103 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify); | |
104 | ||
18434512 | 105 | /* |
e0b3165b VK |
106 | * Generic routine to verify policy & frequency table, requires driver to set |
107 | * policy->freq_table prior to it. | |
18434512 VK |
108 | */ |
109 | int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy) | |
110 | { | |
111 | struct cpufreq_frequency_table *table = | |
112 | cpufreq_frequency_get_table(policy->cpu); | |
113 | if (!table) | |
114 | return -ENODEV; | |
115 | ||
116 | return cpufreq_frequency_table_verify(policy, table); | |
117 | } | |
118 | EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify); | |
1da177e4 LT |
119 | |
120 | int cpufreq_frequency_table_target(struct cpufreq_policy *policy, | |
121 | struct cpufreq_frequency_table *table, | |
122 | unsigned int target_freq, | |
123 | unsigned int relation, | |
124 | unsigned int *index) | |
125 | { | |
484944a5 | 126 | struct cpufreq_frequency_table optimal = { |
50701588 | 127 | .driver_data = ~0, |
484944a5 DJ |
128 | .frequency = 0, |
129 | }; | |
130 | struct cpufreq_frequency_table suboptimal = { | |
50701588 | 131 | .driver_data = ~0, |
484944a5 DJ |
132 | .frequency = 0, |
133 | }; | |
041526f9 | 134 | struct cpufreq_frequency_table *pos; |
5b0c0b16 | 135 | unsigned int freq, diff, i = 0; |
1da177e4 | 136 | |
2d06d8c4 | 137 | pr_debug("request for target %u kHz (relation: %u) for cpu %u\n", |
e08f5f5b | 138 | target_freq, relation, policy->cpu); |
1da177e4 LT |
139 | |
140 | switch (relation) { | |
141 | case CPUFREQ_RELATION_H: | |
1da177e4 LT |
142 | suboptimal.frequency = ~0; |
143 | break; | |
144 | case CPUFREQ_RELATION_L: | |
5b0c0b16 | 145 | case CPUFREQ_RELATION_C: |
1da177e4 | 146 | optimal.frequency = ~0; |
1da177e4 LT |
147 | break; |
148 | } | |
149 | ||
041526f9 SK |
150 | cpufreq_for_each_valid_entry(pos, table) { |
151 | freq = pos->frequency; | |
152 | ||
153 | i = pos - table; | |
1da177e4 LT |
154 | if ((freq < policy->min) || (freq > policy->max)) |
155 | continue; | |
1e498856 SK |
156 | if (freq == target_freq) { |
157 | optimal.driver_data = i; | |
158 | break; | |
159 | } | |
97acec55 | 160 | switch (relation) { |
1da177e4 | 161 | case CPUFREQ_RELATION_H: |
1e498856 | 162 | if (freq < target_freq) { |
1da177e4 LT |
163 | if (freq >= optimal.frequency) { |
164 | optimal.frequency = freq; | |
50701588 | 165 | optimal.driver_data = i; |
1da177e4 LT |
166 | } |
167 | } else { | |
168 | if (freq <= suboptimal.frequency) { | |
169 | suboptimal.frequency = freq; | |
50701588 | 170 | suboptimal.driver_data = i; |
1da177e4 LT |
171 | } |
172 | } | |
173 | break; | |
174 | case CPUFREQ_RELATION_L: | |
1e498856 | 175 | if (freq > target_freq) { |
1da177e4 LT |
176 | if (freq <= optimal.frequency) { |
177 | optimal.frequency = freq; | |
50701588 | 178 | optimal.driver_data = i; |
1da177e4 LT |
179 | } |
180 | } else { | |
181 | if (freq >= suboptimal.frequency) { | |
182 | suboptimal.frequency = freq; | |
50701588 | 183 | suboptimal.driver_data = i; |
1da177e4 LT |
184 | } |
185 | } | |
186 | break; | |
5b0c0b16 SK |
187 | case CPUFREQ_RELATION_C: |
188 | diff = abs(freq - target_freq); | |
189 | if (diff < optimal.frequency || | |
190 | (diff == optimal.frequency && | |
191 | freq > table[optimal.driver_data].frequency)) { | |
192 | optimal.frequency = diff; | |
193 | optimal.driver_data = i; | |
194 | } | |
195 | break; | |
1da177e4 LT |
196 | } |
197 | } | |
50701588 VK |
198 | if (optimal.driver_data > i) { |
199 | if (suboptimal.driver_data > i) | |
1da177e4 | 200 | return -EINVAL; |
50701588 | 201 | *index = suboptimal.driver_data; |
1da177e4 | 202 | } else |
50701588 | 203 | *index = optimal.driver_data; |
1da177e4 | 204 | |
ae87f10f VK |
205 | pr_debug("target index is %u, freq is:%u kHz\n", *index, |
206 | table[*index].frequency); | |
1da177e4 LT |
207 | |
208 | return 0; | |
209 | } | |
210 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target); | |
211 | ||
d3916691 VK |
212 | int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, |
213 | unsigned int freq) | |
214 | { | |
041526f9 | 215 | struct cpufreq_frequency_table *pos, *table; |
d3916691 VK |
216 | |
217 | table = cpufreq_frequency_get_table(policy->cpu); | |
218 | if (unlikely(!table)) { | |
219 | pr_debug("%s: Unable to find frequency table\n", __func__); | |
220 | return -ENOENT; | |
221 | } | |
222 | ||
041526f9 SK |
223 | cpufreq_for_each_valid_entry(pos, table) |
224 | if (pos->frequency == freq) | |
225 | return pos - table; | |
d3916691 VK |
226 | |
227 | return -EINVAL; | |
228 | } | |
229 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index); | |
230 | ||
1da177e4 | 231 | /** |
e32d22f7 | 232 | * show_available_freqs - show available frequencies for the specified CPU |
1da177e4 | 233 | */ |
6f19efc0 LM |
234 | static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf, |
235 | bool show_boost) | |
1da177e4 | 236 | { |
1da177e4 | 237 | ssize_t count = 0; |
041526f9 | 238 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; |
1da177e4 | 239 | |
e0b3165b | 240 | if (!table) |
1da177e4 LT |
241 | return -ENODEV; |
242 | ||
041526f9 | 243 | cpufreq_for_each_valid_entry(pos, table) { |
6f19efc0 LM |
244 | /* |
245 | * show_boost = true and driver_data = BOOST freq | |
246 | * display BOOST freqs | |
247 | * | |
248 | * show_boost = false and driver_data = BOOST freq | |
249 | * show_boost = true and driver_data != BOOST freq | |
250 | * continue - do not display anything | |
251 | * | |
252 | * show_boost = false and driver_data != BOOST freq | |
253 | * display NON BOOST freqs | |
254 | */ | |
041526f9 | 255 | if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ)) |
6f19efc0 LM |
256 | continue; |
257 | ||
041526f9 | 258 | count += sprintf(&buf[count], "%d ", pos->frequency); |
1da177e4 LT |
259 | } |
260 | count += sprintf(&buf[count], "\n"); | |
261 | ||
262 | return count; | |
263 | ||
264 | } | |
265 | ||
6f19efc0 LM |
266 | #define cpufreq_attr_available_freq(_name) \ |
267 | struct freq_attr cpufreq_freq_attr_##_name##_freqs = \ | |
268 | __ATTR_RO(_name##_frequencies) | |
269 | ||
270 | /** | |
271 | * show_scaling_available_frequencies - show available normal frequencies for | |
272 | * the specified CPU | |
273 | */ | |
274 | static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy, | |
275 | char *buf) | |
276 | { | |
277 | return show_available_freqs(policy, buf, false); | |
278 | } | |
279 | cpufreq_attr_available_freq(scaling_available); | |
1da177e4 LT |
280 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs); |
281 | ||
6f19efc0 LM |
282 | /** |
283 | * show_available_boost_freqs - show available boost frequencies for | |
284 | * the specified CPU | |
285 | */ | |
286 | static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy, | |
287 | char *buf) | |
288 | { | |
289 | return show_available_freqs(policy, buf, true); | |
290 | } | |
291 | cpufreq_attr_available_freq(scaling_boost); | |
292 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs); | |
293 | ||
18434512 VK |
294 | struct freq_attr *cpufreq_generic_attr[] = { |
295 | &cpufreq_freq_attr_scaling_available_freqs, | |
6f19efc0 LM |
296 | #ifdef CONFIG_CPU_FREQ_BOOST_SW |
297 | &cpufreq_freq_attr_scaling_boost_freqs, | |
298 | #endif | |
18434512 VK |
299 | NULL, |
300 | }; | |
301 | EXPORT_SYMBOL_GPL(cpufreq_generic_attr); | |
302 | ||
27047a60 VK |
303 | int cpufreq_table_validate_and_show(struct cpufreq_policy *policy, |
304 | struct cpufreq_frequency_table *table) | |
305 | { | |
306 | int ret = cpufreq_frequency_table_cpuinfo(policy, table); | |
307 | ||
308 | if (!ret) | |
e0b3165b | 309 | policy->freq_table = table; |
27047a60 VK |
310 | |
311 | return ret; | |
312 | } | |
313 | EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show); | |
314 | ||
97acec55 DJ |
315 | MODULE_AUTHOR("Dominik Brodowski <[email protected]>"); |
316 | MODULE_DESCRIPTION("CPUfreq frequency table helpers"); | |
317 | MODULE_LICENSE("GPL"); |