]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableDxe/FirmwarePerformanceDxe.c
Separate memory allocation for FPDT S3 performance table and boot performance table...
[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 = AllocateZeroPool (SMM_BOOT_RECORD_COMM_SIZE);
360 ASSERT (SmmBootRecordCommBuffer != NULL);
361 SmmCommData = NULL;
362 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &Communication);
363 if (!EFI_ERROR (Status)) {
364 //
365 // Initialize communicate buffer
366 //
367 SmmCommBufferHeader = (EFI_SMM_COMMUNICATE_HEADER*)SmmBootRecordCommBuffer;
368 SmmCommData = (SMM_BOOT_RECORD_COMMUNICATE*)SmmCommBufferHeader->Data;
369 ZeroMem((UINT8*)SmmCommData, sizeof(SMM_BOOT_RECORD_COMMUNICATE));
370
371 CopyGuid (&SmmCommBufferHeader->HeaderGuid, &gEfiFirmwarePerformanceGuid);
372 SmmCommBufferHeader->MessageLength = sizeof(SMM_BOOT_RECORD_COMMUNICATE);
373 CommSize = SMM_BOOT_RECORD_COMM_SIZE;
374
375 //
376 // Get the size of boot records.
377 //
378 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_SIZE;
379 SmmCommData->BootRecordData = NULL;
380 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);
381 ASSERT_EFI_ERROR (Status);
382
383 if (!EFI_ERROR (SmmCommData->ReturnStatus) && SmmCommData->BootRecordSize != 0) {
384 //
385 // Get all boot records
386 //
387 SmmCommData->Function = SMM_FPDT_FUNCTION_GET_BOOT_RECORD_DATA;
388 SmmCommData->BootRecordData = AllocateZeroPool(SmmCommData->BootRecordSize);
389 ASSERT (SmmCommData->BootRecordData != NULL);
390
391 Status = Communication->Communicate (Communication, SmmBootRecordCommBuffer, &CommSize);
392 ASSERT_EFI_ERROR (Status);
393 ASSERT_EFI_ERROR(SmmCommData->ReturnStatus);
394 }
395 }
396 FreePool (SmmBootRecordCommBuffer);
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 (mAcpiS3PerformanceTable != NULL) {
444 FreePages (mAcpiS3PerformanceTable, EFI_SIZE_TO_PAGES (sizeof (S3_PERFORMANCE_TABLE)));
445 }
446 return EFI_OUT_OF_RESOURCES;
447 }
448
449 //
450 // Prepare Boot Performance Table.
451 //
452 BootPerformanceData = (UINT8 *) mAcpiBootPerformanceTable;
453 //
454 // Fill Basic Boot record to Boot Performance Table.
455 //
456 CopyMem (mAcpiBootPerformanceTable, &mBootPerformanceTableTemplate, sizeof (mBootPerformanceTableTemplate));
457 BootPerformanceData = BootPerformanceData + mAcpiBootPerformanceTable->Header.Length;
458 //
459 // Fill Boot records from boot drivers.
460 //
461 CopyMem (BootPerformanceData, mBootRecordBuffer, mBootRecordSize);
462 mAcpiBootPerformanceTable->Header.Length += mBootRecordSize;
463 BootPerformanceData = BootPerformanceData + mBootRecordSize;
464 if (SmmCommData != NULL && SmmCommData->BootRecordData != NULL) {
465 //
466 // Fill Boot records from SMM drivers.
467 //
468 CopyMem (BootPerformanceData, SmmCommData->BootRecordData, SmmCommData->BootRecordSize);
469 FreePool (SmmCommData->BootRecordData);
470 mAcpiBootPerformanceTable->Header.Length = (UINT32) (mAcpiBootPerformanceTable->Header.Length + SmmCommData->BootRecordSize);
471 BootPerformanceData = BootPerformanceData + SmmCommData->BootRecordSize;
472 }
473 //
474 // Save Boot Performance Table address to Variable for use in S4 resume.
475 //
476 PerformanceVariable.BootPerformanceTablePointer = (EFI_PHYSICAL_ADDRESS) (UINTN) mAcpiBootPerformanceTable;
477 //
478 // Update Boot Performance Table Pointer in template.
479 //
480 mFirmwarePerformanceTableTemplate.BootPointerRecord.BootPerformanceTablePointer = (UINT64) (UINTN) mAcpiBootPerformanceTable;
481
482 //
483 // Save S3 Performance Table address to Variable for use in S4 resume.
484 //
485 PerformanceVariable.S3PerformanceTablePointer = (EFI_PHYSICAL_ADDRESS) (UINTN) mAcpiS3PerformanceTable;
486 //
487 // Update S3 Performance Table Pointer in template.
488 //
489 mFirmwarePerformanceTableTemplate.S3PointerRecord.S3PerformanceTablePointer = (UINT64) (UINTN) mAcpiS3PerformanceTable;
490 //
491 // Save Runtime Performance Table pointers to Variable.
492 //
493 Status = gRT->SetVariable (
494 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
495 &gEfiFirmwarePerformanceGuid,
496 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
497 sizeof (PerformanceVariable),
498 &PerformanceVariable
499 );
500 ASSERT_EFI_ERROR (Status);
501
502 //
503 // Publish Firmware Performance Data Table.
504 //
505 FpdtAcpiTableChecksum ((UINT8 *) &mFirmwarePerformanceTableTemplate, mFirmwarePerformanceTableTemplate.Header.Length);
506 Status = AcpiTableProtocol->InstallAcpiTable (
507 AcpiTableProtocol,
508 &mFirmwarePerformanceTableTemplate,
509 mFirmwarePerformanceTableTemplate.Header.Length,
510 &mFirmwarePerformanceTableTemplateKey
511 );
512 if (EFI_ERROR (Status)) {
513 FreePages (mAcpiBootPerformanceTable, EFI_SIZE_TO_PAGES (BootPerformanceDataSize));
514 if (mAcpiS3PerformanceTable != NULL) {
515 FreePages (mAcpiS3PerformanceTable, EFI_SIZE_TO_PAGES (sizeof (S3_PERFORMANCE_TABLE)));
516 }
517 mAcpiBootPerformanceTable = NULL;
518 mAcpiS3PerformanceTable = NULL;
519 return Status;
520 }
521
522 //
523 // Free temp Boot record, and update Boot Record to point to Basic Boot performance table.
524 //
525 if (mBootRecordBuffer != NULL) {
526 FreePool (mBootRecordBuffer);
527 }
528 mBootRecordBuffer = (UINT8 *) mAcpiBootPerformanceTable;
529 mBootRecordSize = mAcpiBootPerformanceTable->Header.Length;
530 mBootRecordMaxSize = mBootRecordSize + PcdGet32 (PcdExtFpdtBootRecordPadSize);
531
532 return EFI_SUCCESS;
533 }
534
535 /**
536 Notify function for event group EFI_EVENT_GROUP_READY_TO_BOOT. This is used to
537 install the Firmware Performance Data Table.
538
539 @param[in] Event The Event that is being processed.
540 @param[in] Context The Event Context.
541
542 **/
543 VOID
544 EFIAPI
545 FpdtReadyToBootEventNotify (
546 IN EFI_EVENT Event,
547 IN VOID *Context
548 )
549 {
550 if (mAcpiBootPerformanceTable == NULL) {
551 //
552 // ACPI Firmware Performance Data Table not installed yet, install it now.
553 //
554 InstallFirmwarePerformanceDataTable ();
555 }
556 }
557
558 /**
559 Notify function for event group EFI_EVENT_LEGACY_BOOT_GUID. This is used to
560 record performance data for OsLoaderLoadImageStart in FPDT for legacy boot.
561
562 @param[in] Event The Event that is being processed.
563 @param[in] Context The Event Context.
564
565 **/
566 VOID
567 EFIAPI
568 FpdtLegacyBootEventNotify (
569 IN EFI_EVENT Event,
570 IN VOID *Context
571 )
572 {
573 if (mAcpiBootPerformanceTable == NULL) {
574 //
575 // Firmware Performance Data Table not installed, do nothing.
576 //
577 return ;
578 }
579
580 //
581 // Update Firmware Basic Boot Performance Record for legacy boot.
582 //
583 mAcpiBootPerformanceTable->BasicBoot.OsLoaderLoadImageStart = 0;
584 mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart = GetTimeInNanoSecond (GetPerformanceCounter ());
585 mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesEntry = 0;
586 mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesExit = 0;
587
588 //
589 // Dump FPDT Boot Performance record.
590 //
591 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - ResetEnd = %ld\n", mAcpiBootPerformanceTable->BasicBoot.ResetEnd));
592 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - OsLoaderLoadImageStart = 0\n"));
593 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - OsLoaderStartImageStart = %ld\n", mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart));
594 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - ExitBootServicesEntry = 0\n"));
595 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - ExitBootServicesExit = 0\n"));
596 }
597
598 /**
599 Notify function for event EVT_SIGNAL_EXIT_BOOT_SERVICES. This is used to record
600 performance data for ExitBootServicesEntry in FPDT.
601
602 @param[in] Event The Event that is being processed.
603 @param[in] Context The Event Context.
604
605 **/
606 VOID
607 EFIAPI
608 FpdtExitBootServicesEventNotify (
609 IN EFI_EVENT Event,
610 IN VOID *Context
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 ((EFI_D_INFO, "FPDT: Boot Performance - ResetEnd = %ld\n", mAcpiBootPerformanceTable->BasicBoot.ResetEnd));
629 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - OsLoaderLoadImageStart = %ld\n", mAcpiBootPerformanceTable->BasicBoot.OsLoaderLoadImageStart));
630 DEBUG ((EFI_D_INFO, "FPDT: Boot Performance - OsLoaderStartImageStart = %ld\n", mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart));
631 DEBUG ((EFI_D_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 Report status code listener of FPDT. This is used to collect performance data
639 for OsLoaderLoadImageStart and OsLoaderStartImageStart in FPDT.
640
641 @param[in] CodeType Indicates the type of status code being reported.
642 @param[in] Value Describes the current status of a hardware or software entity.
643 This included information about the class and subclass that is used to
644 classify the entity as well as an operation.
645 @param[in] Instance The enumeration of a hardware or software entity within
646 the system. Valid instance numbers start with 1.
647 @param[in] CallerId This optional parameter may be used to identify the caller.
648 This parameter allows the status code driver to apply different rules to
649 different callers.
650 @param[in] Data This optional parameter may be used to pass additional data.
651
652 @retval EFI_SUCCESS Status code is what we expected.
653 @retval EFI_UNSUPPORTED Status code not supported.
654
655 **/
656 EFI_STATUS
657 EFIAPI
658 FpdtStatusCodeListenerDxe (
659 IN EFI_STATUS_CODE_TYPE CodeType,
660 IN EFI_STATUS_CODE_VALUE Value,
661 IN UINT32 Instance,
662 IN EFI_GUID *CallerId,
663 IN EFI_STATUS_CODE_DATA *Data
664 )
665 {
666 EFI_STATUS Status;
667
668 //
669 // Check whether status code is what we are interested in.
670 //
671 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {
672 return EFI_UNSUPPORTED;
673 }
674
675 Status = EFI_SUCCESS;
676 if (Value == PcdGet32 (PcdProgressCodeOsLoaderLoad)) {
677 //
678 // Progress code for OS Loader LoadImage.
679 //
680 if (mAcpiBootPerformanceTable == NULL) {
681 return Status;
682 }
683
684 //
685 // Update OS Loader LoadImage Start for UEFI boot.
686 //
687 mAcpiBootPerformanceTable->BasicBoot.OsLoaderLoadImageStart = GetTimeInNanoSecond (GetPerformanceCounter ());
688 } else if (Value == PcdGet32 (PcdProgressCodeOsLoaderStart)) {
689 //
690 // Progress code for OS Loader StartImage.
691 //
692 if (mAcpiBootPerformanceTable == NULL) {
693 return Status;
694 }
695
696 //
697 // Update OS Loader StartImage Start for UEFI boot.
698 //
699 mAcpiBootPerformanceTable->BasicBoot.OsLoaderStartImageStart = GetTimeInNanoSecond (GetPerformanceCounter ());
700 } else if (Value == (EFI_SOFTWARE_EFI_BOOT_SERVICE | EFI_SW_BS_PC_EXIT_BOOT_SERVICES)) {
701 //
702 // Progress code for ExitBootServices.
703 //
704 if (mAcpiBootPerformanceTable == NULL) {
705 return Status;
706 }
707
708 //
709 // Update ExitBootServicesExit for UEFI boot.
710 //
711 mAcpiBootPerformanceTable->BasicBoot.ExitBootServicesExit = GetTimeInNanoSecond (GetPerformanceCounter ());
712
713 //
714 // Unregister boot time report status code listener.
715 //
716 mRscHandlerProtocol->Unregister (FpdtStatusCodeListenerDxe);
717 } else if (Data != NULL && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
718 //
719 // Append one or more Boot records
720 //
721 if (mAcpiBootPerformanceTable == NULL) {
722 //
723 // Append Boot records before FPDT ACPI table is installed.
724 //
725 if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {
726 mBootRecordBuffer = ReallocatePool (mBootRecordSize, mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE, mBootRecordBuffer);
727 ASSERT (mBootRecordBuffer != NULL);
728 mBootRecordMaxSize = mBootRecordSize + Data->Size + EXTENSION_RECORD_SIZE;
729 }
730 //
731 // Save boot record into the temp memory space.
732 //
733 CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);
734 mBootRecordSize += Data->Size;
735 } else {
736 //
737 // Append Boot records after FPDT ACPI table is installed.
738 //
739 if (mBootRecordSize + Data->Size > mBootRecordMaxSize) {
740 //
741 // No enough space to save boot record.
742 //
743 Status = EFI_OUT_OF_RESOURCES;
744 } else {
745 //
746 // Save boot record into BootPerformance table
747 //
748 CopyMem (mBootRecordBuffer + mBootRecordSize, Data + 1, Data->Size);
749 mBootRecordSize += Data->Size;
750 mAcpiBootPerformanceTable->Header.Length = mBootRecordSize;
751 }
752 }
753 } else {
754 //
755 // Ignore else progress code.
756 //
757 Status = EFI_UNSUPPORTED;
758 }
759
760 return Status;
761 }
762
763 /**
764 The module Entry Point of the Firmware Performance Data Table DXE driver.
765
766 @param[in] ImageHandle The firmware allocated handle for the EFI image.
767 @param[in] SystemTable A pointer to the EFI System Table.
768
769 @retval EFI_SUCCESS The entry point is executed successfully.
770 @retval Other Some error occurs when executing this entry point.
771
772 **/
773 EFI_STATUS
774 EFIAPI
775 FirmwarePerformanceDxeEntryPoint (
776 IN EFI_HANDLE ImageHandle,
777 IN EFI_SYSTEM_TABLE *SystemTable
778 )
779 {
780 EFI_STATUS Status;
781 EFI_HOB_GUID_TYPE *GuidHob;
782 FIRMWARE_SEC_PERFORMANCE *Performance;
783 VOID *Registration;
784
785 //
786 // Get Report Status Code Handler Protocol.
787 //
788 Status = gBS->LocateProtocol (&gEfiRscHandlerProtocolGuid, NULL, (VOID **) &mRscHandlerProtocol);
789 ASSERT_EFI_ERROR (Status);
790
791 //
792 // Register report status code listener for OS Loader load and start.
793 //
794 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerDxe, TPL_HIGH_LEVEL);
795 ASSERT_EFI_ERROR (Status);
796
797 //
798 // Register the notify function to update FPDT on ExitBootServices Event.
799 //
800 Status = gBS->CreateEventEx (
801 EVT_NOTIFY_SIGNAL,
802 TPL_NOTIFY,
803 FpdtExitBootServicesEventNotify,
804 NULL,
805 &gEfiEventExitBootServicesGuid,
806 &mExitBootServicesEvent
807 );
808 ASSERT_EFI_ERROR (Status);
809
810 //
811 // Create ready to boot event to install ACPI FPDT table.
812 //
813 Status = gBS->CreateEventEx (
814 EVT_NOTIFY_SIGNAL,
815 TPL_NOTIFY,
816 FpdtReadyToBootEventNotify,
817 NULL,
818 &gEfiEventReadyToBootGuid,
819 &mReadyToBootEvent
820 );
821 ASSERT_EFI_ERROR (Status);
822
823 //
824 // Create legacy boot event to log OsLoaderStartImageStart for legacy boot.
825 //
826 Status = gBS->CreateEventEx (
827 EVT_NOTIFY_SIGNAL,
828 TPL_NOTIFY,
829 FpdtLegacyBootEventNotify,
830 NULL,
831 &gEfiEventLegacyBootGuid,
832 &mLegacyBootEvent
833 );
834 ASSERT_EFI_ERROR (Status);
835
836 //
837 // Retrieve GUID HOB data that contains the ResetEnd.
838 //
839 GuidHob = GetFirstGuidHob (&gEfiFirmwarePerformanceGuid);
840 if (GuidHob != NULL) {
841 Performance = (FIRMWARE_SEC_PERFORMANCE *) GET_GUID_HOB_DATA (GuidHob);
842 mBootPerformanceTableTemplate.BasicBoot.ResetEnd = Performance->ResetEnd;
843 } else {
844 //
845 // SEC Performance Data Hob not found, ResetEnd in ACPI FPDT table will be 0.
846 //
847 DEBUG ((EFI_D_ERROR, "FPDT: WARNING: SEC Performance Data Hob not found, ResetEnd will be set to 0!\n"));
848 }
849
850 if (FeaturePcdGet (PcdFirmwarePerformanceDataTableS3Support)) {
851 //
852 // Register callback function upon VariableArchProtocol and LockBoxProtocol
853 // to allocate S3 performance table memory and save the pointer to LockBox.
854 //
855 EfiCreateProtocolNotifyEvent (
856 &gEfiVariableArchProtocolGuid,
857 TPL_CALLBACK,
858 FpdtAllocateS3PerformanceTableMemory,
859 NULL,
860 &Registration
861 );
862 EfiCreateProtocolNotifyEvent (
863 &gEfiLockBoxProtocolGuid,
864 TPL_CALLBACK,
865 FpdtAllocateS3PerformanceTableMemory,
866 NULL,
867 &Registration
868 );
869 } else {
870 //
871 // Exclude S3 Performance Table Pointer from FPDT table template.
872 //
873 mFirmwarePerformanceTableTemplate.Header.Length -= sizeof (EFI_ACPI_5_0_FPDT_S3_PERFORMANCE_TABLE_POINTER_RECORD);
874 }
875
876 return EFI_SUCCESS;
877 }