governor.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/cpuidle.h>
  12. #include "cpuidle.h"
  13. LIST_HEAD(cpuidle_governors);
  14. struct cpuidle_governor *cpuidle_curr_governor;
  15. /**
  16. * __cpuidle_find_governor - finds a governor of the specified name
  17. * @str: the name
  18. *
  19. * Must be called with cpuidle_lock acquired.
  20. */
  21. static struct cpuidle_governor * __cpuidle_find_governor(const char *str)
  22. {
  23. struct cpuidle_governor *gov;
  24. list_for_each_entry(gov, &cpuidle_governors, governor_list)
  25. if (!strncasecmp(str, gov->name, CPUIDLE_NAME_LEN))
  26. return gov;
  27. return NULL;
  28. }
  29. /**
  30. * cpuidle_switch_governor - changes the governor
  31. * @gov: the new target governor
  32. *
  33. * NOTE: "gov" can be NULL to specify disabled
  34. * Must be called with cpuidle_lock acquired.
  35. */
  36. int cpuidle_switch_governor(struct cpuidle_governor *gov)
  37. {
  38. struct cpuidle_device *dev;
  39. if (gov == cpuidle_curr_governor)
  40. return 0;
  41. cpuidle_uninstall_idle_handler();
  42. if (cpuidle_curr_governor) {
  43. list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
  44. cpuidle_disable_device(dev);
  45. }
  46. cpuidle_curr_governor = gov;
  47. if (gov) {
  48. list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
  49. cpuidle_enable_device(dev);
  50. cpuidle_install_idle_handler();
  51. printk(KERN_INFO "cpuidle: using governor %s\n", gov->name);
  52. }
  53. return 0;
  54. }
  55. /**
  56. * cpuidle_register_governor - registers a governor
  57. * @gov: the governor
  58. */
  59. int cpuidle_register_governor(struct cpuidle_governor *gov)
  60. {
  61. int ret = -EEXIST;
  62. if (!gov || !gov->select)
  63. return -EINVAL;
  64. if (cpuidle_disabled())
  65. return -ENODEV;
  66. mutex_lock(&cpuidle_lock);
  67. if (__cpuidle_find_governor(gov->name) == NULL) {
  68. ret = 0;
  69. list_add_tail(&gov->governor_list, &cpuidle_governors);
  70. if (!cpuidle_curr_governor ||
  71. cpuidle_curr_governor->rating < gov->rating)
  72. cpuidle_switch_governor(gov);
  73. }
  74. mutex_unlock(&cpuidle_lock);
  75. return ret;
  76. }