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