freq_table.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * linux/drivers/cpufreq/freq_table.c
  3. *
  4. * Copyright (C) 2002 - 2003 Dominik Brodowski
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/cpufreq.h>
  13. #include <linux/module.h>
  14. /*********************************************************************
  15. * FREQUENCY TABLE HELPERS *
  16. *********************************************************************/
  17. int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  18. struct cpufreq_frequency_table *table)
  19. {
  20. struct cpufreq_frequency_table *pos;
  21. unsigned int min_freq = ~0;
  22. unsigned int max_freq = 0;
  23. unsigned int freq;
  24. cpufreq_for_each_valid_entry(pos, table) {
  25. freq = pos->frequency;
  26. if (!cpufreq_boost_enabled()
  27. && (pos->flags & CPUFREQ_BOOST_FREQ))
  28. continue;
  29. pr_debug("table entry %u: %u kHz\n", (int)(pos - table), freq);
  30. if (freq < min_freq)
  31. min_freq = freq;
  32. if (freq > max_freq)
  33. max_freq = freq;
  34. }
  35. policy->min = policy->cpuinfo.min_freq = min_freq;
  36. policy->max = policy->cpuinfo.max_freq = max_freq;
  37. if (policy->min == ~0)
  38. return -EINVAL;
  39. else
  40. return 0;
  41. }
  42. EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
  43. int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  44. struct cpufreq_frequency_table *table)
  45. {
  46. struct cpufreq_frequency_table *pos;
  47. unsigned int freq, next_larger = ~0;
  48. bool found = false;
  49. pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
  50. policy->min, policy->max, policy->cpu);
  51. cpufreq_verify_within_cpu_limits(policy);
  52. cpufreq_for_each_valid_entry(pos, table) {
  53. freq = pos->frequency;
  54. if ((freq >= policy->min) && (freq <= policy->max)) {
  55. found = true;
  56. break;
  57. }
  58. if ((next_larger > freq) && (freq > policy->max))
  59. next_larger = freq;
  60. }
  61. if (!found) {
  62. policy->max = next_larger;
  63. cpufreq_verify_within_cpu_limits(policy);
  64. }
  65. pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
  66. policy->min, policy->max, policy->cpu);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
  70. /*
  71. * Generic routine to verify policy & frequency table, requires driver to set
  72. * policy->freq_table prior to it.
  73. */
  74. int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy)
  75. {
  76. struct cpufreq_frequency_table *table =
  77. cpufreq_frequency_get_table(policy->cpu);
  78. if (!table)
  79. return -ENODEV;
  80. return cpufreq_frequency_table_verify(policy, table);
  81. }
  82. EXPORT_SYMBOL_GPL(cpufreq_generic_frequency_table_verify);
  83. int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  84. struct cpufreq_frequency_table *table,
  85. unsigned int target_freq,
  86. unsigned int relation,
  87. unsigned int *index)
  88. {
  89. struct cpufreq_frequency_table optimal = {
  90. .driver_data = ~0,
  91. .frequency = 0,
  92. };
  93. struct cpufreq_frequency_table suboptimal = {
  94. .driver_data = ~0,
  95. .frequency = 0,
  96. };
  97. struct cpufreq_frequency_table *pos;
  98. unsigned int freq, diff, i = 0;
  99. pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
  100. target_freq, relation, policy->cpu);
  101. switch (relation) {
  102. case CPUFREQ_RELATION_H:
  103. suboptimal.frequency = ~0;
  104. break;
  105. case CPUFREQ_RELATION_L:
  106. case CPUFREQ_RELATION_C:
  107. optimal.frequency = ~0;
  108. break;
  109. }
  110. cpufreq_for_each_valid_entry(pos, table) {
  111. freq = pos->frequency;
  112. i = pos - table;
  113. if ((freq < policy->min) || (freq > policy->max))
  114. continue;
  115. if (freq == target_freq) {
  116. optimal.driver_data = i;
  117. break;
  118. }
  119. switch (relation) {
  120. case CPUFREQ_RELATION_H:
  121. if (freq < target_freq) {
  122. if (freq >= optimal.frequency) {
  123. optimal.frequency = freq;
  124. optimal.driver_data = i;
  125. }
  126. } else {
  127. if (freq <= suboptimal.frequency) {
  128. suboptimal.frequency = freq;
  129. suboptimal.driver_data = i;
  130. }
  131. }
  132. break;
  133. case CPUFREQ_RELATION_L:
  134. if (freq > target_freq) {
  135. if (freq <= optimal.frequency) {
  136. optimal.frequency = freq;
  137. optimal.driver_data = i;
  138. }
  139. } else {
  140. if (freq >= suboptimal.frequency) {
  141. suboptimal.frequency = freq;
  142. suboptimal.driver_data = i;
  143. }
  144. }
  145. break;
  146. case CPUFREQ_RELATION_C:
  147. diff = abs(freq - target_freq);
  148. if (diff < optimal.frequency ||
  149. (diff == optimal.frequency &&
  150. freq > table[optimal.driver_data].frequency)) {
  151. optimal.frequency = diff;
  152. optimal.driver_data = i;
  153. }
  154. break;
  155. }
  156. }
  157. if (optimal.driver_data > i) {
  158. if (suboptimal.driver_data > i)
  159. return -EINVAL;
  160. *index = suboptimal.driver_data;
  161. } else
  162. *index = optimal.driver_data;
  163. pr_debug("target index is %u, freq is:%u kHz\n", *index,
  164. table[*index].frequency);
  165. return 0;
  166. }
  167. EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
  168. int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
  169. unsigned int freq)
  170. {
  171. struct cpufreq_frequency_table *pos, *table;
  172. table = cpufreq_frequency_get_table(policy->cpu);
  173. if (unlikely(!table)) {
  174. pr_debug("%s: Unable to find frequency table\n", __func__);
  175. return -ENOENT;
  176. }
  177. cpufreq_for_each_valid_entry(pos, table)
  178. if (pos->frequency == freq)
  179. return pos - table;
  180. return -EINVAL;
  181. }
  182. EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index);
  183. /**
  184. * show_available_freqs - show available frequencies for the specified CPU
  185. */
  186. static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf,
  187. bool show_boost)
  188. {
  189. ssize_t count = 0;
  190. struct cpufreq_frequency_table *pos, *table = policy->freq_table;
  191. if (!table)
  192. return -ENODEV;
  193. cpufreq_for_each_valid_entry(pos, table) {
  194. /*
  195. * show_boost = true and driver_data = BOOST freq
  196. * display BOOST freqs
  197. *
  198. * show_boost = false and driver_data = BOOST freq
  199. * show_boost = true and driver_data != BOOST freq
  200. * continue - do not display anything
  201. *
  202. * show_boost = false and driver_data != BOOST freq
  203. * display NON BOOST freqs
  204. */
  205. if (show_boost ^ (pos->flags & CPUFREQ_BOOST_FREQ))
  206. continue;
  207. count += sprintf(&buf[count], "%d ", pos->frequency);
  208. }
  209. count += sprintf(&buf[count], "\n");
  210. return count;
  211. }
  212. #define cpufreq_attr_available_freq(_name) \
  213. struct freq_attr cpufreq_freq_attr_##_name##_freqs = \
  214. __ATTR_RO(_name##_frequencies)
  215. /**
  216. * show_scaling_available_frequencies - show available normal frequencies for
  217. * the specified CPU
  218. */
  219. static ssize_t scaling_available_frequencies_show(struct cpufreq_policy *policy,
  220. char *buf)
  221. {
  222. return show_available_freqs(policy, buf, false);
  223. }
  224. cpufreq_attr_available_freq(scaling_available);
  225. EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
  226. /**
  227. * show_available_boost_freqs - show available boost frequencies for
  228. * the specified CPU
  229. */
  230. static ssize_t scaling_boost_frequencies_show(struct cpufreq_policy *policy,
  231. char *buf)
  232. {
  233. return show_available_freqs(policy, buf, true);
  234. }
  235. cpufreq_attr_available_freq(scaling_boost);
  236. EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_boost_freqs);
  237. struct freq_attr *cpufreq_generic_attr[] = {
  238. &cpufreq_freq_attr_scaling_available_freqs,
  239. #ifdef CONFIG_CPU_FREQ_BOOST_SW
  240. &cpufreq_freq_attr_scaling_boost_freqs,
  241. #endif
  242. NULL,
  243. };
  244. EXPORT_SYMBOL_GPL(cpufreq_generic_attr);
  245. int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
  246. struct cpufreq_frequency_table *table)
  247. {
  248. int ret = cpufreq_frequency_table_cpuinfo(policy, table);
  249. if (!ret)
  250. policy->freq_table = table;
  251. return ret;
  252. }
  253. EXPORT_SYMBOL_GPL(cpufreq_table_validate_and_show);
  254. struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
  255. struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
  256. {
  257. struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
  258. return policy ? policy->freq_table : NULL;
  259. }
  260. EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
  261. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
  262. MODULE_DESCRIPTION("CPUfreq frequency table helpers");
  263. MODULE_LICENSE("GPL");