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