From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web09.9861.1637665835171655654 for ; Tue, 23 Nov 2021 03:10:35 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: arm.com, ip: 217.140.110.172, mailfrom: pierre.gondois@arm.com) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id E5F20152B; Tue, 23 Nov 2021 03:10:32 -0800 (PST) Received: from e126645.nice.arm.com (unknown [10.34.129.32]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 24FBB3F5A1; Tue, 23 Nov 2021 03:10:32 -0800 (PST) From: "PierreGondois" To: pierre.gondois@arm.com, devel@edk2.groups.io Cc: Sami Mujawar , Alexei Fedorov Subject: [PATCH v2 4/5] DynamicTablesPkg: DynamicPlatRepo: Add TokenMapper Date: Tue, 23 Nov 2021 12:10:09 +0100 Message-Id: <20211123111010.72679-5-Pierre.Gondois@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211123111010.72679-1-Pierre.Gondois@arm.com> References: <20211123111010.72679-1-Pierre.Gondois@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Pierre Gondois The DynamicPlatRepo library allows to handle dynamically created CmObj. The dynamic platform repository can be in the following states: 1 - Non-initialised 2 - Transient: Possibility to add CmObj to the platform, but not to query them. 3 - Finalised: Possibility to query CmObj, but not to add new. A token is allocated to each CmObj added to the dynamic platform repository (except for reference tokens CmObj). This allows to retrieve dynamic CmObjs among all CmObj (static CmObj for instance). This patch add the TokenMapper files, allowing to retrieve a CmObj from a token/CmObjId couple. Signed-off-by: Pierre Gondois --- .../Common/DynamicPlatRepoLib/TokenMapper.c | 218 ++++++++++++++++++ .../Common/DynamicPlatRepoLib/TokenMapper.h | 123 ++++++++++ 2 files changed, 341 insertions(+) create mode 100644 DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/To= kenMapper.c create mode 100644 DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/To= kenMapper.h diff --git a/DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/TokenMapp= er.c b/DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/TokenMapper.c new file mode 100644 index 000000000000..a871320715a3 --- /dev/null +++ b/DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/TokenMapper.c @@ -0,0 +1,218 @@ +/** @file + Token Mapper + + Copyright (c) 2021, Arm Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + + @par Glossary: + - Cm or CM - Configuration Manager + - Obj or OBJ - Object +**/ + +#include +#include +#include +#include + +#include "TokenMapper.h" + +/** Add a CmObjDesc to the TokenMapper. + + @param [in] TokenMapper The TokenMapper instance. + @param [in] Token CmObj token. + @param [in] ObjectId CmObj ObjectId. + @param [in] Size CmObj Size. + @param [in] Data CmObj Data. + This memory is referenced, not copied. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_BUFFER_TOO_SMALL Buffer too small. + @retval EFI_INVALID_PARAMETER A parameter is invalid. +**/ +EFI_STATUS +EFIAPI +TokenMapperAddObject ( + IN TOKEN_MAPPER *TokenMapper, + IN CM_OBJECT_TOKEN Token, + IN CM_OBJECT_ID ObjectId, + IN UINT32 Size, + IN VOID *Data + ) +{ + TOKEN_MAP_DESCRIPTOR *TokenMapDesc; + CM_OBJ_DESCRIPTOR *CmObjDesc; + + if ((TokenMapper =3D=3D NULL) || + (TokenMapper->TokenDescArray =3D=3D NULL) || + (Size =3D=3D 0) || + (Data =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + if (TokenMapper->ItemCount >=3D TokenMapper->MaxTokenDescCount) { + ASSERT (0); + return EFI_BUFFER_TOO_SMALL; + } + + TokenMapDesc =3D &TokenMapper->TokenDescArray[TokenMapper->ItemCount++= ]; + TokenMapDesc->Token =3D Token; + CmObjDesc =3D &TokenMapDesc->CmObjDesc; + CmObjDesc->ObjectId =3D ObjectId; + CmObjDesc->Size =3D Size; + + // Point inside the finalized array. + CmObjDesc->Data =3D Data; + + // Only EArmObjCmRef CmObj can be added as arrays (more than 1 element= s). + if ((GET_CM_NAMESPACE_ID (ObjectId) =3D=3D EObjNameSpaceArm) && + (GET_CM_OBJECT_ID (ObjectId) =3D=3D EArmObjCmRef)) { + CmObjDesc->Count =3D Size / sizeof (CM_ARM_OBJ_REF); + } else { + CmObjDesc->Count =3D 1; + } + + return EFI_SUCCESS; +} + +/** Get a CmObjDesc from a ObjectId/Token couple. + + The Token parameter is not optional. An existing token must be provide= d. + + @param [in] TokenMapper The TokenMapper instance. + @param [in] Token Token of the CmObj to search. + @param [in] ObjectId Object Id of the CmObj to search. + @param [out] CmObjDesc CM_OBJ_DESCRIPTOR containing the CmObj sear= ched. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER A parameter is invalid. + @retval EFI_NOT_FOUND Not found. +**/ +EFI_STATUS +EFIAPI +TokenMapperGetObject ( + IN TOKEN_MAPPER *TokenMapper, + IN CM_OBJECT_TOKEN Token, + IN CM_OBJECT_ID ObjectId, + OUT CM_OBJ_DESCRIPTOR *CmObjDesc + ) +{ + UINTN Index; + UINTN MaxCount; + TOKEN_MAP_DESCRIPTOR *TokenMapDesc; + + // Nothing to do. + if ((TokenMapper !=3D NULL) && (TokenMapper->MaxTokenDescCount =3D=3D = 0)) { + goto exit_handler; + } + + if ((Token =3D=3D CM_NULL_TOKEN) || + (CmObjDesc =3D=3D NULL) || + (TokenMapper =3D=3D NULL) || + (TokenMapper->TokenDescArray =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + TokenMapDesc =3D TokenMapper->TokenDescArray; + MaxCount =3D TokenMapper->MaxTokenDescCount; + for (Index =3D 0; Index < MaxCount; Index++) { + if ((TokenMapDesc->CmObjDesc.ObjectId =3D=3D ObjectId) && + (TokenMapDesc->Token =3D=3D Token)) { + CopyMem ( + CmObjDesc, + &TokenMapDesc->CmObjDesc, + sizeof (CM_OBJ_DESCRIPTOR) + ); + return EFI_SUCCESS; + } + TokenMapDesc++; + } // for + +exit_handler: + DEBUG (( + DEBUG_INFO, + "INFO: Requested CmObj of type 0x%x with token 0x%x" + " not found in the dynamic repository\n.", + ObjectId, + Token + )); + return EFI_NOT_FOUND; +} + +/** Initialise a TokenMapper. + + @param [in] TokenMapper The TokenMapper to initialise. + @param [in] DescriptorCount Number of entries to allocate. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_ALREADY_STARTED Instance already initialised. + @retval EFI_INVALID_PARAMETER A parameter is invalid. +**/ +EFI_STATUS +EFIAPI +TokenMapperInitialise ( + IN TOKEN_MAPPER * TokenMapper, + IN UINTN DescriptorCount + ) +{ + if (TokenMapper =3D=3D NULL) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + // Nothing to do. + if (DescriptorCount =3D=3D 0) { + return EFI_SUCCESS; + } + + if (TokenMapper->TokenDescArray !=3D NULL) { + DEBUG ((DEBUG_ERROR, "ERROR: Token mapper already initialised\n.")); + ASSERT (0); + return EFI_ALREADY_STARTED; + } + + TokenMapper->TokenDescArray =3D + AllocateZeroPool (sizeof (TOKEN_MAP_DESCRIPTOR) * DescriptorCount); + if (TokenMapper->TokenDescArray =3D=3D NULL) { + ASSERT (0); + return EFI_OUT_OF_RESOURCES; + } + + TokenMapper->MaxTokenDescCount =3D DescriptorCount; + TokenMapper->ItemCount =3D 0; + + return EFI_SUCCESS; +} + +/** Shutdown a TokenMapper. + + @param [in] TokenMapper The TokenMapper to shutdown. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER A parameter is invalid. +**/ +EFI_STATUS +EFIAPI +TokenMapperShutdown ( + IN TOKEN_MAPPER * TokenMapper + ) +{ + // Nothing to do. + if ((TokenMapper !=3D NULL) && (TokenMapper->MaxTokenDescCount =3D=3D = 0)) { + return EFI_SUCCESS; + } + + if ((TokenMapper =3D=3D NULL) || + (TokenMapper->TokenDescArray =3D=3D NULL)) { + ASSERT (0); + return EFI_INVALID_PARAMETER; + } + + FreePool (TokenMapper->TokenDescArray); + TokenMapper->TokenDescArray =3D NULL; + TokenMapper->MaxTokenDescCount =3D 0; + + return EFI_SUCCESS; +} diff --git a/DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/TokenMapp= er.h b/DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/TokenMapper.h new file mode 100644 index 000000000000..377fc03dcd01 --- /dev/null +++ b/DynamicTablesPkg/Library/Common/DynamicPlatRepoLib/TokenMapper.h @@ -0,0 +1,123 @@ +/** @file + Token Mapper + + Copyright (c) 2021, Arm Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + + @par Glossary: + - Cm or CM - Configuration Manager + - Obj or OBJ - Object +**/ + +#ifndef TOKEN_MAPPER_H_ +#define TOKEN_MAPPER_H_ + +#pragma pack(1) + +/** Token mapping descriptor. + + Bind a token and a CmObj together. +*/ +typedef struct TokenMapDescriptor { + /// Object Token. + CM_OBJECT_TOKEN Token; + + /// CmObjectDescriptor CM_OBJ_DESCRIPTOR.Data is a reference copy + /// and not allocated. It points to the individual objects in the + /// Dynamic Plat Repo ArmNameSpaceObjectArray. + CM_OBJ_DESCRIPTOR CmObjDesc; +} TOKEN_MAP_DESCRIPTOR; + +/** Token mapper. + + Contain all the Token/CmObj couple mapping. +**/ +typedef struct TokenMapper { + /// Maximum number of TOKEN_MAP_DESCRIPTOR entries in TokenDescArray. + UINTN MaxTokenDescCount; + + /// Next TOKEN_MAP_DESCRIPTOR entry to use in TokenDescArray. + UINTN ItemCount; + + /// Array of TOKEN_MAP_DESCRIPTOR. + TOKEN_MAP_DESCRIPTOR * TokenDescArray; +} TOKEN_MAPPER; + +#pragma pack() + +/** Add a CmObjDesc to the TokenMapper. + + @param [in] TokenMapper The TokenMapper instance. + @param [in] Token CmObj token. + @param [in] ObjectId CmObj ObjectId. + @param [in] Size CmObj Size. + @param [in] Data CmObj Data. + This memory is referenced, not copied. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_BUFFER_TOO_SMALL Buffer too small. + @retval EFI_INVALID_PARAMETER A parameter is invalid. +**/ +EFI_STATUS +EFIAPI +TokenMapperAddObject ( + IN TOKEN_MAPPER *TokenMapper, + IN CM_OBJECT_TOKEN Token, + IN CM_OBJECT_ID ObjectId, + IN UINT32 Size, + IN VOID *Data + ); + +/** Get a CmObjDesc from a ObjectId/Token couple. + + The Token parameter is not optional. An existing token must be provide= d. + + @param [in] TokenMapper The TokenMapper instance. + @param [in] Token Token of the CmObj to search. + @param [in] ObjectId Object Id of the CmObj to search. + @param [out] CmObjDesc CM_OBJ_DESCRIPTOR containing the CmObj sear= ched. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER A parameter is invalid. + @retval EFI_NOT_FOUND Not found. +**/ +EFI_STATUS +EFIAPI +TokenMapperGetObject ( + IN TOKEN_MAPPER *TokenMapper, + IN CM_OBJECT_TOKEN Token, + IN CM_OBJECT_ID ObjectId, + OUT CM_OBJ_DESCRIPTOR *CmObjDesc + ); + +/** Initialise a TokenMapper. + + @param [in] TokenMapper The TokenMapper to initialise. + @param [in] DescriptorCount Number of entries to allocate. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_ALREADY_STARTED Instance already initialised. + @retval EFI_INVALID_PARAMETER A parameter is invalid. +**/ +EFI_STATUS +EFIAPI +TokenMapperInitialise ( + IN TOKEN_MAPPER * TokenMapper, + IN UINTN DescriptorCount + ); + +/** Shutdown a TokenMapper. + + @param [in] TokenMapper The TokenMapper to shutdown. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_INVALID_PARAMETER A parameter is invalid. +**/ +EFI_STATUS +EFIAPI +TokenMapperShutdown ( + IN TOKEN_MAPPER * TokenMapper + ); + +#endif // TOKEN_MAPPER_H_ --=20 2.25.1