blob: e117b0059123ec3d1a74e3a039bc0335c3f30f4a [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;
55 policy->max = policy->cpuinfo.max_freq = max_freq;
56
57 if (policy->min == ~0)
58 return -EINVAL;
59 else
60 return 0;
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Rafael J. Wysocki1e4f63a2020-01-26 23:40:11 +010063int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct cpufreq_frequency_table *table)
65{
Stratos Karafotis041526f2014-04-25 23:15:38 +030066 struct cpufreq_frequency_table *pos;
67 unsigned int freq, next_larger = ~0;
Viresh Kumar77db50c2013-10-02 14:13:15 +053068 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020070 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053071 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Viresh Kumarbe49e342013-10-02 14:13:19 +053073 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Stratos Karafotis041526f2014-04-25 23:15:38 +030075 cpufreq_for_each_valid_entry(pos, table) {
76 freq = pos->frequency;
77
Viresh Kumar77db50c2013-10-02 14:13:15 +053078 if ((freq >= policy->min) && (freq <= policy->max)) {
79 found = true;
80 break;
81 }
82
83 if ((next_larger > freq) && (freq > policy->max))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 next_larger = freq;
85 }
86
Viresh Kumar77db50c2013-10-02 14:13:15 +053087 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 policy->max = next_larger;
Viresh Kumarbe49e342013-10-02 14:13:19 +053089 cpufreq_verify_within_cpu_limits(policy);
Viresh Kumar77db50c2013-10-02 14:13:15 +053090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020092 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053093 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 return 0;
96}
97EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
98
Viresh Kumar18434512013-10-03 20:27:55 +053099/*
Viresh Kumare0b31652014-03-10 14:53:33 +0530100 * Generic routine to verify policy & frequency table, requires driver to set
101 * policy->freq_table prior to it.
Viresh Kumar18434512013-10-03 20:27:55 +0530102 */
Rafael J. Wysocki1e4f63a2020-01-26 23:40:11 +0100103int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy)
Viresh Kumar18434512013-10-03 20:27:55 +0530104{
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530105 if (!policy->freq_table)
Viresh Kumar18434512013-10-03 20:27:55 +0530106 return -ENODEV;
107
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530108 return cpufreq_frequency_table_verify(policy, policy->freq_table);
Viresh Kumar18434512013-10-03 20:27:55 +0530109}
110EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530112int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
113 unsigned int target_freq,
114 unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Dave Jones484944a2006-05-30 18:09:31 -0400116 struct cpufreq_frequency_table optimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530117 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400118 .frequency = 0,
119 };
120 struct cpufreq_frequency_table suboptimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530121 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400122 .frequency = 0,
123 };
Stratos Karafotis041526f2014-04-25 23:15:38 +0300124 struct cpufreq_frequency_table *pos;
Viresh Kumar7ab4aab2016-06-03 10:58:49 +0530125 struct cpufreq_frequency_table *table = policy->freq_table;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300126 unsigned int freq, diff, i = 0;
Viresh Kumard218ed72016-06-03 10:58:51 +0530127 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200129 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530130 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 switch (relation) {
133 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 suboptimal.frequency = ~0;
135 break;
136 case CPUFREQ_RELATION_L:
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300137 case CPUFREQ_RELATION_C:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 break;
140 }
141
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100142 cpufreq_for_each_valid_entry_idx(pos, table, i) {
Stratos Karafotis041526f2014-04-25 23:15:38 +0300143 freq = pos->frequency;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if ((freq < policy->min) || (freq > policy->max))
146 continue;
Stratos Karafotis1e498852014-05-14 21:05:52 +0300147 if (freq == target_freq) {
148 optimal.driver_data = i;
149 break;
150 }
Dave Jones97acec52009-01-18 01:56:41 -0500151 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 case CPUFREQ_RELATION_H:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300153 if (freq < target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (freq >= optimal.frequency) {
155 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530156 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 } else {
159 if (freq <= suboptimal.frequency) {
160 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530161 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 }
164 break;
165 case CPUFREQ_RELATION_L:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300166 if (freq > target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (freq <= optimal.frequency) {
168 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530169 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 } else {
172 if (freq >= suboptimal.frequency) {
173 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530174 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 }
177 break;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300178 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;
185 }
186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 }
Viresh Kumar50701582013-03-30 16:25:15 +0530189 if (optimal.driver_data > i) {
Viresh Kumard218ed72016-06-03 10:58:51 +0530190 if (suboptimal.driver_data > i) {
191 WARN(1, "Invalid frequency table: %d\n", policy->cpu);
192 return 0;
193 }
194
195 index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 } else
Viresh Kumard218ed72016-06-03 10:58:51 +0530197 index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Viresh Kumard218ed72016-06-03 10:58:51 +0530199 pr_debug("target index is %u, freq is:%u kHz\n", index,
200 table[index].frequency);
201 return index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530203EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Viresh Kumard3916692013-12-03 11:20:46 +0530205int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
206 unsigned int freq)
207{
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530208 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100209 int idx;
Viresh Kumard3916692013-12-03 11:20:46 +0530210
Viresh Kumard3916692013-12-03 11:20:46 +0530211 if (unlikely(!table)) {
212 pr_debug("%s: Unable to find frequency table\n", __func__);
213 return -ENOENT;
214 }
215
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100216 cpufreq_for_each_valid_entry_idx(pos, table, idx)
Stratos Karafotis041526f2014-04-25 23:15:38 +0300217 if (pos->frequency == freq)
Dominik Brodowskiffd81dc2018-01-30 06:42:37 +0100218 return idx;
Viresh Kumard3916692013-12-03 11:20:46 +0530219
220 return -EINVAL;
221}
222EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800225 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100227static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
228 bool show_boost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 ssize_t count = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300231 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Viresh Kumare0b31652014-03-10 14:53:33 +0530233 if (!table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -ENODEV;
235
Stratos Karafotis041526f2014-04-25 23:15:38 +0300236 cpufreq_for_each_valid_entry(pos, table) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100237 /*
238 * show_boost = true and driver_data = BOOST freq
239 * display BOOST freqs
240 *
241 * show_boost = false and driver_data = BOOST freq
242 * show_boost = true and driver_data != BOOST freq
243 * continue - do not display anything
244 *
245 * show_boost = false and driver_data != BOOST freq
246 * display NON BOOST freqs
247 */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300248 if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100249 continue;
250
Stratos Karafotis041526f2014-04-25 23:15:38 +0300251 count += sprintf(&buf[count], "%d ", pos->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 count += sprintf(&buf[count], "\n");
254
255 return count;
256
257}
258
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100259#define cpufreq_attr_available_freq(_name) \
260struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
261__ATTR_RO(_name##_frequencies)
262
263/**
264 * show_scaling_available_frequencies - show available normal frequencies for
265 * the specified CPU
266 */
267static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
268 char *buf)
269{
270 return show_available_freqs(policy, buf, false);
271}
272cpufreq_attr_available_freq(scaling_available);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
274
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100275/**
276 * show_available_boost_freqs - show available boost frequencies for
277 * the specified CPU
278 */
279static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
280 char *buf)
281{
282 return show_available_freqs(policy, buf, true);
283}
284cpufreq_attr_available_freq(scaling_boost);
285EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
286
Viresh Kumar18434512013-10-03 20:27:55 +0530287struct freq_attr *cpufreq_generic_attr[] = {
288 &cpufreq_freq_attr_scaling_available_freqs,
289 NULL,
290};
291EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
292
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530293static int set_freq_table_sorted(struct cpufreq_policy *policy)
294{
295 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
296 struct cpufreq_frequency_table *prev = NULL;
297 int ascending = 0;
298
299 policy->freq_table_sorted = CPUFREQ_TABLE_UNSORTED;
300
301 cpufreq_for_each_valid_entry(pos, table) {
302 if (!prev) {
303 prev = pos;
304 continue;
305 }
306
307 if (pos->frequency == prev->frequency) {
308 pr_warn("Duplicate freq-table entries: %u\n",
309 pos->frequency);
310 return -EINVAL;
311 }
312
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;
319 }
320
321 ascending++;
322 } else {
323 /* Frequency decreased from prev to pos */
324
325 /* But frequency was increasing earlier */
326 if (ascending > 0) {
327 pr_debug("Freq table is unsorted\n");
328 return 0;
329 }
330
331 ascending--;
332 }
333
334 prev = pos;
335 }
336
337 if (ascending > 0)
338 policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_ASCENDING;
339 else
340 policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_DESCENDING;
341
342 pr_debug("Freq table is sorted in %s order\n",
343 ascending > 0 ? "ascending" : "descending");
344
345 return 0;
346}
347
Viresh Kumard417e062018-02-22 11:29:44 +0530348int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy)
349{
350 int ret;
351
352 if (!policy->freq_table)
353 return 0;
354
355 ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
356 if (ret)
357 return ret;
358
359 return set_freq_table_sorted(policy);
360}
361
Dave Jones97acec52009-01-18 01:56:41 -0500362MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
363MODULE_DESCRIPTION("CPUfreq frequency table helpers");
364MODULE_LICENSE("GPL");