domain_governor.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/power/domain_governor.c - Governors for device PM domains.
  4. *
  5. * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/pm_domain.h>
  9. #include <linux/pm_qos.h>
  10. #include <linux/hrtimer.h>
  11. #include <linux/cpuidle.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/ktime.h>
  14. #include <trace/hooks/pm_domain.h>
  15. static int dev_update_qos_constraint(struct device *dev, void *data)
  16. {
  17. s64 *constraint_ns_p = data;
  18. s64 constraint_ns;
  19. if (dev->power.subsys_data && dev->power.subsys_data->domain_data) {
  20. /*
  21. * Only take suspend-time QoS constraints of devices into
  22. * account, because constraints updated after the device has
  23. * been suspended are not guaranteed to be taken into account
  24. * anyway. In order for them to take effect, the device has to
  25. * be resumed and suspended again.
  26. */
  27. constraint_ns = dev_gpd_data(dev)->td.effective_constraint_ns;
  28. } else {
  29. /*
  30. * The child is not in a domain and there's no info on its
  31. * suspend/resume latencies, so assume them to be negligible and
  32. * take its current PM QoS constraint (that's the only thing
  33. * known at this point anyway).
  34. */
  35. constraint_ns = dev_pm_qos_read_value(dev, DEV_PM_QOS_RESUME_LATENCY);
  36. constraint_ns *= NSEC_PER_USEC;
  37. }
  38. if (constraint_ns < *constraint_ns_p)
  39. *constraint_ns_p = constraint_ns;
  40. return 0;
  41. }
  42. /**
  43. * default_suspend_ok - Default PM domain governor routine to suspend devices.
  44. * @dev: Device to check.
  45. */
  46. static bool default_suspend_ok(struct device *dev)
  47. {
  48. struct gpd_timing_data *td = &dev_gpd_data(dev)->td;
  49. unsigned long flags;
  50. s64 constraint_ns;
  51. dev_dbg(dev, "%s()\n", __func__);
  52. spin_lock_irqsave(&dev->power.lock, flags);
  53. if (!td->constraint_changed) {
  54. bool ret = td->cached_suspend_ok;
  55. spin_unlock_irqrestore(&dev->power.lock, flags);
  56. return ret;
  57. }
  58. td->constraint_changed = false;
  59. td->cached_suspend_ok = false;
  60. td->effective_constraint_ns = 0;
  61. constraint_ns = __dev_pm_qos_resume_latency(dev);
  62. spin_unlock_irqrestore(&dev->power.lock, flags);
  63. if (constraint_ns == 0)
  64. return false;
  65. constraint_ns *= NSEC_PER_USEC;
  66. /*
  67. * We can walk the children without any additional locking, because
  68. * they all have been suspended at this point and their
  69. * effective_constraint_ns fields won't be modified in parallel with us.
  70. */
  71. if (!dev->power.ignore_children)
  72. device_for_each_child(dev, &constraint_ns,
  73. dev_update_qos_constraint);
  74. if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) {
  75. /* "No restriction", so the device is allowed to suspend. */
  76. td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
  77. td->cached_suspend_ok = true;
  78. } else if (constraint_ns == 0) {
  79. /*
  80. * This triggers if one of the children that don't belong to a
  81. * domain has a zero PM QoS constraint and it's better not to
  82. * suspend then. effective_constraint_ns is zero already and
  83. * cached_suspend_ok is false, so bail out.
  84. */
  85. return false;
  86. } else {
  87. constraint_ns -= td->suspend_latency_ns +
  88. td->resume_latency_ns;
  89. /*
  90. * effective_constraint_ns is zero already and cached_suspend_ok
  91. * is false, so if the computed value is not positive, return
  92. * right away.
  93. */
  94. if (constraint_ns <= 0)
  95. return false;
  96. td->effective_constraint_ns = constraint_ns;
  97. td->cached_suspend_ok = true;
  98. }
  99. /*
  100. * The children have been suspended already, so we don't need to take
  101. * their suspend latencies into account here.
  102. */
  103. return td->cached_suspend_ok;
  104. }
  105. static void update_domain_next_wakeup(struct generic_pm_domain *genpd, ktime_t now)
  106. {
  107. ktime_t domain_wakeup = KTIME_MAX;
  108. ktime_t next_wakeup;
  109. struct pm_domain_data *pdd;
  110. struct gpd_link *link;
  111. if (!(genpd->flags & GENPD_FLAG_MIN_RESIDENCY))
  112. return;
  113. /*
  114. * Devices that have a predictable wakeup pattern, may specify
  115. * their next wakeup. Let's find the next wakeup from all the
  116. * devices attached to this domain and from all the sub-domains.
  117. * It is possible that component's a next wakeup may have become
  118. * stale when we read that here. We will ignore to ensure the domain
  119. * is able to enter its optimal idle state.
  120. */
  121. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  122. next_wakeup = to_gpd_data(pdd)->next_wakeup;
  123. if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
  124. if (ktime_before(next_wakeup, domain_wakeup))
  125. domain_wakeup = next_wakeup;
  126. }
  127. list_for_each_entry(link, &genpd->parent_links, parent_node) {
  128. next_wakeup = link->child->next_wakeup;
  129. if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
  130. if (ktime_before(next_wakeup, domain_wakeup))
  131. domain_wakeup = next_wakeup;
  132. }
  133. genpd->next_wakeup = domain_wakeup;
  134. }
  135. static bool next_wakeup_allows_state(struct generic_pm_domain *genpd,
  136. unsigned int state, ktime_t now)
  137. {
  138. ktime_t domain_wakeup = genpd->next_wakeup;
  139. s64 idle_time_ns, min_sleep_ns;
  140. min_sleep_ns = genpd->states[state].power_off_latency_ns +
  141. genpd->states[state].residency_ns;
  142. idle_time_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
  143. return idle_time_ns >= min_sleep_ns;
  144. }
  145. static bool __default_power_down_ok(struct dev_pm_domain *pd,
  146. unsigned int state)
  147. {
  148. struct generic_pm_domain *genpd = pd_to_genpd(pd);
  149. struct gpd_link *link;
  150. struct pm_domain_data *pdd;
  151. s64 min_off_time_ns;
  152. s64 off_on_time_ns;
  153. bool allow = true;
  154. trace_android_vh_allow_domain_state(genpd, state, &allow);
  155. if (!allow)
  156. return false;
  157. off_on_time_ns = genpd->states[state].power_off_latency_ns +
  158. genpd->states[state].power_on_latency_ns;
  159. min_off_time_ns = -1;
  160. /*
  161. * Check if subdomains can be off for enough time.
  162. *
  163. * All subdomains have been powered off already at this point.
  164. */
  165. list_for_each_entry(link, &genpd->parent_links, parent_node) {
  166. struct generic_pm_domain *sd = link->child;
  167. s64 sd_max_off_ns = sd->max_off_time_ns;
  168. if (sd_max_off_ns < 0)
  169. continue;
  170. /*
  171. * Check if the subdomain is allowed to be off long enough for
  172. * the current domain to turn off and on (that's how much time
  173. * it will have to wait worst case).
  174. */
  175. if (sd_max_off_ns <= off_on_time_ns)
  176. return false;
  177. if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
  178. min_off_time_ns = sd_max_off_ns;
  179. }
  180. /*
  181. * Check if the devices in the domain can be off enough time.
  182. */
  183. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  184. struct gpd_timing_data *td;
  185. s64 constraint_ns;
  186. /*
  187. * Check if the device is allowed to be off long enough for the
  188. * domain to turn off and on (that's how much time it will
  189. * have to wait worst case).
  190. */
  191. td = &to_gpd_data(pdd)->td;
  192. constraint_ns = td->effective_constraint_ns;
  193. /*
  194. * Zero means "no suspend at all" and this runs only when all
  195. * devices in the domain are suspended, so it must be positive.
  196. */
  197. if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS)
  198. continue;
  199. if (constraint_ns <= off_on_time_ns)
  200. return false;
  201. if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
  202. min_off_time_ns = constraint_ns;
  203. }
  204. /*
  205. * If the computed minimum device off time is negative, there are no
  206. * latency constraints, so the domain can spend arbitrary time in the
  207. * "off" state.
  208. */
  209. if (min_off_time_ns < 0)
  210. return true;
  211. /*
  212. * The difference between the computed minimum subdomain or device off
  213. * time and the time needed to turn the domain on is the maximum
  214. * theoretical time this domain can spend in the "off" state.
  215. */
  216. genpd->max_off_time_ns = min_off_time_ns -
  217. genpd->states[state].power_on_latency_ns;
  218. return true;
  219. }
  220. /**
  221. * _default_power_down_ok - Default generic PM domain power off governor routine.
  222. * @pd: PM domain to check.
  223. *
  224. * This routine must be executed under the PM domain's lock.
  225. */
  226. static bool _default_power_down_ok(struct dev_pm_domain *pd, ktime_t now)
  227. {
  228. struct generic_pm_domain *genpd = pd_to_genpd(pd);
  229. int state_idx = genpd->state_count - 1;
  230. struct gpd_link *link;
  231. /*
  232. * Find the next wakeup from devices that can determine their own wakeup
  233. * to find when the domain would wakeup and do it for every device down
  234. * the hierarchy. It is not worth while to sleep if the state's residency
  235. * cannot be met.
  236. */
  237. update_domain_next_wakeup(genpd, now);
  238. if ((genpd->flags & GENPD_FLAG_MIN_RESIDENCY) && (genpd->next_wakeup != KTIME_MAX)) {
  239. /* Let's find out the deepest domain idle state, the devices prefer */
  240. while (state_idx >= 0) {
  241. if (next_wakeup_allows_state(genpd, state_idx, now)) {
  242. genpd->max_off_time_changed = true;
  243. break;
  244. }
  245. state_idx--;
  246. }
  247. if (state_idx < 0) {
  248. state_idx = 0;
  249. genpd->cached_power_down_ok = false;
  250. goto done;
  251. }
  252. }
  253. if (!genpd->max_off_time_changed) {
  254. genpd->state_idx = genpd->cached_power_down_state_idx;
  255. return genpd->cached_power_down_ok;
  256. }
  257. /*
  258. * We have to invalidate the cached results for the parents, so
  259. * use the observation that default_power_down_ok() is not
  260. * going to be called for any parent until this instance
  261. * returns.
  262. */
  263. list_for_each_entry(link, &genpd->child_links, child_node)
  264. link->parent->max_off_time_changed = true;
  265. genpd->max_off_time_ns = -1;
  266. genpd->max_off_time_changed = false;
  267. genpd->cached_power_down_ok = true;
  268. /*
  269. * Find a state to power down to, starting from the state
  270. * determined by the next wakeup.
  271. */
  272. while (!__default_power_down_ok(pd, state_idx)) {
  273. if (state_idx == 0) {
  274. genpd->cached_power_down_ok = false;
  275. break;
  276. }
  277. state_idx--;
  278. }
  279. done:
  280. genpd->state_idx = state_idx;
  281. genpd->cached_power_down_state_idx = genpd->state_idx;
  282. return genpd->cached_power_down_ok;
  283. }
  284. static bool default_power_down_ok(struct dev_pm_domain *pd)
  285. {
  286. return _default_power_down_ok(pd, ktime_get());
  287. }
  288. static bool always_on_power_down_ok(struct dev_pm_domain *domain)
  289. {
  290. return false;
  291. }
  292. #ifdef CONFIG_CPU_IDLE
  293. static bool cpu_power_down_ok(struct dev_pm_domain *pd)
  294. {
  295. struct generic_pm_domain *genpd = pd_to_genpd(pd);
  296. struct cpuidle_device *dev;
  297. ktime_t domain_wakeup, next_hrtimer;
  298. ktime_t now = ktime_get();
  299. s64 idle_duration_ns;
  300. int cpu, i;
  301. /* Validate dev PM QoS constraints. */
  302. if (!_default_power_down_ok(pd, now))
  303. return false;
  304. if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN))
  305. return true;
  306. /*
  307. * Find the next wakeup for any of the online CPUs within the PM domain
  308. * and its subdomains. Note, we only need the genpd->cpus, as it already
  309. * contains a mask of all CPUs from subdomains.
  310. */
  311. domain_wakeup = ktime_set(KTIME_SEC_MAX, 0);
  312. for_each_cpu_and(cpu, genpd->cpus, cpu_online_mask) {
  313. dev = per_cpu(cpuidle_devices, cpu);
  314. if (dev) {
  315. next_hrtimer = READ_ONCE(dev->next_hrtimer);
  316. if (ktime_before(next_hrtimer, domain_wakeup))
  317. domain_wakeup = next_hrtimer;
  318. }
  319. }
  320. /* The minimum idle duration is from now - until the next wakeup. */
  321. idle_duration_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
  322. if (idle_duration_ns <= 0)
  323. return false;
  324. /*
  325. * Find the deepest idle state that has its residency value satisfied
  326. * and by also taking into account the power off latency for the state.
  327. * Start at the state picked by the dev PM QoS constraint validation.
  328. */
  329. i = genpd->state_idx;
  330. do {
  331. if (idle_duration_ns >= (genpd->states[i].residency_ns +
  332. genpd->states[i].power_off_latency_ns)) {
  333. genpd->state_idx = i;
  334. return true;
  335. }
  336. } while (--i >= 0);
  337. return false;
  338. }
  339. struct dev_power_governor pm_domain_cpu_gov = {
  340. .suspend_ok = default_suspend_ok,
  341. .power_down_ok = cpu_power_down_ok,
  342. };
  343. #endif
  344. struct dev_power_governor simple_qos_governor = {
  345. .suspend_ok = default_suspend_ok,
  346. .power_down_ok = default_power_down_ok,
  347. };
  348. /**
  349. * pm_genpd_gov_always_on - A governor implementing an always-on policy
  350. */
  351. struct dev_power_governor pm_domain_always_on_gov = {
  352. .power_down_ok = always_on_power_down_ok,
  353. .suspend_ok = default_suspend_ok,
  354. };