governor.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * governor.c - governor support
  3. *
  4. * (C) 2006-2007 Venkatesh Pallipadi <[email protected]>
  5. * Shaohua Li <[email protected]>
  6. * Adam Belay <[email protected]>
  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. list_for_each_entry(dev, &cpuidle_detected_devices, device_list)
  53. cpuidle_enable_device(dev);
  54. cpuidle_install_idle_handler();
  55. pr_info("cpuidle: using governor %s\n", gov->name);
  56. return 0;
  57. }
  58. /**
  59. * cpuidle_register_governor - registers a governor
  60. * @gov: the governor
  61. */
  62. int cpuidle_register_governor(struct cpuidle_governor *gov)
  63. {
  64. int ret = -EEXIST;
  65. if (!gov || !gov->select)
  66. return -EINVAL;
  67. if (cpuidle_disabled())
  68. return -ENODEV;
  69. mutex_lock(&cpuidle_lock);
  70. if (cpuidle_find_governor(gov->name) == NULL) {
  71. ret = 0;
  72. list_add_tail(&gov->governor_list, &cpuidle_governors);
  73. if (!cpuidle_curr_governor ||
  74. !strncasecmp(param_governor, gov->name, CPUIDLE_NAME_LEN) ||
  75. (cpuidle_curr_governor->rating < gov->rating &&
  76. strncasecmp(param_governor, cpuidle_curr_governor->name,
  77. CPUIDLE_NAME_LEN)))
  78. cpuidle_switch_governor(gov);
  79. }
  80. mutex_unlock(&cpuidle_lock);
  81. return ret;
  82. }
  83. EXPORT_SYMBOL_GPL(cpuidle_register_governor);
  84. /**
  85. * cpuidle_governor_latency_req - Compute a latency constraint for CPU
  86. * @cpu: Target CPU
  87. */
  88. s64 cpuidle_governor_latency_req(unsigned int cpu)
  89. {
  90. struct device *device = get_cpu_device(cpu);
  91. int device_req = dev_pm_qos_raw_resume_latency(device);
  92. int global_req = cpu_latency_qos_limit();
  93. if (device_req > global_req)
  94. device_req = global_req;
  95. return (s64)device_req * NSEC_PER_USEC;
  96. }
  97. EXPORT_SYMBOL_GPL(cpuidle_governor_latency_req);