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