blob: 8e54f97899ba6feb7225275b7ddf2473ff8dc35b [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
Viresh Kumardb701152012-10-23 01:29:03 +020012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/cpufreq.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053015#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
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 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +010035 if (!cpufreq_boost_enabled()
36 && table[i].driver_data == CPUFREQ_BOOST_FREQ)
37 continue;
38
Viresh Kumar50701582013-03-30 16:25:15 +053039 pr_debug("table entry %u: %u kHz, %u driver_data\n",
40 i, freq, table[i].driver_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (freq < min_freq)
42 min_freq = freq;
43 if (freq > max_freq)
44 max_freq = freq;
45 }
46
47 policy->min = policy->cpuinfo.min_freq = min_freq;
48 policy->max = policy->cpuinfo.max_freq = max_freq;
49
50 if (policy->min == ~0)
51 return -EINVAL;
52 else
53 return 0;
54}
55EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
56
57
58int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
59 struct cpufreq_frequency_table *table)
60{
Viresh Kumar77db50c2013-10-02 14:13:15 +053061 unsigned int next_larger = ~0, freq, i = 0;
62 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020064 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053065 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Viresh Kumarbe49e342013-10-02 14:13:19 +053067 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Viresh Kumar77db50c2013-10-02 14:13:15 +053069 for (; freq = table[i].frequency, freq != CPUFREQ_TABLE_END; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (freq == CPUFREQ_ENTRY_INVALID)
71 continue;
Viresh Kumar77db50c2013-10-02 14:13:15 +053072 if ((freq >= policy->min) && (freq <= policy->max)) {
73 found = true;
74 break;
75 }
76
77 if ((next_larger > freq) && (freq > policy->max))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 next_larger = freq;
79 }
80
Viresh Kumar77db50c2013-10-02 14:13:15 +053081 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 policy->max = next_larger;
Viresh Kumarbe49e342013-10-02 14:13:19 +053083 cpufreq_verify_within_cpu_limits(policy);
Viresh Kumar77db50c2013-10-02 14:13:15 +053084 }
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
Viresh Kumar18434512013-10-03 20:27:55 +053093/*
94 * Generic routine to verify policy & frequency table, requires driver to call
95 * cpufreq_frequency_table_get_attr() prior to it.
96 */
97int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
98{
99 struct cpufreq_frequency_table *table =
100 cpufreq_frequency_get_table(policy->cpu);
101 if (!table)
102 return -ENODEV;
103
104 return cpufreq_frequency_table_verify(policy, table);
105}
106EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
109 struct cpufreq_frequency_table *table,
110 unsigned int target_freq,
111 unsigned int relation,
112 unsigned int *index)
113{
Dave Jones484944a2006-05-30 18:09:31 -0400114 struct cpufreq_frequency_table optimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530115 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400116 .frequency = 0,
117 };
118 struct cpufreq_frequency_table suboptimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530119 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400120 .frequency = 0,
121 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned int i;
123
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200124 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530125 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 switch (relation) {
128 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 suboptimal.frequency = ~0;
130 break;
131 case CPUFREQ_RELATION_L:
132 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 break;
134 }
135
Dave Jones97acec52009-01-18 01:56:41 -0500136 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 unsigned int freq = table[i].frequency;
138 if (freq == CPUFREQ_ENTRY_INVALID)
139 continue;
140 if ((freq < policy->min) || (freq > policy->max))
141 continue;
Dave Jones97acec52009-01-18 01:56:41 -0500142 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 case CPUFREQ_RELATION_H:
144 if (freq <= target_freq) {
145 if (freq >= optimal.frequency) {
146 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530147 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149 } else {
150 if (freq <= suboptimal.frequency) {
151 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530152 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154 }
155 break;
156 case CPUFREQ_RELATION_L:
157 if (freq >= target_freq) {
158 if (freq <= optimal.frequency) {
159 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530160 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 } else {
163 if (freq >= suboptimal.frequency) {
164 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530165 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 }
168 break;
169 }
170 }
Viresh Kumar50701582013-03-30 16:25:15 +0530171 if (optimal.driver_data > i) {
172 if (suboptimal.driver_data > i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -EINVAL;
Viresh Kumar50701582013-03-30 16:25:15 +0530174 *index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 } else
Viresh Kumar50701582013-03-30 16:25:15 +0530176 *index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200178 pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
Viresh Kumar50701582013-03-30 16:25:15 +0530179 table[*index].driver_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 return 0;
182}
183EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
184
Viresh Kumard3916692013-12-03 11:20:46 +0530185int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
186 unsigned int freq)
187{
188 struct cpufreq_frequency_table *table;
189 int i;
190
191 table = cpufreq_frequency_get_table(policy->cpu);
192 if (unlikely(!table)) {
193 pr_debug("%s: Unable to find frequency table\n", __func__);
194 return -ENOENT;
195 }
196
197 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
198 if (table[i].frequency == freq)
199 return i;
200 }
201
202 return -EINVAL;
203}
204EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
205
Tejun Heof1625062009-10-29 22:34:13 +0900206static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
Viresh Kumard3916692013-12-03 11:20:46 +0530207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800209 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100211static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
212 bool show_boost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned int i = 0;
215 unsigned int cpu = policy->cpu;
216 ssize_t count = 0;
217 struct cpufreq_frequency_table *table;
218
Tejun Heof1625062009-10-29 22:34:13 +0900219 if (!per_cpu(cpufreq_show_table, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -ENODEV;
221
Tejun Heof1625062009-10-29 22:34:13 +0900222 table = per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Dave Jones97acec52009-01-18 01:56:41 -0500224 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
226 continue;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100227 /*
228 * show_boost = true and driver_data = BOOST freq
229 * display BOOST freqs
230 *
231 * show_boost = false and driver_data = BOOST freq
232 * show_boost = true and driver_data != BOOST freq
233 * continue - do not display anything
234 *
235 * show_boost = false and driver_data != BOOST freq
236 * display NON BOOST freqs
237 */
238 if (show_boost ^ (table[i].driver_data == CPUFREQ_BOOST_FREQ))
239 continue;
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 count += sprintf(&buf[count], "%d ", table[i].frequency);
242 }
243 count += sprintf(&buf[count], "\n");
244
245 return count;
246
247}
248
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100249#define cpufreq_attr_available_freq(_name) \
250struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
251__ATTR_RO(_name##_frequencies)
252
253/**
254 * show_scaling_available_frequencies - show available normal frequencies for
255 * the specified CPU
256 */
257static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
258 char *buf)
259{
260 return show_available_freqs(policy, buf, false);
261}
262cpufreq_attr_available_freq(scaling_available);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
264
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100265/**
266 * show_available_boost_freqs - show available boost frequencies for
267 * the specified CPU
268 */
269static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
270 char *buf)
271{
272 return show_available_freqs(policy, buf, true);
273}
274cpufreq_attr_available_freq(scaling_boost);
275EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
276
Viresh Kumar18434512013-10-03 20:27:55 +0530277struct freq_attr *cpufreq_generic_attr[] = {
278 &cpufreq_freq_attr_scaling_available_freqs,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100279#ifdef CONFIG_CPU_FREQ_BOOST_SW
280 &cpufreq_freq_attr_scaling_boost_freqs,
281#endif
Viresh Kumar18434512013-10-03 20:27:55 +0530282 NULL,
283};
284EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
287 * if you use these, you must assure that the frequency table is valid
288 * all the time between get_attr and put_attr!
289 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500290void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unsigned int cpu)
292{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200293 pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
Tejun Heof1625062009-10-29 22:34:13 +0900294 per_cpu(cpufreq_show_table, cpu) = table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
297
298void cpufreq_frequency_table_put_attr(unsigned int cpu)
299{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200300 pr_debug("clearing show_table for cpu %u\n", cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900301 per_cpu(cpufreq_show_table, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
304
Viresh Kumar27047a62013-09-16 18:56:03 +0530305int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
306 struct cpufreq_frequency_table *table)
307{
308 int ret = cpufreq_frequency_table_cpuinfo(policy, table);
309
310 if (!ret)
311 cpufreq_frequency_table_get_attr(table, policy->cpu);
312
313 return ret;
314}
315EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
316
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000317void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy)
318{
319 pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n",
320 policy->cpu, policy->last_cpu);
321 per_cpu(cpufreq_show_table, policy->cpu) = per_cpu(cpufreq_show_table,
322 policy->last_cpu);
323 per_cpu(cpufreq_show_table, policy->last_cpu) = NULL;
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
327{
Tejun Heof1625062009-10-29 22:34:13 +0900328 return per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
331
Dave Jones97acec52009-01-18 01:56:41 -0500332MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
333MODULE_DESCRIPTION("CPUfreq frequency table helpers");
334MODULE_LICENSE("GPL");