--- /dev/null
+/** @file\r
+ This is the driver that produce AcpiVariable hob and slit SmramReserve hob\r
+ for ECP platform.\r
+\r
+Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
+\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions\r
+of the BSD License which accompanies this distribution. The\r
+full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <PiPei.h>\r
+#include <Guid/SmramMemoryReserve.h>\r
+#include <Guid/AcpiVariable.h>\r
+\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/HobLib.h>\r
+#include <Library/PeiServicesLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+\r
+/**\r
+ Retrieves the data structure associated witht he GUIDed HOB of type gEfiSmmPeiSmramMemoryReserveGuid\r
+ \r
+ @retval NULL A HOB of type gEfiSmmPeiSmramMemoryReserveGuid could not be found.\r
+ @retval !NULL A pointer to the GUID data from a HIB of type gEfiSmmPeiSmramMemoryReserveGuid\r
+\r
+**/\r
+EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *\r
+GetSrmamHobData (\r
+ VOID\r
+ )\r
+{\r
+ VOID *GuidHob;\r
+\r
+ //\r
+ // Search SmramMemoryReserve HOB that describes SMRAM region\r
+ //\r
+ GuidHob = GetFirstGuidHob (&gEfiSmmPeiSmramMemoryReserveGuid);\r
+ if (GuidHob == NULL) {\r
+ return NULL;\r
+ }\r
+ return (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *)GET_GUID_HOB_DATA (GuidHob);\r
+}\r
+\r
+/**\r
+ This routine will split SmramReserve hob to reserve 1 page for SMRAM content in S3 phase\r
+ for R9 SMM core.\r
+ \r
+ @retval EFI_SUCCESS The gEfiSmmPeiSmramMemoryReserveGuid is splited successfully.\r
+ @retval EFI_NOT_FOUND The gEfiSmmPeiSmramMemoryReserveGuid is not found.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SplitSmramReserveHob (\r
+ VOID\r
+ )\r
+{\r
+ EFI_HOB_GUID_TYPE *GuidHob;\r
+ EFI_PEI_HOB_POINTERS Hob;\r
+ EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *DescriptorBlock;\r
+ EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *NewDescriptorBlock;\r
+ UINTN BufferSize;\r
+ UINTN SmramRanges;\r
+ UINTN Index;\r
+ UINTN SubIndex;\r
+\r
+ //\r
+ // Retrieve the GUID HOB data that contains the set of SMRAM descriptyors\r
+ //\r
+ GuidHob = GetFirstGuidHob (&gEfiSmmPeiSmramMemoryReserveGuid);\r
+ if (GuidHob == NULL) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ DescriptorBlock = (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *)GET_GUID_HOB_DATA (GuidHob);\r
+\r
+ //\r
+ // Allocate one extra EFI_SMRAM_DESCRIPTOR to describe a page of SMRAM memory that contains a pointer\r
+ // to the SMM Services Table that is required on the S3 resume path\r
+ //\r
+ SmramRanges = DescriptorBlock->NumberOfSmmReservedRegions;\r
+ BufferSize = sizeof (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK) + (SmramRanges * sizeof (EFI_SMRAM_DESCRIPTOR));\r
+\r
+ Hob.Raw = BuildGuidHob (\r
+ &gEfiSmmPeiSmramMemoryReserveGuid,\r
+ BufferSize\r
+ );\r
+ ASSERT (Hob.Raw);\r
+ NewDescriptorBlock = (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *)Hob.Raw;\r
+\r
+ //\r
+ // Copy old EFI_SMRAM_HOB_DESCRIPTOR_BLOCK to new allocated region\r
+ //\r
+ CopyMem ((VOID *)Hob.Raw, DescriptorBlock, BufferSize - sizeof(EFI_SMRAM_DESCRIPTOR));\r
+\r
+ //\r
+ // Increase the number of SMRAM descriptors by 1 to make room for the ALLOCATED descriptor of size EFI_PAGE_SIZE\r
+ //\r
+ NewDescriptorBlock->NumberOfSmmReservedRegions = (UINT32)(SmramRanges + 1);\r
+\r
+ ASSERT (SmramRanges >= 1);\r
+ //\r
+ // Copy last entry to the end - we assume TSEG is last entry, which is same assumption as R8 CPU/SMM driver\r
+ //\r
+ CopyMem (&NewDescriptorBlock->Descriptor[SmramRanges], &NewDescriptorBlock->Descriptor[SmramRanges - 1], sizeof(EFI_SMRAM_DESCRIPTOR));\r
+\r
+ //\r
+ // Update the last but 1 entry in the array with a size of EFI_PAGE_SIZE and put into the ALLOCATED state\r
+ //\r
+ NewDescriptorBlock->Descriptor[SmramRanges - 1].PhysicalSize = EFI_PAGE_SIZE;\r
+ NewDescriptorBlock->Descriptor[SmramRanges - 1].RegionState |= EFI_ALLOCATED;\r
+\r
+ //\r
+ // Reduce the size of the last SMRAM descriptor by EFI_PAGE_SIZE \r
+ //\r
+ NewDescriptorBlock->Descriptor[SmramRanges].PhysicalStart += EFI_PAGE_SIZE;\r
+ NewDescriptorBlock->Descriptor[SmramRanges].CpuStart += EFI_PAGE_SIZE;\r
+ NewDescriptorBlock->Descriptor[SmramRanges].PhysicalSize -= EFI_PAGE_SIZE;\r
+\r
+ //\r
+ // Now, we have created SmramReserve Hob for SmmAccess drive. But the issue is that, R8 SmmAccess will assume there is 2 SmramReserve region only.\r
+ // Reporting 3 SmramReserve region will cause buffer overflow. Moreover, we would like to filter AB-SEG or H-SEG to avoid SMM cache-poisoning issue.\r
+ // So we uses scan SmmReserve Hob to remove AB-SEG or H-SEG.\r
+ //\r
+ for (Index = 0; Index <= SmramRanges; Index++) {\r
+ if (NewDescriptorBlock->Descriptor[Index].PhysicalSize == 0) {\r
+ //\r
+ // Skip zero entry\r
+ //\r
+ continue;\r
+ }\r
+ if (NewDescriptorBlock->Descriptor[Index].PhysicalStart < BASE_1MB) {\r
+ //\r
+ // Find AB-SEG or H-SEG\r
+ // remove this region\r
+ //\r
+ for (SubIndex = Index; SubIndex < NewDescriptorBlock->NumberOfSmmReservedRegions - 1; SubIndex++) {\r
+ CopyMem (&NewDescriptorBlock->Descriptor[SubIndex], &NewDescriptorBlock->Descriptor[SubIndex + 1], sizeof (EFI_SMRAM_DESCRIPTOR));\r
+ }\r
+ //\r
+ // Zero last one\r
+ //\r
+ ZeroMem (&NewDescriptorBlock->Descriptor[SubIndex], sizeof(EFI_SMRAM_DESCRIPTOR));\r
+ //\r
+ // Decrease Number\r
+ //\r
+ NewDescriptorBlock->NumberOfSmmReservedRegions --;\r
+ //\r
+ // Decrease Index to let it test mew entry\r
+ //\r
+ Index --;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Last step, we can scrub old one\r
+ //\r
+ ZeroMem (&GuidHob->Name, sizeof(GuidHob->Name));\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ This routine will create AcpiVariable hob to point the reserved smram in S3 phase\r
+ for R9 SMM core.\r
+ \r
+ @retval EFI_SUCCESS The gEfiAcpiVariableGuid is created successfully.\r
+ @retval EFI_NOT_FOUND The gEfiSmmPeiSmramMemoryReserveGuid is not found.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CreateAcpiVariableHob (\r
+ VOID\r
+ )\r
+{\r
+ EFI_PEI_HOB_POINTERS Hob;\r
+ EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *DescriptorBlock;\r
+ UINTN SmramRanges;\r
+\r
+ //\r
+ // Retrieve the GUID HOB data that contains the set of SMRAM descriptyors\r
+ //\r
+ DescriptorBlock = GetSrmamHobData ();\r
+ if (DescriptorBlock == NULL) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ Hob.Raw = BuildGuidHob (\r
+ &gEfiAcpiVariableGuid,\r
+ sizeof (EFI_SMRAM_DESCRIPTOR)\r
+ );\r
+ ASSERT (Hob.Raw);\r
+\r
+ //\r
+ // It should be already patch, so just copy last but 1 region directly.\r
+ //\r
+ SmramRanges = DescriptorBlock->NumberOfSmmReservedRegions;\r
+ ASSERT (SmramRanges >= 2);\r
+ if (SmramRanges >= 2) {\r
+ CopyMem ((VOID *)Hob.Raw, &DescriptorBlock->Descriptor[SmramRanges - 2], sizeof (EFI_SMRAM_DESCRIPTOR));\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Driver Entry for AcpiVariableHobOnSmramReservHob PEIM\r
+ \r
+ @param FileHandle Handle of the file being invoked.\r
+ @param PeiServices Describes the list of possible PEI Services.\r
+ \r
+ @retval EFI_SUCCESS Success create gEfiAcpiVariableGuid and\r
+ split gEfiSmmPeiSmramMemoryReserveGuid.\r
+ @retval EFI_NOT_FOUND Can not get gEfiSmmPeiSmramMemoryReserveGuid hob\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+AcpiVariableHobEntry (\r
+ IN EFI_PEI_FILE_HANDLE FileHandle,\r
+ IN CONST EFI_PEI_SERVICES **PeiServices\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Split SmramReserve hob, which is required for R9 SMM Core for S3.\r
+ //\r
+ Status = SplitSmramReserveHob ();\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Create AcpiVariable hob, which is required for R9 SMM Core for S3.\r
+ //\r
+ Status = CreateAcpiVariableHob ();\r
+\r
+ return Status;\r
+}\r