]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/PlatformPei/MemDetect.c
OvmfPkg/PlatformPei: debug log "etc/reserved-memory-end" from fw_cfg
[mirror_edk2.git] / OvmfPkg / PlatformPei / MemDetect.c
index 97f3fa5afcf5178585c89127336fad50d3576799..2f9e835513649457e2746015a7042cb161d34728 100644 (file)
@@ -19,6 +19,7 @@ Module Name:
 //\r
 // The package level header files this module uses\r
 //\r
+#include <IndustryStandard/E820.h>\r
 #include <IndustryStandard/Q35MchIch9.h>\r
 #include <PiPei.h>\r
 \r
@@ -103,6 +104,109 @@ Q35TsegMbytesInitialization (
 }\r
 \r
 \r
+/**\r
+  Iterate over the RAM entries in QEMU's fw_cfg E820 RAM map that start outside\r
+  of the 32-bit address range.\r
+\r
+  Find the highest exclusive >=4GB RAM address, or produce memory resource\r
+  descriptor HOBs for RAM entries that start at or above 4GB.\r
+\r
+  @param[out] MaxAddress  If MaxAddress is NULL, then ScanOrAdd64BitE820Ram()\r
+                          produces memory resource descriptor HOBs for RAM\r
+                          entries that start at or above 4GB.\r
+\r
+                          Otherwise, MaxAddress holds the highest exclusive\r
+                          >=4GB RAM address on output. If QEMU's fw_cfg E820\r
+                          RAM map contains no RAM entry that starts outside of\r
+                          the 32-bit address range, then MaxAddress is exactly\r
+                          4GB on output.\r
+\r
+  @retval EFI_SUCCESS         The fw_cfg E820 RAM map was found and processed.\r
+\r
+  @retval EFI_PROTOCOL_ERROR  The RAM map was found, but its size wasn't a\r
+                              whole multiple of sizeof(EFI_E820_ENTRY64). No\r
+                              RAM entry was processed.\r
+\r
+  @return                     Error codes from QemuFwCfgFindFile(). No RAM\r
+                              entry was processed.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+ScanOrAdd64BitE820Ram (\r
+  OUT UINT64 *MaxAddress OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS           Status;\r
+  FIRMWARE_CONFIG_ITEM FwCfgItem;\r
+  UINTN                FwCfgSize;\r
+  EFI_E820_ENTRY64     E820Entry;\r
+  UINTN                Processed;\r
+\r
+  Status = QemuFwCfgFindFile ("etc/e820", &FwCfgItem, &FwCfgSize);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  if (FwCfgSize % sizeof E820Entry != 0) {\r
+    return EFI_PROTOCOL_ERROR;\r
+  }\r
+\r
+  if (MaxAddress != NULL) {\r
+    *MaxAddress = BASE_4GB;\r
+  }\r
+\r
+  QemuFwCfgSelectItem (FwCfgItem);\r
+  for (Processed = 0; Processed < FwCfgSize; Processed += sizeof E820Entry) {\r
+    QemuFwCfgReadBytes (sizeof E820Entry, &E820Entry);\r
+    DEBUG ((\r
+      DEBUG_VERBOSE,\r
+      "%a: Base=0x%Lx Length=0x%Lx Type=%u\n",\r
+      __FUNCTION__,\r
+      E820Entry.BaseAddr,\r
+      E820Entry.Length,\r
+      E820Entry.Type\r
+      ));\r
+    if (E820Entry.Type == EfiAcpiAddressRangeMemory &&\r
+        E820Entry.BaseAddr >= BASE_4GB) {\r
+      if (MaxAddress == NULL) {\r
+        UINT64 Base;\r
+        UINT64 End;\r
+\r
+        //\r
+        // Round up the start address, and round down the end address.\r
+        //\r
+        Base = ALIGN_VALUE (E820Entry.BaseAddr, (UINT64)EFI_PAGE_SIZE);\r
+        End = (E820Entry.BaseAddr + E820Entry.Length) &\r
+              ~(UINT64)EFI_PAGE_MASK;\r
+        if (Base < End) {\r
+          AddMemoryRangeHob (Base, End);\r
+          DEBUG ((\r
+            DEBUG_VERBOSE,\r
+            "%a: AddMemoryRangeHob [0x%Lx, 0x%Lx)\n",\r
+            __FUNCTION__,\r
+            Base,\r
+            End\r
+            ));\r
+        }\r
+      } else {\r
+        UINT64 Candidate;\r
+\r
+        Candidate = E820Entry.BaseAddr + E820Entry.Length;\r
+        if (Candidate > *MaxAddress) {\r
+          *MaxAddress = Candidate;\r
+          DEBUG ((\r
+            DEBUG_VERBOSE,\r
+            "%a: MaxAddress=0x%Lx\n",\r
+            __FUNCTION__,\r
+            *MaxAddress\r
+            ));\r
+        }\r
+      }\r
+    }\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
 UINT32\r
 GetSystemMemorySizeBelow4gb (\r
   VOID\r
@@ -170,7 +274,22 @@ GetFirstNonAddress (
   UINT64               HotPlugMemoryEnd;\r
   RETURN_STATUS        PcdStatus;\r
 \r
-  FirstNonAddress = BASE_4GB + GetSystemMemorySizeAbove4gb ();\r
+  //\r
+  // set FirstNonAddress to suppress incorrect compiler/analyzer warnings\r
+  //\r
+  FirstNonAddress = 0;\r
+\r
+  //\r
+  // If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM\r
+  // address from it. This can express an address >= 4GB+1TB.\r
+  //\r
+  // Otherwise, get the flat size of the memory above 4GB from the CMOS (which\r
+  // can only express a size smaller than 1TB), and add it to 4GB.\r
+  //\r
+  Status = ScanOrAdd64BitE820Ram (&FirstNonAddress);\r
+  if (EFI_ERROR (Status)) {\r
+    FirstNonAddress = BASE_4GB + GetSystemMemorySizeAbove4gb ();\r
+  }\r
 \r
   //\r
   // If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO\r
@@ -239,6 +358,8 @@ GetFirstNonAddress (
   if (!EFI_ERROR (Status) && FwCfgSize == sizeof HotPlugMemoryEnd) {\r
     QemuFwCfgSelectItem (FwCfgItem);\r
     QemuFwCfgReadBytes (FwCfgSize, &HotPlugMemoryEnd);\r
+    DEBUG ((DEBUG_VERBOSE, "%a: HotPlugMemoryEnd=0x%Lx\n", __FUNCTION__,\r
+      HotPlugMemoryEnd));\r
 \r
     ASSERT (HotPlugMemoryEnd >= FirstNonAddress);\r
     FirstNonAddress = HotPlugMemoryEnd;\r
@@ -525,7 +646,13 @@ QemuInitializeRam (
       AddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r
     }\r
 \r
-    if (UpperMemorySize != 0) {\r
+    //\r
+    // If QEMU presents an E820 map, then create memory HOBs for the >=4GB RAM\r
+    // entries. Otherwise, create a single memory HOB with the flat >=4GB\r
+    // memory size read from the CMOS.\r
+    //\r
+    Status = ScanOrAdd64BitE820Ram (NULL);\r
+    if (EFI_ERROR (Status) && UpperMemorySize != 0) {\r
       AddMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r
     }\r
   }\r