]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFsp2WrapperPkg/FspWrapperNotifyDxe/FspWrapperNotifyDxe.c
Add IntelFsp2Pkg and IntelFsp2WrapperPkg.
[mirror_edk2.git] / IntelFsp2WrapperPkg / FspWrapperNotifyDxe / FspWrapperNotifyDxe.c
diff --git a/IntelFsp2WrapperPkg/FspWrapperNotifyDxe/FspWrapperNotifyDxe.c b/IntelFsp2WrapperPkg/FspWrapperNotifyDxe/FspWrapperNotifyDxe.c
new file mode 100644 (file)
index 0000000..30c06b8
--- /dev/null
@@ -0,0 +1,250 @@
+/** @file\r
+  This driver will register two callbacks to call fsp's notifies.\r
+\r
+  Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php.\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Protocol/PciEnumerationComplete.h>\r
+\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/FspWrapperApiLib.h>\r
+#include <Library/PerformanceLib.h>\r
+#include <Library/HobLib.h>\r
+\r
+typedef\r
+EFI_STATUS\r
+(EFIAPI * ADD_PERFORMANCE_RECORDS)(\r
+  IN CONST VOID *HobStart\r
+  );\r
+\r
+struct _ADD_PERFORMANCE_RECORD_PROTOCOL {\r
+  ADD_PERFORMANCE_RECORDS          AddPerformanceRecords;\r
+};\r
+\r
+typedef struct _ADD_PERFORMANCE_RECORD_PROTOCOL ADD_PERFORMANCE_RECORD_PROTOCOL;\r
+\r
+extern EFI_GUID gAddPerfRecordProtocolGuid;\r
+extern EFI_GUID gFspHobGuid;\r
+extern EFI_GUID gFspApiPerformanceGuid;\r
+\r
+EFI_EVENT mExitBootServicesEvent     = NULL;\r
+\r
+/**\r
+  Relocate this image under 4G memory.\r
+\r
+  @param  ImageHandle  Handle of driver image.\r
+  @param  SystemTable  Pointer to system table.\r
+\r
+  @retval EFI_SUCCESS  Image successfully relocated.\r
+  @retval EFI_ABORTED  Failed to relocate image.\r
+\r
+**/\r
+EFI_STATUS\r
+RelocateImageUnder4GIfNeeded (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_SYSTEM_TABLE     *SystemTable\r
+  );\r
+\r
+/**\r
+  PciEnumerationComplete Protocol notification event handler.\r
+\r
+  @param[in] Event    Event whose notification function is being invoked.\r
+  @param[in] Context  Pointer to the notification function's context.\r
+**/\r
+VOID\r
+EFIAPI\r
+OnPciEnumerationComplete (\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
+  )\r
+{\r
+  NOTIFY_PHASE_PARAMS NotifyPhaseParams;\r
+  EFI_STATUS          Status;\r
+  VOID                *Interface;\r
+\r
+  //\r
+  // Try to locate it because gEfiPciEnumerationCompleteProtocolGuid will trigger it once when registration.\r
+  // Just return if it is not found.\r
+  //\r
+  Status = gBS->LocateProtocol (\r
+                  &gEfiPciEnumerationCompleteProtocolGuid,\r
+                  NULL,\r
+                  &Interface\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return ;\r
+  }\r
+\r
+  NotifyPhaseParams.Phase = EnumInitPhaseAfterPciEnumeration;\r
+  PERF_START_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, 0x6000);\r
+  Status = CallFspNotifyPhase (&NotifyPhaseParams);\r
+  PERF_END_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, 0x607F);\r
+  if (Status != EFI_SUCCESS) {\r
+    DEBUG((DEBUG_ERROR, "FSP NotifyPhase AfterPciEnumeration failed, status: 0x%x\n", Status));\r
+  } else {\r
+    DEBUG((DEBUG_INFO, "FSP NotifyPhase AfterPciEnumeration Success.\n"));\r
+  }\r
+}\r
+\r
+/**\r
+  Notification function of EVT_GROUP_READY_TO_BOOT event group.\r
+\r
+  This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.\r
+  When the Boot Manager is about to load and execute a boot option, it reclaims variable\r
+  storage if free size is below the threshold.\r
+\r
+  @param[in] Event        Event whose notification function is being invoked.\r
+  @param[in] Context      Pointer to the notification function's context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+OnReadyToBoot (\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
+  )\r
+{\r
+  NOTIFY_PHASE_PARAMS               NotifyPhaseParams;\r
+  EFI_STATUS                        Status;\r
+  ADD_PERFORMANCE_RECORD_PROTOCOL   *AddPerfRecordInterface;\r
+  EFI_PEI_HOB_POINTERS              Hob;\r
+  VOID                              **FspHobListPtr;\r
+\r
+  gBS->CloseEvent (Event);\r
+\r
+  NotifyPhaseParams.Phase = EnumInitPhaseReadyToBoot;\r
+  PERF_START_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, 0x4000);\r
+  Status = CallFspNotifyPhase (&NotifyPhaseParams);\r
+  PERF_END_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, 0x407F);\r
+  if (Status != EFI_SUCCESS) {\r
+    DEBUG((DEBUG_ERROR, "FSP NotifyPhase ReadyToBoot failed, status: 0x%x\n", Status));\r
+  } else {\r
+    DEBUG((DEBUG_INFO, "FSP NotifyPhase ReadyToBoot Success.\n"));\r
+  }\r
+\r
+  Status = gBS->LocateProtocol (\r
+                  &gAddPerfRecordProtocolGuid,\r
+                  NULL,\r
+                  (VOID**) &AddPerfRecordInterface\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG((DEBUG_INFO, "gAddPerfRecordProtocolGuid - Locate protocol failed\n"));\r
+    return;\r
+  } else {\r
+    Hob.Raw = GetHobList ();\r
+    if (Hob.Raw != NULL) {\r
+      Hob.Raw = GetNextGuidHob (&gFspHobGuid, Hob.Raw);\r
+      FspHobListPtr = GET_GUID_HOB_DATA(Hob.Raw);\r
+      AddPerfRecordInterface->AddPerformanceRecords((VOID *)(UINTN)(((UINT32)(UINTN)*FspHobListPtr) & 0xFFFFFFFF));\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  This stage is notified just before the firmware/Preboot environment transfers\r
+  management of all system resources to the OS or next level execution environment.\r
+\r
+  @param  Event         Event whose notification function is being invoked.\r
+  @param  Context       Pointer to the notification function's context, which is\r
+                        always zero in current implementation.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+OnEndOfFirmware (\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
+  )\r
+{\r
+  NOTIFY_PHASE_PARAMS NotifyPhaseParams;\r
+  EFI_STATUS          Status;\r
+\r
+  gBS->CloseEvent (Event);\r
+\r
+  NotifyPhaseParams.Phase = EnumInitPhaseEndOfFirmware;\r
+  PERF_START_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, 0x2000);\r
+  Status = CallFspNotifyPhase (&NotifyPhaseParams);\r
+  PERF_END_EX(&gFspApiPerformanceGuid, "EventRec", NULL, 0, 0x207F);\r
+  if (Status != EFI_SUCCESS) {\r
+    DEBUG((DEBUG_ERROR, "FSP NotifyPhase EndOfFirmware failed, status: 0x%x\n", Status));\r
+  } else {\r
+    DEBUG((DEBUG_INFO, "FSP NotifyPhase EndOfFirmware Success.\n"));\r
+  }\r
+}\r
+\r
+/**\r
+  Main entry for the FSP DXE module.\r
+\r
+  This routine registers two callbacks to call fsp's notifies.\r
+\r
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.\r
+  @param[in] SystemTable    A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS       The entry point is executed successfully.\r
+  @retval other             Some error occurs when executing this entry point.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FspWrapperNotifyDxeEntryPoint (\r
+  IN EFI_HANDLE         ImageHandle,\r
+  IN EFI_SYSTEM_TABLE   *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  EFI_EVENT  ReadyToBootEvent;\r
+  VOID       *Registration;\r
+  EFI_EVENT  ProtocolNotifyEvent;\r
+\r
+  //\r
+  // Load this driver's image to memory\r
+  //\r
+  Status = RelocateImageUnder4GIfNeeded (ImageHandle, SystemTable);\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  ProtocolNotifyEvent = EfiCreateProtocolNotifyEvent (\r
+                          &gEfiPciEnumerationCompleteProtocolGuid,\r
+                          TPL_CALLBACK,\r
+                          OnPciEnumerationComplete,\r
+                          NULL,\r
+                          &Registration\r
+                          );\r
+  ASSERT (ProtocolNotifyEvent != NULL);\r
+\r
+  Status = EfiCreateEventReadyToBootEx (\r
+             TPL_CALLBACK,\r
+             OnReadyToBoot,\r
+             NULL,\r
+             &ReadyToBootEvent\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  OnEndOfFirmware,\r
+                  NULL,\r
+                  &gEfiEventExitBootServicesGuid,\r
+                  &mExitBootServicesEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r