PeiSmmAccessLib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /** @file
  2. This is to publish the SMM Access Ppi instance.
  3. Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/BaseMemoryLib.h>
  7. #include <Library/DebugLib.h>
  8. #include <Library/MemoryAllocationLib.h>
  9. #include <Library/PciSegmentLib.h>
  10. #include <Library/PeiServicesLib.h>
  11. #include <Library/HobLib.h>
  12. #include <Uefi/UefiBaseType.h>
  13. #include <Guid/SmramMemoryReserve.h>
  14. #include <Ppi/MmAccess.h>
  15. #include <IndustryStandard/Pci22.h>
  16. #define SMM_ACCESS_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('4', '5', 's', 'a')
  17. ///
  18. /// Private data
  19. ///
  20. typedef struct {
  21. UINTN Signature;
  22. EFI_HANDLE Handle;
  23. EFI_PEI_MM_ACCESS_PPI SmmAccess;
  24. //
  25. // Local Data for SMM Access interface goes here
  26. //
  27. UINTN NumberRegions;
  28. EFI_SMRAM_DESCRIPTOR *SmramDesc;
  29. } SMM_ACCESS_PRIVATE_DATA;
  30. #define SMM_ACCESS_PRIVATE_DATA_FROM_THIS(a) \
  31. CR (a, \
  32. SMM_ACCESS_PRIVATE_DATA, \
  33. SmmAccess, \
  34. SMM_ACCESS_PRIVATE_DATA_SIGNATURE \
  35. )
  36. /**
  37. This routine accepts a request to "open" a region of SMRAM. The
  38. region could be legacy ABSEG, HSEG, or TSEG near top of physical memory.
  39. The use of "open" means that the memory is visible from all PEIM
  40. and SMM agents.
  41. @param[in] PeiServices - General purpose services available to every PEIM.
  42. @param[in] This - Pointer to the SMM Access Interface.
  43. @param[in] DescriptorIndex - Region of SMRAM to Open.
  44. @retval EFI_SUCCESS - The region was successfully opened.
  45. @retval EFI_DEVICE_ERROR - The region could not be opened because locked by
  46. chipset.
  47. @retval EFI_INVALID_PARAMETER - The descriptor index was out of bounds.
  48. **/
  49. EFI_STATUS
  50. EFIAPI
  51. Open (
  52. IN EFI_PEI_SERVICES **PeiServices,
  53. IN EFI_PEI_MM_ACCESS_PPI *This,
  54. IN UINTN DescriptorIndex
  55. )
  56. {
  57. SMM_ACCESS_PRIVATE_DATA *SmmAccess;
  58. SmmAccess = SMM_ACCESS_PRIVATE_DATA_FROM_THIS (This);
  59. if (DescriptorIndex >= SmmAccess->NumberRegions) {
  60. DEBUG ((DEBUG_WARN, "SMRAM region out of range\n"));
  61. return EFI_INVALID_PARAMETER;
  62. } else if (SmmAccess->SmramDesc[DescriptorIndex].RegionState & EFI_SMRAM_LOCKED) {
  63. //
  64. // Cannot open a "locked" region
  65. //
  66. DEBUG ((DEBUG_WARN, "Cannot open a locked SMRAM region\n"));
  67. return EFI_DEVICE_ERROR;
  68. }
  69. SmmAccess->SmramDesc[DescriptorIndex].RegionState &= (UINT64) ~(EFI_SMRAM_CLOSED | EFI_ALLOCATED);
  70. SmmAccess->SmramDesc[DescriptorIndex].RegionState |= (UINT64) EFI_SMRAM_OPEN;
  71. SmmAccess->SmmAccess.OpenState = TRUE;
  72. return EFI_SUCCESS;
  73. }
  74. /**
  75. This routine accepts a request to "close" a region of SMRAM. This is valid for
  76. compatible SMRAM region.
  77. @param[in] PeiServices - General purpose services available to every PEIM.
  78. @param[in] This - Pointer to the SMM Access Interface.
  79. @param[in] DescriptorIndex - Region of SMRAM to Close.
  80. @retval EFI_SUCCESS - The region was successfully closed.
  81. @retval EFI_DEVICE_ERROR - The region could not be closed because locked by
  82. chipset.
  83. @retval EFI_INVALID_PARAMETER - The descriptor index was out of bounds.
  84. **/
  85. EFI_STATUS
  86. EFIAPI
  87. Close (
  88. IN EFI_PEI_SERVICES **PeiServices,
  89. IN EFI_PEI_MM_ACCESS_PPI *This,
  90. IN UINTN DescriptorIndex
  91. )
  92. {
  93. SMM_ACCESS_PRIVATE_DATA *SmmAccess;
  94. BOOLEAN OpenState;
  95. UINT8 Index;
  96. SmmAccess = SMM_ACCESS_PRIVATE_DATA_FROM_THIS (This);
  97. if (DescriptorIndex >= SmmAccess->NumberRegions) {
  98. DEBUG ((DEBUG_WARN, "SMRAM region out of range\n"));
  99. return EFI_INVALID_PARAMETER;
  100. } else if (SmmAccess->SmramDesc[DescriptorIndex].RegionState & EFI_SMRAM_LOCKED) {
  101. //
  102. // Cannot close a "locked" region
  103. //
  104. DEBUG ((DEBUG_WARN, "Cannot close a locked SMRAM region\n"));
  105. return EFI_DEVICE_ERROR;
  106. }
  107. if (SmmAccess->SmramDesc[DescriptorIndex].RegionState & EFI_SMRAM_CLOSED) {
  108. return EFI_DEVICE_ERROR;
  109. }
  110. SmmAccess->SmramDesc[DescriptorIndex].RegionState &= (UINT64) ~EFI_SMRAM_OPEN;
  111. SmmAccess->SmramDesc[DescriptorIndex].RegionState |= (UINT64) (EFI_SMRAM_CLOSED | EFI_ALLOCATED);
  112. //
  113. // Find out if any regions are still open
  114. //
  115. OpenState = FALSE;
  116. for (Index = 0; Index < SmmAccess->NumberRegions; Index++) {
  117. if ((SmmAccess->SmramDesc[Index].RegionState & EFI_SMRAM_OPEN) == EFI_SMRAM_OPEN) {
  118. OpenState = TRUE;
  119. }
  120. }
  121. SmmAccess->SmmAccess.OpenState = OpenState;
  122. return EFI_SUCCESS;
  123. }
  124. /**
  125. This routine accepts a request to "lock" SMRAM. The
  126. region could be legacy AB or TSEG near top of physical memory.
  127. The use of "lock" means that the memory can no longer be opened
  128. to PEIM.
  129. @param[in] PeiServices - General purpose services available to every PEIM.
  130. @param[in] This - Pointer to the SMM Access Interface.
  131. @param[in] DescriptorIndex - Region of SMRAM to Lock.
  132. @retval EFI_SUCCESS - The region was successfully locked.
  133. @retval EFI_DEVICE_ERROR - The region could not be locked because at least
  134. one range is still open.
  135. @retval EFI_INVALID_PARAMETER - The descriptor index was out of bounds.
  136. **/
  137. EFI_STATUS
  138. EFIAPI
  139. Lock (
  140. IN EFI_PEI_SERVICES **PeiServices,
  141. IN EFI_PEI_MM_ACCESS_PPI *This,
  142. IN UINTN DescriptorIndex
  143. )
  144. {
  145. SMM_ACCESS_PRIVATE_DATA *SmmAccess;
  146. SmmAccess = SMM_ACCESS_PRIVATE_DATA_FROM_THIS (This);
  147. if (DescriptorIndex >= SmmAccess->NumberRegions) {
  148. DEBUG ((DEBUG_WARN, "SMRAM region out of range\n"));
  149. return EFI_INVALID_PARAMETER;
  150. } else if (SmmAccess->SmmAccess.OpenState) {
  151. DEBUG ((DEBUG_WARN, "Cannot lock SMRAM when SMRAM regions are still open\n"));
  152. return EFI_DEVICE_ERROR;
  153. }
  154. SmmAccess->SmramDesc[DescriptorIndex].RegionState |= (UINT64) EFI_SMRAM_LOCKED;
  155. SmmAccess->SmmAccess.LockState = TRUE;
  156. return EFI_SUCCESS;
  157. }
  158. /**
  159. This routine services a user request to discover the SMRAM
  160. capabilities of this platform. This will report the possible
  161. ranges that are possible for SMRAM access, based upon the
  162. memory controller capabilities.
  163. @param[in] PeiServices - General purpose services available to every PEIM.
  164. @param[in] This - Pointer to the SMRAM Access Interface.
  165. @param[in, out] SmramMapSize - Pointer to the variable containing size of the
  166. buffer to contain the description information.
  167. @param[in, out] SmramMap - Buffer containing the data describing the Smram
  168. region descriptors.
  169. @retval EFI_BUFFER_TOO_SMALL - The user did not provide a sufficient buffer.
  170. @retval EFI_SUCCESS - The user provided a sufficiently-sized buffer.
  171. **/
  172. EFI_STATUS
  173. EFIAPI
  174. GetCapabilities (
  175. IN EFI_PEI_SERVICES **PeiServices,
  176. IN EFI_PEI_MM_ACCESS_PPI *This,
  177. IN OUT UINTN *SmramMapSize,
  178. IN OUT EFI_SMRAM_DESCRIPTOR *SmramMap
  179. )
  180. {
  181. EFI_STATUS Status;
  182. SMM_ACCESS_PRIVATE_DATA *SmmAccess;
  183. UINTN NecessaryBufferSize;
  184. SmmAccess = SMM_ACCESS_PRIVATE_DATA_FROM_THIS (This);
  185. NecessaryBufferSize = SmmAccess->NumberRegions * sizeof (EFI_SMRAM_DESCRIPTOR);
  186. if (*SmramMapSize < NecessaryBufferSize) {
  187. DEBUG ((DEBUG_WARN, "SMRAM Map Buffer too small\n"));
  188. Status = EFI_BUFFER_TOO_SMALL;
  189. } else {
  190. CopyMem (SmramMap, SmmAccess->SmramDesc, NecessaryBufferSize);
  191. Status = EFI_SUCCESS;
  192. }
  193. *SmramMapSize = NecessaryBufferSize;
  194. return Status;
  195. }
  196. /**
  197. This function is to install an SMM Access PPI
  198. - <b>Introduction</b> \n
  199. An API to install an instance of EFI_PEI_MM_ACCESS_PPI. This PPI is commonly used to control SMM mode memory access for S3 resume.
  200. @retval EFI_SUCCESS - Ppi successfully started and installed.
  201. @retval EFI_NOT_FOUND - Ppi can't be found.
  202. @retval EFI_OUT_OF_RESOURCES - Ppi does not have enough resources to initialize the driver.
  203. **/
  204. EFI_STATUS
  205. EFIAPI
  206. PeiInstallSmmAccessPpi (
  207. VOID
  208. )
  209. {
  210. EFI_STATUS Status;
  211. UINTN Index;
  212. EFI_PEI_PPI_DESCRIPTOR *PpiList;
  213. EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *DescriptorBlock;
  214. SMM_ACCESS_PRIVATE_DATA *SmmAccessPrivate;
  215. VOID *HobList;
  216. EFI_BOOT_MODE BootMode;
  217. Status = PeiServicesGetBootMode (&BootMode);
  218. if (EFI_ERROR (Status)) {
  219. //
  220. // If not in S3 boot path. do nothing
  221. //
  222. return EFI_SUCCESS;
  223. }
  224. if (BootMode != BOOT_ON_S3_RESUME) {
  225. return EFI_SUCCESS;
  226. }
  227. //
  228. // Initialize private data
  229. //
  230. SmmAccessPrivate = AllocateZeroPool (sizeof (*SmmAccessPrivate));
  231. ASSERT (SmmAccessPrivate != NULL);
  232. if (SmmAccessPrivate == NULL) {
  233. return EFI_OUT_OF_RESOURCES;
  234. }
  235. PpiList = AllocateZeroPool (sizeof (*PpiList));
  236. ASSERT (PpiList != NULL);
  237. if (PpiList == NULL) {
  238. return EFI_OUT_OF_RESOURCES;
  239. }
  240. SmmAccessPrivate->Signature = SMM_ACCESS_PRIVATE_DATA_SIGNATURE;
  241. SmmAccessPrivate->Handle = NULL;
  242. //
  243. // Get Hob list
  244. //
  245. HobList = GetFirstGuidHob (&gEfiSmmSmramMemoryGuid);
  246. if (HobList == NULL) {
  247. DEBUG ((DEBUG_WARN, "SmramMemoryReserve HOB not found\n"));
  248. return EFI_NOT_FOUND;
  249. }
  250. DescriptorBlock = (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *) ((UINT8 *) HobList + sizeof (EFI_HOB_GUID_TYPE));
  251. //
  252. // Alloc space for SmmAccessPrivate->SmramDesc
  253. //
  254. SmmAccessPrivate->SmramDesc = AllocateZeroPool ((DescriptorBlock->NumberOfSmmReservedRegions) * sizeof (EFI_SMRAM_DESCRIPTOR));
  255. if (SmmAccessPrivate->SmramDesc == NULL) {
  256. DEBUG ((DEBUG_WARN, "Alloc SmmAccessPrivate->SmramDesc fail.\n"));
  257. return EFI_OUT_OF_RESOURCES;
  258. }
  259. DEBUG ((DEBUG_INFO, "Alloc SmmAccessPrivate->SmramDesc success.\n"));
  260. //
  261. // use the hob to publish SMRAM capabilities
  262. //
  263. for (Index = 0; Index < DescriptorBlock->NumberOfSmmReservedRegions; Index++) {
  264. SmmAccessPrivate->SmramDesc[Index].PhysicalStart = DescriptorBlock->Descriptor[Index].PhysicalStart;
  265. SmmAccessPrivate->SmramDesc[Index].CpuStart = DescriptorBlock->Descriptor[Index].CpuStart;
  266. SmmAccessPrivate->SmramDesc[Index].PhysicalSize = DescriptorBlock->Descriptor[Index].PhysicalSize;
  267. SmmAccessPrivate->SmramDesc[Index].RegionState = DescriptorBlock->Descriptor[Index].RegionState;
  268. }
  269. SmmAccessPrivate->NumberRegions = Index;
  270. SmmAccessPrivate->SmmAccess.Open = Open;
  271. SmmAccessPrivate->SmmAccess.Close = Close;
  272. SmmAccessPrivate->SmmAccess.Lock = Lock;
  273. SmmAccessPrivate->SmmAccess.GetCapabilities = GetCapabilities;
  274. SmmAccessPrivate->SmmAccess.LockState = FALSE;
  275. SmmAccessPrivate->SmmAccess.OpenState = FALSE;
  276. //
  277. // Install PPI
  278. //
  279. PpiList->Flags = (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);
  280. PpiList->Guid = &gEfiPeiMmAccessPpiGuid;
  281. PpiList->Ppi = &SmmAccessPrivate->SmmAccess;
  282. Status = PeiServicesInstallPpi (PpiList);
  283. ASSERT_EFI_ERROR (Status);
  284. return EFI_SUCCESS;
  285. }