2 * linux/drivers/cpufreq/freq_table.c
4 * Copyright (C) 2002 - 2003 Dominik Brodowski
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/cpufreq.h>
12 #define dprintk(msg...) \
13 cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "freq-table", msg)
15 /*********************************************************************
16 * FREQUENCY TABLE HELPERS *
17 *********************************************************************/
19 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy
*policy
,
20 struct cpufreq_frequency_table
*table
)
22 unsigned int min_freq
= ~0;
23 unsigned int max_freq
= 0;
26 for (i
=0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
27 unsigned int freq
= table
[i
].frequency
;
28 if (freq
== CPUFREQ_ENTRY_INVALID
) {
29 dprintk("table entry %u is invalid, skipping\n", i
);
33 dprintk("table entry %u: %u kHz, %u index\n",
34 i
, freq
, table
[i
].index
);
41 policy
->min
= policy
->cpuinfo
.min_freq
= min_freq
;
42 policy
->max
= policy
->cpuinfo
.max_freq
= max_freq
;
44 if (policy
->min
== ~0)
49 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo
);
52 int cpufreq_frequency_table_verify(struct cpufreq_policy
*policy
,
53 struct cpufreq_frequency_table
*table
)
55 unsigned int next_larger
= ~0;
57 unsigned int count
= 0;
59 dprintk("request for verification of policy (%u - %u kHz) for cpu %u\n",
60 policy
->min
, policy
->max
, policy
->cpu
);
62 if (!cpu_online(policy
->cpu
))
65 cpufreq_verify_within_limits(policy
, policy
->cpuinfo
.min_freq
,
66 policy
->cpuinfo
.max_freq
);
68 for (i
=0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
69 unsigned int freq
= table
[i
].frequency
;
70 if (freq
== CPUFREQ_ENTRY_INVALID
)
72 if ((freq
>= policy
->min
) && (freq
<= policy
->max
))
74 else if ((next_larger
> freq
) && (freq
> policy
->max
))
79 policy
->max
= next_larger
;
81 cpufreq_verify_within_limits(policy
, policy
->cpuinfo
.min_freq
,
82 policy
->cpuinfo
.max_freq
);
84 dprintk("verification lead to (%u - %u kHz) for cpu %u\n",
85 policy
->min
, policy
->max
, policy
->cpu
);
89 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify
);
92 int cpufreq_frequency_table_target(struct cpufreq_policy
*policy
,
93 struct cpufreq_frequency_table
*table
,
94 unsigned int target_freq
,
95 unsigned int relation
,
98 struct cpufreq_frequency_table optimal
= {
102 struct cpufreq_frequency_table suboptimal
= {
108 dprintk("request for target %u kHz (relation: %u) for cpu %u\n",
109 target_freq
, relation
, policy
->cpu
);
112 case CPUFREQ_RELATION_H
:
113 suboptimal
.frequency
= ~0;
115 case CPUFREQ_RELATION_L
:
116 optimal
.frequency
= ~0;
120 if (!cpu_online(policy
->cpu
))
123 for (i
=0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
124 unsigned int freq
= table
[i
].frequency
;
125 if (freq
== CPUFREQ_ENTRY_INVALID
)
127 if ((freq
< policy
->min
) || (freq
> policy
->max
))
130 case CPUFREQ_RELATION_H
:
131 if (freq
<= target_freq
) {
132 if (freq
>= optimal
.frequency
) {
133 optimal
.frequency
= freq
;
137 if (freq
<= suboptimal
.frequency
) {
138 suboptimal
.frequency
= freq
;
139 suboptimal
.index
= i
;
143 case CPUFREQ_RELATION_L
:
144 if (freq
>= target_freq
) {
145 if (freq
<= optimal
.frequency
) {
146 optimal
.frequency
= freq
;
150 if (freq
>= suboptimal
.frequency
) {
151 suboptimal
.frequency
= freq
;
152 suboptimal
.index
= i
;
158 if (optimal
.index
> i
) {
159 if (suboptimal
.index
> i
)
161 *index
= suboptimal
.index
;
163 *index
= optimal
.index
;
165 dprintk("target is %u (%u kHz, %u)\n", *index
, table
[*index
].frequency
,
166 table
[*index
].index
);
170 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target
);
172 static struct cpufreq_frequency_table
*show_table
[NR_CPUS
];
174 * show_scaling_governor - show the current policy for the specified CPU
176 static ssize_t
show_available_freqs (struct cpufreq_policy
*policy
, char *buf
)
179 unsigned int cpu
= policy
->cpu
;
181 struct cpufreq_frequency_table
*table
;
183 if (!show_table
[cpu
])
186 table
= show_table
[cpu
];
188 for (i
=0; (table
[i
].frequency
!= CPUFREQ_TABLE_END
); i
++) {
189 if (table
[i
].frequency
== CPUFREQ_ENTRY_INVALID
)
191 count
+= sprintf(&buf
[count
], "%d ", table
[i
].frequency
);
193 count
+= sprintf(&buf
[count
], "\n");
199 struct freq_attr cpufreq_freq_attr_scaling_available_freqs
= {
200 .attr
= { .name
= "scaling_available_frequencies",
203 .show
= show_available_freqs
,
205 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs
);
208 * if you use these, you must assure that the frequency table is valid
209 * all the time between get_attr and put_attr!
211 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table
*table
,
214 dprintk("setting show_table for cpu %u to %p\n", cpu
, table
);
215 show_table
[cpu
] = table
;
217 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr
);
219 void cpufreq_frequency_table_put_attr(unsigned int cpu
)
221 dprintk("clearing show_table for cpu %u\n", cpu
);
222 show_table
[cpu
] = NULL
;
224 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr
);
226 struct cpufreq_frequency_table
*cpufreq_frequency_get_table(unsigned int cpu
)
228 return show_table
[cpu
];
230 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table
);
232 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>");
233 MODULE_DESCRIPTION ("CPUfreq frequency table helpers");
234 MODULE_LICENSE ("GPL");