freq_table.c 7.7 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 24 25

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

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

28
	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
L
Linus Torvalds 已提交
29 30
		unsigned int freq = table[i].frequency;
		if (freq == CPUFREQ_ENTRY_INVALID) {
31
			pr_debug("table entry %u is invalid, skipping\n", i);
L
Linus Torvalds 已提交
32 33 34

			continue;
		}
35
		if (!cpufreq_boost_enabled()
36
		    && (table[i].flags & CPUFREQ_BOOST_FREQ))
37 38
			continue;

39
		pr_debug("table entry %u: %u kHz\n", i, freq);
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
		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)
{
60 61
	unsigned int next_larger = ~0, freq, i = 0;
	bool found = false;
L
Linus Torvalds 已提交
62

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

66
	cpufreq_verify_within_cpu_limits(policy);
L
Linus Torvalds 已提交
67

68
	for (; freq = table[i].frequency, freq != CPUFREQ_TABLE_END; i++) {
L
Linus Torvalds 已提交
69 70
		if (freq == CPUFREQ_ENTRY_INVALID)
			continue;
71 72 73 74 75 76
		if ((freq >= policy->min) && (freq <= policy->max)) {
			found = true;
			break;
		}

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

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

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

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);

92
/*
93 94
 * Generic routine to verify policy & frequency table, requires driver to set
 * policy->freq_table prior to it.
95 96 97 98 99 100 101 102 103 104 105
 */
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 已提交
106 107 108 109 110 111 112

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

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

	switch (relation) {
	case CPUFREQ_RELATION_H:
		suboptimal.frequency = ~0;
		break;
	case CPUFREQ_RELATION_L:
		optimal.frequency = ~0;
		break;
	}

135
	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
L
Linus Torvalds 已提交
136 137 138 139 140
		unsigned int freq = table[i].frequency;
		if (freq == CPUFREQ_ENTRY_INVALID)
			continue;
		if ((freq < policy->min) || (freq > policy->max))
			continue;
141
		switch (relation) {
L
Linus Torvalds 已提交
142 143 144 145
		case CPUFREQ_RELATION_H:
			if (freq <= target_freq) {
				if (freq >= optimal.frequency) {
					optimal.frequency = freq;
146
					optimal.driver_data = i;
L
Linus Torvalds 已提交
147 148 149 150
				}
			} else {
				if (freq <= suboptimal.frequency) {
					suboptimal.frequency = freq;
151
					suboptimal.driver_data = i;
L
Linus Torvalds 已提交
152 153 154 155 156 157 158
				}
			}
			break;
		case CPUFREQ_RELATION_L:
			if (freq >= target_freq) {
				if (freq <= optimal.frequency) {
					optimal.frequency = freq;
159
					optimal.driver_data = i;
L
Linus Torvalds 已提交
160 161 162 163
				}
			} else {
				if (freq >= suboptimal.frequency) {
					suboptimal.frequency = freq;
164
					suboptimal.driver_data = i;
L
Linus Torvalds 已提交
165 166 167 168 169
				}
			}
			break;
		}
	}
170 171
	if (optimal.driver_data > i) {
		if (suboptimal.driver_data > i)
L
Linus Torvalds 已提交
172
			return -EINVAL;
173
		*index = suboptimal.driver_data;
L
Linus Torvalds 已提交
174
	} else
175
		*index = optimal.driver_data;
L
Linus Torvalds 已提交
176

177 178
	pr_debug("target index is %u, freq is:%u kHz\n", *index,
		 table[*index].frequency);
L
Linus Torvalds 已提交
179 180 181 182 183

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
		unsigned int freq)
{
	struct cpufreq_frequency_table *table;
	int i;

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

	for (i = 0; table[i].frequency != CPUFREQ_TABLE_END; i++) {
		if (table[i].frequency == freq)
			return i;
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);

L
Linus Torvalds 已提交
205
/**
206
 * show_available_freqs - show available frequencies for the specified CPU
L
Linus Torvalds 已提交
207
 */
208 209
static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
				    bool show_boost)
L
Linus Torvalds 已提交
210 211 212
{
	unsigned int i = 0;
	ssize_t count = 0;
213
	struct cpufreq_frequency_table *table = policy->freq_table;
L
Linus Torvalds 已提交
214

215
	if (!table)
L
Linus Torvalds 已提交
216 217
		return -ENODEV;

218
	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
L
Linus Torvalds 已提交
219 220
		if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
			continue;
221 222 223 224 225 226 227 228 229 230 231
		/*
		 * 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
		 */
232
		if (show_boost ^ (table[i].flags & CPUFREQ_BOOST_FREQ))
233 234
			continue;

L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242
		count += sprintf(&buf[count], "%d ", table[i].frequency);
	}
	count += sprintf(&buf[count], "\n");

	return count;

}

243 244 245 246 247 248 249 250 251 252 253 254 255 256
#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 已提交
257 258
EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);

259 260 261 262 263 264 265 266 267 268 269 270
/**
 * 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);

271 272
struct freq_attr *cpufreq_generic_attr[] = {
	&cpufreq_freq_attr_scaling_available_freqs,
273 274 275
#ifdef CONFIG_CPU_FREQ_BOOST_SW
	&cpufreq_freq_attr_scaling_boost_freqs,
#endif
276 277 278 279
	NULL,
};
EXPORT_SYMBOL_GPL(cpufreq_generic_attr);

280 281 282 283 284 285
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)
286
		policy->freq_table = table;
287 288 289 290 291

	return ret;
}
EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);

292
struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
293

L
Linus Torvalds 已提交
294 295
struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
{
296 297
	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
	return policy ? policy->freq_table : NULL;
L
Linus Torvalds 已提交
298 299 300
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);

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