pmp.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. pmp.h
  5. Abstract:
  6. This header contains internal definitions for the power management
  7. subsystem.
  8. Author:
  9. Evan Green 3-Sep-2015
  10. --*/
  11. //
  12. // ------------------------------------------------------------------- Includes
  13. //
  14. //
  15. // ---------------------------------------------------------------- Definitions
  16. //
  17. #define PM_ALLOCATION_TAG 0x21727750
  18. #define PM_DEVICE_ALLOCATION_TAG 0x44727750
  19. #define PM_PSTATE_ALLOCATION_TAG 0x50727750
  20. //
  21. // Define idle history flags.
  22. //
  23. //
  24. // Set this flag if the idle history will be accessed at or above dispatch
  25. // level or with interrupts disabled.
  26. //
  27. #define IDLE_HISTORY_NON_PAGED 0x00000001
  28. //
  29. // ------------------------------------------------------ Data Type Definitions
  30. //
  31. typedef enum _DEVICE_POWER_REQUEST {
  32. DevicePowerRequestInvalid,
  33. DevicePowerRequestNone,
  34. DevicePowerRequestIdle,
  35. DevicePowerRequestSuspend,
  36. DevicePowerRequestResume,
  37. DevicePowerRequestMarkActive,
  38. } DEVICE_POWER_REQUEST, *PDEVICE_POWER_REQUEST;
  39. /*++
  40. Structure Description:
  41. This structure contains the idle history of a device or processor.
  42. Members:
  43. Shift - Stores the bit shift of the number of buckets. That is, 1 << Shift
  44. equals the number of buckets in the history.
  45. NextIndex - Stores the next index to replace.
  46. Flags - Stores a bitfield of flags about the history. See IDLE_HISTORY_*
  47. definitions.
  48. Total - Stores the running total of the data.
  49. Data - Stores the array of data elements.
  50. --*/
  51. typedef struct _IDLE_HISTORY {
  52. ULONG Shift;
  53. ULONG NextIndex;
  54. ULONG Flags;
  55. ULONGLONG Total;
  56. PULONGLONG Data;
  57. } IDLE_HISTORY, *PIDLE_HISTORY;
  58. /*++
  59. Structure Description:
  60. This structure defines the power management state of a device.
  61. Members:
  62. State - Stores the current device's power state. This is protected by the
  63. power state's queued lock.
  64. PreviousState - Stores the previous device power state. This is protected
  65. by the power state's queued lock.
  66. Request - Stores the current device power request, if the device's power
  67. state is transitioning. This is protected by the power state's queued
  68. lock.
  69. ReferenceCount - Stores the number of power references on this device.
  70. ActiveChildren - Stores the number of active children relying on this
  71. device. This value has an extra 1 representing the current device's
  72. reference count.
  73. TimerQueued - Stores an atomic boolean indicating whether or not the timer
  74. is currently queued.
  75. Lock - Stores a pointer to a queued lock that protects the state
  76. transitions and the idle history.
  77. ActiveEvent - Stores a pointer to an event that can be waited on for a
  78. device to become active.
  79. IdleTimer - Stores a pointer to a timer used to delay idle transitions.
  80. IdleDelay - Stores the delay between the last power reference being dropped
  81. and the idle request being sent.
  82. IdleTimeout - Stores the absolute timeout, in time counter ticks, when the
  83. idle timer should expire. This value may occasionally tear, but that's
  84. an acceptable tradeoff.
  85. IdleTimerDpc - Stores a pointer to the DPC that's queued when the idle
  86. timer expires.
  87. IdleTimerWorkItem - Stores a pointer to the work item that's queued when
  88. the idle timer DPC runs.
  89. Irp - Stores a pointer to the IRP used for power requests.
  90. History - Stores a pointer to the idle history for the device.
  91. TransitionTime - Stores the time counter when the transition to the current
  92. state was made.
  93. --*/
  94. struct _DEVICE_POWER {
  95. DEVICE_POWER_STATE State;
  96. DEVICE_POWER_STATE PreviousState;
  97. DEVICE_POWER_REQUEST Request;
  98. volatile UINTN ReferenceCount;
  99. volatile UINTN ActiveChildren;
  100. volatile ULONG TimerQueued;
  101. PQUEUED_LOCK Lock;
  102. PKEVENT ActiveEvent;
  103. PKTIMER IdleTimer;
  104. ULONGLONG IdleDelay;
  105. ULONGLONG IdleTimeout;
  106. PDPC IdleTimerDpc;
  107. PWORK_ITEM IdleTimerWorkItem;
  108. PIRP Irp;
  109. PIDLE_HISTORY History;
  110. ULONGLONG TransitionTime;
  111. };
  112. //
  113. // -------------------------------------------------------------------- Globals
  114. //
  115. //
  116. // -------------------------------------------------------- Function Prototypes
  117. //
  118. KSTATUS
  119. PmpArchInitialize (
  120. VOID
  121. );
  122. /*++
  123. Routine Description:
  124. This routine performs architecture-specific initialization for the power
  125. management library.
  126. Arguments:
  127. None.
  128. Return Value:
  129. Status code.
  130. --*/
  131. VOID
  132. PmpRemoveDevice (
  133. PDEVICE Device
  134. );
  135. /*++
  136. Routine Description:
  137. This routine is called when a device is removed from the system. It cleans
  138. up the power management state. It is assumed that the device lock is
  139. already held exclusive.
  140. Arguments:
  141. Device - Supplies a pointer to the device to remove.
  142. Return Value:
  143. None.
  144. --*/
  145. VOID
  146. PmpDestroyDevice (
  147. PDEVICE Device
  148. );
  149. /*++
  150. Routine Description:
  151. This routine tears down the power management structures associated with a
  152. device.
  153. Arguments:
  154. Device - Supplies a pointer to the device to tear down.
  155. Return Value:
  156. None.
  157. --*/
  158. VOID
  159. PmpDevicePowerTransition (
  160. PDEVICE Device
  161. );
  162. /*++
  163. Routine Description:
  164. This routine is called by the worker thread to perform a device power
  165. transition.
  166. Arguments:
  167. Device - Supplies a pointer to the device to work on.
  168. Return Value:
  169. None.
  170. --*/
  171. //
  172. // Power optimization functions
  173. //
  174. PIDLE_HISTORY
  175. PmpCreateIdleHistory (
  176. ULONG Flags,
  177. ULONG Shift
  178. );
  179. /*++
  180. Routine Description:
  181. This routine creates an idle history structure, which tracks the idle
  182. history of a device or processor.
  183. Arguments:
  184. Flags - Supplies a bitfield of flags governing the creation and behavior of
  185. the idle history. See IDLE_HISTORY_* definitions.
  186. Shift - Supplies the logarithm of the number of history elements to store.
  187. That is, 1 << Shift will equal the number of history elements stored.
  188. Return Value:
  189. Returns a pointer to the new history on success.
  190. NULL on allocation failure.
  191. --*/
  192. VOID
  193. PmpDestroyIdleHistory (
  194. PIDLE_HISTORY History
  195. );
  196. /*++
  197. Routine Description:
  198. This routine destroys an idle history structure.
  199. Arguments:
  200. History - Supplies a pointer to the idle history to destroy.
  201. Return Value:
  202. None.
  203. --*/
  204. VOID
  205. PmpIdleHistoryAddDataPoint (
  206. PIDLE_HISTORY History,
  207. ULONGLONG Value
  208. );
  209. /*++
  210. Routine Description:
  211. This routine adds a datapoint to the running idle history. This routine
  212. is not synchronized.
  213. Arguments:
  214. History - Supplies a pointer to the idle history.
  215. Value - Supplies the new data value to add.
  216. Return Value:
  217. None.
  218. --*/
  219. ULONGLONG
  220. PmpIdleHistoryGetAverage (
  221. PIDLE_HISTORY History
  222. );
  223. /*++
  224. Routine Description:
  225. This routine returns the running average of the idle history.
  226. Arguments:
  227. History - Supplies a pointer to the idle history.
  228. Return Value:
  229. Returns the average idle duration.
  230. --*/
  231. KSTATUS
  232. PmpGetSetPerformanceStateHandlers (
  233. BOOL FromKernelMode,
  234. PVOID Data,
  235. PUINTN DataSize,
  236. BOOL Set
  237. );
  238. /*++
  239. Routine Description:
  240. This routine gets or sets the performance state handlers. In this case
  241. the data pointer is used directly (so the interface structure must not
  242. disappear after the call). This can only be set, can only be set once, and
  243. can only be set from kernel mode for obvious reasons.
  244. Arguments:
  245. FromKernelMode - Supplies a boolean indicating whether or not this request
  246. (and the buffer associated with it) originates from user mode (FALSE)
  247. or kernel mode (TRUE).
  248. Data - Supplies a pointer to the data buffer where the data is either
  249. returned for a get operation or given for a set operation.
  250. DataSize - Supplies a pointer that on input contains the size of the
  251. data buffer. On output, contains the required size of the data buffer.
  252. Set - Supplies a boolean indicating if this is a get operation (FALSE) or
  253. a set operation (TRUE).
  254. Return Value:
  255. STATUS_SUCCESS if the performance state information was initialized.
  256. STATUS_NOT_SUPPORTED for a get operation.
  257. STATUS_PERMISSION_DENIED if this is a user mode request.
  258. STATUS_DATA_LENGTH_MISMATCH if the data size is not the size of the
  259. PM_PERFORMANCE_STATE_INTERFACE structure.
  260. STATUS_TOO_LATE if performance state handlers have already been registered.
  261. Other errors if the performance state runtime could not be initialized.
  262. --*/
  263. KSTATUS
  264. PmpGetSetIdleStateHandlers (
  265. BOOL FromKernelMode,
  266. PVOID Data,
  267. PUINTN DataSize,
  268. BOOL Set
  269. );
  270. /*++
  271. Routine Description:
  272. This routine gets or sets the idle state handlers. In this case the data
  273. pointer is used directly (so the interface structure must not disappear
  274. after the call). This can only be set, can only be set once, and can only
  275. be set from kernel mode for obvious reasons.
  276. Arguments:
  277. FromKernelMode - Supplies a boolean indicating whether or not this request
  278. (and the buffer associated with it) originates from user mode (FALSE)
  279. or kernel mode (TRUE).
  280. Data - Supplies a pointer to the data buffer where the data is either
  281. returned for a get operation or given for a set operation.
  282. DataSize - Supplies a pointer that on input contains the size of the
  283. data buffer. On output, contains the required size of the data buffer.
  284. Set - Supplies a boolean indicating if this is a get operation (FALSE) or
  285. a set operation (TRUE).
  286. Return Value:
  287. STATUS_SUCCESS if the performance state information was initialized.
  288. STATUS_NOT_SUPPORTED for a get operation.
  289. STATUS_PERMISSION_DENIED if this is a user mode request.
  290. STATUS_DATA_LENGTH_MISMATCH if the data size is not the size of the
  291. PM_IDLE_STATE_INTERFACE structure.
  292. STATUS_TOO_LATE if performance state handlers have already been registered.
  293. Other errors if the performance state runtime could not be initialized.
  294. --*/
  295. VOID
  296. PmpIntelCstateDriverEntry (
  297. VOID
  298. );
  299. /*++
  300. Routine Description:
  301. This routine initializes support for Intel C-states. It will register
  302. itself as a processor idle state manager if it supports this processor.
  303. Arguments:
  304. None.
  305. Return Value:
  306. None.
  307. --*/