2 * linux/drivers/cpufreq/freq_table.c
4 * Copyright (C) 2002 - 2003 Dominik Brodowski
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/cpufreq.h>
15 #include <linux/module.h>
17 /*********************************************************************
18 * FREQUENCY TABLE HELPERS *
19 *********************************************************************/
21 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy
*policy
,
22 struct cpufreq_frequency_table
*table
)
24 unsigned int min_freq
= ~0;
25 unsigned int max_freq
= 0;
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
);
35 if (!cpufreq_boost_enabled()
36 && table
[i
].driver_data
== CPUFREQ_BOOST_FREQ
)
39 pr_debug("table entry %u: %u kHz, %u driver_data\n",
40 i
, freq
, table
[i
].driver_data
);
47 policy
->min
= policy
->cpuinfo
.min_freq
= min_freq
;
48 policy
->max
= policy
->cpuinfo
.max_freq
= max_freq
;
50 if (policy
->min
== ~0)
55 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo
);
58 int cpufreq_frequency_table_verify(struct cpufreq_policy
*policy
,
59 struct cpufreq_frequency_table
*table
)
61 unsigned int next_larger
= ~0, freq
, i
= 0;
64 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
65 policy
->min
, policy
->max
, policy
->cpu
);
67 cpufreq_verify_within_cpu_limits(policy
);
69 for (; freq
= table
[i
].frequency
, freq
!= CPUFREQ_TABLE_END
; i
++) {
70 if (freq
== CPUFREQ_ENTRY_INVALID
)
72 if ((freq
>= policy
->min
) && (freq
<= policy
->max
)) {
77 if ((next_larger
> freq
) && (freq
> policy
->max
))
82 policy
->max
= next_larger
;
83 cpufreq_verify_within_cpu_limits(policy
);
86 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
87 policy
->min
, policy
->max
, policy
->cpu
);
91 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify
);
94 * Generic routine to verify policy & frequency table, requires driver to call
95 * cpufreq_frequency_table_get_attr() prior to it.
97 int cpufreq_generic_frequency_table_verify(struct cpufreq_policy
*policy
)
99 struct cpufreq_frequency_table
*table
=
100 cpufreq_frequency_get_table(policy
->cpu
);
104 return cpufreq_frequency_table_verify(policy
, table
);
106 EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify
);
108 int cpufreq_frequency_table_target(struct cpufreq_policy
*policy
,
109 struct cpufreq_frequency_table
*table
,
110 unsigned int target_freq
,
111 unsigned int relation
,
114 struct cpufreq_frequency_table optimal
= {
118 struct cpufreq_frequency_table suboptimal
= {
124 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
125 target_freq
, relation
, policy
->cpu
);
128 case CPUFREQ_RELATION_H
:
129 suboptimal
.frequency
= ~0;
131 case CPUFREQ_RELATION_L
:
132 optimal
.frequency
= ~0;
136 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
137 unsigned int freq
= table
[i
].frequency
;
138 if (freq
== CPUFREQ_ENTRY_INVALID
)
140 if ((freq
< policy
->min
) || (freq
> policy
->max
))
143 case CPUFREQ_RELATION_H
:
144 if (freq
<= target_freq
) {
145 if (freq
>= optimal
.frequency
) {
146 optimal
.frequency
= freq
;
147 optimal
.driver_data
= i
;
150 if (freq
<= suboptimal
.frequency
) {
151 suboptimal
.frequency
= freq
;
152 suboptimal
.driver_data
= i
;
156 case CPUFREQ_RELATION_L
:
157 if (freq
>= target_freq
) {
158 if (freq
<= optimal
.frequency
) {
159 optimal
.frequency
= freq
;
160 optimal
.driver_data
= i
;
163 if (freq
>= suboptimal
.frequency
) {
164 suboptimal
.frequency
= freq
;
165 suboptimal
.driver_data
= i
;
171 if (optimal
.driver_data
> i
) {
172 if (suboptimal
.driver_data
> i
)
174 *index
= suboptimal
.driver_data
;
176 *index
= optimal
.driver_data
;
178 pr_debug("target is %u (%u kHz, %u)\n", *index
, table
[*index
].frequency
,
179 table
[*index
].driver_data
);
183 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target
);
185 int cpufreq_frequency_table_get_index(struct cpufreq_policy
*policy
,
188 struct cpufreq_frequency_table
*table
;
191 table
= cpufreq_frequency_get_table(policy
->cpu
);
192 if (unlikely(!table
)) {
193 pr_debug("%s: Unable to find frequency table\n", __func__
);
197 for (i
= 0; table
[i
].frequency
!= CPUFREQ_TABLE_END
; i
++) {
198 if (table
[i
].frequency
== freq
)
204 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index
);
206 static DEFINE_PER_CPU(struct cpufreq_frequency_table
*, cpufreq_show_table
);
209 * show_available_freqs - show available frequencies for the specified CPU
211 static ssize_t
show_available_freqs(struct cpufreq_policy
*policy
, char *buf
,
215 unsigned int cpu
= policy
->cpu
;
217 struct cpufreq_frequency_table
*table
;
219 if (!per_cpu(cpufreq_show_table
, cpu
))
222 table
= per_cpu(cpufreq_show_table
, cpu
);
224 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
225 if (table
[i
].frequency
== CPUFREQ_ENTRY_INVALID
)
228 * show_boost = true and driver_data = BOOST freq
229 * display BOOST freqs
231 * show_boost = false and driver_data = BOOST freq
232 * show_boost = true and driver_data != BOOST freq
233 * continue - do not display anything
235 * show_boost = false and driver_data != BOOST freq
236 * display NON BOOST freqs
238 if (show_boost
^ (table
[i
].driver_data
== CPUFREQ_BOOST_FREQ
))
241 count
+= sprintf(&buf
[count
], "%d ", table
[i
].frequency
);
243 count
+= sprintf(&buf
[count
], "\n");
249 #define cpufreq_attr_available_freq(_name) \
250 struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
251 __ATTR_RO(_name##_frequencies)
254 * show_scaling_available_frequencies - show available normal frequencies for
257 static ssize_t
scaling_available_frequencies_show(struct cpufreq_policy
*policy
,
260 return show_available_freqs(policy
, buf
, false);
262 cpufreq_attr_available_freq(scaling_available
);
263 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs
);
266 * show_available_boost_freqs - show available boost frequencies for
269 static ssize_t
scaling_boost_frequencies_show(struct cpufreq_policy
*policy
,
272 return show_available_freqs(policy
, buf
, true);
274 cpufreq_attr_available_freq(scaling_boost
);
275 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs
);
277 struct freq_attr
*cpufreq_generic_attr
[] = {
278 &cpufreq_freq_attr_scaling_available_freqs
,
279 #ifdef CONFIG_CPU_FREQ_BOOST_SW
280 &cpufreq_freq_attr_scaling_boost_freqs
,
284 EXPORT_SYMBOL_GPL(cpufreq_generic_attr
);
287 * if you use these, you must assure that the frequency table is valid
288 * all the time between get_attr and put_attr!
290 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table
*table
,
293 pr_debug("setting show_table for cpu %u to %p\n", cpu
, table
);
294 per_cpu(cpufreq_show_table
, cpu
) = table
;
296 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr
);
298 void cpufreq_frequency_table_put_attr(unsigned int cpu
)
300 pr_debug("clearing show_table for cpu %u\n", cpu
);
301 per_cpu(cpufreq_show_table
, cpu
) = NULL
;
303 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr
);
305 int cpufreq_table_validate_and_show(struct cpufreq_policy
*policy
,
306 struct cpufreq_frequency_table
*table
)
308 int ret
= cpufreq_frequency_table_cpuinfo(policy
, table
);
311 cpufreq_frequency_table_get_attr(table
, policy
->cpu
);
315 EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show
);
317 void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy
*policy
)
319 pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n",
320 policy
->cpu
, policy
->last_cpu
);
321 per_cpu(cpufreq_show_table
, policy
->cpu
) = per_cpu(cpufreq_show_table
,
323 per_cpu(cpufreq_show_table
, policy
->last_cpu
) = NULL
;
326 struct cpufreq_frequency_table
*cpufreq_frequency_get_table(unsigned int cpu
)
328 return per_cpu(cpufreq_show_table
, cpu
);
330 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table
);
332 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
333 MODULE_DESCRIPTION("CPUfreq frequency table helpers");
334 MODULE_LICENSE("GPL");