pm.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * pm.h - Power management interface
  3. *
  4. * Copyright (C) 2000 Andrew Henroid
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef _LINUX_PM_H
  21. #define _LINUX_PM_H
  22. #include <linux/list.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/wait.h>
  26. #include <linux/timer.h>
  27. /*
  28. * Callbacks for platform drivers to implement.
  29. */
  30. extern void (*pm_idle)(void);
  31. extern void (*pm_power_off)(void);
  32. extern void (*pm_power_off_prepare)(void);
  33. /*
  34. * Device power management
  35. */
  36. struct device;
  37. typedef struct pm_message {
  38. int event;
  39. } pm_message_t;
  40. /**
  41. * struct dev_pm_ops - device PM callbacks
  42. *
  43. * Several driver power state transitions are externally visible, affecting
  44. * the state of pending I/O queues and (for drivers that touch hardware)
  45. * interrupts, wakeups, DMA, and other hardware state. There may also be
  46. * internal transitions to various low power modes, which are transparent
  47. * to the rest of the driver stack (such as a driver that's ON gating off
  48. * clocks which are not in active use).
  49. *
  50. * The externally visible transitions are handled with the help of the following
  51. * callbacks included in this structure:
  52. *
  53. * @prepare: Prepare the device for the upcoming transition, but do NOT change
  54. * its hardware state. Prevent new children of the device from being
  55. * registered after @prepare() returns (the driver's subsystem and
  56. * generally the rest of the kernel is supposed to prevent new calls to the
  57. * probe method from being made too once @prepare() has succeeded). If
  58. * @prepare() detects a situation it cannot handle (e.g. registration of a
  59. * child already in progress), it may return -EAGAIN, so that the PM core
  60. * can execute it once again (e.g. after the new child has been registered)
  61. * to recover from the race condition. This method is executed for all
  62. * kinds of suspend transitions and is followed by one of the suspend
  63. * callbacks: @suspend(), @freeze(), or @poweroff().
  64. * The PM core executes @prepare() for all devices before starting to
  65. * execute suspend callbacks for any of them, so drivers may assume all of
  66. * the other devices to be present and functional while @prepare() is being
  67. * executed. In particular, it is safe to make GFP_KERNEL memory
  68. * allocations from within @prepare(). However, drivers may NOT assume
  69. * anything about the availability of the user space at that time and it
  70. * is not correct to request firmware from within @prepare() (it's too
  71. * late to do that). [To work around this limitation, drivers may
  72. * register suspend and hibernation notifiers that are executed before the
  73. * freezing of tasks.]
  74. *
  75. * @complete: Undo the changes made by @prepare(). This method is executed for
  76. * all kinds of resume transitions, following one of the resume callbacks:
  77. * @resume(), @thaw(), @restore(). Also called if the state transition
  78. * fails before the driver's suspend callback (@suspend(), @freeze(),
  79. * @poweroff()) can be executed (e.g. if the suspend callback fails for one
  80. * of the other devices that the PM core has unsuccessfully attempted to
  81. * suspend earlier).
  82. * The PM core executes @complete() after it has executed the appropriate
  83. * resume callback for all devices.
  84. *
  85. * @suspend: Executed before putting the system into a sleep state in which the
  86. * contents of main memory are preserved. Quiesce the device, put it into
  87. * a low power state appropriate for the upcoming system state (such as
  88. * PCI_D3hot), and enable wakeup events as appropriate.
  89. *
  90. * @resume: Executed after waking the system up from a sleep state in which the
  91. * contents of main memory were preserved. Put the device into the
  92. * appropriate state, according to the information saved in memory by the
  93. * preceding @suspend(). The driver starts working again, responding to
  94. * hardware events and software requests. The hardware may have gone
  95. * through a power-off reset, or it may have maintained state from the
  96. * previous suspend() which the driver may rely on while resuming. On most
  97. * platforms, there are no restrictions on availability of resources like
  98. * clocks during @resume().
  99. *
  100. * @freeze: Hibernation-specific, executed before creating a hibernation image.
  101. * Quiesce operations so that a consistent image can be created, but do NOT
  102. * otherwise put the device into a low power device state and do NOT emit
  103. * system wakeup events. Save in main memory the device settings to be
  104. * used by @restore() during the subsequent resume from hibernation or by
  105. * the subsequent @thaw(), if the creation of the image or the restoration
  106. * of main memory contents from it fails.
  107. *
  108. * @thaw: Hibernation-specific, executed after creating a hibernation image OR
  109. * if the creation of the image fails. Also executed after a failing
  110. * attempt to restore the contents of main memory from such an image.
  111. * Undo the changes made by the preceding @freeze(), so the device can be
  112. * operated in the same way as immediately before the call to @freeze().
  113. *
  114. * @poweroff: Hibernation-specific, executed after saving a hibernation image.
  115. * Quiesce the device, put it into a low power state appropriate for the
  116. * upcoming system state (such as PCI_D3hot), and enable wakeup events as
  117. * appropriate.
  118. *
  119. * @restore: Hibernation-specific, executed after restoring the contents of main
  120. * memory from a hibernation image. Driver starts working again,
  121. * responding to hardware events and software requests. Drivers may NOT
  122. * make ANY assumptions about the hardware state right prior to @restore().
  123. * On most platforms, there are no restrictions on availability of
  124. * resources like clocks during @restore().
  125. *
  126. * @suspend_noirq: Complete the operations of ->suspend() by carrying out any
  127. * actions required for suspending the device that need interrupts to be
  128. * disabled
  129. *
  130. * @resume_noirq: Prepare for the execution of ->resume() by carrying out any
  131. * actions required for resuming the device that need interrupts to be
  132. * disabled
  133. *
  134. * @freeze_noirq: Complete the operations of ->freeze() by carrying out any
  135. * actions required for freezing the device that need interrupts to be
  136. * disabled
  137. *
  138. * @thaw_noirq: Prepare for the execution of ->thaw() by carrying out any
  139. * actions required for thawing the device that need interrupts to be
  140. * disabled
  141. *
  142. * @poweroff_noirq: Complete the operations of ->poweroff() by carrying out any
  143. * actions required for handling the device that need interrupts to be
  144. * disabled
  145. *
  146. * @restore_noirq: Prepare for the execution of ->restore() by carrying out any
  147. * actions required for restoring the operations of the device that need
  148. * interrupts to be disabled
  149. *
  150. * All of the above callbacks, except for @complete(), return error codes.
  151. * However, the error codes returned by the resume operations, @resume(),
  152. * @thaw(), @restore(), @resume_noirq(), @thaw_noirq(), and @restore_noirq() do
  153. * not cause the PM core to abort the resume transition during which they are
  154. * returned. The error codes returned in that cases are only printed by the PM
  155. * core to the system logs for debugging purposes. Still, it is recommended
  156. * that drivers only return error codes from their resume methods in case of an
  157. * unrecoverable failure (i.e. when the device being handled refuses to resume
  158. * and becomes unusable) to allow us to modify the PM core in the future, so
  159. * that it can avoid attempting to handle devices that failed to resume and
  160. * their children.
  161. *
  162. * It is allowed to unregister devices while the above callbacks are being
  163. * executed. However, it is not allowed to unregister a device from within any
  164. * of its own callbacks.
  165. *
  166. * There also are the following callbacks related to run-time power management
  167. * of devices:
  168. *
  169. * @runtime_suspend: Prepare the device for a condition in which it won't be
  170. * able to communicate with the CPU(s) and RAM due to power management.
  171. * This need not mean that the device should be put into a low power state.
  172. * For example, if the device is behind a link which is about to be turned
  173. * off, the device may remain at full power. If the device does go to low
  174. * power and if device_may_wakeup(dev) is true, remote wake-up (i.e., a
  175. * hardware mechanism allowing the device to request a change of its power
  176. * state, such as PCI PME) should be enabled for it.
  177. *
  178. * @runtime_resume: Put the device into the fully active state in response to a
  179. * wake-up event generated by hardware or at the request of software. If
  180. * necessary, put the device into the full power state and restore its
  181. * registers, so that it is fully operational.
  182. *
  183. * @runtime_idle: Device appears to be inactive and it might be put into a low
  184. * power state if all of the necessary conditions are satisfied. Check
  185. * these conditions and handle the device as appropriate, possibly queueing
  186. * a suspend request for it. The return value is ignored by the PM core.
  187. */
  188. struct dev_pm_ops {
  189. int (*prepare)(struct device *dev);
  190. void (*complete)(struct device *dev);
  191. int (*suspend)(struct device *dev);
  192. int (*resume)(struct device *dev);
  193. int (*freeze)(struct device *dev);
  194. int (*thaw)(struct device *dev);
  195. int (*poweroff)(struct device *dev);
  196. int (*restore)(struct device *dev);
  197. int (*suspend_noirq)(struct device *dev);
  198. int (*resume_noirq)(struct device *dev);
  199. int (*freeze_noirq)(struct device *dev);
  200. int (*thaw_noirq)(struct device *dev);
  201. int (*poweroff_noirq)(struct device *dev);
  202. int (*restore_noirq)(struct device *dev);
  203. int (*runtime_suspend)(struct device *dev);
  204. int (*runtime_resume)(struct device *dev);
  205. int (*runtime_idle)(struct device *dev);
  206. };
  207. /**
  208. * PM_EVENT_ messages
  209. *
  210. * The following PM_EVENT_ messages are defined for the internal use of the PM
  211. * core, in order to provide a mechanism allowing the high level suspend and
  212. * hibernation code to convey the necessary information to the device PM core
  213. * code:
  214. *
  215. * ON No transition.
  216. *
  217. * FREEZE System is going to hibernate, call ->prepare() and ->freeze()
  218. * for all devices.
  219. *
  220. * SUSPEND System is going to suspend, call ->prepare() and ->suspend()
  221. * for all devices.
  222. *
  223. * HIBERNATE Hibernation image has been saved, call ->prepare() and
  224. * ->poweroff() for all devices.
  225. *
  226. * QUIESCE Contents of main memory are going to be restored from a (loaded)
  227. * hibernation image, call ->prepare() and ->freeze() for all
  228. * devices.
  229. *
  230. * RESUME System is resuming, call ->resume() and ->complete() for all
  231. * devices.
  232. *
  233. * THAW Hibernation image has been created, call ->thaw() and
  234. * ->complete() for all devices.
  235. *
  236. * RESTORE Contents of main memory have been restored from a hibernation
  237. * image, call ->restore() and ->complete() for all devices.
  238. *
  239. * RECOVER Creation of a hibernation image or restoration of the main
  240. * memory contents from a hibernation image has failed, call
  241. * ->thaw() and ->complete() for all devices.
  242. *
  243. * The following PM_EVENT_ messages are defined for internal use by
  244. * kernel subsystems. They are never issued by the PM core.
  245. *
  246. * USER_SUSPEND Manual selective suspend was issued by userspace.
  247. *
  248. * USER_RESUME Manual selective resume was issued by userspace.
  249. *
  250. * REMOTE_WAKEUP Remote-wakeup request was received from the device.
  251. *
  252. * AUTO_SUSPEND Automatic (device idle) runtime suspend was
  253. * initiated by the subsystem.
  254. *
  255. * AUTO_RESUME Automatic (device needed) runtime resume was
  256. * requested by a driver.
  257. */
  258. #define PM_EVENT_ON 0x0000
  259. #define PM_EVENT_FREEZE 0x0001
  260. #define PM_EVENT_SUSPEND 0x0002
  261. #define PM_EVENT_HIBERNATE 0x0004
  262. #define PM_EVENT_QUIESCE 0x0008
  263. #define PM_EVENT_RESUME 0x0010
  264. #define PM_EVENT_THAW 0x0020
  265. #define PM_EVENT_RESTORE 0x0040
  266. #define PM_EVENT_RECOVER 0x0080
  267. #define PM_EVENT_USER 0x0100
  268. #define PM_EVENT_REMOTE 0x0200
  269. #define PM_EVENT_AUTO 0x0400
  270. #define PM_EVENT_SLEEP (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE)
  271. #define PM_EVENT_USER_SUSPEND (PM_EVENT_USER | PM_EVENT_SUSPEND)
  272. #define PM_EVENT_USER_RESUME (PM_EVENT_USER | PM_EVENT_RESUME)
  273. #define PM_EVENT_REMOTE_RESUME (PM_EVENT_REMOTE | PM_EVENT_RESUME)
  274. #define PM_EVENT_AUTO_SUSPEND (PM_EVENT_AUTO | PM_EVENT_SUSPEND)
  275. #define PM_EVENT_AUTO_RESUME (PM_EVENT_AUTO | PM_EVENT_RESUME)
  276. #define PMSG_ON ((struct pm_message){ .event = PM_EVENT_ON, })
  277. #define PMSG_FREEZE ((struct pm_message){ .event = PM_EVENT_FREEZE, })
  278. #define PMSG_QUIESCE ((struct pm_message){ .event = PM_EVENT_QUIESCE, })
  279. #define PMSG_SUSPEND ((struct pm_message){ .event = PM_EVENT_SUSPEND, })
  280. #define PMSG_HIBERNATE ((struct pm_message){ .event = PM_EVENT_HIBERNATE, })
  281. #define PMSG_RESUME ((struct pm_message){ .event = PM_EVENT_RESUME, })
  282. #define PMSG_THAW ((struct pm_message){ .event = PM_EVENT_THAW, })
  283. #define PMSG_RESTORE ((struct pm_message){ .event = PM_EVENT_RESTORE, })
  284. #define PMSG_RECOVER ((struct pm_message){ .event = PM_EVENT_RECOVER, })
  285. #define PMSG_USER_SUSPEND ((struct pm_message) \
  286. { .event = PM_EVENT_USER_SUSPEND, })
  287. #define PMSG_USER_RESUME ((struct pm_message) \
  288. { .event = PM_EVENT_USER_RESUME, })
  289. #define PMSG_REMOTE_RESUME ((struct pm_message) \
  290. { .event = PM_EVENT_REMOTE_RESUME, })
  291. #define PMSG_AUTO_SUSPEND ((struct pm_message) \
  292. { .event = PM_EVENT_AUTO_SUSPEND, })
  293. #define PMSG_AUTO_RESUME ((struct pm_message) \
  294. { .event = PM_EVENT_AUTO_RESUME, })
  295. /**
  296. * Device power management states
  297. *
  298. * These state labels are used internally by the PM core to indicate the current
  299. * status of a device with respect to the PM core operations.
  300. *
  301. * DPM_ON Device is regarded as operational. Set this way
  302. * initially and when ->complete() is about to be called.
  303. * Also set when ->prepare() fails.
  304. *
  305. * DPM_PREPARING Device is going to be prepared for a PM transition. Set
  306. * when ->prepare() is about to be called.
  307. *
  308. * DPM_RESUMING Device is going to be resumed. Set when ->resume(),
  309. * ->thaw(), or ->restore() is about to be called.
  310. *
  311. * DPM_SUSPENDING Device has been prepared for a power transition. Set
  312. * when ->prepare() has just succeeded.
  313. *
  314. * DPM_OFF Device is regarded as inactive. Set immediately after
  315. * ->suspend(), ->freeze(), or ->poweroff() has succeeded.
  316. * Also set when ->resume()_noirq, ->thaw_noirq(), or
  317. * ->restore_noirq() is about to be called.
  318. *
  319. * DPM_OFF_IRQ Device is in a "deep sleep". Set immediately after
  320. * ->suspend_noirq(), ->freeze_noirq(), or
  321. * ->poweroff_noirq() has just succeeded.
  322. */
  323. enum dpm_state {
  324. DPM_INVALID,
  325. DPM_ON,
  326. DPM_PREPARING,
  327. DPM_RESUMING,
  328. DPM_SUSPENDING,
  329. DPM_OFF,
  330. DPM_OFF_IRQ,
  331. };
  332. /**
  333. * Device run-time power management status.
  334. *
  335. * These status labels are used internally by the PM core to indicate the
  336. * current status of a device with respect to the PM core operations. They do
  337. * not reflect the actual power state of the device or its status as seen by the
  338. * driver.
  339. *
  340. * RPM_ACTIVE Device is fully operational. Indicates that the device
  341. * bus type's ->runtime_resume() callback has completed
  342. * successfully.
  343. *
  344. * RPM_SUSPENDED Device bus type's ->runtime_suspend() callback has
  345. * completed successfully. The device is regarded as
  346. * suspended.
  347. *
  348. * RPM_RESUMING Device bus type's ->runtime_resume() callback is being
  349. * executed.
  350. *
  351. * RPM_SUSPENDING Device bus type's ->runtime_suspend() callback is being
  352. * executed.
  353. */
  354. enum rpm_status {
  355. RPM_ACTIVE = 0,
  356. RPM_RESUMING,
  357. RPM_SUSPENDED,
  358. RPM_SUSPENDING,
  359. };
  360. /**
  361. * Device run-time power management request types.
  362. *
  363. * RPM_REQ_NONE Do nothing.
  364. *
  365. * RPM_REQ_IDLE Run the device bus type's ->runtime_idle() callback
  366. *
  367. * RPM_REQ_SUSPEND Run the device bus type's ->runtime_suspend() callback
  368. *
  369. * RPM_REQ_RESUME Run the device bus type's ->runtime_resume() callback
  370. */
  371. enum rpm_request {
  372. RPM_REQ_NONE = 0,
  373. RPM_REQ_IDLE,
  374. RPM_REQ_SUSPEND,
  375. RPM_REQ_RESUME,
  376. };
  377. struct dev_pm_info {
  378. pm_message_t power_state;
  379. unsigned int can_wakeup:1;
  380. unsigned int should_wakeup:1;
  381. enum dpm_state status; /* Owned by the PM core */
  382. #ifdef CONFIG_PM_SLEEP
  383. struct list_head entry;
  384. #endif
  385. #ifdef CONFIG_PM_RUNTIME
  386. struct timer_list suspend_timer;
  387. unsigned long timer_expires;
  388. struct work_struct work;
  389. wait_queue_head_t wait_queue;
  390. spinlock_t lock;
  391. atomic_t usage_count;
  392. atomic_t child_count;
  393. unsigned int disable_depth:3;
  394. unsigned int ignore_children:1;
  395. unsigned int idle_notification:1;
  396. unsigned int request_pending:1;
  397. unsigned int deferred_resume:1;
  398. enum rpm_request request;
  399. enum rpm_status runtime_status;
  400. int runtime_error;
  401. #endif
  402. };
  403. /*
  404. * The PM_EVENT_ messages are also used by drivers implementing the legacy
  405. * suspend framework, based on the ->suspend() and ->resume() callbacks common
  406. * for suspend and hibernation transitions, according to the rules below.
  407. */
  408. /* Necessary, because several drivers use PM_EVENT_PRETHAW */
  409. #define PM_EVENT_PRETHAW PM_EVENT_QUIESCE
  410. /*
  411. * One transition is triggered by resume(), after a suspend() call; the
  412. * message is implicit:
  413. *
  414. * ON Driver starts working again, responding to hardware events
  415. * and software requests. The hardware may have gone through
  416. * a power-off reset, or it may have maintained state from the
  417. * previous suspend() which the driver will rely on while
  418. * resuming. On most platforms, there are no restrictions on
  419. * availability of resources like clocks during resume().
  420. *
  421. * Other transitions are triggered by messages sent using suspend(). All
  422. * these transitions quiesce the driver, so that I/O queues are inactive.
  423. * That commonly entails turning off IRQs and DMA; there may be rules
  424. * about how to quiesce that are specific to the bus or the device's type.
  425. * (For example, network drivers mark the link state.) Other details may
  426. * differ according to the message:
  427. *
  428. * SUSPEND Quiesce, enter a low power device state appropriate for
  429. * the upcoming system state (such as PCI_D3hot), and enable
  430. * wakeup events as appropriate.
  431. *
  432. * HIBERNATE Enter a low power device state appropriate for the hibernation
  433. * state (eg. ACPI S4) and enable wakeup events as appropriate.
  434. *
  435. * FREEZE Quiesce operations so that a consistent image can be saved;
  436. * but do NOT otherwise enter a low power device state, and do
  437. * NOT emit system wakeup events.
  438. *
  439. * PRETHAW Quiesce as if for FREEZE; additionally, prepare for restoring
  440. * the system from a snapshot taken after an earlier FREEZE.
  441. * Some drivers will need to reset their hardware state instead
  442. * of preserving it, to ensure that it's never mistaken for the
  443. * state which that earlier snapshot had set up.
  444. *
  445. * A minimally power-aware driver treats all messages as SUSPEND, fully
  446. * reinitializes its device during resume() -- whether or not it was reset
  447. * during the suspend/resume cycle -- and can't issue wakeup events.
  448. *
  449. * More power-aware drivers may also use low power states at runtime as
  450. * well as during system sleep states like PM_SUSPEND_STANDBY. They may
  451. * be able to use wakeup events to exit from runtime low-power states,
  452. * or from system low-power states such as standby or suspend-to-RAM.
  453. */
  454. #ifdef CONFIG_PM_SLEEP
  455. extern void device_pm_lock(void);
  456. extern int sysdev_resume(void);
  457. extern void dpm_resume_noirq(pm_message_t state);
  458. extern void dpm_resume_end(pm_message_t state);
  459. extern void device_pm_unlock(void);
  460. extern int sysdev_suspend(pm_message_t state);
  461. extern int dpm_suspend_noirq(pm_message_t state);
  462. extern int dpm_suspend_start(pm_message_t state);
  463. extern void __suspend_report_result(const char *function, void *fn, int ret);
  464. #define suspend_report_result(fn, ret) \
  465. do { \
  466. __suspend_report_result(__func__, fn, ret); \
  467. } while (0)
  468. #else /* !CONFIG_PM_SLEEP */
  469. #define device_pm_lock() do {} while (0)
  470. #define device_pm_unlock() do {} while (0)
  471. static inline int dpm_suspend_start(pm_message_t state)
  472. {
  473. return 0;
  474. }
  475. #define suspend_report_result(fn, ret) do {} while (0)
  476. #endif /* !CONFIG_PM_SLEEP */
  477. /* How to reorder dpm_list after device_move() */
  478. enum dpm_order {
  479. DPM_ORDER_NONE,
  480. DPM_ORDER_DEV_AFTER_PARENT,
  481. DPM_ORDER_PARENT_BEFORE_DEV,
  482. DPM_ORDER_DEV_LAST,
  483. };
  484. /*
  485. * Global Power Management flags
  486. * Used to keep APM and ACPI from both being active
  487. */
  488. extern unsigned int pm_flags;
  489. #define PM_APM 1
  490. #define PM_ACPI 2
  491. #endif /* _LINUX_PM_H */