io_uring: do not always copy iovec in io_req_map_rw()
[linux/fpc-iii.git] / drivers / cpufreq / freq_table.c
blobe117b0059123ec3d1a74e3a039bc0335c3f30f4a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/cpufreq/freq_table.c
5 * Copyright (C) 2002 - 2003 Dominik Brodowski
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/cpufreq.h>
11 #include <linux/module.h>
13 /*********************************************************************
14 * FREQUENCY TABLE HELPERS *
15 *********************************************************************/
17 bool policy_has_boost_freq(struct cpufreq_policy *policy)
19 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
21 if (!table)
22 return false;
24 cpufreq_for_each_valid_entry(pos, table)
25 if (pos->flags & CPUFREQ_BOOST_FREQ)
26 return true;
28 return false;
30 EXPORT_SYMBOL_GPL(policy_has_boost_freq);
32 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
33 struct cpufreq_frequency_table *table)
35 struct cpufreq_frequency_table *pos;
36 unsigned int min_freq = ~0;
37 unsigned int max_freq = 0;
38 unsigned int freq;
40 cpufreq_for_each_valid_entry(pos, table) {
41 freq = pos->frequency;
43 if (!cpufreq_boost_enabled()
44 && (pos->flags & CPUFREQ_BOOST_FREQ))
45 continue;
47 pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
48 if (freq < min_freq)
49 min_freq = freq;
50 if (freq > max_freq)
51 max_freq = freq;
54 policy->min = policy->cpuinfo.min_freq = min_freq;
55 policy->max = policy->cpuinfo.max_freq = max_freq;
57 if (policy->min == ~0)
58 return -EINVAL;
59 else
60 return 0;
63 int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy,
64 struct cpufreq_frequency_table *table)
66 struct cpufreq_frequency_table *pos;
67 unsigned int freq, next_larger = ~0;
68 bool found = false;
70 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
71 policy->min, policy->max, policy->cpu);
73 cpufreq_verify_within_cpu_limits(policy);
75 cpufreq_for_each_valid_entry(pos, table) {
76 freq = pos->frequency;
78 if ((freq >= policy->min) && (freq <= policy->max)) {
79 found = true;
80 break;
83 if ((next_larger > freq) && (freq > policy->max))
84 next_larger = freq;
87 if (!found) {
88 policy->max = next_larger;
89 cpufreq_verify_within_cpu_limits(policy);
92 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
93 policy->min, policy->max, policy->cpu);
95 return 0;
97 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
100 * Generic routine to verify policy & frequency table, requires driver to set
101 * policy->freq_table prior to it.
103 int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy)
105 if (!policy->freq_table)
106 return -ENODEV;
108 return cpufreq_frequency_table_verify(policy, policy->freq_table);
110 EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
112 int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
113 unsigned int target_freq,
114 unsigned int relation)
116 struct cpufreq_frequency_table optimal = {
117 .driver_data = ~0,
118 .frequency = 0,
120 struct cpufreq_frequency_table suboptimal = {
121 .driver_data = ~0,
122 .frequency = 0,
124 struct cpufreq_frequency_table *pos;
125 struct cpufreq_frequency_table *table = policy->freq_table;
126 unsigned int freq, diff, i = 0;
127 int index;
129 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
130 target_freq, relation, policy->cpu);
132 switch (relation) {
133 case CPUFREQ_RELATION_H:
134 suboptimal.frequency = ~0;
135 break;
136 case CPUFREQ_RELATION_L:
137 case CPUFREQ_RELATION_C:
138 optimal.frequency = ~0;
139 break;
142 cpufreq_for_each_valid_entry_idx(pos, table, i) {
143 freq = pos->frequency;
145 if ((freq < policy->min) || (freq > policy->max))
146 continue;
147 if (freq == target_freq) {
148 optimal.driver_data = i;
149 break;
151 switch (relation) {
152 case CPUFREQ_RELATION_H:
153 if (freq < target_freq) {
154 if (freq >= optimal.frequency) {
155 optimal.frequency = freq;
156 optimal.driver_data = i;
158 } else {
159 if (freq <= suboptimal.frequency) {
160 suboptimal.frequency = freq;
161 suboptimal.driver_data = i;
164 break;
165 case CPUFREQ_RELATION_L:
166 if (freq > target_freq) {
167 if (freq <= optimal.frequency) {
168 optimal.frequency = freq;
169 optimal.driver_data = i;
171 } else {
172 if (freq >= suboptimal.frequency) {
173 suboptimal.frequency = freq;
174 suboptimal.driver_data = i;
177 break;
178 case CPUFREQ_RELATION_C:
179 diff = abs(freq - target_freq);
180 if (diff < optimal.frequency ||
181 (diff == optimal.frequency &&
182 freq > table[optimal.driver_data].frequency)) {
183 optimal.frequency = diff;
184 optimal.driver_data = i;
186 break;
189 if (optimal.driver_data > i) {
190 if (suboptimal.driver_data > i) {
191 WARN(1, "Invalid frequency table: %d\n", policy->cpu);
192 return 0;
195 index = suboptimal.driver_data;
196 } else
197 index = optimal.driver_data;
199 pr_debug("target index is %u, freq is:%u kHz\n", index,
200 table[index].frequency);
201 return index;
203 EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted);
205 int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
206 unsigned int freq)
208 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
209 int idx;
211 if (unlikely(!table)) {
212 pr_debug("%s: Unable to find frequency table\n", __func__);
213 return -ENOENT;
216 cpufreq_for_each_valid_entry_idx(pos, table, idx)
217 if (pos->frequency == freq)
218 return idx;
220 return -EINVAL;
222 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
225 * show_available_freqs - show available frequencies for the specified CPU
227 static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
228 bool show_boost)
230 ssize_t count = 0;
231 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
233 if (!table)
234 return -ENODEV;
236 cpufreq_for_each_valid_entry(pos, table) {
238 * show_boost = true and driver_data = BOOST freq
239 * display BOOST freqs
241 * show_boost = false and driver_data = BOOST freq
242 * show_boost = true and driver_data != BOOST freq
243 * continue - do not display anything
245 * show_boost = false and driver_data != BOOST freq
246 * display NON BOOST freqs
248 if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
249 continue;
251 count += sprintf(&buf[count], "%d ", pos->frequency);
253 count += sprintf(&buf[count], "\n");
255 return count;
259 #define cpufreq_attr_available_freq(_name) \
260 struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
261 __ATTR_RO(_name##_frequencies)
264 * show_scaling_available_frequencies - show available normal frequencies for
265 * the specified CPU
267 static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
268 char *buf)
270 return show_available_freqs(policy, buf, false);
272 cpufreq_attr_available_freq(scaling_available);
273 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
276 * show_available_boost_freqs - show available boost frequencies for
277 * the specified CPU
279 static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
280 char *buf)
282 return show_available_freqs(policy, buf, true);
284 cpufreq_attr_available_freq(scaling_boost);
285 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
287 struct freq_attr *cpufreq_generic_attr[] = {
288 &cpufreq_freq_attr_scaling_available_freqs,
289 NULL,
291 EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
293 static int set_freq_table_sorted(struct cpufreq_policy *policy)
295 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
296 struct cpufreq_frequency_table *prev = NULL;
297 int ascending = 0;
299 policy->freq_table_sorted = CPUFREQ_TABLE_UNSORTED;
301 cpufreq_for_each_valid_entry(pos, table) {
302 if (!prev) {
303 prev = pos;
304 continue;
307 if (pos->frequency == prev->frequency) {
308 pr_warn("Duplicate freq-table entries: %u\n",
309 pos->frequency);
310 return -EINVAL;
313 /* Frequency increased from prev to pos */
314 if (pos->frequency > prev->frequency) {
315 /* But frequency was decreasing earlier */
316 if (ascending < 0) {
317 pr_debug("Freq table is unsorted\n");
318 return 0;
321 ascending++;
322 } else {
323 /* Frequency decreased from prev to pos */
325 /* But frequency was increasing earlier */
326 if (ascending > 0) {
327 pr_debug("Freq table is unsorted\n");
328 return 0;
331 ascending--;
334 prev = pos;
337 if (ascending > 0)
338 policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_ASCENDING;
339 else
340 policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_DESCENDING;
342 pr_debug("Freq table is sorted in %s order\n",
343 ascending > 0 ? "ascending" : "descending");
345 return 0;
348 int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy)
350 int ret;
352 if (!policy->freq_table)
353 return 0;
355 ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
356 if (ret)
357 return ret;
359 return set_freq_table_sorted(policy);
362 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
363 MODULE_DESCRIPTION("CPUfreq frequency table helpers");
364 MODULE_LICENSE("GPL");