]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmVirtPkg: create QemuVirtMemInfoLib version for ArmVirtQemu
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Wed, 22 Nov 2017 09:41:44 +0000 (09:41 +0000)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Thu, 23 Nov 2017 16:53:26 +0000 (16:53 +0000)
The QemuVirtMemInfoLib ArmVirtMemInfoLib implementation created for
ArmVirtQemuKernel does exactly what we need for ArmVirtQemu, the only
difference being that the latter is PrePeiCore based, and so it uses
a different method to ensure that PcdSystemMemorySize is set when
ArmVirtGetMemoryMap() is called.

On ArmVirtQemu, we currently abuse the implied ordering guarantees
provided by ArmPlatformLib, by implementing this as follows:

  ArmPlatformPkg/MemoryInitPei/MemoryInitPeim.inf [ArmVirtPkg/ArmVirtQemu.dsc]
    InitializeMemory()                            [ArmPlatformPkg/MemoryInitPei/MemoryInitPeim.c]
      ArmPlatformInitializeSystemMemory()         [ArmVirtPkg/Library/ArmVirtPlatformLib/Virt.c]
        //
        // set PcdSystemMemorySize from the DT
        //
      MemoryPeim()                                [ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.c]
        InitMmu()                                 [ArmVirtPkg/Library/ArmVirtMemoryInitPeiLib/ArmVirtMemoryInitPeiLib.c]
          ArmPlatformGetVirtualMemoryMap()        [ArmVirtPkg/Library/ArmVirtPlatformLib/VirtMem.c]
            //
            // consume PcdSystemMemorySize
            //

Given that we are trying to get rid of ArmPlatformLib, or at least remove
some of these API functions that are never used for their original purpose
by any platforms, we need to move the PCD assignment elsewhere.

So create a PEIM-only version of QemuVirtMemInfoLib especially for
ArmVirtQemu, and add the PCD assignment code to its constructor.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
ArmVirtPkg/ArmVirtQemu.dsc
ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLib.inf [new file with mode: 0644]
ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLibConstructor.c [new file with mode: 0644]

index d14a0dd0d1d9ae729a5c135935b556e021f73060..519c2ae2e93934c9cb577d7c0462caccc820fa9f 100644 (file)
@@ -67,6 +67,9 @@
   HttpLib|MdeModulePkg/Library/DxeHttpLib/DxeHttpLib.inf\r
 !endif\r
 \r
+[LibraryClasses.common.PEIM]\r
+  ArmVirtMemInfoLib|ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLib.inf\r
+\r
 [LibraryClasses.common.UEFI_DRIVER]\r
   UefiScsiLib|MdePkg/Library/UefiScsiLib/UefiScsiLib.inf\r
 \r
diff --git a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLib.inf b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLib.inf
new file mode 100644 (file)
index 0000000..e4032d3
--- /dev/null
@@ -0,0 +1,58 @@
+#/* @file\r
+#\r
+#  Copyright (c) 2011-2015, ARM Limited. All rights reserved.\r
+#  Copyright (c) 2014-2017, Linaro Limited. All rights reserved.\r
+#\r
+#  This program and the accompanying materials are licensed and made available\r
+#  under the terms and conditions of the BSD License which accompanies this\r
+#  distribution.  The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#*/\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x0001001A\r
+  BASE_NAME                      = QemuVirtMemInfoPeiLib\r
+  FILE_GUID                      = 0c4d10cf-d949-49b4-bd13-47a4ae22efce\r
+  MODULE_TYPE                    = BASE\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = ArmVirtMemInfoLib|PEIM\r
+  CONSTRUCTOR                    = QemuVirtMemInfoPeiLibConstructor\r
+\r
+[Sources]\r
+  QemuVirtMemInfoLib.c\r
+  QemuVirtMemInfoPeiLibConstructor.c\r
+\r
+[Sources.ARM]\r
+  Arm/PhysAddrTop.S\r
+\r
+[Sources.AARCH64]\r
+  AArch64/PhysAddrTop.S\r
+\r
+[Packages]\r
+  ArmPkg/ArmPkg.dec\r
+  ArmVirtPkg/ArmVirtPkg.dec\r
+  EmbeddedPkg/EmbeddedPkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  MdePkg/MdePkg.dec\r
+\r
+[LibraryClasses]\r
+  ArmLib\r
+  BaseMemoryLib\r
+  DebugLib\r
+  FdtLib\r
+  PcdLib\r
+  MemoryAllocationLib\r
+\r
+[Pcd]\r
+  gArmTokenSpaceGuid.PcdFdBaseAddress\r
+  gArmTokenSpaceGuid.PcdSystemMemoryBase\r
+  gArmTokenSpaceGuid.PcdSystemMemorySize\r
+\r
+[FixedPcd]\r
+  gArmTokenSpaceGuid.PcdFdSize\r
+  gArmVirtTokenSpaceGuid.PcdDeviceTreeInitialBaseAddress\r
+  gEmbeddedTokenSpaceGuid.PcdPrePiCpuMemorySize\r
diff --git a/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLibConstructor.c b/ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLibConstructor.c
new file mode 100644 (file)
index 0000000..ef8ac6e
--- /dev/null
@@ -0,0 +1,106 @@
+/** @file\r
+\r
+  Copyright (c) 2014-2017, Linaro Limited. All rights reserved.\r
+\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Base.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <libfdt.h>\r
+\r
+RETURN_STATUS\r
+EFIAPI\r
+QemuVirtMemInfoPeiLibConstructor (\r
+  VOID\r
+  )\r
+{\r
+  VOID          *DeviceTreeBase;\r
+  INT32         Node, Prev;\r
+  UINT64        NewBase, CurBase;\r
+  UINT64        NewSize, CurSize;\r
+  CONST CHAR8   *Type;\r
+  INT32         Len;\r
+  CONST UINT64  *RegProp;\r
+  RETURN_STATUS PcdStatus;\r
+\r
+  NewBase = 0;\r
+  NewSize = 0;\r
+\r
+  DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress);\r
+  ASSERT (DeviceTreeBase != NULL);\r
+\r
+  //\r
+  // Make sure we have a valid device tree blob\r
+  //\r
+  ASSERT (fdt_check_header (DeviceTreeBase) == 0);\r
+\r
+  //\r
+  // Look for the lowest memory node\r
+  //\r
+  for (Prev = 0;; Prev = Node) {\r
+    Node = fdt_next_node (DeviceTreeBase, Prev, NULL);\r
+    if (Node < 0) {\r
+      break;\r
+    }\r
+\r
+    //\r
+    // Check for memory node\r
+    //\r
+    Type = fdt_getprop (DeviceTreeBase, Node, "device_type", &Len);\r
+    if (Type && AsciiStrnCmp (Type, "memory", Len) == 0) {\r
+      //\r
+      // Get the 'reg' property of this node. For now, we will assume\r
+      // two 8 byte quantities for base and size, respectively.\r
+      //\r
+      RegProp = fdt_getprop (DeviceTreeBase, Node, "reg", &Len);\r
+      if (RegProp != 0 && Len == (2 * sizeof (UINT64))) {\r
+\r
+        CurBase = fdt64_to_cpu (ReadUnaligned64 (RegProp));\r
+        CurSize = fdt64_to_cpu (ReadUnaligned64 (RegProp + 1));\r
+\r
+        DEBUG ((DEBUG_INFO, "%a: System RAM @ 0x%lx - 0x%lx\n",\r
+          __FUNCTION__, CurBase, CurBase + CurSize - 1));\r
+\r
+        if (NewBase > CurBase || NewBase == 0) {\r
+          NewBase = CurBase;\r
+          NewSize = CurSize;\r
+        }\r
+      } else {\r
+        DEBUG ((DEBUG_ERROR, "%a: Failed to parse FDT memory node\n",\r
+          __FUNCTION__));\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // Make sure the start of DRAM matches our expectation\r
+  //\r
+  ASSERT (FixedPcdGet64 (PcdSystemMemoryBase) == NewBase);\r
+  PcdStatus = PcdSet64S (PcdSystemMemorySize, NewSize);\r
+  ASSERT_RETURN_ERROR (PcdStatus);\r
+\r
+  //\r
+  // We need to make sure that the machine we are running on has at least\r
+  // 128 MB of memory configured, and is currently executing this binary from\r
+  // NOR flash. This prevents a device tree image in DRAM from getting\r
+  // clobbered when our caller installs permanent PEI RAM, before we have a\r
+  // chance of marking its location as reserved or copy it to a freshly\r
+  // allocated block in the permanent PEI RAM in the platform PEIM.\r
+  //\r
+  ASSERT (NewSize >= SIZE_128MB);\r
+  ASSERT (\r
+    (((UINT64)PcdGet64 (PcdFdBaseAddress) +\r
+      (UINT64)PcdGet32 (PcdFdSize)) <= NewBase) ||\r
+    ((UINT64)PcdGet64 (PcdFdBaseAddress) >= (NewBase + NewSize)));\r
+\r
+  return RETURN_SUCCESS;\r
+}\r