Loading...
1/*
2 * linux/drivers/cpufreq/freq_table.c
3 *
4 * Copyright (C) 2002 - 2003 Dominik Brodowski
5 *
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 *
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/cpufreq.h>
15#include <linux/module.h>
16
17/*********************************************************************
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;
26 unsigned int i;
27
28 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
29 unsigned int freq = table[i].frequency;
30 if (freq == CPUFREQ_ENTRY_INVALID) {
31 pr_debug("table entry %u is invalid, skipping\n", i);
32
33 continue;
34 }
35 if (!cpufreq_boost_enabled()
36 && (table[i].flags & CPUFREQ_BOOST_FREQ))
37 continue;
38
39 pr_debug("table entry %u: %u kHz\n", i, freq);
40 if (freq < min_freq)
41 min_freq = freq;
42 if (freq > max_freq)
43 max_freq = freq;
44 }
45
46 policy->min = policy->cpuinfo.min_freq = min_freq;
47 policy->max = policy->cpuinfo.max_freq = max_freq;
48
49 if (policy->min == ~0)
50 return -EINVAL;
51 else
52 return 0;
53}
54EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
55
56
57int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
58 struct cpufreq_frequency_table *table)
59{
60 unsigned int next_larger = ~0, freq, i = 0;
61 bool found = false;
62
63 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
64 policy->min, policy->max, policy->cpu);
65
66 cpufreq_verify_within_cpu_limits(policy);
67
68 for (; freq = table[i].frequency, freq != CPUFREQ_TABLE_END; i++) {
69 if (freq == CPUFREQ_ENTRY_INVALID)
70 continue;
71 if ((freq >= policy->min) && (freq <= policy->max)) {
72 found = true;
73 break;
74 }
75
76 if ((next_larger > freq) && (freq > policy->max))
77 next_larger = freq;
78 }
79
80 if (!found) {
81 policy->max = next_larger;
82 cpufreq_verify_within_cpu_limits(policy);
83 }
84
85 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
86 policy->min, policy->max, policy->cpu);
87
88 return 0;
89}
90EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
91
92/*
93 * Generic routine to verify policy & frequency table, requires driver to set
94 * policy->freq_table prior to it.
95 */
96int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
97{
98 struct cpufreq_frequency_table *table =
99 cpufreq_frequency_get_table(policy->cpu);
100 if (!table)
101 return -ENODEV;
102
103 return cpufreq_frequency_table_verify(policy, table);
104}
105EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
106
107int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
108 struct cpufreq_frequency_table *table,
109 unsigned int target_freq,
110 unsigned int relation,
111 unsigned int *index)
112{
113 struct cpufreq_frequency_table optimal = {
114 .driver_data = ~0,
115 .frequency = 0,
116 };
117 struct cpufreq_frequency_table suboptimal = {
118 .driver_data = ~0,
119 .frequency = 0,
120 };
121 unsigned int i;
122
123 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
124 target_freq, relation, policy->cpu);
125
126 switch (relation) {
127 case CPUFREQ_RELATION_H:
128 suboptimal.frequency = ~0;
129 break;
130 case CPUFREQ_RELATION_L:
131 optimal.frequency = ~0;
132 break;
133 }
134
135 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
136 unsigned int freq = table[i].frequency;
137 if (freq == CPUFREQ_ENTRY_INVALID)
138 continue;
139 if ((freq < policy->min) || (freq > policy->max))
140 continue;
141 switch (relation) {
142 case CPUFREQ_RELATION_H:
143 if (freq <= target_freq) {
144 if (freq >= optimal.frequency) {
145 optimal.frequency = freq;
146 optimal.driver_data = i;
147 }
148 } else {
149 if (freq <= suboptimal.frequency) {
150 suboptimal.frequency = freq;
151 suboptimal.driver_data = i;
152 }
153 }
154 break;
155 case CPUFREQ_RELATION_L:
156 if (freq >= target_freq) {
157 if (freq <= optimal.frequency) {
158 optimal.frequency = freq;
159 optimal.driver_data = i;
160 }
161 } else {
162 if (freq >= suboptimal.frequency) {
163 suboptimal.frequency = freq;
164 suboptimal.driver_data = i;
165 }
166 }
167 break;
168 }
169 }
170 if (optimal.driver_data > i) {
171 if (suboptimal.driver_data > i)
172 return -EINVAL;
173 *index = suboptimal.driver_data;
174 } else
175 *index = optimal.driver_data;
176
177 pr_debug("target index is %u, freq is:%u kHz\n", *index,
178 table[*index].frequency);
179
180 return 0;
181}
182EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
183
184int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
185 unsigned int freq)
186{
187 struct cpufreq_frequency_table *table;
188 int i;
189
190 table = cpufreq_frequency_get_table(policy->cpu);
191 if (unlikely(!table)) {
192 pr_debug("%s: Unable to find frequency table\n", __func__);
193 return -ENOENT;
194 }
195
196 for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
197 if (table[i].frequency == freq)
198 return i;
199 }
200
201 return -EINVAL;
202}
203EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
204
205/**
206 * show_available_freqs - show available frequencies for the specified CPU
207 */
208static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
209 bool show_boost)
210{
211 unsigned int i = 0;
212 ssize_t count = 0;
213 struct cpufreq_frequency_table *table = policy->freq_table;
214
215 if (!table)
216 return -ENODEV;
217
218 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
219 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
220 continue;
221 /*
222 * show_boost = true and driver_data = BOOST freq
223 * display BOOST freqs
224 *
225 * show_boost = false and driver_data = BOOST freq
226 * show_boost = true and driver_data != BOOST freq
227 * continue - do not display anything
228 *
229 * show_boost = false and driver_data != BOOST freq
230 * display NON BOOST freqs
231 */
232 if (show_boost ^ (table[i].flags & CPUFREQ_BOOST_FREQ))
233 continue;
234
235 count += sprintf(&buf[count], "%d ", table[i].frequency);
236 }
237 count += sprintf(&buf[count], "\n");
238
239 return count;
240
241}
242
243#define cpufreq_attr_available_freq(_name) \
244struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
245__ATTR_RO(_name##_frequencies)
246
247/**
248 * show_scaling_available_frequencies - show available normal frequencies for
249 * the specified CPU
250 */
251static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
252 char *buf)
253{
254 return show_available_freqs(policy, buf, false);
255}
256cpufreq_attr_available_freq(scaling_available);
257EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
258
259/**
260 * show_available_boost_freqs - show available boost frequencies for
261 * the specified CPU
262 */
263static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
264 char *buf)
265{
266 return show_available_freqs(policy, buf, true);
267}
268cpufreq_attr_available_freq(scaling_boost);
269EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
270
271struct freq_attr *cpufreq_generic_attr[] = {
272 &cpufreq_freq_attr_scaling_available_freqs,
273#ifdef CONFIG_CPU_FREQ_BOOST_SW
274 &cpufreq_freq_attr_scaling_boost_freqs,
275#endif
276 NULL,
277};
278EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
279
280int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
281 struct cpufreq_frequency_table *table)
282{
283 int ret = cpufreq_frequency_table_cpuinfo(policy, table);
284
285 if (!ret)
286 policy->freq_table = table;
287
288 return ret;
289}
290EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
291
292struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
293
294struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
295{
296 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
297 return policy ? policy->freq_table : NULL;
298}
299EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
300
301MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
302MODULE_DESCRIPTION("CPUfreq frequency table helpers");
303MODULE_LICENSE("GPL");
1/*
2 * linux/drivers/cpufreq/freq_table.c
3 *
4 * Copyright (C) 2002 - 2003 Dominik Brodowski
5 *
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 *
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/cpufreq.h>
15#include <linux/module.h>
16
17/*********************************************************************
18 * FREQUENCY TABLE HELPERS *
19 *********************************************************************/
20
21bool 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
36int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
37 struct cpufreq_frequency_table *table)
38{
39 struct cpufreq_frequency_table *pos;
40 unsigned int min_freq = ~0;
41 unsigned int max_freq = 0;
42 unsigned int freq;
43
44 cpufreq_for_each_valid_entry(pos, table) {
45 freq = pos->frequency;
46
47 if (!cpufreq_boost_enabled()
48 && (pos->flags & CPUFREQ_BOOST_FREQ))
49 continue;
50
51 pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
52 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}
66
67int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
68 struct cpufreq_frequency_table *table)
69{
70 struct cpufreq_frequency_table *pos;
71 unsigned int freq, next_larger = ~0;
72 bool found = false;
73
74 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
75 policy->min, policy->max, policy->cpu);
76
77 cpufreq_verify_within_cpu_limits(policy);
78
79 cpufreq_for_each_valid_entry(pos, table) {
80 freq = pos->frequency;
81
82 if ((freq >= policy->min) && (freq <= policy->max)) {
83 found = true;
84 break;
85 }
86
87 if ((next_larger > freq) && (freq > policy->max))
88 next_larger = freq;
89 }
90
91 if (!found) {
92 policy->max = next_larger;
93 cpufreq_verify_within_cpu_limits(policy);
94 }
95
96 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
97 policy->min, policy->max, policy->cpu);
98
99 return 0;
100}
101EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
102
103/*
104 * Generic routine to verify policy & frequency table, requires driver to set
105 * policy->freq_table prior to it.
106 */
107int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
108{
109 if (!policy->freq_table)
110 return -ENODEV;
111
112 return cpufreq_frequency_table_verify(policy, policy->freq_table);
113}
114EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
115
116int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
117 unsigned int target_freq,
118 unsigned int relation)
119{
120 struct cpufreq_frequency_table optimal = {
121 .driver_data = ~0,
122 .frequency = 0,
123 };
124 struct cpufreq_frequency_table suboptimal = {
125 .driver_data = ~0,
126 .frequency = 0,
127 };
128 struct cpufreq_frequency_table *pos;
129 struct cpufreq_frequency_table *table = policy->freq_table;
130 unsigned int freq, diff, i = 0;
131 int index;
132
133 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
134 target_freq, relation, policy->cpu);
135
136 switch (relation) {
137 case CPUFREQ_RELATION_H:
138 suboptimal.frequency = ~0;
139 break;
140 case CPUFREQ_RELATION_L:
141 case CPUFREQ_RELATION_C:
142 optimal.frequency = ~0;
143 break;
144 }
145
146 cpufreq_for_each_valid_entry_idx(pos, table, i) {
147 freq = pos->frequency;
148
149 if ((freq < policy->min) || (freq > policy->max))
150 continue;
151 if (freq == target_freq) {
152 optimal.driver_data = i;
153 break;
154 }
155 switch (relation) {
156 case CPUFREQ_RELATION_H:
157 if (freq < target_freq) {
158 if (freq >= optimal.frequency) {
159 optimal.frequency = freq;
160 optimal.driver_data = i;
161 }
162 } else {
163 if (freq <= suboptimal.frequency) {
164 suboptimal.frequency = freq;
165 suboptimal.driver_data = i;
166 }
167 }
168 break;
169 case CPUFREQ_RELATION_L:
170 if (freq > target_freq) {
171 if (freq <= optimal.frequency) {
172 optimal.frequency = freq;
173 optimal.driver_data = i;
174 }
175 } else {
176 if (freq >= suboptimal.frequency) {
177 suboptimal.frequency = freq;
178 suboptimal.driver_data = i;
179 }
180 }
181 break;
182 case CPUFREQ_RELATION_C:
183 diff = abs(freq - target_freq);
184 if (diff < optimal.frequency ||
185 (diff == optimal.frequency &&
186 freq > table[optimal.driver_data].frequency)) {
187 optimal.frequency = diff;
188 optimal.driver_data = i;
189 }
190 break;
191 }
192 }
193 if (optimal.driver_data > i) {
194 if (suboptimal.driver_data > i) {
195 WARN(1, "Invalid frequency table: %d\n", policy->cpu);
196 return 0;
197 }
198
199 index = suboptimal.driver_data;
200 } else
201 index = optimal.driver_data;
202
203 pr_debug("target index is %u, freq is:%u kHz\n", index,
204 table[index].frequency);
205 return index;
206}
207EXPORT_SYMBOL_GPL(cpufreq_table_index_unsorted);
208
209int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
210 unsigned int freq)
211{
212 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
213 int idx;
214
215 if (unlikely(!table)) {
216 pr_debug("%s: Unable to find frequency table\n", __func__);
217 return -ENOENT;
218 }
219
220 cpufreq_for_each_valid_entry_idx(pos, table, idx)
221 if (pos->frequency == freq)
222 return idx;
223
224 return -EINVAL;
225}
226EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
227
228/**
229 * show_available_freqs - show available frequencies for the specified CPU
230 */
231static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
232 bool show_boost)
233{
234 ssize_t count = 0;
235 struct cpufreq_frequency_table *pos, *table = policy->freq_table;
236
237 if (!table)
238 return -ENODEV;
239
240 cpufreq_for_each_valid_entry(pos, table) {
241 /*
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 */
252 if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
253 continue;
254
255 count += sprintf(&buf[count], "%d ", pos->frequency);
256 }
257 count += sprintf(&buf[count], "\n");
258
259 return count;
260
261}
262
263#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);
277EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
278
279/**
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
291struct freq_attr *cpufreq_generic_attr[] = {
292 &cpufreq_freq_attr_scaling_available_freqs,
293#ifdef CONFIG_CPU_FREQ_BOOST_SW
294 &cpufreq_freq_attr_scaling_boost_freqs,
295#endif
296 NULL,
297};
298EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
299
300static 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
355int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy)
356{
357 int ret;
358
359 if (!policy->freq_table)
360 return 0;
361
362 ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
363 if (ret)
364 return ret;
365
366 return set_freq_table_sorted(policy);
367}
368
369MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
370MODULE_DESCRIPTION("CPUfreq frequency table helpers");
371MODULE_LICENSE("GPL");