]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTablePei/FirmwarePerformancePei.c
Add Acpi50 FPDT and BGRT module into MdeModulePkg.
[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. In normal boot mode, this module consumes
4 SecPerformance PPI produced by SEC phase and build Hob to convey the SEC
5 performance data to DXE phase.
6
7 This module register report status code listener to collect performance data
8 for S3 Resume Performance Record on S3 resume boot path.
9
10 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
11 This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
15
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18
19 **/
20
21 #include <PiPei.h>
22
23 #include <IndustryStandard/Acpi50.h>
24
25 #include <Ppi/ReadOnlyVariable2.h>
26 #include <Ppi/ReportStatusCodeHandler.h>
27 #include <Ppi/SecPerformance.h>
28
29 #include <Guid/FirmwarePerformance.h>
30
31 #include <Library/PeiServicesLib.h>
32 #include <Library/BaseLib.h>
33 #include <Library/DebugLib.h>
34 #include <Library/TimerLib.h>
35 #include <Library/BaseMemoryLib.h>
36 #include <Library/LockBoxLib.h>
37 #include <Library/HobLib.h>
38 #include <Library/PcdLib.h>
39
40 /**
41 Report status code listener for PEI. This is used to record the performance
42 data for S3 FullResume in FPDT.
43
44 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
45 @param[in] CodeType Indicates the type of status code being reported.
46 @param[in] Value Describes the current status of a hardware or software entity.
47 This included information about the class and subclass that is used to
48 classify the entity as well as an operation.
49 @param[in] Instance The enumeration of a hardware or software entity within
50 the system. Valid instance numbers start with 1.
51 @param[in] CallerId This optional parameter may be used to identify the caller.
52 This parameter allows the status code driver to apply different rules to
53 different callers.
54 @param[in] Data This optional parameter may be used to pass additional data.
55
56 @retval EFI_SUCCESS Status code is what we expected.
57 @retval EFI_UNSUPPORTED Status code not supported.
58
59 **/
60 EFI_STATUS
61 EFIAPI
62 FpdtStatusCodeListenerPei (
63 IN CONST EFI_PEI_SERVICES **PeiServices,
64 IN EFI_STATUS_CODE_TYPE CodeType,
65 IN EFI_STATUS_CODE_VALUE Value,
66 IN UINT32 Instance,
67 IN CONST EFI_GUID *CallerId,
68 IN CONST EFI_STATUS_CODE_DATA *Data
69 )
70 {
71 EFI_STATUS Status;
72 UINT64 CurrentTime;
73 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariableServices;
74 UINTN VarSize;
75 FIRMWARE_PERFORMANCE_VARIABLE PerformanceVariable;
76 S3_PERFORMANCE_TABLE *AcpiS3PerformanceTable;
77 EFI_ACPI_5_0_FPDT_S3_RESUME_RECORD *AcpiS3ResumeRecord;
78 UINT64 S3ResumeTotal;
79 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord;
80 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD *AcpiS3SuspendRecord;
81
82 //
83 // Check whether status code is what we are interested in.
84 //
85 if (((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) ||
86 (Value != (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_OS_WAKE))) {
87 return EFI_UNSUPPORTED;
88 }
89
90 //
91 // Retrieve current time as early as possible.
92 //
93 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());
94
95 Status = PeiServicesLocatePpi (
96 &gEfiPeiReadOnlyVariable2PpiGuid,
97 0,
98 NULL,
99 (VOID **) &VariableServices
100 );
101 ASSERT_EFI_ERROR (Status);
102
103 //
104 // Update S3 Resume Performance Record.
105 //
106 VarSize = sizeof (FIRMWARE_PERFORMANCE_VARIABLE);
107 Status = VariableServices->GetVariable (
108 VariableServices,
109 EFI_FIRMWARE_PERFORMANCE_VARIABLE_NAME,
110 &gEfiFirmwarePerformanceGuid,
111 NULL,
112 &VarSize,
113 &PerformanceVariable
114 );
115 if (EFI_ERROR (Status)) {
116 return Status;
117 }
118
119 AcpiS3PerformanceTable = (S3_PERFORMANCE_TABLE *) (UINTN) PerformanceVariable.S3PerformanceTablePointer;
120 ASSERT (AcpiS3PerformanceTable != NULL);
121 ASSERT (AcpiS3PerformanceTable->Header.Signature == EFI_ACPI_5_0_FPDT_S3_PERFORMANCE_TABLE_SIGNATURE);
122 AcpiS3ResumeRecord = &AcpiS3PerformanceTable->S3Resume;
123 AcpiS3ResumeRecord->FullResume = CurrentTime;
124 //
125 // Calculate average S3 resume time.
126 //
127 S3ResumeTotal = MultU64x32 (AcpiS3ResumeRecord->AverageResume, AcpiS3ResumeRecord->ResumeCount);
128 AcpiS3ResumeRecord->ResumeCount++;
129 AcpiS3ResumeRecord->AverageResume = DivU64x32 (S3ResumeTotal + AcpiS3ResumeRecord->FullResume, AcpiS3ResumeRecord->ResumeCount);
130
131 DEBUG ((EFI_D_INFO, "FPDT: S3 Resume Performance - ResumeCount = %d\n", AcpiS3ResumeRecord->ResumeCount));
132 DEBUG ((EFI_D_INFO, "FPDT: S3 Resume Performance - FullResume = %ld\n", AcpiS3ResumeRecord->FullResume));
133 DEBUG ((EFI_D_INFO, "FPDT: S3 Resume Performance - AverageResume = %ld\n", AcpiS3ResumeRecord->AverageResume));
134
135 //
136 // Update S3 Suspend Performance Record.
137 //
138 AcpiS3SuspendRecord = &AcpiS3PerformanceTable->S3Suspend;
139 VarSize = sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD);
140 ZeroMem (&S3SuspendRecord, sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD));
141 Status = RestoreLockBox (
142 &gEfiFirmwarePerformanceGuid,
143 &S3SuspendRecord,
144 &VarSize
145 );
146 ASSERT_EFI_ERROR (Status);
147
148 AcpiS3SuspendRecord->SuspendStart = S3SuspendRecord.SuspendStart;
149 AcpiS3SuspendRecord->SuspendEnd = S3SuspendRecord.SuspendEnd;
150
151 DEBUG ((EFI_D_INFO, "FPDT: S3 Suspend Performance - SuspendStart = %ld\n", AcpiS3SuspendRecord->SuspendStart));
152 DEBUG ((EFI_D_INFO, "FPDT: S3 Suspend Performance - SuspendEnd = %ld\n", AcpiS3SuspendRecord->SuspendEnd));
153
154 return EFI_SUCCESS;
155 }
156
157 /**
158 Main entry for Firmware Performance Data Table PEIM.
159
160 This routine is to register report status code listener for FPDT.
161
162 @param[in] FileHandle Handle of the file being invoked.
163 @param[in] PeiServices Pointer to PEI Services table.
164
165 @retval EFI_SUCCESS Report status code listener is registered successfully.
166
167 **/
168 EFI_STATUS
169 EFIAPI
170 FirmwarePerformancePeiEntryPoint (
171 IN EFI_PEI_FILE_HANDLE FileHandle,
172 IN CONST EFI_PEI_SERVICES **PeiServices
173 )
174 {
175 EFI_STATUS Status;
176 EFI_BOOT_MODE BootMode;
177 EFI_PEI_RSC_HANDLER_PPI *RscHandler;
178 PEI_SEC_PERFORMANCE_PPI *SecPerf;
179 FIRMWARE_SEC_PERFORMANCE Performance;
180
181 Status = PeiServicesGetBootMode(&BootMode);
182 ASSERT_EFI_ERROR (Status);
183
184 if (BootMode == BOOT_ON_S3_RESUME) {
185 if (FeaturePcdGet (PcdFirmwarePerformanceDataTableS3Support)) {
186 //
187 // S3 resume - register status code listener for OS wake vector.
188 //
189 Status = PeiServicesLocatePpi (
190 &gEfiPeiRscHandlerPpiGuid,
191 0,
192 NULL,
193 (VOID **) &RscHandler
194 );
195 ASSERT_EFI_ERROR (Status);
196
197 Status = RscHandler->Register (FpdtStatusCodeListenerPei);
198 ASSERT_EFI_ERROR (Status);
199 }
200 } else {
201 //
202 // Normal boot - build Hob for SEC performance data.
203 //
204 Status = PeiServicesLocatePpi (
205 &gPeiSecPerformancePpiGuid,
206 0,
207 NULL,
208 (VOID **) &SecPerf
209 );
210 if (!EFI_ERROR (Status)) {
211 Status = SecPerf->GetPerformance (PeiServices, SecPerf, &Performance);
212 }
213 if (!EFI_ERROR (Status)) {
214 BuildGuidDataHob (
215 &gEfiFirmwarePerformanceGuid,
216 &Performance,
217 sizeof (FIRMWARE_SEC_PERFORMANCE)
218 );
219 DEBUG ((EFI_D_INFO, "FPDT: SEC Performance Hob ResetEnd = %ld\n", Performance.ResetEnd));
220 } else {
221 //
222 // SEC performance PPI is not installed or fail to get performance data
223 // from SEC Performance PPI.
224 //
225 DEBUG ((EFI_D_ERROR, "FPDT: WARNING: SEC Performance PPI not installed or failed!\n"));
226 }
227 }
228
229 return EFI_SUCCESS;
230 }