domain_governor.c 7.2 KB

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