public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Sami Mujawar" <sami.mujawar@arm.com>
To: <devel@edk2.groups.io>
Cc: Sami Mujawar <sami.mujawar@arm.com>, <ard.biesheuvel@arm.com>,
	<leif@nuviainc.com>, <lersek@redhat.com>,
	<Alexandru.Elisei@arm.com>, <Andre.Przywara@arm.com>,
	<Matteo.Carlini@arm.com>, <Laura.Moretta@arm.com>, <nd@arm.com>
Subject: [PATCH v3 05/15] ArmVirtPkg: kvmtool platform memory map
Date: Wed, 24 Jun 2020 14:34:48 +0100	[thread overview]
Message-ID: <20200624133458.61920-6-sami.mujawar@arm.com> (raw)
In-Reply-To: <20200624133458.61920-1-sami.mujawar@arm.com>

Kvmtool is a virtual machine manager that enables
hosting KVM guests. Kvmtool allows to vary the
hardware configuration of the virtual platform
it provides to the guest partition. It provides
the current hardware configuration to the firmware
by handing off a device tree containing the hardware
information.

This library parses the kvmtool provided device
tree and populates the system memory map for the
kvmtool virtual platform.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
Reviewed-by: Ard Biesheuvel <Ard.Biesheuvel@arm.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
---

Notes:
    v3:
      - Fix coding style, use "Txt" in LOG_VM_MAP(txt).             [Laszlo]
      - Fixed coding style and also formatted log output.           [Sami]
        Ref: https://edk2.groups.io/g/devel/topic/74200913#59548
    v2:
      - Library to populate Kvmtool system memory map.              [Sami]

 ArmVirtPkg/Library/KvmtoolVirtMemInfoLib/KvmtoolVirtMemInfoLib.c   | 128 ++++++++++++++++++++
 ArmVirtPkg/Library/KvmtoolVirtMemInfoLib/KvmtoolVirtMemInfoLib.inf |  42 +++++++
 2 files changed, 170 insertions(+)

diff --git a/ArmVirtPkg/Library/KvmtoolVirtMemInfoLib/KvmtoolVirtMemInfoLib.c b/ArmVirtPkg/Library/KvmtoolVirtMemInfoLib/KvmtoolVirtMemInfoLib.c
new file mode 100644
index 0000000000000000000000000000000000000000..981c2b4aa29da2a797206c5770adf30a761ff55c
--- /dev/null
+++ b/ArmVirtPkg/Library/KvmtoolVirtMemInfoLib/KvmtoolVirtMemInfoLib.c
@@ -0,0 +1,128 @@
+/** @file
+
+  Copyright (c) 2018 - 2020, ARM Limited. All rights reserved.
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Base.h>
+#include <Library/ArmLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+
+// Number of Virtual Memory Map Descriptors
+#define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS          5
+
+#define MAX_REGION_NAME_LEN   32
+
+#define LOG_MEM_MAP_HEADER()                \
+  DEBUG ((                                  \
+    DEBUG_ERROR,                            \
+    "%-*a : %-18a : %-18a - %-18a : %-a\n", \
+    MAX_REGION_NAME_LEN,                    \
+    "Region",                               \
+    "Physical Address",                     \
+    "Virtual Address",                      \
+    "Size",                                 \
+    "Attribute"                             \
+    ));
+
+#define LOG_MEM_MAP(Txt)                              \
+  DEBUG ((                                            \
+    DEBUG_ERROR,                                      \
+    "%-*a : 0x%016x : 0x%016x - 0x%016x : 0x%02x\n",  \
+    MAX_REGION_NAME_LEN,                              \
+    Txt,                                              \
+    VirtualMemoryTable[Idx].PhysicalBase,             \
+    VirtualMemoryTable[Idx].VirtualBase,              \
+    VirtualMemoryTable[Idx].Length,                   \
+    VirtualMemoryTable[Idx].Attributes                \
+    ));
+
+/**
+  Return the Virtual Memory Map of your platform
+
+  This Virtual Memory Map is used by MemoryInitPei Module to initialize the MMU
+  on your platform.
+
+  @param[out]   VirtualMemoryMap    Array of ARM_MEMORY_REGION_DESCRIPTOR
+                                    describing a Physical-to-Virtual Memory
+                                    mapping. This array must be ended by a
+                                    zero-filled entry. The allocated memory
+                                    will not be freed.
+
+**/
+VOID
+ArmVirtGetMemoryMap (
+  OUT ARM_MEMORY_REGION_DESCRIPTOR   **VirtualMemoryMap
+  )
+{
+  ARM_MEMORY_REGION_DESCRIPTOR  *VirtualMemoryTable;
+  UINTN                          Idx = 0;
+  EFI_PHYSICAL_ADDRESS           TopOfAddressSpace;
+
+  ASSERT (VirtualMemoryMap != NULL);
+
+  TopOfAddressSpace = LShiftU64 (1ULL, ArmGetPhysicalAddressBits ());
+
+  VirtualMemoryTable = (ARM_MEMORY_REGION_DESCRIPTOR*)
+                        AllocatePages (
+                          EFI_SIZE_TO_PAGES (
+                            sizeof (ARM_MEMORY_REGION_DESCRIPTOR) *
+                            MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS
+                            )
+                          );
+  if (VirtualMemoryTable == NULL) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "%a: Error: Failed to Allocate Pages\n",
+      __FUNCTION__
+      ));
+    return;
+  }
+
+  LOG_MEM_MAP_HEADER ();
+
+  // System DRAM
+  VirtualMemoryTable[Idx].PhysicalBase = PcdGet64 (PcdSystemMemoryBase);
+  VirtualMemoryTable[Idx].VirtualBase  = VirtualMemoryTable[Idx].PhysicalBase;
+  VirtualMemoryTable[Idx].Length       = PcdGet64 (PcdSystemMemorySize);
+  VirtualMemoryTable[Idx].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;
+  LOG_MEM_MAP ("System DRAM Memory Map");
+
+  // Peripheral space before DRAM
+  VirtualMemoryTable[++Idx].PhysicalBase = 0x0;
+  VirtualMemoryTable[Idx].VirtualBase    = 0x0;
+  VirtualMemoryTable[Idx].Length         = PcdGet64 (PcdSystemMemoryBase);
+  VirtualMemoryTable[Idx].Attributes     = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
+  LOG_MEM_MAP ("Peripheral space before DRAM");
+
+  // Peripheral space after DRAM
+  VirtualMemoryTable[++Idx].PhysicalBase = PcdGet64 (PcdSystemMemoryBase) +
+                                           PcdGet64 (PcdSystemMemorySize);
+  VirtualMemoryTable[Idx].VirtualBase    = VirtualMemoryTable[Idx].PhysicalBase;
+  VirtualMemoryTable[Idx].Length         = TopOfAddressSpace -
+                                           VirtualMemoryTable[Idx].PhysicalBase;
+  VirtualMemoryTable[Idx].Attributes     = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
+  LOG_MEM_MAP ("Peripheral space after DRAM");
+
+  // Map the FV region as normal executable memory
+  VirtualMemoryTable[++Idx].PhysicalBase = PcdGet64 (PcdFvBaseAddress);
+  VirtualMemoryTable[Idx].VirtualBase  = VirtualMemoryTable[Idx].PhysicalBase;
+  VirtualMemoryTable[Idx].Length       = FixedPcdGet32 (PcdFvSize);
+  VirtualMemoryTable[Idx].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;
+  LOG_MEM_MAP ("FV region");
+
+  // End of Table
+  VirtualMemoryTable[++Idx].PhysicalBase  = 0;
+  VirtualMemoryTable[Idx].VirtualBase     = 0;
+  VirtualMemoryTable[Idx].Length          = 0;
+  VirtualMemoryTable[Idx].Attributes      = (ARM_MEMORY_REGION_ATTRIBUTES)0;
+
+  ASSERT((Idx + 1) <= MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS);
+
+  *VirtualMemoryMap = VirtualMemoryTable;
+}
diff --git a/ArmVirtPkg/Library/KvmtoolVirtMemInfoLib/KvmtoolVirtMemInfoLib.inf b/ArmVirtPkg/Library/KvmtoolVirtMemInfoLib/KvmtoolVirtMemInfoLib.inf
new file mode 100644
index 0000000000000000000000000000000000000000..dbf4ceabe3ae0db5e743e1d9a575542dca32ed0a
--- /dev/null
+++ b/ArmVirtPkg/Library/KvmtoolVirtMemInfoLib/KvmtoolVirtMemInfoLib.inf
@@ -0,0 +1,42 @@
+#/* @file
+#
+#  Copyright (c) 2018, ARM Limited. All rights reserved.
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#*/
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = KvmtoolVirtMemInfoLib
+  FILE_GUID                      = B752E953-394F-462C-811C-F8BE35C8C071
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = ArmVirtMemInfoLib
+
+[Sources]
+  KvmtoolVirtMemInfoLib.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  ArmVirtPkg/ArmVirtPkg.dec
+  EmbeddedPkg/EmbeddedPkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  ArmLib
+  BaseLib
+  BaseMemoryLib
+  DebugLib
+  MemoryAllocationLib
+  PcdLib
+
+[Pcd]
+  gArmTokenSpaceGuid.PcdFvBaseAddress
+  gArmTokenSpaceGuid.PcdSystemMemoryBase
+  gArmTokenSpaceGuid.PcdSystemMemorySize
+
+[FixedPcd]
+  gArmTokenSpaceGuid.PcdFvSize
+
-- 
'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'


  parent reply	other threads:[~2020-06-24 13:35 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-24 13:34 [PATCH v3 00/15] Kvmtool guest firmware support for Arm Sami Mujawar
2020-06-24 13:34 ` [PATCH v3 01/15] PcAtChipsetPkg: Add MMIO Support to RTC driver Sami Mujawar
2020-06-25 11:24   ` Ard Biesheuvel
2020-06-29  2:36     ` [edk2-devel] " Guomin Jiang
2020-06-24 13:34 ` [PATCH v3 02/15] ArmVirtPkg: Add Kvmtool RTC Fdt Client Library Sami Mujawar
2020-06-25 11:31   ` Ard Biesheuvel
2020-06-24 13:34 ` [PATCH v3 03/15] ArmPlatformPkg: Dynamic flash variable base Sami Mujawar
2020-06-24 13:34 ` [PATCH v3 04/15] ArmVirtPkg: Add kvmtool platform driver Sami Mujawar
2020-06-25 11:35   ` Ard Biesheuvel
2020-06-24 13:34 ` Sami Mujawar [this message]
2020-06-24 13:34 ` [PATCH v3 06/15] ArmVirtPkg: Add Kvmtool NOR flash lib Sami Mujawar
2020-06-25  8:45   ` Philippe Mathieu-Daudé
2020-06-25 11:19     ` [edk2-devel] " Ard Biesheuvel
2020-06-25 11:41       ` Philippe Mathieu-Daudé
2020-06-25 11:38   ` Ard Biesheuvel
2020-06-25 17:33     ` Sami Mujawar
2020-06-24 13:34 ` [PATCH v3 07/15] ArmVirtPkg: Early serial port initialisation Sami Mujawar
2020-06-25 11:42   ` Ard Biesheuvel
2020-06-26  9:23     ` Julien Grall
2020-06-24 13:34 ` [PATCH v3 08/15] MdeModulePkg: Fix constructor invocation ordering Sami Mujawar
2020-06-25 13:51   ` Ard Biesheuvel
2020-06-26 13:22     ` Laszlo Ersek
2020-06-27 11:37       ` Ard Biesheuvel
2020-06-24 13:34 ` [PATCH v3 09/15] ArmVirtPkg: GUID Hob for 16550 UART base address Sami Mujawar
2020-06-25 13:52   ` Ard Biesheuvel
2020-06-24 13:34 ` [PATCH v3 10/15] ArmVirtPkg: 16550 UART Platform hook library Sami Mujawar
2020-06-25 13:56   ` Ard Biesheuvel
2020-06-24 13:34 ` [PATCH v3 11/15] ArmVirtPkg: Add Kvmtool Platform Pei Lib Sami Mujawar
2020-06-25 14:05   ` Ard Biesheuvel
2020-06-24 13:34 ` [PATCH v3 12/15] ArmVirtPkg: Support for kvmtool virtual platform Sami Mujawar
2020-06-25 14:08   ` Ard Biesheuvel
2020-06-26 13:26     ` Laszlo Ersek
2020-06-24 13:34 ` [PATCH v3 13/15] ArmVirtPkg: Package dependency for MC146818 RTC Sami Mujawar
2020-06-25 14:08   ` Ard Biesheuvel
2020-06-24 13:34 ` [PATCH v3 14/15] ArmVirtPkg: Add kvmtool to package dictionary Sami Mujawar
2020-06-25 14:09   ` Ard Biesheuvel
2020-06-24 13:34 ` [PATCH v3 15/15] Maintainer.txt: Add Kvmtool platform reviewer Sami Mujawar
2020-06-25 14:09   ` Ard Biesheuvel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200624133458.61920-6-sami.mujawar@arm.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox