1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/cpufreq/freq_table.c
5 * Copyright (C) 2002 - 2003 Dominik Brodowski
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;
24 cpufreq_for_each_valid_entry(pos, table)
25 if (pos->flags & CPUFREQ_BOOST_FREQ)
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;
40 cpufreq_for_each_valid_entry(pos, table) {
41 freq = pos->frequency;
43 if (!cpufreq_boost_enabled()
44 && (pos->flags & CPUFREQ_BOOST_FREQ))
47 pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
54 policy->min = policy->cpuinfo.min_freq = min_freq;
55 policy->max = policy->cpuinfo.max_freq = max_freq;
57 if (policy->min == ~0)
63 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
64 struct cpufreq_frequency_table *table)
66 struct cpufreq_frequency_table *pos;
67 unsigned int freq, next_larger = ~0;
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)) {
83 if ((next_larger > freq) && (freq > policy->max))
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);
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 *policy)
105 if (!policy->freq_table)
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 = {
120 struct cpufreq_frequency_table suboptimal = {
124 struct cpufreq_frequency_table *pos;
125 struct cpufreq_frequency_table *table = policy->freq_table;
126 unsigned int freq, diff, i = 0;
129 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
130 target_freq, relation, policy->cpu);
133 case CPUFREQ_RELATION_H:
134 suboptimal.frequency = ~0;
136 case CPUFREQ_RELATION_L:
137 case CPUFREQ_RELATION_C:
138 optimal.frequency = ~0;
142 cpufreq_for_each_valid_entry_idx(pos, table, i) {
143 freq = pos->frequency;
145 if ((freq < policy->min) || (freq > policy->max))
147 if (freq == target_freq) {
148 optimal.driver_data = i;
152 case CPUFREQ_RELATION_H:
153 if (freq < target_freq) {
154 if (freq >= optimal.frequency) {
155 optimal.frequency = freq;
156 optimal.driver_data = i;
159 if (freq <= suboptimal.frequency) {
160 suboptimal.frequency = freq;
161 suboptimal.driver_data = i;
165 case CPUFREQ_RELATION_L:
166 if (freq > target_freq) {
167 if (freq <= optimal.frequency) {
168 optimal.frequency = freq;
169 optimal.driver_data = i;
172 if (freq >= suboptimal.frequency) {
173 suboptimal.frequency = freq;
174 suboptimal.driver_data = i;
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;
189 if (optimal.driver_data > i) {
190 if (suboptimal.driver_data > i) {
191 WARN(1, "Invalid frequency table: %d\n", policy->cpu);
195 index = suboptimal.driver_data;
197 index = optimal.driver_data;
199 pr_debug("target index is %u, freq is:%u kHz\n", index,
200 table[index].frequency);
203 EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted);
205 int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
208 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
211 if (unlikely(!table)) {
212 pr_debug("%s: Unable to find frequency table\n", __func__);
216 cpufreq_for_each_valid_entry_idx(pos, table, idx)
217 if (pos->frequency == freq)
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,
231 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
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))
251 count += sprintf(&buf[count], "%d ", pos->frequency);
253 count += sprintf(&buf[count], "\n");
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
267 static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
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
279 static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
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,
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;
299 policy->freq_table_sorted = CPUFREQ_TABLE_UNSORTED;
301 cpufreq_for_each_valid_entry(pos, table) {
307 if (pos->frequency == prev->frequency) {
308 pr_warn("Duplicate freq-table entries: %u\n",
313 /* Frequency increased from prev to pos */
314 if (pos->frequency > prev->frequency) {
315 /* But frequency was decreasing earlier */
317 pr_debug("Freq table is unsorted\n");
323 /* Frequency decreased from prev to pos */
325 /* But frequency was increasing earlier */
327 pr_debug("Freq table is unsorted\n");
338 policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_ASCENDING;
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");
348 int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy)
352 if (!policy->freq_table)
355 ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
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");