]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFsp2WrapperPkg/FspsWrapperPeim/FspsWrapperPeim.c
IntelFsp2WrapperPkg : FSPM/S UPD data address based on Build Type
[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 - 2021, 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 #include <Library/FspMeasurementLib.h>
28
29 #include <Ppi/FspSiliconInitDone.h>
30 #include <Ppi/EndOfPeiPhase.h>
31 #include <Ppi/MemoryDiscovered.h>
32 #include <Ppi/TemporaryRamDone.h>
33 #include <Ppi/SecPlatformInformation.h>
34 #include <Ppi/Tcg.h>
35 #include <Ppi/FirmwareVolumeInfoMeasurementExcluded.h>
36 #include <Library/FspWrapperApiTestLib.h>
37 #include <FspEas.h>
38 #include <FspStatusCode.h>
39
40 extern EFI_PEI_NOTIFY_DESCRIPTOR mS3EndOfPeiNotifyDesc;
41 extern EFI_GUID gFspHobGuid;
42
43 /**
44 This function handles S3 resume task at the end of PEI.
45
46 @param[in] PeiServices Pointer to PEI Services Table.
47 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
48 caused this function to execute.
49 @param[in] Ppi Pointer to the PPI data associated with this function.
50
51 @retval EFI_STATUS Always return EFI_SUCCESS
52 **/
53 EFI_STATUS
54 EFIAPI
55 S3EndOfPeiNotify (
56 IN EFI_PEI_SERVICES **PeiServices,
57 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
58 IN VOID *Ppi
59 );
60
61 EFI_PEI_NOTIFY_DESCRIPTOR mS3EndOfPeiNotifyDesc = {
62 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
63 &gEfiEndOfPeiSignalPpiGuid,
64 S3EndOfPeiNotify
65 };
66
67 /**
68 This function handles S3 resume task at the end of PEI.
69
70 @param[in] PeiServices Pointer to PEI Services Table.
71 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
72 caused this function to execute.
73 @param[in] Ppi Pointer to the PPI data associated with this function.
74
75 @retval EFI_STATUS Always return EFI_SUCCESS
76 **/
77 EFI_STATUS
78 EFIAPI
79 S3EndOfPeiNotify (
80 IN EFI_PEI_SERVICES **PeiServices,
81 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
82 IN VOID *Ppi
83 )
84 {
85 NOTIFY_PHASE_PARAMS NotifyPhaseParams;
86 EFI_STATUS Status;
87
88 DEBUG ((DEBUG_INFO, "S3EndOfPeiNotify enter\n"));
89
90 NotifyPhaseParams.Phase = EnumInitPhaseAfterPciEnumeration;
91 Status = CallFspNotifyPhase (&NotifyPhaseParams);
92 DEBUG ((DEBUG_INFO, "FSP S3NotifyPhase AfterPciEnumeration status: 0x%x\n", Status));
93
94 //
95 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status
96 //
97 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) {
98 DEBUG ((DEBUG_INFO, "FSP S3NotifyPhase AfterPciEnumeration requested reset 0x%x\n", Status));
99 CallFspWrapperResetSystem ((UINT32)Status);
100 }
101
102 NotifyPhaseParams.Phase = EnumInitPhaseReadyToBoot;
103 Status = CallFspNotifyPhase (&NotifyPhaseParams);
104 DEBUG ((DEBUG_INFO, "FSP S3NotifyPhase ReadyToBoot status: 0x%x\n", Status));
105
106 //
107 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status
108 //
109 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) {
110 DEBUG ((DEBUG_INFO, "FSP S3NotifyPhase ReadyToBoot requested reset 0x%x\n", Status));
111 CallFspWrapperResetSystem ((UINT32)Status);
112 }
113
114 NotifyPhaseParams.Phase = EnumInitPhaseEndOfFirmware;
115 Status = CallFspNotifyPhase (&NotifyPhaseParams);
116 DEBUG ((DEBUG_INFO, "FSP S3NotifyPhase EndOfFirmware status: 0x%x\n", Status));
117
118 //
119 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status
120 //
121 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) {
122 DEBUG ((DEBUG_INFO, "FSP S3NotifyPhase EndOfFirmware requested reset 0x%x\n", Status));
123 CallFspWrapperResetSystem ((UINT32)Status);
124 }
125
126 return EFI_SUCCESS;
127 }
128
129 /**
130 Return Hob list produced by FSP.
131
132 @param[in] PeiServices The pointer to the PEI Services Table.
133 @param[in] This The pointer to this instance of this PPI.
134 @param[out] FspHobList The pointer to Hob list produced by FSP.
135
136 @return EFI_SUCCESS Return Hob list produced by FSP successfully.
137 **/
138 EFI_STATUS
139 EFIAPI
140 FspSiliconInitDoneGetFspHobList (
141 IN CONST EFI_PEI_SERVICES **PeiServices,
142 IN FSP_SILICON_INIT_DONE_PPI *This,
143 OUT VOID **FspHobList
144 );
145
146 FSP_SILICON_INIT_DONE_PPI mFspSiliconInitDonePpi = {
147 FspSiliconInitDoneGetFspHobList
148 };
149
150 EFI_PEI_PPI_DESCRIPTOR mPeiFspSiliconInitDonePpi = {
151 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
152 &gFspSiliconInitDonePpiGuid,
153 &mFspSiliconInitDonePpi
154 };
155
156 /**
157 Return Hob list produced by FSP.
158
159 @param[in] PeiServices The pointer to the PEI Services Table.
160 @param[in] This The pointer to this instance of this PPI.
161 @param[out] FspHobList The pointer to Hob list produced by FSP.
162
163 @return EFI_SUCCESS Return Hob list produced by FSP successfully.
164 **/
165 EFI_STATUS
166 EFIAPI
167 FspSiliconInitDoneGetFspHobList (
168 IN CONST EFI_PEI_SERVICES **PeiServices,
169 IN FSP_SILICON_INIT_DONE_PPI *This,
170 OUT VOID **FspHobList
171 )
172 {
173 EFI_HOB_GUID_TYPE *GuidHob;
174
175 GuidHob = GetFirstGuidHob (&gFspHobGuid);
176 if (GuidHob != NULL) {
177 *FspHobList = *(VOID **)GET_GUID_HOB_DATA (GuidHob);
178 return EFI_SUCCESS;
179 } else {
180 return EFI_NOT_FOUND;
181 }
182 }
183
184 /**
185 Get the FSP S UPD Data address
186
187 @return FSP-S UPD Data Address
188 **/
189
190 UINTN
191 EFIAPI
192 GetFspsUpdDataAddress (
193 VOID
194 )
195 {
196 if (PcdGet64 (PcdFspsUpdDataAddress64) != 0) {
197 return (UINTN) PcdGet64 (PcdFspsUpdDataAddress64);
198 } else {
199 return (UINTN) PcdGet32 (PcdFspsUpdDataAddress);
200 }
201 }
202
203 /**
204 This function is for FSP dispatch mode to perform post FSP-S process.
205
206 @param[in] PeiServices Pointer to PEI Services Table.
207 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
208 caused this function to execute.
209 @param[in] Ppi Pointer to the PPI data associated with this function.
210
211 @retval EFI_STATUS Status returned by PeiServicesInstallPpi ()
212 **/
213 EFI_STATUS
214 EFIAPI
215 FspsWrapperEndOfPeiNotify (
216 IN EFI_PEI_SERVICES **PeiServices,
217 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
218 IN VOID *Ppi
219 )
220 {
221 EFI_STATUS Status;
222
223 //
224 // This step may include platform specific process in some boot loaders so
225 // aligning the same behavior between API and Dispatch modes.
226 // Note: In Dispatch mode no FspHobList so passing NULL to function and
227 // expecting function will handle it.
228 //
229 PostFspsHobProcess (NULL);
230
231 //
232 // Install FspSiliconInitDonePpi so that any other driver can consume this info.
233 //
234 Status = PeiServicesInstallPpi (&mPeiFspSiliconInitDonePpi);
235 ASSERT_EFI_ERROR (Status);
236
237 return Status;
238 }
239
240 EFI_PEI_NOTIFY_DESCRIPTOR mFspsWrapperEndOfPeiNotifyDesc = {
241 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
242 &gEfiEndOfPeiSignalPpiGuid,
243 FspsWrapperEndOfPeiNotify
244 };
245
246 /**
247 This function is called after PEI core discover memory and finish migration.
248
249 @param[in] PeiServices Pointer to PEI Services Table.
250 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
251 caused this function to execute.
252 @param[in] Ppi Pointer to the PPI data associated with this function.
253
254 @retval EFI_STATUS Always return EFI_SUCCESS
255 **/
256 EFI_STATUS
257 EFIAPI
258 PeiMemoryDiscoveredNotify (
259 IN EFI_PEI_SERVICES **PeiServices,
260 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
261 IN VOID *Ppi
262 );
263
264 EFI_PEI_NOTIFY_DESCRIPTOR mPeiMemoryDiscoveredNotifyDesc = {
265 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
266 &gEfiPeiMemoryDiscoveredPpiGuid,
267 PeiMemoryDiscoveredNotify
268 };
269
270 /**
271 This function is called after PEI core discover memory and finish migration.
272
273 @param[in] PeiServices Pointer to PEI Services Table.
274 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
275 caused this function to execute.
276 @param[in] Ppi Pointer to the PPI data associated with this function.
277
278 @retval EFI_STATUS Always return EFI_SUCCESS
279 **/
280 EFI_STATUS
281 EFIAPI
282 PeiMemoryDiscoveredNotify (
283 IN EFI_PEI_SERVICES **PeiServices,
284 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
285 IN VOID *Ppi
286 )
287 {
288 FSP_INFO_HEADER *FspsHeaderPtr;
289 UINT64 TimeStampCounterStart;
290 EFI_STATUS Status;
291 VOID *FspHobListPtr;
292 EFI_HOB_GUID_TYPE *GuidHob;
293 FSPS_UPD_COMMON *FspsUpdDataPtr;
294 UINTN *SourceData;
295
296 DEBUG ((DEBUG_INFO, "PeiMemoryDiscoveredNotify enter\n"));
297 FspsUpdDataPtr = NULL;
298
299 FspsHeaderPtr = (FSP_INFO_HEADER *)FspFindFspHeader (PcdGet32 (PcdFspsBaseAddress));
300 DEBUG ((DEBUG_INFO, "FspsHeaderPtr - 0x%x\n", FspsHeaderPtr));
301 if (FspsHeaderPtr == NULL) {
302 return EFI_DEVICE_ERROR;
303 }
304
305 if ((GetFspsUpdDataAddress () == 0) && (FspsHeaderPtr->CfgRegionSize != 0) && (FspsHeaderPtr->CfgRegionOffset != 0)) {
306 //
307 // Copy default FSP-S UPD data from Flash
308 //
309 FspsUpdDataPtr = (FSPS_UPD_COMMON *)AllocateZeroPool ((UINTN)FspsHeaderPtr->CfgRegionSize);
310 ASSERT (FspsUpdDataPtr != NULL);
311 SourceData = (UINTN *)((UINTN)FspsHeaderPtr->ImageBase + (UINTN)FspsHeaderPtr->CfgRegionOffset);
312 CopyMem (FspsUpdDataPtr, SourceData, (UINTN)FspsHeaderPtr->CfgRegionSize);
313 } else {
314 FspsUpdDataPtr = (FSPS_UPD_COMMON *) GetFspsUpdDataAddress();
315 ASSERT (FspsUpdDataPtr != NULL);
316 }
317
318 UpdateFspsUpdData ((VOID *)FspsUpdDataPtr);
319
320 TimeStampCounterStart = AsmReadTsc ();
321 PERF_START_EX (&gFspApiPerformanceGuid, "EventRec", NULL, 0, FSP_STATUS_CODE_SILICON_INIT | FSP_STATUS_CODE_COMMON_CODE | FSP_STATUS_CODE_API_ENTRY);
322 Status = CallFspSiliconInit ((VOID *)FspsUpdDataPtr);
323 PERF_END_EX (&gFspApiPerformanceGuid, "EventRec", NULL, 0, FSP_STATUS_CODE_SILICON_INIT | FSP_STATUS_CODE_COMMON_CODE | FSP_STATUS_CODE_API_EXIT);
324 DEBUG ((DEBUG_INFO, "Total time spent executing FspSiliconInitApi: %d millisecond\n", DivU64x32 (GetTimeInNanoSecond (AsmReadTsc () - TimeStampCounterStart), 1000000)));
325
326 //
327 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status
328 //
329 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) {
330 DEBUG ((DEBUG_INFO, "FspSiliconInitApi requested reset 0x%x\n", Status));
331 CallFspWrapperResetSystem ((UINT32)Status);
332 }
333
334 if (EFI_ERROR (Status)) {
335 DEBUG ((DEBUG_ERROR, "ERROR - Failed to execute FspSiliconInitApi(), Status = %r\n", Status));
336 }
337
338 DEBUG ((DEBUG_INFO, "FspSiliconInit status: 0x%x\n", Status));
339 ASSERT_EFI_ERROR (Status);
340
341 Status = TestFspSiliconInitApiOutput ((VOID *)NULL);
342 if (RETURN_ERROR (Status)) {
343 DEBUG ((DEBUG_ERROR, "ERROR - TestFspSiliconInitApiOutput () fail, Status = %r\n", Status));
344 }
345
346 //
347 // Now FspHobList complete, process it
348 //
349 GuidHob = GetFirstGuidHob (&gFspHobGuid);
350 ASSERT (GuidHob != NULL);
351 FspHobListPtr = *(VOID **)GET_GUID_HOB_DATA (GuidHob);
352 DEBUG ((DEBUG_INFO, "FspHobListPtr - 0x%x\n", FspHobListPtr));
353 PostFspsHobProcess (FspHobListPtr);
354
355 //
356 // Install FspSiliconInitDonePpi so that any other driver can consume this info.
357 //
358 Status = PeiServicesInstallPpi (&mPeiFspSiliconInitDonePpi);
359 ASSERT_EFI_ERROR (Status);
360
361 return Status;
362 }
363
364 /**
365 Do FSP initialization in API mode.
366
367 @retval EFI_STATUS Always return EFI_SUCCESS
368 **/
369 EFI_STATUS
370 FspsWrapperInitApiMode (
371 VOID
372 )
373 {
374 EFI_STATUS Status;
375 EFI_BOOT_MODE BootMode;
376
377 //
378 // Register MemoryDiscovered Notify to run FspSiliconInit
379 //
380 Status = PeiServicesNotifyPpi (&mPeiMemoryDiscoveredNotifyDesc);
381 ASSERT_EFI_ERROR (Status);
382
383 //
384 // Register EndOfPei Notify for S3 to run FSP NotifyPhase
385 //
386 PeiServicesGetBootMode (&BootMode);
387 if (BootMode == BOOT_ON_S3_RESUME) {
388 Status = PeiServicesNotifyPpi (&mS3EndOfPeiNotifyDesc);
389 ASSERT_EFI_ERROR (Status);
390 }
391
392 return EFI_SUCCESS;
393 }
394
395 /**
396 Do FSP initialization in Dispatch mode.
397
398 @retval FSP initialization status.
399 **/
400 EFI_STATUS
401 FspsWrapperInitDispatchMode (
402 VOID
403 )
404 {
405 EFI_STATUS Status;
406 EFI_PEI_FIRMWARE_VOLUME_INFO_MEASUREMENT_EXCLUDED_PPI *MeasurementExcludedFvPpi;
407 EFI_PEI_PPI_DESCRIPTOR *MeasurementExcludedPpiList;
408
409 MeasurementExcludedFvPpi = AllocatePool (sizeof (*MeasurementExcludedFvPpi));
410 ASSERT (MeasurementExcludedFvPpi != NULL);
411 MeasurementExcludedFvPpi->Count = 1;
412 MeasurementExcludedFvPpi->Fv[0].FvBase = PcdGet32 (PcdFspsBaseAddress);
413 MeasurementExcludedFvPpi->Fv[0].FvLength = ((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdFspsBaseAddress))->FvLength;
414
415 MeasurementExcludedPpiList = AllocatePool (sizeof (*MeasurementExcludedPpiList));
416 ASSERT (MeasurementExcludedPpiList != NULL);
417 MeasurementExcludedPpiList->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
418 MeasurementExcludedPpiList->Guid = &gEfiPeiFirmwareVolumeInfoMeasurementExcludedPpiGuid;
419 MeasurementExcludedPpiList->Ppi = MeasurementExcludedFvPpi;
420
421 Status = PeiServicesInstallPpi (MeasurementExcludedPpiList);
422 ASSERT_EFI_ERROR (Status);
423
424 //
425 // FSP-S Wrapper running in Dispatch mode and reports FSP-S FV to PEI dispatcher.
426 //
427 PeiServicesInstallFvInfoPpi (
428 NULL,
429 (VOID *)(UINTN)PcdGet32 (PcdFspsBaseAddress),
430 (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdFspsBaseAddress))->FvLength,
431 NULL,
432 NULL
433 );
434 //
435 // Register EndOfPei Nofity to run post FSP-S process.
436 //
437 Status = PeiServicesNotifyPpi (&mFspsWrapperEndOfPeiNotifyDesc);
438 ASSERT_EFI_ERROR (Status);
439 return Status;
440 }
441
442 /**
443 This function is called after TCG installed PPI.
444
445 @param[in] PeiServices Pointer to PEI Services Table.
446 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
447 caused this function to execute.
448 @param[in] Ppi Pointer to the PPI data associated with this function.
449
450 @retval EFI_STATUS Always return EFI_SUCCESS
451 **/
452 EFI_STATUS
453 EFIAPI
454 TcgPpiNotify (
455 IN EFI_PEI_SERVICES **PeiServices,
456 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
457 IN VOID *Ppi
458 );
459
460 EFI_PEI_NOTIFY_DESCRIPTOR mTcgPpiNotifyDesc = {
461 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
462 &gEdkiiTcgPpiGuid,
463 TcgPpiNotify
464 };
465
466 /**
467 This function is called after TCG installed PPI.
468
469 @param[in] PeiServices Pointer to PEI Services Table.
470 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
471 caused this function to execute.
472 @param[in] Ppi Pointer to the PPI data associated with this function.
473
474 @retval EFI_STATUS Always return EFI_SUCCESS
475 **/
476 EFI_STATUS
477 EFIAPI
478 TcgPpiNotify (
479 IN EFI_PEI_SERVICES **PeiServices,
480 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
481 IN VOID *Ppi
482 )
483 {
484 UINT32 FspMeasureMask;
485
486 DEBUG ((DEBUG_INFO, "TcgPpiNotify FSPS\n"));
487
488 FspMeasureMask = PcdGet32 (PcdFspMeasurementConfig);
489
490 if ((FspMeasureMask & FSP_MEASURE_FSPS) != 0) {
491 MeasureFspFirmwareBlob (
492 0,
493 "FSPS",
494 PcdGet32 (PcdFspsBaseAddress),
495 (UINT32)((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdFspsBaseAddress))->FvLength
496 );
497 }
498
499 return EFI_SUCCESS;
500 }
501
502 /**
503 This is the entrypoint of PEIM.
504
505 @param[in] FileHandle Handle of the file being invoked.
506 @param[in] PeiServices Describes the list of possible PEI Services.
507
508 @retval EFI_SUCCESS if it completed successfully.
509 **/
510 EFI_STATUS
511 EFIAPI
512 FspsWrapperPeimEntryPoint (
513 IN EFI_PEI_FILE_HANDLE FileHandle,
514 IN CONST EFI_PEI_SERVICES **PeiServices
515 )
516 {
517 EFI_STATUS Status;
518
519 DEBUG ((DEBUG_INFO, "FspsWrapperPeimEntryPoint\n"));
520
521 Status = PeiServicesNotifyPpi (&mTcgPpiNotifyDesc);
522 ASSERT_EFI_ERROR (Status);
523
524 if (PcdGet8 (PcdFspModeSelection) == 1) {
525 FspsWrapperInitApiMode ();
526 } else {
527 FspsWrapperInitDispatchMode ();
528 }
529
530 return EFI_SUCCESS;
531 }