freq_table.c 7.1 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 36
		pr_debug("table entry %u: %u kHz, %u driver_data\n",
					i, freq, table[i].driver_data);
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
	unsigned int next_larger = ~0, freq, i = 0;
	bool found = false;
L
Linus Torvalds 已提交
59

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

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

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

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

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

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

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);

89 90 91 92 93 94 95 96 97 98 99 100 101 102
/*
 * Generic routine to verify policy & frequency table, requires driver to call
 * cpufreq_frequency_table_get_attr() prior to it.
 */
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 已提交
103 104 105 106 107 108 109

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

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

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

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

174
	pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
175
		table[*index].driver_data);
L
Linus Torvalds 已提交
176 177 178 179 180

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);

181
static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
L
Linus Torvalds 已提交
182
/**
183
 * show_available_freqs - show available frequencies for the specified CPU
L
Linus Torvalds 已提交
184
 */
185
static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
L
Linus Torvalds 已提交
186 187 188 189 190 191
{
	unsigned int i = 0;
	unsigned int cpu = policy->cpu;
	ssize_t count = 0;
	struct cpufreq_frequency_table *table;

192
	if (!per_cpu(cpufreq_show_table, cpu))
L
Linus Torvalds 已提交
193 194
		return -ENODEV;

195
	table = per_cpu(cpufreq_show_table, cpu);
L
Linus Torvalds 已提交
196

197
	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205 206 207 208
		if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
			continue;
		count += sprintf(&buf[count], "%d ", table[i].frequency);
	}
	count += sprintf(&buf[count], "\n");

	return count;

}

struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
209 210 211
	.attr = { .name = "scaling_available_frequencies",
		  .mode = 0444,
		},
L
Linus Torvalds 已提交
212 213 214 215
	.show = show_available_freqs,
};
EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);

216 217 218 219 220 221
struct freq_attr *cpufreq_generic_attr[] = {
	&cpufreq_freq_attr_scaling_available_freqs,
	NULL,
};
EXPORT_SYMBOL_GPL(cpufreq_generic_attr);

L
Linus Torvalds 已提交
222 223 224 225
/*
 * if you use these, you must assure that the frequency table is valid
 * all the time between get_attr and put_attr!
 */
226
void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
L
Linus Torvalds 已提交
227 228
				      unsigned int cpu)
{
229
	pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
230
	per_cpu(cpufreq_show_table, cpu) = table;
L
Linus Torvalds 已提交
231 232 233 234 235
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);

void cpufreq_frequency_table_put_attr(unsigned int cpu)
{
236
	pr_debug("clearing show_table for cpu %u\n", cpu);
237
	per_cpu(cpufreq_show_table, cpu) = NULL;
L
Linus Torvalds 已提交
238 239 240
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);

241 242 243 244 245 246 247 248 249 250 251 252
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)
		cpufreq_frequency_table_get_attr(table, policy->cpu);

	return ret;
}
EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);

253 254 255 256 257 258 259 260 261
void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy)
{
	pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n",
			policy->cpu, policy->last_cpu);
	per_cpu(cpufreq_show_table, policy->cpu) = per_cpu(cpufreq_show_table,
			policy->last_cpu);
	per_cpu(cpufreq_show_table, policy->last_cpu) = NULL;
}

L
Linus Torvalds 已提交
262 263
struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
{
264
	return per_cpu(cpufreq_show_table, cpu);
L
Linus Torvalds 已提交
265 266 267
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);

268 269 270
MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
MODULE_DESCRIPTION("CPUfreq frequency table helpers");
MODULE_LICENSE("GPL");