]> git.proxmox.com Git - mirror_edk2.git/blame - 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
CommitLineData
0284e90c 1/** @file\r
1c0cc375 2 This module collects performance data for SMM driver boot records and S3 Suspend Performance Record.\r
0284e90c 3\r
1c0cc375
LG
4 This module registers report status code listener to collect performance data\r
5 for SMM driver boot records and S3 Suspend Performance Record.\r
0284e90c 6\r
1c0cc375 7 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>\r
0284e90c
LG
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <PiSmm.h>\r
19\r
20#include <IndustryStandard/Acpi50.h>\r
21\r
22#include <Protocol/SmmReportStatusCodeHandler.h>\r
1c0cc375 23#include <Protocol/SmmAccess2.h>\r
0284e90c
LG
24\r
25#include <Guid/FirmwarePerformance.h>\r
26\r
27#include <Library/SmmServicesTableLib.h>\r
28#include <Library/BaseLib.h>\r
29#include <Library/DebugLib.h>\r
30#include <Library/TimerLib.h>\r
31#include <Library/LockBoxLib.h>\r
32#include <Library/PcdLib.h>\r
1c0cc375
LG
33#include <Library/BaseMemoryLib.h>\r
34#include <Library/MemoryAllocationLib.h>\r
35#include <Library/UefiBootServicesTableLib.h>\r
36#include <Library/SynchronizationLib.h>\r
37\r
38#define EXTENSION_RECORD_SIZE 0x1000\r
0284e90c
LG
39\r
40EFI_SMM_RSC_HANDLER_PROTOCOL *mRscHandlerProtocol = NULL;\r
41UINT64 mSuspendStartTime = 0;\r
42BOOLEAN mS3SuspendLockBoxSaved = FALSE;\r
1c0cc375
LG
43UINT32 mBootRecordSize = 0;\r
44UINT32 mBootRecordMaxSize = 0;\r
45UINT8 *mBootRecordBuffer = NULL;\r
46\r
47EFI_SMRAM_DESCRIPTOR *mSmramRanges;\r
48UINTN mSmramRangeCount;\r
49SPIN_LOCK mSmmFpdtLock;\r
50BOOLEAN mSmramIsOutOfResource = FALSE;\r
0284e90c
LG
51\r
52/**\r
53 Report status code listener for SMM. This is used to record the performance\r
54 data for S3 Suspend Start and S3 Suspend End in FPDT.\r
55\r
56 @param[in] CodeType Indicates the type of status code being reported.\r
57 @param[in] Value Describes the current status of a hardware or software entity.\r
58 This included information about the class and subclass that is used to\r
59 classify the entity as well as an operation.\r
60 @param[in] Instance The enumeration of a hardware or software entity within\r
61 the system. Valid instance numbers start with 1.\r
62 @param[in] CallerId This optional parameter may be used to identify the caller.\r
63 This parameter allows the status code driver to apply different rules to\r
64 different callers.\r
65 @param[in] Data This optional parameter may be used to pass additional data.\r
66\r
67 @retval EFI_SUCCESS Status code is what we expected.\r
68 @retval EFI_UNSUPPORTED Status code not supported.\r
69\r
70**/\r
71EFI_STATUS\r
72EFIAPI\r
73FpdtStatusCodeListenerSmm (\r
74 IN EFI_STATUS_CODE_TYPE CodeType,\r
75 IN EFI_STATUS_CODE_VALUE Value,\r
76 IN UINT32 Instance,\r
77 IN EFI_GUID *CallerId,\r
78 IN EFI_STATUS_CODE_DATA *Data\r
79 )\r
80{\r
81 EFI_STATUS Status;\r
82 UINT64 CurrentTime;\r
83 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord;\r
1c0cc375 84 UINT8 *NewRecordBuffer;\r
0284e90c
LG
85\r
86 //\r
87 // Check whether status code is what we are interested in.\r
88 //\r
89 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {\r
90 return EFI_UNSUPPORTED;\r
91 }\r
1c0cc375
LG
92 \r
93 //\r
94 // Collect one or more Boot records in boot time\r
95 //\r
96 if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {\r
97 AcquireSpinLock (&mSmmFpdtLock);\r
98 \r
99 if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {\r
100 //\r
101 // Try to allocate big SMRAM data to store Boot record. \r
102 //\r
103 if (mSmramIsOutOfResource) {\r
104 return EFI_OUT_OF_RESOURCES;\r
105 }\r
106 NewRecordBuffer = AllocatePool (mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE); \r
107 if (NewRecordBuffer == NULL) {\r
108 mSmramIsOutOfResource = TRUE;\r
109 return EFI_OUT_OF_RESOURCES;\r
110 }\r
111 CopyMem (NewRecordBuffer, mBootRecordBuffer, mBootRecordSize);\r
112 mBootRecordBuffer = NewRecordBuffer;\r
113 mBootRecordMaxSize = mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE;\r
114 }\r
115 //\r
116 // Save boot record into the temp memory space.\r
117 //\r
118 CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);\r
119 mBootRecordSize += Data->Size;\r
120 \r
121 ReleaseSpinLock (&mSmmFpdtLock);\r
122 return EFI_SUCCESS;\r
123 }\r
124\r
0284e90c
LG
125 if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&\r
126 (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {\r
127 return EFI_UNSUPPORTED;\r
128 }\r
129\r
130 //\r
131 // Retrieve current time.\r
132 //\r
133 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());\r
134\r
135 if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {\r
136 //\r
137 // S3 Suspend started, record the performance data and return.\r
138 //\r
139 mSuspendStartTime = CurrentTime;\r
140 return EFI_SUCCESS;\r
141 }\r
142\r
143 //\r
144 // We are going to S3 sleep, record S3 Suspend End performance data.\r
145 //\r
146 S3SuspendRecord.SuspendStart = mSuspendStartTime;\r
147 S3SuspendRecord.SuspendEnd = CurrentTime;\r
148\r
149 //\r
150 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.\r
151 //\r
152 if (!mS3SuspendLockBoxSaved) {\r
153 Status = SaveLockBox (\r
154 &gEfiFirmwarePerformanceGuid,\r
155 &S3SuspendRecord,\r
156 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)\r
157 );\r
158 ASSERT_EFI_ERROR (Status);\r
159\r
160 mS3SuspendLockBoxSaved = TRUE;\r
161 } else {\r
162 Status = UpdateLockBox (\r
163 &gEfiFirmwarePerformanceGuid,\r
164 0,\r
165 &S3SuspendRecord,\r
166 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)\r
167 );\r
168 ASSERT_EFI_ERROR (Status);\r
169 }\r
170\r
171 return EFI_SUCCESS;\r
172}\r
173\r
1c0cc375
LG
174/**\r
175 This function check if the address is in SMRAM.\r
176\r
177 @param Buffer the buffer address to be checked.\r
178 @param Length the buffer length to be checked.\r
179\r
180 @retval TRUE this address is in SMRAM.\r
181 @retval FALSE this address is NOT in SMRAM.\r
182**/\r
183BOOLEAN\r
184InternalIsAddressInSmram (\r
185 IN EFI_PHYSICAL_ADDRESS Buffer,\r
186 IN UINT64 Length\r
187 )\r
188{\r
189 UINTN Index;\r
190\r
191 for (Index = 0; Index < mSmramRangeCount; Index ++) {\r
192 if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||\r
193 ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {\r
194 return TRUE;\r
195 }\r
196 }\r
197\r
198 return FALSE;\r
199}\r
200\r
201/**\r
202 Communication service SMI Handler entry.\r
203\r
204 This SMI handler provides services for report SMM boot records. \r
205\r
206 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
207 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
208 handler was registered.\r
209 @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r
210 be conveyed from a non-SMM environment into an SMM environment.\r
211 @param[in, out] CommBufferSize The size of the CommBuffer.\r
212\r
213 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers should still be called.\r
214 @retval EFI_INVALID_PARAMETER The interrupt parameter is not valid. \r
215 @retval EFI_ACCESS_DENIED The interrupt buffer can't be written. \r
216 @retval EFI_UNSUPPORTED The interrupt is not supported. \r
217**/\r
218EFI_STATUS\r
219EFIAPI\r
220FpdtSmiHandler (\r
221 IN EFI_HANDLE DispatchHandle,\r
222 IN CONST VOID *RegisterContext,\r
223 IN OUT VOID *CommBuffer,\r
224 IN OUT UINTN *CommBufferSize\r
225 )\r
226{\r
227 EFI_STATUS Status;\r
228 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;\r
229 \r
230 ASSERT (CommBuffer != NULL);\r
231 if (CommBuffer == NULL || *CommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {\r
232 return EFI_INVALID_PARAMETER;\r
233 }\r
234\r
235 Status = EFI_SUCCESS;\r
236 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;\r
237\r
238 switch (SmmCommData->Function) {\r
239 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :\r
240 SmmCommData->BootRecordSize = mBootRecordSize;\r
241 break;\r
242\r
243 case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :\r
244 if (SmmCommData->BootRecordData == NULL || SmmCommData->BootRecordSize < mBootRecordSize) {\r
245 Status = EFI_INVALID_PARAMETER;\r
246 break;\r
247 } \r
248 \r
249 //\r
250 // Sanity check\r
251 //\r
252 SmmCommData->BootRecordSize = mBootRecordSize;\r
253 if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)SmmCommData->BootRecordData, mBootRecordSize)) {\r
254 DEBUG ((EFI_D_ERROR, "Smm Data buffer is in SMRAM!\n"));\r
255 Status = EFI_ACCESS_DENIED;\r
256 break;\r
257 }\r
258\r
259 CopyMem (\r
260 (UINT8*)SmmCommData->BootRecordData, \r
261 mBootRecordBuffer, \r
262 mBootRecordSize\r
263 );\r
264 break;\r
265\r
266 default:\r
267 ASSERT (FALSE);\r
268 Status = EFI_UNSUPPORTED;\r
269 }\r
270\r
271 SmmCommData->ReturnStatus = Status;\r
272 return EFI_SUCCESS;\r
273}\r
274\r
0284e90c
LG
275/**\r
276 The module Entry Point of the Firmware Performance Data Table SMM driver.\r
277\r
278 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
279 @param[in] SystemTable A pointer to the EFI System Table.\r
280\r
281 @retval EFI_SUCCESS The entry point is executed successfully.\r
282 @retval Other Some error occurs when executing this entry point.\r
283\r
284**/\r
285EFI_STATUS\r
286EFIAPI\r
287FirmwarePerformanceSmmEntryPoint (\r
288 IN EFI_HANDLE ImageHandle,\r
289 IN EFI_SYSTEM_TABLE *SystemTable\r
290 )\r
291{\r
1c0cc375
LG
292 EFI_STATUS Status;\r
293 EFI_HANDLE Handle;\r
294 EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r
295 UINTN Size;\r
0284e90c 296\r
1c0cc375
LG
297 //\r
298 // Initialize spin lock\r
299 //\r
300 InitializeSpinLock (&mSmmFpdtLock); \r
301 \r
302 //\r
303 // Get SMM Report Status Code Handler Protocol.\r
304 //\r
305 Status = gSmst->SmmLocateProtocol (\r
306 &gEfiSmmRscHandlerProtocolGuid,\r
307 NULL,\r
308 (VOID **) &mRscHandlerProtocol\r
309 );\r
310 ASSERT_EFI_ERROR (Status);\r
0284e90c 311\r
1c0cc375
LG
312 //\r
313 // Register report status code listener for BootRecords and S3 Suspend Start and End.\r
314 //\r
315 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);\r
316 ASSERT_EFI_ERROR (Status);\r
0284e90c 317\r
1c0cc375
LG
318 //\r
319 // Get SMRAM information\r
320 //\r
321 Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r
322 ASSERT_EFI_ERROR (Status);\r
323\r
324 Size = 0;\r
325 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r
326 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
327\r
328 Status = gSmst->SmmAllocatePool (\r
329 EfiRuntimeServicesData,\r
330 Size,\r
331 (VOID **)&mSmramRanges\r
332 );\r
333 ASSERT_EFI_ERROR (Status);\r
334\r
335 Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r
336 ASSERT_EFI_ERROR (Status);\r
337\r
338 mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r
339\r
340 //\r
341 // Register SMI handler.\r
342 //\r
343 Handle = NULL;\r
344 Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);\r
345 ASSERT_EFI_ERROR (Status);\r
346\r
347 return Status;\r
0284e90c 348}\r