freq_table.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/cpufreq.h>
  16. /*********************************************************************
  17. * FREQUENCY TABLE HELPERS *
  18. *********************************************************************/
  19. int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  20. struct cpufreq_frequency_table *table)
  21. {
  22. unsigned int min_freq = ~0;
  23. unsigned int max_freq = 0;
  24. unsigned int i;
  25. for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
  26. unsigned int freq = table[i].frequency;
  27. if (freq == CPUFREQ_ENTRY_INVALID) {
  28. pr_debug("table entry %u is invalid, skipping\n", i);
  29. continue;
  30. }
  31. pr_debug("table entry %u: %u kHz, %u index\n",
  32. i, freq, table[i].index);
  33. if (freq < min_freq)
  34. min_freq = freq;
  35. if (freq > max_freq)
  36. max_freq = freq;
  37. }
  38. policy->min = policy->cpuinfo.min_freq = min_freq;
  39. policy->max = policy->cpuinfo.max_freq = max_freq;
  40. if (policy->min == ~0)
  41. return -EINVAL;
  42. else
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
  46. int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  47. struct cpufreq_frequency_table *table)
  48. {
  49. unsigned int next_larger = ~0;
  50. unsigned int i;
  51. unsigned int count = 0;
  52. pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
  53. policy->min, policy->max, policy->cpu);
  54. if (!cpu_online(policy->cpu))
  55. return -EINVAL;
  56. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  57. policy->cpuinfo.max_freq);
  58. for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
  59. unsigned int freq = table[i].frequency;
  60. if (freq == CPUFREQ_ENTRY_INVALID)
  61. continue;
  62. if ((freq >= policy->min) && (freq <= policy->max))
  63. count++;
  64. else if ((next_larger > freq) && (freq > policy->max))
  65. next_larger = freq;
  66. }
  67. if (!count)
  68. policy->max = next_larger;
  69. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  70. policy->cpuinfo.max_freq);
  71. pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
  72. policy->min, policy->max, policy->cpu);
  73. return 0;
  74. }
  75. EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
  76. int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  77. struct cpufreq_frequency_table *table,
  78. unsigned int target_freq,
  79. unsigned int relation,
  80. unsigned int *index)
  81. {
  82. struct cpufreq_frequency_table optimal = {
  83. .index = ~0,
  84. .frequency = 0,
  85. };
  86. struct cpufreq_frequency_table suboptimal = {
  87. .index = ~0,
  88. .frequency = 0,
  89. };
  90. unsigned int i;
  91. pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
  92. target_freq, relation, policy->cpu);
  93. switch (relation) {
  94. case CPUFREQ_RELATION_H:
  95. suboptimal.frequency = ~0;
  96. break;
  97. case CPUFREQ_RELATION_L:
  98. optimal.frequency = ~0;
  99. break;
  100. }
  101. if (!cpu_online(policy->cpu))
  102. return -EINVAL;
  103. for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
  104. unsigned int freq = table[i].frequency;
  105. if (freq == CPUFREQ_ENTRY_INVALID)
  106. continue;
  107. if ((freq < policy->min) || (freq > policy->max))
  108. continue;
  109. switch (relation) {
  110. case CPUFREQ_RELATION_H:
  111. if (freq <= target_freq) {
  112. if (freq >= optimal.frequency) {
  113. optimal.frequency = freq;
  114. optimal.index = i;
  115. }
  116. } else {
  117. if (freq <= suboptimal.frequency) {
  118. suboptimal.frequency = freq;
  119. suboptimal.index = i;
  120. }
  121. }
  122. break;
  123. case CPUFREQ_RELATION_L:
  124. if (freq >= target_freq) {
  125. if (freq <= optimal.frequency) {
  126. optimal.frequency = freq;
  127. optimal.index = i;
  128. }
  129. } else {
  130. if (freq >= suboptimal.frequency) {
  131. suboptimal.frequency = freq;
  132. suboptimal.index = i;
  133. }
  134. }
  135. break;
  136. }
  137. }
  138. if (optimal.index > i) {
  139. if (suboptimal.index > i)
  140. return -EINVAL;
  141. *index = suboptimal.index;
  142. } else
  143. *index = optimal.index;
  144. pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
  145. table[*index].index);
  146. return 0;
  147. }
  148. EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
  149. static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
  150. /**
  151. * show_available_freqs - show available frequencies for the specified CPU
  152. */
  153. static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
  154. {
  155. unsigned int i = 0;
  156. unsigned int cpu = policy->cpu;
  157. ssize_t count = 0;
  158. struct cpufreq_frequency_table *table;
  159. if (!per_cpu(cpufreq_show_table, cpu))
  160. return -ENODEV;
  161. table = per_cpu(cpufreq_show_table, cpu);
  162. for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
  163. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  164. continue;
  165. count += sprintf(&buf[count], "%d ", table[i].frequency);
  166. }
  167. count += sprintf(&buf[count], "\n");
  168. return count;
  169. }
  170. struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
  171. .attr = { .name = "scaling_available_frequencies",
  172. .mode = 0444,
  173. },
  174. .show = show_available_freqs,
  175. };
  176. EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
  177. /*
  178. * if you use these, you must assure that the frequency table is valid
  179. * all the time between get_attr and put_attr!
  180. */
  181. void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
  182. unsigned int cpu)
  183. {
  184. pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
  185. per_cpu(cpufreq_show_table, cpu) = table;
  186. }
  187. EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
  188. void cpufreq_frequency_table_put_attr(unsigned int cpu)
  189. {
  190. pr_debug("clearing show_table for cpu %u\n", cpu);
  191. per_cpu(cpufreq_show_table, cpu) = NULL;
  192. }
  193. EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
  194. struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
  195. {
  196. return per_cpu(cpufreq_show_table, cpu);
  197. }
  198. EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
  199. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
  200. MODULE_DESCRIPTION("CPUfreq frequency table helpers");
  201. MODULE_LICENSE("GPL");