2 This module collects performance data for SMM driver boot records and S3 Suspend Performance Record.
4 This module registers report status code listener to collect performance data
5 for SMM driver boot records and S3 Suspend Performance Record.
7 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <IndustryStandard/Acpi50.h>
22 #include <Protocol/SmmReportStatusCodeHandler.h>
23 #include <Protocol/SmmAccess2.h>
25 #include <Guid/FirmwarePerformance.h>
27 #include <Library/SmmServicesTableLib.h>
28 #include <Library/BaseLib.h>
29 #include <Library/DebugLib.h>
30 #include <Library/TimerLib.h>
31 #include <Library/LockBoxLib.h>
32 #include <Library/PcdLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/MemoryAllocationLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/SynchronizationLib.h>
38 #define EXTENSION_RECORD_SIZE 0x1000
40 EFI_SMM_RSC_HANDLER_PROTOCOL
*mRscHandlerProtocol
= NULL
;
41 UINT64 mSuspendStartTime
= 0;
42 BOOLEAN mS3SuspendLockBoxSaved
= FALSE
;
43 UINT32 mBootRecordSize
= 0;
44 UINT32 mBootRecordMaxSize
= 0;
45 UINT8
*mBootRecordBuffer
= NULL
;
47 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
;
48 UINTN mSmramRangeCount
;
49 SPIN_LOCK mSmmFpdtLock
;
50 BOOLEAN mSmramIsOutOfResource
= FALSE
;
53 Report status code listener for SMM. This is used to record the performance
54 data for S3 Suspend Start and S3 Suspend End in FPDT.
56 @param[in] CodeType Indicates the type of status code being reported.
57 @param[in] Value Describes the current status of a hardware or software entity.
58 This included information about the class and subclass that is used to
59 classify the entity as well as an operation.
60 @param[in] Instance The enumeration of a hardware or software entity within
61 the system. Valid instance numbers start with 1.
62 @param[in] CallerId This optional parameter may be used to identify the caller.
63 This parameter allows the status code driver to apply different rules to
65 @param[in] Data This optional parameter may be used to pass additional data.
67 @retval EFI_SUCCESS Status code is what we expected.
68 @retval EFI_UNSUPPORTED Status code not supported.
73 FpdtStatusCodeListenerSmm (
74 IN EFI_STATUS_CODE_TYPE CodeType
,
75 IN EFI_STATUS_CODE_VALUE Value
,
77 IN EFI_GUID
*CallerId
,
78 IN EFI_STATUS_CODE_DATA
*Data
83 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord
;
84 UINT8
*NewRecordBuffer
;
87 // Check whether status code is what we are interested in.
89 if ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) != EFI_PROGRESS_CODE
) {
90 return EFI_UNSUPPORTED
;
94 // Collect one or more Boot records in boot time
96 if (Data
!= NULL
&& CompareGuid (&Data
->Type
, &gEfiFirmwarePerformanceGuid
)) {
97 AcquireSpinLock (&mSmmFpdtLock
);
99 if (mBootRecordSize
+ Data
->Size
> mBootRecordMaxSize
) {
101 // Try to allocate big SMRAM data to store Boot record.
103 if (mSmramIsOutOfResource
) {
104 return EFI_OUT_OF_RESOURCES
;
106 NewRecordBuffer
= AllocatePool (mBootRecordSize
+ Data
->Size
+ EXTENSION_RECORD_SIZE
);
107 if (NewRecordBuffer
== NULL
) {
108 mSmramIsOutOfResource
= TRUE
;
109 return EFI_OUT_OF_RESOURCES
;
111 CopyMem (NewRecordBuffer
, mBootRecordBuffer
, mBootRecordSize
);
112 mBootRecordBuffer
= NewRecordBuffer
;
113 mBootRecordMaxSize
= mBootRecordSize
+ Data
->Size
+ EXTENSION_RECORD_SIZE
;
116 // Save boot record into the temp memory space.
118 CopyMem (mBootRecordBuffer
+ mBootRecordSize
, Data
+ 1, Data
->Size
);
119 mBootRecordSize
+= Data
->Size
;
121 ReleaseSpinLock (&mSmmFpdtLock
);
125 if ((Value
!= PcdGet32 (PcdProgressCodeS3SuspendStart
)) &&
126 (Value
!= PcdGet32 (PcdProgressCodeS3SuspendEnd
))) {
127 return EFI_UNSUPPORTED
;
131 // Retrieve current time.
133 CurrentTime
= GetTimeInNanoSecond (GetPerformanceCounter ());
135 if (Value
== PcdGet32 (PcdProgressCodeS3SuspendStart
)) {
137 // S3 Suspend started, record the performance data and return.
139 mSuspendStartTime
= CurrentTime
;
144 // We are going to S3 sleep, record S3 Suspend End performance data.
146 S3SuspendRecord
.SuspendStart
= mSuspendStartTime
;
147 S3SuspendRecord
.SuspendEnd
= CurrentTime
;
150 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
152 if (!mS3SuspendLockBoxSaved
) {
153 Status
= SaveLockBox (
154 &gEfiFirmwarePerformanceGuid
,
156 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD
)
158 ASSERT_EFI_ERROR (Status
);
160 mS3SuspendLockBoxSaved
= TRUE
;
162 Status
= UpdateLockBox (
163 &gEfiFirmwarePerformanceGuid
,
166 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD
)
168 ASSERT_EFI_ERROR (Status
);
175 This function check if the address is in SMRAM.
177 @param Buffer the buffer address to be checked.
178 @param Length the buffer length to be checked.
180 @retval TRUE this address is in SMRAM.
181 @retval FALSE this address is NOT in SMRAM.
184 InternalIsAddressInSmram (
185 IN EFI_PHYSICAL_ADDRESS Buffer
,
191 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
192 if (((Buffer
>= mSmramRanges
[Index
].CpuStart
) && (Buffer
< mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
)) ||
193 ((mSmramRanges
[Index
].CpuStart
>= Buffer
) && (mSmramRanges
[Index
].CpuStart
< Buffer
+ Length
))) {
202 Communication service SMI Handler entry.
204 This SMI handler provides services for report SMM boot records.
206 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
207 @param[in] RegisterContext Points to an optional handler context which was specified when the
208 handler was registered.
209 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
210 be conveyed from a non-SMM environment into an SMM environment.
211 @param[in, out] CommBufferSize The size of the CommBuffer.
213 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers should still be called.
214 @retval EFI_INVALID_PARAMETER The interrupt parameter is not valid.
215 @retval EFI_ACCESS_DENIED The interrupt buffer can't be written.
216 @retval EFI_UNSUPPORTED The interrupt is not supported.
221 IN EFI_HANDLE DispatchHandle
,
222 IN CONST VOID
*RegisterContext
,
223 IN OUT VOID
*CommBuffer
,
224 IN OUT UINTN
*CommBufferSize
228 SMM_BOOT_RECORD_COMMUNICATE
*SmmCommData
;
230 ASSERT (CommBuffer
!= NULL
);
231 if (CommBuffer
== NULL
|| *CommBufferSize
< sizeof (SMM_BOOT_RECORD_COMMUNICATE
)) {
232 return EFI_INVALID_PARAMETER
;
235 Status
= EFI_SUCCESS
;
236 SmmCommData
= (SMM_BOOT_RECORD_COMMUNICATE
*)CommBuffer
;
238 switch (SmmCommData
->Function
) {
239 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE
:
240 SmmCommData
->BootRecordSize
= mBootRecordSize
;
243 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA
:
244 if (SmmCommData
->BootRecordData
== NULL
|| SmmCommData
->BootRecordSize
< mBootRecordSize
) {
245 Status
= EFI_INVALID_PARAMETER
;
252 SmmCommData
->BootRecordSize
= mBootRecordSize
;
253 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)(UINTN
)SmmCommData
->BootRecordData
, mBootRecordSize
)) {
254 DEBUG ((EFI_D_ERROR
, "Smm Data buffer is in SMRAM!\n"));
255 Status
= EFI_ACCESS_DENIED
;
260 (UINT8
*)SmmCommData
->BootRecordData
,
268 Status
= EFI_UNSUPPORTED
;
271 SmmCommData
->ReturnStatus
= Status
;
276 The module Entry Point of the Firmware Performance Data Table SMM driver.
278 @param[in] ImageHandle The firmware allocated handle for the EFI image.
279 @param[in] SystemTable A pointer to the EFI System Table.
281 @retval EFI_SUCCESS The entry point is executed successfully.
282 @retval Other Some error occurs when executing this entry point.
287 FirmwarePerformanceSmmEntryPoint (
288 IN EFI_HANDLE ImageHandle
,
289 IN EFI_SYSTEM_TABLE
*SystemTable
294 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
298 // Initialize spin lock
300 InitializeSpinLock (&mSmmFpdtLock
);
303 // Get SMM Report Status Code Handler Protocol.
305 Status
= gSmst
->SmmLocateProtocol (
306 &gEfiSmmRscHandlerProtocolGuid
,
308 (VOID
**) &mRscHandlerProtocol
310 ASSERT_EFI_ERROR (Status
);
313 // Register report status code listener for BootRecords and S3 Suspend Start and End.
315 Status
= mRscHandlerProtocol
->Register (FpdtStatusCodeListenerSmm
);
316 ASSERT_EFI_ERROR (Status
);
319 // Get SMRAM information
321 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
322 ASSERT_EFI_ERROR (Status
);
325 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
326 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
328 Status
= gSmst
->SmmAllocatePool (
329 EfiRuntimeServicesData
,
331 (VOID
**)&mSmramRanges
333 ASSERT_EFI_ERROR (Status
);
335 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
336 ASSERT_EFI_ERROR (Status
);
338 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
341 // Register SMI handler.
344 Status
= gSmst
->SmiHandlerRegister (FpdtSmiHandler
, &gEfiFirmwarePerformanceGuid
, &Handle
);
345 ASSERT_EFI_ERROR (Status
);