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 | } |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 35 | pr_debug("table entry %u: %u kHz, %u driver_data\n", |
| 36 | i, freq, table[i].driver_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | if (freq < min_freq) |
| 38 | min_freq = freq; |
| 39 | if (freq > max_freq) |
| 40 | max_freq = freq; |
| 41 | } |
| 42 | |
| 43 | policy->min = policy->cpuinfo.min_freq = min_freq; |
| 44 | policy->max = policy->cpuinfo.max_freq = max_freq; |
| 45 | |
| 46 | if (policy->min == ~0) |
| 47 | return -EINVAL; |
| 48 | else |
| 49 | return 0; |
| 50 | } |
| 51 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo); |
| 52 | |
| 53 | |
| 54 | int cpufreq_frequency_table_verify(struct cpufreq_policy *policy, |
| 55 | struct cpufreq_frequency_table *table) |
| 56 | { |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 57 | unsigned int next_larger = ~0, freq, i = 0; |
| 58 | bool found = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 60 | 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] | 61 | policy->min, policy->max, policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame^] | 63 | cpufreq_verify_within_cpu_limits(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 65 | for (; freq = table[i].frequency, freq != CPUFREQ_TABLE_END; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | if (freq == CPUFREQ_ENTRY_INVALID) |
| 67 | continue; |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 68 | if ((freq >= policy->min) && (freq <= policy->max)) { |
| 69 | found = true; |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | if ((next_larger > freq) && (freq > policy->max)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | next_larger = freq; |
| 75 | } |
| 76 | |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 77 | if (!found) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | policy->max = next_larger; |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame^] | 79 | cpufreq_verify_within_cpu_limits(policy); |
Viresh Kumar | 77db50c | 2013-10-02 14:13:15 +0530 | [diff] [blame] | 80 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 82 | 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] | 83 | policy->min, policy->max, policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify); |
| 88 | |
| 89 | |
| 90 | int cpufreq_frequency_table_target(struct cpufreq_policy *policy, |
| 91 | struct cpufreq_frequency_table *table, |
| 92 | unsigned int target_freq, |
| 93 | unsigned int relation, |
| 94 | unsigned int *index) |
| 95 | { |
Dave Jones | 484944a | 2006-05-30 18:09:31 -0400 | [diff] [blame] | 96 | struct cpufreq_frequency_table optimal = { |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 97 | .driver_data = ~0, |
Dave Jones | 484944a | 2006-05-30 18:09:31 -0400 | [diff] [blame] | 98 | .frequency = 0, |
| 99 | }; |
| 100 | struct cpufreq_frequency_table suboptimal = { |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 101 | .driver_data = ~0, |
Dave Jones | 484944a | 2006-05-30 18:09:31 -0400 | [diff] [blame] | 102 | .frequency = 0, |
| 103 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | unsigned int i; |
| 105 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 106 | 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] | 107 | target_freq, relation, policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | switch (relation) { |
| 110 | case CPUFREQ_RELATION_H: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | suboptimal.frequency = ~0; |
| 112 | break; |
| 113 | case CPUFREQ_RELATION_L: |
| 114 | optimal.frequency = ~0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 118 | for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | unsigned int freq = table[i].frequency; |
| 120 | if (freq == CPUFREQ_ENTRY_INVALID) |
| 121 | continue; |
| 122 | if ((freq < policy->min) || (freq > policy->max)) |
| 123 | continue; |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 124 | switch (relation) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | case CPUFREQ_RELATION_H: |
| 126 | if (freq <= target_freq) { |
| 127 | if (freq >= optimal.frequency) { |
| 128 | optimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 129 | optimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | } else { |
| 132 | if (freq <= suboptimal.frequency) { |
| 133 | suboptimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 134 | suboptimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | break; |
| 138 | case CPUFREQ_RELATION_L: |
| 139 | if (freq >= target_freq) { |
| 140 | if (freq <= optimal.frequency) { |
| 141 | optimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 142 | optimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | } else { |
| 145 | if (freq >= suboptimal.frequency) { |
| 146 | suboptimal.frequency = freq; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 147 | suboptimal.driver_data = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | break; |
| 151 | } |
| 152 | } |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 153 | if (optimal.driver_data > i) { |
| 154 | if (suboptimal.driver_data > i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return -EINVAL; |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 156 | *index = suboptimal.driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } else |
Viresh Kumar | 5070158 | 2013-03-30 16:25:15 +0530 | [diff] [blame] | 158 | *index = optimal.driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 160 | 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] | 161 | table[*index].driver_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target); |
| 166 | |
Tejun Heo | f162506 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 167 | static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | /** |
Fenghua Yu | e32d22f | 2007-11-21 14:52:15 -0800 | [diff] [blame] | 169 | * show_available_freqs - show available frequencies for the specified CPU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | */ |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 171 | static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | unsigned int i = 0; |
| 174 | unsigned int cpu = policy->cpu; |
| 175 | ssize_t count = 0; |
| 176 | struct cpufreq_frequency_table *table; |
| 177 | |
Tejun Heo | f162506 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 178 | if (!per_cpu(cpufreq_show_table, cpu)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return -ENODEV; |
| 180 | |
Tejun Heo | f162506 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 181 | table = per_cpu(cpufreq_show_table, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 183 | for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | if (table[i].frequency == CPUFREQ_ENTRY_INVALID) |
| 185 | continue; |
| 186 | count += sprintf(&buf[count], "%d ", table[i].frequency); |
| 187 | } |
| 188 | count += sprintf(&buf[count], "\n"); |
| 189 | |
| 190 | return count; |
| 191 | |
| 192 | } |
| 193 | |
| 194 | struct freq_attr cpufreq_freq_attr_scaling_available_freqs = { |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 195 | .attr = { .name = "scaling_available_frequencies", |
| 196 | .mode = 0444, |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 197 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | .show = show_available_freqs, |
| 199 | }; |
| 200 | EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs); |
| 201 | |
| 202 | /* |
| 203 | * if you use these, you must assure that the frequency table is valid |
| 204 | * all the time between get_attr and put_attr! |
| 205 | */ |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 206 | void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | unsigned int cpu) |
| 208 | { |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 209 | pr_debug("setting show_table for cpu %u to %p\n", cpu, table); |
Tejun Heo | f162506 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 210 | per_cpu(cpufreq_show_table, cpu) = table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr); |
| 213 | |
| 214 | void cpufreq_frequency_table_put_attr(unsigned int cpu) |
| 215 | { |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 216 | pr_debug("clearing show_table for cpu %u\n", cpu); |
Tejun Heo | f162506 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 217 | per_cpu(cpufreq_show_table, cpu) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr); |
| 220 | |
Viresh Kumar | 27047a6 | 2013-09-16 18:56:03 +0530 | [diff] [blame] | 221 | int cpufreq_table_validate_and_show(struct cpufreq_policy *policy, |
| 222 | struct cpufreq_frequency_table *table) |
| 223 | { |
| 224 | int ret = cpufreq_frequency_table_cpuinfo(policy, table); |
| 225 | |
| 226 | if (!ret) |
| 227 | cpufreq_frequency_table_get_attr(table, policy->cpu); |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show); |
| 232 | |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 233 | void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy) |
| 234 | { |
| 235 | pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n", |
| 236 | policy->cpu, policy->last_cpu); |
| 237 | per_cpu(cpufreq_show_table, policy->cpu) = per_cpu(cpufreq_show_table, |
| 238 | policy->last_cpu); |
| 239 | per_cpu(cpufreq_show_table, policy->last_cpu) = NULL; |
| 240 | } |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) |
| 243 | { |
Tejun Heo | f162506 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 244 | return per_cpu(cpufreq_show_table, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table); |
| 247 | |
Dave Jones | 97acec5 | 2009-01-18 01:56:41 -0500 | [diff] [blame] | 248 | MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>"); |
| 249 | MODULE_DESCRIPTION("CPUfreq frequency table helpers"); |
| 250 | MODULE_LICENSE("GPL"); |