]>
Commit | Line | Data |
---|---|---|
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 |
40 | EFI_SMM_RSC_HANDLER_PROTOCOL *mRscHandlerProtocol = NULL;\r | |
41 | UINT64 mSuspendStartTime = 0;\r | |
42 | BOOLEAN mS3SuspendLockBoxSaved = FALSE;\r | |
1c0cc375 LG |
43 | UINT32 mBootRecordSize = 0;\r |
44 | UINT32 mBootRecordMaxSize = 0;\r | |
45 | UINT8 *mBootRecordBuffer = NULL;\r | |
46 | \r | |
47 | EFI_SMRAM_DESCRIPTOR *mSmramRanges;\r | |
48 | UINTN mSmramRangeCount;\r | |
49 | SPIN_LOCK mSmmFpdtLock;\r | |
50 | BOOLEAN 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 | |
71 | EFI_STATUS\r | |
72 | EFIAPI\r | |
73 | FpdtStatusCodeListenerSmm (\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 | |
d69a7bc7 | 104 | ReleaseSpinLock (&mSmmFpdtLock);\r |
1c0cc375 LG |
105 | return EFI_OUT_OF_RESOURCES;\r |
106 | }\r | |
d69a7bc7 | 107 | NewRecordBuffer = ReallocatePool (mBootRecordSize, mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE, mBootRecordBuffer); \r |
1c0cc375 | 108 | if (NewRecordBuffer == NULL) {\r |
d69a7bc7 | 109 | ReleaseSpinLock (&mSmmFpdtLock);\r |
1c0cc375 LG |
110 | mSmramIsOutOfResource = TRUE;\r |
111 | return EFI_OUT_OF_RESOURCES;\r | |
112 | }\r | |
1c0cc375 LG |
113 | mBootRecordBuffer = NewRecordBuffer;\r |
114 | mBootRecordMaxSize = mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE;\r | |
115 | }\r | |
116 | //\r | |
117 | // Save boot record into the temp memory space.\r | |
118 | //\r | |
119 | CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);\r | |
120 | mBootRecordSize += Data->Size;\r | |
121 | \r | |
122 | ReleaseSpinLock (&mSmmFpdtLock);\r | |
123 | return EFI_SUCCESS;\r | |
124 | }\r | |
125 | \r | |
0284e90c LG |
126 | if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&\r |
127 | (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd))) {\r | |
128 | return EFI_UNSUPPORTED;\r | |
129 | }\r | |
130 | \r | |
131 | //\r | |
132 | // Retrieve current time.\r | |
133 | //\r | |
134 | CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());\r | |
135 | \r | |
136 | if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {\r | |
137 | //\r | |
138 | // S3 Suspend started, record the performance data and return.\r | |
139 | //\r | |
140 | mSuspendStartTime = CurrentTime;\r | |
141 | return EFI_SUCCESS;\r | |
142 | }\r | |
143 | \r | |
144 | //\r | |
145 | // We are going to S3 sleep, record S3 Suspend End performance data.\r | |
146 | //\r | |
147 | S3SuspendRecord.SuspendStart = mSuspendStartTime;\r | |
148 | S3SuspendRecord.SuspendEnd = CurrentTime;\r | |
149 | \r | |
150 | //\r | |
151 | // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.\r | |
152 | //\r | |
153 | if (!mS3SuspendLockBoxSaved) {\r | |
154 | Status = SaveLockBox (\r | |
155 | &gEfiFirmwarePerformanceGuid,\r | |
156 | &S3SuspendRecord,\r | |
157 | sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)\r | |
158 | );\r | |
159 | ASSERT_EFI_ERROR (Status);\r | |
160 | \r | |
161 | mS3SuspendLockBoxSaved = TRUE;\r | |
162 | } else {\r | |
163 | Status = UpdateLockBox (\r | |
164 | &gEfiFirmwarePerformanceGuid,\r | |
165 | 0,\r | |
166 | &S3SuspendRecord,\r | |
167 | sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)\r | |
168 | );\r | |
169 | ASSERT_EFI_ERROR (Status);\r | |
170 | }\r | |
171 | \r | |
172 | return EFI_SUCCESS;\r | |
173 | }\r | |
174 | \r | |
1c0cc375 LG |
175 | /**\r |
176 | This function check if the address is in SMRAM.\r | |
177 | \r | |
178 | @param Buffer the buffer address to be checked.\r | |
179 | @param Length the buffer length to be checked.\r | |
180 | \r | |
181 | @retval TRUE this address is in SMRAM.\r | |
182 | @retval FALSE this address is NOT in SMRAM.\r | |
183 | **/\r | |
184 | BOOLEAN\r | |
185 | InternalIsAddressInSmram (\r | |
186 | IN EFI_PHYSICAL_ADDRESS Buffer,\r | |
187 | IN UINT64 Length\r | |
188 | )\r | |
189 | {\r | |
190 | UINTN Index;\r | |
191 | \r | |
192 | for (Index = 0; Index < mSmramRangeCount; Index ++) {\r | |
193 | if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||\r | |
194 | ((mSmramRanges[Index].CpuStart >= Buffer) && (mSmramRanges[Index].CpuStart < Buffer + Length))) {\r | |
195 | return TRUE;\r | |
196 | }\r | |
197 | }\r | |
198 | \r | |
199 | return FALSE;\r | |
200 | }\r | |
201 | \r | |
202 | /**\r | |
203 | Communication service SMI Handler entry.\r | |
204 | \r | |
205 | This SMI handler provides services for report SMM boot records. \r | |
206 | \r | |
207 | @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r | |
208 | @param[in] RegisterContext Points to an optional handler context which was specified when the\r | |
209 | handler was registered.\r | |
210 | @param[in, out] CommBuffer A pointer to a collection of data in memory that will\r | |
211 | be conveyed from a non-SMM environment into an SMM environment.\r | |
212 | @param[in, out] CommBufferSize The size of the CommBuffer.\r | |
213 | \r | |
214 | @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers should still be called.\r | |
215 | @retval EFI_INVALID_PARAMETER The interrupt parameter is not valid. \r | |
216 | @retval EFI_ACCESS_DENIED The interrupt buffer can't be written. \r | |
217 | @retval EFI_UNSUPPORTED The interrupt is not supported. \r | |
218 | **/\r | |
219 | EFI_STATUS\r | |
220 | EFIAPI\r | |
221 | FpdtSmiHandler (\r | |
222 | IN EFI_HANDLE DispatchHandle,\r | |
223 | IN CONST VOID *RegisterContext,\r | |
224 | IN OUT VOID *CommBuffer,\r | |
225 | IN OUT UINTN *CommBufferSize\r | |
226 | )\r | |
227 | {\r | |
228 | EFI_STATUS Status;\r | |
229 | SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;\r | |
230 | \r | |
231 | ASSERT (CommBuffer != NULL);\r | |
232 | if (CommBuffer == NULL || *CommBufferSize < sizeof (SMM_BOOT_RECORD_COMMUNICATE)) {\r | |
233 | return EFI_INVALID_PARAMETER;\r | |
234 | }\r | |
235 | \r | |
236 | Status = EFI_SUCCESS;\r | |
237 | SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)CommBuffer;\r | |
238 | \r | |
239 | switch (SmmCommData->Function) {\r | |
240 | case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE :\r | |
241 | SmmCommData->BootRecordSize = mBootRecordSize;\r | |
242 | break;\r | |
243 | \r | |
244 | case SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA :\r | |
245 | if (SmmCommData->BootRecordData == NULL || SmmCommData->BootRecordSize < mBootRecordSize) {\r | |
246 | Status = EFI_INVALID_PARAMETER;\r | |
247 | break;\r | |
248 | } \r | |
249 | \r | |
250 | //\r | |
251 | // Sanity check\r | |
252 | //\r | |
253 | SmmCommData->BootRecordSize = mBootRecordSize;\r | |
254 | if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)(UINTN)SmmCommData->BootRecordData, mBootRecordSize)) {\r | |
255 | DEBUG ((EFI_D_ERROR, "Smm Data buffer is in SMRAM!\n"));\r | |
256 | Status = EFI_ACCESS_DENIED;\r | |
257 | break;\r | |
258 | }\r | |
259 | \r | |
260 | CopyMem (\r | |
261 | (UINT8*)SmmCommData->BootRecordData, \r | |
262 | mBootRecordBuffer, \r | |
263 | mBootRecordSize\r | |
264 | );\r | |
265 | break;\r | |
266 | \r | |
267 | default:\r | |
268 | ASSERT (FALSE);\r | |
269 | Status = EFI_UNSUPPORTED;\r | |
270 | }\r | |
271 | \r | |
272 | SmmCommData->ReturnStatus = Status;\r | |
273 | return EFI_SUCCESS;\r | |
274 | }\r | |
275 | \r | |
0284e90c LG |
276 | /**\r |
277 | The module Entry Point of the Firmware Performance Data Table SMM driver.\r | |
278 | \r | |
279 | @param[in] ImageHandle The firmware allocated handle for the EFI image.\r | |
280 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
281 | \r | |
282 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
283 | @retval Other Some error occurs when executing this entry point.\r | |
284 | \r | |
285 | **/\r | |
286 | EFI_STATUS\r | |
287 | EFIAPI\r | |
288 | FirmwarePerformanceSmmEntryPoint (\r | |
289 | IN EFI_HANDLE ImageHandle,\r | |
290 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
291 | )\r | |
292 | {\r | |
1c0cc375 LG |
293 | EFI_STATUS Status;\r |
294 | EFI_HANDLE Handle;\r | |
295 | EFI_SMM_ACCESS2_PROTOCOL *SmmAccess;\r | |
296 | UINTN Size;\r | |
0284e90c | 297 | \r |
1c0cc375 LG |
298 | //\r |
299 | // Initialize spin lock\r | |
300 | //\r | |
301 | InitializeSpinLock (&mSmmFpdtLock); \r | |
302 | \r | |
303 | //\r | |
304 | // Get SMM Report Status Code Handler Protocol.\r | |
305 | //\r | |
306 | Status = gSmst->SmmLocateProtocol (\r | |
307 | &gEfiSmmRscHandlerProtocolGuid,\r | |
308 | NULL,\r | |
309 | (VOID **) &mRscHandlerProtocol\r | |
310 | );\r | |
311 | ASSERT_EFI_ERROR (Status);\r | |
0284e90c | 312 | \r |
1c0cc375 LG |
313 | //\r |
314 | // Register report status code listener for BootRecords and S3 Suspend Start and End.\r | |
315 | //\r | |
316 | Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerSmm);\r | |
317 | ASSERT_EFI_ERROR (Status);\r | |
0284e90c | 318 | \r |
1c0cc375 LG |
319 | //\r |
320 | // Get SMRAM information\r | |
321 | //\r | |
322 | Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID **)&SmmAccess);\r | |
323 | ASSERT_EFI_ERROR (Status);\r | |
324 | \r | |
325 | Size = 0;\r | |
326 | Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);\r | |
327 | ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r | |
328 | \r | |
329 | Status = gSmst->SmmAllocatePool (\r | |
330 | EfiRuntimeServicesData,\r | |
331 | Size,\r | |
332 | (VOID **)&mSmramRanges\r | |
333 | );\r | |
334 | ASSERT_EFI_ERROR (Status);\r | |
335 | \r | |
336 | Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);\r | |
337 | ASSERT_EFI_ERROR (Status);\r | |
338 | \r | |
339 | mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);\r | |
340 | \r | |
341 | //\r | |
342 | // Register SMI handler.\r | |
343 | //\r | |
344 | Handle = NULL;\r | |
345 | Status = gSmst->SmiHandlerRegister (FpdtSmiHandler, &gEfiFirmwarePerformanceGuid, &Handle);\r | |
346 | ASSERT_EFI_ERROR (Status);\r | |
347 | \r | |
348 | return Status;\r | |
0284e90c | 349 | }\r |