domain_governor.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 = -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. if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
  21. return 0;
  22. constraint_ns *= NSEC_PER_USEC;
  23. if (constraint_ns < *constraint_ns_p || *constraint_ns_p < 0)
  24. *constraint_ns_p = constraint_ns;
  25. return 0;
  26. }
  27. /**
  28. * default_suspend_ok - Default PM domain governor routine to suspend devices.
  29. * @dev: Device to check.
  30. */
  31. static bool default_suspend_ok(struct device *dev)
  32. {
  33. struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
  34. unsigned long flags;
  35. s64 constraint_ns;
  36. dev_dbg(dev, "%s()\n", __func__);
  37. spin_lock_irqsave(&dev->power.lock, flags);
  38. if (!td->constraint_changed) {
  39. bool ret = td->cached_suspend_ok;
  40. spin_unlock_irqrestore(&dev->power.lock, flags);
  41. return ret;
  42. }
  43. td->constraint_changed = false;
  44. td->cached_suspend_ok = false;
  45. td->effective_constraint_ns = -1;
  46. constraint_ns = __dev_pm_qos_read_value(dev);
  47. spin_unlock_irqrestore(&dev->power.lock, flags);
  48. if (constraint_ns == 0)
  49. return false;
  50. if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
  51. constraint_ns = -1;
  52. else
  53. constraint_ns *= NSEC_PER_USEC;
  54. /*
  55. * We can walk the children without any additional locking, because
  56. * they all have been suspended at this point and their
  57. * effective_constraint_ns fields won't be modified in parallel with us.
  58. */
  59. if (!dev->power.ignore_children)
  60. device_for_each_child(dev, &constraint_ns,
  61. dev_update_qos_constraint);
  62. if (constraint_ns < 0) {
  63. /* The children have no constraints. */
  64. td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
  65. td->cached_suspend_ok = true;
  66. } else {
  67. constraint_ns -= td->suspend_latency_ns + td->resume_latency_ns;
  68. if (constraint_ns > 0) {
  69. td->effective_constraint_ns = constraint_ns;
  70. td->cached_suspend_ok = true;
  71. } else {
  72. td->effective_constraint_ns = 0;
  73. }
  74. }
  75. /*
  76. * The children have been suspended already, so we don't need to take
  77. * their suspend latencies into account here.
  78. */
  79. return td->cached_suspend_ok;
  80. }
  81. static bool __default_power_down_ok(struct dev_pm_domain *pd,
  82. unsigned int state)
  83. {
  84. struct generic_pm_domain *genpd = pd_to_genpd(pd);
  85. struct gpd_link *link;
  86. struct pm_domain_data *pdd;
  87. s64 min_off_time_ns;
  88. s64 off_on_time_ns;
  89. off_on_time_ns = genpd->states[state].power_off_latency_ns +
  90. genpd->states[state].power_on_latency_ns;
  91. min_off_time_ns = -1;
  92. /*
  93. * Check if subdomains can be off for enough time.
  94. *
  95. * All subdomains have been powered off already at this point.
  96. */
  97. list_for_each_entry(link, &genpd->master_links, master_node) {
  98. struct generic_pm_domain *sd = link->slave;
  99. s64 sd_max_off_ns = sd->max_off_time_ns;
  100. if (sd_max_off_ns < 0)
  101. continue;
  102. /*
  103. * Check if the subdomain is allowed to be off long enough for
  104. * the current domain to turn off and on (that's how much time
  105. * it will have to wait worst case).
  106. */
  107. if (sd_max_off_ns <= off_on_time_ns)
  108. return false;
  109. if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
  110. min_off_time_ns = sd_max_off_ns;
  111. }
  112. /*
  113. * Check if the devices in the domain can be off enough time.
  114. */
  115. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  116. struct gpd_timing_data *td;
  117. s64 constraint_ns;
  118. /*
  119. * Check if the device is allowed to be off long enough for the
  120. * domain to turn off and on (that's how much time it will
  121. * have to wait worst case).
  122. */
  123. td = &to_gpd_data(pdd)->td;
  124. constraint_ns = td->effective_constraint_ns;
  125. /* default_suspend_ok() need not be called before us. */
  126. if (constraint_ns < 0)
  127. constraint_ns = dev_pm_qos_read_value(pdd->dev);
  128. if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
  129. continue;
  130. constraint_ns *= NSEC_PER_USEC;
  131. /*
  132. * constraint_ns cannot be negative here, because the device has
  133. * been suspended.
  134. */
  135. if (constraint_ns <= off_on_time_ns)
  136. return false;
  137. if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
  138. min_off_time_ns = constraint_ns;
  139. }
  140. /*
  141. * If the computed minimum device off time is negative, there are no
  142. * latency constraints, so the domain can spend arbitrary time in the
  143. * "off" state.
  144. */
  145. if (min_off_time_ns < 0)
  146. return true;
  147. /*
  148. * The difference between the computed minimum subdomain or device off
  149. * time and the time needed to turn the domain on is the maximum
  150. * theoretical time this domain can spend in the "off" state.
  151. */
  152. genpd->max_off_time_ns = min_off_time_ns -
  153. genpd->states[state].power_on_latency_ns;
  154. return true;
  155. }
  156. /**
  157. * default_power_down_ok - Default generic PM domain power off governor routine.
  158. * @pd: PM domain to check.
  159. *
  160. * This routine must be executed under the PM domain's lock.
  161. */
  162. static bool default_power_down_ok(struct dev_pm_domain *pd)
  163. {
  164. struct generic_pm_domain *genpd = pd_to_genpd(pd);
  165. struct gpd_link *link;
  166. if (!genpd->max_off_time_changed)
  167. return genpd->cached_power_down_ok;
  168. /*
  169. * We have to invalidate the cached results for the masters, so
  170. * use the observation that default_power_down_ok() is not
  171. * going to be called for any master until this instance
  172. * returns.
  173. */
  174. list_for_each_entry(link, &genpd->slave_links, slave_node)
  175. link->master->max_off_time_changed = true;
  176. genpd->max_off_time_ns = -1;
  177. genpd->max_off_time_changed = false;
  178. genpd->cached_power_down_ok = true;
  179. genpd->state_idx = genpd->state_count - 1;
  180. /* Find a state to power down to, starting from the deepest. */
  181. while (!__default_power_down_ok(pd, genpd->state_idx)) {
  182. if (genpd->state_idx == 0) {
  183. genpd->cached_power_down_ok = false;
  184. break;
  185. }
  186. genpd->state_idx--;
  187. }
  188. return genpd->cached_power_down_ok;
  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. .suspend_ok = default_suspend_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. .suspend_ok = default_suspend_ok,
  204. };