]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/PlatformPei/MemDetect.c
OvmfPkg: PlatformPei: account for TSEG size with PcdSmmSmramRequire set
[mirror_edk2.git] / OvmfPkg / PlatformPei / MemDetect.c
index bd57b7792e212e37263b11fa878fe474d16d11f9..1bdc2df6ed91910fa30411f29abcf377027cb63e 100644 (file)
@@ -1,7 +1,7 @@
 /**@file\r
   Memory Detection for Virtual Machines.\r
 \r
-  Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -24,6 +24,7 @@ Module Name:
 //\r
 // The Library classes this module consumes\r
 //\r
+#include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/HobLib.h>\r
 #include <Library/IoLib.h>\r
@@ -35,9 +36,11 @@ Module Name:
 #include "Platform.h"\r
 #include "Cmos.h"\r
 \r
-STATIC\r
-UINTN\r
+UINT8 mPhysMemAddressWidth;\r
+\r
+UINT32\r
 GetSystemMemorySizeBelow4gb (\r
+  VOID\r
   )\r
 {\r
   UINT8 Cmos0x34;\r
@@ -55,7 +58,7 @@ GetSystemMemorySizeBelow4gb (
   Cmos0x34 = (UINT8) CmosRead8 (0x34);\r
   Cmos0x35 = (UINT8) CmosRead8 (0x35);\r
 \r
-  return (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r
+  return (UINT32) (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r
 }\r
 \r
 \r
@@ -83,6 +86,112 @@ GetSystemMemorySizeAbove4gb (
   return LShiftU64 (Size, 16);\r
 }\r
 \r
+\r
+/**\r
+  Initialize the mPhysMemAddressWidth variable, based on guest RAM size.\r
+**/\r
+VOID\r
+AddressWidthInitialization (\r
+  VOID\r
+  )\r
+{\r
+  UINT64 FirstNonAddress;\r
+\r
+  //\r
+  // As guest-physical memory size grows, the permanent PEI RAM requirements\r
+  // are dominated by the identity-mapping page tables built by the DXE IPL.\r
+  // The DXL IPL keys off of the physical address bits advertized in the CPU\r
+  // HOB. To conserve memory, we calculate the minimum address width here.\r
+  //\r
+  FirstNonAddress      = BASE_4GB + GetSystemMemorySizeAbove4gb ();\r
+  mPhysMemAddressWidth = (UINT8)HighBitSet64 (FirstNonAddress);\r
+\r
+  //\r
+  // If FirstNonAddress is not an integral power of two, then we need an\r
+  // additional bit.\r
+  //\r
+  if ((FirstNonAddress & (FirstNonAddress - 1)) != 0) {\r
+    ++mPhysMemAddressWidth;\r
+  }\r
+\r
+  //\r
+  // The minimum address width is 36 (covers up to and excluding 64 GB, which\r
+  // is the maximum for Ia32 + PAE). The theoretical architecture maximum for\r
+  // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We\r
+  // can simply assert that here, since 48 bits are good enough for 256 TB.\r
+  //\r
+  if (mPhysMemAddressWidth <= 36) {\r
+    mPhysMemAddressWidth = 36;\r
+  }\r
+  ASSERT (mPhysMemAddressWidth <= 48);\r
+}\r
+\r
+\r
+/**\r
+  Calculate the cap for the permanent PEI memory.\r
+**/\r
+STATIC\r
+UINT32\r
+GetPeiMemoryCap (\r
+  VOID\r
+  )\r
+{\r
+  BOOLEAN Page1GSupport;\r
+  UINT32  RegEax;\r
+  UINT32  RegEdx;\r
+  UINT32  Pml4Entries;\r
+  UINT32  PdpEntries;\r
+  UINTN   TotalPages;\r
+\r
+  //\r
+  // If DXE is 32-bit, then just return the traditional 64 MB cap.\r
+  //\r
+#ifdef MDE_CPU_IA32\r
+  if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
+    return SIZE_64MB;\r
+  }\r
+#endif\r
+\r
+  //\r
+  // Dependent on physical address width, PEI memory allocations can be\r
+  // dominated by the page tables built for 64-bit DXE. So we key the cap off\r
+  // of those. The code below is based on CreateIdentityMappingPageTables() in\r
+  // "MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c".\r
+  //\r
+  Page1GSupport = FALSE;\r
+  if (PcdGetBool (PcdUse1GPageTable)) {\r
+    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
+    if (RegEax >= 0x80000001) {\r
+      AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r
+      if ((RegEdx & BIT26) != 0) {\r
+        Page1GSupport = TRUE;\r
+      }\r
+    }\r
+  }\r
+\r
+  if (mPhysMemAddressWidth <= 39) {\r
+    Pml4Entries = 1;\r
+    PdpEntries = 1 << (mPhysMemAddressWidth - 30);\r
+    ASSERT (PdpEntries <= 0x200);\r
+  } else {\r
+    Pml4Entries = 1 << (mPhysMemAddressWidth - 39);\r
+    ASSERT (Pml4Entries <= 0x200);\r
+    PdpEntries = 512;\r
+  }\r
+\r
+  TotalPages = Page1GSupport ? Pml4Entries + 1 :\r
+                               (PdpEntries + 1) * Pml4Entries + 1;\r
+  ASSERT (TotalPages <= 0x40201);\r
+\r
+  //\r
+  // Add 64 MB for miscellaneous allocations. Note that for\r
+  // mPhysMemAddressWidth values close to 36, the cap will actually be\r
+  // dominated by this increment.\r
+  //\r
+  return (UINT32)(EFI_PAGES_TO_SIZE (TotalPages) + SIZE_64MB);\r
+}\r
+\r
+\r
 /**\r
   Publish PEI core memory\r
 \r
@@ -98,17 +207,42 @@ PublishPeiMemory (
   EFI_PHYSICAL_ADDRESS        MemoryBase;\r
   UINT64                      MemorySize;\r
   UINT64                      LowerMemorySize;\r
-\r
-  LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r
-\r
-  //\r
-  // Determine the range of memory to use during PEI\r
-  //\r
-  MemoryBase = PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMemFvSize);\r
-  MemorySize = LowerMemorySize - MemoryBase;\r
-  if (MemorySize > SIZE_64MB) {\r
-    MemoryBase = LowerMemorySize - SIZE_64MB;\r
-    MemorySize = SIZE_64MB;\r
+  UINT32                      PeiMemoryCap;\r
+\r
+  if (mBootMode == BOOT_ON_S3_RESUME) {\r
+    MemoryBase = PcdGet32 (PcdS3AcpiReservedMemoryBase);\r
+    MemorySize = PcdGet32 (PcdS3AcpiReservedMemorySize);\r
+  } else {\r
+    LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r
+    if (FeaturePcdGet (PcdSmmSmramRequire)) {\r
+      //\r
+      // TSEG is chipped from the end of low RAM\r
+      //\r
+      LowerMemorySize -= FixedPcdGet8 (PcdQ35TsegMbytes) * SIZE_1MB;\r
+    }\r
+\r
+    PeiMemoryCap = GetPeiMemoryCap ();\r
+    DEBUG ((EFI_D_INFO, "%a: mPhysMemAddressWidth=%d PeiMemoryCap=%u KB\n",\r
+      __FUNCTION__, mPhysMemAddressWidth, PeiMemoryCap >> 10));\r
+\r
+    //\r
+    // Determine the range of memory to use during PEI\r
+    //\r
+    // Technically we could lay the permanent PEI RAM over SEC's temporary\r
+    // decompression and scratch buffer even if "secure S3" is needed, since\r
+    // their lifetimes don't overlap. However, PeiFvInitialization() will cover\r
+    // RAM up to PcdOvmfDecompressionScratchEnd with an EfiACPIMemoryNVS memory\r
+    // allocation HOB, and other allocations served from the permanent PEI RAM\r
+    // shouldn't overlap with that HOB.\r
+    //\r
+    MemoryBase = mS3Supported && FeaturePcdGet (PcdSmmSmramRequire) ?\r
+      PcdGet32 (PcdOvmfDecompressionScratchEnd) :\r
+      PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMemFvSize);\r
+    MemorySize = LowerMemorySize - MemoryBase;\r
+    if (MemorySize > PeiMemoryCap) {\r
+      MemoryBase = LowerMemorySize - PeiMemoryCap;\r
+      MemorySize = PeiMemoryCap;\r
+    }\r
   }\r
 \r
   //\r
@@ -122,19 +256,21 @@ PublishPeiMemory (
 \r
 \r
 /**\r
-  Peform Memory Detection\r
-\r
-  @return Top of memory\r
+  Peform Memory Detection for QEMU / KVM\r
 \r
 **/\r
-EFI_PHYSICAL_ADDRESS\r
-MemDetect (\r
+STATIC\r
+VOID\r
+QemuInitializeRam (\r
+  VOID\r
   )\r
 {\r
   UINT64                      LowerMemorySize;\r
   UINT64                      UpperMemorySize;\r
+  MTRR_SETTINGS               MtrrSettings;\r
+  EFI_STATUS                  Status;\r
 \r
-  DEBUG ((EFI_D_ERROR, "MemDetect called\n"));\r
+  DEBUG ((EFI_D_INFO, "%a called\n", __FUNCTION__));\r
 \r
   //\r
   // Determine total memory size available\r
@@ -142,24 +278,168 @@ MemDetect (
   LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r
   UpperMemorySize = GetSystemMemorySizeAbove4gb ();\r
 \r
-  PublishPeiMemory ();\r
+  if (mBootMode != BOOT_ON_S3_RESUME) {\r
+    //\r
+    // Create memory HOBs\r
+    //\r
+    AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r
+\r
+    if (FeaturePcdGet (PcdSmmSmramRequire)) {\r
+      UINT32 TsegSize;\r
+\r
+      TsegSize = FixedPcdGet8 (PcdQ35TsegMbytes) * SIZE_1MB;\r
+      AddMemoryRangeHob (BASE_1MB, LowerMemorySize - TsegSize);\r
+      AddReservedMemoryBaseSizeHob (LowerMemorySize - TsegSize, TsegSize,\r
+        TRUE);\r
+    } else {\r
+      AddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r
+    }\r
+\r
+    if (UpperMemorySize != 0) {\r
+      AddUntestedMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r
+    }\r
+  }\r
 \r
   //\r
-  // Create memory HOBs\r
+  // We'd like to keep the following ranges uncached:\r
+  // - [640 KB, 1 MB)\r
+  // - [LowerMemorySize, 4 GB)\r
   //\r
-  AddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r
-  AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r
-\r
-  MtrrSetMemoryAttribute (BASE_1MB, LowerMemorySize - BASE_1MB, CacheWriteBack);\r
+  // Everything else should be WB. Unfortunately, programming the inverse (ie.\r
+  // keeping the default UC, and configuring the complement set of the above as\r
+  // WB) is not reliable in general, because the end of the upper RAM can have\r
+  // practically any alignment, and we may not have enough variable MTRRs to\r
+  // cover it exactly.\r
+  //\r
+  if (IsMtrrSupported ()) {\r
+    MtrrGetAllMtrrs (&MtrrSettings);\r
+\r
+    //\r
+    // MTRRs disabled, fixed MTRRs disabled, default type is uncached\r
+    //\r
+    ASSERT ((MtrrSettings.MtrrDefType & BIT11) == 0);\r
+    ASSERT ((MtrrSettings.MtrrDefType & BIT10) == 0);\r
+    ASSERT ((MtrrSettings.MtrrDefType & 0xFF) == 0);\r
+\r
+    //\r
+    // flip default type to writeback\r
+    //\r
+    SetMem (&MtrrSettings.Fixed, sizeof MtrrSettings.Fixed, 0x06);\r
+    ZeroMem (&MtrrSettings.Variables, sizeof MtrrSettings.Variables);\r
+    MtrrSettings.MtrrDefType |= BIT11 | BIT10 | 6;\r
+    MtrrSetAllMtrrs (&MtrrSettings);\r
+\r
+    //\r
+    // Set memory range from 640KB to 1MB to uncacheable\r
+    //\r
+    Status = MtrrSetMemoryAttribute (BASE_512KB + BASE_128KB,\r
+               BASE_1MB - (BASE_512KB + BASE_128KB), CacheUncacheable);\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    //\r
+    // Set memory range from the "top of lower RAM" (RAM below 4GB) to 4GB as\r
+    // uncacheable\r
+    //\r
+    Status = MtrrSetMemoryAttribute (LowerMemorySize,\r
+               SIZE_4GB - LowerMemorySize, CacheUncacheable);\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+}\r
 \r
-  MtrrSetMemoryAttribute (0, BASE_512KB + BASE_128KB, CacheWriteBack);\r
+/**\r
+  Publish system RAM and reserve memory regions\r
 \r
-  if (UpperMemorySize != 0) {\r
-    AddUntestedMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r
+**/\r
+VOID\r
+InitializeRamRegions (\r
+  VOID\r
+  )\r
+{\r
+  if (!mXen) {\r
+    QemuInitializeRam ();\r
+  } else {\r
+    XenPublishRamRegions ();\r
+  }\r
 \r
-    MtrrSetMemoryAttribute (BASE_4GB, UpperMemorySize, CacheWriteBack);\r
+  if (mS3Supported && mBootMode != BOOT_ON_S3_RESUME) {\r
+    //\r
+    // This is the memory range that will be used for PEI on S3 resume\r
+    //\r
+    BuildMemoryAllocationHob (\r
+      (EFI_PHYSICAL_ADDRESS)(UINTN) PcdGet32 (PcdS3AcpiReservedMemoryBase),\r
+      (UINT64)(UINTN) PcdGet32 (PcdS3AcpiReservedMemorySize),\r
+      EfiACPIMemoryNVS\r
+      );\r
+\r
+    //\r
+    // Cover the initial RAM area used as stack and temporary PEI heap.\r
+    //\r
+    // This is reserved as ACPI NVS so it can be used on S3 resume.\r
+    //\r
+    BuildMemoryAllocationHob (\r
+      PcdGet32 (PcdOvmfSecPeiTempRamBase),\r
+      PcdGet32 (PcdOvmfSecPeiTempRamSize),\r
+      EfiACPIMemoryNVS\r
+      );\r
+\r
+    //\r
+    // SEC stores its table of GUIDed section handlers here.\r
+    //\r
+    BuildMemoryAllocationHob (\r
+      PcdGet64 (PcdGuidedExtractHandlerTableAddress),\r
+      PcdGet32 (PcdGuidedExtractHandlerTableSize),\r
+      EfiACPIMemoryNVS\r
+      );\r
+\r
+#ifdef MDE_CPU_X64\r
+    //\r
+    // Reserve the initial page tables built by the reset vector code.\r
+    //\r
+    // Since this memory range will be used by the Reset Vector on S3\r
+    // resume, it must be reserved as ACPI NVS.\r
+    //\r
+    BuildMemoryAllocationHob (\r
+      (EFI_PHYSICAL_ADDRESS)(UINTN) PcdGet32 (PcdOvmfSecPageTablesBase),\r
+      (UINT64)(UINTN) PcdGet32 (PcdOvmfSecPageTablesSize),\r
+      EfiACPIMemoryNVS\r
+      );\r
+#endif\r
   }\r
 \r
-  return LowerMemorySize;\r
+  if (mBootMode != BOOT_ON_S3_RESUME) {\r
+    //\r
+    // Reserve the lock box storage area\r
+    //\r
+    // Since this memory range will be used on S3 resume, it must be\r
+    // reserved as ACPI NVS.\r
+    //\r
+    // If S3 is unsupported, then various drivers might still write to the\r
+    // LockBox area. We ought to prevent DXE from serving allocation requests\r
+    // such that they would overlap the LockBox storage.\r
+    //\r
+    ZeroMem (\r
+      (VOID*)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageBase),\r
+      (UINTN) PcdGet32 (PcdOvmfLockBoxStorageSize)\r
+      );\r
+    BuildMemoryAllocationHob (\r
+      (EFI_PHYSICAL_ADDRESS)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageBase),\r
+      (UINT64)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageSize),\r
+      mS3Supported ? EfiACPIMemoryNVS : EfiBootServicesData\r
+      );\r
+\r
+    if (FeaturePcdGet (PcdSmmSmramRequire)) {\r
+      UINT32 TsegSize;\r
+\r
+      //\r
+      // Make sure the TSEG area that we reported as a reserved memory resource\r
+      // cannot be used for reserved memory allocations.\r
+      //\r
+      TsegSize = FixedPcdGet8 (PcdQ35TsegMbytes) * SIZE_1MB;\r
+      BuildMemoryAllocationHob (\r
+        GetSystemMemorySizeBelow4gb() - TsegSize,\r
+        TsegSize,\r
+        EfiReservedMemoryType\r
+        );\r
+    }\r
+  }\r
 }\r
-\r