PciPlatformDxe.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /** @file
  2. Copyright (c) 2021, Ampere Computing LLC. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include <Uefi.h>
  6. #include <Guid/RootComplexInfoHob.h>
  7. #include <Library/DebugLib.h>
  8. #include <Library/HobLib.h>
  9. #include <Library/Ac01PcieLib.h>
  10. #include <Library/TimerLib.h>
  11. #include <Library/UefiBootServicesTableLib.h>
  12. #include <Protocol/DevicePath.h>
  13. #include <Protocol/PciHostBridgeResourceAllocation.h>
  14. #include <Protocol/PciPlatform.h>
  15. #pragma pack(1)
  16. typedef struct {
  17. ACPI_HID_DEVICE_PATH AcpiDevicePath;
  18. EFI_DEVICE_PATH_PROTOCOL EndDevicePath;
  19. } EFI_PCI_ROOT_BRIDGE_DEVICE_PATH;
  20. #pragma pack ()
  21. /**
  22. Callback funciton for EndEnumeration notification from PCI stack.
  23. @param[in] RootBridgeIndex Index to identify of PCIE Root bridge.
  24. @param[in] Phase The phase of enumeration as informed from PCI stack.
  25. **/
  26. VOID
  27. NotifyPhaseCallBack (
  28. IN UINTN RootBridgeIndex,
  29. IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE Phase
  30. )
  31. {
  32. AC01_ROOT_COMPLEX *RootComplexList;
  33. VOID *Hob;
  34. Hob = GetFirstGuidHob (&gRootComplexInfoHobGuid);
  35. if (Hob == NULL) {
  36. return;
  37. }
  38. RootComplexList = (AC01_ROOT_COMPLEX *)GET_GUID_HOB_DATA (Hob);
  39. switch (Phase) {
  40. case EfiPciHostBridgeEndEnumeration:
  41. Ac01PcieCoreEndEnumeration (&RootComplexList[RootBridgeIndex]);
  42. break;
  43. case EfiPciHostBridgeBeginEnumeration:
  44. // 100ms that help fixing completion timeout issue
  45. MicroSecondDelay (100000);
  46. break;
  47. case EfiPciHostBridgeBeginBusAllocation:
  48. case EfiPciHostBridgeEndBusAllocation:
  49. case EfiPciHostBridgeBeginResourceAllocation:
  50. case EfiPciHostBridgeAllocateResources:
  51. case EfiPciHostBridgeSetResources:
  52. case EfiPciHostBridgeFreeResources:
  53. case EfiPciHostBridgeEndResourceAllocation:
  54. case EfiMaxPciHostBridgeEnumerationPhase:
  55. break;
  56. }
  57. }
  58. /**
  59. Perform initialization by the phase indicated.
  60. @param This Pointer to the EFI_PCI_PLATFORM_PROTOCOL instance.
  61. @param HostBridge The associated PCI host bridge handle.
  62. @param Phase The phase of the PCI controller enumeration.
  63. @param ChipsetPhase Defines the execution phase of the PCI chipset driver.
  64. @retval EFI_SUCCESS Must return with success.
  65. **/
  66. EFI_STATUS
  67. EFIAPI
  68. PhaseNotify (
  69. IN EFI_PCI_PLATFORM_PROTOCOL *This,
  70. IN EFI_HANDLE HostBridge,
  71. IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE Phase,
  72. IN EFI_PCI_CHIPSET_EXECUTION_PHASE ChipsetPhase
  73. )
  74. {
  75. EFI_PCI_ROOT_BRIDGE_DEVICE_PATH *RootBridgeDevPath;
  76. EFI_HANDLE RootBridgeHandle = NULL;
  77. EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL *ResAlloc = NULL;
  78. EFI_STATUS Status;
  79. if (ChipsetPhase != ChipsetExit) {
  80. return EFI_SUCCESS;
  81. }
  82. //
  83. // Get HostBridgeInstance from HostBridge handle.
  84. //
  85. Status = gBS->HandleProtocol (
  86. HostBridge,
  87. &gEfiPciHostBridgeResourceAllocationProtocolGuid,
  88. (VOID **)&ResAlloc
  89. );
  90. while (TRUE) {
  91. Status = ResAlloc->GetNextRootBridge (ResAlloc, &RootBridgeHandle);
  92. if (EFI_ERROR (Status)) {
  93. break;
  94. }
  95. Status = gBS->HandleProtocol (
  96. RootBridgeHandle,
  97. &gEfiDevicePathProtocolGuid,
  98. (VOID **)&RootBridgeDevPath
  99. );
  100. if (EFI_ERROR (Status)) {
  101. DEBUG ((DEBUG_ERROR, "%a %d: Failed to locate RootBridge DevicePath\n", __FUNCTION__, __LINE__));
  102. break;
  103. }
  104. NotifyPhaseCallBack (RootBridgeDevPath->AcpiDevicePath.UID, Phase);
  105. }
  106. return EFI_SUCCESS;
  107. }
  108. /**
  109. The PlatformPrepController() function can be used to notify the platform driver so that
  110. it can perform platform-specific actions. No specific actions are required.
  111. Several notification points are defined at this time. More synchronization points may be
  112. added as required in the future. The PCI bus driver calls the platform driver twice for
  113. every PCI controller-once before the PCI Host Bridge Resource Allocation Protocol driver
  114. is notified, and once after the PCI Host Bridge Resource Allocation Protocol driver has
  115. been notified.
  116. This member function may not perform any error checking on the input parameters. It also
  117. does not return any error codes. If this member function detects any error condition, it
  118. needs to handle those errors on its own because there is no way to surface any errors to
  119. the caller.
  120. @param This Pointer to the EFI_PCI_PLATFORM_PROTOCOL instance.
  121. @param HostBridge The associated PCI host bridge handle.
  122. @param RootBridge The associated PCI root bridge handle.
  123. @param PciAddress The address of the PCI device on the PCI bus.
  124. @param Phase The phase of the PCI controller enumeration.
  125. @param ChipsetPhase Defines the execution phase of the PCI chipset driver.
  126. @retval EFI_SUCCESS The function completed successfully.
  127. @retval EFI_UNSUPPORTED Not supported.
  128. **/
  129. EFI_STATUS
  130. EFIAPI
  131. PlatformPrepController (
  132. IN EFI_PCI_PLATFORM_PROTOCOL *This,
  133. IN EFI_HANDLE HostBridge,
  134. IN EFI_HANDLE RootBridge,
  135. IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS PciAddress,
  136. IN EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE Phase,
  137. IN EFI_PCI_CHIPSET_EXECUTION_PHASE ChipsetPhase
  138. )
  139. {
  140. return EFI_UNSUPPORTED;
  141. }
  142. /**
  143. Set the PciPolicy as EFI_RESERVE_ISA_IO_NO_ALIAS | EFI_RESERVE_VGA_IO_NO_ALIAS.
  144. @param This The pointer to the Protocol itself.
  145. @param PciPolicy The returned Policy.
  146. @retval EFI_UNSUPPORTED Function not supported.
  147. @retval EFI_INVALID_PARAMETER Invalid PciPolicy value.
  148. **/
  149. EFI_STATUS
  150. EFIAPI
  151. GetPlatformPolicy (
  152. IN CONST EFI_PCI_PLATFORM_PROTOCOL *This,
  153. OUT EFI_PCI_PLATFORM_POLICY *PciPolicy
  154. )
  155. {
  156. return EFI_UNSUPPORTED;
  157. }
  158. /**
  159. Return a PCI ROM image for the onboard device represented by PciHandle.
  160. @param This Protocol instance pointer.
  161. @param PciHandle PCI device to return the ROM image for.
  162. @param RomImage PCI Rom Image for onboard device.
  163. @param RomSize Size of RomImage in bytes.
  164. @retval EFI_SUCCESS RomImage is valid.
  165. @retval EFI_NOT_FOUND No RomImage.
  166. **/
  167. EFI_STATUS
  168. EFIAPI
  169. GetPciRom (
  170. IN CONST EFI_PCI_PLATFORM_PROTOCOL *This,
  171. IN EFI_HANDLE PciHandle,
  172. OUT VOID **RomImage,
  173. OUT UINTN *RomSize
  174. )
  175. {
  176. return EFI_NOT_FOUND;
  177. }
  178. //
  179. // Interface defintion of PCI Platform protocol.
  180. //
  181. EFI_PCI_PLATFORM_PROTOCOL mPciPlatformProtocol = {
  182. .PlatformNotify = PhaseNotify,
  183. .PlatformPrepController = PlatformPrepController,
  184. .GetPlatformPolicy = GetPlatformPolicy,
  185. .GetPciRom = GetPciRom
  186. };
  187. /**
  188. The Entry point of the Pci Platform Driver.
  189. @param ImageHandle Handle to the image.
  190. @param SystemTable Handle to System Table.
  191. @retval EFI_STATUS Status of the function calling.
  192. **/
  193. EFI_STATUS
  194. PciPlatformDriverEntry (
  195. IN EFI_HANDLE ImageHandle,
  196. IN EFI_SYSTEM_TABLE *SystemTable
  197. )
  198. {
  199. EFI_STATUS Status;
  200. EFI_HANDLE PciPlatformHandle;
  201. //
  202. // Install on a new handle
  203. //
  204. PciPlatformHandle = NULL;
  205. Status = gBS->InstallMultipleProtocolInterfaces (
  206. &PciPlatformHandle,
  207. &gEfiPciPlatformProtocolGuid,
  208. &mPciPlatformProtocol,
  209. NULL
  210. );
  211. return Status;
  212. }