]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFsp2WrapperPkg/FspsWrapperPeim/FspsWrapperPeim.c
bb126797ae3aafffd480638d39fd2cbf64d33668
[mirror_edk2.git] / IntelFsp2WrapperPkg / FspsWrapperPeim / FspsWrapperPeim.c
1 /** @file
2 This will be invoked only once. It will call FspMemoryInit API,
3 register TemporaryRamDonePpi to call TempRamExit API, and register MemoryDiscoveredPpi
4 notify to call FspSiliconInit API.
5
6 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <PiPei.h>
12
13 #include <Library/PeimEntryPoint.h>
14 #include <Library/PeiServicesLib.h>
15 #include <Library/PeiServicesTablePointerLib.h>
16 #include <Library/BaseLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/HobLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/FspWrapperPlatformLib.h>
23 #include <Library/FspWrapperHobProcessLib.h>
24 #include <Library/TimerLib.h>
25 #include <Library/PerformanceLib.h>
26 #include <Library/FspWrapperApiLib.h>
27
28 #include <Ppi/FspSiliconInitDone.h>
29 #include <Ppi/EndOfPeiPhase.h>
30 #include <Ppi/MemoryDiscovered.h>
31 #include <Ppi/TemporaryRamDone.h>
32 #include <Ppi/SecPlatformInformation.h>
33 #include <Library/FspWrapperApiTestLib.h>
34 #include <FspEas.h>
35 #include <FspStatusCode.h>
36
37 extern EFI_PEI_NOTIFY_DESCRIPTOR mS3EndOfPeiNotifyDesc;
38 extern EFI_GUID gFspHobGuid;
39
40 /**
41 This function handles S3 resume task at the end of PEI
42
43 @param[in] PeiServices Pointer to PEI Services Table.
44 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
45 caused this function to execute.
46 @param[in] Ppi Pointer to the PPI data associated with this function.
47
48 @retval EFI_STATUS Always return EFI_SUCCESS
49 **/
50 EFI_STATUS
51 EFIAPI
52 S3EndOfPeiNotify(
53 IN EFI_PEI_SERVICES **PeiServices,
54 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
55 IN VOID *Ppi
56 );
57
58 EFI_PEI_NOTIFY_DESCRIPTOR mS3EndOfPeiNotifyDesc = {
59 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
60 &gEfiEndOfPeiSignalPpiGuid,
61 S3EndOfPeiNotify
62 };
63
64 /**
65 This function handles S3 resume task at the end of PEI
66
67 @param[in] PeiServices Pointer to PEI Services Table.
68 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
69 caused this function to execute.
70 @param[in] Ppi Pointer to the PPI data associated with this function.
71
72 @retval EFI_STATUS Always return EFI_SUCCESS
73 **/
74 EFI_STATUS
75 EFIAPI
76 S3EndOfPeiNotify(
77 IN EFI_PEI_SERVICES **PeiServices,
78 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
79 IN VOID *Ppi
80 )
81 {
82 NOTIFY_PHASE_PARAMS NotifyPhaseParams;
83 EFI_STATUS Status;
84
85 DEBUG((DEBUG_INFO, "S3EndOfPeiNotify enter\n"));
86
87 NotifyPhaseParams.Phase = EnumInitPhaseAfterPciEnumeration;
88 Status = CallFspNotifyPhase (&NotifyPhaseParams);
89 DEBUG((DEBUG_INFO, "FSP S3NotifyPhase AfterPciEnumeration status: 0x%x\n", Status));
90
91 //
92 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status
93 //
94 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) {
95 DEBUG((DEBUG_INFO, "FSP S3NotifyPhase AfterPciEnumeration requested reset 0x%x\n", Status));
96 CallFspWrapperResetSystem ((UINT32)Status);
97 }
98
99 NotifyPhaseParams.Phase = EnumInitPhaseReadyToBoot;
100 Status = CallFspNotifyPhase (&NotifyPhaseParams);
101 DEBUG((DEBUG_INFO, "FSP S3NotifyPhase ReadyToBoot status: 0x%x\n", Status));
102
103 //
104 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status
105 //
106 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) {
107 DEBUG((DEBUG_INFO, "FSP S3NotifyPhase ReadyToBoot requested reset 0x%x\n", Status));
108 CallFspWrapperResetSystem ((UINT32)Status);
109 }
110
111 NotifyPhaseParams.Phase = EnumInitPhaseEndOfFirmware;
112 Status = CallFspNotifyPhase (&NotifyPhaseParams);
113 DEBUG((DEBUG_INFO, "FSP S3NotifyPhase EndOfFirmware status: 0x%x\n", Status));
114
115 //
116 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status
117 //
118 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) {
119 DEBUG((DEBUG_INFO, "FSP S3NotifyPhase EndOfFirmware requested reset 0x%x\n", Status));
120 CallFspWrapperResetSystem ((UINT32)Status);
121 }
122
123 return EFI_SUCCESS;
124 }
125
126 /**
127 Return Hob list produced by FSP.
128
129 @param[in] PeiServices The pointer to the PEI Services Table.
130 @param[in] This The pointer to this instance of this PPI.
131 @param[out] FspHobList The pointer to Hob list produced by FSP.
132
133 @return EFI_SUCCESS FReturn Hob list produced by FSP successfully.
134 **/
135 EFI_STATUS
136 EFIAPI
137 FspSiliconInitDoneGetFspHobList (
138 IN CONST EFI_PEI_SERVICES **PeiServices,
139 IN FSP_SILICON_INIT_DONE_PPI *This,
140 OUT VOID **FspHobList
141 );
142
143 FSP_SILICON_INIT_DONE_PPI mFspSiliconInitDonePpi = {
144 FspSiliconInitDoneGetFspHobList
145 };
146
147 EFI_PEI_PPI_DESCRIPTOR mPeiFspSiliconInitDonePpi = {
148 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
149 &gFspSiliconInitDonePpiGuid,
150 &mFspSiliconInitDonePpi
151 };
152
153 /**
154 Return Hob list produced by FSP.
155
156 @param[in] PeiServices The pointer to the PEI Services Table.
157 @param[in] This The pointer to this instance of this PPI.
158 @param[out] FspHobList The pointer to Hob list produced by FSP.
159
160 @return EFI_SUCCESS FReturn Hob list produced by FSP successfully.
161 **/
162 EFI_STATUS
163 EFIAPI
164 FspSiliconInitDoneGetFspHobList (
165 IN CONST EFI_PEI_SERVICES **PeiServices,
166 IN FSP_SILICON_INIT_DONE_PPI *This,
167 OUT VOID **FspHobList
168 )
169 {
170 EFI_HOB_GUID_TYPE *GuidHob;
171
172 GuidHob = GetFirstGuidHob (&gFspHobGuid);
173 if (GuidHob != NULL) {
174 *FspHobList = *(VOID **)GET_GUID_HOB_DATA(GuidHob);
175 return EFI_SUCCESS;
176 } else {
177 return EFI_NOT_FOUND;
178 }
179 }
180
181 /**
182 This function is called after PEI core discover memory and finish migration.
183
184 @param[in] PeiServices Pointer to PEI Services Table.
185 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
186 caused this function to execute.
187 @param[in] Ppi Pointer to the PPI data associated with this function.
188
189 @retval EFI_STATUS Always return EFI_SUCCESS
190 **/
191 EFI_STATUS
192 EFIAPI
193 PeiMemoryDiscoveredNotify (
194 IN EFI_PEI_SERVICES **PeiServices,
195 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
196 IN VOID *Ppi
197 );
198
199 EFI_PEI_NOTIFY_DESCRIPTOR mPeiMemoryDiscoveredNotifyDesc = {
200 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
201 &gEfiPeiMemoryDiscoveredPpiGuid,
202 PeiMemoryDiscoveredNotify
203 };
204
205 /**
206 This function is called after PEI core discover memory and finish migration.
207
208 @param[in] PeiServices Pointer to PEI Services Table.
209 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
210 caused this function to execute.
211 @param[in] Ppi Pointer to the PPI data associated with this function.
212
213 @retval EFI_STATUS Always return EFI_SUCCESS
214 **/
215 EFI_STATUS
216 EFIAPI
217 PeiMemoryDiscoveredNotify (
218 IN EFI_PEI_SERVICES **PeiServices,
219 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
220 IN VOID *Ppi
221 )
222 {
223 FSP_INFO_HEADER *FspsHeaderPtr;
224 UINT64 TimeStampCounterStart;
225 EFI_STATUS Status;
226 VOID *FspHobListPtr;
227 EFI_HOB_GUID_TYPE *GuidHob;
228 FSPS_UPD_COMMON *FspsUpdDataPtr;
229 UINTN *SourceData;
230
231 DEBUG ((DEBUG_INFO, "PeiMemoryDiscoveredNotify enter\n"));
232 FspsUpdDataPtr = NULL;
233
234 FspsHeaderPtr = (FSP_INFO_HEADER *)FspFindFspHeader (PcdGet32 (PcdFspsBaseAddress));
235 DEBUG ((DEBUG_INFO, "FspsHeaderPtr - 0x%x\n", FspsHeaderPtr));
236 if (FspsHeaderPtr == NULL) {
237 return EFI_DEVICE_ERROR;
238 }
239
240 if (PcdGet32 (PcdFspsUpdDataAddress) == 0 && (FspsHeaderPtr->CfgRegionSize != 0) && (FspsHeaderPtr->CfgRegionOffset != 0)) {
241 //
242 // Copy default FSP-S UPD data from Flash
243 //
244 FspsUpdDataPtr = (FSPS_UPD_COMMON *)AllocateZeroPool ((UINTN)FspsHeaderPtr->CfgRegionSize);
245 ASSERT (FspsUpdDataPtr != NULL);
246 SourceData = (UINTN *)((UINTN)FspsHeaderPtr->ImageBase + (UINTN)FspsHeaderPtr->CfgRegionOffset);
247 CopyMem (FspsUpdDataPtr, SourceData, (UINTN)FspsHeaderPtr->CfgRegionSize);
248 } else {
249 FspsUpdDataPtr = (FSPS_UPD_COMMON *)PcdGet32 (PcdFspsUpdDataAddress);
250 ASSERT (FspsUpdDataPtr != NULL);
251 }
252
253 UpdateFspsUpdData ((VOID *)FspsUpdDataPtr);
254
255 TimeStampCounterStart = AsmReadTsc ();
256 PERF_START_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, FSP_STATUS_CODE_SILICON_INIT | FSP_STATUS_CODE_COMMON_CODE | FSP_STATUS_CODE_API_ENTRY);
257 Status = CallFspSiliconInit ((VOID *)FspsUpdDataPtr);
258 PERF_END_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, FSP_STATUS_CODE_SILICON_INIT | FSP_STATUS_CODE_COMMON_CODE | FSP_STATUS_CODE_API_EXIT);
259 DEBUG ((DEBUG_INFO, "Total time spent executing FspSiliconInitApi: %d millisecond\n", DivU64x32 (GetTimeInNanoSecond (AsmReadTsc () - TimeStampCounterStart), 1000000)));
260
261 //
262 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status
263 //
264 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) {
265 DEBUG((DEBUG_INFO, "FspSiliconInitApi requested reset 0x%x\n", Status));
266 CallFspWrapperResetSystem ((UINT32)Status);
267 }
268
269 if (EFI_ERROR(Status)) {
270 DEBUG ((DEBUG_ERROR, "ERROR - Failed to execute FspSiliconInitApi(), Status = %r\n", Status));
271 }
272 DEBUG((DEBUG_INFO, "FspSiliconInit status: 0x%x\n", Status));
273 ASSERT_EFI_ERROR (Status);
274
275 Status = TestFspSiliconInitApiOutput ((VOID *)NULL);
276 if (RETURN_ERROR (Status)) {
277 DEBUG ((DEBUG_ERROR, "ERROR - TestFspSiliconInitApiOutput () fail, Status = %r\n", Status));
278 }
279
280 //
281 // Now FspHobList complete, process it
282 //
283 GuidHob = GetFirstGuidHob (&gFspHobGuid);
284 ASSERT (GuidHob != NULL);
285 FspHobListPtr = *(VOID **)GET_GUID_HOB_DATA (GuidHob);
286 DEBUG ((DEBUG_INFO, "FspHobListPtr - 0x%x\n", FspHobListPtr));
287 PostFspsHobProcess (FspHobListPtr);
288
289 //
290 // Install FspSiliconInitDonePpi so that any other driver can consume this info.
291 //
292 Status = PeiServicesInstallPpi (&mPeiFspSiliconInitDonePpi);
293 ASSERT_EFI_ERROR(Status);
294
295 return Status;
296 }
297
298 /**
299 Do FSP initialization.
300
301 @return FSP initialization status.
302 **/
303 EFI_STATUS
304 FspsWrapperInit (
305 VOID
306 )
307 {
308 EFI_STATUS Status;
309 EFI_BOOT_MODE BootMode;
310
311 //
312 // Register MemoryDiscovered Nofity to run FspSiliconInit
313 //
314 Status = PeiServicesNotifyPpi (&mPeiMemoryDiscoveredNotifyDesc);
315 ASSERT_EFI_ERROR (Status);
316
317 //
318 // Register EndOfPei Notify for S3 to run FSP NotifyPhase
319 //
320 PeiServicesGetBootMode (&BootMode);
321 if (BootMode == BOOT_ON_S3_RESUME) {
322 Status = PeiServicesNotifyPpi (&mS3EndOfPeiNotifyDesc);
323 ASSERT_EFI_ERROR (Status);
324 }
325
326 return EFI_SUCCESS;
327 }
328
329 /**
330 This is the entrypoint of PEIM
331
332 @param[in] FileHandle Handle of the file being invoked.
333 @param[in] PeiServices Describes the list of possible PEI Services.
334
335 @retval EFI_SUCCESS if it completed successfully.
336 **/
337 EFI_STATUS
338 EFIAPI
339 FspsWrapperPeimEntryPoint (
340 IN EFI_PEI_FILE_HANDLE FileHandle,
341 IN CONST EFI_PEI_SERVICES **PeiServices
342 )
343 {
344 DEBUG ((DEBUG_INFO, "FspsWrapperPeimEntryPoint\n"));
345
346 if (PcdGet8 (PcdFspModeSelection) == 1) {
347 FspsWrapperInit ();
348 } else {
349 PeiServicesInstallFvInfoPpi (
350 NULL,
351 (VOID *)(UINTN) PcdGet32 (PcdFspsBaseAddress),
352 (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) PcdGet32 (PcdFspsBaseAddress))->FvLength,
353 NULL,
354 NULL
355 );
356 }
357
358 return EFI_SUCCESS;
359 }