domain_governor.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * drivers/base/power/domain_governor.c - Governors for device PM domains.
  3. *
  4. * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
  5. *
  6. * This file is released under the GPLv2.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pm_domain.h>
  11. #include <linux/pm_qos.h>
  12. #include <linux/hrtimer.h>
  13. #ifdef CONFIG_PM_RUNTIME
  14. /**
  15. * default_stop_ok - Default PM domain governor routine for stopping devices.
  16. * @dev: Device to check.
  17. */
  18. bool default_stop_ok(struct device *dev)
  19. {
  20. struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
  21. dev_dbg(dev, "%s()\n", __func__);
  22. if (dev->power.max_time_suspended_ns < 0 || td->break_even_ns == 0)
  23. return true;
  24. return td->stop_latency_ns + td->start_latency_ns < td->break_even_ns
  25. && td->break_even_ns < dev->power.max_time_suspended_ns;
  26. }
  27. /**
  28. * default_power_down_ok - Default generic PM domain power off governor routine.
  29. * @pd: PM domain to check.
  30. *
  31. * This routine must be executed under the PM domain's lock.
  32. */
  33. static bool default_power_down_ok(struct dev_pm_domain *pd)
  34. {
  35. struct generic_pm_domain *genpd = pd_to_genpd(pd);
  36. struct gpd_link *link;
  37. struct pm_domain_data *pdd;
  38. s64 min_dev_off_time_ns;
  39. s64 off_on_time_ns;
  40. ktime_t time_now = ktime_get();
  41. off_on_time_ns = genpd->power_off_latency_ns +
  42. genpd->power_on_latency_ns;
  43. /*
  44. * It doesn't make sense to remove power from the domain if saving
  45. * the state of all devices in it and the power off/power on operations
  46. * take too much time.
  47. *
  48. * All devices in this domain have been stopped already at this point.
  49. */
  50. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  51. if (pdd->dev->driver)
  52. off_on_time_ns +=
  53. to_gpd_data(pdd)->td.save_state_latency_ns;
  54. }
  55. /*
  56. * Check if subdomains can be off for enough time.
  57. *
  58. * All subdomains have been powered off already at this point.
  59. */
  60. list_for_each_entry(link, &genpd->master_links, master_node) {
  61. struct generic_pm_domain *sd = link->slave;
  62. s64 sd_max_off_ns = sd->max_off_time_ns;
  63. if (sd_max_off_ns < 0)
  64. continue;
  65. sd_max_off_ns -= ktime_to_ns(ktime_sub(time_now,
  66. sd->power_off_time));
  67. /*
  68. * Check if the subdomain is allowed to be off long enough for
  69. * the current domain to turn off and on (that's how much time
  70. * it will have to wait worst case).
  71. */
  72. if (sd_max_off_ns <= off_on_time_ns)
  73. return false;
  74. }
  75. /*
  76. * Check if the devices in the domain can be off enough time.
  77. */
  78. min_dev_off_time_ns = -1;
  79. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  80. struct gpd_timing_data *td;
  81. struct device *dev = pdd->dev;
  82. s64 dev_off_time_ns;
  83. if (!dev->driver || dev->power.max_time_suspended_ns < 0)
  84. continue;
  85. td = &to_gpd_data(pdd)->td;
  86. dev_off_time_ns = dev->power.max_time_suspended_ns -
  87. (td->start_latency_ns + td->restore_state_latency_ns +
  88. ktime_to_ns(ktime_sub(time_now,
  89. dev->power.suspend_time)));
  90. if (dev_off_time_ns <= off_on_time_ns)
  91. return false;
  92. if (min_dev_off_time_ns > dev_off_time_ns
  93. || min_dev_off_time_ns < 0)
  94. min_dev_off_time_ns = dev_off_time_ns;
  95. }
  96. if (min_dev_off_time_ns < 0) {
  97. /*
  98. * There are no latency constraints, so the domain can spend
  99. * arbitrary time in the "off" state.
  100. */
  101. genpd->max_off_time_ns = -1;
  102. return true;
  103. }
  104. /*
  105. * The difference between the computed minimum delta and the time needed
  106. * to turn the domain on is the maximum theoretical time this domain can
  107. * spend in the "off" state.
  108. */
  109. min_dev_off_time_ns -= genpd->power_on_latency_ns;
  110. /*
  111. * If the difference between the computed minimum delta and the time
  112. * needed to turn the domain off and back on on is smaller than the
  113. * domain's power break even time, removing power from the domain is not
  114. * worth it.
  115. */
  116. if (genpd->break_even_ns >
  117. min_dev_off_time_ns - genpd->power_off_latency_ns)
  118. return false;
  119. genpd->max_off_time_ns = min_dev_off_time_ns;
  120. return true;
  121. }
  122. static bool always_on_power_down_ok(struct dev_pm_domain *domain)
  123. {
  124. return false;
  125. }
  126. #else /* !CONFIG_PM_RUNTIME */
  127. bool default_stop_ok(struct device *dev)
  128. {
  129. return false;
  130. }
  131. #define default_power_down_ok NULL
  132. #define always_on_power_down_ok NULL
  133. #endif /* !CONFIG_PM_RUNTIME */
  134. struct dev_power_governor simple_qos_governor = {
  135. .stop_ok = default_stop_ok,
  136. .power_down_ok = default_power_down_ok,
  137. };
  138. /**
  139. * pm_genpd_gov_always_on - A governor implementing an always-on policy
  140. */
  141. struct dev_power_governor pm_domain_always_on_gov = {
  142. .power_down_ok = always_on_power_down_ok,
  143. .stop_ok = default_stop_ok,
  144. };