domain_governor.c 6.5 KB

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