domain_governor.c 4.2 KB

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