Add ArmCcaInitPeiLib library that performs the Arm CCA specific
initialisation in the PEI phase like:
 - Configuring the system memory as Protected RAM.
 - Reading the Realm Config and storing the IPA width in
   a GUID HOB i.e., gArmCcaIpaWidthGuid for subsequent use.
 - Calling ArmCcaConfigureMmio () to configure the MMIO regions
   by setting the Unprotected IPA attribute in the page tables.

Cc: Ard Biesheuvel <ardb+tianoc...@kernel.org>
Cc: Leif Lindholm <quic_llind...@quicinc.com>
Cc: Gerd Hoffmann <kra...@redhat.com>
Signed-off-by: Sami Mujawar <sami.muja...@arm.com>
---
 ArmVirtPkg/ArmVirtPkg.dec                                |   1 +
 ArmVirtPkg/Include/Library/ArmCcaInitPeiLib.h            |  49 +++++++++
 ArmVirtPkg/Library/ArmCcaInitPeiLib/ArmCcaInitPeiLib.c   | 116 
++++++++++++++++++++
 ArmVirtPkg/Library/ArmCcaInitPeiLib/ArmCcaInitPeiLib.inf |  39 +++++++
 4 files changed, 205 insertions(+)

diff --git a/ArmVirtPkg/ArmVirtPkg.dec b/ArmVirtPkg/ArmVirtPkg.dec
index 
0f95fd43bd189eda713aeccc6c73019e0f815169..23179c89704db789ab649de81873924c3958ab2a
 100644
--- a/ArmVirtPkg/ArmVirtPkg.dec
+++ b/ArmVirtPkg/ArmVirtPkg.dec
@@ -26,6 +26,7 @@ [Includes.common]
   Include                        # Root include for the package
 
 [LibraryClasses]
+  ArmCcaInitPeiLib|Include/Library/ArmCcaInitPeiLib.h
   ArmCcaRsiLib|Include/Library/ArmCcaRsiLib.h
   ArmVirtMemInfoLib|Include/Library/ArmVirtMemInfoLib.h
 
diff --git a/ArmVirtPkg/Include/Library/ArmCcaInitPeiLib.h 
b/ArmVirtPkg/Include/Library/ArmCcaInitPeiLib.h
new file mode 100644
index 
0000000000000000000000000000000000000000..439a70a54a218badd4cd4d6c419df58f57271cc2
--- /dev/null
+++ b/ArmVirtPkg/Include/Library/ArmCcaInitPeiLib.h
@@ -0,0 +1,49 @@
+/** @file
+  Library that implements the Arm CCA helper functions.
+
+  Copyright (c) 2022 2023, Arm Ltd. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+    - Rsi or RSI   - Realm Service Interface
+    - IPA          - Intermediate Physical Address
+    - RIPAS        - Realm IPA state
+**/
+
+#ifndef ARM_CCA_INIT_PEI_LIB_
+#define ARM_CCA_INIT_PEI_LIB_
+
+#include <Base.h>
+
+/**
+  Configure the System Memory region as Protected RAM.
+
+  When a VMM creates a Realm, a small amount of DRAM (which contains the
+  firmware image) and the initial content is configured as Protected RAM.
+  The remaining System Memory is in the Protected Empty state. The firmware
+  must then initialise the remaining System Memory as Protected RAM before
+  it can be accessed.
+
+  @retval RETURN_SUCCESS            Success.
+  @retval RETURN_INVALID_PARAMETER  A parameter is invalid.
+  @retval RETURN_UNSUPPORTED        The execution context is not in a Realm.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaConfigureSystemMemory (
+  VOID
+  );
+
+/**
+  Perform Arm CCA specific initialisations.
+
+  @retval RETURN_SUCCESS               Success or execution context is not a 
Realm.
+  @retval RETURN_OUT_OF_RESOURCES      Out of resources.
+  @retval RETURN_INVALID_PARAMETER     A parameter is invalid.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaInitialize (
+  VOID
+  );
+
+#endif // ARM_CCA_LIB_
diff --git a/ArmVirtPkg/Library/ArmCcaInitPeiLib/ArmCcaInitPeiLib.c 
b/ArmVirtPkg/Library/ArmCcaInitPeiLib/ArmCcaInitPeiLib.c
new file mode 100644
index 
0000000000000000000000000000000000000000..2b2801cc5426994efc15c970fd6b0adf43bd7d36
--- /dev/null
+++ b/ArmVirtPkg/Library/ArmCcaInitPeiLib/ArmCcaInitPeiLib.c
@@ -0,0 +1,116 @@
+/** @file
+  Library that implements the Arm CCA initialisation in PEI phase.
+
+  Copyright (c) 2022 2023, Arm Limited. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  @par Glossary:
+    - Rsi or RSI   - Realm Service Interface
+    - IPA          - Intermediate Physical Address
+    - RIPAS        - Realm IPA state
+**/
+#include <PiPei.h>
+
+#include <Library/ArmCcaLib.h>
+#include <Library/ArmCcaRsiLib.h>
+#include <Library/ArmMmuLib.h>
+#include <Library/ArmVirtMemInfoLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/PcdLib.h>
+
+/**
+  Configure the System Memory region as Protected RAM.
+
+  When a VMM creates a Realm, a small amount of DRAM (which contains the
+  firmware image) and the initial content is configured as Protected RAM.
+  The remaining System Memory is in the Protected Empty state. The firmware
+  must then initialise the remaining System Memory as Protected RAM before
+  it can be accessed.
+
+  @retval RETURN_SUCCESS            Success.
+  @retval RETURN_INVALID_PARAMETER  A parameter is invalid.
+  @retval RETURN_UNSUPPORTED        The execution context is not in a Realm.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaConfigureSystemMemory (
+  VOID
+  )
+{
+  RETURN_STATUS  Status;
+
+  if (!IsRealm ()) {
+    return RETURN_UNSUPPORTED;
+  }
+
+  Status =  RsiSetIpaState (
+              (UINT64 *)PcdGet64 (PcdSystemMemoryBase),
+              PcdGet64 (PcdSystemMemorySize),
+              RipasRam
+              );
+  if (RETURN_ERROR (Status)) {
+    // Panic
+    CpuDeadLoop ();
+  }
+
+  return Status;
+}
+
+/**
+  Perform Arm CCA specific initialisations.
+
+  @retval RETURN_SUCCESS               Success or execution context is not a 
Realm.
+  @retval RETURN_OUT_OF_RESOURCES      Out of resources.
+  @retval RETURN_INVALID_PARAMETER     A parameter is invalid.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaInitialize (
+  VOID
+  )
+{
+  EFI_STATUS    Status;
+  REALM_CONFIG  *Config;
+  UINT64        *IpaWidthHobData;
+
+  if (!IsRealm ()) {
+    // Noting to do as the execution context is not a Realm.
+    return RETURN_SUCCESS;
+  }
+
+  // Read the Realm Config and store the IPA width in a GUID HOB.
+  Config = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (REALM_CONFIG)));
+  if (Config == NULL) {
+    ASSERT (0);
+    return RETURN_OUT_OF_RESOURCES;
+  }
+
+  ZeroMem (Config, sizeof (REALM_CONFIG));
+
+  Status = RsiGetRealmConfig (Config);
+  if (RETURN_ERROR (Status)) {
+    ASSERT (0);
+    return Status;
+  }
+
+  IpaWidthHobData = BuildGuidHob (
+                      &gArmCcaIpaWidthGuid,
+                      sizeof (*IpaWidthHobData)
+                      );
+  if (IpaWidthHobData == NULL) {
+    ASSERT (0);
+    FreePages (Config, EFI_SIZE_TO_PAGES (sizeof (REALM_CONFIG)));
+    return RETURN_OUT_OF_RESOURCES;
+  }
+
+  *IpaWidthHobData = Config->IpaWidth;
+
+  FreePages (Config, EFI_SIZE_TO_PAGES (sizeof (REALM_CONFIG)));
+
+  // Configure the MMIO memory regions.
+  return ArmCcaConfigureMmio (*IpaWidthHobData);
+}
diff --git a/ArmVirtPkg/Library/ArmCcaInitPeiLib/ArmCcaInitPeiLib.inf 
b/ArmVirtPkg/Library/ArmCcaInitPeiLib/ArmCcaInitPeiLib.inf
new file mode 100644
index 
0000000000000000000000000000000000000000..f2a321d9cdfcf1bd87d6584e3c6834686a298bf0
--- /dev/null
+++ b/ArmVirtPkg/Library/ArmCcaInitPeiLib/ArmCcaInitPeiLib.inf
@@ -0,0 +1,39 @@
+## @file
+#  Library that implements the Arm CCA initialisation in PEI phase.
+#
+#  Copyright (c) 2022 - 2023, Arm Limited. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = ArmCcaInitPeiLib
+  FILE_GUID                      = 9A8C3768-79ED-487E-8155-BBF4DD638296
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = ArmCcaInitPeiLib
+
+[Sources]
+  ArmCcaInitPeiLib.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  ArmVirtPkg/ArmVirtPkg.dec
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  ArmCcaLib
+  ArmCcaRsiLib
+  ArmLib
+  ArmMmuLib
+  ArmVirtMemInfoLib
+  BaseLib
+
+[Pcd]
+  gArmTokenSpaceGuid.PcdSystemMemoryBase
+  gArmTokenSpaceGuid.PcdSystemMemorySize
+
+[Guids]
+  gArmCcaIpaWidthGuid
-- 
'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117671): https://edk2.groups.io/g/devel/message/117671
Mute This Topic: https://groups.io/mt/105483409/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-


Reply via email to