cpufreq_userspace.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. static DEFINE_PER_CPU(unsigned int, cpu_is_managed);
  18. static DEFINE_MUTEX(userspace_mutex);
  19. /**
  20. * cpufreq_set - set the CPU frequency
  21. * @policy: pointer to policy struct where freq is being set
  22. * @freq: target frequency in kHz
  23. *
  24. * Sets the CPU frequency to freq.
  25. */
  26. static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
  27. {
  28. int ret = -EINVAL;
  29. pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
  30. mutex_lock(&userspace_mutex);
  31. if (!per_cpu(cpu_is_managed, policy->cpu))
  32. goto err;
  33. ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
  34. err:
  35. mutex_unlock(&userspace_mutex);
  36. return ret;
  37. }
  38. static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
  39. {
  40. return sprintf(buf, "%u\n", policy->cur);
  41. }
  42. static int cpufreq_governor_userspace(struct cpufreq_policy *policy,
  43. unsigned int event)
  44. {
  45. unsigned int cpu = policy->cpu;
  46. int rc = 0;
  47. switch (event) {
  48. case CPUFREQ_GOV_START:
  49. BUG_ON(!policy->cur);
  50. pr_debug("started managing cpu %u\n", cpu);
  51. mutex_lock(&userspace_mutex);
  52. per_cpu(cpu_is_managed, cpu) = 1;
  53. mutex_unlock(&userspace_mutex);
  54. break;
  55. case CPUFREQ_GOV_STOP:
  56. pr_debug("managing cpu %u stopped\n", cpu);
  57. mutex_lock(&userspace_mutex);
  58. per_cpu(cpu_is_managed, cpu) = 0;
  59. mutex_unlock(&userspace_mutex);
  60. break;
  61. case CPUFREQ_GOV_LIMITS:
  62. mutex_lock(&userspace_mutex);
  63. pr_debug("limit event for cpu %u: %u - %u kHz, currently %u kHz\n",
  64. cpu, policy->min, policy->max,
  65. policy->cur);
  66. if (policy->max < policy->cur)
  67. __cpufreq_driver_target(policy, policy->max,
  68. CPUFREQ_RELATION_H);
  69. else if (policy->min > policy->cur)
  70. __cpufreq_driver_target(policy, policy->min,
  71. CPUFREQ_RELATION_L);
  72. mutex_unlock(&userspace_mutex);
  73. break;
  74. }
  75. return rc;
  76. }
  77. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
  78. static
  79. #endif
  80. struct cpufreq_governor cpufreq_gov_userspace = {
  81. .name = "userspace",
  82. .governor = cpufreq_governor_userspace,
  83. .store_setspeed = cpufreq_set,
  84. .show_setspeed = show_speed,
  85. .owner = THIS_MODULE,
  86. };
  87. static int __init cpufreq_gov_userspace_init(void)
  88. {
  89. return cpufreq_register_governor(&cpufreq_gov_userspace);
  90. }
  91. static void __exit cpufreq_gov_userspace_exit(void)
  92. {
  93. cpufreq_unregister_governor(&cpufreq_gov_userspace);
  94. }
  95. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>, "
  96. "Russell King <rmk@arm.linux.org.uk>");
  97. MODULE_DESCRIPTION("CPUfreq policy governor 'userspace'");
  98. MODULE_LICENSE("GPL");
  99. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE
  100. fs_initcall(cpufreq_gov_userspace_init);
  101. #else
  102. module_init(cpufreq_gov_userspace_init);
  103. #endif
  104. module_exit(cpufreq_gov_userspace_exit);