freq_table.c 7.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * linux/drivers/cpufreq/freq_table.c
 *
 * Copyright (C) 2002 - 2003 Dominik Brodowski
5 6 7 8 9
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
L
Linus Torvalds 已提交
10 11
 */

V
Viresh Kumar 已提交
12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
14
#include <linux/cpufreq.h>
15
#include <linux/module.h>
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23

/*********************************************************************
 *                     FREQUENCY TABLE HELPERS                       *
 *********************************************************************/

int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
				    struct cpufreq_frequency_table *table)
{
24
	struct cpufreq_frequency_table *pos;
L
Linus Torvalds 已提交
25 26
	unsigned int min_freq = ~0;
	unsigned int max_freq = 0;
27
	unsigned int freq;
L
Linus Torvalds 已提交
28

29 30
	cpufreq_for_each_valid_entry(pos, table) {
		freq = pos->frequency;
L
Linus Torvalds 已提交
31

32
		if (!cpufreq_boost_enabled()
33
		    && (pos->flags & CPUFREQ_BOOST_FREQ))
34 35
			continue;

36
		pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		if (freq < min_freq)
			min_freq = freq;
		if (freq > max_freq)
			max_freq = freq;
	}

	policy->min = policy->cpuinfo.min_freq = min_freq;
	policy->max = policy->cpuinfo.max_freq = max_freq;

	if (policy->min == ~0)
		return -EINVAL;
	else
		return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);


int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
				   struct cpufreq_frequency_table *table)
{
57 58
	struct cpufreq_frequency_table *pos;
	unsigned int freq, next_larger = ~0;
59
	bool found = false;
L
Linus Torvalds 已提交
60

61
	pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
62
					policy->min, policy->max, policy->cpu);
L
Linus Torvalds 已提交
63

64
	cpufreq_verify_within_cpu_limits(policy);
L
Linus Torvalds 已提交
65

66 67 68
	cpufreq_for_each_valid_entry(pos, table) {
		freq = pos->frequency;

69 70 71 72 73 74
		if ((freq >= policy->min) && (freq <= policy->max)) {
			found = true;
			break;
		}

		if ((next_larger > freq) && (freq > policy->max))
L
Linus Torvalds 已提交
75 76 77
			next_larger = freq;
	}

78
	if (!found) {
L
Linus Torvalds 已提交
79
		policy->max = next_larger;
80
		cpufreq_verify_within_cpu_limits(policy);
81
	}
L
Linus Torvalds 已提交
82

83
	pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
84
				policy->min, policy->max, policy->cpu);
L
Linus Torvalds 已提交
85 86 87 88 89

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);

90
/*
91 92
 * Generic routine to verify policy & frequency table, requires driver to set
 * policy->freq_table prior to it.
93 94 95 96 97 98 99 100 101 102 103
 */
int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
{
	struct cpufreq_frequency_table *table =
		cpufreq_frequency_get_table(policy->cpu);
	if (!table)
		return -ENODEV;

	return cpufreq_frequency_table_verify(policy, table);
}
EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
L
Linus Torvalds 已提交
104 105 106 107 108 109 110

int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
				   struct cpufreq_frequency_table *table,
				   unsigned int target_freq,
				   unsigned int relation,
				   unsigned int *index)
{
111
	struct cpufreq_frequency_table optimal = {
112
		.driver_data = ~0,
113 114 115
		.frequency = 0,
	};
	struct cpufreq_frequency_table suboptimal = {
116
		.driver_data = ~0,
117 118
		.frequency = 0,
	};
119
	struct cpufreq_frequency_table *pos;
120
	unsigned int freq, diff, i = 0;
L
Linus Torvalds 已提交
121

122
	pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
123
					target_freq, relation, policy->cpu);
L
Linus Torvalds 已提交
124 125 126 127 128 129

	switch (relation) {
	case CPUFREQ_RELATION_H:
		suboptimal.frequency = ~0;
		break;
	case CPUFREQ_RELATION_L:
130
	case CPUFREQ_RELATION_C:
L
Linus Torvalds 已提交
131 132 133 134
		optimal.frequency = ~0;
		break;
	}

135 136 137 138
	cpufreq_for_each_valid_entry(pos, table) {
		freq = pos->frequency;

		i = pos - table;
L
Linus Torvalds 已提交
139 140
		if ((freq < policy->min) || (freq > policy->max))
			continue;
141 142 143 144
		if (freq == target_freq) {
			optimal.driver_data = i;
			break;
		}
145
		switch (relation) {
L
Linus Torvalds 已提交
146
		case CPUFREQ_RELATION_H:
147
			if (freq < target_freq) {
L
Linus Torvalds 已提交
148 149
				if (freq >= optimal.frequency) {
					optimal.frequency = freq;
150
					optimal.driver_data = i;
L
Linus Torvalds 已提交
151 152 153 154
				}
			} else {
				if (freq <= suboptimal.frequency) {
					suboptimal.frequency = freq;
155
					suboptimal.driver_data = i;
L
Linus Torvalds 已提交
156 157 158 159
				}
			}
			break;
		case CPUFREQ_RELATION_L:
160
			if (freq > target_freq) {
L
Linus Torvalds 已提交
161 162
				if (freq <= optimal.frequency) {
					optimal.frequency = freq;
163
					optimal.driver_data = i;
L
Linus Torvalds 已提交
164 165 166 167
				}
			} else {
				if (freq >= suboptimal.frequency) {
					suboptimal.frequency = freq;
168
					suboptimal.driver_data = i;
L
Linus Torvalds 已提交
169 170 171
				}
			}
			break;
172 173 174 175 176 177 178 179 180
		case CPUFREQ_RELATION_C:
			diff = abs(freq - target_freq);
			if (diff < optimal.frequency ||
			    (diff == optimal.frequency &&
			     freq > table[optimal.driver_data].frequency)) {
				optimal.frequency = diff;
				optimal.driver_data = i;
			}
			break;
L
Linus Torvalds 已提交
181 182
		}
	}
183 184
	if (optimal.driver_data > i) {
		if (suboptimal.driver_data > i)
L
Linus Torvalds 已提交
185
			return -EINVAL;
186
		*index = suboptimal.driver_data;
L
Linus Torvalds 已提交
187
	} else
188
		*index = optimal.driver_data;
L
Linus Torvalds 已提交
189

190 191
	pr_debug("target index is %u, freq is:%u kHz\n", *index,
		 table[*index].frequency);
L
Linus Torvalds 已提交
192 193 194 195 196

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);

197 198 199
int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
		unsigned int freq)
{
200
	struct cpufreq_frequency_table *pos, *table;
201 202 203 204 205 206 207

	table = cpufreq_frequency_get_table(policy->cpu);
	if (unlikely(!table)) {
		pr_debug("%s: Unable to find frequency table\n", __func__);
		return -ENOENT;
	}

208 209 210
	cpufreq_for_each_valid_entry(pos, table)
		if (pos->frequency == freq)
			return pos - table;
211 212 213 214 215

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);

L
Linus Torvalds 已提交
216
/**
217
 * show_available_freqs - show available frequencies for the specified CPU
L
Linus Torvalds 已提交
218
 */
219 220
static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
				    bool show_boost)
L
Linus Torvalds 已提交
221 222
{
	ssize_t count = 0;
223
	struct cpufreq_frequency_table *pos, *table = policy->freq_table;
L
Linus Torvalds 已提交
224

225
	if (!table)
L
Linus Torvalds 已提交
226 227
		return -ENODEV;

228
	cpufreq_for_each_valid_entry(pos, table) {
229 230 231 232 233 234 235 236 237 238 239
		/*
		 * show_boost = true and driver_data = BOOST freq
		 * display BOOST freqs
		 *
		 * show_boost = false and driver_data = BOOST freq
		 * show_boost = true and driver_data != BOOST freq
		 * continue - do not display anything
		 *
		 * show_boost = false and driver_data != BOOST freq
		 * display NON BOOST freqs
		 */
240
		if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
241 242
			continue;

243
		count += sprintf(&buf[count], "%d ", pos->frequency);
L
Linus Torvalds 已提交
244 245 246 247 248 249 250
	}
	count += sprintf(&buf[count], "\n");

	return count;

}

251 252 253 254 255 256 257 258 259 260 261 262 263 264
#define cpufreq_attr_available_freq(_name)	  \
struct freq_attr cpufreq_freq_attr_##_name##_freqs =     \
__ATTR_RO(_name##_frequencies)

/**
 * show_scaling_available_frequencies - show available normal frequencies for
 * the specified CPU
 */
static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
						  char *buf)
{
	return show_available_freqs(policy, buf, false);
}
cpufreq_attr_available_freq(scaling_available);
L
Linus Torvalds 已提交
265 266
EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);

267 268 269 270 271 272 273 274 275 276 277 278
/**
 * show_available_boost_freqs - show available boost frequencies for
 * the specified CPU
 */
static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
					      char *buf)
{
	return show_available_freqs(policy, buf, true);
}
cpufreq_attr_available_freq(scaling_boost);
EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);

279 280
struct freq_attr *cpufreq_generic_attr[] = {
	&cpufreq_freq_attr_scaling_available_freqs,
281 282 283
#ifdef CONFIG_CPU_FREQ_BOOST_SW
	&cpufreq_freq_attr_scaling_boost_freqs,
#endif
284 285 286 287
	NULL,
};
EXPORT_SYMBOL_GPL(cpufreq_generic_attr);

288 289 290 291 292 293
int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
				      struct cpufreq_frequency_table *table)
{
	int ret = cpufreq_frequency_table_cpuinfo(policy, table);

	if (!ret)
294
		policy->freq_table = table;
295 296 297 298 299

	return ret;
}
EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);

300 301 302
MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
MODULE_DESCRIPTION("CPUfreq frequency table helpers");
MODULE_LICENSE("GPL");