]>
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 | ||
ffd81dcf | 146 | cpufreq_for_each_valid_entry_idx(pos, table, i) { |
041526f9 SK |
147 | freq = pos->frequency; |
148 | ||
1da177e4 LT |
149 | if ((freq < policy->min) || (freq > policy->max)) |
150 | continue; | |
1e498856 SK |
151 | if (freq == target_freq) { |
152 | optimal.driver_data = i; | |
153 | break; | |
154 | } | |
97acec55 | 155 | switch (relation) { |
1da177e4 | 156 | case CPUFREQ_RELATION_H: |
1e498856 | 157 | if (freq < target_freq) { |
1da177e4 LT |
158 | if (freq >= optimal.frequency) { |
159 | optimal.frequency = freq; | |
50701588 | 160 | optimal.driver_data = i; |
1da177e4 LT |
161 | } |
162 | } else { | |
163 | if (freq <= suboptimal.frequency) { | |
164 | suboptimal.frequency = freq; | |
50701588 | 165 | suboptimal.driver_data = i; |
1da177e4 LT |
166 | } |
167 | } | |
168 | break; | |
169 | case CPUFREQ_RELATION_L: | |
1e498856 | 170 | if (freq > target_freq) { |
1da177e4 LT |
171 | if (freq <= optimal.frequency) { |
172 | optimal.frequency = freq; | |
50701588 | 173 | optimal.driver_data = i; |
1da177e4 LT |
174 | } |
175 | } else { | |
176 | if (freq >= suboptimal.frequency) { | |
177 | suboptimal.frequency = freq; | |
50701588 | 178 | suboptimal.driver_data = i; |
1da177e4 LT |
179 | } |
180 | } | |
181 | break; | |
5b0c0b16 SK |
182 | case CPUFREQ_RELATION_C: |
183 | diff = abs(freq - target_freq); | |
184 | if (diff < optimal.frequency || | |
185 | (diff == optimal.frequency && | |
186 | freq > table[optimal.driver_data].frequency)) { | |
187 | optimal.frequency = diff; | |
188 | optimal.driver_data = i; | |
189 | } | |
190 | break; | |
1da177e4 LT |
191 | } |
192 | } | |
50701588 | 193 | if (optimal.driver_data > i) { |
d218ed77 VK |
194 | if (suboptimal.driver_data > i) { |
195 | WARN(1, "Invalid frequency table: %d\n", policy->cpu); | |
196 | return 0; | |
197 | } | |
1da177e4 | 198 | |
d218ed77 VK |
199 | index = suboptimal.driver_data; |
200 | } else | |
201 | index = optimal.driver_data; | |
1da177e4 | 202 | |
d218ed77 VK |
203 | pr_debug("target index is %u, freq is:%u kHz\n", index, |
204 | table[index].frequency); | |
205 | return index; | |
1da177e4 | 206 | } |
da0c6dc0 | 207 | EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted); |
1da177e4 | 208 | |
d3916691 VK |
209 | int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, |
210 | unsigned int freq) | |
211 | { | |
f8bfc116 | 212 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; |
ffd81dcf | 213 | int idx; |
d3916691 | 214 | |
d3916691 VK |
215 | if (unlikely(!table)) { |
216 | pr_debug("%s: Unable to find frequency table\n", __func__); | |
217 | return -ENOENT; | |
218 | } | |
219 | ||
ffd81dcf | 220 | cpufreq_for_each_valid_entry_idx(pos, table, idx) |
041526f9 | 221 | if (pos->frequency == freq) |
ffd81dcf | 222 | return idx; |
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, | |
293 | NULL, | |
294 | }; | |
295 | EXPORT_SYMBOL_GPL(cpufreq_generic_attr); | |
296 | ||
da0c6dc0 VK |
297 | static int set_freq_table_sorted(struct cpufreq_policy *policy) |
298 | { | |
299 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; | |
300 | struct cpufreq_frequency_table *prev = NULL; | |
301 | int ascending = 0; | |
302 | ||
303 | policy->freq_table_sorted = CPUFREQ_TABLE_UNSORTED; | |
304 | ||
305 | cpufreq_for_each_valid_entry(pos, table) { | |
306 | if (!prev) { | |
307 | prev = pos; | |
308 | continue; | |
309 | } | |
310 | ||
311 | if (pos->frequency == prev->frequency) { | |
312 | pr_warn("Duplicate freq-table entries: %u\n", | |
313 | pos->frequency); | |
314 | return -EINVAL; | |
315 | } | |
316 | ||
317 | /* Frequency increased from prev to pos */ | |
318 | if (pos->frequency > prev->frequency) { | |
319 | /* But frequency was decreasing earlier */ | |
320 | if (ascending < 0) { | |
321 | pr_debug("Freq table is unsorted\n"); | |
322 | return 0; | |
323 | } | |
324 | ||
325 | ascending++; | |
326 | } else { | |
327 | /* Frequency decreased from prev to pos */ | |
328 | ||
329 | /* But frequency was increasing earlier */ | |
330 | if (ascending > 0) { | |
331 | pr_debug("Freq table is unsorted\n"); | |
332 | return 0; | |
333 | } | |
334 | ||
335 | ascending--; | |
336 | } | |
337 | ||
338 | prev = pos; | |
339 | } | |
340 | ||
341 | if (ascending > 0) | |
342 | policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_ASCENDING; | |
343 | else | |
344 | policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_DESCENDING; | |
345 | ||
346 | pr_debug("Freq table is sorted in %s order\n", | |
347 | ascending > 0 ? "ascending" : "descending"); | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
d417e069 VK |
352 | int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy) |
353 | { | |
354 | int ret; | |
355 | ||
356 | if (!policy->freq_table) | |
357 | return 0; | |
358 | ||
359 | ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table); | |
360 | if (ret) | |
361 | return ret; | |
362 | ||
363 | return set_freq_table_sorted(policy); | |
364 | } | |
365 | ||
97acec55 DJ |
366 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>"); |
367 | MODULE_DESCRIPTION("CPUfreq frequency table helpers"); | |
368 | MODULE_LICENSE("GPL"); |