FdtPciPcdProducerLib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /** @file
  2. FDT client library for consumers of PCI related dynamic PCDs
  3. Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Uefi.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/PcdLib.h>
  10. #include <Library/UefiBootServicesTableLib.h>
  11. #include <Protocol/FdtClient.h>
  12. //
  13. // We expect the "ranges" property of "pci-host-ecam-generic" to consist of
  14. // records like this.
  15. //
  16. #pragma pack (1)
  17. typedef struct {
  18. UINT32 Type;
  19. UINT64 ChildBase;
  20. UINT64 CpuBase;
  21. UINT64 Size;
  22. } DTB_PCI_HOST_RANGE_RECORD;
  23. #pragma pack ()
  24. #define DTB_PCI_HOST_RANGE_RELOCATABLE BIT31
  25. #define DTB_PCI_HOST_RANGE_PREFETCHABLE BIT30
  26. #define DTB_PCI_HOST_RANGE_ALIASED BIT29
  27. #define DTB_PCI_HOST_RANGE_MMIO32 BIT25
  28. #define DTB_PCI_HOST_RANGE_MMIO64 (BIT25 | BIT24)
  29. #define DTB_PCI_HOST_RANGE_IO BIT24
  30. #define DTB_PCI_HOST_RANGE_TYPEMASK (BIT31 | BIT30 | BIT29 | BIT25 | BIT24)
  31. STATIC
  32. RETURN_STATUS
  33. GetPciIoTranslation (
  34. IN FDT_CLIENT_PROTOCOL *FdtClient,
  35. IN INT32 Node,
  36. OUT UINT64 *IoTranslation
  37. )
  38. {
  39. UINT32 RecordIdx;
  40. CONST VOID *Prop;
  41. UINT32 Len;
  42. EFI_STATUS Status;
  43. UINT64 IoBase;
  44. //
  45. // Iterate over "ranges".
  46. //
  47. Status = FdtClient->GetNodeProperty (FdtClient, Node, "ranges", &Prop, &Len);
  48. if (EFI_ERROR (Status) || Len == 0 ||
  49. Len % sizeof (DTB_PCI_HOST_RANGE_RECORD) != 0) {
  50. DEBUG ((EFI_D_ERROR, "%a: 'ranges' not found or invalid\n", __FUNCTION__));
  51. return RETURN_PROTOCOL_ERROR;
  52. }
  53. for (RecordIdx = 0; RecordIdx < Len / sizeof (DTB_PCI_HOST_RANGE_RECORD);
  54. ++RecordIdx) {
  55. CONST DTB_PCI_HOST_RANGE_RECORD *Record;
  56. UINT32 Type;
  57. Record = (CONST DTB_PCI_HOST_RANGE_RECORD *)Prop + RecordIdx;
  58. Type = SwapBytes32 (Record->Type) & DTB_PCI_HOST_RANGE_TYPEMASK;
  59. if (Type == DTB_PCI_HOST_RANGE_IO) {
  60. IoBase = SwapBytes64 (Record->ChildBase);
  61. *IoTranslation = SwapBytes64 (Record->CpuBase) - IoBase;
  62. return RETURN_SUCCESS;
  63. }
  64. }
  65. return RETURN_NOT_FOUND;
  66. }
  67. RETURN_STATUS
  68. EFIAPI
  69. FdtPciPcdProducerLibConstructor (
  70. VOID
  71. )
  72. {
  73. UINT64 PciExpressBaseAddress;
  74. FDT_CLIENT_PROTOCOL *FdtClient;
  75. CONST UINT64 *Reg;
  76. UINT32 RegSize;
  77. EFI_STATUS Status;
  78. INT32 Node;
  79. RETURN_STATUS RetStatus;
  80. UINT64 IoTranslation;
  81. RETURN_STATUS PcdStatus;
  82. PciExpressBaseAddress = PcdGet64 (PcdPciExpressBaseAddress);
  83. if (PciExpressBaseAddress != MAX_UINT64) {
  84. //
  85. // Assume that the fact that PciExpressBaseAddress has been changed from
  86. // its default value of MAX_UINT64 implies that this code has been
  87. // executed already, in the context of another module. That means we can
  88. // assume that PcdPciIoTranslation has been discovered from the DT node
  89. // as well.
  90. //
  91. return EFI_SUCCESS;
  92. }
  93. Status = gBS->LocateProtocol (&gFdtClientProtocolGuid, NULL,
  94. (VOID **)&FdtClient);
  95. ASSERT_EFI_ERROR (Status);
  96. PciExpressBaseAddress = 0;
  97. Status = FdtClient->FindCompatibleNode (FdtClient, "pci-host-ecam-generic",
  98. &Node);
  99. if (!EFI_ERROR (Status)) {
  100. Status = FdtClient->GetNodeProperty (FdtClient, Node, "reg",
  101. (CONST VOID **)&Reg, &RegSize);
  102. if (!EFI_ERROR (Status) && RegSize == 2 * sizeof (UINT64)) {
  103. PciExpressBaseAddress = SwapBytes64 (*Reg);
  104. PcdStatus = PcdSetBoolS (PcdPciDisableBusEnumeration, FALSE);
  105. ASSERT_RETURN_ERROR (PcdStatus);
  106. IoTranslation = 0;
  107. RetStatus = GetPciIoTranslation (FdtClient, Node, &IoTranslation);
  108. if (!RETURN_ERROR (RetStatus)) {
  109. PcdStatus = PcdSet64S (PcdPciIoTranslation, IoTranslation);
  110. ASSERT_RETURN_ERROR (PcdStatus);
  111. } else {
  112. //
  113. // Support for I/O BARs is not mandatory, and so it does not make sense
  114. // to abort in the general case. So leave it up to the actual driver to
  115. // complain about this if it wants to, and just issue a warning here.
  116. //
  117. DEBUG ((EFI_D_WARN,
  118. "%a: 'pci-host-ecam-generic' device encountered with no I/O range\n",
  119. __FUNCTION__));
  120. }
  121. }
  122. }
  123. PcdStatus = PcdSet64S (PcdPciExpressBaseAddress, PciExpressBaseAddress);
  124. ASSERT_RETURN_ERROR (PcdStatus);
  125. return RETURN_SUCCESS;
  126. }