1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/drivers/cpufreq/freq_table.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 2002 - 2003 Dominik Brodowski
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
8db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9db701151SViresh Kumar
101da177e4SLinus Torvalds #include <linux/cpufreq.h>
115ff0a268SViresh Kumar #include <linux/module.h>
121da177e4SLinus Torvalds
131da177e4SLinus Torvalds /*********************************************************************
141da177e4SLinus Torvalds * FREQUENCY TABLE HELPERS *
151da177e4SLinus Torvalds *********************************************************************/
161da177e4SLinus Torvalds
policy_has_boost_freq(struct cpufreq_policy * policy)1744139ed4SViresh Kumar bool policy_has_boost_freq(struct cpufreq_policy *policy)
1844139ed4SViresh Kumar {
1944139ed4SViresh Kumar struct cpufreq_frequency_table *pos, *table = policy->freq_table;
2044139ed4SViresh Kumar
2144139ed4SViresh Kumar if (!table)
2244139ed4SViresh Kumar return false;
2344139ed4SViresh Kumar
2444139ed4SViresh Kumar cpufreq_for_each_valid_entry(pos, table)
2544139ed4SViresh Kumar if (pos->flags & CPUFREQ_BOOST_FREQ)
2644139ed4SViresh Kumar return true;
2744139ed4SViresh Kumar
2844139ed4SViresh Kumar return false;
2944139ed4SViresh Kumar }
3044139ed4SViresh Kumar EXPORT_SYMBOL_GPL(policy_has_boost_freq);
3144139ed4SViresh Kumar
cpufreq_frequency_table_cpuinfo(struct cpufreq_policy * policy,struct cpufreq_frequency_table * table)321da177e4SLinus Torvalds int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
331da177e4SLinus Torvalds struct cpufreq_frequency_table *table)
341da177e4SLinus Torvalds {
35041526f9SStratos Karafotis struct cpufreq_frequency_table *pos;
361da177e4SLinus Torvalds unsigned int min_freq = ~0;
371da177e4SLinus Torvalds unsigned int max_freq = 0;
38041526f9SStratos Karafotis unsigned int freq;
391da177e4SLinus Torvalds
40041526f9SStratos Karafotis cpufreq_for_each_valid_entry(pos, table) {
41041526f9SStratos Karafotis freq = pos->frequency;
421da177e4SLinus Torvalds
436f19efc0SLukasz Majewski if ((!cpufreq_boost_enabled() || !policy->boost_enabled)
44041526f9SStratos Karafotis && (pos->flags & CPUFREQ_BOOST_FREQ))
456f19efc0SLukasz Majewski continue;
466f19efc0SLukasz Majewski
47041526f9SStratos Karafotis pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
481da177e4SLinus Torvalds if (freq < min_freq)
491da177e4SLinus Torvalds min_freq = freq;
501da177e4SLinus Torvalds if (freq > max_freq)
511da177e4SLinus Torvalds max_freq = freq;
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds
541da177e4SLinus Torvalds policy->min = policy->cpuinfo.min_freq = min_freq;
55538b0188SRafael J. Wysocki policy->max = max_freq;
56538b0188SRafael J. Wysocki /*
57538b0188SRafael J. Wysocki * If the driver has set its own cpuinfo.max_freq above max_freq, leave
58538b0188SRafael J. Wysocki * it as is.
59538b0188SRafael J. Wysocki */
60538b0188SRafael J. Wysocki if (policy->cpuinfo.max_freq < max_freq)
611da177e4SLinus Torvalds policy->max = policy->cpuinfo.max_freq = max_freq;
621da177e4SLinus Torvalds
631da177e4SLinus Torvalds if (policy->min == ~0)
641da177e4SLinus Torvalds return -EINVAL;
651da177e4SLinus Torvalds else
661da177e4SLinus Torvalds return 0;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds
cpufreq_frequency_table_verify(struct cpufreq_policy_data * policy,struct cpufreq_frequency_table * table)691e4f63aeSRafael J. Wysocki int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy,
701da177e4SLinus Torvalds struct cpufreq_frequency_table *table)
711da177e4SLinus Torvalds {
72041526f9SStratos Karafotis struct cpufreq_frequency_table *pos;
73041526f9SStratos Karafotis unsigned int freq, next_larger = ~0;
7477db50c4SViresh Kumar bool found = false;
751da177e4SLinus Torvalds
762d06d8c4SDominik Brodowski pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
77e08f5f5bSGautham R Shenoy policy->min, policy->max, policy->cpu);
781da177e4SLinus Torvalds
79be49e346SViresh Kumar cpufreq_verify_within_cpu_limits(policy);
801da177e4SLinus Torvalds
81041526f9SStratos Karafotis cpufreq_for_each_valid_entry(pos, table) {
82041526f9SStratos Karafotis freq = pos->frequency;
83041526f9SStratos Karafotis
8477db50c4SViresh Kumar if ((freq >= policy->min) && (freq <= policy->max)) {
8577db50c4SViresh Kumar found = true;
8677db50c4SViresh Kumar break;
8777db50c4SViresh Kumar }
8877db50c4SViresh Kumar
8977db50c4SViresh Kumar if ((next_larger > freq) && (freq > policy->max))
901da177e4SLinus Torvalds next_larger = freq;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds
9377db50c4SViresh Kumar if (!found) {
941da177e4SLinus Torvalds policy->max = next_larger;
95be49e346SViresh Kumar cpufreq_verify_within_cpu_limits(policy);
9677db50c4SViresh Kumar }
971da177e4SLinus Torvalds
982d06d8c4SDominik Brodowski pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
99e08f5f5bSGautham R Shenoy policy->min, policy->max, policy->cpu);
1001da177e4SLinus Torvalds
1011da177e4SLinus Torvalds return 0;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
1041da177e4SLinus Torvalds
10518434512SViresh Kumar /*
106e0b3165bSViresh Kumar * Generic routine to verify policy & frequency table, requires driver to set
107e0b3165bSViresh Kumar * policy->freq_table prior to it.
10818434512SViresh Kumar */
cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data * policy)1091e4f63aeSRafael J. Wysocki int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy)
11018434512SViresh Kumar {
111f8bfc116SViresh Kumar if (!policy->freq_table)
11218434512SViresh Kumar return -ENODEV;
11318434512SViresh Kumar
114f8bfc116SViresh Kumar return cpufreq_frequency_table_verify(policy, policy->freq_table);
11518434512SViresh Kumar }
11618434512SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
1171da177e4SLinus Torvalds
cpufreq_table_index_unsorted(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)118da0c6dc0SViresh Kumar int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
1191da177e4SLinus Torvalds unsigned int target_freq,
120d218ed77SViresh Kumar unsigned int relation)
1211da177e4SLinus Torvalds {
122484944a5SDave Jones struct cpufreq_frequency_table optimal = {
12350701588SViresh Kumar .driver_data = ~0,
124484944a5SDave Jones .frequency = 0,
125484944a5SDave Jones };
126484944a5SDave Jones struct cpufreq_frequency_table suboptimal = {
12750701588SViresh Kumar .driver_data = ~0,
128484944a5SDave Jones .frequency = 0,
129484944a5SDave Jones };
130041526f9SStratos Karafotis struct cpufreq_frequency_table *pos;
1317ab4aabbSViresh Kumar struct cpufreq_frequency_table *table = policy->freq_table;
1325b0c0b16SStratos Karafotis unsigned int freq, diff, i = 0;
133d218ed77SViresh Kumar int index;
1341da177e4SLinus Torvalds
1352d06d8c4SDominik Brodowski pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
136e08f5f5bSGautham R Shenoy target_freq, relation, policy->cpu);
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds switch (relation) {
1391da177e4SLinus Torvalds case CPUFREQ_RELATION_H:
1401da177e4SLinus Torvalds suboptimal.frequency = ~0;
1411da177e4SLinus Torvalds break;
1421da177e4SLinus Torvalds case CPUFREQ_RELATION_L:
1435b0c0b16SStratos Karafotis case CPUFREQ_RELATION_C:
1441da177e4SLinus Torvalds optimal.frequency = ~0;
1451da177e4SLinus Torvalds break;
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds
148ffd81dcfSDominik Brodowski cpufreq_for_each_valid_entry_idx(pos, table, i) {
149041526f9SStratos Karafotis freq = pos->frequency;
150041526f9SStratos Karafotis
1511da177e4SLinus Torvalds if ((freq < policy->min) || (freq > policy->max))
1521da177e4SLinus Torvalds continue;
1531e498856SStratos Karafotis if (freq == target_freq) {
1541e498856SStratos Karafotis optimal.driver_data = i;
1551e498856SStratos Karafotis break;
1561e498856SStratos Karafotis }
1571da177e4SLinus Torvalds switch (relation) {
1581da177e4SLinus Torvalds case CPUFREQ_RELATION_H:
1591e498856SStratos Karafotis if (freq < target_freq) {
1601da177e4SLinus Torvalds if (freq >= optimal.frequency) {
1611da177e4SLinus Torvalds optimal.frequency = freq;
16250701588SViresh Kumar optimal.driver_data = i;
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds } else {
1651da177e4SLinus Torvalds if (freq <= suboptimal.frequency) {
1661da177e4SLinus Torvalds suboptimal.frequency = freq;
16750701588SViresh Kumar suboptimal.driver_data = i;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds break;
1711da177e4SLinus Torvalds case CPUFREQ_RELATION_L:
1721e498856SStratos Karafotis if (freq > target_freq) {
1731da177e4SLinus Torvalds if (freq <= optimal.frequency) {
1741da177e4SLinus Torvalds optimal.frequency = freq;
17550701588SViresh Kumar optimal.driver_data = i;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds } else {
1781da177e4SLinus Torvalds if (freq >= suboptimal.frequency) {
1791da177e4SLinus Torvalds suboptimal.frequency = freq;
18050701588SViresh Kumar suboptimal.driver_data = i;
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds break;
1845b0c0b16SStratos Karafotis case CPUFREQ_RELATION_C:
1855b0c0b16SStratos Karafotis diff = abs(freq - target_freq);
1865b0c0b16SStratos Karafotis if (diff < optimal.frequency ||
1875b0c0b16SStratos Karafotis (diff == optimal.frequency &&
1885b0c0b16SStratos Karafotis freq > table[optimal.driver_data].frequency)) {
1895b0c0b16SStratos Karafotis optimal.frequency = diff;
1905b0c0b16SStratos Karafotis optimal.driver_data = i;
1915b0c0b16SStratos Karafotis }
1925b0c0b16SStratos Karafotis break;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds }
19550701588SViresh Kumar if (optimal.driver_data > i) {
196d218ed77SViresh Kumar if (suboptimal.driver_data > i) {
197d218ed77SViresh Kumar WARN(1, "Invalid frequency table: %d\n", policy->cpu);
1981da177e4SLinus Torvalds return 0;
1991da177e4SLinus Torvalds }
200d218ed77SViresh Kumar
201d218ed77SViresh Kumar index = suboptimal.driver_data;
202d218ed77SViresh Kumar } else
203d218ed77SViresh Kumar index = optimal.driver_data;
204d218ed77SViresh Kumar
205d218ed77SViresh Kumar pr_debug("target index is %u, freq is:%u kHz\n", index,
206d218ed77SViresh Kumar table[index].frequency);
207d218ed77SViresh Kumar return index;
208d218ed77SViresh Kumar }
209da0c6dc0SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted);
2101da177e4SLinus Torvalds
cpufreq_frequency_table_get_index(struct cpufreq_policy * policy,unsigned int freq)211d3916691SViresh Kumar int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
212d3916691SViresh Kumar unsigned int freq)
213d3916691SViresh Kumar {
214f8bfc116SViresh Kumar struct cpufreq_frequency_table *pos, *table = policy->freq_table;
215ffd81dcfSDominik Brodowski int idx;
216d3916691SViresh Kumar
217d3916691SViresh Kumar if (unlikely(!table)) {
218d3916691SViresh Kumar pr_debug("%s: Unable to find frequency table\n", __func__);
219d3916691SViresh Kumar return -ENOENT;
220d3916691SViresh Kumar }
221d3916691SViresh Kumar
222ffd81dcfSDominik Brodowski cpufreq_for_each_valid_entry_idx(pos, table, idx)
223041526f9SStratos Karafotis if (pos->frequency == freq)
224ffd81dcfSDominik Brodowski return idx;
225d3916691SViresh Kumar
226d3916691SViresh Kumar return -EINVAL;
227d3916691SViresh Kumar }
228d3916691SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
229d3916691SViresh Kumar
23019231a87SLee Jones /*
231e32d22f7SFenghua Yu * show_available_freqs - show available frequencies for the specified CPU
2321da177e4SLinus Torvalds */
show_available_freqs(struct cpufreq_policy * policy,char * buf,bool show_boost)2336f19efc0SLukasz Majewski static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
2346f19efc0SLukasz Majewski bool show_boost)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds ssize_t count = 0;
237041526f9SStratos Karafotis struct cpufreq_frequency_table *pos, *table = policy->freq_table;
2381da177e4SLinus Torvalds
239e0b3165bSViresh Kumar if (!table)
2401da177e4SLinus Torvalds return -ENODEV;
2411da177e4SLinus Torvalds
242041526f9SStratos Karafotis cpufreq_for_each_valid_entry(pos, table) {
2436f19efc0SLukasz Majewski /*
2446f19efc0SLukasz Majewski * show_boost = true and driver_data = BOOST freq
2456f19efc0SLukasz Majewski * display BOOST freqs
2466f19efc0SLukasz Majewski *
2476f19efc0SLukasz Majewski * show_boost = false and driver_data = BOOST freq
2486f19efc0SLukasz Majewski * show_boost = true and driver_data != BOOST freq
2496f19efc0SLukasz Majewski * continue - do not display anything
2506f19efc0SLukasz Majewski *
2516f19efc0SLukasz Majewski * show_boost = false and driver_data != BOOST freq
2526f19efc0SLukasz Majewski * display NON BOOST freqs
2536f19efc0SLukasz Majewski */
254041526f9SStratos Karafotis if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
2556f19efc0SLukasz Majewski continue;
2566f19efc0SLukasz Majewski
257041526f9SStratos Karafotis count += sprintf(&buf[count], "%d ", pos->frequency);
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds count += sprintf(&buf[count], "\n");
2601da177e4SLinus Torvalds
2611da177e4SLinus Torvalds return count;
2621da177e4SLinus Torvalds
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds
2656f19efc0SLukasz Majewski #define cpufreq_attr_available_freq(_name) \
2666f19efc0SLukasz Majewski struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
2676f19efc0SLukasz Majewski __ATTR_RO(_name##_frequencies)
2686f19efc0SLukasz Majewski
26919231a87SLee Jones /*
270*a95d8f58SGeert Uytterhoeven * scaling_available_frequencies_show - show available normal frequencies for
2716f19efc0SLukasz Majewski * the specified CPU
2726f19efc0SLukasz Majewski */
scaling_available_frequencies_show(struct cpufreq_policy * policy,char * buf)2736f19efc0SLukasz Majewski static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
2746f19efc0SLukasz Majewski char *buf)
2756f19efc0SLukasz Majewski {
2766f19efc0SLukasz Majewski return show_available_freqs(policy, buf, false);
2776f19efc0SLukasz Majewski }
2786f19efc0SLukasz Majewski cpufreq_attr_available_freq(scaling_available);
2791da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
2801da177e4SLinus Torvalds
28119231a87SLee Jones /*
282*a95d8f58SGeert Uytterhoeven * scaling_boost_frequencies_show - show available boost frequencies for
2836f19efc0SLukasz Majewski * the specified CPU
2846f19efc0SLukasz Majewski */
scaling_boost_frequencies_show(struct cpufreq_policy * policy,char * buf)2856f19efc0SLukasz Majewski static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
2866f19efc0SLukasz Majewski char *buf)
2876f19efc0SLukasz Majewski {
2886f19efc0SLukasz Majewski return show_available_freqs(policy, buf, true);
2896f19efc0SLukasz Majewski }
2906f19efc0SLukasz Majewski cpufreq_attr_available_freq(scaling_boost);
2916f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
2926f19efc0SLukasz Majewski
29318434512SViresh Kumar struct freq_attr *cpufreq_generic_attr[] = {
29418434512SViresh Kumar &cpufreq_freq_attr_scaling_available_freqs,
29518434512SViresh Kumar NULL,
29618434512SViresh Kumar };
29718434512SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
29818434512SViresh Kumar
set_freq_table_sorted(struct cpufreq_policy * policy)299da0c6dc0SViresh Kumar static int set_freq_table_sorted(struct cpufreq_policy *policy)
300da0c6dc0SViresh Kumar {
301da0c6dc0SViresh Kumar struct cpufreq_frequency_table *pos, *table = policy->freq_table;
302da0c6dc0SViresh Kumar struct cpufreq_frequency_table *prev = NULL;
303da0c6dc0SViresh Kumar int ascending = 0;
304da0c6dc0SViresh Kumar
305da0c6dc0SViresh Kumar policy->freq_table_sorted = CPUFREQ_TABLE_UNSORTED;
306da0c6dc0SViresh Kumar
307da0c6dc0SViresh Kumar cpufreq_for_each_valid_entry(pos, table) {
308da0c6dc0SViresh Kumar if (!prev) {
309da0c6dc0SViresh Kumar prev = pos;
310da0c6dc0SViresh Kumar continue;
311da0c6dc0SViresh Kumar }
312da0c6dc0SViresh Kumar
313da0c6dc0SViresh Kumar if (pos->frequency == prev->frequency) {
314da0c6dc0SViresh Kumar pr_warn("Duplicate freq-table entries: %u\n",
315da0c6dc0SViresh Kumar pos->frequency);
316da0c6dc0SViresh Kumar return -EINVAL;
317da0c6dc0SViresh Kumar }
318da0c6dc0SViresh Kumar
319da0c6dc0SViresh Kumar /* Frequency increased from prev to pos */
320da0c6dc0SViresh Kumar if (pos->frequency > prev->frequency) {
321da0c6dc0SViresh Kumar /* But frequency was decreasing earlier */
322da0c6dc0SViresh Kumar if (ascending < 0) {
323da0c6dc0SViresh Kumar pr_debug("Freq table is unsorted\n");
324da0c6dc0SViresh Kumar return 0;
325da0c6dc0SViresh Kumar }
326da0c6dc0SViresh Kumar
327da0c6dc0SViresh Kumar ascending++;
328da0c6dc0SViresh Kumar } else {
329da0c6dc0SViresh Kumar /* Frequency decreased from prev to pos */
330da0c6dc0SViresh Kumar
331da0c6dc0SViresh Kumar /* But frequency was increasing earlier */
332da0c6dc0SViresh Kumar if (ascending > 0) {
333da0c6dc0SViresh Kumar pr_debug("Freq table is unsorted\n");
334da0c6dc0SViresh Kumar return 0;
335da0c6dc0SViresh Kumar }
336da0c6dc0SViresh Kumar
337da0c6dc0SViresh Kumar ascending--;
338da0c6dc0SViresh Kumar }
339da0c6dc0SViresh Kumar
340da0c6dc0SViresh Kumar prev = pos;
341da0c6dc0SViresh Kumar }
342da0c6dc0SViresh Kumar
343da0c6dc0SViresh Kumar if (ascending > 0)
344da0c6dc0SViresh Kumar policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_ASCENDING;
345da0c6dc0SViresh Kumar else
346da0c6dc0SViresh Kumar policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_DESCENDING;
347da0c6dc0SViresh Kumar
348da0c6dc0SViresh Kumar pr_debug("Freq table is sorted in %s order\n",
349da0c6dc0SViresh Kumar ascending > 0 ? "ascending" : "descending");
350da0c6dc0SViresh Kumar
351da0c6dc0SViresh Kumar return 0;
352da0c6dc0SViresh Kumar }
353da0c6dc0SViresh Kumar
cpufreq_table_validate_and_sort(struct cpufreq_policy * policy)354d417e069SViresh Kumar int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy)
355d417e069SViresh Kumar {
356d417e069SViresh Kumar int ret;
357d417e069SViresh Kumar
358d417e069SViresh Kumar if (!policy->freq_table) {
359d417e069SViresh Kumar /* Freq table must be passed by drivers with target_index() */
360d417e069SViresh Kumar if (has_target_index())
361d417e069SViresh Kumar return -EINVAL;
362d417e069SViresh Kumar
363d417e069SViresh Kumar return 0;
364d417e069SViresh Kumar }
365d417e069SViresh Kumar
366d417e069SViresh Kumar ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
367d417e069SViresh Kumar if (ret)
3681da177e4SLinus Torvalds return ret;
3691da177e4SLinus Torvalds
3701da177e4SLinus Torvalds return set_freq_table_sorted(policy);
371 }
372
373 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
374 MODULE_DESCRIPTION("CPUfreq frequency table helpers");
375