governor.c 2.5 KB

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