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