domain_governor.c 6.5 KB

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