xref: /openbmc/linux/drivers/cpufreq/freq_table.c (revision db7011516cbfc3d867b77721f77258d36cfbf705)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/drivers/cpufreq/freq_table.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2002 - 2003 Dominik Brodowski
54f743694SDominik Brodowski  *
64f743694SDominik Brodowski  * This program is free software; you can redistribute it and/or modify
74f743694SDominik Brodowski  * it under the terms of the GNU General Public License version 2 as
84f743694SDominik Brodowski  * published by the Free Software Foundation.
94f743694SDominik Brodowski  *
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
12*db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13*db701151SViresh Kumar 
141da177e4SLinus Torvalds #include <linux/kernel.h>
151da177e4SLinus Torvalds #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/cpufreq.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds /*********************************************************************
201da177e4SLinus Torvalds  *                     FREQUENCY TABLE HELPERS                       *
211da177e4SLinus Torvalds  *********************************************************************/
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
241da177e4SLinus Torvalds 				    struct cpufreq_frequency_table *table)
251da177e4SLinus Torvalds {
261da177e4SLinus Torvalds 	unsigned int min_freq = ~0;
271da177e4SLinus Torvalds 	unsigned int max_freq = 0;
28355eb318SDave Jones 	unsigned int i;
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
311da177e4SLinus Torvalds 		unsigned int freq = table[i].frequency;
321da177e4SLinus Torvalds 		if (freq == CPUFREQ_ENTRY_INVALID) {
332d06d8c4SDominik Brodowski 			pr_debug("table entry %u is invalid, skipping\n", i);
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 			continue;
361da177e4SLinus Torvalds 		}
372d06d8c4SDominik Brodowski 		pr_debug("table entry %u: %u kHz, %u index\n",
38e08f5f5bSGautham R Shenoy 					i, freq, table[i].index);
391da177e4SLinus Torvalds 		if (freq < min_freq)
401da177e4SLinus Torvalds 			min_freq = freq;
411da177e4SLinus Torvalds 		if (freq > max_freq)
421da177e4SLinus Torvalds 			max_freq = freq;
431da177e4SLinus Torvalds 	}
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	policy->min = policy->cpuinfo.min_freq = min_freq;
461da177e4SLinus Torvalds 	policy->max = policy->cpuinfo.max_freq = max_freq;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	if (policy->min == ~0)
491da177e4SLinus Torvalds 		return -EINVAL;
501da177e4SLinus Torvalds 	else
511da177e4SLinus Torvalds 		return 0;
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
571da177e4SLinus Torvalds 				   struct cpufreq_frequency_table *table)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	unsigned int next_larger = ~0;
605557976cSDave Jones 	unsigned int i;
611da177e4SLinus Torvalds 	unsigned int count = 0;
621da177e4SLinus Torvalds 
632d06d8c4SDominik Brodowski 	pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
64e08f5f5bSGautham R Shenoy 					policy->min, policy->max, policy->cpu);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	if (!cpu_online(policy->cpu))
671da177e4SLinus Torvalds 		return -EINVAL;
681da177e4SLinus Torvalds 
69e08f5f5bSGautham R Shenoy 	cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
70e08f5f5bSGautham R Shenoy 				     policy->cpuinfo.max_freq);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
731da177e4SLinus Torvalds 		unsigned int freq = table[i].frequency;
741da177e4SLinus Torvalds 		if (freq == CPUFREQ_ENTRY_INVALID)
751da177e4SLinus Torvalds 			continue;
761da177e4SLinus Torvalds 		if ((freq >= policy->min) && (freq <= policy->max))
771da177e4SLinus Torvalds 			count++;
781da177e4SLinus Torvalds 		else if ((next_larger > freq) && (freq > policy->max))
791da177e4SLinus Torvalds 			next_larger = freq;
801da177e4SLinus Torvalds 	}
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	if (!count)
831da177e4SLinus Torvalds 		policy->max = next_larger;
841da177e4SLinus Torvalds 
85e08f5f5bSGautham R Shenoy 	cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
86e08f5f5bSGautham R Shenoy 				     policy->cpuinfo.max_freq);
871da177e4SLinus Torvalds 
882d06d8c4SDominik Brodowski 	pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
89e08f5f5bSGautham R Shenoy 				policy->min, policy->max, policy->cpu);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	return 0;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
971da177e4SLinus Torvalds 				   struct cpufreq_frequency_table *table,
981da177e4SLinus Torvalds 				   unsigned int target_freq,
991da177e4SLinus Torvalds 				   unsigned int relation,
1001da177e4SLinus Torvalds 				   unsigned int *index)
1011da177e4SLinus Torvalds {
102484944a5SDave Jones 	struct cpufreq_frequency_table optimal = {
103484944a5SDave Jones 		.index = ~0,
104484944a5SDave Jones 		.frequency = 0,
105484944a5SDave Jones 	};
106484944a5SDave Jones 	struct cpufreq_frequency_table suboptimal = {
107484944a5SDave Jones 		.index = ~0,
108484944a5SDave Jones 		.frequency = 0,
109484944a5SDave Jones 	};
1101da177e4SLinus Torvalds 	unsigned int i;
1111da177e4SLinus Torvalds 
1122d06d8c4SDominik Brodowski 	pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
113e08f5f5bSGautham R Shenoy 					target_freq, relation, policy->cpu);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	switch (relation) {
1161da177e4SLinus Torvalds 	case CPUFREQ_RELATION_H:
1171da177e4SLinus Torvalds 		suboptimal.frequency = ~0;
1181da177e4SLinus Torvalds 		break;
1191da177e4SLinus Torvalds 	case CPUFREQ_RELATION_L:
1201da177e4SLinus Torvalds 		optimal.frequency = ~0;
1211da177e4SLinus Torvalds 		break;
1221da177e4SLinus Torvalds 	}
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	if (!cpu_online(policy->cpu))
1251da177e4SLinus Torvalds 		return -EINVAL;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
1281da177e4SLinus Torvalds 		unsigned int freq = table[i].frequency;
1291da177e4SLinus Torvalds 		if (freq == CPUFREQ_ENTRY_INVALID)
1301da177e4SLinus Torvalds 			continue;
1311da177e4SLinus Torvalds 		if ((freq < policy->min) || (freq > policy->max))
1321da177e4SLinus Torvalds 			continue;
1331da177e4SLinus Torvalds 		switch (relation) {
1341da177e4SLinus Torvalds 		case CPUFREQ_RELATION_H:
1351da177e4SLinus Torvalds 			if (freq <= target_freq) {
1361da177e4SLinus Torvalds 				if (freq >= optimal.frequency) {
1371da177e4SLinus Torvalds 					optimal.frequency = freq;
1381da177e4SLinus Torvalds 					optimal.index = i;
1391da177e4SLinus Torvalds 				}
1401da177e4SLinus Torvalds 			} else {
1411da177e4SLinus Torvalds 				if (freq <= suboptimal.frequency) {
1421da177e4SLinus Torvalds 					suboptimal.frequency = freq;
1431da177e4SLinus Torvalds 					suboptimal.index = i;
1441da177e4SLinus Torvalds 				}
1451da177e4SLinus Torvalds 			}
1461da177e4SLinus Torvalds 			break;
1471da177e4SLinus Torvalds 		case CPUFREQ_RELATION_L:
1481da177e4SLinus Torvalds 			if (freq >= target_freq) {
1491da177e4SLinus Torvalds 				if (freq <= optimal.frequency) {
1501da177e4SLinus Torvalds 					optimal.frequency = freq;
1511da177e4SLinus Torvalds 					optimal.index = i;
1521da177e4SLinus Torvalds 				}
1531da177e4SLinus Torvalds 			} else {
1541da177e4SLinus Torvalds 				if (freq >= suboptimal.frequency) {
1551da177e4SLinus Torvalds 					suboptimal.frequency = freq;
1561da177e4SLinus Torvalds 					suboptimal.index = i;
1571da177e4SLinus Torvalds 				}
1581da177e4SLinus Torvalds 			}
1591da177e4SLinus Torvalds 			break;
1601da177e4SLinus Torvalds 		}
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds 	if (optimal.index > i) {
1631da177e4SLinus Torvalds 		if (suboptimal.index > i)
1641da177e4SLinus Torvalds 			return -EINVAL;
1651da177e4SLinus Torvalds 		*index = suboptimal.index;
1661da177e4SLinus Torvalds 	} else
1671da177e4SLinus Torvalds 		*index = optimal.index;
1681da177e4SLinus Torvalds 
1692d06d8c4SDominik Brodowski 	pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
1701da177e4SLinus Torvalds 		table[*index].index);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	return 0;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
1751da177e4SLinus Torvalds 
176f1625066STejun Heo static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
1771da177e4SLinus Torvalds /**
178e32d22f7SFenghua Yu  * show_available_freqs - show available frequencies for the specified CPU
1791da177e4SLinus Torvalds  */
1801da177e4SLinus Torvalds static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	unsigned int i = 0;
1831da177e4SLinus Torvalds 	unsigned int cpu = policy->cpu;
1841da177e4SLinus Torvalds 	ssize_t count = 0;
1851da177e4SLinus Torvalds 	struct cpufreq_frequency_table *table;
1861da177e4SLinus Torvalds 
187f1625066STejun Heo 	if (!per_cpu(cpufreq_show_table, cpu))
1881da177e4SLinus Torvalds 		return -ENODEV;
1891da177e4SLinus Torvalds 
190f1625066STejun Heo 	table = per_cpu(cpufreq_show_table, cpu);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
1931da177e4SLinus Torvalds 		if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
1941da177e4SLinus Torvalds 			continue;
1951da177e4SLinus Torvalds 		count += sprintf(&buf[count], "%d ", table[i].frequency);
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds 	count += sprintf(&buf[count], "\n");
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	return count;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
204e08f5f5bSGautham R Shenoy 	.attr = { .name = "scaling_available_frequencies",
205e08f5f5bSGautham R Shenoy 		  .mode = 0444,
206e08f5f5bSGautham R Shenoy 		},
2071da177e4SLinus Torvalds 	.show = show_available_freqs,
2081da177e4SLinus Torvalds };
2091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /*
2121da177e4SLinus Torvalds  * if you use these, you must assure that the frequency table is valid
2131da177e4SLinus Torvalds  * all the time between get_attr and put_attr!
2141da177e4SLinus Torvalds  */
2151da177e4SLinus Torvalds void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
2161da177e4SLinus Torvalds 				      unsigned int cpu)
2171da177e4SLinus Torvalds {
2182d06d8c4SDominik Brodowski 	pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
219f1625066STejun Heo 	per_cpu(cpufreq_show_table, cpu) = table;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds void cpufreq_frequency_table_put_attr(unsigned int cpu)
2241da177e4SLinus Torvalds {
2252d06d8c4SDominik Brodowski 	pr_debug("clearing show_table for cpu %u\n", cpu);
226f1625066STejun Heo 	per_cpu(cpufreq_show_table, cpu) = NULL;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
2311da177e4SLinus Torvalds {
232f1625066STejun Heo 	return per_cpu(cpufreq_show_table, cpu);
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
2371da177e4SLinus Torvalds MODULE_DESCRIPTION("CPUfreq frequency table helpers");
2381da177e4SLinus Torvalds MODULE_LICENSE("GPL");
239