cpufreq_userspace.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/cpufreq.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
  19. static DEFINE_MUTEX(userspace_mutex);
  20. /**
  21. * cpufreq_set - set the CPU frequency
  22. * @policy: pointer to policy struct where freq is being set
  23. * @freq: target frequency in kHz
  24. *
  25. * Sets the CPU frequency to freq.
  26. */
  27. static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
  28. {
  29. int ret = -EINVAL;
  30. unsigned int *setspeed = policy->governor_data;
  31. pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
  32. mutex_lock(&userspace_mutex);
  33. if (!per_cpu(cpu_is_managed, policy->cpu))
  34. goto err;
  35. *setspeed = freq;
  36. ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
  37. err:
  38. mutex_unlock(&userspace_mutex);
  39. return ret;
  40. }
  41. static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
  42. {
  43. return sprintf(buf, "%u\n", policy->cur);
  44. }
  45. static int cpufreq_userspace_policy_init(struct cpufreq_policy *policy)
  46. {
  47. unsigned int *setspeed;
  48. setspeed = kzalloc(sizeof(*setspeed), GFP_KERNEL);
  49. if (!setspeed)
  50. return -ENOMEM;
  51. policy->governor_data = setspeed;
  52. return 0;
  53. }
  54. static void cpufreq_userspace_policy_exit(struct cpufreq_policy *policy)
  55. {
  56. mutex_lock(&userspace_mutex);
  57. kfree(policy->governor_data);
  58. policy->governor_data = NULL;
  59. mutex_unlock(&userspace_mutex);
  60. }
  61. static int cpufreq_userspace_policy_start(struct cpufreq_policy *policy)
  62. {
  63. unsigned int *setspeed = policy->governor_data;
  64. BUG_ON(!policy->cur);
  65. pr_debug("started managing cpu %u\n", policy->cpu);
  66. mutex_lock(&userspace_mutex);
  67. per_cpu(cpu_is_managed, policy->cpu) = 1;
  68. *setspeed = policy->cur;
  69. mutex_unlock(&userspace_mutex);
  70. return 0;
  71. }
  72. static void cpufreq_userspace_policy_stop(struct cpufreq_policy *policy)
  73. {
  74. unsigned int *setspeed = policy->governor_data;
  75. pr_debug("managing cpu %u stopped\n", policy->cpu);
  76. mutex_lock(&userspace_mutex);
  77. per_cpu(cpu_is_managed, policy->cpu) = 0;
  78. *setspeed = 0;
  79. mutex_unlock(&userspace_mutex);
  80. }
  81. static void cpufreq_userspace_policy_limits(struct cpufreq_policy *policy)
  82. {
  83. unsigned int *setspeed = policy->governor_data;
  84. mutex_lock(&userspace_mutex);
  85. pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n",
  86. policy->cpu, policy->min, policy->max, policy->cur, *setspeed);
  87. if (policy->max < *setspeed)
  88. __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
  89. else if (policy->min > *setspeed)
  90. __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
  91. else
  92. __cpufreq_driver_target(policy, *setspeed, CPUFREQ_RELATION_L);
  93. mutex_unlock(&userspace_mutex);
  94. }
  95. static struct cpufreq_governor cpufreq_gov_userspace = {
  96. .name = "userspace",
  97. .init = cpufreq_userspace_policy_init,
  98. .exit = cpufreq_userspace_policy_exit,
  99. .start = cpufreq_userspace_policy_start,
  100. .stop = cpufreq_userspace_policy_stop,
  101. .limits = cpufreq_userspace_policy_limits,
  102. .store_setspeed = cpufreq_set,
  103. .show_setspeed = show_speed,
  104. .owner = THIS_MODULE,
  105. };
  106. static int __init cpufreq_gov_userspace_init(void)
  107. {
  108. return cpufreq_register_governor(&cpufreq_gov_userspace);
  109. }
  110. static void __exit cpufreq_gov_userspace_exit(void)
  111. {
  112. cpufreq_unregister_governor(&cpufreq_gov_userspace);
  113. }
  114. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
  115. "Russell King <rmk@arm.linux.org.uk>");
  116. MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
  117. MODULE_LICENSE("GPL");
  118. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
  119. struct cpufreq_governor *cpufreq_default_governor(void)
  120. {
  121. return &cpufreq_gov_userspace;
  122. }
  123. fs_initcall(cpufreq_gov_userspace_init);
  124. #else
  125. module_init(cpufreq_gov_userspace_init);
  126. #endif
  127. module_exit(cpufreq_gov_userspace_exit);