]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c
MdeModulePkg/S3SmmInitDone.h: Fix copyright coding style error.
[mirror_edk2.git] / MdeModulePkg / Library / DxeCorePerformanceLib / DxeCorePerformanceLib.c
CommitLineData
8dbae30d 1/** @file\r
857dfc45 2 Performance library instance mainly used by DxeCore.\r
3\r
4 This library provides the performance measurement interfaces and initializes performance\r
5 logging for DXE phase. It first initializes its private global data structure for\r
d1102dba 6 performance logging and saves the performance GUIDed HOB passed from PEI phase.\r
f0da4d7d
SZ
7 It initializes DXE phase performance logging by publishing the Performance and PerformanceEx Protocol,\r
8 which are consumed by DxePerformanceLib to logging performance data in DXE phase.\r
857dfc45 9\r
10 This library is mainly used by DxeCore to start performance logging to ensure that\r
11 Performance Protocol is installed at the very beginning of DXE phase.\r
a0afd019 12\r
9609d24b 13Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
7c50b343 14(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
cd5ebaa0 15This program and the accompanying materials\r
a0afd019 16are licensed and made available under the terms and conditions of the BSD License\r
17which accompanies this distribution. The full text of the license may be found at\r
18http://opensource.org/licenses/bsd-license.php\r
19\r
20THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
21WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
22\r
8dbae30d 23**/\r
a0afd019 24\r
ed7748fe 25\r
aa79b0b3 26#include "DxeCorePerformanceLibInternal.h"\r
ed7748fe 27\r
a0afd019 28//\r
9609d24b 29// Data for FPDT performance records.\r
a0afd019 30//\r
9609d24b
DB
31#define SMM_BOOT_RECORD_COMM_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof(SMM_BOOT_RECORD_COMMUNICATE))\r
32#define STRING_SIZE (FPDT_STRING_EVENT_RECORD_NAME_LENGTH * sizeof (CHAR8))\r
33#define FIRMWARE_RECORD_BUFFER 0x10000\r
34#define CACHE_HANDLE_GUID_COUNT 0x800\r
35\r
36BOOT_PERFORMANCE_TABLE *mAcpiBootPerformanceTable = NULL;\r
37BOOT_PERFORMANCE_TABLE mBootPerformanceTableTemplate = {\r
38 {\r
39 EFI_ACPI_5_0_FPDT_BOOT_PERFORMANCE_TABLE_SIGNATURE,\r
40 sizeof (BOOT_PERFORMANCE_TABLE)\r
41 },\r
42 {\r
43 {\r
44 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_TYPE_FIRMWARE_BASIC_BOOT, // Type\r
45 sizeof (EFI_ACPI_5_0_FPDT_FIRMWARE_BASIC_BOOT_RECORD), // Length\r
46 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_REVISION_FIRMWARE_BASIC_BOOT // Revision\r
47 },\r
48 0, // Reserved\r
49 //\r
50 // These values will be updated at runtime.\r
51 //\r
52 0, // ResetEnd\r
53 0, // OsLoaderLoadImageStart\r
54 0, // OsLoaderStartImageStart\r
55 0, // ExitBootServicesEntry\r
56 0 // ExitBootServicesExit\r
57 }\r
58};\r
857dfc45 59\r
9609d24b
DB
60typedef struct {\r
61 EFI_HANDLE Handle;\r
62 CHAR8 NameString[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];\r
63 EFI_GUID ModuleGuid;\r
64} HANDLE_GUID_MAP;\r
a0afd019 65\r
9609d24b
DB
66HANDLE_GUID_MAP mCacheHandleGuidTable[CACHE_HANDLE_GUID_COUNT];\r
67UINTN mCachePairCount = 0;\r
68\r
69UINT32 mLoadImageCount = 0;\r
70UINT32 mPerformanceLength = 0;\r
71UINT32 mMaxPerformanceLength = 0;\r
72UINT32 mBootRecordSize = 0;\r
73UINT32 mBootRecordMaxSize = 0;\r
6b4d58a1 74UINT32 mCachedLength = 0;\r
9609d24b
DB
75\r
76BOOLEAN mFpdtBufferIsReported = FALSE;\r
77BOOLEAN mLackSpaceIsReported = FALSE;\r
78CHAR8 *mPlatformLanguage = NULL;\r
79UINT8 *mPerformancePointer = NULL;\r
80UINT8 *mBootRecordBuffer = NULL;\r
81BOOLEAN mLockInsertRecord = FALSE;\r
6b4d58a1 82CHAR8 *mDevicePathString = NULL;\r
9609d24b
DB
83\r
84EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *mDevicePathToText = NULL;\r
857dfc45 85\r
86//\r
137fb13d 87// Interfaces for PerformanceMeasurement Protocol.\r
857dfc45 88//\r
137fb13d
BD
89EDKII_PERFORMANCE_MEASUREMENT_PROTOCOL mPerformanceMeasurementInterface = {\r
90 CreatePerformanceMeasurement,\r
f0da4d7d
SZ
91 };\r
92\r
cfb0aba7
SZ
93PERFORMANCE_PROPERTY mPerformanceProperty;\r
94\r
6b4d58a1
BD
95/**\r
96 Return the pointer to the FPDT record in the allocated memory.\r
97\r
98 @param RecordSize The size of FPDT record.\r
99 @param FpdtRecordPtr Pointer the FPDT record in the allocated memory.\r
100\r
101 @retval EFI_SUCCESS Successfully get the pointer to the FPDT record.\r
102 @retval EFI_OUT_OF_RESOURCES Ran out of space to store the records.\r
103**/\r
104EFI_STATUS\r
105GetFpdtRecordPtr (\r
106 IN UINT8 RecordSize,\r
107 IN OUT FPDT_RECORD_PTR *FpdtRecordPtr\r
108)\r
109{\r
110 if (mFpdtBufferIsReported) {\r
111 //\r
112 // Append Boot records to the boot performance table.\r
113 //\r
114 if (mBootRecordSize + RecordSize > mBootRecordMaxSize) {\r
115 if (!mLackSpaceIsReported) {\r
116 DEBUG ((DEBUG_INFO, "DxeCorePerformanceLib: No enough space to save boot records\n"));\r
117 mLackSpaceIsReported = TRUE;\r
118 }\r
119 return EFI_OUT_OF_RESOURCES;\r
120 } else {\r
121 //\r
122 // Save boot record into BootPerformance table\r
123 //\r
124 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mBootRecordSize);\r
125 }\r
126 } else {\r
127 //\r
128 // Check if pre-allocated buffer is full\r
129 //\r
130 if (mPerformanceLength + RecordSize > mMaxPerformanceLength) {\r
131 mPerformancePointer = ReallocatePool (\r
132 mPerformanceLength,\r
133 mPerformanceLength + RecordSize + FIRMWARE_RECORD_BUFFER,\r
134 mPerformancePointer\r
135 );\r
136 if (mPerformancePointer == NULL) {\r
137 return EFI_OUT_OF_RESOURCES;\r
138 }\r
139 mMaxPerformanceLength = mPerformanceLength + RecordSize + FIRMWARE_RECORD_BUFFER;\r
140 }\r
141 //\r
142 // Covert buffer to FPDT Ptr Union type.\r
143 //\r
144 FpdtRecordPtr->RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mPerformanceLength);\r
145 }\r
146 return EFI_SUCCESS;\r
147}\r
148\r
9609d24b
DB
149/**\r
150Check whether the Token is a known one which is uesed by core.\r
151\r
152@param Token Pointer to a Null-terminated ASCII string\r
153\r
154@retval TRUE Is a known one used by core.\r
155@retval FALSE Not a known one.\r
156\r
157**/\r
158BOOLEAN\r
159IsKnownTokens (\r
160 IN CONST CHAR8 *Token\r
161 )\r
162{\r
c9faac27
DB
163 if (Token == NULL) {\r
164 return FALSE;\r
165 }\r
166\r
9609d24b
DB
167 if (AsciiStrCmp (Token, SEC_TOK) == 0 ||\r
168 AsciiStrCmp (Token, PEI_TOK) == 0 ||\r
169 AsciiStrCmp (Token, DXE_TOK) == 0 ||\r
170 AsciiStrCmp (Token, BDS_TOK) == 0 ||\r
171 AsciiStrCmp (Token, DRIVERBINDING_START_TOK) == 0 ||\r
172 AsciiStrCmp (Token, DRIVERBINDING_SUPPORT_TOK) == 0 ||\r
173 AsciiStrCmp (Token, DRIVERBINDING_STOP_TOK) == 0 ||\r
174 AsciiStrCmp (Token, LOAD_IMAGE_TOK) == 0 ||\r
175 AsciiStrCmp (Token, START_IMAGE_TOK) == 0 ||\r
176 AsciiStrCmp (Token, PEIM_TOK) == 0) {\r
177 return TRUE;\r
178 } else {\r
179 return FALSE;\r
180 }\r
181}\r
f1f7190b 182\r
a0afd019 183/**\r
9609d24b
DB
184Check whether the ID is a known one which map to the known Token.\r
185\r
186@param Identifier 32-bit identifier.\r
187\r
188@retval TRUE Is a known one used by core.\r
189@retval FALSE Not a known one.\r
190\r
191**/\r
192BOOLEAN\r
193IsKnownID (\r
194 IN UINT32 Identifier\r
195 )\r
196{\r
197 if (Identifier == MODULE_START_ID ||\r
198 Identifier == MODULE_END_ID ||\r
199 Identifier == MODULE_LOADIMAGE_START_ID ||\r
200 Identifier == MODULE_LOADIMAGE_END_ID ||\r
201 Identifier == MODULE_DB_START_ID ||\r
202 Identifier == MODULE_DB_END_ID ||\r
203 Identifier == MODULE_DB_SUPPORT_START_ID ||\r
204 Identifier == MODULE_DB_SUPPORT_END_ID ||\r
205 Identifier == MODULE_DB_STOP_START_ID ||\r
206 Identifier == MODULE_DB_STOP_END_ID) {\r
207 return TRUE;\r
208 } else {\r
209 return FALSE;\r
210 }\r
211}\r
212\r
9609d24b
DB
213/**\r
214 Allocate buffer for Boot Performance table.\r
215\r
216 @return Status code.\r
217\r
218**/\r
219EFI_STATUS\r
220AllocateBootPerformanceTable (\r
221 )\r
222{\r
223 EFI_STATUS Status;\r
224 UINTN Size;\r
225 UINT8 *SmmBootRecordCommBuffer;\r
226 EFI_SMM_COMMUNICATE_HEADER *SmmCommBufferHeader;\r
227 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;\r
228 UINTN CommSize;\r
229 UINTN BootPerformanceDataSize;\r
230 UINT8 *BootPerformanceData;\r
231 EFI_SMM_COMMUNICATION_PROTOCOL *Communication;\r
232 FIRMWARE_PERFORMANCE_VARIABLE PerformanceVariable;\r
233 EDKII_PI_SMM_COMMUNICATION_REGION_TABLE *SmmCommRegionTable;\r
234 EFI_MEMORY_DESCRIPTOR *SmmCommMemRegion;\r
235 UINTN Index;\r
236 VOID *SmmBootRecordData;\r
237 UINTN SmmBootRecordDataSize;\r
238 UINTN ReservedMemSize;\r
a0afd019 239\r
9609d24b
DB
240 //\r
241 // Collect boot records from SMM drivers.\r
242 //\r
243 SmmBootRecordCommBuffer = NULL;\r
244 SmmCommData = NULL;\r
245 SmmBootRecordData = NULL;\r
246 SmmBootRecordDataSize = 0;\r
247 ReservedMemSize = 0;\r
248 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &Communication);\r
249 if (!EFI_ERROR (Status)) {\r
250 //\r
251 // Initialize communicate buffer\r
252 // Get the prepared Reserved Memory Range\r
253 //\r
254 Status = EfiGetSystemConfigurationTable (\r
255 &gEdkiiPiSmmCommunicationRegionTableGuid,\r
256 (VOID **) &SmmCommRegionTable\r
257 );\r
258 if (!EFI_ERROR (Status)) {\r
259 ASSERT (SmmCommRegionTable != NULL);\r
260 SmmCommMemRegion = (EFI_MEMORY_DESCRIPTOR *) (SmmCommRegionTable + 1);\r
261 for (Index = 0; Index < SmmCommRegionTable->NumberOfEntries; Index ++) {\r
262 if (SmmCommMemRegion->Type == EfiConventionalMemory) {\r
263 break;\r
264 }\r
265 SmmCommMemRegion = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) SmmCommMemRegion + SmmCommRegionTable->DescriptorSize);\r
266 }\r
267 ASSERT (Index < SmmCommRegionTable->NumberOfEntries);\r
268 ASSERT (SmmCommMemRegion->PhysicalStart > 0);\r
269 ASSERT (SmmCommMemRegion->NumberOfPages > 0);\r
270 ReservedMemSize = (UINTN) SmmCommMemRegion->NumberOfPages * EFI_PAGE_SIZE;\r
271\r
272 //\r
273 // Check enough reserved memory space\r
274 //\r
275 if (ReservedMemSize > SMM_BOOT_RECORD_COMM_SIZE) {\r
276 SmmBootRecordCommBuffer = (VOID *) (UINTN) SmmCommMemRegion->PhysicalStart;\r
277 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER*)SmmBootRecordCommBuffer;\r
278 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)SmmCommBufferHeader->Data;\r
279 ZeroMem((UINT8*)SmmCommData, sizeof(SMM_BOOT_RECORD_COMMUNICATE));\r
280\r
281 CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gEfiFirmwarePerformanceGuid);\r
282 SmmCommBufferHeader->MessageLength = sizeof(SMM_BOOT_RECORD_COMMUNICATE);\r
283 CommSize = SMM_BOOT_RECORD_COMM_SIZE;\r
284\r
285 //\r
286 // Get the size of boot records.\r
287 //\r
288 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE;\r
289 SmmCommData->BootRecordData = NULL;\r
290 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);\r
9609d24b 291\r
c9f02a96 292 if (!EFI_ERROR (Status) && !EFI_ERROR (SmmCommData->ReturnStatus) && SmmCommData->BootRecordSize != 0) {\r
9609d24b
DB
293 //\r
294 // Get all boot records\r
295 //\r
296 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA_BY_OFFSET;\r
297 SmmBootRecordDataSize = SmmCommData->BootRecordSize;\r
298 SmmBootRecordData = AllocateZeroPool(SmmBootRecordDataSize);\r
299 ASSERT (SmmBootRecordData != NULL);\r
300 SmmCommData->BootRecordOffset = 0;\r
301 SmmCommData->BootRecordData = (VOID *) ((UINTN) SmmCommMemRegion->PhysicalStart + SMM_BOOT_RECORD_COMM_SIZE);\r
302 SmmCommData->BootRecordSize = ReservedMemSize - SMM_BOOT_RECORD_COMM_SIZE;\r
303 while (SmmCommData->BootRecordOffset < SmmBootRecordDataSize) {\r
304 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);\r
305 ASSERT_EFI_ERROR (Status);\r
306 ASSERT_EFI_ERROR(SmmCommData->ReturnStatus);\r
307 if (SmmCommData->BootRecordOffset + SmmCommData->BootRecordSize > SmmBootRecordDataSize) {\r
308 CopyMem ((UINT8 *) SmmBootRecordData + SmmCommData->BootRecordOffset, SmmCommData->BootRecordData, SmmBootRecordDataSize - SmmCommData->BootRecordOffset);\r
309 } else {\r
310 CopyMem ((UINT8 *) SmmBootRecordData + SmmCommData->BootRecordOffset, SmmCommData->BootRecordData, SmmCommData->BootRecordSize);\r
311 }\r
312 SmmCommData->BootRecordOffset = SmmCommData->BootRecordOffset + SmmCommData->BootRecordSize;\r
313 }\r
314 }\r
315 }\r
316 }\r
317 }\r
a0afd019 318\r
9609d24b
DB
319 //\r
320 // Prepare memory for Boot Performance table.\r
321 // Boot Performance table includes BasicBoot record, and one or more appended Boot Records.\r
322 //\r
323 BootPerformanceDataSize = sizeof (BOOT_PERFORMANCE_TABLE) + mPerformanceLength + PcdGet32 (PcdExtFpdtBootRecordPadSize);\r
c9f02a96 324 if (SmmCommData != NULL && SmmBootRecordData != NULL) {\r
9609d24b
DB
325 BootPerformanceDataSize += SmmBootRecordDataSize;\r
326 }\r
327\r
328 //\r
329 // Try to allocate the same runtime buffer as last time boot.\r
330 //\r
331 ZeroMem (&PerformanceVariable, sizeof (PerformanceVariable));\r
332 Size = sizeof (PerformanceVariable);\r
333 Status = gRT->GetVariable (\r
334 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,\r
335 &gEfiFirmwarePerformanceGuid,\r
336 NULL,\r
337 &Size,\r
338 &PerformanceVariable\r
339 );\r
340 if (!EFI_ERROR (Status)) {\r
341 Status = gBS->AllocatePages (\r
342 AllocateAddress,\r
343 EfiReservedMemoryType,\r
344 EFI_SIZE_TO_PAGES (BootPerformanceDataSize),\r
345 &PerformanceVariable.BootPerformanceTablePointer\r
346 );\r
347 if (!EFI_ERROR (Status)) {\r
348 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *) (UINTN) PerformanceVariable.BootPerformanceTablePointer;\r
349 }\r
350 }\r
351\r
352 if (mAcpiBootPerformanceTable == NULL) {\r
353 //\r
354 // Fail to allocate at specified address, continue to allocate at any address.\r
355 //\r
d79d7b58
AB
356 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *) AllocatePeiAccessiblePages (\r
357 EfiReservedMemoryType,\r
358 EFI_SIZE_TO_PAGES (BootPerformanceDataSize)\r
359 );\r
360 if (mAcpiBootPerformanceTable != NULL) {\r
361 ZeroMem (mAcpiBootPerformanceTable, BootPerformanceDataSize);\r
362 }\r
9609d24b
DB
363 }\r
364 DEBUG ((DEBUG_INFO, "DxeCorePerformanceLib: ACPI Boot Performance Table address = 0x%x\n", mAcpiBootPerformanceTable));\r
365\r
366 if (mAcpiBootPerformanceTable == NULL) {\r
367 if (SmmCommData != NULL && SmmBootRecordData != NULL) {\r
368 FreePool (SmmBootRecordData);\r
369 }\r
370 return EFI_OUT_OF_RESOURCES;\r
371 }\r
372\r
373 //\r
374 // Prepare Boot Performance Table.\r
375 //\r
376 BootPerformanceData = (UINT8 *) mAcpiBootPerformanceTable;\r
377 //\r
378 // Fill Basic Boot record to Boot Performance Table.\r
379 //\r
380 CopyMem (mAcpiBootPerformanceTable, &mBootPerformanceTableTemplate, sizeof (mBootPerformanceTableTemplate));\r
381 BootPerformanceData = BootPerformanceData + mAcpiBootPerformanceTable->Header.Length;\r
382 //\r
383 // Fill Boot records from boot drivers.\r
384 //\r
385 if (mPerformancePointer != NULL) {\r
386 CopyMem (BootPerformanceData, mPerformancePointer, mPerformanceLength);\r
387 mAcpiBootPerformanceTable->Header.Length += mPerformanceLength;\r
388 BootPerformanceData = BootPerformanceData + mPerformanceLength;\r
389 FreePool (mPerformancePointer);\r
390 mPerformancePointer = NULL;\r
391 mPerformanceLength = 0;\r
392 mMaxPerformanceLength = 0;\r
393 }\r
394 if (SmmCommData != NULL && SmmBootRecordData != NULL) {\r
395 //\r
396 // Fill Boot records from SMM drivers.\r
397 //\r
398 CopyMem (BootPerformanceData, SmmBootRecordData, SmmBootRecordDataSize);\r
399 FreePool (SmmBootRecordData);\r
400 mAcpiBootPerformanceTable->Header.Length = (UINT32) (mAcpiBootPerformanceTable->Header.Length + SmmBootRecordDataSize);\r
401 BootPerformanceData = BootPerformanceData + SmmBootRecordDataSize;\r
402 }\r
403\r
404 mBootRecordBuffer = (UINT8 *) mAcpiBootPerformanceTable;\r
405 mBootRecordSize = mAcpiBootPerformanceTable->Header.Length;\r
406 mBootRecordMaxSize = mBootRecordSize + PcdGet32 (PcdExtFpdtBootRecordPadSize);\r
407\r
408 return EFI_SUCCESS;\r
409}\r
410\r
411/**\r
412 Get a human readable module name and module guid for the given image handle.\r
413 If module name can't be found, "" string will return.\r
414 If module guid can't be found, Zero Guid will return.\r
415\r
416 @param Handle Image handle or Controller handle.\r
417 @param NameString The ascii string will be filled into it. If not found, null string will return.\r
418 @param BufferSize Size of the input NameString buffer.\r
419 @param ModuleGuid Point to the guid buffer to store the got module guid value.\r
420\r
421 @retval EFI_SUCCESS Successfully get module name and guid.\r
422 @retval EFI_INVALID_PARAMETER The input parameter NameString is NULL.\r
423 @retval other value Module Name can't be got.\r
424**/\r
425EFI_STATUS\r
426GetModuleInfoFromHandle (\r
427 IN EFI_HANDLE Handle,\r
428 OUT CHAR8 *NameString,\r
429 IN UINTN BufferSize,\r
430 OUT EFI_GUID *ModuleGuid OPTIONAL\r
431 )\r
432{\r
433 EFI_STATUS Status;\r
434 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;\r
435 EFI_DRIVER_BINDING_PROTOCOL *DriverBinding;\r
436 CHAR8 *PdbFileName;\r
437 EFI_GUID *TempGuid;\r
438 UINTN StartIndex;\r
439 UINTN Index;\r
440 INTN Count;\r
441 BOOLEAN ModuleGuidIsGet;\r
442 UINTN StringSize;\r
443 CHAR16 *StringPtr;\r
444 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
445 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *FvFilePath;\r
446\r
447 if (NameString == NULL || BufferSize == 0) {\r
448 return EFI_INVALID_PARAMETER;\r
449 }\r
450 //\r
451 // Try to get the ModuleGuid and name string form the caached array.\r
452 //\r
453 if (mCachePairCount > 0) {\r
454 for (Count = mCachePairCount -1; Count >= 0; Count--) {\r
455 if (Handle == mCacheHandleGuidTable[Count].Handle) {\r
456 CopyGuid (ModuleGuid, &mCacheHandleGuidTable[Count].ModuleGuid);\r
457 AsciiStrCpyS (NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, mCacheHandleGuidTable[Count].NameString);\r
458 return EFI_SUCCESS;\r
459 }\r
460 }\r
461 }\r
462\r
463 Status = EFI_INVALID_PARAMETER;\r
464 LoadedImage = NULL;\r
465 ModuleGuidIsGet = FALSE;\r
466\r
467 //\r
468 // Initialize GUID as zero value.\r
469 //\r
470 TempGuid = &gZeroGuid;\r
471 //\r
472 // Initialize it as "" string.\r
473 //\r
474 NameString[0] = 0;\r
475\r
476 if (Handle != NULL) {\r
477 //\r
478 // Try Handle as ImageHandle.\r
479 //\r
480 Status = gBS->HandleProtocol (\r
481 Handle,\r
482 &gEfiLoadedImageProtocolGuid,\r
483 (VOID**) &LoadedImage\r
484 );\r
485\r
486 if (EFI_ERROR (Status)) {\r
487 //\r
488 // Try Handle as Controller Handle\r
489 //\r
490 Status = gBS->OpenProtocol (\r
491 Handle,\r
492 &gEfiDriverBindingProtocolGuid,\r
493 (VOID **) &DriverBinding,\r
494 NULL,\r
495 NULL,\r
496 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
497 );\r
498 if (!EFI_ERROR (Status)) {\r
499 //\r
500 // Get Image protocol from ImageHandle\r
501 //\r
502 Status = gBS->HandleProtocol (\r
503 DriverBinding->ImageHandle,\r
504 &gEfiLoadedImageProtocolGuid,\r
505 (VOID**) &LoadedImage\r
506 );\r
507 }\r
508 }\r
509 }\r
510\r
511 if (!EFI_ERROR (Status) && LoadedImage != NULL) {\r
512 //\r
513 // Get Module Guid from DevicePath.\r
514 //\r
515 if (LoadedImage->FilePath != NULL &&\r
516 LoadedImage->FilePath->Type == MEDIA_DEVICE_PATH &&\r
517 LoadedImage->FilePath->SubType == MEDIA_PIWG_FW_FILE_DP\r
518 ) {\r
519 //\r
520 // Determine GUID associated with module logging performance\r
521 //\r
522 ModuleGuidIsGet = TRUE;\r
523 FvFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) LoadedImage->FilePath;\r
524 TempGuid = &FvFilePath->FvFileName;\r
525 }\r
526\r
527 //\r
528 // Method 1 Get Module Name from PDB string.\r
529 //\r
530 PdbFileName = PeCoffLoaderGetPdbPointer (LoadedImage->ImageBase);\r
531 if (PdbFileName != NULL && BufferSize > 0) {\r
532 StartIndex = 0;\r
533 for (Index = 0; PdbFileName[Index] != 0; Index++) {\r
534 if ((PdbFileName[Index] == '\\') || (PdbFileName[Index] == '/')) {\r
535 StartIndex = Index + 1;\r
536 }\r
537 }\r
538 //\r
539 // Copy the PDB file name to our temporary string.\r
540 // If the length is bigger than BufferSize, trim the redudant characters to avoid overflow in array boundary.\r
541 //\r
542 for (Index = 0; Index < BufferSize - 1; Index++) {\r
543 NameString[Index] = PdbFileName[Index + StartIndex];\r
544 if (NameString[Index] == 0 || NameString[Index] == '.') {\r
545 NameString[Index] = 0;\r
546 break;\r
547 }\r
548 }\r
549\r
550 if (Index == BufferSize - 1) {\r
551 NameString[Index] = 0;\r
552 }\r
553 //\r
554 // Module Name is got.\r
555 //\r
556 goto Done;\r
557 }\r
558 }\r
559\r
560 //\r
561 // Method 2: Get the name string from ComponentName2 protocol\r
562 //\r
563 Status = gBS->HandleProtocol (\r
564 Handle,\r
565 &gEfiComponentName2ProtocolGuid,\r
566 (VOID **) &ComponentName2\r
567 );\r
568 if (!EFI_ERROR (Status)) {\r
569 //\r
570 // Get the current platform language setting\r
571 //\r
572 if (mPlatformLanguage == NULL) {\r
573 GetEfiGlobalVariable2 (L"PlatformLang", (VOID **) &mPlatformLanguage, NULL);\r
574 }\r
575 if (mPlatformLanguage != NULL) {\r
576 Status = ComponentName2->GetDriverName (\r
577 ComponentName2,\r
578 mPlatformLanguage != NULL ? mPlatformLanguage : "en-US",\r
579 &StringPtr\r
580 );\r
581 if (!EFI_ERROR (Status)) {\r
582 for (Index = 0; Index < BufferSize - 1 && StringPtr[Index] != 0; Index++) {\r
583 NameString[Index] = (CHAR8) StringPtr[Index];\r
584 }\r
585 NameString[Index] = 0;\r
586 //\r
587 // Module Name is got.\r
588 //\r
589 goto Done;\r
590 }\r
591 }\r
592 }\r
593\r
594 if (ModuleGuidIsGet) {\r
595 //\r
596 // Method 3 Try to get the image's FFS UI section by image GUID\r
597 //\r
598 StringPtr = NULL;\r
599 StringSize = 0;\r
600 Status = GetSectionFromAnyFv (\r
601 TempGuid,\r
602 EFI_SECTION_USER_INTERFACE,\r
603 0,\r
604 (VOID **) &StringPtr,\r
605 &StringSize\r
606 );\r
607\r
608 if (!EFI_ERROR (Status)) {\r
609 //\r
610 // Method 3. Get the name string from FFS UI section\r
611 //\r
612 for (Index = 0; Index < BufferSize - 1 && StringPtr[Index] != 0; Index++) {\r
613 NameString[Index] = (CHAR8) StringPtr[Index];\r
614 }\r
615 NameString[Index] = 0;\r
616 FreePool (StringPtr);\r
617 }\r
618 }\r
619\r
620Done:\r
621 //\r
622 // Copy Module Guid\r
623 //\r
624 if (ModuleGuid != NULL) {\r
625 CopyGuid (ModuleGuid, TempGuid);\r
626 if (IsZeroGuid(TempGuid) && (Handle != NULL) && !ModuleGuidIsGet) {\r
627 // Handle is GUID\r
628 CopyGuid (ModuleGuid, (EFI_GUID *) Handle);\r
629 }\r
630 }\r
631\r
632 //\r
633 // Cache the Handle and Guid pairs.\r
634 //\r
635 if (mCachePairCount < CACHE_HANDLE_GUID_COUNT) {\r
636 mCacheHandleGuidTable[mCachePairCount].Handle = Handle;\r
637 CopyGuid (&mCacheHandleGuidTable[mCachePairCount].ModuleGuid, ModuleGuid);\r
638 AsciiStrCpyS (mCacheHandleGuidTable[mCachePairCount].NameString, FPDT_STRING_EVENT_RECORD_NAME_LENGTH, NameString);\r
639 mCachePairCount ++;\r
640 }\r
641\r
642 return Status;\r
643}\r
644\r
645/**\r
6b4d58a1 646 Get the FPDT record identifier.\r
9609d24b 647\r
6b4d58a1
BD
648 @param Attribute The attribute of the Record.\r
649 PerfStartEntry: Start Record.\r
650 PerfEndEntry: End Record.\r
651 @param Handle Pointer to environment specific context used to identify the component being measured.\r
652 @param String Pointer to a Null-terminated ASCII string that identifies the component being measured.\r
653 @param ProgressID On return, pointer to the ProgressID.\r
a0afd019 654\r
6b4d58a1
BD
655 @retval EFI_SUCCESS Get record info successfully.\r
656 @retval EFI_INVALID_PARAMETER No matched FPDT record.\r
a0afd019 657\r
658**/\r
9609d24b 659EFI_STATUS\r
6b4d58a1
BD
660GetFpdtRecordId (\r
661 IN PERF_MEASUREMENT_ATTRIBUTE Attribute,\r
662 IN CONST VOID *Handle,\r
663 IN CONST CHAR8 *String,\r
664 OUT UINT16 *ProgressID\r
a0afd019 665 )\r
666{\r
9609d24b 667 //\r
6b4d58a1 668 // Token to PerfId.\r
9609d24b 669 //\r
6b4d58a1
BD
670 if (String != NULL) {\r
671 if (AsciiStrCmp (String, START_IMAGE_TOK) == 0) { // "StartImage:"\r
672 if (Attribute == PerfStartEntry) {\r
673 *ProgressID = MODULE_START_ID;\r
9609d24b 674 } else {\r
6b4d58a1 675 *ProgressID = MODULE_END_ID;\r
9609d24b 676 }\r
6b4d58a1
BD
677 } else if (AsciiStrCmp (String, LOAD_IMAGE_TOK) == 0) { // "LoadImage:"\r
678 if (Attribute == PerfStartEntry) {\r
679 *ProgressID = MODULE_LOADIMAGE_START_ID;\r
9609d24b 680 } else {\r
6b4d58a1 681 *ProgressID = MODULE_LOADIMAGE_END_ID;\r
9609d24b 682 }\r
6b4d58a1
BD
683 } else if (AsciiStrCmp (String, DRIVERBINDING_START_TOK) == 0) { // "DB:Start:"\r
684 if (Attribute == PerfStartEntry) {\r
685 *ProgressID = MODULE_DB_START_ID;\r
9609d24b 686 } else {\r
6b4d58a1 687 *ProgressID = MODULE_DB_END_ID;\r
9609d24b 688 }\r
6b4d58a1 689 } else if (AsciiStrCmp (String, DRIVERBINDING_SUPPORT_TOK) == 0) { // "DB:Support:"\r
9609d24b
DB
690 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
691 return RETURN_UNSUPPORTED;\r
692 }\r
6b4d58a1
BD
693 if (Attribute == PerfStartEntry) {\r
694 *ProgressID = MODULE_DB_SUPPORT_START_ID;\r
9609d24b 695 } else {\r
6b4d58a1 696 *ProgressID = MODULE_DB_SUPPORT_END_ID;\r
9609d24b 697 }\r
6b4d58a1 698 } else if (AsciiStrCmp (String, DRIVERBINDING_STOP_TOK) == 0) { // "DB:Stop:"\r
9609d24b
DB
699 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
700 return RETURN_UNSUPPORTED;\r
701 }\r
6b4d58a1
BD
702 if (Attribute == PerfStartEntry) {\r
703 *ProgressID = MODULE_DB_STOP_START_ID;\r
9609d24b 704 } else {\r
6b4d58a1 705 *ProgressID = MODULE_DB_STOP_END_ID;\r
9609d24b 706 }\r
6b4d58a1
BD
707 } else if (AsciiStrCmp (String, PEI_TOK) == 0 || // "PEI"\r
708 AsciiStrCmp (String, DXE_TOK) == 0 || // "DXE"\r
709 AsciiStrCmp (String, BDS_TOK) == 0) { // "BDS"\r
710 if (Attribute == PerfStartEntry) {\r
711 *ProgressID = PERF_CROSSMODULE_START_ID;\r
9609d24b 712 } else {\r
6b4d58a1 713 *ProgressID = PERF_CROSSMODULE_END_ID;\r
9609d24b
DB
714 }\r
715 } else { // Pref used in Modules.\r
6b4d58a1
BD
716 if (Attribute == PerfStartEntry) {\r
717 *ProgressID = PERF_INMODULE_START_ID;\r
9609d24b 718 } else {\r
6b4d58a1 719 *ProgressID = PERF_INMODULE_END_ID;\r
9609d24b
DB
720 }\r
721 }\r
6b4d58a1
BD
722 } else if (Handle!= NULL) { // Pref used in Modules.\r
723 if (Attribute == PerfStartEntry) {\r
724 *ProgressID = PERF_INMODULE_START_ID;\r
9609d24b 725 } else {\r
6b4d58a1 726 *ProgressID = PERF_INMODULE_END_ID;\r
9609d24b
DB
727 }\r
728 } else {\r
6b4d58a1 729 return EFI_INVALID_PARAMETER;\r
a0afd019 730 }\r
6b4d58a1
BD
731 return EFI_SUCCESS;\r
732}\r
733\r
734/**\r
735 Copies the string from Source into Destination and updates Length with the\r
736 size of the string.\r
737\r
738 @param Destination - destination of the string copy\r
739 @param Source - pointer to the source string which will get copied\r
740 @param Length - pointer to a length variable to be updated\r
741\r
742**/\r
743VOID\r
744CopyStringIntoPerfRecordAndUpdateLength (\r
745 IN OUT CHAR8 *Destination,\r
746 IN CONST CHAR8 *Source,\r
747 IN OUT UINT8 *Length\r
748 )\r
749{\r
750 UINTN StringLen;\r
751 UINTN DestMax;\r
752\r
753 ASSERT (Source != NULL);\r
a0afd019 754\r
9609d24b 755 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
6b4d58a1 756 DestMax = STRING_SIZE;\r
9609d24b 757 } else {\r
6b4d58a1
BD
758 DestMax = AsciiStrSize (Source);\r
759 if (DestMax > STRING_SIZE) {\r
760 DestMax = STRING_SIZE;\r
761 }\r
762 }\r
763 StringLen = AsciiStrLen (Source);\r
764 if (StringLen >= DestMax) {\r
765 StringLen = DestMax -1;\r
766 }\r
767\r
768 AsciiStrnCpyS(Destination, DestMax, Source, StringLen);\r
769 *Length += (UINT8)DestMax;\r
770\r
771 return;\r
772}\r
773\r
774/**\r
775 Get a string description for device for the given controller handle and update record\r
776 length. If ComponentName2 GetControllerName is supported, the value is included in the string,\r
777 followed by device path, otherwise just device path.\r
a0afd019 778\r
6b4d58a1
BD
779 @param Handle - Image handle\r
780 @param ControllerHandle - Controller handle.\r
781 @param ComponentNameString - Pointer to a location where the string will be saved\r
782 @param Length - Pointer to record length to be updated\r
b504f519 783\r
6b4d58a1
BD
784 @retval EFI_SUCCESS - Successfully got string description for device\r
785 @retval EFI_UNSUPPORTED - Neither ComponentName2 ControllerName nor DevicePath were found\r
9609d24b 786\r
6b4d58a1
BD
787**/\r
788EFI_STATUS\r
789GetDeviceInfoFromHandleAndUpdateLength (\r
790 IN CONST VOID *Handle,\r
791 IN EFI_HANDLE ControllerHandle,\r
792 OUT CHAR8 *ComponentNameString,\r
793 IN OUT UINT8 *Length\r
794 )\r
795{\r
796 EFI_DEVICE_PATH_PROTOCOL *DevicePathProtocol;\r
797 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;\r
798 EFI_STATUS Status;\r
799 CHAR16 *StringPtr;\r
800 CHAR8 *AsciiStringPtr;\r
801 UINTN ControllerNameStringSize;\r
802 UINTN DevicePathStringSize;\r
803\r
804 ControllerNameStringSize = 0;\r
805\r
806 Status = gBS->HandleProtocol (\r
807 (EFI_HANDLE) Handle,\r
808 &gEfiComponentName2ProtocolGuid,\r
809 (VOID **) &ComponentName2\r
810 );\r
811\r
812 if (!EFI_ERROR(Status)) {\r
813 //\r
814 // Get the current platform language setting\r
815 //\r
816 if (mPlatformLanguage == NULL) {\r
817 GetEfiGlobalVariable2 (L"PlatformLang", (VOID **)&mPlatformLanguage, NULL);\r
818 }\r
819\r
820 Status = ComponentName2->GetControllerName (\r
821 ComponentName2,\r
822 ControllerHandle,\r
823 NULL,\r
824 mPlatformLanguage != NULL ? mPlatformLanguage : "en-US",\r
825 &StringPtr\r
826 );\r
827 }\r
828\r
829 if (!EFI_ERROR (Status)) {\r
830 //\r
831 // This will produce the size of the unicode string, which is twice as large as the ASCII one\r
832 // This must be an even number, so ok to divide by 2\r
833 //\r
834 ControllerNameStringSize = StrSize(StringPtr) / 2;\r
835\r
836 //\r
837 // The + 1 is because we want to add a space between the ControllerName and the device path\r
838 //\r
839 if ((ControllerNameStringSize + (*Length) + 1) > FPDT_MAX_PERF_RECORD_SIZE) {\r
9609d24b 840 //\r
6b4d58a1 841 // Only copy enough to fill FPDT_MAX_PERF_RECORD_SIZE worth of the record\r
9609d24b 842 //\r
6b4d58a1 843 ControllerNameStringSize = FPDT_MAX_PERF_RECORD_SIZE - (*Length) - 1;\r
a0afd019 844 }\r
6b4d58a1
BD
845\r
846 UnicodeStrToAsciiStrS(StringPtr, ComponentNameString, ControllerNameStringSize);\r
847\r
848 //\r
849 // Add a space in the end of the ControllerName\r
850 //\r
851 AsciiStringPtr = ComponentNameString + ControllerNameStringSize - 1;\r
852 *AsciiStringPtr = 0x20;\r
853 AsciiStringPtr++;\r
854 *AsciiStringPtr = 0;\r
855 ControllerNameStringSize++;\r
856\r
857 *Length += (UINT8)ControllerNameStringSize;\r
a0afd019 858 }\r
859\r
6b4d58a1
BD
860 //\r
861 // This function returns the device path protocol from the handle specified by Handle. If Handle is\r
862 // NULL or Handle does not contain a device path protocol, then NULL is returned.\r
863 //\r
864 DevicePathProtocol = DevicePathFromHandle(ControllerHandle);\r
865\r
866 if (DevicePathProtocol != NULL) {\r
867 StringPtr = ConvertDevicePathToText (DevicePathProtocol, TRUE, FALSE);\r
868 if (StringPtr != NULL) {\r
869 //\r
870 // This will produce the size of the unicode string, which is twice as large as the ASCII one\r
871 // This must be an even number, so ok to divide by 2\r
872 //\r
873 DevicePathStringSize = StrSize(StringPtr) / 2;\r
874\r
875 if ((DevicePathStringSize + (*Length)) > FPDT_MAX_PERF_RECORD_SIZE) {\r
876 //\r
877 // Only copy enough to fill FPDT_MAX_PERF_RECORD_SIZE worth of the record\r
878 //\r
879 DevicePathStringSize = FPDT_MAX_PERF_RECORD_SIZE - (*Length);\r
880 }\r
881\r
882 if (ControllerNameStringSize != 0) {\r
883 AsciiStringPtr = ComponentNameString + ControllerNameStringSize - 1;\r
884 } else {\r
885 AsciiStringPtr = ComponentNameString;\r
886 }\r
887\r
888 UnicodeStrToAsciiStrS(StringPtr, AsciiStringPtr, DevicePathStringSize);\r
889 *Length += (UINT8)DevicePathStringSize;\r
890 return EFI_SUCCESS;\r
891 }\r
892 }\r
893\r
894 return EFI_UNSUPPORTED;\r
a0afd019 895}\r
896\r
897/**\r
6b4d58a1 898 Create performance record with event description and a timestamp.\r
a0afd019 899\r
6b4d58a1
BD
900 @param CallerIdentifier - Image handle or pointer to caller ID GUID.\r
901 @param Guid - Pointer to a GUID.\r
902 @param String - Pointer to a string describing the measurement.\r
903 @param Ticker - 64-bit time stamp.\r
904 @param Address - Pointer to a location in memory relevant to the measurement.\r
905 @param PerfId - Performance identifier describing the type of measurement.\r
906 @param Attribute - The attribute of the measurement. According to attribute can create a start\r
907 record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,\r
908 or a general record for other Perf macros.\r
a0afd019 909\r
6b4d58a1
BD
910 @retval EFI_SUCCESS - Successfully created performance record.\r
911 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records.\r
912 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
913 pointer or invalid PerfId.\r
914\r
915 @retval EFI_SUCCESS - Successfully created performance record\r
916 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records\r
917 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
918 pointer or invalid PerfId\r
a0afd019 919\r
920**/\r
a0afd019 921EFI_STATUS\r
6b4d58a1
BD
922InsertFpdtRecord (\r
923 IN CONST VOID *CallerIdentifier, OPTIONAL\r
924 IN CONST VOID *Guid, OPTIONAL\r
925 IN CONST CHAR8 *String, OPTIONAL\r
926 IN UINT64 Ticker,\r
927 IN UINT64 Address, OPTIONAL\r
928 IN UINT16 PerfId,\r
929 IN PERF_MEASUREMENT_ATTRIBUTE Attribute\r
a0afd019 930 )\r
931{\r
9609d24b
DB
932 EFI_GUID ModuleGuid;\r
933 CHAR8 ModuleName[FPDT_STRING_EVENT_RECORD_NAME_LENGTH];\r
9609d24b 934 FPDT_RECORD_PTR FpdtRecordPtr;\r
6b4d58a1 935 FPDT_RECORD_PTR CachedFpdtRecordPtr;\r
9609d24b 936 UINT64 TimeStamp;\r
9609d24b 937 CONST CHAR8 *StringPtr;\r
6b4d58a1
BD
938 UINTN DestMax;\r
939 UINTN StringLen;\r
940 EFI_STATUS Status;\r
941 UINT16 ProgressId;\r
9609d24b
DB
942\r
943 StringPtr = NULL;\r
6b4d58a1 944 ProgressId = 0;\r
9609d24b
DB
945 ZeroMem (ModuleName, sizeof (ModuleName));\r
946\r
9609d24b 947 //\r
6b4d58a1
BD
948 // 1. Get the Perf Id for records from PERF_START/PERF_END, PERF_START_EX/PERF_END_EX.\r
949 // notes: For other Perf macros (Attribute == PerfEntry), their Id is known.\r
9609d24b 950 //\r
6b4d58a1 951 if (Attribute != PerfEntry) {\r
9609d24b 952 //\r
6b4d58a1
BD
953 // If PERF_START_EX()/PERF_END_EX() have specified the ProgressID,it has high priority.\r
954 // !!! Note: If the Perf is not the known Token used in the core but have same\r
955 // ID with the core Token, this case will not be supported.\r
956 // And in currtnt usage mode, for the unkown ID, there is a general rule:\r
957 // If it is start pref: the lower 4 bits of the ID should be 0.\r
958 // If it is end pref: the lower 4 bits of the ID should not be 0.\r
959 // If input ID doesn't follow the rule, we will adjust it.\r
a0afd019 960 //\r
6b4d58a1
BD
961 if ((PerfId != 0) && (IsKnownID (PerfId)) && (!IsKnownTokens (String))) {\r
962 return EFI_INVALID_PARAMETER;\r
963 } else if ((PerfId != 0) && (!IsKnownID (PerfId)) && (!IsKnownTokens (String))) {\r
964 if ((Attribute == PerfStartEntry) && ((PerfId & 0x000F) != 0)) {\r
965 PerfId &= 0xFFF0;\r
966 } else if ((Attribute == PerfEndEntry) && ((PerfId & 0x000F) == 0)) {\r
967 PerfId += 1;\r
9609d24b 968 }\r
6b4d58a1 969 } else if (PerfId == 0) {\r
9609d24b 970 //\r
6b4d58a1 971 // Get ProgressID form the String Token.\r
9609d24b 972 //\r
6b4d58a1
BD
973 Status = GetFpdtRecordId (Attribute, CallerIdentifier, String, &ProgressId);\r
974 if (EFI_ERROR (Status)) {\r
975 return Status;\r
9609d24b 976 }\r
6b4d58a1 977 PerfId = ProgressId;\r
9609d24b 978 }\r
9609d24b 979 }\r
a0afd019 980\r
9609d24b 981 //\r
6b4d58a1
BD
982 // 2. Get the buffer to store the FPDT record.\r
983 //\r
984 Status = GetFpdtRecordPtr (FPDT_MAX_PERF_RECORD_SIZE, &FpdtRecordPtr);\r
985 if (EFI_ERROR (Status)) {\r
986 return Status;\r
987 }\r
988\r
989 //\r
990 //3. Get the TimeStamp.\r
9609d24b
DB
991 //\r
992 if (Ticker == 0) {\r
993 Ticker = GetPerformanceCounter ();\r
994 TimeStamp = GetTimeInNanoSecond (Ticker);\r
995 } else if (Ticker == 1) {\r
996 TimeStamp = 0;\r
997 } else {\r
998 TimeStamp = GetTimeInNanoSecond (Ticker);\r
999 }\r
a0afd019 1000\r
9609d24b 1001 //\r
6b4d58a1 1002 // 4. Fill in the FPDT record according to different Performance Identifier.\r
9609d24b 1003 //\r
6b4d58a1
BD
1004 switch (PerfId) {\r
1005 case MODULE_START_ID:\r
1006 case MODULE_END_ID:\r
1007 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
1008 StringPtr = ModuleName;\r
1009 //\r
1010 // Cache the offset of start image start record and use to update the start image end record if needed.\r
1011 //\r
1012 if (Attribute == PerfEntry && PerfId == MODULE_START_ID) {\r
1013 if (mFpdtBufferIsReported) {\r
1014 mCachedLength = mBootRecordSize;\r
1015 } else {\r
1016 mCachedLength = mPerformanceLength;\r
1017 }\r
1018 }\r
1019 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
1020 FpdtRecordPtr.GuidEvent->Header.Type = FPDT_GUID_EVENT_TYPE;\r
1021 FpdtRecordPtr.GuidEvent->Header.Length = sizeof (FPDT_GUID_EVENT_RECORD);\r
1022 FpdtRecordPtr.GuidEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
1023 FpdtRecordPtr.GuidEvent->ProgressID = PerfId;\r
1024 FpdtRecordPtr.GuidEvent->Timestamp = TimeStamp;\r
1025 CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidEvent->Guid));\r
1026 if (CallerIdentifier == NULL && PerfId == MODULE_END_ID && mCachedLength != 0) {\r
1027 if (mFpdtBufferIsReported) {\r
1028 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mCachedLength);\r
1029 } else {\r
1030 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mCachedLength);\r
1031 }\r
1032 CopyMem (&FpdtRecordPtr.GuidEvent->Guid, &CachedFpdtRecordPtr.GuidEvent->Guid, sizeof (FpdtRecordPtr.GuidEvent->Guid));\r
1033 mCachedLength = 0;\r
1034 }\r
1035 }\r
9609d24b
DB
1036 break;\r
1037\r
6b4d58a1
BD
1038 case MODULE_LOADIMAGE_START_ID:\r
1039 case MODULE_LOADIMAGE_END_ID:\r
1040 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
1041 StringPtr = ModuleName;\r
1042 if (PerfId == MODULE_LOADIMAGE_START_ID) {\r
1043 mLoadImageCount ++;\r
1044 //\r
1045 // Cache the offset of load image start record and use to be updated by the load image end record if needed.\r
1046 //\r
1047 if (CallerIdentifier == NULL && Attribute == PerfEntry) {\r
1048 if (mFpdtBufferIsReported) {\r
1049 mCachedLength = mBootRecordSize;\r
1050 } else {\r
1051 mCachedLength = mPerformanceLength;\r
1052 }\r
1053 }\r
9609d24b 1054 }\r
6b4d58a1
BD
1055 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
1056 FpdtRecordPtr.GuidQwordEvent->Header.Type = FPDT_GUID_QWORD_EVENT_TYPE;\r
1057 FpdtRecordPtr.GuidQwordEvent->Header.Length = sizeof (FPDT_GUID_QWORD_EVENT_RECORD);\r
1058 FpdtRecordPtr.GuidQwordEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
1059 FpdtRecordPtr.GuidQwordEvent->ProgressID = PerfId;\r
1060 FpdtRecordPtr.GuidQwordEvent->Timestamp = TimeStamp;\r
1061 FpdtRecordPtr.GuidQwordEvent->Qword = mLoadImageCount;\r
1062 CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordEvent->Guid));\r
1063 if (PerfId == MODULE_LOADIMAGE_END_ID && mCachedLength != 0) {\r
1064 if (mFpdtBufferIsReported) {\r
1065 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mCachedLength);\r
1066 } else {\r
1067 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mCachedLength);\r
1068 }\r
1069 CopyMem (&CachedFpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.GuidQwordEvent->Guid));\r
1070 mCachedLength = 0;\r
9609d24b 1071 }\r
a0afd019 1072 }\r
9609d24b
DB
1073 break;\r
1074\r
6b4d58a1
BD
1075 case MODULE_DB_START_ID:\r
1076 case MODULE_DB_SUPPORT_START_ID:\r
1077 case MODULE_DB_SUPPORT_END_ID:\r
1078 case MODULE_DB_STOP_START_ID:\r
1079 case MODULE_DB_STOP_END_ID:\r
1080 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
1081 StringPtr = ModuleName;\r
1082 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
1083 FpdtRecordPtr.GuidQwordEvent->Header.Type = FPDT_GUID_QWORD_EVENT_TYPE;\r
1084 FpdtRecordPtr.GuidQwordEvent->Header.Length = sizeof (FPDT_GUID_QWORD_EVENT_RECORD);\r
1085 FpdtRecordPtr.GuidQwordEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
1086 FpdtRecordPtr.GuidQwordEvent->ProgressID = PerfId;\r
1087 FpdtRecordPtr.GuidQwordEvent->Timestamp = TimeStamp;\r
1088 FpdtRecordPtr.GuidQwordEvent->Qword = Address;\r
1089 CopyMem (&FpdtRecordPtr.GuidQwordEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordEvent->Guid));\r
9609d24b
DB
1090 }\r
1091 break;\r
1092\r
6b4d58a1
BD
1093 case MODULE_DB_END_ID:\r
1094 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
1095 StringPtr = ModuleName;\r
1096 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
1097 FpdtRecordPtr.GuidQwordStringEvent->Header.Type = FPDT_GUID_QWORD_STRING_EVENT_TYPE;\r
1098 FpdtRecordPtr.GuidQwordStringEvent->Header.Length = sizeof (FPDT_GUID_QWORD_STRING_EVENT_RECORD);;\r
1099 FpdtRecordPtr.GuidQwordStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
1100 FpdtRecordPtr.GuidQwordStringEvent->ProgressID = PerfId;\r
1101 FpdtRecordPtr.GuidQwordStringEvent->Timestamp = TimeStamp;\r
1102 FpdtRecordPtr.GuidQwordStringEvent->Qword = Address;\r
1103 CopyMem (&FpdtRecordPtr.GuidQwordStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.GuidQwordStringEvent->Guid));\r
1104 if (Address != 0) {\r
1105 GetDeviceInfoFromHandleAndUpdateLength(CallerIdentifier, (EFI_HANDLE)(UINTN)Address, FpdtRecordPtr.GuidQwordStringEvent->String, &FpdtRecordPtr.GuidQwordStringEvent->Header.Length);\r
1106 }\r
1107 }\r
9609d24b
DB
1108 break;\r
1109\r
6b4d58a1
BD
1110 case PERF_EVENTSIGNAL_START_ID:\r
1111 case PERF_EVENTSIGNAL_END_ID:\r
1112 case PERF_CALLBACK_START_ID:\r
1113 case PERF_CALLBACK_END_ID:\r
981b7edc 1114 if (String == NULL || Guid == NULL) {\r
6b4d58a1
BD
1115 return EFI_INVALID_PARAMETER;\r
1116 }\r
6b4d58a1 1117 StringPtr = String;\r
981b7edc
DB
1118 if (AsciiStrLen (String) == 0) {\r
1119 StringPtr = "unknown name";\r
1120 }\r
6b4d58a1
BD
1121 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
1122 FpdtRecordPtr.DualGuidStringEvent->Header.Type = FPDT_DUAL_GUID_STRING_EVENT_TYPE;\r
1123 FpdtRecordPtr.DualGuidStringEvent->Header.Length = sizeof (FPDT_DUAL_GUID_STRING_EVENT_RECORD);\r
1124 FpdtRecordPtr.DualGuidStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
1125 FpdtRecordPtr.DualGuidStringEvent->ProgressID = PerfId;\r
1126 FpdtRecordPtr.DualGuidStringEvent->Timestamp = TimeStamp;\r
1127 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid1, CallerIdentifier, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid1));\r
1128 CopyMem (&FpdtRecordPtr.DualGuidStringEvent->Guid2, Guid, sizeof (FpdtRecordPtr.DualGuidStringEvent->Guid2));\r
1129 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DualGuidStringEvent->String, StringPtr, &FpdtRecordPtr.DualGuidStringEvent->Header.Length);\r
1130 }\r
1131 break;\r
1132\r
1133 case PERF_EVENT_ID:\r
1134 case PERF_FUNCTION_START_ID:\r
1135 case PERF_FUNCTION_END_ID:\r
1136 case PERF_INMODULE_START_ID:\r
1137 case PERF_INMODULE_END_ID:\r
1138 case PERF_CROSSMODULE_START_ID:\r
1139 case PERF_CROSSMODULE_END_ID:\r
1140 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
1141 if (String != NULL) {\r
1142 StringPtr = String;\r
1143 } else {\r
1144 StringPtr = ModuleName;\r
1145 }\r
1146 if (AsciiStrLen (StringPtr) == 0) {\r
1147 StringPtr = "unknown name";\r
1148 }\r
1149 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
1150 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
1151 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
1152 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
1153 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;\r
1154 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;\r
1155 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
1156 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
1157 }\r
1158 break;\r
1159\r
1160 default:\r
1161 if (Attribute != PerfEntry) {\r
1162 GetModuleInfoFromHandle ((EFI_HANDLE *)CallerIdentifier, ModuleName, sizeof (ModuleName), &ModuleGuid);\r
1163 if (String != NULL) {\r
1164 StringPtr = String;\r
1165 } else {\r
1166 StringPtr = ModuleName;\r
1167 }\r
1168 if (AsciiStrLen (StringPtr) == 0) {\r
1169 StringPtr = "unknown name";\r
1170 }\r
1171 if (!PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
1172 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
1173 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
1174 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
1175 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;\r
1176 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;\r
1177 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
1178 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
1179 }\r
1180 } else {\r
1181 return EFI_INVALID_PARAMETER;\r
1182 }\r
1183 break;\r
1184 }\r
1185\r
1186 //\r
1187 // 4.2 When PcdEdkiiFpdtStringRecordEnableOnly==TRUE, create string record for all Perf entries.\r
1188 //\r
1189 if (PcdGetBool (PcdEdkiiFpdtStringRecordEnableOnly)) {\r
1190 if (StringPtr == NULL ||PerfId == MODULE_DB_SUPPORT_START_ID || PerfId == MODULE_DB_SUPPORT_END_ID) {\r
1191 return EFI_INVALID_PARAMETER;\r
1192 }\r
1193 FpdtRecordPtr.DynamicStringEvent->Header.Type = FPDT_DYNAMIC_STRING_EVENT_TYPE;\r
1194 FpdtRecordPtr.DynamicStringEvent->Header.Length = sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
1195 FpdtRecordPtr.DynamicStringEvent->Header.Revision = FPDT_RECORD_REVISION_1;\r
1196 FpdtRecordPtr.DynamicStringEvent->ProgressID = PerfId;\r
1197 FpdtRecordPtr.DynamicStringEvent->Timestamp = TimeStamp;\r
981b7edc
DB
1198 if (Guid != NULL) {\r
1199 //\r
1200 // Cache the event guid in string event record.\r
1201 //\r
1202 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, Guid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
1203 } else {\r
1204 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (FpdtRecordPtr.DynamicStringEvent->Guid));\r
1205 }\r
6b4d58a1
BD
1206 if (AsciiStrLen (StringPtr) == 0) {\r
1207 StringPtr = "unknown name";\r
1208 }\r
1209 CopyStringIntoPerfRecordAndUpdateLength (FpdtRecordPtr.DynamicStringEvent->String, StringPtr, &FpdtRecordPtr.DynamicStringEvent->Header.Length);\r
1210\r
1211 if ((PerfId == MODULE_LOADIMAGE_START_ID) || (PerfId == MODULE_END_ID)) {\r
1212 FpdtRecordPtr.DynamicStringEvent->Header.Length = (UINT8)(sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD)+ STRING_SIZE);\r
1213 }\r
1214 if ((PerfId == MODULE_LOADIMAGE_END_ID || PerfId == MODULE_END_ID) && mCachedLength != 0) {\r
1215 if (mFpdtBufferIsReported) {\r
1216 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mBootRecordBuffer + mCachedLength);\r
1217 } else {\r
1218 CachedFpdtRecordPtr.RecordHeader = (EFI_ACPI_5_0_FPDT_PERFORMANCE_RECORD_HEADER *)(mPerformancePointer + mCachedLength);\r
1219 }\r
1220 if (PerfId == MODULE_LOADIMAGE_END_ID) {\r
1221 DestMax = CachedFpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
1222 StringLen = AsciiStrLen (StringPtr);\r
1223 if (StringLen >= DestMax) {\r
1224 StringLen = DestMax -1;\r
1225 }\r
1226 CopyMem (&CachedFpdtRecordPtr.DynamicStringEvent->Guid, &ModuleGuid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));\r
1227 AsciiStrnCpyS (CachedFpdtRecordPtr.DynamicStringEvent->String, DestMax, StringPtr, StringLen);\r
1228 } else if (PerfId == MODULE_END_ID) {\r
1229 DestMax = FpdtRecordPtr.DynamicStringEvent->Header.Length - sizeof (FPDT_DYNAMIC_STRING_EVENT_RECORD);\r
1230 StringLen = AsciiStrLen (CachedFpdtRecordPtr.DynamicStringEvent->String);\r
1231 if (StringLen >= DestMax) {\r
1232 StringLen = DestMax -1;\r
1233 }\r
1234 CopyMem (&FpdtRecordPtr.DynamicStringEvent->Guid, &CachedFpdtRecordPtr.DynamicStringEvent->Guid, sizeof (CachedFpdtRecordPtr.DynamicStringEvent->Guid));\r
1235 AsciiStrnCpyS (FpdtRecordPtr.DynamicStringEvent->String, DestMax, CachedFpdtRecordPtr.DynamicStringEvent->String, StringLen);\r
1236 }\r
1237 mCachedLength = 0;\r
1238 }\r
9609d24b
DB
1239 }\r
1240\r
6b4d58a1
BD
1241 //\r
1242 // 5. Update the length of the used buffer after fill in the record.\r
1243 //\r
1244 if (mFpdtBufferIsReported) {\r
1245 mBootRecordSize += FpdtRecordPtr.RecordHeader->Length;\r
1246 mAcpiBootPerformanceTable->Header.Length += FpdtRecordPtr.RecordHeader->Length;\r
1247 } else {\r
1248 mPerformanceLength += FpdtRecordPtr.RecordHeader->Length;\r
1249 }\r
9609d24b
DB
1250 return EFI_SUCCESS;\r
1251}\r
1252\r
1253/**\r
1254 Dumps all the PEI performance.\r
1255\r
1256 @param HobStart A pointer to a Guid.\r
1257\r
1258 This internal function dumps all the PEI performance log to the DXE performance gauge array.\r
1259 It retrieves the optional GUID HOB for PEI performance and then saves the performance data\r
1260 to DXE performance data structures.\r
f0da4d7d 1261\r
9609d24b
DB
1262**/\r
1263VOID\r
1264InternalGetPeiPerformance (\r
1265 VOID *HobStart\r
1266 )\r
1267{\r
1268 UINT8 *FirmwarePerformanceHob;\r
1269 FPDT_PEI_EXT_PERF_HEADER *PeiPerformanceLogHeader;\r
1270 UINT8 *EventRec;\r
1271 EFI_HOB_GUID_TYPE *GuidHob;\r
1272\r
1273 GuidHob = GetNextGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid, HobStart);\r
1274 while (GuidHob != NULL) {\r
1275 FirmwarePerformanceHob = GET_GUID_HOB_DATA (GuidHob);\r
1276 PeiPerformanceLogHeader = (FPDT_PEI_EXT_PERF_HEADER *)FirmwarePerformanceHob;\r
1277\r
1278 if (mPerformanceLength + PeiPerformanceLogHeader->SizeOfAllEntries > mMaxPerformanceLength) {\r
1279 mPerformancePointer = ReallocatePool (\r
1280 mPerformanceLength,\r
1281 mPerformanceLength +\r
1282 (UINTN)PeiPerformanceLogHeader->SizeOfAllEntries +\r
1283 FIRMWARE_RECORD_BUFFER,\r
1284 mPerformancePointer\r
1285 );\r
1286 ASSERT (mPerformancePointer != NULL);\r
1287 mMaxPerformanceLength = mPerformanceLength +\r
1288 (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries) +\r
1289 FIRMWARE_RECORD_BUFFER;\r
1290 }\r
f0da4d7d 1291\r
9609d24b
DB
1292 EventRec = mPerformancePointer + mPerformanceLength;\r
1293 CopyMem (EventRec, FirmwarePerformanceHob + sizeof (FPDT_PEI_EXT_PERF_HEADER), (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries));\r
a0afd019 1294 //\r
9609d24b 1295 // Update the used buffer size.\r
a0afd019 1296 //\r
9609d24b
DB
1297 mPerformanceLength += (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries);\r
1298 mLoadImageCount += PeiPerformanceLogHeader->LoadImageCount;\r
a0afd019 1299\r
9609d24b
DB
1300 //\r
1301 // Get next performance guid hob\r
1302 //\r
1303 GuidHob = GetNextGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid, GET_NEXT_HOB (GuidHob));\r
a0afd019 1304 }\r
9609d24b 1305}\r
a0afd019 1306\r
9609d24b
DB
1307/**\r
1308 Report Boot Perforamnce table address as report status code.\r
a0afd019 1309\r
9609d24b
DB
1310 @param Event The event of notify protocol.\r
1311 @param Context Notify event context.\r
1312\r
1313**/\r
1314VOID\r
1315EFIAPI\r
1316ReportFpdtRecordBuffer (\r
1317 IN EFI_EVENT Event,\r
1318 IN VOID *Context\r
1319 )\r
1320{\r
1321 EFI_STATUS Status;\r
1322 UINT64 BPDTAddr;\r
1323\r
1324 if (!mFpdtBufferIsReported) {\r
1325 Status = AllocateBootPerformanceTable ();\r
1326 if (!EFI_ERROR(Status)) {\r
1327 BPDTAddr = (UINT64)(UINTN)mAcpiBootPerformanceTable;\r
1328 REPORT_STATUS_CODE_EX (\r
1329 EFI_PROGRESS_CODE,\r
1330 EFI_SOFTWARE_DXE_BS_DRIVER,\r
1331 0,\r
1332 NULL,\r
1333 &gEdkiiFpdtExtendedFirmwarePerformanceGuid,\r
1334 &BPDTAddr,\r
1335 sizeof (UINT64)\r
1336 );\r
1337 }\r
1338 //\r
1339 // Set FPDT report state to TRUE.\r
1340 //\r
1341 mFpdtBufferIsReported = TRUE;\r
a0afd019 1342 }\r
9609d24b 1343}\r
a0afd019 1344\r
a0afd019 1345/**\r
1346 The constructor function initializes Performance infrastructure for DXE phase.\r
1347\r
f0da4d7d 1348 The constructor function publishes Performance and PerformanceEx protocol, allocates memory to log DXE performance\r
a0afd019 1349 and merges PEI performance data to DXE performance log.\r
1350 It will ASSERT() if one of these operations fails and it will always return EFI_SUCCESS.\r
1351\r
1352 @param ImageHandle The firmware allocated handle for the EFI image.\r
1353 @param SystemTable A pointer to the EFI System Table.\r
1354\r
1355 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
1356\r
1357**/\r
1358EFI_STATUS\r
1359EFIAPI\r
1360DxeCorePerformanceLibConstructor (\r
1361 IN EFI_HANDLE ImageHandle,\r
1362 IN EFI_SYSTEM_TABLE *SystemTable\r
1363 )\r
1364{\r
1365 EFI_STATUS Status;\r
9609d24b
DB
1366 EFI_HANDLE Handle;\r
1367 EFI_EVENT ReadyToBootEvent;\r
cfb0aba7
SZ
1368 PERFORMANCE_PROPERTY *PerformanceProperty;\r
1369\r
a0afd019 1370 if (!PerformanceMeasurementEnabled ()) {\r
1371 //\r
1372 // Do not initialize performance infrastructure if not required.\r
1373 //\r
1374 return EFI_SUCCESS;\r
1375 }\r
9609d24b
DB
1376\r
1377 //\r
1378 // Dump normal PEI performance records\r
1379 //\r
1380 InternalGetPeiPerformance (GetHobList());\r
1381\r
a0afd019 1382 //\r
9609d24b 1383 // Install the protocol interfaces for DXE performance library instance.\r
a0afd019 1384 //\r
9609d24b 1385 Handle = NULL;\r
f0da4d7d 1386 Status = gBS->InstallMultipleProtocolInterfaces (\r
9609d24b 1387 &Handle,\r
137fb13d
BD
1388 &gEdkiiPerformanceMeasurementProtocolGuid,\r
1389 &mPerformanceMeasurementInterface,\r
f0da4d7d 1390 NULL\r
a0afd019 1391 );\r
1392 ASSERT_EFI_ERROR (Status);\r
1393\r
9609d24b
DB
1394 //\r
1395 // Register ReadyToBoot event to report StatusCode data\r
1396 //\r
1397 Status = gBS->CreateEventEx (\r
1398 EVT_NOTIFY_SIGNAL,\r
1399 TPL_CALLBACK,\r
1400 ReportFpdtRecordBuffer,\r
1401 NULL,\r
1402 &gEfiEventReadyToBootGuid,\r
1403 &ReadyToBootEvent\r
1404 );\r
a0afd019 1405\r
9609d24b 1406 ASSERT_EFI_ERROR (Status);\r
a0afd019 1407\r
9802d6d5 1408 Status = EfiGetSystemConfigurationTable (&gPerformanceProtocolGuid, (VOID **) &PerformanceProperty);\r
cfb0aba7
SZ
1409 if (EFI_ERROR (Status)) {\r
1410 //\r
1411 // Install configuration table for performance property.\r
1412 //\r
1413 mPerformanceProperty.Revision = PERFORMANCE_PROPERTY_REVISION;\r
1414 mPerformanceProperty.Reserved = 0;\r
1415 mPerformanceProperty.Frequency = GetPerformanceCounterProperties (\r
1416 &mPerformanceProperty.TimerStartValue,\r
1417 &mPerformanceProperty.TimerEndValue\r
1418 );\r
1419 Status = gBS->InstallConfigurationTable (&gPerformanceProtocolGuid, &mPerformanceProperty);\r
1420 ASSERT_EFI_ERROR (Status);\r
1421 }\r
1422\r
1423 return EFI_SUCCESS;\r
a0afd019 1424}\r
1425\r
137fb13d
BD
1426/**\r
1427 Create performance record with event description and a timestamp.\r
1428\r
1429 @param CallerIdentifier - Image handle or pointer to caller ID GUID.\r
1430 @param Guid - Pointer to a GUID.\r
1431 @param String - Pointer to a string describing the measurement.\r
1432 @param TimeStamp - 64-bit time stamp.\r
1433 @param Address - Pointer to a location in memory relevant to the measurement.\r
1434 @param Identifier - Performance identifier describing the type of measurement.\r
1435 @param Attribute - The attribute of the measurement. According to attribute can create a start\r
1436 record for PERF_START/PERF_START_EX, or a end record for PERF_END/PERF_END_EX,\r
1437 or a general record for other Perf macros.\r
1438\r
1439 @retval EFI_SUCCESS - Successfully created performance record.\r
1440 @retval EFI_OUT_OF_RESOURCES - Ran out of space to store the records.\r
1441 @retval EFI_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
1442 pointer or invalid PerfId.\r
1443**/\r
1444EFI_STATUS\r
1445EFIAPI\r
1446CreatePerformanceMeasurement (\r
1447 IN CONST VOID *CallerIdentifier,\r
1448 IN CONST VOID *Guid, OPTIONAL\r
1449 IN CONST CHAR8 *String, OPTIONAL\r
1450 IN UINT64 TimeStamp,\r
1451 IN UINT64 Address, OPTIONAL\r
1452 IN UINT32 Identifier,\r
1453 IN PERF_MEASUREMENT_ATTRIBUTE Attribute\r
1454 )\r
1455{\r
1456 EFI_STATUS Status;\r
1457\r
1458 Status = EFI_SUCCESS;\r
1459\r
1460 if (mLockInsertRecord) {\r
1461 return EFI_INVALID_PARAMETER;\r
1462 }\r
1463 mLockInsertRecord = TRUE;\r
1464\r
6b4d58a1
BD
1465 Status = InsertFpdtRecord (CallerIdentifier, Guid, String, TimeStamp, Address, (UINT16)Identifier, Attribute);\r
1466\r
137fb13d
BD
1467 mLockInsertRecord = FALSE;\r
1468\r
1469 return Status;\r
1470}\r
1471\r
a0afd019 1472/**\r
1473 Adds a record at the end of the performance measurement log\r
1474 that records the start time of a performance measurement.\r
1475\r
1476 Adds a record to the end of the performance measurement log\r
f0da4d7d 1477 that contains the Handle, Token, Module and Identifier.\r
a0afd019 1478 The end time of the new record must be set to zero.\r
1479 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
1480 If TimeStamp is zero, the start time in the record is filled in with the value\r
1481 read from the current time stamp.\r
1482\r
1483 @param Handle Pointer to environment specific context used\r
1484 to identify the component being measured.\r
1485 @param Token Pointer to a Null-terminated ASCII string\r
1486 that identifies the component being measured.\r
1487 @param Module Pointer to a Null-terminated ASCII string\r
1488 that identifies the module being measured.\r
1489 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
1490 @param Identifier 32-bit identifier. If the value is 0, the created record\r
1491 is same as the one created by StartPerformanceMeasurement.\r
a0afd019 1492\r
1493 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
1494 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
1495\r
1496**/\r
1497RETURN_STATUS\r
1498EFIAPI\r
f0da4d7d 1499StartPerformanceMeasurementEx (\r
a0afd019 1500 IN CONST VOID *Handle, OPTIONAL\r
1501 IN CONST CHAR8 *Token, OPTIONAL\r
1502 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
1503 IN UINT64 TimeStamp,\r
1504 IN UINT32 Identifier\r
a0afd019 1505 )\r
1506{\r
137fb13d
BD
1507 CONST CHAR8 *String;\r
1508\r
1509 if (Token != NULL) {\r
1510 String = Token;\r
1511 } else if (Module != NULL) {\r
1512 String = Module;\r
1513 } else {\r
1514 String = NULL;\r
1515 }\r
1516\r
1517 return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfStartEntry);\r
a0afd019 1518}\r
1519\r
1520/**\r
1521 Searches the performance measurement log from the beginning of the log\r
1522 for the first matching record that contains a zero end time and fills in a valid end time.\r
1523\r
1524 Searches the performance measurement log from the beginning of the log\r
9609d24b 1525 for the first record that matches Handle, Token, Module and Identifier and has an end time value of zero.\r
a0afd019 1526 If the record can not be found then return RETURN_NOT_FOUND.\r
1527 If the record is found and TimeStamp is not zero,\r
1528 then the end time in the record is filled in with the value specified by TimeStamp.\r
1529 If the record is found and TimeStamp is zero, then the end time in the matching record\r
1530 is filled in with the current time stamp value.\r
1531\r
1532 @param Handle Pointer to environment specific context used\r
1533 to identify the component being measured.\r
1534 @param Token Pointer to a Null-terminated ASCII string\r
1535 that identifies the component being measured.\r
1536 @param Module Pointer to a Null-terminated ASCII string\r
1537 that identifies the module being measured.\r
1538 @param TimeStamp 64-bit time stamp.\r
f0da4d7d
SZ
1539 @param Identifier 32-bit identifier. If the value is 0, the found record\r
1540 is same as the one found by EndPerformanceMeasurement.\r
a0afd019 1541\r
1542 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
1543 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
1544\r
1545**/\r
1546RETURN_STATUS\r
1547EFIAPI\r
f0da4d7d 1548EndPerformanceMeasurementEx (\r
a0afd019 1549 IN CONST VOID *Handle, OPTIONAL\r
1550 IN CONST CHAR8 *Token, OPTIONAL\r
1551 IN CONST CHAR8 *Module, OPTIONAL\r
f0da4d7d
SZ
1552 IN UINT64 TimeStamp,\r
1553 IN UINT32 Identifier\r
a0afd019 1554 )\r
1555{\r
137fb13d
BD
1556 CONST CHAR8 *String;\r
1557\r
1558 if (Token != NULL) {\r
1559 String = Token;\r
1560 } else if (Module != NULL) {\r
1561 String = Module;\r
1562 } else {\r
1563 String = NULL;\r
1564 }\r
1565\r
1566 return (RETURN_STATUS)CreatePerformanceMeasurement (Handle, NULL, String, TimeStamp, 0, Identifier, PerfEndEntry);\r
a0afd019 1567}\r
1568\r
1569/**\r
1570 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
f0da4d7d
SZ
1571 It can also retrieve the log created by StartPerformanceMeasurement and EndPerformanceMeasurement,\r
1572 and then assign the Identifier with 0.\r
a0afd019 1573\r
9609d24b
DB
1574 !!! Not support!!!\r
1575\r
a0afd019 1576 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
1577 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
1578 and the key for the second entry in the log is returned. If the performance log is empty,\r
1579 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
1580 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
1581 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
1582 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
1583 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
1584 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
f0da4d7d 1585 the log entry is returned in Handle, Token, Module, StartTimeStamp, EndTimeStamp and Identifier.\r
a0afd019 1586 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
1587 If Handle is NULL, then ASSERT().\r
1588 If Token is NULL, then ASSERT().\r
1589 If Module is NULL, then ASSERT().\r
1590 If StartTimeStamp is NULL, then ASSERT().\r
1591 If EndTimeStamp is NULL, then ASSERT().\r
f0da4d7d 1592 If Identifier is NULL, then ASSERT().\r
a0afd019 1593\r
1594 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
1595 0, then the first performance measurement log entry is retrieved.\r
857dfc45 1596 On exit, the key of the next performance log entry.\r
a0afd019 1597 @param Handle Pointer to environment specific context used to identify the component\r
1598 being measured.\r
1599 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
1600 being measured.\r
1601 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
1602 being measured.\r
1603 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
1604 was started.\r
1605 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
1606 was ended.\r
9609d24b
DB
1607 @param Identifier Pointer to the 32-bit identifier that was recorded when the measurement\r
1608 was ended.\r
a0afd019 1609\r
1610 @return The key for the next performance log entry (in general case).\r
1611\r
1612**/\r
1613UINTN\r
1614EFIAPI\r
f0da4d7d 1615GetPerformanceMeasurementEx (\r
9609d24b 1616 IN UINTN LogEntryKey,\r
a0afd019 1617 OUT CONST VOID **Handle,\r
1618 OUT CONST CHAR8 **Token,\r
1619 OUT CONST CHAR8 **Module,\r
1620 OUT UINT64 *StartTimeStamp,\r
f0da4d7d
SZ
1621 OUT UINT64 *EndTimeStamp,\r
1622 OUT UINT32 *Identifier\r
a0afd019 1623 )\r
1624{\r
9609d24b 1625 return 0;\r
a0afd019 1626}\r
1627\r
f0da4d7d
SZ
1628/**\r
1629 Adds a record at the end of the performance measurement log\r
1630 that records the start time of a performance measurement.\r
1631\r
1632 Adds a record to the end of the performance measurement log\r
1633 that contains the Handle, Token, and Module.\r
1634 The end time of the new record must be set to zero.\r
1635 If TimeStamp is not zero, then TimeStamp is used to fill in the start time in the record.\r
1636 If TimeStamp is zero, the start time in the record is filled in with the value\r
1637 read from the current time stamp.\r
1638\r
1639 @param Handle Pointer to environment specific context used\r
1640 to identify the component being measured.\r
1641 @param Token Pointer to a Null-terminated ASCII string\r
1642 that identifies the component being measured.\r
1643 @param Module Pointer to a Null-terminated ASCII string\r
1644 that identifies the module being measured.\r
1645 @param TimeStamp 64-bit time stamp.\r
1646\r
1647 @retval RETURN_SUCCESS The start of the measurement was recorded.\r
1648 @retval RETURN_OUT_OF_RESOURCES There are not enough resources to record the measurement.\r
1649\r
1650**/\r
1651RETURN_STATUS\r
1652EFIAPI\r
1653StartPerformanceMeasurement (\r
1654 IN CONST VOID *Handle, OPTIONAL\r
1655 IN CONST CHAR8 *Token, OPTIONAL\r
1656 IN CONST CHAR8 *Module, OPTIONAL\r
1657 IN UINT64 TimeStamp\r
1658 )\r
1659{\r
137fb13d 1660 return StartPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
f0da4d7d
SZ
1661}\r
1662\r
1663/**\r
1664 Searches the performance measurement log from the beginning of the log\r
1665 for the first matching record that contains a zero end time and fills in a valid end time.\r
1666\r
1667 Searches the performance measurement log from the beginning of the log\r
1668 for the first record that matches Handle, Token, and Module and has an end time value of zero.\r
1669 If the record can not be found then return RETURN_NOT_FOUND.\r
1670 If the record is found and TimeStamp is not zero,\r
1671 then the end time in the record is filled in with the value specified by TimeStamp.\r
1672 If the record is found and TimeStamp is zero, then the end time in the matching record\r
1673 is filled in with the current time stamp value.\r
1674\r
1675 @param Handle Pointer to environment specific context used\r
1676 to identify the component being measured.\r
1677 @param Token Pointer to a Null-terminated ASCII string\r
1678 that identifies the component being measured.\r
1679 @param Module Pointer to a Null-terminated ASCII string\r
1680 that identifies the module being measured.\r
1681 @param TimeStamp 64-bit time stamp.\r
1682\r
1683 @retval RETURN_SUCCESS The end of the measurement was recorded.\r
1684 @retval RETURN_NOT_FOUND The specified measurement record could not be found.\r
1685\r
1686**/\r
1687RETURN_STATUS\r
1688EFIAPI\r
1689EndPerformanceMeasurement (\r
1690 IN CONST VOID *Handle, OPTIONAL\r
1691 IN CONST CHAR8 *Token, OPTIONAL\r
1692 IN CONST CHAR8 *Module, OPTIONAL\r
1693 IN UINT64 TimeStamp\r
1694 )\r
1695{\r
137fb13d 1696 return EndPerformanceMeasurementEx (Handle, Token, Module, TimeStamp, 0);\r
f0da4d7d
SZ
1697}\r
1698\r
1699/**\r
1700 Attempts to retrieve a performance measurement log entry from the performance measurement log.\r
1701 It can also retrieve the log created by StartPerformanceMeasurementEx and EndPerformanceMeasurementEx,\r
1702 and then eliminate the Identifier.\r
1703\r
9609d24b
DB
1704 !!! Not support!!!\r
1705\r
f0da4d7d
SZ
1706 Attempts to retrieve the performance log entry specified by LogEntryKey. If LogEntryKey is\r
1707 zero on entry, then an attempt is made to retrieve the first entry from the performance log,\r
1708 and the key for the second entry in the log is returned. If the performance log is empty,\r
1709 then no entry is retrieved and zero is returned. If LogEntryKey is not zero, then the performance\r
1710 log entry associated with LogEntryKey is retrieved, and the key for the next entry in the log is\r
1711 returned. If LogEntryKey is the key for the last entry in the log, then the last log entry is\r
1712 retrieved and an implementation specific non-zero key value that specifies the end of the performance\r
1713 log is returned. If LogEntryKey is equal this implementation specific non-zero key value, then no entry\r
1714 is retrieved and zero is returned. In the cases where a performance log entry can be returned,\r
1715 the log entry is returned in Handle, Token, Module, StartTimeStamp, and EndTimeStamp.\r
1716 If LogEntryKey is not a valid log entry key for the performance measurement log, then ASSERT().\r
1717 If Handle is NULL, then ASSERT().\r
1718 If Token is NULL, then ASSERT().\r
1719 If Module is NULL, then ASSERT().\r
1720 If StartTimeStamp is NULL, then ASSERT().\r
1721 If EndTimeStamp is NULL, then ASSERT().\r
1722\r
1723 @param LogEntryKey On entry, the key of the performance measurement log entry to retrieve.\r
1724 0, then the first performance measurement log entry is retrieved.\r
1725 On exit, the key of the next performance log entry.\r
1726 @param Handle Pointer to environment specific context used to identify the component\r
1727 being measured.\r
1728 @param Token Pointer to a Null-terminated ASCII string that identifies the component\r
1729 being measured.\r
1730 @param Module Pointer to a Null-terminated ASCII string that identifies the module\r
1731 being measured.\r
1732 @param StartTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
1733 was started.\r
1734 @param EndTimeStamp Pointer to the 64-bit time stamp that was recorded when the measurement\r
1735 was ended.\r
1736\r
1737 @return The key for the next performance log entry (in general case).\r
1738\r
1739**/\r
1740UINTN\r
1741EFIAPI\r
1742GetPerformanceMeasurement (\r
1743 IN UINTN LogEntryKey,\r
1744 OUT CONST VOID **Handle,\r
1745 OUT CONST CHAR8 **Token,\r
1746 OUT CONST CHAR8 **Module,\r
1747 OUT UINT64 *StartTimeStamp,\r
1748 OUT UINT64 *EndTimeStamp\r
1749 )\r
1750{\r
9609d24b 1751 return 0;\r
f0da4d7d
SZ
1752}\r
1753\r
a0afd019 1754/**\r
1755 Returns TRUE if the performance measurement macros are enabled.\r
1756\r
1757 This function returns TRUE if the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
1758 PcdPerformanceLibraryPropertyMask is set. Otherwise FALSE is returned.\r
1759\r
1760 @retval TRUE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
1761 PcdPerformanceLibraryPropertyMask is set.\r
1762 @retval FALSE The PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of\r
1763 PcdPerformanceLibraryPropertyMask is clear.\r
1764\r
1765**/\r
1766BOOLEAN\r
1767EFIAPI\r
1768PerformanceMeasurementEnabled (\r
1769 VOID\r
1770 )\r
1771{\r
1772 return (BOOLEAN) ((PcdGet8(PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0);\r
1773}\r
6b4d58a1
BD
1774\r
1775/**\r
1776 Create performance record with event description and a timestamp.\r
1777\r
1778 @param CallerIdentifier - Image handle or pointer to caller ID GUID\r
1779 @param Guid - Pointer to a GUID\r
1780 @param String - Pointer to a string describing the measurement\r
1781 @param Address - Pointer to a location in memory relevant to the measurement\r
1782 @param Identifier - Performance identifier describing the type of measurement\r
1783\r
1784 @retval RETURN_SUCCESS - Successfully created performance record\r
1785 @retval RETURN_OUT_OF_RESOURCES - Ran out of space to store the records\r
1786 @retval RETURN_INVALID_PARAMETER - Invalid parameter passed to function - NULL\r
1787 pointer or invalid PerfId\r
1788\r
1789**/\r
1790RETURN_STATUS\r
1791EFIAPI\r
1792LogPerformanceMeasurement (\r
1793 IN CONST VOID *CallerIdentifier,\r
1794 IN CONST VOID *Guid, OPTIONAL\r
1795 IN CONST CHAR8 *String, OPTIONAL\r
1796 IN UINT64 Address, OPTIONAL\r
1797 IN UINT32 Identifier\r
1798 )\r
1799{\r
1800 return (RETURN_STATUS)CreatePerformanceMeasurement (CallerIdentifier, Guid, String, 0, Address, Identifier, PerfEntry);\r
1801}\r
1802\r
1803/**\r
1804 Check whether the specified performance measurement can be logged.\r
1805\r
1806 This function returns TRUE when the PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED bit of PcdPerformanceLibraryPropertyMask is set\r
1807 and the Type disable bit in PcdPerformanceLibraryPropertyMask is not set.\r
1808\r
1809 @param Type - Type of the performance measurement entry.\r
1810\r
1811 @retval TRUE The performance measurement can be logged.\r
1812 @retval FALSE The performance measurement can NOT be logged.\r
1813\r
1814**/\r
1815BOOLEAN\r
1816EFIAPI\r
1817LogPerformanceMeasurementEnabled (\r
1818 IN CONST UINTN Type\r
1819 )\r
1820{\r
1821 //\r
1822 // When Performance measurement is enabled and the type is not filtered, the performance can be logged.\r
1823 //\r
1824 if (PerformanceMeasurementEnabled () && (PcdGet8(PcdPerformanceLibraryPropertyMask) & Type) == 0) {\r
1825 return TRUE;\r
1826 }\r
1827 return FALSE;\r
1828}\r