domain_governor.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. static int dev_update_qos_constraint(struct device *dev, void *data)
  15. {
  16. s64 *constraint_ns_p = data;
  17. s32 constraint_ns = -1;
  18. if (dev->power.subsys_data && dev->power.subsys_data->domain_data)
  19. constraint_ns = dev_gpd_data(dev)->td.effective_constraint_ns;
  20. if (constraint_ns < 0) {
  21. constraint_ns = dev_pm_qos_read_value(dev);
  22. constraint_ns *= NSEC_PER_USEC;
  23. }
  24. if (constraint_ns == 0)
  25. return 0;
  26. /*
  27. * constraint_ns cannot be negative here, because the device has been
  28. * suspended.
  29. */
  30. if (constraint_ns < *constraint_ns_p || *constraint_ns_p == 0)
  31. *constraint_ns_p = constraint_ns;
  32. return 0;
  33. }
  34. /**
  35. * default_stop_ok - Default PM domain governor routine for stopping devices.
  36. * @dev: Device to check.
  37. */
  38. bool default_stop_ok(struct device *dev)
  39. {
  40. struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
  41. s64 constraint_ns;
  42. dev_dbg(dev, "%s()\n", __func__);
  43. constraint_ns = dev_pm_qos_read_value(dev);
  44. if (constraint_ns < 0)
  45. return false;
  46. constraint_ns *= NSEC_PER_USEC;
  47. /*
  48. * We can walk the children without any additional locking, because
  49. * they all have been suspended at this point.
  50. */
  51. if (!dev->power.ignore_children)
  52. device_for_each_child(dev, &constraint_ns,
  53. dev_update_qos_constraint);
  54. if (constraint_ns > 0) {
  55. constraint_ns -= td->start_latency_ns;
  56. if (constraint_ns == 0)
  57. return false;
  58. }
  59. td->effective_constraint_ns = constraint_ns;
  60. /*
  61. * The children have been suspended already, so we don't need to take
  62. * their stop latencies into account here.
  63. */
  64. return constraint_ns > td->stop_latency_ns || constraint_ns == 0;
  65. }
  66. /**
  67. * default_power_down_ok - Default generic PM domain power off governor routine.
  68. * @pd: PM domain to check.
  69. *
  70. * This routine must be executed under the PM domain's lock.
  71. */
  72. static bool default_power_down_ok(struct dev_pm_domain *pd)
  73. {
  74. struct generic_pm_domain *genpd = pd_to_genpd(pd);
  75. struct gpd_link *link;
  76. struct pm_domain_data *pdd;
  77. s64 min_dev_off_time_ns;
  78. s64 off_on_time_ns;
  79. ktime_t time_now = ktime_get();
  80. off_on_time_ns = genpd->power_off_latency_ns +
  81. genpd->power_on_latency_ns;
  82. /*
  83. * It doesn't make sense to remove power from the domain if saving
  84. * the state of all devices in it and the power off/power on operations
  85. * take too much time.
  86. *
  87. * All devices in this domain have been stopped already at this point.
  88. */
  89. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  90. if (pdd->dev->driver)
  91. off_on_time_ns +=
  92. to_gpd_data(pdd)->td.save_state_latency_ns;
  93. }
  94. /*
  95. * Check if subdomains can be off for enough time.
  96. *
  97. * All subdomains have been powered off already at this point.
  98. */
  99. list_for_each_entry(link, &genpd->master_links, master_node) {
  100. struct generic_pm_domain *sd = link->slave;
  101. s64 sd_max_off_ns = sd->max_off_time_ns;
  102. if (sd_max_off_ns < 0)
  103. continue;
  104. sd_max_off_ns -= ktime_to_ns(ktime_sub(time_now,
  105. sd->power_off_time));
  106. /*
  107. * Check if the subdomain is allowed to be off long enough for
  108. * the current domain to turn off and on (that's how much time
  109. * it will have to wait worst case).
  110. */
  111. if (sd_max_off_ns <= off_on_time_ns)
  112. return false;
  113. }
  114. /*
  115. * Check if the devices in the domain can be off enough time.
  116. */
  117. min_dev_off_time_ns = -1;
  118. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  119. struct gpd_timing_data *td;
  120. struct device *dev = pdd->dev;
  121. s64 dev_off_time_ns;
  122. if (!dev->driver || dev->power.max_time_suspended_ns < 0)
  123. continue;
  124. td = &to_gpd_data(pdd)->td;
  125. dev_off_time_ns = dev->power.max_time_suspended_ns -
  126. (td->start_latency_ns + td->restore_state_latency_ns +
  127. ktime_to_ns(ktime_sub(time_now,
  128. dev->power.suspend_time)));
  129. if (dev_off_time_ns <= off_on_time_ns)
  130. return false;
  131. if (min_dev_off_time_ns > dev_off_time_ns
  132. || min_dev_off_time_ns < 0)
  133. min_dev_off_time_ns = dev_off_time_ns;
  134. }
  135. if (min_dev_off_time_ns < 0) {
  136. /*
  137. * There are no latency constraints, so the domain can spend
  138. * arbitrary time in the "off" state.
  139. */
  140. genpd->max_off_time_ns = -1;
  141. return true;
  142. }
  143. /*
  144. * The difference between the computed minimum delta and the time needed
  145. * to turn the domain on is the maximum theoretical time this domain can
  146. * spend in the "off" state.
  147. */
  148. min_dev_off_time_ns -= genpd->power_on_latency_ns;
  149. /*
  150. * If the difference between the computed minimum delta and the time
  151. * needed to turn the domain off and back on on is smaller than the
  152. * domain's power break even time, removing power from the domain is not
  153. * worth it.
  154. */
  155. if (genpd->break_even_ns >
  156. min_dev_off_time_ns - genpd->power_off_latency_ns)
  157. return false;
  158. genpd->max_off_time_ns = min_dev_off_time_ns;
  159. return true;
  160. }
  161. static bool always_on_power_down_ok(struct dev_pm_domain *domain)
  162. {
  163. return false;
  164. }
  165. #else /* !CONFIG_PM_RUNTIME */
  166. bool default_stop_ok(struct device *dev)
  167. {
  168. return false;
  169. }
  170. #define default_power_down_ok NULL
  171. #define always_on_power_down_ok NULL
  172. #endif /* !CONFIG_PM_RUNTIME */
  173. struct dev_power_governor simple_qos_governor = {
  174. .stop_ok = default_stop_ok,
  175. .power_down_ok = default_power_down_ok,
  176. };
  177. /**
  178. * pm_genpd_gov_always_on - A governor implementing an always-on policy
  179. */
  180. struct dev_power_governor pm_domain_always_on_gov = {
  181. .power_down_ok = always_on_power_down_ok,
  182. .stop_ok = default_stop_ok,
  183. };