PrmLoaderDxe.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /** @file
  2. This file contains the implementation for a Platform Runtime Mechanism (PRM)
  3. loader driver.
  4. Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
  5. Copyright (c) Microsoft Corporation
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include "PrmAcpiTable.h"
  9. #include <Guid/ZeroGuid.h>
  10. #include <IndustryStandard/Acpi.h>
  11. #include <Library/BaseLib.h>
  12. #include <Library/BaseMemoryLib.h>
  13. #include <Library/DebugLib.h>
  14. #include <Library/MemoryAllocationLib.h>
  15. #include <Library/PrmContextBufferLib.h>
  16. #include <Library/PrmModuleDiscoveryLib.h>
  17. #include <Library/PrmPeCoffLib.h>
  18. #include <Library/UefiBootServicesTableLib.h>
  19. #include <Library/UefiLib.h>
  20. #include <Protocol/AcpiTable.h>
  21. #include <Protocol/PrmConfig.h>
  22. #include <PrmContextBuffer.h>
  23. #include <PrmMmio.h>
  24. #define _DBGMSGID_ "[PRMLOADER]"
  25. UINTN mPrmHandlerCount;
  26. UINTN mPrmModuleCount;
  27. /**
  28. Processes a list of PRM context entries to build a PRM ACPI table.
  29. The ACPI table buffer is allocated and the table structure is built inside this function.
  30. @param[out] PrmAcpiDescriptionTable A pointer to a pointer to a buffer that is allocated within this function
  31. and will contain the PRM ACPI table. In case of an error in this function,
  32. *PrmAcpiDescriptorTable will be NULL.
  33. @retval EFI_SUCCESS All PRM Modules were processed to construct the PRM ACPI table successfully.
  34. @retval EFI_INVALID_PARAMETER THe parameter PrmAcpiDescriptionTable is NULL.
  35. @retval EFI_OUT_OF_RESOURCES Insufficient memory resources to allocate the PRM ACPI table boot services
  36. memory data buffer.
  37. **/
  38. EFI_STATUS
  39. ProcessPrmModules (
  40. OUT PRM_ACPI_DESCRIPTION_TABLE **PrmAcpiDescriptionTable
  41. )
  42. {
  43. EFI_IMAGE_EXPORT_DIRECTORY *CurrentImageExportDirectory;
  44. PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT *CurrentExportDescriptorStruct;
  45. PRM_ACPI_DESCRIPTION_TABLE *PrmAcpiTable;
  46. PRM_MODULE_IMAGE_CONTEXT *CurrentPrmModuleImageContext;
  47. CONST CHAR8 *CurrentExportDescriptorHandlerName;
  48. ACPI_PARAMETER_BUFFER_DESCRIPTOR *CurrentModuleAcpiParamDescriptors;
  49. PRM_CONTEXT_BUFFER *CurrentContextBuffer;
  50. PRM_MODULE_CONTEXT_BUFFERS *CurrentModuleContextBuffers;
  51. PRM_MODULE_INFORMATION_STRUCT *CurrentModuleInfoStruct;
  52. PRM_HANDLER_INFORMATION_STRUCT *CurrentHandlerInfoStruct;
  53. EFI_STATUS Status;
  54. EFI_PHYSICAL_ADDRESS CurrentImageAddress;
  55. UINTN AcpiParamIndex;
  56. UINTN HandlerIndex;
  57. UINT32 PrmAcpiDescriptionTableBufferSize;
  58. UINT64 HandlerPhysicalAddress;
  59. DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__));
  60. if (PrmAcpiDescriptionTable == NULL) {
  61. return EFI_INVALID_PARAMETER;
  62. }
  63. *PrmAcpiDescriptionTable = NULL;
  64. //
  65. // The platform DSC GUID must be set to a non-zero value
  66. //
  67. if (CompareGuid (&gEdkiiDscPlatformGuid, &gZeroGuid)) {
  68. DEBUG ((
  69. DEBUG_ERROR,
  70. " %a %a: The Platform GUID in the DSC file must be set to a unique non-zero value.\n",
  71. _DBGMSGID_,
  72. __FUNCTION__
  73. ));
  74. ASSERT (!CompareGuid (&gEdkiiDscPlatformGuid, &gZeroGuid));
  75. }
  76. DEBUG ((DEBUG_INFO, " %a %a: %d total PRM modules to process.\n", _DBGMSGID_, __FUNCTION__, mPrmModuleCount));
  77. DEBUG ((DEBUG_INFO, " %a %a: %d total PRM handlers to process.\n", _DBGMSGID_, __FUNCTION__, mPrmHandlerCount));
  78. PrmAcpiDescriptionTableBufferSize = (UINT32)(OFFSET_OF (PRM_ACPI_DESCRIPTION_TABLE, PrmModuleInfoStructure) +
  79. (OFFSET_OF (PRM_MODULE_INFORMATION_STRUCT, HandlerInfoStructure) * mPrmModuleCount) +
  80. (sizeof (PRM_HANDLER_INFORMATION_STRUCT) * mPrmHandlerCount)
  81. );
  82. DEBUG ((DEBUG_INFO, " %a %a: Total PRM ACPI table size: 0x%x.\n", _DBGMSGID_, __FUNCTION__, PrmAcpiDescriptionTableBufferSize));
  83. PrmAcpiTable = AllocateZeroPool ((UINTN)PrmAcpiDescriptionTableBufferSize);
  84. if (PrmAcpiTable == NULL) {
  85. return EFI_OUT_OF_RESOURCES;
  86. }
  87. PrmAcpiTable->Header.Signature = PRM_TABLE_SIGNATURE;
  88. PrmAcpiTable->Header.Length = PrmAcpiDescriptionTableBufferSize;
  89. PrmAcpiTable->Header.Revision = PRM_TABLE_REVISION;
  90. PrmAcpiTable->Header.Checksum = 0x0;
  91. CopyMem (&PrmAcpiTable->Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (PrmAcpiTable->Header.OemId));
  92. PrmAcpiTable->Header.OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
  93. PrmAcpiTable->Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
  94. PrmAcpiTable->Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
  95. PrmAcpiTable->Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
  96. CopyGuid (&PrmAcpiTable->PrmPlatformGuid, &gEdkiiDscPlatformGuid);
  97. PrmAcpiTable->PrmModuleInfoOffset = OFFSET_OF (PRM_ACPI_DESCRIPTION_TABLE, PrmModuleInfoStructure);
  98. PrmAcpiTable->PrmModuleInfoCount = (UINT32)mPrmModuleCount;
  99. //
  100. // Iterate across all PRM Modules on the list
  101. //
  102. CurrentModuleInfoStruct = &PrmAcpiTable->PrmModuleInfoStructure[0];
  103. for (
  104. CurrentPrmModuleImageContext = NULL, Status = GetNextPrmModuleEntry (&CurrentPrmModuleImageContext);
  105. !EFI_ERROR (Status);
  106. Status = GetNextPrmModuleEntry (&CurrentPrmModuleImageContext))
  107. {
  108. CurrentImageAddress = CurrentPrmModuleImageContext->PeCoffImageContext.ImageAddress;
  109. CurrentImageExportDirectory = CurrentPrmModuleImageContext->ExportDirectory;
  110. CurrentExportDescriptorStruct = CurrentPrmModuleImageContext->ExportDescriptor;
  111. CurrentModuleAcpiParamDescriptors = NULL;
  112. DEBUG ((
  113. DEBUG_INFO,
  114. " %a %a: PRM Module - %a with %d handlers.\n",
  115. _DBGMSGID_,
  116. __FUNCTION__,
  117. (CHAR8 *)((UINTN)CurrentImageAddress + CurrentImageExportDirectory->Name),
  118. CurrentExportDescriptorStruct->Header.NumberPrmHandlers
  119. ));
  120. CurrentModuleInfoStruct->StructureRevision = PRM_MODULE_INFORMATION_STRUCT_REVISION;
  121. CurrentModuleInfoStruct->StructureLength = (
  122. OFFSET_OF (PRM_MODULE_INFORMATION_STRUCT, HandlerInfoStructure) +
  123. (CurrentExportDescriptorStruct->Header.NumberPrmHandlers * sizeof (PRM_HANDLER_INFORMATION_STRUCT))
  124. );
  125. CopyGuid (&CurrentModuleInfoStruct->Identifier, &CurrentExportDescriptorStruct->Header.ModuleGuid);
  126. CurrentModuleInfoStruct->HandlerCount = (UINT32)CurrentExportDescriptorStruct->Header.NumberPrmHandlers;
  127. CurrentModuleInfoStruct->HandlerInfoOffset = OFFSET_OF (PRM_MODULE_INFORMATION_STRUCT, HandlerInfoStructure);
  128. CurrentModuleInfoStruct->MajorRevision = 0;
  129. CurrentModuleInfoStruct->MinorRevision = 0;
  130. Status = GetImageVersionInPeCoffImage (
  131. (VOID *)(UINTN)CurrentImageAddress,
  132. &CurrentPrmModuleImageContext->PeCoffImageContext,
  133. &CurrentModuleInfoStruct->MajorRevision,
  134. &CurrentModuleInfoStruct->MinorRevision
  135. );
  136. ASSERT_EFI_ERROR (Status);
  137. // It is currently valid for a PRM module not to use a context buffer
  138. Status = GetModuleContextBuffers (
  139. ByModuleGuid,
  140. &CurrentModuleInfoStruct->Identifier,
  141. (CONST PRM_MODULE_CONTEXT_BUFFERS **)&CurrentModuleContextBuffers
  142. );
  143. ASSERT (!EFI_ERROR (Status) || Status == EFI_NOT_FOUND);
  144. if (!EFI_ERROR (Status) && (CurrentModuleContextBuffers != NULL)) {
  145. CurrentModuleInfoStruct->RuntimeMmioRanges = (UINT64)(UINTN)CurrentModuleContextBuffers->RuntimeMmioRanges;
  146. CurrentModuleAcpiParamDescriptors = CurrentModuleContextBuffers->AcpiParameterBufferDescriptors;
  147. }
  148. //
  149. // Iterate across all PRM handlers in the PRM Module
  150. //
  151. for (HandlerIndex = 0; HandlerIndex < CurrentExportDescriptorStruct->Header.NumberPrmHandlers; HandlerIndex++) {
  152. CurrentHandlerInfoStruct = &(CurrentModuleInfoStruct->HandlerInfoStructure[HandlerIndex]);
  153. CurrentHandlerInfoStruct->StructureRevision = PRM_HANDLER_INFORMATION_STRUCT_REVISION;
  154. CurrentHandlerInfoStruct->StructureLength = sizeof (PRM_HANDLER_INFORMATION_STRUCT);
  155. CopyGuid (
  156. &CurrentHandlerInfoStruct->Identifier,
  157. &CurrentExportDescriptorStruct->PrmHandlerExportDescriptors[HandlerIndex].PrmHandlerGuid
  158. );
  159. CurrentExportDescriptorHandlerName = (CONST CHAR8 *)CurrentExportDescriptorStruct->PrmHandlerExportDescriptors[HandlerIndex].PrmHandlerName;
  160. Status = GetContextBuffer (
  161. &CurrentHandlerInfoStruct->Identifier,
  162. CurrentModuleContextBuffers,
  163. (CONST PRM_CONTEXT_BUFFER **)&CurrentContextBuffer
  164. );
  165. if (!EFI_ERROR (Status)) {
  166. CurrentHandlerInfoStruct->StaticDataBuffer = (UINT64)(UINTN)CurrentContextBuffer->StaticDataBuffer;
  167. }
  168. Status = GetExportEntryAddress (
  169. CurrentExportDescriptorHandlerName,
  170. CurrentImageAddress,
  171. CurrentImageExportDirectory,
  172. &HandlerPhysicalAddress
  173. );
  174. ASSERT_EFI_ERROR (Status);
  175. if (!EFI_ERROR (Status)) {
  176. CurrentHandlerInfoStruct->PhysicalAddress = HandlerPhysicalAddress;
  177. DEBUG ((
  178. DEBUG_INFO,
  179. " %a %a: Found %a handler physical address at 0x%016x.\n",
  180. _DBGMSGID_,
  181. __FUNCTION__,
  182. CurrentExportDescriptorHandlerName,
  183. CurrentHandlerInfoStruct->PhysicalAddress
  184. ));
  185. }
  186. //
  187. // Update the handler ACPI parameter buffer address if applicable
  188. //
  189. if (CurrentModuleAcpiParamDescriptors != NULL) {
  190. for (AcpiParamIndex = 0; AcpiParamIndex < CurrentModuleContextBuffers->AcpiParameterBufferDescriptorCount; AcpiParamIndex++) {
  191. if (CompareGuid (&CurrentModuleAcpiParamDescriptors[AcpiParamIndex].HandlerGuid, &CurrentHandlerInfoStruct->Identifier)) {
  192. CurrentHandlerInfoStruct->AcpiParameterBuffer = (UINT64)(UINTN)(
  193. CurrentModuleAcpiParamDescriptors[AcpiParamIndex].AcpiParameterBufferAddress
  194. );
  195. }
  196. }
  197. }
  198. }
  199. CurrentModuleInfoStruct = (PRM_MODULE_INFORMATION_STRUCT *)((UINTN)CurrentModuleInfoStruct + CurrentModuleInfoStruct->StructureLength);
  200. }
  201. *PrmAcpiDescriptionTable = PrmAcpiTable;
  202. return EFI_SUCCESS;
  203. }
  204. /**
  205. Publishes the PRM ACPI table (PRMT).
  206. @param[in] PrmAcpiDescriptionTable A pointer to a buffer with a completely populated and valid PRM
  207. ACPI description table.
  208. @retval EFI_SUCCESS The PRM ACPI was installed successfully.
  209. @retval EFI_INVALID_PARAMETER THe parameter PrmAcpiDescriptionTable is NULL or the table signature
  210. in the table provided is invalid.
  211. @retval EFI_NOT_FOUND The protocol gEfiAcpiTableProtocolGuid could not be found.
  212. @retval EFI_OUT_OF_RESOURCES Insufficient memory resources to allocate the PRM ACPI table buffer.
  213. **/
  214. EFI_STATUS
  215. PublishPrmAcpiTable (
  216. IN PRM_ACPI_DESCRIPTION_TABLE *PrmAcpiDescriptionTable
  217. )
  218. {
  219. EFI_STATUS Status;
  220. EFI_ACPI_TABLE_PROTOCOL *AcpiTableProtocol;
  221. UINTN TableKey;
  222. if ((PrmAcpiDescriptionTable == NULL) || (PrmAcpiDescriptionTable->Header.Signature != PRM_TABLE_SIGNATURE)) {
  223. return EFI_INVALID_PARAMETER;
  224. }
  225. Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTableProtocol);
  226. if (!EFI_ERROR (Status)) {
  227. TableKey = 0;
  228. //
  229. // Publish the PRM ACPI table. The table checksum will be computed during installation.
  230. //
  231. Status = AcpiTableProtocol->InstallAcpiTable (
  232. AcpiTableProtocol,
  233. PrmAcpiDescriptionTable,
  234. PrmAcpiDescriptionTable->Header.Length,
  235. &TableKey
  236. );
  237. if (!EFI_ERROR (Status)) {
  238. DEBUG ((DEBUG_INFO, "%a %a: The PRMT ACPI table was installed successfully.\n", _DBGMSGID_, __FUNCTION__));
  239. }
  240. }
  241. ASSERT_EFI_ERROR (Status);
  242. return Status;
  243. }
  244. /**
  245. The PRM Loader END_OF_DXE protocol notification event handler.
  246. All PRM Modules that are eligible for dispatch should have been loaded the DXE Dispatcher at the
  247. time of this function invocation.
  248. The main responsibilities of the PRM Loader are executed from this function which include 3 phases:
  249. 1.) Disover PRM Modules - Find all PRM modules loaded during DXE dispatch and insert a PRM Module
  250. Context entry into a linked list to be handed off to phase 2.
  251. 2.) Process PRM Modules - Build a GUID to PRM handler mapping for each module that is described in the
  252. PRM ACPI table so the OS can resolve a PRM Handler GUID to the corresponding PRM Handler physical address.
  253. 3.) Publish PRM ACPI Table - Publish the PRM ACPI table with the information gathered in the phase 2.
  254. @param[in] Event Event whose notification function is being invoked.
  255. @param[in] Context The pointer to the notification function's context,
  256. which is implementation-dependent.
  257. **/
  258. VOID
  259. EFIAPI
  260. PrmLoaderEndOfDxeNotification (
  261. IN EFI_EVENT Event,
  262. IN VOID *Context
  263. )
  264. {
  265. EFI_STATUS Status;
  266. PRM_ACPI_DESCRIPTION_TABLE *PrmAcpiDescriptionTable;
  267. DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__));
  268. Status = DiscoverPrmModules (&mPrmModuleCount, &mPrmHandlerCount);
  269. ASSERT_EFI_ERROR (Status);
  270. Status = ProcessPrmModules (&PrmAcpiDescriptionTable);
  271. ASSERT_EFI_ERROR (Status);
  272. Status = PublishPrmAcpiTable (PrmAcpiDescriptionTable);
  273. ASSERT_EFI_ERROR (Status);
  274. if (PrmAcpiDescriptionTable != NULL) {
  275. FreePool (PrmAcpiDescriptionTable);
  276. }
  277. gBS->CloseEvent (Event);
  278. }
  279. /**
  280. The entry point for this module.
  281. @param ImageHandle The firmware allocated handle for the EFI image.
  282. @param SystemTable A pointer to the EFI System Table.
  283. @retval EFI_SUCCESS The entry point is executed successfully.
  284. @retval Others An error occurred when executing this entry point.
  285. **/
  286. EFI_STATUS
  287. EFIAPI
  288. PrmLoaderEntryPoint (
  289. IN EFI_HANDLE ImageHandle,
  290. IN EFI_SYSTEM_TABLE *SystemTable
  291. )
  292. {
  293. EFI_STATUS Status;
  294. EFI_EVENT EndOfDxeEvent;
  295. DEBUG ((DEBUG_INFO, "%a %a - Entry.\n", _DBGMSGID_, __FUNCTION__));
  296. //
  297. // Discover and process installed PRM modules at the End of DXE
  298. // The PRM ACPI table is published if one or PRM modules are discovered
  299. //
  300. Status = gBS->CreateEventEx (
  301. EVT_NOTIFY_SIGNAL,
  302. TPL_CALLBACK,
  303. PrmLoaderEndOfDxeNotification,
  304. NULL,
  305. &gEfiEndOfDxeEventGroupGuid,
  306. &EndOfDxeEvent
  307. );
  308. if (EFI_ERROR (Status)) {
  309. DEBUG ((DEBUG_ERROR, "%a %a: EndOfDxe callback registration failed! %r.\n", _DBGMSGID_, __FUNCTION__, Status));
  310. ASSERT_EFI_ERROR (Status);
  311. }
  312. return EFI_SUCCESS;
  313. }