governor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/cpu.h>
  12. #include <linux/cpuidle.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/ktime.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. * Returns: true if OK to suspend, false if not OK to suspend
  49. */
  50. static bool default_suspend_ok(struct device *dev)
  51. {
  52. struct gpd_timing_data *td = dev_gpd_data(dev)->td;
  53. unsigned long flags;
  54. s64 constraint_ns;
  55. dev_dbg(dev, "%s()\n", __func__);
  56. spin_lock_irqsave(&dev->power.lock, flags);
  57. if (!td->constraint_changed) {
  58. bool ret = td->cached_suspend_ok;
  59. spin_unlock_irqrestore(&dev->power.lock, flags);
  60. return ret;
  61. }
  62. td->constraint_changed = false;
  63. td->cached_suspend_ok = false;
  64. td->effective_constraint_ns = 0;
  65. constraint_ns = __dev_pm_qos_resume_latency(dev);
  66. spin_unlock_irqrestore(&dev->power.lock, flags);
  67. if (constraint_ns == 0)
  68. return false;
  69. constraint_ns *= NSEC_PER_USEC;
  70. /*
  71. * We can walk the children without any additional locking, because
  72. * they all have been suspended at this point and their
  73. * effective_constraint_ns fields won't be modified in parallel with us.
  74. */
  75. if (!dev->power.ignore_children)
  76. device_for_each_child(dev, &constraint_ns,
  77. dev_update_qos_constraint);
  78. if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) {
  79. /* "No restriction", so the device is allowed to suspend. */
  80. td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
  81. td->cached_suspend_ok = true;
  82. } else if (constraint_ns == 0) {
  83. /*
  84. * This triggers if one of the children that don't belong to a
  85. * domain has a zero PM QoS constraint and it's better not to
  86. * suspend then. effective_constraint_ns is zero already and
  87. * cached_suspend_ok is false, so bail out.
  88. */
  89. return false;
  90. } else {
  91. constraint_ns -= td->suspend_latency_ns +
  92. td->resume_latency_ns;
  93. /*
  94. * effective_constraint_ns is zero already and cached_suspend_ok
  95. * is false, so if the computed value is not positive, return
  96. * right away.
  97. */
  98. if (constraint_ns <= 0)
  99. return false;
  100. td->effective_constraint_ns = constraint_ns;
  101. td->cached_suspend_ok = true;
  102. }
  103. /*
  104. * The children have been suspended already, so we don't need to take
  105. * their suspend latencies into account here.
  106. */
  107. return td->cached_suspend_ok;
  108. }
  109. static void update_domain_next_wakeup(struct generic_pm_domain *genpd, ktime_t now)
  110. {
  111. ktime_t domain_wakeup = KTIME_MAX;
  112. ktime_t next_wakeup;
  113. struct pm_domain_data *pdd;
  114. struct gpd_link *link;
  115. if (!(genpd->flags & GENPD_FLAG_MIN_RESIDENCY))
  116. return;
  117. /*
  118. * Devices that have a predictable wakeup pattern, may specify
  119. * their next wakeup. Let's find the next wakeup from all the
  120. * devices attached to this domain and from all the sub-domains.
  121. * It is possible that component's a next wakeup may have become
  122. * stale when we read that here. We will ignore to ensure the domain
  123. * is able to enter its optimal idle state.
  124. */
  125. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  126. next_wakeup = to_gpd_data(pdd)->td->next_wakeup;
  127. if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
  128. if (ktime_before(next_wakeup, domain_wakeup))
  129. domain_wakeup = next_wakeup;
  130. }
  131. list_for_each_entry(link, &genpd->parent_links, parent_node) {
  132. struct genpd_governor_data *cgd = link->child->gd;
  133. next_wakeup = cgd ? cgd->next_wakeup : KTIME_MAX;
  134. if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
  135. if (ktime_before(next_wakeup, domain_wakeup))
  136. domain_wakeup = next_wakeup;
  137. }
  138. genpd->gd->next_wakeup = domain_wakeup;
  139. }
  140. static bool next_wakeup_allows_state(struct generic_pm_domain *genpd,
  141. unsigned int state, ktime_t now)
  142. {
  143. ktime_t domain_wakeup = genpd->gd->next_wakeup;
  144. s64 idle_time_ns, min_sleep_ns;
  145. min_sleep_ns = genpd->states[state].power_off_latency_ns +
  146. genpd->states[state].residency_ns;
  147. idle_time_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
  148. return idle_time_ns >= min_sleep_ns;
  149. }
  150. static bool __default_power_down_ok(struct dev_pm_domain *pd,
  151. unsigned int state)
  152. {
  153. struct generic_pm_domain *genpd = pd_to_genpd(pd);
  154. struct gpd_link *link;
  155. struct pm_domain_data *pdd;
  156. s64 min_off_time_ns;
  157. s64 off_on_time_ns;
  158. off_on_time_ns = genpd->states[state].power_off_latency_ns +
  159. genpd->states[state].power_on_latency_ns;
  160. min_off_time_ns = -1;
  161. /*
  162. * Check if subdomains can be off for enough time.
  163. *
  164. * All subdomains have been powered off already at this point.
  165. */
  166. list_for_each_entry(link, &genpd->parent_links, parent_node) {
  167. struct genpd_governor_data *cgd = link->child->gd;
  168. s64 sd_max_off_ns = cgd ? cgd->max_off_time_ns : -1;
  169. if (sd_max_off_ns < 0)
  170. continue;
  171. /*
  172. * Check if the subdomain is allowed to be off long enough for
  173. * the current domain to turn off and on (that's how much time
  174. * it will have to wait worst case).
  175. */
  176. if (sd_max_off_ns <= off_on_time_ns)
  177. return false;
  178. if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
  179. min_off_time_ns = sd_max_off_ns;
  180. }
  181. /*
  182. * Check if the devices in the domain can be off enough time.
  183. */
  184. list_for_each_entry(pdd, &genpd->dev_list, list_node) {
  185. struct gpd_timing_data *td;
  186. s64 constraint_ns;
  187. /*
  188. * Check if the device is allowed to be off long enough for the
  189. * domain to turn off and on (that's how much time it will
  190. * have to wait worst case).
  191. */
  192. td = to_gpd_data(pdd)->td;
  193. constraint_ns = td->effective_constraint_ns;
  194. /*
  195. * Zero means "no suspend at all" and this runs only when all
  196. * devices in the domain are suspended, so it must be positive.
  197. */
  198. if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS)
  199. continue;
  200. if (constraint_ns <= off_on_time_ns)
  201. return false;
  202. if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
  203. min_off_time_ns = constraint_ns;
  204. }
  205. /*
  206. * If the computed minimum device off time is negative, there are no
  207. * latency constraints, so the domain can spend arbitrary time in the
  208. * "off" state.
  209. */
  210. if (min_off_time_ns < 0)
  211. return true;
  212. /*
  213. * The difference between the computed minimum subdomain or device off
  214. * time and the time needed to turn the domain on is the maximum
  215. * theoretical time this domain can spend in the "off" state.
  216. */
  217. genpd->gd->max_off_time_ns = min_off_time_ns -
  218. genpd->states[state].power_on_latency_ns;
  219. return true;
  220. }
  221. /**
  222. * _default_power_down_ok - Default generic PM domain power off governor routine.
  223. * @pd: PM domain to check.
  224. * @now: current ktime.
  225. *
  226. * This routine must be executed under the PM domain's lock.
  227. *
  228. * Returns: true if OK to power down, false if not OK to power down
  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. struct device *cpu_dev;
  304. s64 cpu_constraint, global_constraint, wakeup_constraint;
  305. s64 idle_duration_ns;
  306. int cpu, i;
  307. /* Validate dev PM QoS constraints. */
  308. if (!_default_power_down_ok(pd, now))
  309. return false;
  310. if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN))
  311. return true;
  312. wakeup_constraint = cpu_wakeup_latency_qos_limit();
  313. global_constraint = cpu_latency_qos_limit();
  314. if (global_constraint > wakeup_constraint)
  315. global_constraint = wakeup_constraint;
  316. /*
  317. * Find the next wakeup for any of the online CPUs within the PM domain
  318. * and its subdomains. Note, we only need the genpd->cpus, as it already
  319. * contains a mask of all CPUs from subdomains.
  320. */
  321. domain_wakeup = ktime_set(KTIME_SEC_MAX, 0);
  322. for_each_cpu_and(cpu, genpd->cpus, cpu_online_mask) {
  323. dev = per_cpu(cpuidle_devices, cpu);
  324. if (dev) {
  325. next_hrtimer = READ_ONCE(dev->next_hrtimer);
  326. if (ktime_before(next_hrtimer, domain_wakeup))
  327. domain_wakeup = next_hrtimer;
  328. }
  329. cpu_dev = get_cpu_device(cpu);
  330. if (cpu_dev) {
  331. cpu_constraint = dev_pm_qos_raw_resume_latency(cpu_dev);
  332. if (cpu_constraint < global_constraint)
  333. global_constraint = cpu_constraint;
  334. }
  335. }
  336. global_constraint *= NSEC_PER_USEC;
  337. /* The minimum idle duration is from now - until the next wakeup. */
  338. idle_duration_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
  339. if (idle_duration_ns <= 0)
  340. return false;
  341. /* Store the next domain_wakeup to allow consumers to use it. */
  342. genpd->gd->next_hrtimer = domain_wakeup;
  343. /*
  344. * Find the deepest idle state that has its residency value satisfied
  345. * and by also taking into account the power off latency for the state.
  346. * Start at the state picked by the dev PM QoS constraint validation.
  347. */
  348. i = genpd->state_idx;
  349. do {
  350. if ((idle_duration_ns >= (genpd->states[i].residency_ns +
  351. genpd->states[i].power_off_latency_ns)) &&
  352. (global_constraint >= (genpd->states[i].power_on_latency_ns +
  353. genpd->states[i].power_off_latency_ns)))
  354. break;
  355. } while (--i >= 0);
  356. if (i < 0)
  357. return false;
  358. if (cpus_peek_for_pending_ipi(genpd->cpus))
  359. return false;
  360. genpd->state_idx = i;
  361. genpd->gd->last_enter = now;
  362. genpd->gd->reflect_residency = true;
  363. return true;
  364. }
  365. static bool cpu_system_power_down_ok(struct dev_pm_domain *pd)
  366. {
  367. s64 constraint_ns = cpu_wakeup_latency_qos_limit() * NSEC_PER_USEC;
  368. struct generic_pm_domain *genpd = pd_to_genpd(pd);
  369. int state_idx = genpd->state_count - 1;
  370. if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN)) {
  371. genpd->state_idx = state_idx;
  372. return true;
  373. }
  374. /* Find the deepest state for the latency constraint. */
  375. while (state_idx >= 0) {
  376. s64 latency_ns = genpd->states[state_idx].power_off_latency_ns +
  377. genpd->states[state_idx].power_on_latency_ns;
  378. if (latency_ns <= constraint_ns) {
  379. genpd->state_idx = state_idx;
  380. return true;
  381. }
  382. state_idx--;
  383. }
  384. return false;
  385. }
  386. struct dev_power_governor pm_domain_cpu_gov = {
  387. .suspend_ok = default_suspend_ok,
  388. .power_down_ok = cpu_power_down_ok,
  389. .system_power_down_ok = cpu_system_power_down_ok,
  390. };
  391. #endif
  392. struct dev_power_governor simple_qos_governor = {
  393. .suspend_ok = default_suspend_ok,
  394. .power_down_ok = default_power_down_ok,
  395. };
  396. /*
  397. * pm_domain_always_on_gov - A governor implementing an always-on policy
  398. */
  399. struct dev_power_governor pm_domain_always_on_gov = {
  400. .suspend_ok = default_suspend_ok,
  401. };