cpufreq_userspace.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 int cpufreq_governor_userspace(struct cpufreq_policy *policy,
  55. unsigned int event)
  56. {
  57. unsigned int *setspeed = policy->governor_data;
  58. unsigned int cpu = policy->cpu;
  59. int rc = 0;
  60. if (event == CPUFREQ_GOV_POLICY_INIT)
  61. return cpufreq_userspace_policy_init(policy);
  62. if (!setspeed)
  63. return -EINVAL;
  64. switch (event) {
  65. case CPUFREQ_GOV_POLICY_EXIT:
  66. mutex_lock(&userspace_mutex);
  67. policy->governor_data = NULL;
  68. kfree(setspeed);
  69. mutex_unlock(&userspace_mutex);
  70. break;
  71. case CPUFREQ_GOV_START:
  72. BUG_ON(!policy->cur);
  73. pr_debug("started managing cpu %u\n", cpu);
  74. mutex_lock(&userspace_mutex);
  75. per_cpu(cpu_is_managed, cpu) = 1;
  76. *setspeed = policy->cur;
  77. mutex_unlock(&userspace_mutex);
  78. break;
  79. case CPUFREQ_GOV_STOP:
  80. pr_debug("managing cpu %u stopped\n", cpu);
  81. mutex_lock(&userspace_mutex);
  82. per_cpu(cpu_is_managed, cpu) = 0;
  83. *setspeed = 0;
  84. mutex_unlock(&userspace_mutex);
  85. break;
  86. case CPUFREQ_GOV_LIMITS:
  87. mutex_lock(&userspace_mutex);
  88. pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz, last set to %u kHz\n",
  89. cpu, policy->min, policy->max, policy->cur, *setspeed);
  90. if (policy->max < *setspeed)
  91. __cpufreq_driver_target(policy, policy->max,
  92. CPUFREQ_RELATION_H);
  93. else if (policy->min > *setspeed)
  94. __cpufreq_driver_target(policy, policy->min,
  95. CPUFREQ_RELATION_L);
  96. else
  97. __cpufreq_driver_target(policy, *setspeed,
  98. CPUFREQ_RELATION_L);
  99. mutex_unlock(&userspace_mutex);
  100. break;
  101. }
  102. return rc;
  103. }
  104. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
  105. static
  106. #endif
  107. struct cpufreq_governor cpufreq_gov_userspace = {
  108. .name = "userspace",
  109. .governor = cpufreq_governor_userspace,
  110. .store_setspeed = cpufreq_set,
  111. .show_setspeed = show_speed,
  112. .owner = THIS_MODULE,
  113. };
  114. static int __init cpufreq_gov_userspace_init(void)
  115. {
  116. return cpufreq_register_governor(&cpufreq_gov_userspace);
  117. }
  118. static void __exit cpufreq_gov_userspace_exit(void)
  119. {
  120. cpufreq_unregister_governor(&cpufreq_gov_userspace);
  121. }
  122. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
  123. "Russell King <rmk@arm.linux.org.uk>");
  124. MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
  125. MODULE_LICENSE("GPL");
  126. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
  127. fs_initcall(cpufreq_gov_userspace_init);
  128. #else
  129. module_init(cpufreq_gov_userspace_init);
  130. #endif
  131. module_exit(cpufreq_gov_userspace_exit);