domain_governor.c 6.3 KB

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