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/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/cpufreq.h>
19 /*********************************************************************
20 * FREQUENCY TABLE HELPERS *
21 *********************************************************************/
23 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
24 struct cpufreq_frequency_table *table)
26 unsigned int min_freq = ~0;
27 unsigned int max_freq = 0;
30 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
31 unsigned int freq = table[i].frequency;
32 if (freq == CPUFREQ_ENTRY_INVALID) {
33 pr_debug("table entry %u is invalid, skipping\n", i);
37 pr_debug("table entry %u: %u kHz, %u index\n",
38 i, freq, table[i].index);
45 policy->min = policy->cpuinfo.min_freq = min_freq;
46 policy->max = policy->cpuinfo.max_freq = max_freq;
48 if (policy->min == ~0)
53 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
56 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
57 struct cpufreq_frequency_table *table)
59 unsigned int next_larger = ~0;
61 unsigned int count = 0;
63 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
64 policy->min, policy->max, policy->cpu);
66 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
67 policy->cpuinfo.max_freq);
69 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
70 unsigned int freq = table[i].frequency;
71 if (freq == CPUFREQ_ENTRY_INVALID)
73 if ((freq >= policy->min) && (freq <= policy->max))
75 else if ((next_larger > freq) && (freq > policy->max))
80 policy->max = next_larger;
82 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
83 policy->cpuinfo.max_freq);
85 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
86 policy->min, policy->max, policy->cpu);
90 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
93 int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
94 struct cpufreq_frequency_table *table,
95 unsigned int target_freq,
96 unsigned int relation,
99 struct cpufreq_frequency_table optimal = {
103 struct cpufreq_frequency_table suboptimal = {
109 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
110 target_freq, relation, policy->cpu);
113 case CPUFREQ_RELATION_H:
114 suboptimal.frequency = ~0;
116 case CPUFREQ_RELATION_L:
117 optimal.frequency = ~0;
121 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
122 unsigned int freq = table[i].frequency;
123 if (freq == CPUFREQ_ENTRY_INVALID)
125 if ((freq < policy->min) || (freq > policy->max))
128 case CPUFREQ_RELATION_H:
129 if (freq <= target_freq) {
130 if (freq >= optimal.frequency) {
131 optimal.frequency = freq;
135 if (freq <= suboptimal.frequency) {
136 suboptimal.frequency = freq;
137 suboptimal.index = i;
141 case CPUFREQ_RELATION_L:
142 if (freq >= target_freq) {
143 if (freq <= optimal.frequency) {
144 optimal.frequency = freq;
148 if (freq >= suboptimal.frequency) {
149 suboptimal.frequency = freq;
150 suboptimal.index = i;
156 if (optimal.index > i) {
157 if (suboptimal.index > i)
159 *index = suboptimal.index;
161 *index = optimal.index;
163 pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
164 table[*index].index);
168 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
170 static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
172 * show_available_freqs - show available frequencies for the specified CPU
174 static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
177 unsigned int cpu = policy->cpu;
179 struct cpufreq_frequency_table *table;
181 if (!per_cpu(cpufreq_show_table, cpu))
184 table = per_cpu(cpufreq_show_table, cpu);
186 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
187 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
189 count += sprintf(&buf[count], "%d ", table[i].frequency);
191 count += sprintf(&buf[count], "\n");
197 struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
198 .attr = { .name = "scaling_available_frequencies",
201 .show = show_available_freqs,
203 EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
206 * if you use these, you must assure that the frequency table is valid
207 * all the time between get_attr and put_attr!
209 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
212 pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
213 per_cpu(cpufreq_show_table, cpu) = table;
215 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
217 void cpufreq_frequency_table_put_attr(unsigned int cpu)
219 pr_debug("clearing show_table for cpu %u\n", cpu);
220 per_cpu(cpufreq_show_table, cpu) = NULL;
222 EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
224 void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy)
226 pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n",
227 policy->cpu, policy->last_cpu);
228 per_cpu(cpufreq_show_table, policy->cpu) = per_cpu(cpufreq_show_table,
230 per_cpu(cpufreq_show_table, policy->last_cpu) = NULL;
233 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
235 return per_cpu(cpufreq_show_table, cpu);
237 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
239 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
240 MODULE_DESCRIPTION("CPUfreq frequency table helpers");
241 MODULE_LICENSE("GPL");