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 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/cpufreq.h>
17 #define dprintk(msg...) \
18 cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "freq-table", msg)
20 /*********************************************************************
21 * FREQUENCY TABLE HELPERS *
22 *********************************************************************/
24 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy
*policy
,
25 struct cpufreq_frequency_table
*table
)
27 unsigned int min_freq
= ~0;
28 unsigned int max_freq
= 0;
31 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
32 unsigned int freq
= table
[i
].frequency
;
33 if (freq
== CPUFREQ_ENTRY_INVALID
) {
34 dprintk("table entry %u is invalid, skipping\n", i
);
38 dprintk("table entry %u: %u kHz, %u index\n",
39 i
, freq
, table
[i
].index
);
46 policy
->min
= policy
->cpuinfo
.min_freq
= min_freq
;
47 policy
->max
= policy
->cpuinfo
.max_freq
= max_freq
;
49 if (policy
->min
== ~0)
54 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo
);
57 int cpufreq_frequency_table_verify(struct cpufreq_policy
*policy
,
58 struct cpufreq_frequency_table
*table
)
60 unsigned int next_larger
= ~0;
62 unsigned int count
= 0;
64 dprintk("request for verification of policy (%u - %u kHz) for cpu %u\n",
65 policy
->min
, policy
->max
, policy
->cpu
);
67 if (!cpu_online(policy
->cpu
))
70 cpufreq_verify_within_limits(policy
, policy
->cpuinfo
.min_freq
,
71 policy
->cpuinfo
.max_freq
);
73 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
74 unsigned int freq
= table
[i
].frequency
;
75 if (freq
== CPUFREQ_ENTRY_INVALID
)
77 if ((freq
>= policy
->min
) && (freq
<= policy
->max
))
79 else if ((next_larger
> freq
) && (freq
> policy
->max
))
84 policy
->max
= next_larger
;
86 cpufreq_verify_within_limits(policy
, policy
->cpuinfo
.min_freq
,
87 policy
->cpuinfo
.max_freq
);
89 dprintk("verification lead to (%u - %u kHz) for cpu %u\n",
90 policy
->min
, policy
->max
, policy
->cpu
);
94 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify
);
97 int cpufreq_frequency_table_target(struct cpufreq_policy
*policy
,
98 struct cpufreq_frequency_table
*table
,
99 unsigned int target_freq
,
100 unsigned int relation
,
103 struct cpufreq_frequency_table optimal
= {
107 struct cpufreq_frequency_table suboptimal
= {
113 dprintk("request for target %u kHz (relation: %u) for cpu %u\n",
114 target_freq
, relation
, policy
->cpu
);
117 case CPUFREQ_RELATION_H
:
118 suboptimal
.frequency
= ~0;
120 case CPUFREQ_RELATION_L
:
121 optimal
.frequency
= ~0;
125 if (!cpu_online(policy
->cpu
))
128 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
129 unsigned int freq
= table
[i
].frequency
;
130 if (freq
== CPUFREQ_ENTRY_INVALID
)
132 if ((freq
< policy
->min
) || (freq
> policy
->max
))
135 case CPUFREQ_RELATION_H
:
136 if (freq
<= target_freq
) {
137 if (freq
>= optimal
.frequency
) {
138 optimal
.frequency
= freq
;
142 if (freq
<= suboptimal
.frequency
) {
143 suboptimal
.frequency
= freq
;
144 suboptimal
.index
= i
;
148 case CPUFREQ_RELATION_L
:
149 if (freq
>= target_freq
) {
150 if (freq
<= optimal
.frequency
) {
151 optimal
.frequency
= freq
;
155 if (freq
>= suboptimal
.frequency
) {
156 suboptimal
.frequency
= freq
;
157 suboptimal
.index
= i
;
163 if (optimal
.index
> i
) {
164 if (suboptimal
.index
> i
)
166 *index
= suboptimal
.index
;
168 *index
= optimal
.index
;
170 dprintk("target is %u (%u kHz, %u)\n", *index
, table
[*index
].frequency
,
171 table
[*index
].index
);
175 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target
);
177 static DEFINE_PER_CPU(struct cpufreq_frequency_table
*, show_table
);
179 * show_available_freqs - show available frequencies for the specified CPU
181 static ssize_t
show_available_freqs(struct cpufreq_policy
*policy
, char *buf
)
184 unsigned int cpu
= policy
->cpu
;
186 struct cpufreq_frequency_table
*table
;
188 if (!per_cpu(show_table
, cpu
))
191 table
= per_cpu(show_table
, cpu
);
193 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
194 if (table
[i
].frequency
== CPUFREQ_ENTRY_INVALID
)
196 count
+= sprintf(&buf
[count
], "%d ", table
[i
].frequency
);
198 count
+= sprintf(&buf
[count
], "\n");
204 struct freq_attr cpufreq_freq_attr_scaling_available_freqs
= {
205 .attr
= { .name
= "scaling_available_frequencies",
208 .show
= show_available_freqs
,
210 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs
);
213 * if you use these, you must assure that the frequency table is valid
214 * all the time between get_attr and put_attr!
216 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table
*table
,
219 dprintk("setting show_table for cpu %u to %p\n", cpu
, table
);
220 per_cpu(show_table
, cpu
) = table
;
222 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr
);
224 void cpufreq_frequency_table_put_attr(unsigned int cpu
)
226 dprintk("clearing show_table for cpu %u\n", cpu
);
227 per_cpu(show_table
, cpu
) = NULL
;
229 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr
);
231 struct cpufreq_frequency_table
*cpufreq_frequency_get_table(unsigned int cpu
)
233 return per_cpu(show_table
, cpu
);
235 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table
);
237 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
238 MODULE_DESCRIPTION("CPUfreq frequency table helpers");
239 MODULE_LICENSE("GPL");