pm.h 18 KB

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