pm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. pm.h
  9. Abstract:
  10. This header contains definitions for the power management subsystem.
  11. Author:
  12. Evan Green 3-Sep-2015
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. //
  18. // ---------------------------------------------------------------- Definitions
  19. //
  20. //
  21. // Define performance state interface flags.
  22. //
  23. //
  24. // Set this flag if the interface is per-processor, in which case target
  25. // changes will be called at dispatch level. If this flag is not set, then
  26. // P-state management is assumed to be global and will get called at low-level
  27. // to affect changes.
  28. //
  29. #define PM_PERFORMANCE_STATE_PER_PROCESSOR 0x00000001
  30. //
  31. // Define the total weight of all the states.
  32. //
  33. #define PM_PERFORMANCE_STATE_WEIGHT_TOTAL 1024
  34. #define PM_PERFORMANCE_STATE_WEIGHT_SHIFT 10
  35. //
  36. // Define the maximum length of an idle state name.
  37. //
  38. #define PM_IDLE_STATE_NAME_LENGTH 8
  39. //
  40. // Define the invalid state used to indicate the CPU is active.
  41. //
  42. #define PM_IDLE_STATE_NONE (-1)
  43. #define PM_IDLE_STATE_HALT (-2)
  44. //
  45. // ------------------------------------------------------ Data Type Definitions
  46. //
  47. typedef enum _DEVICE_POWER_STATE {
  48. DevicePowerStateInvalid,
  49. DevicePowerStateActive,
  50. DevicePowerStateTransitioning,
  51. DevicePowerStateIdle,
  52. DevicePowerStateSuspended,
  53. DevicePowerStateRemoved
  54. } DEVICE_POWER_STATE, *PDEVICE_POWER_STATE;
  55. typedef enum _PM_INFORMATION_TYPE {
  56. PmInformationInvalid,
  57. PmInformationPerformanceStateHandlers,
  58. PmInformationIdleStateHandlers,
  59. } PM_INFORMATION_TYPE, *PPM_INFORMATION_TYPE;
  60. typedef struct _PM_PERFORMANCE_STATE_INTERFACE
  61. PM_PERFORMANCE_STATE_INTERFACE, *PPM_PERFORMANCE_STATE_INTERFACE;
  62. /*++
  63. Structure Description:
  64. This structure defines a particular processor performance state. It is
  65. assumed that all CPUs in the system can switch to this state if performance
  66. state is per-CPU.
  67. Members:
  68. Frequency - Stores the CPU frequency of this state in kilo Hertz.
  69. Weight - Stores the weight to associate with this state. That is, how
  70. much of the range of possible loads fall into this state. The total of
  71. all possible weights should equal 1024 (or close to it). For example,
  72. if there are 4 possible performance states, and they all have equal
  73. weights, then performance state 1 will be used at a load of < 25%,
  74. 2 at < 50%, 3 at < 75%, and 4 otherwise. If they have weights of 170,
  75. 170, 172, and 512, then state 4 will be used for any load above 50%.
  76. --*/
  77. typedef struct _PM_PERFORMANCE_STATE {
  78. ULONG Frequency;
  79. ULONG Weight;
  80. } PM_PERFORMANCE_STATE, *PPM_PERFORMANCE_STATE;
  81. typedef
  82. KSTATUS
  83. (*PPM_SET_PERFORMANCE_STATE) (
  84. PPM_PERFORMANCE_STATE_INTERFACE Interface,
  85. ULONG State
  86. );
  87. /*++
  88. Routine Description:
  89. This routine prototype represents a function that is called to change the
  90. current performance state. If the performance state interface is
  91. per-processor, then this routine is called at dispatch level on the
  92. processor to change. If performance state changes are global, then this
  93. routine is called at low level (and therefore on any processor).
  94. Arguments:
  95. Interface - Supplies a pointer to the interface.
  96. State - Supplies the new state index to change to.
  97. Return Value:
  98. Status code.
  99. --*/
  100. /*++
  101. Structure Description:
  102. This structure defines the kernel performance state interface.
  103. Members:
  104. Flags - Stores a bitfield of flags about the performance state interface.
  105. See PM_PERFORMANCE_STATE_* definitions.
  106. MinimumPeriod - Stores the minimum period in time counter ticks to
  107. re-evaluate performance state data. This should be set to about the
  108. amount of time it takes to affect a performance state change.
  109. States - Stores a pointer to an array of possible performance states. The
  110. interface must not modify this pointer or array without synchronizing
  111. with the kernel.
  112. StateCount - Stores the number of states in the array.
  113. SetPerformanceState - Stores a pointer to a function used to change to a
  114. new performance state.
  115. Context - Stores a pointer's worth of context that the interface provider
  116. can use to get back to its data structures.
  117. --*/
  118. struct _PM_PERFORMANCE_STATE_INTERFACE {
  119. ULONG Flags;
  120. ULONGLONG MinimumPeriod;
  121. PPM_PERFORMANCE_STATE States;
  122. ULONG StateCount;
  123. PPM_SET_PERFORMANCE_STATE SetPerformanceState;
  124. PVOID Context;
  125. };
  126. //
  127. // CPU idle state data types
  128. //
  129. typedef struct _PM_IDLE_STATE_INTERFACE
  130. PM_IDLE_STATE_INTERFACE, *PPM_IDLE_STATE_INTERFACE;
  131. /*++
  132. Structure Description:
  133. This structure defines a single CPU idle state that a processor can enter.
  134. Members:
  135. Name - Stores the name of this idle state, for display purposes.
  136. Flags - Stores a bitfield of flags describing this state. See
  137. PM_IDLE_STATE_* definitions.
  138. Context - Stores a pointer's worth of context that the driver can use to
  139. store additional data about this state.
  140. ExitLatency - Stores the amount of time needed to exit this idle state once
  141. entered, in time counter ticks.
  142. TargetResidency - Stores the minimum duration to be in this idle state to
  143. make it worth it to enter, in time counter ticks.
  144. --*/
  145. typedef struct _PM_IDLE_STATE {
  146. CHAR Name[PM_IDLE_STATE_NAME_LENGTH];
  147. ULONG Flags;
  148. PVOID Context;
  149. ULONGLONG ExitLatency;
  150. ULONGLONG TargetResidency;
  151. } PM_IDLE_STATE, *PPM_IDLE_STATE;
  152. /*++
  153. Structure Description:
  154. This structure defines the per processor CPU idle information.
  155. Members:
  156. States - Stores a pointer to an array of idle states. The CPU idle driver
  157. fills this in upon initialization.
  158. StateCount - Stores the number of states in the array. The CPU idle driver
  159. fills this in upon initialization.
  160. ProcessorNumber - Stores the software index of this processor. The boot
  161. processor will always be zero.
  162. Context - Stores a per processor context pointer the CPU idle driver can
  163. use to store additional state.
  164. CurrentState - Stores the current state of the processor. This will be
  165. initialized to the desired state upon calling enter, and will be
  166. cleared to PM_IDLE_STATE_NONE when the CPU is active.
  167. --*/
  168. typedef struct _PM_IDLE_PROCESSOR_STATE {
  169. PPM_IDLE_STATE States;
  170. ULONG StateCount;
  171. ULONG ProcessorNumber;
  172. PVOID Context;
  173. ULONG CurrentState;
  174. } PM_IDLE_PROCESSOR_STATE, *PPM_IDLE_PROCESSOR_STATE;
  175. typedef
  176. KSTATUS
  177. (*PPM_INITIALIZE_IDLE_STATES) (
  178. PPM_IDLE_STATE_INTERFACE Interface,
  179. PPM_IDLE_PROCESSOR_STATE Processor
  180. );
  181. /*++
  182. Routine Description:
  183. This routine prototype represents a function that is called to go set up
  184. idle state information on the current processor. It should set the states
  185. and state count in the given processor idle information structure. This
  186. routine is called once on every processor. It runs at dispatch level.
  187. Arguments:
  188. Interface - Supplies a pointer to the interface.
  189. Processor - Supplies a pointer to the context for this processor.
  190. Return Value:
  191. Status code.
  192. --*/
  193. typedef
  194. VOID
  195. (*PPM_ENTER_IDLE_STATE) (
  196. PPM_IDLE_PROCESSOR_STATE Processor,
  197. ULONG State
  198. );
  199. /*++
  200. Routine Description:
  201. This routine prototype represents a function that is called to go into a
  202. given idle state on the current processor. This routine is called with
  203. interrupts disabled, and should return with interrupts disabled.
  204. Arguments:
  205. Processor - Supplies a pointer to the information for the current processor.
  206. State - Supplies the new state index to change to.
  207. Return Value:
  208. None. It is assumed when this function returns that the idle state was
  209. entered and then exited.
  210. --*/
  211. /*++
  212. Structure Description:
  213. This structure defines the kernel CPU idle state interface.
  214. Members:
  215. Flags - Stores a bitfield of flags about the performance state interface.
  216. See PM_IDLE_STATE_INTERFACE_* definitions.
  217. InitializeIdleStates - Stores a pointer to a function called on each
  218. active processor that initializes processor idle state support.
  219. EnterIdleState - Stores a pointer to a function used to enter an idle state.
  220. Context - Stores a pointer's worth of context that the interface provider
  221. can use to get back to its data structures.
  222. --*/
  223. struct _PM_IDLE_STATE_INTERFACE {
  224. ULONG Flags;
  225. PPM_INITIALIZE_IDLE_STATES InitializeIdleStates;
  226. PPM_ENTER_IDLE_STATE EnterIdleState;
  227. PVOID Context;
  228. };
  229. //
  230. // -------------------------------------------------------------------- Globals
  231. //
  232. //
  233. // -------------------------------------------------------- Function Prototypes
  234. //
  235. KERNEL_API
  236. KSTATUS
  237. PmInitialize (
  238. PDEVICE Device
  239. );
  240. /*++
  241. Routine Description:
  242. This routine initializes power management infrastructure for a given
  243. device.
  244. Arguments:
  245. Device - Supplies a pointer to the device to prepare to do power management
  246. calls on.
  247. Return Value:
  248. Status code.
  249. --*/
  250. KERNEL_API
  251. KSTATUS
  252. PmDeviceAddReference (
  253. PDEVICE Device
  254. );
  255. /*++
  256. Routine Description:
  257. This routine adds a power management reference on the given device,
  258. and waits for the device to transition to the active state.
  259. Arguments:
  260. Device - Supplies a pointer to the device to add a power reference to.
  261. Return Value:
  262. Status code. On failure, the caller will not have a reference on the
  263. device, and should not assume that the device or its parent lineage is
  264. active.
  265. --*/
  266. KERNEL_API
  267. KSTATUS
  268. PmDeviceAddReferenceAsynchronous (
  269. PDEVICE Device
  270. );
  271. /*++
  272. Routine Description:
  273. This routine adds a power management reference on the given device,
  274. preventing the device from idling until the reference is released.
  275. Arguments:
  276. Device - Supplies a pointer to the device to add a power reference to.
  277. Return Value:
  278. Status code indicating if the request was successfully queued. On failure,
  279. the caller will not have the reference on the device.
  280. --*/
  281. KERNEL_API
  282. VOID
  283. PmDeviceReleaseReference (
  284. PDEVICE Device
  285. );
  286. /*++
  287. Routine Description:
  288. This routine releases a power management reference on a device.
  289. Arguments:
  290. Device - Supplies a pointer to the device to subtract a power reference
  291. from.
  292. Return Value:
  293. None.
  294. --*/
  295. KERNEL_API
  296. KSTATUS
  297. PmDeviceSetState (
  298. PDEVICE Device,
  299. DEVICE_POWER_STATE PowerState
  300. );
  301. /*++
  302. Routine Description:
  303. This routine sets a new power state for the device. This can be used to
  304. clear an error. It should not be called from a power IRP.
  305. Arguments:
  306. Device - Supplies a pointer to the device to set the power state for.
  307. PowerState - Supplies the new power management state to set.
  308. Return Value:
  309. Status code.
  310. --*/
  311. KSTATUS
  312. PmInitializeLibrary (
  313. VOID
  314. );
  315. /*++
  316. Routine Description:
  317. This routine performs global initialization for the power management
  318. library. It is called towards the end of I/O initialization.
  319. Arguments:
  320. None.
  321. Return Value:
  322. None.
  323. --*/
  324. KSTATUS
  325. PmGetSetSystemInformation (
  326. BOOL FromKernelMode,
  327. PM_INFORMATION_TYPE InformationType,
  328. PVOID Data,
  329. PUINTN DataSize,
  330. BOOL Set
  331. );
  332. /*++
  333. Routine Description:
  334. This routine gets or sets system information.
  335. Arguments:
  336. FromKernelMode - Supplies a boolean indicating whether or not this request
  337. (and the buffer associated with it) originates from user mode (FALSE)
  338. or kernel mode (TRUE).
  339. InformationType - Supplies the information type.
  340. Data - Supplies a pointer to the data buffer where the data is either
  341. returned for a get operation or given for a set operation.
  342. DataSize - Supplies a pointer that on input contains the size of the
  343. data buffer. On output, contains the required size of the data buffer.
  344. Set - Supplies a boolean indicating if this is a get operation (FALSE) or
  345. a set operation (TRUE).
  346. Return Value:
  347. Status code.
  348. --*/
  349. VOID
  350. PmIdle (
  351. PPROCESSOR_BLOCK Processor
  352. );
  353. /*++
  354. Routine Description:
  355. This routine is called on a processor to go into a low power idle state. If
  356. no processor idle driver has been registered or processor idle states have
  357. been disabled, then the processor simply halts waiting for an interrupt.
  358. This routine is called with interrupts disabled and returns with interrupts
  359. enabled. This routine should only be called internally by the idle thread.
  360. Arguments:
  361. Processor - Supplies a pointer to the processor going idle.
  362. Return Value:
  363. None. This routine returns from the idle state with interrupts enabled.
  364. --*/