]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTablePei/FirmwarePerformancePei.c
MdeModulePkg/FirmwarePerformancePei:Add FPDT records for S3 phase
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / FirmwarePerformanceDataTablePei / FirmwarePerformancePei.c
1 /** @file
2 This module updates S3 Resume Performance Record in ACPI Firmware Performance
3 Data Table in S3 resume boot mode.
4
5 This module register report status code listener to collect performance data
6 for S3 Resume Performance Record on S3 resume boot path.
7
8 Copyright (c) 2011 - 2018, 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 <PiPei.h>
20
21 #include <Ppi/ReportStatusCodeHandler.h>
22 #include <Ppi/ReadOnlyVariable2.h>
23
24 #include <Guid/FirmwarePerformance.h>
25 #include <Guid/Performance.h>
26 #include <Guid/ExtendedFirmwarePerformance.h>
27
28 #include <Library/PeiServicesLib.h>
29 #include <Library/BaseLib.h>
30 #include <Library/DebugLib.h>
31 #include <Library/TimerLib.h>
32 #include <Library/BaseMemoryLib.h>
33 #include <Library/LockBoxLib.h>
34 #include <Library/PcdLib.h>
35 #include <Library/HobLib.h>
36
37 /**
38 Report status code listener for PEI. This is used to record the performance
39 data for S3 FullResume in FPDT.
40
41 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
42 @param[in] CodeType Indicates the type of status code being reported.
43 @param[in] Value Describes the current status of a hardware or software entity.
44 This included information about the class and subclass that is used to
45 classify the entity as well as an operation.
46 @param[in] Instance The enumeration of a hardware or software entity within
47 the system. Valid instance numbers start with 1.
48 @param[in] CallerId This optional parameter may be used to identify the caller.
49 This parameter allows the status code driver to apply different rules to
50 different callers.
51 @param[in] Data This optional parameter may be used to pass additional data.
52
53 @retval EFI_SUCCESS Status code is what we expected.
54 @retval EFI_UNSUPPORTED Status code not supported.
55
56 **/
57 EFI_STATUS
58 EFIAPI
59 FpdtStatusCodeListenerPei (
60 IN CONST EFI_PEI_SERVICES **PeiServices,
61 IN EFI_STATUS_CODE_TYPE CodeType,
62 IN EFI_STATUS_CODE_VALUE Value,
63 IN UINT32 Instance,
64 IN CONST EFI_GUID *CallerId,
65 IN CONST EFI_STATUS_CODE_DATA *Data
66 )
67 {
68 EFI_STATUS Status;
69 UINT64 CurrentTime;
70 UINTN VarSize;
71 EFI_PHYSICAL_ADDRESS S3PerformanceTablePointer;
72 S3_PERFORMANCE_TABLE *AcpiS3PerformanceTable;
73 EFI_ACPI_5_0_FPDT_S3_RESUME_RECORD *AcpiS3ResumeRecord;
74 UINT64 S3ResumeTotal;
75 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord;
76 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD *AcpiS3SuspendRecord;
77 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariableServices;
78 UINT8 *BootPerformanceTable;
79 FIRMWARE_PERFORMANCE_VARIABLE PerformanceVariable;
80 EFI_HOB_GUID_TYPE *GuidHob;
81 FPDT_PEI_EXT_PERF_HEADER *PeiPerformanceLogHeader;
82 UINT8 *FirmwarePerformanceData;
83 UINT8 *FirmwarePerformanceTablePtr;
84
85 //
86 // Check whether status code is what we are interested in.
87 //
88 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) ||
89 (Value != (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_OS_WAKE))) {
90 return EFI_UNSUPPORTED;
91 }
92
93 //
94 // Retrieve current time as early as possible.
95 //
96 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());
97
98 //
99 // Update S3 Resume Performance Record.
100 //
101 S3PerformanceTablePointer = 0;
102 VarSize = sizeof (EFI_PHYSICAL_ADDRESS);
103 Status = RestoreLockBox (&gFirmwarePerformanceS3PointerGuid, &S3PerformanceTablePointer, &VarSize);
104 ASSERT_EFI_ERROR (Status);
105
106 AcpiS3PerformanceTable = (S3_PERFORMANCE_TABLE *) (UINTN) S3PerformanceTablePointer;
107 ASSERT (AcpiS3PerformanceTable != NULL);
108 if (AcpiS3PerformanceTable->Header.Signature != EFI_ACPI_5_0_FPDT_S3_PERFORMANCE_TABLE_SIGNATURE) {
109 DEBUG ((EFI_D_ERROR, "FPDT S3 performance data in ACPI memory get corrupted\n"));
110 return EFI_ABORTED;
111 }
112 AcpiS3ResumeRecord = &AcpiS3PerformanceTable->S3Resume;
113 AcpiS3ResumeRecord->FullResume = CurrentTime;
114 //
115 // Calculate average S3 resume time.
116 //
117 S3ResumeTotal = MultU64x32 (AcpiS3ResumeRecord->AverageResume, AcpiS3ResumeRecord->ResumeCount);
118 AcpiS3ResumeRecord->ResumeCount++;
119 AcpiS3ResumeRecord->AverageResume = DivU64x32 (S3ResumeTotal + AcpiS3ResumeRecord->FullResume, AcpiS3ResumeRecord->ResumeCount);
120
121 DEBUG ((EFI_D_INFO, "FPDT: S3 Resume Performance - ResumeCount = %d\n", AcpiS3ResumeRecord->ResumeCount));
122 DEBUG ((EFI_D_INFO, "FPDT: S3 Resume Performance - FullResume = %ld\n", AcpiS3ResumeRecord->FullResume));
123 DEBUG ((EFI_D_INFO, "FPDT: S3 Resume Performance - AverageResume = %ld\n", AcpiS3ResumeRecord->AverageResume));
124
125 //
126 // Update S3 Suspend Performance Record.
127 //
128 AcpiS3SuspendRecord = &AcpiS3PerformanceTable->S3Suspend;
129 VarSize = sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD);
130 ZeroMem (&S3SuspendRecord, sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD));
131 Status = RestoreLockBox (
132 &gEfiFirmwarePerformanceGuid,
133 &S3SuspendRecord,
134 &VarSize
135 );
136 ASSERT_EFI_ERROR (Status);
137
138 AcpiS3SuspendRecord->SuspendStart = S3SuspendRecord.SuspendStart;
139 AcpiS3SuspendRecord->SuspendEnd = S3SuspendRecord.SuspendEnd;
140
141 DEBUG ((EFI_D_INFO, "FPDT: S3 Suspend Performance - SuspendStart = %ld\n", AcpiS3SuspendRecord->SuspendStart));
142 DEBUG ((EFI_D_INFO, "FPDT: S3 Suspend Performance - SuspendEnd = %ld\n", AcpiS3SuspendRecord->SuspendEnd));
143
144 Status = PeiServicesLocatePpi (
145 &gEfiPeiReadOnlyVariable2PpiGuid,
146 0,
147 NULL,
148 (VOID **) &VariableServices
149 );
150 ASSERT_EFI_ERROR (Status);
151
152 //
153 // Update S3 boot records into the basic boot performance table.
154 //
155 VarSize = sizeof (PerformanceVariable);
156 Status = VariableServices->GetVariable (
157 VariableServices,
158 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
159 &gEfiFirmwarePerformanceGuid,
160 NULL,
161 &VarSize,
162 &PerformanceVariable
163 );
164 if (EFI_ERROR (Status)) {
165 return Status;
166 }
167 BootPerformanceTable = (UINT8*) (UINTN) PerformanceVariable.BootPerformanceTablePointer;
168
169 //
170 // Dump PEI boot records
171 //
172 FirmwarePerformanceTablePtr = (BootPerformanceTable + sizeof (BOOT_PERFORMANCE_TABLE));
173 GuidHob = GetFirstGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid);
174 while (GuidHob != NULL) {
175 FirmwarePerformanceData = GET_GUID_HOB_DATA (GuidHob);
176 PeiPerformanceLogHeader = (FPDT_PEI_EXT_PERF_HEADER *) FirmwarePerformanceData;
177
178 CopyMem (FirmwarePerformanceTablePtr, FirmwarePerformanceData + sizeof (FPDT_PEI_EXT_PERF_HEADER), (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries));
179
180 GuidHob = GetNextGuidHob (&gEdkiiFpdtExtendedFirmwarePerformanceGuid, GET_NEXT_HOB (GuidHob));
181
182 FirmwarePerformanceTablePtr += (UINTN)(PeiPerformanceLogHeader->SizeOfAllEntries);
183 }
184
185 //
186 // Update Table length.
187 //
188 ((BOOT_PERFORMANCE_TABLE *) BootPerformanceTable)->Header.Length = (UINT32)((UINTN)FirmwarePerformanceTablePtr - (UINTN)BootPerformanceTable);
189
190 return EFI_SUCCESS;
191 }
192
193 /**
194 Main entry for Firmware Performance Data Table PEIM.
195
196 This routine is to register report status code listener for FPDT.
197
198 @param[in] FileHandle Handle of the file being invoked.
199 @param[in] PeiServices Pointer to PEI Services table.
200
201 @retval EFI_SUCCESS Report status code listener is registered successfully.
202
203 **/
204 EFI_STATUS
205 EFIAPI
206 FirmwarePerformancePeiEntryPoint (
207 IN EFI_PEI_FILE_HANDLE FileHandle,
208 IN CONST EFI_PEI_SERVICES **PeiServices
209 )
210 {
211 EFI_STATUS Status;
212 EFI_PEI_RSC_HANDLER_PPI *RscHandler;
213
214 if (FeaturePcdGet (PcdFirmwarePerformanceDataTableS3Support)) {
215 //
216 // S3 resume - register status code listener for OS wake vector.
217 //
218 Status = PeiServicesLocatePpi (
219 &gEfiPeiRscHandlerPpiGuid,
220 0,
221 NULL,
222 (VOID **) &RscHandler
223 );
224 ASSERT_EFI_ERROR (Status);
225
226 Status = RscHandler->Register (FpdtStatusCodeListenerPei);
227 ASSERT_EFI_ERROR (Status);
228 }
229
230 return EFI_SUCCESS;
231 }