public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Michael D Kinney" <michael.d.kinney@intel.com>
To: "Hsu, VictorX" <victorx.hsu@intel.com>,
	"devel@edk2.groups.io" <devel@edk2.groups.io>,
	"Kinney, Michael D" <michael.d.kinney@intel.com>
Cc: "Guo, Gua" <gua.guo@intel.com>,
	"Chan, Laura" <laura.chan@intel.com>,
	"Prakashan,
	Krishnadas Veliyathuparambil"
	<krishnadas.veliyathuparambil.prakashan@intel.com>,
	"K N, Karthik" <karthik.k.n@intel.com>,
	"Kinney, Michael D" <michael.d.kinney@intel.com>
Subject: Re: [PATCH 2/4] MdeModulePkg: Add TraceHubDebugSysTLib library
Date: Mon, 8 May 2023 17:49:49 +0000	[thread overview]
Message-ID: <CO1PR11MB4929648BB6DC2ADEBFA0D3B5D2719@CO1PR11MB4929.namprd11.prod.outlook.com> (raw)
In-Reply-To: <a0b7564d9356a02f13a67f00b0d55bb318699914.1683523672.git.victorx.hsu@intel.com>

Hi Victor,

Comment on SwapBytesGuid() API below.  I believe there is a significant logic issues and an opportunity to simplify.  Please let me know if you found a different algorithm to perform GUID LE <-> BE conversions.

A second comment on the limit on max instances being 5.  Why is 5 selected?  The HOB list can support any number of instances and the consumers that need to cache the HOB state can do dynamical allocation using AllocatePool() to allocate space for the number of HOBs detected.

Mike

> -----Original Message-----
> From: Hsu, VictorX <victorx.hsu@intel.com>
> Sent: Sunday, May 7, 2023 10:32 PM
> To: devel@edk2.groups.io
> Cc: Hsu, VictorX <victorx.hsu@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>; Guo, Gua <gua.guo@intel.com>; Chan, Laura
> <laura.chan@intel.com>; Prakashan, Krishnadas Veliyathuparambil
> <krishnadas.veliyathuparambil.prakashan@intel.com>; K N, Karthik
> <karthik.k.n@intel.com>
> Subject: [PATCH 2/4] MdeModulePkg: Add TraceHubDebugSysTLib library
> 
> From: VictorX Hsu <victorx.hsu@intel.com>
> 
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4144
> 
> This Library provides API to dump Trace Hub message.
> 
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Guo Gua <gua.guo@intel.com>
> Cc: Chan Laura <laura.chan@intel.com>
> Cc: Prakashan Krishnadas Veliyathuparambil
> <krishnadas.veliyathuparambil.prakashan@intel.com>
> Cc: K N Karthik <karthik.k.n@intel.com>
> Signed-off-by: VictorX Hsu <victorx.hsu@intel.com>
> ---
>  .../Include/Guid/TraceHubDebugInfoHob.h       |  25 ++
>  .../BaseTraceHubDebugSysTLib.c                | 251 +++++++++++++++
>  .../BaseTraceHubDebugSysTLib.inf              |  44 +++
>  .../DxeSmmTraceHubDebugSysTLib.c              | 247 +++++++++++++++
>  .../DxeSmmTraceHubDebugSysTLib.inf            |  50 +++
>  .../InternalTraceHubApi.c                     |  82 +++++
>  .../InternalTraceHubApi.h                     |  46 +++
>  .../InternalTraceHubApiCommon.c               | 208 +++++++++++++
>  .../InternalTraceHubApiCommon.h               | 119 +++++++
>  .../PeiTraceHubDebugSysTLib.c                 | 290 ++++++++++++++++++
>  .../PeiTraceHubDebugSysTLib.inf               |  50 +++
>  .../Library/TraceHubDebugSysTLib/Readme.md    |  30 ++
>  MdeModulePkg/MdeModulePkg.dec                 |  21 ++
>  MdeModulePkg/MdeModulePkg.dsc                 |   3 +
>  MdeModulePkg/MdeModulePkg.uni                 |  18 ++
>  MdePkg/Include/Library/TraceHubDebugSysTLib.h |  81 +++++
>  MdePkg/MdePkg.dec                             |   4 +
>  17 files changed, 1569 insertions(+)
>  create mode 100644 MdeModulePkg/Include/Guid/TraceHubDebugInfoHob.h
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/BaseTraceHubDebugSysTLib.c
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/BaseTraceHubDebugSysTLib.i
> nf
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/DxeSmmTraceHubDebugSysTL
> ib.c
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/DxeSmmTraceHubDebugSysTL
> ib.inf
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApi.c
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApi.h
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApiCommon
> .c
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApiCommon
> .h
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/PeiTraceHubDebugSysTLib.c
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/PeiTraceHubDebugSysTLib.inf
>  create mode 100644
> MdeModulePkg/Library/TraceHubDebugSysTLib/Readme.md
>  create mode 100644 MdePkg/Include/Library/TraceHubDebugSysTLib.h
> 
> diff --git a/MdeModulePkg/Include/Guid/TraceHubDebugInfoHob.h
> b/MdeModulePkg/Include/Guid/TraceHubDebugInfoHob.h
> new file mode 100644
> index 0000000000..3a8fcc3d21
> --- /dev/null
> +++ b/MdeModulePkg/Include/Guid/TraceHubDebugInfoHob.h
> @@ -0,0 +1,25 @@
> +/** @file
> 
> +This header file declares Trace Hub related structure.
> 
> +
> 
> +Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +
> 
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +**/
> 
> +
> 
> +#ifndef TRACE_HUB_DEBUG_INFO_HOB_H_
> 
> +#define TRACE_HUB_DEBUG_INFO_HOB_H_
> 
> +
> 
> +#define TRACEHUB_DEBUG_INFO_HOB_REVISION  1
> 
> +#define MAX_TRACE_HUB_DEBUG_INSTANCE      5

Why is this hard coded to 5 instances?  Can't all consumers use the number of instances of this GUIDed HOB that are present in the HOB list?

> 
> +
> 
> +typedef struct {
> 
> +  UINT16     Revision;            // Structure revision
> 
> +  BOOLEAN    Flag;                // Flag to enable or disable Trace Hub debug
> message.
> 
> +  UINT8      DebugLevel;          // Debug level for Trace Hub.
> 
> +  UINT8      Rvsd[4];             // Reserved for future use
> 
> +  UINT64     TraceHubMmioAddress; // MMIO address where Trace Hub
> debug message output to.
> 
> +} TRACEHUB_DEBUG_INFO_HOB;
> 
> +
> 
> +extern GUID  gTraceHubDebugInfoHobGuid;
> 
> +
> 
> +#endif // TRACE_HUB_DEBUG_INFO_HOB_H_
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/BaseTraceHubDebugSysTLib
> .c
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/BaseTraceHubDebugSysTLib
> .c
> new file mode 100644
> index 0000000000..fbf1cc2a9e
> --- /dev/null
> +++
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/BaseTraceHubDebugSysTLib
> .c
> @@ -0,0 +1,251 @@
> +/** @file
> 
> +System prints Trace Hub message in SEC/PEI/DXE/SMM based on fixed PCDs.
> 
> +Only support single Trace Hub debug instance.
> 
> +
> 
> +Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +
> 
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +
> 
> +**/
> 
> +
> 
> +#include <Base.h>
> 
> +#include <Library/BaseLib.h>
> 
> +#include <Library/PcdLib.h>
> 
> +#include <Library/BaseMemoryLib.h>
> 
> +#include <Library/TraceHubDebugSysTLib.h>
> 
> +#include <Library/MipiSysTLib.h>
> 
> +#include <Library/MipiSysTLib/mipi_syst.h>
> 
> +#include <Guid/TraceHubDebugInfoHob.h>
> 
> +#include "InternalTraceHubApiCommon.h"
> 
> +#include "InternalTraceHubApi.h"
> 
> +
> 
> +/**
> 
> +  Write debug string to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType     Severity type of input message.
> 
> +  @param[in]  Buffer           A pointer to the data buffer.
> 
> +  @param[in]  NumberOfBytes    The size of data buffer.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTDebugWrite (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT8                    *Buffer,
> 
> +  IN UINTN                    NumberOfBytes
> 
> +  )
> 
> +{
> 
> +  MIPI_SYST_HANDLE  MipiSystHandle;
> 
> +  MIPI_SYST_HEADER  MipiSystHeader;
> 
> +  RETURN_STATUS     Status;
> 
> +  UINT32            DbgInstCount;
> 
> +  UINT16            Index;
> 
> +
> 
> +  DbgInstCount = 0;
> 
> +
> 
> +  if (NumberOfBytes == 0) {
> 
> +    //
> 
> +    // No data need to be written to Trace Hub
> 
> +    //
> 
> +    return RETURN_ABORTED;
> 
> +  }
> 
> +
> 
> +  if (Buffer == NULL) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  DbgInstCount = CountThDebugInstance ();
> 
> +
> 
> +  ZeroMem (&MipiSystHandle, sizeof (MIPI_SYST_HANDLE));
> 
> +  MipiSystHandle.systh_header = &MipiSystHeader;
> 
> +
> 
> +  Status = InitMipiSystHandle (&MipiSystHandle);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  for (Index = 0; Index < DbgInstCount; Index++) {
> 
> +    Status = CheckWhetherToOutputMsg (
> 
> +               &MipiSystHandle,
> 
> +               NULL,
> 
> +               (MIPI_SYST_SEVERITY)SeverityType,
> 
> +               TraceHubDebugType
> 
> +               );
> 
> +    if (!RETURN_ERROR (Status)) {
> 
> +      Status = MipiSystWriteDebug (
> 
> +                 &MipiSystHandle,
> 
> +                 (MIPI_SYST_SEVERITY)SeverityType,
> 
> +                 (UINT16)NumberOfBytes,
> 
> +                 (CHAR8 *)Buffer
> 
> +                 );
> 
> +      if (RETURN_ERROR (Status)) {
> 
> +        break;
> 
> +      }
> 
> +    }
> 
> +  }
> 
> +
> 
> +  return Status;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Write catalog status code message to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType     Severity type of input message.
> 
> +  @param[in]  Id               Catalog ID.
> 
> +  @param[in]  Guid             Driver Guid.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTWriteCataLog64StatusCode (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT64                   Id,
> 
> +  IN GUID                     *Guid
> 
> +  )
> 
> +{
> 
> +  MIPI_SYST_HANDLE  MipiSystHandle;
> 
> +  MIPI_SYST_HEADER  MipiSystHeader;
> 
> +  RETURN_STATUS     Status;
> 
> +  UINT32            DbgInstCount;
> 
> +  UINT16            Index;
> 
> +  GUID              ConvertedGuid;
> 
> +
> 
> +  DbgInstCount = 0;
> 
> +
> 
> +  if (Guid == NULL) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  DbgInstCount = CountThDebugInstance ();
> 
> +
> 
> +  MipiSystHandle.systh_header = &MipiSystHeader;
> 
> +
> 
> +  ZeroMem (&MipiSystHandle, sizeof (MIPI_SYST_HANDLE));
> 
> +  Status = InitMipiSystHandle (&MipiSystHandle);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  ConvertedGuid = SwapBytesGuid (Guid);
> 
> +  CopyMem (&MipiSystHandle.systh_guid, &ConvertedGuid, sizeof (GUID));
> 
> +  MipiSystHandle.systh_tag.et_guid = 1;
> 
> +
> 
> +  for (Index = 0; Index < DbgInstCount; Index++) {
> 
> +    Status = CheckWhetherToOutputMsg (
> 
> +               &MipiSystHandle,
> 
> +               NULL,
> 
> +               (MIPI_SYST_SEVERITY)SeverityType,
> 
> +               TraceHubCatalogType
> 
> +               );
> 
> +    if (!RETURN_ERROR (Status)) {
> 
> +      Status = MipiSystWriteCatalog (
> 
> +                 &MipiSystHandle,
> 
> +                 (MIPI_SYST_SEVERITY)SeverityType,
> 
> +                 Id
> 
> +                 );
> 
> +      if (RETURN_ERROR (Status)) {
> 
> +        break;
> 
> +      }
> 
> +    }
> 
> +  }
> 
> +
> 
> +  return Status;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Write catalog message to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType   Severity type of input message.
> 
> +  @param[in]  Id             Catalog ID.
> 
> +  @param[in]  NumberOfParams Number of entries in argument list.
> 
> +  @param[in]  ...            Catalog message parameters.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTWriteCataLog64 (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT64                   Id,
> 
> +  IN UINTN                    NumberOfParams,
> 
> +  ...
> 
> +  )
> 
> +{
> 
> +  MIPI_SYST_HANDLE  MipiSystHandle;
> 
> +  MIPI_SYST_HEADER  MipiSystHeader;
> 
> +  VA_LIST           Args;
> 
> +  UINTN             Index;
> 
> +  RETURN_STATUS     Status;
> 
> +  UINT32            DbgInstCount;
> 
> +
> 
> +  DbgInstCount = 0;
> 
> +
> 
> +  if (NumberOfParams > sizeof (MipiSystHandle.systh_param) / sizeof
> (UINT32)) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  DbgInstCount = CountThDebugInstance ();
> 
> +
> 
> +  MipiSystHandle.systh_header = &MipiSystHeader;
> 
> +
> 
> +  ZeroMem (&MipiSystHandle, sizeof (MIPI_SYST_HANDLE));
> 
> +  Status = InitMipiSystHandle (&MipiSystHandle);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  MipiSystHandle.systh_param_count = (UINT32)NumberOfParams;
> 
> +  VA_START (Args, NumberOfParams);
> 
> +  for (Index = 0; Index < NumberOfParams; Index++) {
> 
> +    MipiSystHandle.systh_param[Index] = VA_ARG (Args, UINT32);
> 
> +  }
> 
> +
> 
> +  VA_END (Args);
> 
> +
> 
> +  for (Index = 0; Index < DbgInstCount; Index++) {
> 
> +    Status = CheckWhetherToOutputMsg (
> 
> +               &MipiSystHandle,
> 
> +               NULL,
> 
> +               (MIPI_SYST_SEVERITY)SeverityType,
> 
> +               TraceHubCatalogType
> 
> +               );
> 
> +    if (!RETURN_ERROR (Status)) {
> 
> +      Status = MipiSystWriteCatalog (
> 
> +                 &MipiSystHandle,
> 
> +                 (MIPI_SYST_SEVERITY)SeverityType,
> 
> +                 Id
> 
> +                 );
> 
> +      if (RETURN_ERROR (Status)) {
> 
> +        break;
> 
> +      }
> 
> +    }
> 
> +  }
> 
> +
> 
> +  return Status;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Collect the total number of Trace Hub debug instance in the system.
> 
> +
> 
> +  @retval UINT32      The total number of Trace Hub debug instance in the
> system.
> 
> +**/
> 
> +UINT32
> 
> +EFIAPI
> 
> +CountThDebugInstance (
> 
> +  VOID
> 
> +  )
> 
> +{
> 
> +  UINT32  DbgInstCount;
> 
> +
> 
> +  //
> 
> +  // 1 from PCD.
> 
> +  //
> 
> +  DbgInstCount = 1;
> 
> +
> 
> +  return DbgInstCount;
> 
> +}
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/BaseTraceHubDebugSysTLib
> .inf
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/BaseTraceHubDebugSysTLib
> .inf
> new file mode 100644
> index 0000000000..3edc4e8fd8
> --- /dev/null
> +++
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/BaseTraceHubDebugSysTLib
> .inf
> @@ -0,0 +1,44 @@
> +## @file
> 
> +#  Debug library to output Trace Hub message.
> 
> +#  Support SEC/PEI/DXE/SMM phase TraceHub debug message based on
> fixed settings.
> 
> +#
> 
> +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +#
> 
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +#
> 
> +##
> 
> +
> 
> +[Defines]
> 
> +  INF_VERSION                    = 0x00010005
> 
> +  BASE_NAME                      = BaseTraceHubDebugSysTLib
> 
> +  FILE_GUID                      = 336DA571-AD65-423C-9A43-E0056E5B2D8D
> 
> +  MODULE_TYPE                    = BASE
> 
> +  VERSION_STRING                 = 1.0
> 
> +  LIBRARY_CLASS                  = TraceHubDebugSysTLib
> 
> +
> 
> +#
> 
> +# The following information is for reference only and not required by the
> build tools.
> 
> +#
> 
> +# VALID_ARCHITECTURES = IA32 X64
> 
> +#
> 
> +
> 
> +[LibraryClasses]
> 
> +  BaseLib
> 
> +  PcdLib
> 
> +  BaseMemoryLib
> 
> +  MipiSysTLib
> 
> +
> 
> +[Packages]
> 
> +  MdePkg/MdePkg.dec
> 
> +  MdeModulePkg/MdeModulePkg.dec
> 
> +
> 
> +[Sources]
> 
> +  BaseTraceHubDebugSysTLib.c
> 
> +  InternalTraceHubApiCommon.c
> 
> +  InternalTraceHubApiCommon.h
> 
> +  InternalTraceHubApi.h
> 
> +
> 
> +[Pcd]
> 
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdTraceHubDebugLevel
> 
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdEnableTraceHubDebugMsg
> 
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdTraceHubDebugMmioAddress
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/DxeSmmTraceHubDebugSys
> TLib.c
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/DxeSmmTraceHubDebugSys
> TLib.c
> new file mode 100644
> index 0000000000..626e86c880
> --- /dev/null
> +++
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/DxeSmmTraceHubDebugSys
> TLib.c
> @@ -0,0 +1,247 @@
> +/** @file
> 
> +System prints Trace Hub message in DXE/SMM based on fixed PCDs and HOB.
> 
> +Support at most MAX_TRACE_HUB_DEBUG_INSTANCE of Trace Hub debug
> instances
> 
> +in the system.
> 
> +Trace Hub PCDs will be applied if no HOB exist.
> 
> +
> 
> +Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +
> 
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +
> 
> +**/
> 
> +
> 
> +#include <Base.h>
> 
> +#include <Library/BaseLib.h>
> 
> +#include <Library/PcdLib.h>
> 
> +#include <Library/HobLib.h>
> 
> +#include <Library/BaseMemoryLib.h>
> 
> +#include <Library/MemoryAllocationLib.h>
> 
> +#include <Library/TraceHubDebugSysTLib.h>
> 
> +#include <Library/MipiSysTLib.h>
> 
> +#include <Library/MipiSysTLib/mipi_syst.h>
> 
> +#include <Guid/TraceHubDebugInfoHob.h>
> 
> +#include "InternalTraceHubApiCommon.h"
> 
> +#include "InternalTraceHubApi.h"
> 
> +
> 
> +GLOBAL_REMOVE_IF_UNREFERENCED TRACEHUB_DEBUG_INFO_HOB
> mThDebugInstArray[MAX_TRACE_HUB_DEBUG_INSTANCE];
> 
> +GLOBAL_REMOVE_IF_UNREFERENCED UINT32                   mDbgInstCount = 0;
> 
> +
> 
> +/**
> 
> +  Write debug string to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType     Severity type of input message.
> 
> +  @param[in]  Buffer           A pointer to the data buffer.
> 
> +  @param[in]  NumberOfBytes    The size of data buffer.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTDebugWrite (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT8                    *Buffer,
> 
> +  IN UINTN                    NumberOfBytes
> 
> +  )
> 
> +{
> 
> +  MIPI_SYST_HANDLE  MipiSystHandle;
> 
> +  MIPI_SYST_HEADER  MipiSystHeader;
> 
> +  RETURN_STATUS     Status;
> 
> +  UINT16            Index;
> 
> +
> 
> +  if (NumberOfBytes == 0) {
> 
> +    //
> 
> +    // No data need to be written to Trace Hub
> 
> +    //
> 
> +    return RETURN_ABORTED;
> 
> +  }
> 
> +
> 
> +  if (Buffer == NULL) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  if (mDbgInstCount == 0) {
> 
> +    mDbgInstCount = CountThDebugInstance ();
> 
> +  }
> 
> +
> 
> +  if (mThDebugInstArray[0].TraceHubMmioAddress == 0) {
> 
> +    PackThDebugInstance (&mThDebugInstArray[0], mDbgInstCount);
> 
> +  }
> 
> +
> 
> +  ZeroMem (&MipiSystHandle, sizeof (MIPI_SYST_HANDLE));
> 
> +  MipiSystHandle.systh_header = &MipiSystHeader;
> 
> +
> 
> +  Status = InitMipiSystHandle (&MipiSystHandle);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  for (Index = 0; Index < mDbgInstCount; Index++) {
> 
> +    Status = CheckWhetherToOutputMsg (
> 
> +               &MipiSystHandle,
> 
> +               (UINT8 *)&mThDebugInstArray[Index],
> 
> +               (MIPI_SYST_SEVERITY)SeverityType,
> 
> +               TraceHubDebugType
> 
> +               );
> 
> +    if (!RETURN_ERROR (Status)) {
> 
> +      Status = MipiSystWriteDebug (
> 
> +                 &MipiSystHandle,
> 
> +                 (MIPI_SYST_SEVERITY)SeverityType,
> 
> +                 (UINT16)NumberOfBytes,
> 
> +                 (CHAR8 *)Buffer
> 
> +                 );
> 
> +      if (RETURN_ERROR (Status)) {
> 
> +        break;
> 
> +      }
> 
> +    }
> 
> +  }
> 
> +
> 
> +  return Status;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Write catalog status code message to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType     Severity type of input message.
> 
> +  @param[in]  Id               Catalog ID.
> 
> +  @param[in]  Guid             Driver Guid.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTWriteCataLog64StatusCode (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT64                   Id,
> 
> +  IN GUID                     *Guid
> 
> +  )
> 
> +{
> 
> +  MIPI_SYST_HANDLE  MipiSystHandle;
> 
> +  MIPI_SYST_HEADER  MipiSystHeader;
> 
> +  UINTN             Index;
> 
> +  RETURN_STATUS     Status;
> 
> +  GUID              ConvertedGuid;
> 
> +
> 
> +  if (mDbgInstCount == 0) {
> 
> +    mDbgInstCount = CountThDebugInstance ();
> 
> +  }
> 
> +
> 
> +  if (mThDebugInstArray[0].TraceHubMmioAddress == 0) {
> 
> +    PackThDebugInstance (&mThDebugInstArray[0], mDbgInstCount);
> 
> +  }
> 
> +
> 
> +  MipiSystHandle.systh_header = &MipiSystHeader;
> 
> +
> 
> +  ZeroMem (&MipiSystHandle, sizeof (MIPI_SYST_HANDLE));
> 
> +  Status = InitMipiSystHandle (&MipiSystHandle);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  if (Guid != NULL) {
> 
> +    ConvertedGuid = SwapBytesGuid (Guid);
> 
> +    CopyMem (&MipiSystHandle.systh_guid, &ConvertedGuid, sizeof (GUID));
> 
> +    MipiSystHandle.systh_tag.et_guid = 1;
> 
> +  } else {
> 
> +    MipiSystHandle.systh_tag.et_modunit = 2;
> 
> +    MipiSystHandle.systh_tag.et_guid    = 0;
> 
> +  }
> 
> +
> 
> +  for (Index = 0; Index < mDbgInstCount; Index++) {
> 
> +    Status = CheckWhetherToOutputMsg (
> 
> +               &MipiSystHandle,
> 
> +               (UINT8 *)&mThDebugInstArray[Index],
> 
> +               (MIPI_SYST_SEVERITY)SeverityType,
> 
> +               TraceHubCatalogType
> 
> +               );
> 
> +    if (!RETURN_ERROR (Status)) {
> 
> +      Status = MipiSystWriteCatalog (
> 
> +                 &MipiSystHandle,
> 
> +                 (MIPI_SYST_SEVERITY)SeverityType,
> 
> +                 Id
> 
> +                 );
> 
> +      if (RETURN_ERROR (Status)) {
> 
> +        break;
> 
> +      }
> 
> +    }
> 
> +  }
> 
> +
> 
> +  return Status;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Write catalog message to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType   Severity type of input message.
> 
> +  @param[in]  Id             Catalog ID.
> 
> +  @param[in]  NumberOfParams Number of entries in argument list.
> 
> +  @param[in]  ...            Catalog message parameters.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTWriteCataLog64 (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT64                   Id,
> 
> +  IN UINTN                    NumberOfParams,
> 
> +  ...
> 
> +  )
> 
> +{
> 
> +  MIPI_SYST_HANDLE  MipiSystHandle;
> 
> +  MIPI_SYST_HEADER  MipiSystHeader;
> 
> +  VA_LIST           Args;
> 
> +  UINTN             Index;
> 
> +  RETURN_STATUS     Status;
> 
> +
> 
> +  if (NumberOfParams > sizeof (MipiSystHandle.systh_param) / sizeof
> (UINT32)) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  if (mDbgInstCount == 0) {
> 
> +    mDbgInstCount = CountThDebugInstance ();
> 
> +  }
> 
> +
> 
> +  if (mThDebugInstArray[0].TraceHubMmioAddress == 0) {
> 
> +    PackThDebugInstance (&mThDebugInstArray[0], mDbgInstCount);
> 
> +  }
> 
> +
> 
> +  MipiSystHandle.systh_header = &MipiSystHeader;
> 
> +
> 
> +  ZeroMem (&MipiSystHandle, sizeof (MIPI_SYST_HANDLE));
> 
> +  Status = InitMipiSystHandle (&MipiSystHandle);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  MipiSystHandle.systh_param_count = (UINT32)NumberOfParams;
> 
> +  VA_START (Args, NumberOfParams);
> 
> +  for (Index = 0; Index < NumberOfParams; Index++) {
> 
> +    MipiSystHandle.systh_param[Index] = VA_ARG (Args, UINT32);
> 
> +  }
> 
> +
> 
> +  VA_END (Args);
> 
> +
> 
> +  for (Index = 0; Index < mDbgInstCount; Index++) {
> 
> +    Status = CheckWhetherToOutputMsg (
> 
> +               &MipiSystHandle,
> 
> +               (UINT8 *)&mThDebugInstArray[Index],
> 
> +               (MIPI_SYST_SEVERITY)SeverityType,
> 
> +               TraceHubCatalogType
> 
> +               );
> 
> +    if (!RETURN_ERROR (Status)) {
> 
> +      Status = MipiSystWriteCatalog (
> 
> +                 &MipiSystHandle,
> 
> +                 (MIPI_SYST_SEVERITY)SeverityType,
> 
> +                 Id
> 
> +                 );
> 
> +      if (RETURN_ERROR (Status)) {
> 
> +        break;
> 
> +      }
> 
> +    }
> 
> +  }
> 
> +
> 
> +  return Status;
> 
> +}
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/DxeSmmTraceHubDebugSys
> TLib.inf
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/DxeSmmTraceHubDebugSys
> TLib.inf
> new file mode 100644
> index 0000000000..9ba1b9c42f
> --- /dev/null
> +++
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/DxeSmmTraceHubDebugSys
> TLib.inf
> @@ -0,0 +1,50 @@
> +## @file
> 
> +#  Debug library to output Trace Hub message.
> 
> +#  Support DXE/SMM phase TraceHub debug message based on fixed or
> dynamic settings.
> 
> +#
> 
> +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +#
> 
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +#
> 
> +##
> 
> +
> 
> +[Defines]
> 
> +  INF_VERSION                    = 0x00010005
> 
> +  BASE_NAME                      = DxeSmmTraceHubDebugSysTLib
> 
> +  FILE_GUID                      = A9B7B825-7902-4616-8556-085DA4DFEC72
> 
> +  MODULE_TYPE                    = DXE_DRIVER
> 
> +  VERSION_STRING                 = 1.0
> 
> +  LIBRARY_CLASS                  = TraceHubDebugSysTLib|DXE_CORE DXE_DRIVER
> SMM_CORE DXE_SMM_DRIVER UEFI_DRIVER UEFI_APPLICATION
> 
> +
> 
> +#
> 
> +# The following information is for reference only and not required by the
> build tools.
> 
> +#
> 
> +# VALID_ARCHITECTURES = IA32 X64
> 
> +#
> 
> +
> 
> +[LibraryClasses]
> 
> +  BaseLib
> 
> +  PcdLib
> 
> +  HobLib
> 
> +  BaseMemoryLib
> 
> +  MemoryAllocationLib
> 
> +  MipiSysTLib
> 
> +
> 
> +[Packages]
> 
> +  MdePkg/MdePkg.dec
> 
> +  MdeModulePkg/MdeModulePkg.dec
> 
> +
> 
> +[Sources]
> 
> +  DxeSmmTraceHubDebugSysTLib.c
> 
> +  InternalTraceHubApiCommon.c
> 
> +  InternalTraceHubApiCommon.h
> 
> +  InternalTraceHubApi.h
> 
> +  InternalTraceHubApi.c
> 
> +
> 
> +[Guids]
> 
> +  gTraceHubDebugInfoHobGuid
> 
> +
> 
> +[Pcd]
> 
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdTraceHubDebugLevel
> 
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdEnableTraceHubDebugMsg
> 
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdTraceHubDebugMmioAddress
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApi.c
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApi.c
> new file mode 100644
> index 0000000000..4f2dd0f2e5
> --- /dev/null
> +++ b/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApi.c
> @@ -0,0 +1,82 @@
> +/** @file
> 
> +Functions implementation in this file are not common for all type of
> TraceHubDebugSysTLib.
> 
> +
> 
> +Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +
> 
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +
> 
> +**/
> 
> +
> 
> +#include <Base.h>
> 
> +#include <Guid/TraceHubDebugInfoHob.h>
> 
> +#include <Library/BaseMemoryLib.h>
> 
> +#include <Library/HobLib.h>
> 
> +#include "InternalTraceHubApi.h"
> 
> +
> 
> +/**
> 
> +  Count the total number of Trace Hub debug instance in the system.
> 
> +
> 
> +  @retval UINT32      The total number of Trace Hub debug instance in the
> system.
> 
> +**/
> 
> +UINT32
> 
> +EFIAPI
> 
> +CountThDebugInstance (
> 
> +  VOID
> 
> +  )
> 
> +{
> 
> +  UINT8   *DgbContext;
> 
> +  UINT32  DbgInstCount;
> 
> +
> 
> +  DbgInstCount = 0;
> 
> +
> 
> +  DgbContext = (UINT8 *)GetFirstGuidHob (&gTraceHubDebugInfoHobGuid);
> 
> +  if (DgbContext != NULL) {
> 
> +    while (DgbContext != NULL) {
> 
> +      DbgInstCount++;
> 
> +      DgbContext = (UINT8 *)GetNextGuidHob
> (&gTraceHubDebugInfoHobGuid, GET_NEXT_HOB (DgbContext));
> 
> +    }
> 
> +  } else {
> 
> +    DbgInstCount++;
> 
> +  }
> 
> +
> 
> +  //
> 
> +  // Trace Hub HOB larger than MAX_TRACE_HUB_DEBUG_INSTANCE won't
> be processed.
> 
> +  //
> 
> +  if (DbgInstCount > MAX_TRACE_HUB_DEBUG_INSTANCE) {
> 
> +    DbgInstCount = MAX_TRACE_HUB_DEBUG_INSTANCE;
> 
> +  }
> 
> +
> 
> +  return DbgInstCount;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Pack Trace Hub debug instances in the system.
> 
> +
> 
> +  @param[in, out]  ThPtr     A pointer to TRACEHUB_DEBUG_INFO_HOB
> structure.
> 
> +  @param[in]       Count     Number of Trace Hub HOBs.
> 
> +**/
> 
> +VOID
> 
> +EFIAPI
> 
> +PackThDebugInstance (
> 
> +  IN OUT TRACEHUB_DEBUG_INFO_HOB  *ThPtr,
> 
> +  IN     UINT32                   Count
> 
> +  )
> 
> +{
> 
> +  UINT8   *DgbContext;
> 
> +  UINT16  Index;
> 
> +
> 
> +  ZeroMem (ThPtr, sizeof (TRACEHUB_DEBUG_INFO_HOB) *
> MAX_TRACE_HUB_DEBUG_INSTANCE);
> 
> +  DgbContext = GetFirstGuidHob (&gTraceHubDebugInfoHobGuid);
> 
> +  if (DgbContext != NULL) {
> 
> +    for (Index = 0; Index < Count; Index++) {
> 
> +      CopyMem (&ThPtr[Index], GET_GUID_HOB_DATA (DgbContext), sizeof
> (TRACEHUB_DEBUG_INFO_HOB));
> 
> +      DgbContext = GetNextGuidHob (&gTraceHubDebugInfoHobGuid,
> GET_NEXT_HOB (DgbContext));
> 
> +    }
> 
> +  } else {
> 
> +    for (Index = 0; Index < Count; Index++) {
> 
> +      ThPtr[Index].TraceHubMmioAddress = FixedPcdGet64
> (PcdTraceHubDebugMmioAddress);
> 
> +      ThPtr[Index].Flag                = FixedPcdGetBool
> (PcdEnableTraceHubDebugMsg);
> 
> +      ThPtr[Index].DebugLevel          = FixedPcdGet8 (PcdTraceHubDebugLevel);
> 
> +    }
> 
> +  }
> 
> +}
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApi.h
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApi.h
> new file mode 100644
> index 0000000000..a185c17d8c
> --- /dev/null
> +++ b/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApi.h
> @@ -0,0 +1,46 @@
> +/** @file
> 
> +This header file declares functions that are not for common use.
> 
> +
> 
> +Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +
> 
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +
> 
> +**/
> 
> +
> 
> +#ifndef INTERNAL_TRACE_HUB_API_H_
> 
> +#define INTERNAL_TRACE_HUB_API_H_
> 
> +
> 
> +/**
> 
> +  Count the total number of Trace Hub debug instance in the system.
> 
> +
> 
> +  @retval UINT32      The total number of Trace Hub debug instance in the
> system.
> 
> +**/
> 
> +UINT32
> 
> +EFIAPI
> 
> +CountThDebugInstance (
> 
> +  VOID
> 
> +  );
> 
> +
> 
> +/**
> 
> +  Pack Trace Hub debug instances in the system.
> 
> +
> 
> +  @param[in, out]  ThPtr     A pointer to TRACEHUB_DEBUG_INFO_HOB
> structure.
> 
> +  @param[in]       Count     Number of Trace Hub HOBs.
> 
> +**/
> 
> +VOID
> 
> +EFIAPI
> 
> +PackThDebugInstance (
> 
> +  IN OUT TRACEHUB_DEBUG_INFO_HOB  *ThPtr,
> 
> +  IN     UINT32                   Count
> 
> +  );
> 
> +
> 
> +/**
> 
> +  Convert physical address to virtual address for the pointer to
> TRACEHUB_DEBUG_INFO_HOB structure.
> 
> +**/
> 
> +VOID
> 
> +EFIAPI
> 
> +InternalCreateThExitBootSvcEvent (
> 
> +  VOID
> 
> +  );
> 
> +
> 
> +#endif // INTERNAL_TRACE_HUB_API_H_
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApiComm
> on.c
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApiComm
> on.c
> new file mode 100644
> index 0000000000..f32eb43192
> --- /dev/null
> +++
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApiComm
> on.c
> @@ -0,0 +1,208 @@
> +/** @file
> 
> +Functions implementation defined in this file are common for all type of
> TraceHubDebugSysTLib
> 
> +
> 
> +Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +
> 
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +
> 
> +**/
> 
> +
> 
> +#include <Base.h>
> 
> +#include <Library/BaseLib.h>
> 
> +#include <Library/BaseMemoryLib.h>
> 
> +#include <Library/TraceHubDebugSysTLib.h>
> 
> +#include <Library/MipiSysTLib/mipi_syst.h>
> 
> +#include <Guid/TraceHubDebugInfoHob.h>
> 
> +#include "InternalTraceHubApiCommon.h"
> 
> +#include "InternalTraceHubApi.h"
> 
> +
> 
> +/**
> 
> +  Conditionally determine whether to enable Trace Hub message.
> 
> +
> 
> +  @param[in]  Flag            Flag to enable or disable Trace Hub message.
> 
> +  @param[in]  DbgLevel        Debug Level of Trace Hub.
> 
> +  @param[in]  SeverityType    Severity type of input message.
> 
> +
> 
> +  @retval TRUE            Enable trace hub message.
> 
> +  @retval FALSE           Disable trace hub message.
> 
> +**/
> 
> +BOOLEAN
> 
> +EFIAPI
> 
> +TraceHubDataEnabled (
> 
> +  IN BOOLEAN                  Flag,
> 
> +  IN UINT8                    DbgLevel,
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType
> 
> +  )
> 
> +{
> 
> +  if (Flag == TraceHubRoutingDisable) {
> 
> +    return FALSE;
> 
> +  }
> 
> +
> 
> +  if (DbgLevel == TraceHubDebugLevelError) {
> 
> +    if (((SeverityType == SeverityFatal) || (SeverityType == SeverityError))) {
> 
> +      return TRUE;
> 
> +    }
> 
> +  } else if (DbgLevel == TraceHubDebugLevelErrorWarning) {
> 
> +    if (((SeverityType == SeverityFatal) || (SeverityType == SeverityError) ||
> (SeverityType == SeverityWarning))) {
> 
> +      return TRUE;
> 
> +    }
> 
> +  } else if (DbgLevel == TraceHubDebugLevelErrorWarningInfo) {
> 
> +    if (((SeverityType == SeverityFatal) || (SeverityType == SeverityError) ||
> (SeverityType == SeverityWarning) || (SeverityType == SeverityNormal))) {
> 
> +      return TRUE;
> 
> +    }
> 
> +  } else if (DbgLevel == TraceHubDebugLevelErrorWarningInfoVerbose) {
> 
> +    return TRUE;
> 
> +  }
> 
> +
> 
> +  return FALSE;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Convert GUID from LE to BE or BE to LE.
> 
> +
> 
> +  @param[in]  Guid   GUID to be converted.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Operation is successful.
> 
> +**/
> 
> +GUID
> 
> +EFIAPI
> 
> +SwapBytesGuid (
> 
> +  IN GUID  *Guid
> 
> +  )
> 
> +{
> 
> +  GUID    ConvertedGuid;
> 
> +  UINT64  GuidData4;
> 
> +
> 
> +  ZeroMem (&ConvertedGuid, sizeof (GUID));
> 
> +  ConvertedGuid.Data1 = SwapBytes32 (Guid->Data1);
> 
> +  ConvertedGuid.Data2 = SwapBytes16 (Guid->Data2);
> 
> +  ConvertedGuid.Data3 = SwapBytes16 (Guid->Data3);
> 
> +  CopyMem (&GuidData4, Guid->Data4, sizeof (Guid->Data4));
> 
> +  GuidData4 = SwapBytes64 (GuidData4);
> 
> +  CopyMem (ConvertedGuid.Data4, &GuidData4, sizeof (GuidData4));
> 
> +
> 
> +  return ConvertedGuid;
> 
> +}

I do not think the algorithm in this API is correct.  I used python uuid module to generate a GUID and dump it as little endian and big endian.  The UINT32 and UINT16 fields are swapped, but the 8 byte array GuideData4 is left alone.

>>> [int(x) for x in a.bytes]
[157, 128, 47, 218, 201, 182, 17, 237, 164, 194, 84, 225, 173, 59, 241, 52]
>>> [int(x) for x in a.bytes_le]
[218, 47, 128, 157, 182, 201, 237, 17, 164, 194, 84, 225, 173, 59, 241, 52]

I think the following should be the simplified and correct version of this API.

RETURN_STATUS
EFIAPI
SwapBytesGuid (
  IN OUT GUID  *Guid
  )
{
  Guid->Data1 = SwapBytes32 (Guid->Data1);
  Guid->Data2 = SwapBytes16 (Guid->Data2);
  Guid->Data3 = SwapBytes16 (Guid->Data3);
  return RETURN_SUCCESS;
}


> 
> +
> 
> +/**
> 
> +  Check whether to output Trace Hub message according to some conditions.
> 
> +  Trace Hub message will be disabled if TraceHubDataEnabled() return FALSE
> 
> +  or Trace Hub MMIO address is 0.
> 
> +
> 
> +  @param[in, out]  MipiSystHandle   A pointer to MIPI_SYST_HANDLE
> structure.
> 
> +  @param[in]       DgbContext       A pointer to Trace Hub debug instance.
> 
> +  @param[in]       SeverityType     Severity type of input message.
> 
> +  @param[in]       PrintType        Either catalog print or debug print.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Current Trace Hub message need to be
> output.
> 
> +  @retval Other               Current Trace Hub message will be disabled.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +CheckWhetherToOutputMsg (
> 
> +  IN OUT MIPI_SYST_HANDLE         *MipiSystHandle,
> 
> +  IN     UINT8                    *DgbContext,
> 
> +  IN     TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN     TRACEHUB_PRINTTYPE       PrintType
> 
> +  )
> 
> +{
> 
> +  UINT8          DbgLevel;
> 
> +  BOOLEAN        Flag;
> 
> +  UINT64         Addr;
> 
> +  RETURN_STATUS  Status;
> 
> +
> 
> +  if (MipiSystHandle == NULL) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  if (PrintType == TraceHubDebugType) {
> 
> +    Status = GetTraceHubMsgVisibility (DgbContext, &Flag, &DbgLevel);
> 
> +    if (RETURN_ERROR (Status)) {
> 
> +      return Status;
> 
> +    }
> 
> +
> 
> +    if (!TraceHubDataEnabled (Flag, DbgLevel, SeverityType)) {
> 
> +      return RETURN_ABORTED;
> 
> +    }
> 
> +  }
> 
> +
> 
> +  Status = GetTraceHubMmioAddress (DgbContext, &Addr);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  MipiSystHandle->systh_platform.TraceHubPlatformData.MmioAddr = Addr;
> 
> +  if (MipiSystHandle->systh_platform.TraceHubPlatformData.MmioAddr == 0)
> {
> 
> +    return RETURN_ABORTED;
> 
> +  }
> 
> +
> 
> +  return RETURN_SUCCESS;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Get Trace Hub MMIO Address.
> 
> +
> 
> +  @param[in]      DgbContext        A pointer to Trace Hub debug instance.
> 
> +  @param[in, out] TraceAddress      Trace Hub MMIO Address.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Operation is successfully.
> 
> +  @retval Other               Operation is failed.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +GetTraceHubMmioAddress (
> 
> +  IN     UINT8   *DgbContext,
> 
> +  IN OUT UINT64  *TraceAddress
> 
> +  )
> 
> +{
> 
> +  TRACEHUB_DEBUG_INFO_HOB  *ThDbgContext;
> 
> +
> 
> +  if (TraceAddress == NULL) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  if (DgbContext != NULL) {
> 
> +    ThDbgContext  = (TRACEHUB_DEBUG_INFO_HOB *)DgbContext;
> 
> +    *TraceAddress = ThDbgContext->TraceHubMmioAddress;
> 
> +  } else {
> 
> +    *TraceAddress = FixedPcdGet64 (PcdTraceHubDebugMmioAddress);
> 
> +  }
> 
> +
> 
> +  return RETURN_SUCCESS;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Get visibility of Trace Hub Msg.
> 
> +
> 
> +  @param[in]      DgbContext      A pointer to Trace Hub debug instance.
> 
> +  @param[in, out] Flag            Flag to enable or disable Trace Hub message.
> 
> +  @param[in, out] DbgLevel        Debug Level of Trace Hub.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Operation is successfully.
> 
> +  @retval Other               Operation is failed.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +GetTraceHubMsgVisibility (
> 
> +  IN     UINT8    *DgbContext,
> 
> +  IN OUT BOOLEAN  *Flag,
> 
> +  IN OUT UINT8    *DbgLevel
> 
> +  )
> 
> +{
> 
> +  TRACEHUB_DEBUG_INFO_HOB  *ThDbgContext;
> 
> +
> 
> +  if ((Flag == NULL) || (DbgLevel == NULL)) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  if (DgbContext != NULL) {
> 
> +    ThDbgContext = (TRACEHUB_DEBUG_INFO_HOB *)DgbContext;
> 
> +    *Flag        = ThDbgContext->Flag;
> 
> +    *DbgLevel    = ThDbgContext->DebugLevel;
> 
> +  } else {
> 
> +    *DbgLevel = FixedPcdGet8 (PcdTraceHubDebugLevel);
> 
> +    *Flag     = FixedPcdGetBool (PcdEnableTraceHubDebugMsg);
> 
> +  }
> 
> +
> 
> +  return RETURN_SUCCESS;
> 
> +}
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApiComm
> on.h
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApiComm
> on.h
> new file mode 100644
> index 0000000000..771c4400af
> --- /dev/null
> +++
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/InternalTraceHubApiComm
> on.h
> @@ -0,0 +1,119 @@
> +/** @file
> 
> +This header file declares functions and type for common use.
> 
> +
> 
> +Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +
> 
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +
> 
> +**/
> 
> +
> 
> +#ifndef INTERNAL_TRACE_HUB_API_COMMON_H_
> 
> +#define INTERNAL_TRACE_HUB_API_COMMON_H_
> 
> +
> 
> +typedef enum {
> 
> +  TraceHubDebugType = 0,
> 
> +  TraceHubCatalogType
> 
> +} TRACEHUB_PRINTTYPE;
> 
> +
> 
> +typedef enum {
> 
> +  TraceHubRoutingDisable = 0,
> 
> +  TraceHubRoutingEnable,
> 
> +  TraceHubRoutingMax
> 
> +} TRACE_HUB_ROUTING;
> 
> +
> 
> +typedef enum {
> 
> +  TraceHubDebugLevelError = 0,
> 
> +  TraceHubDebugLevelErrorWarning,
> 
> +  TraceHubDebugLevelErrorWarningInfo,
> 
> +  TraceHubDebugLevelErrorWarningInfoVerbose,
> 
> +  TraceHubDebugLevelMax
> 
> +} TRACE_HUB_DEBUG_LEVEL;
> 
> +
> 
> +/**
> 
> +  Conditionally determine whether to enable Trace Hub message.
> 
> +
> 
> +  @param[in]  Flag            Flag to enable or disable Trace Hub message.
> 
> +  @param[in]  DbgLevel        Debug Level of Trace Hub.
> 
> +  @param[in]  SeverityType    Severity type of input message.
> 
> +
> 
> +  @retval TRUE            Enable trace hub message.
> 
> +  @retval FALSE           Disable trace hub message.
> 
> +**/
> 
> +BOOLEAN
> 
> +EFIAPI
> 
> +TraceHubDataEnabled (
> 
> +  IN BOOLEAN                  Flag,
> 
> +  IN UINT8                    DbgLevel,
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType
> 
> +  );
> 
> +
> 
> +/**
> 
> +  Convert GUID from LE to BE or BE to LE.
> 
> +
> 
> +  @param[in]  Guid   GUID to be converted.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Operation is successful.
> 
> +**/
> 
> +GUID
> 
> +EFIAPI
> 
> +SwapBytesGuid (
> 
> +  IN OUT GUID  *Guid
> 
> +  );
> 
> +
> 
> +/**
> 
> +  Check whether to output Trace Hub message according to some conditions.
> 
> +  Trace Hub message will be disabled if TraceHubDataEnabled() return FALSE
> 
> +  or Trace Hub MMIO address is 0.
> 
> +
> 
> +  @param[in, out]  MipiSystHandle   A pointer to MIPI_SYST_HANDLE
> structure.
> 
> +  @param[in]       DgbContext       A pointer to Trace Hub debug instance.
> 
> +  @param[in]       SeverityType     Severity type of input message.
> 
> +  @param[in]       PrintType        Either catalog print or debug print.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Current Trace Hub message need to be
> output.
> 
> +  @retval Other               Current Trace Hub message will be disabled.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +CheckWhetherToOutputMsg (
> 
> +  IN OUT MIPI_SYST_HANDLE         *MipiSystHandle,
> 
> +  IN     UINT8                    *DgbContext,
> 
> +  IN     TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN     TRACEHUB_PRINTTYPE       PrintType
> 
> +  );
> 
> +
> 
> +/**
> 
> +  Get Trace Hub MMIO Address.
> 
> +
> 
> +  @param[in]      DgbContext        A pointer to Trace Hub debug instance.
> 
> +  @param[in, out] TraceAddress      Trace Hub MMIO Address.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Operation is successfully.
> 
> +  @retval Other               Operation is failed.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +GetTraceHubMmioAddress (
> 
> +  IN     UINT8   *DgbContext,
> 
> +  IN OUT UINT64  *TraceAddress
> 
> +  );
> 
> +
> 
> +/**
> 
> +  Get visibility of Trace Hub Msg.
> 
> +
> 
> +  @param[in]      DgbContext      A pointer to Trace Hub debug instance.
> 
> +  @param[in, out] Flag            Flag to enable or disable Trace Hub message.
> 
> +  @param[in, out] DbgLevel        Debug Level of Trace Hub.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Operation is successfully.
> 
> +  @retval Other               Operation is failed.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +GetTraceHubMsgVisibility (
> 
> +  IN     UINT8    *DgbContext,
> 
> +  IN OUT BOOLEAN  *Flag,
> 
> +  IN OUT UINT8    *DbgLevel
> 
> +  );
> 
> +
> 
> +#endif // INTERNAL_TRACE_HUB_API_COMMON_H_
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/PeiTraceHubDebugSysTLib.c
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/PeiTraceHubDebugSysTLib.c
> new file mode 100644
> index 0000000000..fafd651499
> --- /dev/null
> +++
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/PeiTraceHubDebugSysTLib.c
> @@ -0,0 +1,290 @@
> +/** @file
> 
> +System prints Trace Hub message in PEI based on fixed PCDs and HOB.
> 
> +System applies Trace Hub HOB once it detect gTraceHubDebugInfoHobGuid
> HOB.
> 
> +Support at most MAX_TRACE_HUB_DEBUG_INSTANCE of Trace Hub debug
> instances
> 
> +in the system.
> 
> +Trace Hub PCDs will be applied if no HOB exist.
> 
> +
> 
> +Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +
> 
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +
> 
> +**/
> 
> +
> 
> +#include <Base.h>
> 
> +#include <Library/BaseLib.h>
> 
> +#include <Library/PcdLib.h>
> 
> +#include <Library/HobLib.h>
> 
> +#include <Library/BaseMemoryLib.h>
> 
> +#include <Library/MemoryAllocationLib.h>
> 
> +#include <Library/TraceHubDebugSysTLib.h>
> 
> +#include <Library/MipiSysTLib.h>
> 
> +#include <Library/MipiSysTLib/mipi_syst.h>
> 
> +#include <Guid/TraceHubDebugInfoHob.h>
> 
> +#include "InternalTraceHubApiCommon.h"
> 
> +#include "InternalTraceHubApi.h"
> 
> +
> 
> +/**
> 
> +  Write debug string to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType     Severity type of input message.
> 
> +  @param[in]  Buffer           A pointer to the data buffer.
> 
> +  @param[in]  NumberOfBytes    The size of data buffer.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTDebugWrite (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT8                    *Buffer,
> 
> +  IN UINTN                    NumberOfBytes
> 
> +  )
> 
> +{
> 
> +  MIPI_SYST_HANDLE  MipiSystHandle;
> 
> +  MIPI_SYST_HEADER  MipiSystHeader;
> 
> +  RETURN_STATUS     Status;
> 
> +  UINT8             *DgbContext;
> 
> +  UINTN             Index;
> 
> +  UINT32            DbgInstCount;
> 
> +  UINT8             *ThDebugInfo;
> 
> +
> 
> +  DbgInstCount = 0;
> 
> +
> 
> +  if (NumberOfBytes == 0) {
> 
> +    //
> 
> +    // No data need to be written to Trace Hub
> 
> +    //
> 
> +    return RETURN_ABORTED;
> 
> +  }
> 
> +
> 
> +  if (Buffer == NULL) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  DbgInstCount = CountThDebugInstance ();
> 
> +
> 
> +  ZeroMem (&MipiSystHandle, sizeof (MIPI_SYST_HANDLE));
> 
> +  MipiSystHandle.systh_header = &MipiSystHeader;
> 
> +
> 
> +  Status = InitMipiSystHandle (&MipiSystHandle);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  DgbContext = (UINT8 *)GetFirstGuidHob (&gTraceHubDebugInfoHobGuid);
> 
> +  if (DgbContext != NULL) {
> 
> +    ThDebugInfo = GET_GUID_HOB_DATA (DgbContext);
> 
> +  } else {
> 
> +    ThDebugInfo = NULL;
> 
> +  }
> 
> +
> 
> +  for (Index = 0; Index < DbgInstCount; Index++) {
> 
> +    Status = CheckWhetherToOutputMsg (
> 
> +               &MipiSystHandle,
> 
> +               ThDebugInfo,
> 
> +               (MIPI_SYST_SEVERITY)SeverityType,
> 
> +               TraceHubDebugType
> 
> +               );
> 
> +    if (!RETURN_ERROR (Status)) {
> 
> +      Status = MipiSystWriteDebug (
> 
> +                 &MipiSystHandle,
> 
> +                 (MIPI_SYST_SEVERITY)SeverityType,
> 
> +                 (UINT16)NumberOfBytes,
> 
> +                 (CHAR8 *)Buffer
> 
> +                 );
> 
> +      if (RETURN_ERROR (Status)) {
> 
> +        break;
> 
> +      }
> 
> +    }
> 
> +
> 
> +    if (DgbContext != NULL) {
> 
> +      DgbContext = (UINT8 *)GetNextGuidHob
> (&gTraceHubDebugInfoHobGuid, GET_NEXT_HOB (DgbContext));
> 
> +      if (DgbContext == NULL) {
> 
> +        break;
> 
> +      }
> 
> +
> 
> +      ThDebugInfo = GET_GUID_HOB_DATA (DgbContext);
> 
> +    }
> 
> +  }
> 
> +
> 
> +  return Status;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Write catalog status code message to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType     Severity type of input message.
> 
> +  @param[in]  Id               Catalog ID.
> 
> +  @param[in]  Guid             Driver Guid.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTWriteCataLog64StatusCode (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT64                   Id,
> 
> +  IN GUID                     *Guid
> 
> +  )
> 
> +{
> 
> +  MIPI_SYST_HANDLE  MipiSystHandle;
> 
> +  MIPI_SYST_HEADER  MipiSystHeader;
> 
> +  UINT32            DbgInstCount;
> 
> +  UINT8             *DgbContext;
> 
> +  RETURN_STATUS     Status;
> 
> +  UINTN             Index;
> 
> +  UINT8             *ThDebugInfo;
> 
> +  GUID              ConvertedGuid;
> 
> +
> 
> +  DbgInstCount = 0;
> 
> +
> 
> +  DbgInstCount = CountThDebugInstance ();
> 
> +
> 
> +  MipiSystHandle.systh_header = &MipiSystHeader;
> 
> +
> 
> +  ZeroMem (&MipiSystHandle, sizeof (MIPI_SYST_HANDLE));
> 
> +  Status = InitMipiSystHandle (&MipiSystHandle);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  if (Guid != NULL) {
> 
> +    ConvertedGuid = SwapBytesGuid (Guid);
> 
> +    CopyMem (&MipiSystHandle.systh_guid, &ConvertedGuid, sizeof (GUID));
> 
> +    MipiSystHandle.systh_tag.et_guid = 1;
> 
> +  } else {
> 
> +    MipiSystHandle.systh_tag.et_modunit = 2;
> 
> +    MipiSystHandle.systh_tag.et_guid    = 0;
> 
> +  }
> 
> +
> 
> +  DgbContext = (UINT8 *)GetFirstGuidHob (&gTraceHubDebugInfoHobGuid);
> 
> +  if (DgbContext != NULL) {
> 
> +    ThDebugInfo = GET_GUID_HOB_DATA (DgbContext);
> 
> +  } else {
> 
> +    ThDebugInfo = NULL;
> 
> +  }
> 
> +
> 
> +  for (Index = 0; Index < DbgInstCount; Index++) {
> 
> +    Status = CheckWhetherToOutputMsg (
> 
> +               &MipiSystHandle,
> 
> +               ThDebugInfo,
> 
> +               (MIPI_SYST_SEVERITY)SeverityType,
> 
> +               TraceHubCatalogType
> 
> +               );
> 
> +    if (!RETURN_ERROR (Status)) {
> 
> +      Status = MipiSystWriteCatalog (
> 
> +                 &MipiSystHandle,
> 
> +                 (MIPI_SYST_SEVERITY)SeverityType,
> 
> +                 Id
> 
> +                 );
> 
> +      if (RETURN_ERROR (Status)) {
> 
> +        break;
> 
> +      }
> 
> +    }
> 
> +
> 
> +    if (DgbContext != NULL) {
> 
> +      DgbContext = (UINT8 *)GetNextGuidHob
> (&gTraceHubDebugInfoHobGuid, GET_NEXT_HOB (DgbContext));
> 
> +      if (DgbContext == NULL) {
> 
> +        break;
> 
> +      }
> 
> +
> 
> +      ThDebugInfo = GET_GUID_HOB_DATA (DgbContext);
> 
> +    }
> 
> +  }
> 
> +
> 
> +  return Status;
> 
> +}
> 
> +
> 
> +/**
> 
> +  Write catalog message to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType   Severity type of input message.
> 
> +  @param[in]  Id             Catalog ID.
> 
> +  @param[in]  NumberOfParams Number of entries in argument list.
> 
> +  @param[in]  ...            Catalog message parameters.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTWriteCataLog64 (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT64                   Id,
> 
> +  IN UINTN                    NumberOfParams,
> 
> +  ...
> 
> +  )
> 
> +{
> 
> +  MIPI_SYST_HANDLE  MipiSystHandle;
> 
> +  MIPI_SYST_HEADER  MipiSystHeader;
> 
> +  VA_LIST           Args;
> 
> +  UINTN             Index;
> 
> +  UINT32            DbgInstCount;
> 
> +  UINT8             *DgbContext;
> 
> +  RETURN_STATUS     Status;
> 
> +  UINT8             *ThDebugInfo;
> 
> +
> 
> +  DbgInstCount = 0;
> 
> +
> 
> +  if (NumberOfParams > sizeof (MipiSystHandle.systh_param) / sizeof
> (UINT32)) {
> 
> +    return RETURN_INVALID_PARAMETER;
> 
> +  }
> 
> +
> 
> +  DbgInstCount = CountThDebugInstance ();
> 
> +
> 
> +  MipiSystHandle.systh_header = &MipiSystHeader;
> 
> +
> 
> +  ZeroMem (&MipiSystHandle, sizeof (MIPI_SYST_HANDLE));
> 
> +  Status = InitMipiSystHandle (&MipiSystHandle);
> 
> +  if (RETURN_ERROR (Status)) {
> 
> +    return Status;
> 
> +  }
> 
> +
> 
> +  MipiSystHandle.systh_param_count = (UINT32)NumberOfParams;
> 
> +  VA_START (Args, NumberOfParams);
> 
> +  for (Index = 0; Index < NumberOfParams; Index++) {
> 
> +    MipiSystHandle.systh_param[Index] = VA_ARG (Args, UINT32);
> 
> +  }
> 
> +
> 
> +  VA_END (Args);
> 
> +
> 
> +  DgbContext = (UINT8 *)GetFirstGuidHob (&gTraceHubDebugInfoHobGuid);
> 
> +  if (DgbContext != NULL) {
> 
> +    ThDebugInfo = GET_GUID_HOB_DATA (DgbContext);
> 
> +  } else {
> 
> +    ThDebugInfo = NULL;
> 
> +  }
> 
> +
> 
> +  for (Index = 0; Index < DbgInstCount; Index++) {
> 
> +    Status = CheckWhetherToOutputMsg (
> 
> +               &MipiSystHandle,
> 
> +               ThDebugInfo,
> 
> +               (MIPI_SYST_SEVERITY)SeverityType,
> 
> +               TraceHubCatalogType
> 
> +               );
> 
> +    if (!RETURN_ERROR (Status)) {
> 
> +      Status = MipiSystWriteCatalog (
> 
> +                 &MipiSystHandle,
> 
> +                 (MIPI_SYST_SEVERITY)SeverityType,
> 
> +                 Id
> 
> +                 );
> 
> +      if (RETURN_ERROR (Status)) {
> 
> +        break;
> 
> +      }
> 
> +    }
> 
> +
> 
> +    if (DgbContext != NULL) {
> 
> +      DgbContext = (UINT8 *)GetNextGuidHob
> (&gTraceHubDebugInfoHobGuid, GET_NEXT_HOB (DgbContext));
> 
> +      if (DgbContext == NULL) {
> 
> +        break;
> 
> +      }
> 
> +
> 
> +      ThDebugInfo = GET_GUID_HOB_DATA (DgbContext);
> 
> +    }
> 
> +  }
> 
> +
> 
> +  return Status;
> 
> +}
> 
> diff --git
> a/MdeModulePkg/Library/TraceHubDebugSysTLib/PeiTraceHubDebugSysTLib.i
> nf
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/PeiTraceHubDebugSysTLib.i
> nf
> new file mode 100644
> index 0000000000..2a8184d927
> --- /dev/null
> +++
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/PeiTraceHubDebugSysTLib.i
> nf
> @@ -0,0 +1,50 @@
> +## @file
> 
> +#  Debug library to output Trace Hub message.
> 
> +#  Support PEI phase TraceHub debug message based on fixed or dynamic
> settings.
> 
> +#
> 
> +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +#
> 
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +#
> 
> +##
> 
> +
> 
> +[Defines]
> 
> +  INF_VERSION                    = 0x00010005
> 
> +  BASE_NAME                      = PeiTraceHubDebugSysTLib
> 
> +  FILE_GUID                      = C61E8C2E-0935-4E3D-BCBB-5ED84AFD9FD1
> 
> +  MODULE_TYPE                    = PEIM
> 
> +  VERSION_STRING                 = 1.0
> 
> +  LIBRARY_CLASS                  = TraceHubDebugSysTLib|PEI_CORE PEIM
> 
> +
> 
> +#
> 
> +# The following information is for reference only and not required by the
> build tools.
> 
> +#
> 
> +# VALID_ARCHITECTURES = IA32 X64
> 
> +#
> 
> +
> 
> +[LibraryClasses]
> 
> +  BaseLib
> 
> +  PcdLib
> 
> +  HobLib
> 
> +  BaseMemoryLib
> 
> +  MemoryAllocationLib
> 
> +  MipiSysTLib
> 
> +
> 
> +[Packages]
> 
> +  MdePkg/MdePkg.dec
> 
> +  MdeModulePkg/MdeModulePkg.dec
> 
> +
> 
> +[Sources]
> 
> +  PeiTraceHubDebugSysTLib.c
> 
> +  InternalTraceHubApiCommon.c
> 
> +  InternalTraceHubApiCommon.h
> 
> +  InternalTraceHubApi.h
> 
> +  InternalTraceHubApi.c
> 
> +
> 
> +[Guids]
> 
> +  gTraceHubDebugInfoHobGuid
> 
> +
> 
> +[Pcd]
> 
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdTraceHubDebugLevel
> 
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdEnableTraceHubDebugMsg
> 
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdTraceHubDebugMmioAddress
> 
> diff --git a/MdeModulePkg/Library/TraceHubDebugSysTLib/Readme.md
> b/MdeModulePkg/Library/TraceHubDebugSysTLib/Readme.md
> new file mode 100644
> index 0000000000..24253eb223
> --- /dev/null
> +++ b/MdeModulePkg/Library/TraceHubDebugSysTLib/Readme.md
> @@ -0,0 +1,30 @@
> +## Introduction of TrcaceHubDebugSysTLib ##
> 
> +TrcaceHubDebugSysTLib library is a top level library for dumping Trace Hub
> messages.
> 
> +It provides Trace Hub related APIs to dump Trace Hub message via MIPI SYS-
> T submodule.
> 
> +User need to properly configure following Trace Hub related PCDs and HOB.
> 
> +  (See MdeModulePkg.dec to get detailed definition for PCDs below)
> 
> +  - PcdTraceHubDebugLevel
> 
> +  - PcdEnableTraceHubDebugMsg
> 
> +  - PcdTraceHubDebugMmioAddress
> 
> +  (See TraceHubDebugInfoHob.h to get detailed definition for HOB below)
> 
> +  - gTraceHubDebugInfoHobGuid
> 
> +
> 
> +## BaseTraceHubDebugSysTLib.inf ##
> 
> +System prints Trace Hub message in SEC/PEI/DXE/SMM based on fixed PCDs.
> 
> +Only support single Trace Hub debug instance.
> 
> +
> 
> +## PeiTraceHubDebugSysTLib.inf ##
> 
> +System prints Trace Hub message in PEI based on fixed PCDs and HOB.
> 
> +System applies Trace Hub HOB once it detect gTraceHubDebugInfoHobGuid
> HOB.
> 
> +Support at most MAX_TRACE_HUB_DEBUG_INSTANCE of Trace Hub debug
> instances
> 
> +in the system.
> 
> +Trace Hub PCDs will be applied if no HOB exist.
> 
> +
> 
> +## DxeSmmTraceHubDebugSysTLib.inf ##
> 
> +System prints Trace Hub message in DXE/SMM based on fixed PCDs and HOB.
> 
> +Support at most MAX_TRACE_HUB_DEBUG_INSTANCE of Trace Hub debug
> instances
> 
> +in the system.
> 
> +Trace Hub PCDs will be applied if no HOB exist.
> 
> +
> 
> +## Note ##
> 
> +Trace Hub debug library not support DXE_RUNTIME_DRIVER type of module
> currently.
> 
> diff --git a/MdeModulePkg/MdeModulePkg.dec
> b/MdeModulePkg/MdeModulePkg.dec
> index 3eb4a79bf7..42a1a8e338 100644
> --- a/MdeModulePkg/MdeModulePkg.dec
> +++ b/MdeModulePkg/MdeModulePkg.dec
> @@ -438,6 +438,9 @@
>    ## Include/UniversalPayload/SerialPortInfo.h
> 
>    gUniversalPayloadSerialPortInfoGuid = { 0xaa7e190d, 0xbe21, 0x4409, {
> 0x8e, 0x67, 0xa2, 0xcd, 0xf, 0x61, 0xe1, 0x70 } }
> 
> 
> 
> +  ## Include/Guid/TraceHubDebugInfoHob.h
> 
> +  gTraceHubDebugInfoHobGuid = { 0xf88c9c23, 0x646c, 0x4f6c, { 0x8e, 0x3d,
> 0x36, 0xa9, 0x43, 0xc1, 0x08, 0x35 } }
> 
> +
> 
>    ## GUID used for Boot Discovery Policy FormSet guid and related variables.
> 
>    gBootDiscoveryPolicyMgrFormsetGuid = { 0x5b6f7107, 0xbb3c, 0x4660, {
> 0x92, 0xcd, 0x54, 0x26, 0x90, 0x28, 0x0b, 0xbd } }
> 
> 
> 
> @@ -1094,6 +1097,24 @@
>    # @Prompt Enable UEFI Stack Guard.
> 
> 
> gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard|FALSE|BOOLEAN|0x30
> 001055
> 
> 
> 
> +  ## Indicate debug level of Trace Hub.
> 
> +  #   0x0 - TraceHubDebugLevelError.<BR>
> 
> +  #   0x1 - TraceHubDebugLevelErrorWarning.<BR>
> 
> +  #   0x2 - TraceHubDebugLevelErrorWarningInfo.<BR>
> 
> +  #   0x3 - TraceHubDebugLevelErrorWarningInfoVerbose.<BR>
> 
> +  # @Prompt Debug level of Trace Hub.
> 
> +
> gEfiMdeModulePkgTokenSpaceGuid.PcdTraceHubDebugLevel|0|UINT8|0x3000
> 1056
> 
> +
> 
> +  ## Flag to enable or disable Trace Hub message.
> 
> +  #   FALSE - Disable Trace Hub debug message.<BR>
> 
> +  #   TRUE  - Enable Trace Hub debug message.<BR>
> 
> +  # @Prompt Enable or Disable Trace Hub message.
> 
> +
> gEfiMdeModulePkgTokenSpaceGuid.PcdEnableTraceHubDebugMsg|0|BOOLEA
> N|0x30001057
> 
> +
> 
> +  ## Indicate MMIO address where Trace Hub message output to.
> 
> +  # @Prompt Output MMIO address of Trace Hub message.
> 
> +
> gEfiMdeModulePkgTokenSpaceGuid.PcdTraceHubDebugMmioAddress|0|UINT
> 64|0x30001058
> 
> +
> 
>  [PcdsFixedAtBuild, PcdsPatchableInModule]
> 
>    ## Dynamic type PCD can be registered callback function for Pcd setting
> action.
> 
>    #  PcdMaxPeiPcdCallBackNumberPerPcdEntry indicates the maximum
> number of callback function
> 
> diff --git a/MdeModulePkg/MdeModulePkg.dsc
> b/MdeModulePkg/MdeModulePkg.dsc
> index 1014598f31..5b1f50e9c0 100644
> --- a/MdeModulePkg/MdeModulePkg.dsc
> +++ b/MdeModulePkg/MdeModulePkg.dsc
> @@ -515,6 +515,9 @@
>    MdeModulePkg/Universal/RegularExpressionDxe/RegularExpressionDxe.inf
> 
> 
> MdeModulePkg/Universal/SmmCommunicationBufferDxe/SmmCommunicatio
> nBufferDxe.inf
> 
>    MdeModulePkg/Universal/Disk/RamDiskDxe/RamDiskDxe.inf
> 
> +
> MdeModulePkg/Library/TraceHubDebugSysTLib/BaseTraceHubDebugSysTLib.i
> nf
> 
> +
> MdeModulePkg/Library/TraceHubDebugSysTLib/PeiTraceHubDebugSysTLib.inf
> 
> +
> MdeModulePkg/Library/TraceHubDebugSysTLib/DxeSmmTraceHubDebugSysTL
> ib.inf
> 
> 
> 
>  [Components.X64]
> 
>    MdeModulePkg/Universal/CapsulePei/CapsuleX64.inf
> 
> diff --git a/MdeModulePkg/MdeModulePkg.uni
> b/MdeModulePkg/MdeModulePkg.uni
> index 33ce9f6198..a17d34d60b 100644
> --- a/MdeModulePkg/MdeModulePkg.uni
> +++ b/MdeModulePkg/MdeModulePkg.uni
> @@ -1290,6 +1290,24 @@
>                                                                                      "   TRUE  - UEFI Stack Guard
> will be enabled.<BR>\n"
> 
>                                                                                      "   FALSE - UEFI Stack Guard
> will be disabled.<BR>"
> 
> 
> 
> +#string
> STR_gEfiMdeModulePkgTokenSpaceGuid_PcdTraceHubDebugLevel_PROMPT
> #language en-US "Debug level of Trace Hub."
> 
> +
> 
> +#string
> STR_gEfiMdeModulePkgTokenSpaceGuid_PcdTraceHubDebugLevel_HELP
> #language en-US "Indicate debug level of Trace Hub"
> 
> +                                                                                         "  0x0 -
> TraceHubDebugLevelError.<BR>"
> 
> +                                                                                         "  0x1 -
> TraceHubDebugLevelErrorWarning.<BR>"
> 
> +                                                                                         "  0x2 -
> TraceHubDebugLevelErrorWarningInfo.<BR>"
> 
> +                                                                                         "  0x3 -
> TraceHubDebugLevelErrorWarningInfoVerbose.<BR>"
> 
> +
> 
> +#string
> STR_gEfiMdeModulePkgTokenSpaceGuid_PcdEnableTraceHubDebugMsg_PRO
> MPT  #language en-US "Flag to enable or disable Trace Hub message"
> 
> +
> 
> +#string
> STR_gEfiMdeModulePkgTokenSpaceGuid_PcdEnableTraceHubDebugMsg_HELP
> #language en-US "Enable or Disable Trace Hub message"
> 
> +                                                                                             "  FALSE - Disable
> Trace Hub debug message.<BR>"
> 
> +                                                                                             "  TRUE  - Enable
> Trace Hub debug message.<BR>"
> 
> +
> 
> +#string
> STR_gEfiMdeModulePkgTokenSpaceGuid_PcdTraceHubDebugMmioAddress_P
> ROMPT  #language en-US "Output MMIO address of Trace Hub message"
> 
> +
> 
> +#string
> STR_gEfiMdeModulePkgTokenSpaceGuid_PcdTraceHubDebugMmioAddress_H
> ELP    #language en-US "Indicate MMIO address where Trace Hub message
> output to."
> 
> +
> 
>  #string
> STR_gEfiMdeModulePkgTokenSpaceGuid_PcdSetNvStoreDefaultId_PROMPT
> #language en-US "NV Storage DefaultId"
> 
> 
> 
>  #string
> STR_gEfiMdeModulePkgTokenSpaceGuid_PcdSetNvStoreDefaultId_HELP
> #language en-US "This dynamic PCD enables the default variable setting.\n"
> 
> diff --git a/MdePkg/Include/Library/TraceHubDebugSysTLib.h
> b/MdePkg/Include/Library/TraceHubDebugSysTLib.h
> new file mode 100644
> index 0000000000..7df20e67d6
> --- /dev/null
> +++ b/MdePkg/Include/Library/TraceHubDebugSysTLib.h
> @@ -0,0 +1,81 @@
> +/** @file
> 
> +This header file declares Trace Hub related top level APIs.
> 
> +
> 
> +Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> 
> +
> 
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> +
> 
> +**/
> 
> +
> 
> +#ifndef TRACE_HUB_DEBUG_SYST_LIB_H_
> 
> +#define TRACE_HUB_DEBUG_SYST_LIB_H_
> 
> +
> 
> +typedef enum {
> 
> +  SeverityNone    = 0,
> 
> +  SeverityFatal   = 1,
> 
> +  SeverityError   = 2,
> 
> +  SeverityWarning = 3,
> 
> +  SeverityNormal  = 4,
> 
> +  SeverityUser1   = 5,
> 
> +  SeverityUser2   = 6,
> 
> +  SeverityUser3   = 7,
> 
> +  SeverityMax
> 
> +} TRACE_HUB_SEVERITY_TYPE;
> 
> +
> 
> +/**
> 
> +  Write debug string to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType     Severity type of input message.
> 
> +  @param[in]  Buffer           A pointer to the data buffer.
> 
> +  @param[in]  NumberOfBytes    The size of data buffer.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTDebugWrite (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT8                    *Buffer,
> 
> +  IN UINTN                    NumberOfBytes
> 
> +  );
> 
> +
> 
> +/**
> 
> +  Write catalog status code message to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType     Severity type of input message.
> 
> +  @param[in]  Id               Catalog ID.
> 
> +  @param[in]  Guid             Driver Guid.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTWriteCataLog64StatusCode (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT64                   Id,
> 
> +  IN GUID                     *Guid
> 
> +  );
> 
> +
> 
> +/**
> 
> +  Write catalog message to specified Trace Hub MMIO address.
> 
> +
> 
> +  @param[in]  SeverityType   Severity type of input message.
> 
> +  @param[in]  Id             Catalog ID.
> 
> +  @param[in]  NumberOfParams Number of entries in argument list.
> 
> +  @param[in]  ...            Catalog message parameters.
> 
> +
> 
> +  @retval RETURN_SUCCESS      Data was written to Trace Hub.
> 
> +  @retval Other               Failed to output Trace Hub message.
> 
> +**/
> 
> +RETURN_STATUS
> 
> +EFIAPI
> 
> +TraceHubSysTWriteCataLog64 (
> 
> +  IN TRACE_HUB_SEVERITY_TYPE  SeverityType,
> 
> +  IN UINT64                   Id,
> 
> +  IN UINTN                    NumberOfParams,
> 
> +  ...
> 
> +  );
> 
> +
> 
> +#endif // TRACE_HUB_DEBUG_SYST_LIB_H_
> 
> diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec
> index d68ed988c0..597f4f7137 100644
> --- a/MdePkg/MdePkg.dec
> +++ b/MdePkg/MdePkg.dec
> @@ -298,6 +298,10 @@
>    #
> 
>    MipiSysTLib|Include/Library/MipiSysTLib.h
> 
> 
> 
> +  ##  @libraryclass  Provides API to output Trace Hub debug message.
> 
> +  #
> 
> +  TraceHubDebugSysTLib|Include/Library/TraceHubDebugSysTLib.h
> 
> +
> 
>  [LibraryClasses.IA32, LibraryClasses.X64, LibraryClasses.AARCH64]
> 
>    ##  @libraryclass  Provides services to generate random number.
> 
>    #
> 
> --
> 2.40.0.windows.1


  reply	other threads:[~2023-05-08 17:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-08  5:31 [PATCH 0/4] Trace Hub debug library support victorx.hsu
2023-05-08  5:31 ` [PATCH 1/4] MdePkg: Add MipiSysTLib library victorx.hsu
2023-05-08 17:54   ` Michael D Kinney
2023-05-08  5:31 ` [PATCH 2/4] MdeModulePkg: Add TraceHubDebugSysTLib library victorx.hsu
2023-05-08 17:49   ` Michael D Kinney [this message]
2023-05-08  5:31 ` [PATCH 3/4] MdePkg: Add NULL library of TraceHubDebugSysTLib victorx.hsu
2023-05-08  5:31 ` [PATCH 4/4] Maintainers.txt: Update reviewers and maintainers for Trace Hub dbg lib victorx.hsu
  -- strict thread matches above, loose matches on Subject: below --
2023-04-18 11:04 [PATCH 0/4] Trace Hub debug library support victorx.hsu
2023-04-18 11:04 ` [PATCH 2/4] MdeModulePkg: Add TraceHubDebugSysTLib library victorx.hsu

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=CO1PR11MB4929648BB6DC2ADEBFA0D3B5D2719@CO1PR11MB4929.namprd11.prod.outlook.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