summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Library
diff options
context:
space:
mode:
authorSami Mujawar <sami.mujawar@arm.com>2026-06-04 19:44:21 +0300
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2026-09-29 16:27:06 +0300
commitd45393dc7747cb079fa67924ac61007ee8e89062 (patch)
tree6a745ee4b63f8e258f236d20a55303f55f6a3b10 /MdeModulePkg/Library
parent72fa3af90beeee7690ab4626fd372e0baa830fa4 (diff)
downloadedk2-d45393dc7747cb079fa67924ac61007ee8e89062.tar.xz
ArmVirtPkg, MdeModulePkg: Move Arm CCA Library to MdeModulePkg
ArmCcaLib provides helper functions for detecting Realm execution state and caching Realm IPA width. These helpers build on the architectural Arm CCA RSI library and are not specific to ArmVirtPkg. Therefore, move ArmCcaLib, ArmCcaLibNull, their public header, and related GUID declarations to MdeModulePkg. Update ArmVirtPkg library mappings and consumers to use the new MdeModulePkg library instances. Also perform minor non-functional cleanup to the moved INF files by fixing a comment typo and ordering the package dependencies. Continuous-integration-options: PatchCheck.ignore-multi-package Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
Diffstat (limited to 'MdeModulePkg/Library')
-rw-r--r--MdeModulePkg/Library/ArmCcaLib/ArmCcaLib.c287
-rw-r--r--MdeModulePkg/Library/ArmCcaLib/ArmCcaLib.inf34
-rw-r--r--MdeModulePkg/Library/ArmCcaLibNull/ArmCcaLibNull.c74
-rw-r--r--MdeModulePkg/Library/ArmCcaLibNull/ArmCcaLibNull.inf28
4 files changed, 423 insertions, 0 deletions
diff --git a/MdeModulePkg/Library/ArmCcaLib/ArmCcaLib.c b/MdeModulePkg/Library/ArmCcaLib/ArmCcaLib.c
new file mode 100644
index 0000000000..aa40dc3052
--- /dev/null
+++ b/MdeModulePkg/Library/ArmCcaLib/ArmCcaLib.c
@@ -0,0 +1,287 @@
+/** @file
+ Library that implements the Arm CCA helper functions.
+
+ Copyright (c) 2022 - 2026, 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 <Base.h>
+
+#include <Library/ArmCcaLib.h>
+#include <Library/ArmCcaRsiLib.h>
+#include <Library/ArmLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+
+#include <Uefi/UefiMultiPhase.h>
+#include <Uefi/UefiSpec.h>
+#include <Pi/PiBootMode.h>
+
+#include <Pi/PiHob.h>
+#include <Library/HobLib.h>
+
+/**
+ Queries to see if running in a Realm.
+
+ @retval TRUE The execution is within the context of a Realm.
+ @retval FALSE The execution is not within the context of a Realm.
+**/
+STATIC
+BOOLEAN
+EFIAPI
+ArmCcaReadIsRealm (
+ VOID
+ )
+{
+ RETURN_STATUS Status;
+ UINT32 UefiImpl;
+ UINT32 RmmImplLow;
+ UINT32 RmmImplHigh;
+
+ // Query the RSI_GET_VERSION. If it is implemented then
+ // RMM is present and we must be running in a Realm.
+ Status = ArmCcaRsiGetVersion (
+ &UefiImpl,
+ &RmmImplLow,
+ &RmmImplHigh
+ );
+ if (!RETURN_ERROR (Status)) {
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+/**
+ Check if running in a Realm.
+
+ @retval TRUE The execution is within the context of a Realm.
+ @retval FALSE The execution is not within the context of a Realm.
+**/
+BOOLEAN
+EFIAPI
+ArmCcaIsRealm (
+ VOID
+ )
+{
+ BOOLEAN RealmWorld;
+ VOID *HobList;
+
+ VOID *Hob;
+ UINT64 *IsRealmHobData;
+
+ if (!ArmHasRme ()) {
+ return FALSE;
+ }
+
+ HobList = GetHobList ();
+ if (HobList == NULL) {
+ // HOB not initialised.
+ return ArmCcaReadIsRealm ();
+ }
+
+ RealmWorld = FALSE;
+ Hob = GetFirstGuidHob (&gArmCcaIsRealmGuid);
+ if ((Hob == NULL) ||
+ (GET_GUID_HOB_DATA_SIZE (Hob) < sizeof (*IsRealmHobData)))
+ {
+ // The gArmCcaIsRealmGuid is created in the PlatformPeim() which
+ // may not have been called for caching the IsRealm value yet.
+ // So, read the Realm value.
+ RealmWorld = ArmCcaReadIsRealm ();
+ } else {
+ // Read the value from the HOB
+ IsRealmHobData = GET_GUID_HOB_DATA (Hob);
+ RealmWorld = *IsRealmHobData;
+ }
+
+ return RealmWorld;
+}
+
+/**
+ Read the IPA width of the Realm.
+
+ The IPA width of the Realm is used to configure the protection attribute
+ for memory regions, see ArmCcaSetMemoryProtectionAttribute().
+
+ @param [out] IpaWidth IPA width of the Realm.
+
+ @retval RETURN_SUCCESS Success.
+ @retval RETURN_INVALID_PARAMETER A parameter is invalid.
+ @retval RETURN_OUT_OF_RESOURCES Out of resources.
+
+**/
+STATIC
+RETURN_STATUS
+EFIAPI
+ArmCcaReadIpaWidth (
+ OUT UINT64 *IpaWidth
+ )
+{
+ RETURN_STATUS Status;
+ ARM_CCA_REALM_CONFIG *Config;
+
+ if (IpaWidth == NULL) {
+ return RETURN_INVALID_PARAMETER;
+ }
+
+ Config = AllocateAlignedPages (
+ EFI_SIZE_TO_PAGES (sizeof (ARM_CCA_REALM_CONFIG)),
+ ARM_CCA_REALM_GRANULE_SIZE
+ );
+ if (Config == NULL) {
+ ASSERT (0);
+ return RETURN_OUT_OF_RESOURCES;
+ }
+
+ ZeroMem (Config, sizeof (ARM_CCA_REALM_CONFIG));
+
+ Status = ArmCcaRsiGetRealmConfig (Config);
+ if (RETURN_ERROR (Status)) {
+ ASSERT (0);
+ goto ExitHandler;
+ }
+
+ if (Config->IpaWidth > ArmGetPhysicalAddressBits ()) {
+ ASSERT (0);
+ Status = RETURN_INVALID_PARAMETER;
+ goto ExitHandler;
+ }
+
+ *IpaWidth = Config->IpaWidth;
+
+ExitHandler:
+ FreeAlignedPages (Config, EFI_SIZE_TO_PAGES (sizeof (ARM_CCA_REALM_CONFIG)));
+ return Status;
+}
+
+/**
+ Return the IPA width of the Realm.
+
+ The IPA width of the Realm is used to configure the protection attribute
+ for memory regions, see ArmCcaSetMemoryProtectionAttribute().
+
+ @param [out] IpaWidth IPA width of the Realm.
+
+ @retval RETURN_SUCCESS Success.
+ @retval RETURN_INVALID_PARAMETER A parameter is invalid.
+ @retval RETURN_UNSUPPORTED The execution is not within the
+ context of a Realm.
+ @retval RETURN_OUT_OF_RESOURCES Out of resources.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaGetIpaWidth (
+ OUT UINT64 *IpaWidth
+ )
+{
+ RETURN_STATUS Status;
+ VOID *HobList;
+ VOID *Hob;
+ UINT64 *CcaIpaWidth;
+
+ if (IpaWidth == NULL) {
+ return RETURN_INVALID_PARAMETER;
+ }
+
+ if (!ArmCcaIsRealm ()) {
+ *IpaWidth = 0;
+ return RETURN_UNSUPPORTED;
+ }
+
+ HobList = GetHobList ();
+ if (HobList == NULL) {
+ // HOB not initialised.
+ return ArmCcaReadIpaWidth (IpaWidth);
+ }
+
+ Hob = GetFirstGuidHob (&gArmCcaIpaWidthGuid);
+ if ((Hob == NULL) ||
+ (GET_GUID_HOB_DATA_SIZE (Hob) < sizeof (UINT64)))
+ {
+ // The gArmCcaIpaWidthGuid is created in the PlatformPeim() which
+ // may not have been called for caching the IpaWidth value yet.
+ // So, read the IPA Width value.
+ Status = ArmCcaReadIpaWidth (IpaWidth);
+ if (RETURN_ERROR (Status)) {
+ ASSERT (0);
+ return Status;
+ }
+ } else {
+ // Read the value from the HOB.
+ CcaIpaWidth = GET_GUID_HOB_DATA (Hob);
+ if ((UINT64)*CcaIpaWidth == 0) {
+ return RETURN_NOT_FOUND;
+ }
+
+ *IpaWidth = *CcaIpaWidth;
+ }
+
+ return RETURN_SUCCESS;
+}
+
+/** Check if the address range is protected MMIO
+
+ @param [in] BaseAddress Base address of the device MMIO region.
+ @param [in] Length Length of the device MMIO region.
+ @param [out] IsProtectedMmio TRUE - if the RIPAS for the address range is
+ protected MMIO.
+ FALSE - if the RIPAS for the address range is
+ not protected MMIO.
+
+ @retval RETURN_SUCCESS Success.
+ @retval RETURN_INVALID_PARAMETER A parameter is invalid.
+ @retval RETURN_UNSUPPORTED The request is not initiated in a
+ Realm.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaMemRangeIsProtectedMmio (
+ IN UINT64 BaseAddress,
+ IN UINT64 Length,
+ OUT BOOLEAN *IsProtectedMmio
+ )
+{
+ RETURN_STATUS RetStatus;
+ UINT64 *Base;
+ UINT64 *Top;
+ UINT64 *End;
+ ARM_CCA_RIPAS State;
+
+ if ((Length == 0) || (IsProtectedMmio == NULL)) {
+ return RETURN_INVALID_PARAMETER;
+ }
+
+ Base = (UINT64 *)BaseAddress;
+ End = (UINT64 *)(BaseAddress + Length);
+
+ while (Base < End) {
+ Top = End;
+ RetStatus = ArmCcaRsiGetIpaState (
+ Base,
+ &Top,
+ &State
+ );
+ if (RETURN_ERROR (RetStatus)) {
+ // An error occurred.
+ return RetStatus;
+ }
+
+ if (State != RipasDev) {
+ // The address range is not protected MMIO.
+ *IsProtectedMmio = FALSE;
+ return RetStatus;
+ }
+
+ Base = Top;
+ } // while
+
+ *IsProtectedMmio = TRUE;
+ return RETURN_SUCCESS;
+}
diff --git a/MdeModulePkg/Library/ArmCcaLib/ArmCcaLib.inf b/MdeModulePkg/Library/ArmCcaLib/ArmCcaLib.inf
new file mode 100644
index 0000000000..d7c9caf88c
--- /dev/null
+++ b/MdeModulePkg/Library/ArmCcaLib/ArmCcaLib.inf
@@ -0,0 +1,34 @@
+## @file
+# Library that implements the Arm CCA helper functions.
+#
+# Copyright (c) 2022 - 2026, Arm Limited. All rights reserved.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+ INF_VERSION = 0x0001001B
+ BASE_NAME = ArmCcaLib
+ FILE_GUID = 11C18743-52F9-405E-B35B-D7BE91A26F9F
+ MODULE_TYPE = BASE
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = ArmCcaLib
+
+[Sources]
+ ArmCcaLib.c
+
+[Packages]
+ MdeModulePkg/MdeModulePkg.dec
+ MdePkg/MdePkg.dec
+
+[LibraryClasses]
+ ArmCcaRsiLib
+ ArmLib
+ BaseLib
+ HobLib
+ MemoryAllocationLib
+
+[Guids]
+ gArmCcaIpaWidthGuid
+ gArmCcaIsRealmGuid
diff --git a/MdeModulePkg/Library/ArmCcaLibNull/ArmCcaLibNull.c b/MdeModulePkg/Library/ArmCcaLibNull/ArmCcaLibNull.c
new file mode 100644
index 0000000000..5adbddc6d3
--- /dev/null
+++ b/MdeModulePkg/Library/ArmCcaLibNull/ArmCcaLibNull.c
@@ -0,0 +1,74 @@
+/** @file
+ Null implemmentation of the ArmCcaLib library.
+
+ 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 <Base.h>
+
+/**
+ Check if running in a Realm.
+
+ @retval TRUE The execution is within the context of a Realm.
+ @retval FALSE The execution is not within the context of a Realm.
+**/
+BOOLEAN
+EFIAPI
+ArmCcaIsRealm (
+ VOID
+ )
+{
+ return FALSE;
+}
+
+/**
+ Return the IPA width of the Realm.
+
+ The IPA width of the Realm is used to configure the protection attribute
+ for memory regions, see ArmCcaSetMemoryProtectionAttribute().
+
+ @param [out] IpaWidth IPA width of the Realm.
+
+ @retval RETURN_SUCCESS Success.
+ @retval RETURN_INVALID_PARAMETER A parameter is invalid.
+ @retval RETURN_UNSUPPORTED The execution is not within the
+ context of a Realm.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaGetIpaWidth (
+ OUT UINT64 *IpaWidth
+ )
+{
+ return RETURN_UNSUPPORTED;
+}
+
+/** Check if the address range is protected MMIO
+
+ @param [in] BaseAddress Base address of the device MMIO region.
+ @param [in] Length Length of the device MMIO region.
+ @param [out] IsProtectedMmio TRUE - if the RIPAS for the address range is
+ protected MMIO.
+ FALSE - if the RIPAS for the address range is
+ not protected MMIO.
+
+ @retval RETURN_SUCCESS Success.
+ @retval RETURN_INVALID_PARAMETER A parameter is invalid.
+ @retval RETURN_UNSUPPORTED The request is not initiated in a
+ Realm.
+**/
+RETURN_STATUS
+EFIAPI
+ArmCcaMemRangeIsProtectedMmio (
+ IN UINT64 BaseAddress,
+ IN UINT64 Length,
+ OUT BOOLEAN *IsProtectedMmio
+ )
+{
+ return RETURN_UNSUPPORTED;
+}
diff --git a/MdeModulePkg/Library/ArmCcaLibNull/ArmCcaLibNull.inf b/MdeModulePkg/Library/ArmCcaLibNull/ArmCcaLibNull.inf
new file mode 100644
index 0000000000..a7373aa24e
--- /dev/null
+++ b/MdeModulePkg/Library/ArmCcaLibNull/ArmCcaLibNull.inf
@@ -0,0 +1,28 @@
+## @file
+# Null implementation of the ArmCcaLib library.
+#
+# Copyright (c) 2022 - 2023, Arm Limited. All rights reserved.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+ INF_VERSION = 0x0001001B
+ BASE_NAME = ArmCcaLib
+ FILE_GUID = 9E3F7AAA-10A6-4513-A960-B87F4D7DCFC5
+ MODULE_TYPE = BASE
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = ArmCcaLib
+
+[Sources]
+ ArmCcaLibNull.c
+
+
+[Packages]
+ MdeModulePkg/MdeModulePkg.dec
+ MdePkg/MdePkg.dec
+
+[LibraryClasses]
+ BaseLib
+