]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Library/PlatformInitLib/MemDetect.c
OvmfPkg/PlatformInitLib: Add PlatformGetLowMemoryCB
[mirror_edk2.git] / OvmfPkg / Library / PlatformInitLib / MemDetect.c
index fdfe134247bf6f9e237c9392a854ffa16bf3c39d..57feeb6dab25d75f6a877fb3d3fca159bfcc39c8 100644 (file)
@@ -51,18 +51,16 @@ PlatformQemuUc32BaseInitialization (
   IN OUT EFI_HOB_PLATFORM_INFO  *PlatformInfoHob\r
   )\r
 {\r
-  UINT32  LowerMemorySize;\r
-\r
   if (PlatformInfoHob->HostBridgeDevId == 0xffff /* microvm */) {\r
     return;\r
   }\r
 \r
   if (PlatformInfoHob->HostBridgeDevId == INTEL_Q35_MCH_DEVICE_ID) {\r
-    LowerMemorySize = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
+    PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
     ASSERT (PcdGet64 (PcdPciExpressBaseAddress) <= MAX_UINT32);\r
-    ASSERT (PcdGet64 (PcdPciExpressBaseAddress) >= LowerMemorySize);\r
+    ASSERT (PcdGet64 (PcdPciExpressBaseAddress) >= PlatformInfoHob->LowMemory);\r
 \r
-    if (LowerMemorySize <= BASE_2GB) {\r
+    if (PlatformInfoHob->LowMemory <= BASE_2GB) {\r
       // Newer qemu with gigabyte aligned memory,\r
       // 32-bit pci mmio window is 2G -> 4G then.\r
       PlatformInfoHob->Uc32Base = BASE_2GB;\r
@@ -92,8 +90,8 @@ PlatformQemuUc32BaseInitialization (
   // variable MTRR suffices by truncating the size to a whole power of two,\r
   // while keeping the end affixed to 4GB. This will round the base up.\r
   //\r
-  LowerMemorySize           = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
-  PlatformInfoHob->Uc32Size = GetPowerOfTwo32 ((UINT32)(SIZE_4GB - LowerMemorySize));\r
+  PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
+  PlatformInfoHob->Uc32Size = GetPowerOfTwo32 ((UINT32)(SIZE_4GB - PlatformInfoHob->LowMemory));\r
   PlatformInfoHob->Uc32Base = (UINT32)(SIZE_4GB - PlatformInfoHob->Uc32Size);\r
   //\r
   // Assuming that LowerMemorySize is at least 1 byte, Uc32Size is at most 2GB.\r
@@ -101,13 +99,13 @@ PlatformQemuUc32BaseInitialization (
   //\r
   ASSERT (PlatformInfoHob->Uc32Base >= BASE_2GB);\r
 \r
-  if (PlatformInfoHob->Uc32Base != LowerMemorySize) {\r
+  if (PlatformInfoHob->Uc32Base != PlatformInfoHob->LowMemory) {\r
     DEBUG ((\r
       DEBUG_VERBOSE,\r
       "%a: rounded UC32 base from 0x%x up to 0x%x, for "\r
       "an UC32 size of 0x%x\n",\r
       __FUNCTION__,\r
-      LowerMemorySize,\r
+      PlatformInfoHob->LowMemory,\r
       PlatformInfoHob->Uc32Base,\r
       PlatformInfoHob->Uc32Size\r
       ));\r
@@ -280,6 +278,34 @@ PlatformGetFirstNonAddressCB (
   }\r
 }\r
 \r
+/**\r
+  Store the low (below 4G) memory size in\r
+  PlatformInfoHob->LowMemory\r
+**/\r
+STATIC\r
+VOID\r
+PlatformGetLowMemoryCB (\r
+  IN     EFI_E820_ENTRY64       *E820Entry,\r
+  IN OUT EFI_HOB_PLATFORM_INFO  *PlatformInfoHob\r
+  )\r
+{\r
+  UINT64  Candidate;\r
+\r
+  if (E820Entry->Type != EfiAcpiAddressRangeMemory) {\r
+    return;\r
+  }\r
+\r
+  Candidate = E820Entry->BaseAddr + E820Entry->Length;\r
+  if (Candidate >= BASE_4GB) {\r
+    return;\r
+  }\r
+\r
+  if (PlatformInfoHob->LowMemory < Candidate) {\r
+    DEBUG ((DEBUG_INFO, "%a: LowMemory=0x%Lx\n", __FUNCTION__, Candidate));\r
+    PlatformInfoHob->LowMemory = (UINT32)Candidate;\r
+  }\r
+}\r
+\r
 /**\r
   Iterate over the entries in QEMU's fw_cfg E820 RAM map, call the\r
   passed callback for each entry.\r
@@ -396,14 +422,13 @@ GetHighestSystemMemoryAddressFromPvhMemmap (
   return HighestAddress;\r
 }\r
 \r
-UINT32\r
+VOID\r
 EFIAPI\r
 PlatformGetSystemMemorySizeBelow4gb (\r
   IN EFI_HOB_PLATFORM_INFO  *PlatformInfoHob\r
   )\r
 {\r
   EFI_STATUS  Status;\r
-  UINT64      LowerMemorySize = 0;\r
   UINT8       Cmos0x34;\r
   UINT8       Cmos0x35;\r
 \r
@@ -411,12 +436,13 @@ PlatformGetSystemMemorySizeBelow4gb (
       (CcProbe () != CcGuestTypeIntelTdx))\r
   {\r
     // Get the information from PVH memmap\r
-    return (UINT32)GetHighestSystemMemoryAddressFromPvhMemmap (TRUE);\r
+    PlatformInfoHob->LowMemory = (UINT32)GetHighestSystemMemoryAddressFromPvhMemmap (TRUE);\r
+    return;\r
   }\r
 \r
-  Status = PlatformScanOrAdd64BitE820Ram (FALSE, &LowerMemorySize, NULL);\r
-  if ((Status == EFI_SUCCESS) && (LowerMemorySize > 0)) {\r
-    return (UINT32)LowerMemorySize;\r
+  Status = PlatformScanE820 (PlatformGetLowMemoryCB, PlatformInfoHob);\r
+  if (!EFI_ERROR (Status) && (PlatformInfoHob->LowMemory > 0)) {\r
+    return;\r
   }\r
 \r
   //\r
@@ -431,7 +457,7 @@ PlatformGetSystemMemorySizeBelow4gb (
   Cmos0x34 = (UINT8)PlatformCmosRead8 (0x34);\r
   Cmos0x35 = (UINT8)PlatformCmosRead8 (0x35);\r
 \r
-  return (UINT32)(((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r
+  PlatformInfoHob->LowMemory = (UINT32)(((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r
 }\r
 \r
 STATIC\r
@@ -967,7 +993,6 @@ PlatformQemuInitializeRam (
   IN EFI_HOB_PLATFORM_INFO  *PlatformInfoHob\r
   )\r
 {\r
-  UINT64         LowerMemorySize;\r
   UINT64         UpperMemorySize;\r
   MTRR_SETTINGS  MtrrSettings;\r
   EFI_STATUS     Status;\r
@@ -977,7 +1002,7 @@ PlatformQemuInitializeRam (
   //\r
   // Determine total memory size available\r
   //\r
-  LowerMemorySize = PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
+  PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
 \r
   if (PlatformInfoHob->BootMode == BOOT_ON_S3_RESUME) {\r
     //\r
@@ -1011,14 +1036,14 @@ PlatformQemuInitializeRam (
       UINT32  TsegSize;\r
 \r
       TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;\r
-      PlatformAddMemoryRangeHob (BASE_1MB, LowerMemorySize - TsegSize);\r
+      PlatformAddMemoryRangeHob (BASE_1MB, PlatformInfoHob->LowMemory - TsegSize);\r
       PlatformAddReservedMemoryBaseSizeHob (\r
-        LowerMemorySize - TsegSize,\r
+        PlatformInfoHob->LowMemory - TsegSize,\r
         TsegSize,\r
         TRUE\r
         );\r
     } else {\r
-      PlatformAddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r
+      PlatformAddMemoryRangeHob (BASE_1MB, PlatformInfoHob->LowMemory);\r
     }\r
 \r
     //\r
@@ -1196,9 +1221,10 @@ PlatformQemuInitializeRamForS3 (
       // Make sure the TSEG area that we reported as a reserved memory resource\r
       // cannot be used for reserved memory allocations.\r
       //\r
+      PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob);\r
       TsegSize = PlatformInfoHob->Q35TsegMbytes * SIZE_1MB;\r
       BuildMemoryAllocationHob (\r
-        PlatformGetSystemMemorySizeBelow4gb (PlatformInfoHob) - TsegSize,\r
+        PlatformInfoHob->LowMemory - TsegSize,\r
         TsegSize,\r
         EfiReservedMemoryType\r
         );\r