]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Add performance library instances for SMM performance measurement.
authorrsun3 <rsun3@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 10 Mar 2011 07:39:51 +0000 (07:39 +0000)
committerrsun3 <rsun3@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 10 Mar 2011 07:39:51 +0000 (07:39 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11363 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Include/Guid/Performance.h
MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c [new file with mode: 0644]
MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.inf [new file with mode: 0644]
MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c [new file with mode: 0644]
MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.inf [new file with mode: 0644]
MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLibInternal.h [new file with mode: 0644]
MdeModulePkg/Library/SmmPerformanceLib/SmmPerformanceLib.c [new file with mode: 0644]
MdeModulePkg/Library/SmmPerformanceLib/SmmPerformanceLib.inf [new file with mode: 0644]
MdeModulePkg/MdeModulePkg.dec
MdeModulePkg/MdeModulePkg.dsc

index 176b78f9917a2b82979f240f11cb8ffa8a4217db..3659ccc3280d1443d549d1bf7313352648761864 100644 (file)
@@ -4,7 +4,7 @@
   * performance protocol interfaces.\r
   * performance variables.  \r
 \r
-Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available under \r
 the terms and conditions of the BSD License that accompanies this distribution.  \r
 The full text of the license may be found at\r
@@ -100,6 +100,35 @@ typedef struct {
   UINT32                Reserved;\r
 } GAUGE_DATA_HEADER;\r
 \r
+//\r
+// SMM Performance Protocol definitions\r
+//\r
+\r
+#define SMM_PERFORMANCE_PROTOCOL_GUID \\r
+  { 0xf866226a, 0xeaa5, 0x4f5a, { 0xa9, 0xa, 0x6c, 0xfb, 0xa5, 0x7c, 0x58, 0x8e } }\r
+\r
+//\r
+// SMM_PERFORMANCE_STRING_SIZE.\r
+//\r
+#define SMM_PERFORMANCE_STRING_SIZE     32\r
+#define SMM_PERFORMANCE_STRING_LENGTH   (SMM_PERFORMANCE_STRING_SIZE - 1)\r
+\r
+//\r
+// The default guage entries number for SMM phase.\r
+//\r
+#define INIT_SMM_GAUGE_DATA_ENTRIES     200\r
+\r
+typedef struct {\r
+  UINTN             Function;\r
+  EFI_STATUS        ReturnStatus;\r
+  UINTN             NumberOfEntries;\r
+  UINTN             LogEntryKey;\r
+  GAUGE_DATA_ENTRY  *GaugeData;\r
+} SMM_PERF_COMMUNICATE;\r
+\r
+#define SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER          1\r
+#define SMM_PERF_FUNCTION_GET_GAUGE_DATA                  2\r
+\r
 /**\r
   Adds a record at the end of the performance measurement log\r
   that records the start time of a performance measurement.\r
@@ -194,5 +223,6 @@ struct _PERFORMANCE_PROTOCOL {
 };\r
 \r
 extern EFI_GUID gPerformanceProtocolGuid;\r
+extern EFI_GUID gSmmPerformanceProtocolGuid;\r
 \r
 #endif\r
diff --git a/MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c b/MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.c
new file mode 100644 (file)
index 0000000..3b0a9f6
--- /dev/null
@@ -0,0 +1,363 @@
+/** @file\r
+  Performance library instance used in DXE phase to dump SMM performance data.\r
+\r
+  This library instance allows a DXE driver or UEFI application to dump the SMM performance data.\r
+  StartPerformanceMeasurement() and EndPerformanceMeasurement() are not implemented.\r
+\r
+  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Guid/Performance.h>\r
+\r
+#include <Library/PerformanceLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+#include <Protocol/SmmCommunication.h>\r
+\r
+#define SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)  + sizeof (SMM_PERF_COMMUNICATE))\r
+//\r
+// The cached performance protocol interface.\r
+//\r
+EFI_SMM_COMMUNICATION_PROTOCOL  *mSmmCommunication = NULL;\r
+UINT8                           mSmmPerformanceBuffer[SMM_PERFORMANCE_COMMUNICATION_BUFFER_SIZE];\r
+GAUGE_DATA_ENTRY                *mGaugeData = NULL;\r
+UINTN                           mGaugeNumberOfEntries = 0;\r
+\r
+\r
+/**\r
+  The constructor function caches the pointer to SMM Communication protocol.\r
+\r
+  The constructor function locates Performance protocol from protocol database.\r
+  It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.\r
+\r
+  @retval EFI_SUCCESS     Performance protocol is successfully located.\r
+  @retval Other           Performance protocol is not located to log performance.\r
+\r
+**/\r
+EFI_STATUS\r
+GetCommunicationProtocol (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  EFI_SMM_COMMUNICATION_PROTOCOL  *Communication;\r
+\r
+  if (mSmmCommunication != NULL) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &Communication);\r
+  if (!EFI_ERROR (Status)) {\r
+    ASSERT (Communication != NULL);\r
+    //\r
+    // Cache SMM Communication protocol.\r
+    //\r
+    mSmmCommunication = Communication;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Creates a record for the beginning of a performance measurement.\r
+\r
+  Creates a record that contains the Handle, Token, and Module.\r
+  If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
+  If TimeStamp is zero, then this function reads the current time stamp\r
+  and adds that time stamp value to the record as the start time.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval RETURN_SUCCESS          The start of the measurement was recorded.\r
+  @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+StartPerformanceMeasurement (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+  Fills in the end time of a performance measurement.\r
+\r
+  Looks up the record that matches Handle, Token, and Module.\r
+  If the record can not be found then return RETURN_NOT_FOUND.\r
+  If the record is found and TimeStamp is not zero,\r
+  then TimeStamp is added to the record as the end time.\r
+  If the record is found and TimeStamp is zero, then this function reads\r
+  the current time stamp and adds that time stamp value to the record as the end time.\r
+  If this function is called multiple times for the same record, then the end time is overwritten.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval RETURN_SUCCESS          The end of  the measurement was recorded.\r
+  @retval RETURN_NOT_FOUND        The specified measurement record could not be found.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+EndPerformanceMeasurement (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  return EFI_UNSUPPORTED;\r
+}\r
+\r
+/**\r
+  Retrieves all previous logged performance measurement.\r
+  Function will use SMM communicate protocol to get all previous SMM performance measurement data.\r
+  If success, data buffer will be returned. If fail function will return NULL.\r
+\r
+  @retval !NULL           Get all gauge data success.\r
+  @retval NULL            Get all guage data failed.\r
+**/\r
+GAUGE_DATA_ENTRY*\r
+EFIAPI\r
+GetAllSmmGaugeData (VOID)\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_SMM_COMMUNICATE_HEADER  *SmmCommBufferHeader;\r
+  SMM_PERF_COMMUNICATE        *SmmPerfCommData;\r
+  UINTN                       CommSize;\r
+  UINTN                       DataSize;\r
+\r
+  if (mGaugeData != NULL) {\r
+    return mGaugeData;\r
+  }\r
+\r
+  Status = GetCommunicationProtocol ();\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Initialize communicate buffer \r
+  //\r
+  SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER*)mSmmPerformanceBuffer;\r
+  SmmPerfCommData = (SMM_PERF_COMMUNICATE*)SmmCommBufferHeader->Data;\r
+  ZeroMem((UINT8*)SmmPerfCommData, sizeof(SMM_PERF_COMMUNICATE));\r
+    \r
+  CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gSmmPerformanceProtocolGuid);\r
+  SmmCommBufferHeader->MessageLength = sizeof(SMM_PERF_COMMUNICATE);\r
+  CommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)  + sizeof(SMM_PERF_COMMUNICATE);\r
+\r
+  //\r
+  // Get totol number of SMM gauge entries\r
+  //\r
+  SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER;\r
+  Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  if (EFI_ERROR (SmmPerfCommData->ReturnStatus) || SmmPerfCommData->NumberOfEntries == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  mGaugeNumberOfEntries = SmmPerfCommData->NumberOfEntries;\r
+  \r
+  DataSize = mGaugeNumberOfEntries * sizeof(GAUGE_DATA_ENTRY);\r
+  mGaugeData = AllocateZeroPool(DataSize);\r
+  ASSERT_EFI_ERROR (Status);\r
+  \r
+  //\r
+  // Get all SMM gauge data\r
+  //  \r
+  SmmPerfCommData->Function = SMM_PERF_FUNCTION_GET_GAUGE_DATA;\r
+  SmmPerfCommData->LogEntryKey = 0;\r
+  SmmPerfCommData->NumberOfEntries = mGaugeNumberOfEntries;\r
+  SmmPerfCommData->GaugeData = mGaugeData;\r
+  Status = mSmmCommunication->Communicate (mSmmCommunication, mSmmPerformanceBuffer, &CommSize);\r
+  ASSERT_EFI_ERROR (Status);\r
+  ASSERT_EFI_ERROR(SmmPerfCommData->ReturnStatus);\r
+\r
+  return mGaugeData; \r
+}\r
+\r
+/**\r
+  Retrieves a previously logged performance measurement.\r
+\r
+  Retrieves the performance log entry from the performance log specified by LogEntryKey.\r
+  If it stands for a valid entry, then EFI_SUCCESS is returned and\r
+  GaugeDataEntry stores the pointer to that entry.\r
+\r
+  @param  LogEntryKey             The key for the previous performance measurement log entry.\r
+                                  If 0, then the first performance measurement log entry is retrieved.\r
+  @param  GaugeDataEntry          The indirect pointer to the gauge data entry specified by LogEntryKey\r
+                                  if the retrieval is successful.\r
+\r
+  @retval EFI_SUCCESS             The GuageDataEntry is successfully found based on LogEntryKey.\r
+  @retval EFI_NOT_FOUND           The LogEntryKey is the last entry (equals to the total entry number).\r
+  @retval EFI_INVALIDE_PARAMETER  The LogEntryKey is not a valid entry (greater than the total entry number).\r
+  @retval EFI_INVALIDE_PARAMETER  GaugeDataEntry is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetGauge (\r
+  IN  UINTN               LogEntryKey,\r
+  OUT GAUGE_DATA_ENTRY    **GaugeDataEntry\r
+  )\r
+{\r
+  if (LogEntryKey > mGaugeNumberOfEntries) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  if (LogEntryKey == mGaugeNumberOfEntries) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  if (GaugeDataEntry == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  *GaugeDataEntry = &mGaugeData[LogEntryKey];\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
+\r
+  Attempts to retrieve the performance log entry specified by LogEntryKey.  If LogEntryKey is\r
+  zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
+  and the key for the second entry in the log is returned.  If the performance log is empty,\r
+  then no entry is retrieved and zero is returned.  If LogEntryKey is not zero, then the performance\r
+  log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
+  returned.  If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
+  retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
+  log is returned.  If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
+  is retrieved and zero is returned.  In the cases where a performance log entry can be returned,\r
+  the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
+  If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
+  If Handle is NULL, then ASSERT().\r
+  If Token is NULL, then ASSERT().\r
+  If Module is NULL, then ASSERT().\r
+  If StartTimeStamp is NULL, then ASSERT().\r
+  If EndTimeStamp is NULL, then ASSERT().\r
+\r
+  @param  LogEntryKey             On entry, the key of the performance measurement log entry to retrieve.\r
+                                  0, then the first performance measurement log entry is retrieved.\r
+                                  On exit, the key of the next performance log entry.\r
+  @param  Handle                  Pointer to environment specific context used to identify the component\r
+                                  being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string that identifies the component\r
+                                  being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string that identifies the module\r
+                                  being measured.\r
+  @param  StartTimeStamp          Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was started.\r
+  @param  EndTimeStamp            Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was ended.\r
+\r
+  @return The key for the next performance log entry (in general case).\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+GetPerformanceMeasurement (\r
+  IN  UINTN       LogEntryKey,\r
+  OUT CONST VOID  **Handle,\r
+  OUT CONST CHAR8 **Token,\r
+  OUT CONST CHAR8 **Module,\r
+  OUT UINT64      *StartTimeStamp,\r
+  OUT UINT64      *EndTimeStamp\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  GAUGE_DATA_ENTRY      *GaugeData;\r
+\r
+  GaugeData = NULL;\r
+\r
+  ASSERT (Handle != NULL);\r
+  ASSERT (Token != NULL);\r
+  ASSERT (Module != NULL);\r
+  ASSERT (StartTimeStamp != NULL);\r
+  ASSERT (EndTimeStamp != NULL);\r
+\r
+  mGaugeData = GetAllSmmGaugeData();\r
+  if (mGaugeData == NULL) {\r
+    return 0;\r
+  }\r
+\r
+  Status = GetGauge (LogEntryKey++, &GaugeData);\r
+\r
+  //\r
+  // Make sure that LogEntryKey is a valid log entry key,\r
+  //\r
+  ASSERT (Status != EFI_INVALID_PARAMETER);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // The LogEntryKey is the last entry (equals to the total entry number).\r
+    //\r
+    return 0;\r
+\r
+  }\r
+\r
+  ASSERT (GaugeData != NULL);\r
+\r
+  *Handle         = (VOID *) (UINTN) GaugeData->Handle;\r
+  *Token          = GaugeData->Token;\r
+  *Module         = GaugeData->Module;\r
+  *StartTimeStamp = GaugeData->StartTimeStamp;\r
+  *EndTimeStamp   = GaugeData->EndTimeStamp;\r
+\r
+  return LogEntryKey;\r
+}\r
+\r
+/**\r
+  Returns TRUE if the performance measurement macros are enabled.\r
+\r
+  This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+  PcdPerformanceLibraryPropertyMask is set.  Otherwise FALSE is returned.\r
+\r
+  @retval TRUE                    The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+                                  PcdPerformanceLibraryPropertyMask is set.\r
+  @retval FALSE                   The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+                                  PcdPerformanceLibraryPropertyMask is clear.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PerformanceMeasurementEnabled (\r
+  VOID\r
+  )\r
+{\r
+  return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
+}\r
diff --git a/MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.inf b/MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.inf
new file mode 100644 (file)
index 0000000..5ba86e4
--- /dev/null
@@ -0,0 +1,61 @@
+## @file\r
+#  Performance library instance used in DXE phase to dump SMM performance data.\r
+#  \r
+#  This library instance allows a DXE driver or UEFI application to dump the SMM performance data.\r
+#  StartPerformanceMeasurement() and EndPerformanceMeasurement() are not implemented.\r
+#  \r
+#  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+#  This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution.  The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  \r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#  \r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = DxeSmmPerformanceLib\r
+  FILE_GUID                      = DA80C15C-0B4D-4e75-8946-4043DE559B0C\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = PerformanceLib|DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_APPLICATION\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  DxeSmmPerformanceLib.c\r
+\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  UefiBootServicesTableLib\r
+  PcdLib\r
+  DebugLib\r
+  BaseMemoryLib\r
+  UefiRuntimeServicesTableLib\r
+  MemoryAllocationLib\r
+\r
+[Guids]\r
+  gSmmPerformanceProtocolGuid            ## CONSUMES\r
+\r
+[Protocols]\r
+  gEfiSmmCommunicationProtocolGuid       ## CONSUMES\r
+\r
+\r
+[Pcd]\r
+  gEfiMdePkgTokenSpaceGuid.PcdPerformanceLibraryPropertyMask\r
+\r
+[Depex]\r
+  gEfiSmmCommunicationProtocolGuid\r
diff --git a/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c b/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
new file mode 100644 (file)
index 0000000..98426c3
--- /dev/null
@@ -0,0 +1,715 @@
+/** @file\r
+  Performance library instance used by SMM Core.\r
+\r
+  This library provides the performance measurement interfaces and initializes performance\r
+  logging for the SMM phase.\r
+  It initializes SMM phase performance logging by publishing the Performance Protocol,\r
+  which is consumed by SmmPerformanceLib to logging performance data in SMM phase.\r
+\r
+  This library is mainly used by SMM Core to start performance logging to ensure that\r
+  SMM Performance Protocol is installed at the very beginning of SMM phase.\r
+\r
+Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+\r
+#include "SmmCorePerformanceLibInternal.h"\r
+\r
+//\r
+// The data structure to hold global performance data.\r
+//\r
+GAUGE_DATA_HEADER       *mGaugeData;\r
+\r
+//\r
+// The current maximum number of logging entries. If current number of \r
+// entries exceeds this value, it will re-allocate a larger array and\r
+// migration the old data to the larger array.\r
+//\r
+UINT32                  mMaxGaugeRecords;\r
+\r
+//\r
+// The handle to install Performance Protocol instance.\r
+//\r
+EFI_HANDLE              mHandle = NULL;\r
+\r
+BOOLEAN                 mPerformanceMeasurementEnabled;\r
+\r
+SPIN_LOCK               mSmmPerfLock;\r
+\r
+EFI_SMRAM_DESCRIPTOR    *mSmramRanges;\r
+UINTN                   mSmramRangeCount;\r
+\r
+//\r
+// Interfaces for performance protocol.\r
+//\r
+PERFORMANCE_PROTOCOL mPerformanceInterface = {\r
+  StartGauge,\r
+  EndGauge,\r
+  GetGauge\r
+};\r
+\r
+/**\r
+  Searches in the gauge array with keyword Handle, Token and Module.\r
+\r
+  This internal function searches for the gauge entry in the gauge array.\r
+  If there is an entry that exactly matches the given key word triple\r
+  and its end time stamp is zero, then the index of that gauge entry is returned;\r
+  otherwise, the the number of gauge entries in the array is returned.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+\r
+  @retval The index of gauge entry in the array.\r
+\r
+**/\r
+UINT32\r
+SmmSearchForGaugeEntry (\r
+  IN CONST VOID                 *Handle,  OPTIONAL\r
+  IN CONST CHAR8                *Token,   OPTIONAL\r
+  IN CONST CHAR8                *Module   OPTIONAL\r
+  )\r
+{\r
+  UINT32                    Index;\r
+  UINT32                    NumberOfEntries;\r
+  GAUGE_DATA_ENTRY          *GaugeEntryArray;\r
+\r
+  if (Token == NULL) {\r
+    Token = "";\r
+  }\r
+  if (Module == NULL) {\r
+    Module = "";\r
+  }\r
+\r
+  NumberOfEntries = mGaugeData->NumberOfEntries;\r
+  GaugeEntryArray = (GAUGE_DATA_ENTRY *) (mGaugeData + 1);\r
+\r
+  for (Index = 0; Index < NumberOfEntries; Index++) {\r
+    if ((GaugeEntryArray[Index].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&\r
+         AsciiStrnCmp (GaugeEntryArray[Index].Token, Token, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&\r
+         AsciiStrnCmp (GaugeEntryArray[Index].Module, Module, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&\r
+         GaugeEntryArray[Index].EndTimeStamp == 0) {\r
+      break;\r
+    }\r
+  }\r
+\r
+  return Index;\r
+}\r
+\r
+/**\r
+  Adds a record at the end of the performance measurement log\r
+  that records the start time of a performance measurement.\r
+\r
+  Adds a record to the end of the performance measurement log\r
+  that contains the Handle, Token, and Module.\r
+  The end time of the new record must be set to zero.\r
+  If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
+  If TimeStamp is zero, the start time in the record is filled in with the value\r
+  read from the current time stamp.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval EFI_SUCCESS             The data was read correctly from the device.\r
+  @retval EFI_OUT_OF_RESOURCES    There are not enough resources to record the measurement.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+StartGauge (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  GAUGE_DATA_ENTRY          *GaugeEntryArray;\r
+  UINTN                     GaugeDataSize;\r
+  UINTN                     OldGaugeDataSize;\r
+  GAUGE_DATA_HEADER         *OldGaugeData;\r
+  UINT32                    Index;\r
+\r
+  AcquireSpinLock (&mSmmPerfLock);\r
+\r
+  Index = mGaugeData->NumberOfEntries;\r
+  if (Index >= mMaxGaugeRecords) {\r
+    //\r
+    // Try to enlarge the scale of gauge array.\r
+    //\r
+    OldGaugeData      = mGaugeData;\r
+    OldGaugeDataSize  = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY) * mMaxGaugeRecords;\r
+\r
+    mMaxGaugeRecords *= 2;\r
+    GaugeDataSize     = sizeof (GAUGE_DATA_HEADER) + sizeof (GAUGE_DATA_ENTRY) * mMaxGaugeRecords;\r
+\r
+    mGaugeData = AllocateZeroPool (GaugeDataSize);\r
+    if (mGaugeData == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    //\r
+    // Initialize new data array and migrate old data one.\r
+    //\r
+    mGaugeData = CopyMem (mGaugeData, OldGaugeData, OldGaugeDataSize);\r
+\r
+    FreePool (OldGaugeData);\r
+  }\r
+\r
+  GaugeEntryArray               = (GAUGE_DATA_ENTRY *) (mGaugeData + 1);\r
+  GaugeEntryArray[Index].Handle = (EFI_PHYSICAL_ADDRESS) (UINTN) Handle;\r
+\r
+  if (Token != NULL) {\r
+    AsciiStrnCpy (GaugeEntryArray[Index].Token, Token, SMM_PERFORMANCE_STRING_LENGTH);\r
+  }\r
+  if (Module != NULL) {\r
+    AsciiStrnCpy (GaugeEntryArray[Index].Module, Module, SMM_PERFORMANCE_STRING_LENGTH);\r
+  }\r
+\r
+  if (TimeStamp == 0) {\r
+    TimeStamp = GetPerformanceCounter ();\r
+  }\r
+  GaugeEntryArray[Index].StartTimeStamp = TimeStamp;\r
+\r
+  mGaugeData->NumberOfEntries++;\r
+\r
+  ReleaseSpinLock (&mSmmPerfLock);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Searches the performance measurement log from the beginning of the log\r
+  for the first matching record that contains a zero end time and fills in a valid end time.\r
+\r
+  Searches the performance measurement log from the beginning of the log\r
+  for the first record that matches Handle, Token, and Module and has an end time value of zero.\r
+  If the record can not be found then return EFI_NOT_FOUND.\r
+  If the record is found and TimeStamp is not zero,\r
+  then the end time in the record is filled in with the value specified by TimeStamp.\r
+  If the record is found and TimeStamp is zero, then the end time in the matching record\r
+  is filled in with the current time stamp value.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval EFI_SUCCESS             The end of  the measurement was recorded.\r
+  @retval EFI_NOT_FOUND           The specified measurement record could not be found.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EndGauge (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  GAUGE_DATA_ENTRY  *GaugeEntryArray;\r
+  UINT32            Index;\r
+\r
+  if (TimeStamp == 0) {\r
+    TimeStamp = GetPerformanceCounter ();\r
+  }\r
+\r
+  Index = SmmSearchForGaugeEntry (Handle, Token, Module);\r
+  if (Index >= mGaugeData->NumberOfEntries) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  GaugeEntryArray = (GAUGE_DATA_ENTRY  *) (mGaugeData + 1);\r
+  GaugeEntryArray[Index].EndTimeStamp = TimeStamp;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Retrieves a previously logged performance measurement.\r
+\r
+  Retrieves the performance log entry from the performance log specified by LogEntryKey.\r
+  If it stands for a valid entry, then EFI_SUCCESS is returned and\r
+  GaugeDataEntry stores the pointer to that entry.\r
+\r
+  @param  LogEntryKey             The key for the previous performance measurement log entry.\r
+                                  If 0, then the first performance measurement log entry is retrieved.\r
+  @param  GaugeDataEntry          The indirect pointer to the gauge data entry specified by LogEntryKey\r
+                                  if the retrieval is successful.\r
+\r
+  @retval EFI_SUCCESS             The GuageDataEntry is successfully found based on LogEntryKey.\r
+  @retval EFI_NOT_FOUND           The LogEntryKey is the last entry (equals to the total entry number).\r
+  @retval EFI_INVALIDE_PARAMETER  The LogEntryKey is not a valid entry (greater than the total entry number).\r
+  @retval EFI_INVALIDE_PARAMETER  GaugeDataEntry is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetGauge (\r
+  IN  UINTN               LogEntryKey,\r
+  OUT GAUGE_DATA_ENTRY    **GaugeDataEntry\r
+  )\r
+{\r
+  UINTN               NumberOfEntries;\r
+  GAUGE_DATA_ENTRY    *LogEntryArray;\r
+\r
+  NumberOfEntries = (UINTN) (mGaugeData->NumberOfEntries);\r
+  if (LogEntryKey > NumberOfEntries) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  if (LogEntryKey == NumberOfEntries) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  LogEntryArray   = (GAUGE_DATA_ENTRY *) (mGaugeData + 1);\r
+\r
+  if (GaugeDataEntry == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  *GaugeDataEntry = &LogEntryArray[LogEntryKey];\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  This function check if the address is in SMRAM.\r
+\r
+  @param Buffer  the buffer address to be checked.\r
+  @param Length  the buffer length to be checked.\r
+\r
+  @retval TRUE  this address is in SMRAM.\r
+  @retval FALSE this address is NOT in SMRAM.\r
+**/\r
+BOOLEAN\r
+IsAddressInSmram (\r
+  IN EFI_PHYSICAL_ADDRESS  Buffer,\r
+  IN UINT64                Length\r
+  )\r
+{\r
+  UINTN  Index;\r
+\r
+  for (Index = 0; Index < mSmramRangeCount; Index ++) {\r
+    if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||\r
+        ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {\r
+      return TRUE;\r
+    }\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+  Communication service SMI Handler entry.\r
+\r
+  This SMI handler provides services for the variable wrapper driver.\r
+\r
+  @param[in]     DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
+  @param[in]     RegisterContext Points to an optional handler context which was specified when the\r
+                                 handler was registered.\r
+  @param[in, out] CommBuffer     A pointer to a collection of data in memory that will\r
+                                 be conveyed from a non-SMM environment into an SMM environment.\r
+  @param[in, out] CommBufferSize The size of the CommBuffer.\r
+\r
+  @retval EFI_SUCCESS                         The interrupt was handled and quiesced. No other handlers \r
+                                              should still be called.\r
+  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED  The interrupt has been quiesced but other handlers should \r
+                                              still be called.\r
+  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING   The interrupt is still pending and other handlers should still \r
+                                              be called.\r
+  @retval EFI_INTERRUPT_PENDING               The interrupt could not be quiesced.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmPerformanceHandler (\r
+  IN     EFI_HANDLE                    DispatchHandle,\r
+  IN     CONST VOID                   *RegisterContext,\r
+  IN OUT VOID                           *CommBuffer,\r
+  IN OUT UINTN                          *CommBufferSize\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  SMM_PERF_COMMUNICATE  *SmmPerfCommData;\r
+  GAUGE_DATA_ENTRY      *GaugeData; \r
+  UINTN                 DataSize;\r
+\r
+  GaugeData = NULL;\r
+  \r
+  ASSERT (CommBuffer != NULL);\r
+\r
+  SmmPerfCommData = (SMM_PERF_COMMUNICATE*)CommBuffer;\r
+\r
+  switch (SmmPerfCommData->Function) {\r
+    case SMM_PERF_FUNCTION_GET_GAUGE_ENTRY_NUMBER :\r
+       SmmPerfCommData->NumberOfEntries = mGaugeData->NumberOfEntries;\r
+       return Status = EFI_SUCCESS;\r
+       break;\r
+\r
+    case SMM_PERF_FUNCTION_GET_GAUGE_DATA :\r
+       if ( SmmPerfCommData->GaugeData == NULL || SmmPerfCommData->NumberOfEntries <= 0 ||\r
+            (SmmPerfCommData->LogEntryKey + SmmPerfCommData->NumberOfEntries) > mGaugeData->NumberOfEntries) {\r
+         Status = EFI_INVALID_PARAMETER;\r
+         break;\r
+       } \r
+\r
+       Status = GetGauge(SmmPerfCommData->LogEntryKey, &GaugeData);\r
+       if (EFI_ERROR(Status)) {\r
+         break;\r
+       }\r
+          \r
+       //\r
+       // Sanity check\r
+       //\r
+       DataSize = SmmPerfCommData->NumberOfEntries * sizeof(GAUGE_DATA_ENTRY);\r
+       if (IsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)SmmPerfCommData->GaugeData, DataSize)) {\r
+         DEBUG ((EFI_D_ERROR, "Smm Performance Data buffer is in SMRAM!\n"));\r
+         Status = EFI_ACCESS_DENIED;\r
+         break ;\r
+       }\r
+\r
+       CopyMem(\r
+         (UINT8*)SmmPerfCommData->GaugeData, \r
+         (UINT8*)GaugeData, \r
+         DataSize\r
+         );\r
+       break;\r
+\r
+    default:\r
+       ASSERT (FALSE);\r
+       Status = EFI_UNSUPPORTED;\r
+  }            \r
+\r
+  SmmPerfCommData->ReturnStatus = Status;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  SmmBase2 protocol notify callback function, when SMST and SMM memory service get initialized \r
+  this function is callbacked to to initialize the Smm Performance Lib \r
+\r
+  @param  Event    The event of notify protocol.\r
+  @param  Context  Notify event context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+InitializeSmmCorePerformanceLib (\r
+  IN EFI_EVENT     Event,\r
+  IN VOID          *Context\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  EFI_HANDLE                Handle;\r
+  EFI_SMM_ACCESS2_PROTOCOL  *SmmAccess;\r
+  UINTN                     Size;\r
+\r
+\r
+  //\r
+  // Initialize spin lock\r
+  //\r
+  InitializeSpinLock (&mSmmPerfLock);\r
+\r
+  mMaxGaugeRecords = INIT_SMM_GAUGE_DATA_ENTRIES + PcdGet8 (PcdMaxPeiPerformanceLogEntries);\r
+\r
+  mGaugeData = AllocateZeroPool (sizeof (GAUGE_DATA_HEADER) + (sizeof (GAUGE_DATA_ENTRY) * mMaxGaugeRecords));\r
+  ASSERT (mGaugeData != NULL);\r
+  \r
+  //\r
+  // Get SMRAM information\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Size = 0;\r
+  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
+  ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
+\r
+  Status = gSmst->SmmAllocatePool (\r
+                    EfiRuntimeServicesData,\r
+                    Size,\r
+                    (VOID **)&mSmramRanges\r
+                    );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
+\r
+  //\r
+  // Install the protocol interfaces.\r
+  //\r
+  Status = gSmst->SmmInstallProtocolInterface (\r
+                    &mHandle,\r
+                    &gSmmPerformanceProtocolGuid,\r
+                    EFI_NATIVE_INTERFACE,\r
+                    &mPerformanceInterface\r
+                    );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  ///\r
+  /// Register SMM Performance SMI handler\r
+  ///\r
+  Handle = NULL;\r
+  Status = gSmst->SmiHandlerRegister (SmmPerformanceHandler, &gSmmPerformanceProtocolGuid, &Handle);\r
+  ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
+/**\r
+  The constructor function initializes Performance infrastructure for DXE phase.\r
+\r
+  The constructor function publishes Performance protocol, allocates memory to log DXE performance\r
+  and merges PEI performance data to DXE performance log.\r
+  It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.\r
+\r
+  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
+  @param  SystemTable   A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmCorePerformanceLibConstructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  EFI_EVENT   Event;\r
+  VOID        *Registration;\r
+\r
+  mPerformanceMeasurementEnabled =  (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
+  if (!mPerformanceMeasurementEnabled) {\r
+    //\r
+    // Do not initialize performance infrastructure if not required.\r
+    //\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Create the events to do the library init.\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  InitializeSmmCorePerformanceLib,\r
+                  NULL,\r
+                  &Event\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Register for protocol notifications on this event\r
+  //\r
+  Status = gBS->RegisterProtocolNotify (\r
+                  &gEfiSmmBase2ProtocolGuid,\r
+                  Event,\r
+                  &Registration\r
+                  );\r
+\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Adds a record at the end of the performance measurement log\r
+  that records the start time of a performance measurement.\r
+\r
+  Adds a record to the end of the performance measurement log\r
+  that contains the Handle, Token, and Module.\r
+  The end time of the new record must be set to zero.\r
+  If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
+  If TimeStamp is zero, the start time in the record is filled in with the value\r
+  read from the current time stamp.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval RETURN_SUCCESS          The start of the measurement was recorded.\r
+  @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+StartPerformanceMeasurement (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = StartGauge (Handle, Token, Module, TimeStamp);\r
+  return (RETURN_STATUS) Status;\r
+}\r
+\r
+/**\r
+  Searches the performance measurement log from the beginning of the log\r
+  for the first matching record that contains a zero end time and fills in a valid end time.\r
+\r
+  Searches the performance measurement log from the beginning of the log\r
+  for the first record that matches Handle, Token, and Module and has an end time value of zero.\r
+  If the record can not be found then return RETURN_NOT_FOUND.\r
+  If the record is found and TimeStamp is not zero,\r
+  then the end time in the record is filled in with the value specified by TimeStamp.\r
+  If the record is found and TimeStamp is zero, then the end time in the matching record\r
+  is filled in with the current time stamp value.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval RETURN_SUCCESS          The end of  the measurement was recorded.\r
+  @retval RETURN_NOT_FOUND        The specified measurement record could not be found.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+EndPerformanceMeasurement (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = EndGauge (Handle, Token, Module, TimeStamp);\r
+  return (RETURN_STATUS) Status;\r
+}\r
+\r
+/**\r
+  Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
+\r
+  Attempts to retrieve the performance log entry specified by LogEntryKey.  If LogEntryKey is\r
+  zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
+  and the key for the second entry in the log is returned.  If the performance log is empty,\r
+  then no entry is retrieved and zero is returned.  If LogEntryKey is not zero, then the performance\r
+  log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
+  returned.  If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
+  retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
+  log is returned.  If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
+  is retrieved and zero is returned.  In the cases where a performance log entry can be returned,\r
+  the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
+  If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
+  If Handle is NULL, then ASSERT().\r
+  If Token is NULL, then ASSERT().\r
+  If Module is NULL, then ASSERT().\r
+  If StartTimeStamp is NULL, then ASSERT().\r
+  If EndTimeStamp is NULL, then ASSERT().\r
+\r
+  @param  LogEntryKey             On entry, the key of the performance measurement log entry to retrieve.\r
+                                  0, then the first performance measurement log entry is retrieved.\r
+                                  On exit, the key of the next performance log entry.\r
+  @param  Handle                  Pointer to environment specific context used to identify the component\r
+                                  being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string that identifies the component\r
+                                  being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string that identifies the module\r
+                                  being measured.\r
+  @param  StartTimeStamp          Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was started.\r
+  @param  EndTimeStamp            Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was ended.\r
+\r
+  @return The key for the next performance log entry (in general case).\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+GetPerformanceMeasurement (\r
+  IN  UINTN       LogEntryKey,\r
+  OUT CONST VOID  **Handle,\r
+  OUT CONST CHAR8 **Token,\r
+  OUT CONST CHAR8 **Module,\r
+  OUT UINT64      *StartTimeStamp,\r
+  OUT UINT64      *EndTimeStamp\r
+  )\r
+{\r
+  EFI_STATUS        Status;\r
+  GAUGE_DATA_ENTRY  *GaugeData;\r
+\r
+  GaugeData = NULL;\r
+  \r
+  ASSERT (Handle != NULL);\r
+  ASSERT (Token != NULL);\r
+  ASSERT (Module != NULL);\r
+  ASSERT (StartTimeStamp != NULL);\r
+  ASSERT (EndTimeStamp != NULL);\r
+\r
+  Status = GetGauge (LogEntryKey++, &GaugeData);\r
+\r
+  //\r
+  // Make sure that LogEntryKey is a valid log entry key,\r
+  //\r
+  ASSERT (Status != EFI_INVALID_PARAMETER);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // The LogEntryKey is the last entry (equals to the total entry number).\r
+    //\r
+    return 0;\r
+  }\r
+\r
+  ASSERT (GaugeData != NULL);\r
+\r
+  *Handle         = (VOID *) (UINTN) GaugeData->Handle;\r
+  *Token          = GaugeData->Token;\r
+  *Module         = GaugeData->Module;\r
+  *StartTimeStamp = GaugeData->StartTimeStamp;\r
+  *EndTimeStamp   = GaugeData->EndTimeStamp;\r
+\r
+  return LogEntryKey;\r
+}\r
+\r
+/**\r
+  Returns TRUE if the performance measurement macros are enabled.\r
+\r
+  This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+  PcdPerformanceLibraryPropertyMask is set.  Otherwise FALSE is returned.\r
+\r
+  @retval TRUE                    The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+                                  PcdPerformanceLibraryPropertyMask is set.\r
+  @retval FALSE                   The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+                                  PcdPerformanceLibraryPropertyMask is clear.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PerformanceMeasurementEnabled (\r
+  VOID\r
+  )\r
+{\r
+  return mPerformanceMeasurementEnabled;\r
+}\r
+\r
+\r
diff --git a/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.inf b/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.inf
new file mode 100644 (file)
index 0000000..cbf2c54
--- /dev/null
@@ -0,0 +1,67 @@
+## @file\r
+#  Performance library instance used by SMM Core.\r
+#  \r
+#  This library provides the performance measurement interfaces and initializes performance\r
+#  logging for the SMM phase. \r
+#  It initializes SMM phase performance logging by publishing the Performance Protocol,\r
+#  which is consumed by SmmPerformanceLib to logging performance data in SMM phase.\r
+#  This library is mainly used by SMM Core to start performance logging to ensure that\r
+#  SMM Performance Protocol is installed at the very beginning of SMM phase.\r
+#  \r
+#  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+#  This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution.  The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  \r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#  \r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = SmmCorePerformanceLib\r
+  FILE_GUID                      = 36290D10-0F47-42c1-BBCE-E191C7928DCF\r
+  MODULE_TYPE                    = SMM_CORE\r
+  VERSION_STRING                 = 1.0\r
+  PI_SPECIFICATION_VERSION       = 0x0001000A\r
+  LIBRARY_CLASS                  = PerformanceLib|SMM_CORE\r
+\r
+  CONSTRUCTOR                    = SmmCorePerformanceLibConstructor\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  SmmCorePerformanceLib.c\r
+  SmmCorePerformanceLibInternal.h\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+\r
+\r
+[LibraryClasses]\r
+  MemoryAllocationLib\r
+  UefiBootServicesTableLib\r
+  PcdLib\r
+  TimerLib\r
+  BaseMemoryLib\r
+  BaseLib\r
+  DebugLib\r
+  SynchronizationLib\r
+\r
+[Protocols]\r
+  gEfiSmmBase2ProtocolGuid                  ## CONSUMES\r
+  gEfiSmmAccess2ProtocolGuid                ## CONSUMES\r
+\r
+[Guids]\r
+  gSmmPerformanceProtocolGuid               ## PRODUCES\r
+\r
+[Pcd]\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdMaxPeiPerformanceLogEntries\r
+  gEfiMdePkgTokenSpaceGuid.PcdPerformanceLibraryPropertyMask\r
diff --git a/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLibInternal.h b/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLibInternal.h
new file mode 100644 (file)
index 0000000..159d92b
--- /dev/null
@@ -0,0 +1,133 @@
+/** @file\r
+  Master header files for SmmCorePerformanceLib instance.\r
+\r
+  This header file holds the prototypes of the Performance Protocol published by this\r
+  library instance at its constructor.\r
+\r
+Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+  \r
+#ifndef _SMM_CORE_PERFORMANCE_LIB_INTERNAL_H_\r
+#define _SMM_CORE_PERFORMANCE_LIB_INTERNAL_H_\r
+\r
+\r
+#include <Guid/Performance.h>\r
+\r
+#include <Library/SmmServicesTableLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PerformanceLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/TimerLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Library/UefiLib.h>                   \r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/SynchronizationLib.h>\r
+\r
+#include <Protocol/SmmBase2.h>\r
+#include <Protocol/SmmAccess2.h>\r
+\r
+//\r
+// Interface declarations for Performance Protocol.\r
+//\r
+/**\r
+  Adds a record at the end of the performance measurement log\r
+  that records the start time of a performance measurement.\r
+\r
+  Adds a record to the end of the performance measurement log\r
+  that contains the Handle, Token, and Module.\r
+  The end time of the new record must be set to zero.\r
+  If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
+  If TimeStamp is zero, the start time in the record is filled in with the value\r
+  read from the current time stamp.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval EFI_SUCCESS             The data was read correctly from the device.\r
+  @retval EFI_OUT_OF_RESOURCES    There are not enough resources to record the measurement.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+StartGauge (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  );\r
+\r
+/**\r
+  Searches the performance measurement log from the beginning of the log\r
+  for the first matching record that contains a zero end time and fills in a valid end time.\r
+\r
+  Searches the performance measurement log from the beginning of the log\r
+  for the first record that matches Handle, Token, and Module and has an end time value of zero.\r
+  If the record can not be found then return EFI_NOT_FOUND.\r
+  If the record is found and TimeStamp is not zero,\r
+  then the end time in the record is filled in with the value specified by TimeStamp.\r
+  If the record is found and TimeStamp is zero, then the end time in the matching record\r
+  is filled in with the current time stamp value.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval EFI_SUCCESS             The end of  the measurement was recorded.\r
+  @retval EFI_NOT_FOUND           The specified measurement record could not be found.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EndGauge (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  );\r
+\r
+/**\r
+  Retrieves a previously logged performance measurement.\r
+\r
+  Retrieves the performance log entry from the performance log specified by LogEntryKey.\r
+  If it stands for a valid entry, then EFI_SUCCESS is returned and\r
+  GaugeDataEntry stores the pointer to that entry.\r
+\r
+  @param  LogEntryKey             The key for the previous performance measurement log entry.\r
+                                  If 0, then the first performance measurement log entry is retrieved.\r
+  @param  GaugeDataEntry          The indirect pointer to the gauge data entry specified by LogEntryKey\r
+                                  if the retrieval is successful.\r
+\r
+  @retval EFI_SUCCESS             The GuageDataEntry is successfully found based on LogEntryKey.\r
+  @retval EFI_NOT_FOUND           The LogEntryKey is the last entry (equals to the total entry number).\r
+  @retval EFI_INVALIDE_PARAMETER  The LogEntryKey is not a valid entry (greater than the total entry number).\r
+  @retval EFI_INVALIDE_PARAMETER  GaugeDataEntry is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetGauge (\r
+  IN  UINTN               LogEntryKey,\r
+  OUT GAUGE_DATA_ENTRY    **GaugeDataEntry\r
+  );\r
+\r
+\r
+#endif\r
diff --git a/MdeModulePkg/Library/SmmPerformanceLib/SmmPerformanceLib.c b/MdeModulePkg/Library/SmmPerformanceLib/SmmPerformanceLib.c
new file mode 100644 (file)
index 0000000..982d8ac
--- /dev/null
@@ -0,0 +1,290 @@
+/** @file\r
+  Performance Library used in SMM phase.\r
+\r
+  This library instance provides infrastructure for SMM drivers to log performance\r
+  data. It consumes SMM Performance Protocol published by SmmCorePerformanceLib\r
+  to log performance data. If Performance Protocol is not available, it does not log any\r
+  performance information.\r
+\r
+  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+\r
+#include <Guid/Performance.h>\r
+\r
+#include <Library/PerformanceLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/SmmServicesTableLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+\r
+#include <Protocol/SmmCommunication.h>\r
+\r
+//\r
+// The cached performance protocol interface.\r
+//\r
+PERFORMANCE_PROTOCOL  *mPerformance = NULL;\r
+BOOLEAN               mPerformanceMeasurementEnabled;\r
+\r
+\r
+/**\r
+  The constructor function initializes Performance infrastructure for DXE phase.\r
+\r
+  The constructor function publishes Performance protocol, allocates memory to log DXE performance\r
+  and merges PEI performance data to DXE performance log.\r
+  It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.\r
+\r
+  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
+  @param  SystemTable   A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmPerformanceLibConstructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  \r
+  mPerformanceMeasurementEnabled =  (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  The constructor function caches the pointer to Performance protocol.\r
+\r
+  The constructor function locates SMM erformance protocol from the SMM protocol database.\r
+  It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.\r
+\r
+  @retval EFI_SUCCESS     Performance protocol is successfully located.\r
+  @retval Other           Performance protocol is not located to log performance.\r
+\r
+**/\r
+EFI_STATUS\r
+GetPerformanceProtocol (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  PERFORMANCE_PROTOCOL  *Performance;\r
+\r
+  if (mPerformance != NULL) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  Status = gSmst->SmmLocateProtocol (&gSmmPerformanceProtocolGuid, NULL, (VOID **) &Performance);\r
+  if (!EFI_ERROR (Status)) {\r
+    ASSERT (Performance != NULL);\r
+    //\r
+    // Cache performance protocol.\r
+    //\r
+    mPerformance = Performance;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Creates a record for the beginning of a performance measurement.\r
+\r
+  Creates a record that contains the Handle, Token, and Module.\r
+  If TimeStamp is not zero, then TimeStamp is added to the record as the start time.\r
+  If TimeStamp is zero, then this function reads the current time stamp\r
+  and adds that time stamp value to the record as the start time.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval RETURN_SUCCESS          The start of the measurement was recorded.\r
+  @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+StartPerformanceMeasurement (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = GetPerformanceProtocol ();\r
+  if (EFI_ERROR (Status)) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Status = mPerformance->StartGauge (Handle, Token, Module, TimeStamp);\r
+\r
+  return (RETURN_STATUS) Status;\r
+}\r
+\r
+/**\r
+  Fills in the end time of a performance measurement.\r
+\r
+  Looks up the record that matches Handle, Token, and Module.\r
+  If the record can not be found then return RETURN_NOT_FOUND.\r
+  If the record is found and TimeStamp is not zero,\r
+  then TimeStamp is added to the record as the end time.\r
+  If the record is found and TimeStamp is zero, then this function reads\r
+  the current time stamp and adds that time stamp value to the record as the end time.\r
+  If this function is called multiple times for the same record, then the end time is overwritten.\r
+\r
+  @param  Handle                  Pointer to environment specific context used\r
+                                  to identify the component being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string\r
+                                  that identifies the component being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string\r
+                                  that identifies the module being measured.\r
+  @param  TimeStamp               64-bit time stamp.\r
+\r
+  @retval RETURN_SUCCESS          The end of  the measurement was recorded.\r
+  @retval RETURN_NOT_FOUND        The specified measurement record could not be found.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+EndPerformanceMeasurement (\r
+  IN CONST VOID   *Handle,  OPTIONAL\r
+  IN CONST CHAR8  *Token,   OPTIONAL\r
+  IN CONST CHAR8  *Module,  OPTIONAL\r
+  IN UINT64       TimeStamp\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = GetPerformanceProtocol ();\r
+  if (EFI_ERROR (Status)) {\r
+    return RETURN_NOT_FOUND;\r
+  }\r
+\r
+  Status = mPerformance->EndGauge (Handle, Token, Module, TimeStamp);\r
+\r
+  return (RETURN_STATUS) Status;\r
+}\r
+\r
+/**\r
+  Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
+\r
+  Attempts to retrieve the performance log entry specified by LogEntryKey.  If LogEntryKey is\r
+  zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
+  and the key for the second entry in the log is returned.  If the performance log is empty,\r
+  then no entry is retrieved and zero is returned.  If LogEntryKey is not zero, then the performance\r
+  log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
+  returned.  If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
+  retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
+  log is returned.  If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
+  is retrieved and zero is returned.  In the cases where a performance log entry can be returned,\r
+  the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
+  If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
+  If Handle is NULL, then ASSERT().\r
+  If Token is NULL, then ASSERT().\r
+  If Module is NULL, then ASSERT().\r
+  If StartTimeStamp is NULL, then ASSERT().\r
+  If EndTimeStamp is NULL, then ASSERT().\r
+\r
+  @param  LogEntryKey             On entry, the key of the performance measurement log entry to retrieve.\r
+                                  0, then the first performance measurement log entry is retrieved.\r
+                                  On exit, the key of the next performance log entry.\r
+  @param  Handle                  Pointer to environment specific context used to identify the component\r
+                                  being measured.\r
+  @param  Token                   Pointer to a Null-terminated ASCII string that identifies the component\r
+                                  being measured.\r
+  @param  Module                  Pointer to a Null-terminated ASCII string that identifies the module\r
+                                  being measured.\r
+  @param  StartTimeStamp          Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was started.\r
+  @param  EndTimeStamp            Pointer to the 64-bit time stamp that was recorded when the measurement\r
+                                  was ended.\r
+\r
+  @return The key for the next performance log entry (in general case).\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+GetPerformanceMeasurement (\r
+  IN  UINTN       LogEntryKey,\r
+  OUT CONST VOID  **Handle,\r
+  OUT CONST CHAR8 **Token,\r
+  OUT CONST CHAR8 **Module,\r
+  OUT UINT64      *StartTimeStamp,\r
+  OUT UINT64      *EndTimeStamp\r
+  )\r
+{\r
+  EFI_STATUS        Status;\r
+  GAUGE_DATA_ENTRY  *GaugeData;\r
+       \r
+  GaugeData = NULL;\r
+\r
+  ASSERT (Handle != NULL);\r
+  ASSERT (Token != NULL);\r
+  ASSERT (Module != NULL);\r
+  ASSERT (StartTimeStamp != NULL);\r
+  ASSERT (EndTimeStamp != NULL);\r
+\r
+  Status = GetPerformanceProtocol ();\r
+  if (EFI_ERROR (Status)) {\r
+    return 0;\r
+  }\r
+\r
+  Status = mPerformance->GetGauge (LogEntryKey++, &GaugeData);\r
+\r
+  //\r
+  // Make sure that LogEntryKey is a valid log entry key,\r
+  //\r
+  ASSERT (Status != EFI_INVALID_PARAMETER);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // The LogEntryKey is the last entry (equals to the total entry number).\r
+    //\r
+    return 0;\r
+  }\r
+\r
+  ASSERT (GaugeData != NULL);\r
+\r
+  *Handle         = (VOID *) (UINTN) GaugeData->Handle;\r
+  *Token          = GaugeData->Token;\r
+  *Module         = GaugeData->Module;\r
+  *StartTimeStamp = GaugeData->StartTimeStamp;\r
+  *EndTimeStamp   = GaugeData->EndTimeStamp;\r
+\r
+}\r
+\r
+/**\r
+  Returns TRUE if the performance measurement macros are enabled.\r
+\r
+  This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+  PcdPerformanceLibraryPropertyMask is set.  Otherwise FALSE is returned.\r
+\r
+  @retval TRUE                    The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+                                  PcdPerformanceLibraryPropertyMask is set.\r
+  @retval FALSE                   The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
+                                  PcdPerformanceLibraryPropertyMask is clear.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PerformanceMeasurementEnabled (\r
+  VOID\r
+  )\r
+{\r
+  return mPerformanceMeasurementEnabled;\r
+}\r
diff --git a/MdeModulePkg/Library/SmmPerformanceLib/SmmPerformanceLib.inf b/MdeModulePkg/Library/SmmPerformanceLib/SmmPerformanceLib.inf
new file mode 100644 (file)
index 0000000..9397fec
--- /dev/null
@@ -0,0 +1,63 @@
+## @file\r
+#  Performance library instance used in SMM phase.\r
+#  \r
+#  This library instance provides infrastructure for SMM drivers to log performance\r
+#  data. It consumes SMM Performance Protocol published by SmmCorePerformanceLib\r
+#  to log performance data. If Performance Protocol is not available, it does not log any\r
+#  performance information.\r
+#  \r
+#  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+#  This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution.  The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  \r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#  \r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = SmmPerformanceLib\r
+  FILE_GUID                      = 1EDD13E6-D0CD-4432-A692-FF65C9B4F039\r
+  MODULE_TYPE                    = DXE_SMM_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = PerformanceLib|DXE_SMM_DRIVER\r
+\r
+  CONSTRUCTOR                    = SmmPerformanceLibConstructor\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  SmmPerformanceLib.c\r
+\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+\r
+\r
+[LibraryClasses]\r
+  PcdLib\r
+  SmmServicesTableLib\r
+  DebugLib\r
+  BaseMemoryLib\r
+\r
+[Protocols]\r
+  gEfiSmmCommunicationProtocolGuid      ## CONSUMES\r
+\r
+\r
+[Guids]\r
+  gSmmPerformanceProtocolGuid           ## CONSUMES\r
+\r
+\r
+[Pcd]\r
+  gEfiMdePkgTokenSpaceGuid.PcdPerformanceLibraryPropertyMask\r
+\r
+[Depex]\r
+  gEfiSmmCommunicationProtocolGuid\r
index c1e46fa356615a48f3702031cced8c1145bc2a6f..045fd4152a7f5c91605917a0312c7ac6957d943a 100644 (file)
   ## Performance protocol guid that also acts as the performance HOB guid and performance variable GUID\r
   #  Include/Guid/Performance.h\r
   gPerformanceProtocolGuid       = { 0x76B6BDFA, 0x2ACD, 0x4462, { 0x9E, 0x3F, 0xCB, 0x58, 0xC9, 0x69, 0xD9, 0x37 }}\r
+  gSmmPerformanceProtocolGuid    = { 0xf866226a, 0xeaa5, 0x4f5a, { 0xa9, 0xa, 0x6c, 0xfb, 0xa5, 0x7c, 0x58, 0x8e } }\r
 \r
   ## Guid is defined for CRC32 encapsulation scheme.\r
   #  Include/Guid/Crc32GuidedSectionExtraction.h\r
index 7b583eedba7c0089902c72d46063230065058a19..89cf09e2c5b2fe688b3885638494a4150c8fd583 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # EFI/PI Reference Module Package for All Architectures\r
 #\r
-# Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
 #\r
 #    This program and the accompanying materials\r
 #    are licensed and made available under the terms and conditions of the BSD License\r
   MdeModulePkg/Library/SmmReportStatusCodeLib/SmmReportStatusCodeLib.inf\r
   MdeModulePkg/Universal/StatusCodeHandler/Smm/StatusCodeHandlerSmm.inf\r
   MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf\r
+  MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.inf\r
+  MdeModulePkg/Library/SmmPerformanceLib/SmmPerformanceLib.inf\r
+  MdeModulePkg/Library/DxeSmmPerformanceLib/DxeSmmPerformanceLib.inf\r
+\r