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