/MdeModulePkg/Universal/Network/DpcDxe/Dpc.c

https://bitbucket.org/incubaid/edk2 · C · 347 lines · 113 code · 43 blank · 191 comment · 15 complexity · 445ae69028bc41a58df2d2dc56eadf2c MD5 · raw file

  1. /** @file
  2. Copyright (c) 2007 - 2008, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials
  4. are licensed and made available under the terms and conditions of the BSD License
  5. which accompanies this distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. Dpc.c
  11. Abstract:
  12. **/
  13. #include "Dpc.h"
  14. //
  15. // Handle for the EFI_DPC_PROTOCOL instance
  16. //
  17. EFI_HANDLE mDpcHandle = NULL;
  18. //
  19. // The EFI_DPC_PROTOCOL instances that is installed onto mDpcHandle
  20. //
  21. EFI_DPC_PROTOCOL mDpc = {
  22. DpcQueueDpc,
  23. DpcDispatchDpc
  24. };
  25. //
  26. // Global variables used to meaasure the DPC Queue Depths
  27. //
  28. UINTN mDpcQueueDepth = 0;
  29. UINTN mMaxDpcQueueDepth = 0;
  30. //
  31. // Free list of DPC entries. As DPCs are queued, entries are removed from this
  32. // free list. As DPC entries are dispatched, DPC entries are added to the free list.
  33. // If the free list is empty and a DPC is queued, the free list is grown by allocating
  34. // an additional set of DPC entries.
  35. //
  36. LIST_ENTRY mDpcEntryFreeList = INITIALIZE_LIST_HEAD_VARIABLE(mDpcEntryFreeList);
  37. //
  38. // An array of DPC queues. A DPC queue is allocated for every leval EFI_TPL value.
  39. // As DPCs are queued, they are added to the end of the linked list.
  40. // As DPCs are dispatched, they are removed from the beginning of the linked list.
  41. //
  42. LIST_ENTRY mDpcQueue[TPL_HIGH_LEVEL + 1];
  43. /**
  44. Add a Deferred Procedure Call to the end of the DPC queue.
  45. @param This Protocol instance pointer.
  46. @param DpcTpl The EFI_TPL that the DPC should be invoked.
  47. @param DpcProcedure Pointer to the DPC's function.
  48. @param DpcContext Pointer to the DPC's context. Passed to DpcProcedure
  49. when DpcProcedure is invoked.
  50. @retval EFI_SUCCESS The DPC was queued.
  51. @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL.
  52. @retval EFI_INVALID_PARAMETER DpcProcedure is NULL.
  53. @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
  54. add the DPC to the queue.
  55. **/
  56. EFI_STATUS
  57. EFIAPI
  58. DpcQueueDpc (
  59. IN EFI_DPC_PROTOCOL *This,
  60. IN EFI_TPL DpcTpl,
  61. IN EFI_DPC_PROCEDURE DpcProcedure,
  62. IN VOID *DpcContext OPTIONAL
  63. )
  64. {
  65. EFI_STATUS ReturnStatus;
  66. EFI_TPL OriginalTpl;
  67. DPC_ENTRY *DpcEntry;
  68. UINTN Index;
  69. //
  70. // Make sure DpcTpl is valid
  71. //
  72. if (DpcTpl < TPL_APPLICATION || DpcTpl > TPL_HIGH_LEVEL) {
  73. return EFI_INVALID_PARAMETER;
  74. }
  75. //
  76. // Make sure DpcProcedure is valid
  77. //
  78. if (DpcProcedure == NULL) {
  79. return EFI_INVALID_PARAMETER;
  80. }
  81. //
  82. // Assume this function will succeed
  83. //
  84. ReturnStatus = EFI_SUCCESS;
  85. //
  86. // Raise the TPL level to TPL_HIGH_LEVEL for DPC list operation and save the
  87. // current TPL value so it can be restored when this function returns.
  88. //
  89. OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  90. //
  91. // Check to see if there are any entries in the DPC free list
  92. //
  93. if (IsListEmpty (&mDpcEntryFreeList)) {
  94. //
  95. // If the current TPL is greater than TPL_NOTIFY, then memory allocations
  96. // can not be performed, so the free list can not be expanded. In this case
  97. // return EFI_OUT_OF_RESOURCES.
  98. //
  99. if (OriginalTpl > TPL_NOTIFY) {
  100. ReturnStatus = EFI_OUT_OF_RESOURCES;
  101. goto Done;
  102. }
  103. //
  104. // Add 64 DPC entries to the free list
  105. //
  106. for (Index = 0; Index < 64; Index++) {
  107. //
  108. // Lower the TPL level to perform a memory allocation
  109. //
  110. gBS->RestoreTPL (OriginalTpl);
  111. //
  112. // Allocate a new DPC entry
  113. //
  114. DpcEntry = AllocatePool (sizeof (DPC_ENTRY));
  115. //
  116. // Raise the TPL level back to TPL_HIGH_LEVEL for DPC list operations
  117. //
  118. gBS->RaiseTPL (TPL_HIGH_LEVEL);
  119. //
  120. // If the allocation of a DPC entry fails, and the free list is empty,
  121. // then return EFI_OUT_OF_RESOURCES.
  122. //
  123. if (DpcEntry == NULL) {
  124. if (IsListEmpty (&mDpcEntryFreeList)) {
  125. ReturnStatus = EFI_OUT_OF_RESOURCES;
  126. goto Done;
  127. }
  128. }
  129. //
  130. // Add the newly allocated DPC entry to the DPC free list
  131. //
  132. InsertTailList (&mDpcEntryFreeList, &DpcEntry->ListEntry);
  133. }
  134. }
  135. //
  136. // Retrieve the first node from the free list of DPCs
  137. //
  138. DpcEntry = (DPC_ENTRY *)(GetFirstNode (&mDpcEntryFreeList));
  139. //
  140. // Remove the first node from the free list of DPCs
  141. //
  142. RemoveEntryList (&DpcEntry->ListEntry);
  143. //
  144. // Fill in the DPC entry with the DpcProcedure and DpcContext
  145. //
  146. DpcEntry->DpcProcedure = DpcProcedure;
  147. DpcEntry->DpcContext = DpcContext;
  148. //
  149. // Add the DPC entry to the end of the list for the specified DplTpl.
  150. //
  151. InsertTailList (&mDpcQueue[DpcTpl], &DpcEntry->ListEntry);
  152. //
  153. // Increment the measured DPC queue depth across all TPLs
  154. //
  155. mDpcQueueDepth++;
  156. //
  157. // Measure the maximum DPC queue depth across all TPLs
  158. //
  159. if (mDpcQueueDepth > mMaxDpcQueueDepth) {
  160. mMaxDpcQueueDepth = mDpcQueueDepth;
  161. }
  162. Done:
  163. //
  164. // Restore the original TPL level when this function was called
  165. //
  166. gBS->RestoreTPL (OriginalTpl);
  167. return ReturnStatus;
  168. }
  169. /**
  170. Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl
  171. value greater than or equal to the current TPL are invoked in the order that
  172. they were queued. DPCs with higher DpcTpl values are invoked before DPCs with
  173. lower DpcTpl values.
  174. @param This Protocol instance pointer.
  175. @retval EFI_SUCCESS One or more DPCs were invoked.
  176. @retval EFI_NOT_FOUND No DPCs were invoked.
  177. **/
  178. EFI_STATUS
  179. EFIAPI
  180. DpcDispatchDpc (
  181. IN EFI_DPC_PROTOCOL *This
  182. )
  183. {
  184. EFI_STATUS ReturnStatus;
  185. EFI_TPL OriginalTpl;
  186. EFI_TPL Tpl;
  187. DPC_ENTRY *DpcEntry;
  188. //
  189. // Assume that no DPCs will be invoked
  190. //
  191. ReturnStatus = EFI_NOT_FOUND;
  192. //
  193. // Raise the TPL level to TPL_HIGH_LEVEL for DPC list operation and save the
  194. // current TPL value so it can be restored when this function returns.
  195. //
  196. OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  197. //
  198. // Check to see if there are 1 or more DPCs currently queued
  199. //
  200. if (mDpcQueueDepth > 0) {
  201. //
  202. // Loop from TPL_HIGH_LEVEL down to the current TPL value
  203. //
  204. for (Tpl = TPL_HIGH_LEVEL; Tpl >= OriginalTpl; Tpl--) {
  205. //
  206. // Check to see if the DPC queue is empty
  207. //
  208. while (!IsListEmpty (&mDpcQueue[Tpl])) {
  209. //
  210. // Retrieve the first DPC entry from the DPC queue specified by Tpl
  211. //
  212. DpcEntry = (DPC_ENTRY *)(GetFirstNode (&mDpcQueue[Tpl]));
  213. //
  214. // Remove the first DPC entry from the DPC queue specified by Tpl
  215. //
  216. RemoveEntryList (&DpcEntry->ListEntry);
  217. //
  218. // Decrement the measured DPC Queue Depth across all TPLs
  219. //
  220. mDpcQueueDepth--;
  221. //
  222. // Lower the TPL to TPL value of the current DPC queue
  223. //
  224. gBS->RestoreTPL (Tpl);
  225. //
  226. // Invoke the DPC passing in its context
  227. //
  228. (DpcEntry->DpcProcedure) (DpcEntry->DpcContext);
  229. //
  230. // At least one DPC has been invoked, so set the return status to EFI_SUCCESS
  231. //
  232. ReturnStatus = EFI_SUCCESS;
  233. //
  234. // Raise the TPL level back to TPL_HIGH_LEVEL for DPC list operations
  235. //
  236. gBS->RaiseTPL (TPL_HIGH_LEVEL);
  237. //
  238. // Add the invoked DPC entry to the DPC free list
  239. //
  240. InsertTailList (&mDpcEntryFreeList, &DpcEntry->ListEntry);
  241. }
  242. }
  243. }
  244. //
  245. // Restore the original TPL level when this function was called
  246. //
  247. gBS->RestoreTPL (OriginalTpl);
  248. return ReturnStatus;
  249. }
  250. /**
  251. The entry point for DPC driver which installs the EFI_DPC_PROTOCOL onto a new handle.
  252. @param ImageHandle The image handle of the driver.
  253. @param SystemTable The system table.
  254. @retval EFI_SUCCES The DPC queues were initialized and the EFI_DPC_PROTOCOL was
  255. installed onto a new handle.
  256. @retval Others Failed to install EFI_DPC_PROTOCOL.
  257. **/
  258. EFI_STATUS
  259. EFIAPI
  260. DpcDriverEntryPoint (
  261. IN EFI_HANDLE ImageHandle,
  262. IN EFI_SYSTEM_TABLE *SystemTable
  263. )
  264. {
  265. EFI_STATUS Status;
  266. UINTN Index;
  267. //
  268. // ASSERT() if the EFI_DPC_PROTOCOL is already present in the handle database
  269. //
  270. ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiDpcProtocolGuid);
  271. //
  272. // Initialize the DPC queue for all possible TPL values
  273. //
  274. for (Index = 0; Index <= TPL_HIGH_LEVEL; Index++) {
  275. InitializeListHead (&mDpcQueue[Index]);
  276. }
  277. //
  278. // Install the EFI_DPC_PROTOCOL instance onto a new handle
  279. //
  280. Status = gBS->InstallMultipleProtocolInterfaces (
  281. &mDpcHandle,
  282. &gEfiDpcProtocolGuid,
  283. &mDpc,
  284. NULL
  285. );
  286. ASSERT_EFI_ERROR (Status);
  287. return Status;
  288. }