blob: 49cda256efb2149c2311e588d9cdb151f87dc103 [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
Viresh Kumardb701152012-10-23 01:29:03 +020012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/cpufreq.h>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/*********************************************************************
20 * FREQUENCY TABLE HELPERS *
21 *********************************************************************/
22
23int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
24 struct cpufreq_frequency_table *table)
25{
26 unsigned int min_freq = ~0;
27 unsigned int max_freq = 0;
Dave Jones355eb312006-05-30 17:58:41 -040028 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Dave Jones97acec52009-01-18 01:56:41 -050030 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 unsigned int freq = table[i].frequency;
32 if (freq == CPUFREQ_ENTRY_INVALID) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020033 pr_debug("table entry %u is invalid, skipping\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 continue;
36 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020037 pr_debug("table entry %u: %u kHz, %u index\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053038 i, freq, table[i].index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (freq < min_freq)
40 min_freq = freq;
41 if (freq > max_freq)
42 max_freq = freq;
43 }
44
45 policy->min = policy->cpuinfo.min_freq = min_freq;
46 policy->max = policy->cpuinfo.max_freq = max_freq;
47
48 if (policy->min == ~0)
49 return -EINVAL;
50 else
51 return 0;
52}
53EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
54
55
56int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
57 struct cpufreq_frequency_table *table)
58{
59 unsigned int next_larger = ~0;
Dave Jones55579762006-05-30 17:59:48 -040060 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 unsigned int count = 0;
62
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020063 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053064 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 if (!cpu_online(policy->cpu))
67 return -EINVAL;
68
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053069 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
70 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Dave Jones97acec52009-01-18 01:56:41 -050072 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unsigned int freq = table[i].frequency;
74 if (freq == CPUFREQ_ENTRY_INVALID)
75 continue;
76 if ((freq >= policy->min) && (freq <= policy->max))
77 count++;
78 else if ((next_larger > freq) && (freq > policy->max))
79 next_larger = freq;
80 }
81
82 if (!count)
83 policy->max = next_larger;
84
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053085 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
86 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020088 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053089 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 return 0;
92}
93EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
94
95
96int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
97 struct cpufreq_frequency_table *table,
98 unsigned int target_freq,
99 unsigned int relation,
100 unsigned int *index)
101{
Dave Jones484944a2006-05-30 18:09:31 -0400102 struct cpufreq_frequency_table optimal = {
103 .index = ~0,
104 .frequency = 0,
105 };
106 struct cpufreq_frequency_table suboptimal = {
107 .index = ~0,
108 .frequency = 0,
109 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unsigned int i;
111
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200112 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530113 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 switch (relation) {
116 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 suboptimal.frequency = ~0;
118 break;
119 case CPUFREQ_RELATION_L:
120 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122 }
123
124 if (!cpu_online(policy->cpu))
125 return -EINVAL;
126
Dave Jones97acec52009-01-18 01:56:41 -0500127 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 unsigned int freq = table[i].frequency;
129 if (freq == CPUFREQ_ENTRY_INVALID)
130 continue;
131 if ((freq < policy->min) || (freq > policy->max))
132 continue;
Dave Jones97acec52009-01-18 01:56:41 -0500133 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 case CPUFREQ_RELATION_H:
135 if (freq <= target_freq) {
136 if (freq >= optimal.frequency) {
137 optimal.frequency = freq;
138 optimal.index = i;
139 }
140 } else {
141 if (freq <= suboptimal.frequency) {
142 suboptimal.frequency = freq;
143 suboptimal.index = i;
144 }
145 }
146 break;
147 case CPUFREQ_RELATION_L:
148 if (freq >= target_freq) {
149 if (freq <= optimal.frequency) {
150 optimal.frequency = freq;
151 optimal.index = i;
152 }
153 } else {
154 if (freq >= suboptimal.frequency) {
155 suboptimal.frequency = freq;
156 suboptimal.index = i;
157 }
158 }
159 break;
160 }
161 }
162 if (optimal.index > i) {
163 if (suboptimal.index > i)
164 return -EINVAL;
165 *index = suboptimal.index;
166 } else
167 *index = optimal.index;
168
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200169 pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 table[*index].index);
171
172 return 0;
173}
174EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
175
Tejun Heof1625062009-10-29 22:34:13 +0900176static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800178 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
Dave Jones97acec52009-01-18 01:56:41 -0500180static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 unsigned int i = 0;
183 unsigned int cpu = policy->cpu;
184 ssize_t count = 0;
185 struct cpufreq_frequency_table *table;
186
Tejun Heof1625062009-10-29 22:34:13 +0900187 if (!per_cpu(cpufreq_show_table, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -ENODEV;
189
Tejun Heof1625062009-10-29 22:34:13 +0900190 table = per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dave Jones97acec52009-01-18 01:56:41 -0500192 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
194 continue;
195 count += sprintf(&buf[count], "%d ", table[i].frequency);
196 }
197 count += sprintf(&buf[count], "\n");
198
199 return count;
200
201}
202
203struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530204 .attr = { .name = "scaling_available_frequencies",
205 .mode = 0444,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530206 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .show = show_available_freqs,
208};
209EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
210
211/*
212 * if you use these, you must assure that the frequency table is valid
213 * all the time between get_attr and put_attr!
214 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500215void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 unsigned int cpu)
217{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200218 pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
Tejun Heof1625062009-10-29 22:34:13 +0900219 per_cpu(cpufreq_show_table, cpu) = table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
222
223void cpufreq_frequency_table_put_attr(unsigned int cpu)
224{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200225 pr_debug("clearing show_table for cpu %u\n", cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900226 per_cpu(cpufreq_show_table, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
229
230struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
231{
Tejun Heof1625062009-10-29 22:34:13 +0900232 return per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
235
Dave Jones97acec52009-01-18 01:56:41 -0500236MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
237MODULE_DESCRIPTION("CPUfreq frequency table helpers");
238MODULE_LICENSE("GPL");