governor.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/cpu.h>
  11. #include <linux/cpuidle.h>
  12. #include <linux/mutex.h>
  13. #include <linux/module.h>
  14. #include <linux/pm_qos.h>
  15. #include "cpuidle.h"
  16. char param_governor[CPUIDLE_NAME_LEN];
  17. LIST_HEAD(cpuidle_governors);
  18. struct cpuidle_governor *cpuidle_curr_governor;
  19. struct cpuidle_governor *cpuidle_prev_governor;
  20. /**
  21. * cpuidle_find_governor - finds a governor of the specified name
  22. * @str: the name
  23. *
  24. * Must be called with cpuidle_lock acquired.
  25. */
  26. struct cpuidle_governor *cpuidle_find_governor(const char *str)
  27. {
  28. struct cpuidle_governor *gov;
  29. list_for_each_entry(gov, &cpuidle_governors, governor_list)
  30. if (!strncasecmp(str, gov->name, CPUIDLE_NAME_LEN))
  31. return gov;
  32. return NULL;
  33. }
  34. /**
  35. * cpuidle_switch_governor - changes the governor
  36. * @gov: the new target governor
  37. * Must be called with cpuidle_lock acquired.
  38. */
  39. int cpuidle_switch_governor(struct cpuidle_governor *gov)
  40. {
  41. struct cpuidle_device *dev;
  42. if (!gov)
  43. return -EINVAL;
  44. if (gov == cpuidle_curr_governor)
  45. return 0;
  46. cpuidle_uninstall_idle_handler();
  47. if (cpuidle_curr_governor) {
  48. list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
  49. cpuidle_disable_device(dev);
  50. }
  51. cpuidle_curr_governor = gov;
  52. if (gov) {
  53. list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
  54. cpuidle_enable_device(dev);
  55. cpuidle_install_idle_handler();
  56. printk(KERN_INFO "cpuidle: using governor %s\n", gov->name);
  57. }
  58. return 0;
  59. }
  60. /**
  61. * cpuidle_register_governor - registers a governor
  62. * @gov: the governor
  63. */
  64. int cpuidle_register_governor(struct cpuidle_governor *gov)
  65. {
  66. int ret = -EEXIST;
  67. if (!gov || !gov->select)
  68. return -EINVAL;
  69. if (cpuidle_disabled())
  70. return -ENODEV;
  71. mutex_lock(&cpuidle_lock);
  72. if (cpuidle_find_governor(gov->name) == NULL) {
  73. ret = 0;
  74. list_add_tail(&gov->governor_list, &cpuidle_governors);
  75. if (!cpuidle_curr_governor ||
  76. !strncasecmp(param_governor, gov->name, CPUIDLE_NAME_LEN) ||
  77. (cpuidle_curr_governor->rating < gov->rating &&
  78. strncasecmp(param_governor, cpuidle_curr_governor->name,
  79. CPUIDLE_NAME_LEN)))
  80. cpuidle_switch_governor(gov);
  81. }
  82. mutex_unlock(&cpuidle_lock);
  83. return ret;
  84. }
  85. EXPORT_SYMBOL_GPL(cpuidle_register_governor);
  86. /**
  87. * cpuidle_governor_latency_req - Compute a latency constraint for CPU
  88. * @cpu: Target CPU
  89. */
  90. s64 cpuidle_governor_latency_req(unsigned int cpu)
  91. {
  92. struct device *device = get_cpu_device(cpu);
  93. int device_req = dev_pm_qos_raw_resume_latency(device);
  94. int global_req = cpu_latency_qos_limit();
  95. if (device_req > global_req)
  96. device_req = global_req;
  97. return (s64)device_req * NSEC_PER_USEC;
  98. }
  99. EXPORT_SYMBOL_GPL(cpuidle_governor_latency_req);