]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
Enhance ACPI FPDT DXE and SMM driver to accept the extension boot records.
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / FirmwarePerformanceDataTableSmm / FirmwarePerformanceSmm.c
index 19a2fe68a95c221c7996f1754aa1d7a3c8686467..6de22d4a1cd2ff3384011985b71ec05bfbb81fb3 100644 (file)
@@ -1,10 +1,10 @@
 /** @file\r
-  This module update S3 Suspend Performance Record in ACPI Firmware Performance Data Table.\r
+  This module collects performance data for SMM driver boot records and S3 Suspend Performance Record.\r
 \r
-  This module register report status code listener to collect performance data\r
-  for S3 Suspend Performance Record.\r
+  This module registers report status code listener to collect performance data\r
+  for SMM driver boot records and S3 Suspend Performance Record.\r
 \r
-  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2011 - 2012, 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
@@ -20,6 +20,7 @@
 #include <IndustryStandard/Acpi50.h>\r
 \r
 #include <Protocol/SmmReportStatusCodeHandler.h>\r
+#include <Protocol/SmmAccess2.h>\r
 \r
 #include <Guid/FirmwarePerformance.h>\r
 \r
 #include <Library/TimerLib.h>\r
 #include <Library/LockBoxLib.h>\r
 #include <Library/PcdLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/SynchronizationLib.h>\r
+\r
+#define EXTENSION_RECORD_SIZE     0x1000\r
 \r
 EFI_SMM_RSC_HANDLER_PROTOCOL  *mRscHandlerProtocol    = NULL;\r
 UINT64                        mSuspendStartTime       = 0;\r
 BOOLEAN                       mS3SuspendLockBoxSaved  = FALSE;\r
+UINT32                        mBootRecordSize = 0;\r
+UINT32                        mBootRecordMaxSize = 0;\r
+UINT8                         *mBootRecordBuffer = NULL;\r
+\r
+EFI_SMRAM_DESCRIPTOR          *mSmramRanges;\r
+UINTN                         mSmramRangeCount;\r
+SPIN_LOCK                     mSmmFpdtLock;\r
+BOOLEAN                       mSmramIsOutOfResource = FALSE;\r
 \r
 /**\r
   Report status code listener for SMM. This is used to record the performance\r
@@ -66,6 +81,7 @@ FpdtStatusCodeListenerSmm (
   EFI_STATUS                           Status;\r
   UINT64                               CurrentTime;\r
   EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD  S3SuspendRecord;\r
+  UINT8                                *NewRecordBuffer;\r
 \r
   //\r
   // Check whether status code is what we are interested in.\r
@@ -73,6 +89,39 @@ FpdtStatusCodeListenerSmm (
   if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {\r
     return EFI_UNSUPPORTED;\r
   }\r
+  \r
+  //\r
+  // Collect one or more Boot records in boot time\r
+  //\r
+  if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {\r
+    AcquireSpinLock (&mSmmFpdtLock);\r
+    \r
+    if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {\r
+      //\r
+      // Try to allocate big SMRAM data to store Boot record. \r
+      //\r
+      if (mSmramIsOutOfResource) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+      NewRecordBuffer = AllocatePool (mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE); \r
+      if (NewRecordBuffer == NULL) {\r
+        mSmramIsOutOfResource = TRUE;\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+      CopyMem (NewRecordBuffer, mBootRecordBuffer, mBootRecordSize);\r
+      mBootRecordBuffer  = NewRecordBuffer;\r
+      mBootRecordMaxSize = mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE;\r
+    }\r
+    //\r
+    // Save boot record into the temp memory space.\r
+    //\r
+    CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);\r
+    mBootRecordSize += Data->Size;\r
+    \r
+    ReleaseSpinLock (&mSmmFpdtLock);\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
   if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&\r
       (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {\r
     return EFI_UNSUPPORTED;\r
@@ -122,6 +171,107 @@ FpdtStatusCodeListenerSmm (
   return EFI_SUCCESS;\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
+InternalIsAddressInSmram (\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 report SMM boot records. \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 should still be called.\r
+  @retval EFI_INVALID_PARAMETER  The interrupt parameter is not valid. \r
+  @retval EFI_ACCESS_DENIED      The interrupt buffer can't be written. \r
+  @retval EFI_UNSUPPORTED        The interrupt is not supported. \r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FpdtSmiHandler (\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_BOOT_RECORD_COMMUNICATE  *SmmCommData;\r
+  \r
+  ASSERT (CommBuffer != NULL);\r
+  if (CommBuffer == NULL || *CommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status = EFI_SUCCESS;\r
+  SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;\r
+\r
+  switch (SmmCommData->Function) {\r
+    case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :\r
+       SmmCommData->BootRecordSize = mBootRecordSize;\r
+       break;\r
+\r
+    case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :\r
+       if (SmmCommData->BootRecordData == NULL || SmmCommData->BootRecordSize < mBootRecordSize) {\r
+         Status = EFI_INVALID_PARAMETER;\r
+         break;\r
+       } \r
+          \r
+       //\r
+       // Sanity check\r
+       //\r
+       SmmCommData->BootRecordSize = mBootRecordSize;\r
+       if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)SmmCommData->BootRecordData, mBootRecordSize)) {\r
+         DEBUG ((EFI_D_ERROR, "Smm Data buffer is in SMRAM!\n"));\r
+         Status = EFI_ACCESS_DENIED;\r
+         break;\r
+       }\r
+\r
+       CopyMem (\r
+         (UINT8*)SmmCommData->BootRecordData, \r
+         mBootRecordBuffer, \r
+         mBootRecordSize\r
+         );\r
+       break;\r
+\r
+    default:\r
+       ASSERT (FALSE);\r
+       Status = EFI_UNSUPPORTED;\r
+  }\r
+\r
+  SmmCommData->ReturnStatus = Status;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 /**\r
   The module Entry Point of the Firmware Performance Data Table SMM driver.\r
 \r
@@ -139,27 +289,60 @@ FirmwarePerformanceSmmEntryPoint (
   IN EFI_SYSTEM_TABLE    *SystemTable\r
   )\r
 {\r
-  if (FeaturePcdGet (PcdFirmwarePerformanceDataTableS3Support)) {\r
-    EFI_STATUS  Status;\r
+  EFI_STATUS                Status;\r
+  EFI_HANDLE                Handle;\r
+  EFI_SMM_ACCESS2_PROTOCOL  *SmmAccess;\r
+  UINTN                     Size;\r
 \r
-    //\r
-    // Get SMM Report Status Code Handler Protocol.\r
-    //\r
-    Status = gSmst->SmmLocateProtocol (\r
-                      &gEfiSmmRscHandlerProtocolGuid,\r
-                      NULL,\r
-                      (VOID **) &mRscHandlerProtocol\r
-                      );\r
-    ASSERT_EFI_ERROR (Status);\r
+  //\r
+  // Initialize spin lock\r
+  //\r
+  InitializeSpinLock (&mSmmFpdtLock); \r
+   \r
+  //\r
+  // Get SMM Report Status Code Handler Protocol.\r
+  //\r
+  Status = gSmst->SmmLocateProtocol (\r
+                    &gEfiSmmRscHandlerProtocolGuid,\r
+                    NULL,\r
+                    (VOID **) &mRscHandlerProtocol\r
+                    );\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
-    //\r
-    // Register report status code listener for S3 Suspend Start and End.\r
-    //\r
-    Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);\r
-    ASSERT_EFI_ERROR (Status);\r
+  //\r
+  // Register report status code listener for BootRecords and S3 Suspend Start and End.\r
+  //\r
+  Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
-    return Status;\r
-  } else {\r
-    return EFI_UNSUPPORTED;\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
+  // Register SMI handler.\r
+  //\r
+  Handle = NULL;\r
+  Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
 }\r