PciSupportLib.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /** @file
  2. Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "PiDxe.h"
  6. #include <Base.h>
  7. #include <Guid/SocketIioVariable.h>
  8. #include <Library/IoLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/UefiRuntimeServicesTableLib.h>
  11. #include "IndustryStandard/Pci.h"
  12. #include "PciSupportLib.h"
  13. PCIE_STACK mPcieStack;
  14. /**
  15. This routine is used to check whether the pci device is present
  16. @retval None
  17. **/
  18. BOOLEAN
  19. IsPciDevicePresent (
  20. IN EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo,
  21. PCI_TYPE00 *Pci,
  22. UINT8 Bus,
  23. UINT8 Device,
  24. UINT8 Func
  25. )
  26. // TODO: PciRootBridgeIo - add argument and description to function comment
  27. // TODO: Pci - add argument and description to function comment
  28. // TODO: Bus - add argument and description to function comment
  29. // TODO: Device - add argument and description to function comment
  30. // TODO: Func - add argument and description to function comment
  31. // TODO: EFI_SUCCESS - add return value to function comment
  32. // TODO: EFI_NOT_FOUND - add return value to function comment
  33. {
  34. UINT64 Address;
  35. UINT32 Dummy;
  36. EFI_STATUS Status;
  37. Dummy=0xFFFFFFFF;
  38. //
  39. // Create PCI address map in terms of Bus, Device and Func
  40. //
  41. Address = EFI_PCI_ADDRESS (Bus, Device, Func, 0);
  42. //
  43. // Read the Vendor Id register
  44. //
  45. Status = PciRootBridgeIo->Pci.Read (
  46. PciRootBridgeIo,
  47. EfiPciWidthUint32,
  48. Address,
  49. 1,
  50. Pci
  51. );
  52. if ((Pci->Hdr).VendorId == 0xffff) {
  53. /// PCIe card could have been assigned a temporary bus number.
  54. /// An write cycle can be used to try to rewrite the Bus number in the card
  55. /// Try to write the Vendor Id register, and recheck if the card is present.
  56. Status = PciRootBridgeIo->Pci.Write(
  57. PciRootBridgeIo,
  58. EfiPciWidthUint32,
  59. Address,
  60. 1,
  61. &Dummy
  62. );
  63. // Retry the previous read after the PCI cycle has been tried.
  64. Status = PciRootBridgeIo->Pci.Read (
  65. PciRootBridgeIo,
  66. EfiPciWidthUint32,
  67. Address,
  68. 1,
  69. Pci
  70. );
  71. }
  72. if (!EFI_ERROR (Status) && (Pci->Hdr).VendorId != 0xffff) {
  73. //
  74. // Read the entire config header for the device
  75. //
  76. Status = PciRootBridgeIo->Pci.Read (
  77. PciRootBridgeIo,
  78. EfiPciWidthUint32,
  79. Address,
  80. sizeof (PCI_TYPE00) / sizeof (UINT32),
  81. Pci
  82. );
  83. return TRUE;
  84. }
  85. return FALSE;
  86. }