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