domain_governor.c 12 KB

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