blob: 9071d80fbba2427d6b29c911832e169bf201466a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/freq_table.c
3 *
4 * Copyright (C) 2002 - 2003 Dominik Brodowski
Dominik Brodowski4f743692008-05-22 08:52:05 +02005 *
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.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/cpufreq.h>
16
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053017#define dprintk(msg...) \
18 cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "freq-table", msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*********************************************************************
21 * FREQUENCY TABLE HELPERS *
22 *********************************************************************/
23
24int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
25 struct cpufreq_frequency_table *table)
26{
27 unsigned int min_freq = ~0;
28 unsigned int max_freq = 0;
Dave Jones355eb312006-05-30 17:58:41 -040029 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
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);
35
36 continue;
37 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053038 dprintk("table entry %u: %u kHz, %u index\n",
39 i, freq, table[i].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (freq < min_freq)
41 min_freq = freq;
42 if (freq > max_freq)
43 max_freq = freq;
44 }
45
46 policy->min = policy->cpuinfo.min_freq = min_freq;
47 policy->max = policy->cpuinfo.max_freq = max_freq;
48
49 if (policy->min == ~0)
50 return -EINVAL;
51 else
52 return 0;
53}
54EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
55
56
57int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
58 struct cpufreq_frequency_table *table)
59{
60 unsigned int next_larger = ~0;
Dave Jones55579762006-05-30 17:59:48 -040061 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned int count = 0;
63
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053064 dprintk("request for verification of policy (%u - %u kHz) for cpu %u\n",
65 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if (!cpu_online(policy->cpu))
68 return -EINVAL;
69
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053070 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
71 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 for (i=0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
74 unsigned int freq = table[i].frequency;
75 if (freq == CPUFREQ_ENTRY_INVALID)
76 continue;
77 if ((freq >= policy->min) && (freq <= policy->max))
78 count++;
79 else if ((next_larger > freq) && (freq > policy->max))
80 next_larger = freq;
81 }
82
83 if (!count)
84 policy->max = next_larger;
85
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053086 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
87 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053089 dprintk("verification lead to (%u - %u kHz) for cpu %u\n",
90 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 return 0;
93}
94EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
95
96
97int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
98 struct cpufreq_frequency_table *table,
99 unsigned int target_freq,
100 unsigned int relation,
101 unsigned int *index)
102{
Dave Jones484944a2006-05-30 18:09:31 -0400103 struct cpufreq_frequency_table optimal = {
104 .index = ~0,
105 .frequency = 0,
106 };
107 struct cpufreq_frequency_table suboptimal = {
108 .index = ~0,
109 .frequency = 0,
110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned int i;
112
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530113 dprintk("request for target %u kHz (relation: %u) for cpu %u\n",
114 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 switch (relation) {
117 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 suboptimal.frequency = ~0;
119 break;
120 case CPUFREQ_RELATION_L:
121 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 break;
123 }
124
125 if (!cpu_online(policy->cpu))
126 return -EINVAL;
127
128 for (i=0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
129 unsigned int freq = table[i].frequency;
130 if (freq == CPUFREQ_ENTRY_INVALID)
131 continue;
132 if ((freq < policy->min) || (freq > policy->max))
133 continue;
134 switch(relation) {
135 case CPUFREQ_RELATION_H:
136 if (freq <= target_freq) {
137 if (freq >= optimal.frequency) {
138 optimal.frequency = freq;
139 optimal.index = i;
140 }
141 } else {
142 if (freq <= suboptimal.frequency) {
143 suboptimal.frequency = freq;
144 suboptimal.index = i;
145 }
146 }
147 break;
148 case CPUFREQ_RELATION_L:
149 if (freq >= target_freq) {
150 if (freq <= optimal.frequency) {
151 optimal.frequency = freq;
152 optimal.index = i;
153 }
154 } else {
155 if (freq >= suboptimal.frequency) {
156 suboptimal.frequency = freq;
157 suboptimal.index = i;
158 }
159 }
160 break;
161 }
162 }
163 if (optimal.index > i) {
164 if (suboptimal.index > i)
165 return -EINVAL;
166 *index = suboptimal.index;
167 } else
168 *index = optimal.index;
169
170 dprintk("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
171 table[*index].index);
172
173 return 0;
174}
175EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
176
Mike Travis7a6aedf2008-03-25 15:06:53 -0700177static DEFINE_PER_CPU(struct cpufreq_frequency_table *, show_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800179 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181static ssize_t show_available_freqs (struct cpufreq_policy *policy, char *buf)
182{
183 unsigned int i = 0;
184 unsigned int cpu = policy->cpu;
185 ssize_t count = 0;
186 struct cpufreq_frequency_table *table;
187
Mike Travis7a6aedf2008-03-25 15:06:53 -0700188 if (!per_cpu(show_table, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -ENODEV;
190
Mike Travis7a6aedf2008-03-25 15:06:53 -0700191 table = per_cpu(show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 for (i=0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
194 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
195 continue;
196 count += sprintf(&buf[count], "%d ", table[i].frequency);
197 }
198 count += sprintf(&buf[count], "\n");
199
200 return count;
201
202}
203
204struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530205 .attr = { .name = "scaling_available_frequencies",
206 .mode = 0444,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530207 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .show = show_available_freqs,
209};
210EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
211
212/*
213 * if you use these, you must assure that the frequency table is valid
214 * all the time between get_attr and put_attr!
215 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500216void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unsigned int cpu)
218{
219 dprintk("setting show_table for cpu %u to %p\n", cpu, table);
Mike Travis7a6aedf2008-03-25 15:06:53 -0700220 per_cpu(show_table, cpu) = table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
223
224void cpufreq_frequency_table_put_attr(unsigned int cpu)
225{
226 dprintk("clearing show_table for cpu %u\n", cpu);
Mike Travis7a6aedf2008-03-25 15:06:53 -0700227 per_cpu(show_table, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
230
231struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
232{
Mike Travis7a6aedf2008-03-25 15:06:53 -0700233 return per_cpu(show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
236
237MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>");
238MODULE_DESCRIPTION ("CPUfreq frequency table helpers");
239MODULE_LICENSE ("GPL");