governor.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * governor.c - governor support
  3. *
  4. * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  5. * Shaohua Li <shaohua.li@intel.com>
  6. * Adam Belay <abelay@novell.com>
  7. *
  8. * This code is licenced under the GPL.
  9. */
  10. #include <linux/mutex.h>
  11. #include <linux/module.h>
  12. #include <linux/cpuidle.h>
  13. #include "cpuidle.h"
  14. LIST_HEAD(cpuidle_governors);
  15. struct cpuidle_governor *cpuidle_curr_governor;
  16. /**
  17. * __cpuidle_find_governor - finds a governor of the specified name
  18. * @str: the name
  19. *
  20. * Must be called with cpuidle_lock acquired.
  21. */
  22. static struct cpuidle_governor * __cpuidle_find_governor(const char *str)
  23. {
  24. struct cpuidle_governor *gov;
  25. list_for_each_entry(gov, &cpuidle_governors, governor_list)
  26. if (!strnicmp(str, gov->name, CPUIDLE_NAME_LEN))
  27. return gov;
  28. return NULL;
  29. }
  30. /**
  31. * cpuidle_switch_governor - changes the governor
  32. * @gov: the new target governor
  33. *
  34. * NOTE: "gov" can be NULL to specify disabled
  35. * Must be called with cpuidle_lock acquired.
  36. */
  37. int cpuidle_switch_governor(struct cpuidle_governor *gov)
  38. {
  39. struct cpuidle_device *dev;
  40. if (gov == cpuidle_curr_governor)
  41. return 0;
  42. cpuidle_uninstall_idle_handler();
  43. if (cpuidle_curr_governor) {
  44. list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
  45. cpuidle_disable_device(dev);
  46. module_put(cpuidle_curr_governor->owner);
  47. }
  48. cpuidle_curr_governor = gov;
  49. if (gov) {
  50. if (!try_module_get(cpuidle_curr_governor->owner))
  51. return -EINVAL;
  52. list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
  53. cpuidle_enable_device(dev);
  54. cpuidle_install_idle_handler();
  55. printk(KERN_INFO "cpuidle: using governor %s\n", gov->name);
  56. }
  57. return 0;
  58. }
  59. /**
  60. * cpuidle_register_governor - registers a governor
  61. * @gov: the governor
  62. */
  63. int cpuidle_register_governor(struct cpuidle_governor *gov)
  64. {
  65. int ret = -EEXIST;
  66. if (!gov || !gov->select)
  67. return -EINVAL;
  68. mutex_lock(&cpuidle_lock);
  69. if (__cpuidle_find_governor(gov->name) == NULL) {
  70. ret = 0;
  71. list_add_tail(&gov->governor_list, &cpuidle_governors);
  72. if (!cpuidle_curr_governor ||
  73. cpuidle_curr_governor->rating < gov->rating)
  74. cpuidle_switch_governor(gov);
  75. }
  76. mutex_unlock(&cpuidle_lock);
  77. return ret;
  78. }
  79. /**
  80. * cpuidle_replace_governor - find a replacement governor
  81. * @exclude_rating: the rating that will be skipped while looking for
  82. * new governor.
  83. */
  84. static struct cpuidle_governor *cpuidle_replace_governor(int exclude_rating)
  85. {
  86. struct cpuidle_governor *gov;
  87. struct cpuidle_governor *ret_gov = NULL;
  88. unsigned int max_rating = 0;
  89. list_for_each_entry(gov, &cpuidle_governors, governor_list) {
  90. if (gov->rating == exclude_rating)
  91. continue;
  92. if (gov->rating > max_rating) {
  93. max_rating = gov->rating;
  94. ret_gov = gov;
  95. }
  96. }
  97. return ret_gov;
  98. }
  99. /**
  100. * cpuidle_unregister_governor - unregisters a governor
  101. * @gov: the governor
  102. */
  103. void cpuidle_unregister_governor(struct cpuidle_governor *gov)
  104. {
  105. if (!gov)
  106. return;
  107. mutex_lock(&cpuidle_lock);
  108. if (gov == cpuidle_curr_governor) {
  109. struct cpuidle_governor *new_gov;
  110. new_gov = cpuidle_replace_governor(gov->rating);
  111. cpuidle_switch_governor(new_gov);
  112. }
  113. list_del(&gov->governor_list);
  114. mutex_unlock(&cpuidle_lock);
  115. }