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 ReleaseSpinLock (&mSmmFpdtLock
);
105 return EFI_OUT_OF_RESOURCES
;
107 NewRecordBuffer
= ReallocatePool (mBootRecordSize
, mBootRecordSize
+ Data
->Size
+ EXTENSION_RECORD_SIZE
, mBootRecordBuffer
);
108 if (NewRecordBuffer
== NULL
) {
109 ReleaseSpinLock (&mSmmFpdtLock
);
110 mSmramIsOutOfResource
= TRUE
;
111 return EFI_OUT_OF_RESOURCES
;
113 mBootRecordBuffer
= NewRecordBuffer
;
114 mBootRecordMaxSize
= mBootRecordSize
+ Data
->Size
+ EXTENSION_RECORD_SIZE
;
117 // Save boot record into the temp memory space.
119 CopyMem (mBootRecordBuffer
+ mBootRecordSize
, Data
+ 1, Data
->Size
);
120 mBootRecordSize
+= Data
->Size
;
122 ReleaseSpinLock (&mSmmFpdtLock
);
126 if ((Value
!= PcdGet32 (PcdProgressCodeS3SuspendStart
)) &&
127 (Value
!= PcdGet32 (PcdProgressCodeS3SuspendEnd
))) {
128 return EFI_UNSUPPORTED
;
132 // Retrieve current time.
134 CurrentTime
= GetTimeInNanoSecond (GetPerformanceCounter ());
136 if (Value
== PcdGet32 (PcdProgressCodeS3SuspendStart
)) {
138 // S3 Suspend started, record the performance data and return.
140 mSuspendStartTime
= CurrentTime
;
145 // We are going to S3 sleep, record S3 Suspend End performance data.
147 S3SuspendRecord
.SuspendStart
= mSuspendStartTime
;
148 S3SuspendRecord
.SuspendEnd
= CurrentTime
;
151 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
153 if (!mS3SuspendLockBoxSaved
) {
154 Status
= SaveLockBox (
155 &gEfiFirmwarePerformanceGuid
,
157 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD
)
159 ASSERT_EFI_ERROR (Status
);
161 mS3SuspendLockBoxSaved
= TRUE
;
163 Status
= UpdateLockBox (
164 &gEfiFirmwarePerformanceGuid
,
167 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD
)
169 ASSERT_EFI_ERROR (Status
);
176 This function check if the address is in SMRAM.
178 @param Buffer the buffer address to be checked.
179 @param Length the buffer length to be checked.
181 @retval TRUE this address is in SMRAM.
182 @retval FALSE this address is NOT in SMRAM.
185 InternalIsAddressInSmram (
186 IN EFI_PHYSICAL_ADDRESS Buffer
,
192 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
193 if (((Buffer
>= mSmramRanges
[Index
].CpuStart
) && (Buffer
< mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
)) ||
194 ((mSmramRanges
[Index
].CpuStart
>= Buffer
) && (mSmramRanges
[Index
].CpuStart
< Buffer
+ Length
))) {
203 Communication service SMI Handler entry.
205 This SMI handler provides services for report SMM boot records.
207 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
208 @param[in] RegisterContext Points to an optional handler context which was specified when the
209 handler was registered.
210 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
211 be conveyed from a non-SMM environment into an SMM environment.
212 @param[in, out] CommBufferSize The size of the CommBuffer.
214 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers should still be called.
215 @retval EFI_INVALID_PARAMETER The interrupt parameter is not valid.
216 @retval EFI_ACCESS_DENIED The interrupt buffer can't be written.
217 @retval EFI_UNSUPPORTED The interrupt is not supported.
222 IN EFI_HANDLE DispatchHandle
,
223 IN CONST VOID
*RegisterContext
,
224 IN OUT VOID
*CommBuffer
,
225 IN OUT UINTN
*CommBufferSize
229 SMM_BOOT_RECORD_COMMUNICATE
*SmmCommData
;
231 ASSERT (CommBuffer
!= NULL
);
232 if (CommBuffer
== NULL
|| *CommBufferSize
< sizeof (SMM_BOOT_RECORD_COMMUNICATE
)) {
233 return EFI_INVALID_PARAMETER
;
236 Status
= EFI_SUCCESS
;
237 SmmCommData
= (SMM_BOOT_RECORD_COMMUNICATE
*)CommBuffer
;
239 switch (SmmCommData
->Function
) {
240 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE
:
241 SmmCommData
->BootRecordSize
= mBootRecordSize
;
244 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA
:
245 if (SmmCommData
->BootRecordData
== NULL
|| SmmCommData
->BootRecordSize
< mBootRecordSize
) {
246 Status
= EFI_INVALID_PARAMETER
;
253 SmmCommData
->BootRecordSize
= mBootRecordSize
;
254 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS
)(UINTN
)SmmCommData
->BootRecordData
, mBootRecordSize
)) {
255 DEBUG ((EFI_D_ERROR
, "Smm Data buffer is in SMRAM!\n"));
256 Status
= EFI_ACCESS_DENIED
;
261 (UINT8
*)SmmCommData
->BootRecordData
,
269 Status
= EFI_UNSUPPORTED
;
272 SmmCommData
->ReturnStatus
= Status
;
277 The module Entry Point of the Firmware Performance Data Table SMM driver.
279 @param[in] ImageHandle The firmware allocated handle for the EFI image.
280 @param[in] SystemTable A pointer to the EFI System Table.
282 @retval EFI_SUCCESS The entry point is executed successfully.
283 @retval Other Some error occurs when executing this entry point.
288 FirmwarePerformanceSmmEntryPoint (
289 IN EFI_HANDLE ImageHandle
,
290 IN EFI_SYSTEM_TABLE
*SystemTable
295 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
299 // Initialize spin lock
301 InitializeSpinLock (&mSmmFpdtLock
);
304 // Get SMM Report Status Code Handler Protocol.
306 Status
= gSmst
->SmmLocateProtocol (
307 &gEfiSmmRscHandlerProtocolGuid
,
309 (VOID
**) &mRscHandlerProtocol
311 ASSERT_EFI_ERROR (Status
);
314 // Register report status code listener for BootRecords and S3 Suspend Start and End.
316 Status
= mRscHandlerProtocol
->Register (FpdtStatusCodeListenerSmm
);
317 ASSERT_EFI_ERROR (Status
);
320 // Get SMRAM information
322 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
323 ASSERT_EFI_ERROR (Status
);
326 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
327 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
329 Status
= gSmst
->SmmAllocatePool (
330 EfiRuntimeServicesData
,
332 (VOID
**)&mSmramRanges
334 ASSERT_EFI_ERROR (Status
);
336 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
337 ASSERT_EFI_ERROR (Status
);
339 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
342 // Register SMI handler.
345 Status
= gSmst
->SmiHandlerRegister (FpdtSmiHandler
, &gEfiFirmwarePerformanceGuid
, &Handle
);
346 ASSERT_EFI_ERROR (Status
);