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 pr_debug("table entry %u: %u kHz, %u driver_data\n",
36 i
, freq
, table
[i
].driver_data
);
43 policy
->min
= policy
->cpuinfo
.min_freq
= min_freq
;
44 policy
->max
= policy
->cpuinfo
.max_freq
= max_freq
;
46 if (policy
->min
== ~0)
51 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo
);
54 int cpufreq_frequency_table_verify(struct cpufreq_policy
*policy
,
55 struct cpufreq_frequency_table
*table
)
57 unsigned int next_larger
= ~0, freq
, i
= 0;
60 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
61 policy
->min
, policy
->max
, policy
->cpu
);
63 cpufreq_verify_within_cpu_limits(policy
);
65 for (; freq
= table
[i
].frequency
, freq
!= CPUFREQ_TABLE_END
; i
++) {
66 if (freq
== CPUFREQ_ENTRY_INVALID
)
68 if ((freq
>= policy
->min
) && (freq
<= policy
->max
)) {
73 if ((next_larger
> freq
) && (freq
> policy
->max
))
78 policy
->max
= next_larger
;
79 cpufreq_verify_within_cpu_limits(policy
);
82 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
83 policy
->min
, policy
->max
, policy
->cpu
);
87 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify
);
90 * Generic routine to verify policy & frequency table, requires driver to call
91 * cpufreq_frequency_table_get_attr() prior to it.
93 int cpufreq_generic_frequency_table_verify(struct cpufreq_policy
*policy
)
95 struct cpufreq_frequency_table
*table
=
96 cpufreq_frequency_get_table(policy
->cpu
);
100 return cpufreq_frequency_table_verify(policy
, table
);
102 EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify
);
104 int cpufreq_frequency_table_target(struct cpufreq_policy
*policy
,
105 struct cpufreq_frequency_table
*table
,
106 unsigned int target_freq
,
107 unsigned int relation
,
110 struct cpufreq_frequency_table optimal
= {
114 struct cpufreq_frequency_table suboptimal
= {
120 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
121 target_freq
, relation
, policy
->cpu
);
124 case CPUFREQ_RELATION_H
:
125 suboptimal
.frequency
= ~0;
127 case CPUFREQ_RELATION_L
:
128 optimal
.frequency
= ~0;
132 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
133 unsigned int freq
= table
[i
].frequency
;
134 if (freq
== CPUFREQ_ENTRY_INVALID
)
136 if ((freq
< policy
->min
) || (freq
> policy
->max
))
139 case CPUFREQ_RELATION_H
:
140 if (freq
<= target_freq
) {
141 if (freq
>= optimal
.frequency
) {
142 optimal
.frequency
= freq
;
143 optimal
.driver_data
= i
;
146 if (freq
<= suboptimal
.frequency
) {
147 suboptimal
.frequency
= freq
;
148 suboptimal
.driver_data
= i
;
152 case CPUFREQ_RELATION_L
:
153 if (freq
>= target_freq
) {
154 if (freq
<= optimal
.frequency
) {
155 optimal
.frequency
= freq
;
156 optimal
.driver_data
= i
;
159 if (freq
>= suboptimal
.frequency
) {
160 suboptimal
.frequency
= freq
;
161 suboptimal
.driver_data
= i
;
167 if (optimal
.driver_data
> i
) {
168 if (suboptimal
.driver_data
> i
)
170 *index
= suboptimal
.driver_data
;
172 *index
= optimal
.driver_data
;
174 pr_debug("target is %u (%u kHz, %u)\n", *index
, table
[*index
].frequency
,
175 table
[*index
].driver_data
);
179 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target
);
181 static DEFINE_PER_CPU(struct cpufreq_frequency_table
*, cpufreq_show_table
);
183 * show_available_freqs - show available frequencies for the specified CPU
185 static ssize_t
show_available_freqs(struct cpufreq_policy
*policy
, char *buf
)
188 unsigned int cpu
= policy
->cpu
;
190 struct cpufreq_frequency_table
*table
;
192 if (!per_cpu(cpufreq_show_table
, cpu
))
195 table
= per_cpu(cpufreq_show_table
, cpu
);
197 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
198 if (table
[i
].frequency
== CPUFREQ_ENTRY_INVALID
)
200 count
+= sprintf(&buf
[count
], "%d ", table
[i
].frequency
);
202 count
+= sprintf(&buf
[count
], "\n");
208 struct freq_attr cpufreq_freq_attr_scaling_available_freqs
= {
209 .attr
= { .name
= "scaling_available_frequencies",
212 .show
= show_available_freqs
,
214 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs
);
216 struct freq_attr
*cpufreq_generic_attr
[] = {
217 &cpufreq_freq_attr_scaling_available_freqs
,
220 EXPORT_SYMBOL_GPL(cpufreq_generic_attr
);
223 * if you use these, you must assure that the frequency table is valid
224 * all the time between get_attr and put_attr!
226 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table
*table
,
229 pr_debug("setting show_table for cpu %u to %p\n", cpu
, table
);
230 per_cpu(cpufreq_show_table
, cpu
) = table
;
232 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr
);
234 void cpufreq_frequency_table_put_attr(unsigned int cpu
)
236 pr_debug("clearing show_table for cpu %u\n", cpu
);
237 per_cpu(cpufreq_show_table
, cpu
) = NULL
;
239 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr
);
241 int cpufreq_table_validate_and_show(struct cpufreq_policy
*policy
,
242 struct cpufreq_frequency_table
*table
)
244 int ret
= cpufreq_frequency_table_cpuinfo(policy
, table
);
247 cpufreq_frequency_table_get_attr(table
, policy
->cpu
);
251 EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show
);
253 void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy
*policy
)
255 pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n",
256 policy
->cpu
, policy
->last_cpu
);
257 per_cpu(cpufreq_show_table
, policy
->cpu
) = per_cpu(cpufreq_show_table
,
259 per_cpu(cpufreq_show_table
, policy
->last_cpu
) = NULL
;
262 struct cpufreq_frequency_table
*cpufreq_frequency_get_table(unsigned int cpu
)
264 return per_cpu(cpufreq_show_table
, cpu
);
266 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table
);
268 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
269 MODULE_DESCRIPTION("CPUfreq frequency table helpers");
270 MODULE_LICENSE("GPL");