blob: 90431cb92804b5cb166c231acd709093cfcf6ff6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/freq_table.c
3 *
4 * Copyright (C) 2002 - 2003 Dominik Brodowski
Dominik Brodowski4f743692008-05-22 08:52:05 +02005 *
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 Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/cpufreq.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017/*********************************************************************
18 * FREQUENCY TABLE HELPERS *
19 *********************************************************************/
20
21int 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 Jones355eb312006-05-30 17:58:41 -040026 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Dave Jones97acec52009-01-18 01:56:41 -050028 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 unsigned int freq = table[i].frequency;
30 if (freq == CPUFREQ_ENTRY_INVALID) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020031 pr_debug("table entry %u is invalid, skipping\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 continue;
34 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020035 pr_debug("table entry %u: %u kHz, %u index\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053036 i, freq, table[i].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 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}
51EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
52
53
54int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
55 struct cpufreq_frequency_table *table)
56{
57 unsigned int next_larger = ~0;
Dave Jones55579762006-05-30 17:59:48 -040058 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned int count = 0;
60
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020061 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053062 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 if (!cpu_online(policy->cpu))
65 return -EINVAL;
66
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053067 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
68 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dave Jones97acec52009-01-18 01:56:41 -050070 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 unsigned int freq = table[i].frequency;
72 if (freq == CPUFREQ_ENTRY_INVALID)
73 continue;
74 if ((freq >= policy->min) && (freq <= policy->max))
75 count++;
76 else if ((next_larger > freq) && (freq > policy->max))
77 next_larger = freq;
78 }
79
80 if (!count)
81 policy->max = next_larger;
82
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053083 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
84 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020086 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053087 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 return 0;
90}
91EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
92
93
94int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
95 struct cpufreq_frequency_table *table,
96 unsigned int target_freq,
97 unsigned int relation,
98 unsigned int *index)
99{
Dave Jones484944a2006-05-30 18:09:31 -0400100 struct cpufreq_frequency_table optimal = {
101 .index = ~0,
102 .frequency = 0,
103 };
104 struct cpufreq_frequency_table suboptimal = {
105 .index = ~0,
106 .frequency = 0,
107 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned int i;
109
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200110 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530111 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 switch (relation) {
114 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 suboptimal.frequency = ~0;
116 break;
117 case CPUFREQ_RELATION_L:
118 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
120 }
121
122 if (!cpu_online(policy->cpu))
123 return -EINVAL;
124
Dave Jones97acec52009-01-18 01:56:41 -0500125 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned int freq = table[i].frequency;
127 if (freq == CPUFREQ_ENTRY_INVALID)
128 continue;
129 if ((freq < policy->min) || (freq > policy->max))
130 continue;
Dave Jones97acec52009-01-18 01:56:41 -0500131 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 case CPUFREQ_RELATION_H:
133 if (freq <= target_freq) {
134 if (freq >= optimal.frequency) {
135 optimal.frequency = freq;
136 optimal.index = i;
137 }
138 } else {
139 if (freq <= suboptimal.frequency) {
140 suboptimal.frequency = freq;
141 suboptimal.index = i;
142 }
143 }
144 break;
145 case CPUFREQ_RELATION_L:
146 if (freq >= target_freq) {
147 if (freq <= optimal.frequency) {
148 optimal.frequency = freq;
149 optimal.index = i;
150 }
151 } else {
152 if (freq >= suboptimal.frequency) {
153 suboptimal.frequency = freq;
154 suboptimal.index = i;
155 }
156 }
157 break;
158 }
159 }
160 if (optimal.index > i) {
161 if (suboptimal.index > i)
162 return -EINVAL;
163 *index = suboptimal.index;
164 } else
165 *index = optimal.index;
166
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200167 pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 table[*index].index);
169
170 return 0;
171}
172EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
173
Tejun Heof1625062009-10-29 22:34:13 +0900174static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800176 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
Dave Jones97acec52009-01-18 01:56:41 -0500178static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 unsigned int i = 0;
181 unsigned int cpu = policy->cpu;
182 ssize_t count = 0;
183 struct cpufreq_frequency_table *table;
184
Tejun Heof1625062009-10-29 22:34:13 +0900185 if (!per_cpu(cpufreq_show_table, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -ENODEV;
187
Tejun Heof1625062009-10-29 22:34:13 +0900188 table = per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Dave Jones97acec52009-01-18 01:56:41 -0500190 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
192 continue;
193 count += sprintf(&buf[count], "%d ", table[i].frequency);
194 }
195 count += sprintf(&buf[count], "\n");
196
197 return count;
198
199}
200
201struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530202 .attr = { .name = "scaling_available_frequencies",
203 .mode = 0444,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530204 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .show = show_available_freqs,
206};
207EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
208
209/*
210 * if you use these, you must assure that the frequency table is valid
211 * all the time between get_attr and put_attr!
212 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500213void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned int cpu)
215{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200216 pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
Tejun Heof1625062009-10-29 22:34:13 +0900217 per_cpu(cpufreq_show_table, cpu) = table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
220
221void cpufreq_frequency_table_put_attr(unsigned int cpu)
222{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200223 pr_debug("clearing show_table for cpu %u\n", cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900224 per_cpu(cpufreq_show_table, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
227
228struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
229{
Tejun Heof1625062009-10-29 22:34:13 +0900230 return per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
233
Dave Jones97acec52009-01-18 01:56:41 -0500234MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
235MODULE_DESCRIPTION("CPUfreq frequency table helpers");
236MODULE_LICENSE("GPL");