Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/cpufreq/freq_table.c |
| 3 | * |
| 4 | * Copyright (C) 2002 - 2003 Dominik Brodowski |
Dominik Brodowski | 4f74369 | 2008-05-22 08:52:05 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Viresh Kumar | db70115 | 2012-10-23 01:29:03 +0200 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/cpufreq.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 15 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | /********************************************************************* |
| 18 | * FREQUENCY TABLE HELPERS * |
| 19 | *********************************************************************/ |
| 20 | |
| 21 | int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy, |
| 22 | struct cpufreq_frequency_table *table) |
| 23 | { |
| 24 | unsigned int min_freq = ~0; |
| 25 | unsigned int max_freq = 0; |
Dave Jones | 355eb31 | 2006-05-30 17:58:41 -0400 | [diff] [blame] | 26 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 28 | for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | unsigned int freq = table[i].frequency; |
| 30 | if (freq == CPUFREQ_ENTRY_INVALID) { |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 31 | pr_debug("table entry %u is invalid, skipping\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | continue; |
| 34 | } |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 35 | if (!cpufreq_boost_enabled() |
| 36 | && table[i].driver_data == CPUFREQ_BOOST_FREQ) |
| 37 | continue; |
| 38 | |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 39 | pr_debug("table entry %u: %u kHz, %u driver_data\n", |
| 40 | i, freq, table[i].driver_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | if (freq < min_freq) |
| 42 | min_freq = freq; |
| 43 | if (freq > max_freq) |
| 44 | max_freq = freq; |
| 45 | } |
| 46 | |
| 47 | policy->min = policy->cpuinfo.min_freq = min_freq; |
| 48 | policy->max = policy->cpuinfo.max_freq = max_freq; |
| 49 | |
| 50 | if (policy->min == ~0) |
| 51 | return -EINVAL; |
| 52 | else |
| 53 | return 0; |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo); |
| 56 | |
| 57 | |
| 58 | int cpufreq_frequency_table_verify(struct cpufreq_policy *policy, |
| 59 | struct cpufreq_frequency_table *table) |
| 60 | { |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 61 | unsigned int next_larger = ~0, freq, i = 0; |
| 62 | bool found = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 64 | 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] | 65 | policy->min, policy->max, policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame] | 67 | cpufreq_verify_within_cpu_limits(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 69 | for (; freq = table[i].frequency, freq != CPUFREQ_TABLE_END; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if (freq == CPUFREQ_ENTRY_INVALID) |
| 71 | continue; |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 72 | if ((freq >= policy->min) && (freq <= policy->max)) { |
| 73 | found = true; |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | if ((next_larger > freq) && (freq > policy->max)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | next_larger = freq; |
| 79 | } |
| 80 | |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 81 | if (!found) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | policy->max = next_larger; |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame] | 83 | cpufreq_verify_within_cpu_limits(policy); |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 86 | 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] | 87 | policy->min, policy->max, policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify); |
| 92 | |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 93 | /* |
Viresh Kumar | e0b3165 | 2014-03-10 14:53:33 +0530 | [diff] [blame^] | 94 | * Generic routine to verify policy & frequency table, requires driver to set |
| 95 | * policy->freq_table prior to it. |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 96 | */ |
| 97 | int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy) |
| 98 | { |
| 99 | struct cpufreq_frequency_table *table = |
| 100 | cpufreq_frequency_get_table(policy->cpu); |
| 101 | if (!table) |
| 102 | return -ENODEV; |
| 103 | |
| 104 | return cpufreq_frequency_table_verify(policy, table); |
| 105 | } |
| 106 | EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | int cpufreq_frequency_table_target(struct cpufreq_policy *policy, |
| 109 | struct cpufreq_frequency_table *table, |
| 110 | unsigned int target_freq, |
| 111 | unsigned int relation, |
| 112 | unsigned int *index) |
| 113 | { |
Dave Jones | 484944a | 2006-05-30 18:09:31 -0400 | [diff] [blame] | 114 | struct cpufreq_frequency_table optimal = { |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 115 | .driver_data = ~0, |
Dave Jones | 484944a | 2006-05-30 18:09:31 -0400 | [diff] [blame] | 116 | .frequency = 0, |
| 117 | }; |
| 118 | struct cpufreq_frequency_table suboptimal = { |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 119 | .driver_data = ~0, |
Dave Jones | 484944a | 2006-05-30 18:09:31 -0400 | [diff] [blame] | 120 | .frequency = 0, |
| 121 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | unsigned int i; |
| 123 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 124 | 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] | 125 | target_freq, relation, policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | switch (relation) { |
| 128 | case CPUFREQ_RELATION_H: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | suboptimal.frequency = ~0; |
| 130 | break; |
| 131 | case CPUFREQ_RELATION_L: |
| 132 | optimal.frequency = ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | break; |
| 134 | } |
| 135 | |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 136 | for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | unsigned int freq = table[i].frequency; |
| 138 | if (freq == CPUFREQ_ENTRY_INVALID) |
| 139 | continue; |
| 140 | if ((freq < policy->min) || (freq > policy->max)) |
| 141 | continue; |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 142 | switch (relation) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | case CPUFREQ_RELATION_H: |
| 144 | if (freq <= target_freq) { |
| 145 | if (freq >= optimal.frequency) { |
| 146 | optimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 147 | optimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | } else { |
| 150 | if (freq <= suboptimal.frequency) { |
| 151 | suboptimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 152 | suboptimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | break; |
| 156 | case CPUFREQ_RELATION_L: |
| 157 | if (freq >= target_freq) { |
| 158 | if (freq <= optimal.frequency) { |
| 159 | optimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 160 | optimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | } else { |
| 163 | if (freq >= suboptimal.frequency) { |
| 164 | suboptimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 165 | suboptimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | } |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 171 | if (optimal.driver_data > i) { |
| 172 | if (suboptimal.driver_data > i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return -EINVAL; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 174 | *index = suboptimal.driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } else |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 176 | *index = optimal.driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 178 | pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency, |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 179 | table[*index].driver_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target); |
| 184 | |
Viresh Kumar | d391669 | 2013-12-03 11:20:46 +0530 | [diff] [blame] | 185 | int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, |
| 186 | unsigned int freq) |
| 187 | { |
| 188 | struct cpufreq_frequency_table *table; |
| 189 | int i; |
| 190 | |
| 191 | table = cpufreq_frequency_get_table(policy->cpu); |
| 192 | if (unlikely(!table)) { |
| 193 | pr_debug("%s: Unable to find frequency table\n", __func__); |
| 194 | return -ENOENT; |
| 195 | } |
| 196 | |
| 197 | for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) { |
| 198 | if (table[i].frequency == freq) |
| 199 | return i; |
| 200 | } |
| 201 | |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index); |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | /** |
Fenghua Yu | e32d22f | 2007-11-21 14:52:15 -0800 | [diff] [blame] | 207 | * show_available_freqs - show available frequencies for the specified CPU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | */ |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 209 | static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf, |
| 210 | bool show_boost) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
| 212 | unsigned int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | ssize_t count = 0; |
Viresh Kumar | e0b3165 | 2014-03-10 14:53:33 +0530 | [diff] [blame^] | 214 | struct cpufreq_frequency_table *table = policy->freq_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Viresh Kumar | e0b3165 | 2014-03-10 14:53:33 +0530 | [diff] [blame^] | 216 | if (!table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return -ENODEV; |
| 218 | |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 219 | for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (table[i].frequency == CPUFREQ_ENTRY_INVALID) |
| 221 | continue; |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 222 | /* |
| 223 | * show_boost = true and driver_data = BOOST freq |
| 224 | * display BOOST freqs |
| 225 | * |
| 226 | * show_boost = false and driver_data = BOOST freq |
| 227 | * show_boost = true and driver_data != BOOST freq |
| 228 | * continue - do not display anything |
| 229 | * |
| 230 | * show_boost = false and driver_data != BOOST freq |
| 231 | * display NON BOOST freqs |
| 232 | */ |
| 233 | if (show_boost ^ (table[i].driver_data == CPUFREQ_BOOST_FREQ)) |
| 234 | continue; |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | count += sprintf(&buf[count], "%d ", table[i].frequency); |
| 237 | } |
| 238 | count += sprintf(&buf[count], "\n"); |
| 239 | |
| 240 | return count; |
| 241 | |
| 242 | } |
| 243 | |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 244 | #define cpufreq_attr_available_freq(_name) \ |
| 245 | struct freq_attr cpufreq_freq_attr_##_name##_freqs = \ |
| 246 | __ATTR_RO(_name##_frequencies) |
| 247 | |
| 248 | /** |
| 249 | * show_scaling_available_frequencies - show available normal frequencies for |
| 250 | * the specified CPU |
| 251 | */ |
| 252 | static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy, |
| 253 | char *buf) |
| 254 | { |
| 255 | return show_available_freqs(policy, buf, false); |
| 256 | } |
| 257 | cpufreq_attr_available_freq(scaling_available); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs); |
| 259 | |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 260 | /** |
| 261 | * show_available_boost_freqs - show available boost frequencies for |
| 262 | * the specified CPU |
| 263 | */ |
| 264 | static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy, |
| 265 | char *buf) |
| 266 | { |
| 267 | return show_available_freqs(policy, buf, true); |
| 268 | } |
| 269 | cpufreq_attr_available_freq(scaling_boost); |
| 270 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs); |
| 271 | |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 272 | struct freq_attr *cpufreq_generic_attr[] = { |
| 273 | &cpufreq_freq_attr_scaling_available_freqs, |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 274 | #ifdef CONFIG_CPU_FREQ_BOOST_SW |
| 275 | &cpufreq_freq_attr_scaling_boost_freqs, |
| 276 | #endif |
Viresh Kumar | 1843451 | 2013-10-03 20:27:55 +0530 | [diff] [blame] | 277 | NULL, |
| 278 | }; |
| 279 | EXPORT_SYMBOL_GPL(cpufreq_generic_attr); |
| 280 | |
Viresh Kumar | 27047a6 | 2013-09-16 18:56:03 +0530 | [diff] [blame] | 281 | int cpufreq_table_validate_and_show(struct cpufreq_policy *policy, |
| 282 | struct cpufreq_frequency_table *table) |
| 283 | { |
| 284 | int ret = cpufreq_frequency_table_cpuinfo(policy, table); |
| 285 | |
| 286 | if (!ret) |
Viresh Kumar | e0b3165 | 2014-03-10 14:53:33 +0530 | [diff] [blame^] | 287 | policy->freq_table = table; |
Viresh Kumar | 27047a6 | 2013-09-16 18:56:03 +0530 | [diff] [blame] | 288 | |
| 289 | return ret; |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show); |
| 292 | |
Viresh Kumar | e0b3165 | 2014-03-10 14:53:33 +0530 | [diff] [blame^] | 293 | struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu); |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) |
| 296 | { |
Viresh Kumar | e0b3165 | 2014-03-10 14:53:33 +0530 | [diff] [blame^] | 297 | struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); |
| 298 | return policy ? policy->freq_table : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table); |
| 301 | |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 302 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>"); |
| 303 | MODULE_DESCRIPTION("CPUfreq frequency table helpers"); |
| 304 | MODULE_LICENSE("GPL"); |