]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableDxe/FirmwarePerformanceDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / FirmwarePerformanceDataTableDxe / FirmwarePerformanceDxe.c
1 /** @file
2 This module install ACPI Firmware Performance Data Table (FPDT).
3
4 This module register report status code listener to collect performance data
5 for Firmware Basic Boot Performance Record and other boot performance records,
6 and install FPDT to ACPI table.
7
8 Copyright (c) 2011 - 2021, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
10
11 **/
12
13 #include <PiDxe.h>
14
15 #include <Protocol/ReportStatusCodeHandler.h>
16 #include <Protocol/AcpiTable.h>
17 #include <Protocol/LockBox.h>
18 #include <Protocol/Variable.h>
19 #include <Protocol/VariablePolicy.h>
20
21 #include <Guid/Acpi.h>
22 #include <Guid/FirmwarePerformance.h>
23
24 #include <Library/UefiBootServicesTableLib.h>
25 #include <Library/UefiRuntimeServicesTableLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/DxeServicesLib.h>
29 #include <Library/TimerLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/MemoryAllocationLib.h>
32 #include <Library/PcdLib.h>
33 #include <Library/HobLib.h>
34 #include <Library/LockBoxLib.h>
35 #include <Library/UefiLib.h>
36 #include <Library/VariablePolicyHelperLib.h>
37 #include <Library/PerformanceLib.h>
38
39 #define SMM_BOOT_RECORD_COMM_SIZE (OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof(SMM_BOOT_RECORD_COMMUNICATE))
40
41 EFI_RSC_HANDLER_PROTOCOL *mRscHandlerProtocol = NULL;
42
43 BOOLEAN mLockBoxReady = FALSE;
44 EFI_EVENT mReadyToBootEvent;
45 EFI_EVENT mLegacyBootEvent;
46 static EFI_EVENT mExitBootServicesEvent;
47 UINTN mFirmwarePerformanceTableTemplateKey = 0;
48 BOOLEAN mDxeCoreReportStatusCodeEnable = FALSE;
49
50 BOOT_PERFORMANCE_TABLE *mAcpiBootPerformanceTable = NULL;
51 BOOT_PERFORMANCE_TABLE *mReceivedAcpiBootPerformanceTable = NULL;
52 S3_PERFORMANCE_TABLE *mAcpiS3PerformanceTable = NULL;
53
54 FIRMWARE_PERFORMANCE_TABLE mFirmwarePerformanceTableTemplate = {
55 {
56 EFI_ACPI_5_0_FIRMWARE_PERFORMANCE_DATA_TABLE_SIGNATURE,
57 sizeof (FIRMWARE_PERFORMANCE_TABLE),
58 EFI_ACPI_5_0_FIRMWARE_PERFORMANCE_DATA_TABLE_REVISION, // Revision
59 0x00, // Checksum will be updated at runtime
60 //
61 // It is expected that these values will be updated at EntryPoint.
62 //
63 { 0x00 }, // OEM ID is a 6 bytes long field
64 0x00, // OEM Table ID(8 bytes long)
65 0x00, // OEM Revision
66 0x00, // Creator ID
67 0x00, // Creator Revision
68 },
69 //
70 // Firmware Basic Boot Performance Table Pointer Record.
71 //
72 {
73 {
74 EFI_ACPI_5_0_FPDT_RECORD_TYPE_FIRMWARE_BASIC_BOOT_POINTER, // Type
75 sizeof (EFI_ACPI_5_0_FPDT_BOOT_PERFORMANCE_TABLE_POINTER_RECORD), // Length
76 EFI_ACPI_5_0_FPDT_RECORD_REVISION_FIRMWARE_BASIC_BOOT_POINTER // Revision
77 },
78 0, // Reserved
79 0 // BootPerformanceTablePointer will be updated at runtime.
80 },
81 //
82 // S3 Performance Table Pointer Record.
83 //
84 {
85 {
86 EFI_ACPI_5_0_FPDT_RECORD_TYPE_S3_PERFORMANCE_TABLE_POINTER, // Type
87 sizeof (EFI_ACPI_5_0_FPDT_S3_PERFORMANCE_TABLE_POINTER_RECORD), // Length
88 EFI_ACPI_5_0_FPDT_RECORD_REVISION_S3_PERFORMANCE_TABLE_POINTER // Revision
89 },
90 0, // Reserved
91 0 // S3PerformanceTablePointer will be updated at runtime.
92 }
93 };
94
95 BOOT_PERFORMANCE_TABLE mBootPerformanceTableTemplate = {
96 {
97 EFI_ACPI_5_0_FPDT_BOOT_PERFORMANCE_TABLE_SIGNATURE,
98 sizeof (BOOT_PERFORMANCE_TABLE)
99 },
100 {
101 {
102 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_TYPE_FIRMWARE_BASIC_BOOT, // Type
103 sizeof (EFI_ACPI_5_0_FPDT_FIRMWARE_BASIC_BOOT_RECORD), // Length
104 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_REVISION_FIRMWARE_BASIC_BOOT // Revision
105 },
106 0, // Reserved
107 //
108 // These values will be updated at runtime.
109 //
110 0, // ResetEnd
111 0, // OsLoaderLoadImageStart
112 0, // OsLoaderStartImageStart
113 0, // ExitBootServicesEntry
114 0 // ExitBootServicesExit
115 }
116 };
117
118 S3_PERFORMANCE_TABLE mS3PerformanceTableTemplate = {
119 {
120 EFI_ACPI_5_0_FPDT_S3_PERFORMANCE_TABLE_SIGNATURE,
121 sizeof (S3_PERFORMANCE_TABLE)
122 },
123 {
124 {
125 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_TYPE_S3_RESUME, // Type
126 sizeof (EFI_ACPI_5_0_FPDT_S3_RESUME_RECORD), // Length
127 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_REVISION_S3_RESUME // Revision
128 },
129 //
130 // These values will be updated by Firmware Performance PEIM.
131 //
132 0, // ResumeCount
133 0, // FullResume
134 0 // AverageResume
135 },
136 {
137 {
138 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_TYPE_S3_SUSPEND, // Type
139 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD), // Length
140 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_REVISION_S3_SUSPEND // Revision
141 },
142 //
143 // These values will be updated bye Firmware Performance SMM driver.
144 //
145 0, // SuspendStart
146 0 // SuspendEnd
147 }
148 };
149
150 /**
151 This function calculates and updates an UINT8 checksum.
152
153 @param[in] Buffer Pointer to buffer to checksum
154 @param[in] Size Number of bytes to checksum
155
156 **/
157 VOID
158 FpdtAcpiTableChecksum (
159 IN UINT8 *Buffer,
160 IN UINTN Size
161 )
162 {
163 UINTN ChecksumOffset;
164
165 ChecksumOffset = OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER, Checksum);
166
167 //
168 // Set checksum to 0 first.
169 //
170 Buffer[ChecksumOffset] = 0;
171
172 //
173 // Update checksum value.
174 //
175 Buffer[ChecksumOffset] = CalculateCheckSum8 (Buffer, Size);
176 }
177
178 /**
179 Callback function upon VariableArchProtocol and LockBoxProtocol
180 to allocate S3 performance table memory and save the pointer to LockBox.
181
182 @param[in] Event Event whose notification function is being invoked.
183 @param[in] Context Pointer to the notification function's context.
184 **/
185 VOID
186 EFIAPI
187 FpdtAllocateS3PerformanceTableMemory (
188 IN EFI_EVENT Event,
189 IN VOID *Context
190 )
191 {
192 EFI_STATUS Status;
193 VOID *Interface;
194 FIRMWARE_PERFORMANCE_VARIABLE PerformanceVariable;
195 UINTN Size;
196 EFI_PHYSICAL_ADDRESS S3PerformanceTablePointer;
197
198 if (mLockBoxReady && (mAcpiS3PerformanceTable != NULL)) {
199 //
200 // The memory for S3 performance table should have been ready,
201 // and the pointer should have been saved to LockBox, just return.
202 //
203 return;
204 }
205
206 if (!mLockBoxReady) {
207 Status = gBS->LocateProtocol (&gEfiLockBoxProtocolGuid, NULL, &Interface);
208 if (!EFI_ERROR (Status)) {
209 //
210 // LockBox services has been ready.
211 //
212 mLockBoxReady = TRUE;
213 }
214 }
215
216 if (mAcpiS3PerformanceTable == NULL) {
217 Status = gBS->LocateProtocol (&gEfiVariableArchProtocolGuid, NULL, &Interface);
218 if (!EFI_ERROR (Status)) {
219 //
220 // Try to allocate the same runtime buffer as last time boot.
221 //
222 ZeroMem (&PerformanceVariable, sizeof (PerformanceVariable));
223 Size = sizeof (PerformanceVariable);
224 Status = gRT->GetVariable (
225 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
226 &gEfiFirmwarePerformanceGuid,
227 NULL,
228 &Size,
229 &PerformanceVariable
230 );
231 if (!EFI_ERROR (Status)) {
232 Status = gBS->AllocatePages (
233 AllocateAddress,
234 EfiReservedMemoryType,
235 EFI_SIZE_TO_PAGES (sizeof (S3_PERFORMANCE_TABLE)),
236 &PerformanceVariable.S3PerformanceTablePointer
237 );
238 if (!EFI_ERROR (Status)) {
239 mAcpiS3PerformanceTable = (S3_PERFORMANCE_TABLE *)(UINTN)PerformanceVariable.S3PerformanceTablePointer;
240 }
241 }
242
243 if (mAcpiS3PerformanceTable == NULL) {
244 //
245 // Fail to allocate at specified address, continue to allocate at any address.
246 //
247 mAcpiS3PerformanceTable = (S3_PERFORMANCE_TABLE *)AllocatePeiAccessiblePages (
248 EfiReservedMemoryType,
249 EFI_SIZE_TO_PAGES (sizeof (S3_PERFORMANCE_TABLE))
250 );
251 }
252
253 DEBUG ((DEBUG_INFO, "FPDT: ACPI S3 Performance Table address = 0x%x\n", mAcpiS3PerformanceTable));
254 if (mAcpiS3PerformanceTable != NULL) {
255 CopyMem (mAcpiS3PerformanceTable, &mS3PerformanceTableTemplate, sizeof (mS3PerformanceTableTemplate));
256 }
257 }
258 }
259
260 if (mLockBoxReady && (mAcpiS3PerformanceTable != NULL)) {
261 //
262 // If LockBox services has been ready and memory for FPDT S3 performance table has been allocated,
263 // save the pointer to LockBox for use in S3 resume.
264 //
265 S3PerformanceTablePointer = (EFI_PHYSICAL_ADDRESS)(UINTN)mAcpiS3PerformanceTable;
266 Status = SaveLockBox (
267 &gFirmwarePerformanceS3PointerGuid,
268 &S3PerformanceTablePointer,
269 sizeof (EFI_PHYSICAL_ADDRESS)
270 );
271 ASSERT_EFI_ERROR (Status);
272 }
273 }
274
275 /**
276 Install ACPI Firmware Performance Data Table (FPDT).
277
278 @return Status code.
279
280 **/
281 EFI_STATUS
282 InstallFirmwarePerformanceDataTable (
283 VOID
284 )
285 {
286 EFI_STATUS Status;
287 EFI_ACPI_TABLE_PROTOCOL *AcpiTableProtocol;
288 UINTN BootPerformanceDataSize;
289 FIRMWARE_PERFORMANCE_VARIABLE PerformanceVariable;
290 UINTN Size;
291 EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicyProtocol;
292
293 //
294 // Get AcpiTable Protocol.
295 //
296 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTableProtocol);
297 if (EFI_ERROR (Status)) {
298 return Status;
299 }
300
301 //
302 // Get VariablePolicy Protocol.
303 //
304 Status = gBS->LocateProtocol (&gEdkiiVariablePolicyProtocolGuid, NULL, (VOID **)&VariablePolicyProtocol);
305 if (EFI_ERROR (Status)) {
306 return Status;
307 }
308
309 if (mReceivedAcpiBootPerformanceTable != NULL) {
310 mAcpiBootPerformanceTable = mReceivedAcpiBootPerformanceTable;
311 mAcpiBootPerformanceTable->BasicBoot.ResetEnd = mBootPerformanceTableTemplate.BasicBoot.ResetEnd;
312 } else {
313 //
314 // Try to allocate the same runtime buffer as last time boot.
315 //
316 BootPerformanceDataSize = sizeof (BOOT_PERFORMANCE_TABLE);
317 ZeroMem (&PerformanceVariable, sizeof (PerformanceVariable));
318 Size = sizeof (PerformanceVariable);
319 Status = gRT->GetVariable (
320 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
321 &gEfiFirmwarePerformanceGuid,
322 NULL,
323 &Size,
324 &PerformanceVariable
325 );
326 if (!EFI_ERROR (Status)) {
327 Status = gBS->AllocatePages (
328 AllocateAddress,
329 EfiReservedMemoryType,
330 EFI_SIZE_TO_PAGES (BootPerformanceDataSize),
331 &PerformanceVariable.BootPerformanceTablePointer
332 );
333 if (!EFI_ERROR (Status)) {
334 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *)(UINTN)PerformanceVariable.BootPerformanceTablePointer;
335 }
336 }
337
338 if (mAcpiBootPerformanceTable == NULL) {
339 //
340 // Fail to allocate at specified address, continue to allocate at any address.
341 //
342 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *)AllocatePeiAccessiblePages (
343 EfiReservedMemoryType,
344 EFI_SIZE_TO_PAGES (BootPerformanceDataSize)
345 );
346 }
347
348 DEBUG ((DEBUG_INFO, "FPDT: ACPI Boot Performance Table address = 0x%x\n", mAcpiBootPerformanceTable));
349 if (mAcpiBootPerformanceTable == NULL) {
350 return EFI_OUT_OF_RESOURCES;
351 }
352
353 //
354 // Fill Basic Boot record to Boot Performance Table.
355 //
356 CopyMem (mAcpiBootPerformanceTable, &mBootPerformanceTableTemplate, sizeof (mBootPerformanceTableTemplate));
357 }
358
359 BootPerformanceDataSize = mAcpiBootPerformanceTable->Header.Length;
360
361 //
362 // Save Boot Performance Table address to Variable for use in S4 resume.
363 //
364 PerformanceVariable.BootPerformanceTablePointer = (EFI_PHYSICAL_ADDRESS)(UINTN)mAcpiBootPerformanceTable;
365 //
366 // Update Boot Performance Table Pointer in template.
367 //
368 mFirmwarePerformanceTableTemplate.BootPointerRecord.BootPerformanceTablePointer = (UINT64)(UINTN)mAcpiBootPerformanceTable;
369
370 //
371 // Save S3 Performance Table address to Variable for use in S4 resume.
372 //
373 PerformanceVariable.S3PerformanceTablePointer = (EFI_PHYSICAL_ADDRESS)(UINTN)mAcpiS3PerformanceTable;
374 //
375 // Update S3 Performance Table Pointer in template.
376 //
377 mFirmwarePerformanceTableTemplate.S3PointerRecord.S3PerformanceTablePointer = (UINT64)(UINTN)mAcpiS3PerformanceTable;
378 //
379 // Save Runtime Performance Table pointers to Variable.
380 // Don't check SetVariable return status. It doesn't impact FPDT table generation.
381 //
382 gRT->SetVariable (
383 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
384 &gEfiFirmwarePerformanceGuid,
385 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
386 sizeof (PerformanceVariable),
387 &PerformanceVariable
388 );
389
390 //
391 // Lock the variable which stores the Performance Table pointers.
392 //
393 Status = RegisterBasicVariablePolicy (
394 VariablePolicyProtocol,
395 &gEfiFirmwarePerformanceGuid,
396 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
397 VARIABLE_POLICY_NO_MIN_SIZE,
398 VARIABLE_POLICY_NO_MAX_SIZE,
399 VARIABLE_POLICY_NO_MUST_ATTR,
400 VARIABLE_POLICY_NO_CANT_ATTR,
401 VARIABLE_POLICY_TYPE_LOCK_NOW
402 );
403 if (EFI_ERROR (Status)) {
404 DEBUG ((DEBUG_ERROR, "[FirmwarePerformanceDxe] Error when lock variable %s, Status = %r\n", EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME, Status));
405 ASSERT_EFI_ERROR (Status);
406 }
407
408 //
409 // Publish Firmware Performance Data Table.
410 //
411 FpdtAcpiTableChecksum ((UINT8 *)&mFirmwarePerformanceTableTemplate, mFirmwarePerformanceTableTemplate.Header.Length);
412 Status = AcpiTableProtocol->InstallAcpiTable (
413 AcpiTableProtocol,
414 &mFirmwarePerformanceTableTemplate,
415 mFirmwarePerformanceTableTemplate.Header.Length,
416 &mFirmwarePerformanceTableTemplateKey
417 );
418 if (EFI_ERROR (Status)) {
419 if (mAcpiBootPerformanceTable != NULL) {
420 FreePages (mAcpiBootPerformanceTable, EFI_SIZE_TO_PAGES (BootPerformanceDataSize));
421 }
422
423 if (mAcpiS3PerformanceTable != NULL) {
424 FreePages (mAcpiS3PerformanceTable, EFI_SIZE_TO_PAGES (sizeof (S3_PERFORMANCE_TABLE)));
425 }
426
427 mAcpiBootPerformanceTable = NULL;
428 mAcpiS3PerformanceTable = NULL;
429 return Status;
430 }
431
432 return EFI_SUCCESS;
433 }
434
435 /**
436 Report status code listener of FPDT. This is used to collect performance data
437 for OsLoaderLoadImageStart and OsLoaderStartImageStart in FPDT.
438
439 @param[in] CodeType Indicates the type of status code being reported.
440 @param[in] Value Describes the current status of a hardware or software entity.
441 This included information about the class and subclass that is used to
442 classify the entity as well as an operation.
443 @param[in] Instance The enumeration of a hardware or software entity within
444 the system. Valid instance numbers start with 1.
445 @param[in] CallerId This optional parameter may be used to identify the caller.
446 This parameter allows the status code driver to apply different rules to
447 different callers.
448 @param[in] Data This optional parameter may be used to pass additional data.
449
450 @retval EFI_SUCCESS Status code is what we expected.
451 @retval EFI_UNSUPPORTED Status code not supported.
452
453 **/
454 EFI_STATUS
455 EFIAPI
456 FpdtStatusCodeListenerDxe (
457 IN EFI_STATUS_CODE_TYPE CodeType,
458 IN EFI_STATUS_CODE_VALUE Value,
459 IN UINT32 Instance,
460 IN EFI_GUID *CallerId,
461 IN EFI_STATUS_CODE_DATA *Data
462 )
463 {
464 EFI_STATUS Status;
465
466 //
467 // Check whether status code is what we are interested in.
468 //
469 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {
470 return EFI_UNSUPPORTED;
471 }
472
473 if (Value == (EFI_SOFTWARE_DXE_CORE | EFI_SW_DXE_CORE_PC_HANDOFF_TO_NEXT)) {
474 //
475 // DxeCore ReportStatusCode Enable so that the capability can be supported.
476 //
477 mDxeCoreReportStatusCodeEnable = TRUE;
478 }
479
480 Status = EFI_SUCCESS;
481 if (Value == PcdGet32 (PcdProgressCodeOsLoaderLoad)) {
482 //
483 // Progress code for OS Loader LoadImage.
484 //
485 if (mAcpiBootPerformanceTable == NULL) {
486 return Status;
487 }
488
489 //
490 // Update OS Loader LoadImage Start for UEFI boot.
491 //
492 mAcpiBootPerformanceTable->BasicBoot.OsLoaderLoadImageStart = GetTimeInNanoSecond (GetPerformanceCounter ());
493 } else if (Value == PcdGet32 (PcdProgressCodeOsLoaderStart)) {
494 //
495 // Progress code for OS Loader StartImage.
496 //
497 if (mAcpiBootPerformanceTable == NULL) {
498 return Status;
499 }
500
501 //
502 // Update OS Loader StartImage Start for UEFI boot.
503 //
504 mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart = GetTimeInNanoSecond (GetPerformanceCounter ());
505 } else if (Value == (EFI_SOFTWARE_EFI_BOOT_SERVICE | EFI_SW_BS_PC_EXIT_BOOT_SERVICES)) {
506 //
507 // Unregister boot time report status code listener.
508 //
509 mRscHandlerProtocol->Unregister (FpdtStatusCodeListenerDxe);
510
511 //
512 // Progress code for ExitBootServices.
513 //
514 if (mAcpiBootPerformanceTable == NULL) {
515 return Status;
516 }
517
518 //
519 // Update ExitBootServicesExit for UEFI boot.
520 //
521 mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesExit = GetTimeInNanoSecond (GetPerformanceCounter ());
522 } else if (Value == (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_LEGACY_BOOT_EVENT)) {
523 if (mAcpiBootPerformanceTable == NULL) {
524 //
525 // Firmware Performance Data Table not installed, do nothing.
526 //
527 return Status;
528 }
529
530 //
531 // Update Firmware Basic Boot Performance Record for legacy boot.
532 //
533 mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart = GetTimeInNanoSecond (GetPerformanceCounter ());
534
535 //
536 // Dump FPDT Boot Performance record.
537 //
538 DEBUG ((DEBUG_INFO, "FPDT: Boot Performance - ResetEnd = %ld\n", mAcpiBootPerformanceTable->BasicBoot.ResetEnd));
539 DEBUG ((DEBUG_INFO, "FPDT: Boot Performance - OsLoaderLoadImageStart = 0\n"));
540 DEBUG ((DEBUG_INFO, "FPDT: Boot Performance - OsLoaderStartImageStart = %ld\n", mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart));
541 DEBUG ((DEBUG_INFO, "FPDT: Boot Performance - ExitBootServicesEntry = 0\n"));
542 DEBUG ((DEBUG_INFO, "FPDT: Boot Performance - ExitBootServicesExit = 0\n"));
543 } else if ((Data != NULL) && CompareGuid (&Data->Type, &gEdkiiFpdtExtendedFirmwarePerformanceGuid)) {
544 //
545 // Get the Boot performance table and then install it to ACPI table.
546 //
547 CopyMem (&mReceivedAcpiBootPerformanceTable, Data + 1, Data->Size);
548 InstallFirmwarePerformanceDataTable ();
549 } else if ((Data != NULL) && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
550 DEBUG ((DEBUG_ERROR, "FpdtStatusCodeListenerDxe: Performance data reported through gEfiFirmwarePerformanceGuid will not be collected by FirmwarePerformanceDataTableDxe\n"));
551 Status = EFI_UNSUPPORTED;
552 } else {
553 //
554 // Ignore else progress code.
555 //
556 Status = EFI_UNSUPPORTED;
557 }
558
559 return Status;
560 }
561
562 /**
563 Notify function for event EndOfDxe.
564
565 This is used to install ACPI Firmware Performance Data Table for basic boot records.
566
567 @param[in] Event The Event that is being processed.
568 @param[in] Context The Event Context.
569
570 **/
571 VOID
572 EFIAPI
573 FpdtEndOfDxeEventNotify (
574 IN EFI_EVENT Event,
575 IN VOID *Context
576 )
577 {
578 //
579 // When performance is enabled, the FPDT will be installed when DxeCorePerformanceLib report the data to FimwarePerformanceDxe.
580 // This is used to install the FPDT for the basic boot recods when performance infrastructure is not enabled.
581 //
582 if ((PcdGet8 (PcdPerformanceLibraryPropertyMask) & PERFORMANCE_LIBRARY_PROPERTY_MEASUREMENT_ENABLED) != 0) {
583 return;
584 }
585
586 ASSERT (mReceivedAcpiBootPerformanceTable == NULL);
587 InstallFirmwarePerformanceDataTable ();
588 }
589
590 /**
591 Notify function for event EVT_SIGNAL_EXIT_BOOT_SERVICES. This is used to record
592 performance data for ExitBootServicesEntry in FPDT.
593
594 @param[in] Event The Event that is being processed.
595 @param[in] Context The Event Context.
596
597 **/
598 VOID
599 EFIAPI
600 FpdtExitBootServicesEventNotify (
601 IN EFI_EVENT Event,
602 IN VOID *Context
603 )
604 {
605 if (!mDxeCoreReportStatusCodeEnable) {
606 //
607 // When DxeCore Report Status Code is disabled,
608 // Unregister boot time report status code listener at ExitBootService Event.
609 //
610 mRscHandlerProtocol->Unregister (FpdtStatusCodeListenerDxe);
611 }
612
613 if (mAcpiBootPerformanceTable == NULL) {
614 //
615 // Firmware Performance Data Table not installed, do nothing.
616 //
617 return;
618 }
619
620 //
621 // Update Firmware Basic Boot Performance Record for UEFI boot.
622 //
623 mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesEntry = GetTimeInNanoSecond (GetPerformanceCounter ());
624
625 //
626 // Dump FPDT Boot Performance record.
627 //
628 DEBUG ((DEBUG_INFO, "FPDT: Boot Performance - ResetEnd = %ld\n", mAcpiBootPerformanceTable->BasicBoot.ResetEnd));
629 DEBUG ((DEBUG_INFO, "FPDT: Boot Performance - OsLoaderLoadImageStart = %ld\n", mAcpiBootPerformanceTable->BasicBoot.OsLoaderLoadImageStart));
630 DEBUG ((DEBUG_INFO, "FPDT: Boot Performance - OsLoaderStartImageStart = %ld\n", mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart));
631 DEBUG ((DEBUG_INFO, "FPDT: Boot Performance - ExitBootServicesEntry = %ld\n", mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesEntry));
632 //
633 // ExitBootServicesExit will be updated later, so don't dump it here.
634 //
635 }
636
637 /**
638 The module Entry Point of the Firmware Performance Data Table DXE driver.
639
640 @param[in] ImageHandle The firmware allocated handle for the EFI image.
641 @param[in] SystemTable A pointer to the EFI System Table.
642
643 @retval EFI_SUCCESS The entry point is executed successfully.
644 @retval Other Some error occurs when executing this entry point.
645
646 **/
647 EFI_STATUS
648 EFIAPI
649 FirmwarePerformanceDxeEntryPoint (
650 IN EFI_HANDLE ImageHandle,
651 IN EFI_SYSTEM_TABLE *SystemTable
652 )
653 {
654 EFI_STATUS Status;
655 EFI_HOB_GUID_TYPE *GuidHob;
656 FIRMWARE_SEC_PERFORMANCE *Performance;
657 VOID *Registration;
658 UINT64 OemTableId;
659 EFI_EVENT EndOfDxeEvent;
660
661 CopyMem (
662 mFirmwarePerformanceTableTemplate.Header.OemId,
663 PcdGetPtr (PcdAcpiDefaultOemId),
664 sizeof (mFirmwarePerformanceTableTemplate.Header.OemId)
665 );
666 OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
667 CopyMem (&mFirmwarePerformanceTableTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));
668 mFirmwarePerformanceTableTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
669 mFirmwarePerformanceTableTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
670 mFirmwarePerformanceTableTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
671
672 //
673 // Get Report Status Code Handler Protocol.
674 //
675 Status = gBS->LocateProtocol (&gEfiRscHandlerProtocolGuid, NULL, (VOID **)&mRscHandlerProtocol);
676 ASSERT_EFI_ERROR (Status);
677
678 //
679 // Register report status code listener for OS Loader load and start.
680 //
681 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerDxe, TPL_HIGH_LEVEL);
682 ASSERT_EFI_ERROR (Status);
683
684 //
685 // Register the notify function to install FPDT at EndOfDxe.
686 //
687 Status = gBS->CreateEventEx (
688 EVT_NOTIFY_SIGNAL,
689 TPL_NOTIFY,
690 FpdtEndOfDxeEventNotify,
691 NULL,
692 &gEfiEndOfDxeEventGroupGuid,
693 &EndOfDxeEvent
694 );
695 ASSERT_EFI_ERROR (Status);
696
697 //
698 // Register the notify function to update FPDT on ExitBootServices Event.
699 //
700 Status = gBS->CreateEventEx (
701 EVT_NOTIFY_SIGNAL,
702 TPL_NOTIFY,
703 FpdtExitBootServicesEventNotify,
704 NULL,
705 &gEfiEventExitBootServicesGuid,
706 &mExitBootServicesEvent
707 );
708 ASSERT_EFI_ERROR (Status);
709
710 //
711 // Retrieve GUID HOB data that contains the ResetEnd.
712 //
713 GuidHob = GetFirstGuidHob (&gEfiFirmwarePerformanceGuid);
714 if (GuidHob != NULL) {
715 Performance = (FIRMWARE_SEC_PERFORMANCE *)GET_GUID_HOB_DATA (GuidHob);
716 mBootPerformanceTableTemplate.BasicBoot.ResetEnd = Performance->ResetEnd;
717 } else {
718 //
719 // SEC Performance Data Hob not found, ResetEnd in ACPI FPDT table will be 0.
720 //
721 DEBUG ((DEBUG_WARN, "FPDT: WARNING: SEC Performance Data Hob not found, ResetEnd will be set to 0!\n"));
722 }
723
724 if (FeaturePcdGet (PcdFirmwarePerformanceDataTableS3Support)) {
725 //
726 // Register callback function upon VariableArchProtocol and LockBoxProtocol
727 // to allocate S3 performance table memory and save the pointer to LockBox.
728 //
729 EfiCreateProtocolNotifyEvent (
730 &gEfiVariableArchProtocolGuid,
731 TPL_CALLBACK,
732 FpdtAllocateS3PerformanceTableMemory,
733 NULL,
734 &Registration
735 );
736 EfiCreateProtocolNotifyEvent (
737 &gEfiLockBoxProtocolGuid,
738 TPL_CALLBACK,
739 FpdtAllocateS3PerformanceTableMemory,
740 NULL,
741 &Registration
742 );
743 } else {
744 //
745 // Exclude S3 Performance Table Pointer from FPDT table template.
746 //
747 mFirmwarePerformanceTableTemplate.Header.Length -= sizeof (EFI_ACPI_5_0_FPDT_S3_PERFORMANCE_TABLE_POINTER_RECORD);
748 }
749
750 return EFI_SUCCESS;
751 }