pm_runtime.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * pm_runtime.h - Device run-time power management helper functions.
  4. *
  5. * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>
  6. */
  7. #ifndef _LINUX_PM_RUNTIME_H
  8. #define _LINUX_PM_RUNTIME_H
  9. #include <linux/device.h>
  10. #include <linux/notifier.h>
  11. #include <linux/pm.h>
  12. #include <linux/jiffies.h>
  13. /* Runtime PM flag argument bits */
  14. #define RPM_ASYNC 0x01 /* Request is asynchronous */
  15. #define RPM_NOWAIT 0x02 /* Don't wait for concurrent
  16. state change */
  17. #define RPM_GET_PUT 0x04 /* Increment/decrement the
  18. usage_count */
  19. #define RPM_AUTO 0x08 /* Use autosuspend_delay */
  20. #ifdef CONFIG_PM
  21. extern struct workqueue_struct *pm_wq;
  22. static inline bool queue_pm_work(struct work_struct *work)
  23. {
  24. return queue_work(pm_wq, work);
  25. }
  26. extern int pm_generic_runtime_suspend(struct device *dev);
  27. extern int pm_generic_runtime_resume(struct device *dev);
  28. extern int pm_runtime_force_suspend(struct device *dev);
  29. extern int pm_runtime_force_resume(struct device *dev);
  30. extern int __pm_runtime_idle(struct device *dev, int rpmflags);
  31. extern int __pm_runtime_suspend(struct device *dev, int rpmflags);
  32. extern int __pm_runtime_resume(struct device *dev, int rpmflags);
  33. extern int pm_runtime_get_if_active(struct device *dev, bool ign_usage_count);
  34. extern int pm_schedule_suspend(struct device *dev, unsigned int delay);
  35. extern int __pm_runtime_set_status(struct device *dev, unsigned int status);
  36. extern int pm_runtime_barrier(struct device *dev);
  37. extern void pm_runtime_enable(struct device *dev);
  38. extern void __pm_runtime_disable(struct device *dev, bool check_resume);
  39. extern void pm_runtime_allow(struct device *dev);
  40. extern void pm_runtime_forbid(struct device *dev);
  41. extern void pm_runtime_no_callbacks(struct device *dev);
  42. extern void pm_runtime_irq_safe(struct device *dev);
  43. extern void __pm_runtime_use_autosuspend(struct device *dev, bool use);
  44. extern void pm_runtime_set_autosuspend_delay(struct device *dev, int delay);
  45. extern u64 pm_runtime_autosuspend_expiration(struct device *dev);
  46. extern void pm_runtime_update_max_time_suspended(struct device *dev,
  47. s64 delta_ns);
  48. extern void pm_runtime_set_memalloc_noio(struct device *dev, bool enable);
  49. extern void pm_runtime_get_suppliers(struct device *dev);
  50. extern void pm_runtime_put_suppliers(struct device *dev);
  51. extern void pm_runtime_new_link(struct device *dev);
  52. extern void pm_runtime_drop_link(struct device_link *link);
  53. extern void pm_runtime_release_supplier(struct device_link *link, bool check_idle);
  54. /**
  55. * pm_runtime_get_if_in_use - Conditionally bump up runtime PM usage counter.
  56. * @dev: Target device.
  57. *
  58. * Increment the runtime PM usage counter of @dev if its runtime PM status is
  59. * %RPM_ACTIVE and its runtime PM usage counter is greater than 0.
  60. */
  61. static inline int pm_runtime_get_if_in_use(struct device *dev)
  62. {
  63. return pm_runtime_get_if_active(dev, false);
  64. }
  65. /**
  66. * pm_suspend_ignore_children - Set runtime PM behavior regarding children.
  67. * @dev: Target device.
  68. * @enable: Whether or not to ignore possible dependencies on children.
  69. *
  70. * The dependencies of @dev on its children will not be taken into account by
  71. * the runtime PM framework going forward if @enable is %true, or they will
  72. * be taken into account otherwise.
  73. */
  74. static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
  75. {
  76. dev->power.ignore_children = enable;
  77. }
  78. /**
  79. * pm_runtime_get_noresume - Bump up runtime PM usage counter of a device.
  80. * @dev: Target device.
  81. */
  82. static inline void pm_runtime_get_noresume(struct device *dev)
  83. {
  84. atomic_inc(&dev->power.usage_count);
  85. }
  86. /**
  87. * pm_runtime_put_noidle - Drop runtime PM usage counter of a device.
  88. * @dev: Target device.
  89. *
  90. * Decrement the runtime PM usage counter of @dev unless it is 0 already.
  91. */
  92. static inline void pm_runtime_put_noidle(struct device *dev)
  93. {
  94. atomic_add_unless(&dev->power.usage_count, -1, 0);
  95. }
  96. /**
  97. * pm_runtime_suspended - Check whether or not a device is runtime-suspended.
  98. * @dev: Target device.
  99. *
  100. * Return %true if runtime PM is enabled for @dev and its runtime PM status is
  101. * %RPM_SUSPENDED, or %false otherwise.
  102. *
  103. * Note that the return value of this function can only be trusted if it is
  104. * called under the runtime PM lock of @dev or under conditions in which
  105. * runtime PM cannot be either disabled or enabled for @dev and its runtime PM
  106. * status cannot change.
  107. */
  108. static inline bool pm_runtime_suspended(struct device *dev)
  109. {
  110. return dev->power.runtime_status == RPM_SUSPENDED
  111. && !dev->power.disable_depth;
  112. }
  113. /**
  114. * pm_runtime_active - Check whether or not a device is runtime-active.
  115. * @dev: Target device.
  116. *
  117. * Return %true if runtime PM is disabled for @dev or its runtime PM status is
  118. * %RPM_ACTIVE, or %false otherwise.
  119. *
  120. * Note that the return value of this function can only be trusted if it is
  121. * called under the runtime PM lock of @dev or under conditions in which
  122. * runtime PM cannot be either disabled or enabled for @dev and its runtime PM
  123. * status cannot change.
  124. */
  125. static inline bool pm_runtime_active(struct device *dev)
  126. {
  127. return dev->power.runtime_status == RPM_ACTIVE
  128. || dev->power.disable_depth;
  129. }
  130. /**
  131. * pm_runtime_status_suspended - Check if runtime PM status is "suspended".
  132. * @dev: Target device.
  133. *
  134. * Return %true if the runtime PM status of @dev is %RPM_SUSPENDED, or %false
  135. * otherwise, regardless of whether or not runtime PM has been enabled for @dev.
  136. *
  137. * Note that the return value of this function can only be trusted if it is
  138. * called under the runtime PM lock of @dev or under conditions in which the
  139. * runtime PM status of @dev cannot change.
  140. */
  141. static inline bool pm_runtime_status_suspended(struct device *dev)
  142. {
  143. return dev->power.runtime_status == RPM_SUSPENDED;
  144. }
  145. /**
  146. * pm_runtime_enabled - Check if runtime PM is enabled.
  147. * @dev: Target device.
  148. *
  149. * Return %true if runtime PM is enabled for @dev or %false otherwise.
  150. *
  151. * Note that the return value of this function can only be trusted if it is
  152. * called under the runtime PM lock of @dev or under conditions in which
  153. * runtime PM cannot be either disabled or enabled for @dev.
  154. */
  155. static inline bool pm_runtime_enabled(struct device *dev)
  156. {
  157. return !dev->power.disable_depth;
  158. }
  159. /**
  160. * pm_runtime_has_no_callbacks - Check if runtime PM callbacks may be present.
  161. * @dev: Target device.
  162. *
  163. * Return %true if @dev is a special device without runtime PM callbacks or
  164. * %false otherwise.
  165. */
  166. static inline bool pm_runtime_has_no_callbacks(struct device *dev)
  167. {
  168. return dev->power.no_callbacks;
  169. }
  170. /**
  171. * pm_runtime_mark_last_busy - Update the last access time of a device.
  172. * @dev: Target device.
  173. *
  174. * Update the last access time of @dev used by the runtime PM autosuspend
  175. * mechanism to the current time as returned by ktime_get_mono_fast_ns().
  176. */
  177. static inline void pm_runtime_mark_last_busy(struct device *dev)
  178. {
  179. WRITE_ONCE(dev->power.last_busy, ktime_get_mono_fast_ns());
  180. }
  181. /**
  182. * pm_runtime_is_irq_safe - Check if runtime PM can work in interrupt context.
  183. * @dev: Target device.
  184. *
  185. * Return %true if @dev has been marked as an "IRQ-safe" device (with respect
  186. * to runtime PM), in which case its runtime PM callabcks can be expected to
  187. * work correctly when invoked from interrupt handlers.
  188. */
  189. static inline bool pm_runtime_is_irq_safe(struct device *dev)
  190. {
  191. return dev->power.irq_safe;
  192. }
  193. extern u64 pm_runtime_suspended_time(struct device *dev);
  194. #else /* !CONFIG_PM */
  195. static inline bool queue_pm_work(struct work_struct *work) { return false; }
  196. static inline int pm_generic_runtime_suspend(struct device *dev) { return 0; }
  197. static inline int pm_generic_runtime_resume(struct device *dev) { return 0; }
  198. static inline int pm_runtime_force_suspend(struct device *dev) { return 0; }
  199. static inline int pm_runtime_force_resume(struct device *dev) { return 0; }
  200. static inline int __pm_runtime_idle(struct device *dev, int rpmflags)
  201. {
  202. return -ENOSYS;
  203. }
  204. static inline int __pm_runtime_suspend(struct device *dev, int rpmflags)
  205. {
  206. return -ENOSYS;
  207. }
  208. static inline int __pm_runtime_resume(struct device *dev, int rpmflags)
  209. {
  210. return 1;
  211. }
  212. static inline int pm_schedule_suspend(struct device *dev, unsigned int delay)
  213. {
  214. return -ENOSYS;
  215. }
  216. static inline int pm_runtime_get_if_in_use(struct device *dev)
  217. {
  218. return -EINVAL;
  219. }
  220. static inline int pm_runtime_get_if_active(struct device *dev,
  221. bool ign_usage_count)
  222. {
  223. return -EINVAL;
  224. }
  225. static inline int __pm_runtime_set_status(struct device *dev,
  226. unsigned int status) { return 0; }
  227. static inline int pm_runtime_barrier(struct device *dev) { return 0; }
  228. static inline void pm_runtime_enable(struct device *dev) {}
  229. static inline void __pm_runtime_disable(struct device *dev, bool c) {}
  230. static inline void pm_runtime_allow(struct device *dev) {}
  231. static inline void pm_runtime_forbid(struct device *dev) {}
  232. static inline void pm_suspend_ignore_children(struct device *dev, bool enable) {}
  233. static inline void pm_runtime_get_noresume(struct device *dev) {}
  234. static inline void pm_runtime_put_noidle(struct device *dev) {}
  235. static inline bool pm_runtime_suspended(struct device *dev) { return false; }
  236. static inline bool pm_runtime_active(struct device *dev) { return true; }
  237. static inline bool pm_runtime_status_suspended(struct device *dev) { return false; }
  238. static inline bool pm_runtime_enabled(struct device *dev) { return false; }
  239. static inline void pm_runtime_no_callbacks(struct device *dev) {}
  240. static inline void pm_runtime_irq_safe(struct device *dev) {}
  241. static inline bool pm_runtime_is_irq_safe(struct device *dev) { return false; }
  242. static inline bool pm_runtime_has_no_callbacks(struct device *dev) { return false; }
  243. static inline void pm_runtime_mark_last_busy(struct device *dev) {}
  244. static inline void __pm_runtime_use_autosuspend(struct device *dev,
  245. bool use) {}
  246. static inline void pm_runtime_set_autosuspend_delay(struct device *dev,
  247. int delay) {}
  248. static inline u64 pm_runtime_autosuspend_expiration(
  249. struct device *dev) { return 0; }
  250. static inline void pm_runtime_set_memalloc_noio(struct device *dev,
  251. bool enable){}
  252. static inline void pm_runtime_get_suppliers(struct device *dev) {}
  253. static inline void pm_runtime_put_suppliers(struct device *dev) {}
  254. static inline void pm_runtime_new_link(struct device *dev) {}
  255. static inline void pm_runtime_drop_link(struct device_link *link) {}
  256. static inline void pm_runtime_release_supplier(struct device_link *link,
  257. bool check_idle) {}
  258. #endif /* !CONFIG_PM */
  259. /**
  260. * pm_runtime_idle - Conditionally set up autosuspend of a device or suspend it.
  261. * @dev: Target device.
  262. *
  263. * Invoke the "idle check" callback of @dev and, depending on its return value,
  264. * set up autosuspend of @dev or suspend it (depending on whether or not
  265. * autosuspend has been enabled for it).
  266. */
  267. static inline int pm_runtime_idle(struct device *dev)
  268. {
  269. return __pm_runtime_idle(dev, 0);
  270. }
  271. /**
  272. * pm_runtime_suspend - Suspend a device synchronously.
  273. * @dev: Target device.
  274. */
  275. static inline int pm_runtime_suspend(struct device *dev)
  276. {
  277. return __pm_runtime_suspend(dev, 0);
  278. }
  279. /**
  280. * pm_runtime_autosuspend - Set up autosuspend of a device or suspend it.
  281. * @dev: Target device.
  282. *
  283. * Set up autosuspend of @dev or suspend it (depending on whether or not
  284. * autosuspend is enabled for it) without engaging its "idle check" callback.
  285. */
  286. static inline int pm_runtime_autosuspend(struct device *dev)
  287. {
  288. return __pm_runtime_suspend(dev, RPM_AUTO);
  289. }
  290. /**
  291. * pm_runtime_resume - Resume a device synchronously.
  292. * @dev: Target device.
  293. */
  294. static inline int pm_runtime_resume(struct device *dev)
  295. {
  296. return __pm_runtime_resume(dev, 0);
  297. }
  298. /**
  299. * pm_request_idle - Queue up "idle check" execution for a device.
  300. * @dev: Target device.
  301. *
  302. * Queue up a work item to run an equivalent of pm_runtime_idle() for @dev
  303. * asynchronously.
  304. */
  305. static inline int pm_request_idle(struct device *dev)
  306. {
  307. return __pm_runtime_idle(dev, RPM_ASYNC);
  308. }
  309. /**
  310. * pm_request_resume - Queue up runtime-resume of a device.
  311. * @dev: Target device.
  312. */
  313. static inline int pm_request_resume(struct device *dev)
  314. {
  315. return __pm_runtime_resume(dev, RPM_ASYNC);
  316. }
  317. /**
  318. * pm_request_autosuspend - Queue up autosuspend of a device.
  319. * @dev: Target device.
  320. *
  321. * Queue up a work item to run an equivalent pm_runtime_autosuspend() for @dev
  322. * asynchronously.
  323. */
  324. static inline int pm_request_autosuspend(struct device *dev)
  325. {
  326. return __pm_runtime_suspend(dev, RPM_ASYNC | RPM_AUTO);
  327. }
  328. /**
  329. * pm_runtime_get - Bump up usage counter and queue up resume of a device.
  330. * @dev: Target device.
  331. *
  332. * Bump up the runtime PM usage counter of @dev and queue up a work item to
  333. * carry out runtime-resume of it.
  334. */
  335. static inline int pm_runtime_get(struct device *dev)
  336. {
  337. return __pm_runtime_resume(dev, RPM_GET_PUT | RPM_ASYNC);
  338. }
  339. /**
  340. * pm_runtime_get_sync - Bump up usage counter of a device and resume it.
  341. * @dev: Target device.
  342. *
  343. * Bump up the runtime PM usage counter of @dev and carry out runtime-resume of
  344. * it synchronously.
  345. *
  346. * The possible return values of this function are the same as for
  347. * pm_runtime_resume() and the runtime PM usage counter of @dev remains
  348. * incremented in all cases, even if it returns an error code.
  349. */
  350. static inline int pm_runtime_get_sync(struct device *dev)
  351. {
  352. return __pm_runtime_resume(dev, RPM_GET_PUT);
  353. }
  354. /**
  355. * pm_runtime_resume_and_get - Bump up usage counter of a device and resume it.
  356. * @dev: Target device.
  357. *
  358. * Resume @dev synchronously and if that is successful, increment its runtime
  359. * PM usage counter. Return 0 if the runtime PM usage counter of @dev has been
  360. * incremented or a negative error code otherwise.
  361. */
  362. static inline int pm_runtime_resume_and_get(struct device *dev)
  363. {
  364. int ret;
  365. ret = __pm_runtime_resume(dev, RPM_GET_PUT);
  366. if (ret < 0) {
  367. pm_runtime_put_noidle(dev);
  368. return ret;
  369. }
  370. return 0;
  371. }
  372. /**
  373. * pm_runtime_put - Drop device usage counter and queue up "idle check" if 0.
  374. * @dev: Target device.
  375. *
  376. * Decrement the runtime PM usage counter of @dev and if it turns out to be
  377. * equal to 0, queue up a work item for @dev like in pm_request_idle().
  378. */
  379. static inline int pm_runtime_put(struct device *dev)
  380. {
  381. return __pm_runtime_idle(dev, RPM_GET_PUT | RPM_ASYNC);
  382. }
  383. /**
  384. * pm_runtime_put_autosuspend - Drop device usage counter and queue autosuspend if 0.
  385. * @dev: Target device.
  386. *
  387. * Decrement the runtime PM usage counter of @dev and if it turns out to be
  388. * equal to 0, queue up a work item for @dev like in pm_request_autosuspend().
  389. */
  390. static inline int pm_runtime_put_autosuspend(struct device *dev)
  391. {
  392. return __pm_runtime_suspend(dev,
  393. RPM_GET_PUT | RPM_ASYNC | RPM_AUTO);
  394. }
  395. /**
  396. * pm_runtime_put_sync - Drop device usage counter and run "idle check" if 0.
  397. * @dev: Target device.
  398. *
  399. * Decrement the runtime PM usage counter of @dev and if it turns out to be
  400. * equal to 0, invoke the "idle check" callback of @dev and, depending on its
  401. * return value, set up autosuspend of @dev or suspend it (depending on whether
  402. * or not autosuspend has been enabled for it).
  403. *
  404. * The possible return values of this function are the same as for
  405. * pm_runtime_idle() and the runtime PM usage counter of @dev remains
  406. * decremented in all cases, even if it returns an error code.
  407. */
  408. static inline int pm_runtime_put_sync(struct device *dev)
  409. {
  410. return __pm_runtime_idle(dev, RPM_GET_PUT);
  411. }
  412. /**
  413. * pm_runtime_put_sync_suspend - Drop device usage counter and suspend if 0.
  414. * @dev: Target device.
  415. *
  416. * Decrement the runtime PM usage counter of @dev and if it turns out to be
  417. * equal to 0, carry out runtime-suspend of @dev synchronously.
  418. *
  419. * The possible return values of this function are the same as for
  420. * pm_runtime_suspend() and the runtime PM usage counter of @dev remains
  421. * decremented in all cases, even if it returns an error code.
  422. */
  423. static inline int pm_runtime_put_sync_suspend(struct device *dev)
  424. {
  425. return __pm_runtime_suspend(dev, RPM_GET_PUT);
  426. }
  427. /**
  428. * pm_runtime_put_sync_autosuspend - Drop device usage counter and autosuspend if 0.
  429. * @dev: Target device.
  430. *
  431. * Decrement the runtime PM usage counter of @dev and if it turns out to be
  432. * equal to 0, set up autosuspend of @dev or suspend it synchronously (depending
  433. * on whether or not autosuspend has been enabled for it).
  434. *
  435. * The possible return values of this function are the same as for
  436. * pm_runtime_autosuspend() and the runtime PM usage counter of @dev remains
  437. * decremented in all cases, even if it returns an error code.
  438. */
  439. static inline int pm_runtime_put_sync_autosuspend(struct device *dev)
  440. {
  441. return __pm_runtime_suspend(dev, RPM_GET_PUT | RPM_AUTO);
  442. }
  443. /**
  444. * pm_runtime_set_active - Set runtime PM status to "active".
  445. * @dev: Target device.
  446. *
  447. * Set the runtime PM status of @dev to %RPM_ACTIVE and ensure that dependencies
  448. * of it will be taken into account.
  449. *
  450. * It is not valid to call this function for devices with runtime PM enabled.
  451. */
  452. static inline int pm_runtime_set_active(struct device *dev)
  453. {
  454. return __pm_runtime_set_status(dev, RPM_ACTIVE);
  455. }
  456. /**
  457. * pm_runtime_set_suspended - Set runtime PM status to "suspended".
  458. * @dev: Target device.
  459. *
  460. * Set the runtime PM status of @dev to %RPM_SUSPENDED and ensure that
  461. * dependencies of it will be taken into account.
  462. *
  463. * It is not valid to call this function for devices with runtime PM enabled.
  464. */
  465. static inline int pm_runtime_set_suspended(struct device *dev)
  466. {
  467. return __pm_runtime_set_status(dev, RPM_SUSPENDED);
  468. }
  469. /**
  470. * pm_runtime_disable - Disable runtime PM for a device.
  471. * @dev: Target device.
  472. *
  473. * Prevent the runtime PM framework from working with @dev (by incrementing its
  474. * "blocking" counter).
  475. *
  476. * For each invocation of this function for @dev there must be a matching
  477. * pm_runtime_enable() call in order for runtime PM to be enabled for it.
  478. */
  479. static inline void pm_runtime_disable(struct device *dev)
  480. {
  481. __pm_runtime_disable(dev, true);
  482. }
  483. /**
  484. * pm_runtime_use_autosuspend - Allow autosuspend to be used for a device.
  485. * @dev: Target device.
  486. *
  487. * Allow the runtime PM autosuspend mechanism to be used for @dev whenever
  488. * requested (or "autosuspend" will be handled as direct runtime-suspend for
  489. * it).
  490. */
  491. static inline void pm_runtime_use_autosuspend(struct device *dev)
  492. {
  493. __pm_runtime_use_autosuspend(dev, true);
  494. }
  495. /**
  496. * pm_runtime_dont_use_autosuspend - Prevent autosuspend from being used.
  497. * @dev: Target device.
  498. *
  499. * Prevent the runtime PM autosuspend mechanism from being used for @dev which
  500. * means that "autosuspend" will be handled as direct runtime-suspend for it
  501. * going forward.
  502. */
  503. static inline void pm_runtime_dont_use_autosuspend(struct device *dev)
  504. {
  505. __pm_runtime_use_autosuspend(dev, false);
  506. }
  507. #endif