blob: a8ac0427fbfeb215f00de7a645b69c5e6d51bf63 [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 }
Viresh Kumar50701582013-03-30 16:25:15 +053035 pr_debug("table entry %u: %u kHz, %u driver_data\n",
36 i, freq, table[i].driver_data);
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{
Viresh Kumar77db50c2013-10-02 14:13:15 +053057 unsigned int next_larger = ~0, freq, i = 0;
58 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020060 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053061 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Viresh Kumarbe49e342013-10-02 14:13:19 +053063 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Viresh Kumar77db50c2013-10-02 14:13:15 +053065 for (; freq = table[i].frequency, freq != CPUFREQ_TABLE_END; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (freq == CPUFREQ_ENTRY_INVALID)
67 continue;
Viresh Kumar77db50c2013-10-02 14:13:15 +053068 if ((freq >= policy->min) && (freq <= policy->max)) {
69 found = true;
70 break;
71 }
72
73 if ((next_larger > freq) && (freq > policy->max))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 next_larger = freq;
75 }
76
Viresh Kumar77db50c2013-10-02 14:13:15 +053077 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 policy->max = next_larger;
Viresh Kumarbe49e342013-10-02 14:13:19 +053079 cpufreq_verify_within_cpu_limits(policy);
Viresh Kumar77db50c2013-10-02 14:13:15 +053080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020082 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053083 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 return 0;
86}
87EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
88
Viresh Kumar18434512013-10-03 20:27:55 +053089/*
90 * Generic routine to verify policy & frequency table, requires driver to call
91 * cpufreq_frequency_table_get_attr() prior to it.
92 */
93int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
94{
95 struct cpufreq_frequency_table *table =
96 cpufreq_frequency_get_table(policy->cpu);
97 if (!table)
98 return -ENODEV;
99
100 return cpufreq_frequency_table_verify(policy, table);
101}
102EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
105 struct cpufreq_frequency_table *table,
106 unsigned int target_freq,
107 unsigned int relation,
108 unsigned int *index)
109{
Dave Jones484944a2006-05-30 18:09:31 -0400110 struct cpufreq_frequency_table optimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530111 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400112 .frequency = 0,
113 };
114 struct cpufreq_frequency_table suboptimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530115 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400116 .frequency = 0,
117 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 unsigned int i;
119
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200120 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530121 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 switch (relation) {
124 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 suboptimal.frequency = ~0;
126 break;
127 case CPUFREQ_RELATION_L:
128 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 break;
130 }
131
Dave Jones97acec52009-01-18 01:56:41 -0500132 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned int freq = table[i].frequency;
134 if (freq == CPUFREQ_ENTRY_INVALID)
135 continue;
136 if ((freq < policy->min) || (freq > policy->max))
137 continue;
Dave Jones97acec52009-01-18 01:56:41 -0500138 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 case CPUFREQ_RELATION_H:
140 if (freq <= target_freq) {
141 if (freq >= optimal.frequency) {
142 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530143 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 } else {
146 if (freq <= suboptimal.frequency) {
147 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530148 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 }
151 break;
152 case CPUFREQ_RELATION_L:
153 if (freq >= target_freq) {
154 if (freq <= optimal.frequency) {
155 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530156 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 } else {
159 if (freq >= suboptimal.frequency) {
160 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530161 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 }
164 break;
165 }
166 }
Viresh Kumar50701582013-03-30 16:25:15 +0530167 if (optimal.driver_data > i) {
168 if (suboptimal.driver_data > i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -EINVAL;
Viresh Kumar50701582013-03-30 16:25:15 +0530170 *index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 } else
Viresh Kumar50701582013-03-30 16:25:15 +0530172 *index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200174 pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
Viresh Kumar50701582013-03-30 16:25:15 +0530175 table[*index].driver_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 return 0;
178}
179EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
180
Viresh Kumard3916692013-12-03 11:20:46 +0530181int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
182 unsigned int freq)
183{
184 struct cpufreq_frequency_table *table;
185 int i;
186
187 table = cpufreq_frequency_get_table(policy->cpu);
188 if (unlikely(!table)) {
189 pr_debug("%s: Unable to find frequency table\n", __func__);
190 return -ENOENT;
191 }
192
193 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
194 if (table[i].frequency == freq)
195 return i;
196 }
197
198 return -EINVAL;
199}
200EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
201
Tejun Heof1625062009-10-29 22:34:13 +0900202static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
Viresh Kumard3916692013-12-03 11:20:46 +0530203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800205 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
Dave Jones97acec52009-01-18 01:56:41 -0500207static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 unsigned int i = 0;
210 unsigned int cpu = policy->cpu;
211 ssize_t count = 0;
212 struct cpufreq_frequency_table *table;
213
Tejun Heof1625062009-10-29 22:34:13 +0900214 if (!per_cpu(cpufreq_show_table, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -ENODEV;
216
Tejun Heof1625062009-10-29 22:34:13 +0900217 table = per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Dave Jones97acec52009-01-18 01:56:41 -0500219 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
221 continue;
222 count += sprintf(&buf[count], "%d ", table[i].frequency);
223 }
224 count += sprintf(&buf[count], "\n");
225
226 return count;
227
228}
229
230struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530231 .attr = { .name = "scaling_available_frequencies",
232 .mode = 0444,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530233 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .show = show_available_freqs,
235};
236EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
237
Viresh Kumar18434512013-10-03 20:27:55 +0530238struct freq_attr *cpufreq_generic_attr[] = {
239 &cpufreq_freq_attr_scaling_available_freqs,
240 NULL,
241};
242EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 * if you use these, you must assure that the frequency table is valid
246 * all the time between get_attr and put_attr!
247 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500248void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned int cpu)
250{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200251 pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
Tejun Heof1625062009-10-29 22:34:13 +0900252 per_cpu(cpufreq_show_table, cpu) = table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
255
256void cpufreq_frequency_table_put_attr(unsigned int cpu)
257{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200258 pr_debug("clearing show_table for cpu %u\n", cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900259 per_cpu(cpufreq_show_table, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
262
Viresh Kumar27047a62013-09-16 18:56:03 +0530263int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
264 struct cpufreq_frequency_table *table)
265{
266 int ret = cpufreq_frequency_table_cpuinfo(policy, table);
267
268 if (!ret)
269 cpufreq_frequency_table_get_attr(table, policy->cpu);
270
271 return ret;
272}
273EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
274
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000275void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy)
276{
277 pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n",
278 policy->cpu, policy->last_cpu);
279 per_cpu(cpufreq_show_table, policy->cpu) = per_cpu(cpufreq_show_table,
280 policy->last_cpu);
281 per_cpu(cpufreq_show_table, policy->last_cpu) = NULL;
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
285{
Tejun Heof1625062009-10-29 22:34:13 +0900286 return per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
289
Dave Jones97acec52009-01-18 01:56:41 -0500290MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
291MODULE_DESCRIPTION("CPUfreq frequency table helpers");
292MODULE_LICENSE("GPL");