blob: 67e56cf638efb86a3f26c811c1d330a778a8bd47 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/drivers/cpufreq/freq_table.c
4 *
5 * Copyright (C) 2002 - 2003 Dominik Brodowski
6 */
7
Viresh Kumardb701152012-10-23 01:29:03 +02008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/cpufreq.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013/*********************************************************************
14 * FREQUENCY TABLE HELPERS *
15 *********************************************************************/
16
Viresh Kumar44139ed2015-07-29 16:23:09 +053017bool 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}
30EXPORT_SYMBOL_GPL(policy_has_boost_freq);
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
33 struct cpufreq_frequency_table *table)
34{
Stratos Karafotis041526f2014-04-25 23:15:38 +030035 struct cpufreq_frequency_table *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 unsigned int min_freq = ~0;
37 unsigned int max_freq = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +030038 unsigned int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Stratos Karafotis041526f2014-04-25 23:15:38 +030040 cpufreq_for_each_valid_entry(pos, table) {
41 freq = pos->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Lukasz Majewski6f19efc2013-12-20 15:24:49 +010043 if (!cpufreq_boost_enabled()
Stratos Karafotis041526f2014-04-25 23:15:38 +030044 && (pos->flags & CPUFREQ_BOOST_FREQ))
Lukasz Majewski6f19efc2013-12-20 15:24:49 +010045 continue;
46
Stratos Karafotis041526f2014-04-25 23:15:38 +030047 pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 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;
Rafael J. Wysocki538b0182021-02-15 20:24:46 +010055 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 if (policy->min == ~0)
64 return -EINVAL;
65 else
66 return 0;
67}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Rafael J. Wysocki1e4f63a2020-01-26 23:40:11 +010069int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct cpufreq_frequency_table *table)
71{
Stratos Karafotis041526f2014-04-25 23:15:38 +030072 struct cpufreq_frequency_table *pos;
73 unsigned int freq, next_larger = ~0;
Viresh Kumar77db50c2013-10-02 14:13:15 +053074 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020076 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053077 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Viresh Kumarbe49e342013-10-02 14:13:19 +053079 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Stratos Karafotis041526f2014-04-25 23:15:38 +030081 cpufreq_for_each_valid_entry(pos, table) {
82 freq = pos->frequency;
83
Viresh Kumar77db50c2013-10-02 14:13:15 +053084 if ((freq >= policy->min) && (freq <= policy->max)) {
85 found = true;
86 break;
87 }
88
89 if ((next_larger > freq) && (freq > policy->max))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 next_larger = freq;
91 }
92
Viresh Kumar77db50c2013-10-02 14:13:15 +053093 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 policy->max = next_larger;
Viresh Kumarbe49e342013-10-02 14:13:19 +053095 cpufreq_verify_within_cpu_limits(policy);
Viresh Kumar77db50c2013-10-02 14:13:15 +053096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020098 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053099 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 return 0;
102}
103EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
104
Viresh Kumar18434512013-10-03 20:27:55 +0530105/*
Viresh Kumare0b31652014-03-10 14:53:33 +0530106 * Generic routine to verify policy & frequency table, requires driver to set
107 * policy->freq_table prior to it.
Viresh Kumar18434512013-10-03 20:27:55 +0530108 */
Rafael J. Wysocki1e4f63a2020-01-26 23:40:11 +0100109int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy)
Viresh Kumar18434512013-10-03 20:27:55 +0530110{
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530111 if (!policy->freq_table)
Viresh Kumar18434512013-10-03 20:27:55 +0530112 return -ENODEV;
113
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530114 return cpufreq_frequency_table_verify(policy, policy->freq_table);
Viresh Kumar18434512013-10-03 20:27:55 +0530115}
116EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530118int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
119 unsigned int target_freq,
120 unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Dave Jones484944a2006-05-30 18:09:31 -0400122 struct cpufreq_frequency_table optimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530123 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400124 .frequency = 0,
125 };
126 struct cpufreq_frequency_table suboptimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530127 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400128 .frequency = 0,
129 };
Stratos Karafotis041526f2014-04-25 23:15:38 +0300130 struct cpufreq_frequency_table *pos;
Viresh Kumar7ab4aab2016-06-03 10:58:49 +0530131 struct cpufreq_frequency_table *table = policy->freq_table;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300132 unsigned int freq, diff, i = 0;
Viresh Kumard218ed72016-06-03 10:58:51 +0530133 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200135 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530136 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 switch (relation) {
139 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 suboptimal.frequency = ~0;
141 break;
142 case CPUFREQ_RELATION_L:
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300143 case CPUFREQ_RELATION_C:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 break;
146 }
147
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100148 cpufreq_for_each_valid_entry_idx(pos, table, i) {
Stratos Karafotis041526f2014-04-25 23:15:38 +0300149 freq = pos->frequency;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if ((freq < policy->min) || (freq > policy->max))
152 continue;
Stratos Karafotis1e498852014-05-14 21:05:52 +0300153 if (freq == target_freq) {
154 optimal.driver_data = i;
155 break;
156 }
Dave Jones97acec52009-01-18 01:56:41 -0500157 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 case CPUFREQ_RELATION_H:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300159 if (freq < target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (freq >= optimal.frequency) {
161 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530162 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 } else {
165 if (freq <= suboptimal.frequency) {
166 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530167 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 }
170 break;
171 case CPUFREQ_RELATION_L:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300172 if (freq > target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (freq <= optimal.frequency) {
174 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530175 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 } else {
178 if (freq >= suboptimal.frequency) {
179 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530180 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 }
183 break;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300184 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194 }
Viresh Kumar50701582013-03-30 16:25:15 +0530195 if (optimal.driver_data > i) {
Viresh Kumard218ed72016-06-03 10:58:51 +0530196 if (suboptimal.driver_data > i) {
197 WARN(1, "Invalid frequency table: %d\n", policy->cpu);
198 return 0;
199 }
200
201 index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 } else
Viresh Kumard218ed72016-06-03 10:58:51 +0530203 index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Viresh Kumard218ed72016-06-03 10:58:51 +0530205 pr_debug("target index is %u, freq is:%u kHz\n", index,
206 table[index].frequency);
207 return index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530209EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Viresh Kumard3916692013-12-03 11:20:46 +0530211int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
212 unsigned int freq)
213{
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530214 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100215 int idx;
Viresh Kumard3916692013-12-03 11:20:46 +0530216
Viresh Kumard3916692013-12-03 11:20:46 +0530217 if (unlikely(!table)) {
218 pr_debug("%s: Unable to find frequency table\n", __func__);
219 return -ENOENT;
220 }
221
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100222 cpufreq_for_each_valid_entry_idx(pos, table, idx)
Stratos Karafotis041526f2014-04-25 23:15:38 +0300223 if (pos->frequency == freq)
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100224 return idx;
Viresh Kumard3916692013-12-03 11:20:46 +0530225
226 return -EINVAL;
227}
228EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
229
Lee Jones19231a82020-07-15 09:26:22 +0100230/*
Fenghua Yue32d22f2007-11-21 14:52:15 -0800231 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100233static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
234 bool show_boost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 ssize_t count = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300237 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Viresh Kumare0b31652014-03-10 14:53:33 +0530239 if (!table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -ENODEV;
241
Stratos Karafotis041526f2014-04-25 23:15:38 +0300242 cpufreq_for_each_valid_entry(pos, table) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100243 /*
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 */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300254 if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100255 continue;
256
Stratos Karafotis041526f2014-04-25 23:15:38 +0300257 count += sprintf(&buf[count], "%d ", pos->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 count += sprintf(&buf[count], "\n");
260
261 return count;
262
263}
264
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100265#define cpufreq_attr_available_freq(_name) \
266struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
267__ATTR_RO(_name##_frequencies)
268
Lee Jones19231a82020-07-15 09:26:22 +0100269/*
Geert Uytterhoevena95d8f52021-03-26 13:07:21 +0100270 * scaling_available_frequencies_show - show available normal frequencies for
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100271 * the specified CPU
272 */
273static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
274 char *buf)
275{
276 return show_available_freqs(policy, buf, false);
277}
278cpufreq_attr_available_freq(scaling_available);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
280
Lee Jones19231a82020-07-15 09:26:22 +0100281/*
Geert Uytterhoevena95d8f52021-03-26 13:07:21 +0100282 * scaling_boost_frequencies_show - show available boost frequencies for
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100283 * the specified CPU
284 */
285static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
286 char *buf)
287{
288 return show_available_freqs(policy, buf, true);
289}
290cpufreq_attr_available_freq(scaling_boost);
291EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
292
Viresh Kumar18434512013-10-03 20:27:55 +0530293struct freq_attr *cpufreq_generic_attr[] = {
294 &cpufreq_freq_attr_scaling_available_freqs,
295 NULL,
296};
297EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
298
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530299static 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
Viresh Kumard417e062018-02-22 11:29:44 +0530354int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy)
355{
356 int ret;
357
358 if (!policy->freq_table)
359 return 0;
360
361 ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
362 if (ret)
363 return ret;
364
365 return set_freq_table_sorted(policy);
366}
367
Dave Jones97acec52009-01-18 01:56:41 -0500368MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
369MODULE_DESCRIPTION("CPUfreq frequency table helpers");
370MODULE_LICENSE("GPL");