blob: 11f6fa92a58dce5a6b9df7d2ea0f929655db7357 [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/cpufreq.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053015#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017/*********************************************************************
18 * FREQUENCY TABLE HELPERS *
19 *********************************************************************/
20
21int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
22 struct cpufreq_frequency_table *table)
23{
24 unsigned int min_freq = ~0;
25 unsigned int max_freq = 0;
Dave Jones355eb312006-05-30 17:58:41 -040026 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Dave Jones97acec52009-01-18 01:56:41 -050028 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 unsigned int freq = table[i].frequency;
30 if (freq == CPUFREQ_ENTRY_INVALID) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020031 pr_debug("table entry %u is invalid, skipping\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 continue;
34 }
Viresh Kumar50701582013-03-30 16:25:15 +053035 pr_debug("table entry %u: %u kHz, %u driver_data\n",
36 i, freq, table[i].driver_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 if (freq < min_freq)
38 min_freq = freq;
39 if (freq > max_freq)
40 max_freq = freq;
41 }
42
43 policy->min = policy->cpuinfo.min_freq = min_freq;
44 policy->max = policy->cpuinfo.max_freq = max_freq;
45
46 if (policy->min == ~0)
47 return -EINVAL;
48 else
49 return 0;
50}
51EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
52
53
54int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
55 struct cpufreq_frequency_table *table)
56{
57 unsigned int next_larger = ~0;
Dave Jones55579762006-05-30 17:59:48 -040058 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned int count = 0;
60
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020061 pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053062 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053064 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
65 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Jones97acec52009-01-18 01:56:41 -050067 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned int freq = table[i].frequency;
69 if (freq == CPUFREQ_ENTRY_INVALID)
70 continue;
71 if ((freq >= policy->min) && (freq <= policy->max))
72 count++;
73 else if ((next_larger > freq) && (freq > policy->max))
74 next_larger = freq;
75 }
76
77 if (!count)
78 policy->max = next_larger;
79
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053080 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
81 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020083 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053084 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 return 0;
87}
88EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
89
90
91int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
92 struct cpufreq_frequency_table *table,
93 unsigned int target_freq,
94 unsigned int relation,
95 unsigned int *index)
96{
Dave Jones484944a2006-05-30 18:09:31 -040097 struct cpufreq_frequency_table optimal = {
Viresh Kumar50701582013-03-30 16:25:15 +053098 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -040099 .frequency = 0,
100 };
101 struct cpufreq_frequency_table suboptimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530102 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400103 .frequency = 0,
104 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 unsigned int i;
106
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200107 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530108 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 switch (relation) {
111 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 suboptimal.frequency = ~0;
113 break;
114 case CPUFREQ_RELATION_L:
115 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 break;
117 }
118
Dave Jones97acec52009-01-18 01:56:41 -0500119 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned int freq = table[i].frequency;
121 if (freq == CPUFREQ_ENTRY_INVALID)
122 continue;
123 if ((freq < policy->min) || (freq > policy->max))
124 continue;
Dave Jones97acec52009-01-18 01:56:41 -0500125 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 case CPUFREQ_RELATION_H:
127 if (freq <= target_freq) {
128 if (freq >= optimal.frequency) {
129 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530130 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132 } else {
133 if (freq <= suboptimal.frequency) {
134 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530135 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 }
138 break;
139 case CPUFREQ_RELATION_L:
140 if (freq >= target_freq) {
141 if (freq <= optimal.frequency) {
142 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530143 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 } else {
146 if (freq >= suboptimal.frequency) {
147 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530148 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 }
151 break;
152 }
153 }
Viresh Kumar50701582013-03-30 16:25:15 +0530154 if (optimal.driver_data > i) {
155 if (suboptimal.driver_data > i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -EINVAL;
Viresh Kumar50701582013-03-30 16:25:15 +0530157 *index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 } else
Viresh Kumar50701582013-03-30 16:25:15 +0530159 *index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200161 pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
Viresh Kumar50701582013-03-30 16:25:15 +0530162 table[*index].driver_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return 0;
165}
166EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
167
Tejun Heof1625062009-10-29 22:34:13 +0900168static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800170 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Dave Jones97acec52009-01-18 01:56:41 -0500172static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 unsigned int i = 0;
175 unsigned int cpu = policy->cpu;
176 ssize_t count = 0;
177 struct cpufreq_frequency_table *table;
178
Tejun Heof1625062009-10-29 22:34:13 +0900179 if (!per_cpu(cpufreq_show_table, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -ENODEV;
181
Tejun Heof1625062009-10-29 22:34:13 +0900182 table = per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Dave Jones97acec52009-01-18 01:56:41 -0500184 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
186 continue;
187 count += sprintf(&buf[count], "%d ", table[i].frequency);
188 }
189 count += sprintf(&buf[count], "\n");
190
191 return count;
192
193}
194
195struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530196 .attr = { .name = "scaling_available_frequencies",
197 .mode = 0444,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530198 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .show = show_available_freqs,
200};
201EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
202
203/*
204 * if you use these, you must assure that the frequency table is valid
205 * all the time between get_attr and put_attr!
206 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500207void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int cpu)
209{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200210 pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
Tejun Heof1625062009-10-29 22:34:13 +0900211 per_cpu(cpufreq_show_table, cpu) = table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
214
215void cpufreq_frequency_table_put_attr(unsigned int cpu)
216{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200217 pr_debug("clearing show_table for cpu %u\n", cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900218 per_cpu(cpufreq_show_table, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
221
Viresh Kumar27047a62013-09-16 18:56:03 +0530222int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
223 struct cpufreq_frequency_table *table)
224{
225 int ret = cpufreq_frequency_table_cpuinfo(policy, table);
226
227 if (!ret)
228 cpufreq_frequency_table_get_attr(table, policy->cpu);
229
230 return ret;
231}
232EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
233
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000234void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy)
235{
236 pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n",
237 policy->cpu, policy->last_cpu);
238 per_cpu(cpufreq_show_table, policy->cpu) = per_cpu(cpufreq_show_table,
239 policy->last_cpu);
240 per_cpu(cpufreq_show_table, policy->last_cpu) = NULL;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
244{
Tejun Heof1625062009-10-29 22:34:13 +0900245 return per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
248
Dave Jones97acec52009-01-18 01:56:41 -0500249MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
250MODULE_DESCRIPTION("CPUfreq frequency table helpers");
251MODULE_LICENSE("GPL");