blob: 1632981c4b25b7882008f25991c2071e94faf607 [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{
Stratos Karafotis041526f2014-04-25 23:15:38 +030024 struct cpufreq_frequency_table *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 unsigned int min_freq = ~0;
26 unsigned int max_freq = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +030027 unsigned int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Stratos Karafotis041526f2014-04-25 23:15:38 +030029 cpufreq_for_each_valid_entry(pos, table) {
30 freq = pos->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Lukasz Majewski6f19efc2013-12-20 15:24:49 +010032 if (!cpufreq_boost_enabled()
Stratos Karafotis041526f2014-04-25 23:15:38 +030033 && (pos->flags & CPUFREQ_BOOST_FREQ))
Lukasz Majewski6f19efc2013-12-20 15:24:49 +010034 continue;
35
Stratos Karafotis041526f2014-04-25 23:15:38 +030036 pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
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{
Stratos Karafotis041526f2014-04-25 23:15:38 +030057 struct cpufreq_frequency_table *pos;
58 unsigned int freq, next_larger = ~0;
Viresh Kumar77db50c2013-10-02 14:13:15 +053059 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
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
Viresh Kumarbe49e342013-10-02 14:13:19 +053064 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Stratos Karafotis041526f2014-04-25 23:15:38 +030066 cpufreq_for_each_valid_entry(pos, table) {
67 freq = pos->frequency;
68
Viresh Kumar77db50c2013-10-02 14:13:15 +053069 if ((freq >= policy->min) && (freq <= policy->max)) {
70 found = true;
71 break;
72 }
73
74 if ((next_larger > freq) && (freq > policy->max))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 next_larger = freq;
76 }
77
Viresh Kumar77db50c2013-10-02 14:13:15 +053078 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 policy->max = next_larger;
Viresh Kumarbe49e342013-10-02 14:13:19 +053080 cpufreq_verify_within_cpu_limits(policy);
Viresh Kumar77db50c2013-10-02 14:13:15 +053081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020083 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053084 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 return 0;
87}
88EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
89
Viresh Kumar18434512013-10-03 20:27:55 +053090/*
Viresh Kumare0b31652014-03-10 14:53:33 +053091 * Generic routine to verify policy & frequency table, requires driver to set
92 * policy->freq_table prior to it.
Viresh Kumar18434512013-10-03 20:27:55 +053093 */
94int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
95{
96 struct cpufreq_frequency_table *table =
97 cpufreq_frequency_get_table(policy->cpu);
98 if (!table)
99 return -ENODEV;
100
101 return cpufreq_frequency_table_verify(policy, table);
102}
103EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
106 struct cpufreq_frequency_table *table,
107 unsigned int target_freq,
108 unsigned int relation,
109 unsigned int *index)
110{
Dave Jones484944a2006-05-30 18:09:31 -0400111 struct cpufreq_frequency_table optimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530112 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400113 .frequency = 0,
114 };
115 struct cpufreq_frequency_table suboptimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530116 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400117 .frequency = 0,
118 };
Stratos Karafotis041526f2014-04-25 23:15:38 +0300119 struct cpufreq_frequency_table *pos;
120 unsigned int freq, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200122 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530123 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 switch (relation) {
126 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 suboptimal.frequency = ~0;
128 break;
129 case CPUFREQ_RELATION_L:
130 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 break;
132 }
133
Stratos Karafotis041526f2014-04-25 23:15:38 +0300134 cpufreq_for_each_valid_entry(pos, table) {
135 freq = pos->frequency;
136
137 i = pos - table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if ((freq < policy->min) || (freq > policy->max))
139 continue;
Stratos Karafotis1e498852014-05-14 21:05:52 +0300140 if (freq == target_freq) {
141 optimal.driver_data = i;
142 break;
143 }
Dave Jones97acec52009-01-18 01:56:41 -0500144 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 case CPUFREQ_RELATION_H:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300146 if (freq < target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (freq >= optimal.frequency) {
148 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530149 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 } else {
152 if (freq <= suboptimal.frequency) {
153 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530154 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 }
157 break;
158 case CPUFREQ_RELATION_L:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300159 if (freq > target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (freq <= optimal.frequency) {
161 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530162 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164 } else {
165 if (freq >= suboptimal.frequency) {
166 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530167 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 }
170 break;
171 }
172 }
Viresh Kumar50701582013-03-30 16:25:15 +0530173 if (optimal.driver_data > i) {
174 if (suboptimal.driver_data > i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return -EINVAL;
Viresh Kumar50701582013-03-30 16:25:15 +0530176 *index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 } else
Viresh Kumar50701582013-03-30 16:25:15 +0530178 *index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Viresh Kumarae87f102014-03-28 19:11:45 +0530180 pr_debug("target index is %u, freq is:%u kHz\n", *index,
181 table[*index].frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 return 0;
184}
185EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
186
Viresh Kumard3916692013-12-03 11:20:46 +0530187int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
188 unsigned int freq)
189{
Stratos Karafotis041526f2014-04-25 23:15:38 +0300190 struct cpufreq_frequency_table *pos, *table;
Viresh Kumard3916692013-12-03 11:20:46 +0530191
192 table = cpufreq_frequency_get_table(policy->cpu);
193 if (unlikely(!table)) {
194 pr_debug("%s: Unable to find frequency table\n", __func__);
195 return -ENOENT;
196 }
197
Stratos Karafotis041526f2014-04-25 23:15:38 +0300198 cpufreq_for_each_valid_entry(pos, table)
199 if (pos->frequency == freq)
200 return pos - table;
Viresh Kumard3916692013-12-03 11:20:46 +0530201
202 return -EINVAL;
203}
204EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800207 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100209static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
210 bool show_boost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ssize_t count = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300213 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Viresh Kumare0b31652014-03-10 14:53:33 +0530215 if (!table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -ENODEV;
217
Stratos Karafotis041526f2014-04-25 23:15:38 +0300218 cpufreq_for_each_valid_entry(pos, table) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100219 /*
220 * show_boost = true and driver_data = BOOST freq
221 * display BOOST freqs
222 *
223 * show_boost = false and driver_data = BOOST freq
224 * show_boost = true and driver_data != BOOST freq
225 * continue - do not display anything
226 *
227 * show_boost = false and driver_data != BOOST freq
228 * display NON BOOST freqs
229 */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300230 if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100231 continue;
232
Stratos Karafotis041526f2014-04-25 23:15:38 +0300233 count += sprintf(&buf[count], "%d ", pos->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 count += sprintf(&buf[count], "\n");
236
237 return count;
238
239}
240
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100241#define cpufreq_attr_available_freq(_name) \
242struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
243__ATTR_RO(_name##_frequencies)
244
245/**
246 * show_scaling_available_frequencies - show available normal frequencies for
247 * the specified CPU
248 */
249static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
250 char *buf)
251{
252 return show_available_freqs(policy, buf, false);
253}
254cpufreq_attr_available_freq(scaling_available);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
256
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100257/**
258 * show_available_boost_freqs - show available boost frequencies for
259 * the specified CPU
260 */
261static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
262 char *buf)
263{
264 return show_available_freqs(policy, buf, true);
265}
266cpufreq_attr_available_freq(scaling_boost);
267EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
268
Viresh Kumar18434512013-10-03 20:27:55 +0530269struct freq_attr *cpufreq_generic_attr[] = {
270 &cpufreq_freq_attr_scaling_available_freqs,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100271#ifdef CONFIG_CPU_FREQ_BOOST_SW
272 &cpufreq_freq_attr_scaling_boost_freqs,
273#endif
Viresh Kumar18434512013-10-03 20:27:55 +0530274 NULL,
275};
276EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
277
Viresh Kumar27047a62013-09-16 18:56:03 +0530278int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
279 struct cpufreq_frequency_table *table)
280{
281 int ret = cpufreq_frequency_table_cpuinfo(policy, table);
282
283 if (!ret)
Viresh Kumare0b31652014-03-10 14:53:33 +0530284 policy->freq_table = table;
Viresh Kumar27047a62013-09-16 18:56:03 +0530285
286 return ret;
287}
288EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
289
Viresh Kumare0b31652014-03-10 14:53:33 +0530290struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
293{
Viresh Kumare0b31652014-03-10 14:53:33 +0530294 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
295 return policy ? policy->freq_table : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
298
Dave Jones97acec52009-01-18 01:56:41 -0500299MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
300MODULE_DESCRIPTION("CPUfreq frequency table helpers");
301MODULE_LICENSE("GPL");