blob: a8f1daffc9bcbfd6773ec50fc765e9fc821d4daa [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}
66EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
67
68
69int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
70 struct cpufreq_frequency_table *table)
71{
Stratos Karafotis041526f2014-04-25 23:15:38 +030072 struct cpufreq_frequency_table *pos;
73 unsigned int freq, next_larger = ~0;
Viresh Kumar77db50c2013-10-02 14:13:15 +053074 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020076 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053077 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Viresh Kumarbe49e342013-10-02 14:13:19 +053079 cpufreq_verify_within_cpu_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Stratos Karafotis041526f2014-04-25 23:15:38 +030081 cpufreq_for_each_valid_entry(pos, table) {
82 freq = pos->frequency;
83
Viresh Kumar77db50c2013-10-02 14:13:15 +053084 if ((freq >= policy->min) && (freq <= policy->max)) {
85 found = true;
86 break;
87 }
88
89 if ((next_larger > freq) && (freq > policy->max))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 next_larger = freq;
91 }
92
Viresh Kumar77db50c2013-10-02 14:13:15 +053093 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 policy->max = next_larger;
Viresh Kumarbe49e342013-10-02 14:13:19 +053095 cpufreq_verify_within_cpu_limits(policy);
Viresh Kumar77db50c2013-10-02 14:13:15 +053096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020098 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053099 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 return 0;
102}
103EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
104
Viresh Kumar18434512013-10-03 20:27:55 +0530105/*
Viresh Kumare0b31652014-03-10 14:53:33 +0530106 * Generic routine to verify policy & frequency table, requires driver to set
107 * policy->freq_table prior to it.
Viresh Kumar18434512013-10-03 20:27:55 +0530108 */
109int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
110{
111 struct cpufreq_frequency_table *table =
112 cpufreq_frequency_get_table(policy->cpu);
113 if (!table)
114 return -ENODEV;
115
116 return cpufreq_frequency_table_verify(policy, table);
117}
118EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
121 struct cpufreq_frequency_table *table,
122 unsigned int target_freq,
123 unsigned int relation,
124 unsigned int *index)
125{
Dave Jones484944a2006-05-30 18:09:31 -0400126 struct cpufreq_frequency_table optimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530127 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400128 .frequency = 0,
129 };
130 struct cpufreq_frequency_table suboptimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530131 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400132 .frequency = 0,
133 };
Stratos Karafotis041526f2014-04-25 23:15:38 +0300134 struct cpufreq_frequency_table *pos;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300135 unsigned int freq, diff, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200137 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530138 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 switch (relation) {
141 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 suboptimal.frequency = ~0;
143 break;
144 case CPUFREQ_RELATION_L:
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300145 case CPUFREQ_RELATION_C:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 break;
148 }
149
Stratos Karafotis041526f2014-04-25 23:15:38 +0300150 cpufreq_for_each_valid_entry(pos, table) {
151 freq = pos->frequency;
152
153 i = pos - table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if ((freq < policy->min) || (freq > policy->max))
155 continue;
Stratos Karafotis1e498852014-05-14 21:05:52 +0300156 if (freq == target_freq) {
157 optimal.driver_data = i;
158 break;
159 }
Dave Jones97acec52009-01-18 01:56:41 -0500160 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 case CPUFREQ_RELATION_H:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300162 if (freq < target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (freq >= optimal.frequency) {
164 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530165 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 } else {
168 if (freq <= suboptimal.frequency) {
169 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530170 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 }
173 break;
174 case CPUFREQ_RELATION_L:
Stratos Karafotis1e498852014-05-14 21:05:52 +0300175 if (freq > target_freq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (freq <= optimal.frequency) {
177 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530178 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 } else {
181 if (freq >= suboptimal.frequency) {
182 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530183 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 }
186 break;
Stratos Karafotis5b0c0b12014-06-30 19:59:33 +0300187 case CPUFREQ_RELATION_C:
188 diff = abs(freq - target_freq);
189 if (diff < optimal.frequency ||
190 (diff == optimal.frequency &&
191 freq > table[optimal.driver_data].frequency)) {
192 optimal.frequency = diff;
193 optimal.driver_data = i;
194 }
195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 }
Viresh Kumar50701582013-03-30 16:25:15 +0530198 if (optimal.driver_data > i) {
199 if (suboptimal.driver_data > i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -EINVAL;
Viresh Kumar50701582013-03-30 16:25:15 +0530201 *index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 } else
Viresh Kumar50701582013-03-30 16:25:15 +0530203 *index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Viresh Kumarae87f102014-03-28 19:11:45 +0530205 pr_debug("target index is %u, freq is:%u kHz\n", *index,
206 table[*index].frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 return 0;
209}
210EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
211
Viresh Kumard3916692013-12-03 11:20:46 +0530212int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
213 unsigned int freq)
214{
Stratos Karafotis041526f2014-04-25 23:15:38 +0300215 struct cpufreq_frequency_table *pos, *table;
Viresh Kumard3916692013-12-03 11:20:46 +0530216
217 table = cpufreq_frequency_get_table(policy->cpu);
218 if (unlikely(!table)) {
219 pr_debug("%s: Unable to find frequency table\n", __func__);
220 return -ENOENT;
221 }
222
Stratos Karafotis041526f2014-04-25 23:15:38 +0300223 cpufreq_for_each_valid_entry(pos, table)
224 if (pos->frequency == freq)
225 return pos - table;
Viresh Kumard3916692013-12-03 11:20:46 +0530226
227 return -EINVAL;
228}
229EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800232 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100234static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
235 bool show_boost)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 ssize_t count = 0;
Stratos Karafotis041526f2014-04-25 23:15:38 +0300238 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Viresh Kumare0b31652014-03-10 14:53:33 +0530240 if (!table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -ENODEV;
242
Stratos Karafotis041526f2014-04-25 23:15:38 +0300243 cpufreq_for_each_valid_entry(pos, table) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100244 /*
245 * show_boost = true and driver_data = BOOST freq
246 * display BOOST freqs
247 *
248 * show_boost = false and driver_data = BOOST freq
249 * show_boost = true and driver_data != BOOST freq
250 * continue - do not display anything
251 *
252 * show_boost = false and driver_data != BOOST freq
253 * display NON BOOST freqs
254 */
Stratos Karafotis041526f2014-04-25 23:15:38 +0300255 if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100256 continue;
257
Stratos Karafotis041526f2014-04-25 23:15:38 +0300258 count += sprintf(&buf[count], "%d ", pos->frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 count += sprintf(&buf[count], "\n");
261
262 return count;
263
264}
265
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100266#define cpufreq_attr_available_freq(_name) \
267struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
268__ATTR_RO(_name##_frequencies)
269
270/**
271 * show_scaling_available_frequencies - show available normal frequencies for
272 * the specified CPU
273 */
274static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
275 char *buf)
276{
277 return show_available_freqs(policy, buf, false);
278}
279cpufreq_attr_available_freq(scaling_available);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
281
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100282/**
283 * show_available_boost_freqs - show available boost frequencies for
284 * the specified CPU
285 */
286static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
287 char *buf)
288{
289 return show_available_freqs(policy, buf, true);
290}
291cpufreq_attr_available_freq(scaling_boost);
292EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
293
Viresh Kumar18434512013-10-03 20:27:55 +0530294struct freq_attr *cpufreq_generic_attr[] = {
295 &cpufreq_freq_attr_scaling_available_freqs,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100296#ifdef CONFIG_CPU_FREQ_BOOST_SW
297 &cpufreq_freq_attr_scaling_boost_freqs,
298#endif
Viresh Kumar18434512013-10-03 20:27:55 +0530299 NULL,
300};
301EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
302
Viresh Kumar27047a62013-09-16 18:56:03 +0530303int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
304 struct cpufreq_frequency_table *table)
305{
306 int ret = cpufreq_frequency_table_cpuinfo(policy, table);
307
308 if (!ret)
Viresh Kumare0b31652014-03-10 14:53:33 +0530309 policy->freq_table = table;
Viresh Kumar27047a62013-09-16 18:56:03 +0530310
311 return ret;
312}
313EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
314
Dave Jones97acec52009-01-18 01:56:41 -0500315MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
316MODULE_DESCRIPTION("CPUfreq frequency table helpers");
317MODULE_LICENSE("GPL");