blob: 3bbbf9e6960cdf12d939d3d88c5dc5ba4a89d070 [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
Viresh Kumar44139ed2015-07-29 16:23:09 +053021bool policy_has_boost_freq(struct cpufreq_policy *policy)
22{
23 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
24
25 if (!table)
26 return false;
27
28 cpufreq_for_each_valid_entry(pos, table)
29 if (pos->flags & CPUFREQ_BOOST_FREQ)
30 return true;
31
32 return false;
33}
34EXPORT_SYMBOL_GPL(policy_has_boost_freq);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
37 struct cpufreq_frequency_table *table)
38{
Stratos Karafotis041526f2014-04-25 23:15:38 +030039 struct cpufreq_frequency_table *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 unsigned int min_freq = ~0;
41 unsigned int max_freq = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +030042 unsigned int freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Stratos Karafotis041526f2014-04-25 23:15:38 +030044 cpufreq_for_each_valid_entry(pos, table) {
45 freq = pos->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Lukasz Majewski6f19efc2013-12-20 15:24:49 +010047 if (!cpufreq_boost_enabled()
Stratos Karafotis041526f2014-04-25 23:15:38 +030048 && (pos->flags & CPUFREQ_BOOST_FREQ))
Lukasz Majewski6f19efc2013-12-20 15:24:49 +010049 continue;
50
Stratos Karafotis041526f2014-04-25 23:15:38 +030051 pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (freq < min_freq)
53 min_freq = freq;
54 if (freq > max_freq)
55 max_freq = freq;
56 }
57
58 policy->min = policy->cpuinfo.min_freq = min_freq;
59 policy->max = policy->cpuinfo.max_freq = max_freq;
60
61 if (policy->min == ~0)
62 return -EINVAL;
63 else
64 return 0;
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
68 struct cpufreq_frequency_table *table)
69{
Stratos Karafotis041526f2014-04-25 23:15:38 +030070 struct cpufreq_frequency_table *pos;
71 unsigned int freq, next_larger = ~0;
Viresh Kumar77db50c2013-10-02 14:13:15 +053072 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020074 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053075 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Viresh Kumarbe49e342013-10-02 14:13:19 +053077 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Stratos Karafotis041526f2014-04-25 23:15:38 +030079 cpufreq_for_each_valid_entry(pos, table) {
80 freq = pos->frequency;
81
Viresh Kumar77db50c2013-10-02 14:13:15 +053082 if ((freq >= policy->min) && (freq <= policy->max)) {
83 found = true;
84 break;
85 }
86
87 if ((next_larger > freq) && (freq > policy->max))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 next_larger = freq;
89 }
90
Viresh Kumar77db50c2013-10-02 14:13:15 +053091 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 policy->max = next_larger;
Viresh Kumarbe49e342013-10-02 14:13:19 +053093 cpufreq_verify_within_cpu_limits(policy);
Viresh Kumar77db50c2013-10-02 14:13:15 +053094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020096 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053097 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 return 0;
100}
101EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
102
Viresh Kumar18434512013-10-03 20:27:55 +0530103/*
Viresh Kumare0b31652014-03-10 14:53:33 +0530104 * Generic routine to verify policy & frequency table, requires driver to set
105 * policy->freq_table prior to it.
Viresh Kumar18434512013-10-03 20:27:55 +0530106 */
107int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
108{
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530109 if (!policy->freq_table)
Viresh Kumar18434512013-10-03 20:27:55 +0530110 return -ENODEV;
111
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530112 return cpufreq_frequency_table_verify(policy, policy->freq_table);
Viresh Kumar18434512013-10-03 20:27:55 +0530113}
114EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530116int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
117 unsigned int target_freq,
118 unsigned int relation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Dave Jones484944a2006-05-30 18:09:31 -0400120 struct cpufreq_frequency_table optimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530121 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400122 .frequency = 0,
123 };
124 struct cpufreq_frequency_table suboptimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530125 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400126 .frequency = 0,
127 };
Stratos Karafotis041526f2014-04-25 23:15:38 +0300128 struct cpufreq_frequency_table *pos;
Viresh Kumar7ab4aab2016-06-03 10:58:49 +0530129 struct cpufreq_frequency_table *table = policy->freq_table;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300130 unsigned int freq, diff, i = 0;
Viresh Kumard218ed72016-06-03 10:58:51 +0530131 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200133 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530134 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 switch (relation) {
137 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 suboptimal.frequency = ~0;
139 break;
140 case CPUFREQ_RELATION_L:
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300141 case CPUFREQ_RELATION_C:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144 }
145
Stratos Karafotis041526f2014-04-25 23:15:38 +0300146 cpufreq_for_each_valid_entry(pos, table) {
147 freq = pos->frequency;
148
149 i = pos - table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if ((freq < policy->min) || (freq > policy->max))
151 continue;
Stratos Karafotis1e498852014-05-14 21:05:52 +0300152 if (freq == target_freq) {
153 optimal.driver_data = i;
154 break;
155 }
Dave Jones97acec52009-01-18 01:56:41 -0500156 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 case CPUFREQ_RELATION_H:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300158 if (freq < target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (freq >= optimal.frequency) {
160 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530161 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 } else {
164 if (freq <= suboptimal.frequency) {
165 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530166 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168 }
169 break;
170 case CPUFREQ_RELATION_L:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300171 if (freq > target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (freq <= optimal.frequency) {
173 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530174 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 } else {
177 if (freq >= suboptimal.frequency) {
178 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530179 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 }
182 break;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300183 case CPUFREQ_RELATION_C:
184 diff = abs(freq - target_freq);
185 if (diff < optimal.frequency ||
186 (diff == optimal.frequency &&
187 freq > table[optimal.driver_data].frequency)) {
188 optimal.frequency = diff;
189 optimal.driver_data = i;
190 }
191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193 }
Viresh Kumar50701582013-03-30 16:25:15 +0530194 if (optimal.driver_data > i) {
Viresh Kumard218ed72016-06-03 10:58:51 +0530195 if (suboptimal.driver_data > i) {
196 WARN(1, "Invalid frequency table: %d\n", policy->cpu);
197 return 0;
198 }
199
200 index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 } else
Viresh Kumard218ed72016-06-03 10:58:51 +0530202 index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Viresh Kumard218ed72016-06-03 10:58:51 +0530204 pr_debug("target index is %u, freq is:%u kHz\n", index,
205 table[index].frequency);
206 return index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530208EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Viresh Kumard3916692013-12-03 11:20:46 +0530210int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
211 unsigned int freq)
212{
Viresh Kumarf8bfc112016-06-03 10:58:47 +0530213 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
Viresh Kumard3916692013-12-03 11:20:46 +0530214
Viresh Kumard3916692013-12-03 11:20:46 +0530215 if (unlikely(!table)) {
216 pr_debug("%s: Unable to find frequency table\n", __func__);
217 return -ENOENT;
218 }
219
Stratos Karafotis041526f2014-04-25 23:15:38 +0300220 cpufreq_for_each_valid_entry(pos, table)
221 if (pos->frequency == freq)
222 return pos - table;
Viresh Kumard3916692013-12-03 11:20:46 +0530223
224 return -EINVAL;
225}
226EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800229 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100231static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
232 bool show_boost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 ssize_t count = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300235 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Viresh Kumare0b31652014-03-10 14:53:33 +0530237 if (!table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -ENODEV;
239
Stratos Karafotis041526f2014-04-25 23:15:38 +0300240 cpufreq_for_each_valid_entry(pos, table) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100241 /*
242 * show_boost = true and driver_data = BOOST freq
243 * display BOOST freqs
244 *
245 * show_boost = false and driver_data = BOOST freq
246 * show_boost = true and driver_data != BOOST freq
247 * continue - do not display anything
248 *
249 * show_boost = false and driver_data != BOOST freq
250 * display NON BOOST freqs
251 */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300252 if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100253 continue;
254
Stratos Karafotis041526f2014-04-25 23:15:38 +0300255 count += sprintf(&buf[count], "%d ", pos->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 count += sprintf(&buf[count], "\n");
258
259 return count;
260
261}
262
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100263#define cpufreq_attr_available_freq(_name) \
264struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
265__ATTR_RO(_name##_frequencies)
266
267/**
268 * show_scaling_available_frequencies - show available normal frequencies for
269 * the specified CPU
270 */
271static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
272 char *buf)
273{
274 return show_available_freqs(policy, buf, false);
275}
276cpufreq_attr_available_freq(scaling_available);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
278
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100279/**
280 * show_available_boost_freqs - show available boost frequencies for
281 * the specified CPU
282 */
283static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
284 char *buf)
285{
286 return show_available_freqs(policy, buf, true);
287}
288cpufreq_attr_available_freq(scaling_boost);
289EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
290
Viresh Kumar18434512013-10-03 20:27:55 +0530291struct freq_attr *cpufreq_generic_attr[] = {
292 &cpufreq_freq_attr_scaling_available_freqs,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100293#ifdef CONFIG_CPU_FREQ_BOOST_SW
294 &cpufreq_freq_attr_scaling_boost_freqs,
295#endif
Viresh Kumar18434512013-10-03 20:27:55 +0530296 NULL,
297};
298EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
299
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530300static int set_freq_table_sorted(struct cpufreq_policy *policy)
301{
302 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
303 struct cpufreq_frequency_table *prev = NULL;
304 int ascending = 0;
305
306 policy->freq_table_sorted = CPUFREQ_TABLE_UNSORTED;
307
308 cpufreq_for_each_valid_entry(pos, table) {
309 if (!prev) {
310 prev = pos;
311 continue;
312 }
313
314 if (pos->frequency == prev->frequency) {
315 pr_warn("Duplicate freq-table entries: %u\n",
316 pos->frequency);
317 return -EINVAL;
318 }
319
320 /* Frequency increased from prev to pos */
321 if (pos->frequency > prev->frequency) {
322 /* But frequency was decreasing earlier */
323 if (ascending < 0) {
324 pr_debug("Freq table is unsorted\n");
325 return 0;
326 }
327
328 ascending++;
329 } else {
330 /* Frequency decreased from prev to pos */
331
332 /* But frequency was increasing earlier */
333 if (ascending > 0) {
334 pr_debug("Freq table is unsorted\n");
335 return 0;
336 }
337
338 ascending--;
339 }
340
341 prev = pos;
342 }
343
344 if (ascending > 0)
345 policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_ASCENDING;
346 else
347 policy->freq_table_sorted = CPUFREQ_TABLE_SORTED_DESCENDING;
348
349 pr_debug("Freq table is sorted in %s order\n",
350 ascending > 0 ? "ascending" : "descending");
351
352 return 0;
353}
354
Viresh Kumar27047a62013-09-16 18:56:03 +0530355int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
356 struct cpufreq_frequency_table *table)
357{
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530358 int ret;
Viresh Kumar27047a62013-09-16 18:56:03 +0530359
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530360 ret = cpufreq_frequency_table_cpuinfo(policy, table);
361 if (ret)
362 return ret;
Viresh Kumar27047a62013-09-16 18:56:03 +0530363
Viresh Kumarda0c6dc2016-06-27 16:04:07 +0530364 policy->freq_table = table;
365 return set_freq_table_sorted(policy);
Viresh Kumar27047a62013-09-16 18:56:03 +0530366}
367EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
368
Dave Jones97acec52009-01-18 01:56:41 -0500369MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
370MODULE_DESCRIPTION("CPUfreq frequency table helpers");
371MODULE_LICENSE("GPL");