xref: /openbmc/linux/drivers/cpufreq/freq_table.c (revision 7ab4aabbaa98fbf9f8967fd93162d675439337e1)
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 
2144139ed4SViresh Kumar bool policy_has_boost_freq(struct cpufreq_policy *policy)
2244139ed4SViresh Kumar {
2344139ed4SViresh Kumar 	struct cpufreq_frequency_table *pos, *table = policy->freq_table;
2444139ed4SViresh Kumar 
2544139ed4SViresh Kumar 	if (!table)
2644139ed4SViresh Kumar 		return false;
2744139ed4SViresh Kumar 
2844139ed4SViresh Kumar 	cpufreq_for_each_valid_entry(pos, table)
2944139ed4SViresh Kumar 		if (pos->flags & CPUFREQ_BOOST_FREQ)
3044139ed4SViresh Kumar 			return true;
3144139ed4SViresh Kumar 
3244139ed4SViresh Kumar 	return false;
3344139ed4SViresh Kumar }
3444139ed4SViresh Kumar EXPORT_SYMBOL_GPL(policy_has_boost_freq);
3544139ed4SViresh Kumar 
361da177e4SLinus Torvalds int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
371da177e4SLinus Torvalds 				    struct cpufreq_frequency_table *table)
381da177e4SLinus Torvalds {
39041526f9SStratos Karafotis 	struct cpufreq_frequency_table *pos;
401da177e4SLinus Torvalds 	unsigned int min_freq = ~0;
411da177e4SLinus Torvalds 	unsigned int max_freq = 0;
42041526f9SStratos Karafotis 	unsigned int freq;
431da177e4SLinus Torvalds 
44041526f9SStratos Karafotis 	cpufreq_for_each_valid_entry(pos, table) {
45041526f9SStratos Karafotis 		freq = pos->frequency;
461da177e4SLinus Torvalds 
476f19efc0SLukasz Majewski 		if (!cpufreq_boost_enabled()
48041526f9SStratos Karafotis 		    && (pos->flags & CPUFREQ_BOOST_FREQ))
496f19efc0SLukasz Majewski 			continue;
506f19efc0SLukasz Majewski 
51041526f9SStratos Karafotis 		pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
521da177e4SLinus Torvalds 		if (freq < min_freq)
531da177e4SLinus Torvalds 			min_freq = freq;
541da177e4SLinus Torvalds 		if (freq > max_freq)
551da177e4SLinus Torvalds 			max_freq = freq;
561da177e4SLinus Torvalds 	}
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	policy->min = policy->cpuinfo.min_freq = min_freq;
591da177e4SLinus Torvalds 	policy->max = policy->cpuinfo.max_freq = max_freq;
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 	if (policy->min == ~0)
621da177e4SLinus Torvalds 		return -EINVAL;
631da177e4SLinus Torvalds 	else
641da177e4SLinus Torvalds 		return 0;
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
681da177e4SLinus Torvalds 				   struct cpufreq_frequency_table *table)
691da177e4SLinus Torvalds {
70041526f9SStratos Karafotis 	struct cpufreq_frequency_table *pos;
71041526f9SStratos Karafotis 	unsigned int freq, next_larger = ~0;
7277db50c4SViresh Kumar 	bool found = false;
731da177e4SLinus Torvalds 
742d06d8c4SDominik Brodowski 	pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
75e08f5f5bSGautham R Shenoy 					policy->min, policy->max, policy->cpu);
761da177e4SLinus Torvalds 
77be49e346SViresh Kumar 	cpufreq_verify_within_cpu_limits(policy);
781da177e4SLinus Torvalds 
79041526f9SStratos Karafotis 	cpufreq_for_each_valid_entry(pos, table) {
80041526f9SStratos Karafotis 		freq = pos->frequency;
81041526f9SStratos Karafotis 
8277db50c4SViresh Kumar 		if ((freq >= policy->min) && (freq <= policy->max)) {
8377db50c4SViresh Kumar 			found = true;
8477db50c4SViresh Kumar 			break;
8577db50c4SViresh Kumar 		}
8677db50c4SViresh Kumar 
8777db50c4SViresh Kumar 		if ((next_larger > freq) && (freq > policy->max))
881da177e4SLinus Torvalds 			next_larger = freq;
891da177e4SLinus Torvalds 	}
901da177e4SLinus Torvalds 
9177db50c4SViresh Kumar 	if (!found) {
921da177e4SLinus Torvalds 		policy->max = next_larger;
93be49e346SViresh Kumar 		cpufreq_verify_within_cpu_limits(policy);
9477db50c4SViresh Kumar 	}
951da177e4SLinus Torvalds 
962d06d8c4SDominik Brodowski 	pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
97e08f5f5bSGautham R Shenoy 				policy->min, policy->max, policy->cpu);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	return 0;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
1021da177e4SLinus Torvalds 
10318434512SViresh Kumar /*
104e0b3165bSViresh Kumar  * Generic routine to verify policy & frequency table, requires driver to set
105e0b3165bSViresh Kumar  * policy->freq_table prior to it.
10618434512SViresh Kumar  */
10718434512SViresh Kumar int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
10818434512SViresh Kumar {
109f8bfc116SViresh Kumar 	if (!policy->freq_table)
11018434512SViresh Kumar 		return -ENODEV;
11118434512SViresh Kumar 
112f8bfc116SViresh Kumar 	return cpufreq_frequency_table_verify(policy, policy->freq_table);
11318434512SViresh Kumar }
11418434512SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
1171da177e4SLinus Torvalds 				   unsigned int target_freq,
1181da177e4SLinus Torvalds 				   unsigned int relation,
1191da177e4SLinus Torvalds 				   unsigned int *index)
1201da177e4SLinus Torvalds {
121484944a5SDave Jones 	struct cpufreq_frequency_table optimal = {
12250701588SViresh Kumar 		.driver_data = ~0,
123484944a5SDave Jones 		.frequency = 0,
124484944a5SDave Jones 	};
125484944a5SDave Jones 	struct cpufreq_frequency_table suboptimal = {
12650701588SViresh Kumar 		.driver_data = ~0,
127484944a5SDave Jones 		.frequency = 0,
128484944a5SDave Jones 	};
129041526f9SStratos Karafotis 	struct cpufreq_frequency_table *pos;
130*7ab4aabbSViresh Kumar 	struct cpufreq_frequency_table *table = policy->freq_table;
1315b0c0b16SStratos Karafotis 	unsigned int freq, diff, i = 0;
1321da177e4SLinus Torvalds 
1332d06d8c4SDominik Brodowski 	pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
134e08f5f5bSGautham R Shenoy 					target_freq, relation, policy->cpu);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	switch (relation) {
1371da177e4SLinus Torvalds 	case CPUFREQ_RELATION_H:
1381da177e4SLinus Torvalds 		suboptimal.frequency = ~0;
1391da177e4SLinus Torvalds 		break;
1401da177e4SLinus Torvalds 	case CPUFREQ_RELATION_L:
1415b0c0b16SStratos Karafotis 	case CPUFREQ_RELATION_C:
1421da177e4SLinus Torvalds 		optimal.frequency = ~0;
1431da177e4SLinus Torvalds 		break;
1441da177e4SLinus Torvalds 	}
1451da177e4SLinus Torvalds 
146041526f9SStratos Karafotis 	cpufreq_for_each_valid_entry(pos, table) {
147041526f9SStratos Karafotis 		freq = pos->frequency;
148041526f9SStratos Karafotis 
149041526f9SStratos Karafotis 		i = pos - table;
1501da177e4SLinus Torvalds 		if ((freq < policy->min) || (freq > policy->max))
1511da177e4SLinus Torvalds 			continue;
1521e498856SStratos Karafotis 		if (freq == target_freq) {
1531e498856SStratos Karafotis 			optimal.driver_data = i;
1541e498856SStratos Karafotis 			break;
1551e498856SStratos Karafotis 		}
1561da177e4SLinus Torvalds 		switch (relation) {
1571da177e4SLinus Torvalds 		case CPUFREQ_RELATION_H:
1581e498856SStratos Karafotis 			if (freq < target_freq) {
1591da177e4SLinus Torvalds 				if (freq >= optimal.frequency) {
1601da177e4SLinus Torvalds 					optimal.frequency = freq;
16150701588SViresh Kumar 					optimal.driver_data = i;
1621da177e4SLinus Torvalds 				}
1631da177e4SLinus Torvalds 			} else {
1641da177e4SLinus Torvalds 				if (freq <= suboptimal.frequency) {
1651da177e4SLinus Torvalds 					suboptimal.frequency = freq;
16650701588SViresh Kumar 					suboptimal.driver_data = i;
1671da177e4SLinus Torvalds 				}
1681da177e4SLinus Torvalds 			}
1691da177e4SLinus Torvalds 			break;
1701da177e4SLinus Torvalds 		case CPUFREQ_RELATION_L:
1711e498856SStratos Karafotis 			if (freq > target_freq) {
1721da177e4SLinus Torvalds 				if (freq <= optimal.frequency) {
1731da177e4SLinus Torvalds 					optimal.frequency = freq;
17450701588SViresh Kumar 					optimal.driver_data = i;
1751da177e4SLinus Torvalds 				}
1761da177e4SLinus Torvalds 			} else {
1771da177e4SLinus Torvalds 				if (freq >= suboptimal.frequency) {
1781da177e4SLinus Torvalds 					suboptimal.frequency = freq;
17950701588SViresh Kumar 					suboptimal.driver_data = i;
1801da177e4SLinus Torvalds 				}
1811da177e4SLinus Torvalds 			}
1821da177e4SLinus Torvalds 			break;
1835b0c0b16SStratos Karafotis 		case CPUFREQ_RELATION_C:
1845b0c0b16SStratos Karafotis 			diff = abs(freq - target_freq);
1855b0c0b16SStratos Karafotis 			if (diff < optimal.frequency ||
1865b0c0b16SStratos Karafotis 			    (diff == optimal.frequency &&
1875b0c0b16SStratos Karafotis 			     freq > table[optimal.driver_data].frequency)) {
1885b0c0b16SStratos Karafotis 				optimal.frequency = diff;
1895b0c0b16SStratos Karafotis 				optimal.driver_data = i;
1905b0c0b16SStratos Karafotis 			}
1915b0c0b16SStratos Karafotis 			break;
1921da177e4SLinus Torvalds 		}
1931da177e4SLinus Torvalds 	}
19450701588SViresh Kumar 	if (optimal.driver_data > i) {
19550701588SViresh Kumar 		if (suboptimal.driver_data > i)
1961da177e4SLinus Torvalds 			return -EINVAL;
19750701588SViresh Kumar 		*index = suboptimal.driver_data;
1981da177e4SLinus Torvalds 	} else
19950701588SViresh Kumar 		*index = optimal.driver_data;
2001da177e4SLinus Torvalds 
201ae87f10fSViresh Kumar 	pr_debug("target index is %u, freq is:%u kHz\n", *index,
202ae87f10fSViresh Kumar 		 table[*index].frequency);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	return 0;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
2071da177e4SLinus Torvalds 
208d3916691SViresh Kumar int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
209d3916691SViresh Kumar 		unsigned int freq)
210d3916691SViresh Kumar {
211f8bfc116SViresh Kumar 	struct cpufreq_frequency_table *pos, *table = policy->freq_table;
212d3916691SViresh Kumar 
213d3916691SViresh Kumar 	if (unlikely(!table)) {
214d3916691SViresh Kumar 		pr_debug("%s: Unable to find frequency table\n", __func__);
215d3916691SViresh Kumar 		return -ENOENT;
216d3916691SViresh Kumar 	}
217d3916691SViresh Kumar 
218041526f9SStratos Karafotis 	cpufreq_for_each_valid_entry(pos, table)
219041526f9SStratos Karafotis 		if (pos->frequency == freq)
220041526f9SStratos Karafotis 			return pos - table;
221d3916691SViresh Kumar 
222d3916691SViresh Kumar 	return -EINVAL;
223d3916691SViresh Kumar }
224d3916691SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
225d3916691SViresh Kumar 
2261da177e4SLinus Torvalds /**
227e32d22f7SFenghua Yu  * show_available_freqs - show available frequencies for the specified CPU
2281da177e4SLinus Torvalds  */
2296f19efc0SLukasz Majewski static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
2306f19efc0SLukasz Majewski 				    bool show_boost)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	ssize_t count = 0;
233041526f9SStratos Karafotis 	struct cpufreq_frequency_table *pos, *table = policy->freq_table;
2341da177e4SLinus Torvalds 
235e0b3165bSViresh Kumar 	if (!table)
2361da177e4SLinus Torvalds 		return -ENODEV;
2371da177e4SLinus Torvalds 
238041526f9SStratos Karafotis 	cpufreq_for_each_valid_entry(pos, table) {
2396f19efc0SLukasz Majewski 		/*
2406f19efc0SLukasz Majewski 		 * show_boost = true and driver_data = BOOST freq
2416f19efc0SLukasz Majewski 		 * display BOOST freqs
2426f19efc0SLukasz Majewski 		 *
2436f19efc0SLukasz Majewski 		 * show_boost = false and driver_data = BOOST freq
2446f19efc0SLukasz Majewski 		 * show_boost = true and driver_data != BOOST freq
2456f19efc0SLukasz Majewski 		 * continue - do not display anything
2466f19efc0SLukasz Majewski 		 *
2476f19efc0SLukasz Majewski 		 * show_boost = false and driver_data != BOOST freq
2486f19efc0SLukasz Majewski 		 * display NON BOOST freqs
2496f19efc0SLukasz Majewski 		 */
250041526f9SStratos Karafotis 		if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
2516f19efc0SLukasz Majewski 			continue;
2526f19efc0SLukasz Majewski 
253041526f9SStratos Karafotis 		count += sprintf(&buf[count], "%d ", pos->frequency);
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 	count += sprintf(&buf[count], "\n");
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	return count;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2616f19efc0SLukasz Majewski #define cpufreq_attr_available_freq(_name)	  \
2626f19efc0SLukasz Majewski struct freq_attr cpufreq_freq_attr_##_name##_freqs =     \
2636f19efc0SLukasz Majewski __ATTR_RO(_name##_frequencies)
2646f19efc0SLukasz Majewski 
2656f19efc0SLukasz Majewski /**
2666f19efc0SLukasz Majewski  * show_scaling_available_frequencies - show available normal frequencies for
2676f19efc0SLukasz Majewski  * the specified CPU
2686f19efc0SLukasz Majewski  */
2696f19efc0SLukasz Majewski static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
2706f19efc0SLukasz Majewski 						  char *buf)
2716f19efc0SLukasz Majewski {
2726f19efc0SLukasz Majewski 	return show_available_freqs(policy, buf, false);
2736f19efc0SLukasz Majewski }
2746f19efc0SLukasz Majewski cpufreq_attr_available_freq(scaling_available);
2751da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
2761da177e4SLinus Torvalds 
2776f19efc0SLukasz Majewski /**
2786f19efc0SLukasz Majewski  * show_available_boost_freqs - show available boost frequencies for
2796f19efc0SLukasz Majewski  * the specified CPU
2806f19efc0SLukasz Majewski  */
2816f19efc0SLukasz Majewski static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
2826f19efc0SLukasz Majewski 					      char *buf)
2836f19efc0SLukasz Majewski {
2846f19efc0SLukasz Majewski 	return show_available_freqs(policy, buf, true);
2856f19efc0SLukasz Majewski }
2866f19efc0SLukasz Majewski cpufreq_attr_available_freq(scaling_boost);
2876f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
2886f19efc0SLukasz Majewski 
28918434512SViresh Kumar struct freq_attr *cpufreq_generic_attr[] = {
29018434512SViresh Kumar 	&cpufreq_freq_attr_scaling_available_freqs,
2916f19efc0SLukasz Majewski #ifdef CONFIG_CPU_FREQ_BOOST_SW
2926f19efc0SLukasz Majewski 	&cpufreq_freq_attr_scaling_boost_freqs,
2936f19efc0SLukasz Majewski #endif
29418434512SViresh Kumar 	NULL,
29518434512SViresh Kumar };
29618434512SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
29718434512SViresh Kumar 
29827047a60SViresh Kumar int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
29927047a60SViresh Kumar 				      struct cpufreq_frequency_table *table)
30027047a60SViresh Kumar {
30127047a60SViresh Kumar 	int ret = cpufreq_frequency_table_cpuinfo(policy, table);
30227047a60SViresh Kumar 
30327047a60SViresh Kumar 	if (!ret)
304e0b3165bSViresh Kumar 		policy->freq_table = table;
30527047a60SViresh Kumar 
30627047a60SViresh Kumar 	return ret;
30727047a60SViresh Kumar }
30827047a60SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
30927047a60SViresh Kumar 
3101da177e4SLinus Torvalds MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
3111da177e4SLinus Torvalds MODULE_DESCRIPTION("CPUfreq frequency table helpers");
3121da177e4SLinus Torvalds MODULE_LICENSE("GPL");
313