PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/cpuidle/governor.c

https://bitbucket.org/shawnjgoff/linux-uclinux-dist
C | 141 lines | 82 code | 24 blank | 35 comment | 18 complexity | 99576afef7dee6de5c0c43b7c00f1c4d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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. if (cpuidle_disabled())
  69. return -ENODEV;
  70. mutex_lock(&cpuidle_lock);
  71. if (__cpuidle_find_governor(gov->name) == NULL) {
  72. ret = 0;
  73. list_add_tail(&gov->governor_list, &cpuidle_governors);
  74. if (!cpuidle_curr_governor ||
  75. cpuidle_curr_governor->rating < gov->rating)
  76. cpuidle_switch_governor(gov);
  77. }
  78. mutex_unlock(&cpuidle_lock);
  79. return ret;
  80. }
  81. /**
  82. * cpuidle_replace_governor - find a replacement governor
  83. * @exclude_rating: the rating that will be skipped while looking for
  84. * new governor.
  85. */
  86. static struct cpuidle_governor *cpuidle_replace_governor(int exclude_rating)
  87. {
  88. struct cpuidle_governor *gov;
  89. struct cpuidle_governor *ret_gov = NULL;
  90. unsigned int max_rating = 0;
  91. list_for_each_entry(gov, &cpuidle_governors, governor_list) {
  92. if (gov->rating == exclude_rating)
  93. continue;
  94. if (gov->rating > max_rating) {
  95. max_rating = gov->rating;
  96. ret_gov = gov;
  97. }
  98. }
  99. return ret_gov;
  100. }
  101. /**
  102. * cpuidle_unregister_governor - unregisters a governor
  103. * @gov: the governor
  104. */
  105. void cpuidle_unregister_governor(struct cpuidle_governor *gov)
  106. {
  107. if (!gov)
  108. return;
  109. mutex_lock(&cpuidle_lock);
  110. if (gov == cpuidle_curr_governor) {
  111. struct cpuidle_governor *new_gov;
  112. new_gov = cpuidle_replace_governor(gov->rating);
  113. cpuidle_switch_governor(new_gov);
  114. }
  115. list_del(&gov->governor_list);
  116. mutex_unlock(&cpuidle_lock);
  117. }