]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableDxe/FirmwarePerformanceDxe.c
Fix the issue that FreePool is too early in FirmwarePerformanceDxe.
[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 - 2013, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18
19 #include <PiDxe.h>
20
21 #include <Protocol/ReportStatusCodeHandler.h>
22 #include <Protocol/AcpiTable.h>
23 #include <Protocol/SmmCommunication.h>
24 #include <Protocol/LockBox.h>
25 #include <Protocol/Variable.h>
26
27 #include <Guid/Acpi.h>
28 #include <Guid/FirmwarePerformance.h>
29 #include <Guid/EventGroup.h>
30 #include <Guid/EventLegacyBios.h>
31
32 #include <Library/UefiBootServicesTableLib.h>
33 #include <Library/UefiRuntimeServicesTableLib.h>
34 #include <Library/BaseLib.h>
35 #include <Library/DebugLib.h>
36 #include <Library/TimerLib.h>
37 #include <Library/BaseMemoryLib.h>
38 #include <Library/MemoryAllocationLib.h>
39 #include <Library/PcdLib.h>
40 #include <Library/HobLib.h>
41 #include <Library/LockBoxLib.h>
42 #include <Library/UefiLib.h>
43
44 //
45 // ACPI table information used to initialize tables.
46 //
47 #define EFI_ACPI_OEM_ID "INTEL"
48 #define EFI_ACPI_OEM_TABLE_ID 0x2020204F4E414954ULL // "TIANO "
49 #define EFI_ACPI_OEM_REVISION 0x00000001
50 #define EFI_ACPI_CREATOR_ID 0x5446534D // TBD "MSFT"
51 #define EFI_ACPI_CREATOR_REVISION 0x01000013 // TBD
52 #define EXTENSION_RECORD_SIZE 0x10000
53 #define SMM_BOOT_RECORD_COMM_SIZE OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + sizeof(SMM_BOOT_RECORD_COMMUNICATE)
54
55 EFI_RSC_HANDLER_PROTOCOL *mRscHandlerProtocol = NULL;
56
57 BOOLEAN mLockBoxReady = FALSE;
58 EFI_EVENT mReadyToBootEvent;
59 EFI_EVENT mLegacyBootEvent;
60 EFI_EVENT mExitBootServicesEvent;
61 UINTN mFirmwarePerformanceTableTemplateKey = 0;
62 UINT32 mBootRecordSize = 0;
63 UINT32 mBootRecordMaxSize = 0;
64 UINT8 *mBootRecordBuffer = NULL;
65
66 BOOT_PERFORMANCE_TABLE *mAcpiBootPerformanceTable = NULL;
67 S3_PERFORMANCE_TABLE *mAcpiS3PerformanceTable = NULL;
68
69 FIRMWARE_PERFORMANCE_TABLE mFirmwarePerformanceTableTemplate = {
70 {
71 EFI_ACPI_5_0_FIRMWARE_PERFORMANCE_DATA_TABLE_SIGNATURE,
72 sizeof (FIRMWARE_PERFORMANCE_TABLE),
73 EFI_ACPI_5_0_FIRMWARE_PERFORMANCE_DATA_TABLE_REVISION, // Revision
74 0x00, // Checksum will be updated at runtime
75 //
76 // It is expected that these values will be updated at runtime.
77 //
78 EFI_ACPI_OEM_ID, // OEMID is a 6 bytes long field
79 EFI_ACPI_OEM_TABLE_ID, // OEM table identification(8 bytes long)
80 EFI_ACPI_OEM_REVISION, // OEM revision number
81 EFI_ACPI_CREATOR_ID, // ASL compiler vendor ID
82 EFI_ACPI_CREATOR_REVISION, // ASL compiler revision number
83 },
84 //
85 // Firmware Basic Boot Performance Table Pointer Record.
86 //
87 {
88 {
89 EFI_ACPI_5_0_FPDT_RECORD_TYPE_FIRMWARE_BASIC_BOOT_POINTER , // Type
90 sizeof (EFI_ACPI_5_0_FPDT_BOOT_PERFORMANCE_TABLE_POINTER_RECORD), // Length
91 EFI_ACPI_5_0_FPDT_RECORD_REVISION_FIRMWARE_BASIC_BOOT_POINTER // Revision
92 },
93 0, // Reserved
94 0 // BootPerformanceTablePointer will be updated at runtime.
95 },
96 //
97 // S3 Performance Table Pointer Record.
98 //
99 {
100 {
101 EFI_ACPI_5_0_FPDT_RECORD_TYPE_S3_PERFORMANCE_TABLE_POINTER, // Type
102 sizeof (EFI_ACPI_5_0_FPDT_S3_PERFORMANCE_TABLE_POINTER_RECORD), // Length
103 EFI_ACPI_5_0_FPDT_RECORD_REVISION_S3_PERFORMANCE_TABLE_POINTER // Revision
104 },
105 0, // Reserved
106 0 // S3PerformanceTablePointer will be updated at runtime.
107 }
108 };
109
110 BOOT_PERFORMANCE_TABLE mBootPerformanceTableTemplate = {
111 {
112 EFI_ACPI_5_0_FPDT_BOOT_PERFORMANCE_TABLE_SIGNATURE,
113 sizeof (BOOT_PERFORMANCE_TABLE)
114 },
115 {
116 {
117 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_TYPE_FIRMWARE_BASIC_BOOT, // Type
118 sizeof (EFI_ACPI_5_0_FPDT_FIRMWARE_BASIC_BOOT_RECORD), // Length
119 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_REVISION_FIRMWARE_BASIC_BOOT // Revision
120 },
121 0, // Reserved
122 //
123 // These values will be updated at runtime.
124 //
125 0, // ResetEnd
126 0, // OsLoaderLoadImageStart
127 0, // OsLoaderStartImageStart
128 0, // ExitBootServicesEntry
129 0 // ExitBootServicesExit
130 }
131 };
132
133 S3_PERFORMANCE_TABLE mS3PerformanceTableTemplate = {
134 {
135 EFI_ACPI_5_0_FPDT_S3_PERFORMANCE_TABLE_SIGNATURE,
136 sizeof (S3_PERFORMANCE_TABLE)
137 },
138 {
139 {
140 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_TYPE_S3_RESUME, // Type
141 sizeof (EFI_ACPI_5_0_FPDT_S3_RESUME_RECORD), // Length
142 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_REVISION_S3_RESUME // Revision
143 },
144 //
145 // These values will be updated by Firmware Performance PEIM.
146 //
147 0, // ResumeCount
148 0, // FullResume
149 0 // AverageResume
150 },
151 {
152 {
153 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_TYPE_S3_SUSPEND, // Type
154 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD), // Length
155 EFI_ACPI_5_0_FPDT_RUNTIME_RECORD_REVISION_S3_SUSPEND // Revision
156 },
157 //
158 // These values will be updated bye Firmware Performance SMM driver.
159 //
160 0, // SuspendStart
161 0 // SuspendEnd
162 }
163 };
164
165 /**
166 This function calculates and updates an UINT8 checksum.
167
168 @param[in] Buffer Pointer to buffer to checksum
169 @param[in] Size Number of bytes to checksum
170
171 **/
172 VOID
173 FpdtAcpiTableChecksum (
174 IN UINT8 *Buffer,
175 IN UINTN Size
176 )
177 {
178 UINTN ChecksumOffset;
179
180 ChecksumOffset = OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER, Checksum);
181
182 //
183 // Set checksum to 0 first.
184 //
185 Buffer[ChecksumOffset] = 0;
186
187 //
188 // Update checksum value.
189 //
190 Buffer[ChecksumOffset] = CalculateCheckSum8 (Buffer, Size);
191 }
192
193 /**
194 Allocate EfiReservedMemoryType below 4G memory address.
195
196 This function allocates EfiReservedMemoryType below 4G memory address.
197
198 @param[in] Size Size of memory to allocate.
199
200 @return Allocated address for output.
201
202 **/
203 VOID *
204 FpdtAllocateReservedMemoryBelow4G (
205 IN UINTN Size
206 )
207 {
208 UINTN Pages;
209 EFI_PHYSICAL_ADDRESS Address;
210 EFI_STATUS Status;
211 VOID *Buffer;
212
213 Buffer = NULL;
214 Pages = EFI_SIZE_TO_PAGES (Size);
215 Address = 0xffffffff;
216
217 Status = gBS->AllocatePages (
218 AllocateMaxAddress,
219 EfiReservedMemoryType,
220 Pages,
221 &Address
222 );
223 ASSERT_EFI_ERROR (Status);
224
225 if (!EFI_ERROR (Status)) {
226 Buffer = (VOID *) (UINTN) Address;
227 ZeroMem (Buffer, Size);
228 }
229
230 return Buffer;
231 }
232
233 /**
234 Callback function upon VariableArchProtocol and LockBoxProtocol
235 to allocate S3 performance table memory and save the pointer to LockBox.
236
237 @param[in] Event Event whose notification function is being invoked.
238 @param[in] Context Pointer to the notification function's context.
239 **/
240 VOID
241 EFIAPI
242 FpdtAllocateS3PerformanceTableMemory (
243 IN EFI_EVENT Event,
244 IN VOID *Context
245 )
246 {
247 EFI_STATUS Status;
248 VOID *Interface;
249 FIRMWARE_PERFORMANCE_VARIABLE PerformanceVariable;
250 UINTN Size;
251 EFI_PHYSICAL_ADDRESS S3PerformanceTablePointer;
252
253 if (mLockBoxReady && (mAcpiS3PerformanceTable != NULL)) {
254 //
255 // The memory for S3 performance table should have been ready,
256 // and the pointer should have been saved to LockBox, just return.
257 //
258 return;
259 }
260
261 if (!mLockBoxReady) {
262 Status = gBS->LocateProtocol (&gEfiLockBoxProtocolGuid, NULL, &Interface);
263 if (!EFI_ERROR (Status)) {
264 //
265 // LockBox services has been ready.
266 //
267 mLockBoxReady = TRUE;
268 }
269 }
270
271 if (mAcpiS3PerformanceTable == NULL) {
272 Status = gBS->LocateProtocol (&gEfiVariableArchProtocolGuid, NULL, &Interface);
273 if (!EFI_ERROR (Status)) {
274 //
275 // Try to allocate the same runtime buffer as last time boot.
276 //
277 ZeroMem (&PerformanceVariable, sizeof (PerformanceVariable));
278 Size = sizeof (PerformanceVariable);
279 Status = gRT->GetVariable (
280 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
281 &gEfiFirmwarePerformanceGuid,
282 NULL,
283 &Size,
284 &PerformanceVariable
285 );
286 if (!EFI_ERROR (Status)) {
287 Status = gBS->AllocatePages (
288 AllocateAddress,
289 EfiReservedMemoryType,
290 EFI_SIZE_TO_PAGES (sizeof (S3_PERFORMANCE_TABLE)),
291 &PerformanceVariable.S3PerformanceTablePointer
292 );
293 if (!EFI_ERROR (Status)) {
294 mAcpiS3PerformanceTable = (S3_PERFORMANCE_TABLE *) (UINTN) PerformanceVariable.S3PerformanceTablePointer;
295 }
296 }
297 if (mAcpiS3PerformanceTable == NULL) {
298 //
299 // Fail to allocate at specified address, continue to allocate at any address.
300 //
301 mAcpiS3PerformanceTable = (S3_PERFORMANCE_TABLE *) FpdtAllocateReservedMemoryBelow4G (sizeof (S3_PERFORMANCE_TABLE));
302 }
303 DEBUG ((EFI_D_INFO, "FPDT: ACPI S3 Performance Table address = 0x%x\n", mAcpiS3PerformanceTable));
304 if (mAcpiS3PerformanceTable != NULL) {
305 CopyMem (mAcpiS3PerformanceTable, &mS3PerformanceTableTemplate, sizeof (mS3PerformanceTableTemplate));
306 }
307 }
308 }
309
310 if (mLockBoxReady && (mAcpiS3PerformanceTable != NULL)) {
311 //
312 // If LockBox services has been ready and memory for FPDT S3 performance table has been allocated,
313 // save the pointer to LockBox for use in S3 resume.
314 //
315 S3PerformanceTablePointer = (EFI_PHYSICAL_ADDRESS) (UINTN) mAcpiS3PerformanceTable;
316 Status = SaveLockBox (
317 &gFirmwarePerformanceS3PointerGuid,
318 &S3PerformanceTablePointer,
319 sizeof (EFI_PHYSICAL_ADDRESS)
320 );
321 ASSERT_EFI_ERROR (Status);
322 }
323 }
324
325 /**
326 Install ACPI Firmware Performance Data Table (FPDT).
327
328 @return Status code.
329
330 **/
331 EFI_STATUS
332 InstallFirmwarePerformanceDataTable (
333 VOID
334 )
335 {
336 EFI_STATUS Status;
337 EFI_ACPI_TABLE_PROTOCOL *AcpiTableProtocol;
338 UINTN Size;
339 UINT8 *SmmBootRecordCommBuffer;
340 EFI_SMM_COMMUNICATE_HEADER *SmmCommBufferHeader;
341 SMM_BOOT_RECORD_COMMUNICATE *SmmCommData;
342 UINTN CommSize;
343 UINTN BootPerformanceDataSize;
344 UINT8 *BootPerformanceData;
345 EFI_SMM_COMMUNICATION_PROTOCOL *Communication;
346 FIRMWARE_PERFORMANCE_VARIABLE PerformanceVariable;
347
348 //
349 // Get AcpiTable Protocol.
350 //
351 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **) &AcpiTableProtocol);
352 if (EFI_ERROR (Status)) {
353 return Status;
354 }
355
356 //
357 // Collect boot records from SMM drivers.
358 //
359 SmmBootRecordCommBuffer = NULL;
360 SmmCommData = NULL;
361 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &Communication);
362 if (!EFI_ERROR (Status)) {
363 //
364 // Initialize communicate buffer
365 //
366 SmmBootRecordCommBuffer = AllocateZeroPool (SMM_BOOT_RECORD_COMM_SIZE);
367 ASSERT (SmmBootRecordCommBuffer != NULL);
368 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER*)SmmBootRecordCommBuffer;
369 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)SmmCommBufferHeader->Data;
370 ZeroMem((UINT8*)SmmCommData, sizeof(SMM_BOOT_RECORD_COMMUNICATE));
371
372 CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gEfiFirmwarePerformanceGuid);
373 SmmCommBufferHeader->MessageLength = sizeof(SMM_BOOT_RECORD_COMMUNICATE);
374 CommSize = SMM_BOOT_RECORD_COMM_SIZE;
375
376 //
377 // Get the size of boot records.
378 //
379 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE;
380 SmmCommData->BootRecordData = NULL;
381 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);
382 ASSERT_EFI_ERROR (Status);
383
384 if (!EFI_ERROR (SmmCommData->ReturnStatus) && SmmCommData->BootRecordSize != 0) {
385 //
386 // Get all boot records
387 //
388 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA;
389 SmmCommData->BootRecordData = AllocateZeroPool(SmmCommData->BootRecordSize);
390 ASSERT (SmmCommData->BootRecordData != NULL);
391
392 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);
393 ASSERT_EFI_ERROR (Status);
394 ASSERT_EFI_ERROR(SmmCommData->ReturnStatus);
395 }
396 }
397
398 //
399 // Prepare memory for Boot Performance table.
400 // Boot Performance table includes BasicBoot record, and one or more appended Boot Records.
401 //
402 BootPerformanceDataSize = sizeof (BOOT_PERFORMANCE_TABLE) + mBootRecordSize + PcdGet32 (PcdExtFpdtBootRecordPadSize);
403 if (SmmCommData != NULL) {
404 BootPerformanceDataSize += SmmCommData->BootRecordSize;
405 }
406
407 //
408 // Try to allocate the same runtime buffer as last time boot.
409 //
410 ZeroMem (&PerformanceVariable, sizeof (PerformanceVariable));
411 Size = sizeof (PerformanceVariable);
412 Status = gRT->GetVariable (
413 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
414 &gEfiFirmwarePerformanceGuid,
415 NULL,
416 &Size,
417 &PerformanceVariable
418 );
419 if (!EFI_ERROR (Status)) {
420 Status = gBS->AllocatePages (
421 AllocateAddress,
422 EfiReservedMemoryType,
423 EFI_SIZE_TO_PAGES (BootPerformanceDataSize),
424 &PerformanceVariable.BootPerformanceTablePointer
425 );
426 if (!EFI_ERROR (Status)) {
427 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *) (UINTN) PerformanceVariable.BootPerformanceTablePointer;
428 }
429 }
430
431 if (mAcpiBootPerformanceTable == NULL) {
432 //
433 // Fail to allocate at specified address, continue to allocate at any address.
434 //
435 mAcpiBootPerformanceTable = (BOOT_PERFORMANCE_TABLE *) FpdtAllocateReservedMemoryBelow4G (BootPerformanceDataSize);
436 }
437 DEBUG ((EFI_D_INFO, "FPDT: ACPI Boot Performance Table address = 0x%x\n", mAcpiBootPerformanceTable));
438
439 if (mAcpiBootPerformanceTable == NULL) {
440 if (SmmCommData != NULL && SmmCommData->BootRecordData != NULL) {
441 FreePool (SmmCommData->BootRecordData);
442 }
443 if (SmmBootRecordCommBuffer != NULL) {
444 FreePool (SmmBootRecordCommBuffer);
445 }
446 if (mAcpiS3PerformanceTable != NULL) {
447 FreePages (mAcpiS3PerformanceTable, EFI_SIZE_TO_PAGES (sizeof (S3_PERFORMANCE_TABLE)));
448 }
449 return EFI_OUT_OF_RESOURCES;
450 }
451
452 //
453 // Prepare Boot Performance Table.
454 //
455 BootPerformanceData = (UINT8 *) mAcpiBootPerformanceTable;
456 //
457 // Fill Basic Boot record to Boot Performance Table.
458 //
459 CopyMem (mAcpiBootPerformanceTable, &mBootPerformanceTableTemplate, sizeof (mBootPerformanceTableTemplate));
460 BootPerformanceData = BootPerformanceData + mAcpiBootPerformanceTable->Header.Length;
461 //
462 // Fill Boot records from boot drivers.
463 //
464 CopyMem (BootPerformanceData, mBootRecordBuffer, mBootRecordSize);
465 mAcpiBootPerformanceTable->Header.Length += mBootRecordSize;
466 BootPerformanceData = BootPerformanceData + mBootRecordSize;
467 if (SmmCommData != NULL && SmmCommData->BootRecordData != NULL) {
468 //
469 // Fill Boot records from SMM drivers.
470 //
471 CopyMem (BootPerformanceData, SmmCommData->BootRecordData, SmmCommData->BootRecordSize);
472 FreePool (SmmCommData->BootRecordData);
473 mAcpiBootPerformanceTable->Header.Length = (UINT32) (mAcpiBootPerformanceTable->Header.Length + SmmCommData->BootRecordSize);
474 BootPerformanceData = BootPerformanceData + SmmCommData->BootRecordSize;
475 }
476 if (SmmBootRecordCommBuffer != NULL) {
477 FreePool (SmmBootRecordCommBuffer);
478 }
479
480 //
481 // Save Boot Performance Table address to Variable for use in S4 resume.
482 //
483 PerformanceVariable.BootPerformanceTablePointer = (EFI_PHYSICAL_ADDRESS) (UINTN) mAcpiBootPerformanceTable;
484 //
485 // Update Boot Performance Table Pointer in template.
486 //
487 mFirmwarePerformanceTableTemplate.BootPointerRecord.BootPerformanceTablePointer = (UINT64) (UINTN) mAcpiBootPerformanceTable;
488
489 //
490 // Save S3 Performance Table address to Variable for use in S4 resume.
491 //
492 PerformanceVariable.S3PerformanceTablePointer = (EFI_PHYSICAL_ADDRESS) (UINTN) mAcpiS3PerformanceTable;
493 //
494 // Update S3 Performance Table Pointer in template.
495 //
496 mFirmwarePerformanceTableTemplate.S3PointerRecord.S3PerformanceTablePointer = (UINT64) (UINTN) mAcpiS3PerformanceTable;
497 //
498 // Save Runtime Performance Table pointers to Variable.
499 //
500 Status = gRT->SetVariable (
501 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
502 &gEfiFirmwarePerformanceGuid,
503 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
504 sizeof (PerformanceVariable),
505 &PerformanceVariable
506 );
507 ASSERT_EFI_ERROR (Status);
508
509 //
510 // Publish Firmware Performance Data Table.
511 //
512 FpdtAcpiTableChecksum ((UINT8 *) &mFirmwarePerformanceTableTemplate, mFirmwarePerformanceTableTemplate.Header.Length);
513 Status = AcpiTableProtocol->InstallAcpiTable (
514 AcpiTableProtocol,
515 &mFirmwarePerformanceTableTemplate,
516 mFirmwarePerformanceTableTemplate.Header.Length,
517 &mFirmwarePerformanceTableTemplateKey
518 );
519 if (EFI_ERROR (Status)) {
520 FreePages (mAcpiBootPerformanceTable, EFI_SIZE_TO_PAGES (BootPerformanceDataSize));
521 if (mAcpiS3PerformanceTable != NULL) {
522 FreePages (mAcpiS3PerformanceTable, EFI_SIZE_TO_PAGES (sizeof (S3_PERFORMANCE_TABLE)));
523 }
524 mAcpiBootPerformanceTable = NULL;
525 mAcpiS3PerformanceTable = NULL;
526 return Status;
527 }
528
529 //
530 // Free temp Boot record, and update Boot Record to point to Basic Boot performance table.
531 //
532 if (mBootRecordBuffer != NULL) {
533 FreePool (mBootRecordBuffer);
534 }
535 mBootRecordBuffer = (UINT8 *) mAcpiBootPerformanceTable;
536 mBootRecordSize = mAcpiBootPerformanceTable->Header.Length;
537 mBootRecordMaxSize = mBootRecordSize + PcdGet32 (PcdExtFpdtBootRecordPadSize);
538
539 return EFI_SUCCESS;
540 }
541
542 /**
543 Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to
544 install the Firmware Performance Data Table.
545
546 @param[in] Event The Event that is being processed.
547 @param[in] Context The Event Context.
548
549 **/
550 VOID
551 EFIAPI
552 FpdtReadyToBootEventNotify (
553 IN EFI_EVENT Event,
554 IN VOID *Context
555 )
556 {
557 if (mAcpiBootPerformanceTable == NULL) {
558 //
559 // ACPI Firmware Performance Data Table not installed yet, install it now.
560 //
561 InstallFirmwarePerformanceDataTable ();
562 }
563 }
564
565 /**
566 Notify function for event group EFI_EVENT_LEGACY_BOOT_GUID. This is used to
567 record performance data for OsLoaderLoadImageStart in FPDT for legacy boot.
568
569 @param[in] Event The Event that is being processed.
570 @param[in] Context The Event Context.
571
572 **/
573 VOID
574 EFIAPI
575 FpdtLegacyBootEventNotify (
576 IN EFI_EVENT Event,
577 IN VOID *Context
578 )
579 {
580 if (mAcpiBootPerformanceTable == NULL) {
581 //
582 // Firmware Performance Data Table not installed, do nothing.
583 //
584 return ;
585 }
586
587 //
588 // Update Firmware Basic Boot Performance Record for legacy boot.
589 //
590 mAcpiBootPerformanceTable->BasicBoot.OsLoaderLoadImageStart = 0;
591 mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart = GetTimeInNanoSecond (GetPerformanceCounter ());
592 mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesEntry = 0;
593 mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesExit = 0;
594
595 //
596 // Dump FPDT Boot Performance record.
597 //
598 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - ResetEnd = %ld\n", mAcpiBootPerformanceTable->BasicBoot.ResetEnd));
599 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - OsLoaderLoadImageStart = 0\n"));
600 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - OsLoaderStartImageStart = %ld\n", mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart));
601 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - ExitBootServicesEntry = 0\n"));
602 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - ExitBootServicesExit = 0\n"));
603 }
604
605 /**
606 Notify function for event EVT_SIGNAL_EXIT_BOOT_SERVICES. This is used to record
607 performance data for ExitBootServicesEntry in FPDT.
608
609 @param[in] Event The Event that is being processed.
610 @param[in] Context The Event Context.
611
612 **/
613 VOID
614 EFIAPI
615 FpdtExitBootServicesEventNotify (
616 IN EFI_EVENT Event,
617 IN VOID *Context
618 )
619 {
620 if (mAcpiBootPerformanceTable == NULL) {
621 //
622 // Firmware Performance Data Table not installed, do nothing.
623 //
624 return ;
625 }
626
627 //
628 // Update Firmware Basic Boot Performance Record for UEFI boot.
629 //
630 mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesEntry = GetTimeInNanoSecond (GetPerformanceCounter ());
631
632 //
633 // Dump FPDT Boot Performance record.
634 //
635 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - ResetEnd = %ld\n", mAcpiBootPerformanceTable->BasicBoot.ResetEnd));
636 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - OsLoaderLoadImageStart = %ld\n", mAcpiBootPerformanceTable->BasicBoot.OsLoaderLoadImageStart));
637 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - OsLoaderStartImageStart = %ld\n", mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart));
638 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - ExitBootServicesEntry = %ld\n", mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesEntry));
639 //
640 // ExitBootServicesExit will be updated later, so don't dump it here.
641 //
642 }
643
644 /**
645 Report status code listener of FPDT. This is used to collect performance data
646 for OsLoaderLoadImageStart and OsLoaderStartImageStart in FPDT.
647
648 @param[in] CodeType Indicates the type of status code being reported.
649 @param[in] Value Describes the current status of a hardware or software entity.
650 This included information about the class and subclass that is used to
651 classify the entity as well as an operation.
652 @param[in] Instance The enumeration of a hardware or software entity within
653 the system. Valid instance numbers start with 1.
654 @param[in] CallerId This optional parameter may be used to identify the caller.
655 This parameter allows the status code driver to apply different rules to
656 different callers.
657 @param[in] Data This optional parameter may be used to pass additional data.
658
659 @retval EFI_SUCCESS Status code is what we expected.
660 @retval EFI_UNSUPPORTED Status code not supported.
661
662 **/
663 EFI_STATUS
664 EFIAPI
665 FpdtStatusCodeListenerDxe (
666 IN EFI_STATUS_CODE_TYPE CodeType,
667 IN EFI_STATUS_CODE_VALUE Value,
668 IN UINT32 Instance,
669 IN EFI_GUID *CallerId,
670 IN EFI_STATUS_CODE_DATA *Data
671 )
672 {
673 EFI_STATUS Status;
674
675 //
676 // Check whether status code is what we are interested in.
677 //
678 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {
679 return EFI_UNSUPPORTED;
680 }
681
682 Status = EFI_SUCCESS;
683 if (Value == PcdGet32 (PcdProgressCodeOsLoaderLoad)) {
684 //
685 // Progress code for OS Loader LoadImage.
686 //
687 if (mAcpiBootPerformanceTable == NULL) {
688 return Status;
689 }
690
691 //
692 // Update OS Loader LoadImage Start for UEFI boot.
693 //
694 mAcpiBootPerformanceTable->BasicBoot.OsLoaderLoadImageStart = GetTimeInNanoSecond (GetPerformanceCounter ());
695 } else if (Value == PcdGet32 (PcdProgressCodeOsLoaderStart)) {
696 //
697 // Progress code for OS Loader StartImage.
698 //
699 if (mAcpiBootPerformanceTable == NULL) {
700 return Status;
701 }
702
703 //
704 // Update OS Loader StartImage Start for UEFI boot.
705 //
706 mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart = GetTimeInNanoSecond (GetPerformanceCounter ());
707 } else if (Value == (EFI_SOFTWARE_EFI_BOOT_SERVICE | EFI_SW_BS_PC_EXIT_BOOT_SERVICES)) {
708 //
709 // Progress code for ExitBootServices.
710 //
711 if (mAcpiBootPerformanceTable == NULL) {
712 return Status;
713 }
714
715 //
716 // Update ExitBootServicesExit for UEFI boot.
717 //
718 mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesExit = GetTimeInNanoSecond (GetPerformanceCounter ());
719
720 //
721 // Unregister boot time report status code listener.
722 //
723 mRscHandlerProtocol->Unregister (FpdtStatusCodeListenerDxe);
724 } else if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
725 //
726 // Append one or more Boot records
727 //
728 if (mAcpiBootPerformanceTable == NULL) {
729 //
730 // Append Boot records before FPDT ACPI table is installed.
731 //
732 if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {
733 mBootRecordBuffer = ReallocatePool (mBootRecordSize, mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE, mBootRecordBuffer);
734 ASSERT (mBootRecordBuffer != NULL);
735 mBootRecordMaxSize = mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE;
736 }
737 //
738 // Save boot record into the temp memory space.
739 //
740 CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);
741 mBootRecordSize += Data->Size;
742 } else {
743 //
744 // Append Boot records after FPDT ACPI table is installed.
745 //
746 if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {
747 //
748 // No enough space to save boot record.
749 //
750 Status = EFI_OUT_OF_RESOURCES;
751 } else {
752 //
753 // Save boot record into BootPerformance table
754 //
755 CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);
756 mBootRecordSize += Data->Size;
757 mAcpiBootPerformanceTable->Header.Length = mBootRecordSize;
758 }
759 }
760 } else {
761 //
762 // Ignore else progress code.
763 //
764 Status = EFI_UNSUPPORTED;
765 }
766
767 return Status;
768 }
769
770 /**
771 The module Entry Point of the Firmware Performance Data Table DXE driver.
772
773 @param[in] ImageHandle The firmware allocated handle for the EFI image.
774 @param[in] SystemTable A pointer to the EFI System Table.
775
776 @retval EFI_SUCCESS The entry point is executed successfully.
777 @retval Other Some error occurs when executing this entry point.
778
779 **/
780 EFI_STATUS
781 EFIAPI
782 FirmwarePerformanceDxeEntryPoint (
783 IN EFI_HANDLE ImageHandle,
784 IN EFI_SYSTEM_TABLE *SystemTable
785 )
786 {
787 EFI_STATUS Status;
788 EFI_HOB_GUID_TYPE *GuidHob;
789 FIRMWARE_SEC_PERFORMANCE *Performance;
790 VOID *Registration;
791
792 //
793 // Get Report Status Code Handler Protocol.
794 //
795 Status = gBS->LocateProtocol (&gEfiRscHandlerProtocolGuid, NULL, (VOID **) &mRscHandlerProtocol);
796 ASSERT_EFI_ERROR (Status);
797
798 //
799 // Register report status code listener for OS Loader load and start.
800 //
801 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerDxe, TPL_HIGH_LEVEL);
802 ASSERT_EFI_ERROR (Status);
803
804 //
805 // Register the notify function to update FPDT on ExitBootServices Event.
806 //
807 Status = gBS->CreateEventEx (
808 EVT_NOTIFY_SIGNAL,
809 TPL_NOTIFY,
810 FpdtExitBootServicesEventNotify,
811 NULL,
812 &gEfiEventExitBootServicesGuid,
813 &mExitBootServicesEvent
814 );
815 ASSERT_EFI_ERROR (Status);
816
817 //
818 // Create ready to boot event to install ACPI FPDT table.
819 //
820 Status = gBS->CreateEventEx (
821 EVT_NOTIFY_SIGNAL,
822 TPL_NOTIFY,
823 FpdtReadyToBootEventNotify,
824 NULL,
825 &gEfiEventReadyToBootGuid,
826 &mReadyToBootEvent
827 );
828 ASSERT_EFI_ERROR (Status);
829
830 //
831 // Create legacy boot event to log OsLoaderStartImageStart for legacy boot.
832 //
833 Status = gBS->CreateEventEx (
834 EVT_NOTIFY_SIGNAL,
835 TPL_NOTIFY,
836 FpdtLegacyBootEventNotify,
837 NULL,
838 &gEfiEventLegacyBootGuid,
839 &mLegacyBootEvent
840 );
841 ASSERT_EFI_ERROR (Status);
842
843 //
844 // Retrieve GUID HOB data that contains the ResetEnd.
845 //
846 GuidHob = GetFirstGuidHob (&gEfiFirmwarePerformanceGuid);
847 if (GuidHob != NULL) {
848 Performance = (FIRMWARE_SEC_PERFORMANCE *) GET_GUID_HOB_DATA (GuidHob);
849 mBootPerformanceTableTemplate.BasicBoot.ResetEnd = Performance->ResetEnd;
850 } else {
851 //
852 // SEC Performance Data Hob not found, ResetEnd in ACPI FPDT table will be 0.
853 //
854 DEBUG ((EFI_D_ERROR, "FPDT: WARNING: SEC Performance Data Hob not found, ResetEnd will be set to 0!\n"));
855 }
856
857 if (FeaturePcdGet (PcdFirmwarePerformanceDataTableS3Support)) {
858 //
859 // Register callback function upon VariableArchProtocol and LockBoxProtocol
860 // to allocate S3 performance table memory and save the pointer to LockBox.
861 //
862 EfiCreateProtocolNotifyEvent (
863 &gEfiVariableArchProtocolGuid,
864 TPL_CALLBACK,
865 FpdtAllocateS3PerformanceTableMemory,
866 NULL,
867 &Registration
868 );
869 EfiCreateProtocolNotifyEvent (
870 &gEfiLockBoxProtocolGuid,
871 TPL_CALLBACK,
872 FpdtAllocateS3PerformanceTableMemory,
873 NULL,
874 &Registration
875 );
876 } else {
877 //
878 // Exclude S3 Performance Table Pointer from FPDT table template.
879 //
880 mFirmwarePerformanceTableTemplate.Header.Length -= sizeof (EFI_ACPI_5_0_FPDT_S3_PERFORMANCE_TABLE_POINTER_RECORD);
881 }
882
883 return EFI_SUCCESS;
884 }