pm.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. /*
  24. * Callbacks for platform drivers to implement.
  25. */
  26. extern void (*pm_idle)(void);
  27. extern void (*pm_power_off)(void);
  28. extern void (*pm_power_off_prepare)(void);
  29. /*
  30. * Device power management
  31. */
  32. struct device;
  33. typedef struct pm_message {
  34. int event;
  35. } pm_message_t;
  36. /**
  37. * struct dev_pm_ops - device PM callbacks
  38. *
  39. * Several driver power state transitions are externally visible, affecting
  40. * the state of pending I/O queues and (for drivers that touch hardware)
  41. * interrupts, wakeups, DMA, and other hardware state. There may also be
  42. * internal transitions to various low power modes, which are transparent
  43. * to the rest of the driver stack (such as a driver that's ON gating off
  44. * clocks which are not in active use).
  45. *
  46. * The externally visible transitions are handled with the help of the following
  47. * callbacks included in this structure:
  48. *
  49. * @prepare: Prepare the device for the upcoming transition, but do NOT change
  50. * its hardware state. Prevent new children of the device from being
  51. * registered after @prepare() returns (the driver's subsystem and
  52. * generally the rest of the kernel is supposed to prevent new calls to the
  53. * probe method from being made too once @prepare() has succeeded). If
  54. * @prepare() detects a situation it cannot handle (e.g. registration of a
  55. * child already in progress), it may return -EAGAIN, so that the PM core
  56. * can execute it once again (e.g. after the new child has been registered)
  57. * to recover from the race condition. This method is executed for all
  58. * kinds of suspend transitions and is followed by one of the suspend
  59. * callbacks: @suspend(), @freeze(), or @poweroff().
  60. * The PM core executes @prepare() for all devices before starting to
  61. * execute suspend callbacks for any of them, so drivers may assume all of
  62. * the other devices to be present and functional while @prepare() is being
  63. * executed. In particular, it is safe to make GFP_KERNEL memory
  64. * allocations from within @prepare(). However, drivers may NOT assume
  65. * anything about the availability of the user space at that time and it
  66. * is not correct to request firmware from within @prepare() (it's too
  67. * late to do that). [To work around this limitation, drivers may
  68. * register suspend and hibernation notifiers that are executed before the
  69. * freezing of tasks.]
  70. *
  71. * @complete: Undo the changes made by @prepare(). This method is executed for
  72. * all kinds of resume transitions, following one of the resume callbacks:
  73. * @resume(), @thaw(), @restore(). Also called if the state transition
  74. * fails before the driver's suspend callback (@suspend(), @freeze(),
  75. * @poweroff()) can be executed (e.g. if the suspend callback fails for one
  76. * of the other devices that the PM core has unsuccessfully attempted to
  77. * suspend earlier).
  78. * The PM core executes @complete() after it has executed the appropriate
  79. * resume callback for all devices.
  80. *
  81. * @suspend: Executed before putting the system into a sleep state in which the
  82. * contents of main memory are preserved. Quiesce the device, put it into
  83. * a low power state appropriate for the upcoming system state (such as
  84. * PCI_D3hot), and enable wakeup events as appropriate.
  85. *
  86. * @resume: Executed after waking the system up from a sleep state in which the
  87. * contents of main memory were preserved. Put the device into the
  88. * appropriate state, according to the information saved in memory by the
  89. * preceding @suspend(). The driver starts working again, responding to
  90. * hardware events and software requests. The hardware may have gone
  91. * through a power-off reset, or it may have maintained state from the
  92. * previous suspend() which the driver may rely on while resuming. On most
  93. * platforms, there are no restrictions on availability of resources like
  94. * clocks during @resume().
  95. *
  96. * @freeze: Hibernation-specific, executed before creating a hibernation image.
  97. * Quiesce operations so that a consistent image can be created, but do NOT
  98. * otherwise put the device into a low power device state and do NOT emit
  99. * system wakeup events. Save in main memory the device settings to be
  100. * used by @restore() during the subsequent resume from hibernation or by
  101. * the subsequent @thaw(), if the creation of the image or the restoration
  102. * of main memory contents from it fails.
  103. *
  104. * @thaw: Hibernation-specific, executed after creating a hibernation image OR
  105. * if the creation of the image fails. Also executed after a failing
  106. * attempt to restore the contents of main memory from such an image.
  107. * Undo the changes made by the preceding @freeze(), so the device can be
  108. * operated in the same way as immediately before the call to @freeze().
  109. *
  110. * @poweroff: Hibernation-specific, executed after saving a hibernation image.
  111. * Quiesce the device, put it into a low power state appropriate for the
  112. * upcoming system state (such as PCI_D3hot), and enable wakeup events as
  113. * appropriate.
  114. *
  115. * @restore: Hibernation-specific, executed after restoring the contents of main
  116. * memory from a hibernation image. Driver starts working again,
  117. * responding to hardware events and software requests. Drivers may NOT
  118. * make ANY assumptions about the hardware state right prior to @restore().
  119. * On most platforms, there are no restrictions on availability of
  120. * resources like clocks during @restore().
  121. *
  122. * @suspend_noirq: Complete the operations of ->suspend() by carrying out any
  123. * actions required for suspending the device that need interrupts to be
  124. * disabled
  125. *
  126. * @resume_noirq: Prepare for the execution of ->resume() by carrying out any
  127. * actions required for resuming the device that need interrupts to be
  128. * disabled
  129. *
  130. * @freeze_noirq: Complete the operations of ->freeze() by carrying out any
  131. * actions required for freezing the device that need interrupts to be
  132. * disabled
  133. *
  134. * @thaw_noirq: Prepare for the execution of ->thaw() by carrying out any
  135. * actions required for thawing the device that need interrupts to be
  136. * disabled
  137. *
  138. * @poweroff_noirq: Complete the operations of ->poweroff() by carrying out any
  139. * actions required for handling the device that need interrupts to be
  140. * disabled
  141. *
  142. * @restore_noirq: Prepare for the execution of ->restore() by carrying out any
  143. * actions required for restoring the operations of the device that need
  144. * interrupts to be disabled
  145. *
  146. * All of the above callbacks, except for @complete(), return error codes.
  147. * However, the error codes returned by the resume operations, @resume(),
  148. * @thaw(), @restore(), @resume_noirq(), @thaw_noirq(), and @restore_noirq() do
  149. * not cause the PM core to abort the resume transition during which they are
  150. * returned. The error codes returned in that cases are only printed by the PM
  151. * core to the system logs for debugging purposes. Still, it is recommended
  152. * that drivers only return error codes from their resume methods in case of an
  153. * unrecoverable failure (i.e. when the device being handled refuses to resume
  154. * and becomes unusable) to allow us to modify the PM core in the future, so
  155. * that it can avoid attempting to handle devices that failed to resume and
  156. * their children.
  157. *
  158. * It is allowed to unregister devices while the above callbacks are being
  159. * executed. However, it is not allowed to unregister a device from within any
  160. * of its own callbacks.
  161. */
  162. struct dev_pm_ops {
  163. int (*prepare)(struct device *dev);
  164. void (*complete)(struct device *dev);
  165. int (*suspend)(struct device *dev);
  166. int (*resume)(struct device *dev);
  167. int (*freeze)(struct device *dev);
  168. int (*thaw)(struct device *dev);
  169. int (*poweroff)(struct device *dev);
  170. int (*restore)(struct device *dev);
  171. int (*suspend_noirq)(struct device *dev);
  172. int (*resume_noirq)(struct device *dev);
  173. int (*freeze_noirq)(struct device *dev);
  174. int (*thaw_noirq)(struct device *dev);
  175. int (*poweroff_noirq)(struct device *dev);
  176. int (*restore_noirq)(struct device *dev);
  177. };
  178. /**
  179. * PM_EVENT_ messages
  180. *
  181. * The following PM_EVENT_ messages are defined for the internal use of the PM
  182. * core, in order to provide a mechanism allowing the high level suspend and
  183. * hibernation code to convey the necessary information to the device PM core
  184. * code:
  185. *
  186. * ON No transition.
  187. *
  188. * FREEZE System is going to hibernate, call ->prepare() and ->freeze()
  189. * for all devices.
  190. *
  191. * SUSPEND System is going to suspend, call ->prepare() and ->suspend()
  192. * for all devices.
  193. *
  194. * HIBERNATE Hibernation image has been saved, call ->prepare() and
  195. * ->poweroff() for all devices.
  196. *
  197. * QUIESCE Contents of main memory are going to be restored from a (loaded)
  198. * hibernation image, call ->prepare() and ->freeze() for all
  199. * devices.
  200. *
  201. * RESUME System is resuming, call ->resume() and ->complete() for all
  202. * devices.
  203. *
  204. * THAW Hibernation image has been created, call ->thaw() and
  205. * ->complete() for all devices.
  206. *
  207. * RESTORE Contents of main memory have been restored from a hibernation
  208. * image, call ->restore() and ->complete() for all devices.
  209. *
  210. * RECOVER Creation of a hibernation image or restoration of the main
  211. * memory contents from a hibernation image has failed, call
  212. * ->thaw() and ->complete() for all devices.
  213. *
  214. * The following PM_EVENT_ messages are defined for internal use by
  215. * kernel subsystems. They are never issued by the PM core.
  216. *
  217. * USER_SUSPEND Manual selective suspend was issued by userspace.
  218. *
  219. * USER_RESUME Manual selective resume was issued by userspace.
  220. *
  221. * REMOTE_WAKEUP Remote-wakeup request was received from the device.
  222. *
  223. * AUTO_SUSPEND Automatic (device idle) runtime suspend was
  224. * initiated by the subsystem.
  225. *
  226. * AUTO_RESUME Automatic (device needed) runtime resume was
  227. * requested by a driver.
  228. */
  229. #define PM_EVENT_ON 0x0000
  230. #define PM_EVENT_FREEZE 0x0001
  231. #define PM_EVENT_SUSPEND 0x0002
  232. #define PM_EVENT_HIBERNATE 0x0004
  233. #define PM_EVENT_QUIESCE 0x0008
  234. #define PM_EVENT_RESUME 0x0010
  235. #define PM_EVENT_THAW 0x0020
  236. #define PM_EVENT_RESTORE 0x0040
  237. #define PM_EVENT_RECOVER 0x0080
  238. #define PM_EVENT_USER 0x0100
  239. #define PM_EVENT_REMOTE 0x0200
  240. #define PM_EVENT_AUTO 0x0400
  241. #define PM_EVENT_SLEEP (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE)
  242. #define PM_EVENT_USER_SUSPEND (PM_EVENT_USER | PM_EVENT_SUSPEND)
  243. #define PM_EVENT_USER_RESUME (PM_EVENT_USER | PM_EVENT_RESUME)
  244. #define PM_EVENT_REMOTE_RESUME (PM_EVENT_REMOTE | PM_EVENT_RESUME)
  245. #define PM_EVENT_AUTO_SUSPEND (PM_EVENT_AUTO | PM_EVENT_SUSPEND)
  246. #define PM_EVENT_AUTO_RESUME (PM_EVENT_AUTO | PM_EVENT_RESUME)
  247. #define PMSG_ON ((struct pm_message){ .event = PM_EVENT_ON, })
  248. #define PMSG_FREEZE ((struct pm_message){ .event = PM_EVENT_FREEZE, })
  249. #define PMSG_QUIESCE ((struct pm_message){ .event = PM_EVENT_QUIESCE, })
  250. #define PMSG_SUSPEND ((struct pm_message){ .event = PM_EVENT_SUSPEND, })
  251. #define PMSG_HIBERNATE ((struct pm_message){ .event = PM_EVENT_HIBERNATE, })
  252. #define PMSG_RESUME ((struct pm_message){ .event = PM_EVENT_RESUME, })
  253. #define PMSG_THAW ((struct pm_message){ .event = PM_EVENT_THAW, })
  254. #define PMSG_RESTORE ((struct pm_message){ .event = PM_EVENT_RESTORE, })
  255. #define PMSG_RECOVER ((struct pm_message){ .event = PM_EVENT_RECOVER, })
  256. #define PMSG_USER_SUSPEND ((struct pm_message) \
  257. { .event = PM_EVENT_USER_SUSPEND, })
  258. #define PMSG_USER_RESUME ((struct pm_message) \
  259. { .event = PM_EVENT_USER_RESUME, })
  260. #define PMSG_REMOTE_RESUME ((struct pm_message) \
  261. { .event = PM_EVENT_REMOTE_RESUME, })
  262. #define PMSG_AUTO_SUSPEND ((struct pm_message) \
  263. { .event = PM_EVENT_AUTO_SUSPEND, })
  264. #define PMSG_AUTO_RESUME ((struct pm_message) \
  265. { .event = PM_EVENT_AUTO_RESUME, })
  266. /**
  267. * Device power management states
  268. *
  269. * These state labels are used internally by the PM core to indicate the current
  270. * status of a device with respect to the PM core operations.
  271. *
  272. * DPM_ON Device is regarded as operational. Set this way
  273. * initially and when ->complete() is about to be called.
  274. * Also set when ->prepare() fails.
  275. *
  276. * DPM_PREPARING Device is going to be prepared for a PM transition. Set
  277. * when ->prepare() is about to be called.
  278. *
  279. * DPM_RESUMING Device is going to be resumed. Set when ->resume(),
  280. * ->thaw(), or ->restore() is about to be called.
  281. *
  282. * DPM_SUSPENDING Device has been prepared for a power transition. Set
  283. * when ->prepare() has just succeeded.
  284. *
  285. * DPM_OFF Device is regarded as inactive. Set immediately after
  286. * ->suspend(), ->freeze(), or ->poweroff() has succeeded.
  287. * Also set when ->resume()_noirq, ->thaw_noirq(), or
  288. * ->restore_noirq() is about to be called.
  289. *
  290. * DPM_OFF_IRQ Device is in a "deep sleep". Set immediately after
  291. * ->suspend_noirq(), ->freeze_noirq(), or
  292. * ->poweroff_noirq() has just succeeded.
  293. */
  294. enum dpm_state {
  295. DPM_INVALID,
  296. DPM_ON,
  297. DPM_PREPARING,
  298. DPM_RESUMING,
  299. DPM_SUSPENDING,
  300. DPM_OFF,
  301. DPM_OFF_IRQ,
  302. };
  303. struct dev_pm_info {
  304. pm_message_t power_state;
  305. unsigned can_wakeup:1;
  306. unsigned should_wakeup:1;
  307. enum dpm_state status; /* Owned by the PM core */
  308. #ifdef CONFIG_PM_SLEEP
  309. struct list_head entry;
  310. #endif
  311. };
  312. /*
  313. * The PM_EVENT_ messages are also used by drivers implementing the legacy
  314. * suspend framework, based on the ->suspend() and ->resume() callbacks common
  315. * for suspend and hibernation transitions, according to the rules below.
  316. */
  317. /* Necessary, because several drivers use PM_EVENT_PRETHAW */
  318. #define PM_EVENT_PRETHAW PM_EVENT_QUIESCE
  319. /*
  320. * One transition is triggered by resume(), after a suspend() call; the
  321. * message is implicit:
  322. *
  323. * ON Driver starts working again, responding to hardware events
  324. * and software requests. The hardware may have gone through
  325. * a power-off reset, or it may have maintained state from the
  326. * previous suspend() which the driver will rely on while
  327. * resuming. On most platforms, there are no restrictions on
  328. * availability of resources like clocks during resume().
  329. *
  330. * Other transitions are triggered by messages sent using suspend(). All
  331. * these transitions quiesce the driver, so that I/O queues are inactive.
  332. * That commonly entails turning off IRQs and DMA; there may be rules
  333. * about how to quiesce that are specific to the bus or the device's type.
  334. * (For example, network drivers mark the link state.) Other details may
  335. * differ according to the message:
  336. *
  337. * SUSPEND Quiesce, enter a low power device state appropriate for
  338. * the upcoming system state (such as PCI_D3hot), and enable
  339. * wakeup events as appropriate.
  340. *
  341. * HIBERNATE Enter a low power device state appropriate for the hibernation
  342. * state (eg. ACPI S4) and enable wakeup events as appropriate.
  343. *
  344. * FREEZE Quiesce operations so that a consistent image can be saved;
  345. * but do NOT otherwise enter a low power device state, and do
  346. * NOT emit system wakeup events.
  347. *
  348. * PRETHAW Quiesce as if for FREEZE; additionally, prepare for restoring
  349. * the system from a snapshot taken after an earlier FREEZE.
  350. * Some drivers will need to reset their hardware state instead
  351. * of preserving it, to ensure that it's never mistaken for the
  352. * state which that earlier snapshot had set up.
  353. *
  354. * A minimally power-aware driver treats all messages as SUSPEND, fully
  355. * reinitializes its device during resume() -- whether or not it was reset
  356. * during the suspend/resume cycle -- and can't issue wakeup events.
  357. *
  358. * More power-aware drivers may also use low power states at runtime as
  359. * well as during system sleep states like PM_SUSPEND_STANDBY. They may
  360. * be able to use wakeup events to exit from runtime low-power states,
  361. * or from system low-power states such as standby or suspend-to-RAM.
  362. */
  363. #ifdef CONFIG_PM_SLEEP
  364. extern void device_pm_lock(void);
  365. extern int sysdev_resume(void);
  366. extern void dpm_resume_noirq(pm_message_t state);
  367. extern void dpm_resume_end(pm_message_t state);
  368. extern void device_pm_unlock(void);
  369. extern int sysdev_suspend(pm_message_t state);
  370. extern int dpm_suspend_noirq(pm_message_t state);
  371. extern int dpm_suspend_start(pm_message_t state);
  372. extern void __suspend_report_result(const char *function, void *fn, int ret);
  373. #define suspend_report_result(fn, ret) \
  374. do { \
  375. __suspend_report_result(__func__, fn, ret); \
  376. } while (0)
  377. #else /* !CONFIG_PM_SLEEP */
  378. #define device_pm_lock() do {} while (0)
  379. #define device_pm_unlock() do {} while (0)
  380. static inline int dpm_suspend_start(pm_message_t state)
  381. {
  382. return 0;
  383. }
  384. #define suspend_report_result(fn, ret) do {} while (0)
  385. #endif /* !CONFIG_PM_SLEEP */
  386. /* How to reorder dpm_list after device_move() */
  387. enum dpm_order {
  388. DPM_ORDER_NONE,
  389. DPM_ORDER_DEV_AFTER_PARENT,
  390. DPM_ORDER_PARENT_BEFORE_DEV,
  391. DPM_ORDER_DEV_LAST,
  392. };
  393. /*
  394. * Global Power Management flags
  395. * Used to keep APM and ACPI from both being active
  396. */
  397. extern unsigned int pm_flags;
  398. #define PM_APM 1
  399. #define PM_ACPI 2
  400. #endif /* _LINUX_PM_H */