governor.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 (!strncasecmp(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. }