blob: df14766a8e06b7b692807c08d1bf27d683a339eb [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;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300120 unsigned int freq, diff, 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:
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300130 case CPUFREQ_RELATION_C:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 break;
133 }
134
Stratos Karafotis041526f2014-04-25 23:15:38 +0300135 cpufreq_for_each_valid_entry(pos, table) {
136 freq = pos->frequency;
137
138 i = pos - table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if ((freq < policy->min) || (freq > policy->max))
140 continue;
Stratos Karafotis1e498852014-05-14 21:05:52 +0300141 if (freq == target_freq) {
142 optimal.driver_data = i;
143 break;
144 }
Dave Jones97acec52009-01-18 01:56:41 -0500145 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 case CPUFREQ_RELATION_H:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300147 if (freq < target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (freq >= optimal.frequency) {
149 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530150 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152 } else {
153 if (freq <= suboptimal.frequency) {
154 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530155 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 }
158 break;
159 case CPUFREQ_RELATION_L:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300160 if (freq > target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (freq <= optimal.frequency) {
162 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530163 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 } else {
166 if (freq >= suboptimal.frequency) {
167 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530168 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 }
171 break;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300172 case CPUFREQ_RELATION_C:
173 diff = abs(freq - target_freq);
174 if (diff < optimal.frequency ||
175 (diff == optimal.frequency &&
176 freq > table[optimal.driver_data].frequency)) {
177 optimal.frequency = diff;
178 optimal.driver_data = i;
179 }
180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 }
Viresh Kumar50701582013-03-30 16:25:15 +0530183 if (optimal.driver_data > i) {
184 if (suboptimal.driver_data > i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -EINVAL;
Viresh Kumar50701582013-03-30 16:25:15 +0530186 *index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 } else
Viresh Kumar50701582013-03-30 16:25:15 +0530188 *index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Viresh Kumarae87f102014-03-28 19:11:45 +0530190 pr_debug("target index is %u, freq is:%u kHz\n", *index,
191 table[*index].frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 return 0;
194}
195EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
196
Viresh Kumard3916692013-12-03 11:20:46 +0530197int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
198 unsigned int freq)
199{
Stratos Karafotis041526f2014-04-25 23:15:38 +0300200 struct cpufreq_frequency_table *pos, *table;
Viresh Kumard3916692013-12-03 11:20:46 +0530201
202 table = cpufreq_frequency_get_table(policy->cpu);
203 if (unlikely(!table)) {
204 pr_debug("%s: Unable to find frequency table\n", __func__);
205 return -ENOENT;
206 }
207
Stratos Karafotis041526f2014-04-25 23:15:38 +0300208 cpufreq_for_each_valid_entry(pos, table)
209 if (pos->frequency == freq)
210 return pos - table;
Viresh Kumard3916692013-12-03 11:20:46 +0530211
212 return -EINVAL;
213}
214EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800217 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100219static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
220 bool show_boost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ssize_t count = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300223 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Viresh Kumare0b31652014-03-10 14:53:33 +0530225 if (!table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -ENODEV;
227
Stratos Karafotis041526f2014-04-25 23:15:38 +0300228 cpufreq_for_each_valid_entry(pos, table) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100229 /*
230 * show_boost = true and driver_data = BOOST freq
231 * display BOOST freqs
232 *
233 * show_boost = false and driver_data = BOOST freq
234 * show_boost = true and driver_data != BOOST freq
235 * continue - do not display anything
236 *
237 * show_boost = false and driver_data != BOOST freq
238 * display NON BOOST freqs
239 */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300240 if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100241 continue;
242
Stratos Karafotis041526f2014-04-25 23:15:38 +0300243 count += sprintf(&buf[count], "%d ", pos->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 count += sprintf(&buf[count], "\n");
246
247 return count;
248
249}
250
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100251#define cpufreq_attr_available_freq(_name) \
252struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
253__ATTR_RO(_name##_frequencies)
254
255/**
256 * show_scaling_available_frequencies - show available normal frequencies for
257 * the specified CPU
258 */
259static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
260 char *buf)
261{
262 return show_available_freqs(policy, buf, false);
263}
264cpufreq_attr_available_freq(scaling_available);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
266
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100267/**
268 * show_available_boost_freqs - show available boost frequencies for
269 * the specified CPU
270 */
271static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
272 char *buf)
273{
274 return show_available_freqs(policy, buf, true);
275}
276cpufreq_attr_available_freq(scaling_boost);
277EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
278
Viresh Kumar18434512013-10-03 20:27:55 +0530279struct freq_attr *cpufreq_generic_attr[] = {
280 &cpufreq_freq_attr_scaling_available_freqs,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100281#ifdef CONFIG_CPU_FREQ_BOOST_SW
282 &cpufreq_freq_attr_scaling_boost_freqs,
283#endif
Viresh Kumar18434512013-10-03 20:27:55 +0530284 NULL,
285};
286EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
287
Viresh Kumar27047a62013-09-16 18:56:03 +0530288int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
289 struct cpufreq_frequency_table *table)
290{
291 int ret = cpufreq_frequency_table_cpuinfo(policy, table);
292
293 if (!ret)
Viresh Kumare0b31652014-03-10 14:53:33 +0530294 policy->freq_table = table;
Viresh Kumar27047a62013-09-16 18:56:03 +0530295
296 return ret;
297}
298EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
299
Viresh Kumare0b31652014-03-10 14:53:33 +0530300struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
303{
Viresh Kumare0b31652014-03-10 14:53:33 +0530304 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
305 return policy ? policy->freq_table : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
308
Dave Jones97acec52009-01-18 01:56:41 -0500309MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
310MODULE_DESCRIPTION("CPUfreq frequency table helpers");
311MODULE_LICENSE("GPL");