]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/drivers/cpufreq/freq_table.c | |
4 | * | |
5 | * Copyright (C) 2002 - 2003 Dominik Brodowski | |
6 | */ | |
7 | ||
db701151 VK |
8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
9 | ||
1da177e4 | 10 | #include <linux/cpufreq.h> |
5ff0a268 | 11 | #include <linux/module.h> |
1da177e4 | 12 | |
1da177e4 LT |
13 | /********************************************************************* |
14 | * FREQUENCY TABLE HELPERS * | |
15 | *********************************************************************/ | |
16 | ||
44139ed4 VK |
17 | bool policy_has_boost_freq(struct cpufreq_policy *policy) |
18 | { | |
19 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; | |
20 | ||
21 | if (!table) | |
22 | return false; | |
23 | ||
24 | cpufreq_for_each_valid_entry(pos, table) | |
25 | if (pos->flags & CPUFREQ_BOOST_FREQ) | |
26 | return true; | |
27 | ||
28 | return false; | |
29 | } | |
30 | EXPORT_SYMBOL_GPL(policy_has_boost_freq); | |
31 | ||
1da177e4 LT |
32 | int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy, |
33 | struct cpufreq_frequency_table *table) | |
34 | { | |
041526f9 | 35 | struct cpufreq_frequency_table *pos; |
1da177e4 LT |
36 | unsigned int min_freq = ~0; |
37 | unsigned int max_freq = 0; | |
041526f9 | 38 | unsigned int freq; |
1da177e4 | 39 | |
041526f9 SK |
40 | cpufreq_for_each_valid_entry(pos, table) { |
41 | freq = pos->frequency; | |
1da177e4 | 42 | |
f37a4d6b | 43 | if ((!cpufreq_boost_enabled() || !policy->boost_enabled) |
041526f9 | 44 | && (pos->flags & CPUFREQ_BOOST_FREQ)) |
6f19efc0 LM |
45 | continue; |
46 | ||
041526f9 | 47 | pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq); |
1da177e4 LT |
48 | if (freq < min_freq) |
49 | min_freq = freq; | |
50 | if (freq > max_freq) | |
51 | max_freq = freq; | |
52 | } | |
53 | ||
54 | policy->min = policy->cpuinfo.min_freq = min_freq; | |
538b0188 RW |
55 | policy->max = max_freq; |
56 | /* | |
57 | * If the driver has set its own cpuinfo.max_freq above max_freq, leave | |
58 | * it as is. | |
59 | */ | |
60 | if (policy->cpuinfo.max_freq < max_freq) | |
61 | policy->max = policy->cpuinfo.max_freq = max_freq; | |
1da177e4 LT |
62 | |
63 | if (policy->min == ~0) | |
64 | return -EINVAL; | |
65 | else | |
66 | return 0; | |
67 | } | |
1da177e4 | 68 | |
1e4f63ae | 69 | int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy, |
1da177e4 LT |
70 | struct cpufreq_frequency_table *table) |
71 | { | |
041526f9 | 72 | struct cpufreq_frequency_table *pos; |
db9ea3b2 | 73 | unsigned int freq, prev_smaller = 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 | ||
db9ea3b2 XY |
89 | if ((prev_smaller < freq) && (freq <= policy->max)) |
90 | prev_smaller = freq; | |
1da177e4 LT |
91 | } |
92 | ||
77db50c4 | 93 | if (!found) { |
db9ea3b2 | 94 | policy->max = prev_smaller; |
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 | 108 | */ |
1e4f63ae | 109 | int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy) |
18434512 | 110 | { |
f8bfc116 | 111 | if (!policy->freq_table) |
18434512 VK |
112 | return -ENODEV; |
113 | ||
f8bfc116 | 114 | return cpufreq_frequency_table_verify(policy, policy->freq_table); |
18434512 VK |
115 | } |
116 | EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify); | |
1da177e4 | 117 | |
da0c6dc0 VK |
118 | int cpufreq_table_index_unsorted(struct cpufreq_policy *policy, |
119 | unsigned int target_freq, | |
120 | unsigned int relation) | |
1da177e4 | 121 | { |
484944a5 | 122 | struct cpufreq_frequency_table optimal = { |
50701588 | 123 | .driver_data = ~0, |
484944a5 DJ |
124 | .frequency = 0, |
125 | }; | |
126 | struct cpufreq_frequency_table suboptimal = { | |
50701588 | 127 | .driver_data = ~0, |
484944a5 DJ |
128 | .frequency = 0, |
129 | }; | |
041526f9 | 130 | struct cpufreq_frequency_table *pos; |
7ab4aabb | 131 | struct cpufreq_frequency_table *table = policy->freq_table; |
5b0c0b16 | 132 | unsigned int freq, diff, i = 0; |
d218ed77 | 133 | int index; |
1da177e4 | 134 | |
2d06d8c4 | 135 | pr_debug("request for target %u kHz (relation: %u) for cpu %u\n", |
e08f5f5b | 136 | target_freq, relation, policy->cpu); |
1da177e4 LT |
137 | |
138 | switch (relation) { | |
139 | case CPUFREQ_RELATION_H: | |
1da177e4 LT |
140 | suboptimal.frequency = ~0; |
141 | break; | |
142 | case CPUFREQ_RELATION_L: | |
5b0c0b16 | 143 | case CPUFREQ_RELATION_C: |
1da177e4 | 144 | optimal.frequency = ~0; |
1da177e4 LT |
145 | break; |
146 | } | |
147 | ||
ffd81dcf | 148 | cpufreq_for_each_valid_entry_idx(pos, table, i) { |
041526f9 SK |
149 | freq = pos->frequency; |
150 | ||
1da177e4 LT |
151 | if ((freq < policy->min) || (freq > policy->max)) |
152 | continue; | |
1e498856 SK |
153 | if (freq == target_freq) { |
154 | optimal.driver_data = i; | |
155 | break; | |
156 | } | |
97acec55 | 157 | switch (relation) { |
1da177e4 | 158 | case CPUFREQ_RELATION_H: |
1e498856 | 159 | if (freq < target_freq) { |
1da177e4 LT |
160 | if (freq >= optimal.frequency) { |
161 | optimal.frequency = freq; | |
50701588 | 162 | optimal.driver_data = i; |
1da177e4 LT |
163 | } |
164 | } else { | |
165 | if (freq <= suboptimal.frequency) { | |
166 | suboptimal.frequency = freq; | |
50701588 | 167 | suboptimal.driver_data = i; |
1da177e4 LT |
168 | } |
169 | } | |
170 | break; | |
171 | case CPUFREQ_RELATION_L: | |
1e498856 | 172 | if (freq > target_freq) { |
1da177e4 LT |
173 | if (freq <= optimal.frequency) { |
174 | optimal.frequency = freq; | |
50701588 | 175 | optimal.driver_data = i; |
1da177e4 LT |
176 | } |
177 | } else { | |
178 | if (freq >= suboptimal.frequency) { | |
179 | suboptimal.frequency = freq; | |
50701588 | 180 | suboptimal.driver_data = i; |
1da177e4 LT |
181 | } |
182 | } | |
183 | break; | |
5b0c0b16 SK |
184 | case CPUFREQ_RELATION_C: |
185 | diff = abs(freq - target_freq); | |
186 | if (diff < optimal.frequency || | |
187 | (diff == optimal.frequency && | |
188 | freq > table[optimal.driver_data].frequency)) { | |
189 | optimal.frequency = diff; | |
190 | optimal.driver_data = i; | |
191 | } | |
192 | break; | |
1da177e4 LT |
193 | } |
194 | } | |
50701588 | 195 | if (optimal.driver_data > i) { |
d218ed77 | 196 | if (suboptimal.driver_data > i) { |
a2bd1d26 | 197 | WARN(1, "Invalid frequency table: %u\n", policy->cpu); |
d218ed77 VK |
198 | return 0; |
199 | } | |
1da177e4 | 200 | |
d218ed77 VK |
201 | index = suboptimal.driver_data; |
202 | } else | |
203 | index = optimal.driver_data; | |
1da177e4 | 204 | |
d218ed77 VK |
205 | pr_debug("target index is %u, freq is:%u kHz\n", index, |
206 | table[index].frequency); | |
207 | return index; | |
1da177e4 | 208 | } |
da0c6dc0 | 209 | EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted); |
1da177e4 | 210 | |
d3916691 VK |
211 | int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, |
212 | unsigned int freq) | |
213 | { | |
f8bfc116 | 214 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; |
ffd81dcf | 215 | int idx; |
d3916691 | 216 | |
d3916691 VK |
217 | if (unlikely(!table)) { |
218 | pr_debug("%s: Unable to find frequency table\n", __func__); | |
219 | return -ENOENT; | |
220 | } | |
221 | ||
ffd81dcf | 222 | cpufreq_for_each_valid_entry_idx(pos, table, idx) |
041526f9 | 223 | if (pos->frequency == freq) |
ffd81dcf | 224 | return idx; |
d3916691 VK |
225 | |
226 | return -EINVAL; | |
227 | } | |
228 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index); | |
229 | ||
19231a87 | 230 | /* |
e32d22f7 | 231 | * show_available_freqs - show available frequencies for the specified CPU |
1da177e4 | 232 | */ |
6f19efc0 LM |
233 | static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf, |
234 | bool show_boost) | |
1da177e4 | 235 | { |
1da177e4 | 236 | ssize_t count = 0; |
041526f9 | 237 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; |
1da177e4 | 238 | |
e0b3165b | 239 | if (!table) |
1da177e4 LT |
240 | return -ENODEV; |
241 | ||
041526f9 | 242 | cpufreq_for_each_valid_entry(pos, table) { |
6f19efc0 LM |
243 | /* |
244 | * show_boost = true and driver_data = BOOST freq | |
245 | * display BOOST freqs | |
246 | * | |
247 | * show_boost = false and driver_data = BOOST freq | |
248 | * show_boost = true and driver_data != BOOST freq | |
249 | * continue - do not display anything | |
250 | * | |
251 | * show_boost = false and driver_data != BOOST freq | |
252 | * display NON BOOST freqs | |
253 | */ | |
041526f9 | 254 | if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ)) |
6f19efc0 LM |
255 | continue; |
256 | ||
a2bd1d26 | 257 | count += sprintf(&buf[count], "%u ", pos->frequency); |
1da177e4 LT |
258 | } |
259 | count += sprintf(&buf[count], "\n"); | |
260 | ||
261 | return count; | |
262 | ||
263 | } | |
264 | ||
6f19efc0 LM |
265 | #define cpufreq_attr_available_freq(_name) \ |
266 | struct freq_attr cpufreq_freq_attr_##_name##_freqs = \ | |
267 | __ATTR_RO(_name##_frequencies) | |
268 | ||
19231a87 | 269 | /* |
a95d8f58 | 270 | * scaling_available_frequencies_show - show available normal frequencies for |
6f19efc0 LM |
271 | * the specified CPU |
272 | */ | |
273 | static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy, | |
274 | char *buf) | |
275 | { | |
276 | return show_available_freqs(policy, buf, false); | |
277 | } | |
278 | cpufreq_attr_available_freq(scaling_available); | |
1da177e4 LT |
279 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs); |
280 | ||
19231a87 | 281 | /* |
a95d8f58 | 282 | * scaling_boost_frequencies_show - show available boost frequencies for |
6f19efc0 LM |
283 | * the specified CPU |
284 | */ | |
285 | static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy, | |
286 | char *buf) | |
287 | { | |
288 | return show_available_freqs(policy, buf, true); | |
289 | } | |
290 | cpufreq_attr_available_freq(scaling_boost); | |
291 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs); | |
292 | ||
18434512 VK |
293 | struct freq_attr *cpufreq_generic_attr[] = { |
294 | &cpufreq_freq_attr_scaling_available_freqs, | |
295 | NULL, | |
296 | }; | |
297 | EXPORT_SYMBOL_GPL(cpufreq_generic_attr); | |
298 | ||
da0c6dc0 VK |
299 | static int set_freq_table_sorted(struct cpufreq_policy *policy) |
300 | { | |
301 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; | |
302 | struct cpufreq_frequency_table *prev = NULL; | |
303 | int ascending = 0; | |
304 | ||
305 | policy->freq_table_sorted = CPUFREQ_TABLE_UNSORTED; | |
306 | ||
307 | cpufreq_for_each_valid_entry(pos, table) { | |
308 | if (!prev) { | |
309 | prev = pos; | |
310 | continue; | |
311 | } | |
312 | ||
313 | if (pos->frequency == prev->frequency) { | |
314 | pr_warn("Duplicate freq-table entries: %u\n", | |
315 | pos->frequency); | |
316 | return -EINVAL; | |
317 | } | |
318 | ||
319 | /* Frequency increased from prev to pos */ | |
320 | if (pos->frequency > prev->frequency) { | |
321 | /* But frequency was decreasing earlier */ | |
322 | if (ascending < 0) { | |
323 | pr_debug("Freq table is unsorted\n"); | |
324 | return 0; | |
325 | } | |
326 | ||
327 | ascending++; | |
328 | } else { | |
329 | /* Frequency decreased from prev to pos */ | |
330 | ||
331 | /* But frequency was increasing earlier */ | |
332 | if (ascending > 0) { | |
333 | pr_debug("Freq table is unsorted\n"); | |
334 | return 0; | |
335 | } | |
336 | ||
337 | ascending--; | |
338 | } | |
339 | ||
340 | prev = pos; | |
341 | } | |
342 | ||
343 | if (ascending > 0) | |
344 | policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_ASCENDING; | |
345 | else | |
346 | policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_DESCENDING; | |
347 | ||
348 | pr_debug("Freq table is sorted in %s order\n", | |
349 | ascending > 0 ? "ascending" : "descending"); | |
350 | ||
351 | return 0; | |
352 | } | |
353 | ||
d417e069 VK |
354 | int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy) |
355 | { | |
356 | int ret; | |
357 | ||
a038895e VK |
358 | if (!policy->freq_table) { |
359 | /* Freq table must be passed by drivers with target_index() */ | |
360 | if (has_target_index()) | |
361 | return -EINVAL; | |
362 | ||
d417e069 | 363 | return 0; |
a038895e | 364 | } |
d417e069 VK |
365 | |
366 | ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table); | |
367 | if (ret) | |
368 | return ret; | |
369 | ||
370 | return set_freq_table_sorted(policy); | |
371 | } | |
372 | ||
97acec55 DJ |
373 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>"); |
374 | MODULE_DESCRIPTION("CPUfreq frequency table helpers"); |