(TEST) MemoryInitPeiLib: auto get memory size from fdt
authorBigfootACA <bigfoot@classfun.cn>
星期三, 9 Feb 2022 18:37:04 +0000 (02:37 +0800)
committerBigfootACA <bigfoot@classfun.cn>
星期四, 10 Feb 2022 08:45:00 +0000 (16:45 +0800)
sdm845Pkg/Include/Configuration/DeviceMemoryMap.h
sdm845Pkg/Library/MemoryInitPeiLib/MemoryInitPeiLib.c
sdm845Pkg/Library/MemoryInitPeiLib/PeiMemoryAllocationLib.inf

index dfa683aa27439c8304d91a9b2f90f24584b97140..1614798ff7f8edb502c0c34b596f22a78139d865 100644 (file)
@@ -158,32 +158,6 @@ static ARM_MEMORY_REGION_DESCRIPTOR_EX gDeviceMemoryDescriptorEx[] = {
      ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED, AddMem,\r
      EfiRuntimeServicesData},\r
 \r
-#ifdef MEMORY_4G\r
-    {0xA0000000, 0xDDFA0000, EFI_RESOURCE_SYSTEM_MEMORY,\r
-     SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
-     ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, AddMem, EfiConventionalMemory},\r
-#else\r
-#ifdef MEMORY_8G\r
-    {0xA0000000, 0xE0000000, EFI_RESOURCE_SYSTEM_MEMORY,\r
-     SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
-     ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, AddMem, EfiConventionalMemory},\r
-    {0x180000000, 0xFC8A0000, EFI_RESOURCE_SYSTEM_MEMORY,\r
-     SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
-     ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, AddMem, EfiConventionalMemory},\r
-#else\r
-#ifdef MEMORY_10G\r
-    {0xA0000000, 0x254AC0000, EFI_RESOURCE_SYSTEM_MEMORY,\r
-    SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
-    ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, AddMem, EfiConventionalMemory},\r
-#else\r
-    {0xA0000000, 0x15AE00000, EFI_RESOURCE_SYSTEM_MEMORY,\r
-     SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
-     ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, AddMem, EfiConventionalMemory},\r
-\r
-#endif\r
-#endif\r
-#endif\r
-\r
     /* Other */\r
 \r
     /* AOP_SS_MSG_RAM */\r
index 7f3756b28763cb01fc238baee0cf8889401aaff2..52bd8565be82712012c9fcbffdec11998d8e91c5 100644 (file)
@@ -21,6 +21,7 @@
 #include <Library/HobLib.h>
 #include <Library/MemoryAllocationLib.h>
 #include <Library/PcdLib.h>
+#include <Library/FdtParserLib.h>
 
 // This varies by device
 #include <Configuration/DeviceMemoryMap.h>
@@ -84,12 +85,19 @@ MemoryPeim(IN EFI_PHYSICAL_ADDRESS UefiMemoryBase, IN UINT64 UefiMemorySize)
   ARM_MEMORY_REGION_DESCRIPTOR
   MemoryDescriptor[MAX_ARM_MEMORY_REGION_DESCRIPTOR_COUNT];
   UINTN Index = 0;
+  UINTN Node = 0;
+  UINTN MemoryBase = 0;
+  UINTN MemorySize = 0;
+  UINTN MemoryTotal = 0;
+  fdt *Fdt;
 
-  // Ensure PcdSystemMemorySize has been set
-  ASSERT(PcdGet64(PcdSystemMemorySize) != 0);
+  Fdt = GetFdt();
+  ASSERT(Fdt != NULL);
 
   // Run through each memory descriptor
   while (MemoryDescriptorEx->Length != 0) {
+    if (MemoryDescriptorEx->MemoryType == EfiConventionalMemory)
+      MemoryTotal += MemoryDescriptorEx->Length;
     switch (MemoryDescriptorEx->HobOption) {
     case AddMem:
     case AddDev:
@@ -112,6 +120,33 @@ MemoryPeim(IN EFI_PHYSICAL_ADDRESS UefiMemoryBase, IN UINT64 UefiMemorySize)
     MemoryDescriptorEx++;
   }
 
+  while (fdt_get_memory(Fdt, (int)Node, (uint64_t*)&MemoryBase, (uint64_t*)&MemorySize)) {
+    MemoryTotal += MemorySize;
+    DEBUG((
+      EFI_D_INFO,
+      "FDT Memory %-2d: 0x%016llx - 0x%016llx (0x%016llx)\n",
+      Node, MemoryBase, (MemoryBase + MemorySize), MemorySize
+    ));
+    ASSERT(Index < MAX_ARM_MEMORY_REGION_DESCRIPTOR_COUNT);
+    MemoryDescriptor[Index].PhysicalBase = MemoryBase;
+    MemoryDescriptor[Index].VirtualBase  = MemoryBase;
+    MemoryDescriptor[Index].Length       = MemorySize;
+    MemoryDescriptor[Index].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;
+    BuildResourceDescriptorHob(
+      EFI_RESOURCE_SYSTEM_MEMORY,
+      SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,
+      MemoryBase,
+      MemorySize
+    );
+    BuildMemoryAllocationHob(
+      MemoryBase,
+      MemorySize,
+      EfiConventionalMemory
+    );
+    Index++;
+    Node++;
+  }
+
   // Last one (terminator)
   ASSERT(Index < MAX_ARM_MEMORY_REGION_DESCRIPTOR_COUNT);
   MemoryDescriptor[Index].PhysicalBase = 0;
@@ -119,6 +154,8 @@ MemoryPeim(IN EFI_PHYSICAL_ADDRESS UefiMemoryBase, IN UINT64 UefiMemorySize)
   MemoryDescriptor[Index].Length       = 0;
   MemoryDescriptor[Index].Attributes   = 0;
 
+  DEBUG((EFI_D_INFO, "Memory Total: 0x%016lx (%d GiB)\n", MemoryTotal, MemoryTotal / (1024 * 1024 * 1024)));
+
   // Build Memory Allocation Hob
   DEBUG((EFI_D_INFO, "Configure MMU In \n"));
   InitMmu(MemoryDescriptor);
index 7faf6c060a0e43e93eba4fc71eedc0e3f0891687..550e9094209e87dd9445caae1567805758766f3b 100644 (file)
   ArmPkg/ArmPkg.dec
   ArmPlatformPkg/ArmPlatformPkg.dec
   sdm845Pkg/sdm845Pkg.dec
+  SimpleInit.dec
 
 [LibraryClasses]
   DebugLib
   HobLib
+  SimpleInitLib
   ArmMmuLib
   ArmPlatformLib
 
@@ -46,6 +48,7 @@
 [FixedPcd]
   gArmTokenSpaceGuid.PcdSystemMemoryBase
   gArmTokenSpaceGuid.PcdSystemMemorySize
+  gsdm845PkgTokenSpaceGuid.DeviceTreeStore
 
 [Depex]
   TRUE