]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Library/PlatformInitLib/MemDetect.c
OvmfPkg/PlatformInitLib: Add PlatformAddHobCB
[mirror_edk2.git] / OvmfPkg / Library / PlatformInitLib / MemDetect.c
index 57feeb6dab25d75f6a877fb3d3fca159bfcc39c8..c24105c329334f3563d98a95c8a5bfd89c3ad78d 100644 (file)
@@ -112,143 +112,6 @@ PlatformQemuUc32BaseInitialization (
   }\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 PlatformScanOrAdd64BitE820Ram()\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
-PlatformScanOrAdd64BitE820Ram (\r
-  IN BOOLEAN  AddHighHob,\r
-  OUT UINT64  *LowMemory OPTIONAL,\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
-\r
-  if (FwCfgSize % sizeof E820Entry != 0) {\r
-    return EFI_PROTOCOL_ERROR;\r
-  }\r
-\r
-  if (LowMemory != NULL) {\r
-    *LowMemory = 0;\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
-      if (AddHighHob && (E820Entry.BaseAddr >= BASE_4GB)) {\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
-          PlatformAddMemoryRangeHob (Base, End);\r
-          DEBUG ((\r
-            DEBUG_VERBOSE,\r
-            "%a: PlatformAddMemoryRangeHob [0x%Lx, 0x%Lx)\n",\r
-            __FUNCTION__,\r
-            Base,\r
-            End\r
-            ));\r
-        }\r
-      }\r
-\r
-      if (MaxAddress || LowMemory) {\r
-        UINT64  Candidate;\r
-\r
-        Candidate = E820Entry.BaseAddr + E820Entry.Length;\r
-        if (MaxAddress && (Candidate > *MaxAddress)) {\r
-          *MaxAddress = Candidate;\r
-          DEBUG ((\r
-            DEBUG_VERBOSE,\r
-            "%a: MaxAddress=0x%Lx\n",\r
-            __FUNCTION__,\r
-            *MaxAddress\r
-            ));\r
-        }\r
-\r
-        if (LowMemory && (Candidate > *LowMemory) && (Candidate < BASE_4GB)) {\r
-          *LowMemory = Candidate;\r
-          DEBUG ((\r
-            DEBUG_VERBOSE,\r
-            "%a: LowMemory=0x%Lx\n",\r
-            __FUNCTION__,\r
-            *LowMemory\r
-            ));\r
-        }\r
-      }\r
-    } else if (E820Entry.Type == EfiAcpiAddressRangeReserved) {\r
-      if (AddHighHob) {\r
-        DEBUG ((\r
-          DEBUG_INFO,\r
-          "%a: Reserved: Base=0x%Lx Length=0x%Lx\n",\r
-          __FUNCTION__,\r
-          E820Entry.BaseAddr,\r
-          E820Entry.Length\r
-          ));\r
-        BuildResourceDescriptorHob (\r
-          EFI_RESOURCE_MEMORY_RESERVED,\r
-          0,\r
-          E820Entry.BaseAddr,\r
-          E820Entry.Length\r
-          );\r
-      }\r
-    }\r
-  }\r
-\r
-  return EFI_SUCCESS;\r
-}\r
-\r
 typedef VOID (*E820_SCAN_CALLBACK) (\r
   EFI_E820_ENTRY64       *E820Entry,\r
   EFI_HOB_PLATFORM_INFO  *PlatformInfoHob\r
@@ -306,6 +169,53 @@ PlatformGetLowMemoryCB (
   }\r
 }\r
 \r
+/**\r
+  Create HOBs for reservations and RAM (except low memory).\r
+**/\r
+STATIC\r
+VOID\r
+PlatformAddHobCB (\r
+  IN     EFI_E820_ENTRY64       *E820Entry,\r
+  IN OUT EFI_HOB_PLATFORM_INFO  *PlatformInfoHob\r
+  )\r
+{\r
+  UINT64  Base, End;\r
+\r
+  Base = E820Entry->BaseAddr;\r
+  End  = E820Entry->BaseAddr + E820Entry->Length;\r
+\r
+  switch (E820Entry->Type) {\r
+    case EfiAcpiAddressRangeMemory:\r
+      if (Base >= BASE_4GB) {\r
+        //\r
+        // Round up the start address, and round down the end address.\r
+        //\r
+        Base = ALIGN_VALUE (Base, (UINT64)EFI_PAGE_SIZE);\r
+        End  = End & ~(UINT64)EFI_PAGE_MASK;\r
+        if (Base < End) {\r
+          DEBUG ((DEBUG_INFO, "%a: HighMemory [0x%Lx, 0x%Lx)\n", __FUNCTION__, Base, End));\r
+          PlatformAddMemoryRangeHob (Base, End);\r
+        }\r
+      }\r
+\r
+      break;\r
+    case EfiAcpiAddressRangeReserved:\r
+      BuildResourceDescriptorHob (EFI_RESOURCE_MEMORY_RESERVED, 0, Base, End);\r
+      DEBUG ((DEBUG_INFO, "%a: Reserved [0x%Lx, 0x%Lx)\n", __FUNCTION__, Base, End));\r
+      break;\r
+    default:\r
+      DEBUG ((\r
+        DEBUG_WARN,\r
+        "%a: Type %u [0x%Lx, 0x%Lx) (NOT HANDLED)\n",\r
+        __FUNCTION__,\r
+        E820Entry->Type,\r
+        Base,\r
+        End\r
+        ));\r
+      break;\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
@@ -1051,7 +961,7 @@ PlatformQemuInitializeRam (
     // entries. Otherwise, create a single memory HOB with the flat >=4GB\r
     // memory size read from the CMOS.\r
     //\r
-    Status = PlatformScanOrAdd64BitE820Ram (TRUE, NULL, NULL);\r
+    Status = PlatformScanE820 (PlatformAddHobCB, PlatformInfoHob);\r
     if (EFI_ERROR (Status)) {\r
       UpperMemorySize = PlatformGetSystemMemorySizeAbove4gb ();\r
       if (UpperMemorySize != 0) {\r