domain_governor.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. off_on_time_ns = genpd->power_off_latency_ns +
  80. genpd->power_on_latency_ns;
  81. /*
  82. * It doesn't make sense to remove power from the domain if saving
  83. * the state of all devices in it and the power off/power on operations
  84. * take too much time.
  85. *
  86. * All devices in this domain have been stopped already at this point.
  87. */
  88. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  89. if (pdd->dev->driver)
  90. off_on_time_ns +=
  91. to_gpd_data(pdd)->td.save_state_latency_ns;
  92. }
  93. /*
  94. * Check if subdomains can be off for enough time.
  95. *
  96. * All subdomains have been powered off already at this point.
  97. */
  98. list_for_each_entry(link, &genpd->master_links, master_node) {
  99. struct generic_pm_domain *sd = link->slave;
  100. s64 sd_max_off_ns = sd->max_off_time_ns;
  101. if (sd_max_off_ns < 0)
  102. continue;
  103. /*
  104. * Check if the subdomain is allowed to be off long enough for
  105. * the current domain to turn off and on (that's how much time
  106. * it will have to wait worst case).
  107. */
  108. if (sd_max_off_ns <= off_on_time_ns)
  109. return false;
  110. }
  111. /*
  112. * Check if the devices in the domain can be off enough time.
  113. */
  114. min_dev_off_time_ns = -1;
  115. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  116. struct gpd_timing_data *td;
  117. s64 constraint_ns;
  118. if (!pdd->dev->driver)
  119. continue;
  120. /*
  121. * Check if the device is allowed to be off long enough for the
  122. * domain to turn off and on (that's how much time it will
  123. * have to wait worst case).
  124. */
  125. td = &to_gpd_data(pdd)->td;
  126. constraint_ns = td->effective_constraint_ns;
  127. /* default_stop_ok() need not be called before us. */
  128. if (constraint_ns < 0) {
  129. constraint_ns = dev_pm_qos_read_value(pdd->dev);
  130. constraint_ns *= NSEC_PER_USEC;
  131. }
  132. if (constraint_ns == 0)
  133. continue;
  134. /*
  135. * constraint_ns cannot be negative here, because the device has
  136. * been suspended.
  137. */
  138. constraint_ns -= td->restore_state_latency_ns;
  139. if (constraint_ns <= off_on_time_ns)
  140. return false;
  141. if (min_dev_off_time_ns > constraint_ns
  142. || min_dev_off_time_ns < 0)
  143. min_dev_off_time_ns = constraint_ns;
  144. }
  145. /*
  146. * If the computed minimum device off time is negative, there are no
  147. * latency constraints, so the domain can spend arbitrary time in the
  148. * "off" state.
  149. */
  150. if (min_dev_off_time_ns < 0)
  151. return true;
  152. /*
  153. * The difference between the computed minimum device off time and the
  154. * time needed to turn the domain on is the maximum theoretical time
  155. * this domain can spend in the "off" state.
  156. */
  157. genpd->max_off_time_ns = min_dev_off_time_ns -
  158. genpd->power_on_latency_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. };