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