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 { 24041526f9SStratos Karafotis struct cpufreq_frequency_table *pos; 251da177e4SLinus Torvalds unsigned int min_freq = ~0; 261da177e4SLinus Torvalds unsigned int max_freq = 0; 27041526f9SStratos Karafotis unsigned int freq; 281da177e4SLinus Torvalds 29041526f9SStratos Karafotis cpufreq_for_each_valid_entry(pos, table) { 30041526f9SStratos Karafotis freq = pos->frequency; 311da177e4SLinus Torvalds 326f19efc0SLukasz Majewski if (!cpufreq_boost_enabled() 33041526f9SStratos Karafotis && (pos->flags & CPUFREQ_BOOST_FREQ)) 346f19efc0SLukasz Majewski continue; 356f19efc0SLukasz Majewski 36041526f9SStratos Karafotis pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq); 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 { 57041526f9SStratos Karafotis struct cpufreq_frequency_table *pos; 58041526f9SStratos Karafotis unsigned int freq, next_larger = ~0; 5977db50c4SViresh Kumar bool found = false; 601da177e4SLinus Torvalds 612d06d8c4SDominik Brodowski pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n", 62e08f5f5bSGautham R Shenoy policy->min, policy->max, policy->cpu); 631da177e4SLinus Torvalds 64be49e346SViresh Kumar cpufreq_verify_within_cpu_limits(policy); 651da177e4SLinus Torvalds 66041526f9SStratos Karafotis cpufreq_for_each_valid_entry(pos, table) { 67041526f9SStratos Karafotis freq = pos->frequency; 68041526f9SStratos Karafotis 6977db50c4SViresh Kumar if ((freq >= policy->min) && (freq <= policy->max)) { 7077db50c4SViresh Kumar found = true; 7177db50c4SViresh Kumar break; 7277db50c4SViresh Kumar } 7377db50c4SViresh Kumar 7477db50c4SViresh Kumar if ((next_larger > freq) && (freq > policy->max)) 751da177e4SLinus Torvalds next_larger = freq; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 7877db50c4SViresh Kumar if (!found) { 791da177e4SLinus Torvalds policy->max = next_larger; 80be49e346SViresh Kumar cpufreq_verify_within_cpu_limits(policy); 8177db50c4SViresh Kumar } 821da177e4SLinus Torvalds 832d06d8c4SDominik Brodowski pr_debug("verification lead to (%u - %u kHz) for cpu %u\n", 84e08f5f5bSGautham R Shenoy policy->min, policy->max, policy->cpu); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds return 0; 871da177e4SLinus Torvalds } 881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify); 891da177e4SLinus Torvalds 9018434512SViresh Kumar /* 91e0b3165bSViresh Kumar * Generic routine to verify policy & frequency table, requires driver to set 92e0b3165bSViresh Kumar * policy->freq_table prior to it. 9318434512SViresh Kumar */ 9418434512SViresh Kumar int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy) 9518434512SViresh Kumar { 9618434512SViresh Kumar struct cpufreq_frequency_table *table = 9718434512SViresh Kumar cpufreq_frequency_get_table(policy->cpu); 9818434512SViresh Kumar if (!table) 9918434512SViresh Kumar return -ENODEV; 10018434512SViresh Kumar 10118434512SViresh Kumar return cpufreq_frequency_table_verify(policy, table); 10218434512SViresh Kumar } 10318434512SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify); 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds int cpufreq_frequency_table_target(struct cpufreq_policy *policy, 1061da177e4SLinus Torvalds struct cpufreq_frequency_table *table, 1071da177e4SLinus Torvalds unsigned int target_freq, 1081da177e4SLinus Torvalds unsigned int relation, 1091da177e4SLinus Torvalds unsigned int *index) 1101da177e4SLinus Torvalds { 111484944a5SDave Jones struct cpufreq_frequency_table optimal = { 11250701588SViresh Kumar .driver_data = ~0, 113484944a5SDave Jones .frequency = 0, 114484944a5SDave Jones }; 115484944a5SDave Jones struct cpufreq_frequency_table suboptimal = { 11650701588SViresh Kumar .driver_data = ~0, 117484944a5SDave Jones .frequency = 0, 118484944a5SDave Jones }; 119041526f9SStratos Karafotis struct cpufreq_frequency_table *pos; 120*5b0c0b16SStratos Karafotis unsigned int freq, diff, i = 0; 1211da177e4SLinus Torvalds 1222d06d8c4SDominik Brodowski pr_debug("request for target %u kHz (relation: %u) for cpu %u\n", 123e08f5f5bSGautham R Shenoy target_freq, relation, policy->cpu); 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds switch (relation) { 1261da177e4SLinus Torvalds case CPUFREQ_RELATION_H: 1271da177e4SLinus Torvalds suboptimal.frequency = ~0; 1281da177e4SLinus Torvalds break; 1291da177e4SLinus Torvalds case CPUFREQ_RELATION_L: 130*5b0c0b16SStratos Karafotis case CPUFREQ_RELATION_C: 1311da177e4SLinus Torvalds optimal.frequency = ~0; 1321da177e4SLinus Torvalds break; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 135041526f9SStratos Karafotis cpufreq_for_each_valid_entry(pos, table) { 136041526f9SStratos Karafotis freq = pos->frequency; 137041526f9SStratos Karafotis 138041526f9SStratos Karafotis i = pos - table; 1391da177e4SLinus Torvalds if ((freq < policy->min) || (freq > policy->max)) 1401da177e4SLinus Torvalds continue; 1411e498856SStratos Karafotis if (freq == target_freq) { 1421e498856SStratos Karafotis optimal.driver_data = i; 1431e498856SStratos Karafotis break; 1441e498856SStratos Karafotis } 1451da177e4SLinus Torvalds switch (relation) { 1461da177e4SLinus Torvalds case CPUFREQ_RELATION_H: 1471e498856SStratos Karafotis if (freq < target_freq) { 1481da177e4SLinus Torvalds if (freq >= optimal.frequency) { 1491da177e4SLinus Torvalds optimal.frequency = freq; 15050701588SViresh Kumar optimal.driver_data = i; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds } else { 1531da177e4SLinus Torvalds if (freq <= suboptimal.frequency) { 1541da177e4SLinus Torvalds suboptimal.frequency = freq; 15550701588SViresh Kumar suboptimal.driver_data = i; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds break; 1591da177e4SLinus Torvalds case CPUFREQ_RELATION_L: 1601e498856SStratos Karafotis if (freq > target_freq) { 1611da177e4SLinus Torvalds if (freq <= optimal.frequency) { 1621da177e4SLinus Torvalds optimal.frequency = freq; 16350701588SViresh Kumar optimal.driver_data = i; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds } else { 1661da177e4SLinus Torvalds if (freq >= suboptimal.frequency) { 1671da177e4SLinus Torvalds suboptimal.frequency = freq; 16850701588SViresh Kumar suboptimal.driver_data = i; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds break; 172*5b0c0b16SStratos Karafotis case CPUFREQ_RELATION_C: 173*5b0c0b16SStratos Karafotis diff = abs(freq - target_freq); 174*5b0c0b16SStratos Karafotis if (diff < optimal.frequency || 175*5b0c0b16SStratos Karafotis (diff == optimal.frequency && 176*5b0c0b16SStratos Karafotis freq > table[optimal.driver_data].frequency)) { 177*5b0c0b16SStratos Karafotis optimal.frequency = diff; 178*5b0c0b16SStratos Karafotis optimal.driver_data = i; 179*5b0c0b16SStratos Karafotis } 180*5b0c0b16SStratos Karafotis break; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds } 18350701588SViresh Kumar if (optimal.driver_data > i) { 18450701588SViresh Kumar if (suboptimal.driver_data > i) 1851da177e4SLinus Torvalds return -EINVAL; 18650701588SViresh Kumar *index = suboptimal.driver_data; 1871da177e4SLinus Torvalds } else 18850701588SViresh Kumar *index = optimal.driver_data; 1891da177e4SLinus Torvalds 190ae87f10fSViresh Kumar pr_debug("target index is %u, freq is:%u kHz\n", *index, 191ae87f10fSViresh Kumar table[*index].frequency); 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds return 0; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target); 1961da177e4SLinus Torvalds 197d3916691SViresh Kumar int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, 198d3916691SViresh Kumar unsigned int freq) 199d3916691SViresh Kumar { 200041526f9SStratos Karafotis struct cpufreq_frequency_table *pos, *table; 201d3916691SViresh Kumar 202d3916691SViresh Kumar table = cpufreq_frequency_get_table(policy->cpu); 203d3916691SViresh Kumar if (unlikely(!table)) { 204d3916691SViresh Kumar pr_debug("%s: Unable to find frequency table\n", __func__); 205d3916691SViresh Kumar return -ENOENT; 206d3916691SViresh Kumar } 207d3916691SViresh Kumar 208041526f9SStratos Karafotis cpufreq_for_each_valid_entry(pos, table) 209041526f9SStratos Karafotis if (pos->frequency == freq) 210041526f9SStratos Karafotis return pos - table; 211d3916691SViresh Kumar 212d3916691SViresh Kumar return -EINVAL; 213d3916691SViresh Kumar } 214d3916691SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index); 215d3916691SViresh Kumar 2161da177e4SLinus Torvalds /** 217e32d22f7SFenghua Yu * show_available_freqs - show available frequencies for the specified CPU 2181da177e4SLinus Torvalds */ 2196f19efc0SLukasz Majewski static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf, 2206f19efc0SLukasz Majewski bool show_boost) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds ssize_t count = 0; 223041526f9SStratos Karafotis struct cpufreq_frequency_table *pos, *table = policy->freq_table; 2241da177e4SLinus Torvalds 225e0b3165bSViresh Kumar if (!table) 2261da177e4SLinus Torvalds return -ENODEV; 2271da177e4SLinus Torvalds 228041526f9SStratos Karafotis cpufreq_for_each_valid_entry(pos, table) { 2296f19efc0SLukasz Majewski /* 2306f19efc0SLukasz Majewski * show_boost = true and driver_data = BOOST freq 2316f19efc0SLukasz Majewski * display BOOST freqs 2326f19efc0SLukasz Majewski * 2336f19efc0SLukasz Majewski * show_boost = false and driver_data = BOOST freq 2346f19efc0SLukasz Majewski * show_boost = true and driver_data != BOOST freq 2356f19efc0SLukasz Majewski * continue - do not display anything 2366f19efc0SLukasz Majewski * 2376f19efc0SLukasz Majewski * show_boost = false and driver_data != BOOST freq 2386f19efc0SLukasz Majewski * display NON BOOST freqs 2396f19efc0SLukasz Majewski */ 240041526f9SStratos Karafotis if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ)) 2416f19efc0SLukasz Majewski continue; 2426f19efc0SLukasz Majewski 243041526f9SStratos Karafotis count += sprintf(&buf[count], "%d ", pos->frequency); 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds count += sprintf(&buf[count], "\n"); 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds return count; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2516f19efc0SLukasz Majewski #define cpufreq_attr_available_freq(_name) \ 2526f19efc0SLukasz Majewski struct freq_attr cpufreq_freq_attr_##_name##_freqs = \ 2536f19efc0SLukasz Majewski __ATTR_RO(_name##_frequencies) 2546f19efc0SLukasz Majewski 2556f19efc0SLukasz Majewski /** 2566f19efc0SLukasz Majewski * show_scaling_available_frequencies - show available normal frequencies for 2576f19efc0SLukasz Majewski * the specified CPU 2586f19efc0SLukasz Majewski */ 2596f19efc0SLukasz Majewski static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy, 2606f19efc0SLukasz Majewski char *buf) 2616f19efc0SLukasz Majewski { 2626f19efc0SLukasz Majewski return show_available_freqs(policy, buf, false); 2636f19efc0SLukasz Majewski } 2646f19efc0SLukasz Majewski cpufreq_attr_available_freq(scaling_available); 2651da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs); 2661da177e4SLinus Torvalds 2676f19efc0SLukasz Majewski /** 2686f19efc0SLukasz Majewski * show_available_boost_freqs - show available boost frequencies for 2696f19efc0SLukasz Majewski * the specified CPU 2706f19efc0SLukasz Majewski */ 2716f19efc0SLukasz Majewski static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy, 2726f19efc0SLukasz Majewski char *buf) 2736f19efc0SLukasz Majewski { 2746f19efc0SLukasz Majewski return show_available_freqs(policy, buf, true); 2756f19efc0SLukasz Majewski } 2766f19efc0SLukasz Majewski cpufreq_attr_available_freq(scaling_boost); 2776f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs); 2786f19efc0SLukasz Majewski 27918434512SViresh Kumar struct freq_attr *cpufreq_generic_attr[] = { 28018434512SViresh Kumar &cpufreq_freq_attr_scaling_available_freqs, 2816f19efc0SLukasz Majewski #ifdef CONFIG_CPU_FREQ_BOOST_SW 2826f19efc0SLukasz Majewski &cpufreq_freq_attr_scaling_boost_freqs, 2836f19efc0SLukasz Majewski #endif 28418434512SViresh Kumar NULL, 28518434512SViresh Kumar }; 28618434512SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_attr); 28718434512SViresh Kumar 28827047a60SViresh Kumar int cpufreq_table_validate_and_show(struct cpufreq_policy *policy, 28927047a60SViresh Kumar struct cpufreq_frequency_table *table) 29027047a60SViresh Kumar { 29127047a60SViresh Kumar int ret = cpufreq_frequency_table_cpuinfo(policy, table); 29227047a60SViresh Kumar 29327047a60SViresh Kumar if (!ret) 294e0b3165bSViresh Kumar policy->freq_table = table; 29527047a60SViresh Kumar 29627047a60SViresh Kumar return ret; 29727047a60SViresh Kumar } 29827047a60SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show); 29927047a60SViresh Kumar 300e0b3165bSViresh Kumar struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu); 301b8eed8afSViresh Kumar 3021da177e4SLinus Torvalds struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) 3031da177e4SLinus Torvalds { 304e0b3165bSViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 305e0b3165bSViresh Kumar return policy ? policy->freq_table : NULL; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>"); 3101da177e4SLinus Torvalds MODULE_DESCRIPTION("CPUfreq frequency table helpers"); 3111da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 312