pm.h 18 KB

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