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 - 2016, 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 238 DEBUG ((DEBUG_INFO, "PeiMemoryDiscoveredNotify enter\n")); 239 240 // 241 // Copy default FSP-S UPD data from Flash 242 // 243 FspsHeaderPtr = (FSP_INFO_HEADER *)FspFindFspHeader (PcdGet32 (PcdFspsBaseAddress)); 244 DEBUG ((DEBUG_INFO, "FspsHeaderPtr - 0x%x\n", FspsHeaderPtr)); 245 if (FspsHeaderPtr == NULL) { 246 return EFI_DEVICE_ERROR; 247 } 248 249 FspsUpdDataPtr = (FSPS_UPD_COMMON *)AllocateZeroPool ((UINTN)FspsHeaderPtr->CfgRegionSize); 250 ASSERT (FspsUpdDataPtr != NULL); 251 SourceData = (UINTN *)((UINTN)FspsHeaderPtr->ImageBase + (UINTN)FspsHeaderPtr->CfgRegionOffset); 252 CopyMem (FspsUpdDataPtr, SourceData, (UINTN)FspsHeaderPtr->CfgRegionSize); 253 254 UpdateFspsUpdData ((VOID *)FspsUpdDataPtr); 255 256 TimeStampCounterStart = AsmReadTsc (); 257 PERF_START_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, FSP_STATUS_CODE_SILICON_INIT | FSP_STATUS_CODE_COMMON_CODE | FSP_STATUS_CODE_API_ENTRY); 258 Status = CallFspSiliconInit ((VOID *)FspsUpdDataPtr); 259 PERF_END_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, FSP_STATUS_CODE_SILICON_INIT | FSP_STATUS_CODE_COMMON_CODE | FSP_STATUS_CODE_API_EXIT); 260 DEBUG ((DEBUG_INFO, "Total time spent executing FspSiliconInitApi: %d millisecond\n", DivU64x32 (GetTimeInNanoSecond (AsmReadTsc () - TimeStampCounterStart), 1000000))); 261 262 // 263 // Reset the system if FSP API returned FSP_STATUS_RESET_REQUIRED status 264 // 265 if ((Status >= FSP_STATUS_RESET_REQUIRED_COLD) && (Status <= FSP_STATUS_RESET_REQUIRED_8)) { 266 DEBUG((DEBUG_INFO, "FspSiliconInitApi requested reset 0x%x\n", Status)); 267 CallFspWrapperResetSystem ((UINT32)Status); 268 } 269 270 if (EFI_ERROR(Status)) { 271 DEBUG ((DEBUG_ERROR, "ERROR - Failed to execute FspSiliconInitApi(), Status = %r\n", Status)); 272 } 273 DEBUG((DEBUG_INFO, "FspSiliconInit status: 0x%x\n", Status)); 274 ASSERT_EFI_ERROR (Status); 275 276 Status = TestFspSiliconInitApiOutput ((VOID *)NULL); 277 if (RETURN_ERROR (Status)) { 278 DEBUG ((DEBUG_ERROR, "ERROR - TestFspSiliconInitApiOutput () fail, Status = %r\n", Status)); 279 } 280 281 // 282 // Now FspHobList complete, process it 283 // 284 GuidHob = GetFirstGuidHob (&gFspHobGuid); 285 ASSERT (GuidHob != NULL); 286 FspHobListPtr = *(VOID **)GET_GUID_HOB_DATA (GuidHob); 287 DEBUG ((DEBUG_INFO, "FspHobListPtr - 0x%x\n", FspHobListPtr)); 288 PostFspsHobProcess (FspHobListPtr); 289 290 // 291 // Install FspSiliconInitDonePpi so that any other driver can consume this info. 292 // 293 Status = PeiServicesInstallPpi (&mPeiFspSiliconInitDonePpi); 294 ASSERT_EFI_ERROR(Status); 295 296 return Status; 297 } 298 299 /** 300 Do FSP initialization. 301 302 @return FSP initialization status. 303 **/ 304 EFI_STATUS 305 FspsWrapperInit ( 306 VOID 307 ) 308 { 309 EFI_STATUS Status; 310 EFI_BOOT_MODE BootMode; 311 312 // 313 // Register MemoryDiscovered Nofity to run FspSiliconInit 314 // 315 Status = PeiServicesNotifyPpi (&mPeiMemoryDiscoveredNotifyDesc); 316 ASSERT_EFI_ERROR (Status); 317 318 // 319 // Register EndOfPei Notify for S3 to run FSP NotifyPhase 320 // 321 PeiServicesGetBootMode (&BootMode); 322 if (BootMode == BOOT_ON_S3_RESUME) { 323 Status = PeiServicesNotifyPpi (&mS3EndOfPeiNotifyDesc); 324 ASSERT_EFI_ERROR (Status); 325 } 326 327 return EFI_SUCCESS; 328 } 329 330 /** 331 This is the entrypoint of PEIM 332 333 @param[in] FileHandle Handle of the file being invoked. 334 @param[in] PeiServices Describes the list of possible PEI Services. 335 336 @retval EFI_SUCCESS if it completed successfully. 337 **/ 338 EFI_STATUS 339 EFIAPI 340 FspsWrapperPeimEntryPoint ( 341 IN EFI_PEI_FILE_HANDLE FileHandle, 342 IN CONST EFI_PEI_SERVICES **PeiServices 343 ) 344 { 345 346 DEBUG ((DEBUG_INFO, "FspsWrapperPeimEntryPoint\n")); 347 348 FspsWrapperInit (); 349 350 return EFI_SUCCESS; 351 } 352