]> git.proxmox.com Git - mirror_edk2.git/blob - 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
1 /** @file
2 This module collects performance data for SMM driver boot records and S3 Suspend Performance Record.
3
4 This module registers report status code listener to collect performance data
5 for SMM driver boot records and S3 Suspend Performance Record.
6
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
12
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.
15
16 **/
17
18 #include <PiSmm.h>
19
20 #include <IndustryStandard/Acpi50.h>
21
22 #include <Protocol/SmmReportStatusCodeHandler.h>
23 #include <Protocol/SmmAccess2.h>
24
25 #include <Guid/FirmwarePerformance.h>
26
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>
37
38 #define EXTENSION_RECORD_SIZE 0x1000
39
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;
46
47 EFI_SMRAM_DESCRIPTOR *mSmramRanges;
48 UINTN mSmramRangeCount;
49 SPIN_LOCK mSmmFpdtLock;
50 BOOLEAN mSmramIsOutOfResource = FALSE;
51
52 /**
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.
55
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
64 different callers.
65 @param[in] Data This optional parameter may be used to pass additional data.
66
67 @retval EFI_SUCCESS Status code is what we expected.
68 @retval EFI_UNSUPPORTED Status code not supported.
69
70 **/
71 EFI_STATUS
72 EFIAPI
73 FpdtStatusCodeListenerSmm (
74 IN EFI_STATUS_CODE_TYPE CodeType,
75 IN EFI_STATUS_CODE_VALUE Value,
76 IN UINT32 Instance,
77 IN EFI_GUID *CallerId,
78 IN EFI_STATUS_CODE_DATA *Data
79 )
80 {
81 EFI_STATUS Status;
82 UINT64 CurrentTime;
83 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord;
84 UINT8 *NewRecordBuffer;
85
86 //
87 // Check whether status code is what we are interested in.
88 //
89 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {
90 return EFI_UNSUPPORTED;
91 }
92
93 //
94 // Collect one or more Boot records in boot time
95 //
96 if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
97 AcquireSpinLock (&mSmmFpdtLock);
98
99 if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {
100 //
101 // Try to allocate big SMRAM data to store Boot record.
102 //
103 if (mSmramIsOutOfResource) {
104 return EFI_OUT_OF_RESOURCES;
105 }
106 NewRecordBuffer = AllocatePool (mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE);
107 if (NewRecordBuffer == NULL) {
108 mSmramIsOutOfResource = TRUE;
109 return EFI_OUT_OF_RESOURCES;
110 }
111 CopyMem (NewRecordBuffer, mBootRecordBuffer, mBootRecordSize);
112 mBootRecordBuffer = NewRecordBuffer;
113 mBootRecordMaxSize = mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE;
114 }
115 //
116 // Save boot record into the temp memory space.
117 //
118 CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);
119 mBootRecordSize += Data->Size;
120
121 ReleaseSpinLock (&mSmmFpdtLock);
122 return EFI_SUCCESS;
123 }
124
125 if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&
126 (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {
127 return EFI_UNSUPPORTED;
128 }
129
130 //
131 // Retrieve current time.
132 //
133 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());
134
135 if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {
136 //
137 // S3 Suspend started, record the performance data and return.
138 //
139 mSuspendStartTime = CurrentTime;
140 return EFI_SUCCESS;
141 }
142
143 //
144 // We are going to S3 sleep, record S3 Suspend End performance data.
145 //
146 S3SuspendRecord.SuspendStart = mSuspendStartTime;
147 S3SuspendRecord.SuspendEnd = CurrentTime;
148
149 //
150 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
151 //
152 if (!mS3SuspendLockBoxSaved) {
153 Status = SaveLockBox (
154 &gEfiFirmwarePerformanceGuid,
155 &S3SuspendRecord,
156 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
157 );
158 ASSERT_EFI_ERROR (Status);
159
160 mS3SuspendLockBoxSaved = TRUE;
161 } else {
162 Status = UpdateLockBox (
163 &gEfiFirmwarePerformanceGuid,
164 0,
165 &S3SuspendRecord,
166 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
167 );
168 ASSERT_EFI_ERROR (Status);
169 }
170
171 return EFI_SUCCESS;
172 }
173
174 /**
175 This function check if the address is in SMRAM.
176
177 @param Buffer the buffer address to be checked.
178 @param Length the buffer length to be checked.
179
180 @retval TRUE this address is in SMRAM.
181 @retval FALSE this address is NOT in SMRAM.
182 **/
183 BOOLEAN
184 InternalIsAddressInSmram (
185 IN EFI_PHYSICAL_ADDRESS Buffer,
186 IN UINT64 Length
187 )
188 {
189 UINTN Index;
190
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))) {
194 return TRUE;
195 }
196 }
197
198 return FALSE;
199 }
200
201 /**
202 Communication service SMI Handler entry.
203
204 This SMI handler provides services for report SMM boot records.
205
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.
212
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.
217 **/
218 EFI_STATUS
219 EFIAPI
220 FpdtSmiHandler (
221 IN EFI_HANDLE DispatchHandle,
222 IN CONST VOID *RegisterContext,
223 IN OUT VOID *CommBuffer,
224 IN OUT UINTN *CommBufferSize
225 )
226 {
227 EFI_STATUS Status;
228 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;
229
230 ASSERT (CommBuffer != NULL);
231 if (CommBuffer == NULL || *CommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {
232 return EFI_INVALID_PARAMETER;
233 }
234
235 Status = EFI_SUCCESS;
236 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;
237
238 switch (SmmCommData->Function) {
239 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :
240 SmmCommData->BootRecordSize = mBootRecordSize;
241 break;
242
243 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :
244 if (SmmCommData->BootRecordData == NULL || SmmCommData->BootRecordSize < mBootRecordSize) {
245 Status = EFI_INVALID_PARAMETER;
246 break;
247 }
248
249 //
250 // Sanity check
251 //
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;
256 break;
257 }
258
259 CopyMem (
260 (UINT8*)SmmCommData->BootRecordData,
261 mBootRecordBuffer,
262 mBootRecordSize
263 );
264 break;
265
266 default:
267 ASSERT (FALSE);
268 Status = EFI_UNSUPPORTED;
269 }
270
271 SmmCommData->ReturnStatus = Status;
272 return EFI_SUCCESS;
273 }
274
275 /**
276 The module Entry Point of the Firmware Performance Data Table SMM driver.
277
278 @param[in] ImageHandle The firmware allocated handle for the EFI image.
279 @param[in] SystemTable A pointer to the EFI System Table.
280
281 @retval EFI_SUCCESS The entry point is executed successfully.
282 @retval Other Some error occurs when executing this entry point.
283
284 **/
285 EFI_STATUS
286 EFIAPI
287 FirmwarePerformanceSmmEntryPoint (
288 IN EFI_HANDLE ImageHandle,
289 IN EFI_SYSTEM_TABLE *SystemTable
290 )
291 {
292 EFI_STATUS Status;
293 EFI_HANDLE Handle;
294 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;
295 UINTN Size;
296
297 //
298 // Initialize spin lock
299 //
300 InitializeSpinLock (&mSmmFpdtLock);
301
302 //
303 // Get SMM Report Status Code Handler Protocol.
304 //
305 Status = gSmst->SmmLocateProtocol (
306 &gEfiSmmRscHandlerProtocolGuid,
307 NULL,
308 (VOID **) &mRscHandlerProtocol
309 );
310 ASSERT_EFI_ERROR (Status);
311
312 //
313 // Register report status code listener for BootRecords and S3 Suspend Start and End.
314 //
315 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);
316 ASSERT_EFI_ERROR (Status);
317
318 //
319 // Get SMRAM information
320 //
321 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);
322 ASSERT_EFI_ERROR (Status);
323
324 Size = 0;
325 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
326 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
327
328 Status = gSmst->SmmAllocatePool (
329 EfiRuntimeServicesData,
330 Size,
331 (VOID **)&mSmramRanges
332 );
333 ASSERT_EFI_ERROR (Status);
334
335 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
336 ASSERT_EFI_ERROR (Status);
337
338 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
339
340 //
341 // Register SMI handler.
342 //
343 Handle = NULL;
344 Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);
345 ASSERT_EFI_ERROR (Status);
346
347 return Status;
348 }