cpufreq_userspace.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * linux/drivers/cpufreq/cpufreq_userspace.c
  3. *
  4. * Copyright (C) 2001 Russell King
  5. * (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/smp.h>
  15. #include <linux/init.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/cpu.h>
  20. #include <linux/types.h>
  21. #include <linux/fs.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/mutex.h>
  24. /**
  25. * A few values needed by the userspace governor
  26. */
  27. static DEFINE_PER_CPU(unsigned int, cpu_max_freq);
  28. static DEFINE_PER_CPU(unsigned int, cpu_min_freq);
  29. static DEFINE_PER_CPU(unsigned int, cpu_cur_freq); /* current CPU freq */
  30. static DEFINE_PER_CPU(unsigned int, cpu_set_freq); /* CPU freq desired by
  31. userspace */
  32. static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
  33. static DEFINE_MUTEX(userspace_mutex);
  34. static int cpus_using_userspace_governor;
  35. /* keep track of frequency transitions */
  36. static int
  37. userspace_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  38. void *data)
  39. {
  40. struct cpufreq_freqs *freq = data;
  41. if (!per_cpu(cpu_is_managed, freq->cpu))
  42. return 0;
  43. if (val == CPUFREQ_POSTCHANGE) {
  44. pr_debug("saving cpu_cur_freq of cpu %u to be %u kHz\n",
  45. freq->cpu, freq->new);
  46. per_cpu(cpu_cur_freq, freq->cpu) = freq->new;
  47. }
  48. return 0;
  49. }
  50. static struct notifier_block userspace_cpufreq_notifier_block = {
  51. .notifier_call = userspace_cpufreq_notifier
  52. };
  53. /**
  54. * cpufreq_set - set the CPU frequency
  55. * @policy: pointer to policy struct where freq is being set
  56. * @freq: target frequency in kHz
  57. *
  58. * Sets the CPU frequency to freq.
  59. */
  60. static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
  61. {
  62. int ret = -EINVAL;
  63. pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
  64. mutex_lock(&userspace_mutex);
  65. if (!per_cpu(cpu_is_managed, policy->cpu))
  66. goto err;
  67. per_cpu(cpu_set_freq, policy->cpu) = freq;
  68. if (freq < per_cpu(cpu_min_freq, policy->cpu))
  69. freq = per_cpu(cpu_min_freq, policy->cpu);
  70. if (freq > per_cpu(cpu_max_freq, policy->cpu))
  71. freq = per_cpu(cpu_max_freq, policy->cpu);
  72. /*
  73. * We're safe from concurrent calls to ->target() here
  74. * as we hold the userspace_mutex lock. If we were calling
  75. * cpufreq_driver_target, a deadlock situation might occur:
  76. * A: cpufreq_set (lock userspace_mutex) ->
  77. * cpufreq_driver_target(lock policy->lock)
  78. * B: cpufreq_set_policy(lock policy->lock) ->
  79. * __cpufreq_governor ->
  80. * cpufreq_governor_userspace (lock userspace_mutex)
  81. */
  82. ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
  83. err:
  84. mutex_unlock(&userspace_mutex);
  85. return ret;
  86. }
  87. static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
  88. {
  89. return sprintf(buf, "%u\n", per_cpu(cpu_cur_freq, policy->cpu));
  90. }
  91. static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
  92. unsigned int event)
  93. {
  94. unsigned int cpu = policy->cpu;
  95. int rc = 0;
  96. switch (event) {
  97. case CPUFREQ_GOV_START:
  98. if (!cpu_online(cpu))
  99. return -EINVAL;
  100. BUG_ON(!policy->cur);
  101. mutex_lock(&userspace_mutex);
  102. if (cpus_using_userspace_governor == 0) {
  103. cpufreq_register_notifier(
  104. &userspace_cpufreq_notifier_block,
  105. CPUFREQ_TRANSITION_NOTIFIER);
  106. }
  107. cpus_using_userspace_governor++;
  108. per_cpu(cpu_is_managed, cpu) = 1;
  109. per_cpu(cpu_min_freq, cpu) = policy->min;
  110. per_cpu(cpu_max_freq, cpu) = policy->max;
  111. per_cpu(cpu_cur_freq, cpu) = policy->cur;
  112. per_cpu(cpu_set_freq, cpu) = policy->cur;
  113. pr_debug("managing cpu %u started "
  114. "(%u - %u kHz, currently %u kHz)\n",
  115. cpu,
  116. per_cpu(cpu_min_freq, cpu),
  117. per_cpu(cpu_max_freq, cpu),
  118. per_cpu(cpu_cur_freq, cpu));
  119. mutex_unlock(&userspace_mutex);
  120. break;
  121. case CPUFREQ_GOV_STOP:
  122. mutex_lock(&userspace_mutex);
  123. cpus_using_userspace_governor--;
  124. if (cpus_using_userspace_governor == 0) {
  125. cpufreq_unregister_notifier(
  126. &userspace_cpufreq_notifier_block,
  127. CPUFREQ_TRANSITION_NOTIFIER);
  128. }
  129. per_cpu(cpu_is_managed, cpu) = 0;
  130. per_cpu(cpu_min_freq, cpu) = 0;
  131. per_cpu(cpu_max_freq, cpu) = 0;
  132. per_cpu(cpu_set_freq, cpu) = 0;
  133. pr_debug("managing cpu %u stopped\n", cpu);
  134. mutex_unlock(&userspace_mutex);
  135. break;
  136. case CPUFREQ_GOV_LIMITS:
  137. mutex_lock(&userspace_mutex);
  138. pr_debug("limit event for cpu %u: %u - %u kHz, "
  139. "currently %u kHz, last set to %u kHz\n",
  140. cpu, policy->min, policy->max,
  141. per_cpu(cpu_cur_freq, cpu),
  142. per_cpu(cpu_set_freq, cpu));
  143. if (policy->max < per_cpu(cpu_set_freq, cpu)) {
  144. __cpufreq_driver_target(policy, policy->max,
  145. CPUFREQ_RELATION_H);
  146. } else if (policy->min > per_cpu(cpu_set_freq, cpu)) {
  147. __cpufreq_driver_target(policy, policy->min,
  148. CPUFREQ_RELATION_L);
  149. } else {
  150. __cpufreq_driver_target(policy,
  151. per_cpu(cpu_set_freq, cpu),
  152. CPUFREQ_RELATION_L);
  153. }
  154. per_cpu(cpu_min_freq, cpu) = policy->min;
  155. per_cpu(cpu_max_freq, cpu) = policy->max;
  156. per_cpu(cpu_cur_freq, cpu) = policy->cur;
  157. mutex_unlock(&userspace_mutex);
  158. break;
  159. }
  160. return rc;
  161. }
  162. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
  163. static
  164. #endif
  165. struct cpufreq_governor cpufreq_gov_userspace = {
  166. .name = "userspace",
  167. .governor = cpufreq_governor_userspace,
  168. .store_setspeed = cpufreq_set,
  169. .show_setspeed = show_speed,
  170. .owner = THIS_MODULE,
  171. };
  172. static int __init cpufreq_gov_userspace_init(void)
  173. {
  174. return cpufreq_register_governor(&cpufreq_gov_userspace);
  175. }
  176. static void __exit cpufreq_gov_userspace_exit(void)
  177. {
  178. cpufreq_unregister_governor(&cpufreq_gov_userspace);
  179. }
  180. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
  181. "Russell King <rmk@arm.linux.org.uk>");
  182. MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
  183. MODULE_LICENSE("GPL");
  184. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
  185. fs_initcall(cpufreq_gov_userspace_init);
  186. #else
  187. module_init(cpufreq_gov_userspace_init);
  188. #endif
  189. module_exit(cpufreq_gov_userspace_exit);