From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web08.12716.1632611149834054906 for ; Sat, 25 Sep 2021 16:05:51 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: guo.dong@intel.com) X-IronPort-AV: E=McAfee;i="6200,9189,10118"; a="285311471" X-IronPort-AV: E=Sophos;i="5.85,322,1624345200"; d="scan'208";a="285311471" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2021 16:05:49 -0700 X-IronPort-AV: E=Sophos;i="5.85,322,1624345200"; d="scan'208";a="586418273" Received: from gdong1-mobl1.amr.corp.intel.com ([10.255.67.241]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2021 16:05:49 -0700 From: "Guo Dong" To: devel@edk2.groups.io Cc: ray.ni@intel.com, maurice.ma@intel.com, benjamin.you@intel.com, Guo Dong Subject: [`edk2-devel][PATCH 1/8] UefiPayloadPkg: Add a common SmmAccessDxe module Date: Sat, 25 Sep 2021 16:05:23 -0700 Message-Id: <20210925230530.861-2-guo.dong@intel.com> X-Mailer: git-send-email 2.32.0.windows.2 In-Reply-To: <20210925230530.861-1-guo.dong@intel.com> References: <20210925230530.861-1-guo.dong@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Guo Dong SmmAccessDxe module would consume EFI_SMRAM_HOB_DESCRIPTOR_BLOCK HOB to produce SMM access protocol gEfiSmmAccess2ProtocolGuid (open, close, lock, and GetCapabilities.) Signed-off-by: Guo Dong --- UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.c | 254 +++++++++++++++++++ UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.h | 38 +++ UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.inf | 51 ++++ 3 files changed, 343 insertions(+) create mode 100644 UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.c create mode 100644 UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.h create mode 100644 UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.inf diff --git a/UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.c b/UefiPayloadPkg/Sm= mAccessDxe/SmmAccessDxe.c new file mode 100644 index 0000000000..ad75c6a2d9 --- /dev/null +++ b/UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.c @@ -0,0 +1,254 @@ +/** @file=0D + This driver publishes the SMM Access 2 Protocol.=0D +=0D + Copyright (c) 2021, Intel Corporation. All rights reserved.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include "SmmAccessDxe.h"=0D +=0D +SMM_ACCESS_PRIVATE_DATA mSmmAccess;=0D +=0D +/**=0D + Update region state from SMRAM description=0D +=0D + @param[in] OrLogic Indicate to use OR if ture or AND if false.=0D + @param[in] Value The value to set to region state based on OrLogi= c.=0D +=0D +**/=0D +VOID=0D +SyncRegionState2SmramDesc(=0D + IN BOOLEAN OrLogic,=0D + IN UINT64 Value=0D + )=0D +{=0D + UINT32 Index;=0D +=0D + for (Index =3D 0; Index < mSmmAccess.NumberRegions; Index++) {=0D + if (OrLogic) {=0D + mSmmAccess.SmramDesc[Index].RegionState |=3D Value;=0D + } else {=0D + mSmmAccess.SmramDesc[Index].RegionState &=3D Value;=0D + }=0D + }=0D +}=0D +=0D +/**=0D + This routine accepts a request to "open" a region of SMRAM. The=0D + region could be legacy ABSEG, HSEG, or TSEG near top of physical memory= .=0D + The use of "open" means that the memory is visible from all boot-servic= e=0D + and SMM agents.=0D +=0D + @param This Pointer to the SMM Access Interface.=0D +=0D + @retval EFI_SUCCESS The region was successfully opened.=0D + @retval EFI_DEVICE_ERROR The region could not be opened because l= ocked by chipset.=0D + @retval EFI_INVALID_PARAMETER The descriptor index was out of bounds.= =0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +Open (=0D + IN EFI_SMM_ACCESS2_PROTOCOL *This=0D + )=0D +{=0D + if ((mSmmAccess.SmmRegionState & EFI_SMRAM_LOCKED) !=3D 0) {=0D + //=0D + // Cannot open a "locked" region=0D + //=0D + DEBUG ((DEBUG_INFO, "Cannot open the locked SMRAM Region\n"));=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D + mSmmAccess.SmmRegionState &=3D ~(EFI_SMRAM_CLOSED | EFI_ALLOCATED);=0D + SyncRegionState2SmramDesc(FALSE, (UINT64)(UINTN)(~(EFI_SMRAM_CLOSED | EF= I_ALLOCATED)));=0D +=0D + mSmmAccess.SmmRegionState |=3D EFI_SMRAM_OPEN;=0D + SyncRegionState2SmramDesc(TRUE, EFI_SMRAM_OPEN);=0D + mSmmAccess.SmmAccess.OpenState =3D TRUE;=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + This routine accepts a request to "close" a region of SMRAM. The region= =0D + could be legacy AB or TSEG near top of physical memory.=0D + The use of "close" means that the memory is only visible from SMM agent= s,=0D + not from BS or RT code.=0D +=0D + @param This Pointer to the SMM Access Interface.=0D +=0D + @retval EFI_SUCCESS The region was successfully closed.=0D + @retval EFI_DEVICE_ERROR The region could not be closed because= locked by=0D + chipset.=0D + @retval EFI_INVALID_PARAMETER The descriptor index was out of bounds= .=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +Close (=0D + IN EFI_SMM_ACCESS2_PROTOCOL *This=0D + )=0D +{=0D + if ((mSmmAccess.SmmRegionState & EFI_SMRAM_LOCKED) !=3D 0) {=0D + //=0D + // Cannot close a "locked" region=0D + //=0D + DEBUG ((DEBUG_INFO, "Cannot close the locked SMRAM Region\n"));=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D + if ((mSmmAccess.SmmRegionState & EFI_SMRAM_CLOSED) !=3D 0) {=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D + mSmmAccess.SmmRegionState &=3D ~EFI_SMRAM_OPEN;=0D + SyncRegionState2SmramDesc(FALSE, (UINT64)(UINTN)(~EFI_SMRAM_OPEN));=0D +=0D + mSmmAccess.SmmRegionState |=3D (EFI_SMRAM_CLOSED | EFI_ALLOCATED);=0D + SyncRegionState2SmramDesc(TRUE, EFI_SMRAM_CLOSED | EFI_ALLOCATED);=0D +=0D + mSmmAccess.SmmAccess.OpenState =3D FALSE;=0D +=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + This routine accepts a request to "lock" SMRAM. The=0D + region could be legacy AB or TSEG near top of physical memory.=0D + The use of "lock" means that the memory can no longer be opened=0D + to BS state.=0D +=0D + @param This Pointer to the SMM Access Interface.=0D +=0D + @retval EFI_SUCCESS The region was successfully locked.=0D + @retval EFI_DEVICE_ERROR The region could not be locked because = at least=0D + one range is still open.=0D + @retval EFI_INVALID_PARAMETER The descriptor index was out of bounds.= =0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +Lock (=0D + IN EFI_SMM_ACCESS2_PROTOCOL *This=0D + )=0D +{=0D + if (mSmmAccess.SmmAccess.OpenState) {=0D + DEBUG ((DEBUG_INFO, "Cannot lock SMRAM when it is still open\n"));=0D + return EFI_DEVICE_ERROR;=0D + }=0D +=0D + mSmmAccess.SmmRegionState |=3D EFI_SMRAM_LOCKED;=0D + SyncRegionState2SmramDesc(TRUE, EFI_SMRAM_LOCKED);=0D + mSmmAccess.SmmAccess.LockState =3D TRUE;=0D + return EFI_SUCCESS;=0D +}=0D +=0D +/**=0D + This routine services a user request to discover the SMRAM=0D + capabilities of this platform. This will report the possible=0D + ranges that are possible for SMRAM access, based upon the=0D + memory controller capabilities.=0D +=0D + @param This Pointer to the SMRAM Access Interface.=0D + @param SmramMapSize Pointer to the variable containing size of the=0D + buffer to contain the description information.=0D + @param SmramMap Buffer containing the data describing the Smram= =0D + region descriptors.=0D +=0D + @retval EFI_BUFFER_TOO_SMALL The user did not provide a sufficient buf= fer.=0D + @retval EFI_SUCCESS The user provided a sufficiently-sized bu= ffer.=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +GetCapabilities (=0D + IN CONST EFI_SMM_ACCESS2_PROTOCOL *This,=0D + IN OUT UINTN *SmramMapSize,=0D + IN OUT EFI_SMRAM_DESCRIPTOR *SmramMap=0D + )=0D +{=0D + EFI_STATUS Status;=0D + UINTN NecessaryBufferSize;=0D +=0D + NecessaryBufferSize =3D mSmmAccess.NumberRegions * sizeof(EFI_SMRAM_DESC= RIPTOR);=0D + if (*SmramMapSize < NecessaryBufferSize) {=0D + Status =3D EFI_BUFFER_TOO_SMALL;=0D + } else {=0D + CopyMem(SmramMap, mSmmAccess.SmramDesc, NecessaryBufferSize);=0D + Status =3D EFI_SUCCESS;=0D + }=0D +=0D + *SmramMapSize =3D NecessaryBufferSize;=0D + return Status;=0D +}=0D +=0D +/**=0D + This function installs EFI_SMM_ACCESS_PROTOCOL.=0D +=0D + @param ImageHandle Handle for the image of this driver=0D + @param SystemTable Pointer to the EFI System Table=0D +=0D + @retval EFI_UNSUPPORTED There's no Intel ICH on this platform=0D + @return The status returned from InstallProtocolInterface().=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +SmmAccessEntryPoint (=0D + IN EFI_HANDLE ImageHandle,=0D + IN EFI_SYSTEM_TABLE *SystemTable=0D + )=0D +{=0D + EFI_STATUS Status;=0D + EFI_HOB_GUID_TYPE *GuidHob;=0D + UINT32 SmmRegionNum;=0D + EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *SmramHob;=0D + UINT32 Index;=0D +=0D + //=0D + // Get SMRAM info HOB=0D + //=0D + GuidHob =3D GetFirstGuidHob (&gEfiSmmSmramMemoryGuid);=0D + if (GuidHob =3D=3D NULL) {=0D + DEBUG ((DEBUG_INFO, "SMRAM HOB NOT found\n"));=0D + return EFI_NOT_FOUND;=0D + }=0D + SmramHob =3D (EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *) GET_GUID_HOB_DATA(Gu= idHob);=0D + SmmRegionNum =3D SmramHob->NumberOfSmmReservedRegions;=0D + mSmmAccess.SmramDesc =3D AllocateZeroPool (sizeof (EFI_SMRAM_DESCRIPTOR)= * SmmRegionNum);=0D + if (mSmmAccess.SmramDesc =3D=3D NULL) {=0D + return EFI_OUT_OF_RESOURCES;=0D + }=0D + CopyMem (mSmmAccess.SmramDesc, &SmramHob->Descriptor, sizeof (EFI_SMRAM_= DESCRIPTOR) * SmmRegionNum);=0D +=0D + DEBUG ((DEBUG_INFO, "NumberOfSmmReservedRegions =3D 0x%x\n", SmmRegionNu= m));=0D + for (Index =3D 0; Index < SmmRegionNum; Index++) {=0D + DEBUG ((DEBUG_INFO, "%d: base=3D0x%x, size =3D 0x%x, State=3D0x%x\n",I= ndex,=0D + SmramHob->Descriptor[Index].PhysicalStart,=0D + SmramHob->Descriptor[Index].PhysicalSize,=0D + SmramHob->Descriptor[Index].RegionState));=0D + mSmmAccess.SmramDesc[Index].RegionState &=3D EFI_ALLOCATED;=0D + mSmmAccess.SmramDesc[Index].RegionState |=3D EFI_SMRAM_CLOSED | EFI_C= ACHEABLE;=0D + }=0D +=0D + mSmmAccess.Signature =3D SMM_ACCESS_PRIVATE_DATA_SIGN= ATURE;=0D + mSmmAccess.NumberRegions =3D SmmRegionNum;=0D + mSmmAccess.SmmAccess.Open =3D Open;=0D + mSmmAccess.SmmAccess.Close =3D Close;=0D + mSmmAccess.SmmAccess.Lock =3D Lock;=0D + mSmmAccess.SmmAccess.GetCapabilities =3D GetCapabilities;=0D + mSmmAccess.SmmAccess.LockState =3D FALSE;=0D + mSmmAccess.SmmAccess.OpenState =3D FALSE;=0D + mSmmAccess.SmmRegionState =3D EFI_SMRAM_CLOSED;=0D +=0D + Status =3D gBS->InstallMultipleProtocolInterfaces (=0D + &mSmmAccess.Handle,=0D + &gEfiSmmAccess2ProtocolGuid,=0D + &mSmmAccess.SmmAccess,=0D + NULL=0D + );=0D +=0D + return Status;=0D +}=0D diff --git a/UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.h b/UefiPayloadPkg/Sm= mAccessDxe/SmmAccessDxe.h new file mode 100644 index 0000000000..7b64c0afcb --- /dev/null +++ b/UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.h @@ -0,0 +1,38 @@ +/** @file=0D + The header file of SMM access DXE.=0D +=0D +Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#ifndef _SMM_ACCESS_DRIVER_H=0D +#define _SMM_ACCESS_DRIVER_H=0D +=0D +#include =0D +#include =0D +//#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +=0D +#define SMM_ACCESS_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('S', 'M', 'M', '= A')=0D +=0D +typedef struct {=0D + UINTN Signature;=0D + EFI_HANDLE Handle;=0D + EFI_SMM_ACCESS2_PROTOCOL SmmAccess;=0D + //=0D + // Local Data for SMM Access interface goes here=0D + //=0D + UINT32 SmmRegionState;=0D + UINT32 NumberRegions;=0D + EFI_SMRAM_DESCRIPTOR *SmramDesc;=0D +} SMM_ACCESS_PRIVATE_DATA;=0D +=0D +#endif=0D diff --git a/UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.inf b/UefiPayloadPkg/= SmmAccessDxe/SmmAccessDxe.inf new file mode 100644 index 0000000000..aac5ee8f28 --- /dev/null +++ b/UefiPayloadPkg/SmmAccessDxe/SmmAccessDxe.inf @@ -0,0 +1,51 @@ +## @file=0D +# SMM Access 2 Protocol Dxe Driver=0D +#=0D +# This module produces the SMM Access 2 Protocol.=0D +#=0D +# Copyright (c) 2021, Intel Corporation. All rights reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010005=0D + BASE_NAME =3D SmmAccessDxe=0D + FILE_GUID =3D 47579CF5-1E4F-4b41-99BB-A5C334846D3B= =0D + MODULE_TYPE =3D DXE_DRIVER=0D + VERSION_STRING =3D 1.0=0D + ENTRY_POINT =3D SmmAccessEntryPoint=0D +=0D +#=0D +# The following information is for reference only and not required by the = build tools.=0D +#=0D +# VALID_ARCHITECTURES =3D IA32 X64=0D +#=0D +=0D +[Sources]=0D + SmmAccessDxe.c=0D + SmmAccessDxe.h=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D + UefiPayloadPkg/UefiPayloadPkg.dec=0D +=0D +[LibraryClasses]=0D + UefiDriverEntryPoint=0D + UefiBootServicesTableLib=0D + DebugLib=0D + BaseLib=0D + BaseMemoryLib=0D + MemoryAllocationLib=0D + HobLib=0D +=0D +[Guids]=0D + gEfiSmmSmramMemoryGuid=0D +=0D +[Protocols]=0D + gEfiSmmAccess2ProtocolGuid ## PRODUCES=0D +=0D +[Depex]=0D + TRUE=0D --=20 2.32.0.windows.2