Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/drivers/cpufreq/freq_table.c |
| 4 | * |
| 5 | * Copyright (C) 2002 - 2003 Dominik Brodowski |
| 6 | */ |
| 7 | |
Viresh Kumar | db70115 | 2012-10-23 01:29:03 +0200 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/cpufreq.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 11 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | /********************************************************************* |
| 14 | * FREQUENCY TABLE HELPERS * |
| 15 | *********************************************************************/ |
| 16 | |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 17 | bool 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 | } |
| 30 | EXPORT_SYMBOL_GPL(policy_has_boost_freq); |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy, |
| 33 | struct cpufreq_frequency_table *table) |
| 34 | { |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 35 | struct cpufreq_frequency_table *pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | unsigned int min_freq = ~0; |
| 37 | unsigned int max_freq = 0; |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 38 | unsigned int freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 40 | cpufreq_for_each_valid_entry(pos, table) { |
| 41 | freq = pos->frequency; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 43 | if (!cpufreq_boost_enabled() |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 44 | && (pos->flags & CPUFREQ_BOOST_FREQ)) |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 45 | continue; |
| 46 | |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 47 | pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | 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. Wysocki | 538b018 | 2021-02-15 20:24:46 +0100 | [diff] [blame] | 55 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | if (policy->min == ~0) |
| 64 | return -EINVAL; |
| 65 | else |
| 66 | return 0; |
| 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Rafael J. Wysocki | 1e4f63a | 2020-01-26 23:40:11 +0100 | [diff] [blame] | 69 | int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | struct cpufreq_frequency_table *table) |
| 71 | { |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 72 | struct cpufreq_frequency_table *pos; |
| 73 | unsigned int freq, next_larger = ~0; |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 74 | bool found = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 76 | pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n", |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 77 | policy->min, policy->max, policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame] | 79 | cpufreq_verify_within_cpu_limits(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 81 | cpufreq_for_each_valid_entry(pos, table) { |
| 82 | freq = pos->frequency; |
| 83 | |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 84 | if ((freq >= policy->min) && (freq <= policy->max)) { |
| 85 | found = true; |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | if ((next_larger > freq) && (freq > policy->max)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | next_larger = freq; |
| 91 | } |
| 92 | |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 93 | if (!found) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | policy->max = next_larger; |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame] | 95 | cpufreq_verify_within_cpu_limits(policy); |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 98 | pr_debug("verification lead to (%u - %u kHz) for cpu %u\n", |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 99 | policy->min, policy->max, policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify); |
| 104 | |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 105 | /* |
Viresh Kumar | e0b3165 | 2014-03-10 14:53:33 +0530 | [diff] [blame] | 106 | * Generic routine to verify policy & frequency table, requires driver to set |
| 107 | * policy->freq_table prior to it. |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 108 | */ |
Rafael J. Wysocki | 1e4f63a | 2020-01-26 23:40:11 +0100 | [diff] [blame] | 109 | int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy) |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 110 | { |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 111 | if (!policy->freq_table) |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 112 | return -ENODEV; |
| 113 | |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 114 | return cpufreq_frequency_table_verify(policy, policy->freq_table); |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 115 | } |
| 116 | EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Viresh Kumar | da0c6dc | 2016-06-27 16:04:07 +0530 | [diff] [blame] | 118 | int cpufreq_table_index_unsorted(struct cpufreq_policy *policy, |
| 119 | unsigned int target_freq, |
| 120 | unsigned int relation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Dave Jones | 484944a | 2006-05-30 18:09:31 -0400 | [diff] [blame] | 122 | struct cpufreq_frequency_table optimal = { |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 123 | .driver_data = ~0, |
Dave Jones | 484944a | 2006-05-30 18:09:31 -0400 | [diff] [blame] | 124 | .frequency = 0, |
| 125 | }; |
| 126 | struct cpufreq_frequency_table suboptimal = { |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 127 | .driver_data = ~0, |
Dave Jones | 484944a | 2006-05-30 18:09:31 -0400 | [diff] [blame] | 128 | .frequency = 0, |
| 129 | }; |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 130 | struct cpufreq_frequency_table *pos; |
Viresh Kumar | 7ab4aab | 2016-06-03 10:58:49 +0530 | [diff] [blame] | 131 | struct cpufreq_frequency_table *table = policy->freq_table; |
Stratos Karafotis | 5b0c0b1 | 2014-06-30 19:59:33 +0300 | [diff] [blame] | 132 | unsigned int freq, diff, i = 0; |
Viresh Kumar | d218ed7 | 2016-06-03 10:58:51 +0530 | [diff] [blame] | 133 | int index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 135 | pr_debug("request for target %u kHz (relation: %u) for cpu %u\n", |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 136 | target_freq, relation, policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | switch (relation) { |
| 139 | case CPUFREQ_RELATION_H: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | suboptimal.frequency = ~0; |
| 141 | break; |
| 142 | case CPUFREQ_RELATION_L: |
Stratos Karafotis | 5b0c0b1 | 2014-06-30 19:59:33 +0300 | [diff] [blame] | 143 | case CPUFREQ_RELATION_C: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | optimal.frequency = ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | |
Dominik Brodowski | ffd81dc | 2018-01-30 06:42:37 +0100 | [diff] [blame] | 148 | cpufreq_for_each_valid_entry_idx(pos, table, i) { |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 149 | freq = pos->frequency; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | if ((freq < policy->min) || (freq > policy->max)) |
| 152 | continue; |
Stratos Karafotis | 1e49885 | 2014-05-14 21:05:52 +0300 | [diff] [blame] | 153 | if (freq == target_freq) { |
| 154 | optimal.driver_data = i; |
| 155 | break; |
| 156 | } |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 157 | switch (relation) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | case CPUFREQ_RELATION_H: |
Stratos Karafotis | 1e49885 | 2014-05-14 21:05:52 +0300 | [diff] [blame] | 159 | if (freq < target_freq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | if (freq >= optimal.frequency) { |
| 161 | optimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 162 | optimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | } else { |
| 165 | if (freq <= suboptimal.frequency) { |
| 166 | suboptimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 167 | suboptimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | break; |
| 171 | case CPUFREQ_RELATION_L: |
Stratos Karafotis | 1e49885 | 2014-05-14 21:05:52 +0300 | [diff] [blame] | 172 | if (freq > target_freq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (freq <= optimal.frequency) { |
| 174 | optimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 175 | optimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | } else { |
| 178 | if (freq >= suboptimal.frequency) { |
| 179 | suboptimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 180 | suboptimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | break; |
Stratos Karafotis | 5b0c0b1 | 2014-06-30 19:59:33 +0300 | [diff] [blame] | 184 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 195 | if (optimal.driver_data > i) { |
Viresh Kumar | d218ed7 | 2016-06-03 10:58:51 +0530 | [diff] [blame] | 196 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } else |
Viresh Kumar | d218ed7 | 2016-06-03 10:58:51 +0530 | [diff] [blame] | 203 | index = optimal.driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Viresh Kumar | d218ed7 | 2016-06-03 10:58:51 +0530 | [diff] [blame] | 205 | pr_debug("target index is %u, freq is:%u kHz\n", index, |
| 206 | table[index].frequency); |
| 207 | return index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
Viresh Kumar | da0c6dc | 2016-06-27 16:04:07 +0530 | [diff] [blame] | 209 | EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Viresh Kumar | d391669 | 2013-12-03 11:20:46 +0530 | [diff] [blame] | 211 | int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, |
| 212 | unsigned int freq) |
| 213 | { |
Viresh Kumar | f8bfc11 | 2016-06-03 10:58:47 +0530 | [diff] [blame] | 214 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; |
Dominik Brodowski | ffd81dc | 2018-01-30 06:42:37 +0100 | [diff] [blame] | 215 | int idx; |
Viresh Kumar | d391669 | 2013-12-03 11:20:46 +0530 | [diff] [blame] | 216 | |
Viresh Kumar | d391669 | 2013-12-03 11:20:46 +0530 | [diff] [blame] | 217 | if (unlikely(!table)) { |
| 218 | pr_debug("%s: Unable to find frequency table\n", __func__); |
| 219 | return -ENOENT; |
| 220 | } |
| 221 | |
Dominik Brodowski | ffd81dc | 2018-01-30 06:42:37 +0100 | [diff] [blame] | 222 | cpufreq_for_each_valid_entry_idx(pos, table, idx) |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 223 | if (pos->frequency == freq) |
Dominik Brodowski | ffd81dc | 2018-01-30 06:42:37 +0100 | [diff] [blame] | 224 | return idx; |
Viresh Kumar | d391669 | 2013-12-03 11:20:46 +0530 | [diff] [blame] | 225 | |
| 226 | return -EINVAL; |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index); |
| 229 | |
Lee Jones | 19231a8 | 2020-07-15 09:26:22 +0100 | [diff] [blame] | 230 | /* |
Fenghua Yu | e32d22f | 2007-11-21 14:52:15 -0800 | [diff] [blame] | 231 | * show_available_freqs - show available frequencies for the specified CPU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | */ |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 233 | static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf, |
| 234 | bool show_boost) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | ssize_t count = 0; |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 237 | struct cpufreq_frequency_table *pos, *table = policy->freq_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Viresh Kumar | e0b3165 | 2014-03-10 14:53:33 +0530 | [diff] [blame] | 239 | if (!table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return -ENODEV; |
| 241 | |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 242 | cpufreq_for_each_valid_entry(pos, table) { |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 243 | /* |
| 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 Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 254 | if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ)) |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 255 | continue; |
| 256 | |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 257 | count += sprintf(&buf[count], "%d ", pos->frequency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | count += sprintf(&buf[count], "\n"); |
| 260 | |
| 261 | return count; |
| 262 | |
| 263 | } |
| 264 | |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 265 | #define cpufreq_attr_available_freq(_name) \ |
| 266 | struct freq_attr cpufreq_freq_attr_##_name##_freqs = \ |
| 267 | __ATTR_RO(_name##_frequencies) |
| 268 | |
Lee Jones | 19231a8 | 2020-07-15 09:26:22 +0100 | [diff] [blame] | 269 | /* |
Geert Uytterhoeven | a95d8f5 | 2021-03-26 13:07:21 +0100 | [diff] [blame] | 270 | * scaling_available_frequencies_show - show available normal frequencies for |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 271 | * the specified CPU |
| 272 | */ |
| 273 | static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy, |
| 274 | char *buf) |
| 275 | { |
| 276 | return show_available_freqs(policy, buf, false); |
| 277 | } |
| 278 | cpufreq_attr_available_freq(scaling_available); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs); |
| 280 | |
Lee Jones | 19231a8 | 2020-07-15 09:26:22 +0100 | [diff] [blame] | 281 | /* |
Geert Uytterhoeven | a95d8f5 | 2021-03-26 13:07:21 +0100 | [diff] [blame] | 282 | * scaling_boost_frequencies_show - show available boost frequencies for |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 283 | * the specified CPU |
| 284 | */ |
| 285 | static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy, |
| 286 | char *buf) |
| 287 | { |
| 288 | return show_available_freqs(policy, buf, true); |
| 289 | } |
| 290 | cpufreq_attr_available_freq(scaling_boost); |
| 291 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs); |
| 292 | |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 293 | struct freq_attr *cpufreq_generic_attr[] = { |
| 294 | &cpufreq_freq_attr_scaling_available_freqs, |
| 295 | NULL, |
| 296 | }; |
| 297 | EXPORT_SYMBOL_GPL(cpufreq_generic_attr); |
| 298 | |
Viresh Kumar | da0c6dc | 2016-06-27 16:04:07 +0530 | [diff] [blame] | 299 | static 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 Kumar | d417e06 | 2018-02-22 11:29:44 +0530 | [diff] [blame] | 354 | int 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 Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 368 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>"); |
| 369 | MODULE_DESCRIPTION("CPUfreq frequency table helpers"); |
| 370 | MODULE_LICENSE("GPL"); |