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 /*********************************************************************
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 index\n",
36 i
, freq
, table
[i
].index
);
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;
59 unsigned int count
= 0;
61 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
62 policy
->min
, policy
->max
, policy
->cpu
);
64 if (!cpu_online(policy
->cpu
))
67 cpufreq_verify_within_limits(policy
, policy
->cpuinfo
.min_freq
,
68 policy
->cpuinfo
.max_freq
);
70 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
71 unsigned int freq
= table
[i
].frequency
;
72 if (freq
== CPUFREQ_ENTRY_INVALID
)
74 if ((freq
>= policy
->min
) && (freq
<= policy
->max
))
76 else if ((next_larger
> freq
) && (freq
> policy
->max
))
81 policy
->max
= next_larger
;
83 cpufreq_verify_within_limits(policy
, policy
->cpuinfo
.min_freq
,
84 policy
->cpuinfo
.max_freq
);
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 int cpufreq_frequency_table_target(struct cpufreq_policy
*policy
,
95 struct cpufreq_frequency_table
*table
,
96 unsigned int target_freq
,
97 unsigned int relation
,
100 struct cpufreq_frequency_table optimal
= {
104 struct cpufreq_frequency_table suboptimal
= {
110 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
111 target_freq
, relation
, policy
->cpu
);
114 case CPUFREQ_RELATION_H
:
115 suboptimal
.frequency
= ~0;
117 case CPUFREQ_RELATION_L
:
118 optimal
.frequency
= ~0;
122 if (!cpu_online(policy
->cpu
))
125 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
126 unsigned int freq
= table
[i
].frequency
;
127 if (freq
== CPUFREQ_ENTRY_INVALID
)
129 if ((freq
< policy
->min
) || (freq
> policy
->max
))
132 case CPUFREQ_RELATION_H
:
133 if (freq
<= target_freq
) {
134 if (freq
>= optimal
.frequency
) {
135 optimal
.frequency
= freq
;
139 if (freq
<= suboptimal
.frequency
) {
140 suboptimal
.frequency
= freq
;
141 suboptimal
.index
= i
;
145 case CPUFREQ_RELATION_L
:
146 if (freq
>= target_freq
) {
147 if (freq
<= optimal
.frequency
) {
148 optimal
.frequency
= freq
;
152 if (freq
>= suboptimal
.frequency
) {
153 suboptimal
.frequency
= freq
;
154 suboptimal
.index
= i
;
160 if (optimal
.index
> i
) {
161 if (suboptimal
.index
> i
)
163 *index
= suboptimal
.index
;
165 *index
= optimal
.index
;
167 pr_debug("target is %u (%u kHz, %u)\n", *index
, table
[*index
].frequency
,
168 table
[*index
].index
);
172 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target
);
174 static DEFINE_PER_CPU(struct cpufreq_frequency_table
*, cpufreq_show_table
);
176 * show_available_freqs - show available frequencies for the specified CPU
178 static ssize_t
show_available_freqs(struct cpufreq_policy
*policy
, char *buf
)
181 unsigned int cpu
= policy
->cpu
;
183 struct cpufreq_frequency_table
*table
;
185 if (!per_cpu(cpufreq_show_table
, cpu
))
188 table
= per_cpu(cpufreq_show_table
, cpu
);
190 for (i
= 0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
191 if (table
[i
].frequency
== CPUFREQ_ENTRY_INVALID
)
193 count
+= sprintf(&buf
[count
], "%d ", table
[i
].frequency
);
195 count
+= sprintf(&buf
[count
], "\n");
201 struct freq_attr cpufreq_freq_attr_scaling_available_freqs
= {
202 .attr
= { .name
= "scaling_available_frequencies",
205 .show
= show_available_freqs
,
207 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs
);
210 * if you use these, you must assure that the frequency table is valid
211 * all the time between get_attr and put_attr!
213 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table
*table
,
216 pr_debug("setting show_table for cpu %u to %p\n", cpu
, table
);
217 per_cpu(cpufreq_show_table
, cpu
) = table
;
219 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr
);
221 void cpufreq_frequency_table_put_attr(unsigned int cpu
)
223 pr_debug("clearing show_table for cpu %u\n", cpu
);
224 per_cpu(cpufreq_show_table
, cpu
) = NULL
;
226 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr
);
228 struct cpufreq_frequency_table
*cpufreq_frequency_get_table(unsigned int cpu
)
230 return per_cpu(cpufreq_show_table
, cpu
);
232 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table
);
234 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
235 MODULE_DESCRIPTION("CPUfreq frequency table helpers");
236 MODULE_LICENSE("GPL");