(TEST) MemoryInitPeiLib: select memory based on fdt
authorBigfootACA <bigfoot@classfun.cn>
星期四, 10 Feb 2022 13:11:11 +0000 (21:11 +0800)
committerBigfootACA <bigfoot@classfun.cn>
星期四, 10 Feb 2022 13:11:11 +0000 (21:11 +0800)
sdm845Pkg/Include/Configuration/DeviceMemoryMap.h
sdm845Pkg/Library/MemoryInitPeiLib/MemoryInitPeiLib.c

index 1614798ff7f8edb502c0c34b596f22a78139d865..fbc1ca42292271721463bf4bae7e6d4e144c2237 100644 (file)
@@ -14,7 +14,7 @@
       EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |                            \\r
       EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE\r
 \r
-typedef enum { NoHob, AddMem, AddDev, MaxMem } DeviceMemoryAddHob;\r
+typedef enum { NoHob, AddMem, AddDev, Mem4G, Mem6G, Mem8G, Mem10G, MaxMem } DeviceMemoryAddHob;\r
 \r
 typedef struct {\r
   EFI_PHYSICAL_ADDRESS         Address;\r
@@ -158,6 +158,29 @@ static ARM_MEMORY_REGION_DESCRIPTOR_EX gDeviceMemoryDescriptorEx[] = {
      ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED, AddMem,\r
      EfiRuntimeServicesData},\r
 \r
+    /* 4GiB Memory */\r
+    {0xA0000000, 0xDDFA0000, EFI_RESOURCE_SYSTEM_MEMORY,\r
+     SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
+     ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, Mem4G, EfiConventionalMemory},\r
+\r
+    /* 6GiB Memory */\r
+    {0xA0000000, 0x15AE00000, EFI_RESOURCE_SYSTEM_MEMORY,\r
+     SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
+     ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, Mem6G, EfiConventionalMemory},\r
+\r
+    /* 8GiB Memory */\r
+    {0xA0000000, 0xE0000000, EFI_RESOURCE_SYSTEM_MEMORY,\r
+     SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
+     ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, Mem8G, EfiConventionalMemory},\r
+    {0x180000000, 0xFC8A0000, EFI_RESOURCE_SYSTEM_MEMORY,\r
+     SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
+     ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, Mem8G, EfiConventionalMemory},\r
+\r
+    /* 10GiB Memory */\r
+    {0xA0000000, 0x254AC0000, EFI_RESOURCE_SYSTEM_MEMORY,\r
+    SYSTEM_MEMORY_RESOURCE_ATTR_CAPABILITIES,\r
+    ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK, Mem10G, EfiConventionalMemory},\r
+\r
     /* Other */\r
 \r
     /* AOP_SS_MSG_RAM */\r
index 52bd8565be82712012c9fcbffdec11998d8e91c5..db27491033b65bae48f325cd9a6293f9dd5bf394 100644 (file)
 // This varies by device
 #include <Configuration/DeviceMemoryMap.h>
 
+#define SIZE_KB ((UINTN)(1024))
+#define SIZE_MB ((UINTN)(SIZE_KB * 1024))
+#define SIZE_GB ((UINTN)(SIZE_MB * 1024))
+#define SIZE_MB_BIG(_Size,_Value) ((_Size) > ((_Value) * SIZE_MB))
+#define SIZE_MB_SMALL(_Size,_Value) ((_Size) < ((_Value) * SIZE_MB))
+#define SIZE_MB_IN(_Min,_Max,_Size) \
+  if (SIZE_MB_BIG((MemoryTotal), (_Min)) && SIZE_MB_SMALL((MemoryTotal), (_Max)))\
+    Mem = Mem##_Size##G, MemGB = _Size
+
 extern UINT64 mSystemMemoryEnd;
 
 VOID BuildMemoryTypeInformationHob(VOID);
@@ -89,16 +98,46 @@ MemoryPeim(IN EFI_PHYSICAL_ADDRESS UefiMemoryBase, IN UINT64 UefiMemorySize)
   UINTN MemoryBase = 0;
   UINTN MemorySize = 0;
   UINTN MemoryTotal = 0;
+  DeviceMemoryAddHob Mem = Mem4G;
+  UINT8 MemGB = 4;
   fdt *Fdt;
 
   Fdt = GetFdt();
   ASSERT(Fdt != NULL);
 
+  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
+    ));
+    Node++;
+  }
+
+  // Memory   Min    Max   Config
+  SIZE_MB_IN (3072,  4608, 4);
+  SIZE_MB_IN (5120,  6656, 6);
+  SIZE_MB_IN (7168,  8704, 8);
+  SIZE_MB_IN (9216, 10752, 10);
+
+  DEBUG((EFI_D_INFO, "FDT Memory Total: 0x%016lx (%d GiB)\n", MemoryTotal, MemoryTotal / SIZE_GB));
+  DEBUG((EFI_D_INFO, "Select Config: %d GiB\n", MemGB));
+
   // Run through each memory descriptor
   while (MemoryDescriptorEx->Length != 0) {
     if (MemoryDescriptorEx->MemoryType == EfiConventionalMemory)
       MemoryTotal += MemoryDescriptorEx->Length;
     switch (MemoryDescriptorEx->HobOption) {
+    case Mem4G:
+    case Mem6G:
+    case Mem8G:
+    case Mem10G:
+      if (MemoryDescriptorEx->HobOption != Mem) {
+        MemoryDescriptorEx++;
+        continue;
+      }
+      // fallthrough
     case AddMem:
     case AddDev:
       AddHob(MemoryDescriptorEx);
@@ -120,33 +159,6 @@ 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;
@@ -154,8 +166,6 @@ 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);