From 44b69c8d7c8ed665b4f8d8a9953ea23a731d221f Mon Sep 17 00:00:00 2001
From: Your Name <you@example.com>
Date: Fri, 10 Mar 2023 18:46:49 +0000
Subject: [PATCH] Revert "ArmVirtPkg/QemuVirtMemInfoLib: use HOB not PCD to
 record the memory size"

This reverts commit 7136d5491e225c57f1d73e4a1b7ac27ed656ff72.

Upstream-Status: Inappropriate [other]
Signed-off-by: Jon Mason <jon.mason@arm.com>

---
 ArmVirtPkg/ArmVirtPkg.dec                     |  1 -
 ArmVirtPkg/ArmVirtQemu.dsc                    |  6 ++--
 .../ArmVirtMemoryInitPeiLib.c                 | 14 ++------
 .../ArmVirtMemoryInitPeiLib.inf               |  1 -
 .../QemuVirtMemInfoLib/QemuVirtMemInfoLib.c   | 35 ++-----------------
 .../QemuVirtMemInfoLib/QemuVirtMemInfoLib.inf |  5 +--
 .../QemuVirtMemInfoPeiLib.inf                 |  8 ++---
 .../QemuVirtMemInfoPeiLibConstructor.c        | 30 +++++++---------
 8 files changed, 25 insertions(+), 75 deletions(-)

diff --git a/ArmVirtPkg/ArmVirtPkg.dec b/ArmVirtPkg/ArmVirtPkg.dec
index 4645c91a83..d2d325d71e 100644
--- a/ArmVirtPkg/ArmVirtPkg.dec
+++ b/ArmVirtPkg/ArmVirtPkg.dec
@@ -32,7 +32,6 @@
   gArmVirtTokenSpaceGuid = { 0x0B6F5CA7, 0x4F53, 0x445A, { 0xB7, 0x6E, 0x2E, 0x36, 0x5B, 0x80, 0x63, 0x66 } }
   gEarlyPL011BaseAddressGuid       = { 0xB199DEA9, 0xFD5C, 0x4A84, { 0x80, 0x82, 0x2F, 0x41, 0x70, 0x78, 0x03, 0x05 } }
   gEarly16550UartBaseAddressGuid   = { 0xea67ca3e, 0x1f54, 0x436b, { 0x97, 0x88, 0xd4, 0xeb, 0x29, 0xc3, 0x42, 0x67 } }
-  gArmVirtSystemMemorySizeGuid     = { 0x504eccb9, 0x1bf0, 0x4420, { 0x86, 0x5d, 0xdc, 0x66, 0x06, 0xd4, 0x13, 0xbf } }
 
 [PcdsFeatureFlag]
   #
diff --git a/ArmVirtPkg/ArmVirtQemu.dsc b/ArmVirtPkg/ArmVirtQemu.dsc
index 72a0cacab4..2e786dad12 100644
--- a/ArmVirtPkg/ArmVirtQemu.dsc
+++ b/ArmVirtPkg/ArmVirtQemu.dsc
@@ -226,9 +226,6 @@
   # Shadowing PEI modules is absolutely pointless when the NOR flash is emulated
   gEfiMdeModulePkgTokenSpaceGuid.PcdShadowPeimOnBoot|FALSE
 
-  # System Memory Size -- 128 MB initially, actual size will be fetched from DT
-  gArmTokenSpaceGuid.PcdSystemMemorySize|0x8000000
-
 [PcdsFixedAtBuild.AARCH64]
   # Clearing BIT0 in this PCD prevents installing a 32-bit SMBIOS entry point,
   # if the entry point version is >= 3.0. AARCH64 OSes cannot assume the
@@ -245,6 +242,9 @@
   #  enumeration to complete before installing ACPI tables.
   gEfiMdeModulePkgTokenSpaceGuid.PcdPciDisableBusEnumeration|TRUE
 
+  # System Memory Size -- 1 MB initially, actual size will be fetched from DT
+  gArmTokenSpaceGuid.PcdSystemMemorySize|0x00100000
+
   gArmTokenSpaceGuid.PcdArmArchTimerSecIntrNum|0x0
   gArmTokenSpaceGuid.PcdArmArchTimerIntrNum|0x0
   gArmTokenSpaceGuid.PcdArmArchTimerVirtIntrNum|0x0
diff --git a/ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.c b/ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.c
index 72e5c65af7..98d90ad420 100644
--- a/ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.c
+++ b/ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.c
@@ -52,19 +52,10 @@ MemoryPeim (
 {
   EFI_RESOURCE_ATTRIBUTE_TYPE  ResourceAttributes;
   UINT64                       SystemMemoryTop;
-  UINT64                       SystemMemorySize;
-  VOID                         *Hob;
 
   // Ensure PcdSystemMemorySize has been set
   ASSERT (PcdGet64 (PcdSystemMemorySize) != 0);
 
-  SystemMemorySize = PcdGet64 (PcdSystemMemorySize);
-
-  Hob = GetFirstGuidHob (&gArmVirtSystemMemorySizeGuid);
-  if (Hob != NULL) {
-    SystemMemorySize = *(UINT64 *)GET_GUID_HOB_DATA (Hob);
-  }
-
   //
   // Now, the permanent memory has been installed, we can call AllocatePages()
   //
@@ -75,7 +66,8 @@ MemoryPeim (
                         EFI_RESOURCE_ATTRIBUTE_TESTED
                         );
 
-  SystemMemoryTop = PcdGet64 (PcdSystemMemoryBase) + SystemMemorySize;
+  SystemMemoryTop = PcdGet64 (PcdSystemMemoryBase) +
+                    PcdGet64 (PcdSystemMemorySize);
 
   if (SystemMemoryTop - 1 > MAX_ALLOC_ADDRESS) {
     BuildResourceDescriptorHob (
@@ -95,7 +87,7 @@ MemoryPeim (
       EFI_RESOURCE_SYSTEM_MEMORY,
       ResourceAttributes,
       PcdGet64 (PcdSystemMemoryBase),
-      SystemMemorySize
+      PcdGet64 (PcdSystemMemorySize)
       );
   }
 
diff --git a/ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.inf b/ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.inf
index 48d9c66b22..21327f79f4 100644
--- a/ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.inf
+++ b/ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.inf
@@ -34,7 +34,6 @@
   CacheMaintenanceLib
 
 [Guids]
-  gArmVirtSystemMemorySizeGuid
   gEfiMemoryTypeInformationGuid
 
 [FeaturePcd]
diff --git a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.c b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.c
index 9cf43f06c0..cf569bed99 100644
--- a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.c
+++ b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.c
@@ -6,12 +6,10 @@
 
 **/
 
-#include <Uefi.h>
-#include <Pi/PiMultiPhase.h>
+#include <Base.h>
 #include <Library/ArmLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
-#include <Library/HobLib.h>
 #include <Library/MemoryAllocationLib.h>
 
 // Number of Virtual Memory Map Descriptors
@@ -26,28 +24,6 @@
 #define MACH_VIRT_PERIPH_BASE  0x08000000
 #define MACH_VIRT_PERIPH_SIZE  SIZE_128MB
 
-/**
-  Default library constructur that obtains the memory size from a PCD.
-
-  @return  Always returns RETURN_SUCCESS
-
-**/
-RETURN_STATUS
-EFIAPI
-QemuVirtMemInfoLibConstructor (
-  VOID
-  )
-{
-  UINT64  Size;
-  VOID    *Hob;
-
-  Size = PcdGet64 (PcdSystemMemorySize);
-  Hob  = BuildGuidDataHob (&gArmVirtSystemMemorySizeGuid, &Size, sizeof Size);
-  ASSERT (Hob != NULL);
-
-  return RETURN_SUCCESS;
-}
-
 /**
   Return the Virtual Memory Map of your platform
 
@@ -67,16 +43,9 @@ ArmVirtGetMemoryMap (
   )
 {
   ARM_MEMORY_REGION_DESCRIPTOR  *VirtualMemoryTable;
-  VOID                          *MemorySizeHob;
 
   ASSERT (VirtualMemoryMap != NULL);
 
-  MemorySizeHob = GetFirstGuidHob (&gArmVirtSystemMemorySizeGuid);
-  ASSERT (MemorySizeHob != NULL);
-  if (MemorySizeHob == NULL) {
-    return;
-  }
-
   VirtualMemoryTable = AllocatePool (
                          sizeof (ARM_MEMORY_REGION_DESCRIPTOR) *
                          MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS
@@ -90,7 +59,7 @@ ArmVirtGetMemoryMap (
   // System DRAM
   VirtualMemoryTable[0].PhysicalBase = PcdGet64 (PcdSystemMemoryBase);
   VirtualMemoryTable[0].VirtualBase  = VirtualMemoryTable[0].PhysicalBase;
-  VirtualMemoryTable[0].Length       = *(UINT64 *)GET_GUID_HOB_DATA (MemorySizeHob);
+  VirtualMemoryTable[0].Length       = PcdGet64 (PcdSystemMemorySize);
   VirtualMemoryTable[0].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;
 
   DEBUG ((
diff --git a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.inf b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.inf
index 6acad8bbd7..7150de6c10 100644
--- a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.inf
+++ b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.inf
@@ -14,7 +14,6 @@
   MODULE_TYPE                    = BASE
   VERSION_STRING                 = 1.0
   LIBRARY_CLASS                  = ArmVirtMemInfoLib
-  CONSTRUCTOR                    = QemuVirtMemInfoLibConstructor
 
 [Sources]
   QemuVirtMemInfoLib.c
@@ -31,9 +30,7 @@
   BaseMemoryLib
   DebugLib
   MemoryAllocationLib
-
-[Guids]
-  gArmVirtSystemMemorySizeGuid
+  PcdLib
 
 [Pcd]
   gArmTokenSpaceGuid.PcdFvBaseAddress
diff --git a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLib.inf b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLib.inf
index f045e39a41..7ecf6dfbb7 100644
--- a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLib.inf
+++ b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLib.inf
@@ -32,16 +32,16 @@
   BaseMemoryLib
   DebugLib
   FdtLib
+  PcdLib
   MemoryAllocationLib
 
-[Guids]
-  gArmVirtSystemMemorySizeGuid
-
-[FixedPcd]
+[Pcd]
   gArmTokenSpaceGuid.PcdFdBaseAddress
   gArmTokenSpaceGuid.PcdFvBaseAddress
   gArmTokenSpaceGuid.PcdSystemMemoryBase
   gArmTokenSpaceGuid.PcdSystemMemorySize
+
+[FixedPcd]
   gArmTokenSpaceGuid.PcdFdSize
   gArmTokenSpaceGuid.PcdFvSize
   gArmVirtTokenSpaceGuid.PcdDeviceTreeInitialBaseAddress
diff --git a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLibConstructor.c b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLibConstructor.c
index c47ab82966..33d3597d57 100644
--- a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLibConstructor.c
+++ b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLibConstructor.c
@@ -6,10 +6,9 @@
 
 **/
 
-#include <Uefi.h>
-#include <Pi/PiMultiPhase.h>
+#include <Base.h>
 #include <Library/DebugLib.h>
-#include <Library/HobLib.h>
+#include <Library/PcdLib.h>
 #include <libfdt.h>
 
 RETURN_STATUS
@@ -18,14 +17,14 @@ QemuVirtMemInfoPeiLibConstructor (
   VOID
   )
 {
-  VOID          *DeviceTreeBase;
-  INT32         Node, Prev;
-  UINT64        NewBase, CurBase;
-  UINT64        NewSize, CurSize;
-  CONST CHAR8   *Type;
-  INT32         Len;
-  CONST UINT64  *RegProp;
-  VOID          *Hob;
+  VOID           *DeviceTreeBase;
+  INT32          Node, Prev;
+  UINT64         NewBase, CurBase;
+  UINT64         NewSize, CurSize;
+  CONST CHAR8    *Type;
+  INT32          Len;
+  CONST UINT64   *RegProp;
+  RETURN_STATUS  PcdStatus;
 
   NewBase = 0;
   NewSize = 0;
@@ -87,13 +86,8 @@ QemuVirtMemInfoPeiLibConstructor (
   // Make sure the start of DRAM matches our expectation
   //
   ASSERT (FixedPcdGet64 (PcdSystemMemoryBase) == NewBase);
-
-  Hob = BuildGuidDataHob (
-          &gArmVirtSystemMemorySizeGuid,
-          &NewSize,
-          sizeof NewSize
-          );
-  ASSERT (Hob != NULL);
+  PcdStatus = PcdSet64S (PcdSystemMemorySize, NewSize);
+  ASSERT_RETURN_ERROR (PcdStatus);
 
   //
   // We need to make sure that the machine we are running on has at least
