Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at d986ba0329dcca102e227995371135c9bbcefb6b 823 lines 29 kB view raw
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 8#ifndef _LINUX_PM_RUNTIME_H 9#define _LINUX_PM_RUNTIME_H 10 11#include <linux/device.h> 12#include <linux/notifier.h> 13#include <linux/pm.h> 14 15#include <linux/jiffies.h> 16 17/* Runtime PM flag argument bits */ 18#define RPM_ASYNC 0x01 /* Request is asynchronous */ 19#define RPM_NOWAIT 0x02 /* Don't wait for concurrent 20 state change */ 21#define RPM_GET_PUT 0x04 /* Increment/decrement the 22 usage_count */ 23#define RPM_AUTO 0x08 /* Use autosuspend_delay */ 24#define RPM_TRANSPARENT 0x10 /* Succeed if runtime PM is disabled */ 25 26/* 27 * Use this for defining a set of PM operations to be used in all situations 28 * (system suspend, hibernation or runtime PM). 29 * 30 * Note that the behaviour differs from the deprecated UNIVERSAL_DEV_PM_OPS() 31 * macro, which uses the provided callbacks for both runtime PM and system 32 * sleep, while DEFINE_RUNTIME_DEV_PM_OPS() uses pm_runtime_force_suspend() 33 * and pm_runtime_force_resume() for its system sleep callbacks. 34 * 35 * If the underlying dev_pm_ops struct symbol has to be exported, use 36 * EXPORT_RUNTIME_DEV_PM_OPS() or EXPORT_GPL_RUNTIME_DEV_PM_OPS() instead. 37 */ 38#define DEFINE_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn) \ 39 _DEFINE_DEV_PM_OPS(name, pm_runtime_force_suspend, \ 40 pm_runtime_force_resume, suspend_fn, \ 41 resume_fn, idle_fn) 42 43#define EXPORT_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn) \ 44 EXPORT_DEV_PM_OPS(name) = { \ 45 RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \ 46 } 47#define EXPORT_GPL_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn) \ 48 EXPORT_GPL_DEV_PM_OPS(name) = { \ 49 RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \ 50 } 51#define EXPORT_NS_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn, ns) \ 52 EXPORT_NS_DEV_PM_OPS(name, ns) = { \ 53 RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \ 54 } 55#define EXPORT_NS_GPL_RUNTIME_DEV_PM_OPS(name, suspend_fn, resume_fn, idle_fn, ns) \ 56 EXPORT_NS_GPL_DEV_PM_OPS(name, ns) = { \ 57 RUNTIME_PM_OPS(suspend_fn, resume_fn, idle_fn) \ 58 } 59 60#ifdef CONFIG_PM 61extern struct workqueue_struct *pm_wq; 62 63static inline bool queue_pm_work(struct work_struct *work) 64{ 65 return queue_work(pm_wq, work); 66} 67 68extern int pm_generic_runtime_suspend(struct device *dev); 69extern int pm_generic_runtime_resume(struct device *dev); 70extern int pm_runtime_force_suspend(struct device *dev); 71 72extern int __pm_runtime_idle(struct device *dev, int rpmflags); 73extern int __pm_runtime_suspend(struct device *dev, int rpmflags); 74extern int __pm_runtime_resume(struct device *dev, int rpmflags); 75extern int pm_runtime_get_if_active(struct device *dev); 76extern int pm_runtime_get_if_in_use(struct device *dev); 77extern int pm_schedule_suspend(struct device *dev, unsigned int delay); 78extern int __pm_runtime_set_status(struct device *dev, unsigned int status); 79extern void pm_runtime_barrier(struct device *dev); 80extern bool pm_runtime_block_if_disabled(struct device *dev); 81extern void pm_runtime_unblock(struct device *dev); 82extern void pm_runtime_enable(struct device *dev); 83extern void __pm_runtime_disable(struct device *dev, bool check_resume); 84extern void pm_runtime_allow(struct device *dev); 85extern void pm_runtime_forbid(struct device *dev); 86extern void pm_runtime_no_callbacks(struct device *dev); 87extern void pm_runtime_irq_safe(struct device *dev); 88extern void __pm_runtime_use_autosuspend(struct device *dev, bool use); 89extern void pm_runtime_set_autosuspend_delay(struct device *dev, int delay); 90extern u64 pm_runtime_autosuspend_expiration(struct device *dev); 91extern void pm_runtime_set_memalloc_noio(struct device *dev, bool enable); 92extern void pm_runtime_get_suppliers(struct device *dev); 93extern void pm_runtime_put_suppliers(struct device *dev); 94extern void pm_runtime_new_link(struct device *dev); 95extern void pm_runtime_drop_link(struct device_link *link); 96extern void pm_runtime_release_supplier(struct device_link *link); 97 98int devm_pm_runtime_set_active_enabled(struct device *dev); 99extern int devm_pm_runtime_enable(struct device *dev); 100int devm_pm_runtime_get_noresume(struct device *dev); 101 102/** 103 * pm_suspend_ignore_children - Set runtime PM behavior regarding children. 104 * @dev: Target device. 105 * @enable: Whether or not to ignore possible dependencies on children. 106 * 107 * The dependencies of @dev on its children will not be taken into account by 108 * the runtime PM framework going forward if @enable is %true, or they will 109 * be taken into account otherwise. 110 */ 111static inline void pm_suspend_ignore_children(struct device *dev, bool enable) 112{ 113 dev->power.ignore_children = enable; 114} 115 116/** 117 * pm_runtime_get_noresume - Bump up runtime PM usage counter of a device. 118 * @dev: Target device. 119 */ 120static inline void pm_runtime_get_noresume(struct device *dev) 121{ 122 atomic_inc(&dev->power.usage_count); 123} 124 125/** 126 * pm_runtime_put_noidle - Drop runtime PM usage counter of a device. 127 * @dev: Target device. 128 * 129 * Decrement the runtime PM usage counter of @dev unless it is 0 already. 130 */ 131static inline void pm_runtime_put_noidle(struct device *dev) 132{ 133 atomic_add_unless(&dev->power.usage_count, -1, 0); 134} 135 136/** 137 * pm_runtime_suspended - Check whether or not a device is runtime-suspended. 138 * @dev: Target device. 139 * 140 * Return %true if runtime PM is enabled for @dev and its runtime PM status is 141 * %RPM_SUSPENDED, or %false otherwise. 142 * 143 * Note that the return value of this function can only be trusted if it is 144 * called under the runtime PM lock of @dev or under conditions in which 145 * runtime PM cannot be either disabled or enabled for @dev and its runtime PM 146 * status cannot change. 147 */ 148static inline bool pm_runtime_suspended(struct device *dev) 149{ 150 return dev->power.runtime_status == RPM_SUSPENDED 151 && !dev->power.disable_depth; 152} 153 154/** 155 * pm_runtime_active - Check whether or not a device is runtime-active. 156 * @dev: Target device. 157 * 158 * Return %true if runtime PM is disabled for @dev or its runtime PM status is 159 * %RPM_ACTIVE, or %false otherwise. 160 * 161 * Note that the return value of this function can only be trusted if it is 162 * called under the runtime PM lock of @dev or under conditions in which 163 * runtime PM cannot be either disabled or enabled for @dev and its runtime PM 164 * status cannot change. 165 */ 166static inline bool pm_runtime_active(struct device *dev) 167{ 168 return dev->power.runtime_status == RPM_ACTIVE 169 || dev->power.disable_depth; 170} 171 172/** 173 * pm_runtime_status_suspended - Check if runtime PM status is "suspended". 174 * @dev: Target device. 175 * 176 * Return %true if the runtime PM status of @dev is %RPM_SUSPENDED, or %false 177 * otherwise, regardless of whether or not runtime PM has been enabled for @dev. 178 * 179 * Note that the return value of this function can only be trusted if it is 180 * called under the runtime PM lock of @dev or under conditions in which the 181 * runtime PM status of @dev cannot change. 182 */ 183static inline bool pm_runtime_status_suspended(struct device *dev) 184{ 185 return dev->power.runtime_status == RPM_SUSPENDED; 186} 187 188/** 189 * pm_runtime_enabled - Check if runtime PM is enabled. 190 * @dev: Target device. 191 * 192 * Return %true if runtime PM is enabled for @dev or %false otherwise. 193 * 194 * Note that the return value of this function can only be trusted if it is 195 * called under the runtime PM lock of @dev or under conditions in which 196 * runtime PM cannot be either disabled or enabled for @dev. 197 */ 198static inline bool pm_runtime_enabled(struct device *dev) 199{ 200 return !dev->power.disable_depth; 201} 202 203/** 204 * pm_runtime_blocked - Check if runtime PM enabling is blocked. 205 * @dev: Target device. 206 * 207 * Do not call this function outside system suspend/resume code paths. 208 */ 209static inline bool pm_runtime_blocked(struct device *dev) 210{ 211 return dev->power.last_status == RPM_BLOCKED; 212} 213 214/** 215 * pm_runtime_has_no_callbacks - Check if runtime PM callbacks may be present. 216 * @dev: Target device. 217 * 218 * Return %true if @dev is a special device without runtime PM callbacks or 219 * %false otherwise. 220 */ 221static inline bool pm_runtime_has_no_callbacks(struct device *dev) 222{ 223 return dev->power.no_callbacks; 224} 225 226/** 227 * pm_runtime_mark_last_busy - Update the last access time of a device. 228 * @dev: Target device. 229 * 230 * Update the last access time of @dev used by the runtime PM autosuspend 231 * mechanism to the current time as returned by ktime_get_mono_fast_ns(). 232 */ 233static inline void pm_runtime_mark_last_busy(struct device *dev) 234{ 235 WRITE_ONCE(dev->power.last_busy, ktime_get_mono_fast_ns()); 236} 237 238/** 239 * pm_runtime_is_irq_safe - Check if runtime PM can work in interrupt context. 240 * @dev: Target device. 241 * 242 * Return %true if @dev has been marked as an "IRQ-safe" device (with respect 243 * to runtime PM), in which case its runtime PM callabcks can be expected to 244 * work correctly when invoked from interrupt handlers. 245 */ 246static inline bool pm_runtime_is_irq_safe(struct device *dev) 247{ 248 return dev->power.irq_safe; 249} 250 251extern u64 pm_runtime_suspended_time(struct device *dev); 252 253#else /* !CONFIG_PM */ 254 255static inline bool queue_pm_work(struct work_struct *work) { return false; } 256 257static inline int pm_generic_runtime_suspend(struct device *dev) { return 0; } 258static inline int pm_generic_runtime_resume(struct device *dev) { return 0; } 259static inline int pm_runtime_force_suspend(struct device *dev) { return 0; } 260 261static inline int __pm_runtime_idle(struct device *dev, int rpmflags) 262{ 263 return -ENOSYS; 264} 265static inline int __pm_runtime_suspend(struct device *dev, int rpmflags) 266{ 267 return -ENOSYS; 268} 269static inline int __pm_runtime_resume(struct device *dev, int rpmflags) 270{ 271 return 1; 272} 273static inline int pm_schedule_suspend(struct device *dev, unsigned int delay) 274{ 275 return -ENOSYS; 276} 277static inline int pm_runtime_get_if_in_use(struct device *dev) 278{ 279 return -EINVAL; 280} 281static inline int pm_runtime_get_if_active(struct device *dev) 282{ 283 return -EINVAL; 284} 285static inline int __pm_runtime_set_status(struct device *dev, 286 unsigned int status) { return 0; } 287static inline void pm_runtime_barrier(struct device *dev) {} 288static inline bool pm_runtime_block_if_disabled(struct device *dev) { return true; } 289static inline void pm_runtime_unblock(struct device *dev) {} 290static inline void pm_runtime_enable(struct device *dev) {} 291static inline void __pm_runtime_disable(struct device *dev, bool c) {} 292static inline bool pm_runtime_blocked(struct device *dev) { return true; } 293static inline void pm_runtime_allow(struct device *dev) {} 294static inline void pm_runtime_forbid(struct device *dev) {} 295 296static inline int devm_pm_runtime_set_active_enabled(struct device *dev) { return 0; } 297static inline int devm_pm_runtime_enable(struct device *dev) { return 0; } 298static inline int devm_pm_runtime_get_noresume(struct device *dev) { return 0; } 299 300static inline void pm_suspend_ignore_children(struct device *dev, bool enable) {} 301static inline void pm_runtime_get_noresume(struct device *dev) {} 302static inline void pm_runtime_put_noidle(struct device *dev) {} 303static inline bool pm_runtime_suspended(struct device *dev) { return false; } 304static inline bool pm_runtime_active(struct device *dev) { return true; } 305static inline bool pm_runtime_status_suspended(struct device *dev) { return false; } 306static inline bool pm_runtime_enabled(struct device *dev) { return false; } 307 308static inline void pm_runtime_no_callbacks(struct device *dev) {} 309static inline void pm_runtime_irq_safe(struct device *dev) {} 310static inline bool pm_runtime_is_irq_safe(struct device *dev) { return false; } 311 312static inline bool pm_runtime_has_no_callbacks(struct device *dev) { return false; } 313static inline void pm_runtime_mark_last_busy(struct device *dev) {} 314static inline void __pm_runtime_use_autosuspend(struct device *dev, 315 bool use) {} 316static inline void pm_runtime_set_autosuspend_delay(struct device *dev, 317 int delay) {} 318static inline u64 pm_runtime_autosuspend_expiration( 319 struct device *dev) { return 0; } 320static inline void pm_runtime_set_memalloc_noio(struct device *dev, 321 bool enable){} 322static inline void pm_runtime_get_suppliers(struct device *dev) {} 323static inline void pm_runtime_put_suppliers(struct device *dev) {} 324static inline void pm_runtime_new_link(struct device *dev) {} 325static inline void pm_runtime_drop_link(struct device_link *link) {} 326static inline void pm_runtime_release_supplier(struct device_link *link) {} 327 328#endif /* !CONFIG_PM */ 329 330#ifdef CONFIG_PM_SLEEP 331 332bool pm_runtime_need_not_resume(struct device *dev); 333int pm_runtime_force_resume(struct device *dev); 334 335#else /* !CONFIG_PM_SLEEP */ 336 337static inline bool pm_runtime_need_not_resume(struct device *dev) {return true; } 338static inline int pm_runtime_force_resume(struct device *dev) { return -ENXIO; } 339 340#endif /* CONFIG_PM_SLEEP */ 341 342/** 343 * pm_runtime_idle - Conditionally set up autosuspend of a device or suspend it. 344 * @dev: Target device. 345 * 346 * Invoke the "idle check" callback of @dev and, depending on its return value, 347 * set up autosuspend of @dev or suspend it (depending on whether or not 348 * autosuspend has been enabled for it). 349 * 350 * Return: 351 * * 0: Success. 352 * * -EINVAL: Runtime PM error. 353 * * -EACCES: Runtime PM disabled. 354 * * -EAGAIN: Runtime PM usage counter non-zero, Runtime PM status change 355 * ongoing or device not in %RPM_ACTIVE state. 356 * * -EBUSY: Runtime PM child_count non-zero. 357 * * -EPERM: Device PM QoS resume latency 0. 358 * * -EINPROGRESS: Suspend already in progress. 359 * * -ENOSYS: CONFIG_PM not enabled. 360 * Other values and conditions for the above values are possible as returned by 361 * Runtime PM idle and suspend callbacks. 362 */ 363static inline int pm_runtime_idle(struct device *dev) 364{ 365 return __pm_runtime_idle(dev, 0); 366} 367 368/** 369 * pm_runtime_suspend - Suspend a device synchronously. 370 * @dev: Target device. 371 * 372 * Return: 373 * * 1: Success; device was already suspended. 374 * * 0: Success. 375 * * -EINVAL: Runtime PM error. 376 * * -EACCES: Runtime PM disabled. 377 * * -EAGAIN: Runtime PM usage counter non-zero or Runtime PM status change 378 * ongoing. 379 * * -EBUSY: Runtime PM child_count non-zero. 380 * * -EPERM: Device PM QoS resume latency 0. 381 * * -ENOSYS: CONFIG_PM not enabled. 382 * Other values and conditions for the above values are possible as returned by 383 * Runtime PM suspend callbacks. 384 */ 385static inline int pm_runtime_suspend(struct device *dev) 386{ 387 return __pm_runtime_suspend(dev, 0); 388} 389 390/** 391 * pm_runtime_autosuspend - Update the last access time and set up autosuspend 392 * of a device. 393 * @dev: Target device. 394 * 395 * First update the last access time, then set up autosuspend of @dev or suspend 396 * it (depending on whether or not autosuspend is enabled for it) without 397 * engaging its "idle check" callback. 398 * 399 * Return: 400 * * 1: Success; device was already suspended. 401 * * 0: Success. 402 * * -EINVAL: Runtime PM error. 403 * * -EACCES: Runtime PM disabled. 404 * * -EAGAIN: Runtime PM usage counter non-zero or Runtime PM status change 405 * ongoing. 406 * * -EBUSY: Runtime PM child_count non-zero. 407 * * -EPERM: Device PM QoS resume latency 0. 408 * * -ENOSYS: CONFIG_PM not enabled. 409 * Other values and conditions for the above values are possible as returned by 410 * Runtime PM suspend callbacks. 411 */ 412static inline int pm_runtime_autosuspend(struct device *dev) 413{ 414 pm_runtime_mark_last_busy(dev); 415 return __pm_runtime_suspend(dev, RPM_AUTO); 416} 417 418/** 419 * pm_runtime_resume - Resume a device synchronously. 420 * @dev: Target device. 421 */ 422static inline int pm_runtime_resume(struct device *dev) 423{ 424 return __pm_runtime_resume(dev, 0); 425} 426 427/** 428 * pm_request_idle - Queue up "idle check" execution for a device. 429 * @dev: Target device. 430 * 431 * Queue up a work item to run an equivalent of pm_runtime_idle() for @dev 432 * asynchronously. 433 * 434 * Return: 435 * * 0: Success. 436 * * -EINVAL: Runtime PM error. 437 * * -EACCES: Runtime PM disabled. 438 * * -EAGAIN: Runtime PM usage counter non-zero, Runtime PM status change 439 * ongoing or device not in %RPM_ACTIVE state. 440 * * -EBUSY: Runtime PM child_count non-zero. 441 * * -EPERM: Device PM QoS resume latency 0. 442 * * -EINPROGRESS: Suspend already in progress. 443 * * -ENOSYS: CONFIG_PM not enabled. 444 */ 445static inline int pm_request_idle(struct device *dev) 446{ 447 return __pm_runtime_idle(dev, RPM_ASYNC); 448} 449 450/** 451 * pm_request_resume - Queue up runtime-resume of a device. 452 * @dev: Target device. 453 */ 454static inline int pm_request_resume(struct device *dev) 455{ 456 return __pm_runtime_resume(dev, RPM_ASYNC); 457} 458 459/** 460 * pm_request_autosuspend - Update the last access time and queue up autosuspend 461 * of a device. 462 * @dev: Target device. 463 * 464 * Update the last access time of a device and queue up a work item to run an 465 * equivalent pm_runtime_autosuspend() for @dev asynchronously. 466 * 467 * Return: 468 * * 1: Success; device was already suspended. 469 * * 0: Success. 470 * * -EINVAL: Runtime PM error. 471 * * -EACCES: Runtime PM disabled. 472 * * -EAGAIN: Runtime PM usage counter non-zero or Runtime PM status change 473 * ongoing. 474 * * -EBUSY: Runtime PM child_count non-zero. 475 * * -EPERM: Device PM QoS resume latency 0. 476 * * -EINPROGRESS: Suspend already in progress. 477 * * -ENOSYS: CONFIG_PM not enabled. 478 */ 479static inline int pm_request_autosuspend(struct device *dev) 480{ 481 pm_runtime_mark_last_busy(dev); 482 return __pm_runtime_suspend(dev, RPM_ASYNC | RPM_AUTO); 483} 484 485/** 486 * pm_runtime_get - Bump up usage counter and queue up resume of a device. 487 * @dev: Target device. 488 * 489 * Bump up the runtime PM usage counter of @dev and queue up a work item to 490 * carry out runtime-resume of it. 491 */ 492static inline int pm_runtime_get(struct device *dev) 493{ 494 return __pm_runtime_resume(dev, RPM_GET_PUT | RPM_ASYNC); 495} 496 497/** 498 * pm_runtime_get_sync - Bump up usage counter of a device and resume it. 499 * @dev: Target device. 500 * 501 * Bump up the runtime PM usage counter of @dev and carry out runtime-resume of 502 * it synchronously. 503 * 504 * The possible return values of this function are the same as for 505 * pm_runtime_resume() and the runtime PM usage counter of @dev remains 506 * incremented in all cases, even if it returns an error code. 507 * Consider using pm_runtime_resume_and_get() instead of it, especially 508 * if its return value is checked by the caller, as this is likely to result 509 * in cleaner code. 510 */ 511static inline int pm_runtime_get_sync(struct device *dev) 512{ 513 return __pm_runtime_resume(dev, RPM_GET_PUT); 514} 515 516static inline int pm_runtime_get_active(struct device *dev, int rpmflags) 517{ 518 int ret; 519 520 ret = __pm_runtime_resume(dev, RPM_GET_PUT | rpmflags); 521 if (ret < 0) { 522 pm_runtime_put_noidle(dev); 523 return ret; 524 } 525 526 return 0; 527} 528 529/** 530 * pm_runtime_resume_and_get - Bump up usage counter of a device and resume it. 531 * @dev: Target device. 532 * 533 * Resume @dev synchronously and if that is successful, increment its runtime 534 * PM usage counter. Return 0 if the runtime PM usage counter of @dev has been 535 * incremented or a negative error code otherwise. 536 */ 537static inline int pm_runtime_resume_and_get(struct device *dev) 538{ 539 return pm_runtime_get_active(dev, 0); 540} 541 542/** 543 * pm_runtime_put - Drop device usage counter and queue up "idle check" if 0. 544 * @dev: Target device. 545 * 546 * Decrement the runtime PM usage counter of @dev and if it turns out to be 547 * equal to 0, queue up a work item for @dev like in pm_request_idle(). 548 */ 549static inline void pm_runtime_put(struct device *dev) 550{ 551 __pm_runtime_idle(dev, RPM_GET_PUT | RPM_ASYNC); 552} 553 554/** 555 * __pm_runtime_put_autosuspend - Drop device usage counter and queue autosuspend if 0. 556 * @dev: Target device. 557 * 558 * Decrement the runtime PM usage counter of @dev and if it turns out to be 559 * equal to 0, queue up a work item for @dev like in pm_request_autosuspend(). 560 * 561 * Return: 562 * * 1: Success. Usage counter dropped to zero, but device was already suspended. 563 * * 0: Success. 564 * * -EINVAL: Runtime PM error. 565 * * -EACCES: Runtime PM disabled. 566 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status 567 * change ongoing. 568 * * -EBUSY: Runtime PM child_count non-zero. 569 * * -EPERM: Device PM QoS resume latency 0. 570 * * -EINPROGRESS: Suspend already in progress. 571 * * -ENOSYS: CONFIG_PM not enabled. 572 */ 573static inline int __pm_runtime_put_autosuspend(struct device *dev) 574{ 575 return __pm_runtime_suspend(dev, RPM_GET_PUT | RPM_ASYNC | RPM_AUTO); 576} 577 578/** 579 * pm_runtime_put_autosuspend - Update the last access time of a device, drop 580 * its usage counter and queue autosuspend if the usage counter becomes 0. 581 * @dev: Target device. 582 * 583 * Update the last access time of @dev, decrement runtime PM usage counter of 584 * @dev and if it turns out to be equal to 0, queue up a work item for @dev like 585 * in pm_request_autosuspend(). 586 * 587 * Return: 588 * * 1: Success. Usage counter dropped to zero, but device was already suspended. 589 * * 0: Success. 590 * * -EINVAL: Runtime PM error. 591 * * -EACCES: Runtime PM disabled. 592 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status 593 * change ongoing. 594 * * -EBUSY: Runtime PM child_count non-zero. 595 * * -EPERM: Device PM QoS resume latency 0. 596 * * -EINPROGRESS: Suspend already in progress. 597 * * -ENOSYS: CONFIG_PM not enabled. 598 */ 599static inline int pm_runtime_put_autosuspend(struct device *dev) 600{ 601 pm_runtime_mark_last_busy(dev); 602 return __pm_runtime_put_autosuspend(dev); 603} 604 605DEFINE_GUARD(pm_runtime_noresume, struct device *, 606 pm_runtime_get_noresume(_T), pm_runtime_put_noidle(_T)); 607 608DEFINE_GUARD(pm_runtime_active, struct device *, 609 pm_runtime_get_sync(_T), pm_runtime_put(_T)); 610DEFINE_GUARD(pm_runtime_active_auto, struct device *, 611 pm_runtime_get_sync(_T), pm_runtime_put_autosuspend(_T)); 612/* 613 * Use the following guards with ACQUIRE()/ACQUIRE_ERR(). 614 * 615 * The difference between the "_try" and "_try_enabled" variants is that the 616 * former do not produce an error when runtime PM is disabled for the given 617 * device. 618 */ 619DEFINE_GUARD_COND(pm_runtime_active, _try, 620 pm_runtime_get_active(_T, RPM_TRANSPARENT), _RET == 0) 621DEFINE_GUARD_COND(pm_runtime_active, _try_enabled, 622 pm_runtime_resume_and_get(_T), _RET == 0) 623DEFINE_GUARD_COND(pm_runtime_active_auto, _try, 624 pm_runtime_get_active(_T, RPM_TRANSPARENT), _RET == 0) 625DEFINE_GUARD_COND(pm_runtime_active_auto, _try_enabled, 626 pm_runtime_resume_and_get(_T), _RET == 0) 627 628/* ACQUIRE() wrapper macros for the guards defined above. */ 629 630#define PM_RUNTIME_ACQUIRE(_dev, _var) \ 631 ACQUIRE(pm_runtime_active_try, _var)(_dev) 632 633#define PM_RUNTIME_ACQUIRE_AUTOSUSPEND(_dev, _var) \ 634 ACQUIRE(pm_runtime_active_auto_try, _var)(_dev) 635 636#define PM_RUNTIME_ACQUIRE_IF_ENABLED(_dev, _var) \ 637 ACQUIRE(pm_runtime_active_try_enabled, _var)(_dev) 638 639#define PM_RUNTIME_ACQUIRE_IF_ENABLED_AUTOSUSPEND(_dev, _var) \ 640 ACQUIRE(pm_runtime_active_auto_try_enabled, _var)(_dev) 641 642/* 643 * ACQUIRE_ERR() wrapper macro for guard pm_runtime_active. 644 * 645 * Always check PM_RUNTIME_ACQUIRE_ERR() after using one of the 646 * PM_RUNTIME_ACQUIRE*() macros defined above (yes, it can be used with 647 * any of them) and if it is nonzero, avoid accessing the given device. 648 */ 649#define PM_RUNTIME_ACQUIRE_ERR(_var_ptr) \ 650 ACQUIRE_ERR(pm_runtime_active, _var_ptr) 651 652/** 653 * pm_runtime_put_sync - Drop device usage counter and run "idle check" if 0. 654 * @dev: Target device. 655 * 656 * Decrement the runtime PM usage counter of @dev and if it turns out to be 657 * equal to 0, invoke the "idle check" callback of @dev and, depending on its 658 * return value, set up autosuspend of @dev or suspend it (depending on whether 659 * or not autosuspend has been enabled for it). 660 * 661 * The runtime PM usage counter of @dev remains decremented in all cases, even 662 * if it returns an error code. 663 * 664 * Return: 665 * * 1: Success. Usage counter dropped to zero, but device was already suspended. 666 * * 0: Success. 667 * * -EINVAL: Runtime PM error. 668 * * -EACCES: Runtime PM disabled. 669 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status 670 * change ongoing. 671 * * -EBUSY: Runtime PM child_count non-zero. 672 * * -EPERM: Device PM QoS resume latency 0. 673 * * -ENOSYS: CONFIG_PM not enabled. 674 * Other values and conditions for the above values are possible as returned by 675 * Runtime PM suspend callbacks. 676 */ 677static inline int pm_runtime_put_sync(struct device *dev) 678{ 679 return __pm_runtime_idle(dev, RPM_GET_PUT); 680} 681 682/** 683 * pm_runtime_put_sync_suspend - Drop device usage counter and suspend if 0. 684 * @dev: Target device. 685 * 686 * Decrement the runtime PM usage counter of @dev and if it turns out to be 687 * equal to 0, carry out runtime-suspend of @dev synchronously. 688 * 689 * The runtime PM usage counter of @dev remains decremented in all cases, even 690 * if it returns an error code. 691 * 692 * Return: 693 * * 1: Success. Usage counter dropped to zero, but device was already suspended. 694 * * 0: Success. 695 * * -EINVAL: Runtime PM error. 696 * * -EACCES: Runtime PM disabled. 697 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status 698 * change ongoing. 699 * * -EBUSY: Runtime PM child_count non-zero. 700 * * -EPERM: Device PM QoS resume latency 0. 701 * * -ENOSYS: CONFIG_PM not enabled. 702 * Other values and conditions for the above values are possible as returned by 703 * Runtime PM suspend callbacks. 704 */ 705static inline int pm_runtime_put_sync_suspend(struct device *dev) 706{ 707 return __pm_runtime_suspend(dev, RPM_GET_PUT); 708} 709 710/** 711 * pm_runtime_put_sync_autosuspend - Update the last access time of a device, 712 * drop device usage counter and autosuspend if 0. 713 * @dev: Target device. 714 * 715 * Update the last access time of @dev, decrement the runtime PM usage counter 716 * of @dev and if it turns out to be equal to 0, set up autosuspend of @dev or 717 * suspend it synchronously (depending on whether or not autosuspend has been 718 * enabled for it). 719 * 720 * The runtime PM usage counter of @dev remains decremented in all cases, even 721 * if it returns an error code. 722 * 723 * Return: 724 * * 1: Success. Usage counter dropped to zero, but device was already suspended. 725 * * 0: Success. 726 * * -EINVAL: Runtime PM error. 727 * * -EACCES: Runtime PM disabled. 728 * * -EAGAIN: Runtime PM usage counter became non-zero or Runtime PM status 729 * change ongoing. 730 * * -EBUSY: Runtime PM child_count non-zero. 731 * * -EPERM: Device PM QoS resume latency 0. 732 * * -EINPROGRESS: Suspend already in progress. 733 * * -ENOSYS: CONFIG_PM not enabled. 734 * Other values and conditions for the above values are possible as returned by 735 * Runtime PM suspend callbacks. 736 */ 737static inline int pm_runtime_put_sync_autosuspend(struct device *dev) 738{ 739 pm_runtime_mark_last_busy(dev); 740 return __pm_runtime_suspend(dev, RPM_GET_PUT | RPM_AUTO); 741} 742 743/** 744 * pm_runtime_set_active - Set runtime PM status to "active". 745 * @dev: Target device. 746 * 747 * Set the runtime PM status of @dev to %RPM_ACTIVE and ensure that dependencies 748 * of it will be taken into account. 749 * 750 * It is not valid to call this function for devices with runtime PM enabled. 751 */ 752static inline int pm_runtime_set_active(struct device *dev) 753{ 754 return __pm_runtime_set_status(dev, RPM_ACTIVE); 755} 756 757/** 758 * pm_runtime_set_suspended - Set runtime PM status to "suspended". 759 * @dev: Target device. 760 * 761 * Set the runtime PM status of @dev to %RPM_SUSPENDED and ensure that 762 * dependencies of it will be taken into account. 763 * 764 * It is not valid to call this function for devices with runtime PM enabled. 765 */ 766static inline int pm_runtime_set_suspended(struct device *dev) 767{ 768 return __pm_runtime_set_status(dev, RPM_SUSPENDED); 769} 770 771/** 772 * pm_runtime_disable - Disable runtime PM for a device. 773 * @dev: Target device. 774 * 775 * Prevent the runtime PM framework from working with @dev by incrementing its 776 * "disable" counter. 777 * 778 * If the counter is zero when this function runs and there is a pending runtime 779 * resume request for @dev, it will be resumed. If the counter is still zero at 780 * that point, all of the pending runtime PM requests for @dev will be canceled 781 * and all runtime PM operations in progress involving it will be waited for to 782 * complete. 783 * 784 * For each invocation of this function for @dev, there must be a matching 785 * pm_runtime_enable() call, so that runtime PM is eventually enabled for it 786 * again. 787 */ 788static inline void pm_runtime_disable(struct device *dev) 789{ 790 __pm_runtime_disable(dev, true); 791} 792 793/** 794 * pm_runtime_use_autosuspend - Allow autosuspend to be used for a device. 795 * @dev: Target device. 796 * 797 * Allow the runtime PM autosuspend mechanism to be used for @dev whenever 798 * requested (or "autosuspend" will be handled as direct runtime-suspend for 799 * it). 800 * 801 * NOTE: It's important to undo this with pm_runtime_dont_use_autosuspend() 802 * at driver exit time unless your driver initially enabled pm_runtime 803 * with devm_pm_runtime_enable() (which handles it for you). 804 */ 805static inline void pm_runtime_use_autosuspend(struct device *dev) 806{ 807 __pm_runtime_use_autosuspend(dev, true); 808} 809 810/** 811 * pm_runtime_dont_use_autosuspend - Prevent autosuspend from being used. 812 * @dev: Target device. 813 * 814 * Prevent the runtime PM autosuspend mechanism from being used for @dev which 815 * means that "autosuspend" will be handled as direct runtime-suspend for it 816 * going forward. 817 */ 818static inline void pm_runtime_dont_use_autosuspend(struct device *dev) 819{ 820 __pm_runtime_use_autosuspend(dev, false); 821} 822 823#endif