xref: /openbmc/linux/drivers/cpufreq/freq_table.c (revision 77db50c4eb1991d6e88254390ec368e1d23a8fa5)
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 
12db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13db701151SViresh Kumar 
141da177e4SLinus Torvalds #include <linux/cpufreq.h>
155ff0a268SViresh Kumar #include <linux/module.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds /*********************************************************************
181da177e4SLinus Torvalds  *                     FREQUENCY TABLE HELPERS                       *
191da177e4SLinus Torvalds  *********************************************************************/
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
221da177e4SLinus Torvalds 				    struct cpufreq_frequency_table *table)
231da177e4SLinus Torvalds {
241da177e4SLinus Torvalds 	unsigned int min_freq = ~0;
251da177e4SLinus Torvalds 	unsigned int max_freq = 0;
26355eb318SDave Jones 	unsigned int i;
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds 	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
291da177e4SLinus Torvalds 		unsigned int freq = table[i].frequency;
301da177e4SLinus Torvalds 		if (freq == CPUFREQ_ENTRY_INVALID) {
312d06d8c4SDominik Brodowski 			pr_debug("table entry %u is invalid, skipping\n", i);
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 			continue;
341da177e4SLinus Torvalds 		}
3550701588SViresh Kumar 		pr_debug("table entry %u: %u kHz, %u driver_data\n",
3650701588SViresh Kumar 					i, freq, table[i].driver_data);
371da177e4SLinus Torvalds 		if (freq < min_freq)
381da177e4SLinus Torvalds 			min_freq = freq;
391da177e4SLinus Torvalds 		if (freq > max_freq)
401da177e4SLinus Torvalds 			max_freq = freq;
411da177e4SLinus Torvalds 	}
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	policy->min = policy->cpuinfo.min_freq = min_freq;
441da177e4SLinus Torvalds 	policy->max = policy->cpuinfo.max_freq = max_freq;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	if (policy->min == ~0)
471da177e4SLinus Torvalds 		return -EINVAL;
481da177e4SLinus Torvalds 	else
491da177e4SLinus Torvalds 		return 0;
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
551da177e4SLinus Torvalds 				   struct cpufreq_frequency_table *table)
561da177e4SLinus Torvalds {
57*77db50c4SViresh Kumar 	unsigned int next_larger = ~0, freq, i = 0;
58*77db50c4SViresh Kumar 	bool found = false;
591da177e4SLinus Torvalds 
602d06d8c4SDominik Brodowski 	pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
61e08f5f5bSGautham R Shenoy 					policy->min, policy->max, policy->cpu);
621da177e4SLinus Torvalds 
63e08f5f5bSGautham R Shenoy 	cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
64e08f5f5bSGautham R Shenoy 				     policy->cpuinfo.max_freq);
651da177e4SLinus Torvalds 
66*77db50c4SViresh Kumar 	for (; freq = table[i].frequency, freq != CPUFREQ_TABLE_END; i++) {
671da177e4SLinus Torvalds 		if (freq == CPUFREQ_ENTRY_INVALID)
681da177e4SLinus Torvalds 			continue;
69*77db50c4SViresh Kumar 		if ((freq >= policy->min) && (freq <= policy->max)) {
70*77db50c4SViresh Kumar 			found = true;
71*77db50c4SViresh Kumar 			break;
72*77db50c4SViresh Kumar 		}
73*77db50c4SViresh Kumar 
74*77db50c4SViresh Kumar 		if ((next_larger > freq) && (freq > policy->max))
751da177e4SLinus Torvalds 			next_larger = freq;
761da177e4SLinus Torvalds 	}
771da177e4SLinus Torvalds 
78*77db50c4SViresh Kumar 	if (!found) {
791da177e4SLinus Torvalds 		policy->max = next_larger;
80e08f5f5bSGautham R Shenoy 		cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
81e08f5f5bSGautham R Shenoy 				policy->cpuinfo.max_freq);
82*77db50c4SViresh Kumar 	}
831da177e4SLinus Torvalds 
842d06d8c4SDominik Brodowski 	pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
85e08f5f5bSGautham R Shenoy 				policy->min, policy->max, policy->cpu);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	return 0;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
931da177e4SLinus Torvalds 				   struct cpufreq_frequency_table *table,
941da177e4SLinus Torvalds 				   unsigned int target_freq,
951da177e4SLinus Torvalds 				   unsigned int relation,
961da177e4SLinus Torvalds 				   unsigned int *index)
971da177e4SLinus Torvalds {
98484944a5SDave Jones 	struct cpufreq_frequency_table optimal = {
9950701588SViresh Kumar 		.driver_data = ~0,
100484944a5SDave Jones 		.frequency = 0,
101484944a5SDave Jones 	};
102484944a5SDave Jones 	struct cpufreq_frequency_table suboptimal = {
10350701588SViresh Kumar 		.driver_data = ~0,
104484944a5SDave Jones 		.frequency = 0,
105484944a5SDave Jones 	};
1061da177e4SLinus Torvalds 	unsigned int i;
1071da177e4SLinus Torvalds 
1082d06d8c4SDominik Brodowski 	pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
109e08f5f5bSGautham R Shenoy 					target_freq, relation, policy->cpu);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	switch (relation) {
1121da177e4SLinus Torvalds 	case CPUFREQ_RELATION_H:
1131da177e4SLinus Torvalds 		suboptimal.frequency = ~0;
1141da177e4SLinus Torvalds 		break;
1151da177e4SLinus Torvalds 	case CPUFREQ_RELATION_L:
1161da177e4SLinus Torvalds 		optimal.frequency = ~0;
1171da177e4SLinus Torvalds 		break;
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
1211da177e4SLinus Torvalds 		unsigned int freq = table[i].frequency;
1221da177e4SLinus Torvalds 		if (freq == CPUFREQ_ENTRY_INVALID)
1231da177e4SLinus Torvalds 			continue;
1241da177e4SLinus Torvalds 		if ((freq < policy->min) || (freq > policy->max))
1251da177e4SLinus Torvalds 			continue;
1261da177e4SLinus Torvalds 		switch (relation) {
1271da177e4SLinus Torvalds 		case CPUFREQ_RELATION_H:
1281da177e4SLinus Torvalds 			if (freq <= target_freq) {
1291da177e4SLinus Torvalds 				if (freq >= optimal.frequency) {
1301da177e4SLinus Torvalds 					optimal.frequency = freq;
13150701588SViresh Kumar 					optimal.driver_data = i;
1321da177e4SLinus Torvalds 				}
1331da177e4SLinus Torvalds 			} else {
1341da177e4SLinus Torvalds 				if (freq <= suboptimal.frequency) {
1351da177e4SLinus Torvalds 					suboptimal.frequency = freq;
13650701588SViresh Kumar 					suboptimal.driver_data = i;
1371da177e4SLinus Torvalds 				}
1381da177e4SLinus Torvalds 			}
1391da177e4SLinus Torvalds 			break;
1401da177e4SLinus Torvalds 		case CPUFREQ_RELATION_L:
1411da177e4SLinus Torvalds 			if (freq >= target_freq) {
1421da177e4SLinus Torvalds 				if (freq <= optimal.frequency) {
1431da177e4SLinus Torvalds 					optimal.frequency = freq;
14450701588SViresh Kumar 					optimal.driver_data = i;
1451da177e4SLinus Torvalds 				}
1461da177e4SLinus Torvalds 			} else {
1471da177e4SLinus Torvalds 				if (freq >= suboptimal.frequency) {
1481da177e4SLinus Torvalds 					suboptimal.frequency = freq;
14950701588SViresh Kumar 					suboptimal.driver_data = i;
1501da177e4SLinus Torvalds 				}
1511da177e4SLinus Torvalds 			}
1521da177e4SLinus Torvalds 			break;
1531da177e4SLinus Torvalds 		}
1541da177e4SLinus Torvalds 	}
15550701588SViresh Kumar 	if (optimal.driver_data > i) {
15650701588SViresh Kumar 		if (suboptimal.driver_data > i)
1571da177e4SLinus Torvalds 			return -EINVAL;
15850701588SViresh Kumar 		*index = suboptimal.driver_data;
1591da177e4SLinus Torvalds 	} else
16050701588SViresh Kumar 		*index = optimal.driver_data;
1611da177e4SLinus Torvalds 
1622d06d8c4SDominik Brodowski 	pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
16350701588SViresh Kumar 		table[*index].driver_data);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	return 0;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
1681da177e4SLinus Torvalds 
169f1625066STejun Heo static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
1701da177e4SLinus Torvalds /**
171e32d22f7SFenghua Yu  * show_available_freqs - show available frequencies for the specified CPU
1721da177e4SLinus Torvalds  */
1731da177e4SLinus Torvalds static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	unsigned int i = 0;
1761da177e4SLinus Torvalds 	unsigned int cpu = policy->cpu;
1771da177e4SLinus Torvalds 	ssize_t count = 0;
1781da177e4SLinus Torvalds 	struct cpufreq_frequency_table *table;
1791da177e4SLinus Torvalds 
180f1625066STejun Heo 	if (!per_cpu(cpufreq_show_table, cpu))
1811da177e4SLinus Torvalds 		return -ENODEV;
1821da177e4SLinus Torvalds 
183f1625066STejun Heo 	table = per_cpu(cpufreq_show_table, cpu);
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
1861da177e4SLinus Torvalds 		if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
1871da177e4SLinus Torvalds 			continue;
1881da177e4SLinus Torvalds 		count += sprintf(&buf[count], "%d ", table[i].frequency);
1891da177e4SLinus Torvalds 	}
1901da177e4SLinus Torvalds 	count += sprintf(&buf[count], "\n");
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	return count;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
197e08f5f5bSGautham R Shenoy 	.attr = { .name = "scaling_available_frequencies",
198e08f5f5bSGautham R Shenoy 		  .mode = 0444,
199e08f5f5bSGautham R Shenoy 		},
2001da177e4SLinus Torvalds 	.show = show_available_freqs,
2011da177e4SLinus Torvalds };
2021da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds /*
2051da177e4SLinus Torvalds  * if you use these, you must assure that the frequency table is valid
2061da177e4SLinus Torvalds  * all the time between get_attr and put_attr!
2071da177e4SLinus Torvalds  */
2081da177e4SLinus Torvalds void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
2091da177e4SLinus Torvalds 				      unsigned int cpu)
2101da177e4SLinus Torvalds {
2112d06d8c4SDominik Brodowski 	pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
212f1625066STejun Heo 	per_cpu(cpufreq_show_table, cpu) = table;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds void cpufreq_frequency_table_put_attr(unsigned int cpu)
2171da177e4SLinus Torvalds {
2182d06d8c4SDominik Brodowski 	pr_debug("clearing show_table for cpu %u\n", cpu);
219f1625066STejun Heo 	per_cpu(cpufreq_show_table, cpu) = NULL;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
2221da177e4SLinus Torvalds 
22327047a60SViresh Kumar int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
22427047a60SViresh Kumar 				      struct cpufreq_frequency_table *table)
22527047a60SViresh Kumar {
22627047a60SViresh Kumar 	int ret = cpufreq_frequency_table_cpuinfo(policy, table);
22727047a60SViresh Kumar 
22827047a60SViresh Kumar 	if (!ret)
22927047a60SViresh Kumar 		cpufreq_frequency_table_get_attr(table, policy->cpu);
23027047a60SViresh Kumar 
23127047a60SViresh Kumar 	return ret;
23227047a60SViresh Kumar }
23327047a60SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
23427047a60SViresh Kumar 
235b8eed8afSViresh Kumar void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy)
236b8eed8afSViresh Kumar {
237b8eed8afSViresh Kumar 	pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n",
238b8eed8afSViresh Kumar 			policy->cpu, policy->last_cpu);
239b8eed8afSViresh Kumar 	per_cpu(cpufreq_show_table, policy->cpu) = per_cpu(cpufreq_show_table,
240b8eed8afSViresh Kumar 			policy->last_cpu);
241b8eed8afSViresh Kumar 	per_cpu(cpufreq_show_table, policy->last_cpu) = NULL;
242b8eed8afSViresh Kumar }
243b8eed8afSViresh Kumar 
2441da177e4SLinus Torvalds struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
2451da177e4SLinus Torvalds {
246f1625066STejun Heo 	return per_cpu(cpufreq_show_table, cpu);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
2511da177e4SLinus Torvalds MODULE_DESCRIPTION("CPUfreq frequency table helpers");
2521da177e4SLinus Torvalds MODULE_LICENSE("GPL");
253