blob: f0d87412cc91742a73fa40a500b160387af9fb28 [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 }
Viresh Kumar50701582013-03-30 16:25:15 +053037 pr_debug("table entry %u: %u kHz, %u driver_data\n",
38 i, freq, table[i].driver_data);
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
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053066 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
67 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Dave Jones97acec52009-01-18 01:56:41 -050069 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned int freq = table[i].frequency;
71 if (freq == CPUFREQ_ENTRY_INVALID)
72 continue;
73 if ((freq >= policy->min) && (freq <= policy->max))
74 count++;
75 else if ((next_larger > freq) && (freq > policy->max))
76 next_larger = freq;
77 }
78
79 if (!count)
80 policy->max = next_larger;
81
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053082 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
83 policy->cpuinfo.max_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020085 pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053086 policy->min, policy->max, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 return 0;
89}
90EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
91
92
93int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
94 struct cpufreq_frequency_table *table,
95 unsigned int target_freq,
96 unsigned int relation,
97 unsigned int *index)
98{
Dave Jones484944a2006-05-30 18:09:31 -040099 struct cpufreq_frequency_table optimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530100 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400101 .frequency = 0,
102 };
103 struct cpufreq_frequency_table suboptimal = {
Viresh Kumar50701582013-03-30 16:25:15 +0530104 .driver_data = ~0,
Dave Jones484944a2006-05-30 18:09:31 -0400105 .frequency = 0,
106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned int i;
108
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200109 pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530110 target_freq, relation, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 switch (relation) {
113 case CPUFREQ_RELATION_H:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 suboptimal.frequency = ~0;
115 break;
116 case CPUFREQ_RELATION_L:
117 optimal.frequency = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 break;
119 }
120
Dave Jones97acec52009-01-18 01:56:41 -0500121 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned int freq = table[i].frequency;
123 if (freq == CPUFREQ_ENTRY_INVALID)
124 continue;
125 if ((freq < policy->min) || (freq > policy->max))
126 continue;
Dave Jones97acec52009-01-18 01:56:41 -0500127 switch (relation) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 case CPUFREQ_RELATION_H:
129 if (freq <= target_freq) {
130 if (freq >= optimal.frequency) {
131 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530132 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 } else {
135 if (freq <= suboptimal.frequency) {
136 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530137 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 }
140 break;
141 case CPUFREQ_RELATION_L:
142 if (freq >= target_freq) {
143 if (freq <= optimal.frequency) {
144 optimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530145 optimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147 } else {
148 if (freq >= suboptimal.frequency) {
149 suboptimal.frequency = freq;
Viresh Kumar50701582013-03-30 16:25:15 +0530150 suboptimal.driver_data = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152 }
153 break;
154 }
155 }
Viresh Kumar50701582013-03-30 16:25:15 +0530156 if (optimal.driver_data > i) {
157 if (suboptimal.driver_data > i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -EINVAL;
Viresh Kumar50701582013-03-30 16:25:15 +0530159 *index = suboptimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 } else
Viresh Kumar50701582013-03-30 16:25:15 +0530161 *index = optimal.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200163 pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
Viresh Kumar50701582013-03-30 16:25:15 +0530164 table[*index].driver_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return 0;
167}
168EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
169
Tejun Heof1625062009-10-29 22:34:13 +0900170static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/**
Fenghua Yue32d22f2007-11-21 14:52:15 -0800172 * show_available_freqs - show available frequencies for the specified CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Dave Jones97acec52009-01-18 01:56:41 -0500174static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 unsigned int i = 0;
177 unsigned int cpu = policy->cpu;
178 ssize_t count = 0;
179 struct cpufreq_frequency_table *table;
180
Tejun Heof1625062009-10-29 22:34:13 +0900181 if (!per_cpu(cpufreq_show_table, cpu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return -ENODEV;
183
Tejun Heof1625062009-10-29 22:34:13 +0900184 table = per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dave Jones97acec52009-01-18 01:56:41 -0500186 for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
188 continue;
189 count += sprintf(&buf[count], "%d ", table[i].frequency);
190 }
191 count += sprintf(&buf[count], "\n");
192
193 return count;
194
195}
196
197struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530198 .attr = { .name = "scaling_available_frequencies",
199 .mode = 0444,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530200 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .show = show_available_freqs,
202};
203EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
204
205/*
206 * if you use these, you must assure that the frequency table is valid
207 * all the time between get_attr and put_attr!
208 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500209void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned int cpu)
211{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200212 pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
Tejun Heof1625062009-10-29 22:34:13 +0900213 per_cpu(cpufreq_show_table, cpu) = table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
216
217void cpufreq_frequency_table_put_attr(unsigned int cpu)
218{
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200219 pr_debug("clearing show_table for cpu %u\n", cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900220 per_cpu(cpufreq_show_table, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
223
Viresh Kumarb8eed8a2013-01-14 13:23:03 +0000224void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy)
225{
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,
229 policy->last_cpu);
230 per_cpu(cpufreq_show_table, policy->last_cpu) = NULL;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
234{
Tejun Heof1625062009-10-29 22:34:13 +0900235 return per_cpu(cpufreq_show_table, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
238
Dave Jones97acec52009-01-18 01:56:41 -0500239MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
240MODULE_DESCRIPTION("CPUfreq frequency table helpers");
241MODULE_LICENSE("GPL");