]>
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 | } | |
1da177e4 LT |
66 | |
67 | int cpufreq_frequency_table_verify(struct cpufreq_policy *policy, | |
68 | struct cpufreq_frequency_table *table) | |
69 | { | |
041526f9 SK |
70 | struct cpufreq_frequency_table *pos; |
71 | unsigned int freq, next_larger = ~0; | |
77db50c4 | 72 | bool found = false; |
1da177e4 | 73 | |
2d06d8c4 | 74 | pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n", |
e08f5f5b | 75 | policy->min, policy->max, policy->cpu); |
1da177e4 | 76 | |
be49e346 | 77 | cpufreq_verify_within_cpu_limits(policy); |
1da177e4 | 78 | |
041526f9 SK |
79 | cpufreq_for_each_valid_entry(pos, table) { |
80 | freq = pos->frequency; | |
81 | ||
77db50c4 VK |
82 | if ((freq >= policy->min) && (freq <= policy->max)) { |
83 | found = true; | |
84 | break; | |
85 | } | |
86 | ||
87 | if ((next_larger > freq) && (freq > policy->max)) | |
1da177e4 LT |
88 | next_larger = freq; |
89 | } | |
90 | ||
77db50c4 | 91 | if (!found) { |
1da177e4 | 92 | policy->max = next_larger; |
be49e346 | 93 | cpufreq_verify_within_cpu_limits(policy); |
77db50c4 | 94 | } |
1da177e4 | 95 | |
2d06d8c4 | 96 | pr_debug("verification lead to (%u - %u kHz) for cpu %u\n", |
e08f5f5b | 97 | policy->min, policy->max, policy->cpu); |
1da177e4 LT |
98 | |
99 | return 0; | |
100 | } | |
101 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify); | |
102 | ||
18434512 | 103 | /* |
e0b3165b VK |
104 | * Generic routine to verify policy & frequency table, requires driver to set |
105 | * policy->freq_table prior to it. | |
18434512 VK |
106 | */ |
107 | int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy) | |
108 | { | |
f8bfc116 | 109 | if (!policy->freq_table) |
18434512 VK |
110 | return -ENODEV; |
111 | ||
f8bfc116 | 112 | return cpufreq_frequency_table_verify(policy, policy->freq_table); |
18434512 VK |
113 | } |
114 | EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify); | |
1da177e4 | 115 | |
da0c6dc0 VK |
116 | int cpufreq_table_index_unsorted(struct cpufreq_policy *policy, |
117 | unsigned int target_freq, | |
118 | unsigned int relation) | |
1da177e4 | 119 | { |
484944a5 | 120 | struct cpufreq_frequency_table optimal = { |
50701588 | 121 | .driver_data = ~0, |
484944a5 DJ |
122 | .frequency = 0, |
123 | }; | |
124 | struct cpufreq_frequency_table suboptimal = { | |
50701588 | 125 | .driver_data = ~0, |
484944a5 DJ |
126 | .frequency = 0, |
127 | }; | |
041526f9 | 128 | struct cpufreq_frequency_table *pos; |
7ab4aabb | 129 | struct cpufreq_frequency_table *table = policy->freq_table; |
5b0c0b16 | 130 | unsigned int freq, diff, i = 0; |
d218ed77 | 131 | int index; |
1da177e4 | 132 | |
2d06d8c4 | 133 | pr_debug("request for target %u kHz (relation: %u) for cpu %u\n", |
e08f5f5b | 134 | target_freq, relation, policy->cpu); |
1da177e4 LT |
135 | |
136 | switch (relation) { | |
137 | case CPUFREQ_RELATION_H: | |
1da177e4 LT |
138 | suboptimal.frequency = ~0; |
139 | break; | |
140 | case CPUFREQ_RELATION_L: | |
5b0c0b16 | 141 | case CPUFREQ_RELATION_C: |
1da177e4 | 142 | optimal.frequency = ~0; |
1da177e4 LT |
143 | break; |
144 | } | |
145 | ||
041526f9 SK |
146 | cpufreq_for_each_valid_entry(pos, table) { |
147 | freq = pos->frequency; | |
148 | ||
149 | i = pos - table; | |
1da177e4 LT |
150 | if ((freq < policy->min) || (freq > policy->max)) |
151 | continue; | |
1e498856 SK |
152 | if (freq == target_freq) { |
153 | optimal.driver_data = i; | |
154 | break; | |
155 | } | |
97acec55 | 156 | switch (relation) { |
1da177e4 | 157 | case CPUFREQ_RELATION_H: |
1e498856 | 158 | if (freq < target_freq) { |
1da177e4 LT |
159 | if (freq >= optimal.frequency) { |
160 | optimal.frequency = freq; | |
50701588 | 161 | optimal.driver_data = i; |
1da177e4 LT |
162 | } |
163 | } else { | |
164 | if (freq <= suboptimal.frequency) { | |
165 | suboptimal.frequency = freq; | |
50701588 | 166 | suboptimal.driver_data = i; |
1da177e4 LT |
167 | } |
168 | } | |
169 | break; | |
170 | case CPUFREQ_RELATION_L: | |
1e498856 | 171 | if (freq > target_freq) { |
1da177e4 LT |
172 | if (freq <= optimal.frequency) { |
173 | optimal.frequency = freq; | |
50701588 | 174 | optimal.driver_data = i; |
1da177e4 LT |
175 | } |
176 | } else { | |
177 | if (freq >= suboptimal.frequency) { | |
178 | suboptimal.frequency = freq; | |
50701588 | 179 | suboptimal.driver_data = i; |
1da177e4 LT |
180 | } |
181 | } | |
182 | break; | |
5b0c0b16 SK |
183 | case CPUFREQ_RELATION_C: |
184 | diff = abs(freq - target_freq); | |
185 | if (diff < optimal.frequency || | |
186 | (diff == optimal.frequency && | |
187 | freq > table[optimal.driver_data].frequency)) { | |
188 | optimal.frequency = diff; | |
189 | optimal.driver_data = i; | |
190 | } | |
191 | break; | |
1da177e4 LT |
192 | } |
193 | } | |
50701588 | 194 | if (optimal.driver_data > i) { |
d218ed77 VK |
195 | if (suboptimal.driver_data > i) { |
196 | WARN(1, "Invalid frequency table: %d\n", policy->cpu); | |
197 | return 0; | |
198 | } | |
1da177e4 | 199 | |
d218ed77 VK |
200 | index = suboptimal.driver_data; |
201 | } else | |
202 | index = optimal.driver_data; | |
1da177e4 | 203 | |
d218ed77 VK |
204 | pr_debug("target index is %u, freq is:%u kHz\n", index, |
205 | table[index].frequency); | |
206 | return index; | |
1da177e4 | 207 | } |
da0c6dc0 | 208 | EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted); |
1da177e4 | 209 | |
d3916691 VK |
210 | int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, |
211 | unsigned int freq) | |
212 | { | |
f8bfc116 | 213 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; |
d3916691 | 214 | |
d3916691 VK |
215 | if (unlikely(!table)) { |
216 | pr_debug("%s: Unable to find frequency table\n", __func__); | |
217 | return -ENOENT; | |
218 | } | |
219 | ||
041526f9 SK |
220 | cpufreq_for_each_valid_entry(pos, table) |
221 | if (pos->frequency == freq) | |
222 | return pos - table; | |
d3916691 VK |
223 | |
224 | return -EINVAL; | |
225 | } | |
226 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index); | |
227 | ||
1da177e4 | 228 | /** |
e32d22f7 | 229 | * show_available_freqs - show available frequencies for the specified CPU |
1da177e4 | 230 | */ |
6f19efc0 LM |
231 | static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf, |
232 | bool show_boost) | |
1da177e4 | 233 | { |
1da177e4 | 234 | ssize_t count = 0; |
041526f9 | 235 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; |
1da177e4 | 236 | |
e0b3165b | 237 | if (!table) |
1da177e4 LT |
238 | return -ENODEV; |
239 | ||
041526f9 | 240 | cpufreq_for_each_valid_entry(pos, table) { |
6f19efc0 LM |
241 | /* |
242 | * show_boost = true and driver_data = BOOST freq | |
243 | * display BOOST freqs | |
244 | * | |
245 | * show_boost = false and driver_data = BOOST freq | |
246 | * show_boost = true and driver_data != BOOST freq | |
247 | * continue - do not display anything | |
248 | * | |
249 | * show_boost = false and driver_data != BOOST freq | |
250 | * display NON BOOST freqs | |
251 | */ | |
041526f9 | 252 | if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ)) |
6f19efc0 LM |
253 | continue; |
254 | ||
041526f9 | 255 | count += sprintf(&buf[count], "%d ", pos->frequency); |
1da177e4 LT |
256 | } |
257 | count += sprintf(&buf[count], "\n"); | |
258 | ||
259 | return count; | |
260 | ||
261 | } | |
262 | ||
6f19efc0 LM |
263 | #define cpufreq_attr_available_freq(_name) \ |
264 | struct freq_attr cpufreq_freq_attr_##_name##_freqs = \ | |
265 | __ATTR_RO(_name##_frequencies) | |
266 | ||
267 | /** | |
268 | * show_scaling_available_frequencies - show available normal frequencies for | |
269 | * the specified CPU | |
270 | */ | |
271 | static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy, | |
272 | char *buf) | |
273 | { | |
274 | return show_available_freqs(policy, buf, false); | |
275 | } | |
276 | cpufreq_attr_available_freq(scaling_available); | |
1da177e4 LT |
277 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs); |
278 | ||
6f19efc0 LM |
279 | /** |
280 | * show_available_boost_freqs - show available boost frequencies for | |
281 | * the specified CPU | |
282 | */ | |
283 | static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy, | |
284 | char *buf) | |
285 | { | |
286 | return show_available_freqs(policy, buf, true); | |
287 | } | |
288 | cpufreq_attr_available_freq(scaling_boost); | |
289 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs); | |
290 | ||
18434512 VK |
291 | struct freq_attr *cpufreq_generic_attr[] = { |
292 | &cpufreq_freq_attr_scaling_available_freqs, | |
6f19efc0 LM |
293 | #ifdef CONFIG_CPU_FREQ_BOOST_SW |
294 | &cpufreq_freq_attr_scaling_boost_freqs, | |
295 | #endif | |
18434512 VK |
296 | NULL, |
297 | }; | |
298 | EXPORT_SYMBOL_GPL(cpufreq_generic_attr); | |
299 | ||
da0c6dc0 VK |
300 | static int set_freq_table_sorted(struct cpufreq_policy *policy) |
301 | { | |
302 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; | |
303 | struct cpufreq_frequency_table *prev = NULL; | |
304 | int ascending = 0; | |
305 | ||
306 | policy->freq_table_sorted = CPUFREQ_TABLE_UNSORTED; | |
307 | ||
308 | cpufreq_for_each_valid_entry(pos, table) { | |
309 | if (!prev) { | |
310 | prev = pos; | |
311 | continue; | |
312 | } | |
313 | ||
314 | if (pos->frequency == prev->frequency) { | |
315 | pr_warn("Duplicate freq-table entries: %u\n", | |
316 | pos->frequency); | |
317 | return -EINVAL; | |
318 | } | |
319 | ||
320 | /* Frequency increased from prev to pos */ | |
321 | if (pos->frequency > prev->frequency) { | |
322 | /* But frequency was decreasing earlier */ | |
323 | if (ascending < 0) { | |
324 | pr_debug("Freq table is unsorted\n"); | |
325 | return 0; | |
326 | } | |
327 | ||
328 | ascending++; | |
329 | } else { | |
330 | /* Frequency decreased from prev to pos */ | |
331 | ||
332 | /* But frequency was increasing earlier */ | |
333 | if (ascending > 0) { | |
334 | pr_debug("Freq table is unsorted\n"); | |
335 | return 0; | |
336 | } | |
337 | ||
338 | ascending--; | |
339 | } | |
340 | ||
341 | prev = pos; | |
342 | } | |
343 | ||
344 | if (ascending > 0) | |
345 | policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_ASCENDING; | |
346 | else | |
347 | policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_DESCENDING; | |
348 | ||
349 | pr_debug("Freq table is sorted in %s order\n", | |
350 | ascending > 0 ? "ascending" : "descending"); | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
27047a60 VK |
355 | int cpufreq_table_validate_and_show(struct cpufreq_policy *policy, |
356 | struct cpufreq_frequency_table *table) | |
357 | { | |
da0c6dc0 | 358 | int ret; |
27047a60 | 359 | |
da0c6dc0 VK |
360 | ret = cpufreq_frequency_table_cpuinfo(policy, table); |
361 | if (ret) | |
362 | return ret; | |
27047a60 | 363 | |
da0c6dc0 VK |
364 | policy->freq_table = table; |
365 | return set_freq_table_sorted(policy); | |
27047a60 VK |
366 | } |
367 | EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show); | |
368 | ||
97acec55 DJ |
369 | MODULE_AUTHOR("Dominik Brodowski <[email protected]>"); |
370 | MODULE_DESCRIPTION("CPUfreq frequency table helpers"); | |
371 | MODULE_LICENSE("GPL"); |